1*13443Sralph /* rmjob.c 4.6 83/06/29 */ 212112Sralph /* 312112Sralph * rmjob - remove the specified jobs from the queue. 412112Sralph */ 512112Sralph 612112Sralph #include "lp.h" 712112Sralph 812112Sralph /* 912112Sralph * Stuff for handling lprm specifications 1012112Sralph */ 1112112Sralph extern char *user[]; /* users to process */ 1212112Sralph extern int users; /* # of users in user array */ 1312112Sralph extern int requ[]; /* job number of spool entries */ 1412112Sralph extern int requests; /* # of spool requests */ 1512877Sralph extern char *person; /* name of person doing lprm */ 1612112Sralph 1712877Sralph static char root[] = "root"; 1812877Sralph static int all = 0; /* eliminate all files (root only) */ 1912877Sralph static int cur_daemon; /* daemon's pid */ 2012877Sralph static char current[40]; /* active control file name */ 2112112Sralph 2212112Sralph int iscf(); 2312112Sralph 2412112Sralph rmjob() 2512112Sralph { 2612112Sralph register int i, nitems; 2712112Sralph int assasinated = 0; 2812112Sralph struct direct **files; 2912112Sralph 3012112Sralph if ((i = pgetent(line, printer)) < 0) 3112112Sralph fatal("cannot open printer description file"); 3212112Sralph else if (i == 0) 3312112Sralph fatal("unknown printer"); 3412112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 3512112Sralph SD = DEFSPOOL; 3612112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 3712112Sralph LO = DEFLOCK; 3812112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 3912112Sralph LP = DEFDEVLP; 4012112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 4112433Sralph RP = DEFLP; 4212527Sralph RM = pgetstr("rm", &bp); 4312112Sralph 4412112Sralph /* 4512112Sralph * If the format was `lprm -' and the user isn't the super-user, 4612112Sralph * then fake things to look like he said `lprm user'. 4712112Sralph */ 4812112Sralph if (users < 0) { 4912112Sralph if (getuid() == 0) 5012112Sralph all = 1; /* all files in local queue */ 5112112Sralph else { 5212112Sralph user[0] = person; 5312112Sralph users = 1; 5412112Sralph } 5512112Sralph } 5612112Sralph if (!strcmp(person, "-all")) { 5712112Sralph if (from == host) 5812112Sralph fatal("The login name \"-all\" is reserved"); 5912112Sralph all = 1; /* all those from 'from' */ 6012112Sralph person = root; 6112112Sralph } 6212112Sralph 6312112Sralph if (chdir(SD) < 0) 6412112Sralph fatal("cannot chdir to spool directory"); 6512112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 6612112Sralph fatal("cannot access spool directory"); 6712112Sralph 6812112Sralph if (nitems) { 6912112Sralph /* 7012112Sralph * Check for an active printer daemon (in which case we 7112112Sralph * kill it if it is reading our file) then remove stuff 7212112Sralph * (after which we have to restart the daemon). 7312112Sralph */ 7412112Sralph if (lockchk(LO) && chk(current)) { 7512112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 7612112Sralph if (!assasinated) 7712112Sralph fatal("cannot kill printer daemon"); 7812112Sralph } 7912112Sralph /* 8012112Sralph * process the files 8112112Sralph */ 8212112Sralph for (i = 0; i < nitems; i++) 8312112Sralph process(files[i]->d_name); 8412112Sralph } 8512112Sralph chkremote(); 8612112Sralph /* 8712112Sralph * Restart the printer daemon if it was killed 8812112Sralph */ 8912877Sralph if (assasinated && !startdaemon(host)) 9012112Sralph fatal("cannot restart printer daemon\n"); 9112112Sralph exit(0); 9212112Sralph } 9312112Sralph 9412112Sralph /* 9512112Sralph * Process a lock file: collect the pid of the active 9612112Sralph * daemon and the file name of the active spool entry. 9712112Sralph * Return boolean indicating existence of a lock file. 9812112Sralph */ 9912877Sralph static 10012112Sralph lockchk(s) 10112112Sralph char *s; 10212112Sralph { 10312112Sralph register FILE *fp; 10412112Sralph register int i, n; 10512112Sralph 10612112Sralph if ((fp = fopen(s, "r")) == NULL) 10712112Sralph if (errno == EACCES) 10812112Sralph fatal("can't access lock file"); 10912112Sralph else 11012112Sralph return(0); 111*13443Sralph if (!getline(fp)) { 11212112Sralph (void) fclose(fp); 11312112Sralph return(0); /* no daemon present */ 11412112Sralph } 11512112Sralph cur_daemon = atoi(line); 116*13443Sralph if (kill(cur_daemon, 0) < 0) { 117*13443Sralph (void) fclose(fp); 118*13443Sralph return(0); /* no daemon present */ 119*13443Sralph } 12012112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 12112527Sralph if (i > 5) { 12212112Sralph n = 1; 12312112Sralph break; 12412112Sralph } 12512112Sralph sleep(i); 12612112Sralph } 12712112Sralph current[n-1] = '\0'; 12812112Sralph (void) fclose(fp); 12912112Sralph return(1); 13012112Sralph } 13112112Sralph 13212112Sralph /* 13312112Sralph * Process a control file. 13412112Sralph */ 13512877Sralph static 13612112Sralph process(file) 13712112Sralph char *file; 13812112Sralph { 13912112Sralph FILE *cfp; 14012112Sralph 14112112Sralph if (!chk(file)) 14212112Sralph return; 14312112Sralph if ((cfp = fopen(file, "r")) == NULL) 14412112Sralph fatal("cannot open %s", file); 14512112Sralph while (getline()) { 14612112Sralph switch (line[0]) { 14712112Sralph case 'U': /* unlink associated files */ 14812112Sralph if (from != host) 14912112Sralph printf("%s: ", host); 15012112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 15112112Sralph "%s dequeued\n", line+1); 15212112Sralph } 15312112Sralph } 15412112Sralph (void) fclose(cfp); 15512112Sralph if (from != host) 15612112Sralph printf("%s: ", host); 15712112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 15812112Sralph } 15912112Sralph 16012112Sralph /* 16112112Sralph * Do the dirty work in checking 16212112Sralph */ 16312877Sralph static 16412112Sralph chk(file) 16512112Sralph char *file; 16612112Sralph { 16712112Sralph register int *r, n; 16812112Sralph register char **u, *cp; 16912112Sralph FILE *cfp; 17012112Sralph 17112112Sralph if (all && (from == host || !strcmp(from, file+6))) 17212112Sralph return(1); 17312112Sralph 17412112Sralph /* 17512112Sralph * get the owner's name from the control file. 17612112Sralph */ 17712112Sralph if ((cfp = fopen(file, "r")) == NULL) 17812112Sralph return(0); 17912112Sralph while (getline(cfp)) { 18012112Sralph if (line[0] == 'P') 18112112Sralph break; 18212112Sralph } 18312112Sralph (void) fclose(cfp); 18412112Sralph if (line[0] != 'P') 18512112Sralph return(0); 18612112Sralph 18712112Sralph if (users == 0 && requests == 0) 18812112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 18912112Sralph /* 19012112Sralph * Check the request list 19112112Sralph */ 19212112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 19312112Sralph n = n * 10 + (*cp++ - '0'); 19412112Sralph for (r = requ; r < &requ[requests]; r++) 19512112Sralph if (*r == n && isowner(line+1, file)) 19612112Sralph return(1); 19712112Sralph /* 19812112Sralph * Check to see if it's in the user list 19912112Sralph */ 20012112Sralph for (u = user; u < &user[users]; u++) 20112112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 20212112Sralph return(1); 20312112Sralph return(0); 20412112Sralph } 20512112Sralph 20612112Sralph /* 20712112Sralph * If root is removing a file on the local machine, allow it. 20812433Sralph * If root is removing a file from a remote machine, only allow 20912433Sralph * files sent from the remote machine to be removed. 21012112Sralph * Normal users can only remove the file from where it was sent. 21112112Sralph */ 21212877Sralph static 21312112Sralph isowner(owner, file) 21412112Sralph char *owner, *file; 21512112Sralph { 21612433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 21712433Sralph return(1); 21812433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 21912433Sralph return(1); 22012433Sralph if (from != host) 22112433Sralph printf("%s: ", host); 22212433Sralph printf("%s: Permission denied\n", file); 22312433Sralph return(0); 22412112Sralph } 22512112Sralph 22612112Sralph /* 22712112Sralph * Check to see if we are sending files to a remote machine. If we are, 22812112Sralph * then try removing files on the remote machine. 22912112Sralph */ 23012877Sralph static 23112112Sralph chkremote() 23212112Sralph { 23312112Sralph register char *cp; 23412112Sralph register int i, rem; 23512112Sralph char buf[BUFSIZ]; 23612112Sralph 23712112Sralph if (*LP || RM == NULL) 23812112Sralph return; /* not sending to a remote machine */ 23912112Sralph 24012433Sralph /* 24112433Sralph * Flush stdout so the user can see what has been deleted 24212433Sralph * while we wait (possibly) for the connection. 24312433Sralph */ 24412433Sralph fflush(stdout); 24512433Sralph 24612112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 24712112Sralph cp = buf; 24812112Sralph for (i = 0; i < users; i++) { 24912112Sralph cp += strlen(cp); 25012112Sralph *cp++ = ' '; 25112112Sralph strcpy(cp, user[i]); 25212112Sralph } 25312112Sralph for (i = 0; i < requests; i++) { 25412112Sralph cp += strlen(cp); 25512112Sralph (void) sprintf(cp, " %d", requ[i]); 25612112Sralph } 25712112Sralph strcat(cp, "\n"); 25812527Sralph rem = getport(RM); 25912112Sralph if (rem < 0) { 26012112Sralph if (from != host) 26112112Sralph printf("%s: ", host); 26212112Sralph printf("connection to %s is down\n", RM); 26312112Sralph } else { 26412112Sralph i = strlen(buf); 26512112Sralph if (write(rem, buf, i) != i) 26612112Sralph fatal("Lost connection"); 26712112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 26812112Sralph (void) fwrite(buf, 1, i, stdout); 26912112Sralph (void) close(rem); 27012112Sralph } 27112112Sralph } 27212112Sralph 27312112Sralph /* 27412112Sralph * Return 1 if the filename begins with 'cf' 27512112Sralph */ 27612877Sralph static 27712112Sralph iscf(d) 27812112Sralph struct direct *d; 27912112Sralph { 28012112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 28112112Sralph } 282