1 /* $NetBSD: sun2amd.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2014 Erez Zadok 5 * Copyright (c) 2005 Daniel P. Ottavio 6 * Copyright (c) 1989 Jan-Simon Pendry 7 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 8 * Copyright (c) 1989 The Regents of the University of California. 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * Jan-Simon Pendry at Imperial College, London. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * 39 * File: am-utils/amd/sun2amd.c 40 * 41 */ 42 43 /* 44 * Translate Sun-syntax maps to Amd maps 45 */ 46 47 #ifdef HAVE_CONFIG_H 48 # include <config.h> 49 #endif /* HAVE_CONFIG_H */ 50 #include <am_defs.h> 51 #include <amd.h> 52 #include <sun_map.h> 53 54 55 /* dummies to make the program compile and link */ 56 struct amu_global_options gopt; 57 #if defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) 58 # ifdef NEED_LIBWRAP_SEVERITY_VARIABLES 59 /* 60 * Some systems that define libwrap already define these two variables 61 * in libwrap, while others don't: so I need to know precisely iff 62 * to define these two severity variables. 63 */ 64 int allow_severity=0, deny_severity=0, rfc931_timeout=0; 65 # endif /* NEED_LIBWRAP_SEVERITY_VARIABLES */ 66 #endif /* defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) */ 67 68 69 /* 70 * Parse the stream sun_in, convert the map information to amd, write 71 * the results to amd_out. 72 */ 73 static int 74 sun2amd_convert(FILE *sun_in, FILE *amd_out) 75 { 76 char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry; 77 int pos, line = 0, retval = 1; 78 79 /* just to be safe */ 80 memset(line_buff, 0, sizeof(line_buff)); 81 82 /* Read the input line by line and do the conversion. */ 83 while ((pos = file_read_line(line_buff, sizeof(line_buff), sun_in))) { 84 line++; 85 line_buff[pos - 1] = '\0'; 86 87 /* remove comments */ 88 if ((tmp = strchr(line_buff, '#')) != NULL) { 89 *tmp = '\0'; 90 } 91 92 /* find start of key */ 93 key = line_buff; 94 while (*key != '\0' && isspace((unsigned char)*key)) { 95 key++; 96 } 97 98 /* ignore blank lines */ 99 if (*key == '\0') { 100 continue; 101 } 102 103 /* find the end of the key and NULL terminate */ 104 tmp = key; 105 while (*tmp != '\0' && isspace((unsigned char)*tmp) == 0) { 106 tmp++; 107 } 108 if (*tmp == '\0') { 109 plog(XLOG_ERROR, "map line %d has no entry", line); 110 goto err; 111 } 112 *tmp++ = '\0'; 113 if (*tmp == '\0') { 114 plog(XLOG_ERROR, "map line %d has no entry", line); 115 goto err; 116 } 117 entry = tmp; 118 119 /* convert the sun entry to an amd entry */ 120 if ((tmp = sun_entry2amd(key, entry)) == NULL) { 121 plog(XLOG_ERROR, "parse error on line %d", line); 122 goto err; 123 } 124 125 if (fprintf(amd_out, "%s %s\n", key, tmp) < 0) { 126 plog(XLOG_ERROR, "can't write to output stream: %s", strerror(errno)); 127 goto err; 128 } 129 130 /* just to be safe */ 131 memset(line_buff, 0, sizeof(line_buff)); 132 } 133 134 /* success */ 135 retval = 0; 136 137 err: 138 return retval; 139 } 140 141 142 /* 143 * wrapper open function 144 */ 145 static FILE * 146 sun2amd_open(const char *path, const char *mode) 147 { 148 FILE *retval = NULL; 149 150 if ((retval = fopen(path,mode)) == NULL) { 151 plog(XLOG_ERROR,"could not open file %s",path); 152 } 153 154 return retval; 155 } 156 157 158 /* 159 * echo the usage and exit 160 */ 161 static void 162 sun2amd_usage(void) 163 { 164 fprintf(stderr, 165 "usage : sun2amd [-hH] [-i infile] [-o outfile]\n" 166 "-h\thelp\n" 167 "-i\tspecify an infile (defaults to stdin)\n" 168 "-o\tspecify an outfile (defaults to stdout)\n"); 169 } 170 171 172 int 173 main(int argc, char **argv) 174 { 175 /* default in/out to stdin/stdout */ 176 FILE *sun_in = stdin, *amd_out = stdout; 177 int opt, retval = 1; 178 179 while ((opt = getopt(argc, argv , "i:o:hH")) != -1) { 180 switch (opt) { 181 182 case 'i': 183 if ((sun_in = sun2amd_open(optarg,"r")) == NULL) { 184 goto err; 185 } 186 break; 187 188 case 'o': 189 if ((amd_out = sun2amd_open(optarg,"w")) == NULL) { 190 goto err; 191 } 192 break; 193 194 case 'h': 195 case 'H': 196 sun2amd_usage(); 197 goto err; 198 } 199 } 200 201 retval = sun2amd_convert(sun_in,amd_out); 202 203 err: 204 exit(retval); 205 } 206