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