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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24132Srobinson /*	  All Rights Reserved	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33*1219Sraf #include "mt.h"
340Sstevel@tonic-gate #include "uucp.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <unistd.h>
37132Srobinson #include <string.h>
380Sstevel@tonic-gate #include "sysfiles.h"
390Sstevel@tonic-gate #include <sys/stropts.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * manage systems files (Systems, Devices, and Dialcodes families).
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * also manage new file Devconfig, allows per-device setup.
450Sstevel@tonic-gate  * present use is to specify what streams modules to push/pop for
460Sstevel@tonic-gate  * AT&T TLI/streams network.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * TODO:
490Sstevel@tonic-gate  *	call bsfix()?
500Sstevel@tonic-gate  *	combine the 3 versions of everything (sys, dev, and dial) into one.
510Sstevel@tonic-gate  *	allow arbitrary classes of service.
520Sstevel@tonic-gate  *	need verifysys() for uucheck.
530Sstevel@tonic-gate  *	nameserver interface?
54132Srobinson  *	pass sysname (or 0) to getsysline().  (might want reg. exp. or
55132Srobinson  *		NS processing)
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* private variables */
59132Srobinson static void tokenize(void);
60132Srobinson static void nameparse(void);
61132Srobinson static void setfile(char **, char *);
62132Srobinson static void setioctl(char **, char *);
63132Srobinson static void scansys(const char *);
64132Srobinson static void scancfg(char *, char *);
65132Srobinson static void setconfig(void);
66132Srobinson static int namematch(const char *label, char *line, const char *name);
67132Srobinson static int nextdialers(void);
68132Srobinson static int nextdevices(void);
69132Srobinson static int nextsystems(void);
70132Srobinson static int getline(FILE *, char *);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /* pointer arrays might be dynamically allocated */
730Sstevel@tonic-gate static char *Systems[64];	/* list of Systems files */
740Sstevel@tonic-gate static char *Devices[64];	/* list of Devices files */
750Sstevel@tonic-gate static char *Dialers[64];	/* list of Dialers files */
760Sstevel@tonic-gate static char *Pops[64];		/* list of STREAMS modules to be popped */
770Sstevel@tonic-gate static char *Pushes[64];	/* list of STREAMS modules to be pushed */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static int nsystems;		/* index into list of Systems files */
800Sstevel@tonic-gate static int ndevices;		/* index into list of Devices files */
810Sstevel@tonic-gate static int ndialers;		/* index into list of Dialers files */
820Sstevel@tonic-gate static int npops;		/* index into list of STREAMS modules */
83132Srobinson 							/* to be popped */
840Sstevel@tonic-gate static int npushes;		/* index into list of STREAMS modules */
85132Srobinson 							/* to be pushed */
860Sstevel@tonic-gate 
87132Srobinson static unsigned connecttime, expecttime;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static FILE *fsystems;
900Sstevel@tonic-gate static FILE *fdevices;
910Sstevel@tonic-gate static FILE *fdialers;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* this might be dynamically allocated */
94132Srobinson #define	NTOKENS 16
950Sstevel@tonic-gate static char *tokens[NTOKENS], **tokptr;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /* export these */
98132Srobinson static void setservice(const char *service);
99132Srobinson static void sysreset(void);
100132Srobinson static void devreset(void);
101132Srobinson static void dialreset(void);
102132Srobinson static void setdevcfg(char *, char *);
103132Srobinson static void setservice(const char *);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* import these */
106132Srobinson extern char *strsave(const char *);
107132Srobinson static int eaccess(char *, mode_t);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * setservice init's Systems, Devices, Dialers lists from Sysfiles
1110Sstevel@tonic-gate  */
112132Srobinson static void
113132Srobinson setservice(const char *service)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	setconfig();
1160Sstevel@tonic-gate 	scansys(service);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * setdevcfg init's Pops, Pushes lists from Devconfig
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate 
123132Srobinson static void
124132Srobinson setdevcfg(char *service, char *device)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	scancfg(service, device);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*	administrative files access */
130132Srobinson static int
131132Srobinson sysaccess(int type)
1320Sstevel@tonic-gate {
133132Srobinson 	char errformat[BUFSIZ];
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	switch (type) {
1360Sstevel@tonic-gate 	case ACCESS_SYSTEMS:
1370Sstevel@tonic-gate 		return (access(Systems[nsystems], R_OK));
1380Sstevel@tonic-gate 	case ACCESS_DEVICES:
1390Sstevel@tonic-gate 		return (access(Devices[ndevices], R_OK));
1400Sstevel@tonic-gate 	case ACCESS_DIALERS:
1410Sstevel@tonic-gate 		return (access(Dialers[ndialers], R_OK));
1420Sstevel@tonic-gate 	case EACCESS_SYSTEMS:
143132Srobinson 		return (eaccess(Systems[nsystems], R_OK));
1440Sstevel@tonic-gate 	case EACCESS_DEVICES:
145132Srobinson 		return (eaccess(Devices[ndevices], R_OK));
1460Sstevel@tonic-gate 	case EACCESS_DIALERS:
147132Srobinson 		return (eaccess(Dialers[ndialers], R_OK));
1480Sstevel@tonic-gate 	}
149132Srobinson 	(void) sprintf(errformat, "bad access type %d", type);
150132Srobinson 	logent(errformat, "sysaccess");
151132Srobinson 	return (FAIL);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * read Sysfiles, set up lists of Systems/Devices/Dialers file names.
1570Sstevel@tonic-gate  * allow multiple entries for a given service, allow a service
1580Sstevel@tonic-gate  * type to describe resources more than once, e.g., systems=foo:baz systems=bar.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate static void
161132Srobinson scansys(const char *service)
1620Sstevel@tonic-gate {	FILE *f;
1630Sstevel@tonic-gate 	char *tok, buf[BUFSIZ];
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	Systems[0] = Devices[0] = Dialers[0] = NULL;
1660Sstevel@tonic-gate 	if ((f = fopen(SYSFILES, "r")) != 0) {
167132Srobinson 		while (getline(f, buf) > 0) {
1680Sstevel@tonic-gate 			/* got a (logical) line from Sysfiles */
1690Sstevel@tonic-gate 			/* strtok's of this buf continue in tokenize() */
1700Sstevel@tonic-gate 			tok = strtok(buf, " \t");
1710Sstevel@tonic-gate 			if (namematch("service=", tok, service)) {
1720Sstevel@tonic-gate 				tokenize();
1730Sstevel@tonic-gate 				nameparse();
1740Sstevel@tonic-gate 			}
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 		(void) fclose(f);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* if didn't find entries in Sysfiles, use defaults */
1800Sstevel@tonic-gate 	if (Systems[0] == NULL) {
1810Sstevel@tonic-gate 		Systems[0] = strsave(SYSTEMS);
182132Srobinson 		ASSERT(Systems[0] != NULL, "Ct_ALLOCATE", "scansys: Systems",
183132Srobinson 									0);
1840Sstevel@tonic-gate 		Systems[1] = NULL;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	if (Devices[0] == NULL) {
1870Sstevel@tonic-gate 		Devices[0] = strsave(DEVICES);
188132Srobinson 		ASSERT(Devices[0] != NULL, "Ct_ALLOCATE", "scansys: Devices",
189132Srobinson 									0);
1900Sstevel@tonic-gate 		Devices[1] = NULL;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	if (Dialers[0] == NULL) {
1930Sstevel@tonic-gate 		Dialers[0] = strsave(DIALERS);
194132Srobinson 		ASSERT(Dialers[0] != NULL, "Ct_ALLOCATE", "scansys: Dialers",
195132Srobinson 									0);
1960Sstevel@tonic-gate 		Dialers[1] = NULL;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * read Devconfig.  allow multiple entries for a given service, allow a service
2030Sstevel@tonic-gate  * type to describe resources more than once, e.g., push=foo:baz push=bar.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static void
206132Srobinson scancfg(char *service, char *device)
2070Sstevel@tonic-gate {	FILE *f;
2080Sstevel@tonic-gate 	char *tok, buf[BUFSIZ];
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* (re)initialize device-specific information */
2110Sstevel@tonic-gate 	npops = npushes = 0;
2120Sstevel@tonic-gate 	Pops[0] = Pushes[0] = NULL;
2130Sstevel@tonic-gate 	connecttime = CONNECTTIME;
2140Sstevel@tonic-gate 	expecttime = EXPECTTIME;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if ((f = fopen(DEVCONFIG, "r")) != 0) {
2170Sstevel@tonic-gate 		while (getline(f, buf) > 0) {
2180Sstevel@tonic-gate 			/* got a (logical) line from Devconfig */
2190Sstevel@tonic-gate 			/* strtok's of this buf continue in tokenize() */
2200Sstevel@tonic-gate 			tok = strtok(buf, " \t");
2210Sstevel@tonic-gate 			if (namematch("service=", tok, service)) {
2220Sstevel@tonic-gate 				tok = strtok((char *)0, " \t");
2230Sstevel@tonic-gate 				if (namematch("device=", tok, device)) {
2240Sstevel@tonic-gate 					tokenize();
2250Sstevel@tonic-gate 					nameparse();
2260Sstevel@tonic-gate 				}
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 		(void) fclose(f);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	return;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  *  given a file pointer and buffer, construct logical line in buffer
2370Sstevel@tonic-gate  *  (i.e., concatenate lines ending in '\').  return length of line
2380Sstevel@tonic-gate  *  ASSUMES that buffer is BUFSIZ long!
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static int
242132Srobinson getline(FILE *f, char *line)
2430Sstevel@tonic-gate {	char *lptr, *lend;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	lptr = line;
2460Sstevel@tonic-gate 	while (fgets(lptr, (line + BUFSIZ) - lptr, f) != NULL) {
2470Sstevel@tonic-gate 		lend = lptr + strlen(lptr);
248132Srobinson 		if (lend == lptr || lend[-1] != '\n')
2490Sstevel@tonic-gate 			/* empty buf or line too long! */
2500Sstevel@tonic-gate 			break;
2510Sstevel@tonic-gate 		*--lend = '\0'; /* lop off ending '\n' */
2520Sstevel@tonic-gate 		if (lend == line) /* empty line - ignore */
2530Sstevel@tonic-gate 			continue;
2540Sstevel@tonic-gate 		lptr = lend;
2550Sstevel@tonic-gate 		if (lend[-1] != '\\')
2560Sstevel@tonic-gate 			break;
2570Sstevel@tonic-gate 		/* continuation */
2580Sstevel@tonic-gate 		lend[-1] = ' ';
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	return (lptr - line);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * given a label (e.g., "service=", "device="), a name ("cu", "uucico"),
2650Sstevel@tonic-gate  *  and a line:  if line begins with the label and if the name appears
2660Sstevel@tonic-gate  * in a colon-separated list of names following the label, return true;
2670Sstevel@tonic-gate  * else return false
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate static int
270132Srobinson namematch(const char *label, char *line, const char *name)
271132Srobinson {
272132Srobinson 	char *lend;
2730Sstevel@tonic-gate 
274132Srobinson 	if (strncmp(label, line, strlen(label)) != SAME)
2750Sstevel@tonic-gate 		return (FALSE);	/* probably a comment line */
2760Sstevel@tonic-gate 	line += strlen(label);
277132Srobinson 	if (*line == '\0')
2780Sstevel@tonic-gate 		return (FALSE);
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * can't use strtok() in the following because scansys(),
2810Sstevel@tonic-gate 	 * scancfg() do an initializing call to strtok() before
2820Sstevel@tonic-gate 	 * coming here and then CONTINUE calling strtok() in tokenize(),
2830Sstevel@tonic-gate 	 * after returning from namematch().
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	while ((lend = strchr(line, ':')) != NULL) {
2860Sstevel@tonic-gate 		*lend = '\0';
287132Srobinson 		if (strcmp(line, name) == SAME)
2880Sstevel@tonic-gate 			return (TRUE);
2890Sstevel@tonic-gate 		line = lend+1;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	return (strcmp(line, name) == SAME);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * tokenize() continues pulling tokens out of a buffer -- the
2960Sstevel@tonic-gate  * initializing call to strtok must have been made before calling
2970Sstevel@tonic-gate  * tokenize() -- and starts stuffing 'em into tokptr.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate static void
300132Srobinson tokenize(void)
301132Srobinson {
302132Srobinson 	char *tok;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	tokptr = tokens;
305132Srobinson 	while ((tok = strtok(NULL, " \t")) != NULL) {
3060Sstevel@tonic-gate 		*tokptr++ = tok;
3070Sstevel@tonic-gate 		if (tokptr - tokens >= NTOKENS)
3080Sstevel@tonic-gate 			break;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	*tokptr = NULL;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * look at top token in array: should be line of the form
3150Sstevel@tonic-gate  *	name=item1:item2:item3...
316132Srobinson  * if name is one we recognize, then call set[file|ioctl] to set up
3170Sstevel@tonic-gate  * corresponding list.  otherwise, log bad name.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static void
320132Srobinson nameparse(void)
321132Srobinson {
322132Srobinson 	char **line, *equals;
3230Sstevel@tonic-gate 	int temp;
3240Sstevel@tonic-gate 
325132Srobinson #define	setuint(a, b, c) a = (((temp = atoi(b)) <= 0) ? (c) : temp)
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	for (line = tokens; (line - tokens) < NTOKENS && *line; line++) {
3280Sstevel@tonic-gate 		equals = strchr(*line, '=');
3290Sstevel@tonic-gate 		if (equals == NULL)
3300Sstevel@tonic-gate 			continue;	/* may be meaningful someday? */
3310Sstevel@tonic-gate 		*equals = '\0';
3320Sstevel@tonic-gate 		/* ignore entry with empty rhs */
3330Sstevel@tonic-gate 		if (*++equals == '\0')
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 		if (strcmp(*line, "systems") == SAME)
3360Sstevel@tonic-gate 			setfile(Systems, equals);
3370Sstevel@tonic-gate 		else if (strcmp(*line, "devices") == SAME)
3380Sstevel@tonic-gate 			setfile(Devices, equals);
3390Sstevel@tonic-gate 		else if (strcmp(*line, "dialers") == SAME)
3400Sstevel@tonic-gate 			setfile(Dialers, equals);
3410Sstevel@tonic-gate 		else if (strcmp(*line, "pop") == SAME)
3420Sstevel@tonic-gate 			setioctl(Pops, equals);
3430Sstevel@tonic-gate 		else if (strcmp(*line, "push") == SAME)
3440Sstevel@tonic-gate 			setioctl(Pushes, equals);
3450Sstevel@tonic-gate 		else if (strcmp(*line, "connecttime") == SAME)
3460Sstevel@tonic-gate 			setuint(connecttime, equals, CONNECTTIME);
3470Sstevel@tonic-gate 		else if (strcmp(*line, "expecttime") == SAME)
3480Sstevel@tonic-gate 			setuint(expecttime, equals, EXPECTTIME);
3490Sstevel@tonic-gate 		else if (strcmp(*line, "msgtime") == SAME)
350132Srobinson 			continue;
3510Sstevel@tonic-gate 		else {
3520Sstevel@tonic-gate 			char errformat[BUFSIZ];
3530Sstevel@tonic-gate 
354132Srobinson 			(void) snprintf(errformat, sizeof (errformat),
355132Srobinson 						"unrecognized label %s", *line);
3560Sstevel@tonic-gate 			logent(errformat, "Sysfiles|Devconfig");
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * given the list for a particular type (systems, devices,...)
3630Sstevel@tonic-gate  * and a line of colon-separated files, add 'em to list
3640Sstevel@tonic-gate  */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate static void
367132Srobinson setfile(char **type, char *line)
368132Srobinson {
369132Srobinson 	char **tptr, *tok;
3700Sstevel@tonic-gate 	char expandpath[BUFSIZ];
3710Sstevel@tonic-gate 
372132Srobinson 	if (*line == 0)
3730Sstevel@tonic-gate 		return;
3740Sstevel@tonic-gate 	tptr = type;
375132Srobinson 	while (*tptr)		/* skip over existing entries to */
3760Sstevel@tonic-gate 		tptr++;		/* concatenate multiple entries */
3770Sstevel@tonic-gate 
378132Srobinson 	for (tok = strtok(line, ":"); tok != NULL; tok = strtok(NULL, ":")) {
3790Sstevel@tonic-gate 		expandpath[0] = '\0';
3800Sstevel@tonic-gate 		if (*tok != '/')
3810Sstevel@tonic-gate 			/* by default, file names are relative to SYSDIR */
382132Srobinson 			(void) snprintf(expandpath, sizeof (expandpath),
383132Srobinson 								"%s/", SYSDIR);
384132Srobinson 		(void) strcat(expandpath, tok);
3850Sstevel@tonic-gate 		if (eaccess(expandpath, R_OK) != 0)
3860Sstevel@tonic-gate 			/* if we can't read it, no point in adding to list */
3870Sstevel@tonic-gate 			continue;
3880Sstevel@tonic-gate 		*tptr = strsave(expandpath);
3890Sstevel@tonic-gate 		ASSERT(*tptr != NULL, "Ct_ALLOCATE", "setfile: tptr", 0);
3900Sstevel@tonic-gate 		tptr++;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * given the list for a particular ioctl (push, pop)
3960Sstevel@tonic-gate  * and a line of colon-separated modules, add 'em to list
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate static void
400132Srobinson setioctl(char **type, char *line)
401132Srobinson {
402132Srobinson 	char **tptr, *tok;
4030Sstevel@tonic-gate 
404132Srobinson 	if (*line == 0)
4050Sstevel@tonic-gate 		return;
4060Sstevel@tonic-gate 	tptr = type;
407132Srobinson 	while (*tptr)		/* skip over existing entries to */
4080Sstevel@tonic-gate 		tptr++;		/* concatenate multiple entries */
409132Srobinson 	for (tok = strtok(line, ":"); tok != NULL; tok = strtok(NULL, ":")) {
4100Sstevel@tonic-gate 		*tptr = strsave(tok);
4110Sstevel@tonic-gate 		ASSERT(*tptr != NULL, "Ct_ALLOCATE", "setioctl: tptr", 0);
4120Sstevel@tonic-gate 		tptr++;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * reset Systems files
4180Sstevel@tonic-gate  */
419132Srobinson static void
420132Srobinson sysreset(void)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	if (fsystems)
423132Srobinson 		(void) fclose(fsystems);
4240Sstevel@tonic-gate 	fsystems = NULL;
4250Sstevel@tonic-gate 	nsystems = 0;
4260Sstevel@tonic-gate 	devreset();
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * reset Devices files
4310Sstevel@tonic-gate  */
432132Srobinson static void
433132Srobinson devreset(void)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	if (fdevices)
436132Srobinson 		(void) fclose(fdevices);
4370Sstevel@tonic-gate 	fdevices = NULL;
4380Sstevel@tonic-gate 	ndevices = 0;
4390Sstevel@tonic-gate 	dialreset();
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * reset Dialers files
4440Sstevel@tonic-gate  */
445132Srobinson static void
446132Srobinson dialreset(void)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	if (fdialers)
449132Srobinson 		(void) fclose(fdialers);
4500Sstevel@tonic-gate 	fdialers = NULL;
4510Sstevel@tonic-gate 	ndialers = 0;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * get next line from Systems file
4560Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
4570Sstevel@tonic-gate  */
458132Srobinson static int
4590Sstevel@tonic-gate getsysline(char *buf, int len)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	if (Systems[0] == NULL)
4620Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
4630Sstevel@tonic-gate 		setservice("uucico");
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/* initialize devices and dialers whenever a new line is read */
4660Sstevel@tonic-gate 	/* from systems */
4670Sstevel@tonic-gate 	devreset();
4680Sstevel@tonic-gate 	if (fsystems == NULL)
469132Srobinson 		if (nextsystems() == FALSE)
4700Sstevel@tonic-gate 			return (FALSE);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	for (;;) {
4730Sstevel@tonic-gate 		while (fgets(buf, len, fsystems) != NULL)
4740Sstevel@tonic-gate 			if ((*buf != '#') && (*buf != ' ') &&
475132Srobinson 				(*buf != '\t') && (*buf != '\n'))
4760Sstevel@tonic-gate 			return (TRUE);
477132Srobinson 		if (nextsystems() == FALSE)
4780Sstevel@tonic-gate 			return (FALSE);
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * move to next systems file.  return TRUE if successful, FALSE if not
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate static int
486132Srobinson nextsystems(void)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	devreset();
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (fsystems != NULL) {
4910Sstevel@tonic-gate 		(void) fclose(fsystems);
4920Sstevel@tonic-gate 		nsystems++;
4930Sstevel@tonic-gate 	} else {
4940Sstevel@tonic-gate 		nsystems = 0;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	for (; Systems[nsystems] != NULL; nsystems++)
497132Srobinson 		if ((fsystems = fopen(Systems[nsystems], "r")) != NULL)
4980Sstevel@tonic-gate 			return (TRUE);
4990Sstevel@tonic-gate 	return (FALSE);
5000Sstevel@tonic-gate }
501132Srobinson 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * get next line from Devices file
5040Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
5050Sstevel@tonic-gate  */
506132Srobinson static int
5070Sstevel@tonic-gate getdevline(char *buf, int len)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	if (Devices[0] == NULL)
5100Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
5110Sstevel@tonic-gate 		setservice("uucico");
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (fdevices == NULL)
514132Srobinson 		if (nextdevices() == FALSE)
5150Sstevel@tonic-gate 			return (FALSE);
5160Sstevel@tonic-gate 	for (;;) {
517132Srobinson 		if (fgets(buf, len, fdevices) != NULL)
5180Sstevel@tonic-gate 			return (TRUE);
519132Srobinson 		if (nextdevices() == FALSE)
5200Sstevel@tonic-gate 			return (FALSE);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * move to next devices file.  return TRUE if successful, FALSE if not
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate static int
528132Srobinson nextdevices(void)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	if (fdevices != NULL) {
5310Sstevel@tonic-gate 		(void) fclose(fdevices);
5320Sstevel@tonic-gate 		ndevices++;
5330Sstevel@tonic-gate 	} else {
5340Sstevel@tonic-gate 		ndevices = 0;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	for (; Devices[ndevices] != NULL; ndevices++)
537132Srobinson 		if ((fdevices = fopen(Devices[ndevices], "r")) != NULL)
5380Sstevel@tonic-gate 			return (TRUE);
5390Sstevel@tonic-gate 	return (FALSE);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
542132Srobinson 
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate  * get next line from Dialers file
5450Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate 
548132Srobinson static int
5490Sstevel@tonic-gate getdialline(char *buf, int len)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	if (Dialers[0] == NULL)
5520Sstevel@tonic-gate 		/* not initialized via setservice() - use default */
5530Sstevel@tonic-gate 		setservice("uucico");
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if (fdialers == NULL)
556132Srobinson 		if (nextdialers() == FALSE)
5570Sstevel@tonic-gate 			return (FALSE);
5580Sstevel@tonic-gate 	for (;;) {
559132Srobinson 		if (fgets(buf, len, fdialers) != NULL)
5600Sstevel@tonic-gate 			return (TRUE);
561132Srobinson 		if (nextdialers() == FALSE)
5620Sstevel@tonic-gate 			return (FALSE);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  * move to next dialers file.  return TRUE if successful, FALSE if not
5680Sstevel@tonic-gate  */
5690Sstevel@tonic-gate static int
570132Srobinson nextdialers(void)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	if (fdialers) {
5730Sstevel@tonic-gate 		(void) fclose(fdialers);
5740Sstevel@tonic-gate 		ndialers++;
5750Sstevel@tonic-gate 	} else {
5760Sstevel@tonic-gate 		ndialers = 0;
5770Sstevel@tonic-gate 	}
578132Srobinson 
5790Sstevel@tonic-gate 	for (; Dialers[ndialers] != NULL; ndialers++)
580132Srobinson 		if ((fdialers = fopen(Dialers[ndialers], "r")) != NULL)
5810Sstevel@tonic-gate 			return (TRUE);
5820Sstevel@tonic-gate 	return (FALSE);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate  * get next module to be popped
5870Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate static int
590132Srobinson getpop(char *buf, size_t len, int *optional)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	int slen;
5930Sstevel@tonic-gate 
594132Srobinson 	if (Pops[0] == NULL || Pops[npops] == NULL)
5950Sstevel@tonic-gate 		return (FALSE);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*	if the module name is enclosed in parentheses,	*/
5980Sstevel@tonic-gate 	/*	is optional. set flag & strip parens		*/
5990Sstevel@tonic-gate 	slen = strlen(Pops[npops]) - 1;
600132Srobinson 	if (Pops[npops][0] == '(' && Pops[npops][slen] == ')') {
6010Sstevel@tonic-gate 		*optional = 1;
6020Sstevel@tonic-gate 		len = (slen < len ? slen : len);
603132Srobinson 		(void) strncpy(buf, &(Pops[npops++][1]), len);
6040Sstevel@tonic-gate 	} else {
6050Sstevel@tonic-gate 		*optional = 0;
606132Srobinson 		(void) strncpy(buf, Pops[npops++], len);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	buf[len-1] = '\0';
6090Sstevel@tonic-gate 	return (TRUE);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate  * get next module to be pushed
6140Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate static int
617132Srobinson getpush(char *buf, size_t len)
6180Sstevel@tonic-gate {
619132Srobinson 	if (Pushes[0] == NULL || Pushes[npushes] == NULL)
6200Sstevel@tonic-gate 		return (FALSE);
621132Srobinson 	(void) strncpy(buf, Pushes[npushes++], len);
6220Sstevel@tonic-gate 	return (TRUE);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * pop/push requested modules
6270Sstevel@tonic-gate  * return TRUE if successful, FALSE if not
6280Sstevel@tonic-gate  */
629132Srobinson static int
630132Srobinson pop_push(int fd)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	char	strmod[FMNAMESZ], onstream[FMNAMESZ];
6330Sstevel@tonic-gate 	int		optional;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/*	check for streams modules to pop	*/
636132Srobinson 	while (getpop(strmod, sizeof (strmod), &optional)) {
6370Sstevel@tonic-gate 		DEBUG(5, (optional ?
638132Srobinson 			(const char *)"pop_push: optionally POPing %s\n" :
639132Srobinson 			(const char *)"pop_push: POPing %s\n"), strmod);
6400Sstevel@tonic-gate 		if (ioctl(fd, I_LOOK, onstream) == -1) {
6410Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_LOOK on fd %d failed ", fd);
6420Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6430Sstevel@tonic-gate 			return (FALSE);
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 		if (strcmp(strmod, onstream) != SAME) {
6460Sstevel@tonic-gate 			if (optional)
6470Sstevel@tonic-gate 				continue;
6480Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_POP: %s not there\n", strmod);
6490Sstevel@tonic-gate 			return (FALSE);
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		if (ioctl(fd, I_POP, 0) == -1) {
6520Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_POP on fd %d failed ", fd);
6530Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6540Sstevel@tonic-gate 			return (FALSE);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*	check for streams modules to push	*/
659132Srobinson 	while (getpush(strmod, sizeof (strmod))) {
6600Sstevel@tonic-gate 		DEBUG(5, "pop_push: PUSHing %s\n", strmod);
6610Sstevel@tonic-gate 		if (ioctl(fd, I_PUSH, strmod) == -1) {
6620Sstevel@tonic-gate 			DEBUG(5, "pop_push: I_PUSH on fd %d failed ", fd);
6630Sstevel@tonic-gate 			DEBUG(5, "errno %d\n", errno);
6640Sstevel@tonic-gate 			return (FALSE);
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	return (TRUE);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
670132Srobinson #ifndef SMALL
6710Sstevel@tonic-gate /*
672132Srobinson  *	return name of currently open Systems file
6730Sstevel@tonic-gate  */
674132Srobinson static char *
675132Srobinson currsys(void)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	return (Systems[nsystems]);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*
681132Srobinson  *	return name of currently open Devices file
6820Sstevel@tonic-gate  */
683132Srobinson static char *
684132Srobinson currdev(void)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	return (Devices[ndevices]);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
690132Srobinson  *	return name of currently open Dialers file
6910Sstevel@tonic-gate  */
692132Srobinson static char *
693132Srobinson currdial(void)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	return (Dialers[ndialers]);
6960Sstevel@tonic-gate }
697132Srobinson #endif
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * set configuration parameters provided in Config file
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate static void
703132Srobinson setconfig(void)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	FILE *f;
7060Sstevel@tonic-gate 	char buf[BUFSIZ];
7070Sstevel@tonic-gate 	char *tok;
7080Sstevel@tonic-gate 	extern char _ProtoCfg[];
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if ((f = fopen(CONFIG, "r")) != 0) {
711132Srobinson 	while (getline(f, buf) > 0) {
7120Sstevel@tonic-gate 		/* got a (logical) line from Config file */
7130Sstevel@tonic-gate 		tok = strtok(buf, " \t");
7140Sstevel@tonic-gate 		if ((tok != NULL) && (*tok != '#')) {
7150Sstevel@tonic-gate 			/* got a token */
716132Srobinson 			/*
717132Srobinson 			 * this probably should be table driven when
718132Srobinson 			 * the list of configurable parameters grows.
719132Srobinson 			 */
720132Srobinson 			if (strncmp("Protocol=", tok, strlen("Protocol=")) ==
721132Srobinson 								SAME) {
7220Sstevel@tonic-gate 				tok += strlen("Protocol=");
7230Sstevel@tonic-gate 				if (*tok != '\0') {
7240Sstevel@tonic-gate 					if (_ProtoCfg[0] != '\0') {
7250Sstevel@tonic-gate 						/*EMPTY*/
726132Srobinson 						DEBUG(7, "Protocol string %s ",
727132Srobinson 								tok);
728132Srobinson 						DEBUG(7, "overrides %s\n",
729132Srobinson 								_ProtoCfg);
7300Sstevel@tonic-gate 					}
731132Srobinson 					(void) strcpy(_ProtoCfg, tok);
7320Sstevel@tonic-gate 				}
7330Sstevel@tonic-gate 			} else {
7340Sstevel@tonic-gate 				/*EMPTY*/
735132Srobinson 				DEBUG(7, "Unknown configuration parameter %s\n",
736132Srobinson 								tok);
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 	(void) fclose(f);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate }
743