home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku206.zip / ckcftp.c < prev    next >
C/C++ Source or Header  |  2002-11-17  |  562KB  |  16,871 lines

  1. /*  C K C F T P  --  FTP Client for C-Kermit  */
  2.  
  3. char *ckftpv = "FTP Client, 8.0.200, 24 Oct 2002";
  4.  
  5. /*
  6.   Authors:
  7.     Jeff Altman <jaltman@columbia.edu>
  8.     Frank da Cruz <fdc@columbia.edu>,
  9.     The Kermit Project, Columbia University.
  10.  
  11.   Copyright (C) 2000, 2002,
  12.     Trustees of Columbia University in the City of New York.
  13.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  14.     copyright text in the ckcmai.c module for disclaimer and permissions.
  15.  
  16.   Portions of conditionally included code Copyright Regents of the
  17.     University of California and The Stanford SRP Authentication Project;
  18.     see notices below.
  19. */
  20.  
  21. /*
  22.   Pending...
  23.  
  24.   . Implement recursive NLST downloads by trying to CD to each filename.
  25.     If it works, it's a directory; if not, it's a file -- GET it.  But
  26.     that won't work with servers like wu-ftpd that don't send directory 
  27.     names.  Recursion with MLSD is done.
  28.  
  29.   . Make syslog entries for session?  Files?
  30.  
  31.   . Messages are printed to stdout and stderr in random fashion.  We should
  32.     either print everything to stdout, or else be systematic about when
  33.     to use stderr.
  34.  
  35.   . Implement mail (MAIL, MLFL, MSOM, etc) if any servers support it.
  36.  
  37.   . Adapt to VMS.  Big job because of its record-oriented file system.
  38.     RMS programmer required.  There are probably also some VMS TCP/IP
  39.     product-specific wrinkles, e.g. attribute preservation in VMS-to-VMS
  40.     transfers using special options for Multinet or other FTP servers
  41.     (find out about STRU VMS).
  42. */
  43.  
  44. /*
  45.   Quick FTP command reference:
  46.  
  47.   RFC765 (1980) and earlier:
  48.     MODE  S(tream), B(lock), C(ompressed)
  49.     STRU  F(ILE), R(ECORD), P(AGE)
  50.     TYPE  A(SCII) <format>,  E(BCDIC) <format>, I(MAGE), L(OCAL) <bytesize>
  51.     PORT  - Port
  52.     PASV  - Passive mode
  53.     USER  - User
  54.     PASS  - Password
  55.     ACCT  - Account
  56.     CWD   - Change Working Directory
  57.     REIN  - Logout but not disconnect
  58.     QUIT  - Bye
  59.     RETR  - Retreive
  60.     STOR  - Store
  61.     APPE  - Append
  62.     ALLO  - Allocate
  63.     REST  - Restart
  64.     RNFR  - Rename from
  65.     RNTO  - Rename to
  66.     ABOR  - Cancel
  67.     DELE  - Delete
  68.     LIST  - Directory
  69.     NLST  - Name List
  70.     SITE  - Site parameters or commands
  71.     STAT  - Status
  72.     HELP  - Help
  73.     NOOP  - Noop
  74.  
  75.   RFC959 (1985):
  76.     CDUP  - Change to Parent Directory
  77.     SMNT  - Structure Mount
  78.     STOU  - Store Unique
  79.     RMD   - Remove Directory
  80.     MKD   - Make Directory
  81.     PWD   - Print Directory
  82.     SYST  - System
  83.  
  84.   RFC2389 (1998):
  85.     FEAT  - List Features (done)
  86.     OPTS  - Send options (done)
  87.  
  88.   RFC2640 (1999):
  89.     LANG  - Specify language for messages (not done)
  90.  
  91.   Pending (Internet Drafts):
  92.     SIZE  - File size (done)
  93.     MDTM  - File modification date-time (done)
  94.     MLST  - File name and attribute list (single file) (not done)
  95.     MLSD  - File list with attributes (multiple files) (done)
  96.     MAIL, MLFL, MSOM - mail delivery (not done)
  97.  
  98.   Alphabetical syntax list:
  99.     ABOR <CRLF>
  100.     ACCT <SP> <account-information> <CRLF>
  101.     ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
  102.     APPE <SP> <pathname> <CRLF>
  103.     CDUP <CRLF>
  104.     CWD  <SP> <pathname> <CRLF>
  105.     DELE <SP> <pathname> <CRLF>
  106.     FEAT <CRLF>
  107.     HELP [<SP> <string>] <CRLF>
  108.     LANG [<SP> <language-tag> ] <CRLF>
  109.     LIST [<SP> <pathname>] <CRLF>
  110.     MKD  <SP> <pathname> <CRLF>
  111.     MLSD [<SP> <pathname>] <CRLF>
  112.     MLST [<SP> <pathname>] <CRLF>
  113.     MODE <SP> <mode-code> <CRLF>
  114.     NLST [<SP> <pathname-or-wildcard>] <CRLF>
  115.     NOOP <CRLF>
  116.     OPTS <SP> <commandname> [ <SP> <command-options> ] <CRLF>
  117.     PASS <SP> <password> <CRLF>
  118.     PASV <CRLF>
  119.     PORT <SP> <host-port> <CRLF>
  120.     PWD  <CRLF>
  121.     QUIT <CRLF>
  122.     REIN <CRLF>
  123.     REST <SP> <marker> <CRLF>
  124.     RETR <SP> <pathname> <CRLF>
  125.     RMD  <SP> <pathname> <CRLF>
  126.     RNFR <SP> <pathname> <CRLF>
  127.     RNTO <SP> <pathname> <CRLF>
  128.     SITE <SP> <string> <CRLF>
  129.     SIZE <SP> <pathname> <CRLF>
  130.     SMNT <SP> <pathname> <CRLF>
  131.     STAT [<SP> <pathname>] <CRLF>
  132.     STOR <SP> <pathname> <CRLF>
  133.     STOU <CRLF>
  134.     STRU <SP> <structure-code> <CRLF>
  135.     SYST <CRLF>
  136.     TYPE <SP> <type-code> <CRLF>
  137.     USER <SP> <username> <CRLF>
  138. */
  139. #include "ckcsym.h"                     /* Standard includes */
  140. #include "ckcdeb.h"
  141.  
  142. #ifndef NOFTP                           /* NOFTP  = no FTP */
  143. #ifndef SYSFTP                          /* SYSFTP = use external ftp client */
  144. #ifdef TCPSOCKET                        /* Build only if TCP/IP included */
  145. #define CKCFTP_C
  146.  
  147. /* Note: much of the following duplicates what was done in ckcdeb.h */
  148. /* but let's not mess with it unless it causes trouble. */
  149.  
  150. #ifdef CK_ANSIC
  151. #include <stdarg.h>
  152. #else /* CK_ANSIC */
  153. #include <varargs.h>
  154. #endif /* CK_ANSIC */
  155. #include <signal.h>
  156. #ifdef OS2
  157. #ifdef OS2ONLY
  158. #include <os2.h>
  159. #endif /* OS2ONLY */
  160. #include "ckowin.h"
  161. #include "ckocon.h"
  162. #endif /* OS2 */
  163. #ifndef ZILOG
  164. #ifdef NT
  165. #include <setjmpex.h>
  166. #ifdef NTSIG
  167. extern int TlsIndex;
  168. #endif /* NTSIG */
  169. #else /* NT */
  170. #include <setjmp.h>
  171. #endif /* NT */
  172. #else
  173. #include <setret.h>
  174. #endif /* ZILOG */
  175. #include "ckcsig.h"
  176. #include <sys/stat.h>
  177. #include <ctype.h>
  178. #include <errno.h>
  179. #ifndef NOTIMEH
  180. #include <time.h>
  181. #endif /* NOTIMEH */
  182. #ifndef EPIPE
  183. #define EPIPE 32                        /* Broken pipe error */
  184. #endif /* EPIPE */
  185.  
  186. /* Kermit includes */
  187.  
  188. #include "ckcasc.h"
  189. #include "ckcker.h"
  190. #include "ckucmd.h"
  191. #include "ckuusr.h"
  192. #include "ckcnet.h"                     /* Includes ckctel.h */
  193. #include "ckctel.h"                     /* (then why include it again?) */
  194. #include "ckcxla.h"
  195.  
  196. /*
  197.   How to get the struct timeval definition so we can call select().  The
  198.   xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  199.   targets.  The problem is: maybe we have already included some header file
  200.   that defined struct timeval, and maybe we didn't.  If we did, we don't want
  201.   to include another header file that defines it again or the compilation will
  202.   fail.  If we didn't, we have to include the header file where it's defined.
  203.   But in some cases even that won't work because of strict POSIX constraints
  204.   or somesuch, or because this introduces other conflicts (e.g. struct tm
  205.   multiply defined), in which case we have to define it ourselves, but this
  206.   can work only if we didn't already encounter a definition.
  207. */
  208. #ifndef DCLTIMEVAL
  209. #ifdef SV68R3V6
  210. #define DCLTIMEVAL
  211. #else
  212. #ifdef SCO234
  213. #define DCLTIMEVAL
  214. #endif /* SCO234 */
  215. #endif /* SV68R3V6 */
  216. #endif /* DCLTIMEVAL */
  217.  
  218. #ifdef DCLTIMEVAL
  219. /* Also maybe in some places the elements must be unsigned... */
  220. struct timeval {
  221.     long tv_sec;
  222.     long tv_usec;
  223. };
  224. #ifdef COMMENT
  225. /* Currently we don't use this... */
  226. struct timezone {
  227.     int tz_minuteswest;
  228.     int tz_dsttime;
  229. };
  230. #endif /* COMMENT */
  231. #else  /* !DCLTIMEVAL */
  232. #ifndef NOSYSTIMEH
  233. #ifdef SYSTIMEH
  234. #include <sys/time.h>
  235. #endif /* SYSTIMEH */
  236. #endif /* NOSYSTIMEH */
  237. #ifndef NOSYSTIMEBH
  238. #ifdef SYSTIMEBH
  239. #include <sys/timeb.h>
  240. #endif /* SYSTIMEBH */
  241. #endif /* NOSYSTIMEBH */
  242. #endif /* DCLTIMEVAL */
  243.  
  244. #ifndef NOSETTIME
  245. #ifdef COMMENT
  246. /* This section moved to ckcdeb.h */
  247. #ifdef POSIX
  248. #define UTIMEH
  249. #else
  250. #ifdef HPUX9
  251. #define UTIMEH
  252. #else
  253. #ifdef OS2
  254. #define SYSUTIMEH
  255. #endif /* OS2 */
  256. #endif /* HPUX9 */
  257. #endif /* POSIX */
  258. #endif /* COMMENT */
  259.  
  260. #ifdef SYSUTIMEH
  261. #include <sys/utime.h>
  262. #else
  263. #ifdef UTIMEH
  264. #include <utime.h>
  265. #define SYSUTIMEH
  266. #endif /* UTIMEH */
  267. #endif /* SYSUTIMEH */
  268. #endif /* NOSETTIME */
  269.  
  270. #ifndef SCO_OSR504
  271. #ifdef SELECT_H
  272. #include <sys/select.h>
  273. #endif /* SELECT_H */
  274. #endif /* SCO_OSR504 */
  275.  
  276. /* select() dialects... */
  277.  
  278. #ifdef UNIX
  279. #define BSDSELECT                       /* BSD select() syntax/semantics */
  280. #else
  281. #ifdef OS2                              /* OS/2 or Win32 */
  282. #ifdef NT
  283. #define BSDSELECT
  284. #else /* NT */
  285. #define IBMSELECT
  286. #endif /* NT */
  287. #endif /* OS2 */
  288. #endif /* UNIX */
  289.  
  290. /* Other select() peculiarities */
  291.  
  292. #ifdef HPUX
  293. #ifndef HPUX10                          /* HP-UX 9.xx and earlier */
  294. #ifndef HPUX1100
  295. /* The three interior args to select() are (int *) rather than (fd_set *) */
  296. #ifndef INTSELECT
  297. #define INTSELECT
  298. #endif /* INTSELECT */
  299. #endif /* HPUX1100 */
  300. #endif /* HPUX10 */
  301. #endif /* HPUX */
  302.  
  303. #ifdef CK_SOCKS                         /* SOCKS Internet relay package */
  304. #ifdef CK_SOCKS5                        /* SOCKS 5 */
  305. #define accept  SOCKSaccept
  306. #define bind    SOCKSbind
  307. #define connect SOCKSconnect
  308. #define getsockname SOCKSgetsockname
  309. #define listen SOCKSlisten
  310. #else  /* Not SOCKS 5 */
  311. #define accept  Raccept
  312. #define bind    Rbind
  313. #define connect Rconnect
  314. #define getsockname Rgetsockname
  315. #define listen Rlisten
  316. #endif /* CK_SOCKS5 */
  317. #endif /* CK_SOCKS */
  318.  
  319. #ifndef NOHTTP
  320. extern char * tcp_http_proxy;           /* Name[:port] of http proxy server */
  321. extern int    tcp_http_proxy_errno;
  322. extern char * tcp_http_proxy_user;
  323. extern char * tcp_http_proxy_pwd;
  324. #define HTTPCPYL 1024
  325. static char proxyhost[HTTPCPYL];
  326. #endif /* NOHTTP */
  327. int ssl_ftp_proxy = 0;                  /* FTP over SSL/TLS Proxy Server */
  328.  
  329. /* Feature selection */
  330.  
  331. #ifndef USE_SHUTDOWN
  332. /*
  333.   We don't use shutdown() because (a) we always call it just before close()
  334.   so it's redundant and unnecessary, and (b) it introduces a long pause on
  335.   some platforms like SV/68 R3.
  336. */
  337. /* #define USE_SHUTDOWN */
  338. #endif /* USE_SHUTDOWN */
  339.  
  340. #ifndef NORESEND
  341. #ifndef NORESTART                       /* Restart / recover */
  342. #ifndef FTP_RESTART
  343. #define FTP_RESTART
  344. #endif /* FTP_RESTART */
  345. #endif /* NORESTART */
  346. #endif /* NORESEND */
  347.  
  348. #ifndef NOUPDATE                        /* Update mode */
  349. #ifndef DOUPDATE
  350. #define DOUPDATE
  351. #endif /* DOUPDATE */
  352. #endif /* NOUPDATE */
  353.  
  354. #ifndef UNICODE                         /* Unicode required */
  355. #ifndef NOCSETS                         /* for charset translation */
  356. #define NOCSETS
  357. #endif /* NOCSETS */
  358. #endif /* UNICODE */
  359.  
  360. #ifndef OS2
  361. #ifndef HAVE_MSECS                      /* Millisecond timer */
  362. #ifdef UNIX
  363. #ifdef GFTIMER
  364. #define HAVE_MSECS
  365. #endif /* GFTIMER */
  366. #endif /* UNIX */
  367. #endif /* HAVE_MSECS */
  368. #endif /* OS2 */
  369.  
  370. #ifdef PIPESEND                         /* PUT from pipe */
  371. #ifndef PUTPIPE
  372. #define PUTPIPE
  373. #endif /* PUTPIPE */
  374. #endif /* PIPESEND */
  375.  
  376. #ifndef NOSPL                           /* PUT from array */
  377. #ifndef PUTARRAY
  378. #define PUTARRAY
  379. #endif /* PUTARRAY */
  380. #endif /* NOSPL */
  381.  
  382. /* Security... */
  383.  
  384. #ifdef CK_SRP
  385. #define FTP_SRP
  386. #endif /* CK_SRP */
  387.  
  388. #ifdef CK_KERBEROS
  389. #ifdef KRB4
  390. /*
  391.   There is a conflict between the Key Schedule formats used internally
  392.   within the standalone MIT KRB4 library and that used by Eric Young
  393.   in OpenSSL and his standalone DES library.  Therefore, KRB4 FTP AUTH
  394.   cannot be supported when either of those two packages are used.
  395. */
  396. #ifdef KRB524
  397. #define FTP_KRB4
  398. #else /* KRB524 */
  399. #ifndef CK_SSL
  400. #ifndef LIBDES
  401. #define FTP_KRB4
  402. #endif /* LIBDES */
  403. #endif /* CK_SSL */
  404. #endif /* KRB524 */
  405. #endif /* KRB4 */
  406. #ifdef KRB5
  407. #ifndef HEIMDAL
  408. #define FTP_GSSAPI
  409. #endif /* HEIMDAL */
  410. #endif /* KRB5 */
  411. #endif /* CK_KERBEROS */
  412.  
  413. /* FTP_SECURITY is defined if any of the above is selected */
  414. #ifndef FTP_SECURITY
  415. #ifdef FTP_GSSAPI
  416. #define FTP_SECURITY
  417. #else
  418. #ifdef FTP_KRB4
  419. #define FTP_SECURITY
  420. #else
  421. #ifdef FTP_SRP
  422. #define FTP_SECURITY
  423. #else
  424. #ifdef CK_SSL
  425. #define FTP_SECURITY
  426. #endif /* CK_SSL */
  427. #endif /* FTP_SRP */
  428. #endif /* FTP_KRB4 */
  429. #endif /* FTP_GSSAPI */
  430. #endif /* FTP_SECURITY */
  431.  
  432. #ifdef CK_DES
  433. #ifdef CK_SSL
  434. #ifndef LIBDES
  435. #define LIBDES
  436. #endif /* LIBDES */
  437. #endif /* CK_SSL */
  438. #endif /* CK_DES */
  439.  
  440. #ifdef CRYPT_DLL
  441. #ifndef LIBDES
  442. #define LIBDES
  443. #endif /* LIBDES */
  444. #endif /* CRYPT_DLL */
  445.  
  446. #ifdef FTP_KRB4
  447. #define des_cblock Block
  448. #define des_key_schedule Schedule
  449. #ifdef KRB524
  450. #ifdef NT
  451. #define _WINDOWS
  452. #endif /* NT */
  453. #include "kerberosIV/krb.h"
  454. #else /* KRB524 */
  455. #ifdef SOLARIS
  456. #ifndef sun
  457. /* For some reason lost in history the Makefile Solaris targets have -Usun */
  458. #define sun
  459. #endif /* sun */
  460. #endif /* SOLARIS */
  461. #include "krb.h"
  462. #define krb_get_err_text_entry krb_get_err_text
  463. #endif /* KRB524 */
  464. #endif /* FTP_KRB4 */
  465.  
  466. #ifdef CK_SSL
  467. #ifdef FTP_KRB4
  468. #ifndef HEADER_DES_H
  469. #define HEADER_DES_H
  470. #endif /* HEADER_DES_H */
  471. #endif /* FTP_KRB4 */
  472. #include "ck_ssl.h"
  473. #endif /* CK_SSL */
  474.  
  475. #ifdef FTP_SRP
  476. #ifdef HAVE_PWD_H
  477. #include "pwd.h"
  478. #endif /* HAVE_PWD_H */
  479. #include "t_pwd.h"
  480. #include "t_client.h"
  481. #include "krypto.h"
  482. #endif /* FTP_SRP */
  483.  
  484. #ifdef FTP_GSSAPI
  485. #include <gssapi/gssapi.h>
  486. /*
  487.   Need to include the krb5 file, because we're doing manual fallback
  488.   from the v2 mech to the v1 mech.  Once there's real negotiation,
  489.   we can be generic again.
  490. */
  491. #include <gssapi/gssapi_generic.h>
  492. #include <gssapi/gssapi_krb5.h>
  493. static gss_ctx_id_t gcontext;
  494. #endif /* FTP_GSSAPI */
  495.  
  496. #ifdef OS2
  497. #ifdef FTP_SRP
  498. #define MAP_KRYPTO
  499. #ifdef SRPDLL
  500. #define MAP_SRP
  501. #endif /* SRPDLL */
  502. #endif /* FTP_SRP */
  503. #ifdef FTP_KRB4
  504. #define MAP_KRB4
  505. #define MAP_DES
  506. #endif /* FTP_KRB4 */
  507. #ifdef FTP_GSSAPI
  508. #define MAP_GSSAPI
  509. #define GSS_OIDS
  510. #endif /* FTP_GSSAPI */
  511. #include "ckoath.h"
  512.  
  513. extern int k95stdout, wherex[], wherey[];
  514. extern unsigned char colorcmd;
  515. #endif /* OS2 */
  516.  
  517. #ifdef FTP_KRB4
  518. static char ftp_realm[REALM_SZ + 1];
  519. static KTEXT_ST ftp_tkt;
  520. #ifdef OS2
  521. static LEASH_CREDENTIALS ftp_cred;
  522. #else /* OS2 */
  523. static CREDENTIALS ftp_cred;
  524. #endif /* OS2 */
  525. static MSG_DAT ftp_msg_data;
  526. static des_key_schedule ftp_sched;
  527. static int foo[4] = {99,99,99,99};
  528. #endif /* FTP_KRB4 */
  529.  
  530. /* getreply() function codes */
  531.  
  532. #define GRF_AUTH 1            /* Reply to AUTH command */
  533. #define GRF_FEAT 2            /* Reply to FEAT command */
  534.  
  535. /* Operational definitions */
  536.  
  537. #define DEF_VBM 0                       /* Default verbose mode */
  538. /* #define SETVBM */                    /* (see getreply) */
  539.  
  540. #define URL_ONEFILE                     /* GET, not MGET, for FTP URL */
  541.  
  542. #define FTP_BUFSIZ 10240                /* Max size for FTP cmds & replies */
  543. #define SRVNAMLEN 32                    /* Max length for server type name */
  544.  
  545. #ifndef MGETMAX                         /* Max operands for MGET command */
  546. #define MGETMAX 1000
  547. #endif /* MGETMAX */
  548.  
  549. #ifdef FTP_SRP
  550. #define FUDGE_FACTOR 100
  551. #endif /* FTP_SRP */
  552.  
  553. /*
  554.   Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  555.   number bytes.  Must be defined for each auth type.
  556.   GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  557.   3DES requires 56 bytes.  Lets use 96 just to be sure.
  558. */
  559. #ifdef FTP_GSSAPI
  560. #ifndef FUDGE_FACTOR
  561. #define FUDGE_FACTOR 96
  562. #endif /* FUDGE_FACTOR */
  563. #endif /* FTP_GSSAPI */
  564.  
  565. #ifdef FTP_KRB4
  566. #ifndef FUDGE_FACTOR
  567. #define FUDGE_FACTOR 32
  568. #endif /* FUDGE_FACTOR */
  569. #endif /* FTP_KRB4 */
  570.  
  571. #ifndef FUDGE_FACTOR                    /* In case no auth types define it */
  572. #define FUDGE_FACTOR 0
  573. #endif /* FUDGE_FACTOR */
  574.  
  575. #ifndef MAXHOSTNAMELEN
  576. #define MAXHOSTNAMELEN 64
  577. #endif /* MAXHOSTNAMELEN */
  578. #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
  579.  
  580. /* Fascist compiler toadying */
  581.  
  582. #ifndef SENDARG2TYPE
  583. #ifdef COMMENT                          /* Might be needed here and there */
  584. #define SENDARG2TYPE const char *
  585. #else
  586. #define SENDARG2TYPE char *
  587. #endif /* COMMENT */
  588. #endif /* SENDARG2TYPE */
  589.  
  590. /* Common text messages */
  591.  
  592. static char *nocx = "?No FTP control connection\n";
  593.  
  594. static char *fncnam[] = {
  595.   "rename", "overwrite", "backup", "append", "discard", "ask", "update",
  596.   "dates-differ", ""
  597. };
  598.  
  599. /* Macro definitions */
  600.  
  601. /* Used to speed up text-mode PUTs */
  602. #define zzout(fd,c) \
  603. ((fd<0)?(-1):((nout>ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
  604.  
  605. #define CHECKCONN() if(!connected){printf(nocx);return(-9);}
  606.  
  607. /* Externals */
  608.  
  609. #ifdef CK_URL
  610. extern struct urldata g_url;
  611. #endif /* CK_URL */
  612.  
  613. #ifdef DYNAMIC
  614. extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
  615. #else
  616. extern char zinbuffer[], zoutbuffer[];
  617. #endif /* DYNAMIC */
  618. extern char *zinptr, *zoutptr;
  619. extern int zincnt, zoutcnt, zobufsize, fncact;
  620.  
  621. #ifdef CK_TMPDIR
  622. extern int f_tmpdir;                    /* Directory changed temporarily */
  623. extern char savdir[];                   /* For saving current directory */
  624. extern char * dldir;
  625. #endif /* CK_TMPDIR */
  626.  
  627. extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
  628.  
  629. extern xx_strp xxstring;
  630. extern struct keytab onoff[], txtbin[], rpathtab[];
  631. extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
  632. extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
  633. extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
  634. extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
  635. extern int nolinks, msgflg, keep;
  636. extern long fsize, ffc, tfc, filcnt, xfsecs, tfcps, cps, oldcps;
  637. #ifdef GFTIMER
  638. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  639. #else
  640. extern long xfsecs;
  641. #endif /* GFTIMER */
  642.  
  643. extern char filnam[], * filefile, myhost[];
  644. extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
  645. extern int g_skipbup, skipbup, sendmode;
  646. extern int g_displa, fdispla, displa;
  647.  
  648. #ifdef LOCUS
  649. extern int locus, autolocus;
  650. #endif /* LOCUS */
  651.  
  652. #ifndef NOCSETS
  653. extern int nfilc, dcset7, dcset8, fileorder;
  654. extern struct csinfo fcsinfo[];
  655. extern struct keytab fcstab[];
  656. extern int fcharset;
  657. #endif /* NOCSETS */
  658.  
  659. extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
  660. extern char sndnbefore[], sndnafter[], *rcvexcept[];
  661. extern CHAR feol;
  662. extern long sendstart, sndsmaller, sndlarger, rs_len;
  663.  
  664. extern char * remdest;
  665. extern int remfile, remappd, rempipe;
  666.  
  667. #ifndef NOSPL
  668. extern int cmd_quoting;
  669. #ifdef PUTARRAY
  670. extern int sndxlo, sndxhi, sndxin;
  671. extern char sndxnam[];
  672. extern char **a_ptr[];                  /* Array pointers */
  673. extern int a_dim[];                     /* Array dimensions */
  674. #endif /* PUTARRAY */
  675. #endif /* NOSPL */
  676.  
  677. #ifndef NOMSEND                         /* MPUT and ADD SEND-LIST lists */
  678. extern char *msfiles[];
  679. extern int filesinlist;
  680. extern struct filelist * filehead;
  681. extern struct filelist * filetail;
  682. extern struct filelist * filenext;
  683. extern int addlist;
  684. extern char fspec[];                    /* Most recent filespec */
  685. extern int fspeclen;                    /* Length of fspec[] buffer */
  686. #endif /* NOMSEND */
  687.  
  688. extern int pipesend;
  689. #ifdef PIPESEND
  690. extern char * sndfilter, * rcvfilter;
  691. #endif /* PIPESEND */
  692.  
  693. #ifdef CKROOT
  694. extern int ckrooterr;
  695. #endif /* CKROOT */
  696.  
  697. #ifdef KRB4
  698. extern int krb4_autoget;
  699. _PROTOTYP(char * ck_krb4_realmofhost,(char *));
  700. #endif /* KRB4 */
  701.  
  702. #ifdef KRB5
  703. extern int krb5_autoget;
  704. extern int krb5_d_no_addresses;
  705. _PROTOTYP(char * ck_krb5_realmofhost,(char *));
  706. #endif /* KRB5 */
  707.  
  708. #ifdef DCMDBUF
  709. extern char *atmbuf;                    /* Atom buffer (malloc'd) */
  710. extern char *cmdbuf;                    /* Command buffer (malloc'd) */
  711. extern char *line;                      /* Big string buffer #1 */
  712. extern char *tmpbuf;                    /* Big string buffer #2 */
  713. #else
  714. extern char atmbuf[];                   /* The same, but static */
  715. extern char cmdbuf[];
  716. extern char line[];
  717. extern char tmpbuf[];
  718. #endif /* DCMDBUF */
  719.  
  720. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  721.  
  722. /* Public variables declared here */
  723.  
  724. #ifdef NOXFER
  725. int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
  726. #else
  727. int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
  728. #endif /* NOXFER */
  729. int ftpcode = -1;                       /* Last FTP response code */
  730. int ftp_cmdlin = 0;                     /* FTP invoked from command line */
  731. int ftp_fai = 0;                        /* FTP failure count */
  732. int ftp_deb = 0;                        /* FTP debugging */
  733. int ftp_dis = -1;            /* FTP display style */
  734. int ftp_log = 1;                        /* FTP Auto-login */
  735. int ftp_action = 0;                     /* FTP action from command line */
  736. int ftp_dates = 1;                      /* Set file dates from server */
  737.  
  738. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  739. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  740. char ftp_user_host[MAX_DNS_NAMELEN]= ""; /* FTP hostname specified by user */
  741. char * ftp_host = NULL;                 /* FTP hostname */
  742. char * ftp_logname = NULL;              /* FTP username */
  743. char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
  744. char * ftp_apw = NULL;            /* Anonymous password */
  745.  
  746. /* Static global variables */
  747.  
  748. static char * fts_sto = NULL;
  749. /*
  750.   This is just a first stab -- these strings should match how the
  751.   corresponding FTP servers identify themselves.
  752. */
  753. #ifdef UNIX
  754. static char * myostype = "UNIX";
  755. #else
  756. #ifdef VMS
  757. /* not yet... */
  758. static char * myostype = "VMS";
  759. #else
  760. #ifdef OS2
  761. #ifdef NT
  762. static char * myostype = "WIN32";
  763. #else
  764. static char * myostype = "OS/2";
  765. #endif /* NT */
  766. #else
  767. static char * myostype = "UNSUPPORTED";
  768. #endif /* OS2  */
  769. #endif /* VMS */
  770. #endif /* UNIX */
  771.  
  772. static int noinit = 0;                  /* Don't send REST, STRU, MODE */
  773. static int alike = 0;                   /* Client/server like platforms */
  774. static int local = 1;                   /* Shadows Kermit global 'local' */
  775. static int dout = -1;                   /* Data connection file descriptor */
  776. static int dpyactive = 0;               /* Data transfer is active */
  777. static int globaldin = -1;              /* Data connection f.d. */
  778. static int out2screen = 0;              /* GET output is to screen */
  779. static int forcetype = 0;               /* Force text or binary mode */
  780. static int cancelfile = 0;              /* File canceled */
  781. static int cancelgroup = 0;             /* Group canceled */
  782. static int anonymous = 0;               /* Logging in as anonymous */
  783. static int loggedin = 0;                /* Logged in (or not) */
  784. static int puterror = 0;                /* What to do on PUT error */
  785. static int geterror = 0;                /* What to do on GET error */
  786. static int rfrc = 0;                    /* remote_files() return code */
  787. static int okrestart = 0;               /* Server understands REST */
  788. static int printlines = 0;              /* getreply()should print data lines */
  789. static int haveurl = 0;                 /* Invoked by command-line FTP URL */
  790. static int mdtmok = 1;            /* Server supports MDTM */
  791. static int sizeok = 1;
  792. static int featok = 1;
  793. static int mlstok = 1;
  794. static int stouarg = 1;
  795. static int typesent = 0;
  796. static int havesigint = 0;
  797. static long havetype =  0;
  798. static long havesize = -1L;
  799. static char * havemdtm = NULL;
  800. static int mgetmethod = 0;        /* NLST or MLSD */
  801. static int mgetforced = 0;
  802.  
  803. static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
  804. static int c0, c1;                      /* Temp variables for characters */
  805.  
  806. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  807. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  808.  
  809. #define RFNBUFSIZ 4096            /* Remote filename buffer size */
  810.  
  811. static unsigned int maxbuf = 0, actualbuf = 0;
  812. static CHAR *ucbuf = NULL;
  813. static int ucbufsiz = 0;
  814. static unsigned int nout = 0;           /* Number of chars in ucbuf */
  815.  
  816. static jmp_buf recvcancel;
  817. static jmp_buf sendcancel;
  818. static jmp_buf ptcancel;
  819. static jmp_buf jcancel;
  820. static int ptabflg = 0;
  821.  
  822. /* Protection level symbols */
  823.  
  824. #define FPL_CLR 1                       /* Clear */
  825. #define FPL_SAF 2                       /* Safe */
  826. #define FPL_PRV 3                       /* Private */
  827. #define FPL_CON 4                       /* Confidential */
  828.  
  829. /* Symbols for file types returned by MLST/MLSD */
  830.  
  831. #define FTYP_FILE 1            /* Regular file */
  832. #define FTYP_DIR  2            /* Directory */
  833. #define FTYP_CDIR 3            /* Current directory */
  834. #define FTYP_PDIR 4            /* Parent directory */
  835.  
  836. /* File type symbols keyed to the file-type symbols from ckcker.h */
  837.  
  838. #define FTT_ASC XYFT_T                  /* ASCII (text) */
  839. #define FTT_BIN XYFT_B                  /* Binary (image) */
  840. #define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
  841.  
  842. /* Server feature table - sfttab[0] > 0 means server supports FEAT and OPTS */
  843.  
  844. static int sfttab[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  845.  
  846. #define SFT_AUTH  1            /* FTP server feature codes */
  847. #define SFT_LANG  2
  848. #define SFT_MDTM  3
  849. #define SFT_MLST  4
  850. #define SFT_PBSZ  5
  851. #define SFT_PROT  6
  852. #define SFT_REST  7
  853. #define SFT_SIZE  8
  854. #define SFT_TVFS  9
  855. #define SFT_UTF8 10
  856.  
  857. #define CNV_AUTO  2            /* FTP filename conversion */
  858. #define CNV_CNV   1
  859. #define CNV_LIT   0
  860.  
  861. /* SET FTP values */
  862.  
  863. static int                              /* SET FTP values... */
  864.   ftp_aut = 1,                          /* Auto-authentication */
  865. #ifdef FTP_SECURITY
  866.   ftp_cry = 1,                          /* Auto-encryption */
  867.   ftp_cfw = 0,                          /* Credential forwarding */
  868. #endif /* FTP_SECURITY */
  869.   ftp_cpl = FPL_CLR,                    /* Command protection level */
  870.   ftp_dpl = FPL_CLR,                    /* Data protection level */
  871. #ifdef FTP_PROXY
  872.   ftp_prx = 0,                          /* Use proxy */
  873. #endif /* FTP_PROXY */
  874.   sav_psv = -1,                         /* For saving passive mode */
  875.   ftp_psv = 1,                          /* Passive mode */
  876.   ftp_spc = 1,                          /* Send port commands */
  877.   ftp_typ = FTT_ASC,                    /* Type */
  878.   get_auto = 1,                         /* Automatic type switching for GET */
  879.   tenex = 0,                            /* Type is Tenex */
  880.   ftp_usn = 0,                          /* Unique server names */
  881.   ftp_prm = 0,                          /* Permissions */
  882.   ftp_cnv = CNV_AUTO,            /* Filename conversion (2 = auto) */
  883.   ftp_vbm = DEF_VBM,                    /* Verbose mode */
  884.   ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  885.   ftp_err = 0,                          /* Error action */
  886.   ftp_fnc = -1;                         /* Filename collision action */
  887.  
  888. static int
  889. #ifdef NOCSETS
  890.   ftp_csr = -1,                         /* Remote (server) character set */
  891. #else
  892.   ftp_csr = FC_UTF8,
  893. #endif /* NOCSETS */
  894.   ftp_xla = 0;                          /* Character-set translation on/off */
  895. int
  896.   ftp_csx = -1,                         /* Remote charset currently in use */
  897.   ftp_csl = -1;                         /* Local charset currently in use */
  898.  
  899. static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
  900.  
  901. char * ftp_nml = NULL;                  /* /NAMELIST */
  902. char * ftp_tmp = NULL;                  /* Temporary string */
  903. static char * ftp_acc = NULL;           /* Account string */
  904. static char * auth_type = NULL;         /* Authentication type */
  905. static char * srv_renam = NULL;         /* Server-rename string */
  906. FILE * fp_nml = NULL;                   /* Namelist file pointer */
  907.  
  908. static int csocket = -1;                /* Control socket */
  909. static int connected = 0;               /* Connected to FTP server */
  910. static short ftp_port = 0;              /* FTP port */
  911. #ifdef FTPHOST
  912. static int hostcmd = 0;                 /* Has HOST command been sent */
  913. #endif /* FTPHOST */
  914. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  915. static char bytename[8];
  916.  
  917. /* For parsing replies to FTP server command */
  918. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  919.  
  920. #ifdef FTP_PROXY
  921. static int proxy, unix_proxy
  922. #endif /* FTP_PROXY */
  923.  
  924. static char pasv[64];                   /* Passive-mode port */
  925. static int passivemode = 0;
  926. static int sendport = 0;
  927. static int servertype = 0;              /* FTP server's OS type */
  928.  
  929. static int testing = 0;
  930. static char ftpcmdbuf[FTP_BUFSIZ];
  931.  
  932. /* Macro definitions */
  933.  
  934. #define UC(b) ckitoa(((int)b)&0xff)
  935. #define nz(x) ((x) == 0 ? 1 : (x))
  936.  
  937. /* Command tables and definitions */
  938.  
  939. #define FTP_ACC  1                      /* FTP command keyword codes */
  940. #define FTP_APP  2
  941. #define FTP_CWD  3
  942. #define FTP_CHM  4
  943. #define FTP_CLS  5
  944. #define FTP_DEL  6
  945. #define FTP_DIR  7
  946. #define FTP_GET  8
  947. #define FTP_IDL  9
  948. #define FTP_MDE 10
  949. #define FTP_MDI 11
  950. #define FTP_MGE 12
  951. #define FTP_MKD 13
  952. #define FTP_MOD 14
  953. #define FTP_MPU 15
  954. #define FTP_OPN 16
  955. #define FTP_PUT 17
  956. #define FTP_PWD 18
  957. #define FTP_RGE 19
  958. #define FTP_REN 20
  959. #define FTP_RES 21
  960. #define FTP_HLP 22
  961. #define FTP_RMD 23
  962. #define FTP_STA 24
  963. #define FTP_SIT 25
  964. #define FTP_SIZ 26
  965. #define FTP_SYS 27
  966. #define FTP_UMA 28
  967. #define FTP_GUP 29
  968. #define FTP_USR 30
  969. #define FTP_QUO 31
  970. #define FTP_TYP 32
  971. #define FTP_FEA 33
  972. #define FTP_OPT 34
  973. #define FTP_CHK 35
  974. #define FTP_VDI 36
  975. #define FTP_ENA 37
  976. #define FTP_DIS 38
  977.  
  978. struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
  979.     { "auto",    2, 0 },
  980.     { "ftp",     1, 0 },
  981.     { "kermit",  0, 0  }
  982. };
  983.  
  984. static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
  985.     { "proceed", 0, 0 },                /* 0 = proceed */
  986.     { "quit",    1, 0 }                 /* 1 = quit */
  987. };
  988.  
  989. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  990.     { "account",   FTP_ACC, 0 },
  991.     { "append",    FTP_APP, 0 },
  992.     { "bye",       FTP_CLS, 0 },
  993.     { "cd",        FTP_CWD, 0 },
  994.     { "cdup",      FTP_GUP, 0 },
  995.     { "check",     FTP_CHK, 0 },
  996.     { "chmod",     FTP_CHM, 0 },
  997.     { "close",     FTP_CLS, 0 },
  998.     { "cwd",       FTP_CWD, CM_INV },
  999.     { "delete",    FTP_MDE, 0 },
  1000.     { "directory", FTP_DIR, 0 },
  1001.     { "disable",   FTP_DIS, 0 },
  1002.     { "enable",    FTP_ENA, 0 },
  1003.     { "features",  FTP_FEA, 0 },
  1004.     { "get",       FTP_GET, 0 },
  1005.     { "help",      FTP_HLP, 0 },
  1006.     { "idle",      FTP_IDL, 0 },
  1007.     { "login",     FTP_USR, CM_INV },
  1008.     { "mdelete",   FTP_MDE, CM_INV },
  1009.     { "mget",      FTP_MGE, 0 },
  1010.     { "mkdir",     FTP_MKD, 0 },
  1011.     { "modtime",   FTP_MOD, 0 },
  1012.     { "mput",      FTP_MPU, 0 },
  1013.     { "open",      FTP_OPN, 0 },
  1014.     { "opt",       FTP_OPT, CM_INV|CM_ABR },
  1015.     { "opts",      FTP_OPT, CM_INV },
  1016.     { "options",   FTP_OPT, 0 },
  1017.     { "put",       FTP_PUT, 0 },
  1018.     { "pwd",       FTP_PWD, 0 },
  1019.     { "quit",      FTP_CLS, CM_INV },
  1020.     { "quote",     FTP_QUO, 0 },
  1021.     { "reget",     FTP_RGE, 0 },
  1022.     { "rename",    FTP_REN, 0 },
  1023.     { "reset",     FTP_RES, 0 },
  1024.     { "rmdir",     FTP_RMD, 0 },
  1025.     { "send",      FTP_PUT, CM_INV },
  1026.     { "site",      FTP_SIT, 0 },
  1027.     { "size",      FTP_SIZ, 0 },
  1028.     { "status",    FTP_STA, 0 },
  1029.     { "system",    FTP_SYS, 0 },
  1030.     { "type",      FTP_TYP, 0 },
  1031.     { "umask",     FTP_UMA, 0 },
  1032.     { "up",        FTP_GUP, CM_INV },
  1033.     { "user",      FTP_USR, 0 },
  1034.     { "vdirectory",FTP_VDI, 0 },
  1035.     { "", 0, 0 }
  1036. };
  1037. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  1038.  
  1039. #define OPN_ANO 1            /* FTP OPEN switch codes */
  1040. #define OPN_PSW 2
  1041. #define OPN_USR 3
  1042. #define OPN_ACC 4
  1043. #define OPN_ACT 5
  1044. #define OPN_PSV 6
  1045. #define OPN_TLS 7
  1046. #define OPN_NIN 8
  1047.  
  1048. #ifdef FTP_SECURITY
  1049. #ifdef CK_SSL
  1050. #define USETLSTAB
  1051. static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
  1052.     { "/ssl",       OPN_TLS, 0    },
  1053.     { "/tls",       OPN_TLS, 0    },
  1054.     { "", 0, 0 }
  1055. };
  1056. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  1057. #endif /* CK_SSL */
  1058. #endif /* FTP_SECURITY */
  1059.  
  1060. static struct keytab ftpswitab[] = {    /* FTP command switches */
  1061.     { "/account",   OPN_ACC, CM_ARG },
  1062.     { "/active",    OPN_ACT, 0      },
  1063.     { "/anonymous", OPN_ANO, 0      },
  1064.     { "/noinit",    OPN_NIN, 0      },
  1065.     { "/passive",   OPN_PSV, 0      },
  1066.     { "/password",  OPN_PSW, CM_ARG },
  1067.     { "/user",      OPN_USR, CM_ARG },
  1068.     { "", 0, 0 }
  1069. };
  1070. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  1071.  
  1072. /* FTP { ENABLE, DISABLE } items */
  1073.  
  1074. #define ENA_FEAT 1
  1075. #define ENA_MDTM 2
  1076. #define ENA_MLST 3
  1077. #define ENA_SIZE 4
  1078. #define ENA_AUTH 5
  1079.  
  1080. static struct keytab ftpenatab[] = {
  1081.     { "AUTH",  ENA_AUTH, 0 },
  1082.     { "FEAT",  ENA_FEAT, 0 },
  1083.     { "MDTM",  ENA_MDTM, 0 },
  1084.     { "ML",    ENA_MLST, CM_INV|CM_ABR },
  1085.     { "MLS",   ENA_MLST, CM_INV|CM_ABR },
  1086.     { "MLSD",  ENA_MLST, CM_INV },
  1087.     { "MLST",  ENA_MLST, 0 },
  1088.     { "SIZE",  ENA_SIZE, 0 },
  1089.     { "", 0, 0 }
  1090. };
  1091. static int nftpena = (sizeof(ftpenatab) / sizeof(struct keytab)) - 1;
  1092.  
  1093. /* SET FTP command keyword indices */
  1094.  
  1095. #define FTS_AUT  1                      /* Autoauthentication */
  1096. #define FTS_CRY  2                      /* Encryption */
  1097. #define FTS_LOG  3                      /* Autologin */
  1098. #define FTS_CPL  4                      /* Command protection level */
  1099. #define FTS_CFW  5                      /* Credentials forwarding */
  1100. #define FTS_DPL  6                      /* Data protection level */
  1101. #define FTS_DBG  7                      /* Debugging */
  1102. #define FTS_PSV  8                      /* Passive mode */
  1103. #define FTS_SPC  9                      /* Send port commands */
  1104. #define FTS_TYP 10                      /* (file) Type */
  1105. #define FTS_USN 11                      /* Unique server names (for files) */
  1106. #define FTS_VBM 12                      /* Verbose mode */
  1107. #define FTS_ATP 13                      /* Authentication type */
  1108. #define FTS_CNV 14                      /* Filename conversion */
  1109. #define FTS_TST 15                      /* Test (progress) messages */
  1110. #define FTS_PRM 16                      /* (file) Permissions */
  1111. #define FTS_XLA 17                      /* Charset translation */
  1112. #define FTS_CSR 18                      /* Server charset */
  1113. #define FTS_ERR 19                      /* Error action */
  1114. #define FTS_FNC 20                      /* Collision */
  1115. #define FTS_SRP 21                      /* SRP options */
  1116. #define FTS_GFT 22                      /* GET automatic file-type switching */
  1117. #define FTS_DAT 23                      /* Set file dates */
  1118. #define FTS_STO 24            /* Server time offset */
  1119. #define FTS_APW 25            /* Anonymous password */
  1120. #define FTS_DIS 26            /* File-transfer display style */
  1121.  
  1122. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1123.  
  1124. #define PUT_UPD 1                       /* Update */
  1125. #define PUT_RES 2                       /* Restart */
  1126. #define PUT_SIM 4                       /* Simulation */
  1127. #define PUT_DIF 8            /* Dates Differ */
  1128.  
  1129. static struct keytab ftpcolxtab[] = { /* SET FTP COLLISION options */
  1130. #ifndef MAC
  1131.     { "append",    XYFX_A, 0 },         /* append to old file */
  1132. #endif /* MAC */
  1133. #ifdef COMMENT
  1134.     { "ask",       XYFX_Q, 0 },         /* ask what to do (not implemented) */
  1135. #endif
  1136.     { "backup",    XYFX_B, 0 },         /* rename old file */
  1137. #ifndef MAC
  1138.     { "dates-differ", XYFX_M, 0 },    /* accept if dates differ */
  1139.     { "discard",   XYFX_D, 0 },         /* don't accept new file */
  1140.     { "no-supersede", XYFX_D, CM_INV }, /* ditto (MSK compatibility) */
  1141. #endif /* MAC */
  1142.     { "overwrite", XYFX_X, 0 },         /* overwrite the old file */
  1143.     { "rename",    XYFX_R, 0 },         /* rename the incoming file */
  1144. #ifndef MAC                             /* This crashes Mac Kermit. */
  1145.     { "update",    XYFX_U, 0 },         /* replace if newer */
  1146. #endif /* MAC */
  1147.     { "", 0, 0 }
  1148. };
  1149. static int nftpcolx = (sizeof(ftpcolxtab) / sizeof(struct keytab)) - 1;
  1150.  
  1151.  
  1152. #ifdef FTP_SECURITY
  1153. /* FTP authentication options */
  1154.  
  1155. #define FTA_AUTO 0                      /* Auto */
  1156. #define FTA_SRP  1                      /* SRP */
  1157. #define FTA_GK5  2                      /* Kerberos 5 */
  1158. #define FTA_K4   3                      /* Kerberos 4 */
  1159. #define FTA_SSL  4                      /* SSL */
  1160. #define FTA_TLS  5                      /* TLS */
  1161.  
  1162. /* FTP authentication types */
  1163.  
  1164. #define FTPATYPS 8
  1165. static int ftp_auth_type[FTPATYPS] = {
  1166. #ifdef FTP_GSSAPI
  1167.     FTA_GK5,                            /* GSSAPI Kerberos 5 */
  1168. #endif /* FTP_GK5 */
  1169. #ifdef FTP_SRP
  1170.     FTA_SRP,                            /* SRP */
  1171. #endif /* FTP_SRP */
  1172. #ifdef FTP_KRB4
  1173.     FTA_K4,                             /* Kerberos 4 */
  1174. #endif /* FTP_KRB4 */
  1175. #ifdef CK_SSL
  1176.     FTA_TLS,                            /* TLS */
  1177.     FTA_SSL,                            /* SSL */
  1178. #endif /* CK_SSL */
  1179.     0
  1180. };
  1181.  
  1182. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1183.     { "automatic", FTA_AUTO,  CM_INV },
  1184. #ifdef FTP_GSSAPI
  1185.     { "gssapi-krb5", FTA_GK5, 0 },
  1186. #endif /* FTP_GSSAPI */
  1187. #ifdef FTP_KRB4
  1188.     { "k4",       FTA_K4,     CM_INV },
  1189. #endif /* FTP_KRB4 */
  1190. #ifdef FTP_GSSAPI
  1191.     { "k5",        FTA_GK5,   CM_INV },
  1192. #endif /* FTP_GSSAPI */
  1193. #ifdef FTP_KRB4
  1194.     { "kerberos4", FTA_K4,    0 },
  1195. #endif /* FTP_KRB4 */
  1196. #ifdef FTP_GSSAPI
  1197.     { "kerberos5", FTA_GK5,   CM_INV },
  1198. #endif /* FTP_GSSAPI */
  1199. #ifdef FTP_KRB4
  1200.     { "kerberos_iv",FTA_K4,   CM_INV },
  1201. #endif /* FTP_KRB4 */
  1202. #ifdef FTP_GSSAPI
  1203.     { "kerberos_v", FTA_GK5,  CM_INV },
  1204. #endif /* FTP_GSSAPI */
  1205. #ifdef FTP_KRB4
  1206.     { "krb4",     FTA_K4,     CM_INV },
  1207. #endif /* FTP_KRB4 */
  1208. #ifdef FTP_GSSAPI
  1209.     { "krb5",     FTA_GK5,    CM_INV },
  1210. #endif /* FTP_GSSAPI */
  1211. #ifdef FTP_SRP
  1212.     { "srp",      FTA_SRP,     0 },
  1213. #endif /* FTP_SRP */
  1214. #ifdef CK_SSL
  1215.     { "ssl",      FTA_SSL,     0 },
  1216.     { "tls",      FTA_TLS,     0 },
  1217. #endif /* CK_SSL */
  1218.     { "", 0, 0 }
  1219. };
  1220. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1221.  
  1222. #ifdef FTP_SRP
  1223. #define SRP_CIPHER 1
  1224. #define SRP_HASH   2
  1225. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1226.     { "cipher",   SRP_CIPHER,     0 },
  1227.     { "hash",     SRP_HASH,       0 },
  1228.     { "", 0, 0 }
  1229. };
  1230. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1231. #endif /* FTP_SRP */
  1232. #endif /* FTP_SECURITY */
  1233.  
  1234. static struct keytab ftpset[] = {       /* SET FTP commmand table */
  1235.     { "anonymous-password",       FTS_APW, 0 },
  1236. #ifdef FTP_SECURITY
  1237.     { "authtype",                 FTS_ATP, 0 },
  1238.     { "autoauthentication",       FTS_AUT, 0 },
  1239.     { "autoencryption",           FTS_CRY, 0 },
  1240. #endif /* FTP_SECURITY */
  1241.     { "autologin",                FTS_LOG, 0 },
  1242. #ifndef NOCSETS
  1243.     { "character-set-translation",FTS_XLA, 0 },
  1244. #endif /* NOCSETS */
  1245.     { "collision",                FTS_FNC, 0 },
  1246. #ifdef FTP_SECURITY
  1247.     { "command-protection-level", FTS_CPL, 0 },
  1248.     { "cpl",                      FTS_CPL, CM_INV },
  1249.     { "credential-forwarding",    FTS_CFW, 0 },
  1250.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1251.     { "data-protection-level",    FTS_DPL, 0 },
  1252. #endif /* FTP_SECURITY */
  1253.     { "dates",                    FTS_DAT, 0 },
  1254.     { "debug",                    FTS_DBG, 0 },
  1255.     { "display",                  FTS_DIS, 0 },
  1256. #ifdef FTP_SECURITY
  1257.     { "dpl",                      FTS_DPL, CM_INV },
  1258. #endif /* FTP_SECURITY */
  1259.     { "error-action",             FTS_ERR, 0 },
  1260.     { "filenames",                FTS_CNV, 0 },
  1261.     { "get-filetype-switching",   FTS_GFT, 0 },
  1262.     { "passive-mode",             FTS_PSV, 0 },
  1263.     { "pasv",                     FTS_PSV, CM_INV },
  1264.     { "permissions",              FTS_PRM, 0 },
  1265.     { "progress-messages",        FTS_TST, 0 },
  1266.     { "send-port-commands",       FTS_SPC, 0 },
  1267. #ifndef NOCSETS
  1268.     { "server-character-set",     FTS_CSR, 0 },
  1269. #endif /* NOCSETS */
  1270.     { "server-time-offset",       FTS_STO, 0 },
  1271. #ifdef FTP_SRP
  1272.     { "srp",                      FTS_SRP, 0 },
  1273. #else
  1274.     { "srp",                      FTS_SRP, CM_INV },
  1275. #endif /* FTP_SRP */
  1276.     { "type",                     FTS_TYP, 0 },
  1277.     { "unique-server-names",      FTS_USN, 0 },
  1278.     { "verbose-mode",             FTS_VBM, 0 },
  1279.     { "", 0, 0 }
  1280. };
  1281. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1282.  
  1283. /*
  1284.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1285.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1286.   Don't just make up new ones, since the number of SND_xxx options must be
  1287.   known in advance for the switch-parsing arrays.
  1288. */
  1289. #define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
  1290. #define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
  1291. #define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
  1292.  
  1293. static struct keytab putswi[] = {       /* FTP PUT switch table */
  1294.     { "/after",                SND_AFT, CM_ARG },
  1295. #ifdef PUTARRAY
  1296.     { "/array",                SND_ARR, CM_ARG },
  1297. #endif /* PUTARRAY */
  1298.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1299.     { "/as-name",              SND_ASN, CM_ARG },
  1300.     { "/ascii",                SND_TXT, CM_INV },
  1301.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1302.     { "/before",               SND_BEF, CM_ARG },
  1303.     { "/binary",               SND_BIN, 0 },
  1304. #ifdef PUTPIPE
  1305.     { "/command",              SND_CMD, CM_PSH },
  1306. #endif /* PUTPIPE */
  1307. #ifdef COMMENT
  1308. /* This works but it's dangerous */
  1309. #ifdef DOUPDATE
  1310.     { "/dates-differ",         SND_DIF, CM_INV },
  1311. #endif /* DOUPDATE */
  1312. #endif /* COMMENT */
  1313.     { "/delete",               SND_DEL, 0 },
  1314. #ifdef UNIXOROSK
  1315.     { "/dotfiles",             SND_DOT, 0 },
  1316. #endif /* UNIXOROSK */
  1317.     { "/error-action",         SND_ERR, CM_ARG },
  1318.     { "/except",               SND_EXC, CM_ARG },
  1319.     { "/filenames",            SND_NAM, CM_ARG },
  1320. #ifdef PIPESEND
  1321. #ifndef NOSPL
  1322.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1323. #endif /* NOSPL */
  1324. #endif /* PIPESEND */
  1325. #ifdef CKSYMLINK
  1326.     { "/followlinks",          SND_LNK, 0 },
  1327. #endif /* CKSYMLINK */
  1328. #ifdef VMS
  1329.     { "/image",                SND_IMG, 0 },
  1330. #else
  1331.     { "/image",                SND_BIN, CM_INV },
  1332. #endif /* VMS */
  1333.     { "/larger-than",          SND_LAR, CM_ARG },
  1334.     { "/listfile",             SND_FIL, CM_ARG },
  1335. #ifndef NOCSETS
  1336.     { "/local-character-set",  SND_CSL, CM_ARG },
  1337. #endif /* NOCSETS */
  1338. #ifdef CK_TMPDIR
  1339.     { "/move-to",              SND_MOV, CM_ARG },
  1340. #endif /* CK_TMPDIR */
  1341.     { "/nobackupfiles",        SND_NOB, 0 },
  1342. #ifdef UNIXOROSK
  1343.     { "/nodotfiles",           SND_NOD, 0 },
  1344. #endif /* UNIXOROSK */
  1345. #ifdef CKSYMLINK
  1346.     { "/nofollowlinks",        SND_NLK, 0 },
  1347. #endif /* CKSYMLINK */
  1348.  
  1349.     { "/not-after",            SND_NAF, CM_ARG },
  1350.     { "/not-before",           SND_NBE, CM_ARG },
  1351. #ifdef UNIX
  1352.     { "/permissions",          SND_PRM, CM_ARG },
  1353. #else
  1354.     { "/permissions",          SND_PRM, CM_INV },
  1355. #endif /* UNIX */
  1356.     { "/quiet",                SND_SHH, 0 },
  1357. #ifdef FTP_RESTART
  1358.     { "/recover",              SND_RES, 0 },
  1359. #endif /* FTP_RESTART */
  1360. #ifdef RECURSIVE
  1361.     { "/recursive",            SND_REC, 0 },
  1362. #endif /* RECURSIVE */
  1363.     { "/rename-to",            SND_REN, CM_ARG },
  1364. #ifdef FTP_RESTART
  1365.     { "/restart",              SND_RES, CM_INV },
  1366. #endif /* FTP_RESTART */
  1367. #ifndef NOCSETS
  1368.     { "/server-character-set", SND_CSR, CM_ARG },
  1369. #endif /* NOCSETS */
  1370.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1371.     { "/simulate",             SND_SIM, 0 },
  1372.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1373.     { "/smaller-than",         SND_SMA, CM_ARG },
  1374. #ifdef COMMENT
  1375.     { "/starting-at",          SND_STA, CM_ARG },
  1376. #endif /* COMMENT */
  1377. #ifdef RECURSIVE
  1378.     { "/subdirectories",       SND_REC, CM_INV },
  1379. #endif /* RECURSIVE */
  1380.     { "/tenex",                SND_TEN, 0 },
  1381.     { "/text",                 SND_TXT, 0 },
  1382. #ifndef NOCSETS
  1383.     { "/transparent",          SND_XPA, 0 },
  1384. #endif /* NOCSETS */
  1385.     { "/type",                 SND_TYP, CM_ARG },
  1386. #ifdef DOUPDATE
  1387.     { "/update",               SND_UPD, 0 },
  1388. #endif /* DOUPDATE */
  1389.     { "/unique-server-names",  SND_USN, 0 },
  1390.     { "", 0, 0 }
  1391. };
  1392. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1393.  
  1394. static struct keytab getswi[] = {       /* FTP [M]GET switch table */
  1395.     { "/after",                SND_AFT, CM_INV },
  1396.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1397.     { "/as-name",              SND_ASN, CM_ARG },
  1398.     { "/ascii",                SND_TXT, CM_INV },
  1399.     { "/before",               SND_BEF, CM_INV },
  1400.     { "/binary",               SND_BIN, 0 },
  1401.     { "/collision",            SND_COL, CM_ARG },
  1402. #ifdef PUTPIPE
  1403.     { "/command",              SND_CMD, CM_PSH },
  1404. #endif /* PUTPIPE */
  1405.     { "/delete",               SND_DEL, 0 },
  1406.     { "/error-action",         SND_ERR, CM_ARG },
  1407.     { "/except",               SND_EXC, CM_ARG },
  1408.     { "/filenames",            SND_NAM, CM_ARG },
  1409. #ifdef PIPESEND
  1410. #ifndef NOSPL
  1411.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1412. #endif /* NOSPL */
  1413. #endif /* PIPESEND */
  1414. #ifdef VMS
  1415.     { "/image",                SND_IMG, 0 },
  1416. #else
  1417.     { "/image",                SND_BIN, CM_INV },
  1418. #endif /* VMS */
  1419.     { "/larger-than",          SND_LAR, CM_ARG },
  1420.     { "/listfile",             SND_FIL, CM_ARG },
  1421. #ifndef NOCSETS
  1422.     { "/local-character-set",  SND_CSL, CM_ARG },
  1423. #endif /* NOCSETS */
  1424.     { "/match",                SND_PAT, CM_ARG },
  1425.     { "/ml",                   SND_MLS, CM_INV|CM_ABR },
  1426.     { "/mls",                  SND_MLS, CM_INV|CM_ABR },
  1427.     { "/mlsd",                 SND_MLS, 0 },
  1428.     { "/mlst",                 SND_MLS, CM_INV },
  1429. #ifdef CK_TMPDIR
  1430.     { "/move-to",              SND_MOV, CM_ARG },
  1431. #endif /* CK_TMPDIR */
  1432.     { "/namelist",             SND_NML, CM_ARG },
  1433.     { "/nlst",                 SND_NLS, 0 },
  1434.     { "/nobackupfiles",        SND_NOB, 0 },
  1435.     { "/nodotfiles",           SND_NOD, 0 },
  1436. #ifdef DOUPDATE
  1437.     { "/dates-differ",         SND_DIF, CM_INV },
  1438. #endif /* DOUPDATE */
  1439.     { "/not-after",            SND_NAF, CM_INV },
  1440.     { "/not-before",           SND_NBE, CM_INV },
  1441.     { "/permissions",          SND_PRM, CM_INV },
  1442.     { "/quiet",                SND_SHH, 0 },
  1443. #ifdef FTP_RESTART
  1444.     { "/recover",              SND_RES, 0 },
  1445. #endif /* FTP_RESTART */
  1446. #ifdef RECURSIVE
  1447.     { "/recursive",            SND_REC, 0 },
  1448. #endif /* RECURSIVE */
  1449.     { "/rename-to",            SND_REN, CM_ARG },
  1450. #ifdef FTP_RESTART
  1451.     { "/restart",              SND_RES, CM_INV },
  1452. #endif /* FTP_RESTART */
  1453. #ifndef NOCSETS
  1454.     { "/server-character-set", SND_CSR, CM_ARG },
  1455. #endif /* NOCSETS */
  1456.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1457.     { "/smaller-than",         SND_SMA, CM_ARG },
  1458. #ifdef RECURSIVE
  1459.     { "/subdirectories",       SND_REC, CM_INV },
  1460. #endif /* RECURSIVE */
  1461.     { "/text",                 SND_TXT, 0 },
  1462.     { "/tenex",                SND_TEN, 0 },
  1463. #ifndef NOCSETS
  1464.     { "/transparent",          SND_XPA, 0 },
  1465. #endif /* NOCSETS */
  1466.     { "/to-screen",            SND_MAI, 0 },
  1467. #ifdef DOUPDATE
  1468.     { "/update",               SND_UPD, CM_INV },
  1469. #endif /* DOUPDATE */
  1470.     { "", 0, 0 }
  1471. };
  1472. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1473.  
  1474. static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
  1475.     { "/error-action",         SND_ERR, CM_ARG },
  1476.     { "/except",               SND_EXC, CM_ARG },
  1477.     { "/filenames",            SND_NAM, CM_ARG },
  1478.     { "/larger-than",          SND_LAR, CM_ARG },
  1479.     { "/nobackupfiles",        SND_NOB, 0 },
  1480. #ifdef UNIXOROSK
  1481.     { "/nodotfiles",           SND_NOD, 0 },
  1482. #endif /* UNIXOROSK */
  1483.     { "/quiet",                SND_SHH, 0 },
  1484. #ifdef RECURSIVE
  1485.     { "/recursive",            SND_REC, 0 },
  1486. #endif /* RECURSIVE */
  1487.     { "/smaller-than",         SND_SMA, CM_ARG },
  1488. #ifdef RECURSIVE
  1489.     { "/subdirectories",       SND_REC, CM_INV },
  1490. #endif /* RECURSIVE */
  1491.     { "", 0, 0 }
  1492. };
  1493. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1494.  
  1495. static struct keytab fntab[] = {        /* Filename conversion keyword table */
  1496.     { "automatic",    2, CNV_AUTO },
  1497.     { "converted",    1, CNV_CNV  },
  1498.     { "literal",      0, CNV_LIT  }
  1499. };
  1500. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1501.  
  1502. static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
  1503.     { "ascii",        FTT_ASC, 0 },
  1504.     { "binary",       FTT_BIN, 0 },
  1505.     { "tenex",        FTT_TEN, 0 },
  1506.     { "text",         FTT_ASC, CM_INV },
  1507.     { "", 0, 0 }
  1508. };
  1509. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1510.  
  1511. #ifdef FTP_SECURITY
  1512. static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
  1513.     { "clear",        FPL_CLR, 0 },
  1514.     { "confidential", FPL_CON, 0 },
  1515.     { "private",      FPL_PRV, 0 },
  1516.     { "safe",         FPL_SAF, 0 },
  1517.     { "", 0, 0 }
  1518. };
  1519. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1520. #endif /* FTP_SECURITY */
  1521.  
  1522. /* Definitions for FTP from RFC765. */
  1523.  
  1524. /* Reply codes */
  1525.  
  1526. #define REPLY_PRELIM    1               /* Positive preliminary */
  1527. #define REPLY_COMPLETE  2               /* Positive completion */
  1528. #define REPLY_CONTINUE  3               /* Positive intermediate */
  1529. #define REPLY_TRANSIENT 4               /* Transient negative completion */
  1530. #define REPLY_ERROR     5               /* Permanent negative completion */
  1531. #define REPLY_SECURE    6               /* Security encoded message */
  1532.  
  1533. /* Form codes and names */
  1534.  
  1535. #define FORM_N 1                        /* Non-print */
  1536. #define FORM_T 2                        /* Telnet format effectors */
  1537. #define FORM_C 3                        /* Carriage control (ASA) */
  1538.  
  1539. /* Structure codes and names */
  1540.  
  1541. #define STRU_F 1                        /* File (no record structure) */
  1542. #define STRU_R 2                        /* Record structure */
  1543. #define STRU_P 3                        /* Page structure */
  1544.  
  1545. /* Mode types and names */
  1546.  
  1547. #define MODE_S 1                        /* Stream */
  1548. #define MODE_B 2                        /* Block */
  1549. #define MODE_C 3                        /* Compressed */
  1550.  
  1551. /* Protection levels and names */
  1552.  
  1553. #define PROT_C 1                        /* Clear */
  1554. #define PROT_S 2                        /* Safe */
  1555. #define PROT_P 3                        /* Private */
  1556. #define PROT_E 4                        /* Confidential */
  1557.  
  1558. #ifdef COMMENT                          /* Not used */
  1559. #ifdef FTP_NAMES
  1560. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1561. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1562. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1563. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1564. #endif /* FTP_NAMES */
  1565.  
  1566. /* Record Tokens */
  1567.  
  1568. #define REC_ESC '\377'                  /* Record-mode Escape */
  1569. #define REC_EOR '\001'                  /* Record-mode End-of-Record */
  1570. #define REC_EOF '\002'                  /* Record-mode End-of-File */
  1571.  
  1572. /* Block Header */
  1573.  
  1574. #define BLK_EOR           0x80          /* Block is End-of-Record */
  1575. #define BLK_EOF           0x40          /* Block is End-of-File */
  1576. #define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
  1577. #define BLK_RESTART       0x10          /* Block is Restart Marker */
  1578. #define BLK_BYTECOUNT 2                 /* Bytes in this block */
  1579. #endif /* COMMENT */
  1580.  
  1581. #define RADIX_ENCODE 0                  /* radix_encode() function codes */
  1582. #define RADIX_DECODE 1
  1583.  
  1584. /*
  1585.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1586.   results in a serious performance degradation due to the increased number
  1587.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1588.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1589.   of the typical TCP window.  Maybe we should add a command to allow the value
  1590.   to be changed.
  1591. */
  1592. #define DEFAULT_PBSZ 1<<13
  1593.  
  1594. /* Definitions and typedefs needed for prototypes */
  1595.  
  1596. #define sig_t my_sig_t
  1597. #define sigtype SIGTYP
  1598. typedef sigtype (*sig_t)();
  1599.  
  1600. /* Prototypes */
  1601.  
  1602. _PROTOTYP(int remtxt, (char **) );
  1603. _PROTOTYP(char * gskreason, (int) );
  1604. _PROTOTYP(static int ftpclose,(void));
  1605. _PROTOTYP(static int zzsend, (int, CHAR));
  1606. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1607. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1608. _PROTOTYP(static int setpbsz,(unsigned int));
  1609. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1610.   int,int,char *,int,int,int));
  1611. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1612. _PROTOTYP(static int fts_cpl,(int));
  1613. _PROTOTYP(static int fts_dpl,(int));
  1614. #ifdef FTP_SECURITY
  1615. _PROTOTYP(static int ftp_auth, (void));
  1616. #endif /* FTP_SECURITY */
  1617. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1618. _PROTOTYP(static int ftp_login, (char *));
  1619. _PROTOTYP(static int ftp_reset, (void));
  1620. _PROTOTYP(static int ftp_rename, (char *, char *));
  1621. _PROTOTYP(static int ftp_umask, (char *));
  1622. _PROTOTYP(static int secure_flush, (int));
  1623. #ifdef COMMENT
  1624. _PROTOTYP(static int secure_putc, (char, int));
  1625. #endif /* COMMENT */
  1626. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1627. _PROTOTYP(static int scommand, (char *));
  1628. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1629. _PROTOTYP(static int secure_getc, (int, int));
  1630. _PROTOTYP(static int secure_getbyte, (int, int));
  1631. _PROTOTYP(static int secure_read, (int, char *, int));
  1632. _PROTOTYP(static int initconn, (void));
  1633. _PROTOTYP(static int dataconn, (char *));
  1634. _PROTOTYP(static int setprotbuf,(unsigned int));
  1635. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1636.  
  1637. _PROTOTYP(static char * radix_error,(int));
  1638. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1639. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, CHAR *, int));
  1640.  
  1641. _PROTOTYP(static VOID mlsreset, (void));
  1642. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1643. _PROTOTYP(static VOID lostpeer, (void));
  1644. _PROTOTYP(static VOID cancel_remote, (int));
  1645. _PROTOTYP(static VOID changetype, (int, int));
  1646.  
  1647. _PROTOTYP(static sigtype cmdcancel, (int));
  1648.  
  1649. #ifdef FTP_SRP
  1650. _PROTOTYP(static int srp_reset, ());
  1651. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1652. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1653. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1654. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1655. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1656. _PROTOTYP(static int srp_selcipher, (char *));
  1657. _PROTOTYP(static int srp_selhash, (char *));
  1658. #endif /* FTP_SRP */
  1659.  
  1660. #ifdef FTP_GSSAPI
  1661. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1662. #endif /* FTP_GSSAPI */
  1663.  
  1664. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1665.  
  1666. #ifdef FTP_SECURITY
  1667. #ifndef NOICP
  1668. #define FT_NOGSS   1
  1669. #define FT_NOK4    2
  1670. #define FT_NOSRP   3
  1671. #define FT_NOSSL   4
  1672. #define FT_NOTLS   5
  1673. #define FT_CERTFI  6
  1674. #define FT_OKCERT  7
  1675. #define FT_DEBUG   8
  1676. #define FT_KEY     9
  1677. #define FT_SECURE 10
  1678. #define FT_VERIFY 11
  1679.  
  1680. static struct keytab ftpztab[] = {
  1681.     { "!gss",    FT_NOGSS,  0 },
  1682.     { "!krb4",   FT_NOK4,   0 },
  1683.     { "!srp",    FT_NOSRP,  0 },
  1684.     { "!ssl",    FT_NOSSL,  0 },
  1685.     { "!tls",    FT_NOTLS,  0 },
  1686.     { "cert",    FT_CERTFI, CM_ARG },
  1687.     { "certsok", FT_OKCERT, 0 },
  1688.     { "debug",   FT_DEBUG,  0 },
  1689.     { "key",     FT_KEY,    CM_ARG },
  1690.     { "nogss",   FT_NOGSS,  0 },
  1691.     { "nokrb4",  FT_NOK4,   0 },
  1692.     { "nosrp",   FT_NOSRP,  0 },
  1693.     { "nossl",   FT_NOSSL,  0 },
  1694.     { "notls",   FT_NOTLS,  0 },
  1695. #ifdef COMMENT
  1696.     { "secure",  FT_SECURE, 0 },
  1697. #endif /* COMMENT */
  1698.     { "verify",  FT_VERIFY, CM_ARG },
  1699.     { "", 0, 0 }
  1700. };
  1701. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1702.  
  1703. /*
  1704.   The following cipher and hash tables should be replaced with
  1705.   dynamicly created versions based upon the linked library.
  1706. */
  1707. #define SRP_BLOWFISH_ECB    1
  1708. #define SRP_BLOWFISH_CBC    2
  1709. #define SRP_BLOWFISH_CFB64  3
  1710. #define SRP_BLOWFISH_OFB64  4
  1711. #define SRP_CAST5_ECB       5
  1712. #define SRP_CAST5_CBC       6
  1713. #define SRP_CAST5_CFB64     7
  1714. #define SRP_CAST5_OFB64     8
  1715. #define SRP_DES_ECB         9
  1716. #define SRP_DES_CBC        10
  1717. #define SRP_DES_CFB64      11
  1718. #define SRP_DES_OFB64      12
  1719. #define SRP_DES3_ECB       13
  1720. #define SRP_DES3_CBC       14
  1721. #define SRP_DES3_CFB64     15
  1722. #define SRP_DES3_OFB64     16
  1723.  
  1724. static struct keytab ciphertab[] = {
  1725.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1726.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1727.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1728.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1729.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1730.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1731.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1732.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1733.     { "des_ecb",        SRP_DES_ECB,        0 },
  1734.     { "des_cbc",        SRP_DES_CBC,        0 },
  1735.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1736.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1737.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1738.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1739.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1740.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1741.     { "none",           0, 0 },
  1742.     { "", 0, 0 }
  1743. };
  1744. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1745.  
  1746. #define SRP_MD5  1
  1747. #define SRP_SHA  2
  1748. static struct keytab hashtab[] = {
  1749.     { "md5",              SRP_MD5,        0 },
  1750.     { "none",             0,              0 },
  1751.     { "sha",              SRP_SHA,        0 },
  1752.     { "", 0, 0 }
  1753. };
  1754. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1755. #endif /* NOICP */
  1756. #endif /* FTP_SECURITY */
  1757.  
  1758. static char *
  1759. strval(s1,s2) char * s1, * s2; {
  1760.     if (!s1) s1 = "";
  1761.     if (!s2) s2 = "";
  1762.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1763. }
  1764.  
  1765. #ifndef NOCSETS
  1766. static char * rfnptr = NULL;
  1767. static int rfnlen = 0;
  1768. static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
  1769. static char * xgnbp = NULL;
  1770.  
  1771. static int
  1772. strgetc() {                             /* Helper function for xgnbyte() */
  1773.     int c;
  1774.     if (!xgnbp)
  1775.       return(-1);
  1776.     if (!*xgnbp)
  1777.       return(-1);
  1778.     c = (unsigned) *xgnbp++;
  1779.     return(((unsigned) c) & 0xff);
  1780. }
  1781.  
  1782. static int                              /* Helper function for xpnbyte() */
  1783. #ifdef CK_ANSIC
  1784. strputc(char c)
  1785. #else
  1786. strputc(c) char c;
  1787. #endif /* CK_ANSIC */
  1788. {
  1789.     rfnlen = rfnptr - rfnbuf;
  1790.     if (rfnlen >= (RFNBUFSIZ - 1))
  1791.       return(-1);
  1792.     *rfnptr++ = c;
  1793.     *rfnptr = NUL;
  1794.     return(0);
  1795. }
  1796.  
  1797. static int
  1798. #ifdef CK_ANSIC
  1799. xprintc(char c)
  1800. #else
  1801. xprintc(c) char c;
  1802. #endif /* CK_ANSIC */
  1803. {
  1804.     printf("%c",c);
  1805.     return(0);
  1806. }
  1807.  
  1808. static VOID
  1809. bytswap(c0,c1) int * c0, * c1; {
  1810.     int t;
  1811.     t = *c0;
  1812.     *c0 = *c1;
  1813.     *c1 = t;
  1814. }
  1815. #endif /* NOCSETS */
  1816.  
  1817. #ifdef CKLOGDIAL
  1818. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  1819. int ftplogactive = 0;
  1820. long ftplogprev = 0L;
  1821.  
  1822. VOID
  1823. ftplogend() {
  1824.     extern int dialog;
  1825.     extern char diafil[];
  1826.     long d1, d2, t1, t2;
  1827.     char buf[32], * p;
  1828.  
  1829.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  1830.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  1831.  
  1832.     if (!ftplogactive || !ftplogbuf[0]) /* No active record */
  1833.       return;
  1834.  
  1835.     ftplogactive = 0;                   /* Record is not active */
  1836.  
  1837.     d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
  1838.     ckstrncpy(buf,ckdate(),31);         /* Get current date */
  1839.     d2 = mjd(buf);                      /* Convert them to mjds */
  1840.     p = ftplogbuf;                      /* Get start time */
  1841.     p[11] = NUL;
  1842.     p[14] = NUL;                        /* Convert to seconds */
  1843.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1844.     p[11] = ':';
  1845.     p[14] = ':';
  1846.     p = buf;                            /* Get end time */
  1847.     p[11] = NUL;
  1848.     p[14] = NUL;
  1849.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1850.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  1851.     if (t2 > -1L) {
  1852.         ftplogprev = t2;
  1853.         p = hhmmss(t2);
  1854.         strncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  1855.         strncat(ftplogbuf,p,CXLOGBUFL);
  1856.     } else
  1857.       ftplogprev = 0L;
  1858.     debug(F101,"ftp cx log dialog","",dialog);
  1859.     if (dialog) {                       /* If logging */
  1860.         int x;
  1861.         x = diaopn(diafil,1,1);         /* Open log in append mode */
  1862.         if (x > 0) {
  1863.             debug(F101,"ftp cx log open","",x);
  1864.             x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  1865.             debug(F101,"ftp cx log write","",x);
  1866.             x = zclose(ZDIFIL);         /* Close the log */
  1867.             debug(F101,"ftp cx log close","",x);
  1868.         }
  1869.     }
  1870. }
  1871.  
  1872. VOID
  1873. dologftp() {
  1874.     ftplogend();                        /* Previous session not closed out? */
  1875.     ftplogprev = 0L;
  1876.     ftplogactive = 1;                   /* Record is active */
  1877.  
  1878.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  1879.               ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  1880.               " T=FTP N=", strval(ftp_host,NULL)," H=",myhost," ",NULL,NULL);
  1881.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  1882. }
  1883. #endif /* CKLOGDIAL */
  1884.  
  1885. static char * dummy[2] = { NULL, NULL };
  1886.  
  1887. static struct keytab modetab[] = {
  1888.     { "active",  0, 0 },
  1889.     { "passive", 1, 0 }
  1890. };
  1891.  
  1892. #ifndef NOCMDL
  1893. int                                     /* Called from ckuusy.c */
  1894. #ifdef CK_ANSIC
  1895. doftparg(char c)
  1896. #else
  1897. doftparg(c) char c;
  1898. #endif /* CK_ANSIC */
  1899. /* doftparg */ {
  1900.     int x, z;
  1901.     char *xp;
  1902.     extern char **xargv, *xarg0;
  1903.     extern int xargc, stayflg, haveftpuid;
  1904.     extern char uidbuf[];
  1905.  
  1906.     xp = *xargv+1;                      /* Pointer for bundled args */
  1907.     while (c) {
  1908.         if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
  1909.             if (*(xp+1)) {
  1910.                 fatal("?Invalid argument bundling");
  1911.             }
  1912.             xargv++, xargc--;
  1913.             if ((xargc < 1) || (**xargv == '-')) {
  1914.                 fatal("?Required argument missing");
  1915.             }
  1916.         }
  1917.         switch (c) {                    /* Big switch on arg */
  1918.           case 'h':                     /* help */
  1919.            printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  1920.             printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  1921.             printf("Options:\n");
  1922.             printf("  -h           = help (this message)\n");
  1923.             printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  1924.             printf("  -u name      = username for autologin (or -M)\n");
  1925.             printf("  -P password  = password for autologin (RISKY)\n");
  1926.             printf("  -A           = autologin anonymously\n");
  1927.             printf("  -D directory = cd after autologin\n");
  1928.             printf("  -b           = force binary mode\n");
  1929.             printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  1930.             printf("  -d           = debug (double to add timestamps)\n");
  1931.             printf("  -n           = no autologin\n");
  1932.             printf("  -v           = verbose (default)\n");
  1933.             printf("  -q           = quiet\n");
  1934.             printf("  -S           = Stay (issue command prompt when done)\n");
  1935.             printf("  -Y           = do not execute Kermit init file\n");
  1936.             printf("  -p files     = files to put after autologin (or -s)\n");
  1937.             printf("  -g files     = files to get after autologin\n");
  1938.             printf("  -R           = recursive (for use with -p)\n");
  1939.  
  1940. #ifdef FTP_SECURITY
  1941.             printf("\nSecurity options:\n");
  1942.             printf("  -k realm     = Kerberos 4 realm\n");
  1943.             printf("  -f           = Kerboros 5 credentials forwarding\n");
  1944.             printf("  -x           = autoencryption mode\n");
  1945.             printf("  -c cipher    = SRP cipher type\n");
  1946.             printf("  -H hash      = SRP encryption hash\n");
  1947.             printf("  -z option    = Security options\n");
  1948. #endif /* FTP_SECURITY */
  1949.  
  1950.             printf("\n-p or -g, if given, should be last.  Example:\n");
  1951.             printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  1952.  
  1953.             doexit(GOOD_EXIT,-1);
  1954.             break;
  1955.  
  1956.           case 'R':                     /* Recursive */
  1957.             recursive = 1;
  1958.             break;
  1959.  
  1960.           case 'd':                     /* Debug */
  1961. #ifdef DEBUG
  1962.             if (deblog) {
  1963.                 extern int debtim;
  1964.                 debtim = 1;
  1965.             } else {
  1966.                 deblog = debopn("debug.log",0);
  1967.                 debok = 1;
  1968.             }
  1969. #endif /* DEBUG */
  1970.             /* fall thru on purpose */
  1971.  
  1972.           case 't':                     /* Trace */
  1973.             ftp_deb++;
  1974.             break;
  1975.  
  1976.           case 'n':                     /* No autologin */
  1977.             ftp_log = 0;
  1978.             break;
  1979.  
  1980.           case 'i':                     /* No prompt */
  1981.           case 'v':                     /* Verbose */
  1982.             break;                      /* (ignored) */
  1983.  
  1984.           case 'q':                     /* Quiet */
  1985.             quiet = 1;
  1986.             break;
  1987.  
  1988.           case 'S':                     /* Stay */
  1989.             stayflg = 1;
  1990.             break;
  1991.  
  1992.           case 'M':
  1993.           case 'u':                     /* My User Name */
  1994.             if ((int)strlen(*xargv) > 63) {
  1995.                 fatal("username too long");
  1996.             }
  1997.             ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  1998.             haveftpuid = 1;
  1999.             break;
  2000.  
  2001.           case 'A':
  2002.             ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2003.             haveftpuid = 1;
  2004.             break;
  2005.  
  2006.           case 'T':                     /* Text */
  2007.           case 'a':                     /* "ascii" */
  2008.           case 'b':                     /* Binary */
  2009.             binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  2010.             xfermode = XMODE_M;
  2011.             filepeek = 0;
  2012.             patterns = 0;
  2013.             break;
  2014.  
  2015.           case 'g':                     /* Get */
  2016.           case 'p':                     /* Put */
  2017.           case 's': {                   /* Send (= Put) */
  2018.               int havefiles, rc;
  2019.               if (ftp_action) {
  2020.                   fatal("Only one FTP action at a time please");
  2021.               }
  2022.               if (*(xp+1)) {
  2023.                   fatal("invalid argument bundling after -s");
  2024.               }
  2025.               nfils = 0;                /* Initialize file counter */
  2026.               havefiles = 0;            /* Assume nothing to send  */
  2027.               cmlist = xargv + 1;       /* Remember this pointer */
  2028.  
  2029.               while (++xargv, --xargc > 0) { /* Traverse the list */
  2030.                   if (c == 'g') {
  2031.                       havefiles++;
  2032.                       nfils++;
  2033.                       continue;
  2034.                   }
  2035. #ifdef RECURSIVE
  2036.                   if (!strcmp(*xargv,".")) {
  2037.                       havefiles = 1;
  2038.                       nfils++;
  2039.                       recursive = 1;
  2040.                   } else
  2041. #endif /* RECURSIVE */
  2042.                     if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  2043.                         if  (rc != -2)
  2044.                           havefiles = 1;
  2045.                         nfils++;
  2046.                     } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  2047.                         havefiles = 1;
  2048.                         nfils++;
  2049.                     }
  2050.               }
  2051.               xargc++, xargv--;         /* Adjust argv/argc */
  2052.               if (!havefiles) {
  2053.                   if (c == 'g') {
  2054.                       fatal("No files to put");
  2055.                   } else {
  2056.                       fatal("No files to get");
  2057.                   }
  2058.               }
  2059.               ftp_action = c;
  2060.               break;
  2061.           }
  2062.           case 'D':                     /* Directory */
  2063.             makestr(&ftp_rdir,*xargv);
  2064.             break;
  2065.  
  2066.           case 'm':                     /* Mode (Active/Passive */
  2067.             ftp_psv = lookup(modetab,*xargv,2,NULL);
  2068.             if (ftp_psv < 0) fatal("Invalid mode");
  2069.             break;
  2070.  
  2071.           case 'P':
  2072.             makestr(&ftp_tmp,*xargv);   /* You-Know-What */
  2073.             break;
  2074.  
  2075.           case 'Y':                     /* No initialization file */
  2076.             break;                      /* (already done in prescan) */
  2077.  
  2078. #ifdef CK_URL
  2079.           case 'U': {                   /* URL */
  2080.               /* These are set by urlparse() - any not set are NULL */
  2081.               if (g_url.hos) {
  2082. /*
  2083.   Kermit has accepted host:port notation since many years before URLs were
  2084.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  2085.   host:449" looks like a URL and results in service = host and host = 449.
  2086.   Here we try to catch this situation transparently to the user.
  2087. */
  2088.                   if (ckstrcmp(g_url.svc,"ftp",-1,0)
  2089. #ifdef CK_SSL
  2090.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  2091. #endif /* CK_SSL */
  2092.                        ) {
  2093.                       if (!g_url.usr &&
  2094.                           !g_url.psw &&
  2095.                           !g_url.por &&
  2096.                           !g_url.pth) {
  2097.                           g_url.por = g_url.hos;
  2098.                           g_url.hos = g_url.svc;
  2099.                           g_url.svc = "ftp";
  2100.                       } else {
  2101.                           ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  2102.                                    g_url.svc," host=",g_url.hos);
  2103.                           fatal(tmpbuf);
  2104.                       }
  2105.                   }
  2106.                   makestr(&ftp_host,g_url.hos);
  2107.                   if (g_url.usr) {
  2108.                       haveftpuid = 1;
  2109.                       ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  2110.                       makestr(&ftp_logname,uidbuf);
  2111.                   }
  2112.                   if (g_url.psw) {
  2113.                       makestr(&ftp_tmp,g_url.psw);
  2114.                   }
  2115.                   if (g_url.pth) {
  2116.                       if (!g_url.usr) {
  2117.                           haveftpuid = 1;
  2118.                           ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2119.                           makestr(&ftp_logname,uidbuf);
  2120.                       }
  2121.                       if (ftp_action) {
  2122.                           fatal("Only one FTP action at a time please");
  2123.                       }
  2124.                       if (!stayflg)
  2125.                         quiet = 1;
  2126.                       nfils = 1;
  2127.                       dummy[0] = g_url.pth;
  2128.                       cmlist = dummy;
  2129.                       ftp_action = 'g';
  2130.                   }
  2131.                   xp = NULL;
  2132.                   haveurl = 1;
  2133.               }
  2134.               break;
  2135.           }
  2136. #endif /* CK_URL */
  2137.  
  2138. #ifdef FTP_SECURITY
  2139.           case 'k': {                   /* K4 Realm */
  2140. #ifdef FTP_KRB4
  2141.               ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  2142. #endif /* FTP_KRB4 */
  2143.               if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  2144.               break;
  2145.           }
  2146.           case 'f': {
  2147. #ifdef FTP_GSSAPI
  2148.               ftp_cfw = 1;
  2149.               if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2150. #else /* FTP_GSSAPI */
  2151.               printf("K5 Credentials Forwarding not supported\n");
  2152. #endif /* FTP_GSSAPI */
  2153.               break;
  2154.           }
  2155.           case 'x': {
  2156.               ftp_cry = 1;
  2157.               if (ftp_deb) printf("Autoencryption\n");
  2158.               break;
  2159.           }
  2160.           case 'c': {                   /* Cipher */
  2161. #ifdef FTP_SRP
  2162.               if (!srp_selcipher(*xargv)) {
  2163.                   if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2164.               } else
  2165.                 printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2166. #else /* FTP_SRP */
  2167.               printf("?SRP not supported\n");
  2168. #endif /* FTP_SRP */
  2169.               break;
  2170.           }
  2171.           case 'H': {
  2172. #ifdef FTP_SRP
  2173.               if (!srp_selhash(*xargv)) {
  2174.                   if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2175.               } else
  2176.                 printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2177. #else /* FTP_SRP */
  2178.               printf("?SRP not supported\n");
  2179. #endif /* FTP_SRP */
  2180.               break;
  2181.           }
  2182.           case 'z': {
  2183.               /* *xargv contains a value of the form tag=value */
  2184.               /* we need to lookup the tag and save the value  */
  2185.               char * p = NULL, * q = NULL;
  2186.               makestr(&p,*xargv);
  2187.               y = ckindex("=",p,0,0,1);
  2188.               if (y > 0)
  2189.                 p[y-1] = '\0';
  2190.               x = lookup(ftpztab,p,nftpztab,&z);
  2191.               if (x < 0) {
  2192.                   printf("?Invalid security option: \"%s\"\n",p);
  2193.               } else {
  2194.                   if (ftp_deb)
  2195.             printf("Security option: \"%s",p);
  2196.                   if (ftpztab[z].flgs & CM_ARG) {
  2197.                       if (y <= 0)
  2198.                         fatal("?Missing required value");
  2199.                       q = &p[y];
  2200.                       if (!*q)
  2201.                         fatal("?Missing required value");
  2202.                       if (ftp_deb)
  2203.             printf("=%s\"",q);
  2204.                   }
  2205.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2206.                     case FT_NOGSS:
  2207. #ifdef FTP_GSSAPI
  2208.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2209.                           if (ftp_auth_type[z] == FTA_GK5) {
  2210.                               for (y = z;
  2211.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2212.                                    y++
  2213.                                    )
  2214.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2215.                               ftp_auth_type[FTPATYPS-1] = 0;
  2216.                               break;
  2217.                           }
  2218.                       }
  2219. #endif /* FTP_GSSAPI */
  2220.                       break;
  2221.                     case FT_NOK4:
  2222. #ifdef FTP_KRB4
  2223.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2224.                           if (ftp_auth_type[z] == FTA_K4) {
  2225.                               for (y = z;
  2226.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2227.                                    y++
  2228.                                    )
  2229.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2230.                               ftp_auth_type[FTPATYPS-1] = 0;
  2231.                               break;
  2232.                           }
  2233.                       }
  2234. #endif /* FTP_KRB4 */
  2235.                       break;
  2236.                     case FT_NOSRP:
  2237. #ifdef FTP_SRP
  2238.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2239.                           if (ftp_auth_type[z] == FTA_SRP) {
  2240.                               for (y = z;
  2241.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2242.                                    y++
  2243.                                    )
  2244.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2245.                               ftp_auth_type[FTPATYPS-1] = 0;
  2246.                               break;
  2247.                           }
  2248.                       }
  2249. #endif /* FTP_SRP */
  2250.                       break;
  2251.                     case FT_NOSSL:
  2252. #ifdef CK_SSL
  2253.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2254.                           if (ftp_auth_type[z] == FTA_SSL) {
  2255.                               for (y = z;
  2256.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2257.                                    y++
  2258.                                    )
  2259.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2260.                               ftp_auth_type[FTPATYPS-1] = 0;
  2261.                               break;
  2262.                           }
  2263.                       }
  2264. #endif /* CK_SSL */
  2265.                       break;
  2266.                     case FT_NOTLS:
  2267. #ifdef CK_SSL
  2268.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2269.                           if (ftp_auth_type[z] == FTA_TLS) {
  2270.                               for (y = z;
  2271.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2272.                                    y++
  2273.                                    )
  2274.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2275.                               ftp_auth_type[FTPATYPS-1] = 0;
  2276.                               break;
  2277.                           }
  2278.                       }
  2279. #endif /* CK_SSL */
  2280.                       break;
  2281.                     case FT_CERTFI:
  2282. #ifdef CK_SSL
  2283.                       makestr(&ssl_rsa_cert_file,q);
  2284. #endif /* CK_SSL */
  2285.                       break;
  2286.                     case FT_OKCERT:
  2287. #ifdef CK_SSL
  2288.                       ssl_certsok_flag = 1;
  2289. #endif /* CK_SSL */
  2290.                       break;
  2291.                     case FT_DEBUG:
  2292. #ifdef DEBUG
  2293.                       if (deblog) {
  2294.                           extern int debtim;
  2295.                           debtim = 1;
  2296.                       } else {
  2297.                           deblog = debopn("debug.log",0);
  2298.                       }
  2299. #endif /* DEBUG */
  2300.                       break;
  2301.                     case FT_KEY:
  2302. #ifdef CK_SSL
  2303.                       makestr(&ssl_rsa_key_file,q);
  2304. #endif /* CK_SSL */
  2305.                       break;
  2306.                     case FT_SECURE:
  2307.                       /* no equivalent */
  2308.                       break;
  2309.                     case FT_VERIFY:
  2310. #ifdef CK_SSL
  2311.                       if (!rdigits(q))
  2312.                         printf("?Bad number: %s\n",q);
  2313.                       ssl_verify_flag = atoi(q);
  2314. #endif /* CK_SSL */
  2315.                       break;
  2316.                   }
  2317.               }
  2318.               if (ftp_deb) printf("\"\n");
  2319.               free(p);
  2320.               break;
  2321.           }
  2322. #endif /* FTP_SECURITY */
  2323.  
  2324.           default:
  2325.             fatal2(*xargv,
  2326.                    "unknown command-line option, type \"ftp -h\" for help"
  2327.                    );
  2328.         }
  2329.         if (!xp) break;
  2330.         c = *++xp;                      /* See if options are bundled */
  2331.     }
  2332.     return(0);
  2333. }
  2334. #endif /* NOCMDL */
  2335.  
  2336. int
  2337. ftpisconnected() {
  2338.     return(connected);
  2339. }
  2340.  
  2341. int
  2342. ftpisloggedin() {
  2343.     return(connected ? loggedin : 0);
  2344. }
  2345.  
  2346. int
  2347. ftpissecure() {
  2348.     return((ftp_dpl == FPL_CLR) ? 0 : 1);
  2349. }
  2350.  
  2351. static VOID
  2352. ftscreen(n, c, z, s) int n; char c; long z; char * s; {
  2353.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2354.         if (!dpyactive) {
  2355.             ckscreen(SCR_PT,'S',0L,"");
  2356.             dpyactive = 1;
  2357.         }
  2358.         ckscreen(n,c,z,s);
  2359.     }
  2360. }
  2361.  
  2362. #ifndef OS2
  2363. /*  g m s t i m e r  --  Millisecond timer */
  2364.  
  2365. long
  2366. gmstimer() {
  2367. #ifdef HAVE_MSECS
  2368.     /* For those versions of ztime() that also set global ztmsec. */
  2369.     char *p = NULL;
  2370.     long z;
  2371.     ztime(&p);
  2372.     if (!p) return(0L);
  2373.     if (!*p) return(0L);
  2374.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2375.     return(z * 1000 + ztmsec);
  2376. #else
  2377.     return((long)time(NULL) * 1000L);
  2378. #endif /* HAVE_MSECS */
  2379. }
  2380. #endif /* OS2 */
  2381.  
  2382. /*  d o s e t f t p  --  The SET FTP command  */
  2383.  
  2384. int
  2385. dosetftp() {
  2386.     int cx;
  2387.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2388.       return(cx);
  2389.     switch (cx) {
  2390.  
  2391.       case FTS_FNC:                     /* Filename collision action */
  2392.         if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  2393.           return(x);
  2394.         if ((y = cmcfm()) < 0)
  2395.           return(y);
  2396.         ftp_fnc = x;
  2397.         return(1);
  2398.  
  2399.       case FTS_CNV:                     /* Filename conversion */
  2400.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2401.           return(x);
  2402.         if ((y = cmcfm()) < 0)
  2403.           return(y);
  2404.         ftp_cnv = x;
  2405.         return(1);
  2406.  
  2407.       case FTS_DBG:                     /* Debug messages */
  2408.         return(seton(&ftp_deb));
  2409.  
  2410.       case FTS_LOG:                     /* Auto-login */
  2411.         return(seton(&ftp_log));
  2412.  
  2413.       case FTS_PSV:                     /* Passive mode */
  2414.     return(dosetftppsv());
  2415.  
  2416.       case FTS_SPC:                     /* Send port commands */
  2417.         x = seton(&ftp_spc);
  2418.         if (x > 0) sendport = ftp_spc;
  2419.         return(x);
  2420.  
  2421.       case FTS_TYP:                     /* Type */
  2422.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2423.           return(x);
  2424.         if ((y = cmcfm()) < 0) return(y);
  2425.         ftp_typ = x;
  2426.         g_ftp_typ = x;
  2427.         tenex = (ftp_typ == FTT_TEN);
  2428.         return(1);
  2429.  
  2430.       case FTS_USN:                     /* Unique server names */
  2431.         return(seton(&ftp_usn));
  2432.  
  2433.       case FTS_VBM:                     /* Verbose mode */
  2434.         if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
  2435.           return(x);
  2436.         ftp_vbx = ftp_vbm;              /* Global sticky copy */
  2437.         return(x);
  2438.  
  2439.       case FTS_TST:                     /* "if (testing)" messages */
  2440.         return(seton(&testing));
  2441.  
  2442.       case FTS_PRM:                     /* Send permissions */
  2443.         return(setonaut(&ftp_prm));
  2444.  
  2445.       case FTS_AUT:                     /* Auto-authentication */
  2446.         return(seton(&ftp_aut));
  2447.  
  2448.       case FTS_ERR:                     /* Error action */
  2449.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2450.           return(x);
  2451.         if ((y = cmcfm()) < 0)
  2452.           return(y);
  2453.         ftp_err = x;
  2454.         return(success = 1);
  2455.  
  2456. #ifndef NOCSETS
  2457.       case FTS_XLA:                     /* Translation */
  2458.         return(seton(&ftp_xla));
  2459.  
  2460.       case FTS_CSR:                     /* Server charset */
  2461.         if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2462.           return(x);
  2463.         if ((y = cmcfm()) < 0)
  2464.           return(y);
  2465.         ftp_csr = x;
  2466.         ftp_xla = 1;                    /* Also enable translation */
  2467.         return(success = 1);
  2468. #endif /* NOCSETS */
  2469.  
  2470.       case FTS_GFT:
  2471.         return(seton(&get_auto));       /* GET-filetype-switching */
  2472.  
  2473.       case FTS_DAT:
  2474.         return(seton(&ftp_dates));      /* Set file dates */
  2475.  
  2476.       case FTS_STO: {            /* Server time offset */
  2477.       char * s, * p = NULL;
  2478.       int k;
  2479.       if ((x = cmfld("[+-]hh[:mm[:ss]]","+0",&s,xxstring)) < 0)
  2480.         return(x);
  2481.       if (!strcmp(s,"+0")) {
  2482.           s = NULL;
  2483.       } else if ((x = delta2sec(s,&k)) < 0) { /* Check format */
  2484.           printf("?Invalid time offset\n");
  2485.           return(-9);
  2486.       }
  2487.       makestr(&p,s);        /* Make a safe copy the string */
  2488.       if ((x = cmcfm()) < 0) {    /* Get confirmation */
  2489.           if (p)
  2490.         makestr(&p,NULL);
  2491.           return(x);
  2492.       }
  2493.       fts_sto = p;            /* Confirmed - set the string. */
  2494.       return(success = 1);
  2495.       }
  2496.       case FTS_APW: {
  2497.       char * s;
  2498.       if ((x = cmtxt("Text", "", &s, xxstring)) < 0)
  2499.         return(x);
  2500.       makestr(&ftp_apw, *s ? s : NULL);
  2501.       return(success = 1);
  2502.       }
  2503.  
  2504. #ifdef FTP_SECURITY
  2505.       case FTS_CRY:                     /* Auto-encryption */
  2506.         return(seton(&ftp_cry));
  2507.  
  2508.       case FTS_CFW:                     /* Credential-forwarding */
  2509.         return(seton(&ftp_cfw));
  2510.  
  2511.       case FTS_CPL:                     /* Command protection level */
  2512.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2513.         if ((y = cmcfm()) < 0) return(y);
  2514.         success = fts_cpl(x);
  2515.         return(success);
  2516.  
  2517.       case FTS_DPL:                     /* Data protection level */
  2518.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2519.         if ((y = cmcfm()) < 0) return(y);
  2520.           success = fts_dpl(x);
  2521.           return(success);
  2522.  
  2523.       case FTS_ATP: {                   /* FTP Auth Type */
  2524.           int i, j, atypes[8];
  2525.  
  2526.           for (i = 0; i < 8; i++) {
  2527.               if ((y = cmkey(ftpauth,nftpauth,"",
  2528.                              (i == 0) ? "automatic" : "",
  2529.                              xxstring)) < 0) {
  2530.                   if (y == -3)
  2531.                     break;
  2532.                   return(y);
  2533.               }
  2534.               if (i > 0 && (y == FTA_AUTO)) {
  2535.                   printf("?Choice may only be used in first position.\r\n");
  2536.                   return(-9);
  2537.               }
  2538.               for (j = 0; j < i; j++) {
  2539.                   if (atypes[j] == y) {
  2540.                       printf("\r\n?Choice has already been used.\r\n");
  2541.                       return(-9);
  2542.                   }
  2543.               }
  2544.               atypes[i] = y;
  2545.               if (y == FTA_AUTO) {
  2546.                   i++;
  2547.                   break;
  2548.               }
  2549.           }
  2550.           if (i < 8)
  2551.             atypes[i] = 0;
  2552.           if ((z = cmcfm()) < 0)
  2553.             return(z);
  2554.           if (atypes[0] == FTA_AUTO) {
  2555.               i = 0;
  2556. #ifdef FTP_GSSAPI
  2557.               ftp_auth_type[i++] = FTA_GK5;
  2558. #endif /* FTP_GSSAPI */
  2559. #ifdef FTP_SRP
  2560.               ftp_auth_type[i++] = FTA_SRP;
  2561. #endif /* FTP_SRP */
  2562. #ifdef FTP_KRB4
  2563.               ftp_auth_type[i++] = FTA_K4;
  2564. #endif /* FTP_KRB4 */
  2565. #ifdef CK_SSL
  2566.               ftp_auth_type[i++] = FTA_TLS;
  2567.               ftp_auth_type[i++] = FTA_SSL;
  2568. #endif /* CK_SSL */
  2569.               ftp_auth_type[i] = 0;
  2570.           } else {
  2571.               for (i = 0; i < 8; i++)
  2572.                 ftp_auth_type[i] = atypes[i];
  2573.           }
  2574.           return(success = 1);
  2575.       }
  2576.  
  2577.       case FTS_SRP:
  2578. #ifdef FTP_SRP
  2579.         if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2580.           return(x);
  2581.         switch (x) {
  2582.           case SRP_CIPHER:
  2583.             if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2584.               return(x);
  2585.             if ((z = cmcfm()) < 0)
  2586.               return(z);
  2587.             success = !srp_selcipher(ciphertab[x].kwd);
  2588.             return(success);
  2589.           case SRP_HASH:
  2590.             if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2591.               return(x);
  2592.             if ((z = cmcfm()) < 0)
  2593.               return(z);
  2594.             success = !srp_selhash(hashtab[x].kwd);
  2595.             return(success = 1);
  2596.           default:
  2597.             if ((z = cmcfm()) < 0)
  2598.               return(z);
  2599.             return(-2);
  2600.         }
  2601. #else /* FTP_SRP */
  2602.         if ((z = cmcfm()) < 0)
  2603.           return(z);
  2604.         return(-2);
  2605. #endif /* FTP_SRP */
  2606. #endif /* FTP_SECURITY */
  2607.  
  2608.       case FTS_DIS:
  2609.     doxdis(2);            /* 2 == ftp */
  2610.         return(success = 1);
  2611.  
  2612.       default:
  2613.         return(-2);
  2614.     }
  2615. }
  2616.  
  2617. int
  2618. ftpbye() {
  2619.     int x;
  2620.     if (!connected)
  2621.       return(1);
  2622.     if (testing)
  2623.       printf(" ftp closing %s...\n",ftp_host);
  2624.     x = ftpclose();
  2625.     return((x > -1) ? 1 : 0);
  2626. }
  2627.  
  2628. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2629.  
  2630. static int
  2631. openftp(s,opn_tls) char * s; int opn_tls; {
  2632.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2633.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1;
  2634.     struct FDB sw, fl, cm;
  2635.     extern int nnetdir;                 /* Network services directory */
  2636.     extern int nhcount;                 /* Lookup result */
  2637.     extern char *nh_p[];                /* Network directory entry pointers */
  2638.     extern char *nh_p2[];               /* Network directory entry nettype */
  2639.  
  2640.     if (!s) return(-2);
  2641.     if (!*s) return(-2);
  2642.  
  2643.     makestr(&hostname,s);
  2644.     hostsave = hostname;
  2645.     makestr(&ftp_logname,NULL);
  2646.     anonymous = 0;
  2647.     noinit = 0;
  2648.  
  2649.     debug(F110,"ftp open",hostname,0);
  2650.  
  2651.     if (sav_psv > -1) {                 /* Restore prevailing active/passive */
  2652.         ftp_psv = sav_psv;              /* selection in case it was */
  2653.         sav_psv = -1;                   /* temporarily overriden by a switch */
  2654.     }
  2655.     cmfdbi(&sw,                         /* Switches */
  2656.            _CMKEY,
  2657.            "Service name or port;\n or switch",
  2658.            "",                          /* default */
  2659.            "",                          /* addtl string data */
  2660.            nftpswi,                     /* addtl numeric data 1: tbl size */
  2661.            4,                           /* addtl numeric data 2: none */
  2662.            xxstring,                    /* Processing function */
  2663.            ftpswitab,                   /* Keyword table */
  2664.            &fl                          /* Pointer to next FDB */
  2665.            );
  2666.     cmfdbi(&fl,                         /* A host name or address */
  2667.            _CMFLD,                      /* fcode */
  2668.            "",                          /* help */
  2669.            "xYzBoo",                    /* default */
  2670.            "",                          /* addtl string data */
  2671.            0,                           /* addtl numeric data 1 */
  2672.            0,                           /* addtl numeric data 2 */
  2673.            xxstring,
  2674.            NULL,
  2675.            &cm
  2676.            );
  2677.     cmfdbi(&cm,                         /* Command confirmation */
  2678.            _CMCFM,
  2679.            "",
  2680.            "",
  2681.            "",
  2682.            0,
  2683.            0,
  2684.            NULL,
  2685.            NULL,
  2686.            NULL
  2687.            );
  2688.  
  2689.     for (n = 0;; n++) {
  2690.         rc = cmfdb(&sw);                /* Parse a service name or a switch */
  2691.         if (rc < 0)
  2692.           goto xopenftp;
  2693.  
  2694.         if (cmresult.fcode == _CMCFM) { /* Done? */
  2695.             break;
  2696.         } else if (cmresult.fcode == _CMFLD) {  /* Port */
  2697.             if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2698.               makestr(&service,cmresult.sresult);
  2699.             else
  2700.               makestr(&service,opn_tls?"ftps":"ftp");
  2701.         } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2702.             c = cmgbrk();               /* get break character */
  2703.             getval = (c == ':' || c == '=');
  2704.             rc = -9;
  2705.             if (getval && !(cmresult.kflags & CM_ARG)) {
  2706.                 printf("?This switch does not take arguments\n");
  2707.                 goto xopenftp;
  2708.             }
  2709.             if (!getval && (cmresult.kflags & CM_ARG)) {
  2710.                 printf("?This switch requires an argument\n");
  2711.                 goto xopenftp;
  2712.             }
  2713.             switch (cmresult.nresult) { /* Switch */
  2714.               case OPN_ANO:             /* /ANONYMOUS */
  2715.                 anonymous++;
  2716.                 break;
  2717.               case OPN_NIN:             /* /NOINIT */
  2718.                 noinit++;
  2719.                 break;
  2720.               case OPN_PSW:             /* /PASSWORD */
  2721.                 if (!anonymous)         /* Don't log real passwords */
  2722.                   debok = 0;
  2723.                 rc = cmfld("Password for FTP server","",&p,xxstring);
  2724.                 if (rc == -3) {
  2725.                     makestr(&ftp_tmp,NULL);
  2726.                 } else if (rc < 0) {
  2727.                     goto xopenftp;
  2728.                 } else {
  2729.                     makestr(&ftp_tmp,p);
  2730.                 }
  2731.                 break;
  2732.               case OPN_USR:             /* /USER */
  2733.                 rc = cmfld("Username for FTP server","",&p,xxstring);
  2734.                 if (rc == -3) {
  2735.                     makestr(&ftp_logname,NULL);
  2736.                 } else if (rc < 0) {
  2737.                     goto xopenftp;
  2738.                 } else {
  2739.                     anonymous = 0;
  2740.                     makestr(&ftp_logname,p);
  2741.                 }
  2742.                 break;
  2743.               case OPN_ACC:
  2744.                 rc = cmfld("Account for FTP server","",&p,xxstring);
  2745.                 if (rc == -3) {
  2746.                     makestr(&ftp_acc,NULL);
  2747.                 } else if (rc < 0) {
  2748.                     goto xopenftp;
  2749.                 } else {
  2750.                     makestr(&ftp_acc,p);
  2751.                 }
  2752.                 break;
  2753.               case OPN_ACT:
  2754.                 opn_psv = 0;
  2755.                 break;
  2756.               case OPN_PSV:
  2757.                 opn_psv = 1;
  2758.                 break;
  2759.               case OPN_TLS:
  2760.                 opn_tls = 1;
  2761.                 break;
  2762.               default:
  2763.                 break;
  2764.             }
  2765.         }
  2766.         if (n == 0) {                   /* After first time through */
  2767.             cmfdbi(&sw,                 /* accept only switches */
  2768.                    _CMKEY,
  2769.                    "\nCarriage return to confirm to command, or switch",
  2770.                    "",
  2771.                    "",
  2772.                    nftpswi,
  2773.                    4,
  2774.                    xxstring,
  2775.                    ftpswitab,
  2776.                    &cm
  2777.                    );
  2778.         }
  2779.     }
  2780. #ifdef COMMENT
  2781.     debug(F100,"ftp openftp while exit","",0);
  2782.     rc = cmcfm();
  2783.     debug(F101,"ftp openftp cmcfm rc","",rc);
  2784.     if (rc < 0)
  2785.       goto xopenftp;
  2786. #endif /* COMMENT */
  2787.  
  2788.     if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
  2789.         sav_psv = ftp_psv;
  2790.         ftp_psv = opn_psv;
  2791.     }
  2792.     if (*hostname == '=') {             /* Bypass directory lookup */
  2793.         hostname++;                     /* if hostname starts with '=' */
  2794.         havehost++;
  2795.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  2796.         havehost++;
  2797.     }
  2798.  
  2799. #ifndef NODIAL
  2800.     if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
  2801.         lunet(hostname);                /* Look up the name */
  2802.         debug(F111,"ftp openftp lunet",hostname,nhcount);
  2803.         if (nhcount == 0) {
  2804.             if (testing)
  2805.               printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2806.             success = ftpopen(hostname,service,opn_tls);
  2807.             debug(F101,"ftp openftp A ftpopen success","",success);
  2808.             rc = success;
  2809.         } else {
  2810.             int found = 0;
  2811.             for (i = 0; i < nhcount; i++) {
  2812.                 if (nh_p2[i])           /* If network type specified */
  2813.                   if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  2814.                     continue;
  2815.                 found++;
  2816.                 makestr(&hostname,nh_p[i]);
  2817.                 debug(F111,"ftpopen lunet substitution",hostname,i);
  2818.                 if (testing)
  2819.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2820.                 success = ftpopen(hostname,service,opn_tls);
  2821.                 debug(F101,"ftp openftp B ftpopen success","",success);
  2822.                 rc = success;
  2823.                 if (success)
  2824.                   break;
  2825.             }
  2826.             if (!found) {               /* E.g. if no network types match */
  2827.                 if (testing)
  2828.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2829.                 success = ftpopen(hostname,service,opn_tls);
  2830.                 debug(F101,"ftp openftp C ftpopen success","",success);
  2831.                 rc = success;
  2832.             }
  2833.         }
  2834.     } else {
  2835. #endif /* NODIAL */
  2836.         if (testing)
  2837.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2838.         success = ftpopen(hostname,service,opn_tls);
  2839.         debug(F111,"ftp openftp D ftpopen success",hostname,success);
  2840.         debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  2841.         rc = success;
  2842. #ifndef NODIAL
  2843.     }
  2844. #endif /* NODIAL */
  2845.  
  2846.   xopenftp:
  2847.     debug(F101,"ftp openftp xopenftp rc","",rc);
  2848.     if (hostsave) free(hostsave);
  2849.     if (service) free(service);
  2850.     if (rc < 0 && ftp_logname) {
  2851.         free(ftp_logname);
  2852.         ftp_logname = NULL;
  2853.     }
  2854.     if (ftp_tmp) {
  2855.         free(ftp_tmp);
  2856.         ftp_tmp = NULL;
  2857.     }
  2858.     return(rc);
  2859. }
  2860.  
  2861. int
  2862. doftpacct() {
  2863.     int x;
  2864.     char * s;
  2865.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  2866.       return(x);
  2867.     CHECKCONN();
  2868.     makestr(&ftp_acc,s);
  2869.     if (testing)
  2870.       printf(" ftp account: \"%s\"\n",ftp_acc);
  2871.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  2872.     return(success);
  2873. }
  2874.  
  2875. int
  2876. doftpusr() {                            /* Log in as USER */
  2877.     int x;
  2878.     char *s, * acct = "";
  2879.  
  2880.     debok = 0;                          /* Don't log */
  2881.     if ((x = cmfld("Remote username or ID","",&s,xxstring)) < 0)
  2882.       return(x);
  2883.     ckstrncpy(line,s,LINBUFSIZ);
  2884.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0)
  2885.       if (x != -3)
  2886.         return(x);
  2887.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2888.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  2889.                    "", &s, xxstring)) < 0)
  2890.       return(x);
  2891.     CHECKCONN();
  2892.     if (*s) {
  2893.         x = strlen(tmpbuf);
  2894.         if (x > 0) {
  2895.             acct = &tmpbuf[x+2];
  2896.             ckstrncpy(acct,s,TMPBUFSIZ - x - 2);
  2897.         }
  2898.     }
  2899.     if (testing)
  2900.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  2901.     success = ftp_user(line,tmpbuf,acct);
  2902. #ifdef CKLOGDIAL
  2903.     dologftp();
  2904. #endif /* CKLOGDIAL */
  2905.     return(success);
  2906. }
  2907.  
  2908. /* DO (various FTP commands)... */
  2909.  
  2910. int
  2911. doftptyp(type) int type; {              /* TYPE */
  2912.     CHECKCONN();
  2913.     ftp_typ = type;
  2914.     changetype(ftp_typ,ftp_vbm);
  2915.     return(1);
  2916. }
  2917.  
  2918. static int
  2919. doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
  2920.     int lcs = -1, rcs = -1;
  2921. #ifndef NOCSETS
  2922.     if (ftp_xla) {
  2923.         lcs = ftp_csl;
  2924.         if (lcs < 0) lcs = fcharset;
  2925.         rcs = ftp_csx;
  2926.         if (rcs < 0) rcs = ftp_csr;
  2927.     }
  2928. #endif /* NOCSETS */
  2929.     debug(F110,"ftp doftpmkd",s,0);
  2930.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2931.       return(1);
  2932.     if (ftpcode == 500 || ftpcode == 502) {
  2933.         if (!quiet)
  2934.           printf("MKD command not recognized, trying XMKD\n");
  2935.         if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2936.           return(1);
  2937.     }
  2938.     return(0);
  2939. }
  2940.  
  2941. static int
  2942. doftpmkd() {                            /* MKDIR parse */
  2943.     int x;
  2944.     char * s;
  2945.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  2946.       return(x);
  2947.     CHECKCONN();
  2948.     ckstrncpy(line,s,LINBUFSIZ);
  2949.     if (testing)
  2950.       printf(" ftp mkdir \"%s\"...\n",line);
  2951.     return(success = doftpxmkd(line,-1));
  2952. }
  2953.  
  2954. static int
  2955. doftprmd() {                            /* RMDIR */
  2956.     int x, lcs = -1, rcs = -1;
  2957.     char * s;
  2958.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  2959.       return(x);
  2960.     CHECKCONN();
  2961.     ckstrncpy(line,s,LINBUFSIZ);
  2962.     if (testing)
  2963.       printf(" ftp rmdir \"%s\"...\n",line);
  2964. #ifndef NOCSETS
  2965.     if (ftp_xla) {
  2966.         lcs = ftp_csl;
  2967.         if (lcs < 0) lcs = fcharset;
  2968.         rcs = ftp_csx;
  2969.         if (rcs < 0) rcs = ftp_csr;
  2970.     }
  2971. #endif /* NOCSETS */
  2972.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  2973.       return(success = 1);
  2974.     if (ftpcode == 500 || ftpcode == 502) {
  2975.         if (!quiet)
  2976.           printf("RMD command not recognized, trying XMKD\n");
  2977.         success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  2978.     } else
  2979.       success = 0;
  2980.     return(success);
  2981. }
  2982.  
  2983. static int
  2984. doftpren() {                            /* RENAME */
  2985.     int x;
  2986.     char * s;
  2987.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  2988.       return(x);
  2989.     ckstrncpy(line,s,LINBUFSIZ);
  2990.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  2991.       return(x);
  2992.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2993.     if ((x = cmcfm()) < 0)
  2994.       return(x);
  2995.     CHECKCONN();
  2996.     if (testing)
  2997.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  2998.     success = ftp_rename(line,tmpbuf);
  2999.     return(success);
  3000. }
  3001.  
  3002. int
  3003. doftpres() {                            /* RESET (log out without close) */
  3004.     int x;
  3005.     if ((x = cmcfm()) < 0)
  3006.       return(x);
  3007.     CHECKCONN();
  3008.     if (testing)
  3009.       printf(" ftp reset...\n");
  3010.     return(success = ftp_reset());
  3011. }
  3012.  
  3013. static int
  3014. doftpxhlp() {                           /* HELP */
  3015.     int x;
  3016.     char * s;
  3017.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  3018.       return(x);
  3019.     CHECKCONN();
  3020.     ckstrncpy(line,s,LINBUFSIZ);
  3021.     if (testing)
  3022.       printf(" ftp help \"%s\"...\n",line);
  3023.     /* No need to translate -- all FTP commands are ASCII */
  3024.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  3025. }
  3026.  
  3027. static int
  3028. doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
  3029.     int x, lcs = 0, rcs = 0, xlate = 0;
  3030.     char * p, * s, * m = "";
  3031.     if (cx == FTP_VDI) {
  3032.         switch (servertype) {
  3033.           case SYS_VMS:
  3034.           case SYS_DOS:
  3035.           case SYS_TOPS10:
  3036.           case SYS_TOPS20:
  3037.             m = "*.*";
  3038.             break;
  3039.           default:
  3040.             m = "*";
  3041.         }
  3042.     }
  3043.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  3044.       return(x);
  3045.     if ((x = remtxt(&s)) < 0)
  3046.       return(x);
  3047. #ifdef NOCSETS
  3048.     xlate = 0;
  3049. #else
  3050.     xlate = ftp_xla;
  3051. #endif /* NOCSETS */
  3052.     line[0] = NUL;
  3053.     ckstrncpy(line,s,LINBUFSIZ);
  3054.     s = line;
  3055.     CHECKCONN();
  3056.  
  3057. #ifndef NOCSETS
  3058.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  3059.         lcs = ftp_csl;                  /* Local charset */
  3060.         if (lcs < 0) lcs = fcharset;
  3061.         if (lcs < 0) xlate = 0;
  3062.     }
  3063.     if (xlate) {                        /* Still ON? */
  3064.         rcs = ftp_csx;                  /* Remote (Server) charset */
  3065.         if (rcs < 0) rcs = ftp_csr;
  3066.         if (rcs < 0) xlate = 0;
  3067.     }
  3068. #endif /* NOCSETS */
  3069.  
  3070.     if (testing) {
  3071.         p = s;
  3072.         if (!p) p = "";
  3073.         if (*p)
  3074.           printf("Directory of files %s at %s:\n", line, ftp_host);
  3075.         else
  3076.           printf("Directory of files at %s:\n", ftp_host);
  3077.     }
  3078.     debug(F111,"doftpdir",s,cx);
  3079.  
  3080.     if (cx == FTP_DIR) {
  3081.         /* Translation of line[] is done inside recvrequest() */
  3082.         /* when it calls ftpcmd(). */
  3083.         return(success =
  3084.           (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  3085.     }
  3086.     success = 1;                        /* VDIR - one file at a time... */
  3087.     p = (char *)remote_files(1,(CHAR *)s,NULL,0); /* Get the file list */
  3088.     cancelgroup = 0;
  3089.     if (!ftp_vbm && !quiet)
  3090.       printlines = 1;
  3091.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  3092.         if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  3093.             success = 0;
  3094.             break;
  3095.         }
  3096.         p = (char *)remote_files(0,NULL,NULL,0); /* Get next file */
  3097.         debug(F110,"ftp vdir file",s,0);
  3098.     }
  3099.     return(success);
  3100. }
  3101.  
  3102. static int
  3103. doftppwd() {                            /* PWD */
  3104.     int x, lcs = -1, rcs = -1;
  3105. #ifndef NOCSETS
  3106.     if (ftp_xla) {
  3107.         lcs = ftp_csl;
  3108.         if (lcs < 0) lcs = fcharset;
  3109.         rcs = ftp_csx;
  3110.         if (rcs < 0) rcs = ftp_csr;
  3111.     }
  3112. #endif /* NOCSETS */
  3113.     if ((x = cmcfm()) < 0)
  3114.       return(x);
  3115.     CHECKCONN();
  3116.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  3117.         success = 1;
  3118.     } else if (ftpcode == 500 || ftpcode == 502) {
  3119.         if (ftp_deb)
  3120.           printf("PWD command not recognized, trying XPWD\n");
  3121.         success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  3122.     }
  3123.     return(success);
  3124. }
  3125.  
  3126. static int
  3127. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  3128.     int lcs = -1, rcs = -1;
  3129. #ifndef NOCSETS
  3130.     if (ftp_xla) {
  3131.         lcs = ftp_csl;
  3132.         if (lcs < 0) lcs = fcharset;
  3133.         rcs = ftp_csx;
  3134.         if (rcs < 0) rcs = ftp_csr;
  3135.     }
  3136. #endif /* NOCSETS */
  3137.  
  3138.     debug(F110,"ftp doftpcwd",s,0);
  3139.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3140.       return(1);
  3141.     if (ftpcode == 500 || ftpcode == 502) {
  3142.         if (!quiet)
  3143.           printf("CWD command not recognized, trying XCWD\n");
  3144.         if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3145.           return(1);
  3146.     }
  3147.     return(0);
  3148. }
  3149.  
  3150. static int
  3151. doftpcdup() {                           /* CDUP */
  3152.     debug(F100,"ftp doftpcdup","",0);
  3153.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  3154.       return(1);
  3155.     if (ftpcode == 500 || ftpcode == 502) {
  3156.         if (!quiet)
  3157.           printf("CDUP command not recognized, trying XCUP\n");
  3158.         if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  3159.           return(1);
  3160.     }
  3161.     return(0);
  3162. }
  3163.  
  3164. /* s y n c d i r  --  Synchronizes client & server directories */
  3165.  
  3166. /* Used with recursive PUTs; Returns 0 on failure, 1 on success */
  3167.  
  3168. static int cdlevel = 0, cdsimlvl = 0;
  3169.  
  3170. static int
  3171. syncdir(local,sim) char * local; int sim; {
  3172.     char buf[CKMAXPATH+1];
  3173.     char tmp[CKMAXPATH+1];
  3174.     char msgbuf[CKMAXPATH+64];
  3175.     char c, * p = local, * s = buf, * q = buf;
  3176.     int i, k = 0, done = 0, itsadir = 0, saveq;
  3177.  
  3178.     debug(F110,"ftp syncdir local (new)",local,0);
  3179.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  3180.  
  3181.     itsadir = isdir(local);
  3182.     saveq = quiet;
  3183.  
  3184.     while ((*s = *p)) {                 /* Copy the argument filename */
  3185.         if (++k == CKMAXPATH)           /* so we can poke it. */
  3186.           return(-1);
  3187.         if (*s == '/')                  /* Pointer to rightmost dirsep */
  3188.           q = s;
  3189.         s++;
  3190.         p++;
  3191.     }
  3192.     if (!itsadir)
  3193.       *q = NUL;                         /* Keep just the path part */
  3194.  
  3195.     debug(F110,"ftp syncdir buf",buf,0);
  3196.     if (!strcmp(buf,putpath)) {         /* Same as for previous file? */
  3197.         if (itsadir) {                  /* It's a directory? */
  3198.             if (doftpcwd(local,0)) {    /* Try to CD to it */
  3199.                 doftpcdup();            /* Worked - CD back up */
  3200.             } else if (sim) {           /* Simulating... */
  3201.                 if (fdispla == XYFD_B) {
  3202.                     printf("WOULD CREATE DIRECTORY %s\n",local);
  3203.                 } else if (fdispla) {
  3204.                     ckmakmsg(msgbuf,CKMAXPATH,
  3205.                              "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3206.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3207.                 }
  3208.                 /* See note above */
  3209.                 return(0);
  3210.             } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3211.                 return(0);
  3212.             } else {
  3213.                 if (fdispla == XYFD_B) {
  3214.                     printf("CREATED DIRECTORY %s\n",local);
  3215.                 } else if (fdispla) {
  3216.                     ckmakmsg(msgbuf,CKMAXPATH+64,
  3217.                              "CREATED DIRECTORY ",local,NULL,NULL);
  3218.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3219.                 }
  3220.             }
  3221.         }
  3222.         debug(F110,"ftp syncdir no change",buf,0);
  3223.         return(1);                      /* Yes, done. */
  3224.     }
  3225.     ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
  3226.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3227.  
  3228.     p = buf;                            /* New */
  3229.     s = putpath;                        /* Old */
  3230.  
  3231.     debug(F110,"ftp syncdir A p",p,0);
  3232.     debug(F110,"ftp syncdir A s",s,0);
  3233.  
  3234.     while (*p != NUL && *s != NUL && *p == *s) p++,s++;
  3235.  
  3236.     if (*s == '/' && !*p) s++;          /* Don't count initial slash */
  3237.  
  3238.     debug(F110,"ftp syncdir B p",p,0);
  3239.     debug(F110,"ftp syncdir B s",s,0);
  3240.  
  3241.     /* p and s now point to the leftmost spot where they differ */
  3242.  
  3243.     if (*s) {                           /* We have to back up */
  3244.         k = 1;                          /* How many levels */
  3245.         while ((c = *s++)) {            /* Count dirseps */
  3246.             if (c == '/' && *s)
  3247.               k++;
  3248.         }
  3249.         for (i = 0; i < k; i++) {       /* Do that many CDUPs */
  3250.             debug(F111,"ftp syncdir up",p,i+1);
  3251.             if (sim && cdsimlvl) {
  3252.                 cdsimlvl--;
  3253.             } else {
  3254.                 if (!doftpcdup()) {
  3255.                     quiet = saveq;
  3256.                     return(0);
  3257.                 }
  3258.             }
  3259.             cdlevel--;
  3260.         }
  3261.         if (!*p)                        /* If we don't have to go down */
  3262.           goto xcwd;                    /* we're done. */
  3263.     }
  3264.     while (p > buf && *p && *p != '/')  /* If in middle of segment */
  3265.       p--;                              /* back up to beginning */
  3266.     if (*p == '/')                      /* and terminate there */
  3267.       p++;
  3268.  
  3269.     s = p;                              /* Point to start of new down path. */
  3270.     while (1) {                         /* Loop through characters. */
  3271.         if (*s == '/' || !*s) {         /* Have a segment. */
  3272.             if (!*s)                    /* If end of string, */
  3273.               done++;                   /* after this segment we're done. */
  3274.             else
  3275.               *s = NUL;                 /* NUL out the separator. */
  3276.             if (*p) {                   /* If segment is not empty */
  3277.                 debug(F110,"ftp syncdir down segment",p,0);
  3278.                 if (!doftpcwd(p,0)) {   /* Try to CD to it */
  3279.                     if (sim) {
  3280.                         if (fdispla == XYFD_B) {
  3281.                             printf("WOULD CREATE DIRECTORY %s\n",local);
  3282.                         } else if (fdispla) {
  3283.                             ckmakmsg(msgbuf,CKMAXPATH,"WOULD CREATE DIRECTORY",
  3284.                                      local,NULL,NULL);
  3285.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3286.                         }
  3287.                         cdsimlvl++;
  3288.                     } else {
  3289.                         if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3290. /*
  3291.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3292.   FOO but the remote has a regular file with the same name.  We can't CD
  3293.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3294.   the user handle the problem.
  3295. */
  3296.                             quiet = saveq;
  3297.                             return(0);
  3298.                         }
  3299.                         if (fdispla == XYFD_B) {
  3300.                             printf("CREATED DIRECTORY %s\n",p);
  3301.                         } else if (fdispla) {
  3302.                             ckmakmsg(msgbuf,CKMAXPATH,
  3303.                                      "CREATED DIRECTORY ",p,NULL,NULL);
  3304.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3305.                         }
  3306.                         if (!doftpcwd(p,0)) { /* Try again to CD */
  3307.                             quiet = saveq;
  3308.                             return(0);
  3309.                         }
  3310.                     }
  3311.                 }
  3312.                 cdlevel++;
  3313.             }
  3314.             if (done)                   /* Quit if no next segment */
  3315.               break;
  3316.             p = s+1;                    /* Point to next segment */
  3317.         }
  3318.         s++;                            /* Point to next source char */
  3319.     }
  3320.  
  3321.   xcwd:
  3322.     ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
  3323.     quiet = saveq;
  3324.     return(1);
  3325. }
  3326.  
  3327. #ifdef DOUPDATE
  3328. #ifdef DEBUG
  3329. static VOID
  3330. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3331.     if (deblog) {
  3332.         debug(F111,"ftp year ",s,xx->tm_year);
  3333.         debug(F111,"ftp month",s,xx->tm_mon);
  3334.         debug(F111,"ftp day  ",s,xx->tm_mday);
  3335.         debug(F111,"ftp hour ",s,xx->tm_hour);
  3336.         debug(F111,"ftp min  ",s,xx->tm_min);
  3337.         debug(F111,"ftp sec  ",s,xx->tm_sec);
  3338.     }
  3339. }
  3340. #endif /* DEBUG */
  3341.  
  3342. /*  t m c o m p a r e  --  Compare two struct tm's */
  3343.  
  3344. /*  Like strcmp() but for struct tm's  */
  3345. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3346.  
  3347. static int
  3348. tmcompare(xx,yy) struct tm * xx, * yy; {
  3349.  
  3350.     if (xx->tm_year < yy->tm_year)      /* First year less than second */
  3351.       return(-1);
  3352.     if (xx->tm_year > yy->tm_year)      /* First year greater than second */
  3353.       return(1);
  3354.  
  3355.     /* Years are equal so compare months */
  3356.  
  3357.     if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
  3358.       return(-1);
  3359.     if (xx->tm_mon  > yy->tm_mon)
  3360.       return(1);
  3361.  
  3362.     if (xx->tm_mday < yy->tm_mday)
  3363.       return(-1);
  3364.     if (xx->tm_mday > yy->tm_mday)
  3365.       return(1);
  3366.  
  3367.     if (xx->tm_hour < yy->tm_hour)
  3368.       return(-1);
  3369.     if (xx->tm_hour > yy->tm_hour)
  3370.       return(1);
  3371.  
  3372.     if (xx->tm_min  < yy->tm_min)
  3373.       return(-1);
  3374.     if (xx->tm_min  > yy->tm_min)
  3375.       return(1);
  3376.  
  3377.     if (xx->tm_sec  < yy->tm_sec)
  3378.       return(-1);
  3379.     if (xx->tm_sec  > yy->tm_sec)
  3380.       return(1);
  3381.  
  3382.     return(0);
  3383. }
  3384. #endif /* DOUPDATE */
  3385.  
  3386. #ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
  3387. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3388.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3389. };
  3390.  
  3391. /* Macro for whether a given year is a leap year. */
  3392. #define ISLEAP(year) \
  3393. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3394. #endif /* HAVE_TIMEGM */
  3395.  
  3396. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3397.  
  3398. static time_t
  3399. #ifdef CK_ANSIC
  3400. mkutime(struct tm * tm)
  3401. #else
  3402. mkutime(tm) struct tm * tm;
  3403. #endif /* CK_ANSIC */
  3404. /* mkutime */ {
  3405. #ifdef HAVE_TIMEGM
  3406.     return(timegm(tm));                 /* Have system service, use it. */
  3407. #else
  3408. /*
  3409.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3410.   Given a struct tm representing a calendar time in UTC, convert it to
  3411.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3412.   convertable.  Note that this function does not canonicalize the provided
  3413.   struct tm, nor does it allow out-of-range values or years before 1970.
  3414.   Result should be identical with timegm().
  3415. */
  3416.     time_t result = 0;
  3417.     int i;
  3418.     /*
  3419.       We do allow some ill-formed dates, but we don't do anything special
  3420.       with them and our callers really shouldn't pass them to us.  Do
  3421.       explicitly disallow the ones that would cause invalid array accesses
  3422.       or other algorithm problems.
  3423.     */
  3424. #ifdef DEBUG
  3425.     if (deblog) {
  3426.         debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3427.         debug(F101,"mkutime tm_year","",tm->tm_year);
  3428.     }
  3429. #endif /* DEBUG */
  3430.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3431.       return((time_t) -1);
  3432.  
  3433.     /* Convert to time_t. */
  3434.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3435.       result += 365 + ISLEAP(i);
  3436.     for (i = 0; i < tm->tm_mon; i++)
  3437.       result += MONTHDAYS[i];
  3438.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3439.       result++;
  3440.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3441.     result = 60 * result + tm->tm_min;
  3442.     result = 60 * result + tm->tm_sec;
  3443.     debug(F101,"mkutime result","",result);
  3444.     return(result);
  3445. #endif /* HAVE_TIMEGM */
  3446. }
  3447.  
  3448.  
  3449. /*
  3450.   s e t m o d t i m e  --  Set file modification time.
  3451.  
  3452.   f = char * filename;
  3453.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3454.  
  3455.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3456.   Returns:
  3457.     0 on success,
  3458.    -1 on error.
  3459.  
  3460. */
  3461. static int
  3462. #ifdef CK_ANSIC
  3463. setmodtime(char * f, time_t t)
  3464. #else
  3465. setmodtime(f,t) char * f; time_t t;
  3466. #endif /* CK_ANSIC */
  3467. /* setmodtime */ {
  3468. #ifdef NT
  3469.     struct _stat sb;
  3470. #else /* NT */
  3471.     struct stat sb;
  3472. #endif /* NT */
  3473.     int x, rc = 0;
  3474. #ifdef BSD44
  3475.     struct timeval tp[2];
  3476. #else
  3477. #ifdef V7
  3478.     struct utimbuf {
  3479.         time_t timep[2];
  3480.     } tp;
  3481. #else
  3482. #ifdef SYSUTIMEH
  3483. #ifdef NT
  3484.     struct _utimbuf tp;
  3485. #else /* NT */
  3486.     struct utimbuf tp;
  3487. #endif /* NT */
  3488. #else
  3489.     struct utimbuf {
  3490.         time_t atime;
  3491.         time_t mtime;
  3492.     } tp;
  3493. #endif /* SYSUTIMEH */
  3494. #endif /* V7 */
  3495. #endif /* BSD44 */
  3496.  
  3497.     if (stat(f,&sb) < 0) {
  3498.         debug(F111,"setmodtime stat failure",f,errno);
  3499.         return(-1);
  3500.     }
  3501. #ifdef BSD44
  3502.     tp[0].tv_sec = sb.st_atime;         /* Access time first */
  3503.     tp[1].tv_sec = t;                   /* Update time second */
  3504.     debug(F111,"setmodtime BSD44",f,t);
  3505. #else
  3506. #ifdef V7
  3507.     tp.timep[0] = t;                    /* Set modif. time to creation date */
  3508.     tp.timep[1] = sb.st_atime;          /* Don't change the access time */
  3509.     debug(F111,"setmodtime V7",f,t);
  3510. #else
  3511. #ifdef SYSUTIMEH
  3512.     tp.modtime = t;                     /* Set modif. time to creation date */
  3513.     tp.actime = sb.st_atime;            /* Don't change the access time */
  3514.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3515. #else
  3516.     tp.mtime = t;                       /* Set modif. time to creation date */
  3517.     tp.atime = sb.st_atime;             /* Don't change the access time */
  3518.     debug(F111,"setmodtime (other)",f,t);
  3519. #endif /* SYSUTIMEH */
  3520. #endif /* V7 */
  3521. #endif /* BSD44 */
  3522.  
  3523.     /* Try to set the file date */
  3524.  
  3525. #ifdef BSD44
  3526.     x = utimes(f,tp);
  3527.     debug(F111,"setmodtime utimes()","BSD44",x);
  3528. #else
  3529. #ifdef IRIX65
  3530.     {
  3531.       /*
  3532.         The following produces the nonsensical warning:
  3533.         Argument  of type "const struct utimbuf *" is incompatible with
  3534.         parameter of type "const struct utimbuf *".  If you can make it
  3535.         go away, be my guest.
  3536.       */
  3537.         const struct utimbuf * t2 = &tp;
  3538.         x = utime(f,t2);
  3539.     }
  3540. #else
  3541.     x = utime(f,&tp);
  3542.     debug(F111,"setmodtime utime()","other",x);
  3543. #endif /* IRIX65 */
  3544. #endif /* BSD44 */
  3545.     if (x)
  3546.       rc = -1;
  3547.  
  3548.     debug(F101,"setmodtime result","",rc);
  3549.     return(rc);
  3550. }
  3551.  
  3552.  
  3553. /*
  3554.   c h k m o d t i m e  --  Check/Set file modification time.
  3555.  
  3556.   fc = function code:
  3557.     0 = Check; returns:
  3558.       -1 on error,
  3559.        0 if local older than remote,
  3560.        1 if modtimes are equal,
  3561.        2 if local newer than remote.
  3562.     1 = Set (local file's modtime from remote's); returns:
  3563.       -1 on error,
  3564.        0 on success.
  3565. */
  3566. static int
  3567. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3568. #ifdef NT
  3569.     struct _stat statbuf;
  3570. #else /* NT */
  3571.     struct stat statbuf;
  3572. #endif /* NT */
  3573.     struct tm * tmlocal = NULL;
  3574.     struct tm tmremote;
  3575.     int rc = 0, havedate = 0, lcs = -1, rcs = -1, flag = 0;
  3576.     char * s, timebuf[64];
  3577.  
  3578.     debug(F111,"chkmodtime",local,mdtmok);
  3579.     if (!mdtmok)            /* Server supports MDTM? */
  3580.       return(-1);            /* No don't bother. */
  3581.  
  3582. #ifndef NOCSETS
  3583.     if (ftp_xla) {
  3584.         lcs = ftp_csl;
  3585.         if (lcs < 0) lcs = fcharset;
  3586.         rcs = ftp_csx;
  3587.         if (rcs < 0) rcs = ftp_csr;
  3588.     }
  3589. #endif /* NOCSETS */
  3590.  
  3591.     if (fc == 0) {
  3592.         rc = stat(local,&statbuf);
  3593.         if (rc == 0) {                  /* Get local file's mod time */
  3594.             tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3595. #ifdef DEBUG
  3596.             if (tmlocal) {
  3597.                 dbtime(local,tmlocal);
  3598.             }
  3599. #endif /* DEBUG */
  3600.         }
  3601.     }
  3602.     /* Get remote file's mod time as yyyymmddhhmmss */
  3603.  
  3604.     if (havemdtm) {            /* Already got it from MLSD? */
  3605.     s = havemdtm;
  3606.     flag++;
  3607.     } else if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3608.         char c;
  3609.         bzero((char *)&tmremote, sizeof(struct tm));
  3610.         s = ftp_reply_str;
  3611.         while ((c = *s++)) {            /* Skip past response code */
  3612.             if (c == SP) {
  3613.                 flag++;
  3614.                 break;
  3615.             }
  3616.         }
  3617.     }
  3618.     if (flag) {
  3619.     debug(F111,"ftp chkmodtime string",s,flag);
  3620.     if (fts_sto) {            /* User gave server time offset? */
  3621.         char * p;
  3622.         debug(F110,"ftp chkmodtime offset",fts_sto,0);
  3623.         ckmakmsg(timebuf,64,s," ",fts_sto,NULL); /* Build delta time */
  3624.         if ((p = cmcvtdate(timebuf,1))) { /* Apply delta time */
  3625.         ckstrncpy(timebuf,p,64);      /* Convert to MDTM format */
  3626.         timebuf[8]  = timebuf[9];  /* h */
  3627.         timebuf[9]  = timebuf[10]; /* h */
  3628.         timebuf[10] = timebuf[12]; /* m */
  3629.         timebuf[11] = timebuf[13]; /* m */
  3630.         timebuf[12] = timebuf[12]; /* s */
  3631.         timebuf[13] = timebuf[13]; /* s */
  3632.         timebuf[14] = NUL;
  3633.         s = timebuf;
  3634.         debug(F110,"ftp chkmodtime adjust",s,0);
  3635.         }
  3636.     }
  3637.         if (flag) {                     /* Convert to struct tm */
  3638.             char * pat;
  3639.             int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
  3640.             if (!ckstrcmp(s,"191",3,0)) {
  3641.                 pat = "%05d%02d%02d%02d%02d%02d";
  3642.                 y2kbug++;
  3643.                 debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3644.             } else {
  3645.                 pat = "%04d%02d%02d%02d%02d%02d";
  3646.             }
  3647.             if (sscanf(s,               /* Parse into struct tm */
  3648.                        pat,
  3649.                        &(tmremote.tm_year),
  3650.                        &(tmremote.tm_mon),
  3651.                        &(tmremote.tm_mday),
  3652.                        &(tmremote.tm_hour),
  3653.                        &(tmremote.tm_min),
  3654.                        &(tmremote.tm_sec)
  3655.                        ) == 6) {
  3656.                 tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3657.                 debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3658.                 tmremote.tm_mon--;
  3659.  
  3660. #ifdef DEBUG
  3661.         debug(F100,"SERVER TIME FOLLOWS:","",0);
  3662.                 dbtime(remote,&tmremote);
  3663. #endif /* DEBUG */
  3664.  
  3665.                 if (havedate > -1)
  3666.           havedate = 1;
  3667.             }
  3668.         }
  3669.     } else {                /* Failed */
  3670.     debug(F101,"ftp chkmodtime ftpcode","",ftpcode);
  3671.     if (ftpcode == 500 ||        /* Command unrecognized */
  3672.         ftpcode == 502 ||        /* Command not implemented */
  3673.         ftpcode == 202)        /* Command superfluous */
  3674.       mdtmok = 0;            /* Don't ask this server again */
  3675.     return(-1);
  3676.     }
  3677.     if (fc == 0) {                      /* Compare */
  3678.         if (havedate == 1) {        /* Only if we have both file dates */
  3679.             /*
  3680.               Compare with local file's time.  We don't use
  3681.               clock time (time_t) here in case of signed/unsigned
  3682.               confusion, etc.
  3683.             */
  3684.         int xx;
  3685. #ifdef COMMENT
  3686. #ifdef DEBUG        
  3687.         if (deblog) {
  3688.         dbtime("LOCAL",tmlocal);
  3689.         dbtime("REMOT",&tmremote);
  3690.         }
  3691. #endif /* DEBUG */
  3692. #endif /* COMMENT */
  3693.         xx = tmcompare(tmlocal,&tmremote);
  3694.         debug(F101,"chkmodtime tmcompare","",xx);
  3695.             return(xx + 1);
  3696.         }
  3697.     } else if (ftp_dates) {             /* Set */
  3698.         /*
  3699.           Here we must convert struct tm to time_t
  3700.           without applying timezone conversion, for which
  3701.           there is no portable API.  The method is hidden
  3702.           in mkutime(), defined above.
  3703.         */
  3704.         time_t utc;
  3705.         utc = mkutime(&tmremote);
  3706.         debug(F111,"ftp chkmodtime mkutime",remote,utc);
  3707.         if (utc != (time_t)-1)
  3708.           return(setmodtime(local,utc));
  3709.     }
  3710.     return(-1);
  3711. }
  3712.  
  3713. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  3714.  
  3715. static int
  3716. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  3717.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  3718. /* getfile */ {
  3719.     int rc = -1;
  3720.     ULONG t0, t1;
  3721.  
  3722. #ifdef GFTIMER
  3723.     CKFLOAT sec;
  3724. #else
  3725.     int sec = 0;
  3726. #endif /* GFTIMER */
  3727.     char fullname[CKMAXPATH+1];
  3728.  
  3729.     debug(F110,"ftp getfile remote A",remote,0);
  3730.     debug(F110,"ftp getfile local A",local,0);
  3731.     debug(F110,"ftp getfile pipename",pipename,0);
  3732.     if (!remote) remote = "";
  3733.  
  3734. #ifdef PATTERNS
  3735.     /* Automatic type switching? */
  3736.     if (xfermode == XMODE_A && patterns && get_auto && !forcetype) {
  3737.         int x;
  3738.         x = matchname(remote,0,servertype);
  3739.         debug(F111,"ftp getfile matchname",remote,x);
  3740.         switch (x) {
  3741.           case 0: ftp_typ = FTT_ASC; break;
  3742.           case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  3743.           default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  3744.         }
  3745.         changetype(ftp_typ,ftp_vbm);
  3746.         binary = ftp_typ;               /* For file-transfer display */
  3747.     }
  3748. #endif /* PATTERNS */
  3749.  
  3750. #ifndef NOCSETS
  3751.     ftp_csx = -1;                       /* For file-transfer display */
  3752.     ftp_csl = -1;                       /* ... */
  3753.  
  3754.     if (rcs > -1)                       /* -1 means no translation */
  3755.       if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
  3756.         if (fcs < 0)                    /* File charset not forced? */
  3757.           fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
  3758.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3759.         debug(F110,"ftp getfile","initxlate",0);
  3760.         initxlate(rcs,fcs);             /* NB: opposite order of PUT */
  3761.         ftp_csx = rcs;
  3762.         ftp_csl = fcs;
  3763.     } else
  3764.       xlate = 0;
  3765. #endif /* NOCSETS */
  3766.  
  3767.     if (!pipename && (!local || !local[0]))
  3768.       local = remote;
  3769.  
  3770.     out2screen = !strcmp(local,"-");
  3771.  
  3772.     fullname[0] = NUL;
  3773.     if (pipename) {
  3774.         ckstrncpy(fullname,pipename,CKMAXPATH+1);
  3775.     } else {
  3776.         zfnqfp(local,CKMAXPATH,fullname);
  3777.         if (!fullname[0])
  3778.           ckstrncpy(fullname,local,CKMAXPATH+1);
  3779.     }
  3780.     if (!out2screen && displa && fdispla) { /* Screen */
  3781.         ftscreen(SCR_FN,'F',(long)pktnum,remote);
  3782.         ftscreen(SCR_AN,0,0L,fullname);
  3783.         ftscreen(SCR_FS,0,fsize,"");
  3784.     }
  3785.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  3786.     tlog(F110," as",fullname,0);
  3787.     debug(F111,"ftp getfile size",remote,fsize);
  3788.     debug(F111,"ftp getfile local",local,out2screen);
  3789.  
  3790.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  3791.  
  3792.     t0 = gmstimer();                    /* Start time */
  3793.     debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
  3794.     rc = recvrequest("RETR",
  3795.                      local,
  3796.                      remote,
  3797.                      append ? "ab" : "wb",
  3798.                      0,
  3799.                      recover,
  3800.                      pipename,
  3801.                      xlate,
  3802.                      fcs,
  3803.                      rcs
  3804.                      );
  3805.     t1 = gmstimer();                    /* End time */
  3806.     debug(F111,"ftp getfile t1",remote,t1);
  3807.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  3808. #ifdef GFTIMER
  3809.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3810.     fpxfsecs = sec;                     /* (for doxlog()) */
  3811. #else
  3812.     sec = (t1 - t0) / 1000;
  3813.     xfsecs = (int)sec;
  3814. #endif /* GFTIMER */
  3815.     debug(F111,"ftp recvrequest rc",remote,rc);
  3816.     if (cancelfile || cancelgroup) {
  3817.         debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  3818.         ftscreen(SCR_ST,ST_INT,0l,"");
  3819.     } else if (rc > 0) {
  3820.         debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  3821.         ftscreen(SCR_ST,ST_SKIP,0l,cmarg);
  3822.     } else if (rc < 0) {
  3823.         switch (ftpcode) {
  3824.           case -4:                      /* Network error */
  3825.           case -2:                      /* File error */
  3826.             ftscreen(SCR_ST,ST_MSG,0l,ck_errstr());
  3827.             break;
  3828.           case -3:
  3829.             ftscreen(SCR_ST,ST_MSG,0l,"Failure to make data connection");
  3830.             break;
  3831.           case -1:
  3832.             ftscreen(SCR_ST,ST_INT,0l,""); /* (should be covered above) */
  3833.             break;
  3834.           default:
  3835.             ftscreen(SCR_ST,ST_MSG,0l,&ftp_reply_str[4]);
  3836.         }
  3837.     } else {                            /* Tudo bem */
  3838.         ftscreen(SCR_PT,'Z',0L,"");
  3839.         if (rc == 0) {
  3840.             ftscreen(SCR_ST,ST_OK,0L,""); /* For screen */
  3841.             makestr(&rrfspec,remote);     /* For WHERE command */
  3842.             makestr(&rfspec,fullname);
  3843.         }
  3844.     }
  3845.     if (ftp_dates)            /* If FTP DATES ON... */
  3846.       if (!pipename && !out2screen)    /* and it's a real file */
  3847.     if (rc < 1 && rc != -3)        /* and it wasn't skipped */
  3848.       if (connected)        /* and we still have a connection */
  3849.         if (zchki(local) > -1) {    /* and the file wasn't discarded */
  3850.         chkmodtime(local,remote,1); /* set local file date */
  3851.         debug(F110,"ftp get set date",local,0);
  3852.         }
  3853.     filcnt++;                           /* Used by \v(filenum) */
  3854. #ifdef TLOG
  3855.     if (tralog) {
  3856.         if (rc > 0) {
  3857.             tlog(F100," recovery skipped","",0);
  3858.         } else if (rc == 0) {
  3859.             tlog(F101," complete, size", "", fsize);
  3860.         } else if (cancelfile) {
  3861.             tlog(F100," canceled by user","",0);
  3862.         } else {
  3863.             tlog(F110," failed:",ftp_reply_str,0);
  3864.         }
  3865.         if (!tlogfmt)
  3866.           doxlog(what,local,fsize,ftp_typ,!rc,"");
  3867.     }
  3868. #endif /* TLOG */
  3869.     return(rc);
  3870. }
  3871.  
  3872. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  3873. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  3874.  
  3875. static int
  3876. putfile(cx,
  3877.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  3878.     char * local, * remote, * mvto, *rnto, *srvrn;
  3879.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg;
  3880.  
  3881. /* putfile */ {
  3882.  
  3883.     char asname[CKMAXPATH+1];
  3884.     char fullname[CKMAXPATH+1];
  3885.     int k = -1, x = 0, y = 0, o = -1, rc = 0, nc = 0;
  3886.     int xlate = 0, restart = 0, mt = -1;
  3887.     char * s = NULL, * cmd = NULL;
  3888.     ULONG t0 = 0, t1 = 0;        /* Times for stats */
  3889.     int ofcs = 0, orcs = 0;
  3890.  
  3891. #ifdef GFTIMER
  3892.     CKFLOAT sec = 0.0;
  3893. #else
  3894.     int sec = 0;
  3895. #endif /* GFTIMER */
  3896.     debug(F111,"ftp putfile flg",local,flg);
  3897.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  3898.     debug(F101,"ftp putfile fcs","",fcs);
  3899.     debug(F101,"ftp putfile rcs","",rcs);
  3900.  
  3901.     ofcs = fcs;                         /* Save charset args */
  3902.     orcs = rcs;
  3903.  
  3904.     sendstart = 0L;
  3905.     restart = flg & PUT_RES;
  3906.     if (!remote)
  3907.       remote = "";
  3908.  
  3909.     /* FTP protocol command to send to server */
  3910.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  3911.  
  3912.     if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
  3913.         if (alike) {                    /* If server & client are alike */
  3914.             nc = 0;                     /* no conversion */
  3915.         } else {                        /* If they are different */
  3916.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  3917.               nc = -1;                  /* only minimal conversions needed */
  3918.             else                        /* otherwise */
  3919.               nc = 1;                   /* full conversion */
  3920.         }
  3921.     } else                              /* Not auto - do what user said */
  3922.       nc = x_cnv;
  3923.  
  3924.     /* If Transfer Mode is Automatic, determine file type */
  3925.     if (xfermode == XMODE_A && filepeek && !pipesend) {
  3926.         if (isdir(local)) {             /* If it's a directory */
  3927.             k = FT_BIN;                 /* skip the file scan */
  3928.         } else {
  3929.             debug(F110,"FTP PUT calling scanfile",local,0);
  3930.             k = scanfile(local,&o,nscanfile); /* Scan the file */
  3931.         }
  3932.         debug(F111,"FTP PUT scanfile",local,k);
  3933.         if (k > -1 && !forcetype) {
  3934.             ftp_typ = (k == FT_BIN) ? 1 : 0;
  3935.             if (xft > -1 && ftp_typ != xft) {
  3936.                 if (flg & PUT_SIM)
  3937.                   tlog(F110,"ftp put SKIP (Type):", fullname, 0);
  3938.                 return(SKP_TYP);
  3939.             }
  3940.             if (ftp_typ == 1 && tenex)  /* User said TENEX? */
  3941.               ftp_typ = FTT_TEN;
  3942.         }
  3943.     }
  3944. #ifndef NOCSETS
  3945.     ftp_csx = -1;                       /* For file-transfer display */
  3946.     ftp_csl = -1;                       /* ... */
  3947.  
  3948.     if (rcs > -1) {                     /* -1 means no translation */
  3949.         if (ftp_typ == 0) {             /* File type is "ascii"? */
  3950.             if (fcs < 0) {              /* File charset not forced? */
  3951.                 if (k < 0) {            /* If we didn't scan */
  3952.                     fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
  3953.                 } else {                /* If we did scan, use scan result */
  3954.                     switch (k) {
  3955.                       case FT_TEXT:     /* Unknown text */
  3956.                         fcs = fcharset;
  3957.                         break;
  3958.                       case FT_7BIT:     /* 7-bit text */
  3959.                         fcs = dcset7;
  3960.                         break;
  3961.                       case FT_8BIT:     /* 8-bit text */
  3962.                         fcs = dcset8;
  3963.                         break;
  3964.                       case FT_UTF8:     /* UTF-8 */
  3965.                         fcs = FC_UTF8;
  3966.                         break;
  3967.                       case FT_UCS2:     /* UCS-2 */
  3968.                         fcs = FC_UCS2;
  3969.                         if (o > -1)     /* Input file byte order */
  3970.                           fileorder = o;
  3971.                         break;
  3972.                       default:
  3973.                         rcs = -1;
  3974.                     }
  3975.                 }
  3976.             }
  3977.         }
  3978.     }
  3979.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3980.         debug(F110,"ftp putfile","initxlate",0);
  3981.         initxlate(fcs,rcs);
  3982.         debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  3983.         xlate = 1;
  3984.         ftp_csx = rcs;
  3985.         ftp_csl = fcs;
  3986.     }
  3987. #endif /* NOCSETS */
  3988.  
  3989.     binary = ftp_typ;                   /* For file-transfer display */
  3990.  
  3991.     if (recursive) {                    /* If sending recursively, */
  3992.         if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  3993.           return(-1);                   /* Don't PUT if it fails. */
  3994.         else if (isdir(local))          /* It's a directory */
  3995.           return(0);                    /* Don't send it! */
  3996.     }
  3997.     if (*remote) {                      /* If an as-name template was given */
  3998. #ifndef NOSPL
  3999.         if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
  4000.             y = CKMAXPATH;              /* evaluate it for this file */
  4001.             s = asname;
  4002.             zzstring(remote,&s,&y);
  4003.         } else
  4004. #endif /* NOSPL */
  4005.           ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  4006.     } else {                                    /* No as-name */
  4007.         nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  4008.         debug(F110,"FTP PUT nzltor",asname,0);
  4009.     }
  4010.     /* Preliminary messages and log entries */
  4011.  
  4012.     fullname[0] = NUL;
  4013.     zfnqfp(local,CKMAXPATH,fullname);
  4014.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  4015.  
  4016.     if (displa && fdispla) {            /* Screen */
  4017.         ftscreen(SCR_FN,'F',(long)pktnum,local);
  4018.         ftscreen(SCR_AN,0,0L,asname);
  4019.         ftscreen(SCR_FS,0,fsize,"");
  4020.     }
  4021. #ifdef DOUPDATE
  4022.     if (flg & (PUT_UPD|PUT_DIF)) {    /* Date-checking modes... */
  4023.         mt = chkmodtime(fullname,asname,0);
  4024.         debug(F111,"ftp putfile chkmodtime",asname,mt);
  4025.         if (mt == 0 && ((flg & PUT_DIF) == 0)) { /* Local is older */
  4026.             tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  4027.             ftscreen(SCR_ST,ST_SKIP,SKP_DAT,fullname); /* Skip this one */
  4028.             filcnt++;
  4029.             return(SKP_DAT);
  4030.         } else if (mt == 1) {           /* Times are equal */
  4031.             tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  4032.             ftscreen(SCR_ST,ST_SKIP,SKP_EQU,fullname); /* Skip it */
  4033.             filcnt++;
  4034.             return(SKP_DAT);
  4035.         }
  4036.     /* Local file is newer */
  4037.         tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  4038.              "ftp put /update TEXT:", fullname, 0);
  4039.     } else if (flg & PUT_RES) {
  4040.         tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  4041.              "ftp put /recover TEXT:", fullname, 0);
  4042.     } else {
  4043.         tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4044.     }
  4045. #else
  4046.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4047. #endif /* DOUPDATE */
  4048.     tlog(F110," as",asname,0);
  4049.  
  4050. #ifndef NOCSETS
  4051.     if (xlate) {
  4052.         debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  4053.         tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  4054.         tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  4055.     } else if (!ftp_typ) {
  4056.         tlog(F110," character sets:","no conversion",0);
  4057.         fcs = ofcs;                     /* Binary file but we still must */
  4058.         rcs = orcs;                     /* translate its name */
  4059.     }
  4060. #endif /* NOCSETS */
  4061.  
  4062.     /* PUT THE FILE */
  4063.  
  4064.     t0 = gmstimer();                    /* Start time */
  4065.     if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
  4066.         if (flg & (PUT_UPD|PUT_DIF)) {    /* (see SKP_xxx in ckcker.h) */
  4067.             rc = (mt < 0) ?             /* Update mode... */
  4068.               SKP_XNX :                 /* Remote file doesn't exist */
  4069.                 SKP_XUP;                /* Remote file is older */
  4070.         } else {
  4071.             rc = SKP_SIM;               /* "Would be sent", period. */
  4072.         }
  4073.     } else {
  4074.         rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  4075.     }
  4076.     t1 = gmstimer();                    /* End time */
  4077.     filcnt++;                           /* File number */
  4078.  
  4079. #ifdef GFTIMER
  4080.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4081.     fpxfsecs = sec;                     /* (for doxlog()) */
  4082. #else
  4083.     sec = (t1 - t0) / 1000;
  4084.     xfsecs = (int)sec;
  4085. #endif /* GFTIMER */
  4086.  
  4087.     debug(F111,"ftp sendrequest rc",local,rc);
  4088.  
  4089.     if (cancelfile || cancelgroup) {
  4090.         debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  4091.         ftscreen(SCR_ST,ST_INT,0l,"");
  4092.     } else if (rc > 0) {
  4093.         debug(F101,"ftp put skipped",local,rc);
  4094.         ftscreen(SCR_ST,ST_SKIP,rc,fullname);
  4095.     } else if (rc < 0) {
  4096.         debug(F111,"ftp put error",local,ftpcode);
  4097.         ftscreen(SCR_ST,ST_MSG,0L,&ftp_reply_str[4]);
  4098.     } else {
  4099.         debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  4100.         ftscreen(SCR_PT,'Z',0L,"");
  4101.         debug(F111,"ftp put ST_OK",local,rc);
  4102.         ftscreen(SCR_ST,ST_OK,0L,"");
  4103.         makestr(&sfspec,fullname);      /* For WHERE command */
  4104.         makestr(&srfspec,asname);
  4105.     }
  4106.  
  4107.     /* Final log entries */
  4108.  
  4109. #ifdef TLOG
  4110.     if (tralog) {
  4111.         if (rc > 0) {
  4112.             if (rc == SKP_XNX)
  4113.               tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  4114.             else if (rc == SKP_XUP)
  4115.               tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  4116.             else if (rc == SKP_SIM)
  4117.               tlog(F100," /simulate: WOULD BE SENT","",0);
  4118.             else
  4119.               tlog(F110," skipped:",gskreason(rc),0);
  4120.         } else if (rc == 0) {
  4121.             tlog(F101," complete, size", "", fsize);
  4122.         } else if (cancelfile) {
  4123.             tlog(F100," canceled by user","",0);
  4124.         } else {
  4125.             tlog(F110," failed:",ftp_reply_str,0);
  4126.         }
  4127.         if (!tlogfmt)
  4128.           doxlog(what,local,fsize,ftp_typ,!rc,"");
  4129.     }
  4130. #endif /* TLOG */
  4131.  
  4132.     if (rc < 0)                         /* PUT did not succeed */
  4133.       return(-1);                       /* so done. */
  4134.  
  4135.     if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
  4136.       return(SKP_SIM);
  4137.  
  4138. #ifdef UNIX
  4139.     /* Set permissions too? */
  4140.  
  4141.     if (prm) {                          /* Change permissions? */
  4142.         s = zgperm(local);              /* Get perms of local file */
  4143.         if (!s) s = "";
  4144.         x = strlen(s);
  4145.         if (x > 3) s += (x - 3);
  4146.         if (rdigits(s)) {
  4147.             ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  4148.             x =
  4149.               ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  4150.             tlog(F110, x ? " chmod" : " chmod failed",
  4151.                  s,
  4152.                  0
  4153.                  );
  4154.             if (!x)
  4155.               return(-1);
  4156.         }
  4157.     }
  4158. #endif /* UNIX */
  4159.  
  4160.     /* Disposition of source file */
  4161.  
  4162.     if (moving) {
  4163.         x = zdelet(local);
  4164.         tlog(F110, (x > -1) ?
  4165.              " deleted" : " failed to delete",
  4166.              local,
  4167.              0
  4168.              );
  4169.         if (x < 0)
  4170.           return(-1);
  4171.     } else if (mvto) {
  4172.         x = zrename(local,mvto);
  4173.         tlog(F110, (x > -1) ?
  4174.              " moved source to" : " failed to move source to",
  4175.              mvto,
  4176.              0
  4177.              );
  4178.         if (x < 0)
  4179.           return(-1);
  4180.         /* ftscreen(SCR_ST,ST_MSG,0L,mvto); */
  4181.  
  4182.     } else if (rnto) {
  4183.         char * s = rnto;
  4184. #ifndef NOSPL
  4185.         int y;                          /* Pass it thru the evaluator */
  4186.         extern int cmd_quoting;         /* for \v(filename) */
  4187.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4188.             y = CKMAXPATH;
  4189.             s = (char *)asname;
  4190.             zzstring(rnto,&s,&y);
  4191.             s = (char *)asname;
  4192.         }
  4193. #endif /* NOSPL */
  4194.         if (s) if (*s) {
  4195.             int x;
  4196.             x = zrename(local,s);
  4197.             tlog(F110, (x > -1) ?
  4198.                  " renamed source file to" :
  4199.                  " failed to rename source file to",
  4200.                  s,
  4201.                  0
  4202.                  );
  4203.             if (x < 0)
  4204.               return(-1);
  4205.             /* ftscreen(SCR_ST,ST_MSG,0L,s); */
  4206.         }
  4207.     }
  4208.  
  4209.     /* Disposition of destination file */
  4210.  
  4211.     if (srvrn) {                        /* /SERVER-RENAME: */
  4212.         char * s = srvrn;
  4213. #ifndef NOSPL
  4214.         int y;                          /* Pass it thru the evaluator */
  4215.         extern int cmd_quoting; /* for \v(filename) */
  4216.         debug(F111,"ftp putfile srvrn",s,1);
  4217.  
  4218.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4219.             y = CKMAXPATH;
  4220.             s = (char *)fullname;       /* We can recycle this buffer now */
  4221.             zzstring(srvrn,&s,&y);
  4222.             s = (char *)fullname;
  4223.         }
  4224. #endif /* NOSPL */
  4225.         debug(F111,"ftp putfile srvrn",s,2);
  4226.         if (s) if (*s) {
  4227.             int x;
  4228.             x = ftp_rename(asname,s);
  4229.             debug(F111,"ftp putfile ftp_rename",asname,x);
  4230.             tlog(F110, (x > 0) ?
  4231.                  " renamed destination file to" :
  4232.                  " failed to rename destination file to",
  4233.                  s,
  4234.                  0
  4235.                  );
  4236.             if (x < 1)
  4237.               return(-1);
  4238.         }
  4239.     }
  4240.     return(0);
  4241. }
  4242.  
  4243. static int
  4244. #ifdef CK_ANSIC
  4245. xxout(char c)
  4246. #else
  4247. xxout(c) char c;
  4248. #endif /* CK_ANSIC */
  4249. {
  4250.     return(zzout(dout,c));
  4251. }
  4252.  
  4253. static int
  4254. #ifdef CK_ANSIC
  4255. scrnout(char c)
  4256. #else
  4257. scrnout(c) char c;
  4258. #endif /* CK_ANSIC */
  4259. {
  4260.     return(putchar(c));
  4261. }
  4262.  
  4263. static int
  4264. #ifdef CK_ANSIC
  4265. pipeout(char c)
  4266. #else
  4267. pipeout(c) char c;
  4268. #endif /* CK_ANSIC */
  4269. {
  4270.     return(zmchout(c));
  4271. }
  4272.  
  4273. static int
  4274. ispathsep(c) int c; {
  4275.     switch (servertype) {
  4276.       case SYS_VMS:
  4277.       case SYS_TOPS10:
  4278.       case SYS_TOPS20:
  4279.         return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4280.       case SYS_OS2:
  4281.       case SYS_WIN32:
  4282.       case SYS_DOS:
  4283.         return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4284.       case SYS_VOS:
  4285.         return((c == '>') ? 1 : 0);
  4286.       default:
  4287.         return((c == '/') ? 1 : 0);
  4288.     }
  4289. }
  4290.  
  4291. static int
  4292. iscanceled() {
  4293. #ifdef CK_CURSES
  4294.     extern int ck_repaint();
  4295. #endif /* CK_CURSES */
  4296.     int x, rc = 0;
  4297.     char c = 0;
  4298.     if (cancelfile)
  4299.       return(1);
  4300.     x = conchk();                       /* Any chars waiting at console? */
  4301.     if (x-- > 0) {                      /* Yes...  */
  4302.         c = coninc(5);                  /* Get one */
  4303.         switch (c) {
  4304.           case 032:                     /* Ctrl-X or X */
  4305.           case 'z':
  4306.           case 'Z': cancelgroup++;      /* fall thru on purpose */
  4307.           case 030:                     /* Ctrl-Z or Z */
  4308.           case 'x':
  4309.           case 'X': cancelfile++; rc++; break;
  4310. #ifdef CK_CURSES
  4311.           case 'L':
  4312.           case 'l':
  4313.           case 014:                     /* Ctrl-L or L or Ctrl-W */
  4314.           case 027:
  4315.             ck_repaint();               /* Refresh screen */
  4316. #endif /* CK_CURSES */
  4317.         }
  4318.     }
  4319.     while (x-- > 0)                     /* Soak up any rest */
  4320.       c = coninc(1);
  4321.     return(rc);
  4322. }
  4323.  
  4324. /* zzsend - used by buffered output macros. */
  4325.  
  4326. static int
  4327. #ifdef CK_ANSIC
  4328. zzsend(int fd, CHAR c)
  4329. #else
  4330. zzsend(fd,c) int fd; CHAR c;
  4331. #endif /* CK_ANSIC */
  4332. {
  4333.     int rc;
  4334.  
  4335.     if (iscanceled())                   /* Check for cancellation */
  4336.       return(-9);
  4337.     rc = (ftp_dpl == FPL_CLR) ?
  4338.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4339.         secure_putbuf(fd, ucbuf, nout);
  4340.     ucbuf[nout] = NUL;
  4341.     nout = 0;
  4342.     ucbuf[nout++] = c;
  4343.     spackets++;
  4344.     pktnum++;
  4345.     if (rc > -1 && fdispla != XYFD_B) {
  4346.         spktl = nout;
  4347.         ftscreen(SCR_PT,'D',spackets,NULL);
  4348.     }
  4349.     return(rc);
  4350. }
  4351.  
  4352. /* c m d l i n p u t  --  Command-line PUT */
  4353.  
  4354. int
  4355. cmdlinput(stay) int stay; {
  4356.     int x, rc = 0, done = 0, good = 0, status = 0;
  4357.     ULONG t0, t1;                       /* Times for stats */
  4358. #ifdef GFTIMER
  4359.     CKFLOAT sec;
  4360. #else
  4361.     int sec = 0;
  4362. #endif /* GFTIMER */
  4363.  
  4364.     if (quiet) {                        /* -q really means quiet */
  4365.         displa = 0;
  4366.         fdispla = 0;
  4367.     } else {
  4368.         displa = 1;
  4369.         fdispla = XYFD_B;
  4370.     }
  4371.     testing = 0;
  4372.     out2screen = 0;
  4373.     dpyactive = 0;
  4374.     what = W_FTP|W_SEND;
  4375.  
  4376. #ifndef NOSPL
  4377.     cmd_quoting = 0;
  4378. #endif /* NOSPL */
  4379.     sndsrc = nfils;
  4380.  
  4381.     t0 = gmstimer();                    /* Record starting time */
  4382.  
  4383.     while (!done && !cancelgroup) {     /* Loop for all files */
  4384.  
  4385.         cancelfile = 0;
  4386.         x = gnfile();                   /* Get next file from list(s) */
  4387.         if (x == 0)                     /* (see gnfile() comments...) */
  4388.           x = gnferror;
  4389.  
  4390.         switch (x) {
  4391.           case 1:                       /* File to send */
  4392.             rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
  4393.                          filnam,        /* Local file to send */
  4394.                          filnam,        /* Remote name for file */
  4395.                          forcetype,     /* Text/binary mode forced */
  4396.                          0,             /* Not moving */
  4397.                          NULL,          /* No move-to */
  4398.                          NULL,          /* No rename-to */
  4399.                          NULL,          /* No server-rename */
  4400.                          ftp_cnv,       /* Filename conversion */
  4401.                          0,             /* Unique-server-names */
  4402.                          -1,            /* All file types */
  4403.                          0,             /* No permissions */
  4404.                          -1,            /* No character sets */
  4405.                          -1,            /* No character sets */
  4406.                          0              /* No update or restart */
  4407.                          );
  4408.             if (rc > -1) {
  4409.                 good++;
  4410.                 status = 1;
  4411.             }
  4412.             if (cancelfile) {
  4413.                 continue;               /* Or break? */
  4414.             }
  4415.             if (rc < 0) {
  4416.                 ftp_fai++;
  4417.             }
  4418.             continue;                   /* Or break? */
  4419.  
  4420.           case 0:                       /* No more files, done */
  4421.             done++;
  4422.             continue;
  4423.  
  4424.           case -2:
  4425.           case -1:
  4426.             printf("?%s: file not found - \"%s\"\n",
  4427.                    puterror ? "Fatal" : "Warning",
  4428.                    filnam
  4429.                    );
  4430.             continue;                   /* or break? */
  4431.           case -3:
  4432.             printf("?Warning access denied - \"%s\"\n", filnam);
  4433.             continue;                   /* or break? */
  4434.           case -5:
  4435.             printf("?Too many files match\n");
  4436.             done++;
  4437.             break;
  4438.           case -6:
  4439.             if (good < 1)
  4440.               printf("?No files selected\n");
  4441.             done++;
  4442.             break;
  4443.           default:
  4444.             printf("?getnextfile() - unknown failure\n");
  4445.             done++;
  4446.         }
  4447.     }
  4448.     if (status > 0) {
  4449.         if (cancelgroup)
  4450.           status = 0;
  4451.         else if (cancelfile && good < 1)
  4452.           status = 0;
  4453.     }
  4454.     success = status;
  4455.     x = success;
  4456.     if (x > -1) {
  4457.         lastxfer = W_FTP|W_SEND;
  4458.         xferstat = success;
  4459.     }
  4460.     t1 = gmstimer();                    /* End time */
  4461. #ifdef GFTIMER
  4462.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4463.     if (!sec) sec = 0.001;
  4464.     fptsecs = sec;
  4465. #else
  4466.     sec = (t1 - t0) / 1000;
  4467.     if (!sec) sec = 1;
  4468. #endif /* GFTIMER */
  4469.     tfcps = (long) (tfc / sec);
  4470.     tsecs = (int)sec;
  4471.     lastxfer = W_FTP|W_SEND;
  4472.     xferstat = success;
  4473.     if (dpyactive)
  4474.       ftscreen(SCR_TC,0,0L,"");
  4475.  
  4476.     if (!stay)
  4477.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4478.     return(success);
  4479. }
  4480.  
  4481.  
  4482. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4483.  
  4484. int
  4485. #ifdef CK_ANSIC
  4486. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4487. #else
  4488. doftpput(cx,who) int cx, who;
  4489. #endif /* CK_ANSIC */
  4490. {
  4491.     struct FDB sf, fl, sw, cm;
  4492.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4493.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4494.     char * s, * s2;
  4495.  
  4496.     int x_csl, x_csr = -1;              /* Local and remote charsets */
  4497.     int x_xla = 0;
  4498.     int x_recurse = 0;
  4499.     char c, * p;                        /* Workers */
  4500. #ifdef PUTARRAY
  4501.     int range[2];                       /* Array range */
  4502.     char ** ap = NULL;                  /* Array pointer */
  4503.     int arrayx = -1;                    /* Array index */
  4504. #endif /* PUTARRAY */
  4505.     ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
  4506. #ifdef GFTIMER
  4507.     CKFLOAT sec;
  4508. #else
  4509.     int sec = 0;
  4510. #endif /* GFTIMER */
  4511.  
  4512.     struct stringint {                  /* Temporary array for switch values */
  4513.         char * sval;
  4514.         int ival;
  4515.     } pv[SND_MAX+1];
  4516.  
  4517.     success = 0;                        /* Assume failure */
  4518.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  4519.     out2screen = 0;                     /* Not outputting file to screen */
  4520.     putflags = 0;                       /* PUT options */
  4521.     x_cnv = ftp_cnv;                    /* Filename conversion */
  4522.     x_usn = ftp_usn;                    /* Unique server names */
  4523.     x_prm = ftp_prm;                    /* Permissions */
  4524.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  4525.       x_prm = alike;
  4526.  
  4527. #ifndef NOCSETS
  4528.     x_csr = ftp_csr;                    /* Inherit global server charset */
  4529.     x_csl = ftp_csl;
  4530.     if (x_csl < 0)
  4531.       x_csl = fcharset;
  4532.     x_xla = ftp_xla;
  4533. #endif /* NOCSETS */
  4534.  
  4535.     makestr(&filefile,NULL);            /* No filename list file yet. */
  4536.     makestr(&srv_renam,NULL);        /* Clear /SERVER-RENAME: */
  4537.     makestr(&snd_rename,NULL);        /*  PUT /RENAME */
  4538.     makestr(&snd_move,NULL);        /*  PUT /MOVE */
  4539.     putpath[0] = NUL;                   /* Initialize for syncdir(). */
  4540.     puterror = ftp_err;                 /* Inherit global error action. */
  4541.     what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
  4542.     asnambuf[0] = NUL;                  /* Clear as-name buffer */
  4543.  
  4544.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  4545.         ftp_typ = g_ftp_typ;
  4546.         /* g_ftp_typ = -1; */
  4547.     }
  4548.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4549.         pv[i].sval = NULL;              /* to null pointers */
  4550.         pv[i].ival = -1;                /* and -1 int values */
  4551.     }
  4552.     if (who == 0) {                     /* Called with unprefixed command */
  4553.         switch (cx) {
  4554.           case XXRSEN:  pv[SND_RES].ival = 1; break;
  4555.           case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4556.           case XXMOVE:  pv[SND_DEL].ival = 1; break;
  4557.           case XXMMOVE: pv[SND_DEL].ival = 1; /* fall thru */
  4558.           case XXMSE:   mput++; break;
  4559.         }
  4560.     } else {
  4561.         if (cx == FTP_MPU)
  4562.           mput++;
  4563.     }
  4564.     cmfdbi(&sw,                         /* First FDB - command switches */
  4565.            _CMKEY,                      /* fcode */
  4566.            "Filename, or switch",       /* hlpmsg */
  4567.            "",                          /* default */
  4568.            "",                          /* addtl string data */
  4569.            nputswi,                     /* addtl numeric data 1: tbl size */
  4570.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4571.            xxstring,                    /* Processing function */
  4572.            putswi,                      /* Keyword table */
  4573.            &sf                          /* Pointer to next FDB */
  4574.            );
  4575.     cmfdbi(&fl,                         /* 3rd FDB - local filespec */
  4576.            _CMFLD,                      /* fcode */
  4577.            "",                          /* hlpmsg */
  4578.            "",                          /* default */
  4579.            "",                          /* addtl string data */
  4580.            0,                           /* addtl numeric data 1 */
  4581.            0,                           /* addtl numeric data 2 */
  4582.            xxstring,
  4583.            NULL,
  4584.            &cm
  4585.            );
  4586.     cmfdbi(&cm,                         /* 4th FDB - Confirmation */
  4587.            _CMCFM,                      /* fcode */
  4588.            "",                          /* hlpmsg */
  4589.            "",                          /* default */
  4590.            "",                          /* addtl string data */
  4591.            0,                           /* addtl numeric data 1 */
  4592.            0,                           /* addtl numeric data 2 */
  4593.            NULL,
  4594.            NULL,
  4595.            NULL
  4596.            );
  4597.  
  4598.   again:
  4599.     cmfdbi(&sf,                         /* 2nd FDB - file to send */
  4600.            _CMIFI,                      /* fcode */
  4601.            "",                          /* hlpmsg */
  4602.            "",                          /* default */
  4603.            "",                          /* addtl string data */
  4604.            /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  4605.            nolinks | x_recurse,         /* addtl numeric data 1 */
  4606.            0,                           /* dirflg 0 means "not dirs only" */
  4607.            xxstring,
  4608.            NULL,
  4609.            mput ? &cm : &fl
  4610.            );
  4611.  
  4612.     while (1) {                         /* Parse 0 or more switches */
  4613.         x = cmfdb(&sw);                 /* Parse something */
  4614.         debug(F101,"ftp put cmfdb","",x);
  4615.         debug(F101,"ftp put fcode","",cmresult.fcode);
  4616.         if (x < 0)                      /* Error */
  4617.           goto xputx;                   /* or reparse needed */
  4618.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  4619.           break;
  4620.         c = cmgbrk();                   /* Get break character */
  4621.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  4622.         if (getval && !(cmresult.kflags & CM_ARG)) {
  4623.             printf("?This switch does not take arguments\n");
  4624.             x = -9;
  4625.             goto xputx;
  4626.         }
  4627.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4628.             printf("?This switch requires an argument\n");
  4629.             x = -9;
  4630.             goto xputx;
  4631.         }
  4632.         n = cmresult.nresult;           /* Numeric result = switch value */
  4633.         debug(F101,"ftp put switch","",n);
  4634.         switch (n) {                    /* Process the switch */
  4635.           case SND_AFT:                 /* Send /AFTER:date-time */
  4636.           case SND_BEF:                 /* Send /BEFORE:date-time */
  4637.           case SND_NAF:                 /* Send /NOT-AFTER:date-time */
  4638.           case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
  4639.             if (!getval) break;
  4640.             if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  4641.                 if (x == -3) {
  4642.                     printf("?Date-time required\n");
  4643.                     x = -9;
  4644.                 }
  4645.                 goto xputx;
  4646.             }
  4647.             pv[n].ival = 1;
  4648.             makestr(&(pv[n].sval),s);
  4649.             break;
  4650.  
  4651.           case SND_ASN:                 /* /AS-NAME: */
  4652.             debug(F101,"ftp put /as-name getval","",getval);
  4653.             if (!getval) break;
  4654.             if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  4655.                 if (x == -3) {
  4656.                     printf("?name required\n");
  4657.                     x = -9;
  4658.                 }
  4659.                 goto xputx;
  4660.             }
  4661.             makestr(&(pv[n].sval),brstrip(s));
  4662.             debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  4663.             if (pv[n].sval) pv[n].ival = 1;
  4664.             break;
  4665.  
  4666. #ifdef PUTARRAY
  4667.           case SND_ARR:                 /* /ARRAY */
  4668.             if (!getval) break;
  4669.             ap = NULL;
  4670.             if ((x = cmfld("Array name (a single letter will do)",
  4671.                            "",
  4672.                            &s,
  4673.                            NULL
  4674.                            )) < 0) {
  4675.                 if (x == -3)
  4676.           break;
  4677.         else
  4678.           return(x);
  4679.             }
  4680.             if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  4681.                 printf("?Bad array: %s\n",s);
  4682.                 return(-9);
  4683.             }
  4684.             if (!(ap = a_ptr[x])) {
  4685.                 printf("?No such array: %s\n",s);
  4686.                 return(-9);
  4687.             }
  4688.             pv[n].ival = 1;
  4689.             pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
  4690.             pv[SND_RES].ival = 0;
  4691.             pv[SND_FIL].ival = 0;
  4692.             arrayx = x;
  4693.             break;
  4694. #endif /* PUTARRAY */
  4695.  
  4696.           case SND_BIN:                 /* /BINARY */
  4697.           case SND_TXT:                 /* /TEXT or /ASCII */
  4698.           case SND_TEN:                 /* /TENEX */
  4699.             pv[SND_BIN].ival = 0;
  4700.             pv[SND_TXT].ival = 0;
  4701.             pv[SND_TEN].ival = 0;
  4702.             pv[n].ival = 1;
  4703.             break;
  4704.  
  4705. #ifdef PUTPIPE
  4706.           case SND_CMD:                 /* These take no args */
  4707.             if (nopush) {
  4708.                 printf("?Sorry, system command access is disabled\n");
  4709.                 x = -9;
  4710.                 goto xputx;
  4711.             }
  4712. #ifdef PIPESEND
  4713.             else if (sndfilter) {
  4714.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  4715.                 x = -9;
  4716.                 goto xputx;
  4717.             }
  4718. #endif /* PIPESEND */
  4719.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  4720.             pv[n].ival = 1;             /* Just set the flag */
  4721.             pv[SND_ARR].ival = 0;
  4722.             break;
  4723. #endif /* PUTPIPE */
  4724.  
  4725. #ifdef CKSYMLINK
  4726.           case SND_LNK:
  4727.             nolinks = 0;
  4728.             goto again;
  4729.           case SND_NLK:
  4730.             nolinks = 2;
  4731.             goto again;
  4732. #endif /* CKSYMLINK */
  4733.  
  4734. #ifdef FTP_RESTART
  4735.           case SND_RES:                 /* /RECOVER (resend) */
  4736.             pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
  4737. #endif /* FTP_RESTART */
  4738.  
  4739.           case SND_NOB:
  4740.           case SND_DEL:                 /* /DELETE */
  4741.           case SND_SHH:                 /* /QUIET */
  4742.           case SND_UPD:                 /* /UPDATE */
  4743.           case SND_SIM:                 /* /UPDATE */
  4744.           case SND_USN:                 /* /UNIQUE */
  4745.             pv[n].ival = 1;             /* Just set the flag */
  4746.             break;
  4747.  
  4748.           case SND_REC:                 /* /RECURSIVE */
  4749.             recursive = 2;              /* Must be set before cmifi() */
  4750.             x_recurse = 1;
  4751.             goto again;
  4752.             break;
  4753.  
  4754. #ifdef UNIXOROSK
  4755.           case SND_DOT:                 /* /DOTFILES */
  4756.             matchdot = 1;
  4757.             break;
  4758.           case SND_NOD:                 /* /NODOTFILES */
  4759.             matchdot = 0;
  4760.             break;
  4761. #endif /* UNIXOROSK */
  4762.  
  4763.           case SND_ERR:                 /* /ERROR-ACTION */
  4764.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  4765.               goto xputx;
  4766.             pv[n].ival = x;
  4767.             break;
  4768.  
  4769.           case SND_EXC:                 /* Excludes */
  4770.             if (!getval) break;
  4771.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4772.                 if (x == -3) {
  4773.                     printf("?Pattern required\n");
  4774.                     x = -9;
  4775.                 }
  4776.                 goto xputx;
  4777.             }
  4778.             if (s) if (!*s) s = NULL;
  4779.             makestr(&(pv[n].sval),s);
  4780.             if (pv[n].sval)
  4781.               pv[n].ival = 1;
  4782.             break;
  4783.  
  4784.           case SND_PRM:                 /* /PERMISSIONS */
  4785.             if (!getval)
  4786.               x = 1;
  4787.             else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  4788.               goto xputx;
  4789.             pv[SND_PRM].ival = x;
  4790.             break;
  4791.  
  4792. #ifdef PIPESEND
  4793.           case SND_FLT:                 /* /FILTER */
  4794.             debug(F101,"ftp put /filter getval","",getval);
  4795.             if (!getval) break;
  4796.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  4797.                 if (x == -3)
  4798.                   s = "";
  4799.                 else
  4800.                   goto xputx;
  4801.             }
  4802.             if (*s) s = brstrip(s);
  4803.             y = strlen(s);
  4804.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  4805.                 if (s[x] != '\\') continue;
  4806.                 if (s[x+1] == 'v') break;
  4807.             }
  4808.             if (x == y) {
  4809.                 printf(
  4810.                 "?Filter must contain a replacement variable for filename.\n"
  4811.                        );
  4812.                 x = -9;
  4813.                 goto xputx;
  4814.             }
  4815.             if (s) if (!*s) s = NULL;
  4816.             makestr(&(pv[n].sval),s);
  4817.             if (pv[n].sval)
  4818.               pv[n].ival = 1;
  4819.             break;
  4820. #endif /* PIPESEND */
  4821.  
  4822.           case SND_NAM:                 /* /FILENAMES */
  4823.             if (!getval) break;
  4824.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  4825.               goto xputx;
  4826.             debug(F101,"ftp put /filenames","",x);
  4827.             pv[n].ival = x;
  4828.             break;
  4829.  
  4830.           case SND_SMA:                 /* Smaller / larger than */
  4831.           case SND_LAR:
  4832.             if (!getval) break;
  4833.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  4834.               goto xputx;
  4835.             pv[n].ival = y;
  4836.             break;
  4837.  
  4838.           case SND_FIL:                 /* Name of file containing filenames */
  4839.             if (!getval) break;
  4840.             if ((x = cmifi("Name of file containing list of filenames",
  4841.                                "",&s,&y,xxstring)) < 0) {
  4842.                 if (x == -3) {
  4843.                     printf("?Filename required\n");
  4844.                     x = -9;
  4845.                 }
  4846.                 goto xputx;
  4847.             } else if (y && iswild(s)) {
  4848.                 printf("?Wildcards not allowed\n");
  4849.                 x = -9;
  4850.                 goto xputx;
  4851.             }
  4852.             if (s) if (!*s) s = NULL;
  4853.             makestr(&(pv[n].sval),s);
  4854.             if (pv[n].sval) {
  4855.                 pv[n].ival = 1;
  4856.                 pv[SND_ARR].ival = 0;
  4857.             } else {
  4858.                 pv[n].ival = 0;
  4859.             }
  4860.             mput = 0;
  4861.             break;
  4862.  
  4863.           case SND_MOV:                 /* MOVE after */
  4864.           case SND_REN:                 /* RENAME after */
  4865.           case SND_SRN: {               /* SERVER-RENAME after */
  4866.               char * m = "";
  4867.               switch (n) {
  4868.                 case SND_MOV:
  4869.                   m = "device and/or directory for source file after sending";
  4870.                   break;
  4871.                 case SND_REN:
  4872.                   m = "new name for source file after sending";
  4873.                   break;
  4874.                 case SND_SRN:
  4875.                   m = "new name for destination file after sending";
  4876.                   break;
  4877.               }
  4878.               if (!getval) break;
  4879.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  4880.                   if (x == -3) {
  4881.                       printf("%s\n", n == SND_MOV ?
  4882.                              "?Destination required" :
  4883.                              "?New name required"
  4884.                              );
  4885.                       x = -9;
  4886.                   }
  4887.                   goto xputx;
  4888.               }
  4889.               if (s) if (!*s) s = NULL;
  4890.               makestr(&(pv[n].sval),s ? brstrip(s) : NULL);
  4891.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  4892.               break;
  4893.           }
  4894.           case SND_STA:                 /* Starting position (= PSEND) */
  4895.             if (!getval) break;
  4896.             if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  4897.               goto xputx;
  4898.             pv[n].ival = y;
  4899.             break;
  4900.  
  4901.           case SND_TYP:                 /* /TYPE */
  4902.             if (!getval) break;
  4903.             if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  4904.               goto xputx;
  4905.             pv[n].ival = (x == 2) ? -1 : x;
  4906.             break;
  4907.  
  4908. #ifndef NOCSETS
  4909.           case SND_CSL:                 /* Local character set */
  4910.           case SND_CSR:                 /* Remote (server) charset */
  4911.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0) {
  4912.         return((x == -3) ? -2 : x);
  4913.             }
  4914.         if (n == SND_CSL)
  4915.               x_csl = x;
  4916.             else
  4917.               x_csr = x;
  4918.             x_xla = 1;                  /* Overrides global OFF setting */
  4919.             break;
  4920.  
  4921.           case SND_XPA:                 /* Transparent */
  4922.             x_xla = 0;
  4923.             x_csr = -1;
  4924.             x_csl = -1;
  4925.             break;
  4926. #endif /* NOCSETS */
  4927.         }
  4928.     }
  4929. #ifdef PIPESEND
  4930.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  4931.         if (sndfilter || pv[SND_FLT].ival > 0) {
  4932.             printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  4933.             x = -9;
  4934.             goto xputx;
  4935.         }
  4936.     if (sfttab[0] > 0 && sfttab[SFT_REST] == 0)
  4937.       printf("WARNING: Server says it doesn't support REST.\n");
  4938.     }
  4939. #endif /* PIPESEND */
  4940.  
  4941.     cmarg = "";
  4942.     cmarg2 = asnambuf;
  4943.     line[0] = NUL;
  4944.     s = line;
  4945.     wild = 0;
  4946.  
  4947.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  4948.       case _CMIFI:                      /* Input filename */
  4949.         if (pv[SND_FIL].ival > 0) {
  4950.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4951.             x = -9;
  4952.             goto xputx;
  4953.         }
  4954.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  4955.         if (pv[SND_ARR].ival > 0)
  4956.           ckstrncpy(asnambuf,line,CKMAXPATH);
  4957.         else
  4958.           wild = cmresult.nresult;      /* Wild flag */
  4959.         debug(F111,"ftp put wild",line,wild);
  4960.         if (!wild && !recursive && !mput)
  4961.           nolinks = 0;
  4962.         break;
  4963.       case _CMFLD:                      /* Field */
  4964.         /* Only allowed with /COMMAND and /ARRAY */
  4965.         if (pv[SND_FIL].ival > 0) {
  4966.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4967.             x = -9;
  4968.             goto xputx;
  4969.         }
  4970.         if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  4971. #ifdef CKROOT
  4972.             if (ckrooterr)
  4973.               printf("?Off limits: %s\n",cmresult.sresult);
  4974.             else
  4975. #endif /* CKROOT */
  4976.               printf("?%s - \"%s\"\n",
  4977.                    iswild(cmresult.sresult) ?
  4978.                    "No files match" : "File not found",
  4979.                    cmresult.sresult
  4980.                    );
  4981.             x = -9;
  4982.             goto xputx;
  4983.         }
  4984.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  4985.         if (pv[SND_ARR].ival > 0)
  4986.           ckstrncpy(asnambuf,line,CKMAXPATH);
  4987.         break;
  4988.       case _CMCFM:                      /* Confirmation */
  4989.         confirmed = 1;
  4990.         break;
  4991.       default:
  4992.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  4993.         x = -9;
  4994.         goto xputx;
  4995.     }
  4996.     debug(F110,"ftp put string",s,0);
  4997.     debug(F101,"ftp put confirmed","",confirmed);
  4998.  
  4999.     /* Save and change protocol and transfer mode */
  5000.     /* Global values are restored in main parse loop */
  5001.  
  5002.     g_displa = fdispla;
  5003.     if (ftp_dis > -1)
  5004.       fdispla = ftp_dis;
  5005.     g_skipbup = skipbup;
  5006.  
  5007.     if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
  5008.         g_skipbup = skipbup;
  5009.         skipbup = 1;
  5010.     }
  5011.     if (pv[SND_TYP].ival > -1) {        /* /TYPE */
  5012.         xfiletype = pv[SND_TYP].ival;
  5013.         if (xfiletype == 2)
  5014.           xfiletype = -1;
  5015.     }
  5016.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  5017.         forcetype = 1;                  /* So skip file scan */
  5018.         ftp_typ = FTT_BIN;              /* Set binary */
  5019.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  5020.         forcetype = 1;
  5021.         ftp_typ = FTT_ASC;
  5022.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  5023.         forcetype = 1;
  5024.         ftp_typ = FTT_TEN;
  5025.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  5026.         forcetype = 1;
  5027.         ftp_typ = binary;
  5028.         g_ftp_typ = binary;
  5029.     }
  5030.  
  5031. #ifdef PIPESEND
  5032.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  5033.         debug(F110,"PUT /COMMAND before stripping",s,0);
  5034.         s = brstrip(s);
  5035.         debug(F110,"PUT /COMMAND after stripping",s,0);
  5036.         if (!*s) {
  5037.             printf("?Sorry, a command to send from is required\n");
  5038.             x = -9;
  5039.             goto xputx;
  5040.         }
  5041.         cmarg = s;
  5042.     }
  5043. #endif /* PIPESEND */
  5044.  
  5045. /* Set up /MOVE and /RENAME */
  5046.  
  5047.     if (pv[SND_DEL].ival > 0 &&
  5048.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  5049.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  5050.         x = -9;
  5051.         goto xputx;
  5052.     }
  5053. #ifdef CK_TMPDIR
  5054.     if (pv[SND_MOV].ival > 0) {
  5055.         int len;
  5056.         char * p = pv[SND_MOV].sval;
  5057.         len = strlen(p);
  5058.         if (!isdir(p)) {                /* Check directory */
  5059. #ifdef CK_MKDIR
  5060.             char * s = NULL;
  5061.             s = (char *)malloc(len + 4);
  5062.             if (s) {
  5063.                 strcpy(s,p);            /* safe */
  5064. #ifdef datageneral
  5065.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  5066. #else
  5067.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  5068. #endif /* datageneral */
  5069.                 s[len++] = 'X';
  5070.                 s[len] = NUL;
  5071. #ifdef NOMKDIR
  5072.                 x = -1;
  5073. #else
  5074.                 x = zmkdir(s);
  5075. #endif /* NOMKDIR */
  5076.                 free(s);
  5077.                 if (x < 0) {
  5078.                     printf("?Can't create \"%s\"\n",p);
  5079.                     x = -9;
  5080.                     goto xputx;
  5081.                 }
  5082.             }
  5083. #else
  5084.             printf("?Directory \"%s\" not found\n",p);
  5085.             x = -9;
  5086.             goto xputx;
  5087. #endif /* CK_MKDIR */
  5088.         }
  5089.         makestr(&snd_move,p);
  5090.     }
  5091. #endif /* CK_TMPDIR */
  5092.  
  5093.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  5094.         char * p = pv[SND_REN].sval;
  5095.         if (!p) p = "";
  5096.         if (!*p) {
  5097.             printf("?New name required for /RENAME\n");
  5098.             x = -9;
  5099.             goto xputx;
  5100.         }
  5101.         p = brstrip(p);
  5102. #ifndef NOSPL
  5103.     /* If name given is wild, rename string must contain variables */
  5104.         if (wild) {
  5105.             char * s = tmpbuf;
  5106.             x = TMPBUFSIZ;
  5107.             zzstring(p,&s,&x);
  5108.             if (!strcmp(tmpbuf,p)) {
  5109.                 printf(
  5110.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  5111.                        );
  5112.                 x = -9;
  5113.                 goto xputx;
  5114.             }
  5115.         }
  5116. #endif /* NOSPL */
  5117.         makestr(&snd_rename,p);
  5118.         debug(F110,"FTP snd_rename",snd_rename,0);
  5119.     }
  5120.     if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
  5121.         char * p = pv[SND_SRN].sval;
  5122.         if (!p) p = "";
  5123.         if (!*p) {
  5124.             printf("?New name required for /SERVER-RENAME\n");
  5125.             x = -9;
  5126.             goto xputx;
  5127.         }
  5128.         p = brstrip(p);
  5129. #ifndef NOSPL
  5130.         if (wild) {
  5131.             char * s = tmpbuf;
  5132.             x = TMPBUFSIZ;
  5133.             zzstring(p,&s,&x);
  5134.             if (!strcmp(tmpbuf,p)) {
  5135.                 printf(
  5136. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  5137.                        );
  5138.                 x = -9;
  5139.                 goto xputx;
  5140.             }
  5141.         }
  5142. #endif /* NOSPL */
  5143.         makestr(&srv_renam,p);
  5144.         debug(F110,"ftp put srv_renam",srv_renam,0);
  5145.     }
  5146.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  5147.         char * lp;
  5148.         if (mput) {                     /* MPUT or MMOVE */
  5149.             nfils = 0;                  /* We already have the first one */
  5150. #ifndef NOMSEND
  5151.             msfiles[nfils++] = line;    /* Store pointer */
  5152.             lp = line + (int)strlen(line) + 1; /* Point past it */
  5153.             debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  5154.             while (1) {                 /* Get more filenames */
  5155.                 if ((x = cmifi("Names of files to send, separated by spaces",
  5156.                                "", &s,&y,xxstring)) < 0) {
  5157.                     if (x != -3)
  5158.                       goto xputx;
  5159.                     if ((x = cmcfm()) < 0)
  5160.                       goto xputx;
  5161.                     break;
  5162.                 }
  5163.                 msfiles[nfils++] = lp;  /* Got one, count it, point to it, */
  5164.                 p = lp;                 /* remember pointer, */
  5165.                 while ((*lp++ = *s++))  /* and copy it into buffer */
  5166.                   if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  5167.                       printf("?MPUT list too long\n");
  5168.                       line[0] = NUL;
  5169.                       x = -9;
  5170.                       goto xputx;
  5171.                   }
  5172.                 debug(F111,"ftp put mput nfils",msfiles[nfils-1],nfils-1);
  5173.                 if (nfils == 1) fspec[0] = NUL; /* Take care of \v(filespec) */
  5174. #ifdef ZFNQFP
  5175.                 zfnqfp(p,TMPBUFSIZ,tmpbuf);
  5176.                 p = tmpbuf;
  5177. #endif /* ZFNQFP */
  5178.                 if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  5179.                     strcat(fspec,p);    /* safe */
  5180.                     strcat(fspec," ");  /* safe */
  5181.                 } else
  5182. #ifdef COMMENT
  5183.                   printf("WARNING - \\v(filespec) buffer overflow\n");
  5184. #else
  5185.                   debug(F101,"doxput filespec buffer overflow","",0);
  5186. #endif /* COMMENT */
  5187.             }
  5188. #endif /* NOMSEND */
  5189.         } else {                        /* Regular SEND */
  5190.             nfils = -1;
  5191.             if ((x = cmtxt(wild ?
  5192. "\nOptional as-name template containing replacement variables \
  5193. like \\v(filename)" :
  5194.                            "Optional name to send it with",
  5195.                            "",&p,NULL)) < 0)
  5196.               goto xputx;
  5197.  
  5198.             if (p) if (!*p) p = NULL;
  5199.             p = brstrip(p);
  5200.  
  5201.             if (p && *p) {
  5202.                 makestr(&(pv[SND_ASN].sval),p);
  5203.                 if (pv[SND_ASN].sval)
  5204.                   pv[SND_ASN].ival = 1;
  5205.                 debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  5206.             }
  5207.         }
  5208.     }
  5209.     /* Set cmarg2 from as-name, however we got it. */
  5210.  
  5211.     CHECKCONN();
  5212.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  5213.         char * p;
  5214.         p = brstrip(pv[SND_ASN].sval);
  5215.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  5216.     }
  5217.     debug(F110,"ftp put asnambuf",asnambuf,0);
  5218.  
  5219.     if (pv[SND_FIL].ival > 0) {
  5220.         if (confirmed) {
  5221.             if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  5222.                 debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  5223.                 printf("?Failure to open %s\n",pv[SND_FIL].sval);
  5224.                 x = -9;
  5225.                 goto xputx;
  5226.             }
  5227.             makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  5228.             debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  5229.             wild = 1;
  5230.         }
  5231.     }
  5232.     if (confirmed && !line[0] && !filefile) {
  5233. #ifndef NOMSEND
  5234.         if (filehead) {                 /* OK if we have a SEND-LIST */
  5235.             nfils = filesinlist;
  5236.             sndsrc = nfils;             /* Like MSEND */
  5237.             addlist = 1;                /* But using a different list... */
  5238.             filenext = filehead;
  5239.             goto doput;
  5240.         }
  5241. #endif /* NOMSEND */
  5242.         printf("?Filename required but not given\n");
  5243.         x = -9;
  5244.         goto xputx;
  5245.     }
  5246. #ifndef NOMSEND
  5247.     addlist = 0;                        /* Don't use SEND-LIST. */
  5248. #endif /* NOMSEND */
  5249.  
  5250.     if (mput) {                         /* MPUT (rather than PUT) */
  5251. #ifndef NOMSEND
  5252.         cmlist = msfiles;               /* List of filespecs */
  5253.         sndsrc = nfils;                 /* rather filespec and as-name */
  5254. #endif /* NOMSEND */
  5255.         pipesend = 0;
  5256.     } else if (filefile) {              /* File contains list of filenames */
  5257.         s = "";
  5258.         cmarg = "";
  5259.         line[0] = NUL;
  5260.         nfils = 1;
  5261.         sndsrc = 1;
  5262.  
  5263.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5264.  
  5265.         /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5266.         nfils = sndsrc = -1;
  5267.         if (!wild) {
  5268.             y = zchki(s);
  5269.             if (y < 0) {
  5270.                 printf("?Read access denied - \"%s\"\n", s);
  5271.                 x = -9;
  5272.                 goto xputx;
  5273.             }
  5274.         }
  5275.         if (s != line)                  /* We might already have done this. */
  5276.           ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
  5277. #ifdef DEBUG
  5278.         else
  5279.           debug(F110,"doxput line=s",line,0);
  5280. #endif /* DEBUG */
  5281.         cmarg = line;                   /* File to send */
  5282.     }
  5283. #ifndef NOMSEND
  5284.     zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
  5285. #endif /* NOMSEND */
  5286.  
  5287.     if (!mput) {                        /* For all but MPUT... */
  5288. #ifdef PIPESEND
  5289.         if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
  5290.           pipesend = 1;
  5291.         debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5292.         if (pipesend && filefile) {
  5293.             printf("?Invalid switch combination\n");
  5294.             x = -9;
  5295.             goto xputx;
  5296.         }
  5297. #endif /* PIPESEND */
  5298.  
  5299. #ifndef NOSPL
  5300.     /* If as-name given and filespec is wild, as-name must contain variables */
  5301.         if ((wild || mput) && asnambuf[0]) {
  5302.             char * s = tmpbuf;
  5303.             x = TMPBUFSIZ;
  5304.             zzstring(asnambuf,&s,&x);
  5305.             if (!strcmp(tmpbuf,asnambuf)) {
  5306.                 printf(
  5307.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5308.                        );
  5309.                 x = -9;
  5310.                 goto xputx;
  5311.             }
  5312.         }
  5313. #endif /* NOSPL */
  5314.     }
  5315.  
  5316.   doput:
  5317.  
  5318.     if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
  5319.         fdispla = 0;
  5320.         debug(F101,"ftp put display","",fdispla);
  5321.     } else {
  5322.         displa = 1;
  5323.         if (ftp_deb)
  5324.       fdispla = XYFD_B;
  5325.     }
  5326.  
  5327. #ifdef PUTARRAY                         /* SEND /ARRAY... */
  5328.     if (pv[SND_ARR].ival > 0) {
  5329.         if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5330.         if (range[0] == -1)             /* If low end of range not specified */
  5331.           range[0] = 1;                 /* default to 1 */
  5332.         if (range[1] == -1)             /* If high not specified */
  5333.           range[1] = a_dim[arrayx];     /* default to size of array */
  5334.         if ((range[0] < 0) ||           /* Check range */
  5335.             (range[0] > a_dim[arrayx]) ||
  5336.             (range[1] < range[0]) ||
  5337.             (range[1] > a_dim[arrayx])) {
  5338.             printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5339.             x = -9;
  5340.             goto xputx;
  5341.         }
  5342.         sndarray = ap;                  /* Array pointer */
  5343.         sndxin = arrayx;                /* Array index */
  5344.         sndxlo = range[0];              /* Array range */
  5345.         sndxhi = range[1];
  5346.         sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5347.         if (!asnambuf[0])
  5348.           ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5349.         cmarg = "";
  5350.     }
  5351. #endif /* PUTARRAY */
  5352.  
  5353.     moving = 0;
  5354.  
  5355.     if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
  5356.         if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
  5357.           moving = 1;
  5358.         if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
  5359.           ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5360.         if (pv[SND_BEF].ival > 0)
  5361.           ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5362.         if (pv[SND_NAF].ival > 0)
  5363.           ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5364.         if (pv[SND_NBE].ival > 0)
  5365.           ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5366.         if (pv[SND_EXC].ival > 0)
  5367.           makelist(pv[SND_EXC].sval,sndexcept,NSNDEXCEPT);
  5368.         if (pv[SND_SMA].ival > -1)
  5369.           sndsmaller = pv[SND_SMA].ival;
  5370.         if (pv[SND_LAR].ival > -1)
  5371.           sndlarger = pv[SND_LAR].ival;
  5372.         if (pv[SND_NAM].ival > -1)
  5373.           x_cnv = pv[SND_NAM].ival;
  5374.         if (pv[SND_USN].ival > -1)
  5375.           x_usn = pv[SND_USN].ival;
  5376.         if (pv[SND_ERR].ival > -1)
  5377.           puterror = pv[SND_ERR].ival;
  5378.  
  5379. #ifdef DOUPDATE
  5380.         if (pv[SND_UPD].ival > 0) {
  5381.             if (x_usn) {
  5382.                 printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5383.                 x = -9;
  5384.                 goto xputx;
  5385.             }
  5386.             putflags |= PUT_UPD;
  5387.         ftp_dates |= 2;
  5388.         }
  5389. #ifdef COMMENT
  5390.     /* This works but it's useless, maybe dangerous */
  5391.         if (pv[SND_DIF].ival > 0) {
  5392.             if (x_usn) {
  5393.                 printf("?Conflicting switches: /DATES-DIFFER /UNIQUE\n");
  5394.                 x = -9;
  5395.                 goto xputx;
  5396.             }
  5397.             putflags |= PUT_DIF;
  5398.         ftp_dates |= 2;
  5399.         }
  5400. #endif /* COMMENT */
  5401. #endif /* DOUPDATE */
  5402.  
  5403.         if (pv[SND_SIM].ival > 0)
  5404.           putflags |= PUT_SIM;
  5405.  
  5406. #ifdef UNIX
  5407.         if (pv[SND_PRM].ival > -1) {
  5408.             if (x_usn) {
  5409.                 printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5410.                 x = -9;
  5411.                 goto xputx;
  5412.             }
  5413.             x_prm = pv[SND_PRM].ival;
  5414.         }
  5415. #endif /* UNIX */
  5416. #ifdef FTP_RESTART
  5417.         if (pv[SND_RES].ival > 0) {
  5418.         if (!sizeok) {
  5419.         printf("?PUT /RESTART can't be used because SIZE disabled.\n");
  5420.                 x = -9;
  5421.                 goto xputx;
  5422.         }
  5423.             if (x_usn || putflags) {
  5424.                 printf("?Conflicting switches: /RECOVER %s\n",
  5425.                        x_usn && putflags ? "/UNIQUE /UPDATE" :
  5426.                        (x_usn ? "/UNIQUE" : "/UPDATE")
  5427.                        );
  5428.                 x = -9;
  5429.                 goto xputx;
  5430.             }
  5431. #ifndef NOCSETS
  5432.             if (x_xla &&
  5433.                 (x_csl == FC_UCS2 ||
  5434.                  x_csl == FC_UTF8 ||
  5435.                  x_csr == FC_UCS2 ||
  5436.                  x_csr == FC_UTF8)) {
  5437.                 printf("?/RECOVER can not be used with Unicode translation\n");
  5438.                 x = -9;
  5439.                 goto xputx;
  5440.             }
  5441. #endif /* NOCSETS */
  5442.             putflags = PUT_RES;
  5443.         }
  5444. #endif /* FTP_RESTART */
  5445.     }
  5446.     debug(F101,"ftp PUT restart","",putflags & PUT_RES);
  5447.     debug(F101,"ftp PUT update","",putflags & PUT_UPD);
  5448.  
  5449. #ifdef PIPESEND
  5450.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  5451.         if (!pv[SND_FLT].sval) {
  5452.             sndfilter = NULL;
  5453.         } else {
  5454.             sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5455.             if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5456.         }
  5457.         debug(F110,"ftp put /FILTER", sndfilter, 0);
  5458.     }
  5459.     if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
  5460.       if (putflags)                     /* with pipes or filters */
  5461.         putflags = 0;
  5462. #endif /* PIPESEND */
  5463.  
  5464.     tfc = 0L;                           /* Initialize stats and counters */
  5465.     filcnt = 0;
  5466.     pktnum = 0;
  5467.     spackets = 0L;
  5468.  
  5469.     if (wild)                           /* (is this necessary?) */
  5470.       cx = FTP_MPU;
  5471.  
  5472.     t0 = gmstimer();                    /* Record starting time */
  5473.  
  5474.     done = 0;                           /* Loop control */
  5475.     cancelgroup = 0;
  5476.  
  5477.     cdlevel = 0;
  5478.     cdsimlvl = 0;
  5479.     while (!done && !cancelgroup) {     /* Loop for all files */
  5480.                                         /* or until canceled. */
  5481. #ifdef FTP_PROXY
  5482.         /*
  5483.            If we are using a proxy, we don't use the local file list;
  5484.            instead we use the list on the remote machine which we want
  5485.            sent to someone else, and we use remglob() to get the names.
  5486.            But in that case we shouldn't even be executing this routine;
  5487.            see ftp_mput().
  5488.         */
  5489. #endif /* FTP_PROXY */
  5490.  
  5491.         cancelfile = 0;
  5492.         x = gnfile();                   /* Get next file from list(s) */
  5493.         if (x == 0)                     /* (see gnfile() comments...) */
  5494.           x = gnferror;
  5495.         debug(F111,"FTP PUT gnfile",filnam,x);
  5496.  
  5497.         switch (x) {
  5498.           case 1:                       /* File to send */
  5499.             s2 = asnambuf;
  5500. #ifndef NOSPL
  5501.             if (asnambuf[0]) {          /* As-name */
  5502.                 int n; char *p;         /* to be evaluated... */
  5503.                 n = TMPBUFSIZ;
  5504.                 p = tmpbuf;
  5505.                 zzstring(asnambuf,&p,&n);
  5506.                 s2 = tmpbuf;
  5507.                 debug(F110,"ftp put asname",s2,0);
  5508.             }
  5509. #endif /* NOSPL */
  5510.             rc = putfile(cx,            /* Function (PUT, APPEND) */
  5511.                     filnam, s2,         /* Name to send, as-name */
  5512.                     forcetype, moving,  /* Parameters from switches... */
  5513.                     snd_move, snd_rename, srv_renam,
  5514.                     x_cnv, x_usn, xfiletype, x_prm,
  5515. #ifndef NOCSETS
  5516.                     x_csl, (!x_xla ? -1 : x_csr),
  5517. #else
  5518.                     -1, -1,
  5519. #endif /* NOCSETS */
  5520.                     putflags
  5521.                     );
  5522.             debug(F111,"ftp put putfile rc",filnam,rc);
  5523.             debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  5524.             debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  5525.             if (rc > -1) {
  5526.                 good++;
  5527.                 status = 1;
  5528.             }
  5529.             if (cancelfile)
  5530.               continue;
  5531.             if (rc < 0) {
  5532.                 ftp_fai++;
  5533.                 if (puterror) {
  5534.                     status = 0;
  5535.                     printf("?Fatal upload error: %s\n",filnam);
  5536.                     done++;
  5537.                 }
  5538.             }
  5539.             continue;
  5540.           case 0:                       /* No more files, done */
  5541.             done++;
  5542.             continue;
  5543.           case -1:
  5544.             printf("?%s: file not found - \"%s\"\n",
  5545.                    puterror ? "Fatal" : "Warning",
  5546.                    filnam
  5547.                    );
  5548.             if (puterror) {
  5549.                 status = 0;
  5550.                 done++;
  5551.                 break;
  5552.             }
  5553.             continue;
  5554.           case -2:
  5555.             if (puterror) {
  5556.                 printf("?Fatal: file not found - \"%s\"\n", filnam);
  5557.                 status = 0;
  5558.                 done++;
  5559.                 break;
  5560.             }
  5561.             continue;                   /* Not readable, keep going */
  5562.           case -3:
  5563.             if (puterror) {
  5564.                 printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  5565.                 status = 0;
  5566.                 done++;
  5567.                 break;
  5568.             }
  5569.             printf("?Warning access denied - \"%s\"\n", filnam);
  5570.             continue;
  5571. #ifdef COMMENT
  5572.           case -4:                      /* Canceled */
  5573.             done++;
  5574.             break;
  5575. #endif /* COMMENT */
  5576.           case -5:
  5577.             printf("?Too many files match\n");
  5578.             done++;
  5579.             break;
  5580.           case -6:
  5581.             if (good < 1)
  5582.               printf("?No files selected\n");
  5583.             done++;
  5584.             break;
  5585.           default:
  5586.             printf("?getnextfile() - unknown failure\n");
  5587.             done++;
  5588.         }
  5589.     }
  5590.     if (cdlevel > 0) {
  5591.         while (cdlevel--) {
  5592.             if (cdsimlvl) {
  5593.                 cdsimlvl--;
  5594.             } else if (!doftpcdup())
  5595.               break;
  5596.         }
  5597.     }
  5598.     if (status > 0) {
  5599.         if (cancelgroup)
  5600.           status = 0;
  5601.         else if (cancelfile && good < 1)
  5602.           status = 0;
  5603.     }
  5604.     success = status;
  5605.     x = success;
  5606.  
  5607.   xputx:
  5608.     if (x > -1) {
  5609. #ifdef GFTIMER
  5610.         t1 = gmstimer();                /* End time */
  5611.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5612.         if (!sec) sec = 0.001;
  5613.         fptsecs = sec;
  5614. #else
  5615.         sec = (t1 - t0) / 1000;
  5616.         if (!sec) sec = 1;
  5617. #endif /* GFTIMER */
  5618.         tfcps = (long) (tfc / sec);
  5619.         tsecs = (int)sec;
  5620.         lastxfer = W_FTP|W_SEND;
  5621.         xferstat = success;
  5622.         if (dpyactive)
  5623.           ftscreen(SCR_TC,0,0L,"");
  5624.     }
  5625.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  5626.         if (pv[i].sval)
  5627.           free(pv[i].sval);
  5628.     }
  5629.     ftreset();                          /* Undo switch effects */
  5630.     dpyactive = 0;
  5631.     return(x);
  5632. }
  5633.  
  5634.  
  5635. static char ** mgetlist = NULL;         /* For MGET */
  5636. static int mgetn = 0, mgetx = 0;
  5637. static char xtmpbuf[4096];
  5638.  
  5639. /*
  5640.   c m d l i n g e t
  5641.  
  5642.   Get files specified by -g command-line option.
  5643.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  5644. */
  5645. int
  5646. cmdlinget(stay) int stay; {
  5647.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  5648.     int lcs = -1, rcs = -1, xlate = 0;
  5649.     int first = 1;
  5650.     int mget = 1;
  5651.     int nc;
  5652.     char * s, * s2, * s3;
  5653.     ULONG t0, t1;                       /* Times for stats */
  5654. #ifdef GFTIMER
  5655.     CKFLOAT sec;
  5656. #else
  5657.     int sec = 0;
  5658. #endif /* GFTIMER */
  5659.  
  5660.     if (quiet) {                        /* -q really means quiet */
  5661.         displa = 0;
  5662.         fdispla = 0;
  5663.     } else {
  5664.         displa = 1;
  5665.         fdispla = XYFD_B;
  5666.     }
  5667.     testing = 0;
  5668.     dpyactive = 0;
  5669.     out2screen = 0;
  5670.     what = W_FTP|W_RECV;
  5671.     mgetmethod = 0;
  5672.     mgetforced = 0;
  5673.  
  5674.     havetype = 0;
  5675.     havesize = -1L;
  5676.     makestr(&havemdtm,NULL);
  5677.  
  5678.     if (ftp_fnc < 0)
  5679.       ftp_fnc = fncact;
  5680.  
  5681. #ifndef NOSPL
  5682.     cmd_quoting = 0;
  5683. #endif /* NOSPL */
  5684.     debug(F101,"ftp cmdlinget nfils","",nfils);
  5685.  
  5686.     if (ftp_cnv == CNV_AUTO) {          /* Name conversion is auto */
  5687.         if (alike) {                    /* If server & client are alike */
  5688.             nc = 0;                     /* no conversion */
  5689.         } else {                        /* If they are different */
  5690.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  5691.               nc = -1;                  /* only minimal conversions needed */
  5692.             else                        /* otherwise */
  5693.               nc = 1;                   /* full conversion */
  5694.         }
  5695.     } else                              /* Not auto - do what user said */
  5696.       nc = ftp_cnv;
  5697.  
  5698.     if (nfils < 1)
  5699.       doexit(BAD_EXIT,-1);
  5700.  
  5701.     t0 = gmstimer();                    /* Starting time for this batch */
  5702.  
  5703. #ifndef NOCSETS
  5704.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  5705.         lcs = ftp_csl;                  /* Local charset */
  5706.         if (lcs < 0) lcs = fcharset;
  5707.         if (lcs < 0) xlate = 0;
  5708.     }
  5709.     if (xlate) {                        /* Still ON? */
  5710.         rcs = ftp_csx;                  /* Remote (Server) charset */
  5711.         if (rcs < 0) rcs = ftp_csr;
  5712.         if (rcs < 0) xlate = 0;
  5713.     }
  5714. #endif /* NOCSETS */
  5715.     /*
  5716.       If we have only one file and it is a directory, then we ask for a
  5717.       listing of its contents, rather than retrieving the directory file
  5718.       itself.  This is what (e.g.) Netscape does.
  5719.     */
  5720.     if (nfils == 1) {
  5721.         if (doftpcwd((CHAR *)cmlist[mgetx],-1)) {
  5722.             /* If we can CD to it, it must be a directory */
  5723.             if (recursive) {
  5724.                 cmlist[mgetx] = "*";
  5725.             } else {
  5726.                 status =
  5727.                   (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  5728.                 done = 1;
  5729.             }
  5730.         }
  5731.     }
  5732. /*
  5733.   The following is to work around UNIX servers which, when given a command
  5734.   like "NLST path/blah" (not wild) returns the basename without the path.
  5735. */
  5736.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  5737.         mget = iswild(cmlist[mgetx]);
  5738.     }
  5739.     if (!mget && !done) {               /* Invoked by command-line FTP URL */
  5740.         if (ftp_deb)
  5741.           printf("DOING GET...\n");
  5742.         done++;
  5743.         cancelfile = 0;                 /* This file not canceled yet */
  5744.         s = cmlist[mgetx];
  5745.         rc = 0;                         /* Initial return code */
  5746.     fsize = -1L;
  5747.     if (sizeok) {
  5748.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5749.         if (x == REPLY_COMPLETE)
  5750.           fsize = atol(&ftp_reply_str[4]);
  5751.     }
  5752.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  5753.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5754.  
  5755.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5756.         s2 = tmpbuf;
  5757.  
  5758.         /* If local file already exists, take collision action */
  5759.  
  5760.         x = zchki(s2);
  5761.         if (x > -1) {
  5762.             switch (ftp_fnc) {
  5763.               case XYFX_A:              /* Append */
  5764.                 append = 1;
  5765.                 break;
  5766.               case XYFX_R:              /* Rename */
  5767.               case XYFX_B: {            /* Backup */
  5768.                   char * p = NULL;
  5769.                   int x = -1;
  5770.                   znewn(s2,&p);         /* Make unique name */
  5771.                   debug(F110,"ftp cmdlinget znewn",p,0);
  5772.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  5773.                       x = zrename(s2,p);
  5774.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  5775.                   } else {              /* Rename incoming file */
  5776.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5777.                       s2 = tmpbuf;
  5778.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  5779.                   }
  5780.                   if (x < 0) {
  5781.                       printf("?Backup/Rename failed\n");
  5782.                       return(success = 0);
  5783.                   }
  5784.                   break;
  5785.               }
  5786.               case XYFX_D:              /* Discard */
  5787.                 ftscreen(SCR_FN,'F',0L,s);
  5788.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5789.                 tlog(F100," refused: name","",0);
  5790.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  5791.                 goto xclget;
  5792.  
  5793.               case XYFX_X:              /* Overwrite */
  5794.               case XYFX_U:              /* Update (already handled above) */
  5795.           case XYFX_M:        /* ditto */
  5796.                 break;
  5797.             }
  5798.         }
  5799.         rc = getfile(s,                 /* Remote name */
  5800.                      s2,                /* Local name */
  5801.                      0,                 /* Recover/Restart */
  5802.                      append,            /* Append */
  5803.                      NULL,              /* Pipename */
  5804.                      0,                 /* Translate charsets */
  5805.                      -1,                /* File charset (none) */
  5806.                      -1                 /* Server charset (none) */
  5807.                      );
  5808.         debug(F111,"ftp cmdlinget rc",s,rc);
  5809.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  5810.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  5811.  
  5812.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  5813.             rc = getfile(&s[1],         /* Remote name without leading '/' */
  5814.                          s2,            /* Local name */
  5815.                          0,             /* Recover/Restart */
  5816.                          append,        /* Append */
  5817.                          NULL,          /* Pipename */
  5818.                          0,             /* Translate charsets */
  5819.                          -1,            /* File charset (none) */
  5820.                          -1             /* Server charset (none) */
  5821.                          );
  5822.         if (rc > -1) {
  5823.             good++;
  5824.             status = 1;
  5825.         }
  5826.         if (cancelfile)
  5827.           goto xclget;
  5828.         if (rc < 0) {
  5829.             ftp_fai++;
  5830.             if (geterror) {
  5831.                 status = 0;
  5832.                 done++;
  5833.             }
  5834.         }
  5835.     }
  5836.     if (ftp_deb && !done)
  5837.       printf("DOING MGET...\n");
  5838.     while (!done && !cancelgroup) {
  5839.         cancelfile = 0;                 /* This file not canceled yet */
  5840.         s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  5841.         if (!s) s = "";
  5842.         if (!*s) {
  5843.             first = 1;
  5844.             mgetx++;
  5845.             if (mgetx < nfils)
  5846.               s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  5847.             else
  5848.               s = NULL;
  5849.             debug(F111,"ftp cmdlinget remote_files B",s,0);
  5850.             if (!s) {
  5851.                 done = 1;
  5852.                 break;
  5853.             }
  5854.         }
  5855.         /*
  5856.           The semantics of NLST are ill-defined.  Suppose we have just sent
  5857.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  5858.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  5859.           RETR commands based on the pathless names are not going to work.
  5860.         */
  5861.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  5862.             if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  5863.                 int len, left = 4096;
  5864.                 char * tmp = xtmpbuf;
  5865.                 len = s3 - cmlist[mgetx] + 1;
  5866.                 ckstrncpy(tmp,cmlist[mgetx],left);
  5867.                 tmp += len;
  5868.                 left -= len;
  5869.                 ckstrncpy(tmp,s,left);
  5870.                 s = xtmpbuf;
  5871.         debug(F111,"ftp cmdlinget remote_files X",s,0);
  5872.             }
  5873.         }
  5874.         first = 0;                      /* Not first any more */
  5875.  
  5876.     debug(F111,"ftp cmdlinget havetype",s,havetype);
  5877.     if (havetype > 0 && havetype != FTYP_FILE) { /* Server says not file */
  5878.         debug(F110,"ftp cmdlinget not-a-file",s,0);
  5879.         continue;
  5880.     }
  5881.         rc = 0;                         /* Initial return code */
  5882.     if (havesize > -1L) {        /* Already have file size? */
  5883.         fsize = havesize;
  5884.     } else {            /* No - must ask server */
  5885.         /*
  5886.           Prior to sending the NLST command we necessarily put the
  5887.           server into ASCII mode.  We must now put it back into the
  5888.           the requested mode so the upcoming SIZE command returns
  5889.           right kind of size; this is especially important for
  5890.           GET /RECOVER; otherwise the server returns the "ASCII" size
  5891.           of the file, rather than its true size.
  5892.         */
  5893.         changetype(ftp_typ,0);    /* Change to requested type */
  5894.         fsize = -1L;
  5895.         if (sizeok) {
  5896.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm);
  5897.         if (x == REPLY_COMPLETE)
  5898.           fsize = atol(&ftp_reply_str[4]);
  5899.         }
  5900.     }
  5901.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  5902.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5903.  
  5904.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5905.         s2 = tmpbuf;
  5906.  
  5907.         /* If local file already exists, take collision action */
  5908.  
  5909.         x = zchki(s2);
  5910.         if (x > -1) {
  5911.             switch (ftp_fnc) {
  5912.               case XYFX_A:              /* Append */
  5913.                 append = 1;
  5914.                 break;
  5915.               case XYFX_R:              /* Rename */
  5916.               case XYFX_B: {            /* Backup */
  5917.                   char * p = NULL;
  5918.                   int x = -1;
  5919.                   znewn(s2,&p);         /* Make unique name */
  5920.                   debug(F110,"ftp cmdlinget znewn",p,0);
  5921.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  5922.                       x = zrename(s2,p);
  5923.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  5924.                   } else {              /* Rename incoming file */
  5925.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5926.                       s2 = tmpbuf;
  5927.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  5928.                   }
  5929.                   if (x < 0) {
  5930.                       printf("?Backup/Rename failed\n");
  5931.                       return(success = 0);
  5932.                   }
  5933.                   break;
  5934.               }
  5935.               case XYFX_D:      /* Discard */
  5936.                 ftscreen(SCR_FN,'F',0L,s);
  5937.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5938.                 tlog(F100," refused: name","",0);
  5939.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  5940.                 continue;
  5941.               case XYFX_X:              /* Overwrite */
  5942.               case XYFX_U:              /* Update (already handled above) */
  5943.               case XYFX_M:              /* ditto */
  5944.                 break;
  5945.             }
  5946.         }
  5947.                                         /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  5948.         rc = getfile(s,                 /* Remote name */
  5949.                      s2,                /* Local name */
  5950.                      0,                 /* Recover/Restart */
  5951.                      append,            /* Append */
  5952.                      NULL,              /* Pipename */
  5953.                      0,                 /* Translate charsets */
  5954.                      -1,                /* File charset (none) */
  5955.                      -1                 /* Server charset (none) */
  5956.                      );
  5957.         debug(F111,"ftp cmdlinget rc",s,rc);
  5958.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  5959.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  5960.  
  5961.         if (rc > -1) {
  5962.             good++;
  5963.             status = 1;
  5964.         }
  5965.         if (cancelfile)
  5966.           continue;
  5967.         if (rc < 0) {
  5968.             ftp_fai++;
  5969.             if (geterror) {
  5970.                 status = 0;
  5971.                 done++;
  5972.             }
  5973.         }
  5974.     }
  5975.  
  5976.   xclget:
  5977.     if (cancelgroup)
  5978.       mlsreset();
  5979.     if (status > 0) {
  5980.         if (cancelgroup)
  5981.           status = 0;
  5982.         else if (cancelfile && good < 1)
  5983.           status = 0;
  5984.     }
  5985.     success = status;
  5986.  
  5987. #ifdef GFTIMER
  5988.     t1 = gmstimer();                    /* End time */
  5989.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5990.     if (!sec) sec = 0.001;
  5991.     fptsecs = sec;
  5992. #else
  5993.     sec = (t1 - t0) / 1000;
  5994.     if (!sec) sec = 1;
  5995. #endif /* GFTIMER */
  5996.  
  5997.     tfcps = (long) (tfc / sec);
  5998.     tsecs = (int)sec;
  5999.     lastxfer = W_FTP|W_RECV;
  6000.     xferstat = success;
  6001.     if (dpyactive)
  6002.       ftscreen(SCR_TC,0,0L,"");
  6003.     if (!stay)
  6004.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  6005.     return(success);
  6006. }
  6007.  
  6008. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  6009.  
  6010. /*
  6011.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  6012.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  6013.   the user-interface perspective, since the server's directory listings show
  6014.   its own local times and since we don't know what timezone it's in, there's
  6015.   no way to reconcile our local times with the server's.
  6016. */
  6017. int
  6018. doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  6019.     struct FDB fl, sw, cm;
  6020.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  6021.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  6022.     int x_fnc = 0, first = 0, skipthis = 0, append = 0, selected = 0;
  6023.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  6024.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  6025.     int gotsize = 0;
  6026.     int matchdot = 0;
  6027.     long getlarger = -1, getsmaller = -1;
  6028.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  6029.     char * src = "", * local = "";
  6030.     char * pat = "";
  6031.  
  6032.     int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
  6033.     int x_xla = 0;
  6034.     char c;                             /* Worker char */
  6035.     ULONG t0 = 0L, t1;                  /* Times for stats */
  6036. #ifdef GFTIMER
  6037.     CKFLOAT sec;
  6038. #else
  6039.     int sec = 0;
  6040. #endif /* GFTIMER */
  6041.  
  6042.     struct stringint {                  /* Temporary array for switch values */
  6043.         char * sval;
  6044.         int ival;
  6045.     } pv[SND_MAX+1];
  6046.  
  6047.     success = 0;                        /* Assume failure */
  6048.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  6049.     restart = 0;                        /* No restart yet */
  6050.     out2screen = 0;            /* No TO-SCREEN switch given yet */
  6051.     mgetmethod = 0;            /* No NLST or MLSD switch yet */
  6052.     mgetforced = 0;
  6053.  
  6054.     g_displa = fdispla;
  6055.     if (ftp_dis > -1)
  6056.       fdispla = ftp_dis;
  6057.  
  6058.     x_cnv = ftp_cnv;                    /* Filename conversion */
  6059.     if (x_cnv == CNV_AUTO) {        /* Name conversion is auto */
  6060.         if (alike) {                    /* If server & client are alike */
  6061.             x_cnv = 0;            /* no conversion */
  6062.         } else {                        /* If they are different */
  6063.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6064.               x_cnv = -1;        /* only minimal conversions needed */
  6065.             else                        /* otherwise */
  6066.               x_cnv = 1;        /* full conversion */
  6067.         }
  6068.     } else                              /* Not auto - do what user said */
  6069.       x_cnv = ftp_cnv;
  6070.  
  6071.     x_prm = ftp_prm;                    /* Permissions */
  6072.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  6073.       x_prm = alike;
  6074.  
  6075. #ifndef NOCSETS
  6076.     x_csr = ftp_csr;                    /* Inherit global server charset */
  6077.     x_csl = ftp_csl;                    /* Inherit global local charset */
  6078.     if (x_csl < 0)                      /* If none, use current */
  6079.       x_csl = fcharset;                 /* file character-set. */
  6080.     x_xla = ftp_xla;                    /* Translation On/Off */
  6081. #endif /* NOCSETS */
  6082.  
  6083.     geterror = ftp_err;                 /* Inherit global error action. */
  6084.     asnambuf[0] = NUL;                  /* No as-name yet. */
  6085.     pipesave = pipesend;
  6086.     pipesend = 0;
  6087.  
  6088.     havetype = 0;
  6089.     havesize = -1L;
  6090.     makestr(&havemdtm,NULL);
  6091.  
  6092.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  6093.         ftp_typ = g_ftp_typ;
  6094.         /* g_ftp_typ = -1; */
  6095.     }
  6096.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  6097.         pv[i].sval = NULL;              /* to null pointers */
  6098.         pv[i].ival = -1;                /* and -1 int values */
  6099.     }
  6100.     zclose(ZMFILE);                     /* In case it was left open */
  6101.  
  6102.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  6103.  
  6104.     if (fp_nml) {                       /* Reset /NAMELIST */
  6105.         if (fp_nml != stdout)
  6106.           fclose(fp_nml);
  6107.         fp_nml = NULL;
  6108.     }
  6109.     makestr(&ftp_nml,NULL);
  6110.  
  6111.     /* Initialize list of remote filespecs */
  6112.  
  6113.     if (!mgetlist) {
  6114.         mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  6115.         if (!mgetlist) {
  6116.             printf("?Memory allocation failure - MGET list\n");
  6117.             return(-9);
  6118.         }
  6119.         for (i = 0; i < MGETMAX; i++)
  6120.           mgetlist[i] = NULL;
  6121.     }
  6122.     mgetn = 0;                          /* Number of mget arguments */
  6123.     mgetx = 0;                          /* Current arg */
  6124.  
  6125.     if (who == 0) {                     /* Called with unprefixed command */
  6126.         if (cx == XXGET || cx == XXREGET || cx == XXRETR)
  6127.           getone++;
  6128.         switch (cx) {
  6129.           case XXREGET: pv[SND_RES].ival = 1; break;
  6130.           case XXRETR:  pv[SND_DEL].ival = 1; break;
  6131.           case XXGET:
  6132.           case XXMGET:  mget++; break;
  6133.         }
  6134.     } else {                            /* FTP command */
  6135.         if (cx == FTP_GET || cx == FTP_RGE)
  6136.           getone++;
  6137.         switch (cx) {
  6138.           case FTP_DEL:                 /* (fall thru on purpose) */
  6139.           case FTP_MDE: mdel++;         /* (ditto) */
  6140.           case FTP_GET:                 /* (ditto) */
  6141.           case FTP_MGE: mget++; break;
  6142.           case FTP_RGE: pv[SND_RES].ival = 1; break;
  6143.         }
  6144.     }
  6145.     cmfdbi(&sw,                         /* First FDB - command switches */
  6146.            _CMKEY,                      /* fcode */
  6147.            "Remote filename;\n or switch", /* hlpmsg */
  6148.            "",                          /* default */
  6149.            "",                          /* addtl string data */
  6150.            mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  6151.            4,                           /* addtl numeric data 2: 4 = cmswi */
  6152.            xxstring,                    /* Processing function */
  6153.            mdel ? delswi : getswi,      /* Keyword table */
  6154.            &fl                          /* Pointer to next FDB */
  6155.            );
  6156.     cmfdbi(&fl,                         /* 2nd FDB - remote filename */
  6157.            _CMFLD,                      /* fcode */
  6158.            "",                          /* hlpmsg */
  6159.            "",                          /* default */
  6160.            "",                          /* addtl string data */
  6161.            0,                           /* addtl numeric data 1 */
  6162.            0,                           /* addtl numeric data 2 */
  6163.            xxstring,
  6164.            NULL,
  6165.            &cm
  6166.            );
  6167.     cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
  6168.            _CMCFM,                      /* fcode */
  6169.            "",                          /* hlpmsg */
  6170.            "",                          /* default */
  6171.            "",                          /* addtl string data */
  6172.            0,                           /* addtl numeric data 1 */
  6173.            0,                           /* addtl numeric data 2 */
  6174.            NULL,
  6175.            NULL,
  6176.            NULL
  6177.            );
  6178.  
  6179.     while (1) {                         /* Parse 0 or more switches */
  6180.         x = cmfdb(&sw);                 /* Parse something */
  6181.         debug(F101,"ftp get cmfdb","",x);
  6182.         if (x < 0)                      /* Error */
  6183.           goto xgetx;                   /* or reparse needed */
  6184.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  6185.           break;
  6186.         c = cmgbrk();                   /* Get break character */
  6187.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  6188.         if (getval && !(cmresult.kflags & CM_ARG)) {
  6189.             printf("?This switch does not take arguments\n");
  6190.             x = -9;
  6191.             goto xgetx;
  6192.         }
  6193.         n = cmresult.nresult;           /* Numeric result = switch value */
  6194.         debug(F101,"ftp get switch","",n);
  6195.  
  6196.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6197.             printf("?This switch requires an argument\n");
  6198.             x = -9;
  6199.             goto xgetx;
  6200.         }
  6201.         switch (n) {                    /* Process the switch */
  6202.           case SND_ASN:                 /* /AS-NAME: */
  6203.             debug(F101,"ftp get /as-name getval","",getval);
  6204.             if (!getval) break;
  6205.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  6206.                 if (x == -3) {
  6207.                     printf("?name required\n");
  6208.                     x = -9;
  6209.                 }
  6210.                 goto xgetx;
  6211.             }
  6212.             s = brstrip(s);
  6213.             if (!*s) s = NULL;
  6214.             makestr(&(pv[n].sval),s);
  6215.             pv[n].ival = 1;
  6216.             break;
  6217.  
  6218.           case SND_BIN:                 /* /BINARY */
  6219.           case SND_TXT:                 /* /TEXT or /ASCII */
  6220.           case SND_TEN:                 /* /TENEX */
  6221.             pv[SND_BIN].ival = 0;
  6222.             pv[SND_TXT].ival = 0;
  6223.             pv[SND_TEN].ival = 0;
  6224.             pv[n].ival = 1;
  6225.             break;
  6226.  
  6227. #ifdef PUTPIPE
  6228.           case SND_CMD:                 /* These take no args */
  6229.             if (nopush) {
  6230.                 printf("?Sorry, system command access is disabled\n");
  6231.                 x = -9;
  6232.                 goto xgetx;
  6233.             }
  6234. #ifdef PIPESEND
  6235.             else if (rcvfilter) {
  6236.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  6237.                 x = -9;
  6238.                 goto xgetx;
  6239.             }
  6240. #endif /* PIPESEND */
  6241.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  6242.             pv[n].ival = 1;             /* Just set the flag */
  6243.             pv[SND_ARR].ival = 0;
  6244.             break;
  6245. #endif /* PUTPIPE */
  6246.  
  6247.           case SND_SHH:                 /* /QUIET */
  6248.           case SND_RES:                 /* /RECOVER (reget) */
  6249.           case SND_NOB:                 /* /NOBACKUPFILES */
  6250.           case SND_DEL:                 /* /DELETE */
  6251.           case SND_UPD:                 /* /UPDATE */
  6252.           case SND_USN:                 /* /UNIQUE */
  6253.           case SND_NOD:                 /* /NODOTFILES */
  6254.           case SND_REC:                 /* /RECOVER */
  6255.           case SND_MAI:                 /* /TO-SCREEN */
  6256.             pv[n].ival = 1;             /* Just set the flag */
  6257.             break;
  6258.  
  6259.           case SND_DIF:                 /* /DATES-DIFFER */
  6260.         pv[SND_COL].ival = XYFX_M;    /* Now it's a collision option */
  6261.         pv[n].ival = 1;
  6262.         break;
  6263.  
  6264.           case SND_COL:                 /* /COLLISION: */
  6265.             if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  6266.               goto xgetx;
  6267.         if (x == XYFX_M)
  6268.           pv[SND_DIF].ival = 1;    /* (phase this out) */
  6269.         pv[n].ival = x;        /* this should be sufficient */
  6270.             break;
  6271.  
  6272.           case SND_ERR:                 /* /ERROR-ACTION */
  6273.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  6274.               goto xgetx;
  6275.             pv[n].ival = x;
  6276.             break;
  6277.  
  6278.           case SND_EXC:                 /* Exception list */
  6279.             if (!getval) break;
  6280.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  6281.                 if (x == -3) {
  6282.                     printf("?Pattern required\n");
  6283.                     x = -9;
  6284.                 }
  6285.                 goto xgetx;
  6286.             }
  6287.             if (s) if (!*s) s = NULL;
  6288.             makestr(&(pv[n].sval),s);
  6289.             if (pv[n].sval)
  6290.               pv[n].ival = 1;
  6291.             break;
  6292.  
  6293. #ifdef PIPESEND
  6294.           case SND_FLT:
  6295.             debug(F101,"ftp get /filter getval","",getval);
  6296.             if (!getval) break;
  6297.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  6298.                 if (x == -3)
  6299.                   s = "";
  6300.                 else
  6301.                   goto xgetx;
  6302.             }
  6303.             s = brstrip(s);
  6304.             if (pv[SND_MAI].ival < 1) {
  6305.                 y = strlen(s);
  6306.                 /* Make sure they included "\v(...)" */
  6307.                 for (x = 0; x < y; x++) {
  6308.                     if (s[x] != '\\') continue;
  6309.                     if (s[x+1] == 'v') break;
  6310.                 }
  6311.                 if (x == y) {
  6312.                     printf(
  6313.                 "?Filter must contain a replacement variable for filename.\n"
  6314.                            );
  6315.                     x = -9;
  6316.                     goto xgetx;
  6317.                 }
  6318.             }
  6319.             if (*s) {
  6320.                 pv[n].ival = 1;
  6321.                 makestr(&(pv[n].sval),s);
  6322.             } else {
  6323.                 pv[n].ival = 0;
  6324.                 makestr(&(pv[n].sval),NULL);
  6325.             }
  6326.             break;
  6327. #endif /* PIPESEND */
  6328.  
  6329.           case SND_NAM:
  6330.             if (!getval) break;
  6331.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  6332.               goto xgetx;
  6333.             debug(F101,"ftp get /filenames","",x);
  6334.             pv[n].ival = x;
  6335.             break;
  6336.  
  6337.           case SND_SMA:                 /* Smaller / larger than */
  6338.           case SND_LAR:
  6339.             if (!getval) break;
  6340.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  6341.               goto xgetx;
  6342.             pv[n].ival = y;
  6343.             break;
  6344.  
  6345.           case SND_FIL:                 /* Name of file containing filnames */
  6346.             if (!getval) break;
  6347.             if ((x = cmifi("Name of file containing list of filenames",
  6348.                                "",&s,&y,xxstring)) < 0) {
  6349.                 if (x == -3) {
  6350.                     printf("?Filename required\n");
  6351.                     x = -9;
  6352.                 }
  6353.                 goto xgetx;
  6354.             } else if (y && iswild(s)) {
  6355.                 printf("?Wildcards not allowed BBB\n");
  6356.                 x = -9;
  6357.                 goto xgetx;
  6358.             }
  6359.             if (s) if (!*s) s = NULL;
  6360.             makestr(&(pv[n].sval),s);
  6361.             if (pv[n].sval)
  6362.               pv[n].ival = 1;
  6363.             break;
  6364.  
  6365.           case SND_MOV:                 /* MOVE after */
  6366.           case SND_REN:                 /* RENAME after */
  6367.           case SND_SRN: {               /* SERVER-RENAME */
  6368.               char * m = "";
  6369.               switch (n) {
  6370.                 case SND_MOV:
  6371.                   m =
  6372.                    "Device and/or directory for incoming file after reception";
  6373.                   break;
  6374.                 case SND_REN:
  6375.                   m = "New name for incoming file after reception";
  6376.                   break;
  6377.                 case SND_SRN:
  6378.                   m = "New name for source file on server after reception";
  6379.                   break;
  6380.               }
  6381.               if (!getval) break;
  6382.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6383.                   if (x == -3) {
  6384.                       printf("%s\n", n == SND_MOV ?
  6385.                              "?Destination required" :
  6386.                              "?New name required"
  6387.                              );
  6388.                       x = -9;
  6389.                   }
  6390.                   goto xgetx;
  6391.               }
  6392.               makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6393.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  6394.               break;
  6395.           }
  6396. #ifndef NOCSETS
  6397.           case SND_CSL:                 /* Local character set */
  6398.           case SND_CSR:                 /* Remote (server) charset */
  6399.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6400.               return((x == -3) ? -2 : x);
  6401.             if (n == SND_CSL)
  6402.               x_csl = x;
  6403.             else
  6404.               x_csr = x;
  6405.             x_xla = 1;                  /* Overrides global OFF setting */
  6406.             break;
  6407.  
  6408.           case SND_XPA:                 /* Transparent */
  6409.             x_xla =  0;
  6410.             x_csr = -1;
  6411.             x_csl = -1;
  6412.             break;
  6413. #endif /* NOCSETS */
  6414.  
  6415.           case SND_NML:
  6416.             if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6417.               goto xgetx;
  6418.             makestr(&ftp_nml,s);
  6419.             break;
  6420.  
  6421.       case SND_PAT:            /* /PATTERN: */
  6422.         if (!getval) break;
  6423.         if ((x = cmfld("Pattern","*", &s, xxstring)) < 0)
  6424.           goto xgetx;
  6425.         makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6426.         pv[n].ival = (pv[n].sval) ? 1 : 0;
  6427.         break;
  6428.  
  6429.       case SND_NLS:            /* /NLST */
  6430.             pv[n].ival = 1;        /* Use NLST */
  6431.         pv[SND_MLS].ival = 0;    /* Don't use MLSD */
  6432.         break;
  6433.  
  6434.       case SND_MLS:            /* /MLSD */
  6435.             pv[n].ival = 1;        /* Use MLSD */
  6436.         pv[SND_NLS].ival = 0;    /* Don't use NLST */
  6437.         break;
  6438.  
  6439.           default:                      /* /AFTER, /PERMISSIONS, etc... */
  6440.             printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6441.             x = -9;
  6442.             goto xgetx;
  6443.         }
  6444.     }
  6445.     line[0] = NUL;
  6446.     cmarg = line;
  6447.     cmarg2 = asnambuf;
  6448.     s = line;
  6449. /*
  6450.   For GET, we want to parse an optional as-name, like with PUT.
  6451.   For MGET, we must parse a list of names, and then send NLST or MLSD
  6452.   commands for each name separately.
  6453. */
  6454.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  6455.       case _CMFLD:                      /* Field */
  6456.         if (!getone) {
  6457.             s = brstrip(cmresult.sresult);
  6458.             makestr(&(mgetlist[mgetn++]),s);
  6459.             while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6460.                 if (x < 0)
  6461.                   goto xgetx;
  6462.                 makestr(&(mgetlist[mgetn++]),brstrip(s));
  6463.                 if (mgetn >= MGETMAX) {
  6464.                     printf("?Too many items in MGET list\n");
  6465.                     goto xgetx;
  6466.                 }
  6467.             }
  6468.             if ((x = cmcfm()) < 0)
  6469.               goto xgetx;
  6470.         } else {
  6471.             s = brstrip(cmresult.sresult);
  6472.             ckstrncpy(line,s,LINBUFSIZ);
  6473.             if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6474.               if (x != -3)
  6475.                 goto xgetx;
  6476.             s = brstrip(s);
  6477.             ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6478.             if ((x = cmcfm()) < 0)
  6479.               goto xgetx;
  6480.         }
  6481.         break;
  6482.       case _CMCFM:                      /* Confirmation */
  6483.         break;
  6484.       default:
  6485.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  6486.         x = -9;
  6487.         goto xgetx;
  6488.     }
  6489.     if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
  6490.       recursive = 2;
  6491.  
  6492.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  6493.         forcetype = 1;                  /* So skip the name-pattern match */
  6494.         ftp_typ = XYFT_B;               /* Set binary */
  6495.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  6496.         forcetype = 1;
  6497.         ftp_typ = XYFT_T;
  6498.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  6499.         forcetype = 1;
  6500.         ftp_typ = FTT_TEN;
  6501.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  6502.         forcetype = 1;
  6503.         ftp_typ = binary;
  6504.         g_ftp_typ = binary;
  6505.     }
  6506.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  6507.         char * p;
  6508.         p = brstrip(pv[SND_ASN].sval);  /* As-name */
  6509.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  6510.     }
  6511.     debug(F110,"ftp get asnambuf",asnambuf,0);
  6512.  
  6513. #ifdef PIPESEND
  6514.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  6515.         char * p;
  6516.         p = asnambuf;
  6517.         debug(F110,"GET /COMMAND before stripping",p,0);
  6518.         p = brstrip(p);
  6519.         debug(F110,"GET /COMMAND after stripping",p,0);
  6520.         if (!*p) {
  6521.             printf("?Sorry, a command to write to is required\n");
  6522.             x = -9;
  6523.             goto xgetx;
  6524.         }
  6525.         pipename = p;
  6526.         pipesend = 1;
  6527.     }
  6528. #endif /* PIPESEND */
  6529.  
  6530. /* Set up /MOVE and /RENAME */
  6531.  
  6532.     if (pv[SND_DEL].ival > 0 &&
  6533.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  6534.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  6535.         x = -9;
  6536.         goto xgetx;
  6537.     }
  6538. #ifdef CK_TMPDIR
  6539.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  6540.         int len;
  6541.         char * p = pv[SND_MOV].sval;
  6542.         len = strlen(p);
  6543.         if (!isdir(p)) {                /* Check directory */
  6544. #ifdef CK_MKDIR
  6545.             char * s = NULL;
  6546.             s = (char *)malloc(len + 4);
  6547.             if (s) {
  6548.                 strcpy(s,p);            /* safe */
  6549. #ifdef datageneral
  6550.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  6551. #else
  6552.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  6553. #endif /* datageneral */
  6554.                 s[len++] = 'X';
  6555.                 s[len] = NUL;
  6556. #ifdef NOMKDIR
  6557.                 x = -1;
  6558. #else
  6559.                 x = zmkdir(s);
  6560. #endif /* NOMKDIR */
  6561.                 free(s);
  6562.                 if (x < 0) {
  6563.                     printf("?Can't create \"%s\"\n",p);
  6564.                     x = -9;
  6565.                     goto xgetx;
  6566.                 }
  6567.             }
  6568. #else
  6569.             printf("?Directory \"%s\" not found\n",p);
  6570.             x = -9;
  6571.             goto xgetx;
  6572. #endif /* CK_MKDIR */
  6573.         }
  6574.         makestr(&rcv_move,p);
  6575.         moving = 1;
  6576.     }
  6577. #endif /* CK_TMPDIR */
  6578.  
  6579.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  6580.         char * p = pv[SND_REN].sval;
  6581.         if (!p) p = "";
  6582.         if (!*p) {
  6583.             printf("?New name required for /RENAME\n");
  6584.             x = -9;
  6585.             goto xgetx;
  6586.         }
  6587.         p = brstrip(p);
  6588. #ifndef NOSPL
  6589.     /* If name given is wild, rename string must contain variables */
  6590.         if (mget && !getone) {
  6591.             char * s = tmpbuf;
  6592.             x = TMPBUFSIZ;
  6593.             zzstring(p,&s,&x);
  6594.             if (!strcmp(tmpbuf,p)) {
  6595.                 printf(
  6596.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  6597.                        );
  6598.                 x = -9;
  6599.                 goto xgetx;
  6600.             }
  6601.         }
  6602. #endif /* NOSPL */
  6603.         renaming = 1;
  6604.         makestr(&rcv_rename,p);
  6605.         debug(F110,"FTP rcv_rename",rcv_rename,0);
  6606.     }
  6607.     if (!cmarg[0] && mgetn == 0 && getone && pv[SND_FIL].ival < 1) {
  6608.         printf("?Filename required but not given\n");
  6609.         x = -9;
  6610.         goto xgetx;
  6611.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  6612.         printf("?You can't give both /LISTFILE and a remote filename\n");
  6613.         x = -9;
  6614.         goto xgetx;
  6615.     }
  6616.     CHECKCONN();                        /* Check connection */
  6617.  
  6618.     if (pv[SND_COL].ival > -1)
  6619.       x_fnc = pv[SND_COL].ival;
  6620.  
  6621. #ifndef NOSPL
  6622.     /* If as-name given for MGET, as-name must contain variables */
  6623.     if (mget && !getone && asnambuf[0] && x_fnc != XYFX_A) {
  6624.         char * s = tmpbuf;
  6625.         x = TMPBUFSIZ;
  6626.         zzstring(asnambuf,&s,&x);
  6627.         if (!strcmp(tmpbuf,asnambuf)) {
  6628.             printf(
  6629.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  6630.                    );
  6631.             x = -9;
  6632.             goto xgetx;
  6633.         }
  6634.     }
  6635. #endif /* NOSPL */
  6636.  
  6637. /* doget: */
  6638.  
  6639.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  6640.         fdispla = 0;
  6641.     } else {
  6642.         displa = 1;
  6643.         if (mdel || ftp_deb)
  6644.       fdispla = XYFD_B;
  6645.     }
  6646.     deleting = 0;
  6647.     if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
  6648.       deleting = 1;
  6649.     if (pv[SND_EXC].ival > 0)
  6650.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  6651.     if (pv[SND_SMA].ival > -1)
  6652.       getsmaller = pv[SND_SMA].ival;
  6653.     if (pv[SND_LAR].ival > -1)
  6654.       getlarger = pv[SND_LAR].ival;
  6655.     if (pv[SND_NAM].ival > -1)
  6656.       x_cnv = pv[SND_NAM].ival;
  6657.     if (pv[SND_ERR].ival > -1)
  6658.       geterror = pv[SND_ERR].ival;
  6659.     if (pv[SND_MAI].ival > -1)
  6660.       toscreen = 1;
  6661.  
  6662.     if (pv[SND_NLS].ival > 0) {        /* Force NLST or MLSD? */
  6663.     mgetmethod = SND_NLS;
  6664.     mgetforced = 1;
  6665.     } else if (pv[SND_MLS].ival > 0) {
  6666.     mgetmethod = SND_MLS;
  6667.     mgetforced = 1;
  6668.     }
  6669.  
  6670. #ifdef FTP_RESTART
  6671.     if (pv[SND_RES].ival > 0) {
  6672.         if (!ftp_typ) {
  6673.             printf("?Sorry, GET /RECOVER requires binary mode\n");
  6674.             x = -9;
  6675.             goto xgetx;
  6676. #ifdef COMMENT
  6677.         /* Not true - the fact that the initial REST fails does not mean */
  6678.         /* it will fail here.  */
  6679.         } else if (!okrestart) {
  6680.             printf("WARNING: Server might not support restart...\n");
  6681. #endif /* COMMENT */
  6682.         }
  6683.         restart = 1;
  6684.     }
  6685. #endif /* FTP_RESTART */
  6686.  
  6687. #ifdef PIPESEND
  6688.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  6689.         if (pipesend) {
  6690.             printf("?Switch conflict: /FILTER and /COMMAND\n");
  6691.             x = -9;
  6692.             goto xgetx;
  6693.         }
  6694.         makestr(&rcvfilter,pv[SND_FLT].sval);
  6695.         debug(F110,"ftp get /FILTER", rcvfilter, 0);
  6696.     }
  6697.     if (rcvfilter || pipesend) {        /* /RESTART */
  6698. #ifdef FTP_RESTART
  6699.         if (restart) {                  /* with pipes or filters */
  6700.             printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  6701.             x = -9;
  6702.             goto xgetx;
  6703.         }
  6704. #endif /* FTP_RESTART */
  6705.         if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_M || x_fnc == XYFX_U) {
  6706.             printf(
  6707.         "?Switch conflict: /FILTER or /COMMAND and Date Checking\n");
  6708.             x = -9;
  6709.             goto xgetx;
  6710.         }
  6711.     }
  6712. #endif /* PIPESEND */
  6713.  
  6714.     tfc = 0L;                           /* Initialize stats and counters */
  6715.     filcnt = 0;
  6716.     pktnum = 0;
  6717.     rpackets = 0L;
  6718.  
  6719.     if (pv[SND_FIL].ival > 0) {
  6720.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  6721.             debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  6722.             printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  6723.             x = -9;
  6724.             goto xgetx;
  6725.         }
  6726.         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  6727.             zclose(ZMFILE);                       /* Failed */
  6728.             debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  6729.             printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  6730.             x = -9;
  6731.             goto xgetx;
  6732.         }
  6733.         listfile = 1;
  6734.         debug(F110,"ftp get listfile first",tmpbuf,0);
  6735.         makestr(&(mgetlist[0]),tmpbuf);
  6736.     }
  6737.     t0 = gmstimer();                    /* Record starting time */
  6738.  
  6739.     updating = 0;            /* Checking dates? */
  6740.     if (pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U))
  6741.       updating = 1;
  6742.     if (pv[SND_DIF].ival > 0 || x_fnc == XYFX_M)
  6743.       updating = 2;
  6744.     if (updating)            /* These switches force FTP DATES ON */
  6745.       ftp_dates |= 2;
  6746.  
  6747.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  6748.  
  6749.     cancelgroup = 0;                    /* Group not canceled yet */
  6750.     if (!(xfermode == XMODE_A && patterns && get_auto && !forcetype))
  6751.       changetype(ftp_typ,0);        /* Change to requested type */
  6752.     binary = ftp_typ;                   /* For file-transfer display */
  6753.     first = 1;                          /* For MGET list */
  6754.     done = 0;                           /* Loop control */
  6755.  
  6756. #ifdef CK_TMPDIR
  6757.     if (dldir && !f_tmpdir) {           /* If they have a download directory */
  6758.         if ((s = zgtdir())) {           /* Get current directory */
  6759.             if (zchdir(dldir)) {        /* Change to download directory */
  6760.                 ckstrncpy(savdir,s,TMPDIRLEN);
  6761.                 f_tmpdir = 1;           /* Remember that we did this */
  6762.             }
  6763.         }
  6764.     }
  6765. #endif /* CK_TMPDIR */
  6766.  
  6767.     if (ftp_nml) {                      /* /NAMELIST */
  6768.         debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  6769.         if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  6770.           fp_nml = stdout;
  6771.         else
  6772.           fp_nml = fopen(ftp_nml, "wb");
  6773.         if (!fp_nml) {
  6774.             printf("?%s: %s\n",ftp_nml,ck_errstr());
  6775.             goto xgetx;
  6776.         }
  6777.     }
  6778.     while (!done && !cancelgroup) {     /* Loop for all files */
  6779.                                         /* or until canceled. */
  6780. #ifdef FTP_PROXY
  6781.         /* do something here if proxy */
  6782. #endif /* FTP_PROXY */
  6783.  
  6784.         rs_len = 0L;                    /* REGET position */
  6785.         cancelfile = 0;                 /* This file not canceled yet */
  6786.         haspath = 0;                    /* Recalculate this each time thru */
  6787.  
  6788.         if (getone) {                   /* GET */
  6789.             char * p;
  6790.             s = line;
  6791.             src = line;                 /* Server name */
  6792.             done = 1;
  6793.             debug(F111,"ftp get file",s,0);
  6794.         } else if (mget) {              /* MGET */
  6795.             src = mgetlist[mgetx];
  6796.             debug(F111,"ftp mget remote_files A",src,first);
  6797.             s = (char *)remote_files(first,
  6798.                      (CHAR *)mgetlist[mgetx],
  6799.                      (CHAR *)pv[SND_PAT].sval,
  6800.                      0
  6801.                      );
  6802.             debug(F110,"ftp mget remote_files B",s,0);
  6803.             if (!s) s = "";
  6804.             if (!*s) {
  6805.                 first = 1;
  6806.                 if (listfile) {        /* Names from listfile */
  6807.                   again:
  6808.                     tmpbuf[0] = NUL;
  6809.                     while (!tmpbuf[0]) {
  6810.                         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  6811.                             zclose(ZMFILE);
  6812.                             debug(F110,"ftp get listfile EOF",
  6813.                                   pv[SND_FIL].sval,0);
  6814.                             makestr(&(mgetlist[0]),NULL);
  6815.                             s = NULL;
  6816.                             done = 1;
  6817.                             break;
  6818.                         }
  6819.                     }
  6820.                     if (done)
  6821.                       continue;
  6822.  
  6823.                     makestr(&(mgetlist[0]),tmpbuf);
  6824.             debug(F110,"ftp get listfile next",tmpbuf,0);
  6825.                     s = (char *)remote_files(first,
  6826.                          (CHAR *)mgetlist[0],
  6827.                          (CHAR *)pv[SND_PAT].sval,
  6828.                          0
  6829.                          );
  6830.             debug(F110,"ftp mget remote_files C",s,0);
  6831.                     if (!s) {
  6832.                         ftscreen(SCR_FN,'F',0L,s);
  6833.                         ftscreen(SCR_ST,ST_MSG,0L,"File not found");
  6834.                         tlog(F110,"ftp get file not found:",s,0);
  6835.                         goto again;
  6836.                     }
  6837.                 } else {        /* Names from command line */
  6838.                     mgetx++;
  6839.                     if (mgetx < mgetn)
  6840.               s = (char *)remote_files(first,
  6841.                            (CHAR *)mgetlist[mgetx],
  6842.                            (CHAR *)pv[SND_PAT].sval,
  6843.                            0
  6844.                            );
  6845.                     else
  6846.               s = NULL;
  6847.             if (!s) mgetx++;
  6848.             debug(F111,"ftp mget remote_files D",s,mgetx);
  6849.                 }
  6850.                 if (!s) {
  6851.             if (!first || mgetx >= mgetn) {
  6852.             done = 1;
  6853.             break;
  6854.             } else {
  6855.             continue;
  6856.             }
  6857.                 }
  6858.             }
  6859.         }
  6860.     debug(F111,"ftp mget remote_files E",s,0);
  6861.         /*
  6862.           The semantics of NLST are ill-defined.  Suppose we have just sent
  6863.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6864.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  6865.           RETR commands based on the pathless names are not going to work.
  6866.         */
  6867.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6868.             char * s3;
  6869.             if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  6870.                 int len, left = 4096;
  6871.                 char * tmp = xtmpbuf;
  6872.                 len = s3 - mgetlist[mgetx] + 1;
  6873.                 ckstrncpy(tmp,mgetlist[mgetx],left);
  6874.                 tmp += len;
  6875.                 left -= len;
  6876.                 ckstrncpy(tmp,s,left);
  6877.                 s = xtmpbuf;
  6878.         debug(F111,"ftp mget remote_files F",s,0);
  6879.             }
  6880.         }
  6881.         first = 0;
  6882.         skipthis = 0;                   /* File selection... */
  6883.         msg = "";
  6884.         nam = s;                        /* Filename (without path) */
  6885.         rc = 0;                         /* Initial return code */
  6886.         s2 = "";
  6887.  
  6888.         if (!getone && !skipthis) {     /* For MGET and MDELETE... */
  6889.             char c, * p = s;
  6890.             int srvpath = 0;
  6891.             int usrpath = 0;
  6892.             int i, k = 0;
  6893.  
  6894.         debug(F111,"ftp mget havetype",s,havetype);
  6895.         if (havetype > 0 && havetype != FTYP_FILE) {
  6896.         /* Server says it's not file... */
  6897.         debug(F110,"ftp mget not-a-file",s,0);
  6898.         continue;
  6899.         }
  6900. /*
  6901.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  6902.   But if the client did not ask for a recursive list, we have to ignore any
  6903.   server files that include a pathname that extends beyond any path that
  6904.   was included in the user's request.
  6905.  
  6906.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  6907.   get the user's path segment.  Then, for each incoming file, if it begins
  6908.   with the same path segment, we must strip it (point past it).
  6909. */
  6910.             src = mgetlist[mgetx];      /* In case it moved! */
  6911.         if (src) {
  6912.         for (i = 0; src[i]; i++) { /* Find rightmost path separator */
  6913.             if (ispathsep(src[i])) /* in user's pathname */
  6914.               k = i + 1;
  6915.         }
  6916.         }
  6917.             usrpath = k;                /* User path segment length */
  6918.             debug(F111,"ftp get usrpath",src,usrpath);
  6919.  
  6920.             p = s;                      /* Server filename */
  6921.             while ((c = *p++)) {        /* Look for path in server filename */
  6922.                 if (ispathsep(c)) {
  6923.             /* haspath++; */
  6924.                     nam = p;            /* Pathless name (for ckmatch) */
  6925.                     srvpath = p - s;    /* Server path segment length */
  6926.                 }
  6927.             }
  6928.             debug(F111,"ftp get srvpath",s,srvpath);
  6929.  
  6930.         if (usrpath == 0) {
  6931. /*
  6932.   Here we handle the case where the user said "mget foo" where foo is a
  6933.   directory name, and the server is sending back names like "foo/file1",
  6934.   "foo/file2", etc.  This is a nasty trick but it's necessary because the
  6935.   user can't compensate by typing "mget foo/" because then the server is
  6936.   likely to send back "foo//file1, foo//file2" etc, and we still won't
  6937.   get a match...
  6938. */
  6939.         int srclen = 0, srvlen = 0;
  6940.         if (src) srclen = strlen(src);
  6941.         if (s) srvlen = strlen(s);
  6942.         if (src && (srvlen > srclen)) {
  6943.             if (!strncmp(src,s,srclen) && ispathsep(s[srclen])) {
  6944.             char * tmpsrc = NULL;
  6945.             tmpsrc = (char *)malloc(srclen + 2);
  6946.             strncpy(tmpsrc,src,srclen);
  6947.             tmpsrc[srclen] = s[srclen];
  6948.             tmpsrc[srclen+1] = NUL;
  6949.             free(mgetlist[mgetx]);
  6950.             mgetlist[mgetx] = tmpsrc;
  6951.             tmpsrc = NULL;
  6952.             src = mgetlist[mgetx];
  6953.             usrpath = srclen+1;
  6954.             }                  
  6955.         }
  6956.         }
  6957. /*
  6958.   If as-name not given and server filename includes path that matches
  6959.   the pathname from the user's file specification, we must trim the common
  6960.   path prefix from the server's name when constructing the local name.
  6961. */
  6962.             if (src &&            /* Wed Sep 25 17:27:48 2002 */
  6963.         !asnambuf[0] &&
  6964.         !recursive &&        /* Thu Sep 19 16:11:59 2002 */
  6965.         (srvpath > 0) &&
  6966.         !strncmp(src,s,usrpath)) {
  6967.                 s2 = s + usrpath;       /* Local name skips past remote path */
  6968.             }
  6969. #ifdef COMMENT
  6970.         /* This doesn't work if the path prefix contains wildcards! */
  6971.         haspath = (srvpath > usrpath);
  6972. #else
  6973.         {                /* Count path segments instead */
  6974.         int x1 = 0, x2 = 0;
  6975.         char *p;
  6976.         for (p = s; *p; p++)
  6977.           if (ispathsep(*p)) x1++;
  6978.         for (p = src; *p; p++)
  6979.           if (ispathsep(*p)) x2++;
  6980.         haspath = x1 > x2;
  6981.         debug(F111,"ftp get server path segments",s,x1);
  6982.         debug(F111,"ftp get user   path segments",src,x2);
  6983.         }
  6984. #endif /* COMMENT */
  6985.             debug(F111,"ftp get haspath",s+usrpath,haspath);
  6986.  
  6987.             if (haspath) {              /* Server file has path segments? */
  6988.                 if (!recursive) {       /* [M]GET /RECURSIVE? */
  6989. /*
  6990.   We did not ask for a recursive listing, but the server is sending us one
  6991.   anyway (as wu-ftpd is known to do).  We get here if the current filename
  6992.   includes a path segment beyond any path segment we asked for in our
  6993.   non-recursive [M]GET command.  We MUST skip this file.
  6994. */
  6995.                     debug(F111,"ftp get skipping because of path",s,0);
  6996.                     continue;
  6997.                 }
  6998.             }
  6999.         } else if (getone && !skipthis && !recursive) {
  7000.             char * p = nam;
  7001.             while ((c = *p++)) {        /* Strip path from local name */
  7002.                 if (ispathsep(c))
  7003.                   nam = p;
  7004.             }
  7005.             s2 = nam;
  7006.         }
  7007.         if (!*nam)                      /* Name without path */
  7008.           nam = s;
  7009.  
  7010.         if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  7011.             if (nam[0] == '.')
  7012.           continue;
  7013.         }
  7014.         if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  7015.         int xx;
  7016.             for (i = 0; i < NSNDEXCEPT; i++) {
  7017.                 if (!rcvexcept[i]) {
  7018.                     break;
  7019.                 }
  7020.         xx = ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1);
  7021.         debug(F111,"ftp mget /except match",rcvexcept[i],xx);
  7022.                 if (xx) {
  7023.                     tlog(F100," refused: exception list","",0);
  7024.             msg = "Refused: Exception List";
  7025.                     skipthis++;
  7026.                     break;
  7027.                 }
  7028.             }
  7029.         }
  7030.         if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  7031.             if (ckmatch(
  7032. #ifdef CKREGEX
  7033.                         "*.~[0-9]*~"
  7034. #else
  7035.                         "*.~*~"
  7036. #endif /* CKREGEX */
  7037.                         ,nam,0,1) > 0)
  7038.               continue;
  7039.         }
  7040.         if (!x_xla) {                   /* If translation is off */
  7041.             x_csl = -2;                 /* unset the charsets */
  7042.             x_csr = -2;
  7043.         }
  7044.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  7045.         if (!*s2)                       /* Local name */
  7046.           s2 = asnambuf;                /* As-name */
  7047.  
  7048.     if (!*s2)            /* Sat Nov 16 19:19:39 2002 */
  7049.       s2 = nam;
  7050.  
  7051.         debug(F110,"ftp get filnam ",s,0);
  7052.         debug(F110,"ftp get asnam A",s2,0);
  7053.  
  7054.         /* Receiving to real file */
  7055.         if (!pipesend &&
  7056. #ifdef PIPESEND
  7057.             !rcvfilter &&
  7058. #endif /* PIPESEND */
  7059.             !toscreen) {
  7060. #ifndef NOSPL
  7061.             /* Do this here so we can decide whether to skip */
  7062.             if (cmd_quoting && !skipthis && asnambuf[0]) {
  7063.                 int n; char *p;
  7064.                 n = TMPBUFSIZ;
  7065.                 p = tmpbuf;
  7066.                 zzstring(asnambuf,&p,&n);
  7067.                 s2 = tmpbuf;
  7068.                 debug(F111,"ftp get asname B",s2,updating);
  7069.             }
  7070. #endif /* NOSPL */
  7071.  
  7072.         local = *s2 ? s2 : s;
  7073.  
  7074.         if (!skipthis && x_fnc == XYFX_D) { /* File Collision = Discard */
  7075.         int x;
  7076.         x = zchki(local);
  7077.         debug(F111,"ftp get DISCARD zchki",local,x);
  7078.         if (x > -1) {
  7079.             skipthis++;
  7080.             debug(F110,"ftp get skip name",local,0);
  7081.             tlog(F100," refused: name","",0);
  7082.             msg = "Refused: Name";
  7083.         }
  7084.         }
  7085.  
  7086. #ifdef DOUPDATE
  7087.             if (!skipthis && updating) { /* If updating and not yet skipping */
  7088.                 if (zchki(local) > -1) {
  7089.                     x = chkmodtime(local,s,0);
  7090. #ifdef DEBUG
  7091.             if (deblog) {
  7092.             if (updating == 2)
  7093.               debug(F111,"ftp get /dates-diff chkmodtime",local,x);
  7094.             else
  7095.               debug(F111,"ftp get /update chkmodtime",local,x);
  7096.             }
  7097. #endif /* DEBUG */
  7098.             if ((updating == 1 && x > 0) ||  /* /UPDATE */
  7099.             (updating == 2 && x == 1)) { /* /DATES-DIFFER */
  7100.             skipthis++;
  7101.             tlog(F100," refused: date","",0);
  7102.             msg = "Refused: Date";
  7103.                         debug(F110,"ftp get skip date",local,0);
  7104.                     }
  7105.                 }
  7106.             }
  7107. #endif /* DOUPDATE */
  7108.         }
  7109.         /* Initialize file size to -1 in case server doesn't understand */
  7110.         /* SIZE command, so xxscreen() will know we don't know the size */
  7111.  
  7112.         fsize = -1L;
  7113.  
  7114.     /* Ask for size now only if we need it for selection */
  7115.     /* because if you're going thru a list 100,000 files to select */
  7116.     /* a small subset, 100,000 SIZE commands can take hours... */
  7117.  
  7118.     gotsize = 0;
  7119.         if (!mdel && !skipthis &&        /* Don't need size for DELE... */
  7120.         (getsmaller > -1L || getlarger > -1L)) {
  7121.         if (havesize > -1L) {    /* Already have file size? */
  7122.         fsize = havesize;
  7123.         gotsize = 1;
  7124.         } else {            /* No - must ask server */
  7125.         /*
  7126.           Prior to sending the NLST command we necessarily put the
  7127.           server into ASCII mode.  We must now put it back into the
  7128.           the requested mode so the upcoming SIZE command returns
  7129.           right kind of size; this is especially important for
  7130.           GET /RECOVER; otherwise the server returns the "ASCII" size
  7131.           of the file, rather than its true size.
  7132.         */
  7133.         changetype(ftp_typ,0);    /* Change to requested type */
  7134.         fsize = -1L;
  7135.         if (sizeok) {
  7136.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7137.             if (x == REPLY_COMPLETE) {
  7138.             fsize = atol(&ftp_reply_str[4]);
  7139.             gotsize = 1;
  7140.             }
  7141.         }
  7142.         }
  7143.             if (gotsize) {
  7144.                 if (getsmaller > -1L && fsize >= getsmaller)
  7145.                   skipthis++;
  7146.                 if (getlarger > -1L && fsize <= getlarger)
  7147.                   skipthis++;
  7148.                 if (skipthis) {
  7149.                     debug(F111,"ftp get skip size",s,fsize);
  7150.                     tlog(F100," refused: size","",0);
  7151.                     msg = "Refused: Size";
  7152.                 }
  7153. #ifdef COMMENT
  7154.             } else if (getone) {
  7155.                 /* SIZE can fail for many reasons.  Does the file exist? */
  7156.                 x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  7157.                 if (x != REPLY_COMPLETE) {
  7158.                     printf(">>> FILE NOT FOUND: %s\n",s);
  7159.                     break;
  7160.                 }
  7161. #endif /* COMMENT */
  7162.             }
  7163.         }
  7164.         if (skipthis) {                 /* Skipping this file? */
  7165.             ftscreen(SCR_FN,'F',0L,s);
  7166.             if (msg)
  7167.               ftscreen(SCR_ST,ST_ERR,0L,msg);
  7168.             else
  7169.               ftscreen(SCR_ST,ST_SKIP,0L,s);
  7170.             continue;
  7171.         }
  7172.         if (fp_nml) {                   /* /NAMELIST only - no transfer */
  7173.             fprintf(fp_nml,"%s\n",s);
  7174.             continue;
  7175.         }
  7176.         if (recursive && haspath && !pipesend
  7177. #ifdef PIPESEND
  7178.             && !rcvfilter
  7179. #endif /* PIPESEND */
  7180.             ) {
  7181.         int x;
  7182.  
  7183. #ifdef NOMKDIR
  7184.         x = -1;
  7185. #else
  7186.             x = zmkdir(local);        /* Try to make the directory */
  7187. #endif /* NOMKDIR */
  7188.  
  7189.             if (x < 0) {
  7190.                 rc = -1;                /* Failure is fatal */
  7191.                 if (geterror) {
  7192.                     status = 0;
  7193.                     ftscreen(SCR_EM,0,0L,"Directory creation failure");
  7194.                     break;
  7195.                 }
  7196.             }
  7197.         }
  7198.  
  7199.         /* Not skipping */
  7200.  
  7201.     selected++;            /* Count this file as selected */
  7202.         pn = NULL;
  7203.  
  7204.     if (!gotsize && !mdel) {    /* Didn't get size yet */
  7205.         if (havesize > -1L) {    /* Already have file size? */
  7206.         fsize = havesize;
  7207.         gotsize = 1;
  7208.         } else {            /* No - must ask server */
  7209.         fsize = -1L;
  7210.         if (sizeok) {
  7211.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7212.             if (x == REPLY_COMPLETE) {
  7213.             fsize = atol(&ftp_reply_str[4]);
  7214.             gotsize = 1;
  7215.             }
  7216.         }
  7217.         }
  7218.     }
  7219.         if (mdel) {                     /* [M]DELETE */
  7220.             if (displa && !ftp_vbm)
  7221.               printf(" %s...",s);
  7222.             rc =
  7223.              (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  7224.             if (rc > -1) {
  7225.                 tlog(F110,"ftp mdelete",s,0);
  7226.                 if (displa && !ftp_vbm)
  7227.                   printf("OK\n");
  7228.             } else {
  7229.                 tlog(F110,"ftp mdelete failed:",s,0);
  7230.                 if (displa)
  7231.                   printf("Failed\n");
  7232.             }
  7233. #ifndef NOSPL
  7234. #ifdef PIPESEND
  7235.         } else if (rcvfilter) {         /* [M]GET with filter */
  7236.             int n; char * p;
  7237.             n = CKMAXPATH;
  7238.             p = tmpbuf;                 /* Safe - no asname with filter */
  7239.             zzstring(rcvfilter,&p,&n);
  7240.             if (n > -1)
  7241.               pn = tmpbuf;
  7242.             debug(F111,"ftp get rcvfilter",pn,n);
  7243. #endif /* PIPESEND */
  7244. #endif /* NOSPL */
  7245.             if (toscreen) s2 = "-";
  7246.         } else if (pipesend) {          /* [M]GET /COMMAND */
  7247.             int n; char * p;
  7248.             n = CKMAXPATH;
  7249.             p = tmpbuf;                 /* Safe - no asname with filter */
  7250.             zzstring(pipename,&p,&n);
  7251.             if (n > -1)
  7252.               pn = tmpbuf;
  7253.             debug(F111,"ftp get pipename",pipename,n);
  7254.             if (toscreen) s2 = "-";
  7255.         } else {                        /* [M]GET with no pipes or filters */
  7256.             debug(F111,"ftp get s2 A",s2,x_cnv);
  7257.             if (toscreen) {
  7258.                 s2 = "-";               /* (hokey convention for stdout) */
  7259.             } else if (!*s2) {          /* No asname? */
  7260.                 if (x_cnv) {            /* If converting */
  7261.                     nzrtol(s,tmpbuf,x_cnv,1,CKMAXPATH); /* convert */
  7262.                     s2 = tmpbuf;
  7263.                     debug(F110,"ftp get nzrtol",s2,0);
  7264.                 } else                  /* otherwise */
  7265.                   s2 = s;               /* use incoming file's name */
  7266.             }
  7267.             debug(F110,"ftp get s2 B",s2,0);
  7268.  
  7269.             /* If local file already exists, take collision action */
  7270.  
  7271.             if (!pipesend &&
  7272. #ifdef PIPESEND
  7273.                 !rcvfilter &&
  7274. #endif /* PIPESEND */
  7275.                 !toscreen) {
  7276.                 x = zchki(s2);
  7277.                 debug(F111,"ftp get zchki",s2,x);
  7278.                 debug(F111,"ftp get x_fnc",s2,x_fnc);
  7279.  
  7280.                 if (x > -1 && !restart) {
  7281.             int x = -1;
  7282.             char * newname = NULL;
  7283.  
  7284.                     switch (x_fnc) {
  7285.                       case XYFX_A:      /* Append */
  7286.                         append = 1;
  7287.                         break;
  7288.                       case XYFX_R:      /* Rename */
  7289.                       case XYFX_B:    /* Backup */
  7290.             znewn(s2,&newname); /* Make unique name */
  7291.             debug(F110,"ftp get znewn",newname,0);
  7292.             if (x_fnc == XYFX_B) { /* Backup existing file */
  7293.                 x = zrename(s2,newname);
  7294.                 debug(F111,"ftp get backup zrename",newname,x);
  7295.             } else {      /* Rename incoming file */
  7296.                 x = ckstrncpy(tmpbuf,newname,CKMAXPATH+1);
  7297.                 s2 = tmpbuf;
  7298.                 debug(F111,"ftp get rename incoming",newname,x);
  7299.             }
  7300.             if (x < 0) {
  7301.                 ftscreen(SCR_EM,0,0L,"Backup/Rename failed");
  7302.                 x = 0;
  7303.                 goto xgetx;
  7304.             }
  7305.             break;
  7306.                       case XYFX_D:      /* Discard (already handled above) */
  7307.                       case XYFX_U:      /* Update (ditto) */
  7308.                       case XYFX_M:      /* Update (ditto) */
  7309.                       case XYFX_X:      /* Overwrite */
  7310.                         break;
  7311.                     }
  7312.                 }
  7313.             }
  7314.         }
  7315.         if (!mdel) {
  7316. #ifdef PIPESEND
  7317.             debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  7318. #endif /* PIPESEND */
  7319.             if (pipesend && !toscreen)
  7320.               s2 = NULL;
  7321. #ifdef DEBUG
  7322.             if (deblog) {
  7323.                 debug(F101,"ftp get x_xla","",x_xla);
  7324.                 debug(F101,"ftp get x_csl","",x_csl);
  7325.                 debug(F101,"ftp get x_csr","",x_csr);
  7326.                 debug(F101,"ftp get append","",append);
  7327.             }
  7328. #endif /* DEBUG */
  7329.  
  7330.             rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  7331.  
  7332. #ifdef DEBUG
  7333.             if (deblog) {
  7334.                 debug(F111,"ftp get rc",s,rc);
  7335.                 debug(F111,"ftp get cancelfile",s,cancelfile);
  7336.                 debug(F111,"ftp get cancelgroup",s,cancelgroup);
  7337.                 debug(F111,"ftp get renaming",s,renaming);
  7338.             }
  7339. #endif /* DEBUG */
  7340.         }
  7341.         if (rc > -1) {
  7342.             good++;
  7343.             status = 1;
  7344.             if (!cancelfile) {
  7345.                 if (deleting) {         /* GET /DELETE (source file) */
  7346.                     rc =
  7347.                       (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  7348.                         ? 1 : -1;
  7349.                     tlog(F110, (rc > -1) ?
  7350.                          " deleted" : " failed to delete", s, 0);
  7351.                 } else if (renaming && rcv_rename && !toscreen) {
  7352.                     char *p;            /* Rename downloaded file */
  7353. #ifndef NOSPL
  7354.                     char tmpbuf[CKMAXPATH+1];
  7355.                     int n;
  7356.                     n = CKMAXPATH;
  7357.                     p = tmpbuf;
  7358.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7359.                     zzstring(rcv_rename,&p,&n);
  7360.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7361.                     p = tmpbuf;
  7362. #else
  7363.                     p = rcv_rename;
  7364. #endif /* NOSPL */
  7365.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7366.                     debug(F111,"doftpget /RENAME zrename",p,rc);
  7367.                     tlog(F110, (rc > -1) ?
  7368.                          " renamed to" :
  7369.                          " failed to rename to",
  7370.                          p,
  7371.                          0
  7372.                          );
  7373.                 } else if (moving && rcv_move && !toscreen) {
  7374.                     char *p;            /* Move downloaded file */
  7375. #ifndef NOSPL
  7376.                     char tmpbuf[CKMAXPATH+1];
  7377.                     int n;
  7378.                     n = TMPBUFSIZ;
  7379.                     p = tmpbuf;
  7380.                     debug(F111,"ftp get /move-to",rcv_move,0);
  7381.                     zzstring(rcv_move,&p,&n);
  7382.                     p = tmpbuf;
  7383. #else
  7384.                     p = rcv_move;
  7385. #endif /* NOSPL */
  7386.                     debug(F111,"ftp get /move-to",p,0);
  7387.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7388.                     debug(F111,"doftpget /MOVE zrename",p,rc);
  7389.                     tlog(F110, (rc > -1) ?
  7390.                          " moved to" : " failed to move to", p, 0);
  7391.                 }
  7392.                 if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  7393.                     char * s = pv[SND_SRN].sval;
  7394.                     char * srvrn = pv[SND_SRN].sval;
  7395.                     char tmpbuf[CKMAXPATH+1];
  7396. #ifndef NOSPL
  7397.                     int y;              /* Pass it thru the evaluator */
  7398.                     extern int cmd_quoting; /* for \v(filename) */
  7399.                     debug(F111,"ftp get srv_renam",s,1);
  7400.  
  7401.                     if (cmd_quoting) {
  7402.                         y = CKMAXPATH;
  7403.                         s = (char *)tmpbuf;
  7404.                         zzstring(srvrn,&s,&y);
  7405.                         s = (char *)tmpbuf;
  7406.                     }
  7407. #endif /* NOSPL */
  7408.                     debug(F111,"ftp get srv_renam",s,1);
  7409.                     if (s) if (*s) {
  7410.                         int x;
  7411.                         x = ftp_rename(s2,s);
  7412.                         debug(F111,"ftp get ftp_rename",s2,x);
  7413.                         tlog(F110, (x > 0) ?
  7414.                              " renamed source file to" :
  7415.                              " failed to rename source file to",
  7416.                              s,
  7417.                              0
  7418.                              );
  7419.                         if (x < 1)
  7420.               return(-1);
  7421.                     }
  7422.                 }
  7423.             }
  7424.         }
  7425.         if (cancelfile)
  7426.           continue;
  7427.         if (rc < 0) {
  7428.             ftp_fai++;
  7429.             if (geterror) {
  7430.                 status = 0;
  7431.                 ftscreen(SCR_EM,0,0L,"Fatal download error");
  7432.                 done++;
  7433.             }
  7434.         }
  7435.     }
  7436. #ifdef DEBUG
  7437.     if (deblog) {
  7438.     debug(F101,"ftp get status","",status);
  7439.     debug(F101,"ftp get cancelgroup","",cancelgroup);
  7440.     debug(F101,"ftp get cancelfile","",cancelfile);
  7441.     debug(F101,"ftp get selected","",selected);
  7442.     debug(F101,"ftp get good","",good);
  7443.     }
  7444. #endif /* DEBUG */
  7445.  
  7446.     if (selected == 0) {        /* No files met selection criteria */
  7447.     status = 1;            /* which is a kind of success. */
  7448.     } else if (status > 0) {        /* Some files were selected */
  7449.         if (cancelgroup)        /* but MGET was canceled */
  7450.           status = 0;            /* so MGET failed */
  7451.         else if (cancelfile && good < 1) /* If file was canceled */
  7452.           status = 0;            /* MGET failed if it got no files */
  7453.     }
  7454.     success = status;
  7455.     x = success;
  7456.     debug(F101,"ftp get success","",success);
  7457.  
  7458.   xgetx:
  7459.     pipesend = pipesave;                /* Restore global pipe selection */
  7460.     if (fp_nml) {                       /* Close /NAMELIST */
  7461.         if (fp_nml != stdout)
  7462.           fclose(fp_nml);
  7463.         fp_nml = NULL;
  7464.     }
  7465.     if (x > -1) {                       /* Download successful */
  7466. #ifdef GFTIMER
  7467.         t1 = gmstimer();                /* End time */
  7468.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  7469.         if (!sec) sec = 0.001;
  7470.         fptsecs = sec;
  7471. #else
  7472.         sec = (t1 - t0) / 1000;
  7473.         if (!sec) sec = 1;
  7474. #endif /* GFTIMER */
  7475.         tfcps = (long) (tfc / sec);
  7476.         tsecs = (int)sec;
  7477.         lastxfer = W_FTP|W_RECV;
  7478.         xferstat = success;
  7479.     }
  7480.     if (dpyactive)
  7481.       ftscreen(SCR_TC,0,0L,"");
  7482. #ifdef CK_TMPDIR
  7483.     if (f_tmpdir) {                     /* If we changed to download dir */
  7484.         zchdir((char *) savdir);        /* Go back where we came from */
  7485.         f_tmpdir = 0;
  7486.     }
  7487. #endif /* CK_TMPDIR */
  7488.  
  7489.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  7490.         if (pv[i].sval)
  7491.           free(pv[i].sval);
  7492.     }
  7493.     for (i = 0; i < mgetn; i++)         /* MGET list too */
  7494.       makestr(&(mgetlist[i]),NULL);
  7495.  
  7496.     if (cancelgroup)            /* Clear temp-file stack */
  7497.       mlsreset();
  7498.  
  7499.     ftreset();                          /* Undo switch effects */
  7500.     dpyactive = 0;
  7501.     return(x);
  7502. }
  7503.  
  7504. static struct keytab ftprmt[] = {
  7505.     { "cd",        XZCWD, 0 },
  7506.     { "cdup",      XZCDU, 0 },
  7507.     { "cwd",       XZCWD, CM_INV },
  7508.     { "delete",    XZDEL, 0 },
  7509.     { "directory", XZDIR, 0 },
  7510.     { "exit",      XZXIT, 0 },
  7511.     { "help",      XZHLP, 0 },
  7512.     { "login",     XZLGI, 0 },
  7513.     { "logout",    XZLGO, 0 },
  7514.     { "mkdir",     XZMKD, 0 },
  7515.     { "pwd",       XZPWD, 0 },
  7516.     { "rename",    XZREN, 0 },
  7517.     { "rmdir",     XZRMD, 0 },
  7518.     { "type",      XZTYP, 0 },
  7519.     { "", 0, 0 }
  7520. };
  7521. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  7522.  
  7523. int
  7524. doftpsite() {                /* Send a SITE command */
  7525.     int reply;
  7526.     char * s;
  7527.     int lcs = -1, rcs = -1;
  7528. #ifndef NOCSETS
  7529.     if (ftp_xla) {
  7530.         lcs = ftp_csl;
  7531.         if (lcs < 0) lcs = fcharset;
  7532.         rcs = ftp_csx;
  7533.         if (rcs < 0) rcs = ftp_csr;
  7534.     }
  7535. #endif /* NOCSETS */
  7536.     if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7537.       return(x);
  7538.     CHECKCONN();
  7539.     ckstrncpy(line,s,LINBUFSIZ);
  7540.     if (testing) printf(" ftp site \"%s\"...\n",line);
  7541.     if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  7542.     do {
  7543.         reply = getreply(0,lcs,rcs,ftp_vbm,0);
  7544.     } while (reply == REPLY_PRELIM);
  7545.     }
  7546.     return(success = (reply == REPLY_COMPLETE));
  7547. }
  7548.  
  7549.  
  7550. int
  7551. dosetftppsv() {                /* Passive mode */
  7552.     x = seton(&ftp_psv);
  7553.     if (x > 0) passivemode = ftp_psv;
  7554.     return(x);
  7555. }
  7556.  
  7557. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  7558.  
  7559. int
  7560. doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  7561.     /* cx == 0 means REMOTE */
  7562.     /* cx != 0 is a XZxxx value */
  7563.     char * s;
  7564.  
  7565.     if (who != 0)
  7566.       return(0);
  7567.  
  7568.     if (cx == 0) {
  7569.         if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  7570.           return(x);
  7571.         cx = x;
  7572.     }
  7573.     switch (cx) {
  7574.       case XZCDU:                       /* CDUP */
  7575.         if ((x = cmcfm()) < 0) return(x);
  7576.         return(doftpcdup());
  7577.  
  7578.       case XZCWD:                       /* RCD */
  7579.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7580.           return(x);
  7581.         ckstrncpy(line,s,LINBUFSIZ);
  7582.         return(doftpcwd((char *)line,1));
  7583.       case XZPWD:                       /* RPWD */
  7584.         return(doftppwd());
  7585.       case XZDEL:                       /* RDEL */
  7586.         return(doftpget(FTP_MDE,1));
  7587.       case XZDIR:                       /* RDIR */
  7588.         return(doftpdir(FTP_DIR));
  7589.       case XZHLP:                       /* RHELP */
  7590.         return(doftpxhlp());
  7591.       case XZMKD:                       /* RMKDIR */
  7592.         return(doftpmkd());
  7593.       case XZREN:                       /* RRENAME */
  7594.         return(doftpren());
  7595.       case XZRMD:                       /* RRMDIR */
  7596.         return(doftprmd());
  7597.       case XZLGO:                       /* LOGOUT */
  7598.         return(doftpres());
  7599.       case XZXIT:                       /* EXIT */
  7600.         return(ftpbye());
  7601.     }
  7602.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  7603.     return(-9);
  7604. }
  7605.  
  7606. int
  7607. doxftp() {                              /* Command parser for built-in FTP */
  7608.     int cx, n;
  7609.     struct FDB kw, fl;
  7610.     char * s;
  7611.     int usetls = 0;
  7612.     int lcs = -1, rcs = -1;
  7613.  
  7614. #ifndef NOCSETS
  7615.     if (ftp_xla) {
  7616.         lcs = ftp_csl;
  7617.         if (lcs < 0) lcs = fcharset;
  7618.         rcs = ftp_csx;
  7619.         if (rcs < 0) rcs = ftp_csr;
  7620.     }
  7621. #endif /* NOCSETS */
  7622.  
  7623.     if (inserver)                       /* FTP not allowed in IKSD. */
  7624.       return(-2);
  7625.  
  7626.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  7627.         ftp_typ = g_ftp_typ;
  7628.         /* g_ftp_typ = -1; */
  7629.     }
  7630. #ifdef COMMENT
  7631. /*
  7632.   We'll set the collision action locally in doftpget() based on whether
  7633.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  7634. */
  7635.     if (ftp_fnc < 0)                    /* Inherit global collision action */
  7636.       ftp_fnc = fncact;                 /* if none specified for FTP */
  7637. #endif /* COMMENT */
  7638.  
  7639.     /* Restore global verbose mode */
  7640.     if (ftp_deb)
  7641.       ftp_vbm = 1;
  7642.     else if (quiet)
  7643.       ftp_vbm = 0;
  7644.     else
  7645.       ftp_vbm = ftp_vbx;
  7646.  
  7647.     ftp_dates &= 1;            /* Undo any previous /UPDATE switch */
  7648.  
  7649.     dpyactive = 0;                      /* Reset global transfer-active flag */
  7650.     printlines = 0;                     /* Reset printlines */
  7651.  
  7652.     if (fp_nml) {                       /* Reset /NAMELIST */
  7653.         if (fp_nml != stdout)
  7654.           fclose(fp_nml);
  7655.         fp_nml = NULL;
  7656.     }
  7657.     makestr(&ftp_nml,NULL);
  7658.  
  7659.     cmfdbi(&kw,                         /* First FDB - commands */
  7660.            _CMKEY,                      /* fcode */
  7661.            "Hostname; or FTP command",  /* help */
  7662.            "",                          /* default */
  7663.            "",                          /* addtl string data */
  7664.            nftpcmd,                     /* addtl numeric data 1: tbl size */
  7665.            0,                           /* addtl numeric data 2: none */
  7666.            xxstring,                    /* Processing function */
  7667.            ftpcmdtab,                   /* Keyword table */
  7668.            &fl                          /* Pointer to next FDB */
  7669.            );
  7670.     cmfdbi(&fl,                         /* A host name or address */
  7671.            _CMFLD,                      /* fcode */
  7672.            "Hostname or address",       /* help */
  7673.            "",                          /* default */
  7674.            "",                          /* addtl string data */
  7675.            0,                           /* addtl numeric data 1 */
  7676.            0,                           /* addtl numeric data 2 */
  7677.            xxstring,
  7678.            NULL,
  7679.            NULL
  7680.            );
  7681.     x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
  7682.     if (x == -3) {
  7683.         printf("?ftp what? \"help ftp\" for hints\n");
  7684.         return(-9);
  7685.     }
  7686.     if (x < 0)
  7687.       return(x);
  7688.     if (cmresult.fcode == _CMFLD) {     /* If hostname */
  7689.         return(openftp(cmresult.sresult,0)); /* go open the connection */
  7690.     } else {
  7691.         cx = cmresult.nresult;
  7692.     }
  7693.     switch (cx) {
  7694.       case FTP_ACC:                     /* ACCOUNT */
  7695.         if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  7696.           return(x);
  7697.         CHECKCONN();
  7698.         makestr(&ftp_acc,s);
  7699.         if (testing)
  7700.           printf(" ftp account: \"%s\"\n",ftp_acc);
  7701.         success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  7702.         return(success);
  7703.  
  7704.       case FTP_GUP:                     /* Go UP */
  7705.         if ((x = cmcfm()) < 0) return(x);
  7706.         CHECKCONN();
  7707.         if (testing) printf(" ftp cd: \"(up)\"\n");
  7708.         return(success = doftpcdup());
  7709.  
  7710.       case FTP_CWD:                     /* CD */
  7711.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7712.           return(x);
  7713.         CHECKCONN();
  7714.         ckstrncpy(line,s,LINBUFSIZ);
  7715.         if (testing)
  7716.           printf(" ftp cd: \"%s\"\n", line);
  7717.         return(success = doftpcwd(line,1));
  7718.  
  7719.       case FTP_CHM:                     /* CHMOD */
  7720.         if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  7721.           return(x);
  7722.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7723.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7724.           return(x);
  7725.         CHECKCONN();
  7726.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  7727.         if (testing)
  7728.           printf(" ftp chmod: %s\n",ftpcmdbuf);
  7729.         success =
  7730.           (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7731.         return(success);
  7732.  
  7733.       case FTP_CLS:                     /* CLOSE FTP connection */
  7734.         if ((y = cmcfm()) < 0)
  7735.           return(y);
  7736.         CHECKCONN();
  7737.         if (testing)
  7738.           printf(" ftp closing...\n");
  7739.         ftpclose();
  7740.         return(success = 1);
  7741.  
  7742.       case FTP_DIR:                     /* DIRECTORY of remote files */
  7743.       case FTP_VDI:
  7744.         return(doftpdir(cx));
  7745.  
  7746.       case FTP_GET:                     /* GET a remote file */
  7747.       case FTP_RGE:                     /* REGET */
  7748.       case FTP_MGE:                     /* MGET */
  7749.       case FTP_MDE:                     /* MDELETE */
  7750.         return(doftpget(cx,1));
  7751.  
  7752.       case FTP_IDL:                     /* IDLE */
  7753.         if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  7754.           return(x);
  7755.         if ((y = cmcfm()) < 0)
  7756.           return(y);
  7757.         CHECKCONN();
  7758.         if (z < 0)  {                   /* Display idle timeout */
  7759.             if (testing)
  7760.               printf(" ftp query idle timeout...\n");
  7761.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  7762.         } else {                        /* Set idle timeout */
  7763.             if (testing)
  7764.               printf(" ftp idle timeout set: %d...\n",z);
  7765.             success =
  7766.               (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  7767.         }
  7768.         return(success);
  7769.  
  7770.       case FTP_MKD:                     /* MKDIR */
  7771.         return(doftpmkd());
  7772.  
  7773.       case FTP_MOD:                     /* MODTIME */
  7774.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7775.           return(x);
  7776.         CHECKCONN();
  7777.         ckstrncpy(line,s,LINBUFSIZ);
  7778.         if (testing)
  7779.           printf(" ftp modtime \"%s\"...\n",line);
  7780.         success = 0;
  7781.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  7782.             int flag = 0;
  7783.             char c, * s;
  7784.             struct tm tmremote;
  7785.  
  7786.             bzero((char *)&tmremote, sizeof(struct tm));
  7787.             s = ftp_reply_str;
  7788.             while ((c = *s++)) {
  7789.                 if (c == SP) {
  7790.                     flag++;
  7791.                     break;
  7792.                 }
  7793.             }
  7794.             if (flag) {
  7795.                 if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  7796.                            &tmremote.tm_year,
  7797.                            &tmremote.tm_mon,
  7798.                            &tmremote.tm_mday,
  7799.                            &tmremote.tm_hour,
  7800.                            &tmremote.tm_min,
  7801.                            &tmremote.tm_sec
  7802.                            ) == 6) {
  7803.                     success = 1;
  7804.             mdtmok = 1;
  7805.                     printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  7806.                            line,
  7807.                            tmremote.tm_year,
  7808.                            tmremote.tm_mon,
  7809.                            tmremote.tm_mday,
  7810.                            tmremote.tm_hour,
  7811.                            tmremote.tm_min,
  7812.                            tmremote.tm_sec
  7813.                            );
  7814.                 }
  7815.             }
  7816.         }
  7817.         return(success);
  7818.  
  7819.       case FTP_OPN:                     /* OPEN connection */
  7820. #ifdef COMMENT
  7821.         x = cmfld("IP hostname or address","",&s,xxstring);
  7822.         if (x < 0) {
  7823.             success = 0;
  7824.             return(x);
  7825.         }
  7826.         ckstrncpy(line,s,LINBUFSIZ);
  7827.         s = line;
  7828.         return(openftp(s,0));
  7829. #else
  7830.         {                               /* OPEN connection */
  7831.             char name[TTNAMLEN+1], *p;
  7832.             extern int network;
  7833.             extern char ttname[];
  7834.             if (network)                /* If we have a current connection */
  7835.               ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  7836.             else
  7837.               *name = '\0';             /* as default host */
  7838.             for (p = name; *p; p++)     /* Remove ":service" from end. */
  7839.               if (*p == ':') { *p = '\0'; break; }
  7840. #ifndef USETLSTAB
  7841.             x = cmfld("IP hostname or address",name,&s,xxstring);
  7842. #else
  7843.             cmfdbi(&kw,                 /* First FDB - commands */
  7844.                    _CMKEY,              /* fcode */
  7845.                    "Hostname or switch", /* help */
  7846.                    "",                  /* default */
  7847.                    "",                  /* addtl string data */
  7848.                    ntlstab,             /* addtl numeric data 1: tbl size */
  7849.                    0,                   /* addtl numeric data 2: none */
  7850.                    xxstring,            /* Processing function */
  7851.                    tlstab,              /* Keyword table */
  7852.                    &fl                  /* Pointer to next FDB */
  7853.                    );
  7854.             cmfdbi(&fl,                 /* A host name or address */
  7855.                    _CMFLD,              /* fcode */
  7856.                    "Hostname or address", /* help */
  7857.                    "",                  /* default */
  7858.                    "",                  /* addtl string data */
  7859.                    0,                   /* addtl numeric data 1 */
  7860.                    0,                   /* addtl numeric data 2 */
  7861.                    xxstring,
  7862.                    NULL,
  7863.                    NULL
  7864.                    );
  7865.  
  7866.             for (n = 0;; n++) {
  7867.                 x = cmfdb(&kw);         /* Parse a hostname or a keyword */
  7868.                 if (x == -3) {
  7869.                   printf("?ftp open what? \"help ftp\" for hints\n");
  7870.                   return(-9);
  7871.                 }
  7872.                 if (x < 0)
  7873.                   break;
  7874.                 if (cmresult.fcode == _CMFLD) { /* Hostname */
  7875.                     s = cmresult.sresult;
  7876.                     break;
  7877.                 } else if (cmresult.nresult == OPN_TLS) {
  7878.                     usetls = 1;
  7879.                 }
  7880.             }
  7881. #endif /* USETLSTAB */
  7882.             if (x < 0) {
  7883.                 success = 0;
  7884.                 return(x);
  7885.             }
  7886.             ckstrncpy(line,s,LINBUFSIZ);
  7887.             s = line;
  7888.             return(openftp(s,usetls));
  7889.         }
  7890. #endif /* COMMENT */
  7891.  
  7892.       case FTP_PUT:                     /* PUT */
  7893.       case FTP_MPU:                     /* MPUT */
  7894.       case FTP_APP:                     /* APPEND */
  7895.         return(doftpput(cx,1));
  7896.  
  7897.       case FTP_PWD:                     /* PWD */
  7898.         x = doftppwd();
  7899.         if (x > -1) success = x;
  7900.         return(x);
  7901.  
  7902.       case FTP_REN:                     /* RENAME */
  7903.         return(doftpren());
  7904.  
  7905.       case FTP_RES:                     /* RESET */
  7906.         return(doftpres());
  7907.  
  7908.       case FTP_HLP:                     /* (remote) HELP */
  7909.         return(doftpxhlp());
  7910.  
  7911.       case FTP_RMD:                     /* RMDIR */
  7912.         return(doftprmd());
  7913.  
  7914.       case FTP_STA:                     /* STATUS */
  7915.         if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7916.           return(x);
  7917.         CHECKCONN();
  7918.         ckstrncpy(line,s,LINBUFSIZ);
  7919.         if (testing) printf(" ftp status \"%s\"...\n",line);
  7920.         success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  7921.         return(success);
  7922.  
  7923.       case FTP_SIT: {                   /* SITE */
  7924.       return(doftpsite());
  7925.       }
  7926.  
  7927.       case FTP_SIZ:                     /* (ask for) SIZE */
  7928.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7929.           return(x);
  7930.         CHECKCONN();
  7931.         ckstrncpy(line,s,LINBUFSIZ);
  7932.         if (testing)
  7933.           printf(" ftp size \"%s\"...\n",line);
  7934.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  7935.     if (success)
  7936.       sizeok = 1;
  7937.         return(success);
  7938.  
  7939.       case FTP_SYS:                     /* Ask for server's SYSTEM type */
  7940.         if ((x = cmcfm()) < 0) return(x);
  7941.         CHECKCONN();
  7942.         if (testing)
  7943.           printf(" ftp system...\n");
  7944.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  7945.         return(success);
  7946.  
  7947.       case FTP_UMA:                     /* Set/query UMASK */
  7948.         if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  7949.           if (x != -3)
  7950.             return(x);
  7951.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7952.         if ((x = cmcfm()) < 0) return(x);
  7953.         CHECKCONN();
  7954.         if (testing) {
  7955.             if (tmpbuf[0])
  7956.               printf(" ftp umask \"%s\"...\n",tmpbuf);
  7957.             else
  7958.               printf(" ftp query umask...\n");
  7959.         }
  7960.         success = ftp_umask(tmpbuf);
  7961.         return(success);
  7962.  
  7963.       case FTP_USR:
  7964.         return(doftpusr());
  7965.  
  7966.       case FTP_QUO:
  7967.         if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  7968.           return(x);
  7969.         CHECKCONN();
  7970.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  7971.         return(success);
  7972.  
  7973.       case FTP_TYP:                     /* Type */
  7974.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  7975.           return(x);
  7976.         if ((y = cmcfm()) < 0) return(y);
  7977.         CHECKCONN();
  7978.         ftp_typ = x;
  7979.         g_ftp_typ = x;
  7980.         tenex = (ftp_typ == FTT_TEN);
  7981.         changetype(ftp_typ,ftp_vbm);
  7982.         return(1);
  7983.  
  7984.       case FTP_CHK:                     /* Check if remote file(s) exist(s) */
  7985.         if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  7986.           return(x);
  7987.         CHECKCONN();
  7988.         success = remote_files(1,(CHAR *)s,NULL,0) ? 1 : 0;
  7989.         return(success);
  7990.  
  7991.       case FTP_FEA:                     /* RFC2389 */
  7992.         if ((y = cmcfm()) < 0)
  7993.           return(y);
  7994.         CHECKCONN();
  7995.     success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE);
  7996.     if (success) {
  7997.         if (sfttab[0] > 0) {
  7998.         ftp_aut = sfttab[SFT_AUTH];
  7999.         sizeok  = sfttab[SFT_SIZE];
  8000.         mdtmok  = sfttab[SFT_MDTM];
  8001.         mlstok  = sfttab[SFT_MLST];
  8002.         }
  8003.     }
  8004.     return(success);
  8005.  
  8006.       case FTP_OPT:                     /* RFC2389 */
  8007.         /* Perhaps this should be a keyword list... */
  8008.         if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  8009.           return(x);
  8010.         CHECKCONN();
  8011.         ckstrncpy(line,s,LINBUFSIZ);
  8012.         if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  8013.           return(x);
  8014.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8015.         return(success);
  8016.  
  8017.       case FTP_ENA:            /* FTP ENABLE */
  8018.       case FTP_DIS:            /* FTP DISABLE */
  8019.         if ((x = cmkey(ftpenatab,nftpena,"","",xxstring)) < 0)
  8020.           return(x);
  8021.         if ((y = cmcfm()) < 0) return(y);
  8022.     switch (x) {
  8023.       case ENA_AUTH:        /* OK to use autoauthentication */
  8024.         ftp_aut = (cx == FTP_ENA) ? 1 : 0;
  8025.         sfttab[SFT_AUTH] = ftp_aut;
  8026.         break;
  8027.       case ENA_FEAT:        /* OK to send FEAT command */
  8028.         featok = (cx == FTP_ENA) ? 1 : 0;
  8029.         break;
  8030.       case ENA_MLST:        /* OK to use MLST/MLSD */
  8031.         mlstok = (cx == FTP_ENA) ? 1 : 0;
  8032.         sfttab[SFT_MLST] = mlstok;
  8033.         break;
  8034.       case ENA_MDTM:        /* OK to use MDTM */
  8035.         mdtmok = (cx == FTP_ENA) ? 1 : 0;
  8036.         sfttab[SFT_MDTM] = mdtmok;
  8037.         break;
  8038.       case ENA_SIZE:        /* OK to use SIZE */
  8039.         sizeok = (cx == FTP_ENA) ? 1 : 0;
  8040.         sfttab[SFT_SIZE] = sizeok;
  8041.         break;
  8042.     }
  8043.     return(success = 1);
  8044.     }
  8045.     return(-2);
  8046. }
  8047.  
  8048. #ifndef NOSHOW
  8049. static char *
  8050. shopl(x) int x; {
  8051.     switch (x) {
  8052.       case FPL_CLR: return("clear");
  8053.       case FPL_PRV: return("private");
  8054.       case FPL_SAF: return("safe");
  8055.       case 0:  return("(not set)");
  8056.       default: return("(unknown)");
  8057.     }
  8058. }
  8059.  
  8060. int
  8061. shoftp(brief) {
  8062.     char * s = "?";
  8063.     int n, x;
  8064.  
  8065.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  8066.         ftp_typ = g_ftp_typ;
  8067.         /* g_ftp_typ = -1; */
  8068.     }
  8069.     printf("\n");
  8070.     printf("FTP connection:                 %s\n",connected ?
  8071.            ftp_host :
  8072.            "(none)"
  8073.            );
  8074.     n = 2;
  8075.     if (connected) {
  8076.         n++;
  8077.         printf("FTP server type:                %s\n",
  8078.                ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  8079.     }
  8080.     if (loggedin)
  8081.       printf("Logged in as:                   %s\n",
  8082.              strval(ftp_logname,"(unknown)"));
  8083.     else
  8084.       printf("Not logged in\n");
  8085.     n++;
  8086.     if (brief) return(0);
  8087.  
  8088.     printf("\nSET FTP values:\n\n");
  8089.     n += 3;
  8090.  
  8091.     printf(" ftp anonymous-password:        %s\n",
  8092.        ftp_apw ? ftp_apw : "(default)"
  8093.        );
  8094.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  8095.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  8096.     switch (ftp_typ) {
  8097.       case FTT_ASC: s = "text"; break;
  8098.       case FTT_BIN: s = "binary"; break;
  8099.       case FTT_TEN: s = "tenex"; break;
  8100.     }
  8101.     printf(" ftp type:                      %s\n",s);
  8102.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  8103.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  8104.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  8105.     printf(" ftp filenames:                 %s\n",
  8106.            ftp_cnv == CNV_AUTO ? "auto" : (ftp_cnv ? "converted" : "literal")
  8107.            );
  8108.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  8109.  
  8110.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  8111.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  8112.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  8113.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  8114.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  8115. #ifdef COMMENT
  8116.     /* See note in doxftp() */
  8117.     if (ftp_fnc < 0)
  8118.       ftp_fnc = fncact;
  8119. #endif /* COMMENT */
  8120.     printf(" ftp collision:                 %s\n",
  8121.        fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  8122.     printf(" ftp server-time-offset:        %s\n",
  8123.        fts_sto ? fts_sto : "(none)");
  8124.     n += 15;
  8125.  
  8126. #ifndef NOCSETS
  8127.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  8128.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8129.  
  8130.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  8131.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8132.  
  8133.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  8134.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8135. #endif /* NOCSETS */
  8136.  
  8137.     x = ftp_dis;
  8138.     if (x < 0)
  8139.       x = fdispla;
  8140.     switch (x) {
  8141.       case XYFD_N: s = "none"; break;
  8142.       case XYFD_R: s = "serial"; break;
  8143.       case XYFD_C: s = "fullscreen"; break;
  8144.       case XYFD_S: s = "crt"; break;
  8145.       case XYFD_B: s = "brief"; break;
  8146.     }
  8147.     printf(" ftp display:                   %s\n",s);
  8148.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8149.  
  8150.     if (mlstok || featok || mdtmok || sizeok || ftp_aut) {
  8151.     printf(" enabled:                      ");
  8152.     if (ftp_aut) printf(" AUTH");
  8153.     if (featok)  printf(" FEAT");
  8154.     if (mdtmok)  printf(" MDTM");
  8155.     if (mlstok)  printf(" MLST");
  8156.     if (sizeok)  printf(" SIZE");
  8157.     printf("\n");
  8158.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8159.     }
  8160.     if (!mlstok || !featok || !mdtmok || !sizeok || !ftp_aut) {
  8161.     printf(" disabled:                     ");
  8162.     if (!ftp_aut) printf(" AUTH");
  8163.     if (!featok)  printf(" FEAT");
  8164.     if (!mdtmok)  printf(" MDTM");
  8165.     if (!mlstok)  printf(" MLST");
  8166.     if (!sizeok)  printf(" SIZE");
  8167.     printf("\n");
  8168.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8169.     }
  8170.     switch (ftpget) {
  8171.       case 0: s = "kermit"; break;
  8172.       case 1: s = "ftp"; break;
  8173.       case 2: s = "auto"; break;
  8174.       default: s = "?";
  8175.     }
  8176.     printf(" get-put-remote:                %s\n",s);
  8177.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8178.  
  8179.     printf("\n");
  8180.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8181.  
  8182. #ifdef FTP_SECURITY
  8183.     printf("Available security methods:    ");
  8184. #ifdef FTP_GSSAPI
  8185.     printf("GSSAPI ");
  8186. #endif /* FTP_GSSAPI */
  8187. #ifdef FTP_KRB4
  8188.     printf("Kerberos4 ");
  8189. #endif /* FTP_KRB4 */
  8190. #ifdef FTP_SRP
  8191.     printf("SRP ");
  8192. #endif /* FTP_SRP */
  8193. #ifdef FTP_SSL
  8194.     printf("SSL ");
  8195. #endif /* FTP_SSL */
  8196.  
  8197.     n++;
  8198.     printf("\n\n");
  8199.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8200.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  8201.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8202.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  8203.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8204.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  8205.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8206.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  8207.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8208.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  8209.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8210.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  8211.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8212. #else
  8213.     printf("Available security methods:     (none)\n");
  8214.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8215. #endif /* FTP_SECURITY */
  8216.  
  8217.     if (n <= cmd_rows - 3)
  8218.       printf("\n");
  8219.     return(0);
  8220. }
  8221. #endif /* NOSHOW */
  8222.  
  8223. #ifndef NOHELP
  8224. /* FTP HELP text strings */
  8225.  
  8226. static char * fhs_ftp[] = {
  8227.     "Syntax: FTP subcommand [ operands ]",
  8228.     "  Makes an FTP connection, or sends a command to the FTP server.",
  8229.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  8230.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  8231.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  8232.     ""
  8233. };
  8234.  
  8235. static char * fhs_acc[] = {             /* ACCOUNT */
  8236.     "Syntax: FTP ACCOUNT text",
  8237.     "  Sends an account designator to an FTP server that needs one.",
  8238.     "  Most FTP servers do not use accounts; some use them for other",
  8239.     "  other purposes, such as disk-access passwords.",
  8240.     ""
  8241. };
  8242. static char * fhs_app[] = {             /* APPEND */
  8243.     "Syntax: FTP APPEND filname",
  8244.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  8245.     ""
  8246. };
  8247. static char * fhs_cls[] = {             /* BYE, CLOSE */
  8248.     "Syntax: [ FTP ] BYE",
  8249.     "  Logs out from the FTP server and closes the FTP connection.",
  8250.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  8251.     ""
  8252. };
  8253. static char * fhs_cwd[] = {             /* CD, CWD */
  8254.     "Syntax: [ FTP ] CD directory",
  8255.     "  Asks the FTP server to change to the given directory.",
  8256.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  8257.     ""
  8258. };
  8259. static char * fhs_gup[] = {             /* CDUP, UP */
  8260.     "Syntax: FTP CDUP",
  8261.     "  Asks the FTP server to change to the parent directory of its current",
  8262.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  8263.     ""
  8264. };
  8265. static char * fhs_chm[] = {             /* CHMOD */
  8266.     "Syntax: FTP CHMOD filename permissions",
  8267.     "  Asks the FTP server to change the permissions, protection, or mode of",
  8268.     "  the given file.  The given permissions must be in the syntax of the",
  8269.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  8270.     "  FTP PUT /PERMISSIONS",
  8271.     ""
  8272. };
  8273. static char * fhs_mde[] = {             /* DELETE */
  8274.     "Syntax: FTP DELETE [ switches ] filespec",
  8275.     "  Asks the FTP server to delete the given file or files.",
  8276.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  8277.     "  multiple file deletion).  Optional switches:",
  8278.     " ",
  8279.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8280.     "  /EXCEPT:pattern",
  8281.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  8282.     "  /LARGER-THAN:number",
  8283. #ifdef UNIXOROSK
  8284.     "  /NODOTFILES",
  8285. #endif /* UNIXOROSK */
  8286.     "  /QUIET",
  8287. #ifdef RECURSIVE
  8288.     "  /RECURSIVE (depends on server)",
  8289.     "  /SUBDIRECTORIES",
  8290. #endif /* RECURSIVE */
  8291.     "  /SMALLER-THAN:number",
  8292.     ""
  8293. };
  8294. static char * fhs_dir[] = {             /* DIRECTORY */
  8295.     "Syntax: FTP DIRECTORY [ filespec ]",
  8296.     "  Asks the server to send a directory listing of the files that match",
  8297.     "  the given filespec, or if none is given, all the files in its current",
  8298.     "  directory.  The filespec, including any wildcards, must be in the",
  8299.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  8300.     "  Synonym: RDIRECTORY.",
  8301.     ""
  8302. };
  8303. static char * fhs_vdi[] = {             /* VDIRECTORY */
  8304.     "Syntax: FTP VDIRECTORY [ filespec ]",
  8305.     "  Asks the server to send a directory listing of the files that match",
  8306.     "  the given filespec, or if none is given, all the files in its current",
  8307.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  8308.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  8309.     "  FTP DIRECTORY lists only filenames without details.",
  8310.     ""
  8311. };
  8312. static char * fhs_fea[] = {             /* FEATURES */
  8313.     "Syntax: FTP FEATURES",
  8314.     "  Asks the FTP server to list its special features.  Most FTP servers",
  8315.     "  do not recognize this command.",
  8316.     ""
  8317. };
  8318. static char * fhs_mge[] = {             /* MGET */
  8319.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  8320.     "  Download a single file or multiple files.  Asks the FTP server to send",
  8321.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  8322.     " ",
  8323.     "  /AS-NAME:text",
  8324.     "    Name under which to store incoming file.",
  8325.     "    Pattern required for for multiple files.",
  8326.     "  /BINARY",                        /* /IMAGE */
  8327.     "    Force binary mode.  Synonym: /IMAGE.",
  8328.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  8329.    "    What to do if an incoming file has the same name as an existing file.",
  8330.  
  8331. #ifdef PUTPIPE
  8332.     "  /COMMAND",
  8333.     "    Specifies that the as-name is a command to which the incoming file",
  8334.     "    is to be piped as standard input.",
  8335. #endif /* PUTPIPE */
  8336.  
  8337. #ifdef DOUPDATE
  8338.     "  /DATES-DIFFER",
  8339.     "    Download only those files whose modification date-times differ from",
  8340.     "    those of the corresponding local files, or that do not already",
  8341.     "    exist on the local computer.",
  8342. #endif /* DOUPDATE */
  8343.  
  8344.     "  /DELETE",
  8345.     "    Specifies that each file is to be deleted from the server after,",
  8346.     "    and only if, it is successfully downloaded.",
  8347.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8348.     "    When downloading a group of files, what to do upon failure to",
  8349.     "    transfer a file: quit or proceed to the next one.",
  8350.     "  /EXCEPT:pattern",
  8351.     "    Exception list: don't download any files that match this pattern.",
  8352.     "    See HELP WILDCARD for pattern syntax.",
  8353.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8354.     "    Whether to convert incoming filenames to local syntax.",
  8355. #ifdef PIPESEND
  8356. #ifndef NOSPL
  8357.     "  /FILTER:command",
  8358.     "    Pass incoming files through the given command.",
  8359. #endif /* NOSPL */
  8360. #endif /* PIPESEND */
  8361.     "  /LARGER-THAN:number",
  8362.     "    Only download files that are larger than the given number of bytes.",
  8363.     "  /LISTFILE:filename",
  8364.     "    Obtain the list of files to download from the given file.",
  8365. #ifndef NOCSETS
  8366.     "  /LOCAL-CHARACTER-SET:name",
  8367.     "    When downloading in text mode and character-set conversion is",
  8368.     "    desired, this specifies the target set.",
  8369. #endif /* NOCSETS */
  8370.     "  /MATCH:pattern",
  8371.     "    Specifies a pattern to be used to select filenames locally from the",
  8372.     "    server's list.",
  8373.     "  /MLSD",
  8374.     "    Forces sending of MLSD (rather than NLST) to get the file list.",
  8375. #ifdef CK_TMPDIR
  8376.     "  /MOVE-TO:directory",
  8377.     "    Each file that is downloaded is to be moved to the given local",
  8378.     "    directory immediately after, and only if, it has been received",
  8379.     "    successfully.",
  8380. #endif /* CK_TMPDIR */
  8381.     "  /NAMELIST:filename",
  8382.     "    Instead of downloading the files, stores the list of files that",
  8383.     "    would be downloaded in the given local file, one filename per line.",
  8384.     "  /NLST",
  8385.     "    Forces sending of NLST (rather than MLSD) to get the file list.",
  8386.     "  /NOBACKUPFILES",
  8387.     "    Don't download any files whose names end with .~<number>~.",
  8388.     "  /NODOTFILES",
  8389.     "    Don't download any files whose names begin with period (.).",
  8390.     "  /QUIET",
  8391.     "    Suppress the file-transfer display.",
  8392. #ifdef FTP_RESTART
  8393.     "  /RECOVER",                       /* /RESTART */
  8394.     "    Resume a download that was previously interrupted from the point of",
  8395.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  8396.     "    Synonym: /RESTART.",
  8397. #endif /* FTP_RESTART */
  8398. #ifdef RECURSIVE
  8399.     "  /RECURSIVE",                     /* /SUBDIRECTORIES */
  8400.     "    Create subdirectories automatically if the server sends files",
  8401.     "    recursively and includes pathnames (most don't).",
  8402. #endif /* RECURSIVE */
  8403.     "  /RENAME-TO:text",
  8404.     "    Each file that is downloaded is to be renamed as indicated just,",
  8405.     "    after, and only if, it has arrived successfully.",
  8406. #ifndef NOCSETS
  8407.     "  /SERVER-CHARACTER-SET:name",
  8408.     "    When downloading in text mode and character-set conversion is desired"
  8409. ,   "    this specifies the original file's character set on the server.",
  8410. #endif /* NOCSETS */
  8411.     "  /SERVER-RENAME:text",
  8412.     "    Each server source file is to be renamed on the server as indicated",
  8413.     "    immediately after, but only if, it has arrived succesfully.",
  8414.     "  /SMALLER-THAN:number",
  8415.     "    Download only those files smaller than the given number of bytes.",
  8416.     "  /TEXT",                          /* /ASCII */
  8417.     "    Force text mode.  Synonym: /ASCII.",
  8418.     "  /TENEX",
  8419.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8420. #ifndef NOCSETS
  8421.     "  /TRANSPARENT",
  8422.     "    When downloading in text mode, do not convert chracter-sets.",
  8423. #endif /* NOCSETS */
  8424.     "  /TO-SCREEN",
  8425.     "    The downloaded file is to be displayed on the screen.",
  8426. #ifdef DOUPDATE
  8427.     "  /UPDATE",
  8428.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  8429.     "    newer than than their local counterparts, or that do not exist on",
  8430.     "    the local computer.",
  8431. #endif /* DOUPDATE */
  8432.     ""
  8433. };
  8434. static char * fhs_hlp[] = {             /* HELP */
  8435.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  8436.     "  Asks the FTP server for help about the given command.  First use",
  8437.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  8438.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  8439.     ""
  8440. };
  8441. static char * fhs_idl[] = {             /* IDLE */
  8442.     "Syntax: FTP IDLE [ number ]",
  8443.     "  If given without a number, this asks the FTP server to tell its",
  8444.     "  current idle-time limit.  If given with a number, it asks the server",
  8445.     "  to change its idle-time limit to the given number of seconds.",
  8446.     ""
  8447. };
  8448. static char * fhs_usr[] = {             /* USER, LOGIN */
  8449.     "Syntax: FTP USER username [ password [ account ] ]",
  8450.     "  Log in to the FTP server.  To be used when connected but not yet",
  8451.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  8452.     "  If you omit the password, and one is required by the server, you are",
  8453.     "  prompted for it.  If you omit the account, no account is sent.",
  8454.     "  Synonym: FTP LOGIN.",
  8455.     ""
  8456. };
  8457. static char * fhs_get[] = {             /* GET */
  8458.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  8459.     "  Download a single file.  Asks the FTP server to send the given file.",
  8460.     "  The optional as-name is the name to store it under when it arrives;",
  8461.     "  if omitted, the file is stored with the name it arrived with, as",
  8462.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  8463.     "  value.  Aside from the file list and as-name, syntax and options are",
  8464.     "  the same as for FTP MGET, which is used for downloading multiple files."
  8465. ,   ""
  8466. };
  8467. static char * fhs_mkd[] = {             /* MKDIR */
  8468.     "Syntax: FTP MKDIR directory",
  8469.     "  Asks the FTP server to create a directory with the given name,",
  8470.     "  which must be in the syntax of the server's file system.  Synonyms:",
  8471.     "  REMOTE MKDIR, RMKDIR.",
  8472.     ""
  8473. };
  8474. static char * fhs_mod[] = {             /* MODTIME */
  8475.     "Syntax: FTP MODTIME filename",
  8476.     "  Asks the FTP server to send the modification time of the given file,",
  8477.     "  to be displayed on the screen.  The date-time format is all numeric:",
  8478.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  8479.     "  fractions of seconds).",
  8480.     ""
  8481. };
  8482. static char * fhs_mpu[] = {             /* MPUT */
  8483.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  8484.     "  Uploads files.  Sends the given file or files to the FTP server.",
  8485.     "  Also see FTP PUT.  Optional switches are:",
  8486.     " ",
  8487.     "  /AFTER:date-time",
  8488.     "    Uploads only those files newer than the given date-time.",
  8489.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  8490. #ifdef PUTARRAY
  8491.     "  /ARRAY:array-designator",
  8492.     "    Tells Kermit to upload the contents of the given array, rather than",
  8493.     "    a file.",
  8494. #endif /* PUTARRAY */
  8495.     "  /AS-NAME:text",
  8496.     "    Name under which to send files.",
  8497.     "    Pattern required for for multiple files.",
  8498.     "  /BEFORE:date-time",
  8499.     "    Upload only those files older than the given date-time.",
  8500.     "  /BINARY",
  8501.     "    Force binary mode.  Synonym: /IMAGE.",
  8502. #ifdef PUTPIPE
  8503.     "  /COMMAND",
  8504.     "    Specifies that the filespec is a command whose standard output is",
  8505.     "    to be sent.",
  8506. #endif /* PUTPIPE */
  8507.  
  8508. #ifdef COMMENT
  8509. #ifdef DOUPDATE
  8510.     "  /DATES-DIFFER",
  8511.     "    Upload only those files whose modification date-times differ from",
  8512.     "    those on the server, or that don't exist on the server at all.",
  8513. #endif /* DOUPDATE */
  8514. #endif /* COMMENT */
  8515.  
  8516.     "  /DELETE",
  8517.     "    Specifies that each source file is to be deleted after, and only if,",
  8518.     "    it is successfully uploaded.",
  8519.     "  /DOTFILES",
  8520.     "    Include files whose names begin with period (.).",
  8521.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8522.     "    When uploading a group of files, what to do upon failure to",
  8523.     "    transfer a file: quit or proceed to the next one.",
  8524.     "  /EXCEPT:pattern",
  8525.     "    Exception list: don't upload any files that match this pattern.",
  8526.     "    See HELP WILDCARD for pattern syntax.",
  8527.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8528.     "    Whether to convert outbound filenames to common syntax.",
  8529. #ifdef PIPESEND
  8530. #ifndef NOSPL
  8531.     "  /FILTER:command",
  8532.     "    Pass outbound files through the given command.",
  8533. #endif /* NOSPL */
  8534. #endif /* PIPESEND */
  8535. #ifdef CKSYMLINK
  8536.     "  /FOLLOWINKS",
  8537.     "    Send files that are pointed to by symbolic links.",
  8538.     "  /NOFOLLOWINKS",
  8539.     "    Skip over symbolic links (default).",
  8540. #endif /* CKSYMLINK */
  8541.     "  /LARGER-THAN:number",
  8542.     "    Only upload files that are larger than the given number of bytes.",
  8543.     "  /LISTFILE:filename",
  8544.     "    Obtain the list of files to upload from the given file.",
  8545. #ifndef NOCSETS
  8546.     "  /LOCAL-CHARACTER-SET:name",
  8547.     "    When uploading in text mode and character-set conversion is",
  8548.     "    desired, this specifies the source-file character set.",
  8549. #endif /* NOCSETS */
  8550. #ifdef CK_TMPDIR
  8551.     "  /MOVE-TO:directory",
  8552.     "    Each source file that is uploaded is to be moved to the given local",
  8553.     "    directory when, and only if, the transfer is successful.",
  8554. #endif /* CK_TMPDIR */
  8555.     "  /NOBACKUPFILES",
  8556.     "    Don't upload any files whose names end with .~<number>~.",
  8557. #ifdef UNIXOROSK
  8558.     "  /NODOTFILES",
  8559.     "    Don't upload any files whose names begin with period (.).",
  8560. #endif /* UNIXOROSK */
  8561.     "  /NOT-AFTER:date-time",
  8562.     "    Upload only files that are not newer than the given date-time",
  8563.     "  /NOT-BEFORE:date-time",
  8564.     "    Upload only files that are not older than the given date-time",
  8565. #ifdef UNIX
  8566.     "  /PERMISSIONS",
  8567.     "    Ask the server to set the permissions of each file it receives",
  8568.     "    according to the source file's permissions.",
  8569. #endif /* UNIX */
  8570.     "  /QUIET",
  8571.     "    Suppress the file-transfer display.",
  8572. #ifdef FTP_RESTART
  8573.     "  /RECOVER",
  8574.     "    Resume an upload that was previously interrupted from the point of",
  8575.     "    failure.  Synonym: /RESTART.",
  8576. #endif /* FTP_RESTART */
  8577. #ifdef RECURSIVE
  8578.     "  /RECURSIVE",
  8579.     "    Send files from the given directory and all the directories beneath",
  8580.     "    it.  Synonym: /SUBDIRECTORIES.",
  8581. #endif /* RECURSIVE */
  8582.     "  /RENAME-TO:text",
  8583.     "    Each source file that is uploaded is to be renamed on the local",
  8584.     "    local computer as indicated when and only if, the transfer completes",
  8585.     "    successfully.",
  8586. #ifndef NOCSETS
  8587.     "  /SERVER-CHARACTER-SET:name",
  8588.     "    When uploading in text mode and character-set conversion is desired,",
  8589.     "    this specifies the character set to which the file should be",
  8590.     "    converted for storage on the server.",
  8591. #endif /* NOCSETS */
  8592.     "  /SERVER-RENAME:text",
  8593.     "    Each file that is uploaded is to be renamed as indicated on the",
  8594.     "    server after, and only if, if arrives successfully.",
  8595.     "  /SIMULATE",
  8596.     "    Show which files would be sent without actually sending them.",
  8597.     "  /SMALLER-THAN:number",
  8598.     "    Upload only those files smaller than the given number of bytes.",
  8599.     "  /TEXT",
  8600.     "    Force text mode.  Synonym: /ASCII.",
  8601.     "  /TENEX",
  8602.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8603. #ifndef NOCSETS
  8604.     "  /TRANSPARENT",
  8605.     "    When uploading in text mode, do not convert chracter-sets.",
  8606. #endif /* NOCSETS */
  8607.     "  /TYPE:{TEXT,BINARY}",
  8608.     "    Upload only files of the given type.",
  8609. #ifdef DOUPDATE
  8610.     "  /UPDATE",
  8611.     "    If a file of the same name exists on the server, upload only if",
  8612.     "    the local file is newer.",
  8613. #endif /* DOUPDATE */
  8614.     "  /UNIQUE-SERVER-NAMES",
  8615.     "    Ask the server to compute new names for any incoming file that has",
  8616.     "    the same name as an existing file.",
  8617.     ""
  8618. };
  8619. static char * fhs_opn[] = {             /* OPEN */
  8620. #ifdef CK_SSL
  8621.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  8622.     "  Opens a connection to the FTP server on the given host.  The default",
  8623.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  8624.     "  can be supplied if necessary.  Optional switches are:",
  8625. #else /* CK_SSL */
  8626.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  8627.     "  Opens a connection to the FTP server on the given host.  The default",
  8628.     "  TCP port is 21, but a different port number can be supplied if",
  8629.     "  necessary.  Optional switches are:",
  8630. #endif /* CK_SSL */
  8631.     " ",
  8632.     "  /ANONYMOUS",
  8633.     "    Logs you in anonymously.",
  8634.     "  /USER:text",
  8635.     "    Supplies the given text as your username.",
  8636.     "  /PASSWORD:text",
  8637.     "    Supplies the given text as your password.  If you include a username",
  8638.     "    but omit this switch and the server requires a password, you are",
  8639.     "    prompted for it.",
  8640.     "  /ACCOUNT:text",
  8641.     "    Supplies the given text as your account, if required by the server.",
  8642.     "  /ACTIVE",
  8643.     "    Forces an active (rather than passive) connection.",
  8644.     "  /PASSIVE",
  8645.     "    Forces a passive (rather than active) connection.",
  8646.     "  /NOINIT",
  8647.     "    Inhibits sending initial REST, STRU, and MODE commands, which are",
  8648.     "    well-known standard commands, but to which some servers react badly.",
  8649.     ""
  8650. };
  8651. static char * fhs_opt[] = {             /* OPTS, OPTIONS */
  8652.     "Syntax: FTP OPTIONS",
  8653.     "  Asks the FTP server to list its current options.  Advanced, new,",
  8654.     "  not supported by most FTP servers.",
  8655.     ""
  8656. };
  8657. static char * fhs_put[] = {             /* PUT, SEND */
  8658.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  8659.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  8660.     "  by an additional field, this is interpreted as the name under which",
  8661.     "  to send the file or files.  See HELP FTP MPUT.",
  8662.     ""
  8663. };
  8664. static char * fhs_pwd[] = {             /* PWD */
  8665.     "Syntax: FTP PWD",
  8666.     "  Asks the FTP server to reveal its current working directory.",
  8667.     "  Synonyms: REMOTE PWD, RPWD.",
  8668.     ""
  8669. };
  8670. static char * fhs_quo[] = {             /* QUOTE */
  8671.     "Syntax: FTP QUOTE text",
  8672.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  8673.     "  for sending commands that Kermit might not support.",
  8674.     ""
  8675. };
  8676. static char * fhs_rge[] = {             /* REGET */
  8677.     "Syntax: FTP REGET",
  8678.     "  Synonym for FTP GET /RECOVER.",
  8679.     ""
  8680. };
  8681. static char * fhs_ren[] = {             /* RENAME */
  8682.     "Syntax: FTP RENAME name1 name1",
  8683.     "  Asks the FTP server to change the name of the file whose name is name1",
  8684.     "  and which resides in the FTP server's file system, to name2.  Works",
  8685.     "  only for single files; wildcards are not accepted.",
  8686.     ""
  8687. };
  8688. static char * fhs_res[] = {             /* RESET */
  8689.     "Syntax: FTP RESET",
  8690.     "  Asks the server to log out your session, terminating your access",
  8691.     "  rights, without closing the connection.",
  8692.     ""
  8693. };
  8694. static char * fhs_rmd[] = {             /* RMDIR */
  8695.     "Syntax: FTP RMDIR directory",
  8696.     "  Asks the FTP server to remove the directory whose name is given.",
  8697.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  8698.     "  RMDIR, RRMDIR.",
  8699.     ""
  8700. };
  8701. static char * fhs_sit[] = {             /* SITE */
  8702.     "Syntax: FTP SITE text",
  8703.     "  Sends a site-specific command to the FTP server.",
  8704.     ""
  8705. };
  8706. static char * fhs_siz[] = {             /* SIZE */
  8707.     "Syntax: FTP SIZE filename",
  8708.     "  Asks the FTP server to send a numeric string representing the size",
  8709.     "  of the given file.",
  8710.     ""
  8711. };
  8712. static char * fhs_sta[] = {             /* STATUS */
  8713.     "Syntax: FTP STATUS [ filename ]",
  8714.     "  Asks the FTP server to report its status.  If a filename is given,",
  8715.     "  the FTP server should report details about the file.",
  8716.     ""
  8717. };
  8718. static char * fhs_sys[] = {             /* SYSTEM */
  8719.     "Syntax: FTP SYSTEM",
  8720.     "  Asks the FTP server to report its operating system type.",
  8721.     ""
  8722. };
  8723. static char * fhs_typ[] = {             /* TYPE */
  8724.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  8725.     "  Puts the client and server in the indicated transfer mode.",
  8726.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  8727.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  8728.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  8729.     "  TENEX mode.",
  8730.     ""
  8731. };
  8732. static char * fhs_uma[] = {             /* UMASK */
  8733.     "Syntax: FTP UMASK number",
  8734.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  8735.     "  only (or mainly) to UNIX-based FTP servers.",
  8736.     ""
  8737. };
  8738. static char * fhs_chk[] = {             /* CHECK */
  8739.     "Syntax: FTP CHECK remote-filespec",
  8740.     "  Asks the FTP server if the given file or files exist.  If the",
  8741.     "  remote-filespec contains wildcards, this command fails if no server",
  8742.     "  files match, and succeeds if at least one file matches.  If the",
  8743.     "  remote-filespec does not contain wildcards, this command succeeds if",
  8744.     "  the given file exists and fails if it does not.",
  8745.     ""
  8746. };
  8747. static char * fhs_ena[] = {        /* ENABLE */
  8748.     "Syntax: FTP ENABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  8749.     "  Enables the use of the given FTP protocol command in case it has been",
  8750.     "  disabled (but this is no guarantee that the FTP server understands it)."
  8751. ,
  8752.     "  Use SHOW FTP to see which of these commands is enabled and disabled.",
  8753.     "  Also see FTP DISABLE.",
  8754.     ""
  8755. };
  8756. static char * fhs_dis[] = {        /* DISABLE */
  8757.     "Syntax: FTP DISABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  8758.     "  Disables the use of the given FTP protocol command.",
  8759.     "  Also see FTP ENABLE.",
  8760.     ""
  8761. };
  8762.  
  8763. #endif /* NOHELP */
  8764.  
  8765. int
  8766. doftphlp() {
  8767.     int cx;
  8768.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  8769.       if (cx != -3)
  8770.         return(cx);
  8771.     if ((x = cmcfm()) < 0)
  8772.       return(x);
  8773.  
  8774. #ifdef NOHELP
  8775.     printf("Sorry, no help available\n");
  8776. #else
  8777.     switch (cx) {
  8778.       case -3:
  8779.         return(hmsga(fhs_ftp));
  8780.       case FTP_ACC:                     /* ACCOUNT */
  8781.         return(hmsga(fhs_acc));
  8782.       case FTP_APP:                     /* APPEND */
  8783.         return(hmsga(fhs_app));
  8784.       case FTP_CLS:                     /* BYE, CLOSE */
  8785.         return(hmsga(fhs_cls));
  8786.       case FTP_CWD:                     /* CD, CWD */
  8787.         return(hmsga(fhs_cwd));
  8788.       case FTP_GUP:                     /* CDUP, UP */
  8789.         return(hmsga(fhs_gup));
  8790.       case FTP_CHM:                     /* CHMOD */
  8791.         return(hmsga(fhs_chm));
  8792.       case FTP_MDE:                     /* DELETE, MDELETE */
  8793.         return(hmsga(fhs_mde));
  8794.       case FTP_DIR:                     /* DIRECTORY */
  8795.         return(hmsga(fhs_dir));
  8796.       case FTP_VDI:                     /* VDIRECTORY */
  8797.         return(hmsga(fhs_vdi));
  8798.       case FTP_FEA:                     /* FEATURES */
  8799.         return(hmsga(fhs_fea));
  8800.       case FTP_GET:                     /* GET */
  8801.         return(hmsga(fhs_get));
  8802.       case FTP_HLP:                     /* HELP */
  8803.         return(hmsga(fhs_hlp));
  8804.       case FTP_IDL:                     /* IDLE */
  8805.         return(hmsga(fhs_idl));
  8806.       case FTP_USR:                     /* USER, LOGIN */
  8807.         return(hmsga(fhs_usr));
  8808.       case FTP_MGE:                     /* MGET */
  8809.         return(hmsga(fhs_mge));
  8810.       case FTP_MKD:                     /* MKDIR */
  8811.         return(hmsga(fhs_mkd));
  8812.       case FTP_MOD:                     /* MODTIME */
  8813.         return(hmsga(fhs_mod));
  8814.       case FTP_MPU:                     /* MPUT */
  8815.         return(hmsga(fhs_mpu));
  8816.       case FTP_OPN:                     /* OPEN */
  8817.         return(hmsga(fhs_opn));
  8818.       case FTP_OPT:                     /* OPTS, OPTIONS */
  8819.         return(hmsga(fhs_opt));
  8820.       case FTP_PUT:                     /* PUT, SEND */
  8821.         return(hmsga(fhs_put));
  8822.       case FTP_PWD:                     /* PWD */
  8823.         return(hmsga(fhs_pwd));
  8824.       case FTP_QUO:                     /* QUOTE */
  8825.         return(hmsga(fhs_quo));
  8826.       case FTP_RGE:                     /* REGET */
  8827.         return(hmsga(fhs_rge));
  8828.       case FTP_REN:                     /* RENAME */
  8829.         return(hmsga(fhs_ren));
  8830.       case FTP_RES:                     /* RESET */
  8831.         return(hmsga(fhs_res));
  8832.       case FTP_RMD:                     /* RMDIR */
  8833.         return(hmsga(fhs_rmd));
  8834.       case FTP_SIT:                     /* SITE */
  8835.         return(hmsga(fhs_sit));
  8836.       case FTP_SIZ:                     /* SIZE */
  8837.         return(hmsga(fhs_siz));
  8838.       case FTP_STA:                     /* STATUS */
  8839.         return(hmsga(fhs_sta));
  8840.       case FTP_SYS:                     /* SYSTEM */
  8841.         return(hmsga(fhs_sys));
  8842.       case FTP_TYP:                     /* TYPE */
  8843.         return(hmsga(fhs_typ));
  8844.       case FTP_UMA:                     /* UMASK */
  8845.         return(hmsga(fhs_uma));
  8846.       case FTP_CHK:                     /* CHECK */
  8847.         return(hmsga(fhs_chk));
  8848.       case FTP_ENA:
  8849.         return(hmsga(fhs_ena));
  8850.       case FTP_DIS:
  8851.         return(hmsga(fhs_dis));
  8852.       default:
  8853.         printf("Sorry, help available for this command.\n");
  8854.         break;
  8855.     }
  8856. #endif /* NOHELP */
  8857.     return(0);
  8858. }
  8859.  
  8860. int
  8861. dosetftphlp() {
  8862.     int cx;
  8863.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  8864.       if (cx != -3)
  8865.         return(cx);
  8866.     if (cx != -3)
  8867.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  8868.     if ((x = cmcfm()) < 0)
  8869.       return(x);
  8870.  
  8871. #ifdef NOHELP
  8872.     printf("Sorry, no help available\n");
  8873. #else
  8874.     switch (cx) {
  8875.       case -3:
  8876.         printf("\nSyntax: SET FTP parameter value\n");
  8877.         printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  8878.         printf("  Type \"help set ftp xxx\" for information about setting\n");
  8879.         printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  8880.         return(0);
  8881.  
  8882. #ifdef FTP_SECURITY
  8883.       case FTS_ATP:                     /* "authtype" */
  8884.         printf("\nSyntax: SET FTP AUTHTYPE list\n");
  8885.         printf("  Specifies an ordered list of authentication methods to be\n"
  8886.                );
  8887.         printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  8888.         printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  8889.         return(0);
  8890.  
  8891.       case FTS_AUT:                     /* "autoauthentication" */
  8892.         printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  8893.         printf("  Tells whether authentication should be negotiated by the\n");
  8894.         printf("  FTP OPEN command.  Default is ON.\n\n");
  8895.         break;
  8896.  
  8897.       case FTS_CRY:                     /* "autoencryption" */
  8898.         printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  8899.         printf("  Tells whether encryption (privacy) should be negotiated\n");
  8900.         printf("  by the FTP OPEN command.  Default is ON.\n\n");
  8901.         break;
  8902. #endif /* FTP_SECURITY */
  8903.  
  8904.       case FTS_LOG:                     /* "autologin" */
  8905.         printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  8906.         printf("  Tells Kermit whether to try to log you in automatically\n");
  8907.         printf("  as part of the connection process.\n\n");
  8908.         break;
  8909.  
  8910.       case FTS_DIS:
  8911.         printf("\nSET FTP DISPLAY { BRIEF, FULLSCREEN, CRT, ... }\n");
  8912.     printf("  Chooses the file-transfer display style for FTP.\n");
  8913.         printf("  Like SET TRANSFER DISPLAY but applies only to FTP.\n\n");
  8914.         break;
  8915.  
  8916. #ifndef NOCSETS
  8917.       case FTS_XLA:                     /* "character-set-translation" */
  8918.         printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  8919.         printf("  Whether to translate character sets when transferring\n");
  8920.         printf("  text files with FTP.  OFF by default.\n\n");
  8921.         break;
  8922.  
  8923. #endif /* NOCSETS */
  8924.       case FTS_FNC:                     /* "collision" */
  8925.         printf("\n");
  8926.         printf(
  8927. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  8928.                );
  8929.         printf("  Tells what do when an incoming file has the same name as\n");
  8930.         printf("  an existing file when downloading with FTP.\n\n");
  8931.         break;
  8932.  
  8933. #ifdef FTP_SECURITY
  8934.       case FTS_CPL:                     /* "command-protection-level" */
  8935.         printf("\n");
  8936.         printf(
  8937. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8938.                );
  8939.         printf("\n");
  8940.         printf(
  8941. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  8942.         break;
  8943.       case FTS_CFW:                     /* "credential-forwarding" */
  8944.         printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  8945.         printf("  Tells whether end-user credentials are to be forwarded\n");
  8946.         printf("  to the server if supported by the authentication method\n");
  8947.         printf("  (GSSAPI-KRB5 only).\n\n");
  8948.         break;
  8949.       case FTS_DPL:                     /* "data-protection-level" */
  8950.         printf("\n");
  8951.         printf(
  8952. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8953.                );
  8954.         printf("\n");
  8955.         printf(
  8956. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  8957.         break;
  8958. #endif /* FTP_SECURITY */
  8959.  
  8960.       case FTS_DBG:                     /* "debug" */
  8961.         printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  8962.         printf("  Whether to print FTP protocol messages.\n\n");
  8963.         return(0);
  8964.  
  8965.       case FTS_ERR:                     /* "error-action" */
  8966.         printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  8967.         printf("  What to do when an error occurs when transferring a group\n")
  8968.           ;
  8969.         printf("  of files: quit and fail, or proceed to the next file.\n\n");
  8970.         return(0);
  8971.  
  8972.       case FTS_CNV:                     /* "filenames" */
  8973.         printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  8974.         printf("  What to do with filenames: convert them, take and use them\n"
  8975.                );
  8976.         printf("  literally; or choose what to do automatically based on the\n"
  8977.                );
  8978.         printf("  OS type of the server.  The default is AUTO.\n\n");
  8979.         return(0);
  8980.  
  8981.       case FTS_PSV:                     /* "passive-mode" */
  8982.         printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  8983.         printf("  Whether to use passive mode, which helps to get through\n");
  8984.         printf("  firewalls.  ON by default.\n\n");
  8985.         return(0);
  8986.  
  8987.       case FTS_PRM:                     /* "permissions" */
  8988.         printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  8989.         printf("  Whether to try to send file permissions when uploading.\n");
  8990.         printf("  OFF by default.  AUTO means only if client and server\n");
  8991.         printf("  have the same OS type.\n\n");
  8992.         return(0);
  8993.  
  8994.       case FTS_TST:                     /* "progress-messages" */
  8995.         printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  8996.         printf("  Whether Kermit should print locally-generated feedback\n");
  8997.         printf("  messages for each non-file-transfer command.");
  8998.         printf("  ON by default.\n\n");
  8999.         return(0);
  9000.  
  9001.       case FTS_SPC:                     /* "send-port-commands" */
  9002.         printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  9003.         printf("  Whether Kermit should send a new PORT command for each");
  9004.         printf("  task.\n\n");
  9005.         return(0);
  9006.  
  9007. #ifndef NOCSETS
  9008.       case FTS_CSR:                     /* "server-character-set" */
  9009.         printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  9010.         printf("  The name of the character set used for text files on the\n");
  9011.         printf("  server.  Enter a name of '?' for a menu.\n\n");
  9012.         return(0);
  9013. #endif /* NOCSETS */
  9014.  
  9015.       case FTS_STO:            /* "server-time-offset */
  9016.     printf(
  9017. "\nSyntax: SET FTP SERVER-TIME-OFFSET +hh[:mm[:ss]] or -hh[:mm[:ss]]\n");
  9018.         printf(
  9019. "  Specifies an offset to apply to the server's file timestamps.\n");
  9020.         printf(
  9021. "  Use this to correct for misconfigured server time or timezone.\n");
  9022.         printf(
  9023. "  Format: must begin with + or - sign.  Hours must be given; minutes\n");
  9024.         printf(
  9025. "  and seconds are optional: +4 = +4:00 = +4:00:00 (add 4 hours).\n\n");
  9026.         return(0);
  9027.  
  9028.       case FTS_TYP:                     /* "type" */
  9029.         printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  9030.         printf("  Establishes the default transfer mode.\n");
  9031.         printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  9032.         printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  9033.         printf("  them again.\n\n");
  9034.         return(0);
  9035.  
  9036. #ifdef PATTERNS
  9037.       case FTS_GFT:
  9038.         printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  9039.         printf("  Tells whether GET and MGET should automatically switch\n");
  9040.         printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  9041.         printf("  matching the name of each incoming file with its list of\n");
  9042.         printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  9043.         printf("  default.  SHOW PATTERNS displays the current pattern\n");
  9044.         printf("  list.  HELP SET FILE to see how to change it.\n");
  9045.         break;
  9046. #endif /* PATTERNS */
  9047.  
  9048.       case FTS_USN:                     /* "unique-server-names" */
  9049.         printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  9050.         printf("  Tells whether to ask the server to create unique names\n");
  9051.         printf("  for any uploaded file that has the same name as an\n");
  9052.         printf("  existing file.  Default is OFF.\n\n");
  9053.         return(0);
  9054.  
  9055.       case FTS_VBM:                     /* "verbose-mode" */
  9056.         printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  9057.         printf("  Whether to display all responses from the FTP server.\n");
  9058.         printf("  OFF by default.\n\n");
  9059.         return(0);
  9060.  
  9061.       case FTS_DAT:
  9062.         printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  9063.         printf("  Whether to set date of incoming files from the file date\n");
  9064.         printf("  on the server.  ON by default.  Note: there is no way to\n")
  9065.           ;
  9066.         printf("  set the date on files uploaded to the server.  Also note\n");
  9067.     printf("  that not all servers support this feature.\n\n");
  9068.         return(0);
  9069.  
  9070.       case FTS_APW:
  9071.     printf("\nSyntax: SET FTP ANONYMOUS-PASSWORD [ text ]\n");
  9072.     printf("  Password to supply automatically on anonymous FTP\n");
  9073.     printf("  connections instead of the default user@host.\n");
  9074.     printf("  Omit optional text to restore default.\n\n");
  9075.     return(0);
  9076.  
  9077.       default:
  9078.         printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  9079.     }
  9080. #endif /* NOHELP */
  9081.     return(0);
  9082. }
  9083.  
  9084. #ifndef L_SET
  9085. #define L_SET 0
  9086. #endif /* L_SET */
  9087. #ifndef L_INCR
  9088. #define L_INCR 1
  9089. #endif /* L_INCR */
  9090.  
  9091. #ifdef FTP_SRP
  9092. char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
  9093. char *srp_pass;
  9094. char *srp_acct;
  9095. #endif /* FTP_SRP */
  9096.  
  9097. static int kerror;                      /* Needed for all auth types */
  9098.  
  9099. static struct   sockaddr_in hisctladdr;
  9100. static struct   sockaddr_in hisdataaddr;
  9101. static struct   sockaddr_in data_addr;
  9102. static int      data = -1;
  9103. static int      ptflag = 0;
  9104. static struct   sockaddr_in myctladdr;
  9105.  
  9106. #ifdef COMMENT
  9107. #ifndef OS2
  9108. UID_T getuid();
  9109. #endif /* OS2 */
  9110. #endif /* COMMENT */
  9111.  
  9112.  
  9113. static int cpend = 0;                   /* No pending replies */
  9114.  
  9115. #ifdef CK_SSL
  9116. extern SSL *ssl_ftp_con;
  9117. extern SSL_CTX *ssl_ftp_ctx;
  9118. extern SSL *ssl_ftp_data_con;
  9119. extern int ssl_ftp_active_flag;
  9120. extern int ssl_ftp_data_active_flag;
  9121. #endif /* CK_SSL */
  9122.  
  9123. /*  f t p c m d  --  Send a command to the FTP server  */
  9124. /*
  9125.   Call with:
  9126.     char * cmd: The command to send.
  9127.     char * arg: The argument (e.g. a filename).
  9128.     int lcs: The local character set index.
  9129.     int rcs: The remote (server) character set index.
  9130.     int vbm: Verbose mode:
  9131.       0 = force verbosity off
  9132.      >0 = force verbosity on
  9133.  
  9134.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  9135.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  9136.   character set to the remote one before sending the result to the server.
  9137.  
  9138.    Returns:
  9139.     0 on failure with ftpcode = -1
  9140.     >= 0 on success (getreply() result) with ftpcode = 0.
  9141. */
  9142. static char xcmdbuf[RFNBUFSIZ];
  9143.  
  9144. static int
  9145. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  9146.     char * s = NULL;
  9147.     int r = 0, x = 0, fc = 0, len = 0, cmdlen = 0;
  9148.     sig_t oldintr;
  9149.  
  9150.     if (ftp_deb)                        /* DEBUG */
  9151.       vbm = 1;
  9152.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9153.       vbm = 0;
  9154.     else if (vbm < 0)                   /* VERBOSE */
  9155.       vbm = ftp_vbm;
  9156.  
  9157.     cancelfile = 0;
  9158.     if (!cmd) cmd = "";
  9159.     if (!arg) arg = "";
  9160.     cmdlen = (int)strlen(cmd);
  9161.     len = cmdlen + (int)strlen(arg) + 1;
  9162.  
  9163.     if (ftp_deb /* && !dpyactive */ ) {
  9164. #ifdef FTP_PROXY
  9165.         if (ftp_prx) printf("%s ", ftp_host);
  9166. #endif /* FTP_PROXY */
  9167.         printf("---> ");
  9168.         if (!anonymous && strcmp("PASS",cmd) == 0)
  9169.           printf("PASS XXXX");
  9170.         else
  9171.           printf("%s %s",cmd,arg);
  9172.         printf("\n");
  9173.     }
  9174.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  9175.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  9176.  
  9177. #ifdef DEBUG
  9178.     if (deblog) {
  9179.         debug(F110,"ftpcmd cmd",cmd,0);
  9180.         debug(F110,"ftpcmd arg",arg,0);
  9181.         debug(F101,"ftpcmd lcs","",lcs);
  9182.         debug(F101,"ftpcmd rcs","",rcs);
  9183.     }
  9184. #endif /* DEBUG */
  9185.  
  9186.     if (csocket == -1) {
  9187.         perror("No control connection for command");
  9188.         ftpcode = -1;
  9189.         return(0);
  9190.     }
  9191.     havesigint = 0;
  9192.     oldintr = signal(SIGINT, cmdcancel);
  9193.  
  9194. #ifndef NOCSETS
  9195.     if (*arg &&                         /* If an arg was given */
  9196.         lcs > -1 &&                     /* and a local charset */
  9197.         rcs > -1 &&                     /* and a remote charset */
  9198.         lcs != rcs &&                   /* and the two are not the same */
  9199.         lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
  9200.         rcs != FC_UCS2                  /* ... */
  9201.         ) {
  9202.         initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
  9203.         xgnbp = arg;                    /* Global pointer to input string */
  9204.         rfnptr = rfnbuf;                /* Global pointer to output buffer */
  9205.  
  9206.         while (1) {
  9207.             if ((c0 = xgnbyte(FC_UCS2,lcs,strgetc)) < 0) break;
  9208.             if (xpnbyte(c0,TC_UCS2,rcs,strputc) < 0) break;
  9209.         }
  9210.         /*
  9211.           We have to copy here instead of translating directly into
  9212.           xcmdbuf[] so strputc() can check length.  Alternatively we could
  9213.           write yet another xpnbyte() output function.
  9214.         */
  9215.         if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  9216.             printf("?FTP command too long: %s + arg\n",cmd);
  9217.             ftpcode = -1;
  9218.             return(0);
  9219.         }
  9220.         x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  9221.     }
  9222. #endif /* NOCSETS */
  9223.  
  9224.     s = xcmdbuf;                        /* Command to send to server */
  9225.  
  9226. #ifdef DEBUG
  9227.     if (deblog) {            /* Log it */
  9228.     if (!anonymous && !ckstrcmp(s,"PASS ",5,0)) {
  9229.         /* But don't log passwords */
  9230.         debug(F110,"FTP SENT ","PASS XXXX",0);
  9231.     } else {
  9232.         debug(F110,"FTP SENT ",s,0);
  9233.     }
  9234.     }
  9235. #endif /* DEBUG */
  9236.  
  9237. #ifdef CK_ENCRYPTION
  9238.   again:
  9239. #endif /* CK_ENCRYPTION */
  9240.     if (scommand(s) == 0) {              /* Send it. */
  9241.       signal(SIGINT, oldintr);
  9242.       return(0);
  9243.     }
  9244.     cpend = 1;
  9245.     x = !strcmp(cmd,"QUIT");        /* Is it the QUIT command? */
  9246.     if (x)                /* In case we're interrupted */
  9247.       connected = 0;            /* while waiting for the reply... */
  9248.  
  9249.     fc = 0;                /* Function code for getreply() */
  9250.     if (!strncmp(cmd,"AUTH ",5)        /* Must parse AUTH reply */
  9251. #ifdef FTPHOST
  9252.     && strncmp(cmd, "HOST ",5)
  9253. #endif /* FTPHOST */
  9254.     ) {
  9255.     fc = GRF_AUTH;
  9256.     } else if (!ckstrcmp(cmd,"FEAT",-1,0)) { /* Must parse FEAT reply */
  9257.     fc = GRF_FEAT;
  9258.     }
  9259.     r = getreply(x,            /* Expect connection to close */
  9260.          lcs,rcs,        /* Charsets */
  9261.          vbm,            /* Verbosity */
  9262.          fc            /* Function code */
  9263.          );
  9264. #ifdef CK_ENCRYPTION
  9265.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  9266.         fprintf(stderr,
  9267.                "ENC command not supported at server; retrying under MIC...\n");
  9268.         ftp_cpl = FPL_SAF;
  9269.         goto again;
  9270.     }
  9271. #endif /* CK_ENCRYPTION */
  9272. #ifdef COMMENT
  9273.     if (cancelfile && oldintr != SIG_IGN)
  9274.       (*oldintr)(SIGINT);
  9275. #endif /* COMMENT */
  9276.     signal(SIGINT, oldintr);
  9277.     return(r);
  9278. }
  9279.  
  9280. static VOID
  9281. lostpeer() {
  9282.     debug(F100,"lostpeer","",0);
  9283.     if (connected) {
  9284.         if (csocket != -1) {
  9285. #ifdef CK_SSL
  9286.             if (ssl_ftp_active_flag) {
  9287.                 SSL_shutdown(ssl_ftp_con);
  9288.                 SSL_free(ssl_ftp_con);
  9289.                 ssl_ftp_proxy = 0;
  9290.                 ssl_ftp_active_flag = 0;
  9291.                 ssl_ftp_con = NULL;
  9292.             }
  9293. #endif /* CK_SSL */
  9294. #ifdef TCPIPLIB
  9295.             socket_close(csocket);
  9296. #else /* TCPIPLIB */
  9297. #ifdef USE_SHUTDOWN
  9298.             shutdown(csocket, 1+1);
  9299. #endif /* USE_SHUTDOWN */
  9300.             close(csocket);
  9301. #endif /* TCPIPLIB */
  9302.             csocket = -1;
  9303.         }
  9304.         if (data != -1) {
  9305. #ifdef CK_SSL
  9306.             if (ssl_ftp_data_active_flag) {
  9307.                 SSL_shutdown(ssl_ftp_data_con);
  9308.                 SSL_free(ssl_ftp_data_con);
  9309.                 ssl_ftp_data_active_flag = 0;
  9310.                 ssl_ftp_data_con = NULL;
  9311.             }
  9312. #endif /* CK_SSL */
  9313. #ifdef TCPIPLIB
  9314.             socket_close(data);
  9315. #else /* TCPIPLIB */
  9316. #ifdef USE_SHUTDOWN
  9317.             shutdown(data, 1+1);
  9318. #endif /* USE_SHUTDOWN */
  9319.             close(data);
  9320. #endif /* TCPIPLIB */
  9321.             data = -1;
  9322.             globaldin = -1;
  9323.         }
  9324.         connected = 0;
  9325.         anonymous = 0;
  9326.         loggedin = 0;
  9327.         auth_type = NULL;
  9328.         ftp_cpl = ftp_dpl = FPL_CLR;
  9329. #ifdef CKLOGDIAL
  9330.         ftplogend();
  9331. #endif /* CKLOGDIAL */
  9332.  
  9333. #ifdef LOCUS
  9334.     if (autolocus)            /* Auotomatic locus switching... */
  9335.       setlocus(1,1);        /* Switch locus to local. */
  9336. #endif /* LOCUS */
  9337. #ifdef OS2
  9338.         DialerSend(OPT_KERMIT_HANGUP, 0);
  9339. #endif /* OS2 */
  9340.     }
  9341. #ifdef FTP_PROXY
  9342.     pswitch(1);
  9343.     if (connected) {
  9344.         if (csocket != -1) {
  9345. #ifdef TCPIPLIB
  9346.             socket_close(csocket);
  9347. #else /* TCPIPLIB */
  9348. #ifdef USE_SHUTDOWN
  9349.             shutdown(csocket, 1+1);
  9350. #endif /* USE_SHUTDOWN */
  9351.             close(csocket);
  9352. #endif /* TCPIPLIB */
  9353.             csocket = -1;
  9354.         }
  9355.         connected = 0;
  9356.         anonymous = 0;
  9357.         loggedin = 0;
  9358.         auth_type = NULL;
  9359.         ftp_cpl = ftp_dpl = FPL_CLR;
  9360.     }
  9361.     proxflag = 0;
  9362.     pswitch(0);
  9363. #endif /* FTP_PROXY */
  9364. }
  9365.  
  9366. int
  9367. ftpisopen() {
  9368.     return(connected);
  9369. }
  9370.  
  9371. static int
  9372. ftpclose() {
  9373.     extern int quitting;
  9374.     if (!connected)
  9375.       return(0);
  9376.     if (!ftp_vbm && !quiet) printlines = 1;
  9377.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  9378.     if (csocket) {
  9379. #ifdef CK_SSL
  9380.         if (ssl_ftp_active_flag) {
  9381.             SSL_shutdown(ssl_ftp_con);
  9382.             SSL_free(ssl_ftp_con);
  9383.             ssl_ftp_proxy = 0;
  9384.             ssl_ftp_active_flag = 0;
  9385.             ssl_ftp_con = NULL;
  9386.         }
  9387. #endif /* CK_SSL */
  9388. #ifdef TCPIPLIB
  9389.         socket_close(csocket);
  9390. #else /* TCPIPLIB */
  9391. #ifdef USE_SHUTDOWN
  9392.         shutdown(csocket, 1+1);
  9393. #endif /* USE_SHUTDOWN */
  9394.         close(csocket);
  9395. #endif /* TCPIPLIB */
  9396.     }
  9397.     csocket = -1;
  9398.     connected = 0;
  9399.     anonymous = 0;
  9400.     loggedin = 0;
  9401.     mdtmok = 1;
  9402.     sizeok = 1;
  9403.     featok = 1;
  9404.     stouarg = 1;
  9405.     typesent = 0;
  9406.     data = -1;
  9407.     globaldin = -1;
  9408. #ifdef FTP_PROXY
  9409.     if (!proxy)
  9410.       macnum = 0;
  9411. #endif /* FTP_PROXY */
  9412.     auth_type = NULL;
  9413.     ftp_dpl = FPL_CLR;
  9414. #ifdef CKLOGDIAL
  9415.     ftplogend();
  9416. #endif /* CKLOGDIAL */
  9417. #ifdef LOCUS
  9418.     /* Unprefixed file management commands are executed locally */
  9419.     if (autolocus && !ftp_cmdlin && !quitting) {
  9420.         setlocus(1,1);
  9421.     }
  9422. #endif /* LOCUS */
  9423. #ifdef OS2
  9424.     DialerSend(OPT_KERMIT_HANGUP, 0);
  9425. #endif /* OS2 */
  9426.     return(0);
  9427. }
  9428.  
  9429. int
  9430. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  9431.     char * host;
  9432.  
  9433.     if (connected) {
  9434.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  9435.         ftpcode = -1;
  9436.         return(0);
  9437.     }
  9438. #ifdef FTPHOST
  9439.     hostcmd = 0;
  9440. #endif /* FTPHOST */
  9441.     alike = 0;
  9442.     ftp_srvtyp[0] = NUL;
  9443.     if (!service) service = "";
  9444.     if (!*service) service = use_tls ? "ftps" : "ftp";
  9445.  
  9446.     if (!isdigit(service[0])) {
  9447.         struct servent *destsp;
  9448.         destsp = getservbyname(service, "tcp");
  9449.         if (!destsp) {
  9450.             if (!ckstrcmp(service,"ftp",-1,0)) {
  9451.                 ftp_port = 21;
  9452.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  9453.                 ftp_port = 990;
  9454.             } else {
  9455.                 printf("?Bad port name - \"%s\"\n", service);
  9456.                 ftpcode = -1;
  9457.                 return(0);
  9458.             }
  9459.         } else {
  9460.             ftp_port = destsp->s_port;
  9461.             ftp_port = ntohs(ftp_port);
  9462.         }
  9463.     } else
  9464.         ftp_port = atoi(service);
  9465.     if (ftp_port <= 0) {
  9466.         printf("?Bad port name - \"%s\"\n", service);
  9467.         ftpcode = -1;
  9468.         return(0);
  9469.     }
  9470.     host = ftp_hookup(remote, ftp_port, use_tls);
  9471.     if (host) {
  9472.         ckstrncpy(ftp_user_host,remote,MAX_DNS_NAMELEN);
  9473.         connected = 1;                  /* Set FTP defaults */
  9474.         ftp_cpl = ftp_dpl = FPL_CLR;
  9475.         curtype = FTT_ASC;              /* Server uses ASCII mode */
  9476.         form = FORM_N;
  9477.         mode = MODE_S;
  9478.         stru = STRU_F;
  9479.         strcpy(bytename, "8");
  9480.         bytesize = 8;
  9481.  
  9482. #ifdef FTP_SECURITY
  9483.         if (ftp_aut) {
  9484.             if (ftp_auth()) {
  9485.                 if (ftp_cry 
  9486. #ifdef OS2
  9487.                      && ck_crypt_is_installed()
  9488. #endif /* OS2 */
  9489.                      ) {
  9490.                     if (!quiet)
  9491.                       printf("FTP Command channel is Private (encrypted)\n");
  9492.                     ftp_cpl = FPL_PRV;
  9493.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  9494.                         /* a failure here is most likely caused by a mixup */
  9495.                         /* in the session key used by client and server    */
  9496.             printf("?Protection buffer size negotiation failed\n");
  9497.                         return(0);
  9498.                     }
  9499.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  9500.                         if (!quiet)
  9501.                           printf("FTP Data channel is Private (encrypted)\n");
  9502.                         ftp_dpl = FPL_PRV;
  9503.                     } else
  9504.                       printf("?Unable to enable encryption on data channel\n");
  9505.                 } else {
  9506.                     ftp_cpl = FPL_SAF;
  9507.                 }
  9508.             }
  9509.             if (!connected)
  9510.           goto fail;
  9511.         }
  9512. #endif /* FTP_SECURITY */
  9513.         if (ftp_log)
  9514.           ftp_login(remote);
  9515.  
  9516.         if (!connected)
  9517.       goto fail;
  9518.  
  9519. #ifdef CKLOGDIAL
  9520.         dologftp();
  9521. #endif /* CKLOGDIAL */
  9522. #ifdef OS2
  9523.         DialerSend(OPT_KERMIT_CONNECT, 0);
  9524. #endif /* OS2 */
  9525.         passivemode = ftp_psv;
  9526.         sendport = ftp_spc;
  9527.     mdtmok = 1;
  9528.     sizeok = 1;
  9529.     stouarg = 1;
  9530.     typesent = 0;
  9531.  
  9532.         if (ucbuf == NULL) {
  9533.             actualbuf = DEFAULT_PBSZ;
  9534.             while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  9535.               actualbuf >>= 2;
  9536.         }
  9537.         if (!maxbuf)
  9538.           ucbufsiz = actualbuf - FUDGE_FACTOR;
  9539.         return(1);
  9540.     }
  9541.   fail:
  9542.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  9543.     ftpcode = -1;
  9544.     return(0);
  9545. }
  9546.  
  9547. #ifdef CK_SSL
  9548. int
  9549. ssl_auth() {
  9550.     int i;
  9551.     char* p;
  9552.  
  9553.     if (ssl_debug_flag) {
  9554.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  9555.         fflush(stderr);
  9556.         /* for the moment I want the output on screen */
  9557.     }
  9558.     if (ssl_ftp_data_con != NULL) {
  9559.         SSL_free(ssl_ftp_data_con);
  9560.         ssl_ftp_data_con = NULL;
  9561.     }
  9562.     if (ssl_ftp_con != NULL) {
  9563.         SSL_free(ssl_ftp_con);
  9564.         ssl_ftp_con=NULL;
  9565.     }
  9566.     if (ssl_ftp_ctx != NULL) {
  9567.         SSL_CTX_free(ssl_ftp_ctx);
  9568.         ssl_ftp_ctx = NULL;
  9569.     }
  9570. #ifdef COMMENT
  9571.     if (auth_type && !strcmp(auth_type,"TLS")) {
  9572.         ssl_ftp_ctx=SSL_CTX_new(TLSv1_client_method());
  9573.         if (!ssl_ftp_ctx)
  9574.           return(0);
  9575.         SSL_CTX_set_options(ssl_ftp_ctx,
  9576.                    SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9577.                             );
  9578.     } else {
  9579.         ssl_ftp_ctx = SSL_CTX_new(SSLv3_client_method());
  9580.         if (!ssl_ftp_ctx)
  9581.           return(0);
  9582.         SSL_CTX_set_options(ssl_ftp_ctx,SSL_OP_ALL);
  9583.     }
  9584. #else /* COMMENT */
  9585.     ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  9586.     if (!ssl_ftp_ctx)
  9587.       return(0);
  9588.     /* The SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 
  9589.      * was added to OpenSSL 0.9.6e and 0.9.7.  It does not exist in previous
  9590.      * versions
  9591.      */
  9592. #ifndef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  9593. #define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0L
  9594. #endif
  9595.  
  9596.     SSL_CTX_set_options(ssl_ftp_ctx,
  9597.                     SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9598.                         );
  9599. #endif /* COMMENT */
  9600.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  9601.                                   (pem_password_cb *)ssl_passwd_callback);
  9602.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  9603.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  9604.  
  9605. #ifdef OS2
  9606. #ifdef NT
  9607.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9608.     {
  9609.         char path[CKMAXPATH];
  9610.         extern char exedir[];
  9611.  
  9612.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9613.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9614.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9615.             if (ssl_debug_flag)
  9616.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9617.         }
  9618.  
  9619.         ckmakmsg(path,CKMAXPATH,
  9620.                  (char *)GetAppData(1),"kermit 95/certs",NULL,NULL);
  9621.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9622.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9623.             if (ssl_debug_flag)
  9624.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9625.         }
  9626.  
  9627.         ckmakmsg(path,CKMAXPATH,
  9628.                  (char *)GetAppData(0),"kermit 95/certs",NULL,NULL);
  9629.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9630.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9631.             if (ssl_debug_flag)
  9632.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9633.         }
  9634.  
  9635.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  9636.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9637.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9638.             if (ssl_debug_flag)
  9639.                 printf("?Unable to load verify-file: %s\r\n",path);
  9640.         }
  9641.  
  9642.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  9643.          "kermit 95/ca_certs.pem",NULL,NULL);
  9644.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9645.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9646.             if (ssl_debug_flag)
  9647.                 printf("?Unable to load verify-file: %s\r\n",path);
  9648.         }
  9649.  
  9650.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  9651.          "kermit 95/ca_certs.pem",NULL,NULL);
  9652.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9653.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9654.             if (ssl_debug_flag)
  9655.                 printf("?Unable to load verify-file: %s\r\n",path);
  9656.         }
  9657.     }
  9658. #else /* NT */
  9659.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9660.     {
  9661.  
  9662.         char path[CKMAXPATH];
  9663.         extern char exedir[];
  9664.  
  9665.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9666.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9667.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9668.             if (ssl_debug_flag)
  9669.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9670.         }
  9671.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  9672.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9673.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9674.             if (ssl_debug_flag)
  9675.                 printf("?Unable to load verify-file: %s\r\n",path);
  9676.         }
  9677.     }
  9678. #endif /* NT */
  9679. #else /* OS2 */
  9680.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  9681. #endif /* OS2 */
  9682.  
  9683.     if (ssl_verify_file &&
  9684.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
  9685.         debug(F110,
  9686.               "ftp ssl auth unable to load ssl_verify_file",
  9687.               ssl_verify_file,
  9688.               0
  9689.               );
  9690.         if (ssl_debug_flag)
  9691.           printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  9692.     }
  9693.     if (ssl_verify_dir &&
  9694.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
  9695.         debug(F110,
  9696.               "ftp ssl auth unable to load ssl_verify_dir",
  9697.               ssl_verify_dir,
  9698.               0
  9699.               );
  9700.         if (ssl_debug_flag)
  9701.           printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  9702.     }
  9703.  
  9704.     /* set up the new CRL Store */
  9705.     crl_store = (X509_STORE *)X509_STORE_new();
  9706.     if (crl_store) {
  9707. #ifdef OS2
  9708.         char path[CKMAXPATH];
  9709.         extern char exedir[];
  9710.  
  9711.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  9712.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9713.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9714.             if (ssl_debug_flag)
  9715.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9716.         }
  9717. #ifdef NT
  9718.         ckmakmsg(path,CKMAXPATH,
  9719.          (char *)GetAppData(1),"kermit 95/crls",NULL,NULL);
  9720.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9721.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9722.             if (ssl_debug_flag)
  9723.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9724.         }
  9725.         ckmakmsg(path,CKMAXPATH,
  9726.          (char *)GetAppData(0),"kermit 95/crls",NULL,NULL);
  9727.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9728.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9729.             if (ssl_debug_flag)
  9730.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9731.         }
  9732. #endif /* NT */
  9733.         
  9734.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  9735.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9736.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9737.             if (ssl_debug_flag)
  9738.                 printf("?Unable to load crl-file: %s\r\n",path);
  9739.         }
  9740. #ifdef NT
  9741.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  9742.          "kermit 95/ca_crls.pem",NULL,NULL);
  9743.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9744.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9745.             if (ssl_debug_flag)
  9746.                 printf("?Unable to load crl-file: %s\r\n",path);
  9747.         }
  9748.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  9749.          "kermit 95/ca_crls.pem",NULL,NULL);
  9750.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9751.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9752.             if (ssl_debug_flag)
  9753.                 printf("?Unable to load crl-file: %s\r\n",path);
  9754.         }
  9755. #endif /* NT */
  9756. #endif /* OS2 */
  9757.  
  9758.         if (ssl_crl_file || ssl_crl_dir) {
  9759.             if (ssl_crl_file &&
  9760.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  9761.                 debug(F110,
  9762.                       "ftp ssl auth unable to load ssl_crl_file",
  9763.                       ssl_crl_file,
  9764.                       0
  9765.                       );
  9766.                 if (ssl_debug_flag)
  9767.                   printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  9768.             }
  9769.             if (ssl_crl_dir &&
  9770.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  9771.                 debug(F110,
  9772.                       "ftp ssl auth unable to load ssl_crl_dir",
  9773.                       ssl_crl_dir,
  9774.                       0
  9775.                       );
  9776.                 if (ssl_debug_flag)
  9777.                   printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  9778.             }
  9779.         } else {
  9780.             X509_STORE_set_default_paths(crl_store);
  9781.         }
  9782.     }
  9783.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  9784.                        ssl_client_verify_callback);
  9785.     ssl_verify_depth = -1;
  9786.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  9787.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  9788.     SSL_set_fd(ssl_ftp_con,csocket);
  9789.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  9790.     if (ssl_cipher_list) {
  9791.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  9792.     } else {
  9793.         char * p;
  9794.         if (p = getenv("SSL_CIPHER")) {
  9795.             SSL_set_cipher_list(ssl_ftp_con,p);
  9796.         } else {
  9797.             SSL_set_cipher_list(ssl_ftp_con,
  9798.                                 "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP"
  9799.                                 );
  9800.         }
  9801.     }
  9802.     if (ssl_debug_flag) {
  9803.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  9804.         fflush(stderr);
  9805.     }
  9806.     if (SSL_connect(ssl_ftp_con) <= 0) {
  9807.         static char errbuf[1024];
  9808.         ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  9809.                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  9810.         fprintf(stderr,"%s\n", errbuf);
  9811.         fflush(stderr);
  9812.         ssl_ftp_active_flag=0;
  9813.         SSL_free(ssl_ftp_con);
  9814.         ssl_ftp_con = NULL;
  9815.     } else {
  9816.         ssl_ftp_active_flag = 1;
  9817.  
  9818.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  9819.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  9820.  
  9821.             if (!subject) {
  9822.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  9823.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9824.                     return(ssl_ftp_active_flag = 0);
  9825.                 } else {
  9826.                     if (uq_ok("Warning: Server didn't provide a certificate\n",
  9827.                                "Continue? (Y/N)",3,NULL,0) <= 0) {
  9828.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  9829.                         return(ssl_ftp_active_flag = 0);
  9830.                     }
  9831.                 }
  9832.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_user_host)) {
  9833.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9834.                 return(ssl_ftp_active_flag = 0);
  9835.             }
  9836.         }
  9837.         debug(F110,"ssl_auth","[SSL - OK]",0);
  9838.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  9839.     }
  9840.     if (ssl_debug_flag) {
  9841.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  9842.         fflush(stderr);
  9843.     }
  9844.     return(ssl_ftp_active_flag);
  9845. }
  9846. #endif /* CK_SSL */
  9847.  
  9848. static sigtype
  9849. cmdcancel(sig) int sig; {
  9850. #ifdef OS2
  9851.     /* In Unix we "chain" to trap(), which prints this */
  9852.     printf("^C...\n");
  9853. #endif /* OS2 */
  9854.     debug(F100,"ftp cmdcancel caught SIGINT ","",0);
  9855.     fflush(stdout);
  9856.     secure_getc(0,1);            /* Initialize net input buffers */
  9857.     cancelfile++;
  9858.     cancelgroup++;
  9859.     mlsreset();
  9860. #ifndef OS2
  9861. #ifdef FTP_PROXY
  9862.     if (ptflag)                         /* proxy... */
  9863.       longjmp(ptcancel,1);
  9864. #endif /* FTP_PROXY */
  9865.     debug(F100,"ftp cmdcancel chain to trap()...","",0);
  9866.     trap(SIGINT);
  9867.     /* NOTREACHED */
  9868.     debug(F100,"ftp cmdcancel return from trap()...","",0);
  9869. #else
  9870.     debug(F100,"ftp cmdcancel PostCtrlCSem()...","",0);
  9871.     PostCtrlCSem();
  9872. #endif /* OS2 */
  9873. }
  9874.  
  9875. static int
  9876. #ifdef CK_ANSIC
  9877. scommand(char * s)                      /* Was secure_command() */
  9878. #else
  9879. scommand(s) char * s;
  9880. #endif /* CK_ANSIC */
  9881. {
  9882.     int length = 0, len2;
  9883.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  9884. #ifdef CK_SSL
  9885.     if (ssl_ftp_active_flag) {
  9886.         int error, rc;
  9887.         length = strlen(s) + 2;
  9888.         length = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  9889.         rc = SSL_write(ssl_ftp_con,out,length);
  9890.         error = SSL_get_error(ssl_ftp_con,rc);
  9891.         switch (error) {
  9892.           case SSL_ERROR_NONE:
  9893.             return(1);
  9894.           case SSL_ERROR_WANT_WRITE:
  9895.           case SSL_ERROR_WANT_READ:
  9896.           case SSL_ERROR_SYSCALL:
  9897. #ifdef NT
  9898.             {
  9899.                 int gle = GetLastError();
  9900.             }
  9901. #endif /* NT */
  9902.           case SSL_ERROR_WANT_X509_LOOKUP:
  9903.           case SSL_ERROR_SSL:
  9904.           case SSL_ERROR_ZERO_RETURN:
  9905.           default:
  9906.             lostpeer();
  9907.         }
  9908.         return(0);
  9909.     }
  9910. #endif /* CK_SSL */
  9911.  
  9912.     if (auth_type && ftp_cpl != FPL_CLR) {
  9913. #ifdef FTP_SRP
  9914.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  9915.           if ((length = srp_encode(ftp_cpl == FPL_PRV,
  9916.                                    (CHAR *)s,
  9917.                                    (CHAR *)out,
  9918.                                    strlen(s))) < 0) {
  9919.               fprintf(stderr, "SRP failed to encode message\n");
  9920.               return(0);
  9921.           }
  9922. #endif /* FTP_SRP */
  9923. #ifdef FTP_KRB4
  9924.         if (ck_krb4_is_installed() &&
  9925.             (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  9926.             if (ftp_cpl == FPL_PRV) {
  9927.                 length =
  9928.                   krb_mk_priv((CHAR *)s, (CHAR *)out,
  9929.                               strlen(s), ftp_sched,
  9930. #ifdef KRB524
  9931.                               ftp_cred.session,
  9932. #else /* KRB524 */
  9933.                               &ftp_cred.session,
  9934. #endif /* KRB524 */
  9935.                               &myctladdr, &hisctladdr);
  9936.             } else {
  9937.                 length =
  9938.                   krb_mk_safe((CHAR *)s,
  9939.                               (CHAR *)out,
  9940.                               strlen(s),
  9941. #ifdef KRB524
  9942.                               ftp_cred.session,
  9943. #else /* KRB524 */
  9944.                               &ftp_cred.session,
  9945. #endif /* KRB524 */
  9946.                               &myctladdr, &hisctladdr);
  9947.             }
  9948.             if (length == -1) {
  9949.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  9950.                         ftp_cpl == FPL_PRV ? "priv" : "safe");
  9951.                 return(0);
  9952.             }
  9953.         }
  9954. #endif /* FTP_KRB4 */
  9955. #ifdef FTP_GSSAPI
  9956.         /* Scommand (based on level) */
  9957.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  9958.             gss_buffer_desc in_buf, out_buf;
  9959.             OM_uint32 maj_stat, min_stat;
  9960.             int conf_state;
  9961.             in_buf.value = s;
  9962.             in_buf.length = strlen(s) + 1;
  9963.             maj_stat = gss_seal(&min_stat, gcontext,
  9964.                                 (ftp_cpl==FPL_PRV), /* private */
  9965.                                 GSS_C_QOP_DEFAULT,
  9966.                                 &in_buf, &conf_state,
  9967.                                 &out_buf);
  9968.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  9969.                 user_gss_error(maj_stat, min_stat,
  9970.                                (ftp_cpl==FPL_PRV)?
  9971.                                "gss_seal ENC didn't complete":
  9972.                                "gss_seal MIC didn't complete");
  9973.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  9974.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  9975.             } else {
  9976.                 if (ftp_deb)
  9977.                   fprintf(stderr, "sealed (%s) %d bytes\n",
  9978.                           ftp_cpl==FPL_PRV?"ENC":"MIC",
  9979.                           out_buf.length);
  9980.                 memcpy(out, out_buf.value,
  9981.                        length=out_buf.length);
  9982.                 gss_release_buffer(&min_stat, &out_buf);
  9983.             }
  9984.         }
  9985. #endif /* FTP_GSSAPI */
  9986.         /* Other auth types go here ... */
  9987.  
  9988.         len2 = FTP_BUFSIZ;
  9989.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  9990.                                    length, &len2, RADIX_ENCODE))
  9991.             ) {
  9992.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  9993.                     radix_error(kerror));
  9994.             return(0);
  9995.         }
  9996.         if (ftp_deb)
  9997.           fprintf(stderr, "scommand(%s)\nencoding %d bytes\n",
  9998.                   s, length);
  9999.         len2 = ckmakmsg(out,sizeof(out),ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  10000.                         in, "\r\n", NULL);
  10001.         send(csocket,(SENDARG2TYPE)out,len2,0);
  10002.     } else {
  10003.         char out[FTP_BUFSIZ];
  10004.         int len = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  10005.         send(csocket,(SENDARG2TYPE)out,len,0);
  10006.     }
  10007.     return(1);
  10008. }
  10009.  
  10010. static int
  10011. mygetc() {
  10012.     static char inbuf[4096];
  10013.     static int bp = 0, ep = 0;
  10014.     int rc;
  10015.  
  10016.     if (bp == ep) {
  10017.         bp = ep = 0;
  10018. #ifdef CK_SSL
  10019.         if (ssl_ftp_active_flag) {
  10020.             int error;
  10021.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  10022.             error = SSL_get_error(ssl_ftp_con,rc);
  10023.             switch (error) {
  10024.               case SSL_ERROR_NONE:
  10025.                 break;
  10026.               case SSL_ERROR_WANT_WRITE:
  10027.               case SSL_ERROR_WANT_READ:
  10028.                 return(0);
  10029.               case SSL_ERROR_SYSCALL:
  10030.                 if (rc == 0) {          /* EOF */
  10031.                     break;
  10032.                 } else {
  10033. #ifdef NT
  10034.                     int gle = GetLastError();
  10035. #endif /* NT */
  10036.                     break;
  10037.                 }
  10038.               case SSL_ERROR_WANT_X509_LOOKUP:
  10039.               case SSL_ERROR_SSL:
  10040.               case SSL_ERROR_ZERO_RETURN:
  10041.               default:
  10042.                 break;
  10043.             }
  10044.         } else
  10045. #endif /* CK_SSL */
  10046.           rc = recv(csocket,(char *)inbuf,4096,0);
  10047.         if (rc <= 0)
  10048.           return(EOF);
  10049.         ep = rc;
  10050.     }
  10051.     return(inbuf[bp++]);
  10052. }
  10053.  
  10054. /*  x l a t e c  --  Translate a character  */
  10055. /*
  10056.     Call with:
  10057.       fc    = Function code: 0 = translate, 1 = initialize.
  10058.       c     = Character (as int).
  10059.       incs  = Index of charset to translate from.
  10060.       outcs = Index of charset to translate to.
  10061.  
  10062.     Returns:
  10063.       0: OK
  10064.      -1: Error
  10065. */
  10066. static int
  10067. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  10068. #ifdef NOCSETS
  10069.     return(c);
  10070. #else
  10071.     static char buf[128];
  10072.     static int cx;
  10073.     int c0, c1;
  10074.  
  10075.     if (fc == 1) {                      /* Initialize */
  10076.         cx = 0;                         /* Catch-up buffer write index */
  10077.         xgnbp = buf;                    /* Catch-up buffer read pointer */
  10078.         buf[0] = NUL;                   /* Buffer is empty */
  10079.         return(0);
  10080.     }
  10081.     if (cx >= 127) {                    /* Catch-up buffer full */
  10082.         debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  10083.         printf("?Translation buffer overflow\n");
  10084.         return(-1);
  10085.     }
  10086.     /* Add char to buffer. */
  10087.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  10088.  
  10089.     debug(F000,"xlatec buf",ckitoa(cx),c);
  10090.     buf[cx++] = c;
  10091.     buf[cx] = NUL;
  10092.  
  10093.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  10094.         if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)    /* (NULL was xprintc) */
  10095.           return(-1);
  10096.     }
  10097.     /* If we're caught up, reinitialize the buffer */
  10098.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  10099. #endif /* NOCSETS */
  10100. }
  10101.  
  10102.  
  10103. /*  p a r s e f e a t  */
  10104.  
  10105. /* Note: for convenience we align keyword values with table indices */
  10106. /* If you need to insert a new keyword, adjust the SFT_xxx definitions */
  10107.  
  10108. static struct keytab feattab[] = {
  10109.     { "$$$$", 0,        0 },        /* Dummy for sfttab[0] */
  10110.     { "AUTH", SFT_AUTH, 0 },
  10111.     { "LANG", SFT_LANG, 0 },
  10112.     { "MDTM", SFT_MDTM, 0 },
  10113.     { "MLST", SFT_MLST, 0 },
  10114.     { "PBSZ", SFT_PBSZ, 0 },
  10115.     { "PROT", SFT_PROT, 0 },
  10116.     { "REST", SFT_REST, 0 },
  10117.     { "SIZE", SFT_SIZE, 0 },
  10118.     { "TVFS", SFT_TVFS, 0 },
  10119.     { "UTF8", SFT_UTF8, 0 }
  10120. };
  10121. static int nfeattab = (sizeof(feattab) / sizeof(struct keytab));
  10122.  
  10123. #define FACT_CSET  1
  10124. #define FACT_CREA  2
  10125. #define FACT_LANG  3
  10126. #define FACT_MTYP  4
  10127. #define FACT_MDTM  5
  10128. #define FACT_PERM  6
  10129. #define FACT_SIZE  7
  10130. #define FACT_TYPE  8
  10131. #define FACT_UNIQ  9
  10132.  
  10133. static struct keytab facttab[] = {
  10134.     { "CHARSET",    FACT_CSET, 0 },
  10135.     { "CREATE",     FACT_CREA, 0 },
  10136.     { "LANG",       FACT_LANG, 0 },
  10137.     { "MEDIA-TYPE", FACT_MTYP, 0 },
  10138.     { "MODIFY",     FACT_MDTM, 0 },
  10139.     { "PERM",       FACT_PERM, 0 },
  10140.     { "SIZE",       FACT_SIZE, 0 },
  10141.     { "TYPE",       FACT_TYPE, 0 },
  10142.     { "UNIQUE",     FACT_UNIQ, 0 }
  10143. };
  10144. static int nfacttab = (sizeof(facttab) / sizeof(struct keytab));
  10145.  
  10146. static struct keytab ftyptab[] = {
  10147.     { "CDIR", FTYP_CDIR, 0 },
  10148.     { "DIR",  FTYP_DIR,  0 },
  10149.     { "FILE", FTYP_FILE, 0 },
  10150.     { "PDIR", FTYP_PDIR, 0 }
  10151. };
  10152. static int nftyptab = (sizeof(ftyptab) / sizeof(struct keytab));
  10153.  
  10154. static VOID
  10155. parsefeat(s) char * s; {        /* Parse a FEATURE response */
  10156.     char kwbuf[8];
  10157.     int i, x;
  10158.     if (!s) return;
  10159.     if (!*s) return;
  10160.     while (*s < '!')
  10161.       s++;
  10162.     for (i = 0; i < 4; i++) {
  10163.     if (s[i] < '!')
  10164.       break;
  10165.     kwbuf[i] = s[i];
  10166.     }
  10167.     if (s[i] && s[i] != SP)
  10168.       return;
  10169.     kwbuf[i] = NUL;
  10170.     /* xlookup requires a full (but case independent) match */
  10171.     i = xlookup(feattab,kwbuf,nfeattab,&x);
  10172.     debug(F111,"ftp parsefeat",s,i);
  10173.     if (i < 0 || i > 15)
  10174.       return;
  10175.  
  10176.     switch (i) {
  10177.       case SFT_MDTM:            /* Controlled by ENABLE/DISABLE */
  10178.     sfttab[i] = mdtmok;
  10179.     if (mdtmok) sfttab[0]++;
  10180.     break;
  10181.       case SFT_MLST:            /* ditto */
  10182.     sfttab[i] = mlstok;
  10183.     if (mlstok) sfttab[0]++;
  10184.     break;
  10185.       case SFT_SIZE:            /* ditto */
  10186.     sfttab[i] = sizeok;
  10187.     if (sizeok) sfttab[0]++;
  10188.     break;
  10189.       case SFT_AUTH:            /* ditto */
  10190.     sfttab[i] = ftp_aut;
  10191.     if (ftp_aut) sfttab[0]++;
  10192.     break;
  10193.       default:                /* Others */
  10194.     sfttab[0]++;
  10195.     sfttab[i]++;
  10196.     }
  10197. }
  10198.  
  10199. static char *
  10200. parsefacts(s) char * s; {        /* Parse MLS[DT] File Facts */
  10201.     char * p;
  10202.     int i, j, x;
  10203.     if (!s) return(NULL);
  10204.     if (!*s) return(NULL);
  10205.  
  10206.     /* Maybe we should make a copy of s so we can poke it... */
  10207.  
  10208.     while ((p = ckstrchr(s,'='))) {
  10209.     *p = NUL;            /* s points to fact */
  10210.     i = xlookup(facttab,s,nfacttab,&x); 
  10211.     debug(F111,"ftp parsefact fact",s,i);
  10212.     *p = '=';
  10213.     s = p+1;            /* Now s points to arg */
  10214.     p =  ckstrchr(s,';');
  10215.     if (!p) {
  10216.         debug(F110,"ftp parsefact end-of-val search fail",s,0);
  10217.         break;
  10218.     }
  10219.     *p = NUL;
  10220.     debug(F110,"ftp parsefact valu",s,0);
  10221.     switch (i) {
  10222.       case FACT_CSET:        /* Ignore these for now */
  10223.       case FACT_CREA:
  10224.       case FACT_LANG:
  10225.       case FACT_PERM:
  10226.       case FACT_MTYP:
  10227.       case FACT_UNIQ:
  10228.         break;
  10229.       case FACT_MDTM:        /* Modtime */
  10230.         makestr(&havemdtm,s);
  10231.         debug(F110,"ftp parsefact mdtm",havemdtm,0);
  10232.         break;
  10233.       case FACT_SIZE:        /* Size */
  10234.         havesize = atol(s);
  10235.         debug(F101,"ftp parsefact size","",havesize);
  10236.         break;
  10237.       case FACT_TYPE:        /* Type */
  10238.         j = xlookup(ftyptab,s,nftyptab,NULL);
  10239.         debug(F111,"ftp parsefact type",s,j);
  10240.         havetype = (j < 1) ? 0 : j;
  10241.         break;
  10242.     }
  10243.     *p = ';';
  10244.     s = p+1;            /* s points next fact or name */
  10245.     }
  10246.     while (*s == SP)            /* Skip past spaces. */
  10247.       s++;
  10248.     if (!*s)                /* Make sure we still have a name */
  10249.       s = NULL;
  10250.     debug(F110,"ftp parsefact name",s,0);
  10251.     return(s);
  10252. }
  10253.  
  10254. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  10255.  
  10256. static int
  10257. getreply(expecteof,lcs,rcs,vbm,fc) int expecteof, lcs, rcs, vbm, fc; {
  10258.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  10259.     register int i, c, n;
  10260.     register int dig;
  10261.     register char *cp;
  10262.     int xlate = 0;
  10263.     int count = 0;
  10264.     int auth = 0;
  10265.     int originalcode = 0, continuation = 0;
  10266.     sig_t oldintr;
  10267.     int pflag = 0;
  10268.     char *pt = pasv;
  10269.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  10270.     int safe = 0;
  10271.  
  10272.     auth = (fc == GRF_AUTH);
  10273.  
  10274. #ifndef NOCSETS
  10275.     debug(F101,"ftp getreply lcs","",lcs);
  10276.     debug(F101,"ftp getreply rcs","",rcs);
  10277.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  10278.         xlate = 1;
  10279.         initxlate(rcs,lcs);
  10280.         xlatec(1,0,rcs,lcs);
  10281.     }
  10282. #endif /* NOCSETS */
  10283.     debug(F101,"ftp getreply fc","",fc);
  10284.  
  10285.     if (ftp_deb)                        /* DEBUG */
  10286.       vbm = 1;
  10287.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  10288.       vbm = 0;
  10289.     else if (vbm < 0)                   /* VERBOSE */
  10290.       vbm = ftp_vbm;
  10291.  
  10292.     ibuf[0] = '\0';
  10293.     if (reply_parse)
  10294.       reply_ptr = reply_buf;
  10295.     havesigint = 0;
  10296.     oldintr = signal(SIGINT, cmdcancel);
  10297.     for (count = 0;; count++) {
  10298.         obuf[0] = '\0';
  10299.         dig = n = ftpcode = i = 0;
  10300.         cp = ftp_reply_str;
  10301.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  10302.             if (c == IAC) {             /* Handle telnet commands */
  10303.                 switch (c = mygetc()) {
  10304.                   case WILL:
  10305.                   case WONT:
  10306.                     c = mygetc();
  10307.                     obuf[0] = IAC;
  10308.                     obuf[1] = DONT;
  10309.                     obuf[2] = c;
  10310.                     obuf[3] = NUL;
  10311. #ifdef CK_SSL
  10312.                     if (ssl_ftp_active_flag) {
  10313.                         int error, rc;
  10314.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10315.                         error = SSL_get_error(ssl_ftp_con,rc);
  10316.                         switch (error) {
  10317.                           case SSL_ERROR_NONE:
  10318.                             break;
  10319.                           case SSL_ERROR_WANT_WRITE:
  10320.                           case SSL_ERROR_WANT_READ:
  10321.                             return(0);
  10322.                           case SSL_ERROR_SYSCALL:
  10323.                             if (rc == 0) { /* EOF */
  10324.                                 break;
  10325.                             } else {
  10326. #ifdef NT
  10327.                                 int gle = GetLastError();
  10328. #endif /* NT */
  10329.                                 break;
  10330.                             }
  10331.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10332.                           case SSL_ERROR_SSL:
  10333.                           case SSL_ERROR_ZERO_RETURN:
  10334.                           default:
  10335.                             break;
  10336.                         }
  10337.                     } else
  10338. #endif /* CK_SSL */
  10339.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10340.                     break;
  10341.                   case DO:
  10342.                   case DONT:
  10343.                     c = mygetc();
  10344.                     obuf[0] = IAC;
  10345.                     obuf[1] = WONT;
  10346.                     obuf[2] = c;
  10347.                     obuf[3] = NUL;
  10348. #ifdef CK_SSL
  10349.                     if (ssl_ftp_active_flag) {
  10350.                         int error, rc;
  10351.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10352.                         error = SSL_get_error(ssl_ftp_con,rc);
  10353.                         switch (error) {
  10354.                           case SSL_ERROR_NONE:
  10355.                             break;
  10356.                           case SSL_ERROR_WANT_WRITE:
  10357.                           case SSL_ERROR_WANT_READ:
  10358.                               signal(SIGINT,oldintr);
  10359.                               return(0);
  10360.                           case SSL_ERROR_SYSCALL:
  10361.                             if (rc == 0) { /* EOF */
  10362.                                 break;
  10363.                             } else {
  10364. #ifdef NT
  10365.                                 int gle = GetLastError();
  10366. #endif /* NT */
  10367.                                 break;
  10368.                             }
  10369.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10370.                           case SSL_ERROR_SSL:
  10371.                           case SSL_ERROR_ZERO_RETURN:
  10372.                           default:
  10373.                             break;
  10374.                         }
  10375.                     } else
  10376. #endif /* CK_SSL */
  10377.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10378.                     break;
  10379.                   default:
  10380.                     break;
  10381.                 }
  10382.                 continue;
  10383.             }
  10384.             dig++;
  10385.             if (c == EOF) {
  10386.                 if (expecteof) {
  10387.                     signal(SIGINT,oldintr);
  10388.                     ftpcode = 221;
  10389.                     debug(F101,"ftp getreply EOF","",ftpcode);
  10390.                     return(0);
  10391.                 }
  10392.                 lostpeer();
  10393.                 if (!quiet) {
  10394.                     if (ftp_deb)
  10395.                       printf("421 ");
  10396.                     printf(
  10397.                       "Service not available, connection closed by server\n");
  10398.                     fflush(stdout);
  10399.                 }
  10400.                 signal(SIGINT,oldintr);
  10401.                 ftpcode = 421;
  10402.                 debug(F101,"ftp getreply EOF","",ftpcode);
  10403.                 return(4);
  10404.             }
  10405.             if (n == 0) {        /* First digit */
  10406.         n = c;            /* Save it */
  10407.         }
  10408.             if (auth_type &&
  10409. #ifdef CK_SSL
  10410.                 !ssl_ftp_active_flag &&
  10411. #endif /* CK_SSL */
  10412.                 !ibuf[0] && (n == '6' || continuation)) {
  10413.                 if (c != '\r' && dig > 4)
  10414.                   obuf[i++] = c;
  10415.             } else {
  10416.                 if (auth_type &&
  10417. #ifdef CK_SSL
  10418.                     !ssl_ftp_active_flag &&
  10419. #endif /* CK_SSL */
  10420.                     !ibuf[0] && dig == 1 && vbm)
  10421.                   printf("Unauthenticated reply received from server:\n");
  10422.                 if (reply_parse) {
  10423.                     *reply_ptr++ = c;
  10424.                     *reply_ptr = NUL;
  10425.                 }
  10426.                 if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  10427.                     ftp_cmdlin < 2) {
  10428.                     if ((c != '\r') &&
  10429.                         (ftp_deb || ((vbm || (!auth && n == '5')) &&
  10430.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
  10431.                         )))))
  10432.                     {
  10433. #ifdef FTP_PROXY
  10434.                         if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  10435.                           printf("%s:",ftp_host);
  10436. #endif /* FTP_PROXY */
  10437.  
  10438.                         if (!quiet) {
  10439. #ifdef NOCSETS
  10440.                             printf("%c",c);
  10441. #else
  10442.                             if (xlate) {
  10443.                                 xlatec(0,c,rcs,lcs);
  10444.                             } else {
  10445.                                 printf("%c",c);
  10446.                             }
  10447. #endif /* NOCSETS */
  10448.                         }
  10449.                     }
  10450.                 }
  10451.             }
  10452.             if (auth_type &&
  10453. #ifdef CK_SSL
  10454.                 !ssl_ftp_active_flag &&
  10455. #endif /* CK_SSL */
  10456.                 !ibuf[0] && n != '6')
  10457.               continue;
  10458.             if (dig < 4 && isdigit(c))
  10459.               ftpcode = ftpcode * 10 + (c - '0');
  10460.             if (!pflag && ftpcode == 227)
  10461.               pflag = 1;
  10462.             if (dig > 4 && pflag == 1 && isdigit(c))
  10463.               pflag = 2;
  10464.             if (pflag == 2) {
  10465.                 if (c != '\r' && c != ')')
  10466.                   *pt++ = c;
  10467.                 else {
  10468.                     *pt = '\0';
  10469.                     pflag = 3;
  10470.                 }
  10471.             }
  10472.             if (dig == 4 && c == '-' && n != '6') {
  10473.                 if (continuation)
  10474.                   ftpcode = 0;
  10475.                 continuation++;
  10476.             }
  10477.             if (cp < &ftp_reply_str[sizeof(ftp_reply_str) - 1]) {
  10478.                 *cp++ = c;
  10479.                 *cp = NUL;
  10480.             }
  10481.         }
  10482.         if (deblog ||
  10483. #ifdef COMMENT
  10484. /*
  10485.   Sometimes we need to print the server reply.  printlines is nonzero for any
  10486.   command where the results are sent back on the control connection rather
  10487.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  10488.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  10489.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  10490.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  10491.   lines from UNIX.  Further experimentation needed with other servers.  Of
  10492.   course RFC959 is mute as to the format of the server reply.
  10493.  
  10494.   'printlines' is also true for PWD and BYE.
  10495. */
  10496.         (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  10497. #else
  10498. /* No, we can't be that clever -- it breaks other things like RPWD... */
  10499.             (printlines &&
  10500.              (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  10501. #endif /* COMMENT */
  10502.             ) {
  10503.             char * q = cp;
  10504.             char *r = ftp_reply_str;
  10505.             *q-- = NUL;                 /* NUL-terminate */
  10506.             while (*q < '!' && q > r)   /* Strip CR, etc */
  10507.               *q-- = NUL;
  10508.             if (!ftp_deb && printlines) { /* If printing */
  10509.                 if (ftpcode != 0)       /* strip ftpcode if any */
  10510.                   r += 4;
  10511. #ifdef NOCSETS
  10512.                 printf("%s\n",r);       /* and print */
  10513. #else
  10514.                 if (!xlate) {
  10515.                     printf("%s\n",r);
  10516.                 } else {        /* Translating */
  10517.                     xgnbp = r;        /* Set up strgetc() */
  10518.                     while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  10519.                         if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {    /* (xprintc) */
  10520.                             signal(SIGINT,oldintr);
  10521.                             return(-1);
  10522.                         }
  10523.                     }
  10524.                     printf("\n");
  10525.                 }
  10526. #endif /* NOCSETS */
  10527.             }
  10528.         }
  10529.     debug(F110,"FTP RCVD ",ftp_reply_str,0);
  10530.  
  10531.     if (fc == GRF_FEAT) {        /* Parsing FEAT command response? */
  10532.         if (count == 0 && n == '2') {
  10533.         int i;            /* (Re)-init server FEATure table */
  10534.         debug(F100,"ftp getreply clearing feature table","",0);
  10535.         for (i = 0; i < 16; i++)
  10536.           sfttab[i] = 0;
  10537.         } else {
  10538.         parsefeat((char *)ftp_reply_str);
  10539.         }
  10540.     }
  10541.         if (auth_type &&
  10542. #ifdef CK_SSL
  10543.             !ssl_ftp_active_flag &&
  10544. #endif /* CK_SSL */
  10545.              !ibuf[0] && n != '6') {
  10546.             signal(SIGINT,oldintr);
  10547.             return(getreply(expecteof,lcs,rcs,vbm,auth));
  10548.         }
  10549.         ibuf[0] = obuf[i] = '\0';
  10550.         if (ftpcode && n == '6')
  10551.           if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  10552.               printf("Unknown reply: %d %s\n", ftpcode, obuf);
  10553.               n = '5';
  10554.           } else safe = (ftpcode == 631);
  10555.         if (obuf[0]                     /* if there is a string to decode */
  10556. #ifdef CK_SSL
  10557.             && !ssl_ftp_active_flag     /* and not SSL/TLS */
  10558. #endif /* CK_SSL */
  10559.             ) {
  10560.             if (!auth_type) {
  10561.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  10562.                 n = '5';
  10563.             }
  10564. #ifndef CK_ENCRYPTION
  10565.             else if (ftpcode == 632) {
  10566.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10567.                 n = '5';
  10568.             }
  10569. #endif /* CK_ENCRYPTION */
  10570. #ifdef NOCONFIDENTIAL
  10571.             else if (ftpcode == 633) {
  10572.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10573.                 n = '5';
  10574.             }
  10575. #endif /* NOCONFIDENTIAL */
  10576.             else {
  10577.                 int len = FTP_BUFSIZ;
  10578.                 if ((kerror = radix_encode((CHAR *)obuf,
  10579.                                            (CHAR *)ibuf,
  10580.                                            0,
  10581.                                            &len,
  10582.                                            RADIX_DECODE))
  10583.                     ) {
  10584.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  10585.                            ftpcode, radix_error(kerror), obuf);
  10586.                     n = '5';
  10587.                 }
  10588. #ifdef FTP_SRP
  10589.                 else if (strcmp(auth_type, "SRP") == 0) {
  10590.                     int outlen;
  10591.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  10592.                                         (CHAR *) ibuf, len);
  10593.                     if (outlen < 0) {
  10594.                         printf("Warning: %d reply %s!\n",
  10595.                                ftpcode, safe ? "modified" : "garbled");
  10596.                         n = '5';
  10597.                     } else {
  10598.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  10599.                         if (ftp_deb)
  10600.                           printf("%c:", safe ? 'S' : 'P');
  10601.                         continue;
  10602.                     }
  10603.                 }
  10604. #endif /* FTP_SRP */
  10605. #ifdef FTP_KRB4
  10606.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  10607.                     if (safe) {
  10608.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  10609. #ifdef KRB524
  10610.                                              ftp_cred.session,
  10611. #else /* KRB524 */
  10612.                                              &ftp_cred.session,
  10613. #endif /* KRB524 */
  10614.                                              &hisctladdr,
  10615.                                              &myctladdr,
  10616.                                              &ftp_msg_data
  10617.                                              );
  10618.                     } else {
  10619.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  10620.                                              ftp_sched,
  10621. #ifdef KRB524
  10622.                                              ftp_cred.session,
  10623. #else /* KRB524 */
  10624.                                              &ftp_cred.session,
  10625. #endif /* KRB524 */
  10626.                                              &hisctladdr,
  10627.                                              &myctladdr,
  10628.                                              &ftp_msg_data
  10629.                                              );
  10630.                     }
  10631.                     if (kerror != KSUCCESS) {
  10632.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  10633.                                safe ? "modified" : "garbled",
  10634.                                safe ? "safe" : "priv",
  10635.                                krb_get_err_text(kerror));
  10636.                         n = '5';
  10637.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  10638.                         kerror = KFAILURE;
  10639.                         n = '5';
  10640.                         printf("reply data too large for buffer\n");
  10641.                     } else {
  10642.                         if (ftp_deb)
  10643.                           printf("%c:", safe ? 'S' : 'P');
  10644.                         memcpy(ibuf,ftp_msg_data.app_data,
  10645.                                ftp_msg_data.app_length);
  10646.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  10647.                                   FTP_BUFSIZ - ftp_msg_data.app_length);
  10648.                         continue;
  10649.                     }
  10650.                 }
  10651. #endif /* FTP_KRB4 */
  10652. #ifdef FTP_GSSAPI
  10653.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  10654.                     gss_buffer_desc xmit_buf, msg_buf;
  10655.                     OM_uint32 maj_stat, min_stat;
  10656.                     int conf_state;
  10657.                     xmit_buf.value = ibuf;
  10658.                     xmit_buf.length = len;
  10659.                     /* decrypt/verify the message */
  10660.                     conf_state = safe;
  10661.                     maj_stat = gss_unseal(&min_stat, gcontext,
  10662.                                           &xmit_buf, &msg_buf,
  10663.                                           &conf_state, NULL);
  10664.                     if (maj_stat != GSS_S_COMPLETE) {
  10665.                         user_gss_error(maj_stat, min_stat,
  10666.                                        "failed unsealing reply");
  10667.                         n = '5';
  10668.                     } else {
  10669.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  10670.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  10671.                                   FTP_BUFSIZ-msg_buf.length);
  10672.                         gss_release_buffer(&min_stat,&msg_buf);
  10673.                         if (ftp_deb)
  10674.                           printf("%c:", safe ? 'S' : 'P');
  10675.                         continue;
  10676.                     }
  10677.                 }
  10678. #endif /* FTP_GSSAPI */
  10679.                 /* Other auth types go here... */
  10680.             }
  10681.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  10682.                    !quiet && (vbm || (!auth && (n == '4' || n == '5')))) {
  10683. #ifdef NOCSETS
  10684.             printf("%c",c);
  10685. #else
  10686.             if (xlate) {
  10687.                 xlatec(0,c,rcs,lcs);
  10688.             } else {
  10689.                 printf("%c",c);
  10690.             }
  10691. #endif /* NOCSETS */
  10692.             fflush (stdout);
  10693.         }
  10694.         if (continuation && ftpcode != originalcode) {
  10695.             if (originalcode == 0)
  10696.               originalcode = ftpcode;
  10697.             continue;
  10698.         }
  10699.         *cp = '\0';
  10700.         if (n != '1')
  10701.           cpend = 0;
  10702.         signal(SIGINT,oldintr);
  10703.         if (ftpcode == 421 || originalcode == 421) {
  10704.         lostpeer();
  10705.         if (!quiet && !ftp_deb)
  10706.           printf("%s\n",reply_buf);
  10707.         }
  10708.         if ((cancelfile != 0) &&
  10709. #ifndef ULTRIX3
  10710.             /* Ultrix 3.0 cc objects violently to this clause */
  10711.             (oldintr != cmdcancel) &&
  10712. #endif /* ULTRIX3 */
  10713.             (oldintr != SIG_IGN)) {
  10714.             if (oldintr)
  10715.               (*oldintr)(SIGINT);
  10716.         }
  10717.         if (reply_parse) {
  10718.             *reply_ptr = '\0';
  10719.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  10720.                 reply_parse = reply_ptr + strlen(reply_parse);
  10721.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  10722.                   *reply_ptr = '\0';
  10723.             } else
  10724.               reply_parse = reply_ptr;
  10725.         }
  10726.         while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
  10727.           *cp-- = NUL;
  10728.         debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  10729.         return(n - '0');
  10730.     } /* for (;;) */
  10731. }
  10732.  
  10733. #ifdef BSDSELECT
  10734. static int
  10735. #ifdef CK_ANSIC
  10736. empty(fd_set * mask, int sec)
  10737. #else
  10738. empty(mask, sec) fd_set * mask; int sec;
  10739. #endif /* CK_ANSIC */
  10740. {
  10741.     struct timeval t;
  10742.     t.tv_sec = (long) sec;
  10743.     t.tv_usec = 0L;
  10744.     debug(F100,"ftp empty calling select...","",0);
  10745. #ifdef INTSELECT
  10746.     x = select(32, (int *)mask, NULL, NULL, &t);
  10747. #else
  10748.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  10749. #endif /* INTSELECT */
  10750.     debug(F101,"ftp empty select","",x);
  10751.     return(x);
  10752. }
  10753. #else /* BSDSELECT */
  10754. #ifdef IBMSELECT
  10755. static int
  10756. empty(mask, cnt, sec) int * mask, sec;
  10757.                       int   cnt;
  10758. {
  10759.     return(select(mask,cnt,0,0,sec*1000));
  10760. }
  10761. #endif /* IBMSELECT */
  10762. #endif /* BSDSELECT */
  10763.  
  10764. static sigtype
  10765. cancelsend(sig) int sig; {
  10766.     havesigint++;
  10767.     cancelgroup++;
  10768.     cancelfile = 0;
  10769.     printf(" Canceled...\n");
  10770.     secure_getc(0,1);            /* Initialize net input buffers */
  10771.     debug(F100,"ftp cancelsend caught SIGINT ","",0);
  10772.     fflush(stdout);
  10773. #ifndef OS2
  10774.     longjmp(sendcancel, 1);
  10775. #else
  10776.     PostCtrlCSem();
  10777. #endif /* OS2 */
  10778. }
  10779.  
  10780. static VOID
  10781. #ifdef CK_ANSIC
  10782. secure_error(char *fmt, ...)
  10783. #else
  10784. /* VARARGS1 */
  10785. secure_error(fmt, p1, p2, p3, p4, p5)
  10786.    char *fmt; int p1, p2, p3, p4, p5;
  10787. #endif /* CK_ANSIC */
  10788. {
  10789. #ifdef CK_ANSIC
  10790.     va_list ap;
  10791.  
  10792.     va_start(ap, fmt);
  10793.     vfprintf(stderr, fmt, ap);
  10794.     va_end(ap);
  10795. #else
  10796.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  10797. #endif
  10798.     fprintf(stderr, "\n");
  10799. }
  10800.  
  10801. /*
  10802.  * Internal form of settype; changes current type in use with server
  10803.  * without changing our notion of the type for data transfers.
  10804.  * Used to change to and from ascii for listings.
  10805.  */
  10806. static VOID
  10807. changetype(newtype, show) int newtype, show; {
  10808.     int rc;
  10809.     char * s;
  10810.  
  10811.     if ((newtype == curtype) && typesent++)
  10812.       return;
  10813.     switch (newtype) {
  10814.       case FTT_ASC:
  10815.         s = "A";
  10816.         break;
  10817.       case FTT_BIN:
  10818.         s = "I";
  10819.         break;
  10820.       case FTT_TEN:
  10821.         s = "L 8";
  10822.         break;
  10823.       default:
  10824.         s = "I";
  10825.         break;
  10826.     }
  10827.     rc = ftpcmd("TYPE",s,-1,-1,show);
  10828.     if (rc == REPLY_COMPLETE)
  10829.       curtype = newtype;
  10830. }
  10831.  
  10832. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  10833.  
  10834. static int ftpsndret = 0;
  10835. static struct _ftpsnd {
  10836.     sig_t oldintr, oldintp;
  10837.     int            reply;
  10838.     int            incs,
  10839.                    outcs;
  10840.     char *         cmd, * local, * remote;
  10841.     int            bytes;
  10842.     int            restart;
  10843.     int            xlate;
  10844.     char *         lmode;
  10845. } ftpsnd;
  10846.  
  10847. static VOID
  10848. #ifdef CK_ANSIC
  10849. doftpsend(void * threadinfo)
  10850. #else
  10851. doftpsend(threadinfo) VOID * threadinfo;
  10852. #endif
  10853. {
  10854. #ifdef NTSIG
  10855.     if (threadinfo) {                   /* Thread local storage... */
  10856.         TlsSetValue(TlsIndex,threadinfo);
  10857.         debug(F100, "doftpsend called with threadinfo block","", 0);
  10858.     } else debug(F100, "doftpsend - threadinfo is NULL", "", 0);
  10859. #endif /* NTSIG */
  10860. #ifdef CK_LOGIN
  10861. #ifdef IKSD
  10862. #ifdef NT
  10863.     if (inserver)
  10864.       setntcreds();
  10865. #endif /* NT */
  10866. #endif /* IKSD */
  10867. #endif /* CK_LOGIN */
  10868.  
  10869.     if (initconn()) {
  10870. #ifndef NOHTTP
  10871.         int y = -1;
  10872.         debug(F101,"doftpsend","tcp_http_proxy",tcp_http_proxy);
  10873.  
  10874.        /*  If the connection failed and we are using an HTTP Proxy
  10875.         *  and the reason for the failure was an authentication
  10876.         *  error, then we need to give the user to ability to
  10877.         *  enter a username and password, just like a browser.
  10878.         *
  10879.         *  I tried to do all of this within the netopen() call
  10880.         *  but it is much too much work.
  10881.         */
  10882.         while (y != 0 && tcp_http_proxy != NULL ) {
  10883.  
  10884.             if (tcp_http_proxy_errno == 401 ||
  10885.                  tcp_http_proxy_errno == 407 ) {
  10886.                 char uid[UIDBUFLEN];
  10887.                 char pwd[256];
  10888.                 struct txtbox tb[2];
  10889.                 int ok;
  10890.  
  10891.                 tb[0].t_buf = uid;
  10892.                 tb[0].t_len = UIDBUFLEN;
  10893.                 tb[0].t_lbl = "Proxy Userid: ";
  10894.                 tb[0].t_dflt = NULL;
  10895.                 tb[0].t_echo = 1;
  10896.                 tb[1].t_buf = pwd;
  10897.                 tb[1].t_len = 256;
  10898.                 tb[1].t_lbl = "Proxy Passphrase: ";
  10899.                 tb[1].t_dflt = NULL;
  10900.                 tb[1].t_echo = 2;
  10901.  
  10902.                 ok = uq_mtxt("Proxy Server Authentication Required\n",
  10903.                               NULL, 2, tb);
  10904.                 if (ok && uid[0]) {
  10905.                     char * proxy_user, * proxy_pwd;
  10906.  
  10907.                     proxy_user = tcp_http_proxy_user;
  10908.                     proxy_pwd  = tcp_http_proxy_pwd;
  10909.  
  10910.                     tcp_http_proxy_user = uid;
  10911.                     tcp_http_proxy_pwd = pwd;
  10912.  
  10913.                     y = initconn();
  10914.  
  10915.                     debug(F101,"doftpsend","initconn",y);
  10916.                     memset(pwd,0,sizeof(pwd));
  10917.                     tcp_http_proxy_user = proxy_user;
  10918.                     tcp_http_proxy_pwd = proxy_pwd;
  10919.                 } else
  10920.                     break;
  10921.             } else
  10922.                 break;
  10923.         }
  10924.  
  10925.         if ( y != 0 ) {
  10926. #endif /* NOHTTP */
  10927.             signal(SIGINT, ftpsnd.oldintr);
  10928. #ifdef SIGPIPE
  10929.             if (ftpsnd.oldintp)
  10930.               signal(SIGPIPE, ftpsnd.oldintp);
  10931. #endif /* SIGPIPE */
  10932.             ftpcode = -1;
  10933.             zclose(ZIFILE);
  10934.             ftpsndret = -1;
  10935. #ifdef NTSIG
  10936.             ckThreadEnd(threadinfo);
  10937. #endif /* NTSIG */
  10938.             return;
  10939. #ifndef NOHTTP
  10940.         }
  10941. #endif /* NOHTTP */
  10942.     }
  10943.     ftpsndret = 0;
  10944. #ifdef NTSIG
  10945.      ckThreadEnd(threadinfo);
  10946. #endif /* NTSIG */
  10947. }
  10948.  
  10949. static VOID
  10950. #ifdef CK_ANSIC
  10951. failftpsend(void * threadinfo)
  10952. #else
  10953. failftpsend(threadinfo) VOID * threadinfo;
  10954. #endif /* CK_ANSIC */
  10955. {
  10956. #ifdef NTSIG
  10957.     if (threadinfo) {                   /* Thread local storage... */
  10958.         TlsSetValue(TlsIndex,threadinfo);
  10959.         debug(F100, "docmdfile called with threadinfo block","", 0);
  10960.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  10961. #endif /* NTSIG */
  10962. #ifdef CK_LOGIN
  10963. #ifdef IKSD
  10964. #ifdef NT
  10965.     if (inserver)
  10966.       setntcreds();
  10967. #endif /* NT */
  10968. #endif /* IKSD */
  10969. #endif /* CK_LOGIN */
  10970.  
  10971.     while (cpend) {
  10972.         ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  10973.         debug(F111,"ftp sendrequest getreply","null command",ftpsnd.reply);
  10974.     }
  10975.     if (data >= 0) {
  10976. #ifdef CK_SSL
  10977.         if (ssl_ftp_data_active_flag) {
  10978.             SSL_shutdown(ssl_ftp_data_con);
  10979.             SSL_free(ssl_ftp_data_con);
  10980.             ssl_ftp_data_active_flag = 0;
  10981.             ssl_ftp_data_con = NULL;
  10982.         }
  10983. #endif /* CK_SSL */
  10984. #ifdef TCPIPLIB
  10985.         socket_close(data);
  10986. #else /* TCPIPLIB */
  10987. #ifdef USE_SHUTDOWN
  10988.         shutdown(data, 1+1);
  10989. #endif /* USE_SHUTDOWN */
  10990.         close(data);
  10991. #endif /* TCPIPLIB */
  10992.         data = -1;
  10993.         globaldin = -1;
  10994.     }
  10995.     if (ftpsnd.oldintr)
  10996.         signal(SIGINT,ftpsnd.oldintr);
  10997. #ifdef SIGPIPE
  10998.     if (ftpsnd.oldintp)
  10999.         signal(SIGPIPE,ftpsnd.oldintp);
  11000. #endif /* SIGPIPE */
  11001.     ftpcode = -1;
  11002. #ifndef OS2
  11003.     /* TEST ME IN K95 */
  11004.     if (havesigint) {
  11005.     havesigint = 0;
  11006.     debug(F100,"ftp failftpsend chain to trap()...","",0);
  11007.     if (ftpsnd.oldintr != SIG_IGN)
  11008.       (*ftpsnd.oldintr)(SIGINT);
  11009.     /* NOTREACHED (I hope!) */
  11010.     debug(F100,"ftp failftpsend return from trap()...","",0);
  11011.     }
  11012. #endif /* OS2 */
  11013. }
  11014.  
  11015. static VOID
  11016. #ifdef CK_ANSIC
  11017. failftpsend2(void * threadinfo)
  11018. #else
  11019. failftpsend2(threadinfo) VOID * threadinfo;
  11020. #endif /* CK_ANSIC */
  11021. {
  11022. #ifdef NTSIG
  11023.     if (threadinfo) {                   /* Thread local storage... */
  11024.         TlsSetValue(TlsIndex,threadinfo);
  11025.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11026.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11027. #endif /* NTSIG */
  11028. #ifdef CK_LOGIN
  11029. #ifdef IKSD
  11030. #ifdef NT
  11031.     if (inserver)
  11032.       setntcreds();
  11033. #endif /* NT */
  11034. #endif /* IKSD */
  11035. #endif /* CK_LOGIN */
  11036.  
  11037.     debug(F101,"ftp sendrequest canceled","",ftpsnd.bytes);
  11038.     tfc += ffc;
  11039. #ifdef GFTIMER
  11040.     fpfsecs = gftimer();
  11041. #endif /* GFTIMER */
  11042.     zclose(ZIFILE);
  11043. #ifdef PIPESEND
  11044.     if (sndfilter)
  11045.       pipesend = 0;
  11046. #endif /* PIPESEND */
  11047.     signal(SIGINT, ftpsnd.oldintr);
  11048. #ifdef SIGPIPE
  11049.     if (ftpsnd.oldintp)
  11050.       signal(SIGPIPE, ftpsnd.oldintp);
  11051. #endif /* SIGPIPE */
  11052.     if (!cpend) {
  11053.         ftpcode = -1;
  11054.         ftpsndret = -1;
  11055. #ifdef NTSIG
  11056.         ckThreadEnd(threadinfo);
  11057. #endif /* NTSIG */
  11058.         return;
  11059.     }
  11060.     if (data >= 0) {
  11061. #ifdef CK_SSL
  11062.         if (ssl_ftp_data_active_flag) {
  11063.             SSL_shutdown(ssl_ftp_data_con);
  11064.             SSL_free(ssl_ftp_data_con);
  11065.             ssl_ftp_data_active_flag = 0;
  11066.             ssl_ftp_data_con = NULL;
  11067.         }
  11068. #endif /* CK_SSL */
  11069. #ifdef TCPIPLIB
  11070.         socket_close(data);
  11071. #else /* TCPIPLIB */
  11072. #ifdef USE_SHUTDOWN
  11073.         shutdown(data, 1+1);
  11074. #endif /* USE_SHUTDOWN */
  11075.         close(data);
  11076. #endif /* TCPIPLIB */
  11077.         data = -1;
  11078.         globaldin = -1;
  11079.     }
  11080.     if (dout) {
  11081. #ifdef TCPIPLIB
  11082.         socket_close(dout);
  11083. #else /* TCPIPLIB */
  11084. #ifdef USE_SHUTDOWN
  11085.         shutdown(dout, 1+1);
  11086. #endif /* USE_SHUTDOWN */
  11087.         close(dout);
  11088. #endif /* TCPIPLIB */
  11089.     }
  11090.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11091.     ftpcode = -1;
  11092.     ftpsndret = -1;
  11093.  
  11094. #ifndef OS2
  11095.     /* TEST ME IN K95 */
  11096.     if (havesigint) {
  11097.     havesigint = 0;
  11098.     debug(F100,"ftp failftpsend2 chain to trap()...","",0);
  11099.     if (ftpsnd.oldintr != SIG_IGN)
  11100.       (*ftpsnd.oldintr)(SIGINT);
  11101.     /* NOTREACHED (I hope!) */
  11102.     debug(F100,"ftp failftpsend2 return from trap()...","",0);
  11103.     }
  11104. #endif /* OS2 */
  11105. }
  11106.  
  11107. static VOID
  11108. #ifdef CK_ANSIC
  11109. doftpsend2(void * threadinfo)
  11110. #else
  11111. doftpsend2(threadinfo) VOID * threadinfo;
  11112. #endif
  11113. {
  11114.     register int c, d = 0;
  11115.     int n, t, x, notafile, unique = 0;
  11116.     char buf[FTP_BUFSIZ], *bufp;
  11117.     
  11118. #ifdef NTSIG
  11119.     if (threadinfo) {                   /* Thread local storage... */
  11120.         TlsSetValue(TlsIndex,threadinfo);
  11121.         debug(F100, "doftpsend2 called with threadinfo block","", 0);
  11122.     } else debug(F100, "doftpsend2 - threadinfo is NULL", "", 0);
  11123. #endif /* NTSIG */
  11124. #ifdef CK_LOGIN
  11125. #ifdef IKSD
  11126. #ifdef NT
  11127.     if (inserver)
  11128.       setntcreds();
  11129. #endif /* NT */
  11130. #endif /* IKSD */
  11131. #endif /* CK_LOGIN */
  11132.  
  11133.     unique = strcmp(ftpsnd.cmd,"STOU") ? 0 : 1;
  11134.     notafile = sndarray || pipesend;
  11135.  
  11136. #ifdef FTP_RESTART
  11137.     if (ftpsnd.restart && ((curtype == FTT_BIN) || (alike > 0))) {
  11138.         char * p;
  11139.         changetype(FTT_BIN,0);          /* Change to binary */
  11140.  
  11141.         /* Ask for remote file's size */
  11142.         x = ftpcmd("SIZE",ftpsnd.remote,ftpsnd.incs,ftpsnd.outcs,ftp_vbm);
  11143.  
  11144.         if (x == REPLY_COMPLETE) {      /* Have ftpsnd.reply */
  11145.             p = &ftp_reply_str[4];      /* Parse it */
  11146.             while (isdigit(*p)) {
  11147.                 sendstart = sendstart * 10 + (int)(*p - '0');
  11148.                 p++;
  11149.             }
  11150.             if (*p && *p != CR) {       /* Bad number */
  11151.                 debug(F110,"doftpsend2 bad size",ftp_reply_str,0);
  11152.                 sendstart = 0L;
  11153.             } else if (sendstart > fsize) { /* Remote file bigger than local */
  11154.                 debug(F110,"doftpsend2 big size",ckltoa(fsize),sendstart);
  11155.                 sendstart = 0L;
  11156.             }
  11157.         /* Local is newer */
  11158.             debug(F111,"doftpsend2 size",ftpsnd.remote,sendstart);
  11159.             if (chkmodtime(ftpsnd.local,ftpsnd.remote,0) == 2) {
  11160.                 debug(F110,"doftpsend2 date mismatch",ftp_reply_str,0);
  11161.                 sendstart = 0L;         /* Send the whole file */
  11162.             }
  11163.         }
  11164.         changetype(ftp_typ,0);          /* Change back to appropriate type */
  11165.         if (sendstart > 0L) {           /* Still restarting? */
  11166.             if (sendstart == fsize) {   /* Same size - no need to send */
  11167.                 debug(F111,"doftpsend2 /restart SKIP",fsize,sendstart);
  11168.                 zclose(ZIFILE);
  11169.                 ftpsndret = SKP_RES;
  11170. #ifdef NTSIG
  11171.                 ckThreadEnd(threadinfo);
  11172. #endif /* NTSIG */
  11173.                 return;
  11174.             }
  11175.             errno = 0;                  /* Restart needed, seek to the spot */
  11176.             if (zfseek((long)sendstart) < 0) {
  11177.                 debug(F111,"doftpsend2 zfseek fails",
  11178.               ftpsnd.local,sendstart);
  11179.                 fprintf(stderr, "FSEEK: %s: %s\n", ftpsnd.local, ck_errstr());
  11180.                 sendstart = 0;
  11181.                 zclose(ZIFILE);
  11182.                 ftpsndret = -1;
  11183. #ifdef NTSIG
  11184.                 ckThreadEnd(threadinfo);
  11185. #endif /* NTSIG */
  11186.                 return;
  11187.             }
  11188. #ifdef COMMENT
  11189.             debug(F111,"doftpsend2 zfseek ok",ftpsnd.local,sendstart);
  11190.             x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  11191.             if (x != REPLY_CONTINUE) {
  11192.                 sendstart = 0;
  11193.                 zclose(ZIFILE);
  11194.                 ftpsndret = -1;
  11195. #ifdef NTSIG
  11196.                 ckThreadEnd(threadinfo);
  11197. #endif /* NTSIG */
  11198.                 return;
  11199.             } else {
  11200.                 ftpsnd.cmd = "STOR";
  11201.             }
  11202. #else
  11203.             sendmode = SM_RESEND;
  11204.             ftpsnd.cmd = "APPE";
  11205. #endif /* COMMENT */
  11206.             /* sendstart = 0L; */
  11207.         }
  11208.     }
  11209. #endif /* FTP_RESTART */
  11210.  
  11211.     if (unique && !stouarg)        /* If we know STOU accepts no arg */
  11212.       ftpsnd.remote = NULL;        /* don't include one. */
  11213.  
  11214.     x = ftpcmd(ftpsnd.cmd, ftpsnd.remote, ftpsnd.incs, ftpsnd.outcs, ftp_vbm);
  11215.     debug(F111,"doftpsend2 ftpcode",ftpsnd.cmd,ftpcode);
  11216.  
  11217.     if (x != REPLY_PRELIM && unique) {
  11218.     /*
  11219.       RFC959 says STOU does not take an argument.  But every FTP server
  11220.       I've encountered but one accepts the arg and constructs the unique
  11221.       name from it, which is better than making up a totally random name
  11222.       for the file, which is what RFC959 calls for.  Especially because
  11223.       there is no way for the client to find out the name chosen by the
  11224.       server.  So we try STOU with the argument first, which works with
  11225.       most servers, and if it fails we retry it without the arg, for
  11226.       the benefit of the one picky server that is not "liberal in what
  11227.       it accepts" UNLESS the first STOU got a 502 code ("not implemented")
  11228.       which means STOU is not accepted, period.
  11229.     */
  11230.     if ((x == 5) && stouarg && (ftpcode != 502)) {
  11231.         x = ftpcmd(ftpsnd.cmd,NULL,ftpsnd.incs,ftpsnd.outcs,ftp_vbm); 
  11232.         if (x == REPLY_PRELIM)    /* If accepted */
  11233.           stouarg = 0;        /* flag no STOU arg for this server */
  11234.     }
  11235.     }
  11236.     if (x != REPLY_PRELIM) {
  11237.         signal(SIGINT, ftpsnd.oldintr);
  11238. #ifdef SIGPIPE
  11239.         if (ftpsnd.oldintp)
  11240.           signal(SIGPIPE, ftpsnd.oldintp);
  11241. #endif /* SIGPIPE */
  11242.         zclose(ZIFILE);
  11243. #ifdef PIPESEND
  11244.         if (sndfilter)
  11245.           pipesend = 0;
  11246. #endif /* PIPESEND */
  11247.         ftpsndret = -1;
  11248. #ifdef NTSIG
  11249.         ckThreadEnd(threadinfo);
  11250. #endif /* NTSIG */
  11251.         return;
  11252.     }
  11253.     dout = dataconn(ftpsnd.lmode);             /* Get data connection */
  11254.     if (dout == -1) {
  11255.         failftpsend2(threadinfo);
  11256. #ifdef NTSIG
  11257.         ckThreadEnd(threadinfo);
  11258. #endif /* NTSIG */
  11259.         return;
  11260.     }
  11261.  
  11262.     /* Initialize per-file stats */
  11263.     ffc = 0L;                           /* Character counter */
  11264.     cps = oldcps = 0L;                  /* Thruput */
  11265. #ifdef GFTIMER
  11266.     rftimer();                          /* reset f.p. timer */
  11267. #endif /* GFTIMER */
  11268.  
  11269. #ifdef SIGPIPE
  11270.     ftpsnd.oldintp = signal(SIGPIPE, SIG_IGN);
  11271. #endif /* SIGPIPE */
  11272.     switch (curtype) {
  11273.       case FTT_BIN:                     /* Binary mode */
  11274.       case FTT_TEN:
  11275.         errno = d = 0;
  11276.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ)) > 0 && !cancelfile) {
  11277.             ftpsnd.bytes += n;
  11278.             ffc += n;
  11279.             debug(F111,"doftpsend2 zxin",ckltoa(n),ffc);
  11280.             hexdump("doftpsend2 zxin",buf,16);
  11281. #ifdef CK_SSL
  11282.             if (ssl_ftp_data_active_flag) {
  11283.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11284.                     if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  11285.                       break;
  11286.                     spackets++;
  11287.                     pktnum++;
  11288.                     if (fdispla != XYFD_B) {
  11289.                         spktl = d;
  11290.                         ftscreen(SCR_PT,'D',spackets,NULL);
  11291.                     }
  11292.                 }
  11293.             } else {
  11294. #endif /* CK_SSL */
  11295.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11296.                     if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  11297.                         || iscanceled())
  11298.                       break;
  11299.                     spackets++;
  11300.                     pktnum++;
  11301.                     if (fdispla != XYFD_B) {
  11302.                         spktl = d;
  11303.                         ftscreen(SCR_PT,'D',spackets,NULL);
  11304.                     }
  11305.                 }
  11306. #ifdef CK_SSL
  11307.             }
  11308. #endif /* CK_SSL */
  11309.             if (d <= 0)
  11310.               break;
  11311.         }
  11312.         if (n < 0)
  11313.           fprintf(stderr, "local: %s: %s\n", ftpsnd.local, ck_errstr());
  11314.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  11315.             if (d == -1 && errno && errno != EPIPE)
  11316.               perror("netout");
  11317.             ftpsnd.bytes = -1;
  11318.         }
  11319.         break;
  11320.  
  11321.       case FTT_ASC:                     /* Text mode */
  11322. #ifndef NOCSETS
  11323.         if (ftpsnd.xlate) {             /* With translation */
  11324.             initxlate(ftpsnd.incs,ftpsnd.outcs);
  11325.             while (!cancelfile) {
  11326.                 if ((c0 = xgnbyte(FC_UCS2,ftpsnd.incs,NULL)) < 0) break;
  11327.                 if ((x = xpnbyte(c0,TC_UCS2,ftpsnd.outcs,xxout)) < 0) break;
  11328.             }
  11329.         } else {
  11330. #endif /* NOCSETS */
  11331.             /* Text mode, no translation */
  11332.             while (((c = zminchar()) > -1) && !cancelfile) {
  11333.                 ffc++;
  11334.                 if (c == '\012') {
  11335.                     if (zzout(dout,(CHAR)'\015') < 0)
  11336.                       break;
  11337.                     ftpsnd.bytes++;
  11338.                 }
  11339.                 if (zzout(dout,(CHAR)c) < 0)
  11340.                   break;
  11341.                 ftpsnd.bytes++;
  11342.             }
  11343.             d = 0;
  11344. #ifndef NOCSETS
  11345.         }
  11346. #endif /* NOCSETS */
  11347.         if (dout == -1 || (d = secure_flush(dout)) < 0) {
  11348.             if (d == -1 && errno && errno != EPIPE)
  11349.               perror("netout");
  11350.             ftpsnd.bytes = -1;
  11351.         }
  11352.         break;
  11353.     }
  11354.     tfc += ffc;                         /* Total file chars */
  11355. #ifdef GFTIMER
  11356.     fpfsecs = gftimer();
  11357. #endif /* GFTIMER */
  11358.     zclose(ZIFILE);                     /* Close input file */
  11359. #ifdef PIPESEND
  11360.     if (sndfilter)                      /* Undo this (it's per file) */
  11361.       pipesend = 0;
  11362. #endif /* PIPESEND */
  11363.  
  11364. #ifdef TCPIPLIB
  11365.     socket_close(dout);                 /* Close data connection */
  11366. #else /* TCPIPLIB */
  11367. #ifdef USE_SHUTDOWN
  11368.     shutdown(dout, 1+1);
  11369. #endif /* USE_SHUTDOWN */
  11370.     close(dout);
  11371. #endif /* TCPIPLIB */
  11372.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11373.     signal(SIGINT, ftpsnd.oldintr);            /* Put back interrupts */
  11374. #ifdef SIGPIPE
  11375.     if (ftpsnd.oldintp)
  11376.       signal(SIGPIPE, ftpsnd.oldintp);
  11377. #endif /* SIGPIPE */
  11378.     if (ftpsnd.reply == REPLY_TRANSIENT || ftpsnd.reply == REPLY_ERROR) {
  11379.         debug(F101,"doftpsend2 ftpsnd.reply","",ftpsnd.reply);
  11380.         ftpsndret = -1;
  11381. #ifdef NTSIG
  11382.         ckThreadEnd(threadinfo);
  11383. #endif /* NTSIG */
  11384.         return;
  11385.     } else if (cancelfile) {
  11386.         debug(F101,"doftpsend2 canceled","",ftpsnd.bytes);
  11387.         ftpsndret = -1;
  11388. #ifdef NTSIG
  11389.         ckThreadEnd(threadinfo);
  11390. #endif /* NTSIG */
  11391.         return;
  11392.     }
  11393.     debug(F101,"doftpsend2 ok","",ftpsnd.bytes);
  11394.     ftpsndret = 0;
  11395. #ifdef NTSIG
  11396.      ckThreadEnd(threadinfo);
  11397. #endif /* NTSIG */
  11398. }
  11399.  
  11400. static int
  11401. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  11402.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  11403. {
  11404.     if (!remote) remote = "";           /* Check args */
  11405.     if (!*remote) remote = local;
  11406.     if (!local) local = "";
  11407.     if (!*local) return(-1);
  11408.     if (!cmd) cmd = "";
  11409.     if (!*cmd) cmd = "STOR";
  11410.  
  11411.     debug(F111,"ftp sendrequest restart",local,restart);
  11412.  
  11413.     nout = 0;                           /* Init output buffer count */
  11414.     ftpsnd.bytes = 0;                   /* File input byte count */
  11415.     dout = -1;
  11416.  
  11417. #ifdef FTP_PROXY
  11418.     if (proxy) {
  11419.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  11420.         return(0);
  11421.     }
  11422. #endif /* FTP_PROXY */
  11423.  
  11424.     changetype(ftp_typ,0);              /* Change type for this file */
  11425.  
  11426.     ftpsnd.oldintr = NULL;        /* Set up interrupt handler */
  11427.     ftpsnd.oldintp = NULL;
  11428.     ftpsnd.restart = restart;
  11429.     ftpsnd.xlate = xlate;
  11430.     ftpsnd.lmode = "wb";
  11431.  
  11432. #ifdef PIPESEND                         /* Use Kermit API for file i/o... */
  11433.     if (sndfilter) {
  11434.         char * p = NULL, * q;
  11435. #ifndef NOSPL
  11436.         int n = CKMAXPATH;
  11437.         if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  11438.             q = p;
  11439.             debug(F110,"sendrequest pipesend filter",sndfilter,0);
  11440.             zzstring(sndfilter,&p,&n);
  11441.             debug(F111,"sendrequest pipename",q,n);
  11442.             if (n <= 0) {
  11443.                 printf("?Sorry, send filter + filename too long, %d max.\n",
  11444.                        CKMAXPATH
  11445.                        );
  11446.                 free(q);
  11447.                 return(-1);
  11448.             }
  11449.             ckstrncpy(filnam,q,CKMAXPATH+1);
  11450.             free(q);
  11451.             local = filnam;
  11452.         }
  11453. #endif /* NOSPL */
  11454.     }
  11455.  
  11456.     if (sndfilter)                      /* If sending thru a filter */
  11457.       pipesend = 1;                     /* set this for open and i/o */
  11458. #endif /* PIPESEND */
  11459.     
  11460.     if (openi(local) == 0)              /* Try to open the input file */
  11461.         return(-1);
  11462.  
  11463.     ftpsndret = 0;
  11464.     ftpsnd.incs = incs;
  11465.     ftpsnd.outcs = outcs;
  11466.     ftpsnd.cmd = cmd;
  11467.     ftpsnd.local = local;
  11468.     ftpsnd.remote = remote;
  11469.     ftpsnd.oldintr = signal(SIGINT, cancelsend);
  11470.     havesigint = 0;
  11471.     if (cc_execute(ckjaddr(sendcancel), doftpsend, failftpsend) < 0)
  11472.         return -1;
  11473.     if (ftpsndret < 0)
  11474.         return -1;
  11475.  
  11476.     if (cc_execute(ckjaddr(sendcancel), doftpsend2, failftpsend2) < 0)
  11477.         return -1;
  11478.     return ftpsndret;
  11479. }
  11480.  
  11481. static sigtype
  11482. cancelrecv(sig) int sig; {
  11483.     havesigint++;
  11484.     cancelfile = 0;
  11485.     cancelgroup++;
  11486.     secure_getc(0,1);            /* Initialize net input buffers */
  11487.     printf(" Canceling...\n");
  11488.     debug(F100,"ftp cancelrecv caught SIGINT","",0);
  11489.     fflush(stdout);
  11490.     if (fp_nml) {
  11491.         if (fp_nml != stdout)
  11492.           fclose(fp_nml);
  11493.         fp_nml = NULL;
  11494.     }
  11495. #ifndef OS2
  11496.     longjmp(recvcancel, 1);
  11497. #else
  11498.     PostCtrlCSem();
  11499. #endif /* OS2 */
  11500. }
  11501.  
  11502. /* Argumentless front-end for secure_getc() */
  11503.  
  11504. static int
  11505. netgetc() {
  11506.     return(secure_getc(globaldin,0));
  11507. }
  11508.  
  11509. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  11510.  
  11511. /*
  11512.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  11513.     -1: interrupted by user.
  11514.     -2: error opening or writing output file (reason in errno).
  11515.     -3: failure to make data connection.
  11516.     -4: network read error (reason in errno).
  11517. */
  11518.  
  11519. struct xx_ftprecv {
  11520.     int reply;
  11521.     int fcs;
  11522.     int rcs;
  11523.     int recover;
  11524.     int xlate;
  11525.     int din;
  11526.     int is_retr;
  11527.     sig_t oldintr, oldintp;
  11528.     char * cmd;
  11529.     char * local;
  11530.     char * remote;
  11531.     char * lmode;
  11532.     char * pipename;
  11533.     int    tcrflag;
  11534.     long   localsize;
  11535. };
  11536. static struct xx_ftprecv ftprecv;
  11537.  
  11538. static int ftprecvret = 0;
  11539.  
  11540. static VOID
  11541. #ifdef CK_ANSIC
  11542. failftprecv(VOID * threadinfo)
  11543. #else
  11544. failftprecv(threadinfo) VOID * threadinfo;
  11545. #endif /* CK_ANSIC */
  11546. {
  11547. #ifdef NTSIG
  11548.     if (threadinfo) {                   /* Thread local storage... */
  11549.         TlsSetValue(TlsIndex,threadinfo);
  11550.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11551.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11552. #endif /* NTSIG */
  11553.  
  11554. #ifdef CK_LOGIN
  11555. #ifdef IKSD
  11556. #ifdef NT
  11557.     if (inserver)
  11558.       setntcreds();
  11559. #endif /* NT */
  11560. #endif /* IKSD */
  11561. #endif /* CK_LOGIN */
  11562.  
  11563.     while (cpend) {
  11564.         ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  11565.     }
  11566.     if (data >= 0) {
  11567. #ifdef CK_SSL
  11568.         if (ssl_ftp_data_active_flag) {
  11569.             SSL_shutdown(ssl_ftp_data_con);
  11570.             SSL_free(ssl_ftp_data_con);
  11571.             ssl_ftp_data_active_flag = 0;
  11572.             ssl_ftp_data_con = NULL;
  11573.         }
  11574. #endif /* CK_SSL */
  11575. #ifdef TCPIPLIB
  11576.         socket_close(data);
  11577. #else /* TCPIPLIB */
  11578. #ifdef USE_SHUTDOWN
  11579.         shutdown(data, 1+1);
  11580. #endif /* USE_SHUTDOWN */
  11581.         close(data);
  11582. #endif /* TCPIPLIB */
  11583.         data = -1;
  11584.         globaldin = -1;
  11585.     }
  11586.     if (ftprecv.oldintr)
  11587.       signal(SIGINT, ftprecv.oldintr);
  11588.     ftpcode = -1;
  11589.     ftprecvret = -1;
  11590.  
  11591. #ifndef OS2
  11592.     /* TEST ME IN K95 */
  11593.     if (havesigint) {
  11594.     havesigint = 0;
  11595.     debug(F100,"ftp failftprecv chain to trap()...","",0);
  11596.     if (ftprecv.oldintr != SIG_IGN)
  11597.       (*ftprecv.oldintr)(SIGINT);
  11598.     /* NOTREACHED (I hope!) */
  11599.     debug(F100,"ftp failftprecv return from trap()...","",0);
  11600.     }
  11601. #endif /* OS2 */
  11602.     return;
  11603. }
  11604.  
  11605. static VOID
  11606. #ifdef CK_ANSIC
  11607. doftprecv(VOID * threadinfo)
  11608. #else
  11609. doftprecv(threadinfo) VOID * threadinfo;
  11610. #endif /* CK_ANSIC */
  11611. {
  11612. #ifdef NTSIG
  11613.     if (threadinfo) {                   /* Thread local storage... */
  11614.         TlsSetValue(TlsIndex,threadinfo);
  11615.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11616.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11617. #endif /* NTSIG */
  11618. #ifdef CK_LOGIN
  11619. #ifdef IKSD
  11620. #ifdef NT
  11621.     if (inserver)
  11622.       setntcreds();
  11623. #endif /* NT */
  11624. #endif /* IKSD */
  11625. #endif /* CK_LOGIN */
  11626.  
  11627. #ifndef COMMENT
  11628.     if (!out2screen && !ftprecv.pipename) {
  11629.     int x;
  11630.     char * local;
  11631.     local = ftprecv.local;
  11632.     x = zchko(local);
  11633.         if (x < 0) {
  11634.             if ((!dpyactive || ftp_deb))
  11635.               fprintf(stderr,
  11636.               "Temporary file %s: %s\n", ftprecv.local, ck_errstr());
  11637.             signal(SIGINT, ftprecv.oldintr);
  11638.             ftpcode = -2;
  11639.             ftprecvret = -1;
  11640. #ifdef NTSIG
  11641.             ckThreadEnd(threadinfo);
  11642. #endif /* NTSIG */
  11643.             return;
  11644.         }
  11645.     }
  11646. #endif /* COMMENT */
  11647.     changetype((!ftprecv.is_retr) ? FTT_ASC : ftp_typ, 0);
  11648.     if (initconn()) {                   /* Initialize the data connection */
  11649.         signal(SIGINT, ftprecv.oldintr);
  11650.         ftpcode = -1;
  11651.         ftprecvret = -3;
  11652. #ifdef NTSIG
  11653.         ckThreadEnd(threadinfo);
  11654. #endif /* NTSIG */
  11655.         return;
  11656.     }
  11657.     secure_getc(0,1);            /* Initialize net input buffers */
  11658.     ftprecvret = 0;
  11659.  
  11660. #ifdef NTSIG
  11661.     ckThreadEnd(threadinfo);
  11662. #endif /* NTSIG */
  11663. }
  11664.  
  11665. static VOID
  11666. #ifdef CK_ANSIC
  11667. failftprecv2(VOID * threadinfo)
  11668. #else
  11669. failftprecv2(threadinfo) VOID * threadinfo;
  11670. #endif /* CK_ANSIC */
  11671. {
  11672. #ifdef NTSIG
  11673.     if (threadinfo) {                   /* Thread local storage... */
  11674.         TlsSetValue(TlsIndex,threadinfo);
  11675.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11676.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11677. #endif /* NTSIG */
  11678. #ifdef CK_LOGIN
  11679. #ifdef IKSD
  11680. #ifdef NT
  11681.     if (inserver)
  11682.       setntcreds();
  11683. #endif /* NT */
  11684. #endif /* IKSD */
  11685. #endif /* CK_LOGIN */
  11686.  
  11687.     /* Cancel using RFC959 recommended IP,SYNC sequence  */
  11688.  
  11689.     debug(F100,"ftp recvrequest CANCEL","",0);
  11690. #ifdef GFTIMER
  11691.     fpfsecs = gftimer();
  11692. #endif /* GFTIMER */
  11693. #ifdef SIGPIPE
  11694.     if (ftprecv.oldintp)
  11695.       signal(SIGPIPE, ftprecv.oldintr);
  11696. #endif /* SIGPIPE */
  11697.     signal(SIGINT, SIG_IGN);
  11698.     if (!cpend) {
  11699.         ftpcode = -1;
  11700.         signal(SIGINT, ftprecv.oldintr);
  11701.         ftprecvret = -1;
  11702. #ifdef NTSIG
  11703.         ckThreadEnd(threadinfo);
  11704. #endif /* NTSIG */
  11705.         return;
  11706.     }
  11707.     cancel_remote(ftprecv.din);
  11708.     if (ftpcode > -1)
  11709.       ftpcode = -1;
  11710.     if (data >= 0) {
  11711. #ifdef CK_SSL
  11712.         if (ssl_ftp_data_active_flag) {
  11713.             SSL_shutdown(ssl_ftp_data_con);
  11714.             SSL_free(ssl_ftp_data_con);
  11715.             ssl_ftp_data_active_flag = 0;
  11716.             ssl_ftp_data_con = NULL;
  11717.         }
  11718. #endif /* CK_SSL */
  11719. #ifdef TCPIPLIB
  11720.         socket_close(data);
  11721. #else /* TCPIPLIB */
  11722. #ifdef USE_SHUTDOWN
  11723.         shutdown(data, 1+1);
  11724. #endif /* USE_SHUTDOWN */
  11725.         close(data);
  11726. #endif /* TCPIPLIB */
  11727.         data = -1;
  11728.         globaldin = -1;
  11729.     }
  11730.     if (!out2screen) {
  11731.     int x = 0;
  11732.     debug(F111,"ftp failrecv2 zclose",ftprecv.local,keep);
  11733.     zclose(ZOFILE);
  11734.     switch (keep) {            /* which is... */
  11735.       case SET_AUTO:        /* AUTO */
  11736.         if (curtype == FTT_ASC)    /* Delete file if TYPE A. */
  11737.           x = 1;
  11738.         break;
  11739.       case SET_OFF:            /* DISCARD */
  11740.         x = 1;            /* Delete file, period. */
  11741.         break;
  11742.       default:            /* KEEP */
  11743.         break;
  11744.     }
  11745.     if (x) {
  11746.         x = zdelet(ftprecv.local);
  11747.         debug(F111,"ftp failrecv2 delete incomplete",ftprecv.local,x);
  11748.     }
  11749.     }
  11750.     if (ftprecv.din) {
  11751. #ifdef TCPIPLIB
  11752.         socket_close(ftprecv.din);
  11753. #else /* TCPIPLIB */
  11754. #ifdef USE_SHUTDOWN
  11755.         shutdown(ftprecv.din, 1+1);
  11756. #endif /* USE_SHUTDOWN */
  11757.         close(ftprecv.din);
  11758. #endif /* TCPIPLIB */
  11759.     }
  11760.     signal(SIGINT, ftprecv.oldintr);
  11761.     ftprecvret = -1;
  11762.  
  11763.     if (havesigint) {
  11764.     havesigint = 0;
  11765.     debug(F100,"FTP failftprecv2 chain to trap()...","",0);
  11766. #ifdef OS2
  11767.         debug(F100,"FTP failftprecv2 PostCtrlCSem()...","",0);
  11768.         PostCtrlCSem();
  11769. #else /* OS2 */
  11770.     if (ftprecv.oldintr != SIG_IGN)
  11771.       (*ftprecv.oldintr)(SIGINT);
  11772.     /* NOTREACHED (I hope!) */
  11773.     debug(F100,"ftp failftprecv2 return from trap()...","",0);
  11774. #endif /* OS2 */
  11775.     }
  11776. }
  11777.  
  11778. static VOID
  11779. #ifdef CK_ANSIC
  11780. doftprecv2(VOID * threadinfo)
  11781. #else
  11782. doftprecv2(threadinfo) VOID * threadinfo;
  11783. #endif /* CK_ANSIC */
  11784. {
  11785.     register int c, d;
  11786.     long bytes = 0L;
  11787.     int bare_lfs = 0;
  11788.     int blksize = 0;
  11789.     ULONG start = 0L, stop;
  11790.     char * p;
  11791.     static char * rcvbuf = NULL;
  11792.     static int rcvbufsiz = 0;
  11793. #ifdef CK_URL
  11794.     char newname[CKMAXPATH+1];        /* For file dialog */
  11795. #endif /* CK_URL */
  11796.     extern int adl_ask;
  11797.  
  11798.     ftprecv.din = -1;
  11799. #ifdef NTSIG
  11800.     if (threadinfo) {                   /* Thread local storage... */
  11801.         TlsSetValue(TlsIndex,threadinfo);
  11802.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11803.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11804. #endif /* NTSIG */
  11805. #ifdef CK_LOGIN
  11806. #ifdef IKSD
  11807. #ifdef NT
  11808.     if (inserver)
  11809.       setntcreds();
  11810. #endif /* NT */
  11811. #endif /* IKSD */
  11812. #endif /* CK_LOGIN */
  11813.  
  11814.     if (ftprecv.recover) {                      /* Initiate recovery */
  11815.         x = ftpcmd("REST",ckltoa(ftprecv.localsize),-1,-1,ftp_vbm);
  11816.         debug(F111,"ftp reply","REST",x);
  11817.         if (x == REPLY_CONTINUE) {
  11818.             ftprecv.lmode = "ab";
  11819.             rs_len = ftprecv.localsize;
  11820.         } else {
  11821.             ftprecv.recover = 0;
  11822.         }
  11823.     }
  11824.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  11825.  
  11826.     debug(F111,"ftp recvrequest recover E",ftprecv.remote,ftprecv.recover);
  11827.  
  11828.     /* Send the command and get reply */
  11829.     debug(F110,"ftp recvrequest cmd",ftprecv.cmd,0);
  11830.     debug(F110,"ftp recvrequest remote",ftprecv.remote,0);
  11831.  
  11832.     if (ftpcmd(ftprecv.cmd,ftprecv.remote,ftprecv.fcs,ftprecv.rcs,ftp_vbm)
  11833.     != REPLY_PRELIM) {
  11834.         signal(SIGINT, ftprecv.oldintr); /* Bad reply, fail. */
  11835.         ftprecvret = -1;        /* ftpcode is set by ftpcmd() */
  11836. #ifdef NTSIG
  11837.         ckThreadEnd(threadinfo);
  11838. #endif /* NTSIG */
  11839.         return;
  11840.     }
  11841.     ftprecv.din = dataconn("r");        /* Good reply, open data connection */
  11842.     globaldin = ftprecv.din;            /* Global copy of file descriptor */
  11843.     if (ftprecv.din == -1) {            /* Check for failure */
  11844.         ftpcode = -3;                   /* Code for no data connection */
  11845.         ftprecvret = -1;
  11846. #ifdef NTSIG
  11847.         ckThreadEnd(threadinfo);
  11848. #endif /* NTSIG */
  11849.         return;
  11850.     }
  11851. #ifdef CK_URL
  11852.     /* In K95 GUI put up a file box */
  11853.     if (haveurl && g_url.pth && adl_ask    ) { /* Downloading from a URL */
  11854.     int x;
  11855.     char * preface =
  11856. "\r\nIncoming file from FTP server...\r\n\
  11857. Please confirm output file specification or supply an alternative:";
  11858.  
  11859.     x = uq_file(preface,        /* K95 GUI: Put up file box. */
  11860.             NULL,
  11861.             4,
  11862.             NULL,
  11863.             ftprecv.local ? ftprecv.local : ftprecv.remote,
  11864.             newname,
  11865.             CKMAXPATH+1
  11866.             );
  11867.     if (x > 0) {
  11868.         ftprecv.local = newname;    /* Substitute user's file name */
  11869.         if (x == 2)            /* And append if user said to */
  11870.           ftprecv.lmode = "ab";
  11871.     }
  11872.     }
  11873. #endif /* CK_URL */
  11874.     x = 1;                              /* Output file open OK? */
  11875.     if (ftprecv.pipename) {        /* Command */
  11876.         x = zxcmd(ZOFILE,ftprecv.pipename);
  11877.         debug(F111,"ftp recvrequest zxcmd",ftprecv.pipename,x);
  11878.     } else if (!out2screen) {           /* File */
  11879.         struct filinfo xx;
  11880.         xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  11881.         xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  11882.     /* Append or New */
  11883.         xx.dsp = !strcmp(ftprecv.lmode,"ab") ? XYFZ_A : XYFZ_N;
  11884.         x = zopeno(ZOFILE,ftprecv.local,NULL,&xx);
  11885.         debug(F111,"ftp recvrequest zopeno",ftprecv.local,x);
  11886.     }
  11887.     if (x < 1) {                        /* Failure to open output file */
  11888.         if ((!dpyactive || ftp_deb))
  11889.           fprintf(stderr, "local(2): %s: %s\n", ftprecv.local, ck_errstr());
  11890.         ftprecvret = -1;
  11891. #ifdef NTSIG
  11892.         ckThreadEnd(threadinfo);
  11893. #endif /* NTSIG */
  11894.         return;
  11895.     }
  11896.     blksize = FTP_BUFSIZ;               /* Allocate input buffer */
  11897.  
  11898.     debug(F101,"ftp recvrequest blksize","",blksize);
  11899.     debug(F101,"ftp recvrequest rcvbufsiz","",rcvbufsiz);
  11900.  
  11901.     if (rcvbufsiz < blksize) {          /* if necessary */
  11902.         if (rcvbuf) {
  11903.             free(rcvbuf);
  11904.             rcvbuf = NULL;
  11905.         }
  11906.         rcvbuf = (char *)malloc((unsigned)blksize);
  11907.         if (!rcvbuf) {
  11908.         debug(F100,"ftp get rcvbuf malloc failed","",0);
  11909.             ftpcode = -2;
  11910. #ifdef ENOMEM
  11911.             errno = ENOMEM;
  11912. #endif /* ENOMEM */
  11913.             if ((!dpyactive || ftp_deb))
  11914.               perror("malloc");
  11915.             rcvbufsiz = 0;
  11916.             ftprecvret = -1;
  11917. #ifdef NTSIG
  11918.             ckThreadEnd(threadinfo);
  11919. #endif /* NTSIG */
  11920.             return;
  11921.         }
  11922.     debug(F101,"ftp get rcvbuf malloc ok","",blksize);
  11923.         rcvbufsiz = blksize;
  11924.     }
  11925.     debug(F111,"ftp get rcvbufsiz",ftprecv.local,rcvbufsiz);
  11926.  
  11927.     ffc = 0L;                           /* Character counter */
  11928.     cps = oldcps = 0L;                  /* Thruput */
  11929.     start = gmstimer();                 /* Start time (msecs) */
  11930. #ifdef GFTIMER
  11931.     rftimer();                          /* Start time (float) */
  11932. #endif /* GFTIMER */
  11933.  
  11934.     debug(F111,"ftp get type",ftprecv.local,curtype);
  11935.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  11936.     switch (curtype) {
  11937.       case FTT_BIN:                     /* Binary mode */
  11938.       case FTT_TEN:                     /* TENEX mode */
  11939.         d = 0;
  11940.         while (1) {
  11941.             errno = 0;
  11942.             c = secure_read(ftprecv.din, rcvbuf, rcvbufsiz);
  11943.             if (cancelfile) {
  11944.                 failftprecv2(threadinfo);
  11945. #ifdef NTSIG
  11946.                 ckThreadEnd(threadinfo);
  11947. #endif /* NTSIG */
  11948.                 return;
  11949.             }
  11950.             if (c < 1)
  11951.               break;
  11952. #ifdef printf                           /* (What if it isn't?) */
  11953.             if (out2screen && !ftprecv.pipename) {
  11954.                 int i;
  11955.                 for (i = 0; i < c; i++)
  11956.                   printf("%c",rcvbuf[i]);
  11957.             } else
  11958. #endif /* printf */
  11959.               {
  11960.                 register int i;
  11961.                 i = 0;
  11962.                 errno = 0;
  11963.                 while (i < c) {
  11964.                     if (zmchout(rcvbuf[i++]) < 0) {
  11965.                         d = i;
  11966.                         break;
  11967.                     }
  11968.                 }
  11969.             }
  11970.             bytes += c;
  11971.             ffc += c;
  11972.         }
  11973.         if (c < 0) {
  11974.             debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  11975.             if (c == -1 && errno != EPIPE)
  11976.               if ((!dpyactive || ftp_deb))
  11977.                 perror("netin");
  11978.             bytes = -1;
  11979.             ftpcode = -4;
  11980.         }
  11981.         if (d < c) {
  11982.             ftpcode = -2;
  11983.             if ((!dpyactive || ftp_deb)) {
  11984.                 char * p;
  11985.                 p = ftprecv.local ? ftprecv.local : ftprecv.pipename;
  11986.                 if (d < 0)
  11987.                   fprintf(stderr,
  11988.               "local(3): %s: %s\n", ftprecv.local, ck_errstr());
  11989.                 else
  11990.                   fprintf(stderr,
  11991.               "%s: short write\n", ftprecv.local);
  11992.             }
  11993.         }
  11994.         break;
  11995.  
  11996.       case FTT_ASC:                     /* Text mode */
  11997.     debug(F101,"ftp recvrequest TYPE A xlate","",ftprecv.xlate);
  11998. #ifndef NOCSETS
  11999.         if (ftprecv.xlate) {
  12000.             int t;
  12001. #ifdef CK_ANSIC
  12002.             int (*fn)(char);
  12003. #else
  12004.             int (*fn)();
  12005. #endif /* CK_ANSIC */
  12006.             debug(F110,"ftp recvrequest (data)","initxlate",0);
  12007.             initxlate(ftprecv.rcs,ftprecv.fcs);         /* (From,To) */
  12008.             if (ftprecv.pipename) {
  12009.                 fn = pipeout;
  12010.                 debug(F110,"ftp recvrequest ASCII","pipeout",0);
  12011.             } else {
  12012.                 fn = out2screen ? scrnout : putfil;
  12013.                 debug(F110,"ftp recvrequest ASCII",
  12014.                       out2screen ? "scrnout" : "putfil",0);
  12015.             }
  12016.             while (1) {
  12017.         /* Get byte from net */
  12018.                 c0 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12019.                 if (cancelfile) {
  12020.                     failftprecv2(threadinfo);
  12021. #ifdef NTSIG
  12022.                     ckThreadEnd(threadinfo);
  12023. #endif /* NTSIG */
  12024.                     return;
  12025.                 }
  12026.                 if (c0 < 0)
  12027.                   break;
  12028.         /* Second byte from net */
  12029.                 c1 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12030.                 if (cancelfile) {
  12031.                     failftprecv2(threadinfo);
  12032. #ifdef NTSIG
  12033.                     ckThreadEnd(threadinfo);
  12034. #endif /* NTSIG */
  12035.                     return;
  12036.                 }
  12037.                 if (c1 < 0)
  12038.                   break;
  12039. #ifdef COMMENT
  12040.         /* K95: Check whether we need this */
  12041.         if (fileorder > 0)    /* Little Endian */
  12042.           bytswap(&c0,&c1);    /* swap bytes*/
  12043. #endif /* COMMENT */
  12044.  
  12045. #ifdef OS2
  12046.                 if ( out2screen &&            /* we're translating to UCS-2 */ 
  12047.                      !k95stdout && !inserver) /* for the real screen... */     
  12048.                 {
  12049.                     union {
  12050.                         USHORT ucs2;
  12051.                         UCHAR  bytes[2];
  12052.                     } output;
  12053.  
  12054.                     output.bytes[0] = c1;
  12055.                     output.bytes[1] = c0;
  12056.  
  12057.                     VscrnWrtUCS2StrAtt(VCMD,
  12058.                                        &output.ucs2,
  12059.                                        1,
  12060.                                        wherey[VCMD],
  12061.                                        wherex[VCMD],
  12062.                                        &colorcmd
  12063.                                        );
  12064.  
  12065.                 } else 
  12066. #endif /* OS2 */
  12067.                 {
  12068.                     if ((x = xpnbyte(c0,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12069.                     if ((x = xpnbyte(c1,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12070.                 }
  12071.             }
  12072.         } else {
  12073. #endif /* NOCSETS */
  12074.             while (1) {
  12075.                 c = secure_getc(ftprecv.din,0);
  12076.                 if (cancelfile) {
  12077.                     failftprecv2(threadinfo);
  12078. #ifdef NTSIG
  12079.                     ckThreadEnd(threadinfo);
  12080. #endif /* NTSIG */
  12081.                     return;
  12082.                 }
  12083.                 if (c < 0 || c == EOF)
  12084.                   break;
  12085. #ifdef UNIX
  12086.         /* Record format conversion for Unix */
  12087.         /* SKIP THIS FOR WINDOWS! */
  12088.                 if (c == '\n')
  12089.                   bare_lfs++;
  12090.                 while (c == '\r') {
  12091.                     bytes++;
  12092.                     if ((c = secure_getc(ftprecv.din,0)) != '\n' ||
  12093.             ftprecv.tcrflag) {
  12094.                         if (cancelfile) {
  12095.                             failftprecv2(threadinfo);
  12096. #ifdef NTSIG
  12097.                             ckThreadEnd(threadinfo);
  12098. #endif /* NTSIG */
  12099.                             return;
  12100.                         }
  12101.                         if (c < 0 || c == EOF)
  12102.                           goto break2;
  12103.                         if (c == '\0') {
  12104.                             bytes++;
  12105.                             goto contin2;
  12106.                         }
  12107.                     }
  12108.                 }
  12109.                 if (c < 0)
  12110.                   break;
  12111. #endif /* UNX */
  12112.  
  12113.                 if (out2screen && !ftprecv.pipename)
  12114. #ifdef printf
  12115.                   printf("%c",(char)c);
  12116. #else
  12117.                   putchar((char)c);
  12118. #endif /* printf */
  12119.                 else
  12120.                   if ((d = zmchout(c)) < 0)
  12121.                     break;
  12122.                 bytes++;
  12123.                 ffc++;
  12124.               contin2:
  12125.                 ;
  12126.             }
  12127.           break2:
  12128.             if (bare_lfs && (!dpyactive || ftp_deb)) {
  12129.                 printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  12130.                        bare_lfs);
  12131.                 printf("File might not have transferred correctly.\n");
  12132.             }
  12133.             if (ftprecv.din == -1) {
  12134.                 bytes = -1;
  12135.             }
  12136.             if (c == -2)
  12137.               bytes = -1;
  12138.             break;
  12139. #ifndef NOCSETS
  12140.         }
  12141. #endif /* NOCSETS */
  12142.     }
  12143.     if (ftprecv.pipename || !out2screen) {
  12144.     zclose(ZOFILE);            /* Close the file */
  12145.     debug(F111,"doftprecv2 zclose ftpcode",ftprecv.local,ftpcode);
  12146.     if (ftpcode < 0) {        /* If download failed */
  12147.         int x = 0;
  12148.         switch (keep) {        /* which is... */
  12149.           case SET_AUTO:        /* AUTO */
  12150.         if (curtype == FTT_ASC) /* Delete file if TYPE A. */
  12151.           x = 1;
  12152.         break;
  12153.           case SET_OFF:        /* DISCARD */
  12154.         x = 1;            /* Delete file, period. */
  12155.         break;
  12156.           default:            /* KEEP */
  12157.         break;
  12158.         }
  12159.         if (x) {
  12160.         x = zdelet(ftprecv.local);
  12161.         debug(F111,"ftp get delete incomplete",ftprecv.local,x);
  12162.         }
  12163.     }
  12164.     }
  12165.     signal(SIGINT, ftprecv.oldintr);
  12166. #ifdef SIGPIPE
  12167.     if (ftprecv.oldintp)
  12168.       signal(SIGPIPE, ftprecv.oldintp);
  12169. #endif /* SIGPIPE */
  12170.     stop = gmstimer();
  12171. #ifdef GFTIMER
  12172.     fpfsecs = gftimer();
  12173. #endif /* GFTIMER */
  12174.     tfc += ffc;
  12175.  
  12176. #ifdef TCPIPLIB
  12177.     socket_close(ftprecv.din);
  12178. #else /* TCPIPLIB */
  12179. #ifdef USE_SHUTDOWN
  12180.     shutdown(ftprecv.din, 1+1);
  12181. #endif /* USE_SHUTDOWN */
  12182.     close(ftprecv.din);
  12183. #endif /* TCPIPLIB */
  12184.     ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  12185.     ftprecvret = ((ftpcode < 0 || ftprecv.reply == REPLY_TRANSIENT || 
  12186.                    ftprecv.reply == REPLY_ERROR) ? -1 : 0);
  12187. #ifdef NTSIG
  12188.      ckThreadEnd(threadinfo);
  12189. #endif /* NTSIG */
  12190. }
  12191.  
  12192. static int
  12193. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  12194.             xlate, fcs, rcs)
  12195.     char *cmd, *local, *remote, *lmode, *pipename;
  12196.     int printnames, recover, xlate, fcs, rcs;
  12197. {
  12198. #ifdef NT
  12199.     struct _stat stbuf;
  12200. #else /* NT */
  12201.     struct stat stbuf;
  12202. #endif /* NT */
  12203.  
  12204. #ifdef DEBUG
  12205.     if (deblog) {
  12206.         debug(F111,"ftp recvrequest cmd",cmd,recover);
  12207.         debug(F110,"ftp recvrequest local ",local,0);
  12208.         debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  12209.         debug(F110,"ftp recvrequest pipename ",pipename,0);
  12210.         debug(F101,"ftp recvrequest xlate","",xlate);
  12211.         debug(F101,"ftp recvrequest fcs","",fcs);
  12212.         debug(F101,"ftp recvrequest rcs","",rcs);
  12213.     }
  12214. #endif /* DEBUG */
  12215.  
  12216.     ftprecv.localsize = 0L;
  12217.  
  12218.     if (remfile) {                      /* See remcfm(), remtxt() */
  12219.         if (rempipe) {
  12220.             pipename = remdest;
  12221.         } else {
  12222.             local = remdest;
  12223.             if (remappd) lmode = "ab";
  12224.         }
  12225.     }
  12226.     out2screen = 0;
  12227.     if (!cmd) cmd = "";                 /* Core dump prevention */
  12228.     if (!remote) remote = "";
  12229.     if (!lmode) lmode = "";
  12230.  
  12231.     if (pipename) {                     /* No recovery for pipes. */
  12232.         recover = 0;
  12233.         if (!local)
  12234.           local = pipename;
  12235.     } else {
  12236.         if (!local)                     /* Output to screen? */
  12237.           local = "-";
  12238.         out2screen = !strcmp(local,"-");
  12239.     }
  12240.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  12241.  
  12242. #ifdef OS2
  12243.     if ( ftp_xla && out2screen && !k95stdout && !inserver )
  12244.         fcs = FC_UCS2;
  12245. #endif /* OS2 */
  12246.  
  12247.     if (out2screen)                     /* No recovery to screen */
  12248.       recover = 0;
  12249.     if (!ftp_typ)                       /* No recovery in text mode */
  12250.       recover = 0;
  12251.     ftprecv.is_retr = (strcmp(cmd, "RETR") == 0);
  12252.  
  12253.     if (!ftprecv.is_retr)               /* No recovery except for RETRieve */
  12254.       recover = 0;
  12255.  
  12256.     ftprecv.localsize = 0L;        /* Local file size */
  12257.     rs_len = 0L;                        /* Recovery point */
  12258.  
  12259.     debug(F101,"ftp recvrequest recover","",recover);
  12260.     if (recover) {                      /* Recovering... */
  12261.         if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
  12262.         debug(F101,"ftp recvrequest recover stat failed","",errno);
  12263.             recover = 0;                /* So cancel recovery */
  12264.         } else {                        /* Have local file info */
  12265.             ftprecv.localsize = stbuf.st_size;  /* Get size */
  12266.         /* Remote file smaller than local */
  12267.             if (fsize < ftprecv.localsize) {
  12268.         debug(F101,"ftp recvrequest recover remote smaller","",fsize);
  12269.                 recover = 0;            /* Recovery can't work */
  12270.             } else if (fsize == ftprecv.localsize) { /* Sizes are equal */
  12271.                 debug(F111,"ftp recvrequest recover equal size",
  12272.               remote,ftprecv.localsize);
  12273.                 return(1);
  12274.             }
  12275. #ifdef COMMENT
  12276. /*
  12277.   The problem here is that the original partial file never got its date
  12278.   set, either because FTP DATES was OFF, or because the partial file was
  12279.   downloaded by some other program that doesn't set local file dates, or
  12280.   because Kermit only sets the file's date when the download was complete
  12281.   and successful.  In all these cases, the local file has a later time
  12282.   than the remote.
  12283. */
  12284.             if (recover) {              /* Remote is bigger */
  12285.                 x = chkmodtime(local,remote,0); /* Check file dates */
  12286.                 debug(F111,"ftp recvrequest chkmodtime",remote,x);
  12287.                 if (x != 1)        /* Dates must be equal! */
  12288.                   recover = 0;          /* If not, get whole file */
  12289.             }
  12290. #endif /* COMMENT */
  12291.         }
  12292.         debug(F111,"ftp recvrequest recover",remote,recover);
  12293.     }
  12294.  
  12295. #ifdef FTP_PROXY
  12296.     if (proxy && ftprecv.is_retr)
  12297.       return(proxtrans(cmd, local ? local : remote, remote));
  12298. #endif /* FTP_PROXY */
  12299.  
  12300.     ftprecv.tcrflag = (feol != CR) && ftprecv.is_retr;
  12301.  
  12302.     ftprecv.reply = 0;
  12303.     ftprecv.fcs = fcs;
  12304.     ftprecv.rcs = rcs;
  12305.     ftprecv.recover = recover;
  12306.     ftprecv.xlate = xlate;
  12307.     ftprecv.cmd = cmd;
  12308.     ftprecv.local = local;
  12309.     ftprecv.remote = remote;
  12310.     ftprecv.lmode = lmode;
  12311.     ftprecv.pipename = pipename;
  12312.     ftprecv.oldintp = NULL;
  12313.     ftpcode = 0;
  12314.  
  12315.     havesigint = 0;
  12316.     ftprecv.oldintr = signal(SIGINT, cancelrecv);
  12317.     if (cc_execute(ckjaddr(recvcancel), doftprecv, failftprecv) < 0)
  12318.       return -1;
  12319.     if (ftprecvret < 0)
  12320.       return -1;
  12321.  
  12322.     if (cc_execute(ckjaddr(recvcancel), doftprecv2, failftprecv2) < 0)
  12323.       return -1;
  12324.     return ftprecvret;
  12325. }
  12326.  
  12327. /*
  12328.  * Need to start a listen on the data channel before we send the command,
  12329.  * otherwise the server's connect may fail.
  12330.  */
  12331. static int
  12332. initconn() {
  12333.     register char *p, *a;
  12334.     int result, tmpno = 0;
  12335.     int on = 1;
  12336.     GSOCKNAME_T len;
  12337.  
  12338. #ifndef NO_PASSIVE_MODE
  12339.     int a1,a2,a3,a4,p1,p2;
  12340.  
  12341.     if (passivemode) {
  12342.         data = socket(AF_INET, SOCK_STREAM, 0);
  12343.         globaldin = data;
  12344.         if (data < 0) {
  12345.             perror("ftp: socket");
  12346.             return(-1);
  12347.         }
  12348.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  12349.             printf("Passive mode refused\n");
  12350.             passivemode = 0;
  12351.             return(initconn());
  12352.         }
  12353. /*
  12354.   Now we have a string of comma-separated one-byte unsigned integer values,
  12355.   The first four are the an IP address.  The fifth is the MSB of the port
  12356.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  12357. */
  12358.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  12359.             printf("Passive mode address scan failure\n");
  12360.             return(-1);
  12361.         };
  12362. #ifndef NOHTTP
  12363.         if (tcp_http_proxy) {
  12364. #ifdef OS2
  12365.             char * agent = "Kermit 95"; /* Default user agent */
  12366. #else
  12367.             char * agent = "C-Kermit";
  12368. #endif /* OS2 */
  12369.             register struct hostent *hp = 0;
  12370.             struct servent *destsp;
  12371.             char host[512], *p, *q;
  12372. #ifdef IP_TOS
  12373. #ifdef IPTOS_THROUGHPUT
  12374.             int tos;
  12375. #endif /* IPTOS_THROUGHPUT */
  12376. #endif /* IP_TOS */
  12377.             int s;
  12378. #ifdef DEBUG
  12379.             extern int debtim;
  12380.             int xdebtim;
  12381.             xdebtim = debtim;
  12382.             debtim = 1;
  12383. #endif /* DEBUG */
  12384.  
  12385.             ckmakxmsg(proxyhost,sizeof(proxyhost),ckuitoa(a1),".",ckuitoa(a2),
  12386.                       ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  12387.                       NULL,NULL,NULL
  12388.                       );
  12389.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  12390.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  12391.               *q = *p;
  12392.             *q = '\0';
  12393.  
  12394.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  12395.             if (hisctladdr.sin_addr.s_addr != -1) {
  12396.                 debug(F110,"initconn A",host,0);
  12397.                 hisctladdr.sin_family = AF_INET;
  12398.             } else {
  12399.                 debug(F110,"initconn B",host,0);
  12400.                 hp = gethostbyname(host);
  12401. #ifdef HADDRLIST
  12402.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  12403. #endif /* HADDRLIST */
  12404.                 if (hp == NULL) {
  12405.                     fprintf(stderr, "ftp: %s: Unknown host\n", host);
  12406.                     ftpcode = -1;
  12407. #ifdef DEBUG
  12408.                     debtim = xdebtim;
  12409. #endif /* DEBUG */
  12410.                     return(0);
  12411.                 }
  12412.                 hisctladdr.sin_family = hp->h_addrtype;
  12413. #ifdef HADDRLIST
  12414.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  12415.                        sizeof(hisctladdr.sin_addr));
  12416. #else /* HADDRLIST */
  12417.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  12418.                        sizeof(hisctladdr.sin_addr));
  12419. #endif /* HADDRLIST */
  12420.             }
  12421.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12422.             debug(F101,"initconn socket","",data);
  12423.             if (data < 0) {
  12424.                 perror("ftp: socket");
  12425.                 ftpcode = -1;
  12426. #ifdef DEBUG
  12427.                 debtim = xdebtim;
  12428. #endif /* DEBUG */
  12429.                 return(0);
  12430.             }
  12431.             if (*p == ':')
  12432.               p++;
  12433.             else
  12434.               p = "http";
  12435.  
  12436.             destsp = getservbyname(p,"tcp");
  12437.             if (destsp)
  12438.               hisctladdr.sin_port = destsp->s_port;
  12439.             else if (p)
  12440.               hisctladdr.sin_port = htons(atoi(p));
  12441.             else
  12442.               hisctladdr.sin_port = htons(80);
  12443.             errno = 0;
  12444. #ifdef HADDRLIST
  12445.             debug(F100,"initconn HADDRLIST","",0);
  12446.             while
  12447. #else
  12448.             debug(F100,"initconn no HADDRLIST","",0);
  12449.             if
  12450. #endif /* HADDRLIST */
  12451.               (connect(data, (struct sockaddr *)&hisctladdr,
  12452.                        sizeof (hisctladdr)) < 0) {
  12453.                   debug(F101,"initconn connect failed","",errno);
  12454. #ifdef HADDRLIST
  12455.                   if (hp && hp->h_addr_list[1]) {
  12456.                       int oerrno = errno;
  12457.  
  12458.                       fprintf(stderr,
  12459.                               "ftp: connect to address %s: ",
  12460.                               inet_ntoa(hisctladdr.sin_addr)
  12461.                               );
  12462.                       errno = oerrno;
  12463.                       perror((char *)0);
  12464.                       hp->h_addr_list++;
  12465.                       memcpy((char *)&hisctladdr.sin_addr,
  12466.                              hp->h_addr_list[0],
  12467.                              sizeof(hisctladdr.sin_addr));
  12468.                       fprintf(stdout, "Trying %s...\n",
  12469.                               inet_ntoa(hisctladdr.sin_addr));
  12470. #ifdef TCPIPLIB
  12471.                       socket_close(data);
  12472. #else /* TCPIPLIB */
  12473.                       close(data);
  12474. #endif /* TCPIPLIB */
  12475.                       data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12476.                       if (data < 0) {
  12477.                           perror("ftp: socket");
  12478.                           ftpcode = -1;
  12479. #ifdef DEBUG
  12480.                           debtim = xdebtim;
  12481. #endif /* DEBUG */
  12482.                           return(0);
  12483.                       }
  12484.                       continue;
  12485.                   }
  12486. #endif /* HADDRLIST */
  12487.                   perror("ftp: connect");
  12488.                   ftpcode = -1;
  12489.                   goto bad;
  12490.               }
  12491.             if (http_connect(data,agent,NULL,
  12492.                              tcp_http_proxy_user,
  12493.                              tcp_http_proxy_pwd,
  12494.                              0,
  12495.                              proxyhost
  12496.                              ) < 0) {
  12497. #ifdef TCPIPLIB
  12498.                 socket_close(data);
  12499. #else /* TCPIPLIB */
  12500.                 close(data);
  12501. #endif /* TCPIPLIB */
  12502.                 perror("ftp: connect");
  12503.                 ftpcode = -1;
  12504.                 goto bad;
  12505.             }
  12506.         } else
  12507. #endif /* NOHTTP */
  12508.         {
  12509.             data_addr.sin_family = AF_INET;
  12510.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  12511.             data_addr.sin_port = htons((p1<<8)|p2);
  12512.  
  12513.             if (connect(data,
  12514.                         (struct sockaddr *)&data_addr,
  12515.                         sizeof(data_addr)) < 0
  12516.                 ) {
  12517.                 perror("ftp: connect");
  12518.                 return(-1);
  12519.             }
  12520.         }
  12521.         debug(F100,"initconn connect ok","",0);
  12522. #ifdef IP_TOS
  12523. #ifdef IPTOS_THROUGHPUT
  12524.         on = IPTOS_THROUGHPUT;
  12525.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12526.           perror("ftp: setsockopt TOS (ignored)");
  12527. #endif /* IPTOS_THROUGHPUT */
  12528. #endif /* IP_TOS */
  12529.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  12530.         return(0);
  12531.     }
  12532. #endif /* NO_PASSIVE_MODE */
  12533.  
  12534.   noport:
  12535.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  12536.     if (sendport)
  12537.       data_addr.sin_port = 0;   /* let system pick one */
  12538.     if (data != -1) {
  12539. #ifdef TCPIPLIB
  12540.         socket_close(data);
  12541. #else /* TCPIPLIB */
  12542. #ifdef USE_SHUTDOWN
  12543.         shutdown(data, 1+1);
  12544. #endif /* USE_SHUTDOWN */
  12545.         close(data);
  12546. #endif /* TCPIPLIB */
  12547.     }
  12548.     data = socket(AF_INET, SOCK_STREAM, 0);
  12549.     globaldin = data;
  12550.     if (data < 0) {
  12551.         perror("ftp: socket");
  12552.         if (tmpno)
  12553.           sendport = 1;
  12554.         return(-1);
  12555.     }
  12556.     if (!sendport) {
  12557.         if (setsockopt(data,
  12558.                        SOL_SOCKET,
  12559.                        SO_REUSEADDR,
  12560.                        (char *)&on,
  12561.                        sizeof (on)
  12562.                        ) < 0
  12563.             ) {
  12564.             perror("ftp: setsockopt (reuse address)");
  12565.             goto bad;
  12566.         }
  12567.     }
  12568.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  12569.         perror("ftp: bind");
  12570.         goto bad;
  12571.     }
  12572.     len = sizeof (data_addr);
  12573.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  12574.         perror("ftp: getsockname");
  12575.         goto bad;
  12576.     }
  12577.     if (listen(data, 1) < 0) {
  12578.         perror("ftp: listen");
  12579.         goto bad;
  12580.     }
  12581.     if (sendport) {
  12582.         a = (char *)&data_addr.sin_addr;
  12583.         p = (char *)&data_addr.sin_port;
  12584.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  12585.                   UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  12586.                   UC(p[0]),",", UC(p[1]));
  12587.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  12588.         if (result == REPLY_ERROR && sendport) {
  12589.             sendport = 0;
  12590.             tmpno = 1;
  12591.             goto noport;
  12592.         }
  12593.         return(result != REPLY_COMPLETE);
  12594.     }
  12595.     if (tmpno)
  12596.       sendport = 1;
  12597. #ifdef IP_TOS
  12598. #ifdef IPTOS_THROUGHPUT
  12599.     on = IPTOS_THROUGHPUT;
  12600.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12601.       perror("ftp: setsockopt TOS (ignored)");
  12602. #endif
  12603. #endif
  12604.     return(0);
  12605.   bad:
  12606. #ifdef TCPIPLIB
  12607.     socket_close(data);
  12608. #else /* TCPIPLIB */
  12609. #ifdef USE_SHUTDOWN
  12610.     shutdown(data, 1+1);
  12611. #endif /* USE_SHUTDOWN */
  12612.     close(data);
  12613. #endif /* TCPIPLIB */
  12614.     data = -1;
  12615.     globaldin = data;
  12616.     if (tmpno)
  12617.       sendport = 1;
  12618.     return(-1);
  12619. }
  12620.  
  12621. #ifdef CK_SSL
  12622. static int
  12623. ssl_dataconn() {
  12624.     if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
  12625.         SSL_free(ssl_ftp_data_con);
  12626.         ssl_ftp_data_con=NULL;
  12627.     }
  12628.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  12629.  
  12630.     SSL_set_fd(ssl_ftp_data_con,data);
  12631.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  12632.  
  12633.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  12634.  
  12635.     if (ssl_debug_flag) {
  12636.         fprintf(stderr,"=>START SSL connect on DATA\n");
  12637.         fflush(stderr);
  12638.     }
  12639.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  12640.         static char errbuf[1024];
  12641.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  12642.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  12643.         fprintf(stderr,"%s\n", errbuf);
  12644.         fflush(stderr);
  12645. #ifdef TCPIPLIB
  12646.         socket_close(data);
  12647. #else /* TCPIPLIB */
  12648. #ifdef USE_SHUTDOWN
  12649.         shutdown(data, 1+1);
  12650. #endif /* USE_SHUTDOWN */
  12651.         close(data);
  12652. #endif /* TCPIPLIB */
  12653.         data = -1;
  12654.         globaldin = data;
  12655.         return(-1);
  12656.     } else {
  12657.         ssl_ftp_data_active_flag=1;
  12658.  
  12659.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  12660.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  12661.  
  12662.             if (!subject) {
  12663.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  12664.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  12665.  
  12666.                     ssl_ftp_data_active_flag = 0;
  12667. #ifdef TCPIPLIB
  12668.                     socket_close(data);
  12669. #else /* TCPIPLIB */
  12670. #ifdef USE_SHUTDOWN
  12671.                     shutdown(data, 1+1);
  12672. #endif /* USE_SHUTDOWN */
  12673.                     close(data);
  12674. #endif /* TCPIPLIB */
  12675.                     data = -1;
  12676.                     globaldin = data;
  12677.                     return(-1);
  12678.                 } else {
  12679.                     if (!out2screen && displa && fdispla) {
  12680.                         ftscreen(SCR_TC,0,0L,"Display canceled");
  12681.                         /* fdispla = XYFD_B; */
  12682.                     }
  12683.  
  12684.                     if (uq_ok(
  12685.           "Warning: Server didn't provide a certificate on data connection\n",
  12686.                                "Continue with file transfer? (Y/N)",
  12687.                               3,NULL,0) <= 0) {
  12688.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  12689.                         ssl_ftp_data_active_flag = 0;
  12690. #ifdef TCPIPLIB
  12691.                         socket_close(data);
  12692. #else /* TCPIPLIB */
  12693. #ifdef USE_SHUTDOWN
  12694.                         shutdown(data, 1+1);
  12695. #endif /* USE_SHUTDOWN */
  12696.                         close(data);
  12697. #endif /* TCPIPLIB */
  12698.                         data = -1;
  12699.                         globaldin = data;
  12700.                         return(-1);
  12701.                     }
  12702.                 }
  12703.             } else {
  12704.                 if (!out2screen && displa && fdispla == XYFD_C) {
  12705.                     ftscreen(SCR_TC,0,0L,"Display canceled");
  12706.                     /* fdispla = XYFD_B; */
  12707.                 }
  12708.  
  12709.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_user_host)) {
  12710.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  12711.                     ssl_ftp_data_active_flag = 0;
  12712. #ifdef TCPIPLIB
  12713.                     socket_close(data);
  12714. #else /* TCPIPLIB */
  12715. #ifdef USE_SHUTDOWN
  12716.                     shutdown(data, 1+1);
  12717. #endif /* USE_SHUTDOWN */
  12718.                     close(data);
  12719. #endif /* TCPIPLIB */
  12720.                     data = -1;
  12721.                     globaldin = data;
  12722.                     return(-1);
  12723.                 }
  12724.             }
  12725.         }
  12726.         debug(F110,"dataconn","[SSL - OK]",0);
  12727. #ifdef COMMENT
  12728.         /* This messes up the full screen file transfer display */
  12729.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  12730. #endif /* COMMENT */
  12731.     }
  12732.     if (ssl_debug_flag) {
  12733.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  12734.         fflush(stderr);
  12735.     }
  12736.     return(data);
  12737. }
  12738. #endif /* CK_SSL */
  12739.  
  12740. static int
  12741. dataconn(lmode) char *lmode; {
  12742.     int s;
  12743. #ifdef IP_TOS
  12744.     int tos;
  12745. #endif /* IP_TOS */
  12746. #ifdef UCX50
  12747.     static u_int fromlen;
  12748. #else
  12749.     static SOCKOPT_T fromlen;
  12750. #endif /* UCX50 */
  12751.  
  12752.     fromlen = sizeof(hisdataaddr);
  12753.  
  12754. #ifndef NO_PASSIVE_MODE
  12755.     if (passivemode) {
  12756. #ifdef CK_SSL
  12757.         ssl_ftp_data_active_flag=0;
  12758.         if (ssl_ftp_active_flag &&
  12759.             (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  12760.           return(ssl_dataconn());
  12761. #endif /* CK_SSL */
  12762.         return(data);
  12763.     }
  12764. #endif /* NO_PASSIVE_MODE */
  12765.  
  12766.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  12767.     if (s < 0) {
  12768.         perror("ftp: accept");
  12769. #ifdef TCPIPLIB
  12770.         socket_close(data);
  12771. #else /* TCPIPLIB */
  12772. #ifdef USE_SHUTDOWN
  12773.         shutdown(data, 1+1);
  12774. #endif /* USE_SHUTDOWN */
  12775.         close(data);
  12776. #endif /* TCPIPLIB */
  12777.         data = -1;
  12778.         globaldin = data;
  12779.         return(-1);
  12780.     }
  12781. #ifdef TCPIPLIB
  12782.     socket_close(data);
  12783. #else /* TCPIPLIB */
  12784. #ifdef USE_SHUTDOWN
  12785.     shutdown(data, 1+1);
  12786. #endif /* USE_SHUTDOWN */
  12787.     close(data);
  12788. #endif /* TCPIPLIB */
  12789.     data = s;
  12790.     globaldin = data;
  12791. #ifdef IP_TOS
  12792. #ifdef IPTOS_THROUGHPUT
  12793.     tos = IPTOS_THROUGHPUT;
  12794.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  12795.       perror("ftp: setsockopt TOS (ignored)");
  12796. #endif /* IPTOS_THROUGHPUT */
  12797. #endif /* IP_TOS */
  12798.  
  12799. #ifdef CK_SSL
  12800.     ssl_ftp_data_active_flag=0;
  12801.     if (ssl_ftp_active_flag &&
  12802.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  12803.       return(ssl_dataconn());
  12804. #endif /* CK_SSL */
  12805.     return(data);
  12806. }
  12807.  
  12808. #ifdef FTP_PROXY
  12809. static sigtype
  12810. pscancel(sig) int sig; {
  12811.     cancelfile++;
  12812. }
  12813.  
  12814. static VOID
  12815. pswitch(flag) int flag; {
  12816.     extern int proxy;
  12817.     sig_t oldintr;
  12818.     static struct comvars {
  12819.         int connect;
  12820.         char name[MAXHOSTNAMELEN];
  12821.         struct sockaddr_in mctl;
  12822.         struct sockaddr_in hctl;
  12823.         FILE *in;
  12824.         FILE *out;
  12825.         int tpe;
  12826.         int curtpe;
  12827.         int cpnd;
  12828.         int sunqe;
  12829.         int runqe;
  12830.         int mcse;
  12831.         int ntflg;
  12832.         char nti[17];
  12833.         char nto[17];
  12834.         int mapflg;
  12835.         char mi[CKMAXPATH];
  12836.         char mo[CKMAXPATH];
  12837.         char *authtype;
  12838.         int clvl;
  12839.         int dlvl;
  12840. #ifdef FTP_KRB4
  12841.         des_cblock session;
  12842.         des_key_schedule ftp_sched;
  12843. #endif /* FTP_KRB4 */
  12844. #ifdef FTP_GSSAPI
  12845.         gss_ctx_id_t gcontext;
  12846. #endif /* GSSAPI */
  12847.     } proxstruct, tmpstruct;
  12848.     struct comvars *ip, *op;
  12849.  
  12850.     cancelfile = 0;
  12851.     oldintr = signal(SIGINT, pscancel);
  12852.     if (flag) {
  12853.         if (proxy)
  12854.           return;
  12855.         ip = &tmpstruct;
  12856.         op = &proxstruct;
  12857.         proxy++;
  12858.     } else {
  12859.         if (!proxy)
  12860.           return;
  12861.         ip = &proxstruct;
  12862.         op = &tmpstruct;
  12863.         proxy = 0;
  12864.     }
  12865.     ip->connect = connected;
  12866.     connected = op->connect;
  12867.     if (ftp_host) {
  12868.         strncpy(ip->name, ftp_host, sizeof(ip->name) - 1);
  12869.         ip->name[strlen(ip->name)] = '\0';
  12870.     } else
  12871.       ip->name[0] = 0;
  12872.     ftp_host = op->name;
  12873.     ip->hctl = hisctladdr;
  12874.     hisctladdr = op->hctl;
  12875.     ip->mctl = myctladdr;
  12876.     myctladdr = op->mctl;
  12877.     ip->in = csocket;
  12878.     csocket = op->in;
  12879.     ip->out = csocket;
  12880.     csocket = op->out;
  12881.     ip->tpe = ftp_typ;
  12882.     ftp_typ = op->tpe;
  12883.     ip->curtpe = curtype;
  12884.     curtype = op->curtpe;
  12885.     ip->cpnd = cpend;
  12886.     cpend = op->cpnd;
  12887.     ip->sunqe = ftp_usn;
  12888.     ftp_usn = op->sunqe;
  12889.     ip->mcse = mcase;
  12890.     mcase = op->mcse;
  12891.     ip->ntflg = ntflag;
  12892.     ntflag = op->ntflg;
  12893.     strncpy(ip->nti, ntin, 16);
  12894.     (ip->nti)[strlen(ip->nti)] = '\0';
  12895.     strcpy(ntin, op->nti);
  12896.     strncpy(ip->nto, ntout, 16);
  12897.     (ip->nto)[strlen(ip->nto)] = '\0';
  12898.     strcpy(ntout, op->nto);
  12899.     ip->mapflg = mapflag;
  12900.     mapflag = op->mapflg;
  12901.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  12902.     (ip->mi)[strlen(ip->mi)] = '\0';
  12903.     strcpy(mapin, op->mi);
  12904.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  12905.     (ip->mo)[strlen(ip->mo)] = '\0';
  12906.     strcpy(mapout, op->mo);
  12907.     ip->authtype = auth_type;
  12908.     auth_type = op->authtype;
  12909.     ip->clvl = ftp_cpl;
  12910.     ftp_cpl = op->clvl;
  12911.     ip->dlvl = ftp_dpl;
  12912.     ftp_dpl = op->dlvl;
  12913.     if (!ftp_cpl)
  12914.       ftp_cpl = FPL_CLR;
  12915.     if (!ftp_dpl)
  12916.       ftp_dpl = FPL_CLR;
  12917. #ifdef FTP_KRB4
  12918.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  12919.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  12920.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  12921.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  12922. #endif /* FTP_KRB4 */
  12923. #ifdef FTP_GSSAPI
  12924.     ip->gcontext = gcontext;
  12925.     gcontext = op->gcontext;
  12926. #endif /* GSSAPI */
  12927.     signal(SIGINT, oldintr);
  12928.     if (cancelfile) {
  12929.         cancelfile = 0;
  12930.         debug(F101,"pswitch cancelfile B","",cancelfile);
  12931.         (*oldintr)(SIGINT);
  12932.     }
  12933. }
  12934.  
  12935. static sigtype
  12936. cancelpt(sig) int sig; {
  12937.     printf("\n");
  12938.     fflush(stdout);
  12939.     ptabflg++;
  12940.     cancelfile = 0;
  12941. #ifndef OS2
  12942.     longjmp(ptcancel, 1);
  12943. #else
  12944.     PostCtrlCSem();
  12945. #endif /* OS2 */
  12946. }
  12947.  
  12948. void
  12949. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  12950.     sig_t oldintr;
  12951.     int secndflag = 0, prox_type, nfnd;
  12952.     char *cmd2;
  12953. #ifdef BSDSELECT
  12954.     fd_set mask;
  12955. #endif /* BSDSELECT */
  12956.     sigtype cancelpt();
  12957.  
  12958.     if (strcmp(cmd, "RETR"))
  12959.       cmd2 = "RETR";
  12960.     else
  12961.       cmd2 = unique ? "STOU" : "STOR";
  12962.     if ((prox_type = type) == 0) {
  12963.         if (servertype == SYS_UNIX && unix_proxy)
  12964.           prox_type = FTT_BIN;
  12965.         else
  12966.           prox_type = FTT_ASC;
  12967.     }
  12968.     if (curtype != prox_type)
  12969.       changetype(prox_type, 1);
  12970.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  12971.         printf("Proxy server does not support third party transfers.\n");
  12972.         return;
  12973.     }
  12974.     pswitch(0);
  12975.     if (!connected) {
  12976.         printf("No primary connection\n");
  12977.         pswitch(1);
  12978.         ftpcode = -1;
  12979.         return;
  12980.     }
  12981.     if (curtype != prox_type)
  12982.       changetype(prox_type, 1);
  12983.  
  12984.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  12985.         pswitch(1);
  12986.         return;
  12987.     }
  12988.  
  12989.     /* Replace with calls to cc_execute() */
  12990.     if (setjmp(ptcancel))
  12991.       goto cancel;
  12992.     oldintr = signal(SIGINT, cancelpt);
  12993.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  12994.         signal(SIGINT, oldintr);
  12995.         pswitch(1);
  12996.         return;
  12997.     }
  12998.     sleep(2000);
  12999.     pswitch(1);
  13000.     secndflag++;
  13001.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  13002.       goto cancel;
  13003.     ptflag++;
  13004.     getreply(0,-1,-1,ftp_vbm,0);
  13005.     pswitch(0);
  13006.     getreply(0,-1,-1,ftp_vbm,0);
  13007.     signal(SIGINT, oldintr);
  13008.     pswitch(1);
  13009.     ptflag = 0;
  13010.     return;
  13011.  
  13012.   cancel:
  13013.     signal(SIGINT, SIG_IGN);
  13014.     ptflag = 0;
  13015.     if (strcmp(cmd, "RETR") && !proxy)
  13016.       pswitch(1);
  13017.     else if (!strcmp(cmd, "RETR") && proxy)
  13018.       pswitch(0);
  13019.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  13020.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13021.             pswitch(0);
  13022.             if (cpend)
  13023.               cancel_remote(0);
  13024.         }
  13025.         pswitch(1);
  13026.         if (ptabflg)
  13027.           ftpcode = -1;
  13028.         signal(SIGINT, oldintr);
  13029.         return;
  13030.     }
  13031.     if (cpend)
  13032.       cancel_remote(0);
  13033.     pswitch(!proxy);
  13034.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  13035.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13036.             pswitch(0);
  13037.             if (cpend)
  13038.               cancel_remote(0);
  13039.             pswitch(1);
  13040.             if (ptabflg)
  13041.               ftpcode = -1;
  13042.             signal(SIGINT, oldintr);
  13043.             return;
  13044.         }
  13045.     }
  13046.     if (cpend)
  13047.       cancel_remote(0);
  13048.     pswitch(!proxy);
  13049.     if (cpend) {
  13050. #ifdef BSDSELECT
  13051.         FD_ZERO(&mask);
  13052.         FD_SET(csocket, &mask);
  13053.         if ((nfnd = empty(&mask, 10)) <= 0) {
  13054.             if (nfnd < 0) {
  13055.                 perror("cancel");
  13056.             }
  13057.             if (ptabflg)
  13058.               ftpcode = -1;
  13059.             lostpeer();
  13060.         }
  13061. #else /* BSDSELECT */
  13062. #ifdef IBMSELECT
  13063.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  13064.             if (nfnd < 0) {
  13065.                 perror("cancel");
  13066.             }
  13067.             if (ptabflg)
  13068.               ftpcode = -1;
  13069.             lostpeer();
  13070.         }
  13071. #endif /* IBMSELECT */
  13072. #endif /* BSDSELECT */
  13073.         getreply(0,-1,-1,ftp_vbm,0);
  13074.         getreply(0,-1,-1,ftp_vbm,0);
  13075.     }
  13076.     if (proxy)
  13077.       pswitch(0);
  13078.     pswitch(1);
  13079.     if (ptabflg)
  13080.       ftpcode = -1;
  13081.     signal(SIGINT, oldintr);
  13082. }
  13083. #endif /* FTP_PROXY */
  13084.  
  13085. #ifdef FTP_SECURITY
  13086. #ifdef FTP_GSSAPI
  13087.  
  13088. struct {
  13089.     CONST gss_OID_desc * CONST * mech_type;
  13090.     char *service_name;
  13091. } gss_trials[] = {
  13092.     { &gss_mech_krb5, "ftp" },
  13093.     { &gss_mech_krb5, "host" },
  13094. };
  13095.  
  13096. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  13097. #endif /* FTP_GSSAPI */
  13098.  
  13099. static int
  13100. ftp_auth() {
  13101.     extern int setsafe();
  13102.     int j = 0, n;
  13103. #ifdef FTP_KRB4
  13104.     char *service, inst[INST_SZ];
  13105.     ULONG cksum;
  13106.     ULONG checksum = (ULONG) getpid();
  13107.     CHAR out_buf[FTP_BUFSIZ];
  13108.     int i;
  13109. #else /* FTP_KRB4 */
  13110. #ifdef FTP_GSSAPI
  13111.     CHAR out_buf[FTP_BUFSIZ];
  13112.     int i;
  13113. #endif /* FTP_GSSAPI */
  13114. #endif /* FTP_KRB4 */
  13115.  
  13116.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  13117.         return(0);
  13118.  
  13119.     if (auth_type)
  13120.       return(1);                        /* auth already succeeded */
  13121.  
  13122.     /* Try each auth type as specified by the end user */
  13123.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  13124. #ifdef FTP_GSSAPI
  13125.         if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  13126.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  13127.             if (n == REPLY_CONTINUE) {
  13128.                 OM_uint32 maj_stat, min_stat;
  13129.                 gss_name_t target_name;
  13130.                 gss_buffer_desc send_tok, recv_tok, *token_ptr;
  13131.                 char stbuf[FTP_BUFSIZ];
  13132.                 int comcode, trial;
  13133.                 struct gss_channel_bindings_struct chan;
  13134.                 char * realm = NULL;
  13135.                 char tgt[256];
  13136.  
  13137.                 chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  13138.                 chan.initiator_address.length = 4;
  13139.                 chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  13140.                 chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  13141.                 chan.acceptor_address.length = 4;
  13142.                 chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  13143.                 chan.application_data.length = 0;
  13144.                 chan.application_data.value = 0;
  13145.  
  13146.                 if (!quiet)
  13147.                   printf("GSSAPI accepted as authentication type\n");
  13148.  
  13149.                 realm = ck_krb5_realmofhost(ftp_user_host);
  13150.                 if (realm) {
  13151.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  13152.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  13153.                     if ( krb5_autoget &&
  13154.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  13155.                             (ck_krb5_is_tgt_valid() > 0)) )
  13156.                         ck_krb5_autoget_TGT(realm);
  13157.                 }
  13158.  
  13159.                 /* Blob from gss-client */
  13160.                 for (trial = 0; trial < n_gss_trials; trial++) {
  13161.                     /* ftp@hostname first, the host@hostname */
  13162.                     /* the V5 GSSAPI binding canonicalizes this for us... */
  13163.                     ckmakmsg(stbuf,FTP_BUFSIZ,
  13164.                              gss_trials[trial].service_name,
  13165.                              "@",
  13166.                              ftp_user_host,
  13167.                              NULL
  13168.                              );
  13169.                     if (ftp_deb)
  13170.                       fprintf(stderr,
  13171.                               "Authenticating to <%s>...\n", stbuf);
  13172.                     send_tok.value = stbuf;
  13173.                     send_tok.length = strlen(stbuf);
  13174.                     maj_stat = gss_import_name(&min_stat, &send_tok,
  13175.                                                gss_nt_service_name,
  13176.                                                &target_name
  13177.                                                );
  13178.                     if (maj_stat != GSS_S_COMPLETE) {
  13179.                         user_gss_error(maj_stat, min_stat, "parsing name");
  13180.                         secure_error("name parsed <%s>\n", stbuf);
  13181.                         continue;
  13182.                     }
  13183.                     token_ptr = GSS_C_NO_BUFFER;
  13184.                     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  13185.  
  13186.                     do {
  13187.                         if (ftp_deb)
  13188.                           fprintf(stderr, "calling gss_init_sec_context\n");
  13189.                         maj_stat =
  13190.                           gss_init_sec_context(&min_stat,
  13191.                                                GSS_C_NO_CREDENTIAL,
  13192.                                                &gcontext,
  13193.                                                target_name,
  13194.                                                (gss_OID) *
  13195.                                                  gss_trials[trial].mech_type,
  13196.                                                GSS_C_MUTUAL_FLAG |
  13197.                                                GSS_C_REPLAY_FLAG |
  13198.                                                (ftp_cfw ?
  13199.                                                 GSS_C_DELEG_FLAG : 0),
  13200.                                                0,
  13201.                                                 /* channel bindings */
  13202.                                                 (krb5_d_no_addresses ?
  13203.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  13204.                                                   &chan),
  13205.                                                 token_ptr,
  13206.                                                NULL,    /* ignore mech type */
  13207.                                                &send_tok,
  13208.                                                NULL,    /* ignore ret_flags */
  13209.                                                NULL
  13210.                                                );       /* ignore time_rec */
  13211.  
  13212.                         if (maj_stat != GSS_S_COMPLETE &&
  13213.                             maj_stat != GSS_S_CONTINUE_NEEDED) {
  13214.                             if (trial == n_gss_trials-1)
  13215.                               user_gss_error(maj_stat,
  13216.                                              min_stat,
  13217.                                              "initializing context"
  13218.                                              );
  13219.                             gss_release_name(&min_stat, &target_name);
  13220.                             /* maybe we missed on the service name */
  13221.                             goto outer_loop;
  13222.                         }
  13223.                         if (send_tok.length != 0) {
  13224.                             int len;
  13225.                             reply_parse = "ADAT="; /* for ftpcmd() later */
  13226.                             len = sizeof(out_buf);
  13227.                             kerror =
  13228.                               radix_encode(send_tok.value,
  13229.                                            out_buf,
  13230.                                            send_tok.length,
  13231.                                            &len,
  13232.                                            RADIX_ENCODE
  13233.                                            );
  13234.                             if (kerror)  {
  13235.                                 fprintf(stderr,
  13236.                                         "Base 64 encoding failed: %s\n",
  13237.                                         radix_error(kerror)
  13238.                                         );
  13239.                                 goto gss_complete_loop;
  13240.                             }
  13241.                             comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  13242.                             if (comcode != REPLY_COMPLETE
  13243.                                 /* && comcode != 3 (335)*/
  13244.                                 ) {
  13245.                                 if (trial == n_gss_trials-1) {
  13246.                                     fprintf(stderr, "GSSAPI ADAT failed\n");
  13247.                                     /* force out of loop */
  13248.                                     maj_stat = GSS_S_FAILURE;
  13249.                                 }
  13250.                                 /*
  13251.                                   Backoff to the v1 gssapi is still possible.
  13252.                                   Send a new AUTH command.  If that fails,
  13253.                                   terminate the loop.
  13254.                                 */
  13255.                                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  13256.                                     != REPLY_CONTINUE) {
  13257.                                     fprintf(stderr,
  13258.                                 "GSSAPI ADAT failed, AUTH restart failed\n");
  13259.                                     /* force out of loop */
  13260.                                     maj_stat = GSS_S_FAILURE;
  13261.                                 }
  13262.                                 goto outer_loop;
  13263.                             }
  13264.                             if (!reply_parse) {
  13265.                                 fprintf(stderr,
  13266.                               "No authentication data received from server\n");
  13267.                                 if (maj_stat == GSS_S_COMPLETE) {
  13268.                                     fprintf(stderr,
  13269.                                             "...but no more was needed\n");
  13270.                                     goto gss_complete_loop;
  13271.                                 } else {
  13272.                                     user_gss_error(maj_stat,
  13273.                                                    min_stat,
  13274.                                                    "no reply, huh?"
  13275.                                                    );
  13276.                                     goto gss_complete_loop;
  13277.                                 }
  13278.                             }
  13279.                             len = sizeof(out_buf);
  13280.                             kerror = radix_encode(reply_parse,out_buf,i,&len,
  13281.                                                   RADIX_DECODE);
  13282.                             if (kerror) {
  13283.                                 fprintf(stderr,
  13284.                                         "Base 64 decoding failed: %s\n",
  13285.                                         radix_error(kerror));
  13286.                                 goto gss_complete_loop;
  13287.                             }
  13288.  
  13289.                             /* everything worked */
  13290.                             token_ptr = &recv_tok;
  13291.                             recv_tok.value = out_buf;
  13292.                             recv_tok.length = len;
  13293.                             continue;
  13294.  
  13295.                             /* get out of loop clean */
  13296.                           gss_complete_loop:
  13297.                             trial = n_gss_trials-1;
  13298.                             gss_release_buffer(&min_stat, &send_tok);
  13299.                             gss_release_name(&min_stat, &target_name);
  13300.                             goto outer_loop;
  13301.                         }
  13302.                     } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  13303.  
  13304.                   outer_loop:
  13305.                     if (maj_stat == GSS_S_COMPLETE)
  13306.                       break;
  13307.                 }
  13308.                 if (maj_stat == GSS_S_COMPLETE) {
  13309.                     printf("GSSAPI authentication succeeded\n");
  13310.                     reply_parse = NULL;
  13311.                     auth_type = "GSSAPI";
  13312.                     return(1);
  13313.                 } else {
  13314.                     fprintf(stderr, "GSSAPI authentication failed\n");
  13315.                     reply_parse = NULL;
  13316.                 }
  13317.             } else {
  13318.                 if (ftp_deb)
  13319.                 fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  13320.                 if (ftpcode == 500 || ftpcode == 502)
  13321.                     return(0);
  13322.             }
  13323.         }
  13324. #endif /* FTP_GSSAPI */
  13325. #ifdef FTP_SRP
  13326.         if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  13327.             if (srp_ftp_auth(ftp_user_host,NULL,NULL))
  13328.               return(1);
  13329.             else if (ftpcode == 500 || ftpcode == 502)
  13330.               return(0);
  13331.         }
  13332. #endif /* FTP_SRP */
  13333. #ifdef FTP_KRB4
  13334.         if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  13335.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  13336.             if (n == REPLY_CONTINUE) {
  13337.                 char tgt[4*REALM_SZ+1];
  13338.                 int rc;
  13339.  
  13340.                 if (!quiet)
  13341.                   printf("KERBEROS_V4 accepted as authentication type\n");
  13342.                 ckstrncpy(inst, (char *) krb_get_phost(ftp_user_host),INST_SZ);
  13343.                 ckstrncpy(ftp_realm,
  13344.                           (char *)ck_krb4_realmofhost(ftp_user_host),
  13345.                           REALM_SZ
  13346.                           );
  13347.  
  13348.                 ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  13349.                 rc = ck_krb4_tkt_isvalid(tgt);
  13350.  
  13351.                 if (rc <= 0 && krb4_autoget)
  13352.                   ck_krb4_autoget_TGT(ftp_realm);
  13353.  
  13354.                 service = "ftp";
  13355.                 kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  13356.                 if (kerror == KDC_PR_UNKNOWN) {
  13357.                     service = "rcmd";
  13358.                     kerror = krb_mk_req(&ftp_tkt,
  13359.                                         service,
  13360.                                         inst,
  13361.                                         ftp_realm,
  13362.                                         checksum
  13363.                                         );
  13364.                 }
  13365.                 if (kerror)
  13366.                   fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  13367.                           krb_get_err_text(kerror));
  13368.                 if (!kerror) {
  13369.                     kerror = krb_get_cred(service, inst, ftp_realm,
  13370.                                           (CREDENTIALS *)&ftp_cred);
  13371.                     if (kerror)
  13372.                       fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  13373.                               krb_get_err_text(kerror));
  13374.                 }
  13375.                 if (!kerror) {
  13376.                     int rc;
  13377.                     rc = des_key_sched(ftp_cred.session, ftp_sched);
  13378.                     if (rc == -1) {
  13379.                        printf("?Invalid DES key specified in credentials\r\n");
  13380.                        debug(F110,"ftp_auth",
  13381.                              "invalid DES Key specified in credentials",0);
  13382.                     } else if ( rc == -2 ) {
  13383.                         printf("?Weak DES key specified in credentials\r\n");
  13384.                         debug(F110,"ftp_auth",
  13385.                               "weak DES Key specified in credentials",0);
  13386.                     } else if ( rc != 0 ) {
  13387.                         printf("?DES Key Schedule not set by credentials\r\n");
  13388.                         debug(F110,"ftp_auth",
  13389.                               "DES Key Schedule not set by credentials",0);
  13390.                     }
  13391.                     reply_parse = "ADAT=";
  13392.                     i = sizeof(out_buf);
  13393.                     kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  13394.                                           &i, RADIX_ENCODE);
  13395.                     if (kerror) {
  13396.                         fprintf(stderr, "Base 64 encoding failed: %s\n",
  13397.                                 radix_error(kerror));
  13398.                         goto krb4_err;
  13399.                     }
  13400.                     if (i > FTP_BUFSIZ - 6)
  13401.                       printf("?ADAT data too long\n");
  13402.                     if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  13403.                         REPLY_COMPLETE) {
  13404.                         fprintf(stderr, "Kerberos V4 authentication failed\n");
  13405.                         goto krb4_err;
  13406.                     }
  13407.                     if (!reply_parse) {
  13408.                         fprintf(stderr,
  13409.                              "No authentication data received from server\n");
  13410.                         goto krb4_err;
  13411.                     }
  13412.                     i = sizeof(out_buf);
  13413.                     kerror =
  13414.                       radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  13415.                     if (kerror) {
  13416.                         fprintf(stderr, "Base 64 decoding failed: %s\n",
  13417.                                 radix_error(kerror));
  13418.                         goto krb4_err;
  13419.                     }
  13420.                     kerror = krb_rd_safe(out_buf, i,
  13421. #ifdef KRB524
  13422.                                          ftp_cred.session,
  13423. #else /* KRB524 */
  13424.                                          &ftp_cred.session,
  13425. #endif /* KRB524 */
  13426.                                          &hisctladdr,
  13427.                                          &myctladdr,
  13428.                                          &ftp_msg_data
  13429.                                          );
  13430.                     if (kerror) {
  13431.                         fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  13432.                                 krb_get_err_text(kerror));
  13433.                         goto krb4_err;
  13434.                     }
  13435.  
  13436.                     /* fetch the (modified) checksum */
  13437.                     memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  13438.                     if (ntohl(cksum) == checksum + 1) {
  13439.                         if (ftp_vbm)
  13440.                           printf("Kerberos V4 authentication succeeded\n");
  13441.                         reply_parse = NULL;
  13442.                         auth_type = "KERBEROS_V4";
  13443.                         return(1);
  13444.                     } else
  13445.                       fprintf(stderr,
  13446.                               "Kerberos V4 mutual authentication failed\n");
  13447.                   krb4_err:
  13448.                     reply_parse = NULL;
  13449.                 }
  13450.             } else {
  13451.                 if (ftp_deb)
  13452.           fprintf(stderr,
  13453.                       "KERBEROS_V4 rejected as an authentication type\n");
  13454.                 if (ftpcode == 500 || ftpcode == 502)
  13455.                     return(0);
  13456.             }
  13457.         }
  13458. #endif /* FTP_KRB4 */
  13459. #ifdef CK_SSL
  13460.         if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  13461. #ifdef FTPHOST
  13462.             if (!hostcmd) {
  13463.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13464.                 hostcmd = 1;
  13465.             }
  13466. #endif /* FTPHOST */
  13467.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  13468.             if (n != REPLY_COMPLETE)
  13469.               n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  13470.             if (n == REPLY_COMPLETE) {
  13471.                 if (!quiet)
  13472.                   printf("TLS accepted as authentication type\n");
  13473.  
  13474.                 auth_type = "TLS";
  13475.                 ssl_auth();
  13476.                 if (ssl_ftp_active_flag ) {
  13477.                     ftp_dpl = FPL_CLR;
  13478.                     ftp_cpl = FPL_PRV;
  13479.                     return(1);
  13480.                 } else {
  13481.                     fprintf(stderr,"TLS authentication failed\n");
  13482.                     auth_type = NULL;
  13483. #ifdef TCPIPLIB
  13484.                     socket_close(csocket);
  13485. #else /* TCPIPLIB */
  13486. #ifdef USE_SHUTDOWN
  13487.                     shutdown(csocket, 1+1);
  13488. #endif /* USE_SHUTDOWN */
  13489.                     close(csocket);
  13490. #endif /* TCPIPLIB */
  13491.                     csocket = -1;
  13492.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13493.                       return(0);
  13494.                 }
  13495.             } else {
  13496.                 if (ftp_deb)
  13497.           fprintf(stderr,"TLS rejected as an authentication type\n");
  13498.                 if (ftpcode == 500 || ftpcode == 502)
  13499.                     return(0);
  13500.             }
  13501.         }
  13502.         if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  13503. #ifdef FTPHOST
  13504.             if (!hostcmd) {
  13505.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13506.                 hostcmd = 1;
  13507.             }
  13508. #endif /* FTPHOST */
  13509.             n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  13510.             if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  13511.                 if (!quiet)
  13512.                   printf("SSL accepted as authentication type\n");
  13513.                 auth_type = "SSL";
  13514.                 ssl_auth();
  13515.                 if (ssl_ftp_active_flag) {
  13516.                     ftp_dpl = FPL_PRV;
  13517.                     ftp_cpl = FPL_PRV;
  13518.                     setprotbuf(1<<20);
  13519.                     return(1);
  13520.                 } else {
  13521.                     fprintf(stderr,"SSL authentication failed\n");
  13522.                     auth_type = NULL;
  13523. #ifdef TCPIPLIB
  13524.                     socket_close(csocket);
  13525. #else /* TCPIPLIB */
  13526. #ifdef USE_SHUTDOWN
  13527.                     shutdown(csocket, 1+1);
  13528. #endif /* USE_SHUTDOWN */
  13529.                     close(csocket);
  13530. #endif /* TCPIPLIB */
  13531.                     csocket = -1;
  13532.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13533.                       return(0);
  13534.                 }
  13535.         } else {
  13536.                 if (ftp_deb)
  13537.           fprintf(stderr, "SSL rejected as an authentication type\n");
  13538.                 if (ftpcode == 500 || ftpcode == 502)
  13539.           return(0);
  13540.             }
  13541.         }
  13542. #endif /* CK_SSL */
  13543.         /* Other auth types go here ... */
  13544.     } /* for (j;;) */
  13545.     return(0);
  13546. }
  13547. #endif /* FTP_SECURITY */
  13548.  
  13549. static int
  13550. #ifdef CK_ANSIC
  13551. setprotbuf(unsigned int size)
  13552. #else
  13553. setprotbuf(size) unsigned int size;
  13554. #endif /* CK_ANSIC */
  13555. /* setprotbuf */ {
  13556.     if (ucbuf)
  13557.       free(ucbuf);
  13558.     ucbuf = NULL;
  13559.     ucbufsiz = 0;
  13560.     actualbuf = size;
  13561.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  13562.         if (actualbuf)
  13563.           actualbuf /= 2;
  13564.         else
  13565.           return(0);
  13566.     }
  13567.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  13568.     if (ucbufsiz < 128) {
  13569.         printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  13570.     } else if (ucbufsiz < 0) {
  13571.         printf("ERROR: ucbuf allocation failure\n");
  13572.         return(-1);
  13573.     }
  13574.     maxbuf = actualbuf;
  13575.     return(1);
  13576. }
  13577.  
  13578. static int
  13579. #ifdef CK_ANSIC
  13580. setpbsz(unsigned int size)
  13581. #else
  13582. setpbsz(size) unsigned int size;
  13583. #endif /* CK_ANSIC */
  13584. /* setpbsz */ {
  13585.     if (!setprotbuf(size)) {
  13586.         perror("?Error while trying to malloc PROT buffer:");
  13587. #ifdef FTP_SRP
  13588.         srp_reset();
  13589. #endif /* FTP_SRP */
  13590.         ftpclose();
  13591.         return(-1);
  13592.     }
  13593.     reply_parse = "PBSZ=";
  13594.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  13595. #ifdef CK_SSL
  13596.              ssl_ftp_active_flag ? "0" :
  13597. #endif /* CK_SSL */
  13598.              ckuitoa(actualbuf),NULL,NULL);
  13599.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  13600.         if (connected) {
  13601.             printf("?Unable to negotiate PROT buffer size with FTP server\n");
  13602.             ftpclose();
  13603.         }
  13604.         return(-1);
  13605.     }
  13606.     if (reply_parse) {
  13607.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  13608.           maxbuf = actualbuf;
  13609.     } else
  13610.       maxbuf = actualbuf;
  13611.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  13612.     reply_parse = NULL;
  13613.     return(0);
  13614. }
  13615.  
  13616. static VOID
  13617. cancel_remote(din) int din; {
  13618.     CHAR buf[FTP_BUFSIZ];
  13619.     int x, nfnd;
  13620. #ifdef BSDSELECT
  13621.     fd_set mask;
  13622. #endif /* BSDSELECT */
  13623. #ifdef IBMSELECT
  13624.     int fds[2], fdcnt = 0;
  13625. #endif /* IBMSELECT */
  13626. #ifdef DEBUG
  13627.     extern int debtim;
  13628.     int xdebtim;
  13629.     xdebtim = debtim;
  13630.     debtim = 1;
  13631. #endif /* DEBUG */
  13632.     debug(F100,"ftp cancel_remote entry","",0);
  13633. #ifdef CK_SSL
  13634.     if (ssl_ftp_active_flag) {
  13635.         /*
  13636.          * Send Telnet IP, Telnet DM but do so inline and within the
  13637.          * TLS channel
  13638.          */
  13639.         int count, error;
  13640.  
  13641.         buf[0] = IAC;
  13642.         buf[1] = TN_IP;
  13643.         buf[2] = IAC;
  13644.         buf[3] = TN_DM;
  13645.         buf[4] = NUL;
  13646.  
  13647.         count = SSL_write(ssl_ftp_con, buf, 4);
  13648.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  13649.         error = SSL_get_error(ssl_ftp_con,count);
  13650.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  13651.         switch (error) {
  13652.           case SSL_ERROR_NONE:
  13653.             break;
  13654.           case SSL_ERROR_WANT_WRITE:
  13655.           case SSL_ERROR_WANT_READ:
  13656.           case SSL_ERROR_SYSCALL:
  13657. #ifdef NT
  13658.             {
  13659.                 int gle = GetLastError();
  13660.             }
  13661. #endif /* NT */
  13662.           case SSL_ERROR_WANT_X509_LOOKUP:
  13663.           case SSL_ERROR_SSL:
  13664.           case SSL_ERROR_ZERO_RETURN:
  13665.           default:
  13666.             lostpeer();
  13667.             return;
  13668.         }
  13669.     } else
  13670. #endif /* CK_SSL */
  13671.     {
  13672.         /*
  13673.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  13674.          * after urgent byte rather than before as is protocol now.
  13675.          */
  13676.         buf[0] = IAC;
  13677.         buf[1] = TN_IP;
  13678.         buf[2] = IAC;
  13679.         buf[3] = NUL;
  13680.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  13681.           perror("cancel");
  13682.         debug(F101,"ftp cancel_remote send 1","",x);
  13683.         buf[0] = TN_DM;
  13684.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  13685.         debug(F101,"ftp cancel_remote send 2","",x);
  13686.     }
  13687.     x = scommand("ABOR");
  13688.     debug(F101,"ftp cancel_remote scommand","",x);
  13689. #ifdef BSDSELECT
  13690.     FD_ZERO(&mask);
  13691.     FD_SET(csocket, &mask);
  13692.     if (din) {
  13693.         FD_SET(din, &mask);
  13694.     }
  13695.     nfnd = empty(&mask, 10);
  13696.     debug(F101,"ftp cancel_remote empty","",nfnd);
  13697.     if ((nfnd) <= 0) {
  13698.         if (nfnd < 0) {
  13699.             perror("cancel");
  13700.         }
  13701. #ifdef FTP_PROXY
  13702.         if (ptabflg)
  13703.           ftpcode = -1;
  13704. #endif /* FTP_PROXY */
  13705.         lostpeer();
  13706.     }
  13707.     debug(F110,"ftp cancel_remote","D",0);
  13708.     if (din && FD_ISSET(din, &mask)) {
  13709.         /* Security: No threat associated with this read. */
  13710.         /* But you can't simply read the TLS data stream  */
  13711. #ifdef CK_SSL
  13712.         if (ssl_ftp_data_active_flag) {
  13713.             int count, error;
  13714.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  13715.                     /* LOOP */ ;
  13716.         } else
  13717. #endif /* CK_SSL */
  13718.         {
  13719.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  13720.                 /* LOOP */ ;
  13721.         }
  13722.     }
  13723.     debug(F110,"ftp cancel_remote","E",0);
  13724. #else /* BSDSELECT */
  13725. #ifdef IBMSELECT
  13726.     fds[0] = csocket;
  13727.     fdcnt++;
  13728.     if (din) {
  13729.         fds[1] = din;
  13730.         fdcnt++;
  13731.     }
  13732.     nfnd = empty(fds, fdcnt, 10);
  13733.     debug(F101,"ftp cancel_remote empty","",nfnd);
  13734.     if ((nfnd) <= 0) {
  13735.         if (nfnd < 0) {
  13736.             perror("cancel");
  13737.         }
  13738. #ifdef FTP_PROXY
  13739.         if (ptabflg)
  13740.           ftpcode = -1;
  13741. #endif /* FTP_PROXY */
  13742.         lostpeer();
  13743.     }
  13744.     debug(F110,"ftp cancel_remote","D",0);
  13745.     if (din && select(&din, 1,0,0,1) ) {
  13746. #ifdef CK_SSL
  13747.         if (ssl_ftp_data_active_flag) {
  13748.             int count, error;
  13749.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  13750.                     /* LOOP */ ;
  13751.         } else
  13752. #endif /* CK_SSL */
  13753.         {
  13754.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  13755.                 /* LOOP */ ;
  13756.         }
  13757.     }
  13758.     debug(F110,"ftp cancel_remote","E",0);
  13759. #else /* IBMSELECT */
  13760.     Some form of select is required.
  13761. #endif /* IBMSELECT */
  13762. #endif /* BSDSELECT */
  13763.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  13764.         debug(F110,"ftp cancel_remote","F",0);
  13765.         /* 552 needed for NIC style cancel */
  13766.         getreply(0,-1,-1,ftp_vbm,0);
  13767.         debug(F110,"ftp cancel_remote","G",0);
  13768.     }
  13769.     debug(F110,"ftp cancel_remote","H",0);
  13770.     getreply(0,-1,-1,ftp_vbm,0);
  13771.     debug(F110,"ftp cancel_remote","I",0);
  13772. #ifdef DEBUG
  13773.     debtim = xdebtim;
  13774. #endif /* DEBUG */
  13775. }
  13776.  
  13777. static int
  13778. fts_dpl(x) int x; {
  13779.     if (!auth_type
  13780. #ifdef OS2
  13781.          || !ck_crypt_is_installed()
  13782. #endif /* OS2 */
  13783.          ) {
  13784.         switch ( x ) {
  13785.           case FPL_PRV:
  13786.             printf("?Cannot set protection level to PRIVATE\n");
  13787.             return(0);
  13788.           case FPL_SAF:
  13789.             printf("?Cannot set protection level to SAFE\n");
  13790.             return(0);
  13791.         }
  13792.         ftp_dpl = x;
  13793.         return(1);
  13794.     }
  13795.  
  13796. #ifdef CK_SSL
  13797.     if (x == FPL_SAF &&
  13798.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  13799.         printf("Cannot set protection level to safe\n");
  13800.         return(0);
  13801.     }
  13802. #endif /* CK_SSL */
  13803.     /* Start with a PBSZ of 1 meg */
  13804.     if (x != FPL_CLR) {
  13805.         if (setpbsz(DEFAULT_PBSZ) < 0)
  13806.           return(0);
  13807.     }
  13808.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  13809.                (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  13810.     if (y == REPLY_COMPLETE) {
  13811.         ftp_dpl = x;
  13812.         return(1);
  13813.     }
  13814.     return(0);
  13815. }
  13816.  
  13817. static int
  13818. fts_cpl(x) int x; {
  13819.     if (!auth_type 
  13820. #ifdef OS2
  13821.          || !ck_crypt_is_installed()
  13822. #endif /* OS2 */
  13823.          ) {
  13824.         switch ( x ) {
  13825.           case FPL_PRV:
  13826.             printf("?Cannot set protection level to PRIVATE\n");
  13827.             return(0);
  13828.           case FPL_SAF:
  13829.             printf("?Cannot set protection level to SAFE\n");
  13830.             return(0);
  13831.         }
  13832.         ftp_cpl = x;
  13833.         return(1);
  13834.     }
  13835.     if (x == FPL_CLR) {
  13836.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  13837.         if (y == REPLY_COMPLETE) {
  13838.             ftp_cpl = x;
  13839.             return(1);
  13840.         }
  13841.         return(0);
  13842.     }
  13843.     ftp_cpl = x;
  13844.     return(1);
  13845. }
  13846.  
  13847. #ifdef FTP_GSSAPI
  13848. static VOID
  13849. user_gss_error(maj_stat, min_stat, s)
  13850.     OM_uint32 maj_stat, min_stat;
  13851.     char *s;
  13852. {
  13853.     /* a lot of work just to report the error */
  13854.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  13855.     gss_buffer_desc msg;
  13856.     msg_ctx = 0;
  13857.     while (!msg_ctx) {
  13858.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  13859.                                        GSS_C_GSS_CODE,
  13860.                                        GSS_C_NULL_OID,
  13861.                                        &msg_ctx,
  13862.                                        &msg
  13863.                                        );
  13864.         if ((gmaj_stat == GSS_S_COMPLETE)||
  13865.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  13866.             fprintf(stderr, "GSSAPI error major: %s\n",
  13867.                     (char*)msg.value);
  13868.             gss_release_buffer(&gmin_stat, &msg);
  13869.         }
  13870.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  13871.           break;
  13872.     }
  13873.     msg_ctx = 0;
  13874.     while (!msg_ctx) {
  13875.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  13876.                                        GSS_C_MECH_CODE,
  13877.                                        GSS_C_NULL_OID,
  13878.                                        &msg_ctx,
  13879.                                        &msg
  13880.                                        );
  13881.         if ((gmaj_stat == GSS_S_COMPLETE)||
  13882.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  13883.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  13884.             gss_release_buffer(&gmin_stat, &msg);
  13885.         }
  13886.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  13887.           break;
  13888.     }
  13889.     fprintf(stderr, "GSSAPI error: %s\n", s);
  13890. }
  13891. #endif /* FTP_GSSAPI */
  13892.  
  13893. #ifndef NOMHHOST
  13894. #ifdef datageneral
  13895. #define NOMHHOST
  13896. #else
  13897. #ifdef HPUX5WINTCP
  13898. #define NOMHHOST
  13899. #endif /* HPUX5WINTCP */
  13900. #endif /* datageneral */
  13901. #endif /* NOMHHOST */
  13902.  
  13903. #ifdef INADDRX
  13904. static struct in_addr inaddrx;
  13905. #endif /* INADDRX */
  13906.  
  13907. static char *
  13908. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  13909.     register struct hostent *hp = 0;
  13910. #ifdef IP_TOS
  13911. #ifdef IPTOS_THROUGHPUT
  13912.     int tos;
  13913. #endif /* IPTOS_THROUGHPUT */
  13914. #endif /* IP_TOS */
  13915.     int s;
  13916.     GSOCKNAME_T len;
  13917.     static char hostnamebuf[512];
  13918.     char hostname[512] /* , *p, *q */ ;
  13919.     int  cport;
  13920. #ifdef DEBUG
  13921.     extern int debtim;
  13922.     int xdebtim;
  13923.     xdebtim = debtim;
  13924.     debtim = 1;
  13925. #endif /* DEBUG */
  13926.  
  13927. #ifndef NOHTTP
  13928.     if (tcp_http_proxy) {
  13929.         struct servent *destsp;
  13930.         char *p, *q;
  13931.  
  13932.         ckmakmsg(proxyhost,sizeof(proxyhost),host,":",ckuitoa(port),NULL);
  13933.         for (p = tcp_http_proxy, q = hostname;
  13934.              *p != '\0' && *p != ':';
  13935.              p++, q++
  13936.              )
  13937.           *q = *p;
  13938.         *q = '\0';
  13939.  
  13940.         if (*p == ':')
  13941.           p++;
  13942.         else
  13943.           p = "http";
  13944.  
  13945.         destsp = getservbyname(p,"tcp");
  13946.         if (destsp)
  13947.           cport = ntohs(destsp->s_port);
  13948.         else if (p) {
  13949.           cport = atoi(p);
  13950.         } else
  13951.           cport = 80;
  13952.     } else
  13953. #endif /* NOHTTP */
  13954.     {
  13955.         ckstrncpy(hostname,host,sizeof(hostname));
  13956.         cport = port;
  13957.     }
  13958.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  13959.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  13960.     if (hisctladdr.sin_addr.s_addr != -1) {
  13961.         debug(F110,"ftp hookup A",hostname,0);
  13962.         hisctladdr.sin_family = AF_INET;
  13963.         ckstrncpy(hostnamebuf, hostname, sizeof(hostnamebuf));
  13964.     } else {
  13965.         debug(F110,"ftp hookup B",hostname,0);
  13966.         hp = gethostbyname(hostname);
  13967. #ifdef HADDRLIST
  13968.         hp = ck_copyhostent(hp);        /* make safe copy that won't change */
  13969. #endif /* HADDRLIST */
  13970.         if (hp == NULL) {
  13971.             fprintf(stderr, "ftp: %s: Unknown host\n", host);
  13972.             ftpcode = -1;
  13973. #ifdef DEBUG
  13974.             debtim = xdebtim;
  13975. #endif /* DEBUG */
  13976.             return((char *) 0);
  13977.         }
  13978.         hisctladdr.sin_family = hp->h_addrtype;
  13979. #ifdef HADDRLIST
  13980.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  13981.                sizeof(hisctladdr.sin_addr));
  13982. #else /* HADDRLIST */
  13983.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  13984.                sizeof(hisctladdr.sin_addr));
  13985. #endif /* HADDRLIST */
  13986.         ckstrncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
  13987.     }
  13988.     debug(F110,"ftp hookup C",hostnamebuf,0);
  13989.     ftp_host = hostnamebuf;
  13990.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  13991.     debug(F101,"ftp hookup socket","",s);
  13992.     if (s < 0) {
  13993.         perror("ftp: socket");
  13994.         ftpcode = -1;
  13995. #ifdef DEBUG
  13996.         debtim = xdebtim;
  13997. #endif /* DEBUG */
  13998.         return(0);
  13999.     }
  14000.     hisctladdr.sin_port = htons(cport);
  14001.     errno = 0;
  14002. #ifdef HADDRLIST
  14003.     debug(F100,"ftp hookup HADDRLIST","",0);
  14004.     while
  14005. #else
  14006.     debug(F100,"ftp hookup no HADDRLIST","",0);
  14007.     if
  14008. #endif /* HADDRLIST */
  14009.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  14010.           debug(F101,"ftp hookup connect failed","",errno);
  14011. #ifdef HADDRLIST
  14012.           if (hp && hp->h_addr_list[1]) {
  14013.               int oerrno = errno;
  14014.  
  14015.               fprintf(stderr, "ftp: connect to address %s: ",
  14016.                       inet_ntoa(hisctladdr.sin_addr));
  14017.               errno = oerrno;
  14018.               perror((char *) 0);
  14019.               hp->h_addr_list++;
  14020.               memcpy((char *)&hisctladdr.sin_addr,
  14021.                      hp->h_addr_list[0],
  14022.                      sizeof(hisctladdr.sin_addr));
  14023.               fprintf(stdout, "Trying %s...\n",
  14024.                       inet_ntoa(hisctladdr.sin_addr));
  14025. #ifdef TCPIPLIB
  14026.               socket_close(s);
  14027. #else /* TCPIPLIB */
  14028.               close(s);
  14029. #endif /* TCPIPLIB */
  14030.               s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14031.               if (s < 0) {
  14032.                   perror("ftp: socket");
  14033.                   ftpcode = -1;
  14034. #ifdef DEBUG
  14035.                   debtim = xdebtim;
  14036. #endif /* DEBUG */
  14037.                   return(0);
  14038.               }
  14039.               continue;
  14040.           }
  14041. #endif /* HADDRLIST */
  14042.           perror("ftp: connect");
  14043.           ftpcode = -1;
  14044.           goto bad;
  14045.       }
  14046.     debug(F100,"ftp hookup connect ok","",0);
  14047.  
  14048.     len = sizeof (myctladdr);
  14049.     errno = 0;
  14050.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  14051.         debug(F101,"ftp hookup getsockname failed","",errno);
  14052.         perror("ftp: getsockname");
  14053.         ftpcode = -1;
  14054.         goto bad;
  14055.     }
  14056.     debug(F100,"ftp hookup getsockname ok","",0);
  14057.  
  14058. #ifndef NOHTTP
  14059.     if (tcp_http_proxy) {
  14060. #ifdef OS2
  14061.         char * agent = "Kermit 95";     /* Default user agent */
  14062. #else
  14063.         char * agent = "C-Kermit";
  14064. #endif /* OS2 */
  14065.  
  14066.         if (http_connect(s,agent,NULL,
  14067.                          tcp_http_proxy_user,
  14068.                          tcp_http_proxy_pwd,
  14069.                          0,
  14070.                          proxyhost
  14071.                          ) < 0) {
  14072.             char * foo = NULL;
  14073. #ifdef TCPIPLIB
  14074.             socket_close(s);
  14075. #else /* TCPIPLIB */
  14076.             close(s);
  14077. #endif /* TCPIPLIB */
  14078.  
  14079.             while (foo == NULL && tcp_http_proxy != NULL ) {
  14080.  
  14081.                 if (tcp_http_proxy_errno == 401 ||
  14082.                      tcp_http_proxy_errno == 407 ) {
  14083.                     char uid[UIDBUFLEN];
  14084.                     char pwd[256];
  14085.                     struct txtbox tb[2];
  14086.                     int ok;
  14087.  
  14088.                     tb[0].t_buf = uid;
  14089.                     tb[0].t_len = UIDBUFLEN;
  14090.                     tb[0].t_lbl = "Proxy Userid: ";
  14091.                     tb[0].t_dflt = NULL;
  14092.                     tb[0].t_echo = 1;
  14093.                     tb[1].t_buf = pwd;
  14094.                     tb[1].t_len = 256;
  14095.                     tb[1].t_lbl = "Proxy Passphrase: ";
  14096.                     tb[1].t_dflt = NULL;
  14097.                     tb[1].t_echo = 2;
  14098.  
  14099.                     ok = uq_mtxt("Proxy Server Authentication Required\n",
  14100.                                   NULL, 2, tb);
  14101.  
  14102.                     if (ok && uid[0]) {
  14103.                         char * proxy_user, * proxy_pwd;
  14104.  
  14105.                         proxy_user = tcp_http_proxy_user;
  14106.                         proxy_pwd  = tcp_http_proxy_pwd;
  14107.  
  14108.                         tcp_http_proxy_user = uid;
  14109.                         tcp_http_proxy_pwd = pwd;
  14110.  
  14111.                         foo = ftp_hookup(host, port, 0);
  14112.  
  14113.                         debug(F110,"ftp_hookup()",foo,0);
  14114.                         memset(pwd,0,sizeof(pwd));
  14115.                         tcp_http_proxy_user = proxy_user;
  14116.                         tcp_http_proxy_pwd = proxy_pwd;
  14117.                     } else
  14118.                         break;
  14119.                 } else
  14120.                     break;
  14121.             }
  14122.             if (foo != NULL)
  14123.               return(foo);
  14124.             perror("ftp: connect");
  14125.             ftpcode = -1;
  14126.             goto bad;
  14127.         }
  14128.         ckstrncpy(hostnamebuf, proxyhost, sizeof(hostnamebuf));
  14129.     }
  14130. #endif /* NOHTTP */
  14131.  
  14132.     csocket = s;
  14133.  
  14134. #ifdef CK_SSL
  14135.     if (tls) {
  14136.         /* FTP over SSL
  14137.          * If the connection is over an SSL proxy then the
  14138.          * auth_type will be NULL.  However, I'm not sure
  14139.          * whether we should protect the data channel in
  14140.          * that case or not.
  14141.          */
  14142.  
  14143.         debug(F100,"ftp hookup use_tls","",0);
  14144.         if (!ssl_auth()) {
  14145.             debug(F100,"ftp hookup ssl_auth failed","",0);
  14146.             auth_type = NULL;
  14147.             ftpcode = -1;
  14148.             csocket = -1;
  14149.             goto bad;
  14150.         }
  14151.         ssl_ftp_proxy = 1;
  14152.     }
  14153. #endif /* CK_SSL */
  14154.  
  14155. #ifdef IP_TOS
  14156. #ifdef IPTOS_LOWDELAY
  14157.     tos = IPTOS_LOWDELAY;
  14158.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  14159.       perror("ftp: setsockopt TOS (ignored)");
  14160. #endif
  14161. #endif
  14162.     if (!quiet)
  14163.       printf("Connected to %s.\n", host);
  14164.  
  14165.     /* Read greeting from server */
  14166.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  14167.         debug(F100,"ftp hookup bad reply","",0);
  14168. #ifdef TCPIPLIB
  14169.         socket_close(csocket);
  14170. #else /* TCPIPLIB */
  14171.         close(csocket);
  14172. #endif /* TCPIPLIB */
  14173.         ftpcode = -1;
  14174.         goto bad;
  14175.     }
  14176. #ifdef SO_OOBINLINE
  14177.     {
  14178.         int on = 1;
  14179.         errno = 0;
  14180.         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  14181.                        sizeof(on)) < 0) {
  14182.             perror("ftp: setsockopt");
  14183.             debug(F101,"ftp hookup setsockopt failed","",errno);
  14184.         }
  14185. #ifdef DEBUG
  14186.         else
  14187.           debug(F100,"ftp hookup setsockopt ok","",0);
  14188. #endif /* DEBUG */
  14189.     }
  14190. #endif /* SO_OOBINLINE */
  14191.  
  14192. #ifdef DEBUG
  14193.     debtim = xdebtim;
  14194. #endif /* DEBUG */
  14195.     return(ftp_host);
  14196.  
  14197.   bad:
  14198.     debug(F100,"ftp hookup bad","",0);
  14199. #ifdef TCPIPLIB
  14200.     socket_close(s);
  14201. #else /* TCPIPLIB */
  14202.     close(s);
  14203. #endif /* TCPIPLIB */
  14204. #ifdef DEBUG
  14205.     debtim = xdebtim;
  14206. #endif /* DEBUG */
  14207.     csocket = -1;
  14208.     return((char *)0);
  14209. }
  14210.  
  14211. static VOID
  14212. ftp_init() {
  14213.     int i, n;
  14214.  
  14215.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  14216.     /* clients do it.  In any case, failure of this command is not a */
  14217.     /* reliable indication that the server does not support Restart. */
  14218.  
  14219.     okrestart = 0;
  14220.     if (!noinit) {
  14221.         n = ftpcmd("REST 0",NULL,0,0,0);
  14222.         if (n == REPLY_COMPLETE)
  14223.           okrestart = 1;
  14224. #ifdef COMMENT
  14225.         else if (ftp_deb)
  14226.           printf("WARNING: Unable to restore file pointer.\n");
  14227. #endif /* COMMENT */
  14228.     }
  14229.     n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
  14230.     if (n == REPLY_COMPLETE) {
  14231.         register char *cp, c = NUL;
  14232.         cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  14233.         if (cp == NULL)
  14234.           cp = ckstrchr(ftp_reply_str+4,'\r');
  14235.         if (cp) {
  14236.             if (cp[-1] == '.')
  14237.               cp--;
  14238.             c = *cp;                    /* Save this char */
  14239.             *cp = '\0';                 /* Replace it with NUL */
  14240.         }
  14241.         if (!quiet)
  14242.           printf("Remote system type is %s.\n",ftp_reply_str+4);
  14243.         ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  14244.         if (cp)                         /* Put back saved char */
  14245.           *cp = c;
  14246.     }
  14247.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  14248.  
  14249.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  14250.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  14251.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  14252.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  14253.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  14254.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  14255.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  14256.  
  14257. #ifdef FTP_PROXY
  14258.     unix_proxy = 0;
  14259.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  14260. #endif /* FTP_PROXY */
  14261.  
  14262.     if (ftp_cmdlin && xfermode == XMODE_M)
  14263.       ftp_typ = binary;                 /* Type given on command line */
  14264.     else                                /* Otherwise set it automatically */
  14265.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  14266.     changetype(ftp_typ,0);              /* Change to this type */
  14267.     g_ftp_typ = ftp_typ;                /* Make it the global type */
  14268.     if (!quiet)
  14269.       printf("Default transfer mode is %s\n",
  14270.              ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  14271.              );
  14272.     for (i = 0; i < 16; i++)        /* Init server FEATure table */
  14273.       sfttab[i] = 0;
  14274.     if (!noinit) {
  14275.         n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
  14276. #ifdef COMMENT
  14277.         if (n != REPLY_COMPLETE)
  14278.           printf("WARNING: Server does not accept MODE S(TREAM)\n");
  14279. #endif /* COMMENT */
  14280.         n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
  14281. #ifdef COMMENT
  14282.         if (n != REPLY_COMPLETE)
  14283.           printf("WARNING: Server does not accept STRU F(ILE)\n");
  14284. #endif /* COMMENT */
  14285.     if (featok) {
  14286.         n = ftpcmd("FEAT",NULL,0,0,0); /* Ask server about features */
  14287.         if (n == REPLY_COMPLETE) {
  14288.         debug(F101,"ftp_init FEAT","",sfttab[0]);
  14289.         if (deblog || ftp_deb) {
  14290.             int i;
  14291.             for (i = 1; i < 16 && i < nfeattab; i++) {
  14292.             debug(F111,"ftp_init FEAT",feattab[i].kwd,sfttab[i]);
  14293.             if (ftp_deb)
  14294.               printf("  Server %s %s\n",
  14295.                  sfttab[i] ? "supports" : "does not support",
  14296.                  feattab[i].kwd
  14297.                  );
  14298.             }
  14299.             /* Deal with disabled MLST opts here if necessary */
  14300.             /* But why would it be? */
  14301.         }
  14302.         }
  14303.     }
  14304.     }
  14305. }
  14306.  
  14307. static int
  14308. ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
  14309.     static char ftppass[80]="";
  14310.     char tmp[256];
  14311.     char *user = NULL, *pass = NULL, *acct = NULL;
  14312.     int n, aflag = 0;
  14313.     extern char uidbuf[];
  14314.     extern char pwbuf[];
  14315.     extern int  pwflg, pwcrypt;
  14316.  
  14317.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  14318.  
  14319.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  14320.       anonymous = 1;
  14321.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  14322.       anonymous = 1;
  14323.  
  14324. #ifdef FTP_SRP
  14325.     if (auth_type && !strcmp(auth_type, "SRP")) {
  14326.         user = srp_user;
  14327.         pass = srp_pass;
  14328.         acct = srp_acct;
  14329.     } else
  14330. #endif /* FTP_SRP */
  14331.       if (anonymous) {
  14332.           user = "anonymous";
  14333.           if (ftp_tmp) {        /* They gave a password */
  14334.               pass = ftp_tmp;
  14335.           } else if (ftp_apw) {        /* SET FTP ANONYMOUS-PASSWORD */
  14336.           pass = ftp_apw;
  14337.       } else {            /* Supply user@host */
  14338.           ckmakmsg(tmp,256,whoami(),"@",myhost,NULL);
  14339.           pass = tmp;
  14340.           }
  14341.       debug(F110,"ftp anonymous",pass,0);
  14342.       } else {
  14343. #ifdef USE_RUSERPASS
  14344.           if (ruserpass(host, &user, &pass, &acct) < 0) {
  14345.               ftpcode = -1;
  14346.               return(0);
  14347.           }
  14348. #endif /* USE_RUSERPASS */
  14349.           if (ftp_logname) {
  14350.               user = ftp_logname;
  14351.               pass = ftp_tmp;
  14352.           } else if (uidbuf[0] && (ftp_tmp || pwbuf[0] && pwflg)) {
  14353.               user = uidbuf;
  14354.               if (ftp_tmp) {
  14355.                   pass = ftp_tmp;
  14356.               } else if (pwbuf[0] && pwflg) {
  14357.                   ckstrncpy(ftppass,pwbuf,sizeof(ftppass));
  14358. #ifdef OS2
  14359.                   if ( pwcrypt )
  14360.                       ck_encrypt((char *)ftppass);
  14361. #endif /* OS2 */
  14362.                   pass = ftppass;
  14363.               }
  14364.           }
  14365.           acct = ftp_acc;
  14366.           while (user == NULL) {
  14367.               char *myname, prompt[256];
  14368.               int ok;
  14369.  
  14370.               myname = whoami();
  14371.               if (myname)
  14372.                 ckmakxmsg(prompt,256," Name (",host,":",myname,"): ",
  14373.                           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  14374.               else
  14375.                 ckmakmsg(prompt,256," Name (",host,"): ",NULL);
  14376.               tmp[0] = '\0';
  14377.               
  14378.               ok = uq_txt(NULL,prompt,1,NULL,tmp,80,NULL);
  14379.               if (!ok || *tmp == '\0')
  14380.                 user = myname;
  14381.               else
  14382.                 user = tmp;
  14383.           }
  14384.       }
  14385.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14386.     if (n == REPLY_COMPLETE) {
  14387.         /* determine if we need to send a dummy password */
  14388.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  14389.           ftpcmd("PASS dummy",NULL,0,0,1);
  14390.     } else if (n == REPLY_CONTINUE) {
  14391. #ifdef CK_ENCRYPTION
  14392.         int oldftp_cpl;
  14393. #endif /* CK_ENCRYPTION */
  14394.  
  14395.         if (pass == NULL) {
  14396.             int ok;
  14397.             setint();
  14398.             ok = uq_txt(NULL," Password: ",2,NULL,ftppass,80,NULL);
  14399.             if (ok)
  14400.                 pass = ftppass;
  14401.         }
  14402.  
  14403. #ifdef CK_ENCRYPTION
  14404.         oldftp_cpl = ftp_cpl;
  14405.         ftp_cpl = FPL_PRV;
  14406. #endif /* CK_ENCRYPTION */
  14407.         n = ftpcmd("PASS",pass,-1,-1,1);
  14408.         if (!anonymous && pass) {
  14409.             char * p = pass;
  14410.             while (*p++) *(p-1) = NUL;
  14411.             makestr(&ftp_tmp,NULL);
  14412.         }
  14413. #ifdef CK_ENCRYPTION
  14414.         /* level may have changed */
  14415.         if (ftp_cpl == FPL_PRV)
  14416.           ftp_cpl = oldftp_cpl;
  14417. #endif /* CK_ENCRYPTION */
  14418.     }
  14419.     if (n == REPLY_CONTINUE) {
  14420.         aflag++;
  14421.         if (acct == NULL) {
  14422.             static char ftpacct[80];
  14423.             int ok;
  14424.             setint();
  14425.             ok = uq_txt(NULL," Account: ",2,NULL,ftpacct,80,NULL);
  14426.             if (ok)
  14427.                 acct = ftpacct;
  14428.         }
  14429.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14430.     }
  14431.     if (n != REPLY_COMPLETE) {
  14432.         fprintf(stderr, "FTP login failed.\n");
  14433.         if (haveurl)
  14434.           doexit(BAD_EXIT,-1);
  14435.         return(0);
  14436.     }
  14437.     if (!aflag && acct != NULL) {
  14438.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14439.     }
  14440.     makestr(&ftp_logname,user);
  14441.     loggedin = 1;
  14442. #ifdef LOCUS
  14443.     /* Unprefixed file management commands go to server */
  14444.     if (autolocus && !ftp_cmdlin) {
  14445.     setlocus(0,1);
  14446.     }
  14447. #endif /* LOCUS */
  14448.     ftp_init();
  14449.  
  14450.     if (anonymous && !quiet) {
  14451.         printf(" Logged in as anonymous (%s)\n",pass);
  14452.         memset(pass, 0, strlen(pass));
  14453.     }
  14454.     if (ftp_rdir) {
  14455.         if (doftpcwd(ftp_rdir,-1) < 1)
  14456.           doexit(BAD_EXIT,-1);
  14457.     }
  14458.  
  14459. #ifdef FTP_PROXY
  14460.     if (proxy)
  14461.       return(1);
  14462. #endif /* FTP_PROXY */
  14463.     return(1);
  14464. }
  14465.  
  14466. static int
  14467. ftp_reset() {
  14468.     int rc;
  14469. #ifdef BSDSELECT
  14470.     int nfnd = 1;
  14471.     fd_set mask;
  14472.     FD_ZERO(&mask);
  14473.     while (nfnd > 0) {
  14474.         FD_SET(csocket, &mask);
  14475.         if ((nfnd = empty(&mask,0)) < 0) {
  14476.             perror("reset");
  14477.             ftpcode = -1;
  14478.             lostpeer();
  14479.             return(0);
  14480.         } else if (nfnd) {
  14481.             getreply(0,-1,-1,ftp_vbm,0);
  14482.         }
  14483.     }
  14484. #else /* BSDSELECT */
  14485. #ifdef IBMSELECT
  14486.     int nfnd = 1;
  14487.     while (nfnd > 0) {
  14488.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  14489.             perror("reset");
  14490.             ftpcode = -1;
  14491.             lostpeer();
  14492.             return(0);
  14493.         } else if (nfnd) {
  14494.             getreply(0,-1,-1,ftp_vbm,0);
  14495.         }
  14496.     }
  14497. #endif /* IBMSELECT */
  14498. #endif /* BSDSELECT */
  14499.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  14500.     if (rc > 0)
  14501.       loggedin = 0;
  14502.     return(rc);
  14503. }
  14504.  
  14505. static int
  14506. ftp_rename(from, to) char * from, * to; {
  14507.     int lcs = -1, rcs = -1;
  14508. #ifndef NOCSETS
  14509.     if (ftp_xla) {
  14510.         lcs = ftp_csl;
  14511.         if (lcs < 0) lcs = fcharset;
  14512.         rcs = ftp_csx;
  14513.         if (rcs < 0) rcs = ftp_csr;
  14514.     }
  14515. #endif /* NOCSETS */
  14516.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  14517.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  14518.     }
  14519.     return(0);                          /* Failure */
  14520. }
  14521.  
  14522. static int
  14523. ftp_umask(mask) char * mask; {
  14524.     int rc;
  14525.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  14526.     return(rc);
  14527. }
  14528.  
  14529. static int
  14530. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  14531.     int n = 0, aflag = 0;
  14532.     char pwd[80];
  14533.  
  14534.     if (!auth_type && ftp_aut) {
  14535. #ifdef FTP_SRP
  14536.         if (ck_srp_is_installed()) {
  14537.             if (srp_ftp_auth( NULL, user, pass)) {
  14538.                 makestr(&pass,srp_pass);
  14539.             }
  14540.         }
  14541. #endif /* FTP_SRP */
  14542.     }
  14543.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14544.     if (n == REPLY_COMPLETE)
  14545.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  14546.     else if (n == REPLY_CONTINUE) {
  14547. #ifdef CK_ENCRYPTION
  14548.         int oldftp_cpl;
  14549. #endif /* CK_ENCRYPTION */
  14550.         if (pass == NULL || !pass[0]) {
  14551.             int ok;
  14552.             pwd[0] = '\0';
  14553.             setint();
  14554.             ok = uq_txt(NULL," Password: ",2,NULL,pwd,sizeof(pwd),NULL);
  14555.             if (ok)
  14556.                 pass = pwd;
  14557.         }
  14558.  
  14559. #ifdef CK_ENCRYPTION
  14560.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  14561.           ftp_cpl = PROT_P;
  14562. #endif /* CK_ENCRYPTION */
  14563.         n = ftpcmd("PASS",pass,-1,-1,1);
  14564.         memset(pass, 0, strlen(pass));
  14565. #ifdef CK_ENCRYPTION
  14566.         /* level may have changed */
  14567.         if (ftp_cpl == PROT_P)
  14568.           ftp_cpl = oldftp_cpl;
  14569. #endif /* CK_ENCRYPTION */
  14570.     }
  14571.     if (n == REPLY_CONTINUE) {
  14572.         if (acct == NULL || !acct[0]) {
  14573.             int ok;
  14574.             pwd[0] = '\0';
  14575.             setint();
  14576.             ok = uq_txt(NULL," Account: ",2,NULL,pwd,sizeof(pwd),NULL);
  14577.             if (ok)
  14578.                 acct = pwd;
  14579.         }
  14580.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14581.         aflag++;
  14582.     }
  14583.     if (n != REPLY_COMPLETE) {
  14584.         printf("Login failed.\n");
  14585.         return(0);
  14586.     }
  14587.     if (!aflag && acct != NULL && acct[0]) {
  14588.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14589.     }
  14590.     if (n == REPLY_COMPLETE) {
  14591.         makestr(&ftp_logname,user);
  14592.         loggedin = 1;
  14593.         ftp_init();
  14594.         return(1);
  14595.     }
  14596.     return(0);
  14597. }
  14598.  
  14599. char *
  14600. ftp_authtype() {
  14601.     if (!connected)
  14602.       return("NULL");
  14603.     return(auth_type ? auth_type : "NULL");
  14604. }
  14605.  
  14606. char *
  14607. ftp_cpl_mode() {
  14608.     switch (ftp_cpl) {
  14609.       case FPL_CLR:
  14610.         return("clear");
  14611.       case FPL_SAF:
  14612.         return("safe");
  14613.       case FPL_PRV:
  14614.         return("private");
  14615.       case FPL_CON:
  14616.         return("confidential");
  14617.       default:
  14618.         return("(error)");
  14619.     }
  14620. }
  14621.  
  14622. char *
  14623. ftp_dpl_mode() {
  14624.     switch (ftp_dpl) {
  14625.       case FPL_CLR:
  14626.         return("clear");
  14627.       case FPL_SAF:
  14628.         return("safe");
  14629.       case FPL_PRV:
  14630.         return("private");
  14631.       case FPL_CON:
  14632.         return("confidential");
  14633.       default:
  14634.         return("(error)");
  14635.     }
  14636. }
  14637.  
  14638.  
  14639. /* remote_files() */
  14640. /*
  14641.    Returns next remote filename on success;
  14642.    NULL on error or no more files with global rfrc set to:
  14643.      -1: Bad argument
  14644.      -2: Server error response to NLST, e.g. file not found
  14645.      -3: No more files
  14646.      -9: Internal error
  14647. */
  14648. #define FTPNAMBUFLEN CKMAXPATH+1024
  14649.  
  14650. /* Check: ckmaxfiles CKMAXOPEN */
  14651.  
  14652. #define MLSDEPTH 128            /* Stack of open temp files */
  14653. static mlsdepth = 0;            /* Temp file stack depth */
  14654. static FILE * tmpfilptr[MLSDEPTH+1] = { NULL, NULL }; /* Temp file pointers */
  14655. static char * tmpfilnam[MLSDEPTH+1] = { NULL, NULL }; /* Temp file names */
  14656.  
  14657. static VOID
  14658. mlsreset() {                /* Reset MGET temp-file stack */
  14659.     int i;
  14660.     for (i = 0; i <= mlsdepth; i++) {
  14661.     if (tmpfilptr[i]) {
  14662.         fclose(tmpfilptr[i]);
  14663.         tmpfilptr[i] = NULL;
  14664.         if (tmpfilnam[i]) {
  14665. #ifdef OS2
  14666.         unlink(tmpfilnam[i]);
  14667. #endif /* OS2 */
  14668.         free(tmpfilnam[i]);
  14669.         }
  14670.     }
  14671.     }
  14672.     mlsdepth = 0;
  14673. }
  14674.  
  14675. static CHAR *
  14676. #ifdef CK_ANSIC
  14677. remote_files(int new_query, CHAR * arg, CHAR * pattern, int proxy_switch)
  14678. #else /* CK_ANSIC */
  14679. remote_files(new_query, arg, pattern, proxy_switch)
  14680.     int new_query;
  14681.     CHAR * arg;                /* That we send to the server */
  14682.     CHAR * pattern;            /* That we use locally */
  14683.     int proxy_switch;
  14684. #endif /* CK_ANSIC */
  14685. /* remote_files */ {
  14686.     static CHAR buf[FTPNAMBUFLEN];
  14687.     CHAR *cp, *whicharg;
  14688.     char * cdto = NULL;
  14689.     char * p;
  14690.     int i, x, forced = 0;
  14691.     int lcs = 0, rcs = 0, xlate = 0;
  14692.  
  14693.     debug(F101,"ftp remote_files new_query","",new_query);
  14694.     debug(F110,"ftp remote_files arg",arg,0);
  14695.     debug(F110,"ftp remote_files pattern",pattern,0);
  14696.  
  14697.     rfrc = -1;
  14698.     if (pattern)            /* Treat empty pattern same as NULL */
  14699.       if (!*pattern)
  14700.     pattern = NULL;
  14701.     if (arg)                /* Ditto for arg */
  14702.       if (!*arg)
  14703.     arg = NULL;
  14704.  
  14705.   again:
  14706.  
  14707.     if (new_query) {
  14708.         if (tmpfilptr[mlsdepth]) {
  14709.             fclose(tmpfilptr[mlsdepth]);
  14710.             tmpfilptr[mlsdepth] = NULL;
  14711. #ifdef OS2
  14712.             if (!ftp_deb && !deblog)
  14713.               unlink(tmpfilnam[mlsdepth]);
  14714. #endif /* OS2 */
  14715.         }
  14716.     }
  14717.     if (tmpfilptr[mlsdepth] == NULL) {
  14718.         extern char * tempdir;
  14719.         char * p;
  14720.         debug(F110,"ftp remote_files tempdir",tempdir,0);
  14721.         if (tempdir) {
  14722.             p = tempdir;
  14723.         } else {
  14724. #ifdef OS2
  14725. #ifdef NT
  14726.             p = getenv("K95TMP");
  14727. #else
  14728.             p = getenv("K2TMP");
  14729. #endif /* NT */
  14730.             if (!p)
  14731. #endif /* OS2 */
  14732.               p = getenv("CK_TMP");
  14733.             if (!p)
  14734.               p = getenv("TMPDIR");
  14735.             if (!p) p = getenv("TEMP");
  14736.             if (!p) p = getenv("TMP");
  14737. #ifdef OS2ORUNIX
  14738.             if (p) {
  14739.                 int len = strlen(p);
  14740.                 if (p[len-1] != '/'
  14741. #ifdef OS2
  14742.                     && p[len-1] != '\\'
  14743. #endif /* OS2 */
  14744.                      ) {
  14745.                     static char foo[CKMAXPATH];
  14746.                     ckstrncpy(foo,p,CKMAXPATH);
  14747.                     ckstrncat(foo,"/",CKMAXPATH);
  14748.                     p = foo;
  14749.                 }
  14750.             } else
  14751. #else /* OS2ORUNIX */
  14752.             if (!p)
  14753. #endif /* OS2ORUNIX */
  14754. #ifdef UNIX                             /* Systems that have a standard */
  14755.                 p = "/tmp/";            /* temporary directory... */
  14756. #else
  14757. #ifdef datageneral
  14758.             p = ":TMP:";
  14759. #else
  14760.             p = "";
  14761. #endif /* datageneral */
  14762. #endif /* UNIX */
  14763.         }
  14764.         debug(F110,"ftp remote_files p",p,0);
  14765.  
  14766.     /* Get temp file */
  14767.  
  14768.     if ((tmpfilnam[mlsdepth] = (char *)malloc(CKMAXPATH+1))) {
  14769.         ckmakmsg((char *)tmpfilnam[mlsdepth],
  14770.              CKMAXPATH+1,p,"ckXXXXXX",NULL,NULL);
  14771.     } else {
  14772.         printf("?Malloc failure: remote_files()\n");
  14773.         return(NULL);
  14774.     }
  14775.  
  14776. #ifdef NT
  14777.     {
  14778.         char * tmpfil = mktemp((char *)tmpfilnam[mlsdepth]);
  14779.         if ( tmpfil )
  14780.         ckstrncpy(tmpfilnam[mlsdepth],tmpfil,CKMAXPATH+1);
  14781.     }
  14782. #else /* NT */
  14783. #ifdef MKTEMP
  14784. #ifdef MKSTEMP
  14785.     x = mkstemp((char *)tmpfilnam[mlsdepth]);
  14786.     if (x > -1) close(x);        /* We just want the name. */
  14787. #else
  14788.         mktemp((char *)tmpfilnam[mlsdepth]);
  14789. #endif /* MKSTEMP */
  14790.         /* if no mktmpnam() the name will just be "ckXXXXXX"... */
  14791. #endif /* MKTEMP */
  14792. #endif /* NT */
  14793.  
  14794.     debug(F111,"ftp remote_files tmpfilnam[mlsdepth]",
  14795.           tmpfilnam[mlsdepth],mlsdepth);
  14796.  
  14797. #ifdef FTP_PROXY
  14798.         if (proxy_switch) {
  14799.             pswitch(!proxy);
  14800.         }
  14801. #endif /* FTP_PROXY */
  14802.  
  14803.         debug(F101,"ftp remote_files ftp_xla","",ftp_xla);
  14804.         debug(F101,"ftp remote_files ftp_csl","",ftp_csl);
  14805.         debug(F101,"ftp remote_files ftp_csr","",ftp_csr);
  14806.  
  14807. #ifndef NOCSETS
  14808.         xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
  14809.         if (xlate) {                    /* ON? */
  14810.             lcs = ftp_csl;              /* Local charset */
  14811.             if (lcs < 0) lcs = fcharset;
  14812.             if (lcs < 0) xlate = 0;
  14813.         }
  14814.         if (xlate) {                    /* Still ON? */
  14815.             rcs = ftp_csx;              /* Remote (Server) charset */
  14816.             if (rcs < 0) rcs = ftp_csr;
  14817.             if (rcs < 0) xlate = 0;
  14818.         }
  14819. #endif /* NOCSETS */
  14820.  
  14821.     forced = mgetforced;        /* MGET method forced? */
  14822.     if (!forced || !mgetmethod)    /* Not forced... */
  14823.       mgetmethod = (sfttab[0] && sfttab[SFT_MLST]) ? /* so pick one */
  14824.           SND_MLS :
  14825.           SND_NLS; 
  14826. /*                                           
  14827.   User's Command:                 Result:
  14828.     mget /nlst                     NLST (NULL)
  14829.     mget /nlst foo                 NLST foo
  14830.     mget /nlst *.txt               NLST *.txt 
  14831.     mget /nlst /match:*.txt        NLST (NULL)
  14832.     mget /nlst /match:*.txt  foo   NLST foo   
  14833.     mget /mlsd                     MLSD (NULL)
  14834.     mget /mlsd foo                 MLSD foo
  14835.     mget /mlsd *.txt               MLSD (NULL)
  14836.     mget /mlsd /match:*.txt        MLSD (NULL)
  14837.     mget /mlsd /match:*.txt  foo   MLSD foo
  14838. */
  14839.     x = -1;
  14840.     while (x < 0) {
  14841.         if (pattern) {        /* Don't simplify this! */
  14842.         whicharg = arg;
  14843.         } else if (mgetmethod == SND_MLS) {
  14844.         if (arg)
  14845.           whicharg = iswild((char *)arg) ? NULL : arg;
  14846.         else
  14847.           whicharg = NULL;
  14848.         } else {
  14849.         whicharg = arg;
  14850.         }
  14851.         debug(F110,"ftp remote_files mgetmethod",
  14852.           mgetmethod == SND_MLS ? "MLSD" : "NLST", 0);
  14853.         debug(F110,"ftp remote_files whicharg",whicharg,0);
  14854.  
  14855.         x = recvrequest((mgetmethod == SND_MLS) ? "MLSD" : "NLST",
  14856.                 (char *)tmpfilnam[mlsdepth],
  14857.                 (char *)whicharg,
  14858.                 "wb",
  14859.                 0,
  14860.                 0,
  14861.                 NULL,
  14862.                 xlate,
  14863.                 lcs,
  14864.                 rcs
  14865.                 );
  14866.         if (x < 0) {        /* Chosen method wasn't accepted */
  14867.         if (forced) {
  14868.             if (ftpcode > 500 && ftpcode < 505 && !quiet)
  14869.               printf("?%s: Not supported by server\n",
  14870.                  mgetmethod == SND_MLS ? "MLSD" : "NLST"
  14871.                  );
  14872.             rfrc = -2;        /* Fail */
  14873.             return(NULL);
  14874.         }
  14875.         /* Not forced - if MLSD failed, try NLST */
  14876.         if (mgetmethod == SND_MLS) {  /* Server lied about MLST */
  14877.             sfttab[SFT_MLST] = 0;     /* So disable it */
  14878.             mlstok = 0;              /* and */
  14879.             mgetmethod = SND_NLS;     /* try NLST */
  14880.             continue;
  14881.         }
  14882.         rfrc = -2;
  14883.         return(NULL);
  14884.         }
  14885.     }
  14886. #ifdef FTP_PROXY
  14887.         if (proxy_switch) {
  14888.             pswitch(!proxy);
  14889.         }
  14890. #endif /* FTP_PROXY */
  14891.         tmpfilptr[mlsdepth] = fopen((char *)tmpfilnam[mlsdepth], "r");
  14892. #ifndef OS2
  14893.     if (tmpfilptr[mlsdepth]) {
  14894.         if (!ftp_deb && !deblog)
  14895.           unlink(tmpfilnam[mlsdepth]);
  14896.     }
  14897. #endif /* OS2 */
  14898.       notemp:
  14899.         if (!tmpfilptr[mlsdepth]) {
  14900.             debug(F110,"ftp remote_files open fail",tmpfilnam[mlsdepth],0);
  14901.             if ((!dpyactive || ftp_deb))
  14902.               printf("?Can't find list of remote files, oops\n");
  14903.             rfrc = -9;
  14904.             return(NULL);
  14905.         }
  14906.     if (ftp_deb)
  14907.       printf("LISTFILE: %s\n",tmpfilnam[mlsdepth]);
  14908.     }
  14909.     buf[0] = NUL;
  14910.     buf[FTPNAMBUFLEN-1] = NUL;
  14911.     buf[FTPNAMBUFLEN-2] = NUL;
  14912.  
  14913.     /* We have to redo all this because the first time was only for */
  14914.     /* for getting the file list, now it's for getting each file */
  14915.  
  14916.     if (arg && mgetmethod == SND_MLS) {    /* MLSD */
  14917.     if (!pattern && iswild((char *)arg)) {
  14918.         pattern = arg;        /* Wild arg is really a pattern */
  14919.         if (pattern)
  14920.           if (!*pattern)
  14921.         pattern = NULL;
  14922.         arg = NULL;            /* and not an arg */
  14923.     }
  14924.     if (new_query) {        /* Initial query? */
  14925.         cdto = (char *)arg;        /* (nonwild) arg given? */
  14926.         if (cdto)
  14927.           if (!*cdto)
  14928.         cdto = NULL;
  14929.         if (cdto)            /* If so, then CD to it */
  14930.           doftpcwd(cdto,0);
  14931.     }
  14932.     }
  14933.     new_query = 0;
  14934.  
  14935.     if (fgets((char *)buf, sizeof (buf), tmpfilptr[mlsdepth]) == NULL) {
  14936.         fclose(tmpfilptr[mlsdepth]);
  14937.         tmpfilptr[mlsdepth] = NULL;
  14938.  
  14939. #ifdef OS2
  14940.         if (!ftp_deb && !deblog)
  14941.           unlink(tmpfilnam[mlsdepth]);
  14942. #endif /* OS2 */
  14943.         if (ftp_deb && !deblog) {
  14944.             printf("(Temporary file %s NOT deleted)\n",
  14945.            (char *)tmpfilnam[mlsdepth]);
  14946.         }
  14947.     if (mlsdepth <= 0) {        /* EOF at depth 0 */
  14948.         rfrc = -3;            /* means we're done */
  14949.         return(NULL);
  14950.     }
  14951.     printf("POPPING(%d)...\n",mlsdepth-1); 
  14952.     if (tmpfilnam[mlsdepth]) free(tmpfilnam[mlsdepth]);
  14953.     mlsdepth--;
  14954.     doftpcdup();
  14955.     zchdir("..");            /* <-- Not portable */
  14956.     goto again;
  14957.     }
  14958.     if (buf[FTPNAMBUFLEN-1]) {
  14959.     printf("?BUFFER OVERFLOW -- FTP NLST or MLSD string longer than %d\n",
  14960.            FTPNAMBUFLEN
  14961.            );
  14962.     debug(F101,"remote_files buffer overrun","",FTPNAMBUFLEN);
  14963.     return(NULL);
  14964.     }
  14965.     /* debug(F110,"ftp remote_files buf 1",buf,0); */
  14966.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  14967.       *cp = '\0';
  14968.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  14969.       *cp = '\0';
  14970.     debug(F110,"ftp remote_files buf",buf,0);
  14971.     rfrc = 0;
  14972.  
  14973.     if (ftp_deb)
  14974.       printf("[%s]\n",(char *)buf);
  14975.  
  14976.     havesize = -1L;            /* Initialize file facts... */
  14977.     havetype = -0;
  14978.     makestr(&havemdtm,NULL);
  14979.     p = (char *)buf;
  14980.  
  14981.     if (mgetmethod == SND_NLS) {    /* NLST... */
  14982.     if (pattern) {
  14983.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  14984.           goto again;
  14985.     }
  14986.     } else {                /* MLSD... */
  14987.     p = parsefacts((char *)buf);
  14988.     switch (havetype) {
  14989.       case FTYP_FILE:        /* File: Get it if it matches */
  14990.         if (pattern) {
  14991.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  14992.           goto again;
  14993.         }
  14994.         break;
  14995.       case FTYP_CDIR:        /* Current directory */
  14996.       case FTYP_PDIR:        /* Parent directory */
  14997.         goto again;            /* Skip */
  14998.       case FTYP_DIR:        /* (Sub)Directory */
  14999.         if (!recursive)        /* If not /RECURSIVE */
  15000.           goto again;        /* Skip */
  15001.         if (mlsdepth < MLSDEPTH) {
  15002.         char * p2 = NULL;
  15003.         mlsdepth++;
  15004.         printf("RECURSING [%s](%d)...\n",p,mlsdepth); 
  15005.         if (doftpcwd(p,0) > 0) {
  15006.             int x;
  15007.             if (!ckstrchr(p,'/')) {
  15008.             /* zmkdir() needs dirsep */
  15009.             if ((p2 = (char *)malloc((int)strlen(p) + 2))) {
  15010.                 strcpy(p2,p);    /* SAFE */
  15011.                 strcat(p2,"/");    /* SAFE */
  15012.                 p = p2;
  15013.             }
  15014.             }
  15015. #ifdef NOMKDIR
  15016.             x = -1;
  15017. #else
  15018.             x = zmkdir(p);
  15019. #endif /* NOMKDIR */
  15020.             if (x > -1) {
  15021.             zchdir(p);
  15022.             p = (char *)remote_files(1,arg,pattern,0);
  15023.             if (p2) free(p2);
  15024.             } else {
  15025.             printf("?mkdir failed: [%s] Depth=%d\n",
  15026.                    p,
  15027.                    mlsdepth
  15028.                    );
  15029.             mlsreset();
  15030.             if (p2) free(p2);
  15031.             return(NULL);
  15032.             }
  15033.         } else {
  15034.             printf("?CWD failed: [%s] Depth=%d\n",p,mlsdepth);
  15035.             mlsreset();
  15036.             return(NULL);
  15037.         }
  15038.         } else {
  15039.         printf("MAX DIRECTORY STACK DEPTH EXCEEDED: %d\n",
  15040.                mlsdepth
  15041.                );
  15042.         mlsreset();
  15043.         return(NULL);
  15044.         }
  15045.     }
  15046.     }
  15047.  
  15048. #ifdef DEBUG
  15049.     if (deblog) {
  15050.     debug(F101,"remote_files havesize","",havesize);
  15051.     debug(F101,"remote_files havetype","",havetype);
  15052.     debug(F110,"remote_files havemdtm",havemdtm,0);    
  15053.     debug(F110,"remote_files name",p,0);    
  15054.     }
  15055. #endif /* DEBUG */
  15056.     return((CHAR *)p);
  15057. }
  15058.  
  15059. /* N O T  P O R T A B L E !!! */
  15060.  
  15061. #if (SIZEOF_SHORT == 4)
  15062. typedef unsigned short ftp_uint32;
  15063. typedef short ftp_int32;
  15064. #else
  15065. #if (SIZEOF_INT == 4)
  15066. typedef unsigned int ftp_uint32;
  15067. typedef int ftp_int32;
  15068. #else
  15069. #if (SIZEOF_LONG == 4)
  15070. typedef ULONG ftp_uint32;
  15071. typedef long ftp_int32;
  15072. #endif
  15073. #endif
  15074. #endif
  15075.  
  15076. /* Perhaps use these in general, certainly use them for GSSAPI */
  15077.  
  15078. #ifndef looping_write
  15079. #define ftp_int32 int
  15080. #define ftp_uint32 unsigned int
  15081. static int
  15082. looping_write(fd, buf, len)
  15083.     int fd;
  15084.     register CONST char *buf;
  15085.     int len;
  15086. {
  15087.     int cc;
  15088.     register int wrlen = len;
  15089.     do {
  15090.         cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  15091.         if (cc < 0) {
  15092.             if (errno == EINTR)
  15093.               continue;
  15094.             return(cc);
  15095.         } else {
  15096.             buf += cc;
  15097.             wrlen -= cc;
  15098.         }
  15099.     } while (wrlen > 0);
  15100.     return(len);
  15101. }
  15102. #endif
  15103. #ifndef looping_read
  15104. static int
  15105. looping_read(fd, buf, len)
  15106.     int fd;
  15107.     register char *buf;
  15108.     register int len;
  15109. {
  15110.     int cc, len2 = 0;
  15111.  
  15112.     do {
  15113.         cc = recv(fd, (char *)buf, len,0);
  15114.         if (cc < 0) {
  15115.             if (errno == EINTR)
  15116.               continue;
  15117.             return(cc);                 /* errno is already set */
  15118.         } else if (cc == 0) {
  15119.             return(len2);
  15120.         } else {
  15121.             buf += cc;
  15122.             len2 += cc;
  15123.             len -= cc;
  15124.         }
  15125.     } while (len > 0);
  15126.     return(len2);
  15127. }
  15128. #endif /* looping_read */
  15129.  
  15130. #define ERR -2
  15131.  
  15132. #ifdef COMMENT
  15133. static
  15134. secure_putbyte(fd, c) int fd; CHAR c; {
  15135.     int ret;
  15136.  
  15137.     ucbuf[nout++] = c;
  15138.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  15139.         nout = 0;
  15140.         if (ftp_dpl == FPL_CLR)
  15141.           ret = send(fd, (SENDARG2TYPE)ucbuf,
  15142.                      (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  15143.         else
  15144.           ret = secure_putbuf(fd,
  15145.                               ucbuf,
  15146.                               (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  15147.                               );
  15148.         return(ret?ret:c);
  15149.     }
  15150.     return(c);
  15151. }
  15152. #endif /* COMMENT */
  15153.  
  15154. /* returns:
  15155.  *       0  on success
  15156.  *      -1  on error (errno set)
  15157.  *      -2  on security error
  15158.  */
  15159. static int
  15160. secure_flush(fd) int fd; {
  15161.     int rc = 0;
  15162.     int len = 0;
  15163.  
  15164.     if (nout > 0) {
  15165.         len = nout;
  15166.         if (ftp_dpl == FPL_CLR) {
  15167.             rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  15168.             nout = 0;
  15169.             goto xflush;
  15170.         } else {
  15171.             rc = secure_putbuf(fd, ucbuf, nout);
  15172.             if (rc)
  15173.               goto xflush;
  15174.         }
  15175.     }
  15176.     rc = (ftp_dpl == FPL_CLR) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  15177.  
  15178.   xflush:
  15179.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  15180.     spackets++;
  15181.         spktl = len;
  15182.         ftscreen(SCR_PT,'D',spackets,NULL);
  15183.     }
  15184.     return(rc);
  15185. }
  15186.  
  15187. #ifdef COMMENT                          /* (not used) */
  15188. /* returns:
  15189.  *      c>=0  on success
  15190.  *      -1    on error
  15191.  *      -2    on security error
  15192.  */
  15193. static int
  15194. #ifdef CK_ANSIC
  15195. secure_putc(char c, int fd)
  15196. #else
  15197. secure_putc(c, fd) char c; int fd;
  15198. #endif /* CK_ANSIC */
  15199. /* secure_putc */ {
  15200.     return(secure_putbyte(fd, (CHAR) c));
  15201. }
  15202. #endif /* COMMENT */
  15203.  
  15204. /* returns:
  15205.  *      nbyte on success
  15206.  *      -1  on error (errno set)
  15207.  *      -2  on security error
  15208.  */
  15209. static int
  15210. #ifdef CK_ANSIC
  15211. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  15212. #else
  15213. secure_write(fd, buf, nbyte)
  15214.     int fd;
  15215.     CHAR * buf;
  15216.     unsigned int nbyte;
  15217. #endif /* CK_ANSIC */
  15218. {
  15219.     int ret;
  15220.  
  15221.     if (ftp_dpl == FPL_CLR) {
  15222.         if (nout > 0) {
  15223.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  15224.               return(ret);
  15225.             nout = 0;
  15226.         }
  15227.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  15228.     } else {
  15229.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  15230.         int bsent = 0;
  15231.  
  15232.         while (bsent < nbyte) {
  15233.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  15234.                         (ucbuflen - nout) : (nbyte - bsent));
  15235.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  15236.             nout += b2cp;
  15237.             bsent += b2cp;
  15238.  
  15239.             if (nout == ucbuflen) {
  15240.                 nout = 0;
  15241.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  15242.                 if (ret < 0)
  15243.                   return(ret);
  15244.             }
  15245.         }
  15246.         return(bsent);
  15247.     }
  15248. }
  15249.  
  15250. /* returns:
  15251.  *       0  on success
  15252.  *      -1  on error (errno set)
  15253.  *      -2  on security error
  15254.  */
  15255. static int
  15256. #ifdef CK_ANSIC
  15257. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  15258. #else
  15259. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  15260. #endif /* CK_ANSIC */
  15261. {
  15262.     static char *outbuf = NULL;         /* output ciphertext */
  15263. #ifdef FTP_SECURITY
  15264.     static unsigned int bufsize = 0;    /* size of outbuf */
  15265. #endif /* FTP_SECURITY */
  15266.     ftp_int32 length   = 0;
  15267.     ftp_uint32 net_len = 0;
  15268.  
  15269.     /* Other auth types go here ... */
  15270. #ifdef CK_SSL
  15271.     if (ssl_ftp_data_active_flag) {
  15272.         int count, error;
  15273.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  15274.         error = SSL_get_error(ssl_ftp_data_con,count);
  15275.         switch (error) {
  15276.           case SSL_ERROR_NONE:
  15277.             return(0);
  15278.           case SSL_ERROR_WANT_WRITE:
  15279.           case SSL_ERROR_WANT_READ:
  15280.           case SSL_ERROR_SYSCALL:
  15281. #ifdef NT
  15282.             {
  15283.                 int gle = GetLastError();
  15284.             }
  15285. #endif /* NT */
  15286.           case SSL_ERROR_WANT_X509_LOOKUP:
  15287.           case SSL_ERROR_SSL:
  15288.           case SSL_ERROR_ZERO_RETURN:
  15289.           default:
  15290.             SSL_shutdown(ssl_ftp_data_con);
  15291.             SSL_free(ssl_ftp_data_con);
  15292.             ssl_ftp_data_active_flag = 0;
  15293.             ssl_ftp_data_con = NULL;
  15294. #ifdef TCPIPLIB
  15295.             socket_close(data);
  15296. #else /* TCPIPLIB */
  15297. #ifdef USE_SHUTDOWN
  15298.             shutdown(data, 1+1);
  15299. #endif /* USE_SHUTDOWN */
  15300.             close(data);
  15301. #endif /* TCPIPLIB */
  15302.             data = -1;
  15303.             globaldin = data;
  15304.             return(-1);
  15305.         }
  15306.         return(-1);
  15307.     }
  15308. #endif /* CK_SSL */
  15309.  
  15310. #ifdef FTP_SRP
  15311.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  15312.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15313.             if (outbuf?
  15314.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15315.                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15316.                 bufsize = nbyte + FUDGE_FACTOR;
  15317.             } else {
  15318.                 bufsize = 0;
  15319.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15320.                 return(ERR);
  15321.             }
  15322.         }
  15323.         if ((length =
  15324.              srp_encode(ftp_dpl == FPL_PRV,
  15325.                         (CHAR *) buf,
  15326.                         (CHAR *) outbuf,
  15327.                         nbyte
  15328.                         )
  15329.              ) < 0) {
  15330.             secure_error ("srp_encode failed");
  15331.             return ERR;
  15332.         }
  15333.     }
  15334. #endif /* FTP_SRP */
  15335. #ifdef FTP_KRB4
  15336.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  15337.         struct sockaddr_in myaddr, hisaddr;
  15338.         GSOCKNAME_T len;
  15339.         len = sizeof(myaddr);
  15340.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15341.             secure_error("secure_putbuf: getsockname failed");
  15342.             return(ERR);
  15343.         }
  15344.         len = sizeof(hisaddr);
  15345.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15346.             secure_error("secure_putbuf: getpeername failed");
  15347.             return(ERR);
  15348.         }
  15349.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15350.             if (outbuf ?
  15351.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15352.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15353.                 bufsize = nbyte + FUDGE_FACTOR;
  15354.             } else {
  15355.                 bufsize = 0;
  15356.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15357.                 return(ERR);
  15358.             }
  15359.         }
  15360.         if (ftp_dpl == FPL_PRV) {
  15361.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  15362.                                  ftp_sched,
  15363. #ifdef KRB524
  15364.                                  ftp_cred.session,
  15365. #else /* KRB524 */
  15366.                                  &ftp_cred.session,
  15367. #endif /* KRB524 */
  15368.                                  &myaddr,
  15369.                                  &hisaddr
  15370.                                  );
  15371.         } else {
  15372.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  15373. #ifdef KRB524
  15374.                                  ftp_cred.session,
  15375. #else /* KRB524 */
  15376.                                  &ftp_cred.session,
  15377. #endif /* KRB524 */
  15378.                                  &myaddr,
  15379.                                  &hisaddr
  15380.                                  );
  15381.         }
  15382.         if (length == -1) {
  15383.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  15384.                          ftp_dpl == FPL_PRV ? "priv" : "safe");
  15385.             return(ERR);
  15386.         }
  15387.     }
  15388. #endif /* FTP_KRB4 */
  15389. #ifdef FTP_GSSAPI
  15390.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  15391.         gss_buffer_desc in_buf, out_buf;
  15392.         OM_uint32 maj_stat, min_stat;
  15393.         int conf_state;
  15394.  
  15395.         in_buf.value = buf;
  15396.         in_buf.length = nbyte;
  15397.         maj_stat = gss_seal(&min_stat, gcontext,
  15398.                             (ftp_dpl == FPL_PRV), /* confidential */
  15399.                             GSS_C_QOP_DEFAULT,
  15400.                             &in_buf,
  15401.                             &conf_state,
  15402.                             &out_buf
  15403.                             );
  15404.         if (maj_stat != GSS_S_COMPLETE) {
  15405.             /* generally need to deal */
  15406.             /* ie. should loop, but for now just fail */
  15407.             user_gss_error(maj_stat, min_stat,
  15408.                            ftp_dpl == FPL_PRV?
  15409.                            "GSSAPI seal failed":
  15410.                            "GSSAPI sign failed");
  15411.             return(ERR);
  15412.         }
  15413.         if (bufsize < out_buf.length) {
  15414.             if (outbuf ?
  15415.                 (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  15416.                 (outbuf = malloc((unsigned) out_buf.length))) {
  15417.                 bufsize = out_buf.length;
  15418.             } else {
  15419.                 bufsize = 0;
  15420.                 secure_error("%s (in malloc of PROT buffer)",
  15421.                              ck_errstr());
  15422.                 return(ERR);
  15423.             }
  15424.         }
  15425.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  15426.         gss_release_buffer(&min_stat, &out_buf);
  15427.     }
  15428. #endif /* FTP_GSSAPI */
  15429.     net_len = htonl((ULONG) length);
  15430.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  15431.       return(-1);
  15432.     if (looping_write(fd, outbuf, length) != length)
  15433.       return(-1);
  15434.     return(0);
  15435. }
  15436.  
  15437. /* fc = 0 means to get a byte; nonzero means to initialize buffer pointers */
  15438.  
  15439. static int
  15440. secure_getbyte(fd,fc) int fd,fc; {
  15441.     /* number of chars in ucbuf, pointer into ucbuf */
  15442.     static unsigned int nin = 0, bufp = 0;
  15443.     int kerror;
  15444.     ftp_uint32 length;
  15445.  
  15446.     if (fc) {
  15447.     nin = bufp = 0;
  15448.     ucbuf[0] = NUL;
  15449.     return(0);
  15450.     }
  15451.     if (nin == 0) {
  15452.         if (iscanceled())
  15453.           return(-9);
  15454. #ifdef CK_SSL
  15455.         if (ssl_ftp_data_active_flag) {
  15456.             int count, error;
  15457.             count = SSL_read(ssl_ftp_data_con, ucbuf, ucbufsiz);
  15458.             error = SSL_get_error(ssl_ftp_data_con,count);
  15459.             switch (error) {
  15460.               case SSL_ERROR_NONE:
  15461.                 nin = bufp = count;
  15462.                 rpackets++;
  15463.                 pktnum++;
  15464.                 if (fdispla != XYFD_B) {
  15465.                     rpktl = count;
  15466.                     ftscreen(SCR_PT,'D',rpackets,NULL);
  15467.                 }
  15468.                 break;
  15469.               case SSL_ERROR_WANT_WRITE:
  15470.               case SSL_ERROR_WANT_READ:
  15471.               case SSL_ERROR_SYSCALL:
  15472. #ifdef NT
  15473.                 {
  15474.                     int gle = GetLastError();
  15475.                 }
  15476. #endif /* NT */
  15477.               case SSL_ERROR_WANT_X509_LOOKUP:
  15478.               case SSL_ERROR_SSL:
  15479.               case SSL_ERROR_ZERO_RETURN:
  15480.               default:
  15481.                 nin = bufp = count = 0;
  15482.                 SSL_shutdown(ssl_ftp_data_con);
  15483.                 SSL_free(ssl_ftp_data_con);
  15484.                 ssl_ftp_data_active_flag = 0;
  15485.                 ssl_ftp_data_con = NULL;
  15486. #ifdef TCPIPLIB
  15487.                 socket_close(data);
  15488. #else /* TCPIPLIB */
  15489. #ifdef USE_SHUTDOWN
  15490.                 shutdown(data, 1+1);
  15491. #endif /* USE_SHUTDOWN */
  15492.                 close(data);
  15493. #endif /* TCPIPLIB */
  15494.                 data = -1;
  15495.                 globaldin = data;
  15496.                 break;
  15497.             }
  15498.         } else
  15499. #endif /* CK_SSL */
  15500.           {
  15501.               kerror = looping_read(fd, (char *)&length, sizeof(length));
  15502.               if (kerror != sizeof(length)) {
  15503.                   secure_error("Couldn't read PROT buffer length: %d/%s",
  15504.                                kerror,
  15505.                                kerror == -1 ? ck_errstr()
  15506.                                : "premature EOF"
  15507.                                );
  15508.                   return(ERR);
  15509.               }
  15510.               debug(F101,"secure_getbyte length","",length);
  15511.               debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  15512.  
  15513.               length = (ULONG) ntohl(length);
  15514.               if (length > maxbuf) {
  15515.                   secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  15516.                                length,
  15517.                                maxbuf
  15518.                                );
  15519.                   return(ERR);
  15520.               }
  15521.               if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  15522.                   secure_error("Couldn't read %u byte PROT buffer: %s",
  15523.                                length,
  15524.                                kerror == -1 ? ck_errstr() : "premature EOF"
  15525.                                );
  15526.                   return(ERR);
  15527.               }
  15528.  
  15529.               /* Other auth types go here ... */
  15530. #ifdef FTP_SRP
  15531.               if (strcmp(auth_type, "SRP") == 0) {
  15532.                   if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  15533.                                                 (CHAR *) ucbuf,
  15534.                                                 ucbuf,
  15535.                                                 length
  15536.                                                 )
  15537.                        ) == -1) {
  15538.                       secure_error ("srp_encode failed" );
  15539.                       return ERR;
  15540.                   }
  15541.               }
  15542. #endif /* FTP_SRP */
  15543. #ifdef FTP_KRB4
  15544.               if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  15545.                   struct sockaddr_in myaddr, hisaddr;
  15546.                   GSOCKNAME_T len;
  15547.                   len = sizeof(myaddr);
  15548.                   if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15549.                       secure_error("secure_putbuf: getsockname failed");
  15550.                       return(ERR);
  15551.                   }
  15552.                   len = sizeof(hisaddr);
  15553.                   if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15554.                       secure_error("secure_putbuf: getpeername failed");
  15555.                       return(ERR);
  15556.                   }
  15557.                   if (ftp_dpl) {
  15558.                       kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  15559. #ifdef KRB524
  15560.                                            ftp_cred.session,
  15561. #else /* KRB524 */
  15562.                                            &ftp_cred.session,
  15563. #endif /* KRB524 */
  15564.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15565.                   } else {
  15566.                       kerror = krb_rd_safe(ucbuf, length,
  15567. #ifdef KRB524
  15568.                                            ftp_cred.session,
  15569. #else /* KRB524 */
  15570.                                            &ftp_cred.session,
  15571. #endif /* KRB524 */
  15572.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15573.                   }
  15574.                   if (kerror) {
  15575.                       secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  15576.                                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  15577.                                    krb_get_err_text(kerror));
  15578.                       return(ERR);
  15579.                   }
  15580.                   memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  15581.                   nin = bufp = ftp_msg_data.app_length;
  15582.               }
  15583. #endif /* FTP_KRB4 */
  15584. #ifdef FTP_GSSAPI
  15585.               if (strcmp(auth_type, "GSSAPI") == 0) {
  15586.                   gss_buffer_desc xmit_buf, msg_buf;
  15587.                   OM_uint32 maj_stat, min_stat;
  15588.                   int conf_state;
  15589.  
  15590.                   xmit_buf.value = ucbuf;
  15591.                   xmit_buf.length = length;
  15592.                   conf_state = (ftp_dpl == FPL_PRV);
  15593.                   /* decrypt/verify the message */
  15594.                   maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  15595.                                         &msg_buf, &conf_state, NULL);
  15596.                   if (maj_stat != GSS_S_COMPLETE) {
  15597.                       user_gss_error(maj_stat, min_stat,
  15598.                                      (ftp_dpl == FPL_PRV)?
  15599.                                      "failed unsealing ENC message":
  15600.                                      "failed unsealing MIC message");
  15601.                       return ERR;
  15602.                   }
  15603.                   memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  15604.                   gss_release_buffer(&min_stat, &msg_buf);
  15605.               }
  15606. #endif /* FTP_GSSAPI */
  15607.               /* Other auth types go here ... */
  15608.  
  15609.               /* Update file transfer display */
  15610.               rpackets++;
  15611.               pktnum++;
  15612.               if (fdispla != XYFD_B) {
  15613.                   rpktl = nin;
  15614.                   ftscreen(SCR_PT,'D',rpackets,NULL);
  15615.               }
  15616.           }
  15617.     }
  15618.     if (nin == 0)
  15619.       return(EOF);
  15620.     else
  15621.       return(ucbuf[bufp - nin--]);
  15622. }
  15623.  
  15624. /* secure_getc(fd,fc)
  15625.  * Call with:
  15626.  *   fd = file descriptor for connection.
  15627.  *   fc = 0 to get a character, fc != 0 to initialize buffer pointers.
  15628.  * Returns:
  15629.  *   c>=0 on success (character value)
  15630.  *   -1   on EOF
  15631.  *   -2   on security error
  15632.  */
  15633. static int
  15634. secure_getc(fd,fc) int fd,fc; {        /* file descriptor, function code */
  15635.     if (ftp_dpl == FPL_CLR) {
  15636.         static unsigned int nin = 0, bufp = 0;
  15637.     if (fc) {
  15638.         nin = bufp = 0;
  15639.         ucbuf[0] = NUL;
  15640.         return(0);
  15641.     }
  15642.         if (nin == 0) {
  15643.             if (iscanceled())
  15644.               return(-9);
  15645.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  15646.             if (nin <= 0) {
  15647.                 debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  15648.                 debug(F101,"secure_getc returns EOF","",EOF);
  15649.                 nin = bufp = 0;
  15650.                 return(EOF);
  15651.             }
  15652.             debug(F101,"ftp secure_getc recv","",nin);
  15653.             hexdump("ftp secure_getc recv",ucbuf,16);
  15654.             rpackets++;
  15655.             pktnum++;
  15656.             if (fdispla != XYFD_B) {
  15657.                 rpktl = nin;
  15658.                 ftscreen(SCR_PT,'D',rpackets,NULL);
  15659.             }
  15660.         }
  15661.         return(ucbuf[bufp - nin--]);
  15662.     } else
  15663.       return(secure_getbyte(fd,fc));
  15664. }
  15665.  
  15666. /* returns:
  15667.  *     n>0  on success (n == # of bytes read)
  15668.  *       0  on EOF
  15669.  *      -1  on error (errno set), only for FPL_CLR
  15670.  *      -2  on security error
  15671.  */
  15672. static int
  15673. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  15674.     static int c = 0;
  15675.     int i;
  15676.  
  15677.     debug(F101,"secure_read bytes requested","",nbyte);
  15678.     if (c == EOF)
  15679.       return(c = 0);
  15680.     for (i = 0; nbyte > 0; nbyte--) {
  15681.         c = secure_getc(fd,0);
  15682.         switch (c) {
  15683.           case -9:                      /* Canceled from keyboard */
  15684.             debug(F101,"ftp secure_read interrupted","",c);
  15685.             return(0);
  15686.           case ERR:
  15687.             debug(F101,"ftp secure_read error","",c);
  15688.             return(c);
  15689.           case EOF:
  15690.             debug(F101,"ftp secure_read EOF","",c);
  15691.             if (!i)
  15692.               c = 0;
  15693.             return(i);
  15694.           default:
  15695.             buf[i++] = c;
  15696.         }
  15697.     }
  15698.     return(i);
  15699. }
  15700.  
  15701. #ifdef USE_RUSERPASS
  15702. /* BEGIN_RUSERPASS
  15703.  *
  15704.  * Copyright (c) 1985 Regents of the University of California.
  15705.  * All rights reserved.
  15706.  *
  15707.  * Redistribution and use in source and binary forms, with or without
  15708.  * modification, are permitted provided that the following conditions
  15709.  * are met:
  15710.  * 1. Redistributions of source code must retain the above copyright
  15711.  *    notice, this list of conditions and the following disclaimer.
  15712.  * 2. Redistributions in binary form must reproduce the above copyright
  15713.  *    notice, this list of conditions and the following disclaimer in the
  15714.  *    documentation and/or other materials provided with the distribution.
  15715.  * 3. All advertising materials mentioning features or use of this software
  15716.  *    must display the following acknowledgement:
  15717.  *      This product includes software developed by the University of
  15718.  *      California, Berkeley and its contributors.
  15719.  * 4. Neither the name of the University nor the names of its contributors
  15720.  *    may be used to endorse or promote products derived from this software
  15721.  *    without specific prior written permission.
  15722.  *
  15723.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  15724.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15725.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15726.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  15727.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15728.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  15729.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  15730.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  15731.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  15732.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  15733.  * SUCH DAMAGE.
  15734.  */
  15735.  
  15736. #ifndef lint
  15737. static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
  15738. #endif /* not lint */
  15739.  
  15740. #include <sys/types.h>
  15741. #include <stdio.h>
  15742. #include <string.h>
  15743. #ifdef HAVE_STDLIB_H
  15744. #include <stdlib.h>
  15745. #endif
  15746. #include <ctype.h>
  15747. #include <sys/stat.h>
  15748. #include <errno.h>
  15749.  
  15750. #ifndef MAXHOSTNAMELEN
  15751. #define MAXHOSTNAMELEN 64
  15752. #endif
  15753.  
  15754. char * renvlook();
  15755. static FILE * cfile;
  15756.  
  15757. #define DEFAULT 1
  15758. #define LOGIN   2
  15759. #define PASSWD  3
  15760. #define ACCOUNT 4
  15761. #define MACDEF  5
  15762. #define ID      10
  15763. #define MACH    11
  15764.  
  15765. static char tokval[100];
  15766.  
  15767. static struct toktab {
  15768.     char *tokstr;
  15769.     int tval;
  15770. } toktab[]= {
  15771.     "default",  DEFAULT,
  15772.     "login",    LOGIN,
  15773.     "password", PASSWD,
  15774.     "passwd",   PASSWD,
  15775.     "account",  ACCOUNT,
  15776.     "machine",  MACH,
  15777.     "macdef",   MACDEF,
  15778.     0,          0
  15779. };
  15780.  
  15781. static int
  15782. token() {
  15783.     char *cp;
  15784.     int c;
  15785.     struct toktab *t;
  15786.  
  15787.     if (feof(cfile))
  15788.       return(0);
  15789.     while ((c = getc(cfile)) != EOF &&
  15790.            (c == '\n' || c == '\t' || c == ' ' || c == ','))
  15791.       continue;
  15792.     if (c == EOF)
  15793.       return(0);
  15794.     cp = tokval;
  15795.     if (c == '"') {
  15796.         while ((c = getc(cfile)) != EOF && c != '"') {
  15797.             if (c == '\\')
  15798.               c = getc(cfile);
  15799.             *cp++ = c;
  15800.         }
  15801.     } else {
  15802.         *cp++ = c;
  15803.         while ((c = getc(cfile)) != EOF
  15804.                && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  15805.             if (c == '\\')
  15806.               c = getc(cfile);
  15807.             *cp++ = c;
  15808.         }
  15809.     }
  15810.     *cp = 0;
  15811.     if (tokval[0] == 0)
  15812.       return(0);
  15813.     for (t = toktab; t->tokstr; t++)
  15814.       if (!strcmp(t->tokstr, tokval))
  15815.         return(t->tval);
  15816.     return(ID);
  15817. }
  15818.  
  15819. ruserpass(host, aname, apass, aacct)
  15820.     char *host, **aname, **apass, **aacct;
  15821. {
  15822.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  15823.     char myname[MAXHOSTNAMELEN], *mydomain;
  15824.     int t, i, c, usedefault = 0;
  15825. #ifdef NT
  15826.     struct _stat stb;
  15827. #else /* NT */
  15828.     struct stat stb;
  15829. #endif /* NT */
  15830.  
  15831.     hdir = getenv("HOME");
  15832.     if (hdir == NULL)
  15833.         hdir = ".";
  15834.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  15835.     cfile = fopen(buf, "r");
  15836.     if (cfile == NULL) {
  15837.         if (errno != ENOENT)
  15838.           perror(buf);
  15839.         return(0);
  15840.     }
  15841.     if (gethostname(myname, sizeof(myname)) < 0)
  15842.       myname[0] = '\0';
  15843.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  15844.       mydomain = "";
  15845.  
  15846.   next:
  15847.     while ((t = token())) switch(t) {
  15848.  
  15849.       case DEFAULT:
  15850.         usedefault = 1;
  15851.         /* FALL THROUGH */
  15852.  
  15853.       case MACH:
  15854.         if (!usedefault) {
  15855.             if (token() != ID)
  15856.               continue;
  15857.             /*
  15858.              * Allow match either for user's input host name
  15859.              * or official hostname.  Also allow match of
  15860.              * incompletely-specified host in local domain.
  15861.              */
  15862.             if (ckstrcmp(host, tokval,-1,1) == 0)
  15863.               goto match;
  15864.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  15865.               goto match;
  15866.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  15867.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  15868.                 ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  15869.                 tokval[tmp - ftp_host] == '\0')
  15870.               goto match;
  15871.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  15872.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  15873.                 ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  15874.                 tokval[tmp - host] == '\0')
  15875.               goto match;
  15876.             continue;
  15877.         }
  15878.  
  15879.       match:
  15880.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  15881.  
  15882.           case LOGIN:
  15883.             if (token())
  15884.               if (*aname == 0) {
  15885.                   *aname = malloc((unsigned) strlen(tokval) + 1);
  15886.                   strcpy(*aname, tokval);      /* safe */
  15887.               } else {
  15888.                   if (strcmp(*aname, tokval))
  15889.                     goto next;
  15890.               }
  15891.             break;
  15892.           case PASSWD:
  15893.             if (strcmp(*aname, "anonymous") &&
  15894.                 fstat(fileno(cfile), &stb) >= 0 &&
  15895.                 (stb.st_mode & 077) != 0) {
  15896.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  15897.                 fprintf(stderr, "Remove password or correct mode.\n");
  15898.                 goto bad;
  15899.             }
  15900.             if (token() && *apass == 0) {
  15901.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  15902.                 strcpy(*apass, tokval);          /* safe */
  15903.             }
  15904.             break;
  15905.           case ACCOUNT:
  15906.             if (fstat(fileno(cfile), &stb) >= 0
  15907.                 && (stb.st_mode & 077) != 0) {
  15908.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  15909.                 fprintf(stderr, "Remove account or correct mode.\n");
  15910.                 goto bad;
  15911.             }
  15912.             if (token() && *aacct == 0) {
  15913.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  15914.                 strcpy(*aacct, tokval);          /* safe */
  15915.             }
  15916.             break;
  15917.  
  15918.           default:
  15919.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  15920.             break;
  15921.         }
  15922.         goto done;
  15923.     }
  15924.  
  15925.   done:
  15926.     fclose(cfile);
  15927.     return(0);
  15928.  
  15929.   bad:
  15930.     fclose(cfile);
  15931.     return(-1);
  15932. }
  15933. #endif /* USE_RUSERPASS */
  15934.  
  15935. static char *radixN =
  15936.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  15937.  
  15938. static char pad = '=';
  15939.  
  15940. static int
  15941. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  15942.     CHAR inbuf[], outbuf[];
  15943.     int inlen, *outlen, decode;
  15944. {
  15945.     int i, j, D = 0;
  15946.     char *p;
  15947.     CHAR c = NUL;
  15948.  
  15949.     if (decode) {
  15950.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  15951.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  15952.               return(1);
  15953.             D = p - radixN;
  15954.             switch (i&3) {
  15955.               case 0:
  15956.                 outbuf[j] = D<<2;
  15957.                 break;
  15958.               case 1:
  15959.                 outbuf[j++] |= D>>4;
  15960.                 outbuf[j] = (D&15)<<4;
  15961.                 break;
  15962.               case 2:
  15963.                 outbuf[j++] |= D>>2;
  15964.                 outbuf[j] = (D&3)<<6;
  15965.                 break;
  15966.               case 3:
  15967.                 outbuf[j++] |= D;
  15968.             }
  15969.             if (j == *outlen)
  15970.               return(4);
  15971.         }
  15972.         switch (i&3) {
  15973.           case 1: return(3);
  15974.           case 2: if (D&15) return(3);
  15975.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  15976.             break;
  15977.           case 3: if (D&3) return(3);
  15978.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  15979.         }
  15980.         *outlen = j;
  15981.     } else {
  15982.         for (i = 0, j = 0; i < inlen; i++) {
  15983.             switch (i%3) {
  15984.               case 0:
  15985.                 outbuf[j++] = radixN[inbuf[i]>>2];
  15986.                 c = (inbuf[i]&3)<<4;
  15987.                 break;
  15988.               case 1:
  15989.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  15990.                 c = (inbuf[i]&15)<<2;
  15991.                 break;
  15992.               case 2:
  15993.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  15994.                 outbuf[j++] = radixN[inbuf[i]&63];
  15995.                 c = 0;
  15996.             }
  15997.             if (j == *outlen)
  15998.               return(4);
  15999.         }
  16000.         if (i%3) outbuf[j++] = radixN[c];
  16001.         switch (i%3) {
  16002.           case 1: outbuf[j++] = pad;
  16003.           case 2: outbuf[j++] = pad;
  16004.         }
  16005.         outbuf[*outlen = j] = '\0';
  16006.     }
  16007.     return(0);
  16008. }
  16009.  
  16010. static char *
  16011. radix_error(e) int e;
  16012. {
  16013.     switch (e) {
  16014.       case 0:  return("Success");
  16015.       case 1:  return("Bad character in encoding");
  16016.       case 2:  return("Encoding not properly padded");
  16017.       case 3:  return("Decoded # of bits not a multiple of 8");
  16018.       case 4:  return("Output buffer too small");
  16019.       default: return("Unknown error");
  16020.     }
  16021. }
  16022. /* END_RUSERPASS */
  16023.  
  16024. #ifdef FTP_SRP
  16025. /*---------------------------------------------------------------------------+
  16026.  |                                                                           |
  16027.  |   Package: srpftp                                                         |
  16028.  |   Author: Eugene Jhong                                                    |
  16029.  |                                                                           |
  16030.  +---------------------------------------------------------------------------*/
  16031.  
  16032. /*
  16033.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  16034.  * All Rights Reserved.
  16035.  *
  16036.  * Permission is hereby granted, free of charge, to any person obtaining
  16037.  * a copy of this software and associated documentation files (the
  16038.  * "Software"), to deal in the Software without restriction, including
  16039.  * without limitation the rights to use, copy, modify, merge, publish,
  16040.  * distribute, sublicense, and/or sell copies of the Software, and to
  16041.  * permit persons to whom the Software is furnished to do so, subject to
  16042.  * the following conditions:
  16043.  *
  16044.  * The above copyright notice and this permission notice shall be
  16045.  * included in all copies or substantial portions of the Software.
  16046.  *
  16047.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  16048.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16049.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16050.  *
  16051.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  16052.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16053.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  16054.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  16055.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16056.  *
  16057.  * In addition, the following conditions apply:
  16058.  *
  16059.  * 1. Any software that incorporates the SRP authentication technology
  16060.  *    must display the following acknowlegment:
  16061.  *    "This product uses the 'Secure Remote Password' cryptographic
  16062.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  16063.  *
  16064.  * 2. Any software that incorporates all or part of the SRP distribution
  16065.  *    itself must also display the following acknowledgment:
  16066.  *    "This product includes software developed by Tom Wu and Eugene
  16067.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  16068.  *
  16069.  * 3. Redistributions in source or binary form must retain an intact copy
  16070.  *    of this copyright notice and list of conditions.
  16071.  */
  16072.  
  16073. #define SRP_PROT_VERSION        1
  16074.  
  16075. #ifdef CK_ENCRYPTION
  16076. #define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
  16077. #else
  16078. #define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
  16079. #endif /* CK_ENCRYPTION */
  16080.  
  16081. #define SRP_DEFAULT_HASH        HASH_ID_SHA
  16082.  
  16083. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  16084. CHAR srp_pref_hash = HASH_ID_SHA;
  16085.  
  16086. static struct t_client *tc = NULL;
  16087. static CHAR *skey = NULL;
  16088. static krypto_context *incrypt = NULL;
  16089. static krypto_context *outcrypt = NULL;
  16090.  
  16091. typedef unsigned int srp_uint32;
  16092.  
  16093. /*--------------------------------------------------------------+
  16094.  | srp_selcipher: select cipher                                 |
  16095.  +--------------------------------------------------------------*/
  16096. static int
  16097. srp_selcipher (cname) char *cname; {
  16098.     cipher_desc *cd;
  16099.  
  16100.     if (!(cd = cipher_getdescbyname (cname))) {
  16101.         int i;
  16102.         CHAR *list = cipher_getlist ();
  16103.  
  16104.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  16105.         for (i = 0; i < strlen (list); i++)
  16106.           fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  16107.         fprintf (stderr, "\n");
  16108.         return -1;
  16109.     }
  16110.     srp_pref_cipher = cd->id;
  16111.     return 0;
  16112. }
  16113.  
  16114. /*--------------------------------------------------------------+
  16115.  | srp_selhash: select hash                                     |
  16116.  +--------------------------------------------------------------*/
  16117. static int
  16118. srp_selhash (hname) char *hname; {
  16119.     hash_desc *hd;
  16120.  
  16121.     if (!(hd = hash_getdescbyname (hname))) {
  16122.         int i;
  16123.         CHAR *list = hash_getlist ();
  16124.  
  16125.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  16126.         for (i = 0; i < strlen (list); i++)
  16127.           fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  16128.         fprintf (stderr, "\n");
  16129.         return -1;
  16130.     }
  16131.     srp_pref_hash = hd->id;
  16132.     return 0;
  16133. }
  16134.  
  16135. /*--------------------------------------------------------------+
  16136.  | srp_userpass: get username and password                      |
  16137.  +--------------------------------------------------------------*/
  16138. static int
  16139. srp_userpass (host) char *host; {
  16140.     char tmp[BUFSIZ], prompt[256];
  16141.     char *user;
  16142.  
  16143.     user = NULL;
  16144. #ifdef USE_RUSERPASS
  16145.     ruserpass (host, &user, &srp_pass, &srp_acct);
  16146. #endif /* USE_RUSERPASS */
  16147.  
  16148.     while (user == NULL)     {
  16149.         char *myname;
  16150.         int ok;
  16151.  
  16152.         myname = whoami();
  16153.         if (!myname) myname = "";
  16154.         if (myname[0])
  16155.           ckmakxmsg(prompt,sizeof(prompt)," Name (",host,":",myname,"): ",
  16156.                     NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  16157.         else
  16158.           ckmakmsg(prompt,sizeof(prompt)," Name (",host,"): ",NULL);
  16159.         tmp[0] = '\0';
  16160.         ok = uq_txt(NULL,prompt,1,NULL,tmp,80,NULL);
  16161.         if (!ok || *tmp == '\0')
  16162.           user = myname;
  16163.         else
  16164.           user = tmp;
  16165.     }
  16166.     ckstrncpy (srp_user, user,BUFSIZ);
  16167.     return(0);
  16168. }
  16169.  
  16170. /*--------------------------------------------------------------+
  16171.  | srp_reset: reset srp information                             |
  16172.  +--------------------------------------------------------------*/
  16173. static int
  16174. srp_reset () {
  16175.     if (tc) { t_clientclose (tc); tc = NULL; }
  16176.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  16177.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  16178.     return(0);
  16179. }
  16180.  
  16181. /*--------------------------------------------------------------+
  16182.  | srp_ftp_auth: perform srp authentication                         |
  16183.  +--------------------------------------------------------------*/
  16184. static int
  16185. srp_ftp_auth(host, user, pass)
  16186.     char *host;
  16187.     char *user;
  16188.     char *pass;
  16189. {
  16190.     struct t_num *wp;
  16191.     struct t_num N;
  16192.     struct t_num g;
  16193.     struct t_num s;
  16194.     struct t_num yp;
  16195.     CHAR buf[FTP_BUFSIZ];
  16196.     CHAR tmp[FTP_BUFSIZ];
  16197.     CHAR *bp, *cp;
  16198.     int n, e, clen, blen, len, i;
  16199.     CHAR cid = 0;
  16200.     CHAR hid = 0;
  16201.  
  16202.     srp_pass = srp_acct = 0;
  16203.  
  16204.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  16205.     if (n != REPLY_CONTINUE) {
  16206.         if (ftp_deb)
  16207.             fprintf(stderr, "SRP rejected as an authentication type\n");
  16208.         return(0);
  16209.     } else {                            /* Send protocol version */
  16210.         CHAR vers[4];
  16211.         memset (vers, 0, 4);
  16212.         vers[3] = SRP_PROT_VERSION;
  16213.         if (!quiet)
  16214.           printf ("SRP accepted as authentication type.\n");
  16215.         bp = tmp; blen = 0;
  16216.         srp_put (vers, &bp, 4, &blen);
  16217.         len = FTP_BUFSIZ;
  16218.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16219.           goto encode_error;
  16220.         reply_parse = "ADAT=";
  16221.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16222.     }
  16223.     if (n == REPLY_CONTINUE) {          /* Get protocol version */
  16224.         bp = buf;
  16225.         if (!reply_parse)
  16226.           goto data_error;
  16227.         blen = sizeof(buf);
  16228.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  16229.           goto decode_error;
  16230.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16231.           goto data_error;
  16232.  
  16233.         if (host) {                     /* Get username/password if needed */
  16234.             srp_userpass (host);
  16235.         } else {
  16236.             ckstrncpy (srp_user, user, BUFSIZ);
  16237.             srp_pass = pass;
  16238.         }
  16239.         bp = tmp; blen = 0;             /* Send username */
  16240.         srp_put (srp_user, &bp, strlen (srp_user), &blen);
  16241.         len = sizeof(buf);
  16242.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16243.           goto encode_error;
  16244.         reply_parse = "ADAT=";
  16245.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16246.     }
  16247.     if (n == REPLY_CONTINUE) {          /* Get N, g and s */
  16248.         bp = buf;
  16249.         if (!reply_parse)
  16250.           goto data_error;
  16251.         blen = sizeof(buf);
  16252.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16253.           goto decode_error;
  16254.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  16255.           goto data_error;
  16256.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  16257.           goto data_error;
  16258.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  16259.           goto data_error;
  16260.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  16261.             fprintf (stderr, "Unable to open SRP client structure.\n");
  16262.             goto bad;
  16263.         }
  16264.         wp = t_clientgenexp (tc);       /* Send wp */
  16265.         bp = tmp; blen = 0;
  16266.         srp_put (wp->data, &bp, wp->len, &blen);
  16267.         len = sizeof(buf);
  16268.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16269.           goto encode_error;
  16270.         reply_parse = "ADAT=";
  16271.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16272.     }
  16273.     if (n == REPLY_CONTINUE) {          /* Get yp */
  16274.         bp = buf;
  16275.         if (!reply_parse)
  16276.           goto data_error;
  16277.         blen = sizeof(buf);
  16278.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16279.           goto decode_error;
  16280.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  16281.           goto data_error;
  16282.         if (!srp_pass) {
  16283.             static char ftppass[80];
  16284.             int ok;
  16285.             setint();
  16286.             ok = uq_txt(NULL," SRP Password: ",2,NULL,ftppass,80,NULL);
  16287.             if (ok)
  16288.                 srp_pass = ftppass;
  16289.         }
  16290.         t_clientpasswd (tc, srp_pass);
  16291.         memset (srp_pass, 0, strlen (srp_pass));
  16292.         skey = t_clientgetkey (tc, &yp); /* Send response */
  16293.         bp = tmp; blen = 0;
  16294.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  16295.         len = sizeof(buf);
  16296.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16297.           goto encode_error;
  16298.         reply_parse = "ADAT=";
  16299.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16300.     }
  16301.     if (n == REPLY_CONTINUE) {          /* Get response */
  16302.         bp = buf;
  16303.         if (!reply_parse)
  16304.           goto data_error;
  16305.         blen = sizeof(buf);
  16306.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16307.           goto encode_error;
  16308.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  16309.           goto data_error;
  16310.         if (t_clientverify (tc, cp)) {
  16311.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  16312.             goto bad;
  16313.         }
  16314.         bp = tmp; blen = 0;             /* Send nothing */
  16315.         srp_put ("\0", &bp, 1, &blen);
  16316.         len = sizeof(buf);
  16317.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16318.           goto encode_error;
  16319.         reply_parse = "ADAT=";
  16320.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16321.     }
  16322.     if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
  16323.         CHAR seqnum[4];
  16324.         CHAR *clist;
  16325.         CHAR *hlist;
  16326.         CHAR *p1;
  16327.         int clist_len, hlist_len;
  16328.         bp = buf;
  16329.         if (!reply_parse)
  16330.           goto data_error;
  16331.         blen = sizeof(buf);
  16332.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16333.           goto encode_error;
  16334.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  16335.           goto data_error;
  16336.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  16337.           goto data_error;
  16338.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16339.           goto data_error;
  16340.         memcpy (seqnum, cp, 4);
  16341.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  16342.           cid = srp_pref_cipher;
  16343.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  16344.           cid = SRP_DEFAULT_CIPHER;
  16345.         if (!cid) {
  16346.             CHAR *loclist = cipher_getlist ();
  16347.             for (i = 0; i < strlen (loclist); i++)
  16348.               if (cipher_supported (clist, loclist[i])) {
  16349.                   cid = loclist[i];
  16350.                   break;
  16351.               }
  16352.         }
  16353.         if (!cid) {
  16354.             fprintf (stderr, "Unable to agree on cipher.\n");
  16355.             goto bad;
  16356.         }
  16357.         /* Choose hash */
  16358.  
  16359.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  16360.           hid = srp_pref_hash;
  16361.  
  16362.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  16363.           hid = SRP_DEFAULT_HASH;
  16364.  
  16365.         if (!hid) {
  16366.             CHAR *loclist = hash_getlist ();
  16367.             for (i = 0; i < strlen (loclist); i++)
  16368.               if (hash_supported (hlist, loclist[i])) {
  16369.                   hid = loclist[i];
  16370.                   break;
  16371.               }
  16372.         }
  16373.         if (!hid) {
  16374.             fprintf (stderr, "Unable to agree on hash.\n");
  16375.             goto bad;
  16376.         }
  16377.         /* Set incrypt */
  16378.  
  16379.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  16380.                                     KRYPTO_DECODE)))
  16381.           goto bad;
  16382.  
  16383.         /* Generate random number for outkey and outseqnum */
  16384.  
  16385.         t_random (seqnum, 4);
  16386.  
  16387.         /* Send cid, hid, outkey, outseqnum */
  16388.  
  16389.         bp = tmp; blen = 0;
  16390.         srp_put (&cid, &bp, 1, &blen);
  16391.         srp_put (&hid, &bp, 1, &blen);
  16392.         srp_put (seqnum, &bp, 4, &blen);
  16393.         len = sizeof(buf);
  16394.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16395.           goto encode_error;
  16396.         reply_parse = "ADAT=";
  16397.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16398.  
  16399.         /* Set outcrypt */
  16400.  
  16401.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  16402.                                      KRYPTO_ENCODE)))
  16403.           goto bad;
  16404.  
  16405.         t_clientclose (tc);
  16406.         tc = NULL;
  16407.     }
  16408.     if (n != REPLY_COMPLETE)
  16409.       goto bad;
  16410.  
  16411.     if (ftp_vbm) {
  16412.         if (ftp_deb)
  16413.           printf("\n");
  16414.         printf ("SRP authentication succeeded.\n");
  16415.         printf ("Using cipher %s and hash function %s.\n",
  16416.                 (cipher_getdescbyid(cid))->name,
  16417.                 (hash_getdescbyid(hid))->name
  16418.                 );
  16419.     }
  16420.     reply_parse = NULL;
  16421.     auth_type = "SRP";
  16422.     return(1);
  16423.  
  16424.   encode_error:
  16425.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  16426.     goto bad;
  16427.  
  16428.   decode_error:
  16429.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  16430.     goto bad;
  16431.  
  16432.   data_error:
  16433.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  16434.     goto bad;
  16435.  
  16436.   bad:
  16437.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  16438.     reply_parse = NULL;
  16439.     return(0);
  16440. }
  16441.  
  16442. /*--------------------------------------------------------------+
  16443.  | srp_put: put item to send buffer                             |
  16444.  +--------------------------------------------------------------*/
  16445. static int
  16446. srp_put (in, out, inlen, outlen)
  16447.     CHAR *in;
  16448.     CHAR **out;
  16449.     int inlen;
  16450.     int *outlen;
  16451. {
  16452.     srp_uint32 net_len;
  16453.  
  16454.     net_len = htonl (inlen);
  16455.     memcpy (*out, &net_len, 4);
  16456.  
  16457.     *out += 4; *outlen += 4;
  16458.  
  16459.     memcpy (*out, in, inlen);
  16460.  
  16461.     *out += inlen; *outlen += inlen;
  16462.     return(0);
  16463. }
  16464.  
  16465. /*--------------------------------------------------------------+
  16466.  | srp_get: get item from receive buffer                        |
  16467.  +--------------------------------------------------------------*/
  16468. static int
  16469. srp_get (in, out, inlen, outlen)
  16470.     CHAR **in;
  16471.     CHAR **out;
  16472.     int *inlen;
  16473.     int *outlen;
  16474. {
  16475.     srp_uint32 net_len;
  16476.  
  16477.     if (*inlen < 4) return -1;
  16478.  
  16479.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  16480.     *outlen = ntohl (net_len);
  16481.  
  16482.     if (*inlen < *outlen) return -1;
  16483.  
  16484.     *out = *in; *inlen -= *outlen; *in += *outlen;
  16485.  
  16486.     return *outlen;
  16487. }
  16488.  
  16489. /*--------------------------------------------------------------+
  16490.  | srp_encode: encode control message                           |
  16491.  +--------------------------------------------------------------*/
  16492. static int
  16493. srp_encode (private, in, out, len)
  16494.     int private;
  16495.     CHAR *in;
  16496.     CHAR *out;
  16497.     unsigned len;
  16498. {
  16499.     if (private)
  16500.       return krypto_msg_priv (outcrypt, in, out, len);
  16501.     else
  16502.       return krypto_msg_safe (outcrypt, in, out, len);
  16503. }
  16504.  
  16505. /*--------------------------------------------------------------+
  16506.  | srp_decode: decode control message                           |
  16507.  +--------------------------------------------------------------*/
  16508. static int
  16509. srp_decode (private, in, out, len)
  16510.     int private;
  16511.     CHAR *in;
  16512.     CHAR *out;
  16513.     unsigned len;
  16514. {
  16515.     if (private)
  16516.       return krypto_msg_priv (incrypt, in, out, len);
  16517.     else
  16518.       return krypto_msg_safe (incrypt, in, out, len);
  16519. }
  16520.  
  16521. #endif /* FTP_SRP */
  16522.  
  16523.  
  16524.  
  16525. #ifdef NOT_USED
  16526. /*
  16527.   The following code is from the Unix FTP client.  Be sure to
  16528.   make sure that the functionality is not lost.  Especially
  16529.   the Proxy stuff even though we have not yet implemented it.
  16530. */
  16531.  
  16532. /* Send multiple files  */
  16533.  
  16534. static int
  16535. ftp_mput(argc, argv) int argc; char **argv; {
  16536.     register int i;
  16537.     sig_t oldintr;
  16538.     int ointer;
  16539.     char *tp;
  16540.     sigtype mcancel();
  16541.  
  16542.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  16543.         printf("usage: %s local-files\n", argv[0]);
  16544.         ftpcode = -1;
  16545.         return;
  16546.     }
  16547.     mname = argv[0];
  16548.     mflag = 1;
  16549.     oldintr = signal(SIGINT, mcancel);
  16550.  
  16551.     /* Replace with calls to cc_execute() */
  16552.     setjmp(jcancel);
  16553. #ifdef FTP_PROXY
  16554.     if (proxy) {
  16555.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  16556.  
  16557.         while ((cp = remglob(argv,0)) != NULL) {
  16558.             if (*cp == 0) {
  16559.                 mflag = 0;
  16560.                 continue;
  16561.             }
  16562.             if (mflag && confirm(argv[0], cp)) {
  16563.                 tp = cp;
  16564.                 if (mcase) {
  16565.                     while (*tp && !islower(*tp)) {
  16566.                         tp++;
  16567.                     }
  16568.                     if (!*tp) {
  16569.                         tp = cp;
  16570.                         tp2 = tmpbuf;
  16571.                         while ((*tp2 = *tp) != 0) {
  16572.                             if (isupper(*tp2)) {
  16573.                                 *tp2 = 'a' + *tp2 - 'A';
  16574.                             }
  16575.                             tp++;
  16576.                             tp2++;
  16577.                         }
  16578.                     }
  16579.                     tp = tmpbuf;
  16580.                 }
  16581.                 if (ntflag) {
  16582.                     tp = dotrans(tp);
  16583.                 }
  16584.                 if (mapflag) {
  16585.                     tp = domap(tp);
  16586.                 }
  16587.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  16588.                 if (!mflag && fromatty) {
  16589.                     ointer = interactive;
  16590.                     interactive = 1;
  16591.                     if (confirm("Continue with","mput")) {
  16592.                         mflag++;
  16593.                     }
  16594.                     interactive = ointer;
  16595.                 }
  16596.             }
  16597.         }
  16598.         signal(SIGINT, oldintr);
  16599.         mflag = 0;
  16600.         return;
  16601.     }
  16602. #endif /* FTP_PROXY */
  16603.     for (i = 1; i < argc; i++) {
  16604.         register char **cpp, **gargs;
  16605.  
  16606.         if (mflag && confirm(argv[0], argv[i])) {
  16607.             tp = argv[i];
  16608.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  16609.             if (!mflag && fromatty) {
  16610.                 ointer = interactive;
  16611.                 interactive = 1;
  16612.                 if (confirm("Continue with","mput")) {
  16613.                     mflag++;
  16614.                 }
  16615.                 interactive = ointer;
  16616.             }
  16617.         }
  16618.         continue;
  16619.  
  16620.         gargs = ftpglob(argv[i]);
  16621.         if (globerr != NULL) {
  16622.             printf("%s\n", globerr);
  16623.             if (gargs) {
  16624.                 blkfree(gargs);
  16625.                 free((char *)gargs);
  16626.             }
  16627.             continue;
  16628.         }
  16629.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  16630.             if (mflag && confirm(argv[0], *cpp)) {
  16631.                 tp = *cpp;
  16632.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  16633.                 if (!mflag && fromatty) {
  16634.                     ointer = interactive;
  16635.                     interactive = 1;
  16636.                     if (confirm("Continue with","mput")) {
  16637.                         mflag++;
  16638.                     }
  16639.                     interactive = ointer;
  16640.                 }
  16641.             }
  16642.         }
  16643.         if (gargs != NULL) {
  16644.             blkfree(gargs);
  16645.             free((char *)gargs);
  16646.         }
  16647.     }
  16648.     signal(SIGINT, oldintr);
  16649.     mflag = 0;
  16650. }
  16651.  
  16652. /* Get multiple files */
  16653.  
  16654. static int
  16655. ftp_mget(argc, argv) int argc; char **argv; {
  16656.     int rc = -1;
  16657.     sig_t oldintr;
  16658.     int ointer;
  16659.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  16660.     sigtype mcancel();
  16661.  
  16662.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  16663.         printf("usage: %s remote-files\n", argv[0]);
  16664.         ftpcode = -1;
  16665.         return(-1);
  16666.     }
  16667.     mname = argv[0];
  16668.     mflag = 1;
  16669.     oldintr = signal(SIGINT,mcancel);
  16670.     /* Replace with calls to cc_execute() */
  16671.     setjmp(jcancel);
  16672.     while ((cp = remglob(argv,proxy)) != NULL) {
  16673.         if (*cp == '\0') {
  16674.             mflag = 0;
  16675.             continue;
  16676.         }
  16677.         if (mflag && confirm(argv[0], cp)) {
  16678.             tp = cp;
  16679.             if (mcase) {
  16680.                 while (*tp && !islower(*tp)) {
  16681.                     tp++;
  16682.                 }
  16683.                 if (!*tp) {
  16684.                     tp = cp;
  16685.                     tp2 = tmpbuf;
  16686.                     while ((*tp2 = *tp) != 0) {
  16687.                         if (isupper(*tp2)) {
  16688.                             *tp2 = 'a' + *tp2 - 'A';
  16689.                         }
  16690.                         tp++;
  16691.                         tp2++;
  16692.                     }
  16693.                 }
  16694.                 tp = tmpbuf;
  16695.             }
  16696.             rc = (recvrequest("RETR", tp, cp, "wb",
  16697.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  16698.             if (!mflag && fromatty) {
  16699.                 ointer = interactive;
  16700.                 interactive = 1;
  16701.                 if (confirm("Continue with","mget")) {
  16702.                     mflag++;
  16703.                 }
  16704.                 interactive = ointer;
  16705.             }
  16706.         }
  16707.     }
  16708.     signal(SIGINT,oldintr);
  16709.     mflag = 0;
  16710.     return(rc);
  16711. }
  16712.  
  16713. /* Delete multiple files */
  16714.  
  16715. static int
  16716. mdelete(argc, argv) int argc; char **argv; {
  16717.     sig_t oldintr;
  16718.     int ointer;
  16719.     char *cp;
  16720.     sigtype mcancel();
  16721.  
  16722.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  16723.         printf("usage: %s remote-files\n", argv[0]);
  16724.         ftpcode = -1;
  16725.         return(-1);
  16726.     }
  16727.     mname = argv[0];
  16728.     mflag = 1;
  16729.     oldintr = signal(SIGINT, mcancel);
  16730.     /* Replace with calls to cc_execute() */
  16731.     setjmp(jcancel);
  16732.     while ((cp = remglob(argv,0)) != NULL) {
  16733.         if (*cp == '\0') {
  16734.             mflag = 0;
  16735.             continue;
  16736.         }
  16737.         if (mflag && confirm(argv[0], cp)) {
  16738.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  16739.             if (!mflag && fromatty) {
  16740.                 ointer = interactive;
  16741.                 interactive = 1;
  16742.                 if (confirm("Continue with", "mdelete")) {
  16743.                     mflag++;
  16744.                 }
  16745.                 interactive = ointer;
  16746.             }
  16747.         }
  16748.     }
  16749.     signal(SIGINT, oldintr);
  16750.     mflag = 0;
  16751.     return(rc);
  16752. }
  16753.  
  16754. /* Get a directory listing of multiple remote files */
  16755.  
  16756. static int
  16757. mls(argc, argv) int argc; char **argv; {
  16758.     sig_t oldintr;
  16759.     int ointer, i;
  16760.     char *cmd, mode[1], *dest;
  16761.     sigtype mcancel();
  16762.     int rc = -1;
  16763.  
  16764.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  16765.       goto usage;
  16766.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  16767.       usage:
  16768.         printf("usage: %s remote-files local-file\n", argv[0]);
  16769.         ftpcode = -1;
  16770.         return(-1);
  16771.     }
  16772.     dest = argv[argc - 1];
  16773.     argv[argc - 1] = NULL;
  16774.     if (strcmp(dest, "-") && *dest != '|')
  16775.       if (!globulize(&dest) ||
  16776.           !confirm("output to local-file:", dest)) {
  16777.           ftpcode = -1;
  16778.           return(-1);
  16779.       }
  16780.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  16781.     mname = argv[0];
  16782.     mflag = 1;
  16783.     oldintr = signal(SIGINT, mcancel);
  16784.     /* Replace with calls to cc_execute() */
  16785.     setjmp(jcancel);
  16786.     for (i = 1; mflag && i < argc-1; ++i) {
  16787.         *mode = (i == 1) ? 'w' : 'a';
  16788.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  16789.         if (!mflag && fromatty) {
  16790.             ointer = interactive;
  16791.             interactive = 1;
  16792.             if (confirm("Continue with", argv[0])) {
  16793.                 mflag ++;
  16794.             }
  16795.             interactive = ointer;
  16796.         }
  16797.     }
  16798.     signal(SIGINT, oldintr);
  16799.     mflag = 0;
  16800.     return(rc);
  16801. }
  16802.  
  16803. static char *
  16804. remglob(argv,doswitch) char *argv[]; int doswitch; {
  16805.     char temp[16];
  16806.     static char buf[CKMAXPATH];
  16807.     static FILE *ftemp = NULL;
  16808.     static char **args;
  16809.     int oldhash;
  16810.     char *cp, *mode;
  16811.  
  16812.     if (!mflag) {
  16813.         if (!doglob) {
  16814.             args = NULL;
  16815.         } else {
  16816.             if (ftemp) {
  16817.                 (void) fclose(ftemp);
  16818.                 ftemp = NULL;
  16819.             }
  16820.         }
  16821.         return(NULL);
  16822.     }
  16823.     if (!doglob) {
  16824.         if (args == NULL)
  16825.           args = argv;
  16826.         if ((cp = *++args) == NULL)
  16827.           args = NULL;
  16828.         return(cp);
  16829.     }
  16830.     if (ftemp == NULL) {
  16831.         (void) strcpy(temp, _PATH_TMP);
  16832. #ifdef MKTEMP
  16833. #ifndef MKSTEMP
  16834.         (void) mktemp(temp);
  16835. #endif /* MKSTEMP */
  16836. #endif /* MKTEMP */
  16837.         verbose = 0;
  16838.         oldhash = hash, hash = 0;
  16839. #ifdef FTP_PROXY
  16840.         if (doswitch) {
  16841.             pswitch(!proxy);
  16842.         }
  16843. #endif /* FTP_PROXY */
  16844.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  16845.           recvrequest ("NLST", temp, *argv, mode, 0);
  16846. #ifdef FTP_PROXY
  16847.         if (doswitch) {
  16848.             pswitch(!proxy);
  16849.         }
  16850. #endif /* FTP_PROXY */
  16851.         hash = oldhash;
  16852.         ftemp = fopen(temp, "r");
  16853.         unlink(temp);
  16854.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  16855.             printf("Can't find list of remote files, oops\n");
  16856.             return(NULL);
  16857.         }
  16858.     }
  16859.     if (fgets(buf, sizeof (buf), ftemp) == NULL) {
  16860.         fclose(ftemp), ftemp = NULL;
  16861.         return(NULL);
  16862.     }
  16863.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  16864.       *cp = '\0';
  16865.     return(buf);
  16866. }
  16867. #endif /* NOT_USED */
  16868. #endif /* TCPSOCKET (top of file) */
  16869. #endif /* SYSFTP (top of file) */
  16870. #endif /* NOFTP (top of file) */
  16871.