]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add --pass-fd to samples/mount
authorKarel Zak <kzak@redhat.com>
Wed, 11 Jan 2012 15:17:06 +0000 (16:17 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Jan 2012 15:17:06 +0000 (16:17 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/samples/Makefile.am
libmount/samples/mount.c

index 92f8447a796f3e6ad197bc6dbe5ddef3f75987eb..a8c001f7d3fb0309d896e7cc64b1ca4fe4520b92 100644 (file)
@@ -5,6 +5,10 @@ AM_LDFLAGS += $(ul_libmount_la)
 
 noinst_PROGRAMS = mount umount
 
-mount_SOURCES = mount.c $(top_srcdir)/lib/env.c
+mount_SOURCES = mount.c \
+               $(top_srcdir)/lib/env.c \
+               $(top_srcdir)/lib/xgetpass.c \
+               $(top_srcdir)/lib/strutils.c
+
 umount_SOURCES = umount.c $(top_srcdir)/lib/env.c
 
index 08b92332ba7a95ab8e3287f4bdf9bf348fa330cf..cdd70b8f6e24d4f371c5c2a8e8aa8df28dfe445e 100644 (file)
@@ -26,6 +26,7 @@
 #include <getopt.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/mman.h>
 
 #include <libmount.h>
 
 #include "c.h"
 #include "env.h"
 #include "optutils.h"
+#include "strutils.h"
+#include "xgetpass.h"
 
 /*** TODO: DOCS:
  *
- *  -p, --pass-fd      is unsupported
  *  --guess-fstype     is unsupported
- *  -c =                --no-canonicalize
  */
 
 /* exit status */
@@ -51,6 +52,8 @@
 #define EX_FAIL               32       /* mount failure */
 #define EX_SOMEOK      64      /* some mount succeeded */
 
+static int passfd = -1;
+
 static void __attribute__((__noreturn__)) exit_non_root(const char *option)
 {
        const uid_t ruid = getuid();
@@ -90,6 +93,31 @@ static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)
        return 0;
 }
 
+static char *encrypt_pass_get(struct libmnt_context *cxt)
+{
+       if (!cxt)
+               return 0;
+
+#ifdef MCL_FUTURE
+       if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
+               warn(_("couldn't lock into memory"));
+               return NULL;
+       }
+#endif
+       return xgetpass(passfd, _("Password: "));
+}
+
+static void encrypt_pass_release(struct libmnt_context *cxt, char *pwd)
+{
+       char *p = pwd;
+
+       while (p && *p)
+               *p++ = '\0';
+
+       free(pwd);
+       munlockall();
+}
+
 static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
 {
        struct libmnt_table *tb;
@@ -219,6 +247,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        fprintf(out, _(
        " -o, --options <list>    comma-separated list of mount options\n"
        " -O, --test-opts <list>  limit the set of filesystems (use with -a)\n"
+       " -p, --pass-fd <num>     read the passphrase from file descriptor\n"
        " -r, --read-only         mount the filesystem read-only (same as -o ro)\n"
        " -t, --types <list>      limit the set of filesystem types\n"));
        fprintf(out, _(
@@ -295,6 +324,7 @@ int main(int argc, char **argv)
                { "rw", 0, 0, 'w' },
                { "options", 1, 0, 'o' },
                { "test-opts", 1, 0, 'O' },
+               { "pass-fd", 1, 0, 'p' },
                { "types", 1, 0, 't' },
                { "uuid", 1, 0, 'U' },
                { "label", 1, 0, 'L'},
@@ -327,11 +357,11 @@ int main(int argc, char **argv)
 
        mnt_context_set_tables_errcb(cxt, table_parser_errcb);
 
-       while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:",
+       while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:p:rRsU:vVwt:",
                                        longopts, NULL)) != -1) {
 
                /* only few options are allowed for non-root users */
-               if (mnt_context_is_restricted(cxt) && !strchr("hlLUVv", c))
+               if (mnt_context_is_restricted(cxt) && !strchr("hlLUVvp", c))
                        exit_non_root(option_to_longopt(c, longopts));
 
                switch(c) {
@@ -378,6 +408,10 @@ int main(int argc, char **argv)
                        if (mnt_context_set_options_pattern(cxt, optarg))
                                err(EX_SYSERR, _("failed to set options pattern"));
                        break;
+               case 'p':
+                       passfd = strtol_or_err(optarg,
+                                       _("invalid passphrase file descriptor"));
+                       break;
                case 'L':
                case 'U':
                        if (source)
@@ -454,6 +488,8 @@ int main(int argc, char **argv)
        else if (types)
                mnt_context_set_fstype(cxt, types);
 
+       mnt_context_set_passwd_cb(cxt, encrypt_pass_get, encrypt_pass_release);
+
        if (all) {
                /*
                 * A) Mount all