xref: /titanic_52/usr/src/lib/libnsl/nss/getauuser.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "mt.h"
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <nss_dbdefs.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 	p->name	= NSS_DBNAM_AUDITUSER;
56 	p->config_name    = NSS_DBNAM_PASSWD;  /* use config for "passwd" */
57 	p->default_config = NSS_DEFCONF_AUDITUSER;
58 }
59 
60 
61 /*
62  * Return values: 0 = success, 1 = parse error, 2 = erange ...
63  * The structure pointer passed in is a structure in the caller's space
64  * wherein the field pointers would be set to areas in the buffer if
65  * need be. instring and buffer should be separate areas.
66  */
67 int
68 str2auuser(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
69 {
70 	char		*last = NULL;
71 	char		*sep = KV_TOKEN_DELIMIT;
72 	au_user_str_t	*au_user = (au_user_str_t *)ent;
73 
74 	if (lenstr >= buflen)
75 		return (NSS_STR_PARSE_ERANGE);
76 
77 	if (instr != buffer)
78 		(void) strncpy(buffer, instr, buflen);
79 
80 	/*
81 	 * Remove newline that nis (yp_match) puts at the
82 	 * end of the entry it retrieves from the map.
83 	 */
84 	if (buffer[lenstr] == '\n') {
85 		buffer[lenstr] = '\0';
86 	}
87 
88 	/* quick exit do not entry fill if not needed */
89 	if (ent == (void *)NULL)
90 		return (NSS_STR_PARSE_SUCCESS);
91 
92 	au_user->au_name = _strtok_escape(buffer, sep, &last);
93 	au_user->au_always = _strtok_escape(NULL, sep, &last);
94 	au_user->au_never = _strtok_escape(NULL, sep, &last);
95 
96 	return (0);
97 }
98 
99 
100 void
101 _setauuser(void)
102 {
103 	auuser_stayopen = 0;
104 	nss_setent(&db_root, _nss_initf_auuser, &context);
105 }
106 
107 
108 int
109 _endauuser(void)
110 {
111 	auuser_stayopen = 0;
112 	nss_endent(&db_root, _nss_initf_auuser, &context);
113 	nss_delete(&db_root);
114 	return (0);
115 }
116 
117 
118 au_user_str_t *
119 _getauuserent(au_user_str_t *result, char *buffer, int buflen, int *h_errnop)
120 {
121 	nss_XbyY_args_t arg;
122 	nss_status_t    res;
123 
124 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
125 	res = nss_getent(&db_root, _nss_initf_auuser, &context, &arg);
126 	arg.status = res;
127 	*h_errnop = arg.h_errno;
128 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
129 }
130 
131 
132 au_user_str_t *
133 _getauusernam(const char *name, au_user_str_t *result, char *buffer,
134     int buflen, int *errnop)
135 {
136 	nss_XbyY_args_t arg;
137 	nss_status_t    res;
138 
139 	if (result == NULL) {
140 		*errnop = AUDITUSER_PARSE_ERANGE;
141 		return (NULL);
142 	}
143 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
144 	arg.key.name = name;
145 	arg.stayopen = auuser_stayopen;
146 	arg.h_errno = AUDITUSER_NOT_FOUND;
147 	res = nss_search(&db_root, _nss_initf_auuser,
148 	    NSS_DBOP_AUDITUSER_BYNAME, &arg);
149 	arg.status = res;
150 	*errnop = arg.h_errno;
151 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
152 }
153