xref: /onnv-gate/usr/src/lib/libnisdb/nis_parse_ldap_yp_util.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <syslog.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "ldap_parse.h"
380Sstevel@tonic-gate #include "nis_parse_ldap_conf.h"
390Sstevel@tonic-gate #include "nis_parse_ldap_err.h"
400Sstevel@tonic-gate #include "ldap_util.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate extern __nis_mapping_rule_t **dup_mapping_rules(
430Sstevel@tonic-gate 	__nis_mapping_rule_t **rules, int n_rules);
440Sstevel@tonic-gate extern __nis_mapping_rule_t *dup_mapping_rule(
450Sstevel@tonic-gate 	__nis_mapping_rule_t *in);
460Sstevel@tonic-gate 
47*702Sth160488 static int	merge_table_mapping(__nis_table_mapping_t *in,
480Sstevel@tonic-gate 	__nis_table_mapping_t *out);
490Sstevel@tonic-gate __nis_table_mapping_t *new_merged_mapping(const char *,
500Sstevel@tonic-gate 	__nis_table_mapping_t *intbl);
51*702Sth160488 static int append_mapping_rule(__nis_mapping_rule_t *src_rule,
520Sstevel@tonic-gate 	__nis_table_mapping_t *tbl, int flag);
530Sstevel@tonic-gate 
54*702Sth160488 
55*702Sth160488 static int copy_object_dn(__nis_object_dn_t	*in,
56*702Sth160488 		__nis_object_dn_t	*newdn);
57*702Sth160488 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * FUNCTION:	initialize_table_mapping
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Initialize the __nis_table_mapping_t structure.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * INPUT:	__nis_table_mapping_t
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate void
initialize_table_mapping(__nis_table_mapping_t * mapping)670Sstevel@tonic-gate initialize_table_mapping(
680Sstevel@tonic-gate 	__nis_table_mapping_t *mapping)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	if (mapping != NULL) {
710Sstevel@tonic-gate 		mapping->dbId = NULL;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 		mapping->index.numIndexes = 0;
740Sstevel@tonic-gate 		mapping->index.name = NULL;
750Sstevel@tonic-gate 		mapping->index.value = NULL;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 		mapping->numColumns = 0;
780Sstevel@tonic-gate 		mapping->column = NULL;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		mapping->initTtlLo = (time_t)NO_VALUE_SET;
810Sstevel@tonic-gate 		mapping->initTtlHi = (time_t)NO_VALUE_SET;
820Sstevel@tonic-gate 		mapping->ttl = (time_t)NO_VALUE_SET;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		mapping->usedns_flag = 0;
850Sstevel@tonic-gate 		mapping->securemap_flag = 0;
860Sstevel@tonic-gate 		mapping->commentChar = DEFAULT_COMMENT_CHAR;
870Sstevel@tonic-gate 		mapping->numSplits = 0;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		mapping->objectDN = NULL;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		mapping->separatorStr = DEFAULT_SEP_STRING;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		mapping->numRulesFromLDAP = 0;
940Sstevel@tonic-gate 		mapping->numRulesToLDAP = 0;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 		mapping->ruleFromLDAP = NULL;
970Sstevel@tonic-gate 		mapping->ruleToLDAP = NULL;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		mapping->e = NULL;
1000Sstevel@tonic-gate 		mapping->objName = NULL;
1010Sstevel@tonic-gate 		mapping->objPath = NULL;
1020Sstevel@tonic-gate 		mapping->obj = NULL;
1030Sstevel@tonic-gate 		mapping->isMaster = 0;
1040Sstevel@tonic-gate 		mapping->seq_num = NO_VALUE_SET;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * FUNCTION:	initialize_yp_parse_structs
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  * Initialize the __yp_domain_context_t structure.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * INPUT:		__yp_domain_context_t
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate void
initialize_yp_parse_structs(__yp_domain_context_t * ypDomains)1170Sstevel@tonic-gate initialize_yp_parse_structs(
1180Sstevel@tonic-gate 	__yp_domain_context_t	*ypDomains)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	ypDomains->numDomains = 0;
1210Sstevel@tonic-gate 	ypDomains->domainLabels = NULL;
1220Sstevel@tonic-gate 	ypDomains->domains = NULL;
1230Sstevel@tonic-gate 	ypDomains->numYppasswdd = 0;
1240Sstevel@tonic-gate 	ypDomains->yppasswddDomainLabels = NULL;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * FUNCTION: 	merge_table_mapping
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Merges information from one table_mapping struct
1310Sstevel@tonic-gate  * into another
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  * INPUT: Source and Destination table_mapping structs.
1340Sstevel@tonic-gate  * RETURN: 0 on success and > 0 on error.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int
merge_table_mapping(__nis_table_mapping_t * in,__nis_table_mapping_t * out)1380Sstevel@tonic-gate merge_table_mapping(
1390Sstevel@tonic-gate 	__nis_table_mapping_t *in,
1400Sstevel@tonic-gate 	__nis_table_mapping_t *out)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int i;
1430Sstevel@tonic-gate 	int len;
1440Sstevel@tonic-gate 	int orig_num_rules;
1450Sstevel@tonic-gate 	int append;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (in == NULL)
1480Sstevel@tonic-gate 		return (1);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (in->dbId == NULL)
1510Sstevel@tonic-gate 		return (1);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * If 'in' is generic (non-expanded) and 'out' is domain-specific,
1550Sstevel@tonic-gate 	 * then rules from 'in' should not be appended to those in 'out'.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	if (!strchr(in->dbId, COMMA_CHAR) && strchr(out->dbId, COMMA_CHAR))
1580Sstevel@tonic-gate 		append = 0;
1590Sstevel@tonic-gate 	else
1600Sstevel@tonic-gate 		append = 1;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (!out->index.numIndexes && in->index.numIndexes > 0) {
1640Sstevel@tonic-gate 		if (!dup_index(&in->index, &out->index))
1650Sstevel@tonic-gate 			return (1);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/* add_column() increments numColumns, so we don't */
1690Sstevel@tonic-gate 	if (!out->numColumns && in->numColumns > 0) {
1700Sstevel@tonic-gate 		for (i = 0; i < in->numColumns; i++) {
1710Sstevel@tonic-gate 			if (!add_column(out, in->column[i]))
1720Sstevel@tonic-gate 				return (1);
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (out->commentChar == DEFAULT_COMMENT_CHAR &&
1770Sstevel@tonic-gate 		in->commentChar != DEFAULT_COMMENT_CHAR)
1780Sstevel@tonic-gate 		out->commentChar = in->commentChar;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (out->usedns_flag == 0)
1810Sstevel@tonic-gate 		out->usedns_flag = in->usedns_flag;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (out->securemap_flag == 0)
1840Sstevel@tonic-gate 		out->securemap_flag = in->securemap_flag;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (out->separatorStr == DEFAULT_SEP_STRING &&
1870Sstevel@tonic-gate 		in->separatorStr != DEFAULT_SEP_STRING) {
1880Sstevel@tonic-gate 		out->separatorStr = s_strdup(in->separatorStr);
1890Sstevel@tonic-gate 		if (!out->separatorStr)
1900Sstevel@tonic-gate 			return (2);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (!out->numSplits && !out->e && in->e) {
1940Sstevel@tonic-gate 		out->numSplits = in->numSplits;
1950Sstevel@tonic-gate 		out->e = (__nis_mapping_element_t *)
1960Sstevel@tonic-gate 			s_calloc(1, (in->numSplits+1) *
1970Sstevel@tonic-gate 			sizeof (__nis_mapping_element_t));
1980Sstevel@tonic-gate 		if (!out->e)
1990Sstevel@tonic-gate 			return (2);
2000Sstevel@tonic-gate 		for (i = 0; i <= in->numSplits; i++) {
2010Sstevel@tonic-gate 			if (!dup_mapping_element(&in->e[i], &out->e[i])) {
2020Sstevel@tonic-gate 				for (; i > 0; i--) {
2030Sstevel@tonic-gate 					free_mapping_element(&out->e[i - 1]);
2040Sstevel@tonic-gate 				}
2050Sstevel@tonic-gate 				out->e = NULL;
2060Sstevel@tonic-gate 				return (1);
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (out->initTtlLo == (time_t)NO_VALUE_SET &&
2120Sstevel@tonic-gate 		in->initTtlLo != (time_t)NO_VALUE_SET)
2130Sstevel@tonic-gate 		out->initTtlLo = in->initTtlLo;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (out->initTtlHi == (time_t)NO_VALUE_SET &&
2160Sstevel@tonic-gate 		in->initTtlHi != (time_t)NO_VALUE_SET)
2170Sstevel@tonic-gate 		out->initTtlHi = in->initTtlHi;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (out->ttl == (time_t)NO_VALUE_SET &&
2200Sstevel@tonic-gate 		in->ttl != (time_t)NO_VALUE_SET)
2210Sstevel@tonic-gate 		out->ttl = in->ttl;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (!out->numRulesFromLDAP && in->numRulesFromLDAP) {
2240Sstevel@tonic-gate 		out->ruleFromLDAP = dup_mapping_rules(in->ruleFromLDAP,
2250Sstevel@tonic-gate 			in->numRulesFromLDAP);
2260Sstevel@tonic-gate 		if (!out->ruleFromLDAP)
2270Sstevel@tonic-gate 			return (1);
2280Sstevel@tonic-gate 		out->numRulesFromLDAP = in->numRulesFromLDAP;
2290Sstevel@tonic-gate 	} else if (append && out->numRulesFromLDAP && in->numRulesFromLDAP) {
2300Sstevel@tonic-gate 		orig_num_rules = out->numRulesFromLDAP;
2310Sstevel@tonic-gate 		for (i = 0; i < in->numRulesFromLDAP; i++) {
2320Sstevel@tonic-gate 			if (append_mapping_rule(in->ruleFromLDAP[i], out, 0)) {
2330Sstevel@tonic-gate 				for (i = out->numRulesFromLDAP;
2340Sstevel@tonic-gate 					i > orig_num_rules; i--) {
2350Sstevel@tonic-gate 					free_mapping_rule(out->ruleFromLDAP[i]);
2360Sstevel@tonic-gate 					out->ruleFromLDAP[i] = NULL;
2370Sstevel@tonic-gate 				}
2380Sstevel@tonic-gate 				return (1);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (!out->numRulesToLDAP && in->numRulesToLDAP) {
2450Sstevel@tonic-gate 		out->ruleToLDAP = dup_mapping_rules(in->ruleToLDAP,
2460Sstevel@tonic-gate 			in->numRulesToLDAP);
2470Sstevel@tonic-gate 		if (!out->ruleToLDAP)
2480Sstevel@tonic-gate 			return (1);
2490Sstevel@tonic-gate 		out->numRulesToLDAP = in->numRulesToLDAP;
2500Sstevel@tonic-gate 	} else if (append && out->numRulesToLDAP && in->numRulesToLDAP) {
2510Sstevel@tonic-gate 		orig_num_rules = out->numRulesToLDAP;
2520Sstevel@tonic-gate 		for (i = 0; i < in->numRulesToLDAP; i++) {
2530Sstevel@tonic-gate 			if (append_mapping_rule(in->ruleToLDAP[i], out, 1)) {
2540Sstevel@tonic-gate 				for (i = out->numRulesToLDAP;
2550Sstevel@tonic-gate 					i > orig_num_rules; i--) {
2560Sstevel@tonic-gate 					free_mapping_rule(out->ruleToLDAP[i]);
2570Sstevel@tonic-gate 					out->ruleToLDAP[i] = NULL;
2580Sstevel@tonic-gate 				}
2590Sstevel@tonic-gate 				return (1);
2600Sstevel@tonic-gate 			}
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 	if (!out->objectDN && in->objectDN) {
2640Sstevel@tonic-gate 		out->objectDN = (__nis_object_dn_t *)
2650Sstevel@tonic-gate 			s_calloc(1, sizeof (__nis_object_dn_t));
2660Sstevel@tonic-gate 		if (!out->objectDN)
2670Sstevel@tonic-gate 			return (2);
2680Sstevel@tonic-gate 		if (copy_object_dn(in->objectDN, out->objectDN)) {
2690Sstevel@tonic-gate 			free_object_dn(out->objectDN);
2700Sstevel@tonic-gate 			out->objectDN = NULL;
2710Sstevel@tonic-gate 			return (1);
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (!out->objName && in->objName) {
2760Sstevel@tonic-gate 		if (!strchr(in->objName, SPACE_CHAR)) {
2770Sstevel@tonic-gate 		/* objName has no space- a single map dbIdMapping */
2780Sstevel@tonic-gate 			out->objName = s_strndup(in->objName,
2790Sstevel@tonic-gate 				strlen(in->objName));
2800Sstevel@tonic-gate 			if (!out->objName)
2810Sstevel@tonic-gate 				return (2);
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (!out->objName && out->dbId) {
2860Sstevel@tonic-gate 		out->objName = s_strndup(out->dbId, strlen(out->dbId));
2870Sstevel@tonic-gate 		if (!out->objName)
2880Sstevel@tonic-gate 			return (2);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (out->seq_num == NO_VALUE_SET && in->seq_num >= 0)
2920Sstevel@tonic-gate 		out->seq_num = in->seq_num;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (p_error == no_parse_error ? 0 : 1);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * FUNCTION:	copy_object_dn
2990Sstevel@tonic-gate  *
3000Sstevel@tonic-gate  * Copies a __nis_object_dn_t structure.
3010Sstevel@tonic-gate  *
3020Sstevel@tonic-gate  * RETURN:	0 on success, > 0 on failure.
3030Sstevel@tonic-gate  *
3040Sstevel@tonic-gate  * NOTE:	The caller MUST free newdn using
3050Sstevel@tonic-gate  *		free_object_dn() if return value != 0 (error condition)
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static int
copy_object_dn(__nis_object_dn_t * in,__nis_object_dn_t * newdn)3090Sstevel@tonic-gate copy_object_dn(
3100Sstevel@tonic-gate 	__nis_object_dn_t	*in,
3110Sstevel@tonic-gate 	__nis_object_dn_t	*newdn)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	if (in == NULL) {
3140Sstevel@tonic-gate 		p_error = parse_no_object_dn;
3150Sstevel@tonic-gate 		return (1);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	while (in != NULL) {
3180Sstevel@tonic-gate 		if (in->read.base == NULL) {
3190Sstevel@tonic-gate 			newdn->read.base = NULL;
3200Sstevel@tonic-gate 		} else {
3210Sstevel@tonic-gate 			newdn->read.base = s_strndup(
3220Sstevel@tonic-gate 				in->read.base,
3230Sstevel@tonic-gate 				strlen(in->read.base));
3240Sstevel@tonic-gate 			if (newdn->read.base == NULL)
3250Sstevel@tonic-gate 				return (2);
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 		newdn->read.scope = in->read.scope;
3280Sstevel@tonic-gate 		if (in->read.attrs) {
3290Sstevel@tonic-gate 			newdn->read.attrs = s_strndup(
3300Sstevel@tonic-gate 					in->read.attrs,
3310Sstevel@tonic-gate 					strlen(in->read.attrs));
3320Sstevel@tonic-gate 			if (newdn->read.attrs == NULL) {
3330Sstevel@tonic-gate 				return (2);
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 		} else {
3360Sstevel@tonic-gate 			newdn->read.attrs = NULL;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 		newdn->read.element = in->read.element;
3390Sstevel@tonic-gate 		if (in->write.base != NULL) {
3400Sstevel@tonic-gate 			newdn->write.base = s_strndup(
3410Sstevel@tonic-gate 				in->write.base,
3420Sstevel@tonic-gate 				strlen(in->write.base));
3430Sstevel@tonic-gate 			if (newdn->write.base == NULL)
3440Sstevel@tonic-gate 				return (2);
3450Sstevel@tonic-gate 		} else {
3460Sstevel@tonic-gate 			newdn->write.base = NULL;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 		newdn->write.scope = in->write.scope;
3490Sstevel@tonic-gate 		if (in->write.attrs != NULL) {
3500Sstevel@tonic-gate 			newdn->write.attrs = s_strndup(
3510Sstevel@tonic-gate 				in->write.attrs,
3520Sstevel@tonic-gate 				strlen(in->write.attrs));
3530Sstevel@tonic-gate 			if (newdn->write.attrs == NULL) {
3540Sstevel@tonic-gate 				return (2);
3550Sstevel@tonic-gate 			}
3560Sstevel@tonic-gate 		} else {
3570Sstevel@tonic-gate 			newdn->write.attrs = NULL;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 		newdn->write.element = in->write.element;
3600Sstevel@tonic-gate 		if (in->dbIdName) {
3610Sstevel@tonic-gate 			newdn->dbIdName = s_strndup(in->dbIdName,
3620Sstevel@tonic-gate 						strlen(in->dbIdName));
3630Sstevel@tonic-gate 			if (newdn->dbIdName == NULL)
3640Sstevel@tonic-gate 				return (2);
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		if (in->delDisp)
3680Sstevel@tonic-gate 			newdn->delDisp = in->delDisp;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		if (in->dbId && in->numDbIds > 0) {
3710Sstevel@tonic-gate 			newdn->dbId = dup_mapping_rules(in->dbId,
3720Sstevel@tonic-gate 					in->numDbIds);
3730Sstevel@tonic-gate 			if (!newdn->dbId)
3740Sstevel@tonic-gate 				return (1);
3750Sstevel@tonic-gate 			newdn->numDbIds = in->numDbIds;
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		if (in->next != NULL) {
3780Sstevel@tonic-gate 			newdn->next = (__nis_object_dn_t *)s_calloc(1,
3790Sstevel@tonic-gate 					sizeof (__nis_object_dn_t));
3800Sstevel@tonic-gate 			if (newdn->next == NULL)
3810Sstevel@tonic-gate 				return (1);
3820Sstevel@tonic-gate 			newdn = newdn->next;
3830Sstevel@tonic-gate 			in = in->next;
3840Sstevel@tonic-gate 		} else {
3850Sstevel@tonic-gate 			return (0);
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	} /* End of while on in */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	return (0);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * FUNCTION:	free_yp_domain_context
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  * Frees __yp_domain_context_t
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * INPUT:		__yp_domain_context_t
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate void
free_yp_domain_context(__yp_domain_context_t * domains)4000Sstevel@tonic-gate free_yp_domain_context(__yp_domain_context_t *domains)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	int i;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (domains != NULL) {
4050Sstevel@tonic-gate 		for (i = 0; i < domains->numDomains; i++) {
4060Sstevel@tonic-gate 			if (domains->domains[i] != NULL) {
4070Sstevel@tonic-gate 				free(domains->domains[i]);
4080Sstevel@tonic-gate 				domains->domains[i] = NULL;
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 			if (domains->domainLabels[i] != NULL) {
4110Sstevel@tonic-gate 				free(domains->domainLabels[i]);
4120Sstevel@tonic-gate 				domains->domainLabels[i] = NULL;
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 		domains->domains = NULL;
4160Sstevel@tonic-gate 		domains->domainLabels = NULL;
4170Sstevel@tonic-gate 		for (i = 0; i < domains->numYppasswdd; i++) {
4180Sstevel@tonic-gate 			if (domains->yppasswddDomainLabels[i] != NULL) {
4190Sstevel@tonic-gate 				free(domains->yppasswddDomainLabels[i]);
4200Sstevel@tonic-gate 				domains->yppasswddDomainLabels[i]
4210Sstevel@tonic-gate 					= NULL;
4220Sstevel@tonic-gate 			}
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 		domains->yppasswddDomainLabels = NULL;
4250Sstevel@tonic-gate 		domains->numDomains = 0;
4260Sstevel@tonic-gate 		domains = NULL;
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * FUNCTION:	second_parser_pass
4320Sstevel@tonic-gate  *
4330Sstevel@tonic-gate  * Prepares the linked list of table_mappings for processing
4340Sstevel@tonic-gate  * by finish_parse(), adding, merging and deleting structures
4350Sstevel@tonic-gate  * as necessary. Also adds dummy objectDN info. for splitField's.
4360Sstevel@tonic-gate  *
4370Sstevel@tonic-gate  * RETURN VALUE: 0 on success, > 0 on failure.
4380Sstevel@tonic-gate  */
4390Sstevel@tonic-gate int
second_parser_pass(__nis_table_mapping_t ** table_mapping)4400Sstevel@tonic-gate second_parser_pass(
4410Sstevel@tonic-gate 	__nis_table_mapping_t   **table_mapping)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	__nis_table_mapping_t   *t, *t2;
4440Sstevel@tonic-gate 	__nis_table_mapping_t   *t_new = NULL, *tg;
4450Sstevel@tonic-gate 	__nis_table_mapping_t	*prev = NULL;
4460Sstevel@tonic-gate 	__nis_object_dn_t   *objectDN;
4470Sstevel@tonic-gate 	char	*objs, *dom;
4480Sstevel@tonic-gate 	char	*objName = NULL;
4490Sstevel@tonic-gate 	char	*lasts;
4500Sstevel@tonic-gate 	char	*tobj, *alias, *dupalias, *tmp;
4510Sstevel@tonic-gate 	char	*myself = "second_parser_pass";
4520Sstevel@tonic-gate 	int	i = 0, len;
4530Sstevel@tonic-gate 	int	remove_t = 0;
4540Sstevel@tonic-gate 	int	add_t = 0;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	prev = NULL;
4570Sstevel@tonic-gate 	for (t = *table_mapping; t != NULL; ) {
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * Temporarily using this field to flag deletion.
4600Sstevel@tonic-gate 		 * 0 : don't delete
4610Sstevel@tonic-gate 		 * 1 : delete
4620Sstevel@tonic-gate 		 * The mapping structure will be deleted in final_parser_pass
4630Sstevel@tonic-gate 		 */
4640Sstevel@tonic-gate 		t->isMaster = 0;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		if (!t->dbId) {
4670Sstevel@tonic-gate 			p_error = parse_bad_map_error;
4680Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
4690Sstevel@tonic-gate 			"%s: no dbId field", myself);
4700Sstevel@tonic-gate 			return (1);
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 		tg = NULL;
4730Sstevel@tonic-gate 		dom = strchr(t->dbId, COMMA_CHAR);
4740Sstevel@tonic-gate 		if (t->objName != NULL) {
4750Sstevel@tonic-gate 			objName = strdup(t->objName);
4760Sstevel@tonic-gate 			if (objName == NULL) {
4770Sstevel@tonic-gate 				p_error = parse_no_mem_error;
4780Sstevel@tonic-gate 				logmsg(MSG_NOMEM, LOG_ERR,
4790Sstevel@tonic-gate 		"%s: Cannot allocate memory for objName", myself);
4800Sstevel@tonic-gate 				return (1);
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 			objs = (char *)strtok_r(objName, " ", &lasts);
4830Sstevel@tonic-gate 			/* Get the generic mapping */
4840Sstevel@tonic-gate 			if (dom != NULL) {
4850Sstevel@tonic-gate 				tg = find_table_mapping(t->dbId, dom - t->dbId,
4860Sstevel@tonic-gate 								*table_mapping);
4870Sstevel@tonic-gate 			}
4880Sstevel@tonic-gate 		} else {
4890Sstevel@tonic-gate 			objs = NULL;
4900Sstevel@tonic-gate 			if (dom == NULL) {
4910Sstevel@tonic-gate 				t->objName = s_strndup(t->dbId,
4920Sstevel@tonic-gate 						strlen(t->dbId));
4930Sstevel@tonic-gate 				if (!t->objName) {
4940Sstevel@tonic-gate 					logmsg(MSG_NOMEM, LOG_ERR,
4950Sstevel@tonic-gate "%s: Cannot allocate memory for t->objName",
4960Sstevel@tonic-gate 						myself);
4970Sstevel@tonic-gate 					objs = NULL;
4980Sstevel@tonic-gate 					return (2);
4990Sstevel@tonic-gate 				}
5000Sstevel@tonic-gate 			} else {
5010Sstevel@tonic-gate 				/* Force relationship for domain specific */
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 				/* Get the generic mapping */
5040Sstevel@tonic-gate 				tg = find_table_mapping(t->dbId, dom - t->dbId,
5050Sstevel@tonic-gate 								*table_mapping);
5060Sstevel@tonic-gate 				if (tg == NULL || tg->objName == NULL) {
5070Sstevel@tonic-gate 					/* If not found, use dbId for objName */
5080Sstevel@tonic-gate 					t->objName = s_strndup(t->dbId,
5090Sstevel@tonic-gate 							strlen(t->dbId));
5100Sstevel@tonic-gate 					if (t->objName == NULL) {
5110Sstevel@tonic-gate 						logmsg(MSG_NOMEM, LOG_ERR,
5120Sstevel@tonic-gate "%s: Cannot allocate memory for t->objName",
5130Sstevel@tonic-gate 							myself);
5140Sstevel@tonic-gate 						return (2);
5150Sstevel@tonic-gate 					}
5160Sstevel@tonic-gate 				} else {
5170Sstevel@tonic-gate 					dom++;
5180Sstevel@tonic-gate 					tobj = s_strndup(tg->objName,
5190Sstevel@tonic-gate 							strlen(tg->objName));
5200Sstevel@tonic-gate 					if (tobj == NULL) {
5210Sstevel@tonic-gate 						logmsg(MSG_NOMEM, LOG_ERR,
5220Sstevel@tonic-gate "%s: Cannot allocate memory for t->objName",
5230Sstevel@tonic-gate 							myself);
5240Sstevel@tonic-gate 						return (2);
5250Sstevel@tonic-gate 					}
5260Sstevel@tonic-gate 					alias = (char *)strtok_r(tobj, " ",
5270Sstevel@tonic-gate 									&lasts);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 					/* Loop 'breaks' on errors */
5300Sstevel@tonic-gate 					while (alias) {
5310Sstevel@tonic-gate 						tmp = NULL;
5320Sstevel@tonic-gate 						dupalias = s_strndup(alias,
5330Sstevel@tonic-gate 								strlen(alias));
5340Sstevel@tonic-gate 						if (!dupalias)
5350Sstevel@tonic-gate 							break;
5360Sstevel@tonic-gate 						if (getfullmapname(&dupalias,
5370Sstevel@tonic-gate 								dom)) {
5380Sstevel@tonic-gate 							i = 1;
5390Sstevel@tonic-gate 							break;
5400Sstevel@tonic-gate 						}
5410Sstevel@tonic-gate 						if (t->objName == NULL)
5420Sstevel@tonic-gate 							t->objName = dupalias;
5430Sstevel@tonic-gate 						else {
5440Sstevel@tonic-gate 							len = strlen(t->objName)
5450Sstevel@tonic-gate 							+ strlen(dupalias) + 2;
5460Sstevel@tonic-gate 							tmp = s_calloc(1, len);
5470Sstevel@tonic-gate 							if (tmp == NULL)
5480Sstevel@tonic-gate 								break;
5490Sstevel@tonic-gate 							snprintf(tmp, len,
5500Sstevel@tonic-gate 								"%s %s",
5510Sstevel@tonic-gate 								t->objName,
5520Sstevel@tonic-gate 								dupalias);
5530Sstevel@tonic-gate 							free(dupalias);
5540Sstevel@tonic-gate 							dupalias = NULL;
5550Sstevel@tonic-gate 							free(t->objName);
5560Sstevel@tonic-gate 							t->objName = tmp;
5570Sstevel@tonic-gate 						}
5580Sstevel@tonic-gate 						alias = (char *)strtok_r(NULL,
5590Sstevel@tonic-gate 								" ", &lasts);
5600Sstevel@tonic-gate 					}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 					if (tobj)
5630Sstevel@tonic-gate 						free(tobj);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 					if (alias ||
5660Sstevel@tonic-gate 						(objName = s_strdup(t->objName))
5670Sstevel@tonic-gate 								== NULL) {
5680Sstevel@tonic-gate 						if (i)
5690Sstevel@tonic-gate 							logmsg(MSG_NOTIMECHECK,
5700Sstevel@tonic-gate 							LOG_ERR,
5710Sstevel@tonic-gate "%s: getfullmapname failed for %s for domain \"%s\"",
5720Sstevel@tonic-gate 							myself, dupalias, dom);
5730Sstevel@tonic-gate 						else {
5740Sstevel@tonic-gate 							p_error =
5750Sstevel@tonic-gate 							parse_no_mem_error;
5760Sstevel@tonic-gate 							logmsg(MSG_NOMEM,
5770Sstevel@tonic-gate 							LOG_ERR,
5780Sstevel@tonic-gate "%s: Cannot allocate memory",
5790Sstevel@tonic-gate 							myself);
5800Sstevel@tonic-gate 						}
5810Sstevel@tonic-gate 						if (dupalias)
5820Sstevel@tonic-gate 							free(dupalias);
5830Sstevel@tonic-gate 						if (t->objName)
5840Sstevel@tonic-gate 							free(t->objName);
5850Sstevel@tonic-gate 						return (2);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 					}
5880Sstevel@tonic-gate 					objs = (char *)strtok_r(objName, " ",
5890Sstevel@tonic-gate 								&lasts);
5900Sstevel@tonic-gate 				}
5910Sstevel@tonic-gate 			}
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		if (tg != NULL) {
5950Sstevel@tonic-gate 			if (merge_table_mapping(tg, t)) {
5960Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
5970Sstevel@tonic-gate "Error merging information from the %s to the %s mapping structure",
5980Sstevel@tonic-gate 					tg->dbId, t->dbId);
5990Sstevel@tonic-gate 				objs = NULL;
6000Sstevel@tonic-gate 				if (objName)
6010Sstevel@tonic-gate 					free(objName);
6020Sstevel@tonic-gate 				return (1);
6030Sstevel@tonic-gate 			}
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		/*
6070Sstevel@tonic-gate 		 * If objName is "map1 map2" then do the second pass.
6080Sstevel@tonic-gate 		 * If it is just "map1" however skip the expansion.
6090Sstevel@tonic-gate 		 * Also skip it if t->objName is null.
6100Sstevel@tonic-gate 		 */
6110Sstevel@tonic-gate 		if (objs && strncasecmp(objs, t->objName,
6120Sstevel@tonic-gate 			strlen(t->objName))) {
6130Sstevel@tonic-gate 			t2 = find_table_mapping(objs, strlen(objs),
6140Sstevel@tonic-gate 							*table_mapping);
6150Sstevel@tonic-gate 			if (t2) {
6160Sstevel@tonic-gate 				if (merge_table_mapping(t, t2)) {
6170Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
6180Sstevel@tonic-gate "Error merging information from the %s to the %s mapping structure",
6190Sstevel@tonic-gate 						t->dbId, t2->dbId);
6200Sstevel@tonic-gate 					objs = NULL;
6210Sstevel@tonic-gate 					if (objName)
6220Sstevel@tonic-gate 						free(objName);
6230Sstevel@tonic-gate 					return (1);
6240Sstevel@tonic-gate 				}
6250Sstevel@tonic-gate 				t->isMaster = 1;
6260Sstevel@tonic-gate 			} else {
6270Sstevel@tonic-gate 				t_new = new_merged_mapping(objs, t);
6280Sstevel@tonic-gate 				if (t_new) {
6290Sstevel@tonic-gate 					t->isMaster = 1;
6300Sstevel@tonic-gate 					if (prev != NULL)
6310Sstevel@tonic-gate 						prev->next = t_new;
6320Sstevel@tonic-gate 					else
6330Sstevel@tonic-gate 						*table_mapping = t_new;
6340Sstevel@tonic-gate 					prev = t_new;
6350Sstevel@tonic-gate 					prev->next = t;
6360Sstevel@tonic-gate 				} else {
6370Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
6380Sstevel@tonic-gate "Error creating a new mapping structure %s",
6390Sstevel@tonic-gate 						objs);
6400Sstevel@tonic-gate 					objs = NULL;
6410Sstevel@tonic-gate 					if (objName)
6420Sstevel@tonic-gate 						free(objName);
6430Sstevel@tonic-gate 					return (1);
6440Sstevel@tonic-gate 				}
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 			while ((objs = (char *)strtok_r(NULL, " ", &lasts))
6470Sstevel@tonic-gate 				!= NULL) {
6480Sstevel@tonic-gate 				t2 = find_table_mapping(objs, strlen(objs),
6490Sstevel@tonic-gate 								*table_mapping);
6500Sstevel@tonic-gate 				if (t2) {
6510Sstevel@tonic-gate 					if (merge_table_mapping(t, t2)) {
6520Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
6530Sstevel@tonic-gate "Error merging information from the %s to the %s mapping structure",
6540Sstevel@tonic-gate 							t->dbId, t2->dbId);
6550Sstevel@tonic-gate 						objs = NULL;
6560Sstevel@tonic-gate 						if (objName)
6570Sstevel@tonic-gate 							free(objName);
6580Sstevel@tonic-gate 						return (1);
6590Sstevel@tonic-gate 					}
6600Sstevel@tonic-gate 					t->isMaster = 1;
6610Sstevel@tonic-gate 				} else {
6620Sstevel@tonic-gate 					/*
6630Sstevel@tonic-gate 					 * create a new t_map with dbId = objs
6640Sstevel@tonic-gate 					 * and copy t->* into new t_map
6650Sstevel@tonic-gate 					 */
6660Sstevel@tonic-gate 					t_new = new_merged_mapping(objs, t);
6670Sstevel@tonic-gate 					if (t_new) {
6680Sstevel@tonic-gate 						t->isMaster = 1;
6690Sstevel@tonic-gate 						if (prev != NULL)
6700Sstevel@tonic-gate 							prev->next = t_new;
6710Sstevel@tonic-gate 						else
6720Sstevel@tonic-gate 							*table_mapping = t_new;
6730Sstevel@tonic-gate 						prev = t_new;
6740Sstevel@tonic-gate 						prev->next = t;
6750Sstevel@tonic-gate 					} else {
6760Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
6770Sstevel@tonic-gate "Error creating a new mapping structure %s",
6780Sstevel@tonic-gate 							objs);
6790Sstevel@tonic-gate 						objs = NULL;
6800Sstevel@tonic-gate 						if (objName)
6810Sstevel@tonic-gate 							free(objName);
6820Sstevel@tonic-gate 						return (1);
6830Sstevel@tonic-gate 					}
6840Sstevel@tonic-gate 				}
6850Sstevel@tonic-gate 			}
6860Sstevel@tonic-gate 		} /* if objs!= NULL */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		prev = t;
6890Sstevel@tonic-gate 		t = t->next;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		if (objName) {
6920Sstevel@tonic-gate 			free(objName);
6930Sstevel@tonic-gate 			objName = NULL;
6940Sstevel@tonic-gate 			objs = NULL;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	} /* for t = table_mapping loop */
6970Sstevel@tonic-gate 	return (0);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate __nis_table_mapping_t *
new_merged_mapping(const char * match,__nis_table_mapping_t * intbl)7010Sstevel@tonic-gate new_merged_mapping(const char *match,
7020Sstevel@tonic-gate 	__nis_table_mapping_t	*intbl)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	__nis_table_mapping_t	*outtable = NULL;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	outtable = (__nis_table_mapping_t *)
7080Sstevel@tonic-gate 		s_calloc(1, sizeof (__nis_table_mapping_t));
7090Sstevel@tonic-gate 	if (outtable == NULL)
7100Sstevel@tonic-gate 		return (NULL);
7110Sstevel@tonic-gate 	initialize_table_mapping(outtable);
7120Sstevel@tonic-gate 	outtable->dbId = s_strndup(match, strlen(match));
7130Sstevel@tonic-gate 	if (outtable->dbId == NULL) {
7140Sstevel@tonic-gate 		free_table_mapping(outtable);
7150Sstevel@tonic-gate 		outtable = NULL;
7160Sstevel@tonic-gate 		return (NULL);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	if (merge_table_mapping(intbl, outtable)) {
7190Sstevel@tonic-gate 		free_table_mapping(outtable);
7200Sstevel@tonic-gate 		outtable = NULL;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 	return (outtable);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * FUNCTION:	final_parser_pass
7270Sstevel@tonic-gate  *
7280Sstevel@tonic-gate  * completes the final expansion of t_map structures linked list.
7290Sstevel@tonic-gate  * all structures will have a non-null objPath as well as a objName
7300Sstevel@tonic-gate  * in the form of "mapname . domainname ." or "splitfieldname .
7310Sstevel@tonic-gate  * domainname .".
7320Sstevel@tonic-gate  *
7330Sstevel@tonic-gate  * RETURN VALUE:	0 on success, -1 on failure, -2 on fatal error.
7340Sstevel@tonic-gate  */
7350Sstevel@tonic-gate int
final_parser_pass(__nis_table_mapping_t ** table_mapping,__yp_domain_context_t * ypDomains)7360Sstevel@tonic-gate final_parser_pass(
7370Sstevel@tonic-gate 	__nis_table_mapping_t   **table_mapping,
7380Sstevel@tonic-gate 	__yp_domain_context_t   *ypDomains)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	__nis_table_mapping_t   *t;
7410Sstevel@tonic-gate 	__nis_table_mapping_t	*t1, *returned_map;
7420Sstevel@tonic-gate 	__nis_table_mapping_t   *prev = NULL;
7430Sstevel@tonic-gate 	int			i;
7440Sstevel@tonic-gate 	char			*myself = "final_parser_pass";
7450Sstevel@tonic-gate 	int			nm;
7460Sstevel@tonic-gate 	bool_t			r;
7470Sstevel@tonic-gate 	int			del_tbl_flag = 0;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (ypDomains) {
7500Sstevel@tonic-gate 		if (!ypDomains->numDomains) {
7510Sstevel@tonic-gate 			p_error = parse_internal_error;
7520Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
7530Sstevel@tonic-gate 				"%s:No domains specified.", myself);
7540Sstevel@tonic-gate 			return (-1);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 	} else {
7570Sstevel@tonic-gate 		p_error = parse_internal_error;
7580Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
7590Sstevel@tonic-gate 				"%s:No domain structure supplied.", myself);
7600Sstevel@tonic-gate 		return (-1);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 	prev = NULL;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	for (t = *table_mapping; t != NULL; ) {
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		/* Delete if marked for deletion by second_parser_pass */
7670Sstevel@tonic-gate 		if (t->isMaster == 1) {
7680Sstevel@tonic-gate 			if (prev != NULL)
7690Sstevel@tonic-gate 				prev->next = t->next;
7700Sstevel@tonic-gate 			else
7710Sstevel@tonic-gate 				*table_mapping = t->next;
7720Sstevel@tonic-gate 			t1 = t;
7730Sstevel@tonic-gate 			t = t->next;
7740Sstevel@tonic-gate 			free_table_mapping(t1);
7750Sstevel@tonic-gate 			continue;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		if (!t->objName && t->dbId) {
7790Sstevel@tonic-gate 			t->objName = s_strndup(t->dbId, strlen(t->dbId));
7800Sstevel@tonic-gate 			if (!t->objName) {
7810Sstevel@tonic-gate 				logmsg(MSG_NOMEM, LOG_ERR,
7820Sstevel@tonic-gate 					"%s:Could not allocate.", myself);
7830Sstevel@tonic-gate 				return (-1);
7840Sstevel@tonic-gate 			}
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 		i = ypDomains->numDomains;
7870Sstevel@tonic-gate 		while (i > 0) {
7880Sstevel@tonic-gate 			if (i == 1) {
7890Sstevel@tonic-gate 			/* modify existing table_mapping's */
7900Sstevel@tonic-gate 				nm = checkfullmapname(t->dbId,
7910Sstevel@tonic-gate 					ypDomains->domainLabels[0],
7920Sstevel@tonic-gate 					table_mapping, &returned_map);
7930Sstevel@tonic-gate 				if (nm == 1) {
7940Sstevel@tonic-gate 					/* delete this mapping structure */
7950Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK,
7960Sstevel@tonic-gate 						LOG_WARNING,
7970Sstevel@tonic-gate 						"Mapping structure %s,%s "
7980Sstevel@tonic-gate 						"already exists.",
7990Sstevel@tonic-gate 						t->dbId,
8000Sstevel@tonic-gate 						ypDomains->domainLabels[0]);
8010Sstevel@tonic-gate 					if (merge_table_mapping(t,
8020Sstevel@tonic-gate 						returned_map)) {
8030Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
8040Sstevel@tonic-gate 						    "Error merging information "
8050Sstevel@tonic-gate 						    "from the %s to the %s "
8060Sstevel@tonic-gate 						    "mapping structure.",
8070Sstevel@tonic-gate 						t->dbId, returned_map->dbId);
8080Sstevel@tonic-gate 						return (-1);
8090Sstevel@tonic-gate 					}
8100Sstevel@tonic-gate 					if (del_tbl_flag == 0)
8110Sstevel@tonic-gate 						del_tbl_flag = 1;
8120Sstevel@tonic-gate 				} else if (nm == -1) {
8130Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
8140Sstevel@tonic-gate 			"Error searching for %s,%s structure",
8150Sstevel@tonic-gate 					t->dbId, ypDomains->domainLabels[0]);
8160Sstevel@tonic-gate 					return (-1);
8170Sstevel@tonic-gate 				} else if (nm == 0 || nm == 2) {
8180Sstevel@tonic-gate 					if ((append_domainContext(&t,
8190Sstevel@tonic-gate 					ypDomains->domainLabels[0],
8200Sstevel@tonic-gate 					ypDomains->domains[0])) != 0) {
8210Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
8220Sstevel@tonic-gate 					"Error appending domainContext %s",
8230Sstevel@tonic-gate 						ypDomains->domainLabels[0]);
8240Sstevel@tonic-gate 						return (-1);
8250Sstevel@tonic-gate 					}
8260Sstevel@tonic-gate 					del_tbl_flag = 0;
8270Sstevel@tonic-gate 				}
8280Sstevel@tonic-gate 			} else { /* if (i > 1) */
8290Sstevel@tonic-gate 				/* need to create new table_mapping's */
8300Sstevel@tonic-gate 				nm = checkfullmapname(t->dbId,
8310Sstevel@tonic-gate 					ypDomains->domainLabels[i - 1],
8320Sstevel@tonic-gate 					table_mapping, &returned_map);
8330Sstevel@tonic-gate 				if (nm == -1) {
8340Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
8350Sstevel@tonic-gate 				"Error searching for %s,%s structure",
8360Sstevel@tonic-gate 				t->dbId, ypDomains->domainLabels[i - 1]);
8370Sstevel@tonic-gate 					return (-1);
8380Sstevel@tonic-gate 				} else if (nm == 0) {
8390Sstevel@tonic-gate 					t1 = new_merged_mapping(t->dbId, t);
8400Sstevel@tonic-gate 					/* we clone ourselves */
8410Sstevel@tonic-gate 					if (t1) {
8420Sstevel@tonic-gate 						if ((append_domainContext(&t1,
8430Sstevel@tonic-gate 					ypDomains->domainLabels[i - 1],
8440Sstevel@tonic-gate 					ypDomains->domains[i - 1])) != 0) {
8450Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
8460Sstevel@tonic-gate 					"Error appending domainContext %s",
8470Sstevel@tonic-gate 					ypDomains->domainLabels[i - 1]);
8480Sstevel@tonic-gate 							free(t1);
8490Sstevel@tonic-gate 							return (-1);
8500Sstevel@tonic-gate 						}
8510Sstevel@tonic-gate 						if (prev != NULL) {
8520Sstevel@tonic-gate 							t1->next = prev->next;
8530Sstevel@tonic-gate 							prev->next = t1;
8540Sstevel@tonic-gate 							prev = prev->next;
8550Sstevel@tonic-gate 						} else {
8560Sstevel@tonic-gate 							t1->next =
8570Sstevel@tonic-gate 								*table_mapping;
8580Sstevel@tonic-gate 							*table_mapping = t1;
8590Sstevel@tonic-gate 							prev = t1;
8600Sstevel@tonic-gate 						}
8610Sstevel@tonic-gate 					} else { /* if !t1 */
8620Sstevel@tonic-gate 						p_error = parse_internal_error;
8630Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
8640Sstevel@tonic-gate 					"%s:Could not create new table -"
8650Sstevel@tonic-gate 					" check all instances of %s for errors",
8660Sstevel@tonic-gate 						myself, t->dbId);
8670Sstevel@tonic-gate 						return (-1);
8680Sstevel@tonic-gate 					}
8690Sstevel@tonic-gate 				} else if (nm == 1) {
8700Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
8710Sstevel@tonic-gate 				"Mapping structure %s,%s already exists.",
8720Sstevel@tonic-gate 						t->dbId,
8730Sstevel@tonic-gate 						ypDomains->domainLabels[i - 1]);
8740Sstevel@tonic-gate 					/*
8750Sstevel@tonic-gate 					 * We should be deleting this, but can't
8760Sstevel@tonic-gate 					 * really do it here, because we need to
8770Sstevel@tonic-gate 					 * match with the domainLabels[0] case
8780Sstevel@tonic-gate 					 * too. So we will just flag it for now.
8790Sstevel@tonic-gate 					 */
8800Sstevel@tonic-gate 					if (merge_table_mapping(t,
8810Sstevel@tonic-gate 						returned_map)) {
8820Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
8830Sstevel@tonic-gate 	"Error merging information from the %s to the %s mapping structure.",
8840Sstevel@tonic-gate 							t->dbId,
8850Sstevel@tonic-gate 							returned_map->dbId);
8860Sstevel@tonic-gate 						return (-1);
8870Sstevel@tonic-gate 					}
8880Sstevel@tonic-gate 					del_tbl_flag = 1;
8890Sstevel@tonic-gate 				} else if (nm == 2) {
8900Sstevel@tonic-gate 					if ((append_domainContext(&t,
8910Sstevel@tonic-gate 						ypDomains->domainLabels[i - 1],
8920Sstevel@tonic-gate 						ypDomains->domains[i - 1]))
8930Sstevel@tonic-gate 						!= 0) {
8940Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
8950Sstevel@tonic-gate 					"Error appending domainContext %s",
8960Sstevel@tonic-gate 						ypDomains->domainLabels[i - 1]);
8970Sstevel@tonic-gate 						return (-1);
8980Sstevel@tonic-gate 					}
8990Sstevel@tonic-gate 				} /* end of "if (nm == 0)" */
9000Sstevel@tonic-gate 			} /* end of else if (i > 1) */
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 			/*
9040Sstevel@tonic-gate 			 * 'merge_table_mapping' only copies unexpanded
9050Sstevel@tonic-gate 			 * objectDN values into returned_map. Hence,
9060Sstevel@tonic-gate 			 * read.base and write.base in returned_map
9070Sstevel@tonic-gate 			 * needs to be expanded.
9080Sstevel@tonic-gate 			 */
9090Sstevel@tonic-gate 			if (nm == 1 && returned_map && returned_map->objectDN) {
9100Sstevel@tonic-gate 				r = make_fqdn(
9110Sstevel@tonic-gate 					returned_map->objectDN,
9120Sstevel@tonic-gate 					ypDomains->domains[i - 1]);
9130Sstevel@tonic-gate 				if (r == TRUE &&
9140Sstevel@tonic-gate 					returned_map->objectDN->write.base) {
9150Sstevel@tonic-gate 					r = make_full_dn(
9160Sstevel@tonic-gate 					&returned_map->objectDN->write.base,
9170Sstevel@tonic-gate 					ypDomains->domains[i - 1]);
9180Sstevel@tonic-gate 				}
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 				if (r == FALSE) {
9210Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
9220Sstevel@tonic-gate 						"Error appending domainContext "
9230Sstevel@tonic-gate 						"%s to %s",
9240Sstevel@tonic-gate 						ypDomains->domainLabels[i - 1],
9250Sstevel@tonic-gate 						returned_map->dbId);
9260Sstevel@tonic-gate 					return (-2);
9270Sstevel@tonic-gate 				}
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 			i--;
9300Sstevel@tonic-gate 		} /* end of while i > 0 loop */
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		if (del_tbl_flag == 1) {
9330Sstevel@tonic-gate 			if (prev != NULL) {
9340Sstevel@tonic-gate 				prev->next = t->next;
9350Sstevel@tonic-gate 				free_table_mapping(t);
9360Sstevel@tonic-gate 				t = prev->next;
9370Sstevel@tonic-gate 			} else {
9380Sstevel@tonic-gate 				*table_mapping = t->next;
9390Sstevel@tonic-gate 				free_table_mapping(t);
9400Sstevel@tonic-gate 				t = *table_mapping;
9410Sstevel@tonic-gate 			}
9420Sstevel@tonic-gate 			del_tbl_flag = 0;
9430Sstevel@tonic-gate 		} else {
9440Sstevel@tonic-gate 			prev = t;
9450Sstevel@tonic-gate 			t = t->next;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 	} /* end of table mapping loop */
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	for (t = *table_mapping; t != NULL; t = t->next) {
9500Sstevel@tonic-gate 		if (!t->dbId) {
9510Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
9520Sstevel@tonic-gate 				"%s:Fatal error: structure with no dbId found.",
9530Sstevel@tonic-gate 				myself);
9540Sstevel@tonic-gate 				return (-2);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 		append_dot(&t->dbId);
9570Sstevel@tonic-gate 		if (!t->objectDN) {
9580Sstevel@tonic-gate 			p_error = parse_internal_error;
9590Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
9600Sstevel@tonic-gate 				"%s:No objectDN for %s.", myself, t->dbId);
9610Sstevel@tonic-gate 			return (-1);
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	return (0);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * FUNCTION: append_mapping_rule
9700Sstevel@tonic-gate  *
9710Sstevel@tonic-gate  * Appends mapping rules to a table_mapping structure
9720Sstevel@tonic-gate  * with previously existing rules. flag controls whether
9730Sstevel@tonic-gate  * the functions works on the rules From or To LDAP.
9740Sstevel@tonic-gate  *
9750Sstevel@tonic-gate  * RETURN VALUE: 0 on success, >= 1 on failure.
9760Sstevel@tonic-gate  */
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate static int
append_mapping_rule(__nis_mapping_rule_t * src_rule,__nis_table_mapping_t * dst,int flag)9790Sstevel@tonic-gate append_mapping_rule(__nis_mapping_rule_t *src_rule,
9800Sstevel@tonic-gate 	__nis_table_mapping_t *dst, int flag)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	__nis_mapping_rule_t **rules = NULL;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	if (flag == 0) {
9850Sstevel@tonic-gate 		if (dst->ruleFromLDAP == NULL) {
9860Sstevel@tonic-gate 			p_error = parse_internal_error;
9870Sstevel@tonic-gate 			return (1);
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 		rules = (__nis_mapping_rule_t **)
9900Sstevel@tonic-gate 			s_realloc(dst->ruleFromLDAP,
9910Sstevel@tonic-gate 			(dst->numRulesFromLDAP + 1) *
9920Sstevel@tonic-gate 			sizeof (__nis_mapping_rule_t *));
9930Sstevel@tonic-gate 		if (rules == NULL)
9940Sstevel@tonic-gate 			return (2);
9950Sstevel@tonic-gate 		dst->ruleFromLDAP = rules;
9960Sstevel@tonic-gate 		rules[dst->numRulesFromLDAP] = dup_mapping_rule(src_rule);
9970Sstevel@tonic-gate 		if (rules[dst->numRulesFromLDAP] == NULL) {
9980Sstevel@tonic-gate 			p_error = parse_no_mem_error;
9990Sstevel@tonic-gate 			return (2);
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 		dst->numRulesFromLDAP++;
10020Sstevel@tonic-gate 	} else if (flag == 1) {
10030Sstevel@tonic-gate 		if (dst->ruleToLDAP == NULL) {
10040Sstevel@tonic-gate 			p_error = parse_internal_error;
10050Sstevel@tonic-gate 			return (1);
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 		rules = (__nis_mapping_rule_t **)
10080Sstevel@tonic-gate 			s_realloc(dst->ruleToLDAP,
10090Sstevel@tonic-gate 			(dst->numRulesToLDAP + 1) *
10100Sstevel@tonic-gate 			sizeof (__nis_mapping_rule_t *));
10110Sstevel@tonic-gate 		if (rules == NULL)
10120Sstevel@tonic-gate 			return (2);
10130Sstevel@tonic-gate 		dst->ruleToLDAP = rules;
10140Sstevel@tonic-gate 		rules[dst->numRulesToLDAP] = dup_mapping_rule(src_rule);
10150Sstevel@tonic-gate 		if (rules[dst->numRulesToLDAP] == NULL) {
10160Sstevel@tonic-gate 			p_error = parse_no_mem_error;
10170Sstevel@tonic-gate 			return (2);
10180Sstevel@tonic-gate 		}
10190Sstevel@tonic-gate 		dst->numRulesToLDAP++;
10200Sstevel@tonic-gate 	} else
10210Sstevel@tonic-gate 		return (1);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	return (0);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate  * FUNCTION: check_domain_specific_order
10280Sstevel@tonic-gate  *
10290Sstevel@tonic-gate  * Makes sure that an attribute with explicitly specified
10300Sstevel@tonic-gate  * nisLDAPdomainContext is found before its non-domain
10310Sstevel@tonic-gate  * specific counterpart.
10320Sstevel@tonic-gate  *
10330Sstevel@tonic-gate  * RETURN VALUE: 0 normal exit
10340Sstevel@tonic-gate  *               1 if domain specific attribute found
10350Sstevel@tonic-gate  *                 after non-domain specific one.
10360Sstevel@tonic-gate  *				 -1 some error condition
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate int
check_domain_specific_order(const char * sd,config_key attrib_num,__nis_table_mapping_t * table_mapping,__yp_domain_context_t * ypDomains)10400Sstevel@tonic-gate check_domain_specific_order(const char *sd,
10410Sstevel@tonic-gate 	config_key	attrib_num,
10420Sstevel@tonic-gate 	__nis_table_mapping_t *table_mapping,
10430Sstevel@tonic-gate 	__yp_domain_context_t   *ypDomains)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	__nis_table_mapping_t *t;
10460Sstevel@tonic-gate 	char    *myself = "check_domain_specific_order";
10470Sstevel@tonic-gate 	char	*type;
10480Sstevel@tonic-gate 	char	*dbId = 0;
10490Sstevel@tonic-gate 	int 	i, len;
10500Sstevel@tonic-gate 	int		match = 0;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if (ypDomains) {
10530Sstevel@tonic-gate 		if (!ypDomains->numDomains) {
10540Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
10550Sstevel@tonic-gate 				"%s:No domains specified.", myself);
10560Sstevel@tonic-gate 			return (-1);
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 	} else {
10590Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
10600Sstevel@tonic-gate 			"%s:No domain structure supplied.", myself);
10610Sstevel@tonic-gate 		return (-1);
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	for (i = 0; i < ypDomains->numDomains; i++) {
10650Sstevel@tonic-gate 		for (t = table_mapping; t != NULL; t = t->next) {
10660Sstevel@tonic-gate 			len = strlen(sd);
10670Sstevel@tonic-gate 			if ((strcasecmp(t->dbId, sd) == 0) && (len ==
10680Sstevel@tonic-gate 				strlen(t->dbId)))
10690Sstevel@tonic-gate 				/* prevent from matching against itself */
10700Sstevel@tonic-gate 				continue;
10710Sstevel@tonic-gate 			dbId = s_strndup(t->dbId, strlen(t->dbId));
10720Sstevel@tonic-gate 			if (dbId == NULL) {
10730Sstevel@tonic-gate 				logmsg(MSG_NOMEM, LOG_ERR,
10740Sstevel@tonic-gate 					"%s:Memory allocation error.", myself);
10750Sstevel@tonic-gate 				return (-1);
10760Sstevel@tonic-gate 			}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 			if (getfullmapname(&dbId,
10790Sstevel@tonic-gate 				ypDomains->domainLabels[i])) {
10800Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
10810Sstevel@tonic-gate 				"Error getting fully qualified name for %s",
10820Sstevel@tonic-gate 					dbId);
10830Sstevel@tonic-gate 				free(dbId);
10840Sstevel@tonic-gate 				return (-1);
10850Sstevel@tonic-gate 			}
10860Sstevel@tonic-gate 			if ((strcasecmp(dbId, sd) == 0) && (len ==
10870Sstevel@tonic-gate 				strlen(dbId))) {
10880Sstevel@tonic-gate 				match = 0;
10890Sstevel@tonic-gate 				switch (attrib_num) {
10900Sstevel@tonic-gate 					case key_yp_map_flags:
10910Sstevel@tonic-gate 						if (t->usedns_flag != 0 ||
10920Sstevel@tonic-gate 							t->securemap_flag != 0)
10930Sstevel@tonic-gate 							match = 1;
10940Sstevel@tonic-gate 						type = YP_MAP_FLAGS;
10950Sstevel@tonic-gate 						break;
10960Sstevel@tonic-gate 					case key_yp_comment_char:
10970Sstevel@tonic-gate 						if (t->commentChar !=
10980Sstevel@tonic-gate 							DEFAULT_COMMENT_CHAR)
10990Sstevel@tonic-gate 							match = 1;
11000Sstevel@tonic-gate 						type = YP_COMMENT_CHAR;
11010Sstevel@tonic-gate 						break;
11020Sstevel@tonic-gate 					case key_yp_repeated_field_separators:
11030Sstevel@tonic-gate 						if (t->separatorStr !=
11040Sstevel@tonic-gate 							DEFAULT_SEP_STRING)
11050Sstevel@tonic-gate 							match = 1;
11060Sstevel@tonic-gate 						type =
11070Sstevel@tonic-gate 					YP_REPEATED_FIELD_SEPARATORS;
11080Sstevel@tonic-gate 						break;
11090Sstevel@tonic-gate 					case key_yp_name_fields:
11100Sstevel@tonic-gate 						if (t->e && t->numColumns)
11110Sstevel@tonic-gate 							match = 1;
11120Sstevel@tonic-gate 						type = YP_NAME_FIELDS;
11130Sstevel@tonic-gate 					case key_yp_split_field:
11140Sstevel@tonic-gate 						if (t->e && t->numColumns)
11150Sstevel@tonic-gate 							match = 1;
11160Sstevel@tonic-gate 						type = YP_SPLIT_FIELD;
11170Sstevel@tonic-gate 						break;
11180Sstevel@tonic-gate 					case key_yp_db_id_map:
11190Sstevel@tonic-gate 						if (t->objName)
11200Sstevel@tonic-gate 							match = 1;
11210Sstevel@tonic-gate 						type = YP_DB_ID_MAP;
11220Sstevel@tonic-gate 						break;
11230Sstevel@tonic-gate 					case key_yp_entry_ttl:
11240Sstevel@tonic-gate 						if (t->initTtlLo !=
11250Sstevel@tonic-gate 							(time_t)NO_VALUE_SET)
11260Sstevel@tonic-gate 							match = 1;
11270Sstevel@tonic-gate 						type = YP_ENTRY_TTL;
11280Sstevel@tonic-gate 						break;
11290Sstevel@tonic-gate 					case key_yp_ldap_object_dn:
11300Sstevel@tonic-gate 						if (t->objectDN)
11310Sstevel@tonic-gate 							match = 1;
11320Sstevel@tonic-gate 						type = YP_LDAP_OBJECT_DN;
11330Sstevel@tonic-gate 						break;
11340Sstevel@tonic-gate 					case key_nis_to_ldap_map:
11350Sstevel@tonic-gate 						if (t->ruleToLDAP)
11360Sstevel@tonic-gate 							match = 1;
11370Sstevel@tonic-gate 						type = NIS_TO_LDAP_MAP;
11380Sstevel@tonic-gate 						break;
11390Sstevel@tonic-gate 					case key_ldap_to_nis_map:
11400Sstevel@tonic-gate 						if (t->ruleFromLDAP)
11410Sstevel@tonic-gate 							match = 1;
11420Sstevel@tonic-gate 						type = LDAP_TO_NIS_MAP;
11430Sstevel@tonic-gate 						break;
11440Sstevel@tonic-gate 					default:
11450Sstevel@tonic-gate 						type = "unknown";
11460Sstevel@tonic-gate 						match = 0;
11470Sstevel@tonic-gate 						break;
11480Sstevel@tonic-gate 				}	/* end of switch */
11490Sstevel@tonic-gate 				if (match) {
11500Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
11510Sstevel@tonic-gate "Relative attribute '%s' of type '%s' found before fully qualified one '%s'",
11520Sstevel@tonic-gate 						t->dbId, type, sd);
11530Sstevel@tonic-gate 					free(dbId);
11540Sstevel@tonic-gate 					dbId = NULL;
11550Sstevel@tonic-gate 					return (1);
11560Sstevel@tonic-gate 				}
11570Sstevel@tonic-gate 			} /* end of strncasecmp */
11580Sstevel@tonic-gate 			free(dbId);
11590Sstevel@tonic-gate 			dbId = NULL;
11600Sstevel@tonic-gate 		} /* end of t loop */
11610Sstevel@tonic-gate 	} /* end of i loop */
11620Sstevel@tonic-gate 	if (dbId)
11630Sstevel@tonic-gate 		free(dbId);
11640Sstevel@tonic-gate 	dbId = NULL;
11650Sstevel@tonic-gate 	return (0);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate int
getfullmapname(char ** mapname,const char * domainname)11690Sstevel@tonic-gate getfullmapname(char **mapname, const char *domainname)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	char *maps = *mapname;
11720Sstevel@tonic-gate 	int maplen = strlen(maps);
11730Sstevel@tonic-gate 	int domainlen = strlen(domainname);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	if (!maplen || !domainlen ||
11760Sstevel@tonic-gate 		maps[maplen - 1] == PERIOD_CHAR)
11770Sstevel@tonic-gate 		return (1);
11780Sstevel@tonic-gate 	else if (strchr(maps, COMMA_CHAR)) {
11790Sstevel@tonic-gate 		/* map already has a domain part, do nothing */
11800Sstevel@tonic-gate 		return (0);
11810Sstevel@tonic-gate 	} else {
11820Sstevel@tonic-gate 		append_comma(&maps);
11830Sstevel@tonic-gate 		maplen = strlen(maps);
11840Sstevel@tonic-gate 		maps = realloc(maps, (maplen + domainlen + 1));
11850Sstevel@tonic-gate 		if (maps != NULL) {
11860Sstevel@tonic-gate 			if (strlcat(maps, domainname, (maplen + domainlen + 1))
11870Sstevel@tonic-gate 				>= (maplen + domainlen + 1))
11880Sstevel@tonic-gate 				return (1);
11890Sstevel@tonic-gate 			*mapname = maps;
11900Sstevel@tonic-gate 			return (0);
11910Sstevel@tonic-gate 		} else
11920Sstevel@tonic-gate 			return (1);
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate /*
11970Sstevel@tonic-gate  * FUNCTION: checkfullmapname
11980Sstevel@tonic-gate  *
11990Sstevel@tonic-gate  * Tries to find out if by appending the table mapping structures
12000Sstevel@tonic-gate  * with each of the provided nisLDAPdomainContexts, an already
12010Sstevel@tonic-gate  * existing fqdn table mapping structure results. That would be the
12020Sstevel@tonic-gate  * case when a full qualified domain specific attribute was present.
12030Sstevel@tonic-gate  *
12040Sstevel@tonic-gate  * Note that per NISLDAPmapping(4) such an attribute MUST be listed
12050Sstevel@tonic-gate  * in the mapping file BEFORE its non-fqdn counterpart.
12060Sstevel@tonic-gate  *
12070Sstevel@tonic-gate  * RETURNS:	0 normal exit, 1 if an existing structure found, -1 for all
12080Sstevel@tonic-gate  * errors, 2 if already fqdn. If returning 1 the existing structure is
12090Sstevel@tonic-gate  * in found_map.
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate int
checkfullmapname(const char * mapname,const char * domainname,__nis_table_mapping_t ** table_mapping,__nis_table_mapping_t ** found_map)12130Sstevel@tonic-gate checkfullmapname(const char *mapname, const char *domainname,
12140Sstevel@tonic-gate __nis_table_mapping_t **table_mapping,
12150Sstevel@tonic-gate __nis_table_mapping_t **found_map)
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate 	char *map;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	*found_map = NULL;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/* This function does not alter mapname */
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if (!mapname || !domainname || *table_mapping == NULL)
12240Sstevel@tonic-gate 		return (-1);
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	if (strchr(mapname, COMMA_CHAR))
12270Sstevel@tonic-gate 		return (2);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	if ((map = s_strndup(mapname, strlen(mapname))) == 0)
12300Sstevel@tonic-gate 		return (-1);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	if (getfullmapname(&map, domainname)) {
12330Sstevel@tonic-gate 		free(map);
12340Sstevel@tonic-gate 		return (-1);
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	*found_map = find_table_mapping(map, strlen(map), *table_mapping);
12380Sstevel@tonic-gate 	if (*found_map) {
12390Sstevel@tonic-gate 		free(map);
12400Sstevel@tonic-gate 		return (1);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	free(map);
12440Sstevel@tonic-gate 	return (0);
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate  * FUNCTION:	append_domainContext
12490Sstevel@tonic-gate  *
12500Sstevel@tonic-gate  * Higher level function to append the domains to the appropriate
12510Sstevel@tonic-gate  * fields in a table mapping structure. Calls either getfullmapname()
12520Sstevel@tonic-gate  * or make_full_dn() to do the actual append.
12530Sstevel@tonic-gate  *
12540Sstevel@tonic-gate  * RETURNS: 0 on success, -1 on any error.
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate int
append_domainContext(__nis_table_mapping_t ** table_map,char * DomainLabel,char * Domain)12580Sstevel@tonic-gate append_domainContext(__nis_table_mapping_t **table_map,
12590Sstevel@tonic-gate char   *DomainLabel, char *Domain)
12600Sstevel@tonic-gate {
12610Sstevel@tonic-gate 	__nis_table_mapping_t *tmp_map = *table_map;
12620Sstevel@tonic-gate 	char *lasts;
12630Sstevel@tonic-gate 	char *tmp_dbId = NULL;
12640Sstevel@tonic-gate 	char *id = NULL;
12650Sstevel@tonic-gate 	int  domain_specific = 0;
12660Sstevel@tonic-gate 	char *myself = "append_domainContext";
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	if (!DomainLabel || !Domain || !tmp_map)
12690Sstevel@tonic-gate 		return (-1);
12700Sstevel@tonic-gate 	if (tmp_map->dbId == NULL || tmp_map->objName == NULL) {
12710Sstevel@tonic-gate 		p_error = parse_bad_map_error;
12720Sstevel@tonic-gate 		return (-1);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 	tmp_dbId = s_strndup(tmp_map->dbId, strlen(tmp_map->dbId));
12750Sstevel@tonic-gate 	if (!tmp_dbId)
12760Sstevel@tonic-gate 		return (-1);
12770Sstevel@tonic-gate 	if (strchr(tmp_map->dbId, COMMA_CHAR)) {
12780Sstevel@tonic-gate 		domain_specific = 1;
12790Sstevel@tonic-gate 		id = (char *)strtok_r(tmp_dbId, COMMA_STRING, &lasts);
12800Sstevel@tonic-gate 		if (id)
12810Sstevel@tonic-gate 			id = (char *)strtok_r(NULL, COMMA_STRING, &lasts);
12820Sstevel@tonic-gate 		else {
12830Sstevel@tonic-gate 			free(tmp_dbId);
12840Sstevel@tonic-gate 			return (-1);
12850Sstevel@tonic-gate 		}
12860Sstevel@tonic-gate 		if (!id) {
12870Sstevel@tonic-gate 			free(tmp_dbId);
12880Sstevel@tonic-gate 			return (-1);
12890Sstevel@tonic-gate 		}
12900Sstevel@tonic-gate 		if (strcasecmp(id, DomainLabel)) {
12910Sstevel@tonic-gate 			free(tmp_dbId);
12920Sstevel@tonic-gate 			return (0);
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 	} else {
12950Sstevel@tonic-gate 		if (getfullmapname(&tmp_map->dbId, DomainLabel)) {
12960Sstevel@tonic-gate 			free(tmp_dbId);
12970Sstevel@tonic-gate 			return (-1);
12980Sstevel@tonic-gate 		}
12990Sstevel@tonic-gate 		append_dot(&tmp_map->dbId);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 	if (tmp_dbId)
13020Sstevel@tonic-gate 		free(tmp_dbId);
13030Sstevel@tonic-gate 	tmp_dbId = NULL;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if (getfullmapname(&tmp_map->objName, DomainLabel))
13060Sstevel@tonic-gate 		return (-1);
13070Sstevel@tonic-gate 	append_dot(&tmp_map->objName);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	/*
13100Sstevel@tonic-gate 	 * If domain specific mapping doesn't have objectDN,
13110Sstevel@tonic-gate 	 * then don't touch. Most probably, pass for the generic mapping
13120Sstevel@tonic-gate 	 * will handle this by coping over it's own objectDN
13130Sstevel@tonic-gate 	 */
13140Sstevel@tonic-gate 	if (domain_specific && tmp_map->objectDN == NULL)
13150Sstevel@tonic-gate 		return (0);
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if (tmp_map->objectDN == NULL) {
13180Sstevel@tonic-gate 		/* Allocate memory to objectDN */
13190Sstevel@tonic-gate 		tmp_map->objectDN = (__nis_object_dn_t *)
13200Sstevel@tonic-gate 			s_calloc(1, sizeof (__nis_object_dn_t));
13210Sstevel@tonic-gate 		if (tmp_map->objectDN == NULL) {
13220Sstevel@tonic-gate 			logmsg(MSG_NOMEM, LOG_ERR,
13230Sstevel@tonic-gate "%s: Cannot allocate memory for objectDN",
13240Sstevel@tonic-gate 				myself);
13250Sstevel@tonic-gate 			return (2);
13260Sstevel@tonic-gate 		}
13270Sstevel@tonic-gate 		tmp_map->objectDN->read.base = NULL;
13280Sstevel@tonic-gate 		tmp_map->objectDN->write.base = NULL;
13290Sstevel@tonic-gate 		tmp_map->objectDN->read.attrs = NULL;
13300Sstevel@tonic-gate 		tmp_map->objectDN->write.attrs = NULL;
13310Sstevel@tonic-gate 		tmp_map->objectDN->read.scope = LDAP_SCOPE_ONELEVEL;
13320Sstevel@tonic-gate 		tmp_map->objectDN->write.scope = LDAP_SCOPE_UNKNOWN;
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	if (!make_fqdn(tmp_map->objectDN, Domain))
13360Sstevel@tonic-gate 		return (-1);
13370Sstevel@tonic-gate 	if (tmp_map->objectDN->write.base) {
13380Sstevel@tonic-gate 		if (!make_full_dn(&tmp_map->objectDN->write.base, Domain))
13390Sstevel@tonic-gate 			return (-1);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	return (0);
13430Sstevel@tonic-gate }
1344