xref: /onnv-gate/usr/src/lib/libnsl/dial/sysfiles.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
22*13093SRoger.Faulkner@Oracle.COM /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24*13093SRoger.Faulkner@Oracle.COM  */
25*13093SRoger.Faulkner@Oracle.COM 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27132Srobinson /*	  All Rights Reserved	*/
280Sstevel@tonic-gate 
291219Sraf #include "mt.h"
300Sstevel@tonic-gate #include "uucp.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <unistd.h>
33132Srobinson #include <string.h>
340Sstevel@tonic-gate #include "sysfiles.h"
350Sstevel@tonic-gate #include <sys/stropts.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * manage systems files (Systems, Devices, and Dialcodes families).
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * also manage new file Devconfig, allows per-device setup.
410Sstevel@tonic-gate  * present use is to specify what streams modules to push/pop for
420Sstevel@tonic-gate  * AT&T TLI/streams network.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * TODO:
450Sstevel@tonic-gate  *	call bsfix()?
460Sstevel@tonic-gate  *	combine the 3 versions of everything (sys, dev, and dial) into one.
470Sstevel@tonic-gate  *	allow arbitrary classes of service.
480Sstevel@tonic-gate  *	need verifysys() for uucheck.
490Sstevel@tonic-gate  *	nameserver interface?
50132Srobinson  *	pass sysname (or 0) to getsysline().  (might want reg. exp. or
51132Srobinson  *		NS processing)
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* private variables */
55132Srobinson static void tokenize(void);
56132Srobinson static void nameparse(void);
57132Srobinson static void setfile(char **, char *);
58132Srobinson static void setioctl(char **, char *);
59132Srobinson static void scansys(const char *);
60132Srobinson static void scancfg(char *, char *);
61132Srobinson static void setconfig(void);
62132Srobinson static int namematch(const char *label, char *line, const char *name);
63132Srobinson static int nextdialers(void);
64132Srobinson static int nextdevices(void);
65132Srobinson static int nextsystems(void);
66*13093SRoger.Faulkner@Oracle.COM static int getaline(FILE *, char *);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* pointer arrays might be dynamically allocated */
690Sstevel@tonic-gate static char *Systems[64];	/* list of Systems files */
700Sstevel@tonic-gate static char *Devices[64];	/* list of Devices files */
710Sstevel@tonic-gate static char *Dialers[64];	/* list of Dialers files */
720Sstevel@tonic-gate static char *Pops[64];		/* list of STREAMS modules to be popped */
730Sstevel@tonic-gate static char *Pushes[64];	/* list of STREAMS modules to be pushed */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static int nsystems;		/* index into list of Systems files */
760Sstevel@tonic-gate static int ndevices;		/* index into list of Devices files */
770Sstevel@tonic-gate static int ndialers;		/* index into list of Dialers files */
780Sstevel@tonic-gate static int npops;		/* index into list of STREAMS modules */
79132Srobinson 							/* to be popped */
800Sstevel@tonic-gate static int npushes;		/* index into list of STREAMS modules */
81132Srobinson 							/* to be pushed */
820Sstevel@tonic-gate 
83132Srobinson static unsigned connecttime, expecttime;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static FILE *fsystems;
860Sstevel@tonic-gate static FILE *fdevices;
870Sstevel@tonic-gate static FILE *fdialers;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* this might be dynamically allocated */
90132Srobinson #define	NTOKENS 16
910Sstevel@tonic-gate static char *tokens[NTOKENS], **tokptr;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* export these */
94132Srobinson static void setservice(const char *service);
95132Srobinson static void sysreset(void);
96132Srobinson static void devreset(void);
97132Srobinson static void dialreset(void);
98132Srobinson static void setdevcfg(char *, char *);
99132Srobinson static void setservice(const char *);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* import these */
102132Srobinson extern char *strsave(const char *);
103132Srobinson static int eaccess(char *, mode_t);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * setservice init's Systems, Devices, Dialers lists from Sysfiles
1070Sstevel@tonic-gate  */
108132Srobinson static void
setservice(const char * service)109132Srobinson setservice(const char *service)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	setconfig();
1120Sstevel@tonic-gate 	scansys(service);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * setdevcfg init's Pops, Pushes lists from Devconfig
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate 
119132Srobinson static void
setdevcfg(char * service,char * device)120132Srobinson setdevcfg(char *service, char *device)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	scancfg(service, device);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*	administrative files access */
126132Srobinson static int
sysaccess(int type)127132Srobinson sysaccess(int type)
1280Sstevel@tonic-gate {
129132Srobinson 	char errformat[BUFSIZ];
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	switch (type) {
1320Sstevel@tonic-gate 	case ACCESS_SYSTEMS:
1330Sstevel@tonic-gate 		return (access(Systems[nsystems], R_OK));
1340Sstevel@tonic-gate 	case ACCESS_DEVICES:
1350Sstevel@tonic-gate 		return (access(Devices[ndevices], R_OK));
1360Sstevel@tonic-gate 	case ACCESS_DIALERS:
1370Sstevel@tonic-gate 		return (access(Dialers[ndialers], R_OK));
1380Sstevel@tonic-gate 	case EACCESS_SYSTEMS:
139132Srobinson 		return (eaccess(Systems[nsystems], R_OK));
1400Sstevel@tonic-gate 	case EACCESS_DEVICES:
141132Srobinson 		return (eaccess(Devices[ndevices], R_OK));
1420Sstevel@tonic-gate 	case EACCESS_DIALERS:
143132Srobinson 		return (eaccess(Dialers[ndialers], R_OK));
1440Sstevel@tonic-gate 	}
145132Srobinson 	(void) sprintf(errformat, "bad access type %d", type);
146132Srobinson 	logent(errformat, "sysaccess");
147132Srobinson 	return (FAIL);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * read Sysfiles, set up lists of Systems/Devices/Dialers file names.
1530Sstevel@tonic-gate  * allow multiple entries for a given service, allow a service
1540Sstevel@tonic-gate  * type to describe resources more than once, e.g., systems=foo:baz systems=bar.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate static void
scansys(const char * service)157132Srobinson scansys(const char *service)
1580Sstevel@tonic-gate {	FILE *f;
1590Sstevel@tonic-gate 	char *tok, buf[BUFSIZ];
1609289SSreedhar.Chalamalasetti@Sun.COM 	char **tptr;
1610Sstevel@tonic-gate 
1629289SSreedhar.Chalamalasetti@Sun.COM 	/*
1639289SSreedhar.Chalamalasetti@Sun.COM 	 * Release and Initialize previously allocated memory
1649289SSreedhar.Chalamalasetti@Sun.COM 	 * for Systems, Devices and Dialers.
1659289SSreedhar.Chalamalasetti@Sun.COM 	 */
1669289SSreedhar.Chalamalasetti@Sun.COM 	nsystems = 0;
1679289SSreedhar.Chalamalasetti@Sun.COM 	tptr = Systems;
1689289SSreedhar.Chalamalasetti@Sun.COM 	while (*tptr) {
1699289SSreedhar.Chalamalasetti@Sun.COM 		free(*tptr);
1709289SSreedhar.Chalamalasetti@Sun.COM 		*tptr = NULL;
1719289SSreedhar.Chalamalasetti@Sun.COM 		tptr++;
1729289SSreedhar.Chalamalasetti@Sun.COM 	}
1739289SSreedhar.Chalamalasetti@Sun.COM 
1749289SSreedhar.Chalamalasetti@Sun.COM 	ndevices = 0;
1759289SSreedhar.Chalamalasetti@Sun.COM 	tptr = Devices;
1769289SSreedhar.Chalamalasetti@Sun.COM 	while (*tptr) {
1779289SSreedhar.Chalamalasetti@Sun.COM 		free(*tptr);
1789289SSreedhar.Chalamalasetti@Sun.COM 		*tptr = NULL;
1799289SSreedhar.Chalamalasetti@Sun.COM 		tptr++;
1809289SSreedhar.Chalamalasetti@Sun.COM 	}
1819289SSreedhar.Chalamalasetti@Sun.COM 
1829289SSreedhar.Chalamalasetti@Sun.COM 	ndialers = 0;
1839289SSreedhar.Chalamalasetti@Sun.COM 	tptr = Dialers;
1849289SSreedhar.Chalamalasetti@Sun.COM 	while (*tptr) {
1859289SSreedhar.Chalamalasetti@Sun.COM 		free(*tptr);
1869289SSreedhar.Chalamalasetti@Sun.COM 		*tptr = NULL;
1879289SSreedhar.Chalamalasetti@Sun.COM 		tptr++;
1889289SSreedhar.Chalamalasetti@Sun.COM 	}
1899289SSreedhar.Chalamalasetti@Sun.COM 
1901914Scasper 	if ((f = fopen(SYSFILES, "rF")) != 0) {
191*13093SRoger.Faulkner@Oracle.COM 		while (getaline(f, buf) > 0) {
1920Sstevel@tonic-gate 			/* got a (logical) line from Sysfiles */
1930Sstevel@tonic-gate 			/* strtok's of this buf continue in tokenize() */
1940Sstevel@tonic-gate 			tok = strtok(buf, " \t");
1950Sstevel@tonic-gate 			if (namematch("service=", tok, service)) {
1960Sstevel@tonic-gate 				tokenize();
1970Sstevel@tonic-gate 				nameparse();
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		(void) fclose(f);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* if didn't find entries in Sysfiles, use defaults */
2040Sstevel@tonic-gate 	if (Systems[0] == NULL) {
2050Sstevel@tonic-gate 		Systems[0] = strsave(SYSTEMS);
206132Srobinson 		ASSERT(Systems[0] != NULL, "Ct_ALLOCATE", "scansys: Systems",
207132Srobinson 									0);
2080Sstevel@tonic-gate 		Systems[1] = NULL;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	if (Devices[0] == NULL) {
2110Sstevel@tonic-gate 		Devices[0] = strsave(DEVICES);
212132Srobinson 		ASSERT(Devices[0] != NULL, "Ct_ALLOCATE", "scansys: Devices",
213132Srobinson 									0);
2140Sstevel@tonic-gate 		Devices[1] = NULL;
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	if (Dialers[0] == NULL) {
2170Sstevel@tonic-gate 		Dialers[0] = strsave(DIALERS);
218132Srobinson 		ASSERT(Dialers[0] != NULL, "Ct_ALLOCATE", "scansys: Dialers",
219132Srobinson 									0);
2200Sstevel@tonic-gate 		Dialers[1] = NULL;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * read Devconfig.  allow multiple entries for a given service, allow a service
2270Sstevel@tonic-gate  * type to describe resources more than once, e.g., push=foo:baz push=bar.
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate static void
scancfg(char * service,char * device)230132Srobinson scancfg(char *service, char *device)
2310Sstevel@tonic-gate {	FILE *f;
2320Sstevel@tonic-gate 	char *tok, buf[BUFSIZ];
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* (re)initialize device-specific information */
2350Sstevel@tonic-gate 	npops = npushes = 0;
2360Sstevel@tonic-gate 	Pops[0] = Pushes[0] = NULL;
2370Sstevel@tonic-gate 	connecttime = CONNECTTIME;
2380Sstevel@tonic-gate 	expecttime = EXPECTTIME;
2390Sstevel@tonic-gate 
2401914Scasper 	if ((f = fopen(DEVCONFIG, "rF")) != 0) {
241*13093SRoger.Faulkner@Oracle.COM 		while (getaline(f, buf) > 0) {
2420Sstevel@tonic-gate 			/* got a (logical) line from Devconfig */
2430Sstevel@tonic-gate 			/* strtok's of this buf continue in tokenize() */
2440Sstevel@tonic-gate 			tok = strtok(buf, " \t");
2450Sstevel@tonic-gate 			if (namematch("service=", tok, service)) {
2460Sstevel@tonic-gate 				tok = strtok((char *)0, " \t");
2470Sstevel@tonic-gate 				if (namematch("device=", tok, device)) {
2480Sstevel@tonic-gate 					tokenize();
2490Sstevel@tonic-gate 					nameparse();
2500Sstevel@tonic-gate 				}
2510Sstevel@tonic-gate 			}
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 		(void) fclose(f);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	return;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  *  given a file pointer and buffer, construct logical line in buffer
2610Sstevel@tonic-gate  *  (i.e., concatenate lines ending in '\').  return length of line
2620Sstevel@tonic-gate  *  ASSUMES that buffer is BUFSIZ long!
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static int
getaline(FILE * f,char * line)266*13093SRoger.Faulkner@Oracle.COM getaline(FILE *f, char *line)
2670Sstevel@tonic-gate {	char *lptr, *lend;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	lptr = line;
2700Sstevel@tonic-gate 	while (fgets(lptr, (line + BUFSIZ) - lptr, f) != NULL) {
2710Sstevel@tonic-gate 		lend = lptr + strlen(lptr);
272132Srobinson 		if (lend == lptr || lend[-1] != '\n')
2730Sstevel@tonic-gate 			/* empty buf or line too long! */
2740Sstevel@tonic-gate 			break;
2750Sstevel@tonic-gate 		*--lend = '\0'; /* lop off ending '\n' */
2760Sstevel@tonic-gate 		if (lend == line) /* empty line - ignore */
2770Sstevel@tonic-gate 			continue;
2780Sstevel@tonic-gate 		lptr = lend;
2790Sstevel@tonic-gate 		if (lend[-1] != '\\')
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 		/* continuation */
2820Sstevel@tonic-gate 		lend[-1] = ' ';
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	return (lptr - line);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * given a label (e.g., "service=", "device="), a name ("cu", "uucico"),
2890Sstevel@tonic-gate  *  and a line:  if line begins with the label and if the name appears
2900Sstevel@tonic-gate  * in a colon-separated list of names following the label, return true;
2910Sstevel@tonic-gate  * else return false
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate static int
namematch(const char * label,char * line,const char * name)294132Srobinson namematch(const char *label, char *line, const char *name)
295132Srobinson {
296132Srobinson 	char *lend;
2970Sstevel@tonic-gate 
298132Srobinson 	if (strncmp(label, line, strlen(label)) != SAME)
2990Sstevel@tonic-gate 		return (FALSE);	/* probably a comment line */
3000Sstevel@tonic-gate 	line += strlen(label);
301132Srobinson 	if (*line == '\0')
3020Sstevel@tonic-gate 		return (FALSE);
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * can't use strtok() in the following because scansys(),
3050Sstevel@tonic-gate 	 * scancfg() do an initializing call to strtok() before
3060Sstevel@tonic-gate 	 * coming here and then CONTINUE calling strtok() in tokenize(),
3070Sstevel@tonic-gate 	 * after returning from namematch().
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	while ((lend = strchr(line, ':')) != NULL) {
3100Sstevel@tonic-gate 		*lend = '\0';
311132Srobinson 		if (strcmp(line, name) == SAME)
3120Sstevel@tonic-gate 			return (TRUE);
3130Sstevel@tonic-gate 		line = lend+1;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 	return (strcmp(line, name) == SAME);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * tokenize() continues pulling tokens out of a buffer -- the
3200Sstevel@tonic-gate  * initializing call to strtok must have been made before calling
3210Sstevel@tonic-gate  * tokenize() -- and starts stuffing 'em into tokptr.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate static void
tokenize(void)324132Srobinson tokenize(void)
325132Srobinson {
326132Srobinson 	char *tok;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	tokptr = tokens;
329132Srobinson 	while ((tok = strtok(NULL, " \t")) != NULL) {
3300Sstevel@tonic-gate 		*tokptr++ = tok;
3310Sstevel@tonic-gate 		if (tokptr - tokens >= NTOKENS)
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 	*tokptr = NULL;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * look at top token in array: should be line of the form
3390Sstevel@tonic-gate  *	name=item1:item2:item3...
340132Srobinson  * if name is one we recognize, then call set[file|ioctl] to set up
3410Sstevel@tonic-gate  * corresponding list.  otherwise, log bad name.
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate static void
nameparse(void)344132Srobinson nameparse(void)
345132Srobinson {
346132Srobinson 	char **line, *equals;
3470Sstevel@tonic-gate 	int temp;
3480Sstevel@tonic-gate 
349132Srobinson #define	setuint(a, b, c) a = (((temp = atoi(b)) <= 0) ? (c) : temp)
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	for (line = tokens; (line - tokens) < NTOKENS && *line; line++) {
3520Sstevel@tonic-gate 		equals = strchr(*line, '=');
3530Sstevel@tonic-gate 		if (equals == NULL)
3540Sstevel@tonic-gate 			continue;	/* may be meaningful someday? */
3550Sstevel@tonic-gate 		*equals = '\0';
3560Sstevel@tonic-gate 		/* ignore entry with empty rhs */
3570Sstevel@tonic-gate 		if (*++equals == '\0')
3580Sstevel@tonic-gate 			continue;
3590Sstevel@tonic-gate 		if (strcmp(*line, "systems") == SAME)
3600Sstevel@tonic-gate 			setfile(Systems, equals);
3610Sstevel@tonic-gate 		else if (strcmp(*line, "devices") == SAME)
3620Sstevel@tonic-gate 			setfile(Devices, equals);
3630Sstevel@tonic-gate 		else if (strcmp(*line, "dialers") == SAME)
3640Sstevel@tonic-gate 			setfile(Dialers, equals);
3650Sstevel@tonic-gate 		else if (strcmp(*line, "pop") == SAME)
3660Sstevel@tonic-gate 			setioctl(Pops, equals);
3670Sstevel@tonic-gate 		else if (strcmp(*line, "push") == SAME)
3680Sstevel@tonic-gate 			setioctl(Pushes, equals);
3690Sstevel@tonic-gate 		else if (strcmp(*line, "connecttime") == SAME)
3700Sstevel@tonic-gate 			setuint(connecttime, equals, CONNECTTIME);
3710Sstevel@tonic-gate 		else if (strcmp(*line, "expecttime") == SAME)
3720Sstevel@tonic-gate 			setuint(expecttime, equals, EXPECTTIME);
3730Sstevel@tonic-gate 		else if (strcmp(*line, "msgtime") == SAME)
374132Srobinson 			continue;
3750Sstevel@tonic-gate 		else {
3760Sstevel@tonic-gate 			char errformat[BUFSIZ];
3770Sstevel@tonic-gate 
378132Srobinson 			(void) snprintf(errformat, sizeof (errformat),
3799289SSreedhar.Chalamalasetti@Sun.COM 			    "unrecognized label %s", *line);
3800Sstevel@tonic-gate 			logent(errformat, "Sysfiles|Devconfig");
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  * given the list for a particular type (systems, devices,...)
3870Sstevel@tonic-gate  * and a line of colon-separated files, add 'em to list
3880Sstevel@tonic-gate  */
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate static void
setfile(char ** type,char * line)391132Srobinson setfile(char **type, char *line)
392132Srobinson {
393132Srobinson 	char **tptr, *tok;
3940Sstevel@tonic-gate 	char expandpath[BUFSIZ];
3950Sstevel@tonic-gate 
396132Srobinson 	if (*line == 0)
3970Sstevel@tonic-gate 		return;
3980Sstevel@tonic-gate 	tptr = type;
399132Srobinson 	while (*tptr)		/* skip over existing entries to */
4000Sstevel@tonic-gate 		tptr++;		/* concatenate multiple entries */
4010Sstevel@tonic-gate 
402132Srobinson 	for (tok = strtok(line, ":"); tok != NULL; tok = strtok(NULL, ":")) {
4030Sstevel@tonic-gate 		expandpath[0] = '\0';
4040Sstevel@tonic-gate 		if (*tok != '/')
4050Sstevel@tonic-gate 			/* by default, file names are relative to SYSDIR */
406132Srobinson 			(void) snprintf(expandpath, sizeof (expandpath),
4079289SSreedhar.Chalamalasetti@Sun.COM 			    "%s/", SYSDIR);
408132Srobinson 		(void) strcat(expandpath, tok);
4090Sstevel@tonic-gate 		if (eaccess(expandpath, R_OK) != 0)
4100Sstevel@tonic-gate 			/* if we can't read it, no point in adding to list */
4110Sstevel@tonic-gate 			continue;
4120Sstevel@tonic-gate 		*tptr = strsave(expandpath);
4130Sstevel@tonic-gate 		ASSERT(*tptr != NULL, "Ct_ALLOCATE", "setfile: tptr", 0);
4140Sstevel@tonic-gate 		tptr++;
4150Sstevel@tonic-gate 	}
4169289SSreedhar.Chalamalasetti@Sun.COM 	*tptr = NULL;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate  * given the list for a particular ioctl (push, pop)
4210Sstevel@tonic-gate  * and a line of colon-separated modules, add 'em to list
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate static void
setioctl(char ** type,char * line)425132Srobinson setioctl(char **type, char *line)
426132Srobinson {
427132Srobinson 	char **tptr, *tok;
4280Sstevel@tonic-gate 
429132Srobinson 	if (*line == 0)
4300Sstevel@tonic-gate 		return;
4310Sstevel@tonic-gate 	tptr = type;
432132Srobinson 	while (*tptr)		/* skip over existing entries to */
4330Sstevel@tonic-gate 		tptr++;		/* concatenate multiple entries */
434132Srobinson 	for (tok = strtok(line, ":"); tok != NULL; tok = strtok(NULL, ":")) {
4350Sstevel@tonic-gate 		*tptr = strsave(tok);
4360Sstevel@tonic-gate 		ASSERT(*tptr != NULL, "Ct_ALLOCATE", "setioctl: tptr", 0);
4370Sstevel@tonic-gate 		tptr++;
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * reset Systems files
4430Sstevel@tonic-gate  */
444132Srobinson static void
sysreset(void)445132Srobinson sysreset(void)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	if (fsystems)
448132Srobinson 		(void) fclose(fsystems);
4490Sstevel@tonic-gate 	fsystems = NULL;
4500Sstevel@tonic-gate 	nsystems = 0;
4510Sstevel@tonic-gate 	devreset();
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * reset Devices files
4560Sstevel@tonic-gate  */
457132Srobinson static void
devreset(void)458132Srobinson devreset(void)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	if (fdevices)
461132Srobinson 		(void) fclose(fdevices);
4620Sstevel@tonic-gate 	fdevices = NULL;
4630Sstevel@tonic-gate 	ndevices = 0;
4640Sstevel@tonic-gate 	dialreset();
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * reset Dialers files
4690Sstevel@tonic-gate  */
470132Srobinson static void
dialreset(void)471132Srobinson dialreset(void)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	if (fdialers)
474132Srobinson 		(void) fclose(fdialers);
4750Sstevel@tonic-gate 	fdialers = NULL;
4760Sstevel@tonic-gate 	ndialers = 0;
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * get next line from Systems file
4810Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
4820Sstevel@tonic-gate  */
483132Srobinson static int
getsysline(char * buf,int len)4840Sstevel@tonic-gate getsysline(char *buf, int len)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	if (Systems[0] == NULL)
4870Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
4880Sstevel@tonic-gate 		setservice("uucico");
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/* initialize devices and dialers whenever a new line is read */
4910Sstevel@tonic-gate 	/* from systems */
4920Sstevel@tonic-gate 	devreset();
4930Sstevel@tonic-gate 	if (fsystems == NULL)
494132Srobinson 		if (nextsystems() == FALSE)
4950Sstevel@tonic-gate 			return (FALSE);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	for (;;) {
4980Sstevel@tonic-gate 		while (fgets(buf, len, fsystems) != NULL)
4990Sstevel@tonic-gate 			if ((*buf != '#') && (*buf != ' ') &&
5009289SSreedhar.Chalamalasetti@Sun.COM 			    (*buf != '\t') && (*buf != '\n'))
5010Sstevel@tonic-gate 			return (TRUE);
502132Srobinson 		if (nextsystems() == FALSE)
5030Sstevel@tonic-gate 			return (FALSE);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * move to next systems file.  return TRUE if successful, FALSE if not
5090Sstevel@tonic-gate  */
5100Sstevel@tonic-gate static int
nextsystems(void)511132Srobinson nextsystems(void)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	devreset();
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (fsystems != NULL) {
5160Sstevel@tonic-gate 		(void) fclose(fsystems);
5170Sstevel@tonic-gate 		nsystems++;
5180Sstevel@tonic-gate 	} else {
5190Sstevel@tonic-gate 		nsystems = 0;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	for (; Systems[nsystems] != NULL; nsystems++)
5221914Scasper 		if ((fsystems = fopen(Systems[nsystems], "rF")) != NULL)
5230Sstevel@tonic-gate 			return (TRUE);
5240Sstevel@tonic-gate 	return (FALSE);
5250Sstevel@tonic-gate }
526132Srobinson 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * get next line from Devices file
5290Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
5300Sstevel@tonic-gate  */
531132Srobinson static int
getdevline(char * buf,int len)5320Sstevel@tonic-gate getdevline(char *buf, int len)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	if (Devices[0] == NULL)
5350Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
5360Sstevel@tonic-gate 		setservice("uucico");
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (fdevices == NULL)
539132Srobinson 		if (nextdevices() == FALSE)
5400Sstevel@tonic-gate 			return (FALSE);
5410Sstevel@tonic-gate 	for (;;) {
542132Srobinson 		if (fgets(buf, len, fdevices) != NULL)
5430Sstevel@tonic-gate 			return (TRUE);
544132Srobinson 		if (nextdevices() == FALSE)
5450Sstevel@tonic-gate 			return (FALSE);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * move to next devices file.  return TRUE if successful, FALSE if not
5510Sstevel@tonic-gate  */
5520Sstevel@tonic-gate static int
nextdevices(void)553132Srobinson nextdevices(void)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	if (fdevices != NULL) {
5560Sstevel@tonic-gate 		(void) fclose(fdevices);
5570Sstevel@tonic-gate 		ndevices++;
5580Sstevel@tonic-gate 	} else {
5590Sstevel@tonic-gate 		ndevices = 0;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	for (; Devices[ndevices] != NULL; ndevices++)
5621914Scasper 		if ((fdevices = fopen(Devices[ndevices], "rF")) != NULL)
5630Sstevel@tonic-gate 			return (TRUE);
5640Sstevel@tonic-gate 	return (FALSE);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
567132Srobinson 
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate  * get next line from Dialers file
5700Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
5710Sstevel@tonic-gate  */
5720Sstevel@tonic-gate 
573132Srobinson static int
getdialline(char * buf,int len)5740Sstevel@tonic-gate getdialline(char *buf, int len)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	if (Dialers[0] == NULL)
5770Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
5780Sstevel@tonic-gate 		setservice("uucico");
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (fdialers == NULL)
581132Srobinson 		if (nextdialers() == FALSE)
5820Sstevel@tonic-gate 			return (FALSE);
5830Sstevel@tonic-gate 	for (;;) {
584132Srobinson 		if (fgets(buf, len, fdialers) != NULL)
5850Sstevel@tonic-gate 			return (TRUE);
586132Srobinson 		if (nextdialers() == FALSE)
5870Sstevel@tonic-gate 			return (FALSE);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate  * move to next dialers file.  return TRUE if successful, FALSE if not
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate static int
nextdialers(void)595132Srobinson nextdialers(void)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	if (fdialers) {
5980Sstevel@tonic-gate 		(void) fclose(fdialers);
5990Sstevel@tonic-gate 		ndialers++;
6000Sstevel@tonic-gate 	} else {
6010Sstevel@tonic-gate 		ndialers = 0;
6020Sstevel@tonic-gate 	}
603132Srobinson 
6040Sstevel@tonic-gate 	for (; Dialers[ndialers] != NULL; ndialers++)
6051914Scasper 		if ((fdialers = fopen(Dialers[ndialers], "rF")) != NULL)
6060Sstevel@tonic-gate 			return (TRUE);
6070Sstevel@tonic-gate 	return (FALSE);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6110Sstevel@tonic-gate  * get next module to be popped
6120Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
6130Sstevel@tonic-gate  */
6140Sstevel@tonic-gate static int
getpop(char * buf,size_t len,int * optional)615132Srobinson getpop(char *buf, size_t len, int *optional)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	int slen;
6180Sstevel@tonic-gate 
619132Srobinson 	if (Pops[0] == NULL || Pops[npops] == NULL)
6200Sstevel@tonic-gate 		return (FALSE);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*	if the module name is enclosed in parentheses,	*/
6230Sstevel@tonic-gate 	/*	is optional. set flag & strip parens		*/
6240Sstevel@tonic-gate 	slen = strlen(Pops[npops]) - 1;
625132Srobinson 	if (Pops[npops][0] == '(' && Pops[npops][slen] == ')') {
6260Sstevel@tonic-gate 		*optional = 1;
6270Sstevel@tonic-gate 		len = (slen < len ? slen : len);
628132Srobinson 		(void) strncpy(buf, &(Pops[npops++][1]), len);
6290Sstevel@tonic-gate 	} else {
6300Sstevel@tonic-gate 		*optional = 0;
631132Srobinson 		(void) strncpy(buf, Pops[npops++], len);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	buf[len-1] = '\0';
6340Sstevel@tonic-gate 	return (TRUE);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * get next module to be pushed
6390Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
6400Sstevel@tonic-gate  */
6410Sstevel@tonic-gate static int
getpush(char * buf,size_t len)642132Srobinson getpush(char *buf, size_t len)
6430Sstevel@tonic-gate {
644132Srobinson 	if (Pushes[0] == NULL || Pushes[npushes] == NULL)
6450Sstevel@tonic-gate 		return (FALSE);
646132Srobinson 	(void) strncpy(buf, Pushes[npushes++], len);
6470Sstevel@tonic-gate 	return (TRUE);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * pop/push requested modules
6520Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
6530Sstevel@tonic-gate  */
654132Srobinson static int
pop_push(int fd)655132Srobinson pop_push(int fd)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	char	strmod[FMNAMESZ], onstream[FMNAMESZ];
6580Sstevel@tonic-gate 	int		optional;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/*	check for streams modules to pop	*/
661132Srobinson 	while (getpop(strmod, sizeof (strmod), &optional)) {
6620Sstevel@tonic-gate 		DEBUG(5, (optional ?
6639289SSreedhar.Chalamalasetti@Sun.COM 		    (const char *)"pop_push: optionally POPing %s\n" :
6649289SSreedhar.Chalamalasetti@Sun.COM 		    (const char *)"pop_push: POPing %s\n"), strmod);
6650Sstevel@tonic-gate 		if (ioctl(fd, I_LOOK, onstream) == -1) {
6660Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_LOOK on fd %d failed ", fd);
6670Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6680Sstevel@tonic-gate 			return (FALSE);
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 		if (strcmp(strmod, onstream) != SAME) {
6710Sstevel@tonic-gate 			if (optional)
6720Sstevel@tonic-gate 				continue;
6730Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_POP: %s not there\n", strmod);
6740Sstevel@tonic-gate 			return (FALSE);
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 		if (ioctl(fd, I_POP, 0) == -1) {
6770Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_POP on fd %d failed ", fd);
6780Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6790Sstevel@tonic-gate 			return (FALSE);
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*	check for streams modules to push	*/
684132Srobinson 	while (getpush(strmod, sizeof (strmod))) {
6850Sstevel@tonic-gate 		DEBUG(5, "pop_push: PUSHing %s\n", strmod);
6860Sstevel@tonic-gate 		if (ioctl(fd, I_PUSH, strmod) == -1) {
6870Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_PUSH on fd %d failed ", fd);
6880Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6890Sstevel@tonic-gate 			return (FALSE);
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	return (TRUE);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
695132Srobinson #ifndef SMALL
6960Sstevel@tonic-gate /*
697132Srobinson  *	return name of currently open Systems file
6980Sstevel@tonic-gate  */
699132Srobinson static char *
currsys(void)700132Srobinson currsys(void)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	return (Systems[nsystems]);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
706132Srobinson  *	return name of currently open Devices file
7070Sstevel@tonic-gate  */
708132Srobinson static char *
currdev(void)709132Srobinson currdev(void)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	return (Devices[ndevices]);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate /*
715132Srobinson  *	return name of currently open Dialers file
7160Sstevel@tonic-gate  */
717132Srobinson static char *
currdial(void)718132Srobinson currdial(void)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	return (Dialers[ndialers]);
7210Sstevel@tonic-gate }
722132Srobinson #endif
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * set configuration parameters provided in Config file
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate static void
setconfig(void)728132Srobinson setconfig(void)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	FILE *f;
7310Sstevel@tonic-gate 	char buf[BUFSIZ];
7320Sstevel@tonic-gate 	char *tok;
7330Sstevel@tonic-gate 	extern char _ProtoCfg[];
7340Sstevel@tonic-gate 
7351914Scasper 	if ((f = fopen(CONFIG, "rF")) != 0) {
736*13093SRoger.Faulkner@Oracle.COM 	while (getaline(f, buf) > 0) {
7370Sstevel@tonic-gate 		/* got a (logical) line from Config file */
7380Sstevel@tonic-gate 		tok = strtok(buf, " \t");
7390Sstevel@tonic-gate 		if ((tok != NULL) && (*tok != '#')) {
7400Sstevel@tonic-gate 			/* got a token */
741132Srobinson 			/*
742132Srobinson 			 * this probably should be table driven when
743132Srobinson 			 * the list of configurable parameters grows.
744132Srobinson 			 */
745132Srobinson 			if (strncmp("Protocol=", tok, strlen("Protocol=")) ==
7469289SSreedhar.Chalamalasetti@Sun.COM 			    SAME) {
7470Sstevel@tonic-gate 				tok += strlen("Protocol=");
7480Sstevel@tonic-gate 				if (*tok != '\0') {
7490Sstevel@tonic-gate 					if (_ProtoCfg[0] != '\0') {
7500Sstevel@tonic-gate 						/*EMPTY*/
751132Srobinson 						DEBUG(7, "Protocol string %s ",
7529289SSreedhar.Chalamalasetti@Sun.COM 						    tok);
753132Srobinson 						DEBUG(7, "overrides %s\n",
7549289SSreedhar.Chalamalasetti@Sun.COM 						    _ProtoCfg);
7550Sstevel@tonic-gate 					}
756132Srobinson 					(void) strcpy(_ProtoCfg, tok);
7570Sstevel@tonic-gate 				}
7580Sstevel@tonic-gate 			} else {
7590Sstevel@tonic-gate 				/*EMPTY*/
760132Srobinson 				DEBUG(7, "Unknown configuration parameter %s\n",
7619289SSreedhar.Chalamalasetti@Sun.COM 				    tok);
7620Sstevel@tonic-gate 			}
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	(void) fclose(f);
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate }
768