xref: /illumos-gate/usr/src/cmd/bnu/uucico.c (revision 73a801acfbef73aca0012eca23d6a1b842f5a104)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2014 Garrett D'Amore
23  */
24 /*
25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34 
35  * uucp file transfer program:
36  * to place a call to a remote machine, login, and
37  * copy files between the two machines.
38 
39 */
40 /*
41  * Added check to limit the total number of uucicos as defined
42  * in the Limits file.
43  *
44  * Added -f flag to "force execution", ignoring the limit on the
45  * number of uucicos. This will be used when invoking uucico from
46  * Uutry.
47 */
48 
49 #include "uucp.h"
50 #include "log.h"
51 
52 #ifndef	V7
53 #include <sys/mkdev.h>
54 #endif /* V7 */
55 
56 #ifdef TLI
57 #include	<sys/tiuser.h>
58 #endif /* TLI */
59 
60 jmp_buf Sjbuf;
61 extern unsigned msgtime;
62 char	uuxqtarg[MAXBASENAME] = {'\0'};
63 int	uuxqtflag = 0;
64 int	Dologin = 0;
65 
66 extern int	(*Setup)(), (*Teardown)();	/* defined in interface.c */
67 
68 #define USAGE	"Usage: %s [-x NUM] [-r [0|1]] -s SYSTEM -u USERID -d SPOOL -i INTERFACE [-f]\n"
69 extern void closedem();
70 void cleanup(), cleanTM();
71 
72 extern int sysaccess(), guinfo(), eaccess(), countProcs(), interface(),
73 	savline(), omsg(), restline(), imsg(), callok(), gnxseq(),
74 	cmtseq(), conn(), startup(), cntrl();
75 extern void setuucp(), fixline(), gename(), ulkseq(), pfEndfile();
76 
77 #ifdef	NOSTRANGERS
78 static void checkrmt();		/* See if we want to talk to remote. */
79 #endif /* NOSTRANGERS */
80 
81 extern char *Mytype;
82 
83 static char *pskip();
84 
85 int
86 main(argc, argv, envp)
87 int argc;
88 char *argv[];
89 char **envp;
90 {
91 
92 	extern void intrEXIT(), onintr(), timeout();
93 	extern void setservice();
94 #ifndef ATTSVR3
95 	void setTZ();
96 #endif /* ATTSVR3 */
97 	int ret, seq, exitcode;
98 	char file[NAMESIZE];
99 	char msg[BUFSIZ], *p, *q;
100 	char xflag[6];	/* -xN N is single digit */
101 	char *ttyn;
102 	char *iface;	/* interface name	*/
103 	char	cb[128];
104 	time_t	ts, tconv;
105 	char lockname[MAXFULLNAME];
106 	struct limits limitval;
107 	int maxnumb;
108 	int force = 0;	/* set to force execution, ignoring uucico limit */
109 	char gradedir[2*NAMESIZE];
110 
111 	/* Set locale environment variables local definitions */
112 	(void) setlocale(LC_ALL, "");
113 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
114 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
115 #endif
116 	(void) textdomain(TEXT_DOMAIN);
117 
118 	Ulimit = ulimit(1,0L);
119 	Uid = getuid();
120 	Euid = geteuid();	/* this should be UUCPUID */
121 	if (Uid == 0)
122 	    setuid(UUCPUID);
123 	Env = envp;
124 	Role = SLAVE;
125 	strcpy(Logfile, LOGCICO);
126 	*Rmtname = NULLCHAR;
127 	Ifn = Ofn = -1;		/* must be set before signal handlers */
128 
129 	closedem();
130 	time(&Nstat.t_qtime);
131 	tconv = Nstat.t_start = Nstat.t_qtime;
132 	strcpy(Progname, "uucico");
133 	setservice(Progname);
134 	ret = sysaccess(EACCESS_SYSTEMS);
135 	ASSERT(ret == 0, Ct_OPEN, "Systems", ret);
136 	ret = sysaccess(EACCESS_DEVICES);
137 	ASSERT(ret == 0, Ct_OPEN, "Devices", ret);
138 	ret = sysaccess(EACCESS_DIALERS);
139 	ASSERT(ret == 0, Ct_OPEN, "Dialers", ret);
140 	Pchar = 'C';
141 	(void) signal(SIGILL, intrEXIT);
142 	(void) signal(SIGTRAP, intrEXIT);
143 	(void) signal(SIGIOT, intrEXIT);
144 	(void) signal(SIGEMT, intrEXIT);
145 	(void) signal(SIGFPE, intrEXIT);
146 	(void) signal(SIGBUS, intrEXIT);
147 	(void) signal(SIGSEGV, intrEXIT);
148 	(void) signal(SIGSYS, intrEXIT);
149 	if (signal(SIGPIPE, SIG_IGN) != SIG_IGN)	/* This for sockets */
150 		(void) signal(SIGPIPE, intrEXIT);
151 	(void) signal(SIGINT, onintr);
152 	(void) signal(SIGHUP, onintr);
153 	(void) signal(SIGQUIT, onintr);
154 	(void) signal(SIGTERM, onintr);
155 #ifdef SIGUSR1
156 	(void) signal(SIGUSR1, SIG_IGN);
157 #endif
158 #ifdef SIGUSR2
159 	(void) signal(SIGUSR2, SIG_IGN);
160 #endif
161 #ifdef BSD4_2
162 	(void) sigsetmask(sigblock(0) & ~(1 << (SIGALRM - 1)));
163 #endif /*BSD4_2*/
164 
165 	pfInit();
166 	scInit("xfer");
167 	ret = guinfo(Euid, User);
168 	ASSERT(ret == 0, "BAD UID ", "", ret);
169 	strncpy(Uucp, User, NAMESIZE);
170 
171 	setuucp(User);
172 
173 	*xflag = NULLCHAR;
174 	iface = "UNIX";
175 
176 	while ((ret = getopt(argc, argv, "fd:c:r:s:x:u:i:")) != EOF) {
177 		switch (ret) {
178 		case 'd':
179 			if ( eaccess(optarg, 01) != 0 ) {
180 				(void) fprintf(stderr, gettext("%s: cannot"
181 				    " access spool directory %s\n"),
182 					Progname, optarg);
183 				exit(1);
184 			}
185 			Spool = optarg;
186 			break;
187 		case 'c':
188 			Mytype = optarg;
189 			break;
190 		case 'f':
191 			++force;
192 			break;
193 		case 'r':
194 			if ( (Role = atoi(optarg)) != MASTER && Role != SLAVE ) {
195 				(void) fprintf(stderr, gettext("%s: bad value"
196 				    " '%s' for -r argument\n" USAGE),
197 					Progname, optarg, Progname);
198 				exit(1);
199 			}
200 			break;
201 		case 's':
202 			strncpy(Rmtname, optarg, MAXFULLNAME-1);
203 			if (versys(Rmtname)) {
204 			    (void) fprintf(stderr,
205 				gettext("%s: %s not in Systems file\n"),
206 				Progname, optarg);
207 			    cleanup(101);
208 			}
209 			/* set args for possible xuuxqt call */
210 			strcpy(uuxqtarg, Rmtname);
211 			/* if versys put a longer name in, truncate it again */
212 			Rmtname[MAXBASENAME] = '\0';
213 			break;
214 		case 'x':
215 			Debug = atoi(optarg);
216 			if (Debug <= 0)
217 				Debug = 1;
218 			if (Debug > 9)
219 				Debug = 9;
220 			(void) sprintf(xflag, "-x%d", Debug);
221 			break;
222 		case 'u':
223 			DEBUG(4, "Loginuser %s specified\n", optarg);
224 			strncpy(Loginuser, optarg, NAMESIZE);
225 			Loginuser[NAMESIZE - 1] = NULLCHAR;
226 			break;
227 		case 'i':
228 			/*	interface type		*/
229 			iface = optarg;
230 			break;
231 		default:
232 			(void) fprintf(stderr, gettext(USAGE), Progname);
233 			exit(1);
234 		}
235 	}
236 
237 	if (Role == MASTER || *Loginuser == NULLCHAR) {
238 	    ret = guinfo(Uid, Loginuser);
239 	    ASSERT(ret == 0, "BAD LOGIN_UID ", "", ret);
240 	}
241 
242 	/* limit the total number of uucicos */
243 	if (force) {
244 	    DEBUG(4, "force flag set (ignoring uucico limit)\n%s", "");
245 	} else if (scanlimit("uucico", &limitval) == FAIL) {
246 	    DEBUG(1, "No limits for uucico in %s\n", LIMITS);
247 	} else {
248 	    maxnumb = limitval.totalmax;
249 	    if (maxnumb < 0) {
250 		DEBUG(4, "Non-positive limit for uucico in %s\n", LIMITS);
251 		DEBUG(1, "No limits for uucico\n%s", "");
252 	    } else {
253 		DEBUG(4, "Uucico limit %d -- ", maxnumb);
254 		(void) sprintf(lockname, "%s.", LOCKPRE);
255 		if (countProcs(lockname, (maxnumb-1)) == FALSE) {
256 			DEBUG(4, "exiting\n%s", "");
257 			cleanup(101);
258 		}
259 		DEBUG(4, "continuing\n%s", "");
260 	    }
261 	}
262 
263 	pfStrtConn((Role == MASTER) ? 'M' : 'S');
264 	if (Role == MASTER) {
265 	    if (*Rmtname == NULLCHAR) {
266 		DEBUG(5, "No -s specified\n%s" , "");
267 		cleanup(101);
268 	    }
269 	    /* get Myname - it depends on who I'm calling--Rmtname */
270 	    (void) mchFind(Rmtname);
271 	    myName(Myname);
272 	    if (EQUALSN(Rmtname, Myname, MAXBASENAME)) {
273 		DEBUG(5, "This system specified: -sMyname: %s, ", Myname);
274 		cleanup(101);
275 	    }
276 	    acInit("xfer");
277 	}
278 
279 	ASSERT(chdir(Spool) == 0, Ct_CHDIR, Spool, errno);
280 	strcpy(Wrkdir, Spool);
281 
282 	scReqsys((Role == MASTER) ? Myname : Rmtname); /* log requestor system */
283 
284 	if (Role == SLAVE) {
285 
286 #ifndef ATTSVR3
287 		setTZ();
288 #endif /* ATTSVR3 */
289 
290 		if (freopen(RMTDEBUG, "a", stderr) == 0) {
291 			errent(Ct_OPEN, RMTDEBUG, errno, __FILE__, __LINE__);
292 			freopen("/dev/null", "w", stderr);
293 		}
294 		if ( interface(iface) ) {
295 			(void)fprintf(stderr,
296 			"%s: invalid interface %s\n", Progname, iface);
297 			cleanup(101);
298 		}
299 		/*master setup will be called from processdev()*/
300 		if ( (*Setup)( Role, &Ifn, &Ofn ) ) {
301 			DEBUG(5, "SLAVE Setup failed%s", "");
302 			cleanup(101);
303 		}
304 
305 		/*
306 		 * initial handshake
307 		 */
308 		(void) savline();
309 		fixline(Ifn, 0, D_ACU);
310 		/* get MyName - use logFind to check PERMISSIONS file */
311 		(void) logFind(Loginuser, "");
312 		myName(Myname);
313 
314 		DEBUG(4,"cico.c: Myname - %s\n",Myname);
315 		DEBUG(4,"cico.c: Loginuser - %s\n",Loginuser);
316 		fflush(stderr);
317 		Nstat.t_scall = times(&Nstat.t_tga);
318 		(void) sprintf(msg, "here=%s", Myname);
319 		omsg('S', msg, Ofn);
320 		(void) signal(SIGALRM, timeout);
321 		(void) alarm(msgtime); /* give slow machines a second chance */
322 		if (setjmp(Sjbuf)) {
323 
324 			/*
325 			 * timed out
326 			 */
327 			(void) restline();
328 			rmlock(CNULL);
329 			exit(0);
330 		}
331 		for (;;) {
332 			ret = imsg(msg, Ifn);
333 			if (ret != 0) {
334 				(void) alarm(0);
335 				(void) restline();
336 				rmlock(CNULL);
337 				exit(0);
338 			}
339 			if (msg[0] == 'S')
340 				break;
341 		}
342 		Nstat.t_ecall = times(&Nstat.t_tga);
343 		(void) alarm(0);
344 		q = &msg[1];
345 		p = pskip(q);
346 		strncpy(Rmtname, q, MAXBASENAME);
347 		Rmtname[MAXBASENAME] = '\0';
348 
349 		seq = 0;
350 		while (p && *p == '-') {
351 			q = pskip(p);
352 			switch(*(++p)) {
353 			case 'x':
354 				Debug = atoi(++p);
355 				if (Debug <= 0)
356 					Debug = 1;
357 				(void) sprintf(xflag, "-x%d", Debug);
358 				break;
359 			case 'Q':
360 				seq = atoi(++p);
361 				if (seq < 0)
362 					seq = 0;
363 				break;
364 #ifdef MAXGRADE
365 			case 'v':	/* version -- -vname=val or -vname */
366 				if (strncmp(++p, "grade=", 6) == 0 &&
367 				    isalnum(p[6]))
368 					MaxGrade = p[6];
369 				break;
370 #endif /* MAXGRADE */
371 			case 'R':
372 				Restart++;
373 				p++;
374 				break;
375 			case 'U':
376 				SizeCheck++;
377 				RemUlimit = strtol(++p, (char **) NULL,0);
378 				break;
379 			default:
380 				break;
381 			}
382 			p = q;
383 		}
384 		DEBUG(4, "sys-%s\n", Rmtname);
385 		if (strpbrk(Rmtname, Shchar) != NULL) {
386 			DEBUG(4, "Bad remote system name '%s'\n", Rmtname);
387 			logent(Rmtname, "BAD REMOTE SYSTEM NAME");
388 			omsg('R', "Bad remote system name", Ofn);
389 			cleanup(101);
390 		}
391 		if (Restart)
392 		    CDEBUG(1,"Checkpoint Restart enabled\n%s", "");
393 
394 #ifdef NOSTRANGERS
395 		checkrmt();	/* Do we know the remote system. */
396 #else
397 		(void) versys(Rmtname);	/* in case the real name is longer */
398 #endif /* NOSTRANGERS */
399 
400 		(void) sprintf(lockname, "%ld", (long) getpid());
401 		if (umlock(LOCKPRE, lockname)) {
402 			omsg('R', "LCK", Ofn);
403 			cleanup(101);
404 		}
405 
406 		/* validate login using PERMISSIONS file */
407 		if (logFind(Loginuser, Rmtname) == FAIL) {
408 			scWrite(); /* log security violation */
409 			Uerror = SS_BAD_LOG_MCH;
410 			logent(UERRORTEXT, "FAILED");
411 			systat(Rmtname, SS_BAD_LOG_MCH, UERRORTEXT,
412 			    Retrytime);
413 			omsg('R', "LOGIN", Ofn);
414 			cleanup(101);
415 		}
416 
417 		ret = callBack();
418 		DEBUG(4,"return from callcheck: %s",ret ? "TRUE" : "FALSE");
419 		if (ret==TRUE) {
420 			(void) signal(SIGINT, SIG_IGN);
421 			(void) signal(SIGHUP, SIG_IGN);
422 			omsg('R', "CB", Ofn);
423 			logent("CALLBACK", "REQUIRED");
424 			/*
425 			 * set up for call back
426 			 */
427 			chremdir(Rmtname);
428 			(void) sprintf(file, "%s/%c", Rmtname, D_QUEUE);
429 			chremdir(file);
430 			gename(CMDPRE, Rmtname, 'C', file);
431 			(void) close(creat(file, CFILEMODE));
432 			if (callok(Rmtname) == SS_CALLBACK_LOOP) {
433 			    systat(Rmtname, SS_CALLBACK_LOOP, "CALL BACK - LOOP", Retrytime);
434 			} else {
435 			    systat(Rmtname, SS_CALLBACK, "CALL BACK", Retrytime);
436 			    xuucico(Rmtname);
437 			}
438 			cleanup(101);
439 		}
440 
441 		if (callok(Rmtname) == SS_SEQBAD) {
442 			Uerror = SS_SEQBAD;
443 			logent(UERRORTEXT, "PREVIOUS");
444 			omsg('R', "BADSEQ", Ofn);
445 			cleanup(101);
446 		}
447 
448 		if (gnxseq(Rmtname) == seq) {
449 			if (Restart) {
450 			    if (SizeCheck)
451 				(void) sprintf (msg, "OK -R -U0x%lx %s",
452 					Ulimit, xflag);
453 			    else
454 				(void) sprintf (msg, "OK -R %s", xflag);
455 			    omsg('R', msg, Ofn);
456 			} else
457 			    omsg('R', "OK", Ofn);
458 			(void) cmtseq();
459 		} else {
460 			Uerror = SS_SEQBAD;
461 			systat(Rmtname, SS_SEQBAD, UERRORTEXT, Retrytime);
462 			logent(UERRORTEXT, "HANDSHAKE FAILED");
463 			ulkseq();
464 			omsg('R', "BADSEQ", Ofn);
465 			cleanup(101);
466 		}
467 		ttyn = ttyname(Ifn);
468 		if (ttyn != CNULL && *ttyn != NULLCHAR) {
469 			struct stat ttysbuf;
470 			if ( fstat(Ifn,&ttysbuf) == 0 )
471 				Dev_mode = ttysbuf.st_mode;
472 			else
473 				Dev_mode = R_DEVICEMODE;
474 			if ( EQUALSN(ttyn,"/dev/",5) )
475 			    strcpy(Dc, ttyn+5);
476 			else
477 			    strcpy(Dc, ttyn);
478 			chmod(ttyn, S_DEVICEMODE);
479 		} else
480 			strcpy(Dc, "notty");
481 		/* set args for possible xuuxqt call */
482 		strcpy(uuxqtarg, Rmtname);
483 	}
484 
485 	strcpy(User, Uucp);
486 /*
487  *  Ensure reasonable ulimit (MINULIMIT)
488  */
489 
490 #ifndef	V7
491 	{
492 	long 	minulimit;
493 	minulimit = ulimit(1, (long) 0);
494 	ASSERT(minulimit >= MINULIMIT, "ULIMIT TOO SMALL",
495 	    Loginuser, (int) minulimit);
496 	}
497 #endif
498 	if (Role == MASTER && callok(Rmtname) != 0) {
499 		logent("SYSTEM STATUS", "CAN NOT CALL");
500 		cleanup(101);
501 	}
502 
503 	chremdir(Rmtname);
504 
505 	(void) strcpy(Wrkdir, RemSpool);
506 	if (Role == MASTER) {
507 
508 		/*
509 		 * master part
510 		 */
511 		(void) signal(SIGINT, SIG_IGN);
512 		(void) signal(SIGHUP, SIG_IGN);
513 		(void) signal(SIGQUIT, SIG_IGN);
514 		if (Ifn != -1 && Role == MASTER) {
515 			(void) (*Write)(Ofn, EOTMSG, strlen(EOTMSG));
516 			(void) close(Ofn);
517 			(void) close(Ifn);
518 			Ifn = Ofn = -1;
519 			rmlock(CNULL);
520 			sleep(3);
521 		}
522 
523 		/*
524 		 * Find the highest priority job grade that has
525 		 * jobs to do. This is needed to form the lock name.
526 		 */
527 
528 		findgrade(RemSpool, JobGrade);
529 		DEBUG(4, "Job grade to process - %s\n", JobGrade);
530 
531 		/*
532 		 * Lock the job grade if there is one to process.
533 		 */
534 
535 		if (*JobGrade != NULLCHAR) {
536 			(void) sprintf(gradedir, "%s/%s", Rmtname, JobGrade);
537 			chremdir(gradedir);
538 
539 			(void) sprintf(lockname, "%.*s.%s", SYSNSIZE, Rmtname, JobGrade);
540 			(void) sprintf(msg, "call to %s - process job grade %s ",
541 			    Rmtname, JobGrade);
542 			if (umlock(LOCKPRE, lockname) != 0) {
543 				logent(msg, "LOCKED");
544 				CDEBUG(1, "Currently Talking With %s\n",
545 				    Rmtname);
546  				cleanup(100);
547 			}
548 		} else {
549 			(void) sprintf(msg, "call to %s - no work", Rmtname);
550 		}
551 
552 		Nstat.t_scall = times(&Nstat.t_tga);
553 		Ofn = Ifn = conn(Rmtname);
554 		Nstat.t_ecall = times(&Nstat.t_tga);
555 		if (Ofn < 0) {
556 			delock(LOCKPRE, lockname);
557 			logent(UERRORTEXT, "CONN FAILED");
558 			systat(Rmtname, Uerror, UERRORTEXT, Retrytime);
559 			cleanup(101);
560 		} else {
561 			logent(msg, "SUCCEEDED");
562 			ttyn = ttyname(Ifn);
563 			if (ttyn != CNULL && *ttyn != NULLCHAR) {
564 				struct stat ttysbuf;
565 				if ( fstat(Ifn,&ttysbuf) == 0 )
566 					Dev_mode = ttysbuf.st_mode;
567 				else
568 					Dev_mode = R_DEVICEMODE;
569 				chmod(ttyn, M_DEVICEMODE);
570 			}
571 		}
572 
573 		if (setjmp(Sjbuf)) {
574 			delock(LOCKPRE, lockname);
575 			Uerror = SS_LOGIN_FAILED;
576 			logent(Rmtname, UERRORTEXT);
577 			systat(Rmtname, SS_LOGIN_FAILED,
578 			    UERRORTEXT, Retrytime);
579 			DEBUG(4, "%s - failed\n", UERRORTEXT);
580 			cleanup(101);
581 		}
582 		(void) signal(SIGALRM, timeout);
583 		/* give slow guys lots of time to thrash */
584 		(void) alarm(2 * msgtime);
585 		for (;;) {
586 			ret = imsg(msg, Ifn);
587 			if (ret != 0) {
588 				continue; /* try again */
589 			}
590 			if (msg[0] == 'S')
591 				break;
592 		}
593 		(void) alarm(0);
594 		if(EQUALSN("here=", &msg[1], 5)){
595 			/* This may be a problem, we check up to MAXBASENAME
596 			 * characters now. The old comment was:
597 			 * this is a problem.  We'd like to compare with an
598 			 * untruncated Rmtname but we fear incompatability.
599 			 * So we'll look at most 6 chars (at most).
600 			 */
601 			(void) pskip(&msg[6]);
602 			if (!EQUALSN(&msg[6], Rmtname, MAXBASENAME)) {
603 				delock(LOCKPRE, lockname);
604 				Uerror = SS_WRONG_MCH;
605 				logent(&msg[6], UERRORTEXT);
606 				systat(Rmtname, SS_WRONG_MCH, UERRORTEXT,
607 				     Retrytime);
608 				DEBUG(4, "%s - failed\n", UERRORTEXT);
609 				cleanup(101);
610 			}
611 		}
612 		CDEBUG(1,"Login Successful: System=%s\n",&msg[6]);
613 		seq = gnxseq(Rmtname);
614 		(void) sprintf(msg, "%s -Q%d -R -U0x%lx %s",
615 			Myname, seq, Ulimit, xflag);
616 #ifdef MAXGRADE
617 		if (MaxGrade != NULLCHAR) {
618 			p = strchr(msg, NULLCHAR);
619 			sprintf(p, " -vgrade=%c", MaxGrade);
620 		}
621 #endif /* MAXGRADE */
622 		omsg('S', msg, Ofn);
623 		(void) alarm(msgtime);	/* give slow guys some thrash time */
624 		for (;;) {
625 			ret = imsg(msg, Ifn);
626 			DEBUG(4, "msg-%s\n", msg);
627 			if (ret != 0) {
628 				(void) alarm(0);
629 				delock(LOCKPRE, lockname);
630 				ulkseq();
631 				cleanup(101);
632 			}
633 			if (msg[0] == 'R')
634 				break;
635 		}
636 		(void) alarm(0);
637 
638 		/*  check for rejects from remote */
639 		Uerror = 0;
640 		if (EQUALS(&msg[1], "LCK"))
641 			Uerror = SS_RLOCKED;
642 		else if (EQUALS(&msg[1], "LOGIN"))
643 			Uerror = SS_RLOGIN;
644 		else if (EQUALS(&msg[1], "CB"))
645 			Uerror = (callBack() ? SS_CALLBACK_LOOP : SS_CALLBACK);
646 		else if (EQUALS(&msg[1], "You are unknown to me"))
647 			Uerror = SS_RUNKNOWN;
648 		else if (EQUALS(&msg[1], "BADSEQ"))
649 			Uerror = SS_SEQBAD;
650 		else if (!EQUALSN(&msg[1], "OK", 2))
651 			Uerror = SS_UNKNOWN_RESPONSE;
652 		if (Uerror)  {
653 			delock(LOCKPRE, lockname);
654 			systat(Rmtname, Uerror, UERRORTEXT, Retrytime);
655 			logent(UERRORTEXT, "HANDSHAKE FAILED");
656 			CDEBUG(1, "HANDSHAKE FAILED: %s\n", UERRORTEXT);
657 			ulkseq();
658 			cleanup(101);
659 		}
660 		(void) cmtseq();
661 
662 		/*
663 		 * See if we have any additional parameters on the OK
664 		 */
665 
666 		if (strlen(&msg[3])) {
667 			p = pskip(&msg[3]);
668 			while (p && *p == '-') {
669 				q = pskip(p);
670 				switch(*(++p)) {
671 				case 'R':
672 					Restart++;
673 					p++;
674 					break;
675 				case 'U':
676 					SizeCheck++;
677 					RemUlimit = strtol(++p, (char **) NULL, 0);
678 					break;
679 				case 'x':
680 					if (!Debug) {
681 						Debug = atoi(++p);
682 						if (Debug <= 0)
683 							Debug = 1;
684 					}
685 					break;
686 				default:
687 					break;
688 				}
689 				p = q;
690 			}
691 		}
692 
693 	}
694 	DEBUG(4, " Rmtname %s, ", Rmtname);
695 	DEBUG(4, " Restart %s, ", (Restart ? "YES" : "NO"));
696 	DEBUG(4, "Role %s,  ", Role ? "MASTER" : "SLAVE");
697 	DEBUG(4, "Ifn - %d, ", Ifn);
698 	DEBUG(4, "Loginuser - %s\n", Loginuser);
699 
700 	/* alarm/setjmp added here due to experience with uucico
701 	 * hanging for hours in imsg().
702 	 */
703 	if (setjmp(Sjbuf)) {
704 		delock(LOCKPRE, lockname);
705 		logent("startup", "TIMEOUT");
706 		DEBUG(4, "%s - timeout\n", "startup");
707 		cleanup(101);
708 	}
709 	(void) alarm(MAXSTART);
710 	ret = startup();
711 	(void) alarm(0);
712 
713 	if (ret != SUCCESS) {
714 		delock(LOCKPRE, lockname);
715 		logent("startup", "FAILED");
716 		Uerror = SS_STARTUP;
717 		CDEBUG(1, "%s\n", UERRORTEXT);
718 		systat(Rmtname, Uerror, UERRORTEXT, Retrytime);
719 		exitcode = 101;
720 	} else {
721 		pfConnected(Rmtname, Dc);
722 		acConnected(Rmtname, Dc);
723 		logent("startup", "OK");
724 		systat(Rmtname, SS_INPROGRESS, UTEXT(SS_INPROGRESS),Retrytime);
725 		Nstat.t_sftp = times(&Nstat.t_tga);
726 
727 		exitcode = cntrl();
728 		Nstat.t_eftp = times(&Nstat.t_tga);
729 		DEBUG(4, "cntrl - %d\n", exitcode);
730 		(void) signal(SIGINT, SIG_IGN);
731 		(void) signal(SIGHUP, SIG_IGN);
732 		(void) signal(SIGALRM, timeout);
733 
734 		if (exitcode == 0) {
735 			(void) time(&ts);
736 			(void) sprintf(cb, "conversation complete %s %ld",
737 				Dc, ts - tconv);
738 			logent(cb, "OK");
739 			systat(Rmtname, SS_OK, UTEXT(SS_OK), Retrytime);
740 
741 		} else {
742 			logent("conversation complete", "FAILED");
743 			systat(Rmtname, SS_CONVERSATION,
744 			    UTEXT(SS_CONVERSATION), Retrytime);
745 		}
746 		(void) alarm(msgtime);	/* give slow guys some thrash time */
747 		omsg('O', "OOOOO", Ofn);
748 		CDEBUG(4, "send OO %d,", ret);
749 		if (!setjmp(Sjbuf)) {
750 			for (;;) {
751 				omsg('O', "OOOOO", Ofn);
752 				ret = imsg(msg, Ifn);
753 				if (ret != 0)
754 					break;
755 				if (msg[0] == 'O')
756 					break;
757 			}
758 		}
759 		(void) alarm(0);
760 	}
761 	cleanup(exitcode);
762 	/*NOTREACHED*/
763 	return (0);
764 }
765 
766 /*
767  * clean and exit with "code" status
768  */
769 void
770 cleanup(code)
771 int code;
772 {
773 	(void) signal(SIGINT, SIG_IGN);
774 	(void) signal(SIGHUP, SIG_IGN);
775 	rmlock(CNULL);
776 	closedem();
777 	alarm(msgtime);		/* Start timer in case closes hang. */
778 	if (setjmp(Sjbuf) == 0)
779 		(*Teardown)( Role, Ifn, Ofn );
780 	alarm(0);			/* Turn off timer. */
781 	DEBUG(4, "exit code %d\n", code);
782 	CDEBUG(1, "Conversation Complete: Status %s\n\n",
783 	    code ? "FAILED" : "SUCCEEDED");
784 
785 	cleanTM();
786 	if ((code == 0) && (uuxqtflag == 1))
787 		xuuxqt(uuxqtarg);
788 	exit(code);
789 }
790 
791 short TM_cnt = 0;
792 char TM_name[MAXNAMESIZE];
793 
794 void
795 cleanTM()
796 {
797 	int i;
798 	char tm_name[MAXNAMESIZE];
799 
800 	DEBUG(7,"TM_cnt: %d\n",TM_cnt);
801 	for(i=0; i < TM_cnt; i++) {
802 		(void) sprintf(tm_name, "%s.%3.3d", TM_name, i);
803 		DEBUG(7, "tm_name: %s\n", tm_name);
804 		unlink(tm_name);
805 	}
806 	return;
807 }
808 
809 void
810 TMname(file, pnum)
811 char *file;
812 pid_t pnum;
813 {
814 
815 	(void) sprintf(file, "%s/TM.%.5ld.%.3d", RemSpool, (long) pnum, TM_cnt);
816 	if (TM_cnt == 0)
817 	    (void) sprintf(TM_name, "%s/TM.%.5ld", RemSpool, (long) pnum);
818 	DEBUG(7, "TMname(%s)\n", file);
819 	TM_cnt++;
820 	return;
821 }
822 
823 /*
824  * intrrupt - remove locks and exit
825  */
826 void
827 onintr(inter)
828 int inter;
829 {
830 	char str[30];
831 	/* I'm putting a test for zero here because I saw it happen
832 	 * and don't know how or why, but it seemed to then loop
833 	 * here for ever?
834 	 */
835 	if (inter == 0)
836 	    exit(99);
837 	(void) signal(inter, SIG_IGN);
838 	(void) sprintf(str, "SIGNAL %d", inter);
839 	logent(str, "CAUGHT");
840 	pfEndfile("PARTIAL FILE");
841 	acEnd(PARTIAL); /*stop collecting accounting log */
842 	cleanup(inter);
843 }
844 
845 void
846 intrEXIT(inter)
847 int inter;
848 {
849 	char	cb[20];
850 
851 	(void) sprintf(cb, "SIGNAL %d", inter);
852 	logent("INTREXIT", cb);
853 	(void) signal(SIGIOT, SIG_DFL);
854 	(void) signal(SIGILL, SIG_DFL);
855 	rmlock(CNULL);
856 	closedem();
857 	(void) setuid(Uid);
858 	abort();
859 }
860 
861 /*
862  * catch SIGALRM routine
863  */
864 void
865 timeout()
866 {
867 	longjmp(Sjbuf, 1);
868 }
869 
870 /* skip to next field */
871 static char *
872 pskip(p)
873 char *p;
874 {
875 	if ((p = strchr(p, ' ')) != CNULL)
876 		do
877 			*p++ = NULLCHAR;
878 		while (*p == ' ');
879 	return(p);
880 }
881 
882 void
883 closedem()
884 {
885 	int i, maxfiles;
886 
887 #ifdef ATTSVR3
888 	maxfiles = ulimit(4,0);
889 #else /* !ATTSVR3 */
890 #ifdef BSD4_2
891 	maxfiles = getdtablesize();
892 #else /* BSD4_2 */
893 	maxfiles = _NFILE;
894 #endif /* BSD4_2 */
895 #endif /* ATTSVR3 */
896 
897 	for (  i = 3; i < maxfiles; i++ )
898 		if ( i != Ifn && i != Ofn && i != fileno(stderr) )
899 			(void) close(i);
900 	return;
901 }
902 
903 #ifndef ATTSVR3
904 
905 /*
906  *	setTZ()
907  *
908  *	if login "shell" is uucico (i.e., Role == SLAVE), must set
909  *	timezone env variable TZ.  otherwise will default to EST.
910  */
911 
912 #define	LINELEN	81
913 
914 void
915 setTZ()
916 {
917 	static char	buf[LINELEN], *bp;
918 	extern char	*fgets();
919 	FILE		*tzfp;
920 	extern FILE	*fopen();
921 	int		i;
922 	extern int	fclose(), strncmp();
923 
924 	if ( (tzfp = fopen("/etc/default/init","r")) == (FILE *)NULL )
925 		return;
926 	while ( (bp = fgets(buf,LINELEN,tzfp)) != (char *)NULL ) {
927 		while ( isspace(*bp) )
928 			++bp;
929 		if ( strncmp(bp, "TZ=", 3) == 0 ) {
930 			for ( i = strlen(bp) - 1; i > 0 && isspace(*(bp+i)); --i )
931 				*(bp+i) = '\0';
932 			putenv(bp);
933 			(void)fclose(tzfp);
934 			return;
935 		}
936 	}
937 	(void)fclose(tzfp);
938 	return;
939 }
940 #endif /* ATTSVR3 */
941 
942 #ifdef NOSTRANGERS
943 /*
944 * Function:	checkrmt
945 *
946 * If NOSTRANGERS is defined, see if the remote system is in our systems
947 * file.  If it is not, execute NOSTRANGERS and then reject the call.
948 */
949 
950 static void
951 checkrmt ()
952 
953 {
954 	char **	eVarPtr;	/* Pointer to environment variable. */
955 	char	msgbuf[BUFSIZ];	/* Place to build messages. */
956 	pid_t	procid;		/* ID of Nostranger process. */
957 	static char * safePath = PATH;
958 	int	status;		/* Exit status of child. */
959 	pid_t	waitrv;		/* Return value from wait system call. */
960 
961 	/* here's the place to look the remote system up in the Systems file.
962 	 * If the command NOSTRANGERS is executable and
963 	 * If they're not in my file then hang up */
964 
965 	if (versys(Rmtname) && (access(NOSTRANGERS, 1) == 0)) {
966 		sprintf(msgbuf, "Invoking %s for %%s\n", NOSTRANGERS);
967 		DEBUG(4, msgbuf, Rmtname);
968 
969 		/*
970 		* Ignore hangup in case remote goes away before we can
971 		* finish logging.
972 		*/
973 
974 		(void) signal(SIGHUP, SIG_IGN);
975 		omsg('R', "You are unknown to me", Ofn);
976 		scWrite(); /* log unknown remote system */
977 		procid = fork();
978 		if ( procid == 0 ) {
979 			/*
980 			* Before execing the no strangers program, there is
981 			* a security aspect to consider.  If NOSTRANGERS is
982 			* not a full path name, then the PATH environment
983 			* variable will provide places to look for the file.
984 			* To be safe, we will set the PATH environment
985 			* variable before we do the exec.
986 			*/
987 
988 			/* Find PATH in current environment and change it. */
989 
990 			for (eVarPtr = Env; *eVarPtr != CNULL; eVarPtr++) {
991 				if (PREFIX("PATH=", *eVarPtr))
992 					*eVarPtr = safePath;
993 			}
994 			execlp( NOSTRANGERS, "stranger", Rmtname, (char *) 0);
995 			sprintf(msgbuf, "Execlp of %s failed with errno=%%d\n",
996 				NOSTRANGERS);
997 			DEBUG(4, msgbuf, errno);
998 			perror(gettext("cico.c: execlp NOSTRANGERS failed"));
999 			cleanup(errno);
1000 		} else if (procid < 0) {
1001 			perror(gettext("cico.c: execlp NOSTRANGERS failed"));
1002 			cleanup(errno);
1003 		} else {
1004 			while ((waitrv = wait(&status)) != procid)
1005 				if (waitrv == -1 && errno != EINTR)
1006 					cleanup(errno);
1007 			sprintf(msgbuf, "%s exit status was %%#x\n",
1008 				NOSTRANGERS);
1009 			DEBUG(4, msgbuf, status);
1010 		}
1011 		cleanup(101);
1012 	}
1013 }
1014 #endif /* NOSTRANGERS */
1015