xref: /titanic_51/usr/src/cmd/bnu/dial.c (revision 23c352973f956f97f817e65150aad7e1cebeb228)
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"	/* from SVR4 bnu:dial.c 1.3 */
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 <fcntl.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
130         rlfd;                   /* fd for remote comm line */
131 
132 GLOBAL jmp_buf Sjbuf;			/*needed by connection routines*/
133 
134 /*VARARGS*/
135 /*ARGSUSED*/
136 static void
137 assert(s1,s2,i1,s3,i2)
138 char *s1, *s2, *s3;
139 int i1, i2;
140 {}	/* for ASSERT in conn() */
141 
142 /*ARGSUSED*/
143 static void
144 logent(s1,s2)
145 char *s1, *s2;
146 {}	/* so we can load unlockf() */
147 
148 static void
149 cleanup(Cn) 	/*this is executed only in the parent process*/
150 int Cn;		/*fd for remote comm line */
151 {
152 	(void)restline();
153 	(void)setuid(Euid);
154 	if(Cn > 0) {
155 		(void) close(Cn);
156 	}
157 
158 
159 	rmlock((char*) NULL);	/*uucp routine in ulockf.c*/
160 	return;		/* code=negative for signal causing disconnect*/
161 }
162 
163 int
164 dial(call)
165 CALL call;
166 {
167 char *alt[7];
168 char speed[10];		/* character value of speed passed to dial */
169 
170 	/* set service so we know which Sysfiles entries to use, then	*/
171 	/* be sure can access Devices file(s).  use "cu" entries ...	*/
172 	/* dial is more like cu than like uucico.			*/
173 	(void)strcpy(Progname,"cu");
174 	setservice(Progname);
175 	if ( sysaccess(EACCESS_DEVICES) != 0 ) {
176 		/* can't read Devices file(s)	*/
177 		return(NO_Ldv);
178 	}
179 
180 	if (call.attr != NULL) {
181 		if ( call.attr->c_cflag & PARENB ) {
182 			Evenflag = ((call.attr->c_cflag & PARODD) ? 0 : 1);
183 			Oddflag = ((call.attr->c_cflag & PARODD) ? 1 : 0);
184 		}
185 		line_8bit = (call.attr->c_cflag & CS8 ? 1 : 0);
186 	}
187 
188 	if (call.speed <= 0)
189 		strcpy(speed,"Any");
190 	else
191 		sprintf(speed,"%d",call.speed);
192 
193 	/* Determine whether contents of "telno" is a system name. */
194 	if ( (call.telno != NULL) &&
195 	     (strlen(call.telno) != strspn(call.telno,"0123456789=-*#")) ) {
196 		/* use conn() for system names */
197 		rlfd = conn(call.telno);
198 	} else {
199 		alt[F_NAME] = "dummy";	/* to replace the Systems file fields */
200 		alt[F_TIME] = "Any";    /* needed for getto(); [F_TYPE] and */
201 		alt[F_TYPE] = "";	/* [F_PHONE] assignment below       */
202 		alt[F_CLASS] = speed;
203 		alt[F_PHONE] = "";
204 		alt[F_LOGIN] = "";
205 		alt[6] = "";
206 
207 		if ( (call.telno != NULL) && (*call.telno != '\0') ) {
208 			/* given a phone number, use an ACU */
209 			alt[F_PHONE] = call.telno;
210 			alt[F_TYPE] = "ACU";
211 		} else {
212 			/* otherwise, use a Direct connection */
213 			alt[F_TYPE] = "Direct";
214 			/* If device name starts with "/dev/", strip it off  */
215 			/* since Devices file entries will also be stripped. */
216 			if ( (call.line != NULL) &&
217 				(strncmp(call.line, "/dev/", 5) == 0) )
218 				Myline = (call.line + 5);
219 			else
220 				Myline = call.line;
221 		}
222 
223 #ifdef forfutureuse
224 		if (call->class != NULL)
225 			alt[F_TYPE] = call->class;
226 #endif
227 
228 
229 		rlfd = getto(alt);
230 	}
231 	if (rlfd < 0)
232 		switch (Uerror) {
233 			case SS_NO_DEVICE:	return(NO_BD_A);
234 			case SS_DIAL_FAILED:	return(D_HUNG);
235 			case SS_LOCKED_DEVICE:	return(DV_NT_A);
236 			case SS_BADSYSTEM:	return(BAD_SYS);
237 			case SS_CANT_ACCESS_DEVICE:	return(L_PROB);
238 			case SS_CHAT_FAILED:	return(NO_ANS);
239 			default:	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 */
255 void
256 undial(fd)
257 int fd;
258 {
259 	sethup(fd);
260 	sleep(2);
261 	cleanup(fd);
262 }
263