rpm  5.4.14
ugid.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 #include "ugid.h"
7 #include "debug.h"
8 
9 /* unameToUid(), uidTouname() and the group variants are really poorly
10  implemented. They really ought to use hash tables. I just made the
11  guess that most files would be owned by root or the same person/group
12  who owned the last file. Those two values are cached, everything else
13  is looked up via getpw() and getgr() functions. If this performs
14  too poorly I'll have to implement it properly :-( */
15 
16 int unameToUid(const char * thisUname, uid_t * uid)
17 {
18 /*@only@*/ static char * lastUname = NULL;
19  static size_t lastUnameLen = 0;
20  static size_t lastUnameAlloced;
21  static uid_t lastUid;
22  struct passwd _pw, *pwent = NULL;
23  char _b[BUFSIZ];
24  size_t _nb = sizeof(_b);
25  size_t thisUnameLen;
26 
27 #ifdef SUSE_REFERENCE
28 news
29 uucp
30 man
31 nobody
32 wwwrun
33 mail
34 lp
35 #endif
36  if (thisUname == NULL) {
37  lastUnameLen = 0;
38  return -1;
39 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */
40  } else if (strcmp(thisUname, "root") == 0) {
41  *uid = 0;
42  return 0;
43 #endif
44  }
45 
46  thisUnameLen = strlen(thisUname);
47  if (lastUname == NULL || thisUnameLen != lastUnameLen ||
48  strcmp(thisUname, lastUname) != 0)
49  {
50  if (lastUnameAlloced < thisUnameLen + 1) {
51  lastUnameAlloced = thisUnameLen + 10;
52  lastUname = (char *) DRD_xrealloc(lastUname, lastUnameAlloced);
53  }
54  strcpy(lastUname, thisUname);
55 
56  if (getpwnam_r(thisUname, &_pw, _b, _nb, &pwent) || pwent == NULL) {
57  /*@-internalglobs@*/ /* FIX: shrug */
58  endpwent();
59  /*@=internalglobs@*/
60  if (getpwnam_r(thisUname, &_pw, _b, _nb, &pwent) || pwent == NULL)
61  return -1;
62  }
63  lastUid = pwent->pw_uid;
64  }
65 
66  *uid = lastUid;
67 
68  return 0;
69 }
70 
71 int gnameToGid(const char * thisGname, gid_t * gid)
72 {
73 /*@only@*/ static char * lastGname = NULL;
74  static size_t lastGnameLen = 0;
75  static size_t lastGnameAlloced;
76  static gid_t lastGid;
77  struct group _gr, *grent = NULL;
78  char _b[BUFSIZ];
79  size_t _nb = sizeof(_b);
80  size_t thisGnameLen;
81 
82 #ifdef SUSE_REFERENCE
83 news
84 dialout
85 uucp
86 lp
87 #endif
88  if (thisGname == NULL) {
89  lastGnameLen = 0;
90  return -1;
91 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */
92  } else if (strcmp(thisGname, "root") == 0) {
93  *gid = 0;
94  return 0;
95 #endif
96  }
97 
98  thisGnameLen = strlen(thisGname);
99  if (lastGname == NULL || thisGnameLen != lastGnameLen ||
100  strcmp(thisGname, lastGname) != 0)
101  {
102  if (lastGnameAlloced < thisGnameLen + 1) {
103  lastGnameAlloced = thisGnameLen + 10;
104  lastGname = (char *) DRD_xrealloc(lastGname, lastGnameAlloced);
105  }
106  strcpy(lastGname, thisGname);
107 
108  if (getgrnam_r(thisGname, &_gr, _b, _nb, &grent) || grent == NULL) {
109  /*@-internalglobs@*/ /* FIX: shrug */
110  endgrent();
111  /*@=internalglobs@*/
112  if (getgrnam_r(thisGname, &_gr, _b, _nb, &grent) || grent == NULL) {
113 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */
114  /* XXX The filesystem package needs group/lock w/o getgrnam. */
115  if (strcmp(thisGname, "lock") == 0) {
116  *gid = lastGid = 54;
117  return 0;
118  } else
119  if (strcmp(thisGname, "mail") == 0) {
120  *gid = lastGid = 12;
121  return 0;
122  } else
123 #endif
124  return -1;
125  }
126  }
127  lastGid = grent->gr_gid;
128  }
129 
130  *gid = lastGid;
131 
132  return 0;
133 }
134 
135 char * uidToUname(uid_t uid)
136 {
137  static uid_t lastUid = (uid_t) -1;
138 /*@only@*/ static char * lastUname = NULL;
139  static size_t lastUnameLen = 0;
140 
141  if (uid == (uid_t) -1) {
142  lastUid = (uid_t) -1;
143  return NULL;
144 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */
145  } else if (uid == (uid_t) 0) {
146  return (char *) "root";
147 #endif
148  } else if (uid == lastUid) {
149  return lastUname;
150  } else {
151  struct passwd _pw, *pwent = NULL;
152  char _b[BUFSIZ];
153  size_t _nb = sizeof(_b);
154  size_t len;
155 
156  if (getpwuid_r(uid, &_pw, _b, _nb, &pwent) || pwent == NULL)
157  return NULL;
158 
159  lastUid = uid;
160  len = strlen(pwent->pw_name);
161  if (lastUnameLen < len + 1) {
162  lastUnameLen = len + 20;
163  lastUname = (char *) DRD_xrealloc(lastUname, lastUnameLen);
164  }
165  strcpy(lastUname, pwent->pw_name);
166 
167  return lastUname;
168  }
169 }
170 
171 char * gidToGname(gid_t gid)
172 {
173  static gid_t lastGid = (gid_t) -1;
174 /*@only@*/ static char * lastGname = NULL;
175  static size_t lastGnameLen = 0;
176 
177  if (gid == (gid_t) -1) {
178  lastGid = (gid_t) -1;
179  return NULL;
180 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */
181  } else if (gid == (gid_t) 0) {
182  return (char *) "root";
183 #endif
184  } else if (gid == lastGid) {
185  return lastGname;
186  } else {
187  struct group _gr, *grent = NULL;
188  char _b[BUFSIZ];
189  size_t _nb = sizeof(_b);
190  size_t len;
191 
192  if (getgrgid_r(gid, &_gr, _b, _nb, &grent) || grent == NULL)
193  return NULL;
194 
195  lastGid = gid;
196  len = strlen(grent->gr_name);
197  if (lastGnameLen < len + 1) {
198  lastGnameLen = len + 20;
199  lastGname = (char *) DRD_xrealloc(lastGname, lastGnameLen);
200  }
201  strcpy(lastGname, grent->gr_name);
202 
203  return lastGname;
204  }
205 }
char * gidToGname(gid_t gid)
Definition: ugid.c:171
#define DRD_xrealloc(_ptr, _size)
Definition: debug.h:175
int gnameToGid(const char *thisGname, gid_t *gid)
Definition: ugid.c:71
return strcmp(ame->name, bme->name)
char * uidToUname(uid_t uid)
Definition: ugid.c:135
return NULL
Definition: poptALL.c:613
int unameToUid(const char *thisUname, uid_t *uid)
Definition: ugid.c:16
int len
Definition: rpmdb-py.c:119