5910 libnisdb won't build with modern GCC
[unleashed.git] / usr / src / lib / libnisdb / yptol / map_conv.c
blob024eed6313c84571f6daf20451b8caf2d94f8034
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2015 Gary Mills
24 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * DESCRIPTION: Contains functions relating to movement of entire maps.
32 #include <unistd.h>
33 #include <syslog.h>
34 #include <ndbm.h>
35 #include <string.h>
36 #include "ypsym.h"
37 #include "ypdefs.h"
38 #include "shim.h"
39 #include "yptol.h"
40 #include "../ldap_util.h"
43 * Switch on parts of ypdefs.h
45 USE_YPDBPATH
48 * Decs
50 void add_separator(char *);
51 suc_code dump_domain_to_dit(char *, bool_t);
52 suc_code dump_map_to_dit(char *, char *, bool_t);
53 suc_code dump_maps_to_dit(bool_t);
54 suc_code dump_dit_to_map();
55 suc_code dump_dit_to_maps();
58 * FUNCTION : dump_maps_to_dit()
60 * DESCRIPTION: Dump all the OLD STYLE NIS maps into the DIT.
62 * Since the DIT is not yet set up details about which maps and
63 * domains exist are gathered from the N2L config file and the
64 * existing map files.
66 * GIVEN : Flag indicating if containers and domains should be set up.
68 * RETURNS : Success code
70 suc_code
71 dump_maps_to_dit(bool_t init_containers)
73 char **dom_list;
74 int num_doms, i;
76 num_doms = get_mapping_domain_list(&dom_list);
78 /* Dump all domains in list */
79 for (i = 0; i < num_doms; i++) {
80 if (FAILURE == dump_domain_to_dit(dom_list[i], init_containers))
81 return (FAILURE);
84 return (SUCCESS);
88 * FUNCTION : dump_domain_to_dit()
90 * DESCRIPTION: Dumps all maps in one domain into the DIT
92 * GIVEN : Name of the domain
93 * Flag indicating if containers and domains should be set up.
95 * RETURNS : SUCCESS = domain completely dumped
96 * FAILURE = domain not completely dumped
99 suc_code
100 dump_domain_to_dit(char *dom_name, bool_t init_containers)
102 char **map_list;
103 int i;
105 /* Set up nis domain object */
106 if (SUCCESS != make_nis_domain(dom_name, init_containers)) {
107 if (init_containers)
108 logmsg(MSG_NOTIMECHECK, LOG_ERR,
109 "Could not make nisDomain object for %s", dom_name);
110 else
111 logmsg(MSG_NOTIMECHECK, LOG_ERR,
112 "Problem detected with nisDomain object for %s",
113 dom_name);
114 return (FAILURE);
117 /* Get list of maps from mapping file */
118 map_list = get_mapping_map_list(dom_name);
119 if (NULL == map_list) {
120 logmsg(MSG_NOTIMECHECK, LOG_ERR,
121 "Could not get map list for %s", dom_name);
122 return (FAILURE);
125 for (i = 0; NULL != map_list[i]; i++) {
126 dump_map_to_dit(map_list[i], dom_name, init_containers);
129 free_map_list(map_list);
131 return (SUCCESS);
135 * FUNCTION : dump_map_to_dit()
137 * DESCRIPTION: Dump a OLD STYLE NIS map into the DIT.
139 * GIVEN : Name of map (not fully qualified)
140 * Name of domain
141 * Flag indicating if containers should be set up.
143 * RETURNS : SUCCESS = Map copy completed
144 * FAILURE = Map copy not completed
146 suc_code
147 dump_map_to_dit(char *map_name, char *domain, bool_t init_containers)
149 char *myself = "dump_map_to_dit";
150 DBM *dbm;
151 datum key;
152 datum value;
153 char *map_path; /* Qualified map name */
154 int entry_count;
155 int next_print;
157 printf("Copying map \"%s\", domain \"%s\", to LDAP.\n",
158 map_name, domain);
160 /* Create the NIS container */
161 if (SUCCESS != make_nis_container(map_name, domain, init_containers)) {
162 if (init_containers)
163 logmsg(MSG_NOTIMECHECK, LOG_ERR,
164 "Could not make container for %s %s",
165 map_name, domain);
166 else
167 logmsg(MSG_NOTIMECHECK, LOG_ERR,
168 "Problem detected with container for %s %s",
169 map_name, domain);
171 return (FAILURE);
174 /* Make up fully qualified map name */
175 map_path = (char *)am(myself, strlen(domain) + strlen(map_name) +
176 ypdbpath_sz + 3);
177 if (NULL == map_path) {
178 logmsg(MSG_NOMEM, LOG_ERR,
179 "Could not alloc memory for %s %s", map_name, domain);
180 return (FAILURE);
182 strcpy(map_path, ypdbpath);
183 add_separator(map_path);
184 strcat(map_path, domain);
185 add_separator(map_path);
186 strcat(map_path, map_name);
188 /* Open the DBM file. Use real dbm call */
189 dbm = dbm_open(map_path, O_RDONLY, 0644);
191 /* Finished with full name */
192 sfree(map_path);
194 if (NULL == dbm) {
196 * This map probably didn't exist. No problem, user may be
197 * going to populate container using LDAP.
199 return (SUCCESS);
203 * N2L has no lock for old style maps. No problem ypserv -i is the
204 * only thing that accesses them.
207 /* For all entries in file */
208 for (key = dbm_firstkey(dbm), next_print = PRINT_FREQ, entry_count = 1;
209 NULL != key.dptr; key = dbm_nextkey(dbm), entry_count ++) {
211 /* Don't write zero length keys */
212 if (0 == key.dsize) {
213 logmsg(MSG_NOTIMECHECK, LOG_INFO,
214 "Zero length key ignored in %s %s", map_name, domain);
215 continue;
218 /* Don't write 'special' nis entries */
219 if (is_special_key(&key))
220 continue;
222 /* Get entry */
223 value = dbm_fetch(dbm, key);
225 /* Copy entry to DIT */
226 if (SUCCESS != write_to_dit(map_name, domain, key, value,
227 TRUE, TRUE))
228 /* Syslog will have already been done */
229 break;
231 /* If necessary print a progress report */
232 if (entry_count >= next_print) {
233 printf("%d entries processed.\n", entry_count);
234 next_print *= 2;
238 dbm_close(dbm);
240 return (SUCCESS);
244 * FUNCTION : dump_dit_to_maps()
246 * DESCRIPTION: Dumps the contents of the DIT into the NEW STYLE NIS maps. If
247 * the maps, or their TTL files do not exist creates them.
249 * Since we are now treating the DIT as authoritative details of
250 * which domains and maps exist are gathered from the DIT.
252 * GIVEN : Nothing
254 * RETURNS : Success code
256 suc_code
257 dump_dit_to_maps()
259 char **dom_list;
260 int dom_count;
261 char *dom_path;
262 char **map_list;
263 int i, j;
264 char *myself = "dump_dit_to_maps";
266 /* For all domain objects in DIT */
267 dom_count = get_mapping_domain_list(&dom_list);
269 if (0 == dom_count) {
270 /* No problem, maybe no domains */
271 return (SUCCESS);
274 /* Dump all domains in list */
275 for (i = 0; i < dom_count; i++) {
277 /* If necessary create domain directory */
278 dom_path = (char *)am(myself, ypdbpath_sz +
279 strlen(dom_list[i]) + 2);
280 if (NULL == dom_path) {
281 return (FAILURE);
284 strcpy(dom_path, ypdbpath);
285 strcat(dom_path, "/");
286 strcat(dom_path, dom_list[i]);
288 if (0 != mkdir(dom_path, 0644)) {
289 /* If dir exists fine. Just use it */
290 if (EEXIST != errno) {
291 logmsg(MSG_NOTIMECHECK, LOG_ERR,
292 "Could not make create domain directory %s",
293 dom_path);
294 sfree(dom_path);
298 sfree(dom_path);
300 /* Get list of maps for this domain */
301 map_list = get_mapping_map_list(dom_list[i]);
302 if (NULL == map_list) {
303 /* No problem. Just no maps in this domain */
304 continue;
307 /* For all maps in domain */
308 for (j = 0; map_list[j] != NULL; j++) {
309 /* A normal map update will initialize it. */
310 if (FAILURE == dump_dit_to_map(map_list[j],
311 dom_list[i])) {
312 free_map_list(map_list);
313 return (FAILURE);
316 /* If we have a netgroup also generate netgroup.byxxx */
317 if (0 == strcmp(map_list[j], NETGROUP_MAP)) {
318 if (FAILURE == dump_dit_to_map(NETGROUP_BYHOST,
319 dom_list[i])) {
320 free_map_list(map_list);
321 return (FAILURE);
323 if (FAILURE == dump_dit_to_map(NETGROUP_BYUSER,
324 dom_list[i])) {
325 free_map_list(map_list);
326 return (FAILURE);
330 free_map_list(map_list);
332 return (SUCCESS);
336 * FUNCTION : dump_dit_to_map()
338 * DESCRIPTION: Dumps the contents of the DIT into one NEW STYLE NIS map. If
339 * the map, or its TTL file does not exist creates them.
341 * This is the same operation as is carried out when updating a
342 * map that has timed out. As a result we can call the normal
343 * update function.
346 * GIVEN : Map name (unqualified)
347 * Domain name.
349 * RETURNS : SUCCESS = Map copy complete
350 * FAILURE = Problems
352 suc_code
353 dump_dit_to_map(char *map_name, char *domain)
355 char *myself = "dump_dit_to_map";
356 map_ctrl map;
357 char *map_path;
359 printf("Copying LDAP data to map \"%s\", domain \"%s\".\n",
360 map_name, domain);
363 * To call update_map_from_dit() we need an initialized map_ctrl.
364 * The easiest way to get this is to generate a full path to the new
365 * map and then call map_ctrl_init().
367 map_path = (char *)am(myself, ypdbpath_sz + strlen(map_name) +
368 strlen(domain) + strlen(NTOL_PREFIX) + 3);
369 if (NULL == map_path)
370 return (FAILURE);
372 strcpy(map_path, ypdbpath);
373 add_separator(map_path);
374 strcat(map_path, domain);
375 add_separator(map_path);
376 strcat(map_path, NTOL_PREFIX);
377 strcat(map_path, map_name);
379 if (FAILURE == map_ctrl_init(&map, map_path)) {
380 sfree(map_path);
381 return (FAILURE);
384 sfree(map_path);
387 * This is called before anything else is running so don't need to
388 * do normal update lock.
390 return (update_map_from_dit(&map, TRUE));
394 * FUNCTION : add_seperator()
396 * DESCRIPTION: Adds a file separator to a string (which must already be big
397 * enough.)
399 * GIVEN : Pointer to the string
401 * RETURNS : Nothing
403 void
404 add_separator(char *str)
406 char *p;
408 p = str + strlen(str);
409 *p = SEP_CHAR;
410 *(p+1) = '\0';