]> git.decadent.org.uk Git - dak.git/blob - dak/add_user.py
Adjust to deal with the new Debian supplementaryGid
[dak.git] / dak / add_user.py
1 #!/usr/bin/env python
2
3 """
4 Add a user to to the uid/maintainer/fingerprint table and
5 add his key to the GPGKeyring
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2004, 2009  Joerg Jaspert <joerg@ganneff.de>
9 @license: GNU General Public License version 2 or later
10 """
11
12 ################################################################################
13 # <elmo> wow, sounds like it'll be a big step up.. configuring dak on a
14 #        new machine even scares me :)
15 ################################################################################
16
17 # You don't want to read this script if you know python.
18 # I know what I say. I dont know python and I wrote it. So go and read some other stuff.
19
20 import commands
21 import sys
22 import apt_pkg
23
24 from daklib import utils
25 from daklib.dbconn import DBConn, get_or_set_uid, get_active_keyring_paths
26 from daklib.regexes import re_gpg_fingerprint_colon, re_user_address, re_user_mails, re_user_name
27
28 ################################################################################
29
30 Cnf = None
31 Logger = None
32
33 ################################################################################
34
35 def usage(exit_code=0):
36     print """Usage: add-user [OPTION]...
37 Adds a new user to the dak databases and keyrings
38
39     -k, --key                keyid of the User
40     -u, --user               userid of the User
41     -h, --help               show this help and exit."""
42     sys.exit(exit_code)
43
44 ################################################################################
45
46 def main():
47     global Cnf
48     keyrings = None
49
50     Cnf = utils.get_conf()
51
52     Arguments = [('h',"help","Add-User::Options::Help"),
53                  ('k',"key","Add-User::Options::Key", "HasArg"),
54                  ('u',"user","Add-User::Options::User", "HasArg"),
55                  ]
56
57     for i in [ "help" ]:
58         if not Cnf.has_key("Add-User::Options::%s" % (i)):
59             Cnf["Add-User::Options::%s" % (i)] = ""
60
61     apt_pkg.parse_commandline(Cnf, Arguments, sys.argv)
62
63     Options = Cnf.subtree("Add-User::Options")
64     if Options["help"]:
65         usage()
66
67     session = DBConn().session()
68
69     if not keyrings:
70         keyrings = get_active_keyring_paths()
71
72     cmd = "gpg --with-colons --no-secmem-warning --no-auto-check-trustdb --no-default-keyring %s --with-fingerprint --list-key %s" \
73            % (utils.gpg_keyring_args(keyrings),
74               Cnf["Add-User::Options::Key"])
75     (result, output) = commands.getstatusoutput(cmd)
76     m = re_gpg_fingerprint_colon.search(output)
77     if not m:
78         print output
79         utils.fubar("0x%s: (1) No fingerprint found in gpg output but it returned 0?\n%s" \
80                                         % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, \
81                                                                                                                                                                 " [GPG output:] ")))
82     primary_key = m.group(1)
83     primary_key = primary_key.replace(" ","")
84
85     uid = ""
86     if Cnf.has_key("Add-User::Options::User") and Cnf["Add-User::Options::User"]:
87         uid = Cnf["Add-User::Options::User"]
88         name = Cnf["Add-User::Options::User"]
89     else:
90         u = re_user_address.search(output)
91         if not u:
92             print output
93             utils.fubar("0x%s: (2) No userid found in gpg output but it returned 0?\n%s" \
94                         % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, " [GPG output:] ")))
95         uid = u.group(1)
96         n = re_user_name.search(output)
97         name = n.group(1)
98
99 # Look for all email addresses on the key.
100     emails=[]
101     for line in output.split('\n'):
102         e = re_user_mails.search(line)
103         if not e:
104             continue
105         emails.append(e.group(2))
106
107     print "0x%s -> %s <%s> -> %s -> %s" % (Cnf["Add-User::Options::Key"], name, emails[0], uid, primary_key)
108
109     prompt = "Add user %s with above data (y/N) ? " % (uid)
110     yn = utils.our_raw_input(prompt).lower()
111
112     if yn == "y":
113         # Create an account for the user?
114         summary = ""
115
116         # Now add user to the database.
117         # Note that we provide a session, so we're responsible for committing
118         uidobj = get_or_set_uid(uid, session=session)
119         uid_id = uidobj.uid_id
120         session.commit()
121
122         # Lets add user to the email-whitelist file if its configured.
123         if Cnf.has_key("Dinstall::MailWhiteList") and Cnf["Dinstall::MailWhiteList"] != "":
124             f = utils.open_file(Cnf["Dinstall::MailWhiteList"], "a")
125             for mail in emails:
126                 f.write(mail+'\n')
127             f.close()
128
129         print "Added:\nUid:\t %s (ID: %s)\nMaint:\t %s\nFP:\t %s" % (uid, uid_id, \
130                      name, primary_key)
131
132         # Should we send mail to the newly added user?
133         if Cnf.find_b("Add-User::SendEmail"):
134             mail = name + "<" + emails[0] +">"
135             Subst = {}
136             Subst["__NEW_MAINTAINER__"] = mail
137             Subst["__UID__"] = uid
138             Subst["__KEYID__"] = Cnf["Add-User::Options::Key"]
139             Subst["__PRIMARY_KEY__"] = primary_key
140             Subst["__FROM_ADDRESS__"] = Cnf["Dinstall::MyEmailAddress"]
141             Subst["__ADMIN_ADDRESS__"] = Cnf["Dinstall::MyAdminAddress"]
142             Subst["__HOSTNAME__"] = Cnf["Dinstall::MyHost"]
143             Subst["__DISTRO__"] = Cnf["Dinstall::MyDistribution"]
144             Subst["__SUMMARY__"] = summary
145             new_add_message = utils.TemplateSubst(Subst,Cnf["Dir::Templates"]+"/add-user.added")
146             utils.send_mail(new_add_message)
147
148     else:
149         uid = None
150
151 #######################################################################################
152
153 if __name__ == '__main__':
154     main()