#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"
USE_YPDBPATH
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();
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);
for (i = 0; i < num_doms; i++) {
if (FAILURE == dump_domain_to_dit(dom_list[i], init_containers))
return (FAILURE);
}
return (SUCCESS);
}
suc_code
dump_domain_to_dit(char *dom_name, bool_t init_containers)
{
char **map_list;
int i;
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);
}
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);
}
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;
int entry_count;
int next_print;
printf("Copying map \"%s\", domain \"%s\", to LDAP.\n",
map_name, domain);
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);
}
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);
dbm = dbm_open(map_path, O_RDONLY, 0644);
sfree(map_path);
if (NULL == dbm) {
return (SUCCESS);
}
for (key = dbm_firstkey(dbm), next_print = PRINT_FREQ, entry_count = 1;
NULL != key.dptr; key = dbm_nextkey(dbm), entry_count ++) {
if (0 == key.dsize) {
logmsg(MSG_NOTIMECHECK, LOG_INFO,
"Zero length key ignored in %s %s", map_name, domain);
continue;
}
if (is_special_key(&key))
continue;
value = dbm_fetch(dbm, key);
if (SUCCESS != write_to_dit(map_name, domain, key, value,
TRUE, TRUE))
break;
if (entry_count >= next_print) {
printf("%d entries processed.\n", entry_count);
next_print *= 2;
}
}
dbm_close(dbm);
return (SUCCESS);
}
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";
dom_count = get_mapping_domain_list(&dom_list);
if (0 == dom_count) {
return (SUCCESS);
}
for (i = 0; i < dom_count; i++) {
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 (EEXIST != errno) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Could not make create domain directory %s",
dom_path);
sfree(dom_path);
}
}
sfree(dom_path);
map_list = get_mapping_map_list(dom_list[i]);
if (NULL == map_list) {
continue;
}
for (j = 0; map_list[j] != NULL; j++) {
if (FAILURE == dump_dit_to_map(map_list[j],
dom_list[i])) {
free_map_list(map_list);
return (FAILURE);
}
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);
}
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);
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);
return (update_map_from_dit(&map, TRUE));
}
void
add_separator(char *str)
{
char *p;
p = str + strlen(str);
*p = SEP_CHAR;
*(p+1) = '\0';
}