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