1*12877Sralph /* rmjob.c 4.4 83/06/02 */ 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 */ 15*12877Sralph extern char *person; /* name of person doing lprm */ 1612112Sralph 17*12877Sralph static char root[] = "root"; 18*12877Sralph static int all = 0; /* eliminate all files (root only) */ 19*12877Sralph static int cur_daemon; /* daemon's pid */ 20*12877Sralph 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 */ 89*12877Sralph 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 */ 99*12877Sralph 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); 11112112Sralph if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) { 11212112Sralph (void) fclose(fp); 11312112Sralph return(0); /* no daemon present */ 11412112Sralph } 11512112Sralph cur_daemon = atoi(line); 11612112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 11712527Sralph if (i > 5) { 11812112Sralph n = 1; 11912112Sralph break; 12012112Sralph } 12112112Sralph sleep(i); 12212112Sralph } 12312112Sralph current[n-1] = '\0'; 12412112Sralph (void) fclose(fp); 12512112Sralph return(1); 12612112Sralph } 12712112Sralph 12812112Sralph /* 12912112Sralph * Process a control file. 13012112Sralph */ 131*12877Sralph static 13212112Sralph process(file) 13312112Sralph char *file; 13412112Sralph { 13512112Sralph FILE *cfp; 13612112Sralph 13712112Sralph if (!chk(file)) 13812112Sralph return; 13912112Sralph if ((cfp = fopen(file, "r")) == NULL) 14012112Sralph fatal("cannot open %s", file); 14112112Sralph while (getline()) { 14212112Sralph switch (line[0]) { 14312112Sralph case 'U': /* unlink associated files */ 14412112Sralph if (from != host) 14512112Sralph printf("%s: ", host); 14612112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 14712112Sralph "%s dequeued\n", line+1); 14812112Sralph } 14912112Sralph } 15012112Sralph (void) fclose(cfp); 15112112Sralph if (from != host) 15212112Sralph printf("%s: ", host); 15312112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 15412112Sralph } 15512112Sralph 15612112Sralph /* 15712112Sralph * Do the dirty work in checking 15812112Sralph */ 159*12877Sralph static 16012112Sralph chk(file) 16112112Sralph char *file; 16212112Sralph { 16312112Sralph register int *r, n; 16412112Sralph register char **u, *cp; 16512112Sralph FILE *cfp; 16612112Sralph 16712112Sralph if (all && (from == host || !strcmp(from, file+6))) 16812112Sralph return(1); 16912112Sralph 17012112Sralph /* 17112112Sralph * get the owner's name from the control file. 17212112Sralph */ 17312112Sralph if ((cfp = fopen(file, "r")) == NULL) 17412112Sralph return(0); 17512112Sralph while (getline(cfp)) { 17612112Sralph if (line[0] == 'P') 17712112Sralph break; 17812112Sralph } 17912112Sralph (void) fclose(cfp); 18012112Sralph if (line[0] != 'P') 18112112Sralph return(0); 18212112Sralph 18312112Sralph if (users == 0 && requests == 0) 18412112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 18512112Sralph /* 18612112Sralph * Check the request list 18712112Sralph */ 18812112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 18912112Sralph n = n * 10 + (*cp++ - '0'); 19012112Sralph for (r = requ; r < &requ[requests]; r++) 19112112Sralph if (*r == n && isowner(line+1, file)) 19212112Sralph return(1); 19312112Sralph /* 19412112Sralph * Check to see if it's in the user list 19512112Sralph */ 19612112Sralph for (u = user; u < &user[users]; u++) 19712112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 19812112Sralph return(1); 19912112Sralph return(0); 20012112Sralph } 20112112Sralph 20212112Sralph /* 20312112Sralph * If root is removing a file on the local machine, allow it. 20412433Sralph * If root is removing a file from a remote machine, only allow 20512433Sralph * files sent from the remote machine to be removed. 20612112Sralph * Normal users can only remove the file from where it was sent. 20712112Sralph */ 208*12877Sralph static 20912112Sralph isowner(owner, file) 21012112Sralph char *owner, *file; 21112112Sralph { 21212433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 21312433Sralph return(1); 21412433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 21512433Sralph return(1); 21612433Sralph if (from != host) 21712433Sralph printf("%s: ", host); 21812433Sralph printf("%s: Permission denied\n", file); 21912433Sralph return(0); 22012112Sralph } 22112112Sralph 22212112Sralph /* 22312112Sralph * Check to see if we are sending files to a remote machine. If we are, 22412112Sralph * then try removing files on the remote machine. 22512112Sralph */ 226*12877Sralph static 22712112Sralph chkremote() 22812112Sralph { 22912112Sralph register char *cp; 23012112Sralph register int i, rem; 23112112Sralph char buf[BUFSIZ]; 23212112Sralph 23312112Sralph if (*LP || RM == NULL) 23412112Sralph return; /* not sending to a remote machine */ 23512112Sralph 23612433Sralph /* 23712433Sralph * Flush stdout so the user can see what has been deleted 23812433Sralph * while we wait (possibly) for the connection. 23912433Sralph */ 24012433Sralph fflush(stdout); 24112433Sralph 24212112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 24312112Sralph cp = buf; 24412112Sralph for (i = 0; i < users; i++) { 24512112Sralph cp += strlen(cp); 24612112Sralph *cp++ = ' '; 24712112Sralph strcpy(cp, user[i]); 24812112Sralph } 24912112Sralph for (i = 0; i < requests; i++) { 25012112Sralph cp += strlen(cp); 25112112Sralph (void) sprintf(cp, " %d", requ[i]); 25212112Sralph } 25312112Sralph strcat(cp, "\n"); 25412527Sralph rem = getport(RM); 25512112Sralph if (rem < 0) { 25612112Sralph if (from != host) 25712112Sralph printf("%s: ", host); 25812112Sralph printf("connection to %s is down\n", RM); 25912112Sralph } else { 26012112Sralph i = strlen(buf); 26112112Sralph if (write(rem, buf, i) != i) 26212112Sralph fatal("Lost connection"); 26312112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 26412112Sralph (void) fwrite(buf, 1, i, stdout); 26512112Sralph (void) close(rem); 26612112Sralph } 26712112Sralph } 26812112Sralph 26912112Sralph /* 27012112Sralph * Return 1 if the filename begins with 'cf' 27112112Sralph */ 272*12877Sralph static 27312112Sralph iscf(d) 27412112Sralph struct direct *d; 27512112Sralph { 27612112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 27712112Sralph } 278