Print this page
12236 getmembers_DN doesn't properly handle errors from __ns_ldap_dn2uid
12240 nss_ldap does not properly look up group members by distinguished name
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libsldap/common/ns_mapping.c
+++ new/usr/src/lib/libsldap/common/ns_mapping.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
23 + * Copyright 2020 Joyent, Inc.
23 24 */
24 25
25 26 #include <stdlib.h>
26 27 #include <strings.h>
27 28 #include <ctype.h>
28 29 #include <locale.h>
29 30 #include <syslog.h>
30 31 #include "ns_internal.h"
31 32
32 33 /*
33 34 * Calculate a hash for a string
34 35 * Based on elf_hash algorithm, hash is case insensitive
35 36 * Uses tolower instead of _tolower because of I18N
36 37 */
37 38
38 39 static unsigned long
39 40 ns_hash(const char *str)
40 41 {
41 42 unsigned int hval = 0;
42 43
43 44 while (*str) {
44 45 unsigned int g;
45 46
46 47 hval = (hval << 4) + tolower(*str++);
47 48 if ((g = (hval & 0xf0000000)) != 0)
48 49 hval ^= g >> 24;
49 50 hval &= ~g;
50 51 }
51 52 return ((unsigned long)hval);
52 53 }
53 54
54 55 /*
55 56 * Scan a hash table hit for a matching hash entry.
56 57 * Assume service and str are non-NULL.
57 58 */
58 59
59 60 static ns_hash_t *
60 61 ns_scan_hash(ns_hashtype_t type, const char *service,
61 62 const char *str, ns_hash_t *idx)
62 63 {
63 64 while (idx) {
64 65 if (idx->h_type == type &&
65 66 strcasecmp(service, idx->h_map->service) == 0 &&
66 67 strcasecmp(str, idx->h_map->orig) == 0) {
67 68 return (idx);
68 69 }
69 70 idx = idx->h_next;
70 71 }
71 72 return ((ns_hash_t *)NULL);
72 73 }
73 74
74 75 /*
75 76 * Find an entry in the hash table
76 77 */
77 78
78 79 static ns_hash_t *
79 80 ns_get_hash(const ns_config_t *config,
80 81 ns_hashtype_t type, const char *service, const char *str)
81 82 {
82 83 ns_hash_t *idx, *hashp;
83 84 unsigned long hash;
84 85
85 86 if (config == NULL || service == NULL || str == NULL)
86 87 return (NULL);
87 88
88 89 hash = ns_hash(str) % NS_HASH_MAX;
89 90 idx = config->hashTbl[hash];
90 91 hashp = ns_scan_hash(type, service, str, idx);
91 92
92 93 return (hashp);
93 94 }
94 95
95 96 /*
96 97 * free a map entry
97 98 */
98 99
99 100 static void
100 101 ns_free_map(ns_mapping_t *mapp)
101 102 {
102 103 char **ptr;
103 104
104 105 if (mapp == NULL)
105 106 return;
106 107 if (mapp->service) {
107 108 free(mapp->service);
108 109 mapp->service = NULL;
109 110 }
110 111 if (mapp->orig) {
111 112 free(mapp->orig);
112 113 mapp->orig = NULL;
113 114 }
114 115 if (mapp->map) {
115 116 for (ptr = mapp->map; *ptr; ptr++)
116 117 free(*ptr);
117 118 free(mapp->map);
118 119 mapp->map = NULL;
119 120 }
120 121 free(mapp);
121 122 }
122 123
123 124 /*
124 125 * Remove a hash table entry.
125 126 * This function is not MT safe.
126 127 */
127 128
128 129 static ns_hash_t *
129 130 ns_free_hash(ns_hash_t *p)
130 131 {
131 132 ns_mapping_t *map;
132 133 ns_hash_t *next;
133 134
134 135 map = p->h_map;
135 136 next = p->h_next;
136 137 ns_free_map(map);
137 138 free(p);
138 139 return (next);
139 140 }
140 141
141 142 /*
142 143 * destroy the hash table.
143 144 * This function is not MT safe.
144 145 */
145 146
146 147 void
147 148 __s_api_destroy_hash(ns_config_t *config)
148 149 {
149 150 ns_hash_t *next;
150 151 int i;
151 152
152 153 if (config == NULL)
153 154 return;
154 155 for (i = 0; i < NS_HASH_MAX; i++) {
155 156 next = config->hashTbl[i];
156 157 while (next != NULL) {
157 158 next = ns_free_hash(next);
158 159 }
159 160 config->hashTbl[i] = NULL;
160 161 }
161 162 }
162 163
163 164 /*
164 165 * Add a hash entry to the hash table.
165 166 * This function is not MT safe.
166 167 * Assume map, map->orig, map->service are non-NULL.
167 168 */
168 169
169 170 int
170 171 __s_api_add_map2hash(ns_config_t *config, ns_hashtype_t type,
171 172 ns_mapping_t *map)
172 173 {
173 174 ns_hash_t *idx, *newp;
174 175 unsigned long hash;
175 176
176 177 if (config == NULL)
177 178 return (NS_HASH_RC_CONFIG_ERROR);
178 179
179 180 hash = ns_hash(map->orig) % NS_HASH_MAX;
180 181 idx = config->hashTbl[hash];
181 182 if (idx != NULL &&
182 183 ns_scan_hash(type, map->service, map->orig, idx) != NULL) {
183 184 return (NS_HASH_RC_EXISTED);
184 185 }
185 186
186 187 newp = (ns_hash_t *)malloc(sizeof (ns_hash_t));
187 188 if (newp == NULL)
188 189 return (NS_HASH_RC_NO_MEMORY);
189 190 newp->h_type = type;
190 191 newp->h_map = map;
191 192 newp->h_next = idx;
192 193 config->hashTbl[hash] = newp;
193 194 newp->h_llnext = config->llHead;
↓ open down ↓ |
161 lines elided |
↑ open up ↑ |
194 195 config->llHead = newp;
195 196 return (NS_HASH_RC_SUCCESS);
196 197 }
197 198
198 199
199 200 /*
200 201 * Parse an attribute map string.
201 202 * Assume space is the only legal whitespace.
202 203 * attributeMap syntax:
203 204 * attributeMap = serviceId ":" origAttribute "="
204 - * attributes
205 + * attributes
205 206 * origAttribute = attribute
206 207 * attributes = wattribute *( space wattribute )
207 208 * wattribute = whsp newAttribute whsp
208 209 * newAttribute = descr | "*NULL*"
209 210 * attribute = descr
210 211 *
211 212 * objectclassMap syntax:
212 213 * objectclassMap = serviceId ":" origObjectclass "="
213 - * objectclass
214 + * objectclass
214 215 * origObjectclass = objectclass
215 216 * objectclass = keystring
216 217 */
217 218
218 219 int
219 220 __s_api_parse_map(char *cp, char **sid, char **origA, char ***mapA)
220 221 {
221 222 char *sptr, *dptr, **mapp;
222 223 int i, max;
223 224
224 225 *sid = NULL;
225 226 *origA = NULL;
226 227 *mapA = NULL;
227 228
228 229 sptr = cp;
229 230 dptr = strchr(sptr, COLONTOK);
230 231 if (dptr == NULL)
231 232 return (NS_HASH_RC_SYNTAX_ERROR);
232 233 i = dptr - sptr + 1;
233 234 *sid = (char *)malloc(i);
234 235 if (*sid == NULL)
235 236 return (NS_HASH_RC_NO_MEMORY);
236 237 (void) strlcpy(*sid, sptr, i);
237 238 sptr = dptr+1;
238 239
239 240 dptr = strchr(sptr, TOKENSEPARATOR);
240 241 if (dptr == NULL) {
241 242 free(*sid);
242 243 *sid = NULL;
243 244 return (NS_HASH_RC_SYNTAX_ERROR);
244 245 }
245 246 i = dptr - sptr + 1;
246 247 *origA = (char *)malloc(i);
247 248 if (*origA == NULL) {
248 249 free(*sid);
249 250 *sid = NULL;
250 251 return (NS_HASH_RC_NO_MEMORY);
251 252 }
252 253 (void) strlcpy(*origA, sptr, i);
253 254 sptr = dptr+1;
254 255
255 256 max = 1;
256 257 for (dptr = sptr; *dptr; dptr++) {
257 258 if (*dptr == SPACETOK) {
258 259 max++;
259 260 while (*(dptr+1) == SPACETOK)
260 261 dptr++;
261 262 }
262 263 }
263 264 *mapA = (char **)calloc(max+1, sizeof (char *));
264 265 if (*mapA == NULL) {
265 266 free(*sid);
266 267 *sid = NULL;
267 268 free(*origA);
268 269 *origA = NULL;
269 270 return (NS_HASH_RC_NO_MEMORY);
270 271 }
271 272 mapp = *mapA;
272 273
273 274 while (*sptr) {
274 275 while (*sptr == SPACETOK)
275 276 sptr++;
276 277 dptr = sptr;
277 278 while (*dptr && *dptr != SPACETOK)
278 279 dptr++;
279 280 i = dptr - sptr + 1;
280 281 *mapp = (char *)malloc(i);
281 282 if (*mapp == NULL) {
282 283 free(*sid);
283 284 *sid = NULL;
284 285 free(*origA);
285 286 *origA = NULL;
286 287 __s_api_free2dArray(*mapA);
287 288 *mapA = NULL;
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
288 289 return (NS_HASH_RC_NO_MEMORY);
289 290 }
290 291 (void) strlcpy(*mapp, sptr, i);
291 292 mapp++;
292 293 sptr = dptr;
293 294 }
294 295 return (NS_HASH_RC_SUCCESS);
295 296 }
296 297
297 298
298 -static void
299 +void
299 300 __ns_ldap_freeASearchDesc(ns_ldap_search_desc_t *ptr)
300 301 {
301 302 if (ptr == NULL)
302 303 return;
303 304 if (ptr->basedn)
304 305 free(ptr->basedn);
305 306 if (ptr->filter)
306 307 free(ptr->filter);
307 308 free(ptr);
308 309 }
309 310
310 311 /*
311 312 * Parse a service descriptor
312 313 * and create a service descriptor struct
313 314 * SD Format:
314 315 * serviceid:[base][?[scope][?[filter]]];[[base][?[scope][?[filter]]]]
315 316 * desc format:
316 317 * [base][?[scope][?[filter]]]
317 318 */
318 319
319 320 typedef enum _ns_parse_state {
320 321 P_ERROR, P_INIT, P_BASEDN, P_SCOPE,
321 322 P_INIFILTER, P_FILTER, P_END, P_EXIT, P_MEMERR
322 323 } _ns_parse_state_t;
323 324
324 325 static
325 326 int
326 327 __s_api_parseASearchDesc(const char *service,
327 328 char **cur, ns_ldap_search_desc_t **ret)
328 329 {
329 330 ns_ldap_search_desc_t *ptr;
330 331 char *sptr, *dptr;
331 332 int i, rc;
332 333 ns_ldap_error_t **errorp = NULL;
333 334 ns_ldap_error_t *error = NULL;
334 335 void **paramVal = NULL;
335 336 char **dns = NULL;
336 337 _ns_parse_state_t state = P_INIT;
337 338 int quoted = 0;
338 339 int wasquoted = 0;
339 340 int empty = 1;
340 341
341 342 if (ret == NULL)
342 343 return (NS_LDAP_INVALID_PARAM);
343 344 *ret = NULL;
344 345 if (cur == NULL)
345 346 return (NS_LDAP_INVALID_PARAM);
346 347
347 348 ptr = (ns_ldap_search_desc_t *)
348 349 calloc(1, sizeof (ns_ldap_search_desc_t));
349 350 if (ptr == NULL)
350 351 return (NS_LDAP_MEMORY);
351 352
352 353 sptr = *cur;
353 354
354 355 /* Get the default scope */
355 356 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
356 357 ¶mVal, errorp)) != NS_LDAP_SUCCESS) {
357 358 (void) __ns_ldap_freeError(errorp);
358 359 __ns_ldap_freeASearchDesc(ptr);
359 360 ptr = NULL;
360 361 return (NS_LDAP_MEMORY);
361 362 }
362 363 if (paramVal && *paramVal)
363 364 ptr->scope = * (ScopeType_t *)(*paramVal);
364 365 else
365 366 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
366 367 (void) __ns_ldap_freeParam(¶mVal);
367 368 paramVal = NULL;
368 369
369 370 for (/* none */; state != P_EXIT && sptr && *sptr; sptr++) {
370 371 empty = 0;
371 372 switch (state) {
372 373 case P_INIT:
373 374 if (*sptr == QUESTTOK) {
374 375 /* No basedn */
375 376 ptr->basedn = strdup("");
376 377 if (!ptr->basedn) {
377 378 state = P_MEMERR;
378 379 break;
379 380 }
380 381 state = P_SCOPE;
381 382 break;
382 383 }
383 384 if (*sptr == SEMITOK) {
384 385 /* No SSD */
385 386 ptr->basedn = strdup("");
386 387 if (!ptr->basedn) {
387 388 state = P_MEMERR;
388 389 break;
389 390 }
390 391 state = P_EXIT;
391 392 break;
392 393 }
393 394 /* prepare to copy DN */
394 395 i = strlen(sptr) + 1;
395 396 ptr->basedn = dptr = (char *)calloc(i, sizeof (char));
396 397 if (!ptr->basedn) {
397 398 state = P_MEMERR;
398 399 break;
399 400 }
400 401 if (*sptr == BSLTOK) {
401 402 if (*(sptr+1) == '\0') {
402 403 /* error */
403 404 state = P_ERROR;
404 405 break;
405 406 }
406 407 if (*(sptr+1) == QUOTETOK ||
407 408 *(sptr+1) == BSLTOK) {
408 409 /* escaped CHARS */
409 410 sptr++;
410 411 } else {
411 412 *dptr++ = *sptr++;
412 413 }
413 414 *dptr++ = *sptr;
414 415 } else if (*sptr == QUOTETOK) {
415 416 quoted = 1;
416 417 wasquoted = 1;
417 418 } else {
418 419 *dptr++ = *sptr;
419 420 }
420 421 state = P_BASEDN;
421 422 break;
422 423 case P_INIFILTER:
423 424 if (*sptr == SEMITOK) {
424 425 /* No filter and no more SSD */
425 426 state = P_EXIT;
426 427 break;
427 428 }
428 429 /* prepare to copy DN */
429 430 i = strlen(sptr) + 1;
430 431 ptr->filter = dptr = (char *)calloc(i, sizeof (char));
431 432 if (!ptr->filter) {
432 433 state = P_MEMERR;
433 434 break;
434 435 }
435 436 if (*sptr == BSLTOK) {
436 437 if (*(sptr+1) == '\0') {
437 438 /* error */
438 439 state = P_ERROR;
439 440 break;
440 441 }
441 442 if (*(sptr+1) == QUOTETOK ||
442 443 *(sptr+1) == BSLTOK) {
443 444 /* escaped CHARS */
444 445 sptr++;
445 446 } else {
446 447 *dptr++ = *sptr++;
447 448 }
448 449 *dptr++ = *sptr;
449 450 } else if (*sptr == QUOTETOK) {
450 451 quoted = 1;
451 452 wasquoted = 1;
452 453 } else {
453 454 *dptr++ = *sptr;
454 455 }
455 456 state = P_FILTER;
456 457 break;
457 458 case P_SCOPE:
458 459 if (*sptr == SEMITOK) {
459 460 /* no more SSD */
460 461 state = P_EXIT;
461 462 break;
462 463 }
463 464 if (strncasecmp(sptr, "base", 4) == 0) {
464 465 sptr += 4;
465 466 ptr->scope = NS_LDAP_SCOPE_BASE;
466 467 } else if (strncasecmp(sptr, "one", 3) == 0) {
467 468 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
468 469 sptr += 3;
469 470 } else if (strncasecmp(sptr, "sub", 3) == 0) {
470 471 ptr->scope = NS_LDAP_SCOPE_SUBTREE;
471 472 sptr += 3;
472 473 }
473 474 if (*sptr == '\0' || (*sptr == SEMITOK)) {
474 475 /* no more SSD */
475 476 state = P_EXIT;
476 477 sptr--;
477 478 break;
478 479 }
479 480 if (*sptr != QUESTTOK) {
480 481 state = P_ERROR;
481 482 break;
482 483 }
483 484 state = P_INIFILTER;
484 485 quoted = 0;
485 486 wasquoted = 0;
486 487 break;
487 488 case P_BASEDN:
488 489 case P_FILTER:
489 490 if (quoted) {
490 491 /* Quoted */
491 492 if (*sptr == BSLTOK) {
492 493 if (*(sptr+1) == '\0') {
493 494 state = P_ERROR;
494 495 break;
495 496 }
496 497 if (*(sptr+1) == QUOTETOK ||
497 498 *(sptr+1) == BSLTOK) {
498 499 /* escaped CHARS */
499 500 sptr++;
500 501 } else {
501 502 *dptr++ = *sptr++;
502 503 }
503 504 /* fall through to char copy */
504 505 } else if (*sptr == QUOTETOK) {
505 506 /* end of string */
506 507 *dptr = '\0';
507 508 quoted = 0;
508 509 break;
509 510 }
510 511 /* else fall through to char copy */
511 512 } else {
512 513 /* Unquoted */
513 514 if (wasquoted && *sptr != QUESTTOK) {
514 515 /* error past end of quoted string */
515 516 state = P_ERROR;
516 517 break;
517 518 }
518 519 if (*sptr == BSLTOK) {
519 520 if (*(sptr+1) == '\0') {
520 521 state = P_ERROR;
521 522 break;
522 523 }
523 524 if (*(sptr+1) == SEMITOK ||
524 525 *(sptr+1) == QUESTTOK ||
525 526 *(sptr+1) == QUOTETOK ||
526 527 *(sptr+1) == BSLTOK) {
527 528 /* escaped chars */
528 529 sptr++;
529 530 }
530 531 /* fall through to char copy */
531 532 } else if (*sptr == QUOTETOK) {
532 533 /* error */
533 534 state = P_ERROR;
534 535 break;
535 536 } else if (*sptr == QUESTTOK) {
536 537 /* if filter error */
537 538 if (state == P_FILTER) {
538 539 state = P_ERROR;
539 540 break;
540 541 }
541 542 /* end of basedn goto scope */
542 543 *dptr = '\0';
543 544 state = P_SCOPE;
544 545 break;
545 546 } else if (*sptr == SEMITOK) {
546 547 /* end of current SSD */
547 548 *dptr = '\0';
548 549 state = P_EXIT;
549 550 break;
550 551 }
551 552 }
552 553 /* normal character to copy */
553 554 *dptr++ = *sptr;
554 555 break;
555 556 case P_END:
556 557 if (*sptr == SEMITOK) {
557 558 state = P_EXIT;
558 559 break;
559 560 }
560 561 __ns_ldap_freeASearchDesc(ptr);
561 562 ptr = NULL;
562 563 *cur = NULL;
563 564 return (NS_LDAP_CONFIG);
564 565 default: /* error should never arrive here */
565 566 case P_ERROR:
566 567 __ns_ldap_freeASearchDesc(ptr);
567 568 ptr = NULL;
568 569 *cur = NULL;
569 570 return (NS_LDAP_CONFIG);
570 571 case P_MEMERR:
571 572 __ns_ldap_freeASearchDesc(ptr);
572 573 ptr = NULL;
573 574 *cur = NULL;
574 575 return (NS_LDAP_MEMORY);
575 576 }
576 577 }
577 578
578 579 if (quoted) {
579 580 __ns_ldap_freeASearchDesc(ptr);
580 581 ptr = NULL;
581 582 *cur = NULL;
582 583 return (NS_LDAP_INVALID_PARAM);
583 584 }
584 585
585 586 if (empty || strlen(ptr->basedn) == 0) {
586 587 if (ptr->basedn)
587 588 free(ptr->basedn);
588 589 /* get default base */
589 590 rc = __s_api_getDNs(&dns, service, &error);
590 591 if (rc != NS_LDAP_SUCCESS) {
591 592 if (dns) {
592 593 __s_api_free2dArray(dns);
593 594 dns = NULL;
594 595 }
595 596 (void) __ns_ldap_freeError(&error);
596 597 __ns_ldap_freeASearchDesc(ptr);
597 598 ptr = NULL;
598 599 return (NS_LDAP_MEMORY);
599 600 }
600 601 ptr->basedn = strdup(dns[0]);
601 602 __s_api_free2dArray(dns);
602 603 dns = NULL;
603 604 }
604 605
605 606 *cur = sptr;
606 607 *ret = ptr;
607 608 return (NS_LDAP_SUCCESS);
608 609 }
609 610
610 611
611 612 /*
612 613 * Build up the service descriptor array
613 614 */
614 615 #define NS_SDESC_MAX 4
615 616
616 617 static int
617 618 __ns_ldap_saveSearchDesc(ns_ldap_search_desc_t ***sdlist,
618 619 int *cnt, int *max, ns_ldap_search_desc_t *ret)
619 620 {
620 621 ns_ldap_search_desc_t **tmplist;
621 622
622 623 if (*sdlist == NULL) {
623 624 *cnt = 0;
624 625 *max = NS_SDESC_MAX;
625 626 *sdlist = (ns_ldap_search_desc_t **)
626 627 calloc(*max, sizeof (ns_ldap_search_desc_t *));
627 628 if (*sdlist == NULL)
628 629 return (-1);
629 630 } else if (*cnt+1 >= *max) {
630 631 *max += NS_SDESC_MAX;
631 632 tmplist = (ns_ldap_search_desc_t **)
632 633 realloc((void *)(*sdlist),
633 634 *max * sizeof (ns_ldap_search_desc_t *));
634 635 if (tmplist == NULL)
635 636 return (-1);
636 637 else
637 638 *sdlist = tmplist;
638 639 }
639 640 (*sdlist)[*cnt] = ret;
640 641 (*cnt)++;
641 642 (*sdlist)[*cnt] = NULL;
642 643 return (0);
643 644 }
644 645
645 646
646 647 /*
647 648 * Exported Search Descriptor Routines
648 649 */
649 650
650 651 int __ns_ldap_getSearchDescriptors(
651 652 const char *service,
652 653 ns_ldap_search_desc_t ***desc,
653 654 ns_ldap_error_t **errorp)
654 655 {
↓ open down ↓ |
346 lines elided |
↑ open up ↑ |
655 656 int rc;
656 657 int slen;
657 658 void **param = NULL;
658 659 void **paramVal = NULL;
659 660 char **sdl, *srv, **sdl_save;
660 661 char errstr[2 * MAXERROR];
661 662 ns_ldap_search_desc_t **sdlist;
662 663 int cnt, max;
663 664 int vers;
664 665 ns_config_t *cfg;
665 - ns_ldap_search_desc_t *ret;
666 + ns_ldap_search_desc_t *ret;
666 667
667 668 if ((desc == NULL) || (errorp == NULL))
668 669 return (NS_LDAP_INVALID_PARAM);
669 670
670 671 *desc = NULL;
671 672 *errorp = NULL;
672 673
673 674 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_SEARCH_DESC_P,
674 675 (void ***)¶m, errorp);
675 676 if (rc != NS_LDAP_SUCCESS) {
676 677 return (rc);
677 678 }
678 679 sdl = (char **)param;
679 680 cnt = 0;
680 681 max = 0;
681 682 sdlist = NULL;
682 683
683 684 cfg = __s_api_get_default_config();
684 685
685 686 if (cfg == NULL) {
686 687 (void) snprintf(errstr, sizeof (errstr),
687 688 gettext("No configuration information available."));
688 689 MKERROR(LOG_ERR, *errorp, NS_CONFIG_NOTLOADED, strdup(errstr),
689 690 NS_LDAP_MEMORY);
690 691 return (NS_LDAP_CONFIG);
691 692 }
692 693
693 694 vers = cfg->version;
694 695 __s_api_release_config(cfg);
695 696
696 697 /* If using version1 or no sd's process SEARCH_DN if available */
697 698 if (vers == NS_LDAP_V1 && param == NULL) {
698 699 rc = __s_api_get_search_DNs_v1(&sdl, service, errorp);
699 700 if (rc != NS_LDAP_SUCCESS || sdl == NULL) {
700 701 return (rc);
701 702 }
702 703 sdl_save = sdl;
703 704 /* Convert a SEARCH_DN to a search descriptor */
704 705 for (; *sdl; sdl++) {
705 706 ret = (ns_ldap_search_desc_t *)
706 707 calloc(1, sizeof (ns_ldap_search_desc_t));
707 708 if (ret == NULL) {
708 709 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
709 710 __s_api_free2dArray(sdl_save);
710 711 return (NS_LDAP_MEMORY);
711 712 }
712 713 ret->basedn = strdup(*sdl);
713 714 if (ret->basedn == NULL) {
714 715 free(ret);
715 716 (void) __ns_ldap_freeASearchDesc(ret);
716 717 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
717 718 __s_api_free2dArray(sdl_save);
718 719 return (NS_LDAP_MEMORY);
719 720 }
720 721
721 722 /* default scope */
722 723 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
723 724 ¶mVal, errorp)) != NS_LDAP_SUCCESS) {
724 725 (void) __ns_ldap_freeASearchDesc(ret);
725 726 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
726 727 __s_api_free2dArray(sdl_save);
727 728 return (rc);
728 729 }
729 730 if (paramVal && *paramVal)
730 731 ret->scope = * (ScopeType_t *)(*paramVal);
731 732 else
732 733 ret->scope = NS_LDAP_SCOPE_ONELEVEL;
733 734 (void) __ns_ldap_freeParam(¶mVal);
734 735 paramVal = NULL;
735 736
736 737 rc = __ns_ldap_saveSearchDesc(&sdlist, &cnt, &max, ret);
737 738 if (rc < 0) {
738 739 (void) __ns_ldap_freeASearchDesc(ret);
739 740 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
740 741 __s_api_free2dArray(sdl_save);
741 742 return (NS_LDAP_MEMORY);
742 743 }
743 744 }
744 745 __s_api_free2dArray(sdl_save);
745 746 *desc = sdlist;
746 747 return (NS_LDAP_SUCCESS);
747 748 }
748 749
749 750 if (sdl == NULL || service == NULL) {
750 751 (void) __ns_ldap_freeParam(¶m);
751 752 param = NULL;
752 753 *desc = NULL;
753 754 return (NS_LDAP_SUCCESS);
754 755 }
755 756 slen = strlen(service);
756 757
757 758 /* Process the version2 sd's */
758 759 for (; *sdl; sdl++) {
759 760 srv = *sdl;
760 761 if (strncasecmp(service, srv, slen) != 0)
761 762 continue;
762 763 srv += slen;
763 764 if (*srv != COLONTOK)
764 765 continue;
765 766 srv++;
766 767 while (srv != NULL && *srv != '\0') {
767 768 /* Process 1 */
768 769 rc = __s_api_parseASearchDesc(service, &srv, &ret);
769 770 if (rc != NS_LDAP_SUCCESS) {
770 771 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
771 772 (void) snprintf(errstr, (2 * MAXERROR), gettext(
772 773 "Invalid serviceSearchDescriptor (%s). "
773 774 "Illegal configuration"), *sdl);
774 775 (void) __ns_ldap_freeParam(¶m);
775 776 param = NULL;
776 777 MKERROR(LOG_ERR, *errorp, NS_CONFIG_SYNTAX,
777 778 strdup(errstr), NS_LDAP_MEMORY);
778 779 return (rc);
779 780 }
780 781 if (ret != NULL) {
781 782 rc = __ns_ldap_saveSearchDesc(
782 783 &sdlist, &cnt, &max, ret);
783 784 }
784 785 if (rc < 0) {
785 786 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
786 787 (void) __ns_ldap_freeParam(¶m);
787 788 param = NULL;
788 789 return (NS_LDAP_MEMORY);
789 790 }
790 791 }
791 792 }
792 793
793 794 (void) __ns_ldap_freeParam(¶m);
794 795 param = NULL;
795 796 *desc = sdlist;
796 797 return (NS_LDAP_SUCCESS);
797 798 }
798 799
799 800 int
800 801 __ns_ldap_freeSearchDescriptors(ns_ldap_search_desc_t ***desc)
801 802 {
802 803 ns_ldap_search_desc_t **dptr;
803 804 ns_ldap_search_desc_t *ptr;
804 805
805 806 if (*desc == NULL)
806 807 return (NS_LDAP_SUCCESS);
807 808 for (dptr = *desc; (ptr = *dptr) != NULL; dptr++) {
808 809 __ns_ldap_freeASearchDesc(ptr);
809 810 }
810 811 free(*desc);
811 812 *desc = NULL;
812 813
813 814 return (NS_LDAP_SUCCESS);
814 815 }
815 816
816 817
817 818
818 819
819 820 /*
820 821 * Exported Attribute/Objectclass mapping functions.
821 822 */
822 823
823 824 /*
824 825 * This function is not supported.
825 826 */
826 827 /* ARGSUSED */
827 828 int __ns_ldap_getAttributeMaps(
828 829 const char *service,
829 830 ns_ldap_attribute_map_t ***maps,
830 831 ns_ldap_error_t **errorp)
831 832 {
832 833 *maps = NULL;
833 834 return (NS_LDAP_OP_FAILED);
834 835 }
835 836
836 837 int
837 838 __ns_ldap_freeAttributeMaps(ns_ldap_attribute_map_t ***maps)
838 839 {
839 840 ns_ldap_attribute_map_t **dptr;
840 841 ns_ldap_attribute_map_t *ptr;
841 842 char **cpp, *cp;
842 843
843 844 if (*maps == NULL)
844 845 return (NS_LDAP_SUCCESS);
845 846 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
846 847 if (ptr->origAttr) {
847 848 free(ptr->origAttr);
848 849 ptr->origAttr = NULL;
849 850 }
850 851 if (ptr->mappedAttr) {
851 852 for (cpp = ptr->mappedAttr; (cp = *cpp) != NULL; cpp++)
852 853 free(cp);
853 854 free(ptr->mappedAttr);
854 855 ptr->mappedAttr = NULL;
855 856 }
856 857 free(ptr);
857 858 }
858 859 free(*maps);
859 860 *maps = NULL;
860 861
861 862 return (NS_LDAP_SUCCESS);
862 863 }
863 864
864 865 char **__ns_ldap_getMappedAttributes(
865 866 const char *service,
866 867 const char *origAttribute)
867 868 {
868 869 ns_config_t *ptr = __s_api_loadrefresh_config();
869 870 ns_hash_t *hp;
870 871 char **ret;
871 872
872 873 if (ptr == NULL)
873 874 return (NULL);
874 875
875 876 hp = ns_get_hash(ptr, NS_HASH_AMAP, service, origAttribute);
876 877
877 878 if (hp == NULL || hp->h_map == NULL)
878 879 ret = NULL;
879 880 else
880 881 ret = __s_api_cp2dArray(hp->h_map->map);
881 882 __s_api_release_config(ptr);
882 883 return (ret);
883 884 }
884 885
885 886 char **__ns_ldap_getOrigAttribute(
886 887 const char *service,
887 888 const char *mappedAttribute)
888 889 {
889 890 ns_config_t *ptr = __s_api_loadrefresh_config();
890 891 ns_hash_t *hp;
891 892 char **ret;
892 893
893 894 if (ptr == NULL)
894 895 return (NULL);
895 896
896 897 hp = ns_get_hash(ptr, NS_HASH_RAMAP, service, mappedAttribute);
897 898
898 899 if (hp == NULL || hp->h_map == NULL)
899 900 ret = NULL;
900 901 else
901 902 ret = __s_api_cp2dArray(hp->h_map->map);
902 903 __s_api_release_config(ptr);
903 904 return (ret);
904 905 }
905 906
906 907 /*
907 908 * This function is not supported.
908 909 */
909 910 /* ARGSUSED */
910 911 int __ns_ldap_getObjectClassMaps(
911 912 const char *service,
912 913 ns_ldap_objectclass_map_t ***maps,
913 914 ns_ldap_error_t **errorp)
914 915 {
915 916 *maps = NULL;
916 917 return (NS_LDAP_OP_FAILED);
917 918 }
918 919
919 920 int
920 921 __ns_ldap_freeObjectClassMaps(ns_ldap_objectclass_map_t ***maps)
921 922 {
922 923 ns_ldap_objectclass_map_t **dptr;
923 924 ns_ldap_objectclass_map_t *ptr;
924 925
925 926 if (*maps == NULL)
926 927 return (NS_LDAP_SUCCESS);
927 928 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
928 929 if (ptr->origOC) {
929 930 free(ptr->origOC);
930 931 ptr->origOC = NULL;
931 932 }
932 933 if (ptr->mappedOC) {
933 934 free(ptr->mappedOC);
934 935 ptr->mappedOC = NULL;
935 936 }
936 937 free(ptr);
937 938 }
938 939 free(*maps);
939 940 *maps = NULL;
940 941
941 942 return (NS_LDAP_SUCCESS);
942 943 }
943 944
944 945 char **__ns_ldap_getMappedObjectClass(
945 946 const char *service,
946 947 const char *origObjectClass)
947 948 {
948 949 ns_config_t *ptr = __s_api_loadrefresh_config();
949 950 ns_hash_t *hp;
950 951 char **ret;
951 952
952 953 if (ptr == NULL)
953 954 return (NULL);
954 955
955 956 hp = ns_get_hash(ptr, NS_HASH_OMAP, service, origObjectClass);
956 957
957 958 if (hp == NULL || hp->h_map == NULL)
958 959 ret = NULL;
959 960 else
960 961 ret = __s_api_cp2dArray(hp->h_map->map);
961 962 __s_api_release_config(ptr);
962 963 return (ret);
963 964 }
964 965
965 966 char **__ns_ldap_getOrigObjectClass(
966 967 const char *service,
967 968 const char *mappedObjectClass)
968 969 {
969 970 ns_config_t *ptr = __s_api_loadrefresh_config();
970 971 ns_hash_t *hp;
971 972 char **ret;
972 973
973 974 if (ptr == NULL)
974 975 return (NULL);
975 976
976 977 hp = ns_get_hash(ptr, NS_HASH_ROMAP, service, mappedObjectClass);
977 978
978 979 if (hp == NULL || hp->h_map == NULL)
979 980 ret = NULL;
980 981 else
981 982 ret = __s_api_cp2dArray(hp->h_map->map);
982 983 __s_api_release_config(ptr);
983 984 return (ret);
984 985 }
985 986
986 987 char **__ns_ldap_mapAttributeList(
987 988 const char *service,
988 989 const char * const *origAttrList)
989 990 {
990 991 const char * const *opp;
991 992 char **cpp, **npp;
992 993 int i;
993 994
994 995 if (origAttrList == NULL)
995 996 return (NULL);
996 997
997 998 opp = origAttrList;
998 999 for (i = 0; *opp; i++, opp++)
999 1000 ;
1000 1001 cpp = (char **)calloc(i+1, sizeof (char *));
1001 1002 if (cpp == NULL)
1002 1003 return (NULL);
1003 1004
1004 1005 opp = origAttrList;
1005 1006 for (i = 0; *opp; i++, opp++) {
1006 1007 npp = __ns_ldap_getMappedAttributes(service, *opp);
1007 1008 if (npp && npp[0]) {
1008 1009 cpp[i] = strdup(npp[0]);
1009 1010 __s_api_free2dArray(npp);
1010 1011 npp = NULL;
1011 1012 if (cpp[i] == NULL) {
1012 1013 __s_api_free2dArray(cpp);
1013 1014 return (NULL);
1014 1015 }
1015 1016 } else {
1016 1017 cpp[i] = strdup(*opp);
1017 1018 if (cpp[i] == NULL) {
1018 1019 __s_api_free2dArray(cpp);
1019 1020 return (NULL);
1020 1021 }
1021 1022 }
1022 1023 }
1023 1024 return (cpp);
1024 1025 }
1025 1026
1026 1027 char *
1027 1028 __ns_ldap_mapAttribute(
1028 1029 const char *service,
1029 1030 const char *origAttr)
1030 1031 {
1031 1032 char **npp;
1032 1033 char *mappedAttr;
1033 1034
1034 1035 if (origAttr == NULL)
1035 1036 return (NULL);
1036 1037
1037 1038 npp = __ns_ldap_getMappedAttributes(service, origAttr);
1038 1039 if (npp && npp[0]) {
1039 1040 mappedAttr = strdup(npp[0]);
1040 1041 __s_api_free2dArray(npp);
1041 1042 } else {
1042 1043 mappedAttr = strdup(origAttr);
1043 1044 }
1044 1045 return (mappedAttr);
1045 1046 }
↓ open down ↓ |
370 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX