From andre.albsmeier@mchp.siemens.de Wed Jun  2 12:09:53 1999
Return-Path: <andre.albsmeier@mchp.siemens.de>
Received: from david.siemens.de (david.siemens.de [192.35.17.14])
	by hub.freebsd.org (Postfix) with ESMTP id CD0FF154EC
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  2 Jun 1999 12:09:22 -0700 (PDT)
	(envelope-from andre.albsmeier@mchp.siemens.de)
Received: from mail2.siemens.de (mail2.siemens.de [139.25.208.14])
	by david.siemens.de (8.9.3/8.9.3) with ESMTP id VAA28877
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 2 Jun 1999 21:09:18 +0200 (MET DST)
Received: from curry.mchp.siemens.de (curry.mchp.siemens.de [139.25.42.7])
	by mail2.siemens.de (8.9.3/8.9.3) with ESMTP id VAA24055
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 2 Jun 1999 21:09:18 +0200 (MET DST)
Received: (from daemon@localhost)
	by curry.mchp.siemens.de (8.9.3/8.9.3) id VAA25308
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 2 Jun 1999 21:09:17 +0200 (CEST)
Message-Id: <199906021909.VAA06209@internal>
Date: Wed, 2 Jun 1999 21:09:14 +0200 (CEST)
From: Andre Albsmeier <andre.albsmeier@mchp.siemens.de>
To: FreeBSD-gnats-submit@freebsd.org
Subject: Enhancement for lpr
X-Send-Pr-Version: 3.2

>Number:         11997
>Category:       bin
>Synopsis:       Enhancement for lpr
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jun  2 12:10:01 PDT 1999
>Closed-Date:    Tue Jan 18 15:16:15 PST 2000
>Last-Modified:  Tue Jan 18 15:17:21 PST 2000
>Originator:     Andre Albsmeier
>Release:        FreeBSD 3.2-STABLE i386
>Organization:
>Environment:

any lpr

>Description:

I think I have a nice enhancement for lpr in case the file to be
printed resides on the same filesystem as the lpd spooling
directory and should be deleted after printing.

That really becomes interesting in combination with samba and
pcnfs (and maybe others) since they:

a) create temporay files which are removed after printing,
b) often have their spool area on the same fs as lpd.

Normally they invoke lpr with -r. So lpr copies the (temporary)
file and deletes it. After having been printed, lpd removes its
file also.

So why do we copy the file when it is removed afterwards? Since
especially our files here being printed via samba are rather big
(20MB and more) this consumes time for copying and temporary disk
space as well.

I have made a small patch for lpr.c which tries to move the file
if being called with -r. If that failes (maybe because the file
resides on a different fs), it continues normally.

I would like if someone could look into my patch if it contains
security holes or other flaws I haven't thought about. If this isn't
the case, maybe we could commit it to the main tree. (Yes, in that
case I will change the style :-))

This code is now running here for about a year and never produced
problems. The speed for printing large samba files significally
increased as well as the fs usage was reduced.

The patch is against a recent 3.1-STABLE.

>How-To-Repeat:


>Fix:

*** usr.sbin/lpr/lpr/lpr.c.ORI	Wed Feb  3 19:13:57 1999
--- usr.sbin/lpr/lpr/lpr.c	Wed Feb  3 19:15:23 1999
***************
*** 370,375 ****
--- 370,393 ----
  		}
  		if (sflag)
  			printf("%s: %s: not linked, copying instead\n", name, arg);
+ 		if( f ) {		/* means that the file should be deleted */
+ 			seteuid(euid);	/* needed for rename() to succeed */
+ 			if( ! rename( arg, dfname ) ) {
+ 				register int i;
+ 				chmod( dfname, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
+ 				chown( dfname, userid, getgrnam("daemon")->gr_gid );
+ 				seteuid(uid);
+ 				if (format == 'p')
+ 					card('T', title ? title : arg);
+ 				for (i = 0; i < ncopies; i++)
+ 					card(format, &dfname[inchar-2]);
+ 				card('U', &dfname[inchar-2]);
+ 				card('N', arg);
+ 				nact++;
+ 				continue;
+ 			}
+ 			seteuid(uid);
+ 		}
  		if ((i = open(arg, O_RDONLY)) < 0) {
  			printf("%s: cannot open %s\n", name, arg);
  		} else {


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->suspended 
State-Changed-By: alfred 
State-Changed-When: Tue Dec 7 14:45:07 PST 1999 
State-Changed-Why:  
This introduces a race condition where a user can clobber files by using 
hardlinks, the patch needs to be re-thought. 
State-Changed-From-To: suspended->closed 
State-Changed-By: sheldonh 
State-Changed-When: Tue Jan 18 15:16:15 PST 2000 
State-Changed-Why:  
Superseded by PR 16124. 
>Unformatted:
