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