NetBSD/usr.sbin/rpc.pcnfsd/pcnfsd_print.c

1351 lines
32 KiB
C
Raw Normal View History

1997-10-25 17:45:55 +04:00
/* $NetBSD: pcnfsd_print.c,v 1.5 1997/10/25 13:45:58 lukem Exp $ */
1995-07-26 02:20:13 +04:00
1995-07-24 08:02:44 +04:00
/* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_print.c 1.7 92/01/24 19:58:58 SMI */
/*
**=====================================================================
** Copyright (c) 1986,1987,1988,1989,1990,1991 by Sun Microsystems, Inc.
** @(#)pcnfsd_print.c 1.7 1/24/92
**=====================================================================
*/
/*
**=====================================================================
** I N C L U D E F I L E S E C T I O N *
** *
** If your port requires different include files, add a suitable *
** #define in the customization section, and make the inclusion or *
** exclusion of the files conditional on this. *
**=====================================================================
*/
1997-10-25 17:45:55 +04:00
1995-07-24 08:02:44 +04:00
#include <sys/file.h>
#include <sys/ioctl.h>
1997-10-25 17:45:55 +04:00
#include <sys/stat.h>
#include <ctype.h>
1995-07-24 08:02:44 +04:00
#include <errno.h>
1997-10-25 17:45:55 +04:00
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
1995-07-24 08:02:44 +04:00
#include <string.h>
1997-10-25 17:45:55 +04:00
#include <unistd.h>
1995-07-24 08:02:44 +04:00
#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"
1997-10-25 17:45:55 +04:00
#include "common.h"
#include "pcnfsd.h"
#include "extern.h"
1995-07-24 08:02:44 +04:00
/*
**---------------------------------------------------------------------
1997-10-25 17:45:55 +04:00
** Other #define's
1995-07-24 08:02:44 +04:00
**---------------------------------------------------------------------
*/
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
/*
** The following defintions give the maximum time allowed for
** an external command to run (in seconds)
*/
#define MAXTIME_FOR_PRINT 10
#define MAXTIME_FOR_QUEUE 10
#define MAXTIME_FOR_CANCEL 10
#define MAXTIME_FOR_STATUS 10
#define QMAX 50
/*
** The following is derived from ucb/lpd/displayq.c
*/
#define SIZECOL 62
#define FILECOL 24
1997-10-25 17:45:55 +04:00
char *expand_alias __P((char *, char *, char *, char *));
pr_list list_virtual_printers __P((void));
char *map_printer_name __P((char *));
void substitute __P((char *, char *, char *));
int suspicious __P((char *));
int valid_pr __P((char *));
1995-07-24 08:02:44 +04:00
/*
**---------------------------------------------------------------------
** Misc. variable definitions
**---------------------------------------------------------------------
*/
1997-10-25 17:45:55 +04:00
struct stat statbuf;
char pathname[MAXPATHLEN];
char new_pathname[MAXPATHLEN];
char sp_name[MAXPATHLEN] = SPOOLDIR;
char tempstr[256];
char delims[] = " \t\r\n:()";
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
pr_list printers = NULL;
pr_queue queue = NULL;
1995-07-24 08:02:44 +04:00
/*
**=====================================================================
** C O D E S E C T I O N *
**=====================================================================
*/
/*
* This is the latest word on the security check. The following
* routine "suspicious()" returns non-zero if the character string
* passed to it contains any shell metacharacters.
* Callers will typically code
*
* if(suspicious(some_parameter)) reject();
*/
1997-10-25 17:45:55 +04:00
int
suspicious(s)
char *s;
1995-07-24 08:02:44 +04:00
{
if (strpbrk(s, ";|&<>`'#!?*()[]^/${}\n\r\"\\:") != NULL)
1995-07-24 08:02:44 +04:00
return 1;
return 0;
}
int
valid_pr(pr)
1997-10-25 17:45:55 +04:00
char *pr;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
char *p;
pr_list curr;
if (printers == NULL)
1995-07-24 08:02:44 +04:00
build_pr_list();
1997-10-25 17:45:55 +04:00
if (printers == NULL)
return (1); /* can't tell - assume it's good */
1995-07-24 08:02:44 +04:00
p = map_printer_name(pr);
if (p == NULL)
1997-10-25 17:45:55 +04:00
return (1); /* must be ok is maps to NULL! */
1995-07-24 08:02:44 +04:00
curr = printers;
1997-10-25 17:45:55 +04:00
while (curr) {
if (!strcmp(p, curr->pn))
return (1);
1995-07-24 08:02:44 +04:00
curr = curr->pr_next;
}
1997-10-25 17:45:55 +04:00
return (0);
}
/*
* get pathname of current directory and return to client
*
* Note: This runs as root on behalf of a client request.
* As described in CERT advisory CA-96.08, be careful about
* doing a chmod on something that could be a symlink...
*/
pirstat
pr_init(sys, pr, sp)
1997-10-25 17:45:55 +04:00
char *sys;
char *pr;
char **sp;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
int dir_mode = 0777;
int rc;
mode_t oldmask;
1995-07-24 08:02:44 +04:00
*sp = &pathname[0];
pathname[0] = '\0';
1997-10-25 17:45:55 +04:00
if (suspicious(sys) || suspicious(pr))
return (PI_RES_FAIL);
1995-07-24 08:02:44 +04:00
/*
* Make sure the server spool directory exists.
* Never create it here - the sysadmin does that.
*/
if (stat(sp_name, &statbuf) || !S_ISDIR(statbuf.st_mode))
goto badspool;
1995-07-24 08:02:44 +04:00
/*
* Create the client spool directory if needed.
* Just do the mkdir call and ignore EEXIST.
* Mode of client directory should be 777.
*/
1997-10-25 17:45:55 +04:00
(void) sprintf(pathname, "%s/%s", sp_name, sys);
oldmask = umask(0);
1995-07-24 08:02:44 +04:00
rc = mkdir(pathname, dir_mode); /* DON'T ignore this return code */
umask(oldmask);
if ((rc < 0) && (errno != EEXIST))
goto badspool;
/* By this point the client spool dir should exist. */
if (stat(pathname, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
/* No spool directory... */
1997-10-25 17:45:55 +04:00
badspool:
(void) sprintf(tempstr,
"rpc.pcnfsd: unable to set up spool directory %s\n",
1997-10-25 17:45:55 +04:00
pathname);
msg_out(tempstr);
1997-10-25 17:45:55 +04:00
pathname[0] = '\0'; /* null to tell client bad vibes */
return (PI_RES_FAIL);
}
/* OK, we have a spool directory. */
1997-10-25 17:45:55 +04:00
if (!valid_pr(pr)) {
pathname[0] = '\0'; /* null to tell client bad vibes */
return (PI_RES_NO_SUCH_PRINTER);
}
1997-10-25 17:45:55 +04:00
return (PI_RES_OK);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
psrstat
pr_start2(system, pr, user, fname, opts, id)
char *system;
char *pr;
char *user;
char *fname;
char *opts;
char **id;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
char snum[20];
static char req_id[256];
char cmdbuf[256];
char resbuf[256];
FILE *fd;
int i;
char *xcmd;
int failed = 0;
1995-07-24 08:02:44 +04:00
#ifdef HACK_FOR_ROTATED_TRANSCRIPT
1997-10-25 17:45:55 +04:00
char scratch[512];
1995-07-24 08:02:44 +04:00
#endif
1997-10-25 17:45:55 +04:00
if (suspicious(system) ||
suspicious(pr) ||
suspicious(user) ||
suspicious(fname))
return (PS_RES_FAIL);
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
(void) sprintf(pathname, "%s/%s/%s", sp_name,
system,
fname);
1995-07-24 08:02:44 +04:00
*id = &req_id[0];
req_id[0] = '\0';
1997-10-25 17:45:55 +04:00
if (stat(pathname, &statbuf)) {
/*
**-----------------------------------------------------------------
** We can't stat the file. Let's try appending '.spl' and
** see if it's already in progress.
**-----------------------------------------------------------------
*/
(void) strcat(pathname, ".spl");
if (stat(pathname, &statbuf)) {
/*
**----------------------------------------------------------------
** It really doesn't exist.
**----------------------------------------------------------------
*/
return (PS_RES_NO_FILE);
}
/*
**-------------------------------------------------------------
** It is already on the way.
**-------------------------------------------------------------
*/
return (PS_RES_ALREADY);
}
if (statbuf.st_size == 0) {
/*
**-------------------------------------------------------------
** Null file - don't print it, just kill it.
**-------------------------------------------------------------
*/
(void) unlink(pathname);
return (PS_RES_NULL);
}
/*
**-------------------------------------------------------------
** The file is real, has some data, and is not already going out.
** We rename it by appending '.spl' and exec "lpr" to do the
** actual work.
**-------------------------------------------------------------
*/
(void) strcpy(new_pathname, pathname);
(void) strcat(new_pathname, ".spl");
1995-07-24 08:02:44 +04:00
/*
**-------------------------------------------------------------
** See if the new filename exists so as not to overwrite it.
**-------------------------------------------------------------
*/
1997-10-25 17:45:55 +04:00
if (!stat(new_pathname, &statbuf)) {
(void) strcpy(new_pathname, pathname); /* rebuild a new name */
(void) sprintf(snum, "%d", rand()); /* get some number */
(void) strncat(new_pathname, snum, 3);
(void) strcat(new_pathname, ".spl"); /* new spool file */
}
if (rename(pathname, new_pathname)) {
/*
**---------------------------------------------------------------
** Should never happen.
**---------------------------------------------------------------
*/
(void) sprintf(tempstr, "rpc.pcnfsd: spool file rename (%s->%s) failed.\n",
pathname, new_pathname);
msg_out(tempstr);
return (PS_RES_FAIL);
}
if (*opts == 'd') {
/*
**------------------------------------------------------
** This is a Diablo print stream. Apply the ps630
** filter with the appropriate arguments.
**------------------------------------------------------
*/
#if 0 /* XXX: Temporary fix for CERT advisory
* CA-96.08 */
(void) run_ps630(new_pathname, opts);
#else
1997-10-25 17:45:55 +04:00
(void) sprintf(tempstr,
"rpc.pcnfsd: ps630 filter disabled for %s\n", pathname);
msg_out(tempstr);
return (PS_RES_FAIL);
#endif
1997-10-25 17:45:55 +04:00
}
/*
** Try to match to an aliased printer
*/
xcmd = expand_alias(pr, new_pathname, user, system);
if (!xcmd) {
#ifdef SVR4
1997-10-25 17:45:55 +04:00
/*
* Use the copy option so we can remove the orignal
* spooled nfs file from the spool directory.
*/
1997-10-25 17:45:55 +04:00
sprintf(cmdbuf, "/usr/bin/lp -c -d%s %s",
pr, new_pathname);
#else /* SVR4 */
/* BSD way: lpr */
sprintf(cmdbuf, "%s/lpr -P%s %s",
LPRDIR, pr, new_pathname);
#endif /* SVR4 */
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'; /* asume failure */
while (fgets(resbuf, 255, fd) != NULL) {
i = strlen(resbuf);
if (i)
resbuf[i - 1] = '\0'; /* trim NL */
if (!strncmp(resbuf, "request id is ", 14))
/* New - just the first word is needed */
strcpy(req_id, strtok(&resbuf[14], delims));
else
if (strembedded("disabled", resbuf))
1995-07-24 08:02:44 +04:00
failed = 1;
1997-10-25 17:45:55 +04:00
}
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);
1995-07-24 08:02:44 +04:00
}
/*
* build_pr_list: determine which printers are valid.
1995-07-24 08:02:44 +04:00
* on SVR4 use "lpstat -v"
* on BSD use "lpc status"
*/
#ifdef SVR4
/*
* In SVR4 the command to determine which printers are
* valid is lpstat -v. The output is something like this:
*
* device for lp: /dev/lp0
* system for pcdslw: hinode
* system for bletch: hinode (as printer hisname)
*
* On SunOS using the SysV compatibility package, the output
* is more like:
*
* device for lp is /dev/lp0
* device for pcdslw is the remote printer pcdslw on hinode
* device for bletch is the remote printer hisname on hinode
*
* It is fairly simple to create logic that will handle either
* possibility:
*/
int
build_pr_list()
{
pr_list last = NULL;
pr_list curr = NULL;
1997-10-25 17:45:55 +04:00
char buff[256];
FILE *p;
char *cp;
int saw_system;
p = popen("lpstat -v", "r");
1997-10-25 17:45:55 +04:00
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lp status");
1997-10-25 17:45:55 +04:00
return (0);
}
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
cp = strtok(buff, delims);
1997-10-25 17:45:55 +04:00
if (!cp)
continue;
1997-10-25 17:45:55 +04:00
if (!strcmp(cp, "device"))
saw_system = 0;
else
1997-10-25 17:45:55 +04:00
if (!strcmp(cp, "system"))
saw_system = 1;
else
continue;
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp || strcmp(cp, "for"))
continue;
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp)
continue;
curr = (struct pr_list_item *)
1997-10-25 17:45:55 +04:00
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);
1997-10-25 17:45:55 +04:00
if (cp && !strcmp(cp, "is"))
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp) {
free_pr_list_item(curr);
continue;
}
1997-10-25 17:45:55 +04:00
if (saw_system) {
/* "system" OR "system (as printer pname)" */
curr->remhost = strdup(cp);
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp) {
/* simple format */
curr->device = strdup(curr->pn);
} else {
/* "sys (as printer pname)" */
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);
1997-10-25 17:45:55 +04:00
if (!cp) {
free_pr_list_item(curr);
continue;
}
curr->device = strdup(cp);
}
1997-10-25 17:45:55 +04:00
} else
if (!strcmp(cp, "the")) {
/* start of "the remote printer foo on bar" */
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 {
/* the local name */
curr->device = strdup(cp);
curr->remhost = strdup("");
}
1997-10-25 17:45:55 +04:00
if (last == NULL)
printers = curr;
else
last->pr_next = curr;
last = curr;
}
(void) pclose(p);
/*
** Now add on the virtual printers, if any
*/
1997-10-25 17:45:55 +04:00
if (last == NULL)
printers = list_virtual_printers();
else
last->pr_next = list_virtual_printers();
1997-10-25 17:45:55 +04:00
return (1);
}
1997-10-25 17:45:55 +04:00
#else /* SVR4 */
/*
* BSD way: lpc stat
1995-07-24 08:02:44 +04:00
*/
int
build_pr_list()
{
pr_list last = NULL;
pr_list curr = NULL;
1997-10-25 17:45:55 +04:00
char buff[256];
FILE *p;
char *cp;
1995-07-24 08:02:44 +04:00
sprintf(buff, "%s/lpc status", LPCDIR);
p = popen(buff, "r");
1997-10-25 17:45:55 +04:00
if (p == NULL) {
1995-07-24 08:02:44 +04:00
msg_out("rpc.pcnfsd: unable to popen lpc stat");
1997-10-25 17:45:55 +04:00
return (0);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
1995-07-24 08:02:44 +04:00
if (isspace(buff[0]))
continue;
if ((cp = strtok(buff, delims)) == NULL)
continue;
curr = (struct pr_list_item *)
1997-10-25 17:45:55 +04:00
grab(sizeof(struct pr_list_item));
1995-07-24 08:02:44 +04:00
/* XXX - Should distinguish remote printers. */
curr->pn = strdup(cp);
curr->device = strdup(cp);
curr->remhost = strdup("");
curr->cm = strdup("-");
curr->pr_next = NULL;
1997-10-25 17:45:55 +04:00
if (last == NULL)
1995-07-24 08:02:44 +04:00
printers = curr;
else
last->pr_next = curr;
last = curr;
}
(void) fclose(p);
/*
** Now add on the virtual printers, if any
*/
1997-10-25 17:45:55 +04:00
if (last == NULL)
1995-07-24 08:02:44 +04:00
printers = list_virtual_printers();
else
last->pr_next = list_virtual_printers();
1997-10-25 17:45:55 +04:00
return (1);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
#endif /* SVR4 */
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
void *
grab(n)
int n;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
void *p;
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
p = (void *) malloc(n);
if (p == NULL) {
1995-07-24 08:02:44 +04:00
msg_out("rpc.pcnfsd: malloc failure");
exit(1);
}
1997-10-25 17:45:55 +04:00
return (p);
1995-07-24 08:02:44 +04:00
}
void
free_pr_list_item(curr)
1997-10-25 17:45:55 +04:00
pr_list curr;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
if (curr->pn)
1995-07-24 08:02:44 +04:00
free(curr->pn);
1997-10-25 17:45:55 +04:00
if (curr->device)
1995-07-24 08:02:44 +04:00
free(curr->device);
1997-10-25 17:45:55 +04:00
if (curr->remhost)
1995-07-24 08:02:44 +04:00
free(curr->remhost);
1997-10-25 17:45:55 +04:00
if (curr->cm)
1995-07-24 08:02:44 +04:00
free(curr->cm);
1997-10-25 17:45:55 +04:00
if (curr->pr_next)
free_pr_list_item(curr->pr_next); /* recurse */
1995-07-24 08:02:44 +04:00
free(curr);
}
/*
* build_pr_queue: used to show the print queue.
*
* Note that the first thing we do is to discard any
* existing queue.
*/
#ifdef SVR4
1995-07-24 08:02:44 +04:00
/*
** In SVR4 the command to list the print jobs for printer
** lp is "lpstat lp" (or, equivalently, "lpstat -p lp").
** The output looks like this:
1997-10-25 17:45:55 +04:00
**
** lp-2 root 939 Jul 10 21:56
** lp-5 geoff 15 Jul 12 23:23
** lp-6 geoff 15 Jul 12 23:23
1997-10-25 17:45:55 +04:00
**
** If the first job is actually printing the first line
** is modified, as follows:
1995-07-24 08:02:44 +04:00
**
** lp-2 root 939 Jul 10 21:56 on lp
1997-10-25 17:45:55 +04:00
**
** I don't yet have any info on what it looks like if the printer
** is remote and we're spooling over the net. However for
** the purposes of rpc.pcnfsd we can simply say that field 1 is the
** job ID, field 2 is the submitter, and field 3 is the size.
** We can check for the presence of the string " on " in the
** first record to determine if we should count it as rank 0 or rank 1,
** but it won't hurt if we get it wrong.
**/
pirstat
build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
1997-10-25 17:45:55 +04:00
printername pn;
username user;
int just_mine;
int *p_qlen;
int *p_qshown;
{
1997-10-25 17:45:55 +04:00
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);
1997-10-25 17:45:55 +04:00
if (pn == NULL || !valid_pr(pn) || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
sprintf(buff, "/usr/bin/lpstat %s", pn);
p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
1997-10-25 17:45:55 +04:00
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lpstat queue query");
1997-10-25 17:45:55 +04:00
return (PI_RES_FAIL);
}
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
job = strtok(buff, delims);
1997-10-25 17:45:55 +04:00
if (!job)
continue;
owner = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!owner)
continue;
totsize = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!totsize)
continue;
*p_qlen += 1;
1997-10-25 17:45:55 +04:00
if (*p_qshown > QMAX)
continue;
1997-10-25 17:45:55 +04:00
if (just_mine && strcasecmp(owner, user))
continue;
*p_qshown += 1;
curr = (struct pr_queue_item *)
1997-10-25 17:45:55 +04:00
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;
1997-10-25 17:45:55 +04:00
if (last == NULL)
queue = curr;
else
last->pr_next = curr;
last = curr;
}
(void) su_pclose(p);
1997-10-25 17:45:55 +04:00
return (PI_RES_OK);
}
1997-10-25 17:45:55 +04:00
#else /* SVR4 */
1995-07-24 08:02:44 +04:00
pirstat
build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
1997-10-25 17:45:55 +04:00
printername pn;
username user;
int just_mine;
int *p_qlen;
int *p_qshown;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
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) {
1995-07-24 08:02:44 +04:00
free_pr_queue_item(queue);
queue = NULL;
}
*p_qlen = 0;
*p_qshown = 0;
pn = map_printer_name(pn);
1997-10-25 17:45:55 +04:00
if (pn == NULL || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
1995-07-24 08:02:44 +04:00
sprintf(buff, "%s/lpq -P%s", LPRDIR, pn);
p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
1997-10-25 17:45:55 +04:00
if (p == NULL) {
1995-07-24 08:02:44 +04:00
msg_out("rpc.pcnfsd: unable to popen() lpq");
1997-10-25 17:45:55 +04:00
return (PI_RES_FAIL);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
1995-07-24 08:02:44 +04:00
i = strlen(buff) - 1;
1997-10-25 17:45:55 +04:00
buff[i] = '\0'; /* zap trailing NL */
if (i < SIZECOL)
1995-07-24 08:02:44 +04:00
continue;
1997-10-25 17:45:55 +04:00
if (!strncasecmp(buff, "rank", 4))
1995-07-24 08:02:44 +04:00
continue;
1997-10-25 17:45:55 +04:00
totsize = &buff[SIZECOL - 1];
files = &buff[FILECOL - 1];
1995-07-24 08:02:44 +04:00
cp = totsize;
cp--;
1997-10-25 17:45:55 +04:00
while (cp > files && isspace(*cp))
1995-07-24 08:02:44 +04:00
*cp-- = '\0';
1997-10-25 17:45:55 +04:00
buff[FILECOL - 2] = '\0';
1995-07-24 08:02:44 +04:00
cp = strtok(buff, delims);
1997-10-25 17:45:55 +04:00
if (!cp)
1995-07-24 08:02:44 +04:00
continue;
rank = cp;
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp)
1995-07-24 08:02:44 +04:00
continue;
owner = cp;
cp = strtok(NULL, delims);
1997-10-25 17:45:55 +04:00
if (!cp)
1995-07-24 08:02:44 +04:00
continue;
job = cp;
*p_qlen += 1;
1997-10-25 17:45:55 +04:00
if (*p_qshown > QMAX)
1995-07-24 08:02:44 +04:00
continue;
1997-10-25 17:45:55 +04:00
if (just_mine && strcasecmp(owner, user))
1995-07-24 08:02:44 +04:00
continue;
*p_qshown += 1;
curr = (struct pr_queue_item *)
1997-10-25 17:45:55 +04:00
grab(sizeof(struct pr_queue_item));
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
curr->position = atoi(rank); /* active -> 0 */
1995-07-24 08:02:44 +04:00
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;
1997-10-25 17:45:55 +04:00
if (last == NULL)
1995-07-24 08:02:44 +04:00
queue = curr;
else
last->pr_next = curr;
last = curr;
}
(void) su_pclose(p);
1997-10-25 17:45:55 +04:00
return (PI_RES_OK);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
#endif /* SVR4 */
1995-07-24 08:02:44 +04:00
void
free_pr_queue_item(curr)
1997-10-25 17:45:55 +04:00
pr_queue curr;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
if (curr->id)
1995-07-24 08:02:44 +04:00
free(curr->id);
1997-10-25 17:45:55 +04:00
if (curr->size)
1995-07-24 08:02:44 +04:00
free(curr->size);
1997-10-25 17:45:55 +04:00
if (curr->status)
1995-07-24 08:02:44 +04:00
free(curr->status);
1997-10-25 17:45:55 +04:00
if (curr->system)
1995-07-24 08:02:44 +04:00
free(curr->system);
1997-10-25 17:45:55 +04:00
if (curr->user)
1995-07-24 08:02:44 +04:00
free(curr->user);
1997-10-25 17:45:55 +04:00
if (curr->file)
1995-07-24 08:02:44 +04:00
free(curr->file);
1997-10-25 17:45:55 +04:00
if (curr->cm)
1995-07-24 08:02:44 +04:00
free(curr->cm);
1997-10-25 17:45:55 +04:00
if (curr->pr_next)
free_pr_queue_item(curr->pr_next); /* recurse */
1995-07-24 08:02:44 +04:00
free(curr);
}
#ifdef SVR4
1995-07-24 08:02:44 +04:00
/*
** New - SVR4 printer status handling.
**
** The command we'll use for checking the status of printer "lp"
** is "lpstat -a lp -p lp". Here are some sample outputs:
**
1997-10-25 17:45:55 +04:00
**
** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
** printer lp disabled since Thu Feb 21 22:52:36 EST 1991. available.
** new printer
** ---
** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
** unknown reason
** printer pcdslw disabled since Fri Jul 12 22:15:37 EDT 1991. available.
** new printer
** ---
** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
** ---
** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
** ---
** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
** printer lp disabled since Sat Jul 13 12:05:20 EDT 1991. available.
** unknown reason
** ---
** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
** unknown reason
** printer pcdslw is idle. enabled since Sat Jul 13 12:05:28 EDT 1991. available.
**
** Note that these are actual outputs. The format (which is totally
** different from the lpstat in SunOS) seems to break down as
** follows:
** (1) The first line has the form "printername [not] accepting requests,,,"
** This is trivial to decode.
** (2) The second line has several forms, all beginning "printer printername":
** (2.1) "... disabled"
** (2.2) "... is idle"
** (2.3) "... now printing jobid"
** The "available" comment seems to be meaningless. The next line
** is the "reason" code which the operator can supply when issuing
** a "disable" or "reject" command.
** Note that there is no way to check the number of entries in the
** queue except to ask for the queue and count them.
*/
1995-07-24 08:02:44 +04:00
pirstat
get_pr_status(pn, avail, printing, qlen, needs_operator, status)
1997-10-25 17:45:55 +04:00
printername pn;
bool_t *avail;
bool_t *printing;
int *qlen;
bool_t *needs_operator;
char *status;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
char buff[256];
char cmd[64];
FILE *p;
int n;
pirstat stat = PI_RES_NO_SUCH_PRINTER;
1995-07-24 08:02:44 +04:00
/* assume the worst */
*avail = FALSE;
*printing = FALSE;
*needs_operator = FALSE;
*qlen = 0;
*status = '\0';
pn = map_printer_name(pn);
1997-10-25 17:45:55 +04:00
if (pn == NULL || !valid_pr(pn) || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
n = strlen(pn);
sprintf(cmd, "/usr/bin/lpstat -a %s -p %s", pn, pn);
p = popen(cmd, "r");
1997-10-25 17:45:55 +04:00
if (p == NULL) {
msg_out("rpc.pcnfsd: unable to popen() lp status");
1997-10-25 17:45:55 +04:00
return (PI_RES_FAIL);
}
stat = PI_RES_OK;
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
if (!strncmp(buff, pn, n)) {
if (!strstr(buff, "not accepting"))
*avail = TRUE;
continue;
}
1997-10-25 17:45:55 +04:00
if (!strncmp(buff, "printer ", 8)) {
if (!strstr(buff, "disabled"))
*printing = TRUE;
1997-10-25 17:45:55 +04:00
if (strstr(buff, "printing"))
strcpy(status, "printing");
1997-10-25 17:45:55 +04:00
else
if (strstr(buff, "idle"))
strcpy(status, "idle");
continue;
}
1997-10-25 17:45:55 +04:00
if (!strncmp(buff, "UX:", 3)) {
stat = PI_RES_NO_SUCH_PRINTER;
}
}
(void) pclose(p);
1997-10-25 17:45:55 +04:00
return (stat);
}
1997-10-25 17:45:55 +04:00
#else /* SVR4 */
/*
* BSD way: lpc status
*/
pirstat
get_pr_status(pn, avail, printing, qlen, needs_operator, status)
1997-10-25 17:45:55 +04:00
printername pn;
bool_t *avail;
bool_t *printing;
int *qlen;
bool_t *needs_operator;
char *status;
{
1997-10-25 17:45:55 +04:00
char cmd[128];
char buff[256];
char buff2[256];
char pname[64];
FILE *p;
char *cp;
char *cp1;
char *cp2;
int n;
pirstat stat = PI_RES_NO_SUCH_PRINTER;
/* assume the worst */
*avail = FALSE;
*printing = FALSE;
*needs_operator = FALSE;
*qlen = 0;
*status = '\0';
1995-07-24 08:02:44 +04:00
pn = map_printer_name(pn);
1997-10-25 17:45:55 +04:00
if (pn == NULL || suspicious(pn))
return (PI_RES_NO_SUCH_PRINTER);
1995-07-24 08:02:44 +04:00
sprintf(pname, "%s:", pn);
n = strlen(pname);
sprintf(cmd, "%s/lpc status %s", LPCDIR, pn);
p = popen(cmd, "r");
1997-10-25 17:45:55 +04:00
if (p == NULL) {
1995-07-24 08:02:44 +04:00
msg_out("rpc.pcnfsd: unable to popen() lp status");
1997-10-25 17:45:55 +04:00
return (PI_RES_FAIL);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL) {
if (strncmp(buff, pname, n))
1995-07-24 08:02:44 +04:00
continue;
/*
** We have a match. The only failure now is PI_RES_FAIL if
** lpstat output cannot be decoded
*/
stat = PI_RES_FAIL;
/*
** The next four lines are usually if the form
**
** queuing is [enabled|disabled]
** printing is [enabled|disabled]
** [no entries | N entr[y|ies] in spool area]
** <status message, may include the word "attention">
*/
1997-10-25 17:45:55 +04:00
while (fgets(buff, 255, p) != NULL && isspace(buff[0])) {
1995-07-24 08:02:44 +04:00
cp = buff;
1997-10-25 17:45:55 +04:00
while (isspace(*cp))
1995-07-24 08:02:44 +04:00
cp++;
1997-10-25 17:45:55 +04:00
if (*cp == '\0')
1995-07-24 08:02:44 +04:00
break;
cp1 = cp;
cp2 = buff2;
1997-10-25 17:45:55 +04:00
while (*cp1 && *cp1 != '\n') {
1995-07-24 08:02:44 +04:00
*cp2++ = tolower(*cp1);
cp1++;
}
*cp1 = '\0';
*cp2 = '\0';
/*
** Now buff2 has a lower-cased copy and cp points at the original;
** both are null terminated without any newline
1997-10-25 17:45:55 +04:00
*/
if (!strncmp(buff2, "queuing", 7)) {
1995-07-24 08:02:44 +04:00
*avail = (strstr(buff2, "enabled") != NULL);
continue;
}
1997-10-25 17:45:55 +04:00
if (!strncmp(buff2, "printing", 8)) {
1995-07-24 08:02:44 +04:00
*printing = (strstr(buff2, "enabled") != NULL);
continue;
}
1997-10-25 17:45:55 +04:00
if (isdigit(buff2[0]) && (strstr(buff2, "entr") != NULL)) {
1995-07-24 08:02:44 +04:00
*qlen = atoi(buff2);
continue;
}
1997-10-25 17:45:55 +04:00
if (strstr(buff2, "attention") != NULL ||
strstr(buff2, "error") != NULL)
1995-07-24 08:02:44 +04:00
*needs_operator = TRUE;
1997-10-25 17:45:55 +04:00
if (*needs_operator || strstr(buff2, "waiting") != NULL)
1995-07-24 08:02:44 +04:00
strcpy(status, cp);
}
stat = PI_RES_OK;
break;
}
(void) pclose(p);
1997-10-25 17:45:55 +04:00
return (stat);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
#endif /* SVR4 */
/*
* pr_cancel: cancel a print job
*/
#ifdef SVR4
1995-07-24 08:02:44 +04:00
/*
** For SVR4 we have to be prepared for the following kinds of output:
1997-10-25 17:45:55 +04:00
**
** # cancel lp-6
** request "lp-6" cancelled
** # cancel lp-33
** UX:cancel: WARNING: Request "lp-33" doesn't exist.
** # cancel foo-88
** UX:cancel: WARNING: Request "foo-88" doesn't exist.
** # cancel foo
** UX:cancel: WARNING: "foo" is not a request id or a printer.
** TO FIX: Cancel requests by id or by
** name of printer where printing.
** # su geoff
** $ cancel lp-2
** UX:cancel: WARNING: Can't cancel request "lp-2".
** TO FIX: You are not allowed to cancel
** another's request.
**
** There are probably other variations for remote printers.
** Basically, if the reply begins with the string
** "UX:cancel: WARNING: "
** we can strip this off and look for one of the following
** (1) 'R' - should be part of "Request "xxxx" doesn't exist."
** (2) '"' - should be start of ""foo" is not a request id or..."
** (3) 'C' - should be start of "Can't cancel request..."
**
** The fly in the ointment: all of this can change if these
** messages are localized..... :-(
*/
1997-10-25 17:45:55 +04:00
pcrstat
pr_cancel(pr, user, id)
char *pr;
char *user;
char *id;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
char cmdbuf[256];
char resbuf[256];
FILE *fd;
pcrstat stat = PC_RES_NO_SUCH_JOB;
1995-07-24 08:02:44 +04:00
pr = map_printer_name(pr);
1997-10-25 17:45:55 +04:00
if (pr == NULL || suspicious(pr))
return (PC_RES_NO_SUCH_PRINTER);
if (suspicious(id))
return (PC_RES_NO_SUCH_JOB);
sprintf(cmdbuf, "/usr/bin/cancel %s", id);
if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
msg_out("rpc.pcnfsd: su_popen failed");
1997-10-25 17:45:55 +04:00
return (PC_RES_FAIL);
}
1997-10-25 17:45:55 +04:00
if (fgets(resbuf, 255, fd) == NULL)
stat = PC_RES_FAIL;
1997-10-25 17:45:55 +04:00
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");
1997-10-25 17:45:55 +04:00
return (stat);
}
1997-10-25 17:45:55 +04:00
#else /* SVR4 */
/*
* BSD way: lprm
*/
1997-10-25 17:45:55 +04:00
pcrstat
pr_cancel(pr, user, id)
char *pr;
char *user;
char *id;
{
1997-10-25 17:45:55 +04:00
char cmdbuf[256];
char resbuf[256];
FILE *fd;
int i;
pcrstat stat = PC_RES_NO_SUCH_JOB;
1995-07-24 08:02:44 +04:00
pr = map_printer_name(pr);
1997-10-25 17:45:55 +04:00
if (pr == NULL || suspicious(pr))
return (PC_RES_NO_SUCH_PRINTER);
if (suspicious(id))
return (PC_RES_NO_SUCH_JOB);
sprintf(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'; /* trim NL */
if (strstr(resbuf, "dequeued") != NULL)
stat = PC_RES_OK;
if (strstr(resbuf, "unknown printer") != NULL)
stat = PC_RES_NO_SUCH_PRINTER;
if (strstr(resbuf, "Permission denied") != NULL)
stat = PC_RES_NOT_OWNER;
}
if (su_pclose(fd) == 255)
msg_out("rpc.pcnfsd: su_pclose alert");
return (stat);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
#endif /* SVR4 */
1995-07-24 08:02:44 +04:00
/*
** New subsystem here. We allow the administrator to define
** up to NPRINTERDEFS aliases for printer names. This is done
** using the "/etc/pcnfsd.conf" file, which is read at startup.
** There are three entry points to this subsystem
**
** void add_printer_alias(char *printer, char *alias_for, char *command)
**
** This is invoked from "config_from_file()" for each
** "printer" line. "printer" is the name of a printer; note that
** it is possible to redefine an existing printer. "alias_for"
** is the name of the underlying printer, used for queue listing
** and other control functions. If it is "-", there is no
** underlying printer, or the administrative functions are
** not applicable to this printer. "command"
** is the command which should be run (via "su_popen()") if a
** job is printed on this printer. The following tokens may be
** embedded in the command, and are substituted as follows:
**
** $FILE - path to the file containing the print data
** $USER - login of user
** $HOST - hostname from which job originated
**
** Tokens may occur multiple times. If The command includes no
** $FILE token, the string " $FILE" is silently appended.
**
** pr_list list_virtual_printers()
**
** This is invoked from build_pr_list to generate a list of aliased
** printers, so that the client that asks for a list of valid printers
** will see these ones.
**
** char *map_printer_name(char *printer)
**
** If "printer" identifies an aliased printer, this function returns
** the "alias_for" name, or NULL if the "alias_for" was given as "-".
** Otherwise it returns its argument.
**
** char *expand_alias(char *printer, char *file, char *user, char *host)
**
** If "printer" is an aliased printer, this function returns a
** pointer to a static string in which the corresponding command
** has been expanded. Otherwise ot returns NULL.
*/
#define NPRINTERDEFS 16
1997-10-25 17:45:55 +04:00
int num_aliases = 0;
1995-07-24 08:02:44 +04:00
struct {
1997-10-25 17:45:55 +04:00
char *a_printer;
char *a_alias_for;
char *a_command;
} alias[NPRINTERDEFS];
1995-07-24 08:02:44 +04:00
void
add_printer_alias(printer, alias_for, command)
1997-10-25 17:45:55 +04:00
char *printer;
char *alias_for;
char *command;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
if (num_aliases < NPRINTERDEFS) {
1995-07-24 08:02:44 +04:00
alias[num_aliases].a_printer = strdup(printer);
alias[num_aliases].a_alias_for =
1997-10-25 17:45:55 +04:00
(strcmp(alias_for, "-") ? strdup(alias_for) : NULL);
if (strstr(command, "$FILE"))
1995-07-24 08:02:44 +04:00
alias[num_aliases].a_command = strdup(command);
else {
1997-10-25 17:45:55 +04:00
alias[num_aliases].a_command = (char *) grab(strlen(command) + 8);
1995-07-24 08:02:44 +04:00
strcpy(alias[num_aliases].a_command, command);
strcat(alias[num_aliases].a_command, " $FILE");
}
num_aliases++;
}
}
1997-10-25 17:45:55 +04:00
pr_list
list_virtual_printers()
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
pr_list first = NULL;
pr_list last = NULL;
pr_list curr = NULL;
int i;
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
if (num_aliases == 0)
return (NULL);
1995-07-24 08:02:44 +04:00
for (i = 0; i < num_aliases; i++) {
curr = (struct pr_list_item *)
1997-10-25 17:45:55 +04:00
grab(sizeof(struct pr_list_item));
1995-07-24 08:02:44 +04:00
curr->pn = strdup(alias[i].a_printer);
1997-10-25 17:45:55 +04:00
if (alias[i].a_alias_for == NULL)
1995-07-24 08:02:44 +04:00
curr->device = strdup("");
else
curr->device = strdup(alias[i].a_alias_for);
curr->remhost = strdup("");
curr->cm = strdup("(alias)");
curr->pr_next = NULL;
1997-10-25 17:45:55 +04:00
if (last == NULL)
1995-07-24 08:02:44 +04:00
first = curr;
else
last->pr_next = curr;
last = curr;
}
1997-10-25 17:45:55 +04:00
return (first);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
char *
1995-07-24 08:02:44 +04:00
map_printer_name(printer)
1997-10-25 17:45:55 +04:00
char *printer;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
int i;
for (i = 0; i < num_aliases; i++) {
if (!strcmp(printer, alias[i].a_printer))
return (alias[i].a_alias_for);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
return (printer);
1995-07-24 08:02:44 +04:00
}
1997-10-25 17:45:55 +04:00
void
1995-07-24 08:02:44 +04:00
substitute(string, token, data)
1997-10-25 17:45:55 +04:00
char *string;
char *token;
char *data;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
char temp[512];
char *c;
1995-07-24 08:02:44 +04:00
1997-10-25 17:45:55 +04:00
while ((c = strstr(string, token)) != NULL) {
1995-07-24 08:02:44 +04:00
*c = '\0';
strcpy(temp, string);
strcat(temp, data);
c += strlen(token);
strcat(temp, c);
strcpy(string, temp);
}
}
1997-10-25 17:45:55 +04:00
char *
1995-07-24 08:02:44 +04:00
expand_alias(printer, file, user, host)
1997-10-25 17:45:55 +04:00
char *printer;
char *file;
char *user;
char *host;
1995-07-24 08:02:44 +04:00
{
1997-10-25 17:45:55 +04:00
static char expansion[512];
int i;
for (i = 0; i < num_aliases; i++) {
if (!strcmp(printer, alias[i].a_printer)) {
1995-07-24 08:02:44 +04:00
strcpy(expansion, alias[i].a_command);
substitute(expansion, "$FILE", file);
substitute(expansion, "$USER", user);
substitute(expansion, "$HOST", host);
1997-10-25 17:45:55 +04:00
return (expansion);
1995-07-24 08:02:44 +04:00
}
}
1997-10-25 17:45:55 +04:00
return (NULL);
1995-07-24 08:02:44 +04:00
}