xref: /dragonfly/usr.sbin/rtadvd/advcap.c (revision 6b5c5d0d)
1 /*	$KAME: advcap.c,v 1.5 2001/02/01 09:12:08 jinmei Exp $	*/
2 
3 /*
4  * Copyright (c) 1983 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD: src/usr.sbin/rtadvd/advcap.c,v 1.1.2.2 2001/07/03 11:02:13 ume Exp $
36  * $DragonFly: src/usr.sbin/rtadvd/advcap.c,v 1.5 2005/02/17 14:00:10 joerg Exp $
37  */
38 
39 /*
40  * remcap - routines for dealing with the remote host data base
41  *
42  * derived from termcap
43  */
44 #include <sys/types.h>
45 #include <sys/uio.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <syslog.h>
52 #include <errno.h>
53 #include <string.h>
54 #include "pathnames.h"
55 
56 #ifndef BUFSIZ
57 #define	BUFSIZ		1024
58 #endif
59 #define MAXHOP		32		/* max number of tc= indirections */
60 
61 #define	tgetent		agetent
62 #define	tnchktc		anchktc
63 #define	tnamatch	anamatch
64 #define	tgetnum		agetnum
65 #define	tgetflag	agetflag
66 #define	tgetstr		agetstr
67 
68 #if 0
69 #define V_TERMCAP	"REMOTE"
70 #define V_TERM		"HOST"
71 #endif
72 
73 char	*RM;
74 
75 /*
76  * termcap - routines for dealing with the terminal capability data base
77  *
78  * BUG:		Should use a "last" pointer in tbuf, so that searching
79  *		for capabilities alphabetically would not be a n**2/2
80  *		process when large numbers of capabilities are given.
81  * Note:	If we add a last pointer now we will screw up the
82  *		tc capability. We really should compile termcap.
83  *
84  * Essentially all the work here is scanning and decoding escapes
85  * in string capabilities.  We don't use stdio because the editor
86  * doesn't, and because living w/o it is not hard.
87  */
88 
89 static	char *tbuf;
90 static	int hopcount;	/* detect infinite loops in termcap, init 0 */
91 
92 static	char *remotefile;
93 
94 extern char *conffile;
95 
96 int tgetent(char *, char *);
97 int getent(char *, char *, char *);
98 int tnchktc(void);
99 int tnamatch(char *);
100 static char *tskip(char *);
101 long long tgetnum(char *);
102 int tgetflag(char *);
103 char *tgetstr(char *, char **);
104 static char *tdecode(char *, char **);
105 
106 /*
107  * Get an entry for terminal name in buffer bp,
108  * from the termcap file.  Parse is very rudimentary;
109  * we just notice escaped newlines.
110  */
111 int
112 tgetent(char *bp, char *name)
113 {
114 	char *cp;
115 
116 	remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
117 	return (getent(bp, name, cp));
118 }
119 
120 int
121 getent(char *bp, char *name, char *cp)
122 {
123 	int c;
124 	int i = 0, cnt = 0;
125 	char ibuf[BUFSIZ];
126 	int tf;
127 
128 	tbuf = bp;
129 	tf = 0;
130 	/*
131 	 * TERMCAP can have one of two things in it. It can be the
132 	 * name of a file to use instead of /etc/termcap. In this
133 	 * case it better start with a "/". Or it can be an entry to
134 	 * use so we don't have to read the file. In this case it
135 	 * has to already have the newlines crunched out.
136 	 */
137 	if (cp && *cp) {
138 		tf = open(RM = cp, O_RDONLY);
139 	}
140 	if (tf < 0) {
141 		syslog(LOG_INFO,
142 		       "<%s> open: %s", __func__, strerror(errno));
143 		return (-2);
144 	}
145 	for (;;) {
146 		cp = bp;
147 		for (;;) {
148 			if (i == cnt) {
149 				cnt = read(tf, ibuf, BUFSIZ);
150 				if (cnt <= 0) {
151 					close(tf);
152 					return (0);
153 				}
154 				i = 0;
155 			}
156 			c = ibuf[i++];
157 			if (c == '\n') {
158 				if (cp > bp && cp[-1] == '\\') {
159 					cp--;
160 					continue;
161 				}
162 				break;
163 			}
164 			if (cp >= bp+BUFSIZ) {
165 				write(2,"Remcap entry too long\n", 23);
166 				break;
167 			} else
168 				*cp++ = c;
169 		}
170 		*cp = 0;
171 
172 		/*
173 		 * The real work for the match.
174 		 */
175 		if (tnamatch(name)) {
176 			close(tf);
177 			return (tnchktc());
178 		}
179 	}
180 }
181 
182 /*
183  * tnchktc: check the last entry, see if it's tc=xxx. If so,
184  * recursively find xxx and append that entry (minus the names)
185  * to take the place of the tc=xxx entry. This allows termcap
186  * entries to say "like an HP2621 but doesn't turn on the labels".
187  * Note that this works because of the left to right scan.
188  */
189 int
190 tnchktc(void)
191 {
192 	char *p, *q;
193 	char tcname[16];	/* name of similar terminal */
194 	char tcbuf[BUFSIZ];
195 	char *holdtbuf = tbuf;
196 	int l;
197 
198 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
199 	while (*--p != ':')
200 		if (p<tbuf) {
201 			write(2, "Bad remcap entry\n", 18);
202 			return (0);
203 		}
204 	p++;
205 	/* p now points to beginning of last field */
206 	if (p[0] != 't' || p[1] != 'c')
207 		return (1);
208 	strcpy(tcname, p+3);
209 	q = tcname;
210 	while (*q && *q != ':')
211 		q++;
212 	*q = 0;
213 	if (++hopcount > MAXHOP) {
214 		write(2, "Infinite tc= loop\n", 18);
215 		return (0);
216 	}
217 	if (getent(tcbuf, tcname, remotefile) != 1) {
218 		return (0);
219 	}
220 	for (q = tcbuf; *q++ != ':'; )
221 		;
222 	l = p - holdtbuf + strlen(q);
223 	if (l > BUFSIZ) {
224 		write(2, "Remcap entry too long\n", 23);
225 		q[BUFSIZ - (p-holdtbuf)] = 0;
226 	}
227 	strcpy(p, q);
228 	tbuf = holdtbuf;
229 	return (1);
230 }
231 
232 /*
233  * Tnamatch deals with name matching.  The first field of the termcap
234  * entry is a sequence of names separated by |'s, so we compare
235  * against each such name.  The normal : terminator after the last
236  * name (before the first field) stops us.
237  */
238 int
239 tnamatch(char *np)
240 {
241 	char *Np, *Bp;
242 
243 	Bp = tbuf;
244 	if (*Bp == '#')
245 		return (0);
246 	for (;;) {
247 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
248 			continue;
249 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
250 			return (1);
251 		while (*Bp && *Bp != ':' && *Bp != '|')
252 			Bp++;
253 		if (*Bp == 0 || *Bp == ':')
254 			return (0);
255 		Bp++;
256 	}
257 }
258 
259 /*
260  * Skip to the next field.  Notice that this is very dumb, not
261  * knowing about \: escapes or any such.  If necessary, :'s can be put
262  * into the termcap file in octal.
263  */
264 static char *
265 tskip(char *bp)
266 {
267 	int dquote;
268 
269 	dquote = 0;
270 	while (*bp) {
271 		switch (*bp) {
272 		case ':':
273 			if (!dquote)
274 				goto breakbreak;
275 			else
276 				bp++;
277 			break;
278 		case '\\':
279 			bp++;
280 			if (isdigit(*bp)) {
281 				while (isdigit(*bp++))
282 					;
283 			} else
284 				bp++;
285 		case '"':
286 			dquote = (dquote ? 1 : 0);
287 			bp++;
288 			break;
289 		default:
290 			bp++;
291 			break;
292 		}
293 	}
294 breakbreak:
295 	if (*bp == ':')
296 		bp++;
297 	return (bp);
298 }
299 
300 /*
301  * Return the (numeric) option id.
302  * Numeric options look like
303  *	li#80
304  * i.e. the option string is separated from the numeric value by
305  * a # character.  If the option is not found we return -1.
306  * Note that we handle octal numbers beginning with 0.
307  */
308 long long
309 tgetnum(char *id)
310 {
311 	long long i;
312 	int base;
313 	char *bp = tbuf;
314 
315 	for (;;) {
316 		bp = tskip(bp);
317 		if (*bp == 0)
318 			return (-1);
319 		if (strncmp(bp, id, strlen(id)) != 0)
320 			continue;
321 		bp += strlen(id);
322 		if (*bp == '@')
323 			return (-1);
324 		if (*bp != '#')
325 			continue;
326 		bp++;
327 		base = 10;
328 		if (*bp == '0')
329 			base = 8;
330 		i = 0;
331 		while (isdigit(*bp))
332 			i *= base, i += *bp++ - '0';
333 		return (i);
334 	}
335 }
336 
337 /*
338  * Handle a flag option.
339  * Flag options are given "naked", i.e. followed by a : or the end
340  * of the buffer.  Return 1 if we find the option, or 0 if it is
341  * not given.
342  */
343 int
344 tgetflag(char *id)
345 {
346 	char *bp = tbuf;
347 
348 	for (;;) {
349 		bp = tskip(bp);
350 		if (!*bp)
351 			return (0);
352 		if (strncmp(bp, id, strlen(id)) == 0) {
353 			bp += strlen(id);
354 			if (!*bp || *bp == ':')
355 				return (1);
356 			else if (*bp == '@')
357 				return (0);
358 		}
359 	}
360 }
361 
362 /*
363  * Get a string valued option.
364  * These are given as
365  *	cl=^Z
366  * Much decoding is done on the strings, and the strings are
367  * placed in area, which is a ref parameter which is updated.
368  * No checking on area overflow.
369  */
370 char *
371 tgetstr(char *id, char **area)
372 {
373 	char *bp = tbuf;
374 
375 	for (;;) {
376 		bp = tskip(bp);
377 		if (!*bp)
378 			return (0);
379 		if (strncmp(bp, id, strlen(id)) != 0)
380 			continue;
381 		bp += strlen(id);
382 		if (*bp == '@')
383 			return (0);
384 		if (*bp != '=')
385 			continue;
386 		bp++;
387 		return (tdecode(bp, area));
388 	}
389 }
390 
391 /*
392  * Tdecode does the grunt work to decode the
393  * string capability escapes.
394  */
395 static char *
396 tdecode(char *str, char **area)
397 {
398 	char *cp;
399 	int c;
400 	char *dp;
401 	int i;
402 	char term;
403 
404 	term = ':';
405 	cp = *area;
406 again:
407 	if (*str == '"') {
408 		term = '"';
409 		str++;
410 	}
411 	while ((c = *str++) && c != term) {
412 		switch (c) {
413 
414 		case '^':
415 			c = *str++ & 037;
416 			break;
417 
418 		case '\\':
419 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
420 			c = *str++;
421 nextc:
422 			if (*dp++ == c) {
423 				c = *dp++;
424 				break;
425 			}
426 			dp++;
427 			if (*dp)
428 				goto nextc;
429 			if (isdigit(c)) {
430 				c -= '0', i = 2;
431 				do
432 					c <<= 3, c |= *str++ - '0';
433 				while (--i && isdigit(*str));
434 			}
435 			break;
436 		}
437 		*cp++ = c;
438 	}
439 	if (c == term && term != ':') {
440 		term = ':';
441 		goto again;
442 	}
443 	*cp++ = 0;
444 	str = *area;
445 	*area = cp;
446 	return (str);
447 }
448