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