xref: /illumos-gate/usr/src/lib/libc/port/gen/getpwnam.c (revision 1da57d55)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	All Rights Reserved  	*/
29 
30 #pragma weak _getpwnam = getpwnam
31 #pragma weak _getpwuid = getpwuid
32 
33 #include "lint.h"
34 #include <sys/types.h>
35 #include <pwd.h>
36 #include <nss_dbdefs.h>
37 #include <stdio.h>
38 #include "tsd.h"
39 
40 #ifdef	NSS_INCLUDE_UNSAFE
41 
42 /*
43  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
44  */
45 
46 static void
free_pwbuf(void * arg)47 free_pwbuf(void *arg)
48 {
49 	nss_XbyY_buf_t **buffer = arg;
50 
51 	NSS_XbyY_FREE(buffer);
52 }
53 
54 static nss_XbyY_buf_t *
get_pwbuf()55 get_pwbuf()
56 {
57 	nss_XbyY_buf_t **buffer =
58 	    tsdalloc(_T_PWBUF, sizeof (nss_XbyY_buf_t *), free_pwbuf);
59 	nss_XbyY_buf_t *b;
60 
61 	if (buffer == NULL)
62 		return (NULL);
63 	b = NSS_XbyY_ALLOC(buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD);
64 	return (b);
65 }
66 
67 struct passwd *
getpwuid(uid_t uid)68 getpwuid(uid_t uid)
69 {
70 	nss_XbyY_buf_t *b = get_pwbuf();
71 
72 	return (b == NULL ? NULL :
73 	    getpwuid_r(uid, b->result, b->buffer, b->buflen));
74 }
75 
76 struct passwd *
getpwnam(const char * nam)77 getpwnam(const char *nam)
78 {
79 	nss_XbyY_buf_t *b = get_pwbuf();
80 
81 	return (b == NULL ? NULL :
82 	    getpwnam_r(nam, b->result, b->buffer, b->buflen));
83 }
84 
85 struct passwd *
getpwent(void)86 getpwent(void)
87 {
88 	nss_XbyY_buf_t *b = get_pwbuf();
89 
90 	return (b == NULL ? NULL :
91 	    getpwent_r(b->result, b->buffer, b->buflen));
92 }
93 
94 struct passwd *
fgetpwent(FILE * f)95 fgetpwent(FILE *f)
96 {
97 	nss_XbyY_buf_t *b = get_pwbuf();
98 
99 	return (b == NULL ? NULL :
100 	    fgetpwent_r(f, b->result, b->buffer, b->buflen));
101 }
102 
103 #endif	/* NSS_INCLUDE_UNSAFE */
104