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

proftpd / proftpd / 14581522647

21 Apr 2025 09:13PM UTC coverage: 92.667% (-0.4%) from 93.03%
14581522647

push

github

Castaglia
Adding a regression test for Bug#4417.

47172 of 50905 relevant lines covered (92.67%)

250.83 hits per line

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

72.88
/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 *c;
215
  int bind_port, rev, *root_revoke = NULL, xerrno;
8✔
216
  const pr_netaddr_t *bind_addr = NULL;
8✔
217

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

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

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

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

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

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

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

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

272
      default:
273
        break;
274
    }
275
  }
276

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

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

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

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

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

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

310
  rev = pr_netaddr_set_reverse_dns(ServerUseReverseDNS);
6✔
311

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

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

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

325
  /* Make sure that the necessary socket options are set on the socket prior
326
   * to the call to connect(2).
327
   */
328
  pr_inet_set_proto_opts(session.pool, session.d, main_server->tcp_mss_len, 0,
6✔
329
    IPTOS_THROUGHPUT, 1);
330
  pr_inet_generate_socket_event("core.data-connect", main_server,
6✔
331
    session.d->local_addr, session.d->listen_fd);
6✔
332

333
  if (pr_inet_connect(session.d->pool, session.d, &session.data_addr,
6✔
334
      session.data_port) < 0) {
6✔
335
    xerrno = session.d->xerrno;
6✔
336

337
    pr_log_debug(DEBUG6,
12✔
338
      "Error connecting to %s#%u for active data transfer: %s",
339
      pr_netaddr_get_ipstr(&session.data_addr), session.data_port,
6✔
340
      strerror(xerrno));
341
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
6✔
342
      strerror(xerrno));
343
    pr_data_close2();
6✔
344

345
    errno = xerrno;
6✔
346
    return -1;
6✔
347
  }
348

349
  c = pr_inet_openrw(session.pool, session.d, NULL, PR_NETIO_STRM_DATA,
×
350
    session.d->listen_fd, -1, -1, TRUE);
×
351

352
  pr_netaddr_set_reverse_dns(rev);
×
353

354
  if (c) {
×
355
    pr_log_debug(DEBUG4, "active data connection opened - local  : %s:%d",
×
356
      pr_netaddr_get_ipstr(session.d->local_addr), session.d->local_port);
×
357
    pr_log_debug(DEBUG4, "active data connection opened - remote : %s:%d",
×
358
      pr_netaddr_get_ipstr(session.d->remote_addr), session.d->remote_port);
×
359

360
    if (session.xfer.xfer_type != STOR_UNIQUE) {
×
361
      if (size) {
×
362
        pr_response_send(R_150, _("Opening %s mode data connection for %s "
×
363
          "(%" PR_LU " %s)"), MODE_STRING, reason, (pr_off_t) size,
×
364
          size != 1 ? "bytes" : "byte");
365

366
      } else {
367
        pr_response_send(R_150, _("Opening %s mode data connection for %s"),
×
368
          MODE_STRING, reason);
×
369
      }
370

371
    } else {
372

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

395
    pr_inet_close(session.pool, session.d);
×
396
    (void) pr_inet_set_nonblock(session.pool, session.d);
×
397
    session.d = c;
×
398
    return 0;
×
399
  }
400

401
  pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
402
    strerror(session.d->xerrno));
×
403
  xerrno = session.d->xerrno;
×
404
  pr_data_close2();
×
405

406
  errno = xerrno;
×
407
  return -1;
×
408
}
409

410
void pr_data_set_linger(long linger) {
1✔
411
  timeout_linger = linger;
1✔
412
}
1✔
413

414
int pr_data_get_timeout(int id) {
7✔
415
  switch (id) {
7✔
416
    case PR_DATA_TIMEOUT_IDLE:
2✔
417
      return timeout_idle;
2✔
418

419
    case PR_DATA_TIMEOUT_NO_TRANSFER:
2✔
420
      return timeout_noxfer;
2✔
421

422
    case PR_DATA_TIMEOUT_STALLED:
2✔
423
      return timeout_stalled;
2✔
424
  }
425

426
  errno = EINVAL;
1✔
427
  return -1;
1✔
428
}
429

430
void pr_data_set_timeout(int id, int timeout) {
3✔
431
  switch (id) {
3✔
432
    case PR_DATA_TIMEOUT_IDLE:
1✔
433
      timeout_idle = timeout;
1✔
434
      break;
1✔
435

436
    case PR_DATA_TIMEOUT_NO_TRANSFER:
1✔
437
      timeout_noxfer = timeout;
1✔
438
      break;
1✔
439

440
    case PR_DATA_TIMEOUT_STALLED:
1✔
441
      timeout_stalled = timeout;
1✔
442
      break;
1✔
443
  }
444
}
3✔
445

446
void pr_data_clear_xfer_pool(void) {
19✔
447
  int xfer_type;
448

449
  if (session.xfer.p != NULL) {
19✔
450
    destroy_pool(session.xfer.p);
2✔
451
  }
452

453
  /* Note that session.xfer.xfer_type may have been set already, e.g.
454
   * for STOR_UNIQUE uploads.  To support this, we need to preserve that
455
   * value.
456
   */
457
  xfer_type = session.xfer.xfer_type;
19✔
458

459
  memset(&session.xfer, 0, sizeof(session.xfer));
19✔
460
  session.xfer.xfer_type = xfer_type;
19✔
461
}
19✔
462

463
void pr_data_reset(void) {
9✔
464
  /* Clear any leftover state from previous transfers. */
465
  pr_ascii_ftp_reset();
9✔
466

467
  if (session.d != NULL &&
10✔
468
      session.d->pool != NULL) {
1✔
469
    destroy_pool(session.d->pool);
1✔
470
  }
471

472
  session.d = NULL;
9✔
473
  session.sf_flags &= (SF_ALL^(SF_ABORT|SF_POST_ABORT|SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE|SF_EPSV_ALL));
9✔
474
}
9✔
475

476
int pr_data_ignore_ascii(int ignore_ascii) {
5✔
477
  int res;
478

479
  if (ignore_ascii != TRUE &&
5✔
480
      ignore_ascii != FALSE) {
481
    errno = EINVAL;
1✔
482
    return -1;
1✔
483
  }
484

485
  if (data_opts & PR_DATA_OPT_IGNORE_ASCII) {
4✔
486
    if (!ignore_ascii) {
2✔
487
      data_opts &= ~PR_DATA_OPT_IGNORE_ASCII;
1✔
488
    }
489

490
    res = TRUE;
491

492
  } else {
493
    if (ignore_ascii) {
2✔
494
      data_opts |= PR_DATA_OPT_IGNORE_ASCII;
1✔
495
    }
496

497
    res = FALSE;
498
  }
499

500
  return res;
501
}
502

503
void pr_data_init(char *filename, int direction) {
3✔
504
  if (session.xfer.p == NULL) {
3✔
505
    data_new_xfer(filename, direction);
2✔
506

507
  } else {
508
    if (!(session.sf_flags & SF_PASSIVE)) {
1✔
509
      pr_log_debug(DEBUG5,
1✔
510
        "data_init oddity: session.xfer exists in non-PASV mode");
511
    }
512

513
    session.xfer.direction = direction;
1✔
514
  }
515

516
  /* Clear any leftover state from previous transfers. */
517
  pr_ascii_ftp_reset();
3✔
518
}
3✔
519

520
int pr_data_open(char *filename, char *reason, int direction, off_t size) {
15✔
521
  int res = 0;
15✔
522
  struct sigaction act;
523

524
  if (session.c == NULL) {
15✔
525
    errno = EINVAL;
2✔
526
    return -1;
2✔
527
  }
528

529
  if ((session.sf_flags & SF_PASSIVE) ||
22✔
530
      (session.sf_flags & SF_EPSV_ALL)) {
9✔
531
    /* For passive transfers, we expect there to already be an existing
532
     * data connection...
533
     */
534
    if (session.d == NULL) {
4✔
535
      errno = EINVAL;
1✔
536
      return -1;
1✔
537
    }
538

539
  } else {
540
    /* ...but for active transfers, we expect there to NOT be an existing
541
     * data connection.
542
     */
543
    if (session.d != NULL) {
9✔
544
      errno = session.d->xerrno = EINVAL;
1✔
545
      return -1;
1✔
546
    }
547
  }
548

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

552
  if (session.xfer.p == NULL) {
11✔
553
    data_new_xfer(filename, direction);
5✔
554

555
  } else {
556
    session.xfer.direction = direction;
6✔
557
  }
558

559
  if (reason == NULL) {
11✔
560
    reason = filename;
11✔
561
  }
562

563
  /* Passive data transfers... */
564
  if ((session.sf_flags & SF_PASSIVE) ||
19✔
565
      (session.sf_flags & SF_EPSV_ALL)) {
8✔
566
    res = data_passive_open(reason, size);
3✔
567

568
  /* Active data transfers... */
569
  } else {
570
    res = data_active_open(reason, size);
8✔
571
  }
572

573
  if (res < 0) {
11✔
574
    return res;
575
  }
576

577
  if (pr_netio_postopen(session.d->instrm) < 0) {
×
578
    int xerrno;
579

580
    pr_response_add_err(R_425, _("Unable to build data connection: %s"),
×
581
      strerror(session.d->xerrno));
×
582
    xerrno = session.d->xerrno;
×
583
    pr_data_close2();
×
584

585
    errno = xerrno;
×
586
    return -1;
×
587
  }
588

589
  if (pr_netio_postopen(session.d->outstrm) < 0) {
×
590
    int xerrno;
591

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

597
    errno = xerrno;
×
598
    return -1;
×
599
  }
600

601
  memset(&session.xfer.start_time, '\0', sizeof(session.xfer.start_time));
×
602
  gettimeofday(&session.xfer.start_time, NULL);
×
603

604
  if (session.xfer.direction == PR_NETIO_IO_RD) {
×
605
    nstrm = session.d->instrm;
×
606

607
  } else {
608
    nstrm = session.d->outstrm;
×
609
  }
610

611
  session.sf_flags |= SF_XFER;
×
612

613
  if (timeout_noxfer) {
×
614
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
×
615
  }
616

617
  /* Allow aborts -- set the current NetIO stream to allow interrupted
618
   * syscalls, so our SIGURG handler can interrupt it
619
   */
620
  pr_netio_set_poll_interval(nstrm, 1);
×
621

622
  /* PORTABILITY: sigaction is used here to allow us to indicate
623
   * (w/ POSIX at least) that we want SIGURG to interrupt syscalls.  Put
624
   * in whatever is necessary for your arch here; probably not necessary
625
   * as the only _important_ interrupted syscall is select(), which on
626
   * any sensible system is interrupted.
627
   */
628

629
  act.sa_handler = data_urgent;
×
630
  sigemptyset(&act.sa_mask);
×
631
  act.sa_flags = 0;
632
#ifdef SA_INTERRUPT
633
  act.sa_flags |= SA_INTERRUPT;
×
634
#endif
635

636
  if (sigaction(SIGURG, &act, NULL) < 0) {
×
637
    pr_log_pri(PR_LOG_WARNING,
×
638
      "warning: unable to set SIGURG signal handler: %s", strerror(errno));
×
639
  }
640

641
#ifdef HAVE_SIGINTERRUPT
642
  /* This is the BSD way of ensuring interruption.
643
   * Linux uses it too (??)
644
   */
645
  if (siginterrupt(SIGURG, 1) < 0) {
×
646
    pr_log_pri(PR_LOG_WARNING,
×
647
      "warning: unable to make SIGURG interrupt system calls: %s",
648
      strerror(errno));
×
649
  }
650
#endif
651

652
  /* Reset all of the timing-related variables for data transfers. */
653
  pr_gettimeofday_millis(&data_start_ms);
×
654
  data_first_byte_read = FALSE;
×
655
  data_first_byte_written = FALSE;
×
656

657
  return res;
×
658
}
659

660
void pr_data_close2(void) {
12✔
661
  nstrm = NULL;
12✔
662

663
  if (session.d != NULL) {
12✔
664
    pr_inet_lingering_close(session.pool, session.d, timeout_linger);
10✔
665
    session.d = NULL;
10✔
666
  }
667

668
  /* Aborts no longer necessary */
669
  signal(SIGURG, SIG_IGN);
12✔
670

671
  if (timeout_noxfer) {
12✔
672
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
12✔
673
  }
674

675
  if (timeout_stalled) {
12✔
676
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
12✔
677
  }
678

679
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
12✔
680
  session.sf_flags &= (SF_ALL^(SF_ABORT|SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
12✔
681
  pr_session_set_idle();
12✔
682
}
12✔
683

684
/* close == successful transfer */
685
void pr_data_close(int quiet) {
3✔
686
  pr_data_close2();
3✔
687

688
  if (quiet == FALSE) {
3✔
689
    pr_response_add(R_226, _("Transfer complete"));
1✔
690
  }
691
}
3✔
692

693
/* Note: true_abort may be false in real abort situations, because
694
 * some ftp clients close the data connection at the same time as they
695
 * send the OOB byte (which results in a broken pipe on our
696
 * end).  Thus, it's a race between the OOB data and the tcp close
697
 * finishing.  Either way, it's ok (client will see either "Broken pipe"
698
 * error or "Aborted").  xfer_abor() in mod_xfer cleans up the session
699
 * flags in any case.  session flags will end up have SF_POST_ABORT
700
 * set if the OOB byte won the race.
701
 */
702
void pr_data_cleanup(void) {
2✔
703
  /* sanity check */
704
  if (session.d != NULL) {
2✔
705
    pr_inet_lingering_close(session.pool, session.d, timeout_linger);
1✔
706
    session.d = NULL;
1✔
707
  }
708

709
  pr_data_clear_xfer_pool();
2✔
710

711
  /* Clear/restore the default data transfer type. Otherwise, things like
712
   * APPEs or STOUs will be preserved for the next upload erroneously
713
   * (see Bug#3612).
714
   */
715
  session.xfer.xfer_type = STOR_DEFAULT;
2✔
716
}
2✔
717

718
/* In order to avoid clearing the transfer counters in session.xfer, we don't
719
 * clear session.xfer here, it should be handled by the appropriate
720
 * LOG_CMD/LOG_CMD_ERR handler calling pr_data_cleanup().
721
 */
722
void pr_data_abort(int err, int quiet) {
11✔
723
  int true_abort = XFER_ABORTED;
11✔
724
  nstrm = NULL;
11✔
725

726
  pr_trace_msg(trace_channel, 9,
11✔
727
    "aborting data transfer (errno = %s (%d), quiet = %s, true abort = %s)",
728
    strerror(err), err, quiet ? "true" : "false",
729
    true_abort ? "true" : "false");
730

731
  if (session.d != NULL) {
11✔
732
    if (true_abort) {
9✔
733
      /* For "true" aborts, we also asynchronously send a 426 response
734
       * message via the "lingering abort" functions.
735
       */
736
      pr_inet_lingering_abort(session.pool, session.d, timeout_linger);
8✔
737

738
    } else {
739
      pr_inet_lingering_close(session.pool, session.d, timeout_linger);
1✔
740
    }
741

742
    session.d = NULL;
9✔
743
  }
744

745
  if (timeout_noxfer) {
11✔
746
    pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
11✔
747
  }
748

749
  if (timeout_stalled) {
11✔
750
    pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
11✔
751
  }
752

753
  session.sf_flags &= (SF_ALL^SF_PASSIVE);
11✔
754
  session.sf_flags &= (SF_ALL^(SF_XFER|SF_PASSIVE|SF_ASCII_OVERRIDE));
11✔
755
  pr_session_set_idle();
11✔
756

757
  if (!quiet) {
11✔
758
    char *respcode = R_426;
10✔
759
    char *msg = NULL;
10✔
760
    char msgbuf[64];
761

762
    switch (err) {
10✔
763

764
    case 0:
2✔
765
#ifdef ECONNABORTED
766
    case ECONNABORTED:        /* FALLTHROUGH */
767
#endif
768
#ifdef ECONNRESET
769
    case ECONNRESET:        /* FALLTHROUGH */
770
#endif
771
#ifdef EPIPE
772
    case EPIPE:
773
#endif
774
      respcode = R_426;
2✔
775
      msg = _("Data connection closed");
2✔
776
      break;
2✔
777

778
#ifdef ENXIO
779
    case ENXIO:
1✔
780
      respcode = R_451;
1✔
781
      msg = _("Unexpected streams hangup");
1✔
782
      break;
1✔
783
#endif
784

785
#ifdef EAGAIN
786
    case EAGAIN:                /* FALLTHROUGH */
1✔
787
#endif
788
#ifdef ENOMEM
789
    case ENOMEM:
790
#endif
791
#if defined(EAGAIN) || defined(ENOMEM)
792
      respcode = R_451;
1✔
793
      msg = _("Insufficient memory or file locked");
1✔
794
      break;
1✔
795
#endif
796

797
#ifdef ETXTBSY
798
    case ETXTBSY:                /* FALLTHROUGH */
799
#endif
800
#ifdef EBUSY
801
    case EBUSY:
802
#endif
803
#if defined(ETXTBSY) || defined(EBUSY)
804
      respcode = R_451;
805
      break;
806
#endif
807

808
#ifdef ENOSPC
809
    case ENOSPC:
810
      respcode = R_452;
811
      break;
812
#endif
813

814
#ifdef EDQUOT
815
    case EDQUOT:                /* FALLTHROUGH */
816
#endif
817
#ifdef EFBIG
818
    case EFBIG:
819
#endif
820
#if defined(EDQUOT) || defined(EFBIG)
821
      respcode = R_552;
822
      break;
823
#endif
824

825
#ifdef ECOMM
826
    case ECOMM:                /* FALLTHROUGH */
827
#endif
828
#ifdef EDEADLK
829
    case EDEADLK:                /* FALLTHROUGH */
830
#endif
831
#ifdef EDEADLOCK
832
# if !defined(EDEADLK) || (EDEADLOCK != EDEADLK)
833
    case EDEADLOCK:                /* FALLTHROUGH */
834
# endif
835
#endif
836
#ifdef EXFULL
837
    case EXFULL:                /* FALLTHROUGH */
838
#endif
839
#ifdef ENOSR
840
    case ENOSR:                /* FALLTHROUGH */
841
#endif
842
#ifdef EPROTO
843
    case EPROTO:                /* FALLTHROUGH */
844
#endif
845
#ifdef ETIME
846
    case ETIME:                /* FALLTHROUGH */
847
#endif
848
#ifdef EIO
849
    case EIO:                /* FALLTHROUGH */
850
#endif
851
#ifdef EFAULT
852
    case EFAULT:                /* FALLTHROUGH */
853
#endif
854
#ifdef ESPIPE
855
    case ESPIPE:                /* FALLTHROUGH */
856
#endif
857
#if defined(ECOMM) || defined(EDEADLK) ||  defined(EDEADLOCK) || \
858
    defined(EXFULL) || defined(ENOSR) || defined(EPROTO) || \
859
    defined(ETIME) || defined(EIO) || defined(EFAULT) || \
860
    defined(ESPIPE) || defined(EPIPE)
861
      respcode = R_451;
862
      break;
863
#endif
864

865
#ifdef EREMCHG
866
    case EREMCHG:                /* FALLTHROUGH */
×
867
#endif
868
#ifdef ESRMNT
869
    case ESRMNT:                /* FALLTHROUGH */
870
#endif
871
#ifdef ESTALE
872
    case ESTALE:                /* FALLTHROUGH */
873
#endif
874
#ifdef ENOLINK
875
    case ENOLINK:                /* FALLTHROUGH */
876
#endif
877
#ifdef ENOLCK
878
    case ENOLCK:                /* FALLTHROUGH */
879
#endif
880
#ifdef ENETRESET
881
    case ENETRESET:                /* FALLTHROUGH */
882
#endif
883
#ifdef ETIMEDOUT
884
    case ETIMEDOUT:
885
#endif
886
#if defined(EREMCHG) || defined(ESRMNT) ||  defined(ESTALE) || \
887
    defined(ENOLINK) || defined(ENOLCK) || defined(ENETRESET) || \
888
    defined(ECONNABORTED) || defined(ECONNRESET) || defined(ETIMEDOUT)
889
      respcode = R_450;
×
890
      msg = _("Link to file server lost");
×
891
      break;
×
892
#endif
893
    }
894

895
    if (msg == NULL &&
4✔
896
        (msg = strerror(err)) == NULL) {
897

898
      if (pr_snprintf(msgbuf, sizeof(msgbuf),
×
899
          _("Unknown or out of range errno [%d]"), err) > 0) {
×
900
        msg = msgbuf;
×
901
      }
902
    }
903

904
    pr_log_pri(PR_LOG_NOTICE, "notice: user %s: aborting transfer: %s",
10✔
905
      session.user ? session.user : "(unknown)", msg);
10✔
906

907
    /* If we are aborting, then a 426 response has already been sent via
908
     * pr_inet_lingering_abort(), and we don't want to add another to the
909
     * error queue.
910
     */
911
    if (true_abort == FALSE) {
10✔
912
      pr_response_add_err(respcode, _("Transfer aborted. %s"), msg ? msg : "");
2✔
913
    }
914

915
    /* Forcibly clear the data-transfer instigating command pool from the
916
     * Response API.
917
     */
918
    pr_response_set_pool(session.pool);
10✔
919
  }
920

921
  if (true_abort) {
11✔
922
    session.sf_flags |= SF_POST_ABORT;
8✔
923
  }
924
}
11✔
925

926
/* From response.c.  XXX Need to provide these symbols another way. */
927
extern pr_response_t *resp_list, *resp_err_list;
928

929
static int peek_is_abor_cmd(void) {
14✔
930
  int fd, res;
931
  fd_set rfds;
932
  struct timeval tv;
933
  ssize_t len;
934
  char buf[5];
935

936
  tv.tv_sec = 1;
14✔
937
  tv.tv_usec = 0;
14✔
938

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

942
  FD_ZERO(&rfds);
14✔
943
  FD_SET(fd, &rfds);
14✔
944

945
  res = select(fd + 1, &rfds, NULL, NULL, &tv);
14✔
946
  while (res < 0) {
14✔
947
    int xerrno = errno;
×
948

949
    if (xerrno == EINTR) {
×
950
      pr_signals_handle();
×
951
      res = select(fd + 1, &rfds, NULL, NULL, &tv);
×
952
      continue;
×
953
    }
954

955
    pr_trace_msg(trace_channel, 20,
×
956
      "error waiting for next data on fd %d: %s", fd, strerror(xerrno));
957
    return FALSE;
×
958
  }
959

960
  if (res == 0) {
14✔
961
    /* Timed out. */
962
    pr_trace_msg(trace_channel, 20, "timed out peeking for data on fd %d", fd);
13✔
963
    return FALSE;
13✔
964
  }
965

966
  /* If we reach here, the peer must have sent something.  Let's see what it
967
   * might be.  Chances are that we received at least 5 bytes, but to be
968
   * defensive, we use MSG_WAITALL anyway.  TCP allows for sending one byte
969
   * at time, if need be.  The shortest FTP command is 5 bytes, e.g. "CCC\r\n".
970
   * ABOR would be 6 bytes, but we do not want to block until we see 6 bytes;
971
   * we're peeking opportunistically, and optimistically.
972
   */
973
  memset(&buf, 0, sizeof(buf));
1✔
974
  len = recv(fd, buf, sizeof(buf), MSG_PEEK|MSG_WAITALL);
1✔
975
  while (len < 0) {
1✔
976
    int xerrno = errno;
1✔
977

978
    if (xerrno == EINTR) {
1✔
979
      pr_signals_handle();
×
980
      len = recv(fd, &buf, sizeof(buf), MSG_PEEK|MSG_WAITALL);
×
981
      continue;
×
982
    }
983

984
    pr_trace_msg(trace_channel, 20,
1✔
985
      "error peeking at next data: %s", strerror(xerrno));
986
    return FALSE;
1✔
987
  }
988

989
  pr_trace_msg(trace_channel, 20, "peeking at %ld bytes of next data",
×
990
    (long) len);
991
  if (strncasecmp(buf, "ABOR\r", len) == 0) {
×
992
    pr_trace_msg(trace_channel, 20, "peeked data probably 'ABOR' command");
×
993
    return TRUE;
×
994
  }
995

996
  pr_trace_msg(trace_channel, 20, "peeked data '%.*s' not ABOR command",
×
997
    (int) len, buf);
998
  return FALSE;
×
999
}
1000

1001
static void poll_ctrl(void) {
26✔
1002
  int res;
1003

1004
  if (session.c == NULL) {
26✔
1005
    return;
1006
  }
1007

1008
  pr_trace_msg(trace_channel, 4, "polling for commands on control channel");
14✔
1009
  pr_netio_set_poll_interval(session.c->instrm, 0);
14✔
1010
  res = pr_netio_poll(session.c->instrm);
14✔
1011
  pr_netio_reset_poll_interval(session.c->instrm);
14✔
1012

1013
  if (res == 0 &&
28✔
1014
      !(session.sf_flags & SF_ABORT)) {
14✔
1015

1016
    /* First, we peek at the data, to see if it is an ABOR command.  Why?
1017
     *
1018
     * Consider the case where a client uses the TCP OOB mechanism and
1019
     * marks the ABOR command with the marker.  In that case, a SIGURG signal
1020
     * will have been raised, and the SF_ABORT flag set.  The actual "ABOR"
1021
     * data on the control connection will be read only AFTER the data transfer
1022
     * has been failed.  This leads the proper ordering of multiple responses
1023
     * (first for failed transfer, second for successful ABOR) in such cases.
1024
     *
1025
     * Now consider the case where a client does NOT use the TCP OOB mechanism,
1026
     * and only sends the ABOR command.  We want the same behavior in this case
1027
     * as for the TCP OOB case, BUT now, the SF_ABORT flag has NOT been set at
1028
     * this point in the flow.  Which means that we might read that "ABOR" text
1029
     * from the control connection _in the middle_ of the data transfer, which
1030
     * leads to different behavior, different ordering of responses.
1031
     *
1032
     * Thus we cheat here, and only peek at the control connection data.  IFF
1033
     * it is the "ABOR\r\n" text, then we set the SF_ABORT flag ourselves
1034
     * here, and preserve the expected semantics (Bug #4402).
1035
     */
1036
    if (peek_is_abor_cmd() == TRUE) {
14✔
1037
      pr_trace_msg(trace_channel, 5, "client sent 'ABOR' command during data "
×
1038
        "transfer, setting 'aborted' session flag");
1039

1040
      session.sf_flags |= SF_ABORT;
×
1041

1042
      if (nstrm != NULL) {
×
1043
        pr_netio_abort(nstrm);
×
1044
        errno = 0;
×
1045
      }
1046
    }
1047
  }
1048

1049
  if (res == 0 &&
28✔
1050
      !(session.sf_flags & SF_ABORT)) {
14✔
1051
    cmd_rec *cmd = NULL;
14✔
1052

1053
    pr_trace_msg(trace_channel, 1,
14✔
1054
      "data available for reading on control channel during data transfer, "
1055
      "reading control data");
1056
    res = pr_cmd_read(&cmd);
14✔
1057
    if (res < 0) {
14✔
1058
      int xerrno;
1059

1060
#if defined(ECONNABORTED)
1061
      xerrno = ECONNABORTED;
×
1062
#elif defined(ENOTCONN)
1063
      xerrno = ENOTCONN;
1064
#else
1065
      xerrno = EIO;
1066
#endif
1067

1068
      pr_trace_msg(trace_channel, 1,
×
1069
        "unable to read control command during data transfer: %s",
1070
        strerror(xerrno));
1071
      errno = xerrno;
×
1072

1073
#ifndef PR_DEVEL_NO_DAEMON
1074
      /* Otherwise, EOF */
1075
      pr_session_disconnect(NULL, PR_SESS_DISCONNECT_CLIENT_EOF, NULL);
×
1076
#else
1077
      return;
1078
#endif /* PR_DEVEL_NO_DAEMON */
1079

1080
    } else if (cmd != NULL) {
14✔
1081
      char *ch;
1082

1083
      for (ch = cmd->argv[0]; *ch; ch++) {
35✔
1084
        *ch = toupper((int) *ch);
56✔
1085
      }
1086

1087
      cmd->cmd_id = pr_cmd_get_id(cmd->argv[0]);
7✔
1088

1089
      /* Only handle commands which do not involve data transfers; we
1090
       * already have a data transfer in progress.  For any data transfer
1091
       * command, send a 450 ("busy") reply.  Looks like almost all of the
1092
       * data transfer commands accept that response, as per RFC959.
1093
       *
1094
       * We also prevent the EPRT, EPSV, PASV, and PORT commands, since
1095
       * they will also interfere with the current data transfer.  In doing
1096
       * so, we break RFC compliance a little; RFC959 does not allow a
1097
       * response code of 450 for those commands (although it should).
1098
       */
1099
      if (pr_cmd_cmp(cmd, PR_CMD_APPE_ID) == 0 ||
14✔
1100
          pr_cmd_cmp(cmd, PR_CMD_LIST_ID) == 0 ||
14✔
1101
          pr_cmd_cmp(cmd, PR_CMD_MLSD_ID) == 0 ||
14✔
1102
          pr_cmd_cmp(cmd, PR_CMD_NLST_ID) == 0 ||
14✔
1103
          pr_cmd_cmp(cmd, PR_CMD_RETR_ID) == 0 ||
14✔
1104
          pr_cmd_cmp(cmd, PR_CMD_STOR_ID) == 0 ||
14✔
1105
          pr_cmd_cmp(cmd, PR_CMD_STOU_ID) == 0 ||
14✔
1106
          pr_cmd_cmp(cmd, PR_CMD_RNFR_ID) == 0 ||
14✔
1107
          pr_cmd_cmp(cmd, PR_CMD_RNTO_ID) == 0 ||
14✔
1108
          pr_cmd_cmp(cmd, PR_CMD_PORT_ID) == 0 ||
14✔
1109
          pr_cmd_cmp(cmd, PR_CMD_EPRT_ID) == 0 ||
14✔
1110
          pr_cmd_cmp(cmd, PR_CMD_PASV_ID) == 0 ||
12✔
1111
          pr_cmd_cmp(cmd, PR_CMD_EPSV_ID) == 0) {
7✔
1112
        pool *resp_pool;
1113

1114
        pr_trace_msg(trace_channel, 5,
2✔
1115
          "client sent '%s' command during data transfer, denying",
1116
          (char *) cmd->argv[0]);
2✔
1117

1118
        resp_list = resp_err_list = NULL;
2✔
1119
        resp_pool = pr_response_get_pool();
2✔
1120

1121
        pr_response_set_pool(cmd->pool);
2✔
1122

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

1126
        pr_response_flush(&resp_err_list);
2✔
1127

1128
        pr_response_set_pool(resp_pool);
2✔
1129
        destroy_pool(cmd->pool);
2✔
1130

1131
      /* We don't want to actually dispatch the NOOP command, since that
1132
       * would overwrite the scoreboard with the NOOP state; admins probably
1133
       * want to see the command that caused the data transfer.  And since
1134
       * NOOP doesn't take a 450 response (as per RFC959), we will simply
1135
       * return 200.
1136
       */
1137
      } else if (pr_cmd_cmp(cmd, PR_CMD_NOOP_ID) == 0) {
5✔
1138
        pool *resp_pool;
1139

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

1144
        resp_list = resp_err_list = NULL;
2✔
1145
        resp_pool = pr_response_get_pool();
2✔
1146

1147
        pr_response_set_pool(cmd->pool);
2✔
1148

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

1152
        pr_response_flush(&resp_list);
2✔
1153

1154
        pr_response_set_pool(resp_pool);
2✔
1155
        destroy_pool(cmd->pool);
2✔
1156

1157
      } else {
1158
        char *title_buf = NULL;
3✔
1159
        int curr_cmd_id = 0, title_len = -1;
3✔
1160
        const char *curr_cmd = NULL, *sce_cmd = NULL, *sce_cmd_arg = NULL;
3✔
1161
        cmd_rec *curr_cmd_rec = NULL;
3✔
1162
        pool *resp_pool;
1163

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

1168
        title_len = pr_proctitle_get(NULL, 0);
3✔
1169
        if (title_len > 0) {
3✔
1170
          title_buf = pcalloc(cmd->pool, title_len + 1);
×
1171
          pr_proctitle_get(title_buf, title_len + 1);
×
1172
        }
1173

1174
        curr_cmd = session.curr_cmd;
3✔
1175
        curr_cmd_id = session.curr_cmd_id;
3✔
1176
        curr_cmd_rec = session.curr_cmd_rec;
3✔
1177
        sce_cmd = pr_scoreboard_entry_get(PR_SCORE_CMD);
3✔
1178
        sce_cmd_arg = pr_scoreboard_entry_get(PR_SCORE_CMD_ARG);
3✔
1179

1180
        resp_list = resp_err_list = NULL;
3✔
1181
        resp_pool = pr_response_get_pool();
3✔
1182

1183
        pr_response_set_pool(cmd->pool);
3✔
1184
        pr_cmd_dispatch(cmd);
3✔
1185

1186
        pr_scoreboard_entry_update(session.pid,
3✔
1187
          PR_SCORE_CMD, "%s", sce_cmd, NULL, NULL);
1188
        pr_scoreboard_entry_update(session.pid,
3✔
1189
          PR_SCORE_CMD_ARG, "%s", sce_cmd_arg, NULL, NULL);
1190

1191
        if (title_len > 0) {
3✔
1192
          pr_proctitle_set_str(title_buf);
×
1193
        }
1194

1195
        pr_response_flush(&resp_list);
3✔
1196
        pr_response_set_pool(resp_pool);
3✔
1197
        destroy_pool(cmd->pool);
3✔
1198

1199
        session.curr_cmd = curr_cmd;
3✔
1200
        session.curr_cmd_id = curr_cmd_id;
3✔
1201
        session.curr_cmd_rec = curr_cmd_rec;
3✔
1202
      }
1203

1204
    } else {
1205
      pr_trace_msg(trace_channel, 3,
7✔
1206
        "invalid command sent, sending error response");
1207
      pr_response_send(R_500, _("Invalid command: try being more creative"));
7✔
1208
    }
1209
  }
1210
}
1211

1212
/* pr_data_xfer() actually transfers the data on the data connection.  ASCII
1213
 * translation is performed if necessary.  `direction' is set when the data
1214
 * connection was opened.
1215
 *
1216
 * We determine if the client buffer is read from or written to.  Returns 0 if
1217
 * reading and data connection closes, or -1 if error (with errno set).
1218
 */
1219
int pr_data_xfer(char *cl_buf, size_t cl_size) {
34✔
1220
  int len = 0;
34✔
1221
  int total = 0;
34✔
1222
  int res = 0;
34✔
1223
  pool *tmp_pool = NULL;
34✔
1224

1225
  if (cl_buf == NULL ||
68✔
1226
      cl_size == 0) {
34✔
1227
    errno = EINVAL;
8✔
1228
    return -1;
8✔
1229
  }
1230

1231
  /* Poll the control channel for any commands we should handle, like
1232
   * QUIT or ABOR.
1233
   */
1234
  poll_ctrl();
26✔
1235

1236
  /* If we don't have a data connection here (e.g. might have been closed
1237
   * by an ABOR), then return zero (no data transferred).
1238
   */
1239
  if (session.d == NULL) {
26✔
1240
    int xerrno;
1241

1242
#if defined(ECONNABORTED)
1243
    xerrno = ECONNABORTED;
4✔
1244
#elif defined(ENOTCONN)
1245
    xerrno = ENOTCONN;
1246
#else
1247
    xerrno = EIO;
1248
#endif
1249

1250
    pr_trace_msg(trace_channel, 1,
4✔
1251
      "data connection is null prior to data transfer (possibly from "
1252
      "aborted transfer), returning '%s' error", strerror(xerrno));
1253
    pr_log_debug(DEBUG5,
4✔
1254
      "data connection is null prior to data transfer (possibly from "
1255
       "aborted transfer), returning '%s' error", strerror(xerrno));
1256

1257
    errno = xerrno;
4✔
1258
    return -1;
4✔
1259
  }
1260

1261
  if (session.xfer.direction == PR_NETIO_IO_RD) {
22✔
1262
    char *buf;
1263

1264
    buf = session.xfer.buf;
12✔
1265

1266
    /* We use ASCII translation if:
1267
     *
1268
     * - SF_ASCII session flag is set, AND IGNORE_ASCII data opt NOT set
1269
     */
1270
    if (((session.sf_flags & SF_ASCII) &&
19✔
1271
        !(data_opts & PR_DATA_OPT_IGNORE_ASCII))) {
7✔
1272
      int adjlen, buflen;
1273

1274
      do {
1275
        buflen = session.xfer.buflen;        /* how much remains in buf */
7✔
1276
        adjlen = 0;
7✔
1277

1278
        pr_signals_handle();
7✔
1279

1280
        len = pr_netio_read(session.d->instrm, buf + buflen,
7✔
1281
          session.xfer.bufsize - buflen, 1);
7✔
1282
        while (len < 0) {
7✔
1283
          int xerrno = errno;
1✔
1284

1285
          if (xerrno == EAGAIN || xerrno == EINTR) {
1✔
1286
            /* Since our socket is in non-blocking mode, read(2) can return
1287
             * EAGAIN if there is no data yet for us.  Handle this by
1288
             * delaying temporarily, then trying again.
1289
             */
1290
            errno = EINTR;
×
1291
            pr_signals_handle();
×
1292

1293
            len = pr_netio_read(session.d->instrm, buf + buflen,
×
1294
              session.xfer.bufsize - buflen, 1);
×
1295
            continue;
×
1296
          }
1297

1298
          destroy_pool(tmp_pool);
1✔
1299
          errno = xerrno;
1✔
1300
          return -1;
1✔
1301
        }
1302

1303
        if (len > 0 &&
12✔
1304
            data_first_byte_read == FALSE) {
6✔
1305
          if (pr_trace_get_level(timing_channel)) {
2✔
1306
            unsigned long elapsed_ms;
1307
            uint64_t read_ms;
1308

1309
            pr_gettimeofday_millis(&read_ms);
2✔
1310
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1311

1312
            pr_trace_msg(timing_channel, 7,
2✔
1313
              "Time for first data byte read: %lu ms", elapsed_ms);
1314
          }
1315

1316
          data_first_byte_read = TRUE;
2✔
1317
        }
1318

1319
        if (len > 0) {
6✔
1320
          pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
6✔
1321
            len != 1 ? "bytes" : "byte");
1322

1323
          buflen += len;
6✔
1324

1325
          if (timeout_stalled) {
6✔
1326
            pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
6✔
1327
          }
1328
        }
1329

1330
        /* If buflen > 0, data remains in the buffer to be copied. */
1331
        if (len >= 0 &&
6✔
1332
            buflen > 0) {
6✔
1333

1334
          /* Perform ASCII translation:
1335
           *
1336
           * buflen: is returned as the modified buffer length after
1337
           *         translation
1338
           * res:    is returned as the number of characters unprocessed in
1339
           *         the buffer (to be dealt with later)
1340
           *
1341
           * We skip the call to pr_ascii_ftp_from_crlf() in one case:
1342
           * when we have one character in the buffer and have reached
1343
           * end of data, this is so that pr_ascii_ftp_from_crlf() won't sit
1344
           * forever waiting for the next character after a final '\r'.
1345
           */
1346
          if (len > 0 ||
12✔
1347
              buflen > 1) {
6✔
1348
            size_t outlen = 0;
6✔
1349

1350
            if (tmp_pool == NULL) {
6✔
1351
              tmp_pool = make_sub_pool(session.xfer.p);
6✔
1352
              pr_pool_tag(tmp_pool, "ASCII upload");
6✔
1353
            }
1354

1355
            res = pr_ascii_ftp_from_crlf(tmp_pool, buf, buflen, &buf, &outlen);
6✔
1356
            if (res < 0) {
6✔
1357
              pr_trace_msg(trace_channel, 3, "error reading ASCII data: %s",
×
1358
                strerror(errno));
×
1359

1360
            } else {
1361
              adjlen += res;
6✔
1362
              buflen = (int) outlen;
6✔
1363
            }
1364
          }
1365

1366
          /* Now copy everything we can into cl_buf */
1367
          if ((size_t) buflen > cl_size) {
6✔
1368
            /* Because we have to cut our buffer short, make sure this
1369
             * is made up for later by increasing adjlen.
1370
             */
1371
            adjlen += (buflen - cl_size);
×
1372
            buflen = cl_size;
×
1373
          }
1374

1375
          memcpy(cl_buf, buf, buflen);
12✔
1376

1377
          /* Copy whatever remains at the end of session.xfer.buf to the
1378
           * head of the buffer and adjust buf accordingly.
1379
           *
1380
           * adjlen is now the total bytes still waiting in buf, if
1381
           * anything remains, copy it to the start of the buffer.
1382
           */
1383

1384
          if (adjlen > 0) {
6✔
1385
            memcpy(buf, buf + buflen, adjlen);
1✔
1386
          }
1387

1388
          /* Store everything back in session.xfer. */
1389
          session.xfer.buflen = adjlen;
6✔
1390
          total += buflen;
6✔
1391
        }
1392
        
1393
        /* Restart if data was returned by pr_netio_read() (len > 0) but no
1394
         * data was copied to the client buffer (buflen = 0).  This indicates
1395
         * that pr_ascii_ftp_from_crlf() needs more data in order to
1396
         * translate, so we need to call pr_netio_read() again.
1397
         */
1398
      } while (len > 0 && buflen == 0);
6✔
1399

1400
      /* Return how much data we actually copied into the client buffer. */
1401
      len = buflen;
1402

1403
    } else {
1404
      len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
5✔
1405
      while (len < 0) {
10✔
1406
        int xerrno = errno;
1✔
1407

1408
        if (xerrno == EAGAIN || xerrno == EINTR) {
1✔
1409
          /* Since our socket is in non-blocking mode, read(2) can return
1410
           * EAGAIN if there is no data yet for us.  Handle this by
1411
           * delaying temporarily, then trying again.
1412
           */
1413
          errno = EINTR;
×
1414
          pr_signals_handle();
×
1415

1416
          len = pr_netio_read(session.d->instrm, cl_buf, cl_size, 1);
×
1417
          continue;
×
1418
        }
1419

1420
        break;
1421
      }
1422

1423
      if (len > 0) {
5✔
1424
        pr_trace_msg(trace_channel, 19, "read %d %s from network", len,
4✔
1425
          len != 1 ? "bytes" : "byte");
1426

1427
        if (data_first_byte_read == FALSE) {
4✔
1428
          if (pr_trace_get_level(timing_channel)) {
2✔
1429
            unsigned long elapsed_ms;
1430
            uint64_t read_ms;
1431

1432
            pr_gettimeofday_millis(&read_ms);
2✔
1433
            elapsed_ms = (unsigned long) (read_ms - data_start_ms);
2✔
1434

1435
            pr_trace_msg(timing_channel, 7,
2✔
1436
              "Time for first data byte read: %lu ms", elapsed_ms);
1437
          }
1438

1439
          data_first_byte_read = TRUE;
2✔
1440
        }
1441

1442
        /* Non-ASCII mode doesn't need to use session.xfer.buf */
1443
        if (timeout_stalled) {
4✔
1444
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
4✔
1445
        }
1446

1447
        total += len;
1448
      }
1449
    }
1450

1451
  } else { /* PR_NETIO_IO_WR */
1452

1453
    while (cl_size) {
18✔
1454
      int bwrote = 0;
10✔
1455
      int buflen = cl_size;
10✔
1456
      unsigned int xferbuflen;
1457
      char *xferbuf = NULL, *ascii_buf = NULL;
10✔
1458

1459
      pr_signals_handle();
10✔
1460

1461
      if (buflen > pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR)) {
10✔
1462
        buflen = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR);
×
1463
      }
1464

1465
      xferbuf = cl_buf;
10✔
1466
      xferbuflen = buflen;
10✔
1467

1468
      /* We use ASCII translation if:
1469
       *
1470
       * - SF_ASCII_OVERRIDE session flag is set (e.g. for LIST/NLST)
1471
       * - SF_ASCII session flag is set, AND IGNORE_ASCII data opt NOT set
1472
       */
1473
      if ((session.sf_flags & SF_ASCII_OVERRIDE) ||
14✔
1474
          ((session.sf_flags & SF_ASCII) &&
5✔
1475
           !(data_opts & PR_DATA_OPT_IGNORE_ASCII))) {
1✔
1476
        char *out = NULL;
7✔
1477
        size_t outlen = 0;
7✔
1478

1479
        if (tmp_pool == NULL) {
7✔
1480
          tmp_pool = make_sub_pool(session.xfer.p);
7✔
1481
          pr_pool_tag(tmp_pool, "ASCII download");
7✔
1482
        }
1483

1484
        /* Fill up our internal buffer. */
1485
        memcpy(session.xfer.buf, cl_buf, buflen);
14✔
1486

1487
        /* Scan the internal buffer, looking for LFs with no preceding CRs.
1488
         * Add CRs (and expand the internal buffer) as necessary. xferbuflen
1489
         * will be adjusted so that it contains the length of data in
1490
         * the internal buffer, including any added CRs.
1491
         */
1492
        res = pr_ascii_ftp_to_crlf(tmp_pool, session.xfer.buf, xferbuflen,
7✔
1493
          &out, &outlen);
1494
        if (res < 0) {
7✔
1495
          pr_trace_msg(trace_channel, 1, "error writing ASCII data: %s",
×
1496
            strerror(errno));
×
1497

1498
        } else {
1499
          ascii_buf = session.xfer.buf;
7✔
1500
          session.xfer.buf = out;
7✔
1501
          session.xfer.buflen = xferbuflen = outlen;
7✔
1502
        }
1503

1504
        xferbuf = session.xfer.buf;
7✔
1505
      }
1506

1507
      bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
10✔
1508
      while (bwrote < 0) {
10✔
1509
        int xerrno = errno;
4✔
1510

1511
        if (xerrno == EAGAIN ||
8✔
1512
            xerrno == EINTR) {
4✔
1513
          /* Since our socket may be in non-blocking mode, write(2) can return
1514
           * EAGAIN if there is not enough from for our data yet.  Handle
1515
           * this by delaying temporarily, then trying again.
1516
           */
1517
          errno = EINTR;
2✔
1518
          pr_signals_handle();
2✔
1519

1520
          bwrote = pr_netio_write(session.d->outstrm, xferbuf, xferbuflen);
2✔
1521
          continue;
2✔
1522
        }
1523

1524
        destroy_pool(tmp_pool);
2✔
1525
        if (ascii_buf != NULL) {
2✔
1526
          /* Free up the malloc'd memory. */
1527
          free(session.xfer.buf);
1✔
1528
          session.xfer.buf = ascii_buf;
1✔
1529
        }
1530

1531
        errno = xerrno;
2✔
1532
        return -1;
2✔
1533
      }
1534

1535
      if (bwrote > 0) {
8✔
1536
        pr_trace_msg(trace_channel, 19, "wrote %d %s to network", bwrote,
8✔
1537
          bwrote != 1 ? "bytes" : "byte");
1538

1539
        if (data_first_byte_written == FALSE) {
8✔
1540
          if (pr_trace_get_level(timing_channel)) {
3✔
1541
            unsigned long elapsed_ms;
1542
            uint64_t write_ms;
1543

1544
            pr_gettimeofday_millis(&write_ms);
3✔
1545
            elapsed_ms = (unsigned long) (write_ms - data_start_ms);
3✔
1546

1547
            pr_trace_msg(timing_channel, 7,
3✔
1548
              "Time for first data byte written: %lu ms", elapsed_ms);
1549
          }
1550

1551
          data_first_byte_written = TRUE;
3✔
1552
        }
1553

1554
        if (timeout_stalled) {
8✔
1555
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
8✔
1556
        }
1557

1558
        cl_size -= buflen;
8✔
1559
        cl_buf += buflen;
8✔
1560
        total += buflen;
8✔
1561
      }
1562

1563
      if (ascii_buf != NULL) {
8✔
1564
        /* Yes, we are using malloc et al here, rather than the memory pools.
1565
         * See Bug#4352 for details.
1566
         */
1567
        free(session.xfer.buf);
6✔
1568
        session.xfer.buf = ascii_buf;
6✔
1569
      }
1570
    }
1571

1572
    len = total;
1573
  }
1574

1575
  if (total &&
19✔
1576
      timeout_idle) {
1577
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
18✔
1578
  }
1579

1580
  session.xfer.total_bytes += total;
19✔
1581
  session.total_bytes += total;
19✔
1582
  if (session.xfer.direction == PR_NETIO_IO_RD) {
19✔
1583
    session.total_bytes_in += total;
11✔
1584

1585
  } else {
1586
    session.total_bytes_out += total;
8✔
1587
  }
1588

1589
  destroy_pool(tmp_pool);
19✔
1590
  return (len < 0 ? -1 : len);
19✔
1591
}
1592

1593
#if defined(HAVE_SENDFILE)
1594
/* pr_data_sendfile() actually transfers the data on the data connection.
1595
 * ASCII translation is not performed.
1596
 * return 0 if reading and data connection closes, or -1 if error
1597
 */
1598
pr_sendfile_t pr_data_sendfile(int retr_fd, off_t *offset, off_t count) {
7✔
1599
  int flags, error;
1600
  pr_sendfile_t len = 0, total = 0;
7✔
1601
# if defined(HAVE_AIX_SENDFILE)
1602
  struct sf_parms parms;
1603
  int rc;
1604
# endif /* HAVE_AIX_SENDFILE */
1605

1606
  if (offset == NULL ||
14✔
1607
      count == 0) {
7✔
1608
    errno = EINVAL;
3✔
1609
    return -1;
3✔
1610
  }
1611

1612
  if (session.xfer.direction == PR_NETIO_IO_RD) {
4✔
1613
    errno = EPERM;
1✔
1614
    return -1;
1✔
1615
  }
1616

1617
  if (session.d == NULL) {
3✔
1618
    errno = EPERM;
1✔
1619
    return -1;
1✔
1620
  }
1621

1622
  flags = fcntl(PR_NETIO_FD(session.d->outstrm), F_GETFL);
2✔
1623
  if (flags < 0) {
2✔
1624
    return -1;
1625
  }
1626

1627
  /* Set fd to blocking-mode for sendfile() */
1628
  if (flags & O_NONBLOCK) {
2✔
1629
    if (fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags^O_NONBLOCK) < 0) {
×
1630
      return -1;
1631
    }
1632
  }
1633

1634
  for (;;) {
1635
# if defined(HAVE_LINUX_SENDFILE) || defined(HAVE_SOLARIS_SENDFILE)
1636
    off_t orig_offset = *offset;
2✔
1637

1638
    /* Linux semantics are fairly straightforward in a glibc 2.x world:
1639
     *
1640
     *   #include <sys/sendfile.h>
1641
     *
1642
     *   ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
1643
     *
1644
     * Unfortunately, this API does not allow for an off_t number of bytes
1645
     * to be sent, only a size_t.  This means we need to make sure that
1646
     * the given count does not exceed the maximum value for a size_t.  Even
1647
     * worse, the return value is a ssize_t, not a size_t, which means
1648
     * the maximum value used can only be of the maximum _signed_ value,
1649
     * not the maximum unsigned value.  This means calling sendfile() more
1650
     * times.  How annoying.
1651
     */
1652

1653
#  if defined(HAVE_LINUX_SENDFILE)
1654
    if (count > INT_MAX) {
2✔
1655
      count = INT_MAX;
×
1656
    }
1657
#  elif defined(HAVE_SOLARIS_SENDFILE)
1658
#   if SIZEOF_SIZE_T == SIZEOF_INT
1659
    if (count > INT_MAX) {
1660
      count = INT_MAX;
1661
    }
1662
#   elif SIZEOF_SIZE_T == SIZEOF_LONG
1663
    if (count > LONG_MAX) {
1664
      count = LONG_MAX;
1665
    }
1666
#   elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1667
    if (count > LLONG_MAX) {
1668
      count = LLONG_MAX;
1669
    }
1670
#   endif
1671
#  endif /* !HAVE_SOLARIS_SENDFILE */
1672

1673
    errno = 0;
2✔
1674
    len = sendfile(PR_NETIO_FD(session.d->outstrm), retr_fd, offset, count);
2✔
1675

1676
    /* If no data could be written (e.g. the file was truncated), we're
1677
     * done (Bug#4318).
1678
     */
1679
    if (len == 0) {
2✔
1680
      if (errno != EINTR &&
×
1681
          errno != EAGAIN) {
1682
        break;
1683
      }
1684

1685
      /* Handle our interrupting signal, and continue. */
1686
      pr_signals_handle();
×
1687
    }
1688

1689
    if (len != -1 &&
4✔
1690
        len < count) {
2✔
1691
      /* Under Linux semantics, this occurs when a signal has interrupted
1692
       * sendfile().
1693
       */
1694
      if (XFER_ABORTED) {
×
1695
        errno = EINTR;
×
1696

1697
        session.xfer.total_bytes += len;
×
1698
        session.total_bytes += len;
×
1699
        session.total_bytes_out += len;
×
1700
        session.total_raw_out += len;
×
1701

1702
        return -1;
×
1703
      }
1704

1705
      count -= len;
×
1706

1707
      /* Only reset the timers if data have actually been written out. */
1708
      if (len > 0) {
×
1709
        if (timeout_stalled) {
×
1710
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1711
        }
1712

1713
        if (timeout_idle) {
×
1714
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1715
        }
1716
      }
1717

1718
      session.xfer.total_bytes += len;
×
1719
      session.total_bytes += len;
×
1720
      session.total_bytes_out += len;
×
1721
      session.total_raw_out += len;
×
1722
      total += len;
×
1723

1724
      pr_signals_handle();
×
1725
      continue;
×
1726
    }
1727

1728
    if (len == -1) {
2✔
1729
      /* Linux updates offset on error, not len like BSD, fix up so
1730
       * BSD-based code works.
1731
       */
1732
      len = *offset - orig_offset;
1✔
1733
      *offset = orig_offset;
1✔
1734

1735
# elif defined(HAVE_BSD_SENDFILE)
1736
    /* BSD semantics for sendfile are flexible...it'd be nice if we could
1737
     * standardize on something like it.  The semantics are:
1738
     *
1739
     *   #include <sys/types.h>
1740
     *   #include <sys/socket.h>
1741
     *   #include <sys/uio.h>
1742
     *
1743
     *   int sendfile(int in_fd, int out_fd, off_t offset, size_t count,
1744
     *                struct sf_hdtr *hdtr, off_t *len, int flags)
1745
     *
1746
     *  The comments above, about size_t versus off_t, apply here as
1747
     *  well.  Except that BSD's sendfile() uses an off_t * for returning
1748
     *  the number of bytes sent, so we can use the maximum unsigned
1749
     *  value.
1750
     */
1751

1752
#  if SIZEOF_SIZE_T == SIZEOF_INT
1753
    if (count > UINT_MAX) {
1754
      count = UINT_MAX;
1755
    }
1756
#  elif SIZEOF_SIZE_T == SIZEOF_LONG
1757
    if (count > ULONG_MAX) {
1758
      count = ULONG_MAX;
1759
    }
1760
#  elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1761
    if (count > ULLONG_MAX) {
1762
      count = ULLONG_MAX;
1763
    }
1764
#  endif
1765

1766
    if (sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, count,
1767
        NULL, &len, 0) == -1) {
1768

1769
# elif defined(HAVE_MACOSX_SENDFILE)
1770
    off_t orig_len = count;
1771
    int res;
1772

1773
    /* Since Mac OSX uses the fourth argument as a value-return parameter,
1774
     * success or failure, we need to put the result into len after the
1775
     * call.
1776
     */
1777

1778
    res = sendfile(retr_fd, PR_NETIO_FD(session.d->outstrm), *offset, &orig_len,
1779
      NULL, 0);
1780
    len = orig_len;
1781

1782
    if (res < 0) {
1783
# elif defined(HAVE_AIX_SENDFILE)
1784

1785
    memset(&parms, 0, sizeof(parms));
1786

1787
    parms.file_descriptor = retr_fd;
1788
    parms.file_offset = (uint64_t) *offset;
1789
    parms.file_bytes = (int64_t) count;
1790

1791
    rc = send_file(&(PR_NETIO_FD(session.d->outstrm)), &(parms), (uint_t)0);
1792
    len = (int) parms.bytes_sent;
1793

1794
    if (rc < -1 || rc == 1) {
1795
# else
1796
    if (FALSE) {
1797
# endif /* HAVE_AIX_SENDFILE */
1798

1799
      /* IMO, BSD's semantics are warped.  Apparently, since we have our
1800
       * alarms tagged SA_INTERRUPT (allowing system calls to be
1801
       * interrupted - primarily for select), BSD will interrupt a
1802
       * sendfile operation as well, so we have to catch and handle this
1803
       * case specially.  It should also be noted that the sendfile(2) man
1804
       * page doesn't state any of this.
1805
       *
1806
       * HP/UX has the same semantics, however, EINTR is well documented
1807
       * as a side effect in the sendfile(2) man page.  HP/UX, however,
1808
       * is implemented horribly wrong.  If a signal would result in
1809
       * -1 being returned and EINTR being set, what ACTUALLY happens is
1810
       * that errno is cleared and the number of bytes written is returned.
1811
       *
1812
       * For obvious reasons, HP/UX sendfile is not supported yet.
1813
       */
1814
      if (errno == EINTR) {
1✔
1815
        if (XFER_ABORTED) {
×
1816
          session.xfer.total_bytes += len;
×
1817
          session.total_bytes += len;
×
1818
          session.total_bytes_out += len;
×
1819
          session.total_raw_out += len;
×
1820

1821
          return -1;
×
1822
        }
1823

1824
        pr_signals_handle();
×
1825

1826
        /* If we got everything in this transaction, we're done. */
1827
        if (len >= count) {
×
1828
          break;
1829
        }
1830

1831
        count -= len;
×
1832
        *offset += len;
×
1833

1834
        if (timeout_stalled) {
×
1835
          pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
×
1836
        }
1837

1838
        if (timeout_idle) {
×
1839
          pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
×
1840
        }
1841

1842
        session.xfer.total_bytes += len;
×
1843
        session.total_bytes += len;
×
1844
        session.total_bytes_out += len;
×
1845
        session.total_raw_out += len;
×
1846
        total += len;
×
1847

1848
        continue;
×
1849
      }
1850

1851
      error = errno;
1✔
1852
      (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
1✔
1853
      errno = error;
1✔
1854

1855
      return -1;
1✔
1856
    }
1857

1858
    break;
1859
  }
1860

1861
  if (flags & O_NONBLOCK) {
1✔
1862
    (void) fcntl(PR_NETIO_FD(session.d->outstrm), F_SETFL, flags);
×
1863
  }
1864

1865
  if (timeout_stalled) {
1✔
1866
    pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
1✔
1867
  }
1868

1869
  if (timeout_idle) {
1✔
1870
    pr_timer_reset(PR_TIMER_IDLE, ANY_MODULE);
1✔
1871
  }
1872

1873
  session.xfer.total_bytes += len;
1✔
1874
  session.total_bytes += len;
1✔
1875
  session.total_bytes_out += len;
1✔
1876
  session.total_raw_out += len;
1✔
1877
  total += len;
1✔
1878

1879
  return total;
1✔
1880
}
1881
#else
1882
pr_sendfile_t pr_data_sendfile(int retr_fd, off_t *offset, off_t count) {
1883
  errno = ENOSYS;
1884
  return -1;
1885
}
1886
#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