diff options
author | Karel Zak | 2012-01-11 16:17:06 +0100 |
---|---|---|
committer | Karel Zak | 2012-01-11 16:17:06 +0100 |
commit | dbae36fe40f60065d2cfa2843060edd356e2e57a (patch) | |
tree | 243b75b55efcc31bf0900d6f12bf9fa17ed40746 /libmount/samples/mount.c | |
parent | libmount: add support for encrypted loopdevs (diff) | |
download | kernel-qcow2-util-linux-dbae36fe40f60065d2cfa2843060edd356e2e57a.tar.gz kernel-qcow2-util-linux-dbae36fe40f60065d2cfa2843060edd356e2e57a.tar.xz kernel-qcow2-util-linux-dbae36fe40f60065d2cfa2843060edd356e2e57a.zip |
libmount: add --pass-fd to samples/mount
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/samples/mount.c')
-rw-r--r-- | libmount/samples/mount.c | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/libmount/samples/mount.c b/libmount/samples/mount.c index 08b92332b..cdd70b8f6 100644 --- a/libmount/samples/mount.c +++ b/libmount/samples/mount.c @@ -26,6 +26,7 @@ #include <getopt.h> #include <unistd.h> #include <sys/types.h> +#include <sys/mman.h> #include <libmount.h> @@ -33,12 +34,12 @@ #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 |