File: cdvpasswd

package info (click to toggle)
codeville 0.8.0-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,140 kB
  • sloc: python: 10,335; ansic: 89; sh: 62; makefile: 25
file content (138 lines) | stat: -rwxr-xr-x 3,568 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python

# Written by Ross Cohen
# See LICENSE.txt for license information

try:
    import Codeville.db
except ImportError:
    import sys, os.path
    from os.path import dirname, abspath, join
    pypath = "lib/python%d.%d/site-packages" % \
                (sys.version_info[0], sys.version_info[1])
    base   = dirname(dirname(abspath(sys.argv[0])))
    sys.path[0:0] = [ join(base, pypath) ]  # first place to look

from Codeville.passwd import Passwd
from ConfigParser import ConfigParser
from getopt import getopt, GetoptError
from getpass import getpass
from os import getcwd, path
from sys import argv, exit, platform

def print_help():
    print "Valid options are:\n\t-c <config file>\n\t-f <password file>"
    print 'Valid commands are: add, set, delete'

def run(args):
    try:
        optlist, args = getopt(args, 'c:f:')
    except GetoptError:
        print 'Bad options'
        print_help()
        return 1

    noconfig = False
    if platform == 'win32':
        noconfig = True
    else:
        config_file = '/etc/cdvserver.conf'

    # do a first pass of the command line to pick up an alternate config
    for (opt, arg) in optlist:
        if opt == '-c':
            config_file = arg
        if opt == '-f':
            noconfig = True

    config = ConfigParser()

    config.add_section('control')
    if platform == 'win32':
        config.set('control', 'datadir', getcwd())
    else:
        config.set('control', 'datadir', '/var/lib/cdvserver')

    if not noconfig:
        try:
            confighandle = open(config_file, 'rU')
        except IOError, msg:
            print 'Could not open config file: ' + str(msg)
            return 1

        config.readfp(confighandle)
        confighandle.close()

    passwd_file = path.join(config.get('control', 'datadir'), 'passwd')

    for (opt, arg) in optlist:
        if opt == '-f':
            passwd_file = arg

    if len(args) == 0:
        print_help()
        return 1

    try:
        pw = Passwd(passwd_file, create=1)
    except IOError, msg:
        print 'Could not read password file: ' + str(msg)
        return 1

    if args[0] == 'add':
        if len(args) != 2:
            print 'add takes argument <user>'
            return 1
        try:
            pw.get(args[1])
        except KeyError:
            pass
        else:
            print 'User exists'
            return 1
        password = getpass('Password: ')
        conf = getpass('Confirm password: ')
        if conf != password:
            print 'Confirmation failed'
            return 1
        pw.add(args[1], password)
        print 'User added'

    elif args[0] == 'set':
        if len(args) != 2:
            print 'set takes argument <user>'
            return 1
        try:
            pw.get(args[1])
        except KeyError:
            print 'No such user'
            return 1
        password = getpass('Password: ')
        conf = getpass('Confirm password: ')
        if conf != password:
            print 'Confirmation failed'
            return 1
        pw.set(args[1], password)
        print 'Password set'

    elif args[0] == 'delete':
        if len(args) != 2:
            print 'delete takes argument <user>'
            return 1
        try:
            pw.delete(args[1])
        except ValueError, msg:
            print str(msg)
            return 1
        print 'User deleted'
        return 0

    else:
        print 'Unknown command'
        print_help()
        return 1

    return 0

if __name__ == '__main__':
    exit(run(argv[1:]))