Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created August 5, 2008 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uemuraj/4077 to your computer and use it in GitHub Desktop.
Save uemuraj/4077 to your computer and use it in GitHub Desktop.
Apache ユーザの追加
#!/usr/bin/env python
# coding: utf-8
import sys
import md5
import binascii
from random import randint
from string import Template
from os import remove, rename
#
# htdigest コマンド互換の機能を持つ関数
#
t = Template('$usrid:$realm:$passwd')
def htdigest(realm, userid):
passwd = '%x%x%x%x%x%x' % (randint(0,255), randint(0,255), randint(0,255), randint(0,255), randint(0,255), randint(0,255))
params = {'realm' : realm, 'usrid' : userid, 'passwd' : passwd }
digest = md5.new(t.substitute(params)).digest()
params['passwd'] = binascii.b2a_hex(digest)
sys.stderr.write(userid + '=' + passwd + '\n')
return t.substitute(params)
#
# 引数の解釈、ユーザの指定が無ければ何もしない
#
fname = sys.argv[1]
realm = sys.argv[2]
users = sys.argv[3:]
if len(users) == 0:
sys.exit()
#
# テンポラリファイルに新しい内容を出力していく
# 最後にオリジナルファイルを削除して名前変更で入れ換える
#
dst = open(fname + '.tmp', 'w')
try:
src = open(fname)
for line in src:
for userid in users:
if line.startswith(userid + ':' + realm + ':'):
line = htdigest(realm, userid) + '\n'
users.remove(userid);
break
dst.write(line)
src.close()
remove(fname)
except IOError, error:
sys.stderr.write(str(error) + '\n')
for userid in users:
dst.write(htdigest(realm, userid) + '\n')
dst.close()
rename(fname + '.tmp', fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment