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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1998-1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: getpwent_r.c,v 1.6.18.2 2005/04/27 05:00:59 sra Exp $";
#endif /* LIBC_SCCS and not lint */
#include <port_before.h>
#if !defined(_REENTRANT) || !defined(DO_PTHREADS) || !defined(WANT_IRS_PW)
static int getpwent_r_not_required = 0;
#else
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#if (defined(POSIX_GETPWNAM_R) || defined(POSIX_GETPWUID_R))
#if defined(_POSIX_PTHREAD_SEMANTICS)
/* turn off solaris remapping in <grp.h> */
#undef _POSIX_PTHREAD_SEMANTICS
#include <pwd.h>
#define _POSIX_PTHREAD_SEMANTICS 1
#else
#define _UNIX95 1
#include <pwd.h>
#endif
#else
#include <pwd.h>
#endif
#include <port_after.h>
#ifdef PASS_R_RETURN
static int
copy_passwd(struct passwd *, struct passwd *, char *buf, int buflen);
/* POSIX 1003.1c */
#ifdef POSIX_GETPWNAM_R
int
__posix_getpwnam_r(const char *login, struct passwd *pwptr,
char *buf, size_t buflen, struct passwd **result) {
#else
int
getpwnam_r(const char *login, struct passwd *pwptr,
char *buf, size_t buflen, struct passwd **result) {
#endif
struct passwd *pw = getpwnam(login);
int res;
if (pw == NULL) {
*result = NULL;
return (0);
}
res = copy_passwd(pw, pwptr, buf, buflen);
*result = res ? NULL : pwptr;
return (res);
}
#ifdef POSIX_GETPWNAM_R
struct passwd *
getpwnam_r(const char *login, struct passwd *pwptr, char *buf, int buflen) {
struct passwd *pw = getpwnam(login);
int res;
if (pw == NULL)
return (NULL);
res = copy_passwd(pw, pwptr, buf, buflen);
return (res ? NULL : pwptr);
}
#endif
/* POSIX 1003.1c */
#ifdef POSIX_GETPWUID_R
int
__posix_getpwuid_r(uid_t uid, struct passwd *pwptr,
char *buf, int buflen, struct passwd **result) {
#else
int
getpwuid_r(uid_t uid, struct passwd *pwptr,
char *buf, size_t buflen, struct passwd **result) {
#endif
struct passwd *pw = getpwuid(uid);
int res;
if (pw == NULL) {
*result = NULL;
return (0);
}
res = copy_passwd(pw, pwptr, buf, buflen);
*result = res ? NULL : pwptr;
return (res);
}
#ifdef POSIX_GETPWUID_R
struct passwd *
getpwuid_r(uid_t uid, struct passwd *pwptr, char *buf, int buflen) {
struct passwd *pw = getpwuid(uid);
int res;
if (pw == NULL)
return (NULL);
res = copy_passwd(pw, pwptr, buf, buflen);
return (res ? NULL : pwptr);
}
#endif
/*%
* These assume a single context is in operation per thread.
* If this is not the case we will need to call irs directly
* rather than through the base functions.
*/
PASS_R_RETURN
getpwent_r(struct passwd *pwptr, PASS_R_ARGS) {
struct passwd *pw = getpwent();
int res = 0;
if (pw == NULL)
return (PASS_R_BAD);
res = copy_passwd(pw, pwptr, buf, buflen);
return (res ? PASS_R_BAD : PASS_R_OK);
}
PASS_R_SET_RETURN
#ifdef PASS_R_ENT_ARGS
setpassent_r(int stayopen, PASS_R_ENT_ARGS)
#else
setpassent_r(int stayopen)
#endif
{
setpassent(stayopen);
#ifdef PASS_R_SET_RESULT
return (PASS_R_SET_RESULT);
#endif
}
PASS_R_SET_RETURN
#ifdef PASS_R_ENT_ARGS
setpwent_r(PASS_R_ENT_ARGS)
#else
setpwent_r(void)
#endif
{
setpwent();
#ifdef PASS_R_SET_RESULT
return (PASS_R_SET_RESULT);
#endif
}
PASS_R_END_RETURN
#ifdef PASS_R_ENT_ARGS
endpwent_r(PASS_R_ENT_ARGS)
#else
endpwent_r(void)
#endif
{
endpwent();
PASS_R_END_RESULT(PASS_R_OK);
}
#ifdef HAS_FGETPWENT
PASS_R_RETURN
fgetpwent_r(FILE *f, struct passwd *pwptr, PASS_R_COPY_ARGS) {
struct passwd *pw = fgetpwent(f);
int res = 0;
if (pw == NULL)
return (PASS_R_BAD);
res = copy_passwd(pw, pwptr, PASS_R_COPY);
return (res ? PASS_R_BAD : PASS_R_OK );
}
#endif
/* Private */
static int
copy_passwd(struct passwd *pw, struct passwd *pwptr, char *buf, int buflen) {
char *cp;
int n;
int len;
/* Find out the amount of space required to store the answer. */
len = strlen(pw->pw_name) + 1;
len += strlen(pw->pw_passwd) + 1;
#ifdef HAVE_PW_CLASS
len += strlen(pw->pw_class) + 1;
#endif
len += strlen(pw->pw_gecos) + 1;
len += strlen(pw->pw_dir) + 1;
len += strlen(pw->pw_shell) + 1;
if (len > buflen) {
errno = ERANGE;
return (ERANGE);
}
/* copy fixed atomic values*/
pwptr->pw_uid = pw->pw_uid;
pwptr->pw_gid = pw->pw_gid;
#ifdef HAVE_PW_CHANGE
pwptr->pw_change = pw->pw_change;
#endif
#ifdef HAVE_PW_EXPIRE
pwptr->pw_expire = pw->pw_expire;
#endif
cp = buf;
/* copy official name */
n = strlen(pw->pw_name) + 1;
strcpy(cp, pw->pw_name);
pwptr->pw_name = cp;
cp += n;
/* copy password */
n = strlen(pw->pw_passwd) + 1;
strcpy(cp, pw->pw_passwd);
pwptr->pw_passwd = cp;
cp += n;
#ifdef HAVE_PW_CLASS
/* copy class */
n = strlen(pw->pw_class) + 1;
strcpy(cp, pw->pw_class);
pwptr->pw_class = cp;
cp += n;
#endif
/* copy gecos */
n = strlen(pw->pw_gecos) + 1;
strcpy(cp, pw->pw_gecos);
pwptr->pw_gecos = cp;
cp += n;
/* copy directory */
n = strlen(pw->pw_dir) + 1;
strcpy(cp, pw->pw_dir);
pwptr->pw_dir = cp;
cp += n;
/* copy login shell */
n = strlen(pw->pw_shell) + 1;
strcpy(cp, pw->pw_shell);
pwptr->pw_shell = cp;
cp += n;
return (0);
}
#else /* PASS_R_RETURN */
static int getpwent_r_unknown_system = 0;
#endif /* PASS_R_RETURN */
#endif /* !def(_REENTRANT) || !def(DO_PTHREADS) || !def(WANT_IRS_PW) */
/*! \file */
|