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