#include <stdlib.h>
#include <dirent.h>
#include <strings.h>
#include "ypsym.h"
#include "ypdefs.h"
USE_YPDBPATH
USE_DBM
#include "shim.h"
#include "../ldap_util.h"
bool_t
ypmkfilename(domain, map, path)
char *domain;
char *map;
char *path;
{
int length;
if (strchr(domain, '/') != NULL)
return (FALSE);
length = strlen(domain) + strlen(map) + ypdbpath_sz + 3;
if (yptol_mode)
length += strlen(NTOL_PREFIX) + 1;
if ((MAXNAMLEN + 1) < length) {
(void) fprintf(stderr, "ypserv: Map name string too long.\n");
return (FALSE);
}
strcpy(path, ypdbpath);
strcat(path, "/");
strcat(path, domain);
strcat(path, "/");
if (yptol_mode)
strcat(path, NTOL_PREFIX);
strcat(path, map);
return (TRUE);
}
bool_t
on_maplist(char *mapname, char **list) {
int i = 0;
if (list == NULL) {
return (FALSE);
}
while (list[i] != NULL) {
if (strcmp(mapname, list[i++]) == 0) {
return (TRUE);
}
}
return (FALSE);
}
bool_t
add_in_maplist(char *mapname, char ***list, int *list_len) {
int i = 0;
char **list_tmp;
if (list == NULL) {
return (FALSE);
}
list_tmp = *list;
if (list_tmp == NULL) {
*list_len = 0;
} else {
while (list_tmp[i] != NULL) {
i++;
}
}
if (*list_len == -1) {
*list_len = i;
}
if (i+1 >= *list_len) {
list_tmp = (char **)realloc(list_tmp,
(*list_len + ARRAY_CHUNK) *
sizeof (char *));
if (list_tmp == NULL) {
return (FALSE);
}
*list = list_tmp;
*list_len += ARRAY_CHUNK;
}
(*list)[i] = strdup(mapname);
if ((*list)[i] == NULL) {
return (FALSE);
}
(*list)[++i] = NULL;
return (TRUE);
}
bool
ypcheck_domain_yptol(char *domain)
{
char path[MAXNAMLEN + 1];
struct stat filestat;
bool present = FALSE;
strcpy(path, ypdbpath);
strcat(path, "/");
if (strlcat(path, domain, MAXNAMLEN + 1) >= MAXNAMLEN + 1)
return (present);
if (stat(path, &filestat) != -1) {
if (S_ISDIR(filestat.st_mode))
present = TRUE;
}
return (present);
}
bool
ypcheck_map_existence_yptol(char *pname)
{
char dbfile[MAXNAMLEN + sizeof (TTL_POSTFIX) + 1];
struct stat64 filestat;
int len;
if (!pname || ((len = (int)strlen(pname)) == 0) ||
(len + sizeof (dbm_pag) + sizeof (TTL_POSTFIX)) >
sizeof (dbfile)) {
return (FALSE);
}
errno = 0;
(void) strcpy(dbfile, pname);
(void) strcat(dbfile, dbm_dir);
if (stat64(dbfile, &filestat) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr,
"ypserv: Stat error on map file %s.\n",
dbfile);
}
return (FALSE);
}
(void) strcpy(dbfile, pname);
(void) strcat(dbfile, dbm_pag);
if (stat64(dbfile, &filestat) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr,
"ypserv: Stat error on map file %s.\n",
dbfile);
}
return (FALSE);
}
if (yptol_mode) {
(void) strcpy(dbfile, pname);
(void) strcat(dbfile, TTL_POSTFIX);
(void) strcat(dbfile, dbm_dir);
if (stat64(dbfile, &filestat) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr,
"ypserv: Stat error on map file %s.\n",
dbfile);
}
return (FALSE);
}
(void) strcpy(dbfile, pname);
(void) strcat(dbfile, TTL_POSTFIX);
(void) strcat(dbfile, dbm_pag);
if (stat64(dbfile, &filestat) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr,
"ypserv: Stat error on map file %s.\n",
dbfile);
}
return (FALSE);
}
}
return (TRUE);
}
bool_t
add_map_domain_to_list(char *domain, char ***map_list)
{
char domdir[MAXNAMLEN + 1];
char path[MAXNAMLEN + 1];
int domdir_len = sizeof (domdir);
DIR *dirp;
struct dirent *dp;
int name_len;
int dbm_pag_len = sizeof (dbm_pag);
char *ext;
char *mapname;
int map_list_len = -1;
if (map_list == NULL) {
return (FALSE);
}
if (domain == NULL) {
return (TRUE);
}
if (!ypcheck_domain_yptol(domain)) {
return (TRUE);
}
if (snprintf(domdir, domdir_len, "%s/%s", ypdbpath, domain)
> domdir_len) {
return (FALSE);
}
if ((dirp = opendir(domdir)) == NULL) {
return (FALSE);
}
for (dp = readdir(dirp); dp != NULL;
dp = readdir(dirp)) {
name_len = (int)strlen(dp->d_name);
if (name_len < dbm_pag_len - 1) {
continue;
}
ext = &(dp->d_name[name_len - (dbm_pag_len - 1)]);
if (strcmp(ext, dbm_pag) != 0) {
continue;
}
*ext = '\0';
if (yptol_mode) {
if (0 != strncmp(dp->d_name, NTOL_PREFIX,
strlen(NTOL_PREFIX))) {
continue;
}
mapname = dp->d_name + strlen(NTOL_PREFIX);
} else {
if (0 == strncmp(dp->d_name, NTOL_PREFIX,
strlen(NTOL_PREFIX))) {
continue;
}
mapname = dp->d_name;
}
if (ypmkfilename(domain, mapname, path) == FALSE) {
(void) closedir(dirp);
return (FALSE);
}
if (ypcheck_map_existence_yptol(path) &&
!on_maplist(mapname, *map_list)) {
if (add_in_maplist(mapname, map_list, &map_list_len) ==
FALSE) {
(void) closedir(dirp);
return (FALSE);
}
}
}
(void) closedir(dirp);
return (TRUE);
}