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-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <nss_dbdefs.h>
32 #include <rpc/trace.h>
33 #include <string.h>
34 #include <auth_attr.h>
35 
36 
37 /* externs from parse.c */
38 extern char *_strtok_escape(char *, char *, char **);
39 
40 static int authattr_stayopen = 0;
41 /*
42  * Unsynchronized, but it affects only
43  * efficiency, not correctness
44  */
45 
46 static DEFINE_NSS_DB_ROOT(db_root);
47 static DEFINE_NSS_GETENT(context);
48 
49 void
50 _nss_initf_authattr(nss_db_params_t *p)
51 {
52 	trace1(TR__nss_initf_authattr, 0);
53 	p->name = NSS_DBNAM_AUTHATTR;
54 	p->default_config = NSS_DEFCONF_AUTHATTR;
55 	trace1(TR__nss_initf_authattr, 1);
56 }
57 
58 
59 /*
60  * Return values: 0 = success, 1 = parse error, 2 = erange ...
61  * The structure pointer passed in is a structure in the caller's space
62  * wherein the field pointers would be set to areas in the buffer if
63  * need be. instring and buffer should be separate areas.
64  */
65 int
66 str2authattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
67 {
68 	char		*str = (char *)NULL;
69 	char		*last = (char *)NULL;
70 	char		*sep = KV_TOKEN_DELIMIT;
71 	authstr_t	*auth = (authstr_t *)ent;
72 
73 	trace3(TR_str2authattr, 0, lenstr, buflen);
74 	if ((instr >= buffer && (buffer + buflen) > instr) ||
75 	    (buffer >= instr && (instr + lenstr) > buffer)) {
76 		trace3(TR_str2authattr, 1, lenstr, buflen);
77 		return (NSS_STR_PARSE_PARSE);
78 	}
79 	if (lenstr >= buflen) {
80 		trace3(TR_str2authattr, 1, lenstr, buflen);
81 		return (NSS_STR_PARSE_ERANGE);
82 	}
83 	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 	auth->name = _strtok_escape(buffer, sep, &last);
93 	auth->res1 = _strtok_escape(NULL, sep, &last);
94 	auth->res2 = _strtok_escape(NULL, sep, &last);
95 	auth->short_desc = _strtok_escape(NULL, sep, &last);
96 	auth->long_desc = _strtok_escape(NULL, sep, &last);
97 	auth->attr = _strtok_escape(NULL, sep, &last);
98 
99 	return (0);
100 }
101 
102 
103 void
104 _setauthattr(void)
105 {
106 	trace1(TR_setauthattr, 0);
107 	authattr_stayopen = 0;
108 	nss_setent(&db_root, _nss_initf_authattr, &context);
109 	trace1(TR_setauthattr, 0);
110 }
111 
112 
113 void
114 _endauthattr(void)
115 {
116 	trace1(TR_endauthattr, 0);
117 	authattr_stayopen = 0;
118 	nss_endent(&db_root, _nss_initf_authattr, &context);
119 	nss_delete(&db_root);
120 	trace1(TR_endauthattr, 0);
121 }
122 
123 
124 authstr_t *
125 _getauthattr(authstr_t *result, char *buffer, int buflen, int *h_errnop)
126 {
127 	nss_XbyY_args_t arg;
128 	nss_status_t res;
129 
130 	trace2(TR_getauthattr, 0, buflen);
131 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
132 	res = nss_getent(&db_root, _nss_initf_authattr, &context, &arg);
133 	arg.status = res;
134 	*h_errnop = arg.h_errno;
135 	trace2(TR_getauthattr, 1, buflen);
136 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
137 }
138 
139 
140 authstr_t *
141 _getauthnam(const char *name, authstr_t *result, char *buffer, int buflen,
142     int *errnop)
143 {
144 	nss_XbyY_args_t arg;
145 	nss_status_t    res;
146 
147 	trace2(TR_getauthnam, 0, buflen);
148 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
149 	arg.key.name = name;
150 	arg.stayopen = authattr_stayopen;
151 	res = nss_search(&db_root, _nss_initf_authattr,
152 	    NSS_DBOP_AUTHATTR_BYNAME, &arg);
153 	arg.status = res;
154 	*errnop = arg.h_errno;
155 	trace2(TR_getauthnam, 1, buflen);
156 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
157 }
158