rpm  4.5
names.c
Go to the documentation of this file.
1 /*@-mods@*/
8 #include "system.h"
9 
10 #include "rpmbuild.h"
11 #include "debug.h"
12 
13 typedef /*@owned@*/ /*@null@*/ const char * ugstr_t;
14 
15 /*@unchecked@*/
16 static uid_t uids[1024];
17 /*@unchecked@*/
18 static ugstr_t unames[1024];
19 /*@unchecked@*/
20 static int uid_used = 0;
21 
22 /*@unchecked@*/
23 static gid_t gids[1024];
24 /*@unchecked@*/
25 static ugstr_t gnames[1024];
26 /*@unchecked@*/
27 static int gid_used = 0;
28 
29 /*@-boundswrite@*/
30 void freeNames(void)
31 {
32  int x;
33  for (x = 0; x < uid_used; x++)
34  unames[x] = _free(unames[x]);
35  for (x = 0; x < gid_used; x++)
36  gnames[x] = _free(gnames[x]);
37 }
38 /*@=boundswrite@*/
39 
40 /*@-boundswrite@*/
41 const char *getUname(uid_t uid)
42  /*@globals uid_used, uids, unames @*/
43  /*@modifies uid_used, uids, unames @*/
44 {
45  struct passwd *pw;
46  int x;
47 
48  for (x = 0; x < uid_used; x++) {
49  if (unames[x] == NULL) continue;
50  if (uids[x] == uid)
51  return unames[x];
52  }
53 
54  /* XXX - This is the other hard coded limit */
55  if (x == 1024)
56  rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
57 
58  if ((pw = getpwuid(uid)) == NULL)
59  return NULL;
60  uids[uid_used] = uid;
61  unames[uid_used] = xstrdup(pw->pw_name);
62  return unames[uid_used++];
63 }
64 /*@=boundswrite@*/
65 
66 /*@-boundswrite@*/
67 const char *getUnameS(const char *uname)
68  /*@globals uid_used, uids, unames @*/
69  /*@modifies uid_used, uids, unames @*/
70 {
71  struct passwd *pw;
72  int x;
73 
74  for (x = 0; x < uid_used; x++) {
75  if (unames[x] == NULL) continue;
76  if (!strcmp(unames[x],uname))
77  return unames[x];
78  }
79 
80  /* XXX - This is the other hard coded limit */
81  if (x == 1024)
82  rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
83 
84  if ((pw = getpwnam(uname)) == NULL) {
85  uids[uid_used] = -1;
86  unames[uid_used] = xstrdup(uname);
87  } else {
88  uids[uid_used] = pw->pw_uid;
89  unames[uid_used] = xstrdup(pw->pw_name);
90  }
91  return unames[uid_used++];
92 }
93 /*@=boundswrite@*/
94 
95 /*@-boundswrite@*/
96 uid_t getUidS(const char *uname)
97  /*@globals uid_used, uids, unames @*/
98  /*@modifies uid_used, uids, unames @*/
99 {
100  struct passwd *pw;
101  int x;
102 
103  for (x = 0; x < uid_used; x++) {
104  if (unames[x] == NULL) continue;
105  if (!strcmp(unames[x],uname))
106  return uids[x];
107  }
108 
109  /* XXX - This is the other hard coded limit */
110  if (x == 1024)
111  rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
112 
113  if ((pw = getpwnam(uname)) == NULL) {
114  uids[uid_used] = -1;
115  unames[uid_used] = xstrdup(uname);
116  } else {
117  uids[uid_used] = pw->pw_uid;
118  unames[uid_used] = xstrdup(pw->pw_name);
119  }
120  return uids[uid_used++];
121 }
122 /*@=boundswrite@*/
123 
124 /*@-boundswrite@*/
125 const char *getGname(gid_t gid)
126  /*@globals gid_used, gids, gnames @*/
127  /*@modifies gid_used, gids, gnames @*/
128 {
129  struct group *gr;
130  int x;
131 
132  for (x = 0; x < gid_used; x++) {
133  if (gnames[x] == NULL) continue;
134  if (gids[x] == gid)
135  return gnames[x];
136  }
137 
138  /* XXX - This is the other hard coded limit */
139  if (x == 1024)
140  rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
141 
142  if ((gr = getgrgid(gid)) == NULL)
143  return NULL;
144  gids[gid_used] = gid;
145  gnames[gid_used] = xstrdup(gr->gr_name);
146  return gnames[gid_used++];
147 }
148 /*@=boundswrite@*/
149 
150 /*@-boundswrite@*/
151 const char *getGnameS(const char *gname)
152  /*@globals gid_used, gids, gnames @*/
153  /*@modifies gid_used, gids, gnames @*/
154 {
155  struct group *gr;
156  int x;
157 
158  for (x = 0; x < gid_used; x++) {
159  if (gnames[x] == NULL) continue;
160  if (!strcmp(gnames[x], gname))
161  return gnames[x];
162  }
163 
164  /* XXX - This is the other hard coded limit */
165  if (x == 1024)
166  rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
167 
168  if ((gr = getgrnam(gname)) == NULL) {
169  gids[gid_used] = -1;
170  gnames[gid_used] = xstrdup(gname);
171  } else {
172  gids[gid_used] = gr->gr_gid;
173  gnames[gid_used] = xstrdup(gr->gr_name);
174  }
175  return gnames[gid_used++];
176 }
177 /*@=boundswrite@*/
178 
179 /*@-boundswrite@*/
180 gid_t getGidS(const char *gname)
181  /*@globals gid_used, gids, gnames @*/
182  /*@modifies gid_used, gids, gnames @*/
183 {
184  struct group *gr;
185  int x;
186 
187  for (x = 0; x < gid_used; x++) {
188  if (gnames[x] == NULL) continue;
189  if (!strcmp(gnames[x], gname))
190  return gids[x];
191  }
192 
193  /* XXX - This is the other hard coded limit */
194  if (x == 1024)
195  rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
196 
197  if ((gr = getgrnam(gname)) == NULL) {
198  gids[gid_used] = -1;
199  gnames[gid_used] = xstrdup(gname);
200  } else {
201  gids[gid_used] = gr->gr_gid;
202  gnames[gid_used] = xstrdup(gr->gr_name);
203  }
204  return gids[gid_used++];
205 }
206 /*@=boundswrite@*/
207 
209 {
210  static int_32 buildTime[1];
211 
212 /*@-boundsread@*/
213  if (buildTime[0] == 0)
214  buildTime[0] = (int_32) time(NULL);
215 /*@=boundsread@*/
216  return buildTime;
217 }
218 
219 /*@-boundswrite@*/
220 const char * buildHost(void)
221 {
222  static char hostname[1024];
223  static int oneshot = 0;
224  struct hostent *hbn;
225 
226  if (! oneshot) {
227  (void) gethostname(hostname, sizeof(hostname));
228  /*@-unrecog -multithreaded @*/
229  /*@-globs@*/ /* FIX: h_errno access */
230  hbn = gethostbyname(hostname);
231  /*@=globs@*/
232  /*@=unrecog =multithreaded @*/
233  if (hbn)
234  strcpy(hostname, hbn->h_name);
235  else
237  _("Could not canonicalize hostname: %s\n"), hostname);
238  oneshot = 1;
239  }
240  return(hostname);
241 }
242 /*@=boundswrite@*/
243 /*@=mods@*/