diff options
Diffstat (limited to 'libpwdgrp')
-rw-r--r-- | libpwdgrp/uidgid_get.c | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/libpwdgrp/uidgid_get.c b/libpwdgrp/uidgid_get.c index a2d02a84f..32cbd21b9 100644 --- a/libpwdgrp/uidgid_get.c +++ b/libpwdgrp/uidgid_get.c @@ -1,26 +1,47 @@ #include "busybox.h" -unsigned uidgid_get(struct bb_uidgid_t *u, const char *ug /*, unsigned dogrp */) +int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok) { struct passwd *pwd; struct group *gr; - const char *g; + char *user, *group; + unsigned n; - /* g = 0; if (dogrp) g = strchr(ug, ':'); */ - g = strchr(ug, ':'); - if (g) { - int sz = (++g) - ug; - char buf[sz]; - safe_strncpy(buf, ug, sz); - pwd = getpwnam(buf); - } else - pwd = getpwnam(ug); + user = (char*)ug; + group = strchr(ug, ':'); + if (group) { + int sz = (++group) - ug; + user = alloca(sz); + /* copies sz-1 bytes, stores terminating '\0' */ + safe_strncpy(user, ug, sz); + } + if (numeric_ok) { + n = bb_strtou(user, NULL, 10); + if (!errno) { + u->uid = n; + pwd = getpwuid(n); + /* If we have e.g. "500" string without user */ + /* with uid 500 in /etc/passwd, we set gid == uid */ + u->gid = pwd ? pwd->pw_gid : n; + goto skip; + } + } + pwd = getpwnam(user); if (!pwd) return 0; u->uid = pwd->pw_uid; u->gid = pwd->pw_gid; - if (g) { - gr = getgrnam(g); + + skip: + if (group) { + if (numeric_ok) { + n = bb_strtou(group, NULL, 10); + if (!errno) { + u->gid = n; + return 1; + } + } + gr = getgrnam(group); if (!gr) return 0; u->gid = gr->gr_gid; } @@ -33,16 +54,16 @@ int main() { unsigned u; struct bb_uidgid_t ug; - u = uidgid_get(&ug, "apache"); + u = get_uidgid(&ug, "apache", 0); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = uidgid_get(&ug, "apache"); + u = get_uidgid(&ug, "apache", 0); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = uidgid_get(&ug, "apache:users"); + u = get_uidgid(&ug, "apache:users", 0); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = uidgid_get(&ug, "apache:users"); + u = get_uidgid(&ug, "apache:users", 0); printf("%u = %u:%u\n", u, ug.uid, ug.gid); return 0; } |