pcnfsd_print.c revision 1.3 1 1.3 gwr /* $NetBSD: pcnfsd_print.c,v 1.3 1995/08/14 19:45:18 gwr Exp $ */
2 1.2 gwr
3 1.1 jtc /* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_print.c 1.7 92/01/24 19:58:58 SMI */
4 1.1 jtc /*
5 1.1 jtc **=====================================================================
6 1.1 jtc ** Copyright (c) 1986,1987,1988,1989,1990,1991 by Sun Microsystems, Inc.
7 1.1 jtc ** @(#)pcnfsd_print.c 1.7 1/24/92
8 1.1 jtc **=====================================================================
9 1.1 jtc */
10 1.1 jtc #include "common.h"
11 1.1 jtc /*
12 1.1 jtc **=====================================================================
13 1.1 jtc ** I N C L U D E F I L E S E C T I O N *
14 1.1 jtc ** *
15 1.1 jtc ** If your port requires different include files, add a suitable *
16 1.1 jtc ** #define in the customization section, and make the inclusion or *
17 1.1 jtc ** exclusion of the files conditional on this. *
18 1.1 jtc **=====================================================================
19 1.1 jtc */
20 1.1 jtc #include "pcnfsd.h"
21 1.1 jtc #include <malloc.h>
22 1.1 jtc #include <stdio.h>
23 1.1 jtc #include <pwd.h>
24 1.1 jtc #include <sys/file.h>
25 1.1 jtc #include <signal.h>
26 1.1 jtc #include <sys/stat.h>
27 1.1 jtc #include <sys/ioctl.h>
28 1.1 jtc #include <netdb.h>
29 1.1 jtc #include <errno.h>
30 1.1 jtc #include <string.h>
31 1.1 jtc
32 1.1 jtc #ifndef SYSV
33 1.1 jtc #include <sys/wait.h>
34 1.1 jtc #endif
35 1.1 jtc
36 1.1 jtc #ifdef ISC_2_0
37 1.1 jtc #include <sys/fcntl.h>
38 1.1 jtc #endif
39 1.1 jtc
40 1.1 jtc #ifdef SHADOW_SUPPORT
41 1.1 jtc #include <shadow.h>
42 1.1 jtc #endif
43 1.1 jtc
44 1.3 gwr #include "paths.h"
45 1.3 gwr
46 1.1 jtc /*
47 1.1 jtc **---------------------------------------------------------------------
48 1.1 jtc ** Other #define's
49 1.1 jtc **---------------------------------------------------------------------
50 1.1 jtc */
51 1.1 jtc #ifndef MAXPATHLEN
52 1.1 jtc #define MAXPATHLEN 1024
53 1.1 jtc #endif
54 1.1 jtc
55 1.1 jtc /*
56 1.1 jtc ** The following defintions give the maximum time allowed for
57 1.1 jtc ** an external command to run (in seconds)
58 1.1 jtc */
59 1.1 jtc #define MAXTIME_FOR_PRINT 10
60 1.1 jtc #define MAXTIME_FOR_QUEUE 10
61 1.1 jtc #define MAXTIME_FOR_CANCEL 10
62 1.1 jtc #define MAXTIME_FOR_STATUS 10
63 1.1 jtc
64 1.1 jtc #define QMAX 50
65 1.1 jtc
66 1.1 jtc /*
67 1.1 jtc ** The following is derived from ucb/lpd/displayq.c
68 1.1 jtc */
69 1.1 jtc #define SIZECOL 62
70 1.1 jtc #define FILECOL 24
71 1.1 jtc
72 1.1 jtc extern void scramble();
73 1.1 jtc extern void run_ps630();
74 1.1 jtc extern char *crypt();
75 1.1 jtc extern FILE *su_popen();
76 1.1 jtc extern int su_pclose();
77 1.1 jtc int build_pr_list();
78 1.1 jtc char *map_printer_name();
79 1.1 jtc char *expand_alias();
80 1.1 jtc void *grab();
81 1.1 jtc void free_pr_list_item();
82 1.1 jtc void free_pr_queue_item();
83 1.1 jtc pr_list list_virtual_printers();
84 1.1 jtc
85 1.1 jtc /*
86 1.1 jtc **---------------------------------------------------------------------
87 1.1 jtc ** Misc. variable definitions
88 1.1 jtc **---------------------------------------------------------------------
89 1.1 jtc */
90 1.1 jtc
91 1.1 jtc extern int errno;
92 1.1 jtc extern int interrupted; /* in pcnfsd_misc.c */
93 1.1 jtc struct stat statbuf;
94 1.1 jtc char pathname[MAXPATHLEN];
95 1.1 jtc char new_pathname[MAXPATHLEN];
96 1.1 jtc char sp_name[MAXPATHLEN] = SPOOLDIR;
97 1.1 jtc char tempstr[256];
98 1.1 jtc char delims[] = " \t\r\n:()";
99 1.1 jtc
100 1.1 jtc pr_list printers = NULL;
101 1.1 jtc pr_queue queue = NULL;
102 1.1 jtc
103 1.1 jtc /*
104 1.1 jtc **=====================================================================
105 1.1 jtc ** C O D E S E C T I O N *
106 1.1 jtc **=====================================================================
107 1.1 jtc */
108 1.1 jtc
109 1.1 jtc /*
110 1.1 jtc * This is the latest word on the security check. The following
111 1.1 jtc * routine "suspicious()" returns non-zero if the character string
112 1.1 jtc * passed to it contains any shell metacharacters.
113 1.1 jtc * Callers will typically code
114 1.1 jtc *
115 1.1 jtc * if(suspicious(some_parameter)) reject();
116 1.1 jtc */
117 1.1 jtc
118 1.1 jtc int suspicious (s)
119 1.1 jtc char *s;
120 1.1 jtc {
121 1.1 jtc if(strpbrk(pathname, ";|&<>`'#!?*()[]^") != NULL)
122 1.1 jtc return 1;
123 1.1 jtc return 0;
124 1.1 jtc }
125 1.1 jtc
126 1.1 jtc
127 1.1 jtc int
128 1.1 jtc valid_pr(pr)
129 1.1 jtc char *pr;
130 1.1 jtc {
131 1.1 jtc char *p;
132 1.1 jtc pr_list curr;
133 1.1 jtc if(printers == NULL)
134 1.1 jtc build_pr_list();
135 1.1 jtc
136 1.1 jtc if(printers == NULL)
137 1.1 jtc return(1); /* can't tell - assume it's good */
138 1.1 jtc
139 1.1 jtc p = map_printer_name(pr);
140 1.1 jtc if (p == NULL)
141 1.1 jtc return(1); /* must be ok is maps to NULL! */
142 1.1 jtc curr = printers;
143 1.1 jtc while(curr) {
144 1.1 jtc if(!strcmp(p, curr->pn))
145 1.1 jtc return(1);
146 1.1 jtc curr = curr->pr_next;
147 1.1 jtc }
148 1.1 jtc
149 1.1 jtc return(0);
150 1.1 jtc }
151 1.1 jtc
152 1.1 jtc pirstat pr_init(sys, pr, sp)
153 1.1 jtc char *sys;
154 1.1 jtc char *pr;
155 1.1 jtc char**sp;
156 1.1 jtc {
157 1.1 jtc int dir_mode = 0777;
158 1.1 jtc int rc;
159 1.1 jtc
160 1.1 jtc *sp = &pathname[0];
161 1.1 jtc pathname[0] = '\0';
162 1.1 jtc
163 1.1 jtc if(suspicious(sys) || suspicious(pr))
164 1.1 jtc return(PI_RES_FAIL);
165 1.1 jtc
166 1.1 jtc /* get pathname of current directory and return to client */
167 1.1 jtc
168 1.1 jtc (void)sprintf(pathname,"%s/%s",sp_name, sys);
169 1.1 jtc (void)mkdir(sp_name, dir_mode); /* ignore the return code */
170 1.1 jtc (void)chmod(sp_name, dir_mode);
171 1.1 jtc rc = mkdir(pathname, dir_mode); /* DON'T ignore this return code */
172 1.1 jtc if((rc < 0 && errno != EEXIST) ||
173 1.1 jtc (chmod(pathname, dir_mode) != 0) ||
174 1.1 jtc (stat(pathname, &statbuf) != 0) ||
175 1.1 jtc !(statbuf.st_mode & S_IFDIR)) {
176 1.1 jtc (void)sprintf(tempstr,
177 1.1 jtc "rpc.pcnfsd: unable to set up spool directory %s\n",
178 1.1 jtc pathname);
179 1.1 jtc msg_out(tempstr);
180 1.1 jtc pathname[0] = '\0'; /* null to tell client bad vibes */
181 1.1 jtc return(PI_RES_FAIL);
182 1.1 jtc }
183 1.1 jtc if (!valid_pr(pr))
184 1.1 jtc {
185 1.1 jtc pathname[0] = '\0'; /* null to tell client bad vibes */
186 1.1 jtc return(PI_RES_NO_SUCH_PRINTER);
187 1.1 jtc }
188 1.1 jtc return(PI_RES_OK);
189 1.1 jtc
190 1.1 jtc }
191 1.1 jtc
192 1.1 jtc
194 1.1 jtc psrstat pr_start2(system, pr, user, fname, opts, id)
195 1.1 jtc char *system;
196 1.1 jtc char *pr;
197 1.1 jtc char *user;
198 1.1 jtc char *fname;
199 1.1 jtc char *opts;
200 1.1 jtc char **id;
201 1.1 jtc {
202 1.1 jtc char snum[20];
203 1.1 jtc static char req_id[256];
204 1.1 jtc char cmdbuf[256];
205 1.1 jtc char resbuf[256];
206 1.1 jtc FILE *fd;
207 1.1 jtc int i;
208 1.1 jtc char *xcmd;
209 1.1 jtc char *cp;
210 1.1 jtc int failed = 0;
211 1.1 jtc
212 1.1 jtc #ifdef HACK_FOR_ROTATED_TRANSCRIPT
213 1.1 jtc char scratch[512];
214 1.1 jtc #endif
215 1.1 jtc
216 1.1 jtc
217 1.1 jtc if(suspicious(system) ||
218 1.1 jtc suspicious(pr) ||
219 1.1 jtc suspicious(user) ||
220 1.1 jtc suspicious(fname))
221 1.1 jtc return(PS_RES_FAIL);
222 1.1 jtc
223 1.1 jtc (void)sprintf(pathname,"%s/%s/%s",sp_name,
224 1.1 jtc system,
225 1.1 jtc fname);
226 1.1 jtc
227 1.1 jtc *id = &req_id[0];
228 1.1 jtc req_id[0] = '\0';
229 1.1 jtc
230 1.1 jtc if (stat(pathname, &statbuf))
231 1.1 jtc {
232 1.1 jtc /*
233 1.1 jtc **-----------------------------------------------------------------
234 1.1 jtc ** We can't stat the file. Let's try appending '.spl' and
235 1.1 jtc ** see if it's already in progress.
236 1.1 jtc **-----------------------------------------------------------------
237 1.1 jtc */
238 1.1 jtc
239 1.1 jtc (void)strcat(pathname, ".spl");
240 1.1 jtc if (stat(pathname, &statbuf))
241 1.1 jtc {
242 1.1 jtc /*
243 1.1 jtc **----------------------------------------------------------------
244 1.1 jtc ** It really doesn't exist.
245 1.1 jtc **----------------------------------------------------------------
246 1.1 jtc */
247 1.1 jtc
248 1.1 jtc
249 1.1 jtc return(PS_RES_NO_FILE);
250 1.1 jtc }
251 1.1 jtc /*
252 1.1 jtc **-------------------------------------------------------------
253 1.1 jtc ** It is already on the way.
254 1.1 jtc **-------------------------------------------------------------
255 1.1 jtc */
256 1.1 jtc
257 1.1 jtc
258 1.1 jtc return(PS_RES_ALREADY);
259 1.1 jtc }
260 1.1 jtc
261 1.1 jtc if (statbuf.st_size == 0)
262 1.1 jtc {
263 1.1 jtc /*
264 1.1 jtc **-------------------------------------------------------------
265 1.1 jtc ** Null file - don't print it, just kill it.
266 1.1 jtc **-------------------------------------------------------------
267 1.1 jtc */
268 1.1 jtc (void)unlink(pathname);
269 1.1 jtc
270 1.1 jtc return(PS_RES_NULL);
271 1.1 jtc }
272 1.1 jtc /*
273 1.1 jtc **-------------------------------------------------------------
274 1.1 jtc ** The file is real, has some data, and is not already going out.
275 1.1 jtc ** We rename it by appending '.spl' and exec "lpr" to do the
276 1.1 jtc ** actual work.
277 1.1 jtc **-------------------------------------------------------------
278 1.1 jtc */
279 1.1 jtc (void)strcpy(new_pathname, pathname);
280 1.1 jtc (void)strcat(new_pathname, ".spl");
281 1.1 jtc
282 1.1 jtc /*
283 1.1 jtc **-------------------------------------------------------------
284 1.1 jtc ** See if the new filename exists so as not to overwrite it.
285 1.1 jtc **-------------------------------------------------------------
286 1.1 jtc */
287 1.1 jtc
288 1.1 jtc
289 1.1 jtc if (!stat(new_pathname, &statbuf))
290 1.1 jtc {
291 1.1 jtc (void)strcpy(new_pathname, pathname); /* rebuild a new name */
292 1.1 jtc (void)sprintf(snum, "%d", rand()); /* get some number */
293 1.1 jtc (void)strncat(new_pathname, snum, 3);
294 1.1 jtc (void)strcat(new_pathname, ".spl"); /* new spool file */
295 1.1 jtc }
296 1.1 jtc if (rename(pathname, new_pathname))
297 1.1 jtc {
298 1.1 jtc /*
299 1.1 jtc **---------------------------------------------------------------
300 1.1 jtc ** Should never happen.
301 1.1 jtc **---------------------------------------------------------------
302 1.1 jtc */
303 1.1 jtc (void)sprintf(tempstr, "rpc.pcnfsd: spool file rename (%s->%s) failed.\n",
304 1.1 jtc pathname, new_pathname);
305 1.1 jtc msg_out(tempstr);
306 1.1 jtc return(PS_RES_FAIL);
307 1.1 jtc }
308 1.1 jtc
309 1.1 jtc if (*opts == 'd')
310 1.1 jtc {
311 1.1 jtc /*
312 1.1 jtc **------------------------------------------------------
313 1.1 jtc ** This is a Diablo print stream. Apply the ps630
314 1.1 jtc ** filter with the appropriate arguments.
315 1.1 jtc **------------------------------------------------------
316 1.1 jtc */
317 1.1 jtc (void)run_ps630(new_pathname, opts);
318 1.1 jtc }
319 1.1 jtc /*
320 1.1 jtc ** Try to match to an aliased printer
321 1.1 jtc */
322 1.3 gwr xcmd = expand_alias(pr, new_pathname, user, system);
323 1.3 gwr if(!xcmd) {
324 1.1 jtc #ifdef SVR4
325 1.3 gwr /*
326 1.3 gwr * Use the copy option so we can remove the orignal
327 1.3 gwr * spooled nfs file from the spool directory.
328 1.3 gwr */
329 1.3 gwr sprintf(cmdbuf, "/usr/bin/lp -c -d%s %s",
330 1.3 gwr pr, new_pathname);
331 1.3 gwr #else /* SVR4 */
332 1.1 jtc /* BSD way: lpr */
333 1.1 jtc sprintf(cmdbuf, "%s/lpr -P%s %s",
334 1.3 gwr LPRDIR, pr, new_pathname);
335 1.1 jtc #endif /* SVR4 */
336 1.1 jtc xcmd = cmdbuf;
337 1.1 jtc }
338 1.1 jtc if ((fd = su_popen(user, xcmd, MAXTIME_FOR_PRINT)) == NULL) {
339 1.1 jtc msg_out("rpc.pcnfsd: su_popen failed");
340 1.1 jtc return(PS_RES_FAIL);
341 1.1 jtc }
342 1.1 jtc req_id[0] = '\0'; /* asume failure */
343 1.1 jtc while(fgets(resbuf, 255, fd) != NULL) {
344 1.1 jtc i = strlen(resbuf);
345 1.1 jtc if(i)
346 1.1 jtc resbuf[i-1] = '\0'; /* trim NL */
347 1.1 jtc if(!strncmp(resbuf, "request id is ", 14))
348 1.1 jtc /* New - just the first word is needed */
349 1.1 jtc strcpy(req_id, strtok(&resbuf[14], delims));
350 1.1 jtc else if (strembedded("disabled", resbuf))
351 1.1 jtc failed = 1;
352 1.1 jtc }
353 1.1 jtc if(su_pclose(fd) == 255)
354 1.1 jtc msg_out("rpc.pcnfsd: su_pclose alert");
355 1.1 jtc (void)unlink(new_pathname);
356 1.1 jtc return((failed | interrupted)? PS_RES_FAIL : PS_RES_OK);
357 1.1 jtc }
358 1.1 jtc
359 1.1 jtc
361 1.3 gwr /*
362 1.1 jtc * build_pr_list: determine which printers are valid.
363 1.3 gwr * on SVR4 use "lpstat -v"
364 1.3 gwr * on BSD use "lpc status"
365 1.3 gwr */
366 1.3 gwr
367 1.3 gwr #ifdef SVR4
368 1.3 gwr /*
369 1.3 gwr * In SVR4 the command to determine which printers are
370 1.3 gwr * valid is lpstat -v. The output is something like this:
371 1.3 gwr *
372 1.3 gwr * device for lp: /dev/lp0
373 1.3 gwr * system for pcdslw: hinode
374 1.3 gwr * system for bletch: hinode (as printer hisname)
375 1.3 gwr *
376 1.3 gwr * On SunOS using the SysV compatibility package, the output
377 1.3 gwr * is more like:
378 1.3 gwr *
379 1.3 gwr * device for lp is /dev/lp0
380 1.3 gwr * device for pcdslw is the remote printer pcdslw on hinode
381 1.3 gwr * device for bletch is the remote printer hisname on hinode
382 1.3 gwr *
383 1.3 gwr * It is fairly simple to create logic that will handle either
384 1.3 gwr * possibility:
385 1.3 gwr */
386 1.3 gwr int
387 1.3 gwr build_pr_list()
388 1.3 gwr {
389 1.3 gwr pr_list last = NULL;
390 1.3 gwr pr_list curr = NULL;
391 1.3 gwr char buff[256];
392 1.3 gwr FILE *p;
393 1.3 gwr char *cp;
394 1.3 gwr int saw_system;
395 1.3 gwr
396 1.3 gwr p = popen("lpstat -v", "r");
397 1.3 gwr if(p == NULL) {
398 1.3 gwr msg_out("rpc.pcnfsd: unable to popen() lp status");
399 1.3 gwr return(0);
400 1.3 gwr }
401 1.3 gwr
402 1.3 gwr while(fgets(buff, 255, p) != NULL) {
403 1.3 gwr cp = strtok(buff, delims);
404 1.3 gwr if(!cp)
405 1.3 gwr continue;
406 1.3 gwr if(!strcmp(cp, "device"))
407 1.3 gwr saw_system = 0;
408 1.3 gwr else if (!strcmp(cp, "system"))
409 1.3 gwr saw_system = 1;
410 1.3 gwr else
411 1.3 gwr continue;
412 1.3 gwr cp = strtok(NULL, delims);
413 1.3 gwr if(!cp || strcmp(cp, "for"))
414 1.3 gwr continue;
415 1.3 gwr cp = strtok(NULL, delims);
416 1.3 gwr if(!cp)
417 1.3 gwr continue;
418 1.3 gwr curr = (struct pr_list_item *)
419 1.3 gwr grab(sizeof (struct pr_list_item));
420 1.3 gwr
421 1.3 gwr curr->pn = strdup(cp);
422 1.3 gwr curr->device = NULL;
423 1.3 gwr curr->remhost = NULL;
424 1.3 gwr curr->cm = strdup("-");
425 1.3 gwr curr->pr_next = NULL;
426 1.3 gwr
427 1.3 gwr cp = strtok(NULL, delims);
428 1.3 gwr
429 1.3 gwr if(cp && !strcmp(cp, "is"))
430 1.3 gwr cp = strtok(NULL, delims);
431 1.3 gwr
432 1.3 gwr if(!cp) {
433 1.3 gwr free_pr_list_item(curr);
434 1.3 gwr continue;
435 1.3 gwr }
436 1.3 gwr
437 1.3 gwr if(saw_system) {
438 1.3 gwr /* "system" OR "system (as printer pname)" */
439 1.3 gwr curr->remhost = strdup(cp);
440 1.3 gwr cp = strtok(NULL, delims);
441 1.3 gwr if(!cp) {
442 1.3 gwr /* simple format */
443 1.3 gwr curr->device = strdup(curr->pn);
444 1.3 gwr } else {
445 1.3 gwr /* "sys (as printer pname)" */
446 1.3 gwr if (strcmp(cp, "as")) {
447 1.3 gwr free_pr_list_item(curr);
448 1.3 gwr continue;
449 1.3 gwr }
450 1.3 gwr cp = strtok(NULL, delims);
451 1.3 gwr if (!cp || strcmp(cp, "printer")) {
452 1.3 gwr free_pr_list_item(curr);
453 1.3 gwr continue;
454 1.3 gwr }
455 1.3 gwr cp = strtok(NULL, delims);
456 1.3 gwr if(!cp) {
457 1.3 gwr free_pr_list_item(curr);
458 1.3 gwr continue;
459 1.3 gwr }
460 1.3 gwr curr->device = strdup(cp);
461 1.3 gwr }
462 1.3 gwr }
463 1.3 gwr else if(!strcmp(cp, "the")) {
464 1.3 gwr /* start of "the remote printer foo on bar" */
465 1.3 gwr cp = strtok(NULL, delims);
466 1.3 gwr if(!cp || strcmp(cp, "remote")) {
467 1.3 gwr free_pr_list_item(curr);
468 1.3 gwr continue;
469 1.3 gwr }
470 1.3 gwr cp = strtok(NULL, delims);
471 1.3 gwr if(!cp || strcmp(cp, "printer")) {
472 1.3 gwr free_pr_list_item(curr);
473 1.3 gwr continue;
474 1.3 gwr }
475 1.3 gwr cp = strtok(NULL, delims);
476 1.3 gwr if(!cp) {
477 1.3 gwr free_pr_list_item(curr);
478 1.3 gwr continue;
479 1.3 gwr }
480 1.3 gwr curr->device = strdup(cp);
481 1.3 gwr cp = strtok(NULL, delims);
482 1.3 gwr if(!cp || strcmp(cp, "on")) {
483 1.3 gwr free_pr_list_item(curr);
484 1.3 gwr continue;
485 1.3 gwr }
486 1.3 gwr cp = strtok(NULL, delims);
487 1.3 gwr if(!cp) {
488 1.3 gwr free_pr_list_item(curr);
489 1.3 gwr continue;
490 1.3 gwr }
491 1.3 gwr curr->remhost = strdup(cp);
492 1.3 gwr } else {
493 1.3 gwr /* the local name */
494 1.3 gwr curr->device = strdup(cp);
495 1.3 gwr curr->remhost = strdup("");
496 1.3 gwr }
497 1.3 gwr
498 1.3 gwr if(last == NULL)
499 1.3 gwr printers = curr;
500 1.3 gwr else
501 1.3 gwr last->pr_next = curr;
502 1.3 gwr last = curr;
503 1.3 gwr
504 1.3 gwr }
505 1.3 gwr (void) pclose(p);
506 1.3 gwr
507 1.3 gwr /*
508 1.3 gwr ** Now add on the virtual printers, if any
509 1.3 gwr */
510 1.3 gwr if(last == NULL)
511 1.3 gwr printers = list_virtual_printers();
512 1.3 gwr else
513 1.3 gwr last->pr_next = list_virtual_printers();
514 1.3 gwr
515 1.3 gwr return(1);
516 1.3 gwr }
517 1.3 gwr
518 1.3 gwr #else /* SVR4 */
519 1.3 gwr
520 1.1 jtc /*
521 1.1 jtc * BSD way: lpc stat
522 1.1 jtc */
523 1.1 jtc int
524 1.1 jtc build_pr_list()
525 1.1 jtc {
526 1.1 jtc pr_list last = NULL;
527 1.1 jtc pr_list curr = NULL;
528 1.1 jtc char buff[256];
529 1.1 jtc FILE *p;
530 1.1 jtc char *cp;
531 1.1 jtc int saw_system;
532 1.1 jtc
533 1.1 jtc sprintf(buff, "%s/lpc status", LPCDIR);
534 1.1 jtc p = popen(buff, "r");
535 1.1 jtc if(p == NULL) {
536 1.1 jtc msg_out("rpc.pcnfsd: unable to popen lpc stat");
537 1.1 jtc return(0);
538 1.1 jtc }
539 1.1 jtc
540 1.1 jtc while(fgets(buff, 255, p) != NULL) {
541 1.1 jtc if (isspace(buff[0]))
542 1.1 jtc continue;
543 1.1 jtc
544 1.1 jtc if ((cp = strtok(buff, delims)) == NULL)
545 1.1 jtc continue;
546 1.1 jtc
547 1.1 jtc curr = (struct pr_list_item *)
548 1.1 jtc grab(sizeof (struct pr_list_item));
549 1.1 jtc
550 1.1 jtc /* XXX - Should distinguish remote printers. */
551 1.1 jtc curr->pn = strdup(cp);
552 1.1 jtc curr->device = strdup(cp);
553 1.1 jtc curr->remhost = strdup("");
554 1.1 jtc curr->cm = strdup("-");
555 1.1 jtc curr->pr_next = NULL;
556 1.1 jtc
557 1.1 jtc if(last == NULL)
558 1.1 jtc printers = curr;
559 1.1 jtc else
560 1.1 jtc last->pr_next = curr;
561 1.1 jtc last = curr;
562 1.1 jtc
563 1.1 jtc }
564 1.1 jtc (void) fclose(p);
565 1.1 jtc
566 1.1 jtc /*
567 1.1 jtc ** Now add on the virtual printers, if any
568 1.1 jtc */
569 1.1 jtc if(last == NULL)
570 1.1 jtc printers = list_virtual_printers();
571 1.1 jtc else
572 1.1 jtc last->pr_next = list_virtual_printers();
573 1.1 jtc
574 1.1 jtc return(1);
575 1.3 gwr }
576 1.3 gwr
577 1.1 jtc #endif /* SVR4 */
578 1.1 jtc
579 1.1 jtc void *grab(n)
580 1.3 gwr int n;
581 1.1 jtc {
582 1.1 jtc void *p;
583 1.1 jtc
584 1.1 jtc p = (void *)malloc(n);
585 1.1 jtc if(p == NULL) {
586 1.1 jtc msg_out("rpc.pcnfsd: malloc failure");
587 1.1 jtc exit(1);
588 1.1 jtc }
589 1.1 jtc return(p);
590 1.1 jtc }
591 1.1 jtc
592 1.1 jtc void
593 1.1 jtc free_pr_list_item(curr)
594 1.1 jtc pr_list curr;
595 1.1 jtc {
596 1.1 jtc if(curr->pn)
597 1.1 jtc free(curr->pn);
598 1.1 jtc if(curr->device)
599 1.1 jtc free(curr->device);
600 1.1 jtc if(curr->remhost)
601 1.1 jtc free(curr->remhost);
602 1.1 jtc if(curr->cm)
603 1.1 jtc free(curr->cm);
604 1.1 jtc if(curr->pr_next)
605 1.1 jtc free_pr_list_item(curr->pr_next); /* recurse */
606 1.3 gwr free(curr);
607 1.1 jtc }
608 1.3 gwr
609 1.3 gwr /*
610 1.3 gwr * build_pr_queue: used to show the print queue.
611 1.3 gwr *
612 1.3 gwr * Note that the first thing we do is to discard any
613 1.3 gwr * existing queue.
614 1.3 gwr */
615 1.3 gwr #ifdef SVR4
616 1.3 gwr
617 1.3 gwr /*
618 1.3 gwr ** In SVR4 the command to list the print jobs for printer
619 1.3 gwr ** lp is "lpstat lp" (or, equivalently, "lpstat -p lp").
620 1.3 gwr ** The output looks like this:
621 1.3 gwr **
622 1.3 gwr ** lp-2 root 939 Jul 10 21:56
623 1.3 gwr ** lp-5 geoff 15 Jul 12 23:23
624 1.3 gwr ** lp-6 geoff 15 Jul 12 23:23
625 1.3 gwr **
626 1.1 jtc ** If the first job is actually printing the first line
627 1.3 gwr ** is modified, as follows:
628 1.3 gwr **
629 1.3 gwr ** lp-2 root 939 Jul 10 21:56 on lp
630 1.3 gwr **
631 1.3 gwr ** I don't yet have any info on what it looks like if the printer
632 1.3 gwr ** is remote and we're spooling over the net. However for
633 1.3 gwr ** the purposes of rpc.pcnfsd we can simply say that field 1 is the
634 1.3 gwr ** job ID, field 2 is the submitter, and field 3 is the size.
635 1.3 gwr ** We can check for the presence of the string " on " in the
636 1.3 gwr ** first record to determine if we should count it as rank 0 or rank 1,
637 1.3 gwr ** but it won't hurt if we get it wrong.
638 1.3 gwr **/
639 1.3 gwr
640 1.3 gwr pirstat
641 1.3 gwr build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
642 1.3 gwr printername pn;
643 1.3 gwr username user;
644 1.3 gwr int just_mine;
645 1.3 gwr int *p_qlen;
646 1.3 gwr int *p_qshown;
647 1.3 gwr {
648 1.3 gwr pr_queue last = NULL;
649 1.3 gwr pr_queue curr = NULL;
650 1.3 gwr char buff[256];
651 1.3 gwr FILE *p;
652 1.3 gwr char *owner;
653 1.3 gwr char *job;
654 1.3 gwr char *totsize;
655 1.3 gwr
656 1.3 gwr if(queue) {
657 1.3 gwr free_pr_queue_item(queue);
658 1.3 gwr queue = NULL;
659 1.3 gwr }
660 1.3 gwr *p_qlen = 0;
661 1.3 gwr *p_qshown = 0;
662 1.3 gwr
663 1.3 gwr pn = map_printer_name(pn);
664 1.3 gwr if(pn == NULL || !valid_pr(pn) || suspicious(pn))
665 1.3 gwr return(PI_RES_NO_SUCH_PRINTER);
666 1.3 gwr
667 1.3 gwr sprintf(buff, "/usr/bin/lpstat %s", pn);
668 1.3 gwr p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
669 1.3 gwr if(p == NULL) {
670 1.3 gwr msg_out("rpc.pcnfsd: unable to popen() lpstat queue query");
671 1.3 gwr return(PI_RES_FAIL);
672 1.3 gwr }
673 1.3 gwr
674 1.3 gwr while(fgets(buff, 255, p) != NULL) {
675 1.3 gwr job = strtok(buff, delims);
676 1.3 gwr if(!job)
677 1.3 gwr continue;
678 1.3 gwr
679 1.3 gwr owner = strtok(NULL, delims);
680 1.3 gwr if(!owner)
681 1.3 gwr continue;
682 1.3 gwr
683 1.3 gwr totsize = strtok(NULL, delims);
684 1.3 gwr if(!totsize)
685 1.3 gwr continue;
686 1.3 gwr
687 1.3 gwr *p_qlen += 1;
688 1.3 gwr
689 1.3 gwr if(*p_qshown > QMAX)
690 1.3 gwr continue;
691 1.3 gwr
692 1.3 gwr if(just_mine && mystrcasecmp(owner, user))
693 1.3 gwr continue;
694 1.3 gwr
695 1.3 gwr *p_qshown += 1;
696 1.3 gwr
697 1.3 gwr curr = (struct pr_queue_item *)
698 1.3 gwr grab(sizeof (struct pr_queue_item));
699 1.3 gwr
700 1.3 gwr curr->position = *p_qlen;
701 1.3 gwr curr->id = strdup(job);
702 1.3 gwr curr->size = strdup(totsize);
703 1.3 gwr curr->status = strdup("");
704 1.3 gwr curr->system = strdup("");
705 1.3 gwr curr->user = strdup(owner);
706 1.3 gwr curr->file = strdup("");
707 1.3 gwr curr->cm = strdup("-");
708 1.3 gwr curr->pr_next = NULL;
709 1.3 gwr
710 1.3 gwr if(last == NULL)
711 1.3 gwr queue = curr;
712 1.3 gwr else
713 1.3 gwr last->pr_next = curr;
714 1.3 gwr last = curr;
715 1.3 gwr
716 1.3 gwr }
717 1.3 gwr (void) su_pclose(p);
718 1.3 gwr return(PI_RES_OK);
719 1.3 gwr }
720 1.1 jtc
721 1.1 jtc #else /* SVR4 */
722 1.1 jtc
723 1.1 jtc pirstat
724 1.1 jtc build_pr_queue(pn, user, just_mine, p_qlen, p_qshown)
725 1.1 jtc printername pn;
726 1.1 jtc username user;
727 1.1 jtc int just_mine;
728 1.1 jtc int *p_qlen;
729 1.1 jtc int *p_qshown;
730 1.1 jtc {
731 1.1 jtc pr_queue last = NULL;
732 1.1 jtc pr_queue curr = NULL;
733 1.1 jtc char buff[256];
734 1.1 jtc FILE *p;
735 1.1 jtc char *cp;
736 1.1 jtc int i;
737 1.1 jtc char *rank;
738 1.1 jtc char *owner;
739 1.1 jtc char *job;
740 1.1 jtc char *files;
741 1.1 jtc char *totsize;
742 1.1 jtc
743 1.1 jtc if(queue) {
744 1.1 jtc free_pr_queue_item(queue);
745 1.1 jtc queue = NULL;
746 1.1 jtc }
747 1.1 jtc *p_qlen = 0;
748 1.1 jtc *p_qshown = 0;
749 1.1 jtc pn = map_printer_name(pn);
750 1.1 jtc if(pn == NULL || suspicious(pn))
751 1.1 jtc return(PI_RES_NO_SUCH_PRINTER);
752 1.1 jtc
753 1.1 jtc sprintf(buff, "%s/lpq -P%s", LPRDIR, pn);
754 1.1 jtc
755 1.1 jtc p = su_popen(user, buff, MAXTIME_FOR_QUEUE);
756 1.1 jtc if(p == NULL) {
757 1.1 jtc msg_out("rpc.pcnfsd: unable to popen() lpq");
758 1.1 jtc return(PI_RES_FAIL);
759 1.1 jtc }
760 1.1 jtc
761 1.1 jtc while(fgets(buff, 255, p) != NULL) {
762 1.1 jtc i = strlen(buff) - 1;
763 1.1 jtc buff[i] = '\0'; /* zap trailing NL */
764 1.1 jtc if(i < SIZECOL)
765 1.1 jtc continue;
766 1.1 jtc if(!mystrncasecmp(buff, "rank", 4))
767 1.1 jtc continue;
768 1.1 jtc
769 1.1 jtc totsize = &buff[SIZECOL-1];
770 1.1 jtc files = &buff[FILECOL-1];
771 1.1 jtc cp = totsize;
772 1.1 jtc cp--;
773 1.1 jtc while(cp > files && isspace(*cp))
774 1.1 jtc *cp-- = '\0';
775 1.1 jtc
776 1.1 jtc buff[FILECOL-2] = '\0';
777 1.1 jtc
778 1.1 jtc cp = strtok(buff, delims);
779 1.1 jtc if(!cp)
780 1.1 jtc continue;
781 1.1 jtc rank = cp;
782 1.1 jtc
783 1.1 jtc cp = strtok(NULL, delims);
784 1.1 jtc if(!cp)
785 1.1 jtc continue;
786 1.1 jtc owner = cp;
787 1.1 jtc
788 1.1 jtc cp = strtok(NULL, delims);
789 1.1 jtc if(!cp)
790 1.1 jtc continue;
791 1.1 jtc job = cp;
792 1.1 jtc
793 1.1 jtc *p_qlen += 1;
794 1.1 jtc
795 1.1 jtc if(*p_qshown > QMAX)
796 1.1 jtc continue;
797 1.1 jtc
798 1.1 jtc if(just_mine && mystrcasecmp(owner, user))
799 1.1 jtc continue;
800 1.1 jtc
801 1.1 jtc *p_qshown += 1;
802 1.1 jtc
803 1.1 jtc curr = (struct pr_queue_item *)
804 1.1 jtc grab(sizeof (struct pr_queue_item));
805 1.1 jtc
806 1.1 jtc curr->position = atoi(rank); /* active -> 0 */
807 1.1 jtc curr->id = strdup(job);
808 1.1 jtc curr->size = strdup(totsize);
809 1.1 jtc curr->status = strdup(rank);
810 1.1 jtc curr->system = strdup("");
811 1.1 jtc curr->user = strdup(owner);
812 1.1 jtc curr->file = strdup(files);
813 1.1 jtc curr->cm = strdup("-");
814 1.1 jtc curr->pr_next = NULL;
815 1.1 jtc
816 1.1 jtc if(last == NULL)
817 1.1 jtc queue = curr;
818 1.1 jtc else
819 1.1 jtc last->pr_next = curr;
820 1.1 jtc last = curr;
821 1.1 jtc
822 1.1 jtc }
823 1.1 jtc (void) su_pclose(p);
824 1.1 jtc return(PI_RES_OK);
825 1.3 gwr }
826 1.3 gwr
827 1.1 jtc #endif /* SVR4 */
828 1.1 jtc
829 1.1 jtc void
830 1.1 jtc free_pr_queue_item(curr)
831 1.1 jtc pr_queue curr;
832 1.1 jtc {
833 1.1 jtc if(curr->id)
834 1.1 jtc free(curr->id);
835 1.1 jtc if(curr->size)
836 1.1 jtc free(curr->size);
837 1.1 jtc if(curr->status)
838 1.1 jtc free(curr->status);
839 1.1 jtc if(curr->system)
840 1.1 jtc free(curr->system);
841 1.1 jtc if(curr->user)
842 1.1 jtc free(curr->user);
843 1.1 jtc if(curr->file)
844 1.1 jtc free(curr->file);
845 1.1 jtc if(curr->cm)
846 1.1 jtc free(curr->cm);
847 1.1 jtc if(curr->pr_next)
848 1.1 jtc free_pr_queue_item(curr->pr_next); /* recurse */
849 1.1 jtc free(curr);
850 1.3 gwr }
851 1.1 jtc
852 1.3 gwr #ifdef SVR4
853 1.3 gwr
854 1.3 gwr /*
855 1.3 gwr ** New - SVR4 printer status handling.
856 1.3 gwr **
857 1.3 gwr ** The command we'll use for checking the status of printer "lp"
858 1.3 gwr ** is "lpstat -a lp -p lp". Here are some sample outputs:
859 1.3 gwr **
860 1.3 gwr **
861 1.3 gwr ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
862 1.3 gwr ** printer lp disabled since Thu Feb 21 22:52:36 EST 1991. available.
863 1.3 gwr ** new printer
864 1.3 gwr ** ---
865 1.3 gwr ** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
866 1.3 gwr ** unknown reason
867 1.3 gwr ** printer pcdslw disabled since Fri Jul 12 22:15:37 EDT 1991. available.
868 1.3 gwr ** new printer
869 1.3 gwr ** ---
870 1.3 gwr ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
871 1.3 gwr ** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
872 1.3 gwr ** ---
873 1.3 gwr ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
874 1.3 gwr ** printer lp now printing lp-2. enabled since Sat Jul 13 12:02:17 EDT 1991. available.
875 1.3 gwr ** ---
876 1.3 gwr ** lp accepting requests since Wed Jul 10 21:49:25 EDT 1991
877 1.3 gwr ** printer lp disabled since Sat Jul 13 12:05:20 EDT 1991. available.
878 1.3 gwr ** unknown reason
879 1.3 gwr ** ---
880 1.3 gwr ** pcdslw not accepting requests since Fri Jul 12 22:30:00 EDT 1991 -
881 1.3 gwr ** unknown reason
882 1.3 gwr ** printer pcdslw is idle. enabled since Sat Jul 13 12:05:28 EDT 1991. available.
883 1.3 gwr **
884 1.3 gwr ** Note that these are actual outputs. The format (which is totally
885 1.3 gwr ** different from the lpstat in SunOS) seems to break down as
886 1.3 gwr ** follows:
887 1.3 gwr ** (1) The first line has the form "printername [not] accepting requests,,,"
888 1.3 gwr ** This is trivial to decode.
889 1.3 gwr ** (2) The second line has several forms, all beginning "printer printername":
890 1.3 gwr ** (2.1) "... disabled"
891 1.3 gwr ** (2.2) "... is idle"
892 1.3 gwr ** (2.3) "... now printing jobid"
893 1.3 gwr ** The "available" comment seems to be meaningless. The next line
894 1.3 gwr ** is the "reason" code which the operator can supply when issuing
895 1.3 gwr ** a "disable" or "reject" command.
896 1.3 gwr ** Note that there is no way to check the number of entries in the
897 1.1 jtc ** queue except to ask for the queue and count them.
898 1.1 jtc */
899 1.1 jtc
900 1.1 jtc pirstat
901 1.1 jtc get_pr_status(pn, avail, printing, qlen, needs_operator, status)
902 1.1 jtc printername pn;
903 1.1 jtc bool_t *avail;
904 1.1 jtc bool_t *printing;
905 1.1 jtc int *qlen;
906 1.1 jtc bool_t *needs_operator;
907 1.1 jtc char *status;
908 1.3 gwr {
909 1.1 jtc char buff[256];
910 1.1 jtc char cmd[64];
911 1.1 jtc FILE *p;
912 1.1 jtc int n;
913 1.1 jtc pirstat stat = PI_RES_NO_SUCH_PRINTER;
914 1.1 jtc
915 1.1 jtc /* assume the worst */
916 1.1 jtc *avail = FALSE;
917 1.1 jtc *printing = FALSE;
918 1.1 jtc *needs_operator = FALSE;
919 1.1 jtc *qlen = 0;
920 1.1 jtc *status = '\0';
921 1.3 gwr
922 1.3 gwr pn = map_printer_name(pn);
923 1.3 gwr if(pn == NULL || !valid_pr(pn) || suspicious(pn))
924 1.3 gwr return(PI_RES_NO_SUCH_PRINTER);
925 1.3 gwr n = strlen(pn);
926 1.3 gwr
927 1.3 gwr sprintf(cmd, "/usr/bin/lpstat -a %s -p %s", pn, pn);
928 1.3 gwr
929 1.3 gwr p = popen(cmd, "r");
930 1.3 gwr if(p == NULL) {
931 1.3 gwr msg_out("rpc.pcnfsd: unable to popen() lp status");
932 1.3 gwr return(PI_RES_FAIL);
933 1.3 gwr }
934 1.3 gwr
935 1.3 gwr stat = PI_RES_OK;
936 1.3 gwr
937 1.3 gwr while(fgets(buff, 255, p) != NULL) {
938 1.3 gwr if(!strncmp(buff, pn, n)) {
939 1.3 gwr if(!strstr(buff, "not accepting"))
940 1.3 gwr *avail = TRUE;
941 1.3 gwr continue;
942 1.3 gwr }
943 1.3 gwr if(!strncmp(buff, "printer ", 8)) {
944 1.3 gwr if(!strstr(buff, "disabled"))
945 1.3 gwr *printing = TRUE;
946 1.3 gwr if(strstr(buff, "printing"))
947 1.3 gwr strcpy(status, "printing");
948 1.3 gwr else if (strstr(buff, "idle"))
949 1.3 gwr strcpy(status, "idle");
950 1.3 gwr continue;
951 1.3 gwr }
952 1.3 gwr if(!strncmp(buff, "UX:", 3)) {
953 1.3 gwr stat = PI_RES_NO_SUCH_PRINTER;
954 1.3 gwr }
955 1.3 gwr }
956 1.3 gwr (void) pclose(p);
957 1.3 gwr return(stat);
958 1.3 gwr }
959 1.3 gwr
960 1.3 gwr #else /* SVR4 */
961 1.3 gwr
962 1.3 gwr /*
963 1.3 gwr * BSD way: lpc status
964 1.3 gwr */
965 1.3 gwr pirstat
966 1.3 gwr get_pr_status(pn, avail, printing, qlen, needs_operator, status)
967 1.3 gwr printername pn;
968 1.3 gwr bool_t *avail;
969 1.3 gwr bool_t *printing;
970 1.3 gwr int *qlen;
971 1.3 gwr bool_t *needs_operator;
972 1.3 gwr char *status;
973 1.3 gwr {
974 1.3 gwr char cmd[128];
975 1.3 gwr char buff[256];
976 1.3 gwr char buff2[256];
977 1.3 gwr char pname[64];
978 1.3 gwr FILE *p;
979 1.3 gwr char *cp;
980 1.3 gwr char *cp1;
981 1.3 gwr char *cp2;
982 1.3 gwr int n;
983 1.3 gwr pirstat stat = PI_RES_NO_SUCH_PRINTER;
984 1.3 gwr
985 1.3 gwr /* assume the worst */
986 1.3 gwr *avail = FALSE;
987 1.3 gwr *printing = FALSE;
988 1.3 gwr *needs_operator = FALSE;
989 1.3 gwr *qlen = 0;
990 1.3 gwr *status = '\0';
991 1.1 jtc
992 1.1 jtc pn = map_printer_name(pn);
993 1.1 jtc if(pn == NULL || suspicious(pn))
994 1.1 jtc return(PI_RES_NO_SUCH_PRINTER);
995 1.1 jtc
996 1.1 jtc sprintf(pname, "%s:", pn);
997 1.1 jtc n = strlen(pname);
998 1.1 jtc
999 1.1 jtc sprintf(cmd, "%s/lpc status %s", LPCDIR, pn);
1000 1.1 jtc p = popen(cmd, "r");
1001 1.1 jtc if(p == NULL) {
1002 1.1 jtc msg_out("rpc.pcnfsd: unable to popen() lp status");
1003 1.1 jtc return(PI_RES_FAIL);
1004 1.1 jtc }
1005 1.1 jtc
1006 1.1 jtc while(fgets(buff, 255, p) != NULL) {
1007 1.1 jtc if(strncmp(buff, pname, n))
1008 1.1 jtc continue;
1009 1.1 jtc /*
1010 1.1 jtc ** We have a match. The only failure now is PI_RES_FAIL if
1011 1.1 jtc ** lpstat output cannot be decoded
1012 1.1 jtc */
1013 1.1 jtc stat = PI_RES_FAIL;
1014 1.1 jtc /*
1015 1.1 jtc ** The next four lines are usually if the form
1016 1.1 jtc **
1017 1.1 jtc ** queuing is [enabled|disabled]
1018 1.1 jtc ** printing is [enabled|disabled]
1019 1.1 jtc ** [no entries | N entr[y|ies] in spool area]
1020 1.1 jtc ** <status message, may include the word "attention">
1021 1.1 jtc */
1022 1.1 jtc while(fgets(buff, 255, p) != NULL && isspace(buff[0])) {
1023 1.1 jtc cp = buff;
1024 1.1 jtc while(isspace(*cp))
1025 1.1 jtc cp++;
1026 1.1 jtc if(*cp == '\0')
1027 1.1 jtc break;
1028 1.1 jtc cp1 = cp;
1029 1.1 jtc cp2 = buff2;
1030 1.1 jtc while(*cp1 && *cp1 != '\n') {
1031 1.1 jtc *cp2++ = tolower(*cp1);
1032 1.1 jtc cp1++;
1033 1.1 jtc }
1034 1.1 jtc *cp1 = '\0';
1035 1.1 jtc *cp2 = '\0';
1036 1.1 jtc /*
1037 1.1 jtc ** Now buff2 has a lower-cased copy and cp points at the original;
1038 1.1 jtc ** both are null terminated without any newline
1039 1.1 jtc */
1040 1.1 jtc if(!strncmp(buff2, "queuing", 7)) {
1041 1.1 jtc *avail = (strstr(buff2, "enabled") != NULL);
1042 1.1 jtc continue;
1043 1.1 jtc }
1044 1.1 jtc if(!strncmp(buff2, "printing", 8)) {
1045 1.1 jtc *printing = (strstr(buff2, "enabled") != NULL);
1046 1.1 jtc continue;
1047 1.1 jtc }
1048 1.1 jtc if(isdigit(buff2[0]) && (strstr(buff2, "entr") !=NULL)) {
1049 1.1 jtc
1050 1.1 jtc *qlen = atoi(buff2);
1051 1.1 jtc continue;
1052 1.1 jtc }
1053 1.1 jtc if(strstr(buff2, "attention") != NULL ||
1054 1.1 jtc strstr(buff2, "error") != NULL)
1055 1.1 jtc *needs_operator = TRUE;
1056 1.1 jtc if(*needs_operator || strstr(buff2, "waiting") != NULL)
1057 1.1 jtc strcpy(status, cp);
1058 1.1 jtc }
1059 1.1 jtc stat = PI_RES_OK;
1060 1.1 jtc break;
1061 1.1 jtc }
1062 1.1 jtc (void) pclose(p);
1063 1.1 jtc return(stat);
1064 1.3 gwr }
1065 1.3 gwr
1066 1.3 gwr #endif /* SVR4 */
1067 1.3 gwr
1068 1.3 gwr /*
1069 1.3 gwr * pr_cancel: cancel a print job
1070 1.1 jtc */
1071 1.3 gwr #ifdef SVR4
1072 1.3 gwr
1073 1.3 gwr /*
1074 1.3 gwr ** For SVR4 we have to be prepared for the following kinds of output:
1075 1.3 gwr **
1076 1.3 gwr ** # cancel lp-6
1077 1.3 gwr ** request "lp-6" cancelled
1078 1.3 gwr ** # cancel lp-33
1079 1.3 gwr ** UX:cancel: WARNING: Request "lp-33" doesn't exist.
1080 1.3 gwr ** # cancel foo-88
1081 1.3 gwr ** UX:cancel: WARNING: Request "foo-88" doesn't exist.
1082 1.3 gwr ** # cancel foo
1083 1.3 gwr ** UX:cancel: WARNING: "foo" is not a request id or a printer.
1084 1.3 gwr ** TO FIX: Cancel requests by id or by
1085 1.3 gwr ** name of printer where printing.
1086 1.3 gwr ** # su geoff
1087 1.3 gwr ** $ cancel lp-2
1088 1.3 gwr ** UX:cancel: WARNING: Can't cancel request "lp-2".
1089 1.3 gwr ** TO FIX: You are not allowed to cancel
1090 1.3 gwr ** another's request.
1091 1.3 gwr **
1092 1.3 gwr ** There are probably other variations for remote printers.
1093 1.3 gwr ** Basically, if the reply begins with the string
1094 1.3 gwr ** "UX:cancel: WARNING: "
1095 1.3 gwr ** we can strip this off and look for one of the following
1096 1.3 gwr ** (1) 'R' - should be part of "Request "xxxx" doesn't exist."
1097 1.3 gwr ** (2) '"' - should be start of ""foo" is not a request id or..."
1098 1.3 gwr ** (3) 'C' - should be start of "Can't cancel request..."
1099 1.3 gwr **
1100 1.3 gwr ** The fly in the ointment: all of this can change if these
1101 1.1 jtc ** messages are localized..... :-(
1102 1.1 jtc */
1103 1.1 jtc pcrstat pr_cancel(pr, user, id)
1104 1.1 jtc char *pr;
1105 1.1 jtc char *user;
1106 1.1 jtc char *id;
1107 1.1 jtc {
1108 1.1 jtc char cmdbuf[256];
1109 1.1 jtc char resbuf[256];
1110 1.1 jtc FILE *fd;
1111 1.1 jtc pcrstat stat = PC_RES_NO_SUCH_JOB;
1112 1.1 jtc
1113 1.1 jtc pr = map_printer_name(pr);
1114 1.1 jtc if(pr == NULL || suspicious(pr))
1115 1.1 jtc return(PC_RES_NO_SUCH_PRINTER);
1116 1.1 jtc if(suspicious(id))
1117 1.3 gwr return(PC_RES_NO_SUCH_JOB);
1118 1.3 gwr
1119 1.3 gwr sprintf(cmdbuf, "/usr/bin/cancel %s", id);
1120 1.3 gwr if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
1121 1.3 gwr msg_out("rpc.pcnfsd: su_popen failed");
1122 1.3 gwr return(PC_RES_FAIL);
1123 1.3 gwr }
1124 1.3 gwr
1125 1.3 gwr if(fgets(resbuf, 255, fd) == NULL)
1126 1.3 gwr stat = PC_RES_FAIL;
1127 1.3 gwr else if(!strstr(resbuf, "UX:"))
1128 1.3 gwr stat = PC_RES_OK;
1129 1.3 gwr else if(strstr(resbuf, "doesn't exist"))
1130 1.3 gwr stat = PC_RES_NO_SUCH_JOB;
1131 1.3 gwr else if(strstr(resbuf, "not a request id"))
1132 1.3 gwr stat = PC_RES_NO_SUCH_JOB;
1133 1.3 gwr else if(strstr(resbuf, "Can't cancel request"))
1134 1.3 gwr stat = PC_RES_NOT_OWNER;
1135 1.3 gwr else stat = PC_RES_FAIL;
1136 1.3 gwr
1137 1.3 gwr if(su_pclose(fd) == 255)
1138 1.3 gwr msg_out("rpc.pcnfsd: su_pclose alert");
1139 1.3 gwr return(stat);
1140 1.3 gwr }
1141 1.3 gwr
1142 1.3 gwr #else /* SVR4 */
1143 1.3 gwr
1144 1.3 gwr /*
1145 1.3 gwr * BSD way: lprm
1146 1.3 gwr */
1147 1.3 gwr pcrstat pr_cancel(pr, user, id)
1148 1.3 gwr char *pr;
1149 1.3 gwr char *user;
1150 1.3 gwr char *id;
1151 1.3 gwr {
1152 1.3 gwr char cmdbuf[256];
1153 1.3 gwr char resbuf[256];
1154 1.3 gwr FILE *fd;
1155 1.3 gwr int i;
1156 1.3 gwr pcrstat stat = PC_RES_NO_SUCH_JOB;
1157 1.3 gwr
1158 1.3 gwr pr = map_printer_name(pr);
1159 1.3 gwr if(pr == NULL || suspicious(pr))
1160 1.3 gwr return(PC_RES_NO_SUCH_PRINTER);
1161 1.3 gwr if(suspicious(id))
1162 1.1 jtc return(PC_RES_NO_SUCH_JOB);
1163 1.1 jtc
1164 1.1 jtc sprintf(cmdbuf, "%s/lprm -P%s %s", LPRDIR, pr, id);
1165 1.1 jtc if ((fd = su_popen(user, cmdbuf, MAXTIME_FOR_CANCEL)) == NULL) {
1166 1.1 jtc msg_out("rpc.pcnfsd: su_popen failed");
1167 1.1 jtc return(PC_RES_FAIL);
1168 1.1 jtc }
1169 1.1 jtc while(fgets(resbuf, 255, fd) != NULL) {
1170 1.1 jtc i = strlen(resbuf);
1171 1.1 jtc if(i)
1172 1.1 jtc resbuf[i-1] = '\0'; /* trim NL */
1173 1.1 jtc if(strstr(resbuf, "dequeued") != NULL)
1174 1.1 jtc stat = PC_RES_OK;
1175 1.1 jtc if(strstr(resbuf, "unknown printer") != NULL)
1176 1.1 jtc stat = PC_RES_NO_SUCH_PRINTER;
1177 1.1 jtc if(strstr(resbuf, "Permission denied") != NULL)
1178 1.1 jtc stat = PC_RES_NOT_OWNER;
1179 1.1 jtc }
1180 1.1 jtc if(su_pclose(fd) == 255)
1181 1.1 jtc msg_out("rpc.pcnfsd: su_pclose alert");
1182 1.3 gwr return(stat);
1183 1.1 jtc }
1184 1.1 jtc #endif /* SVR4 */
1185 1.1 jtc
1186 1.1 jtc /*
1188 1.1 jtc ** New subsystem here. We allow the administrator to define
1189 1.1 jtc ** up to NPRINTERDEFS aliases for printer names. This is done
1190 1.1 jtc ** using the "/etc/pcnfsd.conf" file, which is read at startup.
1191 1.1 jtc ** There are three entry points to this subsystem
1192 1.1 jtc **
1193 1.1 jtc ** void add_printer_alias(char *printer, char *alias_for, char *command)
1194 1.1 jtc **
1195 1.1 jtc ** This is invoked from "config_from_file()" for each
1196 1.1 jtc ** "printer" line. "printer" is the name of a printer; note that
1197 1.1 jtc ** it is possible to redefine an existing printer. "alias_for"
1198 1.1 jtc ** is the name of the underlying printer, used for queue listing
1199 1.1 jtc ** and other control functions. If it is "-", there is no
1200 1.1 jtc ** underlying printer, or the administrative functions are
1201 1.1 jtc ** not applicable to this printer. "command"
1202 1.1 jtc ** is the command which should be run (via "su_popen()") if a
1203 1.1 jtc ** job is printed on this printer. The following tokens may be
1204 1.1 jtc ** embedded in the command, and are substituted as follows:
1205 1.1 jtc **
1206 1.1 jtc ** $FILE - path to the file containing the print data
1207 1.1 jtc ** $USER - login of user
1208 1.1 jtc ** $HOST - hostname from which job originated
1209 1.1 jtc **
1210 1.1 jtc ** Tokens may occur multiple times. If The command includes no
1211 1.1 jtc ** $FILE token, the string " $FILE" is silently appended.
1212 1.1 jtc **
1213 1.1 jtc ** pr_list list_virtual_printers()
1214 1.1 jtc **
1215 1.1 jtc ** This is invoked from build_pr_list to generate a list of aliased
1216 1.1 jtc ** printers, so that the client that asks for a list of valid printers
1217 1.1 jtc ** will see these ones.
1218 1.1 jtc **
1219 1.1 jtc ** char *map_printer_name(char *printer)
1220 1.1 jtc **
1221 1.1 jtc ** If "printer" identifies an aliased printer, this function returns
1222 1.1 jtc ** the "alias_for" name, or NULL if the "alias_for" was given as "-".
1223 1.1 jtc ** Otherwise it returns its argument.
1224 1.1 jtc **
1225 1.1 jtc ** char *expand_alias(char *printer, char *file, char *user, char *host)
1226 1.1 jtc **
1227 1.1 jtc ** If "printer" is an aliased printer, this function returns a
1228 1.1 jtc ** pointer to a static string in which the corresponding command
1229 1.1 jtc ** has been expanded. Otherwise ot returns NULL.
1230 1.1 jtc */
1231 1.1 jtc #define NPRINTERDEFS 16
1232 1.1 jtc int num_aliases = 0;
1233 1.1 jtc struct {
1234 1.1 jtc char *a_printer;
1235 1.1 jtc char *a_alias_for;
1236 1.1 jtc char *a_command;
1237 1.1 jtc } alias [NPRINTERDEFS];
1238 1.1 jtc
1239 1.1 jtc
1240 1.1 jtc
1241 1.1 jtc void
1242 1.1 jtc add_printer_alias(printer, alias_for, command)
1243 1.1 jtc char *printer;
1244 1.1 jtc char *alias_for;
1245 1.1 jtc char *command;
1246 1.1 jtc {
1247 1.1 jtc if(num_aliases < NPRINTERDEFS) {
1248 1.1 jtc alias[num_aliases].a_printer = strdup(printer);
1249 1.1 jtc alias[num_aliases].a_alias_for =
1250 1.1 jtc (strcmp(alias_for, "-") ? strdup(alias_for) : NULL);
1251 1.1 jtc if(strstr(command, "$FILE"))
1252 1.1 jtc alias[num_aliases].a_command = strdup(command);
1253 1.1 jtc else {
1254 1.1 jtc alias[num_aliases].a_command = (char *)grab(strlen(command) + 8);
1255 1.1 jtc strcpy(alias[num_aliases].a_command, command);
1256 1.1 jtc strcat(alias[num_aliases].a_command, " $FILE");
1257 1.1 jtc }
1258 1.1 jtc num_aliases++;
1259 1.1 jtc }
1260 1.1 jtc }
1261 1.1 jtc
1262 1.1 jtc pr_list list_virtual_printers()
1263 1.1 jtc {
1264 1.1 jtc pr_list first = NULL;
1265 1.1 jtc pr_list last = NULL;
1266 1.1 jtc pr_list curr = NULL;
1267 1.1 jtc int i;
1268 1.1 jtc
1269 1.1 jtc
1270 1.1 jtc if(num_aliases == 0)
1271 1.1 jtc return(NULL);
1272 1.1 jtc
1273 1.1 jtc for (i = 0; i < num_aliases; i++) {
1274 1.1 jtc curr = (struct pr_list_item *)
1275 1.1 jtc grab(sizeof (struct pr_list_item));
1276 1.1 jtc
1277 1.1 jtc curr->pn = strdup(alias[i].a_printer);
1278 1.1 jtc if(alias[i].a_alias_for == NULL)
1279 1.1 jtc curr->device = strdup("");
1280 1.1 jtc else
1281 1.1 jtc curr->device = strdup(alias[i].a_alias_for);
1282 1.1 jtc curr->remhost = strdup("");
1283 1.1 jtc curr->cm = strdup("(alias)");
1284 1.1 jtc curr->pr_next = NULL;
1285 1.1 jtc if(last == NULL)
1286 1.1 jtc first = curr;
1287 1.1 jtc else
1288 1.1 jtc last->pr_next = curr;
1289 1.1 jtc last = curr;
1290 1.1 jtc
1291 1.1 jtc }
1292 1.1 jtc return(first);
1293 1.1 jtc }
1294 1.1 jtc
1295 1.1 jtc
1296 1.1 jtc char *
1297 1.1 jtc map_printer_name(printer)
1298 1.1 jtc char *printer;
1299 1.1 jtc {
1300 1.1 jtc int i;
1301 1.1 jtc for (i = 0; i < num_aliases; i++){
1302 1.1 jtc if(!strcmp(printer, alias[i].a_printer))
1303 1.1 jtc return(alias[i].a_alias_for);
1304 1.1 jtc }
1305 1.1 jtc return(printer);
1306 1.1 jtc }
1307 1.1 jtc
1308 1.1 jtc static void
1309 1.1 jtc substitute(string, token, data)
1310 1.1 jtc char *string;
1311 1.1 jtc char *token;
1312 1.1 jtc char *data;
1313 1.1 jtc {
1314 1.1 jtc char temp[512];
1315 1.1 jtc char *c;
1316 1.1 jtc
1317 1.1 jtc while(c = strstr(string, token)) {
1318 1.1 jtc *c = '\0';
1319 1.1 jtc strcpy(temp, string);
1320 1.1 jtc strcat(temp, data);
1321 1.1 jtc c += strlen(token);
1322 1.1 jtc strcat(temp, c);
1323 1.1 jtc strcpy(string, temp);
1324 1.1 jtc }
1325 1.1 jtc }
1326 1.1 jtc
1327 1.1 jtc char *
1328 1.1 jtc expand_alias(printer, file, user, host)
1329 1.1 jtc char *printer;
1330 1.1 jtc char *file;
1331 1.1 jtc char *user;
1332 1.1 jtc char *host;
1333 1.1 jtc {
1334 1.1 jtc static char expansion[512];
1335 1.1 jtc int i;
1336 1.1 jtc for (i = 0; i < num_aliases; i++){
1337 1.1 jtc if(!strcmp(printer, alias[i].a_printer)) {
1338 1.1 jtc strcpy(expansion, alias[i].a_command);
1339 1.1 jtc substitute(expansion, "$FILE", file);
1340 1.1 jtc substitute(expansion, "$USER", user);
1341 1.1 jtc substitute(expansion, "$HOST", host);
1342 1.1 jtc return(expansion);
1343 1.1 jtc }
1344 }
1345 return(NULL);
1346 }
1347