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