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.
46 #include <sys/param.h>
47 #include <sys/syslog.h>
49 #include <sys/socket.h>
54 #if defined( sun ) && defined( __svr4__ )
55 #include </usr/ucbinclude/sys/file.h>
60 #include <netinet/in.h>
62 #include <netatalk/at.h>
63 #include <atalk/atp.h>
64 #include <atalk/paths.h>
81 /* These functions aren't used outside of lp.c */
83 int lp_disconn_inet( int );
85 int lp_disconn_unix( int );
87 char hostname[ MAXHOSTNAMELEN ];
89 extern struct sockaddr_at *sat;
100 #define LP_INIT (1<<0)
101 #define LP_OPEN (1<<1)
102 #define LP_PIPE (1<<2)
103 #define LP_CONNECT (1<<3)
104 #define LP_QUEUE (1<<4)
106 void lp_person( person )
109 if ( lp.lp_person != NULL ) {
110 free( lp.lp_person );
112 if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
113 syslog( LOG_ERR, "malloc: %m" );
116 strcpy( lp.lp_person, person );
126 if ( lp.lp_person == NULL ) {
129 err = ABS_canprint( lp.lp_person, printer->p_role, printer->p_srvid,
131 printer->p_pagecost = floor( atof( cost ) * 10000.0 );
132 printer->p_balance = atof( balance ) + atof( cost );
133 return( err < 0 ? -1 : 0 );
140 if ( lp.lp_host != NULL ) {
143 if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
144 syslog( LOG_ERR, "malloc: %m" );
147 strcpy( lp.lp_host, host );
155 if ( lp.lp_job != NULL ) {
158 if (( lp.lp_job = (char *)malloc( strlen( job ) + 1 )) == NULL ) {
159 syslog( LOG_ERR, "malloc: %m" );
162 for ( p = job, q = lp.lp_job; *p != '\0'; p++, q++ ) {
163 if ( !isascii( *p ) || !isprint( *p ) || *p == '\\' ) {
172 int lp_init( out, sat )
174 struct sockaddr_at *sat;
177 char *cp, buf[ BUFSIZ ];
179 int authenticated = 0;
185 if ( printer->p_flags & P_AUTH ) {
188 /* cap style "log on to afp server before printing" authentication */
190 if ( printer->p_authprintdir && (printer->p_flags & P_AUTH_CAP) ) {
191 int addr_net = ntohs( sat->sat_addr.s_net );
192 int addr_node = sat->sat_addr.s_node;
193 char addr_filename[256];
198 sprintf(addr_filename, "%s/net%d.%dnode%d",
199 printer->p_authprintdir, addr_net/256, addr_net%256,
201 if (stat(addr_filename, &cap_st) == 0) {
202 if ((cap_file = fopen(addr_filename, "r")) != NULL) {
203 if (fscanf(cap_file, "%s", username) != EOF) {
204 if (getpwnam(username) != NULL ) {
205 syslog(LOG_INFO, "CAP authenticated %s", username);
209 syslog(LOG_INFO, "CAP error: invalid username: '%s'", username);
212 syslog(LOG_INFO, "CAP error: could not read username");
215 syslog(LOG_INFO, "CAP error: %m");
218 syslog(LOG_INFO, "CAP error: %m");
222 if ( printer->p_flags & P_AUTH_PSSP ) {
223 if ( lp.lp_person != NULL ) {
228 if ( authenticated == 0 ) {
229 syslog( LOG_ERR, "lp_init: must authenticate" );
230 spoolerror( out, "Authentication required." );
235 if (( printer->p_flags & P_ACCOUNT ) && printer->p_pagecost > 0 &&
236 ! ABS_canprint( lp.lp_person, printer->p_role,
237 printer->p_srvid, cost, balance )) {
238 syslog( LOG_ERR, "lp_init: no ABS funds" );
239 spoolerror( out, "No ABS funds available." );
245 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
246 syslog( LOG_ERR, "gethostname: %m" );
250 if ( lp.lp_flags & LP_INIT ) {
251 syslog( LOG_ERR, "lp_init: already inited, die!" );
259 if ( printer->p_flags & P_SPOOLED ) {
260 /* check if queuing is enabled: mode & 010 on lock file */
261 if ( stat( printer->p_lock, &st ) < 0 ) {
262 syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
263 spoolerror( out, NULL );
266 if ( st.st_mode & 010 ) {
267 syslog( LOG_INFO, "lp_init: queuing is disabled" );
268 spoolerror( out, "Queuing is disabled." );
272 if (( fd = open( ".seq", O_RDWR|O_CREAT, 0661 )) < 0 ) {
273 syslog( LOG_ERR, "lp_init: can't create .seq" );
274 spoolerror( out, NULL );
278 if ( flock( fd, LOCK_EX ) < 0 ) {
279 syslog( LOG_ERR, "lp_init: can't lock .seq" );
280 spoolerror( out, NULL );
285 if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
286 syslog( LOG_ERR, "lp_init read: %m" );
287 spoolerror( out, NULL );
291 for ( cp = buf; len; len--, cp++ ) {
292 if ( *cp < '0' || *cp > '9' ) {
295 n = n * 10 + ( *cp - '0' );
300 n = ( n + 1 ) % 1000;
301 sprintf( buf, "%03d\n", n );
303 write( fd, buf, strlen( buf ));
306 lp.lp_flags |= LP_PIPE;
307 lp.lp_seq = getpid();
310 lp.lp_flags |= LP_INIT;
314 int lp_open( out, sat )
316 struct sockaddr_at *sat;
318 char name[ MAXPATHLEN ];
320 struct passwd *pwent;
322 if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out, sat ) != 0 ) {
325 if ( lp.lp_flags & LP_OPEN ) {
326 syslog( LOG_ERR, "lp_open already open" );
330 if ( lp.lp_flags & LP_PIPE ) {
331 /* go right to program */
332 if (lp.lp_person != NULL) {
333 if((pwent = getpwnam(lp.lp_person)) != NULL) {
334 if(setreuid(pwent->pw_uid, pwent->pw_uid) != 0) {
335 syslog(LOG_INFO, "setreuid error: %m");
338 syslog(LOG_INFO, "Error getting username (%s)", lp.lp_person);
341 if (( lp.lp_stream = popen( printer->p_printer, "w" )) == NULL ) {
342 syslog( LOG_ERR, "lp_open popen %s: %m", printer->p_printer );
343 spoolerror( out, NULL );
347 sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
349 if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
350 syslog( LOG_ERR, "lp_open %s: %m", name );
351 spoolerror( out, NULL );
355 if (lp.lp_person != NULL) {
356 if ((pwent = getpwnam(lp.lp_person)) == NULL) {
357 syslog(LOG_ERR, "getpwnam %s: no such user", lp.lp_person);
358 spoolerror( out, NULL );
362 if ((pwent = getpwnam(printer->p_operator)) == NULL) {
363 syslog(LOG_ERR, "getpwnam %s: no such user", printer->p_operator);
364 spoolerror( out, NULL );
369 if (fchown(fd, pwent->pw_uid, -1) < 0) {
370 syslog(LOG_ERR, "chown %s %s: %m", pwent->pw_name, name);
371 spoolerror( out, NULL );
375 if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
376 syslog( LOG_ERR, "lp_open fdopen: %m" );
377 spoolerror( out, NULL );
381 lp.lp_flags |= LP_OPEN;
388 if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
391 fclose( lp.lp_stream );
393 lp.lp_flags &= ~LP_OPEN;
397 int lp_write( buf, len )
401 if (( lp.lp_flags & LP_OPEN ) == 0 ) {
405 if ( fwrite( buf, 1, len, lp.lp_stream ) != len ) {
406 syslog( LOG_ERR, "lp_write: %m" );
414 char name[ MAXPATHLEN ];
417 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
421 if ( lp.lp_flags & LP_OPEN ) {
425 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
426 sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
427 if ( unlink( name ) < 0 ) {
428 syslog( LOG_ERR, "lp_cancel unlink %s: %m", name );
436 * Create printcap control file, signal printer. Errors here should
437 * remove queue files.
443 char buf[ MAXPATHLEN ];
444 char tfname[ MAXPATHLEN ];
445 char cfname[ MAXPATHLEN ];
450 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
455 if ( printer->p_flags & P_SPOOLED ) {
456 sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
457 if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
458 syslog( LOG_ERR, "lp_print %s: %m", tfname );
461 if (( cfile = fdopen( fd, "w" )) == NULL ) {
462 syslog( LOG_ERR, "lp_print %s: %m", tfname );
465 fprintf( cfile, "H%s\n", hostname ); /* XXX lp_host? */
467 if ( lp.lp_person ) {
468 fprintf( cfile, "P%s\n", lp.lp_person );
470 fprintf( cfile, "P%s\n", printer->p_operator );
473 if ( lp.lp_job && *lp.lp_job ) {
474 fprintf( cfile, "J%s\n", lp.lp_job );
475 fprintf( cfile, "T%s\n", lp.lp_job );
477 fprintf( cfile, "JMac Job\n" );
478 fprintf( cfile, "TMac Job\n" );
481 fprintf( cfile, "C%s\n", hostname ); /* XXX lp_host? */
483 if ( lp.lp_person ) {
484 fprintf( cfile, "L%s\n", lp.lp_person );
486 fprintf( cfile, "L%s\n", printer->p_operator );
489 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
490 fprintf( cfile, "fdf%c%03d%s\n", letter, lp.lp_seq, hostname );
491 fprintf( cfile, "Udf%c%03d%s\n", letter, lp.lp_seq, hostname );
494 if ( lp.lp_job && *lp.lp_job ) {
495 fprintf( cfile, "N%s\n", lp.lp_job );
497 fprintf( cfile, "NMac Job\n" );
501 sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
502 if ( link( tfname, cfname ) < 0 ) {
503 syslog( LOG_ERR, "lp_print can't link %s to %s: %m", cfname,
509 if (( s = lp_conn_unix()) < 0 ) {
510 syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
514 sprintf( buf, "\1%s\n", printer->p_printer );
516 if ( write( s, buf, n ) != n ) {
517 syslog( LOG_ERR, "lp_print write: %m" );
520 if ( read( s, buf, 1 ) != 1 ) {
521 syslog( LOG_ERR, "lp_print read: %m" );
525 lp_disconn_unix( s );
527 if ( buf[ 0 ] != '\0' ) {
528 syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
532 syslog( LOG_INFO, "lp_print queued" );
536 int lp_disconn_unix( fd )
538 return( close( fd ));
544 struct sockaddr_un saun;
546 if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
547 syslog( LOG_ERR, "lp_conn_unix socket: %m" );
550 bzero( &saun, sizeof( struct sockaddr_un ));
551 saun.sun_family = AF_UNIX;
552 strcpy( saun.sun_path, _PATH_DEVPRINTER );
553 if ( connect( s, (struct sockaddr *)&saun,
554 strlen( saun.sun_path ) + 2 ) < 0 ) {
555 syslog( LOG_ERR, "lp_conn_unix connect %s: %m", saun.sun_path );
563 int lp_disconn_inet( int fd )
565 return( close( fd ));
570 int privfd, port = IPPORT_RESERVED - 1;
571 struct sockaddr_in sin;
575 if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
576 syslog( LOG_ERR, "printer/tcp: unknown service\n" );
580 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
581 syslog( LOG_ERR, "gethostname: %m" );
585 if (( hp = gethostbyname( hostname )) == NULL ) {
586 syslog( LOG_ERR, "%s: unknown host\n", hostname );
590 if (( privfd = rresvport( &port )) < 0 ) {
591 syslog( LOG_ERR, "lp_connect: socket: %m" );
596 bzero( &sin, sizeof( struct sockaddr_in ));
597 sin.sin_family = AF_INET;
598 /* sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
599 bcopy( hp->h_addr, &sin.sin_addr, hp->h_length );
600 sin.sin_port = sp->s_port;
602 if ( connect( privfd, (struct sockaddr *)&sin,
603 sizeof( struct sockaddr_in )) < 0 ) {
604 syslog( LOG_ERR, "lp_connect: %m" );
618 if (( s = lp_conn_inet()) < 0 ) {
619 syslog( LOG_ERR, "lp_rmjob: %m" );
623 if ( lp.lp_person == NULL ) {
627 sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
629 if ( write( s, buf, n ) != n ) {
630 syslog( LOG_ERR, "lp_rmjob write: %m" );
631 lp_disconn_inet( s );
634 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
635 syslog( LOG_DEBUG, "read %.*s", n, buf );
638 lp_disconn_inet( s );
642 char *kw_rank = "Rank";
643 char *kw_active = "active";
645 char *tag_rank = "rank: ";
646 char *tag_owner = "owner: ";
647 char *tag_job = "job: ";
648 char *tag_files = "files: ";
649 char *tag_size = "size: ";
650 char *tag_status = "status: ";
655 char buf[ 1024 ], *start, *stop, *p, *q;
656 int linelength, crlflength;
657 static struct papfile pf;
660 if (( s = lp_conn_unix()) < 0 ) {
661 syslog( LOG_ERR, "lp_queue: %m" );
665 sprintf( buf, "\3%s\n", printer->p_printer );
667 if ( write( s, buf, n ) != n ) {
668 syslog( LOG_ERR, "lp_queue write: %m" );
669 lp_disconn_unix( s );
672 pf.pf_state = PF_BOT;
674 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
675 append( &pf, buf, n );
679 if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
681 stop = start + linelength;
682 for ( p = start; p < stop; p++ ) {
683 if ( *p == ' ' || *p == '\t' ) {
688 CONSUME( &pf , linelength + crlflength);
693 * Keys: "Rank", a number, "active"
694 * Anything else is status.
697 if ( len == strlen( kw_rank ) &&
698 strncmp( kw_rank, start, len ) == 0 ) {
699 CONSUME( &pf, linelength + crlflength );
702 if (( len == strlen( kw_active ) &&
703 strncmp( kw_active, start, len ) == 0 ) ||
704 isdigit( *start )) { /* a job line */
705 append( out, tag_rank, strlen( tag_rank ));
706 append( out, start, p - start );
707 append( out, "\n", 1 );
709 for ( ; p < stop; p++ ) {
710 if ( *p != ' ' && *p != '\t' ) {
714 for ( q = p; p < stop; p++ ) {
715 if ( *p == ' ' || *p == '\t' ) {
720 append( out, ".\n", 2 );
721 CONSUME( &pf, linelength + crlflength );
724 append( out, tag_owner, strlen( tag_owner ));
725 append( out, q, p - q );
726 append( out, "\n", 1 );
728 for ( ; p < stop; p++ ) {
729 if ( *p != ' ' && *p != '\t' ) {
733 for ( q = p; p < stop; p++ ) {
734 if ( *p == ' ' || *p == '\t' ) {
739 append( out, ".\n", 2 );
740 CONSUME( &pf , linelength + crlflength );
743 append( out, tag_job, strlen( tag_job ));
744 append( out, q, p - q );
745 append( out, "\n", 1 );
747 for ( ; p < stop; p++ ) {
748 if ( *p != ' ' && *p != '\t' ) {
752 for ( q = p, p = stop; p > q; p-- ) {
753 if ( *p == ' ' || *p == '\t' ) {
757 for ( ; p > q; p-- ) {
758 if ( *p != ' ' && *p != '\t' ) {
762 for ( ; p > q; p-- ) {
763 if ( *p == ' ' || *p == '\t' ) {
768 append( out, ".\n", 2 );
769 CONSUME( &pf, linelength + crlflength );
772 append( out, tag_files, strlen( tag_files ));
773 append( out, q, p - q );
774 append( out, "\n", 1 );
776 for ( ; p < stop; p++ ) {
777 if ( *p != ' ' && *p != '\t' ) {
781 append( out, tag_size, strlen( tag_size ));
782 append( out, p, stop - p );
783 append( out, "\n.\n", 3 );
785 CONSUME( &pf, linelength + crlflength );
790 append( out, tag_status, strlen( tag_status ));
791 append( out, start, linelength );
792 append( out, "\n.\n", 3 );
794 CONSUME( &pf, linelength + crlflength );
796 append( out, "*\n", 2 );
797 lp_disconn_unix( s );