1#! /usr/bin/env python3
3"""
4Add a user to to the uid/maintainer/fingerprint table and
5add his key to the GPGKeyring
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"""
12################################################################################
13# <elmo> wow, sounds like it'll be a big step up.. configuring dak on a
14# new machine even scares me :)
15################################################################################
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.
20import subprocess
21import sys
22import apt_pkg
23from typing import NoReturn
25from daklib import utils
26from daklib.dbconn import DBConn, get_or_set_uid, get_active_keyring_paths
27from daklib.regexes import re_gpg_fingerprint_colon, re_user_address, re_user_mails, re_user_name
29################################################################################
31Cnf = None
32Logger = None
34################################################################################
37def usage(exit_code: int = 0) -> NoReturn:
38 print("""Usage: add-user [OPTION]...
39Adds a new user to the dak databases and keyrings
41 -k, --key keyid of the User
42 -u, --user userid of the User
43 -h, --help show this help and exit.""")
44 sys.exit(exit_code)
46################################################################################
49def main():
50 global Cnf
51 keyrings = None
53 Cnf = utils.get_conf()
55 Arguments = [('h', "help", "Add-User::Options::Help"),
56 ('k', "key", "Add-User::Options::Key", "HasArg"),
57 ('u', "user", "Add-User::Options::User", "HasArg"),
58 ]
60 for i in ["help"]:
61 key = "Add-User::Options::%s" % i
62 if key not in Cnf: 62 ↛ 60line 62 didn't jump to line 60, because the condition on line 62 was never false
63 Cnf[key] = ""
65 apt_pkg.parse_commandline(Cnf, Arguments, sys.argv)
67 Options = Cnf.subtree("Add-User::Options")
68 if Options["help"]: 68 ↛ 71line 68 didn't jump to line 71, because the condition on line 68 was never false
69 usage()
71 session = DBConn().session()
73 if not keyrings:
74 keyrings = get_active_keyring_paths()
76 cmd = ["gpg", "--with-colons", "--no-secmem-warning",
77 "--no-auto-check-trustdb", "--with-fingerprint",
78 "--no-default-keyring"]
79 cmd.extend(utils.gpg_keyring_args(keyrings))
80 cmd.extend(["--list-key", "--", Cnf["Add-User::Options::Key"]])
81 output = subprocess.check_output(cmd).rstrip()
82 m = re_gpg_fingerprint_colon.search(output)
83 if not m:
84 print(output)
85 utils.fubar("0x%s: (1) No fingerprint found in gpg output but it returned 0?\n%s"
86 % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output,
87 " [GPG output:] ")))
88 primary_key = m.group(1)
89 primary_key = primary_key.replace(" ", "")
91 uid = ""
92 if "Add-User::Options::User" in Cnf and Cnf["Add-User::Options::User"]:
93 uid = Cnf["Add-User::Options::User"]
94 name = Cnf["Add-User::Options::User"]
95 else:
96 u = re_user_address.search(output)
97 if not u:
98 print(output)
99 utils.fubar("0x%s: (2) No userid found in gpg output but it returned 0?\n%s"
100 % (Cnf["Add-User::Options::Key"], utils.prefix_multi_line_string(output, " [GPG output:] ")))
101 uid = u.group(1)
102 n = re_user_name.search(output)
103 name = n.group(1)
105# Look for all email addresses on the key.
106 emails = []
107 for line in output.split('\n'):
108 e = re_user_mails.search(line)
109 if not e:
110 continue
111 emails.append(e.group(2))
113 print("0x%s -> %s <%s> -> %s -> %s" % (Cnf["Add-User::Options::Key"], name, emails[0], uid, primary_key))
115 prompt = "Add user %s with above data (y/N) ? " % (uid)
116 yn = utils.input_or_exit(prompt).lower()
118 if yn == "y":
119 # Create an account for the user?
120 summary = ""
122 # Now add user to the database.
123 # Note that we provide a session, so we're responsible for committing
124 uidobj = get_or_set_uid(uid, session=session)
125 uid_id = uidobj.uid_id
126 session.commit()
128 # Lets add user to the email-whitelist file if its configured.
129 if "Dinstall::MailWhiteList" in Cnf and Cnf["Dinstall::MailWhiteList"] != "":
130 with open(Cnf["Dinstall::MailWhiteList"], "a") as f:
131 for mail in emails:
132 f.write(mail + '\n')
134 print("Added:\nUid:\t %s (ID: %s)\nMaint:\t %s\nFP:\t %s" % (uid, uid_id,
135 name, primary_key))
137 # Should we send mail to the newly added user?
138 if Cnf.find_b("Add-User::SendEmail"):
139 mail = name + "<" + emails[0] + ">"
140 Subst = {}
141 Subst["__NEW_MAINTAINER__"] = mail
142 Subst["__UID__"] = uid
143 Subst["__KEYID__"] = Cnf["Add-User::Options::Key"]
144 Subst["__PRIMARY_KEY__"] = primary_key
145 Subst["__FROM_ADDRESS__"] = Cnf["Dinstall::MyEmailAddress"]
146 Subst["__ADMIN_ADDRESS__"] = Cnf["Dinstall::MyAdminAddress"]
147 Subst["__HOSTNAME__"] = Cnf["Dinstall::MyHost"]
148 Subst["__DISTRO__"] = Cnf["Dinstall::MyDistribution"]
149 Subst["__SUMMARY__"] = summary
150 new_add_message = utils.TemplateSubst(Subst, Cnf["Dir::Templates"] + "/add-user.added")
151 utils.send_mail(new_add_message)
153 else:
154 uid = None
156#######################################################################################
159if __name__ == '__main__':
160 main()