Project

General

Profile

Docs ModAuth ยป htdigest.sh.txt

htdigest.sh from this article (it's easier to grab that way) - ckujau, 2009-03-28 04:51

 
1
#!/bin/sh
2
#
3
# htdigest.sh
4
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModAuth
5
#
6
export PATH="/bin:/usr/bin:/usr/sbin:$PATH"
7

    
8
# when input ctrl-c, remove lockfile and exit
9
trap '[ $lockstart -eq 1 ] && unlock $pfile && exit 0 || exit 0' INT
10

    
11
pfile="/etc/lighttpd/conf.d/lighttpd.user"
12
lockstart=0
13
remove=0
14

    
15
errmsg() {
16
    echo "$1" > /dev/stderr
17
}
18

    
19
user_check() {
20
    check_user=$1
21
    grep "^${check_user}:" ${pfile} >& /dev/null
22
    return $?
23
}
24

    
25
lock() {
26
    lockfile="$1"
27
    lockfile="${lockfile}.lock"
28

    
29
    [ -f "${lockfile}" ] && {
30
        errmsg "WARNING: lock file ${lockfile} is already exists"
31
        errmsg "         Wait minites for end of previous working ..."
32
    }
33

    
34
    while [ -f "${lockfile}" ]; do echo >& /dev/null ; done
35
    touch ${lockfile}
36
    lockstart=1
37
}
38

    
39
unlock() {
40
    lockfile="$1"
41
    lockfile="${lockfile}.lock"
42

    
43
    [ -f "${lockfile}" ] && rm -f ${lockfile} && lockstart=0
44
}
45

    
46
usage() {
47
    errmsg
48
    errmsg "lightdigest: lighttpd htdigest password generation program"
49
    errmsg "Scripted by JoungKyun.Kim <http://oops.org>"
50
    errmsg
51
    errmsg "Usage: $0 -[hd] -u user -p pass -r realm [-f password_file]"
52
    errmsg "Options:"
53
    errmsg "    -h          print this help messages"
54
    errmsg "    -u user     username"
55
    errmsg "    -p pass     password"
56
    errmsg "    -r realm    realm name"
57
    errmsg "    -f filename password file [default: /etc/lighttpd/conf.d/lighttpd.user]"
58
    errmsg "    -d          remove user"
59
    errmsg
60

    
61
    [ $lockstart -eq 1 ] && rm -f ${pfile}.lock
62

    
63
    exit 1
64
}  
65

    
66
opts=$(getopt df:hp:r:u: $*)
67
[ $? != 0 ] && usage
68

    
69
set -- ${opts}
70
for i
71
do
72
    case "$i" in
73
        -d) remove=1; shift;;
74
        -f) pfile="$2"; shift; shift;;
75
        -p) pass="$2"; shift; shift;;
76
        -r) realm="$2"; shift; shift;;
77
        -u) user="$2"; shift; shift;;
78
        --) shift; break;
79
    esac
80
done
81

    
82
[ -z "$user" ] && errmsg "ERROR: User is none!!" && usage
83
[ ${remove} -eq 0 -a -z "${realm}" ] && errmsg "ERROR: Realm is none!!" && usage
84

    
85
if [ -z "${pass}" -a ${remove} -eq 0 ]; then
86
    echo -n "Input new password : "
87
    read newpass
88
    echo -n "Reinput password for confirm : "
89
    read renewpass
90

    
91
    if [ "${newpass}" != "${renewpass}" ]; then
92
        errmsg "ERROR: Password is not match"
93
        exit 1
94
    fi
95

    
96
    pass=${newpass}
97
fi
98

    
99
lock ${pfile}
100

    
101
if [ ${remove} -eq 0 ]; then
102
    # User Add Mode
103
    hash=$(echo -n "${user}:${realm}:${pass}" | md5sum | cut -b -32)
104
    user_check ${user}
105
    already=$?
106

    
107
    [ -f "${pfile}" ] && cp -af ${pfile} ${pfile}.bak
108
    if [ ${already} -eq 0 ]; then
109
        # already exists
110
        perl -pi -e "s/^${user}:.*$/${user}:${realm}:${hash}/g" ${pfile}
111
    else
112
        # add new user
113
        echo "${user}:${realm}:${hash}" >> ${pfile}
114
    fi
115
else
116
    # User Remove Mode
117
    tmp_htdigest="/tmp/lighttpd-htdiges.tmp.$$"
118
    cp -af ${pfile} ${pfile}.bak
119
    grep -v "^${user}:" ${pfile} > ${tmp_htdigest}
120
    mv -f ${tmp_htdigest} ${pfile}
121
fi
122

    
123
unlock ${pfile}
124

    
125
exit 0
    (1-1/1)