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