Home | History | Annotate | Line # | Download | only in common_source
common.c revision 1.14.4.1
      1  1.14.4.1  wrstuden /*	$NetBSD: common.c,v 1.14.4.1 1999/12/27 18:37:50 wrstuden 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.14.4.1  wrstuden __RCSID("$NetBSD: common.c,v 1.14.4.1 1999/12/27 18:37:50 wrstuden 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.5       cgd #include "lp.h"
     66       1.5       cgd #include "pathnames.h"
     67       1.5       cgd 
     68       1.4       cgd /*
     69       1.4       cgd  * Routines and data common to all the line printer functions.
     70       1.4       cgd  */
     71       1.4       cgd 
     72       1.5       cgd char	*AF;		/* accounting file */
     73       1.5       cgd long	 BR;		/* baud rate if lp is a tty */
     74       1.5       cgd char	*CF;		/* name of cifplot filter (per job) */
     75       1.5       cgd char	*DF;		/* name of tex filter (per job) */
     76       1.5       cgd long	 DU;		/* daeomon user-id */
     77       1.5       cgd long	 FC;		/* flags to clear if lp is a tty */
     78       1.5       cgd char	*FF;		/* form feed string */
     79       1.5       cgd long	 FS;		/* flags to set if lp is a tty */
     80       1.5       cgd char	*GF;		/* name of graph(1G) filter (per job) */
     81       1.5       cgd long	 HL;		/* print header last */
     82       1.5       cgd char	*IF;		/* name of input filter (created per job) */
     83       1.5       cgd char	*LF;		/* log file for error messages */
     84       1.5       cgd char	*LO;		/* lock file name */
     85       1.4       cgd char	*LP;		/* line printer device name */
     86       1.5       cgd long	 MC;		/* maximum number of copies allowed */
     87       1.6   hpeyerl char	*MS;		/* stty flags to set if lp is a tty */
     88       1.5       cgd long	 MX;		/* maximum number of blocks to copy */
     89       1.5       cgd char	*NF;		/* name of ditroff filter (per job) */
     90       1.5       cgd char	*OF;		/* name of output filter (created once) */
     91       1.5       cgd char	*PF;		/* name of vrast filter (per job) */
     92       1.5       cgd long	 PL;		/* page length */
     93       1.5       cgd long	 PW;		/* page width */
     94       1.5       cgd long	 PX;		/* page width in pixels */
     95       1.5       cgd long	 PY;		/* page length in pixels */
     96       1.5       cgd char	*RF;		/* name of fortran text filter (per job) */
     97       1.5       cgd char    *RG;		/* resricted group */
     98       1.4       cgd char	*RM;		/* remote machine name */
     99       1.4       cgd char	*RP;		/* remote printer name */
    100       1.5       cgd long	 RS;		/* restricted to those with local accounts */
    101       1.5       cgd long	 RW;		/* open LP for reading and writing */
    102       1.5       cgd long	 SB;		/* short banner instead of normal header */
    103       1.5       cgd long	 SC;		/* suppress multiple copies */
    104       1.5       cgd char	*SD;		/* spool directory */
    105       1.5       cgd long	 SF;		/* suppress FF on each print job */
    106       1.5       cgd long	 SH;		/* suppress header page */
    107       1.4       cgd char	*ST;		/* status file name */
    108       1.4       cgd char	*TF;		/* name of troff filter (per job) */
    109       1.5       cgd char	*TR;		/* trailer string to be output when Q empties */
    110       1.4       cgd char	*VF;		/* name of vplot filter (per job) */
    111       1.5       cgd long	 XC;		/* flags to clear for local mode */
    112       1.5       cgd long	 XS;		/* flags to set for local mode */
    113       1.4       cgd 
    114       1.4       cgd char	line[BUFSIZ];
    115      1.10       mrg int	remote;		/* true if sending files to a remote host */
    116       1.5       cgd 
    117       1.6   hpeyerl extern uid_t	uid, euid;
    118       1.6   hpeyerl 
    119       1.5       cgd static int compar __P((const void *, const void *));
    120       1.4       cgd 
    121       1.4       cgd /*
    122      1.10       mrg  * Create a TCP connection to host "rhost" at port "rport".
    123      1.10       mrg  * If rport == 0, then use the printer service port.
    124       1.4       cgd  * Most of this code comes from rcmd.c.
    125       1.4       cgd  */
    126       1.5       cgd int
    127      1.10       mrg getport(rhost, rport)
    128       1.4       cgd 	char *rhost;
    129      1.10       mrg 	int rport;
    130       1.4       cgd {
    131       1.4       cgd 	struct hostent *hp;
    132       1.4       cgd 	struct servent *sp;
    133       1.4       cgd 	struct sockaddr_in sin;
    134  1.14.4.1  wrstuden 	u_int timo = 1;
    135  1.14.4.1  wrstuden 	int s, lport = IPPORT_RESERVED - 1;
    136       1.4       cgd 	int err;
    137       1.4       cgd 
    138       1.4       cgd 	/*
    139       1.4       cgd 	 * Get the host address and port number to connect to.
    140       1.4       cgd 	 */
    141       1.4       cgd 	if (rhost == NULL)
    142       1.4       cgd 		fatal("no remote host to connect to");
    143  1.14.4.1  wrstuden 	memset(&sin, 0, sizeof(sin));
    144      1.11       mrg 	if (inet_aton(rhost, &sin.sin_addr) == 1)
    145      1.10       mrg 		sin.sin_family = AF_INET;
    146      1.10       mrg 	else {
    147      1.10       mrg 		hp = gethostbyname(rhost);
    148      1.10       mrg 		if (hp == NULL)
    149      1.10       mrg 			fatal("unknown host %s", rhost);
    150  1.14.4.1  wrstuden 		memmove(&sin.sin_addr, hp->h_addr, (size_t)hp->h_length);
    151      1.10       mrg 		sin.sin_family = hp->h_addrtype;
    152      1.10       mrg 	}
    153      1.10       mrg 	if (rport == 0) {
    154      1.10       mrg 		sp = getservbyname("printer", "tcp");
    155      1.10       mrg 		if (sp == NULL)
    156      1.10       mrg 			fatal("printer/tcp: unknown service");
    157      1.10       mrg 		sin.sin_port = sp->s_port;
    158      1.10       mrg 	} else
    159      1.10       mrg 		sin.sin_port = htons(rport);
    160       1.4       cgd 
    161       1.4       cgd 	/*
    162       1.4       cgd 	 * Try connecting to the server.
    163       1.4       cgd 	 */
    164       1.4       cgd retry:
    165       1.6   hpeyerl 	seteuid(euid);
    166       1.4       cgd 	s = rresvport(&lport);
    167       1.6   hpeyerl 	seteuid(uid);
    168       1.4       cgd 	if (s < 0)
    169       1.4       cgd 		return(-1);
    170  1.14.4.1  wrstuden 	if (connect(s, (const struct sockaddr *)&sin, sizeof(sin)) < 0) {
    171       1.4       cgd 		err = errno;
    172       1.9       mrg 		(void)close(s);
    173       1.4       cgd 		errno = err;
    174       1.4       cgd 		if (errno == EADDRINUSE) {
    175       1.4       cgd 			lport--;
    176       1.4       cgd 			goto retry;
    177       1.4       cgd 		}
    178       1.4       cgd 		if (errno == ECONNREFUSED && timo <= 16) {
    179       1.4       cgd 			sleep(timo);
    180       1.4       cgd 			timo *= 2;
    181       1.4       cgd 			goto retry;
    182       1.4       cgd 		}
    183       1.4       cgd 		return(-1);
    184       1.4       cgd 	}
    185       1.4       cgd 	return(s);
    186       1.4       cgd }
    187       1.4       cgd 
    188       1.4       cgd /*
    189       1.4       cgd  * Getline reads a line from the control file cfp, removes tabs, converts
    190       1.4       cgd  *  new-line to null and leaves it in line.
    191       1.4       cgd  * Returns 0 at EOF or the number of characters read.
    192       1.4       cgd  */
    193       1.5       cgd int
    194       1.4       cgd getline(cfp)
    195       1.4       cgd 	FILE *cfp;
    196       1.4       cgd {
    197      1.11       mrg 	int linel = 0, c;
    198      1.11       mrg 	char *lp = line;
    199       1.4       cgd 
    200      1.11       mrg 	while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) {
    201       1.4       cgd 		if (c == EOF)
    202       1.4       cgd 			return(0);
    203       1.4       cgd 		if (c == '\t') {
    204       1.4       cgd 			do {
    205       1.4       cgd 				*lp++ = ' ';
    206       1.4       cgd 				linel++;
    207      1.11       mrg 			} while ((linel & 07) != 0 && linel+1 < sizeof(line));
    208       1.4       cgd 			continue;
    209       1.4       cgd 		}
    210       1.4       cgd 		*lp++ = c;
    211       1.4       cgd 		linel++;
    212       1.4       cgd 	}
    213       1.4       cgd 	*lp++ = '\0';
    214       1.4       cgd 	return(linel);
    215       1.4       cgd }
    216       1.4       cgd 
    217       1.4       cgd /*
    218       1.4       cgd  * Scan the current directory and make a list of daemon files sorted by
    219       1.4       cgd  * creation time.
    220       1.4       cgd  * Return the number of entries and a pointer to the list.
    221       1.4       cgd  */
    222       1.5       cgd int
    223       1.4       cgd getq(namelist)
    224       1.4       cgd 	struct queue *(*namelist[]);
    225       1.4       cgd {
    226      1.11       mrg 	struct dirent *d;
    227      1.11       mrg 	struct queue *q, **queue;
    228       1.4       cgd 	struct stat stbuf;
    229       1.4       cgd 	DIR *dirp;
    230  1.14.4.1  wrstuden 	u_int nitems, arraysz;
    231       1.4       cgd 
    232       1.6   hpeyerl 	seteuid(euid);
    233       1.4       cgd 	if ((dirp = opendir(SD)) == NULL)
    234       1.4       cgd 		return(-1);
    235       1.4       cgd 	if (fstat(dirp->dd_fd, &stbuf) < 0)
    236       1.4       cgd 		goto errdone;
    237       1.6   hpeyerl 	seteuid(uid);
    238       1.4       cgd 
    239       1.4       cgd 	/*
    240       1.4       cgd 	 * Estimate the array size by taking the size of the directory file
    241       1.4       cgd 	 * and dividing it by a multiple of the minimum size entry.
    242       1.4       cgd 	 */
    243  1.14.4.1  wrstuden 	arraysz = (int)(stbuf.st_size / 24);
    244       1.4       cgd 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
    245       1.4       cgd 	if (queue == NULL)
    246       1.4       cgd 		goto errdone;
    247       1.4       cgd 
    248       1.4       cgd 	nitems = 0;
    249       1.4       cgd 	while ((d = readdir(dirp)) != NULL) {
    250       1.4       cgd 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    251       1.4       cgd 			continue;	/* daemon control files only */
    252       1.6   hpeyerl 		seteuid(euid);
    253       1.4       cgd 		if (stat(d->d_name, &stbuf) < 0)
    254       1.4       cgd 			continue;	/* Doesn't exist */
    255       1.6   hpeyerl 		seteuid(uid);
    256       1.4       cgd 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
    257       1.4       cgd 		if (q == NULL)
    258       1.4       cgd 			goto errdone;
    259       1.4       cgd 		q->q_time = stbuf.st_mtime;
    260       1.9       mrg 		strcpy(q->q_name, d->d_name);	/* XXX: strcpy is safe */
    261       1.4       cgd 		/*
    262       1.4       cgd 		 * Check to make sure the array has space left and
    263       1.4       cgd 		 * realloc the maximum size.
    264       1.4       cgd 		 */
    265       1.4       cgd 		if (++nitems > arraysz) {
    266      1.10       mrg 			arraysz *= 2;
    267  1.14.4.1  wrstuden 			queue = (struct queue **)realloc(queue,
    268      1.10       mrg 				arraysz * sizeof(struct queue *));
    269       1.4       cgd 			if (queue == NULL)
    270       1.4       cgd 				goto errdone;
    271       1.4       cgd 		}
    272       1.4       cgd 		queue[nitems-1] = q;
    273       1.4       cgd 	}
    274       1.4       cgd 	closedir(dirp);
    275       1.4       cgd 	if (nitems)
    276       1.4       cgd 		qsort(queue, nitems, sizeof(struct queue *), compar);
    277       1.4       cgd 	*namelist = queue;
    278       1.4       cgd 	return(nitems);
    279       1.4       cgd 
    280       1.4       cgd errdone:
    281       1.4       cgd 	closedir(dirp);
    282       1.4       cgd 	return(-1);
    283       1.4       cgd }
    284       1.4       cgd 
    285       1.4       cgd /*
    286       1.4       cgd  * Compare modification times.
    287       1.4       cgd  */
    288       1.5       cgd static int
    289       1.4       cgd compar(p1, p2)
    290       1.5       cgd 	const void *p1, *p2;
    291       1.4       cgd {
    292       1.5       cgd 	if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time)
    293       1.4       cgd 		return(-1);
    294       1.5       cgd 	if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time)
    295       1.4       cgd 		return(1);
    296       1.4       cgd 	return(0);
    297       1.4       cgd }
    298       1.4       cgd 
    299       1.4       cgd /*
    300       1.4       cgd  * Figure out whether the local machine is the same
    301       1.4       cgd  * as the remote machine (RM) entry (if it exists).
    302       1.4       cgd  */
    303       1.4       cgd char *
    304       1.4       cgd checkremote()
    305       1.4       cgd {
    306  1.14.4.1  wrstuden 	char hname[MAXHOSTNAMELEN + 1];
    307      1.11       mrg 	struct hostent *hp;
    308       1.4       cgd 	static char errbuf[128];
    309       1.4       cgd 
    310      1.10       mrg 	remote = 0;	/* assume printer is local */
    311      1.10       mrg 	if (RM != NULL) {
    312       1.4       cgd 		/* get the official name of the local host */
    313  1.14.4.1  wrstuden 		gethostname(hname, sizeof(hname));
    314  1.14.4.1  wrstuden 		hname[sizeof(hname)-1] = '\0';
    315  1.14.4.1  wrstuden 		hp = gethostbyname(hname);
    316       1.4       cgd 		if (hp == (struct hostent *) NULL) {
    317       1.9       mrg 		    (void)snprintf(errbuf, sizeof(errbuf),
    318       1.4       cgd 			"unable to get official name for local machine %s",
    319  1.14.4.1  wrstuden 			hname);
    320       1.4       cgd 		    return errbuf;
    321      1.11       mrg 		} else {
    322  1.14.4.1  wrstuden 			(void)strncpy(hname, hp->h_name, sizeof(hname) - 1);
    323  1.14.4.1  wrstuden 			hname[sizeof(hname) - 1] = '\0';
    324      1.11       mrg 		}
    325       1.4       cgd 
    326       1.4       cgd 		/* get the official name of RM */
    327       1.4       cgd 		hp = gethostbyname(RM);
    328       1.4       cgd 		if (hp == (struct hostent *) NULL) {
    329       1.9       mrg 		    (void)snprintf(errbuf, sizeof(errbuf),
    330       1.4       cgd 			"unable to get official name for remote machine %s",
    331       1.4       cgd 			RM);
    332       1.4       cgd 		    return errbuf;
    333       1.4       cgd 		}
    334       1.4       cgd 
    335       1.4       cgd 		/*
    336       1.4       cgd 		 * if the two hosts are not the same,
    337       1.4       cgd 		 * then the printer must be remote.
    338       1.4       cgd 		 */
    339  1.14.4.1  wrstuden 		if (strcasecmp(hname, hp->h_name) != 0)
    340      1.10       mrg 			remote = 1;
    341       1.4       cgd 	}
    342      1.10       mrg 	return NULL;
    343      1.10       mrg }
    344      1.10       mrg 
    345      1.10       mrg /* sleep n milliseconds */
    346      1.10       mrg void
    347      1.10       mrg delay(n)
    348      1.14   msaitoh 	int n;
    349      1.10       mrg {
    350      1.10       mrg 	struct timeval tdelay;
    351      1.10       mrg 
    352      1.10       mrg 	if (n <= 0 || n > 10000)
    353      1.10       mrg 		fatal("unreasonable delay period (%d)", n);
    354      1.10       mrg 	tdelay.tv_sec = n / 1000;
    355      1.10       mrg 	tdelay.tv_usec = n * 1000 % 1000000;
    356      1.10       mrg 	(void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
    357       1.4       cgd }
    358