]> arthur.barton.de Git - netatalk.git/blob - etc/papd/main.c
AC_HEADER_STDC autoconf change
[netatalk.git] / etc / papd / main.c
1 /*
2  * $Id: main.c,v 1.11 2001-09-06 20:00:59 rufustfirefly Exp $
3  *
4  * Copyright (c) 1990,1995 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/uio.h>
15 #if defined( sun ) && defined( __svr4__ )
16 #include </usr/ucbinclude/sys/file.h>
17 #else /* sun && __svr4__ */
18 #include <sys/file.h>
19 #endif /* sun && __svr4__ */
20 #include <sys/socket.h>
21 #include <sys/syslog.h>
22
23 /* POSIX.1 sys/wait.h check */
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_WAIT_H
26 #include <sys/wait.h>
27 #endif /* HAVE_SYS_WAIT_H */
28 #ifndef WEXITSTATUS
29 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
30 #endif /* ! WEXITSTATUS */
31 #ifndef WIFEXITED
32 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
33 #endif /* ! WIFEXITED */
34
35 #include <errno.h>
36
37 /* STDC check */
38 #if STDC_HEADERS
39 #include <string.h>
40 #else /* STDC_HEADERS */
41 #ifndef HAVE_STRCHR
42 #define strchr index
43 #define strrchr index
44 #endif /* HAVE_STRCHR */
45 char *strchr (), *strrchr ();
46 #ifndef HAVE_MEMCPY
47 #define memcpy(d,s,n) bcopy ((s), (d), (n))
48 #define memmove(d,s,n) bcopy ((s), (d), (n))
49 #endif /* ! HAVE_MEMCPY */
50 #endif /* STDC_HEADERS */
51
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <netdb.h>
56 #include <fcntl.h>
57 #ifdef HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif /* HAVE_UNISTD_H */
60
61 #include <netatalk/endian.h>
62 #include <netatalk/at.h>
63 #include <atalk/compat.h>
64 #include <atalk/atp.h>
65 #include <atalk/pap.h>
66 #include <atalk/paths.h>
67 #include <atalk/util.h>
68 #include <atalk/nbp.h>
69
70 #include "printer.h"
71 #include "printcap.h"
72 #include "session.h"
73 #include "uam_auth.h"
74
75 #define _PATH_PAPDPPDFILE       ".ppd"
76
77 #define PIPED_STATUS    "status: print spooler processing job"
78
79 struct printer  defprinter;
80 struct printer  *printers = NULL;
81
82 int             debug = 0;
83 char            *conffile = _PATH_PAPDCONF;
84 char            *printcap = _PATH_PAPDPRINTCAP;
85 unsigned char   connid, quantum, sock, oquantum = PAP_MAXQUANTUM;
86 char            *cannedstatus = PIPED_STATUS;
87 struct printer  *printer = NULL;
88 char            *version = VERSION;
89 static char      *pidfile = _PATH_PAPDLOCK;
90
91 char            *uamlist;
92 char            *uampath = _PATH_PAPDUAMPATH;
93
94 /* Prototypes for locally used functions */
95 int getstatus( struct printer *pr, char *buf );
96 int rprintcap( struct printer *pr );
97 void getprinters( char *cf );
98
99
100 /* this only needs to be used by the server process */
101 static void papd_exit(const int i)
102 {
103   server_unlock(pidfile);
104   auth_unload();
105   exit(i);
106 }
107
108 #if !defined( ibm032 ) && !defined( _IBMR2 )
109     void
110 #endif /* ! ibm032 && ! _IBMR2 */
111 die( n )
112     int                 n;
113 {
114     struct printer      *pr;
115     struct at_addr      addr;
116
117     memset(&addr, 0, sizeof(addr));
118
119     for ( pr = printers; pr; pr = pr->p_next ) {
120         if ( pr->p_flags & P_REGISTERED ) {
121             if ( nbp_unrgstr( pr->p_name, pr->p_type, pr->p_zone, &addr ) < 0 ) {
122                 syslog( LOG_ERR, "can't unregister %s:%s@%s\n", pr->p_name,
123                         pr->p_type, pr->p_zone );
124                 papd_exit( n + 1 );
125             }
126             syslog( LOG_ERR, "unregister %s:%s@%s\n", pr->p_name, pr->p_type,
127                     pr->p_zone );
128         }
129     }
130     papd_exit( n );
131 }
132
133 #if !defined( ibm032 ) && !defined( _IBMR2 )
134     void
135 #endif /* ! ibm032 && ! _IBMR2 */
136 reap()
137 {
138     int         status;
139     int         pid;
140
141     while (( pid = wait3( &status, WNOHANG, 0 )) > 0 ) {
142         if ( WIFEXITED( status )) {
143             if ( WEXITSTATUS( status )) {
144                 syslog( LOG_ERR, "child %d exited with %d", pid,
145                         WEXITSTATUS( status ));
146             } else {
147                 syslog( LOG_INFO, "child %d done", pid );
148             }
149         } else {
150             if ( WIFSIGNALED( status )) {
151                 syslog( LOG_ERR, "child %d killed with %d", pid,
152                         WTERMSIG( status ));
153             } else {
154                 syslog( LOG_ERR, "child %d died", pid );
155             }
156         }
157     }
158     return;
159 }
160
161 char            rbuf[ 255 + 1 + 8 ];
162
163 int main( ac, av )
164     int         ac;
165     char        **av;
166 {
167     extern char         *optarg;
168
169     ATP                 atp;
170     struct atp_block    atpb;
171     struct sockaddr_at  sat;
172     struct sigaction    sv;
173     struct iovec        iov;
174     fd_set              fdset;
175     struct printer      *pr;
176     char                *p, hostname[ MAXHOSTNAMELEN ];
177     char                cbuf[ 8 ];
178     int                 c;
179
180     if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
181         perror( "gethostname" );
182         exit( 1 );
183     }
184     if (( p = strchr( hostname, '.' )) != 0 ) {
185         *p = '\0';
186     }
187     if (( defprinter.p_name = (char *)malloc( strlen( hostname ) + 1 ))
188             == NULL ) {
189         perror( "malloc" );
190         exit( 1 );
191     }
192     strcpy( defprinter.p_name, hostname );
193     defprinter.p_type = "LaserWriter";
194     defprinter.p_zone = "*";
195     memset(&defprinter.p_addr, 0, sizeof(defprinter.p_addr));
196     defprinter.p_ppdfile = _PATH_PAPDPPDFILE;
197 #ifdef __svr4__
198     defprinter.p_flags = P_PIPED;
199     defprinter.p_printer = "/usr/bin/lp -T PS";
200 #else /* __svr4__ */
201     defprinter.p_flags = P_SPOOLED;
202     defprinter.p_printer = "lp";
203 #endif /* __svr4__ */
204     defprinter.p_operator = "operator";
205     defprinter.p_spool = _PATH_PAPDSPOOLDIR;
206 #ifdef ABS_PRINT
207     defprinter.p_role = NULL;
208     defprinter.p_srvid = 0;
209 #endif /* ABS_PRINT */
210     defprinter.p_pagecost = 200;                /* default cost */
211     defprinter.p_pagecost_msg = NULL;
212     defprinter.p_lock = "lock";
213
214     while (( c = getopt( ac, av, "adf:p:P:" )) != EOF ) {
215         switch ( c ) {
216         case 'a' :              /* for compatibility with old papd */
217             break;
218
219         case 'd' :              /* debug */
220             debug++;
221             break;
222
223         case 'f' :              /* conffile */
224             conffile = optarg;
225             break;
226
227         case 'p' :              /* printcap */
228             printcap = optarg;
229             break;
230
231         case 'P' :
232             pidfile = optarg;
233             break;
234
235         default :
236             fprintf( stderr,
237                     "Usage:\t%s [ -d ] [ -f conffile ] [ -p printcap ]\n",
238                     *av );
239             exit( 1 );
240         }
241     }
242
243     getprinters( conffile );
244
245     switch (server_lock("papd", pidfile, debug)) {
246     case 0: /* open a couple things again in the child */
247       if ((c = open("/", O_RDONLY)) >= 0) {
248         dup2(c, 1);
249         dup2(c, 2);
250       }
251       break;
252     case -1:
253       exit(1);
254     default:
255       exit(0);
256     }      
257
258     /*
259      * Start logging.
260      */
261     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
262         p = av[ 0 ];
263     } else {
264         p++;
265     }
266 #ifdef ultrix
267     openlog( p, LOG_PID );
268 #else /* ultrix */
269     openlog( p, LOG_NDELAY|LOG_PID, LOG_LPR );
270 #endif /* ultrix */
271
272     syslog( LOG_INFO, "restart (%s)", version );
273
274     for ( pr = printers; pr; pr = pr->p_next ) {
275         if (( pr->p_flags & P_SPOOLED ) && rprintcap( pr ) < 0 ) {
276             syslog( LOG_ERR, "printcap problem: %s", pr->p_printer );
277         }
278         if (( pr->p_atp = atp_open( ATADDR_ANYPORT, &pr->p_addr )) == NULL ) {
279             syslog( LOG_ERR, "atp_open: %m" );
280             papd_exit( 1 );
281         }
282         if ( nbp_rgstr( atp_sockaddr( pr->p_atp ), pr->p_name, pr->p_type,
283                 pr->p_zone ) < 0 ) {
284             syslog( LOG_ERR, "can't register %s:%s@%s", pr->p_name, pr->p_type,
285                     pr->p_zone );
286             die( 1 );
287         }
288         if ( pr->p_flags & P_AUTH ) {
289                 syslog( LOG_INFO, "Authentication enabled: %s", pr->p_name );
290         }
291         else {
292                 syslog( LOG_INFO, "Authentication disabled: %s", pr->p_name );
293         }
294         syslog( LOG_INFO, "register %s:%s@%s", pr->p_name, pr->p_type,
295                 pr->p_zone );
296         pr->p_flags |= P_REGISTERED;
297     }
298
299     memset(&sv, 0, sizeof(sv));
300     sv.sa_handler = die;
301     sigemptyset( &sv.sa_mask );
302     sv.sa_flags = SA_RESTART;
303     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
304         syslog( LOG_ERR, "sigaction: %m" );
305         papd_exit( 1 );
306     }
307
308     sv.sa_handler = reap;
309     sigemptyset( &sv.sa_mask );
310     sv.sa_flags = SA_RESTART;
311     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
312         syslog( LOG_ERR, "sigaction: %m" );
313         papd_exit( 1 );
314     }
315
316     /*
317      * Load UAMS
318      */
319     auth_load(uampath, uamlist);
320
321     /*
322      * Begin accepting connections.
323      */
324     FD_ZERO( &fdset );
325     for (;;) {
326         for ( pr = printers; pr; pr = pr->p_next ) {
327             FD_SET( atp_fileno( pr->p_atp ), &fdset );
328         }
329         if (( c = select( FD_SETSIZE, &fdset, 0, 0, 0 )) < 0 ) {
330             if ( errno == EINTR ) {
331                 continue;
332             }
333             syslog( LOG_ERR, "select: %m" );
334             papd_exit( 1 );
335         }
336
337         for ( pr = printers; pr; pr = pr->p_next ) {
338             if ( FD_ISSET( atp_fileno( pr->p_atp ), &fdset )) {
339                 int             err = 0;
340
341                 memset( &sat, 0, sizeof( struct sockaddr_at ));
342 #ifdef BSD4_4
343                 sat.sat_len = sizeof( struct sockaddr_at );
344 #endif /* BSD4_4 */
345                 sat.sat_family = AF_APPLETALK;
346                 sat.sat_addr.s_net = ATADDR_ANYNET;
347                 sat.sat_addr.s_node = ATADDR_ANYNODE;
348                 sat.sat_port = ATADDR_ANYPORT;
349                 /* do an atp_rsel(), to prevent hangs */
350                 if (( c = atp_rsel( pr->p_atp, &sat, ATP_TREQ )) != ATP_TREQ ) {
351                     continue;
352                 }
353                 atpb.atp_saddr = &sat;
354                 atpb.atp_rreqdata = cbuf;
355                 atpb.atp_rreqdlen = sizeof( cbuf );
356                 if ( atp_rreq( pr->p_atp, &atpb ) < 0 ) {
357                     syslog( LOG_ERR, "atp_rreq: %m" );
358                     continue;
359                 }
360
361                 /* should check length of req buf */
362
363                 switch( cbuf[ 1 ] ) {
364                 case PAP_OPEN :
365                     connid = (unsigned char)cbuf[ 0 ];
366                     sock = (unsigned char)cbuf[ 4 ];
367                     quantum = (unsigned char)cbuf[ 5 ];
368                     rbuf[ 0 ] = cbuf[ 0 ];
369                     rbuf[ 1 ] = PAP_OPENREPLY;
370                     rbuf[ 2 ] = rbuf[ 3 ] = 0;
371
372                     if (( pr->p_flags & P_SPOOLED ) && rprintcap( pr ) != 0 ) {
373                         syslog( LOG_ERR, "printcap problem: %s",
374                                 pr->p_printer );
375                         rbuf[ 2 ] = rbuf[ 3 ] = 0xff;
376                         err = 1;
377                     }
378
379                     /*
380                      * If this fails, we've run out of sockets. Rather than
381                      * just die(), let's try to continue. Maybe some sockets
382                      * will close, and we can continue;
383                      */
384                     if (( atp = atp_open( ATADDR_ANYPORT, 
385                                           &pr->p_addr)) == NULL ) {
386                         syslog( LOG_ERR, "atp_open: %m" );
387                         rbuf[ 2 ] = rbuf[ 3 ] = 0xff;
388                         err = 1;
389                     }
390                     rbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
391                     rbuf[ 5 ] = oquantum;
392                     rbuf[ 6 ] = rbuf[ 7 ] = 0;
393
394                     iov.iov_base = rbuf;
395                     iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
396                     atpb.atp_sresiov = &iov;
397                     atpb.atp_sresiovcnt = 1;
398                     /*
399                      * This may error out if we lose a route, so we won't die().
400                      */
401                     if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
402                         syslog( LOG_ERR, "atp_sresp: %m" );
403                         continue;
404                     }
405
406                     if ( err ) {
407                         continue;
408                     }
409
410                     switch ( c = fork()) {
411                     case -1 :
412                         syslog( LOG_ERR, "fork: %m" );
413                         continue;
414
415                     case 0 : /* child */
416                         printer = pr;
417
418                         if (( printer->p_flags & P_SPOOLED ) &&
419                                 chdir( printer->p_spool ) < 0 ) {
420                             syslog( LOG_ERR, "chdir %s: %m", printer->p_spool );
421                             exit( 1 );
422                         }
423
424                         sv.sa_handler = SIG_DFL;
425                         sigemptyset( &sv.sa_mask );
426                         sv.sa_flags = SA_RESTART;
427                         if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
428                             syslog( LOG_ERR, "sigaction: %m" );
429                             exit( 1 );
430                         }
431
432                         for ( pr = printers; pr; pr = pr->p_next ) {
433                             atp_close( pr->p_atp );
434                         }
435                         sat.sat_port = sock;
436                         if ( session( atp, &sat ) < 0 ) {
437                             syslog( LOG_ERR, "bad session" );
438                             exit( 1 );
439                         }
440                         exit( 0 );
441                         break;
442
443                     default : /* parent */
444                         syslog( LOG_INFO, "child %d for \"%s\" from %u.%u",
445                                 c, pr->p_name, ntohs( sat.sat_addr.s_net ),
446                                 sat.sat_addr.s_node);
447                         atp_close( atp );
448                     }
449                     break;
450
451                 case PAP_SENDSTATUS :
452                     rbuf[ 0 ] = 0;
453                     rbuf[ 1 ] = PAP_STATUS;
454                     rbuf[ 2 ] = rbuf[ 3 ] = 0;
455                     rbuf[ 4 ] = rbuf[ 5 ] = 0;
456                     rbuf[ 6 ] = rbuf[ 7 ] = 0;
457
458                     iov.iov_base = rbuf;
459                     iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
460                     atpb.atp_sresiov = &iov;
461                     atpb.atp_sresiovcnt = 1;
462                     /*
463                      * This may error out if we lose a route, so we won't die().
464                      */
465                     if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
466                         syslog( LOG_ERR, "atp_sresp: %m" );
467                     }
468                     break;
469
470                 default :
471                     syslog( LOG_ERR, "Bad request from %u.%u!",
472                             ntohs( sat.sat_addr.s_net ), sat.sat_addr.s_node );
473                     continue;
474                     break;
475                 }
476
477 #ifdef notdef
478                 /*
479                  * Sometimes the child process will send its first READ
480                  * before the parent has sent the OPEN REPLY.  Moving this
481                  * code into the OPEN/STATUS switch fixes this problem.
482                  */
483                 iov.iov_base = rbuf;
484                 iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
485                 atpb.atp_sresiov = &iov;
486                 atpb.atp_sresiovcnt = 1;
487                 /*
488                  * This may error out if we lose a route, so we won't die().
489                  */
490                 if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
491                     syslog( LOG_ERR, "atp_sresp: %m" );
492                 }
493 #endif /* notdef */
494             }
495         }
496     }
497     return 0;
498 }
499
500 /*
501  * We assume buf is big enough for 255 bytes of data and a length byte.
502  */
503
504 int getstatus( pr, buf )
505     struct printer      *pr;
506     char                *buf;
507 {
508     char                path[ MAXPATHLEN ];
509     int                 fd = -1, rc;
510
511     if ( pr->p_flags & P_SPOOLED && ( pr->p_spool != NULL )) {
512         strcpy( path, pr->p_spool );
513         strcat( path, "/status" );
514         fd = open( path, O_RDONLY);
515     }
516
517     if (( pr->p_flags & P_PIPED ) || ( fd < 0 )) {
518         *buf = strlen( cannedstatus );
519         strncpy( &buf[ 1 ], cannedstatus, *buf );
520         return( *buf + 1 );
521     } else {
522         if (( rc = read( fd, &buf[ 1 ], 255 )) < 0 ) {
523             rc = 0;
524         }
525         close( fd );
526         if ( rc && buf[ rc ] == '\n' ) {        /* remove trailing newline */
527             rc--;
528         }
529         *buf = rc;
530         return( rc + 1 );
531     }
532 }
533
534 char    *pgetstr();
535 char    *getpname();
536
537 void getprinters( cf )
538     char        *cf;
539 {
540     char                buf[ 1024 ], area[ 1024 ], *a, *p, *name, *type, *zone;
541     struct printer      *pr;
542     int                 c;
543
544     while (( c = getprent( cf, buf )) > 0 ) {
545         a = area;
546         /*
547          * Get the printer's nbp name.
548          */
549         if (( p = getpname( &a )) == NULL ) {
550             fprintf( stderr, "No printer name\n" );
551             exit( 1 );
552         }
553
554         if (( pr = (struct printer *)malloc( sizeof( struct printer )))
555                 == NULL ) {
556             perror( "malloc" );
557             exit( 1 );
558         }
559         memset( pr, 0, sizeof( struct printer ));
560
561         name = defprinter.p_name;
562         type = defprinter.p_type;
563         zone = defprinter.p_zone;
564         if ( nbp_name( p, &name, &type, &zone )) {
565             fprintf( stderr, "Can't parse \"%s\"\n", name );
566             exit( 1 );
567         }
568         if ( name != defprinter.p_name ) {
569             if (( pr->p_name = (char *)malloc( strlen( name ) + 1 )) == NULL ) {
570                 perror( "malloc" );
571                 exit( 1 );
572             }
573             strcpy( pr->p_name, name );
574         } else {
575             pr->p_name = name;
576         }
577         if ( type != defprinter.p_type ) {
578             if (( pr->p_type = (char *)malloc( strlen( type ) + 1 )) == NULL ) {
579                 perror( "malloc" );
580                 exit( 1 );
581             }
582             strcpy( pr->p_type, type );
583         } else {
584             pr->p_type = type;
585         }
586         if ( zone != defprinter.p_zone ) {
587             if (( pr->p_zone = (char *)malloc( strlen( zone ) + 1 )) == NULL ) {
588                 perror( "malloc" );
589                 exit( 1 );
590             }
591             strcpy( pr->p_zone, zone );
592         } else {
593             pr->p_zone = zone;
594         }
595
596         if ( pnchktc( cf ) != 1 ) {
597             fprintf( stderr, "Bad papcap entry\n" );
598             exit( 1 );
599         }
600
601         /*
602          * Get PPD file.
603          */
604         if (( p = pgetstr( "pd", &a )) == NULL ) {
605             pr->p_ppdfile = defprinter.p_ppdfile;
606         } else {
607             if (( pr->p_ppdfile = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
608                 perror( "malloc" );
609                 exit( 1 );
610             }
611             strcpy( pr->p_ppdfile, p );
612         }
613
614         /*
615          * Get lpd printer name.
616          */
617         if (( p = pgetstr( "pr", &a )) == NULL ) {
618             pr->p_printer = defprinter.p_printer;
619             pr->p_flags = defprinter.p_flags;
620         } else {
621             if ( *p == '|' ) {
622                 p++;
623                 pr->p_flags = P_PIPED;
624             } else {
625                 pr->p_flags = P_SPOOLED;
626             }
627             if (( pr->p_printer = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
628                 perror( "malloc" );
629                 exit( 1 );
630             }
631             strcpy( pr->p_printer, p );
632         }
633
634         /*
635          * Do we want authenticated printing?
636          */
637         if ((p = pgetstr( "ca", &a )) != NULL ) {
638             if ((pr->p_authprintdir = (char *)malloc(strlen(p)+1)) == NULL) {
639                 perror( "malloc" );
640                 exit(1);
641             }
642             strcpy( pr->p_authprintdir, p );
643             pr->p_flags |= P_AUTH;
644             pr->p_flags |= P_AUTH_CAP;
645         } else { pr->p_authprintdir = NULL; }
646
647         if ( pgetflag( "sp" ) == 1 ) {
648             pr->p_flags |= P_AUTH;
649             pr->p_flags |= P_AUTH_PSSP;
650         }
651
652         if ((p = pgetstr("am", &a)) != NULL ) {
653                 if ((uamlist = (char *)malloc(strlen(p)+1)) == NULL ) {
654                         perror("malloc");
655                         exit(1);
656                 }
657                 strcpy(uamlist, p);
658         }
659
660         if ( pr->p_flags & P_SPOOLED ) {
661             /*
662              * Get operator name.
663              */
664             if (( p = pgetstr( "op", &a )) == NULL ) {
665                 pr->p_operator = defprinter.p_operator;
666             } else {
667                 if (( pr->p_operator = (char *)malloc( strlen( p ) + 1 ))
668                         == NULL ) {
669                     perror( "malloc" );
670                     exit( 1 );
671                 }
672                 strcpy( pr->p_operator, p );
673             }
674         }
675
676         /* get printer's appletalk address. */
677         if (( p = pgetstr( "pa", &a )) == NULL ) 
678             memcpy(&pr->p_addr, &defprinter.p_addr, sizeof(pr->p_addr));
679         else 
680             atalk_aton(p, &pr->p_addr);
681
682         pr->p_next = printers;
683         printers = pr;
684     }
685     if ( c == 0 ) {
686         endprent();
687     } else {                    /* No capability file, do default */
688         printers = &defprinter;
689     }
690 }
691
692 int rprintcap( pr )
693     struct printer      *pr;
694 {
695     char                buf[ 1024 ], area[ 1024 ], *a, *p;
696     int                 c;
697
698     /*
699      * Spool directory from printcap file.
700      */
701     if ( pr->p_flags & P_SPOOLED ) {
702         if ( pgetent( printcap, buf, pr->p_printer ) != 1 ) {
703             syslog( LOG_ERR, "No such printer: %s", pr->p_printer );
704             return( -1 );
705         }
706
707         /*
708          * Spool directory.
709          */
710         if ( pr->p_spool != NULL && pr->p_spool != defprinter.p_spool ) {
711             free( pr->p_spool );
712         }
713         a = area;
714         if (( p = pgetstr( "sd", &a )) == NULL ) {
715             pr->p_spool = defprinter.p_spool;
716         } else {
717             if (( pr->p_spool = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
718                 syslog( LOG_ERR, "malloc: %m" );
719                 exit( 1 );
720             }
721             strcpy( pr->p_spool, p );
722         }
723
724         /*
725          * Is accounting on?
726          */
727         a = area;
728         if ( pgetstr( "af", &a ) == NULL ) {
729             pr->p_flags &= ~P_ACCOUNT;
730         } else {
731             pr->p_flags |= P_ACCOUNT;
732 #ifdef ABS_PRINT
733             if ( pr->p_role != NULL && pr->p_role != defprinter.p_role ) {
734                 free( pr->p_role );
735             }
736             a = area;
737             if (( p = pgetstr( "ro", &a )) == NULL ) {
738                 pr->p_role = defprinter.p_role;
739             } else {
740                 if (( pr->p_role =
741                         (char *)malloc( strlen( p ) + 1 )) == NULL ) {
742                     syslog( LOG_ERR, "malloc: %m" );
743                     exit( 1 );
744                 }
745                 strcpy( pr->p_role, p );
746             }
747
748             if (( c = pgetnum( "si" )) < 0 ) {
749                 pr->p_srvid = defprinter.p_srvid;
750             } else {
751                 pr->p_srvid = c;
752             }
753 #endif /* ABS_PRINT */
754         }
755
756
757         /*
758          * Cost of printer.
759          */
760         if ( pr->p_pagecost_msg != NULL &&
761                 pr->p_pagecost_msg != defprinter.p_pagecost_msg ) {
762             free( pr->p_pagecost_msg );
763         }
764         a = area;
765         if (( p = pgetstr( "pc", &a )) != NULL ) {
766             if (( pr->p_pagecost_msg =
767                     (char *)malloc( strlen( p ) + 1 )) == NULL ) {
768                 syslog( LOG_ERR, "malloc: %m" );
769                 exit( 1 );
770             }
771             strcpy( pr->p_pagecost_msg, p );
772             pr->p_pagecost = 0;
773         } else if ( pr->p_flags & P_ACCOUNT ) {
774             if (( c = pgetnum( "pc" )) < 0 ) {
775                 pr->p_pagecost = defprinter.p_pagecost;
776             } else {
777                 pr->p_pagecost = c;
778             }
779             pr->p_pagecost_msg = NULL;
780         }
781
782         /*
783          * Get lpd lock file.
784          */
785         if ( pr->p_lock != NULL && pr->p_lock != defprinter.p_lock ) {
786             free( pr->p_lock );
787         }
788         a = area;
789         if (( p = pgetstr( "lo", &a )) == NULL ) {
790             pr->p_lock = defprinter.p_lock;
791         } else {
792             if (( pr->p_lock = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
793                 syslog( LOG_ERR, "malloc: %m" );
794                 exit( 1 );
795             }
796             strcpy( pr->p_lock, p );
797         }
798
799 #ifdef KRB
800         /*
801          * Must Kerberos authenticate?
802          */
803         if ( pgetflag( "ka" ) == 1 ) {
804             pr->p_flags |= P_KRB;
805         } else {
806             pr->p_flags &= ~P_KRB;
807         }
808 #endif /* KRB */
809
810         endprent();
811     }
812
813     return( 0 );
814 }