summaryrefslogtreecommitdiffhomepage
path: root/libpwdgrp/uidgid_get.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-12-28 05:44:47 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-12-28 05:44:47 +0000
commit9a44c4f91ce7e517d5325fd3743e6ad9d54ef3f0 (patch)
tree34292ef12cab59b118e91a6c58844ae25f1bee94 /libpwdgrp/uidgid_get.c
parentba092336f00644c1233735bae4b81382309955d8 (diff)
bb_xget[pw/gr]nam were horribly misnamed - fixed.
uidgid_get -> get_uidgid, add additional param (numeric_ok). Make chown use it. chown: fix "chown user: ...." install: fix incorrect use of bb_xget[pw/gr]nam
Diffstat (limited to 'libpwdgrp/uidgid_get.c')
-rw-r--r--libpwdgrp/uidgid_get.c55
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;
}