1*3b9e8573Sandvar /* $NetBSD: ypalias_init.c,v 1.3 2024/06/02 11:44:29 andvar Exp $ */
2e5962c58Schristos
3e5962c58Schristos /*
4e5962c58Schristos * Copyright (c) 2009 The NetBSD Foundation
5e5962c58Schristos * All rights reserved.
6e5962c58Schristos *
7e5962c58Schristos * Redistribution and use in source and binary forms, with or without
8e5962c58Schristos * modification, are permitted provided that the following conditions
9e5962c58Schristos * are met:
10e5962c58Schristos * 1. Redistributions of source code must retain the above copyright
11e5962c58Schristos * notice, this list of conditions and the following disclaimer.
12e5962c58Schristos * 2. Redistributions in binary form must reproduce the above copyright
13e5962c58Schristos * notice, this list of conditions and the following disclaimer in the
14e5962c58Schristos * documentation and/or other materials provided with the distribution.
15e5962c58Schristos * 3. The name of the author may not be used to endorse or promote
16e5962c58Schristos * products derived from this software without specific prior written
17e5962c58Schristos * permission.
18e5962c58Schristos *
19e5962c58Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20e5962c58Schristos * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21e5962c58Schristos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e5962c58Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23e5962c58Schristos * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e5962c58Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25e5962c58Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26e5962c58Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27e5962c58Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28e5962c58Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29e5962c58Schristos * SUCH DAMAGE.
30e5962c58Schristos */
31e5962c58Schristos
32e5962c58Schristos /* Code contributed by Greywolf <greywolf@starwolf.com>
33e5962c58Schristos * 28 August 2003. All rights released to The NetBSD Foundation.
34e5962c58Schristos */
35e5962c58Schristos
36e5962c58Schristos #include <sys/cdefs.h>
37e5962c58Schristos #ifndef lint
38*3b9e8573Sandvar __RCSID("$NetBSD: ypalias_init.c,v 1.3 2024/06/02 11:44:29 andvar Exp $");
39e5962c58Schristos #endif
40e5962c58Schristos
41e5962c58Schristos #include <err.h>
42e5962c58Schristos #include <stdio.h>
43e5962c58Schristos #include <stdlib.h>
44e5962c58Schristos #include <string.h>
45e5962c58Schristos #include <unistd.h>
46e5962c58Schristos #include <paths.h>
47e5962c58Schristos #include <errno.h>
48e5962c58Schristos
49e5962c58Schristos #include "ypalias_init.h"
50e5962c58Schristos
51e5962c58Schristos #ifndef _PATH_YPNICKNAMES
52e5962c58Schristos #define _PATH_YPNICKNAMES "/var/yp/nicknames"
53e5962c58Schristos #endif
54e5962c58Schristos
55e5962c58Schristos const struct ypalias def_ypaliases[] = {
56e5962c58Schristos { "passwd", "passwd.byname" },
57e5962c58Schristos { "group", "group.byname" },
58e5962c58Schristos { "networks", "networks.byaddr" },
59e5962c58Schristos { "hosts", "hosts.byaddr" },
60e5962c58Schristos { "protocols", "protocols.bynumber" },
61e5962c58Schristos { "services", "services.byname" },
62e5962c58Schristos { "aliases", "mail.aliases" },
63e5962c58Schristos { "ethers", "ethers.byname" },
64e5962c58Schristos { NULL, NULL },
65e5962c58Schristos };
66e5962c58Schristos
67e5962c58Schristos const struct ypalias *
ypalias_init(void)68e5962c58Schristos ypalias_init(void)
69e5962c58Schristos {
70e5962c58Schristos FILE *fp;
71e5962c58Schristos char *cp, *line;
72264da3e6Snia struct ypalias *ypa;
73e5962c58Schristos size_t nypalias = 50;
74264da3e6Snia size_t i = 0, len, lineno;
75e5962c58Schristos
76e5962c58Schristos if ((fp = fopen(_PATH_YPNICKNAMES, "r")) == NULL)
77e5962c58Schristos return &def_ypaliases[0];
78e5962c58Schristos
79e5962c58Schristos if ((ypa = calloc(sizeof(*ypa), nypalias)) == NULL)
80e5962c58Schristos goto out;
81e5962c58Schristos
82e5962c58Schristos lineno = 1;
83264da3e6Snia for (; (line = fparseln(fp, &len, &lineno, NULL,
84e5962c58Schristos FPARSELN_UNESCALL));) {
85e5962c58Schristos cp = line;
86e5962c58Schristos /* Ignore malformed lines */
87e5962c58Schristos if ((ypa[i].alias = strsep(&line, " \t\n")) == NULL ||
88e5962c58Schristos (ypa[i].name = strsep(&line, " \t\n")) == NULL ||
89e5962c58Schristos ypa[i].alias != cp) {
90e5962c58Schristos warnx("%s, %zu: syntax error, ignored",
91e5962c58Schristos _PATH_YPNICKNAMES, lineno);
92e5962c58Schristos free(cp);
93e5962c58Schristos } else
94e5962c58Schristos i++;
95e5962c58Schristos if (i == nypalias) {
96e5962c58Schristos nypalias <<= 1;
97264da3e6Snia if (reallocarr(&ypa, nypalias, sizeof(*ypa)) != 0)
98e5962c58Schristos goto out;
99e5962c58Schristos }
100e5962c58Schristos }
101e5962c58Schristos ypa[i].alias = ypa[i].name = NULL;
102e5962c58Schristos i++;
103e5962c58Schristos
104e5962c58Schristos (void)fclose(fp);
105264da3e6Snia if (reallocarr(&ypa, i, sizeof(*ypa)) == 0)
106264da3e6Snia return ypa;
107e5962c58Schristos out:
108*3b9e8573Sandvar warn("Cannot allocate alias space, returning default list");
109e5962c58Schristos if (ypa) {
110e5962c58Schristos do
111e5962c58Schristos free(__UNCONST(ypa[--i].alias));
112e5962c58Schristos while (i != 0);
113e5962c58Schristos free(ypa);
114e5962c58Schristos }
115e5962c58Schristos (void)fclose(fp);
116e5962c58Schristos return def_ypaliases;
117e5962c58Schristos }
118