xref: /titanic_51/usr/src/lib/libnsl/nss/getauuser.c (revision a77d64af7813dad3dad148a9974e0ec7b80d1f43)
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 2005 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 <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 ((instr >= buffer && (buffer + buflen) > instr) ||
75 	    (buffer >= instr && (instr + lenstr) > buffer))
76 		return (NSS_STR_PARSE_PARSE);
77 	if (lenstr >= buflen)
78 		return (NSS_STR_PARSE_ERANGE);
79 	(void) strncpy(buffer, instr, buflen);
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 	au_user->au_name = _strtok_escape(buffer, sep, &last);
89 	au_user->au_always = _strtok_escape(NULL, sep, &last);
90 	au_user->au_never = _strtok_escape(NULL, sep, &last);
91 
92 	return (0);
93 }
94 
95 
96 void
97 _setauuser(void)
98 {
99 	auuser_stayopen = 0;
100 	nss_setent(&db_root, _nss_initf_auuser, &context);
101 }
102 
103 
104 int
105 _endauuser(void)
106 {
107 	auuser_stayopen = 0;
108 	nss_endent(&db_root, _nss_initf_auuser, &context);
109 	nss_delete(&db_root);
110 	return (0);
111 }
112 
113 
114 au_user_str_t *
115 _getauuserent(au_user_str_t *result, char *buffer, int buflen, int *h_errnop)
116 {
117 	nss_XbyY_args_t arg;
118 	nss_status_t    res;
119 
120 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
121 	res = nss_getent(&db_root, _nss_initf_auuser, &context, &arg);
122 	arg.status = res;
123 	*h_errnop = arg.h_errno;
124 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
125 }
126 
127 
128 au_user_str_t *
129 _getauusernam(const char *name, au_user_str_t *result, char *buffer,
130     int buflen, int *errnop)
131 {
132 	nss_XbyY_args_t arg;
133 	nss_status_t    res;
134 
135 	if (result == NULL) {
136 		*errnop = AUDITUSER_PARSE_ERANGE;
137 		return (NULL);
138 	}
139 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2auuser);
140 	arg.key.name = name;
141 	arg.stayopen = auuser_stayopen;
142 	arg.h_errno = AUDITUSER_NOT_FOUND;
143 	res = nss_search(&db_root, _nss_initf_auuser,
144 	    NSS_DBOP_AUDITUSER_BYNAME, &arg);
145 	arg.status = res;
146 	*errnop = arg.h_errno;
147 	return ((au_user_str_t *)NSS_XbyY_FINI(&arg));
148 }
149