122439Sdist /* 222439Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 542801Sbostic * %sccs.include.redist.c% 622439Sdist */ 722439Sdist 813955Ssam #ifndef lint 9*55470Sbostic static char sccsid[] = "@(#)rmjob.c 5.8 (Berkeley) 07/21/92"; 1034203Sbostic #endif /* not lint */ 1113955Ssam 12*55470Sbostic #include <sys/param.h> 1312112Sralph 14*55470Sbostic #include <signal.h> 15*55470Sbostic #include <errno.h> 16*55470Sbostic #include <dirent.h> 17*55470Sbostic #include <unistd.h> 18*55470Sbostic #include <stdlib.h> 19*55470Sbostic #include <stdio.h> 20*55470Sbostic #include <string.h> 21*55470Sbostic #include <ctype.h> 2212112Sralph #include "lp.h" 23*55470Sbostic #include "lp.local.h" 2437968Sbostic #include "pathnames.h" 2512112Sralph 2612112Sralph /* 27*55470Sbostic * rmjob - remove the specified jobs from the queue. 28*55470Sbostic */ 29*55470Sbostic 30*55470Sbostic /* 3112112Sralph * Stuff for handling lprm specifications 3212112Sralph */ 3312112Sralph extern char *user[]; /* users to process */ 3412112Sralph extern int users; /* # of users in user array */ 3512112Sralph extern int requ[]; /* job number of spool entries */ 3612112Sralph extern int requests; /* # of spool requests */ 3712877Sralph extern char *person; /* name of person doing lprm */ 3812112Sralph 3916759Sralph char root[] = "root"; 4016759Sralph int all = 0; /* eliminate all files (root only) */ 4116759Sralph int cur_daemon; /* daemon's pid */ 4216759Sralph char current[40]; /* active control file name */ 4312112Sralph 44*55470Sbostic void 4512112Sralph rmjob() 4612112Sralph { 4712112Sralph register int i, nitems; 4812112Sralph int assasinated = 0; 49*55470Sbostic struct dirent **files; 5038736Stef char *cp; 5112112Sralph 5212112Sralph if ((i = pgetent(line, printer)) < 0) 5312112Sralph fatal("cannot open printer description file"); 5412112Sralph else if (i == 0) 5512112Sralph fatal("unknown printer"); 5612112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 5737968Sbostic SD = _PATH_DEFSPOOL; 5812112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 5912112Sralph LO = DEFLOCK; 6012112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 6137968Sbostic LP = _PATH_DEFDEVLP; 6212112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 6312433Sralph RP = DEFLP; 6412527Sralph RM = pgetstr("rm", &bp); 6538736Stef if (cp = checkremote()) 6638736Stef printf("Warning: %s\n", cp); 6712112Sralph 6812112Sralph /* 6912112Sralph * If the format was `lprm -' and the user isn't the super-user, 7012112Sralph * then fake things to look like he said `lprm user'. 7112112Sralph */ 7212112Sralph if (users < 0) { 7312112Sralph if (getuid() == 0) 7412112Sralph all = 1; /* all files in local queue */ 7512112Sralph else { 7612112Sralph user[0] = person; 7712112Sralph users = 1; 7812112Sralph } 7912112Sralph } 8012112Sralph if (!strcmp(person, "-all")) { 8112112Sralph if (from == host) 8212112Sralph fatal("The login name \"-all\" is reserved"); 8312112Sralph all = 1; /* all those from 'from' */ 8412112Sralph person = root; 8512112Sralph } 8612112Sralph 8712112Sralph if (chdir(SD) < 0) 8812112Sralph fatal("cannot chdir to spool directory"); 8912112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 9012112Sralph fatal("cannot access spool directory"); 9112112Sralph 9212112Sralph if (nitems) { 9312112Sralph /* 9412112Sralph * Check for an active printer daemon (in which case we 9512112Sralph * kill it if it is reading our file) then remove stuff 9612112Sralph * (after which we have to restart the daemon). 9712112Sralph */ 9812112Sralph if (lockchk(LO) && chk(current)) { 9912112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 10012112Sralph if (!assasinated) 10112112Sralph fatal("cannot kill printer daemon"); 10212112Sralph } 10312112Sralph /* 10412112Sralph * process the files 10512112Sralph */ 10612112Sralph for (i = 0; i < nitems; i++) 10712112Sralph process(files[i]->d_name); 10812112Sralph } 10938736Stef rmremote(); 11012112Sralph /* 11112112Sralph * Restart the printer daemon if it was killed 11212112Sralph */ 11315828Sralph if (assasinated && !startdaemon(printer)) 11412112Sralph fatal("cannot restart printer daemon\n"); 11512112Sralph exit(0); 11612112Sralph } 11712112Sralph 11812112Sralph /* 11912112Sralph * Process a lock file: collect the pid of the active 12012112Sralph * daemon and the file name of the active spool entry. 12112112Sralph * Return boolean indicating existence of a lock file. 12212112Sralph */ 123*55470Sbostic int 12412112Sralph lockchk(s) 12512112Sralph char *s; 12612112Sralph { 12712112Sralph register FILE *fp; 12812112Sralph register int i, n; 12912112Sralph 13012112Sralph if ((fp = fopen(s, "r")) == NULL) 13112112Sralph if (errno == EACCES) 13212112Sralph fatal("can't access lock file"); 13312112Sralph else 13412112Sralph return(0); 13513443Sralph if (!getline(fp)) { 13612112Sralph (void) fclose(fp); 13712112Sralph return(0); /* no daemon present */ 13812112Sralph } 13912112Sralph cur_daemon = atoi(line); 14013443Sralph if (kill(cur_daemon, 0) < 0) { 14113443Sralph (void) fclose(fp); 14213443Sralph return(0); /* no daemon present */ 14313443Sralph } 14412112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 14512527Sralph if (i > 5) { 14612112Sralph n = 1; 14712112Sralph break; 14812112Sralph } 14912112Sralph sleep(i); 15012112Sralph } 15112112Sralph current[n-1] = '\0'; 15212112Sralph (void) fclose(fp); 15312112Sralph return(1); 15412112Sralph } 15512112Sralph 15612112Sralph /* 15712112Sralph * Process a control file. 15812112Sralph */ 159*55470Sbostic void 16012112Sralph process(file) 16112112Sralph char *file; 16212112Sralph { 16312112Sralph FILE *cfp; 16412112Sralph 16512112Sralph if (!chk(file)) 16612112Sralph return; 16712112Sralph if ((cfp = fopen(file, "r")) == NULL) 16812112Sralph fatal("cannot open %s", file); 16916759Sralph while (getline(cfp)) { 17012112Sralph switch (line[0]) { 17112112Sralph case 'U': /* unlink associated files */ 17212112Sralph if (from != host) 17312112Sralph printf("%s: ", host); 17412112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 17512112Sralph "%s dequeued\n", line+1); 17612112Sralph } 17712112Sralph } 17812112Sralph (void) fclose(cfp); 17912112Sralph if (from != host) 18012112Sralph printf("%s: ", host); 18112112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 18212112Sralph } 18312112Sralph 18412112Sralph /* 18512112Sralph * Do the dirty work in checking 18612112Sralph */ 187*55470Sbostic int 18812112Sralph chk(file) 18912112Sralph char *file; 19012112Sralph { 19112112Sralph register int *r, n; 19212112Sralph register char **u, *cp; 19312112Sralph FILE *cfp; 19412112Sralph 19515543Sralph /* 19615543Sralph * Check for valid cf file name (mostly checking current). 19715543Sralph */ 19815543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 19915543Sralph return(0); 20015543Sralph 20112112Sralph if (all && (from == host || !strcmp(from, file+6))) 20212112Sralph return(1); 20312112Sralph 20412112Sralph /* 20512112Sralph * get the owner's name from the control file. 20612112Sralph */ 20712112Sralph if ((cfp = fopen(file, "r")) == NULL) 20812112Sralph return(0); 20912112Sralph while (getline(cfp)) { 21012112Sralph if (line[0] == 'P') 21112112Sralph break; 21212112Sralph } 21312112Sralph (void) fclose(cfp); 21412112Sralph if (line[0] != 'P') 21512112Sralph return(0); 21612112Sralph 21712112Sralph if (users == 0 && requests == 0) 21812112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 21912112Sralph /* 22012112Sralph * Check the request list 22112112Sralph */ 22212112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 22312112Sralph n = n * 10 + (*cp++ - '0'); 22412112Sralph for (r = requ; r < &requ[requests]; r++) 22512112Sralph if (*r == n && isowner(line+1, file)) 22612112Sralph return(1); 22712112Sralph /* 22812112Sralph * Check to see if it's in the user list 22912112Sralph */ 23012112Sralph for (u = user; u < &user[users]; u++) 23112112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 23212112Sralph return(1); 23312112Sralph return(0); 23412112Sralph } 23512112Sralph 23612112Sralph /* 23712112Sralph * If root is removing a file on the local machine, allow it. 23812433Sralph * If root is removing a file from a remote machine, only allow 23912433Sralph * files sent from the remote machine to be removed. 24012112Sralph * Normal users can only remove the file from where it was sent. 24112112Sralph */ 242*55470Sbostic int 24312112Sralph isowner(owner, file) 24412112Sralph char *owner, *file; 24512112Sralph { 24612433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 24712433Sralph return(1); 24812433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 24912433Sralph return(1); 25012433Sralph if (from != host) 25112433Sralph printf("%s: ", host); 25212433Sralph printf("%s: Permission denied\n", file); 25312433Sralph return(0); 25412112Sralph } 25512112Sralph 25612112Sralph /* 25712112Sralph * Check to see if we are sending files to a remote machine. If we are, 25812112Sralph * then try removing files on the remote machine. 25912112Sralph */ 260*55470Sbostic void 26138736Stef rmremote() 26212112Sralph { 26312112Sralph register char *cp; 26412112Sralph register int i, rem; 26512112Sralph char buf[BUFSIZ]; 26612112Sralph 26738736Stef if (!sendtorem) 26812112Sralph return; /* not sending to a remote machine */ 26912112Sralph 27012433Sralph /* 27112433Sralph * Flush stdout so the user can see what has been deleted 27212433Sralph * while we wait (possibly) for the connection. 27312433Sralph */ 27412433Sralph fflush(stdout); 27512433Sralph 276*55470Sbostic (void)snprintf(buf, sizeof(buf), "\5%s %s", RP, all ? "-all" : person); 27712112Sralph cp = buf; 27812112Sralph for (i = 0; i < users; i++) { 27912112Sralph cp += strlen(cp); 28012112Sralph *cp++ = ' '; 28112112Sralph strcpy(cp, user[i]); 28212112Sralph } 28312112Sralph for (i = 0; i < requests; i++) { 28412112Sralph cp += strlen(cp); 28512112Sralph (void) sprintf(cp, " %d", requ[i]); 28612112Sralph } 28712112Sralph strcat(cp, "\n"); 28812527Sralph rem = getport(RM); 28912112Sralph if (rem < 0) { 29012112Sralph if (from != host) 29112112Sralph printf("%s: ", host); 29212112Sralph printf("connection to %s is down\n", RM); 29312112Sralph } else { 29412112Sralph i = strlen(buf); 29512112Sralph if (write(rem, buf, i) != i) 29612112Sralph fatal("Lost connection"); 29712112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 29812112Sralph (void) fwrite(buf, 1, i, stdout); 29912112Sralph (void) close(rem); 30012112Sralph } 30112112Sralph } 30212112Sralph 30312112Sralph /* 30412112Sralph * Return 1 if the filename begins with 'cf' 30512112Sralph */ 306*55470Sbostic int 30712112Sralph iscf(d) 308*55470Sbostic struct dirent *d; 30912112Sralph { 31012112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 31112112Sralph } 312