]> arthur.barton.de Git - netatalk.git/blob - etc/papd/lp.c
replaced all #include <sys/syslog.h> with #include <syslog.h>
[netatalk.git] / etc / papd / lp.c
1 /*
2  * $Id: lp.c,v 1.11 2002-01-03 17:49:39 sibaz Exp $
3  *
4  * Copyright (c) 1990,1994 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  *
7  * Portions:
8  * Copyright (c) 1983 Regents of the University of California.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39
40 /*
41  * Interface to lpr system.
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif /* HAVE_CONFIG_H */
47
48 #include <sys/param.h>
49 #include <syslog.h>
50 #include <sys/time.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <ctype.h>
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif /* HAVE_UNISTD_H */
57
58 #if defined( sun ) && defined( __svr4__ )
59 #include </usr/ucbinclude/sys/file.h>
60 #else /* sun && __svr4__ */
61 #include <sys/file.h>
62 #endif /* sun && __svr4__ */
63 #include <sys/un.h>
64 #include <netinet/in.h>
65 #undef s_net
66 #include <netatalk/at.h>
67 #include <atalk/atp.h>
68 #include <atalk/paths.h>
69
70 #ifdef ABS_PRINT
71 #include <math.h>
72 #endif /* ABS_PRINT */
73
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <netdb.h>
78 #ifdef HAVE_FCNTL_H
79 #include <fcntl.h>
80 #endif /* HAVE_FCNTL_H */
81 #include <pwd.h>
82
83 #include "printer.h"
84 #include "file.h"
85 #include "lp.h"
86
87 /* These functions aren't used outside of lp.c */
88 int lp_conn_inet();
89 int lp_disconn_inet( int );
90 int lp_conn_unix();
91 int lp_disconn_unix( int );
92
93 char    hostname[ MAXHOSTNAMELEN ];
94
95 extern struct sockaddr_at *sat;
96
97 struct lp {
98     int                 lp_flags;
99     FILE                *lp_stream;
100     int                 lp_seq;
101     char                lp_letter;
102     char                *lp_person;
103     char                *lp_host;
104     char                *lp_job;
105 } lp;
106 #define LP_INIT         (1<<0)
107 #define LP_OPEN         (1<<1)
108 #define LP_PIPE         (1<<2)
109 #define LP_CONNECT      (1<<3)
110 #define LP_QUEUE        (1<<4)
111
112 void lp_person( person )
113     char        *person;
114 {
115     if ( lp.lp_person != NULL ) {
116         free( lp.lp_person );
117     }
118     if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
119         syslog( LOG_ERR, "malloc: %m" );
120         exit( 1 );
121     }
122     strcpy( lp.lp_person, person );
123 }
124
125 #ifdef ABS_PRINT
126 int lp_pagecost()
127 {
128     char        cost[ 22 ];
129     char        balance[ 22 ];
130     int         err;
131
132     if ( lp.lp_person == NULL ) {
133         return( -1 );
134     }
135     err = ABS_canprint( lp.lp_person, printer->p_role, printer->p_srvid,
136             cost, balance );
137     printer->p_pagecost = floor( atof( cost ) * 10000.0 );
138     printer->p_balance = atof( balance ) + atof( cost );
139     return( err < 0 ? -1 : 0 );
140 }
141 #endif /* ABS_PRINT */
142
143 void lp_host( host )
144     char        *host;
145 {
146     if ( lp.lp_host != NULL ) {
147         free( lp.lp_host );
148     }
149     if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
150         syslog( LOG_ERR, "malloc: %m" );
151         exit( 1 );
152     }
153     strcpy( lp.lp_host, host );
154 }
155
156 void lp_job( job )
157     char        *job;
158 {
159     char        *p, *q;
160
161     if ( lp.lp_job != NULL ) {
162         free( lp.lp_job );
163     }
164     if (( lp.lp_job = (char *)malloc( strlen( job ) + 1 )) == NULL ) {
165         syslog( LOG_ERR, "malloc: %m" );
166         exit( 1 );
167     }
168     for ( p = job, q = lp.lp_job; *p != '\0'; p++, q++ ) {
169         if ( !isascii( *p ) || !isprint( *p ) || *p == '\\' ) {
170             *q = '.';
171         } else {
172             *q = *p;
173         }
174     }
175     *q = '\0';
176 }
177
178 int lp_init( out, sat )
179     struct papfile      *out;
180     struct sockaddr_at  *sat;
181 {
182     int         fd, n, len;
183     char        *cp, buf[ BUFSIZ ];
184     struct stat st;
185     int         authenticated = 0;
186 #ifdef ABS_PRINT
187     char        cost[ 22 ];
188     char        balance[ 22 ];
189 #endif /* ABS_PRINT */
190
191     if ( printer->p_flags & P_AUTH ) {
192         authenticated = 0;
193
194         /* cap style "log on to afp server before printing" authentication */
195
196         if ( printer->p_authprintdir && (printer->p_flags & P_AUTH_CAP) ) {
197             int addr_net = ntohs( sat->sat_addr.s_net );
198             int addr_node  = sat->sat_addr.s_node;
199             char addr_filename[256];
200             char username[32];
201             struct stat cap_st;
202             FILE *cap_file;
203
204             sprintf(addr_filename, "%s/net%d.%dnode%d", 
205                 printer->p_authprintdir, addr_net/256, addr_net%256, 
206                 addr_node);
207             if (stat(addr_filename, &cap_st) == 0) {
208                 if ((cap_file = fopen(addr_filename, "r")) != NULL) {
209                     if (fscanf(cap_file, "%s", username) != EOF) {
210                         if (getpwnam(username) != NULL ) {
211                             syslog(LOG_INFO, "CAP authenticated %s", username);
212                             lp_person(username);
213                             authenticated = 1;
214                         } else {
215                             syslog(LOG_INFO, "CAP error: invalid username: '%s'", username);
216                         }
217                     } else {
218                         syslog(LOG_INFO, "CAP error: could not read username");
219                     }
220                 } else {
221                     syslog(LOG_INFO, "CAP error: %m");
222                 }
223             } else {
224                 syslog(LOG_INFO, "CAP error: %m");
225             }
226         }
227
228         if ( printer->p_flags & P_AUTH_PSSP ) {
229             if ( lp.lp_person != NULL ) {
230                 authenticated = 1;
231             }
232         }
233
234         if ( authenticated == 0 ) {
235             syslog( LOG_ERR, "lp_init: must authenticate" );
236             spoolerror( out, "Authentication required." );
237             return( -1 );
238         }
239
240 #ifdef ABS_PRINT
241         if (( printer->p_flags & P_ACCOUNT ) && printer->p_pagecost > 0 &&
242                 ! ABS_canprint( lp.lp_person, printer->p_role,
243                 printer->p_srvid, cost, balance )) {
244             syslog( LOG_ERR, "lp_init: no ABS funds" );
245             spoolerror( out, "No ABS funds available." );
246             return( -1 );
247         }
248 #endif /* ABS_PRINT */
249     }
250
251     if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
252         syslog( LOG_ERR, "gethostname: %m" );
253         exit( 1 );
254     }
255
256     if ( lp.lp_flags & LP_INIT ) {
257         syslog( LOG_ERR, "lp_init: already inited, die!" );
258         abort();
259     }
260
261     lp.lp_flags = 0;
262     lp.lp_stream = NULL;
263     lp.lp_letter = 'A';
264
265     if ( printer->p_flags & P_SPOOLED ) {
266         /* check if queuing is enabled: mode & 010 on lock file */
267         if ( stat( printer->p_lock, &st ) < 0 ) {
268             syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
269             spoolerror( out, NULL );
270             return( -1 );
271         }
272         if ( st.st_mode & 010 ) {
273             syslog( LOG_INFO, "lp_init: queuing is disabled" );
274             spoolerror( out, "Queuing is disabled." );
275             return( -1 );
276         }
277
278         if (( fd = open( ".seq", O_RDWR|O_CREAT, 0661 )) < 0 ) {
279             syslog( LOG_ERR, "lp_init: can't create .seq" );
280             spoolerror( out, NULL );
281             return( -1 );
282         }
283
284         if ( flock( fd, LOCK_EX ) < 0 ) {
285             syslog( LOG_ERR, "lp_init: can't lock .seq" );
286             spoolerror( out, NULL );
287             return( -1 );
288         }
289
290         n = 0;
291         if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
292             syslog( LOG_ERR, "lp_init read: %m" );
293             spoolerror( out, NULL );
294             return( -1 );
295         }
296         if ( len > 0 ) {
297             for ( cp = buf; len; len--, cp++ ) {
298                 if ( *cp < '0' || *cp > '9' ) {
299                     break;
300                 }
301                 n = n * 10 + ( *cp - '0' );
302             }
303         }
304         lp.lp_seq = n;
305
306         n = ( n + 1 ) % 1000;
307         sprintf( buf, "%03d\n", n );
308         lseek( fd, 0L, 0 );
309         write( fd, buf, strlen( buf ));
310         close( fd );
311     } else {
312         lp.lp_flags |= LP_PIPE;
313         lp.lp_seq = getpid();
314     }
315
316     lp.lp_flags |= LP_INIT;
317     return( 0 );
318 }
319
320 int lp_open( out, sat )
321     struct papfile      *out;
322     struct sockaddr_at  *sat;
323 {
324     char        name[ MAXPATHLEN ];
325     int         fd;
326     struct passwd       *pwent;
327
328     if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out, sat ) != 0 ) {
329         return( -1 );
330     }
331     if ( lp.lp_flags & LP_OPEN ) {
332         syslog( LOG_ERR, "lp_open already open" );
333         abort();
334     }
335
336     if ( lp.lp_flags & LP_PIPE ) {
337         /* go right to program */
338         if (lp.lp_person != NULL) {
339             if((pwent = getpwnam(lp.lp_person)) != NULL) {
340                 if(setreuid(pwent->pw_uid, pwent->pw_uid) != 0) {
341                     syslog(LOG_INFO, "setreuid error: %m");
342                 }
343             } else {
344                 syslog(LOG_INFO, "Error getting username (%s)", lp.lp_person);
345             }
346         }
347         if (( lp.lp_stream = popen( printer->p_printer, "w" )) == NULL ) {
348             syslog( LOG_ERR, "lp_open popen %s: %m", printer->p_printer );
349             spoolerror( out, NULL );
350             return( -1 );
351         }
352     } else {
353         sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
354
355         if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
356             syslog( LOG_ERR, "lp_open %s: %m", name );
357             spoolerror( out, NULL );
358             return( -1 );
359         }
360
361         if (lp.lp_person != NULL) {
362             if ((pwent = getpwnam(lp.lp_person)) == NULL) {
363                 syslog(LOG_ERR, "getpwnam %s: no such user", lp.lp_person);
364                 spoolerror( out, NULL );
365                 return( -1 );
366             }
367         } else {
368             if ((pwent = getpwnam(printer->p_operator)) == NULL) {
369                 syslog(LOG_ERR, "getpwnam %s: no such user", printer->p_operator);
370                 spoolerror( out, NULL );
371                 return( -1 );
372             }
373         }
374
375         if (fchown(fd, pwent->pw_uid, -1) < 0) {
376             syslog(LOG_ERR, "chown %s %s: %m", pwent->pw_name, name);
377             spoolerror( out, NULL );
378             return( -1 );
379         }
380
381         if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
382             syslog( LOG_ERR, "lp_open fdopen: %m" );
383             spoolerror( out, NULL );
384             return( -1 );
385         }
386     }
387     lp.lp_flags |= LP_OPEN;
388
389     return( 0 );
390 }
391
392 int lp_close()
393 {
394     if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
395         return 0;
396     }
397     fclose( lp.lp_stream );
398     lp.lp_stream = NULL;
399     lp.lp_flags &= ~LP_OPEN;
400     return 0;
401 }
402
403 int lp_write( buf, len )
404     char        *buf;
405     int         len;
406 {
407     if (( lp.lp_flags & LP_OPEN ) == 0 ) {
408         return( -1 );
409     }
410
411     if ( fwrite( buf, 1, len, lp.lp_stream ) != len ) {
412         syslog( LOG_ERR, "lp_write: %m" );
413         abort();
414     }
415     return( 0 );
416 }
417
418 int lp_cancel()
419 {
420     char        name[ MAXPATHLEN ];
421     char        letter;
422
423     if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
424         return 0;
425     }
426
427     if ( lp.lp_flags & LP_OPEN ) {
428         lp_close();
429     }
430
431     for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
432         sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
433         if ( unlink( name ) < 0 ) {
434             syslog( LOG_ERR, "lp_cancel unlink %s: %m", name );
435         }
436     }
437
438     return 0;
439 }
440
441 /*
442  * Create printcap control file, signal printer.  Errors here should
443  * remove queue files.
444  *
445  * XXX piped?
446  */
447 int lp_print()
448 {
449     char                buf[ MAXPATHLEN ];
450     char                tfname[ MAXPATHLEN ];
451     char                cfname[ MAXPATHLEN ];
452     char                letter;
453     int                 fd, n, s;
454     FILE                *cfile;
455
456     if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
457         return 0;
458     }
459     lp_close();
460
461     if ( printer->p_flags & P_SPOOLED ) {
462         sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
463         if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
464             syslog( LOG_ERR, "lp_print %s: %m", tfname );
465             return 0;
466         }
467         if (( cfile = fdopen( fd, "w" )) == NULL ) {
468             syslog( LOG_ERR, "lp_print %s: %m", tfname );
469             return 0;
470         }
471         fprintf( cfile, "H%s\n", hostname );    /* XXX lp_host? */
472
473         if ( lp.lp_person ) {
474             fprintf( cfile, "P%s\n", lp.lp_person );
475         } else {
476             fprintf( cfile, "P%s\n", printer->p_operator );
477         }
478
479         if ( lp.lp_job && *lp.lp_job ) {
480             fprintf( cfile, "J%s\n", lp.lp_job );
481             fprintf( cfile, "T%s\n", lp.lp_job );
482         } else {
483             fprintf( cfile, "JMac Job\n" );
484             fprintf( cfile, "TMac Job\n" );
485         }
486
487         fprintf( cfile, "C%s\n", hostname );    /* XXX lp_host? */
488
489         if ( lp.lp_person ) {
490             fprintf( cfile, "L%s\n", lp.lp_person );
491         } else {
492             fprintf( cfile, "L%s\n", printer->p_operator );
493         }
494
495         for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
496             fprintf( cfile, "fdf%c%03d%s\n", letter, lp.lp_seq, hostname );
497             fprintf( cfile, "Udf%c%03d%s\n", letter, lp.lp_seq, hostname );
498         }
499
500         if ( lp.lp_job && *lp.lp_job ) {
501             fprintf( cfile, "N%s\n", lp.lp_job );
502         } else {
503             fprintf( cfile, "NMac Job\n" );
504         }
505         fclose( cfile );
506
507         sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
508         if ( link( tfname, cfname ) < 0 ) {
509             syslog( LOG_ERR, "lp_print can't link %s to %s: %m", cfname,
510                     tfname );
511             return 0;
512         }
513         unlink( tfname );
514
515         if (( s = lp_conn_unix()) < 0 ) {
516             syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
517             return 0;
518         }
519
520         sprintf( buf, "\1%s\n", printer->p_printer );
521         n = strlen( buf );
522         if ( write( s, buf, n ) != n ) {
523             syslog( LOG_ERR, "lp_print write: %m" );
524             return 0;
525         }
526         if ( read( s, buf, 1 ) != 1 ) {
527             syslog( LOG_ERR, "lp_print read: %m" );
528             return 0;
529         }
530
531         lp_disconn_unix( s );
532
533         if ( buf[ 0 ] != '\0' ) {
534             syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
535             return 0;
536         }
537     }
538     syslog( LOG_INFO, "lp_print queued" );
539     return 0;
540 }
541
542 int lp_disconn_unix( fd )
543 {
544     return( close( fd ));
545 }
546
547 int lp_conn_unix()
548 {
549     int                 s;
550     struct sockaddr_un  saun;
551
552     if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
553         syslog( LOG_ERR, "lp_conn_unix socket: %m" );
554         return( -1 );
555     }
556     memset( &saun, 0, sizeof( struct sockaddr_un ));
557     saun.sun_family = AF_UNIX;
558     strcpy( saun.sun_path, _PATH_DEVPRINTER );
559     if ( connect( s, (struct sockaddr *)&saun,
560             strlen( saun.sun_path ) + 2 ) < 0 ) {
561         syslog( LOG_ERR, "lp_conn_unix connect %s: %m", saun.sun_path );
562         close( s );
563         return( -1 );
564     }
565
566     return( s );
567 }
568
569 int lp_disconn_inet( int fd )
570 {
571     return( close( fd ));
572 }
573
574 int lp_conn_inet()
575 {
576     int                 privfd, port = IPPORT_RESERVED - 1;
577     struct sockaddr_in  sin;
578     struct servent      *sp;
579     struct hostent      *hp;
580
581     if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
582         syslog( LOG_ERR, "printer/tcp: unknown service\n" );
583         return( -1 );
584     }
585
586     if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
587         syslog( LOG_ERR, "gethostname: %m" );
588         exit( 1 );
589     }
590
591     if (( hp = gethostbyname( hostname )) == NULL ) {
592         syslog( LOG_ERR, "%s: unknown host\n", hostname );
593         return( -1 );
594     }
595
596     if (( privfd = rresvport( &port )) < 0 ) {
597         syslog( LOG_ERR, "lp_connect: socket: %m" );
598         close( privfd );
599         return( -1 );
600     }
601
602     memset( &sin, 0, sizeof( struct sockaddr_in ));
603     sin.sin_family = AF_INET;
604 /*    sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
605     memcpy( &sin.sin_addr, hp->h_addr, hp->h_length );
606     sin.sin_port = sp->s_port;
607
608     if ( connect( privfd, (struct sockaddr *)&sin,
609             sizeof( struct sockaddr_in )) < 0 ) {
610         syslog( LOG_ERR, "lp_connect: %m" );
611         close( privfd );
612         return( -1 );
613     }
614
615     return( privfd );
616 }
617
618 int lp_rmjob( job )
619     int         job;
620 {
621     char        buf[ 1024 ];
622     int         n, s;
623
624     if (( s = lp_conn_inet()) < 0 ) {
625         syslog( LOG_ERR, "lp_rmjob: %m" );
626         return( -1 );
627     }
628
629     if ( lp.lp_person == NULL ) {
630         return( -1 );
631     }
632
633     sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
634     n = strlen( buf );
635     if ( write( s, buf, n ) != n ) {
636         syslog( LOG_ERR, "lp_rmjob write: %m" );
637         lp_disconn_inet( s );
638         return( -1 );
639     }
640     while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
641         syslog( LOG_DEBUG, "read %.*s", n, buf );
642     }
643
644     lp_disconn_inet( s );
645     return( 0 );
646 }
647
648 char    *kw_rank = "Rank";
649 char    *kw_active = "active";
650
651 char    *tag_rank = "rank: ";
652 char    *tag_owner = "owner: ";
653 char    *tag_job = "job: ";
654 char    *tag_files = "files: ";
655 char    *tag_size = "size: ";
656 char    *tag_status = "status: ";
657
658 int lp_queue( out )
659     struct papfile      *out;
660 {
661     char                        buf[ 1024 ], *start, *stop, *p, *q;
662     int                         linelength, crlflength;
663     static struct papfile       pf;
664     int                         n, len, s;
665         
666     if (( s = lp_conn_unix()) < 0 ) {
667         syslog( LOG_ERR, "lp_queue: %m" );
668         return( -1 );
669     }
670
671     sprintf( buf, "\3%s\n", printer->p_printer );
672     n = strlen( buf );
673     if ( write( s, buf, n ) != n ) {
674         syslog( LOG_ERR, "lp_queue write: %m" );
675         lp_disconn_unix( s );
676         return( -1 );
677     }
678     pf.pf_state = PF_BOT;
679
680     while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
681         append( &pf, buf, n );
682     }
683
684     for (;;) {
685         if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
686             /* parse */
687             stop = start + linelength;
688             for ( p = start; p < stop; p++ ) {
689                 if ( *p == ' ' || *p == '\t' ) {
690                     break;
691                 }
692             }
693             if ( p >= stop ) {
694                 CONSUME( &pf , linelength + crlflength);
695                 continue;
696             }
697
698             /*
699              * Keys: "Rank", a number, "active"
700              * Anything else is status.
701              */
702             len = p - start;
703             if ( len == strlen( kw_rank ) &&
704                     strncmp( kw_rank, start, len ) == 0 ) {
705                 CONSUME( &pf, linelength + crlflength );
706                 continue;
707             }
708             if (( len == strlen( kw_active ) &&
709                     strncmp( kw_active, start, len ) == 0 ) ||
710                     isdigit( *start )) {                /* a job line */
711                 append( out, tag_rank, strlen( tag_rank ));
712                 append( out, start, p - start );
713                 append( out, "\n", 1 );
714
715                 for ( ; p < stop; p++ ) {
716                     if ( *p != ' ' && *p != '\t' ) {
717                         break;
718                     }
719                 }
720                 for ( q = p; p < stop; p++ ) {
721                     if ( *p == ' ' || *p == '\t' ) {
722                         break;
723                     }
724                 }
725                 if ( p >= stop ) {
726                     append( out, ".\n", 2 );
727                     CONSUME( &pf, linelength + crlflength );
728                     continue;
729                 }
730                 append( out, tag_owner, strlen( tag_owner ));
731                 append( out, q, p - q );
732                 append( out, "\n", 1 );
733
734                 for ( ; p < stop; p++ ) {
735                     if ( *p != ' ' && *p != '\t' ) {
736                         break;
737                     }
738                 }
739                 for ( q = p; p < stop; p++ ) {
740                     if ( *p == ' ' || *p == '\t' ) {
741                         break;
742                     }
743                 }
744                 if ( p >= stop ) {
745                     append( out, ".\n", 2 );
746                     CONSUME( &pf , linelength + crlflength );
747                     continue;
748                 }
749                 append( out, tag_job, strlen( tag_job ));
750                 append( out, q, p - q );
751                 append( out, "\n", 1 );
752
753                 for ( ; p < stop; p++ ) {
754                     if ( *p != ' ' && *p != '\t' ) {
755                         break;
756                     }
757                 }
758                 for ( q = p, p = stop; p > q; p-- ) {
759                     if ( *p == ' ' || *p == '\t' ) {
760                         break;
761                     }
762                 }
763                 for ( ; p > q; p-- ) {
764                     if ( *p != ' ' && *p != '\t' ) {
765                         break;
766                     }
767                 }
768                 for ( ; p > q; p-- ) {
769                     if ( *p == ' ' || *p == '\t' ) {
770                         break;
771                     }
772                 }
773                 if ( p <= q ) {
774                     append( out, ".\n", 2 );
775                     CONSUME( &pf, linelength + crlflength );
776                     continue;
777                 }
778                 append( out, tag_files, strlen( tag_files ));
779                 append( out, q, p - q );
780                 append( out, "\n", 1 );
781
782                 for ( ; p < stop; p++ ) {
783                     if ( *p != ' ' && *p != '\t' ) {
784                         break;
785                     }
786                 }
787                 append( out, tag_size, strlen( tag_size ));
788                 append( out, p, stop - p );
789                 append( out, "\n.\n", 3 );
790
791                 CONSUME( &pf, linelength + crlflength );
792                 continue;
793             }
794
795             /* status */
796             append( out, tag_status, strlen( tag_status ));
797             append( out, start, linelength );
798             append( out, "\n.\n", 3 );
799
800             CONSUME( &pf, linelength + crlflength );
801         } else {
802             append( out, "*\n", 2 );
803             lp_disconn_unix( s );
804             return( 0 );
805         }
806     }
807 }