1161754Sru/*	$OpenBSD: remote.c,v 1.16 2006/06/06 23:24:52 deraadt Exp $	*/
288276Smarkm/*	$NetBSD: remote.c,v 1.5 1997/04/20 00:02:45 mellon Exp $	*/
388276Smarkm
4331722Seadler/*
57527Sjkh * Copyright (c) 1992, 1993
67527Sjkh *	The Regents of the University of California.  All rights reserved.
77527Sjkh *
87527Sjkh *
97527Sjkh * Redistribution and use in source and binary forms, with or without
107527Sjkh * modification, are permitted provided that the following conditions
117527Sjkh * are met:
127527Sjkh * 1. Redistributions of source code must retain the above copyright
137527Sjkh *    notice, this list of conditions and the following disclaimer.
147527Sjkh * 2. Redistributions in binary form must reproduce the above copyright
157527Sjkh *    notice, this list of conditions and the following disclaimer in the
167527Sjkh *    documentation and/or other materials provided with the distribution.
17161754Sru * 3. Neither the name of the University nor the names of its contributors
187527Sjkh *    may be used to endorse or promote products derived from this software
197527Sjkh *    without specific prior written permission.
207527Sjkh *
217527Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227527Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237527Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247527Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257527Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267527Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277527Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287527Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297527Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307527Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317527Sjkh * SUCH DAMAGE.
327527Sjkh */
337527Sjkh
3488276Smarkm#include <sys/cdefs.h>
3588276Smarkm__FBSDID("$FreeBSD$");
3688276Smarkm
377527Sjkh#ifndef lint
38161754Srustatic const char copyright[] =
397527Sjkh"@(#) Copyright (c) 1992, 1993\n\
407527Sjkh	The Regents of the University of California.  All rights reserved.\n";
417527Sjkh#endif /* not lint */
427527Sjkh
437527Sjkh#ifndef lint
4428365Scharnier#if 0
457527Sjkhstatic char sccsid[] = "@(#)remote.c	8.1 (Berkeley) 6/6/93";
46161754Srustatic const char rcsid[] = "$OpenBSD: remote.c,v 1.16 2006/06/06 23:24:52 deraadt Exp $";
4728365Scharnier#endif
487527Sjkh#endif /* not lint */
497527Sjkh
507527Sjkh#include <stdio.h>
517527Sjkh#include <stdlib.h>
527527Sjkh
5388276Smarkm#include "pathnames.h"
547527Sjkh#include "tip.h"
557527Sjkh
567527Sjkh/*
577527Sjkh * Attributes to be gleened from remote host description
587527Sjkh *   data base.
597527Sjkh */
607527Sjkhstatic char **caps[] = {
617527Sjkh	&AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
6288276Smarkm	&ES, &EX, &FO, &RC, &RE, &PA
637527Sjkh};
647527Sjkh
657527Sjkhstatic char *capstrings[] = {
667527Sjkh	"at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
6788276Smarkm	"di", "es", "ex", "fo", "rc", "re", "pa", 0
687527Sjkh};
697527Sjkh
707527Sjkhstatic char	*db_array[3] = { _PATH_REMOTE, 0, 0 };
717527Sjkh
727527Sjkh#define cgetflag(f)	(cgetcap(bp, f, ':') != NULL)
737527Sjkh
74161754Srustatic void	getremcap(char *);
75161754Sru
7628365Scharnierstatic void
77161754Srugetremcap(char *host)
787527Sjkh{
79161754Sru	char **p, ***q, *bp, *rempath;
807527Sjkh	int   stat;
817527Sjkh
827527Sjkh	rempath = getenv("REMOTE");
8388276Smarkm	if (rempath != NULL) {
847527Sjkh		if (*rempath != '/')
857527Sjkh			/* we have an entry */
867527Sjkh			cgetset(rempath);
877527Sjkh		else {	/* we have a path */
887527Sjkh			db_array[1] = rempath;
897527Sjkh			db_array[2] = _PATH_REMOTE;
907527Sjkh		}
9188276Smarkm	}
927527Sjkh
937527Sjkh	if ((stat = cgetent(&bp, db_array, host)) < 0) {
94161754Sru		if ((DV != NULL) ||
95161754Sru		    (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
967527Sjkh			CU = DV;
977527Sjkh			HO = host;
987527Sjkh			HW = 1;
997527Sjkh			DU = 0;
1007527Sjkh			if (!BR)
1017527Sjkh				BR = DEFBR;
1027527Sjkh			FS = DEFFS;
1037527Sjkh			return;
1047527Sjkh		}
105161754Sru		switch (stat) {
1067527Sjkh		case -1:
10788276Smarkm			fprintf(stderr, "%s: unknown host %s\n", __progname,
10888276Smarkm			    host);
1097527Sjkh			break;
1107527Sjkh		case -2:
111161754Sru			fprintf(stderr,
11288276Smarkm			    "%s: can't open host description file\n",
11388276Smarkm			    __progname);
1147527Sjkh			break;
1157527Sjkh		case -3:
116161754Sru			fprintf(stderr,
11788276Smarkm			    "%s: possible reference loop in host description file\n", __progname);
1187527Sjkh			break;
1197527Sjkh		}
1207527Sjkh		exit(3);
1217527Sjkh	}
1227527Sjkh
1237527Sjkh	for (p = capstrings, q = caps; *p != NULL; p++, q++)
1247527Sjkh		if (**q == NULL)
1257527Sjkh			cgetstr(bp, *p, *q);
1267527Sjkh	if (!BR && (cgetnum(bp, "br", &BR) == -1))
1277527Sjkh		BR = DEFBR;
128161754Sru	if (!LD && (cgetnum(bp, "ld", &LD) == -1))
129161754Sru		LD = TTYDISC;
1307527Sjkh	if (cgetnum(bp, "fs", &FS) == -1)
1317527Sjkh		FS = DEFFS;
1327527Sjkh	if (DU < 0)
1337527Sjkh		DU = 0;
1347527Sjkh	else
1357527Sjkh		DU = cgetflag("du");
1367527Sjkh	if (DV == NOSTR) {
1377527Sjkh		fprintf(stderr, "%s: missing device spec\n", host);
1387527Sjkh		exit(3);
1397527Sjkh	}
1407527Sjkh	if (DU && CU == NOSTR)
1417527Sjkh		CU = DV;
1427527Sjkh	if (DU && PN == NOSTR) {
1437527Sjkh		fprintf(stderr, "%s: missing phone number\n", host);
1447527Sjkh		exit(3);
1457527Sjkh	}
14688276Smarkm	if (DU && AT == NOSTR) {
14788276Smarkm		fprintf(stderr, "%s: missing acu type\n", host);
14888276Smarkm		exit(3);
14988276Smarkm	}
1507527Sjkh
1517527Sjkh	HD = cgetflag("hd");
1527527Sjkh
1537527Sjkh	/*
1547527Sjkh	 * This effectively eliminates the "hw" attribute
1557527Sjkh	 *   from the description file
1567527Sjkh	 */
1577527Sjkh	if (!HW)
1587527Sjkh		HW = (CU == NOSTR) || (DU && equal(DV, CU));
1597527Sjkh	HO = host;
1607527Sjkh	/*
1617527Sjkh	 * see if uppercase mode should be turned on initially
1627527Sjkh	 */
1637527Sjkh	if (cgetflag("ra"))
16488276Smarkm		setboolean(value(RAISE), 1);
1657527Sjkh	if (cgetflag("ec"))
16688276Smarkm		setboolean(value(ECHOCHECK), 1);
1677527Sjkh	if (cgetflag("be"))
16888276Smarkm		setboolean(value(BEAUTIFY), 1);
1697527Sjkh	if (cgetflag("nb"))
17088276Smarkm		setboolean(value(BEAUTIFY), 0);
1717527Sjkh	if (cgetflag("sc"))
17288276Smarkm		setboolean(value(SCRIPT), 1);
1737527Sjkh	if (cgetflag("tb"))
17488276Smarkm		setboolean(value(TABEXPAND), 1);
1757527Sjkh	if (cgetflag("vb"))
17688276Smarkm		setboolean(value(VERBOSE), 1);
1777527Sjkh	if (cgetflag("nv"))
17888276Smarkm		setboolean(value(VERBOSE), 0);
1797527Sjkh	if (cgetflag("ta"))
18088276Smarkm		setboolean(value(TAND), 1);
1817527Sjkh	if (cgetflag("nt"))
18288276Smarkm		setboolean(value(TAND), 0);
1837527Sjkh	if (cgetflag("rw"))
18488276Smarkm		setboolean(value(RAWFTP), 1);
1857527Sjkh	if (cgetflag("hd"))
18688276Smarkm		setboolean(value(HALFDUPLEX), 1);
18788276Smarkm	if (cgetflag("dc"))
18888276Smarkm		setboolean(value(DC), 1);
189161754Sru	if (cgetflag("hf"))
190161754Sru		setboolean(value(HARDWAREFLOW), 1);
1917527Sjkh	if (RE == NOSTR)
1927527Sjkh		RE = (char *)"tip.record";
1937527Sjkh	if (EX == NOSTR)
1947527Sjkh		EX = (char *)"\t\n\b\f";
1957527Sjkh	if (ES != NOSTR)
1967527Sjkh		vstring("es", ES);
1977527Sjkh	if (FO != NOSTR)
1987527Sjkh		vstring("fo", FO);
1997527Sjkh	if (PR != NOSTR)
2007527Sjkh		vstring("pr", PR);
2017527Sjkh	if (RC != NOSTR)
2027527Sjkh		vstring("rc", RC);
2037527Sjkh	if (cgetnum(bp, "dl", &DL) == -1)
2047527Sjkh		DL = 0;
2057527Sjkh	if (cgetnum(bp, "cl", &CL) == -1)
2067527Sjkh		CL = 0;
2077527Sjkh	if (cgetnum(bp, "et", &ET) == -1)
2087527Sjkh		ET = 10;
2097527Sjkh}
2107527Sjkh
2117527Sjkhchar *
212161754Srugetremote(char *host)
2137527Sjkh{
21488276Smarkm	char *cp;
2157527Sjkh	static char *next;
2167527Sjkh	static int lookedup = 0;
2177527Sjkh
2187527Sjkh	if (!lookedup) {
21988276Smarkm		if (host == NOSTR && (host = getenv("HOST")) == NOSTR) {
22088276Smarkm			fprintf(stderr, "%s: no host specified\n", __progname);
22188276Smarkm			exit(3);
22288276Smarkm		}
2237527Sjkh		getremcap(host);
2247527Sjkh		next = DV;
2257527Sjkh		lookedup++;
2267527Sjkh	}
2277527Sjkh	/*
2287527Sjkh	 * We return a new device each time we're called (to allow
2297527Sjkh	 *   a rotary action to be simulated)
2307527Sjkh	 */
2317527Sjkh	if (next == NOSTR)
2327527Sjkh		return (NOSTR);
23388276Smarkm	if ((cp = strchr(next, ',')) == NULL) {
2347527Sjkh		DV = next;
2357527Sjkh		next = NOSTR;
2367527Sjkh	} else {
2377527Sjkh		*cp++ = '\0';
2387527Sjkh		DV = next;
2397527Sjkh		next = cp;
2407527Sjkh	}
2417527Sjkh	return (DV);
2427527Sjkh}
243