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