xref: /onnv-gate/usr/src/lib/libnsl/dial/callers.c (revision 9694:78fafb281255)
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 
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23132Srobinson /*	  All Rights Reserved	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*9694SScott.Rotondo@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
301219Sraf #include "mt.h"
310Sstevel@tonic-gate #include "uucp.h"
320Sstevel@tonic-gate 
33132Srobinson static void alarmtr(int);
34132Srobinson static jmp_buf Sjbuf;
35132Srobinson static char *fdig(char *);
36132Srobinson #ifndef SMALL
37132Srobinson static char *strecpy(char *, char *, char *);
380Sstevel@tonic-gate #endif
39132Srobinson static int interface(const char *);
40132Srobinson static int fd_mklock(int);
41132Srobinson static int getdialline(char *, int);
42132Srobinson static int chat(int, char *[], int, char *, char *);
43132Srobinson static void fixline(), fd_rmlock();
44132Srobinson static void translate(char *, char *);
45132Srobinson static int gdial(char *, char *[], int);
46132Srobinson static int	Modemctrl;
47132Srobinson static unsigned connecttime;
48132Srobinson static int (*Setup)();
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  *	to add a new caller:
520Sstevel@tonic-gate  *	declare the function that knows how to call on the device,
530Sstevel@tonic-gate  *	add a line to the callers table giving the name of the device
540Sstevel@tonic-gate  *	(from Devices file) and the name of the function
550Sstevel@tonic-gate  *	add the function to the end of this file
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #ifdef TLI
59132Srobinson static int	tlicall(char *[], char *[]);
600Sstevel@tonic-gate #endif /* TLI */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct caller Caller[] = {
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #ifdef TLI
650Sstevel@tonic-gate 	{"TLI",		tlicall},	/* AT&T Transport Layer Interface */
660Sstevel@tonic-gate #ifdef TLIS
670Sstevel@tonic-gate 	{"TLIS",	tlicall},	/* AT&T Transport Layer Interface */
680Sstevel@tonic-gate #endif /*  TLIS  */
690Sstevel@tonic-gate #endif /* TLI */
700Sstevel@tonic-gate 
71132Srobinson 	{NULL,		NULL}		/* this line must be last */
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  *	exphone - expand phone number for given prefix and number
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *	return code - none
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static void
exphone(char * in,char * out)81132Srobinson exphone(char *in, char *out)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	FILE *fn;
840Sstevel@tonic-gate 	char pre[MAXPH], npart[MAXPH], tpre[MAXPH], p[MAXPH];
850Sstevel@tonic-gate 	char buf[BUFSIZ];
860Sstevel@tonic-gate 	char *s1;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (!isalpha(*in)) {
890Sstevel@tonic-gate 		(void) strcpy(out, in);
900Sstevel@tonic-gate 		return;
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
93132Srobinson 	s1 = pre;
940Sstevel@tonic-gate 	while (isalpha(*in))
950Sstevel@tonic-gate 		*s1++ = *in++;
960Sstevel@tonic-gate 	*s1 = NULLCHAR;
970Sstevel@tonic-gate 	s1 = npart;
980Sstevel@tonic-gate 	while (*in != NULLCHAR)
990Sstevel@tonic-gate 		*s1++ = *in++;
1000Sstevel@tonic-gate 	*s1 = NULLCHAR;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	tpre[0] = NULLCHAR;
1031914Scasper 	fn = fopen(DIALCODES, "rF");
1040Sstevel@tonic-gate 	if (fn != NULL) {
1050Sstevel@tonic-gate 		while (fgets(buf, BUFSIZ, fn)) {
106132Srobinson 			if (sscanf(buf, "%60s%60s", p, tpre) < 1)
1070Sstevel@tonic-gate 				continue;
1080Sstevel@tonic-gate 			if (EQUALS(p, pre))
1090Sstevel@tonic-gate 				break;
1100Sstevel@tonic-gate 			tpre[0] = NULLCHAR;
1110Sstevel@tonic-gate 		}
112132Srobinson 		(void) fclose(fn);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	(void) strcpy(out, tpre);
1160Sstevel@tonic-gate 	(void) strcat(out, npart);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * repphone - Replace \D and \T sequences in arg with phone
1210Sstevel@tonic-gate  * expanding and translating as appropriate.
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate static char *
repphone(char * arg,char * phone,char * trstr)124132Srobinson repphone(char *arg, char *phone, char *trstr)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	static char *pbuf;	/* dynamically allocated below */
127132Srobinson 	char *fp, *tp;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (pbuf == NULL) {
130132Srobinson 		pbuf = malloc(2*(MAXPH+2));
1310Sstevel@tonic-gate 		if (pbuf == NULL)
1320Sstevel@tonic-gate 			return (arg);
1330Sstevel@tonic-gate 	}
134132Srobinson 	for (tp = pbuf; *arg; arg++) {
1350Sstevel@tonic-gate 		if (*arg != '\\') {
1360Sstevel@tonic-gate 			*tp++ = *arg;
1370Sstevel@tonic-gate 			continue;
1380Sstevel@tonic-gate 		} else {
1390Sstevel@tonic-gate 			switch (*(arg+1)) {
1400Sstevel@tonic-gate 			case 'T':
1410Sstevel@tonic-gate 				exphone(phone, tp);
1420Sstevel@tonic-gate 				translate(trstr, tp);
1430Sstevel@tonic-gate 				for (; *tp; tp++)
144132Srobinson 					;
1450Sstevel@tonic-gate 				arg++;
1460Sstevel@tonic-gate 				break;
1470Sstevel@tonic-gate 			case 'D':
148132Srobinson 				for (fp = phone; *tp = *fp++; tp++)
149132Srobinson 					;
1500Sstevel@tonic-gate 				arg++;
1510Sstevel@tonic-gate 				break;
1520Sstevel@tonic-gate 			default:
1530Sstevel@tonic-gate 				*tp++ = *arg;
1540Sstevel@tonic-gate 				break;
1550Sstevel@tonic-gate 			}
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	*tp = '\0';
1590Sstevel@tonic-gate 	return (pbuf);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
162132Srobinson static uint_t saved_mode;
1630Sstevel@tonic-gate static char saved_dcname[20];
1640Sstevel@tonic-gate 
165*9694SScott.Rotondo@Sun.COM static int pop_push(int);
166*9694SScott.Rotondo@Sun.COM static void setdevcfg(char *, char *);
167*9694SScott.Rotondo@Sun.COM static void ttygenbrk(int);
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * processdev - Process a line from the Devices file
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * return codes:
1720Sstevel@tonic-gate  *	file descriptor  -  succeeded
1730Sstevel@tonic-gate  *	FAIL  -  failed
1740Sstevel@tonic-gate  */
175132Srobinson static int
processdev(char * flds[],char * dev[])176132Srobinson processdev(char *flds[], char *dev[])
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	int dcf = -1;
179132Srobinson 	struct caller	*ca;
1800Sstevel@tonic-gate 	char *args[D_MAX+1], dcname[20];
181132Srobinson 	char **sdev;
1820Sstevel@tonic-gate 	int nullfd;
1830Sstevel@tonic-gate 	char *phonecl;			/* clear phone string */
1840Sstevel@tonic-gate 	char phoneex[2*(MAXPH+2)];	/* expanded phone string */
1850Sstevel@tonic-gate 	struct termio tty_orig;
1860Sstevel@tonic-gate 	int ret_orig = -1;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	sdev = dev;
1890Sstevel@tonic-gate 	/*	set up default "break" routine	*/
1900Sstevel@tonic-gate 	genbrk = ttygenbrk;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*	initialize Devconfig info	*/
1930Sstevel@tonic-gate 	DEBUG(5, "processdev: calling setdevcfg(%s, ", Progname);
1940Sstevel@tonic-gate 	DEBUG(5, "%s)\n", flds[F_TYPE]);
1950Sstevel@tonic-gate 	setdevcfg(Progname, flds[F_TYPE]);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	for (ca = Caller; ca->CA_type != NULL; ca++) {
1980Sstevel@tonic-gate 		/* This will find built-in caller functions */
1990Sstevel@tonic-gate 		if (EQUALS(ca->CA_type, dev[D_CALLER])) {
2000Sstevel@tonic-gate 			DEBUG(5, "Internal caller type %s\n", dev[D_CALLER]);
2010Sstevel@tonic-gate 			if (dev[D_ARG] == NULL) {
2020Sstevel@tonic-gate 				/* if NULL - assume translate */
203132Srobinson 				/* needed for for loop later to mark the end */
204132Srobinson 				dev[D_ARG+1] = NULL;
2050Sstevel@tonic-gate 				dev[D_ARG] = "\\T";
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 			dev[D_ARG] = repphone(dev[D_ARG], flds[F_PHONE], "");
208132Srobinson 			if ((dcf = (*(ca->CA_caller))(flds, dev)) < 0)
209132Srobinson 				return (dcf);
2100Sstevel@tonic-gate 			if (interface(ca->CA_type)) {
2110Sstevel@tonic-gate 				DEBUG(5, "interface(%s) failed", ca->CA_type);
2120Sstevel@tonic-gate 				Uerror = SS_DEVICE_FAILED;
2130Sstevel@tonic-gate 				/*	restore vanilla unix interface	*/
214132Srobinson 				(void) interface("UNIX");
2150Sstevel@tonic-gate 				return (FAIL);
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 			dev += 2; /* Skip to next CALLER and ARG */
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	if (dcf == -1) {
2220Sstevel@tonic-gate 		/* Here if not a built-in caller function */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		/* We do locking (file and advisory) after open	*/
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		/*
2270Sstevel@tonic-gate 		 * Open the line
2280Sstevel@tonic-gate 		 */
2290Sstevel@tonic-gate 		if (*dev[D_LINE] != '/') {
230132Srobinson 			(void) snprintf(dcname, sizeof (dcname),
231*9694SScott.Rotondo@Sun.COM 			    "/dev/%s", dev[D_LINE]);
2320Sstevel@tonic-gate 		} else {
2330Sstevel@tonic-gate 			(void) strcpy(dcname, dev[D_LINE]);
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 		/* take care of the possible partial open fd */
2360Sstevel@tonic-gate 		(void) close(nullfd = open("/", O_RDONLY));
2370Sstevel@tonic-gate 		if (setjmp(Sjbuf)) {
2380Sstevel@tonic-gate 			(void) close(nullfd);
2390Sstevel@tonic-gate 			DEBUG(1, "generic open timeout\n%s", "");
2400Sstevel@tonic-gate 			logent("generic open", "TIMEOUT");
2410Sstevel@tonic-gate 			Uerror = SS_CANT_ACCESS_DEVICE;
2420Sstevel@tonic-gate 			goto bad;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		(void) signal(SIGALRM, alarmtr);
2450Sstevel@tonic-gate 		(void) alarm(10);
2460Sstevel@tonic-gate 		if (Modemctrl) {
2470Sstevel@tonic-gate 			DEBUG(7, "opening with O_NDELAY set\n%s", "");
248132Srobinson 			dcf = open(dcname, (O_RDWR | O_NDELAY));
2490Sstevel@tonic-gate 			saved_mode = O_RDWR | O_NDELAY;
2500Sstevel@tonic-gate 		} else {
251132Srobinson 			dcf = open(dcname, O_RDWR);
2520Sstevel@tonic-gate 			saved_mode = O_RDWR;
2530Sstevel@tonic-gate 		}
254132Srobinson 		(void) strcpy(saved_dcname, dcname);
2550Sstevel@tonic-gate 		(void) alarm(0);
2560Sstevel@tonic-gate 		if (dcf < 0) {
2570Sstevel@tonic-gate 			DEBUG(1, "generic open failed, errno = %d\n", errno);
2580Sstevel@tonic-gate 			(void) close(nullfd);
2590Sstevel@tonic-gate 			logent("generic open", "FAILED");
2600Sstevel@tonic-gate 			Uerror = SS_CANT_ACCESS_DEVICE;
2610Sstevel@tonic-gate 			goto bad;
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		/* check locks BEFORE modifying the stream */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		if (fd_mklock(dcf) != SUCCESS) {
2670Sstevel@tonic-gate 			DEBUG(1, "failed to lock device %s\n", dcname);
2680Sstevel@tonic-gate 			Uerror = SS_LOCKED_DEVICE;
2690Sstevel@tonic-gate 			goto bad;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		if (Modemctrl) {
2730Sstevel@tonic-gate 			DEBUG(7, "clear O_NDELAY\n%s", "");
2741219Sraf 			if (fcntl(dcf, F_SETFL,
2751219Sraf 			    (fcntl(dcf, F_GETFL, 0) & ~O_NDELAY)) < 0) {
276132Srobinson 				DEBUG(7, "clear O_NDELAY failed, errno %d\n",
277*9694SScott.Rotondo@Sun.COM 				    errno);
2780Sstevel@tonic-gate 				Uerror = SS_DEVICE_FAILED;
2790Sstevel@tonic-gate 				goto bad;
2800Sstevel@tonic-gate 			}
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if ((*Setup)(MASTER, &dcf, &dcf)) {
2850Sstevel@tonic-gate 		/*	any device|system lock files we should remove?	*/
2860Sstevel@tonic-gate 		DEBUG(5, "MASTER Setup failed%s", "");
2870Sstevel@tonic-gate 		Uerror = SS_DEVICE_FAILED;
2880Sstevel@tonic-gate 		goto bad;
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/* configure any requested streams modules */
2920Sstevel@tonic-gate 	if (!pop_push(dcf)) {
293132Srobinson 		DEBUG(5, "STREAMS module configuration failed%s\n", "");
2940Sstevel@tonic-gate 		Uerror = SS_DEVICE_FAILED;
2950Sstevel@tonic-gate 		goto bad;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/* save initial state of line in case script fails */
2990Sstevel@tonic-gate 	ret_orig = ioctl(dcf, TCGETA, &tty_orig);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/* use sdev[] since dev[] is incremented for internal callers */
3020Sstevel@tonic-gate 	fixline(dcf, atoi(fdig(sdev[D_CLASS])), D_DIRECT);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * Now loop through the remaining callers and chat
3060Sstevel@tonic-gate 	 * according to scripts in dialers file.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	for (; dev[D_CALLER] != NULL; dev += 2) {
309132Srobinson 		int w;
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * Scan Dialers file to find an entry
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		if ((w = gdial(dev[D_CALLER], args, D_MAX)) < 1) {
3140Sstevel@tonic-gate 			logent("generic call to gdial", "FAILED");
3150Sstevel@tonic-gate 			Uerror = SS_CANT_ACCESS_DEVICE;
3160Sstevel@tonic-gate 			goto bad;
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		if (w <= 2)	/* do nothing - no chat */
3190Sstevel@tonic-gate 			break;
3200Sstevel@tonic-gate 		/*
3210Sstevel@tonic-gate 		 * Translate the phone number
3220Sstevel@tonic-gate 		 */
3230Sstevel@tonic-gate 		if (dev[D_ARG] == NULL) {
3240Sstevel@tonic-gate 			/* if NULL - assume no translation */
325132Srobinson 			/* needed for for loop to mark the end */
326132Srobinson 			dev[D_ARG+1] = NULL;
3270Sstevel@tonic-gate 			dev[D_ARG] = "\\D";
3280Sstevel@tonic-gate 		}
329132Srobinson 
3300Sstevel@tonic-gate 		phonecl = repphone(dev[D_ARG], flds[F_PHONE], args[1]);
3310Sstevel@tonic-gate 		exphone(phonecl, phoneex);
3320Sstevel@tonic-gate 		translate(args[1], phoneex);
3330Sstevel@tonic-gate 		/*
3340Sstevel@tonic-gate 		 * Chat
3350Sstevel@tonic-gate 		 */
3360Sstevel@tonic-gate 		if (chat(w-2, &args[2], dcf, phonecl, phoneex) != SUCCESS) {
3370Sstevel@tonic-gate 			CDEBUG(5, "\nCHAT gdial(%s) FAILED\n", dev[D_CALLER]);
3380Sstevel@tonic-gate 			Uerror = SS_CHAT_FAILED;
3390Sstevel@tonic-gate 			goto bad;
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Success at last!
3440Sstevel@tonic-gate 	 */
345132Srobinson 	(void) strcpy(Dc, sdev[D_LINE]);
3460Sstevel@tonic-gate 	return (dcf);
3470Sstevel@tonic-gate bad:
3480Sstevel@tonic-gate 	if (dcf >= 0) {
3490Sstevel@tonic-gate 		/* reset line settings if we got them in the beginning */
3500Sstevel@tonic-gate 		if (ret_orig == 0)
3510Sstevel@tonic-gate 			(void) ioctl(dcf, TCSETAW, &tty_orig);
3520Sstevel@tonic-gate 		fd_rmlock(dcf);
353132Srobinson 		(void) close(dcf);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	/*	restore vanilla unix interface	*/
356132Srobinson 	(void) interface("UNIX");
3570Sstevel@tonic-gate 	return (FAIL);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * clear_hup()	clear the hangup state of the given device
3620Sstevel@tonic-gate  */
363132Srobinson static int
clear_hup(int dcf)364132Srobinson clear_hup(int dcf)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	int ndcf;
3670Sstevel@tonic-gate 	if ((ndcf = open(saved_dcname, saved_mode)) < 0) {
3680Sstevel@tonic-gate 		return (FAIL);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	if (ndcf != dcf) {
371132Srobinson 		(void) close(ndcf);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	return (SUCCESS);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * translate the pairs of characters present in the first
3790Sstevel@tonic-gate  * string whenever the first of the pair appears in the second
3800Sstevel@tonic-gate  * string.
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate static void
translate(char * ttab,char * str)383132Srobinson translate(char *ttab, char *str)
3840Sstevel@tonic-gate {
385132Srobinson 	char *s;
3860Sstevel@tonic-gate 
387132Srobinson 	for (; *ttab && *(ttab+1); ttab += 2)
388132Srobinson 		for (s = str; *s; s++)
3890Sstevel@tonic-gate 			if (*ttab == *s)
3900Sstevel@tonic-gate 				*s = *(ttab+1);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
393132Srobinson #define	MAXLINE	512
394*9694SScott.Rotondo@Sun.COM 
395*9694SScott.Rotondo@Sun.COM static void dialreset(void);
396*9694SScott.Rotondo@Sun.COM #ifndef SMALL
397*9694SScott.Rotondo@Sun.COM static char *currdial(void);
398*9694SScott.Rotondo@Sun.COM #endif
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * Get the information about the dialer.
4010Sstevel@tonic-gate  * gdial(type, arps, narps)
4020Sstevel@tonic-gate  *	type	-> type of dialer (e.g., penril)
4030Sstevel@tonic-gate  *	arps	-> array of pointers returned by gdial
4040Sstevel@tonic-gate  *	narps	-> number of elements in array returned by gdial
4050Sstevel@tonic-gate  * Return value:
4060Sstevel@tonic-gate  *	-1	-> Can't open DIALERFILE
4070Sstevel@tonic-gate  *	0	-> requested type not found
4080Sstevel@tonic-gate  *	>0	-> success - number of fields filled in
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate static int
gdial(char * type,char * arps[],int narps)411132Srobinson gdial(char *type, char *arps[], int narps)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	static char *info;	/* dynamically allocated MAXLINE */
4140Sstevel@tonic-gate 	int na;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	DEBUG(2, "gdial(%s) called\n", type);
4170Sstevel@tonic-gate 	if (info == NULL) {
418132Srobinson 		info = malloc(MAXLINE);
4190Sstevel@tonic-gate 		if (info == NULL) {
4200Sstevel@tonic-gate 			DEBUG(1, "malloc failed for info in gdial\n", 0);
4210Sstevel@tonic-gate 			return (0);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	while (getdialline(info, MAXLINE)) {
4250Sstevel@tonic-gate 		if ((info[0] == '#') || (info[0] == ' ') ||
4260Sstevel@tonic-gate 		    (info[0] == '\t') || (info[0] == '\n'))
4270Sstevel@tonic-gate 			continue;
4280Sstevel@tonic-gate 		if ((na = getargs(info, arps, narps)) == 0)
4290Sstevel@tonic-gate 			continue;
4300Sstevel@tonic-gate 		if (EQUALS(arps[0], type)) {
431*9694SScott.Rotondo@Sun.COM 			DEBUG(5, "Trying caller script '%s'", type);
432*9694SScott.Rotondo@Sun.COM 			DEBUG(5, " from '%s'.\n", currdial());
433*9694SScott.Rotondo@Sun.COM 			dialreset();
434*9694SScott.Rotondo@Sun.COM 			bsfix(arps);
435*9694SScott.Rotondo@Sun.COM 			return (na);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	DEBUG(1, "%s not found in Dialers file\n", type);
4390Sstevel@tonic-gate 	dialreset();
4400Sstevel@tonic-gate 	return (0);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate #ifdef TLI
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate  *
4460Sstevel@tonic-gate  * AT&T Transport Layer Interface
4470Sstevel@tonic-gate  *
4480Sstevel@tonic-gate  * expected in Devices
449132Srobinson  *	TLI line1 - - TLI
4500Sstevel@tonic-gate  * or
4510Sstevel@tonic-gate  *	TLIS line1 - - TLIS
4520Sstevel@tonic-gate  *
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate #include <tiuser.h>
4560Sstevel@tonic-gate 
457132Srobinson static void tfaillog(int fd, const char *s);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate #define	CONNECT_ATTEMPTS	3
460132Srobinson #define	TFREE(p, type)	if ((p)) (void) t_free((char *)(p), (type))
4610Sstevel@tonic-gate 
462*9694SScott.Rotondo@Sun.COM static struct netbuf	*stoa(char *, struct netbuf *);
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate  * returns fd to remote uucp daemon
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate /*ARGSUSED*/
467132Srobinson static int
tlicall(char * flds[],char * dev[])468132Srobinson tlicall(char *flds[], char *dev[])
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	char		addrbuf[ BUFSIZ ];
4710Sstevel@tonic-gate 	char		devname[MAXNAMESIZE];
4720Sstevel@tonic-gate 	int		fd;
473132Srobinson 	int		i, j;
4740Sstevel@tonic-gate 	struct t_bind	*bind_ret = 0;
4750Sstevel@tonic-gate 	struct t_info	tinfo;
4760Sstevel@tonic-gate 	struct t_call	*sndcall = 0, *rcvcall = 0;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if (dev[D_LINE][0] != '/') {
4800Sstevel@tonic-gate 		/*	dev holds device name relative to /dev	*/
481132Srobinson 		(void) snprintf(devname, sizeof (devname),
482*9694SScott.Rotondo@Sun.COM 		    "/dev/%s", dev[D_LINE]);
4830Sstevel@tonic-gate 	} else {
4840Sstevel@tonic-gate 		/*	dev holds full path name of device	*/
485132Srobinson 		(void) strcpy(devname, dev[D_LINE]);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	/* gimme local transport endpoint */
4880Sstevel@tonic-gate 	errno = t_errno = 0;
4890Sstevel@tonic-gate 	if (setjmp(Sjbuf)) {
4900Sstevel@tonic-gate 		DEBUG(1, "t_open timeout\n%s", "");
4910Sstevel@tonic-gate 		logent("t_open", "TIMEOUT");
4920Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
4930Sstevel@tonic-gate 		return (FAIL);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
4960Sstevel@tonic-gate 	(void) alarm(5);
4970Sstevel@tonic-gate 	fd = t_open(devname, O_RDWR, &tinfo);
4980Sstevel@tonic-gate 	(void) alarm(0);
4990Sstevel@tonic-gate 	if (fd < 0) {
5000Sstevel@tonic-gate 		tfaillog(fd, "t_open");
5010Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
5020Sstevel@tonic-gate 		return (FAIL);
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	if (fd_mklock(fd) != SUCCESS) {
505132Srobinson 		(void) t_close(fd);
5060Sstevel@tonic-gate 		DEBUG(1, "tlicall: failed to lock device %s\n", devname);
5070Sstevel@tonic-gate 		Uerror = SS_LOCKED_DEVICE;
5080Sstevel@tonic-gate 		return (FAIL);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/* allocate tli structures	*/
5120Sstevel@tonic-gate 	errno = t_errno = 0;
513132Srobinson 	/* LINTED pointer cast */
514132Srobinson 	if ((bind_ret = (struct t_bind *)t_alloc(fd, T_BIND, T_ALL)) == NULL ||
515132Srobinson 	    /* LINTED pointer cast */
516132Srobinson 	    (sndcall = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL ||
517132Srobinson 	    /* LINTED pointer cast */
518132Srobinson 	    (rcvcall = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
5190Sstevel@tonic-gate 		tfaillog(fd, "t_alloc");
520132Srobinson 		TFREE(bind_ret, T_BIND);
521132Srobinson 		TFREE(sndcall, T_CALL);
5220Sstevel@tonic-gate 		TFREE(rcvcall, T_CALL);
5230Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
5240Sstevel@tonic-gate 		return (FAIL);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/* bind */
5280Sstevel@tonic-gate 	errno = t_errno = 0;
529132Srobinson 	if (t_bind(fd, (struct t_bind *)0, bind_ret) < 0) {
5300Sstevel@tonic-gate 		tfaillog(fd, "t_bind");
531132Srobinson 		TFREE(bind_ret, T_BIND);
532132Srobinson 		TFREE(sndcall, T_CALL);
5330Sstevel@tonic-gate 		TFREE(rcvcall, T_CALL);
5340Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
5350Sstevel@tonic-gate 		fd_rmlock(fd);
5360Sstevel@tonic-gate 		(void) t_close(fd);
5370Sstevel@tonic-gate 		return (FAIL);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	DEBUG(5, "tlicall: bound to %s\n", bind_ret->addr.buf);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Prepare to connect.
5430Sstevel@tonic-gate 	 *
5440Sstevel@tonic-gate 	 * If address begins with "\x", "\X", "\o", or "\O",
5450Sstevel@tonic-gate 	 * assume is hexadecimal or octal address and use stoa()
5460Sstevel@tonic-gate 	 * to convert it.
5470Sstevel@tonic-gate 	 *
5480Sstevel@tonic-gate 	 * Else is usual uucico address -- only \N's left to process.
5490Sstevel@tonic-gate 	 * Walk thru connection address, changing \N's to NULLCHARs.
5500Sstevel@tonic-gate 	 * Note:  If a NULLCHAR must be part of the connection address,
5510Sstevel@tonic-gate 	 * it must be overtly included in the address.  One recommended
5520Sstevel@tonic-gate 	 * way is to do it in the Devices file, thusly:
5530Sstevel@tonic-gate 	 *		Netname /dev/netport - - TLI \D\000
5540Sstevel@tonic-gate 	 * bsfix() turns \000 into \N and then the loop below makes it a
5550Sstevel@tonic-gate 	 * real, included-in-the-length null-byte.
5560Sstevel@tonic-gate 	 *
5570Sstevel@tonic-gate 	 * The DEBUG must print the strecpy'd address (so that
5580Sstevel@tonic-gate 	 * non-printables will have been replaced with C escapes).
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	DEBUG(5, "t_connect to addr \"%s\"\n",
562*9694SScott.Rotondo@Sun.COM 	    strecpy(addrbuf, dev[D_ARG], "\\"));
5630Sstevel@tonic-gate 
564132Srobinson 	if (dev[D_ARG][0] == '\\' && (dev[D_ARG][1] == 'x' ||
565*9694SScott.Rotondo@Sun.COM 	    dev[D_ARG][1] == 'X' || dev[D_ARG][1] == 'o' ||
566*9694SScott.Rotondo@Sun.COM 	    dev[D_ARG][1] == 'O')) {
567132Srobinson 		if (stoa(dev[D_ARG], &(sndcall->addr)) == NULL) {
5680Sstevel@tonic-gate 			DEBUG(5, "tlicall: stoa failed\n%s", "");
5690Sstevel@tonic-gate 			logent("tlicall", "string-to-address failed");
570132Srobinson 			TFREE(bind_ret, T_BIND);
571132Srobinson 			TFREE(sndcall, T_CALL);
5720Sstevel@tonic-gate 			TFREE(rcvcall, T_CALL);
5730Sstevel@tonic-gate 			Uerror = SS_NO_DEVICE;
5740Sstevel@tonic-gate 			fd_rmlock(fd);
5750Sstevel@tonic-gate 			(void) t_close(fd);
5760Sstevel@tonic-gate 			return (FAIL);
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 	} else {
5790Sstevel@tonic-gate 		for (i = j = 0; i < BUFSIZ && dev[D_ARG][i] != NULLCHAR;
580*9694SScott.Rotondo@Sun.COM 		    ++i, ++j) {
581132Srobinson 			if (dev[D_ARG][i] == '\\' && dev[D_ARG][i+1] == 'N') {
5820Sstevel@tonic-gate 				addrbuf[j] = NULLCHAR;
5830Sstevel@tonic-gate 				++i;
584132Srobinson 			} else {
5850Sstevel@tonic-gate 				addrbuf[j] = dev[D_ARG][i];
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		sndcall->addr.buf = addrbuf;
5890Sstevel@tonic-gate 		sndcall->addr.len = j;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (setjmp(Sjbuf)) {
5930Sstevel@tonic-gate 		DEBUG(4, "timeout tlicall\n%s", "");
5940Sstevel@tonic-gate 		logent("tlicall", "TIMEOUT");
595132Srobinson 		TFREE(bind_ret, T_BIND);
596132Srobinson 		TFREE(sndcall, T_CALL);
5970Sstevel@tonic-gate 		TFREE(rcvcall, T_CALL);
5980Sstevel@tonic-gate 		Uerror = SS_NO_DEVICE;
5990Sstevel@tonic-gate 		fd_rmlock(fd);
6000Sstevel@tonic-gate 		(void) t_close(fd);
6010Sstevel@tonic-gate 		return (FAIL);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 	(void) signal(SIGALRM, alarmtr);
6040Sstevel@tonic-gate 	(void) alarm(connecttime);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/* connect to the service -- some listeners can't handle */
6070Sstevel@tonic-gate 	/* multiple connect requests, so try it a few times */
6080Sstevel@tonic-gate 	errno = t_errno = 0;
6090Sstevel@tonic-gate 	for (i = 0; i < CONNECT_ATTEMPTS; ++i) {
6100Sstevel@tonic-gate 		if (t_connect(fd, sndcall, rcvcall) == 0)
6110Sstevel@tonic-gate 			break;
6120Sstevel@tonic-gate 		if ((t_errno == TLOOK) && (t_look(fd) == T_DISCONNECT)) {
613132Srobinson 			(void) t_rcvdis(fd, NULL);
6140Sstevel@tonic-gate 			(void) alarm(0);
6150Sstevel@tonic-gate 		} else {
6160Sstevel@tonic-gate 			(void) alarm(0);
6170Sstevel@tonic-gate 			tfaillog(fd, "t_connect");
618132Srobinson 			TFREE(bind_ret, T_BIND);
619132Srobinson 			TFREE(sndcall, T_CALL);
6200Sstevel@tonic-gate 			TFREE(rcvcall, T_CALL);
6210Sstevel@tonic-gate 			Uerror = SS_DIAL_FAILED;
6220Sstevel@tonic-gate 			fd_rmlock(fd);
6230Sstevel@tonic-gate 			(void) t_close(fd);
6240Sstevel@tonic-gate 			return (FAIL);
6250Sstevel@tonic-gate 		}
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	(void) alarm(0);
628132Srobinson 	TFREE(bind_ret, T_BIND);
629132Srobinson 	TFREE(sndcall, T_CALL);
6300Sstevel@tonic-gate 	TFREE(rcvcall, T_CALL);
6310Sstevel@tonic-gate 	if (i == CONNECT_ATTEMPTS) {
6320Sstevel@tonic-gate 		tfaillog(fd, "t_connect");
6330Sstevel@tonic-gate 		Uerror = SS_DIAL_FAILED;
6340Sstevel@tonic-gate 		fd_rmlock(fd);
6350Sstevel@tonic-gate 		(void) t_close(fd);
6360Sstevel@tonic-gate 		return (FAIL);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	errno = t_errno = 0;
6390Sstevel@tonic-gate 	(void) strcpy(Dc, dev[D_CALLER]);
6400Sstevel@tonic-gate 	return (fd);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate #endif /* TLI */
643