122439Sdist /* 222439Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 534203Sbostic * Redistribution and use in source and binary forms are permitted 6*34936Sbostic * provided that the above copyright notice and this paragraph are 7*34936Sbostic * duplicated in all such forms and that any documentation, 8*34936Sbostic * advertising materials, and other materials related to such 9*34936Sbostic * distribution and use acknowledge that the software was developed 10*34936Sbostic * by the University of California, Berkeley. The name of the 11*34936Sbostic * University may not be used to endorse or promote products derived 12*34936Sbostic * from this software without specific prior written permission. 13*34936Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34936Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34936Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622439Sdist */ 1722439Sdist 1813955Ssam #ifndef lint 19*34936Sbostic static char sccsid[] = "@(#)rmjob.c 5.3 (Berkeley) 06/30/88"; 2034203Sbostic #endif /* not lint */ 2113955Ssam 2212112Sralph /* 2312112Sralph * rmjob - remove the specified jobs from the queue. 2412112Sralph */ 2512112Sralph 2612112Sralph #include "lp.h" 2712112Sralph 2812112Sralph /* 2912112Sralph * Stuff for handling lprm specifications 3012112Sralph */ 3112112Sralph extern char *user[]; /* users to process */ 3212112Sralph extern int users; /* # of users in user array */ 3312112Sralph extern int requ[]; /* job number of spool entries */ 3412112Sralph extern int requests; /* # of spool requests */ 3512877Sralph extern char *person; /* name of person doing lprm */ 3612112Sralph 3716759Sralph char root[] = "root"; 3816759Sralph int all = 0; /* eliminate all files (root only) */ 3916759Sralph int cur_daemon; /* daemon's pid */ 4016759Sralph char current[40]; /* active control file name */ 4112112Sralph 4212112Sralph int iscf(); 4312112Sralph 4412112Sralph rmjob() 4512112Sralph { 4612112Sralph register int i, nitems; 4712112Sralph int assasinated = 0; 4812112Sralph struct direct **files; 4912112Sralph 5012112Sralph if ((i = pgetent(line, printer)) < 0) 5112112Sralph fatal("cannot open printer description file"); 5212112Sralph else if (i == 0) 5312112Sralph fatal("unknown printer"); 5412112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 5512112Sralph SD = DEFSPOOL; 5612112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 5712112Sralph LO = DEFLOCK; 5812112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 5912112Sralph LP = DEFDEVLP; 6012112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 6112433Sralph RP = DEFLP; 6212527Sralph RM = pgetstr("rm", &bp); 6312112Sralph 6412112Sralph /* 6512112Sralph * If the format was `lprm -' and the user isn't the super-user, 6612112Sralph * then fake things to look like he said `lprm user'. 6712112Sralph */ 6812112Sralph if (users < 0) { 6912112Sralph if (getuid() == 0) 7012112Sralph all = 1; /* all files in local queue */ 7112112Sralph else { 7212112Sralph user[0] = person; 7312112Sralph users = 1; 7412112Sralph } 7512112Sralph } 7612112Sralph if (!strcmp(person, "-all")) { 7712112Sralph if (from == host) 7812112Sralph fatal("The login name \"-all\" is reserved"); 7912112Sralph all = 1; /* all those from 'from' */ 8012112Sralph person = root; 8112112Sralph } 8212112Sralph 8312112Sralph if (chdir(SD) < 0) 8412112Sralph fatal("cannot chdir to spool directory"); 8512112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 8612112Sralph fatal("cannot access spool directory"); 8712112Sralph 8812112Sralph if (nitems) { 8912112Sralph /* 9012112Sralph * Check for an active printer daemon (in which case we 9112112Sralph * kill it if it is reading our file) then remove stuff 9212112Sralph * (after which we have to restart the daemon). 9312112Sralph */ 9412112Sralph if (lockchk(LO) && chk(current)) { 9512112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 9612112Sralph if (!assasinated) 9712112Sralph fatal("cannot kill printer daemon"); 9812112Sralph } 9912112Sralph /* 10012112Sralph * process the files 10112112Sralph */ 10212112Sralph for (i = 0; i < nitems; i++) 10312112Sralph process(files[i]->d_name); 10412112Sralph } 10512112Sralph chkremote(); 10612112Sralph /* 10712112Sralph * Restart the printer daemon if it was killed 10812112Sralph */ 10915828Sralph if (assasinated && !startdaemon(printer)) 11012112Sralph fatal("cannot restart printer daemon\n"); 11112112Sralph exit(0); 11212112Sralph } 11312112Sralph 11412112Sralph /* 11512112Sralph * Process a lock file: collect the pid of the active 11612112Sralph * daemon and the file name of the active spool entry. 11712112Sralph * Return boolean indicating existence of a lock file. 11812112Sralph */ 11912112Sralph lockchk(s) 12012112Sralph char *s; 12112112Sralph { 12212112Sralph register FILE *fp; 12312112Sralph register int i, n; 12412112Sralph 12512112Sralph if ((fp = fopen(s, "r")) == NULL) 12612112Sralph if (errno == EACCES) 12712112Sralph fatal("can't access lock file"); 12812112Sralph else 12912112Sralph return(0); 13013443Sralph if (!getline(fp)) { 13112112Sralph (void) fclose(fp); 13212112Sralph return(0); /* no daemon present */ 13312112Sralph } 13412112Sralph cur_daemon = atoi(line); 13513443Sralph if (kill(cur_daemon, 0) < 0) { 13613443Sralph (void) fclose(fp); 13713443Sralph return(0); /* no daemon present */ 13813443Sralph } 13912112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 14012527Sralph if (i > 5) { 14112112Sralph n = 1; 14212112Sralph break; 14312112Sralph } 14412112Sralph sleep(i); 14512112Sralph } 14612112Sralph current[n-1] = '\0'; 14712112Sralph (void) fclose(fp); 14812112Sralph return(1); 14912112Sralph } 15012112Sralph 15112112Sralph /* 15212112Sralph * Process a control file. 15312112Sralph */ 15412112Sralph process(file) 15512112Sralph char *file; 15612112Sralph { 15712112Sralph FILE *cfp; 15812112Sralph 15912112Sralph if (!chk(file)) 16012112Sralph return; 16112112Sralph if ((cfp = fopen(file, "r")) == NULL) 16212112Sralph fatal("cannot open %s", file); 16316759Sralph while (getline(cfp)) { 16412112Sralph switch (line[0]) { 16512112Sralph case 'U': /* unlink associated files */ 16612112Sralph if (from != host) 16712112Sralph printf("%s: ", host); 16812112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 16912112Sralph "%s dequeued\n", line+1); 17012112Sralph } 17112112Sralph } 17212112Sralph (void) fclose(cfp); 17312112Sralph if (from != host) 17412112Sralph printf("%s: ", host); 17512112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 17612112Sralph } 17712112Sralph 17812112Sralph /* 17912112Sralph * Do the dirty work in checking 18012112Sralph */ 18112112Sralph chk(file) 18212112Sralph char *file; 18312112Sralph { 18412112Sralph register int *r, n; 18512112Sralph register char **u, *cp; 18612112Sralph FILE *cfp; 18712112Sralph 18815543Sralph /* 18915543Sralph * Check for valid cf file name (mostly checking current). 19015543Sralph */ 19115543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 19215543Sralph return(0); 19315543Sralph 19412112Sralph if (all && (from == host || !strcmp(from, file+6))) 19512112Sralph return(1); 19612112Sralph 19712112Sralph /* 19812112Sralph * get the owner's name from the control file. 19912112Sralph */ 20012112Sralph if ((cfp = fopen(file, "r")) == NULL) 20112112Sralph return(0); 20212112Sralph while (getline(cfp)) { 20312112Sralph if (line[0] == 'P') 20412112Sralph break; 20512112Sralph } 20612112Sralph (void) fclose(cfp); 20712112Sralph if (line[0] != 'P') 20812112Sralph return(0); 20912112Sralph 21012112Sralph if (users == 0 && requests == 0) 21112112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 21212112Sralph /* 21312112Sralph * Check the request list 21412112Sralph */ 21512112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 21612112Sralph n = n * 10 + (*cp++ - '0'); 21712112Sralph for (r = requ; r < &requ[requests]; r++) 21812112Sralph if (*r == n && isowner(line+1, file)) 21912112Sralph return(1); 22012112Sralph /* 22112112Sralph * Check to see if it's in the user list 22212112Sralph */ 22312112Sralph for (u = user; u < &user[users]; u++) 22412112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 22512112Sralph return(1); 22612112Sralph return(0); 22712112Sralph } 22812112Sralph 22912112Sralph /* 23012112Sralph * If root is removing a file on the local machine, allow it. 23112433Sralph * If root is removing a file from a remote machine, only allow 23212433Sralph * files sent from the remote machine to be removed. 23312112Sralph * Normal users can only remove the file from where it was sent. 23412112Sralph */ 23512112Sralph isowner(owner, file) 23612112Sralph char *owner, *file; 23712112Sralph { 23812433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 23912433Sralph return(1); 24012433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 24112433Sralph return(1); 24212433Sralph if (from != host) 24312433Sralph printf("%s: ", host); 24412433Sralph printf("%s: Permission denied\n", file); 24512433Sralph return(0); 24612112Sralph } 24712112Sralph 24812112Sralph /* 24912112Sralph * Check to see if we are sending files to a remote machine. If we are, 25012112Sralph * then try removing files on the remote machine. 25112112Sralph */ 25212112Sralph chkremote() 25312112Sralph { 25412112Sralph register char *cp; 25512112Sralph register int i, rem; 25612112Sralph char buf[BUFSIZ]; 25712112Sralph 25812112Sralph if (*LP || RM == NULL) 25912112Sralph return; /* not sending to a remote machine */ 26012112Sralph 26112433Sralph /* 26212433Sralph * Flush stdout so the user can see what has been deleted 26312433Sralph * while we wait (possibly) for the connection. 26412433Sralph */ 26512433Sralph fflush(stdout); 26612433Sralph 26712112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 26812112Sralph cp = buf; 26912112Sralph for (i = 0; i < users; i++) { 27012112Sralph cp += strlen(cp); 27112112Sralph *cp++ = ' '; 27212112Sralph strcpy(cp, user[i]); 27312112Sralph } 27412112Sralph for (i = 0; i < requests; i++) { 27512112Sralph cp += strlen(cp); 27612112Sralph (void) sprintf(cp, " %d", requ[i]); 27712112Sralph } 27812112Sralph strcat(cp, "\n"); 27912527Sralph rem = getport(RM); 28012112Sralph if (rem < 0) { 28112112Sralph if (from != host) 28212112Sralph printf("%s: ", host); 28312112Sralph printf("connection to %s is down\n", RM); 28412112Sralph } else { 28512112Sralph i = strlen(buf); 28612112Sralph if (write(rem, buf, i) != i) 28712112Sralph fatal("Lost connection"); 28812112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 28912112Sralph (void) fwrite(buf, 1, i, stdout); 29012112Sralph (void) close(rem); 29112112Sralph } 29212112Sralph } 29312112Sralph 29412112Sralph /* 29512112Sralph * Return 1 if the filename begins with 'cf' 29612112Sralph */ 29712112Sralph iscf(d) 29812112Sralph struct direct *d; 29912112Sralph { 30012112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 30112112Sralph } 302