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
|
/*
* 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.
*/
/*
* Server-side support for directory information lookup functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <malloc.h>
#include <sys/types.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <libuutil.h>
#include <note.h>
#include "idmapd.h"
#include "directory.h"
#include "directory_private.h"
#include <rpcsvc/idmap_prot.h>
#include "directory_library_impl.h"
#include "directory_server_impl.h"
#include "sized_array.h"
/*
* Here's a list of all of the modules that provide directory
* information. In the fullness of time this should probably be
* a plugin-able switch mechanism.
* Note that the list is in precedence order.
*/
extern struct directory_provider_static directory_provider_builtin;
extern struct directory_provider_static directory_provider_nsswitch;
extern struct directory_provider_static directory_provider_ad;
struct directory_provider_static *providers[] = {
&directory_provider_builtin,
&directory_provider_nsswitch,
&directory_provider_ad,
};
/*
* This is the entry point for all directory lookup service requests.
*/
bool_t
directory_get_common_1_svc(
idmap_utf8str_list ids,
idmap_utf8str types,
idmap_utf8str_list attrs,
directory_results_rpc *result,
struct svc_req *req)
{
NOTE(ARGUNUSED(req))
int nids;
directory_entry_rpc *entries;
directory_error_t de;
int i;
nids = ids.idmap_utf8str_list_len;
entries = (directory_entry_rpc *)
calloc(nids, sizeof (directory_entry_rpc));
if (entries == NULL)
goto nomem;
result->directory_results_rpc_u.entries.entries_val = entries;
result->directory_results_rpc_u.entries.entries_len = nids;
result->failed = FALSE;
for (i = 0; i < nids; i++) {
if (strlen(ids.idmap_utf8str_list_val[i]) >
IDMAP_MAX_NAME_LEN) {
directory_entry_set_error(&entries[i],
directory_error("invalid_arg.id.too_long",
"Identifier too long", NULL));
}
}
for (i = 0; i < UU_NELEM(providers); i++) {
de = providers[i]->get(entries, &ids, types,
&attrs);
if (de != NULL)
goto err;
}
return (TRUE);
nomem:
de = directory_error("ENOMEM.get_common",
"Insufficient memory retrieving directory data", NULL);
err:
xdr_free(xdr_directory_results_rpc, (char *)result);
result->failed = TRUE;
return (
directory_error_to_rpc(&result->directory_results_rpc_u.err, de));
}
/*
* Split name into {domain, name}.
* Suggest allocating name and domain on the stack, same size as id,
* using variable length arrays.
*/
void
split_name(char *name, char *domain, char *id)
{
char *p;
if ((p = strchr(id, '@')) != NULL) {
(void) strlcpy(name, id, p - id + 1);
(void) strcpy(domain, p + 1);
} else if ((p = strchr(id, '\\')) != NULL) {
(void) strcpy(name, p + 1);
(void) strlcpy(domain, id, p - id + 1);
} else {
(void) strcpy(name, id);
(void) strcpy(domain, "");
}
}
/*
* Given a list of strings, return a set of directory attribute values.
*
* Mark that the attribute was found.
*
* Note that the terminating \0 is *not* included in the result, because
* that's the way that strings come from LDAP.
* (Note also that the client side stuff adds in a terminating \0.)
*
* Note that on error the array may have been partially populated and will
* need to be cleaned up by the caller. This is normally not a problem
* because the caller will need to clean up several such arrays.
*/
directory_error_t
str_list_dav(directory_values_rpc *lvals, const char * const *str_list, int n)
{
directory_value_rpc *dav;
int i;
if (n == 0) {
for (n = 0; str_list[n] != NULL; n++)
/* LOOP */;
}
dav = calloc(n, sizeof (directory_value_rpc));
if (dav == NULL)
goto nomem;
lvals->directory_values_rpc_u.values.values_val = dav;
lvals->directory_values_rpc_u.values.values_len = n;
lvals->found = TRUE;
for (i = 0; i < n; i++) {
int len;
len = strlen(str_list[i]);
dav[i].directory_value_rpc_val = uu_memdup(str_list[i], len);
if (dav[i].directory_value_rpc_val == NULL)
goto nomem;
dav[i].directory_value_rpc_len = len;
}
return (NULL);
nomem:
return (directory_error("ENOMEM.str_list_dav",
"Insufficient memory copying values"));
}
/*
* Given a list of unsigned integers, return a set of string directory
* attribute values.
*
* Mark that the attribute was found.
*
* Note that the terminating \0 is *not* included in the result, because
* that's the way that strings come from LDAP.
* (Note also that the client side stuff adds in a terminating \0.)
*
* Note that on error the array may have been partially populated and will
* need to be cleaned up by the caller. This is normally not a problem
* because the caller will need to clean up several such arrays.
*/
directory_error_t
uint_list_dav(directory_values_rpc *lvals, const unsigned int *array, int n)
{
directory_value_rpc *dav;
int i;
dav = calloc(n, sizeof (directory_value_rpc));
if (dav == NULL)
goto nomem;
lvals->directory_values_rpc_u.values.values_val = dav;
lvals->directory_values_rpc_u.values.values_len = n;
lvals->found = TRUE;
for (i = 0; i < n; i++) {
char buf[100]; /* larger than any integer */
int len;
(void) snprintf(buf, sizeof (buf), "%u", array[i]);
len = strlen(buf);
dav[i].directory_value_rpc_val = uu_memdup(buf, len);
if (dav[i].directory_value_rpc_val == NULL)
goto nomem;
dav[i].directory_value_rpc_len = len;
}
return (NULL);
nomem:
return (directory_error("ENOMEM.uint_list_dav",
"Insufficient memory copying values"));
}
/*
* Given a list of fixed-length binary chunks, return a set of binary
* directory attribute values.
*
* Mark that the attribute was found.
*
* Note that on error the array may have been partially populated and will
* need to be cleaned up by the caller. This is normally not a problem
* because the caller will need to clean up several such arrays.
*/
directory_error_t
bin_list_dav(directory_values_rpc *lvals, const void *array, int n, size_t sz)
{
directory_value_rpc *dav;
char *inbuf = (char *)array;
int i;
dav = calloc(n, sizeof (directory_value_rpc));
if (dav == NULL)
goto nomem;
lvals->directory_values_rpc_u.values.values_val = dav;
lvals->directory_values_rpc_u.values.values_len = n;
lvals->found = TRUE;
for (i = 0; i < n; i++) {
dav[i].directory_value_rpc_val = uu_memdup(inbuf, sz);
if (dav[i].directory_value_rpc_val == NULL)
goto nomem;
dav[i].directory_value_rpc_len = sz;
inbuf += sz;
}
return (NULL);
nomem:
return (directory_error("ENOMEM.bin_list_dav",
"Insufficient memory copying values"));
}
/*
* Set up to return an error on a particular directory entry.
* Note that the caller need not (and in fact must not) free
* the directory_error_t; it will be freed when the directory entry
* list is freed.
*/
void
directory_entry_set_error(directory_entry_rpc *ent, directory_error_t de)
{
xdr_free(xdr_directory_entry_rpc, (char *)&ent);
ent->status = DIRECTORY_ERROR;
(void) directory_error_to_rpc(&ent->directory_entry_rpc_u.err, de);
}
|