summaryrefslogtreecommitdiff
path: root/usr/src/lib/libidmap/common/directory_helper.c
blob: ff1df9aae6859f4b5ce4477efc339ef95c17be8f (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
/*
 * 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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Some helper routines for directory lookup.  These offer functions that
 * you could implement yourself on top of the generic routines, but since
 * they're a common request we implement them here.  (Well, OK, we cheat a bit
 * and call an internal routine to do the dirty work to reduce code
 * duplication, but you could still implement them using the generic routines.)
 */

#include <stdio.h>
#include <string.h>
#include <libuutil.h>
#include <rpcsvc/idmap_prot.h>
#include "directory.h"
#include "directory_private.h"
#include "directory_library_impl.h"
#include "sidutil.h"

/*
 * Given a username, return a text-form SID.
 *
 * The SID must be free()ed by the caller.
 *
 * d, if non-NULL, specifies an existing directory-search context.
 * If NULL, a temporary one will be created.
 */
directory_error_t
directory_sid_from_name_common(
    directory_t d,
    char *name,
    char *type,
    char **sid,
    uint64_t *classes)
{
	directory_t d1 = NULL;
	static char *attrs[] = {
		"objectSid",
		"objectClass",
		NULL,
	};
	directory_entry_t *ret_list = NULL;
	directory_error_t de;
	struct ret_sid {
		sid_t **objectSid;
		char **objectClass;
	} *ret_sid;

	/* Prep for error cases. */
	*sid = NULL;
	if (classes != NULL)
		*classes = 0;

	if (d == NULL) {
		de = directory_open(&d1);
		if (de != NULL)
			goto out;
	} else {
		d1 = d;
	}

	de = directory_get_v(d1, &ret_list, &name, 1, type, attrs);
	if (de != NULL)
		goto out;
	if (ret_list[0].err != NULL) {
		de = ret_list[0].err;
		ret_list[0].err = NULL;
		goto out;
	}

	ret_sid = (struct ret_sid *)ret_list[0].attrs;
	if (ret_sid == NULL)
		goto out;

	if (ret_sid->objectSid != NULL &&
	    ret_sid->objectSid[0] != NULL) {
		char text_sid[SID_STRSZ+1];
		sid_from_le(ret_sid->objectSid[0]);
		sid_tostr(ret_sid->objectSid[0], text_sid);
		*sid = strdup(text_sid);
		if (*sid == NULL)
			goto nomem;
	}

	if (ret_sid->objectClass != NULL &&
	    classes != NULL)
		*classes = class_bitmap(ret_sid->objectClass);

	goto out;

nomem:
	de = directory_error("ENOMEM.directory_sid_from_name_common",
	    "Insufficient memory retrieving data about SID", NULL);

out:
	directory_free(ret_list);
	if (d == NULL)
		directory_close(d1);
	return (de);
}

directory_error_t
directory_sid_from_name(
    directory_t d,
    char *name,
    char **sid,
    uint64_t *classes)
{
	return (directory_sid_from_name_common(d, name, DIRECTORY_ID_NAME, sid,
	    classes));
}

directory_error_t
directory_sid_from_user_name(directory_t d, char *name, char **sid)
{
	return (directory_sid_from_name_common(d, name, DIRECTORY_ID_USER, sid,
	    NULL));
}

directory_error_t
directory_sid_from_group_name(directory_t d, char *name, char **sid)
{
	return (directory_sid_from_name_common(d, name, DIRECTORY_ID_GROUP, sid,
	    NULL));
}

/*
 * Given a name or text-format SID, return a user@domain.
 *
 * The user@domain returned must be free()ed by the caller.
 *
 * Returns NULL and sets *name to NULL if no error occurred but the specified
 * entity does not exist.
 *
 * d, if non-NULL, specifies an existing directory-search context.
 * If NULL, a temporary one will be created.
 */
static
directory_error_t
directory_canon_common(
    directory_t d,
    char *id,
    char *id_type,
    char **canon,
    uint64_t *classes)
{
	directory_t d1 = NULL;
	directory_entry_t *ret_list = NULL;
	directory_error_t de;
	/*
	 * Attributes required to generate a canonical name, in named-list and
	 * structure form.
	 */
	static char *attrs[] = {
		"x-sun-canonicalName",
		"objectClass",
		NULL,
	};

	struct canon_name_ret {
		char **x_sun_canonicalName;
		char **objectClass;
	} *ret_name;

	/* Prep for error cases. */
	*canon = NULL;
	if (classes != NULL)
		*classes = 0;

	if (d == NULL) {
		de = directory_open(&d1);
		if (de != NULL)
			goto out;
	} else {
		d1 = d;
	}

	de = directory_get_v(d1, &ret_list, &id, 1, id_type, attrs);
	if (de != NULL)
		goto out;
	if (ret_list[0].err != NULL) {
		de = ret_list[0].err;
		ret_list[0].err = NULL;
		goto out;
	}

	ret_name = (struct canon_name_ret *)ret_list[0].attrs;
	if (ret_name == NULL)
		goto out;

	if (ret_name->x_sun_canonicalName != NULL &&
	    ret_name->x_sun_canonicalName[0] != NULL) {
		*canon = strdup(ret_name->x_sun_canonicalName[0]);
		if (*canon == NULL)
			goto nomem;
	}

	if (ret_name->objectClass != NULL &&
	    classes != NULL)
		*classes = class_bitmap(ret_name->objectClass);

	goto out;

nomem:
	de = directory_error("ENOMEM.directory_canon_common",
	    "Insufficient memory retrieving data about name", NULL);

out:
	directory_free(ret_list);
	if (d == NULL)
		directory_close(d1);
	return (de);
}

directory_error_t
directory_name_from_sid(
    directory_t d,
    char *sid,
    char **canon,
    uint64_t *classes)
{
	return (directory_canon_common(d, sid, DIRECTORY_ID_SID, canon,
	    classes));
}

directory_error_t
directory_canon_from_name(
    directory_t d,
    char *name,
    char **canon,
    uint64_t *classes)
{
	return (directory_canon_common(d, name, DIRECTORY_ID_NAME, canon,
	    classes));
}

directory_error_t
directory_canon_from_user_name(directory_t d, char *name, char **canon)
{
	return (
	    directory_canon_common(d, name, DIRECTORY_ID_USER, canon, NULL));
}

directory_error_t
directory_canon_from_group_name(directory_t d, char *name, char **canon)
{
	return (
	    directory_canon_common(d, name, DIRECTORY_ID_GROUP, canon, NULL));
}

boolean_t
is_in_list(char **list, char *val)
{
	for (; *list != NULL; list++) {
		if (uu_strcaseeq(*list, val))
			return (B_TRUE);
	}
	return (B_FALSE);
}

uint64_t
class_bitmap(char **objectClass)
{
	uint64_t ret = 0;

	for (; *objectClass != NULL; objectClass++) {
		if (uu_strcaseeq(*objectClass, "user") ||
		    uu_strcaseeq(*objectClass, "posixAccount"))
			ret |= DIRECTORY_CLASS_USER;

		if (uu_strcaseeq(*objectClass, "group") ||
		    uu_strcaseeq(*objectClass, "posixGroup"))
			ret |= DIRECTORY_CLASS_GROUP;
	}

	return (ret);
}