xref: /titanic_44/usr/src/lib/libnisdb/yptol/map_conv.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * DESCRIPTION: Contains functions relating to movement of entire maps.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <syslog.h>
35*7c478bd9Sstevel@tonic-gate #include <ndbm.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include "ypsym.h"
38*7c478bd9Sstevel@tonic-gate #include "ypdefs.h"
39*7c478bd9Sstevel@tonic-gate #include "shim.h"
40*7c478bd9Sstevel@tonic-gate #include "yptol.h"
41*7c478bd9Sstevel@tonic-gate #include "../ldap_util.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * Switch on parts of ypdefs.h
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate USE_YPDBPATH
47*7c478bd9Sstevel@tonic-gate USE_DBM
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * Decs
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate void add_separator(char *);
53*7c478bd9Sstevel@tonic-gate suc_code dump_domain_to_dit(char *, bool_t);
54*7c478bd9Sstevel@tonic-gate suc_code dump_map_to_dit(char *, char *, bool_t);
55*7c478bd9Sstevel@tonic-gate suc_code dump_maps_to_dit(bool_t);
56*7c478bd9Sstevel@tonic-gate suc_code dump_dit_to_map();
57*7c478bd9Sstevel@tonic-gate suc_code dump_dit_to_maps();
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_maps_to_dit()
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Dump all the OLD STYLE NIS maps into the DIT.
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  *		Since the DIT is not yet set up details about which maps and
65*7c478bd9Sstevel@tonic-gate  *		domains exist are gathered from the N2L config file and the
66*7c478bd9Sstevel@tonic-gate  *		existing map files.
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  * GIVEN :	Flag indicating if containers and domains should be set up.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * RETURNS :	Success code
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate suc_code
dump_maps_to_dit(bool_t init_containers)73*7c478bd9Sstevel@tonic-gate dump_maps_to_dit(bool_t init_containers)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	char **dom_list;
76*7c478bd9Sstevel@tonic-gate 	int num_doms, i;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	num_doms = get_mapping_domain_list(&dom_list);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/* Dump all domains in list */
81*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_doms; i++) {
82*7c478bd9Sstevel@tonic-gate 		if (FAILURE == dump_domain_to_dit(dom_list[i], init_containers))
83*7c478bd9Sstevel@tonic-gate 			return (FAILURE);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_domain_to_dit()
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Dumps all maps in one domain into the DIT
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  * GIVEN :	Name of the domain
95*7c478bd9Sstevel@tonic-gate  *		Flag indicating if containers and domains should be set up.
96*7c478bd9Sstevel@tonic-gate  *
97*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS = domain completely dumped
98*7c478bd9Sstevel@tonic-gate  *		FAILURE = domain not completely dumped
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate suc_code
dump_domain_to_dit(char * dom_name,bool_t init_containers)102*7c478bd9Sstevel@tonic-gate dump_domain_to_dit(char *dom_name, bool_t init_containers)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	char **map_list;
105*7c478bd9Sstevel@tonic-gate 	int	i;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	/* Set up nis domain object */
108*7c478bd9Sstevel@tonic-gate 	if (SUCCESS != make_nis_domain(dom_name, init_containers)) {
109*7c478bd9Sstevel@tonic-gate 		if (init_containers)
110*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
111*7c478bd9Sstevel@tonic-gate 			"Could not make nisDomain object for %s", dom_name);
112*7c478bd9Sstevel@tonic-gate 		else
113*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
114*7c478bd9Sstevel@tonic-gate 			"Problem detected with nisDomain object for %s",
115*7c478bd9Sstevel@tonic-gate 								dom_name);
116*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	/* Get list of maps from mapping file */
120*7c478bd9Sstevel@tonic-gate 	map_list = get_mapping_map_list(dom_name);
121*7c478bd9Sstevel@tonic-gate 	if (NULL == map_list) {
122*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
123*7c478bd9Sstevel@tonic-gate 			"Could not get map list for %s", dom_name);
124*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	for (i = 0; NULL != map_list[i]; i++) {
128*7c478bd9Sstevel@tonic-gate 		dump_map_to_dit(map_list[i], dom_name, init_containers);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	free_map_list(map_list);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_map_to_dit()
138*7c478bd9Sstevel@tonic-gate  *
139*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Dump a OLD STYLE NIS map into the DIT.
140*7c478bd9Sstevel@tonic-gate  *
141*7c478bd9Sstevel@tonic-gate  * GIVEN :	Name of map (not fully qualified)
142*7c478bd9Sstevel@tonic-gate  *		Name of domain
143*7c478bd9Sstevel@tonic-gate  *		Flag indicating if containers should be set up.
144*7c478bd9Sstevel@tonic-gate  *
145*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS = Map copy completed
146*7c478bd9Sstevel@tonic-gate  *		FAILURE = Map copy not completed
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate suc_code
dump_map_to_dit(char * map_name,char * domain,bool_t init_containers)149*7c478bd9Sstevel@tonic-gate dump_map_to_dit(char *map_name, char *domain, bool_t init_containers)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	char *myself = "dump_map_to_dit";
152*7c478bd9Sstevel@tonic-gate 	DBM *dbm;
153*7c478bd9Sstevel@tonic-gate 	datum key;
154*7c478bd9Sstevel@tonic-gate 	datum value;
155*7c478bd9Sstevel@tonic-gate 	char *map_path;		/* Qualified map name */
156*7c478bd9Sstevel@tonic-gate 	int entry_count;
157*7c478bd9Sstevel@tonic-gate 	int next_print;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	printf("Copying map \"%s\", domain \"%s\", to LDAP.\n",
160*7c478bd9Sstevel@tonic-gate 							map_name, domain);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	/* Create the NIS container */
163*7c478bd9Sstevel@tonic-gate 	if (SUCCESS != make_nis_container(map_name, domain, init_containers)) {
164*7c478bd9Sstevel@tonic-gate 		if (init_containers)
165*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
166*7c478bd9Sstevel@tonic-gate 				"Could not make container for %s %s",
167*7c478bd9Sstevel@tonic-gate 				map_name, domain);
168*7c478bd9Sstevel@tonic-gate 		else
169*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
170*7c478bd9Sstevel@tonic-gate 				"Problem detected with container for %s %s",
171*7c478bd9Sstevel@tonic-gate 				map_name, domain);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/* Make up fully qualified map name */
177*7c478bd9Sstevel@tonic-gate 	map_path = (char *)am(myself, strlen(domain) + strlen(map_name) +
178*7c478bd9Sstevel@tonic-gate 						ypdbpath_sz + 3);
179*7c478bd9Sstevel@tonic-gate 	if (NULL == map_path) {
180*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOMEM, LOG_ERR,
181*7c478bd9Sstevel@tonic-gate 			"Could not alloc memory for %s %s", map_name, domain);
182*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 	strcpy(map_path, ypdbpath);
185*7c478bd9Sstevel@tonic-gate 	add_separator(map_path);
186*7c478bd9Sstevel@tonic-gate 	strcat(map_path, domain);
187*7c478bd9Sstevel@tonic-gate 	add_separator(map_path);
188*7c478bd9Sstevel@tonic-gate 	strcat(map_path, map_name);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	/* Open the DBM file. Use real dbm call */
191*7c478bd9Sstevel@tonic-gate 	dbm = dbm_open(map_path, O_RDONLY, 0644);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	/* Finished with full name */
194*7c478bd9Sstevel@tonic-gate 	sfree(map_path);
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	if (NULL == dbm) {
197*7c478bd9Sstevel@tonic-gate 		/*
198*7c478bd9Sstevel@tonic-gate 		 * This map probably didn't exist. No problem, user may be
199*7c478bd9Sstevel@tonic-gate 		 * going to populate container using LDAP.
200*7c478bd9Sstevel@tonic-gate 		 */
201*7c478bd9Sstevel@tonic-gate 		return (SUCCESS);
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	/*
205*7c478bd9Sstevel@tonic-gate 	 * N2L has no lock for old style maps. No problem ypserv -i is the
206*7c478bd9Sstevel@tonic-gate 	 * only thing that accesses them.
207*7c478bd9Sstevel@tonic-gate 	 */
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/* For all entries in file */
210*7c478bd9Sstevel@tonic-gate 	for (key = dbm_firstkey(dbm), next_print = PRINT_FREQ, entry_count = 1;
211*7c478bd9Sstevel@tonic-gate 		NULL != key.dptr; key = dbm_nextkey(dbm), entry_count ++) {
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		/* Don't write zero length keys */
214*7c478bd9Sstevel@tonic-gate 		if (0 == key.dsize) {
215*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
216*7c478bd9Sstevel@tonic-gate 			"Zero length key ignored in %s %s", map_name, domain);
217*7c478bd9Sstevel@tonic-gate 			continue;
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 		/* Don't write 'special' nis entries */
221*7c478bd9Sstevel@tonic-gate 		if (is_special_key(&key))
222*7c478bd9Sstevel@tonic-gate 			continue;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		/* Get entry */
225*7c478bd9Sstevel@tonic-gate 		value = dbm_fetch(dbm, key);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		/* Copy entry to DIT */
228*7c478bd9Sstevel@tonic-gate 		if (SUCCESS != write_to_dit(map_name, domain, key, value,
229*7c478bd9Sstevel@tonic-gate 								TRUE, TRUE))
230*7c478bd9Sstevel@tonic-gate 			/* Syslog will have already been done */
231*7c478bd9Sstevel@tonic-gate 			break;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		/* If necessary print a progress report */
234*7c478bd9Sstevel@tonic-gate 		if (entry_count >= next_print) {
235*7c478bd9Sstevel@tonic-gate 			printf("%d entries processed.\n", entry_count);
236*7c478bd9Sstevel@tonic-gate 			next_print *= 2;
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	dbm_close(dbm);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate /*
246*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_dit_to_maps()
247*7c478bd9Sstevel@tonic-gate  *
248*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Dumps the contents of the DIT into the NEW STYLE NIS maps. If
249*7c478bd9Sstevel@tonic-gate  *		the maps, or their TTL files do not exist creates them.
250*7c478bd9Sstevel@tonic-gate  *
251*7c478bd9Sstevel@tonic-gate  *		Since we are now treating the DIT as authoritative details of
252*7c478bd9Sstevel@tonic-gate  *		which domains and maps exist are gathered from the DIT.
253*7c478bd9Sstevel@tonic-gate  *
254*7c478bd9Sstevel@tonic-gate  * GIVEN :	Nothing
255*7c478bd9Sstevel@tonic-gate  *
256*7c478bd9Sstevel@tonic-gate  * RETURNS :	Success code
257*7c478bd9Sstevel@tonic-gate  */
258*7c478bd9Sstevel@tonic-gate suc_code
dump_dit_to_maps()259*7c478bd9Sstevel@tonic-gate dump_dit_to_maps()
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	char **dom_list;
262*7c478bd9Sstevel@tonic-gate 	int dom_count;
263*7c478bd9Sstevel@tonic-gate 	char *dom_path;
264*7c478bd9Sstevel@tonic-gate 	char **map_list;
265*7c478bd9Sstevel@tonic-gate 	int i, j;
266*7c478bd9Sstevel@tonic-gate 	char *myself = "dump_dit_to_maps";
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	/* For all domain objects in DIT */
269*7c478bd9Sstevel@tonic-gate 	dom_count = get_mapping_domain_list(&dom_list);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	if (0 == dom_count) {
272*7c478bd9Sstevel@tonic-gate 		/* No problem, maybe no domains */
273*7c478bd9Sstevel@tonic-gate 		return (SUCCESS);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	/* Dump all domains in list */
277*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dom_count; i++) {
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/* If necessary create domain directory */
280*7c478bd9Sstevel@tonic-gate 		dom_path = (char *)am(myself, ypdbpath_sz +
281*7c478bd9Sstevel@tonic-gate 						strlen(dom_list[i]) + 2);
282*7c478bd9Sstevel@tonic-gate 		if (NULL == dom_path) {
283*7c478bd9Sstevel@tonic-gate 			return (FAILURE);
284*7c478bd9Sstevel@tonic-gate 		}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		strcpy(dom_path, ypdbpath);
287*7c478bd9Sstevel@tonic-gate 		strcat(dom_path, "/");
288*7c478bd9Sstevel@tonic-gate 		strcat(dom_path, dom_list[i]);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		if (0 != mkdir(dom_path, 0644)) {
291*7c478bd9Sstevel@tonic-gate 			/* If dir exists fine. Just use it */
292*7c478bd9Sstevel@tonic-gate 			if (EEXIST != errno) {
293*7c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
294*7c478bd9Sstevel@tonic-gate 				"Could not make create domain directory %s",
295*7c478bd9Sstevel@tonic-gate 								dom_path);
296*7c478bd9Sstevel@tonic-gate 				sfree(dom_path);
297*7c478bd9Sstevel@tonic-gate 			}
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 		sfree(dom_path);
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 		/* Get list of maps for this domain */
303*7c478bd9Sstevel@tonic-gate 		map_list = get_mapping_map_list(dom_list[i]);
304*7c478bd9Sstevel@tonic-gate 		if (NULL == map_list) {
305*7c478bd9Sstevel@tonic-gate 			/* No problem. Just no maps in this domain */
306*7c478bd9Sstevel@tonic-gate 			continue;
307*7c478bd9Sstevel@tonic-gate 		}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 		/* For all maps in domain */
310*7c478bd9Sstevel@tonic-gate 		for (j = 0; map_list[j] != NULL; j++) {
311*7c478bd9Sstevel@tonic-gate 			/* A normal map update will initialize it. */
312*7c478bd9Sstevel@tonic-gate 			if (FAILURE == dump_dit_to_map(map_list[j],
313*7c478bd9Sstevel@tonic-gate 							dom_list[i])) {
314*7c478bd9Sstevel@tonic-gate 				free_map_list(map_list);
315*7c478bd9Sstevel@tonic-gate 				return (FAILURE);
316*7c478bd9Sstevel@tonic-gate 			}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 			/* If we have a netgroup also generate netgroup.byxxx */
319*7c478bd9Sstevel@tonic-gate 			if (0 == strcmp(map_list[j], NETGROUP_MAP)) {
320*7c478bd9Sstevel@tonic-gate 				if (FAILURE == dump_dit_to_map(NETGROUP_BYHOST,
321*7c478bd9Sstevel@tonic-gate 								dom_list[i])) {
322*7c478bd9Sstevel@tonic-gate 					free_map_list(map_list);
323*7c478bd9Sstevel@tonic-gate 					return (FAILURE);
324*7c478bd9Sstevel@tonic-gate 				}
325*7c478bd9Sstevel@tonic-gate 				if (FAILURE == dump_dit_to_map(NETGROUP_BYUSER,
326*7c478bd9Sstevel@tonic-gate 								dom_list[i])) {
327*7c478bd9Sstevel@tonic-gate 					free_map_list(map_list);
328*7c478bd9Sstevel@tonic-gate 					return (FAILURE);
329*7c478bd9Sstevel@tonic-gate 				}
330*7c478bd9Sstevel@tonic-gate 			}
331*7c478bd9Sstevel@tonic-gate 		}
332*7c478bd9Sstevel@tonic-gate 		free_map_list(map_list);
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate /*
338*7c478bd9Sstevel@tonic-gate  * FUNCTION :	dump_dit_to_map()
339*7c478bd9Sstevel@tonic-gate  *
340*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Dumps the contents of the DIT into one NEW STYLE NIS map. If
341*7c478bd9Sstevel@tonic-gate  *		the map, or its TTL file does not exist creates them.
342*7c478bd9Sstevel@tonic-gate  *
343*7c478bd9Sstevel@tonic-gate  *		This is the same operation as is carried out when updating a
344*7c478bd9Sstevel@tonic-gate  *		map that has timed out. As a result we can call the normal
345*7c478bd9Sstevel@tonic-gate  *		update function.
346*7c478bd9Sstevel@tonic-gate  *
347*7c478bd9Sstevel@tonic-gate  *
348*7c478bd9Sstevel@tonic-gate  * GIVEN :	Map name (unqualified)
349*7c478bd9Sstevel@tonic-gate  *		Domain name.
350*7c478bd9Sstevel@tonic-gate  *
351*7c478bd9Sstevel@tonic-gate  * RETURNS :	SUCCESS = Map copy complete
352*7c478bd9Sstevel@tonic-gate  *		FAILURE = Problems
353*7c478bd9Sstevel@tonic-gate  */
354*7c478bd9Sstevel@tonic-gate suc_code
dump_dit_to_map(char * map_name,char * domain)355*7c478bd9Sstevel@tonic-gate dump_dit_to_map(char *map_name, char *domain)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	char *myself = "dump_dit_to_map";
358*7c478bd9Sstevel@tonic-gate 	map_ctrl map;
359*7c478bd9Sstevel@tonic-gate 	char 	*map_path;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	printf("Copying LDAP data to map \"%s\", domain \"%s\".\n",
362*7c478bd9Sstevel@tonic-gate 							map_name, domain);
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	/*
365*7c478bd9Sstevel@tonic-gate 	 * To call update_map_from_dit() we need an initialized map_ctrl.
366*7c478bd9Sstevel@tonic-gate 	 * The easiest way to get this is to generate a full path to the new
367*7c478bd9Sstevel@tonic-gate 	 * map and then call map_ctrl_init().
368*7c478bd9Sstevel@tonic-gate 	 */
369*7c478bd9Sstevel@tonic-gate 	map_path = (char *)am(myself, ypdbpath_sz + strlen(map_name) +
370*7c478bd9Sstevel@tonic-gate 				strlen(domain) + strlen(NTOL_PREFIX) + 3);
371*7c478bd9Sstevel@tonic-gate 	if (NULL == map_path)
372*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	strcpy(map_path, ypdbpath);
375*7c478bd9Sstevel@tonic-gate 	add_separator(map_path);
376*7c478bd9Sstevel@tonic-gate 	strcat(map_path, domain);
377*7c478bd9Sstevel@tonic-gate 	add_separator(map_path);
378*7c478bd9Sstevel@tonic-gate 	strcat(map_path, NTOL_PREFIX);
379*7c478bd9Sstevel@tonic-gate 	strcat(map_path, map_name);
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (FAILURE == map_ctrl_init(&map, map_path)) {
382*7c478bd9Sstevel@tonic-gate 		sfree(map_path);
383*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	sfree(map_path);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	/*
389*7c478bd9Sstevel@tonic-gate 	 * This is called before anything else is running so don't need to
390*7c478bd9Sstevel@tonic-gate 	 * do normal update lock.
391*7c478bd9Sstevel@tonic-gate 	 */
392*7c478bd9Sstevel@tonic-gate 	return (update_map_from_dit(&map, TRUE));
393*7c478bd9Sstevel@tonic-gate }
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /*
396*7c478bd9Sstevel@tonic-gate  * FUNCTION :	add_seperator()
397*7c478bd9Sstevel@tonic-gate  *
398*7c478bd9Sstevel@tonic-gate  * DESCRIPTION:	Adds a file separator to a string (which must already be big
399*7c478bd9Sstevel@tonic-gate  *		enough.)
400*7c478bd9Sstevel@tonic-gate  *
401*7c478bd9Sstevel@tonic-gate  * GIVEN :	Pointer to the string
402*7c478bd9Sstevel@tonic-gate  *
403*7c478bd9Sstevel@tonic-gate  * RETURNS :	Nothing
404*7c478bd9Sstevel@tonic-gate  */
405*7c478bd9Sstevel@tonic-gate void
add_separator(char * str)406*7c478bd9Sstevel@tonic-gate add_separator(char *str)
407*7c478bd9Sstevel@tonic-gate {
408*7c478bd9Sstevel@tonic-gate 	char *p;
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	p = str + strlen(str);
411*7c478bd9Sstevel@tonic-gate 	*p = SEP_CHAR;
412*7c478bd9Sstevel@tonic-gate 	*(p+1) = '\0';
413*7c478bd9Sstevel@tonic-gate }
414