1*13955Ssam #ifndef lint 2*13955Ssam static char sccsid[] = "@(#)rmjob.c 4.7 (Berkeley) 07/17/83"; 3*13955Ssam #endif 4*13955Ssam 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 2012877Sralph static char root[] = "root"; 2112877Sralph static int all = 0; /* eliminate all files (root only) */ 2212877Sralph static int cur_daemon; /* daemon's pid */ 2312877Sralph static 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 */ 9212877Sralph if (assasinated && !startdaemon(host)) 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 */ 10212877Sralph static 10312112Sralph lockchk(s) 10412112Sralph char *s; 10512112Sralph { 10612112Sralph register FILE *fp; 10712112Sralph register int i, n; 10812112Sralph 10912112Sralph if ((fp = fopen(s, "r")) == NULL) 11012112Sralph if (errno == EACCES) 11112112Sralph fatal("can't access lock file"); 11212112Sralph else 11312112Sralph return(0); 11413443Sralph if (!getline(fp)) { 11512112Sralph (void) fclose(fp); 11612112Sralph return(0); /* no daemon present */ 11712112Sralph } 11812112Sralph cur_daemon = atoi(line); 11913443Sralph if (kill(cur_daemon, 0) < 0) { 12013443Sralph (void) fclose(fp); 12113443Sralph return(0); /* no daemon present */ 12213443Sralph } 12312112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 12412527Sralph if (i > 5) { 12512112Sralph n = 1; 12612112Sralph break; 12712112Sralph } 12812112Sralph sleep(i); 12912112Sralph } 13012112Sralph current[n-1] = '\0'; 13112112Sralph (void) fclose(fp); 13212112Sralph return(1); 13312112Sralph } 13412112Sralph 13512112Sralph /* 13612112Sralph * Process a control file. 13712112Sralph */ 13812877Sralph static 13912112Sralph process(file) 14012112Sralph char *file; 14112112Sralph { 14212112Sralph FILE *cfp; 14312112Sralph 14412112Sralph if (!chk(file)) 14512112Sralph return; 14612112Sralph if ((cfp = fopen(file, "r")) == NULL) 14712112Sralph fatal("cannot open %s", file); 14812112Sralph while (getline()) { 14912112Sralph switch (line[0]) { 15012112Sralph case 'U': /* unlink associated files */ 15112112Sralph if (from != host) 15212112Sralph printf("%s: ", host); 15312112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 15412112Sralph "%s dequeued\n", line+1); 15512112Sralph } 15612112Sralph } 15712112Sralph (void) fclose(cfp); 15812112Sralph if (from != host) 15912112Sralph printf("%s: ", host); 16012112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 16112112Sralph } 16212112Sralph 16312112Sralph /* 16412112Sralph * Do the dirty work in checking 16512112Sralph */ 16612877Sralph static 16712112Sralph chk(file) 16812112Sralph char *file; 16912112Sralph { 17012112Sralph register int *r, n; 17112112Sralph register char **u, *cp; 17212112Sralph FILE *cfp; 17312112Sralph 17412112Sralph if (all && (from == host || !strcmp(from, file+6))) 17512112Sralph return(1); 17612112Sralph 17712112Sralph /* 17812112Sralph * get the owner's name from the control file. 17912112Sralph */ 18012112Sralph if ((cfp = fopen(file, "r")) == NULL) 18112112Sralph return(0); 18212112Sralph while (getline(cfp)) { 18312112Sralph if (line[0] == 'P') 18412112Sralph break; 18512112Sralph } 18612112Sralph (void) fclose(cfp); 18712112Sralph if (line[0] != 'P') 18812112Sralph return(0); 18912112Sralph 19012112Sralph if (users == 0 && requests == 0) 19112112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 19212112Sralph /* 19312112Sralph * Check the request list 19412112Sralph */ 19512112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 19612112Sralph n = n * 10 + (*cp++ - '0'); 19712112Sralph for (r = requ; r < &requ[requests]; r++) 19812112Sralph if (*r == n && isowner(line+1, file)) 19912112Sralph return(1); 20012112Sralph /* 20112112Sralph * Check to see if it's in the user list 20212112Sralph */ 20312112Sralph for (u = user; u < &user[users]; u++) 20412112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 20512112Sralph return(1); 20612112Sralph return(0); 20712112Sralph } 20812112Sralph 20912112Sralph /* 21012112Sralph * If root is removing a file on the local machine, allow it. 21112433Sralph * If root is removing a file from a remote machine, only allow 21212433Sralph * files sent from the remote machine to be removed. 21312112Sralph * Normal users can only remove the file from where it was sent. 21412112Sralph */ 21512877Sralph static 21612112Sralph isowner(owner, file) 21712112Sralph char *owner, *file; 21812112Sralph { 21912433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 22012433Sralph return(1); 22112433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 22212433Sralph return(1); 22312433Sralph if (from != host) 22412433Sralph printf("%s: ", host); 22512433Sralph printf("%s: Permission denied\n", file); 22612433Sralph return(0); 22712112Sralph } 22812112Sralph 22912112Sralph /* 23012112Sralph * Check to see if we are sending files to a remote machine. If we are, 23112112Sralph * then try removing files on the remote machine. 23212112Sralph */ 23312877Sralph static 23412112Sralph chkremote() 23512112Sralph { 23612112Sralph register char *cp; 23712112Sralph register int i, rem; 23812112Sralph char buf[BUFSIZ]; 23912112Sralph 24012112Sralph if (*LP || RM == NULL) 24112112Sralph return; /* not sending to a remote machine */ 24212112Sralph 24312433Sralph /* 24412433Sralph * Flush stdout so the user can see what has been deleted 24512433Sralph * while we wait (possibly) for the connection. 24612433Sralph */ 24712433Sralph fflush(stdout); 24812433Sralph 24912112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 25012112Sralph cp = buf; 25112112Sralph for (i = 0; i < users; i++) { 25212112Sralph cp += strlen(cp); 25312112Sralph *cp++ = ' '; 25412112Sralph strcpy(cp, user[i]); 25512112Sralph } 25612112Sralph for (i = 0; i < requests; i++) { 25712112Sralph cp += strlen(cp); 25812112Sralph (void) sprintf(cp, " %d", requ[i]); 25912112Sralph } 26012112Sralph strcat(cp, "\n"); 26112527Sralph rem = getport(RM); 26212112Sralph if (rem < 0) { 26312112Sralph if (from != host) 26412112Sralph printf("%s: ", host); 26512112Sralph printf("connection to %s is down\n", RM); 26612112Sralph } else { 26712112Sralph i = strlen(buf); 26812112Sralph if (write(rem, buf, i) != i) 26912112Sralph fatal("Lost connection"); 27012112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 27112112Sralph (void) fwrite(buf, 1, i, stdout); 27212112Sralph (void) close(rem); 27312112Sralph } 27412112Sralph } 27512112Sralph 27612112Sralph /* 27712112Sralph * Return 1 if the filename begins with 'cf' 27812112Sralph */ 27912877Sralph static 28012112Sralph iscf(d) 28112112Sralph struct direct *d; 28212112Sralph { 28312112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 28412112Sralph } 285