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