3 # Wrapper for Debian Security team
4 # Copyright (C) 2002 James Troup <james@nocrew.org>
5 # $Id: amber,v 1.4 2002-06-09 17:31:12 troup Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 ################################################################################
24 # <aj> neuro: <usual question>?
25 # <neuro> aj: PPG: the movie! july 3!
27 # <aj> (you think you can distract me, and you're right)
32 ################################################################################
34 import commands, os, pwd, re, string, sys, time;
38 ################################################################################
44 re_taint_free = re.compile(r"^['/;\-\+\.\s\w]+$");
46 ################################################################################
48 def usage (exit_code=0):
49 print """Usage: amber ADV_NUMBER CHANGES_FILE[...]
50 Install CHANGES_FILE(s) as security advisory ADV_NUMBER
52 -h, --help show this help and exit
53 -n, --no-action don't do anything
58 ################################################################################
60 def do_upload(changes_files):
63 component_mapping = {};
64 for component in Cnf.SubTree("Amber::ComponentMappings").List():
65 component_mapping[component] = Cnf["Amber::ComponentMappings::%s" % (component)];
66 uploads = {}; # uploads[uri] = file_list;
67 package_list = {} # package_list[source_name][version];
68 for changes_file in changes_files:
69 changes_file = utils.validate_changes_file_arg(changes_file);
75 # Parse the .katie file for the .changes file
76 Katie.pkg.changes_file = changes_file;
78 files = Katie.pkg.files;
79 changes = Katie.pkg.changes;
81 # Build the file list for this .changes file
82 for file in files.keys():
83 poolname = os.path.join(Cnf["Dir::Root"], Cnf["Dir::PoolRoot"],
84 utils.poolify(changes["source"], files[file]["component"]),
86 file_list.append(poolname);
87 orig_component = files[file].get("original component", files[file]["component"]);
88 components[orig_component] = "";
89 file_list.append(changes_file);
90 # Determine the upload uri for this .changes file
91 for component in components.keys():
92 upload_uri = component_mapping.get(component);
94 upload_uris[upload_uri] = "";
95 num_upload_uris = len(upload_uris.keys());
96 if num_upload_uris == 0:
97 utils.fubar("%s: No valid upload URI found from components (%s)."
98 % (changes_file, string.join(components.keys(), ", ")));
99 elif num_upload_uris > 1:
100 utils.fubar("%s: more than one upload URI (%s) from components (%s)."
101 % (changes_file, string.join(upload_uris.keys(), ", "),
102 string.join(components.keys(), ", ")));
103 upload_uri = upload_uris.keys()[0];
104 # Update the file list for the upload uri
105 if not uploads.has_key(upload_uri):
106 uploads[upload_uri] = [];
107 uploads[upload_uri].extend(file_list);
108 # Remember the suites and source name/version
109 for suite in changes["distribution"].keys():
111 # Remember the source name and version
112 if changes["architecture"].has_key("source") and \
113 changes["distribution"].has_key("testing"):
114 if not package_list.has_key(dsc["source"]):
115 package_list[dsc["source"]] = {};
116 package_list[dsc["source"]][dsc["version"]] = "";
118 if len(suites.keys()) == 1 and suites.has_key("oldstable"):
119 print "Advisory only for 'oldstable'; not uploading elsewhere.";
122 if not Options["No-Action"]:
123 answer = yes_no("Upload to files to main archive (Y/n)?");
127 for uri in uploads.keys():
128 (host, path) = string.split(uri, ":");
129 file_list = string.join(uploads[uri]);
130 print "Uploading files to %s..." % (host);
131 spawn("lftp -c 'open %s; cd %s; put %s'" % (host, path, file_list));
133 if not Options["No-Action"]:
134 filename = "%s/testing-processed" % (Cnf["Dir::Log"]);
135 file = utils.open_file(filename, 'a');
136 for source in package_list.keys():
137 for version in package_list[source].keys():
138 file.write(string.join([source, version])+'\n');
141 ################################################################################
143 # Next two functions originally written by aj and NIHishly merged into
146 def join_with_commas_and(list):
147 if len(list) == 0: return "nothing";
148 if len(list) == 1: return list[0];
149 return string.join(list[:-1], ", ") + " and " + list[-1];
151 ######################################################################
153 def make_advisory(advisory_nr, changes_files):
155 updated_pkgs = {}; # updated_pkgs[distro][arch][file] = {path,md5,size}
157 for arg in changes_files:
158 arg = utils.validate_changes_file_arg(arg);
159 Katie.pkg.changes_file = arg;
163 src = Katie.pkg.changes["source"];
164 if src not in adv_packages:
165 adv_packages = adv_packages + [src];
167 suites = Katie.pkg.changes["distribution"].keys();
169 if not updated_pkgs.has_key(suite):
170 updated_pkgs[suite] = {};
172 files = Katie.pkg.files;
173 for file in files.keys():
174 arch = files[file]["architecture"];
175 md5 = files[file]["md5sum"];
176 size = files[file]["size"];
177 poolname = Cnf["Dir::PoolRoot"] + \
178 utils.poolify(src, files[file]["component"]);
179 if arch == "source" and file[-4:] == ".dsc":
180 dscpoolname = poolname;
182 if not updated_pkgs[suite].has_key(arch):
183 updated_pkgs[suite][arch] = {}
184 updated_pkgs[suite][arch][file] = {
185 "md5": md5, "size": size,
186 "poolname": poolname };
188 dsc_files = Katie.pkg.dsc_files;
189 for file in dsc_files.keys():
191 if not dsc_files[file].has_key("files id"):
194 # otherwise, it's already in the pool and needs to be
196 md5 = dsc_files[file]["md5sum"];
197 size = dsc_files[file]["size"];
199 if not updated_pkgs[suite].has_key(arch):
200 updated_pkgs[suite][arch] = {};
201 updated_pkgs[suite][arch][file] = {
202 "md5": md5, "size": size,
203 "poolname": dscpoolname };
205 if os.environ.has_key("SUDO_UID"):
206 whoami = string.atol(os.environ["SUDO_UID"]);
208 whoami = os.getuid();
209 whoamifull = pwd.getpwuid(whoami);
210 username = string.split(whoamifull[4], ",")[0];
213 "__ADVISORY__": advisory_nr,
214 "__WHOAMI__": username,
215 "__DATE__": time.strftime("%B %d, %Y", time.gmtime(time.time())),
216 "__PACKAGE__": string.join(adv_packages,", "),
217 "__KATIE_ADDRESS__": Cnf["Dinstall::MyEmailAddress"]
220 if Cnf.has_key("Dinstall::Bcc"):
221 Subst["__BCC__"] = "Bcc: %s" % (Cnf["Dinstall::Bcc"]);
224 archive = Cnf["Archive::%s::PrimaryMirror" % (utils.where_am_i())];
225 for suite in updated_pkgs.keys():
226 suite_header = "%s %s (%s)" % (Cnf["Dinstall::MyDistribution"],
227 Cnf["Suite::%s::Version" % suite], suite);
228 adv = adv + "%s\n%s\n\n" % (suite_header, "-"*len(suite_header));
230 arches = Cnf.ValueList("Suite::%s::Architectures" % suite);
231 if "source" in arches:
232 arches.remove("source");
234 arches.remove("all");
237 adv = adv + " %s was released for %s.\n\n" % (
238 string.capitalize(suite), join_with_commas_and(arches));
240 for a in ["source", "all"] + arches:
241 if not updated_pkgs[suite].has_key(a):
245 adv = adv + " Source archives:\n\n";
247 adv = adv + " Architecture independent packages:\n\n";
249 adv = adv + " %s architecture (%s)\n\n" % (a,
250 Cnf["Architectures::%s" % a]);
252 for file in updated_pkgs[suite][a].keys():
253 adv = adv + " http://%s/%s%s\n" % (
254 archive, updated_pkgs[suite][a][file]["poolname"], file);
255 adv = adv + " Size/MD5 checksum: %8s %s\n" % (
256 updated_pkgs[suite][a][file]["size"],
257 updated_pkgs[suite][a][file]["md5"]);
259 adv = string.rstrip(adv);
261 Subst["__ADVISORY_TEXT__"] = adv;
263 adv = utils.TemplateSubst(Subst, Cnf["Dir::Templates"]+"/amber.advisory");
264 if not Options["No-Action"]:
265 utils.send_mail (adv, "");
267 print "[<Would send template advisory mail>]";
269 ######################################################################
272 global Cnf, Katie, Options;
275 Cnf = utils.get_conf();
277 Arguments = [('h', "help", "Amber::Options::Help"),
278 ('n', "no-action", "Amber::Options::No-Action")];
280 for i in [ "help", "no-action" ]:
281 Cnf["Amber::Options::%s" % (i)] = "";
283 arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
284 Options = Cnf.SubTree("Amber::Options")
285 Katie = katie.Katie(Cnf);
293 advisory_number = arguments[0];
294 changes_files = arguments[1:];
295 if advisory_number[-8:] == ".changes":
296 utils.warn("first argument must be the advisory number.");
298 for file in changes_files:
299 file = utils.validate_changes_file_arg(file);
300 return (advisory_number, changes_files);
302 ######################################################################
306 answer = string.lower(utils.our_raw_input(prompt+" "));
307 if answer == "y" or answer == "n":
310 print "Invalid answer; please try again.";
313 ######################################################################
316 if not re_taint_free.match(command):
317 utils.fubar("Invalid character in \"%s\"." % (command));
319 if Options["No-Action"]:
320 print "[%s]" % (command);
322 (result, output) = commands.getstatusoutput(command);
324 utils.fubar("Invocation of '%s' failed:\n%s\n" % (command, output), result);
326 ######################################################################
330 (advisory_number, changes_files) = init();
332 if not Options["No-Action"]:
333 print "About to install the following files: "
334 for file in changes_files:
335 print " %s" % (file);
336 answer = yes_no("Continue (Y/n)?");
340 os.chdir(Cnf["Dir::Queue::Accepted"]);
341 print "Installing packages into the archive...";
342 spawn("%s/katie -pa %s" % (Cnf["Dir::Katie"], string.join(changes_files)));
343 os.chdir(Cnf["Dir::Katie"]);
344 print "Updating file lists for apt-ftparchive...";
346 print "Updating Packages and Sources files...";
347 spawn("apt-ftparchive generate %s" % (utils.which_apt_conf_file()));
348 print "Updating Release files...";
351 if not Options["No-Action"]:
352 os.chdir(Cnf["Dir::Queue::Done"]);
354 os.chdir(Cnf["Dir::Queue::Accepted"]);
355 print "Generating template advisory...";
356 make_advisory(advisory_number, changes_files);
358 do_upload(changes_files);
360 ################################################################################
362 if __name__ == '__main__':
365 ################################################################################