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