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

proftpd / proftpd / 17105437559

20 Aug 2025 05:13PM UTC coverage: 92.624% (-0.4%) from 93.027%
17105437559

push

github

web-flow
Check for excessive attribute length when handling RADIUS packet MACs, just as we do for other packet attributes. (#1973)

47217 of 50977 relevant lines covered (92.62%)

212.88 hits per line

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

72.84
/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;
112
  int rev, xerrno = 0;
3✔
113

114
  if (reason == NULL &&
6✔
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,
4✔
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,
2✔
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;
215
  config_rec *c;
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 &&
14✔
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,
8✔
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,
4✔
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;
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;
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 &&
10✔
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;
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
  struct sigaction act;
538

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

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

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

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

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

570
  } else {
571
    session.xfer.direction = direction;
6✔
572
  }
573

574
  if (reason == NULL) {
11✔
575
    reason = filename;
11✔
576
  }
577

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

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

588
  if (res < 0) {
11✔
589
    return res;
590
  }
591

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

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

600
    errno = xerrno;
×
601
    return -1;
×
602
  }
603

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

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

612
    errno = xerrno;
×
613
    return -1;
×
614
  }
615

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

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

622
  } else {
623
    nstrm = session.d->outstrm;
×
624
  }
625

626
  session.sf_flags |= SF_XFER;
×
627

628
  if (timeout_noxfer) {
×
629
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
×
630
  }
631

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

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

644
  act.sa_handler = data_urgent;
×
645
  sigemptyset(&act.sa_mask);
×
646
  act.sa_flags = 0;
647
#ifdef SA_INTERRUPT
648
  act.sa_flags |= SA_INTERRUPT;
×
649
#endif
650

651
  if (sigaction(SIGURG, &act, NULL) < 0) {
×
652
    pr_log_pri(PR_LOG_WARNING,
×
653
      "warning: unable to set SIGURG signal handler: %s", strerror(errno));
×
654
  }
655

656
#ifdef HAVE_SIGINTERRUPT
657
  /* This is the BSD way of ensuring interruption.
658
   * Linux uses it too (??)
659
   */
660
  if (siginterrupt(SIGURG, 1) < 0) {
×
661
    pr_log_pri(PR_LOG_WARNING,
×
662
      "warning: unable to make SIGURG interrupt system calls: %s",
663
      strerror(errno));
×
664
  }
665
#endif
666

667
  /* Reset all of the timing-related variables for data transfers. */
668
  pr_gettimeofday_millis(&data_start_ms);
×
669
  data_first_byte_read = FALSE;
×
670
  data_first_byte_written = FALSE;
×
671

672
  return res;
×
673
}
674

675
void pr_data_close2(void) {
12✔
676
  nstrm = NULL;
12✔
677

678
  if (session.d != NULL) {
12✔
679
    pr_inet_lingering_close(session.pool, session.d, timeout_linger);
10✔
680
    session.d = NULL;
10✔
681
  }
682

683
  /* Aborts no longer necessary */
684
  signal(SIGURG, SIG_IGN);
12✔
685

686
  if (timeout_noxfer) {
12✔
687
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
12✔
688
  }
689

690
  if (timeout_stalled) {
12✔
691
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
12✔
692
  }
693

694
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
12✔
695
  session.sf_flags &= (SF_ALL^(SF_ABORT|SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
12✔
696
  pr_session_set_idle();
12✔
697
}
12✔
698

699
/* close == successful transfer */
700
void pr_data_close(int quiet) {
3✔
701
  pr_data_close2();
3✔
702

703
  if (quiet == FALSE) {
3✔
704
    pr_response_add(R_226, _("Transfer complete"));
1✔
705
  }
706
}
3✔
707

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

724
  pr_data_clear_xfer_pool();
2✔
725

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

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

741
  pr_trace_msg(trace_channel, 9,
11✔
742
    "aborting data transfer (errno = %s (%d), quiet = %s, true abort = %s)",
743
    strerror(err), err, quiet ? "true" : "false",
744
    true_abort ? "true" : "false");
745

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

753
    } else {
754
      pr_inet_lingering_close(session.pool, session.d, timeout_linger);
1✔
755
    }
756

757
    session.d = NULL;
9✔
758
  }
759

760
  if (timeout_noxfer) {
11✔
761
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
11✔
762
  }
763

764
  if (timeout_stalled) {
11✔
765
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
11✔
766
  }
767

768
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
11✔
769
  session.sf_flags &= (SF_ALL^(SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
11✔
770
  pr_session_set_idle();
11✔
771

772
  if (!quiet) {
11✔
773
    char *respcode = R_426;
10✔
774
    char *msg = NULL;
10✔
775
    char msgbuf[64];
776

777
    switch (err) {
10✔
778

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

793
#ifdef ENXIO
794
    case ENXIO:
1✔
795
      respcode = R_451;
1✔
796
      msg = _("Unexpected streams hangup");
1✔
797
      break;
1✔
798
#endif
799

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

812
#ifdef ETXTBSY
813
    case ETXTBSY:                /* FALLTHROUGH */
814
#endif
815
#ifdef EBUSY
816
    case EBUSY:
817
#endif
818
#if defined(ETXTBSY) || defined(EBUSY)
819
      respcode = R_451;
820
      break;
821
#endif
822

823
#ifdef ENOSPC
824
    case ENOSPC:
825
      respcode = R_452;
826
      break;
827
#endif
828

829
#ifdef EDQUOT
830
    case EDQUOT:                /* FALLTHROUGH */
831
#endif
832
#ifdef EFBIG
833
    case EFBIG:
834
#endif
835
#if defined(EDQUOT) || defined(EFBIG)
836
      respcode = R_552;
837
      break;
838
#endif
839

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

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

909
    default:
2✔
910
      respcode = R_426;
2✔
911
      msg = _("Data connection closed");
2✔
912
      break;
2✔
913
    }
914

915
    if (msg == NULL &&
6✔
916
        (msg = strerror(err)) == NULL) {
917

918
      if (pr_snprintf(msgbuf, sizeof(msgbuf),
×
919
          _("Unknown or out of range errno [%d]"), err) > 0) {
×
920
        msg = msgbuf;
×
921
      }
922
    }
923

924
    pr_log_pri(PR_LOG_NOTICE, "notice: user %s: aborting transfer: %s",
10✔
925
      session.user ? session.user : "(unknown)", msg);
10✔
926

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

935
    /* Forcibly clear the data-transfer instigating command pool from the
936
     * Response API.
937
     */
938
    pr_response_set_pool(session.pool);
10✔
939
  }
940

941
  if (true_abort) {
11✔
942
    session.sf_flags |= SF_POST_ABORT;
8✔
943
  }
944
}
11✔
945

946
/* From response.c.  XXX Need to provide these symbols another way. */
947
extern pr_response_t *resp_list, *resp_err_list;
948

949
static int peek_is_abor_cmd(void) {
14✔
950
  int fd, res;
951
  fd_set rfds;
952
  struct timeval tv;
953
  ssize_t len;
954
  char buf[5];
955

956
  tv.tv_sec = 1;
14✔
957
  tv.tv_usec = 0;
14✔
958

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

962
  FD_ZERO(&rfds);
14✔
963
  FD_SET(fd, &rfds);
14✔
964

965
  res = select(fd + 1, &rfds, NULL, NULL, &tv);
14✔
966
  while (res < 0) {
14✔
967
    int xerrno = errno;
×
968

969
    if (xerrno == EINTR) {
×
970
      pr_signals_handle();
×
971
      res = select(fd + 1, &rfds, NULL, NULL, &tv);
×
972
      continue;
×
973
    }
974

975
    pr_trace_msg(trace_channel, 20,
×
976
      "error waiting for next data on fd %d: %s", fd, strerror(xerrno));
977
    return FALSE;
×
978
  }
979

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

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

998
    if (xerrno == EINTR) {
1✔
999
      pr_signals_handle();
×
1000
      len = recv(fd, &buf, sizeof(buf), MSG_PEEK|MSG_WAITALL);
×
1001
      continue;
×
1002
    }
1003

1004
    pr_trace_msg(trace_channel, 20,
1✔
1005
      "error peeking at next data: %s", strerror(xerrno));
1006
    return FALSE;
1✔
1007
  }
1008

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

1016
  pr_trace_msg(trace_channel, 20, "peeked data '%.*s' not ABOR command",
×
1017
    (int) len, buf);
1018
  return FALSE;
×
1019
}
1020

1021
static void poll_ctrl(void) {
26✔
1022
  int res;
1023

1024
  if (session.c == NULL) {
26✔
1025
    return;
1026
  }
1027

1028
  pr_trace_msg(trace_channel, 4, "polling for commands on control channel");
14✔
1029
  pr_netio_set_poll_interval(session.c->instrm, 0);
14✔
1030
  res = pr_netio_poll(session.c->instrm);
14✔
1031
  pr_netio_reset_poll_interval(session.c->instrm);
14✔
1032

1033
  if (res == 0 &&
28✔
1034
      !(session.sf_flags & SF_ABORT)) {
14✔
1035

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

1060
      session.sf_flags |= SF_ABORT;
×
1061

1062
      if (nstrm != NULL) {
×
1063
        pr_netio_abort(nstrm);
×
1064
        errno = 0;
×
1065
      }
1066
    }
1067
  }
1068

1069
  if (res == 0 &&
28✔
1070
      !(session.sf_flags & SF_ABORT)) {
14✔
1071
    cmd_rec *cmd = NULL;
14✔
1072

1073
    pr_trace_msg(trace_channel, 1,
14✔
1074
      "data available for reading on control channel during data transfer, "
1075
      "reading control data");
1076
    res = pr_cmd_read(&cmd);
14✔
1077
    if (res < 0) {
14✔
1078
      int xerrno;
1079

1080
#if defined(ECONNABORTED)
1081
      xerrno = ECONNABORTED;
×
1082
#elif defined(ENOTCONN)
1083
      xerrno = ENOTCONN;
1084
#else
1085
      xerrno = EIO;
1086
#endif
1087

1088
      pr_trace_msg(trace_channel, 1,
×
1089
        "unable to read control command during data transfer: %s",
1090
        strerror(xerrno));
1091
      errno = xerrno;
×
1092

1093
#ifndef PR_DEVEL_NO_DAEMON
1094
      /* Otherwise, EOF */
1095
      pr_session_disconnect(NULL, PR_SESS_DISCONNECT_CLIENT_EOF, NULL);
×
1096
#else
1097
      return;
1098
#endif /* PR_DEVEL_NO_DAEMON */
1099

1100
    } else if (cmd != NULL) {
14✔
1101
      char *ch;
1102

1103
      for (ch = cmd->argv[0]; *ch; ch++) {
35✔
1104
        *ch = toupper((int) *ch);
56✔
1105
      }
1106

1107
      cmd->cmd_id = pr_cmd_get_id(cmd->argv[0]);
7✔
1108

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

1134
        pr_trace_msg(trace_channel, 5,
2✔
1135
          "client sent '%s' command during data transfer, denying",
1136
          (char *) cmd->argv[0]);
2✔
1137

1138
        resp_list = resp_err_list = NULL;
2✔
1139
        resp_pool = pr_response_get_pool();
2✔
1140

1141
        pr_response_set_pool(cmd->pool);
2✔
1142

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

1146
        pr_response_flush(&resp_err_list);
2✔
1147

1148
        pr_response_set_pool(resp_pool);
2✔
1149
        destroy_pool(cmd->pool);
2✔
1150

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

1160
        pr_trace_msg(trace_channel, 5,
2✔
1161
          "client sent '%s' command during data transfer, ignoring",
1162
          (char *) cmd->argv[0]);
2✔
1163

1164
        resp_list = resp_err_list = NULL;
2✔
1165
        resp_pool = pr_response_get_pool();
2✔
1166

1167
        pr_response_set_pool(cmd->pool);
2✔
1168

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

1172
        pr_response_flush(&resp_list);
2✔
1173

1174
        pr_response_set_pool(resp_pool);
2✔
1175
        destroy_pool(cmd->pool);
2✔
1176

1177
      } else {
1178
        char *title_buf = NULL;
3✔
1179
        int curr_cmd_id = 0, title_len = -1;
3✔
1180
        const char *curr_cmd = NULL, *sce_cmd = NULL, *sce_cmd_arg = NULL;
3✔
1181
        cmd_rec *curr_cmd_rec = NULL;
3✔
1182
        pool *resp_pool;
1183

1184
        pr_trace_msg(trace_channel, 5,
3✔
1185
          "client sent '%s' command during data transfer, dispatching",
1186
          (char *) cmd->argv[0]);
3✔
1187

1188
        title_len = pr_proctitle_get(NULL, 0);
3✔
1189
        if (title_len > 0) {
3✔
1190
          title_buf = pcalloc(cmd->pool, title_len + 1);
×
1191
          pr_proctitle_get(title_buf, title_len + 1);
×
1192
        }
1193

1194
        curr_cmd = session.curr_cmd;
3✔
1195
        curr_cmd_id = session.curr_cmd_id;
3✔
1196
        curr_cmd_rec = session.curr_cmd_rec;
3✔
1197
        sce_cmd = pr_scoreboard_entry_get(PR_SCORE_CMD);
3✔
1198
        sce_cmd_arg = pr_scoreboard_entry_get(PR_SCORE_CMD_ARG);
3✔
1199

1200
        resp_list = resp_err_list = NULL;
3✔
1201
        resp_pool = pr_response_get_pool();
3✔
1202

1203
        pr_response_set_pool(cmd->pool);
3✔
1204
        pr_cmd_dispatch(cmd);
3✔
1205

1206
        pr_scoreboard_entry_update(session.pid,
3✔
1207
          PR_SCORE_CMD, "%s", sce_cmd, NULL, NULL);
1208
        pr_scoreboard_entry_update(session.pid,
3✔
1209
          PR_SCORE_CMD_ARG, "%s", sce_cmd_arg, NULL, NULL);
1210

1211
        if (title_len > 0) {
3✔
1212
          pr_proctitle_set_str(title_buf);
×
1213
        }
1214

1215
        pr_response_flush(&resp_list);
3✔
1216
        pr_response_set_pool(resp_pool);
3✔
1217
        destroy_pool(cmd->pool);
3✔
1218

1219
        session.curr_cmd = curr_cmd;
3✔
1220
        session.curr_cmd_id = curr_cmd_id;
3✔
1221
        session.curr_cmd_rec = curr_cmd_rec;
3✔
1222
      }
1223

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

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

1245
  if (cl_buf == NULL ||
68✔
1246
      cl_size == 0) {
34✔
1247
    errno = EINVAL;
8✔
1248
    return -1;
8✔
1249
  }
1250

1251
  /* Poll the control channel for any commands we should handle, like
1252
   * QUIT or ABOR.
1253
   */
1254
  poll_ctrl();
26✔
1255

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

1262
#if defined(ECONNABORTED)
1263
    xerrno = ECONNABORTED;
4✔
1264
#elif defined(ENOTCONN)
1265
    xerrno = ENOTCONN;
1266
#else
1267
    xerrno = EIO;
1268
#endif
1269

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

1277
    errno = xerrno;
4✔
1278
    return -1;
4✔
1279
  }
1280

1281
  if (session.xfer.direction == PR_NETIO_IO_RD) {
22✔
1282
    char *buf;
1283

1284
    buf = session.xfer.buf;
12✔
1285

1286
    /* We use ASCII translation if:
1287
     *
1288
     * - SF_ASCII session flag is set, AND IGNORE_ASCII data opt NOT set
1289
     */
1290
    if (((session.sf_flags & SF_ASCII) &&
19✔
1291
        !(data_opts & PR_DATA_OPT_IGNORE_ASCII))) {
7✔
1292
      int adjlen, buflen;
1293

1294
      do {
1295
        buflen = session.xfer.buflen;        /* how much remains in buf */
7✔
1296
        adjlen = 0;
7✔
1297

1298
        pr_signals_handle();
7✔
1299

1300
        len = pr_netio_read(session.d->instrm, buf + buflen,
7✔
1301
          session.xfer.bufsize - buflen, 1);
7✔
1302
        while (len < 0) {
7✔
1303
          int xerrno = errno;
1✔
1304

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

1313
            len = pr_netio_read(session.d->instrm, buf + buflen,
×
1314
              session.xfer.bufsize - buflen, 1);
×
1315
            continue;
×
1316
          }
1317

1318
          destroy_pool(tmp_pool);
1✔
1319
          errno = xerrno;
1✔
1320
          return -1;
1✔
1321
        }
1322

1323
        if (len > 0 &&
12✔
1324
            data_first_byte_read == FALSE) {
6✔
1325
          if (pr_trace_get_level(timing_channel)) {
2✔
1326
            unsigned long elapsed_ms;
1327
            uint64_t read_ms;
1328

1329
            pr_gettimeofday_millis(&read_ms);
2✔
1330
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1331

1332
            pr_trace_msg(timing_channel, 7,
2✔
1333
              "Time for first data byte read: %lu ms", elapsed_ms);
1334
          }
1335

1336
          data_first_byte_read = TRUE;
2✔
1337
        }
1338

1339
        if (len > 0) {
6✔
1340
          pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
6✔
1341
            len != 1 ? "bytes" : "byte");
1342

1343
          buflen += len;
6✔
1344

1345
          if (timeout_stalled) {
6✔
1346
            pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
6✔
1347
          }
1348
        }
1349

1350
        /* If buflen > 0, data remains in the buffer to be copied. */
1351
        if (len >= 0 &&
6✔
1352
            buflen > 0) {
6✔
1353

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

1370
            if (tmp_pool == NULL) {
6✔
1371
              tmp_pool = make_sub_pool(session.xfer.p);
6✔
1372
              pr_pool_tag(tmp_pool, "ASCII upload");
6✔
1373
            }
1374

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

1380
            } else {
1381
              adjlen += res;
6✔
1382
              buflen = (int) outlen;
6✔
1383
            }
1384
          }
1385

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

1395
          memcpy(cl_buf, buf, buflen);
12✔
1396

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

1404
          if (adjlen > 0) {
6✔
1405
            memcpy(buf, buf + buflen, adjlen);
1✔
1406
          }
1407

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

1420
      /* Return how much data we actually copied into the client buffer. */
1421
      len = buflen;
1422

1423
    } else {
1424
      len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
5✔
1425
      while (len < 0) {
10✔
1426
        int xerrno = errno;
1✔
1427

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

1436
          len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
×
1437
          continue;
×
1438
        }
1439

1440
        break;
1441
      }
1442

1443
      if (len > 0) {
5✔
1444
        pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
4✔
1445
          len != 1 ? "bytes" : "byte");
1446

1447
        if (data_first_byte_read == FALSE) {
4✔
1448
          if (pr_trace_get_level(timing_channel)) {
2✔
1449
            unsigned long elapsed_ms;
1450
            uint64_t read_ms;
1451

1452
            pr_gettimeofday_millis(&read_ms);
2✔
1453
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1454

1455
            pr_trace_msg(timing_channel, 7,
2✔
1456
              "Time for first data byte read: %lu ms", elapsed_ms);
1457
          }
1458

1459
          data_first_byte_read = TRUE;
2✔
1460
        }
1461

1462
        /* Non-ASCII mode doesn't need to use session.xfer.buf */
1463
        if (timeout_stalled) {
4✔
1464
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
4✔
1465
        }
1466

1467
        total += len;
1468
      }
1469
    }
1470

1471
  } else { /* PR_NETIO_IO_WR */
1472

1473
    while (cl_size) {
18✔
1474
      int bwrote = 0;
10✔
1475
      int buflen = cl_size;
10✔
1476
      unsigned int xferbuflen;
1477
      char *xferbuf = NULL, *ascii_buf = NULL;
10✔
1478

1479
      pr_signals_handle();
10✔
1480

1481
      if (buflen > pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR)) {
10✔
1482
        buflen = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR);
×
1483
      }
1484

1485
      xferbuf = cl_buf;
10✔
1486
      xferbuflen = buflen;
10✔
1487

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

1499
        if (tmp_pool == NULL) {
7✔
1500
          tmp_pool = make_sub_pool(session.xfer.p);
7✔
1501
          pr_pool_tag(tmp_pool, "ASCII download");
7✔
1502
        }
1503

1504
        /* Fill up our internal buffer. */
1505
        memcpy(session.xfer.buf, cl_buf, buflen);
14✔
1506

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

1518
        } else {
1519
          ascii_buf = session.xfer.buf;
7✔
1520
          session.xfer.buf = out;
7✔
1521
          session.xfer.buflen = xferbuflen = outlen;
7✔
1522
        }
1523

1524
        xferbuf = session.xfer.buf;
7✔
1525
      }
1526

1527
      bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
10✔
1528
      while (bwrote < 0) {
10✔
1529
        int xerrno = errno;
4✔
1530

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

1540
          bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
2✔
1541
          continue;
2✔
1542
        }
1543

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

1551
        errno = xerrno;
2✔
1552
        return -1;
2✔
1553
      }
1554

1555
      if (bwrote > 0) {
8✔
1556
        pr_trace_msg(trace_channel, 19, "wrote %d %s to network", bwrote,
8✔
1557
          bwrote != 1 ? "bytes" : "byte");
1558

1559
        if (data_first_byte_written == FALSE) {
8✔
1560
          if (pr_trace_get_level(timing_channel)) {
3✔
1561
            unsigned long elapsed_ms;
1562
            uint64_t write_ms;
1563

1564
            pr_gettimeofday_millis(&write_ms);
3✔
1565
            elapsed_ms = (unsigned long) (write_ms - data_start_ms);
3✔
1566

1567
            pr_trace_msg(timing_channel, 7,
3✔
1568
              "Time for first data byte written: %lu ms", elapsed_ms);
1569
          }
1570

1571
          data_first_byte_written = TRUE;
3✔
1572
        }
1573

1574
        if (timeout_stalled) {
8✔
1575
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
8✔
1576
        }
1577

1578
        cl_size -= buflen;
8✔
1579
        cl_buf += buflen;
8✔
1580
        total += buflen;
8✔
1581
      }
1582

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

1592
    len = total;
1593
  }
1594

1595
  if (total &&
19✔
1596
      timeout_idle) {
1597
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
18✔
1598
  }
1599

1600
  session.xfer.total_bytes += total;
19✔
1601
  session.total_bytes += total;
19✔
1602
  if (session.xfer.direction == PR_NETIO_IO_RD) {
19✔
1603
    session.total_bytes_in += total;
11✔
1604

1605
  } else {
1606
    session.total_bytes_out += total;
8✔
1607
  }
1608

1609
  destroy_pool(tmp_pool);
19✔
1610
  return (len < 0 ? -1 : len);
19✔
1611
}
1612

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

1626
  if (offset == NULL ||
14✔
1627
      count == 0) {
7✔
1628
    errno = EINVAL;
3✔
1629
    return -1;
3✔
1630
  }
1631

1632
  if (session.xfer.direction == PR_NETIO_IO_RD) {
4✔
1633
    errno = EPERM;
1✔
1634
    return -1;
1✔
1635
  }
1636

1637
  if (session.d == NULL) {
3✔
1638
    errno = EPERM;
1✔
1639
    return -1;
1✔
1640
  }
1641

1642
  flags = fcntl(PR_NETIO_FD(session.d->outstrm), F_GETFL);
2✔
1643
  if (flags < 0) {
2✔
1644
    return -1;
1645
  }
1646

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

1654
  for (;;) {
1655
# if defined(HAVE_LINUX_SENDFILE) || defined(HAVE_SOLARIS_SENDFILE)
1656
    off_t orig_offset = *offset;
2✔
1657

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

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

1693
    errno = 0;
2✔
1694
    len = sendfile(PR_NETIO_FD(session.d->outstrm), retr_fd, offset, count);
2✔
1695

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

1705
      /* Handle our interrupting signal, and continue. */
1706
      pr_signals_handle();
×
1707
    }
1708

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

1717
        session.xfer.total_bytes += len;
×
1718
        session.total_bytes += len;
×
1719
        session.total_bytes_out += len;
×
1720
        session.total_raw_out += len;
×
1721

1722
        return -1;
×
1723
      }
1724

1725
      count -= len;
×
1726

1727
      /* Only reset the timers if data have actually been written out. */
1728
      if (len > 0) {
×
1729
        if (timeout_stalled) {
×
1730
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1731
        }
1732

1733
        if (timeout_idle) {
×
1734
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1735
        }
1736
      }
1737

1738
      session.xfer.total_bytes += len;
×
1739
      session.total_bytes += len;
×
1740
      session.total_bytes_out += len;
×
1741
      session.total_raw_out += len;
×
1742
      total += len;
×
1743

1744
      pr_signals_handle();
×
1745
      continue;
×
1746
    }
1747

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

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

1772
#  if SIZEOF_SIZE_T == SIZEOF_INT
1773
    if (count > UINT_MAX) {
1774
      count = UINT_MAX;
1775
    }
1776
#  elif SIZEOF_SIZE_T == SIZEOF_LONG
1777
    if (count > ULONG_MAX) {
1778
      count = ULONG_MAX;
1779
    }
1780
#  elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1781
    if (count > ULLONG_MAX) {
1782
      count = ULLONG_MAX;
1783
    }
1784
#  endif
1785

1786
    if (sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, count,
1787
        NULL, &len, 0) == -1) {
1788

1789
# elif defined(HAVE_MACOSX_SENDFILE)
1790
    off_t orig_len = count;
1791
    int res;
1792

1793
    /* Since Mac OSX uses the fourth argument as a value-return parameter,
1794
     * success or failure, we need to put the result into len after the
1795
     * call.
1796
     */
1797

1798
    res = sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, &orig_len,
1799
      NULL, 0);
1800
    len = orig_len;
1801

1802
    if (res < 0) {
1803
# elif defined(HAVE_AIX_SENDFILE)
1804

1805
    memset(&parms, 0, sizeof(parms));
1806

1807
    parms.file_descriptor = retr_fd;
1808
    parms.file_offset = (uint64_t) *offset;
1809
    parms.file_bytes = (int64_t) count;
1810

1811
    rc = send_file(&(PR_NETIO_FD(session.d->outstrm)), &(parms), (uint_t)0);
1812
    len = (int) parms.bytes_sent;
1813

1814
    if (rc < -1 || rc == 1) {
1815
# else
1816
    if (FALSE) {
1817
# endif /* HAVE_AIX_SENDFILE */
1818

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

1841
          return -1;
×
1842
        }
1843

1844
        pr_signals_handle();
×
1845

1846
        /* If we got everything in this transaction, we're done. */
1847
        if (len >= count) {
×
1848
          break;
1849
        }
1850

1851
        count -= len;
×
1852
        *offset += len;
×
1853

1854
        if (timeout_stalled) {
×
1855
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1856
        }
1857

1858
        if (timeout_idle) {
×
1859
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1860
        }
1861

1862
        session.xfer.total_bytes += len;
×
1863
        session.total_bytes += len;
×
1864
        session.total_bytes_out += len;
×
1865
        session.total_raw_out += len;
×
1866
        total += len;
×
1867

1868
        continue;
×
1869
      }
1870

1871
      error = errno;
1✔
1872
      (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
1✔
1873
      errno = error;
1✔
1874

1875
      return -1;
1✔
1876
    }
1877

1878
    break;
1879
  }
1880

1881
  if (flags & O_NONBLOCK) {
1✔
1882
    (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
×
1883
  }
1884

1885
  if (timeout_stalled) {
1✔
1886
    pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
1✔
1887
  }
1888

1889
  if (timeout_idle) {
1✔
1890
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
1✔
1891
  }
1892

1893
  session.xfer.total_bytes += len;
1✔
1894
  session.total_bytes += len;
1✔
1895
  session.total_bytes_out += len;
1✔
1896
  session.total_raw_out += len;
1✔
1897
  total += len;
1✔
1898

1899
  return total;
1✔
1900
}
1901
#else
1902
pr_sendfile_t pr_data_sendfile(int retr_fd, off_t *offset, off_t count) {
1903
  errno = ENOSYS;
1904
  return -1;
1905
}
1906
#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