#include "lp.cdefs.h"
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lp.h"
#include "lp.local.h"
#include "pathnames.h"
char line[BUFSIZ];
const char *progname;
static int compar(const void *_p1, const void *_p2);
#define isdigitch(Anychar) isdigit((u_char)(Anychar))
int
get_line(FILE *cfp)
{
register int linel = 0;
register char *lp = line;
register int c;
while ((c = getc(cfp)) != '\n' && (size_t)(linel+1) < sizeof(line)) {
if (c == EOF)
return(0);
if (c == '\t') {
do {
*lp++ = ' ';
linel++;
} while ((linel & 07) != 0 && (size_t)(linel+1) <
sizeof(line));
continue;
}
*lp++ = c;
linel++;
}
*lp++ = '\0';
return(linel);
}
int
getq(const struct printer *pp, struct jobqueue *(*namelist[]))
{
register struct dirent *d;
register struct jobqueue *q, **queue;
size_t arraysz, entrysz, nitems;
struct stat stbuf;
DIR *dirp;
int statres;
PRIV_START
if ((dirp = opendir(pp->spool_dir)) == NULL) {
PRIV_END
return (-1);
}
if (fstat(dirfd(dirp), &stbuf) < 0)
goto errdone;
PRIV_END
arraysz = (stbuf.st_size / 24);
if (arraysz < 16)
arraysz = 16;
queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *));
if (queue == NULL)
goto errdone;
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
continue;
PRIV_START
statres = stat(d->d_name, &stbuf);
PRIV_END
if (statres < 0)
continue;
entrysz = sizeof(struct jobqueue) - sizeof(q->job_cfname) +
strlen(d->d_name) + 1;
q = (struct jobqueue *)malloc(entrysz);
if (q == NULL)
goto errdone;
q->job_matched = 0;
q->job_processed = 0;
q->job_time = stbuf.st_mtime;
strcpy(q->job_cfname, d->d_name);
if (++nitems > arraysz) {
queue = (struct jobqueue **)reallocarray((char *)queue,
arraysz, 2 * sizeof(struct jobqueue *));
if (queue == NULL) {
free(q);
goto errdone;
}
arraysz *= 2;
}
queue[nitems-1] = q;
}
closedir(dirp);
if (nitems)
qsort(queue, nitems, sizeof(struct jobqueue *), compar);
*namelist = queue;
return(nitems);
errdone:
closedir(dirp);
PRIV_END
return (-1);
}
static int
compar(const void *p1, const void *p2)
{
const struct jobqueue *qe1, *qe2;
qe1 = *(const struct jobqueue * const *)p1;
qe2 = *(const struct jobqueue * const *)p2;
if (qe1->job_time < qe2->job_time)
return (-1);
if (qe1->job_time > qe2->job_time)
return (1);
if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0'))
return (-1);
if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9'))
return (1);
return (strcmp(qe1->job_cfname, qe2->job_cfname));
}
int
calc_jobnum(const char *cfname, const char **hostpp)
{
int jnum;
const char *cp, *numstr, *hoststr;
numstr = cfname + 3;
if (!isdigitch(*numstr))
numstr++;
jnum = 0;
for (cp = numstr; (cp < numstr + 5) && isdigitch(*cp); cp++)
jnum = jnum * 10 + (*cp - '0');
hoststr = cp;
while(isdigitch(*cp))
cp++;
if (*cp == '.') {
jnum = 0;
for (cp = numstr; (cp < numstr + 3) && isdigitch(*cp); cp++)
jnum = jnum * 10 + (*cp - '0');
hoststr = cp;
}
if (hostpp != NULL)
*hostpp = hoststr;
return (jnum);
}
void
delay(int millisec)
{
struct timeval tdelay;
if (millisec <= 0 || millisec > 10000)
fatal((struct printer *)0,
"unreasonable delay period (%d)", millisec);
tdelay.tv_sec = millisec / 1000;
tdelay.tv_usec = millisec * 1000 % 1000000;
(void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
}
char *
lock_file_name(const struct printer *pp, char *buf, size_t len)
{
static char staticbuf[MAXPATHLEN];
if (buf == NULL)
buf = staticbuf;
if (len == 0)
len = MAXPATHLEN;
if (pp->lock_file[0] == '/')
strlcpy(buf, pp->lock_file, len);
else
snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file);
return buf;
}
char *
status_file_name(const struct printer *pp, char *buf, size_t len)
{
static char staticbuf[MAXPATHLEN];
if (buf == NULL)
buf = staticbuf;
if (len == 0)
len = MAXPATHLEN;
if (pp->status_file[0] == '/')
strlcpy(buf, pp->status_file, len);
else
snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
return buf;
}
int
set_qstate(int action, const char *lfname)
{
struct stat stbuf;
mode_t chgbits, newbits, oldmask;
const char *failmsg, *okmsg;
static const char *nomsg = "no state msg";
int chres, errsav, fd, res, statres;
memset(&stbuf, 0, sizeof(stbuf));
PRIV_START
statres = stat(lfname, &stbuf);
errsav = errno;
PRIV_END
if ((statres < 0) && (errsav != ENOENT)) {
printf("\tcannot stat() lock file\n");
return (SQS_STATFAIL);
}
chgbits = stbuf.st_mode;
newbits = LOCK_FILE_MODE;
okmsg = NULL;
failmsg = NULL;
if (action & SQS_QCHANGED) {
chgbits |= LFM_RESET_QUE;
newbits |= LFM_RESET_QUE;
okmsg = nomsg;
failmsg = "set queue-changed";
}
if (action & SQS_DISABLEQ) {
chgbits |= LFM_QUEUE_DIS;
newbits |= LFM_QUEUE_DIS;
okmsg = "queuing disabled";
failmsg = "disable queuing";
}
if (action & SQS_STOPP) {
chgbits |= LFM_PRINT_DIS;
newbits |= LFM_PRINT_DIS;
okmsg = "printing disabled";
failmsg = "disable printing";
if (action & SQS_DISABLEQ) {
okmsg = "printer and queuing disabled";
failmsg = "disable queuing and printing";
}
}
if (action & SQS_ENABLEQ) {
chgbits &= ~LFM_QUEUE_DIS;
newbits &= ~LFM_QUEUE_DIS;
okmsg = "queuing enabled";
failmsg = "enable queuing";
}
if (action & SQS_STARTP) {
chgbits &= ~LFM_PRINT_DIS;
newbits &= ~LFM_PRINT_DIS;
okmsg = "printing enabled";
failmsg = "enable printing";
}
if (okmsg == NULL) {
printf("\t<error in set_qstate!>\n");
return (SQS_PARMERR);
}
res = 0;
if (statres >= 0) {
PRIV_START
chres = chmod(lfname, chgbits);
errsav = errno;
PRIV_END
res = SQS_CHGOK;
if (chres < 0)
res = SQS_CHGFAIL;
} else if (newbits == LOCK_FILE_MODE) {
res = SQS_SKIPCREOK;
} else {
oldmask = umask(S_IWOTH);
PRIV_START
fd = open(lfname, O_WRONLY|O_CREAT, newbits);
errsav = errno;
PRIV_END
umask(oldmask);
res = SQS_CREFAIL;
if (fd >= 0) {
res = SQS_CREOK;
close(fd);
}
}
switch (res) {
case SQS_CHGOK:
case SQS_CREOK:
case SQS_SKIPCREOK:
if (okmsg != nomsg)
printf("\t%s\n", okmsg);
break;
case SQS_CREFAIL:
printf("\tcannot create lock file: %s\n",
strerror(errsav));
break;
default:
printf("\tcannot %s: %s\n", failmsg, strerror(errsav));
break;
}
return (res);
}
void
lpd_gettime(struct timespec *tsp, char *strp, size_t strsize)
{
struct timespec local_ts;
struct timeval btime;
char tempstr[TIMESTR_SIZE];
#ifdef STRFTIME_WRONG_z
char *destp;
#endif
if (tsp == NULL)
tsp = &local_ts;
memset(tsp, 0, sizeof(struct timespec));
if (clock_gettime(CLOCK_REALTIME, tsp)) {
memset(tsp, 0, sizeof(struct timespec));
gettimeofday(&btime, NULL);
tsp->tv_sec = btime.tv_sec;
tsp->tv_nsec = btime.tv_usec * 1000;
}
if ((strp == NULL) || (strsize < 1))
return;
strftime(tempstr, TIMESTR_SIZE, LPD_TIMESTAMP_PATTERN,
localtime(&tsp->tv_sec));
#ifdef STRFTIME_WRONG_z
destp = strrchr(tempstr, ':');
if (destp != NULL) {
destp += 3;
if ((*destp != '+') && (*destp != '-')) {
char savday[6];
int tzmin = timezone / 60;
int tzhr = tzmin / 60;
if (daylight)
tzhr--;
strcpy(savday, destp + strlen(destp) - 4);
snprintf(destp, (destp - tempstr), "%+03d%02d",
(-1*tzhr), tzmin % 60);
strcat(destp, savday);
}
}
#endif
if (strsize > TIMESTR_SIZE) {
strsize = TIMESTR_SIZE;
strp[TIMESTR_SIZE+1] = '\0';
}
strlcpy(strp, tempstr, strsize);
}
void
trstat_init(struct printer *pp, const char *fname, int filenum)
{
register const char *srcp;
register char *destp, *endp;
memset(pp->jobnum, 0, sizeof(pp->jobnum));
pp->jobnum[0] = '0';
srcp = strchr(fname, '/');
if (srcp == NULL)
srcp = fname;
destp = &(pp->jobnum[0]);
endp = destp + 5;
while (*srcp != '\0' && (*srcp < '0' || *srcp > '9'))
srcp++;
while (*srcp >= '0' && *srcp <= '9' && destp < endp)
*(destp++) = *(srcp++);
pp->jobdfnum = filenum;
lpd_gettime(&pp->tr_start, pp->tr_timestr, (size_t)TIMESTR_SIZE);
}
void
trstat_write(struct printer *pp, tr_sendrecv sendrecv, size_t bytecnt,
const char *userid, const char *otherhost, const char *orighost)
{
#define STATLINE_SIZE 1024
double trtime;
size_t remspace;
int statfile;
char thishost[MAXHOSTNAMELEN], statline[STATLINE_SIZE];
char *eostat;
const char *lprhost, *recvdev, *recvhost, *rectype;
const char *sendhost, *statfname;
#define UPD_EOSTAT(xStr) do { \
eostat = strchr(xStr, '\0'); \
remspace = eostat - xStr; \
} while(0)
lpd_gettime(&pp->tr_done, NULL, (size_t)0);
trtime = DIFFTIME_TS(pp->tr_done, pp->tr_start);
gethostname(thishost, sizeof(thishost));
lprhost = sendhost = recvhost = recvdev = NULL;
switch (sendrecv) {
case TR_SENDING:
rectype = "send";
statfname = pp->stat_send;
sendhost = thishost;
recvhost = otherhost;
break;
case TR_RECVING:
rectype = "recv";
statfname = pp->stat_recv;
sendhost = otherhost;
recvhost = thishost;
break;
case TR_PRINTING:
rectype = "prnt";
statfname = pp->stat_send;
sendhost = thishost;
recvdev = _PATH_DEFDEVLP;
if (pp->lp) recvdev = pp->lp;
break;
default:
return;
}
if (statfname == NULL)
return;
if (orighost && (*orighost != '\0'))
lprhost = orighost;
else
lprhost = ".na.";
if (*userid == '\0')
userid = NULL;
snprintf(statline, STATLINE_SIZE, "%s %s %s %s %03ld %s",
pp->tr_timestr, pp->printer, lprhost, pp->jobnum,
pp->jobdfnum, rectype);
UPD_EOSTAT(statline);
if (userid != NULL) {
snprintf(eostat, remspace, " user=%s", userid);
UPD_EOSTAT(statline);
}
snprintf(eostat, remspace, " secs=%#.2f bytes=%lu", trtime,
(unsigned long)bytecnt);
UPD_EOSTAT(statline);
if ((bytecnt > 25000) && (trtime > 1.1)) {
snprintf(eostat, remspace, " bps=%#.2e",
((double)bytecnt/trtime));
UPD_EOSTAT(statline);
}
if (sendrecv == TR_RECVING) {
if (remspace > 5+strlen(from_ip) ) {
snprintf(eostat, remspace, " sip=%s", from_ip);
UPD_EOSTAT(statline);
}
}
if (0 != strcmp(lprhost, sendhost)) {
if (remspace > 7+strlen(sendhost) ) {
snprintf(eostat, remspace, " shost=%s", sendhost);
UPD_EOSTAT(statline);
}
}
if (recvhost) {
if (remspace > 7+strlen(recvhost) ) {
snprintf(eostat, remspace, " rhost=%s", recvhost);
UPD_EOSTAT(statline);
}
}
if (recvdev) {
if (remspace > 6+strlen(recvdev) ) {
snprintf(eostat, remspace, " rdev=%s", recvdev);
UPD_EOSTAT(statline);
}
}
if (remspace > 1) {
strcpy(eostat, "\n");
} else {
strcpy(statline+STATLINE_SIZE-2, "\n");
}
statfile = open(statfname, O_WRONLY|O_APPEND, 0664);
if (statfile < 0) {
return;
}
write(statfile, statline, strlen(statline));
close(statfile);
return;
#undef UPD_EOSTAT
}
#include <stdarg.h>
void
fatal(const struct printer *pp, const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
if (from_host != local_host)
(void)printf("%s: ", local_host);
(void)printf("%s: ", progname);
if (pp && pp->printer)
(void)printf("%s: ", pp->printer);
(void)vprintf(msg, ap);
va_end(ap);
(void)putchar('\n');
exit(1);
}
void
closeallfds(int start)
{
int stop;
if (USE_CLOSEFROM)
closefrom(start);
else {
stop = getdtablesize();
for (; start < stop; start++)
close(start);
}
}