From and@moon.cc.rsu.ru  Fri Nov 14 04:07:55 2003
Return-Path: <and@moon.cc.rsu.ru>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 4A33716A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 14 Nov 2003 04:07:55 -0800 (PST)
Received: from moon.cc.rsu.ru (moon.cc.rsu.ru [195.208.252.124])
	by mx1.FreeBSD.org (Postfix) with ESMTP id CF97243FA3
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 14 Nov 2003 04:07:53 -0800 (PST)
	(envelope-from and@moon.cc.rsu.ru)
Received: from moon.cc.rsu.ru (localhost [127.0.0.1])
	by moon.cc.rsu.ru (8.12.9/8.12.9) with ESMTP id hAEC7pFH001272;
	Fri, 14 Nov 2003 15:07:51 +0300 (MSK)
	(envelope-from and@moon.cc.rsu.ru)
Received: (from and@localhost)
	by moon.cc.rsu.ru (8.12.9/8.12.9/Submit) id hAEC7oeq001271;
	Fri, 14 Nov 2003 15:07:50 +0300 (MSK)
Message-Id: <200311141207.hAEC7oeq001271@moon.cc.rsu.ru>
Date: Fri, 14 Nov 2003 15:07:50 +0300 (MSK)
From: Andrey Beresovsky <and@rsu.ru>
Reply-To: Andrey Beresovsky <and@rsu.ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc: os@rsu.ru
Subject: [PATCH] /usr/libexec/ftpd has broken ASCII transfer type implementation
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         59285
>Category:       bin
>Synopsis:       [PATCH] /usr/libexec/ftpd has broken ASCII transfer type implementation
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    yar
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Nov 14 04:10:16 PST 2003
>Closed-Date:    Sat Dec 06 10:17:19 PST 2003
>Last-Modified:  Sat Dec 06 10:17:19 PST 2003
>Originator:     Andrey Beresovsky
>Release:        FreeBSD 5.1-CURRENT i386
>Organization:
Rostov State University
>Environment:
System: FreeBSD 5.1-CURRENT #2: Fri Nov 7 22:15:49 MSK 2003 i386

>Description:
According to RFC 959 (FILE TRANSFER PROTOCOL) for ASCII transfer type "the <CRLF> sequence
should be used where necessary to denote the end of a line of text" (Chapter 3.1.1.1.).

ftpd server implements ASCII transfer type by inserting <CR> before each <LF> without 
checking that may be <CR> allready exists. It leads to <CR><CR><LF> sequences in 
files which allready had <CR><LF> end of lines (ex.: text files from Windows systems).

Attached patch should fix this problem.

This problem also exists in lukemftp and lukemftpd. I sent PR with patches to NetBSD.
See http://www.NetBSD.org/cgi-bin/query-pr-single.pl?number=23435.

>How-To-Repeat:
1. Create a text file with <CR><LF> end of lines.
2. Receive (GET) this file from FreeBSD ftpd server.
3. You'll get a file with <CR><CR><LF> EOLs on windows and <CRLF> on unixes.

>Fix:
(ftpd.c rev. 1.146)

--- ftpd.c.orig	Thu Nov 13 23:29:54 2003
+++ ftpd.c	Thu Nov 13 23:33:14 2003
@@ -1971,6 +1971,7 @@
 send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg)
 {
 	int c, filefd, netfd;
+	int d = '\0';
 	char *buf;
 	off_t cnt;
 
@@ -1982,12 +1983,13 @@
 			if (recvurg)
 				goto got_oob;
 			byte_count++;
-			if (c == '\n') {
+			if (c == '\n' && d != '\r') {
 				if (ferror(outstr))
 					goto data_err;
 				(void) putc('\r', outstr);
 			}
 			(void) putc(c, outstr);
+			d = c;
 		}
 		if (recvurg)
 			goto got_oob;
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->yar 
Responsible-Changed-By: ceri 
Responsible-Changed-When: Fri Nov 14 04:12:21 PST 2003 
Responsible-Changed-Why:  
Assign to our defacto ftp maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=59285 
State-Changed-From-To: open->patched 
State-Changed-By: yar 
State-Changed-When: Sat Nov 15 03:09:17 PST 2003 
State-Changed-Why:  
Your fix has been committed to CURRENT with minor style changes. 
MFC is due in a week.  Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=59285 
State-Changed-From-To: patched->closed 
State-Changed-By: yar 
State-Changed-When: Sat Dec 6 10:13:39 PST 2003 
State-Changed-Why:  
The problem has been corrected in both active branches. 
Thank you! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=59285 
>Unformatted:
