Home | History | Annotate | Line # | Download | only in common_source
common.c revision 1.24
      1  1.24  mycroft /*	$NetBSD: common.c,v 1.24 2002/09/18 23:27:25 mycroft Exp $	*/
      2   1.8      jtc 
      3   1.4      cgd /*
      4   1.5      cgd  * Copyright (c) 1983, 1993
      5   1.5      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.4      cgd  * (c) UNIX System Laboratories, Inc.
      7   1.4      cgd  * All or some portions of this file are derived from material licensed
      8   1.4      cgd  * to the University of California by American Telephone and Telegraph
      9   1.4      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.4      cgd  * the permission of UNIX System Laboratories, Inc.
     11   1.4      cgd  *
     12   1.4      cgd  * Redistribution and use in source and binary forms, with or without
     13   1.4      cgd  * modification, are permitted provided that the following conditions
     14   1.4      cgd  * are met:
     15   1.4      cgd  * 1. Redistributions of source code must retain the above copyright
     16   1.4      cgd  *    notice, this list of conditions and the following disclaimer.
     17   1.4      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.4      cgd  *    notice, this list of conditions and the following disclaimer in the
     19   1.4      cgd  *    documentation and/or other materials provided with the distribution.
     20   1.4      cgd  * 3. All advertising materials mentioning features or use of this software
     21   1.4      cgd  *    must display the following acknowledgement:
     22   1.4      cgd  *	This product includes software developed by the University of
     23   1.4      cgd  *	California, Berkeley and its contributors.
     24   1.4      cgd  * 4. Neither the name of the University nor the names of its contributors
     25   1.4      cgd  *    may be used to endorse or promote products derived from this software
     26   1.4      cgd  *    without specific prior written permission.
     27   1.4      cgd  *
     28   1.4      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.4      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.4      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.4      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.4      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.4      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.4      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.4      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.4      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.4      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.4      cgd  * SUCH DAMAGE.
     39   1.4      cgd  */
     40   1.4      cgd 
     41  1.11      mrg #include <sys/cdefs.h>
     42   1.4      cgd #ifndef lint
     43  1.11      mrg #if 0
     44  1.10      mrg static char sccsid[] = "@(#)common.c	8.5 (Berkeley) 4/28/95";
     45  1.11      mrg #else
     46  1.24  mycroft __RCSID("$NetBSD: common.c,v 1.24 2002/09/18 23:27:25 mycroft Exp $");
     47  1.11      mrg #endif
     48   1.4      cgd #endif /* not lint */
     49   1.4      cgd 
     50   1.5      cgd #include <sys/param.h>
     51   1.5      cgd #include <sys/stat.h>
     52  1.10      mrg #include <sys/time.h>
     53   1.5      cgd 
     54   1.5      cgd #include <sys/socket.h>
     55   1.5      cgd #include <netinet/in.h>
     56  1.11      mrg #include <arpa/inet.h>
     57   1.5      cgd #include <netdb.h>
     58   1.5      cgd 
     59   1.5      cgd #include <dirent.h>
     60   1.5      cgd #include <errno.h>
     61   1.5      cgd #include <unistd.h>
     62   1.5      cgd #include <stdlib.h>
     63   1.5      cgd #include <stdio.h>
     64   1.5      cgd #include <string.h>
     65  1.21   itojun #include <ifaddrs.h>
     66   1.5      cgd #include "lp.h"
     67   1.5      cgd #include "pathnames.h"
     68   1.5      cgd 
     69   1.4      cgd /*
     70   1.4      cgd  * Routines and data common to all the line printer functions.
     71   1.4      cgd  */
     72   1.4      cgd 
     73   1.5      cgd char	*AF;		/* accounting file */
     74   1.5      cgd long	 BR;		/* baud rate if lp is a tty */
     75   1.5      cgd char	*CF;		/* name of cifplot filter (per job) */
     76   1.5      cgd char	*DF;		/* name of tex filter (per job) */
     77   1.5      cgd long	 DU;		/* daeomon user-id */
     78   1.5      cgd long	 FC;		/* flags to clear if lp is a tty */
     79   1.5      cgd char	*FF;		/* form feed string */
     80   1.5      cgd long	 FS;		/* flags to set if lp is a tty */
     81   1.5      cgd char	*GF;		/* name of graph(1G) filter (per job) */
     82   1.5      cgd long	 HL;		/* print header last */
     83   1.5      cgd char	*IF;		/* name of input filter (created per job) */
     84   1.5      cgd char	*LF;		/* log file for error messages */
     85   1.5      cgd char	*LO;		/* lock file name */
     86   1.4      cgd char	*LP;		/* line printer device name */
     87   1.5      cgd long	 MC;		/* maximum number of copies allowed */
     88   1.6  hpeyerl char	*MS;		/* stty flags to set if lp is a tty */
     89   1.5      cgd long	 MX;		/* maximum number of blocks to copy */
     90   1.5      cgd char	*NF;		/* name of ditroff filter (per job) */
     91   1.5      cgd char	*OF;		/* name of output filter (created once) */
     92   1.5      cgd char	*PF;		/* name of vrast filter (per job) */
     93   1.5      cgd long	 PL;		/* page length */
     94   1.5      cgd long	 PW;		/* page width */
     95   1.5      cgd long	 PX;		/* page width in pixels */
     96   1.5      cgd long	 PY;		/* page length in pixels */
     97   1.5      cgd char	*RF;		/* name of fortran text filter (per job) */
     98   1.5      cgd char    *RG;		/* resricted group */
     99   1.4      cgd char	*RM;		/* remote machine name */
    100   1.4      cgd char	*RP;		/* remote printer name */
    101   1.5      cgd long	 RS;		/* restricted to those with local accounts */
    102   1.5      cgd long	 RW;		/* open LP for reading and writing */
    103   1.5      cgd long	 SB;		/* short banner instead of normal header */
    104   1.5      cgd long	 SC;		/* suppress multiple copies */
    105   1.5      cgd char	*SD;		/* spool directory */
    106   1.5      cgd long	 SF;		/* suppress FF on each print job */
    107   1.5      cgd long	 SH;		/* suppress header page */
    108   1.4      cgd char	*ST;		/* status file name */
    109   1.4      cgd char	*TF;		/* name of troff filter (per job) */
    110   1.5      cgd char	*TR;		/* trailer string to be output when Q empties */
    111   1.4      cgd char	*VF;		/* name of vplot filter (per job) */
    112   1.5      cgd long	 XC;		/* flags to clear for local mode */
    113   1.5      cgd long	 XS;		/* flags to set for local mode */
    114   1.4      cgd 
    115   1.4      cgd char	line[BUFSIZ];
    116  1.10      mrg int	remote;		/* true if sending files to a remote host */
    117   1.5      cgd 
    118   1.6  hpeyerl extern uid_t	uid, euid;
    119   1.6  hpeyerl 
    120  1.23      wiz static int compar(const void *, const void *);
    121   1.4      cgd 
    122   1.4      cgd /*
    123  1.10      mrg  * Create a TCP connection to host "rhost" at port "rport".
    124  1.10      mrg  * If rport == 0, then use the printer service port.
    125   1.4      cgd  * Most of this code comes from rcmd.c.
    126   1.4      cgd  */
    127   1.5      cgd int
    128  1.23      wiz getport(char *rhost, int rport)
    129   1.4      cgd {
    130  1.17   itojun 	struct addrinfo hints, *res, *r;
    131  1.15      mrg 	u_int timo = 1;
    132  1.15      mrg 	int s, lport = IPPORT_RESERVED - 1;
    133  1.17   itojun 	int error;
    134  1.18   itojun 	int refuse, trial;
    135  1.20   itojun 	char pbuf[NI_MAXSERV];
    136   1.4      cgd 
    137   1.4      cgd 	/*
    138   1.4      cgd 	 * Get the host address and port number to connect to.
    139   1.4      cgd 	 */
    140   1.4      cgd 	if (rhost == NULL)
    141   1.4      cgd 		fatal("no remote host to connect to");
    142  1.17   itojun 	memset(&hints, 0, sizeof(hints));
    143  1.17   itojun 	hints.ai_family = PF_UNSPEC;
    144  1.17   itojun 	hints.ai_socktype = SOCK_STREAM;
    145  1.20   itojun 	if (rport)
    146  1.20   itojun 		snprintf(pbuf, sizeof(pbuf), "%d", rport);
    147  1.20   itojun 	else
    148  1.20   itojun 		snprintf(pbuf, sizeof(pbuf), "printer");
    149  1.20   itojun 	error = getaddrinfo(rhost, pbuf, &hints, &res);
    150  1.17   itojun 	if (error)
    151  1.17   itojun 		fatal("printer/tcp: %s", gai_strerror(error));
    152   1.4      cgd 
    153   1.4      cgd 	/*
    154   1.4      cgd 	 * Try connecting to the server.
    155   1.4      cgd 	 */
    156  1.18   itojun retry:
    157  1.17   itojun 	s = -1;
    158  1.18   itojun 	refuse = trial = 0;
    159  1.17   itojun 	for (r = res; r; r = r->ai_next) {
    160  1.18   itojun 		trial++;
    161  1.18   itojun retryport:
    162  1.17   itojun 		seteuid(euid);
    163  1.17   itojun 		s = rresvport_af(&lport, r->ai_family);
    164  1.17   itojun 		seteuid(uid);
    165  1.17   itojun 		if (s < 0)
    166  1.17   itojun 			return(-1);
    167  1.17   itojun 		if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {
    168  1.17   itojun 			error = errno;
    169  1.17   itojun 			(void)close(s);
    170  1.17   itojun 			s = -1;
    171  1.17   itojun 			errno = error;
    172  1.17   itojun 			if (errno == EADDRINUSE) {
    173  1.17   itojun 				lport--;
    174  1.18   itojun 				goto retryport;
    175  1.18   itojun 			} else if (errno == ECONNREFUSED)
    176  1.18   itojun 				refuse++;
    177  1.17   itojun 			continue;
    178  1.17   itojun 		} else
    179  1.17   itojun 			break;
    180  1.18   itojun 	}
    181  1.18   itojun 	if (s < 0 && trial == refuse && timo <= 16) {
    182  1.18   itojun 		sleep(timo);
    183  1.18   itojun 		timo *= 2;
    184  1.18   itojun 		goto retry;
    185   1.4      cgd 	}
    186  1.17   itojun 	if (res)
    187  1.17   itojun 		freeaddrinfo(res);
    188   1.4      cgd 	return(s);
    189   1.4      cgd }
    190   1.4      cgd 
    191   1.4      cgd /*
    192   1.4      cgd  * Getline reads a line from the control file cfp, removes tabs, converts
    193   1.4      cgd  *  new-line to null and leaves it in line.
    194   1.4      cgd  * Returns 0 at EOF or the number of characters read.
    195   1.4      cgd  */
    196   1.5      cgd int
    197  1.23      wiz getline(FILE *cfp)
    198   1.4      cgd {
    199  1.11      mrg 	int linel = 0, c;
    200  1.11      mrg 	char *lp = line;
    201   1.4      cgd 
    202  1.11      mrg 	while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) {
    203   1.4      cgd 		if (c == EOF)
    204   1.4      cgd 			return(0);
    205   1.4      cgd 		if (c == '\t') {
    206   1.4      cgd 			do {
    207   1.4      cgd 				*lp++ = ' ';
    208   1.4      cgd 				linel++;
    209  1.11      mrg 			} while ((linel & 07) != 0 && linel+1 < sizeof(line));
    210   1.4      cgd 			continue;
    211   1.4      cgd 		}
    212   1.4      cgd 		*lp++ = c;
    213   1.4      cgd 		linel++;
    214   1.4      cgd 	}
    215   1.4      cgd 	*lp++ = '\0';
    216   1.4      cgd 	return(linel);
    217   1.4      cgd }
    218   1.4      cgd 
    219   1.4      cgd /*
    220   1.4      cgd  * Scan the current directory and make a list of daemon files sorted by
    221   1.4      cgd  * creation time.
    222   1.4      cgd  * Return the number of entries and a pointer to the list.
    223   1.4      cgd  */
    224   1.5      cgd int
    225  1.23      wiz getq(struct queue **namelist[])
    226   1.4      cgd {
    227  1.11      mrg 	struct dirent *d;
    228  1.11      mrg 	struct queue *q, **queue;
    229   1.4      cgd 	struct stat stbuf;
    230   1.4      cgd 	DIR *dirp;
    231  1.15      mrg 	u_int nitems, arraysz;
    232   1.4      cgd 
    233   1.6  hpeyerl 	seteuid(euid);
    234  1.22      wiz 	dirp = opendir(SD);
    235  1.22      wiz 	seteuid(uid);
    236  1.22      wiz 	if (dirp == NULL)
    237   1.4      cgd 		return(-1);
    238   1.4      cgd 	if (fstat(dirp->dd_fd, &stbuf) < 0)
    239   1.4      cgd 		goto errdone;
    240   1.4      cgd 
    241   1.4      cgd 	/*
    242   1.4      cgd 	 * Estimate the array size by taking the size of the directory file
    243   1.4      cgd 	 * and dividing it by a multiple of the minimum size entry.
    244   1.4      cgd 	 */
    245  1.15      mrg 	arraysz = (int)(stbuf.st_size / 24);
    246   1.4      cgd 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
    247   1.4      cgd 	if (queue == NULL)
    248   1.4      cgd 		goto errdone;
    249   1.4      cgd 
    250   1.4      cgd 	nitems = 0;
    251   1.4      cgd 	while ((d = readdir(dirp)) != NULL) {
    252   1.4      cgd 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    253   1.4      cgd 			continue;	/* daemon control files only */
    254   1.6  hpeyerl 		seteuid(euid);
    255  1.22      wiz 		if (stat(d->d_name, &stbuf) < 0) {
    256  1.22      wiz 			seteuid(uid);
    257   1.4      cgd 			continue;	/* Doesn't exist */
    258  1.22      wiz 		}
    259   1.6  hpeyerl 		seteuid(uid);
    260   1.4      cgd 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
    261   1.4      cgd 		if (q == NULL)
    262   1.4      cgd 			goto errdone;
    263   1.4      cgd 		q->q_time = stbuf.st_mtime;
    264   1.9      mrg 		strcpy(q->q_name, d->d_name);	/* XXX: strcpy is safe */
    265   1.4      cgd 		/*
    266   1.4      cgd 		 * Check to make sure the array has space left and
    267   1.4      cgd 		 * realloc the maximum size.
    268   1.4      cgd 		 */
    269   1.4      cgd 		if (++nitems > arraysz) {
    270  1.10      mrg 			arraysz *= 2;
    271  1.15      mrg 			queue = (struct queue **)realloc(queue,
    272  1.10      mrg 				arraysz * sizeof(struct queue *));
    273   1.4      cgd 			if (queue == NULL)
    274   1.4      cgd 				goto errdone;
    275   1.4      cgd 		}
    276   1.4      cgd 		queue[nitems-1] = q;
    277   1.4      cgd 	}
    278   1.4      cgd 	closedir(dirp);
    279   1.4      cgd 	if (nitems)
    280   1.4      cgd 		qsort(queue, nitems, sizeof(struct queue *), compar);
    281   1.4      cgd 	*namelist = queue;
    282   1.4      cgd 	return(nitems);
    283   1.4      cgd 
    284   1.4      cgd errdone:
    285   1.4      cgd 	closedir(dirp);
    286   1.4      cgd 	return(-1);
    287   1.4      cgd }
    288   1.4      cgd 
    289   1.4      cgd /*
    290   1.4      cgd  * Compare modification times.
    291   1.4      cgd  */
    292   1.5      cgd static int
    293  1.23      wiz compar(const void *p1, const void *p2)
    294   1.4      cgd {
    295   1.5      cgd 	if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time)
    296   1.4      cgd 		return(-1);
    297   1.5      cgd 	if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time)
    298   1.4      cgd 		return(1);
    299   1.4      cgd 	return(0);
    300   1.4      cgd }
    301   1.4      cgd 
    302   1.4      cgd /*
    303   1.4      cgd  * Figure out whether the local machine is the same
    304   1.4      cgd  * as the remote machine (RM) entry (if it exists).
    305   1.4      cgd  */
    306   1.4      cgd char *
    307  1.23      wiz checkremote(void)
    308   1.4      cgd {
    309  1.21   itojun 	char lname[NI_MAXHOST], rname[NI_MAXHOST];
    310  1.21   itojun 	struct addrinfo hints, *res, *res0;
    311   1.4      cgd 	static char errbuf[128];
    312  1.17   itojun 	int error;
    313  1.21   itojun 	struct ifaddrs *ifap, *ifa;
    314  1.21   itojun #ifdef NI_WITHSCOPEID
    315  1.21   itojun 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
    316  1.21   itojun #else
    317  1.21   itojun 	const int niflags = NI_NUMERICHOST;
    318  1.21   itojun #endif
    319  1.21   itojun #ifdef __KAME__
    320  1.21   itojun 	struct sockaddr_in6 sin6;
    321  1.21   itojun 	struct sockaddr_in6 *sin6p;
    322  1.21   itojun #endif
    323  1.21   itojun 
    324  1.21   itojun 	remote = 0;	/* assume printer is local on failure */
    325   1.4      cgd 
    326  1.21   itojun 	if (RM == NULL)
    327  1.21   itojun 		return NULL;
    328  1.21   itojun 
    329  1.21   itojun 	/* get the local interface addresses */
    330  1.21   itojun 	if (getifaddrs(&ifap) < 0) {
    331  1.21   itojun 		(void)snprintf(errbuf, sizeof(errbuf),
    332  1.21   itojun 		    "unable to get local interface address: %s",
    333  1.21   itojun 		    strerror(errno));
    334  1.21   itojun 		return errbuf;
    335  1.21   itojun 	}
    336   1.4      cgd 
    337  1.21   itojun 	/* get the remote host addresses (RM) */
    338  1.21   itojun 	memset(&hints, 0, sizeof(hints));
    339  1.21   itojun 	hints.ai_flags = AI_CANONNAME;
    340  1.21   itojun 	hints.ai_family = PF_UNSPEC;
    341  1.21   itojun 	hints.ai_socktype = SOCK_STREAM;
    342  1.21   itojun 	res = NULL;
    343  1.21   itojun 	error = getaddrinfo(RM, NULL, &hints, &res0);
    344  1.21   itojun 	if (error) {
    345  1.21   itojun 		(void)snprintf(errbuf, sizeof(errbuf),
    346  1.21   itojun 		    "unable to resolve remote machine %s: %s",
    347  1.21   itojun 		    RM, gai_strerror(error));
    348  1.21   itojun 		freeifaddrs(ifap);
    349  1.21   itojun 		return errbuf;
    350  1.21   itojun 	}
    351   1.4      cgd 
    352  1.21   itojun 	remote = 1;	/* assume printer is remote */
    353  1.17   itojun 
    354  1.21   itojun 	for (res = res0; res; res = res->ai_next) {
    355  1.21   itojun 		if (getnameinfo(res->ai_addr, res->ai_addrlen,
    356  1.21   itojun 		    rname, sizeof(rname), NULL, 0, niflags) != 0)
    357  1.21   itojun 			continue;
    358  1.21   itojun 		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    359  1.21   itojun #ifdef __KAME__
    360  1.21   itojun 			sin6p = (struct sockaddr_in6 *)ifa->ifa_addr;
    361  1.21   itojun 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
    362  1.21   itojun 			    ifa->ifa_addr->sa_len == sizeof(sin6) &&
    363  1.21   itojun 			    IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr) &&
    364  1.21   itojun 			    *(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]) {
    365  1.21   itojun 				/* kame scopeid hack */
    366  1.21   itojun 				memcpy(&sin6, ifa->ifa_addr, sizeof(sin6));
    367  1.21   itojun 				sin6.sin6_scope_id =
    368  1.21   itojun 				    ntohs(*(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]);
    369  1.21   itojun 				sin6.sin6_addr.s6_addr[2] = 0;
    370  1.21   itojun 				sin6.sin6_addr.s6_addr[3] = 0;
    371  1.21   itojun 				if (getnameinfo((struct sockaddr *)&sin6,
    372  1.21   itojun 				    sin6.sin6_len, lname, sizeof(lname),
    373  1.21   itojun 				    NULL, 0, niflags) != 0)
    374  1.21   itojun 					continue;
    375  1.21   itojun 			} else
    376  1.21   itojun #endif
    377  1.21   itojun 			if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
    378  1.21   itojun 			    lname, sizeof(lname), NULL, 0, niflags) != 0)
    379  1.21   itojun 				continue;
    380  1.21   itojun 
    381  1.21   itojun 			if (strcmp(rname, lname) == 0) {
    382  1.21   itojun 				remote = 0;
    383  1.21   itojun 				goto done;
    384  1.21   itojun 			}
    385  1.21   itojun 		}
    386   1.4      cgd 	}
    387  1.21   itojun done:
    388  1.21   itojun 	freeaddrinfo(res0);
    389  1.21   itojun 	freeifaddrs(ifap);
    390  1.10      mrg 	return NULL;
    391  1.10      mrg }
    392  1.10      mrg 
    393  1.10      mrg /* sleep n milliseconds */
    394  1.10      mrg void
    395  1.23      wiz delay(int n)
    396  1.10      mrg {
    397  1.24  mycroft 	struct timespec tdelay;
    398  1.10      mrg 
    399  1.10      mrg 	if (n <= 0 || n > 10000)
    400  1.10      mrg 		fatal("unreasonable delay period (%d)", n);
    401  1.10      mrg 	tdelay.tv_sec = n / 1000;
    402  1.24  mycroft 	tdelay.tv_nsec = (n % 1000) * 1000000;
    403  1.24  mycroft 	nanosleep(&tdelay, NULL);
    404   1.4      cgd }
    405