• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 19508858781

19 Nov 2025 04:39PM UTC coverage: 92.995% (+0.01%) from 92.985%
19508858781

push

github

51413 of 55286 relevant lines covered (92.99%)

227.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.71
/src/data.c
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 1997, 1998 Public Flood Software
4
 * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5
 * Copyright (c) 2001-2025 The ProFTPD Project team
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20
 *
21
 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22
 * and other respective copyright holders give permission to link this program
23
 * with OpenSSL, and distribute the resulting executable, without including
24
 * the source code for OpenSSL in the source distribution.
25
 */
26

27
/* Data connection management functions */
28

29
#include "conf.h"
30

31
#ifdef HAVE_SYS_SENDFILE_H
32
#include <sys/sendfile.h>
33
#endif /* HAVE_SYS_SENDFILE_H */
34

35
#ifdef HAVE_SYS_UIO_H
36
#include <sys/uio.h>
37
#endif /* HAVE_SYS_UIO_H */
38

39
static const char *trace_channel = "data";
40
static const char *timing_channel = "timing";
41

42
#define PR_DATA_OPT_IGNORE_ASCII        0x0001
43
static unsigned long data_opts = 0UL;
44
static uint64_t data_start_ms = 0L;
45
static int data_first_byte_read = FALSE;
46
static int data_first_byte_written = FALSE;
47

48
/* local macro */
49

50
#define MODE_STRING        (session.sf_flags & (SF_ASCII|SF_ASCII_OVERRIDE) ? \
51
                         "ASCII" : "BINARY")
52

53
/* Internal usage: pointer to current data connection stream in use (may be
54
 * in either read or write mode)
55
 */
56
static pr_netio_stream_t *nstrm = NULL;
57

58
static long timeout_linger = PR_TUNABLE_TIMEOUTLINGER;
59

60
static int timeout_idle = PR_TUNABLE_TIMEOUTIDLE;
61
static int timeout_noxfer = PR_TUNABLE_TIMEOUTNOXFER;
62
static int timeout_stalled = PR_TUNABLE_TIMEOUTSTALLED;
63

64
/* Called if the "Stalled" timer goes off
65
 */
66
static int stalled_timeout_cb(CALLBACK_FRAME) {
×
67
  pr_event_generate("core.timeout-stalled", NULL);
×
68
  pr_log_pri(PR_LOG_NOTICE, "Data transfer stall timeout: %d %s",
×
69
    timeout_stalled, timeout_stalled != 1 ? "seconds" : "second");
×
70
  pr_session_disconnect(NULL, PR_SESS_DISCONNECT_TIMEOUT,
×
71
    "TimeoutStalled during data transfer");
72

73
  /* Prevent compiler warning. */
74
  return 0;
×
75
}
76

77
/* This signal is raised if we get OOB data on the control connection, and
78
 * a data transfer is in progress.
79
 */
80
static RETSIGTYPE data_urgent(int signo) {
×
81
  if (session.sf_flags & SF_XFER) {
×
82
    pr_trace_msg(trace_channel, 5, "received SIGURG signal (signal %d), "
×
83
      "setting 'aborted' session flag", signo);
84
    session.sf_flags |= SF_ABORT;
×
85

86
    if (nstrm) {
×
87
      pr_netio_abort(nstrm);
×
88
    }
89
  }
90

91
  signal(SIGURG, data_urgent);
×
92
}
×
93

94
static void data_new_xfer(char *filename, int direction) {
7✔
95
  pr_data_clear_xfer_pool();
7✔
96

97
  session.xfer.p = make_sub_pool(session.pool);
7✔
98
  pr_pool_tag(session.xfer.p, "Data Transfer pool");
7✔
99

100
  session.xfer.filename = pstrdup(session.xfer.p, filename);
7✔
101
  session.xfer.direction = direction;
7✔
102
  session.xfer.bufsize = pr_config_get_server_xfer_bufsz(direction);
7✔
103
  session.xfer.buf = pcalloc(session.xfer.p, session.xfer.bufsize + 1);
7✔
104
  pr_trace_msg(trace_channel, 8, "allocated data transfer buffer of %lu bytes",
7✔
105
    (unsigned long) session.xfer.bufsize);
7✔
106
  session.xfer.buf++;        /* leave room for ascii translation */
7✔
107
  session.xfer.buflen = 0;
7✔
108
}
7✔
109

110
static int data_passive_open(const char *reason, off_t size) {
3✔
111
  conn_t *c;
3✔
112
  int rev, xerrno = 0;
3✔
113

114
  if (reason == NULL &&
3✔
115
      session.xfer.filename != NULL) {
3✔
116
    reason = session.xfer.filename;
×
117
  }
118

119
  /* Set the "stalled" timer, if any, to prevent the connection
120
   * open from taking too long
121
   */
122
  if (timeout_stalled) {
3✔
123
    pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL, stalled_timeout_cb,
3✔
124
      "TimeoutStalled");
125
  }
126

127
  /* We save the state of our current disposition for doing reverse
128
   * lookups, and then set it to what the configuration wants it to
129
   * be.
130
   */
131
  rev = pr_netaddr_set_reverse_dns(ServerUseReverseDNS);
3✔
132

133
  /* Protocol and socket options should be set before handshaking. */
134

135
  if (session.xfer.direction == PR_NETIO_IO_RD) {
3✔
136
    pr_inet_set_socket_opts2(session.d->pool, session.d,
2✔
137
      (main_server->tcp_rcvbuf_override ? main_server->tcp_rcvbuf_len : 0), 0,
2✔
138
      main_server->tcp_keepalive, 0);
2✔
139

140
  } else {
141
    pr_inet_set_socket_opts2(session.d->pool, session.d,
1✔
142
      0, (main_server->tcp_sndbuf_override ? main_server->tcp_sndbuf_len : 0),
1✔
143
      main_server->tcp_keepalive, 0);
1✔
144
  }
145

146
  c = pr_inet_accept(session.pool, session.d, session.c, -1, -1, TRUE);
3✔
147
  pr_netaddr_set_reverse_dns(rev);
3✔
148

149
  if (c && c->mode != CM_ERROR) {
3✔
150
    pr_inet_close(session.pool, session.d);
×
151
    (void) pr_inet_set_nonblock(session.pool, c);
×
152
    session.d = c;
×
153

154
    pr_log_debug(DEBUG4, "passive data connection opened - local  : %s:%d",
×
155
      pr_netaddr_get_ipstr(session.d->local_addr), session.d->local_port);
156
    pr_log_debug(DEBUG4, "passive data connection opened - remote : %s:%d",
×
157
      pr_netaddr_get_ipstr(session.d->remote_addr), session.d->remote_port);
×
158

159
    if (session.xfer.xfer_type != STOR_UNIQUE) {
×
160
      if (size) {
×
161
        pr_response_send(R_150, _("Opening %s mode data connection for %s "
×
162
          "(%" PR_LU " %s)"), MODE_STRING, reason, (pr_off_t) size,
×
163
          size != 1 ? "bytes" : "byte");
164

165
      } else {
166
        pr_response_send(R_150, _("Opening %s mode data connection for %s"),
×
167
          MODE_STRING, reason);
×
168
      }
169

170
    } else {
171

172
      /* Format of 150 responses for STOU is explicitly dictated by
173
       * RFC 1123:
174
       *
175
       *  4.1.2.9  STOU Command: RFC-959 Section 4.1.3
176
       *
177
       *    The STOU command stores into a uniquely named file.  When it
178
       *    receives an STOU command, a Server-FTP MUST return the
179
       *    actual file name in the "125 Transfer Starting" or the "150
180
       *    Opening Data Connection" message that precedes the transfer
181
       *    (the 250 reply code mentioned in RFC-959 is incorrect).  The
182
       *    exact format of these messages is hereby defined to be as
183
       *    follows:
184
       *
185
       *        125 FILE: pppp
186
       *        150 FILE: pppp
187
       *
188
       *    where pppp represents the unique pathname of the file that
189
       *    will be written.
190
       */
191
      pr_response_send(R_150, "FILE: %s", reason);
×
192
    }
193

194
    return 0;
×
195
  }
196

197
  /* Check for error conditions. */
198
  if (c != NULL &&
3✔
199
      c->mode == CM_ERROR) {
×
200
    pr_log_pri(PR_LOG_ERR, "error: unable to accept an incoming data "
×
201
      "connection: %s", strerror(c->xerrno));
202
  }
203

204
  xerrno = session.d->xerrno;
3✔
205
  pr_response_add_err(R_425, _("Unable to build data connection: %s"),
3✔
206
    strerror(xerrno));
207
  pr_data_close2();
3✔
208

209
  errno = xerrno;
3✔
210
  return -1;
3✔
211
}
212

213
static int data_active_open(const char *reason, off_t size) {
8✔
214
  conn_t *conn;
8✔
215
  config_rec *c;
8✔
216
  int bind_port, rev, *root_revoke = NULL, tcp_nodelay = 1, xerrno;
8✔
217
  const pr_netaddr_t *bind_addr = NULL;
8✔
218

219
  if (session.c->remote_addr == NULL) {
8✔
220
    /* An opened but unconnected connection? */
221
    errno = EINVAL;
1✔
222
    return -1;
1✔
223
  }
224

225
  if (reason == NULL &&
7✔
226
      session.xfer.filename != NULL) {
7✔
227
    reason = session.xfer.filename;
4✔
228
  }
229

230
  if (pr_netaddr_get_family(session.c->local_addr) == pr_netaddr_get_family(session.c->remote_addr)) {
7✔
231
    bind_addr = session.c->local_addr;
7✔
232

233
  } else {
234
    /* In this scenario, the server has an IPv6 socket, but the remote client
235
     * is an IPv4 (or IPv4-mapped IPv6) peer.
236
     */
237
    bind_addr = pr_netaddr_v6tov4(session.xfer.p, session.c->local_addr);
×
238
  }
239

240
  /* Default source port to which to bind for the active transfer, as
241
   * per RFC959.
242
   */
243
  bind_port = session.c->local_port-1;
7✔
244

245
  root_revoke = get_param_ptr(TOPLEVEL_CONF, "RootRevoke", FALSE);
7✔
246
  if (root_revoke == NULL) {
7✔
247
    /* In the absence of any explicit RootRevoke, the default behavior is to
248
     * change the source port.  This means we are technically noncompliant,
249
     * but most clients do not enforce this behavior.
250
     */
251
    bind_port = INPORT_ANY;
252

253
  } else {
254
    /* A RootRevoke value of 0 indicates 'false', 1 indicates 'true', and
255
     * 2 indicates 'NonCompliantActiveTransfer'.  We change the source port for
256
     * a RootRevoke value of 2, and for a value of 1, we make sure that
257
     * that the port is not a privileged port.
258
     */
259
    switch (*root_revoke) {
4✔
260
      case 1:
2✔
261
        if (bind_port < 1024) {
2✔
262
          pr_log_debug(DEBUG0, "RootRevoke in effect, unable to bind to local "
1✔
263
            "port %d for active transfer", bind_port);
264
          errno = EPERM;
1✔
265
          return -1;
1✔
266
        }
267
        break;
268

269
      case 2:
1✔
270
        bind_port = INPORT_ANY;
1✔
271
        break;
1✔
272

273
      default:
274
        break;
275
    }
276
  }
277

278
  session.d = pr_inet_create_conn2(session.pool, -1, bind_addr, bind_port,
6✔
279
    PR_INET_CREATE_CONN_FL_RETRY_BIND);
280
  if (session.d == NULL) {
6✔
281
    xerrno = errno;
×
282

283
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
284
      strerror(xerrno));
285

286
    errno = xerrno;
×
287
    return -1;
×
288
  }
289

290
  /* Default remote address to which to connect for an active transfer,
291
   * if the client has not specified a different address via PORT/EPRT,
292
   * as per RFC 959.
293
   */
294
  if (pr_netaddr_get_family(&session.data_addr) == AF_UNSPEC) {
6✔
295
    pr_log_debug(DEBUG6, "Client has not sent previous PORT/EPRT command, "
2✔
296
      "defaulting to %s#%u for active transfer",
297
      pr_netaddr_get_ipstr(session.c->remote_addr), session.c->remote_port);
2✔
298

299
    pr_netaddr_set_family(&session.data_addr, pr_netaddr_get_family(session.c->remote_addr));
2✔
300
    pr_netaddr_set_sockaddr(&session.data_addr, pr_netaddr_get_sockaddr(session.c->remote_addr));
2✔
301
  }
302

303
  /* Set the "stalled" timer, if any, to prevent the connection
304
   * open from taking too long
305
   */
306
  if (timeout_stalled > 0) {
6✔
307
    pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL, stalled_timeout_cb,
6✔
308
      "TimeoutStalled");
309
  }
310

311
  rev = pr_netaddr_set_reverse_dns(ServerUseReverseDNS);
6✔
312

313
  /* Protocol and socket options should be set before handshaking. */
314

315
  if (session.xfer.direction == PR_NETIO_IO_RD) {
6✔
316
    pr_inet_set_socket_opts2(session.d->pool, session.d,
4✔
317
      (main_server->tcp_rcvbuf_override ? main_server->tcp_rcvbuf_len : 0), 0,
4✔
318
      main_server->tcp_keepalive, 1);
4✔
319

320
  } else {
321
    pr_inet_set_socket_opts2(session.d->pool, session.d,
2✔
322
      0, (main_server->tcp_sndbuf_override ? main_server->tcp_sndbuf_len : 0),
2✔
323
      main_server->tcp_keepalive, 1);
2✔
324
  }
325

326
  c = find_config(main_server->conf, CONF_PARAM, "TCPNoDelay", FALSE);
6✔
327
  if (c != NULL) {
6✔
328
    int data_use_nodelay;
×
329

330
    data_use_nodelay = *((int *) c->argv[1]);
×
331
    if (data_use_nodelay == FALSE) {
×
332
      tcp_nodelay = 0;
×
333
    }
334
  }
335

336
  session.d->use_nodelay = tcp_nodelay;
6✔
337
  pr_inet_set_proto_opts(session.pool, session.d, main_server->tcp_mss_len,
6✔
338
    tcp_nodelay, IPTOS_THROUGHPUT, 1);
339
  pr_inet_generate_socket_event("core.data-connect", main_server,
6✔
340
    session.d->local_addr, session.d->listen_fd);
6✔
341

342
  if (pr_inet_connect(session.d->pool, session.d, &session.data_addr,
6✔
343
      session.data_port) < 0) {
6✔
344
    xerrno = session.d->xerrno;
6✔
345

346
    pr_log_debug(DEBUG6,
12✔
347
      "Error connecting to %s#%u for active data transfer: %s",
348
      pr_netaddr_get_ipstr(&session.data_addr), session.data_port,
6✔
349
      strerror(xerrno));
350
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
6✔
351
      strerror(xerrno));
352
    pr_data_close2();
6✔
353

354
    errno = xerrno;
6✔
355
    return -1;
6✔
356
  }
357

358
  conn = pr_inet_openrw(session.pool, session.d, NULL, PR_NETIO_STRM_DATA,
×
359
    session.d->listen_fd, -1, -1, TRUE);
×
360

361
  pr_netaddr_set_reverse_dns(rev);
×
362

363
  if (conn != NULL) {
×
364
    pr_log_debug(DEBUG4, "active data connection opened - local  : %s:%d",
×
365
      pr_netaddr_get_ipstr(session.d->local_addr), session.d->local_port);
×
366
    pr_log_debug(DEBUG4, "active data connection opened - remote : %s:%d",
×
367
      pr_netaddr_get_ipstr(session.d->remote_addr), session.d->remote_port);
×
368

369
    if (session.xfer.xfer_type != STOR_UNIQUE) {
×
370
      if (size) {
×
371
        pr_response_send(R_150, _("Opening %s mode data connection for %s "
×
372
          "(%" PR_LU " %s)"), MODE_STRING, reason, (pr_off_t) size,
×
373
          size != 1 ? "bytes" : "byte");
374

375
      } else {
376
        pr_response_send(R_150, _("Opening %s mode data connection for %s"),
×
377
          MODE_STRING, reason);
×
378
      }
379

380
    } else {
381

382
      /* Format of 150 responses for STOU is explicitly dictated by
383
       * RFC 1123:
384
       *
385
       *  4.1.2.9  STOU Command: RFC-959 Section 4.1.3
386
       *
387
       *    The STOU command stores into a uniquely named file.  When it
388
       *    receives an STOU command, a Server-FTP MUST return the
389
       *    actual file name in the "125 Transfer Starting" or the "150
390
       *    Opening Data Connection" message that precedes the transfer
391
       *    (the 250 reply code mentioned in RFC-959 is incorrect).  The
392
       *    exact format of these messages is hereby defined to be as
393
       *    follows:
394
       *
395
       *        125 FILE: pppp
396
       *        150 FILE: pppp
397
       *
398
       *    where pppp represents the unique pathname of the file that
399
       *    will be written.
400
       */
401
      pr_response_send(R_150, "FILE: %s", reason);
×
402
    }
403

404
    pr_inet_close(session.pool, session.d);
×
405
    (void) pr_inet_set_nonblock(session.pool, session.d);
×
406
    session.d = conn;
×
407
    return 0;
×
408
  }
409

410
  pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
411
    strerror(session.d->xerrno));
×
412
  xerrno = session.d->xerrno;
×
413
  pr_data_close2();
×
414

415
  errno = xerrno;
×
416
  return -1;
×
417
}
418

419
void pr_data_set_linger(long linger) {
1✔
420
  timeout_linger = linger;
1✔
421
}
1✔
422

423
int pr_data_get_timeout(int id) {
7✔
424
  switch (id) {
7✔
425
    case PR_DATA_TIMEOUT_IDLE:
2✔
426
      return timeout_idle;
2✔
427

428
    case PR_DATA_TIMEOUT_NO_TRANSFER:
2✔
429
      return timeout_noxfer;
2✔
430

431
    case PR_DATA_TIMEOUT_STALLED:
2✔
432
      return timeout_stalled;
2✔
433

434
    default:
435
      break;
1✔
436
  }
437

438
  errno = EINVAL;
1✔
439
  return -1;
1✔
440
}
441

442
void pr_data_set_timeout(int id, int timeout) {
3✔
443
  switch (id) {
3✔
444
    case PR_DATA_TIMEOUT_IDLE:
1✔
445
      timeout_idle = timeout;
1✔
446
      break;
1✔
447

448
    case PR_DATA_TIMEOUT_NO_TRANSFER:
1✔
449
      timeout_noxfer = timeout;
1✔
450
      break;
1✔
451

452
    case PR_DATA_TIMEOUT_STALLED:
1✔
453
      timeout_stalled = timeout;
1✔
454
      break;
1✔
455

456
    default:
457
      break;
458
  }
459
}
3✔
460

461
void pr_data_clear_xfer_pool(void) {
19✔
462
  int xfer_type;
19✔
463

464
  if (session.xfer.p != NULL) {
19✔
465
    destroy_pool(session.xfer.p);
2✔
466
  }
467

468
  /* Note that session.xfer.xfer_type may have been set already, e.g.
469
   * for STOR_UNIQUE uploads.  To support this, we need to preserve that
470
   * value.
471
   */
472
  xfer_type = session.xfer.xfer_type;
19✔
473

474
  memset(&session.xfer, 0, sizeof(session.xfer));
19✔
475
  session.xfer.xfer_type = xfer_type;
19✔
476
}
19✔
477

478
void pr_data_reset(void) {
9✔
479
  /* Clear any leftover state from previous transfers. */
480
  pr_ascii_ftp_reset();
9✔
481

482
  if (session.d != NULL &&
9✔
483
      session.d->pool != NULL) {
1✔
484
    destroy_pool(session.d->pool);
1✔
485
  }
486

487
  session.d = NULL;
9✔
488
  session.sf_flags &= (SF_ALL^(SF_ABORT|SF_POST_ABORT|SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE|SF_EPSV_ALL));
9✔
489
}
9✔
490

491
int pr_data_ignore_ascii(int ignore_ascii) {
5✔
492
  int res;
5✔
493

494
  if (ignore_ascii != TRUE &&
5✔
495
      ignore_ascii != FALSE) {
496
    errno = EINVAL;
1✔
497
    return -1;
1✔
498
  }
499

500
  if (data_opts & PR_DATA_OPT_IGNORE_ASCII) {
4✔
501
    if (!ignore_ascii) {
2✔
502
      data_opts &= ~PR_DATA_OPT_IGNORE_ASCII;
1✔
503
    }
504

505
    res = TRUE;
506

507
  } else {
508
    if (ignore_ascii) {
2✔
509
      data_opts |= PR_DATA_OPT_IGNORE_ASCII;
1✔
510
    }
511

512
    res = FALSE;
513
  }
514

515
  return res;
516
}
517

518
void pr_data_init(char *filename, int direction) {
3✔
519
  if (session.xfer.p == NULL) {
3✔
520
    data_new_xfer(filename, direction);
2✔
521

522
  } else {
523
    if (!(session.sf_flags & SF_PASSIVE)) {
1✔
524
      pr_log_debug(DEBUG5,
1✔
525
        "data_init oddity: session.xfer exists in non-PASV mode");
526
    }
527

528
    session.xfer.direction = direction;
1✔
529
  }
530

531
  /* Clear any leftover state from previous transfers. */
532
  pr_ascii_ftp_reset();
3✔
533
}
3✔
534

535
int pr_data_open(char *filename, char *reason, int direction, off_t size) {
15✔
536
  int res = 0;
15✔
537
#if defined(HAVE_SIGACTION)
538
  struct sigaction act;
15✔
539
#endif /* HAVE_SIGACTION */
540

541
  if (session.c == NULL) {
15✔
542
    errno = EINVAL;
2✔
543
    return -1;
2✔
544
  }
545

546
  if ((session.sf_flags & SF_PASSIVE) ||
13✔
547
      (session.sf_flags & SF_EPSV_ALL)) {
9✔
548
    /* For passive transfers, we expect there to already be an existing
549
     * data connection...
550
     */
551
    if (session.d == NULL) {
4✔
552
      errno = EINVAL;
1✔
553
      return -1;
1✔
554
    }
555

556
  } else {
557
    /* ...but for active transfers, we expect there to NOT be an existing
558
     * data connection.
559
     */
560
    if (session.d != NULL) {
9✔
561
      errno = session.d->xerrno = EINVAL;
1✔
562
      return -1;
1✔
563
    }
564
  }
565

566
  /* Make sure that any abort flags have been cleared. */
567
  session.sf_flags &= ~(SF_ABORT|SF_POST_ABORT);
11✔
568

569
  if (session.xfer.p == NULL) {
11✔
570
    data_new_xfer(filename, direction);
5✔
571

572
  } else {
573
    session.xfer.direction = direction;
6✔
574
  }
575

576
  if (reason == NULL) {
11✔
577
    reason = filename;
11✔
578
  }
579

580
  /* Passive data transfers... */
581
  if ((session.sf_flags & SF_PASSIVE) ||
11✔
582
      (session.sf_flags & SF_EPSV_ALL)) {
8✔
583
    res = data_passive_open(reason, size);
3✔
584

585
  /* Active data transfers... */
586
  } else {
587
    res = data_active_open(reason, size);
8✔
588
  }
589

590
  if (res < 0) {
11✔
591
    return res;
592
  }
593

594
  if (pr_netio_postopen(session.d->instrm) < 0) {
×
595
    int xerrno;
×
596

597
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
598
      strerror(session.d->xerrno));
×
599
    xerrno = session.d->xerrno;
×
600
    pr_data_close2();
×
601

602
    errno = xerrno;
×
603
    return -1;
×
604
  }
605

606
  if (pr_netio_postopen(session.d->outstrm) < 0) {
×
607
    int xerrno;
×
608

609
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
610
      strerror(session.d->xerrno));
×
611
    xerrno = session.d->xerrno;
×
612
    pr_data_close2();
×
613

614
    errno = xerrno;
×
615
    return -1;
×
616
  }
617

618
  memset(&session.xfer.start_time, '\0', sizeof(session.xfer.start_time));
×
619
  gettimeofday(&session.xfer.start_time, NULL);
×
620

621
  if (session.xfer.direction == PR_NETIO_IO_RD) {
×
622
    nstrm = session.d->instrm;
×
623

624
  } else {
625
    nstrm = session.d->outstrm;
×
626
  }
627

628
  session.sf_flags |= SF_XFER;
×
629

630
  if (timeout_noxfer) {
×
631
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
×
632
  }
633

634
  /* Allow aborts -- set the current NetIO stream to allow interrupted
635
   * syscalls, so our SIGURG handler can interrupt it
636
   */
637
  pr_netio_set_poll_interval(nstrm, 1);
×
638

639
  /* PORTABILITY: sigaction is used here to allow us to indicate
640
   * (w/ POSIX at least) that we want SIGURG to interrupt syscalls.  Put
641
   * in whatever is necessary for your arch here; probably not necessary
642
   * as the only _important_ interrupted syscall is select(), which on
643
   * any sensible system is interrupted.
644
   */
645

646
#if defined(HAVE_SIGACTION)
647
  act.sa_handler = data_urgent;
×
648
  sigemptyset(&act.sa_mask);
×
649
  act.sa_flags = 0;
×
650

651
# if defined(SA_INTERRUPT)
652
  act.sa_flags |= SA_INTERRUPT;
×
653
# endif /* SA_INTERRUPT */
654
# if defined(SA_RESTART)
655
  act.sa_flags &= SA_RESTART;
×
656
# endif /* SA_RESTART */
657

658
  if (sigaction(SIGURG, &act, NULL) < 0) {
×
659
    pr_log_pri(PR_LOG_WARNING,
×
660
      "warning: unable to set SIGURG signal handler: %s", strerror(errno));
×
661
  }
662

663
#elif defined(HAVE_SIGINTERRUPT)
664
  if (siginterrupt(SIGURG, 1) < 0) {
665
    pr_log_pri(PR_LOG_WARNING,
666
      "warning: unable to make SIGURG interrupt system calls: %s",
667
      strerror(errno));
668
  }
669
#endif /* HAVE_SIGACTION or HAVE_SIGINTERRUPT */
670

671
  /* Reset all of the timing-related variables for data transfers. */
672
  pr_gettimeofday_millis(&data_start_ms);
×
673
  data_first_byte_read = FALSE;
×
674
  data_first_byte_written = FALSE;
×
675

676
  return res;
×
677
}
678

679
void pr_data_close2(void) {
12✔
680
  nstrm = NULL;
12✔
681

682
  if (session.d != NULL) {
12✔
683
    pr_inet_lingering_close(session.pool, session.d, timeout_linger);
10✔
684
    session.d = NULL;
10✔
685
  }
686

687
  /* Aborts no longer necessary */
688
  signal(SIGURG, SIG_IGN);
12✔
689

690
  if (timeout_noxfer) {
12✔
691
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
12✔
692
  }
693

694
  if (timeout_stalled) {
12✔
695
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
12✔
696
  }
697

698
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
12✔
699
  session.sf_flags &= (SF_ALL^(SF_ABORT|SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
12✔
700
  pr_session_set_idle();
12✔
701
}
12✔
702

703
/* close == successful transfer */
704
void pr_data_close(int quiet) {
3✔
705
  pr_data_close2();
3✔
706

707
  if (quiet == FALSE) {
3✔
708
    pr_response_add(R_226, _("Transfer complete"));
1✔
709
  }
710
}
3✔
711

712
/* Note: true_abort may be false in real abort situations, because
713
 * some ftp clients close the data connection at the same time as they
714
 * send the OOB byte (which results in a broken pipe on our
715
 * end).  Thus, it's a race between the OOB data and the tcp close
716
 * finishing.  Either way, it's ok (client will see either "Broken pipe"
717
 * error or "Aborted").  xfer_abor() in mod_xfer cleans up the session
718
 * flags in any case.  session flags will end up have SF_POST_ABORT
719
 * set if the OOB byte won the race.
720
 */
721
void pr_data_cleanup(void) {
2✔
722
  /* sanity check */
723
  if (session.d != NULL) {
2✔
724
    pr_inet_lingering_close(session.pool, session.d, timeout_linger);
1✔
725
    session.d = NULL;
1✔
726
  }
727

728
  pr_data_clear_xfer_pool();
2✔
729

730
  /* Clear/restore the default data transfer type. Otherwise, things like
731
   * APPEs or STOUs will be preserved for the next upload erroneously
732
   * (see Bug#3612).
733
   */
734
  session.xfer.xfer_type = STOR_DEFAULT;
2✔
735
}
2✔
736

737
/* In order to avoid clearing the transfer counters in session.xfer, we don't
738
 * clear session.xfer here, it should be handled by the appropriate
739
 * LOG_CMD/LOG_CMD_ERR handler calling pr_data_cleanup().
740
 */
741
void pr_data_abort(int err, int quiet) {
11✔
742
  int true_abort = XFER_ABORTED;
11✔
743
  nstrm = NULL;
11✔
744

745
  pr_trace_msg(trace_channel, 9,
24✔
746
    "aborting data transfer (errno = %s (%d), quiet = %s, true abort = %s)",
747
    strerror(err), err, quiet ? "true" : "false",
748
    true_abort ? "true" : "false");
749

750
  if (session.d != NULL) {
11✔
751
    if (true_abort) {
9✔
752
      /* For "true" aborts, we also asynchronously send a 426 response
753
       * message via the "lingering abort" functions.
754
       */
755
      pr_inet_lingering_abort(session.pool, session.d, timeout_linger);
8✔
756

757
    } else {
758
      pr_inet_lingering_close(session.pool, session.d, timeout_linger);
1✔
759
    }
760

761
    session.d = NULL;
9✔
762
  }
763

764
  if (timeout_noxfer) {
11✔
765
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
11✔
766
  }
767

768
  if (timeout_stalled) {
11✔
769
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
11✔
770
  }
771

772
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
11✔
773
  session.sf_flags &= (SF_ALL^(SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
11✔
774
  pr_session_set_idle();
11✔
775

776
  if (!quiet) {
11✔
777
    char *respcode = R_426;
10✔
778
    char *msg = NULL;
10✔
779
    char msgbuf[64];
10✔
780

781
    switch (err) {
10✔
782

783
    case 0:
2✔
784
#ifdef ECONNABORTED
785
    case ECONNABORTED:        /* FALLTHROUGH */
786
#endif
787
#ifdef ECONNRESET
788
    case ECONNRESET:        /* FALLTHROUGH */
789
#endif
790
#ifdef EPIPE
791
    case EPIPE:
792
#endif
793
      respcode = R_426;
2✔
794
      msg = _("Data connection closed");
2✔
795
      break;
2✔
796

797
#ifdef ENXIO
798
    case ENXIO:
1✔
799
      respcode = R_451;
1✔
800
      msg = _("Unexpected streams hangup");
1✔
801
      break;
1✔
802
#endif
803

804
#ifdef EAGAIN
805
    case EAGAIN:                /* FALLTHROUGH */
1✔
806
#endif
807
#ifdef ENOMEM
808
    case ENOMEM:
809
#endif
810
#if defined(EAGAIN) || defined(ENOMEM)
811
      respcode = R_451;
1✔
812
      msg = _("Insufficient memory or file locked");
1✔
813
      break;
1✔
814
#endif
815

816
#ifdef ETXTBSY
817
    case ETXTBSY:                /* FALLTHROUGH */
818
#endif
819
#ifdef EBUSY
820
    case EBUSY:
821
#endif
822
#if defined(ETXTBSY) || defined(EBUSY)
823
      respcode = R_451;
824
      break;
825
#endif
826

827
#ifdef ENOSPC
828
    case ENOSPC:
829
      respcode = R_452;
830
      break;
831
#endif
832

833
#ifdef EDQUOT
834
    case EDQUOT:                /* FALLTHROUGH */
835
#endif
836
#ifdef EFBIG
837
    case EFBIG:
838
#endif
839
#if defined(EDQUOT) || defined(EFBIG)
840
      respcode = R_552;
841
      break;
842
#endif
843

844
#ifdef ECOMM
845
    case ECOMM:                /* FALLTHROUGH */
846
#endif
847
#ifdef EDEADLK
848
    case EDEADLK:                /* FALLTHROUGH */
849
#endif
850
#ifdef EDEADLOCK
851
# if !defined(EDEADLK) || (EDEADLOCK != EDEADLK)
852
    case EDEADLOCK:                /* FALLTHROUGH */
853
# endif
854
#endif
855
#ifdef EXFULL
856
    case EXFULL:                /* FALLTHROUGH */
857
#endif
858
#ifdef ENOSR
859
    case ENOSR:                /* FALLTHROUGH */
860
#endif
861
#ifdef EPROTO
862
    case EPROTO:                /* FALLTHROUGH */
863
#endif
864
#ifdef ETIME
865
    case ETIME:                /* FALLTHROUGH */
866
#endif
867
#ifdef EIO
868
    case EIO:                /* FALLTHROUGH */
869
#endif
870
#ifdef EFAULT
871
    case EFAULT:                /* FALLTHROUGH */
872
#endif
873
#ifdef ESPIPE
874
    case ESPIPE:                /* FALLTHROUGH */
875
#endif
876
#if defined(ECOMM) || defined(EDEADLK) ||  defined(EDEADLOCK) || \
877
    defined(EXFULL) || defined(ENOSR) || defined(EPROTO) || \
878
    defined(ETIME) || defined(EIO) || defined(EFAULT) || \
879
    defined(ESPIPE) || defined(EPIPE)
880
      respcode = R_451;
881
      break;
882
#endif
883

884
#ifdef EREMCHG
885
    case EREMCHG:                /* FALLTHROUGH */
×
886
#endif
887
#ifdef ESRMNT
888
    case ESRMNT:                /* FALLTHROUGH */
889
#endif
890
#ifdef ESTALE
891
    case ESTALE:                /* FALLTHROUGH */
892
#endif
893
#ifdef ENOLINK
894
    case ENOLINK:                /* FALLTHROUGH */
895
#endif
896
#ifdef ENOLCK
897
    case ENOLCK:                /* FALLTHROUGH */
898
#endif
899
#ifdef ENETRESET
900
    case ENETRESET:                /* FALLTHROUGH */
901
#endif
902
#ifdef ETIMEDOUT
903
    case ETIMEDOUT:
904
#endif
905
#if defined(EREMCHG) || defined(ESRMNT) ||  defined(ESTALE) || \
906
    defined(ENOLINK) || defined(ENOLCK) || defined(ENETRESET) || \
907
    defined(ECONNABORTED) || defined(ECONNRESET) || defined(ETIMEDOUT)
908
      respcode = R_450;
×
909
      msg = _("Link to file server lost");
×
910
      break;
×
911
#endif
912

913
    default:
2✔
914
      respcode = R_426;
2✔
915
      msg = _("Data connection closed");
2✔
916
      break;
2✔
917
    }
918

919
    if (msg == NULL &&
6✔
920
        (msg = strerror(err)) == NULL) {
4✔
921

922
      if (pr_snprintf(msgbuf, sizeof(msgbuf),
×
923
          _("Unknown or out of range errno [%d]"), err) > 0) {
×
924
        msg = msgbuf;
×
925
      }
926
    }
927

928
    pr_log_pri(PR_LOG_NOTICE, "notice: user %s: aborting transfer: %s",
10✔
929
      session.user ? session.user : "(unknown)", msg);
10✔
930

931
    /* If we are aborting, then a 426 response has already been sent via
932
     * pr_inet_lingering_abort(), and we don't want to add another to the
933
     * error queue.
934
     */
935
    if (true_abort == FALSE) {
10✔
936
      pr_response_add_err(respcode, _("Transfer aborted. %s"), msg ? msg : "");
2✔
937
    }
938

939
    /* Forcibly clear the data-transfer instigating command pool from the
940
     * Response API.
941
     */
942
    pr_response_set_pool(session.pool);
10✔
943
  }
944

945
  if (true_abort) {
11✔
946
    session.sf_flags |= SF_POST_ABORT;
8✔
947
  }
948
}
11✔
949

950
/* From response.c.  XXX Need to provide these symbols another way. */
951
extern pr_response_t *resp_list, *resp_err_list;
952

953
static int peek_is_abor_cmd(void) {
14✔
954
  int fd, res;
14✔
955
  fd_set rfds;
14✔
956
  struct timeval tv;
14✔
957
  ssize_t len;
14✔
958
  char buf[5];
14✔
959

960
  tv.tv_sec = 1;
14✔
961
  tv.tv_usec = 0;
14✔
962

963
  fd = PR_NETIO_FD(session.c->instrm);
14✔
964
  pr_trace_msg(trace_channel, 20, "peeking at next data for fd %d", fd);
14✔
965

966
  FD_ZERO(&rfds);
252✔
967
  FD_SET(fd, &rfds);
14✔
968

969
  res = select(fd + 1, &rfds, NULL, NULL, &tv);
14✔
970
  while (res < 0) {
14✔
971
    int xerrno = errno;
×
972

973
    if (xerrno == EINTR) {
×
974
      pr_signals_handle();
×
975
      res = select(fd + 1, &rfds, NULL, NULL, &tv);
×
976
      continue;
×
977
    }
978

979
    pr_trace_msg(trace_channel, 20,
×
980
      "error waiting for next data on fd %d: %s", fd, strerror(xerrno));
981
    return FALSE;
×
982
  }
983

984
  if (res == 0) {
14✔
985
    /* Timed out. */
986
    pr_trace_msg(trace_channel, 20, "timed out peeking for data on fd %d", fd);
13✔
987
    return FALSE;
13✔
988
  }
989

990
  /* If we reach here, the peer must have sent something.  Let's see what it
991
   * might be.  Chances are that we received at least 5 bytes, but to be
992
   * defensive, we use MSG_WAITALL anyway.  TCP allows for sending one byte
993
   * at time, if need be.  The shortest FTP command is 5 bytes, e.g. "CCC\r\n".
994
   * ABOR would be 6 bytes, but we do not want to block until we see 6 bytes;
995
   * we're peeking opportunistically, and optimistically.
996
   */
997
  memset(&buf, 0, sizeof(buf));
1✔
998
  len = recv(fd, buf, sizeof(buf), MSG_PEEK|MSG_WAITALL);
1✔
999
  while (len < 0) {
1✔
1000
    int xerrno = errno;
1✔
1001

1002
    if (xerrno == EINTR) {
1✔
1003
      pr_signals_handle();
×
1004
      len = recv(fd, &buf, sizeof(buf), MSG_PEEK|MSG_WAITALL);
×
1005
      continue;
×
1006
    }
1007

1008
    pr_trace_msg(trace_channel, 20,
1✔
1009
      "error peeking at next data: %s", strerror(xerrno));
1010
    return FALSE;
1✔
1011
  }
1012

1013
  pr_trace_msg(trace_channel, 20, "peeking at %ld bytes of next data",
×
1014
    (long) len);
1015
  if (strncasecmp(buf, "ABOR\r", len) == 0) {
×
1016
    pr_trace_msg(trace_channel, 20, "peeked data probably 'ABOR' command");
×
1017
    return TRUE;
×
1018
  }
1019

1020
  pr_trace_msg(trace_channel, 20, "peeked data '%.*s' not ABOR command",
×
1021
    (int) len, buf);
1022
  return FALSE;
×
1023
}
1024

1025
static void poll_ctrl(void) {
26✔
1026
  int res;
26✔
1027

1028
  if (session.c == NULL) {
26✔
1029
    return;
1030
  }
1031

1032
  pr_trace_msg(trace_channel, 4, "polling for commands on control channel");
14✔
1033
  pr_netio_set_poll_interval(session.c->instrm, 0);
14✔
1034
  res = pr_netio_poll(session.c->instrm);
14✔
1035
  pr_netio_reset_poll_interval(session.c->instrm);
14✔
1036

1037
  if (res == 0 &&
14✔
1038
      !(session.sf_flags & SF_ABORT)) {
14✔
1039

1040
    /* First, we peek at the data, to see if it is an ABOR command.  Why?
1041
     *
1042
     * Consider the case where a client uses the TCP OOB mechanism and
1043
     * marks the ABOR command with the marker.  In that case, a SIGURG signal
1044
     * will have been raised, and the SF_ABORT flag set.  The actual "ABOR"
1045
     * data on the control connection will be read only AFTER the data transfer
1046
     * has been failed.  This leads the proper ordering of multiple responses
1047
     * (first for failed transfer, second for successful ABOR) in such cases.
1048
     *
1049
     * Now consider the case where a client does NOT use the TCP OOB mechanism,
1050
     * and only sends the ABOR command.  We want the same behavior in this case
1051
     * as for the TCP OOB case, BUT now, the SF_ABORT flag has NOT been set at
1052
     * this point in the flow.  Which means that we might read that "ABOR" text
1053
     * from the control connection _in the middle_ of the data transfer, which
1054
     * leads to different behavior, different ordering of responses.
1055
     *
1056
     * Thus we cheat here, and only peek at the control connection data.  IFF
1057
     * it is the "ABOR\r\n" text, then we set the SF_ABORT flag ourselves
1058
     * here, and preserve the expected semantics (Bug #4402).
1059
     */
1060
    if (peek_is_abor_cmd() == TRUE) {
14✔
1061
      pr_trace_msg(trace_channel, 5, "client sent 'ABOR' command during data "
×
1062
        "transfer, setting 'aborted' session flag");
1063

1064
      session.sf_flags |= SF_ABORT;
×
1065

1066
      if (nstrm != NULL) {
×
1067
        pr_netio_abort(nstrm);
×
1068
        errno = 0;
×
1069
      }
1070
    }
1071
  }
1072

1073
  if (res == 0 &&
14✔
1074
      !(session.sf_flags & SF_ABORT)) {
14✔
1075
    cmd_rec *cmd = NULL;
14✔
1076

1077
    pr_trace_msg(trace_channel, 1,
14✔
1078
      "data available for reading on control channel during data transfer, "
1079
      "reading control data");
1080
    res = pr_cmd_read(&cmd);
14✔
1081
    if (res < 0) {
14✔
1082
      int xerrno;
×
1083

1084
#if defined(ECONNABORTED)
1085
      xerrno = ECONNABORTED;
×
1086
#elif defined(ENOTCONN)
1087
      xerrno = ENOTCONN;
1088
#else
1089
      xerrno = EIO;
1090
#endif
1091

1092
      pr_trace_msg(trace_channel, 1,
×
1093
        "unable to read control command during data transfer: %s",
1094
        strerror(xerrno));
1095
      errno = xerrno;
×
1096

1097
#ifndef PR_DEVEL_NO_DAEMON
1098
      /* Otherwise, EOF */
1099
      pr_session_disconnect(NULL, PR_SESS_DISCONNECT_CLIENT_EOF, NULL);
×
1100
#else
1101
      return;
1102
#endif /* PR_DEVEL_NO_DAEMON */
1103

1104
    } else if (cmd != NULL) {
14✔
1105
      char *ch;
7✔
1106

1107
      for (ch = cmd->argv[0]; *ch; ch++) {
35✔
1108
        *ch = toupper((int) *ch);
28✔
1109
      }
1110

1111
      cmd->cmd_id = pr_cmd_get_id(cmd->argv[0]);
7✔
1112

1113
      /* Only handle commands which do not involve data transfers; we
1114
       * already have a data transfer in progress.  For any data transfer
1115
       * command, send a 450 ("busy") reply.  Looks like almost all of the
1116
       * data transfer commands accept that response, as per RFC959.
1117
       *
1118
       * We also prevent the EPRT, EPSV, PASV, and PORT commands, since
1119
       * they will also interfere with the current data transfer.  In doing
1120
       * so, we break RFC compliance a little; RFC959 does not allow a
1121
       * response code of 450 for those commands (although it should).
1122
       */
1123
      if (pr_cmd_cmp(cmd, PR_CMD_APPE_ID) == 0 ||
14✔
1124
          pr_cmd_cmp(cmd, PR_CMD_LIST_ID) == 0 ||
14✔
1125
          pr_cmd_cmp(cmd, PR_CMD_MLSD_ID) == 0 ||
14✔
1126
          pr_cmd_cmp(cmd, PR_CMD_NLST_ID) == 0 ||
14✔
1127
          pr_cmd_cmp(cmd, PR_CMD_RETR_ID) == 0 ||
14✔
1128
          pr_cmd_cmp(cmd, PR_CMD_STOR_ID) == 0 ||
14✔
1129
          pr_cmd_cmp(cmd, PR_CMD_STOU_ID) == 0 ||
14✔
1130
          pr_cmd_cmp(cmd, PR_CMD_RNFR_ID) == 0 ||
14✔
1131
          pr_cmd_cmp(cmd, PR_CMD_RNTO_ID) == 0 ||
14✔
1132
          pr_cmd_cmp(cmd, PR_CMD_PORT_ID) == 0 ||
14✔
1133
          pr_cmd_cmp(cmd, PR_CMD_EPRT_ID) == 0 ||
14✔
1134
          pr_cmd_cmp(cmd, PR_CMD_PASV_ID) == 0 ||
12✔
1135
          pr_cmd_cmp(cmd, PR_CMD_EPSV_ID) == 0) {
7✔
1136
        pool *resp_pool;
2✔
1137

1138
        pr_trace_msg(trace_channel, 5,
2✔
1139
          "client sent '%s' command during data transfer, denying",
1140
          (char *) cmd->argv[0]);
2✔
1141

1142
        resp_list = resp_err_list = NULL;
2✔
1143
        resp_pool = pr_response_get_pool();
2✔
1144

1145
        pr_response_set_pool(cmd->pool);
2✔
1146

1147
        pr_response_add_err(R_450, _("%s: data transfer in progress"),
2✔
1148
          (char *) cmd->argv[0]);
2✔
1149

1150
        pr_response_flush(&resp_err_list);
2✔
1151

1152
        pr_response_set_pool(resp_pool);
2✔
1153
        destroy_pool(cmd->pool);
2✔
1154

1155
      /* We don't want to actually dispatch the NOOP command, since that
1156
       * would overwrite the scoreboard with the NOOP state; admins probably
1157
       * want to see the command that caused the data transfer.  And since
1158
       * NOOP doesn't take a 450 response (as per RFC959), we will simply
1159
       * return 200.
1160
       */
1161
      } else if (pr_cmd_cmp(cmd, PR_CMD_NOOP_ID) == 0) {
5✔
1162
        pool *resp_pool;
2✔
1163

1164
        pr_trace_msg(trace_channel, 5,
2✔
1165
          "client sent '%s' command during data transfer, ignoring",
1166
          (char *) cmd->argv[0]);
2✔
1167

1168
        resp_list = resp_err_list = NULL;
2✔
1169
        resp_pool = pr_response_get_pool();
2✔
1170

1171
        pr_response_set_pool(cmd->pool);
2✔
1172

1173
        pr_response_add(R_200, _("%s: data transfer in progress"),
2✔
1174
          (char *) cmd->argv[0]);
2✔
1175

1176
        pr_response_flush(&resp_list);
2✔
1177

1178
        pr_response_set_pool(resp_pool);
2✔
1179
        destroy_pool(cmd->pool);
2✔
1180

1181
      } else {
1182
        char *title_buf = NULL;
3✔
1183
        int curr_cmd_id = 0, title_len = -1;
3✔
1184
        const char *curr_cmd = NULL, *sce_cmd = NULL, *sce_cmd_arg = NULL;
3✔
1185
        cmd_rec *curr_cmd_rec = NULL;
3✔
1186
        pool *resp_pool;
3✔
1187

1188
        pr_trace_msg(trace_channel, 5,
3✔
1189
          "client sent '%s' command during data transfer, dispatching",
1190
          (char *) cmd->argv[0]);
3✔
1191

1192
        title_len = pr_proctitle_get(NULL, 0);
3✔
1193
        if (title_len > 0) {
3✔
1194
          title_buf = pcalloc(cmd->pool, title_len + 1);
×
1195
          pr_proctitle_get(title_buf, title_len + 1);
×
1196
        }
1197

1198
        curr_cmd = session.curr_cmd;
3✔
1199
        curr_cmd_id = session.curr_cmd_id;
3✔
1200
        curr_cmd_rec = session.curr_cmd_rec;
3✔
1201
        sce_cmd = pr_scoreboard_entry_get(PR_SCORE_CMD);
3✔
1202
        sce_cmd_arg = pr_scoreboard_entry_get(PR_SCORE_CMD_ARG);
3✔
1203

1204
        resp_list = resp_err_list = NULL;
3✔
1205
        resp_pool = pr_response_get_pool();
3✔
1206

1207
        pr_response_set_pool(cmd->pool);
3✔
1208
        pr_cmd_dispatch(cmd);
3✔
1209

1210
        pr_scoreboard_entry_update(session.pid,
3✔
1211
          PR_SCORE_CMD, "%s", sce_cmd, NULL, NULL);
1212
        pr_scoreboard_entry_update(session.pid,
3✔
1213
          PR_SCORE_CMD_ARG, "%s", sce_cmd_arg, NULL, NULL);
1214

1215
        if (title_len > 0) {
3✔
1216
          pr_proctitle_set_str(title_buf);
×
1217
        }
1218

1219
        pr_response_flush(&resp_list);
3✔
1220
        pr_response_set_pool(resp_pool);
3✔
1221
        destroy_pool(cmd->pool);
3✔
1222

1223
        session.curr_cmd = curr_cmd;
3✔
1224
        session.curr_cmd_id = curr_cmd_id;
3✔
1225
        session.curr_cmd_rec = curr_cmd_rec;
3✔
1226
      }
1227

1228
    } else {
1229
      pr_trace_msg(trace_channel, 3,
7✔
1230
        "invalid command sent, sending error response");
1231
      pr_response_send(R_500, _("Invalid command: try being more creative"));
7✔
1232
    }
1233
  }
1234
}
1235

1236
/* pr_data_xfer() actually transfers the data on the data connection.  ASCII
1237
 * translation is performed if necessary.  `direction' is set when the data
1238
 * connection was opened.
1239
 *
1240
 * We determine if the client buffer is read from or written to.  Returns 0 if
1241
 * reading and data connection closes, or -1 if error (with errno set).
1242
 */
1243
int pr_data_xfer(char *cl_buf, size_t cl_size) {
34✔
1244
  int len = 0;
34✔
1245
  int total = 0;
34✔
1246
  int res = 0;
34✔
1247
  pool *tmp_pool = NULL;
34✔
1248

1249
  if (cl_buf == NULL ||
34✔
1250
      cl_size == 0) {
34✔
1251
    errno = EINVAL;
8✔
1252
    return -1;
8✔
1253
  }
1254

1255
  /* Poll the control channel for any commands we should handle, like
1256
   * QUIT or ABOR.
1257
   */
1258
  poll_ctrl();
26✔
1259

1260
  /* If we don't have a data connection here (e.g. might have been closed
1261
   * by an ABOR), then return zero (no data transferred).
1262
   */
1263
  if (session.d == NULL) {
26✔
1264
    int xerrno;
4✔
1265

1266
#if defined(ECONNABORTED)
1267
    xerrno = ECONNABORTED;
4✔
1268
#elif defined(ENOTCONN)
1269
    xerrno = ENOTCONN;
1270
#else
1271
    xerrno = EIO;
1272
#endif
1273

1274
    pr_trace_msg(trace_channel, 1,
4✔
1275
      "data connection is null prior to data transfer (possibly from "
1276
      "aborted transfer), returning '%s' error", strerror(xerrno));
1277
    pr_log_debug(DEBUG5,
4✔
1278
      "data connection is null prior to data transfer (possibly from "
1279
       "aborted transfer), returning '%s' error", strerror(xerrno));
1280

1281
    errno = xerrno;
4✔
1282
    return -1;
4✔
1283
  }
1284

1285
  if (session.xfer.direction == PR_NETIO_IO_RD) {
22✔
1286
    char *buf;
12✔
1287

1288
    buf = session.xfer.buf;
12✔
1289

1290
    /* We use ASCII translation if:
1291
     *
1292
     * - SF_ASCII session flag is set, AND IGNORE_ASCII data opt NOT set
1293
     */
1294
    if (((session.sf_flags & SF_ASCII) &&
12✔
1295
        !(data_opts & PR_DATA_OPT_IGNORE_ASCII))) {
7✔
1296
      int adjlen, buflen;
7✔
1297

1298
      do {
7✔
1299
        buflen = session.xfer.buflen;        /* how much remains in buf */
7✔
1300
        adjlen = 0;
7✔
1301

1302
        pr_signals_handle();
7✔
1303

1304
        len = pr_netio_read(session.d->instrm, buf + buflen,
7✔
1305
          session.xfer.bufsize - buflen, 1);
7✔
1306
        while (len < 0) {
7✔
1307
          int xerrno = errno;
1✔
1308

1309
          if (xerrno == EAGAIN || xerrno == EINTR) {
1✔
1310
            /* Since our socket is in non-blocking mode, read(2) can return
1311
             * EAGAIN if there is no data yet for us.  Handle this by
1312
             * delaying temporarily, then trying again.
1313
             */
1314
            errno = EINTR;
×
1315
            pr_signals_handle();
×
1316

1317
            len = pr_netio_read(session.d->instrm, buf + buflen,
×
1318
              session.xfer.bufsize - buflen, 1);
×
1319
            continue;
×
1320
          }
1321

1322
          destroy_pool(tmp_pool);
1✔
1323
          errno = xerrno;
1✔
1324
          return -1;
1✔
1325
        }
1326

1327
        if (len > 0 &&
6✔
1328
            data_first_byte_read == FALSE) {
6✔
1329
          if (pr_trace_get_level(timing_channel)) {
2✔
1330
            unsigned long elapsed_ms;
2✔
1331
            uint64_t read_ms;
2✔
1332

1333
            pr_gettimeofday_millis(&read_ms);
2✔
1334
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1335

1336
            pr_trace_msg(timing_channel, 7,
2✔
1337
              "Time for first data byte read: %lu ms", elapsed_ms);
1338
          }
1339

1340
          data_first_byte_read = TRUE;
2✔
1341
        }
1342

1343
        if (len > 0) {
6✔
1344
          pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
6✔
1345
            len != 1 ? "bytes" : "byte");
1346

1347
          buflen += len;
6✔
1348

1349
          if (timeout_stalled) {
6✔
1350
            pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
6✔
1351
          }
1352
        }
1353

1354
        /* If buflen > 0, data remains in the buffer to be copied. */
1355
        if (len >= 0 &&
6✔
1356
            buflen > 0) {
6✔
1357

1358
          /* Perform ASCII translation:
1359
           *
1360
           * buflen: is returned as the modified buffer length after
1361
           *         translation
1362
           * res:    is returned as the number of characters unprocessed in
1363
           *         the buffer (to be dealt with later)
1364
           *
1365
           * We skip the call to pr_ascii_ftp_from_crlf() in one case:
1366
           * when we have one character in the buffer and have reached
1367
           * end of data, this is so that pr_ascii_ftp_from_crlf() won't sit
1368
           * forever waiting for the next character after a final '\r'.
1369
           */
1370
          if (len > 0 ||
6✔
1371
              buflen > 1) {
6✔
1372
            size_t outlen = 0;
6✔
1373

1374
            if (tmp_pool == NULL) {
6✔
1375
              tmp_pool = make_sub_pool(session.xfer.p);
6✔
1376
              pr_pool_tag(tmp_pool, "ASCII upload");
6✔
1377
            }
1378

1379
            res = pr_ascii_ftp_from_crlf(tmp_pool, buf, buflen, &buf, &outlen);
6✔
1380
            if (res < 0) {
6✔
1381
              pr_trace_msg(trace_channel, 3, "error reading ASCII data: %s",
×
1382
                strerror(errno));
×
1383

1384
            } else {
1385
              adjlen += res;
6✔
1386
              buflen = (int) outlen;
6✔
1387
            }
1388
          }
1389

1390
          /* Now copy everything we can into cl_buf */
1391
          if ((size_t) buflen > cl_size) {
6✔
1392
            /* Because we have to cut our buffer short, make sure this
1393
             * is made up for later by increasing adjlen.
1394
             */
1395
            adjlen += (buflen - cl_size);
×
1396
            buflen = cl_size;
×
1397
          }
1398

1399
          memcpy(cl_buf, buf, buflen);
6✔
1400

1401
          /* Copy whatever remains at the end of session.xfer.buf to the
1402
           * head of the buffer and adjust buf accordingly.
1403
           *
1404
           * adjlen is now the total bytes still waiting in buf, if
1405
           * anything remains, copy it to the start of the buffer.
1406
           */
1407

1408
          if (adjlen > 0) {
6✔
1409
            memcpy(buf, buf + buflen, adjlen);
1✔
1410
          }
1411

1412
          /* Store everything back in session.xfer. */
1413
          session.xfer.buflen = adjlen;
6✔
1414
          total += buflen;
6✔
1415
        }
1416
        
1417
        /* Restart if data was returned by pr_netio_read() (len > 0) but no
1418
         * data was copied to the client buffer (buflen = 0).  This indicates
1419
         * that pr_ascii_ftp_from_crlf() needs more data in order to
1420
         * translate, so we need to call pr_netio_read() again.
1421
         */
1422
      } while (len > 0 && buflen == 0);
6✔
1423

1424
      /* Return how much data we actually copied into the client buffer. */
1425
      len = buflen;
1426

1427
    } else {
1428
      len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
5✔
1429
      while (len < 0) {
5✔
1430
        int xerrno = errno;
1✔
1431

1432
        if (xerrno == EAGAIN || xerrno == EINTR) {
1✔
1433
          /* Since our socket is in non-blocking mode, read(2) can return
1434
           * EAGAIN if there is no data yet for us.  Handle this by
1435
           * delaying temporarily, then trying again.
1436
           */
1437
          errno = EINTR;
×
1438
          pr_signals_handle();
×
1439

1440
          len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
×
1441
          continue;
×
1442
        }
1443

1444
        break;
1445
      }
1446

1447
      if (len > 0) {
5✔
1448
        pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
4✔
1449
          len != 1 ? "bytes" : "byte");
1450

1451
        if (data_first_byte_read == FALSE) {
4✔
1452
          if (pr_trace_get_level(timing_channel)) {
2✔
1453
            unsigned long elapsed_ms;
2✔
1454
            uint64_t read_ms;
2✔
1455

1456
            pr_gettimeofday_millis(&read_ms);
2✔
1457
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1458

1459
            pr_trace_msg(timing_channel, 7,
2✔
1460
              "Time for first data byte read: %lu ms", elapsed_ms);
1461
          }
1462

1463
          data_first_byte_read = TRUE;
2✔
1464
        }
1465

1466
        /* Non-ASCII mode doesn't need to use session.xfer.buf */
1467
        if (timeout_stalled) {
4✔
1468
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
4✔
1469
        }
1470

1471
        total += len;
1472
      }
1473
    }
1474

1475
  } else { /* PR_NETIO_IO_WR */
1476

1477
    while (cl_size) {
18✔
1478
      int bwrote = 0;
10✔
1479
      int buflen = cl_size;
10✔
1480
      unsigned int xferbuflen;
10✔
1481
      char *xferbuf = NULL, *ascii_buf = NULL;
10✔
1482

1483
      pr_signals_handle();
10✔
1484

1485
      if (buflen > pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR)) {
10✔
1486
        buflen = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR);
×
1487
      }
1488

1489
      xferbuf = cl_buf;
10✔
1490
      xferbuflen = buflen;
10✔
1491

1492
      /* We use ASCII translation if:
1493
       *
1494
       * - SF_ASCII_OVERRIDE session flag is set (e.g. for LIST/NLST)
1495
       * - SF_ASCII session flag is set, AND IGNORE_ASCII data opt NOT set
1496
       */
1497
      if ((session.sf_flags & SF_ASCII_OVERRIDE) ||
10✔
1498
          ((session.sf_flags & SF_ASCII) &&
4✔
1499
           !(data_opts & PR_DATA_OPT_IGNORE_ASCII))) {
1✔
1500
        char *out = NULL;
7✔
1501
        size_t outlen = 0;
7✔
1502

1503
        if (tmp_pool == NULL) {
7✔
1504
          tmp_pool = make_sub_pool(session.xfer.p);
7✔
1505
          pr_pool_tag(tmp_pool, "ASCII download");
7✔
1506
        }
1507

1508
        /* Fill up our internal buffer. */
1509
        memcpy(session.xfer.buf, cl_buf, buflen);
7✔
1510

1511
        /* Scan the internal buffer, looking for LFs with no preceding CRs.
1512
         * Add CRs (and expand the internal buffer) as necessary. xferbuflen
1513
         * will be adjusted so that it contains the length of data in
1514
         * the internal buffer, including any added CRs.
1515
         */
1516
        res = pr_ascii_ftp_to_crlf(tmp_pool, session.xfer.buf, xferbuflen,
7✔
1517
          &out, &outlen);
1518
        if (res < 0) {
7✔
1519
          pr_trace_msg(trace_channel, 1, "error writing ASCII data: %s",
×
1520
            strerror(errno));
×
1521

1522
        } else {
1523
          ascii_buf = session.xfer.buf;
7✔
1524
          session.xfer.buf = out;
7✔
1525
          session.xfer.buflen = xferbuflen = outlen;
7✔
1526
        }
1527

1528
        xferbuf = session.xfer.buf;
7✔
1529
      }
1530

1531
      bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
10✔
1532
      while (bwrote < 0) {
12✔
1533
        int xerrno = errno;
4✔
1534

1535
        if (xerrno == EAGAIN ||
6✔
1536
            xerrno == EINTR) {
4✔
1537
          /* Since our socket may be in non-blocking mode, write(2) can return
1538
           * EAGAIN if there is not enough from for our data yet.  Handle
1539
           * this by delaying temporarily, then trying again.
1540
           */
1541
          errno = EINTR;
2✔
1542
          pr_signals_handle();
2✔
1543

1544
          bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
2✔
1545
          continue;
2✔
1546
        }
1547

1548
        destroy_pool(tmp_pool);
2✔
1549
        if (ascii_buf != NULL) {
2✔
1550
          /* Free up the malloc'd memory. */
1551
          free(session.xfer.buf);
1✔
1552
          session.xfer.buf = ascii_buf;
1✔
1553
        }
1554

1555
        errno = xerrno;
2✔
1556
        return -1;
2✔
1557
      }
1558

1559
      if (bwrote > 0) {
8✔
1560
        pr_trace_msg(trace_channel, 19, "wrote %d %s to network", bwrote,
8✔
1561
          bwrote != 1 ? "bytes" : "byte");
1562

1563
        if (data_first_byte_written == FALSE) {
8✔
1564
          if (pr_trace_get_level(timing_channel)) {
3✔
1565
            unsigned long elapsed_ms;
3✔
1566
            uint64_t write_ms;
3✔
1567

1568
            pr_gettimeofday_millis(&write_ms);
3✔
1569
            elapsed_ms = (unsigned long) (write_ms - data_start_ms);
3✔
1570

1571
            pr_trace_msg(timing_channel, 7,
3✔
1572
              "Time for first data byte written: %lu ms", elapsed_ms);
1573
          }
1574

1575
          data_first_byte_written = TRUE;
3✔
1576
        }
1577

1578
        if (timeout_stalled) {
8✔
1579
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
8✔
1580
        }
1581

1582
        cl_size -= buflen;
8✔
1583
        cl_buf += buflen;
8✔
1584
        total += buflen;
8✔
1585
      }
1586

1587
      if (ascii_buf != NULL) {
8✔
1588
        /* Yes, we are using malloc et al here, rather than the memory pools.
1589
         * See Bug#4352 for details.
1590
         */
1591
        free(session.xfer.buf);
6✔
1592
        session.xfer.buf = ascii_buf;
6✔
1593
      }
1594
    }
1595

1596
    len = total;
1597
  }
1598

1599
  if (total &&
19✔
1600
      timeout_idle) {
1601
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
18✔
1602
  }
1603

1604
  session.xfer.total_bytes += total;
19✔
1605
  session.total_bytes += total;
19✔
1606
  if (session.xfer.direction == PR_NETIO_IO_RD) {
19✔
1607
    session.total_bytes_in += total;
11✔
1608

1609
  } else {
1610
    session.total_bytes_out += total;
8✔
1611
  }
1612

1613
  destroy_pool(tmp_pool);
19✔
1614
  return (len < 0 ? -1 : len);
19✔
1615
}
1616

1617
#if defined(HAVE_SENDFILE)
1618
/* pr_data_sendfile() actually transfers the data on the data connection.
1619
 * ASCII translation is not performed.
1620
 * return 0 if reading and data connection closes, or -1 if error
1621
 */
1622
pr_sendfile_t pr_data_sendfile(int retr_fd, off_t *offset, off_t count) {
7✔
1623
  int flags, error;
7✔
1624
  pr_sendfile_t len = 0, total = 0;
7✔
1625
# if defined(HAVE_AIX_SENDFILE)
1626
  struct sf_parms parms;
1627
  int rc;
1628
# endif /* HAVE_AIX_SENDFILE */
1629

1630
  if (offset == NULL ||
7✔
1631
      count == 0) {
7✔
1632
    errno = EINVAL;
3✔
1633
    return -1;
3✔
1634
  }
1635

1636
  if (session.xfer.direction == PR_NETIO_IO_RD) {
4✔
1637
    errno = EPERM;
1✔
1638
    return -1;
1✔
1639
  }
1640

1641
  if (session.d == NULL) {
3✔
1642
    errno = EPERM;
1✔
1643
    return -1;
1✔
1644
  }
1645

1646
  flags = fcntl(PR_NETIO_FD(session.d->outstrm), F_GETFL);
2✔
1647
  if (flags < 0) {
2✔
1648
    return -1;
1649
  }
1650

1651
  /* Set fd to blocking-mode for sendfile() */
1652
  if (flags & O_NONBLOCK) {
2✔
1653
    if (fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags^O_NONBLOCK) < 0) {
×
1654
      return -1;
1655
    }
1656
  }
1657

1658
  for (;;) {
2✔
1659
# if defined(HAVE_LINUX_SENDFILE) || defined(HAVE_SOLARIS_SENDFILE)
1660
    off_t orig_offset = *offset;
2✔
1661

1662
    /* Linux semantics are fairly straightforward in a glibc 2.x world:
1663
     *
1664
     *   #include <sys/sendfile.h>
1665
     *
1666
     *   ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
1667
     *
1668
     * Unfortunately, this API does not allow for an off_t number of bytes
1669
     * to be sent, only a size_t.  This means we need to make sure that
1670
     * the given count does not exceed the maximum value for a size_t.  Even
1671
     * worse, the return value is a ssize_t, not a size_t, which means
1672
     * the maximum value used can only be of the maximum _signed_ value,
1673
     * not the maximum unsigned value.  This means calling sendfile() more
1674
     * times.  How annoying.
1675
     */
1676

1677
#  if defined(HAVE_LINUX_SENDFILE)
1678
    if (count > INT_MAX) {
2✔
1679
      count = INT_MAX;
1680
    }
1681
#  elif defined(HAVE_SOLARIS_SENDFILE)
1682
#   if SIZEOF_SIZE_T == SIZEOF_INT
1683
    if (count > INT_MAX) {
1684
      count = INT_MAX;
1685
    }
1686
#   elif SIZEOF_SIZE_T == SIZEOF_LONG
1687
    if (count > LONG_MAX) {
1688
      count = LONG_MAX;
1689
    }
1690
#   elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1691
    if (count > LLONG_MAX) {
1692
      count = LLONG_MAX;
1693
    }
1694
#   endif
1695
#  endif /* !HAVE_SOLARIS_SENDFILE */
1696

1697
    errno = 0;
2✔
1698
    len = sendfile(PR_NETIO_FD(session.d->outstrm), retr_fd, offset, count);
2✔
1699

1700
    /* If no data could be written (e.g. the file was truncated), we're
1701
     * done (Bug#4318).
1702
     */
1703
    if (len == 0) {
2✔
1704
      if (errno != EINTR &&
×
1705
          errno != EAGAIN) {
1706
        break;
1707
      }
1708

1709
      /* Handle our interrupting signal, and continue. */
1710
      pr_signals_handle();
×
1711
    }
1712

1713
    if (len != -1 &&
2✔
1714
        len < count) {
2✔
1715
      /* Under Linux semantics, this occurs when a signal has interrupted
1716
       * sendfile().
1717
       */
1718
      if (XFER_ABORTED) {
×
1719
        errno = EINTR;
×
1720

1721
        session.xfer.total_bytes += len;
×
1722
        session.total_bytes += len;
×
1723
        session.total_bytes_out += len;
×
1724
        session.total_raw_out += len;
×
1725

1726
        return -1;
×
1727
      }
1728

1729
      count -= len;
×
1730

1731
      /* Only reset the timers if data have actually been written out. */
1732
      if (len > 0) {
×
1733
        if (timeout_stalled) {
×
1734
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1735
        }
1736

1737
        if (timeout_idle) {
×
1738
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1739
        }
1740
      }
1741

1742
      session.xfer.total_bytes += len;
×
1743
      session.total_bytes += len;
×
1744
      session.total_bytes_out += len;
×
1745
      session.total_raw_out += len;
×
1746
      total += len;
×
1747

1748
      pr_signals_handle();
×
1749
      continue;
×
1750
    }
1751

1752
    if (len == -1) {
2✔
1753
      /* Linux updates offset on error, not len like BSD, fix up so
1754
       * BSD-based code works.
1755
       */
1756
      len = *offset - orig_offset;
1✔
1757
      *offset = orig_offset;
1✔
1758

1759
# elif defined(HAVE_BSD_SENDFILE)
1760
    /* BSD semantics for sendfile are flexible...it'd be nice if we could
1761
     * standardize on something like it.  The semantics are:
1762
     *
1763
     *   #include <sys/types.h>
1764
     *   #include <sys/socket.h>
1765
     *   #include <sys/uio.h>
1766
     *
1767
     *   int sendfile(int in_fd, int out_fd, off_t offset, size_t count,
1768
     *                struct sf_hdtr *hdtr, off_t *len, int flags)
1769
     *
1770
     *  The comments above, about size_t versus off_t, apply here as
1771
     *  well.  Except that BSD's sendfile() uses an off_t * for returning
1772
     *  the number of bytes sent, so we can use the maximum unsigned
1773
     *  value.
1774
     */
1775

1776
#  if SIZEOF_SIZE_T == SIZEOF_INT
1777
    if (count > UINT_MAX) {
1778
      count = UINT_MAX;
1779
    }
1780
#  elif SIZEOF_SIZE_T == SIZEOF_LONG
1781
    if (count > ULONG_MAX) {
1782
      count = ULONG_MAX;
1783
    }
1784
#  elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1785
    if (count > ULLONG_MAX) {
1786
      count = ULLONG_MAX;
1787
    }
1788
#  endif
1789

1790
    if (sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, count,
1791
        NULL, &len, 0) == -1) {
1792

1793
# elif defined(HAVE_MACOSX_SENDFILE)
1794
    off_t orig_len = count;
1795
    int res;
1796

1797
    /* Since Mac OSX uses the fourth argument as a value-return parameter,
1798
     * success or failure, we need to put the result into len after the
1799
     * call.
1800
     */
1801

1802
    res = sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, &orig_len,
1803
      NULL, 0);
1804
    len = orig_len;
1805

1806
    if (res < 0) {
1807
# elif defined(HAVE_AIX_SENDFILE)
1808

1809
    memset(&parms, 0, sizeof(parms));
1810

1811
    parms.file_descriptor = retr_fd;
1812
    parms.file_offset = (uint64_t) *offset;
1813
    parms.file_bytes = (int64_t) count;
1814

1815
    rc = send_file(&(PR_NETIO_FD(session.d->outstrm)), &(parms), (uint_t)0);
1816
    len = (int) parms.bytes_sent;
1817

1818
    if (rc < -1 || rc == 1) {
1819
# else
1820
    if (FALSE) {
1821
# endif /* HAVE_AIX_SENDFILE */
1822

1823
      /* IMO, BSD's semantics are warped.  Apparently, since we have our
1824
       * alarms tagged SA_INTERRUPT (allowing system calls to be
1825
       * interrupted - primarily for select), BSD will interrupt a
1826
       * sendfile operation as well, so we have to catch and handle this
1827
       * case specially.  It should also be noted that the sendfile(2) man
1828
       * page doesn't state any of this.
1829
       *
1830
       * HP/UX has the same semantics, however, EINTR is well documented
1831
       * as a side effect in the sendfile(2) man page.  HP/UX, however,
1832
       * is implemented horribly wrong.  If a signal would result in
1833
       * -1 being returned and EINTR being set, what ACTUALLY happens is
1834
       * that errno is cleared and the number of bytes written is returned.
1835
       *
1836
       * For obvious reasons, HP/UX sendfile is not supported yet.
1837
       */
1838
      if (errno == EINTR) {
1✔
1839
        if (XFER_ABORTED) {
×
1840
          session.xfer.total_bytes += len;
×
1841
          session.total_bytes += len;
×
1842
          session.total_bytes_out += len;
×
1843
          session.total_raw_out += len;
×
1844

1845
          return -1;
×
1846
        }
1847

1848
        pr_signals_handle();
×
1849

1850
        /* If we got everything in this transaction, we're done. */
1851
        if (len >= count) {
×
1852
          break;
1853
        }
1854

1855
        count -= len;
×
1856
        *offset += len;
×
1857

1858
        if (timeout_stalled) {
×
1859
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1860
        }
1861

1862
        if (timeout_idle) {
×
1863
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1864
        }
1865

1866
        session.xfer.total_bytes += len;
×
1867
        session.total_bytes += len;
×
1868
        session.total_bytes_out += len;
×
1869
        session.total_raw_out += len;
×
1870
        total += len;
×
1871

1872
        continue;
×
1873
      }
1874

1875
      error = errno;
1✔
1876
      (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
1✔
1877
      errno = error;
1✔
1878

1879
      return -1;
1✔
1880
    }
1881

1882
    break;
1883
  }
1884

1885
  if (flags & O_NONBLOCK) {
1✔
1886
    (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
×
1887
  }
1888

1889
  if (timeout_stalled) {
1✔
1890
    pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
1✔
1891
  }
1892

1893
  if (timeout_idle) {
1✔
1894
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
1✔
1895
  }
1896

1897
  session.xfer.total_bytes += len;
1✔
1898
  session.total_bytes += len;
1✔
1899
  session.total_bytes_out += len;
1✔
1900
  session.total_raw_out += len;
1✔
1901
  total += len;
1✔
1902

1903
  return total;
1✔
1904
}
1905
#else
1906
pr_sendfile_t pr_data_sendfile(int retr_fd, off_t *offset, off_t count) {
1907
  errno = ENOSYS;
1908
  return -1;
1909
}
1910
#endif /* HAVE_SENDFILE */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc