Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <pwd.h> |
| 3 | #include <grp.h> |
| 4 | #include "uidgid.h" |
| 5 | |
| 6 | static unsigned str_chr(const char *s, int c) |
| 7 | { |
| 8 | const char *t = s; |
| 9 | while (t[0] && t[0] != (char)c) |
| 10 | t++; |
| 11 | return t - s; |
| 12 | } |
| 13 | |
| 14 | |
| 15 | unsigned uidgid_get(struct uidgid *u, char *ug, unsigned dogrp) { |
| 16 | char *g = 0; |
| 17 | struct passwd *pwd = 0; |
| 18 | struct group *gr = 0; |
| 19 | int i, d = 0; |
| 20 | |
| 21 | if (dogrp) |
| 22 | d = str_chr(ug, ':'); |
| 23 | if (ug[d] == ':') { |
| 24 | ug[d] = 0; |
| 25 | g = ug + d + 1; |
| 26 | } |
| 27 | pwd = getpwnam(ug); |
| 28 | if (!pwd) { |
| 29 | if (g) ug[d] = ':'; |
| 30 | return 0; |
| 31 | } |
| 32 | if (g) { |
| 33 | ug[d] = ':'; |
| 34 | for (i = 0; i < 60; ++i) { |
| 35 | d = str_chr(g, ':'); |
| 36 | if (g[d] == ':') { |
| 37 | g[d] = 0; |
| 38 | gr = getgrnam(g); |
| 39 | if (!gr) { |
| 40 | g[d] = ':'; |
| 41 | return 0; |
| 42 | } |
| 43 | g[d] = ':'; |
| 44 | u->gid[i] = gr->gr_gid; |
| 45 | g += d+1; |
| 46 | } |
| 47 | else { |
| 48 | gr = getgrnam(g); |
| 49 | if (!gr) return 0; |
| 50 | u->gid[i++] = gr->gr_gid; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | u->gid[i] = 0; |
| 55 | u->gids = i; |
| 56 | } |
| 57 | if (!g) { |
| 58 | u->gid[0] = pwd->pw_gid; |
| 59 | u->gids = 1; |
| 60 | } |
| 61 | u->uid = pwd->pw_uid; |
| 62 | return 1; |
| 63 | } |