xref: /csrg-svn/usr.bin/uucp/uucico/cico.c (revision 37930)
113639Ssam #ifndef lint
2*37930Sbostic static char sccsid[] = "@(#)cico.c	5.19	(Berkeley) 05/11/89";
313639Ssam #endif
413639Ssam 
523590Sbloom #include <signal.h>
613639Ssam #include "uucp.h"
713639Ssam #include <setjmp.h>
817767Sralph #ifdef	USG
913639Ssam #include <termio.h>
1033559Srick #include <fcntl.h>
1113639Ssam #endif
1217767Sralph #ifndef	USG
1313639Ssam #include <sgtty.h>
1413639Ssam #endif
1517767Sralph #ifdef BSDTCP
1617767Sralph #include <netdb.h>
1717767Sralph #include <netinet/in.h>
1817767Sralph #include <sys/socket.h>
1917767Sralph #endif BSDTCP
2017767Sralph #include <sys/stat.h>
2133559Srick #ifdef BSD4_2
2233559Srick #include <sys/time.h>
2333559Srick #include <fcntl.h>
2433559Srick #else
2533559Srick #include <time.h>
2633559Srick #endif
2717767Sralph #include "uust.h"
2817767Sralph #include "uusub.h"
29*37930Sbostic #include "pathnames.h"
3013639Ssam 
3123590Sbloom #if defined(VMS) && defined(BSDTCP)
3223590Sbloom #define NOGETPEER
3323590Sbloom #endif
3423590Sbloom 
3517767Sralph jmp_buf Sjbuf;
3617767Sralph jmp_buf Pipebuf;
3713639Ssam 
3817767Sralph /*  call fail text  */
3913639Ssam char *Stattext[] = {
4013639Ssam 	"",
4113639Ssam 	"BAD SYSTEM",
4217767Sralph 	"WRONG TIME TO CALL",
4313639Ssam 	"SYSTEM LOCKED",
4413639Ssam 	"NO DEVICE",
4525703Sbloom 	"CALL FAILED",
4613639Ssam 	"LOGIN FAILED",
4713639Ssam 	"BAD SEQUENCE"
4817767Sralph };
4913639Ssam 
5017767Sralph /*  call fail codes  */
5117767Sralph int Stattype[] = {
5217767Sralph 	0,
5317767Sralph 	0,
5417767Sralph 	SS_WRONGTIME,
5517767Sralph 	0,
5617767Sralph 	SS_NODEVICE,
5717767Sralph 	SS_FAIL,
5817767Sralph 	SS_FAIL,
5917767Sralph 	SS_BADSEQ
6017767Sralph };
6113639Ssam 
6225703Sbloom 				/* Arguments to setdebug():		     */
6325703Sbloom #define DBG_TEMP  0		/*   Create a temporary audit file	     */
6425703Sbloom #define DBG_PERM  1		/*   Create a permanent audit file	     */
6525703Sbloom #define DBG_CLEAN 2		/*   Cleanup, discard temp file		     */
6613639Ssam 
6717767Sralph int ReverseRole = 0;
6817767Sralph int Role = SLAVE;
6933559Srick int InitialRole = SLAVE;
7033559Srick long StartTime;
7117767Sralph int onesys = 0;
7218616Sralph int turntime = 30 * 60;	/* 30 minutes expressed in seconds */
7325703Sbloom char *ttyn = NULL;
7417767Sralph extern int LocalOnly;
7525703Sbloom extern int errno;
7618616Sralph extern char MaxGrade, DefMaxGrade;
7718616Sralph extern char Myfullname[];
7817767Sralph 
7933559Srick long Bytes_Sent, Bytes_Received;
8033559Srick 
8117767Sralph #ifdef	USG
8213639Ssam struct termio Savettyb;
8313639Ssam #endif
8417767Sralph #ifndef	USG
8513639Ssam struct sgttyb Savettyb;
8613639Ssam #endif
8713639Ssam 
8834164Srick #define SETPROCTITLE
8934164Srick #ifdef SETPROCTITLE
9034164Srick char	**Argv = NULL;		/* pointer to argument vector */
9134164Srick char	*LastArgv = NULL;	/* end of argv */
9234164Srick #endif SETPROCTITLE
9334164Srick 
9425703Sbloom /*
9525703Sbloom  *	this program is used  to place a call to a
9613639Ssam  *	remote machine, login, and copy files between the two machines.
9713639Ssam  */
9834164Srick main(argc, argv, envp)
9925703Sbloom int argc;
10033945Srick char **argv;
10134164Srick char **envp;
10213639Ssam {
10313639Ssam 	register int ret;
10413639Ssam 	int seq;
10513639Ssam 	char wkpre[NAMESIZE], file[NAMESIZE];
10625703Sbloom 	char msg[MAXFULLNAME], *q;
10713639Ssam 	register char *p;
10825703Sbloom 	extern onintr(), timeout(), dbg_signal();
10913639Ssam 	extern char *pskip();
11033945Srick 	extern char *optarg;
11133945Srick 	extern int optind;
11223725Sbloom 	char rflags[MAXFULLNAME];
11323590Sbloom #ifdef NOGETPEER
11417767Sralph 	u_long Hostnumber = 0;
11523590Sbloom #endif NOGETPEER
11613639Ssam 
11713639Ssam 	strcpy(Progname, "uucico");
11813639Ssam 
11926149Sbloom #ifdef BSD4_2
12033945Srick 	sigsetmask(0L);	/* in case we inherit blocked signals */
12126149Sbloom #endif BSD4_2
12213639Ssam 	signal(SIGINT, onintr);
12313639Ssam 	signal(SIGHUP, onintr);
12413639Ssam 	signal(SIGQUIT, onintr);
12513639Ssam 	signal(SIGTERM, onintr);
12613639Ssam 	signal(SIGPIPE, onintr);	/* 4.1a tcp-ip stupidity */
12736449Smarc 	signal(SIGUSR1, dbg_signal);
12813639Ssam 	ret = guinfo(getuid(), User, msg);
12913639Ssam 	strcpy(Loginuser, User);
13023590Sbloom 	uucpname(Myname);
13133945Srick 	if (ret == FAIL) {
13233945Srick 		syslog(LOG_ERR, "can't get uid");
13333945Srick 		cleanup(FAIL);
13433945Srick 	}
13513639Ssam 
13625703Sbloom 	setbuf (stderr, CNULL);
13725703Sbloom 
13825703Sbloom 	rflags[0] = '\0';
13913639Ssam 	umask(WFMASK);
14013639Ssam 	strcpy(Rmtname, Myname);
14113639Ssam 	Ifn = Ofn = -1;
14233945Srick 	while ((ret = getopt(argc, argv, "RLd:g:p:r:s:x:t:")) != EOF)
14333945Srick 		switch(ret){
14413639Ssam 		case 'd':
14533945Srick 			Spool = optarg;
14613639Ssam 			break;
14713639Ssam 		case 'g':
14818616Sralph 		case 'p':
14933945Srick 			MaxGrade = DefMaxGrade = *optarg;
15013639Ssam 			break;
15113639Ssam 		case 'r':
15233945Srick 			Role = atoi(optarg);
15313639Ssam 			break;
15417767Sralph 		case 'R':
15517767Sralph 			ReverseRole++;
15617767Sralph 			Role = MASTER;
15717767Sralph 			break;
15813639Ssam 		case 's':
15933945Srick 			strncpy(Rmtname, optarg, MAXBASENAME);
16023590Sbloom 			Rmtname[MAXBASENAME] = '\0';
16113639Ssam 			if (Rmtname[0] != '\0')
16213639Ssam 				onesys = 1;
16313639Ssam 			break;
16413639Ssam 		case 'x':
16533945Srick 			Debug = atoi(optarg);
16613639Ssam 			if (Debug <= 0)
16713639Ssam 				Debug = 1;
16833945Srick 			strcat(rflags, argv[optind-1]);
16913639Ssam 			break;
17018616Sralph 		case 't':
17133945Srick 			turntime = atoi(optarg)*60;/* minutes to seconds */
17218616Sralph 			break;
17317767Sralph 		case 'L':	/* local calls only */
17417767Sralph 			LocalOnly++;
17517767Sralph 			break;
17623590Sbloom #ifdef NOGETPEER
17717767Sralph 		case 'h':
17817767Sralph 			Hostnumber = inet_addr(&argv[1][2]);
17917767Sralph 			break;
18023590Sbloom #endif NOGETPEER
18133945Srick 		case '?':
18213639Ssam 		default:
18333945Srick 			fprintf(stderr, "unknown flag %s (ignored)\n",
18433945Srick 				argv[optind-1]);
18513639Ssam 			break;
18613639Ssam 		}
18713639Ssam 
18833945Srick 	while (optind < argc)
18933945Srick 		fprintf(stderr, "unknown argument %s (ignored)\n",
19033945Srick 			argv[optind++]);
19117767Sralph 
19227069Sbloom 	if (Debug && Role == MASTER)
19327069Sbloom 		chkdebug();
19427069Sbloom 
19534164Srick #ifdef SETPROCTITLE
19634164Srick 	/*
19734164Srick 	 *  Save start and extent of argv for setproctitle.
19834164Srick 	 */
19934164Srick 
20034164Srick 	Argv = argv;
20134164Srick 	LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
20234164Srick #endif SETPROCTITLE
20334164Srick 
20425124Sbloom 	/* Try to run as uucp */
20517767Sralph 	setgid(getegid());
20617767Sralph 	setuid(geteuid());
20717767Sralph #ifdef	TIOCNOTTY
20817767Sralph 	/*
20917767Sralph 	 * detach uucico from controlling terminal
21017767Sralph 	 * to defend against rlogind sending us a SIGKILL (!!!)
21117767Sralph 	 */
212*37930Sbostic 	if (Role == MASTER && (ret = open(_PATH_TTY, 2)) >= 0) {
21317767Sralph 		ioctl(ret, TIOCNOTTY, STBNULL);
21417767Sralph 		close(ret);
21517767Sralph 	}
21617767Sralph #endif TIOCNOTTY
21717767Sralph #ifdef BSD4_2
21825517Stef 	if (getpgrp(0) == 0) { /* We have no controlling terminal */
21917767Sralph 		setpgrp(0, getpid());
22017767Sralph 	}
22133945Srick #ifdef USE_SYSLOG
22233945Srick #ifdef BSD4_3
22333945Srick 	openlog("uucico", LOG_PID, LOG_UUCP);
22433945Srick #else /* !BSD4_3 */
22533945Srick 	openlog("uucico", LOG_PID);
22633945Srick #endif /* !BSD4_3 */
22733945Srick #endif /* USE_SYSLOG */
22817767Sralph #endif BSD4_2
22917767Sralph 
23033945Srick #ifdef BSD4_3
23133945Srick 	unsetenv("TZ");		/* We don't want him resetting our time zone */
23233945Srick #endif /* !BSD4_3 */
23333945Srick 
23433945Srick 	if (subchdir(Spool) < 0) {
23533945Srick 		syslog(LOG_ERR, "chdir(%s) failed: %m", Spool);
23633945Srick 		cleanup(FAIL);
23733945Srick 	}
23833945Srick 
23913639Ssam 	strcpy(Wrkdir, Spool);
24013639Ssam 
24125703Sbloom 	if (Debug) {
24225703Sbloom 		setdebug ((Role == SLAVE) ? DBG_TEMP : DBG_PERM);
24325703Sbloom 		if (Debug > 0)
24425703Sbloom 			logent ("Local Enabled", "DEBUG");
24525703Sbloom 	}
24625703Sbloom 
24725703Sbloom 	/*
24825703Sbloom 	 * First time through: If we're the slave, do initial checking.
24925703Sbloom 	 */
25013639Ssam 	if (Role == SLAVE) {
25117767Sralph 		/* check for /etc/nologin */
25225124Sbloom 		if (access(NOLOGIN, 0) == 0) {
25317767Sralph 			logent(NOLOGIN, "UUCICO SHUTDOWN");
25423590Sbloom 			if (Debug > 4)
25517767Sralph 				logent("DEBUGGING", "continuing anyway");
25617767Sralph 			else
25717767Sralph 				cleanup(1);
25817767Sralph 		}
25925703Sbloom 		Ifn = 0;
26025703Sbloom 		Ofn = 1;
26117767Sralph #ifdef	TCPIP
26217767Sralph 		/*
26317767Sralph 		 * Determine if we are on TCPIP
26417767Sralph 		 */
26533559Srick 		if (isatty(Ifn) == 0) {
26617767Sralph 			IsTcpIp = 1;
26717767Sralph 			DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
26823590Sbloom 		} else
26923590Sbloom 			IsTcpIp = 0;
27017767Sralph #endif TCPIP
27113639Ssam 		/* initial handshake */
27213639Ssam 		onesys = 1;
27317767Sralph 		if (!IsTcpIp) {
27417767Sralph #ifdef	USG
27525703Sbloom 			ret = ioctl(Ifn, TCGETA, &Savettyb);
27613639Ssam 			Savettyb.c_cflag = (Savettyb.c_cflag & ~CS8) | CS7;
27713639Ssam 			Savettyb.c_oflag |= OPOST;
27813639Ssam 			Savettyb.c_lflag |= (ISIG|ICANON|ECHO);
27917767Sralph #else !USG
28025703Sbloom 			ret = ioctl(Ifn, TIOCGETP, &Savettyb);
28113639Ssam 			Savettyb.sg_flags |= ECHO;
28213639Ssam 			Savettyb.sg_flags &= ~RAW;
28317767Sralph #endif !USG
28425703Sbloom 			ttyn = ttyname(Ifn);
28513639Ssam 		}
28613639Ssam 		fixmode(Ifn);
28725703Sbloom 
28825703Sbloom 		/*
28925703Sbloom 		 * Initial Message -- tell them we're here, and who we are.
29025703Sbloom 		 */
29118616Sralph 		sprintf(msg, "here=%s", Myfullname);
29217767Sralph 		omsg('S', msg, Ofn);
29313639Ssam 		signal(SIGALRM, timeout);
29433559Srick 		alarm(IsTcpIp?MAXMSGTIME*4:MAXMSGTIME);
29513639Ssam 		if (setjmp(Sjbuf)) {
29613639Ssam 			/* timed out */
29717767Sralph 			if (!IsTcpIp) {
29817767Sralph #ifdef	USG
29925703Sbloom 				ret = ioctl(Ifn, TCSETA, &Savettyb);
30025703Sbloom 
30123590Sbloom #else	!USG
30225703Sbloom 				ret = ioctl(Ifn, TIOCSETP, &Savettyb);
30323590Sbloom #endif !USG
30413639Ssam 			}
30517767Sralph 			cleanup(0);
30613639Ssam 		}
30713639Ssam 		for (;;) {
30813639Ssam 			ret = imsg(msg, Ifn);
30925703Sbloom 			if (ret != SUCCESS) {
31013639Ssam 				alarm(0);
31117767Sralph 				if (!IsTcpIp) {
31217767Sralph #ifdef	USG
31325703Sbloom 					ret = ioctl(Ifn, TCSETA, &Savettyb);
31423590Sbloom #else	!USG
31525703Sbloom 					ret = ioctl(Ifn, TIOCSETP, &Savettyb);
31623590Sbloom #endif !USG
31713639Ssam 				}
31817767Sralph 				cleanup(0);
31913639Ssam 			}
32013639Ssam 			if (msg[0] == 'S')
32113639Ssam 				break;
32213639Ssam 		}
32313639Ssam 		alarm(0);
32413639Ssam 		q = &msg[1];
32513639Ssam 		p = pskip(q);
32623590Sbloom 		strncpy(Rmtname, q, MAXBASENAME);
32723590Sbloom 		Rmtname[MAXBASENAME] = '\0';
32825703Sbloom 
32925703Sbloom 		/*
33025703Sbloom 		 * Now that we know who they are, give the audit file the right
33125703Sbloom 		 * name.
33225703Sbloom 		 */
33325703Sbloom 		setdebug (DBG_PERM);
33413639Ssam 		DEBUG(4, "sys-%s\n", Rmtname);
33523725Sbloom 		/* The versys will also do an alias on the incoming name */
33623725Sbloom 		if (versys(&Rmtname)) {
33733559Srick #ifdef	NOSTRANGERS
33823725Sbloom 			/* If we don't know them, we won't talk to them... */
33933945Srick 			syslog(LOG_WARNING, "Unknown host: %s", Rmtname);
34023590Sbloom 			omsg('R', "You are unknown to me", Ofn);
34123590Sbloom 			cleanup(0);
34223725Sbloom #endif	NOSTRANGERS
34323590Sbloom 		}
34417767Sralph #ifdef BSDTCP
34517767Sralph 		/* we must make sure they are really who they say they
34617767Sralph 		 * are. We compare the hostnumber with the number in the hosts
34717767Sralph 		 * table for the site they claim to be.
34817767Sralph 		 */
34917767Sralph 		if (IsTcpIp) {
35017767Sralph 			struct hostent *hp;
35117767Sralph 			char *cpnt, *inet_ntoa();
35225703Sbloom 			int fromlen;
35317767Sralph 			struct sockaddr_in from;
35425124Sbloom 			extern char PhoneNumber[];
35517767Sralph 
35623590Sbloom #ifdef	NOGETPEER
35723590Sbloom 			from.sin_addr.s_addr = Hostnumber;
35823590Sbloom 			from.sin_family = AF_INET;
35923590Sbloom #else	!NOGETPEER
36025703Sbloom 			fromlen = sizeof(from);
36125703Sbloom 			if (getpeername(Ifn, &from, &fromlen) < 0) {
36217767Sralph 				logent(Rmtname, "NOT A TCP CONNECTION");
36317767Sralph 				omsg('R', "NOT TCP", Ofn);
36417767Sralph 				cleanup(0);
36517767Sralph 			}
36623590Sbloom #endif	!NOGETPEER
36717767Sralph 			hp = gethostbyaddr(&from.sin_addr,
36817767Sralph 				sizeof (struct in_addr), from.sin_family);
36925703Sbloom 			if (hp == NULL) {
37017767Sralph 				/* security break or just old host table? */
37117767Sralph 				logent(Rmtname, "UNKNOWN IP-HOST Name =");
37217767Sralph 				cpnt = inet_ntoa(from.sin_addr),
37317767Sralph 				logent(cpnt, "UNKNOWN IP-HOST Number =");
37417767Sralph 				sprintf(wkpre, "%s/%s isn't in my host table",
37517767Sralph 					Rmtname, cpnt);
37617767Sralph 				omsg('R' ,wkpre ,Ofn);
37717767Sralph 				cleanup(0);
37817767Sralph 			}
37925703Sbloom 			if (Debug > 99)
38017767Sralph 				logent(Rmtname,"Request from IP-Host name =");
38125124Sbloom 			/*
38225124Sbloom 			 * The following is to determine if the name given us by
38325124Sbloom 			 * the Remote uucico matches any of the names
38417767Sralph 			 * given its network number (remote machine) in our
38517767Sralph 			 * host table.
38625124Sbloom 			 * We could check the aliases, but that won't work in
38725124Sbloom 			 * all cases (like if you are running the domain
38825124Sbloom 			 * server, where you don't get any aliases). The only
38925124Sbloom 			 * reliable way I can think of that works in ALL cases
39025124Sbloom 			 * is too look up the site in L.sys and see if the
39125124Sbloom 			 * sitename matches what we would call him if we
39225124Sbloom 			 * originated the call.
39317767Sralph 			 */
39425124Sbloom 			/* PhoneNumber contains the official network name of the 			   host we are checking. (set in versys.c) */
39525124Sbloom 			if (sncncmp(PhoneNumber, hp->h_name, SYSNSIZE) == 0) {
39617767Sralph 				if (Debug > 99)
39717767Sralph 					logent(q,"Found in host Tables");
39825124Sbloom 			} else {
39925124Sbloom 				logent(hp->h_name, "FORGED HOSTNAME");
40025124Sbloom 				logent(inet_ntoa(from.sin_addr), "ORIGINATED AT");
40125124Sbloom 				logent(PhoneNumber, "SHOULD BE");
40225124Sbloom 				sprintf(wkpre, "You're not who you claim to be: %s !=  %s", hp->h_name, PhoneNumber);
40325124Sbloom 				omsg('R', wkpre, Ofn);
40425124Sbloom 				cleanup(0);
40517767Sralph 			}
40617767Sralph 		}
40717767Sralph #endif	BSDTCP
40817767Sralph 
40925703Sbloom 		if (mlock(Rmtname)) {
41013639Ssam 			omsg('R', "LCK", Ofn);
41113639Ssam 			cleanup(0);
41213639Ssam 		}
41313639Ssam 		else if (callback(Loginuser)) {
41413639Ssam 			signal(SIGINT, SIG_IGN);
41513639Ssam 			signal(SIGHUP, SIG_IGN);
41613639Ssam 			omsg('R', "CB", Ofn);
41713639Ssam 			logent("CALLBACK", "REQUIRED");
41813639Ssam 			/*  set up for call back  */
41917767Sralph 			systat(Rmtname, SS_CALLBACK, "CALLING BACK");
42013639Ssam 			gename(CMDPRE, Rmtname, 'C', file);
42113639Ssam 			close(creat(subfile(file), 0666));
42213639Ssam 			xuucico(Rmtname);
42313639Ssam 			cleanup(0);
42413639Ssam 		}
42513639Ssam 		seq = 0;
42613639Ssam 		while (*p == '-') {
42713639Ssam 			q = pskip(p);
42813639Ssam 			switch(*(++p)) {
42913639Ssam 			case 'x':
43025703Sbloom 				if (Debug == 0) {
43125703Sbloom 					Debug = atoi(++p);
43225703Sbloom 					if (Debug <= 0)
43325703Sbloom 						Debug = 1;
43425703Sbloom 					setdebug(DBG_PERM);
43525703Sbloom 					if (Debug > 0)
43625703Sbloom 						logent("Remote Enabled", "DEBUG");
43725703Sbloom 				} else {
43825703Sbloom 					DEBUG(1, "Remote debug request ignored\n",
43925703Sbloom 					   CNULL);
44025703Sbloom 				}
44113639Ssam 				break;
44213639Ssam 			case 'Q':
44313639Ssam 				seq = atoi(++p);
44413639Ssam 				break;
44518616Sralph 			case 'p':
44618616Sralph 				MaxGrade = DefMaxGrade = *++p;
44718616Sralph 				DEBUG(4, "MaxGrade set to %c\n", MaxGrade);
44818616Sralph 				break;
44923590Sbloom 			case 'v':
45023590Sbloom 				if (strncmp(p, "grade", 5) == 0) {
45123590Sbloom 					p += 6;
45223590Sbloom 					MaxGrade = DefMaxGrade = *p++;
45323590Sbloom 					DEBUG(4, "MaxGrade set to %c\n", MaxGrade);
45423590Sbloom 				}
45523590Sbloom 				break;
45613639Ssam 			default:
45713639Ssam 				break;
45813639Ssam 			}
45913639Ssam 			p = q;
46013639Ssam 		}
46134164Srick 		setproctitle("%s: startup", Rmtname);
46213639Ssam 		if (callok(Rmtname) == SS_BADSEQ) {
46313639Ssam 			logent("BADSEQ", "PREVIOUS");
46413639Ssam 			omsg('R', "BADSEQ", Ofn);
46513639Ssam 			cleanup(0);
46613639Ssam 		}
46717767Sralph #ifdef GNXSEQ
46813639Ssam 		if ((ret = gnxseq(Rmtname)) == seq) {
46913639Ssam 			omsg('R', "OK", Ofn);
47013639Ssam 			cmtseq();
47117767Sralph 		} else {
47217767Sralph #else !GNXSEQ
47317767Sralph 		if (seq == 0)
47417767Sralph 			omsg('R', "OK", Ofn);
47513639Ssam 		else {
47617767Sralph #endif !GNXSEQ
47713639Ssam 			systat(Rmtname, Stattype[7], Stattext[7]);
47823590Sbloom 			logent("BAD SEQ", "FAILED HANDSHAKE");
47917767Sralph #ifdef GNXSEQ
48013639Ssam 			ulkseq();
48117767Sralph #endif GNXSEQ
48213639Ssam 			omsg('R', "BADSEQ", Ofn);
48313639Ssam 			cleanup(0);
48413639Ssam 		}
48513639Ssam 		if (ttyn != NULL)
48613639Ssam 			chmod(ttyn, 0600);
48713639Ssam 	}
48817767Sralph 
48913639Ssam loop:
49017767Sralph 	if(setjmp(Pipebuf)) {	/* come here on SIGPIPE	*/
49117767Sralph 		clsacu();
49234164Srick 		logcls();
49317767Sralph 		close(Ofn);
49417767Sralph 		close(Ifn);
49517767Sralph 		Ifn = Ofn = -1;
49617767Sralph 		rmlock(CNULL);
49717767Sralph 		sleep(3);
49817767Sralph 	}
49913639Ssam 	if (!onesys) {
50033559Srick 		do_connect_accounting();
50133945Srick #ifdef DIALINOUT
50233945Srick 		/* reenable logins on dialout */
50333945Srick 		reenable();
50433945Srick #endif DIALINOUT
50533559Srick 		StartTime = 0;
50634164Srick 		setproctitle("looking for work");
50713639Ssam 		ret = gnsys(Rmtname, Spool, CMDPRE);
50834164Srick 		setproctitle("%s: startup", Rmtname);
50925703Sbloom 		setdebug(DBG_PERM);
51013639Ssam 		if (ret == FAIL)
51113639Ssam 			cleanup(100);
51233559Srick 		else if (ret == SUCCESS)
51313639Ssam 			cleanup(0);
51434164Srick 		logcls();
51517767Sralph 	} else if (Role == MASTER && callok(Rmtname) != 0) {
51613639Ssam 		logent("SYSTEM STATUS", "CAN NOT CALL");
51713639Ssam 		cleanup(0);
51813639Ssam 	}
51913639Ssam 
52023590Sbloom 	sprintf(wkpre, "%c.%.*s", CMDPRE, SYSNSIZE, Rmtname);
52133559Srick 	StartTime = 0;
52233559Srick 	Bytes_Sent = Bytes_Received = 0L;
52313639Ssam 
52417767Sralph 	signal(SIGINT, SIG_IGN);
52517767Sralph 	signal(SIGQUIT, SIG_IGN);
52613639Ssam 	if (Role == MASTER) {
52733559Srick 		extern char LineType[];
52817767Sralph 		/* check for /etc/nologin */
52925124Sbloom 		if (access(NOLOGIN, 0) == 0) {
53017767Sralph 			logent(NOLOGIN, "UUCICO SHUTDOWN");
53123590Sbloom 			if (Debug > 4)
53217767Sralph 				logent("DEBUGGING", "continuing anyway");
53317767Sralph 			else
53417767Sralph 				cleanup(1);
53517767Sralph 		}
53613639Ssam 		/*  master part */
53713639Ssam 		signal(SIGHUP, SIG_IGN);
53813639Ssam 		if (Ifn != -1 && Role == MASTER) {
53913639Ssam 			write(Ofn, EOTMSG, strlen(EOTMSG));
54013639Ssam 			clsacu();
54113639Ssam 			close(Ofn);
54213639Ssam 			close(Ifn);
54313639Ssam 			Ifn = Ofn = -1;
54413639Ssam 			rmlock(CNULL);
54513639Ssam 			sleep(3);
54613639Ssam 		}
54725124Sbloom 		if (mlock(Rmtname) != SUCCESS) {
54833559Srick 			DEBUG(1, "LOCKED: call to %s\n", Rmtname);
54917767Sralph 			US_SST(us_s_lock);
55013639Ssam 			goto next;
55113639Ssam 		}
55234164Srick 		setproctitle("%s: starting call", Rmtname);
55313639Ssam 		Ofn = Ifn = conn(Rmtname);
55434164Srick 		sprintf(msg, "(call to %s via %s)", Rmtname, LineType);
55513639Ssam 		if (Ofn < 0) {
55617767Sralph 			if (Ofn != CF_TIME)
55717767Sralph 				logent(msg, _FAILED);
55817767Sralph 			/* avoid excessive 'wrong time' info */
55923590Sbloom 			if (Stattype[-Ofn] != SS_WRONGTIME){
56017767Sralph 				systat(Rmtname, Stattype[-Ofn], Stattext[-Ofn]);
56117767Sralph 				US_SST(-Ofn);
56217767Sralph 				UB_SST(-Ofn);
56317767Sralph 			}
56413639Ssam 			goto next;
56517767Sralph 		} else {
56613639Ssam 			logent(msg, "SUCCEEDED");
56717767Sralph 			US_SST(us_s_cok);
56817767Sralph 			UB_SST(ub_ok);
56913639Ssam 		}
57033559Srick 		InitialRole = MASTER;
57117767Sralph #ifdef	TCPIP
57217767Sralph 		/*
57317767Sralph 		 * Determine if we are on TCPIP
57417767Sralph 		 */
57525703Sbloom 		if (isatty(Ifn) == 0) {
57617767Sralph 			IsTcpIp = 1;
57717767Sralph 			DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
57823590Sbloom 		} else
57923590Sbloom 			IsTcpIp = 0;
58017767Sralph #endif
58117767Sralph 
58213639Ssam 		if (setjmp(Sjbuf))
58313639Ssam 			goto next;
58413639Ssam 		signal(SIGALRM, timeout);
58533559Srick 		alarm(IsTcpIp?MAXMSGTIME*4:MAXMSGTIME*2);
58613639Ssam 		for (;;) {
58713639Ssam 			ret = imsg(msg, Ifn);
58833559Srick 			if (ret != SUCCESS) {
58913639Ssam 				alarm(0);
59033559Srick 				DEBUG(4,"\nimsg failed: errno %d\n", errno);
59117767Sralph 				logent("imsg 1", _FAILED);
59217767Sralph 				goto Failure;
59313639Ssam 			}
59413639Ssam 			if (msg[0] == 'S')
59513639Ssam 				break;
59613639Ssam 		}
59733559Srick 		alarm(IsTcpIp?MAXMSGTIME*4:MAXMSGTIME);
59817767Sralph #ifdef GNXSEQ
59913639Ssam 		seq = gnxseq(Rmtname);
60017767Sralph #else !GNXSEQ
60117767Sralph 		seq = 0;
60217767Sralph #endif !GNXSEQ
60318616Sralph 		if (MaxGrade != '\177') {
60423590Sbloom 			DEBUG(2, "Max Grade this transfer is %c\n", MaxGrade);
60525963Sbloom 			sprintf(msg, "%s -Q%d -p%c -vgrade=%c %s",
60625963Sbloom 				Myname, seq, MaxGrade, MaxGrade, rflags);
60725963Sbloom 		} else
60825963Sbloom 			sprintf(msg, "%s -Q%d %s", Myname, seq, rflags);
60913639Ssam 		omsg('S', msg, Ofn);
61013639Ssam 		for (;;) {
61113639Ssam 			ret = imsg(msg, Ifn);
61213639Ssam 			DEBUG(4, "msg-%s\n", msg);
61317767Sralph 			if (ret != SUCCESS) {
61413639Ssam 				alarm(0);
61517767Sralph #ifdef GNXSEQ
61613639Ssam 				ulkseq();
61717767Sralph #endif GNXSEQ
61817767Sralph 				logent("imsg 2", _FAILED);
61917767Sralph 				goto Failure;
62013639Ssam 			}
62113639Ssam 			if (msg[0] == 'R')
62213639Ssam 				break;
62313639Ssam 		}
62413639Ssam 		alarm(0);
62513639Ssam 		if (msg[1] == 'B') {
62613639Ssam 			/* bad sequence */
62723590Sbloom 			logent("BAD SEQ", "FAILED HANDSHAKE");
62817767Sralph 			US_SST(us_s_hand);
62917767Sralph 			systat(Rmtname, SS_BADSEQ, Stattext[SS_BADSEQ]);
63017767Sralph #ifdef GNXSEQ
63113639Ssam 			ulkseq();
63217767Sralph #endif GNXSEQ
63313639Ssam 			goto next;
63413639Ssam 		}
63513639Ssam 		if (strcmp(&msg[1], "OK") != SAME)  {
63623590Sbloom 			logent(&msg[1], "FAILED HANDSHAKE");
63717767Sralph 			US_SST(us_s_hand);
63817767Sralph #ifdef GNXSEQ
63913639Ssam 			ulkseq();
64017767Sralph #endif GNXSEQ
64117767Sralph 			systat(Rmtname, SS_INPROGRESS,
64217767Sralph 				strcmp(&msg[1], "CB") == SAME?
64323590Sbloom 				"AWAITING CALLBACK": "FAILED HANDSHAKE");
64413639Ssam 			goto next;
64513639Ssam 		}
64617767Sralph #ifdef GNXSEQ
64713639Ssam 		cmtseq();
64817767Sralph #endif GNXSEQ
64913639Ssam 	}
65017767Sralph 	DEBUG(1, "Rmtname %s, ", Rmtname);
65113639Ssam 	DEBUG(1, "Role %s,  ", Role ? "MASTER" : "SLAVE");
65213639Ssam 	DEBUG(1, "Ifn - %d, ", Ifn);
65313639Ssam 	DEBUG(1, "Loginuser - %s\n", Loginuser);
65434164Srick 	setproctitle("%s: %s", Rmtname, Role ? "MASTER" : "SLAVE");
65513639Ssam 
65625703Sbloom 	ttyn = ttyname(Ifn);
65725703Sbloom 
65833559Srick 	alarm(IsTcpIp?MAXMSGTIME*4:MAXMSGTIME);
65918616Sralph 	if (ret=setjmp(Sjbuf))
66013639Ssam 		goto Failure;
66113639Ssam 	ret = startup(Role);
66213639Ssam 	alarm(0);
66313639Ssam 	if (ret != SUCCESS) {
66434164Srick 		logent("(startup)", _FAILED);
66513639Ssam Failure:
66617767Sralph 		US_SST(us_s_start);
66718616Sralph 		systat(Rmtname, SS_FAIL, ret > 0 ? "CONVERSATION FAILED" :
66818616Sralph 			"STARTUP FAILED");
66913639Ssam 		goto next;
67017767Sralph 	} else {
67133559Srick 		char smsg[BUFSIZ], gmsg[10], pmsg[20], bpsmsg[20];
67233559Srick 		extern char UsingProtocol;
67333559Srick 		extern int linebaudrate;
67433559Srick 		if (ttyn != NULL)
67533559Srick 			sprintf(bpsmsg, " %s %d bps", &ttyn[5], linebaudrate);
67633559Srick 		else
67733559Srick 			bpsmsg[0] = '\0';
67833559Srick 		if (UsingProtocol != 'g')
67933559Srick 			sprintf(pmsg, " %c protocol", UsingProtocol);
68033559Srick 		else
68133559Srick 			pmsg[0] = '\0';
68233559Srick 		if (MaxGrade != '\177')
68333559Srick 			sprintf(gmsg, " grade %c", MaxGrade);
68433559Srick 		else
68533559Srick 			gmsg[0] = '\0';
68634164Srick 		sprintf(smsg, "(startup%s%s%s)", bpsmsg, pmsg, gmsg);
68733559Srick 		logent(smsg, "OK");
68817767Sralph 		US_SST(us_s_gress);
68933559Srick 		StartTime = Now.time;
69013639Ssam 		systat(Rmtname, SS_INPROGRESS, "TALKING");
69113639Ssam 		ret = cntrl(Role, wkpre);
69213639Ssam 		DEBUG(1, "cntrl - %d\n", ret);
69313639Ssam 		signal(SIGINT, SIG_IGN);
69413639Ssam 		signal(SIGHUP, SIG_IGN);
69513639Ssam 		signal(SIGALRM, timeout);
69634164Srick 		sprintf(smsg, "(conversation complete %ld sent %ld received)",
69733559Srick 			Bytes_Sent, Bytes_Received);
69817767Sralph 		if (ret == SUCCESS) {
69933559Srick 			logent(smsg, "OK");
70017767Sralph 			US_SST(us_s_ok);
70113639Ssam 			rmstat(Rmtname);
70213639Ssam 
70317767Sralph 		} else {
70433559Srick 			logent(smsg, _FAILED);
70517767Sralph 			US_SST(us_s_cf);
70617767Sralph 			systat(Rmtname, SS_FAIL, "CONVERSATION FAILED");
70713639Ssam 		}
70833559Srick 		alarm(IsTcpIp?MAXMSGTIME*4:MAXMSGTIME);
70913639Ssam 		DEBUG(4, "send OO %d,", ret);
71013639Ssam 		if (!setjmp(Sjbuf)) {
71113639Ssam 			for (;;) {
71213639Ssam 				omsg('O', "OOOOO", Ofn);
71313639Ssam 				ret = imsg(msg, Ifn);
71413639Ssam 				if (ret != 0)
71513639Ssam 					break;
71613639Ssam 				if (msg[0] == 'O')
71713639Ssam 					break;
71813639Ssam 			}
71913639Ssam 		}
72013639Ssam 		alarm(0);
72117767Sralph 		clsacu();
72217767Sralph 		rmlock(CNULL);
72333559Srick 
72413639Ssam 	}
72513639Ssam next:
72613639Ssam 	if (!onesys) {
72713639Ssam 		goto loop;
72813639Ssam 	}
72913639Ssam 	cleanup(0);
73013639Ssam }
73113639Ssam 
73217767Sralph #ifndef	USG
73313639Ssam struct sgttyb Hupvec;
73413639Ssam #endif
73513639Ssam 
73625703Sbloom /*
73725703Sbloom  *	cleanup and exit with "code" status
73813639Ssam  */
73913639Ssam cleanup(code)
74013639Ssam register int code;
74113639Ssam {
74213639Ssam 	signal(SIGINT, SIG_IGN);
74313639Ssam 	signal(SIGHUP, SIG_IGN);
74413639Ssam 	rmlock(CNULL);
74525703Sbloom 	sleep(5);			/* Wait for any pending output	  */
74613639Ssam 	clsacu();
74713639Ssam 	logcls();
74813639Ssam 	if (Role == SLAVE) {
74917767Sralph 		if (!IsTcpIp) {
75017767Sralph #ifdef USG
75113639Ssam 			Savettyb.c_cflag |= HUPCL;
75223590Sbloom 			(void) ioctl(0, TCSETA, &Savettyb);
75317767Sralph #else !USG
75423590Sbloom 			(void) ioctl(0, TIOCHPCL, STBNULL);
75518616Sralph #ifdef TIOCSDTR
75623590Sbloom 			(void) ioctl(0, TIOCCDTR, STBNULL);
75718616Sralph 			sleep(2);
75823590Sbloom 			(void) ioctl(0, TIOCSDTR, STBNULL);
75918616Sralph #else !TIOCSDTR
76023590Sbloom 			(void) ioctl(0, TIOCGETP, &Hupvec);
76113639Ssam 			Hupvec.sg_ispeed = B0;
76213639Ssam 			Hupvec.sg_ospeed = B0;
76323590Sbloom 			(void) ioctl(0, TIOCSETP, &Hupvec);
76425703Sbloom #endif !TIOCSDTR
76513639Ssam 			sleep(2);
76623590Sbloom 			(void) ioctl(0, TIOCSETP, &Savettyb);
76717767Sralph 			/* make *sure* exclusive access is off */
76823590Sbloom 			(void) ioctl(0, TIOCNXCL, STBNULL);
76917767Sralph #endif !USG
77013639Ssam 		}
77113639Ssam 		if (ttyn != NULL)
77213639Ssam 			chmod(ttyn, 0600);
77313639Ssam 	}
77413639Ssam 	if (Ofn != -1) {
77513639Ssam 		if (Role == MASTER)
77613639Ssam 			write(Ofn, EOTMSG, strlen(EOTMSG));
77713639Ssam 		close(Ifn);
77813639Ssam 		close(Ofn);
77913639Ssam 	}
78018616Sralph #ifdef DIALINOUT
78118616Sralph 	/* reenable logins on dialout */
78218616Sralph 	reenable();
78318616Sralph #endif DIALINOUT
78413639Ssam 	if (code == 0)
78513639Ssam 		xuuxqt();
78617767Sralph 	else
78717767Sralph 		DEBUG(1, "exit code %d\n", code);
78825703Sbloom 	setdebug (DBG_CLEAN);
78933559Srick 	do_connect_accounting();
79013639Ssam 	exit(code);
79113639Ssam }
79213639Ssam 
79333559Srick do_connect_accounting()
79433559Srick {
79533945Srick #ifdef DO_CONNECT_ACCOUNTING
79633559Srick 	register FILE *fp;
79733559Srick 	struct tm *localtime();
79833559Srick 	register struct tm *tm;
79933559Srick 	int flags;
80033559Srick 
80133559Srick 	if (StartTime == 0)
80233559Srick 		return;
80333559Srick 
80433945Srick 	fp = fopen(DO_CONNECT_ACCOUNTING, "a");
80533945Srick 	if (fp == NULL) {
80633945Srick 		syslog(LOG_ALERT, "fopen(%s) failed: %m",DO_CONNECT_ACCOUNTING);
80733945Srick 		cleanup(FAIL);
80833945Srick 	}
80933559Srick 
81033559Srick 	tm = localtime(&StartTime);
81133559Srick #ifdef F_SETFL
81233559Srick 	flags = fcntl(fileno(fp), F_GETFL, 0);
81333559Srick 	fcntl(fileno(fp), F_SETFL, flags|O_APPEND);
81433559Srick #endif
81533559Srick #ifdef USG
81633559Srick 	fprintf(fp,"%s %d %d%.2d%.2d %.2d%.2d %d %ld %s %ld %ld\n",
81733559Srick #else /* V7 */
81833559Srick 	fprintf(fp,"%s %d %d%02d%02d %02d%02d %d %ld %s %ld %ld\n",
81933559Srick #endif /* V7 */
82033559Srick 		Rmtname, InitialRole, tm->tm_year, tm->tm_mon + 1,
82133559Srick 		tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_wday,
82233559Srick 		(Now.time - StartTime + 59) / 60,
82333559Srick 		ttyn == NULL ? "ttyp0" : &ttyn[5],
82433559Srick 			Bytes_Sent, Bytes_Received);
82533559Srick 	fclose(fp);
82633559Srick #endif /* DO_CONNECT_ACCOUNTING */
82733559Srick }
82833559Srick 
82925703Sbloom /*
83025703Sbloom  *	on interrupt - remove locks and exit
83113639Ssam  */
83213639Ssam 
83313639Ssam onintr(inter)
83413639Ssam register int inter;
83513639Ssam {
83633559Srick 	char str[BUFSIZ];
83713639Ssam 	signal(inter, SIG_IGN);
83834164Srick 	sprintf(str, "(SIGNAL %d)", inter);
83913639Ssam 	logent(str, "CAUGHT");
84017767Sralph 	US_SST(us_s_intr);
84123590Sbloom 	if (*Rmtname && strncmp(Rmtname, Myname, MAXBASENAME))
84217767Sralph 		systat(Rmtname, SS_FAIL, str);
84334164Srick 	sprintf(str, "(conversation complete %ld sent %ld received)",
84433559Srick 		Bytes_Sent, Bytes_Received);
84533559Srick 	logent(str, _FAILED);
84617767Sralph 	if (inter == SIGPIPE && !onesys)
84717767Sralph 		longjmp(Pipebuf, 1);
84813639Ssam 	cleanup(inter);
84913639Ssam }
85013639Ssam 
85113639Ssam /*
85213639Ssam  * Catch a special signal
85336449Smarc  * (SIGUSR1), and toggle debugging between 0 and 30.
85413639Ssam  * Handy for looking in on long running uucicos.
85513639Ssam  */
85625703Sbloom dbg_signal()
85713639Ssam {
85825703Sbloom 	Debug = (Debug == 0) ? 30 : 0;
85925703Sbloom 	setdebug(DBG_PERM);
86025703Sbloom 	if (Debug > 0)
86125703Sbloom 		logent("Signal Enabled", "DEBUG");
86225703Sbloom }
86317767Sralph 
86425703Sbloom 
86525703Sbloom /*
86625703Sbloom  * Check debugging requests, and open RMTDEBUG audit file if necessary. If an
86725703Sbloom  * audit file is needed, the parm argument indicates how to create the file:
86825703Sbloom  *
86925703Sbloom  *	DBG_TEMP  - Open a temporary file, with filename = RMTDEBUG/pid.
87025703Sbloom  *	DBG_PERM  - Open a permanent audit file, filename = RMTDEBUG/Rmtname.
87125703Sbloom  *		    If a temp file already exists, it is mv'ed to be permanent.
87225703Sbloom  *	DBG_CLEAN - Cleanup; unlink temp files.
87325703Sbloom  *
87425703Sbloom  * Restrictions - this code can only cope with one open debug file at a time.
87525703Sbloom  * Each call creates a new file; if an old one of the same name exists it will
87625703Sbloom  * be overwritten.
87725703Sbloom  */
87825703Sbloom setdebug(parm)
87925703Sbloom int parm;
88025703Sbloom {
88125703Sbloom 	char buf[BUFSIZ];		/* Buffer for building filenames     */
88225703Sbloom 	static char *temp = NULL;	/* Ptr to temporary file name	     */
88325703Sbloom 	static int auditopen = 0;	/* Set to 1 when we open a file	     */
88425703Sbloom 	struct stat stbuf;		/* File status buffer		     */
88525703Sbloom 
88625703Sbloom 	/*
88725703Sbloom 	 * If movement or cleanup of a temp file is indicated, we do it no
88825703Sbloom 	 * matter what.
88925703Sbloom 	 */
89025703Sbloom 	if (temp != CNULL && parm == DBG_PERM) {
89125703Sbloom 		sprintf(buf, "%s/%s", RMTDEBUG, Rmtname);
89225703Sbloom 		unlink(buf);
89325703Sbloom 		if (link(temp, buf) != 0) {
89423590Sbloom 			Debug = 0;
89533945Srick 			syslog(LOG_ERR, "RMTDEBUG link(%s,%s) failed: %m",
89633945Srick 				temp, buf);
89733945Srick 			cleanup(FAIL);
89825703Sbloom 		}
89925703Sbloom 		parm = DBG_CLEAN;
90023590Sbloom 	}
90125703Sbloom 	if (parm == DBG_CLEAN) {
90225703Sbloom 		if (temp != CNULL) {
90325703Sbloom 			unlink(temp);
90425703Sbloom 			free(temp);
90525703Sbloom 			temp = CNULL;
90625703Sbloom 		}
90725703Sbloom 		return;
90825703Sbloom 	}
90925703Sbloom 
91025703Sbloom 	if (Debug == 0)
91125703Sbloom 		return;		/* Gotta be in debug to come here.   */
91225703Sbloom 
91325703Sbloom 	/*
91425703Sbloom 	 * If we haven't opened a file already, we can just return if it's
91525703Sbloom 	 * alright to use the stderr we came in with. We can if:
91625703Sbloom 	 *
91725703Sbloom 	 *	Role == MASTER, and Stderr is a regular file, a TTY or a pipe.
91825703Sbloom 	 *
91925703Sbloom 	 * Caution: Detecting when stderr is a pipe is tricky, because the 4.2
92025703Sbloom 	 * man page for fstat(2) disagrees with reality, and System V leaves it
92125703Sbloom 	 * undefined, which means different implementations act differently.
92225703Sbloom 	 */
92325703Sbloom 	if (!auditopen && Role == MASTER) {
92425703Sbloom 		if (isatty(fileno(stderr)))
92525703Sbloom 			return;
92625703Sbloom 		else if (fstat(fileno(stderr), &stbuf) == 0) {
92725703Sbloom #ifdef USG
92825703Sbloom 			/* Is Regular File or Fifo   */
92925703Sbloom 			if ((stbuf.st_mode & 0060000) == 0)
93025703Sbloom 				return;
93125703Sbloom #else !USG
93217767Sralph #ifdef BSD4_2
93325703Sbloom 					/* Is Regular File */
93425703Sbloom 			if ((stbuf.st_mode & S_IFMT) == S_IFREG ||
93525703Sbloom 			    stbuf.st_mode == 0)		/* Is a pipe */
93625703Sbloom 				return;
93725703Sbloom #else !BSD4_2
93825703Sbloom 					 /* Is Regular File or Pipe  */
93925703Sbloom 			if ((stbuf.st_mode & S_IFMT) == S_IFREG)
94025703Sbloom 				return;
94125703Sbloom #endif BSD4_2
94225703Sbloom #endif USG
94325703Sbloom 		}
94417767Sralph 	}
94513639Ssam 
94625703Sbloom 	/*
94725703Sbloom 	 * We need RMTDEBUG directory to do auditing. If the file doesn't exist,
94825703Sbloom 	 * then we forget about debugging; if it exists but has improper owner-
94925703Sbloom 	 * ship or modes, we gripe about it in ERRLOG.
95025703Sbloom 	 */
95125703Sbloom 	if (stat(RMTDEBUG, &stbuf) != SUCCESS) {
95225703Sbloom 		Debug = 0;
95325703Sbloom 		return;
95425703Sbloom 	}
95525703Sbloom 	if ((geteuid() != stbuf.st_uid) ||	  	/* We must own it    */
95625703Sbloom 	    ((stbuf.st_mode & 0170700) != 040700)) {	/* Directory, rwx    */
95725703Sbloom 		Debug = 0;
95833945Srick 		syslog(LOG_ERR, "%s: invalid directory mode: %o", RMTDEBUG,
95933945Srick 			stbuf.st_mode);
96025703Sbloom 		return;
96125703Sbloom 	}
96213639Ssam 
96325703Sbloom 	if (parm == DBG_TEMP) {
96425703Sbloom 		sprintf(buf, "%s/%d", RMTDEBUG, getpid());
96525703Sbloom 		temp = malloc(strlen (buf) + 1);
96625703Sbloom 		if (temp == CNULL) {
96725703Sbloom 			Debug = 0;
96833945Srick 			syslog(LOG_ERR, "RMTDEBUG malloc failed: %m");
96933945Srick 			cleanup(FAIL);
97025703Sbloom 		}
97125703Sbloom 		strcpy(temp, buf);
97225703Sbloom 	} else
97325703Sbloom 		sprintf(buf, "%s/%s", RMTDEBUG, Rmtname);
97413639Ssam 
97525703Sbloom 	unlink(buf);
97625703Sbloom 	if (freopen(buf, "w", stderr) != stderr) {
97725703Sbloom 		Debug = 0;
97833945Srick 		syslog(LOG_ERR, "RMTDEBUG freopen(%s) failed: %m", buf);
97933945Srick 		cleanup(FAIL);
98025703Sbloom 	}
98125703Sbloom 	setbuf(stderr, CNULL);
98225703Sbloom 	auditopen = 1;
98313639Ssam }
98413639Ssam 
98523590Sbloom /*
98625703Sbloom  *	catch SIGALRM routine
98713639Ssam  */
98813639Ssam timeout()
98913639Ssam {
99023590Sbloom 	extern int HaveSentHup;
99123590Sbloom 	if (!HaveSentHup) {
99223590Sbloom 		logent(Rmtname, "TIMEOUT");
99323590Sbloom 		if (*Rmtname && strncmp(Rmtname, Myname, MAXBASENAME)) {
99423590Sbloom 			US_SST(us_s_tmot);
99523590Sbloom 			systat(Rmtname, SS_FAIL, "TIMEOUT");
99623590Sbloom 		}
99717767Sralph 	}
99813639Ssam 	longjmp(Sjbuf, 1);
99913639Ssam }
100013639Ssam 
100113639Ssam static char *
100213639Ssam pskip(p)
100313639Ssam register char *p;
100413639Ssam {
100517767Sralph 	while(*p && *p != ' ')
100613639Ssam 		++p;
100723590Sbloom 	while(*p && *p == ' ')
100817767Sralph 		*p++ = 0;
100917767Sralph 	return p;
101013639Ssam }
101134164Srick 
101234164Srick /*
101334164Srick  * clobber argv so ps will show what we're doing.
101434164Srick  * stolen from sendmail
101534164Srick  */
101634164Srick /*VARARGS1*/
101734164Srick setproctitle(fmt, a, b, c)
101834164Srick char *fmt;
101934164Srick {
102034164Srick #ifdef SETPROCTITLE
102134164Srick 	register char *p;
102234164Srick 	register int i;
102334164Srick 	extern char **Argv;
102434164Srick 	extern char *LastArgv;
102534164Srick 	char buf[BUFSIZ];
102634164Srick 
102734164Srick 	(void) sprintf(buf, fmt, a, b, c);
102834164Srick 
102934164Srick 	/* make ps print "(sendmail)" */
103034164Srick 	p = Argv[0];
103134164Srick 	*p++ = '-';
103234164Srick 
103334164Srick 	i = strlen(buf);
103434164Srick 	if (i > LastArgv - p - 2) {
103534164Srick 		i = LastArgv - p - 2;
103634164Srick 		buf[i] = '\0';
103734164Srick 	}
103834164Srick 	(void) strcpy(p, buf);
103934164Srick 	p += i;
104034164Srick 	while (p < LastArgv)
104134164Srick 		*p++ = ' ';
104234164Srick #endif SETPROCTITLE
104334164Srick }
1044