#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <ifaddrs.h>
#include "lp.h"
#include "pathnames.h"
char *AF;
long BR;
char *CF;
char *DF;
long DU;
char *FF;
char *GF;
long HL;
char *IF;
char *LF;
char *LO;
char *LP;
long MC;
char *MS;
long MX;
char *NF;
char *OF;
long PL;
long PW;
long PX;
long PY;
char *RF;
char *RG;
char *RM;
char *RP;
long RS;
long RW;
long SB;
long SC;
char *SD;
long SF;
long SH;
char *ST;
char *TF;
char *TR;
char *VF;
char line[BUFSIZ];
int remote;
static int compar(const void *, const void *);
int
getport(char *rhost, int rport)
{
struct addrinfo hints, *res, *r;
u_int timo = 1;
int s, lport = IPPORT_RESERVED - 1;
int error;
int refuse, trial;
char pbuf[NI_MAXSERV];
if (rhost == NULL)
fatal("no remote host to connect to");
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (rport)
snprintf(pbuf, sizeof(pbuf), "%d", rport);
else
snprintf(pbuf, sizeof(pbuf), "printer");
siginterrupt(SIGINT, 1);
error = getaddrinfo(rhost, pbuf, &hints, &res);
siginterrupt(SIGINT, 0);
if (error)
fatal("printer/tcp: %s", gai_strerror(error));
retry:
s = -1;
refuse = trial = 0;
for (r = res; r; r = r->ai_next) {
trial++;
retryport:
PRIV_START;
s = rresvport_af(&lport, r->ai_family);
PRIV_END;
if (s < 0) {
if (errno != EACCES ||
(s = socket(r->ai_family, SOCK_STREAM, 0)) < 0) {
freeaddrinfo(res);
return(-1);
}
}
siginterrupt(SIGINT, 1);
if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {
error = errno;
siginterrupt(SIGINT, 0);
(void)close(s);
s = -1;
errno = error;
if (errno == EADDRINUSE) {
lport--;
goto retryport;
} else if (errno == ECONNREFUSED)
refuse++;
continue;
} else {
siginterrupt(SIGINT, 0);
break;
}
}
if (s < 0 && trial == refuse && timo <= 16) {
sleep(timo);
timo *= 2;
goto retry;
}
if (res)
freeaddrinfo(res);
trial = 1;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &trial, sizeof(trial));
return(s);
}
int
get_line(FILE *cfp)
{
int linel = 0;
char *lp = line;
int c;
while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) {
if (c == EOF)
return(0);
if (c == '\t') {
do {
*lp++ = ' ';
linel++;
} while ((linel & 07) != 0 && linel+1 < sizeof(line));
continue;
}
*lp++ = c;
linel++;
}
*lp++ = '\0';
return(linel);
}
int
getq(struct queue ***namelist)
{
struct dirent *d;
struct queue *q, **queue = NULL;
size_t nitems = 0, arraysz;
struct stat stbuf;
DIR *dirp;
PRIV_START;
dirp = opendir(SD);
PRIV_END;
if (dirp == NULL)
return(-1);
if (fstat(dirfd(dirp), &stbuf) < 0)
goto errdone;
arraysz = (stbuf.st_size / 24);
queue = calloc(arraysz, sizeof(struct queue *));
if (queue == NULL)
goto errdone;
while ((d = readdir(dirp)) != NULL) {
if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
continue;
PRIV_START;
if (stat(d->d_name, &stbuf) < 0) {
PRIV_END;
continue;
}
PRIV_END;
q = malloc(sizeof(struct queue));
if (q == NULL)
goto errdone;
q->q_time = stbuf.st_mtime;
strlcpy(q->q_name, d->d_name, sizeof(q->q_name));
if (nitems == arraysz) {
struct queue **newqueue;
newqueue = reallocarray(queue,
arraysz, 2 * sizeof(struct queue *));
if (newqueue == NULL) {
free(q);
goto errdone;
}
arraysz *= 2;
queue = newqueue;
}
queue[nitems++] = q;
}
closedir(dirp);
if (nitems)
qsort(queue, nitems, sizeof(struct queue *), compar);
*namelist = queue;
return(nitems);
errdone:
if (queue != NULL) {
while (nitems--)
free(queue[nitems]);
free(queue);
}
closedir(dirp);
return(-1);
}
static int
compar(const void *v1, const void *v2)
{
struct queue *p1 = *(struct queue **)v1;
struct queue *p2 = *(struct queue **)v2;
return(p1->q_time - p2->q_time);
}
char *
checkremote(void)
{
char lname[NI_MAXHOST], rname[NI_MAXHOST];
struct addrinfo hints, *res, *res0;
static char errbuf[128];
int error;
struct ifaddrs *ifap, *ifa;
const int niflags = NI_NUMERICHOST;
struct sockaddr *sa;
#ifdef __KAME__
struct sockaddr_in6 sin6;
struct sockaddr_in6 *sin6p;
#endif
remote = 0;
if (RM == NULL || *RM == '\0')
return NULL;
siginterrupt(SIGINT, 1);
if (getifaddrs(&ifap) < 0) {
(void)snprintf(errbuf, sizeof(errbuf),
"unable to get local interface address: %s",
strerror(errno));
siginterrupt(SIGINT, 0);
return errbuf;
}
siginterrupt(SIGINT, 0);
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
res = NULL;
siginterrupt(SIGINT, 1);
error = getaddrinfo(RM, NULL, &hints, &res0);
siginterrupt(SIGINT, 0);
if (error) {
(void)snprintf(errbuf, sizeof(errbuf),
"unable to resolve remote machine %s: %s",
RM, gai_strerror(error));
freeifaddrs(ifap);
return errbuf;
}
remote = 1;
for (res = res0; res; res = res->ai_next) {
siginterrupt(SIGINT, 1);
error = getnameinfo(res->ai_addr, res->ai_addrlen,
rname, sizeof(rname), NULL, 0, niflags);
siginterrupt(SIGINT, 0);
if (error != 0)
continue;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
sa = ifa->ifa_addr;
#ifdef __KAME__
sin6p = (struct sockaddr_in6 *)sa;
if (sa->sa_family == AF_INET6 &&
sa->sa_len == sizeof(sin6) &&
IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr) &&
*(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]) {
memcpy(&sin6, ifa->ifa_addr, sizeof(sin6));
sin6.sin6_scope_id =
ntohs(*(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]);
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
sa = (struct sockaddr *)&sin6;
}
#endif
siginterrupt(SIGINT, 1);
error = getnameinfo(sa, sa->sa_len,
lname, sizeof(lname), NULL, 0, niflags);
siginterrupt(SIGINT, 0);
if (error != 0)
continue;
if (strcmp(rname, lname) == 0) {
remote = 0;
goto done;
}
}
}
done:
freeaddrinfo(res0);
freeifaddrs(ifap);
return NULL;
}
__dead void
fatal(const char *msg, ...)
{
extern char *__progname;
va_list ap;
va_start(ap, msg);
if (from != host)
(void)printf("%s: ", host);
(void)printf("%s: ", __progname);
if (printer)
(void)printf("%s: ", printer);
(void)vprintf(msg, ap);
va_end(ap);
(void)putchar('\n');
exit(1);
}
int
safe_open(const char *path, int flags, mode_t mode)
{
int fd, serrno;
struct stat stbuf;
if ((fd = open(path, flags|O_NONBLOCK, mode)) < 0 ||
fstat(fd, &stbuf) < 0) {
if (fd >= 0) {
serrno = errno;
close(fd);
errno = serrno;
}
return (-1);
}
if (!S_ISREG(stbuf.st_mode)) {
close(fd);
errno = EACCES;
return (-1);
}
if (mode)
(void)fchmod(fd, mode);
return (fd);
}
int
ckqueue(char *cap)
{
struct dirent *d;
DIR *dirp;
char *spooldir;
if (cgetstr(cap, "sd", &spooldir) >= 0) {
dirp = opendir(spooldir);
free(spooldir);
} else
dirp = opendir(_PATH_DEFSPOOL);
if (dirp == NULL)
return (-1);
while ((d = readdir(dirp)) != NULL) {
if (d->d_name[0] == 'c' && d->d_name[1] == 'f') {
closedir(dirp);
return (1);
}
}
closedir(dirp);
return (0);
}