#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef SYSV
#include <sys/wait.h>
#endif
#ifdef ISC_2_0
#include <sys/fcntl.h>
#endif
#ifdef SHADOW_SUPPORT
#include <shadow.h>
#endif
#include "paths.h"
#include "common.h"
#include "pcnfsd.h"
#include "extern.h"
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
#define MAXTIME_FOR_PRINT 10
#define MAXTIME_FOR_QUEUE 10
#define MAXTIME_FOR_CANCEL 10
#define MAXTIME_FOR_STATUS 10
#define QMAX 50
#define SIZECOL 62
#define FILECOL 24
char *expand_alias(char *, char *, char *, char *);
pr_list list_virtual_printers(void);
char *map_printer_name(char *);
void substitute(char *, const char *, const char *);
int suspicious(char *);
int valid_pr(char *);
struct stat statbuf;
char pathname[MAXPATHLEN];
char new_pathname[MAXPATHLEN];
char sp_name[MAXPATHLEN] = SPOOLDIR;
static char tempstr[256];
char delims[] = " \t\r\n:()";
pr_list printers = NULL;
pr_queue queue = NULL;
int
suspicious(char *s)
{
if (strpbrk(s, ";|&<>`'#!?*()[]^/${}\n\r\"\\:") != NULL)
return 1;
return 0;
}
int
valid_pr(char *pr)
{
char *p;
pr_list curr;
if (printers == NULL)
build_pr_list();
if (printers == NULL)
return (1);
p = map_printer_name(pr);
if (p == NULL)
return (1);
curr = printers;
while (curr) {
if (!strcmp(p, curr->pn))
return (1);
curr = curr->pr_next;
}
return (0);
}
pirstat
pr_init(char *sys, char *pr, char **sp)
{
int dir_mode = 0777;
int rc;
mode_t oldmask;
*sp = &pathname[0];
pathname[0] = '\0';
if (suspicious(sys) || suspicious(pr))
return (PI_RES_FAIL);
if (stat(sp_name, &statbuf) || !S_ISDIR(statbuf.st_mode))
goto badspool;
(void) snprintf(pathname, sizeof(pathname), "%s/%s", sp_name, sys);
oldmask = umask(0);
rc = mkdir(pathname, dir_mode);
umask(oldmask);
if ((rc < 0) && (errno != EEXIST))
goto badspool;
if (stat(pathname, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
badspool:
(void) snprintf(tempstr, sizeof(tempstr),
"rpc.pcnfsd: unable to set up spool directory %s\n",
pathname);
msg_out(tempstr);
pathname[0] = '\0';
return (PI_RES_FAIL);
}
if (!valid_pr(pr)) {
pathname[0] = '\0';
return (PI_RES_NO_SUCH_PRINTER);
}
return (PI_RES_OK);
}
psrstat
pr_start2(char *sys, char *pr, char *user, char *fname, char *opts, char **id)
{
char snum[20];
static char req_id[256];
char cmdbuf[256];
char resbuf[256];
FILE *fd;
int i;
char *xcmd;
int failed = 0;
#ifdef HACK_FOR_ROTATED_TRANSCRIPT
char scratch[512];
#endif
if (suspicious(sys) ||
suspicious(pr) ||
suspicious(user) ||
suspicious(fname))
return (PS_RES_FAIL);
(void) snprintf(pathname, sizeof(pathname), "%s/%s/%s", sp_name,
sys,
fname);
*id = &req_id[0];
req_id[0] = '\0';
if (stat(pathname, &statbuf)) {
(void) strlcat(pathname, ".spl", sizeof(pathname));
if (stat(pathname, &statbuf)) {
return (PS_RES_NO_FILE);
}
return (PS_RES_ALREADY);
}
if (statbuf.st_size == 0) {
(void) unlink(pathname);
return (PS_RES_NULL);
}
(void) strlcpy(new_pathname, pathname, sizeof(new_pathname));
(void) strlcat(new_pathname, ".spl", sizeof(new_pathname));
if (!stat(new_pathname, &statbuf)) {
(void) strlcpy(new_pathname, pathname, sizeof(new_pathname));
(void) snprintf(snum, sizeof(snum), "%d", rand());
(void) strlcat(new_pathname, snum, 4);
(void) strlcat(new_pathname, ".spl", sizeof(new_pathname));
}
if (rename(pathname, new_pathname)) {
(void) snprintf(tempstr, sizeof(tempstr),
"rpc.pcnfsd: spool file rename (%s->%s) failed.\n",
pathname, new_pathname);
msg_out(tempstr);
return (PS_RES_FAIL);
}
if (*opts == 'd') {
#if 0
(void) run_ps630(new_pathname, opts);
#else
(void) snprintf(tempstr, sizeof(tempstr),
"rpc.pcnfsd: ps630 filter disabled for %s\n", pathname);
msg_out(tempstr);
return (PS_RES_FAIL);
#endif
}
xcmd = expand_alias(pr, new_pathname, user, sys);
if (!xcmd) {
#ifdef SVR4
snprintf(cmdbuf, sizeof(cmdbuf), "/usr/bin/lp -c -d%s %s",
pr, new_pathname);
#else
snprintf(cmdbuf, sizeof(cmdbuf), "%s/lpr -P%s %s",
LPRDIR, pr, new_pathname);
#endif
xcmd = cmdbuf;
}
if ((fd = su_popen(user, xcmd, MAXTIME_FOR_PRINT)) == NULL) {
msg_out("rpc.pcnfsd: su_popen failed");
return (PS_RES_FAIL);
}
req_id[0] = '\0';
while (fgets(resbuf, 255, fd) != NULL) {
i = strlen(resbuf);
if (i)
resbuf[i - 1] = '\0';
if (!strncmp(resbuf, "request id is ", 14))
strlcpy(req_id, strtok(&resbuf[14], delims),
sizeof(req_id));
else
if (strembedded("disabled", resbuf))
failed = 1;
}
if (su_pclose(fd) == 255)
msg_out("rpc.pcnfsd: su_pclose alert");
(void) unlink(new_pathname);
return ((failed | interrupted) ? PS_RES_FAIL : PS_RES_OK);
}
#ifdef SVR4
int
build_pr_list()
{
pr_list last = NULL;
pr_list curr = NULL;
char buff[256];
FILE *p;
char *cp;
int saw_system;
p = popen("lpstat -v", "r");
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lp status");
return (0);
}
while (fgets(buff, 255, p) != NULL) {
cp = strtok(buff, delims);
if (!cp)
continue;
if (!strcmp(cp, "device"))
saw_system = 0;
else
if (!strcmp(cp, "system"))
saw_system = 1;
else
continue;
cp = strtok(NULL, delims);
if (!cp || strcmp(cp, "for"))
continue;
cp = strtok(NULL, delims);
if (!cp)
continue;
curr = (struct pr_list_item *)
grab(sizeof(struct pr_list_item));
curr->pn = strdup(cp);
curr->device = NULL;
curr->remhost = NULL;
curr->cm = strdup("-");
curr->pr_next = NULL;
cp = strtok(NULL, delims);
if (cp && !strcmp(cp, "is"))
cp = strtok(NULL, delims);
if (!cp) {
free_pr_list_item(curr);
continue;
}
if (saw_system) {
curr->remhost = strdup(cp);
cp = strtok(NULL, delims);
if (!cp) {
curr->device = strdup(curr->pn);
} else {
if (strcmp(cp, "as")) {
free_pr_list_item(curr);
continue;
}
cp = strtok(NULL, delims);
if (!cp || strcmp(cp, "printer")) {
free_pr_list_item(curr);
continue;
}
cp = strtok(NULL, delims);
if (!cp) {
free_pr_list_item(curr);
continue;
}
curr->device = strdup(cp);
}
} else
if (!strcmp(cp, "the")) {
cp = strtok(NULL, delims);
if (!cp || strcmp(cp, "remote")) {
free_pr_list_item(curr);
continue;
}
cp = strtok(NULL, delims);
if (!cp || strcmp(cp, "printer")) {
free_pr_list_item(curr);
continue;
}
cp = strtok(NULL, delims);
if (!cp) {
free_pr_list_item(curr);
continue;
}
curr->device = strdup(cp);
cp = strtok(NULL, delims);
if (!cp || strcmp(cp, "on")) {
free_pr_list_item(curr);
continue;
}
cp = strtok(NULL, delims);
if (!cp) {
free_pr_list_item(curr);
continue;
}
curr->remhost = strdup(cp);
} else {
curr->device = strdup(cp);
curr->remhost = strdup("");
}
if (last == NULL)
printers = curr;
else
last->pr_next = curr;
last = curr;
}
(void) pclose(p);
if (last == NULL)
printers = list_virtual_printers();
else
last->pr_next = list_virtual_printers();
return (1);
}
#else
int
build_pr_list()
{
pr_list last = NULL;
pr_list curr = NULL;
char buff[256];
FILE *p;
char *cp;
snprintf(buff, sizeof(buff), "%s/lpc status", LPCDIR);
p = popen(buff, "r");
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen lpc stat");
return (0);
}
while (fgets(buff, 255, p) != NULL) {
if (isspace((unsigned char)buff[0]))
continue;
if ((cp = strtok(buff, delims)) == NULL)
continue;
curr = (struct pr_list_item *)
grab(sizeof(struct pr_list_item));
curr->pn = strdup(cp);
curr->device = strdup(cp);
curr->remhost = strdup("");
curr->cm = strdup("-");
curr->pr_next = NULL;
if (last == NULL)
printers = curr;
else
last->pr_next = curr;
last = curr;
}
(void) pclose(p);
if (last == NULL)
printers = list_virtual_printers();
else
last->pr_next = list_virtual_printers();
return (1);
}
#endif
void *
grab(int n)
{
void *p;
p = (void *) malloc(n);
if (p == NULL) {
msg_out("rpc.pcnfsd: malloc failure");
exit(1);
}
return (p);
}
void
free_pr_list_item(pr_list curr)
{
if (curr->pn)
free(curr->pn);
if (curr->device)
free(curr->device);
if (curr->remhost)
free(curr->remhost);
if (curr->cm)
free(curr->cm);
if (curr->pr_next)
free_pr_list_item(curr->pr_next);
free(curr);
}
#ifdef SVR4
pirstat
build_pr_queue(printername pn, username user, int just_mine, int p_qlen, int p_qshown)
{
pr_queue last = NULL;
pr_queue curr = NULL;
char buff[256];
FILE *p;
char *owner;
char *job;
char *totsize;
if (queue) {
free_pr_queue_item(queue);
queue = NULL;
}
*p_qlen = 0;
*p_qshown = 0;
pn = map_printer_name(pn);
if (pn == NULL || !valid_pr(pn) || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
snprintf(buff, sizeof(buff), "/usr/bin/lpstat %s", pn);
p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lpstat queue query");
return (PI_RES_FAIL);
}
while (fgets(buff, 255, p) != NULL) {
job = strtok(buff, delims);
if (!job)
continue;
owner = strtok(NULL, delims);
if (!owner)
continue;
totsize = strtok(NULL, delims);
if (!totsize)
continue;
*p_qlen += 1;
if (*p_qshown > QMAX)
continue;
if (just_mine && strcasecmp(owner, user))
continue;
*p_qshown += 1;
curr = (struct pr_queue_item *)
grab(sizeof(struct pr_queue_item));
curr->position = *p_qlen;
curr->id = strdup(job);
curr->size = strdup(totsize);
curr->status = strdup("");
curr->system = strdup("");
curr->user = strdup(owner);
curr->file = strdup("");
curr->cm = strdup("-");
curr->pr_next = NULL;
if (last == NULL)
queue = curr;
else
last->pr_next = curr;
last = curr;
}
(void) su_pclose(p);
return (PI_RES_OK);
}
#else
pirstat
build_pr_queue(printername pn, username user, int just_mine, int *p_qlen, int *p_qshown)
{
pr_queue last = NULL;
pr_queue curr = NULL;
char buff[256];
FILE *p;
char *cp;
int i;
char *rank;
char *owner;
char *job;
char *files;
char *totsize;
if (queue) {
free_pr_queue_item(queue);
queue = NULL;
}
*p_qlen = 0;
*p_qshown = 0;
pn = map_printer_name(pn);
if (pn == NULL || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
snprintf(buff, sizeof(buff), "%s/lpq -P%s", LPRDIR, pn);
p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lpq");
return (PI_RES_FAIL);
}
while (fgets(buff, 255, p) != NULL) {
i = strlen(buff) - 1;
buff[i] = '\0';
if (i < SIZECOL)
continue;
if (!strncasecmp(buff, "rank", 4))
continue;
totsize = &buff[SIZECOL - 1];
files = &buff[FILECOL - 1];
cp = totsize;
cp--;
while (cp > files && isspace((unsigned char)*cp))
*cp-- = '\0';
buff[FILECOL - 2] = '\0';
cp = strtok(buff, delims);
if (!cp)
continue;
rank = cp;
cp = strtok(NULL, delims);
if (!cp)
continue;
owner = cp;
cp = strtok(NULL, delims);
if (!cp)
continue;
job = cp;
*p_qlen += 1;
if (*p_qshown > QMAX)
continue;
if (just_mine && strcasecmp(owner, user))
continue;
*p_qshown += 1;
curr = (struct pr_queue_item *)
grab(sizeof(struct pr_queue_item));
curr->position = atoi(rank);
curr->id = strdup(job);
curr->size = strdup(totsize);
curr->status = strdup(rank);
curr->system = strdup("");
curr->user = strdup(owner);
curr->file = strdup(files);
curr->cm = strdup("-");
curr->pr_next = NULL;
if (last == NULL)
queue = curr;
else
last->pr_next = curr;
last = curr;
}
(void) su_pclose(p);
return (PI_RES_OK);
}
#endif
void
free_pr_queue_item(pr_queue curr)
{
if (curr->id)
free(curr->id);
if (curr->size)
free(curr->size);
if (curr->status)
free(curr->status);
if (curr->system)
free(curr->system);
if (curr->user)
free(curr->user);
if (curr->file)
free(curr->file);
if (curr->cm)
free(curr->cm);
if (curr->pr_next)
free_pr_queue_item(curr->pr_next);
free(curr);
}
#ifdef SVR4
pirstat
get_pr_status(printername pn, bool_t *avail, bool_t *printing, int *qlen, bool_t *needs_operator, char *status, size_t statuslen)
{
char buff[256];
char cmd[64];
FILE *p;
int n;
pirstat stat = PI_RES_NO_SUCH_PRINTER;
*avail = FALSE;
*printing = FALSE;
*needs_operator = FALSE;
*qlen = 0;
*status = '\0';
pn = map_printer_name(pn);
if (pn == NULL || !valid_pr(pn) || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
n = strlen(pn);
snprintf(cmd, sizeof(cmd), "/usr/bin/lpstat -a %s -p %s", pn, pn);
p = popen(cmd, "r");
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lp status");
return (PI_RES_FAIL);
}
stat = PI_RES_OK;
while (fgets(buff, 255, p) != NULL) {
if (!strncmp(buff, pn, n)) {
if (!strstr(buff, "not accepting"))
*avail = TRUE;
continue;
}
if (!strncmp(buff, "printer ", 8)) {
if (!strstr(buff, "disabled"))
*printing = TRUE;
if (strstr(buff, "printing"))
strlcpy(status, "printing", statuslen);
else
if (strstr(buff, "idle"))
strlcpy(status, "idle", statuslen);
continue;
}
if (!strncmp(buff, "UX:", 3)) {
stat = PI_RES_NO_SUCH_PRINTER;
}
}
(void) pclose(p);
return (stat);
}
#else
pirstat
get_pr_status(printername pn, bool_t *avail, bool_t *printing, int *qlen, bool_t *needs_operator, char *status, size_t statuslen)
{
char cmd[128];
char buff[256];
char buff2[256];
char pname[64];
FILE *p;
char *cp;
char *cp1;
char *cp2;
int n;
pirstat pstat = PI_RES_NO_SUCH_PRINTER;
*avail = FALSE;
*printing = FALSE;
*needs_operator = FALSE;
*qlen = 0;
*status = '\0';
pn = map_printer_name(pn);
if (pn == NULL || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
snprintf(pname, sizeof(pname), "%s:", pn);
n = strlen(pname);
snprintf(cmd, sizeof(cmd), "%s/lpc status %s", LPCDIR, pn);
p = popen(cmd, "r");
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lp status");
return (PI_RES_FAIL);
}
while (fgets(buff, 255, p) != NULL) {
if (strncmp(buff, pname, n))
continue;
pstat = PI_RES_FAIL;
while (fgets(buff, 255, p) != NULL && isspace((unsigned char)buff[0])) {
cp = buff;
while (isspace((unsigned char)*cp))
cp++;
if (*cp == '\0')
break;
cp1 = cp;
cp2 = buff2;
while (*cp1 && *cp1 != '\n') {
*cp2++ = tolower((unsigned char)*cp1);
cp1++;
}
*cp1 = '\0';
*cp2 = '\0';
if (!strncmp(buff2, "queuing", 7)) {
*avail = (strstr(buff2, "enabled") != NULL);
continue;
}
if (!strncmp(buff2, "printing", 8)) {
*printing = (strstr(buff2, "enabled") != NULL);
continue;
}
if (isdigit((unsigned char)buff2[0]) && (strstr(buff2, "entr") != NULL)) {
*qlen = atoi(buff2);
continue;
}
if (strstr(buff2, "attention") != NULL ||
strstr(buff2, "error") != NULL)
*needs_operator = TRUE;
if (*needs_operator || strstr(buff2, "waiting") != NULL)
strlcpy(status, cp, statuslen);
}
pstat = PI_RES_OK;
break;
}
(void) pclose(p);
return (pstat);
}
#endif
#ifdef SVR4
pcrstat
pr_cancel(char *pr, char *user, char *id)
{
char cmdbuf[256];
char resbuf[256];
FILE *fd;
pcrstat stat = PC_RES_NO_SUCH_JOB;
pr = map_printer_name(pr);
if (pr == NULL || suspicious(pr))
return (PC_RES_NO_SUCH_PRINTER);
if (suspicious(id))
return (PC_RES_NO_SUCH_JOB);
snprintf(cmdbuf, sizeof(cmdbuf), "/usr/bin/cancel %s", id);
if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
msg_out("rpc.pcnfsd: su_popen failed");
return (PC_RES_FAIL);
}
if (fgets(resbuf, 255, fd) == NULL)
stat = PC_RES_FAIL;
else
if (!strstr(resbuf, "UX:"))
stat = PC_RES_OK;
else
if (strstr(resbuf, "doesn't exist"))
stat = PC_RES_NO_SUCH_JOB;
else
if (strstr(resbuf, "not a request id"))
stat = PC_RES_NO_SUCH_JOB;
else
if (strstr(resbuf, "Can't cancel request"))
stat = PC_RES_NOT_OWNER;
else
stat = PC_RES_FAIL;
if (su_pclose(fd) == 255)
msg_out("rpc.pcnfsd: su_pclose alert");
return (stat);
}
#else
pcrstat
pr_cancel(char *pr, char *user, char *id)
{
char cmdbuf[256];
char resbuf[256];
FILE *fd;
int i;
pcrstat pstat = PC_RES_NO_SUCH_JOB;
pr = map_printer_name(pr);
if (pr == NULL || suspicious(pr))
return (PC_RES_NO_SUCH_PRINTER);
if (suspicious(id))
return (PC_RES_NO_SUCH_JOB);
snprintf(cmdbuf, sizeof(cmdbuf), "%s/lprm -P%s %s", LPRDIR, pr, id);
if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
msg_out("rpc.pcnfsd: su_popen failed");
return (PC_RES_FAIL);
}
while (fgets(resbuf, 255, fd) != NULL) {
i = strlen(resbuf);
if (i)
resbuf[i - 1] = '\0';
if (strstr(resbuf, "dequeued") != NULL)
pstat = PC_RES_OK;
if (strstr(resbuf, "unknown printer") != NULL)
pstat = PC_RES_NO_SUCH_PRINTER;
if (strstr(resbuf, "Permission denied") != NULL)
pstat = PC_RES_NOT_OWNER;
}
if (su_pclose(fd) == 255)
msg_out("rpc.pcnfsd: su_pclose alert");
return (pstat);
}
#endif
#define NPRINTERDEFS 16
int num_aliases = 0;
struct {
char *a_printer;
char *a_alias_for;
char *a_command;
} alias[NPRINTERDEFS];
void
add_printer_alias(char *printer, char *alias_for, char *command)
{
size_t l;
if (num_aliases < NPRINTERDEFS) {
alias[num_aliases].a_printer = strdup(printer);
alias[num_aliases].a_alias_for =
(strcmp(alias_for, "-") ? strdup(alias_for) : NULL);
if (strstr(command, "$FILE"))
alias[num_aliases].a_command = strdup(command);
else {
l = strlen(command) + 8;
alias[num_aliases].a_command = (char *) grab(l);
strlcpy(alias[num_aliases].a_command, command, l);
strlcat(alias[num_aliases].a_command, " $FILE", l);
}
num_aliases++;
}
}
pr_list
list_virtual_printers()
{
pr_list first = NULL;
pr_list last = NULL;
pr_list curr = NULL;
int i;
if (num_aliases == 0)
return (NULL);
for (i = 0; i < num_aliases; i++) {
curr = (struct pr_list_item *)
grab(sizeof(struct pr_list_item));
curr->pn = strdup(alias[i].a_printer);
if (alias[i].a_alias_for == NULL)
curr->device = strdup("");
else
curr->device = strdup(alias[i].a_alias_for);
curr->remhost = strdup("");
curr->cm = strdup("(alias)");
curr->pr_next = NULL;
if (last == NULL)
first = curr;
else
last->pr_next = curr;
last = curr;
}
return (first);
}
char *
map_printer_name(char *printer)
{
int i;
for (i = 0; i < num_aliases; i++) {
if (!strcmp(printer, alias[i].a_printer))
return (alias[i].a_alias_for);
}
return (printer);
}
void
substitute(char *string, const char *token, const char *data)
{
char temp[512];
char *c;
while ((c = strstr(string, token)) != NULL) {
*c = '\0';
strlcpy(temp, string, sizeof(temp));
strlcat(temp, data, sizeof(temp));
c += strlen(token);
strlcat(temp, c, sizeof(temp));
strcpy(string, temp);
}
}
char *
expand_alias(char *printer, char *file, char *user, char *host)
{
static char expansion[512];
int i;
for (i = 0; i < num_aliases; i++) {
if (!strcmp(printer, alias[i].a_printer)) {
strlcpy(expansion, alias[i].a_command,
sizeof(expansion));
substitute(expansion, "$FILE", file);
substitute(expansion, "$USER", user);
substitute(expansion, "$HOST", host);
return (expansion);
}
}
return (NULL);
}