1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/* $Source: /u/mark/src/pax/RCS/names.c,v $
*
* $Revision: 1.2 $
*
* names.c - Look up user and/or group names.
*
* DESCRIPTION
*
* These functions support UID and GID name lookup. The results are
* cached to improve performance.
*
* AUTHOR
*
* Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
*
* Sponsored by The USENIX Association for public distribution.
*
* Copyright (c) 1989 Mark H. Colburn.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice is duplicated in all such
* forms and that any documentation, advertising materials, and other
* materials related to such distribution and use acknowledge that the
* software was developed * by Mark H. Colburn and sponsored by The
* USENIX Association.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Log: names.c,v $
* Revision 1.2 89/02/12 10:05:05 mark
* 1.2 release fixes
*
* Revision 1.1 88/12/23 18:02:19 mark
* Initial revision
*
*/
#ifndef lint
static char *ident = "$Id: names.c,v 1.2 89/02/12 10:05:05 mark Exp $";
static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
#endif /* ! lint */
/* Headers */
#include "pax.h"
/* Defines */
#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
/* Internal Identifiers */
static int saveuid = -993;
static char saveuname[TUNMLEN];
static int my_uid = -993;
static int savegid = -993;
static char savegname[TGNMLEN];
static int my_gid = -993;
/* finduname - find a user or group name from a uid or gid
*
* DESCRIPTION
*
* Look up a user name from a uid/gid, maintaining a cache.
*
* PARAMETERS
*
* char uname[] - name (to be returned to user)
* int uuid - id of name to find
*
*
* RETURNS
*
* Returns a name which is associated with the user id given. If there
* is not name which corresponds to the user-id given, then a pointer
* to a string of zero length is returned.
*
* FIXME
*
* 1. for now it's a one-entry cache.
* 2. The "-993" is to reduce the chance of a hit on the first lookup.
*/
#ifdef __STDC__
char *finduname(int uuid)
#else
char *finduname(uuid)
int uuid;
#endif
{
struct passwd *pw;
if (uuid != saveuid) {
saveuid = uuid;
saveuname[0] = '\0';
pw = getpwuid(uuid);
if (pw) {
strncpy(saveuname, pw->pw_name, TUNMLEN);
}
}
return(saveuname);
}
/* finduid - get the uid for a given user name
*
* DESCRIPTION
*
* This does just the opposit of finduname. Given a user name it
* finds the corresponding UID for that name.
*
* PARAMETERS
*
* char uname[] - username to find a UID for
*
* RETURNS
*
* The UID which corresponds to the uname given, if any. If no UID
* could be found, then the UID which corrsponds the user running the
* program is returned.
*
*/
#ifdef __STDC__
int finduid(char *uname)
#else
int finduid(uname)
char *uname;
#endif
{
struct passwd *pw;
extern struct passwd *getpwnam();
if (uname[0] != saveuname[0]/* Quick test w/o proc call */
||0 != strncmp(uname, saveuname, TUNMLEN)) {
strncpy(saveuname, uname, TUNMLEN);
pw = getpwnam(uname);
if (pw) {
saveuid = pw->pw_uid;
} else {
saveuid = myuid;
}
}
return (saveuid);
}
/* findgname - look up a group name from a gid
*
* DESCRIPTION
*
* Look up a group name from a gid, maintaining a cache.
*
*
* PARAMETERS
*
* int ggid - goupid of group to find
*
* RETURNS
*
* A string which is associated with the group ID given. If no name
* can be found, a string of zero length is returned.
*/
#ifdef __STDC__
char *findgname(int ggid)
#else
char *findgname(ggid)
int ggid;
#endif
{
struct group *gr;
if (ggid != savegid) {
savegid = ggid;
savegname[0] = '\0';
#ifndef _POSIX_SOURCE
setgrent();
#endif
gr = getgrgid(ggid);
if (gr) {
strncpy(savegname, gr->gr_name, TGNMLEN);
}
}
return(savegname);
}
/* findgid - get the gid for a given group name
*
* DESCRIPTION
*
* This does just the opposit of finduname. Given a group name it
* finds the corresponding GID for that name.
*
* PARAMETERS
*
* char uname[] - groupname to find a GID for
*
* RETURNS
*
* The GID which corresponds to the uname given, if any. If no GID
* could be found, then the GID which corrsponds the group running the
* program is returned.
*
*/
#ifdef __STDC__
int findgid(char *gname)
#else
int findgid(gname)
char *gname;
#endif
{
struct group *gr;
/* Quick test w/o proc call */
if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) {
strncpy(savegname, gname, TUNMLEN);
gr = getgrnam(gname);
if (gr) {
savegid = gr->gr_gid;
} else {
savegid = mygid;
}
}
return (savegid);
}
|