xref: /titanic_51/usr/src/lib/libnsl/nss/getuserattr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits.h>
32 #include <sys/types.h>
33 #include <nss_dbdefs.h>
34 #include <rpc/trace.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 	trace1(TR__nss_initf_userattr, 0);
59 	p->name = NSS_DBNAM_USERATTR;
60 	p->config_name    = NSS_DBNAM_PASSWD; /* use config for "passwd" */
61 	p->default_config = NSS_DEFCONF_USERATTR;
62 	trace1(TR__nss_initf_userattr, 1);
63 }
64 
65 
66 /*
67  * Return values: 0 = success, 1 = parse error, 2 = erange ...
68  * The structure pointer passed in is a structure in the caller's space
69  * wherein the field pointers would be set to areas in the buffer if
70  * need be. instring and buffer should be separate areas.
71  */
72 int
73 str2userattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
74 {
75 	char		*str = (char *)NULL;
76 	char		*last = (char *)NULL;
77 	char		*sep = KV_TOKEN_DELIMIT;
78 	userstr_t	*user = (userstr_t *)ent;
79 
80 	trace3(TR_str2userattr, 0, lenstr, buflen);
81 	if ((instr >= buffer && (buffer + buflen) > instr) ||
82 	    (buffer >= instr && (instr + lenstr) > buffer)) {
83 		trace3(TR_str2userattr, 1, lenstr, buflen);
84 		return (NSS_STR_PARSE_PARSE);
85 	}
86 	if (lenstr >= buflen) {
87 		trace3(TR_str2userattr, 1, lenstr, buflen);
88 		return (NSS_STR_PARSE_ERANGE);
89 	}
90 	strncpy(buffer, instr, buflen);
91 	/*
92 	 * Remove newline that nis (yp_match) puts at the
93 	 * end of the entry it retrieves from the map.
94 	 */
95 	if (buffer[lenstr] == '\n') {
96 		buffer[lenstr] = '\0';
97 	}
98 
99 	user->name = _strtok_escape(buffer, sep, &last);
100 	user->qualifier = _strtok_escape(NULL, sep, &last);
101 	user->res1 = _strtok_escape(NULL, sep, &last);
102 	user->res2 = _strtok_escape(NULL, sep, &last);
103 	user->attr = _strtok_escape(NULL, sep, &last);
104 
105 	return (0);
106 }
107 
108 
109 void
110 _setuserattr(void)
111 {
112 	trace1(TR_setuserattr, 0);
113 	userattr_stayopen = 0;
114 	nss_setent(&db_root, _nss_initf_userattr, &context);
115 	trace1(TR_setuserattr, 0);
116 }
117 
118 
119 void
120 _enduserattr(void)
121 {
122 	trace1(TR_enduserattr, 0);
123 	userattr_stayopen = 0;
124 	nss_endent(&db_root, _nss_initf_userattr, &context);
125 	nss_delete(&db_root);
126 	trace1(TR_enduserattr, 0);
127 }
128 
129 
130 userstr_t *
131 _getuserattr(userstr_t *result, char *buffer, int buflen, int *h_errnop)
132 {
133 	nss_XbyY_args_t arg;
134 	nss_status_t    res;
135 
136 	trace2(TR_getuserattr, 0, buflen);
137 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
138 	res = nss_getent(&db_root, _nss_initf_userattr, &context, &arg);
139 	arg.status = res;
140 	*h_errnop = arg.h_errno;
141 	trace2(TR_getuserattr, 1, buflen);
142 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
143 }
144 
145 
146 userstr_t *
147 _fgetuserattr(FILE *f, userstr_t *result, char *buffer, int buflen)
148 {
149 	nss_XbyY_args_t arg;
150 
151 	trace2(TR_getuserattr, 0, buflen);
152 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
153 	_nss_XbyY_fgets(f, &arg);
154 	trace2(TR_getuserattr, 1, buflen);
155 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
156 }
157 
158 
159 
160 userstr_t *
161 _getusernam(const char *name, userstr_t *result, char *buffer, int buflen,
162     int *errnop)
163 {
164 	nss_XbyY_args_t arg;
165 	nss_status_t    res;
166 
167 	trace2(TR_getusernam, 0, buflen);
168 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
169 	arg.key.name = name;
170 	arg.stayopen = userattr_stayopen;
171 	res = nss_search(&db_root, _nss_initf_userattr,
172 	    NSS_DBOP_USERATTR_BYNAME, &arg);
173 	arg.status = res;
174 	*errnop = arg.h_errno;
175 	trace2(TR_getusernam, 1, buflen);
176 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
177 }
178