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