Home | History | Annotate | Line # | Download | only in rpc.pcnfsd
pcnfsd_print.c revision 1.3
      1 /*	$NetBSD: pcnfsd_print.c,v 1.3 1995/08/14 19:45:18 gwr Exp $	*/
      2 
      3 /* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_print.c 1.7 92/01/24 19:58:58 SMI */
      4 /*
      5 **=====================================================================
      6 ** Copyright (c) 1986,1987,1988,1989,1990,1991 by Sun Microsystems, Inc.
      7 **	@(#)pcnfsd_print.c	1.7	1/24/92
      8 **=====================================================================
      9 */
     10 #include "common.h"
     11 /*
     12 **=====================================================================
     13 **             I N C L U D E   F I L E   S E C T I O N                *
     14 **                                                                    *
     15 ** If your port requires different include files, add a suitable      *
     16 ** #define in the customization section, and make the inclusion or    *
     17 ** exclusion of the files conditional on this.                        *
     18 **=====================================================================
     19 */
     20 #include "pcnfsd.h"
     21 #include <malloc.h>
     22 #include <stdio.h>
     23 #include <pwd.h>
     24 #include <sys/file.h>
     25 #include <signal.h>
     26 #include <sys/stat.h>
     27 #include <sys/ioctl.h>
     28 #include <netdb.h>
     29 #include <errno.h>
     30 #include <string.h>
     31 
     32 #ifndef SYSV
     33 #include <sys/wait.h>
     34 #endif
     35 
     36 #ifdef ISC_2_0
     37 #include <sys/fcntl.h>
     38 #endif
     39 
     40 #ifdef SHADOW_SUPPORT
     41 #include <shadow.h>
     42 #endif
     43 
     44 #include "paths.h"
     45 
     46 /*
     47 **---------------------------------------------------------------------
     48 ** Other #define's
     49 **---------------------------------------------------------------------
     50 */
     51 #ifndef MAXPATHLEN
     52 #define MAXPATHLEN 1024
     53 #endif
     54 
     55 /*
     56 ** The following defintions give the maximum time allowed for
     57 ** an external command to run (in seconds)
     58 */
     59 #define MAXTIME_FOR_PRINT	10
     60 #define MAXTIME_FOR_QUEUE	10
     61 #define MAXTIME_FOR_CANCEL	10
     62 #define MAXTIME_FOR_STATUS	10
     63 
     64 #define QMAX 50
     65 
     66 /*
     67 ** The following is derived from ucb/lpd/displayq.c
     68 */
     69 #define SIZECOL 62
     70 #define FILECOL 24
     71 
     72 extern void     scramble();
     73 extern void     run_ps630();
     74 extern char    *crypt();
     75 extern FILE    *su_popen();
     76 extern int      su_pclose();
     77 int             build_pr_list();
     78 char 	       *map_printer_name();
     79 char	       *expand_alias();
     80 void           *grab();
     81 void            free_pr_list_item();
     82 void            free_pr_queue_item();
     83 pr_list		list_virtual_printers();
     84 
     85 /*
     86 **---------------------------------------------------------------------
     87 **                       Misc. variable definitions
     88 **---------------------------------------------------------------------
     89 */
     90 
     91 extern int      errno;
     92 extern int	interrupted;	/* in pcnfsd_misc.c */
     93 struct stat     statbuf;
     94 char            pathname[MAXPATHLEN];
     95 char            new_pathname[MAXPATHLEN];
     96 char            sp_name[MAXPATHLEN] = SPOOLDIR;
     97 char            tempstr[256];
     98 char            delims[] = " \t\r\n:()";
     99 
    100 pr_list         printers = NULL;
    101 pr_queue        queue = NULL;
    102 
    103 /*
    104 **=====================================================================
    105 **                      C O D E   S E C T I O N                       *
    106 **=====================================================================
    107 */
    108 
    109 /*
    110  * This is the latest word on the security check. The following
    111  * routine "suspicious()" returns non-zero if the character string
    112  * passed to it contains any shell metacharacters.
    113  * Callers will typically code
    114  *
    115  *	if(suspicious(some_parameter)) reject();
    116  */
    117 
    118 int suspicious (s)
    119 char *s;
    120 {
    121 	if(strpbrk(pathname, ";|&<>`'#!?*()[]^") != NULL)
    122 		return 1;
    123 	return 0;
    124 }
    125 
    126 
    127 int
    128 valid_pr(pr)
    129 char *pr;
    130 {
    131 char *p;
    132 pr_list curr;
    133 	if(printers == NULL)
    134 		build_pr_list();
    135 
    136 	if(printers == NULL)
    137 		return(1); /* can't tell - assume it's good */
    138 
    139 	p = map_printer_name(pr);
    140 	if (p == NULL)
    141 		return(1);	/* must be ok is maps to NULL! */
    142 	curr = printers;
    143 	while(curr) {
    144 		if(!strcmp(p, curr->pn))
    145 			return(1);
    146 		curr = curr->pr_next;
    147 	}
    148 
    149 	return(0);
    150 }
    151 
    152 pirstat pr_init(sys, pr, sp)
    153 char *sys;
    154 char *pr;
    155 char**sp;
    156 {
    157 int    dir_mode = 0777;
    158 int rc;
    159 
    160 	*sp = &pathname[0];
    161 	pathname[0] = '\0';
    162 
    163 	if(suspicious(sys) || suspicious(pr))
    164 		return(PI_RES_FAIL);
    165 
    166 	/* get pathname of current directory and return to client */
    167 
    168 	(void)sprintf(pathname,"%s/%s",sp_name, sys);
    169 	(void)mkdir(sp_name, dir_mode);	/* ignore the return code */
    170 	(void)chmod(sp_name, dir_mode);
    171 	rc = mkdir(pathname, dir_mode);	/* DON'T ignore this return code */
    172 	if((rc < 0 && errno != EEXIST) ||
    173 	   (chmod(pathname, dir_mode) != 0) ||
    174 	   (stat(pathname, &statbuf) != 0) ||
    175 	   !(statbuf.st_mode & S_IFDIR)) {
    176 	   (void)sprintf(tempstr,
    177 		         "rpc.pcnfsd: unable to set up spool directory %s\n",
    178 		 	  pathname);
    179             msg_out(tempstr);
    180 	    pathname[0] = '\0';	/* null to tell client bad vibes */
    181 	    return(PI_RES_FAIL);
    182 	    }
    183  	if (!valid_pr(pr))
    184            {
    185 	    pathname[0] = '\0';	/* null to tell client bad vibes */
    186 	    return(PI_RES_NO_SUCH_PRINTER);
    187 	    }
    188 	return(PI_RES_OK);
    189 
    190 }
    191 
    192 
    194 psrstat pr_start2(system, pr, user, fname, opts, id)
    195 char *system;
    196 char *pr;
    197 char *user;
    198 char *fname;
    199 char *opts;
    200 char **id;
    201 {
    202 char            snum[20];
    203 static char     req_id[256];
    204 char            cmdbuf[256];
    205 char            resbuf[256];
    206 FILE *fd;
    207 int i;
    208 char *xcmd;
    209 char *cp;
    210 int failed = 0;
    211 
    212 #ifdef HACK_FOR_ROTATED_TRANSCRIPT
    213 char            scratch[512];
    214 #endif
    215 
    216 
    217 	if(suspicious(system) ||
    218 		suspicious(pr) ||
    219 		suspicious(user) ||
    220 		suspicious(fname))
    221 		return(PS_RES_FAIL);
    222 
    223 	(void)sprintf(pathname,"%s/%s/%s",sp_name,
    224 	                         system,
    225 	                         fname);
    226 
    227 	*id = &req_id[0];
    228 	req_id[0] = '\0';
    229 
    230 	if (stat(pathname, &statbuf))
    231            {
    232 	   /*
    233            **-----------------------------------------------------------------
    234 	   ** We can't stat the file. Let's try appending '.spl' and
    235 	   ** see if it's already in progress.
    236            **-----------------------------------------------------------------
    237 	   */
    238 
    239 	   (void)strcat(pathname, ".spl");
    240 	   if (stat(pathname, &statbuf))
    241 	      {
    242 	      /*
    243               **----------------------------------------------------------------
    244 	      ** It really doesn't exist.
    245               **----------------------------------------------------------------
    246 	      */
    247 
    248 
    249 	      return(PS_RES_NO_FILE);
    250 	      }
    251 	      /*
    252               **-------------------------------------------------------------
    253 	      ** It is already on the way.
    254               **-------------------------------------------------------------
    255 	      */
    256 
    257 
    258 		return(PS_RES_ALREADY);
    259 	     }
    260 
    261 	if (statbuf.st_size == 0)
    262 	   {
    263 	   /*
    264            **-------------------------------------------------------------
    265 	   ** Null file - don't print it, just kill it.
    266            **-------------------------------------------------------------
    267 	   */
    268 	   (void)unlink(pathname);
    269 
    270 	    return(PS_RES_NULL);
    271 	    }
    272 	 /*
    273          **-------------------------------------------------------------
    274 	 ** The file is real, has some data, and is not already going out.
    275 	 ** We rename it by appending '.spl' and exec "lpr" to do the
    276 	 ** actual work.
    277          **-------------------------------------------------------------
    278 	 */
    279 	(void)strcpy(new_pathname, pathname);
    280 	(void)strcat(new_pathname, ".spl");
    281 
    282 	/*
    283         **-------------------------------------------------------------
    284 	** See if the new filename exists so as not to overwrite it.
    285         **-------------------------------------------------------------
    286 	*/
    287 
    288 
    289 	if (!stat(new_pathname, &statbuf))
    290 	   {
    291 	   (void)strcpy(new_pathname, pathname);  /* rebuild a new name */
    292 	   (void)sprintf(snum, "%d", rand());	  /* get some number */
    293 	   (void)strncat(new_pathname, snum, 3);
    294 	   (void)strcat(new_pathname, ".spl");	  /* new spool file */
    295 	    }
    296 	if (rename(pathname, new_pathname))
    297 	   {
    298 	   /*
    299            **---------------------------------------------------------------
    300 	   ** Should never happen.
    301            **---------------------------------------------------------------
    302            */
    303 	   (void)sprintf(tempstr, "rpc.pcnfsd: spool file rename (%s->%s) failed.\n",
    304 			pathname, new_pathname);
    305                 msg_out(tempstr);
    306 		return(PS_RES_FAIL);
    307 	    }
    308 
    309 		if (*opts == 'd')
    310 	           {
    311 		   /*
    312                    **------------------------------------------------------
    313 		   ** This is a Diablo print stream. Apply the ps630
    314 		   ** filter with the appropriate arguments.
    315                    **------------------------------------------------------
    316 		   */
    317 		   (void)run_ps630(new_pathname, opts);
    318 		   }
    319 		/*
    320 		** Try to match to an aliased printer
    321 		*/
    322 		xcmd = expand_alias(pr, new_pathname, user, system);
    323 		if(!xcmd) {
    324 #ifdef	SVR4
    325 	        /*
    326 			 * Use the copy option so we can remove the orignal
    327 			 * spooled nfs file from the spool directory.
    328 			 */
    329 			sprintf(cmdbuf, "/usr/bin/lp -c -d%s %s",
    330 				pr, new_pathname);
    331 #else	/* SVR4 */
    332 			/* BSD way: lpr */
    333 			sprintf(cmdbuf, "%s/lpr -P%s %s",
    334 				LPRDIR, pr, new_pathname);
    335 #endif	/* SVR4 */
    336 			xcmd = cmdbuf;
    337 		}
    338 		if ((fd = su_popen(user, xcmd, MAXTIME_FOR_PRINT)) == NULL) {
    339 			msg_out("rpc.pcnfsd: su_popen failed");
    340 			return(PS_RES_FAIL);
    341 		}
    342 		req_id[0] = '\0';	/* asume failure */
    343 		while(fgets(resbuf, 255, fd) != NULL) {
    344 			i = strlen(resbuf);
    345 			if(i)
    346 				resbuf[i-1] = '\0'; /* trim NL */
    347 			if(!strncmp(resbuf, "request id is ", 14))
    348 				/* New - just the first word is needed */
    349 				strcpy(req_id, strtok(&resbuf[14], delims));
    350 			else if (strembedded("disabled", resbuf))
    351 				failed = 1;
    352 		}
    353 		if(su_pclose(fd) == 255)
    354 			msg_out("rpc.pcnfsd: su_pclose alert");
    355 		(void)unlink(new_pathname);
    356 		return((failed | interrupted)? PS_RES_FAIL : PS_RES_OK);
    357 }
    358 
    359 
    361 /*
    362  * build_pr_list: determine which printers are valid.
    363  * on SVR4 use "lpstat -v"
    364  * on BSD use "lpc status"
    365  */
    366 
    367 #ifdef	SVR4
    368 /*
    369  * In SVR4 the command to determine which printers are
    370  * valid is lpstat -v. The output is something like this:
    371  *
    372  * device for lp: /dev/lp0
    373  * system for pcdslw: hinode
    374  * system for bletch: hinode (as printer hisname)
    375  *
    376  * On SunOS using the SysV compatibility package, the output
    377  * is more like:
    378  *
    379  * device for lp is /dev/lp0
    380  * device for pcdslw is the remote printer pcdslw on hinode
    381  * device for bletch is the remote printer hisname on hinode
    382  *
    383  * It is fairly simple to create logic that will handle either
    384  * possibility:
    385  */
    386 int
    387 build_pr_list()
    388 {
    389 	pr_list last = NULL;
    390 	pr_list curr = NULL;
    391 	char buff[256];
    392 	FILE *p;
    393 	char *cp;
    394 	int saw_system;
    395 
    396 	p = popen("lpstat -v", "r");
    397 	if(p == NULL) {
    398 		msg_out("rpc.pcnfsd: unable to popen() lp status");
    399 		return(0);
    400 	}
    401 
    402 	while(fgets(buff, 255, p) != NULL) {
    403 		cp = strtok(buff, delims);
    404 		if(!cp)
    405 			continue;
    406 		if(!strcmp(cp, "device"))
    407 			saw_system = 0;
    408 		else if (!strcmp(cp, "system"))
    409 			saw_system = 1;
    410 		else
    411 			continue;
    412 		cp = strtok(NULL, delims);
    413 		if(!cp || strcmp(cp, "for"))
    414 			continue;
    415 		cp = strtok(NULL, delims);
    416 		if(!cp)
    417 			continue;
    418 		curr = (struct pr_list_item *)
    419 			grab(sizeof (struct pr_list_item));
    420 
    421 		curr->pn = strdup(cp);
    422 		curr->device = NULL;
    423 		curr->remhost = NULL;
    424 		curr->cm = strdup("-");
    425 		curr->pr_next = NULL;
    426 
    427 		cp = strtok(NULL, delims);
    428 
    429 		if(cp && !strcmp(cp, "is"))
    430 			cp = strtok(NULL, delims);
    431 
    432 		if(!cp) {
    433 			free_pr_list_item(curr);
    434 			continue;
    435 		}
    436 
    437 		if(saw_system) {
    438 			/* "system" OR "system (as printer pname)" */
    439 			curr->remhost = strdup(cp);
    440 			cp = strtok(NULL, delims);
    441 			if(!cp) {
    442 				/* simple format */
    443 				curr->device = strdup(curr->pn);
    444 			} else {
    445 				/* "sys (as printer pname)" */
    446 				if (strcmp(cp, "as")) {
    447 					free_pr_list_item(curr);
    448 					continue;
    449 				}
    450 				cp = strtok(NULL, delims);
    451 				if (!cp || strcmp(cp, "printer")) {
    452 					free_pr_list_item(curr);
    453 					continue;
    454 				}
    455 				cp = strtok(NULL, delims);
    456 				if(!cp) {
    457 					free_pr_list_item(curr);
    458 					continue;
    459 				}
    460 				curr->device = strdup(cp);
    461 			}
    462 		}
    463 		else if(!strcmp(cp, "the")) {
    464 			/* start of "the remote printer foo on bar" */
    465 			cp = strtok(NULL, delims);
    466 			if(!cp || strcmp(cp, "remote")) {
    467 				free_pr_list_item(curr);
    468 				continue;
    469 			}
    470 			cp = strtok(NULL, delims);
    471 			if(!cp || strcmp(cp, "printer")) {
    472 				free_pr_list_item(curr);
    473 				continue;
    474 			}
    475 			cp = strtok(NULL, delims);
    476 			if(!cp) {
    477 				free_pr_list_item(curr);
    478 				continue;
    479 			}
    480 			curr->device = strdup(cp);
    481 			cp = strtok(NULL, delims);
    482 			if(!cp || strcmp(cp, "on")) {
    483 				free_pr_list_item(curr);
    484 				continue;
    485 			}
    486 			cp = strtok(NULL, delims);
    487 			if(!cp) {
    488 				free_pr_list_item(curr);
    489 				continue;
    490 			}
    491 			curr->remhost = strdup(cp);
    492 		} else {
    493 			/* the local name */
    494 			curr->device = strdup(cp);
    495 			curr->remhost = strdup("");
    496 		}
    497 
    498 		if(last == NULL)
    499 			printers = curr;
    500 		else
    501 			last->pr_next = curr;
    502 		last = curr;
    503 
    504 	}
    505 	(void) pclose(p);
    506 
    507 	/*
    508 	 ** Now add on the virtual printers, if any
    509 	 */
    510 	if(last == NULL)
    511 		printers = list_virtual_printers();
    512 	else
    513 		last->pr_next = list_virtual_printers();
    514 
    515 	return(1);
    516 }
    517 
    518 #else	/* SVR4 */
    519 
    520 /*
    521  * BSD way: lpc stat
    522  */
    523 int
    524 build_pr_list()
    525 {
    526 	pr_list last = NULL;
    527 	pr_list curr = NULL;
    528 	char buff[256];
    529 	FILE *p;
    530 	char *cp;
    531 	int saw_system;
    532 
    533 	sprintf(buff, "%s/lpc status", LPCDIR);
    534 	p = popen(buff, "r");
    535 	if(p == NULL) {
    536 		msg_out("rpc.pcnfsd: unable to popen lpc stat");
    537 		return(0);
    538 	}
    539 
    540 	while(fgets(buff, 255, p) != NULL) {
    541 		if (isspace(buff[0]))
    542 			continue;
    543 
    544 		if ((cp = strtok(buff, delims)) == NULL)
    545 			continue;
    546 
    547 		curr = (struct pr_list_item *)
    548 			grab(sizeof (struct pr_list_item));
    549 
    550 		/* XXX - Should distinguish remote printers. */
    551 		curr->pn = strdup(cp);
    552 		curr->device = strdup(cp);
    553 		curr->remhost = strdup("");
    554 		curr->cm = strdup("-");
    555 		curr->pr_next = NULL;
    556 
    557 		if(last == NULL)
    558 			printers = curr;
    559 		else
    560 			last->pr_next = curr;
    561 		last = curr;
    562 
    563 	}
    564 	(void) fclose(p);
    565 
    566 	/*
    567 	 ** Now add on the virtual printers, if any
    568 	 */
    569 	if(last == NULL)
    570 		printers = list_virtual_printers();
    571 	else
    572 		last->pr_next = list_virtual_printers();
    573 
    574 	return(1);
    575 }
    576 
    577 #endif	/* SVR4 */
    578 
    579 void *grab(n)
    580 int n;
    581 {
    582 	void *p;
    583 
    584 	p = (void *)malloc(n);
    585 	if(p == NULL) {
    586 		msg_out("rpc.pcnfsd: malloc failure");
    587 		exit(1);
    588 	}
    589 	return(p);
    590 }
    591 
    592 void
    593 free_pr_list_item(curr)
    594 pr_list curr;
    595 {
    596 	if(curr->pn)
    597 		free(curr->pn);
    598 	if(curr->device)
    599 		free(curr->device);
    600 	if(curr->remhost)
    601 		free(curr->remhost);
    602 	if(curr->cm)
    603 		free(curr->cm);
    604 	if(curr->pr_next)
    605 		free_pr_list_item(curr->pr_next); /* recurse */
    606 	free(curr);
    607 }
    608 
    609 /*
    610  * build_pr_queue:  used to show the print queue.
    611  *
    612  * Note that the first thing we do is to discard any
    613  * existing queue.
    614  */
    615 #ifdef SVR4
    616 
    617 /*
    618 ** In SVR4 the command to list the print jobs for printer
    619 ** lp is "lpstat lp" (or, equivalently, "lpstat -p lp").
    620 ** The output looks like this:
    621 **
    622 ** lp-2                    root               939   Jul 10 21:56
    623 ** lp-5                    geoff               15   Jul 12 23:23
    624 ** lp-6                    geoff               15   Jul 12 23:23
    625 **
    626 ** If the first job is actually printing the first line
    627 ** is modified, as follows:
    628 **
    629 ** lp-2                    root               939   Jul 10 21:56 on lp
    630 **
    631 ** I don't yet have any info on what it looks like if the printer
    632 ** is remote and we're spooling over the net. However for
    633 ** the purposes of rpc.pcnfsd we can simply say that field 1 is the
    634 ** job ID, field 2 is the submitter, and field 3 is the size.
    635 ** We can check for the presence of the string " on " in the
    636 ** first record to determine if we should count it as rank 0 or rank 1,
    637 ** but it won't hurt if we get it wrong.
    638 **/
    639 
    640 pirstat
    641 build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
    642 printername     pn;
    643 username        user;
    644 int            just_mine;
    645 int            *p_qlen;
    646 int            *p_qshown;
    647 {
    648 pr_queue last = NULL;
    649 pr_queue curr = NULL;
    650 char buff[256];
    651 FILE *p;
    652 char *owner;
    653 char *job;
    654 char *totsize;
    655 
    656 	if(queue) {
    657 		free_pr_queue_item(queue);
    658 		queue = NULL;
    659 	}
    660 	*p_qlen = 0;
    661 	*p_qshown = 0;
    662 
    663 	pn = map_printer_name(pn);
    664 	if(pn == NULL || !valid_pr(pn) || suspicious(pn))
    665 		return(PI_RES_NO_SUCH_PRINTER);
    666 
    667 	sprintf(buff, "/usr/bin/lpstat %s", pn);
    668 	p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
    669 	if(p == NULL) {
    670 		msg_out("rpc.pcnfsd: unable to popen() lpstat queue query");
    671 		return(PI_RES_FAIL);
    672 	}
    673 
    674 	while(fgets(buff, 255, p) != NULL) {
    675 		job = strtok(buff, delims);
    676 		if(!job)
    677 			continue;
    678 
    679 		owner = strtok(NULL, delims);
    680 		if(!owner)
    681 			continue;
    682 
    683 		totsize = strtok(NULL, delims);
    684 		if(!totsize)
    685 			continue;
    686 
    687 		*p_qlen += 1;
    688 
    689 		if(*p_qshown > QMAX)
    690 			continue;
    691 
    692 		if(just_mine && mystrcasecmp(owner, user))
    693 			continue;
    694 
    695 		*p_qshown += 1;
    696 
    697 		curr = (struct pr_queue_item *)
    698 			grab(sizeof (struct pr_queue_item));
    699 
    700 		curr->position = *p_qlen;
    701 		curr->id = strdup(job);
    702 		curr->size = strdup(totsize);
    703 		curr->status = strdup("");
    704 		curr->system = strdup("");
    705 		curr->user = strdup(owner);
    706 		curr->file = strdup("");
    707 		curr->cm = strdup("-");
    708 		curr->pr_next = NULL;
    709 
    710 		if(last == NULL)
    711 			queue = curr;
    712 		else
    713 			last->pr_next = curr;
    714 		last = curr;
    715 
    716 	}
    717 	(void) su_pclose(p);
    718 	return(PI_RES_OK);
    719 }
    720 
    721 #else /* SVR4 */
    722 
    723 pirstat
    724 build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
    725 printername     pn;
    726 username        user;
    727 int            just_mine;
    728 int            *p_qlen;
    729 int            *p_qshown;
    730 {
    731 pr_queue last = NULL;
    732 pr_queue curr = NULL;
    733 char buff[256];
    734 FILE *p;
    735 char *cp;
    736 int i;
    737 char *rank;
    738 char *owner;
    739 char *job;
    740 char *files;
    741 char *totsize;
    742 
    743 	if(queue) {
    744 		free_pr_queue_item(queue);
    745 		queue = NULL;
    746 	}
    747 	*p_qlen = 0;
    748 	*p_qshown = 0;
    749 	pn = map_printer_name(pn);
    750 	if(pn == NULL || suspicious(pn))
    751 		return(PI_RES_NO_SUCH_PRINTER);
    752 
    753 	sprintf(buff, "%s/lpq -P%s", LPRDIR, pn);
    754 
    755 	p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
    756 	if(p == NULL) {
    757 		msg_out("rpc.pcnfsd: unable to popen() lpq");
    758 		return(PI_RES_FAIL);
    759 	}
    760 
    761 	while(fgets(buff, 255, p) != NULL) {
    762 		i = strlen(buff) - 1;
    763 		buff[i] = '\0';		/* zap trailing NL */
    764 		if(i < SIZECOL)
    765 			continue;
    766 		if(!mystrncasecmp(buff, "rank", 4))
    767 			continue;
    768 
    769 		totsize = &buff[SIZECOL-1];
    770 		files = &buff[FILECOL-1];
    771 		cp = totsize;
    772 		cp--;
    773 		while(cp > files && isspace(*cp))
    774 			*cp-- = '\0';
    775 
    776 		buff[FILECOL-2] = '\0';
    777 
    778 		cp = strtok(buff, delims);
    779 		if(!cp)
    780 			continue;
    781 		rank = cp;
    782 
    783 		cp = strtok(NULL, delims);
    784 		if(!cp)
    785 			continue;
    786 		owner = cp;
    787 
    788 		cp = strtok(NULL, delims);
    789 		if(!cp)
    790 			continue;
    791 		job = cp;
    792 
    793 		*p_qlen += 1;
    794 
    795 		if(*p_qshown > QMAX)
    796 			continue;
    797 
    798 		if(just_mine && mystrcasecmp(owner, user))
    799 			continue;
    800 
    801 		*p_qshown += 1;
    802 
    803 		curr = (struct pr_queue_item *)
    804 			grab(sizeof (struct pr_queue_item));
    805 
    806 		curr->position = atoi(rank); /* active -> 0 */
    807 		curr->id = strdup(job);
    808 		curr->size = strdup(totsize);
    809 		curr->status = strdup(rank);
    810 		curr->system = strdup("");
    811 		curr->user = strdup(owner);
    812 		curr->file = strdup(files);
    813 		curr->cm = strdup("-");
    814 		curr->pr_next = NULL;
    815 
    816 		if(last == NULL)
    817 			queue = curr;
    818 		else
    819 			last->pr_next = curr;
    820 		last = curr;
    821 
    822 	}
    823 	(void) su_pclose(p);
    824 	return(PI_RES_OK);
    825 }
    826 
    827 #endif /* SVR4 */
    828 
    829 void
    830 free_pr_queue_item(curr)
    831 pr_queue curr;
    832 {
    833 	if(curr->id)
    834 		free(curr->id);
    835 	if(curr->size)
    836 		free(curr->size);
    837 	if(curr->status)
    838 		free(curr->status);
    839 	if(curr->system)
    840 		free(curr->system);
    841 	if(curr->user)
    842 		free(curr->user);
    843 	if(curr->file)
    844 		free(curr->file);
    845 	if(curr->cm)
    846 		free(curr->cm);
    847 	if(curr->pr_next)
    848 		free_pr_queue_item(curr->pr_next); /* recurse */
    849 	free(curr);
    850 }
    851 
    852 #ifdef SVR4
    853 
    854 /*
    855 ** New - SVR4 printer status handling.
    856 **
    857 ** The command we'll use for checking the status of printer "lp"
    858 ** is "lpstat -a lp -p lp". Here are some sample outputs:
    859 **
    860 **
    861 ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
    862 ** printer lp disabled since Thu Feb 21 22:52:36 EST 1991. available.
    863 ** 	new printer
    864 ** ---
    865 ** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
    866 ** 	unknown reason
    867 ** printer pcdslw disabled since Fri Jul 12 22:15:37 EDT 1991. available.
    868 ** 	new printer
    869 ** ---
    870 ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
    871 ** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
    872 ** ---
    873 ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
    874 ** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
    875 ** ---
    876 ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
    877 ** printer lp disabled since Sat Jul 13 12:05:20 EDT 1991. available.
    878 ** 	unknown reason
    879 ** ---
    880 ** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
    881 ** 	unknown reason
    882 ** printer pcdslw is idle. enabled since Sat Jul 13 12:05:28 EDT 1991. available.
    883 **
    884 ** Note that these are actual outputs. The format (which is totally
    885 ** different from the lpstat in SunOS) seems to break down as
    886 ** follows:
    887 ** (1) The first line has the form "printername [not] accepting requests,,,"
    888 **    This is trivial to decode.
    889 ** (2) The second line has several forms, all beginning "printer printername":
    890 ** (2.1) "... disabled"
    891 ** (2.2) "... is idle"
    892 ** (2.3) "... now printing jobid"
    893 ** The "available" comment seems to be meaningless. The next line
    894 ** is the "reason" code which the operator can supply when issuing
    895 ** a "disable" or "reject" command.
    896 ** Note that there is no way to check the number of entries in the
    897 ** queue except to ask for the queue and count them.
    898 */
    899 
    900 pirstat
    901 get_pr_status(pn, avail, printing, qlen, needs_operator, status)
    902 printername   pn;
    903 bool_t       *avail;
    904 bool_t       *printing;
    905 int          *qlen;
    906 bool_t       *needs_operator;
    907 char         *status;
    908 {
    909 char buff[256];
    910 char cmd[64];
    911 FILE *p;
    912 int n;
    913 pirstat stat = PI_RES_NO_SUCH_PRINTER;
    914 
    915 	/* assume the worst */
    916 	*avail = FALSE;
    917 	*printing = FALSE;
    918 	*needs_operator = FALSE;
    919 	*qlen = 0;
    920 	*status = '\0';
    921 
    922 	pn = map_printer_name(pn);
    923 	if(pn == NULL || !valid_pr(pn) || suspicious(pn))
    924 		return(PI_RES_NO_SUCH_PRINTER);
    925 	n = strlen(pn);
    926 
    927 	sprintf(cmd, "/usr/bin/lpstat -a %s -p %s", pn, pn);
    928 
    929 	p = popen(cmd, "r");
    930 	if(p == NULL) {
    931 		msg_out("rpc.pcnfsd: unable to popen() lp status");
    932 		return(PI_RES_FAIL);
    933 	}
    934 
    935 	stat = PI_RES_OK;
    936 
    937 	while(fgets(buff, 255, p) != NULL) {
    938 		if(!strncmp(buff, pn, n)) {
    939 			if(!strstr(buff, "not accepting"))
    940 			*avail = TRUE;
    941 			continue;
    942 		}
    943 		if(!strncmp(buff, "printer ", 8)) {
    944 			if(!strstr(buff, "disabled"))
    945 				*printing = TRUE;
    946 			if(strstr(buff, "printing"))
    947 				strcpy(status, "printing");
    948 			else if (strstr(buff, "idle"))
    949 				strcpy(status, "idle");
    950 			continue;
    951 		}
    952 		if(!strncmp(buff, "UX:", 3)) {
    953 			stat = PI_RES_NO_SUCH_PRINTER;
    954 		}
    955 	}
    956 	(void) pclose(p);
    957 	return(stat);
    958 }
    959 
    960 #else /* SVR4 */
    961 
    962 /*
    963  * BSD way: lpc status
    964  */
    965 pirstat
    966 get_pr_status(pn, avail, printing, qlen, needs_operator, status)
    967 printername   pn;
    968 bool_t       *avail;
    969 bool_t       *printing;
    970 int          *qlen;
    971 bool_t       *needs_operator;
    972 char         *status;
    973 {
    974 	char cmd[128];
    975 	char buff[256];
    976 	char buff2[256];
    977 	char pname[64];
    978 	FILE *p;
    979 	char *cp;
    980 	char *cp1;
    981 	char *cp2;
    982 	int n;
    983 	pirstat stat = PI_RES_NO_SUCH_PRINTER;
    984 
    985 	/* assume the worst */
    986 	*avail = FALSE;
    987 	*printing = FALSE;
    988 	*needs_operator = FALSE;
    989 	*qlen = 0;
    990 	*status = '\0';
    991 
    992 	pn = map_printer_name(pn);
    993 	if(pn == NULL || suspicious(pn))
    994 		return(PI_RES_NO_SUCH_PRINTER);
    995 
    996 	sprintf(pname, "%s:", pn);
    997 	n = strlen(pname);
    998 
    999 	sprintf(cmd, "%s/lpc status %s", LPCDIR, pn);
   1000 	p = popen(cmd, "r");
   1001 	if(p == NULL) {
   1002 		msg_out("rpc.pcnfsd: unable to popen() lp status");
   1003 		return(PI_RES_FAIL);
   1004 	}
   1005 
   1006 	while(fgets(buff, 255, p) != NULL) {
   1007 		if(strncmp(buff, pname, n))
   1008 			continue;
   1009 /*
   1010 ** We have a match. The only failure now is PI_RES_FAIL if
   1011 ** lpstat output cannot be decoded
   1012 */
   1013 		stat = PI_RES_FAIL;
   1014 /*
   1015 ** The next four lines are usually if the form
   1016 **
   1017 **     queuing is [enabled|disabled]
   1018 **     printing is [enabled|disabled]
   1019 **     [no entries | N entr[y|ies] in spool area]
   1020 **     <status message, may include the word "attention">
   1021 */
   1022 		while(fgets(buff, 255, p) != NULL && isspace(buff[0])) {
   1023 			cp = buff;
   1024 			while(isspace(*cp))
   1025 				cp++;
   1026 			if(*cp == '\0')
   1027 				break;
   1028 			cp1 = cp;
   1029 			cp2 = buff2;
   1030 			while(*cp1 && *cp1 != '\n') {
   1031 				*cp2++ = tolower(*cp1);
   1032 				cp1++;
   1033 			}
   1034 			*cp1 = '\0';
   1035 			*cp2 = '\0';
   1036 /*
   1037 ** Now buff2 has a lower-cased copy and cp points at the original;
   1038 ** both are null terminated without any newline
   1039 */
   1040 			if(!strncmp(buff2, "queuing", 7)) {
   1041 				*avail = (strstr(buff2, "enabled") != NULL);
   1042 				continue;
   1043 			}
   1044 			if(!strncmp(buff2, "printing", 8)) {
   1045 				*printing = (strstr(buff2, "enabled") != NULL);
   1046 				continue;
   1047 			}
   1048 			if(isdigit(buff2[0]) && (strstr(buff2, "entr") !=NULL)) {
   1049 
   1050 				*qlen = atoi(buff2);
   1051 				continue;
   1052 			}
   1053 			if(strstr(buff2, "attention") != NULL ||
   1054 			   strstr(buff2, "error") != NULL)
   1055 				*needs_operator = TRUE;
   1056 			if(*needs_operator || strstr(buff2, "waiting") != NULL)
   1057 				strcpy(status, cp);
   1058 		}
   1059 		stat = PI_RES_OK;
   1060 		break;
   1061 	}
   1062 	(void) pclose(p);
   1063 	return(stat);
   1064 }
   1065 
   1066 #endif /* SVR4 */
   1067 
   1068 /*
   1069  * pr_cancel: cancel a print job
   1070  */
   1071 #ifdef SVR4
   1072 
   1073 /*
   1074 ** For SVR4 we have to be prepared for the following kinds of output:
   1075 **
   1076 ** # cancel lp-6
   1077 ** request "lp-6" cancelled
   1078 ** # cancel lp-33
   1079 ** UX:cancel: WARNING: Request "lp-33" doesn't exist.
   1080 ** # cancel foo-88
   1081 ** UX:cancel: WARNING: Request "foo-88" doesn't exist.
   1082 ** # cancel foo
   1083 ** UX:cancel: WARNING: "foo" is not a request id or a printer.
   1084 **             TO FIX: Cancel requests by id or by
   1085 **                     name of printer where printing.
   1086 ** # su geoff
   1087 ** $ cancel lp-2
   1088 ** UX:cancel: WARNING: Can't cancel request "lp-2".
   1089 **             TO FIX: You are not allowed to cancel
   1090 **                     another's request.
   1091 **
   1092 ** There are probably other variations for remote printers.
   1093 ** Basically, if the reply begins with the string
   1094 **          "UX:cancel: WARNING: "
   1095 ** we can strip this off and look for one of the following
   1096 ** (1) 'R' - should be part of "Request "xxxx" doesn't exist."
   1097 ** (2) '"' - should be start of ""foo" is not a request id or..."
   1098 ** (3) 'C' - should be start of "Can't cancel request..."
   1099 **
   1100 ** The fly in the ointment: all of this can change if these
   1101 ** messages are localized..... :-(
   1102 */
   1103 pcrstat pr_cancel(pr, user, id)
   1104 char *pr;
   1105 char *user;
   1106 char *id;
   1107 {
   1108 char            cmdbuf[256];
   1109 char            resbuf[256];
   1110 FILE *fd;
   1111 pcrstat stat = PC_RES_NO_SUCH_JOB;
   1112 
   1113 	pr = map_printer_name(pr);
   1114 	if(pr == NULL || suspicious(pr))
   1115 		return(PC_RES_NO_SUCH_PRINTER);
   1116 	if(suspicious(id))
   1117 		return(PC_RES_NO_SUCH_JOB);
   1118 
   1119 	sprintf(cmdbuf, "/usr/bin/cancel %s", id);
   1120 	if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
   1121 		msg_out("rpc.pcnfsd: su_popen failed");
   1122 		return(PC_RES_FAIL);
   1123 	}
   1124 
   1125 	if(fgets(resbuf, 255, fd) == NULL)
   1126 		stat = PC_RES_FAIL;
   1127 	else if(!strstr(resbuf, "UX:"))
   1128 		stat = PC_RES_OK;
   1129 	else if(strstr(resbuf, "doesn't exist"))
   1130 		stat = PC_RES_NO_SUCH_JOB;
   1131 	else if(strstr(resbuf, "not a request id"))
   1132 		stat = PC_RES_NO_SUCH_JOB;
   1133 	else if(strstr(resbuf, "Can't cancel request"))
   1134 		stat = PC_RES_NOT_OWNER;
   1135 	else	stat = PC_RES_FAIL;
   1136 
   1137 	if(su_pclose(fd) == 255)
   1138 		msg_out("rpc.pcnfsd: su_pclose alert");
   1139 	return(stat);
   1140 }
   1141 
   1142 #else /* SVR4 */
   1143 
   1144 /*
   1145  * BSD way: lprm
   1146  */
   1147 pcrstat pr_cancel(pr, user, id)
   1148 char *pr;
   1149 char *user;
   1150 char *id;
   1151 {
   1152 	char            cmdbuf[256];
   1153 	char            resbuf[256];
   1154 	FILE *fd;
   1155 	int i;
   1156 	pcrstat stat = PC_RES_NO_SUCH_JOB;
   1157 
   1158 	pr = map_printer_name(pr);
   1159 	if(pr == NULL || suspicious(pr))
   1160 		return(PC_RES_NO_SUCH_PRINTER);
   1161 	if(suspicious(id))
   1162 		return(PC_RES_NO_SUCH_JOB);
   1163 
   1164 		sprintf(cmdbuf, "%s/lprm -P%s %s", LPRDIR, pr, id);
   1165 		if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
   1166 			msg_out("rpc.pcnfsd: su_popen failed");
   1167 			return(PC_RES_FAIL);
   1168 		}
   1169 		while(fgets(resbuf, 255, fd) != NULL) {
   1170 			i = strlen(resbuf);
   1171 			if(i)
   1172 				resbuf[i-1] = '\0'; /* trim NL */
   1173 			if(strstr(resbuf, "dequeued") != NULL)
   1174 				stat = PC_RES_OK;
   1175 			if(strstr(resbuf, "unknown printer") != NULL)
   1176 				stat = PC_RES_NO_SUCH_PRINTER;
   1177 			if(strstr(resbuf, "Permission denied") != NULL)
   1178 				stat = PC_RES_NOT_OWNER;
   1179 		}
   1180 		if(su_pclose(fd) == 255)
   1181 			msg_out("rpc.pcnfsd: su_pclose alert");
   1182 		return(stat);
   1183 }
   1184 #endif /* SVR4 */
   1185 
   1186 /*
   1188 ** New subsystem here. We allow the administrator to define
   1189 ** up to NPRINTERDEFS aliases for printer names. This is done
   1190 ** using the "/etc/pcnfsd.conf" file, which is read at startup.
   1191 ** There are three entry points to this subsystem
   1192 **
   1193 ** void add_printer_alias(char *printer, char *alias_for, char *command)
   1194 **
   1195 ** This is invoked from "config_from_file()" for each
   1196 ** "printer" line. "printer" is the name of a printer; note that
   1197 ** it is possible to redefine an existing printer. "alias_for"
   1198 ** is the name of the underlying printer, used for queue listing
   1199 ** and other control functions. If it is "-", there is no
   1200 ** underlying printer, or the administrative functions are
   1201 ** not applicable to this printer. "command"
   1202 ** is the command which should be run (via "su_popen()") if a
   1203 ** job is printed on this printer. The following tokens may be
   1204 ** embedded in the command, and are substituted as follows:
   1205 **
   1206 ** $FILE	-	path to the file containing the print data
   1207 ** $USER	-	login of user
   1208 ** $HOST	-	hostname from which job originated
   1209 **
   1210 ** Tokens may occur multiple times. If The command includes no
   1211 ** $FILE token, the string " $FILE" is silently appended.
   1212 **
   1213 ** pr_list list_virtual_printers()
   1214 **
   1215 ** This is invoked from build_pr_list to generate a list of aliased
   1216 ** printers, so that the client that asks for a list of valid printers
   1217 ** will see these ones.
   1218 **
   1219 ** char *map_printer_name(char *printer)
   1220 **
   1221 ** If "printer" identifies an aliased printer, this function returns
   1222 ** the "alias_for" name, or NULL if the "alias_for" was given as "-".
   1223 ** Otherwise it returns its argument.
   1224 **
   1225 ** char *expand_alias(char *printer, char *file, char *user, char *host)
   1226 **
   1227 ** If "printer" is an aliased printer, this function returns a
   1228 ** pointer to a static string in which the corresponding command
   1229 ** has been expanded. Otherwise ot returns NULL.
   1230 */
   1231 #define NPRINTERDEFS	16
   1232 int num_aliases = 0;
   1233 struct {
   1234 	char *a_printer;
   1235 	char *a_alias_for;
   1236 	char *a_command;
   1237 } alias [NPRINTERDEFS];
   1238 
   1239 
   1240 
   1241 void
   1242 add_printer_alias(printer, alias_for, command)
   1243 char *printer;
   1244 char *alias_for;
   1245 char *command;
   1246 {
   1247 	if(num_aliases < NPRINTERDEFS) {
   1248 		alias[num_aliases].a_printer = strdup(printer);
   1249 		alias[num_aliases].a_alias_for =
   1250 			(strcmp(alias_for,  "-") ? strdup(alias_for) : NULL);
   1251 		if(strstr(command, "$FILE"))
   1252 			alias[num_aliases].a_command = strdup(command);
   1253 		else {
   1254 			alias[num_aliases].a_command = (char *)grab(strlen(command) + 8);
   1255 			strcpy(alias[num_aliases].a_command, command);
   1256 			strcat(alias[num_aliases].a_command, " $FILE");
   1257 		}
   1258 		num_aliases++;
   1259 	}
   1260 }
   1261 
   1262 pr_list list_virtual_printers()
   1263 {
   1264 pr_list first = NULL;
   1265 pr_list last = NULL;
   1266 pr_list curr = NULL;
   1267 int i;
   1268 
   1269 
   1270 	if(num_aliases == 0)
   1271 		return(NULL);
   1272 
   1273 	for (i = 0; i < num_aliases; i++) {
   1274 		curr = (struct pr_list_item *)
   1275 			grab(sizeof (struct pr_list_item));
   1276 
   1277 		curr->pn = strdup(alias[i].a_printer);
   1278 		if(alias[i].a_alias_for == NULL)
   1279 			curr->device = strdup("");
   1280 		else
   1281 			curr->device = strdup(alias[i].a_alias_for);
   1282 		curr->remhost = strdup("");
   1283 		curr->cm = strdup("(alias)");
   1284 		curr->pr_next = NULL;
   1285 		if(last == NULL)
   1286 			first = curr;
   1287 		else
   1288 			last->pr_next = curr;
   1289 		last = curr;
   1290 
   1291 	}
   1292 	return(first);
   1293 }
   1294 
   1295 
   1296 char *
   1297 map_printer_name(printer)
   1298 char *printer;
   1299 {
   1300 int i;
   1301 	for (i = 0; i < num_aliases; i++){
   1302 		if(!strcmp(printer, alias[i].a_printer))
   1303 			return(alias[i].a_alias_for);
   1304 	}
   1305 	return(printer);
   1306 }
   1307 
   1308 static void
   1309 substitute(string, token, data)
   1310 char *string;
   1311 char *token;
   1312 char *data;
   1313 {
   1314 char temp[512];
   1315 char *c;
   1316 
   1317 	while(c = strstr(string, token)) {
   1318 		*c = '\0';
   1319 		strcpy(temp, string);
   1320 		strcat(temp, data);
   1321 		c += strlen(token);
   1322 		strcat(temp, c);
   1323 		strcpy(string, temp);
   1324 	}
   1325 }
   1326 
   1327 char *
   1328 expand_alias(printer, file, user, host)
   1329 char *printer;
   1330 char *file;
   1331 char *user;
   1332 char *host;
   1333 {
   1334 static char expansion[512];
   1335 int i;
   1336 	for (i = 0; i < num_aliases; i++){
   1337 		if(!strcmp(printer, alias[i].a_printer)) {
   1338 			strcpy(expansion, alias[i].a_command);
   1339 			substitute(expansion, "$FILE", file);
   1340 			substitute(expansion, "$USER", user);
   1341 			substitute(expansion, "$HOST", host);
   1342 			return(expansion);
   1343 		}
   1344 	}
   1345 	return(NULL);
   1346 }
   1347