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