xref: /dragonfly/usr.bin/tip/remote.c (revision aeaecd48)
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 "tip.h"
44 #include "pathnames.h"
45 
46 /*
47  * Attributes to be gleened from remote host description
48  *   data base.
49  */
50 static char **caps[] = {
51 	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
52 	&ES, &EX, &FO, &RC, &RE, &PA, &LI, &LO
53 };
54 
55 static char *capstrings[] = {
56 	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
57 	"di", "es", "ex", "fo", "rc", "re", "pa", "li", "lo", 0
58 };
59 
60 static char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
61 
62 #define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
63 
64 static void getremcap(char *);
65 
66 /*
67 	Expand the start tilde sequence at the start of the
68 	specified path. Optionally, free space allocated to
69 	path before reinitializing it.
70 */
71 static int
72 expand_tilde (char **path, void (*free) (char *p))
73 {
74 	int rc = 0;
75 	char buffer [PATH_MAX];
76 	char *tailp;
77 	if ((tailp = strchr (*path + 1, '/')) != NULL)
78 	{
79 		struct passwd *pwd;
80 		*tailp++ = '\0';
81 		if (*(*path + 1) == '\0')
82 			strcpy (buffer, getlogin ());
83 		else
84 			strcpy (buffer, *path + 1);
85 		if ((pwd = getpwnam (buffer)) != NULL)
86 		{
87 			strcpy (buffer, pwd->pw_dir);
88 			strcat (buffer, "/");
89 			strcat (buffer, tailp);
90 			if (free)
91 				free (*path);
92 			*path = strdup (buffer);
93 			rc++;
94 		}
95 		return rc;
96 	}
97 	return (-1);
98 }
99 
100 static void
101 getremcap(char *host)
102 {
103 	char **p, ***q;
104 	char *bp;
105 	char *rempath;
106 	int   stat;
107 
108 	rempath = getenv("REMOTE");
109 	if (rempath != NULL) {
110 		if (*rempath != '/')
111 			/* we have an entry */
112 			cgetset(rempath);
113 		else {	/* we have a path */
114 			db_array[1] = rempath;
115 			db_array[2] = _PATH_REMOTE;
116 		}
117 	}
118 
119 	if ((stat = cgetent(&bp, db_array, host)) < 0) {
120 		if (DV ||
121 		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
122 			CU = DV;
123 			HO = host;
124 			HW = 1;
125 			DU = 0;
126 			if (!BR)
127 				BR = DEFBR;
128 			FS = BUFSIZ;
129 			return;
130 		}
131 		switch(stat) {
132 		case -1:
133 			warnx("unknown host %s", host);
134 			break;
135 		case -2:
136 			warnx("can't open host description file");
137 			break;
138 		case -3:
139 			warnx("possible reference loop in host description file");
140 			break;
141 		}
142 		exit(3);
143 	}
144 
145 	for (p = capstrings, q = caps; *p != NULL; p++, q++)
146 		if (**q == NULL)
147 			cgetstr(bp, *p, *q);
148 	if (!BR && (cgetnum(bp, "br", &BR) == -1))
149 		BR = DEFBR;
150 	if (cgetnum(bp, "fs", &FS) == -1)
151 		FS = BUFSIZ;
152 	if (DU < 0)
153 		DU = 0;
154 	else
155 		DU = cgetflag("du");
156 	if (DV == NULL) {
157 		fprintf(stderr, "%s: missing device spec\n", host);
158 		exit(3);
159 	}
160 	if (DU && CU == NULL)
161 		CU = DV;
162 	if (DU && PN == NULL) {
163 		fprintf(stderr, "%s: missing phone number\n", host);
164 		exit(3);
165 	}
166 
167 	HD = cgetflag("hd");
168 
169 	/*
170 	 * This effectively eliminates the "hw" attribute
171 	 *   from the description file
172 	 */
173 	if (!HW)
174 		HW = (CU == NULL) || (DU && equal(DV, CU));
175 	HO = host;
176 
177 	/*
178 		If login script, verify access
179 	*/
180 	if (LI != NULL) {
181 		if (*LI == '~')
182 			(void) expand_tilde (&LI, NULL);
183 		if (access (LI, F_OK | X_OK) != 0) {
184 			printf("tip (warning): can't open login script \"%s\"\n", LI);
185 			LI = NULL;
186 		}
187 	}
188 
189 	/*
190 		If logout script, verify access
191 	*/
192 	if (LO != NULL) {
193 		if (*LO == '~')
194 			(void) expand_tilde (&LO, NULL);
195 		if (access (LO, F_OK | X_OK) != 0) {
196 			printf("tip (warning): can't open logout script \"%s\"\n", LO);
197 			LO = NULL;
198 		}
199 	}
200 
201 	/*
202 	 * see if uppercase mode should be turned on initially
203 	 */
204 	if (cgetflag("ra"))
205 		boolean(value(RAISE)) = 1;
206 	if (cgetflag("ec"))
207 		boolean(value(ECHOCHECK)) = 1;
208 	if (cgetflag("be"))
209 		boolean(value(BEAUTIFY)) = 1;
210 	if (cgetflag("nb"))
211 		boolean(value(BEAUTIFY)) = 0;
212 	if (cgetflag("sc"))
213 		boolean(value(SCRIPT)) = 1;
214 	if (cgetflag("tb"))
215 		boolean(value(TABEXPAND)) = 1;
216 	if (cgetflag("vb"))
217 		boolean(value(VERBOSE)) = 1;
218 	if (cgetflag("nv"))
219 		boolean(value(VERBOSE)) = 0;
220 	if (cgetflag("ta"))
221 		boolean(value(TAND)) = 1;
222 	if (cgetflag("nt"))
223 		boolean(value(TAND)) = 0;
224 	if (cgetflag("rw"))
225 		boolean(value(RAWFTP)) = 1;
226 	if (cgetflag("hd"))
227 		boolean(value(HALFDUPLEX)) = 1;
228 	if (RE == NULL)
229 		RE = (char *)"tip.record";
230 	if (EX == NULL)
231 		EX = (char *)"\t\n\b\f";
232 	if (ES != NULL)
233 		vstring("es", ES);
234 	if (FO != NULL)
235 		vstring("fo", FO);
236 	if (PR != NULL)
237 		vstring("pr", PR);
238 	if (RC != NULL)
239 		vstring("rc", RC);
240 	if (cgetnum(bp, "dl", &DL) == -1)
241 		DL = 0;
242 	if (cgetnum(bp, "cl", &CL) == -1)
243 		CL = 0;
244 	if (cgetnum(bp, "et", &ET) == -1)
245 		ET = 10;
246 }
247 
248 char *
249 getremote(char *host)
250 {
251 	char *cp;
252 	static char *next;
253 	static int lookedup = 0;
254 
255 	if (!lookedup) {
256 		if (host == NULL && (host = getenv("HOST")) == NULL)
257 			errx(3, "no host specified");
258 		getremcap(host);
259 		next = DV;
260 		lookedup++;
261 	}
262 	/*
263 	 * We return a new device each time we're called (to allow
264 	 *   a rotary action to be simulated)
265 	 */
266 	if (next == NULL)
267 		return (NULL);
268 	if ((cp = index(next, ',')) == NULL) {
269 		DV = next;
270 		next = NULL;
271 	} else {
272 		*cp++ = '\0';
273 		DV = next;
274 		next = cp;
275 	}
276 	return (DV);
277 }
278