aboutsummaryrefslogtreecommitdiff
path: root/portal-gui/scripts/adduser.py
blob: f5740ba9501de6175efe660b08e703b5097ea35f (plain) (blame)
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
generated by cgit v1.2.3 (git 2.25.1) at 2025-05-29 03:06:52 +0000
 


ass="n">username=username)
    if user_set:
	print >>sys.stderr, 'user already exists'
	user = user_set[0]
    else:
	print >>sys.stderr, 'creating user'
	# FIXME: password is absent, assuming that apache auth is used.
	user = User.objects.create_user(username, email)

    conf_set = Conf.objects.filter(handle=username)
    if conf_set:
	conf = conf_set[0]
    else:
	print >>sys.stderr, 'creating conf'
	conf = Conf.objects.create(handle=username)
	conf.owner.add(user)
	conf.save()

    parent_set = conf.parents.filter(handle=parent)
    if parent_set:
	print 'parent %s is already present' % parent
    else:
	print "creating %s' parent %s" % (username, parent)
	parent = Parent.objects.create(handle=parent, conf=conf)

    myrpki_dir = '%s/%s' % (settings.MYRPKI_DATA_DIR, username)
    print 'myrpki_dir=', myrpki_dir
    if not os.path.exists(myrpki_dir):
	print 'creating ', myrpki_dir
	os.mkdir(myrpki_dir)

    # create stuf myrpki.conf enough to fool portal-gui
    myrpki_conf = myrpki_dir + '/myrpki.conf'
    if not os.path.exists(myrpki_conf):
	print 'creating ', myrpki_conf
	with open(myrpki_conf, 'w') as f:
	    print >>f, """[myrpki]
run_rpkidemo=true
run_rpkid=false
asn_csv=%(path)s/asns.csv
roa_csv=%(path)s/roas.csv
prefix_csv=%(path)s/prefixes.csv""" % { 'path': myrpki_dir }

    # create empty csv files so portal-gui doesn't barf
    for base in ['roas', 'asns', 'prefixes']:
        fname = '%s/%s.csv' % (myrpki_dir, base)
        if not os.path.exists(fname):
            print 'creating ', fname
            with open(fname, 'w') as f:
                pass # just create an empty file

    # add a password for this user to the apache passwd file if not present

    #determine where the passwd file is likely to reside
    # <prefix>/portal-gui/scripts/adduser.py
    path = os.path.realpath(sys.argv[0])
    prefix = '/'.join(path.split('/')[:-2]) # strip trailing components
    passfile = prefix+'/htpasswd'
    print 'passfile=', passfile
    if not user_has_password(passfile, username):
        print 'adding user to apache password file'
        password = getpass.getpass()
        update_apache_auth_file(passfile, username, realm, password)
    else:
        print 'user is already present in apache password file'

# vim:sw=4 ts=8