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]
22 * Copyright 2015 Gary Mills
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <sys/types.h>
32 #include <rpcsvc/nis.h>
35 #include "ldap_util.h"
36 #include "ldap_attr.h"
37 #include "ldap_ruleval.h"
40 #include "ldap_glob.h"
44 /* From yptol/dit_access_utils.h */
45 #define N2LKEY "rf_key"
46 #define N2LIPKEY "rf_ipkey"
48 __nis_hash_table_mt ldapMappingList
= NIS_HASH_TABLE_MT_INIT
;
53 setColumnNames(__nis_table_mapping_t
*t
) {
57 char *myself
= "setColumnNames";
64 nic
= (col
!= 0) ? t
->numColumns
: -1;
66 t
->objType
= NIS_BOGUS_OBJ
;
70 * If it's a table object, but there are no translation rules,
71 * this mapping is for the table object itself. In that case,
72 * we throw away the column names (if any).
74 if (t
->objType
== NIS_TABLE_OBJ
&& t
->numRulesFromLDAP
== 0 &&
75 t
->numRulesToLDAP
== 0) {
76 for (i
= 0; i
< t
->numColumns
; i
++)
85 * Verify that all column names found by the parser
86 * are present in the actual column list.
89 for (i
= 0, noc
= 0; i
< nic
; i
++) {
94 /* Skip the 'zo_*' special column names */
95 if (isObjAttrString(col
[i
]))
97 for (j
= 0; j
< t
->numColumns
; j
++) {
98 if (strcmp(col
[i
], t
->column
[j
]) == 0) {
105 logmsg(MSG_NOTIMECHECK
, LOG_WARNING
,
106 "%s: No column \"%s\" in \"%s\"",
107 myself
, NIL(col
[i
]), NIL(t
->objName
));
112 /* Remove any setup by the parser */
113 for (i
= 0; i
< nic
; i
++) {
122 freeSingleObjAttr(__nis_obj_attr_t
*attr
) {
126 sfree(attr
->zo_owner
);
127 sfree(attr
->zo_group
);
128 sfree(attr
->zo_domain
);
133 freeObjAttr(__nis_obj_attr_t
**attr
, int numAttr
) {
139 for (i
= 0; i
< numAttr
; i
++) {
140 freeSingleObjAttr(attr
[i
]);
147 cloneObjAttr(__nis_obj_attr_t
*old
) {
148 __nis_obj_attr_t
*new;
149 char *myself
= "cloneObjAttr";
154 new = am(myself
, sizeof (*new));
158 new->zo_owner
= sdup(myself
, T
, old
->zo_owner
);
159 if (new->zo_owner
== 0 && old
->zo_owner
!= 0)
162 new->zo_group
= sdup(myself
, T
, old
->zo_group
);
163 if (new->zo_group
== 0 && old
->zo_group
!= 0)
166 new->zo_domain
= sdup(myself
, T
, old
->zo_domain
);
167 if (new->zo_domain
== 0 && old
->zo_domain
!= 0)
170 new->zo_access
= old
->zo_access
;
171 new->zo_ttl
= old
->zo_ttl
;
176 freeSingleObjAttr(new);
183 * Obtain NIS+ entries (in the form of db_query's) from the supplied table
184 * mapping and db_query.
186 * If 'qin' is NULL, enumeration is desired.
188 * On exit, '*numQueries' contains the number of (db_query *)'s in the
189 * return array, '*ldapStat' the LDAP operation status, and '*objAttr'
190 * a pointer to an array (of '*numQueries elements) of object attributes
191 * (zo_owner, etc.). If no object attributes were retrieved, '*objAttr'
192 * is NULL; any and all of the (*objAttr)[i]'s may be NULL.
195 mapFromLDAP(__nis_table_mapping_t
*t
, db_query
*qin
, int *numQueries
,
196 char *dbId
, int *ldapStat
, __nis_obj_attr_t
***objAttr
) {
197 __nis_table_mapping_t
**tp
;
199 __nis_rule_value_t
*rv
;
200 __nis_ldap_search_t
*ls
;
201 int n
, numVals
, numMatches
= 0;
203 __nis_obj_attr_t
**attr
;
204 char *myself
= "mapFromLDAP";
209 if (t
== 0 || numQueries
== 0) {
210 *ldapStat
= LDAP_PARAM_ERROR
;
214 /* Select the correct table mapping(s) */
215 tp
= selectTableMapping(t
, qin
, 0, 0, dbId
, &numMatches
);
216 if (tp
== 0 || numMatches
<= 0) {
218 * Not really an error; just no matching mapping
221 *ldapStat
= LDAP_SUCCESS
;
228 /* For each mapping */
229 for (numVals
= 0, n
= 0; n
< numMatches
; n
++) {
231 int i
, nqt
= 0, filterOnQin
, res
= 0;
236 rv
= buildNisPlusRuleValue(t
, qin
, 0);
239 * Depending on the value of res, we shall
240 * proceed to next table mapping.
242 ls
= createLdapRequest(t
, rv
, 0, 1, &res
, NULL
);
247 /* Build enumeration request */
249 ls
= createLdapRequest(t
, 0, 0, 1, NULL
, NULL
);
252 freeRuleValue(rv
, 1);
256 * if the res is NP_LDAP_RULES_NO_VALUE, that means we
257 * have enough NIS+ columns for the rules to produce
258 * values, but none of them did, so continue to the
259 * next table mapping. Otherwise do cleanup and return
262 if (res
== NP_LDAP_RULES_NO_VALUE
)
264 for (i
= 0; i
< numVals
; i
++)
268 *ldapStat
= LDAP_OPERATIONS_ERROR
;
273 nqt
= (ls
->isDN
|| qin
!= 0) ? 0 : -1;
274 rv
= ldapSearch(ls
, &nqt
, 0, ldapStat
);
277 * If qin != 0, then we need to make sure that the
278 * LDAP search is filtered so that only entries that
279 * are compatible with 'qin' are retained. This will
280 * happen automatically if we do a DN search (in which
281 * case, no need to filter on 'qin').
283 if (ls
->isDN
|| qin
== 0)
290 /* Convert rule-values to db_query's */
291 if (rv
!= 0 && nqt
> 0) {
293 __nis_obj_attr_t
**at
= 0;
295 qt
= ruleValue2Query(t
, rv
,
296 (filterOnQin
) ? qin
: 0, &at
, &nqt
);
297 freeRuleValue(rv
, nrv
);
299 if (qt
!= 0 && q
== 0) {
303 } else if (qt
!= 0) {
305 __nis_obj_attr_t
**atmp
;
307 /* Extend the 'q' array */
309 (numVals
+nqt
) * sizeof (q
[0]));
310 /* ... and the 'attr' array */
312 (numVals
+nqt
) * sizeof (attr
[0]));
313 if (tmp
== 0 || atmp
== 0) {
314 logmsg(MSG_NOMEM
, LOG_ERR
,
315 "%s: realloc(%d) => NULL",
317 (numVals
+nqt
) * sizeof (q
[0]));
318 for (i
= 0; i
< numVals
; i
++)
320 for (i
= 0; i
< nqt
; i
++)
327 freeObjAttr(at
, nqt
);
328 freeObjAttr(attr
, numVals
);
329 *ldapStat
= LDAP_NO_MEMORY
;
334 /* Add the results for this 't' */
335 (void) memcpy(&q
[numVals
], qt
,
336 nqt
* sizeof (qt
[0]));
337 (void) memcpy(&attr
[numVals
], at
,
338 nqt
* sizeof (at
[0]));
347 *numQueries
= numVals
;
351 freeObjAttr(attr
, numVals
);
358 * Add the object attributes (zo_owner, etc.) to the rule-value 'rv'.
359 * Returns a pointer to the (possibly newly allocated) rule-value,
360 * or NULL in case of failure. If not returning 'rvIn', the latter
361 * will have been freed.
364 addObjAttr2RuleValue(nis_object
*obj
, __nis_rule_value_t
*rvIn
) {
365 __nis_rule_value_t
*rv
;
366 char abuf
[2 * sizeof (obj
->zo_access
) + 1];
367 char tbuf
[2 * sizeof (obj
->zo_ttl
) + 1];
375 rv
= initRuleValue(1, 0);
380 if (obj
->zo_owner
!= 0) {
381 if (addSCol2RuleValue("zo_owner", obj
->zo_owner
, rv
) != 0) {
382 freeRuleValue(rv
, 1);
387 if (obj
->zo_group
!= 0) {
388 if (addSCol2RuleValue("zo_group", obj
->zo_group
, rv
) != 0) {
389 freeRuleValue(rv
, 1);
394 if (obj
->zo_domain
!= 0) {
395 if (addSCol2RuleValue("zo_domain", obj
->zo_domain
, rv
) != 0) {
396 freeRuleValue(rv
, 1);
401 (void) memset(abuf
, 0, sizeof (abuf
));
402 (void) memset(tbuf
, 0, sizeof (tbuf
));
404 sprintf(abuf
, "%x", obj
->zo_access
);
405 sprintf(tbuf
, "%x", obj
->zo_ttl
);
407 if (addSCol2RuleValue("zo_access", abuf
, rv
) != 0) {
408 freeRuleValue(rv
, 1);
411 if (addSCol2RuleValue("zo_ttl", tbuf
, rv
) != 0) {
412 freeRuleValue(rv
, 1);
420 * Returns a pointer to (NOT a copy of) the value for the specified
421 * column 'col' in the rule-value 'rv'.
424 findColValue(char *col
, __nis_rule_value_t
*rv
) {
427 if (col
== 0 || rv
== 0 || rv
->numColumns
<= 0)
430 for (i
= 0; i
< rv
->numColumns
; i
++) {
431 if (strcmp(col
, rv
->colName
[i
]) == 0)
432 return (&rv
->colVal
[i
]);
439 * Return the NIS+ object attributes (if any) in the rule-value 'rv'.
442 ruleValue2ObjAttr(__nis_rule_value_t
*rv
) {
443 __nis_obj_attr_t
*attr
;
445 char *myself
= "ruleValue2ObjAttr";
447 if (rv
== 0 || rv
->numColumns
<= 0)
450 attr
= am(myself
, sizeof (*attr
));
452 if ((val
= findColValue("zo_owner", rv
)) != 0 &&
453 val
->type
== vt_string
&& val
->numVals
== 1 &&
454 val
->val
[0].value
!= 0) {
455 attr
->zo_owner
= sdup(myself
, T
, val
->val
[0].value
);
456 if (attr
->zo_owner
== 0) {
457 freeSingleObjAttr(attr
);
462 if ((val
= findColValue("zo_group", rv
)) != 0 &&
463 val
->type
== vt_string
&& val
->numVals
== 1 &&
464 val
->val
[0].value
!= 0) {
465 attr
->zo_group
= sdup(myself
, T
, val
->val
[0].value
);
466 if (attr
->zo_group
== 0) {
467 freeSingleObjAttr(attr
);
472 if ((val
= findColValue("zo_domain", rv
)) != 0 &&
473 val
->type
== vt_string
&& val
->numVals
== 1 &&
474 val
->val
[0].value
!= 0) {
475 attr
->zo_domain
= sdup(myself
, T
, val
->val
[0].value
);
476 if (attr
->zo_domain
== 0) {
477 freeSingleObjAttr(attr
);
482 if ((val
= findColValue("zo_access", rv
)) != 0 &&
483 val
->type
== vt_string
&& val
->numVals
== 1 &&
484 val
->val
[0].value
!= 0) {
485 if (sscanf(val
->val
[0].value
, "%x", &attr
->zo_access
) != 1) {
486 freeSingleObjAttr(attr
);
491 if ((val
= findColValue("zo_ttl", rv
)) != 0 &&
492 val
->type
== vt_string
&& val
->numVals
== 1 &&
493 val
->val
[0].value
!= 0) {
494 if (sscanf(val
->val
[0].value
, "%x", &attr
->zo_ttl
) != 1) {
495 freeSingleObjAttr(attr
);
504 * If the supplied string is one of the object attributes, return one.
505 * Otherwise, return zero.
508 isObjAttrString(char *str
) {
512 if (strcmp("zo_owner", str
) == 0 ||
513 strcmp("zo_group", str
) == 0 ||
514 strcmp("zo_domain", str
) == 0 ||
515 strcmp("zo_access", str
) == 0 ||
516 strcmp("zo_ttl", str
) == 0)
524 * If the supplied value is one of the object attribute strings, return
525 * a pointer to the string. Otherwise, return NULL.
528 isObjAttr(__nis_single_value_t
*val
) {
529 if (val
== 0 || val
->length
<= 0 || val
->value
== 0)
532 if (isObjAttrString(val
->value
))
539 setObjAttrField(char *attrName
, __nis_single_value_t
*val
,
540 __nis_obj_attr_t
**objAttr
) {
541 __nis_obj_attr_t
*attr
;
542 char *myself
= "setObjAttrField";
544 if (attrName
== 0 || val
== 0 || objAttr
== 0 ||
545 val
->value
== 0 || val
->length
<= 0)
551 attr
= am(myself
, sizeof (*attr
));
557 if (strcmp("zo_owner", attrName
) == 0) {
558 if (attr
->zo_owner
== 0) {
559 attr
->zo_owner
= sdup(myself
, T
, val
->value
);
560 if (attr
->zo_owner
== 0)
563 } else if (strcmp("zo_group", attrName
) == 0) {
564 if (attr
->zo_group
== 0) {
565 attr
->zo_group
= sdup(myself
, T
, val
->value
);
566 if (attr
->zo_group
== 0)
569 } else if (strcmp("zo_domain", attrName
) == 0) {
570 if (attr
->zo_domain
== 0) {
571 attr
->zo_domain
= sdup(myself
, T
, val
->value
);
572 if (attr
->zo_domain
== 0)
575 } else if (strcmp("zo_access", attrName
) == 0) {
576 if (attr
->zo_access
== 0) {
577 if (sscanf(val
->value
, "%x", &attr
->zo_access
) != 1)
580 } else if (strcmp("zo_ttl", attrName
) == 0) {
581 if (attr
->zo_ttl
== 0) {
582 if (sscanf(val
->value
, "%x", &attr
->zo_ttl
) != 1)
591 * Return a DN and rule-value for the supplied mapping, db_query's, and
592 * input rule-value. This function only works on a single mapping. See
593 * mapToLDAP() below for a description of the action depending on the
594 * values of 'old' and 'new'.
596 * If both 'old' and 'new' are supplied, and the modify would result
597 * in a change to the DN, '*oldDN' will contain the old DN. Otherwise
598 * (and normally), '*oldDN' will be NULL.
601 map1qToLDAP(__nis_table_mapping_t
*t
, db_query
*old
, db_query
*new,
602 __nis_rule_value_t
*rvIn
, __nis_rule_value_t
**rvOutP
,
605 __nis_rule_value_t
*rv
, *rvt
;
606 __nis_ldap_search_t
*ls
;
607 char *dn
= 0, *oldDn
= 0;
608 __nis_table_mapping_t del
;
609 char *myself
= "map1qToLDAP";
611 if (t
== 0 || (old
== 0 && new == 0) || rvOutP
== 0)
615 * If entry should be deleted, we look at the delete
616 * policy in the table mapping. Should it specify a
617 * rule set, we use that rule set to build a rule-
618 * value, and the delete actually becomes a modify
621 if (old
!= 0 && new == 0) {
622 if (t
->objectDN
->delDisp
== dd_perDbId
) {
624 * The functions that build a rule-value from a
625 * rule set expect a __nis_table_mapping_t, but the
626 * rule set in the __nis_object_dn_t isn't of that
627 * form. So, build a pseudo-__nis_table_mapping_t that
628 * borrows heavily from 't'.
632 del
.numRulesToLDAP
= del
.objectDN
->numDbIds
;
633 del
.ruleToLDAP
= del
.objectDN
->dbId
;
636 * Do a modify with the pseudo-table
637 * mapping, and the 'old' db_query
638 * supplying input to the delete rule
643 } else if (t
->objectDN
->delDisp
== dd_always
) {
645 /* Nothing to do here; all handled below */
647 } else if (t
->objectDN
->delDisp
== dd_never
) {
653 logmsg(MSG_INVALIDDELDISP
, LOG_WARNING
,
654 "%s: Invalid delete disposition %d for \"%s\"",
655 myself
, t
->objectDN
->delDisp
,
662 /* Make a copy of the input rule-value */
664 rv
= initRuleValue(1, rvIn
);
671 /* First get a rule-value from the supplied NIS+ entry. */
673 rv
= buildNisPlusRuleValue(t
, ((old
!= 0) ? old
: new), rvt
);
674 freeRuleValue(rvt
, 1);
676 logmsg(MSG_NORULEVALUE
, LOG_WARNING
,
677 "%s: No in-query rule-value derived for \"%s\"",
678 myself
, NIL(t
->dbId
));
683 * Create a request (really only care about the DN) from the
684 * supplied NIS+ entry data.
686 ls
= createLdapRequest(t
, rv
, &dn
, 0, NULL
, NULL
);
687 if (ls
== 0 || dn
== 0) {
688 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
689 "%s: Unable to create LDAP request for %s: %s",
690 myself
, NIL(t
->dbId
),
691 (dn
!= 0) ? dn
: rvId(rv
, mit_nisplus
));
694 freeRuleValue(rv
, 1);
702 * Create a rule-value from the new NIS+ entry.
703 * Don't want to mix in the rule-value derived
704 * from 'old', so delete it. However, we still
705 * want the owner, group, etc., from 'rvIn'.
708 freeRuleValue(rv
, 1);
710 rv
= initRuleValue(1, rvIn
);
720 rv
= buildNisPlusRuleValue(t
, new, rvt
);
721 freeRuleValue(rvt
, 1);
723 logmsg(MSG_NORULEVALUE
, LOG_WARNING
,
724 "%s: No new rule-value derived for \"%s: %s\"",
725 myself
, NIL(t
->dbId
), dn
);
730 * Check if the proposed modification would result in a
731 * a change to the DN.
736 ls
= createLdapRequest(t
, rv
, &dn
, 0, NULL
, NULL
);
737 if (ls
== 0 || dn
== 0) {
738 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
739 "%s: Unable to create new DN for \"%s: %s\"",
740 myself
, NIL(t
->dbId
), oldDn
);
743 freeRuleValue(rv
, 1);
747 if (strcasecmp(oldDn
, dn
) == 0) {
763 * Since the DN hash list is an automatic variable, there's no need for
764 * locking, and we remove the locking overhead by using the libnsl
768 #undef NIS_HASH_TABLE
777 * Update LDAP per the supplied table mapping and db_query's.
779 * 'nq' is the number of elements in the 'old', 'new', and 'rvIn'
780 * arrays. mapToLDAP() generally performs one update for each
781 * element; however, if one or more of the individual queries
782 * produce the same DN, they're merged into a single update.
784 * There are four cases, depending on the values of 'old[iq]' and
787 * (1) old[iq] == 0 && new[iq] == 0
788 * No action; skip to next query
790 * (2) old[iq] == 0 && new[iq] != 0
791 * Attempt to use the 'new' db_query to get a DN, and try to create
792 * the corresponding LDAP entry.
794 * (3) old[iq] != 0 && new[iq] == 0
795 * Use the 'old' db_query to get a DN, and try to delete the LDAP
796 * entry per the table mapping.
798 * (4) old[iq] != 0 && new[iq] != 0
799 * Use the 'old' db_query to get a DN, and update (possibly create)
800 * the corresponding LDAP entry per the 'new' db_query.
802 * If 'rvIn' is non-NULL, it is expected to contain the object attributes
803 * (zo_owner, etc.) to be written to LDAP. 'rvIn' is an array with 'nq'
806 * If 'firstOnly' is set, only the first old[iq]/new[iq] pair is used
807 * to perform the actual update. Any additional queries specified will
808 * have their values folded in, but are not used to derive update targets.
809 * This mode is inteded to support the case where multiple NIS+ entries
810 * map to one and the same LDAP entry. Note that 'rvIn' must still be
811 * an array of 'nq' elements, though if 'firstOnly' is set, it should be
812 * OK to leave all but 'rvIn[0]' empty.
814 * 'dbId' is used to further narow down the selection of mapping candidates
815 * to those matching the 'dbId' value.
818 mapToLDAP(__nis_table_mapping_t
*tm
, int nq
, db_query
**old
, db_query
**new,
819 __nis_rule_value_t
*rvIn
, int firstOnly
, char *dbId
) {
820 __nis_table_mapping_t
**tp
, **tpa
;
821 int i
, n
, rnq
, iq
, r
, ret
= LDAP_SUCCESS
;
822 int maxMatches
, numMatches
= 0;
823 __nis_ldap_search_t
*ls
;
824 char **dn
= 0, **odn
= 0;
825 __nis_rule_value_t
**rv
;
827 char *myself
= "mapToLDAP";
830 if (tm
== 0 || (old
== 0 && new == 0) || nq
<= 0)
831 return (LDAP_PARAM_ERROR
);
833 /* Determine maximum number of table mapping matches */
835 tp
= selectTableMapping(tm
,
836 (old
!= 0 && old
[0] != 0) ? old
[0] : new[0], 1, 0,
838 numMatches
= maxMatches
;
840 tp
= selectTableMapping(tm
, 0, 1, 0, dbId
, &maxMatches
);
844 * If no matching mapping, we're not mapping to LDAP in this
847 if (tp
== 0 || maxMatches
== 0) {
849 return (LDAP_SUCCESS
);
853 * Allocate the 'rv', 'dn', and 'tpa' arrays. Worst case is that
854 * we need nq * maxMatches elements in each array. However, if
855 * 'firstOnly' is set, we only need one element per matching
858 dn
= am(myself
, (firstOnly
? 1 : nq
) * maxMatches
* sizeof (dn
[0]));
859 odn
= am(myself
, (firstOnly
? 1 : nq
) * maxMatches
* sizeof (odn
[0]));
860 rv
= am(myself
, (firstOnly
? 1 : nq
) * maxMatches
* sizeof (rv
[0]));
861 tpa
= am(myself
, (firstOnly
? 1 : nq
) * maxMatches
* sizeof (tpa
[0]));
862 if (dn
== 0 || odn
== 0 || rv
== 0 || tpa
== 0) {
868 return (LDAP_NO_MEMORY
);
871 /* Unless nq == 1, we don't need the 'tp' value */
875 logmsg(MSG_NOTIMECHECK
,
876 #ifdef NISDB_LDAP_DEBUG
880 #endif /* NISDB_LDAP_DEBUG */
881 "%s: %s: %d * %d potential updates",
882 myself
, NIL(tm
->objName
), nq
, maxMatches
);
885 * Create DNs, column and attribute values, and merge duplicate DNs.
887 for (iq
= 0, rnq
= 0; iq
< nq
; iq
++) {
890 if ((old
== 0 || old
[iq
] == 0) &&
891 (new == 0 || new[iq
] == 0))
895 * Select matching table mappings; if nq == 1, we've already
896 * got the 'tp' array from above. We expect this to be the
897 * most common case, so it's worth special treatment.
900 tp
= selectTableMapping(tm
,
901 (old
!= 0 && old
[iq
] != 0) ? old
[iq
] : new[iq
], 1, 0,
905 else if (numMatches
<= 0) {
910 idx
= iq
* maxMatches
;
912 if (idx
== 0 || !firstOnly
)
913 (void) memcpy(&tpa
[idx
], tp
,
914 numMatches
* sizeof (tpa
[idx
]));
916 for (n
= 0; n
< numMatches
; n
++) {
918 __nis_rule_value_t
*rvt
= 0;
923 dnt
= map1qToLDAP(tp
[n
],
924 (old
!= 0) ? old
[iq
] : 0,
925 (new != 0) ? new[iq
] : 0,
926 (rvIn
!= 0) ? &rvIn
[iq
] : 0,
932 #ifdef NISDB_LDAP_DEBUG
938 #endif /* NISDB_LDAP_DEBUG */
942 * Create a request to get a rule-value with
943 * NIS+ data translated to LDAP equivalents.
945 ls
= createLdapRequest(tp
[n
], rvt
, 0, 0, NULL
, NULL
);
947 if (ret
== LDAP_SUCCESS
)
948 ret
= LDAP_OPERATIONS_ERROR
;
949 logmsg(MSG_NOTIMECHECK
, LOG_WARNING
,
950 "%s: Unable to map to LDAP attrs for %s:dn=%s",
951 myself
, NIL(tp
[n
]->dbId
), dnt
);
953 freeRuleValue(rvt
, 1);
959 * If the DN is the same as one we already know
960 * about, merge the rule-values.
963 if ((iq
== 0 || !firstOnly
) && dnt
!= 0) {
964 dni
= am(myself
, sizeof (*dni
));
966 dni
->item
.name
= dnt
;
967 dni
->index
= idx
+ n
;
970 logmsg(MSG_NOTIMECHECK
, LOG_WARNING
,
971 "%s: Skipping update for dn=\"%s\"",
982 freeRuleValue(rvt
, 1);
985 } else if (dnt
!= 0) {
988 freeRuleValue(rvt
, 1);
994 logmsg(MSG_NOTIMECHECK
,
995 #ifdef NISDB_LDAP_DEBUG
999 #endif /* NISDB_LDAP_DEBUG */
1000 "%s: %s: %d update%s requested",
1001 myself
, NIL(tm
->objName
), rnq
, rnq
!= 1 ? "s" : "");
1003 /* Perform the updates */
1004 for (i
= rnq
= 0; i
< (firstOnly
? maxMatches
: nq
*maxMatches
); i
++) {
1010 #ifdef NISDB_LDAP_DEBUG
1011 logmsg(MSG_NOTIMECHECK
, LOG_INFO
,
1014 (new != 0 && new[i
/maxMatches
] != 0) ?
1015 "modify" : "delete",
1016 NIL(tpa
[i
]->dbId
), dn
[i
]);
1017 #endif /* NISDB_LDAP_DEBUG */
1019 delPerDbId
= (tpa
[i
]->objectDN
->delDisp
== dd_perDbId
);
1020 if ((new != 0 && new[i
/maxMatches
] != 0) || delPerDbId
) {
1022 * Try to modify/create the specified DN. First,
1023 * however, if the update changes the DN, make
1026 if (odn
[i
] == 0 || (r
= ldapChangeDN(odn
[i
], dn
[i
])) ==
1030 addFirst
= (new != 0 &&
1031 new[i
/maxMatches
] != 0 &&
1033 r
= ldapModify(dn
[i
], rv
[i
],
1034 tpa
[i
]->objectDN
->write
.attrs
,
1038 /* Try to delete the specified DN */
1039 r
= ldapModify(dn
[i
], 0,
1040 tpa
[i
]->objectDN
->write
.attrs
, 0);
1043 if (r
== LDAP_SUCCESS
) {
1046 if (ret
== LDAP_SUCCESS
)
1048 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
1049 "%s: LDAP %s request error %d for %s:dn=%s",
1051 (new != 0 && new[i
/maxMatches
] != 0) ?
1052 "modify" : "delete",
1053 r
, NIL(tpa
[i
]->dbId
), dn
[i
]);
1058 freeRuleValue(rv
[i
], 1);
1067 logmsg(MSG_NOTIMECHECK
,
1068 #ifdef NISDB_LDAP_DEBUG
1072 #endif /* NISDB_LDAP_DEBUG */
1073 "%s: %s: %d update%s performed",
1074 myself
, NIL(tm
->objName
), rnq
, rnq
!= 1 ? "s" : "");
1080 * In nis+2ldap, check if the query 'q' matches the selector index 'x->index'.
1082 * In nis2ldap, if 'name' is provided then check if its value in 'val'
1083 * matches the selector index. If 'name' is NULL, then check if rule-value 'rv'
1084 * matches the index.
1085 * To match the selector index, all fieldspecs in the indexlist should match
1086 * (AND). In nis2ldap, an exception is, if there are multiple fieldspecs with
1087 * the same fieldname then only one of them needs to match (OR).
1089 * Indexlist = [host="H*", host="I*", user="U*", domain="D*"]
1091 * host = "H1", user="U1", domain="D1" ==> pass
1092 * host = "I1", user="U1", domain="D1" ==> pass
1093 * host = "X1", user="U1", domain="D1" ==> fail
1094 * host = "H1", user="X1", domain="D1" ==> fail
1095 * host = "H1", user="U1" ==> fail
1097 * Return 1 in case of a match, 0 otherwise.
1100 verifyIndexMatch(__nis_table_mapping_t
*x
, db_query
*q
,
1101 __nis_rule_value_t
*rv
, char *name
, char *val
) {
1102 int i
, j
, k
, match
= 1;
1103 char *myself
= "verifyIndexMatch";
1106 * The pass and fail arrays are used by N2L to keep track of
1107 * index matches. This saves us from having matches in a
1108 * nested loop to decide OR or AND.
1111 char **pass
, **fail
;
1117 if (x
->index
.numIndexes
<= 0 || (!yp2ldap
&& q
== 0))
1121 if (!(pass
= am(myself
, x
->index
.numIndexes
* sizeof (char *))))
1123 if (!(fail
= am(myself
,
1124 x
->index
.numIndexes
* sizeof (char *)))) {
1131 /* Check each index */
1132 for (i
= 0; i
< x
->index
.numIndexes
; i
++) {
1136 /* Skip NULL index names */
1137 if (x
->index
.name
[i
] == 0)
1140 /* Check N2L values */
1143 if (strcasecmp(x
->index
.name
[i
], name
) == 0)
1148 if (strcasecmp(x
->index
.name
[i
], N2LKEY
) == 0 ||
1149 strcasecmp(x
->index
.name
[i
], N2LIPKEY
)
1152 value
= findVal(x
->index
.name
[i
], rv
,
1156 if (value
&& verifyMappingMatch(x
->index
.value
[i
],
1158 pass
[ppos
++] = x
->index
.name
[i
];
1160 fail
[fpos
++] = x
->index
.name
[i
];
1164 /* If here, means nis+2ldap */
1166 /* Is the index name a known column ? */
1167 for (j
= 0; j
< x
->numColumns
; j
++) {
1168 if (strcmp(x
->index
.name
[i
], x
->column
[j
]) == 0) {
1170 * Do we have a value for the column ?
1172 for (k
= 0; k
< q
->components
.components_len
;
1174 if (q
->components
.components_val
[k
].
1176 value
= q
->components
.
1181 len
= q
->components
.
1195 * If we found a value, check if it matches the
1196 * format. If no value found or no match, this
1197 * mapping is _not_ an alternative. Otherwise,
1198 * we continue checking any other indexes.
1201 !verifyMappingMatch(x
->index
.value
[i
],
1209 for (--fpos
; fpos
>= 0; fpos
--) {
1210 for (i
= 0; i
< ppos
; i
++) {
1211 if (strcmp(pass
[i
], fail
[fpos
]) == 0)
1227 * Return all table mappings that match the column values in 'q'.
1228 * If there's no match, return those alternative mappings that don't
1229 * have an index; if no such mapping exists, return NULL.
1231 * If 'wantWrite' is set, we want mappings for writing (i.e., data
1232 * to LDAP); otherwise, we want mappings for reading.
1234 * If 'wantObj' is set, we want object mappings only (i.e., _not_
1235 * those used to map entries in tables).
1237 * If 'dbId' is non-NULL, we select mappings with a matching dbId field.
1239 __nis_table_mapping_t
**
1240 selectTableMapping(__nis_table_mapping_t
*t
, db_query
*q
,
1241 int wantWrite
, int wantObj
, char *dbId
,
1243 __nis_table_mapping_t
*r
, *x
, **tp
;
1245 char *myself
= "selectTableMapping";
1247 if (numMatches
== 0)
1251 * Count the number of possible mappings, so that we can
1252 * allocate the 'tp' array up front.
1254 for (numap
= 0, x
= t
; x
!= 0; numap
++, x
= x
->next
);
1261 tp
= am(myself
, numap
* sizeof (tp
[0]));
1270 * q == 0 trivially matches any 't' of the correct object type
1272 * wantObj != 0 means we ignore 'q'
1274 if (q
== 0 || wantObj
) {
1275 for (i
= 0, x
= t
, nm
= 0; i
< numap
; i
++, x
= x
->next
) {
1276 if (x
->objectDN
== 0)
1279 if (x
->objectDN
->write
.scope
==
1283 if (x
->objectDN
->read
.scope
==
1288 if (x
->numColumns
> 0)
1291 if (x
->numColumns
<= 0)
1294 if (dbId
!= 0 && x
->dbId
!= 0 &&
1295 strcmp(dbId
, x
->dbId
) != 0)
1308 /* Scan all mappings, and collect candidates */
1309 for (nm
= 0, r
= 0, x
= t
; x
!= 0; x
= x
->next
) {
1310 if (x
->objectDN
== 0)
1313 if (x
->objectDN
->write
.scope
== LDAP_SCOPE_UNKNOWN
)
1316 if (x
->objectDN
->read
.scope
== LDAP_SCOPE_UNKNOWN
)
1319 /* Only want table/entry mappings */
1320 if (x
->numColumns
<= 0)
1322 if (dbId
!= 0 && x
->dbId
!= 0 &&
1323 strcmp(dbId
, x
->dbId
) != 0)
1326 * It's a match if: there are no indexes, or we actually
1327 * match the query with the indexes.
1329 if (x
->index
.numIndexes
<= 0 ||
1330 verifyIndexMatch(x
, q
, 0, 0, 0)) {
1347 * Return 1 if there's an indexed mapping, 0 otherwise.
1350 haveIndexedMapping(__nis_table_mapping_t
*t
) {
1351 __nis_table_mapping_t
*x
;
1353 for (x
= t
; x
!= 0; x
= x
->next
) {
1354 if (x
->index
.numIndexes
> 0)
1362 * Given an input string 'attrs' of the form "attr1=val1,attr2=val2,...",
1363 * or a filter, return the value associated with the attribute 'attrName'.
1364 * If no instance of 'attrName' is found, return 'default'. In all cases,
1365 * the return value is a copy, and must be freed by the caller.
1367 * Of course, return NULL in case of failure.
1370 attrVal(char *msg
, char *attrName
, char *def
, char *attrs
) {
1371 char *val
, *filter
, **fc
= 0;
1373 char *myself
= "attrVal";
1375 if (attrName
== 0 || attrs
== 0)
1383 filter
= makeFilter(attrs
);
1384 if (filter
!= 0 && (fc
= makeFilterComp(filter
, &nfc
)) != 0 &&
1386 for (i
= 0; i
< nfc
; i
++) {
1390 /* Skip if not of attr=value form */
1391 if ((value
= strchr(name
, '=')) == 0)
1397 if (strcasecmp(attrName
, name
) == 0) {
1405 val
= sdup(msg
, T
, val
);
1408 freeFilterComp(fc
, nfc
);
1413 extern bool_t
xdr_nis_object(register XDR
*xdrs
, nis_object
*objp
);
1416 * Copy an XDR:ed version of the NIS+ object 'o' (or the one indicated
1417 * by 't->objName' if 'o' is NULL) to the place indicated by
1418 * 't->objectDN->write'. Return an appropriate LDAP status code.
1421 objToLDAP(__nis_table_mapping_t
*t
, nis_object
*o
, entry_obj
**ea
, int numEa
) {
1422 __nis_table_mapping_t
**tp
;
1423 int stat
, osize
, n
, numMatches
= 0;
1425 __nis_rule_value_t
*rv
;
1427 __nis_single_value_t
*sv
;
1428 char **attrName
, *dn
;
1429 char *myself
= "objToLDAP";
1432 return (LDAP_PARAM_ERROR
);
1434 logmsg(MSG_NOTIMECHECK
,
1435 #ifdef NISDB_LDAP_DEBUG
1439 #endif /* NISDB_LDAP_DEBUG */
1440 "%s: %s", myself
, NIL(t
->objName
));
1442 tp
= selectTableMapping(t
, 0, 1, 1, 0, &numMatches
);
1443 if (tp
== 0 || numMatches
<= 0) {
1445 logmsg(MSG_NOTIMECHECK
,
1446 #ifdef NISDB_LDAP_DEBUG
1450 #endif /* NISDB_LDAP_DEBUG */
1451 "%s: %s (no mapping)", myself
, NIL(t
->objName
));
1452 return (LDAP_SUCCESS
);
1455 for (n
= 0; n
< numMatches
; n
++) {
1461 return (LDAP_OPERATIONS_ERROR
);
1464 buf
= (char *)xdrNisObject(o
, ea
, numEa
, &osize
);
1467 return (LDAP_OPERATIONS_ERROR
);
1471 * Prepare to build a rule-value containing the XDR:ed
1474 rv
= am(myself
, sizeof (*rv
));
1475 sv
= am(myself
, sizeof (*sv
));
1476 val
= am(myself
, sizeof (*val
));
1477 attrName
= am(myself
, sizeof (attrName
[0]));
1479 attrName
[0] = attrVal(myself
, "nisplusObject",
1481 t
->objectDN
->write
.attrs
);
1482 if (rv
== 0 || sv
== 0 || val
== 0 || attrName
== 0 ||
1490 return (LDAP_NO_MEMORY
);
1496 /* 'vt_ber' just means "not a NUL-terminated string" */
1503 rv
->attrName
= attrName
;
1507 * The 'write.base' is the actual DN of the entry (and the
1508 * scope had better be 'base', but we don't check that).
1510 dn
= t
->objectDN
->write
.base
;
1512 stat
= ldapModify(dn
, rv
, t
->objectDN
->write
.attrs
, 1);
1514 freeRuleValue(rv
, 1);
1516 logmsg(MSG_NOTIMECHECK
,
1517 #ifdef NISDB_LDAP_DEBUG
1521 #endif /* NISDB_LDAP_DEBUG */
1522 "%s: %s (%s)", myself
, NIL(t
->objName
), ldap_err2string(stat
));
1524 if (stat
!= LDAP_SUCCESS
)
1535 * Retrieve a copy of the 't->objName' object from LDAP, where it's
1536 * stored in XDR:ed form in the place indicated by 't->objectDN->read'.
1537 * Un-XDR the object, and return a pointer to it in '*obj'; it's the
1538 * responsibility of the caller to free the object when it's no
1541 * Returns an appropriate LDAP status.
1544 objFromLDAP(__nis_table_mapping_t
*t
, nis_object
**obj
,
1545 entry_obj
***eaP
, int *numEaP
) {
1546 __nis_table_mapping_t
**tp
;
1548 __nis_rule_value_t
*rv
;
1549 __nis_ldap_search_t
*ls
;
1550 char *attrs
[2], *filter
, **fc
= 0;
1552 int i
, j
, nfc
, nrv
, blen
, stat
= LDAP_SUCCESS
;
1554 char *myself
= "objFromLDAP";
1557 return (LDAP_PARAM_ERROR
);
1560 * If there's nowhere to store the result, we might as
1561 * well pretend all went well, and return right away.
1564 return (LDAP_SUCCESS
);
1566 /* Prepare for the worst */
1569 logmsg(MSG_NOTIMECHECK
,
1570 #ifdef NISDB_LDAP_DEBUG
1574 #endif /* NISDB_LDAP_DEBUG */
1575 "%s: %s", myself
, NIL(t
->objName
));
1577 tp
= selectTableMapping(t
, 0, 0, 1, 0, &numMatches
);
1578 if (tp
== 0 || numMatches
<= 0) {
1580 logmsg(MSG_NOTIMECHECK
,
1581 #ifdef NISDB_LDAP_DEBUG
1585 #endif /* NISDB_LDAP_DEBUG */
1586 "%s: %s (no mapping)", myself
, NIL(t
->objName
));
1587 return (LDAP_SUCCESS
);
1590 for (n
= 0; n
< numMatches
; n
++) {
1594 filter
= makeFilter(t
->objectDN
->read
.attrs
);
1595 if (filter
== 0 || (fc
= makeFilterComp(filter
, &nfc
)) == 0 ||
1599 freeFilterComp(fc
, nfc
);
1600 return ((t
->objectDN
->read
.attrs
!= 0) ?
1601 LDAP_NO_MEMORY
: LDAP_PARAM_ERROR
);
1603 /* Don't need the filter, just the components */
1607 * Look for a "nisplusObject" attribute, and (if found) copy
1608 * the value to attrs[0]. Also remove the "nisplusObject"
1609 * attribute and value from the filter components.
1611 attrs
[0] = sdup(myself
, T
, "nisplusObject");
1612 if (attrs
[0] == 0) {
1614 freeFilterComp(fc
, nfc
);
1615 return (LDAP_NO_MEMORY
);
1618 for (i
= 0; i
< nfc
; i
++) {
1623 /* Skip if not of attr=value form */
1624 if ((value
= strchr(name
, '=')) == 0)
1627 /* Temporarily overWrite the '=' with a '\0' */
1630 /* Compare with our target attribute name */
1631 compare
= strcasecmp("nisplusObject", name
);
1633 /* Put back the '=' */
1636 /* Is it the name we're looking for ? */
1639 attrs
[0] = sdup(myself
, T
, value
+1);
1640 if (attrs
[0] == 0) {
1642 freeFilterComp(fc
, nfc
);
1643 return (LDAP_NO_MEMORY
);
1647 (void) memmove(&fc
[i
], &fc
[i
+1],
1648 (nfc
-1-i
) * sizeof (fc
[i
]));
1654 ls
= buildLdapSearch(t
->objectDN
->read
.base
,
1655 t
->objectDN
->read
.scope
,
1656 nfc
, fc
, 0, attrs
, 0, 1);
1658 freeFilterComp(fc
, nfc
);
1661 return (LDAP_OPERATIONS_ERROR
);
1665 rv
= ldapSearch(ls
, &nrv
, 0, &stat
);
1672 for (i
= 0, buf
= 0; i
< nrv
&& buf
== 0; i
++) {
1673 for (j
= 0; j
< rv
[i
].numAttrs
; j
++) {
1674 if (strcasecmp(ls
->attrs
[0],
1675 rv
[i
].attrName
[j
]) == 0) {
1676 if (rv
[i
].attrVal
[j
].numVals
<= 0)
1678 buf
= rv
[i
].attrVal
[j
].val
[0].value
;
1679 blen
= rv
[i
].attrVal
[j
].val
[0].length
;
1686 o
= unXdrNisObject(buf
, blen
, eaP
, numEaP
);
1690 freeRuleValue(rv
, nrv
);
1691 return (LDAP_OPERATIONS_ERROR
);
1693 stat
= LDAP_SUCCESS
;
1696 stat
= LDAP_NO_SUCH_OBJECT
;
1700 freeRuleValue(rv
, nrv
);
1702 logmsg(MSG_NOTIMECHECK
,
1703 #ifdef NISDB_LDAP_DEBUG
1707 #endif /* NISDB_LDAP_DEBUG */
1708 "%s: %s (%s)", myself
, NIL(t
->objName
), ldap_err2string(stat
));
1710 if (stat
!= LDAP_SUCCESS
)
1721 deleteLDAPobj(__nis_table_mapping_t
*t
) {
1722 __nis_table_mapping_t
**tp
;
1723 int n
, stat
, numMatches
= 0;
1724 char *myself
= "deleteLDAPobj";
1727 return (LDAP_PARAM_ERROR
);
1729 logmsg(MSG_NOTIMECHECK
,
1730 #ifdef NISDB_LDAP_DEBUG
1734 #endif /* NISDB_LDAP_DEBUG */
1735 "%s: %s", myself
, NIL(t
->objName
));
1737 tp
= selectTableMapping(t
, 0, 1, 1, 0, &numMatches
);
1738 if (tp
== 0 || numMatches
<= 0) {
1740 logmsg(MSG_NOTIMECHECK
,
1741 #ifdef NISDB_LDAP_DEBUG
1745 #endif /* NISDB_LDAP_DEBUG */
1746 "%s: %s (no mapping)", myself
, NIL(t
->objName
));
1747 return (LDAP_SUCCESS
);
1750 for (n
= 0; n
< numMatches
; n
++) {
1754 if (t
->objectDN
->delDisp
== dd_always
) {
1755 /* Delete entire entry */
1756 stat
= ldapModify(t
->objectDN
->write
.base
, 0,
1757 t
->objectDN
->write
.attrs
, 1);
1758 } else if (t
->objectDN
->delDisp
== dd_perDbId
) {
1760 * Delete the attribute holding the object.
1761 * First, determine what that attribute is called.
1767 t
->objectDN
->write
.attrs
);
1768 __nis_rule_value_t rv
;
1771 if (attrName
== 0) {
1773 return (LDAP_NO_MEMORY
);
1777 * Build a __nis_value_t with 'numVals' < 0 to
1778 * indicate deletion.
1785 * Build a rule-value with the name we determined
1786 * above, and the deletion value.
1788 (void) memset(&rv
, 0, sizeof (rv
));
1790 rv
.attrName
= &attrName
;
1793 stat
= ldapModify(t
->objectDN
->write
.base
, &rv
,
1794 t
->objectDN
->write
.attrs
, 0);
1797 } else if (t
->objectDN
->delDisp
== dd_never
) {
1798 /* Nothing to do, so we're trivially successful */
1799 stat
= LDAP_SUCCESS
;
1801 stat
= LDAP_PARAM_ERROR
;
1804 logmsg(MSG_NOTIMECHECK
,
1805 #ifdef NISDB_LDAP_DEBUG
1809 #endif /* NISDB_LDAP_DEBUG */
1810 "%s: %s (%s)", myself
, NIL(t
->objName
), ldap_err2string(stat
));
1812 /* If there were no such object, we've trivially succeeded */
1813 if (stat
== LDAP_NO_SUCH_OBJECT
)
1814 stat
= LDAP_SUCCESS
;
1816 if (stat
!= LDAP_SUCCESS
)