uidgid.c (1607B)
1 #include <sys/types.h> 2 #include <pwd.h> 3 #include <grp.h> 4 #include "uidgid.h" 5 #include "str.h" 6 #include "scan.h" 7 8 static unsigned int uidgids_set(struct uidgid *u, char *ug); 9 10 /* user */ 11 unsigned int uidgid_get(struct uidgid *u, char *ug) { 12 struct passwd *pwd =0; 13 14 if (! (pwd =getpwnam(ug))) return(0); 15 u->gid[0] =pwd->pw_gid; u->gids =1; 16 u->uid =pwd->pw_uid; 17 return(1); 18 } 19 20 /* uid:gid[:gid[:gid]...] */ 21 unsigned int uidgids_set(struct uidgid *u, char *ug) { 22 unsigned long id; 23 int i; 24 25 if (*(ug +=scan_ulong(ug, &id)) != ':') return(0); 26 u->uid =(uid_t)id; 27 ++ug; 28 for (i =0; i < 60; ++i, ++ug) { 29 ug +=scan_ulong(ug, &id); 30 u->gid[i] =(gid_t)id; 31 if (*ug != ':') { ++i; break; } 32 } 33 u->gid[i] =0; 34 u->gids =i; 35 if (*ug) return(0); 36 return(1); 37 } 38 39 /* [:]user[:group[:group]...] */ 40 unsigned int uidgids_get(struct uidgid *u, char *ug) { 41 char *g =0; 42 struct passwd *pwd =0; 43 struct group *gr =0; 44 int i, d =0; 45 46 if (*ug == ':') return(uidgids_set(u, ug +1)); 47 if (ug[(d =str_chr(ug, ':'))] == ':') { 48 ug[d] =0; 49 g =ug +d +1; 50 } 51 if (! (pwd =getpwnam(ug))) { if (g) ug[d] =':'; return(0); } 52 u->uid =pwd->pw_uid; 53 if (! g) { 54 u->gid[0] =pwd->pw_gid; 55 u->gids =1; 56 return(1); 57 } 58 ug[d] =':'; 59 for (i =0; i < 60; ++i) { 60 if (g[(d =str_chr(g, ':'))] == ':') { 61 g[d] =0; 62 if (! (gr =getgrnam(g))) { g[d] =':'; return(0); } 63 g[d] =':'; 64 u->gid[i] =gr->gr_gid; 65 g +=d +1; 66 } 67 else { 68 if (! (gr =getgrnam(g))) return(0); 69 u->gid[i++] =gr->gr_gid; 70 break; 71 } 72 } 73 u->gid[i] =0; 74 u->gids =i; 75 return(1); 76 }