xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/soconfig.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1991-1996,2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <ctype.h>
35*7c478bd9Sstevel@tonic-gate #include <locale.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	MAXLINELEN	4096
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Usage:
41*7c478bd9Sstevel@tonic-gate  *	sonconfig -f <file>
42*7c478bd9Sstevel@tonic-gate  *		Reads input from file. The file is structured as
43*7c478bd9Sstevel@tonic-gate  *			 <fam> <type> <protocol> <path>
44*7c478bd9Sstevel@tonic-gate  *			 <fam> <type> <protocol>
45*7c478bd9Sstevel@tonic-gate  *		with the first line registering and the second line
46*7c478bd9Sstevel@tonic-gate  *		deregistering.
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  *	soconfig <fam> <type> <protocol> <path>
49*7c478bd9Sstevel@tonic-gate  *		registers
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  *	soconfig <fam> <type> <protocol>
52*7c478bd9Sstevel@tonic-gate  *		deregisters
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static int	parse_file(char *filename);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int	split_line(char *line, char *argvec[], int maxargvec);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static int	parse_params(char *famstr, char *typestr, char *protostr,
60*7c478bd9Sstevel@tonic-gate 				char *path, int line);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int	parse_int(char *str);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static void	usage(void);
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate int
67*7c478bd9Sstevel@tonic-gate main(argc, argv)
68*7c478bd9Sstevel@tonic-gate 	int argc;
69*7c478bd9Sstevel@tonic-gate 	char *argv[];
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	int ret;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	argc--; argv++;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
76*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
77*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
78*7c478bd9Sstevel@tonic-gate #endif
79*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (argc == 2 && strcmp(argv[0], "-f") == 0) {
82*7c478bd9Sstevel@tonic-gate 		ret = parse_file(argv[1]);
83*7c478bd9Sstevel@tonic-gate 		exit(ret);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	if (argc == 3) {
86*7c478bd9Sstevel@tonic-gate 		ret = parse_params(argv[0], argv[1], argv[2], NULL, -1);
87*7c478bd9Sstevel@tonic-gate 		exit(ret);
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 	if (argc == 4) {
90*7c478bd9Sstevel@tonic-gate 		ret = parse_params(argv[0], argv[1], argv[2], argv[3], -1);
91*7c478bd9Sstevel@tonic-gate 		exit(ret);
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	usage();
94*7c478bd9Sstevel@tonic-gate 	exit(1);
95*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate static void
99*7c478bd9Sstevel@tonic-gate usage(void)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
102*7c478bd9Sstevel@tonic-gate 		"Usage:	soconfig -f <file>\n"
103*7c478bd9Sstevel@tonic-gate 		"\tsoconfig <fam> <type> <protocol> <path>\n"
104*7c478bd9Sstevel@tonic-gate 		"\tsoconfig <fam> <type> <protocol>\n"));
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate  * Open the specified file and parse each line. Skip comments (everything
109*7c478bd9Sstevel@tonic-gate  * after a '#'). Return 1 if at least one error was encountered; otherwise 0.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate static int
112*7c478bd9Sstevel@tonic-gate parse_file(char *filename)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	char line[MAXLINELEN];
115*7c478bd9Sstevel@tonic-gate 	char pline[MAXLINELEN];
116*7c478bd9Sstevel@tonic-gate 	int argcount;
117*7c478bd9Sstevel@tonic-gate 	char *argvec[20];
118*7c478bd9Sstevel@tonic-gate 	FILE *fp;
119*7c478bd9Sstevel@tonic-gate 	int linecount = 0;
120*7c478bd9Sstevel@tonic-gate 	int numerror = 0;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	fp = fopen(filename, "r");
123*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
124*7c478bd9Sstevel@tonic-gate 		perror("soconfig: open");
125*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n");
126*7c478bd9Sstevel@tonic-gate 		usage();
127*7c478bd9Sstevel@tonic-gate 		return (1);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp) != NULL) {
131*7c478bd9Sstevel@tonic-gate 		linecount++;
132*7c478bd9Sstevel@tonic-gate 		strcpy(pline, line);
133*7c478bd9Sstevel@tonic-gate 		argcount = split_line(pline, argvec,
134*7c478bd9Sstevel@tonic-gate 				sizeof (argvec) / sizeof (argvec[0]));
135*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
136*7c478bd9Sstevel@tonic-gate 		{
137*7c478bd9Sstevel@tonic-gate 			int i;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 			printf("scanned %d args\n", argcount);
140*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < argcount; i++)
141*7c478bd9Sstevel@tonic-gate 				printf("arg[%d]: %s\n", i, argvec[i]);
142*7c478bd9Sstevel@tonic-gate 		}
143*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
144*7c478bd9Sstevel@tonic-gate 		switch (argcount) {
145*7c478bd9Sstevel@tonic-gate 		case 0:
146*7c478bd9Sstevel@tonic-gate 			/* Empty line - or comment only line */
147*7c478bd9Sstevel@tonic-gate 			break;
148*7c478bd9Sstevel@tonic-gate 		case 3:
149*7c478bd9Sstevel@tonic-gate 			numerror += parse_params(argvec[0], argvec[1],
150*7c478bd9Sstevel@tonic-gate 					argvec[2], NULL, linecount);
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		case 4:
153*7c478bd9Sstevel@tonic-gate 			numerror += parse_params(argvec[0], argvec[1],
154*7c478bd9Sstevel@tonic-gate 					argvec[2], argvec[3], linecount);
155*7c478bd9Sstevel@tonic-gate 			break;
156*7c478bd9Sstevel@tonic-gate 		default:
157*7c478bd9Sstevel@tonic-gate 			numerror++;
158*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
159*7c478bd9Sstevel@tonic-gate 				gettext("Malformed line: <%s>\n"), line);
160*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
161*7c478bd9Sstevel@tonic-gate 				gettext("\ton line %d\n"), linecount);
162*7c478bd9Sstevel@tonic-gate 			break;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if (numerror > 0)
168*7c478bd9Sstevel@tonic-gate 		return (1);
169*7c478bd9Sstevel@tonic-gate 	else
170*7c478bd9Sstevel@tonic-gate 		return (0);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Parse a line splitting it off at whitspace characters.
175*7c478bd9Sstevel@tonic-gate  * Modifies the content of the string by inserting NULLs.
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate static int
178*7c478bd9Sstevel@tonic-gate split_line(char *line, char *argvec[], int maxargvec)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	int i = 0;
181*7c478bd9Sstevel@tonic-gate 	char *cp;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	/* Truncate at the beginning of a comment */
184*7c478bd9Sstevel@tonic-gate 	cp = strchr(line, '#');
185*7c478bd9Sstevel@tonic-gate 	if (cp != NULL)
186*7c478bd9Sstevel@tonic-gate 		*cp = NULL;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
189*7c478bd9Sstevel@tonic-gate 	while (1) {
190*7c478bd9Sstevel@tonic-gate 		/* Skip any whitespace */
191*7c478bd9Sstevel@tonic-gate 		while (isspace(*line) && *line != NULL)
192*7c478bd9Sstevel@tonic-gate 			line++;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		if (i >= maxargvec)
195*7c478bd9Sstevel@tonic-gate 			return (i);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		argvec[i] = line;
198*7c478bd9Sstevel@tonic-gate 		if (*line == NULL)
199*7c478bd9Sstevel@tonic-gate 			return (i);
200*7c478bd9Sstevel@tonic-gate 		i++;
201*7c478bd9Sstevel@tonic-gate 		/* Skip until next whitespace */
202*7c478bd9Sstevel@tonic-gate 		while (!isspace(*line) && *line != NULL)
203*7c478bd9Sstevel@tonic-gate 			line++;
204*7c478bd9Sstevel@tonic-gate 		if (*line != NULL) {
205*7c478bd9Sstevel@tonic-gate 			/* Break off argument */
206*7c478bd9Sstevel@tonic-gate 			*line++ = NULL;
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate  * Parse the set of parameters and issues the sockconfig syscall.
214*7c478bd9Sstevel@tonic-gate  * If line is not -1 it is assumed to be the line number in the file.
215*7c478bd9Sstevel@tonic-gate  */
216*7c478bd9Sstevel@tonic-gate static int
217*7c478bd9Sstevel@tonic-gate parse_params(char *famstr, char *typestr, char *protostr, char *path, int line)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	int fam, type, protocol;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	fam = parse_int(famstr);
222*7c478bd9Sstevel@tonic-gate 	if (fam == -1) {
223*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Bad family number: %s\n"), famstr);
224*7c478bd9Sstevel@tonic-gate 		if (line != -1)
225*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
226*7c478bd9Sstevel@tonic-gate 				gettext("\ton line %d\n"), line);
227*7c478bd9Sstevel@tonic-gate 		else {
228*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n");
229*7c478bd9Sstevel@tonic-gate 			usage();
230*7c478bd9Sstevel@tonic-gate 		}
231*7c478bd9Sstevel@tonic-gate 		return (1);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	type = parse_int(typestr);
235*7c478bd9Sstevel@tonic-gate 	if (type == -1) {
236*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
237*7c478bd9Sstevel@tonic-gate 			gettext("Bad socket type number: %s\n"), typestr);
238*7c478bd9Sstevel@tonic-gate 		if (line != -1)
239*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
240*7c478bd9Sstevel@tonic-gate 				gettext("\ton line %d\n"), line);
241*7c478bd9Sstevel@tonic-gate 		else {
242*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n");
243*7c478bd9Sstevel@tonic-gate 			usage();
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 		return (1);
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	protocol = parse_int(protostr);
249*7c478bd9Sstevel@tonic-gate 	if (protocol == -1) {
250*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
251*7c478bd9Sstevel@tonic-gate 			gettext("Bad protocol number: %s\n"), protostr);
252*7c478bd9Sstevel@tonic-gate 		if (line != -1)
253*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
254*7c478bd9Sstevel@tonic-gate 				gettext("\ton line %d\n"), line);
255*7c478bd9Sstevel@tonic-gate 		else {
256*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n");
257*7c478bd9Sstevel@tonic-gate 			usage();
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 		return (1);
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (path != NULL) {
264*7c478bd9Sstevel@tonic-gate 		struct stat stats;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		if (stat(path, &stats) == -1) {
267*7c478bd9Sstevel@tonic-gate 			perror(path);
268*7c478bd9Sstevel@tonic-gate 			if (line != -1)
269*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
270*7c478bd9Sstevel@tonic-gate 					gettext("\ton line %d\n"), line);
271*7c478bd9Sstevel@tonic-gate 			else {
272*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n");
273*7c478bd9Sstevel@tonic-gate 				usage();
274*7c478bd9Sstevel@tonic-gate 			}
275*7c478bd9Sstevel@tonic-gate 			return (1);
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
280*7c478bd9Sstevel@tonic-gate 	printf("not calling sockconfig(%d, %d, %d, %s)\n",
281*7c478bd9Sstevel@tonic-gate 		fam, type, protocol, path == NULL ? "(null)" : path);
282*7c478bd9Sstevel@tonic-gate #else
283*7c478bd9Sstevel@tonic-gate 	if (_sockconfig(fam, type, protocol, path) == -1) {
284*7c478bd9Sstevel@tonic-gate 		perror("sockconfig");
285*7c478bd9Sstevel@tonic-gate 		return (1);
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate #endif
288*7c478bd9Sstevel@tonic-gate 	return (0);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate static int
292*7c478bd9Sstevel@tonic-gate parse_int(char *str)
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	char *end;
295*7c478bd9Sstevel@tonic-gate 	int res;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	res = strtol(str, &end, 0);
298*7c478bd9Sstevel@tonic-gate 	if (end == str)
299*7c478bd9Sstevel@tonic-gate 		return (-1);
300*7c478bd9Sstevel@tonic-gate 	return (res);
301*7c478bd9Sstevel@tonic-gate }
302