1 /* rmjob.c 4.1 83/04/29 */ 2 /* 3 * rmjob - remove the specified jobs from the queue. 4 */ 5 6 #include "lp.h" 7 8 /* 9 * Stuff for handling lprm specifications 10 */ 11 extern char *user[]; /* users to process */ 12 extern int users; /* # of users in user array */ 13 extern int requ[]; /* job number of spool entries */ 14 extern int requests; /* # of spool requests */ 15 16 char *person; /* name of person doing lprm */ 17 char root[] = "root"; 18 int all = 0; /* eliminate all files (root only) */ 19 int cur_daemon; /* daemon's pid */ 20 char current[40]; /* active control file name */ 21 22 int iscf(); 23 24 rmjob() 25 { 26 register int i, nitems; 27 int assasinated = 0; 28 struct direct **files; 29 30 name = "rmjob"; 31 32 if ((i = pgetent(line, printer)) < 0) 33 fatal("cannot open printer description file"); 34 else if (i == 0) 35 fatal("unknown printer"); 36 if ((SD = pgetstr("sd", &bp)) == NULL) 37 SD = DEFSPOOL; 38 if ((LO = pgetstr("lo", &bp)) == NULL) 39 LO = DEFLOCK; 40 if ((LP = pgetstr("lp", &bp)) == NULL) 41 LP = DEFDEVLP; 42 if ((RP = pgetstr("rp", &bp)) == NULL) 43 RP = printer; 44 if ((RM = pgetstr("rm", &bp)) == NULL) 45 RM = host; 46 47 /* 48 * If the format was `lprm -' and the user isn't the super-user, 49 * then fake things to look like he said `lprm user'. 50 */ 51 if (users < 0) { 52 if (getuid() == 0) 53 all = 1; /* all files in local queue */ 54 else { 55 user[0] = person; 56 users = 1; 57 } 58 } 59 if (!strcmp(person, "-all")) { 60 if (from == host) 61 fatal("The login name \"-all\" is reserved"); 62 all = 1; /* all those from 'from' */ 63 person = root; 64 } 65 66 if (chdir(SD) < 0) 67 fatal("cannot chdir to spool directory"); 68 if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 69 fatal("cannot access spool directory"); 70 71 if (nitems) { 72 /* 73 * Check for an active printer daemon (in which case we 74 * kill it if it is reading our file) then remove stuff 75 * (after which we have to restart the daemon). 76 */ 77 if (lockchk(LO) && chk(current)) { 78 assasinated = kill(cur_daemon, SIGINT) == 0; 79 if (!assasinated) 80 fatal("cannot kill printer daemon"); 81 } 82 /* 83 * process the files 84 */ 85 for (i = 0; i < nitems; i++) 86 process(files[i]->d_name); 87 } 88 chkremote(); 89 /* 90 * Restart the printer daemon if it was killed 91 */ 92 if (assasinated && !startdaemon()) 93 fatal("cannot restart printer daemon\n"); 94 exit(0); 95 } 96 97 /* 98 * Process a lock file: collect the pid of the active 99 * daemon and the file name of the active spool entry. 100 * Return boolean indicating existence of a lock file. 101 */ 102 lockchk(s) 103 char *s; 104 { 105 register FILE *fp; 106 register int i, n; 107 108 if ((fp = fopen(s, "r")) == NULL) 109 if (errno == EACCES) 110 fatal("can't access lock file"); 111 else 112 return(0); 113 if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) { 114 (void) fclose(fp); 115 return(0); /* no daemon present */ 116 } 117 cur_daemon = atoi(line); 118 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 119 if (i > 20) { 120 n = 1; 121 break; 122 } 123 sleep(i); 124 } 125 current[n-1] = '\0'; 126 (void) fclose(fp); 127 return(1); 128 } 129 130 /* 131 * Process a control file. 132 */ 133 process(file) 134 char *file; 135 { 136 FILE *cfp; 137 138 if (!chk(file)) 139 return; 140 if ((cfp = fopen(file, "r")) == NULL) 141 fatal("cannot open %s", file); 142 while (getline()) { 143 switch (line[0]) { 144 case 'U': /* unlink associated files */ 145 if (from != host) 146 printf("%s: ", host); 147 printf(unlink(line+1) ? "cannot dequeue %s\n" : 148 "%s dequeued\n", line+1); 149 } 150 } 151 (void) fclose(cfp); 152 if (from != host) 153 printf("%s: ", host); 154 printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file); 155 } 156 157 /* 158 * Do the dirty work in checking 159 */ 160 chk(file) 161 char *file; 162 { 163 register int *r, n; 164 register char **u, *cp; 165 FILE *cfp; 166 167 if (all && (from == host || !strcmp(from, file+6))) 168 return(1); 169 170 /* 171 * get the owner's name from the control file. 172 */ 173 if ((cfp = fopen(file, "r")) == NULL) 174 return(0); 175 while (getline(cfp)) { 176 if (line[0] == 'P') 177 break; 178 } 179 (void) fclose(cfp); 180 if (line[0] != 'P') 181 return(0); 182 183 if (users == 0 && requests == 0) 184 return(!strcmp(file, current) && isowner(line+1, file)); 185 /* 186 * Check the request list 187 */ 188 for (n = 0, cp = file+3; isdigit(*cp); ) 189 n = n * 10 + (*cp++ - '0'); 190 for (r = requ; r < &requ[requests]; r++) 191 if (*r == n && isowner(line+1, file)) 192 return(1); 193 /* 194 * Check to see if it's in the user list 195 */ 196 for (u = user; u < &user[users]; u++) 197 if (!strcmp(*u, line+1) && isowner(line+1, file)) 198 return(1); 199 return(0); 200 } 201 202 /* 203 * If root is removing a file on the local machine, allow it. 204 * If root is removing a file on a remote machine, only allow 205 * files sent from the local machine to be removed. 206 * Normal users can only remove the file from where it was sent. 207 */ 208 isowner(owner, file) 209 char *owner, *file; 210 { 211 register int r; 212 213 if (from == host) 214 r = access(file, 2) == 0; 215 else 216 r = (!strcmp(person, root) || !strcmp(person, owner)) && 217 !strcmp(from, file+6); 218 if (!r) { 219 if (from != host) 220 printf("%s: ", host); 221 printf("%s: Permission denied\n", file); 222 } 223 return(r); 224 } 225 226 /* 227 * Check to see if we are sending files to a remote machine. If we are, 228 * then try removing files on the remote machine. 229 */ 230 chkremote() 231 { 232 register char *cp; 233 register int i, rem; 234 char buf[BUFSIZ]; 235 236 if (*LP || RM == NULL) 237 return; /* not sending to a remote machine */ 238 239 sprintf(buf, "\5%s %s", RP, all ? "-all" : person); 240 cp = buf; 241 for (i = 0; i < users; i++) { 242 cp += strlen(cp); 243 *cp++ = ' '; 244 strcpy(cp, user[i]); 245 } 246 for (i = 0; i < requests; i++) { 247 cp += strlen(cp); 248 (void) sprintf(cp, " %d", requ[i]); 249 } 250 strcat(cp, "\n"); 251 rem = getport(); 252 if (rem < 0) { 253 if (from != host) 254 printf("%s: ", host); 255 printf("connection to %s is down\n", RM); 256 } else { 257 i = strlen(buf); 258 if (write(rem, buf, i) != i) 259 fatal("Lost connection"); 260 while ((i = read(rem, buf, sizeof(buf))) > 0) 261 (void) fwrite(buf, 1, i, stdout); 262 (void) close(rem); 263 } 264 } 265 266 /* 267 * Return 1 if the filename begins with 'cf' 268 */ 269 iscf(d) 270 struct direct *d; 271 { 272 return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 273 } 274