home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / ftpd / ftpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  32.1 KB  |  1,500 lines

  1. /*
  2.  * Copyright (c) 1985, 1988, 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)ftpd.c    5.40 (Berkeley) 7/2/91";*/
  42. static char rcsid[] = "$Id: ftpd.c,v 1.1 1994/05/23 09:03:47 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * FTP server.
  47.  */
  48. #include <sys/param.h>
  49. #include <sys/stat.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/socket.h>
  52. #include <sys/wait.h>
  53.  
  54. #include <netinet/in.h>
  55. #include <netinet/ip.h>
  56.  
  57. #define    FTP_NAMES
  58. #include <arpa/ftp.h>
  59. #include <arpa/inet.h>
  60. #include <arpa/telnet.h>
  61.  
  62. #include <stdio.h>
  63. #include <grp.h>
  64. #include <signal.h>
  65. #include <dirent.h>
  66. #include <fcntl.h>
  67. #include <time.h>
  68. #ifdef    SHADOW_PWD
  69. #include <shadow.h>
  70. #endif
  71. #include <pwd.h>
  72. #include <setjmp.h>
  73. #include <netdb.h>
  74. #include <errno.h>
  75. #include <syslog.h>
  76. #include <varargs.h>
  77. #include <unistd.h>
  78. #include <ctype.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include "pathnames.h"
  82.  
  83. /*
  84.  * File containing login names
  85.  * NOT to be used on this machine.
  86.  * Commonly used to disallow uucp.
  87.  */
  88. extern    int errno;
  89. extern    char version[];
  90. extern    char *home;        /* pointer to home directory for glob */
  91. extern    FILE *ftpd_popen(), *fopen(), *freopen();
  92. extern    int  ftpd_pclose(), fclose();
  93. extern    char *getline();
  94. extern    char cbuf[];
  95. extern    off_t restart_point;
  96.  
  97. struct    sockaddr_in ctrl_addr;
  98. struct    sockaddr_in data_source;
  99. struct    sockaddr_in data_dest;
  100. struct    sockaddr_in his_addr;
  101. struct    sockaddr_in pasv_addr;
  102.  
  103. int    data;
  104. jmp_buf    errcatch, urgcatch;
  105. int    logged_in;
  106. struct    passwd *pw;
  107. int    debug;
  108. int    timeout = 900;    /* timeout after 15 minutes of inactivity */
  109. int    maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
  110. int    logging;
  111. int    guest;
  112. int    type;
  113. int    form;
  114. int    stru;            /* avoid C keyword */
  115. int    mode;
  116. int    usedefault = 1;        /* for data transfers */
  117. int    pdata = -1;        /* for passive mode */
  118. int    transflag;
  119. off_t    file_size;
  120. off_t    byte_count;
  121. #if !defined(CMASK) || CMASK == 0
  122. #undef CMASK
  123. #define CMASK 027
  124. #endif
  125. int    defumask = CMASK;        /* default umask value */
  126. char    tmpline[7];
  127. char    hostname[MAXHOSTNAMELEN];
  128. char    remotehost[MAXHOSTNAMELEN];
  129.  
  130. /*
  131.  * Timeout intervals for retrying connections
  132.  * to hosts that don't accept PORT cmds.  This
  133.  * is a kludge, but given the problems with TCP...
  134.  */
  135. #define    SWAITMAX    90    /* wait at most 90 seconds */
  136. #define    SWAITINT    5    /* interval between retries */
  137.  
  138. int    swaitmax = SWAITMAX;
  139. int    swaitint = SWAITINT;
  140.  
  141. void    lostconn(), myoob();
  142. FILE    *getdatasock(), *dataconn();
  143.  
  144. #ifdef SETPROCTITLE
  145. char    **Argv = NULL;        /* pointer to argument vector */
  146. char    *LastArgv = NULL;    /* end of argv */
  147. char    proctitle[BUFSIZ];    /* initial part of title */
  148. #endif /* SETPROCTITLE */
  149.  
  150. main(argc, argv, envp)
  151.     int argc;
  152.     char *argv[];
  153.     char **envp;
  154. {
  155.     int addrlen, on = 1, tos;
  156.     char *cp;
  157.  
  158.     /*
  159.      * LOG_NDELAY sets up the logging connection immediately,
  160.      * necessary for anonymous ftp's that chroot and can't do it later.
  161.      */
  162.     openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
  163.     addrlen = sizeof (his_addr);
  164.     if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) {
  165.         syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
  166.         exit(1);
  167.     }
  168.     addrlen = sizeof (ctrl_addr);
  169.     if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
  170.         syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
  171.         exit(1);
  172.     }
  173. #ifdef IP_TOS
  174.     tos = IPTOS_LOWDELAY;
  175.     if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  176.         syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
  177. #endif
  178.     data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
  179.     debug = 0;
  180. #ifdef SETPROCTITLE
  181.     /*
  182.      *  Save start and extent of argv for setproctitle.
  183.      */
  184.     Argv = argv;
  185.     while (*envp)
  186.         envp++;
  187.     LastArgv = envp[-1] + strlen(envp[-1]);
  188. #endif /* SETPROCTITLE */
  189.  
  190.     argc--, argv++;
  191.     while (argc > 0 && *argv[0] == '-') {
  192.         for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
  193.  
  194.         case 'v':
  195.             debug = 1;
  196.             break;
  197.  
  198.         case 'd':
  199.             debug = 1;
  200.             break;
  201.  
  202.         case 'l':
  203.             logging = 1;
  204.             break;
  205.  
  206.         case 't':
  207.             timeout = atoi(++cp);
  208.             if (maxtimeout < timeout)
  209.                 maxtimeout = timeout;
  210.             goto nextopt;
  211.  
  212.         case 'T':
  213.             maxtimeout = atoi(++cp);
  214.             if (timeout > maxtimeout)
  215.                 timeout = maxtimeout;
  216.             goto nextopt;
  217.  
  218.         case 'u':
  219.             {
  220.             int val = 0;
  221.  
  222.             while (*++cp && *cp >= '0' && *cp <= '9')
  223.                 val = val*8 + *cp - '0';
  224.             if (*cp)
  225.                 fprintf(stderr, "ftpd: Bad value for -u\n");
  226.             else
  227.                 defumask = val;
  228.             goto nextopt;
  229.             }
  230.  
  231.         default:
  232.             fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n",
  233.                  *cp);
  234.             break;
  235.         }
  236. nextopt:
  237.         argc--, argv++;
  238.     }
  239.     (void) freopen(_PATH_DEVNULL, "w", stderr);
  240.     (void) signal(SIGPIPE, lostconn);
  241.     (void) signal(SIGCHLD, SIG_IGN);
  242.     if ((int)signal(SIGURG, myoob) < 0)
  243.         syslog(LOG_ERR, "signal: %m");
  244.  
  245.     /* Try to handle urgent data inline */
  246. #ifdef SO_OOBINLINE
  247.     if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0)
  248.         syslog(LOG_ERR, "setsockopt: %m");
  249. #endif
  250.  
  251. #ifdef    F_SETOWN
  252.     if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
  253.         syslog(LOG_ERR, "fcntl F_SETOWN: %m");
  254. #endif
  255.     dolog(&his_addr);
  256.     /*
  257.      * Set up default state
  258.      */
  259.     data = -1;
  260.     type = TYPE_A;
  261.     form = FORM_N;
  262.     stru = STRU_F;
  263.     mode = MODE_S;
  264.     tmpline[0] = '\0';
  265.     (void) gethostname(hostname, sizeof (hostname));
  266.     reply(220, "%s FTP server (%s) ready.", hostname, version);
  267.     (void) setjmp(errcatch);
  268.     for (;;)
  269.         (void) yyparse();
  270.     /* NOTREACHED */
  271. }
  272.  
  273. void
  274. lostconn()
  275. {
  276.     if (debug)
  277.         syslog(LOG_DEBUG, "lost connection");
  278.     dologout(-1);
  279. }
  280.  
  281. static char ttyline[20];
  282.  
  283. /*
  284.  * Helper function for sgetpwnam().
  285.  */
  286. char *
  287. sgetsave(s)
  288.     char *s;
  289. {
  290.     char *new = malloc((unsigned) strlen(s) + 1);
  291.  
  292.     if (new == NULL) {
  293.         perror_reply(421, "Local resource failure: malloc");
  294.         dologout(1);
  295.         /* NOTREACHED */
  296.     }
  297.     (void) strcpy(new, s);
  298.     return (new);
  299. }
  300.  
  301. /*
  302.  * Save the result of a getpwnam.  Used for USER command, since
  303.  * the data returned must not be clobbered by any other command
  304.  * (e.g., globbing).
  305.  */
  306. struct passwd *
  307. sgetpwnam(name)
  308.     char *name;
  309. {
  310.     static struct passwd save;
  311.     register struct passwd *p;
  312.     char *sgetsave();
  313. #ifdef    SHADOW_PWD
  314.     struct spwd *sp;
  315.  
  316.     if ((sp = getspnam(name)) == NULL)
  317.         return NULL;
  318. #endif
  319.  
  320.     if ((p = getpwnam(name)) == NULL)
  321.         return (p);
  322.     if (save.pw_name) {
  323.         free(save.pw_name);
  324.         free(save.pw_passwd);
  325.         free(save.pw_gecos);
  326.         free(save.pw_dir);
  327.         free(save.pw_shell);
  328.     }
  329.     save = *p;
  330.     save.pw_name = sgetsave(p->pw_name);
  331. #ifdef    SHADOW_PWD
  332.     save.pw_passwd = sgetsave(sp->sp_pwdp);
  333. #else
  334.     save.pw_passwd = sgetsave(p->pw_passwd);
  335. #endif
  336.     save.pw_gecos = sgetsave(p->pw_gecos);
  337.     save.pw_dir = sgetsave(p->pw_dir);
  338.     save.pw_shell = sgetsave(p->pw_shell);
  339.     return (&save);
  340. }
  341.  
  342. int login_attempts;        /* number of failed login attempts */
  343. int askpasswd;            /* had user command, ask for passwd */
  344.  
  345. /*
  346.  * USER command.
  347.  * Sets global passwd pointer pw if named account exists and is acceptable;
  348.  * sets askpasswd if a PASS command is expected.  If logged in previously,
  349.  * need to reset state.  If name is "ftp" or "anonymous", the name is not in
  350.  * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
  351.  * If account doesn't exist, ask for passwd anyway.  Otherwise, check user
  352.  * requesting login privileges.  Disallow anyone who does not have a standard
  353.  * shell as returned by getusershell().  Disallow anyone mentioned in the file
  354.  * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
  355.  */
  356. user(name)
  357.     char *name;
  358. {
  359.     register char *cp;
  360.     char *shell;
  361.     char *getusershell();
  362.  
  363.     if (logged_in) {
  364.         if (guest) {
  365.             reply(530, "Can't change user from guest login.");
  366.             return;
  367.         }
  368.         end_login();
  369.     }
  370.  
  371.     guest = 0;
  372.     if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
  373.         if (checkuser("ftp") || checkuser("anonymous"))
  374.             reply(530, "User %s access denied.", name);
  375.         else if ((pw = sgetpwnam("ftp")) != NULL) {
  376.             guest = 1;
  377.             askpasswd = 1;
  378.             reply(331, "Guest login ok, send ident as password.");
  379.         } else
  380.             reply(530, "User %s unknown.", name);
  381.         return;
  382.     }
  383.     if (pw = sgetpwnam(name)) {
  384.         if ((shell = pw->pw_shell) == NULL || *shell == 0)
  385.             shell = _PATH_BSHELL;
  386.         while ((cp = getusershell()) != NULL)
  387.             if (strcmp(cp, shell) == 0)
  388.                 break;
  389.         endusershell();
  390.         if (cp == NULL || checkuser(name)) {
  391.             reply(530, "User %s access denied.", name);
  392.             if (logging)
  393.                 syslog(LOG_NOTICE,
  394.                     "FTP LOGIN REFUSED FROM %s, %s",
  395.                     remotehost, name);
  396.             pw = (struct passwd *) NULL;
  397.             return;
  398.         }
  399.     }
  400.     reply(331, "Password required for %s.", name);
  401.     askpasswd = 1;
  402.     /*
  403.      * Delay before reading passwd after first failed
  404.      * attempt to slow down passwd-guessing programs.
  405.      */
  406.     if (login_attempts)
  407.         sleep((unsigned) login_attempts);
  408. }
  409.  
  410. /*
  411.  * Check if a user is in the file _PATH_FTPUSERS
  412.  */
  413. checkuser(name)
  414.     char *name;
  415. {
  416.     register FILE *fd;
  417.     register char *p;
  418.     char line[BUFSIZ];
  419.  
  420.     if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
  421.         while (fgets(line, sizeof(line), fd) != NULL)
  422.             if ((p = index(line, '\n')) != NULL) {
  423.                 *p = '\0';
  424.                 if (line[0] == '#')
  425.                     continue;
  426.                 if (strcmp(line, name) == 0)
  427.                     return (1);
  428.             }
  429.         (void) fclose(fd);
  430.     }
  431.     return (0);
  432. }
  433.  
  434. /*
  435.  * Terminate login as previous user, if any, resetting state;
  436.  * used when USER command is given or login fails.
  437.  */
  438. end_login()
  439. {
  440.  
  441.     (void) seteuid((uid_t)0);
  442.     if (logged_in)
  443.         logwtmp(ttyline, "", "");
  444.     pw = NULL;
  445.     logged_in = 0;
  446.     guest = 0;
  447. }
  448.  
  449. pass(passwd)
  450.     char *passwd;
  451. {
  452.     char *xpasswd, *salt;
  453.  
  454.     if (logged_in || askpasswd == 0) {
  455.         reply(503, "Login with USER first.");
  456.         return;
  457.     }
  458.     askpasswd = 0;
  459.     if (!guest) {        /* "ftp" is only account allowed no password */
  460.         if (pw == NULL)
  461.             salt = "xx";
  462.         else
  463.             salt = pw->pw_passwd;
  464. #ifdef        SHADOW_PWD
  465.         xpasswd = (char*)pw_encrypt(passwd, salt);
  466. #else
  467.         xpasswd = crypt(passwd, salt);
  468. #endif
  469.         /* The strcmp does not catch null passwords! */
  470.         if (pw == NULL || *pw->pw_passwd == '\0' ||
  471.             strcmp(xpasswd, pw->pw_passwd)) {
  472.             reply(530, "Login incorrect.");
  473.             pw = NULL;
  474.             if (login_attempts++ >= 5) {
  475.                 syslog(LOG_NOTICE,
  476.                     "repeated login failures from %s",
  477.                     remotehost);
  478.                 exit(0);
  479.             }
  480.             return;
  481.         }
  482.     }
  483.     login_attempts = 0;        /* this time successful */
  484.     (void) setegid((gid_t)pw->pw_gid);
  485.     (void) initgroups(pw->pw_name, pw->pw_gid);
  486.  
  487.     /* open wtmp before chroot */
  488.     (void)sprintf(ttyline, "ftp%d", getpid());
  489.     logwtmp(ttyline, pw->pw_name, remotehost);
  490.     logged_in = 1;
  491.  
  492.     if (guest) {
  493.         /*
  494.          * We MUST do a chdir() after the chroot. Otherwise
  495.          * the old current directory will be accessible as "."
  496.          * outside the new root!
  497.          */
  498.         if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
  499.             reply(550, "Can't set guest privileges.");
  500.             goto bad;
  501.         }
  502.     } else if (chdir(pw->pw_dir) < 0) {
  503.         if (chdir("/") < 0) {
  504.             reply(530, "User %s: can't change directory to %s.",
  505.                 pw->pw_name, pw->pw_dir);
  506.             goto bad;
  507.         } else
  508.             lreply(230, "No directory! Logging in with home=/");
  509.     }
  510.     if (seteuid((uid_t)pw->pw_uid) < 0) {
  511.         reply(550, "Can't set uid.");
  512.         goto bad;
  513.     }
  514.     if (guest) {
  515.         reply(230, "Guest login ok, access restrictions apply.");
  516. #ifdef SETPROCTITLE
  517.         sprintf(proctitle, "%s: anonymous/%.*s", remotehost,
  518.             sizeof(proctitle) - sizeof(remotehost) -
  519.             sizeof(": anonymous/"), passwd);
  520.         setproctitle(proctitle);
  521. #endif /* SETPROCTITLE */
  522.         if (logging)
  523.             syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
  524.                 remotehost, passwd);
  525.     } else {
  526.         reply(230, "User %s logged in.", pw->pw_name);
  527. #ifdef SETPROCTITLE
  528.         sprintf(proctitle, "%s: %s", remotehost, pw->pw_name);
  529.         setproctitle(proctitle);
  530. #endif /* SETPROCTITLE */
  531.         if (logging)
  532.             syslog(LOG_INFO, "FTP LOGIN FROM %s, %s",
  533.                 remotehost, pw->pw_name);
  534.     }
  535.     home = pw->pw_dir;        /* home dir for globbing */
  536.     (void) umask(defumask);
  537.     return;
  538. bad:
  539.     /* Forget all about it... */
  540.     end_login();
  541. }
  542.  
  543. retrieve(cmd, name)
  544.     char *cmd, *name;
  545. {
  546.     FILE *fin, *dout;
  547.     struct stat st;
  548.     int (*closefunc)();
  549.  
  550.     if (cmd == 0) {
  551.         fin = fopen(name, "r"), closefunc = fclose;
  552.         st.st_size = 0;
  553.     } else {
  554.         char line[BUFSIZ];
  555.  
  556.         (void) sprintf(line, cmd, name), name = line;
  557.         fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
  558.         st.st_size = -1;
  559.         st.st_blksize = BUFSIZ;
  560.     }
  561.     if (fin == NULL) {
  562.         if (errno != 0)
  563.             perror_reply(550, name);
  564.         return;
  565.     }
  566.     if (cmd == 0 &&
  567.         (fstat(fileno(fin), &st) < 0 || (st.st_mode&S_IFMT) != S_IFREG)) {
  568.         reply(550, "%s: not a plain file.", name);
  569.         goto done;
  570.     }
  571.     if (restart_point) {
  572.         if (type == TYPE_A) {
  573.             register int i, n, c;
  574.  
  575.             n = restart_point;
  576.             i = 0;
  577.             while (i++ < n) {
  578.                 if ((c=getc(fin)) == EOF) {
  579.                     perror_reply(550, name);
  580.                     goto done;
  581.                 }
  582.                 if (c == '\n')
  583.                     i++;
  584.             }    
  585.         } else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
  586.             perror_reply(550, name);
  587.             goto done;
  588.         }
  589.     }
  590.     dout = dataconn(name, st.st_size, "w");
  591.     if (dout == NULL)
  592.         goto done;
  593.     send_data(fin, dout, st.st_blksize);
  594.     (void) fclose(dout);
  595.     data = -1;
  596.     pdata = -1;
  597. done:
  598.     (*closefunc)(fin);
  599. }
  600.  
  601. _store(name, mode, unique)
  602.     char *name, *mode;
  603.     int unique;
  604. {
  605.     FILE *fout, *din;
  606.     struct stat st;
  607.     int (*closefunc)();
  608.     char *gunique();
  609.  
  610.     if (unique && stat(name, &st) == 0 &&
  611.         (name = gunique(name)) == NULL)
  612.         return;
  613.  
  614.     if (restart_point)
  615.         mode = "r+w";
  616.     fout = fopen(name, mode);
  617.     closefunc = fclose;
  618.     if (fout == NULL) {
  619.         perror_reply(553, name);
  620.         return;
  621.     }
  622.     if (restart_point) {
  623.         if (type == TYPE_A) {
  624.             register int i, n, c;
  625.  
  626.             n = restart_point;
  627.             i = 0;
  628.             while (i++ < n) {
  629.                 if ((c=getc(fout)) == EOF) {
  630.                     perror_reply(550, name);
  631.                     goto done;
  632.                 }
  633.                 if (c == '\n')
  634.                     i++;
  635.             }    
  636.             /*
  637.              * We must do this seek to "current" position
  638.              * because we are changing from reading to
  639.              * writing.
  640.              */
  641.             if (fseek(fout, 0L, L_INCR) < 0) {
  642.                 perror_reply(550, name);
  643.                 goto done;
  644.             }
  645.         } else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
  646.             perror_reply(550, name);
  647.             goto done;
  648.         }
  649.     }
  650.     din = dataconn(name, (off_t)-1, "r");
  651.     if (din == NULL)
  652.         goto done;
  653.     if (receive_data(din, fout) == 0) {
  654.         if (unique)
  655.             reply(226, "Transfer complete (unique file name:%s).",
  656.                 name);
  657.         else
  658.             reply(226, "Transfer complete.");
  659.     }
  660.     (void) fclose(din);
  661.     data = -1;
  662.     pdata = -1;
  663. done:
  664.     (*closefunc)(fout);
  665. }
  666.  
  667. FILE *
  668. getdatasock(mode)
  669.     char *mode;
  670. {
  671.     int s, on = 1, tries;
  672.  
  673.     if (data >= 0)
  674.         return (fdopen(data, mode));
  675.     (void) seteuid((uid_t)0);
  676.     s = socket(AF_INET, SOCK_STREAM, 0);
  677.     if (s < 0)
  678.         goto bad;
  679.     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  680.         (char *) &on, sizeof (on)) < 0)
  681.         goto bad;
  682.     /* anchor socket to avoid multi-homing problems */
  683.     data_source.sin_family = AF_INET;
  684.     data_source.sin_addr = ctrl_addr.sin_addr;
  685.     for (tries = 1; ; tries++) {
  686.         if (bind(s, (struct sockaddr *)&data_source,
  687.             sizeof (data_source)) >= 0)
  688.             break;
  689.         if (errno != EADDRINUSE || tries > 10)
  690.             goto bad;
  691.         sleep(tries);
  692.     }
  693.     (void) seteuid((uid_t)pw->pw_uid);
  694. #ifdef IP_TOS
  695.     on = IPTOS_THROUGHPUT;
  696.     if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  697.         syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
  698. #endif
  699.     return (fdopen(s, mode));
  700. bad:
  701.     (void) seteuid((uid_t)pw->pw_uid);
  702.     (void) close(s);
  703.     return (NULL);
  704. }
  705.  
  706. FILE *
  707. dataconn(name, size, mode)
  708.     char *name;
  709.     off_t size;
  710.     char *mode;
  711. {
  712.     char sizebuf[32];
  713.     FILE *file;
  714.     int retry = 0, tos;
  715.  
  716.     file_size = size;
  717.     byte_count = 0;
  718.     if (size != (off_t) -1)
  719.         (void) sprintf (sizebuf, " (%ld bytes)", size);
  720.     else
  721.         (void) strcpy(sizebuf, "");
  722.     if (pdata >= 0) {
  723.         struct sockaddr_in from;
  724.         int s, fromlen = sizeof(from);
  725.  
  726.         s = accept(pdata, (struct sockaddr *)&from, &fromlen);
  727.         if (s < 0) {
  728.             reply(425, "Can't open data connection.");
  729.             (void) close(pdata);
  730.             pdata = -1;
  731.             return(NULL);
  732.         }
  733.         (void) close(pdata);
  734.         pdata = s;
  735. #ifdef IP_TOS
  736.         tos = IPTOS_LOWDELAY;
  737.         (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
  738.             sizeof(int));
  739. #endif
  740.         reply(150, "Opening %s mode data connection for %s%s.",
  741.              type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
  742.         return(fdopen(pdata, mode));
  743.     }
  744.     if (data >= 0) {
  745.         reply(125, "Using existing data connection for %s%s.",
  746.             name, sizebuf);
  747.         usedefault = 1;
  748.         return (fdopen(data, mode));
  749.     }
  750.     if (usedefault)
  751.         data_dest = his_addr;
  752.     usedefault = 1;
  753.     file = getdatasock(mode);
  754.     if (file == NULL) {
  755.         reply(425, "Can't create data socket (%s,%d): %s.",
  756.             inet_ntoa(data_source.sin_addr),
  757.             ntohs(data_source.sin_port), strerror(errno));
  758.         return (NULL);
  759.     }
  760.     data = fileno(file);
  761.     while (connect(data, (struct sockaddr *)&data_dest,
  762.         sizeof (data_dest)) < 0) {
  763.         if (errno == EADDRINUSE && retry < swaitmax) {
  764.             sleep((unsigned) swaitint);
  765.             retry += swaitint;
  766.             continue;
  767.         }
  768.         perror_reply(425, "Can't build data connection");
  769.         (void) fclose(file);
  770.         data = -1;
  771.         return (NULL);
  772.     }
  773.     reply(150, "Opening %s mode data connection for %s%s.",
  774.          type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
  775.     return (file);
  776. }
  777.  
  778. /*
  779.  * Tranfer the contents of "instr" to
  780.  * "outstr" peer using the appropriate
  781.  * encapsulation of the data subject
  782.  * to Mode, Structure, and Type.
  783.  *
  784.  * NB: Form isn't handled.
  785.  */
  786. send_data(instr, outstr, blksize)
  787.     FILE *instr, *outstr;
  788.     off_t blksize;
  789. {
  790.     register int c, cnt;
  791.     register char *buf;
  792.     int netfd, filefd;
  793.  
  794.     transflag++;
  795.     if (setjmp(urgcatch)) {
  796.         transflag = 0;
  797.         return;
  798.     }
  799.     switch (type) {
  800.  
  801.     case TYPE_A:
  802.         while ((c = getc(instr)) != EOF) {
  803.             byte_count++;
  804.             if (c == '\n') {
  805.                 if (ferror(outstr))
  806.                     goto data_err;
  807.                 (void) putc('\r', outstr);
  808.             }
  809.             (void) putc(c, outstr);
  810.         }
  811.         fflush(outstr);
  812.         transflag = 0;
  813.         if (ferror(instr))
  814.             goto file_err;
  815.         if (ferror(outstr))
  816.             goto data_err;
  817.         reply(226, "Transfer complete.");
  818.         return;
  819.  
  820.     case TYPE_I:
  821.     case TYPE_L:
  822.         if ((buf = malloc((u_int)blksize)) == NULL) {
  823.             transflag = 0;
  824.             perror_reply(451, "Local resource failure: malloc");
  825.             return;
  826.         }
  827.         netfd = fileno(outstr);
  828.         filefd = fileno(instr);
  829.         while ((cnt = read(filefd, buf, (u_int)blksize)) > 0) {
  830.             int offset = 0, sent;
  831.  
  832.             while (cnt > 0) {
  833.                 sent = write(netfd, buf+offset, cnt);
  834.                 if (sent <= 0) break;
  835.                 offset += sent;
  836.                 cnt -= sent;
  837.             }
  838.             byte_count += cnt;
  839.             if (cnt) break;
  840.         }
  841.         transflag = 0;
  842.         (void)free(buf);
  843.         if (cnt != 0) {
  844.             if (cnt < 0)
  845.                 goto file_err;
  846.             goto data_err;
  847.         }
  848.         reply(226, "Transfer complete.");
  849.         return;
  850.     default:
  851.         transflag = 0;
  852.         reply(550, "Unimplemented TYPE %d in send_data", type);
  853.         return;
  854.     }
  855.  
  856. data_err:
  857.     transflag = 0;
  858.     perror_reply(426, "Data connection");
  859.     return;
  860.  
  861. file_err:
  862.     transflag = 0;
  863.     perror_reply(551, "Error on input file");
  864. }
  865.  
  866. /*
  867.  * Transfer data from peer to
  868.  * "outstr" using the appropriate
  869.  * encapulation of the data subject
  870.  * to Mode, Structure, and Type.
  871.  *
  872.  * N.B.: Form isn't handled.
  873.  */
  874. receive_data(instr, outstr)
  875.     FILE *instr, *outstr;
  876. {
  877.     register int c;
  878.     int cnt, bare_lfs = 0;
  879.     char buf[BUFSIZ];
  880.  
  881.     transflag++;
  882.     if (setjmp(urgcatch)) {
  883.         transflag = 0;
  884.         return (-1);
  885.     }
  886.     switch (type) {
  887.  
  888.     case TYPE_I:
  889.     case TYPE_L:
  890.         while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) {
  891.             if (write(fileno(outstr), buf, cnt) != cnt)
  892.                 goto file_err;
  893.             byte_count += cnt;
  894.         }
  895.         if (cnt < 0)
  896.             goto data_err;
  897.         transflag = 0;
  898.         return (0);
  899.  
  900.     case TYPE_E:
  901.         reply(553, "TYPE E not implemented.");
  902.         transflag = 0;
  903.         return (-1);
  904.  
  905.     case TYPE_A:
  906.         while ((c = getc(instr)) != EOF) {
  907.             byte_count++;
  908.             if (c == '\n')
  909.                 bare_lfs++;
  910.             while (c == '\r') {
  911.                 if (ferror(outstr))
  912.                     goto data_err;
  913.                 if ((c = getc(instr)) != '\n') {
  914.                     (void) putc ('\r', outstr);
  915.                     if (c == '\0' || c == EOF)
  916.                         goto contin2;
  917.                 }
  918.             }
  919.             (void) putc(c, outstr);
  920.     contin2:    ;
  921.         }
  922.         fflush(outstr);
  923.         if (ferror(instr))
  924.             goto data_err;
  925.         if (ferror(outstr))
  926.             goto file_err;
  927.         transflag = 0;
  928.         if (bare_lfs) {
  929.             lreply(230, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs);
  930.             printf("   File may not have transferred correctly.\r\n");
  931.         }
  932.         return (0);
  933.     default:
  934.         reply(550, "Unimplemented TYPE %d in receive_data", type);
  935.         transflag = 0;
  936.         return (-1);
  937.     }
  938.  
  939. data_err:
  940.     transflag = 0;
  941.     perror_reply(426, "Data Connection");
  942.     return (-1);
  943.  
  944. file_err:
  945.     transflag = 0;
  946.     perror_reply(452, "Error writing file");
  947.     return (-1);
  948. }
  949.  
  950. statfilecmd(filename)
  951.     char *filename;
  952. {
  953.     char line[BUFSIZ];
  954.     FILE *fin;
  955.     int c;
  956.  
  957.     (void) sprintf(line, "/bin/ls -lgA %s", filename);
  958.     fin = ftpd_popen(line, "r");
  959.     lreply(211, "status of %s:", filename);
  960.     while ((c = getc(fin)) != EOF) {
  961.         if (c == '\n') {
  962.             if (ferror(stdout)){
  963.                 perror_reply(421, "control connection");
  964.                 (void) ftpd_pclose(fin);
  965.                 dologout(1);
  966.                 /* NOTREACHED */
  967.             }
  968.             if (ferror(fin)) {
  969.                 perror_reply(551, filename);
  970.                 (void) ftpd_pclose(fin);
  971.                 return;
  972.             }
  973.             (void) putc('\r', stdout);
  974.         }
  975.         (void) putc(c, stdout);
  976.     }
  977.     (void) ftpd_pclose(fin);
  978.     reply(211, "End of Status");
  979. }
  980.  
  981. statcmd()
  982. {
  983.     struct sockaddr_in *sin;
  984.     u_char *a, *p;
  985.  
  986.     lreply(211, "%s FTP server status:", hostname, version);
  987.     printf("     %s\r\n", version);
  988.     printf("     Connected to %s", remotehost);
  989.     if (!isdigit(remotehost[0]))
  990.         printf(" (%s)", inet_ntoa(his_addr.sin_addr));
  991.     printf("\r\n");
  992.     if (logged_in) {
  993.         if (guest)
  994.             printf("     Logged in anonymously\r\n");
  995.         else
  996.             printf("     Logged in as %s\r\n", pw->pw_name);
  997.     } else if (askpasswd)
  998.         printf("     Waiting for password\r\n");
  999.     else
  1000.         printf("     Waiting for user name\r\n");
  1001.     printf("     TYPE: %s", typenames[type]);
  1002.     if (type == TYPE_A || type == TYPE_E)
  1003.         printf(", FORM: %s", formnames[form]);
  1004.     if (type == TYPE_L)
  1005. #if NBBY == 8
  1006.         printf(" %d", NBBY);
  1007. #else
  1008.         printf(" %d", bytesize);    /* need definition! */
  1009. #endif
  1010.     printf("; STRUcture: %s; transfer MODE: %s\r\n",
  1011.         strunames[stru], modenames[mode]);
  1012.     if (data != -1)
  1013.         printf("     Data connection open\r\n");
  1014.     else if (pdata != -1) {
  1015.         printf("     in Passive mode");
  1016.         sin = &pasv_addr;
  1017.         goto printaddr;
  1018.     } else if (usedefault == 0) {
  1019.         printf("     PORT");
  1020.         sin = &data_dest;
  1021. printaddr:
  1022.         a = (u_char *) &sin->sin_addr;
  1023.         p = (u_char *) &sin->sin_port;
  1024. #define UC(b) (((int) b) & 0xff)
  1025.         printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]),
  1026.             UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
  1027. #undef UC
  1028.     } else
  1029.         printf("     No data connection\r\n");
  1030.     reply(211, "End of status");
  1031. }
  1032.  
  1033. fatal(s)
  1034.     char *s;
  1035. {
  1036.     reply(451, "Error in server: %s\n", s);
  1037.     reply(221, "Closing connection due to server error.");
  1038.     dologout(0);
  1039.     /* NOTREACHED */
  1040. }
  1041.  
  1042. /* VARARGS2 */
  1043. reply(n, fmt, p0, p1, p2, p3, p4, p5)
  1044.     int n;
  1045.     char *fmt;
  1046. {
  1047.     printf("%d ", n);
  1048.     printf(fmt, p0, p1, p2, p3, p4, p5);
  1049.     printf("\r\n");
  1050.     (void)fflush(stdout);
  1051.     if (debug) {
  1052.         syslog(LOG_DEBUG, "<--- %d ", n);
  1053.         syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
  1054. }
  1055. }
  1056.  
  1057. /* VARARGS2 */
  1058. lreply(n, fmt, p0, p1, p2, p3, p4, p5)
  1059.     int n;
  1060.     char *fmt;
  1061. {
  1062.     printf("%d- ", n);
  1063.     printf(fmt, p0, p1, p2, p3, p4, p5);
  1064.     printf("\r\n");
  1065.     (void)fflush(stdout);
  1066.     if (debug) {
  1067.         syslog(LOG_DEBUG, "<--- %d- ", n);
  1068.         syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
  1069.     }
  1070. }
  1071.  
  1072. ack(s)
  1073.     char *s;
  1074. {
  1075.     reply(250, "%s command successful.", s);
  1076. }
  1077.  
  1078. nack(s)
  1079.     char *s;
  1080. {
  1081.     reply(502, "%s command not implemented.", s);
  1082. }
  1083.  
  1084. /* ARGSUSED */
  1085. yyerror(s)
  1086.     char *s;
  1087. {
  1088.     char *cp;
  1089.  
  1090.     if (cp = index(cbuf,'\n'))
  1091.         *cp = '\0';
  1092.     reply(500, "'%s': command not understood.", cbuf);
  1093. }
  1094.  
  1095. _delete(name)
  1096.     char *name;
  1097. {
  1098.     struct stat st;
  1099.  
  1100.     if (stat(name, &st) < 0) {
  1101.         perror_reply(550, name);
  1102.         return;
  1103.     }
  1104.     if ((st.st_mode&S_IFMT) == S_IFDIR) {
  1105.         if (rmdir(name) < 0) {
  1106.             perror_reply(550, name);
  1107.             return;
  1108.         }
  1109.         goto done;
  1110.     }
  1111.     if (unlink(name) < 0) {
  1112.         perror_reply(550, name);
  1113.         return;
  1114.     }
  1115. done:
  1116.     ack("DELE");
  1117. }
  1118.  
  1119. cwd(path)
  1120.     char *path;
  1121. {
  1122.     if (chdir(path) < 0)
  1123.         perror_reply(550, path);
  1124.     else
  1125.         ack("CWD");
  1126. }
  1127.  
  1128. makedir(name)
  1129.     char *name;
  1130. {
  1131.     if (mkdir(name, 0777) < 0)
  1132.         perror_reply(550, name);
  1133.     else
  1134.         reply(257, "MKD command successful.");
  1135. }
  1136.  
  1137. removedir(name)
  1138.     char *name;
  1139. {
  1140.     if (rmdir(name) < 0)
  1141.         perror_reply(550, name);
  1142.     else
  1143.         ack("RMD");
  1144. }
  1145.  
  1146. pwd()
  1147. {
  1148.     char path[MAXPATHLEN + 1];
  1149.     extern char *getwd();
  1150.  
  1151.     if (getwd(path) == (char *)NULL)
  1152.         reply(550, "%s.", path);
  1153.     else
  1154.         reply(257, "\"%s\" is current directory.", path);
  1155. }
  1156.  
  1157. char *
  1158. renamefrom(name)
  1159.     char *name;
  1160. {
  1161.     struct stat st;
  1162.  
  1163.     if (stat(name, &st) < 0) {
  1164.         perror_reply(550, name);
  1165.         return ((char *)0);
  1166.     }
  1167.     reply(350, "File exists, ready for destination name");
  1168.     return (name);
  1169. }
  1170.  
  1171. renamecmd(from, to)
  1172.     char *from, *to;
  1173. {
  1174.     if (rename(from, to) < 0)
  1175.         perror_reply(550, "rename");
  1176.     else
  1177.         ack("RNTO");
  1178. }
  1179.  
  1180. dolog(sin)
  1181.     struct sockaddr_in *sin;
  1182. {
  1183.     struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr,
  1184.         sizeof (struct in_addr), AF_INET);
  1185.     time_t t, time();
  1186.     extern char *ctime();
  1187.  
  1188.     if (hp)
  1189.         (void) strncpy(remotehost, hp->h_name, sizeof (remotehost));
  1190.     else
  1191.         (void) strncpy(remotehost, inet_ntoa(sin->sin_addr),
  1192.             sizeof (remotehost));
  1193. #ifdef SETPROCTITLE
  1194.     sprintf(proctitle, "%s: connected", remotehost);
  1195.     setproctitle(proctitle);
  1196. #endif /* SETPROCTITLE */
  1197.  
  1198.     if (logging) {
  1199.         t = time((time_t *) 0);
  1200.         syslog(LOG_INFO, "connection from %s at %s",
  1201.             remotehost, ctime(&t));
  1202.     }
  1203. }
  1204.  
  1205. /*
  1206.  * Record logout in wtmp file
  1207.  * and exit with supplied status.
  1208.  */
  1209. dologout(status)
  1210.     int status;
  1211. {
  1212.     if (logged_in) {
  1213.         (void) seteuid((uid_t)0);
  1214.         logwtmp(ttyline, "", "");
  1215.     }
  1216.     /* beware of flushing buffers after a SIGPIPE */
  1217.     _exit(status);
  1218. }
  1219.  
  1220. void
  1221. myoob()
  1222. {
  1223.     char *cp;
  1224.  
  1225.     /* only process if transfer occurring */
  1226.     if (!transflag)
  1227.         return;
  1228.     cp = tmpline;
  1229.     if (getline(cp, 7, stdin) == NULL) {
  1230.         reply(221, "You could at least say goodbye.");
  1231.         dologout(0);
  1232.     }
  1233.     upper(cp);
  1234.     if (strcmp(cp, "ABOR\r\n") == 0) {
  1235.         tmpline[0] = '\0';
  1236.         reply(426, "Transfer aborted. Data connection closed.");
  1237.         reply(226, "Abort successful");
  1238.         longjmp(urgcatch, 1);
  1239.     }
  1240.     if (strcmp(cp, "STAT\r\n") == 0) {
  1241.         if (file_size != (off_t) -1)
  1242.             reply(213, "Status: %lu of %lu bytes transferred",
  1243.                 byte_count, file_size);
  1244.         else
  1245.             reply(213, "Status: %lu bytes transferred", byte_count);
  1246.     }
  1247. }
  1248.  
  1249. /*
  1250.  * Note: a response of 425 is not mentioned as a possible response to
  1251.  *     the PASV command in RFC959. However, it has been blessed as
  1252.  *     a legitimate response by Jon Postel in a telephone conversation
  1253.  *    with Rick Adams on 25 Jan 89.
  1254.  */
  1255. passive()
  1256. {
  1257.     int len;
  1258.     register char *p, *a;
  1259.  
  1260.     pdata = socket(AF_INET, SOCK_STREAM, 0);
  1261.     if (pdata < 0) {
  1262.         perror_reply(425, "Can't open passive connection");
  1263.         return;
  1264.     }
  1265.     pasv_addr = ctrl_addr;
  1266.     pasv_addr.sin_port = 0;
  1267.     (void) seteuid((uid_t)0);
  1268.     if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) {
  1269.         (void) seteuid((uid_t)pw->pw_uid);
  1270.         goto pasv_error;
  1271.     }
  1272.     (void) seteuid((uid_t)pw->pw_uid);
  1273.     len = sizeof(pasv_addr);
  1274.     if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
  1275.         goto pasv_error;
  1276.     if (listen(pdata, 1) < 0)
  1277.         goto pasv_error;
  1278.     a = (char *) &pasv_addr.sin_addr;
  1279.     p = (char *) &pasv_addr.sin_port;
  1280.  
  1281. #define UC(b) (((int) b) & 0xff)
  1282.  
  1283.     reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
  1284.         UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
  1285.     return;
  1286.  
  1287. pasv_error:
  1288.     (void) close(pdata);
  1289.     pdata = -1;
  1290.     perror_reply(425, "Can't open passive connection");
  1291.     return;
  1292. }
  1293.  
  1294. /*
  1295.  * Generate unique name for file with basename "local".
  1296.  * The file named "local" is already known to exist.
  1297.  * Generates failure reply on error.
  1298.  */
  1299. char *
  1300. gunique(local)
  1301.     char *local;
  1302. {
  1303.     static char new[MAXPATHLEN];
  1304.     struct stat st;
  1305.     char *cp = rindex(local, '/');
  1306.     int count = 0;
  1307.  
  1308.     if (cp)
  1309.         *cp = '\0';
  1310.     if (stat(cp ? local : ".", &st) < 0) {
  1311.         perror_reply(553, cp ? local : ".");
  1312.         return((char *) 0);
  1313.     }
  1314.     if (cp)
  1315.         *cp = '/';
  1316.     (void) strcpy(new, local);
  1317.     cp = new + strlen(new);
  1318.     *cp++ = '.';
  1319.     for (count = 1; count < 100; count++) {
  1320.         (void) sprintf(cp, "%d", count);
  1321.         if (stat(new, &st) < 0)
  1322.             return(new);
  1323.     }
  1324.     reply(452, "Unique file name cannot be created.");
  1325.     return((char *) 0);
  1326. }
  1327.  
  1328. /*
  1329.  * Format and send reply containing system error number.
  1330.  */
  1331. perror_reply(code, string)
  1332.     int code;
  1333.     char *string;
  1334. {
  1335.     reply(code, "%s: %s.", string, strerror(errno));
  1336. }
  1337.  
  1338. static char *onefile[] = {
  1339.     "",
  1340.     0
  1341. };
  1342.  
  1343. send_file_list(whichfiles)
  1344.     char *whichfiles;
  1345. {
  1346.     struct stat st;
  1347.     DIR *dirp = NULL;
  1348.     struct dirent *dir;
  1349.     FILE *dout = NULL;
  1350.     register char **dirlist, *dirname;
  1351.     int simple = 0;
  1352.     char *strpbrk();
  1353.  
  1354.     if (strpbrk(whichfiles, "~{[*?") != NULL) {
  1355.         extern char **ftpglob(), *globerr;
  1356.  
  1357.         globerr = NULL;
  1358.         dirlist = ftpglob(whichfiles);
  1359.         if (globerr != NULL) {
  1360.             reply(550, globerr);
  1361.             return;
  1362.         } else if (dirlist == NULL) {
  1363.             errno = ENOENT;
  1364.             perror_reply(550, whichfiles);
  1365.             return;
  1366.         }
  1367.     } else {
  1368.         onefile[0] = whichfiles;
  1369.         dirlist = onefile;
  1370.         simple = 1;
  1371.     }
  1372.  
  1373.     if (setjmp(urgcatch)) {
  1374.         transflag = 0;
  1375.         return;
  1376.     }
  1377.     while (dirname = *dirlist++) {
  1378.         if (stat(dirname, &st) < 0) {
  1379.             /*
  1380.              * If user typed "ls -l", etc, and the client
  1381.              * used NLST, do what the user meant.
  1382.              */
  1383.             if (dirname[0] == '-' && *dirlist == NULL &&
  1384.                 transflag == 0) {
  1385.                 retrieve("/bin/ls %s", dirname);
  1386.                 return;
  1387.             }
  1388.             perror_reply(550, whichfiles);
  1389.             if (dout != NULL) {
  1390.                 (void) fclose(dout);
  1391.                 transflag = 0;
  1392.                 data = -1;
  1393.                 pdata = -1;
  1394.             }
  1395.             return;
  1396.         }
  1397.  
  1398.         if ((st.st_mode&S_IFMT) == S_IFREG) {
  1399.             if (dout == NULL) {
  1400.                 dout = dataconn("file list", (off_t)-1, "w");
  1401.                 if (dout == NULL)
  1402.                     return;
  1403.                 transflag++;
  1404.             }
  1405.             fprintf(dout, "%s%s\n", dirname,
  1406.                 type == TYPE_A ? "\r" : "");
  1407.             byte_count += strlen(dirname) + 1;
  1408.             continue;
  1409.         } else if ((st.st_mode&S_IFMT) != S_IFDIR)
  1410.             continue;
  1411.  
  1412.         if ((dirp = opendir(dirname)) == NULL)
  1413.             continue;
  1414.  
  1415.         while ((dir = readdir(dirp)) != NULL) {
  1416.             char nbuf[MAXPATHLEN];
  1417.  
  1418.             if (dir->d_name[0] == '.' && dir->d_namlen == 1)
  1419.                 continue;
  1420.             if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
  1421.                 dir->d_namlen == 2)
  1422.                 continue;
  1423.  
  1424.             sprintf(nbuf, "%s/%s", dirname, dir->d_name);
  1425.  
  1426.             /*
  1427.              * We have to do a stat to insure it's
  1428.              * not a directory or special file.
  1429.              */
  1430.             if (simple || (stat(nbuf, &st) == 0 &&
  1431.                 (st.st_mode&S_IFMT) == S_IFREG)) {
  1432.                 if (dout == NULL) {
  1433.                     dout = dataconn("file list", (off_t)-1,
  1434.                         "w");
  1435.                     if (dout == NULL)
  1436.                         return;
  1437.                     transflag++;
  1438.                 }
  1439.                 if (nbuf[0] == '.' && nbuf[1] == '/')
  1440.                     fprintf(dout, "%s%s\n", &nbuf[2],
  1441.                         type == TYPE_A ? "\r" : "");
  1442.                 else
  1443.                     fprintf(dout, "%s%s\n", nbuf,
  1444.                         type == TYPE_A ? "\r" : "");
  1445.                 byte_count += strlen(nbuf) + 1;
  1446.             }
  1447.         }
  1448.         (void) closedir(dirp);
  1449.     }
  1450.  
  1451.     if (dout == NULL)
  1452.         reply(550, "No files found.");
  1453.     else if (ferror(dout) != 0)
  1454.         perror_reply(550, "Data connection");
  1455.     else
  1456.         reply(226, "Transfer complete.");
  1457.  
  1458.     transflag = 0;
  1459.     if (dout != NULL)
  1460.         (void) fclose(dout);
  1461.     data = -1;
  1462.     pdata = -1;
  1463. }
  1464.  
  1465. #ifdef SETPROCTITLE
  1466. /*
  1467.  * clobber argv so ps will show what we're doing.
  1468.  * (stolen from sendmail)
  1469.  * warning, since this is usually started from inetd.conf, it
  1470.  * often doesn't have much of an environment or arglist to overwrite.
  1471.  */
  1472.  
  1473. /*VARARGS1*/
  1474. setproctitle(fmt, a, b, c)
  1475. char *fmt;
  1476. {
  1477.     register char *p, *bp, ch;
  1478.     register int i;
  1479.     char buf[BUFSIZ];
  1480.  
  1481.     (void) sprintf(buf, fmt, a, b, c);
  1482.  
  1483.     /* make ps print our process name */
  1484.     p = Argv[0];
  1485.     *p++ = '-';
  1486.  
  1487.     i = strlen(buf);
  1488.     if (i > LastArgv - p - 2) {
  1489.         i = LastArgv - p - 2;
  1490.         buf[i] = '\0';
  1491.     }
  1492.     bp = buf;
  1493.     while (ch = *bp++)
  1494.         if (ch != '\n' && ch != '\r')
  1495.             *p++ = ch;
  1496.     while (p < LastArgv)
  1497.         *p++ = ' ';
  1498. }
  1499. #endif /* SETPROCTITLE */
  1500.