xref: /illumos-gate/usr/src/lib/libnsl/nss/getuserattr.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "mt.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #include <nss_dbdefs.h>
35 #include <string.h>
36 #include <user_attr.h>
37 
38 /* externs from libc */
39 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
40 /* externs from parse.c */
41 extern char *_strtok_escape(char *, char *, char **);
42 
43 
44 static int userattr_stayopen;
45 
46 /*
47  * Unsynchronized, but it affects only
48  * efficiency, not correctness
49  */
50 
51 static DEFINE_NSS_DB_ROOT(db_root);
52 static DEFINE_NSS_GETENT(context);
53 
54 
55 void
56 _nss_initf_userattr(nss_db_params_t *p)
57 {
58 	p->name = NSS_DBNAM_USERATTR;
59 	p->config_name    = NSS_DBNAM_PASSWD; /* use config for "passwd" */
60 	p->default_config = NSS_DEFCONF_USERATTR;
61 }
62 
63 
64 /*
65  * Return values: 0 = success, 1 = parse error, 2 = erange ...
66  * The structure pointer passed in is a structure in the caller's space
67  * wherein the field pointers would be set to areas in the buffer if
68  * need be. instring and buffer should be separate areas.
69  */
70 int
71 str2userattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
72 {
73 	char		*last = NULL;
74 	char		*sep = KV_TOKEN_DELIMIT;
75 	userstr_t	*user = (userstr_t *)ent;
76 
77 	if (lenstr >= buflen)
78 		return (NSS_STR_PARSE_ERANGE);
79 
80 	if (instr != buffer)
81 		(void) strncpy(buffer, instr, buflen);
82 
83 	/*
84 	 * Remove newline that nis (yp_match) puts at the
85 	 * end of the entry it retrieves from the map.
86 	 */
87 	if (buffer[lenstr] == '\n') {
88 		buffer[lenstr] = '\0';
89 	}
90 
91 	/* quick exit do not entry fill if not needed */
92 	if (ent == (void *)NULL)
93 		return (NSS_STR_PARSE_SUCCESS);
94 
95 	user->name = _strtok_escape(buffer, sep, &last);
96 	user->qualifier = _strtok_escape(NULL, sep, &last);
97 	user->res1 = _strtok_escape(NULL, sep, &last);
98 	user->res2 = _strtok_escape(NULL, sep, &last);
99 	user->attr = _strtok_escape(NULL, sep, &last);
100 
101 	return (0);
102 }
103 
104 
105 void
106 _setuserattr(void)
107 {
108 	userattr_stayopen = 0;
109 	nss_setent(&db_root, _nss_initf_userattr, &context);
110 }
111 
112 
113 void
114 _enduserattr(void)
115 {
116 	userattr_stayopen = 0;
117 	nss_endent(&db_root, _nss_initf_userattr, &context);
118 	nss_delete(&db_root);
119 }
120 
121 
122 userstr_t *
123 _getuserattr(userstr_t *result, char *buffer, int buflen, int *h_errnop)
124 {
125 	nss_XbyY_args_t arg;
126 	nss_status_t    res;
127 
128 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
129 	res = nss_getent(&db_root, _nss_initf_userattr, &context, &arg);
130 	arg.status = res;
131 	*h_errnop = arg.h_errno;
132 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
133 }
134 
135 
136 userstr_t *
137 _fgetuserattr(FILE *f, userstr_t *result, char *buffer, int buflen)
138 {
139 	nss_XbyY_args_t arg;
140 
141 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
142 	_nss_XbyY_fgets(f, &arg);
143 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
144 }
145 
146 
147 
148 userstr_t *
149 _getusernam(const char *name, userstr_t *result, char *buffer, int buflen,
150     int *errnop)
151 {
152 	nss_XbyY_args_t arg;
153 	nss_status_t    res;
154 
155 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
156 	arg.key.name = name;
157 	arg.stayopen = userattr_stayopen;
158 	res = nss_search(&db_root, _nss_initf_userattr,
159 	    NSS_DBOP_USERATTR_BYNAME, &arg);
160 	arg.status = res;
161 	*errnop = arg.h_errno;
162 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
163 }
164