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>
73 char hostname[ MAXHOSTNAMELEN ];
75 extern struct sockaddr_at *sat;
77 /* initialize printing interface */
79 /* cancel current job */
81 /* print current job */
84 /* open a file for spooling */
86 /* open a buffer to the current open file */
88 /* close current spooling file */
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)
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 == '\\' ) {
174 struct sockaddr_at *sat;
177 char *cp, buf[ BUFSIZ ];
179 int authenticated = 0;
186 int addr_net, addr_node;
189 char addr_filename[256];
192 if ( printer->p_flags & P_AUTH ) {
195 if ( printer->p_flags & P_AUTH_CAP ) {
196 addr_net = ntohs( sat->sat_addr.s_net );
197 addr_node = sat->sat_addr.s_node;
198 sprintf(addr_filename, "%s/net%d.%dnode%d", CAPDIR, addr_net/256, addr_net%256, addr_node);
199 if (stat(addr_filename, &cap_st) == 0) {
200 if ((cap_file = fopen(addr_filename, "r")) != NULL) {
201 if (fscanf(cap_file, "%s", username) != EOF) {
202 if (getpwnam(username) != NULL ) {
203 syslog(LOG_INFO, "CAP authenticated %s", username);
207 syslog(LOG_INFO, "CAP error: invalid username: '%s'", username);
210 syslog(LOG_INFO, "CAP error: could not read username");
213 syslog(LOG_INFO, "CAP error: %m");
216 syslog(LOG_INFO, "CAP error: %m");
221 if ( printer->p_flags & P_AUTH_PSSP ) {
222 if ( lp.lp_person != NULL ) {
227 if ( authenticated == 0 ) {
228 syslog( LOG_ERR, "lp_init: must authenticate" );
229 spoolerror( out, "Authentication required." );
234 if (( printer->p_flags & P_ACCOUNT ) && printer->p_pagecost > 0 &&
235 ! ABS_canprint( lp.lp_person, printer->p_role,
236 printer->p_srvid, cost, balance )) {
237 syslog( LOG_ERR, "lp_init: no ABS funds" );
238 spoolerror( out, "No ABS funds available." );
244 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
245 syslog( LOG_ERR, "gethostname: %m" );
249 if ( lp.lp_flags & LP_INIT ) {
250 syslog( LOG_ERR, "lp_init: already inited, die!" );
258 if ( printer->p_flags & P_SPOOLED ) {
259 /* check if queuing is enabled: mode & 010 on lock file */
260 if ( stat( printer->p_lock, &st ) < 0 ) {
261 syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
262 spoolerror( out, NULL );
265 if ( st.st_mode & 010 ) {
266 syslog( LOG_INFO, "lp_init: queuing is disabled" );
267 spoolerror( out, "Queuing is disabled." );
271 if (( fd = open( ".seq", O_RDWR|O_CREAT, 0661 )) < 0 ) {
272 syslog( LOG_ERR, "lp_init: can't create .seq" );
273 spoolerror( out, NULL );
277 if ( flock( fd, LOCK_EX ) < 0 ) {
278 syslog( LOG_ERR, "lp_init: can't lock .seq" );
279 spoolerror( out, NULL );
284 if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
285 syslog( LOG_ERR, "lp_init read: %m" );
286 spoolerror( out, NULL );
290 for ( cp = buf; len; len--, cp++ ) {
291 if ( *cp < '0' || *cp > '9' ) {
294 n = n * 10 + ( *cp - '0' );
299 n = ( n + 1 ) % 1000;
300 sprintf( buf, "%03d\n", n );
302 write( fd, buf, strlen( buf ));
305 lp.lp_flags |= LP_PIPE;
306 lp.lp_seq = getpid();
309 lp.lp_flags |= LP_INIT;
315 struct sockaddr_at *sat;
317 char name[ MAXPATHLEN ];
319 struct passwd *pwent;
321 if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out, sat ) != 0 ) {
324 if ( lp.lp_flags & LP_OPEN ) {
325 syslog( LOG_ERR, "lp_open already open" );
329 if ( lp.lp_flags & LP_PIPE ) {
330 /* go right to program */
331 if (lp.lp_person != NULL) {
332 if((pwent = getpwnam(lp.lp_person)) != NULL) {
333 if(setreuid(pwent->pw_uid, pwent->pw_uid) != 0) {
334 syslog(LOG_INFO, "setreuid error: %m");
337 syslog(LOG_INFO, "Error getting username (%s)", lp.lp_person);
340 if (( lp.lp_stream = popen( printer->p_printer, "w" )) == NULL ) {
341 syslog( LOG_ERR, "lp_open popen %s: %m", printer->p_printer );
342 spoolerror( out, NULL );
346 sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
348 if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
349 syslog( LOG_ERR, "lp_open %s: %m", name );
350 spoolerror( out, NULL );
353 if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
354 syslog( LOG_ERR, "lp_open fdopen: %m" );
355 spoolerror( out, NULL );
359 lp.lp_flags |= LP_OPEN;
366 if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
369 fclose( lp.lp_stream );
371 lp.lp_flags &= ~LP_OPEN;
379 if (( lp.lp_flags & LP_OPEN ) == 0 ) {
383 if ( fwrite( buf, 1, len, lp.lp_stream ) != len ) {
384 syslog( LOG_ERR, "lp_write: %m" );
392 char name[ MAXPATHLEN ];
395 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
399 if ( lp.lp_flags & LP_OPEN ) {
403 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
404 sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
405 if ( unlink( name ) < 0 ) {
406 syslog( LOG_ERR, "lp_cancel unlink %s: %m", name );
414 * Create printcap control file, signal printer. Errors here should
415 * remove queue files.
421 char buf[ MAXPATHLEN ];
422 char tfname[ MAXPATHLEN ];
423 char cfname[ MAXPATHLEN ];
428 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
433 if ( printer->p_flags & P_SPOOLED ) {
434 sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
435 if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
436 syslog( LOG_ERR, "lp_print %s: %m", tfname );
439 if (( cfile = fdopen( fd, "w" )) == NULL ) {
440 syslog( LOG_ERR, "lp_print %s: %m", tfname );
443 fprintf( cfile, "H%s\n", hostname ); /* XXX lp_host? */
445 if ( lp.lp_person ) {
446 fprintf( cfile, "P%s\n", lp.lp_person );
448 fprintf( cfile, "P%s\n", printer->p_operator );
451 if ( lp.lp_job && *lp.lp_job ) {
452 fprintf( cfile, "J%s\n", lp.lp_job );
453 fprintf( cfile, "T%s\n", lp.lp_job );
455 fprintf( cfile, "JMac Job\n" );
456 fprintf( cfile, "TMac Job\n" );
459 fprintf( cfile, "C%s\n", hostname ); /* XXX lp_host? */
461 if ( lp.lp_person ) {
462 fprintf( cfile, "L%s\n", lp.lp_person );
464 fprintf( cfile, "L%s\n", printer->p_operator );
467 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
468 fprintf( cfile, "fdf%c%03d%s\n", letter, lp.lp_seq, hostname );
469 fprintf( cfile, "Udf%c%03d%s\n", letter, lp.lp_seq, hostname );
472 if ( lp.lp_job && *lp.lp_job ) {
473 fprintf( cfile, "N%s\n", lp.lp_job );
475 fprintf( cfile, "NMac Job\n" );
479 sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
480 if ( link( tfname, cfname ) < 0 ) {
481 syslog( LOG_ERR, "lp_print can't link %s to %s: %m", cfname,
487 if (( s = lp_conn_unix()) < 0 ) {
488 syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
492 sprintf( buf, "\1%s\n", printer->p_printer );
494 if ( write( s, buf, n ) != n ) {
495 syslog( LOG_ERR, "lp_print write: %m" );
498 if ( read( s, buf, 1 ) != 1 ) {
499 syslog( LOG_ERR, "lp_print read: %m" );
503 lp_disconn_unix( s );
505 if ( buf[ 0 ] != '\0' ) {
506 syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
510 syslog( LOG_INFO, "lp_print queued" );
514 lp_disconn_unix( fd )
516 return( close( fd ));
522 struct sockaddr_un saun;
524 if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
525 syslog( LOG_ERR, "lp_conn_unix socket: %m" );
528 bzero( &saun, sizeof( struct sockaddr_un ));
529 saun.sun_family = AF_UNIX;
530 strcpy( saun.sun_path, _PATH_DEVPRINTER );
531 if ( connect( s, (struct sockaddr *)&saun,
532 strlen( saun.sun_path ) + 2 ) < 0 ) {
533 syslog( LOG_ERR, "lp_conn_unix connect %s: %m", saun.sun_path );
541 lp_disconn_inet( fd )
543 return( close( fd ));
548 int privfd, port = IPPORT_RESERVED - 1;
549 struct sockaddr_in sin;
553 if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
554 syslog( LOG_ERR, "printer/tcp: unknown service\n" );
558 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
559 syslog( LOG_ERR, "gethostname: %m" );
563 if (( hp = gethostbyname( hostname )) == NULL ) {
564 syslog( LOG_ERR, "%s: unknown host\n", hostname );
568 if (( privfd = rresvport( &port )) < 0 ) {
569 syslog( LOG_ERR, "lp_connect: socket: %m" );
574 bzero( &sin, sizeof( struct sockaddr_in ));
575 sin.sin_family = AF_INET;
576 /* sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
577 bcopy( hp->h_addr, &sin.sin_addr, hp->h_length );
578 sin.sin_port = sp->s_port;
580 if ( connect( privfd, (struct sockaddr *)&sin,
581 sizeof( struct sockaddr_in )) < 0 ) {
582 syslog( LOG_ERR, "lp_connect: %m" );
596 if (( s = lp_conn_inet()) < 0 ) {
597 syslog( LOG_ERR, "lp_rmjob: %m" );
601 if ( lp.lp_person == NULL ) {
605 sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
607 if ( write( s, buf, n ) != n ) {
608 syslog( LOG_ERR, "lp_rmjob write: %m" );
609 lp_disconn_inet( s );
612 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
613 syslog( LOG_DEBUG, "read %.*s", n, buf );
616 lp_disconn_inet( s );
620 char *kw_rank = "Rank";
621 char *kw_active = "active";
623 char *tag_rank = "rank: ";
624 char *tag_owner = "owner: ";
625 char *tag_job = "job: ";
626 char *tag_files = "files: ";
627 char *tag_size = "size: ";
628 char *tag_status = "status: ";
633 char buf[ 1024 ], *start, *stop, *p, *q;
634 int linelength, crlflength;
635 static struct papfile pf;
638 if (( s = lp_conn_unix()) < 0 ) {
639 syslog( LOG_ERR, "lp_queue: %m" );
643 sprintf( buf, "\3%s\n", printer->p_printer );
645 if ( write( s, buf, n ) != n ) {
646 syslog( LOG_ERR, "lp_queue write: %m" );
647 lp_disconn_unix( s );
650 pf.pf_state = PF_BOT;
652 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
653 append( &pf, buf, n );
657 if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
659 stop = start + linelength;
660 for ( p = start; p < stop; p++ ) {
661 if ( *p == ' ' || *p == '\t' ) {
666 CONSUME( &pf , linelength + crlflength);
671 * Keys: "Rank", a number, "active"
672 * Anything else is status.
675 if ( len == strlen( kw_rank ) &&
676 strncmp( kw_rank, start, len ) == 0 ) {
677 CONSUME( &pf, linelength + crlflength );
680 if (( len == strlen( kw_active ) &&
681 strncmp( kw_active, start, len ) == 0 ) ||
682 isdigit( *start )) { /* a job line */
683 append( out, tag_rank, strlen( tag_rank ));
684 append( out, start, p - start );
685 append( out, "\n", 1 );
687 for ( ; p < stop; p++ ) {
688 if ( *p != ' ' && *p != '\t' ) {
692 for ( q = p; p < stop; p++ ) {
693 if ( *p == ' ' || *p == '\t' ) {
698 append( out, ".\n", 2 );
699 CONSUME( &pf, linelength + crlflength );
702 append( out, tag_owner, strlen( tag_owner ));
703 append( out, q, p - q );
704 append( out, "\n", 1 );
706 for ( ; p < stop; p++ ) {
707 if ( *p != ' ' && *p != '\t' ) {
711 for ( q = p; p < stop; p++ ) {
712 if ( *p == ' ' || *p == '\t' ) {
717 append( out, ".\n", 2 );
718 CONSUME( &pf , linelength + crlflength );
721 append( out, tag_job, strlen( tag_job ));
722 append( out, q, p - q );
723 append( out, "\n", 1 );
725 for ( ; p < stop; p++ ) {
726 if ( *p != ' ' && *p != '\t' ) {
730 for ( q = p, p = stop; p > q; p-- ) {
731 if ( *p == ' ' || *p == '\t' ) {
735 for ( ; p > q; p-- ) {
736 if ( *p != ' ' && *p != '\t' ) {
740 for ( ; p > q; p-- ) {
741 if ( *p == ' ' || *p == '\t' ) {
746 append( out, ".\n", 2 );
747 CONSUME( &pf, linelength + crlflength );
750 append( out, tag_files, strlen( tag_files ));
751 append( out, q, p - q );
752 append( out, "\n", 1 );
754 for ( ; p < stop; p++ ) {
755 if ( *p != ' ' && *p != '\t' ) {
759 append( out, tag_size, strlen( tag_size ));
760 append( out, p, stop - p );
761 append( out, "\n.\n", 3 );
763 CONSUME( &pf, linelength + crlflength );
768 append( out, tag_status, strlen( tag_status ));
769 append( out, start, linelength );
770 append( out, "\n.\n", 3 );
772 CONSUME( &pf, linelength + crlflength );
774 append( out, "*\n", 2 );
775 lp_disconn_unix( s );