113955Ssam #ifndef lint 2*16759Sralph static char sccsid[] = "@(#)rmjob.c 4.10 (Berkeley) 07/24/84"; 313955Ssam #endif 413955Ssam 512112Sralph /* 612112Sralph * rmjob - remove the specified jobs from the queue. 712112Sralph */ 812112Sralph 912112Sralph #include "lp.h" 1012112Sralph 1112112Sralph /* 1212112Sralph * Stuff for handling lprm specifications 1312112Sralph */ 1412112Sralph extern char *user[]; /* users to process */ 1512112Sralph extern int users; /* # of users in user array */ 1612112Sralph extern int requ[]; /* job number of spool entries */ 1712112Sralph extern int requests; /* # of spool requests */ 1812877Sralph extern char *person; /* name of person doing lprm */ 1912112Sralph 20*16759Sralph char root[] = "root"; 21*16759Sralph int all = 0; /* eliminate all files (root only) */ 22*16759Sralph int cur_daemon; /* daemon's pid */ 23*16759Sralph char current[40]; /* active control file name */ 2412112Sralph 2512112Sralph int iscf(); 2612112Sralph 2712112Sralph rmjob() 2812112Sralph { 2912112Sralph register int i, nitems; 3012112Sralph int assasinated = 0; 3112112Sralph struct direct **files; 3212112Sralph 3312112Sralph if ((i = pgetent(line, printer)) < 0) 3412112Sralph fatal("cannot open printer description file"); 3512112Sralph else if (i == 0) 3612112Sralph fatal("unknown printer"); 3712112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 3812112Sralph SD = DEFSPOOL; 3912112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 4012112Sralph LO = DEFLOCK; 4112112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 4212112Sralph LP = DEFDEVLP; 4312112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 4412433Sralph RP = DEFLP; 4512527Sralph RM = pgetstr("rm", &bp); 4612112Sralph 4712112Sralph /* 4812112Sralph * If the format was `lprm -' and the user isn't the super-user, 4912112Sralph * then fake things to look like he said `lprm user'. 5012112Sralph */ 5112112Sralph if (users < 0) { 5212112Sralph if (getuid() == 0) 5312112Sralph all = 1; /* all files in local queue */ 5412112Sralph else { 5512112Sralph user[0] = person; 5612112Sralph users = 1; 5712112Sralph } 5812112Sralph } 5912112Sralph if (!strcmp(person, "-all")) { 6012112Sralph if (from == host) 6112112Sralph fatal("The login name \"-all\" is reserved"); 6212112Sralph all = 1; /* all those from 'from' */ 6312112Sralph person = root; 6412112Sralph } 6512112Sralph 6612112Sralph if (chdir(SD) < 0) 6712112Sralph fatal("cannot chdir to spool directory"); 6812112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 6912112Sralph fatal("cannot access spool directory"); 7012112Sralph 7112112Sralph if (nitems) { 7212112Sralph /* 7312112Sralph * Check for an active printer daemon (in which case we 7412112Sralph * kill it if it is reading our file) then remove stuff 7512112Sralph * (after which we have to restart the daemon). 7612112Sralph */ 7712112Sralph if (lockchk(LO) && chk(current)) { 7812112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 7912112Sralph if (!assasinated) 8012112Sralph fatal("cannot kill printer daemon"); 8112112Sralph } 8212112Sralph /* 8312112Sralph * process the files 8412112Sralph */ 8512112Sralph for (i = 0; i < nitems; i++) 8612112Sralph process(files[i]->d_name); 8712112Sralph } 8812112Sralph chkremote(); 8912112Sralph /* 9012112Sralph * Restart the printer daemon if it was killed 9112112Sralph */ 9215828Sralph if (assasinated && !startdaemon(printer)) 9312112Sralph fatal("cannot restart printer daemon\n"); 9412112Sralph exit(0); 9512112Sralph } 9612112Sralph 9712112Sralph /* 9812112Sralph * Process a lock file: collect the pid of the active 9912112Sralph * daemon and the file name of the active spool entry. 10012112Sralph * Return boolean indicating existence of a lock file. 10112112Sralph */ 10212112Sralph lockchk(s) 10312112Sralph char *s; 10412112Sralph { 10512112Sralph register FILE *fp; 10612112Sralph register int i, n; 10712112Sralph 10812112Sralph if ((fp = fopen(s, "r")) == NULL) 10912112Sralph if (errno == EACCES) 11012112Sralph fatal("can't access lock file"); 11112112Sralph else 11212112Sralph return(0); 11313443Sralph if (!getline(fp)) { 11412112Sralph (void) fclose(fp); 11512112Sralph return(0); /* no daemon present */ 11612112Sralph } 11712112Sralph cur_daemon = atoi(line); 11813443Sralph if (kill(cur_daemon, 0) < 0) { 11913443Sralph (void) fclose(fp); 12013443Sralph return(0); /* no daemon present */ 12113443Sralph } 12212112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 12312527Sralph if (i > 5) { 12412112Sralph n = 1; 12512112Sralph break; 12612112Sralph } 12712112Sralph sleep(i); 12812112Sralph } 12912112Sralph current[n-1] = '\0'; 13012112Sralph (void) fclose(fp); 13112112Sralph return(1); 13212112Sralph } 13312112Sralph 13412112Sralph /* 13512112Sralph * Process a control file. 13612112Sralph */ 13712112Sralph process(file) 13812112Sralph char *file; 13912112Sralph { 14012112Sralph FILE *cfp; 14112112Sralph 14212112Sralph if (!chk(file)) 14312112Sralph return; 14412112Sralph if ((cfp = fopen(file, "r")) == NULL) 14512112Sralph fatal("cannot open %s", file); 146*16759Sralph while (getline(cfp)) { 14712112Sralph switch (line[0]) { 14812112Sralph case 'U': /* unlink associated files */ 14912112Sralph if (from != host) 15012112Sralph printf("%s: ", host); 15112112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 15212112Sralph "%s dequeued\n", line+1); 15312112Sralph } 15412112Sralph } 15512112Sralph (void) fclose(cfp); 15612112Sralph if (from != host) 15712112Sralph printf("%s: ", host); 15812112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 15912112Sralph } 16012112Sralph 16112112Sralph /* 16212112Sralph * Do the dirty work in checking 16312112Sralph */ 16412112Sralph chk(file) 16512112Sralph char *file; 16612112Sralph { 16712112Sralph register int *r, n; 16812112Sralph register char **u, *cp; 16912112Sralph FILE *cfp; 17012112Sralph 17115543Sralph /* 17215543Sralph * Check for valid cf file name (mostly checking current). 17315543Sralph */ 17415543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 17515543Sralph return(0); 17615543Sralph 17712112Sralph if (all && (from == host || !strcmp(from, file+6))) 17812112Sralph return(1); 17912112Sralph 18012112Sralph /* 18112112Sralph * get the owner's name from the control file. 18212112Sralph */ 18312112Sralph if ((cfp = fopen(file, "r")) == NULL) 18412112Sralph return(0); 18512112Sralph while (getline(cfp)) { 18612112Sralph if (line[0] == 'P') 18712112Sralph break; 18812112Sralph } 18912112Sralph (void) fclose(cfp); 19012112Sralph if (line[0] != 'P') 19112112Sralph return(0); 19212112Sralph 19312112Sralph if (users == 0 && requests == 0) 19412112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 19512112Sralph /* 19612112Sralph * Check the request list 19712112Sralph */ 19812112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 19912112Sralph n = n * 10 + (*cp++ - '0'); 20012112Sralph for (r = requ; r < &requ[requests]; r++) 20112112Sralph if (*r == n && isowner(line+1, file)) 20212112Sralph return(1); 20312112Sralph /* 20412112Sralph * Check to see if it's in the user list 20512112Sralph */ 20612112Sralph for (u = user; u < &user[users]; u++) 20712112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 20812112Sralph return(1); 20912112Sralph return(0); 21012112Sralph } 21112112Sralph 21212112Sralph /* 21312112Sralph * If root is removing a file on the local machine, allow it. 21412433Sralph * If root is removing a file from a remote machine, only allow 21512433Sralph * files sent from the remote machine to be removed. 21612112Sralph * Normal users can only remove the file from where it was sent. 21712112Sralph */ 21812112Sralph isowner(owner, file) 21912112Sralph char *owner, *file; 22012112Sralph { 22112433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 22212433Sralph return(1); 22312433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 22412433Sralph return(1); 22512433Sralph if (from != host) 22612433Sralph printf("%s: ", host); 22712433Sralph printf("%s: Permission denied\n", file); 22812433Sralph return(0); 22912112Sralph } 23012112Sralph 23112112Sralph /* 23212112Sralph * Check to see if we are sending files to a remote machine. If we are, 23312112Sralph * then try removing files on the remote machine. 23412112Sralph */ 23512112Sralph chkremote() 23612112Sralph { 23712112Sralph register char *cp; 23812112Sralph register int i, rem; 23912112Sralph char buf[BUFSIZ]; 24012112Sralph 24112112Sralph if (*LP || RM == NULL) 24212112Sralph return; /* not sending to a remote machine */ 24312112Sralph 24412433Sralph /* 24512433Sralph * Flush stdout so the user can see what has been deleted 24612433Sralph * while we wait (possibly) for the connection. 24712433Sralph */ 24812433Sralph fflush(stdout); 24912433Sralph 25012112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 25112112Sralph cp = buf; 25212112Sralph for (i = 0; i < users; i++) { 25312112Sralph cp += strlen(cp); 25412112Sralph *cp++ = ' '; 25512112Sralph strcpy(cp, user[i]); 25612112Sralph } 25712112Sralph for (i = 0; i < requests; i++) { 25812112Sralph cp += strlen(cp); 25912112Sralph (void) sprintf(cp, " %d", requ[i]); 26012112Sralph } 26112112Sralph strcat(cp, "\n"); 26212527Sralph rem = getport(RM); 26312112Sralph if (rem < 0) { 26412112Sralph if (from != host) 26512112Sralph printf("%s: ", host); 26612112Sralph printf("connection to %s is down\n", RM); 26712112Sralph } else { 26812112Sralph i = strlen(buf); 26912112Sralph if (write(rem, buf, i) != i) 27012112Sralph fatal("Lost connection"); 27112112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 27212112Sralph (void) fwrite(buf, 1, i, stdout); 27312112Sralph (void) close(rem); 27412112Sralph } 27512112Sralph } 27612112Sralph 27712112Sralph /* 27812112Sralph * Return 1 if the filename begins with 'cf' 27912112Sralph */ 28012112Sralph iscf(d) 28112112Sralph struct direct *d; 28212112Sralph { 28312112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 28412112Sralph } 285