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