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