| 1 |
/* |
| 2 |
Copyright (C) 1994-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* - Ftp Server |
| 20 |
* Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
/* |
| 49 |
* FTP server. |
| 50 |
*/ |
| 51 |
|
| 52 |
#include <config.h> |
| 53 |
|
| 54 |
#include <alloca.h> |
| 55 |
#include <sys/param.h> |
| 56 |
#include <sys/stat.h> |
| 57 |
#include <sys/ioctl.h> |
| 58 |
#include <sys/socket.h> |
| 59 |
#include <sys/wait.h> |
| 60 |
|
| 61 |
#include <netinet/in.h> |
| 62 |
#ifdef HAVE_NETINET_IN_SYSTM_H |
| 63 |
# include <netinet/in_systm.h> |
| 64 |
#endif |
| 65 |
#ifdef HAVE_NETINET_IP_H |
| 66 |
# include <netinet/ip.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#define FTP_NAMES 1 |
| 70 |
#include <arpa/ftp.h> |
| 71 |
#include <arpa/inet.h> |
| 72 |
#include <arpa/telnet.h> |
| 73 |
|
| 74 |
#include <ctype.h> |
| 75 |
#include <dirent.h> |
| 76 |
#include <errno.h> |
| 77 |
#include <fcntl.h> |
| 78 |
#include <limits.h> |
| 79 |
#include <netdb.h> |
| 80 |
#include <setjmp.h> |
| 81 |
#include <signal.h> |
| 82 |
#include <grp.h> |
| 83 |
#include <stdarg.h> |
| 84 |
#include <stdio.h> |
| 85 |
#include <stdlib.h> |
| 86 |
#include <string.h> |
| 87 |
#include <syslog.h> |
| 88 |
#include <sys/time.h> |
| 89 |
#include <time.h> |
| 90 |
#include <unistd.h> |
| 91 |
#ifdef HAVE_MMAP |
| 92 |
# include <sys/mman.h> |
| 93 |
#endif |
| 94 |
/* Include glob.h last, because it may define "const" which breaks |
| 95 |
system headers on some platforms. */ |
| 96 |
#include <glob.h> |
| 97 |
#include <argp.h> |
| 98 |
#include <error.h> |
| 99 |
#include <xgetcwd.h> |
| 100 |
|
| 101 |
#include <progname.h> |
| 102 |
#include <libinetutils.h> |
| 103 |
#include "extern.h" |
| 104 |
#include "attribute.h" |
| 105 |
|
| 106 |
#ifndef LINE_MAX |
| 107 |
# define LINE_MAX 2048 |
| 108 |
#endif |
| 109 |
|
| 110 |
#ifndef LOG_FTP |
| 111 |
# define LOG_FTP LOG_DAEMON /* Use generic facility. */ |
| 112 |
#endif |
| 113 |
|
| 114 |
#ifndef MAP_FAILED |
| 115 |
# define MAP_FAILED (void*)-1 |
| 116 |
#endif |
| 117 |
|
| 118 |
/* Exported to ftpcmd.h. */ |
| 119 |
struct sockaddr_storage data_dest; /* Data port. */ |
| 120 |
socklen_t data_dest_len; |
| 121 |
struct sockaddr_storage his_addr; /* Peer address. */ |
| 122 |
socklen_t his_addrlen; |
| 123 |
int logging; /* Enable log to syslog. */ |
| 124 |
int no_version; /* Don't print version to client. */ |
| 125 |
int type = TYPE_A; /* Default TYPE_A. */ |
| 126 |
int form = FORM_N; /* Default FORM_N. */ |
| 127 |
int debug; /* Enable debug mode if 1. */ |
| 128 |
int rfc2577 = 1; /* Follow suggestions in RFC 2577. */ |
| 129 |
int timeout = 900; /* Timeout after 15 minutes of inactivity. */ |
| 130 |
int maxtimeout = 7200; /* Don't allow idle time to be set |
| 131 |
beyond 2 hours. */ |
| 132 |
int pdata = -1; /* For passive mode. */ |
| 133 |
char *hostname; /* Who we are. */ |
| 134 |
int usedefault = 1; /* For data transfers. */ |
| 135 |
char tmpline[7]; /* Temp buffer use in OOB. */ |
| 136 |
char addrstr[NI_MAXHOST]; /* Host name or address string. */ |
| 137 |
char portstr[8]; /* Numeric port as string. */ |
| 138 |
|
| 139 |
/* Requester credentials. */ |
| 140 |
struct credentials cred; |
| 141 |
|
| 142 |
jmp_buf errcatch; |
| 143 |
|
| 144 |
static struct sockaddr_storage ctrl_addr; /* Control address. */ |
| 145 |
static socklen_t ctrl_addrlen; |
| 146 |
static struct sockaddr_storage data_source; /* Port address. */ |
| 147 |
static socklen_t data_source_len; |
| 148 |
static struct sockaddr_storage pasv_addr; /* Pasv address. */ |
| 149 |
static socklen_t pasv_addrlen; |
| 150 |
|
| 151 |
static int data = -1; /* Port data connection socket. */ |
| 152 |
static jmp_buf urgcatch; |
| 153 |
static int stru = STRU_F; /* Avoid C keyword. */ |
| 154 |
static int stru_mode = MODE_S; /* Default STRU mode stru_mode = MODE_S. */ |
| 155 |
static int anon_only; /* Allow only anonymous login. */ |
| 156 |
static int daemon_mode; /* Start in daemon mode. */ |
| 157 |
static off_t file_size; |
| 158 |
static off_t byte_count; |
| 159 |
static sig_atomic_t transflag; /* Flag where in a middle of transfer. */ |
| 160 |
static const char *pid_file = PATH_FTPDPID; |
| 161 |
#if !defined CMASK || CMASK == 0 |
| 162 |
# undef CMASK |
| 163 |
# define CMASK 027 |
| 164 |
#endif |
| 165 |
static int defumask = CMASK; /* Default umask value. */ |
| 166 |
static int login_attempts; /* Number of failed login attempts. */ |
| 167 |
static int askpasswd; /* Had user command, ask for passwd. */ |
| 168 |
static char curname[10]; /* Current USER name. */ |
| 169 |
static char ttyline[20]; /* Line to log in utmp. */ |
| 170 |
|
| 171 |
|
| 172 |
#define NUM_SIMUL_OFF_TO_STRS 4 |
| 173 |
|
| 174 |
/* Returns a string with the decimal representation of the off_t OFF, taking |
| 175 |
into account that off_t might be longer than a long. The return value is |
| 176 |
a pointer to a static buffer, but a return value will only be reused every |
| 177 |
NUM_SIMUL_OFF_TO_STRS calls, to allow multiple off_t's to be conveniently |
| 178 |
printed with a single printf statement. */ |
| 179 |
static char * |
| 180 |
off_to_str (off_t off) |
| 181 |
{ |
| 182 |
static char bufs[NUM_SIMUL_OFF_TO_STRS][80]; |
| 183 |
static char (*next_buf)[80] = bufs; |
| 184 |
|
| 185 |
if (next_buf >= (bufs + NUM_SIMUL_OFF_TO_STRS)) |
| 186 |
next_buf = bufs; |
| 187 |
|
| 188 |
if (sizeof (off) > sizeof (long)) |
| 189 |
sprintf (*next_buf, "%lld", (long long int) off); |
| 190 |
else if (sizeof (off) == sizeof (long)) |
| 191 |
sprintf (*next_buf, "%ld", (long) off); |
| 192 |
else |
| 193 |
sprintf (*next_buf, "%d", (int) off); |
| 194 |
|
| 195 |
return *next_buf++; |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* Timeout intervals for retrying connections |
| 200 |
* to hosts that don't accept PORT cmds. This |
| 201 |
* is a kludge, but given the problems with TCP... |
| 202 |
*/ |
| 203 |
#define SWAITMAX 90 /* wait at most 90 seconds */ |
| 204 |
#define SWAITINT 5 /* interval between retries */ |
| 205 |
|
| 206 |
static int swaitmax = SWAITMAX; |
| 207 |
static int swaitint = SWAITINT; |
| 208 |
|
| 209 |
#ifdef HAVE_SETPROCTITLE |
| 210 |
char proctitle[LINE_MAX]; /* initial part of title */ |
| 211 |
#endif /* SETPROCTITLE */ |
| 212 |
|
| 213 |
#define LOGCMD(cmd, file) \ |
| 214 |
if (logging > 1) \ |
| 215 |
syslog(LOG_INFO,"%s %s%s", cmd, \ |
| 216 |
*(file) == '/' ? "" : curdir(), file); |
| 217 |
#define LOGCMD2(cmd, file1, file2) \ |
| 218 |
if (logging > 1) \ |
| 219 |
syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ |
| 220 |
*(file1) == '/' ? "" : curdir(), file1, \ |
| 221 |
*(file2) == '/' ? "" : curdir(), file2); |
| 222 |
#define LOGBYTES(cmd, file, cnt) \ |
| 223 |
if (logging > 1) { \ |
| 224 |
if (cnt == (off_t)-1) \ |
| 225 |
syslog(LOG_INFO,"%s %s%s", cmd, \ |
| 226 |
*(file) == '/' ? "" : curdir(), file); \ |
| 227 |
else \ |
| 228 |
syslog(LOG_INFO, "%s %s%s = %s bytes", \ |
| 229 |
cmd, (*(file) == '/') ? "" : curdir(), file, \ |
| 230 |
off_to_str (cnt)); \ |
| 231 |
} |
| 232 |
|
| 233 |
extern int yyparse (void); |
| 234 |
|
| 235 |
static void ack (const char *); |
| 236 |
#ifdef HAVE_LIBWRAP |
| 237 |
static int check_host (struct sockaddr *sa, socklen_t len); |
| 238 |
#endif |
| 239 |
static void complete_login (struct credentials *); |
| 240 |
static char *curdir (void); |
| 241 |
static FILE *dataconn (const char *, off_t, const char *); |
| 242 |
static void dolog (struct sockaddr *, socklen_t, struct credentials *); |
| 243 |
static void end_login (struct credentials *); |
| 244 |
static FILE *getdatasock (const char *); |
| 245 |
static char *gunique (const char *); |
| 246 |
static void lostconn (int); |
| 247 |
static void myoob (int); |
| 248 |
static int receive_data (FILE *, FILE *, off_t); |
| 249 |
static void send_data (FILE *, FILE *, off_t); |
| 250 |
static void sigquit (int); |
| 251 |
|
| 252 |
const char doc[] = |
| 253 |
#ifdef WITH_PAM |
| 254 |
"File Transfer Protocol daemon, offering PAM service 'ftp'."; |
| 255 |
#else |
| 256 |
"File Transfer Protocol daemon."; |
| 257 |
#endif |
| 258 |
|
| 259 |
enum |
| 260 |
{ |
| 261 |
OPT_NONRFC2577 = CHAR_MAX + 1, |
| 262 |
}; |
| 263 |
|
| 264 |
static struct argp_option options[] = { |
| 265 |
#define GRID 0 |
| 266 |
{"anonymous-only", 'A', NULL, 0, |
| 267 |
"server configured for anonymous service only", |
| 268 |
GRID + 1}, |
| 269 |
{"daemon", 'D', NULL, 0, |
| 270 |
"start the ftpd standalone", |
| 271 |
GRID + 1}, |
| 272 |
{"debug", 'd', NULL, 0, |
| 273 |
"debug mode", |
| 274 |
GRID + 1}, |
| 275 |
{"ipv4", '4', NULL, 0, |
| 276 |
"restrict daemon to IPv4", |
| 277 |
GRID + 1}, |
| 278 |
{"ipv6", '6', NULL, 0, |
| 279 |
"restrict daemon to IPv6", |
| 280 |
GRID + 1}, |
| 281 |
{"logging", 'l', NULL, 0, |
| 282 |
"increase verbosity of syslog messages", |
| 283 |
GRID + 1}, |
| 284 |
{"pidfile", 'p', "PIDFILE", OPTION_ARG_OPTIONAL, |
| 285 |
"change default location of pidfile", |
| 286 |
GRID + 1}, |
| 287 |
{"no-version", 'q', NULL, 0, |
| 288 |
"do not display version in banner", |
| 289 |
GRID + 1}, |
| 290 |
{"timeout", 't', "TIMEOUT", 0, |
| 291 |
"set default idle timeout", |
| 292 |
GRID + 1}, |
| 293 |
{"max-timeout", 'T', "MAX_TIMEOUT", 0, |
| 294 |
"set maximum value of timeout allowed", |
| 295 |
GRID + 1}, |
| 296 |
{"non-rfc2577", OPT_NONRFC2577, NULL, 0, |
| 297 |
"neglect RFC 2577 by giving info on missing users", |
| 298 |
GRID + 1}, |
| 299 |
{"umask", 'u', "VAL", 0, |
| 300 |
"set default umask", |
| 301 |
GRID + 1}, |
| 302 |
{"auth", 'a', "AUTH", 0, |
| 303 |
"use AUTH for authentication", |
| 304 |
GRID + 1}, |
| 305 |
{NULL, 0, NULL, 0, "AUTH can be one of the following:", GRID + 2}, |
| 306 |
{" default", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, |
| 307 |
"passwd authentication", |
| 308 |
GRID + 3}, |
| 309 |
#ifdef WITH_PAM |
| 310 |
{" pam", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, |
| 311 |
"using PAM service 'ftp'", |
| 312 |
GRID + 3}, |
| 313 |
#endif |
| 314 |
#ifdef WITH_KERBEROS |
| 315 |
{" kerberos", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, |
| 316 |
"", |
| 317 |
GRID + 3}, |
| 318 |
#endif |
| 319 |
#ifdef WITH_KERBEROS5 |
| 320 |
{" kerberos5", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, |
| 321 |
"", |
| 322 |
GRID + 3}, |
| 323 |
#endif |
| 324 |
#ifdef WITH_OPIE |
| 325 |
{" opie", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, |
| 326 |
"", |
| 327 |
GRID + 3}, |
| 328 |
#endif |
| 329 |
{NULL, 0, NULL, 0, NULL, 0} |
| 330 |
}; |
| 331 |
|
| 332 |
static error_t |
| 333 |
parse_opt (int key, char *arg, struct argp_state *state) |
| 334 |
{ |
| 335 |
switch (key) |
| 336 |
{ |
| 337 |
case '4': |
| 338 |
/* Active in daemon mode only. */ |
| 339 |
usefamily = AF_INET; |
| 340 |
break; |
| 341 |
|
| 342 |
case '6': |
| 343 |
/* Active in daemon mode only. */ |
| 344 |
usefamily = AF_INET6; |
| 345 |
break; |
| 346 |
|
| 347 |
case 'A': |
| 348 |
/* Anonymous ftp only. */ |
| 349 |
anon_only = 1; |
| 350 |
break; |
| 351 |
|
| 352 |
case 'a': |
| 353 |
if (strcasecmp (arg, "default") == 0) |
| 354 |
cred.auth_type = AUTH_TYPE_PASSWD; |
| 355 |
#ifdef WITH_PAM |
| 356 |
else if (strcasecmp (arg, "pam") == 0) |
| 357 |
cred.auth_type = AUTH_TYPE_PAM; |
| 358 |
#endif |
| 359 |
#ifdef WITH_KERBEROS |
| 360 |
else if (strcasecmp (arg, "kerberos") == 0) |
| 361 |
cred.auth_type = AUTH_TYPE_KERBEROS; |
| 362 |
#endif |
| 363 |
#ifdef WITH_KERBEROS5 |
| 364 |
else if (strcasecmp (arg, "kerberos5") == 0) |
| 365 |
cred.auth_type = AUTH_TYPE_KERBEROS5; |
| 366 |
#endif |
| 367 |
#ifdef WITH_OPIE |
| 368 |
else if (strcasecmp (arg, "opie") == 0) |
| 369 |
cred.auth_type = AUTH_TYPE_OPIE; |
| 370 |
#endif |
| 371 |
break; |
| 372 |
|
| 373 |
case 'D': |
| 374 |
/* Run ftpd as daemon. */ |
| 375 |
daemon_mode = 1; |
| 376 |
break; |
| 377 |
|
| 378 |
case 'd': |
| 379 |
/* Enable debug mode. */ |
| 380 |
debug = 1; |
| 381 |
break; |
| 382 |
|
| 383 |
case 'l': |
| 384 |
/* Increase logging level. */ |
| 385 |
logging++; /* > 1 == Extra logging. */ |
| 386 |
break; |
| 387 |
|
| 388 |
case 'p': |
| 389 |
/* Override pid file */ |
| 390 |
pid_file = arg; |
| 391 |
break; |
| 392 |
|
| 393 |
case 'q': |
| 394 |
/* Don't include version number in banner. */ |
| 395 |
no_version = 1; |
| 396 |
break; |
| 397 |
|
| 398 |
case 't': |
| 399 |
/* Set default timeout value. */ |
| 400 |
timeout = atoi (arg); |
| 401 |
if (maxtimeout < timeout) |
| 402 |
maxtimeout = timeout; |
| 403 |
break; |
| 404 |
|
| 405 |
case 'T': /* Maximum timeout allowed. */ |
| 406 |
maxtimeout = atoi (arg); |
| 407 |
if (timeout > maxtimeout) |
| 408 |
timeout = maxtimeout; |
| 409 |
break; |
| 410 |
|
| 411 |
case 'u': /* Set umask. */ |
| 412 |
{ |
| 413 |
long val = 0; |
| 414 |
|
| 415 |
val = strtol (arg, &arg, 8); |
| 416 |
if (*arg != '\0' || val < 0) |
| 417 |
argp_error (state, "bad value for -u"); |
| 418 |
else |
| 419 |
defumask = val; |
| 420 |
break; |
| 421 |
} |
| 422 |
|
| 423 |
case OPT_NONRFC2577: |
| 424 |
rfc2577 = 0; |
| 425 |
break; |
| 426 |
|
| 427 |
default: |
| 428 |
return ARGP_ERR_UNKNOWN; |
| 429 |
} |
| 430 |
|
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
static struct argp argp = { |
| 435 |
options, |
| 436 |
parse_opt, |
| 437 |
NULL, |
| 438 |
doc, |
| 439 |
NULL, |
| 440 |
NULL, |
| 441 |
NULL |
| 442 |
}; |
| 443 |
|
| 444 |
int |
| 445 |
main (int argc, char *argv[], char **envp) |
| 446 |
{ |
| 447 |
int index; |
| 448 |
|
| 449 |
set_program_name (argv[0]); |
| 450 |
|
| 451 |
#ifdef HAVE_TZSET |
| 452 |
tzset (); /* In case no timezone database in ~ftp. */ |
| 453 |
#endif |
| 454 |
|
| 455 |
#ifdef HAVE_INITSETPROCTITLE |
| 456 |
/* Save start and extent of argv for setproctitle. */ |
| 457 |
initsetproctitle (argc, argv, envp); |
| 458 |
#else /* !HAVE_INITSETPROCTITLE */ |
| 459 |
(void) envp; /* Silence warnings. */ |
| 460 |
#endif |
| 461 |
|
| 462 |
/* Parse the command line */ |
| 463 |
iu_argp_init ("ftpd", default_program_authors); |
| 464 |
argp_parse (&argp, argc, argv, 0, &index, NULL); |
| 465 |
|
| 466 |
/* Bail out, wrong usage */ |
| 467 |
if (argc - index != 0) |
| 468 |
error (EXIT_FAILURE, 0, |
| 469 |
"surplus arguments; try `%s --help' for more info", program_name); |
| 470 |
|
| 471 |
/* LOG_NDELAY sets up the logging connection immediately, |
| 472 |
necessary for anonymous ftp's that chroot and can't do it later. */ |
| 473 |
openlog ("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); |
| 474 |
freopen (PATH_DEVNULL, "w", stderr); |
| 475 |
|
| 476 |
/* If not running via inetd, we detach and dup(fd, 0), dup(fd, 1) the |
| 477 |
fd = accept(). tcpd is check if compile with the support */ |
| 478 |
if (daemon_mode) |
| 479 |
{ |
| 480 |
#ifndef HAVE_FORK |
| 481 |
/* Shift out the daemon option in subforks */ |
| 482 |
int i; |
| 483 |
for (i = 0; i < argc; ++i) |
| 484 |
if (strcmp (argv[i], "-D") == 0) |
| 485 |
{ |
| 486 |
int j; |
| 487 |
for (j = i; j < argc; ++j) |
| 488 |
argv[j] = argv[j + 1]; |
| 489 |
argv[--argc] = NULL; |
| 490 |
} |
| 491 |
#endif |
| 492 |
his_addrlen = sizeof (his_addr); |
| 493 |
if (server_mode (pid_file, (struct sockaddr *) &his_addr, |
| 494 |
&his_addrlen, argv) < 0) |
| 495 |
exit (EXIT_FAILURE); |
| 496 |
} |
| 497 |
else |
| 498 |
{ |
| 499 |
his_addrlen = sizeof (his_addr); |
| 500 |
if (getpeername (STDIN_FILENO, (struct sockaddr *) &his_addr, |
| 501 |
&his_addrlen) < 0) |
| 502 |
{ |
| 503 |
syslog (LOG_ERR, "getpeername (%s): %m", program_name); |
| 504 |
exit (EXIT_FAILURE); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
signal (SIGHUP, sigquit); |
| 509 |
signal (SIGINT, sigquit); |
| 510 |
signal (SIGQUIT, sigquit); |
| 511 |
signal (SIGTERM, sigquit); |
| 512 |
signal (SIGPIPE, lostconn); |
| 513 |
signal (SIGCHLD, SIG_IGN); |
| 514 |
if (signal (SIGURG, myoob) == SIG_ERR) |
| 515 |
syslog (LOG_ERR, "signal: %m"); |
| 516 |
|
| 517 |
/* Get info on the ctrl connection. */ |
| 518 |
{ |
| 519 |
ctrl_addrlen = sizeof (ctrl_addr); |
| 520 |
if (getsockname (STDIN_FILENO, (struct sockaddr *) &ctrl_addr, |
| 521 |
&ctrl_addrlen) < 0) |
| 522 |
{ |
| 523 |
syslog (LOG_ERR, "getsockname (%s): %m", program_name); |
| 524 |
exit (EXIT_FAILURE); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
#if defined IP_TOS && defined IPTOS_LOWDELAY && defined IPPROTO_IP |
| 529 |
/* To minimize delays for interactive traffic. */ |
| 530 |
if (ctrl_addr.ss_family == AF_INET) |
| 531 |
{ |
| 532 |
int tos = IPTOS_LOWDELAY; |
| 533 |
if (setsockopt (STDIN_FILENO, IPPROTO_IP, IP_TOS, |
| 534 |
(char *) &tos, sizeof (int)) < 0) |
| 535 |
syslog (LOG_WARNING, "setsockopt (IP_TOS): %m"); |
| 536 |
} |
| 537 |
#endif |
| 538 |
|
| 539 |
#ifdef SO_OOBINLINE |
| 540 |
/* Try to handle urgent data inline. */ |
| 541 |
{ |
| 542 |
int on = 1; |
| 543 |
if (setsockopt (STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, |
| 544 |
(char *) &on, sizeof (on)) < 0) |
| 545 |
syslog (LOG_ERR, "setsockopt: %m"); |
| 546 |
} |
| 547 |
#endif |
| 548 |
|
| 549 |
#ifdef SO_KEEPALIVE |
| 550 |
/* Set keepalives on the socket to detect dropped connections. */ |
| 551 |
{ |
| 552 |
int keepalive = 1; |
| 553 |
if (setsockopt (STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, |
| 554 |
(char *) &keepalive, sizeof (keepalive)) < 0) |
| 555 |
syslog (LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); |
| 556 |
} |
| 557 |
#endif |
| 558 |
|
| 559 |
#ifdef F_SETOWN |
| 560 |
if (fcntl (STDIN_FILENO, F_SETOWN, getpid ()) == -1) |
| 561 |
syslog (LOG_ERR, "fcntl F_SETOWN: %m"); |
| 562 |
#endif |
| 563 |
|
| 564 |
dolog ((struct sockaddr *) &his_addr, his_addrlen, &cred); |
| 565 |
|
| 566 |
/* Deal with login disable. */ |
| 567 |
if (display_file (PATH_NOLOGIN, 530) == 0) |
| 568 |
{ |
| 569 |
reply (530, "System not available."); |
| 570 |
exit (EXIT_SUCCESS); |
| 571 |
} |
| 572 |
|
| 573 |
hostname = localhost (); |
| 574 |
if (!hostname) |
| 575 |
perror_reply (550, "Local resource failure: malloc"); |
| 576 |
|
| 577 |
/* Display a Welcome message if it exists. |
| 578 |
N.B. a reply(220,) must follow as continuation. */ |
| 579 |
display_file (PATH_FTPWELCOME, 220); |
| 580 |
|
| 581 |
/* Tell them we're ready to roll. */ |
| 582 |
if (!no_version) |
| 583 |
reply (220, "%s FTP server (%s %s) ready.", |
| 584 |
hostname, PACKAGE_NAME, PACKAGE_VERSION); |
| 585 |
else |
| 586 |
reply (220, "%s FTP server ready.", hostname); |
| 587 |
|
| 588 |
/* Set the jump, if we have an error parsing, |
| 589 |
come here and start fresh. */ |
| 590 |
setjmp (errcatch); |
| 591 |
|
| 592 |
/* Roll. */ |
| 593 |
for (;;) |
| 594 |
yyparse (); |
| 595 |
} |
| 596 |
|
| 597 |
static char * |
| 598 |
curdir (void) |
| 599 |
{ |
| 600 |
static char *path = 0; |
| 601 |
|
| 602 |
free (path); |
| 603 |
path = xgetcwd (); |
| 604 |
if (!path) |
| 605 |
return (char *) ""; |
| 606 |
if (path[1] != '\0') /* special case for root dir. */ |
| 607 |
{ |
| 608 |
char *tmp = realloc (path, strlen (path) + 2); /* '/' + '\0' */ |
| 609 |
if (!tmp) |
| 610 |
{ |
| 611 |
free (path); |
| 612 |
return (char *) ""; |
| 613 |
} |
| 614 |
strcat (tmp, "/"); |
| 615 |
path = tmp; |
| 616 |
} |
| 617 |
/* For guest account, skip / since it's chrooted */ |
| 618 |
return (cred.guest ? path + 1 : path); |
| 619 |
} |
| 620 |
|
| 621 |
static void |
| 622 |
sigquit (int signo) |
| 623 |
{ |
| 624 |
syslog (LOG_ERR, "got signal %s", strsignal (signo)); |
| 625 |
dologout (-1); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
static void |
| 630 |
lostconn (int signo MAYBE_UNUSED) |
| 631 |
{ |
| 632 |
if (debug) |
| 633 |
syslog (LOG_DEBUG, "lost connection"); |
| 634 |
dologout (-1); |
| 635 |
} |
| 636 |
|
| 637 |
/* Helper function. */ |
| 638 |
char * |
| 639 |
sgetsave (const char *s) |
| 640 |
{ |
| 641 |
char *string; |
| 642 |
size_t len; |
| 643 |
|
| 644 |
if (s == NULL) |
| 645 |
s = ""; |
| 646 |
|
| 647 |
len = strlen (s) + 1; |
| 648 |
string = malloc (len); |
| 649 |
if (string == NULL) |
| 650 |
{ |
| 651 |
perror_reply (421, "Local resource failure: malloc"); |
| 652 |
dologout (1); |
| 653 |
} |
| 654 |
/* (void) strcpy (string, s); */ |
| 655 |
memcpy (string, s, len); |
| 656 |
return string; |
| 657 |
} |
| 658 |
|
| 659 |
static void |
| 660 |
complete_login (struct credentials *pcred) |
| 661 |
{ |
| 662 |
char *cwd; |
| 663 |
|
| 664 |
if (setegid ((gid_t) pcred->gid) < 0) |
| 665 |
{ |
| 666 |
reply (550, "Can't set gid."); |
| 667 |
goto bad; |
| 668 |
} |
| 669 |
|
| 670 |
#ifdef HAVE_INITGROUPS |
| 671 |
initgroups (pcred->name, pcred->gid); |
| 672 |
#endif |
| 673 |
|
| 674 |
/* open wtmp before chroot */ |
| 675 |
snprintf (ttyline, sizeof (ttyline), "ftp%d", (int) getpid ()); |
| 676 |
logwtmp_keep_open (ttyline, pcred->name, pcred->remotehost); |
| 677 |
|
| 678 |
if (pcred->guest) |
| 679 |
{ |
| 680 |
/* We MUST do a chdir () after the chroot. Otherwise |
| 681 |
the old current directory will be accessible as "." |
| 682 |
outside the new root! */ |
| 683 |
if (chroot (pcred->rootdir) < 0 || chdir (pcred->homedir) < 0) |
| 684 |
{ |
| 685 |
reply (550, "Can't set guest privileges."); |
| 686 |
goto bad; |
| 687 |
} |
| 688 |
} |
| 689 |
else if (pcred->dochroot) |
| 690 |
{ |
| 691 |
if (chroot (pcred->rootdir) < 0 || chdir (pcred->homedir) < 0) |
| 692 |
{ |
| 693 |
reply (550, "Can't change root."); |
| 694 |
goto bad; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
if (seteuid ((uid_t) pcred->uid) < 0) |
| 699 |
{ |
| 700 |
reply (550, "Can't set uid."); |
| 701 |
goto bad; |
| 702 |
} |
| 703 |
|
| 704 |
if (!pcred->guest && !pcred->dochroot) /* Remaining case. */ |
| 705 |
{ |
| 706 |
if (chdir (pcred->rootdir) < 0) |
| 707 |
{ |
| 708 |
if (chdir ("/") < 0) |
| 709 |
{ |
| 710 |
reply (530, "User %s: can't change directory to %s.", |
| 711 |
pcred->name, pcred->homedir); |
| 712 |
goto bad; |
| 713 |
} |
| 714 |
|
| 715 |
lreply (230, "No directory! Logging in with home=/"); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
cwd = xgetcwd (); |
| 720 |
if (cwd) |
| 721 |
{ |
| 722 |
setenv ("HOME", cwd, 1); |
| 723 |
free (cwd); |
| 724 |
} |
| 725 |
|
| 726 |
/* Display a login message, if it exists. |
| 727 |
N.B. a reply(230,) must follow after this message. */ |
| 728 |
display_file (PATH_FTPLOGINMESG, 230); |
| 729 |
|
| 730 |
if (pcred->guest) |
| 731 |
{ |
| 732 |
reply (230, "Guest login ok, access restrictions apply."); |
| 733 |
#ifdef HAVE_SETPROCTITLE |
| 734 |
snprintf (proctitle, sizeof (proctitle), "%s: anonymous", |
| 735 |
pcred->remotehost); |
| 736 |
setproctitle ("%s", proctitle); |
| 737 |
#endif /* HAVE_SETPROCTITLE */ |
| 738 |
if (logging) |
| 739 |
syslog (LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s", pcred->remotehost); |
| 740 |
} |
| 741 |
else |
| 742 |
{ |
| 743 |
reply (230, "User %s logged in.", pcred->name); |
| 744 |
#ifdef HAVE_SETPROCTITLE |
| 745 |
snprintf (proctitle, sizeof (proctitle), |
| 746 |
"%s: %s", pcred->remotehost, pcred->name); |
| 747 |
setproctitle ("%s", proctitle); |
| 748 |
#endif /* HAVE_SETPROCTITLE */ |
| 749 |
if (logging) |
| 750 |
syslog (LOG_INFO, "FTP LOGIN FROM %s as %s", |
| 751 |
pcred->remotehost, pcred->name); |
| 752 |
} |
| 753 |
umask (defumask); |
| 754 |
return; |
| 755 |
bad: |
| 756 |
/* Forget all about it... */ |
| 757 |
end_login (pcred); /* Resets pcred->logged_in. */ |
| 758 |
} |
| 759 |
|
| 760 |
/* USER command. |
| 761 |
Sets global passwd pointer pw if named account exists and is acceptable; |
| 762 |
sets askpasswd if a PASS command is expected. If logged in previously, |
| 763 |
need to reset state. */ |
| 764 |
void |
| 765 |
user (const char *name) |
| 766 |
{ |
| 767 |
int ret; |
| 768 |
|
| 769 |
if (cred.logged_in) |
| 770 |
{ |
| 771 |
if (cred.guest || cred.dochroot) |
| 772 |
{ |
| 773 |
reply (530, "Can't change user from guest login."); |
| 774 |
return; |
| 775 |
} |
| 776 |
end_login (&cred); |
| 777 |
} |
| 778 |
|
| 779 |
/* Non zero means failed. */ |
| 780 |
ret = auth_user (name, &cred); |
| 781 |
if (!rfc2577 && ret != 0) |
| 782 |
{ |
| 783 |
/* If they gave us a reason. */ |
| 784 |
if (cred.message) |
| 785 |
{ |
| 786 |
reply (530, "%s", cred.message); |
| 787 |
free (cred.message); |
| 788 |
cred.message = NULL; |
| 789 |
} |
| 790 |
else |
| 791 |
reply (530, "User %s access denied.", name); |
| 792 |
if (logging) |
| 793 |
syslog (LOG_NOTICE, "FTP LOGIN REFUSED FROM %s, %s", |
| 794 |
cred.remotehost, name); |
| 795 |
return; |
| 796 |
} |
| 797 |
else if (rfc2577 && ret != 0) |
| 798 |
cred.delayed_reject = 1; |
| 799 |
else |
| 800 |
cred.delayed_reject = 0; |
| 801 |
|
| 802 |
/* Only messages for anonymous guests are accepted. */ |
| 803 |
if (rfc2577 && !cred.guest && cred.message) |
| 804 |
{ |
| 805 |
free (cred.message); |
| 806 |
cred.message = NULL; |
| 807 |
} |
| 808 |
|
| 809 |
/* If the server is set to serve anonymous service only |
| 810 |
the request have to come from a guest or a chrooted. */ |
| 811 |
if (anon_only && !cred.guest && !cred.dochroot) |
| 812 |
{ |
| 813 |
reply (530, "Sorry, only anonymous ftp allowed"); |
| 814 |
return; |
| 815 |
} |
| 816 |
|
| 817 |
if (logging) |
| 818 |
{ |
| 819 |
strncpy (curname, name, sizeof (curname) - 1); |
| 820 |
curname[sizeof (curname) - 1] = '\0'; /* Make sure null terminated. */ |
| 821 |
} |
| 822 |
|
| 823 |
if (cred.message) |
| 824 |
{ |
| 825 |
/* Stacked PAM modules for authentication may have |
| 826 |
* produced a multiline message at this point. |
| 827 |
* The FTP protocol does not cope well with this, |
| 828 |
* so we transfer only the very last line, which |
| 829 |
* should reflect the active authentication mechanism. |
| 830 |
*/ |
| 831 |
char *msg = strrchr (cred.message, '\n'); |
| 832 |
|
| 833 |
if (msg) |
| 834 |
msg++; /* Step over separator. */ |
| 835 |
else |
| 836 |
msg = cred.message; |
| 837 |
|
| 838 |
reply (331, "%s", msg); |
| 839 |
free (cred.message); |
| 840 |
cred.message = NULL; |
| 841 |
} |
| 842 |
else |
| 843 |
reply (331, "Password required for %s.", name); |
| 844 |
|
| 845 |
askpasswd = 1; |
| 846 |
|
| 847 |
/* Delay before reading passwd after first failed |
| 848 |
attempt to slow down passwd-guessing programs. */ |
| 849 |
if (login_attempts) |
| 850 |
sleep ((unsigned) login_attempts); |
| 851 |
} |
| 852 |
|
| 853 |
/* Terminate login as previous user, if any, resetting state; |
| 854 |
used when USER command is given or login fails. */ |
| 855 |
static void |
| 856 |
end_login (struct credentials *pcred) |
| 857 |
{ |
| 858 |
char *remotehost = pcred->remotehost; |
| 859 |
int atype = pcred->auth_type; |
| 860 |
|
| 861 |
if (seteuid ((uid_t) 0) == -1) |
| 862 |
_exit (EXIT_FAILURE); |
| 863 |
|
| 864 |
if (pcred->logged_in) |
| 865 |
{ |
| 866 |
logwtmp_keep_open (ttyline, "", ""); |
| 867 |
#ifdef WITH_PAM |
| 868 |
pam_end_login (pcred); |
| 869 |
#endif |
| 870 |
} |
| 871 |
|
| 872 |
free (pcred->name); |
| 873 |
if (pcred->passwd) |
| 874 |
{ |
| 875 |
memset (pcred->passwd, 0, strlen (pcred->passwd)); |
| 876 |
free (pcred->passwd); |
| 877 |
} |
| 878 |
free (pcred->homedir); |
| 879 |
free (pcred->rootdir); |
| 880 |
free (pcred->shell); |
| 881 |
if (pcred->pass) /* Properly erase old password. */ |
| 882 |
{ |
| 883 |
memset (pcred->pass, 0, strlen (pcred->pass)); |
| 884 |
free (pcred->pass); |
| 885 |
} |
| 886 |
free (pcred->message); |
| 887 |
memset (pcred, 0, sizeof (*pcred)); |
| 888 |
pcred->remotehost = remotehost; |
| 889 |
pcred->auth_type = atype; |
| 890 |
pcred->logged_in = 0; |
| 891 |
pcred->delayed_reject = 0; |
| 892 |
} |
| 893 |
|
| 894 |
void |
| 895 |
pass (const char *passwd) |
| 896 |
{ |
| 897 |
if (cred.logged_in || askpasswd == 0) |
| 898 |
{ |
| 899 |
reply (503, "Login with USER first."); |
| 900 |
return; |
| 901 |
} |
| 902 |
askpasswd = 0; |
| 903 |
|
| 904 |
if (!cred.guest) /* "ftp" is the only account allowed no password. */ |
| 905 |
{ |
| 906 |
/* Try to authenticate the user. Failed if != 0. */ |
| 907 |
if (auth_pass (passwd, &cred) != 0 || cred.delayed_reject) |
| 908 |
{ |
| 909 |
/* Any particular reason? */ |
| 910 |
if (rfc2577) |
| 911 |
{ |
| 912 |
if (cred.message) |
| 913 |
{ |
| 914 |
free (cred.message); |
| 915 |
cred.message = NULL; |
| 916 |
} |
| 917 |
reply (530, "Login incorrect."); |
| 918 |
} |
| 919 |
else if (cred.message) |
| 920 |
{ |
| 921 |
reply (530, "%s", cred.message); |
| 922 |
free (cred.message); |
| 923 |
cred.message = NULL; |
| 924 |
} |
| 925 |
else if (cred.expired & AUTH_EXPIRED_ACCT) |
| 926 |
reply (530, "Account is expired."); |
| 927 |
else if (cred.expired & AUTH_EXPIRED_PASS) |
| 928 |
reply (530, "Password has expired."); |
| 929 |
else |
| 930 |
reply (530, "Login incorrect."); |
| 931 |
|
| 932 |
if (logging) |
| 933 |
syslog (LOG_NOTICE, "FTP LOGIN FAILED FROM %s, %s", |
| 934 |
cred.remotehost, curname); |
| 935 |
if (login_attempts++ >= 5) |
| 936 |
{ |
| 937 |
syslog (LOG_NOTICE, "repeated login failures from %s", |
| 938 |
cred.remotehost); |
| 939 |
reply (421, |
| 940 |
"Service not available, closing control connection."); |
| 941 |
exit (EXIT_SUCCESS); |
| 942 |
} |
| 943 |
return; |
| 944 |
} |
| 945 |
if (cred.message) |
| 946 |
{ |
| 947 |
/* At least PAM might have committed additional messages. |
| 948 |
* Reply code 230 is used, since at this point the client |
| 949 |
* has been accepted. */ |
| 950 |
lreply_multiline (230, cred.message); |
| 951 |
free (cred.message); |
| 952 |
cred.message = NULL; |
| 953 |
} |
| 954 |
} |
| 955 |
cred.logged_in = 1; /* Everything seems to be all right. */ |
| 956 |
complete_login (&cred); |
| 957 |
if (cred.logged_in) |
| 958 |
login_attempts = 0; /* This time successful. */ |
| 959 |
else |
| 960 |
++login_attempts; |
| 961 |
} |
| 962 |
|
| 963 |
void |
| 964 |
retrieve (const char *cmd, const char *name) |
| 965 |
{ |
| 966 |
FILE *fin, *dout; |
| 967 |
struct stat st; |
| 968 |
int (*closefunc) (FILE *); |
| 969 |
size_t buffer_size = BUFSIZ; /* Dynamic buffer. */ |
| 970 |
|
| 971 |
if (cmd == 0) |
| 972 |
{ |
| 973 |
fin = fopen (name, "r"), closefunc = fclose; |
| 974 |
st.st_size = 0; |
| 975 |
} |
| 976 |
else |
| 977 |
{ |
| 978 |
char line[BUFSIZ]; |
| 979 |
|
| 980 |
snprintf (line, sizeof line, cmd, name); |
| 981 |
name = line; |
| 982 |
fin = ftpd_popen (line, "r"), closefunc = ftpd_pclose; |
| 983 |
st.st_size = -1; |
| 984 |
} |
| 985 |
|
| 986 |
if (fin == NULL) |
| 987 |
{ |
| 988 |
if (errno != 0) |
| 989 |
{ |
| 990 |
perror_reply (550, name); |
| 991 |
if (cmd == 0) |
| 992 |
{ |
| 993 |
LOGCMD ("get", name); |
| 994 |
} |
| 995 |
} |
| 996 |
return; |
| 997 |
} |
| 998 |
byte_count = -1; |
| 999 |
if (cmd == 0 && (fstat (fileno (fin), &st) < 0 || !S_ISREG (st.st_mode))) |
| 1000 |
{ |
| 1001 |
reply (550, "%s: not a plain file.", name); |
| 1002 |
goto done; |
| 1003 |
} |
| 1004 |
else if (cmd == 0) |
| 1005 |
buffer_size = st.st_blksize; /* Depends on file system. */ |
| 1006 |
|
| 1007 |
if (restart_point) |
| 1008 |
{ |
| 1009 |
if (type == TYPE_A) |
| 1010 |
{ |
| 1011 |
off_t i, n; |
| 1012 |
int c; |
| 1013 |
|
| 1014 |
n = restart_point; |
| 1015 |
i = 0; |
| 1016 |
while (i++ < n) |
| 1017 |
{ |
| 1018 |
c = getc (fin); |
| 1019 |
if (c == EOF) |
| 1020 |
{ |
| 1021 |
/* Error code 554 was introduced in RFC 1123. */ |
| 1022 |
reply (554, |
| 1023 |
"Action not taken: invalid REST value %jd for %s.", |
| 1024 |
restart_point, name); |
| 1025 |
goto done; |
| 1026 |
} |
| 1027 |
if (c == '\n') |
| 1028 |
i++; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
else if (lseek (fileno (fin), restart_point, SEEK_SET) < 0) |
| 1032 |
{ |
| 1033 |
if (errno == EINVAL) |
| 1034 |
reply (554, "Action not taken: invalid REST value %jd for %s.", |
| 1035 |
restart_point, name); |
| 1036 |
else |
| 1037 |
perror_reply (550, name); |
| 1038 |
goto done; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
dout = dataconn (name, st.st_size, "w"); |
| 1042 |
if (dout == NULL) |
| 1043 |
goto done; |
| 1044 |
send_data (fin, dout, buffer_size); |
| 1045 |
fclose (dout); |
| 1046 |
data = -1; |
| 1047 |
pdata = -1; |
| 1048 |
done: |
| 1049 |
if (cmd == 0) |
| 1050 |
LOGBYTES ("get", name, byte_count); |
| 1051 |
(*closefunc) (fin); |
| 1052 |
} |
| 1053 |
|
| 1054 |
void |
| 1055 |
store (const char *name, const char *mode, int unique) |
| 1056 |
{ |
| 1057 |
FILE *fout, *din; |
| 1058 |
struct stat st; |
| 1059 |
int (*closefunc) (FILE *); |
| 1060 |
|
| 1061 |
if (unique && stat (name, &st) == 0) |
| 1062 |
{ |
| 1063 |
const char *name_unique = gunique (name); |
| 1064 |
|
| 1065 |
if (name_unique) |
| 1066 |
name = name_unique; |
| 1067 |
else |
| 1068 |
{ |
| 1069 |
LOGCMD (*mode == 'w' ? "put" : "append", name); |
| 1070 |
return; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
if (restart_point) |
| 1075 |
mode = "r+"; |
| 1076 |
fout = fopen (name, mode); |
| 1077 |
closefunc = fclose; |
| 1078 |
if (fout == NULL || fstat (fileno (fout), &st) < 0) |
| 1079 |
{ |
| 1080 |
perror_reply (553, name); |
| 1081 |
LOGCMD (*mode == 'w' ? "put" : "append", name); |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
byte_count = -1; |
| 1085 |
if (restart_point) |
| 1086 |
{ |
| 1087 |
if (type == TYPE_A) |
| 1088 |
{ |
| 1089 |
off_t i, n; |
| 1090 |
int c; |
| 1091 |
|
| 1092 |
n = restart_point; |
| 1093 |
i = 0; |
| 1094 |
while (i++ < n) |
| 1095 |
{ |
| 1096 |
c = getc (fout); |
| 1097 |
if (c == EOF) |
| 1098 |
{ |
| 1099 |
/* Error code 554 was introduced in RFC 1123. */ |
| 1100 |
reply (554, |
| 1101 |
"Action not taken: invalid REST value %jd for %s.", |
| 1102 |
restart_point, name); |
| 1103 |
goto done; |
| 1104 |
} |
| 1105 |
if (c == '\n') |
| 1106 |
i++; |
| 1107 |
} |
| 1108 |
/* We must do this seek to "current" position |
| 1109 |
because we are changing from reading to |
| 1110 |
writing. */ |
| 1111 |
if (fseeko (fout, 0L, SEEK_CUR) < 0) |
| 1112 |
{ |
| 1113 |
perror_reply (550, name); |
| 1114 |
goto done; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
else if (lseek (fileno (fout), restart_point, SEEK_SET) < 0) |
| 1118 |
{ |
| 1119 |
if (errno == EINVAL) |
| 1120 |
reply (554, "Action not taken: invalid REST value %jd for %s.", |
| 1121 |
restart_point, name); |
| 1122 |
else |
| 1123 |
perror_reply (550, name); |
| 1124 |
goto done; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
din = dataconn (name, (off_t) - 1, "r"); |
| 1128 |
if (din == NULL) |
| 1129 |
goto done; |
| 1130 |
if (receive_data (din, fout, st.st_blksize) == 0) |
| 1131 |
{ |
| 1132 |
if (unique) |
| 1133 |
reply (226, "Transfer complete (unique file name:%s).", name); |
| 1134 |
else |
| 1135 |
reply (226, "Transfer complete."); |
| 1136 |
} |
| 1137 |
fclose (din); |
| 1138 |
data = -1; |
| 1139 |
pdata = -1; |
| 1140 |
done: |
| 1141 |
LOGBYTES (*mode == 'w' ? "put" : "append", name, byte_count); |
| 1142 |
(*closefunc) (fout); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static FILE * |
| 1146 |
getdatasock (const char *mode) |
| 1147 |
{ |
| 1148 |
int s, t, tries; |
| 1149 |
|
| 1150 |
if (data >= 0) |
| 1151 |
return fdopen (data, mode); |
| 1152 |
if (seteuid ((uid_t) 0) == -1) |
| 1153 |
_exit (EXIT_FAILURE); |
| 1154 |
s = socket (ctrl_addr.ss_family, SOCK_STREAM, 0); |
| 1155 |
if (s < 0) |
| 1156 |
goto bad; |
| 1157 |
|
| 1158 |
/* Enables local reuse address. */ |
| 1159 |
{ |
| 1160 |
int on = 1; |
| 1161 |
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, |
| 1162 |
(char *) &on, sizeof (on)) < 0) |
| 1163 |
goto bad; |
| 1164 |
} |
| 1165 |
|
| 1166 |
/* Anchor socket to avoid multi-homing problems. */ |
| 1167 |
memcpy (&data_source, &ctrl_addr, sizeof (data_source)); |
| 1168 |
data_source_len = ctrl_addrlen; |
| 1169 |
|
| 1170 |
/* Erase port number, suggesting bind() to allocate a new port. */ |
| 1171 |
switch (data_source.ss_family) |
| 1172 |
{ |
| 1173 |
case AF_INET6: |
| 1174 |
((struct sockaddr_in6 *) &data_source)->sin6_port = 0; |
| 1175 |
break; |
| 1176 |
case AF_INET: |
| 1177 |
((struct sockaddr_in *) &data_source)->sin_port = 0; |
| 1178 |
break; |
| 1179 |
default: |
| 1180 |
break; /* Do nothing; should not happen! */ |
| 1181 |
} |
| 1182 |
|
| 1183 |
for (tries = 1;; tries++) |
| 1184 |
{ |
| 1185 |
if (bind (s, (struct sockaddr *) &data_source, data_source_len) >= 0) |
| 1186 |
break; |
| 1187 |
if (errno != EADDRINUSE || tries > 10) |
| 1188 |
goto bad; |
| 1189 |
sleep (tries); |
| 1190 |
} |
| 1191 |
if (seteuid ((uid_t) cred.uid) != 0) |
| 1192 |
_exit (EXIT_FAILURE); |
| 1193 |
|
| 1194 |
#if defined IP_TOS && defined IPTOS_THROUGHPUT && defined IPPROTO_IP |
| 1195 |
if (ctrl_addr.ss_family == AF_INET) |
| 1196 |
{ |
| 1197 |
int on = IPTOS_THROUGHPUT; |
| 1198 |
if (setsockopt (s, IPPROTO_IP, IP_TOS, (char *) &on, sizeof (int)) < 0) |
| 1199 |
syslog (LOG_WARNING, "setsockopt (IP_TOS): %m"); |
| 1200 |
} |
| 1201 |
#endif |
| 1202 |
|
| 1203 |
return (fdopen (s, mode)); |
| 1204 |
bad: |
| 1205 |
/* Return the real value of errno (close may change it) */ |
| 1206 |
t = errno; |
| 1207 |
if (seteuid ((uid_t) cred.uid) != 0) |
| 1208 |
_exit (EXIT_FAILURE); |
| 1209 |
close (s); |
| 1210 |
errno = t; |
| 1211 |
return NULL; |
| 1212 |
} |
| 1213 |
|
| 1214 |
static FILE * |
| 1215 |
dataconn (const char *name, off_t size, const char *mode) |
| 1216 |
{ |
| 1217 |
char sizebuf[32]; |
| 1218 |
FILE *file; |
| 1219 |
int retry = 0; |
| 1220 |
|
| 1221 |
file_size = size; |
| 1222 |
byte_count = 0; |
| 1223 |
if (size != (off_t) - 1) |
| 1224 |
snprintf (sizebuf, sizeof (sizebuf), " (%s bytes)", off_to_str (size)); |
| 1225 |
else |
| 1226 |
*sizebuf = '\0'; |
| 1227 |
if (pdata >= 0) |
| 1228 |
{ |
| 1229 |
struct sockaddr_storage from; |
| 1230 |
int s; |
| 1231 |
socklen_t fromlen = sizeof (from); |
| 1232 |
|
| 1233 |
signal (SIGALRM, toolong); |
| 1234 |
alarm ((unsigned) timeout); |
| 1235 |
s = accept (pdata, (struct sockaddr *) &from, &fromlen); |
| 1236 |
alarm (0); |
| 1237 |
if (s < 0) |
| 1238 |
{ |
| 1239 |
reply (425, "Can't open data connection."); |
| 1240 |
close (pdata); |
| 1241 |
pdata = -1; |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
close (pdata); |
| 1245 |
pdata = s; |
| 1246 |
#if defined IP_TOS && defined IPTOS_THROUGHPUT && defined IPPROTO_IP |
| 1247 |
/* Optimize throughput. */ |
| 1248 |
if (from.ss_family == AF_INET) |
| 1249 |
{ |
| 1250 |
int tos = IPTOS_THROUGHPUT; |
| 1251 |
setsockopt (s, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof (int)); |
| 1252 |
} |
| 1253 |
#endif |
| 1254 |
#ifdef SO_KEEPALIVE |
| 1255 |
/* Set keepalives on the socket to detect dropped conns. */ |
| 1256 |
{ |
| 1257 |
int keepalive = 1; |
| 1258 |
setsockopt (s, SOL_SOCKET, SO_KEEPALIVE, |
| 1259 |
(char *) &keepalive, sizeof (int)); |
| 1260 |
} |
| 1261 |
#endif |
| 1262 |
reply (150, "Opening %s mode data connection for '%s'%s.", |
| 1263 |
type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); |
| 1264 |
return fdopen (pdata, mode); |
| 1265 |
} |
| 1266 |
if (data >= 0) |
| 1267 |
{ |
| 1268 |
reply (125, "Using existing data connection for '%s'%s.", |
| 1269 |
name, sizebuf); |
| 1270 |
usedefault = 1; |
| 1271 |
return fdopen (data, mode); |
| 1272 |
} |
| 1273 |
if (usedefault) |
| 1274 |
{ |
| 1275 |
memcpy (&data_dest, &his_addr, sizeof (data_dest)); |
| 1276 |
data_dest_len = his_addrlen; |
| 1277 |
} |
| 1278 |
usedefault = 1; |
| 1279 |
file = getdatasock (mode); |
| 1280 |
if (file == NULL) |
| 1281 |
{ |
| 1282 |
int oerrno = errno; |
| 1283 |
(void) getnameinfo ((struct sockaddr *) &data_source, data_source_len, |
| 1284 |
addrstr, sizeof (addrstr), |
| 1285 |
portstr, sizeof (portstr), NI_NUMERICSERV); |
| 1286 |
reply (425, "Can't create data socket (%s,%s): %s.", |
| 1287 |
addrstr, portstr, strerror (oerrno)); |
| 1288 |
return NULL; |
| 1289 |
} |
| 1290 |
data = fileno (file); |
| 1291 |
while (connect (data, (struct sockaddr *) &data_dest, data_dest_len) < 0) |
| 1292 |
{ |
| 1293 |
if (errno == EADDRINUSE && retry < swaitmax) |
| 1294 |
{ |
| 1295 |
sleep ((unsigned) swaitint); |
| 1296 |
retry += swaitint; |
| 1297 |
continue; |
| 1298 |
} |
| 1299 |
perror_reply (425, "Can't build data connection"); |
| 1300 |
fclose (file); |
| 1301 |
data = -1; |
| 1302 |
return NULL; |
| 1303 |
} |
| 1304 |
reply (150, "Opening %s mode data connection for '%s'%s.", |
| 1305 |
type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); |
| 1306 |
return file; |
| 1307 |
} |
| 1308 |
|
| 1309 |
#define IU_MMAP_SIZE 0x800000 /* 8 MByte */ |
| 1310 |
|
| 1311 |
/* Transfer the contents of "instr" to "outstr" peer using the appropriate |
| 1312 |
encapsulation of the data subject * to Mode, Structure, and Type. |
| 1313 |
|
| 1314 |
NB: Form isn't handled. */ |
| 1315 |
static void |
| 1316 |
send_data (FILE *instr, FILE *outstr, off_t blksize) |
| 1317 |
{ |
| 1318 |
int c, cnt, filefd, netfd; |
| 1319 |
char *buf = MAP_FAILED, *bp; |
| 1320 |
off_t curpos; |
| 1321 |
off_t len, filesize; |
| 1322 |
|
| 1323 |
transflag++; |
| 1324 |
if (setjmp (urgcatch)) |
| 1325 |
{ |
| 1326 |
transflag = 0; |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
|
| 1330 |
netfd = fileno (outstr); |
| 1331 |
filefd = fileno (instr); |
| 1332 |
#ifdef HAVE_MMAP |
| 1333 |
/* Last argument in mmap() must be page aligned, |
| 1334 |
* at least for Solaris and Linux, so use mmap() |
| 1335 |
* only with null offset retrievals. |
| 1336 |
*/ |
| 1337 |
if (file_size > 0 && file_size < IU_MMAP_SIZE && restart_point == 0) |
| 1338 |
{ |
| 1339 |
curpos = lseek (filefd, 0, SEEK_CUR); |
| 1340 |
if (debug) |
| 1341 |
syslog (LOG_DEBUG, "Position is %jd. Attempting mmap call.", curpos); |
| 1342 |
if (curpos >= 0) |
| 1343 |
{ |
| 1344 |
filesize = file_size - curpos; |
| 1345 |
buf = mmap (0, filesize, PROT_READ, MAP_SHARED, filefd, curpos); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
#endif |
| 1349 |
|
| 1350 |
switch (type) |
| 1351 |
{ |
| 1352 |
|
| 1353 |
case TYPE_A: |
| 1354 |
#ifdef HAVE_MMAP |
| 1355 |
if (file_size > 0 && curpos >= 0 && buf != MAP_FAILED) |
| 1356 |
{ |
| 1357 |
if (debug) |
| 1358 |
syslog (LOG_DEBUG, "Reading file as ascii in mmap mode."); |
| 1359 |
len = 0; |
| 1360 |
while (len < filesize) |
| 1361 |
{ |
| 1362 |
byte_count++; |
| 1363 |
if (buf[len] == '\n') |
| 1364 |
{ |
| 1365 |
if (ferror (outstr)) |
| 1366 |
break; |
| 1367 |
putc ('\r', outstr); |
| 1368 |
} |
| 1369 |
putc (buf[len], outstr); |
| 1370 |
len++; |
| 1371 |
} |
| 1372 |
fflush (outstr); |
| 1373 |
transflag = 0; |
| 1374 |
munmap (buf, filesize); |
| 1375 |
if (ferror (outstr)) |
| 1376 |
goto data_err; |
| 1377 |
reply (226, "Transfer complete."); |
| 1378 |
return; |
| 1379 |
} |
| 1380 |
#endif |
| 1381 |
if (debug) |
| 1382 |
syslog (LOG_DEBUG, "Reading file as ascii in byte mode."); |
| 1383 |
while ((c = getc (instr)) != EOF) |
| 1384 |
{ |
| 1385 |
byte_count++; |
| 1386 |
if (c == '\n') |
| 1387 |
{ |
| 1388 |
if (ferror (outstr)) |
| 1389 |
goto data_err; |
| 1390 |
putc ('\r', outstr); |
| 1391 |
} |
| 1392 |
putc (c, outstr); |
| 1393 |
} |
| 1394 |
fflush (outstr); |
| 1395 |
transflag = 0; |
| 1396 |
if (ferror (instr)) |
| 1397 |
goto file_err; |
| 1398 |
if (ferror (outstr)) |
| 1399 |
goto data_err; |
| 1400 |
reply (226, "Transfer complete."); |
| 1401 |
return; |
| 1402 |
|
| 1403 |
case TYPE_I: |
| 1404 |
case TYPE_L: |
| 1405 |
#ifdef HAVE_MMAP |
| 1406 |
if (file_size > 0 && curpos >= 0 && buf != MAP_FAILED) |
| 1407 |
{ |
| 1408 |
if (debug) |
| 1409 |
syslog (LOG_DEBUG, "Reading file as image in mmap mode."); |
| 1410 |
bp = buf; |
| 1411 |
len = filesize; |
| 1412 |
do |
| 1413 |
{ |
| 1414 |
cnt = write (netfd, bp, len); |
| 1415 |
len -= cnt; |
| 1416 |
bp += cnt; |
| 1417 |
if (cnt > 0) |
| 1418 |
byte_count += cnt; |
| 1419 |
} |
| 1420 |
while (cnt > 0 && len > 0); |
| 1421 |
transflag = 0; |
| 1422 |
munmap (buf, (size_t) filesize); |
| 1423 |
if (cnt < 0) |
| 1424 |
goto data_err; |
| 1425 |
reply (226, "Transfer complete."); |
| 1426 |
return; |
| 1427 |
} |
| 1428 |
#endif |
| 1429 |
if (debug) |
| 1430 |
{ |
| 1431 |
syslog (LOG_DEBUG, "Reading file as image in block mode."); |
| 1432 |
curpos = lseek (filefd, 0, SEEK_CUR); |
| 1433 |
if (curpos < 0) |
| 1434 |
syslog (LOG_DEBUG, "Input file: %m"); |
| 1435 |
else |
| 1436 |
syslog (LOG_DEBUG, "Starting at position %jd.", curpos); |
| 1437 |
} |
| 1438 |
|
| 1439 |
buf = malloc ((unsigned int) blksize); |
| 1440 |
if (buf == NULL) |
| 1441 |
{ |
| 1442 |
transflag = 0; |
| 1443 |
perror_reply (451, "Local resource failure: malloc"); |
| 1444 |
return; |
| 1445 |
} |
| 1446 |
while ((cnt = read (filefd, buf, (unsigned int) blksize)) > 0 && |
| 1447 |
write (netfd, buf, cnt) == cnt) |
| 1448 |
byte_count += cnt; |
| 1449 |
|
| 1450 |
transflag = 0; |
| 1451 |
free (buf); |
| 1452 |
if (cnt != 0) |
| 1453 |
{ |
| 1454 |
if (cnt < 0) |
| 1455 |
goto file_err; |
| 1456 |
goto data_err; |
| 1457 |
} |
| 1458 |
reply (226, "Transfer complete."); |
| 1459 |
return; |
| 1460 |
default: |
| 1461 |
transflag = 0; |
| 1462 |
reply (550, "Unimplemented TYPE %d in send_data", type); |
| 1463 |
return; |
| 1464 |
} |
| 1465 |
|
| 1466 |
data_err: |
| 1467 |
transflag = 0; |
| 1468 |
perror_reply (426, "Data connection"); |
| 1469 |
return; |
| 1470 |
|
| 1471 |
file_err: |
| 1472 |
transflag = 0; |
| 1473 |
perror_reply (551, "Error on input file"); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* Transfer data from peer to "outstr" using the appropriate encapulation of |
| 1477 |
the data subject to Mode, Structure, and Type. |
| 1478 |
|
| 1479 |
N.B.: Form isn't handled. */ |
| 1480 |
static int |
| 1481 |
receive_data (FILE *instr, FILE *outstr, off_t blksize) |
| 1482 |
{ |
| 1483 |
int c; |
| 1484 |
int cnt, bare_lfs = 0; |
| 1485 |
char *buf; |
| 1486 |
|
| 1487 |
transflag++; |
| 1488 |
if (setjmp (urgcatch)) |
| 1489 |
{ |
| 1490 |
transflag = 0; |
| 1491 |
return -1; |
| 1492 |
} |
| 1493 |
switch (type) |
| 1494 |
{ |
| 1495 |
case TYPE_I: |
| 1496 |
case TYPE_L: |
| 1497 |
buf = malloc ((unsigned int) blksize); |
| 1498 |
if (buf == NULL) |
| 1499 |
{ |
| 1500 |
transflag = 0; |
| 1501 |
perror_reply (451, "Local resource failure: malloc"); |
| 1502 |
return -1; |
| 1503 |
} |
| 1504 |
|
| 1505 |
while ((cnt = read (fileno (instr), buf, blksize)) > 0) |
| 1506 |
{ |
| 1507 |
if (write (fileno (outstr), buf, cnt) != cnt) |
| 1508 |
{ |
| 1509 |
free (buf); |
| 1510 |
goto file_err; |
| 1511 |
} |
| 1512 |
byte_count += cnt; |
| 1513 |
} |
| 1514 |
free (buf); |
| 1515 |
if (cnt < 0) |
| 1516 |
goto data_err; |
| 1517 |
transflag = 0; |
| 1518 |
return 0; |
| 1519 |
|
| 1520 |
case TYPE_E: |
| 1521 |
reply (553, "TYPE E not implemented."); |
| 1522 |
transflag = 0; |
| 1523 |
return -1; |
| 1524 |
|
| 1525 |
case TYPE_A: |
| 1526 |
while ((c = getc (instr)) != EOF) |
| 1527 |
{ |
| 1528 |
byte_count++; |
| 1529 |
if (c == '\n') |
| 1530 |
bare_lfs++; |
| 1531 |
while (c == '\r') |
| 1532 |
{ |
| 1533 |
if (ferror (outstr)) |
| 1534 |
goto data_err; |
| 1535 |
c = getc (instr); |
| 1536 |
if (c != '\n') |
| 1537 |
{ |
| 1538 |
putc ('\r', outstr); |
| 1539 |
if (c == '\0' || c == EOF) |
| 1540 |
goto contin2; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
putc (c, outstr); |
| 1544 |
contin2:; |
| 1545 |
} |
| 1546 |
fflush (outstr); |
| 1547 |
if (ferror (instr)) |
| 1548 |
goto data_err; |
| 1549 |
if (ferror (outstr)) |
| 1550 |
goto file_err; |
| 1551 |
transflag = 0; |
| 1552 |
if (bare_lfs) |
| 1553 |
{ |
| 1554 |
lreply (226, "WARNING! %d bare linefeeds received in ASCII mode", |
| 1555 |
bare_lfs); |
| 1556 |
printf (" File may not have transferred correctly.\r\n"); |
| 1557 |
} |
| 1558 |
return (0); |
| 1559 |
default: |
| 1560 |
reply (550, "Unimplemented TYPE %d in receive_data", type); |
| 1561 |
transflag = 0; |
| 1562 |
return -1; |
| 1563 |
} |
| 1564 |
|
| 1565 |
data_err: |
| 1566 |
transflag = 0; |
| 1567 |
perror_reply (426, "Data Connection"); |
| 1568 |
return -1; |
| 1569 |
|
| 1570 |
file_err: |
| 1571 |
transflag = 0; |
| 1572 |
perror_reply (452, "Error writing file"); |
| 1573 |
return -1; |
| 1574 |
} |
| 1575 |
|
| 1576 |
void |
| 1577 |
statfilecmd (const char *filename) |
| 1578 |
{ |
| 1579 |
FILE *fin; |
| 1580 |
int c; |
| 1581 |
char line[LINE_MAX]; |
| 1582 |
|
| 1583 |
snprintf (line, sizeof (line), "/bin/ls -lgA %s", filename); |
| 1584 |
fin = ftpd_popen (line, "r"); |
| 1585 |
lreply (211, "status of %s:", filename); |
| 1586 |
while ((c = getc (fin)) != EOF) |
| 1587 |
{ |
| 1588 |
if (c == '\n') |
| 1589 |
{ |
| 1590 |
if (ferror (stdout)) |
| 1591 |
{ |
| 1592 |
perror_reply (421, "control connection"); |
| 1593 |
ftpd_pclose (fin); |
| 1594 |
dologout (1); |
| 1595 |
} |
| 1596 |
if (ferror (fin)) |
| 1597 |
{ |
| 1598 |
perror_reply (551, filename); |
| 1599 |
ftpd_pclose (fin); |
| 1600 |
return; |
| 1601 |
} |
| 1602 |
putc ('\r', stdout); |
| 1603 |
} |
| 1604 |
putc (c, stdout); |
| 1605 |
} |
| 1606 |
ftpd_pclose (fin); |
| 1607 |
reply (211, "End of Status"); |
| 1608 |
} |
| 1609 |
|
| 1610 |
void |
| 1611 |
statcmd (void) |
| 1612 |
{ |
| 1613 |
struct sockaddr_storage *sin; |
| 1614 |
unsigned char *a, *p; |
| 1615 |
|
| 1616 |
lreply (211, "%s FTP server status:", hostname); |
| 1617 |
if (!no_version) |
| 1618 |
printf (" ftpd (%s) %s\r\n", PACKAGE_NAME, PACKAGE_VERSION); |
| 1619 |
printf (" Connected to %s", cred.remotehost); |
| 1620 |
(void) getnameinfo ((struct sockaddr *) &his_addr, his_addrlen, |
| 1621 |
addrstr, sizeof (addrstr), NULL, 0, NI_NUMERICHOST); |
| 1622 |
if (!isdigit (cred.remotehost[0])) |
| 1623 |
printf (" (%s)", addrstr); |
| 1624 |
printf ("\r\n"); |
| 1625 |
printf (" Session timeout is %d seconds\r\n", timeout); |
| 1626 |
if (cred.logged_in) |
| 1627 |
{ |
| 1628 |
if (cred.guest) |
| 1629 |
printf (" Logged in anonymously\r\n"); |
| 1630 |
else |
| 1631 |
printf (" Logged in as %s\r\n", cred.name); |
| 1632 |
} |
| 1633 |
else if (askpasswd) |
| 1634 |
printf (" Waiting for password\r\n"); |
| 1635 |
else |
| 1636 |
printf (" Waiting for user name\r\n"); |
| 1637 |
printf (" TYPE: %s", typenames[type]); |
| 1638 |
if (type == TYPE_A || type == TYPE_E) |
| 1639 |
printf (", FORM: %s", formnames[form]); |
| 1640 |
if (type == TYPE_L) |
| 1641 |
printf (" %d", CHAR_BIT); |
| 1642 |
printf ("; STRUcture: %s; transfer MODE: %s\r\n", |
| 1643 |
strunames[stru], modenames[stru_mode]); |
| 1644 |
if (data != -1) |
| 1645 |
printf (" Data connection open\r\n"); |
| 1646 |
else if (pdata != -1) |
| 1647 |
{ |
| 1648 |
printf (" in Passive mode"); |
| 1649 |
sin = &pasv_addr; |
| 1650 |
goto printaddr; |
| 1651 |
} |
| 1652 |
else if (usedefault == 0) |
| 1653 |
{ |
| 1654 |
printf (" PORT"); |
| 1655 |
sin = &data_dest; |
| 1656 |
printaddr: |
| 1657 |
a = (unsigned char *) &((struct sockaddr_in *) sin)->sin_addr; |
| 1658 |
p = (unsigned char *) &((struct sockaddr_in *) sin)->sin_port; |
| 1659 |
#define UC(b) (((int) b) & 0xff) |
| 1660 |
printf (" (%d,%d,%d,%d,%d,%d)\r\n", UC (a[0]), |
| 1661 |
UC (a[1]), UC (a[2]), UC (a[3]), UC (p[0]), UC (p[1])); |
| 1662 |
#undef UC |
| 1663 |
} |
| 1664 |
else |
| 1665 |
printf (" No data connection\r\n"); |
| 1666 |
reply (211, "End of status"); |
| 1667 |
} |
| 1668 |
|
| 1669 |
void |
| 1670 |
fatal (const char *s) |
| 1671 |
{ |
| 1672 |
reply (451, "Error in server: %s\n", s); |
| 1673 |
reply (221, "Closing connection due to server error."); |
| 1674 |
dologout (0); |
| 1675 |
} |
| 1676 |
|
| 1677 |
void |
| 1678 |
reply (int n, const char *fmt, ...) |
| 1679 |
{ |
| 1680 |
va_list ap; |
| 1681 |
va_start (ap, fmt); |
| 1682 |
printf ("%d ", n); |
| 1683 |
vprintf (fmt, ap); |
| 1684 |
va_end (ap); |
| 1685 |
printf ("\r\n"); |
| 1686 |
fflush (stdout); |
| 1687 |
if (debug) |
| 1688 |
{ |
| 1689 |
syslog (LOG_DEBUG, "<--- %d ", n); |
| 1690 |
va_start (ap, fmt); |
| 1691 |
vsyslog (LOG_DEBUG, fmt, ap); |
| 1692 |
va_end (ap); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
void |
| 1697 |
lreply (int n, const char *fmt, ...) |
| 1698 |
{ |
| 1699 |
va_list ap; |
| 1700 |
va_start (ap, fmt); |
| 1701 |
printf ("%d- ", n); |
| 1702 |
vprintf (fmt, ap); |
| 1703 |
va_end (ap); |
| 1704 |
printf ("\r\n"); |
| 1705 |
fflush (stdout); |
| 1706 |
if (debug) |
| 1707 |
{ |
| 1708 |
syslog (LOG_DEBUG, "<--- %d- ", n); |
| 1709 |
va_start (ap, fmt); |
| 1710 |
vsyslog (LOG_DEBUG, fmt, ap); |
| 1711 |
va_end (ap); |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
/* Send a possibly multiline reply as individual |
| 1716 |
* lines of message with identical status code. |
| 1717 |
* No format string input! |
| 1718 |
*/ |
| 1719 |
void |
| 1720 |
lreply_multiline (int n, const char *text) |
| 1721 |
{ |
| 1722 |
char *line; |
| 1723 |
|
| 1724 |
line = strdup (text); |
| 1725 |
if (line == NULL) |
| 1726 |
return; |
| 1727 |
else |
| 1728 |
{ |
| 1729 |
int stop = 0; |
| 1730 |
char *p1 = line, *p2; |
| 1731 |
|
| 1732 |
do |
| 1733 |
{ |
| 1734 |
p2 = strchrnul (p1, '\n'); |
| 1735 |
stop = (*p2 == '\0'); /* End of input string? */ |
| 1736 |
*p2 = '\0'; |
| 1737 |
printf ("%d- ", n); |
| 1738 |
printf ("%s\r\n", p1); |
| 1739 |
if (debug) |
| 1740 |
{ |
| 1741 |
syslog (LOG_DEBUG, "<--- %d- ", n); |
| 1742 |
syslog (LOG_DEBUG, "%s", p1); |
| 1743 |
} |
| 1744 |
p1 = ++p2; /* P1 is used within bounds. */ |
| 1745 |
} |
| 1746 |
while (!stop); |
| 1747 |
free (line); |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
static void |
| 1752 |
ack (const char *s) |
| 1753 |
{ |
| 1754 |
reply (250, "%s command successful.", s); |
| 1755 |
} |
| 1756 |
|
| 1757 |
void |
| 1758 |
nack (const char *s) |
| 1759 |
{ |
| 1760 |
reply (502, "%s command not implemented.", s); |
| 1761 |
} |
| 1762 |
|
| 1763 |
void |
| 1764 |
delete (const char *name) |
| 1765 |
{ |
| 1766 |
struct stat st; |
| 1767 |
|
| 1768 |
LOGCMD ("delete", name); |
| 1769 |
if (stat (name, &st) < 0) |
| 1770 |
{ |
| 1771 |
perror_reply (550, name); |
| 1772 |
return; |
| 1773 |
} |
| 1774 |
if (S_ISDIR (st.st_mode)) |
| 1775 |
{ |
| 1776 |
if (rmdir (name) < 0) |
| 1777 |
{ |
| 1778 |
perror_reply (550, name); |
| 1779 |
return; |
| 1780 |
} |
| 1781 |
goto done; |
| 1782 |
} |
| 1783 |
if (unlink (name) < 0) |
| 1784 |
{ |
| 1785 |
perror_reply (550, name); |
| 1786 |
return; |
| 1787 |
} |
| 1788 |
done: |
| 1789 |
ack ("DELE"); |
| 1790 |
} |
| 1791 |
|
| 1792 |
void |
| 1793 |
cwd (const char *path) |
| 1794 |
{ |
| 1795 |
if (chdir (path) < 0) |
| 1796 |
perror_reply (550, path); |
| 1797 |
else |
| 1798 |
ack ("CWD"); |
| 1799 |
} |
| 1800 |
|
| 1801 |
void |
| 1802 |
makedir (const char *name) |
| 1803 |
{ |
| 1804 |
LOGCMD ("mkdir", name); |
| 1805 |
if (mkdir (name, 0777) < 0) |
| 1806 |
perror_reply (550, name); |
| 1807 |
else if (name[0] == '/') |
| 1808 |
reply (257, "\"%s\" new directory created.", name); |
| 1809 |
else |
| 1810 |
{ |
| 1811 |
/* We have to figure out what our current directory is so that we can |
| 1812 |
give an absolute name in the reply. */ |
| 1813 |
char *current = xgetcwd (); |
| 1814 |
if (current) |
| 1815 |
{ |
| 1816 |
if (current[1] == '\0') |
| 1817 |
current[0] = '\0'; |
| 1818 |
reply (257, "\"%s/%s\" new directory created.", current, name); |
| 1819 |
free (current); |
| 1820 |
} |
| 1821 |
else |
| 1822 |
reply (257, "(unknown absolute name) new directory created."); |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
void |
| 1827 |
removedir (const char *name) |
| 1828 |
{ |
| 1829 |
LOGCMD ("rmdir", name); |
| 1830 |
if (rmdir (name) < 0) |
| 1831 |
perror_reply (550, name); |
| 1832 |
else |
| 1833 |
ack ("RMD"); |
| 1834 |
} |
| 1835 |
|
| 1836 |
void |
| 1837 |
pwd (void) |
| 1838 |
{ |
| 1839 |
char *path = xgetcwd (); |
| 1840 |
|
| 1841 |
if (path) |
| 1842 |
{ |
| 1843 |
reply (257, "\"%s\" is current directory.", path); |
| 1844 |
free (path); |
| 1845 |
} |
| 1846 |
else |
| 1847 |
reply (550, "%s.", strerror (errno)); |
| 1848 |
} |
| 1849 |
|
| 1850 |
char * |
| 1851 |
renamefrom (const char *name) |
| 1852 |
{ |
| 1853 |
struct stat st; |
| 1854 |
|
| 1855 |
if (stat (name, &st) < 0) |
| 1856 |
{ |
| 1857 |
perror_reply (550, name); |
| 1858 |
return ((char *) 0); |
| 1859 |
} |
| 1860 |
reply (350, "File exists, ready for destination name"); |
| 1861 |
return (char *) (name); |
| 1862 |
} |
| 1863 |
|
| 1864 |
void |
| 1865 |
renamecmd (const char *from, const char *to) |
| 1866 |
{ |
| 1867 |
LOGCMD2 ("rename", from, to); |
| 1868 |
if (rename (from, to) < 0) |
| 1869 |
perror_reply (550, "rename"); |
| 1870 |
else |
| 1871 |
ack ("RNTO"); |
| 1872 |
} |
| 1873 |
|
| 1874 |
static void |
| 1875 |
dolog (struct sockaddr *sa, socklen_t salen, struct credentials *pcred) |
| 1876 |
{ |
| 1877 |
(void) getnameinfo (sa, salen, addrstr, sizeof (addrstr), NULL, 0, 0); |
| 1878 |
|
| 1879 |
free (pcred->remotehost); |
| 1880 |
pcred->remotehost = sgetsave (addrstr); |
| 1881 |
|
| 1882 |
#ifdef HAVE_SETPROCTITLE |
| 1883 |
snprintf (proctitle, sizeof (proctitle), "%s: connected", |
| 1884 |
pcred->remotehost); |
| 1885 |
setproctitle ("%s", proctitle); |
| 1886 |
#endif /* HAVE_SETPROCTITLE */ |
| 1887 |
|
| 1888 |
if (logging) |
| 1889 |
syslog (LOG_INFO, "connection from %s", pcred->remotehost); |
| 1890 |
} |
| 1891 |
|
| 1892 |
/* Record logout in wtmp file |
| 1893 |
and exit with supplied status. */ |
| 1894 |
void |
| 1895 |
dologout (int status) |
| 1896 |
{ |
| 1897 |
/* Race condition with SIGURG: If SIGURG is received |
| 1898 |
here, it will jump back has root in the main loop. |
| 1899 |
David Greenman:[email protected]. */ |
| 1900 |
transflag = 0; |
| 1901 |
end_login (&cred); |
| 1902 |
|
| 1903 |
/* Beware of flushing buffers after a SIGPIPE. */ |
| 1904 |
_exit (status); |
| 1905 |
} |
| 1906 |
|
| 1907 |
static void |
| 1908 |
myoob (int signo MAYBE_UNUSED) |
| 1909 |
{ |
| 1910 |
char *cp; |
| 1911 |
|
| 1912 |
/* only process if transfer occurring */ |
| 1913 |
if (!transflag) |
| 1914 |
return; |
| 1915 |
cp = tmpline; |
| 1916 |
if (telnet_fgets (cp, 7, stdin) == NULL) |
| 1917 |
{ |
| 1918 |
reply (221, "You could at least say goodbye."); |
| 1919 |
dologout (0); |
| 1920 |
} |
| 1921 |
upper (cp); |
| 1922 |
if (strcmp (cp, "ABOR\r\n") == 0) |
| 1923 |
{ |
| 1924 |
tmpline[0] = '\0'; |
| 1925 |
reply (426, "Transfer aborted. Data connection closed."); |
| 1926 |
reply (226, "Abort successful"); |
| 1927 |
longjmp (urgcatch, 1); |
| 1928 |
} |
| 1929 |
if (strcmp (cp, "STAT\r\n") == 0) |
| 1930 |
{ |
| 1931 |
if (file_size != (off_t) - 1) |
| 1932 |
reply (213, "Status: %s of %s bytes transferred", |
| 1933 |
off_to_str (byte_count), off_to_str (file_size)); |
| 1934 |
else |
| 1935 |
reply (213, "Status: %s bytes transferred", off_to_str (byte_count)); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
/* Note: a response of 425 is not mentioned as a possible response to |
| 1940 |
the PASV command in RFC959. However, it has been blessed as |
| 1941 |
a legitimate response by Jon Postel in a telephone conversation |
| 1942 |
with Rick Adams on 25 Jan 89. */ |
| 1943 |
void |
| 1944 |
passive (int epsv, int af) |
| 1945 |
{ |
| 1946 |
char *p, *a; |
| 1947 |
int try_af; |
| 1948 |
|
| 1949 |
/* EPSV might ask for a particular address family. */ |
| 1950 |
if (epsv == PASSIVE_EPSV && af > 0) |
| 1951 |
try_af = af; |
| 1952 |
else |
| 1953 |
try_af = ctrl_addr.ss_family; |
| 1954 |
|
| 1955 |
pdata = socket (try_af, SOCK_STREAM, 0); |
| 1956 |
if (pdata < 0) |
| 1957 |
{ |
| 1958 |
perror_reply (425, "Can't open passive connection"); |
| 1959 |
return; |
| 1960 |
} |
| 1961 |
memcpy (&pasv_addr, &ctrl_addr, sizeof (pasv_addr)); |
| 1962 |
pasv_addrlen = ctrl_addrlen; |
| 1963 |
|
| 1964 |
/* Erase the port number. */ |
| 1965 |
if (pasv_addr.ss_family == AF_INET6) |
| 1966 |
((struct sockaddr_in6 *) &pasv_addr)->sin6_port = 0; |
| 1967 |
else /* !AF_INET6 */ |
| 1968 |
((struct sockaddr_in *) &pasv_addr)->sin_port = 0; |
| 1969 |
|
| 1970 |
if (seteuid ((uid_t) 0) == -1) |
| 1971 |
_exit (EXIT_FAILURE); |
| 1972 |
if (bind (pdata, (struct sockaddr *) &pasv_addr, pasv_addrlen) < 0) |
| 1973 |
{ |
| 1974 |
if (seteuid ((uid_t) cred.uid)) |
| 1975 |
_exit (EXIT_FAILURE); |
| 1976 |
goto pasv_error; |
| 1977 |
} |
| 1978 |
if (seteuid ((uid_t) cred.uid)) |
| 1979 |
_exit (EXIT_FAILURE); |
| 1980 |
pasv_addrlen = sizeof (pasv_addr); |
| 1981 |
if (getsockname (pdata, (struct sockaddr *) &pasv_addr, &pasv_addrlen) < 0) |
| 1982 |
goto pasv_error; |
| 1983 |
if (listen (pdata, 1) < 0) |
| 1984 |
goto pasv_error; |
| 1985 |
|
| 1986 |
if (epsv == PASSIVE_EPSV) |
| 1987 |
{ |
| 1988 |
/* EPSV for IPv4 and IPv6. */ |
| 1989 |
reply (229, "Entering Extended Passive Mode (|||%u|)", |
| 1990 |
ntohs ((pasv_addr.ss_family == AF_INET) |
| 1991 |
? ((struct sockaddr_in *) &pasv_addr)->sin_port |
| 1992 |
: ((struct sockaddr_in6 *) &pasv_addr)->sin6_port)); |
| 1993 |
return; |
| 1994 |
} |
| 1995 |
else /* !EPSV */ |
| 1996 |
{ |
| 1997 |
/* PASV for IPv4, or LPSV for IPv4 or IPv6. |
| 1998 |
* |
| 1999 |
* Some systems, like OpenSolaris, prefer to return |
| 2000 |
* an IPv4-mapped-IPv6 address, which must be processed |
| 2001 |
* for printout. */ |
| 2002 |
|
| 2003 |
#define UC(b) (((int) b) & 0xff) |
| 2004 |
|
| 2005 |
if (pasv_addr.ss_family == AF_INET6 |
| 2006 |
&& |
| 2007 |
IN6_IS_ADDR_V4MAPPED (& |
| 2008 |
((struct sockaddr_in6 *) |
| 2009 |
&pasv_addr)->sin6_addr)) |
| 2010 |
{ |
| 2011 |
a = (char *) &((struct sockaddr_in6 *) &pasv_addr)->sin6_addr; |
| 2012 |
a += 3 * sizeof (struct in_addr); /* Skip padding up to IPv4 content. */ |
| 2013 |
p = (char *) &((struct sockaddr_in6 *) &pasv_addr)->sin6_port; |
| 2014 |
} |
| 2015 |
else if (pasv_addr.ss_family == AF_INET6) |
| 2016 |
{ |
| 2017 |
/* LPSV for IPv6, not mapped. */ |
| 2018 |
a = (char *) &((struct sockaddr_in6 *) &pasv_addr)->sin6_addr; |
| 2019 |
p = (char *) &((struct sockaddr_in6 *) &pasv_addr)->sin6_port; |
| 2020 |
|
| 2021 |
reply (228, "Entering Long Passive Mode " "(6,16,%d,%d,%d,%d,%d,%d,%d,%d" /* a[0..7] */ |
| 2022 |
",%d,%d,%d,%d,%d,%d,%d,%d" /* a[8..15] */ |
| 2023 |
",2,%d,%d)", /* p0, p1 */ |
| 2024 |
UC (a[0]), UC (a[1]), UC (a[2]), UC (a[3]), |
| 2025 |
UC (a[4]), UC (a[5]), UC (a[6]), UC (a[7]), |
| 2026 |
UC (a[8]), UC (a[9]), UC (a[10]), UC (a[11]), |
| 2027 |
UC (a[12]), UC (a[13]), UC (a[14]), UC (a[15]), |
| 2028 |
UC (p[0]), UC (p[1])); |
| 2029 |
return; |
| 2030 |
} |
| 2031 |
else |
| 2032 |
{ |
| 2033 |
a = (char *) &((struct sockaddr_in *) &pasv_addr)->sin_addr; |
| 2034 |
p = (char *) &((struct sockaddr_in *) &pasv_addr)->sin_port; |
| 2035 |
} |
| 2036 |
|
| 2037 |
if (epsv == PASSIVE_LPSV) |
| 2038 |
reply (228, "Entering Long Passive Mode " |
| 2039 |
"(4,4,%d,%d,%d,%d,2,%d,%d)", |
| 2040 |
UC (a[0]), UC (a[1]), UC (a[2]), UC (a[3]), |
| 2041 |
UC (p[0]), UC (p[1])); |
| 2042 |
else |
| 2043 |
reply (227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", |
| 2044 |
UC (a[0]), UC (a[1]), UC (a[2]), UC (a[3]), |
| 2045 |
UC (p[0]), UC (p[1])); |
| 2046 |
return; |
| 2047 |
} |
| 2048 |
|
| 2049 |
pasv_error: |
| 2050 |
close (pdata); |
| 2051 |
pdata = -1; |
| 2052 |
perror_reply (425, "Can't open passive connection"); |
| 2053 |
return; |
| 2054 |
} |
| 2055 |
|
| 2056 |
/* Generate unique name for file with basename "local". |
| 2057 |
The file named "local" is already known to exist. |
| 2058 |
Generates failure reply on error. */ |
| 2059 |
static char * |
| 2060 |
gunique (const char *local) |
| 2061 |
{ |
| 2062 |
static char *string = 0; |
| 2063 |
struct stat st; |
| 2064 |
int count; |
| 2065 |
char *cp; |
| 2066 |
|
| 2067 |
cp = strrchr (local, '/'); |
| 2068 |
if (cp) |
| 2069 |
*cp = '\0'; |
| 2070 |
if (stat (cp ? local : ".", &st) < 0) |
| 2071 |
{ |
| 2072 |
perror_reply (553, cp ? local : "."); |
| 2073 |
return ((char *) 0); |
| 2074 |
} |
| 2075 |
if (cp) |
| 2076 |
*cp = '/'; |
| 2077 |
|
| 2078 |
free (string); |
| 2079 |
string = malloc (strlen (local) + 5); /* '.' + DIG + DIG + '\0' */ |
| 2080 |
if (string) |
| 2081 |
{ |
| 2082 |
strcpy (string, local); |
| 2083 |
cp = string + strlen (string); |
| 2084 |
*cp++ = '.'; |
| 2085 |
for (count = 1; count < 100; count++) |
| 2086 |
{ |
| 2087 |
sprintf (cp, "%d", count); |
| 2088 |
if (stat (string, &st) < 0) |
| 2089 |
return string; |
| 2090 |
} |
| 2091 |
} |
| 2092 |
|
| 2093 |
reply (452, "Unique file name cannot be created."); |
| 2094 |
return NULL; |
| 2095 |
} |
| 2096 |
|
| 2097 |
/* |
| 2098 |
* Format and send reply containing system error number. |
| 2099 |
*/ |
| 2100 |
void |
| 2101 |
perror_reply (int code, const char *string) |
| 2102 |
{ |
| 2103 |
reply (code, "%s: %s.", string, strerror (errno)); |
| 2104 |
} |
| 2105 |
|
| 2106 |
static char *onefile[] = { |
| 2107 |
"", |
| 2108 |
0 |
| 2109 |
}; |
| 2110 |
|
| 2111 |
void |
| 2112 |
send_file_list (const char *whichf) |
| 2113 |
{ |
| 2114 |
struct stat st; |
| 2115 |
DIR *dirp = NULL; |
| 2116 |
struct dirent *dir; |
| 2117 |
FILE *dout = NULL; |
| 2118 |
char **dirlist, *dirname; |
| 2119 |
int simple = 0; |
| 2120 |
int freeglob = 0; |
| 2121 |
glob_t gl; |
| 2122 |
char *p = NULL; |
| 2123 |
|
| 2124 |
if (strpbrk (whichf, "~{[*?") != NULL) |
| 2125 |
{ |
| 2126 |
int flags = GLOB_NOCHECK; |
| 2127 |
|
| 2128 |
#ifdef GLOB_BRACE |
| 2129 |
flags |= GLOB_BRACE; |
| 2130 |
#endif |
| 2131 |
#ifdef GLOB_QUOTE |
| 2132 |
flags |= GLOB_QUOTE; |
| 2133 |
#endif |
| 2134 |
#ifdef GLOB_TILDE |
| 2135 |
flags |= GLOB_TILDE; |
| 2136 |
#endif |
| 2137 |
|
| 2138 |
memset (&gl, 0, sizeof (gl)); |
| 2139 |
freeglob = 1; |
| 2140 |
if (glob (whichf, flags, 0, &gl)) |
| 2141 |
{ |
| 2142 |
reply (550, "not found"); |
| 2143 |
goto out; |
| 2144 |
} |
| 2145 |
else if (gl.gl_pathc == 0) |
| 2146 |
{ |
| 2147 |
errno = ENOENT; |
| 2148 |
perror_reply (550, whichf); |
| 2149 |
goto out; |
| 2150 |
} |
| 2151 |
dirlist = gl.gl_pathv; |
| 2152 |
} |
| 2153 |
else |
| 2154 |
{ |
| 2155 |
p = strdup (whichf); |
| 2156 |
onefile[0] = p; |
| 2157 |
dirlist = onefile; |
| 2158 |
simple = 1; |
| 2159 |
} |
| 2160 |
|
| 2161 |
if (setjmp (urgcatch)) |
| 2162 |
{ |
| 2163 |
transflag = 0; |
| 2164 |
goto out; |
| 2165 |
} |
| 2166 |
while ((dirname = *dirlist++)) |
| 2167 |
{ |
| 2168 |
if (stat (dirname, &st) < 0) |
| 2169 |
{ |
| 2170 |
/* If user typed "ls -l", etc, and the client |
| 2171 |
used NLST, do what the user meant. */ |
| 2172 |
if (dirname[0] == '-' && *dirlist == NULL && transflag == 0) |
| 2173 |
{ |
| 2174 |
retrieve ("/bin/ls %s", dirname); |
| 2175 |
goto out; |
| 2176 |
} |
| 2177 |
perror_reply (550, whichf); |
| 2178 |
if (dout != NULL) |
| 2179 |
{ |
| 2180 |
fclose (dout); |
| 2181 |
transflag = 0; |
| 2182 |
data = -1; |
| 2183 |
pdata = -1; |
| 2184 |
} |
| 2185 |
goto out; |
| 2186 |
} |
| 2187 |
|
| 2188 |
if (S_ISREG (st.st_mode)) |
| 2189 |
{ |
| 2190 |
if (dout == NULL) |
| 2191 |
{ |
| 2192 |
dout = dataconn ("file list", (off_t) - 1, "w"); |
| 2193 |
if (dout == NULL) |
| 2194 |
goto out; |
| 2195 |
transflag++; |
| 2196 |
} |
| 2197 |
fprintf (dout, "%s%s\n", dirname, type == TYPE_A ? "\r" : ""); |
| 2198 |
byte_count += strlen (dirname) + 1; |
| 2199 |
continue; |
| 2200 |
} |
| 2201 |
else if (!S_ISDIR (st.st_mode)) |
| 2202 |
continue; |
| 2203 |
|
| 2204 |
dirp = opendir (dirname); |
| 2205 |
if (dirp == NULL) |
| 2206 |
continue; |
| 2207 |
|
| 2208 |
while ((dir = readdir (dirp)) != NULL) |
| 2209 |
{ |
| 2210 |
char *nbuf; |
| 2211 |
|
| 2212 |
if (dir->d_name[0] == '.' && dir->d_name[1] == '\0') |
| 2213 |
continue; |
| 2214 |
if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && |
| 2215 |
dir->d_name[2] == '\0') |
| 2216 |
continue; |
| 2217 |
|
| 2218 |
nbuf = alloca (strlen (dirname) + 1 + strlen (dir->d_name) + 1); |
| 2219 |
sprintf (nbuf, "%s/%s", dirname, dir->d_name); |
| 2220 |
|
| 2221 |
/* We have to do a stat to insure it's |
| 2222 |
not a directory or special file. */ |
| 2223 |
if (simple || (stat (nbuf, &st) == 0 && S_ISREG (st.st_mode))) |
| 2224 |
{ |
| 2225 |
if (dout == NULL) |
| 2226 |
{ |
| 2227 |
dout = dataconn ("file list", (off_t) - 1, "w"); |
| 2228 |
if (dout == NULL) |
| 2229 |
goto out; |
| 2230 |
transflag++; |
| 2231 |
} |
| 2232 |
if (nbuf[0] == '.' && nbuf[1] == '/') |
| 2233 |
fprintf (dout, "%s%s\n", &nbuf[2], |
| 2234 |
type == TYPE_A ? "\r" : ""); |
| 2235 |
else |
| 2236 |
fprintf (dout, "%s%s\n", nbuf, type == TYPE_A ? "\r" : ""); |
| 2237 |
byte_count += strlen (nbuf) + 1; |
| 2238 |
} |
| 2239 |
} |
| 2240 |
closedir (dirp); |
| 2241 |
} |
| 2242 |
|
| 2243 |
if (dout == NULL) |
| 2244 |
reply (550, "No files found."); |
| 2245 |
else if (ferror (dout) != 0) |
| 2246 |
perror_reply (550, "Data connection"); |
| 2247 |
else |
| 2248 |
reply (226, "Transfer complete."); |
| 2249 |
|
| 2250 |
transflag = 0; |
| 2251 |
if (dout != NULL) |
| 2252 |
fclose (dout); |
| 2253 |
data = -1; |
| 2254 |
pdata = -1; |
| 2255 |
out: |
| 2256 |
free (p); |
| 2257 |
if (freeglob) |
| 2258 |
{ |
| 2259 |
freeglob = 0; |
| 2260 |
globfree (&gl); |
| 2261 |
} |
| 2262 |
} |