xref: /titanic_50/usr/src/lib/libnsl/dial/dial.c (revision 6fec3791b5a9a5621db93bfef3a6514bc0511b5d)
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 2005 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 <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <sys/types.h>
106 #include <unistd.h>
107 #include <setjmp.h>
108 #include <sys/stat.h>
109 #include <sys/times.h>
110 
111 #include "dial.h"
112 
113 #include "uucp.h"
114 #include "uucpdefs.c"
115 
116 #include "callers.c"
117 #include "conn.c"
118 #include "getargs.c"
119 #include "interface.c"
120 #include "line.c"
121 #include "stoa.c"
122 #include "strecpy.c"
123 #include "strsave.c"
124 #include "sysfiles.c"
125 #include "ulockf.c"
126 
127 static int rlfd;			/* fd for remote comm line */
128 
129 static jmp_buf Sjbuf;		/* needed by connection routines */
130 
131 /* VARARGS */
132 /* ARGSUSED */
133 static void
134 assert(const char *s1, const char *s2, int i1, const char *s3, int i2)
135 {					/* for ASSERT in conn() */
136 }
137 
138 /* ARGSUSED */
139 static void
140 logent(const char *s1, const char *s2)
141 {					/* so we can load unlockf() */
142 }
143 
144 static void
145 cleanup(int Cn)		/* this is executed only in the parent process */
146 {
147 	(void) restline();
148 	(void) setuid(Euid);
149 	if (Cn > 0)
150 		(void) close(Cn);
151 
152 	rmlock(NULL);	/* uucp routine in ulockf.c */
153 }
154 
155 int
156 dial(CALL call)
157 {
158 	char *alt[7];
159 	char speed[10];		/* character value of speed passed to dial */
160 
161 	/* set service so we know which Sysfiles entries to use, then	*/
162 	/* be sure can access Devices file(s).  use "cu" entries ...	*/
163 	/* dial is more like cu than like uucico.			*/
164 
165 	(void) strcpy(Progname, "cu");
166 	setservice(Progname);
167 	if (sysaccess(EACCESS_DEVICES) != 0)
168 		/* can't read Devices file(s)	*/
169 		return (NO_Ldv);
170 
171 	if (call.attr != NULL) {
172 		if (call.attr->c_cflag & PARENB) {
173 			Evenflag = ((call.attr->c_cflag & PARODD) ? 0 : 1);
174 			Oddflag = ((call.attr->c_cflag & PARODD) ? 1 : 0);
175 		}
176 		line_8bit = (call.attr->c_cflag & CS8 ? 1 : 0);
177 	}
178 
179 	if (call.speed <= 0)
180 		(void) strcpy(speed, "Any");
181 	else
182 		(void) sprintf(speed, "%d", call.speed);
183 
184 	/* Determine whether contents of "telno" is a system name. */
185 	if ((call.telno != NULL) &&
186 		(strlen(call.telno) != strspn(call.telno, "0123456789=-*#"))) {
187 		/* use conn() for system names */
188 		rlfd = conn(call.telno);
189 	} else {
190 		alt[F_NAME] = "dummy";	/* to replace the Systems file fields */
191 		alt[F_TIME] = "Any";	/* needed for getto(); [F_TYPE] and */
192 		alt[F_TYPE] = "";	/* [F_PHONE] assignment below	   */
193 		alt[F_CLASS] = speed;
194 		alt[F_PHONE] = "";
195 		alt[F_LOGIN] = "";
196 		alt[6] = "";
197 
198 		if ((call.telno != NULL) && (*call.telno != '\0')) {
199 			/* given a phone number, use an ACU */
200 			alt[F_PHONE] = call.telno;
201 			alt[F_TYPE] = "ACU";
202 		} else {
203 			/* otherwise, use a Direct connection */
204 			alt[F_TYPE] = "Direct";
205 			/* If device name starts with "/dev/", strip it off  */
206 			/* since Devices file entries will also be stripped. */
207 			if ((call.line != NULL) &&
208 				(strncmp(call.line, "/dev/", 5) == 0))
209 				Myline = (call.line + 5);
210 			else
211 				Myline = call.line;
212 		}
213 
214 #ifdef forfutureuse
215 		if (call->class != NULL)
216 			alt[F_TYPE] = call->class;
217 #endif
218 
219 
220 		rlfd = getto(alt);
221 	}
222 	if (rlfd < 0)
223 		switch (Uerror) {
224 			case SS_NO_DEVICE:
225 				return (NO_BD_A);
226 			case SS_DIAL_FAILED:
227 				return (D_HUNG);
228 			case SS_LOCKED_DEVICE:
229 				return (DV_NT_A);
230 			case SS_BADSYSTEM:
231 				return (BAD_SYS);
232 			case SS_CANT_ACCESS_DEVICE:
233 				return (L_PROB);
234 			case SS_CHAT_FAILED:
235 				return (NO_ANS);
236 			default:
237 				return (-Uerror);
238 		}
239 	(void) savline();
240 	if ((call.attr) && ioctl(rlfd, TCSETA, call.attr) < 0) {
241 		perror("stty for remote");
242 		return (L_PROB);
243 	}
244 	Euid = geteuid();
245 	if (setuid(getuid()) && setgid(getgid()) < 0)
246 		undial(rlfd);
247 	return (rlfd);
248 }
249 
250 /*
251  * undial(fd)
252  */
253 void
254 undial(int fd)
255 {
256 	sethup(fd);
257 	(void) sleep(2);
258 	cleanup(fd);
259 }
260