1305100Scy/* 2305100Scy * Copyright (c) 1997-2014 Erez Zadok 3305100Scy * Copyright (c) 2005 Daniel P. Ottavio 4305100Scy * Copyright (c) 1989 Jan-Simon Pendry 5305100Scy * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 6305100Scy * Copyright (c) 1989 The Regents of the University of California. 7305100Scy * All rights reserved. 8305100Scy * 9305100Scy * This code is derived from software contributed to Berkeley by 10305100Scy * Jan-Simon Pendry at Imperial College, London. 11305100Scy * 12305100Scy * Redistribution and use in source and binary forms, with or without 13305100Scy * modification, are permitted provided that the following conditions 14305100Scy * are met: 15305100Scy * 1. Redistributions of source code must retain the above copyright 16305100Scy * notice, this list of conditions and the following disclaimer. 17305100Scy * 2. Redistributions in binary form must reproduce the above copyright 18305100Scy * notice, this list of conditions and the following disclaimer in the 19305100Scy * documentation and/or other materials provided with the distribution. 20305100Scy * 3. Neither the name of the University nor the names of its contributors 21305100Scy * may be used to endorse or promote products derived from this software 22305100Scy * without specific prior written permission. 23305100Scy * 24305100Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25305100Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26305100Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27305100Scy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28305100Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29305100Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30305100Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31305100Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32305100Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33305100Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34305100Scy * SUCH DAMAGE. 35305100Scy * 36305100Scy * 37305100Scy * File: am-utils/amd/sun2amd.c 38305100Scy * 39305100Scy */ 40305100Scy 41305100Scy/* 42305100Scy * Translate Sun-syntax maps to Amd maps 43305100Scy */ 44305100Scy 45305100Scy#ifdef HAVE_CONFIG_H 46305100Scy# include <config.h> 47305100Scy#endif /* HAVE_CONFIG_H */ 48305100Scy#include <am_defs.h> 49305100Scy#include <amd.h> 50305100Scy#include <sun_map.h> 51305100Scy 52305100Scy 53305100Scy/* dummies to make the program compile and link */ 54305100Scystruct amu_global_options gopt; 55305100Scy#if defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) 56305100Scy# ifdef NEED_LIBWRAP_SEVERITY_VARIABLES 57305100Scy/* 58305100Scy * Some systems that define libwrap already define these two variables 59305100Scy * in libwrap, while others don't: so I need to know precisely iff 60305100Scy * to define these two severity variables. 61305100Scy */ 62305100Scyint allow_severity=0, deny_severity=0, rfc931_timeout=0; 63305100Scy# endif /* NEED_LIBWRAP_SEVERITY_VARIABLES */ 64305100Scy#endif /* defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) */ 65305100Scy 66305100Scy 67305100Scy/* 68305100Scy * Parse the stream sun_in, convert the map information to amd, write 69305100Scy * the results to amd_out. 70305100Scy */ 71305100Scystatic int 72305100Scysun2amd_convert(FILE *sun_in, FILE *amd_out) 73305100Scy{ 74305100Scy char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry; 75305100Scy int pos, line = 0, retval = 1; 76305100Scy 77305100Scy /* just to be safe */ 78305100Scy memset(line_buff, 0, sizeof(line_buff)); 79305100Scy 80305100Scy /* Read the input line by line and do the conversion. */ 81305100Scy while ((pos = file_read_line(line_buff, sizeof(line_buff), sun_in))) { 82305100Scy line++; 83305100Scy line_buff[pos - 1] = '\0'; 84305100Scy 85305100Scy /* remove comments */ 86305100Scy if ((tmp = strchr(line_buff, '#')) != NULL) { 87305100Scy *tmp = '\0'; 88305100Scy } 89305100Scy 90305100Scy /* find start of key */ 91305100Scy key = line_buff; 92305100Scy while (*key != '\0' && isspace((unsigned char)*key)) { 93305100Scy key++; 94305100Scy } 95305100Scy 96305100Scy /* ignore blank lines */ 97305100Scy if (*key == '\0') { 98305100Scy continue; 99305100Scy } 100305100Scy 101305100Scy /* find the end of the key and NULL terminate */ 102305100Scy tmp = key; 103305100Scy while (*tmp != '\0' && isspace((unsigned char)*tmp) == 0) { 104305100Scy tmp++; 105305100Scy } 106305100Scy if (*tmp == '\0') { 107305100Scy plog(XLOG_ERROR, "map line %d has no entry", line); 108305100Scy goto err; 109305100Scy } 110305100Scy *tmp++ = '\0'; 111305100Scy if (*tmp == '\0') { 112305100Scy plog(XLOG_ERROR, "map line %d has no entry", line); 113305100Scy goto err; 114305100Scy } 115305100Scy entry = tmp; 116305100Scy 117305100Scy /* convert the sun entry to an amd entry */ 118305100Scy if ((tmp = sun_entry2amd(key, entry)) == NULL) { 119305100Scy plog(XLOG_ERROR, "parse error on line %d", line); 120305100Scy goto err; 121305100Scy } 122305100Scy 123305100Scy if (fprintf(amd_out, "%s %s\n", key, tmp) < 0) { 124305100Scy plog(XLOG_ERROR, "can't write to output stream: %s", strerror(errno)); 125305100Scy goto err; 126305100Scy } 127305100Scy 128305100Scy /* just to be safe */ 129305100Scy memset(line_buff, 0, sizeof(line_buff)); 130305100Scy } 131305100Scy 132305100Scy /* success */ 133305100Scy retval = 0; 134305100Scy 135305100Scy err: 136305100Scy return retval; 137305100Scy} 138305100Scy 139305100Scy 140305100Scy/* 141305100Scy * wrapper open function 142305100Scy */ 143305100Scystatic FILE * 144305100Scysun2amd_open(const char *path, const char *mode) 145305100Scy{ 146305100Scy FILE *retval = NULL; 147305100Scy 148305100Scy if ((retval = fopen(path,mode)) == NULL) { 149305100Scy plog(XLOG_ERROR,"could not open file %s",path); 150305100Scy } 151305100Scy 152305100Scy return retval; 153305100Scy} 154305100Scy 155305100Scy 156305100Scy/* 157305100Scy * echo the usage and exit 158305100Scy */ 159305100Scystatic void 160305100Scysun2amd_usage(void) 161305100Scy{ 162305100Scy fprintf(stderr, 163305100Scy "usage : sun2amd [-hH] [-i infile] [-o outfile]\n" 164305100Scy "-h\thelp\n" 165305100Scy "-i\tspecify an infile (defaults to stdin)\n" 166305100Scy "-o\tspecify an outfile (defaults to stdout)\n"); 167305100Scy} 168305100Scy 169305100Scy 170305100Scyint 171305100Scymain(int argc, char **argv) 172305100Scy{ 173305100Scy /* default in/out to stdin/stdout */ 174305100Scy FILE *sun_in = stdin, *amd_out = stdout; 175305100Scy int opt, retval = 1; 176305100Scy 177305100Scy while ((opt = getopt(argc, argv , "i:o:hH")) != -1) { 178305100Scy switch (opt) { 179305100Scy 180305100Scy case 'i': 181305100Scy if ((sun_in = sun2amd_open(optarg,"r")) == NULL) { 182305100Scy goto err; 183305100Scy } 184305100Scy break; 185305100Scy 186305100Scy case 'o': 187305100Scy if ((amd_out = sun2amd_open(optarg,"w")) == NULL) { 188305100Scy goto err; 189305100Scy } 190305100Scy break; 191305100Scy 192305100Scy case 'h': 193305100Scy case 'H': 194305100Scy sun2amd_usage(); 195305100Scy goto err; 196305100Scy } 197305100Scy } 198305100Scy 199305100Scy retval = sun2amd_convert(sun_in,amd_out); 200305100Scy 201305100Scy err: 202305100Scy exit(retval); 203305100Scy} 204