8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsldap / common / ns_mapping.c
blob1ceb035941cd39f5f07c714ee53c0123ab7e5377
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <stdlib.h>
26 #include <strings.h>
27 #include <ctype.h>
28 #include <locale.h>
29 #include <syslog.h>
30 #include "ns_internal.h"
33 * Calculate a hash for a string
34 * Based on elf_hash algorithm, hash is case insensitive
35 * Uses tolower instead of _tolower because of I18N
38 static unsigned long
39 ns_hash(const char *str)
41 unsigned int hval = 0;
43 while (*str) {
44 unsigned int g;
46 hval = (hval << 4) + tolower(*str++);
47 if ((g = (hval & 0xf0000000)) != 0)
48 hval ^= g >> 24;
49 hval &= ~g;
51 return ((unsigned long)hval);
55 * Scan a hash table hit for a matching hash entry.
56 * Assume service and str are non-NULL.
59 static ns_hash_t *
60 ns_scan_hash(ns_hashtype_t type, const char *service,
61 const char *str, ns_hash_t *idx)
63 while (idx) {
64 if (idx->h_type == type &&
65 strcasecmp(service, idx->h_map->service) == 0 &&
66 strcasecmp(str, idx->h_map->orig) == 0) {
67 return (idx);
69 idx = idx->h_next;
71 return ((ns_hash_t *)NULL);
75 * Find an entry in the hash table
78 static ns_hash_t *
79 ns_get_hash(const ns_config_t *config,
80 ns_hashtype_t type, const char *service, const char *str)
82 ns_hash_t *idx, *hashp;
83 unsigned long hash;
85 if (config == NULL || service == NULL || str == NULL)
86 return (NULL);
88 hash = ns_hash(str) % NS_HASH_MAX;
89 idx = config->hashTbl[hash];
90 hashp = ns_scan_hash(type, service, str, idx);
92 return (hashp);
96 * free a map entry
99 static void
100 ns_free_map(ns_mapping_t *mapp)
102 char **ptr;
104 if (mapp == NULL)
105 return;
106 if (mapp->service) {
107 free(mapp->service);
108 mapp->service = NULL;
110 if (mapp->orig) {
111 free(mapp->orig);
112 mapp->orig = NULL;
114 if (mapp->map) {
115 for (ptr = mapp->map; *ptr; ptr++)
116 free(*ptr);
117 free(mapp->map);
118 mapp->map = NULL;
120 free(mapp);
124 * Remove a hash table entry.
125 * This function is not MT safe.
128 static ns_hash_t *
129 ns_free_hash(ns_hash_t *p)
131 ns_mapping_t *map;
132 ns_hash_t *next;
134 map = p->h_map;
135 next = p->h_next;
136 ns_free_map(map);
137 free(p);
138 return (next);
142 * destroy the hash table.
143 * This function is not MT safe.
146 void
147 __s_api_destroy_hash(ns_config_t *config)
149 ns_hash_t *next;
150 int i;
152 if (config == NULL)
153 return;
154 for (i = 0; i < NS_HASH_MAX; i++) {
155 next = config->hashTbl[i];
156 while (next != NULL) {
157 next = ns_free_hash(next);
159 config->hashTbl[i] = NULL;
164 * Add a hash entry to the hash table.
165 * This function is not MT safe.
166 * Assume map, map->orig, map->service are non-NULL.
170 __s_api_add_map2hash(ns_config_t *config, ns_hashtype_t type,
171 ns_mapping_t *map)
173 ns_hash_t *idx, *newp;
174 unsigned long hash;
176 if (config == NULL)
177 return (NS_HASH_RC_CONFIG_ERROR);
179 hash = ns_hash(map->orig) % NS_HASH_MAX;
180 idx = config->hashTbl[hash];
181 if (idx != NULL &&
182 ns_scan_hash(type, map->service, map->orig, idx) != NULL) {
183 return (NS_HASH_RC_EXISTED);
186 newp = (ns_hash_t *)malloc(sizeof (ns_hash_t));
187 if (newp == NULL)
188 return (NS_HASH_RC_NO_MEMORY);
189 newp->h_type = type;
190 newp->h_map = map;
191 newp->h_next = idx;
192 config->hashTbl[hash] = newp;
193 newp->h_llnext = config->llHead;
194 config->llHead = newp;
195 return (NS_HASH_RC_SUCCESS);
200 * Parse an attribute map string.
201 * Assume space is the only legal whitespace.
202 * attributeMap syntax:
203 * attributeMap = serviceId ":" origAttribute "="
204 * attributes
205 * origAttribute = attribute
206 * attributes = wattribute *( space wattribute )
207 * wattribute = whsp newAttribute whsp
208 * newAttribute = descr | "*NULL*"
209 * attribute = descr
211 * objectclassMap syntax:
212 * objectclassMap = serviceId ":" origObjectclass "="
213 * objectclass
214 * origObjectclass = objectclass
215 * objectclass = keystring
219 __s_api_parse_map(char *cp, char **sid, char **origA, char ***mapA)
221 char *sptr, *dptr, **mapp;
222 int i, max;
224 *sid = NULL;
225 *origA = NULL;
226 *mapA = NULL;
228 sptr = cp;
229 dptr = strchr(sptr, COLONTOK);
230 if (dptr == NULL)
231 return (NS_HASH_RC_SYNTAX_ERROR);
232 i = dptr - sptr + 1;
233 *sid = (char *)malloc(i);
234 if (*sid == NULL)
235 return (NS_HASH_RC_NO_MEMORY);
236 (void) strlcpy(*sid, sptr, i);
237 sptr = dptr+1;
239 dptr = strchr(sptr, TOKENSEPARATOR);
240 if (dptr == NULL) {
241 free(*sid);
242 *sid = NULL;
243 return (NS_HASH_RC_SYNTAX_ERROR);
245 i = dptr - sptr + 1;
246 *origA = (char *)malloc(i);
247 if (*origA == NULL) {
248 free(*sid);
249 *sid = NULL;
250 return (NS_HASH_RC_NO_MEMORY);
252 (void) strlcpy(*origA, sptr, i);
253 sptr = dptr+1;
255 max = 1;
256 for (dptr = sptr; *dptr; dptr++) {
257 if (*dptr == SPACETOK) {
258 max++;
259 while (*(dptr+1) == SPACETOK)
260 dptr++;
263 *mapA = (char **)calloc(max+1, sizeof (char *));
264 if (*mapA == NULL) {
265 free(*sid);
266 *sid = NULL;
267 free(*origA);
268 *origA = NULL;
269 return (NS_HASH_RC_NO_MEMORY);
271 mapp = *mapA;
273 while (*sptr) {
274 while (*sptr == SPACETOK)
275 sptr++;
276 dptr = sptr;
277 while (*dptr && *dptr != SPACETOK)
278 dptr++;
279 i = dptr - sptr + 1;
280 *mapp = (char *)malloc(i);
281 if (*mapp == NULL) {
282 free(*sid);
283 *sid = NULL;
284 free(*origA);
285 *origA = NULL;
286 __s_api_free2dArray(*mapA);
287 *mapA = NULL;
288 return (NS_HASH_RC_NO_MEMORY);
290 (void) strlcpy(*mapp, sptr, i);
291 mapp++;
292 sptr = dptr;
294 return (NS_HASH_RC_SUCCESS);
298 static void
299 __ns_ldap_freeASearchDesc(ns_ldap_search_desc_t *ptr)
301 if (ptr == NULL)
302 return;
303 if (ptr->basedn)
304 free(ptr->basedn);
305 if (ptr->filter)
306 free(ptr->filter);
307 free(ptr);
311 * Parse a service descriptor
312 * and create a service descriptor struct
313 * SD Format:
314 * serviceid:[base][?[scope][?[filter]]];[[base][?[scope][?[filter]]]]
315 * desc format:
316 * [base][?[scope][?[filter]]]
319 typedef enum _ns_parse_state {
320 P_ERROR, P_INIT, P_BASEDN, P_SCOPE,
321 P_INIFILTER, P_FILTER, P_END, P_EXIT, P_MEMERR
322 } _ns_parse_state_t;
324 static
326 __s_api_parseASearchDesc(const char *service,
327 char **cur, ns_ldap_search_desc_t **ret)
329 ns_ldap_search_desc_t *ptr;
330 char *sptr, *dptr;
331 char buf[BUFSIZ];
332 int i, rc;
333 ns_ldap_error_t **errorp = NULL;
334 ns_ldap_error_t *error = NULL;
335 void **paramVal = NULL;
336 char **dns = NULL;
337 _ns_parse_state_t state = P_INIT;
338 int quoted = 0;
339 int wasquoted = 0;
340 int empty = 1;
342 if (ret == NULL)
343 return (NS_LDAP_INVALID_PARAM);
344 *ret = NULL;
345 if (cur == NULL)
346 return (NS_LDAP_INVALID_PARAM);
348 ptr = (ns_ldap_search_desc_t *)
349 calloc(1, sizeof (ns_ldap_search_desc_t));
350 if (ptr == NULL)
351 return (NS_LDAP_MEMORY);
353 sptr = *cur;
355 /* Get the default scope */
356 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
357 &paramVal, errorp)) != NS_LDAP_SUCCESS) {
358 (void) __ns_ldap_freeError(errorp);
359 __ns_ldap_freeASearchDesc(ptr);
360 ptr = NULL;
361 return (NS_LDAP_MEMORY);
363 if (paramVal && *paramVal)
364 ptr->scope = * (ScopeType_t *)(*paramVal);
365 else
366 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
367 (void) __ns_ldap_freeParam(&paramVal);
368 paramVal = NULL;
370 for (/* none */; state != P_EXIT && sptr && *sptr; sptr++) {
371 empty = 0;
372 switch (state) {
373 case P_INIT:
374 if (*sptr == QUESTTOK) {
375 /* No basedn */
376 ptr->basedn = strdup("");
377 if (!ptr->basedn) {
378 state = P_MEMERR;
379 break;
381 state = P_SCOPE;
382 break;
384 if (*sptr == SEMITOK) {
385 /* No SSD */
386 ptr->basedn = strdup("");
387 if (!ptr->basedn) {
388 state = P_MEMERR;
389 break;
391 state = P_EXIT;
392 break;
394 /* prepare to copy DN */
395 i = strlen(sptr) + 1;
396 ptr->basedn = dptr = (char *)calloc(i, sizeof (char));
397 if (!ptr->basedn) {
398 state = P_MEMERR;
399 break;
401 if (*sptr == BSLTOK) {
402 if (*(sptr+1) == '\0') {
403 /* error */
404 state = P_ERROR;
405 break;
407 if (*(sptr+1) == QUOTETOK ||
408 *(sptr+1) == BSLTOK) {
409 /* escaped CHARS */
410 sptr++;
411 } else {
412 *dptr++ = *sptr++;
414 *dptr++ = *sptr;
415 } else if (*sptr == QUOTETOK) {
416 quoted = 1;
417 wasquoted = 1;
418 } else {
419 *dptr++ = *sptr;
421 state = P_BASEDN;
422 break;
423 case P_INIFILTER:
424 if (*sptr == SEMITOK) {
425 /* No filter and no more SSD */
426 state = P_EXIT;
427 break;
429 /* prepare to copy DN */
430 i = strlen(sptr) + 1;
431 ptr->filter = dptr = (char *)calloc(i, sizeof (char));
432 if (!ptr->filter) {
433 state = P_MEMERR;
434 break;
436 if (*sptr == BSLTOK) {
437 if (*(sptr+1) == '\0') {
438 /* error */
439 state = P_ERROR;
440 break;
442 if (*(sptr+1) == QUOTETOK ||
443 *(sptr+1) == BSLTOK) {
444 /* escaped CHARS */
445 sptr++;
446 } else {
447 *dptr++ = *sptr++;
449 *dptr++ = *sptr;
450 } else if (*sptr == QUOTETOK) {
451 quoted = 1;
452 wasquoted = 1;
453 } else {
454 *dptr++ = *sptr;
456 state = P_FILTER;
457 break;
458 case P_SCOPE:
459 buf[0] = '\0';
460 if (*sptr == SEMITOK) {
461 /* no more SSD */
462 state = P_EXIT;
463 break;
465 if (strncasecmp(sptr, "base", 4) == 0) {
466 sptr += 4;
467 ptr->scope = NS_LDAP_SCOPE_BASE;
468 } else if (strncasecmp(sptr, "one", 3) == 0) {
469 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
470 sptr += 3;
471 } else if (strncasecmp(sptr, "sub", 3) == 0) {
472 ptr->scope = NS_LDAP_SCOPE_SUBTREE;
473 sptr += 3;
475 if (*sptr == '\0' || (*sptr == SEMITOK)) {
476 /* no more SSD */
477 state = P_EXIT;
478 sptr--;
479 break;
481 if (*sptr != QUESTTOK) {
482 state = P_ERROR;
483 break;
485 state = P_INIFILTER;
486 quoted = 0;
487 wasquoted = 0;
488 break;
489 case P_BASEDN:
490 case P_FILTER:
491 if (quoted) {
492 /* Quoted */
493 if (*sptr == BSLTOK) {
494 if (*(sptr+1) == '\0') {
495 state = P_ERROR;
496 break;
498 if (*(sptr+1) == QUOTETOK ||
499 *(sptr+1) == BSLTOK) {
500 /* escaped CHARS */
501 sptr++;
502 } else {
503 *dptr++ = *sptr++;
505 /* fall through to char copy */
506 } else if (*sptr == QUOTETOK) {
507 /* end of string */
508 *dptr = '\0';
509 quoted = 0;
510 break;
512 /* else fall through to char copy */
513 } else {
514 /* Unquoted */
515 if (wasquoted && *sptr != QUESTTOK) {
516 /* error past end of quoted string */
517 state = P_ERROR;
518 break;
520 if (*sptr == BSLTOK) {
521 if (*(sptr+1) == '\0') {
522 state = P_ERROR;
523 break;
525 if (*(sptr+1) == SEMITOK ||
526 *(sptr+1) == QUESTTOK ||
527 *(sptr+1) == QUOTETOK ||
528 *(sptr+1) == BSLTOK) {
529 /* escaped chars */
530 sptr++;
532 /* fall through to char copy */
533 } else if (*sptr == QUOTETOK) {
534 /* error */
535 state = P_ERROR;
536 break;
537 } else if (*sptr == QUESTTOK) {
538 /* if filter error */
539 if (state == P_FILTER) {
540 state = P_ERROR;
541 break;
543 /* end of basedn goto scope */
544 *dptr = '\0';
545 state = P_SCOPE;
546 break;
547 } else if (*sptr == SEMITOK) {
548 /* end of current SSD */
549 *dptr = '\0';
550 state = P_EXIT;
551 break;
554 /* normal character to copy */
555 *dptr++ = *sptr;
556 break;
557 case P_END:
558 if (*sptr == SEMITOK) {
559 state = P_EXIT;
560 break;
562 __ns_ldap_freeASearchDesc(ptr);
563 ptr = NULL;
564 *cur = NULL;
565 return (NS_LDAP_CONFIG);
566 default: /* error should never arrive here */
567 case P_ERROR:
568 __ns_ldap_freeASearchDesc(ptr);
569 ptr = NULL;
570 *cur = NULL;
571 return (NS_LDAP_CONFIG);
572 case P_MEMERR:
573 __ns_ldap_freeASearchDesc(ptr);
574 ptr = NULL;
575 *cur = NULL;
576 return (NS_LDAP_MEMORY);
580 if (quoted) {
581 __ns_ldap_freeASearchDesc(ptr);
582 ptr = NULL;
583 *cur = NULL;
584 return (NS_LDAP_INVALID_PARAM);
587 if (empty || strlen(ptr->basedn) == 0) {
588 if (ptr->basedn)
589 free(ptr->basedn);
590 /* get default base */
591 rc = __s_api_getDNs(&dns, service, &error);
592 if (rc != NS_LDAP_SUCCESS) {
593 if (dns) {
594 __s_api_free2dArray(dns);
595 dns = NULL;
597 (void) __ns_ldap_freeError(&error);
598 __ns_ldap_freeASearchDesc(ptr);
599 ptr = NULL;
600 return (NS_LDAP_MEMORY);
602 ptr->basedn = strdup(dns[0]);
603 __s_api_free2dArray(dns);
604 dns = NULL;
607 *cur = sptr;
608 *ret = ptr;
609 return (NS_LDAP_SUCCESS);
614 * Build up the service descriptor array
616 #define NS_SDESC_MAX 4
618 static int
619 __ns_ldap_saveSearchDesc(ns_ldap_search_desc_t ***sdlist,
620 int *cnt, int *max, ns_ldap_search_desc_t *ret)
622 ns_ldap_search_desc_t **tmplist;
624 if (*sdlist == NULL) {
625 *cnt = 0;
626 *max = NS_SDESC_MAX;
627 *sdlist = (ns_ldap_search_desc_t **)
628 calloc(*max, sizeof (ns_ldap_search_desc_t *));
629 if (*sdlist == NULL)
630 return (-1);
631 } else if (*cnt+1 >= *max) {
632 *max += NS_SDESC_MAX;
633 tmplist = (ns_ldap_search_desc_t **)
634 realloc((void *)(*sdlist),
635 *max * sizeof (ns_ldap_search_desc_t *));
636 if (tmplist == NULL)
637 return (-1);
638 else
639 *sdlist = tmplist;
641 (*sdlist)[*cnt] = ret;
642 (*cnt)++;
643 (*sdlist)[*cnt] = NULL;
644 return (0);
649 * Exported Search Descriptor Routines
652 int __ns_ldap_getSearchDescriptors(
653 const char *service,
654 ns_ldap_search_desc_t ***desc,
655 ns_ldap_error_t **errorp)
657 int rc;
658 int slen;
659 void **param = NULL;
660 void **paramVal = NULL;
661 char **sdl, *srv, **sdl_save;
662 char errstr[2 * MAXERROR];
663 ns_ldap_search_desc_t **sdlist;
664 int cnt, max;
665 int vers;
666 ns_config_t *cfg;
667 ns_ldap_search_desc_t *ret;
669 if ((desc == NULL) || (errorp == NULL))
670 return (NS_LDAP_INVALID_PARAM);
672 *desc = NULL;
673 *errorp = NULL;
675 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_SEARCH_DESC_P,
676 (void ***)&param, errorp);
677 if (rc != NS_LDAP_SUCCESS) {
678 return (rc);
680 sdl = (char **)param;
681 cnt = 0;
682 max = 0;
683 sdlist = NULL;
685 cfg = __s_api_get_default_config();
687 if (cfg == NULL) {
688 (void) snprintf(errstr, sizeof (errstr),
689 gettext("No configuration information available."));
690 MKERROR(LOG_ERR, *errorp, NS_CONFIG_NOTLOADED, strdup(errstr),
691 NULL);
692 return (NS_LDAP_CONFIG);
695 vers = cfg->version;
696 __s_api_release_config(cfg);
698 /* If using version1 or no sd's process SEARCH_DN if available */
699 if (vers == NS_LDAP_V1 && param == NULL) {
700 rc = __s_api_get_search_DNs_v1(&sdl, service, errorp);
701 if (rc != NS_LDAP_SUCCESS || sdl == NULL) {
702 return (rc);
704 sdl_save = sdl;
705 /* Convert a SEARCH_DN to a search descriptor */
706 for (; *sdl; sdl++) {
707 ret = (ns_ldap_search_desc_t *)
708 calloc(1, sizeof (ns_ldap_search_desc_t));
709 if (ret == NULL) {
710 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
711 __s_api_free2dArray(sdl_save);
712 return (NS_LDAP_MEMORY);
714 ret->basedn = strdup(*sdl);
715 if (ret->basedn == NULL) {
716 free(ret);
717 (void) __ns_ldap_freeASearchDesc(ret);
718 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
719 __s_api_free2dArray(sdl_save);
720 return (NS_LDAP_MEMORY);
723 /* default scope */
724 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
725 &paramVal, errorp)) != NS_LDAP_SUCCESS) {
726 (void) __ns_ldap_freeASearchDesc(ret);
727 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
728 __s_api_free2dArray(sdl_save);
729 return (rc);
731 if (paramVal && *paramVal)
732 ret->scope = * (ScopeType_t *)(*paramVal);
733 else
734 ret->scope = NS_LDAP_SCOPE_ONELEVEL;
735 (void) __ns_ldap_freeParam(&paramVal);
736 paramVal = NULL;
738 rc = __ns_ldap_saveSearchDesc(&sdlist, &cnt, &max, ret);
739 if (rc < 0) {
740 (void) __ns_ldap_freeASearchDesc(ret);
741 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
742 __s_api_free2dArray(sdl_save);
743 return (NS_LDAP_MEMORY);
746 __s_api_free2dArray(sdl_save);
747 *desc = sdlist;
748 return (NS_LDAP_SUCCESS);
751 if (sdl == NULL || service == NULL) {
752 (void) __ns_ldap_freeParam(&param);
753 param = NULL;
754 *desc = NULL;
755 return (NS_LDAP_SUCCESS);
757 slen = strlen(service);
759 /* Process the version2 sd's */
760 for (; *sdl; sdl++) {
761 srv = *sdl;
762 if (strncasecmp(service, srv, slen) != 0)
763 continue;
764 srv += slen;
765 if (*srv != COLONTOK)
766 continue;
767 srv++;
768 while (srv != NULL && *srv != NULL) {
769 /* Process 1 */
770 rc = __s_api_parseASearchDesc(service, &srv, &ret);
771 if (rc != NS_LDAP_SUCCESS) {
772 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
773 (void) snprintf(errstr, (2 * MAXERROR), gettext(
774 "Invalid serviceSearchDescriptor (%s). "
775 "Illegal configuration"), *sdl);
776 (void) __ns_ldap_freeParam(&param);
777 param = NULL;
778 MKERROR(LOG_ERR, *errorp, NS_CONFIG_SYNTAX,
779 strdup(errstr), NULL);
780 return (rc);
782 if (ret != NULL) {
783 rc = __ns_ldap_saveSearchDesc(
784 &sdlist, &cnt, &max, ret);
786 if (rc < 0) {
787 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
788 (void) __ns_ldap_freeParam(&param);
789 param = NULL;
790 return (NS_LDAP_MEMORY);
795 (void) __ns_ldap_freeParam(&param);
796 param = NULL;
797 *desc = sdlist;
798 return (NS_LDAP_SUCCESS);
802 __ns_ldap_freeSearchDescriptors(ns_ldap_search_desc_t ***desc)
804 ns_ldap_search_desc_t **dptr;
805 ns_ldap_search_desc_t *ptr;
807 if (*desc == NULL)
808 return (NS_LDAP_SUCCESS);
809 for (dptr = *desc; (ptr = *dptr) != NULL; dptr++) {
810 __ns_ldap_freeASearchDesc(ptr);
812 free(*desc);
813 *desc = NULL;
815 return (NS_LDAP_SUCCESS);
822 * Exported Attribute/Objectclass mapping functions.
826 * This function is not supported.
828 /* ARGSUSED */
829 int __ns_ldap_getAttributeMaps(
830 const char *service,
831 ns_ldap_attribute_map_t ***maps,
832 ns_ldap_error_t **errorp)
834 *maps = NULL;
835 return (NS_LDAP_OP_FAILED);
839 __ns_ldap_freeAttributeMaps(ns_ldap_attribute_map_t ***maps)
841 ns_ldap_attribute_map_t **dptr;
842 ns_ldap_attribute_map_t *ptr;
843 char **cpp, *cp;
845 if (*maps == NULL)
846 return (NS_LDAP_SUCCESS);
847 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
848 if (ptr->origAttr) {
849 free(ptr->origAttr);
850 ptr->origAttr = NULL;
852 if (ptr->mappedAttr) {
853 for (cpp = ptr->mappedAttr; (cp = *cpp) != NULL; cpp++)
854 free(cp);
855 free(ptr->mappedAttr);
856 ptr->mappedAttr = NULL;
858 free(ptr);
860 free(*maps);
861 *maps = NULL;
863 return (NS_LDAP_SUCCESS);
866 char **__ns_ldap_getMappedAttributes(
867 const char *service,
868 const char *origAttribute)
870 ns_config_t *ptr = __s_api_loadrefresh_config();
871 ns_hash_t *hp;
872 char **ret;
874 if (ptr == NULL)
875 return (NULL);
877 hp = ns_get_hash(ptr, NS_HASH_AMAP, service, origAttribute);
879 if (hp == NULL || hp->h_map == NULL)
880 ret = NULL;
881 else
882 ret = __s_api_cp2dArray(hp->h_map->map);
883 __s_api_release_config(ptr);
884 return (ret);
887 char **__ns_ldap_getOrigAttribute(
888 const char *service,
889 const char *mappedAttribute)
891 ns_config_t *ptr = __s_api_loadrefresh_config();
892 ns_hash_t *hp;
893 char **ret;
895 if (ptr == NULL)
896 return (NULL);
898 hp = ns_get_hash(ptr, NS_HASH_RAMAP, service, mappedAttribute);
900 if (hp == NULL || hp->h_map == NULL)
901 ret = NULL;
902 else
903 ret = __s_api_cp2dArray(hp->h_map->map);
904 __s_api_release_config(ptr);
905 return (ret);
909 * This function is not supported.
911 /* ARGSUSED */
912 int __ns_ldap_getObjectClassMaps(
913 const char *service,
914 ns_ldap_objectclass_map_t ***maps,
915 ns_ldap_error_t **errorp)
917 *maps = NULL;
918 return (NS_LDAP_OP_FAILED);
922 __ns_ldap_freeObjectClassMaps(ns_ldap_objectclass_map_t ***maps)
924 ns_ldap_objectclass_map_t **dptr;
925 ns_ldap_objectclass_map_t *ptr;
927 if (*maps == NULL)
928 return (NS_LDAP_SUCCESS);
929 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
930 if (ptr->origOC) {
931 free(ptr->origOC);
932 ptr->origOC = NULL;
934 if (ptr->mappedOC) {
935 free(ptr->mappedOC);
936 ptr->mappedOC = NULL;
938 free(ptr);
940 free(*maps);
941 *maps = NULL;
943 return (NS_LDAP_SUCCESS);
946 char **__ns_ldap_getMappedObjectClass(
947 const char *service,
948 const char *origObjectClass)
950 ns_config_t *ptr = __s_api_loadrefresh_config();
951 ns_hash_t *hp;
952 char **ret;
954 if (ptr == NULL)
955 return (NULL);
957 hp = ns_get_hash(ptr, NS_HASH_OMAP, service, origObjectClass);
959 if (hp == NULL || hp->h_map == NULL)
960 ret = NULL;
961 else
962 ret = __s_api_cp2dArray(hp->h_map->map);
963 __s_api_release_config(ptr);
964 return (ret);
967 char **__ns_ldap_getOrigObjectClass(
968 const char *service,
969 const char *mappedObjectClass)
971 ns_config_t *ptr = __s_api_loadrefresh_config();
972 ns_hash_t *hp;
973 char **ret;
975 if (ptr == NULL)
976 return (NULL);
978 hp = ns_get_hash(ptr, NS_HASH_ROMAP, service, mappedObjectClass);
980 if (hp == NULL || hp->h_map == NULL)
981 ret = NULL;
982 else
983 ret = __s_api_cp2dArray(hp->h_map->map);
984 __s_api_release_config(ptr);
985 return (ret);
988 char **__ns_ldap_mapAttributeList(
989 const char *service,
990 const char * const *origAttrList)
992 const char * const *opp;
993 char **cpp, **npp;
994 int i;
996 if (origAttrList == NULL)
997 return (NULL);
999 opp = origAttrList;
1000 for (i = 0; *opp; i++, opp++)
1002 cpp = (char **)calloc(i+1, sizeof (char *));
1003 if (cpp == NULL)
1004 return (NULL);
1006 opp = origAttrList;
1007 for (i = 0; *opp; i++, opp++) {
1008 npp = __ns_ldap_getMappedAttributes(service, *opp);
1009 if (npp && npp[0]) {
1010 cpp[i] = strdup(npp[0]);
1011 __s_api_free2dArray(npp);
1012 npp = NULL;
1013 if (cpp[i] == NULL) {
1014 __s_api_free2dArray(cpp);
1015 return (NULL);
1017 } else {
1018 cpp[i] = strdup(*opp);
1019 if (cpp[i] == NULL) {
1020 __s_api_free2dArray(cpp);
1021 return (NULL);
1025 return (cpp);
1028 char *
1029 __ns_ldap_mapAttribute(
1030 const char *service,
1031 const char *origAttr)
1033 char **npp;
1034 char *mappedAttr;
1036 if (origAttr == NULL)
1037 return (NULL);
1039 npp = __ns_ldap_getMappedAttributes(service, origAttr);
1040 if (npp && npp[0]) {
1041 mappedAttr = strdup(npp[0]);
1042 __s_api_free2dArray(npp);
1043 } else {
1044 mappedAttr = strdup(origAttr);
1046 return (mappedAttr);