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