$OpenBSD: patch-etcpapd-lpc,v 1.1.1.1 2002/07/23 11:48:12 naddy Exp $
--- etc/papd/lp.c.orig	Sun Aug 17 09:20:25 1997
+++ etc/papd/lp.c	Thu Mar 29 16:31:06 2001
@@ -44,6 +44,8 @@
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h>
 #if defined( sun ) && defined( __svr4__ )
 #include </usr/ucbinclude/sys/file.h>
 #else sun __svr4__
@@ -60,11 +62,13 @@
 #include <math.h>
 #endif ABS_PRINT
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <strings.h>
+#include <string.h>
 #include <netdb.h>
 #include <fcntl.h>
+#include <unistd.h>
 
 #include "printer.h"
 #include "file.h"
@@ -100,20 +104,26 @@ struct lp {
 #define LP_CONNECT	(1<<3)
 #define LP_QUEUE	(1<<4)
 
+int
 lp_person( person )
     char	*person;
 {
+    size_t	len;
+
     if ( lp.lp_person != NULL ) {
 	free( lp.lp_person );
     }
-    if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
+	len = (strlen( person ) + 1);
+    if (( lp.lp_person = (char *)malloc( len )) == NULL ) {
 	syslog( LOG_ERR, "malloc: %m" );
 	exit( 1 );
     }
-    strcpy( lp.lp_person, person );
+    (void)strlcpy( lp.lp_person, person, len );
+    return(0);
 }
 
 #ifdef ABS_PRINT
+int
 lp_pagecost()
 {
     char	cost[ 22 ];
@@ -131,19 +141,25 @@ lp_pagecost()
 }
 #endif ABS_PRINT
 
+int
 lp_host( host )
     char	*host;
 {
+    size_t	len;
+
     if ( lp.lp_host != NULL ) {
 	free( lp.lp_host );
     }
-    if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
+	len = (strlen( host ) + 1);
+    if (( lp.lp_host = (char *)malloc( len )) == NULL ) {
 	syslog( LOG_ERR, "malloc: %m" );
 	exit( 1 );
     }
-    strcpy( lp.lp_host, host );
+    (void)strlcpy( lp.lp_host, host, len);
+	return(0);
 }
 
+int
 lp_job( job )
     char	*job;
 {
@@ -164,12 +180,14 @@ lp_job( job )
 	}
     }
     *q = '\0';
+	return(0);
 }
 
+int
 lp_init( out )
     struct papfile	*out;
 {
-    int		fd, n, len;
+    int		fd, n, len, lfd;
     char	*cp, buf[ BUFSIZ ];
     struct stat	st;
 #ifdef ABS_PRINT
@@ -209,12 +227,27 @@ lp_init( out )
     lp.lp_letter = 'A';
 
     if ( printer->p_flags & P_SPOOLED ) {
-	/* check if queuing is enabled: mode & 010 on lock file */
+	/* check if queuing is enabled: mode & 010 on lock file.
+	   if lock file doesn't exist create it and close it.
+	   So far I can't see papd actually using the lock file.
+	   it seems to pass this on to the default host print system.
+		ianm@cit.nepean.uws.edu.au */
+
 	if ( stat( printer->p_lock, &st ) < 0 ) {
-	    syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
-	    spoolerror( out, NULL );
-	    return( -1 );
+	    if ( lfd = open( printer->p_lock, O_CREAT, 0644) < 0 ) {
+	   	syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
+	    	spoolerror( out, NULL );
+	    	return( -1 );
+		}
+		close(lfd);
 	}
+	
+	if ( stat( printer->p_lock, &st ) < 0 ) {
+		syslog( LOG_ERR, "lp_init: %s: %m", printer->p_lock );
+		spoolerror ( out, NULL );
+		return ( -1 );
+	}
+
 	if ( st.st_mode & 010 ) {
 	    syslog( LOG_INFO, "lp_init: queuing is disabled" );
 	    spoolerror( out, "Queuing is disabled." );
@@ -250,7 +283,7 @@ lp_init( out )
 	lp.lp_seq = n;
 
 	n = ( n + 1 ) % 1000;
-	sprintf( buf, "%03d\n", n );
+	snprintf( buf, sizeof( buf ), "%03d\n", n );
 	lseek( fd, 0L, 0 );
 	write( fd, buf, strlen( buf ));
 	close( fd );
@@ -263,6 +296,7 @@ lp_init( out )
     return( 0 );
 }
 
+int
 lp_open( out )
     struct papfile	*out;
 {
@@ -285,7 +319,7 @@ lp_open( out )
 	    return( -1 );
 	}
     } else {
-	sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
+	snprintf( name, sizeof ( name ), "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
 
 	if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
 	    syslog( LOG_ERR, "lp_open %s: %m", name );
@@ -303,17 +337,19 @@ lp_open( out )
     return( 0 );
 }
 
+int
 lp_close()
 {
     if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
-	return;
+	return(0);
     }
     fclose( lp.lp_stream );
     lp.lp_stream = NULL;
     lp.lp_flags &= ~LP_OPEN;
-    return;
+    return(0);
 }
 
+int
 lp_write( buf, len )
     char	*buf;
     int		len;
@@ -329,13 +365,14 @@ lp_write( buf, len )
     return( 0 );
 }
 
+int
 lp_cancel()
 {
     char	name[ MAXPATHLEN ];
     char	letter;
 
     if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
-	return;
+	return(0);
     }
 
     if ( lp.lp_flags & LP_OPEN ) {
@@ -343,13 +380,13 @@ lp_cancel()
     }
 
     for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
-	sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
+	snprintf( name, sizeof ( name ), "df%c%03d%s", letter, lp.lp_seq, hostname );
 	if ( unlink( name ) < 0 ) {
 	    syslog( LOG_ERR, "lp_cancel unlink %s: %m", name );
 	}
     }
 
-    return;
+    return(0);
 }
 
 /*
@@ -358,6 +395,7 @@ lp_cancel()
  *
  * XXX piped?
  */
+int
 lp_print()
 {
     char		buf[ MAXPATHLEN ];
@@ -368,19 +406,19 @@ lp_print()
     FILE		*cfile;
 
     if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
-	return;
+	return (0);
     }
     lp_close();
 
     if ( printer->p_flags & P_SPOOLED ) {
-	sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
+	snprintf( tfname, sizeof ( tfname ), "tfA%03d%s", lp.lp_seq, hostname );
 	if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
 	    syslog( LOG_ERR, "lp_print %s: %m", tfname );
-	    return;
+	    return(0);
 	}
 	if (( cfile = fdopen( fd, "w" )) == NULL ) {
 	    syslog( LOG_ERR, "lp_print %s: %m", tfname );
-	    return;
+	    return(0);
 	}
 	fprintf( cfile, "H%s\n", hostname );	/* XXX lp_host? */
 
@@ -418,46 +456,48 @@ lp_print()
 	}
 	fclose( cfile );
 
-	sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
+	snprintf( cfname, sizeof ( cfname ), "cfA%03d%s", lp.lp_seq, hostname );
 	if ( link( tfname, cfname ) < 0 ) {
 	    syslog( LOG_ERR, "lp_print can't link %s to %s: %m", cfname,
 		    tfname );
-	    return;
+	    return(0);
 	}
 	unlink( tfname );
 
 	if (( s = lp_conn_unix()) < 0 ) {
 	    syslog( LOG_ERR, "lp_print: lp_conn_unix: %m" );
-	    return;
+	    return(0);
 	}
 
-	sprintf( buf, "\1%s\n", printer->p_printer );
+	snprintf( buf, sizeof ( buf ), "\1%s\n", printer->p_printer );
 	n = strlen( buf );
 	if ( write( s, buf, n ) != n ) {
 	    syslog( LOG_ERR, "lp_print write: %m" );
-	    return;
+	    return(0);
 	}
 	if ( read( s, buf, 1 ) != 1 ) {
 	    syslog( LOG_ERR, "lp_print read: %m" );
-	    return;
+	    return(0);
 	}
 
 	lp_disconn_unix( s );
 
 	if ( buf[ 0 ] != '\0' ) {
 	    syslog( LOG_ERR, "lp_print lpd said %c: %m", buf[ 0 ] );
-	    return;
+	    return(0);
 	}
     }
     syslog( LOG_INFO, "lp_print queued" );
-    return;
+    return(0);
 }
 
+int
 lp_disconn_unix( fd )
 {
     return( close( fd ));
 }
 
+int
 lp_conn_unix()
 {
     int			s;
@@ -467,11 +507,11 @@ lp_conn_unix()
 	syslog( LOG_ERR, "lp_conn_unix socket: %m" );
 	return( -1 );
     }
-    bzero( &saun, sizeof( struct sockaddr_un ));
+    memset( &saun, 0,sizeof( struct sockaddr_un ));
     saun.sun_family = AF_UNIX;
-    strcpy( saun.sun_path, _PATH_DEVPRINTER );
+    (void)strlcpy( saun.sun_path, _PATH_DEVPRINTER, sizeof(saun.sun_path) );
     if ( connect( s, (struct sockaddr *)&saun,
-	    strlen( saun.sun_path ) + 2 ) < 0 ) {
+	    (strlen( saun.sun_path ) + 2)  ) < 0 ) {
 	syslog( LOG_ERR, "lp_conn_unix connect %s: %m", saun.sun_path );
 	close( s );
 	return( -1 );
@@ -480,11 +520,13 @@ lp_conn_unix()
     return( s );
 }
 
+int
 lp_disconn_inet( fd )
 {
     return( close( fd ));
 }
 
+int
 lp_conn_inet()
 {
     int			privfd, port = IPPORT_RESERVED - 1;
@@ -513,10 +555,10 @@ lp_conn_inet()
 	return( -1 );
     }
 
-    bzero( &sin, sizeof( struct sockaddr_in ));
+    memset( &sin, 0, sizeof( struct sockaddr_in ));
     sin.sin_family = AF_INET;
 /*    sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
-    bcopy( hp->h_addr, &sin.sin_addr, hp->h_length );
+    memcpy( &sin.sin_addr, hp->h_addr, hp->h_length );
     sin.sin_port = sp->s_port;
 
     if ( connect( privfd, (struct sockaddr *)&sin,
@@ -529,6 +571,7 @@ lp_conn_inet()
     return( privfd );
 }
 
+int
 lp_rmjob( job )
     int		job;
 {
@@ -544,7 +587,7 @@ lp_rmjob( job )
 	return( -1 );
     }
 
-    sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
+    snprintf( buf, sizeof ( buf ), "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
     n = strlen( buf );
     if ( write( s, buf, n ) != n ) {
 	syslog( LOG_ERR, "lp_rmjob write: %m" );
@@ -569,6 +612,7 @@ char	*tag_files = "files: ";
 char	*tag_size = "size: ";
 char	*tag_status = "status: ";
 
+int
 lp_queue( out )
     struct papfile	*out;
 {
@@ -581,7 +625,7 @@ lp_queue( out )
 	return( -1 );
     }
 
-    sprintf( buf, "\3%s\n", printer->p_printer );
+    snprintf( buf, sizeof( buf ), "\3%s\n", printer->p_printer );
     n = strlen( buf );
     if ( write( s, buf, n ) != n ) {
 	syslog( LOG_ERR, "lp_queue write: %m" );
