Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 1 | #include "busybox.h" |
| 2 | |
| 3 | unsigned uidgid_get(struct bb_uidgid_t *u, const char *ug /*, unsigned dogrp */) |
| 4 | { |
| 5 | struct passwd *pwd; |
| 6 | struct group *gr; |
| 7 | const char *g; |
| 8 | |
| 9 | /* g = 0; if (dogrp) g = strchr(ug, ':'); */ |
| 10 | g = strchr(ug, ':'); |
| 11 | if (g) { |
| 12 | int sz = (++g) - ug; |
| 13 | char buf[sz]; |
| 14 | safe_strncpy(buf, ug, sz); |
| 15 | pwd = getpwnam(buf); |
| 16 | } else |
| 17 | pwd = getpwnam(ug); |
| 18 | if (!pwd) |
| 19 | return 0; |
| 20 | u->uid = pwd->pw_uid; |
| 21 | u->gid = pwd->pw_gid; |
| 22 | if (g) { |
| 23 | gr = getgrnam(g); |
| 24 | if (!gr) return 0; |
| 25 | u->gid = gr->gr_gid; |
| 26 | } |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | #if 0 |
| 31 | #include <stdio.h> |
| 32 | int main() |
| 33 | { |
| 34 | unsigned u; |
| 35 | struct bb_uidgid_t ug; |
| 36 | u = uidgid_get(&ug, "apache"); |
| 37 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 38 | ug.uid = ug.gid = 1111; |
| 39 | u = uidgid_get(&ug, "apache"); |
| 40 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 41 | ug.uid = ug.gid = 1111; |
| 42 | u = uidgid_get(&ug, "apache:users"); |
| 43 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 44 | ug.uid = ug.gid = 1111; |
| 45 | u = uidgid_get(&ug, "apache:users"); |
| 46 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 47 | return 0; |
| 48 | } |
| 49 | #endif |