xref: /onnv-gate/usr/src/lib/libnsl/dial/dial.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
23132Srobinson /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25132Srobinson  * Use is subject to license terms.
26132Srobinson  */
270Sstevel@tonic-gate 
28132Srobinson /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29132Srobinson /*	  All Rights Reserved	*/
30132Srobinson 
31132Srobinson #pragma ident	"%Z%%M%	%I%	%E% SMI"
32132Srobinson 
33132Srobinson /*
34132Srobinson  * *************************************************************
350Sstevel@tonic-gate  *	  dial() returns an fd for an open tty-line connected to the
360Sstevel@tonic-gate  *	  specified remote.  The caller should trap all ways to
370Sstevel@tonic-gate  *	  terminate, and call undial(). This will release the `lock'
380Sstevel@tonic-gate  *	  file and return the outgoing line to the system.  This routine
390Sstevel@tonic-gate  *	  would prefer that the calling routine not use the `alarm()'
400Sstevel@tonic-gate  *	  system call, nor issue a `signal(SIGALRM, xxx)' call.
410Sstevel@tonic-gate  *	  If you must, then please save and restore the alarm times.
420Sstevel@tonic-gate  *	  The sleep() library routine is ok, though.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *	#include <sys/types.h>
450Sstevel@tonic-gate  *	#include <sys/stat.h>
460Sstevel@tonic-gate  *	  #include "dial.h"
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *	  int dial(call);
490Sstevel@tonic-gate  *	  CALL call;
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *	  void undial(rlfd);
520Sstevel@tonic-gate  *	  int rlfd;
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  *	  rlfd is the "remote-lne file descriptor" returned from dial.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *	  The CALL structure as (defined in dial.h):
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *	  typedef struct {
590Sstevel@tonic-gate  *			  struct termio *attr;	ptr to term attribute structure
600Sstevel@tonic-gate  *			  int	 baud;		   no longer used --
610Sstevel@tonic-gate  *					left in for backwards compatibility
620Sstevel@tonic-gate  *			  int	 speed;		  212A modem: low=300, high=1200
630Sstevel@tonic-gate  *					negative for "Any" speed
640Sstevel@tonic-gate  *			  char	*line;		  device name for out-going line
650Sstevel@tonic-gate  *			  char	*telno;		 ptr to tel-no digit string
660Sstevel@tonic-gate  *		int	modem		no longer used --
670Sstevel@tonic-gate  *					left in for backwards compatibility
68132Srobinson  *		char	*device		no longer used --
690Sstevel@tonic-gate  *					left in for backwards compatibility
700Sstevel@tonic-gate  *		int	dev_len		no longer used --
710Sstevel@tonic-gate  *					left in for backwards compatibility
720Sstevel@tonic-gate  *	  } CALL;
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  *	  The error returns from dial are negative, in the range -1
750Sstevel@tonic-gate  *	  to -13, and their meanings are:
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *			  INTRPT   -1: interrupt occured
780Sstevel@tonic-gate  *			  D_HUNG   -2: dialer hung (no return from write)
790Sstevel@tonic-gate  *			  NO_ANS   -3: no answer (caller script failed)
800Sstevel@tonic-gate  *			  ILL_BD   -4: illegal baud-rate
810Sstevel@tonic-gate  *			  A_PROB   -5: acu problem (open() failure)
820Sstevel@tonic-gate  *			  L_PROB   -6: line problem (open() failure)
830Sstevel@tonic-gate  *			  NO_Ldv   -7: can't open Devices file
840Sstevel@tonic-gate  *			  DV_NT_A  -8: specified device not available
850Sstevel@tonic-gate  *			  DV_NT_K  -9: specified device not known
86132Srobinson  *			  NO_BD_A -10: no dev available at requested baud-rate
870Sstevel@tonic-gate  *			  NO_BD_K -11: no device known at requested baud-rate
880Sstevel@tonic-gate  *		DV_NT_E -12: requested speed does not match
890Sstevel@tonic-gate  *		BAD_SYS -13: system not in Systems file
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  *	  Setting attributes in the termio structure indicated in
920Sstevel@tonic-gate  *	  the `attr' field of the CALL structure before passing the
930Sstevel@tonic-gate  *	  structure to dial(), will cause those attributes to be set
940Sstevel@tonic-gate  *	  before the connection is made.  This can be important for
950Sstevel@tonic-gate  *	  some attributes such as parity and baud.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  *	  With an error return (negative value), there will not be
980Sstevel@tonic-gate  *	  any `lock-file' entry, so no need to call undial().
99132Srobinson  * *************************************************************
100132Srobinson  */
1010Sstevel@tonic-gate 
102*1219Sraf #include "mt.h"
103*1219Sraf 
1040Sstevel@tonic-gate #include <stdio.h>
1050Sstevel@tonic-gate #include <stdlib.h>
1060Sstevel@tonic-gate #include <string.h>
1070Sstevel@tonic-gate #include <sys/types.h>
1080Sstevel@tonic-gate #include <unistd.h>
1090Sstevel@tonic-gate #include <setjmp.h>
1100Sstevel@tonic-gate #include <sys/stat.h>
1110Sstevel@tonic-gate #include <sys/times.h>
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #include "dial.h"
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #include "uucp.h"
1160Sstevel@tonic-gate #include "uucpdefs.c"
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #include "callers.c"
1190Sstevel@tonic-gate #include "conn.c"
1200Sstevel@tonic-gate #include "getargs.c"
1210Sstevel@tonic-gate #include "interface.c"
1220Sstevel@tonic-gate #include "line.c"
1230Sstevel@tonic-gate #include "stoa.c"
1240Sstevel@tonic-gate #include "strecpy.c"
1250Sstevel@tonic-gate #include "strsave.c"
1260Sstevel@tonic-gate #include "sysfiles.c"
1270Sstevel@tonic-gate #include "ulockf.c"
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int rlfd;			/* fd for remote comm line */
1300Sstevel@tonic-gate 
131132Srobinson static jmp_buf Sjbuf;		/* needed by connection routines */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* VARARGS */
1340Sstevel@tonic-gate /* ARGSUSED */
1350Sstevel@tonic-gate static void
assert(const char * s1,const char * s2,int i1,const char * s3,int i2)136132Srobinson assert(const char *s1, const char *s2, int i1, const char *s3, int i2)
1370Sstevel@tonic-gate {					/* for ASSERT in conn() */
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /* ARGSUSED */
1410Sstevel@tonic-gate static void
logent(const char * s1,const char * s2)142132Srobinson logent(const char *s1, const char *s2)
1430Sstevel@tonic-gate {					/* so we can load unlockf() */
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static void
cleanup(int Cn)147132Srobinson cleanup(int Cn)		/* this is executed only in the parent process */
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	(void) restline();
1500Sstevel@tonic-gate 	(void) setuid(Euid);
1510Sstevel@tonic-gate 	if (Cn > 0)
1520Sstevel@tonic-gate 		(void) close(Cn);
1530Sstevel@tonic-gate 
154132Srobinson 	rmlock(NULL);	/* uucp routine in ulockf.c */
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
dial(CALL call)158132Srobinson dial(CALL call)
1590Sstevel@tonic-gate {
160132Srobinson 	char *alt[7];
161132Srobinson 	char speed[10];		/* character value of speed passed to dial */
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/* set service so we know which Sysfiles entries to use, then	*/
1640Sstevel@tonic-gate 	/* be sure can access Devices file(s).  use "cu" entries ...	*/
1650Sstevel@tonic-gate 	/* dial is more like cu than like uucico.			*/
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	(void) strcpy(Progname, "cu");
1680Sstevel@tonic-gate 	setservice(Progname);
169132Srobinson 	if (sysaccess(EACCESS_DEVICES) != 0)
1700Sstevel@tonic-gate 		/* can't read Devices file(s)	*/
1710Sstevel@tonic-gate 		return (NO_Ldv);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if (call.attr != NULL) {
1740Sstevel@tonic-gate 		if (call.attr->c_cflag & PARENB) {
1750Sstevel@tonic-gate 			Evenflag = ((call.attr->c_cflag & PARODD) ? 0 : 1);
1760Sstevel@tonic-gate 			Oddflag = ((call.attr->c_cflag & PARODD) ? 1 : 0);
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 		line_8bit = (call.attr->c_cflag & CS8 ? 1 : 0);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (call.speed <= 0)
182132Srobinson 		(void) strcpy(speed, "Any");
1830Sstevel@tonic-gate 	else
184132Srobinson 		(void) sprintf(speed, "%d", call.speed);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* Determine whether contents of "telno" is a system name. */
1870Sstevel@tonic-gate 	if ((call.telno != NULL) &&
1880Sstevel@tonic-gate 		(strlen(call.telno) != strspn(call.telno, "0123456789=-*#"))) {
1890Sstevel@tonic-gate 		/* use conn() for system names */
1900Sstevel@tonic-gate 		rlfd = conn(call.telno);
1910Sstevel@tonic-gate 	} else {
1920Sstevel@tonic-gate 		alt[F_NAME] = "dummy";	/* to replace the Systems file fields */
1930Sstevel@tonic-gate 		alt[F_TIME] = "Any";	/* needed for getto(); [F_TYPE] and */
1940Sstevel@tonic-gate 		alt[F_TYPE] = "";	/* [F_PHONE] assignment below	   */
1950Sstevel@tonic-gate 		alt[F_CLASS] = speed;
1960Sstevel@tonic-gate 		alt[F_PHONE] = "";
1970Sstevel@tonic-gate 		alt[F_LOGIN] = "";
1980Sstevel@tonic-gate 		alt[6] = "";
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if ((call.telno != NULL) && (*call.telno != '\0')) {
2010Sstevel@tonic-gate 			/* given a phone number, use an ACU */
2020Sstevel@tonic-gate 			alt[F_PHONE] = call.telno;
2030Sstevel@tonic-gate 			alt[F_TYPE] = "ACU";
2040Sstevel@tonic-gate 		} else {
2050Sstevel@tonic-gate 			/* otherwise, use a Direct connection */
2060Sstevel@tonic-gate 			alt[F_TYPE] = "Direct";
2070Sstevel@tonic-gate 			/* If device name starts with "/dev/", strip it off  */
2080Sstevel@tonic-gate 			/* since Devices file entries will also be stripped. */
2090Sstevel@tonic-gate 			if ((call.line != NULL) &&
2100Sstevel@tonic-gate 				(strncmp(call.line, "/dev/", 5) == 0))
2110Sstevel@tonic-gate 				Myline = (call.line + 5);
2120Sstevel@tonic-gate 			else
2130Sstevel@tonic-gate 				Myline = call.line;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate #ifdef forfutureuse
2170Sstevel@tonic-gate 		if (call->class != NULL)
2180Sstevel@tonic-gate 			alt[F_TYPE] = call->class;
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		rlfd = getto(alt);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	if (rlfd < 0)
2250Sstevel@tonic-gate 		switch (Uerror) {
2260Sstevel@tonic-gate 			case SS_NO_DEVICE:
227132Srobinson 				return (NO_BD_A);
2280Sstevel@tonic-gate 			case SS_DIAL_FAILED:
229132Srobinson 				return (D_HUNG);
2300Sstevel@tonic-gate 			case SS_LOCKED_DEVICE:
231132Srobinson 				return (DV_NT_A);
2320Sstevel@tonic-gate 			case SS_BADSYSTEM:
233132Srobinson 				return (BAD_SYS);
2340Sstevel@tonic-gate 			case SS_CANT_ACCESS_DEVICE:
235132Srobinson 				return (L_PROB);
2360Sstevel@tonic-gate 			case SS_CHAT_FAILED:
237132Srobinson 				return (NO_ANS);
2380Sstevel@tonic-gate 			default:
239132Srobinson 				return (-Uerror);
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 	(void) savline();
2420Sstevel@tonic-gate 	if ((call.attr) && ioctl(rlfd, TCSETA, call.attr) < 0) {
2430Sstevel@tonic-gate 		perror("stty for remote");
2440Sstevel@tonic-gate 		return (L_PROB);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	Euid = geteuid();
2470Sstevel@tonic-gate 	if (setuid(getuid()) && setgid(getgid()) < 0)
2480Sstevel@tonic-gate 		undial(rlfd);
2490Sstevel@tonic-gate 	return (rlfd);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
253132Srobinson  * undial(fd)
254132Srobinson  */
2550Sstevel@tonic-gate void
undial(int fd)256132Srobinson undial(int fd)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	sethup(fd);
259132Srobinson 	(void) sleep(2);
2600Sstevel@tonic-gate 	cleanup(fd);
2610Sstevel@tonic-gate }
262