xref: /csrg-svn/usr.bin/uucp/uucico/conn.c (revision 46879)
133581Srick #ifndef lint
2*46879Sbostic static char sccsid[] = "@(#)conn.c	5.16	(Berkeley) 03/02/91";
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  */
48*46879Sbostic void
4913642Ssam alarmtr()
5013642Ssam {
5113642Ssam 	signal(SIGALRM, alarmtr);
5213642Ssam 	if (next_fd >= 0) {
5313642Ssam 		if (close(next_fd))
5413642Ssam 			logent("FAIL", "ACU LINE CLOSE");
5513642Ssam 		next_fd = -1;
5613642Ssam 	}
5713642Ssam 	longjmp(Sjbuf, 1);
5813642Ssam }
5913642Ssam 
6025705Sbloom /* This template is for seismo to call ihnp4
6125705Sbloom  * the 3 lines marked ---> will be overwritten for the appropriate city
6225705Sbloom  */
6325705Sbloom #define PCP_BAUD	3
6425705Sbloom #define PCP_PHONE	4
6533562Srick #define PCP_CITY	14
6633562Srick #define PCP_PASSWORD	16
6733562Srick #define PCP_RPHONE	20
6833562Srick #define NPCFIELDS	23
6925705Sbloom 
7025705Sbloom static char *PCFlds[] = {
7125705Sbloom 	"PC-PURSUIT",
7225705Sbloom 	"Any",
7325705Sbloom 	"ACU",
7425705Sbloom 	"1200",
7533562Srick 	CNULL,
7633562Srick 	CNULL,
7733562Srick 	"P_ZERO",	/* Telenet insists on zero parity */
7825705Sbloom 	"ABORT",
7933562Srick 	"BUSY",		/* Abort on Busy Signal */
8033562Srick 	CNULL,
8133562Srick 	"\\d\\d\\r\\d\\r",	/* Get telenet's attention */
8233562Srick 	"TERMINAL=~3-\r-TERM~3-\r-TERM~5", 	/* Terminal type ? */
8333562Srick 	"\\r",
8433562Srick 	"@",		/* telenet's prompt */
8533562Srick 	"D/DCWAS/21,telenetloginstring", /* overwritten later */
8633562Srick 	"PASSWORD",
8733562Srick 	CNULL,		/* telenet password */
8833562Srick 	"CONNECTED",	/* We're now talking to a Hayes in the remote city */
8933562Srick 	"ATZ",		/* Reset it */
9033562Srick 	"OK",
9133562Srick 	"ATDT6907171", /* overwritten */
9233562Srick 	"CONNECT",
9333562Srick 	"\\d\\r",		/* We're in !*/
9433562Srick 	CNULL,
9525705Sbloom };
9625705Sbloom 
9733562Srick static char PCP_brand[25];
9834167Srick int Dcf = -1;
9934167Srick char *Flds[MAXC/10];
10034167Srick char LineType[10];
10134167Srick extern int LocalOnly;
10225705Sbloom 
10318619Sralph /*
10418619Sralph  *	place a telephone call to system and login, etc.
10513642Ssam  *
10613642Ssam  *	return codes:
10713642Ssam  *		CF_SYSTEM: don't know system
10813642Ssam  *		CF_TIME: wrong time to call
10913642Ssam  *		CF_DIAL: call failed
11013642Ssam  *		CF_NODEV: no devices available to place call
11113642Ssam  *		CF_LOGIN: login/password dialog failed
11213642Ssam  *
11313642Ssam  *		>0  - file no.  -  connect ok
11413642Ssam  */
11513642Ssam conn(system)
11613642Ssam char *system;
11713642Ssam {
11825705Sbloom 	int nf;
11918619Sralph 	char info[MAXC], wkpre[NAMESIZE], file[NAMESIZE];
12013642Ssam 	register FILE *fsys;
12113642Ssam 	int fcode = 0;
12213642Ssam 
12313642Ssam 	nf = 0;
12413642Ssam 
12513642Ssam 	fsys = fopen(SYSFILE, "r");
12633948Srick 	if (fsys == NULL) {
12733948Srick 		syslog(LOG_ERR, "fopen(%s) failed: %m", SYSFILE);
12833948Srick 		cleanup(FAIL);
12933948Srick 	}
13013642Ssam 
13117834Sralph 	DEBUG(4, "finds (%s) called\n", system);
13225127Sbloom keeplooking:
13317834Sralph 	while((nf = finds(fsys, system, info, Flds)) > 0) {
13425966Sbloom 		strncpy(LineType, Flds[F_LINE], 10);
13517834Sralph 		if (LocalOnly) {
13625966Sbloom 			if (strcmp("TCP", LineType)
13725966Sbloom 				&& strcmp("DIR", LineType)
13825966Sbloom 				&& strcmp("LOCAL", LineType) ) {
13925705Sbloom 					fcode = CF_TIME;
14025705Sbloom 					continue;
14125705Sbloom 			}
14217834Sralph 		}
14323596Sbloom 		sprintf(wkpre, "%c.%.*s", CMDPRE, SYSNSIZE, Rmtname);
14418619Sralph 		if (!onesys && MaxGrade != DefMaxGrade &&
14525705Sbloom 			!iswrk(file, "chk", Spool, wkpre))  {
14625705Sbloom 				fcode = CF_TIME;
14725705Sbloom 				continue;
14825705Sbloom 		}
14925705Sbloom 		/* For GTE's PC Pursuit */
15025966Sbloom 		if (snccmp(LineType, PCP) == SAME) {
15125705Sbloom 			FILE *dfp;
15225705Sbloom 			int status;
15325705Sbloom 			static struct Devices dev;
15433562Srick 
15525705Sbloom 			dfp = fopen(DEVFILE, "r");
15633948Srick 			if (dfp == NULL) {
15733948Srick 				syslog(LOG_ERR, "fopen(%s) failed: %m",
15833948Srick 					DEVFILE);
15933948Srick 				cleanup(FAIL);
16033948Srick 			}
16125705Sbloom 			while ((status=rddev(dfp, &dev)) != FAIL
16225705Sbloom 				&& strcmp(PCP, dev.D_type) != SAME)
16325705Sbloom 					;
16425705Sbloom 			fclose(dfp);
16525705Sbloom 			if (status == FAIL)
16625705Sbloom 				continue;
16725705Sbloom 			if (mlock(PCP) == FAIL) {
16825705Sbloom 				fcode = CF_NODEV;
16925705Sbloom 				logent("DEVICE", "NO");
17025705Sbloom 				continue;
17125705Sbloom 			}
17225705Sbloom 			PCFlds[PCP_BAUD] = dev.D_class;
17325705Sbloom 			PCFlds[PCP_PHONE] = dev.D_calldev;
17433562Srick 			sprintf(PCFlds[PCP_CITY], "c d/%s%s,%s",
17533562Srick 				Flds[F_CLASS],
17633562Srick 				index(Flds[F_CLASS], '/') == NULL ? "/12" : "",
17733562Srick 				dev.D_arg[D_CHAT]);
17833562Srick 			PCFlds[PCP_PASSWORD] = dev.D_line;
17933562Srick 			strncpy(&PCFlds[PCP_RPHONE][4], Flds[F_PHONE], 7);
18025705Sbloom 			strncpy(PCP_brand, dev.D_brand, sizeof(PCP_brand));
18133562Srick 			if ((fcode = getto(PCFlds)) < 0) {
18233562Srick 				rmlock(PCP);
18325705Sbloom 				continue;
18433562Srick 			}
18525705Sbloom 			Dcf = fcode;
18625705Sbloom 			fcode = login(NPCFIELDS, PCFlds, Dcf);
18733562Srick 			if (fcode == SUCCESS)
18833562Srick 				break;
18933562Srick 			fcode = CF_DIAL;
19033562Srick 			rmlock(PCP);
19133562Srick 			/* end PC Pursuit */
19233562Srick 		} else if ((fcode = getto(Flds)) > 0)  {
19333562Srick 			Dcf = fcode;
19413642Ssam 			break;
19533562Srick 		}
19613642Ssam 	}
19713642Ssam 
19825127Sbloom 	if (nf <= 0) {
19925127Sbloom 		fclose(fsys);
20017834Sralph 		return fcode ? fcode : nf;
20125127Sbloom 	}
20213642Ssam 
20325705Sbloom 
20425705Sbloom 	if (fcode >= 0) {
20525705Sbloom 		DEBUG(4, "login %s\n", "called");
20634167Srick 		setproctitle("login");
20733562Srick 		fcode = login(nf, Flds, Dcf); }
20825705Sbloom 	if (fcode < 0) {
20913642Ssam 		clsacu();
21025705Sbloom 		if (fcode == ABORT) {
21125127Sbloom 			fcode = CF_DIAL;
21225127Sbloom 			goto  keeplooking;
21325127Sbloom 		} else {
21425127Sbloom 			fclose(fsys);
21523596Sbloom 			return CF_LOGIN;
21625127Sbloom 		}
21713642Ssam 	}
21825127Sbloom 	fclose(fsys);
21925705Sbloom 	fioclex(Dcf);
22025705Sbloom 	return Dcf;
22113642Ssam }
22213642Ssam 
22334167Srick int nulldev();
22434167Srick int (*CU_end)() = nulldev;
22534167Srick 
22625127Sbloom /*
22725127Sbloom  *	connect to remote machine
22813642Ssam  *
22913642Ssam  *	return codes:
23013642Ssam  *		>0  -  file number - ok
23113642Ssam  *		FAIL  -  failed
23213642Ssam  */
23313642Ssam getto(flds)
23413642Ssam register char *flds[];
23513642Ssam {
23613642Ssam 	register struct condev *cd;
23734094Sbostic 	int diropn();
23825127Sbloom 	char *line;
23913642Ssam 
24017834Sralph 	DEBUG(4, "getto: call no. %s ", flds[F_PHONE]);
24113642Ssam 	DEBUG(4, "for sys %s\n", flds[F_NAME]);
24213642Ssam 
24325127Sbloom 	if (snccmp(flds[F_LINE], "LOCAL") == SAME)
24425127Sbloom 		line = "ACU";
24525127Sbloom 	else
24625127Sbloom 		line = flds[F_LINE];
24725127Sbloom #ifdef DIALINOUT
24825127Sbloom 	if (snccmp(line, "ACU") != SAME)
24925127Sbloom 		reenable();
25025127Sbloom #endif DIALINOUT
25113642Ssam 	CU_end = nulldev;
25225705Sbloom 	if (snccmp(line, PCP) == SAME) {
25325705Sbloom 		for(cd = condevs; cd->CU_meth != NULL; cd++) {
25425705Sbloom 			if (snccmp(PCP_brand, cd->CU_brand) == SAME) {
25525705Sbloom 				CU_end = cd->CU_clos;
25625705Sbloom 				return diropn(flds);
25725705Sbloom 			}
25813642Ssam 		}
25925705Sbloom 		logent(PCP_brand, "UNSUPPORTED ACU TYPE");
26025705Sbloom 	} else {
26125705Sbloom 		for (cd = condevs; cd->CU_meth != NULL; cd++) {
26225705Sbloom 			if (snccmp(cd->CU_meth, line) == SAME) {
26325705Sbloom 				DEBUG(4, "Using %s to call\n", cd->CU_meth);
26425705Sbloom 				return (*(cd->CU_gen))(flds);
26525705Sbloom 			}
26625705Sbloom 		}
26725705Sbloom 		DEBUG(1, "Can't find %s, assuming DIR\n", flds[F_LINE]);
26813642Ssam 	}
26917834Sralph 	return diropn(flds);	/* search failed, so use direct */
27017834Sralph }
27113642Ssam 
27225127Sbloom /*
27325127Sbloom  *	close call unit
27413642Ssam  *
27513642Ssam  *	return codes:  none
27613642Ssam  */
27713642Ssam clsacu()
27813642Ssam {
27917834Sralph 	/* make *sure* Dcf is no longer exclusive.
28017834Sralph 	 * Otherwise dual call-in/call-out modems could get stuck.
28117834Sralph 	 * Unfortunately, doing this here is not ideal, but it is the
28217834Sralph 	 * easiest place to put the call.
28317834Sralph 	 * Hopefully everyone honors the LCK protocol, of course
28417834Sralph 	 */
28525705Sbloom #ifdef	TIOCNXCL
28623596Sbloom 	if (!IsTcpIp && Dcf >= 0 && ioctl(Dcf, TIOCNXCL, STBNULL) < 0)
28723596Sbloom 		DEBUG(5, "clsacu ioctl %s\n", sys_errlist[errno]);
28817834Sralph #endif
28917834Sralph 	if  (setjmp(Sjbuf))
29017834Sralph 		logent(Rmtname, "CLOSE TIMEOUT");
29117834Sralph 	else {
29217834Sralph 		signal(SIGALRM, alarmtr);
29317834Sralph 		alarm(20);
29417834Sralph 		(*(CU_end))(Dcf);
29517834Sralph 		alarm(0);
29617834Sralph 	}
29713642Ssam 	if (close(Dcf) == 0) {
29813642Ssam 		DEBUG(4, "fd %d NOT CLOSED by CU_clos\n", Dcf);
29913642Ssam 		logent("clsacu", "NOT CLOSED by CU_clos");
30013642Ssam 	}
30113642Ssam 	Dcf = -1;
30213642Ssam 	CU_end = nulldev;
30313642Ssam }
30413642Ssam 
30525127Sbloom /*
30625127Sbloom  *	expand phone number for given prefix and number
30713642Ssam  */
30813642Ssam exphone(in, out)
30913642Ssam register char *in, *out;
31013642Ssam {
31113642Ssam 	FILE *fn;
31213642Ssam 	char pre[MAXPH], npart[MAXPH], tpre[MAXPH], p[MAXPH];
31313642Ssam 	char buf[BUFSIZ];
31413642Ssam 	register char *s1;
31513642Ssam 
31617834Sralph 	if (!isascii(*in) || !isalpha(*in)) {
31713642Ssam 		strcpy(out, in);
31813642Ssam 		return;
31913642Ssam 	}
32013642Ssam 
32113642Ssam 	s1=pre;
32217834Sralph 	while (isascii(*in) && isalpha(*in))
32313642Ssam 		*s1++ = *in++;
32413642Ssam 	*s1 = '\0';
32513642Ssam 	s1 = npart;
32613642Ssam 	while (*in != '\0')
32713642Ssam 		*s1++ = *in++;
32813642Ssam 	*s1 = '\0';
32913642Ssam 
33013642Ssam 	tpre[0] = '\0';
33113642Ssam 	if ((fn = fopen(DIALFILE, "r")) == NULL)
33213642Ssam 		DEBUG(2, "CAN'T OPEN %s\n", DIALFILE);
33313642Ssam 	else {
33413642Ssam 		while (cfgets(buf, BUFSIZ, fn)) {
33517834Sralph 			if (sscanf(buf, "%s%s", p, tpre) != 2)
33617834Sralph 				continue;
33713642Ssam 			if (strcmp(p, pre) == SAME)
33813642Ssam 				goto found;
33913642Ssam 			tpre[0] = '\0';
34013642Ssam 		}
34113642Ssam 		DEBUG(2, "CAN'T FIND dialcodes prefix '%s'\n", pre);
34213642Ssam 	found:;
34313642Ssam 		fclose(fn);
34413642Ssam 	}
34513642Ssam 
34613642Ssam 	strcpy(out, tpre);
34713642Ssam 	strcat(out, npart);
34813642Ssam }
34913642Ssam 
35018619Sralph /*
35118619Sralph  *	read and decode a line from device file
35213642Ssam  *
35313642Ssam  *	return code - FAIL at end-of file; 0 otherwise
35413642Ssam  */
35513642Ssam rddev(fp, dev)
35613642Ssam register struct Devices *dev;
35713642Ssam FILE *fp;
35813642Ssam {
35917834Sralph 	register int na;
36013642Ssam 
36117834Sralph 	if (!cfgets(dev->D_argbfr, sizeof(dev->D_argbfr), fp))
36217834Sralph 		return FAIL;
36317834Sralph 	na = getargs(dev->D_argbfr, dev->D_arg, 20);
36433948Srick 	if (na < 4) {
36533948Srick 		syslog(LOG_ERR, "%s: invalid device entry", dev->D_argbfr);
36633948Srick 		cleanup(FAIL);
36733948Srick 	}
36817834Sralph 	if (na == 4) {
36917834Sralph 		dev->D_brand = "";
37017834Sralph 		na++;
37117834Sralph 	}
37213642Ssam 	dev->D_speed = atoi(fdig(dev->D_class));
37317834Sralph 	dev->D_numargs = na;
37417834Sralph 	return 0;
37513642Ssam }
37613642Ssam 
37718619Sralph /*
37818619Sralph  *	set system attribute vector
37913642Ssam  *
38013642Ssam  *	return codes:
38113642Ssam  *		>0  -  number of arguments in vector - succeeded
38213642Ssam  *		CF_SYSTEM  -  system name not found
38313642Ssam  *		CF_TIME  -  wrong time to call
38413642Ssam  */
38513642Ssam finds(fsys, sysnam, info, flds)
38613642Ssam char *sysnam, info[], *flds[];
38713642Ssam FILE *fsys;
38813642Ssam {
38913642Ssam 	int na;
39013642Ssam 	int fcode = 0;
39113642Ssam 
39213642Ssam 	/* format of fields
39313642Ssam 	 *	0 name;
39413642Ssam 	 *	1 time
39513642Ssam 	 *	2 acu/hardwired
39613642Ssam 	 *	3 speed
39713642Ssam 	 *	etc
39813642Ssam 	 */
39913642Ssam 	while (cfgets(info, MAXC, fsys) != NULL) {
40017834Sralph 		na = getargs(info, flds, MAXC/10);
40123596Sbloom 		if (strncmp(sysnam, flds[F_NAME], MAXBASENAME) != SAME)
40213642Ssam 			continue;
40318619Sralph 		if (ifdate(flds[F_TIME]) != FAIL)
40413642Ssam 			/*  found a good entry  */
40517834Sralph 			return na;
40613642Ssam 		DEBUG(2, "Wrong time ('%s') to call\n", flds[F_TIME]);
40713642Ssam 		fcode = CF_TIME;
40813642Ssam 	}
40917834Sralph 	return fcode ? fcode : CF_SYSTEM;
41013642Ssam }
41113642Ssam 
41218619Sralph /*
41318619Sralph  *	do login conversation
41413642Ssam  *
41523596Sbloom  *	return codes:  SUCCESS  |  FAIL
41613642Ssam  */
41713642Ssam login(nf, flds, fn)
41813642Ssam register char *flds[];
41913642Ssam int nf, fn;
42013642Ssam {
42113642Ssam 	register char *want, *altern;
42213642Ssam 	int k, ok;
42313642Ssam 
42433948Srick 	if (nf < 4) {
42533948Srick 		syslog(LOG_ERR, "Too few log fields: %d", nf);
42633948Srick 		cleanup(FAIL);
42733948Srick 	}
42817834Sralph 	if (setjmp(Cjbuf))
42917834Sralph 		return FAIL;
43017834Sralph 	AbortOn = NULL;
43113642Ssam 	for (k = F_LOGIN; k < nf; k += 2) {
43213642Ssam 		want = flds[k];
43333562Srick 		if (want == NULL)
43433562Srick 			want = "";
43513642Ssam 		ok = FAIL;
43617834Sralph 		while (ok != SUCCESS) {
43713642Ssam 			altern = index(want, '-');
43813642Ssam 			if (altern != NULL)
43913642Ssam 				*altern++ = '\0';
44017834Sralph 			if (strcmp(want, "ABORT") == 0) {
44117834Sralph 				AbortOn = flds[k+1];
44217834Sralph 				DEBUG(4, "ABORT ON: %s\n", AbortOn);
44317834Sralph 				goto nextfield;
44417834Sralph 			}
44525705Sbloom 			DEBUG(4, "wanted \"%s\"\n", want);
44613642Ssam 			ok = expect(want, fn);
44717834Sralph 			DEBUG(4, "got: %s\n", ok ? "?" : "that");
44817834Sralph 			if (ok == FAIL) {
44917834Sralph 				if (altern == NULL) {
45017834Sralph 					logent("LOGIN", _FAILED);
45117834Sralph 					return FAIL;
45217834Sralph 				}
45317834Sralph 				want = index(altern, '-');
45417834Sralph 				if (want != NULL)
45517834Sralph 					*want++ = '\0';
45617834Sralph 				sendthem(altern, fn);
45717834Sralph 			} else
45817834Sralph 				if (ok == ABORT) {
45933562Srick 					char sbuf[MAXFULLNAME];
46033562Srick 					sprintf(sbuf, "LOGIN ABORTED on \"%s\"",						AbortOn);
46133562Srick 					logent(sbuf, _FAILED);
46223596Sbloom 					return ABORT;
46317834Sralph 				}
46413642Ssam 		}
46517834Sralph 		sleep(1);
46613642Ssam 		if (k+1 < nf)
46713642Ssam 			sendthem(flds[k+1], fn);
46817834Sralph nextfield: ;
46913642Ssam 	}
47017834Sralph 	return SUCCESS;
47113642Ssam }
47213642Ssam 
47313642Ssam 
47417834Sralph /* conditional table generation to support odd speeds */
47513642Ssam struct sg_spds {int sp_val, sp_name;} spds[] = {
47613642Ssam #ifdef B50
47713642Ssam 	{  50,	 B50},
47813642Ssam #endif
47913642Ssam #ifdef B75
48013642Ssam 	{  75,	 B75},
48113642Ssam #endif
48213642Ssam #ifdef B110
48313642Ssam 	{ 110,	B110},
48413642Ssam #endif
48513642Ssam #ifdef B150
48613642Ssam 	{ 150,	B150},
48713642Ssam #endif
48813642Ssam #ifdef B200
48913642Ssam 	{ 200,	B200},
49013642Ssam #endif
49113642Ssam #ifdef B300
49213642Ssam 	{ 300,  B300},
49313642Ssam #endif
49413642Ssam #ifdef B600
49513642Ssam 	{600,	B600},
49613642Ssam #endif
49713642Ssam #ifdef B1200
49813642Ssam 	{1200, B1200},
49913642Ssam #endif
50013642Ssam #ifdef B1800
50113642Ssam 	{1800, B1800},
50213642Ssam #endif
50313642Ssam #ifdef B2000
50413642Ssam 	{2000, B2000},
50513642Ssam #endif
50613642Ssam #ifdef B2400
50713642Ssam 	{2400, B2400},
50813642Ssam #endif
50913642Ssam #ifdef B3600
51013642Ssam 	{3600, B3600},
51113642Ssam #endif
51213642Ssam #ifdef B4800
51313642Ssam 	{4800, B4800},
51413642Ssam #endif
51513642Ssam #ifdef B7200
51613642Ssam 	{7200, B7200},
51713642Ssam #endif
51813642Ssam #ifdef B9600
51913642Ssam 	{9600, B9600},
52013642Ssam #endif
52113642Ssam #ifdef B19200
52217834Sralph 	{19200, B19200},
52313642Ssam #endif
52417834Sralph #ifdef EXTA
52517834Sralph 	{19200, EXTA},
52617834Sralph #endif
52713642Ssam 	{0, 0}
52813642Ssam };
52913642Ssam 
53018619Sralph /*
53118619Sralph  *	set speed/echo/mode...
53213642Ssam  *
53313642Ssam  *	return codes:  none
53413642Ssam  */
53513642Ssam fixline(tty, spwant)
53613642Ssam int tty, spwant;
53713642Ssam {
53817834Sralph #ifdef	USG
53913642Ssam 	struct termio ttbuf;
54017834Sralph #else	!USG
54113642Ssam 	struct sgttyb ttbuf;
54217834Sralph #endif !USG
54313642Ssam 	register struct sg_spds *ps;
54413642Ssam 	int speed = -1;
54513642Ssam 
54613642Ssam 	for (ps = spds; ps->sp_val; ps++)
54713642Ssam 		if (ps->sp_val == spwant)
54813642Ssam 			speed = ps->sp_name;
54933948Srick 	if (speed < 0) {
55033948Srick 		syslog(LOG_ERR, "unrecognized speed: %d", speed);
55133948Srick 		cleanup(FAIL);
55233948Srick 	}
55317834Sralph #ifdef	USG
55423596Sbloom 	if (ioctl(tty, TCGETA, &ttbuf) < 0)
55523596Sbloom 		return FAIL;
55613642Ssam 	/* ttbuf.sg_flags = (ANYP|RAW);
55713642Ssam 	ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed; */
55813642Ssam 	ttbuf.c_iflag = (ushort)0;
55913642Ssam 	ttbuf.c_oflag = (ushort)0;
56013642Ssam 	ttbuf.c_cflag = (speed|CS8|HUPCL|CREAD);
56113642Ssam 	ttbuf.c_lflag = (ushort)0;
56213642Ssam 	ttbuf.c_cc[VMIN] = 6;
56313642Ssam 	ttbuf.c_cc[VTIME] = 1;
56423596Sbloom 	if (ioctl(tty, TCSETA, &ttbuf) < 0)
56523596Sbloom 		return FAIL;
56617834Sralph #else	!USG
56723596Sbloom 	if (ioctl(tty, TIOCGETP, &ttbuf) < 0)
56823596Sbloom 		return FAIL;
56913642Ssam 	ttbuf.sg_flags = (ANYP|RAW);
57013642Ssam 	ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed;
57123596Sbloom 	if (ioctl(tty, TIOCSETP, &ttbuf) < 0)
57223596Sbloom 		return FAIL;
57313642Ssam #endif
57417834Sralph #ifndef	USG
57523596Sbloom 	if (ioctl(tty, TIOCHPCL, STBNULL) < 0)
57623596Sbloom 		return FAIL;
57723596Sbloom 	if (ioctl(tty, TIOCEXCL, STBNULL) < 0)
57823596Sbloom 		return FAIL;
57913642Ssam #endif
58017834Sralph 	linebaudrate = spwant;
58123596Sbloom 	return SUCCESS;
58213642Ssam }
58313642Ssam 
58417834Sralph #define MR 100
58513642Ssam 
58618619Sralph /*
58718619Sralph  *	look for expected string
58813642Ssam  *
58913642Ssam  *	return codes:
59013642Ssam  *		0  -  found
59113642Ssam  *		FAIL  -  lost line or too many characters read
59213642Ssam  *		some character  -  timed out
59313642Ssam  */
59413642Ssam expect(str, fn)
59513642Ssam register char *str;
59613642Ssam int fn;
59713642Ssam {
59814592Skarels 	char rdvec[MR];
59917834Sralph 	register char *rp = rdvec, *strptr;
60017834Sralph 	int kr, cnt_char;
60113642Ssam 	char nextch;
60225127Sbloom 	int timo = MAXMSGTIME;
60313642Ssam 
60417834Sralph 	if (*str == '\0' || strcmp(str, "\"\"") == SAME)
60517834Sralph 		return SUCCESS;
60617834Sralph 	/* Cleanup str, convert \0xx strings to one char  */
60717834Sralph 	for (strptr = str; *strptr; strptr++) {
60817834Sralph 		if (*strptr == '\\')
60917834Sralph 			switch(*++strptr) {
61017834Sralph 			case 's':
61117834Sralph 				DEBUG(5, "BLANK\n", CNULL);
61234167Srick 				strptr--;
61317834Sralph 				*strptr = ' ';
61434167Srick 				strcpy(&strptr[1], &strptr[4]);
61517834Sralph 				break;
61617834Sralph 			default:
61717834Sralph 				strptr--;  /* back up to backslash */
61817834Sralph 				sscanf(strptr + 1,"%o", &cnt_char);
61917834Sralph 				DEBUG(6, "BACKSLASHED %02xH\n", cnt_char);
62017834Sralph 				*strptr = (char) (cnt_char);
62117834Sralph 				strcpy(&strptr[1], &strptr[4]);
62217834Sralph 			}
62317834Sralph 	}
62417834Sralph 
62525127Sbloom 	strptr = index(str, '~');
62625127Sbloom 	if (strptr != NULL) {
62725127Sbloom 		*strptr++ = '\0';
62825127Sbloom 		timo = atoi(strptr);
62925127Sbloom 		if (timo <= 0)
63025127Sbloom 			timo = MAXMSGTIME;
63125127Sbloom 	}
63225127Sbloom 
63317834Sralph 	if (setjmp(Sjbuf))
63417834Sralph 		return FAIL;
63513642Ssam 	signal(SIGALRM, alarmtr);
63625127Sbloom 	alarm(timo);
63725127Sbloom 	*rp = 0;
63813642Ssam 	while (notin(str, rdvec)) {
63925705Sbloom 		int c;
64017834Sralph 		if(AbortOn != NULL && !notin(AbortOn, rdvec)) {
64117834Sralph 			DEBUG(1, "Call aborted on '%s'\n", AbortOn);
64217834Sralph 			alarm(0);
64317834Sralph 			return ABORT;
64417834Sralph 		}
64513642Ssam 		kr = read(fn, &nextch, 1);
64613642Ssam 		if (kr <= 0) {
64713642Ssam 			alarm(0);
64813642Ssam 			DEBUG(4, "lost line kr - %d\n, ", kr);
64913642Ssam 			logent("LOGIN", "LOST LINE");
65017834Sralph 			return FAIL;
65113642Ssam 		}
65213642Ssam 		c = nextch & 0177;
65325705Sbloom 		if (c == '\0')
65425705Sbloom 			continue;
65525705Sbloom 		DEBUG(4, (isprint(c) || isspace(c)) ? "%c" : "\\%03o", c);
65625705Sbloom 		*rp++ = c;
65713642Ssam 		if (rp >= rdvec + MR) {
65817834Sralph 			register char *p;
65917834Sralph 			for (p = rdvec+MR/2; p < rp; p++)
66017834Sralph 				*(p-MR/2) = *p;
66117834Sralph 			rp -= MR/2;
66213642Ssam 		}
66313642Ssam 		*rp = '\0';
66413642Ssam 	}
66513642Ssam 	alarm(0);
66617834Sralph 	return SUCCESS;
66713642Ssam }
66813642Ssam 
66913642Ssam 
67013642Ssam /*
67113642Ssam  * Determine next file descriptor that would be allocated.
67213642Ssam  * This permits later closing of a file whose open was interrupted.
67313642Ssam  * It is a UNIX kernel problem, but it has to be handled.
67413642Ssam  * unc!smb (Steve Bellovin) probably first discovered it.
67513642Ssam  */
67613642Ssam getnextfd()
67713642Ssam {
67813642Ssam 	close(next_fd = open("/", 0));
67913642Ssam }
68013642Ssam 
68117834Sralph /*
68217834Sralph  *	send line of login sequence
68313642Ssam  *
68413642Ssam  *	return codes:  none
68513642Ssam  */
68613642Ssam sendthem(str, fn)
68713642Ssam register char *str;
68813642Ssam int fn;
68913642Ssam {
69013642Ssam 	register char *strptr;
69113642Ssam 	int i, n, cr = 1;
69217834Sralph 	register char c;
69313642Ssam 	static int p_init = 0;
69413642Ssam 
69525705Sbloom 	DEBUG(5, "send \"%s\"\n", str);
69613642Ssam 
69713642Ssam 	if (!p_init) {
69813642Ssam 		p_init++;
69933948Srick 		bld_partab(P_ZERO);
70013642Ssam 	}
70113642Ssam 
70213642Ssam 	if (prefix("BREAK", str)) {
70313642Ssam 		sscanf(&str[5], "%1d", &i);
70413642Ssam 		if (i <= 0 || i > 10)
70513642Ssam 			i = 3;
70613642Ssam 		/* send break */
70713642Ssam 		genbrk(fn, i);
70813642Ssam 		return;
70913642Ssam 	}
71013642Ssam 
71113642Ssam 	if (prefix("PAUSE", str)) {
71213642Ssam 		sscanf(&str[5], "%1d", &i);
71313642Ssam 		if (i <= 0 || i > 10)
71413642Ssam 			i = 3;
71513642Ssam 		/* pause for a while */
71613642Ssam 		sleep((unsigned)i);
71713642Ssam 		return;
71813642Ssam 	}
71913642Ssam 
72013642Ssam 	if (strcmp(str, "EOT") == SAME) {
72113642Ssam 		p_chwrite(fn, '\04');
72213642Ssam 		return;
72313642Ssam 	}
72413642Ssam 
72513642Ssam 	/* Send a '\n' */
72625705Sbloom 	if (strcmp(str, "LF") == SAME) {
72725705Sbloom 		p_chwrite(fn, '\n');
72825705Sbloom 		return;
72925705Sbloom 	}
73013642Ssam 
73113642Ssam 	/* Send a '\r' */
73225705Sbloom 	if (strcmp(str, "CR") == SAME) {
73325705Sbloom 		p_chwrite(fn, '\r');
73425705Sbloom 		return;
73525705Sbloom 	}
73613642Ssam 
73713642Ssam 	/* Set parity as needed */
73813642Ssam 	if (strcmp(str, "P_ZERO") == SAME) {
73913642Ssam 		bld_partab(P_ZERO);
74013642Ssam 		return;
74113642Ssam 	}
74213642Ssam 	if (strcmp(str, "P_ONE") == SAME) {
74313642Ssam 		bld_partab(P_ONE);
74413642Ssam 		return;
74513642Ssam 	}
74613642Ssam 	if (strcmp(str, "P_EVEN") == SAME) {
74713642Ssam 		bld_partab(P_EVEN);
74813642Ssam 		return;
74913642Ssam 	}
75013642Ssam 	if (strcmp(str, "P_ODD") == SAME) {
75113642Ssam 		bld_partab(P_ODD);
75213642Ssam 		return;
75313642Ssam 	}
75413642Ssam 
75513642Ssam 	/* If "", just send '\r' */
75617834Sralph 	if (strcmp(str, "\"\"") == SAME) {
75717834Sralph 		p_chwrite(fn, '\r');
75817834Sralph 		return;
75917834Sralph 	}
76017834Sralph 
76125705Sbloom 	strptr = str;
76225705Sbloom 	while ((c = *strptr++) != '\0') {
76317834Sralph 		if (c == '\\') {
76417834Sralph 			switch(*strptr++) {
76525705Sbloom 			case '\0':
76625705Sbloom 				DEBUG(5, "TRAILING BACKSLASH IGNORED\n", CNULL);
76725705Sbloom 				--strptr;
76825705Sbloom 				continue;
76917834Sralph 			case 's':
77017834Sralph 				DEBUG(5, "BLANK\n", CNULL);
77125705Sbloom 				c = ' ';
77217834Sralph 				break;
77317834Sralph 			case 'd':
77417834Sralph 				DEBUG(5, "DELAY\n", CNULL);
77517834Sralph 				sleep(1);
77617834Sralph 				continue;
77725705Sbloom 			case 'n':
77825705Sbloom 				DEBUG(5, "NEW LINE\n", CNULL);
77925705Sbloom 				c = '\n';
78025705Sbloom 				break;
78117834Sralph 			case 'r':
78217834Sralph 				DEBUG(5, "RETURN\n", CNULL);
78325705Sbloom 				c = '\r';
78417834Sralph 				break;
78517834Sralph 			case 'b':
78617834Sralph 				if (isdigit(*strptr)) {
78717834Sralph 					i = (*strptr++ - '0');
78817834Sralph 					if (i <= 0 || i > 10)
78917834Sralph 						i = 3;
79017834Sralph 				} else
79113642Ssam 					i = 3;
79217834Sralph 				/* send break */
79317834Sralph 				genbrk(fn, i);
79417834Sralph 				if (*strptr == '\0')
79517834Sralph 					cr = 0;
79613642Ssam 				continue;
79717834Sralph 			case 'c':
79817834Sralph 				if (*strptr == '\0') {
79917834Sralph 					DEBUG(5, "NO CR\n", CNULL);
80017834Sralph 					cr = 0;
80125705Sbloom 				} else
80225705Sbloom 					DEBUG(5, "NO CR - IGNORED NOT EOL\n", CNULL);
80313642Ssam 				continue;
80425705Sbloom #define isoctal(x)	((x >= '0') && (x <= '7'))
80517834Sralph 			default:
80625705Sbloom 				if (isoctal(strptr[-1])) {
80717834Sralph 					i = 0;
80817834Sralph 					n = 0;
80925705Sbloom 					--strptr;
81025705Sbloom 					while (isoctal(*strptr) && ++n <= 3)
81125705Sbloom 						i = i * 8 + (*strptr++ - '0');
81225705Sbloom 					DEBUG(5, "\\%o\n", i);
81317834Sralph 					p_chwrite(fn, (char)i);
81417834Sralph 					continue;
81517834Sralph 				}
81613642Ssam 			}
81725705Sbloom 		}
81825705Sbloom 		p_chwrite(fn, c);
81913642Ssam 	}
82013642Ssam 
82113642Ssam 	if (cr)
82213642Ssam 		p_chwrite(fn, '\r');
82313642Ssam 	return;
82413642Ssam }
82513642Ssam 
82613642Ssam p_chwrite(fd, c)
82713642Ssam int fd;
82817834Sralph char c;
82913642Ssam {
83017834Sralph 	c = par_tab[c&0177];
83117834Sralph 	if (write(fd, &c, 1) != 1) {
83217834Sralph 		logent(sys_errlist[errno], "BAD WRITE");
83317834Sralph 		longjmp(Cjbuf, 2);
83417834Sralph 	}
83513642Ssam }
83613642Ssam 
83713642Ssam /*
83813642Ssam  * generate parity table for use by p_chwrite.
83913642Ssam  */
84013642Ssam bld_partab(type)
84113642Ssam int type;
84213642Ssam {
84313642Ssam 	register int i, j, n;
84413642Ssam 
84513642Ssam 	for (i = 0; i < sizeof(par_tab); i++) {
84613642Ssam 		n = 0;
84713642Ssam 		for (j = i&(sizeof(par_tab)-1); j; j = (j-1)&j)
84813642Ssam 			n++;
84913642Ssam 		par_tab[i] = i;
85013642Ssam 		if (type == P_ONE
85113642Ssam 		 || (type == P_EVEN && (n&01) != 0)
85213642Ssam 		 || (type == P_ODD && (n&01) == 0))
85313642Ssam 			par_tab[i] |= sizeof(par_tab);
85413642Ssam 	}
85513642Ssam }
85613642Ssam 
85718619Sralph /*
85818619Sralph  *	check for occurrence of substring "sh"
85913642Ssam  *
86013642Ssam  *	return codes:
86113642Ssam  *		0  -  found the string
86213642Ssam  *		1  -  not in the string
86313642Ssam  */
86413642Ssam notin(sh, lg)
86513642Ssam register char *sh, *lg;
86613642Ssam {
86713642Ssam 	while (*lg != '\0') {
86813642Ssam 		if (wprefix(sh, lg))
86918619Sralph 			return 0;
87013642Ssam 		else
87113642Ssam 			lg++;
87213642Ssam 	}
87318619Sralph 	return 1;
87413642Ssam }
87513642Ssam 
87618619Sralph /*
87723596Sbloom  *	Allow multiple date specifications separated by ','.
87813642Ssam  */
87918619Sralph ifdate(p)
88018619Sralph register char *p;
88113642Ssam {
88225705Sbloom 	register char *np;
88318619Sralph 	register int ret, g;
88423596Sbloom 	int rtime, i;
88513642Ssam 
88623596Sbloom 	/*  pick up retry time for failures  */
88723596Sbloom 	/*  global variable Retrytime is set here  */
88823596Sbloom 	if ((np = index(p, ';')) == NULL) {
88923596Sbloom 		Retrytime = RETRYTIME;
89023596Sbloom 	} else {
89123596Sbloom 		i = sscanf(np+1, "%d", &rtime);
89223596Sbloom 		if (i < 1 || rtime < 0)
89323596Sbloom 			rtime = 5;
89423596Sbloom 		Retrytime  = rtime * 60;
89523596Sbloom 	}
89623596Sbloom 
89718619Sralph 	ret = FAIL;
89818619Sralph 	MaxGrade = '\0';
89918619Sralph 	do {
90023596Sbloom 		np = strpbrk(p, ",|");	/* prefer , but allow | for compat */
90123596Sbloom 		if (np)
90223596Sbloom 			*np = '\0';
90318619Sralph 		g = ifadate(p);
90418619Sralph 		DEBUG(11,"ifadate returns %o\n", g);
90518619Sralph 		if (g != FAIL) {
90618619Sralph 			ret = SUCCESS;
90718619Sralph 			if (g > MaxGrade)
90818619Sralph 				MaxGrade = g;
90918619Sralph 		}
91023596Sbloom 		if (np)
91123596Sbloom 			*np = ',';
91223596Sbloom 		p = np + 1;
91323596Sbloom 	} while (np);
91423596Sbloom 	if (MaxGrade == '\0')
91523596Sbloom 		MaxGrade = DefMaxGrade;
91618619Sralph 	return ret;
91713642Ssam }
91813642Ssam 
91918619Sralph /*
92018619Sralph  *	this routine will check a string (string)
92113642Ssam  *	like "MoTu0800-1730" to see if the present
92213642Ssam  *	time is within the given limits.
92313642Ssam  *	SIDE EFFECT - Retrytime is set
92413642Ssam  *
92513642Ssam  *	return codes:
92613642Ssam  *		0  -  not within limits
92713642Ssam  *		1  -  within limits
92813642Ssam  */
92913642Ssam 
93018619Sralph ifadate(string)
93118619Sralph char *string;
93213642Ssam {
93313642Ssam 	static char *days[]={
93413642Ssam 		"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
93513642Ssam 	};
93613642Ssam 	time_t clock;
93718619Sralph 	register char *s = string;
93817834Sralph 	int i, tl, th, tn, dayok=0;
93913642Ssam 	struct tm *localtime();
94013642Ssam 	struct tm *tp;
94118619Sralph 	char *p, MGrade;
94213642Ssam 
94323596Sbloom 	if ((p = index(s, '/')) == NULL)
94418619Sralph 		MGrade = DefMaxGrade;
94518619Sralph 	else
94618619Sralph 		MGrade = p[1];
94718619Sralph 
94813642Ssam 	time(&clock);
94913642Ssam 	tp = localtime(&clock);
95017834Sralph 	while (isascii(*s) && isalpha(*s)) {
95113642Ssam 		for (i = 0; days[i]; i++) {
95213642Ssam 			if (prefix(days[i], s))
95313642Ssam 				if (tp->tm_wday == i)
95413642Ssam 					dayok = 1;
95513642Ssam 		}
95613642Ssam 
95713642Ssam 		if (prefix("Wk", s))
95813642Ssam 			if (tp->tm_wday >= 1 && tp->tm_wday <= 5)
95913642Ssam 				dayok = 1;
96013642Ssam 		if (prefix("Any", s))
96113642Ssam 			dayok = 1;
96217834Sralph 		if (prefix("Evening", s)) {
96317834Sralph 			/* Sat or Sun */
96417834Sralph 			if (tp->tm_wday == 6 || tp->tm_wday == 0
96517834Sralph 				|| tp->tm_hour >= 17 || tp->tm_hour < 8)
96617834Sralph 					dayok = 1;
96717834Sralph 		}
96817834Sralph 		if (prefix("Night", s)) {
96917834Sralph 			if (tp->tm_wday == 6  /* Sat */
97018619Sralph 				|| tp->tm_hour >= 23 || tp->tm_hour < 8
97118619Sralph 					/* Sunday before 5pm */
97218619Sralph 				|| (tp->tm_wday == 0 && tp->tm_hour < 17))
97317834Sralph 					dayok = 1;
97417834Sralph 		}
97525705Sbloom 		if (prefix("NonPeak", s)) { /* For Tymnet and PC Pursuit */
97625705Sbloom 			/* Sat or Sun */
97725705Sbloom 			if (tp->tm_wday == 6 || tp->tm_wday == 0
97825705Sbloom 				|| tp->tm_hour >= 18 || tp->tm_hour < 7)
97925705Sbloom 					dayok = 1;
98025705Sbloom 		}
98113642Ssam 		s++;
98213642Ssam 	}
98313642Ssam 
98418619Sralph 	if (dayok == 0 && s != string)
98518619Sralph 		return FAIL;
98613642Ssam 	i = sscanf(s, "%d-%d", &tl, &th);
98718619Sralph   	if (i < 2)
98818619Sralph   		return MGrade;
98918619Sralph 	tn = tp->tm_hour * 100 + tp->tm_min;
99018619Sralph   	if (th < tl) { 		/* crosses midnight */
99118619Sralph   		if (tl <= tn || tn < th)
99218619Sralph   			return MGrade;
99325966Sbloom   	} else {
99417834Sralph 		if (tl <= tn && tn < th)
99518619Sralph 			return MGrade;
99625966Sbloom 	}
99718619Sralph 	return FAIL;
99813642Ssam }
99913642Ssam 
100018619Sralph /*
100118619Sralph  *	find first digit in string
100213642Ssam  *
100313642Ssam  *	return - pointer to first digit in string or end of string
100413642Ssam  */
100513642Ssam char *
100613642Ssam fdig(cp)
100713642Ssam register char *cp;
100813642Ssam {
100913642Ssam 	register char *c;
101013642Ssam 
101113642Ssam 	for (c = cp; *c; c++)
101213642Ssam 		if (*c >= '0' && *c <= '9')
101313642Ssam 			break;
101417834Sralph 	return c;
101513642Ssam }
101613642Ssam 
101713642Ssam /*
101813642Ssam  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
101913642Ssam  * Strings are compared as if they contain all capital letters.
102013642Ssam  */
102113642Ssam snccmp(s1, s2)
102213642Ssam register char *s1, *s2;
102313642Ssam {
102413642Ssam 	char c1, c2;
102513642Ssam 
102625127Sbloom 	if (islower(*s1))
102725127Sbloom 		c1 = toupper(*s1);
102825127Sbloom 	else
102925127Sbloom 		c1 = *s1;
103025127Sbloom 	if (islower(*s2))
103125127Sbloom 		c2 = toupper(*s2);
103225127Sbloom 	else
103325127Sbloom 		c2 = *s2;
103413642Ssam 
103513642Ssam 	while (c1 == c2) {
103625127Sbloom 		if (*s1++ == '\0')
103717834Sralph 			return 0;
103813642Ssam 		s2++;
103925127Sbloom 		if (islower(*s1))
104025127Sbloom 			c1 = toupper(*s1);
104125127Sbloom 		else
104225127Sbloom 			c1 = *s1;
104325127Sbloom 		if (islower(*s2))
104425127Sbloom 			c2 = toupper(*s2);
104525127Sbloom 		else
104625127Sbloom 			c2 = *s2;
104713642Ssam 	}
104817834Sralph 	return c1 - c2;
104913642Ssam }
105025127Sbloom 
105117834Sralph /*
105225127Sbloom  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
105325127Sbloom  * Strings are compared as if they contain all capital letters.
105425127Sbloom  */
105525127Sbloom sncncmp(s1, s2, n)
105625127Sbloom register char *s1, *s2;
105725127Sbloom register int n;
105825127Sbloom {
105925127Sbloom 	char c1, c2;
106025127Sbloom 
106125127Sbloom 	if (islower(*s1))
106225127Sbloom 		c1 = toupper(*s1);
106325127Sbloom 	else
106425127Sbloom 		c1 = *s1;
106525127Sbloom 	if (islower(*s2))
106625127Sbloom 		c2 = toupper(*s2);
106725127Sbloom 	else
106825127Sbloom 		c2 = *s2;
106925127Sbloom 
107025127Sbloom 	while ( --n >= 0 && c1 == c2) {
107125127Sbloom 		if (*s1++ == '\0')
107225127Sbloom 			return 0;
107325127Sbloom 		s2++;
107425127Sbloom 		if (islower(*s1))
107525127Sbloom 			c1 = toupper(*s1);
107625127Sbloom 		else
107725127Sbloom 			c1 = *s1;
107825127Sbloom 		if (islower(*s2))
107925127Sbloom 			c2 = toupper(*s2);
108025127Sbloom 		else
108125127Sbloom 			c2 = *s2;
108225127Sbloom 	}
108325127Sbloom 	return n<0 ? 0 : (c1 - c2);
108425127Sbloom }
108525127Sbloom /*
108617834Sralph  * do chat script
108717834Sralph  * occurs after local port is opened,
108817834Sralph  * before 'dialing' the other machine.
108917834Sralph  */
109017834Sralph dochat(dev, flds, fd)
109117834Sralph register struct Devices *dev;
109217834Sralph char *flds[];
109317834Sralph int fd;
109417834Sralph {
109517834Sralph 	register int i;
109617834Sralph 	register char *p;
109717834Sralph 	char bfr[sizeof(dev->D_argbfr)];
109817834Sralph 
109917834Sralph 	if (dev->D_numargs <= 5)
110017834Sralph 		return(0);
110117834Sralph 	DEBUG(4, "dochat called %d\n", dev->D_numargs);
110217834Sralph 	for (i = 0; i < dev->D_numargs-5; i++) {
110317834Sralph 		sprintf(bfr, dev->D_arg[D_CHAT+i], flds[F_PHONE]);
110417834Sralph 		if (strcmp(bfr, dev->D_arg[D_CHAT+i])) {
110517834Sralph 			p = malloc((unsigned)strlen(bfr)+1);
110617834Sralph 			if (p != NULL) {
110717834Sralph 				strcpy(p, bfr);
110817834Sralph 				dev->D_arg[D_CHAT+i] = p;
110917834Sralph 			}
111017834Sralph 		}
111117834Sralph 	}
111217834Sralph 	/* following is a kludge because login() arglist is a kludge */
111317834Sralph 	i = login(dev->D_numargs, &dev->D_arg[D_CHAT-5], fd);
111417834Sralph 	/*
111517834Sralph 	 * If login() last did a sendthem(), must pause so things can settle.
111617834Sralph 	 * But don't bother if chat failed.
111717834Sralph 	 */
111817834Sralph 	if (i == 0 && (dev->D_numargs&01))
111917834Sralph 		sleep(2);
112017834Sralph 	return(i);
112117834Sralph }
112225705Sbloom 
112325705Sbloom /*
112425705Sbloom  *	fix kill/echo/raw on line
112525705Sbloom  *
112625705Sbloom  *	return codes:  none
112725705Sbloom  */
112825705Sbloom fixmode(tty)
112925705Sbloom register int tty;
113025705Sbloom {
113125705Sbloom #ifdef	USG
113225705Sbloom 	struct termio ttbuf;
113325705Sbloom #else	!USG
113425705Sbloom 	struct sgttyb ttbuf;
113525705Sbloom #endif	!USG
113625705Sbloom 	register struct sg_spds *ps;
113725705Sbloom 	int speed;
113825705Sbloom 
113925705Sbloom 	if (IsTcpIp)
114025705Sbloom 		return;
114125705Sbloom #ifdef	USG
114225705Sbloom 	ioctl(tty, TCGETA, &ttbuf);
114325705Sbloom 	ttbuf.c_iflag = ttbuf.c_oflag = ttbuf.c_lflag = (ushort)0;
114425705Sbloom 	speed = ttbuf.c_cflag &= (CBAUD);
114525705Sbloom 	ttbuf.c_cflag |= (CS8|CREAD);
114625705Sbloom 	ttbuf.c_cc[VMIN] = 6;
114725705Sbloom 	ttbuf.c_cc[VTIME] = 1;
114825705Sbloom 	ioctl(tty, TCSETA, &ttbuf);
114925705Sbloom #else	!USG
115025705Sbloom 	ioctl(tty, TIOCGETP, &ttbuf);
115125705Sbloom 	ttbuf.sg_flags = (ANYP | RAW);
115225705Sbloom 	ioctl(tty, TIOCSETP, &ttbuf);
115325705Sbloom 	speed = ttbuf.sg_ispeed;
115425705Sbloom 	ioctl(tty, TIOCEXCL, STBNULL);
115525705Sbloom #endif	!USG
115625705Sbloom 
115725705Sbloom 	for (ps = spds; ps->sp_val; ps++)
115825705Sbloom 		if (ps->sp_name == speed) {
115925705Sbloom 			linebaudrate = ps->sp_val;
116025705Sbloom 			DEBUG(9,"Incoming baudrate is %d\n", linebaudrate);
116125705Sbloom 			return;
116225705Sbloom 		}
116333948Srick 	if (linebaudrate < 0) {
116433948Srick 		syslog(LOG_ERR, "unrecognized speed: %d", linebaudrate);
116533948Srick 		cleanup(FAIL);
116633948Srick 	}
116725705Sbloom }
1168