common.c revision 1.20.4.1 1 /* $NetBSD: common.c,v 1.20.4.1 2001/04/26 08:12:47 he 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.20.4.1 2001/04/26 08:12:47 he 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 char pbuf[NI_MAXSERV];
137
138 /*
139 * Get the host address and port number to connect to.
140 */
141 if (rhost == NULL)
142 fatal("no remote host to connect to");
143 memset(&hints, 0, sizeof(hints));
144 hints.ai_family = PF_UNSPEC;
145 hints.ai_socktype = SOCK_STREAM;
146 if (rport)
147 snprintf(pbuf, sizeof(pbuf), "%d", rport);
148 else
149 snprintf(pbuf, sizeof(pbuf), "printer");
150 error = getaddrinfo(rhost, pbuf, &hints, &res);
151 if (error)
152 fatal("printer/tcp: %s", gai_strerror(error));
153
154 /*
155 * Try connecting to the server.
156 */
157 retry:
158 s = -1;
159 refuse = trial = 0;
160 for (r = res; r; r = r->ai_next) {
161 trial++;
162 retryport:
163 seteuid(euid);
164 s = rresvport_af(&lport, r->ai_family);
165 seteuid(uid);
166 if (s < 0)
167 return(-1);
168 if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {
169 error = errno;
170 (void)close(s);
171 s = -1;
172 errno = error;
173 if (errno == EADDRINUSE) {
174 lport--;
175 goto retryport;
176 } else if (errno == ECONNREFUSED)
177 refuse++;
178 continue;
179 } else
180 break;
181 }
182 if (s < 0 && trial == refuse && timo <= 16) {
183 sleep(timo);
184 timo *= 2;
185 goto retry;
186 }
187 if (res)
188 freeaddrinfo(res);
189 return(s);
190 }
191
192 /*
193 * Getline reads a line from the control file cfp, removes tabs, converts
194 * new-line to null and leaves it in line.
195 * Returns 0 at EOF or the number of characters read.
196 */
197 int
198 getline(cfp)
199 FILE *cfp;
200 {
201 int linel = 0, c;
202 char *lp = line;
203
204 while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) {
205 if (c == EOF)
206 return(0);
207 if (c == '\t') {
208 do {
209 *lp++ = ' ';
210 linel++;
211 } while ((linel & 07) != 0 && linel+1 < sizeof(line));
212 continue;
213 }
214 *lp++ = c;
215 linel++;
216 }
217 *lp++ = '\0';
218 return(linel);
219 }
220
221 /*
222 * Scan the current directory and make a list of daemon files sorted by
223 * creation time.
224 * Return the number of entries and a pointer to the list.
225 */
226 int
227 getq(namelist)
228 struct queue *(*namelist[]);
229 {
230 struct dirent *d;
231 struct queue *q, **queue;
232 struct stat stbuf;
233 DIR *dirp;
234 u_int nitems, arraysz;
235
236 seteuid(euid);
237 dirp = opendir(SD);
238 seteuid(uid);
239 if (dirp == NULL)
240 return(-1);
241 if (fstat(dirp->dd_fd, &stbuf) < 0)
242 goto errdone;
243
244 /*
245 * Estimate the array size by taking the size of the directory file
246 * and dividing it by a multiple of the minimum size entry.
247 */
248 arraysz = (int)(stbuf.st_size / 24);
249 queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
250 if (queue == NULL)
251 goto errdone;
252
253 nitems = 0;
254 while ((d = readdir(dirp)) != NULL) {
255 if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
256 continue; /* daemon control files only */
257 seteuid(euid);
258 if (stat(d->d_name, &stbuf) < 0) {
259 seteuid(uid);
260 continue; /* Doesn't exist */
261 }
262 seteuid(uid);
263 q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
264 if (q == NULL)
265 goto errdone;
266 q->q_time = stbuf.st_mtime;
267 strcpy(q->q_name, d->d_name); /* XXX: strcpy is safe */
268 /*
269 * Check to make sure the array has space left and
270 * realloc the maximum size.
271 */
272 if (++nitems > arraysz) {
273 arraysz *= 2;
274 queue = (struct queue **)realloc(queue,
275 arraysz * sizeof(struct queue *));
276 if (queue == NULL)
277 goto errdone;
278 }
279 queue[nitems-1] = q;
280 }
281 closedir(dirp);
282 if (nitems)
283 qsort(queue, nitems, sizeof(struct queue *), compar);
284 *namelist = queue;
285 return(nitems);
286
287 errdone:
288 closedir(dirp);
289 return(-1);
290 }
291
292 /*
293 * Compare modification times.
294 */
295 static int
296 compar(p1, p2)
297 const void *p1, *p2;
298 {
299 if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time)
300 return(-1);
301 if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time)
302 return(1);
303 return(0);
304 }
305
306 /*
307 * Figure out whether the local machine is the same
308 * as the remote machine (RM) entry (if it exists).
309 *
310 * XXX not really the right way to determine.
311 */
312 char *
313 checkremote()
314 {
315 char hname[NI_MAXHOST];
316 struct addrinfo hints, *res;
317 static char errbuf[128];
318 int error;
319
320 remote = 0; /* assume printer is local */
321 if (RM != NULL) {
322 /* get the official name of the local host */
323 gethostname(hname, sizeof(hname));
324 hname[sizeof(hname)-1] = '\0';
325
326 memset(&hints, 0, sizeof(hints));
327 hints.ai_flags = AI_CANONNAME;
328 hints.ai_family = PF_UNSPEC;
329 hints.ai_socktype = SOCK_STREAM;
330 res = NULL;
331 error = getaddrinfo(hname, NULL, &hints, &res);
332 if (error || !res->ai_canonname) {
333 (void)snprintf(errbuf, sizeof(errbuf),
334 "unable to get official name for local machine %s: "
335 "%s", hname, gai_strerror(error));
336 if (res)
337 freeaddrinfo(res);
338 return errbuf;
339 } else {
340 (void)strncpy(hname, res->ai_canonname,
341 sizeof(hname) - 1);
342 hname[sizeof(hname) - 1] = '\0';
343 }
344 freeaddrinfo(res);
345
346 /* get the official name of RM */
347 memset(&hints, 0, sizeof(hints));
348 hints.ai_flags = AI_CANONNAME;
349 hints.ai_family = PF_UNSPEC;
350 hints.ai_socktype = SOCK_STREAM;
351 res = NULL;
352 error = getaddrinfo(RM, NULL, &hints, &res);
353 if (error || !res->ai_canonname) {
354 (void)snprintf(errbuf, sizeof(errbuf),
355 "unable to get official name for local machine %s: "
356 "%s", RM, gai_strerror(error));
357 if (res)
358 freeaddrinfo(res);
359 return errbuf;
360 }
361
362 /*
363 * if the two hosts are not the same,
364 * then the printer must be remote.
365 */
366 if (strcasecmp(hname, res->ai_canonname) != 0)
367 remote = 1;
368
369 freeaddrinfo(res);
370 }
371 return NULL;
372 }
373
374 /* sleep n milliseconds */
375 void
376 delay(n)
377 int n;
378 {
379 struct timeval tdelay;
380
381 if (n <= 0 || n > 10000)
382 fatal("unreasonable delay period (%d)", n);
383 tdelay.tv_sec = n / 1000;
384 tdelay.tv_usec = n * 1000 % 1000000;
385 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
386 }
387