2 * $Id: lp.c,v 1.10 2001-08-03 22:13:28 srittau Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
8 * Copyright (c) 1983 Regents of the University of California.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 * Interface to lpr system.
46 #endif /* HAVE_CONFIG_H */
48 #include <sys/param.h>
49 #include <sys/syslog.h>
51 #include <sys/socket.h>
56 #endif /* HAVE_UNISTD_H */
58 #if defined( sun ) && defined( __svr4__ )
59 #include </usr/ucbinclude/sys/file.h>
60 #else /* sun && __svr4__ */
62 #endif /* sun && __svr4__ */
64 #include <netinet/in.h>
66 #include <netatalk/at.h>
67 #include <atalk/atp.h>
68 #include <atalk/paths.h>
72 #endif /* ABS_PRINT */
80 #endif /* HAVE_FCNTL_H */
87 /* These functions aren't used outside of lp.c */
89 int lp_disconn_inet( int );
91 int lp_disconn_unix( int );
93 char hostname[ MAXHOSTNAMELEN ];
95 extern struct sockaddr_at *sat;
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)
112 void lp_person( person )
115 if ( lp.lp_person != NULL ) {
116 free( lp.lp_person );
118 if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
119 syslog( LOG_ERR, "malloc: %m" );
122 strcpy( lp.lp_person, person );
132 if ( lp.lp_person == NULL ) {
135 err = ABS_canprint( lp.lp_person, printer->p_role, printer->p_srvid,
137 printer->p_pagecost = floor( atof( cost ) * 10000.0 );
138 printer->p_balance = atof( balance ) + atof( cost );
139 return( err < 0 ? -1 : 0 );
141 #endif /* ABS_PRINT */
146 if ( lp.lp_host != NULL ) {
149 if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
150 syslog( LOG_ERR, "malloc: %m" );
153 strcpy( lp.lp_host, host );
161 if ( lp.lp_job != NULL ) {
164 if (( lp.lp_job = (char *)malloc( strlen( job ) + 1 )) == NULL ) {
165 syslog( LOG_ERR, "malloc: %m" );
168 for ( p = job, q = lp.lp_job; *p != '\0'; p++, q++ ) {
169 if ( !isascii( *p ) || !isprint( *p ) || *p == '\\' ) {
178 int lp_init( out, sat )
180 struct sockaddr_at *sat;
183 char *cp, buf[ BUFSIZ ];
185 int authenticated = 0;
189 #endif /* ABS_PRINT */
191 if ( printer->p_flags & P_AUTH ) {
194 /* cap style "log on to afp server before printing" authentication */
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];
204 sprintf(addr_filename, "%s/net%d.%dnode%d",
205 printer->p_authprintdir, addr_net/256, addr_net%256,
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);
215 syslog(LOG_INFO, "CAP error: invalid username: '%s'", username);
218 syslog(LOG_INFO, "CAP error: could not read username");
221 syslog(LOG_INFO, "CAP error: %m");
224 syslog(LOG_INFO, "CAP error: %m");
228 if ( printer->p_flags & P_AUTH_PSSP ) {
229 if ( lp.lp_person != NULL ) {
234 if ( authenticated == 0 ) {
235 syslog( LOG_ERR, "lp_init: must authenticate" );
236 spoolerror( out, "Authentication required." );
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." );
248 #endif /* ABS_PRINT */
251 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
252 syslog( LOG_ERR, "gethostname: %m" );
256 if ( lp.lp_flags & LP_INIT ) {
257 syslog( LOG_ERR, "lp_init: already inited, die!" );
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 );
272 if ( st.st_mode & 010 ) {
273 syslog( LOG_INFO, "lp_init: queuing is disabled" );
274 spoolerror( out, "Queuing is disabled." );
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 );
284 if ( flock( fd, LOCK_EX ) < 0 ) {
285 syslog( LOG_ERR, "lp_init: can't lock .seq" );
286 spoolerror( out, NULL );
291 if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
292 syslog( LOG_ERR, "lp_init read: %m" );
293 spoolerror( out, NULL );
297 for ( cp = buf; len; len--, cp++ ) {
298 if ( *cp < '0' || *cp > '9' ) {
301 n = n * 10 + ( *cp - '0' );
306 n = ( n + 1 ) % 1000;
307 sprintf( buf, "%03d\n", n );
309 write( fd, buf, strlen( buf ));
312 lp.lp_flags |= LP_PIPE;
313 lp.lp_seq = getpid();
316 lp.lp_flags |= LP_INIT;
320 int lp_open( out, sat )
322 struct sockaddr_at *sat;
324 char name[ MAXPATHLEN ];
326 struct passwd *pwent;
328 if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out, sat ) != 0 ) {
331 if ( lp.lp_flags & LP_OPEN ) {
332 syslog( LOG_ERR, "lp_open already open" );
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");
344 syslog(LOG_INFO, "Error getting username (%s)", lp.lp_person);
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 );
353 sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
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 );
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 );
368 if ((pwent = getpwnam(printer->p_operator)) == NULL) {
369 syslog(LOG_ERR, "getpwnam %s: no such user", printer->p_operator);
370 spoolerror( out, NULL );
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 );
381 if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
382 syslog( LOG_ERR, "lp_open fdopen: %m" );
383 spoolerror( out, NULL );
387 lp.lp_flags |= LP_OPEN;
394 if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
397 fclose( lp.lp_stream );
399 lp.lp_flags &= ~LP_OPEN;
403 int lp_write( buf, len )
407 if (( lp.lp_flags & LP_OPEN ) == 0 ) {
411 if ( fwrite( buf, 1, len, lp.lp_stream ) != len ) {
412 syslog( LOG_ERR, "lp_write: %m" );
420 char name[ MAXPATHLEN ];
423 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
427 if ( lp.lp_flags & LP_OPEN ) {
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 );
442 * Create printcap control file, signal printer. Errors here should
443 * remove queue files.
449 char buf[ MAXPATHLEN ];
450 char tfname[ MAXPATHLEN ];
451 char cfname[ MAXPATHLEN ];
456 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
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 );
467 if (( cfile = fdopen( fd, "w" )) == NULL ) {
468 syslog( LOG_ERR, "lp_print %s: %m", tfname );
471 fprintf( cfile, "H%s\n", hostname ); /* XXX lp_host? */
473 if ( lp.lp_person ) {
474 fprintf( cfile, "P%s\n", lp.lp_person );
476 fprintf( cfile, "P%s\n", printer->p_operator );
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 );
483 fprintf( cfile, "JMac Job\n" );
484 fprintf( cfile, "TMac Job\n" );
487 fprintf( cfile, "C%s\n", hostname ); /* XXX lp_host? */
489 if ( lp.lp_person ) {
490 fprintf( cfile, "L%s\n", lp.lp_person );
492 fprintf( cfile, "L%s\n", printer->p_operator );
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 );
500 if ( lp.lp_job && *lp.lp_job ) {
501 fprintf( cfile, "N%s\n", lp.lp_job );
503 fprintf( cfile, "NMac Job\n" );
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,
515 if (( s = lp_conn_unix()) < 0 ) {
516 syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
520 sprintf( buf, "\1%s\n", printer->p_printer );
522 if ( write( s, buf, n ) != n ) {
523 syslog( LOG_ERR, "lp_print write: %m" );
526 if ( read( s, buf, 1 ) != 1 ) {
527 syslog( LOG_ERR, "lp_print read: %m" );
531 lp_disconn_unix( s );
533 if ( buf[ 0 ] != '\0' ) {
534 syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
538 syslog( LOG_INFO, "lp_print queued" );
542 int lp_disconn_unix( fd )
544 return( close( fd ));
550 struct sockaddr_un saun;
552 if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
553 syslog( LOG_ERR, "lp_conn_unix socket: %m" );
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 );
569 int lp_disconn_inet( int fd )
571 return( close( fd ));
576 int privfd, port = IPPORT_RESERVED - 1;
577 struct sockaddr_in sin;
581 if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
582 syslog( LOG_ERR, "printer/tcp: unknown service\n" );
586 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
587 syslog( LOG_ERR, "gethostname: %m" );
591 if (( hp = gethostbyname( hostname )) == NULL ) {
592 syslog( LOG_ERR, "%s: unknown host\n", hostname );
596 if (( privfd = rresvport( &port )) < 0 ) {
597 syslog( LOG_ERR, "lp_connect: socket: %m" );
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;
608 if ( connect( privfd, (struct sockaddr *)&sin,
609 sizeof( struct sockaddr_in )) < 0 ) {
610 syslog( LOG_ERR, "lp_connect: %m" );
624 if (( s = lp_conn_inet()) < 0 ) {
625 syslog( LOG_ERR, "lp_rmjob: %m" );
629 if ( lp.lp_person == NULL ) {
633 sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
635 if ( write( s, buf, n ) != n ) {
636 syslog( LOG_ERR, "lp_rmjob write: %m" );
637 lp_disconn_inet( s );
640 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
641 syslog( LOG_DEBUG, "read %.*s", n, buf );
644 lp_disconn_inet( s );
648 char *kw_rank = "Rank";
649 char *kw_active = "active";
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: ";
661 char buf[ 1024 ], *start, *stop, *p, *q;
662 int linelength, crlflength;
663 static struct papfile pf;
666 if (( s = lp_conn_unix()) < 0 ) {
667 syslog( LOG_ERR, "lp_queue: %m" );
671 sprintf( buf, "\3%s\n", printer->p_printer );
673 if ( write( s, buf, n ) != n ) {
674 syslog( LOG_ERR, "lp_queue write: %m" );
675 lp_disconn_unix( s );
678 pf.pf_state = PF_BOT;
680 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
681 append( &pf, buf, n );
685 if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
687 stop = start + linelength;
688 for ( p = start; p < stop; p++ ) {
689 if ( *p == ' ' || *p == '\t' ) {
694 CONSUME( &pf , linelength + crlflength);
699 * Keys: "Rank", a number, "active"
700 * Anything else is status.
703 if ( len == strlen( kw_rank ) &&
704 strncmp( kw_rank, start, len ) == 0 ) {
705 CONSUME( &pf, linelength + crlflength );
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 );
715 for ( ; p < stop; p++ ) {
716 if ( *p != ' ' && *p != '\t' ) {
720 for ( q = p; p < stop; p++ ) {
721 if ( *p == ' ' || *p == '\t' ) {
726 append( out, ".\n", 2 );
727 CONSUME( &pf, linelength + crlflength );
730 append( out, tag_owner, strlen( tag_owner ));
731 append( out, q, p - q );
732 append( out, "\n", 1 );
734 for ( ; p < stop; p++ ) {
735 if ( *p != ' ' && *p != '\t' ) {
739 for ( q = p; p < stop; p++ ) {
740 if ( *p == ' ' || *p == '\t' ) {
745 append( out, ".\n", 2 );
746 CONSUME( &pf , linelength + crlflength );
749 append( out, tag_job, strlen( tag_job ));
750 append( out, q, p - q );
751 append( out, "\n", 1 );
753 for ( ; p < stop; p++ ) {
754 if ( *p != ' ' && *p != '\t' ) {
758 for ( q = p, p = stop; p > q; p-- ) {
759 if ( *p == ' ' || *p == '\t' ) {
763 for ( ; p > q; p-- ) {
764 if ( *p != ' ' && *p != '\t' ) {
768 for ( ; p > q; p-- ) {
769 if ( *p == ' ' || *p == '\t' ) {
774 append( out, ".\n", 2 );
775 CONSUME( &pf, linelength + crlflength );
778 append( out, tag_files, strlen( tag_files ));
779 append( out, q, p - q );
780 append( out, "\n", 1 );
782 for ( ; p < stop; p++ ) {
783 if ( *p != ' ' && *p != '\t' ) {
787 append( out, tag_size, strlen( tag_size ));
788 append( out, p, stop - p );
789 append( out, "\n.\n", 3 );
791 CONSUME( &pf, linelength + crlflength );
796 append( out, tag_status, strlen( tag_status ));
797 append( out, start, linelength );
798 append( out, "\n.\n", 3 );
800 CONSUME( &pf, linelength + crlflength );
802 append( out, "*\n", 2 );
803 lp_disconn_unix( s );