PROJECTVERSION = 2.8
PROJECT_TYPE = Tool
-HFILES = extern.h pathnames.h
+HFILES = ls.h ls_extern.h extern.h pathnames.h
OTHERLINKED = ftpcmd.y
-CFILES = ftpd.c logwtmp.c popen.c vers.c
+CFILES = ftpd.c logwtmp.c popen.c ls.c util.c cmp.c print.c
OTHERSRCS = Makefile.preamble Makefile Makefile.postamble ftpd.8
## (e.g. change -O to -O2), see Makefile.postamble.
# Flags passed to compiler (in addition to -g, -O, etc)
-OTHER_CFLAGS =
+OTHER_CFLAGS = -Dmain=ls_main
# Flags passed to ld (in addition to -ObjC, etc.)
OTHER_LDFLAGS =
# Flags passed to libtool when building libraries
{
FILESTABLE = {
C_FILES = ();
- H_FILES = (extern.h, pathnames.h);
+ H_FILES = (ls.h, ls_extern.h, extern.h, pathnames.h);
M_FILES = ();
OTHER_LIBS = ();
- OTHER_LINKED = (ftpcmd.y, ftpd.c, logwtmp.c, popen.c, vers.c);
+ OTHER_LINKED = (ftpcmd.y, ftpd.c, logwtmp.c, popen.c, vers.c, ls.c, cmp.c, util.c, print.c);
OTHER_SOURCES = (Makefile.preamble, Makefile, Makefile.postamble, ftpd.8);
PRECOMPILED_HEADERS = ();
PROJECT_HEADERS = ();
--- /dev/null
+/*
+ * Copyright (c) 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93";
+#else
+static const char rcsid[] =
+ "$FreeBSD: src/bin/ls/cmp.c,v 1.9 1999/08/27 23:14:31 peter Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fts.h>
+#include <string.h>
+
+#include "ls.h"
+#include "ls_extern.h"
+
+int
+namecmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (strcoll(a->fts_name, b->fts_name));
+}
+
+int
+revnamecmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (strcoll(b->fts_name, a->fts_name));
+}
+
+int
+modcmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (b->fts_statp->st_mtime - a->fts_statp->st_mtime);
+}
+
+int
+revmodcmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (a->fts_statp->st_mtime - b->fts_statp->st_mtime);
+}
+
+int
+acccmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (b->fts_statp->st_atime - a->fts_statp->st_atime);
+}
+
+int
+revacccmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (a->fts_statp->st_atime - b->fts_statp->st_atime);
+}
+
+int
+statcmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (b->fts_statp->st_ctime - a->fts_statp->st_ctime);
+}
+
+int
+revstatcmp(a, b)
+ const FTSENT *a, *b;
+{
+ return (a->fts_statp->st_ctime - b->fts_statp->st_ctime);
+}
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* SUCH DAMAGE.
*
* @(#)extern.h 8.2 (Berkeley) 4/4/94
+ * $FreeBSD: src/libexec/ftpd/extern.h,v 1.14 2000/01/27 09:28:19 shin Exp $
*/
void blkfree __P((char **));
void delete __P((char *));
void dologout __P((int));
void fatal __P((char *));
+void ftpd_logwtmp __P((char *, char *, char *));
int ftpd_pclose __P((FILE *));
FILE *ftpd_popen __P((char *, char *));
char *getline __P((char *, int, FILE *));
-void logwtmp __P((char *, char *, char *));
void lreply __P((int, const char *, ...));
void makedir __P((char *));
void nack __P((char *));
void pass __P((char *));
void passive __P((void));
+void long_passive __P((char *, int));
void perror_reply __P((int, char *));
void pwd __P((void));
void removedir __P((char *));
void reply __P((int, const char *, ...));
void retrieve __P((char *, char *));
void send_file_list __P((char *));
+#ifdef OLD_SETPROCTITLE
void setproctitle __P((const char *, ...));
+#endif
void statcmd __P((void));
void statfilecmd __P((char *));
void store __P((char *, char *, int));
void upper __P((char *));
void user __P((char *));
void yyerror __P((char *));
+int yyparse __P((void));
+#if defined(SKEY) && defined(_PWD_H_) /* XXX evil */
+char *skey_challenge __P((char *, struct passwd *, int));
+#endif
+int ls_main __P((int, char **));
+
+struct sockaddr_in;
+struct sockaddr_in6;
+union sockunion {
+ struct sockinet {
+ u_char si_len;
+ u_char si_family;
+ u_short si_port;
+ } su_si;
+ struct sockaddr_in su_sin;
+ struct sockaddr_in6 su_sin6;
+};
+#define su_len su_si.si_len
+#define su_family su_si.si_family
+#define su_port su_si.si_port
%{
#ifndef lint
+#if 0
static char sccsid[] = "@(#)ftpcmd.y 8.3 (Berkeley) 4/6/94";
+#endif
+static const char rcsid[] =
+ "$FreeBSD: src/libexec/ftpd/ftpcmd.y,v 1.21 2001/02/19 21:51:26 des Exp $";
#endif /* not lint */
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#include <glob.h>
+#include <netdb.h>
#include <pwd.h>
#include <setjmp.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
+//#include <libutil.h>
#include "extern.h"
-extern struct sockaddr_in data_dest;
+extern union sockunion data_dest, his_addr;
extern int logged_in;
extern struct passwd *pw;
extern int guest;
+extern int paranoid;
extern int logging;
extern int type;
extern int form;
extern int timeout;
extern int maxtimeout;
extern int pdata;
-extern char hostname[], remotehost[];
+extern char *hostname;
+extern char remotehost[];
extern char proctitle[];
extern int usedefault;
extern int transflag;
extern char tmpline[];
+extern int readonly;
+extern int noepsv;
off_t restart_point;
char cbuf[512];
char *fromname;
+#if defined(VIRTUAL_HOSTING)
+extern int epsvall;
+#endif
+
%}
%union {
%token
A B C E F I
L N P R S T
+ ALL
SP CRLF COMMA
ABOR DELE CWD LIST NLST SITE
STAT HELP NOOP MKD RMD PWD
CDUP STOU SMNT SYST SIZE MDTM
+ LPRT LPSV EPRT EPSV
UMASK IDLE CHMOD
%token <i> NUMBER
%type <i> check_login octal_number byte_size
+%type <i> check_login_ro octal_number byte_size
+%type <i> check_login_epsv octal_number byte_size
%type <i> struct_code mode_code type_code form_code
-%type <s> pathstring pathname password username
+%type <s> pathstring pathname password username ext_arg
+%type <s> ALL
%start cmd_list
pass($3);
free($3);
}
- | PORT SP host_port CRLF
+ | PORT check_login SP host_port CRLF
{
- usedefault = 0;
- if (pdata >= 0) {
- (void) close(pdata);
- pdata = -1;
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall) {
+ reply(501, "no PORT allowed after EPSV ALL");
+ goto port_done;
+ }
+#endif
+ if (!$2)
+ goto port_done;
+ if (port_check("PORT") == 1)
+ goto port_done;
+#ifdef INET6
+ if ((his_addr.su_family != AF_INET6 ||
+ !IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))) {
+ /* shoud never happen */
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ goto port_done;
+ }
+ port_check_v6("pcmd");
+#endif
+ port_done:
+ }
+ | LPRT check_login SP host_long_port CRLF
+ {
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall) {
+ reply(501, "no LPRT allowed after EPSV ALL");
+ goto lprt_done;
+ }
+#endif
+ if (!$2)
+ goto lprt_done;
+ if (port_check("LPRT") == 1)
+ goto lprt_done;
+#ifdef INET6
+ if (his_addr.su_family != AF_INET6) {
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ goto lprt_done;
+ }
+ if (port_check_v6("LPRT") == 1)
+ goto lprt_done;
+#endif
+ lprt_done:
+ }
+ | EPRT check_login SP STRING CRLF
+ {
+ char delim;
+ char *tmp = NULL;
+ char *p, *q;
+ char *result[3];
+ struct addrinfo hints;
+ struct addrinfo *res;
+ int i;
+
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall) {
+ reply(501, "no EPRT allowed after EPSV ALL");
+ goto eprt_done;
+ }
+#endif
+ if (!$2)
+ goto eprt_done;
+
+ memset(&data_dest, 0, sizeof(data_dest));
+ tmp = strdup($4);
+ if (debug)
+ syslog(LOG_DEBUG, "%s", tmp);
+ if (!tmp) {
+ fatal("not enough core");
+ /*NOTREACHED*/
+ }
+ p = tmp;
+ delim = p[0];
+ p++;
+ memset(result, 0, sizeof(result));
+ for (i = 0; i < 3; i++) {
+ q = strchr(p, delim);
+ if (!q || *q != delim) {
+ parsefail:
+ reply(500,
+ "Invalid argument, rejected.");
+ if (tmp)
+ free(tmp);
+ usedefault = 1;
+ goto eprt_done;
+ }
+ *q++ = '\0';
+ result[i] = p;
+ if (debug)
+ syslog(LOG_DEBUG, "%d: %s", i, p);
+ p = q;
+ }
+
+ /* some more sanity check */
+ p = result[0];
+ while (*p) {
+ if (!isdigit(*p))
+ goto parsefail;
+ p++;
+ }
+ p = result[2];
+ while (*p) {
+ if (!isdigit(*p))
+ goto parsefail;
+ p++;
}
- reply(200, "PORT command successful.");
+
+ /* grab address */
+ memset(&hints, 0, sizeof(hints));
+ if (atoi(result[0]) == 1)
+ hints.ai_family = PF_INET;
+#ifdef INET6
+ else if (atoi(result[0]) == 2)
+ hints.ai_family = PF_INET6;
+#endif
+ else
+ hints.ai_family = PF_UNSPEC; /*XXX*/
+ hints.ai_socktype = SOCK_STREAM;
+ i = getaddrinfo(result[1], result[2], &hints, &res);
+ if (i)
+ goto parsefail;
+ memcpy(&data_dest, res->ai_addr, res->ai_addrlen);
+#ifdef INET6
+ if (his_addr.su_family == AF_INET6
+ && data_dest.su_family == AF_INET6) {
+ /* XXX more sanity checks! */
+ data_dest.su_sin6.sin6_scope_id =
+ his_addr.su_sin6.sin6_scope_id;
+ }
+#endif
+ free(tmp);
+ tmp = NULL;
+
+ if (port_check("EPRT") == 1)
+ goto eprt_done;
+#ifdef INET6
+ if (his_addr.su_family != AF_INET6) {
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ goto eprt_done;
+ }
+ if (port_check_v6("EPRT") == 1)
+ goto eprt_done;
+#endif
+ eprt_done:;
+ }
+ | PASV check_login CRLF
+ {
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall)
+ reply(501, "no PASV allowed after EPSV ALL");
+ else
+#endif
+ if ($2)
+ passive();
+ }
+ | LPSV check_login CRLF
+ {
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall)
+ reply(501, "no LPSV allowed after EPSV ALL");
+ else
+#endif
+ if ($2)
+ long_passive("LPSV", PF_UNSPEC);
}
- | PASV CRLF
+ | EPSV check_login_epsv SP NUMBER CRLF
{
- passive();
+ if ($2) {
+ int pf;
+ switch ($4) {
+ case 1:
+ pf = PF_INET;
+ break;
+#ifdef INET6
+ case 2:
+ pf = PF_INET6;
+ break;
+#endif
+ default:
+ pf = -1; /*junk value*/
+ break;
+ }
+ long_passive("EPSV", pf);
+ }
}
- | TYPE SP type_code CRLF
+ | EPSV check_login_epsv SP ALL CRLF
{
- switch (cmd_type) {
+ if ($2) {
+ reply(200,
+ "EPSV ALL command successful.");
+#if defined(VIRTUAL_HOSTING)
+ epsvall++;
+#endif
+ }
+ }
+ | EPSV check_login_epsv CRLF
+ {
+ if ($2)
+ long_passive("EPSV", PF_UNSPEC);
+ }
+ | TYPE check_login SP type_code CRLF
+ {
+ if ($2) {
+ switch (cmd_type) {
- case TYPE_A:
- if (cmd_form == FORM_N) {
- reply(200, "Type set to A.");
- type = cmd_type;
- form = cmd_form;
- } else
- reply(504, "Form must be N.");
- break;
+ case TYPE_A:
+ if (cmd_form == FORM_N) {
+ reply(200, "Type set to A.");
+ type = cmd_type;
+ form = cmd_form;
+ } else
+ reply(504, "Form must be N.");
+ break;
- case TYPE_E:
- reply(504, "Type E not implemented.");
- break;
+ case TYPE_E:
+ reply(504, "Type E not implemented.");
+ break;
- case TYPE_I:
- reply(200, "Type set to I.");
- type = cmd_type;
- break;
+ case TYPE_I:
+ reply(200, "Type set to I.");
+ type = cmd_type;
+ break;
- case TYPE_L:
+ case TYPE_L:
#if NBBY == 8
- if (cmd_bytesz == 8) {
- reply(200,
- "Type set to L (byte size 8).");
- type = cmd_type;
- } else
- reply(504, "Byte size must be 8.");
+ if (cmd_bytesz == 8) {
+ reply(200,
+ "Type set to L (byte size 8).");
+ type = cmd_type;
+ } else
+ reply(504, "Byte size must be 8.");
#else /* NBBY == 8 */
- UNIMPLEMENTED for NBBY != 8
+ UNIMPLEMENTED for NBBY != 8
#endif /* NBBY == 8 */
+ }
}
}
- | STRU SP struct_code CRLF
+ | STRU check_login SP struct_code CRLF
{
- switch ($3) {
+ if ($2) {
+ switch ($4) {
- case STRU_F:
- reply(200, "STRU F ok.");
- break;
+ case STRU_F:
+ reply(200, "STRU F ok.");
+ break;
- default:
- reply(504, "Unimplemented STRU type.");
+ default:
+ reply(504, "Unimplemented STRU type.");
+ }
}
}
- | MODE SP mode_code CRLF
+ | MODE check_login SP mode_code CRLF
{
- switch ($3) {
+ if ($2) {
+ switch ($4) {
- case MODE_S:
- reply(200, "MODE S ok.");
- break;
-
- default:
- reply(502, "Unimplemented MODE type.");
+ case MODE_S:
+ reply(200, "MODE S ok.");
+ break;
+
+ default:
+ reply(502, "Unimplemented MODE type.");
+ }
}
}
- | ALLO SP NUMBER CRLF
+ | ALLO check_login SP NUMBER CRLF
{
- reply(202, "ALLO command ignored.");
+ if ($2) {
+ reply(202, "ALLO command ignored.");
+ }
}
- | ALLO SP NUMBER SP R SP NUMBER CRLF
+ | ALLO check_login SP NUMBER SP R SP NUMBER CRLF
{
- reply(202, "ALLO command ignored.");
+ if ($2) {
+ reply(202, "ALLO command ignored.");
+ }
}
| RETR check_login SP pathname CRLF
{
if ($4 != NULL)
free($4);
}
- | STOR check_login SP pathname CRLF
+ | STOR check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
store($4, "w", 0);
if ($4 != NULL)
free($4);
}
- | APPE check_login SP pathname CRLF
+ | APPE check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
store($4, "a", 0);
if ($4 != NULL)
free($4);
}
- | STAT CRLF
+ | STAT check_login CRLF
{
- statcmd();
+ if ($2) {
+ statcmd();
+ }
}
- | DELE check_login SP pathname CRLF
+ | DELE check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
delete($4);
if ($4 != NULL)
free($4);
}
- | RNTO SP pathname CRLF
+ | RNTO check_login_ro SP pathname CRLF
{
- if (fromname) {
- renamecmd(fromname, $3);
- free(fromname);
- fromname = (char *) 0;
- } else {
- reply(503, "Bad sequence of commands.");
+ if ($2) {
+ if (fromname) {
+ renamecmd(fromname, $4);
+ free(fromname);
+ fromname = (char *) 0;
+ } else {
+ reply(503, "Bad sequence of commands.");
+ }
}
- free($3);
+ free($4);
}
- | ABOR CRLF
+ | ABOR check_login CRLF
{
- reply(225, "ABOR command successful.");
+ if ($2)
+ reply(225, "ABOR command successful.");
}
| CWD check_login CRLF
{
- if ($2)
- cwd(pw->pw_dir);
+ if ($2) {
+ if (guest)
+ cwd("/");
+ else
+ cwd(pw->pw_dir);
+ }
}
| CWD check_login SP pathname CRLF
{
{
reply(200, "NOOP command successful.");
}
- | MKD check_login SP pathname CRLF
+ | MKD check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
makedir($4);
if ($4 != NULL)
free($4);
}
- | RMD check_login SP pathname CRLF
+ | RMD check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
removedir($4);
}
}
}
- | SITE SP CHMOD check_login SP octal_number SP pathname CRLF
+ | SITE SP CHMOD check_login_ro SP octal_number SP pathname CRLF
{
if ($4 && ($8 != NULL)) {
if ($6 > 0777)
if ($8 != NULL)
free($8);
}
- | SITE SP IDLE CRLF
+ | SITE SP check_login IDLE CRLF
{
- reply(200,
- "Current IDLE time limit is %d seconds; max %d",
- timeout, maxtimeout);
+ if ($3)
+ reply(200,
+ "Current IDLE time limit is %d seconds; max %d",
+ timeout, maxtimeout);
}
- | SITE SP IDLE SP NUMBER CRLF
+ | SITE SP check_login IDLE SP NUMBER CRLF
{
- if ($5 < 30 || $5 > maxtimeout) {
- reply(501,
- "Maximum IDLE time must be between 30 and %d seconds",
- maxtimeout);
- } else {
- timeout = $5;
- (void) alarm((unsigned) timeout);
- reply(200,
- "Maximum IDLE time set to %d seconds",
- timeout);
+ if ($3) {
+ if ($6 < 30 || $6 > maxtimeout) {
+ reply(501,
+ "Maximum IDLE time must be between 30 and %d seconds",
+ maxtimeout);
+ } else {
+ timeout = $6;
+ (void) alarm((unsigned) timeout);
+ reply(200,
+ "Maximum IDLE time set to %d seconds",
+ timeout);
+ }
}
}
- | STOU check_login SP pathname CRLF
+ | STOU check_login_ro SP pathname CRLF
{
if ($2 && $4 != NULL)
store($4, "w", 1);
if ($4 != NULL)
free($4);
}
- | SYST CRLF
+ | SYST check_login CRLF
{
-#ifdef __APPLE__
- reply(215, "BSD Type: L%d", NBBY);
-#else
-#if defined(unix)
+ if ($2)
+#ifdef unix
#ifdef BSD
- reply(215, "UNIX Type: L%d Version: BSD-%d", NBBY, BSD);
+ reply(215, "UNIX Type: L%d Version: BSD-%d",
+ NBBY, BSD);
#else /* BSD */
reply(215, "UNIX Type: L%d", NBBY);
#endif /* BSD */
#else /* unix */
reply(215, "UNKNOWN Type: L%d", NBBY);
#endif /* unix */
-#endif /* __APPLE__ */
}
/*
t = gmtime(&stbuf.st_mtime);
reply(213,
"%04d%02d%02d%02d%02d%02d",
- 1900+t->tm_year, t->tm_mon+1, t->tm_mday,
+ 1900 + t->tm_year,
+ t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
}
}
}
;
rcmd
- : RNFR check_login SP pathname CRLF
+ : RNFR check_login_ro SP pathname CRLF
{
char *renamefrom();
}
}
}
- | REST SP byte_size CRLF
+ | REST check_login SP byte_size CRLF
{
- fromname = (char *) 0;
- restart_point = $3; /* XXX $3 is only "int" */
- reply(350, "Restarting at %qd. %s", restart_point,
- "Send STORE or RETRIEVE to initiate transfer.");
+ if ($2) {
+ fromname = (char *) 0;
+ restart_point = $4; /* XXX $4 is only "int" */
+ reply(350, "Restarting at %qd. %s",
+ restart_point,
+ "Send STORE or RETRIEVE to initiate transfer.");
+ }
}
;
{
char *a, *p;
- a = (char *)&data_dest.sin_addr;
- a[0] = $1; a[1] = $3; a[2] = $5; a[3] = $7;
- p = (char *)&data_dest.sin_port;
+ data_dest.su_len = sizeof(struct sockaddr_in);
+ data_dest.su_family = AF_INET;
+ p = (char *)&data_dest.su_sin.sin_port;
p[0] = $9; p[1] = $11;
- data_dest.sin_family = AF_INET;
+ a = (char *)&data_dest.su_sin.sin_addr;
+ a[0] = $1; a[1] = $3; a[2] = $5; a[3] = $7;
+ }
+ ;
+
+host_long_port
+ : NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER
+ {
+ char *a, *p;
+
+ memset(&data_dest, 0, sizeof(data_dest));
+ data_dest.su_len = sizeof(struct sockaddr_in6);
+ data_dest.su_family = AF_INET6;
+ p = (char *)&data_dest.su_port;
+ p[0] = $39; p[1] = $41;
+ a = (char *)&data_dest.su_sin6.sin6_addr;
+ a[0] = $5; a[1] = $7; a[2] = $9; a[3] = $11;
+ a[4] = $13; a[5] = $15; a[6] = $17; a[7] = $19;
+ a[8] = $21; a[9] = $23; a[10] = $25; a[11] = $27;
+ a[12] = $29; a[13] = $31; a[14] = $33; a[15] = $35;
+ if (his_addr.su_family == AF_INET6) {
+ /* XXX more sanity checks! */
+ data_dest.su_sin6.sin6_scope_id =
+ his_addr.su_sin6.sin6_scope_id;
+ }
+ if ($1 != 6 || $3 != 16 || $37 != 2)
+ memset(&data_dest, 0, sizeof(data_dest));
+ }
+ | NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
+ NUMBER
+ {
+ char *a, *p;
+
+ memset(&data_dest, 0, sizeof(data_dest));
+ data_dest.su_sin.sin_len = sizeof(struct sockaddr_in);
+ data_dest.su_family = AF_INET;
+ p = (char *)&data_dest.su_port;
+ p[0] = $15; p[1] = $17;
+ a = (char *)&data_dest.su_sin.sin_addr;
+ a[0] = $5; a[1] = $7; a[2] = $9; a[3] = $11;
+ if ($1 != 4 || $3 != 4 || $13 != 2)
+ memset(&data_dest, 0, sizeof(data_dest));
}
;
check_login
: /* empty */
{
- if (logged_in)
- $$ = 1;
- else {
- reply(530, "Please login with USER and PASS.");
- $$ = 0;
- }
+ $$ = check_login1();
+ }
+ ;
+
+check_login_epsv
+ : /* empty */
+ {
+ if (noepsv) {
+ reply(500, "EPSV command disabled");
+ $$ = 0;
+ }
+ else
+ $$ = check_login1();
+ }
+ ;
+
+check_login_ro
+ : /* empty */
+ {
+ if (readonly) {
+ reply(550, "Permission denied.");
+ $$ = 0;
+ }
+ else
+ $$ = check_login1();
}
;
{ "REIN", REIN, ARGS, 0, "(reinitialize server state)" },
{ "QUIT", QUIT, ARGS, 1, "(terminate service)", },
{ "PORT", PORT, ARGS, 1, "<sp> b0, b1, b2, b3, b4" },
+ { "LPRT", LPRT, ARGS, 1, "<sp> af, hal, h1, h2, h3,..., pal, p1, p2..." },
+ { "EPRT", EPRT, STR1, 1, "<sp> |af|addr|port|" },
{ "PASV", PASV, ARGS, 1, "(set server in passive mode)" },
+ { "LPSV", LPSV, ARGS, 1, "(set server in passive mode)" },
+ { "EPSV", EPSV, ARGS, 1, "[<sp> af|ALL]" },
{ "TYPE", TYPE, ARGS, 1, "<sp> [ A | E | I | L ]" },
{ "STRU", STRU, ARGS, 1, "(specify file structure)" },
{ "MODE", MODE, ARGS, 1, "(specify transfer mode)" },
static void help __P((struct tab *, char *));
static struct tab *
lookup __P((struct tab *, char *));
+static int port_check __P((const char *));
+static int port_check_v6 __P((const char *));
static void sizecmd __P((char *));
static void toolong __P((int));
+static void v4map_data_dest __P((void));
static int yylex __P((void));
static struct tab *
}
(void) alarm(0);
#ifdef SETPROCTITLE
- if (strncasecmp(cbuf, "PASS", 4) != NULL)
+ if (strncasecmp(cbuf, "PASS", 4) != 0)
setproctitle("%s: %s", proctitle, cbuf);
#endif /* SETPROCTITLE */
if ((cp = strchr(cbuf, '\r'))) {
upper(cp);
p = lookup(sitetab, cp);
cbuf[cpos] = c;
- if (p != 0) {
+ if (guest == 0 && p != 0) {
if (p->implemented == 0) {
state = CMD;
nack(p->name);
dostr1:
if (cbuf[cpos] == ' ') {
cpos++;
- state = state == OSTR ? STR2 : ++state;
+ state = state == OSTR ? STR2 : state+1;
return (SP);
}
break;
cbuf[cpos] = c;
return (NUMBER);
}
+ if (strncasecmp(&cbuf[cpos], "ALL", 3) == 0
+ && !isalnum(cbuf[cpos + 3])) {
+ cpos += 3;
+ return ALL;
+ }
switch (cbuf[cpos++]) {
case '\n':
case TYPE_L:
case TYPE_I: {
struct stat stbuf;
- if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
+ if (stat(filename, &stbuf) < 0)
+ perror_reply(550, filename);
+ else if (!S_ISREG(stbuf.st_mode))
reply(550, "%s: not a plain file.", filename);
else
reply(213, "%qu", stbuf.st_size);
perror_reply(550, filename);
return;
}
- if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
+ if (fstat(fileno(fin), &stbuf) < 0) {
+ perror_reply(550, filename);
+ (void) fclose(fin);
+ return;
+ } else if (!S_ISREG(stbuf.st_mode)) {
reply(550, "%s: not a plain file.", filename);
(void) fclose(fin);
return;
reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
}
}
+
+/* Return 1, if port check is done. Return 0, if not yet. */
+static int
+port_check(pcmd)
+ const char *pcmd;
+{
+ if (his_addr.su_family == AF_INET) {
+ if (data_dest.su_family != AF_INET) {
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ return 1;
+ }
+ if (paranoid &&
+ ((ntohs(data_dest.su_port) < IPPORT_RESERVED) ||
+ memcmp(&data_dest.su_sin.sin_addr,
+ &his_addr.su_sin.sin_addr,
+ sizeof(data_dest.su_sin.sin_addr)))) {
+ usedefault = 1;
+ reply(500, "Illegal PORT range rejected.");
+ } else {
+ usedefault = 0;
+ if (pdata >= 0) {
+ (void) close(pdata);
+ pdata = -1;
+ }
+ reply(200, "%s command successful.", pcmd);
+ }
+ return 1;
+ }
+ return 0;
+}
+
+static int
+check_login1()
+{
+ if (logged_in)
+ return 1;
+ else {
+ reply(530, "Please login with USER and PASS.");
+ return 0;
+ }
+}
+
+#ifdef INET6
+/* Return 1, if port check is done. Return 0, if not yet. */
+static int
+port_check_v6(pcmd)
+ const char *pcmd;
+{
+ if (his_addr.su_family == AF_INET6) {
+ if (IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
+ /* Convert data_dest into v4 mapped sockaddr.*/
+ v4map_data_dest();
+ if (data_dest.su_family != AF_INET6) {
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ return 1;
+ }
+ if (paranoid &&
+ ((ntohs(data_dest.su_port) < IPPORT_RESERVED) ||
+ memcmp(&data_dest.su_sin6.sin6_addr,
+ &his_addr.su_sin6.sin6_addr,
+ sizeof(data_dest.su_sin6.sin6_addr)))) {
+ usedefault = 1;
+ reply(500, "Illegal PORT range rejected.");
+ } else {
+ usedefault = 0;
+ if (pdata >= 0) {
+ (void) close(pdata);
+ pdata = -1;
+ }
+ reply(200, "%s command successful.", pcmd);
+ }
+ return 1;
+ }
+ return 0;
+}
+
+static void
+v4map_data_dest()
+{
+ struct in_addr savedaddr;
+ int savedport;
+
+ if (data_dest.su_family != AF_INET) {
+ usedefault = 1;
+ reply(500, "Invalid address rejected.");
+ return;
+ }
+
+ savedaddr = data_dest.su_sin.sin_addr;
+ savedport = data_dest.su_port;
+
+ memset(&data_dest, 0, sizeof(data_dest));
+ data_dest.su_sin6.sin6_len = sizeof(struct sockaddr_in6);
+ data_dest.su_sin6.sin6_family = AF_INET6;
+ data_dest.su_sin6.sin6_port = savedport;
+ memset((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[10], 0xff, 2);
+ memcpy((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[12],
+ (caddr_t)&savedaddr, sizeof(savedaddr));
+}
+#endif
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" @(#)ftpd.8 8.3 (Berkeley) 6/1/94
+.\" @(#)ftpd.8 8.2 (Berkeley) 4/19/94
+.\" $FreeBSD: src/libexec/ftpd/ftpd.8,v 1.36 2000/12/18 08:33:25 ru Exp $
.\"
-.Dd June 1, 1994
+.Dd January 27, 2000
.Dt FTPD 8
.Os BSD 4.2
.Sh NAME
.Nd
Internet File Transfer Protocol server
.Sh SYNOPSIS
-.Nm ftpd
-.Op Fl dl
+.Nm
+.Op Fl 4
+.Op Fl 6
+.Op Fl d
+.Op Fl l Op Fl l
+.Op Fl A
+.Op Fl D
+.Op Fl R
+.Op Fl S
+.Op Fl U
+.Op Fl r
+.Op Fl E
.Op Fl T Ar maxtimeout
.Op Fl t Ar timeout
+.Op Fl a Ar address
+.Op Fl p Ar file
.Sh DESCRIPTION
.Nm Ftpd
is the
.Xr services 5 .
.Pp
Available options:
-.Bl -tag -width Ds
+.Bl -tag -width indent
.It Fl d
Debugging information is written to the syslog using LOG_FTP.
.It Fl l
session is logged using syslog with a facility of LOG_FTP.
If this option is specified twice, the retrieve (get), store (put), append,
delete, make directory, remove directory and rename operations and
-their filename arguments are also logged.
+their filename arguments are also logged. Note: LOG_FTP messages
+are not displayed by
+.Xr syslogd 8
+by default, and may have to be enabled in
+.Xr syslogd 8 Ns 's
+configuration file.
+.It Fl D
+With this option set,
+.Nm
+will detach and become a daemon, accepting connections on the FTP port and
+forking children processes to handle them.
+This is lower overhead than starting
+.Nm
+from
+.Xr inetd 8
+and is thus useful on busy servers to reduce load.
+.It Fl R
+With this option set,
+.Nm
+will revert to historical behavior with regard to security checks on
+user operations and restrictions on PORT requests.
+Currently,
+.Nm
+will only honor PORT commands directed to unprivileged ports on the
+remote user's host (which violates the FTP protocol specification but
+closes some security holes).
+.It Fl S
+With this option set,
+.Nm
+logs all anonymous file downloads to the file
+.Pa /var/log/ftpd
+when this file exists.
+.It Fl U
+In previous versions of
+.Nm ,
+when a passive mode client requested a data connection to the server,
+the server would use data ports in the range 1024..4999. Now, by default,
+the server will use data ports in the range 49152..65535. Specifying this
+option will revert to the old behavior.
.It Fl T
A client may also request a different timeout period;
the maximum period allowed may be set to
The inactivity timeout period is set to
.Ar timeout
seconds (the default is 15 minutes).
+.It Fl a
+When
+.Fl D
+is specified, accept connections only on the specified
+.Ar address .
+.It Fl p
+When
+.Fl D
+is specified, write the daemon's process ID to
+.Ar file .
+.It Fl 6
+When
+.Fl D
+is specified, accept connections via AF_INET6 socket.
+.It Fl 4
+When
+.Fl D
+is specified, accept IPv4 connections.
+When
+.Fl 6
+is also specified, accept IPv4 connection via AF_INET6 socket.
+When
+.Fl 6
+is not specified, accept IPv4 connection via AF_INET socket.
+.It Fl A
+Allow only anonymous ftp access.
+.It Fl r
+Put server in read-only mode.
+All commands which may modify the local filesystem are disabled.
+.It Fl E
+Disable the EPSV command.
+This is useful for servers behind older firewalls.
.El
.Pp
The file
-.Pa /etc/nologin
+.Pa /var/run/nologin
can be used to disable ftp access.
If the file exists,
.Nm
.Dq ready
message.
If the file
-.Pa /etc/motd
+.Pa /etc/ftpmotd
exists,
.Nm
-prints it after a successful login.
+prints it after a successful login. Note the motd file used is the one
+relative to the login environment. This means the one in
+.Pa ~ftp/etc
+in the anonymous user's case.
.Pp
The ftp server currently supports the following ftp requests.
-The case of the requests is ignored.
+The case of the requests is ignored. Requests marked [RW] are
+disabled if
+.Fl r
+is specified.
.Bl -column "Request" -offset indent
-.It Request Ta "Description"
+.It Sy Request Ta Sy "Description"
.It ABOR Ta "abort previous command"
.It ACCT Ta "specify account (ignored)"
.It ALLO Ta "allocate storage (vacuously)"
-.It APPE Ta "append to a file"
+.It APPE Ta "append to a file [RW]"
.It CDUP Ta "change to parent of current working directory"
.It CWD Ta "change working directory"
-.It DELE Ta "delete a file"
+.It DELE Ta "delete a file [RW]"
+.It EPRT Ta "specify data connection port, multiprotocol"
+.It EPSV Ta "prepare for server-to-server transfer, multiprotocol"
.It HELP Ta "give help information"
.It LIST Ta "give list files in a directory" Pq Dq Li "ls -lgA"
-.It MKD Ta "make a directory"
+.It LPRT Ta "specify data connection port, multiprotocol"
+.It LPSV Ta "prepare for server-to-server transfer, multiprotocol"
.It MDTM Ta "show last modification time of file"
+.It MKD Ta "make a directory [RW]"
.It MODE Ta "specify data transfer" Em mode
.It NLST Ta "give name list of files in directory"
.It NOOP Ta "do nothing"
.It QUIT Ta "terminate session"
.It REST Ta "restart incomplete transfer"
.It RETR Ta "retrieve a file"
-.It RMD Ta "remove a directory"
-.It RNFR Ta "specify rename-from file name"
-.It RNTO Ta "specify rename-to file name"
+.It RMD Ta "remove a directory [RW]"
+.It RNFR Ta "specify rename-from file name [RW]"
+.It RNTO Ta "specify rename-to file name [RW]"
.It SITE Ta "non-standard commands (see next section)"
.It SIZE Ta "return size of file"
.It STAT Ta "return status of server"
-.It STOR Ta "store a file"
-.It STOU Ta "store a file with a unique name"
+.It STOR Ta "store a file [RW]"
+.It STOU Ta "store a file with a unique name [RW]"
.It STRU Ta "specify data transfer" Em structure
.It SYST Ta "show operating system type of server system"
.It TYPE Ta "specify data transfer" Em type
.It USER Ta "specify user name"
.It XCUP Ta "change to parent of current working directory (deprecated)"
.It XCWD Ta "change working directory (deprecated)"
-.It XMKD Ta "make a directory (deprecated)"
+.It XMKD Ta "make a directory (deprecated) [RW]"
.It XPWD Ta "print the current working directory (deprecated)"
-.It XRMD Ta "remove a directory (deprecated)"
+.It XRMD Ta "remove a directory (deprecated) [RW]"
.El
.Pp
The following non-standard or
.It Sy Request Ta Sy Description
.It UMASK Ta change umask, e.g. ``SITE UMASK 002''
.It IDLE Ta set idle-timer, e.g. ``SITE IDLE 60''
-.It CHMOD Ta change mode of a file, e.g. ``SITE CHMOD 755 filename''
-.It HELP Ta give help information.
+.It CHMOD Ta "change mode of a file [RW], e.g. ``SITE CHMOD 755 filename''"
+.It HELP Ta give help information
.El
.Pp
The remaining ftp requests specified in Internet RFC 959
.Dq Li \&*?[]{}~ .
.Pp
.Nm Ftpd
-authenticates users according to three rules.
+authenticates users according to six rules.
.Pp
.Bl -enum -offset indent
.It
-The login name must be in the password data base,
-.Pa /etc/passwd ,
+The login name must be in the password data base
and not have a null password.
In this case a password must be provided by the client before any
file operations may be performed.
+If the user has an S/Key key, the response from a successful USER
+command will include an S/Key challenge.
+The client may choose to respond with a PASS command giving either
+a standard password or an S/Key one-time password.
+The server will automatically determine which type of
+password it has been given and attempt to authenticate accordingly.
+See
+.Xr key 1
+for more information on S/Key authentication.
+S/Key is a Trademark of Bellcore.
.It
The login name must not appear in the file
.Pa /etc/ftpusers .
.It
+The login name must not be a member of a group specified in the file
+.Pa /etc/ftpusers .
+Entries in this file interpreted as group names are prefixed by an "at"
+.Ql \&@
+sign.
+.It
The user must have a standard shell returned by
.Xr getusershell 3 .
.It
+If the user name appears in the file
+.Pa /etc/ftpchroot ,
+or the user is a member of a group with a group entry in this file,
+i.e. one prefixed with
+.Ql \&@ ,
+the session's root will be changed to the user's login directory by
+.Xr chroot 2
+as for an
+.Dq anonymous
+or
+.Dq ftp
+account (see next item).
+This facility may also be triggered by enabling the boolean "ftp-chroot"
+capability in
+.Xr login.conf 5 .
+However, the user must still supply a password.
+This feature is intended as a compromise between a fully anonymous
+account and a fully privileged account.
+The account should also be set up as for an anonymous account.
+.It
If the user name is
.Dq anonymous
or
In this case the user is allowed
to log in by specifying any password (by convention an email address for
the user should be used as the password).
+When the
+.Fl S
+option is set, all transfers are logged as well.
.El
.Pp
In the last case,
-.Nm ftpd
+.Nm
takes special measures to restrict the client's access privileges.
The server performs a
.Xr chroot 2
Make the home directory owned by
.Dq root
and unwritable by anyone.
-.ne 1i
-.It Pa ~ftp/bin
-Make this directory owned by
-.Dq root
-and unwritable by anyone (mode 555).
-The program
-.Xr ls 1
-must be present to support the list command.
-This program should be mode 111.
.It Pa ~ftp/etc
Make this directory owned by
.Dq root
and unwritable by anyone (mode 555).
-The files
-.Xr passwd 5
+The files pwd.db (see
+.Xr passwd 5 )
and
.Xr group 5
must be present for the
.Xr passwd
is not used, and should not contain real passwords.
The file
-.Pa motd ,
+.Pa ftpmotd ,
if present, will be printed after a successful login.
These files should be mode 444.
.It Pa ~ftp/pub
can then place files which are to be accessible via the anonymous
account in this directory.
.El
+.Pp
+If the system has multiple IP addresses,
+.Nm
+supports the idea of virtual hosts, which provides the ability to
+define multiple anonymous ftp areas, each one allocated to a different
+internet address.
+The file
+.Pa /etc/ftphosts
+contains information pertaining to each of the virtual hosts.
+Each host is defined on its own line which contains a number of
+fields separated by whitespace:
+.Bl -tag -offset indent -width hostname
+.It hostname
+Contains the hostname or IP address of the virtual host.
+.It user
+Contains a user record in the system password file.
+As with normal anonymous ftp, this user's access uid, gid and group
+memberships determine file access to the anonymous ftp area.
+The anonymous ftp area (to which any user is chrooted on login)
+is determined by the home directory defined for the account.
+User id and group for any ftp account may be the same as for the
+standard ftp user.
+.It statfile
+File to which all file transfers are logged, which
+defaults to
+.Pa /var/log/ftpd .
+.It welcome
+This file is the welcome message displayed before the server ready
+prompt.
+It defaults to
+.Pa /etc/ftpwelcome .
+.It motd
+This file is displayed after the user logs in.
+It defaults to
+.Pa /etc/ftpmotd .
+.El
+.Pp
+Lines beginning with a '#' are ignored and can be used to include
+comments.
+.Pp
+Defining a virtual host for the primary IP address or hostname
+changes the default for ftp logins to that address.
+The 'user', 'statfile', 'welcome' and 'motd' fields may be left
+blank, or a single hypen '-' used to indicate that the default
+value is to be used.
+.Pp
+As with any anonymous login configuration, due care must be given
+to setup and maintenance to guard against security related problems.
+.Pp
+.Nm
+has internal support for handling remote requests to list
+files, and will not execute
+.Pa /bin/ls
+in either a chrooted or non-chrooted environment. The
+.Pa ~/bin/ls
+executable need not be placed into the chrooted tree, nor need the
+.Pa ~/bin
+directory exist.
.Sh FILES
.Bl -tag -width /etc/ftpwelcome -compact
.It Pa /etc/ftpusers
List of unwelcome/restricted users.
+.It Pa /etc/ftpchroot
+List of normal users who should be chroot'd.
+.It Pa /etc/ftphosts
+Virtual hosting configuration file.
.It Pa /etc/ftpwelcome
Welcome notice.
-.It Pa /etc/motd
+.It Pa /etc/ftpmotd
Welcome notice after login.
-.It Pa /etc/nologin
+.It Pa /var/run/nologin
Displayed and access refused.
+.It Pa /var/log/ftpd
+Log file for anonymous transfers.
.El
.Sh SEE ALSO
.Xr ftp 1 ,
+.Xr key 1 ,
.Xr getusershell 3 ,
+.Xr login.conf 5 ,
+.Xr inetd 8 ,
.Xr syslogd 8
.Sh BUGS
The server must run as the super-user
.Nm
command appeared in
.Bx 4.2 .
+IPv6 support was added in WIDE Hydrangea IPv6 stack kit.
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
/*
* Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* SUCH DAMAGE.
*/
+#if 0
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
+#endif
#ifndef lint
-static char sccsid[] = "@(#)ftpd.c 8.5 (Berkeley) 4/28/95";
+#if 0
+static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94";
+#endif
+static const char rcsid[] =
+ "$FreeBSD: src/libexec/ftpd/ftpd.c,v 1.75 2001/03/27 19:40:50 markm Exp $";
#endif /* not lint */
-/* XXX this is to get around a compiler bug with long long's on ppc */
-#pragma CC_OPT_OFF
-
/*
* FTP server.
*/
#include <sys/param.h>
-#include <sys/stat.h>
#include <sys/ioctl.h>
+#include <sys/mman.h>
#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
+#include <netinet/tcp.h>
#define FTP_NAMES
#include <arpa/ftp.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
+#include <grp.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
+// #include <libutil.h>
+#ifdef LOGIN_CAP
+#include <login_cap.h>
+#endif
+
+#ifdef SKEY
+#include <skey.h>
+#endif
+
+#ifdef USE_PAM
+#include <security/pam_appl.h>
+#endif
#include "pathnames.h"
#include "extern.h"
#include <varargs.h>
#endif
-static char version[] = "Version 6.00";
+static char version[] = "Version 6.00LS";
+#undef main
+
+/* wrapper for KAME-special getnameinfo() */
+#ifndef NI_WITHSCOPEID
+#define NI_WITHSCOPEID 0
+#endif
extern off_t restart_point;
extern char cbuf[];
-struct sockaddr_in ctrl_addr;
-struct sockaddr_in data_source;
-struct sockaddr_in data_dest;
-struct sockaddr_in his_addr;
-struct sockaddr_in pasv_addr;
+union sockunion server_addr;
+union sockunion ctrl_addr;
+union sockunion data_source;
+union sockunion data_dest;
+union sockunion his_addr;
+union sockunion pasv_addr;
+int daemon_mode;
int data;
jmp_buf errcatch, urgcatch;
int logged_in;
int timeout = 900; /* timeout after 15 minutes of inactivity */
int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
int logging;
+int restricted_data_ports = 1;
+int paranoid = 1; /* be extra careful about security */
+int anon_only = 0; /* Only anonymous ftp allowed */
int guest;
+int dochroot;
+int stats;
+int statfd = -1;
int type;
int form;
int stru; /* avoid C keyword */
int mode;
int usedefault = 1; /* for data transfers */
int pdata = -1; /* for passive mode */
+int readonly=0; /* Server is in readonly mode. */
+int noepsv=0; /* EPSV command is disabled. */
sig_atomic_t transflag;
off_t file_size;
off_t byte_count;
#endif
int defumask = CMASK; /* default umask value */
char tmpline[7];
-char hostname[MAXHOSTNAMELEN];
+char *hostname;
+#ifdef VIRTUAL_HOSTING
+char *ftpuser;
+
+int epsvall = 0;
+
+static struct ftphost {
+ struct ftphost *next;
+ struct addrinfo *hostinfo;
+ char *hostname;
+ char *anonuser;
+ char *statfile;
+ char *welcome;
+ char *loginmsg;
+} *thishost, *firsthost;
+
+#endif
char remotehost[MAXHOSTNAMELEN];
+char *ident = NULL;
+
+static char ttyline[20];
+char *tty = ttyline; /* for klogin */
+
+#ifdef USE_PAM
+static int auth_pam __P((struct passwd**, const char*));
+pam_handle_t *pamh = NULL;
+#endif
+
+char *pid_file = NULL;
+
+/*
+ * Limit number of pathnames that glob can return.
+ * A limit of 0 indicates the number of pathnames is unlimited.
+ */
+#define MAXGLOBARGS 16384
+#
/*
* Timeout intervals for retrying connections
int swaitint = SWAITINT;
#ifdef SETPROCTITLE
+#ifdef OLD_SETPROCTITLE
char **Argv = NULL; /* pointer to argument vector */
char *LastArgv = NULL; /* end of argv */
+#endif /* OLD_SETPROCTITLE */
char proctitle[LINE_MAX]; /* initial part of title */
#endif /* SETPROCTITLE */
+#ifdef SKEY
+int pwok = 0;
+#endif
+
#define LOGCMD(cmd, file) \
if (logging > 1) \
syslog(LOG_INFO,"%s %s%s", cmd, \
cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \
}
+#ifdef VIRTUAL_HOSTING
+static void inithosts __P((void));
+static void selecthost __P((union sockunion *));
+#endif
static void ack __P((char *));
static void myoob __P((int));
-static int checkuser __P((char *));
+static int checkuser __P((char *, char *, int));
static FILE *dataconn __P((char *, off_t, char *));
-static void dolog __P((struct sockaddr_in *));
+static void dolog __P((struct sockaddr *));
static char *curdir __P((void));
static void end_login __P((void));
static FILE *getdatasock __P((char *));
static char *gunique __P((char *));
static void lostconn __P((int));
static int receive_data __P((FILE *, FILE *));
-static void send_data __P((FILE *, FILE *, off_t));
+static void send_data __P((FILE *, FILE *, off_t, off_t, int));
static struct passwd *
sgetpwnam __P((char *));
static char *sgetsave __P((char *));
+static void reapchild __P((int));
+static void logxfer __P((char *, long, long));
static char *
curdir()
int addrlen, ch, on = 1, tos;
char *cp, line[LINE_MAX];
FILE *fd;
+ int error;
+ char *bindname = NULL;
+ int family = AF_UNSPEC;
+ int enable_v4 = 0;
- /*
- * LOG_NDELAY sets up the logging connection immediately,
- * necessary for anonymous ftp's that chroot and can't do it later.
- */
- openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
- addrlen = sizeof(his_addr);
- if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
- syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
- exit(1);
- }
- addrlen = sizeof(ctrl_addr);
- if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
- syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
- exit(1);
- }
-#ifdef IP_TOS
- tos = IPTOS_LOWDELAY;
- if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
- syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
-#endif
- data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
- debug = 0;
-#ifdef SETPROCTITLE
+ tzset(); /* in case no timezone database in ~ftp */
+
+#ifdef OLD_SETPROCTITLE
/*
* Save start and extent of argv for setproctitle.
*/
while (*envp)
envp++;
LastArgv = envp[-1] + strlen(envp[-1]);
-#endif /* SETPROCTITLE */
+#endif /* OLD_SETPROCTITLE */
+
- while ((ch = getopt(argc, argv, "dlt:T:u:v")) != -1) {
+ while ((ch = getopt(argc, argv, "AdlDESURrt:T:u:va:p:46")) != -1) {
switch (ch) {
+ case 'D':
+ daemon_mode++;
+ break;
+
case 'd':
- debug = 1;
+ debug++;
+ break;
+
+ case 'E':
+ noepsv = 1;
break;
case 'l':
logging++; /* > 1 == extra logging */
break;
- case 't':
- timeout = atoi(optarg);
- if (maxtimeout < timeout)
- maxtimeout = timeout;
+ case 'r':
+ readonly = 1;
+ break;
+
+ case 'R':
+ paranoid = 0;
+ break;
+
+ case 'S':
+ stats++;
break;
case 'T':
timeout = maxtimeout;
break;
+ case 't':
+ timeout = atoi(optarg);
+ if (maxtimeout < timeout)
+ maxtimeout = timeout;
+ break;
+
+ case 'U':
+ restricted_data_ports = 0;
+ break;
+
+ case 'a':
+ bindname = optarg;
+ break;
+
+ case 'p':
+ pid_file = optarg;
+ break;
+
case 'u':
{
long val = 0;
defumask = val;
break;
}
+ case 'A':
+ anon_only = 1;
+ break;
case 'v':
debug = 1;
break;
+ case '4':
+ enable_v4 = 1;
+ if (family == AF_UNSPEC)
+ family = AF_INET;
+ break;
+
+ case '6':
+ family = AF_INET6;
+ break;
+
default:
warnx("unknown flag -%c ignored", optopt);
break;
}
}
+
+#ifdef VIRTUAL_HOSTING
+ inithosts();
+#endif
(void) freopen(_PATH_DEVNULL, "w", stderr);
- (void) signal(SIGPIPE, lostconn);
+
+ /*
+ * LOG_NDELAY sets up the logging connection immediately,
+ * necessary for anonymous ftp's that chroot and can't do it later.
+ */
+ openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
+
+ if (daemon_mode) {
+ int ctl_sock, fd;
+ struct addrinfo hints, *res;
+
+ /*
+ * Detach from parent.
+ */
+ if (daemon(1, 1) < 0) {
+ syslog(LOG_ERR, "failed to become a daemon");
+ exit(1);
+ }
+ (void) signal(SIGCHLD, reapchild);
+ /* init bind_sa */
+ memset(&hints, 0, sizeof(hints));
+
+ hints.ai_family = family == AF_UNSPEC ? AF_INET : family;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = 0;
+ hints.ai_flags = AI_PASSIVE;
+ error = getaddrinfo(bindname, "ftp", &hints, &res);
+ if (error) {
+ if (family == AF_UNSPEC) {
+ hints.ai_family = AF_UNSPEC;
+ error = getaddrinfo(bindname, "ftp", &hints,
+ &res);
+ }
+ }
+ if (error) {
+ syslog(LOG_ERR, "%s", gai_strerror(error));
+ if (error == EAI_SYSTEM)
+ syslog(LOG_ERR, "%s", strerror(errno));
+ exit(1);
+ }
+ if (res->ai_addr == NULL) {
+ syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname);
+ exit(1);
+ } else
+ family = res->ai_addr->sa_family;
+ /*
+ * Open a socket, bind it to the FTP port, and start
+ * listening.
+ */
+ ctl_sock = socket(family, SOCK_STREAM, 0);
+ if (ctl_sock < 0) {
+ syslog(LOG_ERR, "control socket: %m");
+ exit(1);
+ }
+ if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&on, sizeof(on)) < 0)
+ syslog(LOG_ERR, "control setsockopt: %m");
+#ifdef IPV6_BINDV6ONLY
+ if (family == AF_INET6 && enable_v4 == 0) {
+ if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_BINDV6ONLY,
+ (char *)&on, sizeof (on)) < 0)
+ syslog(LOG_ERR,
+ "control setsockopt(IPV6_BINDV6ONLY): %m");
+ }
+#endif /* IPV6_BINDV6ONLY */
+ memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len);
+ if (bind(ctl_sock, (struct sockaddr *)&server_addr,
+ server_addr.su_len) < 0) {
+ syslog(LOG_ERR, "control bind: %m");
+ exit(1);
+ }
+ if (listen(ctl_sock, 32) < 0) {
+ syslog(LOG_ERR, "control listen: %m");
+ exit(1);
+ }
+ /*
+ * Atomically write process ID
+ */
+ if (pid_file)
+ {
+ int fd;
+ char buf[20];
+
+ fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC
+ | O_NONBLOCK | O_EXLOCK, 0644);
+ if (fd < 0) {
+ if (errno == EAGAIN)
+ errx(1, "%s: file locked", pid_file);
+ else
+ err(1, "%s", pid_file);
+ }
+ snprintf(buf, sizeof(buf),
+ "%lu\n", (unsigned long) getpid());
+ if (write(fd, buf, strlen(buf)) < 0)
+ err(1, "%s: write", pid_file);
+ /* Leave the pid file open and locked */
+ }
+ /*
+ * Loop forever accepting connection requests and forking off
+ * children to handle them.
+ */
+ while (1) {
+ addrlen = server_addr.su_len;
+ fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen);
+ if (fork() == 0) {
+ /* child */
+ (void) dup2(fd, 0);
+ (void) dup2(fd, 1);
+ close(ctl_sock);
+ break;
+ }
+ close(fd);
+ }
+ } else {
+ addrlen = sizeof(his_addr);
+ if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
+ syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
+ exit(1);
+ }
+ }
+
(void) signal(SIGCHLD, SIG_IGN);
- if ((int)signal(SIGURG, myoob) < 0)
+ (void) signal(SIGPIPE, lostconn);
+ if (signal(SIGURG, myoob) == SIG_ERR)
syslog(LOG_ERR, "signal: %m");
+ addrlen = sizeof(ctrl_addr);
+ if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
+ syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
+ exit(1);
+ }
+#ifdef VIRTUAL_HOSTING
+ /* select our identity from virtual host table */
+ selecthost(&ctrl_addr);
+#endif
+#ifdef IP_TOS
+ if (ctrl_addr.su_family == AF_INET)
+ {
+ tos = IPTOS_LOWDELAY;
+ if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
+ syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
+ }
+#endif
+ /*
+ * Disable Nagle on the control channel so that we don't have to wait
+ * for peer's ACK before issuing our next reply.
+ */
+ if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
+ syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m");
+
+ data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
+
+ /* set this here so klogin can use it... */
+ (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid());
+
/* Try to handle urgent data inline */
#ifdef SO_OOBINLINE
if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
syslog(LOG_ERR, "fcntl F_SETOWN: %m");
#endif
- dolog(&his_addr);
+ dolog((struct sockaddr *)&his_addr);
/*
* Set up default state
*/
reply(530, "System not available.");
exit(0);
}
+#ifdef VIRTUAL_HOSTING
+ if ((fd = fopen(thishost->welcome, "r")) != NULL) {
+#else
if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
+#endif
while (fgets(line, sizeof(line), fd) != NULL) {
if ((cp = strchr(line, '\n')) != NULL)
*cp = '\0';
(void) fclose(fd);
/* reply(220,) must follow */
}
- (void) gethostname(hostname, sizeof(hostname));
+#ifndef VIRTUAL_HOSTING
+ if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
+ fatal("Ran out of memory.");
+ (void) gethostname(hostname, MAXHOSTNAMELEN - 1);
+ hostname[MAXHOSTNAMELEN - 1] = '\0';
+#endif
reply(220, "%s FTP server (%s) ready.", hostname, version);
(void) setjmp(errcatch);
for (;;)
if (debug)
syslog(LOG_DEBUG, "lost connection");
- dologout(-1);
+ dologout(1);
}
-static char ttyline[20];
+#ifdef VIRTUAL_HOSTING
+/*
+ * read in virtual host tables (if they exist)
+ */
+
+static void
+inithosts()
+{
+ FILE *fp;
+ char *cp;
+ struct ftphost *hrp, *lhrp;
+ char line[1024];
+ struct addrinfo hints, *res, *ai;
+
+ /*
+ * Fill in the default host information
+ */
+ if (gethostname(line, sizeof(line)) < 0)
+ line[0] = '\0';
+ if ((hrp = malloc(sizeof(struct ftphost))) == NULL ||
+ (hrp->hostname = strdup(line)) == NULL)
+ fatal("Ran out of memory.");
+ hrp->hostinfo = NULL;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_CANONNAME;
+ hints.ai_family = AF_UNSPEC;
+ getaddrinfo(hrp->hostname, NULL, &hints, &res);
+ if (res)
+ hrp->hostinfo = res;
+ hrp->statfile = _PATH_FTPDSTATFILE;
+ hrp->welcome = _PATH_FTPWELCOME;
+ hrp->loginmsg = _PATH_FTPLOGINMESG;
+ hrp->anonuser = "ftp";
+ hrp->next = NULL;
+ thishost = firsthost = lhrp = hrp;
+ if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
+ int addrsize, error, gothost;
+ void *addr;
+ struct hostent *hp;
+
+ while (fgets(line, sizeof(line), fp) != NULL) {
+ int i, hp_error;
+
+ if ((cp = strchr(line, '\n')) == NULL) {
+ /* ignore long lines */
+ while (fgets(line, sizeof(line), fp) != NULL &&
+ strchr(line, '\n') == NULL)
+ ;
+ continue;
+ }
+ *cp = '\0';
+ cp = strtok(line, " \t");
+ /* skip comments and empty lines */
+ if (cp == NULL || line[0] == '#')
+ continue;
+
+ hints.ai_flags = 0;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_flags = AI_PASSIVE;
+ error = getaddrinfo(cp, NULL, &hints, &res);
+ if (error != NULL)
+ continue;
+ for (ai = res; ai != NULL && ai->ai_addr != NULL;
+ ai = ai->ai_next) {
+
+ gothost = 0;
+ for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
+ struct addrinfo *hi;
+
+ for (hi = hrp->hostinfo; hi != NULL;
+ hi = hi->ai_next)
+ if (hi->ai_addrlen == ai->ai_addrlen &&
+ memcmp(hi->ai_addr,
+ ai->ai_addr,
+ ai->ai_addr->sa_len) == 0) {
+ gothost++;
+ break;
+ }
+ if (gothost)
+ break;
+ }
+ if (hrp == NULL) {
+ if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
+ continue;
+ /* defaults */
+ hrp->statfile = _PATH_FTPDSTATFILE;
+ hrp->welcome = _PATH_FTPWELCOME;
+ hrp->loginmsg = _PATH_FTPLOGINMESG;
+ hrp->anonuser = "ftp";
+ hrp->next = NULL;
+ lhrp->next = hrp;
+ lhrp = hrp;
+ }
+ hrp->hostinfo = res;
+
+ /*
+ * determine hostname to use.
+ * force defined name if there is a valid alias
+ * otherwise fallback to primary hostname
+ */
+ /* XXX: getaddrinfo() can't do alias check */
+ switch(hrp->hostinfo->ai_family) {
+ case AF_INET:
+ addr = &((struct sockaddr_in *)&hrp->hostinfo->ai_addr)->sin_addr;
+ addrsize = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ addr = &((struct sockaddr_in6 *)&hrp->hostinfo->ai_addr)->sin6_addr;
+ addrsize = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ /* should not reach here */
+ if (hrp->hostinfo != NULL)
+ freeaddrinfo(hrp->hostinfo);
+ free(hrp);
+ continue;
+ /* NOTREACHED */
+ }
+ if ((hp = getipnodebyaddr((char*)addr, addrsize,
+ hrp->hostinfo->ai_family,
+ &hp_error)) != NULL) {
+ if (strcmp(cp, hp->h_name) != 0) {
+ if (hp->h_aliases == NULL)
+ cp = hp->h_name;
+ else {
+ i = 0;
+ while (hp->h_aliases[i] &&
+ strcmp(cp, hp->h_aliases[i]) != 0)
+ ++i;
+ if (hp->h_aliases[i] == NULL)
+ cp = hp->h_name;
+ }
+ }
+ }
+ hrp->hostname = strdup(cp);
+ freehostent(hp);
+ /* ok, now we now peel off the rest */
+ i = 0;
+ while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) {
+ if (*cp != '-' && (cp = strdup(cp)) != NULL) {
+ switch (i) {
+ case 0: /* anon user permissions */
+ hrp->anonuser = cp;
+ break;
+ case 1: /* statistics file */
+ hrp->statfile = cp;
+ break;
+ case 2: /* welcome message */
+ hrp->welcome = cp;
+ break;
+ case 3: /* login message */
+ hrp->loginmsg = cp;
+ break;
+ }
+ }
+ ++i;
+ }
+ /* XXX: re-initialization for getaddrinfo() loop */
+ cp = strtok(line, " \t");
+ }
+ }
+ (void) fclose(fp);
+ }
+}
+
+static void
+selecthost(su)
+ union sockunion *su;
+{
+ struct ftphost *hrp;
+ u_int16_t port;
+#ifdef INET6
+ struct in6_addr *mapped_in6 = NULL;
+#endif
+ struct addrinfo *hi;
+
+#ifdef INET6
+ /*
+ * XXX IPv4 mapped IPv6 addr consideraton,
+ * specified in rfc2373.
+ */
+ if (su->su_family == AF_INET6 &&
+ IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
+ mapped_in6 = &su->su_sin6.sin6_addr;
+#endif
+
+ hrp = thishost = firsthost; /* default */
+ port = su->su_port;
+ su->su_port = 0;
+ while (hrp != NULL) {
+ for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
+ if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
+ thishost = hrp;
+ break;
+ }
+#ifdef INET6
+ /* XXX IPv4 mapped IPv6 addr consideraton */
+ if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
+ (memcmp(&mapped_in6->s6_addr[12],
+ &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
+ sizeof(struct in_addr)) == 0)) {
+ thishost = hrp;
+ break;
+ }
+#endif
+ }
+ hrp = hrp->next;
+ }
+ su->su_port = port;
+ /* setup static variables as appropriate */
+ hostname = thishost->hostname;
+ ftpuser = thishost->anonuser;
+}
+#endif
/*
* Helper function for sgetpwnam().
static int login_attempts; /* number of failed login attempts */
static int askpasswd; /* had user command, ask for passwd */
-static char curname[10]; /* current USER name */
+static char curname[MAXLOGNAME]; /* current USER name */
/*
* USER command.
if (guest) {
reply(530, "Can't change user from guest login.");
return;
+ } else if (dochroot) {
+ reply(530, "Can't change user from chroot user.");
+ return;
}
end_login();
}
guest = 0;
if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
- if (checkuser("ftp") || checkuser("anonymous"))
+#if !defined(_PATH_FTPUSERS)
+#define _PATH_FTPUSERS "/etc/ftpusers"
+#endif
+ if (checkuser(_PATH_FTPUSERS, "ftp", 0) ||
+ checkuser(_PATH_FTPUSERS, "anonymous", 0))
reply(530, "User %s access denied.", name);
+#ifdef VIRTUAL_HOSTING
+ else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) {
+#else
else if ((pw = sgetpwnam("ftp")) != NULL) {
+#endif
guest = 1;
askpasswd = 1;
reply(331,
- "Guest login ok, type your name as password.");
+ "Guest login ok, send your email address as password.");
} else
reply(530, "User %s unknown.", name);
if (!askpasswd && logging)
"ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
return;
}
- if (pw = sgetpwnam(name)) {
+ if (anon_only != 0) {
+ reply(530, "Sorry, only anonymous ftp allowed.");
+ return;
+ }
+
+ if ((pw = sgetpwnam(name))) {
if ((shell = pw->pw_shell) == NULL || *shell == 0)
shell = _PATH_BSHELL;
while ((cp = getusershell()) != NULL)
break;
endusershell();
- if (cp == NULL || checkuser(name)) {
+ if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) {
reply(530, "User %s access denied.", name);
if (logging)
syslog(LOG_NOTICE,
}
if (logging)
strncpy(curname, name, sizeof(curname)-1);
+#ifdef SKEY
+ pwok = skeyaccess(name, NULL, remotehost, remotehost);
+ reply(331, "%s", skey_challenge(name, pw, pwok));
+#else
reply(331, "Password required for %s.", name);
+#endif
askpasswd = 1;
/*
* Delay before reading passwd after first failed
}
/*
- * Check if a user is in the file _PATH_FTPUSERS
+ * Check if a user is in the file "fname"
*/
static int
-checkuser(name)
+checkuser(fname, name, pwset)
+ char *fname;
char *name;
+ int pwset;
{
FILE *fd;
int found = 0;
char *p, line[BUFSIZ];
- if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
- while (fgets(line, sizeof(line), fd) != NULL)
+ if ((fd = fopen(fname, "r")) != NULL) {
+ while (!found && fgets(line, sizeof(line), fd) != NULL)
if ((p = strchr(line, '\n')) != NULL) {
*p = '\0';
if (line[0] == '#')
continue;
- if (strcmp(line, name) == 0) {
- found = 1;
- break;
+ /*
+ * if first chr is '@', check group membership
+ */
+ if (line[0] == '@') {
+ int i = 0;
+ struct group *grp;
+
+ if ((grp = getgrnam(line+1)) == NULL)
+ continue;
+ /*
+ * Check user's default group
+ */
+ if (pwset && grp->gr_gid == pw->pw_gid)
+ found = 1;
+ /*
+ * Check supplementary groups
+ */
+ while (!found && grp->gr_mem[i])
+ found = strcmp(name,
+ grp->gr_mem[i++])
+ == 0;
}
+ /*
+ * Otherwise, just check for username match
+ */
+ else
+ found = strcmp(line, name) == 0;
}
(void) fclose(fd);
}
static void
end_login()
{
+#ifdef USE_PAM
+ int e;
+#endif
(void) seteuid((uid_t)0);
if (logged_in)
- logwtmp(ttyline, "", "");
+ ftpd_logwtmp(ttyline, "", "");
pw = NULL;
+#ifdef LOGIN_CAP
+ setusercontext(NULL, getpwuid(0), (uid_t)0,
+ LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
+#endif
+#ifdef USE_PAM
+ if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
+ syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
+ if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
+ syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
+ if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
+ syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
+ pamh = NULL;
+#endif
logged_in = 0;
guest = 0;
+ dochroot = 0;
+}
+
+#ifdef USE_PAM
+
+/*
+ * the following code is stolen from imap-uw PAM authentication module and
+ * login.c
+ */
+#define COPY_STRING(s) (s ? strdup(s) : NULL)
+
+struct cred_t {
+ const char *uname; /* user name */
+ const char *pass; /* password */
+};
+typedef struct cred_t cred_t;
+
+static int
+auth_conv(int num_msg, const struct pam_message **msg,
+ struct pam_response **resp, void *appdata)
+{
+ int i;
+ cred_t *cred = (cred_t *) appdata;
+ struct pam_response *reply =
+ malloc(sizeof(struct pam_response) * num_msg);
+
+ for (i = 0; i < num_msg; i++) {
+ switch (msg[i]->msg_style) {
+ case PAM_PROMPT_ECHO_ON: /* assume want user name */
+ reply[i].resp_retcode = PAM_SUCCESS;
+ reply[i].resp = COPY_STRING(cred->uname);
+ /* PAM frees resp. */
+ break;
+ case PAM_PROMPT_ECHO_OFF: /* assume want password */
+ reply[i].resp_retcode = PAM_SUCCESS;
+ reply[i].resp = COPY_STRING(cred->pass);
+ /* PAM frees resp. */
+ break;
+ case PAM_TEXT_INFO:
+ case PAM_ERROR_MSG:
+ reply[i].resp_retcode = PAM_SUCCESS;
+ reply[i].resp = NULL;
+ break;
+ default: /* unknown message style */
+ free(reply);
+ return PAM_CONV_ERR;
+ }
+ }
+
+ *resp = reply;
+ return PAM_SUCCESS;
+}
+
+/*
+ * Attempt to authenticate the user using PAM. Returns 0 if the user is
+ * authenticated, or 1 if not authenticated. If some sort of PAM system
+ * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
+ * function returns -1. This can be used as an indication that we should
+ * fall back to a different authentication mechanism.
+ */
+static int
+auth_pam(struct passwd **ppw, const char *pass)
+{
+ pam_handle_t *pamh = NULL;
+ const char *tmpl_user;
+ const void *item;
+ int rval;
+ int e;
+ cred_t auth_cred = { (*ppw)->pw_name, pass };
+ struct pam_conv conv = { &auth_conv, &auth_cred };
+
+ e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
+ if (e != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
+ return -1;
+ }
+
+ e = pam_set_item(pamh, PAM_RHOST, remotehost);
+ if (e != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
+ pam_strerror(pamh, e));
+ return -1;
+ }
+
+ e = pam_authenticate(pamh, 0);
+ switch (e) {
+ case PAM_SUCCESS:
+ /*
+ * With PAM we support the concept of a "template"
+ * user. The user enters a login name which is
+ * authenticated by PAM, usually via a remote service
+ * such as RADIUS or TACACS+. If authentication
+ * succeeds, a different but related "template" name
+ * is used for setting the credentials, shell, and
+ * home directory. The name the user enters need only
+ * exist on the remote authentication server, but the
+ * template name must be present in the local password
+ * database.
+ *
+ * This is supported by two various mechanisms in the
+ * individual modules. However, from the application's
+ * point of view, the template user is always passed
+ * back as a changed value of the PAM_USER item.
+ */
+ if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
+ PAM_SUCCESS) {
+ tmpl_user = (const char *) item;
+ if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
+ *ppw = getpwnam(tmpl_user);
+ } else
+ syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
+ pam_strerror(pamh, e));
+ rval = 0;
+ break;
+
+ case PAM_AUTH_ERR:
+ case PAM_USER_UNKNOWN:
+ case PAM_MAXTRIES:
+ rval = 1;
+ break;
+
+ default:
+ syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
+ rval = -1;
+ break;
+ }
+
+ if (rval == 0) {
+ e = pam_acct_mgmt(pamh, 0);
+ if (e == PAM_NEW_AUTHTOK_REQD) {
+ e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
+ if (e != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e));
+ rval = 1;
+ }
+ } else if (e != PAM_SUCCESS) {
+ rval = 1;
+ }
+ }
+
+ if (rval != 0) {
+ if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
+ }
+ pamh = NULL;
+ }
+ return rval;
}
+#endif /* USE_PAM */
+
void
pass(passwd)
char *passwd;
{
- char *salt, *xpasswd;
+ int rval;
FILE *fd;
+#ifdef LOGIN_CAP
+ login_cap_t *lc = NULL;
+#endif
+#ifdef USE_PAM
+ int e;
+#endif
if (logged_in || askpasswd == 0) {
reply(503, "Login with USER first.");
}
askpasswd = 0;
if (!guest) { /* "ftp" is only account allowed no password */
- if (pw == NULL)
- salt = "xx";
- else
- salt = pw->pw_passwd;
- xpasswd = crypt(passwd, salt);
+ if (pw == NULL) {
+ rval = 1; /* failure below */
+ goto skip;
+ }
+#ifdef USE_PAM
+ rval = auth_pam(&pw, passwd);
+ if (rval >= 0)
+ goto skip;
+#endif
+#ifdef SKEY
+ if (pwok)
+ rval = strcmp(pw->pw_passwd,
+ crypt(passwd, pw->pw_passwd));
+ if (rval)
+ rval = strcmp(pw->pw_passwd,
+ skey_crypt(passwd, pw->pw_passwd, pw, pwok));
+#else
+ rval = strcmp(pw->pw_passwd, crypt(passwd, pw->pw_passwd));
+#endif
/* The strcmp does not catch null passwords! */
- if (pw == NULL || *pw->pw_passwd == '\0' ||
- strcmp(xpasswd, pw->pw_passwd)) {
+ if (*pw->pw_passwd == '\0' ||
+ (pw->pw_expire && time(NULL) >= pw->pw_expire))
+ rval = 1; /* failure */
+skip:
+ /*
+ * If rval == 1, the user failed the authentication check
+ * above. If rval == 0, either PAM or local authentication
+ * succeeded.
+ */
+ if (rval) {
reply(530, "Login incorrect.");
if (logging)
syslog(LOG_NOTICE,
return;
}
}
+#ifdef SKEY
+ pwok = 0;
+#endif
login_attempts = 0; /* this time successful */
if (setegid((gid_t)pw->pw_gid) < 0) {
reply(550, "Can't set gid.");
return;
}
+ /* May be overridden by login.conf */
+ (void) umask(defumask);
+#ifdef LOGIN_CAP
+ if ((lc = login_getpwclass(pw)) != NULL) {
+ char remote_ip[MAXHOSTNAMELEN];
+
+ getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
+ remote_ip, sizeof(remote_ip) - 1, NULL, 0,
+ NI_NUMERICHOST|NI_WITHSCOPEID);
+ remote_ip[sizeof(remote_ip) - 1] = 0;
+ if (!auth_hostok(lc, remotehost, remote_ip)) {
+ syslog(LOG_INFO|LOG_AUTH,
+ "FTP LOGIN FAILED (HOST) as %s: permission denied.",
+ pw->pw_name);
+ reply(530, "Permission denied.\n");
+ pw = NULL;
+ return;
+ }
+ if (!auth_timeok(lc, time(NULL))) {
+ reply(530, "Login not available right now.\n");
+ pw = NULL;
+ return;
+ }
+ }
+ setusercontext(lc, pw, (uid_t)0,
+ LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY|
+ LOGIN_SETRESOURCES|LOGIN_SETUMASK);
+#else
+ setlogin(pw->pw_name);
(void) initgroups(pw->pw_name, pw->pw_gid);
+#endif
+
+#ifdef USE_PAM
+ if (pamh) {
+ if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
+ } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
+ syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
+ }
+ }
+#endif
/* open wtmp before chroot */
- (void)sprintf(ttyline, "ftp%d", getpid());
- logwtmp(ttyline, pw->pw_name, remotehost);
+ ftpd_logwtmp(ttyline, pw->pw_name, remotehost);
logged_in = 1;
+ if (guest && stats && statfd < 0)
+#ifdef VIRTUAL_HOSTING
+ if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0)
+#else
+ if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0)
+#endif
+ stats = 0;
+
+ dochroot =
+#ifdef LOGIN_CAP /* Allow login.conf configuration as well */
+ login_getcapbool(lc, "ftp-chroot", 0) ||
+#endif
+ checkuser(_PATH_FTPCHROOT, pw->pw_name, 1);
if (guest) {
/*
* We MUST do a chdir() after the chroot. Otherwise
reply(550, "Can't set guest privileges.");
goto bad;
}
+ } else if (dochroot) {
+ if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
+ reply(550, "Can't change root.");
+ goto bad;
+ }
} else if (chdir(pw->pw_dir) < 0) {
if (chdir("/") < 0) {
reply(530, "User %s: can't change directory to %s.",
reply(550, "Can't set uid.");
goto bad;
}
+
/*
* Display a login message, if it exists.
* N.B. reply(230,) must follow the message.
*/
+#ifdef VIRTUAL_HOSTING
+ if ((fd = fopen(thishost->loginmsg, "r")) != NULL) {
+#else
if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) {
+#endif
char *cp, line[LINE_MAX];
while (fgets(line, sizeof(line), fd) != NULL) {
(void) fclose(fd);
}
if (guest) {
+ if (ident != NULL)
+ free(ident);
+ ident = strdup(passwd);
+ if (ident == NULL)
+ fatal("Ran out of memory.");
+
reply(230, "Guest login ok, access restrictions apply.");
#ifdef SETPROCTITLE
- snprintf(proctitle, sizeof(proctitle),
- "%s: anonymous/%.*s", remotehost,
- sizeof(proctitle) - sizeof(remotehost) -
- sizeof(": anonymous/"), passwd);
+#ifdef VIRTUAL_HOSTING
+ if (thishost != firsthost)
+ snprintf(proctitle, sizeof(proctitle),
+ "%s: anonymous(%s)/%.*s", remotehost, hostname,
+ (int)(sizeof(proctitle) - sizeof(remotehost) -
+ sizeof(": anonymous/")), passwd);
+ else
+#endif
+ snprintf(proctitle, sizeof(proctitle),
+ "%s: anonymous/%.*s", remotehost,
+ (int)(sizeof(proctitle) - sizeof(remotehost) -
+ sizeof(": anonymous/")), passwd);
setproctitle("%s", proctitle);
#endif /* SETPROCTITLE */
if (logging)
syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
remotehost, passwd);
} else {
+ if (dochroot)
+ reply(230, "User %s logged in, access restrictions apply.",
+ pw->pw_name);
+ else
reply(230, "User %s logged in.", pw->pw_name);
+
#ifdef SETPROCTITLE
snprintf(proctitle, sizeof(proctitle),
- "%s: %s", remotehost, pw->pw_name);
+ "%s: %s", remotehost, pw->pw_name);
setproctitle("%s", proctitle);
#endif /* SETPROCTITLE */
if (logging)
syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
remotehost, pw->pw_name);
}
- (void) umask(defumask);
+#ifdef LOGIN_CAP
+ login_close(lc);
+#endif
return;
bad:
/* Forget all about it... */
+#ifdef LOGIN_CAP
+ login_close(lc);
+#endif
end_login();
}
FILE *fin, *dout;
struct stat st;
int (*closefunc) __P((FILE *));
+ time_t start;
if (cmd == 0) {
fin = fopen(name, "r"), closefunc = fclose;
} else {
char line[BUFSIZ];
- (void) sprintf(line, cmd, name), name = line;
+ (void) snprintf(line, sizeof(line), cmd, name), name = line;
fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
st.st_size = -1;
st.st_blksize = BUFSIZ;
dout = dataconn(name, st.st_size, "w");
if (dout == NULL)
goto done;
- send_data(fin, dout, st.st_blksize);
+ time(&start);
+ send_data(fin, dout, st.st_blksize, st.st_size,
+ restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
+ if (cmd == 0 && guest && stats)
+ logxfer(name, st.st_size, start);
(void) fclose(dout);
data = -1;
pdata = -1;
struct stat st;
int (*closefunc) __P((FILE *));
- if (unique && stat(name, &st) == 0 &&
+ if ((unique || guest) && stat(name, &st) == 0 &&
(name = gunique(name)) == NULL) {
LOGCMD(*mode == 'w' ? "put" : "append", name);
return;
if (data >= 0)
return (fdopen(data, mode));
(void) seteuid((uid_t)0);
- s = socket(AF_INET, SOCK_STREAM, 0);
+
+ s = socket(data_dest.su_family, SOCK_STREAM, 0);
if (s < 0)
goto bad;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *) &on, sizeof(on)) < 0)
goto bad;
/* anchor socket to avoid multi-homing problems */
- data_source.sin_family = AF_INET;
- data_source.sin_addr = ctrl_addr.sin_addr;
+ data_source = ctrl_addr;
+ data_source.su_port = htons(20); /* ftp-data port */
for (tries = 1; ; tries++) {
if (bind(s, (struct sockaddr *)&data_source,
- sizeof(data_source)) >= 0)
+ data_source.su_len) >= 0)
break;
if (errno != EADDRINUSE || tries > 10)
goto bad;
}
(void) seteuid((uid_t)pw->pw_uid);
#ifdef IP_TOS
+ if (data_source.su_family == AF_INET)
+ {
on = IPTOS_THROUGHPUT;
if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
+ }
+#endif
+#ifdef TCP_NOPUSH
+ /*
+ * Turn off push flag to keep sender TCP from sending short packets
+ * at the boundaries of each write(). Should probably do a SO_SNDBUF
+ * to set the send buffer size as well, but that may not be desirable
+ * in heavy-load situations.
+ */
+ on = 1;
+ if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0)
+ syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m");
#endif
+#ifdef SO_SNDBUF
+ on = 65536;
+ if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0)
+ syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m");
+#endif
+
return (fdopen(s, mode));
bad:
/* Return the real value of errno (close may change it) */
file_size = size;
byte_count = 0;
if (size != (off_t) -1)
- (void) sprintf(sizebuf, " (%qd bytes)", size);
+ (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size);
else
- (void) strcpy(sizebuf, "");
+ *sizebuf = '\0';
if (pdata >= 0) {
- struct sockaddr_in from;
- int s, fromlen = sizeof(from);
+ union sockunion from;
+ int s, fromlen = ctrl_addr.su_len;
+ struct timeval timeout;
+ fd_set set;
+
+ FD_ZERO(&set);
+ FD_SET(pdata, &set);
- s = accept(pdata, (struct sockaddr *)&from, &fromlen);
- if (s < 0) {
+ timeout.tv_usec = 0;
+ timeout.tv_sec = 120;
+
+ if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 ||
+ (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) {
reply(425, "Can't open data connection.");
(void) close(pdata);
pdata = -1;
(void) close(pdata);
pdata = s;
#ifdef IP_TOS
- tos = IPTOS_LOWDELAY;
+ if (from.su_family == AF_INET)
+ {
+ tos = IPTOS_THROUGHPUT;
(void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
sizeof(int));
+ }
#endif
reply(150, "Opening %s mode data connection for '%s'%s.",
type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
usedefault = 1;
file = getdatasock(mode);
if (file == NULL) {
- reply(425, "Can't create data socket (%s,%d): %s.",
- inet_ntoa(data_source.sin_addr),
- ntohs(data_source.sin_port), strerror(errno));
+#if defined(HAVE_GETNAMEINFO)
+ char hostbuf[BUFSIZ], portbuf[BUFSIZ];
+ getnameinfo((struct sockaddr *)&data_source,
+ data_source.su_len, hostbuf, sizeof(hostbuf) - 1,
+ portbuf, sizeof(portbuf),
+ NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID);
+ reply(425, "Can't create data socket (%s,%s): %s.",
+ hostbuf, portbuf, strerror(errno));
+#else
+ reply(425, "Can't create data socket (%s,%d): %s.",
+ inet_ntoa(data_source.su_sin.sin_addr),
+ ntohs(data_source.su_sin.sin_port), strerror(errno));
+#endif
return (NULL);
}
data = fileno(file);
while (connect(data, (struct sockaddr *)&data_dest,
- sizeof(data_dest)) < 0) {
+ data_dest.su_len) < 0) {
if (errno == EADDRINUSE && retry < swaitmax) {
sleep((unsigned) swaitint);
retry += swaitint;
/*
* Tranfer the contents of "instr" to "outstr" peer using the appropriate
- * encapsulation of the data subject * to Mode, Structure, and Type.
+ * encapsulation of the data subject to Mode, Structure, and Type.
*
* NB: Form isn't handled.
*/
static void
-send_data(instr, outstr, blksize)
+send_data(instr, outstr, blksize, filesize, isreg)
FILE *instr, *outstr;
off_t blksize;
+ off_t filesize;
+ int isreg;
{
- int c, cnt, filefd, netfd;
- char *buf;
+ int c, filefd, netfd;
+ char *buf, *bp;
+ size_t len;
+ off_t cnt;
transflag++;
if (setjmp(urgcatch)) {
case TYPE_I:
case TYPE_L:
+ /*
+ * isreg is only set if we are not doing restart and we
+ * are sending a regular file
+ */
+ netfd = fileno(outstr);
+ filefd = fileno(instr);
+
+#if defined(HAVE_SENDFILE)
+ if (isreg) {
+
+ off_t offset;
+ int err;
+
+ len = filesize;
+ err = cnt = offset = 0;
+
+ while (err != -1 && cnt < filesize) {
+ err = sendfile(filefd, netfd, offset, len,
+ (struct sf_hdtr *) NULL, &cnt, 0);
+ byte_count += cnt;
+ offset += cnt;
+ len -= cnt;
+
+ if (err == -1) {
+ if (!cnt)
+ goto oldway;
+
+ goto data_err;
+ }
+ }
+
+ reply(226, "Transfer complete.");
+ return;
+ }
+#else
+ if (isreg && filesize < (off_t)16 * 1024 * 1024) {
+ buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd,
+ (off_t)0);
+ if (buf == MAP_FAILED) {
+ syslog(LOG_WARNING, "mmap(%lu): %m",
+ (unsigned long)filesize);
+ goto oldway;
+ }
+ bp = buf;
+ len = filesize;
+ do {
+ cnt = write(netfd, bp, len);
+ len -= cnt;
+ bp += cnt;
+ if (cnt > 0) byte_count += cnt;
+ } while(cnt > 0 && len > 0);
+
+ transflag = 0;
+ munmap(buf, (size_t)filesize);
+ if (cnt < 0)
+ goto data_err;
+ reply(226, "Transfer complete.");
+ return;
+ }
+#endif
+oldway:
if ((buf = malloc((u_int)blksize)) == NULL) {
transflag = 0;
perror_reply(451, "Local resource failure: malloc");
return;
}
- netfd = fileno(outstr);
- filefd = fileno(instr);
+
while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 &&
write(netfd, buf, cnt) == cnt)
byte_count += cnt;
FILE *instr, *outstr;
{
int c;
- int cnt, bare_lfs = 0;
+ int cnt, bare_lfs;
char buf[BUFSIZ];
transflag++;
transflag = 0;
return (-1);
}
+
+ bare_lfs = 0;
+
switch (type) {
case TYPE_I:
int c;
char line[LINE_MAX];
- (void)snprintf(line, sizeof(line), "/bin/ls -lgA %s", filename);
+ (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename);
fin = ftpd_popen(line, "r");
lreply(211, "status of %s:", filename);
while ((c = getc(fin)) != EOF) {
void
statcmd()
{
- struct sockaddr_in *sin;
+ union sockunion *su;
u_char *a, *p;
+ char hname[INET6_ADDRSTRLEN];
+ int ispassive;
lreply(211, "%s FTP server status:", hostname, version);
printf(" %s\r\n", version);
printf(" Connected to %s", remotehost);
- if (!isdigit(remotehost[0]))
- printf(" (%s)", inet_ntoa(his_addr.sin_addr));
+#if defined(HAVE_GETNAMEINFO)
+ if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
+ hname, sizeof(hname) - 1, NULL, 0,
+ NI_NUMERICHOST|NI_WITHSCOPEID)) {
+ if (strcmp(hname, remotehost) != 0)
+ printf(" (%s)", hname);
+#else
+ {
+ if (!isdigit(remotehost[0]))
+ printf(" (%s)", inet_ntoa(his_addr.su_sin.sin_addr));
+#endif
+ }
printf("\r\n");
if (logged_in) {
if (guest)
if (data != -1)
printf(" Data connection open\r\n");
else if (pdata != -1) {
- printf(" in Passive mode");
- sin = &pasv_addr;
+ ispassive = 1;
+ su = &pasv_addr;
goto printaddr;
} else if (usedefault == 0) {
- printf(" PORT");
- sin = &data_dest;
+ ispassive = 0;
+ su = &data_dest;
printaddr:
- a = (u_char *) &sin->sin_addr;
- p = (u_char *) &sin->sin_port;
#define UC(b) (((int) b) & 0xff)
- printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]),
- UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
+#if defined(VIRTUAL_HOSTING)
+ if (epsvall) {
+ printf(" EPSV only mode (EPSV ALL)\r\n");
+ goto epsvonly;
+ }
+#endif
+
+ /* PORT/PASV */
+ if (su->su_family == AF_INET) {
+ a = (u_char *) &su->su_sin.sin_addr;
+ p = (u_char *) &su->su_sin.sin_port;
+ printf(" %s (%d,%d,%d,%d,%d,%d)\r\n",
+ ispassive ? "PASV" : "PORT",
+ UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
+ UC(p[0]), UC(p[1]));
+ }
+
+ /* LPRT/LPSV */
+ {
+ int alen, af, i;
+
+ switch (su->su_family) {
+ case AF_INET:
+ a = (u_char *) &su->su_sin.sin_addr;
+ p = (u_char *) &su->su_sin.sin_port;
+ alen = sizeof(su->su_sin.sin_addr);
+ af = 4;
+ break;
+ case AF_INET6:
+ a = (u_char *) &su->su_sin6.sin6_addr;
+ p = (u_char *) &su->su_sin6.sin6_port;
+ alen = sizeof(su->su_sin6.sin6_addr);
+ af = 6;
+ break;
+ default:
+ af = 0;
+ break;
+ }
+ if (af) {
+ printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
+ af, alen);
+ for (i = 0; i < alen; i++)
+ printf("%d,", UC(a[i]));
+ printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
+ }
+ }
+
+#if defined(HAVE_GETNAMEINFO)
+epsvonly:;
+ /* EPRT/EPSV */
+ {
+ int af;
+
+ switch (su->su_family) {
+ case AF_INET:
+ af = 1;
+ break;
+ case AF_INET6:
+ af = 2;
+ break;
+ default:
+ af = 0;
+ break;
+ }
+ if (af) {
+ if (!getnameinfo((struct sockaddr *)su, su->su_len,
+ hname, sizeof(hname) - 1, NULL, 0,
+ NI_NUMERICHOST)) {
+ printf(" %s |%d|%s|%d|\r\n",
+ ispassive ? "EPSV" : "EPRT",
+ af, hname, htons(su->su_port));
+ }
+ }
+ }
+#endif
#undef UC
} else
printf(" No data connection\r\n");
{
char *cp;
- if (cp = strchr(cbuf,'\n'))
+ if ((cp = strchr(cbuf,'\n')))
*cp = '\0';
reply(500, "'%s': command not understood.", cbuf);
}
void
pwd()
{
- char path[MAXPATHLEN];
+ char path[MAXPATHLEN + 1];
- if (getcwd(path, sizeof(path)) == (char *)NULL)
+ if (getwd(path) == (char *)NULL)
reply(550, "%s.", path);
else
reply(257, "\"%s\" is current directory.", path);
renamecmd(from, to)
char *from, *to;
{
+ struct stat st;
LOGCMD2("rename", from, to);
+
+ if (guest && (stat(to, &st) == 0)) {
+ reply(550, "%s: permission denied", to);
+ return;
+ }
+
if (rename(from, to) < 0)
perror_reply(550, "rename");
else
}
static void
-dolog(sin)
- struct sockaddr_in *sin;
+dolog(who)
+ struct sockaddr *who;
{
+#if defined(HAVE_GETNAMEINFO)
+ int error;
+#endif
+
+#if defined(HAVE_REALHOSTNAME_SA)
+ realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
+#else
+ struct sockaddr_in *sin = (struct sockaddr_in *)who;
struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
- sizeof(struct in_addr), AF_INET);
+ sizeof(struct in_addr), AF_INET);
+
+ if (hp)
+ (void) strncpy(remotehost, hp->h_name, sizeof(remotehost));
+ else
+ (void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
+ sizeof(remotehost));
+#endif
- if (hp)
- (void) strncpy(remotehost, hp->h_name, sizeof(remotehost));
- else
- (void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
- sizeof(remotehost));
#ifdef SETPROCTITLE
- snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost);
+#ifdef VIRTUAL_HOSTING
+ if (thishost != firsthost)
+ snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
+ remotehost, hostname);
+ else
+#endif
+ snprintf(proctitle, sizeof(proctitle), "%s: connected",
+ remotehost);
setproctitle("%s", proctitle);
#endif /* SETPROCTITLE */
- if (logging)
- syslog(LOG_INFO, "connection from %s", remotehost);
+ if (logging) {
+#ifdef VIRTUAL_HOSTING
+ if (thishost != firsthost)
+ syslog(LOG_INFO, "connection from %s (to %s)",
+ remotehost, hostname);
+ else
+#endif
+ {
+#if defined(HAVE_GETNAMEINFO)
+ char who_name[MAXHOSTNAMELEN];
+
+ error = getnameinfo(who, who->sa_len,
+ who_name, sizeof(who_name) - 1,
+ NULL, 0,
+ NI_NUMERICHOST|NI_WITHSCOPEID);
+ syslog(LOG_INFO, "connection from %s (%s)", remotehost,
+ error == 0 ? who_name : "");
+#else
+ syslog(LOG_INFO, "connection from %s", remotehost);
+#endif
+ }
+ }
}
/*
dologout(status)
int status;
{
- /*
- * Prevent reception of SIGURG from resulting in a resumption
- * back to the main program loop.
- */
- transflag = 0;
+ /*
+ * Prevent reception of SIGURG from resulting in a resumption
+ * back to the main program loop.
+ */
+ transflag = 0;
if (logged_in) {
(void) seteuid((uid_t)0);
- logwtmp(ttyline, "", "");
+ ftpd_logwtmp(ttyline, "", "");
}
/* beware of flushing buffers after a SIGPIPE */
_exit(status);
longjmp(urgcatch, 1);
}
if (strcmp(cp, "STAT\r\n") == 0) {
+ tmpline[0] = '\0';
if (file_size != (off_t) -1)
reply(213, "Status: %qd of %qd bytes transferred",
byte_count, file_size);
int len;
char *p, *a;
- pdata = socket(AF_INET, SOCK_STREAM, 0);
+ if (pdata >= 0) /* close old port if one set */
+ close(pdata);
+
+ pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
if (pdata < 0) {
perror_reply(425, "Can't open passive connection");
return;
}
- pasv_addr = ctrl_addr;
- pasv_addr.sin_port = 0;
+
(void) seteuid((uid_t)0);
- if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) {
- (void) seteuid((uid_t)pw->pw_uid);
- goto pasv_error;
+
+#ifdef IP_PORTRANGE
+ if (ctrl_addr.su_family == AF_INET) {
+ int on = restricted_data_ports ? IP_PORTRANGE_HIGH
+ : IP_PORTRANGE_DEFAULT;
+
+ if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
+ (char *)&on, sizeof(on)) < 0)
+ goto pasv_error;
}
+#endif
+#ifdef IPV6_PORTRANGE
+ if (ctrl_addr.su_family == AF_INET6) {
+ int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
+ : IPV6_PORTRANGE_DEFAULT;
+
+ if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
+ (char *)&on, sizeof(on)) < 0)
+ goto pasv_error;
+ }
+#endif
+
+ pasv_addr = ctrl_addr;
+ pasv_addr.su_port = 0;
+ if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
+ goto pasv_error;
+
(void) seteuid((uid_t)pw->pw_uid);
+
len = sizeof(pasv_addr);
if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
goto pasv_error;
if (listen(pdata, 1) < 0)
goto pasv_error;
- a = (char *) &pasv_addr.sin_addr;
- p = (char *) &pasv_addr.sin_port;
+ if (pasv_addr.su_family == AF_INET)
+ a = (char *) &pasv_addr.su_sin.sin_addr;
+ else if (pasv_addr.su_family == AF_INET6 &&
+ IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
+ a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
+ else
+ goto pasv_error;
+
+ p = (char *) &pasv_addr.su_port;
#define UC(b) (((int) b) & 0xff)
return;
pasv_error:
+ (void) seteuid((uid_t)pw->pw_uid);
+ (void) close(pdata);
+ pdata = -1;
+ perror_reply(425, "Can't open passive connection");
+ return;
+}
+
+/*
+ * Long Passive defined in RFC 1639.
+ * 228 Entering Long Passive Mode
+ * (af, hal, h1, h2, h3,..., pal, p1, p2...)
+ */
+
+void
+long_passive(cmd, pf)
+ char *cmd;
+ int pf;
+{
+ int len;
+ char *p, *a;
+
+ if (pdata >= 0) /* close old port if one set */
+ close(pdata);
+
+ if (pf != PF_UNSPEC) {
+ if (ctrl_addr.su_family != pf) {
+ switch (ctrl_addr.su_family) {
+ case AF_INET:
+ pf = 1;
+ break;
+ case AF_INET6:
+ pf = 2;
+ break;
+ default:
+ pf = 0;
+ break;
+ }
+ /*
+ * XXX
+ * only EPRT/EPSV ready clients will understand this
+ */
+ if (strcmp(cmd, "EPSV") == 0 && pf) {
+ reply(522, "Network protocol mismatch, "
+ "use (%d)", pf);
+ } else
+ reply(501, "Network protocol mismatch"); /*XXX*/
+
+ return;
+ }
+ }
+
+ pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
+ if (pdata < 0) {
+ perror_reply(425, "Can't open passive connection");
+ return;
+ }
+
+ (void) seteuid((uid_t)0);
+
+ pasv_addr = ctrl_addr;
+ pasv_addr.su_port = 0;
+ len = pasv_addr.su_len;
+
+#ifdef IP_PORTRANGE
+ if (ctrl_addr.su_family == AF_INET) {
+ int on = restricted_data_ports ? IP_PORTRANGE_HIGH
+ : IP_PORTRANGE_DEFAULT;
+
+ if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
+ (char *)&on, sizeof(on)) < 0)
+ goto pasv_error;
+ }
+#endif
+#ifdef IPV6_PORTRANGE
+ if (ctrl_addr.su_family == AF_INET6) {
+ int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
+ : IPV6_PORTRANGE_DEFAULT;
+
+ if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
+ (char *)&on, sizeof(on)) < 0)
+ goto pasv_error;
+ }
+#endif
+
+ if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
+ goto pasv_error;
+
+ (void) seteuid((uid_t)pw->pw_uid);
+
+ if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
+ goto pasv_error;
+ if (listen(pdata, 1) < 0)
+ goto pasv_error;
+
+#define UC(b) (((int) b) & 0xff)
+
+ if (strcmp(cmd, "LPSV") == 0) {
+ p = (char *)&pasv_addr.su_port;
+ switch (pasv_addr.su_family) {
+ case AF_INET:
+ a = (char *) &pasv_addr.su_sin.sin_addr;
+ v4_reply:
+ reply(228,
+"Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
+ 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
+ 2, UC(p[0]), UC(p[1]));
+ return;
+ case AF_INET6:
+ if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
+ a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
+ goto v4_reply;
+ }
+ a = (char *) &pasv_addr.su_sin6.sin6_addr;
+ reply(228,
+"Entering Long Passive Mode "
+"(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
+ 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
+ UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
+ UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
+ UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
+ 2, UC(p[0]), UC(p[1]));
+ return;
+ }
+ } else if (strcmp(cmd, "EPSV") == 0) {
+ switch (pasv_addr.su_family) {
+ case AF_INET:
+ case AF_INET6:
+ reply(229, "Entering Extended Passive Mode (|||%d|)",
+ ntohs(pasv_addr.su_port));
+ return;
+ }
+ } else {
+ /* more proper error code? */
+ }
+
+pasv_error:
+ (void) seteuid((uid_t)pw->pw_uid);
(void) close(pdata);
pdata = -1;
perror_reply(425, "Can't open passive connection");
}
if (cp)
*cp = '/';
- (void) strcpy(new, local);
+ /* -4 is for the .nn<null> we put on the end below */
+ (void) snprintf(new, sizeof(new) - 4, "%s", local);
cp = new + strlen(new);
*cp++ = '.';
for (count = 1; count < 100; count++) {
int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
memset(&gl, 0, sizeof(gl));
+ gl.gl_matchc = MAXGLOBARGS;
+#if !defined(GLOB_MAXPATH)
+#define GLOB_MAXPATH 0x1000
+#endif
+ flags |= GLOB_MAXPATH;
freeglob = 1;
if (glob(whichf, flags, 0, &gl)) {
reply(550, "not found");
transflag = 0;
goto out;
}
- while (dirname = *dirlist++) {
+ while ((dirname = *dirlist++)) {
if (stat(dirname, &st) < 0) {
/*
* If user typed "ls -l", etc, and the client
*/
if (dirname[0] == '-' && *dirlist == NULL &&
transflag == 0) {
- retrieve("/bin/ls %s", dirname);
+ retrieve(_PATH_LS " %s", dirname);
goto out;
}
perror_reply(550, whichf);
dir->d_namlen == 2)
continue;
- sprintf(nbuf, "%s/%s", dirname, dir->d_name);
+ snprintf(nbuf, sizeof(nbuf),
+ "%s/%s", dirname, dir->d_name);
/*
* We have to do a stat to insure it's
}
}
-#ifdef SETPROCTITLE
+void
+reapchild(signo)
+ int signo;
+{
+ while (wait3(NULL, WNOHANG, NULL) > 0);
+}
+
+#ifdef OLD_SETPROCTITLE
/*
* Clobber argv so ps will show what we're doing. (Stolen from sendmail.)
* Warning, since this is usually started from inetd.conf, it often doesn't
while (p < LastArgv)
*p++ = ' ';
}
-#endif /* SETPROCTITLE */
-#pragma CC_OPT_RESTORE
+#endif /* OLD_SETPROCTITLE */
+
+static void
+logxfer(name, size, start)
+ char *name;
+ long size;
+ long start;
+{
+ char buf[1024];
+ char path[MAXPATHLEN + 1];
+ time_t now;
+
+ if (statfd >= 0 && getwd(path) != NULL) {
+ time(&now);
+ snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n",
+ ctime(&now)+4, ident, remotehost,
+ path, name, size, now - start + (now == start));
+ write(statfd, buf, strlen(buf));
+ }
+}
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
*/
#ifndef lint
+#if 0
static char sccsid[] = "@(#)logwtmp.c 8.1 (Berkeley) 6/4/93";
+#endif
+static const char rcsid[] =
+ "$FreeBSD: src/libexec/ftpd/logwtmp.c,v 1.9 2000/01/27 09:28:21 shin Exp $";
#endif /* not lint */
#include <sys/types.h>
-#include <sys/time.h>
#include <sys/stat.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
#include <fcntl.h>
+#include <time.h>
+#include <netdb.h>
#include <utmp.h>
#include <unistd.h>
#include <stdio.h>
* after login, but before logout).
*/
void
-logwtmp(line, name, host)
+ftpd_logwtmp(line, name, host)
char *line, *name, *host;
{
struct utmp ut;
struct stat buf;
+#if defined(HAVE_GETNAMEINFO)
+ if (strlen(host) > UT_HOSTSIZE) {
+ struct addrinfo hints, *res;
+ int error;
+ static char hostbuf[BUFSIZ];
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = PF_UNSPEC;
+ error = getaddrinfo(host, NULL, &hints, &res);
+ if (error)
+ host = "invalid hostname";
+ else {
+ getnameinfo(res->ai_addr, res->ai_addrlen,
+ hostbuf, sizeof(hostbuf), NULL, 0,
+ NI_NUMERICHOST);
+ host = hostbuf;
+ if (strlen(host) > UT_HOSTSIZE)
+ host[UT_HOSTSIZE] = '\0';
+ }
+ }
+#else
+ if (strlen(host) > UT_HOSTSIZE) {
+ struct hostent *hp = gethostbyname(host);
+
+ if (hp != NULL) {
+ struct in_addr in;
+
+ memmove(&in, hp->h_addr, sizeof(in));
+ host = inet_ntoa(in);
+ } else
+ host = "invalid hostname";
+ }
+#endif
if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
return;
if (fstat(fd, &buf) == 0) {
--- /dev/null
+/*
+ * Copyright (c) 1989, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static const char copyright[] =
+"@(#) Copyright (c) 1989, 1993, 1994\n\
+ The Regents of the University of California. All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
+#else
+static const char rcsid[] =
+ "$FreeBSD: src/bin/ls/ls.c,v 1.45 2000/08/13 12:17:03 joe Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <dirent.h>
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <limits.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#ifdef COLORLS
+#include <termcap.h>
+#include <signal.h>
+#endif
+
+#include "ls.h"
+#include "ls_extern.h"
+
+#if !defined(HAVE_FFLAGSTOSTR)
+static struct {
+ char *name;
+ u_long flag;
+ int invert;
+} mapping[] = {
+ /* shorter names per flag first, all prefixed by "no" */
+ { "nosappnd", SF_APPEND, 0 },
+ { "nosappend", SF_APPEND, 0 },
+ { "noarch", SF_ARCHIVED, 0 },
+ { "noarchived", SF_ARCHIVED, 0 },
+ { "noschg", SF_IMMUTABLE, 0 },
+ { "noschange", SF_IMMUTABLE, 0 },
+ { "nosimmutable", SF_IMMUTABLE, 0 },
+#if defined(SF_NOUNLINK)
+ { "nosunlnk", SF_NOUNLINK, 0 },
+ { "nosunlink", SF_NOUNLINK, 0 },
+#endif
+ { "nouappnd", UF_APPEND, 0 },
+ { "nouappend", UF_APPEND, 0 },
+ { "nouchg", UF_IMMUTABLE, 0 },
+ { "nouchange", UF_IMMUTABLE, 0 },
+ { "nouimmutable", UF_IMMUTABLE, 0 },
+ { "nodump", UF_NODUMP, 1 },
+ { "noopaque", UF_OPAQUE, 0 }
+#if defined(UF_NOUNLINK)
+,
+ { "nouunlnk", UF_NOUNLINK, 0 },
+ { "nouunlink", UF_NOUNLINK, 0 }
+#endif
+};
+#define longestflaglen 12
+#define nmappings (sizeof(mapping) / sizeof(mapping[0]))
+
+/*
+ * fflagstostr --
+ * Convert file flags to a comma-separated string. If no flags
+ * are set, return the empty string.
+ */
+char *
+fflagstostr(flags)
+ u_long flags;
+{
+ char *string;
+ char *sp, *dp;
+ u_long setflags;
+ int i;
+
+ if ((string = (char *)malloc(nmappings * (longestflaglen + 1))) == NULL)
+ return (NULL);
+
+ setflags = flags;
+ dp = string;
+ for (i = 0; i < nmappings; i++) {
+ if (setflags & mapping[i].flag) {
+ if (dp > string)
+ *dp++ = ',';
+ for (sp = mapping[i].invert ? mapping[i].name :
+ mapping[i].name + 2; *sp; *dp++ = *sp++) ;
+ setflags &= ~mapping[i].flag;
+ }
+ }
+ *dp = '\0';
+ return (string);
+}
+#endif
+
+/*
+ * Upward approximation of the maximum number of characters needed to
+ * represent a value of integral type t as a string, excluding the
+ * NUL terminator, with provision for a sign.
+ */
+#define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1)
+
+static void display __P((FTSENT *, FTSENT *));
+static u_quad_t makenines __P((u_long));
+static int mastercmp __P((const FTSENT **, const FTSENT **));
+static void traverse __P((int, char **, int));
+
+static void (*printfcn) __P((DISPLAY *));
+static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
+
+long blocksize; /* block size units */
+int termwidth = 80; /* default terminal width */
+
+/* flags */
+int f_accesstime; /* use time of last access */
+int f_column; /* columnated format */
+int f_flags; /* show flags associated with a file */
+int f_inode; /* print inode */
+int f_kblocks; /* print size in kilobytes */
+int f_listdir; /* list actual directory, not contents */
+int f_listdot; /* list files beginning with . */
+int f_longform; /* long listing format */
+int f_nonprint; /* show unprintables as ? */
+int f_nosort; /* don't sort output */
+int f_notabs; /* don't use tab-separated multi-col output */
+int f_numericonly; /* don't convert uid/gid to name */
+int f_octal; /* show unprintables as \xxx */
+int f_octal_escape; /* like f_octal but use C escapes if possible */
+int f_recursive; /* ls subdirectories also */
+int f_reversesort; /* reverse whatever sort is used */
+int f_sectime; /* print the real time for all files */
+int f_singlecol; /* use single column output */
+int f_size; /* list size in short listing */
+int f_statustime; /* use time of last mode change */
+int f_timesort; /* sort by time vice name */
+int f_type; /* add type character for non-regular files */
+int f_whiteout; /* show whiteout entries */
+#ifdef COLORLS
+int f_color; /* add type in color for non-regular files */
+
+char *ansi_bgcol; /* ANSI sequence to set background colour */
+char *ansi_fgcol; /* ANSI sequence to set foreground colour */
+char *ansi_coloff; /* ANSI sequence to reset colours */
+#endif
+
+int rval;
+
+int
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ static char dot[] = ".", *dotav[] = { dot, NULL };
+ struct winsize win;
+ int ch, fts_options, notused;
+ char *p;
+
+#ifdef COLORLS
+ char termcapbuf[1024]; /* termcap definition buffer */
+ char tcapbuf[512]; /* capability buffer */
+ char *bp = tcapbuf;
+#endif
+
+ (void) setlocale(LC_ALL, "");
+
+ /* Terminal defaults to -Cq, non-terminal defaults to -1. */
+ if (isatty(STDOUT_FILENO)) {
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
+ !win.ws_col) {
+ if ((p = getenv("COLUMNS")) != NULL)
+ termwidth = atoi(p);
+ }
+ else
+ termwidth = win.ws_col;
+ f_column = f_nonprint = 1;
+ } else {
+ f_singlecol = 1;
+ /* retrieve environment variable, in case of explicit -C */
+ if ((p = getenv("COLUMNS")))
+ termwidth = atoi(p);
+ }
+
+ /* Root is -A automatically. */
+ if (!getuid())
+ f_listdot = 1;
+
+ fts_options = FTS_PHYSICAL;
+ while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgiklnoqrstu")) != -1) {
+ switch (ch) {
+ /*
+ * The -1, -C and -l options all override each other so shell
+ * aliasing works right.
+ */
+ case '1':
+ f_singlecol = 1;
+ f_column = f_longform = 0;
+ break;
+ case 'B':
+ f_nonprint = 0;
+ f_octal = 1;
+ f_octal_escape = 0;
+ break;
+ case 'C':
+ f_column = 1;
+ f_longform = f_singlecol = 0;
+ break;
+ case 'l':
+ f_longform = 1;
+ f_column = f_singlecol = 0;
+ break;
+ /* The -c and -u options override each other. */
+ case 'c':
+ f_statustime = 1;
+ f_accesstime = 0;
+ break;
+ case 'u':
+ f_accesstime = 1;
+ f_statustime = 0;
+ break;
+ case 'F':
+ f_type = 1;
+ break;
+ case 'H':
+ fts_options |= FTS_COMFOLLOW;
+ break;
+ case 'G':
+ setenv("CLICOLOR", "", 1);
+ break;
+ case 'L':
+ fts_options &= ~FTS_PHYSICAL;
+ fts_options |= FTS_LOGICAL;
+ break;
+ case 'P':
+ fts_options &= ~FTS_COMFOLLOW;
+ fts_options &= ~FTS_LOGICAL;
+ fts_options |= FTS_PHYSICAL;
+ break;
+ case 'R':
+ f_recursive = 1;
+ break;
+ case 'a':
+ fts_options |= FTS_SEEDOT;
+ /* FALLTHROUGH */
+ case 'A':
+ f_listdot = 1;
+ break;
+ /* The -d option turns off the -R option. */
+ case 'd':
+ f_listdir = 1;
+ f_recursive = 0;
+ break;
+ case 'f':
+ f_nosort = 1;
+ break;
+ case 'g': /* Compatibility with 4.3BSD. */
+ break;
+ case 'i':
+ f_inode = 1;
+ break;
+ case 'k':
+ f_kblocks = 1;
+ break;
+ case 'n':
+ f_numericonly = 1;
+ break;
+ case 'o':
+ f_flags = 1;
+ break;
+ case 'q':
+ f_nonprint = 1;
+ f_octal = 0;
+ f_octal_escape = 0;
+ break;
+ case 'r':
+ f_reversesort = 1;
+ break;
+ case 's':
+ f_size = 1;
+ break;
+ case 'T':
+ f_sectime = 1;
+ break;
+ case 't':
+ f_timesort = 1;
+ break;
+ case 'W':
+ f_whiteout = 1;
+ break;
+ case 'b':
+ f_nonprint = 0;
+ f_octal = 0;
+ f_octal_escape = 1;
+ break;
+ default:
+ case '?':
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ /* Enabling of colours is conditional on the environment. */
+ if (getenv("CLICOLOR") &&
+ (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
+#ifdef COLORLS
+ if (tgetent(termcapbuf, getenv("TERM")) == 1) {
+ ansi_fgcol = tgetstr("AF", &bp);
+ ansi_bgcol = tgetstr("AB", &bp);
+
+ /* To switch colours off use 'op' if
+ * available, otherwise use 'oc', or
+ * don't do colours at all. */
+ ansi_coloff = tgetstr("op", &bp);
+ if (!ansi_coloff)
+ ansi_coloff = tgetstr("oc", &bp);
+ if (ansi_fgcol && ansi_bgcol && ansi_coloff)
+ f_color = 1;
+ }
+#else
+ (void)fprintf(stderr, "Color support not compiled in.\n");
+#endif /*COLORLS*/
+
+#ifdef COLORLS
+ if (f_color) {
+ /*
+ * We can't put tabs and color sequences together:
+ * column number will be incremented incorrectly
+ * for "stty oxtabs" mode.
+ */
+ f_notabs = 1;
+ (void) signal(SIGINT, colorquit);
+ (void) signal(SIGQUIT, colorquit);
+ parsecolors(getenv("LSCOLORS"));
+ }
+#endif
+
+ /*
+ * If not -F, -i, -l, -s or -t options, don't require stat
+ * information, unless in color mode in which case we do
+ * need this to determine which colors to display.
+ */
+ if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
+#ifdef COLORLS
+ && !f_color
+#endif
+ )
+ fts_options |= FTS_NOSTAT;
+
+ /*
+ * If not -F, -d or -l options, follow any symbolic links listed on
+ * the command line.
+ */
+ if (!f_longform && !f_listdir && !f_type)
+ fts_options |= FTS_COMFOLLOW;
+
+ /*
+ * If -W, show whiteout entries
+ */
+#ifdef FTS_WHITEOUT
+ if (f_whiteout)
+ fts_options |= FTS_WHITEOUT;
+#endif
+
+ /* If -l or -s, figure out block size. */
+ if (f_longform || f_size) {
+ if (f_kblocks)
+ blocksize = 2;
+ else {
+ (void)getbsize(¬used, &blocksize);
+ blocksize /= 512;
+ }
+ }
+
+ /* Select a sort function. */
+ if (f_reversesort) {
+ if (!f_timesort)
+ sortfcn = revnamecmp;
+ else if (f_accesstime)
+ sortfcn = revacccmp;
+ else if (f_statustime)
+ sortfcn = revstatcmp;
+ else /* Use modification time. */
+ sortfcn = revmodcmp;
+ } else {
+ if (!f_timesort)
+ sortfcn = namecmp;
+ else if (f_accesstime)
+ sortfcn = acccmp;
+ else if (f_statustime)
+ sortfcn = statcmp;
+ else /* Use modification time. */
+ sortfcn = modcmp;
+ }
+
+ /* Select a print function. */
+ if (f_singlecol)
+ printfcn = printscol;
+ else if (f_longform)
+ printfcn = printlong;
+ else
+ printfcn = printcol;
+
+ if (argc)
+ traverse(argc, argv, fts_options);
+ else
+ traverse(1, dotav, fts_options);
+ exit(rval);
+}
+
+static int output; /* If anything output. */
+
+/*
+ * Traverse() walks the logical directory structure specified by the argv list
+ * in the order specified by the mastercmp() comparison function. During the
+ * traversal it passes linked lists of structures to display() which represent
+ * a superset (may be exact set) of the files to be displayed.
+ */
+static void
+traverse(argc, argv, options)
+ int argc, options;
+ char *argv[];
+{
+ FTS *ftsp;
+ FTSENT *p, *chp;
+ int ch_options;
+
+ if ((ftsp =
+ fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
+ err(1, NULL);
+
+ display(NULL, fts_children(ftsp, 0));
+ if (f_listdir)
+ return;
+
+ /*
+ * If not recursing down this tree and don't need stat info, just get
+ * the names.
+ */
+ ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
+
+ while ((p = fts_read(ftsp)) != NULL)
+ switch (p->fts_info) {
+ case FTS_DC:
+ warnx("%s: directory causes a cycle", p->fts_name);
+ break;
+ case FTS_DNR:
+ case FTS_ERR:
+ warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
+ rval = 1;
+ break;
+ case FTS_D:
+ if (p->fts_level != FTS_ROOTLEVEL &&
+ p->fts_name[0] == '.' && !f_listdot)
+ break;
+
+ /*
+ * If already output something, put out a newline as
+ * a separator. If multiple arguments, precede each
+ * directory with its name.
+ */
+ if (output)
+ (void)printf("\n%s:\n", p->fts_path);
+ else if (argc > 1) {
+ (void)printf("%s:\n", p->fts_path);
+ output = 1;
+ }
+
+ chp = fts_children(ftsp, ch_options);
+ display(p, chp);
+
+ if (!f_recursive && chp != NULL)
+ (void)fts_set(ftsp, p, FTS_SKIP);
+ break;
+ }
+ if (errno)
+ err(1, "fts_read");
+}
+
+/*
+ * Display() takes a linked list of FTSENT structures and passes the list
+ * along with any other necessary information to the print function. P
+ * points to the parent directory of the display list.
+ */
+static void
+display(p, list)
+ FTSENT *p, *list;
+{
+ struct stat *sp;
+ DISPLAY d;
+ FTSENT *cur;
+ NAMES *np;
+ u_quad_t maxsize;
+ u_long btotal, maxblock, maxinode, maxlen, maxnlink;
+ int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
+ char *initmax;
+ int entries, needstats;
+ char *user, *group, *flags;
+ char buf[STRBUF_SIZEOF(u_quad_t) + 1];
+ char ngroup[STRBUF_SIZEOF(uid_t) + 1];
+ char nuser[STRBUF_SIZEOF(gid_t) + 1];
+
+ /*
+ * If list is NULL there are two possibilities: that the parent
+ * directory p has no children, or that fts_children() returned an
+ * error. We ignore the error case since it will be replicated
+ * on the next call to fts_read() on the post-order visit to the
+ * directory p, and will be signaled in traverse().
+ */
+ if (list == NULL)
+ return;
+
+ needstats = f_inode || f_longform || f_size;
+ flen = 0;
+ btotal = 0;
+ initmax = getenv("LS_COLWIDTHS");
+ /* Fields match -lios order. New ones should be added at the end. */
+ if (initmax != NULL && *initmax != '\0') {
+ char *initmax2, *jinitmax;
+ int ninitmax;
+
+ /* Fill-in "::" as "0:0:0" for the sake of scanf. */
+ jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
+ if (jinitmax == NULL)
+ err(1, NULL);
+ if (*initmax == ':')
+ strcpy(initmax2, "0:"), initmax2 += 2;
+ else
+ *initmax2++ = *initmax, *initmax2 = '\0';
+ for (initmax++; *initmax != '\0'; initmax++) {
+ if (initmax[-1] == ':' && initmax[0] == ':') {
+ *initmax2++ = '0';
+ *initmax2++ = initmax[0];
+ initmax2[1] = '\0';
+ } else {
+ *initmax2++ = initmax[0];
+ initmax2[1] = '\0';
+ }
+ }
+ if (initmax2[-1] == ':') strcpy(initmax2, "0");
+
+ ninitmax = sscanf(jinitmax,
+ " %lu : %lu : %lu : %i : %i : %i : %qu : %lu ",
+ &maxinode, &maxblock, &maxnlink, &maxuser,
+ &maxgroup, &maxflags, &maxsize, &maxlen);
+ f_notabs = 1;
+ switch (ninitmax) {
+ case 0: maxinode = 0;
+ case 1: maxblock = 0;
+ case 2: maxnlink = 0;
+ case 3: maxuser = 0;
+ case 4: maxgroup = 0;
+ case 5: maxflags = 0;
+ case 6: maxsize = 0;
+ case 7: maxlen = 0;
+#ifdef COLORLS
+ if (!f_color)
+#endif
+ f_notabs = 0;
+ }
+ maxinode = makenines(maxinode);
+ maxblock = makenines(maxblock);
+ maxnlink = makenines(maxnlink);
+ maxsize = makenines(maxsize);
+ } else if (initmax == NULL || *initmax == '\0')
+ maxblock = maxinode = maxlen = maxnlink =
+ maxuser = maxgroup = maxflags = maxsize = 0;
+ bcfile = 0;
+ flags = NULL;
+ for (cur = list, entries = 0; cur; cur = cur->fts_link) {
+ if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
+ warnx("%s: %s",
+ cur->fts_name, strerror(cur->fts_errno));
+ cur->fts_number = NO_PRINT;
+ rval = 1;
+ continue;
+ }
+
+ /*
+ * P is NULL if list is the argv list, to which different rules
+ * apply.
+ */
+ if (p == NULL) {
+ /* Directories will be displayed later. */
+ if (cur->fts_info == FTS_D && !f_listdir) {
+ cur->fts_number = NO_PRINT;
+ continue;
+ }
+ } else {
+ /* Only display dot file if -a/-A set. */
+ if (cur->fts_name[0] == '.' && !f_listdot) {
+ cur->fts_number = NO_PRINT;
+ continue;
+ }
+ }
+ if (cur->fts_namelen > maxlen)
+ maxlen = cur->fts_namelen;
+ if (f_octal || f_octal_escape) {
+ int t = len_octal(cur->fts_name, cur->fts_namelen);
+ if (t > maxlen) maxlen = t;
+ }
+ if (needstats) {
+ sp = cur->fts_statp;
+ if (sp->st_blocks > maxblock)
+ maxblock = sp->st_blocks;
+ if (sp->st_ino > maxinode)
+ maxinode = sp->st_ino;
+ if (sp->st_nlink > maxnlink)
+ maxnlink = sp->st_nlink;
+ if (sp->st_size > maxsize)
+ maxsize = sp->st_size;
+
+ btotal += sp->st_blocks;
+ if (f_longform) {
+ if (f_numericonly) {
+ (void)snprintf(nuser, sizeof(nuser),
+ "%u", sp->st_uid);
+ (void)snprintf(ngroup, sizeof(ngroup),
+ "%u", sp->st_gid);
+ user = nuser;
+ group = ngroup;
+ } else {
+ user = user_from_uid(sp->st_uid, 0);
+ group = group_from_gid(sp->st_gid, 0);
+ }
+ if ((ulen = strlen(user)) > maxuser)
+ maxuser = ulen;
+ if ((glen = strlen(group)) > maxgroup)
+ maxgroup = glen;
+ if (f_flags) {
+ flags = fflagstostr(sp->st_flags);
+ if (flags != NULL && *flags == '\0') {
+ free(flags);
+ flags = strdup("-");
+ }
+ if (flags == NULL)
+ err(1, NULL);
+ if ((flen = strlen(flags)) > maxflags)
+ maxflags = flen;
+ } else
+ flen = 0;
+
+ if ((np = malloc(sizeof(NAMES) +
+ ulen + glen + flen + 3)) == NULL)
+ err(1, NULL);
+
+ np->user = &np->data[0];
+ (void)strcpy(np->user, user);
+ np->group = &np->data[ulen + 1];
+ (void)strcpy(np->group, group);
+
+ if (S_ISCHR(sp->st_mode) ||
+ S_ISBLK(sp->st_mode))
+ bcfile = 1;
+
+ if (f_flags) {
+ np->flags = &np->data[ulen + glen + 2];
+ (void)strcpy(np->flags, flags);
+ free(flags);
+ }
+ cur->fts_pointer = np;
+ }
+ }
+ ++entries;
+ }
+
+ if (!entries)
+ return;
+
+ d.list = list;
+ d.entries = entries;
+ d.maxlen = maxlen;
+ if (needstats) {
+ d.bcfile = bcfile;
+ d.btotal = btotal;
+ (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
+ d.s_block = strlen(buf);
+ d.s_flags = maxflags;
+ d.s_group = maxgroup;
+ (void)snprintf(buf, sizeof(buf), "%lu", maxinode);
+ d.s_inode = strlen(buf);
+ (void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
+ d.s_nlink = strlen(buf);
+ (void)snprintf(buf, sizeof(buf), "%qu", maxsize);
+ d.s_size = strlen(buf);
+ d.s_user = maxuser;
+ }
+
+ printfcn(&d);
+ output = 1;
+
+ if (f_longform)
+ for (cur = list; cur; cur = cur->fts_link)
+ free(cur->fts_pointer);
+}
+
+/*
+ * Ordering for mastercmp:
+ * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
+ * as larger than directories. Within either group, use the sort function.
+ * All other levels use the sort function. Error entries remain unsorted.
+ */
+static int
+mastercmp(a, b)
+ const FTSENT **a, **b;
+{
+ int a_info, b_info;
+
+ a_info = (*a)->fts_info;
+ if (a_info == FTS_ERR)
+ return (0);
+ b_info = (*b)->fts_info;
+ if (b_info == FTS_ERR)
+ return (0);
+
+ if (a_info == FTS_NS || b_info == FTS_NS)
+ return (namecmp(*a, *b));
+
+ if (a_info != b_info &&
+ (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
+ if (a_info == FTS_D)
+ return (1);
+ if (b_info == FTS_D)
+ return (-1);
+ }
+ return (sortfcn(*a, *b));
+}
+
+/*
+ * Makenines() returns (10**n)-1. This is useful for converting a width
+ * into a number that wide in decimal.
+ */
+static u_quad_t
+makenines(n)
+ u_long n;
+{
+ u_long i;
+ u_quad_t reg;
+
+ reg = 1;
+ /* Use a loop instead of pow(), since all values of n are small. */
+ for (i = 0; i < n; i++)
+ reg *= 10;
+ reg--;
+
+ return reg;
+}
--- /dev/null
+
+/*
+ * Copyright (c) 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * from: @(#)ls.h 8.1 (Berkeley) 5/31/93
+ * $FreeBSD: src/bin/ls/ls.h,v 1.14 2000/07/04 23:09:23 assar Exp $
+ */
+
+#define NO_PRINT 1
+
+extern long blocksize; /* block size units */
+
+extern int f_accesstime; /* use time of last access */
+extern int f_flags; /* show flags associated with a file */
+extern int f_inode; /* print inode */
+extern int f_longform; /* long listing format */
+extern int f_octal; /* print unprintables in octal */
+extern int f_octal_escape; /* like f_octal but use C escapes if possible */
+extern int f_nonprint; /* show unprintables as ? */
+extern int f_sectime; /* print the real time for all files */
+extern int f_size; /* list size in short listing */
+extern int f_statustime; /* use time of last mode change */
+extern int f_notabs; /* don't use tab-separated multi-col output */
+extern int f_type; /* add type character for non-regular files */
+#ifdef COLORLS
+extern int f_color; /* add type in color for non-regular files */
+#endif
+
+typedef struct {
+ FTSENT *list;
+ u_long btotal;
+ int bcfile;
+ int entries;
+ int maxlen;
+ int s_block;
+ int s_flags;
+ int s_group;
+ int s_inode;
+ int s_nlink;
+ int s_size;
+ int s_user;
+} DISPLAY;
+
+typedef struct {
+ char *user;
+ char *group;
+ char *flags;
+ char data[1];
+} NAMES;
--- /dev/null
+/*-
+ * Copyright (c) 1991, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * from: @(#)extern.h 8.1 (Berkeley) 5/31/93
+ * $FreeBSD: src/bin/ls/extern.h,v 1.14 2000/07/04 23:09:23 assar Exp $
+ */
+
+int acccmp __P((const FTSENT *, const FTSENT *));
+int revacccmp __P((const FTSENT *, const FTSENT *));
+int modcmp __P((const FTSENT *, const FTSENT *));
+int revmodcmp __P((const FTSENT *, const FTSENT *));
+int namecmp __P((const FTSENT *, const FTSENT *));
+int revnamecmp __P((const FTSENT *, const FTSENT *));
+int statcmp __P((const FTSENT *, const FTSENT *));
+int revstatcmp __P((const FTSENT *, const FTSENT *));
+
+void printcol __P((DISPLAY *));
+void printlong __P((DISPLAY *));
+void printscol __P((DISPLAY *));
+void usage __P((void));
+int len_octal __P((const char *, int));
+int prn_octal __P((const char *));
+int prn_printable __P((const char *));
+#ifdef COLORLS
+void parsecolors __P((char *cs));
+void colorquit __P((int));
+
+extern char *ansi_fgcol;
+extern char *ansi_bgcol;
+extern char *ansi_coloff;
+#endif
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 6/4/93
+ * $FreeBSD: src/libexec/ftpd/pathnames.h,v 1.11 1999/08/28 00:09:31 peter Exp $
*/
#include <paths.h>
-#define _PATH_FTPUSERS "/etc/ftpusers"
+#define _PATH_FTPCHROOT "/etc/ftpchroot"
#define _PATH_FTPWELCOME "/etc/ftpwelcome"
-#define _PATH_FTPLOGINMESG "/etc/motd"
+#define _PATH_FTPLOGINMESG "/etc/ftpmotd"
+#define _PATH_FTPHOSTS "/etc/ftphosts"
+#define _PATH_FTPDSTATFILE "/var/log/ftpd"
+#define _PATH_LS "/bin/ls"
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
*/
#ifndef lint
+#if 0
static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94";
+#endif
+static const char rcsid[] =
+ "$FreeBSD: src/libexec/ftpd/popen.c,v 1.20 2001/03/19 19:11:00 jlemon Exp $";
#endif /* not lint */
#include <sys/types.h>
#include <sys/wait.h>
+#include <netinet/in.h>
#include <errno.h>
#include <glob.h>
#include <unistd.h>
#include "extern.h"
+#include "pathnames.h"
+#include <syslog.h>
+#include <time.h>
+#include <varargs.h>
+
+#define MAXUSRARGS 100
+#define MAXGLOBARGS 1000
/*
* Special version of popen which avoids call to shell. This ensures noone
char *cp;
FILE *iop;
int argc, gargc, pdes[2], pid;
- char **pop, *argv[100], *gargv[1000];
+ char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS];
- if (*type != 'r' && *type != 'w' || type[1])
+ if (((*type != 'r') && (*type != 'w')) || type[1])
return (NULL);
if (!pids) {
return (NULL);
/* break up string into pieces */
- for (argc = 0, cp = program;; cp = NULL)
+ for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) {
if (!(argv[argc++] = strtok(cp, " \t\n")))
break;
+ }
+ argv[argc - 1] = NULL;
/* glob each piece */
gargv[0] = argv[0];
- for (gargc = argc = 1; argv[argc]; argc++) {
+ for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) {
glob_t gl;
int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
memset(&gl, 0, sizeof(gl));
+ gl.gl_matchc = MAXGLOBARGS;
+#if !defined(GLOB_MAXPATH)
+#define GLOB_MAXPATH 0x1000
+#endif
+ flags |= GLOB_MAXPATH;
if (glob(argv[argc], flags, NULL, &gl))
gargv[gargc++] = strdup(argv[argc]);
else
- for (pop = gl.gl_pathv; *pop; pop++)
+ for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1);
+ pop++)
gargv[gargc++] = strdup(*pop);
globfree(&gl);
}
gargv[gargc] = NULL;
iop = NULL;
- switch(pid = vfork()) {
+ fflush(NULL);
+ pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork();
+ switch(pid) {
case -1: /* error */
(void)close(pdes[0]);
(void)close(pdes[1]);
}
(void)close(pdes[1]);
}
+ if (strcmp(gargv[0], _PATH_LS) == 0) {
+ /* Reset getopt for ls_main() */
+ optreset = optind = optopt = 1;
+ /* Close syslogging to remove pwd.db missing msgs */
+ closelog();
+ /* Trigger to sense new /etc/localtime after chroot */
+ if (getenv("TZ") == NULL) {
+ setenv("TZ", "", 0);
+ tzset();
+ unsetenv("TZ");
+ tzset();
+ }
+ exit(ls_main(gargc, gargv));
+ }
execv(gargv[0], gargv);
_exit(1);
}
--- /dev/null
+/*
+ * Copyright (c) 1989, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
+#else
+static const char rcsid[] =
+ "$FreeBSD: src/bin/ls/print.c,v 1.38 2001/03/21 15:14:47 ache Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fts.h>
+#include <grp.h>
+//#include <langinfo.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#ifdef COLORLS
+#include <ctype.h>
+#include <termcap.h>
+#include <signal.h>
+#endif
+
+#include "ls.h"
+#include "ls_extern.h"
+
+static int printaname __P((FTSENT *, u_long, u_long));
+static void printlink __P((FTSENT *));
+static void printtime __P((time_t));
+static int printtype __P((u_int));
+#ifdef COLORLS
+static void endcolor __P((int));
+static int colortype __P((mode_t));
+#endif
+
+#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
+
+#ifdef COLORLS
+/* Most of these are taken from <sys/stat.h> */
+typedef enum Colors {
+ C_DIR, /* directory */
+ C_LNK, /* symbolic link */
+ C_SOCK, /* socket */
+ C_FIFO, /* pipe */
+ C_EXEC, /* executable */
+ C_BLK, /* block special */
+ C_CHR, /* character special */
+ C_SUID, /* setuid executable */
+ C_SGID, /* setgid executable */
+ C_WSDIR, /* directory writeble to others, with sticky bit */
+ C_WDIR, /* directory writeble to others, without sticky bit */
+ C_NUMCOLORS /* just a place-holder */
+} Colors ;
+
+char *defcolors = "4x5x2x3x1x464301060203";
+
+static int colors[C_NUMCOLORS][2];
+#endif
+
+void
+printscol(dp)
+ DISPLAY *dp;
+{
+ FTSENT *p;
+
+ for (p = dp->list; p; p = p->fts_link) {
+ if (IS_NOPRINT(p))
+ continue;
+ (void)printaname(p, dp->s_inode, dp->s_block);
+ (void)putchar('\n');
+ }
+}
+
+/*
+ * print name in current style
+ */
+static int
+printname(name)
+ const char *name;
+{
+ if (f_octal || f_octal_escape)
+ return prn_octal(name);
+ else if (f_nonprint)
+ return prn_printable(name);
+ else
+ return printf("%s", name);
+}
+
+void
+printlong(dp)
+ DISPLAY *dp;
+{
+ struct stat *sp;
+ FTSENT *p;
+ NAMES *np;
+ char buf[20];
+#ifdef COLORLS
+ int color_printed = 0;
+#endif
+
+ if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
+ (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
+
+ for (p = dp->list; p; p = p->fts_link) {
+ if (IS_NOPRINT(p))
+ continue;
+ sp = p->fts_statp;
+ if (f_inode)
+ (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
+ if (f_size)
+ (void)printf("%*qd ",
+ dp->s_block, howmany(sp->st_blocks, blocksize));
+ (void)strmode(sp->st_mode, buf);
+ np = p->fts_pointer;
+ (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
+ sp->st_nlink, dp->s_user, np->user, dp->s_group,
+ np->group);
+ if (f_flags)
+ (void)printf("%-*s ", dp->s_flags, np->flags);
+ if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
+ if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
+ (void)printf("%3d, 0x%08x ",
+ major(sp->st_rdev),
+ (u_int)minor(sp->st_rdev));
+ else
+ (void)printf("%3d, %3d ",
+ major(sp->st_rdev), minor(sp->st_rdev));
+ else if (dp->bcfile)
+ (void)printf("%*s%*qd ",
+ 8 - dp->s_size, "", dp->s_size, sp->st_size);
+ else
+ (void)printf("%*qd ", dp->s_size, sp->st_size);
+ if (f_accesstime)
+ printtime(sp->st_atime);
+ else if (f_statustime)
+ printtime(sp->st_ctime);
+ else
+ printtime(sp->st_mtime);
+#ifdef COLORLS
+ if (f_color)
+ color_printed = colortype(sp->st_mode);
+#endif
+ (void)printname(p->fts_name);
+#ifdef COLORLS
+ if (f_color && color_printed)
+ endcolor(0);
+#endif
+ if (f_type)
+ (void)printtype(sp->st_mode);
+ if (S_ISLNK(sp->st_mode))
+ printlink(p);
+ (void)putchar('\n');
+ }
+}
+
+void
+printcol(dp)
+ DISPLAY *dp;
+{
+ extern int termwidth;
+ static FTSENT **array;
+ static int lastentries = -1;
+ FTSENT *p;
+ int base, chcnt, cnt, col, colwidth, num;
+ int endcol, numcols, numrows, row;
+ int tabwidth;
+
+ if (f_notabs)
+ tabwidth = 1;
+ else
+ tabwidth = 8;
+
+ /*
+ * Have to do random access in the linked list -- build a table
+ * of pointers.
+ */
+ if (dp->entries > lastentries) {
+ lastentries = dp->entries;
+ if ((array =
+ realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
+ warn(NULL);
+ printscol(dp);
+ }
+ }
+ for (p = dp->list, num = 0; p; p = p->fts_link)
+ if (p->fts_number != NO_PRINT)
+ array[num++] = p;
+
+ colwidth = dp->maxlen;
+ if (f_inode)
+ colwidth += dp->s_inode + 1;
+ if (f_size)
+ colwidth += dp->s_block + 1;
+ if (f_type)
+ colwidth += 1;
+
+ colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
+ if (termwidth < 2 * colwidth) {
+ printscol(dp);
+ return;
+ }
+
+ numcols = termwidth / colwidth;
+ numrows = num / numcols;
+ if (num % numcols)
+ ++numrows;
+
+ if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
+ (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
+ for (row = 0; row < numrows; ++row) {
+ endcol = colwidth;
+ for (base = row, chcnt = col = 0; col < numcols; ++col) {
+ chcnt += printaname(array[base], dp->s_inode,
+ dp->s_block);
+ if ((base += numrows) >= num)
+ break;
+ while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
+ <= endcol){
+ (void)putchar(f_notabs ? ' ' : '\t');
+ chcnt = cnt;
+ }
+ endcol += colwidth;
+ }
+ (void)putchar('\n');
+ }
+}
+
+/*
+ * print [inode] [size] name
+ * return # of characters printed, no trailing characters.
+ */
+static int
+printaname(p, inodefield, sizefield)
+ FTSENT *p;
+ u_long sizefield, inodefield;
+{
+ struct stat *sp;
+ int chcnt;
+#ifdef COLORLS
+ int color_printed = 0;
+#endif
+
+ sp = p->fts_statp;
+ chcnt = 0;
+ if (f_inode)
+ chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
+ if (f_size)
+ chcnt += printf("%*qd ",
+ (int)sizefield, howmany(sp->st_blocks, blocksize));
+#ifdef COLORLS
+ if (f_color)
+ color_printed = colortype(sp->st_mode);
+#endif
+ chcnt += printname(p->fts_name);
+#ifdef COLORLS
+ if (f_color && color_printed)
+ endcolor(0);
+#endif
+ if (f_type)
+ chcnt += printtype(sp->st_mode);
+ return (chcnt);
+}
+
+static void
+printtime(ftime)
+ time_t ftime;
+{
+ char longstring[80];
+ static time_t now;
+ const char *format;
+ static int d_first = -1;
+
+ if (d_first < 0)
+#if defined(HAVE_LANGINFO)
+ d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
+#else
+ d_first = 0;
+#endif
+ if (now == 0)
+ now = time(NULL);
+
+#define SIXMONTHS ((365 / 2) * 86400)
+ if (f_sectime)
+ /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
+ format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
+ else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
+ /* mmm dd hh:mm || dd mmm hh:mm */
+ format = d_first ? "%e %b %R " : "%b %e %R ";
+ else
+ /* mmm dd yyyy || dd mmm yyyy */
+ format = d_first ? "%e %b %Y " : "%b %e %Y ";
+ strftime(longstring, sizeof(longstring), format, localtime(&ftime));
+ fputs(longstring, stdout);
+}
+
+static int
+printtype(mode)
+ u_int mode;
+{
+ switch (mode & S_IFMT) {
+ case S_IFDIR:
+ (void)putchar('/');
+ return (1);
+ case S_IFIFO:
+ (void)putchar('|');
+ return (1);
+ case S_IFLNK:
+ (void)putchar('@');
+ return (1);
+ case S_IFSOCK:
+ (void)putchar('=');
+ return (1);
+ case S_IFWHT:
+ (void)putchar('%');
+ return (1);
+ }
+ if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
+ (void)putchar('*');
+ return (1);
+ }
+ return (0);
+}
+
+#ifdef COLORLS
+static int
+putch(c)
+ int c;
+{
+ (void) putchar(c);
+ return 0;
+}
+
+static int
+writech(c)
+ int c;
+{
+ char tmp = c;
+
+ (void) write(STDOUT_FILENO, &tmp, 1);
+ return 0;
+}
+
+static void
+printcolor(c)
+ Colors c;
+{
+ char *ansiseq;
+
+ if (colors[c][0] != -1) {
+ ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]);
+ if (ansiseq)
+ tputs(ansiseq, 1, putch);
+ }
+
+ if (colors[c][1] != -1) {
+ ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]);
+ if (ansiseq)
+ tputs(ansiseq, 1, putch);
+ }
+}
+
+static void
+endcolor(sig)
+ int sig;
+{
+ tputs(ansi_coloff, 1, sig ? writech : putch);
+}
+
+static int
+colortype(mode)
+ mode_t mode;
+{
+ switch(mode & S_IFMT) {
+ case S_IFDIR:
+ if (mode & S_IWOTH)
+ if (mode & S_ISTXT)
+ printcolor(C_WSDIR);
+ else
+ printcolor(C_WDIR);
+ else
+ printcolor(C_DIR);
+ return(1);
+ case S_IFLNK:
+ printcolor(C_LNK);
+ return(1);
+ case S_IFSOCK:
+ printcolor(C_SOCK);
+ return(1);
+ case S_IFIFO:
+ printcolor(C_FIFO);
+ return(1);
+ case S_IFBLK:
+ printcolor(C_BLK);
+ return(1);
+ case S_IFCHR:
+ printcolor(C_CHR);
+ return(1);
+ }
+ if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
+ if (mode & S_ISUID)
+ printcolor(C_SUID);
+ else if (mode & S_ISGID)
+ printcolor(C_SGID);
+ else
+ printcolor(C_EXEC);
+ return(1);
+ }
+ return(0);
+}
+
+void
+parsecolors(cs)
+char *cs;
+{
+ int i, j, len;
+ char c[2];
+
+ if (cs == NULL) cs = ""; /* LSCOLORS not set */
+ len = strlen(cs);
+ for (i = 0 ; i < C_NUMCOLORS ; i++) {
+ if (len <= 2*i) {
+ c[0] = defcolors[2*i];
+ c[1] = defcolors[2*i+1];
+ }
+ else {
+ c[0] = cs[2*i];
+ c[1] = cs[2*i+1];
+ }
+ for (j = 0 ; j < 2 ; j++) {
+ if ((c[j] < '0' || c[j] > '7') &&
+ tolower((unsigned char)c[j]) != 'x') {
+ fprintf(stderr,
+ "error: invalid character '%c' in LSCOLORS env var\n",
+ c[j]);
+ c[j] = defcolors[2*i+j];
+ }
+ if (tolower((unsigned char)c[j]) == 'x')
+ colors[i][j] = -1;
+ else
+ colors[i][j] = c[j]-'0';
+ }
+ }
+}
+
+void
+colorquit(sig)
+ int sig;
+{
+ endcolor(sig);
+
+ (void) signal(sig, SIG_DFL);
+ (void) kill(getpid(), sig);
+}
+#endif /*COLORLS*/
+
+static void
+printlink(p)
+ FTSENT *p;
+{
+ int lnklen;
+ char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
+
+ if (p->fts_level == FTS_ROOTLEVEL)
+ (void)snprintf(name, sizeof(name), "%s", p->fts_name);
+ else
+ (void)snprintf(name, sizeof(name),
+ "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
+ if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
+ (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
+ return;
+ }
+ path[lnklen] = '\0';
+ (void)printf(" -> ");
+ printname(path);
+}
--- /dev/null
+/*
+ * Copyright (c) 1989, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94";
+#else
+static const char rcsid[] =
+ "$FreeBSD: src/bin/ls/util.c,v 1.23 2000/07/22 05:28:46 green Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <fts.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ls.h"
+#include "ls_extern.h"
+
+int
+prn_printable(s)
+ const char *s;
+{
+ unsigned char c;
+ int n;
+
+ for (n = 0; (c = *s) != '\0'; ++s, ++n)
+ if (isprint(c))
+ putchar(c);
+ else
+ putchar('?');
+ return n;
+}
+
+/*
+ * The fts system makes it difficult to replace fts_name with a different-
+ * sized string, so we just calculate the real length here and do the
+ * conversion in prn_octal()
+ *
+ * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
+ * length computed by len_octal may be too big. I just can't be buggered
+ * to fix this as an efficient fix would involve a lookup table. Same goes
+ * for the rather inelegant code in prn_octal.
+ *
+ * DES 1998/04/23
+ */
+
+int
+len_octal(s, len)
+ const char *s;
+ int len;
+{
+ int r = 0;
+
+ while (len--)
+ if (isprint((unsigned char)*s++)) r++; else r += 4;
+ return r;
+}
+
+int
+prn_octal(s)
+ const char *s;
+{
+ unsigned char ch;
+ int len = 0;
+
+ while ((ch = *s++))
+ {
+ if (isprint(ch) && (ch != '\"') && (ch != '\\'))
+ putchar(ch), len++;
+ else if (f_octal_escape) {
+ putchar('\\');
+ switch (ch) {
+ case '\\':
+ putchar('\\');
+ break;
+ case '\"':
+ putchar('"');
+ break;
+ case '\a':
+ putchar('a');
+ break;
+ case '\b':
+ putchar('b');
+ break;
+ case '\f':
+ putchar('f');
+ break;
+ case '\n':
+ putchar('n');
+ break;
+ case '\r':
+ putchar('r');
+ break;
+ case '\t':
+ putchar('t');
+ break;
+ case '\v':
+ putchar('v');
+ break;
+ default:
+ putchar('0' + (ch >> 6));
+ putchar('0' + ((ch >> 3) & 7));
+ putchar('0' + (ch & 7));
+ len += 2;
+ break;
+ }
+ len += 2;
+ }
+ else {
+ putchar('\\');
+ putchar('0' + (ch >> 6));
+ putchar('0' + ((ch >> 3) & 7));
+ putchar('0' + (ch & 7));
+ len += 4;
+ }
+ }
+ return len;
+}
+
+void
+usage()
+{
+ (void)fprintf(stderr,
+#ifdef COLORLS
+ "usage: ls [-ABCFGHLPRTWabcdfgiklnoqrstu1]"
+#else
+ "usage: ls [-ABCFHLPRTWabcdfgiklnoqrstu1]"
+#endif
+ " [file ...]\n");
+ exit(1);
+}
+++ /dev/null
-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
- * Reserved. This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License'). You may not use this file
- * except in compliance with the License. Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- *
- * @APPLE_LICENSE_HEADER_END@
- */
-/*-
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef lint
-/*static char sccsid[] = "from: @(#)vers.c 5.1 (Berkeley) 6/24/90";*/
-static char rcsid[] = "$Id: vers.c,v 1.1.1.1 1999/05/02 03:57:40 wsanchez Exp $";
-#endif /* not lint */
-
-char version[] = "Version 5.60";