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