xref: /illumos-gate/usr/src/lib/libnsl/nss/getauthattr.c (revision fc910014e8a32a65612105835a10995f2c13d942)
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 <stdlib.h>
29 #include <sys/types.h>
30 #include <nss_dbdefs.h>
31 #include <string.h>
32 #include <auth_attr.h>
33 
34 
35 /* externs from parse.c */
36 extern char *_strtok_escape(char *, char *, char **);
37 
38 static int authattr_stayopen = 0;
39 /*
40  * Unsynchronized, but it affects only
41  * efficiency, not correctness
42  */
43 
44 static DEFINE_NSS_DB_ROOT(db_root);
45 static DEFINE_NSS_GETENT(context);
46 
47 void
48 _nss_initf_authattr(nss_db_params_t *p)
49 {
50 	p->name = NSS_DBNAM_AUTHATTR;
51 	p->default_config = NSS_DEFCONF_AUTHATTR;
52 }
53 
54 
55 /*
56  * Return values: 0 = success, 1 = parse error, 2 = erange ...
57  * The structure pointer passed in is a structure in the caller's space
58  * wherein the field pointers would be set to areas in the buffer if
59  * need be. instring and buffer should be separate areas.
60  */
61 int
62 str2authattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
63 {
64 	char		*last = NULL;
65 	char		*sep = KV_TOKEN_DELIMIT;
66 	authstr_t	*auth = (authstr_t *)ent;
67 
68 	if (lenstr >= buflen)
69 		return (NSS_STR_PARSE_ERANGE);
70 
71 	if (instr != buffer)
72 		(void) strncpy(buffer, instr, buflen);
73 
74 	/*
75 	 * Remove newline that nis (yp_match) puts at the
76 	 * end of the entry it retrieves from the map.
77 	 */
78 	if (buffer[lenstr] == '\n')
79 		buffer[lenstr] = '\0';
80 
81 	/* quick exit do not entry fill if not needed */
82 	if (ent == (void *)NULL)
83 		return (NSS_STR_PARSE_SUCCESS);
84 
85 	auth->name = _strtok_escape(buffer, sep, &last);
86 	auth->res1 = _strtok_escape(NULL, sep, &last);
87 	auth->res2 = _strtok_escape(NULL, sep, &last);
88 	auth->short_desc = _strtok_escape(NULL, sep, &last);
89 	auth->long_desc = _strtok_escape(NULL, sep, &last);
90 	auth->attr = _strtok_escape(NULL, sep, &last);
91 
92 	return (0);
93 }
94 
95 
96 void
97 _setauthattr(void)
98 {
99 	authattr_stayopen = 0;
100 	nss_setent(&db_root, _nss_initf_authattr, &context);
101 }
102 
103 
104 void
105 _endauthattr(void)
106 {
107 	authattr_stayopen = 0;
108 	nss_endent(&db_root, _nss_initf_authattr, &context);
109 	nss_delete(&db_root);
110 }
111 
112 
113 authstr_t *
114 _getauthattr(authstr_t *result, char *buffer, int buflen, int *h_errnop)
115 {
116 	nss_XbyY_args_t arg;
117 	nss_status_t res;
118 
119 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
120 	res = nss_getent(&db_root, _nss_initf_authattr, &context, &arg);
121 	arg.status = res;
122 	*h_errnop = arg.h_errno;
123 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
124 }
125 
126 
127 authstr_t *
128 _getauthnam(const char *name, authstr_t *result, char *buffer, int buflen,
129     int *errnop)
130 {
131 	nss_XbyY_args_t arg;
132 	nss_status_t    res;
133 
134 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2authattr);
135 	arg.key.name = name;
136 	arg.stayopen = authattr_stayopen;
137 	res = nss_search(&db_root, _nss_initf_authattr,
138 	    NSS_DBOP_AUTHATTR_BYNAME, &arg);
139 	arg.status = res;
140 	*errnop = arg.h_errno;
141 	return ((authstr_t *)NSS_XbyY_FINI(&arg));
142 }
143