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