Debian Codegraph @

build-aux/gnupload

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