1 /*	$OpenBSD: remote.c,v 1.13 2003/09/20 18:15:32 millert 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 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] = "$OpenBSD: remote.c,v 1.13 2003/09/20 18:15:32 millert Exp $";
45 #endif /* not lint */
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 
50 #include "pathnames.h"
51 #include "tip.h"
52 
53 /*
54  * Attributes to be gleened from remote host description
55  *   data base.
56  */
57 static char **caps[] = {
58 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
59 	&ES, &EX, &FO, &RC, &RE, &PA
60 };
61 
62 static char *capstrings[] = {
63 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
64 	"di", "es", "ex", "fo", "rc", "re", "pa", 0
65 };
66 
67 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
68 
69 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
70 
71 static void
getremcap(host)72 getremcap(host)
73 	char *host;
74 {
75 	char **p, ***q;
76 	char *bp;
77 	char *rempath;
78 	int   stat;
79 
80 	rempath = getenv("REMOTE");
81 	if (rempath != NULL) {
82 		if (*rempath != '/')
83 			/* we have an entry */
84 			cgetset(rempath);
85 		else {	/* we have a path */
86 			db_array[1] = rempath;
87 			db_array[2] = _PATH_REMOTE;
88 		}
89 	}
90 
91 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
92 		if ((DV != NULL) ||
93 		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
94 			CU = DV;
95 			HO = host;
96 			HW = 1;
97 			DU = 0;
98 			if (!BR)
99 				BR = DEFBR;
100 			FS = DEFFS;
101 			return;
102 		}
103 		switch(stat) {
104 		case -1:
105 			fprintf(stderr, "%s: unknown host %s\n", __progname,
106 			    host);
107 			break;
108 		case -2:
109 			fprintf(stderr,
110 			    "%s: can't open host description file\n",
111 			    __progname);
112 			break;
113 		case -3:
114 			fprintf(stderr,
115 			    "%s: possible reference loop in host description file\n", __progname);
116 			break;
117 		}
118 		exit(3);
119 	}
120 
121 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
122 		if (**q == NULL)
123 			cgetstr(bp, *p, *q);
124 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
125 		BR = DEFBR;
126 	if (cgetnum(bp, "fs", &FS) == -1)
127 		FS = DEFFS;
128 	if (DU < 0)
129 		DU = 0;
130 	else
131 		DU = cgetflag("du");
132 	if (DV == NOSTR) {
133 		fprintf(stderr, "%s: missing device spec\n", host);
134 		exit(3);
135 	}
136 	if (DU && CU == NOSTR)
137 		CU = DV;
138 	if (DU && PN == NOSTR) {
139 		fprintf(stderr, "%s: missing phone number\n", host);
140 		exit(3);
141 	}
142 	if (DU && AT == NOSTR) {
143 		fprintf(stderr, "%s: missing acu type\n", host);
144 		exit(3);
145 	}
146 
147 	HD = cgetflag("hd");
148 
149 	/*
150 	 * This effectively eliminates the "hw" attribute
151 	 *   from the description file
152 	 */
153 	if (!HW)
154 		HW = (CU == NOSTR) || (DU && equal(DV, CU));
155 	HO = host;
156 	/*
157 	 * see if uppercase mode should be turned on initially
158 	 */
159 	if (cgetflag("ra"))
160 		setboolean(value(RAISE), 1);
161 	if (cgetflag("ec"))
162 		setboolean(value(ECHOCHECK), 1);
163 	if (cgetflag("be"))
164 		setboolean(value(BEAUTIFY), 1);
165 	if (cgetflag("nb"))
166 		setboolean(value(BEAUTIFY), 0);
167 	if (cgetflag("sc"))
168 		setboolean(value(SCRIPT), 1);
169 	if (cgetflag("tb"))
170 		setboolean(value(TABEXPAND), 1);
171 	if (cgetflag("vb"))
172 		setboolean(value(VERBOSE), 1);
173 	if (cgetflag("nv"))
174 		setboolean(value(VERBOSE), 0);
175 	if (cgetflag("ta"))
176 		setboolean(value(TAND), 1);
177 	if (cgetflag("nt"))
178 		setboolean(value(TAND), 0);
179 	if (cgetflag("rw"))
180 		setboolean(value(RAWFTP), 1);
181 	if (cgetflag("hd"))
182 		setboolean(value(HALFDUPLEX), 1);
183 	if (cgetflag("dc"))
184 		setboolean(value(DC), 1);
185 	if (cgetflag("hf"))
186 		setboolean(value(HARDWAREFLOW), 1);
187 	if (RE == NOSTR)
188 		RE = (char *)"tip.record";
189 	if (EX == NOSTR)
190 		EX = (char *)"\t\n\b\f";
191 	if (ES != NOSTR)
192 		vstring("es", ES);
193 	if (FO != NOSTR)
194 		vstring("fo", FO);
195 	if (PR != NOSTR)
196 		vstring("pr", PR);
197 	if (RC != NOSTR)
198 		vstring("rc", RC);
199 	if (cgetnum(bp, "dl", &DL) == -1)
200 		DL = 0;
201 	if (cgetnum(bp, "cl", &CL) == -1)
202 		CL = 0;
203 	if (cgetnum(bp, "et", &ET) == -1)
204 		ET = 10;
205 }
206 
207 char *
getremote(host)208 getremote(host)
209 	char *host;
210 {
211 	char *cp;
212 	static char *next;
213 	static int lookedup = 0;
214 
215 	if (!lookedup) {
216 		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
217 			fprintf(stderr, "%s: no host specified\n", __progname);
218 			exit(3);
219 		}
220 		getremcap(host);
221 		next = DV;
222 		lookedup++;
223 	}
224 	/*
225 	 * We return a new device each time we're called (to allow
226 	 *   a rotary action to be simulated)
227 	 */
228 	if (next == NOSTR)
229 		return (NOSTR);
230 	if ((cp = strchr(next, ',')) == NULL) {
231 		DV = next;
232 		next = NOSTR;
233 	} else {
234 		*cp++ = '\0';
235 		DV = next;
236 		next = cp;
237 	}
238 	return (DV);
239 }
240