#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ypalias_init.c,v 1.3 2024/06/02 11:44:29 andvar Exp $");
#endif
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <paths.h>
#include <errno.h>
#include "ypalias_init.h"
#ifndef _PATH_YPNICKNAMES
#define _PATH_YPNICKNAMES "/var/yp/nicknames"
#endif
const struct ypalias def_ypaliases[] = {
{ "passwd", "passwd.byname" },
{ "group", "group.byname" },
{ "networks", "networks.byaddr" },
{ "hosts", "hosts.byaddr" },
{ "protocols", "protocols.bynumber" },
{ "services", "services.byname" },
{ "aliases", "mail.aliases" },
{ "ethers", "ethers.byname" },
{ NULL, NULL },
};
const struct ypalias *
ypalias_init(void)
{
FILE *fp;
char *cp, *line;
struct ypalias *ypa;
size_t nypalias = 50;
size_t i = 0, len, lineno;
if ((fp = fopen(_PATH_YPNICKNAMES, "r")) == NULL)
return &def_ypaliases[0];
if ((ypa = calloc(sizeof(*ypa), nypalias)) == NULL)
goto out;
lineno = 1;
for (; (line = fparseln(fp, &len, &lineno, NULL,
FPARSELN_UNESCALL));) {
cp = line;
if ((ypa[i].alias = strsep(&line, " \t\n")) == NULL ||
(ypa[i].name = strsep(&line, " \t\n")) == NULL ||
ypa[i].alias != cp) {
warnx("%s, %zu: syntax error, ignored",
_PATH_YPNICKNAMES, lineno);
free(cp);
} else
i++;
if (i == nypalias) {
nypalias <<= 1;
if (reallocarr(&ypa, nypalias, sizeof(*ypa)) != 0)
goto out;
}
}
ypa[i].alias = ypa[i].name = NULL;
i++;
(void)fclose(fp);
if (reallocarr(&ypa, i, sizeof(*ypa)) == 0)
return ypa;
out:
warn("Cannot allocate alias space, returning default list");
if (ypa) {
do
free(__UNCONST(ypa[--i].alias));
while (i != 0);
free(ypa);
}
(void)fclose(fp);
return def_ypaliases;
}