2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
6 * Copyright (c) 1983 Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Interface to lpr system.
42 #include <sys/param.h>
43 #include <sys/syslog.h>
45 #include <sys/socket.h>
47 #if defined( sun ) && defined( __svr4__ )
48 #include </usr/ucbinclude/sys/file.h>
53 #include <netinet/in.h>
55 #include <netatalk/at.h>
56 #include <atalk/atp.h>
57 #include <atalk/paths.h>
72 char hostname[ MAXHOSTNAMELEN ];
74 /* initialize printing interface */
76 /* cancel current job */
78 /* print current job */
81 /* open a file for spooling */
83 /* open a buffer to the current open file */
85 /* close current spooling file */
97 #define LP_INIT (1<<0)
98 #define LP_OPEN (1<<1)
99 #define LP_PIPE (1<<2)
100 #define LP_CONNECT (1<<3)
101 #define LP_QUEUE (1<<4)
106 if ( lp.lp_person != NULL ) {
107 free( lp.lp_person );
109 if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
110 syslog( LOG_ERR, "malloc: %m" );
113 strcpy( lp.lp_person, person );
123 if ( lp.lp_person == NULL ) {
126 err = ABS_canprint( lp.lp_person, printer->p_role, printer->p_srvid,
128 printer->p_pagecost = floor( atof( cost ) * 10000.0 );
129 printer->p_balance = atof( balance ) + atof( cost );
130 return( err < 0 ? -1 : 0 );
137 if ( lp.lp_host != NULL ) {
140 if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
141 syslog( LOG_ERR, "malloc: %m" );
144 strcpy( lp.lp_host, host );
152 if ( lp.lp_job != NULL ) {
155 if (( lp.lp_job = (char *)malloc( strlen( job ) + 1 )) == NULL ) {
156 syslog( LOG_ERR, "malloc: %m" );
159 for ( p = job, q = lp.lp_job; *p != '\0'; p++, q++ ) {
160 if ( !isascii( *p ) || !isprint( *p ) || *p == '\\' ) {
173 char *cp, buf[ BUFSIZ ];
180 if ( printer->p_flags & P_AUTH ) {
181 if ( lp.lp_person == NULL ) {
182 syslog( LOG_ERR, "lp_init: must authenticate" );
183 spoolerror( out, "Authentication required." );
187 if (( printer->p_flags & P_ACCOUNT ) && printer->p_pagecost > 0 &&
188 ! ABS_canprint( lp.lp_person, printer->p_role,
189 printer->p_srvid, cost, balance )) {
190 syslog( LOG_ERR, "lp_init: no ABS funds" );
191 spoolerror( out, "No ABS funds available." );
197 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
198 syslog( LOG_ERR, "gethostname: %m" );
202 if ( lp.lp_flags & LP_INIT ) {
203 syslog( LOG_ERR, "lp_init: already inited, die!" );
211 if ( printer->p_flags & P_SPOOLED ) {
212 /* check if queuing is enabled: mode & 010 on lock file */
213 if ( stat( printer->p_lock, &st ) < 0 ) {
214 syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
215 spoolerror( out, NULL );
218 if ( st.st_mode & 010 ) {
219 syslog( LOG_INFO, "lp_init: queuing is disabled" );
220 spoolerror( out, "Queuing is disabled." );
224 if (( fd = open( ".seq", O_RDWR|O_CREAT, 0661 )) < 0 ) {
225 syslog( LOG_ERR, "lp_init: can't create .seq" );
226 spoolerror( out, NULL );
230 if ( flock( fd, LOCK_EX ) < 0 ) {
231 syslog( LOG_ERR, "lp_init: can't lock .seq" );
232 spoolerror( out, NULL );
237 if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
238 syslog( LOG_ERR, "lp_init read: %m" );
239 spoolerror( out, NULL );
243 for ( cp = buf; len; len--, cp++ ) {
244 if ( *cp < '0' || *cp > '9' ) {
247 n = n * 10 + ( *cp - '0' );
252 n = ( n + 1 ) % 1000;
253 sprintf( buf, "%03d\n", n );
255 write( fd, buf, strlen( buf ));
258 lp.lp_flags |= LP_PIPE;
259 lp.lp_seq = getpid();
262 lp.lp_flags |= LP_INIT;
269 char name[ MAXPATHLEN ];
272 if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out ) != 0 ) {
275 if ( lp.lp_flags & LP_OPEN ) {
276 syslog( LOG_ERR, "lp_open already open" );
280 if ( lp.lp_flags & LP_PIPE ) {
281 /* go right to program */
282 if (( lp.lp_stream = popen( printer->p_printer, "w" )) == NULL ) {
283 syslog( LOG_ERR, "lp_open popen %s: %m", printer->p_printer );
284 spoolerror( out, NULL );
288 sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
290 if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
291 syslog( LOG_ERR, "lp_open %s: %m", name );
292 spoolerror( out, NULL );
295 if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
296 syslog( LOG_ERR, "lp_open fdopen: %m" );
297 spoolerror( out, NULL );
301 lp.lp_flags |= LP_OPEN;
308 if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
311 fclose( lp.lp_stream );
313 lp.lp_flags &= ~LP_OPEN;
321 if (( lp.lp_flags & LP_OPEN ) == 0 ) {
325 if ( fwrite( buf, 1, len, lp.lp_stream ) != len ) {
326 syslog( LOG_ERR, "lp_write: %m" );
334 char name[ MAXPATHLEN ];
337 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
341 if ( lp.lp_flags & LP_OPEN ) {
345 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
346 sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
347 if ( unlink( name ) < 0 ) {
348 syslog( LOG_ERR, "lp_cancel unlink %s: %m", name );
356 * Create printcap control file, signal printer. Errors here should
357 * remove queue files.
363 char buf[ MAXPATHLEN ];
364 char tfname[ MAXPATHLEN ];
365 char cfname[ MAXPATHLEN ];
370 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
375 if ( printer->p_flags & P_SPOOLED ) {
376 sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
377 if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
378 syslog( LOG_ERR, "lp_print %s: %m", tfname );
381 if (( cfile = fdopen( fd, "w" )) == NULL ) {
382 syslog( LOG_ERR, "lp_print %s: %m", tfname );
385 fprintf( cfile, "H%s\n", hostname ); /* XXX lp_host? */
387 if ( lp.lp_person ) {
388 fprintf( cfile, "P%s\n", lp.lp_person );
390 fprintf( cfile, "P%s\n", printer->p_operator );
393 if ( lp.lp_job && *lp.lp_job ) {
394 fprintf( cfile, "J%s\n", lp.lp_job );
395 fprintf( cfile, "T%s\n", lp.lp_job );
397 fprintf( cfile, "JMac Job\n" );
398 fprintf( cfile, "TMac Job\n" );
401 fprintf( cfile, "C%s\n", hostname ); /* XXX lp_host? */
403 if ( lp.lp_person ) {
404 fprintf( cfile, "L%s\n", lp.lp_person );
406 fprintf( cfile, "L%s\n", printer->p_operator );
409 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
410 fprintf( cfile, "fdf%c%03d%s\n", letter, lp.lp_seq, hostname );
411 fprintf( cfile, "Udf%c%03d%s\n", letter, lp.lp_seq, hostname );
414 if ( lp.lp_job && *lp.lp_job ) {
415 fprintf( cfile, "N%s\n", lp.lp_job );
417 fprintf( cfile, "NMac Job\n" );
421 sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
422 if ( link( tfname, cfname ) < 0 ) {
423 syslog( LOG_ERR, "lp_print can't link %s to %s: %m", cfname,
429 if (( s = lp_conn_unix()) < 0 ) {
430 syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
434 sprintf( buf, "\1%s\n", printer->p_printer );
436 if ( write( s, buf, n ) != n ) {
437 syslog( LOG_ERR, "lp_print write: %m" );
440 if ( read( s, buf, 1 ) != 1 ) {
441 syslog( LOG_ERR, "lp_print read: %m" );
445 lp_disconn_unix( s );
447 if ( buf[ 0 ] != '\0' ) {
448 syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
452 syslog( LOG_INFO, "lp_print queued" );
456 lp_disconn_unix( fd )
458 return( close( fd ));
464 struct sockaddr_un saun;
466 if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
467 syslog( LOG_ERR, "lp_conn_unix socket: %m" );
470 bzero( &saun, sizeof( struct sockaddr_un ));
471 saun.sun_family = AF_UNIX;
472 strcpy( saun.sun_path, _PATH_DEVPRINTER );
473 if ( connect( s, (struct sockaddr *)&saun,
474 strlen( saun.sun_path ) + 2 ) < 0 ) {
475 syslog( LOG_ERR, "lp_conn_unix connect %s: %m", saun.sun_path );
483 lp_disconn_inet( fd )
485 return( close( fd ));
490 int privfd, port = IPPORT_RESERVED - 1;
491 struct sockaddr_in sin;
495 if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
496 syslog( LOG_ERR, "printer/tcp: unknown service\n" );
500 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
501 syslog( LOG_ERR, "gethostname: %m" );
505 if (( hp = gethostbyname( hostname )) == NULL ) {
506 syslog( LOG_ERR, "%s: unknown host\n", hostname );
510 if (( privfd = rresvport( &port )) < 0 ) {
511 syslog( LOG_ERR, "lp_connect: socket: %m" );
516 bzero( &sin, sizeof( struct sockaddr_in ));
517 sin.sin_family = AF_INET;
518 /* sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
519 bcopy( hp->h_addr, &sin.sin_addr, hp->h_length );
520 sin.sin_port = sp->s_port;
522 if ( connect( privfd, (struct sockaddr *)&sin,
523 sizeof( struct sockaddr_in )) < 0 ) {
524 syslog( LOG_ERR, "lp_connect: %m" );
538 if (( s = lp_conn_inet()) < 0 ) {
539 syslog( LOG_ERR, "lp_rmjob: %m" );
543 if ( lp.lp_person == NULL ) {
547 sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
549 if ( write( s, buf, n ) != n ) {
550 syslog( LOG_ERR, "lp_rmjob write: %m" );
551 lp_disconn_inet( s );
554 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
555 syslog( LOG_DEBUG, "read %.*s", n, buf );
558 lp_disconn_inet( s );
562 char *kw_rank = "Rank";
563 char *kw_active = "active";
565 char *tag_rank = "rank: ";
566 char *tag_owner = "owner: ";
567 char *tag_job = "job: ";
568 char *tag_files = "files: ";
569 char *tag_size = "size: ";
570 char *tag_status = "status: ";
575 char buf[ 1024 ], *start, *stop, *p, *q;
576 int linelength, crlflength;
577 static struct papfile pf;
580 if (( s = lp_conn_unix()) < 0 ) {
581 syslog( LOG_ERR, "lp_queue: %m" );
585 sprintf( buf, "\3%s\n", printer->p_printer );
587 if ( write( s, buf, n ) != n ) {
588 syslog( LOG_ERR, "lp_queue write: %m" );
589 lp_disconn_unix( s );
592 pf.pf_state = PF_BOT;
594 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
595 append( &pf, buf, n );
599 if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
601 stop = start + linelength;
602 for ( p = start; p < stop; p++ ) {
603 if ( *p == ' ' || *p == '\t' ) {
608 CONSUME( &pf , linelength + crlflength);
613 * Keys: "Rank", a number, "active"
614 * Anything else is status.
617 if ( len == strlen( kw_rank ) &&
618 strncmp( kw_rank, start, len ) == 0 ) {
619 CONSUME( &pf, linelength + crlflength );
622 if (( len == strlen( kw_active ) &&
623 strncmp( kw_active, start, len ) == 0 ) ||
624 isdigit( *start )) { /* a job line */
625 append( out, tag_rank, strlen( tag_rank ));
626 append( out, start, p - start );
627 append( out, "\n", 1 );
629 for ( ; p < stop; p++ ) {
630 if ( *p != ' ' && *p != '\t' ) {
634 for ( q = p; p < stop; p++ ) {
635 if ( *p == ' ' || *p == '\t' ) {
640 append( out, ".\n", 2 );
641 CONSUME( &pf, linelength + crlflength );
644 append( out, tag_owner, strlen( tag_owner ));
645 append( out, q, p - q );
646 append( out, "\n", 1 );
648 for ( ; p < stop; p++ ) {
649 if ( *p != ' ' && *p != '\t' ) {
653 for ( q = p; p < stop; p++ ) {
654 if ( *p == ' ' || *p == '\t' ) {
659 append( out, ".\n", 2 );
660 CONSUME( &pf , linelength + crlflength );
663 append( out, tag_job, strlen( tag_job ));
664 append( out, q, p - q );
665 append( out, "\n", 1 );
667 for ( ; p < stop; p++ ) {
668 if ( *p != ' ' && *p != '\t' ) {
672 for ( q = p, p = stop; p > q; p-- ) {
673 if ( *p == ' ' || *p == '\t' ) {
677 for ( ; p > q; p-- ) {
678 if ( *p != ' ' && *p != '\t' ) {
682 for ( ; p > q; p-- ) {
683 if ( *p == ' ' || *p == '\t' ) {
688 append( out, ".\n", 2 );
689 CONSUME( &pf, linelength + crlflength );
692 append( out, tag_files, strlen( tag_files ));
693 append( out, q, p - q );
694 append( out, "\n", 1 );
696 for ( ; p < stop; p++ ) {
697 if ( *p != ' ' && *p != '\t' ) {
701 append( out, tag_size, strlen( tag_size ));
702 append( out, p, stop - p );
703 append( out, "\n.\n", 3 );
705 CONSUME( &pf, linelength + crlflength );
710 append( out, tag_status, strlen( tag_status ));
711 append( out, start, linelength );
712 append( out, "\n.\n", 3 );
714 CONSUME( &pf, linelength + crlflength );
716 append( out, "*\n", 2 );
717 lp_disconn_unix( s );