xref: /titanic_41/usr/src/cmd/bnu/dial.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*24da5b34Srie  * Common Development and Distribution License (the "License").
6*24da5b34Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
22*24da5b34Srie /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
23*24da5b34Srie  *	  All Rights Reserved
24*24da5b34Srie  *
25*24da5b34Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
26*24da5b34Srie  * Use is subject to license terms.
27*24da5b34Srie  */
287c478bd9Sstevel@tonic-gate 
29*24da5b34Srie #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:dial.c 1.3 */
30*24da5b34Srie 
317c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
327c478bd9Sstevel@tonic-gate /***************************************************************
337c478bd9Sstevel@tonic-gate  *      dial() returns an fd for an open tty-line connected to the
347c478bd9Sstevel@tonic-gate  *      specified remote.  The caller should trap all ways to
357c478bd9Sstevel@tonic-gate  *      terminate, and call undial(). This will release the `lock'
367c478bd9Sstevel@tonic-gate  *      file and return the outgoing line to the system.  This routine
377c478bd9Sstevel@tonic-gate  *      would prefer that the calling routine not use the `alarm()'
387c478bd9Sstevel@tonic-gate  *      system call, nor issue a `signal(SIGALRM, xxx)' call.
397c478bd9Sstevel@tonic-gate  *      If you must, then please save and restore the alarm times.
407c478bd9Sstevel@tonic-gate  *      The sleep() library routine is ok, though.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *	#include <sys/types.h>
437c478bd9Sstevel@tonic-gate  *	#include <sys/stat.h>
447c478bd9Sstevel@tonic-gate  *      #include "dial.h"
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *      int dial(call);
477c478bd9Sstevel@tonic-gate  *      CALL call;
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *      void undial(rlfd);
507c478bd9Sstevel@tonic-gate  *      int rlfd;
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *      rlfd is the "remote-lne file descriptor" returned from dial.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *      The CALL structure as (defined in dial.h):
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *      typedef struct {
577c478bd9Sstevel@tonic-gate  *              struct termio *attr;    ptr to term attribute structure
587c478bd9Sstevel@tonic-gate  *              int     baud;           no longer used --
597c478bd9Sstevel@tonic-gate  *					left in for backwards compatibility
607c478bd9Sstevel@tonic-gate  *              int     speed;          212A modem: low=300, high=1200
617c478bd9Sstevel@tonic-gate  *					negative for "Any" speed
627c478bd9Sstevel@tonic-gate  *              char    *line;          device name for out-going line
637c478bd9Sstevel@tonic-gate  *              char    *telno;         ptr to tel-no digit string
647c478bd9Sstevel@tonic-gate  *		int	modem		no longer used --
657c478bd9Sstevel@tonic-gate  *					left in for backwards compatibility
667c478bd9Sstevel@tonic-gate  *		char 	*device		no longer used --
677c478bd9Sstevel@tonic-gate  *					left in for backwards compatibility
687c478bd9Sstevel@tonic-gate  *		int	dev_len		no longer used --
697c478bd9Sstevel@tonic-gate  *					left in for backwards compatibility
707c478bd9Sstevel@tonic-gate  *      } CALL;
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *      The error returns from dial are negative, in the range -1
737c478bd9Sstevel@tonic-gate  *      to -13, and their meanings are:
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  *              INTRPT   -1: interrupt occured
767c478bd9Sstevel@tonic-gate  *              D_HUNG   -2: dialer hung (no return from write)
777c478bd9Sstevel@tonic-gate  *              NO_ANS   -3: no answer (caller script failed)
787c478bd9Sstevel@tonic-gate  *              ILL_BD   -4: illegal baud-rate
797c478bd9Sstevel@tonic-gate  *              A_PROB   -5: acu problem (open() failure)
807c478bd9Sstevel@tonic-gate  *              L_PROB   -6: line problem (open() failure)
817c478bd9Sstevel@tonic-gate  *              NO_Ldv   -7: can't open Devices file
827c478bd9Sstevel@tonic-gate  *              DV_NT_A  -8: specified device not available
837c478bd9Sstevel@tonic-gate  *              DV_NT_K  -9: specified device not known
847c478bd9Sstevel@tonic-gate  *              NO_BD_A -10: no device available at requested baud-rate
857c478bd9Sstevel@tonic-gate  *              NO_BD_K -11: no device known at requested baud-rate
867c478bd9Sstevel@tonic-gate  *		DV_NT_E -12: requested speed does not match
877c478bd9Sstevel@tonic-gate  *		BAD_SYS -13: system not in Systems file
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  *      Setting attributes in the termio structure indicated in
907c478bd9Sstevel@tonic-gate  *      the `attr' field of the CALL structure before passing the
917c478bd9Sstevel@tonic-gate  *      structure to dial(), will cause those attributes to be set
927c478bd9Sstevel@tonic-gate  *      before the connection is made.  This can be important for
937c478bd9Sstevel@tonic-gate  *      some attributes such as parity and baud.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *      With an error return (negative value), there will not be
967c478bd9Sstevel@tonic-gate  *      any `lock-file' entry, so no need to call undial().
977c478bd9Sstevel@tonic-gate  ***************************************************************/
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #include <stdio.h>
1007c478bd9Sstevel@tonic-gate #include <stdlib.h>
1017c478bd9Sstevel@tonic-gate #include <string.h>
1027c478bd9Sstevel@tonic-gate #include <sys/types.h>
1037c478bd9Sstevel@tonic-gate #include <unistd.h>
1047c478bd9Sstevel@tonic-gate #include <setjmp.h>
1057c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1067c478bd9Sstevel@tonic-gate #include <sys/times.h>
1077c478bd9Sstevel@tonic-gate #include <fcntl.h>
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include "dial.h"
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #include "uucp.h"
1127c478bd9Sstevel@tonic-gate #include "uucpdefs.c"
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #include "callers.c"
1157c478bd9Sstevel@tonic-gate #include "conn.c"
1167c478bd9Sstevel@tonic-gate #include "getargs.c"
1177c478bd9Sstevel@tonic-gate #include "interface.c"
1187c478bd9Sstevel@tonic-gate #include "line.c"
1197c478bd9Sstevel@tonic-gate #include "stoa.c"
1207c478bd9Sstevel@tonic-gate #include "strsave.c"
1217c478bd9Sstevel@tonic-gate #include "sysfiles.c"
1227c478bd9Sstevel@tonic-gate #include "ulockf.c"
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #ifdef DATAKIT
1257c478bd9Sstevel@tonic-gate #include "dkbreak.c"
1267c478bd9Sstevel@tonic-gate #include "dkerr.c"
1277c478bd9Sstevel@tonic-gate #include "dkdial.c"
1287c478bd9Sstevel@tonic-gate #include "dkminor.c"
1297c478bd9Sstevel@tonic-gate #include "dtnamer.c"
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static int
1337c478bd9Sstevel@tonic-gate         rlfd;                   /* fd for remote comm line */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate GLOBAL jmp_buf Sjbuf;			/*needed by connection routines*/
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*VARARGS*/
1387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1397c478bd9Sstevel@tonic-gate static void
assert(s1,s2,i1,s3,i2)1407c478bd9Sstevel@tonic-gate assert(s1,s2,i1,s3,i2)
1417c478bd9Sstevel@tonic-gate char *s1, *s2, *s3;
1427c478bd9Sstevel@tonic-gate int i1, i2;
1437c478bd9Sstevel@tonic-gate {}	/* for ASSERT in conn() */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1467c478bd9Sstevel@tonic-gate static void
logent(s1,s2)1477c478bd9Sstevel@tonic-gate logent(s1,s2)
1487c478bd9Sstevel@tonic-gate char *s1, *s2;
1497c478bd9Sstevel@tonic-gate {}	/* so we can load unlockf() */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
cleanup(Cn)1527c478bd9Sstevel@tonic-gate cleanup(Cn) 	/*this is executed only in the parent process*/
1537c478bd9Sstevel@tonic-gate int Cn;		/*fd for remote comm line */
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	(void)restline();
1567c478bd9Sstevel@tonic-gate 	(void)setuid(Euid);
1577c478bd9Sstevel@tonic-gate 	if(Cn > 0) {
1587c478bd9Sstevel@tonic-gate 		(void) close(Cn);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	rmlock((char*) NULL);	/*uucp routine in ulockf.c*/
1637c478bd9Sstevel@tonic-gate 	return;		/* code=negative for signal causing disconnect*/
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate int
dial(call)1677c478bd9Sstevel@tonic-gate dial(call)
1687c478bd9Sstevel@tonic-gate CALL call;
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate char *alt[7];
1717c478bd9Sstevel@tonic-gate char speed[10];		/* character value of speed passed to dial */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* set service so we know which Sysfiles entries to use, then	*/
1747c478bd9Sstevel@tonic-gate 	/* be sure can access Devices file(s).  use "cu" entries ...	*/
1757c478bd9Sstevel@tonic-gate 	/* dial is more like cu than like uucico.			*/
1767c478bd9Sstevel@tonic-gate 	(void)strcpy(Progname,"cu");
1777c478bd9Sstevel@tonic-gate 	setservice(Progname);
1787c478bd9Sstevel@tonic-gate 	if ( sysaccess(EACCESS_DEVICES) != 0 ) {
1797c478bd9Sstevel@tonic-gate 		/* can't read Devices file(s)	*/
1807c478bd9Sstevel@tonic-gate 		return(NO_Ldv);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (call.attr != NULL) {
1847c478bd9Sstevel@tonic-gate 		if ( call.attr->c_cflag & PARENB ) {
1857c478bd9Sstevel@tonic-gate 			Evenflag = ((call.attr->c_cflag & PARODD) ? 0 : 1);
1867c478bd9Sstevel@tonic-gate 			Oddflag = ((call.attr->c_cflag & PARODD) ? 1 : 0);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		line_8bit = (call.attr->c_cflag & CS8 ? 1 : 0);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (call.speed <= 0)
1927c478bd9Sstevel@tonic-gate 		strcpy(speed,"Any");
1937c478bd9Sstevel@tonic-gate 	else
1947c478bd9Sstevel@tonic-gate 		sprintf(speed,"%d",call.speed);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/* Determine whether contents of "telno" is a system name. */
1977c478bd9Sstevel@tonic-gate 	if ( (call.telno != NULL) &&
1987c478bd9Sstevel@tonic-gate 	     (strlen(call.telno) != strspn(call.telno,"0123456789=-*#")) ) {
1997c478bd9Sstevel@tonic-gate 		/* use conn() for system names */
2007c478bd9Sstevel@tonic-gate 		rlfd = conn(call.telno);
2017c478bd9Sstevel@tonic-gate 	} else {
2027c478bd9Sstevel@tonic-gate 		alt[F_NAME] = "dummy";	/* to replace the Systems file fields */
2037c478bd9Sstevel@tonic-gate 		alt[F_TIME] = "Any";    /* needed for getto(); [F_TYPE] and */
2047c478bd9Sstevel@tonic-gate 		alt[F_TYPE] = "";	/* [F_PHONE] assignment below       */
2057c478bd9Sstevel@tonic-gate 		alt[F_CLASS] = speed;
2067c478bd9Sstevel@tonic-gate 		alt[F_PHONE] = "";
2077c478bd9Sstevel@tonic-gate 		alt[F_LOGIN] = "";
2087c478bd9Sstevel@tonic-gate 		alt[6] = "";
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if ( (call.telno != NULL) && (*call.telno != '\0') ) {
2117c478bd9Sstevel@tonic-gate 			/* given a phone number, use an ACU */
2127c478bd9Sstevel@tonic-gate 			alt[F_PHONE] = call.telno;
2137c478bd9Sstevel@tonic-gate 			alt[F_TYPE] = "ACU";
2147c478bd9Sstevel@tonic-gate 		} else {
2157c478bd9Sstevel@tonic-gate 			/* otherwise, use a Direct connection */
2167c478bd9Sstevel@tonic-gate 			alt[F_TYPE] = "Direct";
2177c478bd9Sstevel@tonic-gate 			/* If device name starts with "/dev/", strip it off  */
2187c478bd9Sstevel@tonic-gate 			/* since Devices file entries will also be stripped. */
2197c478bd9Sstevel@tonic-gate 			if ( (call.line != NULL) &&
2207c478bd9Sstevel@tonic-gate 				(strncmp(call.line, "/dev/", 5) == 0) )
2217c478bd9Sstevel@tonic-gate 				Myline = (call.line + 5);
2227c478bd9Sstevel@tonic-gate 			else
2237c478bd9Sstevel@tonic-gate 				Myline = call.line;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #ifdef forfutureuse
2277c478bd9Sstevel@tonic-gate 		if (call->class != NULL)
2287c478bd9Sstevel@tonic-gate 			alt[F_TYPE] = call->class;
2297c478bd9Sstevel@tonic-gate #endif
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		rlfd = getto(alt);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	if (rlfd < 0)
2357c478bd9Sstevel@tonic-gate 		switch (Uerror) {
2367c478bd9Sstevel@tonic-gate 			case SS_NO_DEVICE:	return(NO_BD_A);
2377c478bd9Sstevel@tonic-gate 			case SS_DIAL_FAILED:	return(D_HUNG);
2387c478bd9Sstevel@tonic-gate 			case SS_LOCKED_DEVICE:	return(DV_NT_A);
2397c478bd9Sstevel@tonic-gate 			case SS_BADSYSTEM:	return(BAD_SYS);
2407c478bd9Sstevel@tonic-gate 			case SS_CANT_ACCESS_DEVICE:	return(L_PROB);
2417c478bd9Sstevel@tonic-gate 			case SS_CHAT_FAILED:	return(NO_ANS);
2427c478bd9Sstevel@tonic-gate 			default:	return(-Uerror);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	(void)savline();
2457c478bd9Sstevel@tonic-gate 	if ((call.attr) && ioctl(rlfd, TCSETA, call.attr) < 0) {
2467c478bd9Sstevel@tonic-gate 		perror("stty for remote");
2477c478bd9Sstevel@tonic-gate 		return(L_PROB);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	Euid = geteuid();
2507c478bd9Sstevel@tonic-gate 	if(setuid(getuid()) && setgid(getgid()) < 0)
2517c478bd9Sstevel@tonic-gate 		undial(rlfd);
2527c478bd9Sstevel@tonic-gate 	return(rlfd);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * undial(fd)
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate void
undial(fd)2597c478bd9Sstevel@tonic-gate undial(fd)
2607c478bd9Sstevel@tonic-gate int fd;
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	sethup(fd);
2637c478bd9Sstevel@tonic-gate 	sleep(2);
2647c478bd9Sstevel@tonic-gate 	cleanup(fd);
2657c478bd9Sstevel@tonic-gate }
266