gnupload 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/bin/sh
  2. # Sign files and upload them.
  3. scriptversion=2018-03-07.03; # UTC
  4. # Copyright (C) 2004-2018 Free Software Foundation, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. # Originally written by Alexandre Duret-Lutz <adl@gnu.org>.
  19. # The master copy of this file is maintained in the gnulib Git repository.
  20. # Please send bug reports and feature requests to bug-gnulib@gnu.org.
  21. set -e
  22. GPG='gpg --batch --no-tty'
  23. conffile=.gnuploadrc
  24. to=
  25. dry_run=false
  26. replace=
  27. symlink_files=
  28. delete_files=
  29. delete_symlinks=
  30. collect_var=
  31. dbg=
  32. nl='
  33. '
  34. usage="Usage: $0 [OPTION]... [CMD] FILE... [[CMD] FILE...]
  35. Sign all FILES, and process them at the destinations specified with --to.
  36. If CMD is not given, it defaults to uploading. See examples below.
  37. Commands:
  38. --delete delete FILES from destination
  39. --symlink create symbolic links
  40. --rmsymlink remove symbolic links
  41. -- treat the remaining arguments as files to upload
  42. Options:
  43. --to DEST specify a destination DEST for FILES
  44. (multiple --to options are allowed)
  45. --user NAME sign with key NAME
  46. --replace allow replacements of existing files
  47. --symlink-regex[=EXPR] use sed script EXPR to compute symbolic link names
  48. --dry-run do nothing, show what would have been done
  49. (including the constructed directive file)
  50. --version output version information and exit
  51. --help print this help text and exit
  52. If --symlink-regex is given without EXPR, then the link target name
  53. is created by replacing the version information with '-latest', e.g.:
  54. foo-1.3.4.tar.gz -> foo-latest.tar.gz
  55. Recognized destinations are:
  56. alpha.gnu.org:DIRECTORY
  57. savannah.gnu.org:DIRECTORY
  58. savannah.nongnu.org:DIRECTORY
  59. ftp.gnu.org:DIRECTORY
  60. build directive files and upload files by FTP
  61. download.gnu.org.ua:{alpha|ftp}/DIRECTORY
  62. build directive files and upload files by SFTP
  63. [user@]host:DIRECTORY upload files with scp
  64. Options and commands are applied in order. If the file $conffile exists
  65. in the current working directory, its contents are prepended to the
  66. actual command line options. Use this to keep your defaults. Comments
  67. (#) and empty lines in $conffile are allowed.
  68. <https://www.gnu.org/prep/maintain/html_node/Automated-FTP-Uploads.html>
  69. gives some further background.
  70. Examples:
  71. 1. Upload foobar-1.0.tar.gz to ftp.gnu.org:
  72. gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz
  73. 2. Upload foobar-1.0.tar.gz and foobar-1.0.tar.xz to ftp.gnu.org:
  74. gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz foobar-1.0.tar.xz
  75. 3. Same as above, and also create symbolic links to foobar-latest.tar.*:
  76. gnupload --to ftp.gnu.org:foobar \\
  77. --symlink-regex \\
  78. foobar-1.0.tar.gz foobar-1.0.tar.xz
  79. 4. Upload foobar-0.9.90.tar.gz to two sites:
  80. gnupload --to alpha.gnu.org:foobar \\
  81. --to sources.redhat.com:~ftp/pub/foobar \\
  82. foobar-0.9.90.tar.gz
  83. 5. Delete oopsbar-0.9.91.tar.gz and upload foobar-0.9.91.tar.gz
  84. (the -- terminates the list of files to delete):
  85. gnupload --to alpha.gnu.org:foobar \\
  86. --to sources.redhat.com:~ftp/pub/foobar \\
  87. --delete oopsbar-0.9.91.tar.gz \\
  88. -- foobar-0.9.91.tar.gz
  89. gnupload executes a program ncftpput to do the transfers; if you don't
  90. happen to have an ncftp package installed, the ncftpput-ftp script in
  91. the build-aux/ directory of the gnulib package
  92. (https://savannah.gnu.org/projects/gnulib) may serve as a replacement.
  93. Send patches and bug reports to <bug-gnulib@gnu.org>."
  94. # Read local configuration file
  95. if test -r "$conffile"; then
  96. echo "$0: Reading configuration file $conffile"
  97. conf=`sed 's/#.*$//;/^$/d' "$conffile" | tr "\015$nl" ' '`
  98. eval set x "$conf \"\$@\""
  99. shift
  100. fi
  101. while test -n "$1"; do
  102. case $1 in
  103. -*)
  104. collect_var=
  105. case $1 in
  106. --help)
  107. echo "$usage"
  108. exit $?
  109. ;;
  110. --to)
  111. if test -z "$2"; then
  112. echo "$0: Missing argument for --to" 1>&2
  113. exit 1
  114. elif echo "$2" | grep 'ftp-upload\.gnu\.org' >/dev/null; then
  115. echo "$0: Use ftp.gnu.org:PKGNAME or alpha.gnu.org:PKGNAME" >&2
  116. echo "$0: for the destination, not ftp-upload.gnu.org (which" >&2
  117. echo "$0: is used for direct ftp uploads, not with gnupload)." >&2
  118. echo "$0: See --help and its examples if need be." >&2
  119. exit 1
  120. else
  121. to="$to $2"
  122. shift
  123. fi
  124. ;;
  125. --user)
  126. if test -z "$2"; then
  127. echo "$0: Missing argument for --user" 1>&2
  128. exit 1
  129. else
  130. GPG="$GPG --local-user $2"
  131. shift
  132. fi
  133. ;;
  134. --delete)
  135. collect_var=delete_files
  136. ;;
  137. --replace)
  138. replace="replace: true"
  139. ;;
  140. --rmsymlink)
  141. collect_var=delete_symlinks
  142. ;;
  143. --symlink-regex=*)
  144. symlink_expr=`expr "$1" : '[^=]*=\(.*\)'`
  145. ;;
  146. --symlink-regex)
  147. symlink_expr='s|-[0-9][0-9\.]*\(-[0-9][0-9]*\)\{0,1\}\.|-latest.|'
  148. ;;
  149. --symlink)
  150. collect_var=symlink_files
  151. ;;
  152. --dry-run|-n)
  153. dry_run=:
  154. ;;
  155. --version)
  156. echo "gnupload $scriptversion"
  157. exit $?
  158. ;;
  159. --)
  160. shift
  161. break
  162. ;;
  163. -*)
  164. echo "$0: Unknown option '$1', try '$0 --help'" 1>&2
  165. exit 1
  166. ;;
  167. esac
  168. ;;
  169. *)
  170. if test -z "$collect_var"; then
  171. break
  172. else
  173. eval "$collect_var=\"\$$collect_var $1\""
  174. fi
  175. ;;
  176. esac
  177. shift
  178. done
  179. dprint()
  180. {
  181. echo "Running $* ..."
  182. }
  183. if $dry_run; then
  184. dbg=dprint
  185. fi
  186. if test -z "$to"; then
  187. echo "$0: Missing destination sites" >&2
  188. exit 1
  189. fi
  190. if test -n "$symlink_files"; then
  191. x=`echo "$symlink_files" | sed 's/[^ ]//g;s/ //g'`
  192. if test -n "$x"; then
  193. echo "$0: Odd number of symlink arguments" >&2
  194. exit 1
  195. fi
  196. fi
  197. if test $# = 0; then
  198. if test -z "${symlink_files}${delete_files}${delete_symlinks}"; then
  199. echo "$0: No file to upload" 1>&2
  200. exit 1
  201. fi
  202. else
  203. # Make sure all files exist. We don't want to ask
  204. # for the passphrase if the script will fail.
  205. for file
  206. do
  207. if test ! -f $file; then
  208. echo "$0: Cannot find '$file'" 1>&2
  209. exit 1
  210. elif test -n "$symlink_expr"; then
  211. linkname=`echo $file | sed "$symlink_expr"`
  212. if test -z "$linkname"; then
  213. echo "$0: symlink expression produces empty results" >&2
  214. exit 1
  215. elif test "$linkname" = $file; then
  216. echo "$0: symlink expression does not alter file name" >&2
  217. exit 1
  218. fi
  219. fi
  220. done
  221. fi
  222. # Make sure passphrase is not exported in the environment.
  223. unset passphrase
  224. unset passphrase_fd_0
  225. GNUPGHOME=${GNUPGHOME:-$HOME/.gnupg}
  226. # Reset PATH to be sure that echo is a built-in. We will later use
  227. # 'echo $passphrase' to output the passphrase, so it is important that
  228. # it is a built-in (third-party programs tend to appear in 'ps'
  229. # listings with their arguments...).
  230. # Remember this script runs with 'set -e', so if echo is not built-in
  231. # it will exit now.
  232. if $dry_run || grep -q "^use-agent" $GNUPGHOME/gpg.conf; then :; else
  233. PATH=/empty echo -n "Enter GPG passphrase: "
  234. stty -echo
  235. read -r passphrase
  236. stty echo
  237. echo
  238. passphrase_fd_0="--passphrase-fd 0"
  239. fi
  240. if test $# -ne 0; then
  241. for file
  242. do
  243. echo "Signing $file ..."
  244. rm -f $file.sig
  245. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 -ba -o $file.sig $file
  246. done
  247. fi
  248. # mkdirective DESTDIR BASE FILE STMT
  249. # Arguments: See upload, below
  250. mkdirective ()
  251. {
  252. stmt="$4"
  253. if test -n "$3"; then
  254. stmt="
  255. filename: $3$stmt"
  256. fi
  257. cat >${2}.directive<<EOF
  258. version: 1.2
  259. directory: $1
  260. comment: gnupload v. $scriptversion$stmt
  261. EOF
  262. if $dry_run; then
  263. echo "File ${2}.directive:"
  264. cat ${2}.directive
  265. echo "File ${2}.directive:" | sed 's/./-/g'
  266. fi
  267. }
  268. mksymlink ()
  269. {
  270. while test $# -ne 0
  271. do
  272. echo "symlink: $1 $2"
  273. shift
  274. shift
  275. done
  276. }
  277. # upload DEST DESTDIR BASE FILE STMT FILES
  278. # Arguments:
  279. # DEST Destination site;
  280. # DESTDIR Destination directory;
  281. # BASE Base name for the directive file;
  282. # FILE Name of the file to distribute (may be empty);
  283. # STMT Additional statements for the directive file;
  284. # FILES List of files to upload.
  285. upload ()
  286. {
  287. dest=$1
  288. destdir=$2
  289. base=$3
  290. file=$4
  291. stmt=$5
  292. files=$6
  293. rm -f $base.directive $base.directive.asc
  294. case $dest in
  295. alpha.gnu.org:*)
  296. mkdirective "$destdir" "$base" "$file" "$stmt"
  297. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  298. $dbg ncftpput ftp-upload.gnu.org /incoming/alpha $files $base.directive.asc
  299. ;;
  300. ftp.gnu.org:*)
  301. mkdirective "$destdir" "$base" "$file" "$stmt"
  302. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  303. $dbg ncftpput ftp-upload.gnu.org /incoming/ftp $files $base.directive.asc
  304. ;;
  305. savannah.gnu.org:*)
  306. if test -z "$files"; then
  307. echo "$0: warning: standalone directives not applicable for $dest" >&2
  308. fi
  309. $dbg ncftpput savannah.gnu.org /incoming/savannah/$destdir $files
  310. ;;
  311. savannah.nongnu.org:*)
  312. if test -z "$files"; then
  313. echo "$0: warning: standalone directives not applicable for $dest" >&2
  314. fi
  315. $dbg ncftpput savannah.nongnu.org /incoming/savannah/$destdir $files
  316. ;;
  317. download.gnu.org.ua:alpha/*|download.gnu.org.ua:ftp/*)
  318. destdir_p1=`echo "$destdir" | sed 's,^[^/]*/,,'`
  319. destdir_topdir=`echo "$destdir" | sed 's,/.*,,'`
  320. mkdirective "$destdir_p1" "$base" "$file" "$stmt"
  321. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  322. for f in $files $base.directive.asc
  323. do
  324. echo put $f
  325. done | $dbg sftp -b - puszcza.gnu.org.ua:/incoming/$destdir_topdir
  326. ;;
  327. /*)
  328. dest_host=`echo "$dest" | sed 's,:.*,,'`
  329. mkdirective "$destdir" "$base" "$file" "$stmt"
  330. echo "$passphrase" | $dbg $GPG $passphrase_fd_0 --clearsign $base.directive
  331. $dbg cp $files $base.directive.asc $dest_host
  332. ;;
  333. *)
  334. if test -z "$files"; then
  335. echo "$0: warning: standalone directives not applicable for $dest" >&2
  336. fi
  337. $dbg scp $files $dest
  338. ;;
  339. esac
  340. rm -f $base.directive $base.directive.asc
  341. }
  342. #####
  343. # Process any standalone directives
  344. stmt=
  345. if test -n "$symlink_files"; then
  346. stmt="$stmt
  347. `mksymlink $symlink_files`"
  348. fi
  349. for file in $delete_files
  350. do
  351. stmt="$stmt
  352. archive: $file"
  353. done
  354. for file in $delete_symlinks
  355. do
  356. stmt="$stmt
  357. rmsymlink: $file"
  358. done
  359. if test -n "$stmt"; then
  360. for dest in $to
  361. do
  362. destdir=`echo $dest | sed 's/[^:]*://'`
  363. upload "$dest" "$destdir" "`hostname`-$$" "" "$stmt"
  364. done
  365. fi
  366. # Process actual uploads
  367. for dest in $to
  368. do
  369. for file
  370. do
  371. echo "Uploading $file to $dest ..."
  372. stmt=
  373. #
  374. # allowing file replacement is all or nothing.
  375. if test -n "$replace"; then stmt="$stmt
  376. $replace"
  377. fi
  378. #
  379. files="$file $file.sig"
  380. destdir=`echo $dest | sed 's/[^:]*://'`
  381. if test -n "$symlink_expr"; then
  382. linkname=`echo $file | sed "$symlink_expr"`
  383. stmt="$stmt
  384. symlink: $file $linkname
  385. symlink: $file.sig $linkname.sig"
  386. fi
  387. upload "$dest" "$destdir" "$file" "$file" "$stmt" "$files"
  388. done
  389. done
  390. exit 0
  391. # Local variables:
  392. # eval: (add-hook 'before-save-hook 'time-stamp)
  393. # time-stamp-start: "scriptversion="
  394. # time-stamp-format: "%:y-%02m-%02d.%02H"
  395. # time-stamp-time-zone: "UTC0"
  396. # time-stamp-end: "; # UTC"
  397. # End: