122439Sdist /* 222439Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 5*42801Sbostic * %sccs.include.redist.c% 622439Sdist */ 722439Sdist 813955Ssam #ifndef lint 9*42801Sbostic static char sccsid[] = "@(#)rmjob.c 5.7 (Berkeley) 06/01/90"; 1034203Sbostic #endif /* not lint */ 1113955Ssam 1212112Sralph /* 1312112Sralph * rmjob - remove the specified jobs from the queue. 1412112Sralph */ 1512112Sralph 1612112Sralph #include "lp.h" 1737968Sbostic #include "pathnames.h" 1812112Sralph 1912112Sralph /* 2012112Sralph * Stuff for handling lprm specifications 2112112Sralph */ 2212112Sralph extern char *user[]; /* users to process */ 2312112Sralph extern int users; /* # of users in user array */ 2412112Sralph extern int requ[]; /* job number of spool entries */ 2512112Sralph extern int requests; /* # of spool requests */ 2612877Sralph extern char *person; /* name of person doing lprm */ 2712112Sralph 2816759Sralph char root[] = "root"; 2916759Sralph int all = 0; /* eliminate all files (root only) */ 3016759Sralph int cur_daemon; /* daemon's pid */ 3116759Sralph char current[40]; /* active control file name */ 3212112Sralph 3312112Sralph int iscf(); 3412112Sralph 3512112Sralph rmjob() 3612112Sralph { 3712112Sralph register int i, nitems; 3812112Sralph int assasinated = 0; 3912112Sralph struct direct **files; 4038736Stef char *cp; 4112112Sralph 4212112Sralph if ((i = pgetent(line, printer)) < 0) 4312112Sralph fatal("cannot open printer description file"); 4412112Sralph else if (i == 0) 4512112Sralph fatal("unknown printer"); 4612112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 4737968Sbostic SD = _PATH_DEFSPOOL; 4812112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 4912112Sralph LO = DEFLOCK; 5012112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 5137968Sbostic LP = _PATH_DEFDEVLP; 5212112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 5312433Sralph RP = DEFLP; 5412527Sralph RM = pgetstr("rm", &bp); 5538736Stef if (cp = checkremote()) 5638736Stef printf("Warning: %s\n", cp); 5712112Sralph 5812112Sralph /* 5912112Sralph * If the format was `lprm -' and the user isn't the super-user, 6012112Sralph * then fake things to look like he said `lprm user'. 6112112Sralph */ 6212112Sralph if (users < 0) { 6312112Sralph if (getuid() == 0) 6412112Sralph all = 1; /* all files in local queue */ 6512112Sralph else { 6612112Sralph user[0] = person; 6712112Sralph users = 1; 6812112Sralph } 6912112Sralph } 7012112Sralph if (!strcmp(person, "-all")) { 7112112Sralph if (from == host) 7212112Sralph fatal("The login name \"-all\" is reserved"); 7312112Sralph all = 1; /* all those from 'from' */ 7412112Sralph person = root; 7512112Sralph } 7612112Sralph 7712112Sralph if (chdir(SD) < 0) 7812112Sralph fatal("cannot chdir to spool directory"); 7912112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 8012112Sralph fatal("cannot access spool directory"); 8112112Sralph 8212112Sralph if (nitems) { 8312112Sralph /* 8412112Sralph * Check for an active printer daemon (in which case we 8512112Sralph * kill it if it is reading our file) then remove stuff 8612112Sralph * (after which we have to restart the daemon). 8712112Sralph */ 8812112Sralph if (lockchk(LO) && chk(current)) { 8912112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 9012112Sralph if (!assasinated) 9112112Sralph fatal("cannot kill printer daemon"); 9212112Sralph } 9312112Sralph /* 9412112Sralph * process the files 9512112Sralph */ 9612112Sralph for (i = 0; i < nitems; i++) 9712112Sralph process(files[i]->d_name); 9812112Sralph } 9938736Stef rmremote(); 10012112Sralph /* 10112112Sralph * Restart the printer daemon if it was killed 10212112Sralph */ 10315828Sralph if (assasinated && !startdaemon(printer)) 10412112Sralph fatal("cannot restart printer daemon\n"); 10512112Sralph exit(0); 10612112Sralph } 10712112Sralph 10812112Sralph /* 10912112Sralph * Process a lock file: collect the pid of the active 11012112Sralph * daemon and the file name of the active spool entry. 11112112Sralph * Return boolean indicating existence of a lock file. 11212112Sralph */ 11312112Sralph lockchk(s) 11412112Sralph char *s; 11512112Sralph { 11612112Sralph register FILE *fp; 11712112Sralph register int i, n; 11812112Sralph 11912112Sralph if ((fp = fopen(s, "r")) == NULL) 12012112Sralph if (errno == EACCES) 12112112Sralph fatal("can't access lock file"); 12212112Sralph else 12312112Sralph return(0); 12413443Sralph if (!getline(fp)) { 12512112Sralph (void) fclose(fp); 12612112Sralph return(0); /* no daemon present */ 12712112Sralph } 12812112Sralph cur_daemon = atoi(line); 12913443Sralph if (kill(cur_daemon, 0) < 0) { 13013443Sralph (void) fclose(fp); 13113443Sralph return(0); /* no daemon present */ 13213443Sralph } 13312112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 13412527Sralph if (i > 5) { 13512112Sralph n = 1; 13612112Sralph break; 13712112Sralph } 13812112Sralph sleep(i); 13912112Sralph } 14012112Sralph current[n-1] = '\0'; 14112112Sralph (void) fclose(fp); 14212112Sralph return(1); 14312112Sralph } 14412112Sralph 14512112Sralph /* 14612112Sralph * Process a control file. 14712112Sralph */ 14812112Sralph process(file) 14912112Sralph char *file; 15012112Sralph { 15112112Sralph FILE *cfp; 15212112Sralph 15312112Sralph if (!chk(file)) 15412112Sralph return; 15512112Sralph if ((cfp = fopen(file, "r")) == NULL) 15612112Sralph fatal("cannot open %s", file); 15716759Sralph while (getline(cfp)) { 15812112Sralph switch (line[0]) { 15912112Sralph case 'U': /* unlink associated files */ 16012112Sralph if (from != host) 16112112Sralph printf("%s: ", host); 16212112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 16312112Sralph "%s dequeued\n", line+1); 16412112Sralph } 16512112Sralph } 16612112Sralph (void) fclose(cfp); 16712112Sralph if (from != host) 16812112Sralph printf("%s: ", host); 16912112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 17012112Sralph } 17112112Sralph 17212112Sralph /* 17312112Sralph * Do the dirty work in checking 17412112Sralph */ 17512112Sralph chk(file) 17612112Sralph char *file; 17712112Sralph { 17812112Sralph register int *r, n; 17912112Sralph register char **u, *cp; 18012112Sralph FILE *cfp; 18112112Sralph 18215543Sralph /* 18315543Sralph * Check for valid cf file name (mostly checking current). 18415543Sralph */ 18515543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 18615543Sralph return(0); 18715543Sralph 18812112Sralph if (all && (from == host || !strcmp(from, file+6))) 18912112Sralph return(1); 19012112Sralph 19112112Sralph /* 19212112Sralph * get the owner's name from the control file. 19312112Sralph */ 19412112Sralph if ((cfp = fopen(file, "r")) == NULL) 19512112Sralph return(0); 19612112Sralph while (getline(cfp)) { 19712112Sralph if (line[0] == 'P') 19812112Sralph break; 19912112Sralph } 20012112Sralph (void) fclose(cfp); 20112112Sralph if (line[0] != 'P') 20212112Sralph return(0); 20312112Sralph 20412112Sralph if (users == 0 && requests == 0) 20512112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 20612112Sralph /* 20712112Sralph * Check the request list 20812112Sralph */ 20912112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 21012112Sralph n = n * 10 + (*cp++ - '0'); 21112112Sralph for (r = requ; r < &requ[requests]; r++) 21212112Sralph if (*r == n && isowner(line+1, file)) 21312112Sralph return(1); 21412112Sralph /* 21512112Sralph * Check to see if it's in the user list 21612112Sralph */ 21712112Sralph for (u = user; u < &user[users]; u++) 21812112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 21912112Sralph return(1); 22012112Sralph return(0); 22112112Sralph } 22212112Sralph 22312112Sralph /* 22412112Sralph * If root is removing a file on the local machine, allow it. 22512433Sralph * If root is removing a file from a remote machine, only allow 22612433Sralph * files sent from the remote machine to be removed. 22712112Sralph * Normal users can only remove the file from where it was sent. 22812112Sralph */ 22912112Sralph isowner(owner, file) 23012112Sralph char *owner, *file; 23112112Sralph { 23212433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 23312433Sralph return(1); 23412433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 23512433Sralph return(1); 23612433Sralph if (from != host) 23712433Sralph printf("%s: ", host); 23812433Sralph printf("%s: Permission denied\n", file); 23912433Sralph return(0); 24012112Sralph } 24112112Sralph 24212112Sralph /* 24312112Sralph * Check to see if we are sending files to a remote machine. If we are, 24412112Sralph * then try removing files on the remote machine. 24512112Sralph */ 24638736Stef rmremote() 24712112Sralph { 24812112Sralph register char *cp; 24912112Sralph register int i, rem; 25012112Sralph char buf[BUFSIZ]; 25112112Sralph 25238736Stef if (!sendtorem) 25312112Sralph return; /* not sending to a remote machine */ 25412112Sralph 25512433Sralph /* 25612433Sralph * Flush stdout so the user can see what has been deleted 25712433Sralph * while we wait (possibly) for the connection. 25812433Sralph */ 25912433Sralph fflush(stdout); 26012433Sralph 26112112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 26212112Sralph cp = buf; 26312112Sralph for (i = 0; i < users; i++) { 26412112Sralph cp += strlen(cp); 26512112Sralph *cp++ = ' '; 26612112Sralph strcpy(cp, user[i]); 26712112Sralph } 26812112Sralph for (i = 0; i < requests; i++) { 26912112Sralph cp += strlen(cp); 27012112Sralph (void) sprintf(cp, " %d", requ[i]); 27112112Sralph } 27212112Sralph strcat(cp, "\n"); 27312527Sralph rem = getport(RM); 27412112Sralph if (rem < 0) { 27512112Sralph if (from != host) 27612112Sralph printf("%s: ", host); 27712112Sralph printf("connection to %s is down\n", RM); 27812112Sralph } else { 27912112Sralph i = strlen(buf); 28012112Sralph if (write(rem, buf, i) != i) 28112112Sralph fatal("Lost connection"); 28212112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 28312112Sralph (void) fwrite(buf, 1, i, stdout); 28412112Sralph (void) close(rem); 28512112Sralph } 28612112Sralph } 28712112Sralph 28812112Sralph /* 28912112Sralph * Return 1 if the filename begins with 'cf' 29012112Sralph */ 29112112Sralph iscf(d) 29212112Sralph struct direct *d; 29312112Sralph { 29412112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 29512112Sralph } 296