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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23/*
24 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25 * Use is subject to license terms.
26 */
27
28/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29/*	  All Rights Reserved	*/
30
31#pragma ident	"%Z%%M%	%I%	%E% SMI"
32
33/*
34 * *************************************************************
35 *	  dial() returns an fd for an open tty-line connected to the
36 *	  specified remote.  The caller should trap all ways to
37 *	  terminate, and call undial(). This will release the `lock'
38 *	  file and return the outgoing line to the system.  This routine
39 *	  would prefer that the calling routine not use the `alarm()'
40 *	  system call, nor issue a `signal(SIGALRM, xxx)' call.
41 *	  If you must, then please save and restore the alarm times.
42 *	  The sleep() library routine is ok, though.
43 *
44 *	#include <sys/types.h>
45 *	#include <sys/stat.h>
46 *	  #include "dial.h"
47 *
48 *	  int dial(call);
49 *	  CALL call;
50 *
51 *	  void undial(rlfd);
52 *	  int rlfd;
53 *
54 *	  rlfd is the "remote-lne file descriptor" returned from dial.
55 *
56 *	  The CALL structure as (defined in dial.h):
57 *
58 *	  typedef struct {
59 *			  struct termio *attr;	ptr to term attribute structure
60 *			  int	 baud;		   no longer used --
61 *					left in for backwards compatibility
62 *			  int	 speed;		  212A modem: low=300, high=1200
63 *					negative for "Any" speed
64 *			  char	*line;		  device name for out-going line
65 *			  char	*telno;		 ptr to tel-no digit string
66 *		int	modem		no longer used --
67 *					left in for backwards compatibility
68 *		char	*device		no longer used --
69 *					left in for backwards compatibility
70 *		int	dev_len		no longer used --
71 *					left in for backwards compatibility
72 *	  } CALL;
73 *
74 *	  The error returns from dial are negative, in the range -1
75 *	  to -13, and their meanings are:
76 *
77 *			  INTRPT   -1: interrupt occured
78 *			  D_HUNG   -2: dialer hung (no return from write)
79 *			  NO_ANS   -3: no answer (caller script failed)
80 *			  ILL_BD   -4: illegal baud-rate
81 *			  A_PROB   -5: acu problem (open() failure)
82 *			  L_PROB   -6: line problem (open() failure)
83 *			  NO_Ldv   -7: can't open Devices file
84 *			  DV_NT_A  -8: specified device not available
85 *			  DV_NT_K  -9: specified device not known
86 *			  NO_BD_A -10: no dev available at requested baud-rate
87 *			  NO_BD_K -11: no device known at requested baud-rate
88 *		DV_NT_E -12: requested speed does not match
89 *		BAD_SYS -13: system not in Systems file
90 *
91 *	  Setting attributes in the termio structure indicated in
92 *	  the `attr' field of the CALL structure before passing the
93 *	  structure to dial(), will cause those attributes to be set
94 *	  before the connection is made.  This can be important for
95 *	  some attributes such as parity and baud.
96 *
97 *	  With an error return (negative value), there will not be
98 *	  any `lock-file' entry, so no need to call undial().
99 * *************************************************************
100 */
101
102#include "mt.h"
103
104#include <stdio.h>
105#include <stdlib.h>
106#include <string.h>
107#include <sys/types.h>
108#include <unistd.h>
109#include <setjmp.h>
110#include <sys/stat.h>
111#include <sys/times.h>
112
113#include "dial.h"
114
115#include "uucp.h"
116#include "uucpdefs.c"
117
118#include "callers.c"
119#include "conn.c"
120#include "getargs.c"
121#include "interface.c"
122#include "line.c"
123#include "stoa.c"
124#include "strecpy.c"
125#include "strsave.c"
126#include "sysfiles.c"
127#include "ulockf.c"
128
129static int rlfd;			/* fd for remote comm line */
130
131static jmp_buf Sjbuf;		/* needed by connection routines */
132
133/* VARARGS */
134/* ARGSUSED */
135static void
136assert(const char *s1, const char *s2, int i1, const char *s3, int i2)
137{					/* for ASSERT in conn() */
138}
139
140/* ARGSUSED */
141static void
142logent(const char *s1, const char *s2)
143{					/* so we can load unlockf() */
144}
145
146static void
147cleanup(int Cn)		/* this is executed only in the parent process */
148{
149	(void) restline();
150	(void) setuid(Euid);
151	if (Cn > 0)
152		(void) close(Cn);
153
154	rmlock(NULL);	/* uucp routine in ulockf.c */
155}
156
157int
158dial(CALL call)
159{
160	char *alt[7];
161	char speed[10];		/* character value of speed passed to dial */
162
163	/* set service so we know which Sysfiles entries to use, then	*/
164	/* be sure can access Devices file(s).  use "cu" entries ...	*/
165	/* dial is more like cu than like uucico.			*/
166
167	(void) strcpy(Progname, "cu");
168	setservice(Progname);
169	if (sysaccess(EACCESS_DEVICES) != 0)
170		/* can't read Devices file(s)	*/
171		return (NO_Ldv);
172
173	if (call.attr != NULL) {
174		if (call.attr->c_cflag & PARENB) {
175			Evenflag = ((call.attr->c_cflag & PARODD) ? 0 : 1);
176			Oddflag = ((call.attr->c_cflag & PARODD) ? 1 : 0);
177		}
178		line_8bit = (call.attr->c_cflag & CS8 ? 1 : 0);
179	}
180
181	if (call.speed <= 0)
182		(void) strcpy(speed, "Any");
183	else
184		(void) sprintf(speed, "%d", call.speed);
185
186	/* Determine whether contents of "telno" is a system name. */
187	if ((call.telno != NULL) &&
188		(strlen(call.telno) != strspn(call.telno, "0123456789=-*#"))) {
189		/* use conn() for system names */
190		rlfd = conn(call.telno);
191	} else {
192		alt[F_NAME] = "dummy";	/* to replace the Systems file fields */
193		alt[F_TIME] = "Any";	/* needed for getto(); [F_TYPE] and */
194		alt[F_TYPE] = "";	/* [F_PHONE] assignment below	   */
195		alt[F_CLASS] = speed;
196		alt[F_PHONE] = "";
197		alt[F_LOGIN] = "";
198		alt[6] = "";
199
200		if ((call.telno != NULL) && (*call.telno != '\0')) {
201			/* given a phone number, use an ACU */
202			alt[F_PHONE] = call.telno;
203			alt[F_TYPE] = "ACU";
204		} else {
205			/* otherwise, use a Direct connection */
206			alt[F_TYPE] = "Direct";
207			/* If device name starts with "/dev/", strip it off  */
208			/* since Devices file entries will also be stripped. */
209			if ((call.line != NULL) &&
210				(strncmp(call.line, "/dev/", 5) == 0))
211				Myline = (call.line + 5);
212			else
213				Myline = call.line;
214		}
215
216#ifdef forfutureuse
217		if (call->class != NULL)
218			alt[F_TYPE] = call->class;
219#endif
220
221
222		rlfd = getto(alt);
223	}
224	if (rlfd < 0)
225		switch (Uerror) {
226			case SS_NO_DEVICE:
227				return (NO_BD_A);
228			case SS_DIAL_FAILED:
229				return (D_HUNG);
230			case SS_LOCKED_DEVICE:
231				return (DV_NT_A);
232			case SS_BADSYSTEM:
233				return (BAD_SYS);
234			case SS_CANT_ACCESS_DEVICE:
235				return (L_PROB);
236			case SS_CHAT_FAILED:
237				return (NO_ANS);
238			default:
239				return (-Uerror);
240		}
241	(void) savline();
242	if ((call.attr) && ioctl(rlfd, TCSETA, call.attr) < 0) {
243		perror("stty for remote");
244		return (L_PROB);
245	}
246	Euid = geteuid();
247	if (setuid(getuid()) && setgid(getgid()) < 0)
248		undial(rlfd);
249	return (rlfd);
250}
251
252/*
253 * undial(fd)
254 */
255void
256undial(int fd)
257{
258	sethup(fd);
259	(void) sleep(2);
260	cleanup(fd);
261}
262