From: Matt Helsley Date: Sun, 8 Mar 2009 16:09:27 +0000 (+0100) Subject: liblxc: Add username and uid lookup/check. X-Git-Tag: lxc_0_6_1~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7f85ccbb4bb9e2cc954a874179f137dc6f6ea18;p=thirdparty%2Flxc.git liblxc: Add username and uid lookup/check. Add the ability to lookup usernames and check uids. Bails out early if the given uid/name does not exist and avoids using atoi() (which is bad because we can't tell if it parsed an int or a pumpkin). Signed-off-by: Matt Helsley Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_unshare.c b/src/lxc/lxc_unshare.c index fb5ec62da..5e9de2a98 100644 --- a/src/lxc/lxc_unshare.c +++ b/src/lxc/lxc_unshare.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "lxc_namespace.h" @@ -48,12 +49,37 @@ void usage(char *cmd) _exit(1); } +static uid_t lookup_user(const char *optarg) +{ + char name[sysconf(_SC_LOGIN_NAME_MAX)]; + uid_t uid = -1; + + if (!optarg || (optarg[0] == '\0')) + return uid; + if (sscanf(optarg, "%u", &uid) < 1) { + struct passwd pwent; /* not a uid -- perhaps a username */ + struct passwd *pent; + + if (sscanf(optarg, "%s", name) < 1) + return uid; + if (getpwnam_r(name, &pwent, NULL, 0, &pent) || !pent) + return uid; + uid = pent->pw_uid; + } else { + if (getpwuid_r(uid, NULL, NULL, 0, NULL)) { + uid = -1; + return uid; + } + } + return uid; +} + int main(int argc, char *argv[]) { int opt, nbargs = 0, status = 1, hastofork = 0; char **args; long flags = 0; - uid_t uid = 0; + uid_t uid = -1; /* valid only if (flags & CLONE_NEWUSER) */ pid_t pid; while ((opt = getopt(argc, argv, "fmphiu:n")) != -1) { @@ -71,8 +97,10 @@ int main(int argc, char *argv[]) flags |= CLONE_NEWIPC; break; case 'u': + uid = lookup_user(optarg); + if (uid == -1) + break; flags |= CLONE_NEWUSER; - uid = atoi(optarg); break; case 'n': flags |= CLONE_NEWNET;