122439Sdist /* 222439Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 5*56120Selan * Redistribution and use in source and binary forms, with or without 6*56120Selan * modification, are permitted provided that the following conditions 7*56120Selan * are met: 8*56120Selan * 1. Redistributions of source code must retain the above copyright 9*56120Selan * notice, this list of conditions and the following disclaimer. 10*56120Selan * 2. Redistributions in binary form must reproduce the above copyright 11*56120Selan * notice, this list of conditions and the following disclaimer in the 12*56120Selan * documentation and/or other materials provided with the distribution. 13*56120Selan * 3. All advertising materials mentioning features or use of this software 14*56120Selan * must display the following acknowledgement: 15*56120Selan * This product includes software developed by the University of 16*56120Selan * California, Berkeley and its contributors. 17*56120Selan * 4. Neither the name of the University nor the names of its contributors 18*56120Selan * may be used to endorse or promote products derived from this software 19*56120Selan * without specific prior written permission. 20*56120Selan * 21*56120Selan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22*56120Selan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*56120Selan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*56120Selan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25*56120Selan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*56120Selan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*56120Selan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*56120Selan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*56120Selan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*56120Selan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*56120Selan * SUCH DAMAGE. 3222439Sdist */ 3322439Sdist 3413955Ssam #ifndef lint 35*56120Selan static char sccsid[] = "@(#)rmjob.c 5.10 (Berkeley) 8/31/92"; 3634203Sbostic #endif /* not lint */ 3713955Ssam 3855470Sbostic #include <sys/param.h> 3912112Sralph 4055470Sbostic #include <signal.h> 4155470Sbostic #include <errno.h> 4255470Sbostic #include <dirent.h> 4355470Sbostic #include <unistd.h> 4455470Sbostic #include <stdlib.h> 4555470Sbostic #include <stdio.h> 4655470Sbostic #include <string.h> 4755470Sbostic #include <ctype.h> 4812112Sralph #include "lp.h" 4955470Sbostic #include "lp.local.h" 5037968Sbostic #include "pathnames.h" 5112112Sralph 5212112Sralph /* 5355470Sbostic * rmjob - remove the specified jobs from the queue. 5455470Sbostic */ 5555470Sbostic 5655470Sbostic /* 5712112Sralph * Stuff for handling lprm specifications 5812112Sralph */ 5912112Sralph extern char *user[]; /* users to process */ 6012112Sralph extern int users; /* # of users in user array */ 6112112Sralph extern int requ[]; /* job number of spool entries */ 6212112Sralph extern int requests; /* # of spool requests */ 6312877Sralph extern char *person; /* name of person doing lprm */ 6412112Sralph 65*56120Selan static char root[] = "root"; 66*56120Selan static int all = 0; /* eliminate all files (root only) */ 67*56120Selan static int cur_daemon; /* daemon's pid */ 68*56120Selan static char current[40]; /* active control file name */ 6912112Sralph 7055470Sbostic void 7112112Sralph rmjob() 7212112Sralph { 7312112Sralph register int i, nitems; 7412112Sralph int assasinated = 0; 7555470Sbostic struct dirent **files; 7638736Stef char *cp; 7712112Sralph 78*56120Selan if ((i = cgetent(&bp, printcapdb, printer)) == -2) 79*56120Selan fatal("can't open printer description file"); 80*56120Selan else if (i == -1) 8112112Sralph fatal("unknown printer"); 82*56120Selan else if (i == -3) 83*56120Selan fatal("potential reference loop detected in printcap file"); 84*56120Selan if (cgetstr(bp, "lp", &LP) < 0) 85*56120Selan LP = _PATH_DEFDEVLP; 86*56120Selan if (cgetstr(bp, "rp", &RP) < 0) 87*56120Selan RP = DEFLP; 88*56120Selan if (cgetstr(bp, "sd", &SD) < 0) 8937968Sbostic SD = _PATH_DEFSPOOL; 90*56120Selan if (cgetstr(bp,"lo", &LO) < 0) 9112112Sralph LO = DEFLOCK; 92*56120Selan cgetstr(bp, "rm", &RM); 9338736Stef if (cp = checkremote()) 9438736Stef printf("Warning: %s\n", cp); 9512112Sralph 9612112Sralph /* 9712112Sralph * If the format was `lprm -' and the user isn't the super-user, 9812112Sralph * then fake things to look like he said `lprm user'. 9912112Sralph */ 10012112Sralph if (users < 0) { 10112112Sralph if (getuid() == 0) 10212112Sralph all = 1; /* all files in local queue */ 10312112Sralph else { 10412112Sralph user[0] = person; 10512112Sralph users = 1; 10612112Sralph } 10712112Sralph } 10812112Sralph if (!strcmp(person, "-all")) { 10912112Sralph if (from == host) 11012112Sralph fatal("The login name \"-all\" is reserved"); 11112112Sralph all = 1; /* all those from 'from' */ 11212112Sralph person = root; 11312112Sralph } 11412112Sralph 11512112Sralph if (chdir(SD) < 0) 11612112Sralph fatal("cannot chdir to spool directory"); 11712112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 11812112Sralph fatal("cannot access spool directory"); 11912112Sralph 12012112Sralph if (nitems) { 12112112Sralph /* 12212112Sralph * Check for an active printer daemon (in which case we 12312112Sralph * kill it if it is reading our file) then remove stuff 12412112Sralph * (after which we have to restart the daemon). 12512112Sralph */ 12612112Sralph if (lockchk(LO) && chk(current)) { 12712112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 12812112Sralph if (!assasinated) 12912112Sralph fatal("cannot kill printer daemon"); 13012112Sralph } 13112112Sralph /* 13212112Sralph * process the files 13312112Sralph */ 13412112Sralph for (i = 0; i < nitems; i++) 13512112Sralph process(files[i]->d_name); 13612112Sralph } 13738736Stef rmremote(); 13812112Sralph /* 13912112Sralph * Restart the printer daemon if it was killed 14012112Sralph */ 14115828Sralph if (assasinated && !startdaemon(printer)) 14212112Sralph fatal("cannot restart printer daemon\n"); 14312112Sralph exit(0); 14412112Sralph } 14512112Sralph 14612112Sralph /* 14712112Sralph * Process a lock file: collect the pid of the active 14812112Sralph * daemon and the file name of the active spool entry. 14912112Sralph * Return boolean indicating existence of a lock file. 15012112Sralph */ 15155470Sbostic int 15212112Sralph lockchk(s) 15312112Sralph char *s; 15412112Sralph { 15512112Sralph register FILE *fp; 15612112Sralph register int i, n; 15712112Sralph 15812112Sralph if ((fp = fopen(s, "r")) == NULL) 15912112Sralph if (errno == EACCES) 16012112Sralph fatal("can't access lock file"); 16112112Sralph else 16212112Sralph return(0); 16313443Sralph if (!getline(fp)) { 16412112Sralph (void) fclose(fp); 16512112Sralph return(0); /* no daemon present */ 16612112Sralph } 16712112Sralph cur_daemon = atoi(line); 16813443Sralph if (kill(cur_daemon, 0) < 0) { 16913443Sralph (void) fclose(fp); 17013443Sralph return(0); /* no daemon present */ 17113443Sralph } 17212112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 17312527Sralph if (i > 5) { 17412112Sralph n = 1; 17512112Sralph break; 17612112Sralph } 17712112Sralph sleep(i); 17812112Sralph } 17912112Sralph current[n-1] = '\0'; 18012112Sralph (void) fclose(fp); 18112112Sralph return(1); 18212112Sralph } 18312112Sralph 18412112Sralph /* 18512112Sralph * Process a control file. 18612112Sralph */ 18755470Sbostic void 18812112Sralph process(file) 18912112Sralph char *file; 19012112Sralph { 19112112Sralph FILE *cfp; 19212112Sralph 19312112Sralph if (!chk(file)) 19412112Sralph return; 19512112Sralph if ((cfp = fopen(file, "r")) == NULL) 19612112Sralph fatal("cannot open %s", file); 19716759Sralph while (getline(cfp)) { 19812112Sralph switch (line[0]) { 19912112Sralph case 'U': /* unlink associated files */ 20012112Sralph if (from != host) 20112112Sralph printf("%s: ", host); 20212112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 20312112Sralph "%s dequeued\n", line+1); 20412112Sralph } 20512112Sralph } 20612112Sralph (void) fclose(cfp); 20712112Sralph if (from != host) 20812112Sralph printf("%s: ", host); 20912112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 21012112Sralph } 21112112Sralph 21212112Sralph /* 21312112Sralph * Do the dirty work in checking 21412112Sralph */ 21555470Sbostic int 21612112Sralph chk(file) 21712112Sralph char *file; 21812112Sralph { 21912112Sralph register int *r, n; 22012112Sralph register char **u, *cp; 22112112Sralph FILE *cfp; 22212112Sralph 22315543Sralph /* 22415543Sralph * Check for valid cf file name (mostly checking current). 22515543Sralph */ 22615543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 22715543Sralph return(0); 22815543Sralph 22912112Sralph if (all && (from == host || !strcmp(from, file+6))) 23012112Sralph return(1); 23112112Sralph 23212112Sralph /* 23312112Sralph * get the owner's name from the control file. 23412112Sralph */ 23512112Sralph if ((cfp = fopen(file, "r")) == NULL) 23612112Sralph return(0); 23712112Sralph while (getline(cfp)) { 23812112Sralph if (line[0] == 'P') 23912112Sralph break; 24012112Sralph } 24112112Sralph (void) fclose(cfp); 24212112Sralph if (line[0] != 'P') 24312112Sralph return(0); 24412112Sralph 24512112Sralph if (users == 0 && requests == 0) 24612112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 24712112Sralph /* 24812112Sralph * Check the request list 24912112Sralph */ 25012112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 25112112Sralph n = n * 10 + (*cp++ - '0'); 25212112Sralph for (r = requ; r < &requ[requests]; r++) 25312112Sralph if (*r == n && isowner(line+1, file)) 25412112Sralph return(1); 25512112Sralph /* 25612112Sralph * Check to see if it's in the user list 25712112Sralph */ 25812112Sralph for (u = user; u < &user[users]; u++) 25912112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 26012112Sralph return(1); 26112112Sralph return(0); 26212112Sralph } 26312112Sralph 26412112Sralph /* 26512112Sralph * If root is removing a file on the local machine, allow it. 26612433Sralph * If root is removing a file from a remote machine, only allow 26712433Sralph * files sent from the remote machine to be removed. 26812112Sralph * Normal users can only remove the file from where it was sent. 26912112Sralph */ 27055470Sbostic int 27112112Sralph isowner(owner, file) 27212112Sralph char *owner, *file; 27312112Sralph { 27412433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 27512433Sralph return(1); 27612433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 27712433Sralph return(1); 27812433Sralph if (from != host) 27912433Sralph printf("%s: ", host); 28012433Sralph printf("%s: Permission denied\n", file); 28112433Sralph return(0); 28212112Sralph } 28312112Sralph 28412112Sralph /* 28512112Sralph * Check to see if we are sending files to a remote machine. If we are, 28612112Sralph * then try removing files on the remote machine. 28712112Sralph */ 28855470Sbostic void 28938736Stef rmremote() 29012112Sralph { 29112112Sralph register char *cp; 29212112Sralph register int i, rem; 29312112Sralph char buf[BUFSIZ]; 29412112Sralph 29538736Stef if (!sendtorem) 29612112Sralph return; /* not sending to a remote machine */ 29712112Sralph 29812433Sralph /* 29912433Sralph * Flush stdout so the user can see what has been deleted 30012433Sralph * while we wait (possibly) for the connection. 30112433Sralph */ 30212433Sralph fflush(stdout); 30312433Sralph 30455470Sbostic (void)snprintf(buf, sizeof(buf), "\5%s %s", RP, all ? "-all" : person); 30512112Sralph cp = buf; 30612112Sralph for (i = 0; i < users; i++) { 30712112Sralph cp += strlen(cp); 30812112Sralph *cp++ = ' '; 30912112Sralph strcpy(cp, user[i]); 31012112Sralph } 31112112Sralph for (i = 0; i < requests; i++) { 31212112Sralph cp += strlen(cp); 31312112Sralph (void) sprintf(cp, " %d", requ[i]); 31412112Sralph } 31512112Sralph strcat(cp, "\n"); 31612527Sralph rem = getport(RM); 31712112Sralph if (rem < 0) { 31812112Sralph if (from != host) 31912112Sralph printf("%s: ", host); 32012112Sralph printf("connection to %s is down\n", RM); 32112112Sralph } else { 32212112Sralph i = strlen(buf); 32312112Sralph if (write(rem, buf, i) != i) 32412112Sralph fatal("Lost connection"); 32512112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 32612112Sralph (void) fwrite(buf, 1, i, stdout); 32712112Sralph (void) close(rem); 32812112Sralph } 32912112Sralph } 33012112Sralph 33112112Sralph /* 33212112Sralph * Return 1 if the filename begins with 'cf' 33312112Sralph */ 33455470Sbostic int 33512112Sralph iscf(d) 33655470Sbostic struct dirent *d; 33712112Sralph { 33812112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 33912112Sralph } 340