1 diff -Naur util-linux-2.11r-o/mount/lomount.c util-linux-2.11r/mount/lomount.c
2 --- util-linux-2.11r-o/mount/lomount.c Mon Jul 9 16:10:58 2001
3 +++ util-linux-2.11r/mount/lomount.c Mon Jul 9 16:19:24 2001
5 * - added Native Language Support
6 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * - fixed strerr(errno) in gettext calls
8 + * 2000-09-24 Marc Mutz <Marc@Mutz.com>
9 + * - added long option names and the --pass-fd option to pass
10 + * passphrases via fd's to losetup/mount. Used for encryption in
11 + * non-interactive environments. The idea behind xgetpass() is stolen
12 + * from GnuPG, v.1.0.3 (http://www.gnupg.org/).
15 #define PROC_DEVICES "/proc/devices"
20 +/* A function to read the passphrase either from the terminal or from
21 + * an open file descriptor */
23 +xgetpass (int pfd, const char *prompt)
25 + if (pfd < 0) /* terminal */
26 + return (getpass(prompt));
27 + else { /* file descriptor */
33 + if (i >= buflen-1) {
34 + /* we're running out of space in the buffer.
35 + * Make it bigger: */
36 + char *tmppass = pass;
38 + pass = realloc(tmppass,buflen);
40 + /* realloc failed. Stop reading _now_. */
41 + error("not enough memory while reading passphrase");
42 + pass = tmppass; /* the old buffer hasn't changed */
46 + if ( read(pfd,pass+i, 1) != 1 || pass[i] == '\n' )
59 #define PASSWDBUFFLEN 130 /* getpass returns only max. 128 bytes, see man getpass */
62 set_loop (const char *device, const char *file, int offset,
63 - const char *encryption, int *loopro) {
64 + const char *encryption, int pfd, int *loopro) {
65 struct loop_info loopinfo;
69 loopinfo.lo_encrypt_key_size = 0;
72 - pass = getpass (_("Password: "));
73 + /* WARNING: xgetpass() can return massive amounts of data,
74 + * not only 128 bytes like the original getpass(3) */
75 + pass = xgetpass (pfd,_("Password: "));
76 xstrncpy (loopinfo.lo_encrypt_key, pass, LO_KEY_SIZE);
77 loopinfo.lo_encrypt_key_size = strlen(loopinfo.lo_encrypt_key);
80 printf(_("WARNING: Use of DES is depreciated.\n"));
81 - pass = getpass (_("Password: "));
82 + pass = xgetpass (pfd,_("Password: "));
83 strncpy (loopinfo.lo_encrypt_key, pass, 8);
84 loopinfo.lo_encrypt_key[8] = 0;
85 loopinfo.lo_encrypt_key_size = 8;
86 - pass = getpass (_("Init (up to 16 hex digits): "));
87 + pass = xgetpass (pfd,_("Init (up to 16 hex digits): "));
88 for (i = 0; i < 16 && pass[i]; i++)
89 if (isxdigit (pass[i])) {
90 loopinfo.lo_init[i >> 3] |= (pass[i] > '9' ?
93 case LO_CRYPT_DES_EDE3:
95 - pass = getpass("Password :");
96 + pass = xgetpass(pfd,"Password :");
97 strncpy(passwdbuff+1,pass,PASSWDBUFFLEN-1);
99 rmd160_hash_buffer(keybits,pass,strlen(pass));
103 set_loop (const char *device, const char *file, int offset,
104 - const char *encryption, int *loopro) {
105 + const char *encryption, int pfd, int *loopro) {
109 @@ -390,20 +435,34 @@
111 static char *progname;
113 +static struct option longopts[] = {
114 + { "delete", 0, 0, 'd' },
115 + { "detach", 0, 0, 'd' },
116 + { "encryption", 1, 0, 'e' },
117 + { "help", 0, 0, 'h' },
118 + { "offset", 1, 0, 'o' },
119 + { "pass-fd", 1, 0, 'p' },
120 + { "verbose", 0, 0, 'v' },
127 - struct crypt_type_struct *c;
128 fprintf(stderr, _("usage:\n\
129 %s loop_device # give info\n\
130 %s -d loop_device # delete\n\
131 - %s [ -e encryption ] [ -o offset ] loop_device file # setup\n"),
132 + %s [ options ] loop_device file # setup\n\
133 + where options include\n\
134 + --offset <num>, -o <num>\n\
135 + start at offset <num> into file.\n\
136 + --pass-fd <num>, -p <num>\n\
137 + read passphrase from file descriptor <num>\n\
138 + instead of the terminal.\n\
139 + --encryption <cipher>, -e <cipher>\n\
140 + encrypt with <cipher>.\n\
141 + Check /proc/crypto/cipher for available ciphers.\n"),
142 progname, progname, progname);
143 - fprintf(stderr, " where encryption is one of:\n");
144 - c = &crypt_type_tbl[0];
146 - fprintf(stderr, " %s\n", c->name);
155 main(int argc, char **argv) {
156 - char *offset, *encryption;
157 + char *offset, *encryption, *passfd;
167 - offset = encryption = NULL;
168 + offset = encryption = passfd = NULL;
170 - while ((c = getopt(argc,argv,"de:o:v")) != -1) {
171 + while ((c = getopt_long(argc,argv,"de:ho:p:v",
172 + longopts, NULL)) != -1) {
189 if (argc == 1) usage();
190 - if ((delete && (argc != optind+1 || encryption || offset)) ||
191 + if ((delete && (argc != optind+1 || encryption || offset || passfd)) ||
192 (!delete && (argc < optind+1 || argc > optind+2)))
194 if (argc == optind+1) {
197 if (offset && sscanf(offset,"%d",&off) != 1)
199 - res = set_loop(argv[optind],argv[optind+1],off,encryption,&ro);
200 + if (passfd && sscanf(passfd,"%d",&pfd) != 1)
202 + res = set_loop(argv[optind],argv[optind+1],off,encryption,pfd,&ro);
206 diff -Naur util-linux-2.11g-o/mount/lomount.h util-linux-2.11g/mount/lomount.h
207 --- util-linux-2.11g-o/mount/lomount.h Fri Dec 8 19:08:02 2000
208 +++ util-linux-2.11g/mount/lomount.h Mon Jul 9 16:11:38 2001
211 -extern int set_loop (const char *, const char *, int, const char *, int *);
212 +extern int set_loop (const char *, const char *, int, const char *, int, int *);
213 extern int del_loop (const char *);
214 extern int is_loop_device (const char *);
215 extern char * find_unused_loop_device (void);
216 diff -Naur util-linux-2.11g-o/mount/losetup.8 util-linux-2.11g/mount/losetup.8
217 --- util-linux-2.11g-o/mount/losetup.8 Mon Jul 9 16:10:58 2001
218 +++ util-linux-2.11g/mount/losetup.8 Mon Jul 9 16:11:38 2001
230 \fIloop_device\fP argument is given, the status of the corresponding loop
234 +.IP "\fB\-\-delete, \-\-detach, \-d\fP"
235 detach the file or device associated with the specified loop device.
236 -.IP "\fB\-e \fIencryption\fP"
237 +.IP "\fB\-\-encryption, \-e \fIencryption\fP"
239 enable data encryption. The following keywords are recognized:
242 enabled in the Crypto API.
245 -.IP "\fB\-o \fIoffset\fP"
246 +.IP "\fB\-\-offset, \-o \fIoffset\fP"
247 the data start is moved \fIoffset\fP bytes into the specified file or
249 +.IP "\fB\-\-pass-fd, \-p \fInum\fP"
250 +read the passphrase from file descriptor \fInum\fP instead of the
254 returns 0 on success, nonzero on failure. When
255 diff -Naur util-linux-2.11g-o/mount/mount.8 util-linux-2.11g/mount/mount.8
256 --- util-linux-2.11g-o/mount/mount.8 Wed Jun 27 01:19:12 2001
257 +++ util-linux-2.11g/mount/mount.8 Mon Jul 9 16:11:38 2001
263 +If the mount requires a passphrase to be entered, read it from file
266 +instead of from the terminal.
269 Mount all filesystems (of the given types) mentioned in
271 @@ -1475,7 +1481,10 @@
272 .BR loop ", " offset " and " encryption ,
273 that are really options to
275 -If no explicit loop device is mentioned
276 +If the mount requires a passphrase, you will be prompted for one unless
277 +you specify a file descriptor to read from instead with the
279 +option. If no explicit loop device is mentioned
280 (but just an option `\fB\-o loop\fP' is given), then
282 will try to find some unused loop device and use that.
283 diff -Naur util-linux-2.11r-o/mount/mount.c util-linux-2.11r/mount/mount.c
284 --- util-linux-2.11r-o/mount/mount.c Fri Jun 8 01:24:28 2001
285 +++ util-linux-2.11r/mount/mount.c Mon Jul 9 16:22:13 2001
287 /* True if ruid != euid. */
290 +/* Contains the fd no. to read the passphrase from, if any */
291 +static int pfd = -1;
293 /* Map from -o and fstab option strings to the flag argument to mount(2). */
295 const char *opt; /* option name */
298 printf(_("mount: going to use the loop device %s\n"), *loopdev);
299 offset = opt_offset ? strtoul(opt_offset, NULL, 0) : 0;
300 - if (set_loop (*loopdev, *loopfile, offset, opt_encryption, &loopro)) {
301 + if (set_loop (*loopdev, *loopfile, offset, opt_encryption, pfd, &loopro)) {
303 printf(_("mount: failed setting up loop device\n"));
305 @@ -1305,6 +1308,7 @@
306 { "read-write", 0, 0, 'w' },
308 { "options", 1, 0, 'o' },
309 + { "pass-fd", 1, 0, 'p' },
310 { "test-opts", 1, 0, 'O' },
311 { "types", 1, 0, 't' },
312 { "bind", 0, 0, 128 },
313 @@ -1337,7 +1341,7 @@
314 " mount --bind olddir newdir\n"
315 "A device can be given by name, say /dev/hda1 or /dev/cdrom,\n"
316 "or by label, using -L label or by uuid, using -U uuid .\n"
317 - "Other options: [-nfFrsvw] [-o options].\n"
318 + "Other options: [-nfFrsvw] [-o options] [-p num].\n"
319 "For many more details, say man 8 mount .\n"
322 @@ -1353,6 +1357,7 @@
323 int c, result = 0, specseen;
324 char *options = NULL, *spec, *node;
325 char *volumelabel = NULL;
326 + char *passfd = NULL;
329 struct mntentchn *mc;
330 @@ -1374,7 +1379,7 @@
331 initproctitle(argc, argv);
334 - while ((c = getopt_long (argc, argv, "afFhlL:no:O:rsU:vVwt:",
335 + while ((c = getopt_long (argc, argv, "afFhlL:no:O:p:rsU:vVwt:",
336 longopts, NULL)) != -1) {
338 case 'a': /* mount everything in fstab */
339 @@ -1408,6 +1413,9 @@
343 + case 'p': /* read passphrase from given fd */
346 case 's': /* allow sloppy mount options */
349 @@ -1491,6 +1499,9 @@
350 printf(_("mount: mounting %s\n"), spec);
352 spec = NULL; /* just for gcc */
354 + if (passfd && sscanf(passfd,"%d",&pfd) != 1)
355 + die (EX_USAGE, _("mount: argument to --pass-fd or -p must be a number"));
357 switch (argc+specseen) {