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