1 /*	rmjob.c	4.3	83/05/18	*/
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 	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())
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 lockchk(s)
100 	char *s;
101 {
102 	register FILE *fp;
103 	register int i, n;
104 
105 	if ((fp = fopen(s, "r")) == NULL)
106 		if (errno == EACCES)
107 			fatal("can't access lock file");
108 		else
109 			return(0);
110 	if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
111 		(void) fclose(fp);
112 		return(0);		/* no daemon present */
113 	}
114 	cur_daemon = atoi(line);
115 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
116 		if (i > 5) {
117 			n = 1;
118 			break;
119 		}
120 		sleep(i);
121 	}
122 	current[n-1] = '\0';
123 	(void) fclose(fp);
124 	return(1);
125 }
126 
127 /*
128  * Process a control file.
129  */
130 process(file)
131 	char *file;
132 {
133 	FILE *cfp;
134 
135 	if (!chk(file))
136 		return;
137 	if ((cfp = fopen(file, "r")) == NULL)
138 		fatal("cannot open %s", file);
139 	while (getline()) {
140 		switch (line[0]) {
141 		case 'U':  /* unlink associated files */
142 			if (from != host)
143 				printf("%s: ", host);
144 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
145 				"%s dequeued\n", line+1);
146 		}
147 	}
148 	(void) fclose(cfp);
149 	if (from != host)
150 		printf("%s: ", host);
151 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
152 }
153 
154 /*
155  * Do the dirty work in checking
156  */
157 chk(file)
158 	char *file;
159 {
160 	register int *r, n;
161 	register char **u, *cp;
162 	FILE *cfp;
163 
164 	if (all && (from == host || !strcmp(from, file+6)))
165 		return(1);
166 
167 	/*
168 	 * get the owner's name from the control file.
169 	 */
170 	if ((cfp = fopen(file, "r")) == NULL)
171 		return(0);
172 	while (getline(cfp)) {
173 		if (line[0] == 'P')
174 			break;
175 	}
176 	(void) fclose(cfp);
177 	if (line[0] != 'P')
178 		return(0);
179 
180 	if (users == 0 && requests == 0)
181 		return(!strcmp(file, current) && isowner(line+1, file));
182 	/*
183 	 * Check the request list
184 	 */
185 	for (n = 0, cp = file+3; isdigit(*cp); )
186 		n = n * 10 + (*cp++ - '0');
187 	for (r = requ; r < &requ[requests]; r++)
188 		if (*r == n && isowner(line+1, file))
189 			return(1);
190 	/*
191 	 * Check to see if it's in the user list
192 	 */
193 	for (u = user; u < &user[users]; u++)
194 		if (!strcmp(*u, line+1) && isowner(line+1, file))
195 			return(1);
196 	return(0);
197 }
198 
199 /*
200  * If root is removing a file on the local machine, allow it.
201  * If root is removing a file from a remote machine, only allow
202  * files sent from the remote machine to be removed.
203  * Normal users can only remove the file from where it was sent.
204  */
205 isowner(owner, file)
206 	char *owner, *file;
207 {
208 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
209 		return(1);
210 	if (!strcmp(person, owner) && !strcmp(from, file+6))
211 		return(1);
212 	if (from != host)
213 		printf("%s: ", host);
214 	printf("%s: Permission denied\n", file);
215 	return(0);
216 }
217 
218 /*
219  * Check to see if we are sending files to a remote machine. If we are,
220  * then try removing files on the remote machine.
221  */
222 chkremote()
223 {
224 	register char *cp;
225 	register int i, rem;
226 	char buf[BUFSIZ];
227 
228 	if (*LP || RM == NULL)
229 		return;	/* not sending to a remote machine */
230 
231 	/*
232 	 * Flush stdout so the user can see what has been deleted
233 	 * while we wait (possibly) for the connection.
234 	 */
235 	fflush(stdout);
236 
237 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
238 	cp = buf;
239 	for (i = 0; i < users; i++) {
240 		cp += strlen(cp);
241 		*cp++ = ' ';
242 		strcpy(cp, user[i]);
243 	}
244 	for (i = 0; i < requests; i++) {
245 		cp += strlen(cp);
246 		(void) sprintf(cp, " %d", requ[i]);
247 	}
248 	strcat(cp, "\n");
249 	rem = getport(RM);
250 	if (rem < 0) {
251 		if (from != host)
252 			printf("%s: ", host);
253 		printf("connection to %s is down\n", RM);
254 	} else {
255 		i = strlen(buf);
256 		if (write(rem, buf, i) != i)
257 			fatal("Lost connection");
258 		while ((i = read(rem, buf, sizeof(buf))) > 0)
259 			(void) fwrite(buf, 1, i, stdout);
260 		(void) close(rem);
261 	}
262 }
263 
264 /*
265  * Return 1 if the filename begins with 'cf'
266  */
267 iscf(d)
268 	struct direct *d;
269 {
270 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
271 }
272