dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libsldap / common / ns_mapping.c
blob6ce3e7513e9ecee239f01bc22fba109d8b82eee6
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 free(ptr->basedn);
304 free(ptr->filter);
305 free(ptr);
309 * Parse a service descriptor
310 * and create a service descriptor struct
311 * SD Format:
312 * serviceid:[base][?[scope][?[filter]]];[[base][?[scope][?[filter]]]]
313 * desc format:
314 * [base][?[scope][?[filter]]]
317 typedef enum _ns_parse_state {
318 P_ERROR, P_INIT, P_BASEDN, P_SCOPE,
319 P_INIFILTER, P_FILTER, P_END, P_EXIT, P_MEMERR
320 } _ns_parse_state_t;
322 static
324 __s_api_parseASearchDesc(const char *service,
325 char **cur, ns_ldap_search_desc_t **ret)
327 ns_ldap_search_desc_t *ptr;
328 char *sptr, *dptr;
329 char buf[BUFSIZ];
330 int i, rc;
331 ns_ldap_error_t **errorp = NULL;
332 ns_ldap_error_t *error = NULL;
333 void **paramVal = NULL;
334 char **dns = NULL;
335 _ns_parse_state_t state = P_INIT;
336 int quoted = 0;
337 int wasquoted = 0;
338 int empty = 1;
340 if (ret == NULL)
341 return (NS_LDAP_INVALID_PARAM);
342 *ret = NULL;
343 if (cur == NULL)
344 return (NS_LDAP_INVALID_PARAM);
346 ptr = (ns_ldap_search_desc_t *)
347 calloc(1, sizeof (ns_ldap_search_desc_t));
348 if (ptr == NULL)
349 return (NS_LDAP_MEMORY);
351 sptr = *cur;
353 /* Get the default scope */
354 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
355 &paramVal, errorp)) != NS_LDAP_SUCCESS) {
356 (void) __ns_ldap_freeError(errorp);
357 __ns_ldap_freeASearchDesc(ptr);
358 ptr = NULL;
359 return (NS_LDAP_MEMORY);
361 if (paramVal && *paramVal)
362 ptr->scope = * (ScopeType_t *)(*paramVal);
363 else
364 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
365 (void) __ns_ldap_freeParam(&paramVal);
366 paramVal = NULL;
368 for (/* none */; state != P_EXIT && sptr && *sptr; sptr++) {
369 empty = 0;
370 switch (state) {
371 case P_INIT:
372 if (*sptr == QUESTTOK) {
373 /* No basedn */
374 ptr->basedn = strdup("");
375 if (!ptr->basedn) {
376 state = P_MEMERR;
377 break;
379 state = P_SCOPE;
380 break;
382 if (*sptr == SEMITOK) {
383 /* No SSD */
384 ptr->basedn = strdup("");
385 if (!ptr->basedn) {
386 state = P_MEMERR;
387 break;
389 state = P_EXIT;
390 break;
392 /* prepare to copy DN */
393 i = strlen(sptr) + 1;
394 ptr->basedn = dptr = (char *)calloc(i, sizeof (char));
395 if (!ptr->basedn) {
396 state = P_MEMERR;
397 break;
399 if (*sptr == BSLTOK) {
400 if (*(sptr+1) == '\0') {
401 /* error */
402 state = P_ERROR;
403 break;
405 if (*(sptr+1) == QUOTETOK ||
406 *(sptr+1) == BSLTOK) {
407 /* escaped CHARS */
408 sptr++;
409 } else {
410 *dptr++ = *sptr++;
412 *dptr++ = *sptr;
413 } else if (*sptr == QUOTETOK) {
414 quoted = 1;
415 wasquoted = 1;
416 } else {
417 *dptr++ = *sptr;
419 state = P_BASEDN;
420 break;
421 case P_INIFILTER:
422 if (*sptr == SEMITOK) {
423 /* No filter and no more SSD */
424 state = P_EXIT;
425 break;
427 /* prepare to copy DN */
428 i = strlen(sptr) + 1;
429 ptr->filter = dptr = (char *)calloc(i, sizeof (char));
430 if (!ptr->filter) {
431 state = P_MEMERR;
432 break;
434 if (*sptr == BSLTOK) {
435 if (*(sptr+1) == '\0') {
436 /* error */
437 state = P_ERROR;
438 break;
440 if (*(sptr+1) == QUOTETOK ||
441 *(sptr+1) == BSLTOK) {
442 /* escaped CHARS */
443 sptr++;
444 } else {
445 *dptr++ = *sptr++;
447 *dptr++ = *sptr;
448 } else if (*sptr == QUOTETOK) {
449 quoted = 1;
450 wasquoted = 1;
451 } else {
452 *dptr++ = *sptr;
454 state = P_FILTER;
455 break;
456 case P_SCOPE:
457 buf[0] = '\0';
458 if (*sptr == SEMITOK) {
459 /* no more SSD */
460 state = P_EXIT;
461 break;
463 if (strncasecmp(sptr, "base", 4) == 0) {
464 sptr += 4;
465 ptr->scope = NS_LDAP_SCOPE_BASE;
466 } else if (strncasecmp(sptr, "one", 3) == 0) {
467 ptr->scope = NS_LDAP_SCOPE_ONELEVEL;
468 sptr += 3;
469 } else if (strncasecmp(sptr, "sub", 3) == 0) {
470 ptr->scope = NS_LDAP_SCOPE_SUBTREE;
471 sptr += 3;
473 if (*sptr == '\0' || (*sptr == SEMITOK)) {
474 /* no more SSD */
475 state = P_EXIT;
476 sptr--;
477 break;
479 if (*sptr != QUESTTOK) {
480 state = P_ERROR;
481 break;
483 state = P_INIFILTER;
484 quoted = 0;
485 wasquoted = 0;
486 break;
487 case P_BASEDN:
488 case P_FILTER:
489 if (quoted) {
490 /* Quoted */
491 if (*sptr == BSLTOK) {
492 if (*(sptr+1) == '\0') {
493 state = P_ERROR;
494 break;
496 if (*(sptr+1) == QUOTETOK ||
497 *(sptr+1) == BSLTOK) {
498 /* escaped CHARS */
499 sptr++;
500 } else {
501 *dptr++ = *sptr++;
503 /* fall through to char copy */
504 } else if (*sptr == QUOTETOK) {
505 /* end of string */
506 *dptr = '\0';
507 quoted = 0;
508 break;
510 /* else fall through to char copy */
511 } else {
512 /* Unquoted */
513 if (wasquoted && *sptr != QUESTTOK) {
514 /* error past end of quoted string */
515 state = P_ERROR;
516 break;
518 if (*sptr == BSLTOK) {
519 if (*(sptr+1) == '\0') {
520 state = P_ERROR;
521 break;
523 if (*(sptr+1) == SEMITOK ||
524 *(sptr+1) == QUESTTOK ||
525 *(sptr+1) == QUOTETOK ||
526 *(sptr+1) == BSLTOK) {
527 /* escaped chars */
528 sptr++;
530 /* fall through to char copy */
531 } else if (*sptr == QUOTETOK) {
532 /* error */
533 state = P_ERROR;
534 break;
535 } else if (*sptr == QUESTTOK) {
536 /* if filter error */
537 if (state == P_FILTER) {
538 state = P_ERROR;
539 break;
541 /* end of basedn goto scope */
542 *dptr = '\0';
543 state = P_SCOPE;
544 break;
545 } else if (*sptr == SEMITOK) {
546 /* end of current SSD */
547 *dptr = '\0';
548 state = P_EXIT;
549 break;
552 /* normal character to copy */
553 *dptr++ = *sptr;
554 break;
555 case P_END:
556 if (*sptr == SEMITOK) {
557 state = P_EXIT;
558 break;
560 __ns_ldap_freeASearchDesc(ptr);
561 ptr = NULL;
562 *cur = NULL;
563 return (NS_LDAP_CONFIG);
564 default: /* error should never arrive here */
565 case P_ERROR:
566 __ns_ldap_freeASearchDesc(ptr);
567 ptr = NULL;
568 *cur = NULL;
569 return (NS_LDAP_CONFIG);
570 case P_MEMERR:
571 __ns_ldap_freeASearchDesc(ptr);
572 ptr = NULL;
573 *cur = NULL;
574 return (NS_LDAP_MEMORY);
578 if (quoted) {
579 __ns_ldap_freeASearchDesc(ptr);
580 ptr = NULL;
581 *cur = NULL;
582 return (NS_LDAP_INVALID_PARAM);
585 if (empty || strlen(ptr->basedn) == 0) {
586 free(ptr->basedn);
587 /* get default base */
588 rc = __s_api_getDNs(&dns, service, &error);
589 if (rc != NS_LDAP_SUCCESS) {
590 if (dns) {
591 __s_api_free2dArray(dns);
592 dns = NULL;
594 (void) __ns_ldap_freeError(&error);
595 __ns_ldap_freeASearchDesc(ptr);
596 ptr = NULL;
597 return (NS_LDAP_MEMORY);
599 ptr->basedn = strdup(dns[0]);
600 __s_api_free2dArray(dns);
601 dns = NULL;
604 *cur = sptr;
605 *ret = ptr;
606 return (NS_LDAP_SUCCESS);
611 * Build up the service descriptor array
613 #define NS_SDESC_MAX 4
615 static int
616 __ns_ldap_saveSearchDesc(ns_ldap_search_desc_t ***sdlist,
617 int *cnt, int *max, ns_ldap_search_desc_t *ret)
619 ns_ldap_search_desc_t **tmplist;
621 if (*sdlist == NULL) {
622 *cnt = 0;
623 *max = NS_SDESC_MAX;
624 *sdlist = (ns_ldap_search_desc_t **)
625 calloc(*max, sizeof (ns_ldap_search_desc_t *));
626 if (*sdlist == NULL)
627 return (-1);
628 } else if (*cnt+1 >= *max) {
629 *max += NS_SDESC_MAX;
630 tmplist = (ns_ldap_search_desc_t **)
631 reallocarray(*sdlist, *max,
632 sizeof (ns_ldap_search_desc_t *));
633 if (tmplist == NULL)
634 return (-1);
635 else
636 *sdlist = tmplist;
638 (*sdlist)[*cnt] = ret;
639 (*cnt)++;
640 (*sdlist)[*cnt] = NULL;
641 return (0);
646 * Exported Search Descriptor Routines
649 int __ns_ldap_getSearchDescriptors(
650 const char *service,
651 ns_ldap_search_desc_t ***desc,
652 ns_ldap_error_t **errorp)
654 int rc;
655 int slen;
656 void **param = NULL;
657 void **paramVal = NULL;
658 char **sdl, *srv, **sdl_save;
659 char errstr[2 * MAXERROR];
660 ns_ldap_search_desc_t **sdlist;
661 int cnt, max;
662 int vers;
663 ns_config_t *cfg;
664 ns_ldap_search_desc_t *ret;
666 if ((desc == NULL) || (errorp == NULL))
667 return (NS_LDAP_INVALID_PARAM);
669 *desc = NULL;
670 *errorp = NULL;
672 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_SEARCH_DESC_P,
673 (void ***)&param, errorp);
674 if (rc != NS_LDAP_SUCCESS) {
675 return (rc);
677 sdl = (char **)param;
678 cnt = 0;
679 max = 0;
680 sdlist = NULL;
682 cfg = __s_api_get_default_config();
684 if (cfg == NULL) {
685 (void) snprintf(errstr, sizeof (errstr),
686 gettext("No configuration information available."));
687 MKERROR(LOG_ERR, *errorp, NS_CONFIG_NOTLOADED, strdup(errstr),
689 return (NS_LDAP_CONFIG);
692 vers = cfg->version;
693 __s_api_release_config(cfg);
695 /* If using version1 or no sd's process SEARCH_DN if available */
696 if (vers == NS_LDAP_V1 && param == NULL) {
697 rc = __s_api_get_search_DNs_v1(&sdl, service, errorp);
698 if (rc != NS_LDAP_SUCCESS || sdl == NULL) {
699 return (rc);
701 sdl_save = sdl;
702 /* Convert a SEARCH_DN to a search descriptor */
703 for (; *sdl; sdl++) {
704 ret = (ns_ldap_search_desc_t *)
705 calloc(1, sizeof (ns_ldap_search_desc_t));
706 if (ret == NULL) {
707 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
708 __s_api_free2dArray(sdl_save);
709 return (NS_LDAP_MEMORY);
711 ret->basedn = strdup(*sdl);
712 if (ret->basedn == NULL) {
713 free(ret);
714 (void) __ns_ldap_freeASearchDesc(ret);
715 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
716 __s_api_free2dArray(sdl_save);
717 return (NS_LDAP_MEMORY);
720 /* default scope */
721 if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
722 &paramVal, errorp)) != NS_LDAP_SUCCESS) {
723 (void) __ns_ldap_freeASearchDesc(ret);
724 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
725 __s_api_free2dArray(sdl_save);
726 return (rc);
728 if (paramVal && *paramVal)
729 ret->scope = * (ScopeType_t *)(*paramVal);
730 else
731 ret->scope = NS_LDAP_SCOPE_ONELEVEL;
732 (void) __ns_ldap_freeParam(&paramVal);
733 paramVal = NULL;
735 rc = __ns_ldap_saveSearchDesc(&sdlist, &cnt, &max, ret);
736 if (rc < 0) {
737 (void) __ns_ldap_freeASearchDesc(ret);
738 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
739 __s_api_free2dArray(sdl_save);
740 return (NS_LDAP_MEMORY);
743 __s_api_free2dArray(sdl_save);
744 *desc = sdlist;
745 return (NS_LDAP_SUCCESS);
748 if (sdl == NULL || service == NULL) {
749 (void) __ns_ldap_freeParam(&param);
750 param = NULL;
751 *desc = NULL;
752 return (NS_LDAP_SUCCESS);
754 slen = strlen(service);
756 /* Process the version2 sd's */
757 for (; *sdl; sdl++) {
758 srv = *sdl;
759 if (strncasecmp(service, srv, slen) != 0)
760 continue;
761 srv += slen;
762 if (*srv != COLONTOK)
763 continue;
764 srv++;
765 while (srv != NULL && *srv != '\0') {
766 /* Process 1 */
767 rc = __s_api_parseASearchDesc(service, &srv, &ret);
768 if (rc != NS_LDAP_SUCCESS) {
769 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
770 (void) snprintf(errstr, (2 * MAXERROR), gettext(
771 "Invalid serviceSearchDescriptor (%s). "
772 "Illegal configuration"), *sdl);
773 (void) __ns_ldap_freeParam(&param);
774 param = NULL;
775 MKERROR(LOG_ERR, *errorp, NS_CONFIG_SYNTAX,
776 strdup(errstr), 0);
777 return (rc);
779 if (ret != NULL) {
780 rc = __ns_ldap_saveSearchDesc(
781 &sdlist, &cnt, &max, ret);
783 if (rc < 0) {
784 (void) __ns_ldap_freeSearchDescriptors(&sdlist);
785 (void) __ns_ldap_freeParam(&param);
786 param = NULL;
787 return (NS_LDAP_MEMORY);
792 (void) __ns_ldap_freeParam(&param);
793 param = NULL;
794 *desc = sdlist;
795 return (NS_LDAP_SUCCESS);
799 __ns_ldap_freeSearchDescriptors(ns_ldap_search_desc_t ***desc)
801 ns_ldap_search_desc_t **dptr;
802 ns_ldap_search_desc_t *ptr;
804 if (*desc == NULL)
805 return (NS_LDAP_SUCCESS);
806 for (dptr = *desc; (ptr = *dptr) != NULL; dptr++) {
807 __ns_ldap_freeASearchDesc(ptr);
809 free(*desc);
810 *desc = NULL;
812 return (NS_LDAP_SUCCESS);
819 * Exported Attribute/Objectclass mapping functions.
823 * This function is not supported.
825 /* ARGSUSED */
826 int __ns_ldap_getAttributeMaps(
827 const char *service,
828 ns_ldap_attribute_map_t ***maps,
829 ns_ldap_error_t **errorp)
831 *maps = NULL;
832 return (NS_LDAP_OP_FAILED);
836 __ns_ldap_freeAttributeMaps(ns_ldap_attribute_map_t ***maps)
838 ns_ldap_attribute_map_t **dptr;
839 ns_ldap_attribute_map_t *ptr;
840 char **cpp, *cp;
842 if (*maps == NULL)
843 return (NS_LDAP_SUCCESS);
844 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
845 if (ptr->origAttr) {
846 free(ptr->origAttr);
847 ptr->origAttr = NULL;
849 if (ptr->mappedAttr) {
850 for (cpp = ptr->mappedAttr; (cp = *cpp) != NULL; cpp++)
851 free(cp);
852 free(ptr->mappedAttr);
853 ptr->mappedAttr = NULL;
855 free(ptr);
857 free(*maps);
858 *maps = NULL;
860 return (NS_LDAP_SUCCESS);
863 char **__ns_ldap_getMappedAttributes(
864 const char *service,
865 const char *origAttribute)
867 ns_config_t *ptr = __s_api_loadrefresh_config();
868 ns_hash_t *hp;
869 char **ret;
871 if (ptr == NULL)
872 return (NULL);
874 hp = ns_get_hash(ptr, NS_HASH_AMAP, service, origAttribute);
876 if (hp == NULL || hp->h_map == NULL)
877 ret = NULL;
878 else
879 ret = __s_api_cp2dArray(hp->h_map->map);
880 __s_api_release_config(ptr);
881 return (ret);
884 char **__ns_ldap_getOrigAttribute(
885 const char *service,
886 const char *mappedAttribute)
888 ns_config_t *ptr = __s_api_loadrefresh_config();
889 ns_hash_t *hp;
890 char **ret;
892 if (ptr == NULL)
893 return (NULL);
895 hp = ns_get_hash(ptr, NS_HASH_RAMAP, service, mappedAttribute);
897 if (hp == NULL || hp->h_map == NULL)
898 ret = NULL;
899 else
900 ret = __s_api_cp2dArray(hp->h_map->map);
901 __s_api_release_config(ptr);
902 return (ret);
906 * This function is not supported.
908 /* ARGSUSED */
909 int __ns_ldap_getObjectClassMaps(
910 const char *service,
911 ns_ldap_objectclass_map_t ***maps,
912 ns_ldap_error_t **errorp)
914 *maps = NULL;
915 return (NS_LDAP_OP_FAILED);
919 __ns_ldap_freeObjectClassMaps(ns_ldap_objectclass_map_t ***maps)
921 ns_ldap_objectclass_map_t **dptr;
922 ns_ldap_objectclass_map_t *ptr;
924 if (*maps == NULL)
925 return (NS_LDAP_SUCCESS);
926 for (dptr = *maps; (ptr = *dptr) != NULL; dptr++) {
927 if (ptr->origOC) {
928 free(ptr->origOC);
929 ptr->origOC = NULL;
931 if (ptr->mappedOC) {
932 free(ptr->mappedOC);
933 ptr->mappedOC = NULL;
935 free(ptr);
937 free(*maps);
938 *maps = NULL;
940 return (NS_LDAP_SUCCESS);
943 char **__ns_ldap_getMappedObjectClass(
944 const char *service,
945 const char *origObjectClass)
947 ns_config_t *ptr = __s_api_loadrefresh_config();
948 ns_hash_t *hp;
949 char **ret;
951 if (ptr == NULL)
952 return (NULL);
954 hp = ns_get_hash(ptr, NS_HASH_OMAP, service, origObjectClass);
956 if (hp == NULL || hp->h_map == NULL)
957 ret = NULL;
958 else
959 ret = __s_api_cp2dArray(hp->h_map->map);
960 __s_api_release_config(ptr);
961 return (ret);
964 char **__ns_ldap_getOrigObjectClass(
965 const char *service,
966 const char *mappedObjectClass)
968 ns_config_t *ptr = __s_api_loadrefresh_config();
969 ns_hash_t *hp;
970 char **ret;
972 if (ptr == NULL)
973 return (NULL);
975 hp = ns_get_hash(ptr, NS_HASH_ROMAP, service, mappedObjectClass);
977 if (hp == NULL || hp->h_map == NULL)
978 ret = NULL;
979 else
980 ret = __s_api_cp2dArray(hp->h_map->map);
981 __s_api_release_config(ptr);
982 return (ret);
985 char **__ns_ldap_mapAttributeList(
986 const char *service,
987 const char * const *origAttrList)
989 const char * const *opp;
990 char **cpp, **npp;
991 int i;
993 if (origAttrList == NULL)
994 return (NULL);
996 opp = origAttrList;
997 for (i = 0; *opp; i++, opp++)
999 cpp = (char **)calloc(i+1, sizeof (char *));
1000 if (cpp == NULL)
1001 return (NULL);
1003 opp = origAttrList;
1004 for (i = 0; *opp; i++, opp++) {
1005 npp = __ns_ldap_getMappedAttributes(service, *opp);
1006 if (npp && npp[0]) {
1007 cpp[i] = strdup(npp[0]);
1008 __s_api_free2dArray(npp);
1009 npp = NULL;
1010 if (cpp[i] == NULL) {
1011 __s_api_free2dArray(cpp);
1012 return (NULL);
1014 } else {
1015 cpp[i] = strdup(*opp);
1016 if (cpp[i] == NULL) {
1017 __s_api_free2dArray(cpp);
1018 return (NULL);
1022 return (cpp);
1025 char *
1026 __ns_ldap_mapAttribute(
1027 const char *service,
1028 const char *origAttr)
1030 char **npp;
1031 char *mappedAttr;
1033 if (origAttr == NULL)
1034 return (NULL);
1036 npp = __ns_ldap_getMappedAttributes(service, origAttr);
1037 if (npp && npp[0]) {
1038 mappedAttr = strdup(npp[0]);
1039 __s_api_free2dArray(npp);
1040 } else {
1041 mappedAttr = strdup(origAttr);
1043 return (mappedAttr);