aboutsummaryrefslogtreecommitdiffstats
path: root/linux/lightdigest.sh
blob: 10e1c621b8888100ec4394b65a4fd604c64420bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
#
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModAuth
#

export PATH="/bin:/usr/bin:/usr/sbin:$PATH"

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

pfile="/etc/lighttpd/conf.d/lighttpd.user"
lockstart=0
remove=0

errmsg() {
    echo "$1" > /dev/stderr
}

user_check() {
    local check_user=$1
    grep "^${check_user}:" ${pfile} >& /dev/null
    return $?
}

lock() {
    local lockfile="$1.lock"

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

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

unlock() {
    local lockfile="$1.lock"

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

usage() {
    errmsg
    errmsg "lightdigest: lighttpd htdigest password generation program"
    errmsg "Scripted by JoungKyun.Kim <http://oops.org>"
    errmsg
    errmsg "Usage: $0 -[hd] -u user -p pass -r realm [-f password_file]"
    errmsg "Options:"
    errmsg "    -h          print this help messages"
    errmsg "    -u user     username"
    errmsg "    -p pass     password"
    errmsg "    -r realm    realm name"
    errmsg "    -f filename password file [default: /etc/lighttpd/conf.d/lighttpd.user"
    errmsg "    -d          remove user"
    errmsg

    local lockfile="${pfile}.lock"
    [ $lockstart -eq 1 ] && rm -f ${lockfile}

    exit 1
}

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

set -- ${opts}
for i
do
    case "$i" in
        -d) remove=1; shift;;
        -f) pfile="$2"; shift; shift;;
        -p) pass="$2"; shift; shift;;
        -r) realm="$2"; shift; shift;;
        -u) user="$2"; shift; shift;;
        --) shift; break;
    esac
done

#echo $user
#echo $realm
#echo $pass
#echo $pfile
#echo $remove

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

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

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

    pass=${newpass}
fi

lock ${pfile}

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

    [ -f "${pfile}" ] && cp -af ${pfile} ${pfile}.bak
    if [ ${already} -eq 0 ]; then
        # already exists
        perl -pi -e "s/^${user}:.*$/${user}:${realm}:${hash}/g" ${pfile}
    else
        # add new user
        echo "${user}:${realm}:${hash}" >> ${pfile}
    fi
else
    # User Remove Mode
    tmp_htdigest=$(mktemp)
    cp -af ${pfile} ${pfile}.bak
    grep -v "^${user}:" ${pfile} > ${tmp_htdigest}
    mv -f ${tmp_htdigest} ${pfile}
fi

unlock ${pfile}

exit 0