#!/usr/bin/env ruby22 # coding: utf-8 require 'sqlite3' if ARGV[1] == nil STDERR.puts "第1引数にユーザ名(届くメイルアドレス)を" STDERR.puts "第2引数にデータベースファイルを指定して下さい。" exit(2) end uname = ARGV[0] # ユーザ名 string = [*'a'..'z', *'A'..'Z', *'*'..'9'] # 英数記号($が入らないように) salt = string.sample(4).join # ランダム文字列のSALT pswd = string.sample(9).join # ランダム文字列の初期パスワード crpt = pswd.crypt("$1$"+salt) # MD5化 db = SQLite3::Database.new(ARGV[1]) r = db.execute("select uid, email from user where uname=?", uname)[0] if r # 既存ユーザの場合 r=[uid, email] db.execute("update user set pswd=? where uid=?", crpt, r[0]) else # 新規作成 db.execute("insert into user(uname, pswd, email, name) values(?,?,?,?)", uname, crpt, uname, uname) end printf("Set pswd for [%s] to [%s]\n", uname, pswd) if ENV["HTTP_HOST"] # パスワード管理しているURLを送付 printf("(http://%s%s)\n", ENV["HTTP_HOST"], ENV["REQUEST_URI"]) end