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