]> git.decadent.org.uk Git - dak.git/blob - dak/add_user.py
Merge commit 'djpig/djpig' into merge
[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, add_database_user, get_or_set_uid
26 from daklib.regexes import re_gpg_fingerprint, 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     -c, --create             create a system account for the user
42     -h, --help               show this help and exit."""
43     sys.exit(exit_code)
44
45 ################################################################################
46 # Stolen from userdir-ldap
47 # Compute a random password using /dev/urandom.
48 def GenPass():
49    # Generate a 10 character random string
50    SaltVals = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/."
51    Rand = open("/dev/urandom")
52    Password = ""
53    for i in range(0,15):
54       Password = Password + SaltVals[ord(Rand.read(1)[0]) % len(SaltVals)]
55    return Password
56
57 # Compute the MD5 crypted version of the given password
58 def HashPass(Password):
59    import crypt
60    # Hash it telling glibc to use the MD5 algorithm - if you dont have
61    # glibc then just change Salt = "$1$" to Salt = ""
62    SaltVals = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/."
63    Salt  = "$1$"
64    Rand = open("/dev/urandom")
65    for x in range(0,10):
66       Salt = Salt + SaltVals[ord(Rand.read(1)[0]) % len(SaltVals)]
67    Pass = crypt.crypt(Password,Salt)
68    if len(Pass) < 14:
69       raise "Password Error", "MD5 password hashing failed, not changing the password!"
70    return Pass
71
72 ################################################################################
73
74 def createMail(login, passwd, keyid, keyring):
75     import GnuPGInterface
76
77     message= """
78
79 Additionally there is now an account created for you.
80
81 """
82     message+= "\nYour password for the login %s is: %s\n" % (login, passwd)
83
84     gnupg = GnuPGInterface.GnuPG()
85     gnupg.options.armor = 1
86     gnupg.options.meta_interactive = 0
87     gnupg.options.extra_args.append("--no-default-keyring")
88     gnupg.options.extra_args.append("--always-trust")
89     gnupg.options.extra_args.append("--no-secmem-warning")
90     gnupg.options.extra_args.append("--keyring=%s" % keyring)
91     gnupg.options.recipients = [keyid]
92     proc = gnupg.run(['--encrypt'], create_fhs=['stdin', 'stdout'])
93     proc.handles['stdin'].write(message)
94     proc.handles['stdin'].close()
95     output = proc.handles['stdout'].read()
96     proc.handles['stdout'].close()
97     proc.wait()
98     return output
99
100 ################################################################################
101
102 def main():
103     global Cnf
104     keyrings = None
105
106     Cnf = utils.get_conf()
107
108     Arguments = [('h',"help","Add-User::Options::Help"),
109                  ('c',"create","Add-User::Options::Create"),
110                  ('k',"key","Add-User::Options::Key", "HasArg"),
111                  ('u',"user","Add-User::Options::User", "HasArg"),
112                  ]
113
114     for i in [ "help", "create" ]:
115         if not Cnf.has_key("Add-User::Options::%s" % (i)):
116             Cnf["Add-User::Options::%s" % (i)] = ""
117
118     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
119
120     Options = Cnf.SubTree("Add-User::Options")
121     if Options["help"]:
122         usage()
123
124     session = DBConn().session()
125
126     if not keyrings:
127         keyrings = Cnf.ValueList("Dinstall::GPGKeyring")
128
129 # Ignore the PGP keyring for download of new keys. Ignore errors, if key is missing it will
130 # barf with the next commands.
131     cmd = "gpg --no-secmem-warning --no-default-keyring %s --recv-keys %s" \
132            % (utils.gpg_keyring_args(keyrings), Cnf["Add-User::Options::Key"])
133     (result, output) = commands.getstatusoutput(cmd)
134
135     cmd = "gpg --with-colons --no-secmem-warning --no-auto-check-trustdb --no-default-keyring %s --with-fingerprint --list-key %s" \
136            % (utils.gpg_keyring_args(keyrings),
137               Cnf["Add-User::Options::Key"])
138     (result, output) = commands.getstatusoutput(cmd)
139     m = re_gpg_fingerprint.search(output)
140     if not m:
141         print output
142         utils.fubar("0x%s: (1) No fingerprint found in gpg output but it returned 0?\n%s" \
143                                         % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, \
144                                                                                                                                                                 " [GPG output:] ")))
145     primary_key = m.group(1)
146     primary_key = primary_key.replace(" ","")
147
148     uid = ""
149     if Cnf.has_key("Add-User::Options::User") and Cnf["Add-User::Options::User"]:
150         uid = Cnf["Add-User::Options::User"]
151         name = Cnf["Add-User::Options::User"]
152     else:
153         u = re_user_address.search(output)
154         if not u:
155             print output
156             utils.fubar("0x%s: (2) No userid found in gpg output but it returned 0?\n%s" \
157                         % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, " [GPG output:] ")))
158         uid = u.group(1)
159         n = re_user_name.search(output)
160         name = n.group(1)
161
162 # Look for all email addresses on the key.
163     emails=[]
164     for line in output.split('\n'):
165         e = re_user_mails.search(line)
166         if not e:
167             continue
168         emails.append(e.group(2))
169
170
171     print "0x%s -> %s <%s> -> %s -> %s" % (Cnf["Add-User::Options::Key"], name, emails[0], uid, primary_key)
172
173     prompt = "Add user %s with above data (y/N) ? " % (uid)
174     yn = utils.our_raw_input(prompt).lower()
175
176     if yn == "y":
177 # Create an account for the user?
178           summary = ""
179           if Cnf.FindB("Add-User::CreateAccount") or Cnf["Add-User::Options::Create"]:
180               password = GenPass()
181               pwcrypt = HashPass(password)
182               if Cnf.has_key("Add-User::GID"):
183                   cmd = "sudo /usr/sbin/useradd -g users -m -p '%s' -c '%s' -G %s %s" \
184                          % (pwcrypt, name, Cnf["Add-User::GID"], uid)
185               else:
186                   cmd = "sudo /usr/sbin/useradd -g users -m -p '%s' -c '%s' %s" \
187                          % (pwcrypt, name, uid)
188               (result, output) = commands.getstatusoutput(cmd)
189               if (result != 0):
190                    utils.fubar("Invocation of '%s' failed:\n%s\n" % (cmd, output), result)
191               try:
192                   summary+=createMail(uid, password, Cnf["Add-User::Options::Key"], Cnf["Dinstall::GPGKeyring"])
193               except:
194                   summary=""
195                   utils.warn("Could not prepare password information for mail, not sending password.")
196
197 # Now add user to the database.
198           # Note that we provide a session, so we're responsible for committing
199           uidobj = get_or_set_uid(uid, session=session)
200           uid_id = uidobj.uid_id
201           add_database_user(uid)
202           session.commit()
203 # The following two are kicked out in rhona, so we don't set them. kelly adds
204 # them as soon as she installs a package with unknown ones, so no problems to expect here.
205 # Just leave the comment in, to not think about "Why the hell aren't they added" in
206 # a year, if we ever touch uma again.
207 #          maint_id = database.get_or_set_maintainer_id(name)
208 #          session.execute("INSERT INTO fingerprint (fingerprint, uid) VALUES (:fingerprint, uid)",
209 #                          {'fingerprint': primary_key, 'uid': uid_id})
210
211 # Lets add user to the email-whitelist file if its configured.
212           if Cnf.has_key("Dinstall::MailWhiteList") and Cnf["Dinstall::MailWhiteList"] != "":
213               file = utils.open_file(Cnf["Dinstall::MailWhiteList"], "a")
214               for mail in emails:
215                   file.write(mail+'\n')
216               file.close()
217
218           print "Added:\nUid:\t %s (ID: %s)\nMaint:\t %s\nFP:\t %s" % (uid, uid_id, \
219                      name, primary_key)
220
221 # Should we send mail to the newly added user?
222           if Cnf.FindB("Add-User::SendEmail"):
223               mail = name + "<" + emails[0] +">"
224               Subst = {}
225               Subst["__NEW_MAINTAINER__"] = mail
226               Subst["__UID__"] = uid
227               Subst["__KEYID__"] = Cnf["Add-User::Options::Key"]
228               Subst["__PRIMARY_KEY__"] = primary_key
229               Subst["__FROM_ADDRESS__"] = Cnf["Dinstall::MyEmailAddress"]
230               Subst["__HOSTNAME__"] = Cnf["Dinstall::MyHost"]
231               Subst["__SUMMARY__"] = summary
232               new_add_message = utils.TemplateSubst(Subst,Cnf["Dir::Templates"]+"/add-user.added")
233               utils.send_mail(new_add_message)
234
235     else:
236           uid = None
237
238
239 #######################################################################################
240
241 if __name__ == '__main__':
242     main()
243