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]
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.
29 #include <sys/types.h>
31 #include <nss_dbdefs.h>
34 #include <sys/param.h>
40 int str2passwd(const char *, int, void *,
43 static DEFINE_NSS_DB_ROOT(db_root
);
44 static DEFINE_NSS_GETENT(context
);
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>
56 _uncached_getpwuid_r(uid_t uid
, struct passwd
*result
, char *buffer
,
60 _uncached_getpwnam_r(const char *name
, struct passwd
*result
, char *buffer
,
64 * POSIX.1c Draft-6 version of the function getpwnam_r.
65 * It was implemented by Solaris 2.3.
68 getpwnam_r(const char *name
, struct passwd
*result
, char *buffer
, int buflen
)
72 if (name
== (const char *)NULL
) {
76 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2passwd
);
78 (void) nss_search(&db_root
, _nss_initf_passwd
, NSS_DBOP_PASSWD_BYNAME
,
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.
88 getpwuid_r(uid_t uid
, struct passwd
*result
, char *buffer
, int buflen
)
92 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2passwd
);
94 (void) nss_search(&db_root
, _nss_initf_passwd
, NSS_DBOP_PASSWD_BYUID
,
96 return ((struct passwd
*)NSS_XbyY_FINI(&arg
));
101 _uncached_getpwuid_r(uid_t uid
, struct passwd
*result
, char *buffer
,
106 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2passwd
);
108 (void) nss_search(&db_root
, _nss_initf_passwd
, NSS_DBOP_PASSWD_BYUID
,
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
)
126 if ((*result
= getpwuid_r(uid
, pwd
, buffer
, (uintptr_t)bufsize
))
135 _uncached_getpwnam_r(const char *name
, struct passwd
*result
, char *buffer
,
140 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2passwd
);
142 (void) nss_search(&db_root
, _nss_initf_passwd
, NSS_DBOP_PASSWD_BYNAME
,
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
)
159 if ((*result
= getpwnam_r(name
, pwd
, buffer
, (uintptr_t)bufsize
))
170 nss_setent(&db_root
, _nss_initf_passwd
, &context
);
176 nss_endent(&db_root
, _nss_initf_passwd
, &context
);
177 nss_delete(&db_root
);
181 getpwent_r(struct passwd
*result
, char *buffer
, int buflen
)
186 /* In getXXent_r(), protect the unsuspecting caller from +/- entries */
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
));
200 fgetpwent_r(FILE *f
, struct passwd
*result
, char *buffer
, int buflen
)
202 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t
*);
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
));
214 gettok(char **nextpp
)
223 while ((c
= *q
) != '\0' && c
!= ':')
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
;
246 int black_magic
; /* "+" or "-" entry */
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
);
268 passwd
->pw_name
= p
= gettok(&next
); /* username */
270 /* Empty username; not allowed */
271 return (NSS_STR_PARSE_PARSE
);
273 black_magic
= (*p
== '+' || *p
== '-');
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
= "";
283 * the rest of the passwd entry is "optional"
285 passwd
->pw_comment
= "";
286 passwd
->pw_gecos
= "";
288 passwd
->pw_shell
= "";
291 passwd
->pw_passwd
= p
= gettok(&next
); /* password */
294 return (NSS_STR_PARSE_SUCCESS
);
296 return (NSS_STR_PARSE_PARSE
);
298 for (; *p
!= '\0'; p
++) { /* age */
307 if (p
== 0 || *p
== '\0') {
309 return (NSS_STR_PARSE_SUCCESS
);
311 return (NSS_STR_PARSE_PARSE
);
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.
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
;
340 passwd
->pw_uid
= (uid_t
)tmp
;
342 if (*next
++ != ':') {
344 (void) gettok(&next
);
346 return (NSS_STR_PARSE_PARSE
);
349 if (p
== 0 || *p
== '\0') {
351 return (NSS_STR_PARSE_SUCCESS
);
353 return (NSS_STR_PARSE_PARSE
);
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
;
370 passwd
->pw_gid
= (gid_t
)tmp
;
372 if (*next
++ != ':') {
374 (void) gettok(&next
);
376 return (NSS_STR_PARSE_PARSE
);
379 passwd
->pw_gecos
= passwd
->pw_comment
= p
= gettok(&next
);
382 return (NSS_STR_PARSE_SUCCESS
);
384 return (NSS_STR_PARSE_PARSE
);
387 passwd
->pw_dir
= p
= gettok(&next
);
390 return (NSS_STR_PARSE_SUCCESS
);
392 return (NSS_STR_PARSE_PARSE
);
395 passwd
->pw_shell
= p
= gettok(&next
);
398 return (NSS_STR_PARSE_SUCCESS
);
400 return (NSS_STR_PARSE_PARSE
);
403 /* Better not be any more fields... */
405 /* Successfully parsed and stored */
406 return (NSS_STR_PARSE_SUCCESS
);
408 return (NSS_STR_PARSE_PARSE
);