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