xref: /csrg-svn/usr.bin/uucp/uucico/conn.c (revision 34167)
133581Srick #ifndef lint
2*34167Srick static char sccsid[] = "@(#)conn.c	5.15	(Berkeley) 05/04/88";
333581Srick #endif
413642Ssam 
523596Sbloom #include <signal.h>
613642Ssam #include "uucp.h"
713642Ssam #include <setjmp.h>
813642Ssam #include <ctype.h>
913642Ssam #include <errno.h>
1017834Sralph #ifdef	USG
1113642Ssam #include <termio.h>
1213642Ssam #include <fcntl.h>
1313642Ssam #endif
1417834Sralph #ifndef	USG
1513642Ssam #include <sgtty.h>
1613642Ssam #endif
1717834Sralph #ifdef BSD4_2
1817834Sralph #include <sys/time.h>
1917834Sralph #else
2017834Sralph #include <time.h>
2117834Sralph #endif
2213642Ssam 
2313642Ssam #define MAXC 1000
2413642Ssam 
2513642Ssam extern jmp_buf Sjbuf;
2617834Sralph jmp_buf Cjbuf;
2718619Sralph extern int errno, onesys;
2817834Sralph extern char *sys_errlist[];
2918619Sralph extern char MaxGrade, DefMaxGrade;
3013642Ssam 
3113642Ssam /* Parity control during login procedure */
3213642Ssam #define	P_ZERO	0
3313642Ssam #define	P_ONE	1
3413642Ssam #define	P_EVEN	2
3513642Ssam #define	P_ODD	3
3617834Sralph 
3717834Sralph #define ABORT -2
3817834Sralph 
3917834Sralph char 	*AbortOn = NULL;
4013642Ssam char	par_tab[128];	/* must be power of two */
4117834Sralph int	linebaudrate;	/* used for the sleep test in pk1.c */
4213642Ssam int next_fd = -1;	/* predicted fd to close interrupted opens */
4325705Sbloom 
4425705Sbloom char *PCP = "PCP";	/* PC Pursuit device type */
4525127Sbloom /*
4625127Sbloom  *	catch alarm routine for "expect".
4713642Ssam  */
4813642Ssam alarmtr()
4913642Ssam {
5013642Ssam 	signal(SIGALRM, alarmtr);
5113642Ssam 	if (next_fd >= 0) {
5213642Ssam 		if (close(next_fd))
5313642Ssam 			logent("FAIL", "ACU LINE CLOSE");
5413642Ssam 		next_fd = -1;
5513642Ssam 	}
5613642Ssam 	longjmp(Sjbuf, 1);
5713642Ssam }
5813642Ssam 
5925705Sbloom /* This template is for seismo to call ihnp4
6025705Sbloom  * the 3 lines marked ---> will be overwritten for the appropriate city
6125705Sbloom  */
6225705Sbloom #define PCP_BAUD	3
6325705Sbloom #define PCP_PHONE	4
6433562Srick #define PCP_CITY	14
6533562Srick #define PCP_PASSWORD	16
6633562Srick #define PCP_RPHONE	20
6733562Srick #define NPCFIELDS	23
6825705Sbloom 
6925705Sbloom static char *PCFlds[] = {
7025705Sbloom 	"PC-PURSUIT",
7125705Sbloom 	"Any",
7225705Sbloom 	"ACU",
7325705Sbloom 	"1200",
7433562Srick 	CNULL,
7533562Srick 	CNULL,
7633562Srick 	"P_ZERO",	/* Telenet insists on zero parity */
7725705Sbloom 	"ABORT",
7833562Srick 	"BUSY",		/* Abort on Busy Signal */
7933562Srick 	CNULL,
8033562Srick 	"\\d\\d\\r\\d\\r",	/* Get telenet's attention */
8133562Srick 	"TERMINAL=~3-\r-TERM~3-\r-TERM~5", 	/* Terminal type ? */
8233562Srick 	"\\r",
8333562Srick 	"@",		/* telenet's prompt */
8433562Srick 	"D/DCWAS/21,telenetloginstring", /* overwritten later */
8533562Srick 	"PASSWORD",
8633562Srick 	CNULL,		/* telenet password */
8733562Srick 	"CONNECTED",	/* We're now talking to a Hayes in the remote city */
8833562Srick 	"ATZ",		/* Reset it */
8933562Srick 	"OK",
9033562Srick 	"ATDT6907171", /* overwritten */
9133562Srick 	"CONNECT",
9233562Srick 	"\\d\\r",		/* We're in !*/
9333562Srick 	CNULL,
9425705Sbloom };
9525705Sbloom 
9633562Srick static char PCP_brand[25];
97*34167Srick int Dcf = -1;
98*34167Srick char *Flds[MAXC/10];
99*34167Srick char LineType[10];
100*34167Srick extern int LocalOnly;
10125705Sbloom 
10218619Sralph /*
10318619Sralph  *	place a telephone call to system and login, etc.
10413642Ssam  *
10513642Ssam  *	return codes:
10613642Ssam  *		CF_SYSTEM: don't know system
10713642Ssam  *		CF_TIME: wrong time to call
10813642Ssam  *		CF_DIAL: call failed
10913642Ssam  *		CF_NODEV: no devices available to place call
11013642Ssam  *		CF_LOGIN: login/password dialog failed
11113642Ssam  *
11213642Ssam  *		>0  - file no.  -  connect ok
11313642Ssam  */
11413642Ssam conn(system)
11513642Ssam char *system;
11613642Ssam {
11725705Sbloom 	int nf;
11818619Sralph 	char info[MAXC], wkpre[NAMESIZE], file[NAMESIZE];
11913642Ssam 	register FILE *fsys;
12013642Ssam 	int fcode = 0;
12113642Ssam 
12213642Ssam 	nf = 0;
12313642Ssam 
12413642Ssam 	fsys = fopen(SYSFILE, "r");
12533948Srick 	if (fsys == NULL) {
12633948Srick 		syslog(LOG_ERR, "fopen(%s) failed: %m", SYSFILE);
12733948Srick 		cleanup(FAIL);
12833948Srick 	}
12913642Ssam 
13017834Sralph 	DEBUG(4, "finds (%s) called\n", system);
13125127Sbloom keeplooking:
13217834Sralph 	while((nf = finds(fsys, system, info, Flds)) > 0) {
13325966Sbloom 		strncpy(LineType, Flds[F_LINE], 10);
13417834Sralph 		if (LocalOnly) {
13525966Sbloom 			if (strcmp("TCP", LineType)
13625966Sbloom 				&& strcmp("DIR", LineType)
13725966Sbloom 				&& strcmp("LOCAL", LineType) ) {
13825705Sbloom 					fcode = CF_TIME;
13925705Sbloom 					continue;
14025705Sbloom 			}
14117834Sralph 		}
14223596Sbloom 		sprintf(wkpre, "%c.%.*s", CMDPRE, SYSNSIZE, Rmtname);
14318619Sralph 		if (!onesys && MaxGrade != DefMaxGrade &&
14425705Sbloom 			!iswrk(file, "chk", Spool, wkpre))  {
14525705Sbloom 				fcode = CF_TIME;
14625705Sbloom 				continue;
14725705Sbloom 		}
14825705Sbloom 		/* For GTE's PC Pursuit */
14925966Sbloom 		if (snccmp(LineType, PCP) == SAME) {
15025705Sbloom 			FILE *dfp;
15125705Sbloom 			int status;
15225705Sbloom 			static struct Devices dev;
15333562Srick 
15425705Sbloom 			dfp = fopen(DEVFILE, "r");
15533948Srick 			if (dfp == NULL) {
15633948Srick 				syslog(LOG_ERR, "fopen(%s) failed: %m",
15733948Srick 					DEVFILE);
15833948Srick 				cleanup(FAIL);
15933948Srick 			}
16025705Sbloom 			while ((status=rddev(dfp, &dev)) != FAIL
16125705Sbloom 				&& strcmp(PCP, dev.D_type) != SAME)
16225705Sbloom 					;
16325705Sbloom 			fclose(dfp);
16425705Sbloom 			if (status == FAIL)
16525705Sbloom 				continue;
16625705Sbloom 			if (mlock(PCP) == FAIL) {
16725705Sbloom 				fcode = CF_NODEV;
16825705Sbloom 				logent("DEVICE", "NO");
16925705Sbloom 				continue;
17025705Sbloom 			}
17125705Sbloom 			PCFlds[PCP_BAUD] = dev.D_class;
17225705Sbloom 			PCFlds[PCP_PHONE] = dev.D_calldev;
17333562Srick 			sprintf(PCFlds[PCP_CITY], "c d/%s%s,%s",
17433562Srick 				Flds[F_CLASS],
17533562Srick 				index(Flds[F_CLASS], '/') == NULL ? "/12" : "",
17633562Srick 				dev.D_arg[D_CHAT]);
17733562Srick 			PCFlds[PCP_PASSWORD] = dev.D_line;
17833562Srick 			strncpy(&PCFlds[PCP_RPHONE][4], Flds[F_PHONE], 7);
17925705Sbloom 			strncpy(PCP_brand, dev.D_brand, sizeof(PCP_brand));
18033562Srick 			if ((fcode = getto(PCFlds)) < 0) {
18133562Srick 				rmlock(PCP);
18225705Sbloom 				continue;
18333562Srick 			}
18425705Sbloom 			Dcf = fcode;
18525705Sbloom 			fcode = login(NPCFIELDS, PCFlds, Dcf);
18633562Srick 			if (fcode == SUCCESS)
18733562Srick 				break;
18833562Srick 			fcode = CF_DIAL;
18933562Srick 			rmlock(PCP);
19033562Srick 			/* end PC Pursuit */
19133562Srick 		} else if ((fcode = getto(Flds)) > 0)  {
19233562Srick 			Dcf = fcode;
19313642Ssam 			break;
19433562Srick 		}
19513642Ssam 	}
19613642Ssam 
19725127Sbloom 	if (nf <= 0) {
19825127Sbloom 		fclose(fsys);
19917834Sralph 		return fcode ? fcode : nf;
20025127Sbloom 	}
20113642Ssam 
20225705Sbloom 
20325705Sbloom 	if (fcode >= 0) {
20425705Sbloom 		DEBUG(4, "login %s\n", "called");
205*34167Srick 		setproctitle("login");
20633562Srick 		fcode = login(nf, Flds, Dcf); }
20725705Sbloom 	if (fcode < 0) {
20813642Ssam 		clsacu();
20925705Sbloom 		if (fcode == ABORT) {
21025127Sbloom 			fcode = CF_DIAL;
21125127Sbloom 			goto  keeplooking;
21225127Sbloom 		} else {
21325127Sbloom 			fclose(fsys);
21423596Sbloom 			return CF_LOGIN;
21525127Sbloom 		}
21613642Ssam 	}
21725127Sbloom 	fclose(fsys);
21825705Sbloom 	fioclex(Dcf);
21925705Sbloom 	return Dcf;
22013642Ssam }
22113642Ssam 
222*34167Srick int nulldev();
223*34167Srick int (*CU_end)() = nulldev;
224*34167Srick 
22525127Sbloom /*
22625127Sbloom  *	connect to remote machine
22713642Ssam  *
22813642Ssam  *	return codes:
22913642Ssam  *		>0  -  file number - ok
23013642Ssam  *		FAIL  -  failed
23113642Ssam  */
23213642Ssam getto(flds)
23313642Ssam register char *flds[];
23413642Ssam {
23513642Ssam 	register struct condev *cd;
23634094Sbostic 	int diropn();
23725127Sbloom 	char *line;
23813642Ssam 
23917834Sralph 	DEBUG(4, "getto: call no. %s ", flds[F_PHONE]);
24013642Ssam 	DEBUG(4, "for sys %s\n", flds[F_NAME]);
24113642Ssam 
24225127Sbloom 	if (snccmp(flds[F_LINE], "LOCAL") == SAME)
24325127Sbloom 		line = "ACU";
24425127Sbloom 	else
24525127Sbloom 		line = flds[F_LINE];
24625127Sbloom #ifdef DIALINOUT
24725127Sbloom 	if (snccmp(line, "ACU") != SAME)
24825127Sbloom 		reenable();
24925127Sbloom #endif DIALINOUT
25013642Ssam 	CU_end = nulldev;
25125705Sbloom 	if (snccmp(line, PCP) == SAME) {
25225705Sbloom 		for(cd = condevs; cd->CU_meth != NULL; cd++) {
25325705Sbloom 			if (snccmp(PCP_brand, cd->CU_brand) == SAME) {
25425705Sbloom 				CU_end = cd->CU_clos;
25525705Sbloom 				return diropn(flds);
25625705Sbloom 			}
25713642Ssam 		}
25825705Sbloom 		logent(PCP_brand, "UNSUPPORTED ACU TYPE");
25925705Sbloom 	} else {
26025705Sbloom 		for (cd = condevs; cd->CU_meth != NULL; cd++) {
26125705Sbloom 			if (snccmp(cd->CU_meth, line) == SAME) {
26225705Sbloom 				DEBUG(4, "Using %s to call\n", cd->CU_meth);
26325705Sbloom 				return (*(cd->CU_gen))(flds);
26425705Sbloom 			}
26525705Sbloom 		}
26625705Sbloom 		DEBUG(1, "Can't find %s, assuming DIR\n", flds[F_LINE]);
26713642Ssam 	}
26817834Sralph 	return diropn(flds);	/* search failed, so use direct */
26917834Sralph }
27013642Ssam 
27125127Sbloom /*
27225127Sbloom  *	close call unit
27313642Ssam  *
27413642Ssam  *	return codes:  none
27513642Ssam  */
27613642Ssam clsacu()
27713642Ssam {
27817834Sralph 	/* make *sure* Dcf is no longer exclusive.
27917834Sralph 	 * Otherwise dual call-in/call-out modems could get stuck.
28017834Sralph 	 * Unfortunately, doing this here is not ideal, but it is the
28117834Sralph 	 * easiest place to put the call.
28217834Sralph 	 * Hopefully everyone honors the LCK protocol, of course
28317834Sralph 	 */
28425705Sbloom #ifdef	TIOCNXCL
28523596Sbloom 	if (!IsTcpIp && Dcf >= 0 && ioctl(Dcf, TIOCNXCL, STBNULL) < 0)
28623596Sbloom 		DEBUG(5, "clsacu ioctl %s\n", sys_errlist[errno]);
28717834Sralph #endif
28817834Sralph 	if  (setjmp(Sjbuf))
28917834Sralph 		logent(Rmtname, "CLOSE TIMEOUT");
29017834Sralph 	else {
29117834Sralph 		signal(SIGALRM, alarmtr);
29217834Sralph 		alarm(20);
29317834Sralph 		(*(CU_end))(Dcf);
29417834Sralph 		alarm(0);
29517834Sralph 	}
29613642Ssam 	if (close(Dcf) == 0) {
29713642Ssam 		DEBUG(4, "fd %d NOT CLOSED by CU_clos\n", Dcf);
29813642Ssam 		logent("clsacu", "NOT CLOSED by CU_clos");
29913642Ssam 	}
30013642Ssam 	Dcf = -1;
30113642Ssam 	CU_end = nulldev;
30213642Ssam }
30313642Ssam 
30425127Sbloom /*
30525127Sbloom  *	expand phone number for given prefix and number
30613642Ssam  */
30713642Ssam exphone(in, out)
30813642Ssam register char *in, *out;
30913642Ssam {
31013642Ssam 	FILE *fn;
31113642Ssam 	char pre[MAXPH], npart[MAXPH], tpre[MAXPH], p[MAXPH];
31213642Ssam 	char buf[BUFSIZ];
31313642Ssam 	register char *s1;
31413642Ssam 
31517834Sralph 	if (!isascii(*in) || !isalpha(*in)) {
31613642Ssam 		strcpy(out, in);
31713642Ssam 		return;
31813642Ssam 	}
31913642Ssam 
32013642Ssam 	s1=pre;
32117834Sralph 	while (isascii(*in) && isalpha(*in))
32213642Ssam 		*s1++ = *in++;
32313642Ssam 	*s1 = '\0';
32413642Ssam 	s1 = npart;
32513642Ssam 	while (*in != '\0')
32613642Ssam 		*s1++ = *in++;
32713642Ssam 	*s1 = '\0';
32813642Ssam 
32913642Ssam 	tpre[0] = '\0';
33013642Ssam 	if ((fn = fopen(DIALFILE, "r")) == NULL)
33113642Ssam 		DEBUG(2, "CAN'T OPEN %s\n", DIALFILE);
33213642Ssam 	else {
33313642Ssam 		while (cfgets(buf, BUFSIZ, fn)) {
33417834Sralph 			if (sscanf(buf, "%s%s", p, tpre) != 2)
33517834Sralph 				continue;
33613642Ssam 			if (strcmp(p, pre) == SAME)
33713642Ssam 				goto found;
33813642Ssam 			tpre[0] = '\0';
33913642Ssam 		}
34013642Ssam 		DEBUG(2, "CAN'T FIND dialcodes prefix '%s'\n", pre);
34113642Ssam 	found:;
34213642Ssam 		fclose(fn);
34313642Ssam 	}
34413642Ssam 
34513642Ssam 	strcpy(out, tpre);
34613642Ssam 	strcat(out, npart);
34713642Ssam }
34813642Ssam 
34918619Sralph /*
35018619Sralph  *	read and decode a line from device file
35113642Ssam  *
35213642Ssam  *	return code - FAIL at end-of file; 0 otherwise
35313642Ssam  */
35413642Ssam rddev(fp, dev)
35513642Ssam register struct Devices *dev;
35613642Ssam FILE *fp;
35713642Ssam {
35817834Sralph 	register int na;
35913642Ssam 
36017834Sralph 	if (!cfgets(dev->D_argbfr, sizeof(dev->D_argbfr), fp))
36117834Sralph 		return FAIL;
36217834Sralph 	na = getargs(dev->D_argbfr, dev->D_arg, 20);
36333948Srick 	if (na < 4) {
36433948Srick 		syslog(LOG_ERR, "%s: invalid device entry", dev->D_argbfr);
36533948Srick 		cleanup(FAIL);
36633948Srick 	}
36717834Sralph 	if (na == 4) {
36817834Sralph 		dev->D_brand = "";
36917834Sralph 		na++;
37017834Sralph 	}
37113642Ssam 	dev->D_speed = atoi(fdig(dev->D_class));
37217834Sralph 	dev->D_numargs = na;
37317834Sralph 	return 0;
37413642Ssam }
37513642Ssam 
37618619Sralph /*
37718619Sralph  *	set system attribute vector
37813642Ssam  *
37913642Ssam  *	return codes:
38013642Ssam  *		>0  -  number of arguments in vector - succeeded
38113642Ssam  *		CF_SYSTEM  -  system name not found
38213642Ssam  *		CF_TIME  -  wrong time to call
38313642Ssam  */
38413642Ssam finds(fsys, sysnam, info, flds)
38513642Ssam char *sysnam, info[], *flds[];
38613642Ssam FILE *fsys;
38713642Ssam {
38813642Ssam 	int na;
38913642Ssam 	int fcode = 0;
39013642Ssam 
39113642Ssam 	/* format of fields
39213642Ssam 	 *	0 name;
39313642Ssam 	 *	1 time
39413642Ssam 	 *	2 acu/hardwired
39513642Ssam 	 *	3 speed
39613642Ssam 	 *	etc
39713642Ssam 	 */
39813642Ssam 	while (cfgets(info, MAXC, fsys) != NULL) {
39917834Sralph 		na = getargs(info, flds, MAXC/10);
40023596Sbloom 		if (strncmp(sysnam, flds[F_NAME], MAXBASENAME) != SAME)
40113642Ssam 			continue;
40218619Sralph 		if (ifdate(flds[F_TIME]) != FAIL)
40313642Ssam 			/*  found a good entry  */
40417834Sralph 			return na;
40513642Ssam 		DEBUG(2, "Wrong time ('%s') to call\n", flds[F_TIME]);
40613642Ssam 		fcode = CF_TIME;
40713642Ssam 	}
40817834Sralph 	return fcode ? fcode : CF_SYSTEM;
40913642Ssam }
41013642Ssam 
41118619Sralph /*
41218619Sralph  *	do login conversation
41313642Ssam  *
41423596Sbloom  *	return codes:  SUCCESS  |  FAIL
41513642Ssam  */
41613642Ssam login(nf, flds, fn)
41713642Ssam register char *flds[];
41813642Ssam int nf, fn;
41913642Ssam {
42013642Ssam 	register char *want, *altern;
42113642Ssam 	int k, ok;
42213642Ssam 
42333948Srick 	if (nf < 4) {
42433948Srick 		syslog(LOG_ERR, "Too few log fields: %d", nf);
42533948Srick 		cleanup(FAIL);
42633948Srick 	}
42717834Sralph 	if (setjmp(Cjbuf))
42817834Sralph 		return FAIL;
42917834Sralph 	AbortOn = NULL;
43013642Ssam 	for (k = F_LOGIN; k < nf; k += 2) {
43113642Ssam 		want = flds[k];
43233562Srick 		if (want == NULL)
43333562Srick 			want = "";
43413642Ssam 		ok = FAIL;
43517834Sralph 		while (ok != SUCCESS) {
43613642Ssam 			altern = index(want, '-');
43713642Ssam 			if (altern != NULL)
43813642Ssam 				*altern++ = '\0';
43917834Sralph 			if (strcmp(want, "ABORT") == 0) {
44017834Sralph 				AbortOn = flds[k+1];
44117834Sralph 				DEBUG(4, "ABORT ON: %s\n", AbortOn);
44217834Sralph 				goto nextfield;
44317834Sralph 			}
44425705Sbloom 			DEBUG(4, "wanted \"%s\"\n", want);
44513642Ssam 			ok = expect(want, fn);
44617834Sralph 			DEBUG(4, "got: %s\n", ok ? "?" : "that");
44717834Sralph 			if (ok == FAIL) {
44817834Sralph 				if (altern == NULL) {
44917834Sralph 					logent("LOGIN", _FAILED);
45017834Sralph 					return FAIL;
45117834Sralph 				}
45217834Sralph 				want = index(altern, '-');
45317834Sralph 				if (want != NULL)
45417834Sralph 					*want++ = '\0';
45517834Sralph 				sendthem(altern, fn);
45617834Sralph 			} else
45717834Sralph 				if (ok == ABORT) {
45833562Srick 					char sbuf[MAXFULLNAME];
45933562Srick 					sprintf(sbuf, "LOGIN ABORTED on \"%s\"",						AbortOn);
46033562Srick 					logent(sbuf, _FAILED);
46123596Sbloom 					return ABORT;
46217834Sralph 				}
46313642Ssam 		}
46417834Sralph 		sleep(1);
46513642Ssam 		if (k+1 < nf)
46613642Ssam 			sendthem(flds[k+1], fn);
46717834Sralph nextfield: ;
46813642Ssam 	}
46917834Sralph 	return SUCCESS;
47013642Ssam }
47113642Ssam 
47213642Ssam 
47317834Sralph /* conditional table generation to support odd speeds */
47413642Ssam struct sg_spds {int sp_val, sp_name;} spds[] = {
47513642Ssam #ifdef B50
47613642Ssam 	{  50,	 B50},
47713642Ssam #endif
47813642Ssam #ifdef B75
47913642Ssam 	{  75,	 B75},
48013642Ssam #endif
48113642Ssam #ifdef B110
48213642Ssam 	{ 110,	B110},
48313642Ssam #endif
48413642Ssam #ifdef B150
48513642Ssam 	{ 150,	B150},
48613642Ssam #endif
48713642Ssam #ifdef B200
48813642Ssam 	{ 200,	B200},
48913642Ssam #endif
49013642Ssam #ifdef B300
49113642Ssam 	{ 300,  B300},
49213642Ssam #endif
49313642Ssam #ifdef B600
49413642Ssam 	{600,	B600},
49513642Ssam #endif
49613642Ssam #ifdef B1200
49713642Ssam 	{1200, B1200},
49813642Ssam #endif
49913642Ssam #ifdef B1800
50013642Ssam 	{1800, B1800},
50113642Ssam #endif
50213642Ssam #ifdef B2000
50313642Ssam 	{2000, B2000},
50413642Ssam #endif
50513642Ssam #ifdef B2400
50613642Ssam 	{2400, B2400},
50713642Ssam #endif
50813642Ssam #ifdef B3600
50913642Ssam 	{3600, B3600},
51013642Ssam #endif
51113642Ssam #ifdef B4800
51213642Ssam 	{4800, B4800},
51313642Ssam #endif
51413642Ssam #ifdef B7200
51513642Ssam 	{7200, B7200},
51613642Ssam #endif
51713642Ssam #ifdef B9600
51813642Ssam 	{9600, B9600},
51913642Ssam #endif
52013642Ssam #ifdef B19200
52117834Sralph 	{19200, B19200},
52213642Ssam #endif
52317834Sralph #ifdef EXTA
52417834Sralph 	{19200, EXTA},
52517834Sralph #endif
52613642Ssam 	{0, 0}
52713642Ssam };
52813642Ssam 
52918619Sralph /*
53018619Sralph  *	set speed/echo/mode...
53113642Ssam  *
53213642Ssam  *	return codes:  none
53313642Ssam  */
53413642Ssam fixline(tty, spwant)
53513642Ssam int tty, spwant;
53613642Ssam {
53717834Sralph #ifdef	USG
53813642Ssam 	struct termio ttbuf;
53917834Sralph #else	!USG
54013642Ssam 	struct sgttyb ttbuf;
54117834Sralph #endif !USG
54213642Ssam 	register struct sg_spds *ps;
54313642Ssam 	int speed = -1;
54413642Ssam 
54513642Ssam 	for (ps = spds; ps->sp_val; ps++)
54613642Ssam 		if (ps->sp_val == spwant)
54713642Ssam 			speed = ps->sp_name;
54833948Srick 	if (speed < 0) {
54933948Srick 		syslog(LOG_ERR, "unrecognized speed: %d", speed);
55033948Srick 		cleanup(FAIL);
55133948Srick 	}
55217834Sralph #ifdef	USG
55323596Sbloom 	if (ioctl(tty, TCGETA, &ttbuf) < 0)
55423596Sbloom 		return FAIL;
55513642Ssam 	/* ttbuf.sg_flags = (ANYP|RAW);
55613642Ssam 	ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed; */
55713642Ssam 	ttbuf.c_iflag = (ushort)0;
55813642Ssam 	ttbuf.c_oflag = (ushort)0;
55913642Ssam 	ttbuf.c_cflag = (speed|CS8|HUPCL|CREAD);
56013642Ssam 	ttbuf.c_lflag = (ushort)0;
56113642Ssam 	ttbuf.c_cc[VMIN] = 6;
56213642Ssam 	ttbuf.c_cc[VTIME] = 1;
56323596Sbloom 	if (ioctl(tty, TCSETA, &ttbuf) < 0)
56423596Sbloom 		return FAIL;
56517834Sralph #else	!USG
56623596Sbloom 	if (ioctl(tty, TIOCGETP, &ttbuf) < 0)
56723596Sbloom 		return FAIL;
56813642Ssam 	ttbuf.sg_flags = (ANYP|RAW);
56913642Ssam 	ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed;
57023596Sbloom 	if (ioctl(tty, TIOCSETP, &ttbuf) < 0)
57123596Sbloom 		return FAIL;
57213642Ssam #endif
57317834Sralph #ifndef	USG
57423596Sbloom 	if (ioctl(tty, TIOCHPCL, STBNULL) < 0)
57523596Sbloom 		return FAIL;
57623596Sbloom 	if (ioctl(tty, TIOCEXCL, STBNULL) < 0)
57723596Sbloom 		return FAIL;
57813642Ssam #endif
57917834Sralph 	linebaudrate = spwant;
58023596Sbloom 	return SUCCESS;
58113642Ssam }
58213642Ssam 
58317834Sralph #define MR 100
58413642Ssam 
58518619Sralph /*
58618619Sralph  *	look for expected string
58713642Ssam  *
58813642Ssam  *	return codes:
58913642Ssam  *		0  -  found
59013642Ssam  *		FAIL  -  lost line or too many characters read
59113642Ssam  *		some character  -  timed out
59213642Ssam  */
59313642Ssam expect(str, fn)
59413642Ssam register char *str;
59513642Ssam int fn;
59613642Ssam {
59714592Skarels 	char rdvec[MR];
59817834Sralph 	register char *rp = rdvec, *strptr;
59917834Sralph 	int kr, cnt_char;
60013642Ssam 	char nextch;
60125127Sbloom 	int timo = MAXMSGTIME;
60213642Ssam 
60317834Sralph 	if (*str == '\0' || strcmp(str, "\"\"") == SAME)
60417834Sralph 		return SUCCESS;
60517834Sralph 	/* Cleanup str, convert \0xx strings to one char  */
60617834Sralph 	for (strptr = str; *strptr; strptr++) {
60717834Sralph 		if (*strptr == '\\')
60817834Sralph 			switch(*++strptr) {
60917834Sralph 			case 's':
61017834Sralph 				DEBUG(5, "BLANK\n", CNULL);
611*34167Srick 				strptr--;
61217834Sralph 				*strptr = ' ';
613*34167Srick 				strcpy(&strptr[1], &strptr[4]);
61417834Sralph 				break;
61517834Sralph 			default:
61617834Sralph 				strptr--;  /* back up to backslash */
61717834Sralph 				sscanf(strptr + 1,"%o", &cnt_char);
61817834Sralph 				DEBUG(6, "BACKSLASHED %02xH\n", cnt_char);
61917834Sralph 				*strptr = (char) (cnt_char);
62017834Sralph 				strcpy(&strptr[1], &strptr[4]);
62117834Sralph 			}
62217834Sralph 	}
62317834Sralph 
62425127Sbloom 	strptr = index(str, '~');
62525127Sbloom 	if (strptr != NULL) {
62625127Sbloom 		*strptr++ = '\0';
62725127Sbloom 		timo = atoi(strptr);
62825127Sbloom 		if (timo <= 0)
62925127Sbloom 			timo = MAXMSGTIME;
63025127Sbloom 	}
63125127Sbloom 
63217834Sralph 	if (setjmp(Sjbuf))
63317834Sralph 		return FAIL;
63413642Ssam 	signal(SIGALRM, alarmtr);
63525127Sbloom 	alarm(timo);
63625127Sbloom 	*rp = 0;
63713642Ssam 	while (notin(str, rdvec)) {
63825705Sbloom 		int c;
63917834Sralph 		if(AbortOn != NULL && !notin(AbortOn, rdvec)) {
64017834Sralph 			DEBUG(1, "Call aborted on '%s'\n", AbortOn);
64117834Sralph 			alarm(0);
64217834Sralph 			return ABORT;
64317834Sralph 		}
64413642Ssam 		kr = read(fn, &nextch, 1);
64513642Ssam 		if (kr <= 0) {
64613642Ssam 			alarm(0);
64713642Ssam 			DEBUG(4, "lost line kr - %d\n, ", kr);
64813642Ssam 			logent("LOGIN", "LOST LINE");
64917834Sralph 			return FAIL;
65013642Ssam 		}
65113642Ssam 		c = nextch & 0177;
65225705Sbloom 		if (c == '\0')
65325705Sbloom 			continue;
65425705Sbloom 		DEBUG(4, (isprint(c) || isspace(c)) ? "%c" : "\\%03o", c);
65525705Sbloom 		*rp++ = c;
65613642Ssam 		if (rp >= rdvec + MR) {
65717834Sralph 			register char *p;
65817834Sralph 			for (p = rdvec+MR/2; p < rp; p++)
65917834Sralph 				*(p-MR/2) = *p;
66017834Sralph 			rp -= MR/2;
66113642Ssam 		}
66213642Ssam 		*rp = '\0';
66313642Ssam 	}
66413642Ssam 	alarm(0);
66517834Sralph 	return SUCCESS;
66613642Ssam }
66713642Ssam 
66813642Ssam 
66913642Ssam /*
67013642Ssam  * Determine next file descriptor that would be allocated.
67113642Ssam  * This permits later closing of a file whose open was interrupted.
67213642Ssam  * It is a UNIX kernel problem, but it has to be handled.
67313642Ssam  * unc!smb (Steve Bellovin) probably first discovered it.
67413642Ssam  */
67513642Ssam getnextfd()
67613642Ssam {
67713642Ssam 	close(next_fd = open("/", 0));
67813642Ssam }
67913642Ssam 
68017834Sralph /*
68117834Sralph  *	send line of login sequence
68213642Ssam  *
68313642Ssam  *	return codes:  none
68413642Ssam  */
68513642Ssam sendthem(str, fn)
68613642Ssam register char *str;
68713642Ssam int fn;
68813642Ssam {
68913642Ssam 	register char *strptr;
69013642Ssam 	int i, n, cr = 1;
69117834Sralph 	register char c;
69213642Ssam 	static int p_init = 0;
69313642Ssam 
69425705Sbloom 	DEBUG(5, "send \"%s\"\n", str);
69513642Ssam 
69613642Ssam 	if (!p_init) {
69713642Ssam 		p_init++;
69833948Srick 		bld_partab(P_ZERO);
69913642Ssam 	}
70013642Ssam 
70113642Ssam 	if (prefix("BREAK", str)) {
70213642Ssam 		sscanf(&str[5], "%1d", &i);
70313642Ssam 		if (i <= 0 || i > 10)
70413642Ssam 			i = 3;
70513642Ssam 		/* send break */
70613642Ssam 		genbrk(fn, i);
70713642Ssam 		return;
70813642Ssam 	}
70913642Ssam 
71013642Ssam 	if (prefix("PAUSE", str)) {
71113642Ssam 		sscanf(&str[5], "%1d", &i);
71213642Ssam 		if (i <= 0 || i > 10)
71313642Ssam 			i = 3;
71413642Ssam 		/* pause for a while */
71513642Ssam 		sleep((unsigned)i);
71613642Ssam 		return;
71713642Ssam 	}
71813642Ssam 
71913642Ssam 	if (strcmp(str, "EOT") == SAME) {
72013642Ssam 		p_chwrite(fn, '\04');
72113642Ssam 		return;
72213642Ssam 	}
72313642Ssam 
72413642Ssam 	/* Send a '\n' */
72525705Sbloom 	if (strcmp(str, "LF") == SAME) {
72625705Sbloom 		p_chwrite(fn, '\n');
72725705Sbloom 		return;
72825705Sbloom 	}
72913642Ssam 
73013642Ssam 	/* Send a '\r' */
73125705Sbloom 	if (strcmp(str, "CR") == SAME) {
73225705Sbloom 		p_chwrite(fn, '\r');
73325705Sbloom 		return;
73425705Sbloom 	}
73513642Ssam 
73613642Ssam 	/* Set parity as needed */
73713642Ssam 	if (strcmp(str, "P_ZERO") == SAME) {
73813642Ssam 		bld_partab(P_ZERO);
73913642Ssam 		return;
74013642Ssam 	}
74113642Ssam 	if (strcmp(str, "P_ONE") == SAME) {
74213642Ssam 		bld_partab(P_ONE);
74313642Ssam 		return;
74413642Ssam 	}
74513642Ssam 	if (strcmp(str, "P_EVEN") == SAME) {
74613642Ssam 		bld_partab(P_EVEN);
74713642Ssam 		return;
74813642Ssam 	}
74913642Ssam 	if (strcmp(str, "P_ODD") == SAME) {
75013642Ssam 		bld_partab(P_ODD);
75113642Ssam 		return;
75213642Ssam 	}
75313642Ssam 
75413642Ssam 	/* If "", just send '\r' */
75517834Sralph 	if (strcmp(str, "\"\"") == SAME) {
75617834Sralph 		p_chwrite(fn, '\r');
75717834Sralph 		return;
75817834Sralph 	}
75917834Sralph 
76025705Sbloom 	strptr = str;
76125705Sbloom 	while ((c = *strptr++) != '\0') {
76217834Sralph 		if (c == '\\') {
76317834Sralph 			switch(*strptr++) {
76425705Sbloom 			case '\0':
76525705Sbloom 				DEBUG(5, "TRAILING BACKSLASH IGNORED\n", CNULL);
76625705Sbloom 				--strptr;
76725705Sbloom 				continue;
76817834Sralph 			case 's':
76917834Sralph 				DEBUG(5, "BLANK\n", CNULL);
77025705Sbloom 				c = ' ';
77117834Sralph 				break;
77217834Sralph 			case 'd':
77317834Sralph 				DEBUG(5, "DELAY\n", CNULL);
77417834Sralph 				sleep(1);
77517834Sralph 				continue;
77625705Sbloom 			case 'n':
77725705Sbloom 				DEBUG(5, "NEW LINE\n", CNULL);
77825705Sbloom 				c = '\n';
77925705Sbloom 				break;
78017834Sralph 			case 'r':
78117834Sralph 				DEBUG(5, "RETURN\n", CNULL);
78225705Sbloom 				c = '\r';
78317834Sralph 				break;
78417834Sralph 			case 'b':
78517834Sralph 				if (isdigit(*strptr)) {
78617834Sralph 					i = (*strptr++ - '0');
78717834Sralph 					if (i <= 0 || i > 10)
78817834Sralph 						i = 3;
78917834Sralph 				} else
79013642Ssam 					i = 3;
79117834Sralph 				/* send break */
79217834Sralph 				genbrk(fn, i);
79317834Sralph 				if (*strptr == '\0')
79417834Sralph 					cr = 0;
79513642Ssam 				continue;
79617834Sralph 			case 'c':
79717834Sralph 				if (*strptr == '\0') {
79817834Sralph 					DEBUG(5, "NO CR\n", CNULL);
79917834Sralph 					cr = 0;
80025705Sbloom 				} else
80125705Sbloom 					DEBUG(5, "NO CR - IGNORED NOT EOL\n", CNULL);
80213642Ssam 				continue;
80325705Sbloom #define isoctal(x)	((x >= '0') && (x <= '7'))
80417834Sralph 			default:
80525705Sbloom 				if (isoctal(strptr[-1])) {
80617834Sralph 					i = 0;
80717834Sralph 					n = 0;
80825705Sbloom 					--strptr;
80925705Sbloom 					while (isoctal(*strptr) && ++n <= 3)
81025705Sbloom 						i = i * 8 + (*strptr++ - '0');
81125705Sbloom 					DEBUG(5, "\\%o\n", i);
81217834Sralph 					p_chwrite(fn, (char)i);
81317834Sralph 					continue;
81417834Sralph 				}
81513642Ssam 			}
81625705Sbloom 		}
81725705Sbloom 		p_chwrite(fn, c);
81813642Ssam 	}
81913642Ssam 
82013642Ssam 	if (cr)
82113642Ssam 		p_chwrite(fn, '\r');
82213642Ssam 	return;
82313642Ssam }
82413642Ssam 
82513642Ssam p_chwrite(fd, c)
82613642Ssam int fd;
82717834Sralph char c;
82813642Ssam {
82917834Sralph 	c = par_tab[c&0177];
83017834Sralph 	if (write(fd, &c, 1) != 1) {
83117834Sralph 		logent(sys_errlist[errno], "BAD WRITE");
83217834Sralph 		longjmp(Cjbuf, 2);
83317834Sralph 	}
83413642Ssam }
83513642Ssam 
83613642Ssam /*
83713642Ssam  * generate parity table for use by p_chwrite.
83813642Ssam  */
83913642Ssam bld_partab(type)
84013642Ssam int type;
84113642Ssam {
84213642Ssam 	register int i, j, n;
84313642Ssam 
84413642Ssam 	for (i = 0; i < sizeof(par_tab); i++) {
84513642Ssam 		n = 0;
84613642Ssam 		for (j = i&(sizeof(par_tab)-1); j; j = (j-1)&j)
84713642Ssam 			n++;
84813642Ssam 		par_tab[i] = i;
84913642Ssam 		if (type == P_ONE
85013642Ssam 		 || (type == P_EVEN && (n&01) != 0)
85113642Ssam 		 || (type == P_ODD && (n&01) == 0))
85213642Ssam 			par_tab[i] |= sizeof(par_tab);
85313642Ssam 	}
85413642Ssam }
85513642Ssam 
85618619Sralph /*
85718619Sralph  *	check for occurrence of substring "sh"
85813642Ssam  *
85913642Ssam  *	return codes:
86013642Ssam  *		0  -  found the string
86113642Ssam  *		1  -  not in the string
86213642Ssam  */
86313642Ssam notin(sh, lg)
86413642Ssam register char *sh, *lg;
86513642Ssam {
86613642Ssam 	while (*lg != '\0') {
86713642Ssam 		if (wprefix(sh, lg))
86818619Sralph 			return 0;
86913642Ssam 		else
87013642Ssam 			lg++;
87113642Ssam 	}
87218619Sralph 	return 1;
87313642Ssam }
87413642Ssam 
87518619Sralph /*
87623596Sbloom  *	Allow multiple date specifications separated by ','.
87713642Ssam  */
87818619Sralph ifdate(p)
87918619Sralph register char *p;
88013642Ssam {
88125705Sbloom 	register char *np;
88218619Sralph 	register int ret, g;
88323596Sbloom 	int rtime, i;
88413642Ssam 
88523596Sbloom 	/*  pick up retry time for failures  */
88623596Sbloom 	/*  global variable Retrytime is set here  */
88723596Sbloom 	if ((np = index(p, ';')) == NULL) {
88823596Sbloom 		Retrytime = RETRYTIME;
88923596Sbloom 	} else {
89023596Sbloom 		i = sscanf(np+1, "%d", &rtime);
89123596Sbloom 		if (i < 1 || rtime < 0)
89223596Sbloom 			rtime = 5;
89323596Sbloom 		Retrytime  = rtime * 60;
89423596Sbloom 	}
89523596Sbloom 
89618619Sralph 	ret = FAIL;
89718619Sralph 	MaxGrade = '\0';
89818619Sralph 	do {
89923596Sbloom 		np = strpbrk(p, ",|");	/* prefer , but allow | for compat */
90023596Sbloom 		if (np)
90123596Sbloom 			*np = '\0';
90218619Sralph 		g = ifadate(p);
90318619Sralph 		DEBUG(11,"ifadate returns %o\n", g);
90418619Sralph 		if (g != FAIL) {
90518619Sralph 			ret = SUCCESS;
90618619Sralph 			if (g > MaxGrade)
90718619Sralph 				MaxGrade = g;
90818619Sralph 		}
90923596Sbloom 		if (np)
91023596Sbloom 			*np = ',';
91123596Sbloom 		p = np + 1;
91223596Sbloom 	} while (np);
91323596Sbloom 	if (MaxGrade == '\0')
91423596Sbloom 		MaxGrade = DefMaxGrade;
91518619Sralph 	return ret;
91613642Ssam }
91713642Ssam 
91818619Sralph /*
91918619Sralph  *	this routine will check a string (string)
92013642Ssam  *	like "MoTu0800-1730" to see if the present
92113642Ssam  *	time is within the given limits.
92213642Ssam  *	SIDE EFFECT - Retrytime is set
92313642Ssam  *
92413642Ssam  *	return codes:
92513642Ssam  *		0  -  not within limits
92613642Ssam  *		1  -  within limits
92713642Ssam  */
92813642Ssam 
92918619Sralph ifadate(string)
93018619Sralph char *string;
93113642Ssam {
93213642Ssam 	static char *days[]={
93313642Ssam 		"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
93413642Ssam 	};
93513642Ssam 	time_t clock;
93618619Sralph 	register char *s = string;
93717834Sralph 	int i, tl, th, tn, dayok=0;
93813642Ssam 	struct tm *localtime();
93913642Ssam 	struct tm *tp;
94018619Sralph 	char *p, MGrade;
94113642Ssam 
94223596Sbloom 	if ((p = index(s, '/')) == NULL)
94318619Sralph 		MGrade = DefMaxGrade;
94418619Sralph 	else
94518619Sralph 		MGrade = p[1];
94618619Sralph 
94713642Ssam 	time(&clock);
94813642Ssam 	tp = localtime(&clock);
94917834Sralph 	while (isascii(*s) && isalpha(*s)) {
95013642Ssam 		for (i = 0; days[i]; i++) {
95113642Ssam 			if (prefix(days[i], s))
95213642Ssam 				if (tp->tm_wday == i)
95313642Ssam 					dayok = 1;
95413642Ssam 		}
95513642Ssam 
95613642Ssam 		if (prefix("Wk", s))
95713642Ssam 			if (tp->tm_wday >= 1 && tp->tm_wday <= 5)
95813642Ssam 				dayok = 1;
95913642Ssam 		if (prefix("Any", s))
96013642Ssam 			dayok = 1;
96117834Sralph 		if (prefix("Evening", s)) {
96217834Sralph 			/* Sat or Sun */
96317834Sralph 			if (tp->tm_wday == 6 || tp->tm_wday == 0
96417834Sralph 				|| tp->tm_hour >= 17 || tp->tm_hour < 8)
96517834Sralph 					dayok = 1;
96617834Sralph 		}
96717834Sralph 		if (prefix("Night", s)) {
96817834Sralph 			if (tp->tm_wday == 6  /* Sat */
96918619Sralph 				|| tp->tm_hour >= 23 || tp->tm_hour < 8
97018619Sralph 					/* Sunday before 5pm */
97118619Sralph 				|| (tp->tm_wday == 0 && tp->tm_hour < 17))
97217834Sralph 					dayok = 1;
97317834Sralph 		}
97425705Sbloom 		if (prefix("NonPeak", s)) { /* For Tymnet and PC Pursuit */
97525705Sbloom 			/* Sat or Sun */
97625705Sbloom 			if (tp->tm_wday == 6 || tp->tm_wday == 0
97725705Sbloom 				|| tp->tm_hour >= 18 || tp->tm_hour < 7)
97825705Sbloom 					dayok = 1;
97925705Sbloom 		}
98013642Ssam 		s++;
98113642Ssam 	}
98213642Ssam 
98318619Sralph 	if (dayok == 0 && s != string)
98418619Sralph 		return FAIL;
98513642Ssam 	i = sscanf(s, "%d-%d", &tl, &th);
98618619Sralph   	if (i < 2)
98718619Sralph   		return MGrade;
98818619Sralph 	tn = tp->tm_hour * 100 + tp->tm_min;
98918619Sralph   	if (th < tl) { 		/* crosses midnight */
99018619Sralph   		if (tl <= tn || tn < th)
99118619Sralph   			return MGrade;
99225966Sbloom   	} else {
99317834Sralph 		if (tl <= tn && tn < th)
99418619Sralph 			return MGrade;
99525966Sbloom 	}
99618619Sralph 	return FAIL;
99713642Ssam }
99813642Ssam 
99918619Sralph /*
100018619Sralph  *	find first digit in string
100113642Ssam  *
100213642Ssam  *	return - pointer to first digit in string or end of string
100313642Ssam  */
100413642Ssam char *
100513642Ssam fdig(cp)
100613642Ssam register char *cp;
100713642Ssam {
100813642Ssam 	register char *c;
100913642Ssam 
101013642Ssam 	for (c = cp; *c; c++)
101113642Ssam 		if (*c >= '0' && *c <= '9')
101213642Ssam 			break;
101317834Sralph 	return c;
101413642Ssam }
101513642Ssam 
101613642Ssam /*
101713642Ssam  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
101813642Ssam  * Strings are compared as if they contain all capital letters.
101913642Ssam  */
102013642Ssam snccmp(s1, s2)
102113642Ssam register char *s1, *s2;
102213642Ssam {
102313642Ssam 	char c1, c2;
102413642Ssam 
102525127Sbloom 	if (islower(*s1))
102625127Sbloom 		c1 = toupper(*s1);
102725127Sbloom 	else
102825127Sbloom 		c1 = *s1;
102925127Sbloom 	if (islower(*s2))
103025127Sbloom 		c2 = toupper(*s2);
103125127Sbloom 	else
103225127Sbloom 		c2 = *s2;
103313642Ssam 
103413642Ssam 	while (c1 == c2) {
103525127Sbloom 		if (*s1++ == '\0')
103617834Sralph 			return 0;
103713642Ssam 		s2++;
103825127Sbloom 		if (islower(*s1))
103925127Sbloom 			c1 = toupper(*s1);
104025127Sbloom 		else
104125127Sbloom 			c1 = *s1;
104225127Sbloom 		if (islower(*s2))
104325127Sbloom 			c2 = toupper(*s2);
104425127Sbloom 		else
104525127Sbloom 			c2 = *s2;
104613642Ssam 	}
104717834Sralph 	return c1 - c2;
104813642Ssam }
104925127Sbloom 
105017834Sralph /*
105125127Sbloom  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
105225127Sbloom  * Strings are compared as if they contain all capital letters.
105325127Sbloom  */
105425127Sbloom sncncmp(s1, s2, n)
105525127Sbloom register char *s1, *s2;
105625127Sbloom register int n;
105725127Sbloom {
105825127Sbloom 	char c1, c2;
105925127Sbloom 
106025127Sbloom 	if (islower(*s1))
106125127Sbloom 		c1 = toupper(*s1);
106225127Sbloom 	else
106325127Sbloom 		c1 = *s1;
106425127Sbloom 	if (islower(*s2))
106525127Sbloom 		c2 = toupper(*s2);
106625127Sbloom 	else
106725127Sbloom 		c2 = *s2;
106825127Sbloom 
106925127Sbloom 	while ( --n >= 0 && c1 == c2) {
107025127Sbloom 		if (*s1++ == '\0')
107125127Sbloom 			return 0;
107225127Sbloom 		s2++;
107325127Sbloom 		if (islower(*s1))
107425127Sbloom 			c1 = toupper(*s1);
107525127Sbloom 		else
107625127Sbloom 			c1 = *s1;
107725127Sbloom 		if (islower(*s2))
107825127Sbloom 			c2 = toupper(*s2);
107925127Sbloom 		else
108025127Sbloom 			c2 = *s2;
108125127Sbloom 	}
108225127Sbloom 	return n<0 ? 0 : (c1 - c2);
108325127Sbloom }
108425127Sbloom /*
108517834Sralph  * do chat script
108617834Sralph  * occurs after local port is opened,
108717834Sralph  * before 'dialing' the other machine.
108817834Sralph  */
108917834Sralph dochat(dev, flds, fd)
109017834Sralph register struct Devices *dev;
109117834Sralph char *flds[];
109217834Sralph int fd;
109317834Sralph {
109417834Sralph 	register int i;
109517834Sralph 	register char *p;
109617834Sralph 	char bfr[sizeof(dev->D_argbfr)];
109717834Sralph 
109817834Sralph 	if (dev->D_numargs <= 5)
109917834Sralph 		return(0);
110017834Sralph 	DEBUG(4, "dochat called %d\n", dev->D_numargs);
110117834Sralph 	for (i = 0; i < dev->D_numargs-5; i++) {
110217834Sralph 		sprintf(bfr, dev->D_arg[D_CHAT+i], flds[F_PHONE]);
110317834Sralph 		if (strcmp(bfr, dev->D_arg[D_CHAT+i])) {
110417834Sralph 			p = malloc((unsigned)strlen(bfr)+1);
110517834Sralph 			if (p != NULL) {
110617834Sralph 				strcpy(p, bfr);
110717834Sralph 				dev->D_arg[D_CHAT+i] = p;
110817834Sralph 			}
110917834Sralph 		}
111017834Sralph 	}
111117834Sralph 	/* following is a kludge because login() arglist is a kludge */
111217834Sralph 	i = login(dev->D_numargs, &dev->D_arg[D_CHAT-5], fd);
111317834Sralph 	/*
111417834Sralph 	 * If login() last did a sendthem(), must pause so things can settle.
111517834Sralph 	 * But don't bother if chat failed.
111617834Sralph 	 */
111717834Sralph 	if (i == 0 && (dev->D_numargs&01))
111817834Sralph 		sleep(2);
111917834Sralph 	return(i);
112017834Sralph }
112125705Sbloom 
112225705Sbloom /*
112325705Sbloom  *	fix kill/echo/raw on line
112425705Sbloom  *
112525705Sbloom  *	return codes:  none
112625705Sbloom  */
112725705Sbloom fixmode(tty)
112825705Sbloom register int tty;
112925705Sbloom {
113025705Sbloom #ifdef	USG
113125705Sbloom 	struct termio ttbuf;
113225705Sbloom #else	!USG
113325705Sbloom 	struct sgttyb ttbuf;
113425705Sbloom #endif	!USG
113525705Sbloom 	register struct sg_spds *ps;
113625705Sbloom 	int speed;
113725705Sbloom 
113825705Sbloom 	if (IsTcpIp)
113925705Sbloom 		return;
114025705Sbloom #ifdef	USG
114125705Sbloom 	ioctl(tty, TCGETA, &ttbuf);
114225705Sbloom 	ttbuf.c_iflag = ttbuf.c_oflag = ttbuf.c_lflag = (ushort)0;
114325705Sbloom 	speed = ttbuf.c_cflag &= (CBAUD);
114425705Sbloom 	ttbuf.c_cflag |= (CS8|CREAD);
114525705Sbloom 	ttbuf.c_cc[VMIN] = 6;
114625705Sbloom 	ttbuf.c_cc[VTIME] = 1;
114725705Sbloom 	ioctl(tty, TCSETA, &ttbuf);
114825705Sbloom #else	!USG
114925705Sbloom 	ioctl(tty, TIOCGETP, &ttbuf);
115025705Sbloom 	ttbuf.sg_flags = (ANYP | RAW);
115125705Sbloom 	ioctl(tty, TIOCSETP, &ttbuf);
115225705Sbloom 	speed = ttbuf.sg_ispeed;
115325705Sbloom 	ioctl(tty, TIOCEXCL, STBNULL);
115425705Sbloom #endif	!USG
115525705Sbloom 
115625705Sbloom 	for (ps = spds; ps->sp_val; ps++)
115725705Sbloom 		if (ps->sp_name == speed) {
115825705Sbloom 			linebaudrate = ps->sp_val;
115925705Sbloom 			DEBUG(9,"Incoming baudrate is %d\n", linebaudrate);
116025705Sbloom 			return;
116125705Sbloom 		}
116233948Srick 	if (linebaudrate < 0) {
116333948Srick 		syslog(LOG_ERR, "unrecognized speed: %d", linebaudrate);
116433948Srick 		cleanup(FAIL);
116533948Srick 	}
116625705Sbloom }
1167