summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ldap/ns_ldap/ldapaddrbac.c
blob: 6e8d9f76b9e4c631d1aa495318cbca3b58e87c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * ldapaddrbac.c
 *
 * Routines to add RBAC /etc files into LDAP.
 * Can also be used to dump entries from a ldap container in /etc format.
 */

#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <strings.h>
#include <sys/param.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <locale.h>
#include <syslog.h>
#include "ldapaddent.h"

#undef opaque
#undef	GROUP
#include <bsm/libbsm.h>

extern	char	*_strtok_escape(char *, char *, char **); /* from libnsl */

#include <user_attr.h>
#include <prof_attr.h>
#include <exec_attr.h>
#include <auth_attr.h>

/*
 * The parsing routines for RBAC databases
 */

/*
 * genent_attr:
 *   Generic function for generating entries for all of the *_attr databases.
 */
int
genent_attr(
	char	*line,		/* entry to parse */
	int	ncol,		/* number of columns in the database */
	entry_col	**ecolret)	/* return entry array */
{
	int		i;
	char		(*buf)[BUFSIZ + 1];
	char		*s;
	char		*sep = KV_TOKEN_DELIMIT;
	char		*lasts;
	entry_col	*ecol;

	/*
	 * check input length
	 */
	if (strlen(line) >= sizeof (*buf)) {
		(void) strcpy(parse_err_msg, "line too long");
		return (GENENT_PARSEERR);
	}

	/*
	 * setup and clear column data
	 */
	if ((ecol = (entry_col *)malloc(ncol * sizeof (entry_col) +
	    sizeof (*buf))) == NULL)
		return (GENENT_ERR);
	(void) memset((char *)ecol, 0, ncol * sizeof (ecol));

	/* don't scribble over input */
	buf = (char (*)[sizeof (*buf)]) (ecol + ncol);
	(void) strncpy((char *)buf, line, sizeof (*buf));

	/* Split up columns */
	for (i = 0; i < ncol; i++, buf = NULL) {
		s = _strtok_escape((char *)buf, sep, &lasts);
		if (s == NULL) {
			ecol[i].ec_value.ec_value_val = "";
			ecol[i].ec_value.ec_value_len = 0;
		} else {
			ecol[i].ec_value.ec_value_val = s;
			ecol[i].ec_value.ec_value_len = strlen(s)+1;
		}
	}

	*ecolret = ecol;
	return (GENENT_OK);
}

int
genent_user_attr(char *line, int (*cback)())
{
	entry_col	*ecol;
	userstr_t	data;
	int		res, retval;

	/*
	 * parse entry into columns
	 */
	res = genent_attr(line, USERATTR_DB_NCOL, &ecol);
	if (res != GENENT_OK)
		return (res);

	data.name = ecol[0].ec_value.ec_value_val;
	data.qualifier = ecol[1].ec_value.ec_value_val;
	data.res1 = NULL;
	data.res2 = NULL;
	data.attr = ecol[4].ec_value.ec_value_val;

	if (flags & F_VERBOSE)
		(void) fprintf(stdout,
		    gettext("Adding entry : %s\n"), data.name);

	retval = (*cback)(&data, 1);
	if (retval != NS_LDAP_SUCCESS) {
		if (retval == LDAP_NO_SUCH_OBJECT)
			(void) fprintf(stdout,
			gettext("Cannot add user_attr entry (%s), "
			"add passwd entry first\n"), data.name);
		if (continue_onerror == 0) res = GENENT_CBERR;
	}

	free(ecol);

	return (res);
}

void
dump_user_attr(ns_ldap_result_t *res)
{
	char	**value = NULL;

	value = __ns_ldap_getAttr(res->entry, "uid");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	else
		return;

	(void) fprintf(stdout, "::::");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrKeyValue");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, "\n");
}

int
genent_prof_attr(char *line, int (*cback)())
{
	entry_col	*ecol;
	profstr_t	data;
	int		res, retval;

	/*
	 * parse entry into columns
	 */
	res = genent_attr(line, PROFATTR_DB_NCOL, &ecol);
	if (res != GENENT_OK)
		return (res);

	data.name = ecol[0].ec_value.ec_value_val;
	data.res1 = NULL;
	data.res2 = NULL;
	data.desc = ecol[3].ec_value.ec_value_val;
	data.attr = ecol[4].ec_value.ec_value_val;

	if (flags & F_VERBOSE)
		(void) fprintf(stdout,
		    gettext("Adding entry : %s\n"), data.name);

	retval = (*cback)(&data, 0);
	if (retval == LDAP_ALREADY_EXISTS) {
		if (continue_onerror)
			(void) fprintf(stderr,
			    gettext("Entry: %s - already Exists,"
			    " skipping it.\n"),
			    data.name);
		else {
			res = GENENT_CBERR;
			(void) fprintf(stderr,
			    gettext("Entry: %s - already Exists\n"),
			    data.name);
		}
	} else if (retval)
		res = GENENT_CBERR;

	free(ecol);

	return (res);
}

void
dump_prof_attr(ns_ldap_result_t *res)
{
	char	**value = NULL;

	value = __ns_ldap_getAttr(res->entry, "cn");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	else
		return;

	(void) fprintf(stdout, ":::");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrLongDesc");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, ":");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrKeyValue");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, "\n");
}

int
genent_exec_attr(char *line, int (*cback)())
{
	entry_col	*ecol;
	execstr_t	data;
	int		res, retval;

	/*
	 * parse entry into columns
	 */
	res = genent_attr(line, EXECATTR_DB_NCOL, &ecol);
	if (res != GENENT_OK)
		return (res);

	data.name = ecol[0].ec_value.ec_value_val;
	data.policy = ecol[1].ec_value.ec_value_val;
	data.type = ecol[2].ec_value.ec_value_val;
	data.res1 = NULL;
	data.res2 = NULL;
	data.id = ecol[5].ec_value.ec_value_val;
	data.attr = ecol[6].ec_value.ec_value_val;
	data.next = NULL;

	if (flags & F_VERBOSE)
		(void) fprintf(stdout,
		    gettext("Adding entry : %s+%s+%s+%s\n"),
		    data.name, data.policy, data.type, data.id);

	retval = (*cback)(&data, 0);
	if (retval == LDAP_ALREADY_EXISTS) {
		if (continue_onerror)
			(void) fprintf(stderr,
			    gettext("Entry: %s+%s+%s+%s - already Exists,"
			    " skipping it.\n"),
			    data.name, data.policy, data.type, data.id);
		else {
			res = GENENT_CBERR;
			(void) fprintf(stderr,
			    gettext("Entry: %s+%s+%s+%s - already Exists\n"),
			    data.name, data.policy, data.type, data.id);
		}
	} else if (retval)
		res = GENENT_CBERR;

	free(ecol);

	return (res);
}

void
dump_exec_attr(ns_ldap_result_t *res)
{
	char	**profile;
	char	**policy;
	char	**type;
	char	**id;
	char	**value;

	profile = __ns_ldap_getAttr(res->entry, "cn");
	policy = __ns_ldap_getAttr(res->entry, "SolarisKernelSecurityPolicy");
	type = __ns_ldap_getAttr(res->entry, "SolarisProfileType");
	id = __ns_ldap_getAttr(res->entry, "SolarisProfileId");

	if (profile == NULL || profile[0] == NULL ||
	    policy == NULL || policy[0] == NULL ||
	    type == NULL || type[0] == NULL ||
	    id == NULL || id[0] == NULL)
		return;

	(void) fprintf(stdout, "%s", profile[0]);
	(void) fprintf(stdout, ":");
	(void) fprintf(stdout, "%s", policy[0]);
	(void) fprintf(stdout, ":");
	(void) fprintf(stdout, "%s", type[0]);
	(void) fprintf(stdout, ":::");
	(void) fprintf(stdout, "%s", id[0]);
	(void) fprintf(stdout, ":");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrKeyValue");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, "\n");
}

int
genent_auth_attr(char *line, int (*cback)())
{
	entry_col	*ecol;
	authstr_t	data;
	int		res, retval;

	/*
	 * parse entry into columns
	 */
	res = genent_attr(line, AUTHATTR_DB_NCOL, &ecol);
	if (res != GENENT_OK)
		return (res);

	data.name = ecol[0].ec_value.ec_value_val;
	data.res1 = NULL;
	data.res2 = NULL;
	data.short_desc = ecol[3].ec_value.ec_value_val;
	data.long_desc = ecol[4].ec_value.ec_value_val;
	data.attr = ecol[5].ec_value.ec_value_val;

	if (flags & F_VERBOSE)
		(void) fprintf(stdout,
		    gettext("Adding entry : %s\n"), data.name);

	retval = (*cback)(&data, 0);
	if (retval == LDAP_ALREADY_EXISTS) {
		if (continue_onerror)
			(void) fprintf(stderr,
			    gettext("Entry: %s - already Exists,"
			    " skipping it.\n"), data.name);
		else {
			res = GENENT_CBERR;
			(void) fprintf(stderr,
			    gettext("Entry: %s - already Exists\n"),
			    data.name);
		}
	} else if (retval)
		res = GENENT_CBERR;

	free(ecol);

	return (res);
}

void
dump_auth_attr(ns_ldap_result_t *res)
{
	char	**value = NULL;

	value = __ns_ldap_getAttr(res->entry, "cn");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	else
		return;

	(void) fprintf(stdout, ":::");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrShortDesc");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, ":");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrLongDesc");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, ":");
	value = __ns_ldap_getAttr(res->entry, "SolarisAttrKeyValue");
	if (value && value[0])
		(void) fprintf(stdout, "%s", value[0]);
	(void) fprintf(stdout, "\n");
}