Package dak :: Module add_user
[hide private]
[frames] | no frames]

Source Code for Module dak.add_user

  1  #! /usr/bin/env python3 
  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 subprocess 
 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   
36 -def usage(exit_code=0):
37 print("""Usage: add-user [OPTION]... 38 Adds a new user to the dak databases and keyrings 39 40 -k, --key keyid of the User 41 -u, --user userid of the User 42 -h, --help show this help and exit.""") 43 sys.exit(exit_code)
44 45 ################################################################################ 46 47
48 -def main():
49 global Cnf 50 keyrings = None 51 52 Cnf = utils.get_conf() 53 54 Arguments = [('h', "help", "Add-User::Options::Help"), 55 ('k', "key", "Add-User::Options::Key", "HasArg"), 56 ('u', "user", "Add-User::Options::User", "HasArg"), 57 ] 58 59 for i in ["help"]: 60 key = "Add-User::Options::%s" % i 61 if key not in Cnf: 62 Cnf[key] = "" 63 64 apt_pkg.parse_commandline(Cnf, Arguments, sys.argv) 65 66 Options = Cnf.subtree("Add-User::Options") 67 if Options["help"]: 68 usage() 69 70 session = DBConn().session() 71 72 if not keyrings: 73 keyrings = get_active_keyring_paths() 74 75 cmd = ["gpg", "--with-colons", "--no-secmem-warning", 76 "--no-auto-check-trustdb", "--with-fingerprint", 77 "--no-default-keyring"] 78 cmd.extend(utils.gpg_keyring_args(keyrings)) 79 cmd.extend(["--list-key", "--", Cnf["Add-User::Options::Key"]]) 80 output = subprocess.check_output(cmd).rstrip() 81 m = re_gpg_fingerprint_colon.search(output) 82 if not m: 83 print(output) 84 utils.fubar("0x%s: (1) No fingerprint found in gpg output but it returned 0?\n%s" 85 % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, 86 " [GPG output:] "))) 87 primary_key = m.group(1) 88 primary_key = primary_key.replace(" ", "") 89 90 uid = "" 91 if "Add-User::Options::User" in Cnf and Cnf["Add-User::Options::User"]: 92 uid = Cnf["Add-User::Options::User"] 93 name = Cnf["Add-User::Options::User"] 94 else: 95 u = re_user_address.search(output) 96 if not u: 97 print(output) 98 utils.fubar("0x%s: (2) No userid found in gpg output but it returned 0?\n%s" 99 % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, " [GPG output:] "))) 100 uid = u.group(1) 101 n = re_user_name.search(output) 102 name = n.group(1) 103 104 # Look for all email addresses on the key. 105 emails = [] 106 for line in output.split('\n'): 107 e = re_user_mails.search(line) 108 if not e: 109 continue 110 emails.append(e.group(2)) 111 112 print("0x%s -> %s <%s> -> %s -> %s" % (Cnf["Add-User::Options::Key"], name, emails[0], uid, primary_key)) 113 114 prompt = "Add user %s with above data (y/N) ? " % (uid) 115 yn = utils.input_or_exit(prompt).lower() 116 117 if yn == "y": 118 # Create an account for the user? 119 summary = "" 120 121 # Now add user to the database. 122 # Note that we provide a session, so we're responsible for committing 123 uidobj = get_or_set_uid(uid, session=session) 124 uid_id = uidobj.uid_id 125 session.commit() 126 127 # Lets add user to the email-whitelist file if its configured. 128 if "Dinstall::MailWhiteList" in Cnf and Cnf["Dinstall::MailWhiteList"] != "": 129 with open(Cnf["Dinstall::MailWhiteList"], "a") as f: 130 for mail in emails: 131 f.write(mail + '\n') 132 133 print("Added:\nUid:\t %s (ID: %s)\nMaint:\t %s\nFP:\t %s" % (uid, uid_id, 134 name, primary_key)) 135 136 # Should we send mail to the newly added user? 137 if Cnf.find_b("Add-User::SendEmail"): 138 mail = name + "<" + emails[0] + ">" 139 Subst = {} 140 Subst["__NEW_MAINTAINER__"] = mail 141 Subst["__UID__"] = uid 142 Subst["__KEYID__"] = Cnf["Add-User::Options::Key"] 143 Subst["__PRIMARY_KEY__"] = primary_key 144 Subst["__FROM_ADDRESS__"] = Cnf["Dinstall::MyEmailAddress"] 145 Subst["__ADMIN_ADDRESS__"] = Cnf["Dinstall::MyAdminAddress"] 146 Subst["__HOSTNAME__"] = Cnf["Dinstall::MyHost"] 147 Subst["__DISTRO__"] = Cnf["Dinstall::MyDistribution"] 148 Subst["__SUMMARY__"] = summary 149 new_add_message = utils.TemplateSubst(Subst, Cnf["Dir::Templates"] + "/add-user.added") 150 utils.send_mail(new_add_message) 151 152 else: 153 uid = None
154 155 ####################################################################################### 156 157 158 if __name__ == '__main__': 159 main() 160