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