From 4796b46d69f30237db0a0788b4df511d3112a0b8 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 27 Sep 2010 12:14:41 +0200 Subject: [PATCH] libmount: add mnt_optstr_fix_user Signed-off-by: Karel Zak --- shlibs/mount/src/mount.h.in | 1 + shlibs/mount/src/mount.sym | 1 + shlibs/mount/src/optstr.c | 42 ++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index ac69ac55..37ff3366 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -177,6 +177,7 @@ extern int mnt_optstr_apply_flags(char **optstr, unsigned long flags, extern int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next); extern int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next); extern int mnt_optstr_fix_secontext(char **optstr, char *value, size_t valsz, char **next); +extern int mnt_optstr_fix_user(char **optstr, char *value, size_t valsz, char **next); /* iter.c */ enum { diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 308abcde..bb3ac242 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -82,6 +82,7 @@ global: mnt_optstr_fix_gid; mnt_optstr_fix_selinux; mnt_optstr_fix_uid; + mnt_optstr_fix_user; mnt_optstr_apply_flags; mnt_optstr_get_flags; mnt_parse_version_string; diff --git a/shlibs/mount/src/optstr.c b/shlibs/mount/src/optstr.c index 0cfcd513..dd8b5451 100644 --- a/shlibs/mount/src/optstr.c +++ b/shlibs/mount/src/optstr.c @@ -849,6 +849,43 @@ int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next) return rc; } +/** + * mnt_optstr_fix_user: + * @optstr: string with comma separated list of options + * @value: pointer to place where has to start the value + * @valsz: size of the value or zero if value not define + * @next: returns pointer to the next option (optional argument) + + * Add/replace username. This is usually used to convert "user" (without value) + * to to "user=" -- in this case the @value has to pointer behind the + * "user" option name; in case you want to replace already defined + * then the @valsz must be greater than zero. + * + * Returns: 0 on success, negative number in case of error. + */ +int mnt_optstr_fix_user(char **optstr, char *value, size_t valsz, char **next) +{ + char *username; + int rc = 0; + + if (!optstr || !value) + return -EINVAL; + + username = mnt_get_username(getuid()); + if (!username) + return -ENOMEM; + + if (!valsz || strncmp(value, username, valsz)) { + if (valsz) + /* remove old value */ + mnt_optstr_remove_option_at(optstr, value, value + valsz); + + rc = insert_substring(optstr, value, username, next); + } + + free(username); + return rc; +} #ifdef TEST_PROGRAM @@ -1061,6 +1098,9 @@ int test_fix(struct mtest *ts, int argc, char *argv[]) rc = mnt_optstr_fix_gid(&optstr, val, valsz, &next); else if (!strncmp(name, "context", 7)) rc = mnt_optstr_fix_secontext(&optstr, val, valsz, &next); + else if (!strncmp(name, "user", 4)) + rc = mnt_optstr_fix_user(&optstr, + val ? val : name + namesz, valsz, &next); if (rc) break; } @@ -1083,7 +1123,7 @@ int main(int argc, char *argv[]) { "--split", test_split, " split into FS, VFS and userspace" }, { "--flags", test_flags, " convert options to MS_* flags" }, { "--apply", test_apply, "--{linux,user} apply mask to optstr" }, - { "--fix", test_fix, " fix uid=, gid= and context=" }, + { "--fix", test_fix, " fix uid=, gid=, user, and context=" }, { NULL } }; -- 2.39.5