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