summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-config-SVN/myth_user_call
blob: 0479895ce13a3568b6a32f3d07366471e9921f96 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
#Wrapper script to manage USERNAME accounts + web security
# myth_USERNAME_all -c add -u USERNAME
# myth_USERNAME_all -c delete -u USERNAME
# myth_USERNAME_all -c pass  -u USERNAME -p pass
# myth_USERNAME_all -c web  -u USERNAME -p pass

INIT_CHECK=TRUE
FULL_CALL="$@"
function CHROOT_CHECK {
    INIT=`ps -p 1 -o comm=`
    if [ x$INIT = xrunit ]
    then
       CHROOT_NEEDED=FALSE
    else
       CHROOT_NEEDED=TRUE
    fi
}

function store_commands () {
    echo "$FULL_CALL" >> /root/myth_user_call.out
    chmod 600 /root/myth_user_call.out
}


function add_user() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to add user"
        store_commands
    else
        echo "adding user $USERNAME"
	useradd -m -s /bin/bash $USERNAME -G audio,video,optical,storage,users
    fi

}


function del_user() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to delete user"
        store_commands
    else
        echo "removing  user $USERNAME"
        userdel $USERNAME
    fi

}


function pass_change() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to change password"
        store_commands
    else
        echo "changing password for $USERNAME"
        echo $USERNAME:$PASSWORD | chpasswd
    fi

}


function web_security {

    grep -q ${USERNAME}: /etc/lighttpd/lighttpd.user
    if [ $? = 0 ]
    then
            #delete user
            sed -i "/${USERNAME}\:/d"  /etc/lighttpd/lighttpd.user
    fi
    echo "${USERNAME}:${PASSWORD}" >> /etc/lighttpd/lighttpd.user

}

function ARG_ERR()  {
        if [  x$OPTARG = "x" ]
        then
            echo "$SWITCH NEEDS AND ARG"
            exit 11
        fi
}


function print_help {

    echo "Valid options are:"
    echo "  -c (add|delete|pass|web)"
    echo "  -u  USERNAMEname"
    echo "  -p  password"
    exit 1
}

if [ $# -eq 0 ]
then
    print_help
fi

declare -r OPTSTRING="c:u:p:i"
while getopts "$OPTSTRING"  SWITCH
do
    case $SWITCH in

            c)  ARG_ERR
                OPERATION=$OPTARG
                ;;
            u)  ARG_ERR
                USERNAME=$OPTARG
                ;;
            p)  ARG_ERR
                PASSWORD=$OPTARG
                ;;
            i)  INIT_CHECK=FALSE

    esac
done

if [ $INIT_CHECK = TRUE ]
then
    CHROOT_CHECK
else
    CHROOT_NEEDED=FALSE
fi

case $OPERATION in
    add)
            if [ x$USERNAME = x ]
            then
                print_help
            fi
            add_user
        ;;
    delete)
            if [ x$USERNAME = x ]
            then
                print_help
            fi
            del_user
        ;;
    pass)
            if [ x$USERNAME = x ]
            then
                print_help
            fi

            if [ x$PASSWORD = x ]
            then
                print_help
            fi
            pass_change
        ;;

    web)
            if [ x$USERNAME = x ]
            then
                print_help
            fi


            if [ x$PASSWORD = x ]
            then
                print_help
            fi
            echo "adding webUSERNAME $USERNAME with pass $PASSWORD"
	    web_security
            ;;
    *)  print_help
        ;;
esac