1 /* $NetBSD: common.c,v 1.12 1997/10/18 08:52:17 lukem 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.12 1997/10/18 08:52:17 lukem 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 char *bp; /* pointer into printcap buffer. */ 116 char *name; /* program name */ 117 char *printer; /* printer name */ 118 /* host machine name */ 119 char host[MAXHOSTNAMELEN]; 120 char *from = host; /* client's machine name */ 121 int remote; /* true if sending files to a remote host */ 122 char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; 123 124 extern uid_t uid, euid; 125 126 static int compar __P((const void *, const void *)); 127 128 /* 129 * Create a TCP connection to host "rhost" at port "rport". 130 * If rport == 0, then use the printer service port. 131 * Most of this code comes from rcmd.c. 132 */ 133 int 134 getport(rhost, rport) 135 char *rhost; 136 int rport; 137 { 138 struct hostent *hp; 139 struct servent *sp; 140 struct sockaddr_in sin; 141 int s, timo = 1, lport = IPPORT_RESERVED - 1; 142 int err; 143 144 /* 145 * Get the host address and port number to connect to. 146 */ 147 if (rhost == NULL) 148 fatal("no remote host to connect to"); 149 memset((char *)&sin, 0, sizeof(sin)); 150 if (inet_aton(rhost, &sin.sin_addr) == 1) 151 sin.sin_family = AF_INET; 152 else { 153 hp = gethostbyname(rhost); 154 if (hp == NULL) 155 fatal("unknown host %s", rhost); 156 memmove((caddr_t)&sin.sin_addr, hp->h_addr, hp->h_length); 157 sin.sin_family = hp->h_addrtype; 158 } 159 if (rport == 0) { 160 sp = getservbyname("printer", "tcp"); 161 if (sp == NULL) 162 fatal("printer/tcp: unknown service"); 163 sin.sin_port = sp->s_port; 164 } else 165 sin.sin_port = htons(rport); 166 167 /* 168 * Try connecting to the server. 169 */ 170 retry: 171 seteuid(euid); 172 s = rresvport(&lport); 173 seteuid(uid); 174 if (s < 0) 175 return(-1); 176 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 177 err = errno; 178 (void)close(s); 179 errno = err; 180 if (errno == EADDRINUSE) { 181 lport--; 182 goto retry; 183 } 184 if (errno == ECONNREFUSED && timo <= 16) { 185 sleep(timo); 186 timo *= 2; 187 goto retry; 188 } 189 return(-1); 190 } 191 return(s); 192 } 193 194 /* 195 * Getline reads a line from the control file cfp, removes tabs, converts 196 * new-line to null and leaves it in line. 197 * Returns 0 at EOF or the number of characters read. 198 */ 199 int 200 getline(cfp) 201 FILE *cfp; 202 { 203 int linel = 0, c; 204 char *lp = line; 205 206 while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) { 207 if (c == EOF) 208 return(0); 209 if (c == '\t') { 210 do { 211 *lp++ = ' '; 212 linel++; 213 } while ((linel & 07) != 0 && linel+1 < sizeof(line)); 214 continue; 215 } 216 *lp++ = c; 217 linel++; 218 } 219 *lp++ = '\0'; 220 return(linel); 221 } 222 223 /* 224 * Scan the current directory and make a list of daemon files sorted by 225 * creation time. 226 * Return the number of entries and a pointer to the list. 227 */ 228 int 229 getq(namelist) 230 struct queue *(*namelist[]); 231 { 232 struct dirent *d; 233 struct queue *q, **queue; 234 struct stat stbuf; 235 DIR *dirp; 236 int nitems, arraysz; 237 238 seteuid(euid); 239 if ((dirp = opendir(SD)) == NULL) 240 return(-1); 241 if (fstat(dirp->dd_fd, &stbuf) < 0) 242 goto errdone; 243 seteuid(uid); 244 245 /* 246 * Estimate the array size by taking the size of the directory file 247 * and dividing it by a multiple of the minimum size entry. 248 */ 249 arraysz = (stbuf.st_size / 24); 250 queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 251 if (queue == NULL) 252 goto errdone; 253 254 nitems = 0; 255 while ((d = readdir(dirp)) != NULL) { 256 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 257 continue; /* daemon control files only */ 258 seteuid(euid); 259 if (stat(d->d_name, &stbuf) < 0) 260 continue; /* Doesn't exist */ 261 seteuid(uid); 262 q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 263 if (q == NULL) 264 goto errdone; 265 q->q_time = stbuf.st_mtime; 266 strcpy(q->q_name, d->d_name); /* XXX: strcpy is safe */ 267 /* 268 * Check to make sure the array has space left and 269 * realloc the maximum size. 270 */ 271 if (++nitems > arraysz) { 272 arraysz *= 2; 273 queue = (struct queue **)realloc((char *)queue, 274 arraysz * sizeof(struct queue *)); 275 if (queue == NULL) 276 goto errdone; 277 } 278 queue[nitems-1] = q; 279 } 280 closedir(dirp); 281 if (nitems) 282 qsort(queue, nitems, sizeof(struct queue *), compar); 283 *namelist = queue; 284 return(nitems); 285 286 errdone: 287 closedir(dirp); 288 return(-1); 289 } 290 291 /* 292 * Compare modification times. 293 */ 294 static int 295 compar(p1, p2) 296 const void *p1, *p2; 297 { 298 if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time) 299 return(-1); 300 if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time) 301 return(1); 302 return(0); 303 } 304 305 /* 306 * Figure out whether the local machine is the same 307 * as the remote machine (RM) entry (if it exists). 308 */ 309 char * 310 checkremote() 311 { 312 char name[MAXHOSTNAMELEN]; 313 struct hostent *hp; 314 static char errbuf[128]; 315 316 remote = 0; /* assume printer is local */ 317 if (RM != NULL) { 318 /* get the official name of the local host */ 319 gethostname(name, sizeof(name)); 320 name[sizeof(name)-1] = '\0'; 321 hp = gethostbyname(name); 322 if (hp == (struct hostent *) NULL) { 323 (void)snprintf(errbuf, sizeof(errbuf), 324 "unable to get official name for local machine %s", 325 name); 326 return errbuf; 327 } else { 328 (void)strncpy(name, hp->h_name, sizeof(name) - 1); 329 name[sizeof(name) - 1] = '\0'; 330 } 331 332 /* get the official name of RM */ 333 hp = gethostbyname(RM); 334 if (hp == (struct hostent *) NULL) { 335 (void)snprintf(errbuf, sizeof(errbuf), 336 "unable to get official name for remote machine %s", 337 RM); 338 return errbuf; 339 } 340 341 /* 342 * if the two hosts are not the same, 343 * then the printer must be remote. 344 */ 345 if (strcasecmp(name, hp->h_name) != 0) 346 remote = 1; 347 } 348 return NULL; 349 } 350 351 /* sleep n milliseconds */ 352 void 353 delay(n) 354 { 355 struct timeval tdelay; 356 357 if (n <= 0 || n > 10000) 358 fatal("unreasonable delay period (%d)", n); 359 tdelay.tv_sec = n / 1000; 360 tdelay.tv_usec = n * 1000 % 1000000; 361 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay); 362 } 363 364 #ifdef __STDC__ 365 #include <stdarg.h> 366 #else 367 #include <varargs.h> 368 #endif 369 370 void 371 #ifdef __STDC__ 372 fatal(const char *msg, ...) 373 #else 374 fatal(msg, va_alist) 375 char *msg; 376 va_dcl 377 #endif 378 { 379 va_list ap; 380 #ifdef __STDC__ 381 va_start(ap, msg); 382 #else 383 va_start(ap); 384 #endif 385 if (from != host) 386 (void)printf("%s: ", host); 387 (void)printf("%s: ", name); 388 if (printer) 389 (void)printf("%s: ", printer); 390 (void)vprintf(msg, ap); 391 va_end(ap); 392 (void)putchar('\n'); 393 exit(1); 394 } 395