8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / getpwnam_r.c
bloba1dc20cb36748e9fe10912f17f6ae445b1c7d52d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
28 #include "lint.h"
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <nss_dbdefs.h>
32 #include <stdio.h>
33 #include <synch.h>
34 #include <sys/param.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/mman.h>
38 #include <errno.h>
40 int str2passwd(const char *, int, void *,
41 char *, int);
43 static DEFINE_NSS_DB_ROOT(db_root);
44 static DEFINE_NSS_GETENT(context);
46 void
47 _nss_initf_passwd(nss_db_params_t *p)
49 p->name = NSS_DBNAM_PASSWD;
50 p->default_config = NSS_DEFCONF_PASSWD;
53 #include <getxby_door.h>
55 struct passwd *
56 _uncached_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
57 int buflen);
59 struct passwd *
60 _uncached_getpwnam_r(const char *name, struct passwd *result, char *buffer,
61 int buflen);
64 * POSIX.1c Draft-6 version of the function getpwnam_r.
65 * It was implemented by Solaris 2.3.
67 struct passwd *
68 getpwnam_r(const char *name, struct passwd *result, char *buffer, int buflen)
70 nss_XbyY_args_t arg;
72 if (name == (const char *)NULL) {
73 errno = ERANGE;
74 return (NULL);
76 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
77 arg.key.name = name;
78 (void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYNAME,
79 &arg);
80 return ((struct passwd *)NSS_XbyY_FINI(&arg));
84 * POSIX.1c Draft-6 version of the function getpwuid_r.
85 * It was implemented by Solaris 2.3.
87 struct passwd *
88 getpwuid_r(uid_t uid, struct passwd *result, char *buffer, int buflen)
90 nss_XbyY_args_t arg;
92 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
93 arg.key.uid = uid;
94 (void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYUID,
95 &arg);
96 return ((struct passwd *)NSS_XbyY_FINI(&arg));
100 struct passwd *
101 _uncached_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
102 int buflen)
104 nss_XbyY_args_t arg;
106 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
107 arg.key.uid = uid;
108 (void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYUID,
109 &arg);
110 return ((struct passwd *)NSS_XbyY_FINI(&arg));
115 * POSIX.1c standard version of the function getpwuid_r.
116 * User gets it via static getpwuid_r from the header file.
119 __posix_getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
120 size_t bufsize, struct passwd **result)
122 int nerrno = 0;
123 int oerrno = errno;
125 errno = 0;
126 if ((*result = getpwuid_r(uid, pwd, buffer, (uintptr_t)bufsize))
127 == NULL) {
128 nerrno = errno;
130 errno = oerrno;
131 return (nerrno);
134 struct passwd *
135 _uncached_getpwnam_r(const char *name, struct passwd *result, char *buffer,
136 int buflen)
138 nss_XbyY_args_t arg;
140 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
141 arg.key.name = name;
142 (void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYNAME,
143 &arg);
144 return ((struct passwd *)NSS_XbyY_FINI(&arg));
148 * POSIX.1c standard version of the function getpwnam_r.
149 * User gets it via static getpwnam_r from the header file.
152 __posix_getpwnam_r(const char *name, struct passwd *pwd, char *buffer,
153 size_t bufsize, struct passwd **result)
155 int nerrno = 0;
156 int oerrno = errno;
158 errno = 0;
159 if ((*result = getpwnam_r(name, pwd, buffer, (uintptr_t)bufsize))
160 == NULL) {
161 nerrno = errno;
163 errno = oerrno;
164 return (nerrno);
167 void
168 setpwent(void)
170 nss_setent(&db_root, _nss_initf_passwd, &context);
173 void
174 endpwent(void)
176 nss_endent(&db_root, _nss_initf_passwd, &context);
177 nss_delete(&db_root);
180 struct passwd *
181 getpwent_r(struct passwd *result, char *buffer, int buflen)
183 nss_XbyY_args_t arg;
184 char *nam;
186 /* In getXXent_r(), protect the unsuspecting caller from +/- entries */
188 do {
189 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
190 /* No key to fill in */
191 (void) nss_getent(&db_root, _nss_initf_passwd, &context, &arg);
192 } while (arg.returnval != 0 &&
193 (nam = ((struct passwd *)arg.returnval)->pw_name) != 0 &&
194 (*nam == '+' || *nam == '-'));
196 return ((struct passwd *)NSS_XbyY_FINI(&arg));
199 struct passwd *
200 fgetpwent_r(FILE *f, struct passwd *result, char *buffer, int buflen)
202 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
203 nss_XbyY_args_t arg;
205 /* ... but in fgetXXent_r, the caller deserves any +/- entry it gets */
207 /* No key to fill in */
208 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
209 _nss_XbyY_fgets(f, &arg);
210 return ((struct passwd *)NSS_XbyY_FINI(&arg));
213 static char *
214 gettok(char **nextpp)
216 char *p = *nextpp;
217 char *q = p;
218 char c;
220 if (p == 0)
221 return (0);
223 while ((c = *q) != '\0' && c != ':')
224 q++;
226 if (c == '\0')
227 *nextpp = 0;
228 else {
229 *q++ = '\0';
230 *nextpp = q;
232 return (p);
236 * Return values: 0 = success, 1 = parse error, 2 = erange ...
237 * The structure pointer passed in is a structure in the caller's space
238 * wherein the field pointers would be set to areas in the buffer if
239 * need be. instring and buffer should be separate areas.
242 str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
244 struct passwd *passwd = (struct passwd *)ent;
245 char *p, *next;
246 int black_magic; /* "+" or "-" entry */
247 ulong_t tmp;
249 if (lenstr + 1 > buflen)
250 return (NSS_STR_PARSE_ERANGE);
253 * We copy the input string into the output buffer and
254 * operate on it in place.
256 if (instr != buffer) {
257 /* Overlapping buffer copies are OK */
258 (void) memmove(buffer, instr, lenstr);
259 buffer[lenstr] = '\0';
262 /* quick exit do not entry fill if not needed */
263 if (ent == (void *)NULL)
264 return (NSS_STR_PARSE_SUCCESS);
266 next = buffer;
268 passwd->pw_name = p = gettok(&next); /* username */
269 if (*p == '\0') {
270 /* Empty username; not allowed */
271 return (NSS_STR_PARSE_PARSE);
273 black_magic = (*p == '+' || *p == '-');
274 if (black_magic) {
275 passwd->pw_uid = UID_NOBODY;
276 passwd->pw_gid = GID_NOBODY;
278 * pwconv tests pw_passwd and pw_age == NULL
280 passwd->pw_passwd = "";
281 passwd->pw_age = "";
283 * the rest of the passwd entry is "optional"
285 passwd->pw_comment = "";
286 passwd->pw_gecos = "";
287 passwd->pw_dir = "";
288 passwd->pw_shell = "";
291 passwd->pw_passwd = p = gettok(&next); /* password */
292 if (p == 0) {
293 if (black_magic)
294 return (NSS_STR_PARSE_SUCCESS);
295 else
296 return (NSS_STR_PARSE_PARSE);
298 for (; *p != '\0'; p++) { /* age */
299 if (*p == ',') {
300 *p++ = '\0';
301 break;
304 passwd->pw_age = p;
306 p = next; /* uid */
307 if (p == 0 || *p == '\0') {
308 if (black_magic)
309 return (NSS_STR_PARSE_SUCCESS);
310 else
311 return (NSS_STR_PARSE_PARSE);
313 if (!black_magic) {
315 * strtoul returns unsigned long which is
316 * 8 bytes on a 64-bit system. We don't want
317 * to assign it directly to passwd->pw_uid
318 * which is 4 bytes or else we will end up
319 * truncating the value.
321 errno = 0;
322 tmp = strtoul(p, &next, 10);
323 if (next == p || errno != 0) {
324 /* uid field should be nonempty */
325 /* also check errno from strtoul */
326 return (NSS_STR_PARSE_PARSE);
329 * The old code (in 2.0 through 2.5) would check
330 * for the uid being negative, or being greater
331 * than 60001 (the rfs limit). If it met either of
332 * these conditions, the uid was translated to 60001.
334 * Now we just check for -1 (UINT32_MAX); anything else
335 * is administrative policy
337 if (tmp >= UINT32_MAX)
338 passwd->pw_uid = UID_NOBODY;
339 else
340 passwd->pw_uid = (uid_t)tmp;
342 if (*next++ != ':') {
343 if (black_magic)
344 (void) gettok(&next);
345 else
346 return (NSS_STR_PARSE_PARSE);
348 p = next; /* gid */
349 if (p == 0 || *p == '\0') {
350 if (black_magic)
351 return (NSS_STR_PARSE_SUCCESS);
352 else
353 return (NSS_STR_PARSE_PARSE);
355 if (!black_magic) {
356 errno = 0;
357 tmp = strtoul(p, &next, 10);
358 if (next == p || errno != 0) {
359 /* gid field should be nonempty */
360 /* also check errno from strtoul */
361 return (NSS_STR_PARSE_PARSE);
364 * gid should not be -1; anything else
365 * is administrative policy.
367 if (tmp >= UINT32_MAX)
368 passwd->pw_gid = GID_NOBODY;
369 else
370 passwd->pw_gid = (gid_t)tmp;
372 if (*next++ != ':') {
373 if (black_magic)
374 (void) gettok(&next);
375 else
376 return (NSS_STR_PARSE_PARSE);
379 passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
380 if (p == 0) {
381 if (black_magic)
382 return (NSS_STR_PARSE_SUCCESS);
383 else
384 return (NSS_STR_PARSE_PARSE);
387 passwd->pw_dir = p = gettok(&next);
388 if (p == 0) {
389 if (black_magic)
390 return (NSS_STR_PARSE_SUCCESS);
391 else
392 return (NSS_STR_PARSE_PARSE);
395 passwd->pw_shell = p = gettok(&next);
396 if (p == 0) {
397 if (black_magic)
398 return (NSS_STR_PARSE_SUCCESS);
399 else
400 return (NSS_STR_PARSE_PARSE);
403 /* Better not be any more fields... */
404 if (next == 0) {
405 /* Successfully parsed and stored */
406 return (NSS_STR_PARSE_SUCCESS);
408 return (NSS_STR_PARSE_PARSE);