1331722Seadler/* 21553Srgrimes * Copyright (c) 1983, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * (c) UNIX System Laboratories, Inc. 51553Srgrimes * All or some portions of this file are derived from material licensed 61553Srgrimes * to the University of California by American Telephone and Telegraph 71553Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81553Srgrimes * the permission of UNIX System Laboratories, Inc. 91553Srgrimes * 101553Srgrimes * Redistribution and use in source and binary forms, with or without 111553Srgrimes * modification, are permitted provided that the following conditions 121553Srgrimes * are met: 131553Srgrimes * 1. Redistributions of source code must retain the above copyright 141553Srgrimes * notice, this list of conditions and the following disclaimer. 151553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161553Srgrimes * notice, this list of conditions and the following disclaimer in the 171553Srgrimes * documentation and/or other materials provided with the distribution. 181553Srgrimes * 3. All advertising materials mentioning features or use of this software 191553Srgrimes * must display the following acknowledgement: 201553Srgrimes * This product includes software developed by the University of 211553Srgrimes * California, Berkeley and its contributors. 221553Srgrimes * 4. Neither the name of the University nor the names of its contributors 231553Srgrimes * may be used to endorse or promote products derived from this software 241553Srgrimes * without specific prior written permission. 251553Srgrimes * 261553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361553Srgrimes * SUCH DAMAGE. 371553Srgrimes */ 381553Srgrimes 39117541Sgad#if 0 40117592Sgad#ifndef lint 4115648Sjoergstatic char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95"; 42117592Sgad#endif /* not lint */ 43117541Sgad#endif 44117592Sgad 45117541Sgad#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 46117541Sgad__FBSDID("$FreeBSD: stable/11/usr.sbin/lpr/common_source/common.c 315659 2017-03-21 03:42:28Z pfg $"); 471553Srgrimes 481553Srgrimes#include <sys/param.h> 491553Srgrimes#include <sys/stat.h> 5015648Sjoerg#include <sys/time.h> 5168253Sgad#include <sys/types.h> 521553Srgrimes 53139464Sgad#include <ctype.h> 541553Srgrimes#include <dirent.h> 55241852Seadler#include <err.h> 5698152Sgad#include <errno.h> 5768253Sgad#include <fcntl.h> 5831492Swollman#include <stdio.h> 591553Srgrimes#include <stdlib.h> 601553Srgrimes#include <string.h> 6131492Swollman#include <unistd.h> 6231492Swollman 631553Srgrimes#include "lp.h" 6431492Swollman#include "lp.local.h" 651553Srgrimes#include "pathnames.h" 661553Srgrimes 671553Srgrimes/* 681553Srgrimes * Routines and data common to all the line printer functions. 691553Srgrimes */ 701553Srgrimeschar line[BUFSIZ]; 7178280Sgadconst char *progname; /* program name */ 721553Srgrimes 7378146Sgadstatic int compar(const void *_p1, const void *_p2); 741553Srgrimes 751553Srgrimes/* 76139464Sgad * isdigit() takes a parameter of 'int', but expect values in the range 77139464Sgad * of unsigned char. Define a wrapper which takes a value of type 'char', 78139464Sgad * whether signed or unsigned, and ensure it ends up in the right range. 79139464Sgad */ 80139464Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 81139464Sgad 82139464Sgad/* 83299357Sbapt * get_line reads a line from the control file cfp, removes tabs, converts 841553Srgrimes * new-line to null and leaves it in line. 851553Srgrimes * Returns 0 at EOF or the number of characters read. 861553Srgrimes */ 871553Srgrimesint 88299357Sbaptget_line(FILE *cfp) 891553Srgrimes{ 901553Srgrimes register int linel = 0; 911553Srgrimes register char *lp = line; 9239084Swollman register int c; 931553Srgrimes 9480172Sgad while ((c = getc(cfp)) != '\n' && (size_t)(linel+1) < sizeof(line)) { 951553Srgrimes if (c == EOF) 961553Srgrimes return(0); 971553Srgrimes if (c == '\t') { 981553Srgrimes do { 991553Srgrimes *lp++ = ' '; 1001553Srgrimes linel++; 10180172Sgad } while ((linel & 07) != 0 && (size_t)(linel+1) < 10280172Sgad sizeof(line)); 1031553Srgrimes continue; 1041553Srgrimes } 1051553Srgrimes *lp++ = c; 1061553Srgrimes linel++; 1071553Srgrimes } 1081553Srgrimes *lp++ = '\0'; 1091553Srgrimes return(linel); 1101553Srgrimes} 1111553Srgrimes 1121553Srgrimes/* 1131553Srgrimes * Scan the current directory and make a list of daemon files sorted by 1141553Srgrimes * creation time. 1151553Srgrimes * Return the number of entries and a pointer to the list. 1161553Srgrimes */ 1171553Srgrimesint 11878146Sgadgetq(const struct printer *pp, struct jobqueue *(*namelist[])) 1191553Srgrimes{ 1201553Srgrimes register struct dirent *d; 12168401Sgad register struct jobqueue *q, **queue; 12299842Sgad size_t arraysz, entrysz, nitems; 1231553Srgrimes struct stat stbuf; 1241553Srgrimes DIR *dirp; 12580172Sgad int statres; 1261553Srgrimes 127241852Seadler PRIV_START 12868740Sgad if ((dirp = opendir(pp->spool_dir)) == NULL) { 129241852Seadler PRIV_END 13068739Sgad return (-1); 13168740Sgad } 132235647Sgleb if (fstat(dirfd(dirp), &stbuf) < 0) 1331553Srgrimes goto errdone; 134241852Seadler PRIV_END 1351553Srgrimes 1361553Srgrimes /* 1371553Srgrimes * Estimate the array size by taking the size of the directory file 138238546Sjh * and dividing it by a multiple of the minimum size entry. 1391553Srgrimes */ 1401553Srgrimes arraysz = (stbuf.st_size / 24); 141238547Sjh if (arraysz < 16) 142238547Sjh arraysz = 16; 14368401Sgad queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *)); 1441553Srgrimes if (queue == NULL) 1451553Srgrimes goto errdone; 1461553Srgrimes 1471553Srgrimes nitems = 0; 1481553Srgrimes while ((d = readdir(dirp)) != NULL) { 1491553Srgrimes if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 1501553Srgrimes continue; /* daemon control files only */ 151241852Seadler PRIV_START 15275253Sgad statres = stat(d->d_name, &stbuf); 153241852Seadler PRIV_END 15475253Sgad if (statres < 0) 1551553Srgrimes continue; /* Doesn't exist */ 15699842Sgad entrysz = sizeof(struct jobqueue) - sizeof(q->job_cfname) + 15799842Sgad strlen(d->d_name) + 1; 15899842Sgad q = (struct jobqueue *)malloc(entrysz); 1591553Srgrimes if (q == NULL) 1601553Srgrimes goto errdone; 16199842Sgad q->job_matched = 0; 16299842Sgad q->job_processed = 0; 16368401Sgad q->job_time = stbuf.st_mtime; 16468401Sgad strcpy(q->job_cfname, d->d_name); 1651553Srgrimes /* 1661553Srgrimes * Check to make sure the array has space left and 1671553Srgrimes * realloc the maximum size. 1681553Srgrimes */ 1691553Srgrimes if (++nitems > arraysz) { 170315659Spfg queue = (struct jobqueue **)reallocarray((char *)queue, 171315659Spfg arraysz, 2 * sizeof(struct jobqueue *)); 172315659Spfg if (queue == NULL) { 173315659Spfg free(q); 174315659Spfg goto errdone; 175315659Spfg } 17615648Sjoerg arraysz *= 2; 1771553Srgrimes } 1781553Srgrimes queue[nitems-1] = q; 1791553Srgrimes } 1801553Srgrimes closedir(dirp); 1811553Srgrimes if (nitems) 18268401Sgad qsort(queue, nitems, sizeof(struct jobqueue *), compar); 1831553Srgrimes *namelist = queue; 1841553Srgrimes return(nitems); 1851553Srgrimes 1861553Srgrimeserrdone: 1871553Srgrimes closedir(dirp); 188241852Seadler PRIV_END 18968739Sgad return (-1); 1901553Srgrimes} 1911553Srgrimes 1921553Srgrimes/* 1931553Srgrimes * Compare modification times. 1941553Srgrimes */ 1951553Srgrimesstatic int 19678146Sgadcompar(const void *p1, const void *p2) 1971553Srgrimes{ 19868401Sgad const struct jobqueue *qe1, *qe2; 19968739Sgad 20095290Sgad qe1 = *(const struct jobqueue * const *)p1; 20195290Sgad qe2 = *(const struct jobqueue * const *)p2; 20259920Swollman 20368401Sgad if (qe1->job_time < qe2->job_time) 20459920Swollman return (-1); 20568401Sgad if (qe1->job_time > qe2->job_time) 20659920Swollman return (1); 20759920Swollman /* 20859920Swollman * At this point, the two files have the same last-modification time. 20959920Swollman * return a result based on filenames, so that 'cfA001some.host' will 21059920Swollman * come before 'cfA002some.host'. Since the jobid ('001') will wrap 21159920Swollman * around when it gets to '999', we also assume that '9xx' jobs are 21259920Swollman * older than '0xx' jobs. 21359920Swollman */ 21468401Sgad if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0')) 21559920Swollman return (-1); 21668401Sgad if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9')) 21759920Swollman return (1); 21868401Sgad return (strcmp(qe1->job_cfname, qe2->job_cfname)); 2191553Srgrimes} 2201553Srgrimes 221139464Sgad/* 222139464Sgad * A simple routine to determine the job number for a print job based on 223139464Sgad * the name of its control file. The algorithm used here may look odd, but 224139464Sgad * the main issue is that all parts of `lpd', `lpc', `lpq' & `lprm' must be 225139464Sgad * using the same algorithm, whatever that algorithm may be. If the caller 226139464Sgad * provides a non-null value for ''hostpp', then this returns a pointer to 227139464Sgad * the start of the hostname (or IP address?) as found in the filename. 228139464Sgad * 229139464Sgad * Algorithm: The standard `cf' file has the job number start in position 4, 230139464Sgad * but some implementations have that as an extra file-sequence letter, and 231139464Sgad * start the job number in position 5. The job number is usually three bytes, 232139464Sgad * but may be as many as five. Confusing matters still more, some Windows 233139464Sgad * print servers will append an IP address to the job number, instead of 234139464Sgad * the expected hostname. So, if the job number ends with a '.', then 235139464Sgad * assume the correct jobnum value is the first three digits. 236139464Sgad */ 237139464Sgadint 238139464Sgadcalc_jobnum(const char *cfname, const char **hostpp) 239139464Sgad{ 240139464Sgad int jnum; 241139464Sgad const char *cp, *numstr, *hoststr; 242139464Sgad 243139464Sgad numstr = cfname + 3; 244139464Sgad if (!isdigitch(*numstr)) 245139464Sgad numstr++; 246139464Sgad jnum = 0; 247139464Sgad for (cp = numstr; (cp < numstr + 5) && isdigitch(*cp); cp++) 248139464Sgad jnum = jnum * 10 + (*cp - '0'); 249139464Sgad hoststr = cp; 250139464Sgad 251139464Sgad /* 252139464Sgad * If the filename was built with an IP number instead of a hostname, 253139464Sgad * then recalculate using only the first three digits found. 254139464Sgad */ 255139464Sgad while(isdigitch(*cp)) 256139464Sgad cp++; 257139464Sgad if (*cp == '.') { 258139464Sgad jnum = 0; 259139464Sgad for (cp = numstr; (cp < numstr + 3) && isdigitch(*cp); cp++) 260139464Sgad jnum = jnum * 10 + (*cp - '0'); 261139464Sgad hoststr = cp; 262139464Sgad } 263139464Sgad if (hostpp != NULL) 264139464Sgad *hostpp = hoststr; 265139464Sgad return (jnum); 266139464Sgad} 267139464Sgad 26815648Sjoerg/* sleep n milliseconds */ 26915648Sjoergvoid 27078146Sgaddelay(int millisec) 27115648Sjoerg{ 27215648Sjoerg struct timeval tdelay; 27315648Sjoerg 27478146Sgad if (millisec <= 0 || millisec > 10000) 27531492Swollman fatal((struct printer *)0, /* fatal() knows how to deal */ 27678146Sgad "unreasonable delay period (%d)", millisec); 27778146Sgad tdelay.tv_sec = millisec / 1000; 27878146Sgad tdelay.tv_usec = millisec * 1000 % 1000000; 27915648Sjoerg (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay); 28015648Sjoerg} 28115648Sjoerg 28231492Swollmanchar * 28378146Sgadlock_file_name(const struct printer *pp, char *buf, size_t len) 28431492Swollman{ 28531492Swollman static char staticbuf[MAXPATHLEN]; 28631492Swollman 287297795Spfg if (buf == NULL) 28831492Swollman buf = staticbuf; 28931492Swollman if (len == 0) 29031492Swollman len = MAXPATHLEN; 29131492Swollman 29279740Sgad if (pp->lock_file[0] == '/') 29379740Sgad strlcpy(buf, pp->lock_file, len); 29479740Sgad else 29531492Swollman snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file); 29679740Sgad 29731492Swollman return buf; 29831492Swollman} 29931492Swollman 30031492Swollmanchar * 30178146Sgadstatus_file_name(const struct printer *pp, char *buf, size_t len) 30231492Swollman{ 30331492Swollman static char staticbuf[MAXPATHLEN]; 30431492Swollman 305297795Spfg if (buf == NULL) 30631492Swollman buf = staticbuf; 30731492Swollman if (len == 0) 30831492Swollman len = MAXPATHLEN; 30931492Swollman 31079740Sgad if (pp->status_file[0] == '/') 31179740Sgad strlcpy(buf, pp->status_file, len); 31279740Sgad else 31331492Swollman snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file); 31479740Sgad 31531492Swollman return buf; 31631492Swollman} 31731492Swollman 31898152Sgad/* 31998152Sgad * Routine to change operational state of a print queue. The operational 32098154Sgad * state is indicated by the access bits on the lock file for the queue. 32198152Sgad * At present, this is only called from various routines in lpc/cmds.c. 32298152Sgad * 32398152Sgad * XXX - Note that this works by changing access-bits on the 32498152Sgad * file, and you can only do that if you are the owner of 32598152Sgad * the file, or root. Thus, this won't really work for 32698152Sgad * userids in the "LPR_OPER" group, unless lpc is running 32798152Sgad * setuid to root (or maybe setuid to daemon). 32898152Sgad * Generally lpc is installed setgid to daemon, but does 32998152Sgad * not run setuid. 33098152Sgad */ 33198152Sgadint 33298152Sgadset_qstate(int action, const char *lfname) 33398152Sgad{ 33498152Sgad struct stat stbuf; 33598152Sgad mode_t chgbits, newbits, oldmask; 33698152Sgad const char *failmsg, *okmsg; 33799844Sgad static const char *nomsg = "no state msg"; 33898152Sgad int chres, errsav, fd, res, statres; 33998152Sgad 34098152Sgad /* 34198152Sgad * Find what the current access-bits are. 34298152Sgad */ 34398152Sgad memset(&stbuf, 0, sizeof(stbuf)); 344241852Seadler PRIV_START 34598152Sgad statres = stat(lfname, &stbuf); 34698152Sgad errsav = errno; 347241852Seadler PRIV_END 34898152Sgad if ((statres < 0) && (errsav != ENOENT)) { 34998152Sgad printf("\tcannot stat() lock file\n"); 35098152Sgad return (SQS_STATFAIL); 35198152Sgad /* NOTREACHED */ 35298152Sgad } 35398152Sgad 35498152Sgad /* 35598152Sgad * Determine which bit(s) should change for the requested action. 35698152Sgad */ 35798152Sgad chgbits = stbuf.st_mode; 35898152Sgad newbits = LOCK_FILE_MODE; 35998152Sgad okmsg = NULL; 36098152Sgad failmsg = NULL; 36199844Sgad if (action & SQS_QCHANGED) { 36299844Sgad chgbits |= LFM_RESET_QUE; 36399844Sgad newbits |= LFM_RESET_QUE; 36499844Sgad /* The okmsg is not actually printed for this case. */ 36599844Sgad okmsg = nomsg; 36699844Sgad failmsg = "set queue-changed"; 36799844Sgad } 36898152Sgad if (action & SQS_DISABLEQ) { 36998152Sgad chgbits |= LFM_QUEUE_DIS; 37098152Sgad newbits |= LFM_QUEUE_DIS; 37198152Sgad okmsg = "queuing disabled"; 37298152Sgad failmsg = "disable queuing"; 37398152Sgad } 37498152Sgad if (action & SQS_STOPP) { 37598152Sgad chgbits |= LFM_PRINT_DIS; 37698152Sgad newbits |= LFM_PRINT_DIS; 37798152Sgad okmsg = "printing disabled"; 37898152Sgad failmsg = "disable printing"; 37998152Sgad if (action & SQS_DISABLEQ) { 38098152Sgad okmsg = "printer and queuing disabled"; 38198152Sgad failmsg = "disable queuing and printing"; 38298152Sgad } 38398152Sgad } 38498152Sgad if (action & SQS_ENABLEQ) { 38598152Sgad chgbits &= ~LFM_QUEUE_DIS; 38698152Sgad newbits &= ~LFM_QUEUE_DIS; 38798152Sgad okmsg = "queuing enabled"; 38898152Sgad failmsg = "enable queuing"; 38998152Sgad } 39098152Sgad if (action & SQS_STARTP) { 39198152Sgad chgbits &= ~LFM_PRINT_DIS; 39298152Sgad newbits &= ~LFM_PRINT_DIS; 39398152Sgad okmsg = "printing enabled"; 39498152Sgad failmsg = "enable printing"; 39598152Sgad } 39698152Sgad if (okmsg == NULL) { 39798152Sgad /* This routine was called with an invalid action. */ 39898152Sgad printf("\t<error in set_qstate!>\n"); 39998152Sgad return (SQS_PARMERR); 40098152Sgad /* NOTREACHED */ 40198152Sgad } 40298152Sgad 40398152Sgad res = 0; 40498152Sgad if (statres >= 0) { 40598152Sgad /* The file already exists, so change the access. */ 406241852Seadler PRIV_START 40798152Sgad chres = chmod(lfname, chgbits); 40898152Sgad errsav = errno; 409241852Seadler PRIV_END 41098152Sgad res = SQS_CHGOK; 41199845Sgad if (chres < 0) 41298152Sgad res = SQS_CHGFAIL; 41398152Sgad } else if (newbits == LOCK_FILE_MODE) { 41498152Sgad /* 41598152Sgad * The file does not exist, but the state requested is 41698152Sgad * the same as the default state when no file exists. 41798152Sgad * Thus, there is no need to create the file. 41898152Sgad */ 41998152Sgad res = SQS_SKIPCREOK; 42098152Sgad } else { 42198152Sgad /* 42298152Sgad * The file did not exist, so create it with the 42398152Sgad * appropriate access bits for the requested action. 42498152Sgad * Push a new umask around that create, to make sure 42598152Sgad * all the read/write bits are set as desired. 42698152Sgad */ 42798152Sgad oldmask = umask(S_IWOTH); 428241852Seadler PRIV_START 42998152Sgad fd = open(lfname, O_WRONLY|O_CREAT, newbits); 43098152Sgad errsav = errno; 431241852Seadler PRIV_END 43298152Sgad umask(oldmask); 43398152Sgad res = SQS_CREFAIL; 43498152Sgad if (fd >= 0) { 43598152Sgad res = SQS_CREOK; 43698152Sgad close(fd); 43798152Sgad } 43898152Sgad } 43998152Sgad 44098152Sgad switch (res) { 44198152Sgad case SQS_CHGOK: 44298152Sgad case SQS_CREOK: 44398152Sgad case SQS_SKIPCREOK: 44499844Sgad if (okmsg != nomsg) 44599844Sgad printf("\t%s\n", okmsg); 44698152Sgad break; 44798152Sgad case SQS_CREFAIL: 44898152Sgad printf("\tcannot create lock file: %s\n", 44998152Sgad strerror(errsav)); 45098152Sgad break; 45198152Sgad default: 45298152Sgad printf("\tcannot %s: %s\n", failmsg, strerror(errsav)); 45398152Sgad break; 45498152Sgad } 45598152Sgad 45698152Sgad return (res); 45798152Sgad} 45898152Sgad 45968253Sgad/* routine to get a current timestamp, optionally in a standard-fmt string */ 46068253Sgadvoid 46179740Sgadlpd_gettime(struct timespec *tsp, char *strp, size_t strsize) 46268253Sgad{ 46368253Sgad struct timespec local_ts; 46468253Sgad struct timeval btime; 46580172Sgad char tempstr[TIMESTR_SIZE]; 46680172Sgad#ifdef STRFTIME_WRONG_z 46768739Sgad char *destp; 46880172Sgad#endif 46980172Sgad 47068253Sgad if (tsp == NULL) 47168253Sgad tsp = &local_ts; 47268253Sgad 47368253Sgad /* some platforms have a routine called clock_gettime, but the 47468253Sgad * routine does nothing but return "not implemented". */ 47568253Sgad memset(tsp, 0, sizeof(struct timespec)); 47668253Sgad if (clock_gettime(CLOCK_REALTIME, tsp)) { 47768253Sgad /* nanosec-aware rtn failed, fall back to microsec-aware rtn */ 47868253Sgad memset(tsp, 0, sizeof(struct timespec)); 47968253Sgad gettimeofday(&btime, NULL); 48068253Sgad tsp->tv_sec = btime.tv_sec; 48168253Sgad tsp->tv_nsec = btime.tv_usec * 1000; 48268253Sgad } 48368253Sgad 48468253Sgad /* caller may not need a character-ized version */ 48568253Sgad if ((strp == NULL) || (strsize < 1)) 48668253Sgad return; 48768253Sgad 48868253Sgad strftime(tempstr, TIMESTR_SIZE, LPD_TIMESTAMP_PATTERN, 48968253Sgad localtime(&tsp->tv_sec)); 49068253Sgad 49168253Sgad /* 49268253Sgad * This check is for implementations of strftime which treat %z 49368253Sgad * (timezone as [+-]hhmm ) like %Z (timezone as characters), or 49468253Sgad * completely ignore %z. This section is not needed on freebsd. 49568253Sgad * I'm not sure this is completely right, but it should work OK 49668253Sgad * for EST and EDT... 49768253Sgad */ 49868253Sgad#ifdef STRFTIME_WRONG_z 49968253Sgad destp = strrchr(tempstr, ':'); 50068253Sgad if (destp != NULL) { 50168253Sgad destp += 3; 50268253Sgad if ((*destp != '+') && (*destp != '-')) { 50368253Sgad char savday[6]; 50468253Sgad int tzmin = timezone / 60; 50568253Sgad int tzhr = tzmin / 60; 50668253Sgad if (daylight) 50768253Sgad tzhr--; 50868253Sgad strcpy(savday, destp + strlen(destp) - 4); 50968253Sgad snprintf(destp, (destp - tempstr), "%+03d%02d", 51068739Sgad (-1*tzhr), tzmin % 60); 51168253Sgad strcat(destp, savday); 51268253Sgad } 51368253Sgad } 51468253Sgad#endif 51568253Sgad 51668253Sgad if (strsize > TIMESTR_SIZE) { 51768253Sgad strsize = TIMESTR_SIZE; 51868253Sgad strp[TIMESTR_SIZE+1] = '\0'; 51968253Sgad } 52079740Sgad strlcpy(strp, tempstr, strsize); 52168253Sgad} 52268253Sgad 52368253Sgad/* routines for writing transfer-statistic records */ 52468253Sgadvoid 52578146Sgadtrstat_init(struct printer *pp, const char *fname, int filenum) 52668253Sgad{ 52768253Sgad register const char *srcp; 52868253Sgad register char *destp, *endp; 52968253Sgad 53068253Sgad /* 53168253Sgad * Figure out the job id of this file. The filename should be 53268253Sgad * 'cf', 'df', or maybe 'tf', followed by a letter (or sometimes 53368253Sgad * two), followed by the jobnum, followed by a hostname. 53468253Sgad * The jobnum is usually 3 digits, but might be as many as 5. 53568253Sgad * Note that some care has to be taken parsing this, as the 53668253Sgad * filename could be coming from a remote-host, and thus might 53768253Sgad * not look anything like what is expected... 53868253Sgad */ 53968253Sgad memset(pp->jobnum, 0, sizeof(pp->jobnum)); 54068253Sgad pp->jobnum[0] = '0'; 54168253Sgad srcp = strchr(fname, '/'); 54268253Sgad if (srcp == NULL) 54368253Sgad srcp = fname; 54468253Sgad destp = &(pp->jobnum[0]); 54568253Sgad endp = destp + 5; 54668253Sgad while (*srcp != '\0' && (*srcp < '0' || *srcp > '9')) 54768253Sgad srcp++; 54868253Sgad while (*srcp >= '0' && *srcp <= '9' && destp < endp) 54968253Sgad *(destp++) = *(srcp++); 55068253Sgad 55168253Sgad /* get the starting time in both numeric and string formats, and 55268253Sgad * save those away along with the file-number */ 55368253Sgad pp->jobdfnum = filenum; 55479740Sgad lpd_gettime(&pp->tr_start, pp->tr_timestr, (size_t)TIMESTR_SIZE); 55568253Sgad 55668253Sgad return; 55768253Sgad} 55868253Sgad 55968253Sgadvoid 56078146Sgadtrstat_write(struct printer *pp, tr_sendrecv sendrecv, size_t bytecnt, 56178146Sgad const char *userid, const char *otherhost, const char *orighost) 56268253Sgad{ 56368253Sgad#define STATLINE_SIZE 1024 56468739Sgad double trtime; 56580172Sgad size_t remspace; 56680172Sgad int statfile; 56768739Sgad char thishost[MAXHOSTNAMELEN], statline[STATLINE_SIZE]; 56868739Sgad char *eostat; 56968739Sgad const char *lprhost, *recvdev, *recvhost, *rectype; 57068739Sgad const char *sendhost, *statfname; 57168253Sgad#define UPD_EOSTAT(xStr) do { \ 57268253Sgad eostat = strchr(xStr, '\0'); \ 57368253Sgad remspace = eostat - xStr; \ 57468253Sgad} while(0) 57580172Sgad 57679740Sgad lpd_gettime(&pp->tr_done, NULL, (size_t)0); 57768253Sgad trtime = DIFFTIME_TS(pp->tr_done, pp->tr_start); 57868253Sgad 57968253Sgad gethostname(thishost, sizeof(thishost)); 58068253Sgad lprhost = sendhost = recvhost = recvdev = NULL; 58168253Sgad switch (sendrecv) { 58268253Sgad case TR_SENDING: 58368253Sgad rectype = "send"; 58468253Sgad statfname = pp->stat_send; 58568253Sgad sendhost = thishost; 58668253Sgad recvhost = otherhost; 58768253Sgad break; 58868253Sgad case TR_RECVING: 58968253Sgad rectype = "recv"; 59068253Sgad statfname = pp->stat_recv; 59168253Sgad sendhost = otherhost; 59268253Sgad recvhost = thishost; 59368253Sgad break; 59468253Sgad case TR_PRINTING: 59568739Sgad /* 59668739Sgad * This case is for copying to a device (presumably local, 59768739Sgad * though filters using things like 'net/CAP' can confuse 59868739Sgad * this assumption...). 59968739Sgad */ 60068253Sgad rectype = "prnt"; 60168253Sgad statfname = pp->stat_send; 60268253Sgad sendhost = thishost; 60368253Sgad recvdev = _PATH_DEFDEVLP; 60468253Sgad if (pp->lp) recvdev = pp->lp; 60568253Sgad break; 60668253Sgad default: 60768253Sgad /* internal error... should we syslog/printf an error? */ 60868253Sgad return; 60968253Sgad } 61068739Sgad if (statfname == NULL) 61168739Sgad return; 61268253Sgad 61368253Sgad /* 61468253Sgad * the original-host and userid are found out by reading thru the 61568253Sgad * cf (control-file) for the job. Unfortunately, on incoming jobs 61668253Sgad * the df's (data-files) are sent before the matching cf, so the 61768253Sgad * orighost & userid are generally not-available for incoming jobs. 61868253Sgad * 61968253Sgad * (it would be nice to create a work-around for that..) 62068739Sgad */ 62168253Sgad if (orighost && (*orighost != '\0')) 62268253Sgad lprhost = orighost; 62368253Sgad else 62468253Sgad lprhost = ".na."; 62568739Sgad if (*userid == '\0') 62668739Sgad userid = NULL; 62768253Sgad 62868253Sgad /* 62968253Sgad * Format of statline. 63068253Sgad * Some of the keywords listed here are not implemented here, but 63168253Sgad * they are listed to reserve the meaning for a given keyword. 63268253Sgad * Fields are separated by a blank. The fields in statline are: 63368253Sgad * <tstamp> - time the transfer started 63468253Sgad * <ptrqueue> - name of the printer queue (the short-name...) 63568253Sgad * <hname> - hostname the file originally came from (the 63668253Sgad * 'lpr host'), if known, or "_na_" if not known. 63768253Sgad * <xxx> - id of job from that host (generally three digits) 63868253Sgad * <n> - file count (# of file within job) 63968253Sgad * <rectype> - 4-byte field indicating the type of transfer 64068253Sgad * statistics record. "send" means it's from the 64168253Sgad * host sending a datafile, "recv" means it's from 64268253Sgad * a host as it receives a datafile. 64368253Sgad * user=<userid> - user who sent the job (if known) 64468253Sgad * secs=<n> - seconds it took to transfer the file 645298886Spfg * bytes=<n> - number of bytes transferred (ie, "bytecount") 64668253Sgad * bps=<n.n>e<n> - Bytes/sec (if the transfer was "big enough" 647238546Sjh * for this to be useful) 64868253Sgad * ! top=<str> - type of printer (if the type is defined in 64968253Sgad * printcap, and if this statline is for sending 65068253Sgad * a file to that ptr) 65168253Sgad * ! qls=<n> - queue-length at start of send/print-ing a job 65268253Sgad * ! qle=<n> - queue-length at end of send/print-ing a job 65368253Sgad * sip=<addr> - IP address of sending host, only included when 65468253Sgad * receiving a job. 65568253Sgad * shost=<hname> - sending host (if that does != the original host) 65668253Sgad * rhost=<hname> - hostname receiving the file (ie, "destination") 65768253Sgad * rdev=<dev> - device receiving the file, when the file is being 65868253Sgad * send to a device instead of a remote host. 65968253Sgad * 66068253Sgad * Note: A single print job may be transferred multiple times. The 66168253Sgad * original 'lpr' occurs on one host, and that original host might 66268253Sgad * send to some interim host (or print server). That interim host 66368253Sgad * might turn around and send the job to yet another host (most likely 66468253Sgad * the real printer). The 'shost=' parameter is only included if the 66568253Sgad * sending host for this particular transfer is NOT the same as the 66668253Sgad * host which did the original 'lpr'. 66768253Sgad * 66868253Sgad * Many values have 'something=' tags before them, because they are 66968253Sgad * in some sense "optional", or their order may vary. "Optional" may 67068253Sgad * mean in the sense that different SITES might choose to have other 67168253Sgad * fields in the record, or that some fields are only included under 67268253Sgad * some circumstances. Programs processing these records should not 67368253Sgad * assume the order or existence of any of these keyword fields. 67468253Sgad */ 67568253Sgad snprintf(statline, STATLINE_SIZE, "%s %s %s %s %03ld %s", 67668739Sgad pp->tr_timestr, pp->printer, lprhost, pp->jobnum, 67768739Sgad pp->jobdfnum, rectype); 67868253Sgad UPD_EOSTAT(statline); 67968253Sgad 68068253Sgad if (userid != NULL) { 68168253Sgad snprintf(eostat, remspace, " user=%s", userid); 68268253Sgad UPD_EOSTAT(statline); 68368253Sgad } 68480172Sgad snprintf(eostat, remspace, " secs=%#.2f bytes=%lu", trtime, 68580172Sgad (unsigned long)bytecnt); 68668253Sgad UPD_EOSTAT(statline); 68780172Sgad 68868739Sgad /* 68968739Sgad * The bps field duplicates info from bytes and secs, so do 69068739Sgad * not bother to include it for very small files. 69168739Sgad */ 69268253Sgad if ((bytecnt > 25000) && (trtime > 1.1)) { 69368253Sgad snprintf(eostat, remspace, " bps=%#.2e", 69468739Sgad ((double)bytecnt/trtime)); 69568253Sgad UPD_EOSTAT(statline); 69668253Sgad } 69768253Sgad 69868253Sgad if (sendrecv == TR_RECVING) { 69968253Sgad if (remspace > 5+strlen(from_ip) ) { 70068253Sgad snprintf(eostat, remspace, " sip=%s", from_ip); 70168253Sgad UPD_EOSTAT(statline); 70268253Sgad } 70368253Sgad } 70468253Sgad if (0 != strcmp(lprhost, sendhost)) { 70568253Sgad if (remspace > 7+strlen(sendhost) ) { 70668253Sgad snprintf(eostat, remspace, " shost=%s", sendhost); 70768253Sgad UPD_EOSTAT(statline); 70868253Sgad } 70968253Sgad } 71068253Sgad if (recvhost) { 71168253Sgad if (remspace > 7+strlen(recvhost) ) { 71268253Sgad snprintf(eostat, remspace, " rhost=%s", recvhost); 71368253Sgad UPD_EOSTAT(statline); 71468253Sgad } 71568253Sgad } 71668253Sgad if (recvdev) { 71768253Sgad if (remspace > 6+strlen(recvdev) ) { 71868253Sgad snprintf(eostat, remspace, " rdev=%s", recvdev); 71968253Sgad UPD_EOSTAT(statline); 72068253Sgad } 72168253Sgad } 72268253Sgad if (remspace > 1) { 72368253Sgad strcpy(eostat, "\n"); 72468253Sgad } else { 725238546Sjh /* probably should back up to just before the final " x=".. */ 72668253Sgad strcpy(statline+STATLINE_SIZE-2, "\n"); 72768253Sgad } 72868253Sgad statfile = open(statfname, O_WRONLY|O_APPEND, 0664); 72968253Sgad if (statfile < 0) { 73068253Sgad /* statfile was given, but we can't open it. should we 73168253Sgad * syslog/printf this as an error? */ 73268253Sgad return; 73368253Sgad } 73468253Sgad write(statfile, statline, strlen(statline)); 73568253Sgad close(statfile); 73668253Sgad 73768253Sgad return; 738238546Sjh#undef UPD_EOSTAT 73968253Sgad} 74068253Sgad 7411553Srgrimes#include <stdarg.h> 7421553Srgrimes 7431553Srgrimesvoid 74431492Swollmanfatal(const struct printer *pp, const char *msg, ...) 7451553Srgrimes{ 7461553Srgrimes va_list ap; 7471553Srgrimes va_start(ap, msg); 74878300Sgad /* this error message is being sent to the 'from_host' */ 74978300Sgad if (from_host != local_host) 75078300Sgad (void)printf("%s: ", local_host); 75178280Sgad (void)printf("%s: ", progname); 75231492Swollman if (pp && pp->printer) 75331492Swollman (void)printf("%s: ", pp->printer); 7541553Srgrimes (void)vprintf(msg, ap); 7551553Srgrimes va_end(ap); 7561553Srgrimes (void)putchar('\n'); 7571553Srgrimes exit(1); 7581553Srgrimes} 75931492Swollman 76031492Swollman/* 76131492Swollman * Close all file descriptors from START on up. 76231492Swollman */ 76331492Swollmanvoid 76478146Sgadcloseallfds(int start) 76531492Swollman{ 766251044Sgad int stop; 767251044Sgad 768251044Sgad if (USE_CLOSEFROM) /* The faster, modern solution */ 769251044Sgad closefrom(start); 770251044Sgad else { 771251044Sgad /* This older logic can be pretty awful on some OS's. The 772251044Sgad * getdtablesize() might return ``infinity'', and then this 773251044Sgad * will waste a lot of time closing file descriptors which 774251044Sgad * had never been open()-ed. */ 775251044Sgad stop = getdtablesize(); 776251044Sgad for (; start < stop; start++) 777251044Sgad close(start); 778251044Sgad } 77931492Swollman} 78031492Swollman 781