Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.bin / 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. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * @(#) Copyright (c) 1992, 1993 The Regents of the University of California.  All rights reserved.
31  * @(#)remote.c 8.1 (Berkeley) 6/6/93
32  * $FreeBSD: src/usr.bin/tip/tip/remote.c,v 1.4 1999/08/28 01:06:35 peter Exp $
33  */
34
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include "tip.h"
40 #include "pathnames.h"
41
42 /*
43  * Attributes to be gleened from remote host description
44  *   data base.
45  */
46 static char **caps[] = {
47         &AT, &DV, &CM, &CU, &EL, &IE, &OE, &PN, &PR, &DI,
48         &ES, &EX, &FO, &RC, &RE, &PA, &LI, &LO
49 };
50
51 static char *capstrings[] = {
52         "at", "dv", "cm", "cu", "el", "ie", "oe", "pn", "pr",
53         "di", "es", "ex", "fo", "rc", "re", "pa", "li", "lo", 0
54 };
55
56 static char     *db_array[3] = { _PATH_REMOTE, 0, 0 };
57
58 #define cgetflag(f)     (cgetcap(bp, f, ':') != NULL)
59
60 static void getremcap(char *);
61
62 /*
63         Expand the start tilde sequence at the start of the
64         specified path. Optionally, free space allocated to
65         path before reinitializing it.
66 */
67 static int
68 expand_tilde (char **path, void (*free) (char *p))
69 {
70         int rc = 0;
71         char buffer [PATH_MAX];
72         char *tailp;
73         if ((tailp = strchr (*path + 1, '/')) != NULL)
74         {
75                 struct passwd *pwd;
76                 *tailp++ = '\0';
77                 if (*(*path + 1) == '\0')
78                         strcpy (buffer, getlogin ());
79                 else
80                         strcpy (buffer, *path + 1);
81                 if ((pwd = getpwnam (buffer)) != NULL)
82                 {
83                         strcpy (buffer, pwd->pw_dir);
84                         strcat (buffer, "/");
85                         strcat (buffer, tailp);
86                         if (free)
87                                 free (*path);
88                         *path = strdup (buffer);
89                         rc++;
90                 }
91                 return rc;
92         }
93         return (-1);
94 }
95
96 static void
97 getremcap(char *host)
98 {
99         char **p, ***q;
100         char *bp;
101         char *rempath;
102         int   stat;
103
104         rempath = getenv("REMOTE");
105         if (rempath != NULL) {
106                 if (*rempath != '/')
107                         /* we have an entry */
108                         cgetset(rempath);
109                 else {  /* we have a path */
110                         db_array[1] = rempath;
111                         db_array[2] = _PATH_REMOTE;
112                 }
113         }
114
115         if ((stat = cgetent(&bp, db_array, host)) < 0) {
116                 if (DV ||
117                     (host[0] == '/' && access(DV = host, R_OK | W_OK) == 0)) {
118                         CU = DV;
119                         HO = host;
120                         HW = 1;
121                         DU = 0;
122                         if (!BR)
123                                 BR = DEFBR;
124                         FS = BUFSIZ;
125                         return;
126                 }
127                 switch(stat) {
128                 case -1:
129                         warnx("unknown host %s", host);
130                         break;
131                 case -2:
132                         warnx("can't open host description file");
133                         break;
134                 case -3:
135                         warnx("possible reference loop in host description file");
136                         break;
137                 }
138                 exit(3);
139         }
140
141         for (p = capstrings, q = caps; *p != NULL; p++, q++)
142                 if (**q == NULL)
143                         cgetstr(bp, *p, *q);
144         if (!BR && (cgetnum(bp, "br", &BR) == -1))
145                 BR = DEFBR;
146         if (cgetnum(bp, "fs", &FS) == -1)
147                 FS = BUFSIZ;
148         if (DU < 0)
149                 DU = 0;
150         else
151                 DU = cgetflag("du");
152         if (DV == NULL) {
153                 fprintf(stderr, "%s: missing device spec\n", host);
154                 exit(3);
155         }
156         if (DU && CU == NULL)
157                 CU = DV;
158         if (DU && PN == NULL) {
159                 fprintf(stderr, "%s: missing phone number\n", host);
160                 exit(3);
161         }
162
163         HD = cgetflag("hd");
164
165         /*
166          * This effectively eliminates the "hw" attribute
167          *   from the description file
168          */
169         if (!HW)
170                 HW = (CU == NULL) || (DU && equal(DV, CU));
171         HO = host;
172
173         /*
174                 If login script, verify access
175         */
176         if (LI != NULL) {
177                 if (*LI == '~')
178                         (void) expand_tilde (&LI, NULL);
179                 if (access (LI, F_OK | X_OK) != 0) {
180                         printf("tip (warning): can't open login script \"%s\"\n", LI);
181                         LI = NULL;
182                 }
183         }
184
185         /*
186                 If logout script, verify access
187         */
188         if (LO != NULL) {
189                 if (*LO == '~')
190                         (void) expand_tilde (&LO, NULL);
191                 if (access (LO, F_OK | X_OK) != 0) {
192                         printf("tip (warning): can't open logout script \"%s\"\n", LO);
193                         LO = NULL;
194                 }
195         }
196
197         /*
198          * see if uppercase mode should be turned on initially
199          */
200         if (cgetflag("ra"))
201                 boolean(value(RAISE)) = 1;
202         if (cgetflag("ec"))
203                 boolean(value(ECHOCHECK)) = 1;
204         if (cgetflag("be"))
205                 boolean(value(BEAUTIFY)) = 1;
206         if (cgetflag("nb"))
207                 boolean(value(BEAUTIFY)) = 0;
208         if (cgetflag("sc"))
209                 boolean(value(SCRIPT)) = 1;
210         if (cgetflag("tb"))
211                 boolean(value(TABEXPAND)) = 1;
212         if (cgetflag("vb"))
213                 boolean(value(VERBOSE)) = 1;
214         if (cgetflag("nv"))
215                 boolean(value(VERBOSE)) = 0;
216         if (cgetflag("ta"))
217                 boolean(value(TAND)) = 1;
218         if (cgetflag("nt"))
219                 boolean(value(TAND)) = 0;
220         if (cgetflag("rw"))
221                 boolean(value(RAWFTP)) = 1;
222         if (cgetflag("hd"))
223                 boolean(value(HALFDUPLEX)) = 1;
224         if (RE == NULL)
225                 RE = (char *)"tip.record";
226         if (EX == NULL)
227                 EX = (char *)"\t\n\b\f";
228         if (ES != NULL)
229                 vstring("es", ES);
230         if (FO != NULL)
231                 vstring("fo", FO);
232         if (PR != NULL)
233                 vstring("pr", PR);
234         if (RC != NULL)
235                 vstring("rc", RC);
236         if (cgetnum(bp, "dl", &DL) == -1)
237                 DL = 0;
238         if (cgetnum(bp, "cl", &CL) == -1)
239                 CL = 0;
240         if (cgetnum(bp, "et", &ET) == -1)
241                 ET = 10;
242 }
243
244 char *
245 getremote(char *host)
246 {
247         char *cp;
248         static char *next;
249         static int lookedup = 0;
250
251         if (!lookedup) {
252                 if (host == NULL && (host = getenv("HOST")) == NULL)
253                         errx(3, "no host specified");
254                 getremcap(host);
255                 next = DV;
256                 lookedup++;
257         }
258         /*
259          * We return a new device each time we're called (to allow
260          *   a rotary action to be simulated)
261          */
262         if (next == NULL)
263                 return (NULL);
264         if ((cp = index(next, ',')) == NULL) {
265                 DV = next;
266                 next = NULL;
267         } else {
268                 *cp++ = '\0';
269                 DV = next;
270                 next = cp;
271         }
272         return (DV);
273 }