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 634936Sbostic * provided that the above copyright notice and this paragraph are 734936Sbostic * duplicated in all such forms and that any documentation, 834936Sbostic * advertising materials, and other materials related to such 934936Sbostic * distribution and use acknowledge that the software was developed 1034936Sbostic * by the University of California, Berkeley. The name of the 1134936Sbostic * University may not be used to endorse or promote products derived 1234936Sbostic * from this software without specific prior written permission. 1334936Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434936Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534936Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622439Sdist */ 1722439Sdist 1813955Ssam #ifndef lint 19*37968Sbostic static char sccsid[] = "@(#)rmjob.c 5.5 (Berkeley) 05/11/89"; 2034203Sbostic #endif /* not lint */ 2113955Ssam 2212112Sralph /* 2312112Sralph * rmjob - remove the specified jobs from the queue. 2412112Sralph */ 2512112Sralph 2612112Sralph #include "lp.h" 27*37968Sbostic #include "pathnames.h" 2812112Sralph 2912112Sralph /* 3012112Sralph * Stuff for handling lprm specifications 3112112Sralph */ 3212112Sralph extern char *user[]; /* users to process */ 3312112Sralph extern int users; /* # of users in user array */ 3412112Sralph extern int requ[]; /* job number of spool entries */ 3512112Sralph extern int requests; /* # of spool requests */ 3612877Sralph extern char *person; /* name of person doing lprm */ 3712112Sralph 3816759Sralph char root[] = "root"; 3916759Sralph int all = 0; /* eliminate all files (root only) */ 4016759Sralph int cur_daemon; /* daemon's pid */ 4116759Sralph char current[40]; /* active control file name */ 4212112Sralph 4312112Sralph int iscf(); 4412112Sralph 4512112Sralph rmjob() 4612112Sralph { 4712112Sralph register int i, nitems; 4812112Sralph int assasinated = 0; 4912112Sralph struct direct **files; 5012112Sralph 5112112Sralph if ((i = pgetent(line, printer)) < 0) 5212112Sralph fatal("cannot open printer description file"); 5312112Sralph else if (i == 0) 5412112Sralph fatal("unknown printer"); 5512112Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 56*37968Sbostic SD = _PATH_DEFSPOOL; 5712112Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 5812112Sralph LO = DEFLOCK; 5912112Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 60*37968Sbostic LP = _PATH_DEFDEVLP; 6112112Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 6212433Sralph RP = DEFLP; 6312527Sralph RM = pgetstr("rm", &bp); 6436844Stef /* 6536844Stef * Figure out whether the local machine is the same as the remote 6636844Stef * machine entry (if it exists). If not, then ignore the local 6736844Stef * queue information. 6836844Stef */ 6936844Stef if (RM != (char *) NULL) { 7036844Stef char name[256]; 7136844Stef struct hostent *hp; 7212112Sralph 7336844Stef /* get the standard network name of the local host */ 7436844Stef gethostname(name, sizeof(name)); 7536844Stef name[sizeof(name)-1] = '\0'; 7636844Stef hp = gethostbyname(name); 7736844Stef if (hp == (struct hostent *) NULL) { 7836844Stef printf("unable to get network name for local machine %s", 7936844Stef name); 8036844Stef goto localcheck_done; 8136844Stef } else strcpy(name, hp->h_name); 8236844Stef 8336844Stef /* get the standard network name of RM */ 8436844Stef hp = gethostbyname(RM); 8536844Stef if (hp == (struct hostent *) NULL) { 8636844Stef printf("unable to get hostname for remote machine %s", RM); 8736844Stef goto localcheck_done; 8836844Stef } 8936844Stef 9036844Stef /* if printer is not on local machine, ignore LP */ 9136844Stef if (strcmp(name, hp->h_name) != 0) *LP = '\0'; 9236844Stef } 9336844Stef localcheck_done: 9436844Stef 9512112Sralph /* 9612112Sralph * If the format was `lprm -' and the user isn't the super-user, 9712112Sralph * then fake things to look like he said `lprm user'. 9812112Sralph */ 9912112Sralph if (users < 0) { 10012112Sralph if (getuid() == 0) 10112112Sralph all = 1; /* all files in local queue */ 10212112Sralph else { 10312112Sralph user[0] = person; 10412112Sralph users = 1; 10512112Sralph } 10612112Sralph } 10712112Sralph if (!strcmp(person, "-all")) { 10812112Sralph if (from == host) 10912112Sralph fatal("The login name \"-all\" is reserved"); 11012112Sralph all = 1; /* all those from 'from' */ 11112112Sralph person = root; 11212112Sralph } 11312112Sralph 11412112Sralph if (chdir(SD) < 0) 11512112Sralph fatal("cannot chdir to spool directory"); 11612112Sralph if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 11712112Sralph fatal("cannot access spool directory"); 11812112Sralph 11912112Sralph if (nitems) { 12012112Sralph /* 12112112Sralph * Check for an active printer daemon (in which case we 12212112Sralph * kill it if it is reading our file) then remove stuff 12312112Sralph * (after which we have to restart the daemon). 12412112Sralph */ 12512112Sralph if (lockchk(LO) && chk(current)) { 12612112Sralph assasinated = kill(cur_daemon, SIGINT) == 0; 12712112Sralph if (!assasinated) 12812112Sralph fatal("cannot kill printer daemon"); 12912112Sralph } 13012112Sralph /* 13112112Sralph * process the files 13212112Sralph */ 13312112Sralph for (i = 0; i < nitems; i++) 13412112Sralph process(files[i]->d_name); 13512112Sralph } 13612112Sralph chkremote(); 13712112Sralph /* 13812112Sralph * Restart the printer daemon if it was killed 13912112Sralph */ 14015828Sralph if (assasinated && !startdaemon(printer)) 14112112Sralph fatal("cannot restart printer daemon\n"); 14212112Sralph exit(0); 14312112Sralph } 14412112Sralph 14512112Sralph /* 14612112Sralph * Process a lock file: collect the pid of the active 14712112Sralph * daemon and the file name of the active spool entry. 14812112Sralph * Return boolean indicating existence of a lock file. 14912112Sralph */ 15012112Sralph lockchk(s) 15112112Sralph char *s; 15212112Sralph { 15312112Sralph register FILE *fp; 15412112Sralph register int i, n; 15512112Sralph 15612112Sralph if ((fp = fopen(s, "r")) == NULL) 15712112Sralph if (errno == EACCES) 15812112Sralph fatal("can't access lock file"); 15912112Sralph else 16012112Sralph return(0); 16113443Sralph if (!getline(fp)) { 16212112Sralph (void) fclose(fp); 16312112Sralph return(0); /* no daemon present */ 16412112Sralph } 16512112Sralph cur_daemon = atoi(line); 16613443Sralph if (kill(cur_daemon, 0) < 0) { 16713443Sralph (void) fclose(fp); 16813443Sralph return(0); /* no daemon present */ 16913443Sralph } 17012112Sralph for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 17112527Sralph if (i > 5) { 17212112Sralph n = 1; 17312112Sralph break; 17412112Sralph } 17512112Sralph sleep(i); 17612112Sralph } 17712112Sralph current[n-1] = '\0'; 17812112Sralph (void) fclose(fp); 17912112Sralph return(1); 18012112Sralph } 18112112Sralph 18212112Sralph /* 18312112Sralph * Process a control file. 18412112Sralph */ 18512112Sralph process(file) 18612112Sralph char *file; 18712112Sralph { 18812112Sralph FILE *cfp; 18912112Sralph 19012112Sralph if (!chk(file)) 19112112Sralph return; 19212112Sralph if ((cfp = fopen(file, "r")) == NULL) 19312112Sralph fatal("cannot open %s", file); 19416759Sralph while (getline(cfp)) { 19512112Sralph switch (line[0]) { 19612112Sralph case 'U': /* unlink associated files */ 19712112Sralph if (from != host) 19812112Sralph printf("%s: ", host); 19912112Sralph printf(unlink(line+1) ? "cannot dequeue %s\n" : 20012112Sralph "%s dequeued\n", line+1); 20112112Sralph } 20212112Sralph } 20312112Sralph (void) fclose(cfp); 20412112Sralph if (from != host) 20512112Sralph printf("%s: ", host); 20612112Sralph printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 20712112Sralph } 20812112Sralph 20912112Sralph /* 21012112Sralph * Do the dirty work in checking 21112112Sralph */ 21212112Sralph chk(file) 21312112Sralph char *file; 21412112Sralph { 21512112Sralph register int *r, n; 21612112Sralph register char **u, *cp; 21712112Sralph FILE *cfp; 21812112Sralph 21915543Sralph /* 22015543Sralph * Check for valid cf file name (mostly checking current). 22115543Sralph */ 22215543Sralph if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 22315543Sralph return(0); 22415543Sralph 22512112Sralph if (all && (from == host || !strcmp(from, file+6))) 22612112Sralph return(1); 22712112Sralph 22812112Sralph /* 22912112Sralph * get the owner's name from the control file. 23012112Sralph */ 23112112Sralph if ((cfp = fopen(file, "r")) == NULL) 23212112Sralph return(0); 23312112Sralph while (getline(cfp)) { 23412112Sralph if (line[0] == 'P') 23512112Sralph break; 23612112Sralph } 23712112Sralph (void) fclose(cfp); 23812112Sralph if (line[0] != 'P') 23912112Sralph return(0); 24012112Sralph 24112112Sralph if (users == 0 && requests == 0) 24212112Sralph return(!strcmp(file, current) && isowner(line+1, file)); 24312112Sralph /* 24412112Sralph * Check the request list 24512112Sralph */ 24612112Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 24712112Sralph n = n * 10 + (*cp++ - '0'); 24812112Sralph for (r = requ; r < &requ[requests]; r++) 24912112Sralph if (*r == n && isowner(line+1, file)) 25012112Sralph return(1); 25112112Sralph /* 25212112Sralph * Check to see if it's in the user list 25312112Sralph */ 25412112Sralph for (u = user; u < &user[users]; u++) 25512112Sralph if (!strcmp(*u, line+1) && isowner(line+1, file)) 25612112Sralph return(1); 25712112Sralph return(0); 25812112Sralph } 25912112Sralph 26012112Sralph /* 26112112Sralph * If root is removing a file on the local machine, allow it. 26212433Sralph * If root is removing a file from a remote machine, only allow 26312433Sralph * files sent from the remote machine to be removed. 26412112Sralph * Normal users can only remove the file from where it was sent. 26512112Sralph */ 26612112Sralph isowner(owner, file) 26712112Sralph char *owner, *file; 26812112Sralph { 26912433Sralph if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 27012433Sralph return(1); 27112433Sralph if (!strcmp(person, owner) && !strcmp(from, file+6)) 27212433Sralph return(1); 27312433Sralph if (from != host) 27412433Sralph printf("%s: ", host); 27512433Sralph printf("%s: Permission denied\n", file); 27612433Sralph return(0); 27712112Sralph } 27812112Sralph 27912112Sralph /* 28012112Sralph * Check to see if we are sending files to a remote machine. If we are, 28112112Sralph * then try removing files on the remote machine. 28212112Sralph */ 28312112Sralph chkremote() 28412112Sralph { 28512112Sralph register char *cp; 28612112Sralph register int i, rem; 28712112Sralph char buf[BUFSIZ]; 28812112Sralph 28912112Sralph if (*LP || RM == NULL) 29012112Sralph return; /* not sending to a remote machine */ 29112112Sralph 29212433Sralph /* 29312433Sralph * Flush stdout so the user can see what has been deleted 29412433Sralph * while we wait (possibly) for the connection. 29512433Sralph */ 29612433Sralph fflush(stdout); 29712433Sralph 29812112Sralph sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 29912112Sralph cp = buf; 30012112Sralph for (i = 0; i < users; i++) { 30112112Sralph cp += strlen(cp); 30212112Sralph *cp++ = ' '; 30312112Sralph strcpy(cp, user[i]); 30412112Sralph } 30512112Sralph for (i = 0; i < requests; i++) { 30612112Sralph cp += strlen(cp); 30712112Sralph (void) sprintf(cp, " %d", requ[i]); 30812112Sralph } 30912112Sralph strcat(cp, "\n"); 31012527Sralph rem = getport(RM); 31112112Sralph if (rem < 0) { 31212112Sralph if (from != host) 31312112Sralph printf("%s: ", host); 31412112Sralph printf("connection to %s is down\n", RM); 31512112Sralph } else { 31612112Sralph i = strlen(buf); 31712112Sralph if (write(rem, buf, i) != i) 31812112Sralph fatal("Lost connection"); 31912112Sralph while ((i = read(rem, buf, sizeof(buf))) > 0) 32012112Sralph (void) fwrite(buf, 1, i, stdout); 32112112Sralph (void) close(rem); 32212112Sralph } 32312112Sralph } 32412112Sralph 32512112Sralph /* 32612112Sralph * Return 1 if the filename begins with 'cf' 32712112Sralph */ 32812112Sralph iscf(d) 32912112Sralph struct direct *d; 33012112Sralph { 33112112Sralph return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 33212112Sralph } 333