summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/myth_user_call.py
blob: 63915a401bae31859042b2f33408083b9db1f0fb (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#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

import pexpect, sys, time
import os, re, getopt
import mv_common




def chroot_check():
    if  not INIT_CHECK :
        return False
    cmd = '''ps -p 1 -o comm='''
    init = mv_common.runcmd_output(cmd).strip()
    if init == "runit":
        return False
    else:
        return True


def store_commands(full_call):
    file_name = "/root/myth_user_call.out"
    try:
        f = open(file_name,'a')
    except:
        print "%s could not be opened" % file_name
        sys.exit(1)
    f.write(full_call)
    f.write("\n")
    f.close()
    try:
        os.chmod(600,file_name)
    except:
        pass


def add_user(username,full_call):
    if chroot_check() :
        print "calling myth_call_user in chroot to add user"
        store_commands(full_call)
    else:
        print "Adding user %s" %username
        cmd = '''useradd -m -s /bin/bash %s -G audio,video,optical,storage,users''' %username
        mv_common.runcmd(cmd)
        cmd = ''' usermod -a -G mythtv %s ''' %username


def del_user(username,full_call):
    if chroot_check() :
        print "calling myth_call_user in chroot to del user"
        store_commands(full_call)
    else:
        print "Removing  user %s" %username
        cmd = '''userdel  %s ''' %username
        mv_common.runcmd(cmd)


def pass_change(username,password,full_call) :
    if chroot_check() :
        print "calling myth_call_user in chroot to change password"
        store_commands(full_call)
    else:
        print "changing password for %s" %username
        passwd = pexpect.spawn("/usr/bin/passwd %s" % username)

        for repeat in (1, 2):
                passwd.expect("password: ")
                passwd.sendline(password)
                time.sleep(0.1)

def web_security(username,password) :
    file_name = "/etc/lighttpd/lighttpd.user"
    file_contents = ''
    if  os.path.exists(file_name):
        try:
            f = open(file_name,'r')
            file_contents = f.readlines()
        except:
            print " couldn't read %s" % file_name
    try:
        f = open(file_name,'w')
    except:
        print "%s could not be opened for writing" % file_name
        return

    for line in file_contents:
        if not re.search(username, line):
            f.write(line)
    line = "%s:%s" %(username, password)
    f.write(line)
    f.write("\n")
    f.close()


def usage ():
    print "Valid options are:"
    print "  -c (add|delete|pass|web)"
    print "  -u  USERNAME"
    print "  -p  password"
    sys.exit(0)

def main(argv):
    global INIT_CHECK
    full_call = ""
    INIT_CHECK = True
    try:
        opts,  args = getopt.getopt(argv,  'c:u:p:ih' )
    except getopt.GetoptError, why:
        print why
        usage()
        print "exception happened"
        sys.exit(2)

    for opt,  arg in opts:
        if opt in ("-h",  "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-c") :
            module = arg
            full_call += ''' -c %s ''' %re.escape(arg)
        elif opt in ("-u") :
            username = arg
            full_call += ''' -u %s ''' %re.escape(arg)
        elif opt in ("-p") :
            password = arg
            full_call += ''' -p %s ''' %re.escape(arg)
        elif opt in ("-i") :
            INIT_CHECK = False
            full_call += ''' -i '''
    try:
        username
    except:
        usage()

    if module == "add":
        add_user(username,full_call)
    elif module == "delete":
        del_user(username,full_call)
    elif module == "pass":
        try:
            password
        except:
            print "needs a password"
            usage()
        pass_change(username,password,full_call)
    elif module == "web":
        web_security(username,password)
    else:
        usage()

if __name__ == "__main__":
   main(sys.argv[1:])