home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / wu-ftpd-.000 / wu-ftpd- / wu-ftpd-2.4 / src / ftpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-08  |  74.5 KB  |  2,819 lines

  1. /* Copyright (c) 1985, 1988, 1990 Regents of the University of California.
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met: 1. Redistributions of source code must retain the above copyright
  7.  * notice, this list of conditions and the following disclaimer. 2.
  8.  * Redistributions in binary form must reproduce the above copyright notice,
  9.  * this list of conditions and the following disclaimer in the documentation
  10.  * and/or other materials provided with the distribution. 3. All advertising
  11.  * materials mentioning features or use of this software must display the
  12.  * following acknowledgement: This product includes software developed by the
  13.  * University of California, Berkeley and its contributors. 4. Neither the
  14.  * name of the University nor the names of its contributors may be used to
  15.  * endorse or promote products derived from this software without specific
  16.  * prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  19.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21.  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  22.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE. 
  29.  */
  30.  
  31. #ifndef lint
  32. char copyright[] =
  33. "@(#) Copyright (c) 1985, 1988, 1990 Regents of the University of California.\n\
  34.  All rights reserved.\n";
  35. #endif /* not lint */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)ftpd.c  5.40 (Berkeley) 7/2/91";
  39. #endif /* not lint */
  40.  
  41. /* FTP server. */
  42. #include "config.h"
  43.  
  44. #include <sys/types.h>
  45. #include <sys/param.h>
  46. #include <sys/stat.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/socket.h>
  49. #include <sys/file.h>
  50. #include <sys/wait.h>
  51.  
  52. #ifdef AIX
  53. #include <sys/id.h>
  54. #include <sys/priv.h>
  55. #endif
  56.  
  57. #include <netinet/in.h>
  58. #include <netinet/in_systm.h>
  59. #include <netinet/ip.h>
  60.  
  61. #define FTP_NAMES
  62. #include <arpa/ftp.h>
  63. #include <arpa/inet.h>
  64. #include <arpa/telnet.h>
  65.  
  66. #include <ctype.h>
  67. #include <stdio.h>
  68. #include <signal.h>
  69. #include <pwd.h>
  70. #include <setjmp.h>
  71. #include <netdb.h>
  72. #include <errno.h>
  73. #include <string.h>
  74. #include <varargs.h>
  75. #ifdef SYSSYSLOG
  76. #include <sys/syslog.h>
  77. #else
  78. #include <syslog.h>
  79. #endif
  80. #include <time.h>
  81. #include "conversions.h"
  82. #include "extensions.h"
  83. #include "pathnames.h"
  84.  
  85. #ifdef M_UNIX
  86. #include <arpa/nameser.h>
  87. #include <resolv.h>
  88. #endif
  89.  
  90. #if defined(SVR4) || defined(ISC)
  91. #include <fcntl.h>
  92. #endif
  93.  
  94. #ifdef HAVE_SYSINFO
  95. #include <sys/systeminfo.h>
  96. #endif
  97.  
  98. #ifdef SHADOW_PASSWORD
  99. #include <shadow.h>
  100. #endif
  101.  
  102. #ifdef KERBEROS
  103. #include <sys/types.h>
  104. #include <auth.h>
  105. #include <krb.h>
  106. #endif
  107.  
  108. #ifdef ULTRIX_AUTH
  109. #include <auth.h>
  110. #include <sys/svcinfo.h>
  111. #endif
  112.  
  113. #ifdef HAVE_DIRENT
  114. #include <dirent.h>
  115. #else
  116. #include <sys/dir.h>
  117. #endif
  118.  
  119. #ifndef TRUE
  120. #define  TRUE   1
  121. #endif
  122.  
  123. #ifndef FALSE
  124. #define  FALSE  !TRUE
  125. #endif
  126.  
  127. /* File containing login names NOT to be used on this machine. Commonly used
  128.  * to disallow uucp. */
  129. extern int errno;
  130. extern int pidfd;
  131. extern char *ctime(const time_t *);
  132. #ifndef NO_CRYPT_PROTO
  133. extern char *crypt(const char *, const char *);
  134. #endif
  135. extern char version[];
  136. extern char *home;              /* pointer to home directory for glob */
  137. extern FILE *ftpd_popen(char *program, char *type, int closestderr),
  138.  *fopen(const char *, const char *),
  139.  *freopen(const char *, const char *, FILE *);
  140. extern int ftpd_pclose(FILE *iop),
  141.   fclose(FILE *);
  142. extern char *getline(),
  143.  *realpath(char *pathname, char *result);
  144. extern char cbuf[];
  145. extern off_t restart_point;
  146.  
  147. struct sockaddr_in ctrl_addr;
  148. struct sockaddr_in data_source;
  149. struct sockaddr_in data_dest;
  150. struct sockaddr_in his_addr;
  151. struct sockaddr_in pasv_addr;
  152.  
  153. int data;
  154. jmp_buf errcatch,
  155.   urgcatch;
  156. int logged_in;
  157. struct passwd *pw;
  158. int debug;
  159. int timeout = 900;              /* timeout after 15 minutes of inactivity */
  160. int maxtimeout = 7200;          /* don't allow idle time to be set beyond 2
  161.                                  * hours */
  162. int logging;
  163. int log_commands = 1;
  164. int anonymous;
  165. int guest;
  166. int type;
  167. int form;
  168. int stru;                       /* avoid C keyword */
  169. int mode;
  170. int usedefault = 1;             /* for data transfers */
  171. int pdata = -1;                 /* for passive mode */
  172. int transflag;
  173. off_t file_size;
  174. off_t byte_count;
  175.  
  176. #if !defined(CMASK) || CMASK == 0
  177. #undef CMASK
  178. #define CMASK 002
  179. #endif
  180. mode_t defumask = CMASK;           /* default umask value */
  181. char tmpline[7];
  182. char hostname[MAXHOSTNAMELEN];
  183. char remotehost[MAXHOSTNAMELEN];
  184. char remoteaddr[MAXHOSTNAMELEN];
  185.  
  186. /* log failures     27-apr-93 ehk/bm */
  187. #ifdef LOG_FAILED
  188. #define MAXUSERNAMELEN    32
  189. char the_user[MAXUSERNAMELEN];
  190. #endif
  191.  
  192. /* Access control and logging passwords */
  193. int use_accessfile = 1;
  194. char guestpw[MAXHOSTNAMELEN];
  195. char privatepw[MAXHOSTNAMELEN];
  196. int nameserved = 0;
  197. extern char authuser[];
  198. extern int authenticated;
  199.  
  200. /* File transfer logging */
  201. int xferlog = 0;
  202. int log_outbound_xfers = 0;
  203. int log_incoming_xfers = 0;
  204.  
  205. /* Allow use of lreply(); this is here since some older FTP clients don't
  206.  * support continuation messages.  In violation of the RFCs... */
  207. int dolreplies = 1;
  208.  
  209. /* Spontaneous reply text.  To be sent along with next reply to user */
  210. char *autospout = NULL;
  211. int autospout_free = 0;
  212.  
  213. /* allowed on-the-fly file manipulations (compress, tar) */
  214. int mangleopts = 0;
  215.  
  216. /* number of login failures before attempts are logged and FTP *EXITS* */
  217. int lgi_failure_threshold = 5;
  218.  
  219. /* Timeout intervals for retrying connections to hosts that don't accept PORT
  220.  * cmds.  This is a kludge, but given the problems with TCP... */
  221. #define SWAITMAX    90          /* wait at most 90 seconds */
  222. #define SWAITINT    5           /* interval between retries */
  223.  
  224. int swaitmax = SWAITMAX;
  225. int swaitint = SWAITINT;
  226.  
  227. SIGNAL_TYPE lostconn(int sig);
  228. SIGNAL_TYPE randomsig(int sig);
  229. SIGNAL_TYPE myoob(int sig);
  230. FILE *getdatasock(char *mode),
  231.  *dataconn(char *name, off_t size, char *mode);
  232.  
  233. #ifdef SETPROCTITLE
  234. char **Argv = NULL;             /* pointer to argument vector */
  235. char *LastArgv = NULL;          /* end of argv */
  236. char proctitle[BUFSIZ];         /* initial part of title */
  237.  
  238. #endif /* SETPROCTITLE */
  239.  
  240. #ifdef KERBEROS
  241. void init_krb();
  242. void end_krb();
  243. char krb_ticket_name[100];
  244. #endif /* KERBEROS */
  245.  
  246. #ifdef ULTRIX_AUTH
  247. int ultrix_check_pass(char *passwd, char *xpasswd);
  248. #endif
  249.  
  250. /* ls program commands and options for lreplies on and off */
  251. char  ls_long[50];
  252. char  ls_short[50];
  253. struct aclmember *entry = NULL;
  254.  
  255. main(int argc, char **argv, char **envp)
  256. {
  257.     int addrlen,
  258.       on = 1;
  259. #ifdef IPTOS_LOWDELAY
  260.     int tos;
  261. #endif
  262.     char *cp;
  263.  
  264. #ifdef SecureWare
  265.     setluid(1);                         /* make sure there is a valid luid */
  266.     set_auth_parameters(argc,argv);
  267.     setreuid(0, 0);
  268. #endif
  269. #if defined(M_UNIX) && !defined(_M_UNIX)
  270.     res_init();                         /* bug in old (1.1.1) resolver     */
  271.     _res.retrans = 20;                  /* because of fake syslog in 3.2.2 */
  272.     setlogmask(LOG_UPTO(LOG_INFO));
  273. #endif
  274.  
  275.     addrlen = sizeof(his_addr);
  276.     if (getpeername(0, (struct sockaddr *) &his_addr, &addrlen) < 0) {
  277.         syslog(LOG_ERR, "getpeername (%s): %m", argv[0]);
  278. #ifndef DEBUG
  279.         exit(1);
  280. #endif
  281.     }
  282.     addrlen = sizeof(ctrl_addr);
  283.     if (getsockname(0, (struct sockaddr *) &ctrl_addr, &addrlen) < 0) {
  284.         syslog(LOG_ERR, "getsockname (%s): %m", argv[0]);
  285. #ifndef DEBUG
  286.         exit(1);
  287. #endif
  288.     }
  289. #ifdef IPTOS_LOWDELAY
  290.     tos = IPTOS_LOWDELAY;
  291.     if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int)) < 0)
  292.           syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
  293. #endif
  294.  
  295.     data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
  296.     debug = 0;
  297.  
  298. #ifdef FACILITY
  299.     openlog("ftpd", LOG_PID | LOG_NDELAY, FACILITY);
  300. #else
  301.     openlog("ftpd", LOG_PID);
  302. #endif
  303.  
  304. #ifdef SETPROCTITLE
  305.     /* Save start and extent of argv for setproctitle. */
  306.     Argv = argv;
  307.     while (*envp)
  308.         envp++;
  309.     LastArgv = envp[-1] + strlen(envp[-1]);
  310. #endif /* SETPROCTITLE */
  311.  
  312.     argc--, argv++;
  313.     while (argc > 0 && *argv[0] == '-') {
  314.         for (cp = &argv[0][1]; *cp; cp++)
  315.             switch (*cp) {
  316.  
  317.             case 'a':
  318.                 use_accessfile = 1;
  319.                 break;
  320.  
  321.             case 'A':
  322.                 use_accessfile = 0;
  323.                 break;
  324.  
  325.             case 'v':
  326.                 debug = 1;
  327.                 break;
  328.  
  329.             case 'd':
  330.                 debug = 1;
  331.                 break;
  332.  
  333.             case 'l':
  334.                 logging = 1;
  335.                 break;
  336.  
  337.             case 'L':
  338.                 log_commands = 1;
  339.                 break;
  340.  
  341.             case 'i':
  342.                 log_incoming_xfers = 1;
  343.                 break;
  344.  
  345.             case 'o':
  346.                 log_outbound_xfers = 1;
  347.                 break;
  348.  
  349.             case 't':
  350.                 timeout = atoi(++cp);
  351.                 if (maxtimeout < timeout)
  352.                     maxtimeout = timeout;
  353.                 goto nextopt;
  354.  
  355.             case 'T':
  356.                 maxtimeout = atoi(++cp);
  357.                 if (timeout > maxtimeout)
  358.                     timeout = maxtimeout;
  359.                 goto nextopt;
  360.  
  361.             case 'u':
  362.                 {
  363.                     int val = 0;
  364.  
  365.                     while (*++cp && *cp >= '0' && *cp <= '9')
  366.                         val = val * 8 + *cp - '0';
  367.                     if (*cp)
  368.                         fprintf(stderr, "ftpd: Bad value for -u\n");
  369.                     else
  370.                         defumask = val;
  371.                     goto nextopt;
  372.                 }
  373.  
  374.             default:
  375.                 fprintf(stderr, "ftpd: Unknown flag -%c ignored.\n",
  376.                         *cp);
  377.                 break;
  378.             }
  379.       nextopt:
  380.         argc--, argv++;
  381.     }
  382.     (void) freopen(_PATH_DEVNULL, "w", stderr);
  383.  
  384.     /* Checking for random signals ... */
  385. #ifdef SIGHUP
  386.     (void) signal(SIGHUP, randomsig);
  387. #endif
  388. #ifdef SIGINT
  389.     (void) signal(SIGINT, randomsig);
  390. #endif
  391. #ifdef SIGQUIT
  392.     (void) signal(SIGQUIT, randomsig);
  393. #endif
  394. #ifdef SIGILL
  395.     (void) signal(SIGILL, randomsig);
  396. #endif
  397. #ifdef SIGTRAP
  398.     (void) signal(SIGTRAP, randomsig);
  399. #endif
  400. #ifdef SIGIOT
  401.     (void) signal(SIGIOT, randomsig);
  402. #endif
  403. #ifdef SIGEMT
  404.     (void) signal(SIGEMT, randomsig);
  405. #endif
  406. #ifdef SIGFPE
  407.     (void) signal(SIGFPE, randomsig);
  408. #endif
  409. #ifdef SIGKILL
  410.     (void) signal(SIGKILL, randomsig);
  411. #endif
  412. #ifdef SIGBUS
  413.     (void) signal(SIGBUS, randomsig);
  414. #endif
  415. #ifdef SIGSEGV
  416.     (void) signal(SIGSEGV, randomsig);
  417. #endif
  418. #ifdef SIGSYS
  419.     (void) signal(SIGSYS, randomsig);
  420. #endif
  421. #ifdef SIGALRM
  422.     (void) signal(SIGALRM, randomsig);
  423. #endif
  424. #ifdef SIGSTOP
  425.     (void) signal(SIGSTOP, randomsig);
  426. #endif
  427. #ifdef SIGTSTP
  428.     (void) signal(SIGTSTP, randomsig);
  429. #endif
  430. #ifdef SIGTTIN
  431.     (void) signal(SIGTTIN, randomsig);
  432. #endif
  433. #ifdef SIGTTOU
  434.     (void) signal(SIGTTOU, randomsig);
  435. #endif
  436. #ifdef SIGIO
  437.     (void) signal(SIGIO, randomsig);
  438. #endif
  439. #ifdef SIGXCPU
  440.     (void) signal(SIGXCPU, randomsig);
  441. #endif
  442. #ifdef SIGXFSZ
  443.     (void) signal(SIGXFSZ, randomsig);
  444. #endif
  445. #ifdef SIGWINCH
  446.     (void) signal(SIGWINCH, randomsig);
  447. #endif
  448. #ifdef SIGVTALRM
  449.     (void) signal(SIGVTALRM, randomsig);
  450. #endif
  451. #ifdef SIGPROF
  452.     (void) signal(SIGPROF, randomsig);
  453. #endif
  454. #ifdef SIGUSR1
  455.     (void) signal(SIGUSR1, randomsig);
  456. #endif
  457. #ifdef SIGUSR2
  458.     (void) signal(SIGUSR2, randomsig);
  459. #endif
  460.  
  461. #ifdef SIGPIPE
  462.     (void) signal(SIGPIPE, lostconn);
  463. #endif
  464. #ifdef SIGCHLD
  465.     (void) signal(SIGCHLD, SIG_IGN);
  466. #endif
  467.  
  468. #ifdef SIGURG
  469.     if ((int) signal(SIGURG, myoob) < 0)
  470.         syslog(LOG_ERR, "signal: %m");
  471. #endif
  472.  
  473.     /* Try to handle urgent data inline */
  474. #ifdef SO_OOBINLINE
  475.     if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(int)) < 0)
  476.         syslog(LOG_ERR, "setsockopt (SO_OOBINLINE): %m");
  477. #endif
  478.  
  479. #ifdef  F_SETOWN
  480.     if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1)
  481.         syslog(LOG_ERR, "fcntl F_SETOWN: %m");
  482. #endif
  483.     dolog(&his_addr);
  484.     /* Set up default state */
  485.     data = -1;
  486.     type = TYPE_A;
  487.     form = FORM_N;
  488.     stru = STRU_F;
  489.     mode = MODE_S;
  490.     tmpline[0] = '\0';
  491.  
  492. #ifdef HAVE_SYSINFO
  493.     sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
  494. #else
  495.     (void) gethostname(hostname, sizeof (hostname));
  496. #endif
  497.  
  498.     access_init();
  499.     authenticate();
  500.     conv_init();
  501.  
  502.     if (is_shutdown(1) != 0) {
  503.         syslog(LOG_INFO, "connection refused (server shut down) from %s [%s]",
  504.                remotehost, remoteaddr);
  505.         reply(500, "%s FTP server shut down -- please try again later.",
  506.               hostname);
  507.         exit(0);
  508.     }
  509.     show_banner(220);
  510.  
  511.     entry = (struct aclmember *) NULL;
  512.     if (getaclentry("lslong", &entry) && ARG0 && strlen(ARG0) > 0) {
  513.           strcpy(ls_long,ARG0);
  514.       if (ARG1 && strlen(ARG1)) {
  515.              strcat(ls_long," ");
  516.          strcat(ls_long,ARG1);
  517.           }
  518.     } else {
  519. #ifdef SVR4
  520. #ifndef AIX
  521.           strcpy(ls_long,"/bin/ls -la");
  522. #else
  523.           strcpy(ls_long,"/bin/ls -lA");
  524. #endif
  525. #else
  526.           strcpy(ls_long,"/bin/ls -lgA");
  527. #endif
  528.     }
  529.     strcat(ls_long," %s");
  530.  
  531.     entry = (struct aclmember *) NULL;
  532.     if (getaclentry("lsshort", &entry) && ARG0 && strlen(ARG0) > 0) {
  533.           strcpy(ls_short,ARG0);
  534.       if (ARG1 && strlen(ARG1)) {
  535.              strcat(ls_short," ");
  536.              strcat(ls_short,ARG1);
  537.       }
  538.     } else {
  539. #ifdef SVR4
  540. #ifndef AIX
  541.           strcpy(ls_short,"/bin/ls -la");
  542. #else
  543.           strcpy(ls_short,"/bin/ls -lA");
  544. #endif
  545. #else
  546.           strcpy(ls_short,"/bin/ls -lgA");
  547. #endif
  548.     }
  549.     strcat(ls_short," %s");
  550.  
  551.     reply(220, "%s FTP server (%s) ready.", hostname, version);
  552.     (void) setjmp(errcatch);
  553.  
  554.     for (;;)
  555.         (void) yyparse();
  556.     /* NOTREACHED */
  557. }
  558.  
  559. SIGNAL_TYPE
  560. randomsig(int sig)
  561. {
  562.     syslog(LOG_ERR, "exiting on signal %d", sig);
  563.     chdir("/etc/tmp");
  564.     signal(SIGIOT, SIG_DFL);
  565.     signal(SIGILL, SIG_DFL);
  566.     abort();
  567.     /* dologout(-1); *//* NOTREACHED */
  568. }
  569.  
  570. SIGNAL_TYPE
  571. lostconn(int sig)
  572. {
  573.     if (debug)
  574.         syslog(LOG_DEBUG, "lost connection to %s [%s]", remotehost, remoteaddr);
  575.     dologout(-1);
  576. }
  577.  
  578. static char ttyline[20];
  579.  
  580. /* Helper function for sgetpwnam(). */
  581. char *
  582. sgetsave(char *s)
  583. {
  584.     char *new;
  585.     
  586.     new = (char *) malloc(strlen(s) + 1);
  587.  
  588.     if (new == NULL) {
  589.         perror_reply(421, "Local resource failure: malloc");
  590.         dologout(1);
  591.         /* NOTREACHED */
  592.     }
  593.     (void) strcpy(new, s);
  594.     return (new);
  595. }
  596.  
  597. /* Save the result of a getpwnam.  Used for USER command, since the data
  598.  * returned must not be clobbered by any other command (e.g., globbing). */
  599. struct passwd *
  600. sgetpwnam(char *name)
  601. {
  602.     static struct passwd save;
  603.     register struct passwd *p;
  604. #ifdef M_UNIX
  605.     struct passwd *ret = (struct passwd *) NULL;
  606. #endif
  607.     char *sgetsave(char *s);
  608.  
  609. #ifdef KERBEROS
  610.     register struct authorization *q;
  611. #endif /* KERBEROS */
  612.  
  613. #ifdef SecureWare
  614.     struct pr_passwd *pr;
  615. #endif
  616.  
  617. #ifdef KERBEROS
  618.     init_krb();
  619.     q = getauthuid(p->pw_uid);
  620.     end_krb();
  621. #endif /* KERBEROS */
  622.  
  623. #ifdef M_UNIX
  624. # ifdef SecureWare
  625.     if ((pr = getprpwnam(name)) == NULL)
  626.         goto DONE;
  627. # endif /* SecureWare */
  628.     if ((p = getpwnam(name)) == NULL)
  629.         goto DONE;
  630. #else   /* M_UNIX */
  631. # ifdef SecureWare
  632.     if ((pr = getprpwnam(name)) == NULL)
  633.         return((struct passwd *) pr);
  634. # endif /* SecureWare */
  635.     if ((p = getpwnam(name)) == NULL)
  636.         return (p);
  637. #endif  /* M_UNIX */
  638.  
  639.     if (save.pw_name)   free(save.pw_name);
  640.     if (save.pw_gecos)  free(save.pw_gecos);
  641.     if (save.pw_dir)    free(save.pw_dir);
  642.     if (save.pw_shell)  free(save.pw_shell);
  643.  
  644.     save = *p;
  645.  
  646.     save.pw_name = sgetsave(p->pw_name);
  647.  
  648. #ifdef KERBEROS
  649.     save.pw_passwd = sgetsave(q->a_password);
  650. #elif defined(SecureWare)
  651.      if (pr->uflg.fg_encrypt && pr->ufld.fd_encrypt && *pr->ufld.fd_encrypt)
  652.         save.pw_passwd = sgetsave(pr->ufld.fd_encrypt);
  653.      else
  654.         save.pw_passwd = sgetsave("");
  655. #else
  656.     save.pw_passwd = sgetsave(p->pw_passwd);
  657. #endif
  658.  
  659.     save.pw_gecos = sgetsave(p->pw_gecos);
  660.     save.pw_dir = sgetsave(p->pw_dir);
  661.     save.pw_shell = sgetsave(p->pw_shell);
  662. #ifdef M_UNIX
  663.     ret = &save;
  664. DONE:
  665.     endpwent();
  666. #endif
  667. #ifdef SecureWare
  668.     endprpwent();
  669. #endif
  670. #ifdef M_UNIX
  671.     return(ret);
  672. #else
  673.     return(&save);
  674. #endif
  675. }
  676.  
  677. int login_attempts;             /* number of failed login attempts */
  678. int askpasswd;                  /* had user command, ask for passwd */
  679.  
  680. /* USER command. Sets global passwd pointer pw if named account exists and is
  681.  * acceptable; sets askpasswd if a PASS command is expected.  If logged in
  682.  * previously, need to reset state.  If name is "ftp" or "anonymous", the
  683.  * name is not in _PATH_FTPUSERS, and ftp account exists, set anonymous and
  684.  * pw, then just return.  If account doesn't exist, ask for passwd anyway.
  685.  * Otherwise, check user requesting login privileges.  Disallow anyone who
  686.  * does not have a standard shell as returned by getusershell().  Disallow
  687.  * anyone mentioned in the file _PATH_FTPUSERS to allow people such as root
  688.  * and uucp to be avoided. */
  689. user(char *name)
  690. {
  691.     register char *cp;
  692.     char *shell;
  693.     char *getusershell();
  694.     int   why = 0;
  695.  
  696. #ifdef HOST_ACCESS                     /* 19-Mar-93    BM              */
  697.     if (!rhost_ok(name, remotehost, remoteaddr))
  698.     {
  699.             reply(530, "User %s access denied.", name);
  700.             syslog(LOG_NOTICE,
  701.                     "FTP LOGIN REFUSED (name in %s) FROM %s [%s], %s",
  702.                      _PATH_FTPHOSTS, remotehost, remoteaddr, name);
  703.             return;
  704.     }
  705. #endif
  706.  
  707. #ifdef LOG_FAILED                       /* 06-Nov-92    EHK             */
  708.     strncpy(the_user, name, MAXUSERNAMELEN - 1);
  709. #endif
  710.  
  711.     if (logged_in) {
  712.         if (anonymous || guest) {
  713.             reply(530, "Can't change user from guest login.");
  714.             return;
  715.         }
  716.         end_login();
  717.     }
  718.  
  719.     anonymous = 0;
  720.     acl_remove();
  721.  
  722.     if (!strcasecmp(name, "ftp") || !strcasecmp(name, "anonymous")) {
  723.       struct aclmember *entry = NULL;
  724.       int machineok=1;
  725.       char guestservername[MAXHOSTNAMELEN];
  726.       guestservername[0]='\0';
  727.  
  728.       if (checkuser("ftp") || checkuser("anonymous")) {
  729.           reply(530, "User %s access denied.", name);
  730.           syslog(LOG_NOTICE,
  731.            "FTP LOGIN REFUSED (ftp in /etc/ftpusers) FROM %s [%s], %s",
  732.            remotehost, remoteaddr, name);
  733.           return;
  734.           
  735.         /*
  736.         ** Algorithm used:
  737.         ** - if no "guestserver" directive is present,
  738.         **     anonymous access is allowed, for backward compatibility.
  739.         ** - if a "guestserver" directive is present,
  740.         **     anonymous access is restricted to the machines listed,
  741.         **     usually the machine whose CNAME on the current domain
  742.         **     is "ftp"...
  743.         **
  744.         ** the format of the "guestserver" line is
  745.         ** guestserver [<machine1> [<machineN>]]
  746.         ** that is, "guestserver" will forbid anonymous access on all machines
  747.         ** while "guestserver ftp inf" will allow anonymous access on
  748.         ** the two machines whose CNAMES are "ftp.enst.fr" and "inf.enst.fr".
  749.         **
  750.         ** if anonymous access is denied on the current machine,
  751.         ** the user will be asked to use the first machine listed (if any)
  752.         ** on the "guestserver" line instead:
  753.         ** 530- Guest login not allowed on this machine,
  754.         **      connect to ftp.enst.fr instead.
  755.         **
  756.         ** -- <Nicolas.Pioch@enst.fr>
  757.         */
  758.       } else if (getaclentry("guestserver", &entry)
  759.                  && ARG0 && strlen(ARG0) > 0) {
  760.         struct hostent *tmphostent;
  761.  
  762.         /*
  763.         ** if a "guestserver" line is present,
  764.         ** default is not to allow guest logins
  765.         */
  766.         machineok=0;
  767.  
  768.         if (hostname[0]
  769.             && ((tmphostent=gethostbyname(hostname)))) {
  770.  
  771.           /*
  772.           ** hostname is the only first part of the FQDN
  773.           ** this may or may not correspond to the h_name value
  774.           ** (machines with more than one IP#, CNAMEs...)
  775.           ** -> need to fix that, calling gethostbyname on hostname
  776.           **
  777.           ** WARNING!
  778.           ** for SunOS 4.x, you need to have a working resolver in the libc
  779.           ** for CNAMES to work properly.
  780.           ** If you don't, add "-lresolv" to the libraries before compiling!
  781.           */
  782.           char dns_localhost[MAXHOSTNAMELEN];
  783.           int machinecount;
  784.  
  785.           strncpy(dns_localhost,
  786.                   tmphostent->h_name,
  787.                   sizeof(dns_localhost));
  788.           dns_localhost[sizeof(dns_localhost)-1]='\0';
  789.  
  790.           for (machinecount=0;
  791.                entry->arg[machinecount] && (entry->arg[machinecount])[0];
  792.                machinecount++) {
  793.  
  794.             if ((tmphostent=gethostbyname(entry->arg[machinecount]))) {
  795.               /*
  796.               ** remember the name of the first machine for redirection
  797.               */
  798.  
  799.               if ((!machinecount) && tmphostent->h_name) {
  800.                 strncpy(guestservername, entry->arg[machinecount],
  801.                         sizeof(guestservername));
  802.                 guestservername[sizeof(guestservername)-1]='\0';
  803.               }
  804.  
  805.               if (!strcasecmp(tmphostent->h_name, dns_localhost)) {
  806.                 machineok++;
  807.                 break;
  808.               }
  809.             }
  810.           }
  811.         }
  812.       }
  813.       if (!machineok) {
  814.         if (guestservername[0])
  815.           reply(530,
  816.              "Guest login not allowed on this machine, connect to %s instead.",
  817.                 guestservername);
  818.         else
  819.           reply(530,
  820.                 "Guest login not allowed on this machine.");
  821.         syslog(LOG_NOTICE,
  822.                "FTP LOGIN REFUSED (localhost not in guestservers) FROM %s [%s], %s",
  823.                remotehost, remoteaddr, name);
  824.         /* End of the big patch -- Nap */
  825.  
  826.         } else if ((pw = sgetpwnam("ftp")) != NULL) {
  827.             anonymous = 1;      /* for the access_ok call */
  828.             if ((why = access_ok(530)) == 1) {
  829.                 askpasswd = 1;
  830.                 acl_setfunctions();
  831.                 reply(331, "Guest login ok, send your complete e-mail address as password.");
  832.             } else if (why == 0) {
  833.                 reply(530, "User %s access denied..", name);
  834.                 syslog(LOG_NOTICE,
  835.                        "FTP LOGIN REFUSED (access denied) FROM %s [%s], %s",
  836.                        remotehost, remoteaddr, name);
  837.                 anonymous = 0;
  838.             } else {
  839.                 reply(530, "User %s access denied.", name);
  840.                 syslog(LOG_NOTICE,
  841.                        "FTP LOGIN REFUSED (access denied) FROM %s [%s], %s",
  842.                        remotehost, remoteaddr, name);
  843.                 dologout(0);
  844.             }
  845.         } else {
  846.             reply(530, "User %s unknown.", name);
  847.             syslog(LOG_NOTICE,
  848.               "FTP LOGIN REFUSED (ftp not in /etc/passwd) FROM %s [%s], %s",
  849.                    remotehost, remoteaddr, name);
  850.         }
  851.         return;
  852.     }
  853.     if ((pw = sgetpwnam(name)) != NULL) {
  854.         if ((shell = pw->pw_shell) == NULL || *shell == 0)
  855.             shell = _PATH_BSHELL;
  856.         while ((cp = getusershell()) != NULL)
  857.             if (strcmp(cp, shell) == 0)
  858.                 break;
  859.         endusershell();
  860.         if (cp == NULL || checkuser(name)) {
  861.             reply(530, "User %s access denied...", name);
  862.             if (logging)
  863.                 syslog(LOG_NOTICE,
  864.                        "FTP LOGIN REFUSED (bad shell) FROM %s [%s], %s",
  865.                        remotehost, remoteaddr, name);
  866.             pw = (struct passwd *) NULL;
  867.             return;
  868.         }
  869.         /* if user is a member of any of the guestgroups, cause a chroot() */
  870.         /* after they log in successfully                                  */
  871.         guest = acl_guestgroup(pw);
  872.     }
  873.     if (access_ok(530) < 1) {
  874.         reply(530, "User %s access denied....", name);
  875.         syslog(LOG_NOTICE, "FTP LOGIN REFUSED (access denied) FROM %s [%s], %s",
  876.                remotehost, remoteaddr, name);
  877.         return;
  878.     } else
  879.         acl_setfunctions();
  880.  
  881.     reply(331, "Password required for %s.", name);
  882.     askpasswd = 1;
  883.     /* Delay before reading passwd after first failed attempt to slow down
  884.      * passwd-guessing programs. */
  885.     if (login_attempts)
  886.         sleep((unsigned) login_attempts);
  887. }
  888.  
  889. /* Check if a user is in the file _PATH_FTPUSERS */
  890. checkuser(char *name)
  891. {
  892.     register FILE *fd;
  893.     register char *p;
  894.     char line[BUFSIZ];
  895.  
  896.     if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) {
  897.         while (fgets(line, sizeof(line), fd) != NULL)
  898.             if ((p = strchr(line, '\n')) != NULL) {
  899.                 *p = '\0';
  900.                 if (line[0] == '#')
  901.                     continue;
  902.                 if (strcmp(line, name) == 0) {
  903.                     (void) fclose(fd);
  904.                     return (1);
  905.                 }
  906.             }
  907.         (void) fclose(fd);
  908.     }
  909.     return (0);
  910. }
  911.  
  912. /* Terminate login as previous user, if any, resetting state; used when USER
  913.  * command is given or login fails. */
  914. end_login(void)
  915. {
  916.  
  917.     (void) seteuid((uid_t) 0);
  918.     if (logged_in)
  919.         logwtmp(ttyline, "", "");
  920.     pw = NULL;
  921.     logged_in = 0;
  922.     anonymous = 0;
  923.     guest = 0;
  924. }
  925.  
  926. int
  927. validate_eaddr(char *eaddr)
  928. {
  929.     int i,
  930.       host,
  931.       state;
  932.  
  933.     for (i = host = state = 0; eaddr[i] != '\0'; i++) {
  934.         switch (eaddr[i]) {
  935.         case '.':
  936.             if (!host)
  937.                 return 0;
  938.             if (state == 2)
  939.                 state = 3;
  940.             host = 0;
  941.             break;
  942.         case '@':
  943.             if (!host || state > 1 || !strncasecmp("ftp", eaddr + i - host, host))
  944.                 return 0;
  945.             state = 2;
  946.             host = 0;
  947.             break;
  948.         case '!':
  949.         case '%':
  950.             if (!host || state > 1)
  951.                 return 0;
  952.             state = 1;
  953.             host = 0;
  954.             break;
  955.         case '-':
  956.             break;
  957.         default:
  958.             host++;
  959.         }
  960.     }
  961.     if (((state == 3) && host > 1) || ((state == 2) && !host) ||
  962.         ((state == 1) && host > 1))
  963.         return 1;
  964.     else
  965.         return 0;
  966. }
  967.  
  968. pass(char *passwd)
  969. {
  970.     char *xpasswd,
  971.      *salt;
  972.  
  973. #ifdef ULTRIX_AUTH
  974.     int numfails;
  975. #endif /* ULTRIX_AUTH */
  976.  
  977.     if (logged_in || askpasswd == 0) {
  978.         reply(503, "Login with USER first.");
  979.         return;
  980.     }
  981.     askpasswd = 0;
  982.  
  983.     /* Disable lreply() if the first character of the password is '-' since
  984.      * some hosts don't understand continuation messages and hang... */
  985.  
  986.     if (*passwd == '-')
  987.         dolreplies = 0;
  988.     else
  989.         dolreplies = 1;
  990.  
  991.     if (!anonymous) {           /* "ftp" is only account allowed no password */
  992.         if (*passwd == '-')
  993.             passwd++;
  994. #ifdef SHADOW_PASSWORD
  995.         if (pw) {
  996.            struct spwd *spw = getspnam( pw->pw_name );
  997.            if( !spw ) { pw->pw_passwd = ""; }
  998.            else { pw->pw_passwd = spw->sp_pwdp; }
  999.         }
  1000. #endif
  1001.  
  1002.         *guestpw = NULL;
  1003.         if (pw == NULL)
  1004.             salt = "xx";
  1005.         else
  1006.             salt = pw->pw_passwd;
  1007. #ifdef KERBEROS
  1008.         xpasswd = crypt16(passwd, salt);
  1009. #else
  1010.         xpasswd = crypt(passwd, salt);
  1011. #endif
  1012.  
  1013. #ifdef ULTRIX_AUTH
  1014.         if ((numfails = ultrix_check_pass(passwd, xpasswd)) < 0) {
  1015. #else
  1016.         /* The strcmp does not catch null passwords! */
  1017.         if (pw == NULL || *pw->pw_passwd == '\0' ||
  1018.             strcmp(xpasswd, pw->pw_passwd)) {
  1019. #endif
  1020.             reply(530, "Login incorrect.");
  1021.  
  1022. #ifdef LOG_FAILED                       /* 27-Apr-93    EHK/BM             */
  1023.             syslog(LOG_INFO, "failed login from %s [%s], %s",
  1024.                               remotehost, remoteaddr, the_user);
  1025. #endif
  1026.             acl_remove();
  1027.  
  1028.             pw = NULL;
  1029.             if (++login_attempts >= lgi_failure_threshold) {
  1030.                 syslog(LOG_NOTICE, "repeated login failures from %s [%s]",
  1031.                        remotehost, remoteaddr);
  1032.                 exit(0);
  1033.             }
  1034.             return;
  1035.         }
  1036.     } else {
  1037.         char *pwin,
  1038.          *pwout = guestpw;
  1039.         struct aclmember *entry = NULL;
  1040.         int valid;
  1041.  
  1042.         if (getaclentry("passwd-check", &entry) &&
  1043.             ARG0 && strcasecmp(ARG0, "none")) {
  1044.  
  1045.             if (!strcasecmp(ARG0, "rfc822"))
  1046.                 valid = validate_eaddr(passwd);
  1047.             else if (!strcasecmp(ARG0, "trivial"))
  1048.                 valid = (strchr(passwd, '@') == NULL) ? 0 : 1;
  1049.             else
  1050.                 valid = 1;
  1051.  
  1052.             if (!valid && ARG1 && !strcasecmp(ARG1, "enforce")) {
  1053.                 lreply(530, "The response '%s' is not valid", passwd);
  1054.                 lreply(530, "Please use your e-mail address as your password");
  1055.                 lreply(530, "   for example: %s@%s or %s@",
  1056.                        authenticated ? authuser : "joe", remotehost,
  1057.                        authenticated ? authuser : "joe");
  1058.                 lreply(530, "[%s will be added if password ends with @]",
  1059.                        remotehost);
  1060.                 reply(530, "Login incorrect.");
  1061.         acl_remove();    
  1062.                 if (++login_attempts >= lgi_failure_threshold) {
  1063.                     syslog(LOG_NOTICE, "repeated login failures from %s [%s]",
  1064.                            remotehost, remoteaddr);
  1065.                     exit(0);
  1066.                 }
  1067.                 return;
  1068.             } else if (!valid) {
  1069.                 lreply(230, "The response '%s' is not valid", passwd);
  1070.                 lreply(230,
  1071.                 "Next time please use your e-mail address as your password");
  1072.                 lreply(230, "        for example: %s@%s",
  1073.                        authenticated ? authuser : "joe", remotehost);
  1074.             }
  1075.         }
  1076.         if (!*passwd) {
  1077.             strcpy(guestpw, "[none_given]");
  1078.         } else {
  1079.             int cnt = sizeof(guestpw) - 2;
  1080.  
  1081.             for (pwin = passwd; *pwin && cnt--; pwin++)
  1082.                 if (!isgraph(*pwin))
  1083.                     *pwout++ = '_';
  1084.                 else
  1085.                     *pwout++ = *pwin;
  1086.         }
  1087.     }
  1088.  
  1089.     /* if autogroup command applies to user's class change pw->pw_gid */
  1090.     if (anonymous)
  1091.         (void) acl_autogroup(pw);
  1092.  
  1093.     login_attempts = 0;         /* this time successful */
  1094.     (void) setegid((gid_t) pw->pw_gid);
  1095.     (void) initgroups(pw->pw_name, pw->pw_gid);
  1096.  
  1097.     /* open wtmp before chroot */
  1098.     (void) sprintf(ttyline, "ftp%d", getpid());
  1099.     logwtmp(ttyline, pw->pw_name, remotehost);
  1100.     logged_in = 1;
  1101.  
  1102.     /* if logging is enabled, open logfile before chroot */
  1103.     if (log_outbound_xfers || log_incoming_xfers)
  1104.         xferlog = open(_PATH_XFERLOG, O_WRONLY | O_APPEND | O_CREAT, 0660);
  1105.  
  1106.     expand_id();
  1107.  
  1108.     if (anonymous || guest) {
  1109.         /* We MUST do a chdir() after the chroot. Otherwise the old current
  1110.          * directory will be accessible as "." outside the new root! */
  1111.         if (anonymous) {
  1112.             if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
  1113.                 reply(550, "Can't set guest privileges.");
  1114.                 goto bad;
  1115.             }
  1116.         } else if (guest) {
  1117.             char *sp;
  1118.  
  1119.             /* determine root and home directory */
  1120.  
  1121.             if ((sp = strstr(pw->pw_dir, "/./")) == NULL) {
  1122.                 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) {
  1123.                     reply(550, "Can't set guest privileges.");
  1124.                     goto bad;
  1125.                 }
  1126.             } else {
  1127.                 *sp++ = '\0';
  1128.  
  1129.                 if (chroot(pw->pw_dir) < 0 || chdir(++sp) < 0) {
  1130.                     reply(550, "Can't set guest privileges.");
  1131.                     goto bad;
  1132.                 }
  1133.             }
  1134.         }
  1135.     } else {
  1136.         if (chdir(pw->pw_dir) < 0) {
  1137.             if (chdir("/") < 0) {
  1138.                 reply(530, "User %s: can't change directory to %s.",
  1139.                       pw->pw_name, pw->pw_dir);
  1140.                 goto bad;
  1141.             } else
  1142.                 lreply(230, "No directory! Logging in with home=/");
  1143.         }
  1144.     }
  1145.  
  1146. #ifdef AIX
  1147.     {
  1148.        /* AIX 3 lossage.  Don't ask.  It's undocumented.  */
  1149.        priv_t priv;
  1150.  
  1151.        priv.pv_priv[0] = 0;
  1152.        priv.pv_priv[1] = 0;
  1153.        setgroups(NULL, NULL);
  1154.        if (setpriv(PRIV_SET|PRIV_INHERITED|PRIV_EFFECTIVE|PRIV_BEQUEATH,
  1155.                    &priv, sizeof(priv_t)) < 0 ||
  1156.            setuidx(ID_REAL|ID_EFFECTIVE, (uid_t)pw->pw_uid) < 0 ||
  1157.            seteuid((uid_t)pw->pw_uid) < 0) {
  1158.                reply(550, "Can't set uid (AIX3).");
  1159.                goto bad;
  1160.        }
  1161.     }
  1162. # ifdef UID_DEBUG
  1163.     lreply(230, "ruid=%d, euid=%d, suid=%d, luid=%d", getuidx(ID_REAL),
  1164.          getuidx(ID_EFFECTIVE), getuidx(ID_SAVED), getuidx(ID_LOGIN));
  1165.     lreply(230, "rgid=%d, egid=%d, sgid=%d, lgid=%d", getgidx(ID_REAL),
  1166.          getgidx(ID_EFFECTIVE), getgidx(ID_SAVED), getgidx(ID_LOGIN));
  1167. #endif
  1168. #else
  1169.     if (seteuid((uid_t) pw->pw_uid) < 0) {
  1170.         reply(550, "Can't set uid.");
  1171.         goto bad;
  1172.     }
  1173. #endif
  1174.     /* * following two lines were inside the next scope... */
  1175.  
  1176.     show_message(230, LOG_IN);
  1177.     show_readme(230, LOG_IN);
  1178.  
  1179. #ifdef ULTRIX_AUTH
  1180.     if (!anonymous && numfails > 0) {
  1181.         lreply(230,
  1182.             "There have been %d unsuccessful login attempts on your account",
  1183.             numfails);
  1184.     }
  1185. #endif /* ULTRIX_AUTH */    
  1186.  
  1187.     if (anonymous) {
  1188.         (void) is_shutdown(0);  /* display any shutdown messages now */
  1189.  
  1190.         reply(230, "Guest login ok, access restrictions apply.");
  1191. #ifdef SETPROCTITLE
  1192.         sprintf(proctitle, "%s: anonymous/%.*s", remotehost,
  1193.                     sizeof(proctitle) - sizeof(remotehost) -
  1194.                     sizeof(": anonymous/"), passwd);
  1195.         setproctitle("%s", proctitle);
  1196. #endif /* SETPROCTITLE */
  1197.         if (logging)
  1198.             syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s [%s], %s",
  1199.                    remotehost, remoteaddr, passwd);
  1200.     } else {
  1201.         reply(230, "User %s logged in.%s", pw->pw_name, guest ?
  1202.               "  Access restrictions apply." : "");
  1203. #ifdef SETPROCTITLE
  1204.         sprintf(proctitle, "%s: %s", remotehost, pw->pw_name);
  1205.         setproctitle(proctitle);
  1206. #endif /* SETPROCTITLE */
  1207.         if (logging)
  1208.             syslog(LOG_INFO, "FTP LOGIN FROM %s [%s], %s",
  1209.                    remotehost, remoteaddr, pw->pw_name);
  1210.     }
  1211.     home = pw->pw_dir;          /* home dir for globbing */
  1212.     (void) umask(defumask);
  1213.     return;
  1214.   bad:
  1215.     /* Forget all about it... */
  1216.     xferlog = 0;
  1217.     end_login();
  1218. }
  1219.  
  1220. char *
  1221. opt_string(int options)
  1222. {
  1223.     static char buf[100];
  1224.     char *ptr = buf;
  1225.  
  1226.     if ((options & O_COMPRESS) != NULL)
  1227.         *ptr++ = 'C';
  1228.     if ((options & O_TAR) != NULL)
  1229.         *ptr++ = 'T';
  1230.     if ((options & O_UNCOMPRESS) != NULL)
  1231.         *ptr++ = 'U';
  1232.     if (options == 0)
  1233.         *ptr++ = '_';
  1234.     *ptr++ = '\0';
  1235.     return (buf);
  1236. }
  1237.  
  1238. retrieve(char *cmd, char *name)
  1239. {
  1240.     FILE *fin,
  1241.      *dout;
  1242.     struct stat st,
  1243.       junk;
  1244.     int (*closefunc) () = NULL;
  1245.     int options = 0;
  1246.     time_t start_time = time(NULL);
  1247.     static char *logname;
  1248.     char namebuf[MAXPATHLEN];
  1249.     struct convert *cptr;
  1250.  
  1251.     if (!cmd && stat(name, &st)) {
  1252.         char fnbuf[MAXPATHLEN],
  1253.          *ptr;
  1254.  
  1255.         cptr = cvtptr;
  1256.  
  1257.         if (cptr == NULL) {
  1258.             (void) reply(550, "%s: No such file OR directory.", name);
  1259.             return;
  1260.         }
  1261.  
  1262.         do {
  1263.             if (!(mangleopts & O_COMPRESS) && (cptr->options & O_COMPRESS))
  1264.                 continue;
  1265.             if (!(mangleopts & O_UNCOMPRESS) && (cptr->options & O_UNCOMPRESS))
  1266.                 continue;
  1267.             if (!(mangleopts & O_TAR) && (cptr->options & O_TAR))
  1268.                 continue;
  1269.  
  1270.             if ( (cptr->stripfix) && (cptr->postfix) ) {
  1271.                 int pfxlen = strlen(cptr->postfix);
  1272.         int sfxlen = strlen(cptr->stripfix);
  1273.                 int namelen = strlen(name);
  1274.                 (void) strcpy(fnbuf, name);
  1275.  
  1276.                 if (namelen <= pfxlen)
  1277.                     continue;
  1278.         if ((namelen - pfxlen + sfxlen) >= sizeof(fnbuf))
  1279.             continue;
  1280.  
  1281.         if (strcmp(fnbuf + namelen - pfxlen, cptr->postfix))
  1282.             continue;
  1283.                 *(fnbuf + namelen - pfxlen) = '\0';
  1284.                 (void) strcat(fnbuf, cptr->stripfix);
  1285.                 if (stat(fnbuf, &st))
  1286.                     continue;
  1287.             } else if (cptr->postfix) {
  1288.                 int pfxlen = strlen(cptr->postfix);
  1289.                 int namelen = strlen(name);
  1290.  
  1291.                 if (namelen <= pfxlen)
  1292.                     continue;
  1293.                 (void) strcpy(fnbuf, name);
  1294.                 if (strcmp(fnbuf + namelen - pfxlen, cptr->postfix))
  1295.                     continue;
  1296.                 *(fnbuf + namelen - pfxlen) = (char) NULL;
  1297.                 if (stat(fnbuf, &st))
  1298.                     continue;
  1299.             } else if (cptr->stripfix) {
  1300.                 (void) strcpy(fnbuf, name);
  1301.                 (void) strcat(fnbuf, cptr->stripfix);
  1302.                 if (stat(fnbuf, &st))
  1303.                     continue;
  1304.             } else {
  1305.                 (void) reply(550, "%s: No such file OR directory.", name);
  1306.                 return;
  1307.             }
  1308.  
  1309.             if (S_ISDIR(st.st_mode)) {
  1310.                 if (!(cptr->types & T_DIR)) {
  1311.                     (void) reply(550, "Cannot %s directories.", cptr->name);
  1312.                     return;
  1313.                 }
  1314.                 if (cptr->options & O_TAR) {
  1315.                     strcpy(namebuf, fnbuf);
  1316.                     strcat(namebuf, "/.notar");
  1317.                     if (!stat(namebuf, &junk)) {
  1318.                         (void) reply(550,
  1319.                                   "Sorry, you may not TAR that directory.");
  1320.                         return;
  1321.                     }
  1322.                 }
  1323.             }
  1324.  
  1325.             if (S_ISREG(st.st_mode) && !(cptr->types & T_REG)) {
  1326.                 (void) reply(550, "Cannot %s plain files.", cptr->name);
  1327.                 return;
  1328.             }
  1329.             if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
  1330.                 (void) reply(550, "Cannot %s special files.", cptr->name);
  1331.                 return;
  1332.             }
  1333.             if (!(cptr->types & T_ASCII) && deny_badasciixfer(550, ""))
  1334.                 return;
  1335.  
  1336.             logname = fnbuf;
  1337.             options |= cptr->options;
  1338.  
  1339.             strcpy(namebuf, cptr->external_cmd);
  1340.             if ((ptr = strchr(namebuf, ' ')) != NULL)
  1341.                 *ptr = '\0';
  1342.             if (stat(namebuf, &st) != NULL) {
  1343.                 syslog(LOG_ERR, "external command %s not found",
  1344.                        namebuf);
  1345.                 (void) reply(550,
  1346.                 "Local error: conversion program not found. Cannot %s file.",
  1347.                              cptr->name);
  1348.                 return;
  1349.             }
  1350.             (void) retrieve(cptr->external_cmd, fnbuf);
  1351.  
  1352.             goto dolog;
  1353.         } while ( (cptr = cptr->next) != NULL );
  1354.  
  1355.     } else
  1356.         logname = (char *) NULL;
  1357.  
  1358.     if (cmd == 0) {
  1359.         fin = fopen(name, "r"), closefunc = fclose;
  1360.         st.st_size = 0;
  1361.     } else {
  1362.         char line[BUFSIZ];
  1363.  
  1364.         (void) sprintf(line, cmd, name), name = line;
  1365.         fin = ftpd_popen(line, "r", 1), closefunc = ftpd_pclose;
  1366.         st.st_size = -1;
  1367. #ifdef HAVE_ST_BLKSIZE
  1368.         st.st_blksize = BUFSIZ;
  1369. #endif
  1370.     }
  1371.     if (fin == NULL) {
  1372.         if (errno != 0)
  1373.             perror_reply(550, name);
  1374.         return;
  1375.     }
  1376.     if (cmd == 0 &&
  1377.         (fstat(fileno(fin), &st) < 0 || (st.st_mode & S_IFMT) != S_IFREG)) {
  1378.         reply(550, "%s: not a plain file.", name);
  1379.         goto done;
  1380.     }
  1381.     if (restart_point) {
  1382.         if (type == TYPE_A) {
  1383.             register int i,
  1384.               n,
  1385.               c;
  1386.  
  1387.             n = restart_point;
  1388.             i = 0;
  1389.             while (i++ < n) {
  1390.                 if ((c = getc(fin)) == EOF) {
  1391.                     perror_reply(550, name);
  1392.                     goto done;
  1393.                 }
  1394.                 if (c == '\n')
  1395.                     i++;
  1396.             }
  1397.         } else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
  1398.             perror_reply(550, name);
  1399.             goto done;
  1400.         }
  1401.     }
  1402.     dout = dataconn(name, st.st_size, "w");
  1403.     if (dout == NULL)
  1404.         goto done;
  1405. #ifdef HAVE_ST_BLKSIZE
  1406.     send_data(fin, dout, st.st_blksize);
  1407. #else
  1408.     send_data(fin, dout, BUFSIZ);
  1409. #endif
  1410.     (void) fclose(dout);
  1411.  
  1412.   dolog:
  1413.     if (log_outbound_xfers && xferlog && (cmd == 0)) {
  1414.         char msg[MAXPATHLEN];
  1415.         int xfertime = time(NULL) - start_time;
  1416.         time_t curtime = time(NULL);
  1417.         int loop;
  1418.  
  1419.         if (!xfertime)
  1420.             xfertime++;
  1421.         realpath(logname ? logname : name, namebuf);
  1422.         for (loop = 0; namebuf[loop]; loop++)
  1423.             if (isspace(namebuf[loop]) || iscntrl(namebuf[loop]))
  1424.                 namebuf[loop] = '_';
  1425.         sprintf(msg, "%.24s %d %s %d %s %c %s %c %c %s ftp %d %s\n",
  1426.                 ctime(&curtime),
  1427.                 xfertime,
  1428.                 remotehost,
  1429.                 byte_count,
  1430.                 namebuf,
  1431.                 (type == TYPE_A) ? 'a' : 'b',
  1432.                 opt_string(options),
  1433.                 'o',
  1434.                 anonymous ? 'a' : 'r',
  1435.                 anonymous ? guestpw : pw->pw_name,
  1436.                 authenticated,
  1437.                 authenticated ? authuser : "*"
  1438.             );
  1439.         write(xferlog, msg, strlen(msg));
  1440.     }
  1441.     data = -1;
  1442.     pdata = -1;
  1443.   done:
  1444.     if (closefunc)
  1445.         (*closefunc) (fin);
  1446. }
  1447.  
  1448. store(char *name, char *mode, int unique)
  1449. {
  1450.     FILE *fout, *din;
  1451.     struct stat st;
  1452.     int (*closefunc) ();
  1453.     char *gunique(char *local);
  1454.     time_t start_time = time(NULL);
  1455.  
  1456.     struct aclmember *entry = NULL;
  1457.  
  1458.     int fdout;
  1459.  
  1460. #ifdef OVERWRITE
  1461.     int overwrite = 1;
  1462.  
  1463. #endif /* OVERWRITE */
  1464.  
  1465. #ifdef UPLOAD
  1466.     int open_flags = (O_RDWR | O_CREAT |
  1467.               ((mode && *mode == 'a') ? O_APPEND : O_TRUNC));
  1468.  
  1469.     mode_t oldmask;
  1470.     int f_mode = -1,
  1471.       dir_mode,
  1472.       match_value = -1;
  1473.     uid_t uid;
  1474.     gid_t gid;
  1475.     uid_t oldid;
  1476.     int trunc = O_TRUNC;
  1477.     int valid = 0;
  1478.  
  1479. #endif /* UPLOAD */
  1480.  
  1481.     if (unique && stat(name, &st) == 0 &&
  1482.         (name = gunique(name)) == NULL)
  1483.         return;
  1484.  
  1485.     /*
  1486.      * check the filename, is it legal?
  1487.      */
  1488.     if ( (fn_check(name)) <= 0 )
  1489.         return;
  1490.  
  1491. #ifdef OVERWRITE
  1492.     /* if overwrite permission denied and file exists... then deny the user
  1493.      * permission to write the file. */
  1494.     while (getaclentry("overwrite", &entry) && ARG0 && ARG1 != NULL) {
  1495.         if (type_match(ARG1))
  1496.             if (strcmp(ARG0, "yes") != 0) {
  1497.                 overwrite = 0;
  1498.                 open_flags |= O_EXCL;
  1499.             }
  1500.     }
  1501.  
  1502.     if (!overwrite && !stat(name, &st)) {
  1503.         reply(553, "%s: Permission denied. (Overwrite)", name);
  1504.         return;
  1505.     }
  1506. #endif /* OVERWRITE */
  1507.  
  1508. #ifdef UPLOAD
  1509.     if ( (match_value = upl_check(name, &uid, &gid, &f_mode, &valid)) < 0 )
  1510.         return;
  1511.  
  1512.     /* Only truncate on open if the file is not to be appended to. */
  1513.     if (mode[0] == 'a' || (mode[0] == 'r' && mode[0] == '+') || restart_point)
  1514.         trunc = 0;
  1515.  
  1516.     /* if the user has an explicit new file mode, than open the file using
  1517.      * that mode.  We must take care to not let the umask affect the file
  1518.      * mode.
  1519.      * 
  1520.      * else open the file and let the default umask determine the file mode. */
  1521.     if (f_mode >= 0) {
  1522.         oldmask = umask(0000);
  1523.         fdout = open(name, open_flags, f_mode);
  1524.         umask(oldmask);
  1525.     } else
  1526.         fdout = open(name, open_flags, 0666);
  1527.  
  1528.     if (fdout < 0) {
  1529.         perror_reply(553, name);
  1530.         return;
  1531.     }
  1532.     /* if we have a uid and gid, then use them. */
  1533.  
  1534.     if (valid > 0) {
  1535.         oldid = geteuid();
  1536.         (void) seteuid((uid_t) 0);
  1537.         if ((fchown(fdout, uid, gid)) < 0) {
  1538.             (void) seteuid(oldid);
  1539.             perror_reply(550, "fchown");
  1540.             return;
  1541.         }
  1542.         (void) seteuid(oldid);
  1543.     }
  1544. #endif /* UPLOAD */
  1545.  
  1546.     if (restart_point)
  1547.         mode = "r+w";
  1548.  
  1549. #ifdef UPLOAD
  1550.     fout = fdopen(fdout, mode);
  1551. #else
  1552.     fout = fopen(name, mode);
  1553. #endif /* UPLOAD */
  1554.  
  1555.     closefunc = fclose;
  1556.     if (fout == NULL) {
  1557.         perror_reply(553, name);
  1558.         return;
  1559.     }
  1560.     if (restart_point) {
  1561.         if (type == TYPE_A) {
  1562.             register int i,
  1563.               n,
  1564.               c;
  1565.  
  1566.             n = restart_point;
  1567.             i = 0;
  1568.             while (i++ < n) {
  1569.                 if ((c = getc(fout)) == EOF) {
  1570.                     perror_reply(550, name);
  1571.                     goto done;
  1572.                 }
  1573.                 if (c == '\n')
  1574.                     i++;
  1575.             }
  1576.             /* We must do this seek to "current" position because we are
  1577.              * changing from reading to writing. */
  1578.             if (fseek(fout, 0L, L_INCR) < 0) {
  1579.                 perror_reply(550, name);
  1580.                 goto done;
  1581.             }
  1582.         } else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
  1583.             perror_reply(550, name);
  1584.             goto done;
  1585.         }
  1586.     }
  1587.     din = dataconn(name, (off_t) - 1, "r");
  1588.     if (din == NULL)
  1589.         goto done;
  1590.     if (receive_data(din, fout) == 0) {
  1591.         if (unique)
  1592.             reply(226, "Transfer complete (unique file name:%s).",
  1593.                   name);
  1594.         else
  1595.             reply(226, "Transfer complete.");
  1596.     }
  1597.     (void) fclose(din);
  1598.  
  1599.   dolog:
  1600.     if (log_incoming_xfers && xferlog) {
  1601.         char namebuf[MAXPATHLEN],
  1602.           msg[MAXPATHLEN];
  1603.         int xfertime = time(NULL) - start_time;
  1604.         time_t curtime = time(NULL);
  1605.         int loop;
  1606.  
  1607.         if (!xfertime)
  1608.             xfertime++;
  1609.         realpath(name, namebuf);
  1610.         for (loop = 0; namebuf[loop]; loop++)
  1611.             if (isspace(namebuf[loop]) || iscntrl(namebuf[loop]))
  1612.                 namebuf[loop] = '_';
  1613.         sprintf(msg, "%.24s %d %s %d %s %c %s %c %c %s ftp %d %s\n",
  1614.                 ctime(&curtime),
  1615.                 xfertime,
  1616.                 remotehost,
  1617.                 byte_count,
  1618.                 namebuf,
  1619.                 (type == TYPE_A) ? 'a' : 'b',
  1620.                 opt_string(0),
  1621.                 'i',
  1622.                 anonymous ? 'a' : 'r',
  1623.                 anonymous ? guestpw : pw->pw_name,
  1624.                 authenticated,
  1625.                 authenticated ? authuser : "*"
  1626.             );
  1627.         write(xferlog, msg, strlen(msg));
  1628.     }
  1629.     data = -1;
  1630.     pdata = -1;
  1631.   done:
  1632.     (*closefunc) (fout);
  1633. }
  1634.  
  1635. FILE *
  1636. getdatasock(char *mode)
  1637. {
  1638.     int s,
  1639.       on = 1,
  1640.       tries;
  1641.  
  1642.     if (data >= 0)
  1643.         return (fdopen(data, mode));
  1644.     (void) seteuid((uid_t) 0);
  1645.     s = socket(AF_INET, SOCK_STREAM, 0);
  1646.     if (s < 0)
  1647.         goto bad;
  1648.     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  1649.                    (char *) &on, sizeof(on)) < 0)
  1650.         goto bad;
  1651.     /* anchor socket to avoid multi-homing problems */
  1652.     data_source.sin_family = AF_INET;
  1653.     data_source.sin_addr = ctrl_addr.sin_addr;
  1654.     for (tries = 1;; tries++) {
  1655.         if (bind(s, (struct sockaddr *) &data_source,
  1656.                  sizeof(data_source)) >= 0)
  1657.             break;
  1658.         if (errno != EADDRINUSE || tries > 10)
  1659.             goto bad;
  1660.         sleep(tries);
  1661.     }
  1662. #if defined(M_UNIX) && !defined(_M_UNIX)  /* bug in old TCP/IP release */
  1663.     {
  1664.         struct linger li;
  1665.         li.l_onoff = 1;
  1666.         li.l_linger = 900;
  1667.         if (setsockopt(s, SOL_SOCKET, SO_LINGER,
  1668.           (char *)&li, sizeof(struct linger)) < 0) {
  1669.             syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
  1670.             goto bad;
  1671.         }
  1672.     }
  1673. #endif
  1674.     (void) seteuid((uid_t) pw->pw_uid);
  1675.  
  1676. #ifdef IPTOS_THROUGHPUT
  1677.     on = IPTOS_THROUGHPUT;
  1678.     if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *) &on, sizeof(int)) < 0)
  1679.           syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
  1680. #endif
  1681.  
  1682.     return (fdopen(s, mode));
  1683.   bad:
  1684.     (void) seteuid((uid_t) pw->pw_uid);
  1685.     (void) close(s);
  1686.     return (NULL);
  1687. }
  1688.  
  1689. FILE *
  1690. dataconn(char *name, off_t size, char *mode)
  1691. {
  1692.     char sizebuf[32];
  1693.     FILE *file;
  1694.     int retry = 0;
  1695. #ifdef IPTOS_LOWDELAY
  1696.     int tos;
  1697. #endif
  1698.  
  1699.     file_size = size;
  1700.     byte_count = 0;
  1701.     if (size != (off_t) - 1)
  1702.         (void) sprintf(sizebuf, " (%ld bytes)", size);
  1703.     else
  1704.         (void) strcpy(sizebuf, "");
  1705.     if (pdata >= 0) {
  1706.         struct sockaddr_in from;
  1707.         int s,
  1708.           fromlen = sizeof(from);
  1709.  
  1710.         s = accept(pdata, (struct sockaddr *) &from, &fromlen);
  1711.         if (s < 0) {
  1712.             reply(425, "Can't open data connection.");
  1713.             (void) close(pdata);
  1714.             pdata = -1;
  1715.             return (NULL);
  1716.         }
  1717.         (void) close(pdata);
  1718.         pdata = s;
  1719. #ifdef IPTOS_LOWDELAY
  1720.         tos = IPTOS_LOWDELAY;
  1721.         (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *) &tos,
  1722.                           sizeof(int));
  1723.  
  1724. #endif
  1725.         reply(150, "Opening %s mode data connection for %s%s.",
  1726.               type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
  1727.         return (fdopen(pdata, mode));
  1728.     }
  1729.     if (data >= 0) {
  1730.         reply(125, "Using existing data connection for %s%s.",
  1731.               name, sizebuf);
  1732.         usedefault = 1;
  1733.         return (fdopen(data, mode));
  1734.     }
  1735.     if (usedefault)
  1736.         data_dest = his_addr;
  1737.     usedefault = 1;
  1738.     file = getdatasock(mode);
  1739.     if (file == NULL) {
  1740.         reply(425, "Can't create data socket (%s,%d): %s.",
  1741.               inet_ntoa(data_source.sin_addr),
  1742.               ntohs(data_source.sin_port), strerror(errno));
  1743.         return (NULL);
  1744.     }
  1745.     data = fileno(file);
  1746.     while (connect(data, (struct sockaddr *) &data_dest,
  1747.                    sizeof(data_dest)) < 0) {
  1748.         if ((errno == EADDRINUSE || errno == EINTR) && retry < swaitmax) {
  1749.             sleep((unsigned) swaitint);
  1750.             retry += swaitint;
  1751.             continue;
  1752.         }
  1753.         perror_reply(425, "Can't build data connection");
  1754.         (void) fclose(file);
  1755.         data = -1;
  1756.         return (NULL);
  1757.     }
  1758.     reply(150, "Opening %s mode data connection for %s%s.",
  1759.           type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
  1760.     return (file);
  1761. }
  1762.  
  1763. /* Tranfer the contents of "instr" to "outstr" peer using the appropriate
  1764.  * encapsulation of the data subject to Mode, Structure, and Type.
  1765.  *
  1766.  * NB: Form isn't handled. */
  1767. send_data(FILE *instr, FILE *outstr, off_t blksize)
  1768. {
  1769.     register int c,
  1770.       cnt;
  1771.     register char *buf;
  1772.     int netfd,
  1773.       filefd;
  1774.  
  1775.     transflag++;
  1776.     if (setjmp(urgcatch)) {
  1777.         transflag = 0;
  1778.         return;
  1779.     }
  1780.     switch (type) {
  1781.  
  1782.     case TYPE_A:
  1783.         while ((c = getc(instr)) != EOF) {
  1784.             byte_count++;
  1785.             if (c == '\n') {
  1786.                 if (ferror(outstr))
  1787.                     goto data_err;
  1788.                 (void) putc('\r', outstr);
  1789.             }
  1790.             (void) putc(c, outstr);
  1791.         }
  1792.         fflush(outstr);
  1793.         transflag = 0;
  1794.         if (ferror(instr))
  1795.             goto file_err;
  1796.         if (ferror(outstr))
  1797.             goto data_err;
  1798.         reply(226, "Transfer complete.");
  1799.         return;
  1800.  
  1801.     case TYPE_I:
  1802.     case TYPE_L:
  1803.         if ((buf = (char *) malloc((u_int) blksize)) == NULL) {
  1804.             transflag = 0;
  1805.             perror_reply(451, "Local resource failure: malloc");
  1806.             return;
  1807.         }
  1808.         netfd = fileno(outstr);
  1809.         filefd = fileno(instr);
  1810.         while ((cnt = read(filefd, buf, (u_int) blksize)) > 0 &&
  1811.                write(netfd, buf, cnt) == cnt)
  1812.             byte_count += cnt;
  1813.         transflag = 0;
  1814.         (void) free(buf);
  1815.         if (cnt != 0) {
  1816.             if (cnt < 0)
  1817.                 goto file_err;
  1818.             goto data_err;
  1819.         }
  1820.         reply(226, "Transfer complete.");
  1821.         return;
  1822.     default:
  1823.         transflag = 0;
  1824.         reply(550, "Unimplemented TYPE %d in send_data", type);
  1825.         return;
  1826.     }
  1827.  
  1828.   data_err:
  1829.     transflag = 0;
  1830.     perror_reply(426, "Data connection");
  1831.     return;
  1832.  
  1833.   file_err:
  1834.     transflag = 0;
  1835.     perror_reply(551, "Error on input file");
  1836. }
  1837.  
  1838. /* Transfer data from peer to "outstr" using the appropriate encapulation of
  1839.  * the data subject to Mode, Structure, and Type.
  1840.  *
  1841.  * N.B.: Form isn't handled. */
  1842. receive_data(FILE *instr, FILE *outstr)
  1843. {
  1844.     register int c;
  1845.     int cnt,
  1846.       bare_lfs = 0;
  1847.     char buf[BUFSIZ];
  1848.  
  1849.     transflag++;
  1850.     if (setjmp(urgcatch)) {
  1851.         transflag = 0;
  1852.         return (-1);
  1853.     }
  1854.     switch (type) {
  1855.  
  1856.     case TYPE_I:
  1857.     case TYPE_L:
  1858.         while ((cnt = read(fileno(instr), buf, sizeof buf)) > 0) {
  1859.             if (write(fileno(outstr), buf, cnt) != cnt)
  1860.                 goto file_err;
  1861.             byte_count += cnt;
  1862.         }
  1863.         if (cnt < 0)
  1864.             goto data_err;
  1865.         transflag = 0;
  1866.         return (0);
  1867.  
  1868.     case TYPE_E:
  1869.         reply(553, "TYPE E not implemented.");
  1870.         transflag = 0;
  1871.         return (-1);
  1872.  
  1873.     case TYPE_A:
  1874.         while ((c = getc(instr)) != EOF) {
  1875.             byte_count++;
  1876.             if (c == '\n')
  1877.                 bare_lfs++;
  1878.             while (c == '\r') {
  1879.                 if (ferror(outstr))
  1880.                     goto data_err;
  1881.                 if ((c = getc(instr)) != '\n') {
  1882.                     (void) putc('\r', outstr);
  1883.                     if (c == '\0' || c == EOF)
  1884.                         goto contin2;
  1885.                 }
  1886.             }
  1887.             (void) putc(c, outstr);
  1888.           contin2:;
  1889.         }
  1890.         fflush(outstr);
  1891.         if (ferror(instr))
  1892.             goto data_err;
  1893.         if (ferror(outstr))
  1894.             goto file_err;
  1895.         transflag = 0;
  1896.         if (bare_lfs) {
  1897.             lreply(226, "WARNING! %d bare linefeeds received in ASCII mode", bare_lfs);
  1898.             printf("   File may not have transferred correctly.\r\n");
  1899.         }
  1900.         return (0);
  1901.     default:
  1902.         reply(550, "Unimplemented TYPE %d in receive_data", type);
  1903.         transflag = 0;
  1904.         return (-1);
  1905.     }
  1906.  
  1907.   data_err:
  1908.     transflag = 0;
  1909.     perror_reply(426, "Data Connection");
  1910.     return (-1);
  1911.  
  1912.   file_err:
  1913.     transflag = 0;
  1914.     perror_reply(452, "Error writing file");
  1915.     return (-1);
  1916. }
  1917.  
  1918. statfilecmd(char *filename)
  1919. {
  1920.     char line[BUFSIZ];
  1921.     FILE *fin;
  1922.     int c;
  1923.  
  1924.     if (anonymous && dolreplies)
  1925.         (void) sprintf(line, ls_long, filename);
  1926.     else
  1927.         (void) sprintf(line, ls_short, filename);
  1928.     fin = ftpd_popen(line, "r", 0);
  1929.     lreply(211, "status of %s:", filename);
  1930.     while ((c = getc(fin)) != EOF) {
  1931.         if (c == '\n') {
  1932.             if (ferror(stdout)) {
  1933.                 perror_reply(421, "control connection");
  1934.                 (void) ftpd_pclose(fin);
  1935.                 dologout(1);
  1936.                 /* NOTREACHED */
  1937.             }
  1938.             if (ferror(fin)) {
  1939.                 perror_reply(551, filename);
  1940.                 (void) ftpd_pclose(fin);
  1941.                 return;
  1942.             }
  1943.             (void) putc('\r', stdout);
  1944.         }
  1945.         (void) putc(c, stdout);
  1946.     }
  1947.     (void) ftpd_pclose(fin);
  1948.     reply(211, "End of Status");
  1949. }
  1950.  
  1951. statcmd(void)
  1952. {
  1953.     struct sockaddr_in *sin;
  1954.     u_char *a,
  1955.      *p;
  1956.  
  1957.     lreply(211, "%s FTP server status:", hostname);
  1958.     printf("     %s\r\n", version);
  1959.     printf("     Connected to %s", remotehost);
  1960.     if (!isdigit(remotehost[0]))
  1961.         printf(" (%s)", inet_ntoa(his_addr.sin_addr));
  1962.     printf("\r\n");
  1963.     if (logged_in) {
  1964.         if (anonymous)
  1965.             printf("     Logged in anonymously\r\n");
  1966.         else
  1967.             printf("     Logged in as %s\r\n", pw->pw_name);
  1968.     } else if (askpasswd)
  1969.         printf("     Waiting for password\r\n");
  1970.     else
  1971.         printf("     Waiting for user name\r\n");
  1972.     printf("     TYPE: %s", typenames[type]);
  1973.     if (type == TYPE_A || type == TYPE_E)
  1974.         printf(", FORM: %s", formnames[form]);
  1975.     if (type == TYPE_L)
  1976. #if NBBY == 8
  1977.         printf(" %d", NBBY);
  1978. #else
  1979.         printf(" %d", bytesize);/* need definition! */
  1980. #endif
  1981.     printf("; STRUcture: %s; transfer MODE: %s\r\n",
  1982.            strunames[stru], modenames[mode]);
  1983.     if (data != -1)
  1984.         printf("     Data connection open\r\n");
  1985.     else if (pdata != -1) {
  1986.         printf("     in Passive mode");
  1987.         sin = &pasv_addr;
  1988.         goto printaddr;
  1989.     } else if (usedefault == 0) {
  1990.         printf("     PORT");
  1991.         sin = &data_dest;
  1992.       printaddr:
  1993.         a = (u_char *) & sin->sin_addr;
  1994.         p = (u_char *) & sin->sin_port;
  1995. #define UC(b) (((int) b) & 0xff)
  1996.         printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]),
  1997.                UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
  1998. #undef UC
  1999.     } else
  2000.         printf("     No data connection\r\n");
  2001.     reply(211, "End of status");
  2002. }
  2003.  
  2004. fatal(char *s)
  2005. {
  2006.     reply(451, "Error in server: %s\n", s);
  2007.     reply(221, "Closing connection due to server error.");
  2008.     dologout(0);
  2009.     /* NOTREACHED */
  2010. }
  2011.  
  2012. #if defined (HAVE_VPRINTF)
  2013. /* VARARGS2 */
  2014. reply(va_alist)
  2015.   va_dcl
  2016. {
  2017.     int n;
  2018.     char *fmt;
  2019.     va_list ap;
  2020.     
  2021.     va_start(ap);
  2022.     /* first argument is always the return code */
  2023.     n = va_arg(ap, int);
  2024.     /* second argument is always fmt string     */
  2025.     fmt = va_arg(ap, char *);
  2026.  
  2027.     if (autospout != NULL) {
  2028.         char *ptr = autospout;
  2029.  
  2030.         printf("%d-", n);
  2031.         while (*ptr) {
  2032.             if (*ptr == '\n') {
  2033.                 fputs("\r\n", stdout);
  2034.                 if (*(++ptr))
  2035.                     printf("%03d-", n);
  2036.             } else {
  2037.                 putchar(*ptr++);
  2038.             }
  2039.         }
  2040.         if (*(--ptr) != '\n')
  2041.             printf("\r\n");
  2042.         if (autospout_free) {
  2043.             (void) free(autospout);
  2044.             autospout_free = 0;
  2045.         }
  2046.         autospout = 0;
  2047.     }
  2048.     printf("%d ", n);
  2049.     vprintf(fmt, ap);
  2050.     printf("\r\n");
  2051.     (void) fflush(stdout);
  2052.  
  2053.     if (debug) {
  2054.         char buf[BUFSIZ];
  2055.         (void) vsprintf(buf, fmt, ap);
  2056.  
  2057.         syslog(LOG_DEBUG, "<--- %d ", n);
  2058.         syslog(LOG_DEBUG, buf);
  2059.     }
  2060.  
  2061.     va_end(ap);
  2062. }
  2063.  
  2064. /* VARARGS2 */
  2065. lreply(va_alist)
  2066.   va_dcl
  2067. {
  2068.     va_list ap;
  2069.     int n;
  2070.     char *fmt;
  2071.  
  2072.     va_start(ap);
  2073.     /* first argument is always the return code */
  2074.     n = va_arg(ap, int);
  2075.     /* second argument is always fmt string     */
  2076.     fmt = va_arg(ap, char *);
  2077.  
  2078.     if (!dolreplies)
  2079.         return;
  2080.     printf("%d-", n);
  2081.     vprintf(fmt, ap);
  2082.     printf("\r\n");
  2083.     (void) fflush(stdout);
  2084.  
  2085.     if (debug) {
  2086.         char buf[BUFSIZ];
  2087.         (void) vsprintf(buf, fmt, ap);
  2088.  
  2089.         syslog(LOG_DEBUG, "<--- %d- ", n);
  2090.         syslog(LOG_DEBUG, buf);
  2091.     }
  2092.  
  2093.     va_end(ap);
  2094. }
  2095.  
  2096. #else
  2097. /* VARARGS2 */
  2098. reply(int n, char *fmt, int p0, int p1, int p2, int p3, int p4, int p5)
  2099. {
  2100.     if (autospout != NULL) {
  2101.         char *ptr = autospout;
  2102.  
  2103.         printf("%d-", n);
  2104.         while (*ptr) {
  2105.             if (*ptr == '\n') {
  2106.                 printf("\r\n");
  2107.                 if (*(++ptr))
  2108.                     printf("%d-", n);
  2109.             } else {
  2110.                 putchar(*ptr++);
  2111.             }
  2112.         }
  2113.         if (*(--ptr) != '\n')
  2114.             printf("\r\n");
  2115.         if (autospout_free) {
  2116.             (void) free(autospout);
  2117.             autospout_free = 0;
  2118.         }
  2119.         autospout = 0;
  2120.     }
  2121.     printf("%d ", n);
  2122.     printf(fmt, p0, p1, p2, p3, p4, p5);
  2123.     printf("\r\n");
  2124.     (void) fflush(stdout);
  2125.     if (debug) {
  2126.         syslog(LOG_DEBUG, "<--- %d ", n);
  2127.         syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
  2128.     }
  2129. }
  2130.  
  2131. /* VARARGS2 */
  2132. lreply(int n, char *fmt, int p0, int p1, int p2, int p3, int p4, int p5)
  2133. {
  2134.     if (!dolreplies)
  2135.         return;
  2136.     printf("%d-", n);
  2137.     printf(fmt, p0, p1, p2, p3, p4, p5);
  2138.     printf("\r\n");
  2139.     (void) fflush(stdout);
  2140.     if (debug) {
  2141.         syslog(LOG_DEBUG, "<--- %d- ", n);
  2142.         syslog(LOG_DEBUG, fmt, p0, p1, p2, p3, p4, p5);
  2143.     }
  2144. }
  2145. #endif
  2146.  
  2147. ack(char *s)
  2148. {
  2149.     reply(250, "%s command successful.", s);
  2150. }
  2151.  
  2152. nack(char *s)
  2153. {
  2154.     reply(502, "%s command not implemented.", s);
  2155. }
  2156.  
  2157. /* ARGSUSED */
  2158. yyerror(char *s)
  2159. {
  2160.     char *cp;
  2161.  
  2162.     if ((cp = strchr(cbuf, '\n')) != NULL)
  2163.         *cp = '\0';
  2164.     reply(500, "'%s': command not understood.", cbuf);
  2165. }
  2166.  
  2167. delete(char *name)
  2168. {
  2169.     struct stat st;
  2170.  
  2171.     /*
  2172.      * delete permission?
  2173.      */
  2174.  
  2175.     if ( (del_check(name)) == 0 )
  2176.         return;
  2177.  
  2178.     if (lstat(name, &st) < 0) {
  2179.         perror_reply(550, name);
  2180.         return;
  2181.     }
  2182.     if ((st.st_mode & S_IFMT) == S_IFDIR) {
  2183.         if (rmdir(name) < 0) {
  2184.             perror_reply(550, name);
  2185.             return;
  2186.         }
  2187.         goto done;
  2188.     }
  2189.     if (unlink(name) < 0) {
  2190.         perror_reply(550, name);
  2191.         return;
  2192.     }
  2193.   done:
  2194.     {
  2195.         char path[MAXPATHLEN];
  2196.  
  2197.         realpath(name, path);
  2198.  
  2199.         if (anonymous) {
  2200.             syslog(LOG_NOTICE, "%s of %s [%s] deleted %s", guestpw, remotehost,
  2201.                    remoteaddr, path);
  2202.         } else {
  2203.             syslog(LOG_NOTICE, "%s of %s [%s] deleted %s", pw->pw_name,
  2204.                    remotehost, remoteaddr, path);
  2205.         }
  2206.     }
  2207.  
  2208.     ack("DELE");
  2209. }
  2210.  
  2211. cwd(char *path)
  2212. {
  2213.     struct aclmember *entry = NULL;
  2214.     char cdpath[MAXPATHLEN + 1];
  2215.  
  2216.     if (chdir(path) < 0) {
  2217.         /* alias checking */
  2218.         while (getaclentry("alias", &entry) && ARG0 && ARG1 != NULL) {
  2219.             if (!strcasecmp(ARG0, path)) {
  2220.                 if (chdir(ARG1) < 0)
  2221.                     perror_reply(550, path);
  2222.                 else {
  2223.                     show_message(250, C_WD);
  2224.                     show_readme(250, C_WD);
  2225.                     ack("CWD");
  2226.                 }
  2227.                 return;
  2228.             }
  2229.         }
  2230.     /* check for "cdpath" directories. */
  2231.     entry = (struct aclmember *) NULL;
  2232.         while (getaclentry("cdpath", &entry) && ARG0 != NULL) {
  2233.         strcpy(cdpath,ARG0);
  2234.         strcat(cdpath,"/");
  2235.         strcat(cdpath,path);
  2236.             if (chdir(cdpath) >= 0) {
  2237.                 show_message(250, C_WD);
  2238.                 show_readme(250, C_WD);
  2239.                 ack("CWD");
  2240.                 return;
  2241.             }
  2242.         }
  2243.         perror_reply(550,path);
  2244.     } else {
  2245.         show_message(250, C_WD);
  2246.         show_readme(250, C_WD);
  2247.         ack("CWD");
  2248.     }
  2249. }
  2250.  
  2251. makedir(char *name)
  2252. {
  2253.     uid_t uid;
  2254.     gid_t gid;
  2255.     int   valid = 0;
  2256.  
  2257.     /*
  2258.      * check the directory, can we mkdir here?
  2259.      */
  2260.     if ( (dir_check(name, &uid, &gid, &valid)) <= 0 )
  2261.         return;
  2262.  
  2263.     /*
  2264.      * check the filename, is it legal?
  2265.      */
  2266.     if ( (fn_check(name)) <= 0 )
  2267.         return;
  2268.  
  2269.     if (mkdir(name, 0777) < 0) {
  2270.         perror_reply(550, name);
  2271.     return;
  2272.     }
  2273.  
  2274.     reply(257, "MKD command successful.");
  2275. }
  2276.  
  2277. removedir(char *name)
  2278. {
  2279.     int c, d;  /* dummy variables */
  2280.         int valid = 0;
  2281.  
  2282.     /*
  2283.      * check the directory, can we rmdir here?
  2284.      */
  2285.     if ( (dir_check(name, &c, &d, &valid)) <= 0 )
  2286.         return;
  2287.  
  2288.     /*
  2289.      * delete permission?
  2290.      */
  2291.  
  2292.     if ( (del_check(name)) == 0 )
  2293.         return;
  2294.  
  2295.     if (rmdir(name) < 0)
  2296.         perror_reply(550, name);
  2297.     else
  2298.         ack("RMD");
  2299. }
  2300.  
  2301. pwd(void)
  2302. {
  2303.     char path[MAXPATHLEN + 1];
  2304. #ifdef HAVE_GETCWD
  2305.     extern char *getcwd();
  2306. #else
  2307.     extern char *getwd(char *);
  2308. #endif
  2309.  
  2310. #ifdef HAVE_GETCWD
  2311.     if (getcwd(path,MAXPATHLEN) == (char *) NULL)
  2312. #else
  2313.     if (getwd(path) == (char *) NULL)
  2314. #endif
  2315.         reply(550, "%s.", path);
  2316.     else
  2317.         reply(257, "\"%s\" is current directory.", path);
  2318. }
  2319.  
  2320. char *
  2321. renamefrom(char *name)
  2322. {
  2323.     struct stat st;
  2324.  
  2325.     if (lstat(name, &st) < 0) {
  2326.         perror_reply(550, name);
  2327.         return ((char *) 0);
  2328.     }
  2329.  
  2330.     /* if rename permission denied and file exists... then deny the user
  2331.      * permission to rename the file. 
  2332.      */
  2333.     while (getaclentry("rename", &entry) && ARG0 && ARG1 != NULL) {
  2334.         if (type_match(ARG1))
  2335.             if (strcmp(ARG0, "yes")) {
  2336.                 reply(553, "%s: Permission denied. (rename)", name);
  2337.                 return ((char *) 0);
  2338.             }
  2339.     }
  2340.  
  2341.     reply(350, "File exists, ready for destination name");
  2342.     return (name);
  2343. }
  2344.  
  2345. renamecmd(char *from, char *to)
  2346. {
  2347.  
  2348.     /*
  2349.      * check the filename, is it legal?
  2350.      */
  2351.     if ( (fn_check(to)) == 0 )
  2352.         return;
  2353.  
  2354.     if (rename(from, to) < 0)
  2355.         perror_reply(550, "rename");
  2356.     else
  2357.         ack("RNTO");
  2358. }
  2359.  
  2360. dolog(struct sockaddr_in *sin)
  2361. {
  2362.     struct hostent *hp;
  2363.     char *blah;
  2364.  
  2365. #ifdef    DNS_TRYAGAIN
  2366.     int num_dns_tries = 0;
  2367.     /*
  2368.      * 27-Apr-93    EHK/BM
  2369.      * far away connections might take some time to get their IP address
  2370.      * resolved. That's why we try again -- maybe our DNS cache has the
  2371.      * PTR-RR now. This code is sloppy. Far better is to check what the
  2372.      * resolver returned so that in case of error, there's no need to
  2373.      * try again.
  2374.      */
  2375. dns_again:
  2376.      hp = gethostbyaddr((char *) &sin->sin_addr,
  2377.                                 sizeof (struct in_addr), AF_INET);
  2378.  
  2379.      if ( !hp && ++num_dns_tries <= 1 ) {
  2380.         sleep(3);
  2381.         goto dns_again;         /* try DNS lookup once more     */
  2382.      }
  2383. #else
  2384.     hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET);
  2385. #endif
  2386.  
  2387.     blah = inet_ntoa(sin->sin_addr);
  2388.  
  2389.     (void) strncpy(remoteaddr, blah, sizeof(remoteaddr));
  2390.  
  2391.     if (!strcmp(remoteaddr, "0.0.0.0")) {
  2392.         nameserved = 1;
  2393.         strncpy(remotehost, "localhost", sizeof(remotehost));
  2394.     } else {
  2395.         if (hp) {
  2396.             nameserved = 1;
  2397.             (void) strncpy(remotehost, hp->h_name, sizeof(remotehost));
  2398.         } else {
  2399.             nameserved = 0;
  2400.             (void) strncpy(remotehost, remoteaddr, sizeof(remotehost));
  2401.         }
  2402.     }
  2403.  
  2404. #ifdef SETPROCTITLE
  2405.     sprintf(proctitle, "%s: connected", remotehost);
  2406.     setproctitle(proctitle);
  2407. #endif /* SETPROCTITLE */
  2408.  
  2409.     if (logging)
  2410.         syslog(LOG_INFO, "connection from %s [%s]", remotehost,
  2411.                remoteaddr);
  2412. }
  2413.  
  2414. /* Record logout in wtmp file and exit with supplied status. */
  2415. dologout(int status)
  2416. {
  2417.     if (logged_in) {
  2418.         (void) seteuid((uid_t) 0);
  2419.         logwtmp(ttyline, "", "");
  2420.     }
  2421.     syslog(LOG_INFO, "FTP session closed");
  2422.     if (xferlog)
  2423.         close(xferlog);
  2424.     acl_remove();
  2425.     /* beware of flushing buffers after a SIGPIPE */
  2426.     _exit(status);
  2427. }
  2428.  
  2429. SIGNAL_TYPE
  2430. myoob(int sig)
  2431. {
  2432.     char *cp;
  2433.  
  2434.     /* only process if transfer occurring */
  2435.     if (!transflag)
  2436.         return;
  2437.     cp = tmpline;
  2438.     if (getline(cp, 7, stdin) == NULL) {
  2439.         reply(221, "You could at least say goodbye.");
  2440.         dologout(0);
  2441.     }
  2442.     upper(cp);
  2443.     if (strcmp(cp, "ABOR\r\n") == 0) {
  2444.         tmpline[0] = '\0';
  2445.         reply(426, "Transfer aborted. Data connection closed.");
  2446.         reply(226, "Abort successful");
  2447.         longjmp(urgcatch, 1);
  2448.     }
  2449.     if (strcmp(cp, "STAT\r\n") == 0) {
  2450.         if (file_size != (off_t) - 1)
  2451.             reply(213, "Status: %lu of %lu bytes transferred",
  2452.                   byte_count, file_size);
  2453.         else
  2454.             reply(213, "Status: %lu bytes transferred", byte_count);
  2455.     }
  2456. }
  2457.  
  2458. /* Note: a response of 425 is not mentioned as a possible response to the
  2459.  * PASV command in RFC959. However, it has been blessed as a legitimate
  2460.  * response by Jon Postel in a telephone conversation with Rick Adams on 25
  2461.  * Jan 89. */
  2462. passive(void)
  2463. {
  2464.     int len;
  2465.     register char *p,
  2466.      *a;
  2467.  
  2468.     pdata = socket(AF_INET, SOCK_STREAM, 0);
  2469.     if (pdata < 0) {
  2470.         perror_reply(425, "Can't open passive connection");
  2471.         return;
  2472.     }
  2473.     pasv_addr = ctrl_addr;
  2474.     pasv_addr.sin_port = 0;
  2475.     (void) seteuid((uid_t) 0);
  2476.     if (bind(pdata, (struct sockaddr *) &pasv_addr, sizeof(pasv_addr)) < 0) {
  2477.         (void) seteuid((uid_t) pw->pw_uid);
  2478.         goto pasv_error;
  2479.     }
  2480.     (void) seteuid((uid_t) pw->pw_uid);
  2481.     len = sizeof(pasv_addr);
  2482.     if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
  2483.         goto pasv_error;
  2484.     if (listen(pdata, 1) < 0)
  2485.         goto pasv_error;
  2486.     a = (char *) &pasv_addr.sin_addr;
  2487.     p = (char *) &pasv_addr.sin_port;
  2488.  
  2489. #define UC(b) (((int) b) & 0xff)
  2490.  
  2491.     reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
  2492.           UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
  2493.     return;
  2494.  
  2495.   pasv_error:
  2496.     (void) close(pdata);
  2497.     pdata = -1;
  2498.     perror_reply(425, "Can't open passive connection");
  2499.     return;
  2500. }
  2501.  
  2502. /* Generate unique name for file with basename "local". The file named
  2503.  * "local" is already known to exist. Generates failure reply on error. */
  2504. char *
  2505. gunique(char *local)
  2506. {
  2507.     static char new[MAXPATHLEN];
  2508.     struct stat st;
  2509.     char *cp = strrchr(local, '/');
  2510.     int count = 0;
  2511.  
  2512.     if (cp)
  2513.         *cp = '\0';
  2514.     if (stat(cp ? local : ".", &st) < 0) {
  2515.         perror_reply(553, cp ? local : ".");
  2516.         return ((char *) 0);
  2517.     }
  2518.     if (cp)
  2519.         *cp = '/';
  2520.     (void) strcpy(new, local);
  2521.     cp = new + strlen(new);
  2522.     *cp++ = '.';
  2523.     for (count = 1; count < 100; count++) {
  2524.         (void) sprintf(cp, "%d", count);
  2525.         if (stat(new, &st) < 0)
  2526.             return (new);
  2527.     }
  2528.     reply(452, "Unique file name cannot be created.");
  2529.     return ((char *) 0);
  2530. }
  2531.  
  2532. /* Format and send reply containing system error number. */
  2533. perror_reply(int code, char *string)
  2534. {
  2535.     reply(code, "%s: %s.", string, strerror(errno));
  2536. }
  2537.  
  2538. static char *onefile[] =
  2539. {"", 0};
  2540.  
  2541. send_file_list(char *whichfiles)
  2542. {
  2543.     struct stat st;
  2544.     DIR *dirp = NULL;
  2545.  
  2546. #ifdef HAVE_DIRENT
  2547.     struct dirent *dir;
  2548. #else
  2549.     struct direct *dir;
  2550. #endif
  2551.  
  2552.     FILE *dout = NULL;
  2553.     register char **dirlist,
  2554.      *dirname;
  2555.     int simple = 0;
  2556.     char *strpbrk(const char *, const char *);
  2557.  
  2558.     if (strpbrk(whichfiles, "~{[*?") != NULL) {
  2559.         extern char **ftpglob(register char *v),
  2560.          *globerr;
  2561.  
  2562.         globerr = NULL;
  2563.         dirlist = ftpglob(whichfiles);
  2564.         if (globerr != NULL) {
  2565.             reply(550, globerr);
  2566.             return;
  2567.         } else if (dirlist == NULL) {
  2568.             errno = ENOENT;
  2569.             perror_reply(550, whichfiles);
  2570.             return;
  2571.         }
  2572.     } else {
  2573.         onefile[0] = whichfiles;
  2574.         dirlist = onefile;
  2575.         simple = 1;
  2576.     }
  2577.  
  2578.     if (setjmp(urgcatch)) {
  2579.         transflag = 0;
  2580.         return;
  2581.     }
  2582.     while ((dirname = *dirlist++) != NULL) {
  2583.         if (stat(dirname, &st) < 0) {
  2584.             /* If user typed "ls -l", etc, and the client used NLST, do what
  2585.              * the user meant. */
  2586.             if (dirname[0] == '-' && *dirlist == NULL && transflag == 0) {
  2587.                 retrieve("/bin/ls %s", dirname);
  2588.                 return;
  2589.             }
  2590.             perror_reply(550, whichfiles);
  2591.             if (dout != NULL) {
  2592.                 (void) fclose(dout);
  2593.                 transflag = 0;
  2594.                 data = -1;
  2595.                 pdata = -1;
  2596.             }
  2597.             return;
  2598.         }
  2599.         if ((st.st_mode & S_IFMT) == S_IFREG) {
  2600.             if (dout == NULL) {
  2601.                 dout = dataconn("file list", (off_t) - 1, "w");
  2602.                 if (dout == NULL)
  2603.                     return;
  2604.                 transflag++;
  2605.             }
  2606.             fprintf(dout, "%s%s\n", dirname,
  2607.                     type == TYPE_A ? "\r" : "");
  2608.             byte_count += strlen(dirname) + 1;
  2609.             continue;
  2610.         } else if ((st.st_mode & S_IFMT) != S_IFDIR)
  2611.             continue;
  2612.  
  2613.         if ((dirp = opendir(dirname)) == NULL)
  2614.             continue;
  2615.  
  2616.         while ((dir = readdir(dirp)) != NULL) {
  2617.             char nbuf[MAXPATHLEN];
  2618.  
  2619. #ifndef HAVE_DIRENT    /* does not have d_namlen */
  2620.             if (dir->d_name[0] == '.' && dir->d_namlen == 1)
  2621. #else
  2622.             if (dir->d_name[0] == '.' && (strlen(dir->d_name) == 1))
  2623. #endif
  2624.                 continue;
  2625. #ifndef HAVE_DIRENT    /* does not have d_namlen */
  2626.             if (dir->d_namlen == 2 && dir->d_name[0] == '.' &&
  2627.                 dir->d_name[1] == '.')
  2628. #else
  2629.             if ((strlen(dir->d_name) == 2) && dir->d_name[0] == '.' &&
  2630.                 dir->d_name[1] == '.')
  2631. #endif
  2632.                 continue;
  2633.  
  2634.             sprintf(nbuf, "%s/%s", dirname, dir->d_name);
  2635.  
  2636.             /* We have to do a stat to insure it's not a directory or special
  2637.              * file. */
  2638.             if (simple || (stat(nbuf, &st) == 0 &&
  2639.                            (st.st_mode & S_IFMT) == S_IFREG)) {
  2640.                 if (dout == NULL) {
  2641.                     dout = dataconn("file list", (off_t) - 1,
  2642.                                     "w");
  2643.                     if (dout == NULL)
  2644.                         return;
  2645.                     transflag++;
  2646.                 }
  2647.                 if (nbuf[0] == '.' && nbuf[1] == '/')
  2648.                     fprintf(dout, "%s%s\n", &nbuf[2],
  2649.                             type == TYPE_A ? "\r" : "");
  2650.                 else
  2651.                     fprintf(dout, "%s%s\n", nbuf,
  2652.                             type == TYPE_A ? "\r" : "");
  2653.                 byte_count += strlen(nbuf) + 1;
  2654.             }
  2655.         }
  2656.         (void) closedir(dirp);
  2657.     }
  2658.  
  2659.     if (dout == NULL)
  2660.         reply(550, "No files found.");
  2661.     else if (ferror(dout) != 0)
  2662.         perror_reply(550, "Data connection");
  2663.     else
  2664.         reply(226, "Transfer complete.");
  2665.  
  2666.     transflag = 0;
  2667.     if (dout != NULL)
  2668.         (void) fclose(dout);
  2669.     data = -1;
  2670.     pdata = -1;
  2671. }
  2672.  
  2673. #ifdef SETPROCTITLE
  2674. # ifndef setproctitle /* because of SCO, we use SCOproctitle instead */
  2675. /* clobber argv so ps will show what we're doing. (stolen from sendmail)
  2676.  * warning, since this is usually started from inetd.conf, it often doesn't
  2677.  * have much of an environment or arglist to overwrite. */
  2678.  
  2679. #ifdef HAVE_PSTAT
  2680. #include <sys/pstat.h>
  2681. #endif
  2682.  
  2683. /* VARARGS2 */
  2684. setproctitle(va_alist)
  2685.   va_dcl
  2686. {
  2687.     va_list ap;
  2688.     char *fmt;
  2689.  
  2690.     register char *p,
  2691.      *bp,
  2692.       ch;
  2693.     register int i;
  2694.     char buf[BUFSIZ];
  2695. #ifdef HAVE_PSTAT
  2696.     union pstun un;
  2697. #endif
  2698.  
  2699.     va_start(ap);
  2700.     /* first argument is always the fmt string */
  2701.     fmt = va_arg(ap, char *);
  2702.  
  2703.     (void) vsprintf(buf, fmt, ap);
  2704.     va_end(ap);
  2705.  
  2706.     /* make ps print our process name */
  2707.     p = Argv[0];
  2708.     *p++ = '-';
  2709.  
  2710.     i = strlen(buf);
  2711. #ifdef HAVE_PSTAT
  2712.     un.pst_command = buf;
  2713.     pstat(PSTAT_SETCMD, un, i, 0, 0);
  2714. #else
  2715.     if (i > LastArgv - p - 2) {
  2716.         i = LastArgv - p - 2;
  2717.         buf[i] = '\0';
  2718.     }
  2719.     bp = buf;
  2720.     while ((ch = *bp++) != (char) NULL)
  2721.         if (ch != '\n' && ch != '\r')
  2722.             *p++ = ch;
  2723.     while (p < LastArgv)
  2724.         *p++ = ' ';
  2725. #endif
  2726. }
  2727. #endif /* setproctitle */
  2728. #endif /* SETPROCTITLE */
  2729.  
  2730. #ifdef KERBEROS
  2731. /* thanks to gshapiro@wpi.wpi.edu for the following kerberosities */
  2732.  
  2733. void
  2734. init_krb()
  2735. {
  2736.     char hostname[100];
  2737.  
  2738. #ifdef HAVE_SYSINFO
  2739.     if (sysinfo(SI_HOSTNAME, hostname, sizeof (hostname)) < 0) {
  2740.         perror("sysinfo");
  2741. #else
  2742.     if (gethostname(hostname, sizeof(hostname)) < 0) {
  2743.         perror("gethostname");
  2744. #endif
  2745.         exit(1);
  2746.     }
  2747.     if (strchr(hostname, '.'))
  2748.         *(strchr(hostname, '.')) = 0;
  2749.  
  2750.     sprintf(krb_ticket_name, "/var/dss/kerberos/tkt/tkt.%d", getpid());
  2751.     krb_set_tkt_string(krb_ticket_name);
  2752.  
  2753.     config_auth();
  2754.  
  2755.     if (krb_svc_init("hesiod", hostname, (char *) NULL, 0, (char *) NULL,
  2756.                      (char *) NULL) != KSUCCESS) {
  2757.         fprintf(stderr, "Couldn't initialize Kerberos\n");
  2758.         exit(1);
  2759.     }
  2760. }
  2761.  
  2762. void
  2763. end_krb()
  2764. {
  2765.     unlink(krb_ticket_name);
  2766. }
  2767. #endif /* KERBEROS */
  2768.  
  2769. #ifdef ULTRIX_AUTH
  2770. static int
  2771. ultrix_check_pass(char *passwd, char *xpasswd)
  2772. {
  2773.     struct svcinfo *svp;
  2774.     int auth_status;
  2775.  
  2776.     if ((svp = getsvc()) == (struct svcinfo *) NULL) {
  2777.         syslog(LOG_WARNING, "getsvc() failed in ultrix_check_pass");
  2778.         return -1;
  2779.     }
  2780.     if (pw == (struct passwd *) NULL) {
  2781.         return -1;
  2782.     }
  2783.     if (((svp->svcauth.seclevel == SEC_UPGRADE) &&
  2784.         (!strcmp(pw->pw_passwd, "*")))
  2785.         || (svp->svcauth.seclevel == SEC_ENHANCED)) {
  2786.         if ((auth_status=authenticate_user(pw, passwd, "/dev/ttypXX")) >= 0) {
  2787.             /* Indicate successful validation */
  2788.             return auth_status;
  2789.         }
  2790.         if (auth_status < 0 && errno == EPERM) {
  2791.             /* Log some information about the failed login attempt. */
  2792.             switch(abs(auth_status)) {
  2793.             case A_EBADPASS:
  2794.                 break;
  2795.             case A_ESOFTEXP:
  2796.                 syslog(LOG_NOTICE, "password will expire soon for user %s",
  2797.                     pw->pw_name);
  2798.                 break;
  2799.             case A_EHARDEXP:
  2800.                 syslog(LOG_NOTICE, "password has expired for user %s",
  2801.                     pw->pw_name);
  2802.                 break;
  2803.             case A_ENOLOGIN:
  2804.                 syslog(LOG_NOTICE, "user %s attempted login to disabled acct",
  2805.                     pw->pw_name);
  2806.                 break;
  2807.             }
  2808.         }
  2809.     }
  2810.     else {
  2811.         if ((*pw->pw_passwd != '\0') && (!strcmp(xpasswd, pw->pw_passwd))) {
  2812.             /* passwd in /etc/passwd isn't empty && encrypted passwd matches */
  2813.             return 0;
  2814.         }
  2815.     }
  2816.     return -1;
  2817. }
  2818. #endif /* ULTRIX_AUTH */
  2819.