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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Directory lookup functions. These are shims that translate from the API
* into the RPC protocol.
*/
#include <assert.h>
#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 "directory.h"
#include "directory_private.h"
#include <rpcsvc/idmap_prot.h>
#include "directory_library_impl.h"
#include "sized_array.h"
static directory_error_t copy_directory_attribute_value(
directory_attribute_value_t *dav,
directory_values_rpc *dav_rpc);
static directory_error_t copy_directory_entry(directory_entry_t *ent,
directory_entry_rpc *ent_rpc);
static void directory_results_free(directory_results_rpc *dr);
static directory_datum_t directory_datum(void *data, size_t len);
static void directory_datum_free(directory_datum_t d);
/*
* This is the actual implementation of the opaque directory_t structure.
*/
struct directory {
CLIENT *client;
};
/*
* Set up a directory search context.
*/
directory_error_t
directory_open(directory_t *ret)
{
directory_t d;
directory_error_t de;
char host[] = "localhost";
*ret = NULL;
d = calloc(1, sizeof (*d));
if (d == NULL)
goto nomem;
d->client = clnt_door_create(IDMAP_PROG, IDMAP_V1, 0);
if (d->client == NULL) {
de = directory_error("clnt_create.directory_open",
"Error: %1",
clnt_spcreateerror(host),
NULL);
goto err;
}
*ret = d;
return (NULL);
nomem:
de = directory_error("ENOMEM.directory_open",
"Insufficient memory setting up directory access", NULL);
err:
directory_close(d);
return (de);
}
/*
* Tear down a directory search context.
*
* Does nothing if d==NULL.
*/
void
directory_close(directory_t d)
{
if (d == NULL)
return;
if (d->client != NULL)
clnt_destroy(d->client);
free(d);
}
/*
* Given a list of identifiers, a list of their types, and a list of attributes,
* return the information.
*/
directory_error_t
directory_get_v(
directory_t d,
directory_entry_list_t *ret,
char **ids,
int nids,
char *types,
char **attr_list)
{
int nattrs;
directory_entry_list_t del;
directory_error_t de;
directory_results_rpc dr;
idmap_utf8str_list sl_ids;
idmap_utf8str_list sl_attrs;
directory_entry_rpc *users;
int i;
enum clnt_stat cs;
*ret = NULL;
del = NULL;
if (nids == 0) {
for (nids = 0; ids[nids] != NULL; nids++)
/* LOOP */;
}
for (nattrs = 0; attr_list[nattrs] != NULL; nattrs++)
/* LOOP */;
sl_ids.idmap_utf8str_list_len = nids;
sl_ids.idmap_utf8str_list_val = ids;
sl_attrs.idmap_utf8str_list_len = nattrs;
sl_attrs.idmap_utf8str_list_val = attr_list;
(void) memset(&dr, 0, sizeof (dr));
cs = directory_get_common_1(sl_ids, types, sl_attrs, &dr, d->client);
if (cs != RPC_SUCCESS) {
char errbuf[100]; /* well long enough for any integer */
(void) sprintf(errbuf, "%d", cs);
de = directory_error("RPC.Get_common",
"Get_common RPC (%1)%2", errbuf,
clnt_sperror(d->client, ""), NULL);
goto err;
}
if (dr.failed) {
de = directory_error_from_rpc(
&dr.directory_results_rpc_u.err);
goto err;
}
assert(dr.directory_results_rpc_u.entries.entries_len == nids);
users = dr.directory_results_rpc_u.entries.entries_val;
del = sized_array(nids, sizeof (directory_entry_t));
for (i = 0; i < nids; i++) {
de = copy_directory_entry(&del[i], &users[i]);
if (de != NULL)
goto err;
}
directory_results_free(&dr);
*ret = del;
return (NULL);
err:
directory_results_free(&dr);
directory_free(del);
return (de);
}
/*
* Free the results from a directory_get_*() request.
*/
void
directory_free(directory_entry_list_t del)
{
directory_entry_t *ent;
directory_attribute_value_t dav;
int i;
int j;
int k;
if (del == NULL)
return;
/* For each directory entry returned */
for (i = 0; i < sized_array_n(del); i++) {
ent = &del[i];
if (ent->attrs != NULL) {
/* For each attribute */
for (j = 0; j < sized_array_n(ent->attrs); j++) {
dav = ent->attrs[j];
if (dav != NULL) {
for (k = 0; k < sized_array_n(dav); k++)
directory_datum_free(dav[k]);
sized_array_free(dav);
}
}
sized_array_free(ent->attrs);
}
directory_error_free(ent->err);
}
sized_array_free(del);
}
/*
* Create a directory datum. Note that we allocate an extra byte and
* zero it, so that strings get null-terminated. Return NULL on error.
*/
static
directory_datum_t
directory_datum(void *data, size_t len)
{
void *p;
p = sized_array(len + 1, 1);
if (p == NULL)
return (NULL);
(void) memcpy(p, data, len);
*((char *)p + len) = '\0';
return (p);
}
/*
* Return the size of a directory_datum_t. Note that this does not include
* the terminating \0, so it represents the value as returned by LDAP.
*/
size_t
directory_datum_len(directory_datum_t d)
{
/*
* Deduct the terminal \0, so that binary data gets the
* expected length.
*/
return (sized_array_n(d) - 1);
}
static
void
directory_datum_free(directory_datum_t d)
{
sized_array_free(d);
}
/*
* Unmarshall an RPC directory entry into an API directory entry.
*/
static
directory_error_t
copy_directory_entry(
directory_entry_t *ent,
directory_entry_rpc *ent_rpc)
{
int nattrs;
int i;
directory_error_t de;
/* If the entry wasn't found, leave the entry attrs and err NULL. */
if (ent_rpc->status == DIRECTORY_NOT_FOUND)
return (NULL);
if (ent_rpc->status == DIRECTORY_ERROR) {
ent->err = directory_error_from_rpc(
&ent_rpc->directory_entry_rpc_u.err);
return (NULL);
}
nattrs = ent_rpc->directory_entry_rpc_u.attrs.attrs_len;
ent->attrs = sized_array(nattrs, sizeof (directory_attribute_value_t));
if (ent->attrs == NULL) {
return (directory_error("ENOMEM.copy_directory_entry",
"Insufficient memory copying directory entry", NULL));
}
for (i = 0; i < nattrs; i++) {
de = copy_directory_attribute_value(&ent->attrs[i],
&ent_rpc->directory_entry_rpc_u.attrs.attrs_val[i]);
if (de != NULL)
return (de);
}
return (NULL);
}
/*
* Unmarshall an RPC directory attribute value into the API equivalent.
*
* Note that on error some entries may have been copied, and so
* the caller needs to clean up dav. This is normally not a problem
* since the caller will have called this function several times and
* will need to clean up the results from the other calls too.
*/
static
directory_error_t
copy_directory_attribute_value(
directory_attribute_value_t *dav,
directory_values_rpc *dav_rpc)
{
int i;
int nvals;
directory_value_rpc *vals;
/* If it wasn't found, leave the corresponding entry NULL */
if (!dav_rpc->found)
return (NULL);
nvals = dav_rpc->directory_values_rpc_u.values.values_len;
*dav = sized_array(nvals + 1, sizeof (directory_datum_t));
if (*dav == NULL) {
return (directory_error("ENOMEM.copy_directory_attribute_value",
"Insufficient memory copying directory entry", NULL));
}
vals = dav_rpc->directory_values_rpc_u.values.values_val;
for (i = 0; i < nvals; i++) {
(*dav)[i] = directory_datum(vals[i].directory_value_rpc_val,
vals[i].directory_value_rpc_len);
if ((*dav)[i] == NULL) {
return (directory_error(
"ENOMEM.copy_directory_attribute_value",
"Insufficient memory copying directory entry",
NULL));
}
}
return (NULL);
}
/*
* Free the results of a directory RPC request.
*/
static
void
directory_results_free(directory_results_rpc *dr)
{
xdr_free(xdr_directory_results_rpc, (char *)&dr);
}
|