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