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 <stdlib.h>
31 #include <sys/types.h>
32 #include <nss_dbdefs.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 	p->name = NSS_DBNAM_AUTHATTR;
53 	p->default_config = NSS_DEFCONF_AUTHATTR;
54 }
55 
56 
57 /*
58  * Return values: 0 = success, 1 = parse error, 2 = erange ...
59  * The structure pointer passed in is a structure in the caller's space
60  * wherein the field pointers would be set to areas in the buffer if
61  * need be. instring and buffer should be separate areas.
62  */
63 int
64 str2authattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
65 {
66 	char		*last = NULL;
67 	char		*sep = KV_TOKEN_DELIMIT;
68 	authstr_t	*auth = (authstr_t *)ent;
69 
70 	if ((instr >= buffer && (buffer + buflen) > instr) ||
71 	    (buffer >= instr && (instr + lenstr) > buffer))
72 		return (NSS_STR_PARSE_PARSE);
73 	if (lenstr >= buflen)
74 		return (NSS_STR_PARSE_ERANGE);
75 	(void) strncpy(buffer, instr, buflen);
76 	/*
77 	 * Remove newline that nis (yp_match) puts at the
78 	 * end of the entry it retrieves from the map.
79 	 */
80 	if (buffer[lenstr] == '\n')
81 		buffer[lenstr] = '\0';
82 
83 	auth->name = _strtok_escape(buffer, sep, &last);
84 	auth->res1 = _strtok_escape(NULL, sep, &last);
85 	auth->res2 = _strtok_escape(NULL, sep, &last);
86 	auth->short_desc = _strtok_escape(NULL, sep, &last);
87 	auth->long_desc = _strtok_escape(NULL, sep, &last);
88 	auth->attr = _strtok_escape(NULL, sep, &last);
89 
90 	return (0);
91 }
92 
93 
94 void
95 _setauthattr(void)
96 {
97 	authattr_stayopen = 0;
98 	nss_setent(&db_root, _nss_initf_authattr, &context);
99 }
100 
101 
102 void
103 _endauthattr(void)
104 {
105 	authattr_stayopen = 0;
106 	nss_endent(&db_root, _nss_initf_authattr, &context);
107 	nss_delete(&db_root);
108 }
109 
110 
111 authstr_t *
112 _getauthattr(authstr_t *result, char *buffer, int buflen, int *h_errnop)
113 {
114 	nss_XbyY_args_t arg;
115 	nss_status_t res;
116 
117 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
118 	res = nss_getent(&db_root, _nss_initf_authattr, &context, &arg);
119 	arg.status = res;
120 	*h_errnop = arg.h_errno;
121 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
122 }
123 
124 
125 authstr_t *
126 _getauthnam(const char *name, authstr_t *result, char *buffer, int buflen,
127     int *errnop)
128 {
129 	nss_XbyY_args_t arg;
130 	nss_status_t    res;
131 
132 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
133 	arg.key.name = name;
134 	arg.stayopen = authattr_stayopen;
135 	res = nss_search(&db_root, _nss_initf_authattr,
136 	    NSS_DBOP_AUTHATTR_BYNAME, &arg);
137 	arg.status = res;
138 	*errnop = arg.h_errno;
139 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
140 }
141