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
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]
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.
40 #include "../ldap_util.h"
43 * Switch on parts of ypdefs.h
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
66 * GIVEN : Flag indicating if containers and domains should be set up.
68 * RETURNS : Success code
71 dump_maps_to_dit(bool_t init_containers
)
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
))
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
100 dump_domain_to_dit(char *dom_name
, bool_t init_containers
)
105 /* Set up nis domain object */
106 if (SUCCESS
!= make_nis_domain(dom_name
, init_containers
)) {
108 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
109 "Could not make nisDomain object for %s", dom_name
);
111 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
112 "Problem detected with nisDomain object for %s",
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
);
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
);
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)
141 * Flag indicating if containers should be set up.
143 * RETURNS : SUCCESS = Map copy completed
144 * FAILURE = Map copy not completed
147 dump_map_to_dit(char *map_name
, char *domain
, bool_t init_containers
)
149 char *myself
= "dump_map_to_dit";
153 char *map_path
; /* Qualified map name */
157 printf("Copying map \"%s\", domain \"%s\", to LDAP.\n",
160 /* Create the NIS container */
161 if (SUCCESS
!= make_nis_container(map_name
, domain
, init_containers
)) {
163 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
164 "Could not make container for %s %s",
167 logmsg(MSG_NOTIMECHECK
, LOG_ERR
,
168 "Problem detected with container for %s %s",
174 /* Make up fully qualified map name */
175 map_path
= (char *)am(myself
, strlen(domain
) + strlen(map_name
) +
177 if (NULL
== map_path
) {
178 logmsg(MSG_NOMEM
, LOG_ERR
,
179 "Could not alloc memory for %s %s", map_name
, domain
);
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 */
196 * This map probably didn't exist. No problem, user may be
197 * going to populate container using LDAP.
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
);
218 /* Don't write 'special' nis entries */
219 if (is_special_key(&key
))
223 value
= dbm_fetch(dbm
, key
);
225 /* Copy entry to DIT */
226 if (SUCCESS
!= write_to_dit(map_name
, domain
, key
, value
,
228 /* Syslog will have already been done */
231 /* If necessary print a progress report */
232 if (entry_count
>= next_print
) {
233 printf("%d entries processed.\n", entry_count
);
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.
254 * RETURNS : Success code
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 */
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
) {
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",
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 */
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
],
312 free_map_list(map_list
);
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
,
320 free_map_list(map_list
);
323 if (FAILURE
== dump_dit_to_map(NETGROUP_BYUSER
,
325 free_map_list(map_list
);
330 free_map_list(map_list
);
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
346 * GIVEN : Map name (unqualified)
349 * RETURNS : SUCCESS = Map copy complete
353 dump_dit_to_map(char *map_name
, char *domain
)
355 char *myself
= "dump_dit_to_map";
359 printf("Copying LDAP data to map \"%s\", domain \"%s\".\n",
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
)
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
)) {
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
399 * GIVEN : Pointer to the string
404 add_separator(char *str
)
408 p
= str
+ strlen(str
);