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