xref: /titanic_41/usr/src/lib/libnsl/nss/getuserattr.c (revision d58fda4376e4bf67072ce2e69f6f47036f9dbb68)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
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 ((instr >= buffer && (buffer + buflen) > instr) ||
78 	    (buffer >= instr && (instr + lenstr) > buffer))
79 		return (NSS_STR_PARSE_PARSE);
80 	if (lenstr >= buflen)
81 		return (NSS_STR_PARSE_ERANGE);
82 	(void) strncpy(buffer, instr, buflen);
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 	user->name = _strtok_escape(buffer, sep, &last);
92 	user->qualifier = _strtok_escape(NULL, sep, &last);
93 	user->res1 = _strtok_escape(NULL, sep, &last);
94 	user->res2 = _strtok_escape(NULL, sep, &last);
95 	user->attr = _strtok_escape(NULL, sep, &last);
96 
97 	return (0);
98 }
99 
100 
101 void
102 _setuserattr(void)
103 {
104 	userattr_stayopen = 0;
105 	nss_setent(&db_root, _nss_initf_userattr, &context);
106 }
107 
108 
109 void
110 _enduserattr(void)
111 {
112 	userattr_stayopen = 0;
113 	nss_endent(&db_root, _nss_initf_userattr, &context);
114 	nss_delete(&db_root);
115 }
116 
117 
118 userstr_t *
119 _getuserattr(userstr_t *result, char *buffer, int buflen, int *h_errnop)
120 {
121 	nss_XbyY_args_t arg;
122 	nss_status_t    res;
123 
124 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
125 	res = nss_getent(&db_root, _nss_initf_userattr, &context, &arg);
126 	arg.status = res;
127 	*h_errnop = arg.h_errno;
128 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
129 }
130 
131 
132 userstr_t *
133 _fgetuserattr(FILE *f, userstr_t *result, char *buffer, int buflen)
134 {
135 	nss_XbyY_args_t arg;
136 
137 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
138 	_nss_XbyY_fgets(f, &arg);
139 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
140 }
141 
142 
143 
144 userstr_t *
145 _getusernam(const char *name, userstr_t *result, char *buffer, int buflen,
146     int *errnop)
147 {
148 	nss_XbyY_args_t arg;
149 	nss_status_t    res;
150 
151 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
152 	arg.key.name = name;
153 	arg.stayopen = userattr_stayopen;
154 	res = nss_search(&db_root, _nss_initf_userattr,
155 	    NSS_DBOP_USERATTR_BYNAME, &arg);
156 	arg.status = res;
157 	*errnop = arg.h_errno;
158 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
159 }
160