Subject: [netatalk-admins] fixing papd - pre-patch 1

From: Andras Kadinger <[email-masked]>
Date: Fri, 04 Sep 1998 01:30:51 +0200

So, I came up with this a couple of days ago, I just didn't want to send
it out for I hoped I can do overhauling of papd in a reasonable time.

This gross hack modifies the line marking routine in papd to handle both
the one and two character version line endings valid in PostScript, and
hopefully also removes 'canonization' of them so that incoming data goes
out unchanged.

This however doesn't fix the logic in papd, that forces it to read an
'entire line' (that is, up to a lineending) regardless of the length of
the line - so you should still be able to exhaust all your virtual
memory (and eat up enormous CPU time for that matter) by sending a large
binary job to papd. 

One possible workaround could be here to let markline return a long but
unfinished line as a complete one, preventing papd eating up memory.
This however has some chance to disturb comment handling; but since papd
currently handles only query comments necessary for spooling to work,
the best would be here I think to make up some heuristics to recognize
the beginning of the actual print job, and blindly copy data to the
spool from that point on. Does this sound plausible?

Andras

diff -udr netatalk-1.4b2+asun2.1.0-original/etc/papd/file.c netatalk-1.4b2+asun2.1.0/etc/papd/file.c
--- netatalk-1.4b2+asun2.1.0-original/etc/papd/file.c	Tue May 20 19:35:42 1997
+++ netatalk-1.4b2+asun2.1.0/etc/papd/file.c	Mon Aug 31 06:32:51 1998
@@ -11,8 +11,8 @@
 
 #include "file.h"
 
-markline( start, stop, pf )
-    char		**start, **stop;
+markline( start, stop, nextline, pf )
+    char		**start, **stop, **nextline;
     struct papfile	*pf;
 {
     char		*p;
@@ -27,9 +27,16 @@
 	    break;
 	}
     }
+
     if ( p >= pf->pf_end ) {
 	return( -1 );
     }
+
+    *nextline=p+1;
+
+    if ((*p=='\n' && *(p+1)=='\r') || (*p=='\r' && *(p+1)=='\n')) {
+	*nextline=p+2;
+    };
 
     *start = pf->pf_cur;
     *stop = p;
diff -udr netatalk-1.4b2+asun2.1.0-original/etc/papd/headers.c netatalk-1.4b2+asun2.1.0/etc/papd/headers.c
--- netatalk-1.4b2+asun2.1.0-original/etc/papd/headers.c	Fri May 30 02:29:48 1997
+++ netatalk-1.4b2+asun2.1.0/etc/papd/headers.c	Mon Aug 31 06:11:02 1998
@@ -13,10 +13,10 @@
 ch_title( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p, *q, c;
+    char		*start, *stop, *nextline, *p, *q, c;
     struct comment	*comment = compeek();
 
-    switch ( markline( &start, &stop, in )) {
+    switch ( markline( &start, &stop, &nextline, in )) {
     case 0 :
 	return( 0 );
 
@@ -50,10 +50,9 @@
 	*q = c;
     }
 
-    *stop = '\n';
-    lp_write( start, stop - start + 1 );
+    lp_write( start, nextline - start );
     compop();
-    consumetomark( start, stop, in );
+    consumetomark( start, nextline-1, in );
     return( CH_DONE );
 }
 
diff -udr netatalk-1.4b2+asun2.1.0-original/etc/papd/lp.c netatalk-1.4b2+asun2.1.0/etc/papd/lp.c
--- netatalk-1.4b2+asun2.1.0-original/etc/papd/lp.c	Thu Jun 26 22:48:43 1997
+++ netatalk-1.4b2+asun2.1.0/etc/papd/lp.c	Mon Aug 31 06:13:03 1998
@@ -572,7 +572,7 @@
 lp_queue( out )
     struct papfile	*out;
 {
-    char			buf[ 1024 ], *start, *stop, *p, *q;
+    char			buf[ 1024 ], *start, *stop, *nextline, *p, *q;
     static struct papfile	pf;
     int				n, len, s;
 	
@@ -595,7 +595,7 @@
     }
 
     for (;;) {
-	if ( markline( &start, &stop, &pf ) > 0 ) {
+	if ( markline( &start, &stop, &nextline, &pf ) > 0 ) {
 	    /* parse */
 	    for ( p = start; p < stop; p++ ) {
 		if ( *p == ' ' || *p == '\t' ) {
@@ -603,7 +603,7 @@
 		}
 	    }
 	    if ( p >= stop ) {
-		consumetomark( start, stop, &pf );
+		consumetomark( start, nextline-1, &pf );
 		continue;
 	    }
 
@@ -614,7 +614,7 @@
 	    len = p - start;
 	    if ( len == strlen( kw_rank ) &&
 		    strncmp( kw_rank, start, len ) == 0 ) {
-		consumetomark( start, stop, &pf );
+		consumetomark( start, nextline-1, &pf );
 		continue;
 	    }
 	    if (( len == strlen( kw_active ) &&
@@ -636,7 +636,7 @@
 		}
 		if ( p >= stop ) {
 		    APPEND( out, ".\n", 2 );
-		    consumetomark( start, stop, &pf );
+		    consumetomark( start, nextline-1, &pf );
 		    continue;
 		}
 		APPEND( out, tag_owner, strlen( tag_owner ));
@@ -655,7 +655,7 @@
 		}
 		if ( p >= stop ) {
 		    APPEND( out, ".\n", 2 );
-		    consumetomark( start, stop, &pf );
+		    consumetomark( start, nextline-1, &pf );
 		    continue;
 		}
 		APPEND( out, tag_job, strlen( tag_job ));
@@ -684,7 +684,7 @@
 		}
 		if ( p <= q ) {
 		    APPEND( out, ".\n", 2 );
-		    consumetomark( start, stop, &pf );
+		    consumetomark( start, nextline-1, &pf );
 		    continue;
 		}
 		APPEND( out, tag_files, strlen( tag_files ));
@@ -700,7 +700,7 @@
 		APPEND( out, p, stop - p );
 		APPEND( out, "\n.\n", 3 );
 
-		consumetomark( start, stop, &pf );
+		consumetomark( start, nextline-1, &pf );
 		continue;
 	    }
 
@@ -709,7 +709,7 @@
 	    APPEND( out, start, stop - start );
 	    APPEND( out, "\n.\n", 3 );
 
-	    consumetomark( start, stop, &pf );
+	    consumetomark( start, nextline-1, &pf );
 	} else {
 	    APPEND( out, "*\n", 2 );
 	    lp_disconn_unix( s );
diff -udr netatalk-1.4b2+asun2.1.0-original/etc/papd/magics.c netatalk-1.4b2+asun2.1.0/etc/papd/magics.c
--- netatalk-1.4b2+asun2.1.0-original/etc/papd/magics.c	Fri May 30 02:28:03 1997
+++ netatalk-1.4b2+asun2.1.0/etc/papd/magics.c	Mon Aug 31 06:16:26 1998
@@ -14,7 +14,7 @@
 ps( infile, outfile )
     struct papfile	*infile, *outfile;
 {
-    char			*start, *stop;
+    char			*start, *stop, *nextline;
     struct comment		*comment;
 
     for (;;) {
@@ -31,7 +31,7 @@
 	    }
 
 	} else {
-	    switch ( markline( &start, &stop, infile )) {
+	    switch ( markline( &start, &stop, &nextline, infile )) {
 	    case 0 :
 		/* eof on infile */
 		outfile->pf_state |= PF_EOF;
@@ -57,9 +57,8 @@
 	    }
 
 	    /* write to file */
-	    *stop = '\n';
-	    lp_write( start, stop - start + 1 );
-	    consumetomark( start, stop, infile );
+	    lp_write( start, nextline - start );
+	    consumetomark( start, nextline-1, infile );
 	}
     }
 }
@@ -68,10 +67,10 @@
     struct papfile	*in, *out;
 {
     struct comment	*comment;
-    char		*start, *stop;
+    char		*start, *stop,*nextline;
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    /* eof on infile */
 	    out->pf_state |= PF_EOF;
@@ -91,18 +90,18 @@
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 
 cm_psadobe( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop;
+    char		*start, *stop, *nextline;
     struct comment	*comment = compeek();
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    /* eof on infile */
 	    out->pf_state |= PF_EOF;
@@ -127,8 +126,8 @@
 	}
 
 	*stop = '\n';
-	lp_write( start, stop - start + 1 );
-	consumetomark( start, stop, in );
+	lp_write( start, nextline - start );
+	consumetomark( start, nextline-1, in );
     }
 }
 
@@ -137,10 +136,10 @@
 cm_psswitch( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     struct comment	*comment = compeek();
 
-    switch ( markline( &start, &stop, in )) {
+    switch ( markline( &start, &stop, &nextline, in )) {
     case 0 :
 	/* eof on infile */
 	out->pf_state |= PF_EOF;
diff -udr netatalk-1.4b2+asun2.1.0-original/etc/papd/queries.c netatalk-1.4b2+asun2.1.0/etc/papd/queries.c
--- netatalk-1.4b2+asun2.1.0-original/etc/papd/queries.c	Fri May 30 02:30:26 1997
+++ netatalk-1.4b2+asun2.1.0/etc/papd/queries.c	Mon Aug 31 06:26:59 1998
@@ -23,11 +23,11 @@
 cq_default( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     struct comment	*comment = compeek();
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    return( 0 );
 
@@ -40,7 +40,7 @@
 		comsetflags( 1 );
 	    } else {
 		compop();
-		consumetomark( start, stop, in );
+		consumetomark( start, nextline-1, in );
 		return( CH_DONE );
 	    }
 	} else {
@@ -59,12 +59,12 @@
 		/* *stop = '\r'; */
 		APPEND( out, p, stop - p + 1 );
 		compop();
-		consumetomark( start, stop, in );
+		consumetomark( start, nextline-1, in );
 		return( CH_DONE );
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 
@@ -77,14 +77,14 @@
 cq_k4login( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     unsigned char	*t;
     struct comment	*comment = compeek();
     KTEXT_ST		tkt;
     AUTH_DAT		ad;
     int			rc, i;
 
-    switch ( markline( &start, &stop, in )) {
+    switch ( markline( &start, &stop, &nextline, in )) {
     case 0 :
 	return( 0 );
 
@@ -109,7 +109,7 @@
 	syslog( LOG_ERR, "cq_k4login: %s", krb_err_txt[ rc ] );
 	APPEND( out, LoginFailed, strlen( LoginFailed ));
 	compop();
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
 	return( CH_DONE );
     }
     syslog( LOG_INFO, "cq_k4login: %s.%s@%s", ad.pname, ad.pinst,
@@ -119,7 +119,7 @@
 
     APPEND( out, LoginOK, strlen( LoginOK ));
     compop();
-    consumetomark( start, stop, in );
+    consumetomark( start, nextline-1, in );
     return( CH_DONE );
 }
 
@@ -132,7 +132,7 @@
     struct comment	*c, *comment = compeek();
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    return( 0 );
 
@@ -157,7 +157,7 @@
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 #endif KRB
@@ -226,13 +226,13 @@
 cq_query( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p, *q;
+    char		*start, *stop, *nextline, *p, *q;
     struct comment	*comment = compeek();
     struct genquery	*gq;

     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    return( 0 );
 
@@ -277,12 +277,12 @@
 	} else {
 	    if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
 		compop();
-		consumetomark( start, stop, in );
+		consumetomark( start, nextline-1, in );
 		return( CH_DONE );
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 
@@ -326,11 +326,11 @@
 cq_font( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     struct comment	*comment = compeek();
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    return( 0 );
 
@@ -367,25 +367,25 @@
 		if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
 		    APPEND( out, "*\n", 2 );
 		    compop();
-		    consumetomark( start, stop, in );
+		    consumetomark( start, nextline-1, in );
 		    return( CH_DONE );
 		}
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 
 cq_feature( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     struct comment	*comment = compeek();
     struct ppd_feature	*pfe;
 
     for (;;) {
-	switch ( markline( &start, &stop, in )) {
+	switch ( markline( &start, &stop, &nextline, in )) {
 	case 0 :
 	    return( 0 );
 
@@ -420,12 +420,12 @@
 	} else {
 	    if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
 		compop();
-		consumetomark( start, stop, in );
+		consumetomark( start, nextline-1, in );
 		return( CH_DONE );
 	    }
 	}
 
-	consumetomark( start, stop, in );
+	consumetomark( start, nextline-1, in );
     }
 }
 
@@ -435,10 +435,10 @@
 cq_rmjob( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop, *p;
+    char		*start, *stop, *nextline, *p;
     int			job;
 
-    switch ( markline( &start, &stop, in )) {
+    switch ( markline( &start, &stop, &nextline, in )) {
     case 0 :
 	return( 0 );
 
@@ -466,16 +466,16 @@
     }
 
     compop();
-    consumetomark( start, stop, in );
+    consumetomark( start, nextline-1, in );
     return( CH_DONE );
 }
 
 cq_listq( in, out )
     struct papfile	*in, *out;
 {
-    char		*start, *stop;
+    char		*start, *stop, *nextline;
 
-    switch ( markline( &start, &stop, in )) {
+    switch ( markline( &start, &stop, &nextline, in )) {
     case 0 :
 	return( 0 );
 
@@ -488,7 +488,7 @@
     }
 
     compop();
-    consumetomark( start, stop, in );
+    consumetomark( start, nextline-1, in );
     return( CH_DONE );
 }