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