xref: /freebsd/usr.bin/tip/tip/remote.c (revision a0ee8cc6)
1 /*	$OpenBSD: remote.c,v 1.16 2006/06/06 23:24:52 deraadt Exp $	*/
2 /*	$NetBSD: remote.c,v 1.5 1997/04/20 00:02:45 mellon Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1992, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
46 static const char rcsid[] = "$OpenBSD: remote.c,v 1.16 2006/06/06 23:24:52 deraadt Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include "pathnames.h"
54 #include "tip.h"
55 
56 /*
57  * Attributes to be gleened from remote host description
58  *   data base.
59  */
60 static char **caps[] = {
61 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
62 	&ES, &EX, &FO, &RC, &RE, &PA
63 };
64 
65 static char *capstrings[] = {
66 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
67 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
68 };
69 
70 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
71 
72 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
73 
74 static void	getremcap(char *);
75 
76 static void
77 getremcap(char *host)
78 {
79 	char **p, ***q, *bp, *rempath;
80 	int   stat;
81 
82 	rempath = getenv("REMOTE");
83 	if (rempath != NULL) {
84 		if (*rempath != '/')
85 			/* we have an entry */
86 			cgetset(rempath);
87 		else {	/* we have a path */
88 			db_array[1] = rempath;
89 			db_array[2] = _PATH_REMOTE;
90 		}
91 	}
92 
93 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
94 		if ((DV != NULL) ||
95 		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
96 			CU = DV;
97 			HO = host;
98 			HW = 1;
99 			DU = 0;
100 			if (!BR)
101 				BR = DEFBR;
102 			FS = DEFFS;
103 			return;
104 		}
105 		switch (stat) {
106 		case -1:
107 			fprintf(stderr, "%s: unknown host %s\n", __progname,
108 			    host);
109 			break;
110 		case -2:
111 			fprintf(stderr,
112 			    "%s: can't open host description file\n",
113 			    __progname);
114 			break;
115 		case -3:
116 			fprintf(stderr,
117 			    "%s: possible reference loop in host description file\n", __progname);
118 			break;
119 		}
120 		exit(3);
121 	}
122 
123 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
124 		if (**q == NULL)
125 			cgetstr(bp, *p, *q);
126 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
127 		BR = DEFBR;
128 	if (!LD && (cgetnum(bp, "ld", &LD) == -1))
129 		LD = TTYDISC;
130 	if (cgetnum(bp, "fs", &FS) == -1)
131 		FS = DEFFS;
132 	if (DU < 0)
133 		DU = 0;
134 	else
135 		DU = cgetflag("du");
136 	if (DV == NOSTR) {
137 		fprintf(stderr, "%s: missing device spec\n", host);
138 		exit(3);
139 	}
140 	if (DU && CU == NOSTR)
141 		CU = DV;
142 	if (DU && PN == NOSTR) {
143 		fprintf(stderr, "%s: missing phone number\n", host);
144 		exit(3);
145 	}
146 	if (DU && AT == NOSTR) {
147 		fprintf(stderr, "%s: missing acu type\n", host);
148 		exit(3);
149 	}
150 
151 	HD = cgetflag("hd");
152 
153 	/*
154 	 * This effectively eliminates the "hw" attribute
155 	 *   from the description file
156 	 */
157 	if (!HW)
158 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
159 	HO = host;
160 	/*
161 	 * see if uppercase mode should be turned on initially
162 	 */
163 	if (cgetflag("ra"))
164 		setboolean(value(RAISE), 1);
165 	if (cgetflag("ec"))
166 		setboolean(value(ECHOCHECK), 1);
167 	if (cgetflag("be"))
168 		setboolean(value(BEAUTIFY), 1);
169 	if (cgetflag("nb"))
170 		setboolean(value(BEAUTIFY), 0);
171 	if (cgetflag("sc"))
172 		setboolean(value(SCRIPT), 1);
173 	if (cgetflag("tb"))
174 		setboolean(value(TABEXPAND), 1);
175 	if (cgetflag("vb"))
176 		setboolean(value(VERBOSE), 1);
177 	if (cgetflag("nv"))
178 		setboolean(value(VERBOSE), 0);
179 	if (cgetflag("ta"))
180 		setboolean(value(TAND), 1);
181 	if (cgetflag("nt"))
182 		setboolean(value(TAND), 0);
183 	if (cgetflag("rw"))
184 		setboolean(value(RAWFTP), 1);
185 	if (cgetflag("hd"))
186 		setboolean(value(HALFDUPLEX), 1);
187 	if (cgetflag("dc"))
188 		setboolean(value(DC), 1);
189 	if (cgetflag("hf"))
190 		setboolean(value(HARDWAREFLOW), 1);
191 	if (RE == NOSTR)
192 		RE = (char *)"tip.record";
193 	if (EX == NOSTR)
194 		EX = (char *)"\t\n\b\f";
195 	if (ES != NOSTR)
196 		vstring("es", ES);
197 	if (FO != NOSTR)
198 		vstring("fo", FO);
199 	if (PR != NOSTR)
200 		vstring("pr", PR);
201 	if (RC != NOSTR)
202 		vstring("rc", RC);
203 	if (cgetnum(bp, "dl", &DL) == -1)
204 		DL = 0;
205 	if (cgetnum(bp, "cl", &CL) == -1)
206 		CL = 0;
207 	if (cgetnum(bp, "et", &ET) == -1)
208 		ET = 10;
209 }
210 
211 char *
212 getremote(char *host)
213 {
214 	char *cp;
215 	static char *next;
216 	static int lookedup = 0;
217 
218 	if (!lookedup) {
219 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
220 			fprintf(stderr, "%s: no host specified\n", __progname);
221 			exit(3);
222 		}
223 		getremcap(host);
224 		next = DV;
225 		lookedup++;
226 	}
227 	/*
228 	 * We return a new device each time we're called (to allow
229 	 *   a rotary action to be simulated)
230 	 */
231 	if (next == NOSTR)
232 		return (NOSTR);
233 	if ((cp = strchr(next, ',')) == NULL) {
234 		DV = next;
235 		next = NOSTR;
236 	} else {
237 		*cp++ = '\0';
238 		DV = next;
239 		next = cp;
240 	}
241 	return (DV);
242 }
243