xref: /illumos-gate/usr/src/lib/libnsl/nss/getauuser.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
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 <bsm/libbsm.h>
35 #include <secdb.h>
36 
37 
38 /* externs from parse.c */
39 extern char *_strtok_escape(char *, char *, char **);
40 
41 static int auuser_stayopen;
42 
43 /*
44  * Unsynchronized, but it affects only
45  * efficiency, not correctness
46  */
47 
48 static DEFINE_NSS_DB_ROOT(db_root);
49 static DEFINE_NSS_GETENT(context);
50 
51 
52 void
53 _nss_initf_auuser(nss_db_params_t *p)
54 {
55 	trace1(TR__nss_initf_auuser, 0);
56 	p->name	= NSS_DBNAM_AUDITUSER;
57 	p->config_name    = NSS_DBNAM_PASSWD;  /* use config for "passwd" */
58 	p->default_config = NSS_DEFCONF_AUDITUSER;
59 	trace1(TR__nss_initf_auuser, 1);
60 }
61 
62 
63 /*
64  * Return values: 0 = success, 1 = parse error, 2 = erange ...
65  * The structure pointer passed in is a structure in the caller's space
66  * wherein the field pointers would be set to areas in the buffer if
67  * need be. instring and buffer should be separate areas.
68  */
69 int
70 str2auuser(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
71 {
72 	char		*last = (char *)NULL;
73 	char		*sep = KV_TOKEN_DELIMIT;
74 	au_user_str_t	*au_user = (au_user_str_t *)ent;
75 
76 	trace3(TR_str2auuser, 0, lenstr, buflen);
77 	if ((instr >= buffer && (buffer + buflen) > instr) ||
78 	    (buffer >= instr && (instr + lenstr) > buffer)) {
79 		trace3(TR_str2auuser, 1, lenstr, buflen);
80 		return (NSS_STR_PARSE_PARSE);
81 	}
82 	if (lenstr >= buflen) {
83 		trace3(TR_str2auuser, 1, lenstr, buflen);
84 		return (NSS_STR_PARSE_ERANGE);
85 	}
86 	strncpy(buffer, instr, buflen);
87 	/*
88 	 * Remove newline that nis (yp_match) puts at the
89 	 * end of the entry it retrieves from the map.
90 	 */
91 	if (buffer[lenstr] == '\n') {
92 		buffer[lenstr] = '\0';
93 	}
94 
95 	au_user->au_name = _strtok_escape(buffer, sep, &last);
96 	au_user->au_always = _strtok_escape(NULL, sep, &last);
97 	au_user->au_never = _strtok_escape(NULL, sep, &last);
98 
99 	return (0);
100 }
101 
102 
103 void
104 _setauuser(void)
105 {
106 	trace1(TR_setauuser, 0);
107 	auuser_stayopen = 0;
108 	nss_setent(&db_root, _nss_initf_auuser, &context);
109 	trace1(TR_setauuser, 0);
110 }
111 
112 
113 int
114 _endauuser(void)
115 {
116 	trace1(TR_endauuser, 0);
117 	auuser_stayopen = 0;
118 	nss_endent(&db_root, _nss_initf_auuser, &context);
119 	nss_delete(&db_root);
120 	trace1(TR_endauuser, 0);
121 	return (0);
122 }
123 
124 
125 au_user_str_t *
126 _getauuserent(au_user_str_t *result, char *buffer, int buflen, int *h_errnop)
127 {
128 	nss_XbyY_args_t arg;
129 	nss_status_t    res;
130 
131 	trace2(TR_getauuser, 0, buflen);
132 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
133 	res = nss_getent(&db_root, _nss_initf_auuser, &context, &arg);
134 	arg.status = res;
135 	*h_errnop = arg.h_errno;
136 	trace2(TR_getauuser, 1, buflen);
137 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
138 }
139 
140 
141 au_user_str_t *
142 _getauusernam(const char *name, au_user_str_t *result, char *buffer,
143     int buflen, int *errnop)
144 {
145 	nss_XbyY_args_t arg;
146 	nss_status_t    res;
147 
148 	trace2(TR_getauusernam, 0, buflen);
149 	if (result == NULL) {
150 		*errnop = AUDITUSER_PARSE_ERANGE;
151 		return (NULL);
152 	}
153 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
154 	arg.key.name = name;
155 	arg.stayopen = auuser_stayopen;
156 	arg.h_errno = AUDITUSER_NOT_FOUND;
157 	res = nss_search(&db_root, _nss_initf_auuser,
158 	    NSS_DBOP_AUDITUSER_BYNAME, &arg);
159 	arg.status = res;
160 	*errnop = arg.h_errno;
161 	trace2(TR_getauusernam, 1, buflen);
162 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
163 }
164