summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnisdb/yptol/map_conv.c
blob: 024eed6313c84571f6daf20451b8caf2d94f8034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2015 Gary Mills
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * DESCRIPTION: Contains functions relating to movement of entire maps.
 */

#include <unistd.h>
#include <syslog.h>
#include <ndbm.h>
#include <string.h>
#include "ypsym.h"
#include "ypdefs.h"
#include "shim.h"
#include "yptol.h"
#include "../ldap_util.h"

/*
 * Switch on parts of ypdefs.h
 */
USE_YPDBPATH

/*
 * Decs
 */
void add_separator(char *);
suc_code dump_domain_to_dit(char *, bool_t);
suc_code dump_map_to_dit(char *, char *, bool_t);
suc_code dump_maps_to_dit(bool_t);
suc_code dump_dit_to_map();
suc_code dump_dit_to_maps();

/*
 * FUNCTION :	dump_maps_to_dit()
 *
 * DESCRIPTION:	Dump all the OLD STYLE NIS maps into the DIT.
 *
 *		Since the DIT is not yet set up details about which maps and
 *		domains exist are gathered from the N2L config file and the
 *		existing map files.
 *
 * GIVEN :	Flag indicating if containers and domains should be set up.
 *
 * RETURNS :	Success code
 */
suc_code
dump_maps_to_dit(bool_t init_containers)
{
	char **dom_list;
	int num_doms, i;

	num_doms = get_mapping_domain_list(&dom_list);

	/* Dump all domains in list */
	for (i = 0; i < num_doms; i++) {
		if (FAILURE == dump_domain_to_dit(dom_list[i], init_containers))
			return (FAILURE);
	}

	return (SUCCESS);
}

/*
 * FUNCTION :	dump_domain_to_dit()
 *
 * DESCRIPTION:	Dumps all maps in one domain into the DIT
 *
 * GIVEN :	Name of the domain
 *		Flag indicating if containers and domains should be set up.
 *
 * RETURNS :	SUCCESS = domain completely dumped
 *		FAILURE = domain not completely dumped
 *
 */
suc_code
dump_domain_to_dit(char *dom_name, bool_t init_containers)
{
	char **map_list;
	int	i;

	/* Set up nis domain object */
	if (SUCCESS != make_nis_domain(dom_name, init_containers)) {
		if (init_containers)
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
			"Could not make nisDomain object for %s", dom_name);
		else
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
			"Problem detected with nisDomain object for %s",
								dom_name);
		return (FAILURE);
	}

	/* Get list of maps from mapping file */
	map_list = get_mapping_map_list(dom_name);
	if (NULL == map_list) {
		logmsg(MSG_NOTIMECHECK, LOG_ERR,
			"Could not get map list for %s", dom_name);
		return (FAILURE);
	}

	for (i = 0; NULL != map_list[i]; i++) {
		dump_map_to_dit(map_list[i], dom_name, init_containers);
	}

	free_map_list(map_list);

	return (SUCCESS);
}

/*
 * FUNCTION :	dump_map_to_dit()
 *
 * DESCRIPTION:	Dump a OLD STYLE NIS map into the DIT.
 *
 * GIVEN :	Name of map (not fully qualified)
 *		Name of domain
 *		Flag indicating if containers should be set up.
 *
 * RETURNS :	SUCCESS = Map copy completed
 *		FAILURE = Map copy not completed
 */
suc_code
dump_map_to_dit(char *map_name, char *domain, bool_t init_containers)
{
	char *myself = "dump_map_to_dit";
	DBM *dbm;
	datum key;
	datum value;
	char *map_path;		/* Qualified map name */
	int entry_count;
	int next_print;

	printf("Copying map \"%s\", domain \"%s\", to LDAP.\n",
							map_name, domain);

	/* Create the NIS container */
	if (SUCCESS != make_nis_container(map_name, domain, init_containers)) {
		if (init_containers)
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
				"Could not make container for %s %s",
				map_name, domain);
		else
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
				"Problem detected with container for %s %s",
				map_name, domain);

		return (FAILURE);
	}

	/* Make up fully qualified map name */
	map_path = (char *)am(myself, strlen(domain) + strlen(map_name) +
						ypdbpath_sz + 3);
	if (NULL == map_path) {
		logmsg(MSG_NOMEM, LOG_ERR,
			"Could not alloc memory for %s %s", map_name, domain);
		return (FAILURE);
	}
	strcpy(map_path, ypdbpath);
	add_separator(map_path);
	strcat(map_path, domain);
	add_separator(map_path);
	strcat(map_path, map_name);

	/* Open the DBM file. Use real dbm call */
	dbm = dbm_open(map_path, O_RDONLY, 0644);

	/* Finished with full name */
	sfree(map_path);

	if (NULL == dbm) {
		/*
		 * This map probably didn't exist. No problem, user may be
		 * going to populate container using LDAP.
		 */
		return (SUCCESS);
	}

	/*
	 * N2L has no lock for old style maps. No problem ypserv -i is the
	 * only thing that accesses them.
	 */

	/* For all entries in file */
	for (key = dbm_firstkey(dbm), next_print = PRINT_FREQ, entry_count = 1;
		NULL != key.dptr; key = dbm_nextkey(dbm), entry_count ++) {

		/* Don't write zero length keys */
		if (0 == key.dsize) {
			logmsg(MSG_NOTIMECHECK, LOG_INFO,
			"Zero length key ignored in %s %s", map_name, domain);
			continue;
		}

		/* Don't write 'special' nis entries */
		if (is_special_key(&key))
			continue;

		/* Get entry */
		value = dbm_fetch(dbm, key);

		/* Copy entry to DIT */
		if (SUCCESS != write_to_dit(map_name, domain, key, value,
								TRUE, TRUE))
			/* Syslog will have already been done */
			break;

		/* If necessary print a progress report */
		if (entry_count >= next_print) {
			printf("%d entries processed.\n", entry_count);
			next_print *= 2;
		}
	}

	dbm_close(dbm);

	return (SUCCESS);
}

/*
 * FUNCTION :	dump_dit_to_maps()
 *
 * DESCRIPTION:	Dumps the contents of the DIT into the NEW STYLE NIS maps. If
 *		the maps, or their TTL files do not exist creates them.
 *
 *		Since we are now treating the DIT as authoritative details of
 *		which domains and maps exist are gathered from the DIT.
 *
 * GIVEN :	Nothing
 *
 * RETURNS :	Success code
 */
suc_code
dump_dit_to_maps()
{
	char **dom_list;
	int dom_count;
	char *dom_path;
	char **map_list;
	int i, j;
	char *myself = "dump_dit_to_maps";

	/* For all domain objects in DIT */
	dom_count = get_mapping_domain_list(&dom_list);

	if (0 == dom_count) {
		/* No problem, maybe no domains */
		return (SUCCESS);
	}

	/* Dump all domains in list */
	for (i = 0; i < dom_count; i++) {

		/* If necessary create domain directory */
		dom_path = (char *)am(myself, ypdbpath_sz +
						strlen(dom_list[i]) + 2);
		if (NULL == dom_path) {
			return (FAILURE);
		}

		strcpy(dom_path, ypdbpath);
		strcat(dom_path, "/");
		strcat(dom_path, dom_list[i]);

		if (0 != mkdir(dom_path, 0644)) {
			/* If dir exists fine. Just use it */
			if (EEXIST != errno) {
				logmsg(MSG_NOTIMECHECK, LOG_ERR,
				"Could not make create domain directory %s",
								dom_path);
				sfree(dom_path);
			}
		}

		sfree(dom_path);

		/* Get list of maps for this domain */
		map_list = get_mapping_map_list(dom_list[i]);
		if (NULL == map_list) {
			/* No problem. Just no maps in this domain */
			continue;
		}

		/* For all maps in domain */
		for (j = 0; map_list[j] != NULL; j++) {
			/* A normal map update will initialize it. */
			if (FAILURE == dump_dit_to_map(map_list[j],
							dom_list[i])) {
				free_map_list(map_list);
				return (FAILURE);
			}

			/* If we have a netgroup also generate netgroup.byxxx */
			if (0 == strcmp(map_list[j], NETGROUP_MAP)) {
				if (FAILURE == dump_dit_to_map(NETGROUP_BYHOST,
								dom_list[i])) {
					free_map_list(map_list);
					return (FAILURE);
				}
				if (FAILURE == dump_dit_to_map(NETGROUP_BYUSER,
								dom_list[i])) {
					free_map_list(map_list);
					return (FAILURE);
				}
			}
		}
		free_map_list(map_list);
	}
	return (SUCCESS);
}

/*
 * FUNCTION :	dump_dit_to_map()
 *
 * DESCRIPTION:	Dumps the contents of the DIT into one NEW STYLE NIS map. If
 *		the map, or its TTL file does not exist creates them.
 *
 *		This is the same operation as is carried out when updating a
 *		map that has timed out. As a result we can call the normal
 *		update function.
 *
 *
 * GIVEN :	Map name (unqualified)
 *		Domain name.
 *
 * RETURNS :	SUCCESS = Map copy complete
 *		FAILURE = Problems
 */
suc_code
dump_dit_to_map(char *map_name, char *domain)
{
	char *myself = "dump_dit_to_map";
	map_ctrl map;
	char 	*map_path;

	printf("Copying LDAP data to map \"%s\", domain \"%s\".\n",
							map_name, domain);

	/*
	 * To call update_map_from_dit() we need an initialized map_ctrl.
	 * The easiest way to get this is to generate a full path to the new
	 * map and then call map_ctrl_init().
	 */
	map_path = (char *)am(myself, ypdbpath_sz + strlen(map_name) +
				strlen(domain) + strlen(NTOL_PREFIX) + 3);
	if (NULL == map_path)
		return (FAILURE);

	strcpy(map_path, ypdbpath);
	add_separator(map_path);
	strcat(map_path, domain);
	add_separator(map_path);
	strcat(map_path, NTOL_PREFIX);
	strcat(map_path, map_name);

	if (FAILURE == map_ctrl_init(&map, map_path)) {
		sfree(map_path);
		return (FAILURE);
	}

	sfree(map_path);

	/*
	 * This is called before anything else is running so don't need to
	 * do normal update lock.
	 */
	return (update_map_from_dit(&map, TRUE));
}

/*
 * FUNCTION :	add_seperator()
 *
 * DESCRIPTION:	Adds a file separator to a string (which must already be big
 *		enough.)
 *
 * GIVEN :	Pointer to the string
 *
 * RETURNS :	Nothing
 */
void
add_separator(char *str)
{
	char *p;

	p = str + strlen(str);
	*p = SEP_CHAR;
	*(p+1) = '\0';
}