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