summaryrefslogtreecommitdiff
path: root/misc/mc/files/patch-vfs-ftpfs.c
blob: bfd46fef34362077e60ba9412d46116abbbf740a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
--- vfs/ftpfs.c.orig	Thu Dec 26 08:21:43 2002
+++ vfs/ftpfs.c	Tue Jun 15 03:15:09 2004
@@ -266,8 +266,7 @@
 	switch (sscanf(answer, "%d", &code)){
 	    case 0:
 	        if (string_buf) {
-		    strncpy (string_buf, answer, string_len - 1);
-		    *(string_buf + string_len - 1) = 0;
+		    g_strlcpy (string_buf, answer, string_len);
 		}
 	        code = 500;
 	        return 5;
@@ -286,8 +285,7 @@
 		    }
 		}
 	        if (string_buf){
-		    strncpy (string_buf, answer, string_len - 1);
-		    *(string_buf + string_len - 1) = 0;
+		    g_strlcpy (string_buf, answer, string_len);
 		}
 		return code / 100;
 	}
@@ -321,28 +319,28 @@
     va_list ap;
     char *str, *fmt_str;
     int status;
-    int sock = SUP.sock;
+    int cmdlen;
     
     va_start (ap, fmt);
     fmt_str = g_strdup_vprintf (fmt, ap);
     va_end (ap);
 
-    status = strlen (fmt_str);
-    str = g_realloc (fmt_str, status + 3);
-    strcpy (str + status, "\r\n");
+    cmdlen = strlen (fmt_str);
+    str = g_realloc (fmt_str, cmdlen + 3);
+    strcpy (str + cmdlen, "\r\n");
 
     if (logfile){
         if (strncmp (str, "PASS ", 5) == 0){
             fputs ("PASS <Password not logged>\r\n", logfile);
         } else
-	    fwrite (str, status + 2, 1, logfile);
+	    fwrite (str, cmdlen + 2, 1, logfile);
 
 	fflush (logfile);
     }
 
     got_sigpipe = 0;
     enable_interrupt_key ();
-    status = write (SUP.sock, str, status + 2);
+    status = write (SUP.sock, str, cmdlen + 2);
     
     if (status < 0){
 	code = 421;
@@ -353,7 +351,7 @@
 		level = 1;
 		status = reconnect (me, super);
 		level = 0;
-		if (status && write (SUP.sock, str, status + 2) > 0)
+		if (status && write (SUP.sock, str, cmdlen + 2) > 0)
 		    goto ok;
 	    }
 	    got_sigpipe = 1;
@@ -367,7 +365,7 @@
     disable_interrupt_key ();
     
     if (wait_reply)
-	return get_reply (me, sock, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
+	return get_reply (me, SUP.sock, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
     return COMPLETE;
 }
 
@@ -903,23 +901,29 @@
     int data, len = sizeof(data_addr);
     struct protoent *pe;
 
-    getsockname(SUP.sock, (struct sockaddr *) &data_addr, &len);
-    data_addr.sin_port = 0;
-    
     pe = getprotobyname("tcp");
     if (pe == NULL)
 	    ERRNOR (EIO, -1);
+again:
+    if (getsockname(SUP.sock, (struct sockaddr *) &data_addr, &len) == -1)
+	ERRNOR (EIO, -1);
+    data_addr.sin_port = 0;
+    
     data = socket (AF_INET, SOCK_STREAM, pe->p_proto);
     if (data < 0)
 	    ERRNOR (EIO, -1);
 
     if (SUP.use_passive_connection){
-	if ((SUP.use_passive_connection = setup_passive (me, super, data, &data_addr)))
+	if (setup_passive (me, super, data, &data_addr))
 	    return data;
 
 	SUP.use_source_route = 0;
 	SUP.use_passive_connection = 0;
 	print_vfs_message (_("ftpfs: could not setup passive mode"));
+
+	/* data or data_addr may be damaged by setup_passive */
+	close (data);
+	goto again;
     }
 
     /* If passive setup fails, fallback to active connections */
@@ -971,11 +975,12 @@
 	data = s;
     else {
 	data = accept (s, (struct sockaddr *)&from, &fromlen);
-	close(s);
 	if (data < 0) {
 	    my_errno = errno;
+	    close(s);
 	    return -1;
 	}
+	close(s);
     } 
     disable_interrupt_key();
     return data;
@@ -1019,6 +1024,7 @@
 		gettimeofday (&tim, NULL);
 		if (tim.tv_sec > start_tim.tv_sec + ABORT_TIMEOUT) {
 		    /* server keeps sending, drop the connection and reconnect */
+		    close (dsock);
 		    reconnect (me, super);
 		    return;
 		}