Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

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