]> arthur.barton.de Git - netatalk.git/blob - etc/papd/main.c
6602e8b722ec2082239816a9c44edf77fb041acb
[netatalk.git] / etc / papd / main.c
1 /*
2  * $Id: main.c,v 1.13 2001-09-12 19:13:17 uhees 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:v" )) != 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         case 'v' :              /* version */
236             printf( "papd (version %s)\n", VERSION );
237             exit ( 1 );
238             break;
239
240         default :
241             fprintf( stderr,
242                     "Usage:\t%s [ -d ] [ -f conffile ] [ -p printcap ]\n",
243                     *av );
244             exit( 1 );
245         }
246     }
247
248     getprinters( conffile );
249
250     switch (server_lock("papd", pidfile, debug)) {
251     case 0: /* open a couple things again in the child */
252       if ((c = open("/", O_RDONLY)) >= 0) {
253         dup2(c, 1);
254         dup2(c, 2);
255       }
256       break;
257     case -1:
258       exit(1);
259     default:
260       exit(0);
261     }      
262
263     /*
264      * Start logging.
265      */
266     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
267         p = av[ 0 ];
268     } else {
269         p++;
270     }
271 #ifdef ultrix
272     openlog( p, LOG_PID );
273 #else /* ultrix */
274     openlog( p, LOG_NDELAY|LOG_PID, LOG_LPR );
275 #endif /* ultrix */
276
277     syslog( LOG_INFO, "restart (%s)", version );
278
279     for ( pr = printers; pr; pr = pr->p_next ) {
280         if (( pr->p_flags & P_SPOOLED ) && rprintcap( pr ) < 0 ) {
281             syslog( LOG_ERR, "printcap problem: %s", pr->p_printer );
282         }
283         if (( pr->p_atp = atp_open( ATADDR_ANYPORT, &pr->p_addr )) == NULL ) {
284             syslog( LOG_ERR, "atp_open: %m" );
285             papd_exit( 1 );
286         }
287         if ( nbp_rgstr( atp_sockaddr( pr->p_atp ), pr->p_name, pr->p_type,
288                 pr->p_zone ) < 0 ) {
289             syslog( LOG_ERR, "can't register %s:%s@%s", pr->p_name, pr->p_type,
290                     pr->p_zone );
291             die( 1 );
292         }
293         if ( pr->p_flags & P_AUTH ) {
294                 syslog( LOG_INFO, "Authentication enabled: %s", pr->p_name );
295         }
296         else {
297                 syslog( LOG_INFO, "Authentication disabled: %s", pr->p_name );
298         }
299         syslog( LOG_INFO, "register %s:%s@%s", pr->p_name, pr->p_type,
300                 pr->p_zone );
301         pr->p_flags |= P_REGISTERED;
302     }
303
304     memset(&sv, 0, sizeof(sv));
305     sv.sa_handler = die;
306     sigemptyset( &sv.sa_mask );
307     sv.sa_flags = SA_RESTART;
308     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
309         syslog( LOG_ERR, "sigaction: %m" );
310         papd_exit( 1 );
311     }
312
313     sv.sa_handler = reap;
314     sigemptyset( &sv.sa_mask );
315     sv.sa_flags = SA_RESTART;
316     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
317         syslog( LOG_ERR, "sigaction: %m" );
318         papd_exit( 1 );
319     }
320
321     /*
322      * Load UAMS
323      */
324     auth_load(uampath, uamlist);
325
326     /*
327      * Begin accepting connections.
328      */
329     FD_ZERO( &fdset );
330     for (;;) {
331         for ( pr = printers; pr; pr = pr->p_next ) {
332             FD_SET( atp_fileno( pr->p_atp ), &fdset );
333         }
334         if (( c = select( FD_SETSIZE, &fdset, 0, 0, 0 )) < 0 ) {
335             if ( errno == EINTR ) {
336                 continue;
337             }
338             syslog( LOG_ERR, "select: %m" );
339             papd_exit( 1 );
340         }
341
342         for ( pr = printers; pr; pr = pr->p_next ) {
343             if ( FD_ISSET( atp_fileno( pr->p_atp ), &fdset )) {
344                 int             err = 0;
345
346                 memset( &sat, 0, sizeof( struct sockaddr_at ));
347 #ifdef BSD4_4
348                 sat.sat_len = sizeof( struct sockaddr_at );
349 #endif /* BSD4_4 */
350                 sat.sat_family = AF_APPLETALK;
351                 sat.sat_addr.s_net = ATADDR_ANYNET;
352                 sat.sat_addr.s_node = ATADDR_ANYNODE;
353                 sat.sat_port = ATADDR_ANYPORT;
354                 /* do an atp_rsel(), to prevent hangs */
355                 if (( c = atp_rsel( pr->p_atp, &sat, ATP_TREQ )) != ATP_TREQ ) {
356                     continue;
357                 }
358                 atpb.atp_saddr = &sat;
359                 atpb.atp_rreqdata = cbuf;
360                 atpb.atp_rreqdlen = sizeof( cbuf );
361                 if ( atp_rreq( pr->p_atp, &atpb ) < 0 ) {
362                     syslog( LOG_ERR, "atp_rreq: %m" );
363                     continue;
364                 }
365
366                 /* should check length of req buf */
367
368                 switch( cbuf[ 1 ] ) {
369                 case PAP_OPEN :
370                     connid = (unsigned char)cbuf[ 0 ];
371                     sock = (unsigned char)cbuf[ 4 ];
372                     quantum = (unsigned char)cbuf[ 5 ];
373                     rbuf[ 0 ] = cbuf[ 0 ];
374                     rbuf[ 1 ] = PAP_OPENREPLY;
375                     rbuf[ 2 ] = rbuf[ 3 ] = 0;
376
377                     if (( pr->p_flags & P_SPOOLED ) && rprintcap( pr ) != 0 ) {
378                         syslog( LOG_ERR, "printcap problem: %s",
379                                 pr->p_printer );
380                         rbuf[ 2 ] = rbuf[ 3 ] = 0xff;
381                         err = 1;
382                     }
383
384                     /*
385                      * If this fails, we've run out of sockets. Rather than
386                      * just die(), let's try to continue. Maybe some sockets
387                      * will close, and we can continue;
388                      */
389                     if (( atp = atp_open( ATADDR_ANYPORT, 
390                                           &pr->p_addr)) == NULL ) {
391                         syslog( LOG_ERR, "atp_open: %m" );
392                         rbuf[ 2 ] = rbuf[ 3 ] = 0xff;
393                         err = 1;
394                     }
395                     rbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
396                     rbuf[ 5 ] = oquantum;
397                     rbuf[ 6 ] = rbuf[ 7 ] = 0;
398
399                     iov.iov_base = rbuf;
400                     iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
401                     atpb.atp_sresiov = &iov;
402                     atpb.atp_sresiovcnt = 1;
403                     /*
404                      * This may error out if we lose a route, so we won't die().
405                      */
406                     if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
407                         syslog( LOG_ERR, "atp_sresp: %m" );
408                         continue;
409                     }
410
411                     if ( err ) {
412                         continue;
413                     }
414
415                     switch ( c = fork()) {
416                     case -1 :
417                         syslog( LOG_ERR, "fork: %m" );
418                         continue;
419
420                     case 0 : /* child */
421                         printer = pr;
422
423                         if (( printer->p_flags & P_SPOOLED ) &&
424                                 chdir( printer->p_spool ) < 0 ) {
425                             syslog( LOG_ERR, "chdir %s: %m", printer->p_spool );
426                             exit( 1 );
427                         }
428
429                         sv.sa_handler = SIG_DFL;
430                         sigemptyset( &sv.sa_mask );
431                         sv.sa_flags = SA_RESTART;
432                         if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
433                             syslog( LOG_ERR, "sigaction: %m" );
434                             exit( 1 );
435                         }
436
437                         for ( pr = printers; pr; pr = pr->p_next ) {
438                             atp_close( pr->p_atp );
439                         }
440                         sat.sat_port = sock;
441                         if ( session( atp, &sat ) < 0 ) {
442                             syslog( LOG_ERR, "bad session" );
443                             exit( 1 );
444                         }
445                         exit( 0 );
446                         break;
447
448                     default : /* parent */
449                         syslog( LOG_INFO, "child %d for \"%s\" from %u.%u",
450                                 c, pr->p_name, ntohs( sat.sat_addr.s_net ),
451                                 sat.sat_addr.s_node);
452                         atp_close( atp );
453                     }
454                     break;
455
456                 case PAP_SENDSTATUS :
457                     rbuf[ 0 ] = 0;
458                     rbuf[ 1 ] = PAP_STATUS;
459                     rbuf[ 2 ] = rbuf[ 3 ] = 0;
460                     rbuf[ 4 ] = rbuf[ 5 ] = 0;
461                     rbuf[ 6 ] = rbuf[ 7 ] = 0;
462
463                     iov.iov_base = rbuf;
464                     iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
465                     atpb.atp_sresiov = &iov;
466                     atpb.atp_sresiovcnt = 1;
467                     /*
468                      * This may error out if we lose a route, so we won't die().
469                      */
470                     if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
471                         syslog( LOG_ERR, "atp_sresp: %m" );
472                     }
473                     break;
474
475                 default :
476                     syslog( LOG_ERR, "Bad request from %u.%u!",
477                             ntohs( sat.sat_addr.s_net ), sat.sat_addr.s_node );
478                     continue;
479                     break;
480                 }
481
482 #ifdef notdef
483                 /*
484                  * Sometimes the child process will send its first READ
485                  * before the parent has sent the OPEN REPLY.  Moving this
486                  * code into the OPEN/STATUS switch fixes this problem.
487                  */
488                 iov.iov_base = rbuf;
489                 iov.iov_len = 8 + getstatus( pr, &rbuf[ 8 ] );
490                 atpb.atp_sresiov = &iov;
491                 atpb.atp_sresiovcnt = 1;
492                 /*
493                  * This may error out if we lose a route, so we won't die().
494                  */
495                 if ( atp_sresp( pr->p_atp, &atpb ) < 0 ) {
496                     syslog( LOG_ERR, "atp_sresp: %m" );
497                 }
498 #endif /* notdef */
499             }
500         }
501     }
502     return 0;
503 }
504
505 /*
506  * We assume buf is big enough for 255 bytes of data and a length byte.
507  */
508
509 int getstatus( pr, buf )
510     struct printer      *pr;
511     char                *buf;
512 {
513     char                path[ MAXPATHLEN ];
514     int                 fd = -1, rc;
515
516     if ( pr->p_flags & P_SPOOLED && ( pr->p_spool != NULL )) {
517         strcpy( path, pr->p_spool );
518         strcat( path, "/status" );
519         fd = open( path, O_RDONLY);
520     }
521
522     if (( pr->p_flags & P_PIPED ) || ( fd < 0 )) {
523         *buf = strlen( cannedstatus );
524         strncpy( &buf[ 1 ], cannedstatus, *buf );
525         return( *buf + 1 );
526     } else {
527         if (( rc = read( fd, &buf[ 1 ], 255 )) < 0 ) {
528             rc = 0;
529         }
530         close( fd );
531         if ( rc && buf[ rc ] == '\n' ) {        /* remove trailing newline */
532             rc--;
533         }
534         *buf = rc;
535         return( rc + 1 );
536     }
537 }
538
539 char    *pgetstr();
540 char    *getpname();
541
542 void getprinters( cf )
543     char        *cf;
544 {
545     char                buf[ 1024 ], area[ 1024 ], *a, *p, *name, *type, *zone;
546     struct printer      *pr;
547     int                 c;
548
549     while (( c = getprent( cf, buf )) > 0 ) {
550         a = area;
551         /*
552          * Get the printer's nbp name.
553          */
554         if (( p = getpname( &a )) == NULL ) {
555             fprintf( stderr, "No printer name\n" );
556             exit( 1 );
557         }
558
559         if (( pr = (struct printer *)malloc( sizeof( struct printer )))
560                 == NULL ) {
561             perror( "malloc" );
562             exit( 1 );
563         }
564         memset( pr, 0, sizeof( struct printer ));
565
566         name = defprinter.p_name;
567         type = defprinter.p_type;
568         zone = defprinter.p_zone;
569         if ( nbp_name( p, &name, &type, &zone )) {
570             fprintf( stderr, "Can't parse \"%s\"\n", name );
571             exit( 1 );
572         }
573         if ( name != defprinter.p_name ) {
574             if (( pr->p_name = (char *)malloc( strlen( name ) + 1 )) == NULL ) {
575                 perror( "malloc" );
576                 exit( 1 );
577             }
578             strcpy( pr->p_name, name );
579         } else {
580             pr->p_name = name;
581         }
582         if ( type != defprinter.p_type ) {
583             if (( pr->p_type = (char *)malloc( strlen( type ) + 1 )) == NULL ) {
584                 perror( "malloc" );
585                 exit( 1 );
586             }
587             strcpy( pr->p_type, type );
588         } else {
589             pr->p_type = type;
590         }
591         if ( zone != defprinter.p_zone ) {
592             if (( pr->p_zone = (char *)malloc( strlen( zone ) + 1 )) == NULL ) {
593                 perror( "malloc" );
594                 exit( 1 );
595             }
596             strcpy( pr->p_zone, zone );
597         } else {
598             pr->p_zone = zone;
599         }
600
601         if ( pnchktc( cf ) != 1 ) {
602             fprintf( stderr, "Bad papcap entry\n" );
603             exit( 1 );
604         }
605
606         /*
607          * Get PPD file.
608          */
609         if (( p = pgetstr( "pd", &a )) == NULL ) {
610             pr->p_ppdfile = defprinter.p_ppdfile;
611         } else {
612             if (( pr->p_ppdfile = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
613                 perror( "malloc" );
614                 exit( 1 );
615             }
616             strcpy( pr->p_ppdfile, p );
617         }
618
619         /*
620          * Get lpd printer name.
621          */
622         if (( p = pgetstr( "pr", &a )) == NULL ) {
623             pr->p_printer = defprinter.p_printer;
624             pr->p_flags = defprinter.p_flags;
625         } else {
626             if ( *p == '|' ) {
627                 p++;
628                 pr->p_flags = P_PIPED;
629             } else {
630                 pr->p_flags = P_SPOOLED;
631             }
632             if (( pr->p_printer = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
633                 perror( "malloc" );
634                 exit( 1 );
635             }
636             strcpy( pr->p_printer, p );
637         }
638
639         /*
640          * Do we want authenticated printing?
641          */
642         if ((p = pgetstr( "ca", &a )) != NULL ) {
643             if ((pr->p_authprintdir = (char *)malloc(strlen(p)+1)) == NULL) {
644                 perror( "malloc" );
645                 exit(1);
646             }
647             strcpy( pr->p_authprintdir, p );
648             pr->p_flags |= P_AUTH;
649             pr->p_flags |= P_AUTH_CAP;
650         } else { pr->p_authprintdir = NULL; }
651
652         if ( pgetflag( "sp" ) == 1 ) {
653             pr->p_flags |= P_AUTH;
654             pr->p_flags |= P_AUTH_PSSP;
655         }
656
657         if ((p = pgetstr("am", &a)) != NULL ) {
658                 if ((uamlist = (char *)malloc(strlen(p)+1)) == NULL ) {
659                         perror("malloc");
660                         exit(1);
661                 }
662                 strcpy(uamlist, p);
663         }
664
665         if ( pr->p_flags & P_SPOOLED ) {
666             /*
667              * Get operator name.
668              */
669             if (( p = pgetstr( "op", &a )) == NULL ) {
670                 pr->p_operator = defprinter.p_operator;
671             } else {
672                 if (( pr->p_operator = (char *)malloc( strlen( p ) + 1 ))
673                         == NULL ) {
674                     perror( "malloc" );
675                     exit( 1 );
676                 }
677                 strcpy( pr->p_operator, p );
678             }
679         }
680
681         /* get printer's appletalk address. */
682         if (( p = pgetstr( "pa", &a )) == NULL ) 
683             memcpy(&pr->p_addr, &defprinter.p_addr, sizeof(pr->p_addr));
684         else 
685             atalk_aton(p, &pr->p_addr);
686
687         pr->p_next = printers;
688         printers = pr;
689     }
690     if ( c == 0 ) {
691         endprent();
692     } else {                    /* No capability file, do default */
693         printers = &defprinter;
694     }
695 }
696
697 int rprintcap( pr )
698     struct printer      *pr;
699 {
700     char                buf[ 1024 ], area[ 1024 ], *a, *p;
701     int                 c;
702
703     /*
704      * Spool directory from printcap file.
705      */
706     if ( pr->p_flags & P_SPOOLED ) {
707         if ( pgetent( printcap, buf, pr->p_printer ) != 1 ) {
708             syslog( LOG_ERR, "No such printer: %s", pr->p_printer );
709             return( -1 );
710         }
711
712         /*
713          * Spool directory.
714          */
715         if ( pr->p_spool != NULL && pr->p_spool != defprinter.p_spool ) {
716             free( pr->p_spool );
717         }
718         a = area;
719         if (( p = pgetstr( "sd", &a )) == NULL ) {
720             pr->p_spool = defprinter.p_spool;
721         } else {
722             if (( pr->p_spool = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
723                 syslog( LOG_ERR, "malloc: %m" );
724                 exit( 1 );
725             }
726             strcpy( pr->p_spool, p );
727         }
728
729         /*
730          * Is accounting on?
731          */
732         a = area;
733         if ( pgetstr( "af", &a ) == NULL ) {
734             pr->p_flags &= ~P_ACCOUNT;
735         } else {
736             pr->p_flags |= P_ACCOUNT;
737 #ifdef ABS_PRINT
738             if ( pr->p_role != NULL && pr->p_role != defprinter.p_role ) {
739                 free( pr->p_role );
740             }
741             a = area;
742             if (( p = pgetstr( "ro", &a )) == NULL ) {
743                 pr->p_role = defprinter.p_role;
744             } else {
745                 if (( pr->p_role =
746                         (char *)malloc( strlen( p ) + 1 )) == NULL ) {
747                     syslog( LOG_ERR, "malloc: %m" );
748                     exit( 1 );
749                 }
750                 strcpy( pr->p_role, p );
751             }
752
753             if (( c = pgetnum( "si" )) < 0 ) {
754                 pr->p_srvid = defprinter.p_srvid;
755             } else {
756                 pr->p_srvid = c;
757             }
758 #endif /* ABS_PRINT */
759         }
760
761
762         /*
763          * Cost of printer.
764          */
765         if ( pr->p_pagecost_msg != NULL &&
766                 pr->p_pagecost_msg != defprinter.p_pagecost_msg ) {
767             free( pr->p_pagecost_msg );
768         }
769         a = area;
770         if (( p = pgetstr( "pc", &a )) != NULL ) {
771             if (( pr->p_pagecost_msg =
772                     (char *)malloc( strlen( p ) + 1 )) == NULL ) {
773                 syslog( LOG_ERR, "malloc: %m" );
774                 exit( 1 );
775             }
776             strcpy( pr->p_pagecost_msg, p );
777             pr->p_pagecost = 0;
778         } else if ( pr->p_flags & P_ACCOUNT ) {
779             if (( c = pgetnum( "pc" )) < 0 ) {
780                 pr->p_pagecost = defprinter.p_pagecost;
781             } else {
782                 pr->p_pagecost = c;
783             }
784             pr->p_pagecost_msg = NULL;
785         }
786
787         /*
788          * Get lpd lock file.
789          */
790         if ( pr->p_lock != NULL && pr->p_lock != defprinter.p_lock ) {
791             free( pr->p_lock );
792         }
793         a = area;
794         if (( p = pgetstr( "lo", &a )) == NULL ) {
795             pr->p_lock = defprinter.p_lock;
796         } else {
797             if (( pr->p_lock = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
798                 syslog( LOG_ERR, "malloc: %m" );
799                 exit( 1 );
800             }
801             strcpy( pr->p_lock, p );
802         }
803
804 #ifdef KRB
805         /*
806          * Must Kerberos authenticate?
807          */
808         if ( pgetflag( "ka" ) == 1 ) {
809             pr->p_flags |= P_KRB;
810         } else {
811             pr->p_flags &= ~P_KRB;
812         }
813 #endif /* KRB */
814
815         endprent();
816     }
817
818     return( 0 );
819 }