debianqueued 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. #!/usr/bin/perl -w
  2. #
  3. # debianqueued -- daemon for managing Debian upload queues
  4. #
  5. # Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  6. # Copyright (C) 2001-2007 Ryan Murray <rmurray@debian.org>
  7. # Copyright (C) 2008 Thomas Viehmann <tv@beamnet.de>
  8. #
  9. # This program is free software. You can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License as
  11. # published by the Free Software Foundation: either version 2 or
  12. # (at your option) any later version.
  13. # This program comes with ABSOLUTELY NO WARRANTY!
  14. #
  15. require 5.002;
  16. no lib '.';
  17. use strict;
  18. use POSIX;
  19. use POSIX qw( strftime sys_stat_h sys_wait_h signal_h );
  20. use Net::Ping;
  21. use Net::FTP;
  22. use Socket qw( PF_INET AF_INET SOCK_STREAM );
  23. use Config;
  24. use Sys::Hostname;
  25. use File::Copy;
  26. use Digest::MD5;
  27. setlocale(&POSIX::LC_ALL, "C");
  28. $ENV{"LC_ALL"} = "C";
  29. # ---------------------------------------------------------------------------
  30. # configuration
  31. # ---------------------------------------------------------------------------
  32. package conf;
  33. ( $conf::queued_dir = ( ( $0 !~ m,^/, ) ? POSIX::getcwd() . "/" : "" ) . $0 )
  34. =~ s,/[^/]+$,,;
  35. require "$conf::queued_dir/config";
  36. my $junk = $conf::debug; # avoid spurious warnings about unused vars
  37. $junk = $conf::ssh_key_file;
  38. $junk = $conf::stray_remove_timeout;
  39. $junk = $conf::problem_report_timeout;
  40. $junk = $conf::queue_delay;
  41. $junk = $conf::keep_files;
  42. $junk = $conf::valid_files;
  43. $junk = $conf::max_upload_retries;
  44. $junk = $conf::upload_delay_1;
  45. $junk = $conf::upload_delay_2;
  46. $junk = $conf::check_md5sum;
  47. #$junk = $conf::ls;
  48. $junk = $conf::ftpdebug;
  49. $junk = $conf::ftptimeout;
  50. $junk = @conf::nonus_packages;
  51. $junk = @conf::test_binaries;
  52. $junk = @conf::maintainer_mail;
  53. $junk = @conf::targetdir_delayed;
  54. $junk = $conf::mail ||= '/usr/sbin/sendmail';
  55. $junk = $conf::overridemail;
  56. $conf::target = "localhost" if $conf::upload_method eq "copy";
  57. package main;
  58. ( $main::progname = $0 ) =~ s,.*/,,;
  59. ($main::hostname, undef, undef, undef, undef) = gethostbyname(hostname());
  60. my %packages = ();
  61. my $re_file_safe_prefix = qr/\A([a-zA-Z0-9.][a-zA-Z0-9_.:~+-]*)/s;
  62. my $re_file_safe = qr/$re_file_safe_prefix\z/s;
  63. # extract -r and -k args
  64. $main::arg = "";
  65. if ( @ARGV == 1 && $ARGV[0] =~ /^-[rk]$/ ) {
  66. $main::arg = ( $ARGV[0] eq '-k' ) ? "kill" : "restart";
  67. shift @ARGV;
  68. }
  69. # test for another instance of the queued already running
  70. my ( $pid, $delayed_dirs, $adelayedcore );
  71. if ( open( PIDFILE, "<", $conf::pidfile ) ) {
  72. chomp( $pid = <PIDFILE> );
  73. close(PIDFILE);
  74. if ( !$pid ) {
  75. # remove stale pid file
  76. unlink($conf::pidfile);
  77. } elsif ($main::arg) {
  78. local ($|) = 1;
  79. print "Killing running daemon (pid $pid) ...";
  80. kill( 15, $pid );
  81. my $cnt = 20;
  82. while ( kill( 0, $pid ) && $cnt-- > 0 ) {
  83. sleep 1;
  84. print ".";
  85. }
  86. if ( kill( 0, $pid ) ) {
  87. print " failed!\nProcess $pid still running.\n";
  88. exit 1;
  89. }
  90. print "ok\n";
  91. if ( -e "$conf::incoming/core" ) {
  92. unlink("$conf::incoming/core");
  93. print "(Removed core file)\n";
  94. }
  95. for ( $delayed_dirs = 0 ;
  96. $delayed_dirs <= $conf::max_delayed ;
  97. $delayed_dirs++ )
  98. {
  99. $adelayedcore =
  100. sprintf( "$conf::incoming_delayed/core", $delayed_dirs );
  101. if ( -e $adelayedcore ) {
  102. unlink($adelayedcore);
  103. print "(Removed core file)\n";
  104. }
  105. } ## end for ( $delayed_dirs = 0...
  106. exit 0 if $main::arg eq "kill";
  107. } else {
  108. die "Another $main::progname is already running (pid $pid)\n"
  109. if $pid && kill( 0, $pid );
  110. }
  111. } elsif ( $main::arg eq "kill" ) {
  112. die "No daemon running\n";
  113. } elsif ( $main::arg eq "restart" ) {
  114. print "(No daemon running; starting anyway)\n";
  115. }
  116. # if started without arguments (initial invocation), then fork
  117. if ( !@ARGV ) {
  118. # now go to background
  119. die "$main::progname: fork failed: $!\n"
  120. unless defined( $pid = fork );
  121. if ($pid) {
  122. # parent: wait for signal from child (SIGCHLD or SIGUSR1) and exit
  123. my $sigset = POSIX::SigSet->new();
  124. $sigset->emptyset();
  125. $SIG{"CHLD"} = sub { };
  126. $SIG{"USR1"} = sub { };
  127. POSIX::sigsuspend($sigset);
  128. waitpid( $pid, WNOHANG );
  129. if ( kill( 0, $pid ) ) {
  130. print "Daemon (on $main::hostname) started in background (pid $pid)\n";
  131. exit 0;
  132. } else {
  133. exit 1;
  134. }
  135. } else {
  136. # child
  137. setsid;
  138. if ( $conf::upload_method eq "ssh" ) {
  139. # exec an ssh-agent that starts us again
  140. # force shell to be /bin/sh, ssh-agent may base its decision
  141. # whether to use a fd or a Unix socket on the shell...
  142. $ENV{"SHELL"} = "/bin/sh";
  143. exec $conf::ssh_agent, $0, "startup", getppid();
  144. die "$main::progname: Could not exec $conf::ssh_agent: $!\n";
  145. } else {
  146. # no need to exec, just set up @ARGV as expected below
  147. @ARGV = ( "startup", getppid() );
  148. }
  149. } ## end else [ if ($pid)
  150. } ## end if ( !@ARGV )
  151. die "Please start without any arguments.\n"
  152. if @ARGV != 2 || $ARGV[0] ne "startup";
  153. my $parent_pid = $ARGV[1];
  154. do {
  155. my $version;
  156. ( $version = 'Release: 0.95' ) =~ s/\$ ?//g;
  157. print "debianqueued $version\n";
  158. };
  159. # check if all programs exist
  160. my $prg;
  161. foreach $prg ( $conf::gpg, $conf::ssh, $conf::scp, $conf::ssh_agent,
  162. $conf::ssh_add, $conf::mail, $conf::mkfifo )
  163. {
  164. die "Required program $prg doesn't exist or isn't executable\n"
  165. if !-x $prg;
  166. # check for correct upload method
  167. die "Bad upload method '$conf::upload_method'.\n"
  168. if $conf::upload_method ne "ssh"
  169. && $conf::upload_method ne "ftp"
  170. && $conf::upload_method ne "copy";
  171. die "No keyrings\n" if !@conf::keyrings;
  172. } ## end foreach $prg ( $conf::gpg, ...
  173. die "statusfile path must be absolute."
  174. if $conf::statusfile !~ m,^/,;
  175. die "upload and target queue paths must be absolute."
  176. if $conf::incoming !~ m,^/,
  177. || $conf::incoming_delayed !~ m,^/,
  178. || $conf::targetdir !~ m,^/,
  179. || $conf::targetdir_delayed !~ m,^/,;
  180. # ---------------------------------------------------------------------------
  181. # initializations
  182. # ---------------------------------------------------------------------------
  183. # prototypes
  184. sub calc_delta();
  185. sub check_dir();
  186. sub get_filelist_from_known_good_changes($);
  187. sub age_delayed_queues();
  188. sub process_changes($\@);
  189. sub process_commands($);
  190. sub age_delayed_queues();
  191. sub is_on_target($\@);
  192. sub copy_to_target(@);
  193. sub pgp_check($);
  194. sub check_alive(;$);
  195. sub check_incoming_writable();
  196. sub fork_statusd();
  197. sub write_status_file();
  198. sub print_status($$$$$$);
  199. sub format_status_num(\$$);
  200. sub format_status_str(\$$);
  201. sub send_status();
  202. sub ftp_open();
  203. sub ftp_cmd($@);
  204. sub ftp_close();
  205. sub ftp_response();
  206. sub ftp_code();
  207. sub ftp_error();
  208. sub ssh_cmd($);
  209. sub scp_cmd(@);
  210. sub check_alive(;$);
  211. sub check_incoming_writable();
  212. sub rm(@);
  213. sub md5sum($);
  214. sub msg($@);
  215. sub debug(@);
  216. sub init_mail(;$);
  217. sub finish_mail();
  218. sub send_mail($$$);
  219. sub try_to_get_mail_addr($$);
  220. sub format_time();
  221. sub print_time($);
  222. sub block_signals();
  223. sub unblock_signals();
  224. sub close_log($);
  225. sub kid_died($);
  226. sub restart_statusd();
  227. sub fatal_signal($);
  228. $ENV{"PATH"} = "/bin:/usr/bin";
  229. $ENV{"IFS"} = "" if defined( $ENV{"IFS"} && $ENV{"IFS"} ne "" );
  230. # constants for stat
  231. sub ST_DEV() { 0 }
  232. sub ST_INO() { 1 }
  233. sub ST_MODE() { 2 }
  234. sub ST_NLINK() { 3 }
  235. sub ST_UID() { 4 }
  236. sub ST_GID() { 5 }
  237. sub ST_RDEV() { 6 }
  238. sub ST_SIZE() { 7 }
  239. sub ST_ATIME() { 8 }
  240. sub ST_MTIME() { 9 }
  241. sub ST_CTIME() { 10 }
  242. # fixed lengths of data items passed over status pipe
  243. sub STATNUM_LEN() { 30 }
  244. sub STATSTR_LEN() { 128 }
  245. # init list of signals
  246. defined $Config{sig_name}
  247. or die "$main::progname: No signal list defined!\n";
  248. my $i = 0;
  249. my $name;
  250. foreach $name ( split( ' ', $Config{sig_name} ) ) {
  251. $main::signo{$name} = $i++;
  252. }
  253. @main::fatal_signals = qw( INT QUIT ILL TRAP ABRT BUS FPE USR2 SEGV PIPE
  254. TERM XCPU XFSZ PWR );
  255. $main::block_sigset = POSIX::SigSet->new;
  256. $main::block_sigset->addset( $main::signo{"INT"} );
  257. $main::block_sigset->addset( $main::signo{"TERM"} );
  258. # some constant net stuff
  259. $main::tcp_proto = ( getprotobyname('tcp') )[2]
  260. or die "Cannot get protocol number for 'tcp'\n";
  261. my $used_service = ( $conf::upload_method eq "ssh" ) ? "ssh" : "ftp";
  262. $main::echo_port = ( getservbyname( $used_service, 'tcp' ) )[2]
  263. or die "Cannot get port number for service '$used_service'\n";
  264. # clear queue of stored mails
  265. @main::stored_mails = ();
  266. # run ssh-add to bring the key into the agent (will use stdin/stdout)
  267. if ( $conf::upload_method eq "ssh" ) {
  268. system "$conf::ssh_add $conf::ssh_key_file"
  269. and die "$main::progname: Running $conf::ssh_add failed "
  270. . "(exit status ", $? >> 8, ")\n";
  271. }
  272. # change to queue dir
  273. chdir($conf::incoming)
  274. or die "$main::progname: cannot cd to $conf::incoming: $!\n";
  275. # needed before /dev/null redirects, some system send a SIGHUP when loosing
  276. # the controlling tty
  277. $SIG{"HUP"} = "IGNORE";
  278. # open logfile, make it unbuffered
  279. open( LOG, ">>", $conf::logfile )
  280. or die "Cannot open my logfile $conf::logfile: $!\n";
  281. chmod( 0644, $conf::logfile )
  282. or die "Cannot set modes of $conf::logfile: $!\n";
  283. select( ( select(LOG), $| = 1 )[0] );
  284. sleep(1);
  285. $SIG{"HUP"} = \&close_log;
  286. # redirect stdin, ... to /dev/null
  287. open( STDIN, "<", "/dev/null" )
  288. or die "$main::progname: Can't redirect stdin to /dev/null: $!\n";
  289. open( STDOUT, ">&", \*LOG )
  290. or die "$main::progname: Can't redirect stdout to $conf::logfile: $!\n";
  291. open( STDERR, ">&", \*LOG )
  292. or die "$main::progname: Can't redirect stderr to $conf::logfile: $!\n";
  293. # ok, from this point usually no "die" anymore, stderr is gone!
  294. msg( "log", "daemon (pid $$) (on $main::hostname) started\n" );
  295. # initialize variables used by send_status before launching the status daemon
  296. $main::dstat = "i";
  297. format_status_num( $main::next_run, time + 10 );
  298. format_status_str( $main::current_changes, "" );
  299. check_alive();
  300. $main::incoming_writable = 1; # assume this for now
  301. # start the daemon watching the 'status' FIFO
  302. if ( $conf::statusfile && $conf::statusdelay == 0 ) {
  303. $main::statusd_pid = fork_statusd();
  304. $SIG{"CHLD"} = \&kid_died; # watch out for dead status daemon
  305. # SIGUSR1 triggers status info
  306. $SIG{"USR1"} = \&send_status;
  307. } ## end if ( $conf::statusfile...
  308. $main::maind_pid = $$;
  309. END {
  310. kill( $main::signo{"ABRT"}, $$ )
  311. if defined $main::signo{"ABRT"};
  312. }
  313. # write the pid file
  314. open( PIDFILE, ">", $conf::pidfile )
  315. or msg( "log", "Can't open $conf::pidfile: $!\n" );
  316. printf PIDFILE "%5d\n", $$;
  317. close(PIDFILE);
  318. chmod( 0644, $conf::pidfile )
  319. or die "Cannot set modes of $conf::pidfile: $!\n";
  320. # other signals will just log an error and exit
  321. foreach (@main::fatal_signals) {
  322. $SIG{$_} = \&fatal_signal;
  323. }
  324. # send signal to user-started process that we're ready and it can exit
  325. kill( $main::signo{"USR1"}, $parent_pid );
  326. # ---------------------------------------------------------------------------
  327. # the mainloop
  328. # ---------------------------------------------------------------------------
  329. # default to classical incoming/target
  330. $main::current_incoming = $conf::incoming;
  331. $main::current_targetdir = $conf::targetdir;
  332. $main::dstat = "i";
  333. write_status_file() if $conf::statusdelay;
  334. while (1) {
  335. # ping target only if there is the possibility that we'll contact it (but
  336. # also don't wait too long).
  337. my @have_changes = <*.changes *.commands *.dak-commands>;
  338. for ( my $delayed_dirs = 0 ;
  339. $delayed_dirs <= $conf::max_delayed ;
  340. $delayed_dirs++ )
  341. {
  342. my $adelayeddir = sprintf( "$conf::incoming_delayed", $delayed_dirs );
  343. push( @have_changes, <$adelayeddir/*.changes> );
  344. } ## end for ( my $delayed_dirs ...
  345. check_alive()
  346. if @have_changes || ( time - $main::last_ping_time ) > 8 * 60 * 60;
  347. if ( @have_changes && $main::target_up ) {
  348. check_incoming_writable if !$main::incoming_writable;
  349. check_dir() if $main::incoming_writable;
  350. }
  351. $main::dstat = "i";
  352. write_status_file() if $conf::statusdelay;
  353. if ( $conf::upload_method eq "copy" ) {
  354. age_delayed_queues();
  355. }
  356. # sleep() returns if we received a signal (SIGUSR1 for status FIFO), so
  357. # calculate the end time once and wait for it being reached.
  358. format_status_num( $main::next_run, time + $conf::queue_delay );
  359. my $delta;
  360. while ( ( $delta = calc_delta() ) > 0 ) {
  361. debug("mainloop sleeping $delta secs");
  362. sleep($delta);
  363. # check if statusd died, if using status FIFO, or update status file
  364. if ($conf::statusdelay) {
  365. write_status_file();
  366. } else {
  367. restart_statusd();
  368. }
  369. } ## end while ( ( $delta = calc_delta...
  370. } ## end while (1)
  371. sub calc_delta() {
  372. my $delta;
  373. $delta = $main::next_run - time;
  374. $delta = $conf::statusdelay
  375. if $conf::statusdelay && $conf::statusdelay < $delta;
  376. return $delta;
  377. } ## end sub calc_delta()
  378. # ---------------------------------------------------------------------------
  379. # main working functions
  380. # ---------------------------------------------------------------------------
  381. #
  382. # main function for checking the incoming dir
  383. #
  384. sub check_dir() {
  385. my ( @files, @changes, @keep_files, @this_keep_files, @stats, $file,
  386. $adelay );
  387. debug("starting checkdir");
  388. $main::dstat = "c";
  389. write_status_file() if $conf::statusdelay;
  390. # test if needed binaries are available; this is if they're on maybe
  391. # slow-mounted NFS filesystems
  392. foreach (@conf::test_binaries) {
  393. next if -f $_;
  394. # maybe the mount succeeds now
  395. sleep 5;
  396. next if -f $_;
  397. msg( "log", "binary test failed for $_; delaying queue run\n" );
  398. goto end_run;
  399. } ## end foreach (@conf::test_binaries)
  400. for ( $adelay = -1 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
  401. if ( $adelay == -1 ) {
  402. $main::current_incoming = $conf::incoming;
  403. $main::current_incoming_short = "";
  404. $main::current_targetdir = $conf::targetdir;
  405. } else {
  406. $main::current_incoming = sprintf( $conf::incoming_delayed, $adelay );
  407. $main::current_incoming_short = sprintf( "DELAYED/%d-day", $adelay );
  408. $main::current_targetdir = sprintf( $conf::targetdir_delayed, $adelay );
  409. }
  410. # need to clear directory specific variables
  411. undef(@keep_files);
  412. undef(@this_keep_files);
  413. chdir($main::current_incoming)
  414. or (
  415. msg(
  416. "log",
  417. "Cannot change to dir "
  418. . "${main::current_incoming_short}: $!\n"
  419. ),
  420. return
  421. );
  422. # look for *.commands and *.dak-commands files but not in delayed queues
  423. if ( $adelay == -1 ) {
  424. foreach $file (<*.commands>) {
  425. next unless $file =~ /$re_file_safe/;
  426. init_mail($file);
  427. block_signals();
  428. process_commands($file);
  429. unblock_signals();
  430. $main::dstat = "c";
  431. write_status_file() if $conf::statusdelay;
  432. finish_mail();
  433. } ## end foreach $file (<*.commands>)
  434. foreach $file (<*.dak-commands>) {
  435. next unless $file =~ /$re_file_safe/;
  436. init_mail($file);
  437. block_signals();
  438. process_dak_commands($file);
  439. unblock_signals();
  440. $main::dstat = "c";
  441. write_status_file() if $conf::statusdelay;
  442. finish_mail();
  443. }
  444. } ## end if ( $adelay == -1 )
  445. opendir( INC, "." )
  446. or (
  447. msg(
  448. "log", "Cannot open dir ${main::current_incoming_short}: $!\n"
  449. ),
  450. return
  451. );
  452. @files = readdir(INC);
  453. closedir(INC);
  454. # process all .changes files found
  455. @changes = grep /\.changes$/, @files;
  456. push( @keep_files, @changes ); # .changes files aren't stray
  457. foreach $file (@changes) {
  458. next unless $file =~ /$re_file_safe/;
  459. init_mail($file);
  460. # wrap in an eval to allow jumpbacks to here with die in case
  461. # of errors
  462. block_signals();
  463. eval { process_changes( $file, @this_keep_files ); };
  464. unblock_signals();
  465. msg( "log,mail", $@ ) if $@;
  466. $main::dstat = "c";
  467. write_status_file() if $conf::statusdelay;
  468. # files which are ok in conjunction with this .changes
  469. debug("$file tells to keep @this_keep_files");
  470. push( @keep_files, @this_keep_files );
  471. finish_mail();
  472. # break out of this loop if the incoming dir has become unwritable
  473. goto end_run if !$main::incoming_writable;
  474. } ## end foreach $file (@changes)
  475. ftp_close() if $conf::upload_method eq "ftp";
  476. # find files which aren't related to any .changes
  477. foreach $file (@files) {
  478. # filter out files we never want to delete
  479. next if !-f $file || # may have disappeared in the meantime
  480. $file eq "."
  481. || $file eq ".."
  482. || ( grep { $_ eq $file } @keep_files )
  483. || $file =~ /$conf::keep_files/;
  484. # Delete such files if they're older than
  485. # $stray_remove_timeout; they could be part of an
  486. # yet-incomplete upload, with the .changes still missing.
  487. # Cannot send any notification, since owner unknown.
  488. next if !( @stats = stat($file) );
  489. my $age = time - $stats[ST_MTIME];
  490. my ( $maint, $pattern, @job_files );
  491. if ( $file =~ /^junk-for-writable-test/
  492. || $file !~ m,$conf::valid_files,
  493. || $file !~ /$re_file_safe/
  494. || $age >= $conf::stray_remove_timeout )
  495. {
  496. msg( "log",
  497. "Deleted stray file ${main::current_incoming_short}/$file\n" )
  498. if rm($file);
  499. } else {
  500. debug(
  501. "found stray file ${main::current_incoming_short}/$file, deleting in ",
  502. print_time( $conf::stray_remove_timeout - $age )
  503. );
  504. } ## end else [ if ( $file =~ /^junk-for-writable-test/...
  505. } ## end foreach $file (@files)
  506. } ## end for ( $adelay = -1 ; $adelay...
  507. chdir($conf::incoming);
  508. end_run:
  509. $main::dstat = "i";
  510. write_status_file() if $conf::statusdelay;
  511. } ## end sub check_dir()
  512. sub get_filelist_from_known_good_changes($) {
  513. my $changes = shift;
  514. local (*CHANGES);
  515. my (@filenames);
  516. # parse the .changes file
  517. open( CHANGES, "<", $changes )
  518. or die "$changes: $!\n";
  519. outer_loop: while (<CHANGES>) {
  520. if (/^Files:/i) {
  521. while (<CHANGES>) {
  522. redo outer_loop if !/^\s/;
  523. my @field = split(/\s+/);
  524. next if @field != 6;
  525. # forbid shell meta chars in the name, we pass it to a
  526. # subshell several times...
  527. $field[5] =~ /$re_file_safe/;
  528. if ( $1 ne $field[5] ) {
  529. msg( "log", "found suspicious filename $field[5]\n" );
  530. next;
  531. }
  532. push( @filenames, $field[5] );
  533. } ## end while (<CHANGES>)
  534. } ## end if (/^Files:/i)
  535. } ## end while (<CHANGES>)
  536. close(CHANGES);
  537. return @filenames;
  538. } ## end sub get_filelist_from_known_good_changes($)
  539. #
  540. # process one .changes file
  541. #
  542. sub process_changes($\@) {
  543. my $changes = shift;
  544. my $keep_list = shift;
  545. my (
  546. $pgplines, @files, @filenames, @changes_stats,
  547. $failure_file, $retries, $last_retry, $upload_time,
  548. $file, $do_report, $ls_l, $problems_reported,
  549. $errs, $pkgname, $signator, $extralines
  550. );
  551. local (*CHANGES);
  552. local (*FAILS);
  553. format_status_str( $main::current_changes,
  554. "$main::current_incoming_short/$changes" );
  555. $main::dstat = "c";
  556. $main::mail_addr = "";
  557. write_status_file() if $conf::statusdelay;
  558. @$keep_list = ();
  559. msg( "log", "processing ${main::current_incoming_short}/$changes\n" );
  560. # run PGP on the file to check the signature
  561. if ( !( $signator = pgp_check($changes) ) ) {
  562. msg(
  563. "log,mail",
  564. "$main::current_incoming_short/$changes has bad PGP/GnuPG signature!\n"
  565. );
  566. goto remove_only_changes;
  567. } elsif ( $signator eq "LOCAL ERROR" ) {
  568. # An error has appened when starting pgp... Don't process the file,
  569. # but also don't delete it
  570. debug(
  571. "Can't PGP/GnuPG check $main::current_incoming_short/$changes -- don't process it for now"
  572. );
  573. return;
  574. } ## end elsif ( $signator eq "LOCAL ERROR")
  575. # parse the .changes file
  576. open( CHANGES, "<", $changes )
  577. or die "Cannot open ${main::current_incoming_short}/$changes: $!\n";
  578. $pgplines = 0;
  579. $extralines = 0;
  580. @files = ();
  581. outer_loop: while (<CHANGES>) {
  582. if (/^---+(BEGIN|END) PGP .*---+$/) {
  583. ++$pgplines;
  584. next;
  585. }
  586. if ( $pgplines < 1 or $pgplines >= 3 ) {
  587. $extralines++ if length $_ > 1;
  588. next;
  589. }
  590. if (/^Maintainer:\s*/i) {
  591. chomp( $main::mail_addr = $' );
  592. $main::mail_addr = $1 if $main::mail_addr =~ /<([^>]*)>/;
  593. } elsif (/^Source:\s*/i) {
  594. chomp( $pkgname = $' );
  595. $pkgname =~ s/\s+$//;
  596. $main::packages{$pkgname}++;
  597. } elsif (/^Files:/i) {
  598. while (<CHANGES>) {
  599. redo outer_loop if !/^\s/;
  600. my @field = split(/\s+/);
  601. next if @field != 6;
  602. # forbid shell meta chars in the name, we pass it to a
  603. # subshell several times...
  604. $field[5] =~ /$re_file_safe/;
  605. if ( $1 ne $field[5] ) {
  606. msg( "log", "found suspicious filename $field[5]\n" );
  607. msg(
  608. "mail",
  609. "File '$field[5]' mentioned in $main::current_incoming_short/$changes\n",
  610. "has bad characters in its name. Removed.\n"
  611. );
  612. rm( $field[5] );
  613. next;
  614. } ## end if ( $1 ne $field[5] )
  615. push(
  616. @files,
  617. {
  618. md5 => $field[1],
  619. size => $field[2],
  620. name => $field[5]
  621. }
  622. );
  623. push( @filenames, $field[5] );
  624. debug( "includes file $field[5], size $field[2], ", "md5 $field[1]" );
  625. } ## end while (<CHANGES>)
  626. } ## end elsif (/^Files:/i)
  627. } ## end while (<CHANGES>)
  628. close(CHANGES);
  629. # tell check_dir that the files mentioned in this .changes aren't stray,
  630. # we know about them somehow
  631. @$keep_list = @filenames;
  632. # some consistency checks
  633. if ( $extralines ) {
  634. msg( "log,mail",
  635. "$main::current_incoming_short/$changes contained lines outside the pgp signed "
  636. ."part, cannot process\n" );
  637. goto remove_only_changes;
  638. } ## end if ( $extralines )
  639. if ( !$main::mail_addr ) {
  640. msg( "log,mail",
  641. "$main::current_incoming_short/$changes doesn't contain a Maintainer: field; "
  642. . "cannot process\n" );
  643. goto remove_only_changes;
  644. } ## end if ( !$main::mail_addr)
  645. if ( $main::mail_addr !~ /^(buildd_\S+-\S+|\S+\@\S+\.\S+)/ ) {
  646. # doesn't look like a mail address, maybe only the name
  647. my ( $new_addr, @addr_list );
  648. if ( $new_addr = try_to_get_mail_addr( $main::mail_addr, \@addr_list ) ) {
  649. # substitute (unique) found addr, but give a warning
  650. msg(
  651. "mail",
  652. "(The Maintainer: field didn't contain a proper "
  653. . "mail address.\n"
  654. );
  655. msg(
  656. "mail",
  657. "Looking for `$main::mail_addr' in the Debian "
  658. . "keyring gave your address\n"
  659. );
  660. msg( "mail", "as unique result, so I used this.)\n" );
  661. msg( "log",
  662. "Substituted $new_addr for malformed " . "$main::mail_addr\n" );
  663. $main::mail_addr = $new_addr;
  664. } else {
  665. # not found or not unique: hold the job and inform queue maintainer
  666. my $old_addr = $main::mail_addr;
  667. $main::mail_addr = $conf::maintainer_mail;
  668. msg(
  669. "mail",
  670. "The job ${main::current_incoming_short}/$changes doesn't have a correct email\n"
  671. );
  672. msg( "mail", "address in the Maintainer: field:\n" );
  673. msg( "mail", " $old_addr\n" );
  674. msg( "mail", "A check for this in the Debian keyring gave:\n" );
  675. msg( "mail",
  676. @addr_list
  677. ? " " . join( ", ", @addr_list ) . "\n"
  678. : " nothing\n" );
  679. msg( "mail", "Please fix this manually\n" );
  680. msg(
  681. "log",
  682. "Bad Maintainer: field in ${main::current_incoming_short}/$changes: $old_addr\n"
  683. );
  684. goto remove_only_changes;
  685. } ## end else [ if ( $new_addr = try_to_get_mail_addr...
  686. } ## end if ( $main::mail_addr ...
  687. if ( $pgplines < 3 ) {
  688. msg(
  689. "log,mail",
  690. "$main::current_incoming_short/$changes isn't signed with PGP/GnuPG\n"
  691. );
  692. msg( "log", "(uploader $main::mail_addr)\n" );
  693. goto remove_only_changes;
  694. } ## end if ( $pgplines < 3 )
  695. if ( !@files ) {
  696. msg( "log,mail",
  697. "$main::current_incoming_short/$changes doesn't mention any files\n" );
  698. msg( "log", "(uploader $main::mail_addr)\n" );
  699. goto remove_only_changes;
  700. } ## end if ( !@files )
  701. # check for packages that shouldn't be processed
  702. if ( grep( $_ eq $pkgname, @conf::nonus_packages ) ) {
  703. msg(
  704. "log,mail",
  705. "$pkgname is a package that must be uploaded "
  706. . "to nonus.debian.org\n"
  707. );
  708. msg( "log,mail", "instead of target.\n" );
  709. msg( "log,mail",
  710. "Job rejected and removed all files belonging " . "to it:\n" );
  711. msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
  712. rm( $changes, @filenames );
  713. return;
  714. } ## end if ( grep( $_ eq $pkgname...
  715. $failure_file = $changes . ".failures";
  716. $retries = $last_retry = 0;
  717. if ( -f $failure_file ) {
  718. open( FAILS, "<", $failure_file )
  719. or die "Cannot open $main::current_incoming_short/$failure_file: $!\n";
  720. my $line = <FAILS>;
  721. close(FAILS);
  722. ( $retries, $last_retry ) = ( $1, $2 )
  723. if $line =~ /^(\d+)\s+(\d+)$/;
  724. push( @$keep_list, $failure_file );
  725. } ## end if ( -f $failure_file )
  726. die "Cannot stat ${main::current_incoming_short}/$changes (??): $!\n"
  727. if !( @changes_stats = stat($changes) );
  728. # Make $upload_time the maximum of all modification times of files
  729. # related to this .changes (and the .changes it self). This is the
  730. # last time something changes to these files.
  731. $upload_time = $changes_stats[ST_MTIME];
  732. for $file (@files) {
  733. my @stats;
  734. next if !( @stats = stat( $file->{"name"} ) );
  735. $file->{"stats"} = \@stats;
  736. $upload_time = $stats[ST_MTIME] if $stats[ST_MTIME] > $upload_time;
  737. } ## end for $file (@files)
  738. $do_report = ( time - $upload_time ) > $conf::problem_report_timeout;
  739. $problems_reported = $changes_stats[ST_MODE] & S_ISGID;
  740. # if any of the files is newer than the .changes' ctime (the time
  741. # we sent a report and set the sticky bit), send new problem reports
  742. if ( $problems_reported && $changes_stats[ST_CTIME] < $upload_time ) {
  743. $problems_reported = 0;
  744. chmod +( $changes_stats[ST_MODE] &= ~S_ISGID ), $changes;
  745. debug("upload_time>changes-ctime => resetting problems reported");
  746. }
  747. debug("do_report=$do_report problems_reported=$problems_reported");
  748. # now check all files for correct size and md5 sum
  749. for $file (@files) {
  750. my $filename = $file->{"name"};
  751. if ( !defined( $file->{"stats"} ) ) {
  752. # could be an upload that isn't complete yet, be quiet,
  753. # but don't process the file;
  754. msg( "log,mail", "$filename doesn't exist\n" )
  755. if $do_report && !$problems_reported;
  756. msg( "log", "$filename doesn't exist (ignored for now)\n" )
  757. if !$do_report;
  758. msg( "log", "$filename doesn't exist (already reported)\n" )
  759. if $problems_reported;
  760. ++$errs;
  761. } elsif ( $file->{"stats"}->[ST_SIZE] < $file->{"size"}
  762. && !$do_report )
  763. {
  764. # could be an upload that isn't complete yet, be quiet,
  765. # but don't process the file
  766. msg( "log", "$filename is too small (ignored for now)\n" );
  767. ++$errs;
  768. } elsif ( $file->{"stats"}->[ST_SIZE] != $file->{"size"} ) {
  769. msg( "log,mail", "$filename has incorrect size; deleting it\n" );
  770. rm($filename);
  771. ++$errs;
  772. } elsif ( md5sum($filename) ne $file->{"md5"} ) {
  773. msg( "log,mail",
  774. "$filename has incorrect md5 checksum; ",
  775. "deleting it\n" );
  776. rm($filename);
  777. ++$errs;
  778. } ## end elsif ( md5sum($filename)...
  779. } ## end for $file (@files)
  780. if ($errs) {
  781. if ( ( time - $upload_time ) > $conf::bad_changes_timeout ) {
  782. # if a .changes fails for a really long time (several days
  783. # or so), remove it and all associated files
  784. msg(
  785. "log,mail",
  786. "$main::current_incoming_short/$changes couldn't be processed for ",
  787. int( $conf::bad_changes_timeout / ( 60 * 60 ) ),
  788. " hours and is now deleted\n"
  789. );
  790. msg( "log,mail", "All files it mentions are also removed:\n" );
  791. msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
  792. rm( $changes, @filenames, $failure_file );
  793. } elsif ( $do_report && !$problems_reported ) {
  794. # otherwise, send a problem report, if not done already
  795. msg(
  796. "mail",
  797. "Due to the errors above, the .changes file couldn't ",
  798. "be processed.\n",
  799. "Please fix the problems for the upload to happen.\n"
  800. );
  801. # remember we already have sent a mail regarding this file
  802. debug("Sending problem report mail and setting SGID bit");
  803. my $mode = $changes_stats[ST_MODE] |= S_ISGID;
  804. msg( "log", "chmod failed: $!" )
  805. if ( chmod( $mode, $changes ) != 1 );
  806. } ## end elsif ( $do_report && !$problems_reported)
  807. # else: be quiet
  808. return;
  809. } ## end if ($errs)
  810. # if this upload already failed earlier, wait until the delay requirement
  811. # is fulfilled
  812. if ( $retries > 0
  813. && ( time - $last_retry ) <
  814. ( $retries == 1 ? $conf::upload_delay_1 : $conf::upload_delay_2 ) )
  815. {
  816. msg( "log", "delaying retry of upload\n" );
  817. return;
  818. } ## end if ( $retries > 0 && (...
  819. return if !ftp_open();
  820. # check if the job is already present on target
  821. # (moved to here, to avoid bothering target as long as there are errors in
  822. # the job)
  823. if ( $ls_l = is_on_target( $changes, @filenames ) ) {
  824. msg(
  825. "log,mail",
  826. "$main::current_incoming_short/$changes is already present on target host:\n"
  827. );
  828. msg( "log,mail", "$ls_l\n" );
  829. msg( "mail",
  830. "Either you already uploaded it, or someone else ",
  831. "came first.\n" );
  832. msg( "log,mail", "Job $changes removed.\n" );
  833. rm( $changes, @filenames, $failure_file );
  834. return;
  835. } ## end if ( $ls_l = is_on_target...
  836. # clear sgid bit before upload, scp would copy it to target. We don't need
  837. # it anymore, we know there are no problems if we come here. Also change
  838. # mode of files to 644 if this should be done locally.
  839. $changes_stats[ST_MODE] &= ~S_ISGID;
  840. if ( !$conf::chmod_on_target ) {
  841. $changes_stats[ST_MODE] &= ~0777;
  842. $changes_stats[ST_MODE] |= 0644;
  843. }
  844. chmod +( $changes_stats[ST_MODE] ), $changes;
  845. # try uploading to target
  846. if ( !copy_to_target( $changes, @filenames ) ) {
  847. # if the upload failed, increment the retry counter and remember the
  848. # current time; both things are written to the .failures file. Don't
  849. # increment the fail counter if the error was due to incoming
  850. # unwritable.
  851. return if !$main::incoming_writable;
  852. if ( ++$retries >= $conf::max_upload_retries ) {
  853. msg( "log,mail",
  854. "$changes couldn't be uploaded for $retries times now.\n" );
  855. msg( "log,mail",
  856. "Giving up and removing it and its associated files:\n" );
  857. msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
  858. rm( $changes, @filenames, $failure_file );
  859. } else {
  860. $last_retry = time;
  861. if ( open( FAILS, ">", $failure_file ) ) {
  862. print FAILS "$retries $last_retry\n";
  863. close(FAILS);
  864. chmod( 0600, $failure_file )
  865. or die "Cannot set modes of $failure_file: $!\n";
  866. } ## end if ( open( FAILS, ">$failure_file"...
  867. push( @$keep_list, $failure_file );
  868. debug("now $retries failed uploads");
  869. msg(
  870. "mail",
  871. "The upload will be retried in ",
  872. print_time(
  873. $retries == 1
  874. ? $conf::upload_delay_1
  875. : $conf::upload_delay_2
  876. ),
  877. "\n"
  878. );
  879. } ## end else [ if ( ++$retries >= $conf::max_upload_retries)
  880. return;
  881. } ## end if ( !copy_to_target( ...
  882. # If the files were uploaded ok, remove them
  883. rm( $changes, @filenames, $failure_file );
  884. msg( "mail", "$changes uploaded successfully to $conf::target\n" );
  885. msg( "mail", "along with the files:\n ", join( "\n ", @filenames ),
  886. "\n" );
  887. msg( "log",
  888. "$changes processed successfully (uploader $main::mail_addr)\n" );
  889. return;
  890. remove_only_changes:
  891. msg(
  892. "log,mail",
  893. "Removing $main::current_incoming_short/$changes, but keeping its "
  894. . "associated files for now.\n"
  895. );
  896. rm($changes);
  897. return;
  898. # Check for files that have the same stem as the .changes (and weren't
  899. # mentioned there) and delete them. It happens often enough that people
  900. # upload a .orig.tar.gz where it isn't needed and also not in the
  901. # .changes. Explicitly deleting it (and not waiting for the
  902. # $stray_remove_timeout) reduces clutter in the queue dir and maybe also
  903. # educates uploaders :-)
  904. # my $pattern = debian_file_stem( $changes );
  905. # my $spattern = substr( $pattern, 0, -1 ); # strip off '*' at end
  906. # my @other_files = glob($pattern);
  907. # filter out files that have a Debian revision at all and a different
  908. # revision. Those belong to a different upload.
  909. # if ($changes =~ /^\Q$spattern\E-([\d.+-]+)/) {
  910. # my $this_rev = $1;
  911. # @other_files = grep( !/^\Q$spattern\E-([\d.+-]+)/ || $1 eq $this_rev,
  912. # @other_files);
  913. #}
  914. # Also do not remove those files if a .changes is among them. Then there
  915. # is probably a second upload for another version or another architecture.
  916. # if (@other_files && !grep( /\.changes$/, @other_files )) {
  917. # rm( @other_files );
  918. # msg( "mail", "\nThe following file(s) seemed to belong to the same ".
  919. # "upload, but weren't listed\n" );
  920. # msg( "mail", "in the .changes file:\n " );
  921. # msg( "mail", join( "\n ", @other_files ), "\n" );
  922. # msg( "mail", "They have been deleted.\n" );
  923. # msg( "log", "Deleted files in upload not in $changes: @other_files\n" );
  924. #}
  925. } ## end sub process_changes($\@)
  926. #
  927. # process one .dak-commands file
  928. #
  929. sub process_dak_commands {
  930. my $commands = shift;
  931. msg("log", "processing ${main::current_incoming_short}/$commands\n");
  932. # TODO: get mail address from signed contents
  933. # and NOT implement a third parser for armored PGP...
  934. $main::mail_addr = undef;
  935. # check signature
  936. my $signator = pgp_check($commands);
  937. if (!$signator) {
  938. msg("log,mail",
  939. "$main::current_incoming_short/$commands has bad PGP/GnuPG signature!\n");
  940. msg("log,mail",
  941. "Removing $main::current_incoming_short/$commands\n");
  942. rm($commands);
  943. return;
  944. }
  945. elsif ($signator eq 'LOCAL ERROR') {
  946. debug("Can't check signature for $main::current_incoming_short/$commands -- don't process it for now");
  947. return;
  948. }
  949. msg("log,mail", "(PGP/GnuPG signature by $signator)\n");
  950. return if !ftp_open();
  951. # check target
  952. my @filenames = ($commands);
  953. if (my $ls_l = is_on_target($commands, @filenames)) {
  954. msg("log,mail", "$main::current_incoming_short/$commands is already present on target host:\n");
  955. msg("log,mail", "$ls_l\n");
  956. msg("log,mail", "Job $commands removed.\n");
  957. rm($commands);
  958. return;
  959. }
  960. if (!copy_to_target($commands)) {
  961. msg("log,mail", "$commands couldn't be uploaded to target.\n");
  962. msg("log,mail", "Giving up and removing it.\n");
  963. rm($commands);
  964. return;
  965. }
  966. rm($commands);
  967. msg("mail", "$commands uploaded successfully to $conf::target\n");
  968. }
  969. #
  970. # process one .commands file
  971. #
  972. sub process_commands($) {
  973. my $commands = shift;
  974. my ( @cmds, $cmd, $pgplines, $signator );
  975. local (*COMMANDS);
  976. my ($file, @removed, $target_delay );
  977. format_status_str( $main::current_changes, $commands );
  978. $main::dstat = "c";
  979. $main::mail_addr = "";
  980. write_status_file() if $conf::statusdelay;
  981. msg( "log", "processing $main::current_incoming_short/$commands\n" );
  982. # run PGP on the file to check the signature
  983. if ( !( $signator = pgp_check($commands) ) ) {
  984. msg(
  985. "log,mail",
  986. "$main::current_incoming_short/$commands has bad PGP/GnuPG signature!\n"
  987. );
  988. goto remove;
  989. } elsif ( $signator eq "LOCAL ERROR" ) {
  990. # An error has appened when starting pgp... Don't process the file,
  991. # but also don't delete it
  992. debug(
  993. "Can't PGP/GnuPG check $main::current_incoming_short/$commands -- don't process it for now"
  994. );
  995. return;
  996. } ## end elsif ( $signator eq "LOCAL ERROR")
  997. msg( "log", "(PGP/GnuPG signature by $signator)\n" );
  998. # parse the .commands file
  999. if ( !open( COMMANDS, "<", $commands ) ) {
  1000. msg( "log", "Cannot open $main::current_incoming_short/$commands: $!\n" );
  1001. return;
  1002. }
  1003. $pgplines = 0;
  1004. @cmds = ();
  1005. outer_loop: while (<COMMANDS>) {
  1006. if (/^---+(BEGIN|END) PGP .*---+$/) {
  1007. ++$pgplines;
  1008. } elsif (/^Uploader:\s*/i) {
  1009. chomp( $main::mail_addr = $' );
  1010. $main::mail_addr = $1 if $main::mail_addr =~ /<([^>]*)>/;
  1011. } elsif (/^Commands:/i) {
  1012. $_ = $';
  1013. for ( ; ; ) {
  1014. s/^\s*(.*)\s*$/$1/; # delete whitespace at both ends
  1015. if ( !/^\s*$/ ) {
  1016. push( @cmds, $_ );
  1017. debug("includes cmd $_");
  1018. }
  1019. last outer_loop if !defined( $_ = scalar(<COMMANDS>) );
  1020. chomp;
  1021. redo outer_loop if !/^\s/ || /^$/;
  1022. } ## end for ( ; ; )
  1023. } ## end elsif (/^Commands:/i)
  1024. } ## end while (<COMMANDS>)
  1025. close(COMMANDS);
  1026. # some consistency checks
  1027. if ( !$main::mail_addr || $main::mail_addr !~ /^\S+\@\S+\.\S+/ ) {
  1028. msg( "log,mail",
  1029. "$main::current_incoming_short/$commands contains no or bad Uploader: field: "
  1030. . "$main::mail_addr\n" );
  1031. msg( "log,mail",
  1032. "cannot process $main::current_incoming_short/$commands\n" );
  1033. $main::mail_addr = "";
  1034. goto remove;
  1035. } ## end if ( !$main::mail_addr...
  1036. msg( "log", "(command uploader $main::mail_addr)\n" );
  1037. if ( $pgplines < 3 ) {
  1038. msg(
  1039. "log,mail",
  1040. "$main::current_incoming_short/$commands isn't signed with PGP/GnuPG\n"
  1041. );
  1042. msg(
  1043. "mail",
  1044. "or the uploaded file is broken. Make sure to transfer in binary mode\n"
  1045. );
  1046. msg( "mail", "or better yet - use dcut for commands files\n" );
  1047. goto remove;
  1048. } ## end if ( $pgplines < 3 )
  1049. # now process commands
  1050. msg(
  1051. "mail",
  1052. "Log of processing your commands file $main::current_incoming_short/$commands:\n\n"
  1053. );
  1054. foreach $cmd (@cmds) {
  1055. my @word = split( /\s+/, $cmd );
  1056. msg( "mail,log", "> @word\n" );
  1057. my $selecteddelayed = -1;
  1058. next if @word < 1;
  1059. if ( $word[0] eq "rm" ) {
  1060. my @files = ();
  1061. foreach ( @word[ 1 .. $#word ] ) {
  1062. my $origword = $_;
  1063. if (m,^DELAYED/([0-9]+)-day/,) {
  1064. $selecteddelayed = $1;
  1065. s,^DELAYED/[0-9]+-day/,,;
  1066. }
  1067. if (m,(^|/)\*,) {
  1068. msg("mail,log", "$_: filename component cannot start with a wildcard\n");
  1069. } elsif ( $origword eq "--searchdirs" ) {
  1070. $selecteddelayed = -2;
  1071. } elsif (m,/,) {
  1072. msg(
  1073. "mail,log",
  1074. "$_: filename may not contain slashes except for DELAYED/#-day/ prefixes\n"
  1075. );
  1076. } else {
  1077. # process wildcards but also plain names
  1078. my (@thesefiles);
  1079. my $pat = quotemeta($_);
  1080. $pat =~ s/\\\*/.*/g;
  1081. $pat =~ s/\\\?/.?/g;
  1082. $pat =~ s/\\([][])/$1/g;
  1083. if ( $selecteddelayed < 0 ) { # scanning or explicitly incoming
  1084. opendir( DIR, "." );
  1085. push( @thesefiles, grep /^$pat$/, readdir(DIR) );
  1086. closedir(DIR);
  1087. }
  1088. if ( $selecteddelayed >= 0 ) {
  1089. my $dir = sprintf( $conf::incoming_delayed, $selecteddelayed );
  1090. opendir( DIR, $dir );
  1091. push( @thesefiles,
  1092. map ( "$dir/$_", grep /^$pat$/, readdir(DIR) ) );
  1093. closedir(DIR);
  1094. } elsif ( $selecteddelayed == -2 ) {
  1095. for ( my ($adelay) = 0 ;
  1096. ( !@thesefiles ) && $adelay <= $conf::max_delayed ;
  1097. $adelay++ )
  1098. {
  1099. my $dir = sprintf( $conf::incoming_delayed, $adelay );
  1100. opendir( DIR, $dir );
  1101. push( @thesefiles,
  1102. map ( "$dir/$_", grep /^$pat$/, readdir(DIR) ) );
  1103. closedir(DIR);
  1104. } ## end for ( my ($adelay) = 0 ...
  1105. } ## end elsif ( $selecteddelayed ...
  1106. push( @files, @thesefiles );
  1107. if ( !@thesefiles ) {
  1108. msg( "mail,log", "$origword did not match anything\n" );
  1109. }
  1110. } ## end else [ if ( $origword eq "--searchdirs")
  1111. } ## end foreach ( @word[ 1 .. $#word...
  1112. if ( !@files ) {
  1113. msg( "mail,log", "No files to delete\n" );
  1114. } else {
  1115. @removed = ();
  1116. foreach $file (@files) {
  1117. if ( !-f $file ) {
  1118. msg( "mail,log", "$file: no such file\n" );
  1119. } elsif ( $file =~ /$conf::keep_files/ ) {
  1120. msg( "mail,log", "$file is protected, cannot " . "remove\n" );
  1121. } elsif ( !unlink($file) ) {
  1122. msg( "mail,log", "$file: rm: $!\n" );
  1123. } else {
  1124. $file =~ s,$conf::incoming/?,,;
  1125. push( @removed, $file );
  1126. }
  1127. } ## end foreach $file (@files)
  1128. msg( "mail,log", "Files removed: @removed\n" ) if @removed;
  1129. } ## end else [ if ( !@files )
  1130. } elsif ( $word[0] eq "reschedule" ) {
  1131. if ( @word != 3 ) {
  1132. msg( "mail,log", "Wrong number of arguments\n" );
  1133. } elsif ( $conf::upload_method ne "copy" ) {
  1134. msg( "mail,log", "reschedule not available\n" );
  1135. } elsif ( $word[1] =~ m,/, || $word[1] !~ m/\.changes/ ) {
  1136. msg(
  1137. "mail,log",
  1138. "$word[1]: filename may not contain slashes and must be .changes\n"
  1139. );
  1140. } elsif ( !( ($target_delay) = $word[2] =~ m,^([0-9]+)-day$, )
  1141. || $target_delay > $conf::max_delayed )
  1142. {
  1143. msg(
  1144. "mail,log",
  1145. "$word[2]: rescheduling target must be #-day with # between 0 and $conf::max_delayed (in particular, no '/' allowed)\n"
  1146. );
  1147. } elsif ( $word[1] =~ /$conf::keep_files/ ) {
  1148. msg( "mail,log", "$word[1] is protected, cannot do stuff with it\n" );
  1149. } else {
  1150. my ($adelay);
  1151. for ( $adelay = 0 ;
  1152. $adelay <= $conf::max_delayed
  1153. && !-f (
  1154. sprintf( "$conf::targetdir_delayed", $adelay ) . "/$word[1]" ) ;
  1155. $adelay++ )
  1156. {
  1157. } ## end for ( $adelay = 0 ; $adelay...
  1158. if ( $adelay > $conf::max_delayed ) {
  1159. msg( "mail,log", "$word[1] not found\n" );
  1160. } elsif ( $adelay == $target_delay ) {
  1161. msg( "mail,log", "$word[1] already is in $word[2]\n" );
  1162. } else {
  1163. my (@thesefiles);
  1164. my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
  1165. my ($target_dir) =
  1166. sprintf( "$conf::targetdir_delayed", $target_delay );
  1167. push( @thesefiles, $word[1] );
  1168. push( @thesefiles,
  1169. get_filelist_from_known_good_changes("$dir/$word[1]") );
  1170. for my $afile (@thesefiles) {
  1171. if ( $afile =~ m/\.changes$/ ) {
  1172. utime undef, undef, ("$dir/$afile");
  1173. }
  1174. if ( !move("$dir/$afile", "$target_dir/$afile") ) {
  1175. msg( "mail,log", "move: $!\n" );
  1176. } else {
  1177. msg( "mail,log", "$afile moved to $target_delay-day\n" );
  1178. }
  1179. } ## end for my $afile (@thesefiles)
  1180. } ## end else [ if ( $adelay > $conf::max_delayed)
  1181. } ## end else [ if ( @word != 3 )
  1182. } elsif ( $word[0] eq "cancel" ) {
  1183. if ( @word != 2 ) {
  1184. msg( "mail,log", "Wrong number of arguments\n" );
  1185. } elsif ( $conf::upload_method ne "copy" ) {
  1186. msg( "mail,log", "cancel not available\n" );
  1187. } elsif (
  1188. $word[1] !~ m,$re_file_safe_prefix\.changes\z, )
  1189. {
  1190. msg( "mail,log",
  1191. "argument to cancel must be one .changes filename without path\n" );
  1192. } ## end elsif ( $word[1] !~ ...
  1193. my (@files) = ();
  1194. for ( my ($adelay) = 0 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
  1195. my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
  1196. if ( -f "$dir/$word[1]" ) {
  1197. @removed = ();
  1198. push( @files, "$word[1]" );
  1199. push( @files,
  1200. get_filelist_from_known_good_changes("$dir/$word[1]") );
  1201. foreach $file (@files) {
  1202. if ( !-f "$dir/$file" ) {
  1203. msg( "mail,log", "$dir/$file: no such file\n" );
  1204. } elsif ( "$dir/$file" =~ /$conf::keep_files/ ) {
  1205. msg( "mail,log",
  1206. "$dir/$file is protected, cannot " . "remove\n" );
  1207. } elsif ( !unlink("$dir/$file") ) {
  1208. msg( "mail,log", "$dir/$file: rm: $!\n" );
  1209. } else {
  1210. push( @removed, $file );
  1211. }
  1212. } ## end foreach $file (@files)
  1213. msg( "mail,log", "Files removed from $adelay-day: @removed\n" )
  1214. if @removed;
  1215. } ## end if ( -f "$dir/$word[1]")
  1216. } ## end for ( my ($adelay) = 0 ...
  1217. if ( !@files ) {
  1218. msg( "mail,log", "No upload found: $word[1]\n" );
  1219. }
  1220. } else {
  1221. msg( "mail,log", "unknown command $word[0]\n" );
  1222. }
  1223. } ## end foreach $cmd (@cmds)
  1224. rm($commands);
  1225. msg( "log",
  1226. "-- End of $main::current_incoming_short/$commands processing\n" );
  1227. return;
  1228. remove:
  1229. msg("log,mail", "Removing $main::current_incoming_short/$commands\n");
  1230. rm($commands);
  1231. return;
  1232. } ## end sub process_commands($)
  1233. sub age_delayed_queues() {
  1234. for ( my ($adelay) = 0 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
  1235. my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
  1236. my ($target_dir);
  1237. if ( $adelay == 0 ) {
  1238. $target_dir = $conf::targetdir;
  1239. } else {
  1240. $target_dir = sprintf( "$conf::targetdir_delayed", $adelay - 1 );
  1241. }
  1242. for my $achanges (<$dir/*.changes>) {
  1243. my $mtime = ( stat($achanges) )[9];
  1244. if ( $mtime + 24 * 60 * 60 <= time || $adelay == 0 ) {
  1245. utime undef, undef, ($achanges);
  1246. my @thesefiles = ( $achanges =~ m,.*/([^/]*), );
  1247. push( @thesefiles, get_filelist_from_known_good_changes($achanges) );
  1248. for my $afile (@thesefiles) {
  1249. if ( !move("$dir/$afile", "$target_dir/$afile") ) {
  1250. msg( "log", "move: $!\n" );
  1251. } else {
  1252. msg( "log", "$afile moved to $target_dir\n" );
  1253. }
  1254. } ## end for my $afile (@thesefiles)
  1255. } ## end if ( $mtime + 24 * 60 ...
  1256. } ## end for my $achanges (<$dir/*.changes>)
  1257. } ## end for ( my ($adelay) = 0 ...
  1258. } ## end sub age_delayed_queues()
  1259. #
  1260. # check if a file is already on target
  1261. #
  1262. sub is_on_target($\@) {
  1263. my $file = shift;
  1264. my $filelist = shift;
  1265. my $msg;
  1266. my $stat;
  1267. if ( $conf::upload_method eq "ssh" ) {
  1268. ( $msg, $stat ) = ssh_cmd("ls -l $file");
  1269. } elsif ( $conf::upload_method eq "ftp" ) {
  1270. my $err;
  1271. ( $msg, $err ) = ftp_cmd( "dir", $file );
  1272. if ($err) {
  1273. $stat = 1;
  1274. $msg = $err;
  1275. } elsif ( !$msg ) {
  1276. $stat = 1;
  1277. $msg = "ls: no such file\n";
  1278. } else {
  1279. $stat = 0;
  1280. $msg = join( "\n", @$msg );
  1281. }
  1282. } else {
  1283. my @allfiles = ($file);
  1284. push( @allfiles, @$filelist );
  1285. $stat = 1;
  1286. $msg = "no such file";
  1287. for my $afile (@allfiles) {
  1288. if ( -f "$conf::targetdir/$afile" ) {
  1289. $stat = 0;
  1290. $msg = "$afile";
  1291. }
  1292. } ## end for my $afile (@allfiles)
  1293. for ( my ($adelay) = 0 ;
  1294. $adelay <= $conf::max_delayed && $stat ;
  1295. $adelay++ )
  1296. {
  1297. for my $afile (@allfiles) {
  1298. if (
  1299. -f ( sprintf( "$conf::targetdir_delayed", $adelay ) . "/$afile" ) )
  1300. {
  1301. $stat = 0;
  1302. $msg = sprintf( "%d-day", $adelay ) . "/$afile";
  1303. } ## end if ( -f ( sprintf( "$conf::targetdir_delayed"...
  1304. } ## end for my $afile (@allfiles)
  1305. } ## end for ( my ($adelay) = 0 ...
  1306. } ## end else [ if ( $conf::upload_method...
  1307. chomp($msg);
  1308. debug("exit status: $stat, output was: $msg");
  1309. return "" if $stat && $msg =~ /no such file/i; # file not present
  1310. msg( "log", "strange ls -l output on target:\n", $msg ), return ""
  1311. if $stat || $@; # some other error, but still try to upload
  1312. # ls -l returned 0 -> file already there
  1313. $msg =~ s/\s\s+/ /g; # make multiple spaces into one, to save space
  1314. return $msg;
  1315. } ## end sub is_on_target($\@)
  1316. #
  1317. # copy a list of files to target
  1318. #
  1319. sub copy_to_target(@) {
  1320. my @files = @_;
  1321. my ( @md5sum, @expected_files, $sum, $name, $msgs, $stat );
  1322. $main::dstat = "u";
  1323. write_status_file() if $conf::statusdelay;
  1324. # copy the files
  1325. if ( $conf::upload_method eq "ssh" ) {
  1326. ( $msgs, $stat ) = scp_cmd(@files);
  1327. goto err if $stat;
  1328. } elsif ( $conf::upload_method eq "ftp" ) {
  1329. my ( $rv, $file );
  1330. if ( !$main::FTP_chan->cwd($main::current_targetdir) ) {
  1331. msg( "log,mail",
  1332. "Can't cd to $main::current_targetdir on $conf::target\n" );
  1333. goto err;
  1334. }
  1335. foreach $file (@files) {
  1336. ( $rv, $msgs ) = ftp_cmd( "put", $file );
  1337. goto err if !$rv;
  1338. }
  1339. } else {
  1340. for my $file (@files) {
  1341. eval { File::Copy::copy($file, $main::current_targetdir) };
  1342. if ($@) {
  1343. $stat = 1;
  1344. $msgs = $@;
  1345. goto err;
  1346. }
  1347. }
  1348. }
  1349. # check md5sums or sizes on target against our own
  1350. my $have_md5sums = 1;
  1351. if ($conf::check_md5sum) {
  1352. if ( $conf::upload_method eq "ssh" ) {
  1353. ( $msgs, $stat ) = ssh_cmd("md5sum @files");
  1354. goto err if $stat;
  1355. @md5sum = split( "\n", $msgs );
  1356. } elsif ( $conf::upload_method eq "ftp" ) {
  1357. my ( $rv, $err, $file );
  1358. foreach $file (@files) {
  1359. ( $rv, $err ) = ftp_cmd( "quot", "site", "md5sum", $file );
  1360. if ($err) {
  1361. next if ftp_code() == 550; # file not found
  1362. if ( ftp_code() == 500 ) { # unimplemented
  1363. $have_md5sums = 0;
  1364. goto get_sizes_instead;
  1365. }
  1366. $msgs = $err;
  1367. goto err;
  1368. } ## end if ($err)
  1369. chomp( my $t = ftp_response() );
  1370. push( @md5sum, $t );
  1371. } ## end foreach $file (@files)
  1372. if ( !$have_md5sums ) {
  1373. get_sizes_instead:
  1374. foreach $file (@files) {
  1375. ( $rv, $err ) = ftp_cmd( "size", $file );
  1376. if ($err) {
  1377. next if ftp_code() == 550; # file not found
  1378. $msgs = $err;
  1379. goto err;
  1380. }
  1381. push( @md5sum, "$rv $file" );
  1382. } ## end foreach $file (@files)
  1383. } ## end if ( !$have_md5sums )
  1384. } else {
  1385. for my $file (@files) {
  1386. my $md5 = eval { md5sum("$main::current_targetdir/$file") };
  1387. if ($@) {
  1388. $msgs = $@;
  1389. goto err;
  1390. }
  1391. push @md5sum, "$md5 $file" if $md5;
  1392. }
  1393. }
  1394. @expected_files = @files;
  1395. foreach (@md5sum) {
  1396. chomp;
  1397. ( $sum, $name ) = split;
  1398. next if !grep { $_ eq $name } @files; # a file we didn't upload??
  1399. next if $sum eq "md5sum:"; # looks like an error message
  1400. if ( ( $have_md5sums && $sum ne md5sum($name) )
  1401. || ( !$have_md5sums && $sum != ( -s $name ) ) )
  1402. {
  1403. msg(
  1404. "log,mail",
  1405. "Upload of $name to $conf::target failed ",
  1406. "(" . ( $have_md5sums ? "md5sum" : "size" ) . " mismatch)\n"
  1407. );
  1408. goto err;
  1409. } ## end if ( ( $have_md5sums &&...
  1410. # seen that file, remove it from expect list
  1411. @expected_files = map { $_ eq $name ? () : $_ } @expected_files;
  1412. } ## end foreach (@md5sum)
  1413. if (@expected_files) {
  1414. msg( "log,mail", "Failed to upload the files\n" );
  1415. msg( "log,mail", " ", join( ", ", @expected_files ), "\n" );
  1416. msg( "log,mail", "(Not present on target after upload)\n" );
  1417. goto err;
  1418. } ## end if (@expected_files)
  1419. } ## end if ($conf::check_md5sum)
  1420. if ($conf::chmod_on_target) {
  1421. # change file's mode explicitly to 644 on target
  1422. if ( $conf::upload_method eq "ssh" ) {
  1423. ( $msgs, $stat ) = ssh_cmd("chmod 644 @files");
  1424. goto err if $stat;
  1425. } elsif ( $conf::upload_method eq "ftp" ) {
  1426. my ( $rv, $file );
  1427. foreach $file (@files) {
  1428. ( $rv, $msgs ) = ftp_cmd( "quot", "site", "chmod", "644", $file );
  1429. msg( "log", "Can't chmod $file on target:\n$msgs" )
  1430. if $msgs;
  1431. goto err if !$rv;
  1432. } ## end foreach $file (@files)
  1433. } else {
  1434. for my $file (@files) {
  1435. unless (chmod 0644, "$main::current_targetdir/$file") {
  1436. $msgs = "Could not chmod $file: $!";
  1437. goto err;
  1438. }
  1439. }
  1440. }
  1441. } ## end if ($conf::chmod_on_target)
  1442. $main::dstat = "c";
  1443. write_status_file() if $conf::statusdelay;
  1444. return 1;
  1445. err:
  1446. msg( "log,mail",
  1447. "Upload to $conf::target failed",
  1448. $? ? ", last exit status " . sprintf( "%s", $? >> 8 ) : "", "\n" );
  1449. msg( "log,mail", "Error messages:\n", $msgs )
  1450. if $msgs;
  1451. # If "permission denied" was among the errors, test if the incoming is
  1452. # writable at all.
  1453. if ( $msgs && $msgs =~ /(permission denied|read-?only file)/i ) {
  1454. if ( !check_incoming_writable() ) {
  1455. msg( "log,mail", "(The incoming directory seems to be ",
  1456. "unwritable.)\n" );
  1457. }
  1458. } ## end if ( $msgs =~ /(permission denied|read-?only file)/i)
  1459. # remove bad files or an incomplete upload on target
  1460. if ( $conf::upload_method eq "ssh" ) {
  1461. ssh_cmd("rm -f @files");
  1462. } elsif ( $conf::upload_method eq "ftp" ) {
  1463. my $file;
  1464. foreach $file (@files) {
  1465. my ( $rv, $err );
  1466. ( $rv, $err ) = ftp_cmd( "delete", $file );
  1467. msg( "log", "Can't delete $file on target:\n$err" )
  1468. if $err;
  1469. } ## end foreach $file (@files)
  1470. } else {
  1471. my @tfiles = map { "$main::current_targetdir/$_" } @files;
  1472. debug("executing unlink(@tfiles)");
  1473. rm(@tfiles);
  1474. }
  1475. $main::dstat = "c";
  1476. write_status_file() if $conf::statusdelay;
  1477. return 0;
  1478. } ## end sub copy_to_target(@)
  1479. #
  1480. # check if a file is correctly signed with PGP
  1481. #
  1482. sub pgp_check($) {
  1483. my $file = shift;
  1484. my $output = "";
  1485. my $signator;
  1486. my $found = 0;
  1487. my $stat = 1;
  1488. local (*PIPE);
  1489. local $_;
  1490. if ($file =~ /$re_file_safe/) {
  1491. $file = $1;
  1492. } else {
  1493. msg( "log", "Tainted filename, skipping: $file\n" );
  1494. return "LOCAL ERROR";
  1495. }
  1496. # check the file has only one clear-signed section
  1497. my $fh;
  1498. unless (open $fh, "<", $file) {
  1499. msg("log,mail", "Could not open $file\n");
  1500. return "";
  1501. }
  1502. unless (<$fh> eq "-----BEGIN PGP SIGNED MESSAGE-----\n") {
  1503. msg("log,mail", "$file: does not start with a clearsigned message\n");
  1504. return "";
  1505. }
  1506. my $pgplines = 1;
  1507. while (<$fh>) {
  1508. if (/\A- /) {
  1509. msg("log,mail", "$file: dash-escaped messages are not accepted\n");
  1510. return "";
  1511. }
  1512. elsif ($_ eq "-----BEGIN PGP SIGNATURE-----\n"
  1513. || $_ eq "-----END PGP SIGNATURE-----\n") {
  1514. $pgplines++;
  1515. }
  1516. elsif (/\A--/) {
  1517. msg("log,mail", "$file: unexpected OpenPGP armor\n");
  1518. return "";
  1519. }
  1520. elsif ($pgplines > 3 && /\S/) {
  1521. msg("log,mail", "$file: found text after end of signature\n");
  1522. return "";
  1523. }
  1524. }
  1525. if ($pgplines != 3) {
  1526. msg("log,mail", "$file: doesn't seem to be a valid clearsigned OpenPGP message\n");
  1527. return "";
  1528. }
  1529. close $fh;
  1530. if ( -x $conf::gpg ) {
  1531. my @command = ("$conf::gpg", "--no-options", "--batch", "--no-tty",
  1532. "--trust-model", "always", "--no-default-keyring",
  1533. (map +("--keyring" => $_), @conf::keyrings),
  1534. "--verify", "-");
  1535. debug( "executing " . join(" ", @command) );
  1536. my $child = open(PIPE, "-|");
  1537. if (!defined($child)) {
  1538. msg("log", "Can't open pipe to $conf::gpg: $!\n");
  1539. return "LOCAL ERROR";
  1540. }
  1541. if ($child == 0) {
  1542. unless (open(STDERR, ">&", \*STDOUT)) {
  1543. print "Could not redirect STDERR.";
  1544. exit(-1);
  1545. }
  1546. unless (open(STDIN, "<", $file)) {
  1547. print "Could not open $file: $!";
  1548. exit(-1);
  1549. }
  1550. { exec(@command) }; # BLOCK avoids warning about likely unreachable code
  1551. print "Could not exec gpg: $!";
  1552. exit(-1);
  1553. }
  1554. $output .= $_ while (<PIPE>);
  1555. close(PIPE);
  1556. $stat = $?;
  1557. } ## end if ( -x $conf::gpg )
  1558. if ($stat) {
  1559. msg( "log,mail", "GnuPG signature check failed on $file\n" );
  1560. msg( "mail", $output );
  1561. msg( "log,mail", "(Exit status ", $stat >> 8, ")\n" );
  1562. return "";
  1563. } ## end if ($stat)
  1564. $output =~ /^(gpg: )?good signature from (user )?"(.*)"\.?$/im;
  1565. ( $signator = $3 ) ||= "unknown signator";
  1566. if ($conf::debug) {
  1567. debug("GnuPG signature ok (by $signator)");
  1568. }
  1569. return $signator;
  1570. } ## end sub pgp_check($)
  1571. # ---------------------------------------------------------------------------
  1572. # the status daemon
  1573. # ---------------------------------------------------------------------------
  1574. #
  1575. # fork a subprocess that watches the 'status' FIFO
  1576. #
  1577. # that process blocks until someone opens the FIFO, then sends a
  1578. # signal (SIGUSR1) to the main process, expects
  1579. #
  1580. sub fork_statusd() {
  1581. my $statusd_pid;
  1582. my $main_pid = $$;
  1583. my $errs;
  1584. local (*STATFIFO);
  1585. $statusd_pid = open( STATUSD, "|-" );
  1586. die "cannot fork: $!\n" if !defined($statusd_pid);
  1587. # parent just returns
  1588. if ($statusd_pid) {
  1589. msg( "log", "forked status daemon (pid $statusd_pid)\n" );
  1590. return $statusd_pid;
  1591. }
  1592. # child: the status FIFO daemon
  1593. # ignore SIGPIPE here, in case some closes the FIFO without completely
  1594. # reading it
  1595. $SIG{"PIPE"} = "IGNORE";
  1596. # also ignore SIGCLD, we don't want to inherit the restart-statusd handler
  1597. # from our parent
  1598. $SIG{"CHLD"} = "DEFAULT";
  1599. rm($conf::statusfile);
  1600. $errs = `$conf::mkfifo $conf::statusfile`;
  1601. die "$main::progname: cannot create named pipe $conf::statusfile: $errs"
  1602. if $?;
  1603. chmod( 0644, $conf::statusfile )
  1604. or die "Cannot set modes of $conf::statusfile: $!\n";
  1605. # close log file, so that log rotating works
  1606. close(LOG);
  1607. close(STDOUT);
  1608. close(STDERR);
  1609. while (1) {
  1610. my ( $status, $mup, $incw, $ds, $next_run, $last_ping, $currch, $l );
  1611. # open the FIFO for writing; this blocks until someone (probably ftpd)
  1612. # opens it for reading
  1613. open( STATFIFO, ">", $conf::statusfile )
  1614. or die "Cannot open $conf::statusfile\n";
  1615. select(STATFIFO);
  1616. # tell main daemon to send us status infos
  1617. kill( $main::signo{"USR1"}, $main_pid );
  1618. # get the infos from stdin; must loop until enough bytes received!
  1619. my $expect_len = 3 + 2 * STATNUM_LEN + STATSTR_LEN;
  1620. for ( $status = "" ; ( $l = length($status) ) < $expect_len ; ) {
  1621. sysread( STDIN, $status, $expect_len - $l, $l );
  1622. }
  1623. # disassemble the status byte stream
  1624. my $pos = 0;
  1625. foreach (
  1626. [ mup => 1 ],
  1627. [ incw => 1 ],
  1628. [ ds => 1 ],
  1629. [ next_run => STATNUM_LEN ],
  1630. [ last_ping => STATNUM_LEN ],
  1631. [ currch => STATSTR_LEN ]
  1632. )
  1633. {
  1634. eval "\$$_->[0] = substr( \$status, $pos, $_->[1] );";
  1635. $pos += $_->[1];
  1636. } ## end foreach ( [ mup => 1 ], [ incw...
  1637. $currch =~ s/\n+//g;
  1638. print_status( $mup, $incw, $ds, $next_run, $last_ping, $currch );
  1639. close(STATFIFO);
  1640. # This sleep is necessary so that we can't reopen the FIFO
  1641. # immediately, in case the reader hasn't closed it yet if we get to
  1642. # the open again. Is there a better solution for this??
  1643. sleep 1;
  1644. } ## end while (1)
  1645. } ## end sub fork_statusd()
  1646. #
  1647. # update the status file, in case we use a plain file and not a FIFO
  1648. #
  1649. sub write_status_file() {
  1650. return if !$conf::statusfile;
  1651. open( STATFILE, ">", $conf::statusfile )
  1652. or ( msg( "log", "Could not open $conf::statusfile: $!\n" ), return );
  1653. my $oldsel = select(STATFILE);
  1654. print_status(
  1655. $main::target_up, $main::incoming_writable,
  1656. $main::dstat, $main::next_run,
  1657. $main::last_ping_time, $main::current_changes
  1658. );
  1659. select($oldsel);
  1660. close(STATFILE);
  1661. } ## end sub write_status_file()
  1662. sub print_status($$$$$$) {
  1663. my $mup = shift;
  1664. my $incw = shift;
  1665. my $ds = shift;
  1666. my $next_run = shift;
  1667. my $last_ping = shift;
  1668. my $currch = shift;
  1669. my $approx;
  1670. my $version;
  1671. ( $version = 'Release: 0.9 $Revision: 1.51 $' ) =~ s/\$ ?//g;
  1672. print "debianqueued $version\n";
  1673. $approx = $conf::statusdelay ? "approx. " : "";
  1674. if ( $mup eq "0" ) {
  1675. print "$conf::target is down, queue pausing\n";
  1676. return;
  1677. } elsif ( $conf::upload_method ne "copy" ) {
  1678. print "$conf::target seems to be up, last ping $approx",
  1679. print_time( time - $last_ping ), " ago\n";
  1680. }
  1681. if ( $incw eq "0" ) {
  1682. print "The incoming directory is not writable, queue pausing\n";
  1683. return;
  1684. }
  1685. if ( $ds eq "i" ) {
  1686. print "Next queue check in $approx", print_time( $next_run - time ), "\n";
  1687. return;
  1688. } elsif ( $ds eq "c" ) {
  1689. print "Checking queue directory\n";
  1690. } elsif ( $ds eq "u" ) {
  1691. print "Uploading to $conf::target\n";
  1692. } else {
  1693. print "Bad status data from daemon: \"$mup$incw$ds\"\n";
  1694. return;
  1695. }
  1696. print "Current job is $currch\n" if $currch;
  1697. } ## end sub print_status($$$$$$)
  1698. #
  1699. # format a number for sending to statusd (fixed length STATNUM_LEN)
  1700. #
  1701. sub format_status_num(\$$) {
  1702. my $varref = shift;
  1703. my $num = shift;
  1704. $$varref = sprintf "%" . STATNUM_LEN . "d", $num;
  1705. } ## end sub format_status_num(\$$)
  1706. #
  1707. # format a string for sending to statusd (fixed length STATSTR_LEN)
  1708. #
  1709. sub format_status_str(\$$) {
  1710. my $varref = shift;
  1711. my $str = shift;
  1712. $$varref = substr( $str, 0, STATSTR_LEN );
  1713. $$varref .= "\n" x ( STATSTR_LEN - length($$varref) );
  1714. } ## end sub format_status_str(\$$)
  1715. #
  1716. # send a status string to the status daemon
  1717. #
  1718. # Avoid all operations that could call malloc() here! Most libc
  1719. # implementations aren't reentrant, so we may not call it from a
  1720. # signal handler. So use only already-defined variables.
  1721. #
  1722. sub send_status() {
  1723. local $! = 0; # preserve errno
  1724. # re-setup handler, in case we have broken SysV signals
  1725. $SIG{"USR1"} = \&send_status;
  1726. syswrite( STATUSD, $main::target_up, 1 );
  1727. syswrite( STATUSD, $main::incoming_writable, 1 );
  1728. syswrite( STATUSD, $main::dstat, 1 );
  1729. syswrite( STATUSD, $main::next_run, STATNUM_LEN );
  1730. syswrite( STATUSD, $main::last_ping_time, STATNUM_LEN );
  1731. syswrite( STATUSD, $main::current_changes, STATSTR_LEN );
  1732. } ## end sub send_status()
  1733. # ---------------------------------------------------------------------------
  1734. # FTP functions
  1735. # ---------------------------------------------------------------------------
  1736. #
  1737. # open FTP connection to target host if not already open
  1738. #
  1739. sub ftp_open() {
  1740. return 1 unless $conf::upload_method eq "ftp";
  1741. if ($main::FTP_chan) {
  1742. # is already open, but might have timed out; test with a cwd
  1743. return $main::FTP_chan
  1744. if $main::FTP_chan->cwd($main::current_targetdir);
  1745. # cwd didn't work, channel is closed, try to reopen it
  1746. $main::FTP_chan = undef;
  1747. } ## end if ($main::FTP_chan)
  1748. if (
  1749. !(
  1750. $main::FTP_chan =
  1751. Net::FTP->new(
  1752. $conf::target,
  1753. Debug => $conf::ftpdebug,
  1754. Timeout => $conf::ftptimeout,
  1755. Passive => 1,
  1756. )
  1757. )
  1758. )
  1759. {
  1760. msg( "log,mail", "Cannot open FTP server $conf::target\n" );
  1761. goto err;
  1762. } ## end if ( !( $main::FTP_chan...
  1763. if ( !$main::FTP_chan->login() ) {
  1764. msg( "log,mail", "Anonymous login on FTP server $conf::target failed\n" );
  1765. goto err;
  1766. }
  1767. if ( !$main::FTP_chan->binary() ) {
  1768. msg( "log,mail", "Can't set binary FTP mode on $conf::target\n" );
  1769. goto err;
  1770. }
  1771. if ( !$main::FTP_chan->cwd($main::current_targetdir) ) {
  1772. msg( "log,mail",
  1773. "Can't cd to $main::current_targetdir on $conf::target\n" );
  1774. goto err;
  1775. }
  1776. debug("opened FTP channel to $conf::target");
  1777. return 1;
  1778. err:
  1779. $main::FTP_chan = undef;
  1780. return 0;
  1781. } ## end sub ftp_open()
  1782. sub ftp_cmd($@) {
  1783. my $cmd = shift;
  1784. my ( $rv, $err );
  1785. my $direct_resp_cmd = ( $cmd eq "quot" );
  1786. debug( "executing FTP::$cmd(" . join( ", ", @_ ) . ")" );
  1787. $SIG{"ALRM"} = sub { die "timeout in FTP::$cmd\n" };
  1788. alarm($conf::remote_timeout);
  1789. eval { $rv = $main::FTP_chan->$cmd(@_); };
  1790. alarm(0);
  1791. $err = "";
  1792. $rv = ( ftp_code() =~ /^2/ ) ? 1 : 0 if $direct_resp_cmd;
  1793. if ($@) {
  1794. $err = $@;
  1795. undef $rv;
  1796. } elsif ( !$rv ) {
  1797. $err = ftp_response();
  1798. }
  1799. return ( $rv, $err );
  1800. } ## end sub ftp_cmd($@)
  1801. sub ftp_close() {
  1802. if ($main::FTP_chan) {
  1803. $main::FTP_chan->quit();
  1804. $main::FTP_chan = undef;
  1805. }
  1806. return 1;
  1807. } ## end sub ftp_close()
  1808. sub ftp_response() {
  1809. return join( '', @{ ${*$main::FTP_chan}{'net_cmd_resp'} } );
  1810. }
  1811. sub ftp_code() {
  1812. return ${*$main::FTP_chan}{'net_cmd_code'};
  1813. }
  1814. sub ftp_error() {
  1815. my $code = ftp_code();
  1816. return ( $code =~ /^[45]/ ) ? 1 : 0;
  1817. }
  1818. # ---------------------------------------------------------------------------
  1819. # utility functions
  1820. # ---------------------------------------------------------------------------
  1821. sub ssh_cmd($) {
  1822. my $cmd = shift;
  1823. my ( $msg, $stat );
  1824. my $ecmd = "$conf::ssh $conf::ssh_options $conf::target "
  1825. . "-l $conf::targetlogin \'cd $main::current_targetdir; $cmd\'";
  1826. debug("executing $ecmd");
  1827. $SIG{"ALRM"} = sub { die "timeout in ssh command\n" };
  1828. alarm($conf::remote_timeout);
  1829. eval { $msg = `$ecmd 2>&1`; };
  1830. alarm(0);
  1831. if ($@) {
  1832. $msg = $@;
  1833. $stat = 1;
  1834. } else {
  1835. $stat = $?;
  1836. }
  1837. return ( $msg, $stat );
  1838. } ## end sub ssh_cmd($)
  1839. sub scp_cmd(@) {
  1840. my ( $msg, $stat );
  1841. my $ecmd = "$conf::scp $conf::ssh_options @_ "
  1842. . "$conf::targetlogin\@$conf::target:$main::current_targetdir";
  1843. debug("executing $ecmd");
  1844. $SIG{"ALRM"} = sub { die "timeout in scp\n" };
  1845. alarm($conf::remote_timeout);
  1846. eval { $msg = `$ecmd 2>&1`; };
  1847. alarm(0);
  1848. if ($@) {
  1849. $msg = $@;
  1850. $stat = 1;
  1851. } else {
  1852. $stat = $?;
  1853. }
  1854. return ( $msg, $stat );
  1855. } ## end sub scp_cmd(@)
  1856. #
  1857. # check if target is alive (code stolen from Net::Ping.pm)
  1858. #
  1859. sub check_alive(;$) {
  1860. my $timeout = shift;
  1861. my ( $saddr, $ret, $target_ip );
  1862. local (*PINGSOCK);
  1863. if ( $conf::upload_method eq "copy" ) {
  1864. format_status_num( $main::last_ping_time, time );
  1865. $main::target_up = 1;
  1866. return;
  1867. }
  1868. $timeout ||= 30;
  1869. if ( !( $target_ip = ( gethostbyname($conf::target) )[4] ) ) {
  1870. msg( "log", "Cannot get IP address of $conf::target\n" );
  1871. $ret = 0;
  1872. goto out;
  1873. }
  1874. $saddr = pack( 'S n a4 x8', AF_INET, $main::echo_port, $target_ip );
  1875. $SIG{'ALRM'} = sub { die };
  1876. alarm($timeout);
  1877. $ret = $main::tcp_proto; # avoid warnings about unused variable
  1878. $ret = 0;
  1879. eval <<'EOM' ;
  1880. return unless socket( PINGSOCK, PF_INET, SOCK_STREAM, $main::tcp_proto );
  1881. return unless connect( PINGSOCK, $saddr );
  1882. $ret = 1;
  1883. EOM
  1884. alarm(0);
  1885. close(PINGSOCK);
  1886. msg( "log", "pinging $conf::target: " . ( $ret ? "ok" : "down" ) . "\n" );
  1887. out:
  1888. $main::target_up = $ret ? "1" : "0";
  1889. format_status_num( $main::last_ping_time, time );
  1890. write_status_file() if $conf::statusdelay;
  1891. } ## end sub check_alive(;$)
  1892. #
  1893. # check if incoming dir on target is writable
  1894. #
  1895. sub check_incoming_writable() {
  1896. my $testfile = ".debianqueued-testfile";
  1897. my ( $msg, $stat );
  1898. if ( $conf::upload_method eq "ssh" ) {
  1899. ( $msg, $stat ) =
  1900. ssh_cmd( "rm -f $testfile; touch $testfile; " . "rm -f $testfile" );
  1901. } elsif ( $conf::upload_method eq "ftp" ) {
  1902. my $file = "junk-for-writable-test-" . format_time();
  1903. $file =~ s/[ :.]/-/g;
  1904. local (*F);
  1905. open( F, ">", $file );
  1906. close(F);
  1907. my $rv;
  1908. ( $rv, $msg ) = ftp_cmd( "put", $file );
  1909. $stat = 0;
  1910. $msg = "" if !defined $msg;
  1911. unlink $file;
  1912. ftp_cmd( "delete", $file );
  1913. } elsif ( $conf::upload_method eq "copy" ) {
  1914. unless(POSIX::access($main::current_targetdir, &POSIX::W_OK)) {
  1915. $msg = "No write access: $!";
  1916. $stat = 1;
  1917. }
  1918. }
  1919. chomp($msg);
  1920. debug("exit status: $stat, output was: $msg");
  1921. if ( !$stat ) {
  1922. # change incoming_writable only if ssh didn't return an error
  1923. $main::incoming_writable =
  1924. ( $msg =~ /(permission denied|read-?only file|cannot create)/i )
  1925. ? "0"
  1926. : "1";
  1927. } else {
  1928. debug("local error, keeping old status");
  1929. }
  1930. debug("incoming_writable = $main::incoming_writable");
  1931. write_status_file() if $conf::statusdelay;
  1932. return $main::incoming_writable;
  1933. } ## end sub check_incoming_writable()
  1934. #
  1935. # remove a list of files, log failing ones
  1936. #
  1937. sub rm(@) {
  1938. my $done = 0;
  1939. foreach (@_) {
  1940. ( unlink $_ and ++$done )
  1941. or $! == ENOENT
  1942. or msg( "log", "Could not delete $_: $!\n" );
  1943. }
  1944. return $done;
  1945. } ## end sub rm(@)
  1946. #
  1947. # get md5 checksum of a file
  1948. #
  1949. sub md5sum($) {
  1950. my $file = shift;
  1951. my $md5 = Digest::MD5->new;
  1952. open my $fh, "<", $file or return "";
  1953. $md5->addfile($fh);
  1954. close $fh;
  1955. return $md5->hexdigest;
  1956. } ## end sub md5sum($)
  1957. #
  1958. # output a messages to several destinations
  1959. #
  1960. # first arg is a comma-separated list of destinations; valid are "log"
  1961. # and "mail"; rest is stuff to be printed, just as with print
  1962. #
  1963. sub msg($@) {
  1964. my @dest = split( ',', shift );
  1965. if ( grep /log/, @dest ) {
  1966. my $now = format_time();
  1967. print LOG "$now ", @_;
  1968. }
  1969. if ( grep /mail/, @dest ) {
  1970. $main::mail_text .= join( '', @_ );
  1971. }
  1972. } ## end sub msg($@)
  1973. #
  1974. # print a debug messages, if $debug is true
  1975. #
  1976. sub debug(@) {
  1977. return if !$conf::debug;
  1978. my $now = format_time();
  1979. print LOG "$now DEBUG ", @_, "\n";
  1980. }
  1981. #
  1982. # intialize the "mail" destination of msg() (this clears text,
  1983. # address, subject, ...)
  1984. #
  1985. sub init_mail(;$) {
  1986. my $file = shift;
  1987. $main::mail_addr = "";
  1988. $main::mail_text = "";
  1989. %main::packages = ();
  1990. $main::mail_subject = $file ? "Processing of $file" : "";
  1991. } ## end sub init_mail(;$)
  1992. #
  1993. # finalize mail to be sent from msg(): check if something present, and
  1994. # then send out
  1995. #
  1996. sub finish_mail() {
  1997. debug("No mail for $main::mail_addr")
  1998. if $main::mail_addr && !$main::mail_text;
  1999. return unless $main::mail_addr && $main::mail_text;
  2000. if ( !send_mail( $main::mail_addr, $main::mail_subject, $main::mail_text ) )
  2001. {
  2002. # store this mail in memory so it isn't lost if executing sendmail
  2003. # failed.
  2004. push(
  2005. @main::stored_mails,
  2006. {
  2007. addr => $main::mail_addr,
  2008. subject => $main::mail_subject,
  2009. text => $main::mail_text
  2010. }
  2011. );
  2012. } ## end if ( !send_mail( $main::mail_addr...
  2013. init_mail();
  2014. # try to send out stored mails
  2015. my $mailref;
  2016. while ( $mailref = shift(@main::stored_mails) ) {
  2017. if (
  2018. !send_mail( $mailref->{'addr'}, $mailref->{'subject'},
  2019. $mailref->{'text'} )
  2020. )
  2021. {
  2022. unshift( @main::stored_mails, $mailref );
  2023. last;
  2024. } ## end if ( !send_mail( $mailref...
  2025. } ## end while ( $mailref = shift(...
  2026. } ## end sub finish_mail()
  2027. #
  2028. # send one mail
  2029. #
  2030. sub send_mail($$$) {
  2031. my $addr = shift;
  2032. my $subject = shift;
  2033. my $text = shift;
  2034. my $package =
  2035. keys %main::packages ? join( ' ', keys %main::packages ) : "";
  2036. use Email::Sender::Simple;
  2037. if ($conf::overridemail) {
  2038. $addr = $conf::overridemail;
  2039. }
  2040. my $date = sprintf "%s",
  2041. strftime( "%a, %d %b %Y %T %z", ( localtime(time) ) );
  2042. my $message = <<__MESSAGE__;
  2043. To: $addr
  2044. From: Debian FTP Masters <ftpmaster\@ftp-master.debian.org>
  2045. Subject: $subject
  2046. Date: $date
  2047. X-Debian: DAK
  2048. X-DAK: DAK
  2049. Precedence: bulk
  2050. Auto-Submitted: auto-generated
  2051. __MESSAGE__
  2052. if ( length $package ) {
  2053. $message .= "X-Debian-Package: $package\n";
  2054. }
  2055. $message .= "\n$text";
  2056. $message .= "\nGreetings,\n\n\tYour Debian queue daemon (running on host $main::hostname)\n";
  2057. return Email::Sender::Simple->try_to_send($message);
  2058. } ## end sub send_mail($$$)
  2059. #
  2060. # try to find a mail address for a name in the keyrings
  2061. #
  2062. sub try_to_get_mail_addr($$) {
  2063. my $name = shift;
  2064. my $listref = shift;
  2065. @$listref = ();
  2066. open( F,
  2067. "$conf::gpg --no-options --batch --no-default-keyring "
  2068. . "--always-trust --keyring "
  2069. . join( " --keyring ", @conf::keyrings )
  2070. . " --list-keys |"
  2071. ) or return "";
  2072. while (<F>) {
  2073. if ( /^pub / && / $name / ) {
  2074. /<([^>]*)>/;
  2075. push( @$listref, $1 );
  2076. }
  2077. } ## end while (<F>)
  2078. close(F);
  2079. return ( @$listref >= 1 ) ? $listref->[0] : "";
  2080. } ## end sub try_to_get_mail_addr($$)
  2081. #
  2082. # return current time as string
  2083. #
  2084. sub format_time() {
  2085. my $t;
  2086. # omit weekday and year for brevity
  2087. ( $t = localtime ) =~ /^\w+\s(.*)\s\d+$/;
  2088. return $1;
  2089. } ## end sub format_time()
  2090. sub print_time($) {
  2091. my $secs = shift;
  2092. my $hours = int( $secs / ( 60 * 60 ) );
  2093. $secs -= $hours * 60 * 60;
  2094. return sprintf "%d:%02d:%02d", $hours, int( $secs / 60 ), $secs % 60;
  2095. } ## end sub print_time($)
  2096. #
  2097. # block some signals during queue processing
  2098. #
  2099. # This is just to avoid data inconsistency or uploads being aborted in the
  2100. # middle. Only "soft" signals are blocked, i.e. SIGINT and SIGTERM, try harder
  2101. # ones if you really want to kill the daemon at once.
  2102. #
  2103. sub block_signals() {
  2104. POSIX::sigprocmask( SIG_BLOCK, $main::block_sigset );
  2105. }
  2106. sub unblock_signals() {
  2107. POSIX::sigprocmask( SIG_UNBLOCK, $main::block_sigset );
  2108. }
  2109. #
  2110. # process SIGHUP: close log file and reopen it (for logfile cycling)
  2111. #
  2112. sub close_log($) {
  2113. close(LOG);
  2114. close(STDOUT);
  2115. close(STDERR);
  2116. open( LOG, ">>", $conf::logfile )
  2117. or die "Cannot open my logfile $conf::logfile: $!\n";
  2118. chmod( 0644, $conf::logfile )
  2119. or msg( "log", "Cannot set modes of $conf::logfile: $!\n" );
  2120. select( ( select(LOG), $| = 1 )[0] );
  2121. open( STDOUT, ">&", \*LOG )
  2122. or msg( "log",
  2123. "$main::progname: Can't redirect stdout to " . "$conf::logfile: $!\n" );
  2124. open( STDERR, ">&", \*LOG )
  2125. or msg( "log",
  2126. "$main::progname: Can't redirect stderr to " . "$conf::logfile: $!\n" );
  2127. msg( "log", "Restart after SIGHUP\n" );
  2128. } ## end sub close_log($)
  2129. #
  2130. # process SIGCHLD: check if it was our statusd process
  2131. #
  2132. sub kid_died($) {
  2133. my $pid;
  2134. # reap statusd, so that it's no zombie when we try to kill(0) it
  2135. waitpid( $main::statusd_pid, WNOHANG );
  2136. # Uncomment the following line if your Perl uses unreliable System V signal
  2137. # (i.e. if handlers reset to default if the signal is delivered).
  2138. # (Unfortunately, the re-setup can't be done in any case, since on some
  2139. # systems this will cause the SIGCHLD to be delivered again if there are
  2140. # still unreaped children :-(( )
  2141. # $SIG{"CHLD"} = \&kid_died; # resetup handler for SysV
  2142. } ## end sub kid_died($)
  2143. sub restart_statusd() {
  2144. # restart statusd if it died
  2145. if ( !kill( 0, $main::statusd_pid ) ) {
  2146. close(STATUSD); # close out pipe end
  2147. $main::statusd_pid = fork_statusd();
  2148. }
  2149. } ## end sub restart_statusd()
  2150. #
  2151. # process a fatal signal: cleanup and exit
  2152. #
  2153. sub fatal_signal($) {
  2154. my $signame = shift;
  2155. my $sig;
  2156. # avoid recursions of fatal_signal in case of BSD signals
  2157. foreach $sig (qw( ILL ABRT BUS FPE SEGV PIPE )) {
  2158. $SIG{$sig} = "DEFAULT";
  2159. }
  2160. if ( $$ == $main::maind_pid ) {
  2161. # only the main daemon should do this
  2162. kill( $main::signo{"TERM"}, $main::statusd_pid )
  2163. if defined $main::statusd_pid;
  2164. unlink( $conf::statusfile, $conf::pidfile );
  2165. } ## end if ( $$ == $main::maind_pid)
  2166. msg( "log", "Caught SIG$signame -- exiting (pid $$)\n" );
  2167. exit 1;
  2168. } ## end sub fatal_signal($)
  2169. # Local Variables:
  2170. # tab-width: 4
  2171. # fill-column: 78
  2172. # End: