home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku209.tar / ckcftp.c < prev    next >
C/C++ Source or Header  |  2003-03-26  |  549KB  |  17,036 lines

  1. /*  C K C F T P  --  FTP Client for C-Kermit  */
  2.  
  3. char *ckftpv = "FTP Client, 8.0.217, 18 Mar 2003";
  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, 2003,
  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. #define PWDSIZ 256
  545. #define PASSBUFSIZ 256
  546. #define PROMPTSIZ 256
  547.  
  548. #ifndef MGETMAX                         /* Max operands for MGET command */
  549. #define MGETMAX 1000
  550. #endif /* MGETMAX */
  551.  
  552. #ifdef FTP_SRP
  553. #define FUDGE_FACTOR 100
  554. #endif /* FTP_SRP */
  555.  
  556. /*
  557.   Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  558.   number bytes.  Must be defined for each auth type.
  559.   GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  560.   3DES requires 56 bytes.  Lets use 96 just to be sure.
  561. */
  562. #ifdef FTP_GSSAPI
  563. #ifndef FUDGE_FACTOR
  564. #define FUDGE_FACTOR 96
  565. #endif /* FUDGE_FACTOR */
  566. #endif /* FTP_GSSAPI */
  567.  
  568. #ifdef FTP_KRB4
  569. #ifndef FUDGE_FACTOR
  570. #define FUDGE_FACTOR 32
  571. #endif /* FUDGE_FACTOR */
  572. #endif /* FTP_KRB4 */
  573.  
  574. #ifndef FUDGE_FACTOR                    /* In case no auth types define it */
  575. #define FUDGE_FACTOR 0
  576. #endif /* FUDGE_FACTOR */
  577.  
  578. #ifndef MAXHOSTNAMELEN
  579. #define MAXHOSTNAMELEN 64
  580. #endif /* MAXHOSTNAMELEN */
  581. #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
  582.  
  583. /* Fascist compiler toadying */
  584.  
  585. #ifndef SENDARG2TYPE
  586. #ifdef COMMENT                          /* Might be needed here and there */
  587. #define SENDARG2TYPE const char *
  588. #else
  589. #define SENDARG2TYPE char *
  590. #endif /* COMMENT */
  591. #endif /* SENDARG2TYPE */
  592.  
  593. /* Common text messages */
  594.  
  595. static char *nocx = "?No FTP control connection\n";
  596.  
  597. static char *fncnam[] = {
  598.   "rename", "overwrite", "backup", "append", "discard", "ask", "update",
  599.   "dates-differ", ""
  600. };
  601.  
  602. /* Macro definitions */
  603.  
  604. /* Used to speed up text-mode PUTs */
  605. #define zzout(fd,c) \
  606. ((fd<0)?(-1):((nout>=ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
  607.  
  608. #define CHECKCONN() if(!connected){printf(nocx);return(-9);}
  609.  
  610. /* Externals */
  611.  
  612. #ifdef CK_URL
  613. extern struct urldata g_url;
  614. #endif /* CK_URL */
  615.  
  616. #ifdef DYNAMIC
  617. extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
  618. #else
  619. extern char zinbuffer[], zoutbuffer[];
  620. #endif /* DYNAMIC */
  621. extern char *zinptr, *zoutptr;
  622. extern int zincnt, zoutcnt, zobufsize, fncact;
  623.  
  624. #ifdef CK_TMPDIR
  625. extern int f_tmpdir;                    /* Directory changed temporarily */
  626. extern char savdir[];                   /* For saving current directory */
  627. extern char * dldir;
  628. #endif /* CK_TMPDIR */
  629.  
  630. extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
  631.  
  632. extern xx_strp xxstring;
  633. extern struct keytab onoff[], txtbin[], rpathtab[];
  634. extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
  635. extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
  636. extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
  637. extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
  638. extern int nolinks, msgflg, keep;
  639. extern long fsize, ffc, tfc, filcnt, xfsecs, tfcps, cps, oldcps;
  640. #ifdef GFTIMER
  641. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  642. #else
  643. extern long xfsecs;
  644. #endif /* GFTIMER */
  645.  
  646. extern char filnam[], * filefile, myhost[];
  647. extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
  648. extern int g_skipbup, skipbup, sendmode;
  649. extern int g_displa, fdispla, displa;
  650.  
  651. #ifdef LOCUS
  652. extern int locus, autolocus;
  653. #endif /* LOCUS */
  654.  
  655. #ifndef NOCSETS
  656. extern int nfilc, dcset7, dcset8, fileorder;
  657. extern struct csinfo fcsinfo[];
  658. extern struct keytab fcstab[];
  659. extern int fcharset;
  660. #endif /* NOCSETS */
  661.  
  662. extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
  663. extern char sndnbefore[], sndnafter[], *rcvexcept[];
  664. extern CHAR feol;
  665. extern long sendstart, sndsmaller, sndlarger, rs_len;
  666.  
  667. extern char * remdest;
  668. extern int remfile, remappd, rempipe;
  669.  
  670. #ifndef NOSPL
  671. extern int cmd_quoting;
  672. #ifdef PUTARRAY
  673. extern int sndxlo, sndxhi, sndxin;
  674. extern char sndxnam[];
  675. extern char **a_ptr[];                  /* Array pointers */
  676. extern int a_dim[];                     /* Array dimensions */
  677. #endif /* PUTARRAY */
  678. #endif /* NOSPL */
  679.  
  680. #ifndef NOMSEND                         /* MPUT and ADD SEND-LIST lists */
  681. extern char *msfiles[];
  682. extern int filesinlist;
  683. extern struct filelist * filehead;
  684. extern struct filelist * filetail;
  685. extern struct filelist * filenext;
  686. extern int addlist;
  687. extern char fspec[];                    /* Most recent filespec */
  688. extern int fspeclen;                    /* Length of fspec[] buffer */
  689. #endif /* NOMSEND */
  690.  
  691. extern int pipesend;
  692. #ifdef PIPESEND
  693. extern char * sndfilter, * rcvfilter;
  694. #endif /* PIPESEND */
  695.  
  696. #ifdef CKROOT
  697. extern int ckrooterr;
  698. #endif /* CKROOT */
  699.  
  700. #ifdef KRB4
  701. extern int krb4_autoget;
  702. _PROTOTYP(char * ck_krb4_realmofhost,(char *));
  703. #endif /* KRB4 */
  704.  
  705. #ifdef KRB5
  706. extern int krb5_autoget;
  707. extern int krb5_d_no_addresses;
  708. _PROTOTYP(char * ck_krb5_realmofhost,(char *));
  709. #endif /* KRB5 */
  710.  
  711. #ifdef DCMDBUF
  712. extern char *atmbuf;                    /* Atom buffer (malloc'd) */
  713. extern char *cmdbuf;                    /* Command buffer (malloc'd) */
  714. extern char *line;                      /* Big string buffer #1 */
  715. extern char *tmpbuf;                    /* Big string buffer #2 */
  716. #else
  717. extern char atmbuf[];                   /* The same, but static */
  718. extern char cmdbuf[];
  719. extern char line[];
  720. extern char tmpbuf[];
  721. #endif /* DCMDBUF */
  722.  
  723. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  724.  
  725. /* Public variables declared here */
  726.  
  727. #ifdef NOXFER
  728. int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
  729. #else
  730. int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
  731. #endif /* NOXFER */
  732. int ftpcode = -1;                       /* Last FTP response code */
  733. int ftp_cmdlin = 0;                     /* FTP invoked from command line */
  734. int ftp_fai = 0;                        /* FTP failure count */
  735. int ftp_deb = 0;                        /* FTP debugging */
  736. int ftp_dis = -1;            /* FTP display style */
  737. int ftp_log = 1;                        /* FTP Auto-login */
  738. int sav_log = -1;
  739. int ftp_action = 0;                     /* FTP action from command line */
  740. int ftp_dates = 1;                      /* Set file dates from server */
  741.  
  742. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  743. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  744. char ftp_user_host[MAX_DNS_NAMELEN]= ""; /* FTP hostname specified by user */
  745. char * ftp_host = NULL;                 /* FTP hostname */
  746. char * ftp_logname = NULL;              /* FTP username */
  747. char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
  748. char * ftp_apw = NULL;            /* Anonymous password */
  749.  
  750. /* Static global variables */
  751.  
  752. static char ftpsndbuf[FTP_BUFSIZ+64];
  753.  
  754. static char * fts_sto = NULL;
  755. /*
  756.   This is just a first stab -- these strings should match how the
  757.   corresponding FTP servers identify themselves.
  758. */
  759. #ifdef UNIX
  760. static char * myostype = "UNIX";
  761. #else
  762. #ifdef VMS
  763. /* not yet... */
  764. static char * myostype = "VMS";
  765. #else
  766. #ifdef OS2
  767. #ifdef NT
  768. static char * myostype = "WIN32";
  769. #else
  770. static char * myostype = "OS/2";
  771. #endif /* NT */
  772. #else
  773. static char * myostype = "UNSUPPORTED";
  774. #endif /* OS2  */
  775. #endif /* VMS */
  776. #endif /* UNIX */
  777.  
  778. static int noinit = 0;                  /* Don't send REST, STRU, MODE */
  779. static int alike = 0;                   /* Client/server like platforms */
  780. static int local = 1;                   /* Shadows Kermit global 'local' */
  781. static int dout = -1;                   /* Data connection file descriptor */
  782. static int dpyactive = 0;               /* Data transfer is active */
  783. static int globaldin = -1;              /* Data connection f.d. */
  784. static int out2screen = 0;              /* GET output is to screen */
  785. static int forcetype = 0;               /* Force text or binary mode */
  786. static int cancelfile = 0;              /* File canceled */
  787. static int cancelgroup = 0;             /* Group canceled */
  788. static int anonymous = 0;               /* Logging in as anonymous */
  789. static int loggedin = 0;                /* Logged in (or not) */
  790. static int puterror = 0;                /* What to do on PUT error */
  791. static int geterror = 0;                /* What to do on GET error */
  792. static int rfrc = 0;                    /* remote_files() return code */
  793. static int okrestart = 0;               /* Server understands REST */
  794. static int printlines = 0;              /* getreply()should print data lines */
  795. static int haveurl = 0;                 /* Invoked by command-line FTP URL */
  796. static int mdtmok = 1;            /* Server supports MDTM */
  797. static int sizeok = 1;
  798. static int featok = 1;
  799. static int mlstok = 1;
  800. static int stouarg = 1;
  801. static int typesent = 0;
  802. static int havesigint = 0;
  803. static long havetype =  0;
  804. static long havesize = -1L;
  805. static char * havemdtm = NULL;
  806. static int mgetmethod = 0;        /* NLST or MLSD */
  807. static int mgetforced = 0;
  808.  
  809. static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
  810. static int c0, c1;                      /* Temp variables for characters */
  811.  
  812. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  813. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  814.  
  815. #define RFNBUFSIZ 4096            /* Remote filename buffer size */
  816.  
  817. static unsigned int maxbuf = 0, actualbuf = 0;
  818. static CHAR *ucbuf = NULL;
  819. static int ucbufsiz = 0;
  820. static unsigned int nout = 0;           /* Number of chars in ucbuf */
  821.  
  822. static jmp_buf recvcancel;
  823. static jmp_buf sendcancel;
  824. static jmp_buf ptcancel;
  825. static jmp_buf jcancel;
  826. static int ptabflg = 0;
  827.  
  828. /* Protection level symbols */
  829.  
  830. #define FPL_CLR 1                       /* Clear */
  831. #define FPL_SAF 2                       /* Safe */
  832. #define FPL_PRV 3                       /* Private */
  833. #define FPL_CON 4                       /* Confidential */
  834.  
  835. /* Symbols for file types returned by MLST/MLSD */
  836.  
  837. #define FTYP_FILE 1            /* Regular file */
  838. #define FTYP_DIR  2            /* Directory */
  839. #define FTYP_CDIR 3            /* Current directory */
  840. #define FTYP_PDIR 4            /* Parent directory */
  841.  
  842. /* File type symbols keyed to the file-type symbols from ckcker.h */
  843.  
  844. #define FTT_ASC XYFT_T                  /* ASCII (text) */
  845. #define FTT_BIN XYFT_B                  /* Binary (image) */
  846. #define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
  847.  
  848. /* Server feature table - sfttab[0] > 0 means server supports FEAT and OPTS */
  849.  
  850. static int sfttab[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  851.  
  852. #define SFT_AUTH  1            /* FTP server feature codes */
  853. #define SFT_LANG  2
  854. #define SFT_MDTM  3
  855. #define SFT_MLST  4
  856. #define SFT_PBSZ  5
  857. #define SFT_PROT  6
  858. #define SFT_REST  7
  859. #define SFT_SIZE  8
  860. #define SFT_TVFS  9
  861. #define SFT_UTF8 10
  862.  
  863. #define CNV_AUTO  2            /* FTP filename conversion */
  864. #define CNV_CNV   1
  865. #define CNV_LIT   0
  866.  
  867. /* SET FTP values */
  868.  
  869. static int                              /* SET FTP values... */
  870.   ftp_aut = 1,                          /* Auto-authentication */
  871. #ifdef FTP_SECURITY
  872.   ftp_cry = 1,                          /* Auto-encryption */
  873.   ftp_cfw = 0,                          /* Credential forwarding */
  874. #endif /* FTP_SECURITY */
  875.   ftp_cpl = FPL_CLR,                    /* Command protection level */
  876.   ftp_dpl = FPL_CLR,                    /* Data protection level */
  877. #ifdef FTP_PROXY
  878.   ftp_prx = 0,                          /* Use proxy */
  879. #endif /* FTP_PROXY */
  880.   sav_psv = -1,                         /* For saving passive mode */
  881.   ftp_psv = 1,                          /* Passive mode */
  882.   ftp_spc = 1,                          /* Send port commands */
  883.   ftp_typ = FTT_ASC,                    /* Type */
  884.   get_auto = 1,                         /* Automatic type switching for GET */
  885.   tenex = 0,                            /* Type is Tenex */
  886.   ftp_usn = 0,                          /* Unique server names */
  887.   ftp_prm = 0,                          /* Permissions */
  888.   ftp_cnv = CNV_AUTO,            /* Filename conversion (2 = auto) */
  889.   ftp_vbm = DEF_VBM,                    /* Verbose mode */
  890.   ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  891.   ftp_err = 0,                          /* Error action */
  892.   ftp_fnc = -1;                         /* Filename collision action */
  893.  
  894. static int
  895. #ifdef NOCSETS
  896.   ftp_csr = -1,                         /* Remote (server) character set */
  897. #else
  898.   ftp_csr = FC_UTF8,
  899. #endif /* NOCSETS */
  900.   ftp_xla = 0;                          /* Character-set translation on/off */
  901. int
  902.   ftp_csx = -1,                         /* Remote charset currently in use */
  903.   ftp_csl = -1;                         /* Local charset currently in use */
  904.  
  905. static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
  906.  
  907. char * ftp_nml = NULL;                  /* /NAMELIST */
  908. char * ftp_tmp = NULL;                  /* Temporary string */
  909. static char * ftp_acc = NULL;           /* Account string */
  910. static char * auth_type = NULL;         /* Authentication type */
  911. static char * srv_renam = NULL;         /* Server-rename string */
  912. FILE * fp_nml = NULL;                   /* Namelist file pointer */
  913.  
  914. static int csocket = -1;                /* Control socket */
  915. static int connected = 0;               /* Connected to FTP server */
  916. static short ftp_port = 0;              /* FTP port */
  917. #ifdef FTPHOST
  918. static int hostcmd = 0;                 /* Has HOST command been sent */
  919. #endif /* FTPHOST */
  920. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  921. static char bytename[8];
  922.  
  923. /* For parsing replies to FTP server command */
  924. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  925.  
  926. #ifdef FTP_PROXY
  927. static int proxy, unix_proxy
  928. #endif /* FTP_PROXY */
  929.  
  930. static char pasv[64];                   /* Passive-mode port */
  931. static int passivemode = 0;
  932. static int sendport = 0;
  933. static int servertype = 0;              /* FTP server's OS type */
  934.  
  935. static int testing = 0;
  936. static char ftpcmdbuf[FTP_BUFSIZ];
  937.  
  938. /* Macro definitions */
  939.  
  940. #define UC(b) ckitoa(((int)b)&0xff)
  941. #define nz(x) ((x) == 0 ? 1 : (x))
  942.  
  943. /* Command tables and definitions */
  944.  
  945. #define FTP_ACC  1                      /* FTP command keyword codes */
  946. #define FTP_APP  2
  947. #define FTP_CWD  3
  948. #define FTP_CHM  4
  949. #define FTP_CLS  5
  950. #define FTP_DEL  6
  951. #define FTP_DIR  7
  952. #define FTP_GET  8
  953. #define FTP_IDL  9
  954. #define FTP_MDE 10
  955. #define FTP_MDI 11
  956. #define FTP_MGE 12
  957. #define FTP_MKD 13
  958. #define FTP_MOD 14
  959. #define FTP_MPU 15
  960. #define FTP_OPN 16
  961. #define FTP_PUT 17
  962. #define FTP_PWD 18
  963. #define FTP_RGE 19
  964. #define FTP_REN 20
  965. #define FTP_RES 21
  966. #define FTP_HLP 22
  967. #define FTP_RMD 23
  968. #define FTP_STA 24
  969. #define FTP_SIT 25
  970. #define FTP_SIZ 26
  971. #define FTP_SYS 27
  972. #define FTP_UMA 28
  973. #define FTP_GUP 29
  974. #define FTP_USR 30
  975. #define FTP_QUO 31
  976. #define FTP_TYP 32
  977. #define FTP_FEA 33
  978. #define FTP_OPT 34
  979. #define FTP_CHK 35
  980. #define FTP_VDI 36
  981. #define FTP_ENA 37
  982. #define FTP_DIS 38
  983.  
  984. struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
  985.     { "auto",    2, 0 },
  986.     { "ftp",     1, 0 },
  987.     { "kermit",  0, 0  }
  988. };
  989.  
  990. static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
  991.     { "proceed", 0, 0 },                /* 0 = proceed */
  992.     { "quit",    1, 0 }                 /* 1 = quit */
  993. };
  994.  
  995. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  996.     { "account",   FTP_ACC, 0 },
  997.     { "append",    FTP_APP, 0 },
  998.     { "bye",       FTP_CLS, 0 },
  999.     { "cd",        FTP_CWD, 0 },
  1000.     { "cdup",      FTP_GUP, 0 },
  1001.     { "check",     FTP_CHK, 0 },
  1002.     { "chmod",     FTP_CHM, 0 },
  1003.     { "close",     FTP_CLS, 0 },
  1004.     { "cwd",       FTP_CWD, CM_INV },
  1005.     { "delete",    FTP_MDE, 0 },
  1006.     { "directory", FTP_DIR, 0 },
  1007.     { "disable",   FTP_DIS, 0 },
  1008.     { "enable",    FTP_ENA, 0 },
  1009.     { "features",  FTP_FEA, 0 },
  1010.     { "get",       FTP_GET, 0 },
  1011.     { "help",      FTP_HLP, 0 },
  1012.     { "idle",      FTP_IDL, 0 },
  1013.     { "login",     FTP_USR, CM_INV },
  1014.     { "mdelete",   FTP_MDE, CM_INV },
  1015.     { "mget",      FTP_MGE, 0 },
  1016.     { "mkdir",     FTP_MKD, 0 },
  1017.     { "modtime",   FTP_MOD, 0 },
  1018.     { "mput",      FTP_MPU, 0 },
  1019.     { "open",      FTP_OPN, 0 },
  1020.     { "opt",       FTP_OPT, CM_INV|CM_ABR },
  1021.     { "opts",      FTP_OPT, CM_INV },
  1022.     { "options",   FTP_OPT, 0 },
  1023.     { "put",       FTP_PUT, 0 },
  1024.     { "pwd",       FTP_PWD, 0 },
  1025.     { "quit",      FTP_CLS, CM_INV },
  1026.     { "quote",     FTP_QUO, 0 },
  1027.     { "reget",     FTP_RGE, 0 },
  1028.     { "rename",    FTP_REN, 0 },
  1029.     { "reset",     FTP_RES, 0 },
  1030.     { "rmdir",     FTP_RMD, 0 },
  1031.     { "send",      FTP_PUT, CM_INV },
  1032.     { "site",      FTP_SIT, 0 },
  1033.     { "size",      FTP_SIZ, 0 },
  1034.     { "status",    FTP_STA, 0 },
  1035.     { "system",    FTP_SYS, 0 },
  1036.     { "type",      FTP_TYP, 0 },
  1037.     { "umask",     FTP_UMA, 0 },
  1038.     { "up",        FTP_GUP, CM_INV },
  1039.     { "user",      FTP_USR, 0 },
  1040.     { "vdirectory",FTP_VDI, 0 },
  1041.     { "", 0, 0 }
  1042. };
  1043. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  1044.  
  1045. #define OPN_ANO 1            /* FTP OPEN switch codes */
  1046. #define OPN_PSW 2
  1047. #define OPN_USR 3
  1048. #define OPN_ACC 4
  1049. #define OPN_ACT 5
  1050. #define OPN_PSV 6
  1051. #define OPN_TLS 7
  1052. #define OPN_NIN 8
  1053. #define OPN_NOL 9
  1054.  
  1055. #ifdef FTP_SECURITY
  1056. #ifdef CK_SSL
  1057. #define USETLSTAB
  1058. static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
  1059.     { "/ssl",       OPN_TLS, 0    },
  1060.     { "/tls",       OPN_TLS, 0    },
  1061.     { "", 0, 0 }
  1062. };
  1063. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  1064. #endif /* CK_SSL */
  1065. #endif /* FTP_SECURITY */
  1066.  
  1067. static struct keytab ftpswitab[] = {    /* FTP command switches */
  1068.     { "/account",   OPN_ACC, CM_ARG },
  1069.     { "/active",    OPN_ACT, 0      },
  1070.     { "/anonymous", OPN_ANO, 0      },
  1071.     { "/noinit",    OPN_NIN, 0      },
  1072.     { "/nologin",   OPN_NOL, 0      },
  1073.     { "/passive",   OPN_PSV, 0      },
  1074.     { "/password",  OPN_PSW, CM_ARG },
  1075.     { "/user",      OPN_USR, CM_ARG },
  1076.     { "", 0, 0 }
  1077. };
  1078. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  1079.  
  1080. /* FTP { ENABLE, DISABLE } items */
  1081.  
  1082. #define ENA_FEAT 1
  1083. #define ENA_MDTM 2
  1084. #define ENA_MLST 3
  1085. #define ENA_SIZE 4
  1086. #define ENA_AUTH 5
  1087.  
  1088. static struct keytab ftpenatab[] = {
  1089.     { "AUTH",  ENA_AUTH, 0 },
  1090.     { "FEAT",  ENA_FEAT, 0 },
  1091.     { "MDTM",  ENA_MDTM, 0 },
  1092.     { "ML",    ENA_MLST, CM_INV|CM_ABR },
  1093.     { "MLS",   ENA_MLST, CM_INV|CM_ABR },
  1094.     { "MLSD",  ENA_MLST, CM_INV },
  1095.     { "MLST",  ENA_MLST, 0 },
  1096.     { "SIZE",  ENA_SIZE, 0 },
  1097.     { "", 0, 0 }
  1098. };
  1099. static int nftpena = (sizeof(ftpenatab) / sizeof(struct keytab)) - 1;
  1100.  
  1101. /* SET FTP command keyword indices */
  1102.  
  1103. #define FTS_AUT  1                      /* Autoauthentication */
  1104. #define FTS_CRY  2                      /* Encryption */
  1105. #define FTS_LOG  3                      /* Autologin */
  1106. #define FTS_CPL  4                      /* Command protection level */
  1107. #define FTS_CFW  5                      /* Credentials forwarding */
  1108. #define FTS_DPL  6                      /* Data protection level */
  1109. #define FTS_DBG  7                      /* Debugging */
  1110. #define FTS_PSV  8                      /* Passive mode */
  1111. #define FTS_SPC  9                      /* Send port commands */
  1112. #define FTS_TYP 10                      /* (file) Type */
  1113. #define FTS_USN 11                      /* Unique server names (for files) */
  1114. #define FTS_VBM 12                      /* Verbose mode */
  1115. #define FTS_ATP 13                      /* Authentication type */
  1116. #define FTS_CNV 14                      /* Filename conversion */
  1117. #define FTS_TST 15                      /* Test (progress) messages */
  1118. #define FTS_PRM 16                      /* (file) Permissions */
  1119. #define FTS_XLA 17                      /* Charset translation */
  1120. #define FTS_CSR 18                      /* Server charset */
  1121. #define FTS_ERR 19                      /* Error action */
  1122. #define FTS_FNC 20                      /* Collision */
  1123. #define FTS_SRP 21                      /* SRP options */
  1124. #define FTS_GFT 22                      /* GET automatic file-type switching */
  1125. #define FTS_DAT 23                      /* Set file dates */
  1126. #define FTS_STO 24            /* Server time offset */
  1127. #define FTS_APW 25            /* Anonymous password */
  1128. #define FTS_DIS 26            /* File-transfer display style */
  1129.  
  1130. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1131.  
  1132. #define PUT_UPD 1                       /* Update */
  1133. #define PUT_RES 2                       /* Restart */
  1134. #define PUT_SIM 4                       /* Simulation */
  1135. #define PUT_DIF 8            /* Dates Differ */
  1136.  
  1137. static struct keytab ftpcolxtab[] = { /* SET FTP COLLISION options */
  1138. #ifndef MAC
  1139.     { "append",    XYFX_A, 0 },         /* append to old file */
  1140. #endif /* MAC */
  1141. #ifdef COMMENT
  1142.     { "ask",       XYFX_Q, 0 },         /* ask what to do (not implemented) */
  1143. #endif
  1144.     { "backup",    XYFX_B, 0 },         /* rename old file */
  1145. #ifndef MAC
  1146.     { "dates-differ", XYFX_M, 0 },    /* accept if dates differ */
  1147.     { "discard",   XYFX_D, 0 },         /* don't accept new file */
  1148.     { "no-supersede", XYFX_D, CM_INV }, /* ditto (MSK compatibility) */
  1149. #endif /* MAC */
  1150.     { "overwrite", XYFX_X, 0 },         /* overwrite the old file */
  1151.     { "rename",    XYFX_R, 0 },         /* rename the incoming file */
  1152. #ifndef MAC                             /* This crashes Mac Kermit. */
  1153.     { "update",    XYFX_U, 0 },         /* replace if newer */
  1154. #endif /* MAC */
  1155.     { "", 0, 0 }
  1156. };
  1157. static int nftpcolx = (sizeof(ftpcolxtab) / sizeof(struct keytab)) - 1;
  1158.  
  1159.  
  1160. #ifdef FTP_SECURITY
  1161. /* FTP authentication options */
  1162.  
  1163. #define FTA_AUTO 0                      /* Auto */
  1164. #define FTA_SRP  1                      /* SRP */
  1165. #define FTA_GK5  2                      /* Kerberos 5 */
  1166. #define FTA_K4   3                      /* Kerberos 4 */
  1167. #define FTA_SSL  4                      /* SSL */
  1168. #define FTA_TLS  5                      /* TLS */
  1169.  
  1170. /* FTP authentication types */
  1171.  
  1172. #define FTPATYPS 8
  1173. static int ftp_auth_type[FTPATYPS] = {
  1174. #ifdef FTP_GSSAPI
  1175.     FTA_GK5,                            /* GSSAPI Kerberos 5 */
  1176. #endif /* FTP_GK5 */
  1177. #ifdef FTP_SRP
  1178.     FTA_SRP,                            /* SRP */
  1179. #endif /* FTP_SRP */
  1180. #ifdef FTP_KRB4
  1181.     FTA_K4,                             /* Kerberos 4 */
  1182. #endif /* FTP_KRB4 */
  1183. #ifdef CK_SSL
  1184.     FTA_TLS,                            /* TLS */
  1185.     FTA_SSL,                            /* SSL */
  1186. #endif /* CK_SSL */
  1187.     0
  1188. };
  1189.  
  1190. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1191.     { "automatic", FTA_AUTO,  CM_INV },
  1192. #ifdef FTP_GSSAPI
  1193.     { "gssapi-krb5", FTA_GK5, 0 },
  1194. #endif /* FTP_GSSAPI */
  1195. #ifdef FTP_KRB4
  1196.     { "k4",       FTA_K4,     CM_INV },
  1197. #endif /* FTP_KRB4 */
  1198. #ifdef FTP_GSSAPI
  1199.     { "k5",        FTA_GK5,   CM_INV },
  1200. #endif /* FTP_GSSAPI */
  1201. #ifdef FTP_KRB4
  1202.     { "kerberos4", FTA_K4,    0 },
  1203. #endif /* FTP_KRB4 */
  1204. #ifdef FTP_GSSAPI
  1205.     { "kerberos5", FTA_GK5,   CM_INV },
  1206. #endif /* FTP_GSSAPI */
  1207. #ifdef FTP_KRB4
  1208.     { "kerberos_iv",FTA_K4,   CM_INV },
  1209. #endif /* FTP_KRB4 */
  1210. #ifdef FTP_GSSAPI
  1211.     { "kerberos_v", FTA_GK5,  CM_INV },
  1212. #endif /* FTP_GSSAPI */
  1213. #ifdef FTP_KRB4
  1214.     { "krb4",     FTA_K4,     CM_INV },
  1215. #endif /* FTP_KRB4 */
  1216. #ifdef FTP_GSSAPI
  1217.     { "krb5",     FTA_GK5,    CM_INV },
  1218. #endif /* FTP_GSSAPI */
  1219. #ifdef FTP_SRP
  1220.     { "srp",      FTA_SRP,     0 },
  1221. #endif /* FTP_SRP */
  1222. #ifdef CK_SSL
  1223.     { "ssl",      FTA_SSL,     0 },
  1224.     { "tls",      FTA_TLS,     0 },
  1225. #endif /* CK_SSL */
  1226.     { "", 0, 0 }
  1227. };
  1228. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1229.  
  1230. #ifdef FTP_SRP
  1231. #define SRP_CIPHER 1
  1232. #define SRP_HASH   2
  1233. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1234.     { "cipher",   SRP_CIPHER,     0 },
  1235.     { "hash",     SRP_HASH,       0 },
  1236.     { "", 0, 0 }
  1237. };
  1238. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1239. #endif /* FTP_SRP */
  1240. #endif /* FTP_SECURITY */
  1241.  
  1242. static struct keytab ftpset[] = {       /* SET FTP commmand table */
  1243.     { "anonymous-password",       FTS_APW, 0 },
  1244. #ifdef FTP_SECURITY
  1245.     { "authtype",                 FTS_ATP, 0 },
  1246.     { "autoauthentication",       FTS_AUT, 0 },
  1247.     { "autoencryption",           FTS_CRY, 0 },
  1248. #endif /* FTP_SECURITY */
  1249.     { "autologin",                FTS_LOG, 0 },
  1250. #ifndef NOCSETS
  1251.     { "character-set-translation",FTS_XLA, 0 },
  1252. #endif /* NOCSETS */
  1253.     { "collision",                FTS_FNC, 0 },
  1254. #ifdef FTP_SECURITY
  1255.     { "command-protection-level", FTS_CPL, 0 },
  1256.     { "cpl",                      FTS_CPL, CM_INV },
  1257.     { "credential-forwarding",    FTS_CFW, 0 },
  1258.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1259.     { "data-protection-level",    FTS_DPL, 0 },
  1260. #endif /* FTP_SECURITY */
  1261.     { "dates",                    FTS_DAT, 0 },
  1262.     { "debug",                    FTS_DBG, 0 },
  1263.     { "display",                  FTS_DIS, 0 },
  1264. #ifdef FTP_SECURITY
  1265.     { "dpl",                      FTS_DPL, CM_INV },
  1266. #endif /* FTP_SECURITY */
  1267.     { "error-action",             FTS_ERR, 0 },
  1268.     { "filenames",                FTS_CNV, 0 },
  1269.     { "get-filetype-switching",   FTS_GFT, 0 },
  1270.     { "passive-mode",             FTS_PSV, 0 },
  1271.     { "pasv",                     FTS_PSV, CM_INV },
  1272.     { "permissions",              FTS_PRM, 0 },
  1273.     { "progress-messages",        FTS_TST, 0 },
  1274.     { "send-port-commands",       FTS_SPC, 0 },
  1275. #ifndef NOCSETS
  1276.     { "server-character-set",     FTS_CSR, 0 },
  1277. #endif /* NOCSETS */
  1278.     { "server-time-offset",       FTS_STO, 0 },
  1279. #ifdef FTP_SRP
  1280.     { "srp",                      FTS_SRP, 0 },
  1281. #else
  1282.     { "srp",                      FTS_SRP, CM_INV },
  1283. #endif /* FTP_SRP */
  1284.     { "type",                     FTS_TYP, 0 },
  1285.     { "unique-server-names",      FTS_USN, 0 },
  1286.     { "verbose-mode",             FTS_VBM, 0 },
  1287.     { "", 0, 0 }
  1288. };
  1289. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1290.  
  1291. /*
  1292.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1293.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1294.   Don't just make up new ones, since the number of SND_xxx options must be
  1295.   known in advance for the switch-parsing arrays.
  1296. */
  1297. #define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
  1298. #define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
  1299. #define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
  1300.  
  1301. static struct keytab putswi[] = {       /* FTP PUT switch table */
  1302.     { "/after",                SND_AFT, CM_ARG },
  1303. #ifdef PUTARRAY
  1304.     { "/array",                SND_ARR, CM_ARG },
  1305. #endif /* PUTARRAY */
  1306.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1307.     { "/as-name",              SND_ASN, CM_ARG },
  1308.     { "/ascii",                SND_TXT, CM_INV },
  1309.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1310.     { "/before",               SND_BEF, CM_ARG },
  1311.     { "/binary",               SND_BIN, 0 },
  1312. #ifdef PUTPIPE
  1313.     { "/command",              SND_CMD, CM_PSH },
  1314. #endif /* PUTPIPE */
  1315. #ifdef COMMENT
  1316. /* This works but it's dangerous */
  1317. #ifdef DOUPDATE
  1318.     { "/dates-differ",         SND_DIF, CM_INV },
  1319. #endif /* DOUPDATE */
  1320. #endif /* COMMENT */
  1321.     { "/delete",               SND_DEL, 0 },
  1322. #ifdef UNIXOROSK
  1323.     { "/dotfiles",             SND_DOT, 0 },
  1324. #endif /* UNIXOROSK */
  1325.     { "/error-action",         SND_ERR, CM_ARG },
  1326.     { "/except",               SND_EXC, CM_ARG },
  1327.     { "/filenames",            SND_NAM, CM_ARG },
  1328. #ifdef PIPESEND
  1329. #ifndef NOSPL
  1330.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1331. #endif /* NOSPL */
  1332. #endif /* PIPESEND */
  1333. #ifdef CKSYMLINK
  1334.     { "/followlinks",          SND_LNK, 0 },
  1335. #endif /* CKSYMLINK */
  1336. #ifdef VMS
  1337.     { "/image",                SND_IMG, 0 },
  1338. #else
  1339.     { "/image",                SND_BIN, CM_INV },
  1340. #endif /* VMS */
  1341.     { "/larger-than",          SND_LAR, CM_ARG },
  1342.     { "/listfile",             SND_FIL, CM_ARG },
  1343. #ifndef NOCSETS
  1344.     { "/local-character-set",  SND_CSL, CM_ARG },
  1345. #endif /* NOCSETS */
  1346. #ifdef CK_TMPDIR
  1347.     { "/move-to",              SND_MOV, CM_ARG },
  1348. #endif /* CK_TMPDIR */
  1349.     { "/nobackupfiles",        SND_NOB, 0 },
  1350. #ifdef UNIXOROSK
  1351.     { "/nodotfiles",           SND_NOD, 0 },
  1352. #endif /* UNIXOROSK */
  1353. #ifdef CKSYMLINK
  1354.     { "/nofollowlinks",        SND_NLK, 0 },
  1355. #endif /* CKSYMLINK */
  1356.  
  1357.     { "/not-after",            SND_NAF, CM_ARG },
  1358.     { "/not-before",           SND_NBE, CM_ARG },
  1359. #ifdef UNIX
  1360.     { "/permissions",          SND_PRM, CM_ARG },
  1361. #else
  1362.     { "/permissions",          SND_PRM, CM_ARG|CM_INV },
  1363. #endif /* UNIX */
  1364.     { "/quiet",                SND_SHH, 0 },
  1365. #ifdef FTP_RESTART
  1366.     { "/recover",              SND_RES, 0 },
  1367. #endif /* FTP_RESTART */
  1368. #ifdef RECURSIVE
  1369.     { "/recursive",            SND_REC, 0 },
  1370. #endif /* RECURSIVE */
  1371.     { "/rename-to",            SND_REN, CM_ARG },
  1372. #ifdef FTP_RESTART
  1373.     { "/restart",              SND_RES, CM_INV },
  1374. #endif /* FTP_RESTART */
  1375. #ifndef NOCSETS
  1376.     { "/server-character-set", SND_CSR, CM_ARG },
  1377. #endif /* NOCSETS */
  1378.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1379.     { "/simulate",             SND_SIM, 0 },
  1380.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1381.     { "/smaller-than",         SND_SMA, CM_ARG },
  1382. #ifdef COMMENT
  1383.     { "/starting-at",          SND_STA, CM_ARG },
  1384. #endif /* COMMENT */
  1385. #ifdef RECURSIVE
  1386.     { "/subdirectories",       SND_REC, CM_INV },
  1387. #endif /* RECURSIVE */
  1388.     { "/tenex",                SND_TEN, 0 },
  1389.     { "/text",                 SND_TXT, 0 },
  1390. #ifndef NOCSETS
  1391.     { "/transparent",          SND_XPA, 0 },
  1392. #endif /* NOCSETS */
  1393.     { "/type",                 SND_TYP, CM_ARG },
  1394. #ifdef DOUPDATE
  1395.     { "/update",               SND_UPD, 0 },
  1396. #endif /* DOUPDATE */
  1397.     { "/unique-server-names",  SND_USN, 0 },
  1398.     { "", 0, 0 }
  1399. };
  1400. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1401.  
  1402. static struct keytab getswi[] = {       /* FTP [M]GET switch table */
  1403.     { "/after",                SND_AFT, CM_INV },
  1404.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1405.     { "/as-name",              SND_ASN, CM_ARG },
  1406.     { "/ascii",                SND_TXT, CM_INV },
  1407.     { "/before",               SND_BEF, CM_INV },
  1408.     { "/binary",               SND_BIN, 0 },
  1409.     { "/collision",            SND_COL, CM_ARG },
  1410. #ifdef PUTPIPE
  1411.     { "/command",              SND_CMD, CM_PSH },
  1412. #endif /* PUTPIPE */
  1413.     { "/delete",               SND_DEL, 0 },
  1414.     { "/error-action",         SND_ERR, CM_ARG },
  1415.     { "/except",               SND_EXC, CM_ARG },
  1416.     { "/filenames",            SND_NAM, CM_ARG },
  1417. #ifdef PIPESEND
  1418. #ifndef NOSPL
  1419.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1420. #endif /* NOSPL */
  1421. #endif /* PIPESEND */
  1422. #ifdef VMS
  1423.     { "/image",                SND_IMG, 0 },
  1424. #else
  1425.     { "/image",                SND_BIN, CM_INV },
  1426. #endif /* VMS */
  1427.     { "/larger-than",          SND_LAR, CM_ARG },
  1428.     { "/listfile",             SND_FIL, CM_ARG },
  1429. #ifndef NOCSETS
  1430.     { "/local-character-set",  SND_CSL, CM_ARG },
  1431. #endif /* NOCSETS */
  1432.     { "/match",                SND_PAT, CM_ARG },
  1433.     { "/ml",                   SND_MLS, CM_INV|CM_ABR },
  1434.     { "/mls",                  SND_MLS, CM_INV|CM_ABR },
  1435.     { "/mlsd",                 SND_MLS, 0 },
  1436.     { "/mlst",                 SND_MLS, CM_INV },
  1437. #ifdef CK_TMPDIR
  1438.     { "/move-to",              SND_MOV, CM_ARG },
  1439. #endif /* CK_TMPDIR */
  1440.     { "/namelist",             SND_NML, CM_ARG },
  1441.     { "/nlst",                 SND_NLS, 0 },
  1442.     { "/nobackupfiles",        SND_NOB, 0 },
  1443.     { "/nodotfiles",           SND_NOD, 0 },
  1444. #ifdef DOUPDATE
  1445.     { "/dates-differ",         SND_DIF, CM_INV },
  1446. #endif /* DOUPDATE */
  1447.     { "/not-after",            SND_NAF, CM_INV },
  1448.     { "/not-before",           SND_NBE, CM_INV },
  1449.     { "/permissions",          SND_PRM, CM_INV },
  1450.     { "/quiet",                SND_SHH, 0 },
  1451. #ifdef FTP_RESTART
  1452.     { "/recover",              SND_RES, 0 },
  1453. #endif /* FTP_RESTART */
  1454. #ifdef RECURSIVE
  1455.     { "/recursive",            SND_REC, 0 },
  1456. #endif /* RECURSIVE */
  1457.     { "/rename-to",            SND_REN, CM_ARG },
  1458. #ifdef FTP_RESTART
  1459.     { "/restart",              SND_RES, CM_INV },
  1460. #endif /* FTP_RESTART */
  1461. #ifndef NOCSETS
  1462.     { "/server-character-set", SND_CSR, CM_ARG },
  1463. #endif /* NOCSETS */
  1464.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1465.     { "/smaller-than",         SND_SMA, CM_ARG },
  1466. #ifdef RECURSIVE
  1467.     { "/subdirectories",       SND_REC, CM_INV },
  1468. #endif /* RECURSIVE */
  1469.     { "/text",                 SND_TXT, 0 },
  1470.     { "/tenex",                SND_TEN, 0 },
  1471. #ifndef NOCSETS
  1472.     { "/transparent",          SND_XPA, 0 },
  1473. #endif /* NOCSETS */
  1474.     { "/to-screen",            SND_MAI, 0 },
  1475. #ifdef DOUPDATE
  1476.     { "/update",               SND_UPD, CM_INV },
  1477. #endif /* DOUPDATE */
  1478.     { "", 0, 0 }
  1479. };
  1480. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1481.  
  1482. static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
  1483.     { "/error-action",         SND_ERR, CM_ARG },
  1484.     { "/except",               SND_EXC, CM_ARG },
  1485.     { "/filenames",            SND_NAM, CM_ARG },
  1486.     { "/larger-than",          SND_LAR, CM_ARG },
  1487.     { "/nobackupfiles",        SND_NOB, 0 },
  1488. #ifdef UNIXOROSK
  1489.     { "/nodotfiles",           SND_NOD, 0 },
  1490. #endif /* UNIXOROSK */
  1491.     { "/quiet",                SND_SHH, 0 },
  1492. #ifdef RECURSIVE
  1493.     { "/recursive",            SND_REC, 0 },
  1494. #endif /* RECURSIVE */
  1495.     { "/smaller-than",         SND_SMA, CM_ARG },
  1496. #ifdef RECURSIVE
  1497.     { "/subdirectories",       SND_REC, CM_INV },
  1498. #endif /* RECURSIVE */
  1499.     { "", 0, 0 }
  1500. };
  1501. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1502.  
  1503. static struct keytab fntab[] = {        /* Filename conversion keyword table */
  1504.     { "automatic",    2, CNV_AUTO },
  1505.     { "converted",    1, CNV_CNV  },
  1506.     { "literal",      0, CNV_LIT  }
  1507. };
  1508. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1509.  
  1510. static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
  1511.     { "ascii",        FTT_ASC, 0 },
  1512.     { "binary",       FTT_BIN, 0 },
  1513.     { "tenex",        FTT_TEN, 0 },
  1514.     { "text",         FTT_ASC, CM_INV },
  1515.     { "", 0, 0 }
  1516. };
  1517. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1518.  
  1519. #ifdef FTP_SECURITY
  1520. static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
  1521.     { "clear",        FPL_CLR, 0 },
  1522.     { "confidential", FPL_CON, 0 },
  1523.     { "private",      FPL_PRV, 0 },
  1524.     { "safe",         FPL_SAF, 0 },
  1525.     { "", 0, 0 }
  1526. };
  1527. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1528. #endif /* FTP_SECURITY */
  1529.  
  1530. /* Definitions for FTP from RFC765. */
  1531.  
  1532. /* Reply codes */
  1533.  
  1534. #define REPLY_PRELIM    1               /* Positive preliminary */
  1535. #define REPLY_COMPLETE  2               /* Positive completion */
  1536. #define REPLY_CONTINUE  3               /* Positive intermediate */
  1537. #define REPLY_TRANSIENT 4               /* Transient negative completion */
  1538. #define REPLY_ERROR     5               /* Permanent negative completion */
  1539. #define REPLY_SECURE    6               /* Security encoded message */
  1540.  
  1541. /* Form codes and names */
  1542.  
  1543. #define FORM_N 1                        /* Non-print */
  1544. #define FORM_T 2                        /* Telnet format effectors */
  1545. #define FORM_C 3                        /* Carriage control (ASA) */
  1546.  
  1547. /* Structure codes and names */
  1548.  
  1549. #define STRU_F 1                        /* File (no record structure) */
  1550. #define STRU_R 2                        /* Record structure */
  1551. #define STRU_P 3                        /* Page structure */
  1552.  
  1553. /* Mode types and names */
  1554.  
  1555. #define MODE_S 1                        /* Stream */
  1556. #define MODE_B 2                        /* Block */
  1557. #define MODE_C 3                        /* Compressed */
  1558.  
  1559. /* Protection levels and names */
  1560.  
  1561. #define PROT_C 1                        /* Clear */
  1562. #define PROT_S 2                        /* Safe */
  1563. #define PROT_P 3                        /* Private */
  1564. #define PROT_E 4                        /* Confidential */
  1565.  
  1566. #ifdef COMMENT                          /* Not used */
  1567. #ifdef FTP_NAMES
  1568. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1569. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1570. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1571. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1572. #endif /* FTP_NAMES */
  1573.  
  1574. /* Record Tokens */
  1575.  
  1576. #define REC_ESC '\377'                  /* Record-mode Escape */
  1577. #define REC_EOR '\001'                  /* Record-mode End-of-Record */
  1578. #define REC_EOF '\002'                  /* Record-mode End-of-File */
  1579.  
  1580. /* Block Header */
  1581.  
  1582. #define BLK_EOR           0x80          /* Block is End-of-Record */
  1583. #define BLK_EOF           0x40          /* Block is End-of-File */
  1584. #define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
  1585. #define BLK_RESTART       0x10          /* Block is Restart Marker */
  1586. #define BLK_BYTECOUNT 2                 /* Bytes in this block */
  1587. #endif /* COMMENT */
  1588.  
  1589. #define RADIX_ENCODE 0                  /* radix_encode() function codes */
  1590. #define RADIX_DECODE 1
  1591.  
  1592. /*
  1593.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1594.   results in a serious performance degradation due to the increased number
  1595.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1596.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1597.   of the typical TCP window.  Maybe we should add a command to allow the value
  1598.   to be changed.
  1599. */
  1600. #define DEFAULT_PBSZ 1<<13
  1601.  
  1602. /* Definitions and typedefs needed for prototypes */
  1603.  
  1604. #define sig_t my_sig_t
  1605. #define sigtype SIGTYP
  1606. typedef sigtype (*sig_t)();
  1607.  
  1608. /* Prototypes */
  1609.  
  1610. _PROTOTYP(int remtxt, (char **) );
  1611. _PROTOTYP(char * gskreason, (int) );
  1612. _PROTOTYP(static int ftpclose,(void));
  1613. _PROTOTYP(static int zzsend, (int, CHAR));
  1614. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1615. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1616. _PROTOTYP(static int setpbsz,(unsigned int));
  1617. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1618.   int,int,char *,int,int,int));
  1619. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1620. _PROTOTYP(static int fts_cpl,(int));
  1621. _PROTOTYP(static int fts_dpl,(int));
  1622. #ifdef FTP_SECURITY
  1623. _PROTOTYP(static int ftp_auth, (void));
  1624. #endif /* FTP_SECURITY */
  1625. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1626. _PROTOTYP(static int ftp_login, (char *));
  1627. _PROTOTYP(static int ftp_reset, (void));
  1628. _PROTOTYP(static int ftp_rename, (char *, char *));
  1629. _PROTOTYP(static int ftp_umask, (char *));
  1630. _PROTOTYP(static int secure_flush, (int));
  1631. #ifdef COMMENT
  1632. _PROTOTYP(static int secure_putc, (char, int));
  1633. #endif /* COMMENT */
  1634. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1635. _PROTOTYP(static int scommand, (char *));
  1636. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1637. _PROTOTYP(static int secure_getc, (int, int));
  1638. _PROTOTYP(static int secure_getbyte, (int, int));
  1639. _PROTOTYP(static int secure_read, (int, char *, int));
  1640. _PROTOTYP(static int initconn, (void));
  1641. _PROTOTYP(static int dataconn, (char *));
  1642. _PROTOTYP(static int setprotbuf,(unsigned int));
  1643. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1644.  
  1645. _PROTOTYP(static char * radix_error,(int));
  1646. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1647. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, CHAR *, int));
  1648.  
  1649. _PROTOTYP(static VOID mlsreset, (void));
  1650. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1651. _PROTOTYP(static VOID lostpeer, (void));
  1652. _PROTOTYP(static VOID cancel_remote, (int));
  1653. _PROTOTYP(static VOID changetype, (int, int));
  1654.  
  1655. _PROTOTYP(static sigtype cmdcancel, (int));
  1656.  
  1657. #ifdef FTP_SRP
  1658. _PROTOTYP(static int srp_reset, ());
  1659. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1660. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1661. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1662. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1663. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1664. _PROTOTYP(static int srp_selcipher, (char *));
  1665. _PROTOTYP(static int srp_selhash, (char *));
  1666. #endif /* FTP_SRP */
  1667.  
  1668. #ifdef FTP_GSSAPI
  1669. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1670. #endif /* FTP_GSSAPI */
  1671.  
  1672. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1673.  
  1674. #ifdef FTP_SECURITY
  1675. #ifndef NOICP
  1676. #define FT_NOGSS   1
  1677. #define FT_NOK4    2
  1678. #define FT_NOSRP   3
  1679. #define FT_NOSSL   4
  1680. #define FT_NOTLS   5
  1681. #define FT_CERTFI  6
  1682. #define FT_OKCERT  7
  1683. #define FT_DEBUG   8
  1684. #define FT_KEY     9
  1685. #define FT_SECURE 10
  1686. #define FT_VERIFY 11
  1687.  
  1688. static struct keytab ftpztab[] = {
  1689.     { "!gss",    FT_NOGSS,  0 },
  1690.     { "!krb4",   FT_NOK4,   0 },
  1691.     { "!srp",    FT_NOSRP,  0 },
  1692.     { "!ssl",    FT_NOSSL,  0 },
  1693.     { "!tls",    FT_NOTLS,  0 },
  1694.     { "cert",    FT_CERTFI, CM_ARG },
  1695.     { "certsok", FT_OKCERT, 0 },
  1696.     { "debug",   FT_DEBUG,  0 },
  1697.     { "key",     FT_KEY,    CM_ARG },
  1698.     { "nogss",   FT_NOGSS,  0 },
  1699.     { "nokrb4",  FT_NOK4,   0 },
  1700.     { "nosrp",   FT_NOSRP,  0 },
  1701.     { "nossl",   FT_NOSSL,  0 },
  1702.     { "notls",   FT_NOTLS,  0 },
  1703. #ifdef COMMENT
  1704.     { "secure",  FT_SECURE, 0 },
  1705. #endif /* COMMENT */
  1706.     { "verify",  FT_VERIFY, CM_ARG },
  1707.     { "", 0, 0 }
  1708. };
  1709. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1710.  
  1711. /*
  1712.   The following cipher and hash tables should be replaced with
  1713.   dynamicly created versions based upon the linked library.
  1714. */
  1715. #define SRP_BLOWFISH_ECB    1
  1716. #define SRP_BLOWFISH_CBC    2
  1717. #define SRP_BLOWFISH_CFB64  3
  1718. #define SRP_BLOWFISH_OFB64  4
  1719. #define SRP_CAST5_ECB       5
  1720. #define SRP_CAST5_CBC       6
  1721. #define SRP_CAST5_CFB64     7
  1722. #define SRP_CAST5_OFB64     8
  1723. #define SRP_DES_ECB         9
  1724. #define SRP_DES_CBC        10
  1725. #define SRP_DES_CFB64      11
  1726. #define SRP_DES_OFB64      12
  1727. #define SRP_DES3_ECB       13
  1728. #define SRP_DES3_CBC       14
  1729. #define SRP_DES3_CFB64     15
  1730. #define SRP_DES3_OFB64     16
  1731.  
  1732. static struct keytab ciphertab[] = {
  1733.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1734.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1735.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1736.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1737.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1738.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1739.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1740.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1741.     { "des_ecb",        SRP_DES_ECB,        0 },
  1742.     { "des_cbc",        SRP_DES_CBC,        0 },
  1743.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1744.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1745.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1746.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1747.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1748.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1749.     { "none",           0, 0 },
  1750.     { "", 0, 0 }
  1751. };
  1752. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1753.  
  1754. #define SRP_MD5  1
  1755. #define SRP_SHA  2
  1756. static struct keytab hashtab[] = {
  1757.     { "md5",              SRP_MD5,        0 },
  1758.     { "none",             0,              0 },
  1759.     { "sha",              SRP_SHA,        0 },
  1760.     { "", 0, 0 }
  1761. };
  1762. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1763. #endif /* NOICP */
  1764. #endif /* FTP_SECURITY */
  1765.  
  1766. static char *
  1767. strval(s1,s2) char * s1, * s2; {
  1768.     if (!s1) s1 = "";
  1769.     if (!s2) s2 = "";
  1770.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1771. }
  1772.  
  1773. #ifndef NOCSETS
  1774. static char * rfnptr = NULL;
  1775. static int rfnlen = 0;
  1776. static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
  1777. static char * xgnbp = NULL;
  1778.  
  1779. static int
  1780. strgetc() {                             /* Helper function for xgnbyte() */
  1781.     int c;
  1782.     if (!xgnbp)
  1783.       return(-1);
  1784.     if (!*xgnbp)
  1785.       return(-1);
  1786.     c = (unsigned) *xgnbp++;
  1787.     return(((unsigned) c) & 0xff);
  1788. }
  1789.  
  1790. static int                              /* Helper function for xpnbyte() */
  1791. #ifdef CK_ANSIC
  1792. strputc(char c)
  1793. #else
  1794. strputc(c) char c;
  1795. #endif /* CK_ANSIC */
  1796. {
  1797.     rfnlen = rfnptr - rfnbuf;
  1798.     if (rfnlen >= (RFNBUFSIZ - 1))
  1799.       return(-1);
  1800.     *rfnptr++ = c;
  1801.     *rfnptr = NUL;
  1802.     return(0);
  1803. }
  1804.  
  1805. static int
  1806. #ifdef CK_ANSIC
  1807. xprintc(char c)
  1808. #else
  1809. xprintc(c) char c;
  1810. #endif /* CK_ANSIC */
  1811. {
  1812.     printf("%c",c);
  1813.     return(0);
  1814. }
  1815.  
  1816. static VOID
  1817. bytswap(c0,c1) int * c0, * c1; {
  1818.     int t;
  1819.     t = *c0;
  1820.     *c0 = *c1;
  1821.     *c1 = t;
  1822. }
  1823. #endif /* NOCSETS */
  1824.  
  1825. #ifdef CKLOGDIAL
  1826. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  1827. int ftplogactive = 0;
  1828. long ftplogprev = 0L;
  1829.  
  1830. VOID
  1831. ftplogend() {
  1832.     extern int dialog;
  1833.     extern char diafil[];
  1834.     long d1, d2, t1, t2;
  1835.     char buf[32], * p;
  1836.  
  1837.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  1838.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  1839.  
  1840.     if (!ftplogactive || !ftplogbuf[0]) /* No active record */
  1841.       return;
  1842.  
  1843.     ftplogactive = 0;                   /* Record is not active */
  1844.  
  1845.     d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
  1846.     ckstrncpy(buf,ckdate(),31);         /* Get current date */
  1847.     d2 = mjd(buf);                      /* Convert them to mjds */
  1848.     p = ftplogbuf;                      /* Get start time */
  1849.     p[11] = NUL;
  1850.     p[14] = NUL;                        /* Convert to seconds */
  1851.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1852.     p[11] = ':';
  1853.     p[14] = ':';
  1854.     p = buf;                            /* Get end time */
  1855.     p[11] = NUL;
  1856.     p[14] = NUL;
  1857.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1858.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  1859.     if (t2 > -1L) {
  1860.         ftplogprev = t2;
  1861.         p = hhmmss(t2);
  1862.         strncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  1863.         strncat(ftplogbuf,p,CXLOGBUFL);
  1864.     } else
  1865.       ftplogprev = 0L;
  1866.     debug(F101,"ftp cx log dialog","",dialog);
  1867.     if (dialog) {                       /* If logging */
  1868.         int x;
  1869.         x = diaopn(diafil,1,1);         /* Open log in append mode */
  1870.         if (x > 0) {
  1871.             debug(F101,"ftp cx log open","",x);
  1872.             x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  1873.             debug(F101,"ftp cx log write","",x);
  1874.             x = zclose(ZDIFIL);         /* Close the log */
  1875.             debug(F101,"ftp cx log close","",x);
  1876.         }
  1877.     }
  1878. }
  1879.  
  1880. VOID
  1881. dologftp() {
  1882.     ftplogend();                        /* Previous session not closed out? */
  1883.     ftplogprev = 0L;
  1884.     ftplogactive = 1;                   /* Record is active */
  1885.  
  1886.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  1887.               ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  1888.               " T=FTP N=", strval(ftp_host,NULL)," H=",myhost," ",NULL,NULL);
  1889.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  1890. }
  1891. #endif /* CKLOGDIAL */
  1892.  
  1893. static char * dummy[2] = { NULL, NULL };
  1894.  
  1895. static struct keytab modetab[] = {
  1896.     { "active",  0, 0 },
  1897.     { "passive", 1, 0 }
  1898. };
  1899.  
  1900. #ifndef NOCMDL
  1901. int                                     /* Called from ckuusy.c */
  1902. #ifdef CK_ANSIC
  1903. doftparg(char c)
  1904. #else
  1905. doftparg(c) char c;
  1906. #endif /* CK_ANSIC */
  1907. /* doftparg */ {
  1908.     int x, z;
  1909.     char *xp;
  1910.     extern char **xargv, *xarg0;
  1911.     extern int xargc, stayflg, haveftpuid;
  1912.     extern char uidbuf[];
  1913.  
  1914.     xp = *xargv+1;                      /* Pointer for bundled args */
  1915.     while (c) {
  1916.         if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
  1917.             if (*(xp+1)) {
  1918.                 fatal("?Invalid argument bundling");
  1919.             }
  1920.             xargv++, xargc--;
  1921.             if ((xargc < 1) || (**xargv == '-')) {
  1922.                 fatal("?Required argument missing");
  1923.             }
  1924.         }
  1925.         switch (c) {                    /* Big switch on arg */
  1926.           case 'h':                     /* help */
  1927.            printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  1928.             printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  1929.             printf("Options:\n");
  1930.             printf("  -h           = help (this message)\n");
  1931.             printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  1932.             printf("  -u name      = username for autologin (or -M)\n");
  1933.             printf("  -P password  = password for autologin (RISKY)\n");
  1934.             printf("  -A           = autologin anonymously\n");
  1935.             printf("  -D directory = cd after autologin\n");
  1936.             printf("  -b           = force binary mode\n");
  1937.             printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  1938.             printf("  -d           = debug (double to add timestamps)\n");
  1939.             printf("  -n           = no autologin\n");
  1940.             printf("  -v           = verbose (default)\n");
  1941.             printf("  -q           = quiet\n");
  1942.             printf("  -S           = Stay (issue command prompt when done)\n");
  1943.             printf("  -Y           = do not execute Kermit init file\n");
  1944.             printf("  -p files     = files to put after autologin (or -s)\n");
  1945.             printf("  -g files     = files to get after autologin\n");
  1946.             printf("  -R           = recursive (for use with -p)\n");
  1947.  
  1948. #ifdef FTP_SECURITY
  1949.             printf("\nSecurity options:\n");
  1950.             printf("  -k realm     = Kerberos 4 realm\n");
  1951.             printf("  -f           = Kerboros 5 credentials forwarding\n");
  1952.             printf("  -x           = autoencryption mode\n");
  1953.             printf("  -c cipher    = SRP cipher type\n");
  1954.             printf("  -H hash      = SRP encryption hash\n");
  1955.             printf("  -z option    = Security options\n");
  1956. #endif /* FTP_SECURITY */
  1957.  
  1958.             printf("\n-p or -g, if given, should be last.  Example:\n");
  1959.             printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  1960.  
  1961.             doexit(GOOD_EXIT,-1);
  1962.             break;
  1963.  
  1964.           case 'R':                     /* Recursive */
  1965.             recursive = 1;
  1966.             break;
  1967.  
  1968.           case 'd':                     /* Debug */
  1969. #ifdef DEBUG
  1970.             if (deblog) {
  1971.                 extern int debtim;
  1972.                 debtim = 1;
  1973.             } else {
  1974.                 deblog = debopn("debug.log",0);
  1975.                 debok = 1;
  1976.             }
  1977. #endif /* DEBUG */
  1978.             /* fall thru on purpose */
  1979.  
  1980.           case 't':                     /* Trace */
  1981.             ftp_deb++;
  1982.             break;
  1983.  
  1984.           case 'n':                     /* No autologin */
  1985.             ftp_log = 0;
  1986.             break;
  1987.  
  1988.           case 'i':                     /* No prompt */
  1989.           case 'v':                     /* Verbose */
  1990.             break;                      /* (ignored) */
  1991.  
  1992.           case 'q':                     /* Quiet */
  1993.             quiet = 1;
  1994.             break;
  1995.  
  1996.           case 'S':                     /* Stay */
  1997.             stayflg = 1;
  1998.             break;
  1999.  
  2000.           case 'M':
  2001.           case 'u':                     /* My User Name */
  2002.             if ((int)strlen(*xargv) > 63) {
  2003.                 fatal("username too long");
  2004.             }
  2005.             ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  2006.             haveftpuid = 1;
  2007.             break;
  2008.  
  2009.           case 'A':
  2010.             ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2011.             haveftpuid = 1;
  2012.             break;
  2013.  
  2014.           case 'T':                     /* Text */
  2015.           case 'a':                     /* "ascii" */
  2016.           case 'b':                     /* Binary */
  2017.             binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  2018.             xfermode = XMODE_M;
  2019.             filepeek = 0;
  2020.             patterns = 0;
  2021.             break;
  2022.  
  2023.           case 'g':                     /* Get */
  2024.           case 'p':                     /* Put */
  2025.           case 's': {                   /* Send (= Put) */
  2026.               int havefiles, rc;
  2027.               if (ftp_action) {
  2028.                   fatal("Only one FTP action at a time please");
  2029.               }
  2030.               if (*(xp+1)) {
  2031.                   fatal("invalid argument bundling after -s");
  2032.               }
  2033.               nfils = 0;                /* Initialize file counter */
  2034.               havefiles = 0;            /* Assume nothing to send  */
  2035.               cmlist = xargv + 1;       /* Remember this pointer */
  2036.  
  2037.               while (++xargv, --xargc > 0) { /* Traverse the list */
  2038.                   if (c == 'g') {
  2039.                       havefiles++;
  2040.                       nfils++;
  2041.                       continue;
  2042.                   }
  2043. #ifdef RECURSIVE
  2044.                   if (!strcmp(*xargv,".")) {
  2045.                       havefiles = 1;
  2046.                       nfils++;
  2047.                       recursive = 1;
  2048.                   } else
  2049. #endif /* RECURSIVE */
  2050.                     if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  2051.                         if  (rc != -2)
  2052.                           havefiles = 1;
  2053.                         nfils++;
  2054.                     } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  2055.                         havefiles = 1;
  2056.                         nfils++;
  2057.                     }
  2058.               }
  2059.               xargc++, xargv--;         /* Adjust argv/argc */
  2060.               if (!havefiles) {
  2061.                   if (c == 'g') {
  2062.                       fatal("No files to put");
  2063.                   } else {
  2064.                       fatal("No files to get");
  2065.                   }
  2066.               }
  2067.               ftp_action = c;
  2068.               break;
  2069.           }
  2070.           case 'D':                     /* Directory */
  2071.             makestr(&ftp_rdir,*xargv);
  2072.             break;
  2073.  
  2074.           case 'm':                     /* Mode (Active/Passive */
  2075.             ftp_psv = lookup(modetab,*xargv,2,NULL);
  2076.             if (ftp_psv < 0) fatal("Invalid mode");
  2077.             break;
  2078.  
  2079.           case 'P':
  2080.             makestr(&ftp_tmp,*xargv);   /* You-Know-What */
  2081.             break;
  2082.  
  2083.           case 'Y':                     /* No initialization file */
  2084.             break;                      /* (already done in prescan) */
  2085.  
  2086. #ifdef CK_URL
  2087.           case 'U': {                   /* URL */
  2088.               /* These are set by urlparse() - any not set are NULL */
  2089.               if (g_url.hos) {
  2090. /*
  2091.   Kermit has accepted host:port notation since many years before URLs were
  2092.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  2093.   host:449" looks like a URL and results in service = host and host = 449.
  2094.   Here we try to catch this situation transparently to the user.
  2095. */
  2096.                   if (ckstrcmp(g_url.svc,"ftp",-1,0)
  2097. #ifdef CK_SSL
  2098.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  2099. #endif /* CK_SSL */
  2100.                        ) {
  2101.                       if (!g_url.usr &&
  2102.                           !g_url.psw &&
  2103.                           !g_url.por &&
  2104.                           !g_url.pth) {
  2105.                           g_url.por = g_url.hos;
  2106.                           g_url.hos = g_url.svc;
  2107.                           g_url.svc = "ftp";
  2108.                       } else {
  2109.                           ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  2110.                                    g_url.svc," host=",g_url.hos);
  2111.                           fatal(tmpbuf);
  2112.                       }
  2113.                   }
  2114.                   makestr(&ftp_host,g_url.hos);
  2115.                   if (g_url.usr) {
  2116.                       haveftpuid = 1;
  2117.                       ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  2118.                       makestr(&ftp_logname,uidbuf);
  2119.                   }
  2120.                   if (g_url.psw) {
  2121.                       makestr(&ftp_tmp,g_url.psw);
  2122.                   }
  2123.                   if (g_url.pth) {
  2124.                       if (!g_url.usr) {
  2125.                           haveftpuid = 1;
  2126.                           ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2127.                           makestr(&ftp_logname,uidbuf);
  2128.                       }
  2129.                       if (ftp_action) {
  2130.                           fatal("Only one FTP action at a time please");
  2131.                       }
  2132.                       if (!stayflg)
  2133.                         quiet = 1;
  2134.                       nfils = 1;
  2135.                       dummy[0] = g_url.pth;
  2136.                       cmlist = dummy;
  2137.                       ftp_action = 'g';
  2138.                   }
  2139.                   xp = NULL;
  2140.                   haveurl = 1;
  2141.               }
  2142.               break;
  2143.           }
  2144. #endif /* CK_URL */
  2145.  
  2146. #ifdef FTP_SECURITY
  2147.           case 'k': {                   /* K4 Realm */
  2148. #ifdef FTP_KRB4
  2149.               ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  2150. #endif /* FTP_KRB4 */
  2151.               if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  2152.               break;
  2153.           }
  2154.           case 'f': {
  2155. #ifdef FTP_GSSAPI
  2156.               ftp_cfw = 1;
  2157.               if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2158. #else /* FTP_GSSAPI */
  2159.               printf("K5 Credentials Forwarding not supported\n");
  2160. #endif /* FTP_GSSAPI */
  2161.               break;
  2162.           }
  2163.           case 'x': {
  2164.               ftp_cry = 1;
  2165.               if (ftp_deb) printf("Autoencryption\n");
  2166.               break;
  2167.           }
  2168.           case 'c': {                   /* Cipher */
  2169. #ifdef FTP_SRP
  2170.               if (!srp_selcipher(*xargv)) {
  2171.                   if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2172.               } else
  2173.                 printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2174. #else /* FTP_SRP */
  2175.               printf("?SRP not supported\n");
  2176. #endif /* FTP_SRP */
  2177.               break;
  2178.           }
  2179.           case 'H': {
  2180. #ifdef FTP_SRP
  2181.               if (!srp_selhash(*xargv)) {
  2182.                   if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2183.               } else
  2184.                 printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2185. #else /* FTP_SRP */
  2186.               printf("?SRP not supported\n");
  2187. #endif /* FTP_SRP */
  2188.               break;
  2189.           }
  2190.           case 'z': {
  2191.               /* *xargv contains a value of the form tag=value */
  2192.               /* we need to lookup the tag and save the value  */
  2193.               char * p = NULL, * q = NULL;
  2194.               makestr(&p,*xargv);
  2195.               y = ckindex("=",p,0,0,1);
  2196.               if (y > 0)
  2197.                 p[y-1] = '\0';
  2198.               x = lookup(ftpztab,p,nftpztab,&z);
  2199.               if (x < 0) {
  2200.                   printf("?Invalid security option: \"%s\"\n",p);
  2201.               } else {
  2202.                   if (ftp_deb)
  2203.             printf("Security option: \"%s",p);
  2204.                   if (ftpztab[z].flgs & CM_ARG) {
  2205.                       if (y <= 0)
  2206.                         fatal("?Missing required value");
  2207.                       q = &p[y];
  2208.                       if (!*q)
  2209.                         fatal("?Missing required value");
  2210.                       if (ftp_deb)
  2211.             printf("=%s\"",q);
  2212.                   }
  2213.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2214.                     case FT_NOGSS:
  2215. #ifdef FTP_GSSAPI
  2216.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2217.                           if (ftp_auth_type[z] == FTA_GK5) {
  2218.                               for (y = z;
  2219.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2220.                                    y++
  2221.                                    )
  2222.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2223.                               ftp_auth_type[FTPATYPS-1] = 0;
  2224.                               break;
  2225.                           }
  2226.                       }
  2227. #endif /* FTP_GSSAPI */
  2228.                       break;
  2229.                     case FT_NOK4:
  2230. #ifdef FTP_KRB4
  2231.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2232.                           if (ftp_auth_type[z] == FTA_K4) {
  2233.                               for (y = z;
  2234.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2235.                                    y++
  2236.                                    )
  2237.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2238.                               ftp_auth_type[FTPATYPS-1] = 0;
  2239.                               break;
  2240.                           }
  2241.                       }
  2242. #endif /* FTP_KRB4 */
  2243.                       break;
  2244.                     case FT_NOSRP:
  2245. #ifdef FTP_SRP
  2246.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2247.                           if (ftp_auth_type[z] == FTA_SRP) {
  2248.                               for (y = z;
  2249.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2250.                                    y++
  2251.                                    )
  2252.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2253.                               ftp_auth_type[FTPATYPS-1] = 0;
  2254.                               break;
  2255.                           }
  2256.                       }
  2257. #endif /* FTP_SRP */
  2258.                       break;
  2259.                     case FT_NOSSL:
  2260. #ifdef CK_SSL
  2261.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2262.                           if (ftp_auth_type[z] == FTA_SSL) {
  2263.                               for (y = z;
  2264.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2265.                                    y++
  2266.                                    )
  2267.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2268.                               ftp_auth_type[FTPATYPS-1] = 0;
  2269.                               break;
  2270.                           }
  2271.                       }
  2272. #endif /* CK_SSL */
  2273.                       break;
  2274.                     case FT_NOTLS:
  2275. #ifdef CK_SSL
  2276.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2277.                           if (ftp_auth_type[z] == FTA_TLS) {
  2278.                               for (y = z;
  2279.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2280.                                    y++
  2281.                                    )
  2282.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2283.                               ftp_auth_type[FTPATYPS-1] = 0;
  2284.                               break;
  2285.                           }
  2286.                       }
  2287. #endif /* CK_SSL */
  2288.                       break;
  2289.                     case FT_CERTFI:
  2290. #ifdef CK_SSL
  2291.                       makestr(&ssl_rsa_cert_file,q);
  2292. #endif /* CK_SSL */
  2293.                       break;
  2294.                     case FT_OKCERT:
  2295. #ifdef CK_SSL
  2296.                       ssl_certsok_flag = 1;
  2297. #endif /* CK_SSL */
  2298.                       break;
  2299.                     case FT_DEBUG:
  2300. #ifdef DEBUG
  2301.                       if (deblog) {
  2302.                           extern int debtim;
  2303.                           debtim = 1;
  2304.                       } else {
  2305.                           deblog = debopn("debug.log",0);
  2306.                       }
  2307. #endif /* DEBUG */
  2308.                       break;
  2309.                     case FT_KEY:
  2310. #ifdef CK_SSL
  2311.                       makestr(&ssl_rsa_key_file,q);
  2312. #endif /* CK_SSL */
  2313.                       break;
  2314.                     case FT_SECURE:
  2315.                       /* no equivalent */
  2316.                       break;
  2317.                     case FT_VERIFY:
  2318. #ifdef CK_SSL
  2319.                       if (!rdigits(q))
  2320.                         printf("?Bad number: %s\n",q);
  2321.                       ssl_verify_flag = atoi(q);
  2322. #endif /* CK_SSL */
  2323.                       break;
  2324.                   }
  2325.               }
  2326.               if (ftp_deb) printf("\"\n");
  2327.               free(p);
  2328.               break;
  2329.           }
  2330. #endif /* FTP_SECURITY */
  2331.  
  2332.           default:
  2333.             fatal2(*xargv,
  2334.                    "unknown command-line option, type \"ftp -h\" for help"
  2335.                    );
  2336.         }
  2337.         if (!xp) break;
  2338.         c = *++xp;                      /* See if options are bundled */
  2339.     }
  2340.     return(0);
  2341. }
  2342. #endif /* NOCMDL */
  2343.  
  2344. int
  2345. ftpisconnected() {
  2346.     return(connected);
  2347. }
  2348.  
  2349. int
  2350. ftpisloggedin() {
  2351.     return(connected ? loggedin : 0);
  2352. }
  2353.  
  2354. int
  2355. ftpissecure() {
  2356.     return((ftp_dpl == FPL_CLR) ? 0 : 1);
  2357. }
  2358.  
  2359. static VOID
  2360. ftscreen(n, c, z, s) int n; char c; long z; char * s; {
  2361.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2362.         if (!dpyactive) {
  2363.             ckscreen(SCR_PT,'S',0L,"");
  2364.             dpyactive = 1;
  2365.         }
  2366.         ckscreen(n,c,z,s);
  2367.     }
  2368. }
  2369.  
  2370. #ifndef OS2
  2371. /*  g m s t i m e r  --  Millisecond timer */
  2372.  
  2373. long
  2374. gmstimer() {
  2375. #ifdef HAVE_MSECS
  2376.     /* For those versions of ztime() that also set global ztmsec. */
  2377.     char *p = NULL;
  2378.     long z;
  2379.     ztime(&p);
  2380.     if (!p) return(0L);
  2381.     if (!*p) return(0L);
  2382.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2383.     return(z * 1000 + ztmsec);
  2384. #else
  2385.     return((long)time(NULL) * 1000L);
  2386. #endif /* HAVE_MSECS */
  2387. }
  2388. #endif /* OS2 */
  2389.  
  2390. /*  d o s e t f t p  --  The SET FTP command  */
  2391.  
  2392. int
  2393. dosetftp() {
  2394.     int cx;
  2395.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2396.       return(cx);
  2397.     switch (cx) {
  2398.  
  2399.       case FTS_FNC:                     /* Filename collision action */
  2400.         if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  2401.           return(x);
  2402.         if ((y = cmcfm()) < 0)
  2403.           return(y);
  2404.         ftp_fnc = x;
  2405.         return(1);
  2406.  
  2407.       case FTS_CNV:                     /* Filename conversion */
  2408.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2409.           return(x);
  2410.         if ((y = cmcfm()) < 0)
  2411.           return(y);
  2412.         ftp_cnv = x;
  2413.         return(1);
  2414.  
  2415.       case FTS_DBG:                     /* Debug messages */
  2416.         return(seton(&ftp_deb));
  2417.  
  2418.       case FTS_LOG:                     /* Auto-login */
  2419.         return(seton(&ftp_log));
  2420.  
  2421.       case FTS_PSV:                     /* Passive mode */
  2422.     return(dosetftppsv());
  2423.  
  2424.       case FTS_SPC:                     /* Send port commands */
  2425.         x = seton(&ftp_spc);
  2426.         if (x > 0) sendport = ftp_spc;
  2427.         return(x);
  2428.  
  2429.       case FTS_TYP:                     /* Type */
  2430.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2431.           return(x);
  2432.         if ((y = cmcfm()) < 0) return(y);
  2433.         ftp_typ = x;
  2434.         g_ftp_typ = x;
  2435.         tenex = (ftp_typ == FTT_TEN);
  2436.         return(1);
  2437.  
  2438.       case FTS_USN:                     /* Unique server names */
  2439.         return(seton(&ftp_usn));
  2440.  
  2441.       case FTS_VBM:                     /* Verbose mode */
  2442.         if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
  2443.           return(x);
  2444.         ftp_vbx = ftp_vbm;              /* Global sticky copy */
  2445.         return(x);
  2446.  
  2447.       case FTS_TST:                     /* "if (testing)" messages */
  2448.         return(seton(&testing));
  2449.  
  2450.       case FTS_PRM:                     /* Send permissions */
  2451.         return(setonaut(&ftp_prm));
  2452.  
  2453.       case FTS_AUT:                     /* Auto-authentication */
  2454.         return(seton(&ftp_aut));
  2455.  
  2456.       case FTS_ERR:                     /* Error action */
  2457.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2458.           return(x);
  2459.         if ((y = cmcfm()) < 0)
  2460.           return(y);
  2461.         ftp_err = x;
  2462.         return(success = 1);
  2463.  
  2464. #ifndef NOCSETS
  2465.       case FTS_XLA:                     /* Translation */
  2466.         return(seton(&ftp_xla));
  2467.  
  2468.       case FTS_CSR:                     /* Server charset */
  2469.         if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2470.           return(x);
  2471.         if ((y = cmcfm()) < 0)
  2472.           return(y);
  2473.         ftp_csr = x;
  2474.         ftp_xla = 1;                    /* Also enable translation */
  2475.         return(success = 1);
  2476. #endif /* NOCSETS */
  2477.  
  2478.       case FTS_GFT:
  2479.         return(seton(&get_auto));       /* GET-filetype-switching */
  2480.  
  2481.       case FTS_DAT:
  2482.         return(seton(&ftp_dates));      /* Set file dates */
  2483.  
  2484.       case FTS_STO: {            /* Server time offset */
  2485.       char * s, * p = NULL;
  2486.       int k;
  2487.       if ((x = cmfld("[+-]hh[:mm[:ss]]","+0",&s,xxstring)) < 0)
  2488.         return(x);
  2489.       if (!strcmp(s,"+0")) {
  2490.           s = NULL;
  2491.       } else if ((x = delta2sec(s,&k)) < 0) { /* Check format */
  2492.           printf("?Invalid time offset\n");
  2493.           return(-9);
  2494.       }
  2495.       makestr(&p,s);        /* Make a safe copy the string */
  2496.       if ((x = cmcfm()) < 0) {    /* Get confirmation */
  2497.           if (p)
  2498.         makestr(&p,NULL);
  2499.           return(x);
  2500.       }
  2501.       fts_sto = p;            /* Confirmed - set the string. */
  2502.       return(success = 1);
  2503.       }
  2504.       case FTS_APW: {
  2505.       char * s;
  2506.       if ((x = cmtxt("Text", "", &s, xxstring)) < 0)
  2507.         return(x);
  2508.       makestr(&ftp_apw, *s ? s : NULL);
  2509.       return(success = 1);
  2510.       }
  2511.  
  2512. #ifdef FTP_SECURITY
  2513.       case FTS_CRY:                     /* Auto-encryption */
  2514.         return(seton(&ftp_cry));
  2515.  
  2516.       case FTS_CFW:                     /* Credential-forwarding */
  2517.         return(seton(&ftp_cfw));
  2518.  
  2519.       case FTS_CPL:                     /* Command protection level */
  2520.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2521.         if ((y = cmcfm()) < 0) return(y);
  2522.         success = fts_cpl(x);
  2523.         return(success);
  2524.  
  2525.       case FTS_DPL:                     /* Data protection level */
  2526.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2527.         if ((y = cmcfm()) < 0) return(y);
  2528.           success = fts_dpl(x);
  2529.           return(success);
  2530.  
  2531.       case FTS_ATP: {                   /* FTP Auth Type */
  2532.           int i, j, atypes[8];
  2533.  
  2534.           for (i = 0; i < 8; i++) {
  2535.               if ((y = cmkey(ftpauth,nftpauth,"",
  2536.                              (i == 0) ? "automatic" : "",
  2537.                              xxstring)) < 0) {
  2538.                   if (y == -3)
  2539.                     break;
  2540.                   return(y);
  2541.               }
  2542.               if (i > 0 && (y == FTA_AUTO)) {
  2543.                   printf("?Choice may only be used in first position.\r\n");
  2544.                   return(-9);
  2545.               }
  2546.               for (j = 0; j < i; j++) {
  2547.                   if (atypes[j] == y) {
  2548.                       printf("\r\n?Choice has already been used.\r\n");
  2549.                       return(-9);
  2550.                   }
  2551.               }
  2552.               atypes[i] = y;
  2553.               if (y == FTA_AUTO) {
  2554.                   i++;
  2555.                   break;
  2556.               }
  2557.           }
  2558.           if (i < 8)
  2559.             atypes[i] = 0;
  2560.           if ((z = cmcfm()) < 0)
  2561.             return(z);
  2562.           if (atypes[0] == FTA_AUTO) {
  2563.               i = 0;
  2564. #ifdef FTP_GSSAPI
  2565.               ftp_auth_type[i++] = FTA_GK5;
  2566. #endif /* FTP_GSSAPI */
  2567. #ifdef FTP_SRP
  2568.               ftp_auth_type[i++] = FTA_SRP;
  2569. #endif /* FTP_SRP */
  2570. #ifdef FTP_KRB4
  2571.               ftp_auth_type[i++] = FTA_K4;
  2572. #endif /* FTP_KRB4 */
  2573. #ifdef CK_SSL
  2574.               ftp_auth_type[i++] = FTA_TLS;
  2575.               ftp_auth_type[i++] = FTA_SSL;
  2576. #endif /* CK_SSL */
  2577.               ftp_auth_type[i] = 0;
  2578.           } else {
  2579.               for (i = 0; i < 8; i++)
  2580.                 ftp_auth_type[i] = atypes[i];
  2581.           }
  2582.           return(success = 1);
  2583.       }
  2584.  
  2585.       case FTS_SRP:
  2586. #ifdef FTP_SRP
  2587.         if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2588.           return(x);
  2589.         switch (x) {
  2590.           case SRP_CIPHER:
  2591.             if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2592.               return(x);
  2593.             if ((z = cmcfm()) < 0)
  2594.               return(z);
  2595.             success = !srp_selcipher(ciphertab[x].kwd);
  2596.             return(success);
  2597.           case SRP_HASH:
  2598.             if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2599.               return(x);
  2600.             if ((z = cmcfm()) < 0)
  2601.               return(z);
  2602.             success = !srp_selhash(hashtab[x].kwd);
  2603.             return(success = 1);
  2604.           default:
  2605.             if ((z = cmcfm()) < 0)
  2606.               return(z);
  2607.             return(-2);
  2608.         }
  2609. #else /* FTP_SRP */
  2610.         if ((z = cmcfm()) < 0)
  2611.           return(z);
  2612.         return(-2);
  2613. #endif /* FTP_SRP */
  2614. #endif /* FTP_SECURITY */
  2615.  
  2616.       case FTS_DIS:
  2617.     doxdis(2);            /* 2 == ftp */
  2618.         return(success = 1);
  2619.  
  2620.       default:
  2621.         return(-2);
  2622.     }
  2623. }
  2624.  
  2625. int
  2626. ftpbye() {
  2627.     int x;
  2628.     if (!connected)
  2629.       return(1);
  2630.     if (testing)
  2631.       printf(" ftp closing %s...\n",ftp_host);
  2632.     x = ftpclose();
  2633.     return((x > -1) ? 1 : 0);
  2634. }
  2635.  
  2636. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2637.  
  2638. static int
  2639. openftp(s,opn_tls) char * s; int opn_tls; {
  2640.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2641.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1, nologin = 0;
  2642.     int haveuser = 0;
  2643.     struct FDB sw, fl, cm;
  2644.     extern int nnetdir;                 /* Network services directory */
  2645.     extern int nhcount;                 /* Lookup result */
  2646.     extern char *nh_p[];                /* Network directory entry pointers */
  2647.     extern char *nh_p2[];               /* Network directory entry nettype */
  2648.  
  2649.     if (!s) return(-2);
  2650.     if (!*s) return(-2);
  2651.  
  2652.     makestr(&hostname,s);
  2653.     hostsave = hostname;
  2654.     makestr(&ftp_logname,NULL);
  2655.     anonymous = 0;
  2656.     noinit = 0;
  2657.  
  2658.     debug(F110,"ftp open",hostname,0);
  2659.  
  2660.     if (sav_psv > -1) {                 /* Restore prevailing active/passive */
  2661.         ftp_psv = sav_psv;              /* selection in case it was */
  2662.         sav_psv = -1;                   /* temporarily overriden by a switch */
  2663.     }
  2664.     if (sav_log > -1) {                 /* Ditto for autologin */
  2665.         ftp_log = sav_log;
  2666.         sav_log = -1;
  2667.     }
  2668.     cmfdbi(&sw,                         /* Switches */
  2669.            _CMKEY,
  2670.            "Service name or port;\n or switch",
  2671.            "",                          /* default */
  2672.            "",                          /* addtl string data */
  2673.            nftpswi,                     /* addtl numeric data 1: tbl size */
  2674.            4,                           /* addtl numeric data 2: none */
  2675.            xxstring,                    /* Processing function */
  2676.            ftpswitab,                   /* Keyword table */
  2677.            &fl                          /* Pointer to next FDB */
  2678.            );
  2679.     cmfdbi(&fl,                         /* A host name or address */
  2680.            _CMFLD,                      /* fcode */
  2681.            "",                          /* help */
  2682.            "xYzBoo",                    /* default */
  2683.            "",                          /* addtl string data */
  2684.            0,                           /* addtl numeric data 1 */
  2685.            0,                           /* addtl numeric data 2 */
  2686.            xxstring,
  2687.            NULL,
  2688.            &cm
  2689.            );
  2690.     cmfdbi(&cm,                         /* Command confirmation */
  2691.            _CMCFM,
  2692.            "",
  2693.            "",
  2694.            "",
  2695.            0,
  2696.            0,
  2697.            NULL,
  2698.            NULL,
  2699.            NULL
  2700.            );
  2701.  
  2702.     for (n = 0;; n++) {
  2703.         rc = cmfdb(&sw);                /* Parse a service name or a switch */
  2704.         if (rc < 0)
  2705.           goto xopenftp;
  2706.  
  2707.         if (cmresult.fcode == _CMCFM) { /* Done? */
  2708.             break;
  2709.         } else if (cmresult.fcode == _CMFLD) {  /* Port */
  2710.             if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2711.               makestr(&service,cmresult.sresult);
  2712.             else
  2713.               makestr(&service,opn_tls?"ftps":"ftp");
  2714.         } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2715.             c = cmgbrk();               /* get break character */
  2716.             getval = (c == ':' || c == '=');
  2717.             rc = -9;
  2718.             if (getval && !(cmresult.kflags & CM_ARG)) {
  2719.                 printf("?This switch does not take arguments\n");
  2720.                 goto xopenftp;
  2721.             }
  2722.             if (!getval && (cmresult.kflags & CM_ARG)) {
  2723.                 printf("?This switch requires an argument\n");
  2724.                 goto xopenftp;
  2725.             }
  2726.             switch (cmresult.nresult) { /* Switch */
  2727.               case OPN_ANO:             /* /ANONYMOUS */
  2728.                 anonymous++;
  2729.         nologin = 0;
  2730.                 break;
  2731.               case OPN_NIN:             /* /NOINIT */
  2732.                 noinit++;
  2733.                 break;
  2734.               case OPN_NOL:             /* /NOLOGIN */
  2735.                 nologin++;
  2736.         anonymous = 0;
  2737.         makestr(&ftp_logname,NULL);
  2738.                 break;
  2739.               case OPN_PSW:             /* /PASSWORD */
  2740.                 if (!anonymous)         /* Don't log real passwords */
  2741.                   debok = 0;
  2742.                 rc = cmfld("Password for FTP server","",&p,xxstring);
  2743.                 if (rc == -3) {
  2744.                     makestr(&ftp_tmp,NULL);
  2745.                 } else if (rc < 0) {
  2746.                     goto xopenftp;
  2747.                 } else {
  2748.                     makestr(&ftp_tmp,brstrip(p));
  2749.             nologin = 0;
  2750.                 }
  2751.                 break;
  2752.               case OPN_USR:             /* /USER */
  2753.                 rc = cmfld("Username for FTP server","",&p,xxstring);
  2754.                 if (rc == -3) {
  2755.                     makestr(&ftp_logname,NULL);
  2756.                 } else if (rc < 0) {
  2757.                     goto xopenftp;
  2758.                 } else {
  2759.             nologin = 0;
  2760.                     anonymous = 0;
  2761.             haveuser = 1;
  2762.                     makestr(&ftp_logname,brstrip(p));
  2763.                 }
  2764.                 break;
  2765.               case OPN_ACC:
  2766.                 rc = cmfld("Account for FTP server","",&p,xxstring);
  2767.                 if (rc == -3) {
  2768.                     makestr(&ftp_acc,NULL);
  2769.                 } else if (rc < 0) {
  2770.                     goto xopenftp;
  2771.                 } else {
  2772.                     makestr(&ftp_acc,brstrip(p));
  2773.                 }
  2774.                 break;
  2775.               case OPN_ACT:
  2776.                 opn_psv = 0;
  2777.                 break;
  2778.               case OPN_PSV:
  2779.                 opn_psv = 1;
  2780.                 break;
  2781.               case OPN_TLS:
  2782.                 opn_tls = 1;
  2783.                 break;
  2784.               default:
  2785.                 break;
  2786.             }
  2787.         }
  2788.         if (n == 0) {                   /* After first time through */
  2789.             cmfdbi(&sw,                 /* accept only switches */
  2790.                    _CMKEY,
  2791.                    "\nCarriage return to confirm to command, or switch",
  2792.                    "",
  2793.                    "",
  2794.                    nftpswi,
  2795.                    4,
  2796.                    xxstring,
  2797.                    ftpswitab,
  2798.                    &cm
  2799.                    );
  2800.         }
  2801.     }
  2802. #ifdef COMMENT
  2803.     debug(F100,"ftp openftp while exit","",0);
  2804.     rc = cmcfm();
  2805.     debug(F101,"ftp openftp cmcfm rc","",rc);
  2806.     if (rc < 0)
  2807.       goto xopenftp;
  2808. #endif /* COMMENT */
  2809.  
  2810.     if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
  2811.         sav_psv = ftp_psv;
  2812.         ftp_psv = opn_psv;
  2813.     }
  2814.     if (nologin || haveuser) {        /* /NOLOGIN or /USER switch given */
  2815.     sav_log = ftp_log;
  2816.     ftp_log = haveuser ? 1 : 0;
  2817.     }
  2818.     if (*hostname == '=') {             /* Bypass directory lookup */
  2819.         hostname++;                     /* if hostname starts with '=' */
  2820.         havehost++;
  2821.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  2822.         havehost++;
  2823.     }
  2824.     if (!service)
  2825.       makestr(&service,opn_tls?"ftps":"ftp");
  2826.  
  2827. #ifndef NODIAL
  2828.     if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
  2829.         lunet(hostname);                /* Look up the name */
  2830.         debug(F111,"ftp openftp lunet",hostname,nhcount);
  2831.         if (nhcount == 0) {
  2832.             if (testing)
  2833.               printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2834.             success = ftpopen(hostname,service,opn_tls);
  2835.             debug(F101,"ftp openftp A ftpopen success","",success);
  2836.             rc = success;
  2837.         } else {
  2838.             int found = 0;
  2839.             for (i = 0; i < nhcount; i++) {
  2840.                 if (nh_p2[i])           /* If network type specified */
  2841.                   if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  2842.                     continue;
  2843.                 found++;
  2844.                 makestr(&hostname,nh_p[i]);
  2845.                 debug(F111,"ftpopen lunet substitution",hostname,i);
  2846.                 if (testing)
  2847.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2848.                 success = ftpopen(hostname,service,opn_tls);
  2849.                 debug(F101,"ftp openftp B ftpopen success","",success);
  2850.                 rc = success;
  2851.                 if (success)
  2852.                   break;
  2853.             }
  2854.             if (!found) {               /* E.g. if no network types match */
  2855.                 if (testing)
  2856.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2857.                 success = ftpopen(hostname,service,opn_tls);
  2858.                 debug(F101,"ftp openftp C ftpopen success","",success);
  2859.                 rc = success;
  2860.             }
  2861.         }
  2862.     } else {
  2863. #endif /* NODIAL */
  2864.         if (testing)
  2865.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2866.         success = ftpopen(hostname,service,opn_tls);
  2867.         debug(F111,"ftp openftp D ftpopen success",hostname,success);
  2868.         debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  2869.         rc = success;
  2870. #ifndef NODIAL
  2871.     }
  2872. #endif /* NODIAL */
  2873.  
  2874.   xopenftp:
  2875.     debug(F101,"ftp openftp xopenftp rc","",rc);
  2876.     if (hostsave) free(hostsave);
  2877.     if (service) free(service);
  2878.     if (rc < 0 && ftp_logname) {
  2879.         free(ftp_logname);
  2880.         ftp_logname = NULL;
  2881.     }
  2882.     if (ftp_tmp) {
  2883.         free(ftp_tmp);
  2884.         ftp_tmp = NULL;
  2885.     }
  2886.     return(rc);
  2887. }
  2888.  
  2889. int
  2890. doftpacct() {
  2891.     int x;
  2892.     char * s;
  2893.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  2894.       return(x);
  2895.     CHECKCONN();
  2896.     makestr(&ftp_acc,brstrip(s));
  2897.     if (testing)
  2898.       printf(" ftp account: \"%s\"\n",ftp_acc);
  2899.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  2900.     return(success);
  2901. }
  2902.  
  2903. int
  2904. doftpusr() {                            /* Log in as USER */
  2905.     int x;
  2906.     char *s, * acct = "";
  2907.  
  2908.     debok = 0;                          /* Don't log */
  2909.     if ((x = cmfld("Remote username or ID","",&s,xxstring)) < 0)
  2910.       return(x);
  2911.     ckstrncpy(line,brstrip(s),LINBUFSIZ); /* brstrip: 15 Jan 2003 */
  2912.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0)
  2913.       if (x != -3)
  2914.         return(x);
  2915.     ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  2916.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  2917.                    "", &s, xxstring)) < 0)
  2918.       return(x);
  2919.     CHECKCONN();
  2920.     if (*s) {
  2921.         x = strlen(tmpbuf);
  2922.         if (x > 0) {
  2923.             acct = &tmpbuf[x+2];
  2924.             ckstrncpy(acct,brstrip(s),TMPBUFSIZ - x - 2);
  2925.         }
  2926.     }
  2927.     if (testing)
  2928.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  2929.     success = ftp_user(line,tmpbuf,acct);
  2930. #ifdef CKLOGDIAL
  2931.     dologftp();
  2932. #endif /* CKLOGDIAL */
  2933.     return(success);
  2934. }
  2935.  
  2936. /* DO (various FTP commands)... */
  2937.  
  2938. int
  2939. doftptyp(type) int type; {              /* TYPE */
  2940.     CHECKCONN();
  2941.     ftp_typ = type;
  2942.     changetype(ftp_typ,ftp_vbm);
  2943.     return(1);
  2944. }
  2945.  
  2946. static int
  2947. doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
  2948.     int lcs = -1, rcs = -1;
  2949. #ifndef NOCSETS
  2950.     if (ftp_xla) {
  2951.         lcs = ftp_csl;
  2952.         if (lcs < 0) lcs = fcharset;
  2953.         rcs = ftp_csx;
  2954.         if (rcs < 0) rcs = ftp_csr;
  2955.     }
  2956. #endif /* NOCSETS */
  2957.     debug(F110,"ftp doftpmkd",s,0);
  2958.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2959.       return(success = 1);
  2960.     if (ftpcode == 500 || ftpcode == 502) {
  2961.         if (!quiet)
  2962.           printf("MKD command not recognized, trying XMKD\n");
  2963.         if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2964.           return(success = 1);
  2965.     }
  2966.     return(success = 0);
  2967. }
  2968.  
  2969. static int
  2970. doftpmkd() {                            /* MKDIR parse */
  2971.     int x;
  2972.     char * s;
  2973.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  2974.       return(x);
  2975.     CHECKCONN();
  2976.     ckstrncpy(line,s,LINBUFSIZ);
  2977.     if (testing)
  2978.       printf(" ftp mkdir \"%s\"...\n",line);
  2979.     return(success = doftpxmkd(line,-1));
  2980. }
  2981.  
  2982. static int
  2983. doftprmd() {                            /* RMDIR */
  2984.     int x, lcs = -1, rcs = -1;
  2985.     char * s;
  2986.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  2987.       return(x);
  2988.     CHECKCONN();
  2989.     ckstrncpy(line,s,LINBUFSIZ);
  2990.     if (testing)
  2991.       printf(" ftp rmdir \"%s\"...\n",line);
  2992. #ifndef NOCSETS
  2993.     if (ftp_xla) {
  2994.         lcs = ftp_csl;
  2995.         if (lcs < 0) lcs = fcharset;
  2996.         rcs = ftp_csx;
  2997.         if (rcs < 0) rcs = ftp_csr;
  2998.     }
  2999. #endif /* NOCSETS */
  3000.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  3001.       return(success = 1);
  3002.     if (ftpcode == 500 || ftpcode == 502) {
  3003.         if (!quiet)
  3004.           printf("RMD command not recognized, trying XMKD\n");
  3005.         success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  3006.     } else
  3007.       success = 0;
  3008.     return(success);
  3009. }
  3010.  
  3011. static int
  3012. doftpren() {                            /* RENAME */
  3013.     int x;
  3014.     char * s;
  3015.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  3016.       return(x);
  3017.     ckstrncpy(line,s,LINBUFSIZ);
  3018.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  3019.       return(x);
  3020.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  3021.     if ((x = cmcfm()) < 0)
  3022.       return(x);
  3023.     CHECKCONN();
  3024.     if (testing)
  3025.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  3026.     success = ftp_rename(line,tmpbuf);
  3027.     return(success);
  3028. }
  3029.  
  3030. int
  3031. doftpres() {                            /* RESET (log out without close) */
  3032.     int x;
  3033.     if ((x = cmcfm()) < 0)
  3034.       return(x);
  3035.     CHECKCONN();
  3036.     if (testing)
  3037.       printf(" ftp reset...\n");
  3038.     return(success = ftp_reset());
  3039. }
  3040.  
  3041. static int
  3042. doftpxhlp() {                           /* HELP */
  3043.     int x;
  3044.     char * s;
  3045.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  3046.       return(x);
  3047.     CHECKCONN();
  3048.     ckstrncpy(line,s,LINBUFSIZ);
  3049.     if (testing)
  3050.       printf(" ftp help \"%s\"...\n",line);
  3051.     /* No need to translate -- all FTP commands are ASCII */
  3052.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  3053. }
  3054.  
  3055. static int
  3056. doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
  3057.     int x, lcs = 0, rcs = 0, xlate = 0;
  3058.     char * p, * s, * m = "";
  3059.     if (cx == FTP_VDI) {
  3060.         switch (servertype) {
  3061.           case SYS_VMS:
  3062.           case SYS_DOS:
  3063.           case SYS_TOPS10:
  3064.           case SYS_TOPS20:
  3065.             m = "*.*";
  3066.             break;
  3067.           default:
  3068.             m = "*";
  3069.         }
  3070.     }
  3071.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  3072.       return(x);
  3073.     if ((x = remtxt(&s)) < 0)
  3074.       return(x);
  3075. #ifdef NOCSETS
  3076.     xlate = 0;
  3077. #else
  3078.     xlate = ftp_xla;
  3079. #endif /* NOCSETS */
  3080.     line[0] = NUL;
  3081.     ckstrncpy(line,s,LINBUFSIZ);
  3082.     s = line;
  3083.     CHECKCONN();
  3084.  
  3085. #ifndef NOCSETS
  3086.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  3087.         lcs = ftp_csl;                  /* Local charset */
  3088.         if (lcs < 0) lcs = fcharset;
  3089.         if (lcs < 0) xlate = 0;
  3090.     }
  3091.     if (xlate) {                        /* Still ON? */
  3092.         rcs = ftp_csx;                  /* Remote (Server) charset */
  3093.         if (rcs < 0) rcs = ftp_csr;
  3094.         if (rcs < 0) xlate = 0;
  3095.     }
  3096. #endif /* NOCSETS */
  3097.  
  3098.     if (testing) {
  3099.         p = s;
  3100.         if (!p) p = "";
  3101.         if (*p)
  3102.           printf("Directory of files %s at %s:\n", line, ftp_host);
  3103.         else
  3104.           printf("Directory of files at %s:\n", ftp_host);
  3105.     }
  3106.     debug(F111,"doftpdir",s,cx);
  3107.  
  3108.     if (cx == FTP_DIR) {
  3109.         /* Translation of line[] is done inside recvrequest() */
  3110.         /* when it calls ftpcmd(). */
  3111.         return(success =
  3112.           (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  3113.     }
  3114.     success = 1;                        /* VDIR - one file at a time... */
  3115.     p = (char *)remote_files(1,(CHAR *)s,NULL,0); /* Get the file list */
  3116.     cancelgroup = 0;
  3117.     if (!ftp_vbm && !quiet)
  3118.       printlines = 1;
  3119.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  3120.         if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  3121.             success = 0;
  3122.             break;
  3123.         }
  3124.         p = (char *)remote_files(0,NULL,NULL,0); /* Get next file */
  3125.         debug(F110,"ftp vdir file",s,0);
  3126.     }
  3127.     return(success);
  3128. }
  3129.  
  3130. static int
  3131. doftppwd() {                            /* PWD */
  3132.     int x, lcs = -1, rcs = -1;
  3133. #ifndef NOCSETS
  3134.     if (ftp_xla) {
  3135.         lcs = ftp_csl;
  3136.         if (lcs < 0) lcs = fcharset;
  3137.         rcs = ftp_csx;
  3138.         if (rcs < 0) rcs = ftp_csr;
  3139.     }
  3140. #endif /* NOCSETS */
  3141.     if ((x = cmcfm()) < 0)
  3142.       return(x);
  3143.     CHECKCONN();
  3144.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  3145.         success = 1;
  3146.     } else if (ftpcode == 500 || ftpcode == 502) {
  3147.         if (ftp_deb)
  3148.           printf("PWD command not recognized, trying XPWD\n");
  3149.         success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  3150.     }
  3151.     return(success);
  3152. }
  3153.  
  3154. static int
  3155. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  3156.     int lcs = -1, rcs = -1;
  3157. #ifndef NOCSETS
  3158.     if (ftp_xla) {
  3159.         lcs = ftp_csl;
  3160.         if (lcs < 0) lcs = fcharset;
  3161.         rcs = ftp_csx;
  3162.         if (rcs < 0) rcs = ftp_csr;
  3163.     }
  3164. #endif /* NOCSETS */
  3165.  
  3166.     debug(F110,"ftp doftpcwd",s,0);
  3167.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3168.       return(success = 1);
  3169.     if (ftpcode == 500 || ftpcode == 502) {
  3170.         if (!quiet)
  3171.           printf("CWD command not recognized, trying XCWD\n");
  3172.         if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3173.           return(success = 1);
  3174.     }
  3175.     return(success = 0);
  3176. }
  3177.  
  3178. static int
  3179. doftpcdup() {                           /* CDUP */
  3180.     debug(F100,"ftp doftpcdup","",0);
  3181.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  3182.       return(success = 1);
  3183.     if (ftpcode == 500 || ftpcode == 502) {
  3184.         if (!quiet)
  3185.           printf("CDUP command not recognized, trying XCUP\n");
  3186.         if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  3187.           return(success = 1);
  3188.     }
  3189.     return(success = 0);
  3190. }
  3191.  
  3192. /* s y n c d i r  --  Synchronizes client & server directories */
  3193.  
  3194. /* Used with recursive PUTs; Returns 0 on failure, 1 on success */
  3195.  
  3196. static int cdlevel = 0, cdsimlvl = 0;
  3197.  
  3198. static int
  3199. syncdir(local,sim) char * local; int sim; {
  3200.     char buf[CKMAXPATH+1];
  3201.     char tmp[CKMAXPATH+1];
  3202.     char msgbuf[CKMAXPATH+64];
  3203.     char c, * p = local, * s = buf, * q = buf;
  3204.     int i, k = 0, done = 0, itsadir = 0, saveq;
  3205.  
  3206.     debug(F110,"ftp syncdir local (new)",local,0);
  3207.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  3208.  
  3209.     itsadir = isdir(local);
  3210.     saveq = quiet;
  3211.  
  3212.     while ((*s = *p)) {                 /* Copy the argument filename */
  3213.         if (++k == CKMAXPATH)           /* so we can poke it. */
  3214.           return(-1);
  3215.         if (*s == '/')                  /* Pointer to rightmost dirsep */
  3216.           q = s;
  3217.         s++;
  3218.         p++;
  3219.     }
  3220.     if (!itsadir)
  3221.       *q = NUL;                         /* Keep just the path part */
  3222.  
  3223.     debug(F110,"ftp syncdir buf",buf,0);
  3224.     if (!strcmp(buf,putpath)) {         /* Same as for previous file? */
  3225.         if (itsadir) {                  /* It's a directory? */
  3226.             if (doftpcwd(local,0)) {    /* Try to CD to it */
  3227.                 doftpcdup();            /* Worked - CD back up */
  3228.             } else if (sim) {           /* Simulating... */
  3229.                 if (fdispla == XYFD_B) {
  3230.                     printf("WOULD CREATE DIRECTORY %s\n",local);
  3231.                 } else if (fdispla) {
  3232.                     ckmakmsg(msgbuf,CKMAXPATH,
  3233.                              "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3234.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3235.                 }
  3236.                 /* See note above */
  3237.                 return(0);
  3238.             } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3239.                 return(0);
  3240.             } else {
  3241.                 if (fdispla == XYFD_B) {
  3242.                     printf("CREATED DIRECTORY %s\n",local);
  3243.                 } else if (fdispla) {
  3244.                     ckmakmsg(msgbuf,CKMAXPATH+64,
  3245.                              "CREATED DIRECTORY ",local,NULL,NULL);
  3246.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3247.                 }
  3248.             }
  3249.         }
  3250.         debug(F110,"ftp syncdir no change",buf,0);
  3251.         return(1);                      /* Yes, done. */
  3252.     }
  3253.     ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
  3254.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3255.  
  3256.     p = buf;                            /* New */
  3257.     s = putpath;                        /* Old */
  3258.  
  3259.     debug(F110,"ftp syncdir A p",p,0);
  3260.     debug(F110,"ftp syncdir A s",s,0);
  3261.  
  3262.     while (*p != NUL && *s != NUL && *p == *s) p++,s++;
  3263.  
  3264.     if (*s == '/' && !*p) s++;          /* Don't count initial slash */
  3265.  
  3266.     debug(F110,"ftp syncdir B p",p,0);
  3267.     debug(F110,"ftp syncdir B s",s,0);
  3268.  
  3269.     /* p and s now point to the leftmost spot where they differ */
  3270.  
  3271.     if (*s) {                           /* We have to back up */
  3272.         k = 1;                          /* How many levels */
  3273.         while ((c = *s++)) {            /* Count dirseps */
  3274.             if (c == '/' && *s)
  3275.               k++;
  3276.         }
  3277.         for (i = 0; i < k; i++) {       /* Do that many CDUPs */
  3278.             debug(F111,"ftp syncdir up",p,i+1);
  3279.             if (sim && cdsimlvl) {
  3280.                 cdsimlvl--;
  3281.             } else {
  3282.                 if (!doftpcdup()) {
  3283.                     quiet = saveq;
  3284.                     return(0);
  3285.                 }
  3286.             }
  3287.             cdlevel--;
  3288.         }
  3289.         if (!*p)                        /* If we don't have to go down */
  3290.           goto xcwd;                    /* we're done. */
  3291.     }
  3292.     while (p > buf && *p && *p != '/')  /* If in middle of segment */
  3293.       p--;                              /* back up to beginning */
  3294.     if (*p == '/')                      /* and terminate there */
  3295.       p++;
  3296.  
  3297.     s = p;                              /* Point to start of new down path. */
  3298.     while (1) {                         /* Loop through characters. */
  3299.         if (*s == '/' || !*s) {         /* Have a segment. */
  3300.             if (!*s)                    /* If end of string, */
  3301.               done++;                   /* after this segment we're done. */
  3302.             else
  3303.               *s = NUL;                 /* NUL out the separator. */
  3304.             if (*p) {                   /* If segment is not empty */
  3305.                 debug(F110,"ftp syncdir down segment",p,0);
  3306.                 if (!doftpcwd(p,0)) {   /* Try to CD to it */
  3307.                     if (sim) {
  3308.                         if (fdispla == XYFD_B) {
  3309.                             printf("WOULD CREATE DIRECTORY %s\n",local);
  3310.                         } else if (fdispla) {
  3311.                             ckmakmsg(msgbuf,CKMAXPATH,"WOULD CREATE DIRECTORY",
  3312.                                      local,NULL,NULL);
  3313.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3314.                         }
  3315.                         cdsimlvl++;
  3316.                     } else {
  3317.                         if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3318. /*
  3319.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3320.   FOO but the remote has a regular file with the same name.  We can't CD
  3321.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3322.   the user handle the problem.
  3323. */
  3324.                             quiet = saveq;
  3325.                             return(0);
  3326.                         }
  3327.                         if (fdispla == XYFD_B) {
  3328.                             printf("CREATED DIRECTORY %s\n",p);
  3329.                         } else if (fdispla) {
  3330.                             ckmakmsg(msgbuf,CKMAXPATH,
  3331.                                      "CREATED DIRECTORY ",p,NULL,NULL);
  3332.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3333.                         }
  3334.                         if (!doftpcwd(p,0)) { /* Try again to CD */
  3335.                             quiet = saveq;
  3336.                             return(0);
  3337.                         }
  3338.                     }
  3339.                 }
  3340.                 cdlevel++;
  3341.             }
  3342.             if (done)                   /* Quit if no next segment */
  3343.               break;
  3344.             p = s+1;                    /* Point to next segment */
  3345.         }
  3346.         s++;                            /* Point to next source char */
  3347.     }
  3348.  
  3349.   xcwd:
  3350.     ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
  3351.     quiet = saveq;
  3352.     return(1);
  3353. }
  3354.  
  3355. #ifdef DOUPDATE
  3356. #ifdef DEBUG
  3357. static VOID
  3358. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3359.     if (deblog) {
  3360.         debug(F111,"ftp year ",s,xx->tm_year);
  3361.         debug(F111,"ftp month",s,xx->tm_mon);
  3362.         debug(F111,"ftp day  ",s,xx->tm_mday);
  3363.         debug(F111,"ftp hour ",s,xx->tm_hour);
  3364.         debug(F111,"ftp min  ",s,xx->tm_min);
  3365.         debug(F111,"ftp sec  ",s,xx->tm_sec);
  3366.     }
  3367. }
  3368. #endif /* DEBUG */
  3369.  
  3370. /*  t m c o m p a r e  --  Compare two struct tm's */
  3371.  
  3372. /*  Like strcmp() but for struct tm's  */
  3373. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3374.  
  3375. static int
  3376. tmcompare(xx,yy) struct tm * xx, * yy; {
  3377.  
  3378.     if (xx->tm_year < yy->tm_year)      /* First year less than second */
  3379.       return(-1);
  3380.     if (xx->tm_year > yy->tm_year)      /* First year greater than second */
  3381.       return(1);
  3382.  
  3383.     /* Years are equal so compare months */
  3384.  
  3385.     if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
  3386.       return(-1);
  3387.     if (xx->tm_mon  > yy->tm_mon)
  3388.       return(1);
  3389.  
  3390.     if (xx->tm_mday < yy->tm_mday)
  3391.       return(-1);
  3392.     if (xx->tm_mday > yy->tm_mday)
  3393.       return(1);
  3394.  
  3395.     if (xx->tm_hour < yy->tm_hour)
  3396.       return(-1);
  3397.     if (xx->tm_hour > yy->tm_hour)
  3398.       return(1);
  3399.  
  3400.     if (xx->tm_min  < yy->tm_min)
  3401.       return(-1);
  3402.     if (xx->tm_min  > yy->tm_min)
  3403.       return(1);
  3404.  
  3405.     if (xx->tm_sec  < yy->tm_sec)
  3406.       return(-1);
  3407.     if (xx->tm_sec  > yy->tm_sec)
  3408.       return(1);
  3409.  
  3410.     return(0);
  3411. }
  3412. #endif /* DOUPDATE */
  3413.  
  3414. #ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
  3415. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3416.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3417. };
  3418.  
  3419. /* Macro for whether a given year is a leap year. */
  3420. #define ISLEAP(year) \
  3421. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3422. #endif /* HAVE_TIMEGM */
  3423.  
  3424. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3425.  
  3426. static time_t
  3427. #ifdef CK_ANSIC
  3428. mkutime(struct tm * tm)
  3429. #else
  3430. mkutime(tm) struct tm * tm;
  3431. #endif /* CK_ANSIC */
  3432. /* mkutime */ {
  3433. #ifdef HAVE_TIMEGM
  3434.     return(timegm(tm));                 /* Have system service, use it. */
  3435. #else
  3436. /*
  3437.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3438.   Given a struct tm representing a calendar time in UTC, convert it to
  3439.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3440.   convertable.  Note that this function does not canonicalize the provided
  3441.   struct tm, nor does it allow out-of-range values or years before 1970.
  3442.   Result should be identical with timegm().
  3443. */
  3444.     time_t result = 0;
  3445.     int i;
  3446.     /*
  3447.       We do allow some ill-formed dates, but we don't do anything special
  3448.       with them and our callers really shouldn't pass them to us.  Do
  3449.       explicitly disallow the ones that would cause invalid array accesses
  3450.       or other algorithm problems.
  3451.     */
  3452. #ifdef DEBUG
  3453.     if (deblog) {
  3454.         debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3455.         debug(F101,"mkutime tm_year","",tm->tm_year);
  3456.     }
  3457. #endif /* DEBUG */
  3458.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3459.       return((time_t) -1);
  3460.  
  3461.     /* Convert to time_t. */
  3462.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3463.       result += 365 + ISLEAP(i);
  3464.     for (i = 0; i < tm->tm_mon; i++)
  3465.       result += MONTHDAYS[i];
  3466.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3467.       result++;
  3468.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3469.     result = 60 * result + tm->tm_min;
  3470.     result = 60 * result + tm->tm_sec;
  3471.     debug(F101,"mkutime result","",result);
  3472.     return(result);
  3473. #endif /* HAVE_TIMEGM */
  3474. }
  3475.  
  3476.  
  3477. /*
  3478.   s e t m o d t i m e  --  Set file modification time.
  3479.  
  3480.   f = char * filename;
  3481.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3482.  
  3483.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3484.   Returns:
  3485.     0 on success,
  3486.    -1 on error.
  3487.  
  3488. */
  3489. static int
  3490. #ifdef CK_ANSIC
  3491. setmodtime(char * f, time_t t)
  3492. #else
  3493. setmodtime(f,t) char * f; time_t t;
  3494. #endif /* CK_ANSIC */
  3495. /* setmodtime */ {
  3496. #ifdef NT
  3497.     struct _stat sb;
  3498. #else /* NT */
  3499.     struct stat sb;
  3500. #endif /* NT */
  3501.     int x, rc = 0;
  3502. #ifdef BSD44
  3503.     struct timeval tp[2];
  3504. #else
  3505. #ifdef V7
  3506.     struct utimbuf {
  3507.         time_t timep[2];
  3508.     } tp;
  3509. #else
  3510. #ifdef SYSUTIMEH
  3511. #ifdef NT
  3512.     struct _utimbuf tp;
  3513. #else /* NT */
  3514.     struct utimbuf tp;
  3515. #endif /* NT */
  3516. #else
  3517.     struct utimbuf {
  3518.         time_t atime;
  3519.         time_t mtime;
  3520.     } tp;
  3521. #endif /* SYSUTIMEH */
  3522. #endif /* V7 */
  3523. #endif /* BSD44 */
  3524.  
  3525.     if (stat(f,&sb) < 0) {
  3526.         debug(F111,"setmodtime stat failure",f,errno);
  3527.         return(-1);
  3528.     }
  3529. #ifdef BSD44
  3530.     tp[0].tv_sec = sb.st_atime;         /* Access time first */
  3531.     tp[1].tv_sec = t;                   /* Update time second */
  3532.     debug(F111,"setmodtime BSD44",f,t);
  3533. #else
  3534. #ifdef V7
  3535.     tp.timep[0] = t;                    /* Set modif. time to creation date */
  3536.     tp.timep[1] = sb.st_atime;          /* Don't change the access time */
  3537.     debug(F111,"setmodtime V7",f,t);
  3538. #else
  3539. #ifdef SYSUTIMEH
  3540.     tp.modtime = t;                     /* Set modif. time to creation date */
  3541.     tp.actime = sb.st_atime;            /* Don't change the access time */
  3542.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3543. #else
  3544.     tp.mtime = t;                       /* Set modif. time to creation date */
  3545.     tp.atime = sb.st_atime;             /* Don't change the access time */
  3546.     debug(F111,"setmodtime (other)",f,t);
  3547. #endif /* SYSUTIMEH */
  3548. #endif /* V7 */
  3549. #endif /* BSD44 */
  3550.  
  3551.     /* Try to set the file date */
  3552.  
  3553. #ifdef BSD44
  3554.     x = utimes(f,tp);
  3555.     debug(F111,"setmodtime utimes()","BSD44",x);
  3556. #else
  3557. #ifdef IRIX65
  3558.     {
  3559.       /*
  3560.         The following produces the nonsensical warning:
  3561.         Argument  of type "const struct utimbuf *" is incompatible with
  3562.         parameter of type "const struct utimbuf *".  If you can make it
  3563.         go away, be my guest.
  3564.       */
  3565.         const struct utimbuf * t2 = &tp;
  3566.         x = utime(f,t2);
  3567.     }
  3568. #else
  3569.     x = utime(f,&tp);
  3570.     debug(F111,"setmodtime utime()","other",x);
  3571. #endif /* IRIX65 */
  3572. #endif /* BSD44 */
  3573.     if (x)
  3574.       rc = -1;
  3575.  
  3576.     debug(F101,"setmodtime result","",rc);
  3577.     return(rc);
  3578. }
  3579.  
  3580.  
  3581. /*
  3582.   c h k m o d t i m e  --  Check/Set file modification time.
  3583.  
  3584.   fc = function code:
  3585.     0 = Check; returns:
  3586.       -1 on error,
  3587.        0 if local older than remote,
  3588.        1 if modtimes are equal,
  3589.        2 if local newer than remote.
  3590.     1 = Set (local file's modtime from remote's); returns:
  3591.       -1 on error,
  3592.        0 on success.
  3593. */
  3594. static int
  3595. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3596. #ifdef NT
  3597.     struct _stat statbuf;
  3598. #else /* NT */
  3599.     struct stat statbuf;
  3600. #endif /* NT */
  3601.     struct tm * tmlocal = NULL;
  3602.     struct tm tmremote;
  3603.     int rc = 0, havedate = 0, lcs = -1, rcs = -1, flag = 0;
  3604.     char * s, timebuf[64];
  3605.  
  3606.     debug(F111,"chkmodtime",local,mdtmok);
  3607.     if (!mdtmok)            /* Server supports MDTM? */
  3608.       return(-1);            /* No don't bother. */
  3609.  
  3610. #ifndef NOCSETS
  3611.     if (ftp_xla) {
  3612.         lcs = ftp_csl;
  3613.         if (lcs < 0) lcs = fcharset;
  3614.         rcs = ftp_csx;
  3615.         if (rcs < 0) rcs = ftp_csr;
  3616.     }
  3617. #endif /* NOCSETS */
  3618.  
  3619.     if (fc == 0) {
  3620.         rc = stat(local,&statbuf);
  3621.         if (rc == 0) {                  /* Get local file's mod time */
  3622.             tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3623. #ifdef DEBUG
  3624.             if (tmlocal) {
  3625.                 dbtime(local,tmlocal);
  3626.             }
  3627. #endif /* DEBUG */
  3628.         }
  3629.     }
  3630.     /* Get remote file's mod time as yyyymmddhhmmss */
  3631.  
  3632.     if (havemdtm) {            /* Already got it from MLSD? */
  3633.     s = havemdtm;
  3634.     flag++;
  3635.     } else if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3636.         char c;
  3637.         bzero((char *)&tmremote, sizeof(struct tm));
  3638.         s = ftp_reply_str;
  3639.         while ((c = *s++)) {            /* Skip past response code */
  3640.             if (c == SP) {
  3641.                 flag++;
  3642.                 break;
  3643.             }
  3644.         }
  3645.     }
  3646.     if (flag) {
  3647.     debug(F111,"ftp chkmodtime string",s,flag);
  3648.     if (fts_sto) {            /* User gave server time offset? */
  3649.         char * p;
  3650.         debug(F110,"ftp chkmodtime offset",fts_sto,0);
  3651.         ckmakmsg(timebuf,64,s," ",fts_sto,NULL); /* Build delta time */
  3652.         if ((p = cmcvtdate(timebuf,1))) { /* Apply delta time */
  3653.         ckstrncpy(timebuf,p,64);      /* Convert to MDTM format */
  3654.         timebuf[8]  = timebuf[9];  /* h */
  3655.         timebuf[9]  = timebuf[10]; /* h */
  3656.         timebuf[10] = timebuf[12]; /* m */
  3657.         timebuf[11] = timebuf[13]; /* m */
  3658.         timebuf[12] = timebuf[12]; /* s */
  3659.         timebuf[13] = timebuf[13]; /* s */
  3660.         timebuf[14] = NUL;
  3661.         s = timebuf;
  3662.         debug(F110,"ftp chkmodtime adjust",s,0);
  3663.         }
  3664.     }
  3665.         if (flag) {                     /* Convert to struct tm */
  3666.             char * pat;
  3667.             int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
  3668.             if (!ckstrcmp(s,"191",3,0)) {
  3669.                 pat = "%05d%02d%02d%02d%02d%02d";
  3670.                 y2kbug++;
  3671.                 debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3672.             } else {
  3673.                 pat = "%04d%02d%02d%02d%02d%02d";
  3674.             }
  3675.             if (sscanf(s,               /* Parse into struct tm */
  3676.                        pat,
  3677.                        &(tmremote.tm_year),
  3678.                        &(tmremote.tm_mon),
  3679.                        &(tmremote.tm_mday),
  3680.                        &(tmremote.tm_hour),
  3681.                        &(tmremote.tm_min),
  3682.                        &(tmremote.tm_sec)
  3683.                        ) == 6) {
  3684.                 tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3685.                 debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3686.                 tmremote.tm_mon--;
  3687.  
  3688. #ifdef DEBUG
  3689.         debug(F100,"SERVER TIME FOLLOWS:","",0);
  3690.                 dbtime(remote,&tmremote);
  3691. #endif /* DEBUG */
  3692.  
  3693.                 if (havedate > -1)
  3694.           havedate = 1;
  3695.             }
  3696.         }
  3697.     } else {                /* Failed */
  3698.     debug(F101,"ftp chkmodtime ftpcode","",ftpcode);
  3699.     if (ftpcode == 500 ||        /* Command unrecognized */
  3700.         ftpcode == 502 ||        /* Command not implemented */
  3701.         ftpcode == 202)        /* Command superfluous */
  3702.       mdtmok = 0;            /* Don't ask this server again */
  3703.     return(-1);
  3704.     }
  3705.     if (fc == 0) {                      /* Compare */
  3706.         if (havedate == 1) {        /* Only if we have both file dates */
  3707.             /*
  3708.               Compare with local file's time.  We don't use
  3709.               clock time (time_t) here in case of signed/unsigned
  3710.               confusion, etc.
  3711.             */
  3712.         int xx;
  3713. #ifdef COMMENT
  3714. #ifdef DEBUG        
  3715.         if (deblog) {
  3716.         dbtime("LOCAL",tmlocal);
  3717.         dbtime("REMOT",&tmremote);
  3718.         }
  3719. #endif /* DEBUG */
  3720. #endif /* COMMENT */
  3721.         xx = tmcompare(tmlocal,&tmremote);
  3722.         debug(F101,"chkmodtime tmcompare","",xx);
  3723.             return(xx + 1);
  3724.         }
  3725.     } else if (ftp_dates) {             /* Set */
  3726.         /*
  3727.           Here we must convert struct tm to time_t
  3728.           without applying timezone conversion, for which
  3729.           there is no portable API.  The method is hidden
  3730.           in mkutime(), defined above.
  3731.         */
  3732.         time_t utc;
  3733.         utc = mkutime(&tmremote);
  3734.         debug(F111,"ftp chkmodtime mkutime",remote,utc);
  3735.         if (utc != (time_t)-1)
  3736.           return(setmodtime(local,utc));
  3737.     }
  3738.     return(-1);
  3739. }
  3740.  
  3741. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  3742.  
  3743. static int
  3744. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  3745.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  3746. /* getfile */ {
  3747.     int rc = -1;
  3748.     ULONG t0, t1;
  3749.  
  3750. #ifdef GFTIMER
  3751.     CKFLOAT sec;
  3752. #else
  3753.     int sec = 0;
  3754. #endif /* GFTIMER */
  3755.     char fullname[CKMAXPATH+1];
  3756.  
  3757.     debug(F110,"ftp getfile remote A",remote,0);
  3758.     debug(F110,"ftp getfile local A",local,0);
  3759.     debug(F110,"ftp getfile pipename",pipename,0);
  3760.     if (!remote) remote = "";
  3761.  
  3762. #ifdef PATTERNS
  3763.     /* Automatic type switching? */
  3764.     if (xfermode == XMODE_A && patterns && get_auto && !forcetype) {
  3765.         int x;
  3766.         x = matchname(remote,0,servertype);
  3767.         debug(F111,"ftp getfile matchname",remote,x);
  3768.         switch (x) {
  3769.           case 0: ftp_typ = FTT_ASC; break;
  3770.           case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  3771.           default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  3772.         }
  3773.         changetype(ftp_typ,ftp_vbm);
  3774.         binary = ftp_typ;               /* For file-transfer display */
  3775.     }
  3776. #endif /* PATTERNS */
  3777.  
  3778. #ifndef NOCSETS
  3779.     ftp_csx = -1;                       /* For file-transfer display */
  3780.     ftp_csl = -1;                       /* ... */
  3781.  
  3782.     if (rcs > -1)                       /* -1 means no translation */
  3783.       if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
  3784.         if (fcs < 0)                    /* File charset not forced? */
  3785.           fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
  3786.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3787.         debug(F110,"ftp getfile","initxlate",0);
  3788.         initxlate(rcs,fcs);             /* NB: opposite order of PUT */
  3789.         ftp_csx = rcs;
  3790.         ftp_csl = fcs;
  3791.     } else
  3792.       xlate = 0;
  3793. #endif /* NOCSETS */
  3794.  
  3795.     if (!pipename && (!local || !local[0]))
  3796.       local = remote;
  3797.  
  3798.     out2screen = !strcmp(local,"-");
  3799.  
  3800.     fullname[0] = NUL;
  3801.     if (pipename) {
  3802.         ckstrncpy(fullname,pipename,CKMAXPATH+1);
  3803.     } else {
  3804.         zfnqfp(local,CKMAXPATH,fullname);
  3805.         if (!fullname[0])
  3806.           ckstrncpy(fullname,local,CKMAXPATH+1);
  3807.     }
  3808.     if (!out2screen && displa && fdispla) { /* Screen */
  3809.         ftscreen(SCR_FN,'F',(long)pktnum,remote);
  3810.         ftscreen(SCR_AN,0,0L,fullname);
  3811.         ftscreen(SCR_FS,0,fsize,"");
  3812.     }
  3813.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  3814.     tlog(F110," as",fullname,0);
  3815.     debug(F111,"ftp getfile size",remote,fsize);
  3816.     debug(F111,"ftp getfile local",local,out2screen);
  3817.  
  3818.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  3819.  
  3820.     t0 = gmstimer();                    /* Start time */
  3821.     debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
  3822.     rc = recvrequest("RETR",
  3823.                      local,
  3824.                      remote,
  3825.                      append ? "ab" : "wb",
  3826.                      0,
  3827.                      recover,
  3828.                      pipename,
  3829.                      xlate,
  3830.                      fcs,
  3831.                      rcs
  3832.                      );
  3833.     t1 = gmstimer();                    /* End time */
  3834.     debug(F111,"ftp getfile t1",remote,t1);
  3835.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  3836. #ifdef GFTIMER
  3837.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3838.     fpxfsecs = sec;                     /* (for doxlog()) */
  3839. #else
  3840.     sec = (t1 - t0) / 1000;
  3841.     xfsecs = (int)sec;
  3842. #endif /* GFTIMER */
  3843.     debug(F111,"ftp recvrequest rc",remote,rc);
  3844.     if (cancelfile || cancelgroup) {
  3845.         debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  3846.         ftscreen(SCR_ST,ST_INT,0l,"");
  3847.     } else if (rc > 0) {
  3848.         debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  3849.         ftscreen(SCR_ST,ST_SKIP,0l,cmarg);
  3850.     } else if (rc < 0) {
  3851.         switch (ftpcode) {
  3852.           case -4:                      /* Network error */
  3853.           case -2:                      /* File error */
  3854.             ftscreen(SCR_ST,ST_MSG,0l,ck_errstr());
  3855.             break;
  3856.           case -3:
  3857.             ftscreen(SCR_ST,ST_MSG,0l,"Failure to make data connection");
  3858.             break;
  3859.           case -1:
  3860.             ftscreen(SCR_ST,ST_INT,0l,""); /* (should be covered above) */
  3861.             break;
  3862.           default:
  3863.             ftscreen(SCR_ST,ST_MSG,0l,&ftp_reply_str[4]);
  3864.         }
  3865.     } else {                            /* Tudo bem */
  3866.         ftscreen(SCR_PT,'Z',0L,"");
  3867.         if (rc == 0) {
  3868.             ftscreen(SCR_ST,ST_OK,0L,""); /* For screen */
  3869.             makestr(&rrfspec,remote);     /* For WHERE command */
  3870.             makestr(&rfspec,fullname);
  3871.         }
  3872.     }
  3873.     if (ftp_dates)            /* If FTP DATES ON... */
  3874.       if (!pipename && !out2screen)    /* and it's a real file */
  3875.     if (rc < 1 && rc != -3)        /* and it wasn't skipped */
  3876.       if (connected)        /* and we still have a connection */
  3877.         if (zchki(local) > -1) {    /* and the file wasn't discarded */
  3878.         chkmodtime(local,remote,1); /* set local file date */
  3879.         debug(F110,"ftp get set date",local,0);
  3880.         }
  3881.     filcnt++;                           /* Used by \v(filenum) */
  3882. #ifdef TLOG
  3883.     if (tralog) {
  3884.         if (rc > 0) {
  3885.             tlog(F100," recovery skipped","",0);
  3886.         } else if (rc == 0) {
  3887.             tlog(F101," complete, size", "", fsize);
  3888.         } else if (cancelfile) {
  3889.             tlog(F100," canceled by user","",0);
  3890.         } else {
  3891.             tlog(F110," failed:",ftp_reply_str,0);
  3892.         }
  3893.         if (!tlogfmt)
  3894.           doxlog(what,local,fsize,ftp_typ,rc,"");
  3895.     }
  3896. #endif /* TLOG */
  3897.     return(rc);
  3898. }
  3899.  
  3900. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  3901. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  3902.  
  3903. static int
  3904. putfile(cx,
  3905.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  3906.     char * local, * remote, * mvto, *rnto, *srvrn;
  3907.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg;
  3908.  
  3909. /* putfile */ {
  3910.  
  3911.     char asname[CKMAXPATH+1];
  3912.     char fullname[CKMAXPATH+1];
  3913.     int k = -1, x = 0, y = 0, o = -1, rc = 0, nc = 0;
  3914.     int xlate = 0, restart = 0, mt = -1;
  3915.     char * s = NULL, * cmd = NULL;
  3916.     ULONG t0 = 0, t1 = 0;        /* Times for stats */
  3917.     int ofcs = 0, orcs = 0;
  3918.  
  3919. #ifdef GFTIMER
  3920.     CKFLOAT sec = 0.0;
  3921. #else
  3922.     int sec = 0;
  3923. #endif /* GFTIMER */
  3924.     debug(F111,"ftp putfile flg",local,flg);
  3925.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  3926.     debug(F101,"ftp putfile fcs","",fcs);
  3927.     debug(F101,"ftp putfile rcs","",rcs);
  3928.  
  3929.     ofcs = fcs;                         /* Save charset args */
  3930.     orcs = rcs;
  3931.  
  3932.     sendstart = 0L;
  3933.     restart = flg & PUT_RES;
  3934.     if (!remote)
  3935.       remote = "";
  3936.  
  3937.     /* FTP protocol command to send to server */
  3938.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  3939.  
  3940.     if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
  3941.         if (alike) {                    /* If server & client are alike */
  3942.             nc = 0;                     /* no conversion */
  3943.         } else {                        /* If they are different */
  3944.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  3945.               nc = -1;                  /* only minimal conversions needed */
  3946.             else                        /* otherwise */
  3947.               nc = 1;                   /* full conversion */
  3948.         }
  3949.     } else                              /* Not auto - do what user said */
  3950.       nc = x_cnv;
  3951.  
  3952.     /* If Transfer Mode is Automatic, determine file type */
  3953.     if (xfermode == XMODE_A && filepeek && !pipesend) {
  3954.         if (isdir(local)) {             /* If it's a directory */
  3955.             k = FT_BIN;                 /* skip the file scan */
  3956.         } else {
  3957.             debug(F110,"FTP PUT calling scanfile",local,0);
  3958.             k = scanfile(local,&o,nscanfile); /* Scan the file */
  3959.         }
  3960.         debug(F111,"FTP PUT scanfile",local,k);
  3961.         if (k > -1 && !forcetype) {
  3962.             ftp_typ = (k == FT_BIN) ? 1 : 0;
  3963.             if (xft > -1 && ftp_typ != xft) {
  3964.                 if (flg & PUT_SIM)
  3965.                   tlog(F110,"ftp put SKIP (Type):", local, 0);
  3966.                 return(SKP_TYP);
  3967.             }
  3968.             if (ftp_typ == 1 && tenex)  /* User said TENEX? */
  3969.               ftp_typ = FTT_TEN;
  3970.         }
  3971.     }
  3972. #ifndef NOCSETS
  3973.     ftp_csx = -1;                       /* For file-transfer display */
  3974.     ftp_csl = -1;                       /* ... */
  3975.  
  3976.     if (rcs > -1) {                     /* -1 means no translation */
  3977.         if (ftp_typ == 0) {             /* File type is "ascii"? */
  3978.             if (fcs < 0) {              /* File charset not forced? */
  3979.                 if (k < 0) {            /* If we didn't scan */
  3980.                     fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
  3981.                 } else {                /* If we did scan, use scan result */
  3982.                     switch (k) {
  3983.                       case FT_TEXT:     /* Unknown text */
  3984.                         fcs = fcharset;
  3985.                         break;
  3986.                       case FT_7BIT:     /* 7-bit text */
  3987.                         fcs = dcset7;
  3988.                         break;
  3989.                       case FT_8BIT:     /* 8-bit text */
  3990.                         fcs = dcset8;
  3991.                         break;
  3992.                       case FT_UTF8:     /* UTF-8 */
  3993.                         fcs = FC_UTF8;
  3994.                         break;
  3995.                       case FT_UCS2:     /* UCS-2 */
  3996.                         fcs = FC_UCS2;
  3997.                         if (o > -1)     /* Input file byte order */
  3998.                           fileorder = o;
  3999.                         break;
  4000.                       default:
  4001.                         rcs = -1;
  4002.                     }
  4003.                 }
  4004.             }
  4005.         }
  4006.     }
  4007.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  4008.         debug(F110,"ftp putfile","initxlate",0);
  4009.         initxlate(fcs,rcs);
  4010.         debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  4011.         xlate = 1;
  4012.         ftp_csx = rcs;
  4013.         ftp_csl = fcs;
  4014.     }
  4015. #endif /* NOCSETS */
  4016.  
  4017.     binary = ftp_typ;                   /* For file-transfer display */
  4018.     asname[0] = NUL;
  4019.  
  4020.     if (recursive) {                    /* If sending recursively, */
  4021.         if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  4022.           return(-1);                   /* Don't PUT if it fails. */
  4023.         else if (isdir(local))          /* It's a directory */
  4024.           return(0);                    /* Don't send it! */
  4025.     }
  4026.     if (*remote) {                      /* If an as-name template was given */
  4027. #ifndef NOSPL
  4028.         if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
  4029.             y = CKMAXPATH;              /* evaluate it for this file */
  4030.             s = asname;
  4031.             zzstring(remote,&s,&y);
  4032.         } else
  4033. #endif /* NOSPL */
  4034.           ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  4035.     } else {                                    /* No as-name */
  4036.         nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  4037.         debug(F110,"FTP PUT nzltor",asname,0);
  4038.     }
  4039.     /* Preliminary messages and log entries */
  4040.  
  4041.     fullname[0] = NUL;
  4042.     zfnqfp(local,CKMAXPATH,fullname);
  4043.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  4044.     fullname[CKMAXPATH] = NUL;
  4045.  
  4046.     if (displa && fdispla) {            /* Screen */
  4047.         ftscreen(SCR_FN,'F',(long)pktnum,local);
  4048.         ftscreen(SCR_AN,0,0L,asname);
  4049.         ftscreen(SCR_FS,0,fsize,"");
  4050.     }
  4051. #ifdef DOUPDATE
  4052.     if (flg & (PUT_UPD|PUT_DIF)) {    /* Date-checking modes... */
  4053.         mt = chkmodtime(fullname,asname,0);
  4054.         debug(F111,"ftp putfile chkmodtime",asname,mt);
  4055.         if (mt == 0 && ((flg & PUT_DIF) == 0)) { /* Local is older */
  4056.             tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  4057.             ftscreen(SCR_ST,ST_SKIP,SKP_DAT,fullname); /* Skip this one */
  4058.             filcnt++;
  4059.             return(SKP_DAT);
  4060.         } else if (mt == 1) {           /* Times are equal */
  4061.             tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  4062.             ftscreen(SCR_ST,ST_SKIP,SKP_EQU,fullname); /* Skip it */
  4063.             filcnt++;
  4064.             return(SKP_DAT);
  4065.         }
  4066.     /* Local file is newer */
  4067.         tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  4068.              "ftp put /update TEXT:", fullname, 0);
  4069.     } else if (flg & PUT_RES) {
  4070.         tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  4071.              "ftp put /recover TEXT:", fullname, 0);
  4072.     } else {
  4073.         tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4074.     }
  4075. #else
  4076.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4077. #endif /* DOUPDATE */
  4078.     tlog(F110," as",asname,0);
  4079.  
  4080. #ifndef NOCSETS
  4081.     if (xlate) {
  4082.         debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  4083.         tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  4084.         tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  4085.     } else if (!ftp_typ) {
  4086.         tlog(F110," character sets:","no conversion",0);
  4087.         fcs = ofcs;                     /* Binary file but we still must */
  4088.         rcs = orcs;                     /* translate its name */
  4089.     }
  4090. #endif /* NOCSETS */
  4091.  
  4092.     /* PUT THE FILE */
  4093.  
  4094.     t0 = gmstimer();                    /* Start time */
  4095.     if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
  4096.         if (flg & (PUT_UPD|PUT_DIF)) {    /* (see SKP_xxx in ckcker.h) */
  4097.             rc = (mt < 0) ?             /* Update mode... */
  4098.               SKP_XNX :                 /* Remote file doesn't exist */
  4099.                 SKP_XUP;                /* Remote file is older */
  4100.         } else {
  4101.             rc = SKP_SIM;               /* "Would be sent", period. */
  4102.         }
  4103.     } else {
  4104.         rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  4105.     }
  4106.     t1 = gmstimer();                    /* End time */
  4107.     filcnt++;                           /* File number */
  4108.  
  4109. #ifdef GFTIMER
  4110.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4111.     fpxfsecs = sec;                     /* (for doxlog()) */
  4112. #else
  4113.     sec = (t1 - t0) / 1000;
  4114.     xfsecs = (int)sec;
  4115. #endif /* GFTIMER */
  4116.  
  4117.     debug(F111,"ftp sendrequest rc",local,rc);
  4118.  
  4119.     if (cancelfile || cancelgroup) {
  4120.         debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  4121.         ftscreen(SCR_ST,ST_INT,0l,"");
  4122.     } else if (rc > 0) {
  4123.         debug(F101,"ftp put skipped",local,rc);
  4124.         ftscreen(SCR_ST,ST_SKIP,rc,fullname);
  4125.     } else if (rc < 0) {
  4126.         debug(F111,"ftp put error",local,ftpcode);
  4127.         ftscreen(SCR_ST,ST_MSG,0L,&ftp_reply_str[4]);
  4128.     } else {
  4129.         debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  4130.         ftscreen(SCR_PT,'Z',0L,"");
  4131.         debug(F111,"ftp put ST_OK",local,rc);
  4132.         ftscreen(SCR_ST,ST_OK,0L,"");
  4133.         debug(F110,"ftp put old sfspec",sfspec,0);
  4134.         makestr(&sfspec,fullname);      /* For WHERE command */
  4135.         debug(F110,"ftp put new sfspec",sfspec,0);
  4136.         debug(F110,"ftp put old srfspec",srfspec,0);
  4137.         makestr(&srfspec,asname);
  4138.         debug(F110,"ftp put new srfspec",srfspec,0);
  4139.     }
  4140.  
  4141.     /* Final log entries */
  4142.  
  4143. #ifdef TLOG
  4144.     if (tralog) {
  4145.         if (rc > 0) {
  4146.             if (rc == SKP_XNX)
  4147.               tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  4148.             else if (rc == SKP_XUP)
  4149.               tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  4150.             else if (rc == SKP_SIM)
  4151.               tlog(F100," /simulate: WOULD BE SENT","",0);
  4152.             else
  4153.               tlog(F110," skipped:",gskreason(rc),0);
  4154.         } else if (rc == 0) {
  4155.             tlog(F101," complete, size", "", fsize);
  4156.         } else if (cancelfile) {
  4157.             tlog(F100," canceled by user","",0);
  4158.         } else {
  4159.             tlog(F110," failed:",ftp_reply_str,0);
  4160.         }
  4161.         if (!tlogfmt)
  4162.           doxlog(what,local,fsize,ftp_typ,rc,"");
  4163.     }
  4164. #endif /* TLOG */
  4165.  
  4166.     if (rc < 0)                         /* PUT did not succeed */
  4167.       return(-1);                       /* so done. */
  4168.  
  4169.     if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
  4170.       return(SKP_SIM);
  4171.  
  4172. #ifdef UNIX
  4173.     /* Set permissions too? */
  4174.  
  4175.     if (prm) {                          /* Change permissions? */
  4176.         s = zgperm(local);              /* Get perms of local file */
  4177.         if (!s) s = "";
  4178.         x = strlen(s);
  4179.         if (x > 3) s += (x - 3);
  4180.         if (rdigits(s)) {
  4181.             ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  4182.             x =
  4183.               ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  4184.             tlog(F110, x ? " chmod" : " chmod failed",
  4185.                  s,
  4186.                  0
  4187.                  );
  4188.             if (!x)
  4189.               return(-1);
  4190.         }
  4191.     }
  4192. #endif /* UNIX */
  4193.  
  4194.     /* Disposition of source file */
  4195.  
  4196.     if (moving) {
  4197.         x = zdelet(local);
  4198.         tlog(F110, (x > -1) ?
  4199.              " deleted" : " failed to delete",
  4200.              local,
  4201.              0
  4202.              );
  4203.         if (x < 0)
  4204.           return(-1);
  4205.     } else if (mvto) {
  4206.         x = zrename(local,mvto);
  4207.         tlog(F110, (x > -1) ?
  4208.              " moved source to" : " failed to move source to",
  4209.              mvto,
  4210.              0
  4211.              );
  4212.         if (x < 0)
  4213.           return(-1);
  4214.         /* ftscreen(SCR_ST,ST_MSG,0L,mvto); */
  4215.  
  4216.     } else if (rnto) {
  4217.         char * s = rnto;
  4218. #ifndef NOSPL
  4219.         int y;                          /* Pass it thru the evaluator */
  4220.         extern int cmd_quoting;         /* for \v(filename) */
  4221.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4222.             y = CKMAXPATH;
  4223.             s = (char *)asname;
  4224.             zzstring(rnto,&s,&y);
  4225.             s = (char *)asname;
  4226.         }
  4227. #endif /* NOSPL */
  4228.         if (s) if (*s) {
  4229.             int x;
  4230.             x = zrename(local,s);
  4231.             tlog(F110, (x > -1) ?
  4232.                  " renamed source file to" :
  4233.                  " failed to rename source file to",
  4234.                  s,
  4235.                  0
  4236.                  );
  4237.             if (x < 0)
  4238.               return(-1);
  4239.             /* ftscreen(SCR_ST,ST_MSG,0L,s); */
  4240.         }
  4241.     }
  4242.  
  4243.     /* Disposition of destination file */
  4244.  
  4245.     if (srvrn) {                        /* /SERVER-RENAME: */
  4246.         char * s = srvrn;
  4247. #ifndef NOSPL
  4248.         int y;                          /* Pass it thru the evaluator */
  4249.         extern int cmd_quoting; /* for \v(filename) */
  4250.         debug(F111,"ftp putfile srvrn",s,1);
  4251.  
  4252.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4253.             y = CKMAXPATH;
  4254.             s = (char *)fullname;       /* We can recycle this buffer now */
  4255.             zzstring(srvrn,&s,&y);
  4256.             s = (char *)fullname;
  4257.         }
  4258. #endif /* NOSPL */
  4259.         debug(F111,"ftp putfile srvrn",s,2);
  4260.         if (s) if (*s) {
  4261.             int x;
  4262.             x = ftp_rename(asname,s);
  4263.             debug(F111,"ftp putfile ftp_rename",asname,x);
  4264.             tlog(F110, (x > 0) ?
  4265.                  " renamed destination file to" :
  4266.                  " failed to rename destination file to",
  4267.                  s,
  4268.                  0
  4269.                  );
  4270.             if (x < 1)
  4271.               return(-1);
  4272.         }
  4273.     }
  4274.     return(0);
  4275. }
  4276.  
  4277. static int
  4278. #ifdef CK_ANSIC
  4279. xxout(char c)
  4280. #else
  4281. xxout(c) char c;
  4282. #endif /* CK_ANSIC */
  4283. {
  4284.     return(zzout(dout,c));
  4285. }
  4286.  
  4287. static int
  4288. #ifdef CK_ANSIC
  4289. scrnout(char c)
  4290. #else
  4291. scrnout(c) char c;
  4292. #endif /* CK_ANSIC */
  4293. {
  4294.     return(putchar(c));
  4295. }
  4296.  
  4297. static int
  4298. #ifdef CK_ANSIC
  4299. pipeout(char c)
  4300. #else
  4301. pipeout(c) char c;
  4302. #endif /* CK_ANSIC */
  4303. {
  4304.     return(zmchout(c));
  4305. }
  4306.  
  4307. static int
  4308. ispathsep(c) int c; {
  4309.     switch (servertype) {
  4310.       case SYS_VMS:
  4311.       case SYS_TOPS10:
  4312.       case SYS_TOPS20:
  4313.         return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4314.       case SYS_OS2:
  4315.       case SYS_WIN32:
  4316.       case SYS_DOS:
  4317.         return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4318.       case SYS_VOS:
  4319.         return((c == '>') ? 1 : 0);
  4320.       default:
  4321.         return((c == '/') ? 1 : 0);
  4322.     }
  4323. }
  4324.  
  4325. static int
  4326. iscanceled() {
  4327. #ifdef CK_CURSES
  4328.     extern int ck_repaint();
  4329. #endif /* CK_CURSES */
  4330.     int x, rc = 0;
  4331.     char c = 0;
  4332.     if (cancelfile)
  4333.       return(1);
  4334.     x = conchk();                       /* Any chars waiting at console? */
  4335.     if (x-- > 0) {                      /* Yes...  */
  4336.         c = coninc(5);                  /* Get one */
  4337.         switch (c) {
  4338.           case 032:                     /* Ctrl-X or X */
  4339.           case 'z':
  4340.           case 'Z': cancelgroup++;      /* fall thru on purpose */
  4341.           case 030:                     /* Ctrl-Z or Z */
  4342.           case 'x':
  4343.           case 'X': cancelfile++; rc++; break;
  4344. #ifdef CK_CURSES
  4345.           case 'L':
  4346.           case 'l':
  4347.           case 014:                     /* Ctrl-L or L or Ctrl-W */
  4348.           case 027:
  4349.             ck_repaint();               /* Refresh screen */
  4350. #endif /* CK_CURSES */
  4351.         }
  4352.     }
  4353.     while (x-- > 0)                     /* Soak up any rest */
  4354.       c = coninc(1);
  4355.     return(rc);
  4356. }
  4357.  
  4358. /* zzsend - used by buffered output macros. */
  4359.  
  4360. static int
  4361. #ifdef CK_ANSIC
  4362. zzsend(int fd, CHAR c)
  4363. #else
  4364. zzsend(fd,c) int fd; CHAR c;
  4365. #endif /* CK_ANSIC */
  4366. {
  4367.     int rc;
  4368.  
  4369.     debug(F101,"zzsend ucbufsiz","",ucbufsiz);
  4370.     debug(F101,"zzsend nout","",nout);
  4371.  
  4372.     if (iscanceled())                   /* Check for cancellation */
  4373.       return(-9);
  4374.     rc = (ftp_dpl == FPL_CLR) ?
  4375.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4376.         secure_putbuf(fd, ucbuf, nout);
  4377.     ucbuf[nout] = NUL;
  4378.     nout = 0;
  4379.     ucbuf[nout++] = c;
  4380.     spackets++;
  4381.     pktnum++;
  4382.     if (rc > -1 && fdispla != XYFD_B) {
  4383.         spktl = nout;
  4384.         ftscreen(SCR_PT,'D',spackets,NULL);
  4385.     }
  4386.     return(rc);
  4387. }
  4388.  
  4389. /* c m d l i n p u t  --  Command-line PUT */
  4390.  
  4391. int
  4392. cmdlinput(stay) int stay; {
  4393.     int x, rc = 0, done = 0, good = 0, status = 0;
  4394.     ULONG t0, t1;                       /* Times for stats */
  4395. #ifdef GFTIMER
  4396.     CKFLOAT sec;
  4397. #else
  4398.     int sec = 0;
  4399. #endif /* GFTIMER */
  4400.  
  4401.     if (quiet) {                        /* -q really means quiet */
  4402.         displa = 0;
  4403.         fdispla = 0;
  4404.     } else {
  4405.         displa = 1;
  4406.         fdispla = XYFD_B;
  4407.     }
  4408.     testing = 0;
  4409.     out2screen = 0;
  4410.     dpyactive = 0;
  4411.     what = W_FTP|W_SEND;
  4412.  
  4413. #ifndef NOSPL
  4414.     cmd_quoting = 0;
  4415. #endif /* NOSPL */
  4416.     sndsrc = nfils;
  4417.  
  4418.     t0 = gmstimer();                    /* Record starting time */
  4419.  
  4420.     while (!done && !cancelgroup) {     /* Loop for all files */
  4421.  
  4422.         cancelfile = 0;
  4423.         x = gnfile();                   /* Get next file from list(s) */
  4424.         if (x == 0)                     /* (see gnfile() comments...) */
  4425.           x = gnferror;
  4426.  
  4427.         switch (x) {
  4428.           case 1:                       /* File to send */
  4429.             rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
  4430.                          filnam,        /* Local file to send */
  4431.                          filnam,        /* Remote name for file */
  4432.                          forcetype,     /* Text/binary mode forced */
  4433.                          0,             /* Not moving */
  4434.                          NULL,          /* No move-to */
  4435.                          NULL,          /* No rename-to */
  4436.                          NULL,          /* No server-rename */
  4437.                          ftp_cnv,       /* Filename conversion */
  4438.                          0,             /* Unique-server-names */
  4439.                          -1,            /* All file types */
  4440.                          0,             /* No permissions */
  4441.                          -1,            /* No character sets */
  4442.                          -1,            /* No character sets */
  4443.                          0              /* No update or restart */
  4444.                          );
  4445.             if (rc > -1) {
  4446.                 good++;
  4447.                 status = 1;
  4448.             }
  4449.             if (cancelfile) {
  4450.                 continue;               /* Or break? */
  4451.             }
  4452.             if (rc < 0) {
  4453.                 ftp_fai++;
  4454.             }
  4455.             continue;                   /* Or break? */
  4456.  
  4457.           case 0:                       /* No more files, done */
  4458.             done++;
  4459.             continue;
  4460.  
  4461.           case -2:
  4462.           case -1:
  4463.             printf("?%s: file not found - \"%s\"\n",
  4464.                    puterror ? "Fatal" : "Warning",
  4465.                    filnam
  4466.                    );
  4467.             continue;                   /* or break? */
  4468.           case -3:
  4469.             printf("?Warning access denied - \"%s\"\n", filnam);
  4470.             continue;                   /* or break? */
  4471.           case -5:
  4472.             printf("?Too many files match\n");
  4473.             done++;
  4474.             break;
  4475.           case -6:
  4476.             if (good < 1)
  4477.               printf("?No files selected\n");
  4478.             done++;
  4479.             break;
  4480.           default:
  4481.             printf("?getnextfile() - unknown failure\n");
  4482.             done++;
  4483.         }
  4484.     }
  4485.     if (status > 0) {
  4486.         if (cancelgroup)
  4487.           status = 0;
  4488.         else if (cancelfile && good < 1)
  4489.           status = 0;
  4490.     }
  4491.     success = status;
  4492.     x = success;
  4493.     if (x > -1) {
  4494.         lastxfer = W_FTP|W_SEND;
  4495.         xferstat = success;
  4496.     }
  4497.     t1 = gmstimer();                    /* End time */
  4498. #ifdef GFTIMER
  4499.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4500.     if (!sec) sec = 0.001;
  4501.     fptsecs = sec;
  4502. #else
  4503.     sec = (t1 - t0) / 1000;
  4504.     if (!sec) sec = 1;
  4505. #endif /* GFTIMER */
  4506.     tfcps = (long) (tfc / sec);
  4507.     tsecs = (int)sec;
  4508.     lastxfer = W_FTP|W_SEND;
  4509.     xferstat = success;
  4510.     if (dpyactive)
  4511.       ftscreen(SCR_TC,0,0L,"");
  4512.  
  4513.     if (!stay)
  4514.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4515.     return(success);
  4516. }
  4517.  
  4518.  
  4519. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4520.  
  4521. int
  4522. #ifdef CK_ANSIC
  4523. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4524. #else
  4525. doftpput(cx,who) int cx, who;
  4526. #endif /* CK_ANSIC */
  4527. {
  4528.     struct FDB sf, fl, sw, cm;
  4529.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4530.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4531.     char * s, * s2;
  4532.  
  4533.     int x_csl, x_csr = -1;              /* Local and remote charsets */
  4534.     int x_xla = 0;
  4535.     int x_recurse = 0;
  4536.     char c, * p;                        /* Workers */
  4537. #ifdef PUTARRAY
  4538.     int range[2];                       /* Array range */
  4539.     char ** ap = NULL;                  /* Array pointer */
  4540.     int arrayx = -1;                    /* Array index */
  4541. #endif /* PUTARRAY */
  4542.     ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
  4543. #ifdef GFTIMER
  4544.     CKFLOAT sec;
  4545. #else
  4546.     int sec = 0;
  4547. #endif /* GFTIMER */
  4548.  
  4549.     struct stringint {                  /* Temporary array for switch values */
  4550.         char * sval;
  4551.         int ival;
  4552.     } pv[SND_MAX+1];
  4553.  
  4554.     success = 0;                        /* Assume failure */
  4555.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  4556.     out2screen = 0;                     /* Not outputting file to screen */
  4557.     putflags = 0;                       /* PUT options */
  4558.     x_cnv = ftp_cnv;                    /* Filename conversion */
  4559.     x_usn = ftp_usn;                    /* Unique server names */
  4560.     x_prm = ftp_prm;                    /* Permissions */
  4561.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  4562.       x_prm = alike;
  4563.  
  4564. #ifndef NOCSETS
  4565.     x_csr = ftp_csr;                    /* Inherit global server charset */
  4566.     x_csl = ftp_csl;
  4567.     if (x_csl < 0)
  4568.       x_csl = fcharset;
  4569.     x_xla = ftp_xla;
  4570. #endif /* NOCSETS */
  4571.  
  4572.     makestr(&filefile,NULL);            /* No filename list file yet. */
  4573.     makestr(&srv_renam,NULL);        /* Clear /SERVER-RENAME: */
  4574.     makestr(&snd_rename,NULL);        /*  PUT /RENAME */
  4575.     makestr(&snd_move,NULL);        /*  PUT /MOVE */
  4576.     putpath[0] = NUL;                   /* Initialize for syncdir(). */
  4577.     puterror = ftp_err;                 /* Inherit global error action. */
  4578.     what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
  4579.     asnambuf[0] = NUL;                  /* Clear as-name buffer */
  4580.  
  4581.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  4582.         ftp_typ = g_ftp_typ;
  4583.         /* g_ftp_typ = -1; */
  4584.     }
  4585.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4586.         pv[i].sval = NULL;              /* to null pointers */
  4587.         pv[i].ival = -1;                /* and -1 int values */
  4588.     }
  4589.     if (who == 0) {                     /* Called with unprefixed command */
  4590.         switch (cx) {
  4591.           case XXRSEN:  pv[SND_RES].ival = 1; break;
  4592.           case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4593.           case XXMOVE:  pv[SND_DEL].ival = 1; break;
  4594.           case XXMMOVE: pv[SND_DEL].ival = 1; /* fall thru */
  4595.           case XXMSE:   mput++; break;
  4596.         }
  4597.     } else {
  4598.         if (cx == FTP_MPU)
  4599.           mput++;
  4600.     }
  4601.     cmfdbi(&sw,                         /* First FDB - command switches */
  4602.            _CMKEY,                      /* fcode */
  4603.            "Filename, or switch",       /* hlpmsg */
  4604.            "",                          /* default */
  4605.            "",                          /* addtl string data */
  4606.            nputswi,                     /* addtl numeric data 1: tbl size */
  4607.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4608.            xxstring,                    /* Processing function */
  4609.            putswi,                      /* Keyword table */
  4610.            &sf                          /* Pointer to next FDB */
  4611.            );
  4612.     cmfdbi(&fl,                         /* 3rd FDB - local filespec */
  4613.            _CMFLD,                      /* fcode */
  4614.            "",                          /* hlpmsg */
  4615.            "",                          /* default */
  4616.            "",                          /* addtl string data */
  4617.            0,                           /* addtl numeric data 1 */
  4618.            0,                           /* addtl numeric data 2 */
  4619.            xxstring,
  4620.            NULL,
  4621.            &cm
  4622.            );
  4623.     cmfdbi(&cm,                         /* 4th FDB - Confirmation */
  4624.            _CMCFM,                      /* fcode */
  4625.            "",                          /* hlpmsg */
  4626.            "",                          /* default */
  4627.            "",                          /* addtl string data */
  4628.            0,                           /* addtl numeric data 1 */
  4629.            0,                           /* addtl numeric data 2 */
  4630.            NULL,
  4631.            NULL,
  4632.            NULL
  4633.            );
  4634.  
  4635.   again:
  4636.     cmfdbi(&sf,                         /* 2nd FDB - file to send */
  4637.            _CMIFI,                      /* fcode */
  4638.            "",                          /* hlpmsg */
  4639.            "",                          /* default */
  4640.            "",                          /* addtl string data */
  4641.            /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  4642.            nolinks | x_recurse,         /* addtl numeric data 1 */
  4643.            0,                           /* dirflg 0 means "not dirs only" */
  4644.            xxstring,
  4645.            NULL,
  4646. #ifdef COMMENT
  4647.            mput ? &cm : &fl
  4648. #else
  4649.        &fl
  4650. #endif /* COMMENT */
  4651.            );
  4652.  
  4653.     while (1) {                         /* Parse zero or more switches */
  4654.         x = cmfdb(&sw);                 /* Parse something */
  4655.         debug(F101,"ftp put cmfdb A","",x);
  4656.         debug(F101,"ftp put fcode A","",cmresult.fcode);
  4657.         if (x < 0)                      /* Error */
  4658.           goto xputx;                   /* or reparse needed */
  4659.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  4660.           break;
  4661.         c = cmgbrk();                   /* Get break character */
  4662.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  4663.         if (getval && !(cmresult.kflags & CM_ARG)) {
  4664.             printf("?This switch does not take arguments\n");
  4665.             x = -9;
  4666.             goto xputx;
  4667.         }
  4668.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4669.             printf("?This switch requires an argument\n");
  4670.             x = -9;
  4671.             goto xputx;
  4672.         }
  4673.         n = cmresult.nresult;           /* Numeric result = switch value */
  4674.         debug(F101,"ftp put switch","",n);
  4675.  
  4676.         switch (n) {                    /* Process the switch */
  4677.           case SND_AFT:                 /* Send /AFTER:date-time */
  4678.           case SND_BEF:                 /* Send /BEFORE:date-time */
  4679.           case SND_NAF:                 /* Send /NOT-AFTER:date-time */
  4680.           case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
  4681.             if (!getval) break;
  4682.             if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  4683.                 if (x == -3) {
  4684.                     printf("?Date-time required\n");
  4685.                     x = -9;
  4686.                 }
  4687.                 goto xputx;
  4688.             }
  4689.             pv[n].ival = 1;
  4690.             makestr(&(pv[n].sval),s);
  4691.             break;
  4692.  
  4693.           case SND_ASN:                 /* /AS-NAME: */
  4694.             debug(F101,"ftp put /as-name getval","",getval);
  4695.             if (!getval) break;
  4696.             if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  4697.                 if (x == -3) {
  4698.                     printf("?name required\n");
  4699.                     x = -9;
  4700.                 }
  4701.                 goto xputx;
  4702.             }
  4703.             makestr(&(pv[n].sval),brstrip(s));
  4704.             debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  4705.             if (pv[n].sval) pv[n].ival = 1;
  4706.             break;
  4707.  
  4708. #ifdef PUTARRAY
  4709.           case SND_ARR:                 /* /ARRAY */
  4710.             if (!getval) break;
  4711.             ap = NULL;
  4712.             if ((x = cmfld("Array name (a single letter will do)",
  4713.                            "",
  4714.                            &s,
  4715.                            NULL
  4716.                            )) < 0) {
  4717.                 if (x == -3)
  4718.           break;
  4719.         else
  4720.           return(x);
  4721.             }
  4722.             if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  4723.                 printf("?Bad array: %s\n",s);
  4724.                 return(-9);
  4725.             }
  4726.             if (!(ap = a_ptr[x])) {
  4727.                 printf("?No such array: %s\n",s);
  4728.                 return(-9);
  4729.             }
  4730.             pv[n].ival = 1;
  4731.             pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
  4732.             pv[SND_RES].ival = 0;
  4733.             pv[SND_FIL].ival = 0;
  4734.             arrayx = x;
  4735.             break;
  4736. #endif /* PUTARRAY */
  4737.  
  4738.           case SND_BIN:                 /* /BINARY */
  4739.           case SND_TXT:                 /* /TEXT or /ASCII */
  4740.           case SND_TEN:                 /* /TENEX */
  4741.             pv[SND_BIN].ival = 0;
  4742.             pv[SND_TXT].ival = 0;
  4743.             pv[SND_TEN].ival = 0;
  4744.             pv[n].ival = 1;
  4745.             break;
  4746.  
  4747. #ifdef PUTPIPE
  4748.           case SND_CMD:                 /* These take no args */
  4749.             if (nopush) {
  4750.                 printf("?Sorry, system command access is disabled\n");
  4751.                 x = -9;
  4752.                 goto xputx;
  4753.             }
  4754. #ifdef PIPESEND
  4755.             else if (sndfilter) {
  4756.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  4757.                 x = -9;
  4758.                 goto xputx;
  4759.             }
  4760. #endif /* PIPESEND */
  4761.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  4762.             pv[n].ival = 1;             /* Just set the flag */
  4763.             pv[SND_ARR].ival = 0;
  4764.             break;
  4765. #endif /* PUTPIPE */
  4766.  
  4767. #ifdef CKSYMLINK
  4768.           case SND_LNK:
  4769.             nolinks = 0;
  4770.             goto again;            /* Because CMIFI params changed... */
  4771.           case SND_NLK:
  4772.             nolinks = 2;
  4773.             goto again;
  4774. #endif /* CKSYMLINK */
  4775.  
  4776. #ifdef FTP_RESTART
  4777.           case SND_RES:                 /* /RECOVER (resend) */
  4778.             pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
  4779. #endif /* FTP_RESTART */
  4780.  
  4781.           case SND_NOB:
  4782.           case SND_DEL:                 /* /DELETE */
  4783.           case SND_SHH:                 /* /QUIET */
  4784.           case SND_UPD:                 /* /UPDATE */
  4785.           case SND_SIM:                 /* /UPDATE */
  4786.           case SND_USN:                 /* /UNIQUE */
  4787.             pv[n].ival = 1;             /* Just set the flag */
  4788.             break;
  4789.  
  4790.           case SND_REC:                 /* /RECURSIVE */
  4791.             recursive = 2;              /* Must be set before cmifi() */
  4792.             x_recurse = 1;
  4793.             goto again;            /* Because CMIFI params changed... */
  4794.             break;
  4795.  
  4796. #ifdef UNIXOROSK
  4797.           case SND_DOT:                 /* /DOTFILES */
  4798.             matchdot = 1;
  4799.             break;
  4800.           case SND_NOD:                 /* /NODOTFILES */
  4801.             matchdot = 0;
  4802.             break;
  4803. #endif /* UNIXOROSK */
  4804.  
  4805.           case SND_ERR:                 /* /ERROR-ACTION */
  4806.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  4807.               goto xputx;
  4808.             pv[n].ival = x;
  4809.             break;
  4810.  
  4811.           case SND_EXC:                 /* Excludes */
  4812.             if (!getval) break;
  4813.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4814.                 if (x == -3) {
  4815.                     printf("?Pattern required\n");
  4816.                     x = -9;
  4817.                 }
  4818.                 goto xputx;
  4819.             }
  4820.             if (s) if (!*s) s = NULL;
  4821.             makestr(&(pv[n].sval),s);
  4822.             if (pv[n].sval)
  4823.               pv[n].ival = 1;
  4824.             break;
  4825.  
  4826.           case SND_PRM:                 /* /PERMISSIONS */
  4827.             if (!getval)
  4828.               x = 1;
  4829.             else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  4830.               goto xputx;
  4831.             pv[SND_PRM].ival = x;
  4832.             break;
  4833.  
  4834. #ifdef PIPESEND
  4835.           case SND_FLT:                 /* /FILTER */
  4836.             debug(F101,"ftp put /filter getval","",getval);
  4837.             if (!getval) break;
  4838.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  4839.                 if (x == -3)
  4840.                   s = "";
  4841.                 else
  4842.                   goto xputx;
  4843.             }
  4844.             if (*s) s = brstrip(s);
  4845.             y = strlen(s);
  4846.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  4847.                 if (s[x] != '\\') continue;
  4848.                 if (s[x+1] == 'v') break;
  4849.             }
  4850.             if (x == y) {
  4851.                 printf(
  4852.                 "?Filter must contain a replacement variable for filename.\n"
  4853.                        );
  4854.                 x = -9;
  4855.                 goto xputx;
  4856.             }
  4857.             if (s) if (!*s) s = NULL;
  4858.             makestr(&(pv[n].sval),s);
  4859.             if (pv[n].sval)
  4860.               pv[n].ival = 1;
  4861.             break;
  4862. #endif /* PIPESEND */
  4863.  
  4864.           case SND_NAM:                 /* /FILENAMES */
  4865.             if (!getval) break;
  4866.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  4867.               goto xputx;
  4868.             debug(F101,"ftp put /filenames","",x);
  4869.             pv[n].ival = x;
  4870.             break;
  4871.  
  4872.           case SND_SMA:                 /* Smaller / larger than */
  4873.           case SND_LAR:
  4874.             if (!getval) break;
  4875.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  4876.               goto xputx;
  4877.             pv[n].ival = y;
  4878.             break;
  4879.  
  4880.           case SND_FIL:                 /* Name of file containing filenames */
  4881.             if (!getval) break;
  4882.             if ((x = cmifi("Name of file containing list of filenames",
  4883.                                "",&s,&y,xxstring)) < 0) {
  4884.                 if (x == -3) {
  4885.                     printf("?Filename required\n");
  4886.                     x = -9;
  4887.                 }
  4888.                 goto xputx;
  4889.             } else if (y && iswild(s)) {
  4890.                 printf("?Wildcards not allowed\n");
  4891.                 x = -9;
  4892.                 goto xputx;
  4893.             }
  4894.             if (s) if (!*s) s = NULL;
  4895.             makestr(&(pv[n].sval),s);
  4896.             if (pv[n].sval) {
  4897.                 pv[n].ival = 1;
  4898.                 pv[SND_ARR].ival = 0;
  4899.             } else {
  4900.                 pv[n].ival = 0;
  4901.             }
  4902.             mput = 0;
  4903.             break;
  4904.  
  4905.           case SND_MOV:                 /* MOVE after */
  4906.           case SND_REN:                 /* RENAME after */
  4907.           case SND_SRN: {               /* SERVER-RENAME after */
  4908.               char * m = "";
  4909.               switch (n) {
  4910.                 case SND_MOV:
  4911.                   m = "device and/or directory for source file after sending";
  4912.                   break;
  4913.                 case SND_REN:
  4914.                   m = "new name for source file after sending";
  4915.                   break;
  4916.                 case SND_SRN:
  4917.                   m = "new name for destination file after sending";
  4918.                   break;
  4919.               }
  4920.               if (!getval) break;
  4921.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  4922.                   if (x == -3) {
  4923.                       printf("%s\n", n == SND_MOV ?
  4924.                              "?Destination required" :
  4925.                              "?New name required"
  4926.                              );
  4927.                       x = -9;
  4928.                   }
  4929.                   goto xputx;
  4930.               }
  4931.               if (s) if (!*s) s = NULL;
  4932.               makestr(&(pv[n].sval),s ? brstrip(s) : NULL);
  4933.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  4934.               break;
  4935.           }
  4936.           case SND_STA:                 /* Starting position (= PSEND) */
  4937.             if (!getval) break;
  4938.             if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  4939.               goto xputx;
  4940.             pv[n].ival = y;
  4941.             break;
  4942.  
  4943.           case SND_TYP:                 /* /TYPE */
  4944.             if (!getval) break;
  4945.             if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  4946.               goto xputx;
  4947.             pv[n].ival = (x == 2) ? -1 : x;
  4948.             break;
  4949.  
  4950. #ifndef NOCSETS
  4951.           case SND_CSL:                 /* Local character set */
  4952.           case SND_CSR:                 /* Remote (server) charset */
  4953.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0) {
  4954.         return((x == -3) ? -2 : x);
  4955.             }
  4956.         if (n == SND_CSL)
  4957.               x_csl = x;
  4958.             else
  4959.               x_csr = x;
  4960.             x_xla = 1;                  /* Overrides global OFF setting */
  4961.             break;
  4962.  
  4963.           case SND_XPA:                 /* Transparent */
  4964.             x_xla = 0;
  4965.             x_csr = -1;
  4966.             x_csl = -1;
  4967.             break;
  4968. #endif /* NOCSETS */
  4969.         }
  4970.     }
  4971. #ifdef PIPESEND
  4972.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  4973.         if (sndfilter || pv[SND_FLT].ival > 0) {
  4974.             printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  4975.             x = -9;
  4976.             goto xputx;
  4977.         }
  4978.     if (sfttab[0] > 0 && sfttab[SFT_REST] == 0)
  4979.       printf("WARNING: Server says it doesn't support REST.\n");
  4980.     }
  4981. #endif /* PIPESEND */
  4982.  
  4983.     cmarg = "";
  4984.     cmarg2 = asnambuf;
  4985.     line[0] = NUL;
  4986.     s = line;
  4987.     wild = 0;
  4988.  
  4989.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  4990.       case _CMIFI:                      /* Input filename */
  4991.         if (pv[SND_FIL].ival > 0) {
  4992.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4993.             x = -9;
  4994.             goto xputx;
  4995.         }
  4996.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  4997.         if (pv[SND_ARR].ival > 0)
  4998.           ckstrncpy(asnambuf,line,CKMAXPATH);
  4999.         else
  5000.           wild = cmresult.nresult;      /* Wild flag */
  5001.         debug(F111,"ftp put wild",line,wild);
  5002.         if (!wild && !recursive && !mput)
  5003.           nolinks = 0;
  5004.         break;
  5005.       case _CMFLD:                      /* Field */
  5006.         /* Only allowed with /COMMAND and /ARRAY */
  5007.         if (pv[SND_FIL].ival > 0) {
  5008.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  5009.             x = -9;
  5010.             goto xputx;
  5011.         }
  5012.     /* For MPUT it's OK to have filespecs that don't match any files */
  5013.     if (mput)
  5014.       break;
  5015.         if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  5016. #ifdef CKROOT
  5017.             if (ckrooterr)
  5018.               printf("?Off limits: %s\n",cmresult.sresult);
  5019.             else
  5020. #endif /* CKROOT */
  5021.               printf("?%s - \"%s\"\n",
  5022.                    iswild(cmresult.sresult) ?
  5023.                    "No files match" : "File not found",
  5024.                    cmresult.sresult
  5025.                    );
  5026.             x = -9;
  5027.             goto xputx;
  5028.         }
  5029.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  5030.     debug(F110,"XXX CMFLD line",line,0);
  5031.         if (pv[SND_ARR].ival > 0)
  5032.           ckstrncpy(asnambuf,line,CKMAXPATH);
  5033.         break;
  5034.       case _CMCFM:                      /* Confirmation */
  5035.         confirmed = 1;
  5036.         break;
  5037.       default:
  5038.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  5039.         x = -9;
  5040.         goto xputx;
  5041.     }
  5042.     debug(F110,"ftp put string",s,0);
  5043.     debug(F101,"ftp put confirmed","",confirmed);
  5044.  
  5045.     /* Save and change protocol and transfer mode */
  5046.     /* Global values are restored in main parse loop */
  5047.  
  5048.     g_displa = fdispla;
  5049.     if (ftp_dis > -1)
  5050.       fdispla = ftp_dis;
  5051.     g_skipbup = skipbup;
  5052.  
  5053.     if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
  5054.         g_skipbup = skipbup;
  5055.         skipbup = 1;
  5056.     }
  5057.     if (pv[SND_TYP].ival > -1) {        /* /TYPE */
  5058.         xfiletype = pv[SND_TYP].ival;
  5059.         if (xfiletype == 2)
  5060.           xfiletype = -1;
  5061.     }
  5062.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  5063.         forcetype = 1;                  /* So skip file scan */
  5064.         ftp_typ = FTT_BIN;              /* Set binary */
  5065.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  5066.         forcetype = 1;
  5067.         ftp_typ = FTT_ASC;
  5068.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  5069.         forcetype = 1;
  5070.         ftp_typ = FTT_TEN;
  5071.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  5072.         forcetype = 1;
  5073.         ftp_typ = binary;
  5074.         g_ftp_typ = binary;
  5075.     }
  5076.  
  5077. #ifdef PIPESEND
  5078.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  5079.         debug(F110,"PUT /COMMAND before stripping",s,0);
  5080.         s = brstrip(s);
  5081.         debug(F110,"PUT /COMMAND after stripping",s,0);
  5082.         if (!*s) {
  5083.             printf("?Sorry, a command to send from is required\n");
  5084.             x = -9;
  5085.             goto xputx;
  5086.         }
  5087.         cmarg = s;
  5088.     }
  5089. #endif /* PIPESEND */
  5090.  
  5091. /* Set up /MOVE and /RENAME */
  5092.  
  5093.     if (pv[SND_DEL].ival > 0 &&
  5094.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  5095.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  5096.         x = -9;
  5097.         goto xputx;
  5098.     }
  5099. #ifdef CK_TMPDIR
  5100.     if (pv[SND_MOV].ival > 0) {
  5101.         int len;
  5102.         char * p = pv[SND_MOV].sval;
  5103.         len = strlen(p);
  5104.         if (!isdir(p)) {                /* Check directory */
  5105. #ifdef CK_MKDIR
  5106.             char * s = NULL;
  5107.             s = (char *)malloc(len + 4);
  5108.             if (s) {
  5109.                 strcpy(s,p);            /* safe */
  5110. #ifdef datageneral
  5111.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  5112. #else
  5113.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  5114. #endif /* datageneral */
  5115.                 s[len++] = 'X';
  5116.                 s[len] = NUL;
  5117. #ifdef NOMKDIR
  5118.                 x = -1;
  5119. #else
  5120.                 x = zmkdir(s);
  5121. #endif /* NOMKDIR */
  5122.                 free(s);
  5123.                 if (x < 0) {
  5124.                     printf("?Can't create \"%s\"\n",p);
  5125.                     x = -9;
  5126.                     goto xputx;
  5127.                 }
  5128.             }
  5129. #else
  5130.             printf("?Directory \"%s\" not found\n",p);
  5131.             x = -9;
  5132.             goto xputx;
  5133. #endif /* CK_MKDIR */
  5134.         }
  5135.         makestr(&snd_move,p);
  5136.     }
  5137. #endif /* CK_TMPDIR */
  5138.  
  5139.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  5140.         char * p = pv[SND_REN].sval;
  5141.         if (!p) p = "";
  5142.         if (!*p) {
  5143.             printf("?New name required for /RENAME\n");
  5144.             x = -9;
  5145.             goto xputx;
  5146.         }
  5147.         p = brstrip(p);
  5148. #ifndef NOSPL
  5149.     /* If name given is wild, rename string must contain variables */
  5150.         if (wild) {
  5151.             char * s = tmpbuf;
  5152.             x = TMPBUFSIZ;
  5153.             zzstring(p,&s,&x);
  5154.             if (!strcmp(tmpbuf,p)) {
  5155.                 printf(
  5156.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  5157.                        );
  5158.                 x = -9;
  5159.                 goto xputx;
  5160.             }
  5161.         }
  5162. #endif /* NOSPL */
  5163.         makestr(&snd_rename,p);
  5164.         debug(F110,"FTP snd_rename",snd_rename,0);
  5165.     }
  5166.     if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
  5167.         char * p = pv[SND_SRN].sval;
  5168.         if (!p) p = "";
  5169.         if (!*p) {
  5170.             printf("?New name required for /SERVER-RENAME\n");
  5171.             x = -9;
  5172.             goto xputx;
  5173.         }
  5174.         p = brstrip(p);
  5175. #ifndef NOSPL
  5176.         if (wild) {
  5177.             char * s = tmpbuf;
  5178.             x = TMPBUFSIZ;
  5179.             zzstring(p,&s,&x);
  5180.             if (!strcmp(tmpbuf,p)) {
  5181.                 printf(
  5182. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  5183.                        );
  5184.                 x = -9;
  5185.                 goto xputx;
  5186.             }
  5187.         }
  5188. #endif /* NOSPL */
  5189.         makestr(&srv_renam,p);
  5190.         debug(F110,"ftp put srv_renam",srv_renam,0);
  5191.     }
  5192.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  5193.         char * lp;
  5194.         if (mput) {                     /* MPUT or MMOVE */
  5195.             nfils = 0;                  /* We already have the first one */
  5196. #ifndef NOMSEND
  5197.         if (cmresult.fcode == _CMIFI) {
  5198.         /* First filespec is valid */
  5199.         msfiles[nfils++] = line;    /* Store pointer */
  5200.         lp = line + (int)strlen(line) + 1; /* Point past it */
  5201.         debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  5202.         } else {
  5203.         /* First filespec matches no files */
  5204.         debug(F110,"ftp put mput skipping first filespec",
  5205.               cmresult.sresult,
  5206.               0
  5207.               );
  5208.         lp = line;
  5209.         }
  5210.         /* Parse a filespec, a "field", or confirmation */
  5211.  
  5212.         cmfdbi(&sf,            /* 1st FDB - file to send */
  5213.            _CMIFI,        /* fcode */
  5214.            "",            /* hlpmsg */
  5215.            "",            /* default */
  5216.            "",            /* addtl string data */
  5217.            nolinks | x_recurse,    /* addtl numeric data 1 */
  5218.            0,            /* dirflg 0 means "not dirs only" */
  5219.            xxstring,
  5220.            NULL,
  5221.            &fl
  5222.            );
  5223.         cmfdbi(&fl,            /* 2nd FDB - local filespec */
  5224.            _CMFLD,        /* fcode */
  5225.            "",            /* hlpmsg */
  5226.            "",            /* default */
  5227.            "",            /* addtl string data */
  5228.            0,            /* addtl numeric data 1 */
  5229.            0,            /* addtl numeric data 2 */
  5230.            xxstring,
  5231.            NULL,
  5232.            &cm
  5233.            );
  5234.         cmfdbi(&cm,            /* 3rd FDB - Confirmation */
  5235.            _CMCFM,        /* fcode */
  5236.            "",
  5237.            "",
  5238.            "",
  5239.            0,
  5240.            0,
  5241.            NULL,
  5242.            NULL,
  5243.            NULL
  5244.            );
  5245.  
  5246.             while (!confirmed) {    /* Get more filenames */
  5247.         x = cmfdb(&sf);        /* Parse something */
  5248.         debug(F101,"ftp put cmfdb B","",x);
  5249.         debug(F101,"ftp put fcode B","",cmresult.fcode);
  5250.         if (x < 0)        /* Error */
  5251.           goto xputx;        /* or reparse needed */
  5252.         switch (cmresult.fcode) {
  5253.           case _CMCFM:        /* End of command */
  5254.             confirmed++;
  5255.             if (nfils < 1) {
  5256.             debug(F100,"ftp put mput no files match","",0);
  5257.             printf("?No files match MPUT list\n");
  5258.             x = -9;
  5259.             goto xputx;
  5260.             }
  5261.             break;
  5262.           case _CMFLD:        /* No match */
  5263.             debug(F110,"ftp put mput skipping",cmresult.sresult,0);
  5264.             continue;
  5265.           case _CMIFI:        /* Good match */
  5266.             s = cmresult.sresult;
  5267.             msfiles[nfils++] = lp; /* Got one, count, point to it, */
  5268.             p = lp;           /* remember pointer, */
  5269.             while ((*lp++ = *s++)) /* and copy it into buffer */
  5270.               if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  5271.               printf("?MPUT list too long\n");
  5272.               line[0] = NUL;
  5273.               x = -9;
  5274.               goto xputx;
  5275.               }
  5276.             debug(F111,"ftp put mput adding",msfiles[nfils-1],nfils-1);
  5277.             if (nfils == 1)    /* Take care of \v(filespec) */
  5278.               fspec[0] = NUL;
  5279. #ifdef ZFNQFP
  5280.             zfnqfp(p,TMPBUFSIZ,tmpbuf);
  5281.             p = tmpbuf;
  5282. #endif /* ZFNQFP */
  5283.             if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  5284.             strcat(fspec,p);    /* safe */
  5285.             strcat(fspec," ");  /* safe */
  5286.             } else {
  5287. #ifdef COMMENT
  5288.             printf("WARNING - \\v(filespec) buffer overflow\n");
  5289. #else
  5290.             debug(F101,"doxput filespec buffer overflow","",0);
  5291. #endif /* COMMENT */
  5292.             }
  5293.         }
  5294.         }
  5295. #endif /* NOMSEND */
  5296.         } else {                        /* Regular PUT */
  5297.             nfils = -1;
  5298.             if ((x = cmtxt(wild ?
  5299. "\nOptional as-name template containing replacement variables \
  5300. like \\v(filename)" :
  5301.                            "Optional name to send it with",
  5302.                            "",&p,NULL)) < 0)
  5303.               goto xputx;
  5304.  
  5305.             if (p) if (!*p) p = NULL;
  5306.             p = brstrip(p);
  5307.  
  5308.             if (p && *p) {
  5309.                 makestr(&(pv[SND_ASN].sval),p);
  5310.                 if (pv[SND_ASN].sval)
  5311.                   pv[SND_ASN].ival = 1;
  5312.                 debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  5313.             }
  5314.         }
  5315.     }
  5316.     /* Set cmarg2 from as-name, however we got it. */
  5317.  
  5318.     CHECKCONN();
  5319.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  5320.         char * p;
  5321.         p = brstrip(pv[SND_ASN].sval);
  5322.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  5323.     }
  5324.     debug(F110,"ftp put asnambuf",asnambuf,0);
  5325.  
  5326.     if (pv[SND_FIL].ival > 0) {
  5327.         if (confirmed) {
  5328.             if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  5329.                 debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  5330.                 printf("?Failure to open %s\n",pv[SND_FIL].sval);
  5331.                 x = -9;
  5332.                 goto xputx;
  5333.             }
  5334.             makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  5335.             debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  5336.             wild = 1;
  5337.         }
  5338.     }
  5339.     if (confirmed && !line[0] && !filefile) {
  5340. #ifndef NOMSEND
  5341.         if (filehead) {                 /* OK if we have a SEND-LIST */
  5342.             nfils = filesinlist;
  5343.             sndsrc = nfils;             /* Like MSEND */
  5344.             addlist = 1;                /* But using a different list... */
  5345.             filenext = filehead;
  5346.             goto doput;
  5347.         }
  5348. #endif /* NOMSEND */
  5349.         printf("?Filename required but not given\n");
  5350.         x = -9;
  5351.         goto xputx;
  5352.     }
  5353. #ifndef NOMSEND
  5354.     addlist = 0;                        /* Don't use SEND-LIST. */
  5355. #endif /* NOMSEND */
  5356.  
  5357.     if (mput) {                         /* MPUT (rather than PUT) */
  5358. #ifndef NOMSEND
  5359.         cmlist = msfiles;               /* List of filespecs */
  5360.         sndsrc = nfils;                 /* rather filespec and as-name */
  5361. #endif /* NOMSEND */
  5362.         pipesend = 0;
  5363.     } else if (filefile) {              /* File contains list of filenames */
  5364.         s = "";
  5365.         cmarg = "";
  5366.         line[0] = NUL;
  5367.         nfils = 1;
  5368.         sndsrc = 1;
  5369.  
  5370.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5371.  
  5372.         /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5373.         nfils = sndsrc = -1;
  5374.         if (!wild) {
  5375.             y = zchki(s);
  5376.             if (y < 0) {
  5377.                 printf("?Read access denied - \"%s\"\n", s);
  5378.                 x = -9;
  5379.                 goto xputx;
  5380.             }
  5381.         }
  5382.         if (s != line)                  /* We might already have done this. */
  5383.           ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
  5384. #ifdef DEBUG
  5385.         else
  5386.           debug(F110,"doxput line=s",line,0);
  5387. #endif /* DEBUG */
  5388.         cmarg = line;                   /* File to send */
  5389.     }
  5390. #ifndef NOMSEND
  5391.     zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
  5392. #endif /* NOMSEND */
  5393.  
  5394.     if (!mput) {                        /* For all but MPUT... */
  5395. #ifdef PIPESEND
  5396.         if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
  5397.           pipesend = 1;
  5398.         debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5399.         if (pipesend && filefile) {
  5400.             printf("?Invalid switch combination\n");
  5401.             x = -9;
  5402.             goto xputx;
  5403.         }
  5404. #endif /* PIPESEND */
  5405.  
  5406. #ifndef NOSPL
  5407.     /* If as-name given and filespec is wild, as-name must contain variables */
  5408.         if ((wild || mput) && asnambuf[0]) {
  5409.             char * s = tmpbuf;
  5410.             x = TMPBUFSIZ;
  5411.             zzstring(asnambuf,&s,&x);
  5412.             if (!strcmp(tmpbuf,asnambuf)) {
  5413.                 printf(
  5414.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5415.                        );
  5416.                 x = -9;
  5417.                 goto xputx;
  5418.             }
  5419.         }
  5420. #endif /* NOSPL */
  5421.     }
  5422.  
  5423.   doput:
  5424.  
  5425.     if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
  5426.         fdispla = 0;
  5427.         debug(F101,"ftp put display","",fdispla);
  5428.     } else {
  5429.         displa = 1;
  5430.         if (ftp_deb)
  5431.       fdispla = XYFD_B;
  5432.     }
  5433.  
  5434. #ifdef PUTARRAY                         /* SEND /ARRAY... */
  5435.     if (pv[SND_ARR].ival > 0) {
  5436.         if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5437.         if (range[0] == -1)             /* If low end of range not specified */
  5438.           range[0] = 1;                 /* default to 1 */
  5439.         if (range[1] == -1)             /* If high not specified */
  5440.           range[1] = a_dim[arrayx];     /* default to size of array */
  5441.         if ((range[0] < 0) ||           /* Check range */
  5442.             (range[0] > a_dim[arrayx]) ||
  5443.             (range[1] < range[0]) ||
  5444.             (range[1] > a_dim[arrayx])) {
  5445.             printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5446.             x = -9;
  5447.             goto xputx;
  5448.         }
  5449.         sndarray = ap;                  /* Array pointer */
  5450.         sndxin = arrayx;                /* Array index */
  5451.         sndxlo = range[0];              /* Array range */
  5452.         sndxhi = range[1];
  5453.         sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5454.         if (!asnambuf[0])
  5455.           ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5456.         cmarg = "";
  5457.     }
  5458. #endif /* PUTARRAY */
  5459.  
  5460.     moving = 0;
  5461.  
  5462.     if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
  5463.         if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
  5464.           moving = 1;
  5465.         if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
  5466.           ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5467.         if (pv[SND_BEF].ival > 0)
  5468.           ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5469.         if (pv[SND_NAF].ival > 0)
  5470.           ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5471.         if (pv[SND_NBE].ival > 0)
  5472.           ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5473.         if (pv[SND_EXC].ival > 0)
  5474.           makelist(pv[SND_EXC].sval,sndexcept,NSNDEXCEPT);
  5475.         if (pv[SND_SMA].ival > -1)
  5476.           sndsmaller = pv[SND_SMA].ival;
  5477.         if (pv[SND_LAR].ival > -1)
  5478.           sndlarger = pv[SND_LAR].ival;
  5479.         if (pv[SND_NAM].ival > -1)
  5480.           x_cnv = pv[SND_NAM].ival;
  5481.         if (pv[SND_USN].ival > -1)
  5482.           x_usn = pv[SND_USN].ival;
  5483.         if (pv[SND_ERR].ival > -1)
  5484.           puterror = pv[SND_ERR].ival;
  5485.  
  5486. #ifdef DOUPDATE
  5487.         if (pv[SND_UPD].ival > 0) {
  5488.             if (x_usn) {
  5489.                 printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5490.                 x = -9;
  5491.                 goto xputx;
  5492.             }
  5493.             putflags |= PUT_UPD;
  5494.         ftp_dates |= 2;
  5495.         }
  5496. #ifdef COMMENT
  5497.     /* This works but it's useless, maybe dangerous */
  5498.         if (pv[SND_DIF].ival > 0) {
  5499.             if (x_usn) {
  5500.                 printf("?Conflicting switches: /DATES-DIFFER /UNIQUE\n");
  5501.                 x = -9;
  5502.                 goto xputx;
  5503.             }
  5504.             putflags |= PUT_DIF;
  5505.         ftp_dates |= 2;
  5506.         }
  5507. #endif /* COMMENT */
  5508. #endif /* DOUPDATE */
  5509.  
  5510.         if (pv[SND_SIM].ival > 0)
  5511.           putflags |= PUT_SIM;
  5512.  
  5513.         if (pv[SND_PRM].ival > -1) {
  5514. #ifdef UNIX
  5515.             if (x_usn) {
  5516.                 printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5517.                 x = -9;
  5518.                 goto xputx;
  5519.             }
  5520.             x_prm = pv[SND_PRM].ival;
  5521. #else /* UNIX */
  5522.             printf("?/PERMISSIONS switch is not supported\n");
  5523. #endif /* UNIX */
  5524.         }
  5525. #ifdef FTP_RESTART
  5526.         if (pv[SND_RES].ival > 0) {
  5527.         if (!sizeok) {
  5528.         printf("?PUT /RESTART can't be used because SIZE disabled.\n");
  5529.                 x = -9;
  5530.                 goto xputx;
  5531.         }
  5532.             if (x_usn || putflags) {
  5533.                 printf("?Conflicting switches: /RECOVER %s\n",
  5534.                        x_usn && putflags ? "/UNIQUE /UPDATE" :
  5535.                        (x_usn ? "/UNIQUE" : "/UPDATE")
  5536.                        );
  5537.                 x = -9;
  5538.                 goto xputx;
  5539.             }
  5540. #ifndef NOCSETS
  5541.             if (x_xla &&
  5542.                 (x_csl == FC_UCS2 ||
  5543.                  x_csl == FC_UTF8 ||
  5544.                  x_csr == FC_UCS2 ||
  5545.                  x_csr == FC_UTF8)) {
  5546.                 printf("?/RECOVER can not be used with Unicode translation\n");
  5547.                 x = -9;
  5548.                 goto xputx;
  5549.             }
  5550. #endif /* NOCSETS */
  5551.             putflags = PUT_RES;
  5552.         }
  5553. #endif /* FTP_RESTART */
  5554.     }
  5555.     debug(F101,"ftp PUT restart","",putflags & PUT_RES);
  5556.     debug(F101,"ftp PUT update","",putflags & PUT_UPD);
  5557.  
  5558. #ifdef PIPESEND
  5559.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  5560.         if (!pv[SND_FLT].sval) {
  5561.             sndfilter = NULL;
  5562.         } else {
  5563.             sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5564.             if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5565.         }
  5566.         debug(F110,"ftp put /FILTER", sndfilter, 0);
  5567.     }
  5568.     if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
  5569.       if (putflags)                     /* with pipes or filters */
  5570.         putflags = 0;
  5571. #endif /* PIPESEND */
  5572.  
  5573.     tfc = 0L;                           /* Initialize stats and counters */
  5574.     filcnt = 0;
  5575.     pktnum = 0;
  5576.     spackets = 0L;
  5577.  
  5578.     if (wild)                           /* (is this necessary?) */
  5579.       cx = FTP_MPU;
  5580.  
  5581.     t0 = gmstimer();                    /* Record starting time */
  5582.  
  5583.     done = 0;                           /* Loop control */
  5584.     cancelgroup = 0;
  5585.  
  5586.     cdlevel = 0;
  5587.     cdsimlvl = 0;
  5588.     while (!done && !cancelgroup) {     /* Loop for all files */
  5589.                                         /* or until canceled. */
  5590. #ifdef FTP_PROXY
  5591.         /*
  5592.            If we are using a proxy, we don't use the local file list;
  5593.            instead we use the list on the remote machine which we want
  5594.            sent to someone else, and we use remglob() to get the names.
  5595.            But in that case we shouldn't even be executing this routine;
  5596.            see ftp_mput().
  5597.         */
  5598. #endif /* FTP_PROXY */
  5599.  
  5600.         cancelfile = 0;
  5601.         x = gnfile();                   /* Get next file from list(s) */
  5602.  
  5603.         if (x == 0)                     /* (see gnfile() comments...) */
  5604.           x = gnferror;
  5605.         debug(F111,"FTP PUT gnfile",filnam,x);
  5606.  
  5607.         switch (x) {
  5608.           case 1:                       /* File to send */
  5609.             s2 = asnambuf;
  5610. #ifndef NOSPL
  5611.             if (asnambuf[0]) {          /* As-name */
  5612.                 int n; char *p;         /* to be evaluated... */
  5613.                 n = TMPBUFSIZ;
  5614.                 p = tmpbuf;
  5615.                 zzstring(asnambuf,&p,&n);
  5616.                 s2 = tmpbuf;
  5617.                 debug(F110,"ftp put asname",s2,0);
  5618.             }
  5619. #endif /* NOSPL */
  5620.             rc = putfile(cx,            /* Function (PUT, APPEND) */
  5621.                     filnam, s2,         /* Name to send, as-name */
  5622.                     forcetype, moving,  /* Parameters from switches... */
  5623.                     snd_move, snd_rename, srv_renam,
  5624.                     x_cnv, x_usn, xfiletype, x_prm,
  5625. #ifndef NOCSETS
  5626.                     x_csl, (!x_xla ? -1 : x_csr),
  5627. #else
  5628.                     -1, -1,
  5629. #endif /* NOCSETS */
  5630.                     putflags
  5631.                     );
  5632.             debug(F111,"ftp put putfile rc",filnam,rc);
  5633.             debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  5634.             debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  5635.             if (rc > -1) {
  5636.                 good++;
  5637.                 status = 1;
  5638.             }
  5639.             if (cancelfile)
  5640.               continue;
  5641.             if (rc < 0) {
  5642.                 ftp_fai++;
  5643.                 if (puterror) {
  5644.                     status = 0;
  5645.                     printf("?Fatal upload error: %s\n",filnam);
  5646.                     done++;
  5647.                 }
  5648.             }
  5649.             continue;
  5650.           case 0:                       /* No more files, done */
  5651.             done++;
  5652.             continue;
  5653.           case -1:
  5654.             printf("?%s: file not found - \"%s\"\n",
  5655.                    puterror ? "Fatal" : "Warning",
  5656.                    filnam
  5657.                    );
  5658.             if (puterror) {
  5659.                 status = 0;
  5660.                 done++;
  5661.                 break;
  5662.             }
  5663.             continue;
  5664.           case -2:
  5665.             if (puterror) {
  5666.                 printf("?Fatal: file not found - \"%s\"\n", filnam);
  5667.                 status = 0;
  5668.                 done++;
  5669.                 break;
  5670.             }
  5671.             continue;                   /* Not readable, keep going */
  5672.           case -3:
  5673.             if (puterror) {
  5674.                 printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  5675.                 status = 0;
  5676.                 done++;
  5677.                 break;
  5678.             }
  5679.             printf("?Warning access denied - \"%s\"\n", filnam);
  5680.             continue;
  5681. #ifdef COMMENT
  5682.           case -4:                      /* Canceled */
  5683.             done++;
  5684.             break;
  5685. #endif /* COMMENT */
  5686.           case -5:
  5687.             printf("?Too many files match\n");
  5688.             done++;
  5689.             break;
  5690.           case -6:
  5691.             if (good < 1)
  5692.               printf("?No files selected\n");
  5693.             done++;
  5694.             break;
  5695.           default:
  5696.             printf("?getnextfile() - unknown failure\n");
  5697.             done++;
  5698.         }
  5699.     }
  5700.     if (cdlevel > 0) {
  5701.         while (cdlevel--) {
  5702.             if (cdsimlvl) {
  5703.                 cdsimlvl--;
  5704.             } else if (!doftpcdup())
  5705.               break;
  5706.         }
  5707.     }
  5708.     if (status > 0) {
  5709.         if (cancelgroup)
  5710.           status = 0;
  5711.         else if (cancelfile && good < 1)
  5712.           status = 0;
  5713.     }
  5714.     success = status;
  5715.     x = success;
  5716.  
  5717.   xputx:
  5718.     if (x > -1) {
  5719. #ifdef GFTIMER
  5720.         t1 = gmstimer();                /* End time */
  5721.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5722.         if (!sec) sec = 0.001;
  5723.         fptsecs = sec;
  5724. #else
  5725.         sec = (t1 - t0) / 1000;
  5726.         if (!sec) sec = 1;
  5727. #endif /* GFTIMER */
  5728.         tfcps = (long) (tfc / sec);
  5729.         tsecs = (int)sec;
  5730.         lastxfer = W_FTP|W_SEND;
  5731.         xferstat = success;
  5732.         if (dpyactive)
  5733.           ftscreen(SCR_TC,0,0L,"");
  5734.     }
  5735.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  5736.         if (pv[i].sval)
  5737.           free(pv[i].sval);
  5738.     }
  5739.     ftreset();                          /* Undo switch effects */
  5740.     dpyactive = 0;
  5741.     return(x);
  5742. }
  5743.  
  5744.  
  5745. static char ** mgetlist = NULL;         /* For MGET */
  5746. static int mgetn = 0, mgetx = 0;
  5747. static char xtmpbuf[4096];
  5748.  
  5749. /*
  5750.   c m d l i n g e t
  5751.  
  5752.   Get files specified by -g command-line option.
  5753.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  5754. */
  5755. int
  5756. cmdlinget(stay) int stay; {
  5757.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  5758.     int lcs = -1, rcs = -1, xlate = 0;
  5759.     int first = 1;
  5760.     int mget = 1;
  5761.     int nc;
  5762.     char * s, * s2, * s3;
  5763.     ULONG t0, t1;                       /* Times for stats */
  5764. #ifdef GFTIMER
  5765.     CKFLOAT sec;
  5766. #else
  5767.     int sec = 0;
  5768. #endif /* GFTIMER */
  5769.  
  5770.     if (quiet) {                        /* -q really means quiet */
  5771.         displa = 0;
  5772.         fdispla = 0;
  5773.     } else {
  5774.         displa = 1;
  5775.         fdispla = XYFD_B;
  5776.     }
  5777.     testing = 0;
  5778.     dpyactive = 0;
  5779.     out2screen = 0;
  5780.     what = W_FTP|W_RECV;
  5781.     mgetmethod = 0;
  5782.     mgetforced = 0;
  5783.  
  5784.     havetype = 0;
  5785.     havesize = -1L;
  5786.     makestr(&havemdtm,NULL);
  5787.  
  5788.     if (ftp_fnc < 0)
  5789.       ftp_fnc = fncact;
  5790.  
  5791. #ifndef NOSPL
  5792.     cmd_quoting = 0;
  5793. #endif /* NOSPL */
  5794.     debug(F101,"ftp cmdlinget nfils","",nfils);
  5795.  
  5796.     if (ftp_cnv == CNV_AUTO) {          /* Name conversion is auto */
  5797.         if (alike) {                    /* If server & client are alike */
  5798.             nc = 0;                     /* no conversion */
  5799.         } else {                        /* If they are different */
  5800.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  5801.               nc = -1;                  /* only minimal conversions needed */
  5802.             else                        /* otherwise */
  5803.               nc = 1;                   /* full conversion */
  5804.         }
  5805.     } else                              /* Not auto - do what user said */
  5806.       nc = ftp_cnv;
  5807.  
  5808.     if (nfils < 1)
  5809.       doexit(BAD_EXIT,-1);
  5810.  
  5811.     t0 = gmstimer();                    /* Starting time for this batch */
  5812.  
  5813. #ifndef NOCSETS
  5814.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  5815.         lcs = ftp_csl;                  /* Local charset */
  5816.         if (lcs < 0) lcs = fcharset;
  5817.         if (lcs < 0) xlate = 0;
  5818.     }
  5819.     if (xlate) {                        /* Still ON? */
  5820.         rcs = ftp_csx;                  /* Remote (Server) charset */
  5821.         if (rcs < 0) rcs = ftp_csr;
  5822.         if (rcs < 0) xlate = 0;
  5823.     }
  5824. #endif /* NOCSETS */
  5825.     /*
  5826.       If we have only one file and it is a directory, then we ask for a
  5827.       listing of its contents, rather than retrieving the directory file
  5828.       itself.  This is what (e.g.) Netscape does.
  5829.     */
  5830.     if (nfils == 1) {
  5831.         if (doftpcwd((char *)cmlist[mgetx],-1)) {
  5832.             /* If we can CD to it, it must be a directory */
  5833.             if (recursive) {
  5834.                 cmlist[mgetx] = "*";
  5835.             } else {
  5836.                 status =
  5837.                   (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  5838.                 done = 1;
  5839.             }
  5840.         }
  5841.     }
  5842. /*
  5843.   The following is to work around UNIX servers which, when given a command
  5844.   like "NLST path/blah" (not wild) returns the basename without the path.
  5845. */
  5846.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  5847.         mget = iswild(cmlist[mgetx]);
  5848.     }
  5849.     if (!mget && !done) {               /* Invoked by command-line FTP URL */
  5850.         if (ftp_deb)
  5851.           printf("DOING GET...\n");
  5852.         done++;
  5853.         cancelfile = 0;                 /* This file not canceled yet */
  5854.         s = cmlist[mgetx];
  5855.         rc = 0;                         /* Initial return code */
  5856.     fsize = -1L;
  5857.     if (sizeok) {
  5858.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5859.         if (x == REPLY_COMPLETE)
  5860.           fsize = atol(&ftp_reply_str[4]);
  5861.     }
  5862.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  5863.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5864.  
  5865.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5866.         s2 = tmpbuf;
  5867.  
  5868.         /* If local file already exists, take collision action */
  5869.  
  5870.         x = zchki(s2);
  5871.         if (x > -1) {
  5872.             switch (ftp_fnc) {
  5873.               case XYFX_A:              /* Append */
  5874.                 append = 1;
  5875.                 break;
  5876.               case XYFX_R:              /* Rename */
  5877.               case XYFX_B: {            /* Backup */
  5878.                   char * p = NULL;
  5879.                   int x = -1;
  5880.                   znewn(s2,&p);         /* Make unique name */
  5881.                   debug(F110,"ftp cmdlinget znewn",p,0);
  5882.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  5883.                       x = zrename(s2,p);
  5884.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  5885.                   } else {              /* Rename incoming file */
  5886.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5887.                       s2 = tmpbuf;
  5888.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  5889.                   }
  5890.                   if (x < 0) {
  5891.                       printf("?Backup/Rename failed\n");
  5892.                       return(success = 0);
  5893.                   }
  5894.                   break;
  5895.               }
  5896.               case XYFX_D:              /* Discard */
  5897.                 ftscreen(SCR_FN,'F',0L,s);
  5898.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5899.                 tlog(F100," refused: name","",0);
  5900.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  5901.                 goto xclget;
  5902.  
  5903.               case XYFX_X:              /* Overwrite */
  5904.               case XYFX_U:              /* Update (already handled above) */
  5905.           case XYFX_M:        /* ditto */
  5906.                 break;
  5907.             }
  5908.         }
  5909.         rc = getfile(s,                 /* Remote name */
  5910.                      s2,                /* Local name */
  5911.                      0,                 /* Recover/Restart */
  5912.                      append,            /* Append */
  5913.                      NULL,              /* Pipename */
  5914.                      0,                 /* Translate charsets */
  5915.                      -1,                /* File charset (none) */
  5916.                      -1                 /* Server charset (none) */
  5917.                      );
  5918.         debug(F111,"ftp cmdlinget rc",s,rc);
  5919.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  5920.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  5921.  
  5922.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  5923.             rc = getfile(&s[1],         /* Remote name without leading '/' */
  5924.                          s2,            /* Local name */
  5925.                          0,             /* Recover/Restart */
  5926.                          append,        /* Append */
  5927.                          NULL,          /* Pipename */
  5928.                          0,             /* Translate charsets */
  5929.                          -1,            /* File charset (none) */
  5930.                          -1             /* Server charset (none) */
  5931.                          );
  5932.         if (rc > -1) {
  5933.             good++;
  5934.             status = 1;
  5935.         }
  5936.         if (cancelfile)
  5937.           goto xclget;
  5938.         if (rc < 0) {
  5939.             ftp_fai++;
  5940.             if (geterror) {
  5941.                 status = 0;
  5942.                 done++;
  5943.             }
  5944.         }
  5945.     }
  5946.     if (ftp_deb && !done)
  5947.       printf("DOING MGET...\n");
  5948.     while (!done && !cancelgroup) {
  5949.         cancelfile = 0;                 /* This file not canceled yet */
  5950.         s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  5951.         if (!s) s = "";
  5952.         if (!*s) {
  5953.             first = 1;
  5954.             mgetx++;
  5955.             if (mgetx < nfils)
  5956.               s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  5957.             else
  5958.               s = NULL;
  5959.             debug(F111,"ftp cmdlinget remote_files B",s,0);
  5960.             if (!s) {
  5961.                 done = 1;
  5962.                 break;
  5963.             }
  5964.         }
  5965.         /*
  5966.           The semantics of NLST are ill-defined.  Suppose we have just sent
  5967.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  5968.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  5969.           RETR commands based on the pathless names are not going to work.
  5970.         */
  5971.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  5972.             if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  5973.                 int len, left = 4096;
  5974.                 char * tmp = xtmpbuf;
  5975.                 len = s3 - cmlist[mgetx] + 1;
  5976.                 ckstrncpy(tmp,cmlist[mgetx],left);
  5977.                 tmp += len;
  5978.                 left -= len;
  5979.                 ckstrncpy(tmp,s,left);
  5980.                 s = xtmpbuf;
  5981.         debug(F111,"ftp cmdlinget remote_files X",s,0);
  5982.             }
  5983.         }
  5984.         first = 0;                      /* Not first any more */
  5985.  
  5986.     debug(F111,"ftp cmdlinget havetype",s,havetype);
  5987.     if (havetype > 0 && havetype != FTYP_FILE) { /* Server says not file */
  5988.         debug(F110,"ftp cmdlinget not-a-file",s,0);
  5989.         continue;
  5990.     }
  5991.         rc = 0;                         /* Initial return code */
  5992.     if (havesize > -1L) {        /* Already have file size? */
  5993.         fsize = havesize;
  5994.     } else {            /* No - must ask server */
  5995.         /*
  5996.           Prior to sending the NLST command we necessarily put the
  5997.           server into ASCII mode.  We must now put it back into the
  5998.           the requested mode so the upcoming SIZE command returns
  5999.           right kind of size; this is especially important for
  6000.           GET /RECOVER; otherwise the server returns the "ASCII" size
  6001.           of the file, rather than its true size.
  6002.         */
  6003.         changetype(ftp_typ,0);    /* Change to requested type */
  6004.         fsize = -1L;
  6005.         if (sizeok) {
  6006.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm);
  6007.         if (x == REPLY_COMPLETE)
  6008.           fsize = atol(&ftp_reply_str[4]);
  6009.         }
  6010.     }
  6011.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6012.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  6013.  
  6014.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  6015.         s2 = tmpbuf;
  6016.  
  6017.         /* If local file already exists, take collision action */
  6018.  
  6019.         x = zchki(s2);
  6020.         if (x > -1) {
  6021.             switch (ftp_fnc) {
  6022.               case XYFX_A:              /* Append */
  6023.                 append = 1;
  6024.                 break;
  6025.               case XYFX_R:              /* Rename */
  6026.               case XYFX_B: {            /* Backup */
  6027.                   char * p = NULL;
  6028.                   int x = -1;
  6029.                   znewn(s2,&p);         /* Make unique name */
  6030.                   debug(F110,"ftp cmdlinget znewn",p,0);
  6031.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  6032.                       x = zrename(s2,p);
  6033.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  6034.                   } else {              /* Rename incoming file */
  6035.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6036.                       s2 = tmpbuf;
  6037.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  6038.                   }
  6039.                   if (x < 0) {
  6040.                       printf("?Backup/Rename failed\n");
  6041.                       return(success = 0);
  6042.                   }
  6043.                   break;
  6044.               }
  6045.               case XYFX_D:      /* Discard */
  6046.                 ftscreen(SCR_FN,'F',0L,s);
  6047.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  6048.                 tlog(F100," refused: name","",0);
  6049.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  6050.                 continue;
  6051.               case XYFX_X:              /* Overwrite */
  6052.               case XYFX_U:              /* Update (already handled above) */
  6053.               case XYFX_M:              /* ditto */
  6054.                 break;
  6055.             }
  6056.         }
  6057.                                         /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  6058.         rc = getfile(s,                 /* Remote name */
  6059.                      s2,                /* Local name */
  6060.                      0,                 /* Recover/Restart */
  6061.                      append,            /* Append */
  6062.                      NULL,              /* Pipename */
  6063.                      0,                 /* Translate charsets */
  6064.                      -1,                /* File charset (none) */
  6065.                      -1                 /* Server charset (none) */
  6066.                      );
  6067.         debug(F111,"ftp cmdlinget rc",s,rc);
  6068.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  6069.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  6070.  
  6071.         if (rc > -1) {
  6072.             good++;
  6073.             status = 1;
  6074.         }
  6075.         if (cancelfile)
  6076.           continue;
  6077.         if (rc < 0) {
  6078.             ftp_fai++;
  6079.             if (geterror) {
  6080.                 status = 0;
  6081.                 done++;
  6082.             }
  6083.         }
  6084.     }
  6085.  
  6086.   xclget:
  6087.     if (cancelgroup)
  6088.       mlsreset();
  6089.     if (status > 0) {
  6090.         if (cancelgroup)
  6091.           status = 0;
  6092.         else if (cancelfile && good < 1)
  6093.           status = 0;
  6094.     }
  6095.     success = status;
  6096.  
  6097. #ifdef GFTIMER
  6098.     t1 = gmstimer();                    /* End time */
  6099.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6100.     if (!sec) sec = 0.001;
  6101.     fptsecs = sec;
  6102. #else
  6103.     sec = (t1 - t0) / 1000;
  6104.     if (!sec) sec = 1;
  6105. #endif /* GFTIMER */
  6106.  
  6107.     tfcps = (long) (tfc / sec);
  6108.     tsecs = (int)sec;
  6109.     lastxfer = W_FTP|W_RECV;
  6110.     xferstat = success;
  6111.     if (dpyactive)
  6112.       ftscreen(SCR_TC,0,0L,"");
  6113.     if (!stay)
  6114.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  6115.     return(success);
  6116. }
  6117.  
  6118. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  6119.  
  6120. /*
  6121.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  6122.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  6123.   the user-interface perspective, since the server's directory listings show
  6124.   its own local times and since we don't know what timezone it's in, there's
  6125.   no way to reconcile our local times with the server's.
  6126. */
  6127. int
  6128. doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  6129.     struct FDB fl, sw, cm;
  6130.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  6131.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  6132.     int x_fnc = 0, first = 0, skipthis = 0, append = 0, selected = 0;
  6133.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  6134.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  6135.     int gotsize = 0;
  6136.     int matchdot = 0;
  6137.     long getlarger = -1, getsmaller = -1;
  6138.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  6139.     char * src = "", * local = "";
  6140.     char * pat = "";
  6141.  
  6142.     int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
  6143.     int x_xla = 0;
  6144.     char c;                             /* Worker char */
  6145.     ULONG t0 = 0L, t1;                  /* Times for stats */
  6146. #ifdef GFTIMER
  6147.     CKFLOAT sec;
  6148. #else
  6149.     int sec = 0;
  6150. #endif /* GFTIMER */
  6151.  
  6152.     struct stringint {                  /* Temporary array for switch values */
  6153.         char * sval;
  6154.         int ival;
  6155.     } pv[SND_MAX+1];
  6156.  
  6157.     success = 0;                        /* Assume failure */
  6158.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  6159.     restart = 0;                        /* No restart yet */
  6160.     out2screen = 0;            /* No TO-SCREEN switch given yet */
  6161.     mgetmethod = 0;            /* No NLST or MLSD switch yet */
  6162.     mgetforced = 0;
  6163.  
  6164.     g_displa = fdispla;
  6165.     if (ftp_dis > -1)
  6166.       fdispla = ftp_dis;
  6167.  
  6168.     x_cnv = ftp_cnv;                    /* Filename conversion */
  6169.     if (x_cnv == CNV_AUTO) {        /* Name conversion is auto */
  6170.         if (alike) {                    /* If server & client are alike */
  6171.             x_cnv = 0;            /* no conversion */
  6172.         } else {                        /* If they are different */
  6173.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6174.               x_cnv = -1;        /* only minimal conversions needed */
  6175.             else                        /* otherwise */
  6176.               x_cnv = 1;        /* full conversion */
  6177.         }
  6178.     } else                              /* Not auto - do what user said */
  6179.       x_cnv = ftp_cnv;
  6180.  
  6181.     x_prm = ftp_prm;                    /* Permissions */
  6182.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  6183.       x_prm = alike;
  6184.  
  6185. #ifndef NOCSETS
  6186.     x_csr = ftp_csr;                    /* Inherit global server charset */
  6187.     x_csl = ftp_csl;                    /* Inherit global local charset */
  6188.     if (x_csl < 0)                      /* If none, use current */
  6189.       x_csl = fcharset;                 /* file character-set. */
  6190.     x_xla = ftp_xla;                    /* Translation On/Off */
  6191. #endif /* NOCSETS */
  6192.  
  6193.     geterror = ftp_err;                 /* Inherit global error action. */
  6194.     asnambuf[0] = NUL;                  /* No as-name yet. */
  6195.     pipesave = pipesend;
  6196.     pipesend = 0;
  6197.  
  6198.     havetype = 0;
  6199.     havesize = -1L;
  6200.     makestr(&havemdtm,NULL);
  6201.  
  6202.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  6203.         ftp_typ = g_ftp_typ;
  6204.         /* g_ftp_typ = -1; */
  6205.     }
  6206.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  6207.         pv[i].sval = NULL;              /* to null pointers */
  6208.         pv[i].ival = -1;                /* and -1 int values */
  6209.     }
  6210.     zclose(ZMFILE);                     /* In case it was left open */
  6211.  
  6212.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  6213.  
  6214.     if (fp_nml) {                       /* Reset /NAMELIST */
  6215.         if (fp_nml != stdout)
  6216.           fclose(fp_nml);
  6217.         fp_nml = NULL;
  6218.     }
  6219.     makestr(&ftp_nml,NULL);
  6220.  
  6221.     /* Initialize list of remote filespecs */
  6222.  
  6223.     if (!mgetlist) {
  6224.         mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  6225.         if (!mgetlist) {
  6226.             printf("?Memory allocation failure - MGET list\n");
  6227.             return(-9);
  6228.         }
  6229.         for (i = 0; i < MGETMAX; i++)
  6230.           mgetlist[i] = NULL;
  6231.     }
  6232.     mgetn = 0;                          /* Number of mget arguments */
  6233.     mgetx = 0;                          /* Current arg */
  6234.  
  6235.     if (who == 0) {                     /* Called with unprefixed command */
  6236.         if (cx == XXGET || cx == XXREGET || cx == XXRETR)
  6237.           getone++;
  6238.         switch (cx) {
  6239.           case XXREGET: pv[SND_RES].ival = 1; break;
  6240.           case XXRETR:  pv[SND_DEL].ival = 1; break;
  6241.           case XXGET:
  6242.           case XXMGET:  mget++; break;
  6243.         }
  6244.     } else {                            /* FTP command */
  6245.         if (cx == FTP_GET || cx == FTP_RGE)
  6246.           getone++;
  6247.         switch (cx) {
  6248.           case FTP_DEL:                 /* (fall thru on purpose) */
  6249.           case FTP_MDE: mdel++;         /* (ditto) */
  6250.           case FTP_GET:                 /* (ditto) */
  6251.           case FTP_MGE: mget++; break;
  6252.           case FTP_RGE: pv[SND_RES].ival = 1; break;
  6253.         }
  6254.     }
  6255.     cmfdbi(&sw,                         /* First FDB - command switches */
  6256.            _CMKEY,                      /* fcode */
  6257.            "Remote filename;\n or switch", /* hlpmsg */
  6258.            "",                          /* default */
  6259.            "",                          /* addtl string data */
  6260.            mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  6261.            4,                           /* addtl numeric data 2: 4 = cmswi */
  6262.            xxstring,                    /* Processing function */
  6263.            mdel ? delswi : getswi,      /* Keyword table */
  6264.            &fl                          /* Pointer to next FDB */
  6265.            );
  6266.     cmfdbi(&fl,                         /* 2nd FDB - remote filename */
  6267.            _CMFLD,                      /* fcode */
  6268.            "",                          /* hlpmsg */
  6269.            "",                          /* default */
  6270.            "",                          /* addtl string data */
  6271.            0,                           /* addtl numeric data 1 */
  6272.            0,                           /* addtl numeric data 2 */
  6273.            xxstring,
  6274.            NULL,
  6275.            &cm
  6276.            );
  6277.     cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
  6278.            _CMCFM,                      /* fcode */
  6279.            "",                          /* hlpmsg */
  6280.            "",                          /* default */
  6281.            "",                          /* addtl string data */
  6282.            0,                           /* addtl numeric data 1 */
  6283.            0,                           /* addtl numeric data 2 */
  6284.            NULL,
  6285.            NULL,
  6286.            NULL
  6287.            );
  6288.  
  6289.     while (1) {                         /* Parse 0 or more switches */
  6290.         x = cmfdb(&sw);                 /* Parse something */
  6291.         debug(F101,"ftp get cmfdb","",x);
  6292.         if (x < 0)                      /* Error */
  6293.           goto xgetx;                   /* or reparse needed */
  6294.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  6295.           break;
  6296.         c = cmgbrk();                   /* Get break character */
  6297.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  6298.         if (getval && !(cmresult.kflags & CM_ARG)) {
  6299.             printf("?This switch does not take arguments\n");
  6300.             x = -9;
  6301.             goto xgetx;
  6302.         }
  6303.         n = cmresult.nresult;           /* Numeric result = switch value */
  6304.         debug(F101,"ftp get switch","",n);
  6305.  
  6306.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6307.             printf("?This switch requires an argument\n");
  6308.             x = -9;
  6309.             goto xgetx;
  6310.         }
  6311.         switch (n) {                    /* Process the switch */
  6312.           case SND_ASN:                 /* /AS-NAME: */
  6313.             debug(F101,"ftp get /as-name getval","",getval);
  6314.             if (!getval) break;
  6315.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  6316.                 if (x == -3) {
  6317.                     printf("?name required\n");
  6318.                     x = -9;
  6319.                 }
  6320.                 goto xgetx;
  6321.             }
  6322.             s = brstrip(s);
  6323.             if (!*s) s = NULL;
  6324.             makestr(&(pv[n].sval),s);
  6325.             pv[n].ival = 1;
  6326.             break;
  6327.  
  6328.           case SND_BIN:                 /* /BINARY */
  6329.           case SND_TXT:                 /* /TEXT or /ASCII */
  6330.           case SND_TEN:                 /* /TENEX */
  6331.             pv[SND_BIN].ival = 0;
  6332.             pv[SND_TXT].ival = 0;
  6333.             pv[SND_TEN].ival = 0;
  6334.             pv[n].ival = 1;
  6335.             break;
  6336.  
  6337. #ifdef PUTPIPE
  6338.           case SND_CMD:                 /* These take no args */
  6339.             if (nopush) {
  6340.                 printf("?Sorry, system command access is disabled\n");
  6341.                 x = -9;
  6342.                 goto xgetx;
  6343.             }
  6344. #ifdef PIPESEND
  6345.             else if (rcvfilter) {
  6346.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  6347.                 x = -9;
  6348.                 goto xgetx;
  6349.             }
  6350. #endif /* PIPESEND */
  6351.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  6352.             pv[n].ival = 1;             /* Just set the flag */
  6353.             pv[SND_ARR].ival = 0;
  6354.             break;
  6355. #endif /* PUTPIPE */
  6356.  
  6357.           case SND_SHH:                 /* /QUIET */
  6358.           case SND_RES:                 /* /RECOVER (reget) */
  6359.           case SND_NOB:                 /* /NOBACKUPFILES */
  6360.           case SND_DEL:                 /* /DELETE */
  6361.           case SND_UPD:                 /* /UPDATE */
  6362.           case SND_USN:                 /* /UNIQUE */
  6363.           case SND_NOD:                 /* /NODOTFILES */
  6364.           case SND_REC:                 /* /RECOVER */
  6365.           case SND_MAI:                 /* /TO-SCREEN */
  6366.             pv[n].ival = 1;             /* Just set the flag */
  6367.             break;
  6368.  
  6369.           case SND_DIF:                 /* /DATES-DIFFER */
  6370.         pv[SND_COL].ival = XYFX_M;    /* Now it's a collision option */
  6371.         pv[n].ival = 1;
  6372.         break;
  6373.  
  6374.           case SND_COL:                 /* /COLLISION: */
  6375.             if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  6376.               goto xgetx;
  6377.         if (x == XYFX_M)
  6378.           pv[SND_DIF].ival = 1;    /* (phase this out) */
  6379.         pv[n].ival = x;        /* this should be sufficient */
  6380.             break;
  6381.  
  6382.           case SND_ERR:                 /* /ERROR-ACTION */
  6383.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  6384.               goto xgetx;
  6385.             pv[n].ival = x;
  6386.             break;
  6387.  
  6388.           case SND_EXC:                 /* Exception list */
  6389.             if (!getval) break;
  6390.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  6391.                 if (x == -3) {
  6392.                     printf("?Pattern required\n");
  6393.                     x = -9;
  6394.                 }
  6395.                 goto xgetx;
  6396.             }
  6397.             if (s) if (!*s) s = NULL;
  6398.             makestr(&(pv[n].sval),s);
  6399.             if (pv[n].sval)
  6400.               pv[n].ival = 1;
  6401.             break;
  6402.  
  6403. #ifdef PIPESEND
  6404.           case SND_FLT:
  6405.             debug(F101,"ftp get /filter getval","",getval);
  6406.             if (!getval) break;
  6407.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  6408.                 if (x == -3)
  6409.                   s = "";
  6410.                 else
  6411.                   goto xgetx;
  6412.             }
  6413.             s = brstrip(s);
  6414.             if (pv[SND_MAI].ival < 1) {
  6415.                 y = strlen(s);
  6416.                 /* Make sure they included "\v(...)" */
  6417.                 for (x = 0; x < y; x++) {
  6418.                     if (s[x] != '\\') continue;
  6419.                     if (s[x+1] == 'v') break;
  6420.                 }
  6421.                 if (x == y) {
  6422.                     printf(
  6423.                 "?Filter must contain a replacement variable for filename.\n"
  6424.                            );
  6425.                     x = -9;
  6426.                     goto xgetx;
  6427.                 }
  6428.             }
  6429.             if (*s) {
  6430.                 pv[n].ival = 1;
  6431.                 makestr(&(pv[n].sval),s);
  6432.             } else {
  6433.                 pv[n].ival = 0;
  6434.                 makestr(&(pv[n].sval),NULL);
  6435.             }
  6436.             break;
  6437. #endif /* PIPESEND */
  6438.  
  6439.           case SND_NAM:
  6440.             if (!getval) break;
  6441.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  6442.               goto xgetx;
  6443.             debug(F101,"ftp get /filenames","",x);
  6444.             pv[n].ival = x;
  6445.             break;
  6446.  
  6447.           case SND_SMA:                 /* Smaller / larger than */
  6448.           case SND_LAR:
  6449.             if (!getval) break;
  6450.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  6451.               goto xgetx;
  6452.             pv[n].ival = y;
  6453.             break;
  6454.  
  6455.           case SND_FIL:                 /* Name of file containing filnames */
  6456.             if (!getval) break;
  6457.             if ((x = cmifi("Name of file containing list of filenames",
  6458.                                "",&s,&y,xxstring)) < 0) {
  6459.                 if (x == -3) {
  6460.                     printf("?Filename required\n");
  6461.                     x = -9;
  6462.                 }
  6463.                 goto xgetx;
  6464.             } else if (y && iswild(s)) {
  6465.                 printf("?Wildcards not allowed BBB\n");
  6466.                 x = -9;
  6467.                 goto xgetx;
  6468.             }
  6469.             if (s) if (!*s) s = NULL;
  6470.             makestr(&(pv[n].sval),s);
  6471.             if (pv[n].sval)
  6472.               pv[n].ival = 1;
  6473.             break;
  6474.  
  6475.           case SND_MOV:                 /* MOVE after */
  6476.           case SND_REN:                 /* RENAME after */
  6477.           case SND_SRN: {               /* SERVER-RENAME */
  6478.               char * m = "";
  6479.               switch (n) {
  6480.                 case SND_MOV:
  6481.                   m =
  6482.                    "Device and/or directory for incoming file after reception";
  6483.                   break;
  6484.                 case SND_REN:
  6485.                   m = "New name for incoming file after reception";
  6486.                   break;
  6487.                 case SND_SRN:
  6488.                   m = "New name for source file on server after reception";
  6489.                   break;
  6490.               }
  6491.               if (!getval) break;
  6492.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6493.                   if (x == -3) {
  6494.                       printf("%s\n", n == SND_MOV ?
  6495.                              "?Destination required" :
  6496.                              "?New name required"
  6497.                              );
  6498.                       x = -9;
  6499.                   }
  6500.                   goto xgetx;
  6501.               }
  6502.               makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6503.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  6504.               break;
  6505.           }
  6506. #ifndef NOCSETS
  6507.           case SND_CSL:                 /* Local character set */
  6508.           case SND_CSR:                 /* Remote (server) charset */
  6509.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6510.               return((x == -3) ? -2 : x);
  6511.             if (n == SND_CSL)
  6512.               x_csl = x;
  6513.             else
  6514.               x_csr = x;
  6515.             x_xla = 1;                  /* Overrides global OFF setting */
  6516.             break;
  6517.  
  6518.           case SND_XPA:                 /* Transparent */
  6519.             x_xla =  0;
  6520.             x_csr = -1;
  6521.             x_csl = -1;
  6522.             break;
  6523. #endif /* NOCSETS */
  6524.  
  6525.           case SND_NML:
  6526.             if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6527.               goto xgetx;
  6528.             makestr(&ftp_nml,s);
  6529.             break;
  6530.  
  6531.       case SND_PAT:            /* /PATTERN: */
  6532.         if (!getval) break;
  6533.         if ((x = cmfld("Pattern","*", &s, xxstring)) < 0)
  6534.           goto xgetx;
  6535.         makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6536.         pv[n].ival = (pv[n].sval) ? 1 : 0;
  6537.         break;
  6538.  
  6539.       case SND_NLS:            /* /NLST */
  6540.             pv[n].ival = 1;        /* Use NLST */
  6541.         pv[SND_MLS].ival = 0;    /* Don't use MLSD */
  6542.         break;
  6543.  
  6544.       case SND_MLS:            /* /MLSD */
  6545.             pv[n].ival = 1;        /* Use MLSD */
  6546.         pv[SND_NLS].ival = 0;    /* Don't use NLST */
  6547.         break;
  6548.  
  6549.           default:                      /* /AFTER, /PERMISSIONS, etc... */
  6550.             printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6551.             x = -9;
  6552.             goto xgetx;
  6553.         }
  6554.     }
  6555.     line[0] = NUL;
  6556.     cmarg = line;
  6557.     cmarg2 = asnambuf;
  6558.     s = line;
  6559. /*
  6560.   For GET, we want to parse an optional as-name, like with PUT.
  6561.   For MGET, we must parse a list of names, and then send NLST or MLSD
  6562.   commands for each name separately.
  6563. */
  6564.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  6565.       case _CMFLD:                      /* Field */
  6566.         if (!getone) {
  6567.             s = brstrip(cmresult.sresult);
  6568.             makestr(&(mgetlist[mgetn++]),s);
  6569.             while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6570.                 if (x < 0)
  6571.                   goto xgetx;
  6572.                 makestr(&(mgetlist[mgetn++]),brstrip(s));
  6573.                 if (mgetn >= MGETMAX) {
  6574.                     printf("?Too many items in MGET list\n");
  6575.                     goto xgetx;
  6576.                 }
  6577.             }
  6578.             if ((x = cmcfm()) < 0)
  6579.               goto xgetx;
  6580.         } else {
  6581.             s = brstrip(cmresult.sresult);
  6582.             ckstrncpy(line,s,LINBUFSIZ);
  6583.             if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6584.               if (x != -3)
  6585.                 goto xgetx;
  6586.             s = brstrip(s);
  6587.             ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6588.             if ((x = cmcfm()) < 0)
  6589.               goto xgetx;
  6590.         }
  6591.         break;
  6592.       case _CMCFM:                      /* Confirmation */
  6593.         break;
  6594.       default:
  6595.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  6596.         x = -9;
  6597.         goto xgetx;
  6598.     }
  6599.     if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
  6600.       recursive = 2;
  6601.  
  6602.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  6603.         forcetype = 1;                  /* So skip the name-pattern match */
  6604.         ftp_typ = XYFT_B;               /* Set binary */
  6605.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  6606.         forcetype = 1;
  6607.         ftp_typ = XYFT_T;
  6608.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  6609.         forcetype = 1;
  6610.         ftp_typ = FTT_TEN;
  6611.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  6612.         forcetype = 1;
  6613.         ftp_typ = binary;
  6614.         g_ftp_typ = binary;
  6615.     }
  6616.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  6617.         char * p;
  6618.         p = brstrip(pv[SND_ASN].sval);  /* As-name */
  6619.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  6620.     }
  6621.     debug(F110,"ftp get asnambuf",asnambuf,0);
  6622.  
  6623. #ifdef PIPESEND
  6624.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  6625.         char * p;
  6626.         p = asnambuf;
  6627.         debug(F110,"GET /COMMAND before stripping",p,0);
  6628.         p = brstrip(p);
  6629.         debug(F110,"GET /COMMAND after stripping",p,0);
  6630.         if (!*p) {
  6631.             printf("?Sorry, a command to write to is required\n");
  6632.             x = -9;
  6633.             goto xgetx;
  6634.         }
  6635.         pipename = p;
  6636.         pipesend = 1;
  6637.     }
  6638. #endif /* PIPESEND */
  6639.  
  6640. /* Set up /MOVE and /RENAME */
  6641.  
  6642.     if (pv[SND_DEL].ival > 0 &&
  6643.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  6644.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  6645.         x = -9;
  6646.         goto xgetx;
  6647.     }
  6648. #ifdef CK_TMPDIR
  6649.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  6650.         int len;
  6651.         char * p = pv[SND_MOV].sval;
  6652.         len = strlen(p);
  6653.         if (!isdir(p)) {                /* Check directory */
  6654. #ifdef CK_MKDIR
  6655.             char * s = NULL;
  6656.             s = (char *)malloc(len + 4);
  6657.             if (s) {
  6658.                 strcpy(s,p);            /* safe */
  6659. #ifdef datageneral
  6660.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  6661. #else
  6662.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  6663. #endif /* datageneral */
  6664.                 s[len++] = 'X';
  6665.                 s[len] = NUL;
  6666. #ifdef NOMKDIR
  6667.                 x = -1;
  6668. #else
  6669.                 x = zmkdir(s);
  6670. #endif /* NOMKDIR */
  6671.                 free(s);
  6672.                 if (x < 0) {
  6673.                     printf("?Can't create \"%s\"\n",p);
  6674.                     x = -9;
  6675.                     goto xgetx;
  6676.                 }
  6677.             }
  6678. #else
  6679.             printf("?Directory \"%s\" not found\n",p);
  6680.             x = -9;
  6681.             goto xgetx;
  6682. #endif /* CK_MKDIR */
  6683.         }
  6684.         makestr(&rcv_move,p);
  6685.         moving = 1;
  6686.     }
  6687. #endif /* CK_TMPDIR */
  6688.  
  6689.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  6690.         char * p = pv[SND_REN].sval;
  6691.         if (!p) p = "";
  6692.         if (!*p) {
  6693.             printf("?New name required for /RENAME\n");
  6694.             x = -9;
  6695.             goto xgetx;
  6696.         }
  6697.         p = brstrip(p);
  6698. #ifndef NOSPL
  6699.     /* If name given is wild, rename string must contain variables */
  6700.         if (mget && !getone) {
  6701.             char * s = tmpbuf;
  6702.             x = TMPBUFSIZ;
  6703.             zzstring(p,&s,&x);
  6704.             if (!strcmp(tmpbuf,p)) {
  6705.                 printf(
  6706.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  6707.                        );
  6708.                 x = -9;
  6709.                 goto xgetx;
  6710.             }
  6711.         }
  6712. #endif /* NOSPL */
  6713.         renaming = 1;
  6714.         makestr(&rcv_rename,p);
  6715.         debug(F110,"FTP rcv_rename",rcv_rename,0);
  6716.     }
  6717.     if (!cmarg[0] && mgetn == 0 && getone && pv[SND_FIL].ival < 1) {
  6718.         printf("?Filename required but not given\n");
  6719.         x = -9;
  6720.         goto xgetx;
  6721.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  6722.         printf("?You can't give both /LISTFILE and a remote filename\n");
  6723.         x = -9;
  6724.         goto xgetx;
  6725.     }
  6726.     CHECKCONN();                        /* Check connection */
  6727.  
  6728.     if (pv[SND_COL].ival > -1)
  6729.       x_fnc = pv[SND_COL].ival;
  6730.  
  6731. #ifndef NOSPL
  6732.     /* If as-name given for MGET, as-name must contain variables */
  6733.     if (mget && !getone && asnambuf[0] && x_fnc != XYFX_A) {
  6734.         char * s = tmpbuf;
  6735.         x = TMPBUFSIZ;
  6736.         zzstring(asnambuf,&s,&x);
  6737.         if (!strcmp(tmpbuf,asnambuf)) {
  6738.             printf(
  6739.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  6740.                    );
  6741.             x = -9;
  6742.             goto xgetx;
  6743.         }
  6744.     }
  6745. #endif /* NOSPL */
  6746.  
  6747. /* doget: */
  6748.  
  6749.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  6750.         fdispla = 0;
  6751.     } else {
  6752.         displa = 1;
  6753.         if (mdel || ftp_deb)
  6754.       fdispla = XYFD_B;
  6755.     }
  6756.     deleting = 0;
  6757.     if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
  6758.       deleting = 1;
  6759.     if (pv[SND_EXC].ival > 0)
  6760.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  6761.     if (pv[SND_SMA].ival > -1)
  6762.       getsmaller = pv[SND_SMA].ival;
  6763.     if (pv[SND_LAR].ival > -1)
  6764.       getlarger = pv[SND_LAR].ival;
  6765.     if (pv[SND_NAM].ival > -1)
  6766.       x_cnv = pv[SND_NAM].ival;
  6767.     if (pv[SND_ERR].ival > -1)
  6768.       geterror = pv[SND_ERR].ival;
  6769.     if (pv[SND_MAI].ival > -1)
  6770.       toscreen = 1;
  6771.  
  6772.     if (pv[SND_NLS].ival > 0) {        /* Force NLST or MLSD? */
  6773.     mgetmethod = SND_NLS;
  6774.     mgetforced = 1;
  6775.     } else if (pv[SND_MLS].ival > 0) {
  6776.     mgetmethod = SND_MLS;
  6777.     mgetforced = 1;
  6778.     }
  6779.  
  6780. #ifdef FTP_RESTART
  6781.     if (pv[SND_RES].ival > 0) {
  6782.         if (!ftp_typ) {
  6783.             printf("?Sorry, GET /RECOVER requires binary mode\n");
  6784.             x = -9;
  6785.             goto xgetx;
  6786. #ifdef COMMENT
  6787.         /* Not true - the fact that the initial REST fails does not mean */
  6788.         /* it will fail here.  */
  6789.         } else if (!okrestart) {
  6790.             printf("WARNING: Server might not support restart...\n");
  6791. #endif /* COMMENT */
  6792.         }
  6793.         restart = 1;
  6794.     }
  6795. #endif /* FTP_RESTART */
  6796.  
  6797. #ifdef PIPESEND
  6798.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  6799.         if (pipesend) {
  6800.             printf("?Switch conflict: /FILTER and /COMMAND\n");
  6801.             x = -9;
  6802.             goto xgetx;
  6803.         }
  6804.         makestr(&rcvfilter,pv[SND_FLT].sval);
  6805.         debug(F110,"ftp get /FILTER", rcvfilter, 0);
  6806.     }
  6807.     if (rcvfilter || pipesend) {        /* /RESTART */
  6808. #ifdef FTP_RESTART
  6809.         if (restart) {                  /* with pipes or filters */
  6810.             printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  6811.             x = -9;
  6812.             goto xgetx;
  6813.         }
  6814. #endif /* FTP_RESTART */
  6815.         if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_M || x_fnc == XYFX_U) {
  6816.             printf(
  6817.         "?Switch conflict: /FILTER or /COMMAND and Date Checking\n");
  6818.             x = -9;
  6819.             goto xgetx;
  6820.         }
  6821.     }
  6822. #endif /* PIPESEND */
  6823.  
  6824.     tfc = 0L;                           /* Initialize stats and counters */
  6825.     filcnt = 0;
  6826.     pktnum = 0;
  6827.     rpackets = 0L;
  6828.  
  6829.     if (pv[SND_FIL].ival > 0) {
  6830.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  6831.             debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  6832.             printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  6833.             x = -9;
  6834.             goto xgetx;
  6835.         }
  6836.         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  6837.             zclose(ZMFILE);                       /* Failed */
  6838.             debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  6839.             printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  6840.             x = -9;
  6841.             goto xgetx;
  6842.         }
  6843.         listfile = 1;
  6844.         debug(F110,"ftp get listfile first",tmpbuf,0);
  6845.         makestr(&(mgetlist[0]),tmpbuf);
  6846.     }
  6847.     t0 = gmstimer();                    /* Record starting time */
  6848.  
  6849.     updating = 0;            /* Checking dates? */
  6850.     if (pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U))
  6851.       updating = 1;
  6852.     if (pv[SND_DIF].ival > 0 || x_fnc == XYFX_M)
  6853.       updating = 2;
  6854.     if (updating)            /* These switches force FTP DATES ON */
  6855.       ftp_dates |= 2;
  6856.  
  6857.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  6858.  
  6859.     cancelgroup = 0;                    /* Group not canceled yet */
  6860.     if (!(xfermode == XMODE_A && patterns && get_auto && !forcetype))
  6861.       changetype(ftp_typ,0);        /* Change to requested type */
  6862.     binary = ftp_typ;                   /* For file-transfer display */
  6863.     first = 1;                          /* For MGET list */
  6864.     done = 0;                           /* Loop control */
  6865.  
  6866. #ifdef CK_TMPDIR
  6867.     if (dldir && !f_tmpdir) {           /* If they have a download directory */
  6868.         if ((s = zgtdir())) {           /* Get current directory */
  6869.             if (zchdir(dldir)) {        /* Change to download directory */
  6870.                 ckstrncpy(savdir,s,TMPDIRLEN);
  6871.                 f_tmpdir = 1;           /* Remember that we did this */
  6872.             }
  6873.         }
  6874.     }
  6875. #endif /* CK_TMPDIR */
  6876.  
  6877.     if (ftp_nml) {                      /* /NAMELIST */
  6878.         debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  6879.         if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  6880.           fp_nml = stdout;
  6881.         else
  6882.           fp_nml = fopen(ftp_nml, "wb");
  6883.         if (!fp_nml) {
  6884.             printf("?%s: %s\n",ftp_nml,ck_errstr());
  6885.             goto xgetx;
  6886.         }
  6887.     }
  6888.     while (!done && !cancelgroup) {     /* Loop for all files */
  6889.                                         /* or until canceled. */
  6890. #ifdef FTP_PROXY
  6891.         /* do something here if proxy */
  6892. #endif /* FTP_PROXY */
  6893.  
  6894.         rs_len = 0L;                    /* REGET position */
  6895.         cancelfile = 0;                 /* This file not canceled yet */
  6896.         haspath = 0;                    /* Recalculate this each time thru */
  6897.  
  6898.         if (getone) {                   /* GET */
  6899.             char * p;
  6900.             s = line;
  6901.             src = line;                 /* Server name */
  6902.             done = 1;
  6903.             debug(F111,"ftp get file",s,0);
  6904.         } else if (mget) {              /* MGET */
  6905.             src = mgetlist[mgetx];
  6906.             debug(F111,"ftp mget remote_files A",src,first);
  6907.             s = (char *)remote_files(first,
  6908.                      (CHAR *)mgetlist[mgetx],
  6909.                      (CHAR *)pv[SND_PAT].sval,
  6910.                      0
  6911.                      );
  6912.             debug(F110,"ftp mget remote_files B",s,0);
  6913.             if (!s) s = "";
  6914.             if (!*s) {
  6915.                 first = 1;
  6916.                 if (listfile) {        /* Names from listfile */
  6917.                   again:
  6918.                     tmpbuf[0] = NUL;
  6919.                     while (!tmpbuf[0]) {
  6920.                         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  6921.                             zclose(ZMFILE);
  6922.                             debug(F110,"ftp get listfile EOF",
  6923.                                   pv[SND_FIL].sval,0);
  6924.                             makestr(&(mgetlist[0]),NULL);
  6925.                             s = NULL;
  6926.                             done = 1;
  6927.                             break;
  6928.                         }
  6929.                     }
  6930.                     if (done)
  6931.                       continue;
  6932.  
  6933.                     makestr(&(mgetlist[0]),tmpbuf);
  6934.             debug(F110,"ftp get listfile next",tmpbuf,0);
  6935.                     s = (char *)remote_files(first,
  6936.                          (CHAR *)mgetlist[0],
  6937.                          (CHAR *)pv[SND_PAT].sval,
  6938.                          0
  6939.                          );
  6940.             debug(F110,"ftp mget remote_files C",s,0);
  6941.                     if (!s) {
  6942.                         ftscreen(SCR_FN,'F',0L,s);
  6943.                         ftscreen(SCR_ST,ST_MSG,0L,"File not found");
  6944.                         tlog(F110,"ftp get file not found:",s,0);
  6945.                         goto again;
  6946.                     }
  6947.                 } else {        /* Names from command line */
  6948.                     mgetx++;
  6949.                     if (mgetx < mgetn)
  6950.               s = (char *)remote_files(first,
  6951.                            (CHAR *)mgetlist[mgetx],
  6952.                            (CHAR *)pv[SND_PAT].sval,
  6953.                            0
  6954.                            );
  6955.                     else
  6956.               s = NULL;
  6957.             if (!s) mgetx++;
  6958.             debug(F111,"ftp mget remote_files D",s,mgetx);
  6959.                 }
  6960.                 if (!s) {
  6961.             if (!first || mgetx >= mgetn) {
  6962.             done = 1;
  6963.             break;
  6964.             } else {
  6965.             continue;
  6966.             }
  6967.                 }
  6968.             }
  6969.         }
  6970.     debug(F111,"ftp mget remote_files E",s,0);
  6971.         /*
  6972.           The semantics of NLST are ill-defined.  Suppose we have just sent
  6973.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6974.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  6975.           RETR commands based on the pathless names are not going to work.
  6976.         */
  6977.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6978.             char * s3;
  6979.             if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  6980.                 int len, left = 4096;
  6981.                 char * tmp = xtmpbuf;
  6982.                 len = s3 - mgetlist[mgetx] + 1;
  6983.                 ckstrncpy(tmp,mgetlist[mgetx],left);
  6984.                 tmp += len;
  6985.                 left -= len;
  6986.                 ckstrncpy(tmp,s,left);
  6987.                 s = xtmpbuf;
  6988.         debug(F111,"ftp mget remote_files F",s,0);
  6989.             }
  6990.         }
  6991.         first = 0;
  6992.         skipthis = 0;                   /* File selection... */
  6993.         msg = "";
  6994.         nam = s;                        /* Filename (without path) */
  6995.         rc = 0;                         /* Initial return code */
  6996.         s2 = "";
  6997.  
  6998.         if (!getone && !skipthis) {     /* For MGET and MDELETE... */
  6999.             char c, * p = s;
  7000.             int srvpath = 0;
  7001.             int usrpath = 0;
  7002.             int i, k = 0;
  7003.  
  7004.         debug(F111,"ftp mget havetype",s,havetype);
  7005.         if (havetype > 0 && havetype != FTYP_FILE) {
  7006.         /* Server says it's not file... */
  7007.         debug(F110,"ftp mget not-a-file",s,0);
  7008.         continue;
  7009.         }
  7010. /*
  7011.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  7012.   But if the client did not ask for a recursive list, we have to ignore any
  7013.   server files that include a pathname that extends beyond any path that
  7014.   was included in the user's request.
  7015.  
  7016.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  7017.   get the user's path segment.  Then, for each incoming file, if it begins
  7018.   with the same path segment, we must strip it (point past it).
  7019. */
  7020.             src = mgetlist[mgetx];      /* In case it moved! */
  7021.         if (src) {
  7022.         for (i = 0; src[i]; i++) { /* Find rightmost path separator */
  7023.             if (ispathsep(src[i])) /* in user's pathname */
  7024.               k = i + 1;
  7025.         }
  7026.         }
  7027.             usrpath = k;                /* User path segment length */
  7028.             debug(F111,"ftp get usrpath",src,usrpath);
  7029.  
  7030.             p = s;                      /* Server filename */
  7031.             while ((c = *p++)) {        /* Look for path in server filename */
  7032.                 if (ispathsep(c)) {
  7033.             /* haspath++; */
  7034.                     nam = p;            /* Pathless name (for ckmatch) */
  7035.                     srvpath = p - s;    /* Server path segment length */
  7036.                 }
  7037.             }
  7038.             debug(F111,"ftp get srvpath",s,srvpath);
  7039.  
  7040.         if (usrpath == 0) {
  7041. /*
  7042.   Here we handle the case where the user said "mget foo" where foo is a
  7043.   directory name, and the server is sending back names like "foo/file1",
  7044.   "foo/file2", etc.  This is a nasty trick but it's necessary because the
  7045.   user can't compensate by typing "mget foo/" because then the server is
  7046.   likely to send back "foo//file1, foo//file2" etc, and we still won't
  7047.   get a match...
  7048. */
  7049.         int srclen = 0, srvlen = 0;
  7050.         if (src) srclen = strlen(src);
  7051.         if (s) srvlen = strlen(s);
  7052.         if (src && (srvlen > srclen)) {
  7053.             if (!strncmp(src,s,srclen) && ispathsep(s[srclen])) {
  7054.             char * tmpsrc = NULL;
  7055.             tmpsrc = (char *)malloc(srclen + 2);
  7056.             strncpy(tmpsrc,src,srclen);
  7057.             tmpsrc[srclen] = s[srclen];
  7058.             tmpsrc[srclen+1] = NUL;
  7059.             free(mgetlist[mgetx]);
  7060.             mgetlist[mgetx] = tmpsrc;
  7061.             tmpsrc = NULL;
  7062.             src = mgetlist[mgetx];
  7063.             usrpath = srclen+1;
  7064.             }                  
  7065.         }
  7066.         }
  7067. /*
  7068.   If as-name not given and server filename includes path that matches
  7069.   the pathname from the user's file specification, we must trim the common
  7070.   path prefix from the server's name when constructing the local name.
  7071. */
  7072.             if (src &&            /* Wed Sep 25 17:27:48 2002 */
  7073.         !asnambuf[0] &&
  7074.         !recursive &&        /* Thu Sep 19 16:11:59 2002 */
  7075.         (srvpath > 0) &&
  7076.         !strncmp(src,s,usrpath)) {
  7077.                 s2 = s + usrpath;       /* Local name skips past remote path */
  7078.             }
  7079. #ifdef COMMENT
  7080.         /* This doesn't work if the path prefix contains wildcards! */
  7081.         haspath = (srvpath > usrpath);
  7082. #else
  7083.         {                /* Count path segments instead */
  7084.         int x1 = 0, x2 = 0;
  7085.         char *p;
  7086.         for (p = s; *p; p++)
  7087.           if (ispathsep(*p)) x1++;
  7088.         for (p = src; *p; p++)
  7089.           if (ispathsep(*p)) x2++;
  7090.         haspath = recursive ? x1 || x2 : x1 > x2;
  7091.         debug(F111,"ftp get server path segments",s,x1);
  7092.         debug(F111,"ftp get user   path segments",src,x2);
  7093.         }
  7094.  
  7095. #endif /* COMMENT */
  7096.             debug(F111,"ftp get haspath",s+usrpath,haspath);
  7097.  
  7098.             if (haspath) {              /* Server file has path segments? */
  7099.                 if (!recursive) {       /* [M]GET /RECURSIVE? */
  7100. /*
  7101.   We did not ask for a recursive listing, but the server is sending us one
  7102.   anyway (as wu-ftpd is wont to do).  We get here if the current filename
  7103.   includes a path segment beyond any path segment we asked for in our
  7104.   non-recursive [M]GET command.  We MUST skip this file.
  7105. */
  7106.                     debug(F111,"ftp get skipping because of path",s,0);
  7107.                     continue;
  7108.                 }
  7109.             }
  7110.         } else if (getone && !skipthis) { /* GET (not MGET) */
  7111.             char * p = nam;
  7112.         while ((c = *p++)) {    /* Handle path in local name */
  7113.         if (ispathsep(c)) {
  7114.             if (recursive) {    /* If recursive, keep it */
  7115.             haspath = 1;
  7116.             break;
  7117.             } else {        /* Otherwise lose it. */
  7118.               nam = p;
  7119.             }
  7120.         }
  7121.             }
  7122.             s2 = nam;
  7123.         }
  7124.         if (!*nam)                      /* Name without path */
  7125.           nam = s;
  7126.  
  7127.         if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  7128.             if (nam[0] == '.')
  7129.           continue;
  7130.         }
  7131.         if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  7132.         int xx;
  7133.             for (i = 0; i < NSNDEXCEPT; i++) {
  7134.                 if (!rcvexcept[i]) {
  7135.                     break;
  7136.                 }
  7137.         xx = ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1);
  7138.         debug(F111,"ftp mget /except match",rcvexcept[i],xx);
  7139.                 if (xx) {
  7140.                     tlog(F100," refused: exception list","",0);
  7141.             msg = "Refused: Exception List";
  7142.                     skipthis++;
  7143.                     break;
  7144.                 }
  7145.             }
  7146.         }
  7147.         if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  7148.             if (ckmatch(
  7149. #ifdef CKREGEX
  7150.                         "*.~[0-9]*~"
  7151. #else
  7152.                         "*.~*~"
  7153. #endif /* CKREGEX */
  7154.                         ,nam,0,1) > 0)
  7155.               continue;
  7156.         }
  7157.         if (!x_xla) {                   /* If translation is off */
  7158.             x_csl = -2;                 /* unset the charsets */
  7159.             x_csr = -2;
  7160.         }
  7161.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  7162.         if (!*s2)                       /* Local name */
  7163.           s2 = asnambuf;                /* As-name */
  7164.  
  7165.     if (!*s2)            /* Sat Nov 16 19:19:39 2002 */
  7166.       s2 = recursive ? s : nam;    /* Fri Jan 10 13:15:19 2003 */
  7167.  
  7168.         debug(F110,"ftp get filnam  ",s,0);
  7169.         debug(F110,"ftp get asname A",s2,0);
  7170.  
  7171.         /* Receiving to real file */
  7172.         if (!pipesend &&
  7173. #ifdef PIPESEND
  7174.             !rcvfilter &&
  7175. #endif /* PIPESEND */
  7176.             !toscreen) {
  7177. #ifndef NOSPL
  7178.             /* Do this here so we can decide whether to skip */
  7179.             if (cmd_quoting && !skipthis && asnambuf[0]) {
  7180.                 int n; char *p;
  7181.                 n = TMPBUFSIZ;
  7182.                 p = tmpbuf;
  7183.                 zzstring(asnambuf,&p,&n);
  7184.                 s2 = tmpbuf;
  7185.                 debug(F111,"ftp get asname B",s2,updating);
  7186.             }
  7187. #endif /* NOSPL */
  7188.  
  7189.         local = *s2 ? s2 : s;
  7190.  
  7191.         if (!skipthis && x_fnc == XYFX_D) { /* File Collision = Discard */
  7192.         int x;
  7193.         x = zchki(local);
  7194.         debug(F111,"ftp get DISCARD zchki",local,x);
  7195.         if (x > -1) {
  7196.             skipthis++;
  7197.             debug(F110,"ftp get skip name",local,0);
  7198.             tlog(F100," refused: name","",0);
  7199.             msg = "Refused: Name";
  7200.         }
  7201.         }
  7202.  
  7203. #ifdef DOUPDATE
  7204.             if (!skipthis && updating) { /* If updating and not yet skipping */
  7205.                 if (zchki(local) > -1) {
  7206.                     x = chkmodtime(local,s,0);
  7207. #ifdef DEBUG
  7208.             if (deblog) {
  7209.             if (updating == 2)
  7210.               debug(F111,"ftp get /dates-diff chkmodtime",local,x);
  7211.             else
  7212.               debug(F111,"ftp get /update chkmodtime",local,x);
  7213.             }
  7214. #endif /* DEBUG */
  7215.             if ((updating == 1 && x > 0) ||  /* /UPDATE */
  7216.             (updating == 2 && x == 1)) { /* /DATES-DIFFER */
  7217.             skipthis++;
  7218.             tlog(F100," refused: date","",0);
  7219.             msg = "Refused: Date";
  7220.                         debug(F110,"ftp get skip date",local,0);
  7221.                     }
  7222.                 }
  7223.             }
  7224. #endif /* DOUPDATE */
  7225.         }
  7226.         /* Initialize file size to -1 in case server doesn't understand */
  7227.         /* SIZE command, so xxscreen() will know we don't know the size */
  7228.  
  7229.         fsize = -1L;
  7230.  
  7231.     /* Ask for size now only if we need it for selection */
  7232.     /* because if you're going thru a list 100,000 files to select */
  7233.     /* a small subset, 100,000 SIZE commands can take hours... */
  7234.  
  7235.     gotsize = 0;
  7236.         if (!mdel && !skipthis &&        /* Don't need size for DELE... */
  7237.         (getsmaller > -1L || getlarger > -1L)) {
  7238.         if (havesize > -1L) {    /* Already have file size? */
  7239.         fsize = havesize;
  7240.         gotsize = 1;
  7241.         } else {            /* No - must ask server */
  7242.         /*
  7243.           Prior to sending the NLST command we necessarily put the
  7244.           server into ASCII mode.  We must now put it back into the
  7245.           the requested mode so the upcoming SIZE command returns
  7246.           right kind of size; this is especially important for
  7247.           GET /RECOVER; otherwise the server returns the "ASCII" size
  7248.           of the file, rather than its true size.
  7249.         */
  7250.         changetype(ftp_typ,0);    /* Change to requested type */
  7251.         fsize = -1L;
  7252.         if (sizeok) {
  7253.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7254.             if (x == REPLY_COMPLETE) {
  7255.             fsize = atol(&ftp_reply_str[4]);
  7256.             gotsize = 1;
  7257.             }
  7258.         }
  7259.         }
  7260.             if (gotsize) {
  7261.                 if (getsmaller > -1L && fsize >= getsmaller)
  7262.                   skipthis++;
  7263.                 if (getlarger > -1L && fsize <= getlarger)
  7264.                   skipthis++;
  7265.                 if (skipthis) {
  7266.                     debug(F111,"ftp get skip size",s,fsize);
  7267.                     tlog(F100," refused: size","",0);
  7268.                     msg = "Refused: Size";
  7269.                 }
  7270. #ifdef COMMENT
  7271.             } else if (getone) {
  7272.                 /* SIZE can fail for many reasons.  Does the file exist? */
  7273.                 x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  7274.                 if (x != REPLY_COMPLETE) {
  7275.                     printf(">>> FILE NOT FOUND: %s\n",s);
  7276.                     break;
  7277.                 }
  7278. #endif /* COMMENT */
  7279.             }
  7280.         }
  7281.         if (skipthis) {                 /* Skipping this file? */
  7282.             ftscreen(SCR_FN,'F',0L,s);
  7283.             if (msg)
  7284.               ftscreen(SCR_ST,ST_ERR,0L,msg);
  7285.             else
  7286.               ftscreen(SCR_ST,ST_SKIP,0L,s);
  7287.             continue;
  7288.         }
  7289.         if (fp_nml) {                   /* /NAMELIST only - no transfer */
  7290.             fprintf(fp_nml,"%s\n",s);
  7291.             continue;
  7292.         }
  7293.     debug(F101,"XXX recursive","",recursive);
  7294.     debug(F101,"XXX haspath","",haspath);
  7295.     debug(F101,"XXX pipesend","",pipesend);
  7296.  
  7297.         if (recursive && haspath && !pipesend
  7298. #ifdef PIPESEND
  7299.             && !rcvfilter
  7300. #endif /* PIPESEND */
  7301.             ) {
  7302.         int x;
  7303.  
  7304. #ifdef NOMKDIR
  7305.         x = -1;
  7306. #else
  7307.             x = zmkdir(s);        /* Try to make the directory */
  7308. #endif /* NOMKDIR */
  7309.  
  7310.             if (x < 0) {
  7311.                 rc = -1;                /* Failure is fatal */
  7312.                 if (geterror) {
  7313.                     status = 0;
  7314.                     ftscreen(SCR_EM,0,0L,"Directory creation failure");
  7315.                     break;
  7316.                 }
  7317.             }
  7318.         }
  7319.  
  7320.         /* Not skipping */
  7321.  
  7322.     selected++;            /* Count this file as selected */
  7323.         pn = NULL;
  7324.  
  7325.     if (!gotsize && !mdel) {    /* Didn't get size yet */
  7326.         if (havesize > -1L) {    /* Already have file size? */
  7327.         fsize = havesize;
  7328.         gotsize = 1;
  7329.         } else {            /* No - must ask server */
  7330.         fsize = -1L;
  7331.         if (sizeok) {
  7332.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7333.             if (x == REPLY_COMPLETE) {
  7334.             fsize = atol(&ftp_reply_str[4]);
  7335.             gotsize = 1;
  7336.             }
  7337.         }
  7338.         }
  7339.     }
  7340.         if (mdel) {                     /* [M]DELETE */
  7341.             if (displa && !ftp_vbm)
  7342.               printf(" %s...",s);
  7343.             rc =
  7344.              (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  7345.             if (rc > -1) {
  7346.                 tlog(F110,"ftp mdelete",s,0);
  7347.                 if (displa && !ftp_vbm)
  7348.                   printf("OK\n");
  7349.             } else {
  7350.                 tlog(F110,"ftp mdelete failed:",s,0);
  7351.                 if (displa)
  7352.                   printf("Failed\n");
  7353.             }
  7354. #ifndef NOSPL
  7355. #ifdef PIPESEND
  7356.         } else if (rcvfilter) {         /* [M]GET with filter */
  7357.             int n; char * p;
  7358.             n = CKMAXPATH;
  7359.             p = tmpbuf;                 /* Safe - no asname with filter */
  7360.             zzstring(rcvfilter,&p,&n);
  7361.             if (n > -1)
  7362.               pn = tmpbuf;
  7363.             debug(F111,"ftp get rcvfilter",pn,n);
  7364. #endif /* PIPESEND */
  7365. #endif /* NOSPL */
  7366.             if (toscreen) s2 = "-";
  7367.         } else if (pipesend) {          /* [M]GET /COMMAND */
  7368.             int n; char * p;
  7369.             n = CKMAXPATH;
  7370.             p = tmpbuf;                 /* Safe - no asname with filter */
  7371.             zzstring(pipename,&p,&n);
  7372.             if (n > -1)
  7373.               pn = tmpbuf;
  7374.             debug(F111,"ftp get pipename",pipename,n);
  7375.             if (toscreen) s2 = "-";
  7376.         } else {                        /* [M]GET with no pipes or filters */
  7377.             debug(F111,"ftp get s2 A",s2,x_cnv);
  7378.             if (toscreen) {
  7379.                 s2 = "-";               /* (hokey convention for stdout) */
  7380.             } else if (!*s2) {          /* No asname? */
  7381.                 if (x_cnv) {            /* If converting */
  7382.                     nzrtol(s,tmpbuf,x_cnv,1,CKMAXPATH); /* convert */
  7383.                     s2 = tmpbuf;
  7384.                     debug(F110,"ftp get nzrtol",s2,0);
  7385.                 } else                  /* otherwise */
  7386.                   s2 = s;               /* use incoming file's name */
  7387.             }
  7388.             debug(F110,"ftp get s2 B",s2,0);
  7389.  
  7390.             /* If local file already exists, take collision action */
  7391.  
  7392.             if (!pipesend &&
  7393. #ifdef PIPESEND
  7394.                 !rcvfilter &&
  7395. #endif /* PIPESEND */
  7396.                 !toscreen) {
  7397.                 x = zchki(s2);
  7398.                 debug(F111,"ftp get zchki",s2,x);
  7399.                 debug(F111,"ftp get x_fnc",s2,x_fnc);
  7400.  
  7401.                 if (x > -1 && !restart) {
  7402.             int x = -1;
  7403.             char * newname = NULL;
  7404.  
  7405.                     switch (x_fnc) {
  7406.                       case XYFX_A:      /* Append */
  7407.                         append = 1;
  7408.                         break;
  7409.                       case XYFX_R:      /* Rename */
  7410.                       case XYFX_B:    /* Backup */
  7411.             znewn(s2,&newname); /* Make unique name */
  7412.             debug(F110,"ftp get znewn",newname,0);
  7413.             if (x_fnc == XYFX_B) { /* Backup existing file */
  7414.                 x = zrename(s2,newname);
  7415.                 debug(F111,"ftp get backup zrename",newname,x);
  7416.             } else {      /* Rename incoming file */
  7417.                 x = ckstrncpy(tmpbuf,newname,CKMAXPATH+1);
  7418.                 s2 = tmpbuf;
  7419.                 debug(F111,"ftp get rename incoming",newname,x);
  7420.             }
  7421.             if (x < 0) {
  7422.                 ftscreen(SCR_EM,0,0L,"Backup/Rename failed");
  7423.                 x = 0;
  7424.                 goto xgetx;
  7425.             }
  7426.             break;
  7427.                       case XYFX_D:      /* Discard (already handled above) */
  7428.                       case XYFX_U:      /* Update (ditto) */
  7429.                       case XYFX_M:      /* Update (ditto) */
  7430.                       case XYFX_X:      /* Overwrite */
  7431.                         break;
  7432.                     }
  7433.                 }
  7434.             }
  7435.         }
  7436.         if (!mdel) {
  7437. #ifdef PIPESEND
  7438.             debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  7439. #endif /* PIPESEND */
  7440.             if (pipesend && !toscreen)
  7441.               s2 = NULL;
  7442. #ifdef DEBUG
  7443.             if (deblog) {
  7444.                 debug(F101,"ftp get x_xla","",x_xla);
  7445.                 debug(F101,"ftp get x_csl","",x_csl);
  7446.                 debug(F101,"ftp get x_csr","",x_csr);
  7447.                 debug(F101,"ftp get append","",append);
  7448.             }
  7449. #endif /* DEBUG */
  7450.  
  7451.             rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  7452.  
  7453. #ifdef DEBUG
  7454.             if (deblog) {
  7455.                 debug(F111,"ftp get rc",s,rc);
  7456.                 debug(F111,"ftp get cancelfile",s,cancelfile);
  7457.                 debug(F111,"ftp get cancelgroup",s,cancelgroup);
  7458.                 debug(F111,"ftp get renaming",s,renaming);
  7459.             }
  7460. #endif /* DEBUG */
  7461.         }
  7462.         if (rc > -1) {
  7463.             good++;
  7464.             status = 1;
  7465.             if (!cancelfile) {
  7466.                 if (deleting) {         /* GET /DELETE (source file) */
  7467.                     rc =
  7468.                       (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  7469.                         ? 1 : -1;
  7470.                     tlog(F110, (rc > -1) ?
  7471.                          " deleted" : " failed to delete", s, 0);
  7472.                 } else if (renaming && rcv_rename && !toscreen) {
  7473.                     char *p;            /* Rename downloaded file */
  7474. #ifndef NOSPL
  7475.                     char tmpbuf[CKMAXPATH+1];
  7476.                     int n;
  7477.                     n = CKMAXPATH;
  7478.                     p = tmpbuf;
  7479.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7480.                     zzstring(rcv_rename,&p,&n);
  7481.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7482.                     p = tmpbuf;
  7483. #else
  7484.                     p = rcv_rename;
  7485. #endif /* NOSPL */
  7486.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7487.                     debug(F111,"doftpget /RENAME zrename",p,rc);
  7488.                     tlog(F110, (rc > -1) ?
  7489.                          " renamed to" :
  7490.                          " failed to rename to",
  7491.                          p,
  7492.                          0
  7493.                          );
  7494.                 } else if (moving && rcv_move && !toscreen) {
  7495.                     char *p;            /* Move downloaded file */
  7496. #ifndef NOSPL
  7497.                     char tmpbuf[CKMAXPATH+1];
  7498.                     int n;
  7499.                     n = TMPBUFSIZ;
  7500.                     p = tmpbuf;
  7501.                     debug(F111,"ftp get /move-to",rcv_move,0);
  7502.                     zzstring(rcv_move,&p,&n);
  7503.                     p = tmpbuf;
  7504. #else
  7505.                     p = rcv_move;
  7506. #endif /* NOSPL */
  7507.                     debug(F111,"ftp get /move-to",p,0);
  7508.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7509.                     debug(F111,"doftpget /MOVE zrename",p,rc);
  7510.                     tlog(F110, (rc > -1) ?
  7511.                          " moved to" : " failed to move to", p, 0);
  7512.                 }
  7513.                 if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  7514.                     char * s = pv[SND_SRN].sval;
  7515.                     char * srvrn = pv[SND_SRN].sval;
  7516.                     char tmpbuf[CKMAXPATH+1];
  7517. #ifndef NOSPL
  7518.                     int y;              /* Pass it thru the evaluator */
  7519.                     extern int cmd_quoting; /* for \v(filename) */
  7520.                     debug(F111,"ftp get srv_renam",s,1);
  7521.  
  7522.                     if (cmd_quoting) {
  7523.                         y = CKMAXPATH;
  7524.                         s = (char *)tmpbuf;
  7525.                         zzstring(srvrn,&s,&y);
  7526.                         s = (char *)tmpbuf;
  7527.                     }
  7528. #endif /* NOSPL */
  7529.                     debug(F111,"ftp get srv_renam",s,1);
  7530.                     if (s) if (*s) {
  7531.                         int x;
  7532.                         x = ftp_rename(s2,s);
  7533.                         debug(F111,"ftp get ftp_rename",s2,x);
  7534.                         tlog(F110, (x > 0) ?
  7535.                              " renamed source file to" :
  7536.                              " failed to rename source file to",
  7537.                              s,
  7538.                              0
  7539.                              );
  7540.                         if (x < 1)
  7541.               return(-1);
  7542.                     }
  7543.                 }
  7544.             }
  7545.         }
  7546.         if (cancelfile)
  7547.           continue;
  7548.         if (rc < 0) {
  7549.             ftp_fai++;
  7550.             if (geterror) {
  7551.                 status = 0;
  7552.                 ftscreen(SCR_EM,0,0L,"Fatal download error");
  7553.                 done++;
  7554.             }
  7555.         }
  7556.     }
  7557. #ifdef DEBUG
  7558.     if (deblog) {
  7559.     debug(F101,"ftp get status","",status);
  7560.     debug(F101,"ftp get cancelgroup","",cancelgroup);
  7561.     debug(F101,"ftp get cancelfile","",cancelfile);
  7562.     debug(F101,"ftp get selected","",selected);
  7563.     debug(F101,"ftp get good","",good);
  7564.     }
  7565. #endif /* DEBUG */
  7566.  
  7567.     if (selected == 0) {        /* No files met selection criteria */
  7568.     status = 1;            /* which is a kind of success. */
  7569.     } else if (status > 0) {        /* Some files were selected */
  7570.         if (cancelgroup)        /* but MGET was canceled */
  7571.           status = 0;            /* so MGET failed */
  7572.         else if (cancelfile && good < 1) /* If file was canceled */
  7573.           status = 0;            /* MGET failed if it got no files */
  7574.     }
  7575.     success = status;
  7576.     x = success;
  7577.     debug(F101,"ftp get success","",success);
  7578.  
  7579.   xgetx:
  7580.     pipesend = pipesave;                /* Restore global pipe selection */
  7581.     if (fp_nml) {                       /* Close /NAMELIST */
  7582.         if (fp_nml != stdout)
  7583.           fclose(fp_nml);
  7584.         fp_nml = NULL;
  7585.     }
  7586.     if (x > -1) {                       /* Download successful */
  7587. #ifdef GFTIMER
  7588.         t1 = gmstimer();                /* End time */
  7589.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  7590.         if (!sec) sec = 0.001;
  7591.         fptsecs = sec;
  7592. #else
  7593.         sec = (t1 - t0) / 1000;
  7594.         if (!sec) sec = 1;
  7595. #endif /* GFTIMER */
  7596.         tfcps = (long) (tfc / sec);
  7597.         tsecs = (int)sec;
  7598.         lastxfer = W_FTP|W_RECV;
  7599.         xferstat = success;
  7600.     }
  7601.     if (dpyactive)
  7602.       ftscreen(SCR_TC,0,0L,"");
  7603. #ifdef CK_TMPDIR
  7604.     if (f_tmpdir) {                     /* If we changed to download dir */
  7605.         zchdir((char *) savdir);        /* Go back where we came from */
  7606.         f_tmpdir = 0;
  7607.     }
  7608. #endif /* CK_TMPDIR */
  7609.  
  7610.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  7611.         if (pv[i].sval)
  7612.           free(pv[i].sval);
  7613.     }
  7614.     for (i = 0; i < mgetn; i++)         /* MGET list too */
  7615.       makestr(&(mgetlist[i]),NULL);
  7616.  
  7617.     if (cancelgroup)            /* Clear temp-file stack */
  7618.       mlsreset();
  7619.  
  7620.     ftreset();                          /* Undo switch effects */
  7621.     dpyactive = 0;
  7622.     return(x);
  7623. }
  7624.  
  7625. static struct keytab ftprmt[] = {
  7626.     { "cd",        XZCWD, 0 },
  7627.     { "cdup",      XZCDU, 0 },
  7628.     { "cwd",       XZCWD, CM_INV },
  7629.     { "delete",    XZDEL, 0 },
  7630.     { "directory", XZDIR, 0 },
  7631.     { "exit",      XZXIT, 0 },
  7632.     { "help",      XZHLP, 0 },
  7633.     { "login",     XZLGI, 0 },
  7634.     { "logout",    XZLGO, 0 },
  7635.     { "mkdir",     XZMKD, 0 },
  7636.     { "pwd",       XZPWD, 0 },
  7637.     { "rename",    XZREN, 0 },
  7638.     { "rmdir",     XZRMD, 0 },
  7639.     { "type",      XZTYP, 0 },
  7640.     { "", 0, 0 }
  7641. };
  7642. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  7643.  
  7644. int
  7645. doftpsite() {                /* Send a SITE command */
  7646.     int reply;
  7647.     char * s;
  7648.     int lcs = -1, rcs = -1;
  7649. #ifndef NOCSETS
  7650.     if (ftp_xla) {
  7651.         lcs = ftp_csl;
  7652.         if (lcs < 0) lcs = fcharset;
  7653.         rcs = ftp_csx;
  7654.         if (rcs < 0) rcs = ftp_csr;
  7655.     }
  7656. #endif /* NOCSETS */
  7657.     if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7658.       return(x);
  7659.     CHECKCONN();
  7660.     ckstrncpy(line,s,LINBUFSIZ);
  7661.     if (testing) printf(" ftp site \"%s\"...\n",line);
  7662.     if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  7663.     do {
  7664.         reply = getreply(0,lcs,rcs,ftp_vbm,0);
  7665.     } while (reply == REPLY_PRELIM);
  7666.     }
  7667.     return(success = (reply == REPLY_COMPLETE));
  7668. }
  7669.  
  7670.  
  7671. int
  7672. dosetftppsv() {                /* Passive mode */
  7673.     x = seton(&ftp_psv);
  7674.     if (x > 0) passivemode = ftp_psv;
  7675.     return(x);
  7676. }
  7677.  
  7678. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  7679.  
  7680. int
  7681. doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  7682.     /* cx == 0 means REMOTE */
  7683.     /* cx != 0 is a XZxxx value */
  7684.     char * s;
  7685.  
  7686.     if (who != 0)
  7687.       return(0);
  7688.  
  7689.     if (cx == 0) {
  7690.         if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  7691.           return(x);
  7692.         cx = x;
  7693.     }
  7694.     switch (cx) {
  7695.       case XZCDU:                       /* CDUP */
  7696.         if ((x = cmcfm()) < 0) return(x);
  7697.         return(doftpcdup());
  7698.  
  7699.       case XZCWD:                       /* RCD */
  7700.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7701.           return(x);
  7702.         ckstrncpy(line,s,LINBUFSIZ);
  7703.         return(doftpcwd((char *)line,1));
  7704.       case XZPWD:                       /* RPWD */
  7705.         return(doftppwd());
  7706.       case XZDEL:                       /* RDEL */
  7707.         return(doftpget(FTP_MDE,1));
  7708.       case XZDIR:                       /* RDIR */
  7709.         return(doftpdir(FTP_DIR));
  7710.       case XZHLP:                       /* RHELP */
  7711.         return(doftpxhlp());
  7712.       case XZMKD:                       /* RMKDIR */
  7713.         return(doftpmkd());
  7714.       case XZREN:                       /* RRENAME */
  7715.         return(doftpren());
  7716.       case XZRMD:                       /* RRMDIR */
  7717.         return(doftprmd());
  7718.       case XZLGO:                       /* LOGOUT */
  7719.         return(doftpres());
  7720.       case XZXIT:                       /* EXIT */
  7721.         return(ftpbye());
  7722.     }
  7723.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  7724.     return(-9);
  7725. }
  7726.  
  7727. int
  7728. doxftp() {                              /* Command parser for built-in FTP */
  7729.     int cx, n;
  7730.     struct FDB kw, fl;
  7731.     char * s;
  7732.     int usetls = 0;
  7733.     int lcs = -1, rcs = -1;
  7734.  
  7735. #ifndef NOCSETS
  7736.     if (ftp_xla) {
  7737.         lcs = ftp_csl;
  7738.         if (lcs < 0) lcs = fcharset;
  7739.         rcs = ftp_csx;
  7740.         if (rcs < 0) rcs = ftp_csr;
  7741.     }
  7742. #endif /* NOCSETS */
  7743.  
  7744.     if (inserver)                       /* FTP not allowed in IKSD. */
  7745.       return(-2);
  7746.  
  7747.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  7748.         ftp_typ = g_ftp_typ;
  7749.         /* g_ftp_typ = -1; */
  7750.     }
  7751. #ifdef COMMENT
  7752. /*
  7753.   We'll set the collision action locally in doftpget() based on whether
  7754.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  7755. */
  7756.     if (ftp_fnc < 0)                    /* Inherit global collision action */
  7757.       ftp_fnc = fncact;                 /* if none specified for FTP */
  7758. #endif /* COMMENT */
  7759.  
  7760.     /* Restore global verbose mode */
  7761.     if (ftp_deb)
  7762.       ftp_vbm = 1;
  7763.     else if (quiet)
  7764.       ftp_vbm = 0;
  7765.     else
  7766.       ftp_vbm = ftp_vbx;
  7767.  
  7768.     ftp_dates &= 1;            /* Undo any previous /UPDATE switch */
  7769.  
  7770.     dpyactive = 0;                      /* Reset global transfer-active flag */
  7771.     printlines = 0;                     /* Reset printlines */
  7772.  
  7773.     if (fp_nml) {                       /* Reset /NAMELIST */
  7774.         if (fp_nml != stdout)
  7775.           fclose(fp_nml);
  7776.         fp_nml = NULL;
  7777.     }
  7778.     makestr(&ftp_nml,NULL);
  7779.  
  7780.     cmfdbi(&kw,                         /* First FDB - commands */
  7781.            _CMKEY,                      /* fcode */
  7782.            "Hostname; or FTP command",  /* help */
  7783.            "",                          /* default */
  7784.            "",                          /* addtl string data */
  7785.            nftpcmd,                     /* addtl numeric data 1: tbl size */
  7786.            0,                           /* addtl numeric data 2: none */
  7787.            xxstring,                    /* Processing function */
  7788.            ftpcmdtab,                   /* Keyword table */
  7789.            &fl                          /* Pointer to next FDB */
  7790.            );
  7791.     cmfdbi(&fl,                         /* A host name or address */
  7792.            _CMFLD,                      /* fcode */
  7793.            "Hostname or address",       /* help */
  7794.            "",                          /* default */
  7795.            "",                          /* addtl string data */
  7796.            0,                           /* addtl numeric data 1 */
  7797.            0,                           /* addtl numeric data 2 */
  7798.            xxstring,
  7799.            NULL,
  7800.            NULL
  7801.            );
  7802.     x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
  7803.     if (x == -3) {
  7804.         printf("?ftp what? \"help ftp\" for hints\n");
  7805.         return(-9);
  7806.     }
  7807.     if (x < 0)
  7808.       return(x);
  7809.     if (cmresult.fcode == _CMFLD) {     /* If hostname */
  7810.         return(openftp(cmresult.sresult,0)); /* go open the connection */
  7811.     } else {
  7812.         cx = cmresult.nresult;
  7813.     }
  7814.     switch (cx) {
  7815.       case FTP_ACC:                     /* ACCOUNT */
  7816.         if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  7817.           return(x);
  7818.         CHECKCONN();
  7819.         makestr(&ftp_acc,s);
  7820.         if (testing)
  7821.           printf(" ftp account: \"%s\"\n",ftp_acc);
  7822.         success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  7823.         return(success);
  7824.  
  7825.       case FTP_GUP:                     /* Go UP */
  7826.         if ((x = cmcfm()) < 0) return(x);
  7827.         CHECKCONN();
  7828.         if (testing) printf(" ftp cd: \"(up)\"\n");
  7829.         return(success = doftpcdup());
  7830.  
  7831.       case FTP_CWD:                     /* CD */
  7832.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7833.           return(x);
  7834.         CHECKCONN();
  7835.         ckstrncpy(line,s,LINBUFSIZ);
  7836.         if (testing)
  7837.           printf(" ftp cd: \"%s\"\n", line);
  7838.         return(success = doftpcwd(line,1));
  7839.  
  7840.       case FTP_CHM:                     /* CHMOD */
  7841.         if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  7842.           return(x);
  7843.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7844.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7845.           return(x);
  7846.         CHECKCONN();
  7847.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  7848.         if (testing)
  7849.           printf(" ftp chmod: %s\n",ftpcmdbuf);
  7850.         success =
  7851.           (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7852.         return(success);
  7853.  
  7854.       case FTP_CLS:                     /* CLOSE FTP connection */
  7855.         if ((y = cmcfm()) < 0)
  7856.           return(y);
  7857.         CHECKCONN();
  7858.         if (testing)
  7859.           printf(" ftp closing...\n");
  7860.         ftpclose();
  7861.         return(success = 1);
  7862.  
  7863.       case FTP_DIR:                     /* DIRECTORY of remote files */
  7864.       case FTP_VDI:
  7865.         return(doftpdir(cx));
  7866.  
  7867.       case FTP_GET:                     /* GET a remote file */
  7868.       case FTP_RGE:                     /* REGET */
  7869.       case FTP_MGE:                     /* MGET */
  7870.       case FTP_MDE:                     /* MDELETE */
  7871.         return(doftpget(cx,1));
  7872.  
  7873.       case FTP_IDL:                     /* IDLE */
  7874.         if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  7875.           return(x);
  7876.         if ((y = cmcfm()) < 0)
  7877.           return(y);
  7878.         CHECKCONN();
  7879.         if (z < 0)  {                   /* Display idle timeout */
  7880.             if (testing)
  7881.               printf(" ftp query idle timeout...\n");
  7882.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  7883.         } else {                        /* Set idle timeout */
  7884.             if (testing)
  7885.               printf(" ftp idle timeout set: %d...\n",z);
  7886.             success =
  7887.               (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  7888.         }
  7889.         return(success);
  7890.  
  7891.       case FTP_MKD:                     /* MKDIR */
  7892.         return(doftpmkd());
  7893.  
  7894.       case FTP_MOD:                     /* MODTIME */
  7895.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7896.           return(x);
  7897.         CHECKCONN();
  7898.         ckstrncpy(line,s,LINBUFSIZ);
  7899.         if (testing)
  7900.           printf(" ftp modtime \"%s\"...\n",line);
  7901.         success = 0;
  7902.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  7903.         success = 1;
  7904.         mdtmok = 1;
  7905.         if (!quiet) {
  7906.         int flag = 0;
  7907.         char c, * s;
  7908.         struct tm tmremote;
  7909.  
  7910.         bzero((char *)&tmremote, sizeof(struct tm));
  7911.         s = ftp_reply_str;
  7912.         while ((c = *s++)) {
  7913.             if (c == SP) {
  7914.             flag++;
  7915.             break;
  7916.             }
  7917.         }
  7918.         if (flag) {
  7919.             if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  7920.                    &tmremote.tm_year,
  7921.                    &tmremote.tm_mon,
  7922.                    &tmremote.tm_mday,
  7923.                    &tmremote.tm_hour,
  7924.                    &tmremote.tm_min,
  7925.                    &tmremote.tm_sec
  7926.                    ) == 6) {
  7927.             printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  7928.                    line,
  7929.                    tmremote.tm_year,
  7930.                    tmremote.tm_mon,
  7931.                    tmremote.tm_mday,
  7932.                    tmremote.tm_hour,
  7933.                    tmremote.tm_min,
  7934.                    tmremote.tm_sec
  7935.                    );
  7936.             } else {
  7937.             success = 0;
  7938.             }
  7939.         }
  7940.         }
  7941.         }
  7942.         return(success);
  7943.  
  7944.       case FTP_OPN:                     /* OPEN connection */
  7945. #ifdef COMMENT
  7946.         x = cmfld("IP hostname or address","",&s,xxstring);
  7947.         if (x < 0) {
  7948.             success = 0;
  7949.             return(x);
  7950.         }
  7951.         ckstrncpy(line,s,LINBUFSIZ);
  7952.         s = line;
  7953.         return(openftp(s,0));
  7954. #else
  7955.         {                               /* OPEN connection */
  7956.             char name[TTNAMLEN+1], *p;
  7957.             extern int network;
  7958.             extern char ttname[];
  7959.             if (network)                /* If we have a current connection */
  7960.               ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  7961.             else
  7962.               *name = '\0';             /* as default host */
  7963.             for (p = name; *p; p++)     /* Remove ":service" from end. */
  7964.               if (*p == ':') { *p = '\0'; break; }
  7965. #ifndef USETLSTAB
  7966.             x = cmfld("IP hostname or address",name,&s,xxstring);
  7967. #else
  7968.             cmfdbi(&kw,                 /* First FDB - commands */
  7969.                    _CMKEY,              /* fcode */
  7970.                    "Hostname or switch", /* help */
  7971.                    "",                  /* default */
  7972.                    "",                  /* addtl string data */
  7973.                    ntlstab,             /* addtl numeric data 1: tbl size */
  7974.                    0,                   /* addtl numeric data 2: none */
  7975.                    xxstring,            /* Processing function */
  7976.                    tlstab,              /* Keyword table */
  7977.                    &fl                  /* Pointer to next FDB */
  7978.                    );
  7979.             cmfdbi(&fl,                 /* A host name or address */
  7980.                    _CMFLD,              /* fcode */
  7981.                    "Hostname or address", /* help */
  7982.                    "",                  /* default */
  7983.                    "",                  /* addtl string data */
  7984.                    0,                   /* addtl numeric data 1 */
  7985.                    0,                   /* addtl numeric data 2 */
  7986.                    xxstring,
  7987.                    NULL,
  7988.                    NULL
  7989.                    );
  7990.  
  7991.             for (n = 0;; n++) {
  7992.                 x = cmfdb(&kw);         /* Parse a hostname or a keyword */
  7993.                 if (x == -3) {
  7994.                   printf("?ftp open what? \"help ftp\" for hints\n");
  7995.                   return(-9);
  7996.                 }
  7997.                 if (x < 0)
  7998.                   break;
  7999.                 if (cmresult.fcode == _CMFLD) { /* Hostname */
  8000.                     s = cmresult.sresult;
  8001.                     break;
  8002.                 } else if (cmresult.nresult == OPN_TLS) {
  8003.                     usetls = 1;
  8004.                 }
  8005.             }
  8006. #endif /* USETLSTAB */
  8007.             if (x < 0) {
  8008.                 success = 0;
  8009.                 return(x);
  8010.             }
  8011.             ckstrncpy(line,s,LINBUFSIZ);
  8012.             s = line;
  8013.             return(openftp(s,usetls));
  8014.         }
  8015. #endif /* COMMENT */
  8016.  
  8017.       case FTP_PUT:                     /* PUT */
  8018.       case FTP_MPU:                     /* MPUT */
  8019.       case FTP_APP:                     /* APPEND */
  8020.         return(doftpput(cx,1));
  8021.  
  8022.       case FTP_PWD:                     /* PWD */
  8023.         x = doftppwd();
  8024.         if (x > -1) success = x;
  8025.         return(x);
  8026.  
  8027.       case FTP_REN:                     /* RENAME */
  8028.         return(doftpren());
  8029.  
  8030.       case FTP_RES:                     /* RESET */
  8031.         return(doftpres());
  8032.  
  8033.       case FTP_HLP:                     /* (remote) HELP */
  8034.         return(doftpxhlp());
  8035.  
  8036.       case FTP_RMD:                     /* RMDIR */
  8037.         return(doftprmd());
  8038.  
  8039.       case FTP_STA:                     /* STATUS */
  8040.         if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  8041.           return(x);
  8042.         CHECKCONN();
  8043.         ckstrncpy(line,s,LINBUFSIZ);
  8044.         if (testing) printf(" ftp status \"%s\"...\n",line);
  8045.         success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  8046.         return(success);
  8047.  
  8048.       case FTP_SIT: {                   /* SITE */
  8049.       return(doftpsite());
  8050.       }
  8051.  
  8052.       case FTP_SIZ:                     /* (ask for) SIZE */
  8053.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8054.           return(x);
  8055.         CHECKCONN();
  8056.         ckstrncpy(line,s,LINBUFSIZ);
  8057.         if (testing)
  8058.           printf(" ftp size \"%s\"...\n",line);
  8059.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  8060.     if (success)
  8061.       sizeok = 1;
  8062.         return(success);
  8063.  
  8064.       case FTP_SYS:                     /* Ask for server's SYSTEM type */
  8065.         if ((x = cmcfm()) < 0) return(x);
  8066.         CHECKCONN();
  8067.         if (testing)
  8068.           printf(" ftp system...\n");
  8069.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  8070.         return(success);
  8071.  
  8072.       case FTP_UMA:                     /* Set/query UMASK */
  8073.         if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  8074.           if (x != -3)
  8075.             return(x);
  8076.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8077.         if ((x = cmcfm()) < 0) return(x);
  8078.         CHECKCONN();
  8079.         if (testing) {
  8080.             if (tmpbuf[0])
  8081.               printf(" ftp umask \"%s\"...\n",tmpbuf);
  8082.             else
  8083.               printf(" ftp query umask...\n");
  8084.         }
  8085.         success = ftp_umask(tmpbuf);
  8086.         return(success);
  8087.  
  8088.       case FTP_USR:
  8089.         return(doftpusr());
  8090.  
  8091.       case FTP_QUO:
  8092.         if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  8093.           return(x);
  8094.         CHECKCONN();
  8095.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  8096.         return(success);
  8097.  
  8098.       case FTP_TYP:                     /* Type */
  8099.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  8100.           return(x);
  8101.         if ((y = cmcfm()) < 0) return(y);
  8102.         CHECKCONN();
  8103.         ftp_typ = x;
  8104.         g_ftp_typ = x;
  8105.         tenex = (ftp_typ == FTT_TEN);
  8106.         changetype(ftp_typ,ftp_vbm);
  8107.         return(1);
  8108.  
  8109.       case FTP_CHK:                     /* Check if remote file(s) exist(s) */
  8110.         if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  8111.           return(x);
  8112.         CHECKCONN();
  8113.         success = remote_files(1,(CHAR *)s,NULL,0) ? 1 : 0;
  8114.         return(success);
  8115.  
  8116.       case FTP_FEA:                     /* RFC2389 */
  8117.         if ((y = cmcfm()) < 0)
  8118.           return(y);
  8119.         CHECKCONN();
  8120.     success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE);
  8121.     if (success) {
  8122.         if (sfttab[0] > 0) {
  8123.         ftp_aut = sfttab[SFT_AUTH];
  8124.         sizeok  = sfttab[SFT_SIZE];
  8125.         mdtmok  = sfttab[SFT_MDTM];
  8126.         mlstok  = sfttab[SFT_MLST];
  8127.         }
  8128.     }
  8129.     return(success);
  8130.  
  8131.       case FTP_OPT:                     /* RFC2389 */
  8132.         /* Perhaps this should be a keyword list... */
  8133.         if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  8134.           return(x);
  8135.         CHECKCONN();
  8136.         ckstrncpy(line,s,LINBUFSIZ);
  8137.         if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  8138.           return(x);
  8139.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8140.         return(success);
  8141.  
  8142.       case FTP_ENA:            /* FTP ENABLE */
  8143.       case FTP_DIS:            /* FTP DISABLE */
  8144.         if ((x = cmkey(ftpenatab,nftpena,"","",xxstring)) < 0)
  8145.           return(x);
  8146.         if ((y = cmcfm()) < 0) return(y);
  8147.     switch (x) {
  8148.       case ENA_AUTH:        /* OK to use autoauthentication */
  8149.         ftp_aut = (cx == FTP_ENA) ? 1 : 0;
  8150.         sfttab[SFT_AUTH] = ftp_aut;
  8151.         break;
  8152.       case ENA_FEAT:        /* OK to send FEAT command */
  8153.         featok = (cx == FTP_ENA) ? 1 : 0;
  8154.         break;
  8155.       case ENA_MLST:        /* OK to use MLST/MLSD */
  8156.         mlstok = (cx == FTP_ENA) ? 1 : 0;
  8157.         sfttab[SFT_MLST] = mlstok;
  8158.         break;
  8159.       case ENA_MDTM:        /* OK to use MDTM */
  8160.         mdtmok = (cx == FTP_ENA) ? 1 : 0;
  8161.         sfttab[SFT_MDTM] = mdtmok;
  8162.         break;
  8163.       case ENA_SIZE:        /* OK to use SIZE */
  8164.         sizeok = (cx == FTP_ENA) ? 1 : 0;
  8165.         sfttab[SFT_SIZE] = sizeok;
  8166.         break;
  8167.     }
  8168.     return(success = 1);
  8169.     }
  8170.     return(-2);
  8171. }
  8172.  
  8173. #ifndef NOSHOW
  8174. static char *
  8175. shopl(x) int x; {
  8176.     switch (x) {
  8177.       case FPL_CLR: return("clear");
  8178.       case FPL_PRV: return("private");
  8179.       case FPL_SAF: return("safe");
  8180.       case 0:  return("(not set)");
  8181.       default: return("(unknown)");
  8182.     }
  8183. }
  8184.  
  8185. int
  8186. shoftp(brief) {
  8187.     char * s = "?";
  8188.     int n, x;
  8189.  
  8190.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  8191.         ftp_typ = g_ftp_typ;
  8192.         /* g_ftp_typ = -1; */
  8193.     }
  8194.     printf("\n");
  8195.     printf("FTP connection:                 %s\n",connected ?
  8196.            ftp_host :
  8197.            "(none)"
  8198.            );
  8199.     n = 2;
  8200.     if (connected) {
  8201.         n++;
  8202.         printf("FTP server type:                %s\n",
  8203.                ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  8204.     }
  8205.     if (loggedin)
  8206.       printf("Logged in as:                   %s\n",
  8207.              strval(ftp_logname,"(unknown)"));
  8208.     else
  8209.       printf("Not logged in\n");
  8210.     n++;
  8211.     if (brief) return(0);
  8212.  
  8213.     printf("\nSET FTP values:\n\n");
  8214.     n += 3;
  8215.  
  8216.     printf(" ftp anonymous-password:        %s\n",
  8217.        ftp_apw ? ftp_apw : "(default)"
  8218.        );
  8219.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  8220.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  8221.     switch (ftp_typ) {
  8222.       case FTT_ASC: s = "text"; break;
  8223.       case FTT_BIN: s = "binary"; break;
  8224.       case FTT_TEN: s = "tenex"; break;
  8225.     }
  8226.     printf(" ftp type:                      %s\n",s);
  8227.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  8228.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  8229.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  8230.     printf(" ftp filenames:                 %s\n",
  8231.            ftp_cnv == CNV_AUTO ? "auto" : (ftp_cnv ? "converted" : "literal")
  8232.            );
  8233.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  8234.  
  8235.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  8236.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  8237.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  8238.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  8239.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  8240. #ifdef COMMENT
  8241.     /* See note in doxftp() */
  8242.     if (ftp_fnc < 0)
  8243.       ftp_fnc = fncact;
  8244. #endif /* COMMENT */
  8245.     printf(" ftp collision:                 %s\n",
  8246.        fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  8247.     printf(" ftp server-time-offset:        %s\n",
  8248.        fts_sto ? fts_sto : "(none)");
  8249.     n += 15;
  8250.  
  8251. #ifndef NOCSETS
  8252.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  8253.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8254.  
  8255.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  8256.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8257.  
  8258.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  8259.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8260. #endif /* NOCSETS */
  8261.  
  8262.     x = ftp_dis;
  8263.     if (x < 0)
  8264.       x = fdispla;
  8265.     switch (x) {
  8266.       case XYFD_N: s = "none"; break;
  8267.       case XYFD_R: s = "serial"; break;
  8268.       case XYFD_C: s = "fullscreen"; break;
  8269.       case XYFD_S: s = "crt"; break;
  8270.       case XYFD_B: s = "brief"; break;
  8271.     }
  8272.     printf(" ftp display:                   %s\n",s);
  8273.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8274.  
  8275.     if (mlstok || featok || mdtmok || sizeok || ftp_aut) {
  8276.     printf(" enabled:                      ");
  8277.     if (ftp_aut) printf(" AUTH");
  8278.     if (featok)  printf(" FEAT");
  8279.     if (mdtmok)  printf(" MDTM");
  8280.     if (mlstok)  printf(" MLST");
  8281.     if (sizeok)  printf(" SIZE");
  8282.     printf("\n");
  8283.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8284.     }
  8285.     if (!mlstok || !featok || !mdtmok || !sizeok || !ftp_aut) {
  8286.     printf(" disabled:                     ");
  8287.     if (!ftp_aut) printf(" AUTH");
  8288.     if (!featok)  printf(" FEAT");
  8289.     if (!mdtmok)  printf(" MDTM");
  8290.     if (!mlstok)  printf(" MLST");
  8291.     if (!sizeok)  printf(" SIZE");
  8292.     printf("\n");
  8293.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8294.     }
  8295.     switch (ftpget) {
  8296.       case 0: s = "kermit"; break;
  8297.       case 1: s = "ftp"; break;
  8298.       case 2: s = "auto"; break;
  8299.       default: s = "?";
  8300.     }
  8301.     printf(" get-put-remote:                %s\n",s);
  8302.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8303.  
  8304.     printf("\n");
  8305.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8306.  
  8307. #ifdef FTP_SECURITY
  8308.     printf("Available security methods:    ");
  8309. #ifdef FTP_GSSAPI
  8310.     printf("GSSAPI ");
  8311. #endif /* FTP_GSSAPI */
  8312. #ifdef FTP_KRB4
  8313.     printf("Kerberos4 ");
  8314. #endif /* FTP_KRB4 */
  8315. #ifdef FTP_SRP
  8316.     printf("SRP ");
  8317. #endif /* FTP_SRP */
  8318. #ifdef FTP_SSL
  8319.     printf("SSL ");
  8320. #endif /* FTP_SSL */
  8321.  
  8322.     n++;
  8323.     printf("\n\n");
  8324.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8325.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  8326.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8327.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  8328.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8329.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  8330.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8331.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  8332.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8333.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  8334.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8335.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  8336.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8337. #else
  8338.     printf("Available security methods:     (none)\n");
  8339.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8340. #endif /* FTP_SECURITY */
  8341.  
  8342.     if (n <= cmd_rows - 3)
  8343.       printf("\n");
  8344.     return(0);
  8345. }
  8346. #endif /* NOSHOW */
  8347.  
  8348. #ifndef NOHELP
  8349. /* FTP HELP text strings */
  8350.  
  8351. static char * fhs_ftp[] = {
  8352.     "Syntax: FTP subcommand [ operands ]",
  8353.     "  Makes an FTP connection, or sends a command to the FTP server.",
  8354.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  8355.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  8356.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  8357.     ""
  8358. };
  8359.  
  8360. static char * fhs_acc[] = {             /* ACCOUNT */
  8361.     "Syntax: FTP ACCOUNT text",
  8362.     "  Sends an account designator to an FTP server that needs one.",
  8363.     "  Most FTP servers do not use accounts; some use them for other",
  8364.     "  other purposes, such as disk-access passwords.",
  8365.     ""
  8366. };
  8367. static char * fhs_app[] = {             /* APPEND */
  8368.     "Syntax: FTP APPEND filname",
  8369.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  8370.     ""
  8371. };
  8372. static char * fhs_cls[] = {             /* BYE, CLOSE */
  8373.     "Syntax: [ FTP ] BYE",
  8374.     "  Logs out from the FTP server and closes the FTP connection.",
  8375.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  8376.     ""
  8377. };
  8378. static char * fhs_cwd[] = {             /* CD, CWD */
  8379.     "Syntax: [ FTP ] CD directory",
  8380.     "  Asks the FTP server to change to the given directory.",
  8381.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  8382.     ""
  8383. };
  8384. static char * fhs_gup[] = {             /* CDUP, UP */
  8385.     "Syntax: FTP CDUP",
  8386.     "  Asks the FTP server to change to the parent directory of its current",
  8387.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  8388.     ""
  8389. };
  8390. static char * fhs_chm[] = {             /* CHMOD */
  8391.     "Syntax: FTP CHMOD filename permissions",
  8392.     "  Asks the FTP server to change the permissions, protection, or mode of",
  8393.     "  the given file.  The given permissions must be in the syntax of the",
  8394.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  8395.     "  FTP PUT /PERMISSIONS",
  8396.     ""
  8397. };
  8398. static char * fhs_mde[] = {             /* DELETE */
  8399.     "Syntax: FTP DELETE [ switches ] filespec",
  8400.     "  Asks the FTP server to delete the given file or files.",
  8401.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  8402.     "  multiple file deletion).  Optional switches:",
  8403.     " ",
  8404.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8405.     "  /EXCEPT:pattern",
  8406.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  8407.     "  /LARGER-THAN:number",
  8408. #ifdef UNIXOROSK
  8409.     "  /NODOTFILES",
  8410. #endif /* UNIXOROSK */
  8411.     "  /QUIET",
  8412. #ifdef RECURSIVE
  8413.     "  /RECURSIVE (depends on server)",
  8414.     "  /SUBDIRECTORIES",
  8415. #endif /* RECURSIVE */
  8416.     "  /SMALLER-THAN:number",
  8417.     ""
  8418. };
  8419. static char * fhs_dir[] = {             /* DIRECTORY */
  8420.     "Syntax: FTP DIRECTORY [ filespec ]",
  8421.     "  Asks the server to send a directory listing of the files that match",
  8422.     "  the given filespec, or if none is given, all the files in its current",
  8423.     "  directory.  The filespec, including any wildcards, must be in the",
  8424.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  8425.     "  Synonym: RDIRECTORY.",
  8426.     ""
  8427. };
  8428. static char * fhs_vdi[] = {             /* VDIRECTORY */
  8429.     "Syntax: FTP VDIRECTORY [ filespec ]",
  8430.     "  Asks the server to send a directory listing of the files that match",
  8431.     "  the given filespec, or if none is given, all the files in its current",
  8432.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  8433.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  8434.     "  FTP DIRECTORY lists only filenames without details.",
  8435.     ""
  8436. };
  8437. static char * fhs_fea[] = {             /* FEATURES */
  8438.     "Syntax: FTP FEATURES",
  8439.     "  Asks the FTP server to list its special features.  Most FTP servers",
  8440.     "  do not recognize this command.",
  8441.     ""
  8442. };
  8443. static char * fhs_mge[] = {             /* MGET */
  8444.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  8445.     "  Download a single file or multiple files.  Asks the FTP server to send",
  8446.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  8447.     " ",
  8448.     "  /AS-NAME:text",
  8449.     "    Name under which to store incoming file.",
  8450.     "    Pattern required for for multiple files.",
  8451.     "  /BINARY",                        /* /IMAGE */
  8452.     "    Force binary mode.  Synonym: /IMAGE.",
  8453.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  8454.    "    What to do if an incoming file has the same name as an existing file.",
  8455.  
  8456. #ifdef PUTPIPE
  8457.     "  /COMMAND",
  8458.     "    Specifies that the as-name is a command to which the incoming file",
  8459.     "    is to be piped as standard input.",
  8460. #endif /* PUTPIPE */
  8461.  
  8462. #ifdef DOUPDATE
  8463.     "  /DATES-DIFFER",
  8464.     "    Download only those files whose modification date-times differ from",
  8465.     "    those of the corresponding local files, or that do not already",
  8466.     "    exist on the local computer.",
  8467. #endif /* DOUPDATE */
  8468.  
  8469.     "  /DELETE",
  8470.     "    Specifies that each file is to be deleted from the server after,",
  8471.     "    and only if, it is successfully downloaded.",
  8472.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8473.     "    When downloading a group of files, what to do upon failure to",
  8474.     "    transfer a file: quit or proceed to the next one.",
  8475.     "  /EXCEPT:pattern",
  8476.     "    Exception list: don't download any files that match this pattern.",
  8477.     "    See HELP WILDCARD for pattern syntax.",
  8478.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8479.     "    Whether to convert incoming filenames to local syntax.",
  8480. #ifdef PIPESEND
  8481. #ifndef NOSPL
  8482.     "  /FILTER:command",
  8483.     "    Pass incoming files through the given command.",
  8484. #endif /* NOSPL */
  8485. #endif /* PIPESEND */
  8486.     "  /LARGER-THAN:number",
  8487.     "    Only download files that are larger than the given number of bytes.",
  8488.     "  /LISTFILE:filename",
  8489.     "    Obtain the list of files to download from the given file.",
  8490. #ifndef NOCSETS
  8491.     "  /LOCAL-CHARACTER-SET:name",
  8492.     "    When downloading in text mode and character-set conversion is",
  8493.     "    desired, this specifies the target set.",
  8494. #endif /* NOCSETS */
  8495.     "  /MATCH:pattern",
  8496.     "    Specifies a pattern to be used to select filenames locally from the",
  8497.     "    server's list.",
  8498.     "  /MLSD",
  8499.     "    Forces sending of MLSD (rather than NLST) to get the file list.",
  8500. #ifdef CK_TMPDIR
  8501.     "  /MOVE-TO:directory",
  8502.     "    Each file that is downloaded is to be moved to the given local",
  8503.     "    directory immediately after, and only if, it has been received",
  8504.     "    successfully.",
  8505. #endif /* CK_TMPDIR */
  8506.     "  /NAMELIST:filename",
  8507.     "    Instead of downloading the files, stores the list of files that",
  8508.     "    would be downloaded in the given local file, one filename per line.",
  8509.     "  /NLST",
  8510.     "    Forces sending of NLST (rather than MLSD) to get the file list.",
  8511.     "  /NOBACKUPFILES",
  8512.     "    Don't download any files whose names end with .~<number>~.",
  8513.     "  /NODOTFILES",
  8514.     "    Don't download any files whose names begin with period (.).",
  8515.     "  /QUIET",
  8516.     "    Suppress the file-transfer display.",
  8517. #ifdef FTP_RESTART
  8518.     "  /RECOVER",                       /* /RESTART */
  8519.     "    Resume a download that was previously interrupted from the point of",
  8520.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  8521.     "    Synonym: /RESTART.",
  8522. #endif /* FTP_RESTART */
  8523. #ifdef RECURSIVE
  8524.     "  /RECURSIVE",                     /* /SUBDIRECTORIES */
  8525.     "    Create subdirectories automatically if the server sends files",
  8526.     "    recursively and includes pathnames (most don't).",
  8527. #endif /* RECURSIVE */
  8528.     "  /RENAME-TO:text",
  8529.     "    Each file that is downloaded is to be renamed as indicated just,",
  8530.     "    after, and only if, it has arrived successfully.",
  8531. #ifndef NOCSETS
  8532.     "  /SERVER-CHARACTER-SET:name",
  8533.     "    When downloading in text mode and character-set conversion is desired"
  8534. ,   "    this specifies the original file's character set on the server.",
  8535. #endif /* NOCSETS */
  8536.     "  /SERVER-RENAME:text",
  8537.     "    Each server source file is to be renamed on the server as indicated",
  8538.     "    immediately after, but only if, it has arrived succesfully.",
  8539.     "  /SMALLER-THAN:number",
  8540.     "    Download only those files smaller than the given number of bytes.",
  8541.     "  /TEXT",                          /* /ASCII */
  8542.     "    Force text mode.  Synonym: /ASCII.",
  8543.     "  /TENEX",
  8544.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8545. #ifndef NOCSETS
  8546.     "  /TRANSPARENT",
  8547.     "    When downloading in text mode, do not convert chracter-sets.",
  8548. #endif /* NOCSETS */
  8549.     "  /TO-SCREEN",
  8550.     "    The downloaded file is to be displayed on the screen.",
  8551. #ifdef DOUPDATE
  8552.     "  /UPDATE",
  8553.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  8554.     "    newer than than their local counterparts, or that do not exist on",
  8555.     "    the local computer.",
  8556. #endif /* DOUPDATE */
  8557.     ""
  8558. };
  8559. static char * fhs_hlp[] = {             /* HELP */
  8560.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  8561.     "  Asks the FTP server for help about the given command.  First use",
  8562.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  8563.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  8564.     ""
  8565. };
  8566. static char * fhs_idl[] = {             /* IDLE */
  8567.     "Syntax: FTP IDLE [ number ]",
  8568.     "  If given without a number, this asks the FTP server to tell its",
  8569.     "  current idle-time limit.  If given with a number, it asks the server",
  8570.     "  to change its idle-time limit to the given number of seconds.",
  8571.     ""
  8572. };
  8573. static char * fhs_usr[] = {             /* USER, LOGIN */
  8574.     "Syntax: FTP USER username [ password [ account ] ]",
  8575.     "  Log in to the FTP server.  To be used when connected but not yet",
  8576.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  8577.     "  If you omit the password, and one is required by the server, you are",
  8578.     "  prompted for it.  If you omit the account, no account is sent.",
  8579.     "  Synonym: FTP LOGIN.",
  8580.     ""
  8581. };
  8582. static char * fhs_get[] = {             /* GET */
  8583.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  8584.     "  Download a single file.  Asks the FTP server to send the given file.",
  8585.     "  The optional as-name is the name to store it under when it arrives;",
  8586.     "  if omitted, the file is stored with the name it arrived with, as",
  8587.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  8588.     "  value.  Aside from the file list and as-name, syntax and options are",
  8589.     "  the same as for FTP MGET, which is used for downloading multiple files."
  8590. ,   ""
  8591. };
  8592. static char * fhs_mkd[] = {             /* MKDIR */
  8593.     "Syntax: FTP MKDIR directory",
  8594.     "  Asks the FTP server to create a directory with the given name,",
  8595.     "  which must be in the syntax of the server's file system.  Synonyms:",
  8596.     "  REMOTE MKDIR, RMKDIR.",
  8597.     ""
  8598. };
  8599. static char * fhs_mod[] = {             /* MODTIME */
  8600.     "Syntax: FTP MODTIME filename",
  8601.     "  Asks the FTP server to send the modification time of the given file,",
  8602.     "  to be displayed on the screen.  The date-time format is all numeric:",
  8603.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  8604.     "  fractions of seconds).",
  8605.     ""
  8606. };
  8607. static char * fhs_mpu[] = {             /* MPUT */
  8608.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  8609.     "  Uploads files.  Sends the given file or files to the FTP server.",
  8610.     "  Also see FTP PUT.  Optional switches are:",
  8611.     " ",
  8612.     "  /AFTER:date-time",
  8613.     "    Uploads only those files newer than the given date-time.",
  8614.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  8615. #ifdef PUTARRAY
  8616.     "  /ARRAY:array-designator",
  8617.     "    Tells Kermit to upload the contents of the given array, rather than",
  8618.     "    a file.",
  8619. #endif /* PUTARRAY */
  8620.     "  /AS-NAME:text",
  8621.     "    Name under which to send files.",
  8622.     "    Pattern required for for multiple files.",
  8623.     "  /BEFORE:date-time",
  8624.     "    Upload only those files older than the given date-time.",
  8625.     "  /BINARY",
  8626.     "    Force binary mode.  Synonym: /IMAGE.",
  8627. #ifdef PUTPIPE
  8628.     "  /COMMAND",
  8629.     "    Specifies that the filespec is a command whose standard output is",
  8630.     "    to be sent.",
  8631. #endif /* PUTPIPE */
  8632.  
  8633. #ifdef COMMENT
  8634. #ifdef DOUPDATE
  8635.     "  /DATES-DIFFER",
  8636.     "    Upload only those files whose modification date-times differ from",
  8637.     "    those on the server, or that don't exist on the server at all.",
  8638. #endif /* DOUPDATE */
  8639. #endif /* COMMENT */
  8640.  
  8641.     "  /DELETE",
  8642.     "    Specifies that each source file is to be deleted after, and only if,",
  8643.     "    it is successfully uploaded.",
  8644.     "  /DOTFILES",
  8645.     "    Include files whose names begin with period (.).",
  8646.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8647.     "    When uploading a group of files, what to do upon failure to",
  8648.     "    transfer a file: quit or proceed to the next one.",
  8649.     "  /EXCEPT:pattern",
  8650.     "    Exception list: don't upload any files that match this pattern.",
  8651.     "    See HELP WILDCARD for pattern syntax.",
  8652.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8653.     "    Whether to convert outbound filenames to common syntax.",
  8654. #ifdef PIPESEND
  8655. #ifndef NOSPL
  8656.     "  /FILTER:command",
  8657.     "    Pass outbound files through the given command.",
  8658. #endif /* NOSPL */
  8659. #endif /* PIPESEND */
  8660. #ifdef CKSYMLINK
  8661.     "  /FOLLOWINKS",
  8662.     "    Send files that are pointed to by symbolic links.",
  8663.     "  /NOFOLLOWINKS",
  8664.     "    Skip over symbolic links (default).",
  8665. #endif /* CKSYMLINK */
  8666.     "  /LARGER-THAN:number",
  8667.     "    Only upload files that are larger than the given number of bytes.",
  8668.     "  /LISTFILE:filename",
  8669.     "    Obtain the list of files to upload from the given file.",
  8670. #ifndef NOCSETS
  8671.     "  /LOCAL-CHARACTER-SET:name",
  8672.     "    When uploading in text mode and character-set conversion is",
  8673.     "    desired, this specifies the source-file character set.",
  8674. #endif /* NOCSETS */
  8675. #ifdef CK_TMPDIR
  8676.     "  /MOVE-TO:directory",
  8677.     "    Each source file that is uploaded is to be moved to the given local",
  8678.     "    directory when, and only if, the transfer is successful.",
  8679. #endif /* CK_TMPDIR */
  8680.     "  /NOBACKUPFILES",
  8681.     "    Don't upload any files whose names end with .~<number>~.",
  8682. #ifdef UNIXOROSK
  8683.     "  /NODOTFILES",
  8684.     "    Don't upload any files whose names begin with period (.).",
  8685. #endif /* UNIXOROSK */
  8686.     "  /NOT-AFTER:date-time",
  8687.     "    Upload only files that are not newer than the given date-time",
  8688.     "  /NOT-BEFORE:date-time",
  8689.     "    Upload only files that are not older than the given date-time",
  8690. #ifdef UNIX
  8691.     "  /PERMISSIONS",
  8692.     "    Ask the server to set the permissions of each file it receives",
  8693.     "    according to the source file's permissions.",
  8694. #endif /* UNIX */
  8695.     "  /QUIET",
  8696.     "    Suppress the file-transfer display.",
  8697. #ifdef FTP_RESTART
  8698.     "  /RECOVER",
  8699.     "    Resume an upload that was previously interrupted from the point of",
  8700.     "    failure.  Synonym: /RESTART.",
  8701. #endif /* FTP_RESTART */
  8702. #ifdef RECURSIVE
  8703.     "  /RECURSIVE",
  8704.     "    Send files from the given directory and all the directories beneath",
  8705.     "    it.  Synonym: /SUBDIRECTORIES.",
  8706. #endif /* RECURSIVE */
  8707.     "  /RENAME-TO:text",
  8708.     "    Each source file that is uploaded is to be renamed on the local",
  8709.     "    local computer as indicated when and only if, the transfer completes",
  8710.     "    successfully.",
  8711. #ifndef NOCSETS
  8712.     "  /SERVER-CHARACTER-SET:name",
  8713.     "    When uploading in text mode and character-set conversion is desired,",
  8714.     "    this specifies the character set to which the file should be",
  8715.     "    converted for storage on the server.",
  8716. #endif /* NOCSETS */
  8717.     "  /SERVER-RENAME:text",
  8718.     "    Each file that is uploaded is to be renamed as indicated on the",
  8719.     "    server after, and only if, if arrives successfully.",
  8720.     "  /SIMULATE",
  8721.     "    Show which files would be sent without actually sending them.",
  8722.     "  /SMALLER-THAN:number",
  8723.     "    Upload only those files smaller than the given number of bytes.",
  8724.     "  /TEXT",
  8725.     "    Force text mode.  Synonym: /ASCII.",
  8726.     "  /TENEX",
  8727.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8728. #ifndef NOCSETS
  8729.     "  /TRANSPARENT",
  8730.     "    When uploading in text mode, do not convert chracter-sets.",
  8731. #endif /* NOCSETS */
  8732.     "  /TYPE:{TEXT,BINARY}",
  8733.     "    Upload only files of the given type.",
  8734. #ifdef DOUPDATE
  8735.     "  /UPDATE",
  8736.     "    If a file of the same name exists on the server, upload only if",
  8737.     "    the local file is newer.",
  8738. #endif /* DOUPDATE */
  8739.     "  /UNIQUE-SERVER-NAMES",
  8740.     "    Ask the server to compute new names for any incoming file that has",
  8741.     "    the same name as an existing file.",
  8742.     ""
  8743. };
  8744. static char * fhs_opn[] = {             /* OPEN */
  8745. #ifdef CK_SSL
  8746.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  8747.     "  Opens a connection to the FTP server on the given host.  The default",
  8748.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  8749.     "  can be supplied if necessary.  Optional switches are:",
  8750. #else /* CK_SSL */
  8751.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  8752.     "  Opens a connection to the FTP server on the given host.  The default",
  8753.     "  TCP port is 21, but a different port number can be supplied if",
  8754.     "  necessary.  Optional switches are:",
  8755. #endif /* CK_SSL */
  8756.     " ",
  8757.     "  /ANONYMOUS",
  8758.     "    Logs you in anonymously.",
  8759.     "  /USER:text",
  8760.     "    Supplies the given text as your username.",
  8761.     "  /PASSWORD:text",
  8762.     "    Supplies the given text as your password.  If you include a username",
  8763.     "    but omit this switch and the server requires a password, you are",
  8764.     "    prompted for it.",
  8765.     "  /ACCOUNT:text",
  8766.     "    Supplies the given text as your account, if required by the server.",
  8767.     "  /ACTIVE",
  8768.     "    Forces an active (rather than passive) connection.",
  8769.     "  /PASSIVE",
  8770.     "    Forces a passive (rather than active) connection.",
  8771.     "  /NOINIT",
  8772.     "    Inhibits sending initial REST, STRU, and MODE commands, which are",
  8773.     "    well-known standard commands, but to which some servers react badly.",
  8774.     "  /NOLOGIN",
  8775.     "    Inhibits autologin for this connection only.",
  8776.     ""
  8777. };
  8778. static char * fhs_opt[] = {             /* OPTS, OPTIONS */
  8779.     "Syntax: FTP OPTIONS",
  8780.     "  Asks the FTP server to list its current options.  Advanced, new,",
  8781.     "  not supported by most FTP servers.",
  8782.     ""
  8783. };
  8784. static char * fhs_put[] = {             /* PUT, SEND */
  8785.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  8786.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  8787.     "  by an additional field, this is interpreted as the name under which",
  8788.     "  to send the file or files.  See HELP FTP MPUT.",
  8789.     ""
  8790. };
  8791. static char * fhs_pwd[] = {             /* PWD */
  8792.     "Syntax: FTP PWD",
  8793.     "  Asks the FTP server to reveal its current working directory.",
  8794.     "  Synonyms: REMOTE PWD, RPWD.",
  8795.     ""
  8796. };
  8797. static char * fhs_quo[] = {             /* QUOTE */
  8798.     "Syntax: FTP QUOTE text",
  8799.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  8800.     "  for sending commands that Kermit might not support.",
  8801.     ""
  8802. };
  8803. static char * fhs_rge[] = {             /* REGET */
  8804.     "Syntax: FTP REGET",
  8805.     "  Synonym for FTP GET /RECOVER.",
  8806.     ""
  8807. };
  8808. static char * fhs_ren[] = {             /* RENAME */
  8809.     "Syntax: FTP RENAME name1 name1",
  8810.     "  Asks the FTP server to change the name of the file whose name is name1",
  8811.     "  and which resides in the FTP server's file system, to name2.  Works",
  8812.     "  only for single files; wildcards are not accepted.",
  8813.     ""
  8814. };
  8815. static char * fhs_res[] = {             /* RESET */
  8816.     "Syntax: FTP RESET",
  8817.     "  Asks the server to log out your session, terminating your access",
  8818.     "  rights, without closing the connection.",
  8819.     ""
  8820. };
  8821. static char * fhs_rmd[] = {             /* RMDIR */
  8822.     "Syntax: FTP RMDIR directory",
  8823.     "  Asks the FTP server to remove the directory whose name is given.",
  8824.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  8825.     "  RMDIR, RRMDIR.",
  8826.     ""
  8827. };
  8828. static char * fhs_sit[] = {             /* SITE */
  8829.     "Syntax: FTP SITE text",
  8830.     "  Sends a site-specific command to the FTP server.",
  8831.     ""
  8832. };
  8833. static char * fhs_siz[] = {             /* SIZE */
  8834.     "Syntax: FTP SIZE filename",
  8835.     "  Asks the FTP server to send a numeric string representing the size",
  8836.     "  of the given file.",
  8837.     ""
  8838. };
  8839. static char * fhs_sta[] = {             /* STATUS */
  8840.     "Syntax: FTP STATUS [ filename ]",
  8841.     "  Asks the FTP server to report its status.  If a filename is given,",
  8842.     "  the FTP server should report details about the file.",
  8843.     ""
  8844. };
  8845. static char * fhs_sys[] = {             /* SYSTEM */
  8846.     "Syntax: FTP SYSTEM",
  8847.     "  Asks the FTP server to report its operating system type.",
  8848.     ""
  8849. };
  8850. static char * fhs_typ[] = {             /* TYPE */
  8851.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  8852.     "  Puts the client and server in the indicated transfer mode.",
  8853.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  8854.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  8855.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  8856.     "  TENEX mode.",
  8857.     ""
  8858. };
  8859. static char * fhs_uma[] = {             /* UMASK */
  8860.     "Syntax: FTP UMASK number",
  8861.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  8862.     "  only (or mainly) to UNIX-based FTP servers.",
  8863.     ""
  8864. };
  8865. static char * fhs_chk[] = {             /* CHECK */
  8866.     "Syntax: FTP CHECK remote-filespec",
  8867.     "  Asks the FTP server if the given file or files exist.  If the",
  8868.     "  remote-filespec contains wildcards, this command fails if no server",
  8869.     "  files match, and succeeds if at least one file matches.  If the",
  8870.     "  remote-filespec does not contain wildcards, this command succeeds if",
  8871.     "  the given file exists and fails if it does not.",
  8872.     ""
  8873. };
  8874. static char * fhs_ena[] = {        /* ENABLE */
  8875.     "Syntax: FTP ENABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  8876.     "  Enables the use of the given FTP protocol command in case it has been",
  8877.     "  disabled (but this is no guarantee that the FTP server understands it)."
  8878. ,
  8879.     "  Use SHOW FTP to see which of these commands is enabled and disabled.",
  8880.     "  Also see FTP DISABLE.",
  8881.     ""
  8882. };
  8883. static char * fhs_dis[] = {        /* DISABLE */
  8884.     "Syntax: FTP DISABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  8885.     "  Disables the use of the given FTP protocol command.",
  8886.     "  Also see FTP ENABLE.",
  8887.     ""
  8888. };
  8889.  
  8890. #endif /* NOHELP */
  8891.  
  8892. int
  8893. doftphlp() {
  8894.     int cx;
  8895.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  8896.       if (cx != -3)
  8897.         return(cx);
  8898.     if ((x = cmcfm()) < 0)
  8899.       return(x);
  8900.  
  8901. #ifdef NOHELP
  8902.     printf("Sorry, no help available\n");
  8903. #else
  8904.     switch (cx) {
  8905.       case -3:
  8906.         return(hmsga(fhs_ftp));
  8907.       case FTP_ACC:                     /* ACCOUNT */
  8908.         return(hmsga(fhs_acc));
  8909.       case FTP_APP:                     /* APPEND */
  8910.         return(hmsga(fhs_app));
  8911.       case FTP_CLS:                     /* BYE, CLOSE */
  8912.         return(hmsga(fhs_cls));
  8913.       case FTP_CWD:                     /* CD, CWD */
  8914.         return(hmsga(fhs_cwd));
  8915.       case FTP_GUP:                     /* CDUP, UP */
  8916.         return(hmsga(fhs_gup));
  8917.       case FTP_CHM:                     /* CHMOD */
  8918.         return(hmsga(fhs_chm));
  8919.       case FTP_MDE:                     /* DELETE, MDELETE */
  8920.         return(hmsga(fhs_mde));
  8921.       case FTP_DIR:                     /* DIRECTORY */
  8922.         return(hmsga(fhs_dir));
  8923.       case FTP_VDI:                     /* VDIRECTORY */
  8924.         return(hmsga(fhs_vdi));
  8925.       case FTP_FEA:                     /* FEATURES */
  8926.         return(hmsga(fhs_fea));
  8927.       case FTP_GET:                     /* GET */
  8928.         return(hmsga(fhs_get));
  8929.       case FTP_HLP:                     /* HELP */
  8930.         return(hmsga(fhs_hlp));
  8931.       case FTP_IDL:                     /* IDLE */
  8932.         return(hmsga(fhs_idl));
  8933.       case FTP_USR:                     /* USER, LOGIN */
  8934.         return(hmsga(fhs_usr));
  8935.       case FTP_MGE:                     /* MGET */
  8936.         return(hmsga(fhs_mge));
  8937.       case FTP_MKD:                     /* MKDIR */
  8938.         return(hmsga(fhs_mkd));
  8939.       case FTP_MOD:                     /* MODTIME */
  8940.         return(hmsga(fhs_mod));
  8941.       case FTP_MPU:                     /* MPUT */
  8942.         return(hmsga(fhs_mpu));
  8943.       case FTP_OPN:                     /* OPEN */
  8944.         return(hmsga(fhs_opn));
  8945.       case FTP_OPT:                     /* OPTS, OPTIONS */
  8946.         return(hmsga(fhs_opt));
  8947.       case FTP_PUT:                     /* PUT, SEND */
  8948.         return(hmsga(fhs_put));
  8949.       case FTP_PWD:                     /* PWD */
  8950.         return(hmsga(fhs_pwd));
  8951.       case FTP_QUO:                     /* QUOTE */
  8952.         return(hmsga(fhs_quo));
  8953.       case FTP_RGE:                     /* REGET */
  8954.         return(hmsga(fhs_rge));
  8955.       case FTP_REN:                     /* RENAME */
  8956.         return(hmsga(fhs_ren));
  8957.       case FTP_RES:                     /* RESET */
  8958.         return(hmsga(fhs_res));
  8959.       case FTP_RMD:                     /* RMDIR */
  8960.         return(hmsga(fhs_rmd));
  8961.       case FTP_SIT:                     /* SITE */
  8962.         return(hmsga(fhs_sit));
  8963.       case FTP_SIZ:                     /* SIZE */
  8964.         return(hmsga(fhs_siz));
  8965.       case FTP_STA:                     /* STATUS */
  8966.         return(hmsga(fhs_sta));
  8967.       case FTP_SYS:                     /* SYSTEM */
  8968.         return(hmsga(fhs_sys));
  8969.       case FTP_TYP:                     /* TYPE */
  8970.         return(hmsga(fhs_typ));
  8971.       case FTP_UMA:                     /* UMASK */
  8972.         return(hmsga(fhs_uma));
  8973.       case FTP_CHK:                     /* CHECK */
  8974.         return(hmsga(fhs_chk));
  8975.       case FTP_ENA:
  8976.         return(hmsga(fhs_ena));
  8977.       case FTP_DIS:
  8978.         return(hmsga(fhs_dis));
  8979.       default:
  8980.         printf("Sorry, help available for this command.\n");
  8981.         break;
  8982.     }
  8983. #endif /* NOHELP */
  8984.     return(success = 0);
  8985. }
  8986.  
  8987. int
  8988. dosetftphlp() {
  8989.     int cx;
  8990.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  8991.       if (cx != -3)
  8992.         return(cx);
  8993.     if (cx != -3)
  8994.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  8995.     if ((x = cmcfm()) < 0)
  8996.       return(x);
  8997.  
  8998. #ifdef NOHELP
  8999.     printf("Sorry, no help available\n");
  9000. #else
  9001.     switch (cx) {
  9002.       case -3:
  9003.         printf("\nSyntax: SET FTP parameter value\n");
  9004.         printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  9005.         printf("  Type \"help set ftp xxx\" for information about setting\n");
  9006.         printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  9007.         return(0);
  9008.  
  9009. #ifdef FTP_SECURITY
  9010.       case FTS_ATP:                     /* "authtype" */
  9011.         printf("\nSyntax: SET FTP AUTHTYPE list\n");
  9012.         printf("  Specifies an ordered list of authentication methods to be\n"
  9013.                );
  9014.         printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  9015.         printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  9016.         return(0);
  9017.  
  9018.       case FTS_AUT:                     /* "autoauthentication" */
  9019.         printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  9020.         printf("  Tells whether authentication should be negotiated by the\n");
  9021.         printf("  FTP OPEN command.  Default is ON.\n\n");
  9022.         break;
  9023.  
  9024.       case FTS_CRY:                     /* "autoencryption" */
  9025.         printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  9026.         printf("  Tells whether encryption (privacy) should be negotiated\n");
  9027.         printf("  by the FTP OPEN command.  Default is ON.\n\n");
  9028.         break;
  9029. #endif /* FTP_SECURITY */
  9030.  
  9031.       case FTS_LOG:                     /* "autologin" */
  9032.         printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  9033.         printf("  Tells Kermit whether to try to log you in automatically\n");
  9034.         printf("  as part of the connection process.\n\n");
  9035.         break;
  9036.  
  9037.       case FTS_DIS:
  9038.         printf("\nSET FTP DISPLAY { BRIEF, FULLSCREEN, CRT, ... }\n");
  9039.     printf("  Chooses the file-transfer display style for FTP.\n");
  9040.         printf("  Like SET TRANSFER DISPLAY but applies only to FTP.\n\n");
  9041.         break;
  9042.  
  9043. #ifndef NOCSETS
  9044.       case FTS_XLA:                     /* "character-set-translation" */
  9045.         printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  9046.         printf("  Whether to translate character sets when transferring\n");
  9047.         printf("  text files with FTP.  OFF by default.\n\n");
  9048.         break;
  9049.  
  9050. #endif /* NOCSETS */
  9051.       case FTS_FNC:                     /* "collision" */
  9052.         printf("\n");
  9053.         printf(
  9054. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  9055.                );
  9056.         printf("  Tells what do when an incoming file has the same name as\n");
  9057.         printf("  an existing file when downloading with FTP.\n\n");
  9058.         break;
  9059.  
  9060. #ifdef FTP_SECURITY
  9061.       case FTS_CPL:                     /* "command-protection-level" */
  9062.         printf("\n");
  9063.         printf(
  9064. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9065.                );
  9066.         printf("\n");
  9067.         printf(
  9068. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  9069.         break;
  9070.       case FTS_CFW:                     /* "credential-forwarding" */
  9071.         printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  9072.         printf("  Tells whether end-user credentials are to be forwarded\n");
  9073.         printf("  to the server if supported by the authentication method\n");
  9074.         printf("  (GSSAPI-KRB5 only).\n\n");
  9075.         break;
  9076.       case FTS_DPL:                     /* "data-protection-level" */
  9077.         printf("\n");
  9078.         printf(
  9079. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9080.                );
  9081.         printf("\n");
  9082.         printf(
  9083. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  9084.         break;
  9085. #endif /* FTP_SECURITY */
  9086.  
  9087.       case FTS_DBG:                     /* "debug" */
  9088.         printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  9089.         printf("  Whether to print FTP protocol messages.\n\n");
  9090.         return(0);
  9091.  
  9092.       case FTS_ERR:                     /* "error-action" */
  9093.         printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  9094.         printf("  What to do when an error occurs when transferring a group\n")
  9095.           ;
  9096.         printf("  of files: quit and fail, or proceed to the next file.\n\n");
  9097.         return(0);
  9098.  
  9099.       case FTS_CNV:                     /* "filenames" */
  9100.         printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  9101.         printf("  What to do with filenames: convert them, take and use them\n"
  9102.                );
  9103.         printf("  literally; or choose what to do automatically based on the\n"
  9104.                );
  9105.         printf("  OS type of the server.  The default is AUTO.\n\n");
  9106.         return(0);
  9107.  
  9108.       case FTS_PSV:                     /* "passive-mode" */
  9109.         printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  9110.         printf("  Whether to use passive mode, which helps to get through\n");
  9111.         printf("  firewalls.  ON by default.\n\n");
  9112.         return(0);
  9113.  
  9114.       case FTS_PRM:                     /* "permissions" */
  9115.         printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  9116.         printf("  Whether to try to send file permissions when uploading.\n");
  9117.         printf("  OFF by default.  AUTO means only if client and server\n");
  9118.         printf("  have the same OS type.\n\n");
  9119.         return(0);
  9120.  
  9121.       case FTS_TST:                     /* "progress-messages" */
  9122.         printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  9123.         printf("  Whether Kermit should print locally-generated feedback\n");
  9124.         printf("  messages for each non-file-transfer command.");
  9125.         printf("  ON by default.\n\n");
  9126.         return(0);
  9127.  
  9128.       case FTS_SPC:                     /* "send-port-commands" */
  9129.         printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  9130.         printf("  Whether Kermit should send a new PORT command for each");
  9131.         printf("  task.\n\n");
  9132.         return(0);
  9133.  
  9134. #ifndef NOCSETS
  9135.       case FTS_CSR:                     /* "server-character-set" */
  9136.         printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  9137.         printf("  The name of the character set used for text files on the\n");
  9138.         printf("  server.  Enter a name of '?' for a menu.\n\n");
  9139.         return(0);
  9140. #endif /* NOCSETS */
  9141.  
  9142.       case FTS_STO:            /* "server-time-offset */
  9143.     printf(
  9144. "\nSyntax: SET FTP SERVER-TIME-OFFSET +hh[:mm[:ss]] or -hh[:mm[:ss]]\n");
  9145.         printf(
  9146. "  Specifies an offset to apply to the server's file timestamps.\n");
  9147.         printf(
  9148. "  Use this to correct for misconfigured server time or timezone.\n");
  9149.         printf(
  9150. "  Format: must begin with + or - sign.  Hours must be given; minutes\n");
  9151.         printf(
  9152. "  and seconds are optional: +4 = +4:00 = +4:00:00 (add 4 hours).\n\n");
  9153.         return(0);
  9154.  
  9155.       case FTS_TYP:                     /* "type" */
  9156.         printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  9157.         printf("  Establishes the default transfer mode.\n");
  9158.         printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  9159.         printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  9160.         printf("  them again.\n\n");
  9161.         return(0);
  9162.  
  9163. #ifdef PATTERNS
  9164.       case FTS_GFT:
  9165.         printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  9166.         printf("  Tells whether GET and MGET should automatically switch\n");
  9167.         printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  9168.         printf("  matching the name of each incoming file with its list of\n");
  9169.         printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  9170.         printf("  default.  SHOW PATTERNS displays the current pattern\n");
  9171.         printf("  list.  HELP SET FILE to see how to change it.\n");
  9172.         break;
  9173. #endif /* PATTERNS */
  9174.  
  9175.       case FTS_USN:                     /* "unique-server-names" */
  9176.         printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  9177.         printf("  Tells whether to ask the server to create unique names\n");
  9178.         printf("  for any uploaded file that has the same name as an\n");
  9179.         printf("  existing file.  Default is OFF.\n\n");
  9180.         return(0);
  9181.  
  9182.       case FTS_VBM:                     /* "verbose-mode" */
  9183.         printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  9184.         printf("  Whether to display all responses from the FTP server.\n");
  9185.         printf("  OFF by default.\n\n");
  9186.         return(0);
  9187.  
  9188.       case FTS_DAT:
  9189.         printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  9190.         printf("  Whether to set date of incoming files from the file date\n");
  9191.         printf("  on the server.  ON by default.  Note: there is no way to\n")
  9192.           ;
  9193.         printf("  set the date on files uploaded to the server.  Also note\n");
  9194.     printf("  that not all servers support this feature.\n\n");
  9195.         return(0);
  9196.  
  9197.       case FTS_APW:
  9198.     printf("\nSyntax: SET FTP ANONYMOUS-PASSWORD [ text ]\n");
  9199.     printf("  Password to supply automatically on anonymous FTP\n");
  9200.     printf("  connections instead of the default user@host.\n");
  9201.     printf("  Omit optional text to restore default.\n\n");
  9202.     return(0);
  9203.  
  9204.       default:
  9205.         printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  9206.     }
  9207. #endif /* NOHELP */
  9208.     return(0);
  9209. }
  9210.  
  9211. #ifndef L_SET
  9212. #define L_SET 0
  9213. #endif /* L_SET */
  9214. #ifndef L_INCR
  9215. #define L_INCR 1
  9216. #endif /* L_INCR */
  9217.  
  9218. #ifdef FTP_SRP
  9219. char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
  9220. char *srp_pass;
  9221. char *srp_acct;
  9222. #endif /* FTP_SRP */
  9223.  
  9224. static int kerror;                      /* Needed for all auth types */
  9225.  
  9226. static struct   sockaddr_in hisctladdr;
  9227. static struct   sockaddr_in hisdataaddr;
  9228. static struct   sockaddr_in data_addr;
  9229. static int      data = -1;
  9230. static int      ptflag = 0;
  9231. static struct   sockaddr_in myctladdr;
  9232.  
  9233. #ifdef COMMENT
  9234. #ifndef OS2
  9235. UID_T getuid();
  9236. #endif /* OS2 */
  9237. #endif /* COMMENT */
  9238.  
  9239.  
  9240. static int cpend = 0;                   /* No pending replies */
  9241.  
  9242. #ifdef CK_SSL
  9243. extern SSL *ssl_ftp_con;
  9244. extern SSL_CTX *ssl_ftp_ctx;
  9245. extern SSL *ssl_ftp_data_con;
  9246. extern int ssl_ftp_active_flag;
  9247. extern int ssl_ftp_data_active_flag;
  9248. #endif /* CK_SSL */
  9249.  
  9250. /*  f t p c m d  --  Send a command to the FTP server  */
  9251. /*
  9252.   Call with:
  9253.     char * cmd: The command to send.
  9254.     char * arg: The argument (e.g. a filename).
  9255.     int lcs: The local character set index.
  9256.     int rcs: The remote (server) character set index.
  9257.     int vbm: Verbose mode:
  9258.       0 = force verbosity off
  9259.      >0 = force verbosity on
  9260.  
  9261.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  9262.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  9263.   character set to the remote one before sending the result to the server.
  9264.  
  9265.    Returns:
  9266.     0 on failure with ftpcode = -1
  9267.     >= 0 on success (getreply() result) with ftpcode = 0.
  9268. */
  9269. static char xcmdbuf[RFNBUFSIZ];
  9270.  
  9271. static int
  9272. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  9273.     char * s = NULL;
  9274.     int r = 0, x = 0, fc = 0, len = 0, cmdlen = 0, q = -1;
  9275.     sig_t oldintr;
  9276.  
  9277.     if (ftp_deb)                        /* DEBUG */
  9278.       vbm = 1;
  9279.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9280.       vbm = 0;
  9281.     else if (vbm < 0)                   /* VERBOSE */
  9282.       vbm = ftp_vbm;
  9283.  
  9284.     cancelfile = 0;
  9285.     if (!cmd) cmd = "";
  9286.     if (!arg) arg = "";
  9287.     cmdlen = (int)strlen(cmd);
  9288.     len = cmdlen + (int)strlen(arg) + 1;
  9289.  
  9290.     if (ftp_deb /* && !dpyactive */ ) {
  9291. #ifdef FTP_PROXY
  9292.         if (ftp_prx) printf("%s ", ftp_host);
  9293. #endif /* FTP_PROXY */
  9294.         printf("---> ");
  9295.         if (!anonymous && strcmp("PASS",cmd) == 0)
  9296.           printf("PASS XXXX");
  9297.         else
  9298.           printf("%s %s",cmd,arg);
  9299.         printf("\n");
  9300.     }
  9301.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  9302.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  9303.  
  9304. #ifdef DEBUG
  9305.     if (deblog) {
  9306.         debug(F110,"ftpcmd cmd",cmd,0);
  9307.         debug(F110,"ftpcmd arg",arg,0);
  9308.         debug(F101,"ftpcmd lcs","",lcs);
  9309.         debug(F101,"ftpcmd rcs","",rcs);
  9310.     }
  9311. #endif /* DEBUG */
  9312.  
  9313.     if (csocket == -1) {
  9314.         perror("No control connection for command");
  9315.         ftpcode = -1;
  9316.         return(0);
  9317.     }
  9318.     havesigint = 0;
  9319.     oldintr = signal(SIGINT, cmdcancel);
  9320.  
  9321. #ifndef NOCSETS
  9322.     if (*arg &&                         /* If an arg was given */
  9323.         lcs > -1 &&                     /* and a local charset */
  9324.         rcs > -1 &&                     /* and a remote charset */
  9325.         lcs != rcs &&                   /* and the two are not the same */
  9326.         lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
  9327.         rcs != FC_UCS2                  /* ... */
  9328.         ) {
  9329.         initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
  9330.         xgnbp = arg;                    /* Global pointer to input string */
  9331.         rfnptr = rfnbuf;                /* Global pointer to output buffer */
  9332.  
  9333.         while (1) {
  9334.             if ((c0 = xgnbyte(FC_UCS2,lcs,strgetc)) < 0) break;
  9335.             if (xpnbyte(c0,TC_UCS2,rcs,strputc) < 0) break;
  9336.         }
  9337.         /*
  9338.           We have to copy here instead of translating directly into
  9339.           xcmdbuf[] so strputc() can check length.  Alternatively we could
  9340.           write yet another xpnbyte() output function.
  9341.         */
  9342.         if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  9343.             printf("?FTP command too long: %s + arg\n",cmd);
  9344.             ftpcode = -1;
  9345.             return(0);
  9346.         }
  9347.         x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  9348.     }
  9349. #endif /* NOCSETS */
  9350.  
  9351.     s = xcmdbuf;                        /* Command to send to server */
  9352.  
  9353. #ifdef DEBUG
  9354.     if (deblog) {            /* Log it */
  9355.     if (!anonymous && !ckstrcmp(s,"PASS ",5,0)) {
  9356.         /* But don't log passwords */
  9357.         debug(F110,"FTP SENT ","PASS XXXX",0);
  9358.     } else {
  9359.         debug(F110,"FTP SENT ",s,0);
  9360.     }
  9361.     }
  9362. #endif /* DEBUG */
  9363.  
  9364. #ifdef CK_ENCRYPTION
  9365.   again:
  9366. #endif /* CK_ENCRYPTION */
  9367.     if (scommand(s) == 0) {              /* Send it. */
  9368.       signal(SIGINT, oldintr);
  9369.       return(0);
  9370.     }
  9371.     cpend = 1;
  9372.     x = !strcmp(cmd,"QUIT");        /* Is it the QUIT command? */
  9373.     if (x)                /* In case we're interrupted */
  9374.       connected = 0;            /* while waiting for the reply... */
  9375.  
  9376.     fc = 0;                /* Function code for getreply() */
  9377.     if (!strncmp(cmd,"AUTH ",5)        /* Must parse AUTH reply */
  9378. #ifdef FTPHOST
  9379.     && strncmp(cmd, "HOST ",5)
  9380. #endif /* FTPHOST */
  9381.     ) {
  9382.     fc = GRF_AUTH;
  9383.     } else if (!ckstrcmp(cmd,"FEAT",-1,0)) { /* Must parse FEAT reply */
  9384.     fc = GRF_FEAT;            /* But FEAT not widely understood */
  9385.     if (!ftp_deb)            /* So suppress error messages */
  9386.       vbm = 9;
  9387.     }
  9388.     r = getreply(x,            /* Expect connection to close */
  9389.          lcs,rcs,        /* Charsets */
  9390.          vbm,            /* Verbosity */
  9391.          fc            /* Function code */
  9392.          );
  9393.     if (q > -1)
  9394.       quiet = q;
  9395.  
  9396. #ifdef CK_ENCRYPTION
  9397.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  9398.         fprintf(stderr,
  9399.                "ENC command not supported at server; retrying under MIC...\n");
  9400.         ftp_cpl = FPL_SAF;
  9401.         goto again;
  9402.     }
  9403. #endif /* CK_ENCRYPTION */
  9404. #ifdef COMMENT
  9405.     if (cancelfile && oldintr != SIG_IGN)
  9406.       (*oldintr)(SIGINT);
  9407. #endif /* COMMENT */
  9408.     signal(SIGINT, oldintr);
  9409.     return(r);
  9410. }
  9411.  
  9412. static VOID
  9413. lostpeer() {
  9414.     debug(F100,"lostpeer","",0);
  9415.     if (connected) {
  9416.         if (csocket != -1) {
  9417. #ifdef CK_SSL
  9418.             if (ssl_ftp_active_flag) {
  9419.                 SSL_shutdown(ssl_ftp_con);
  9420.                 SSL_free(ssl_ftp_con);
  9421.                 ssl_ftp_proxy = 0;
  9422.                 ssl_ftp_active_flag = 0;
  9423.                 ssl_ftp_con = NULL;
  9424.             }
  9425. #endif /* CK_SSL */
  9426. #ifdef TCPIPLIB
  9427.             socket_close(csocket);
  9428. #else /* TCPIPLIB */
  9429. #ifdef USE_SHUTDOWN
  9430.             shutdown(csocket, 1+1);
  9431. #endif /* USE_SHUTDOWN */
  9432.             close(csocket);
  9433. #endif /* TCPIPLIB */
  9434.             csocket = -1;
  9435.         }
  9436.         if (data != -1) {
  9437. #ifdef CK_SSL
  9438.             if (ssl_ftp_data_active_flag) {
  9439.                 SSL_shutdown(ssl_ftp_data_con);
  9440.                 SSL_free(ssl_ftp_data_con);
  9441.                 ssl_ftp_data_active_flag = 0;
  9442.                 ssl_ftp_data_con = NULL;
  9443.             }
  9444. #endif /* CK_SSL */
  9445. #ifdef TCPIPLIB
  9446.             socket_close(data);
  9447. #else /* TCPIPLIB */
  9448. #ifdef USE_SHUTDOWN
  9449.             shutdown(data, 1+1);
  9450. #endif /* USE_SHUTDOWN */
  9451.             close(data);
  9452. #endif /* TCPIPLIB */
  9453.             data = -1;
  9454.             globaldin = -1;
  9455.         }
  9456.         connected = 0;
  9457.         anonymous = 0;
  9458.         loggedin = 0;
  9459.         auth_type = NULL;
  9460.         ftp_cpl = ftp_dpl = FPL_CLR;
  9461. #ifdef CKLOGDIAL
  9462.         ftplogend();
  9463. #endif /* CKLOGDIAL */
  9464.  
  9465. #ifdef LOCUS
  9466.     if (autolocus)            /* Auotomatic locus switching... */
  9467.       setlocus(1,1);        /* Switch locus to local. */
  9468. #endif /* LOCUS */
  9469. #ifdef OS2
  9470.         DialerSend(OPT_KERMIT_HANGUP, 0);
  9471. #endif /* OS2 */
  9472.     }
  9473. #ifdef FTP_PROXY
  9474.     pswitch(1);
  9475.     if (connected) {
  9476.         if (csocket != -1) {
  9477. #ifdef TCPIPLIB
  9478.             socket_close(csocket);
  9479. #else /* TCPIPLIB */
  9480. #ifdef USE_SHUTDOWN
  9481.             shutdown(csocket, 1+1);
  9482. #endif /* USE_SHUTDOWN */
  9483.             close(csocket);
  9484. #endif /* TCPIPLIB */
  9485.             csocket = -1;
  9486.         }
  9487.         connected = 0;
  9488.         anonymous = 0;
  9489.         loggedin = 0;
  9490.         auth_type = NULL;
  9491.         ftp_cpl = ftp_dpl = FPL_CLR;
  9492.     }
  9493.     proxflag = 0;
  9494.     pswitch(0);
  9495. #endif /* FTP_PROXY */
  9496. }
  9497.  
  9498. int
  9499. ftpisopen() {
  9500.     return(connected);
  9501. }
  9502.  
  9503. static int
  9504. ftpclose() {
  9505.     extern int quitting;
  9506.     if (!connected)
  9507.       return(0);
  9508.     if (!ftp_vbm && !quiet) printlines = 1;
  9509.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  9510.     if (csocket) {
  9511. #ifdef CK_SSL
  9512.         if (ssl_ftp_active_flag) {
  9513.             SSL_shutdown(ssl_ftp_con);
  9514.             SSL_free(ssl_ftp_con);
  9515.             ssl_ftp_proxy = 0;
  9516.             ssl_ftp_active_flag = 0;
  9517.             ssl_ftp_con = NULL;
  9518.         }
  9519. #endif /* CK_SSL */
  9520. #ifdef TCPIPLIB
  9521.         socket_close(csocket);
  9522. #else /* TCPIPLIB */
  9523. #ifdef USE_SHUTDOWN
  9524.         shutdown(csocket, 1+1);
  9525. #endif /* USE_SHUTDOWN */
  9526.         close(csocket);
  9527. #endif /* TCPIPLIB */
  9528.     }
  9529.     csocket = -1;
  9530.     connected = 0;
  9531.     anonymous = 0;
  9532.     loggedin = 0;
  9533.     mdtmok = 1;
  9534.     sizeok = 1;
  9535.     featok = 1;
  9536.     stouarg = 1;
  9537.     typesent = 0;
  9538.     data = -1;
  9539.     globaldin = -1;
  9540. #ifdef FTP_PROXY
  9541.     if (!proxy)
  9542.       macnum = 0;
  9543. #endif /* FTP_PROXY */
  9544.     auth_type = NULL;
  9545.     ftp_dpl = FPL_CLR;
  9546. #ifdef CKLOGDIAL
  9547.     ftplogend();
  9548. #endif /* CKLOGDIAL */
  9549. #ifdef LOCUS
  9550.     /* Unprefixed file management commands are executed locally */
  9551.     if (autolocus && !ftp_cmdlin && !quitting) {
  9552.         setlocus(1,1);
  9553.     }
  9554. #endif /* LOCUS */
  9555. #ifdef OS2
  9556.     DialerSend(OPT_KERMIT_HANGUP, 0);
  9557. #endif /* OS2 */
  9558.     return(0);
  9559. }
  9560.  
  9561. int
  9562. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  9563.     char * host;
  9564.  
  9565.     if (connected) {
  9566.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  9567.         ftpcode = -1;
  9568.         return(0);
  9569.     }
  9570. #ifdef FTPHOST
  9571.     hostcmd = 0;
  9572. #endif /* FTPHOST */
  9573.     alike = 0;
  9574.     ftp_srvtyp[0] = NUL;
  9575.     if (!service) service = "";
  9576.     if (!*service) service = use_tls ? "ftps" : "ftp";
  9577.  
  9578.     if (!isdigit(service[0])) {
  9579.         struct servent *destsp;
  9580.         destsp = getservbyname(service, "tcp");
  9581.         if (!destsp) {
  9582.             if (!ckstrcmp(service,"ftp",-1,0)) {
  9583.                 ftp_port = 21;
  9584.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  9585.                 ftp_port = 990;
  9586.             } else {
  9587.                 printf("?Bad port name - \"%s\"\n", service);
  9588.                 ftpcode = -1;
  9589.                 return(0);
  9590.             }
  9591.         } else {
  9592.             ftp_port = destsp->s_port;
  9593.             ftp_port = ntohs(ftp_port);
  9594.         }
  9595.     } else
  9596.         ftp_port = atoi(service);
  9597.     if (ftp_port <= 0) {
  9598.         printf("?Bad port name - \"%s\"\n", service);
  9599.         ftpcode = -1;
  9600.         return(0);
  9601.     }
  9602.     host = ftp_hookup(remote, ftp_port, use_tls);
  9603.     if (host) {
  9604.         ckstrncpy(ftp_user_host,remote,MAX_DNS_NAMELEN);
  9605.         connected = 1;                  /* Set FTP defaults */
  9606.         ftp_cpl = ftp_dpl = FPL_CLR;
  9607.         curtype = FTT_ASC;              /* Server uses ASCII mode */
  9608.         form = FORM_N;
  9609.         mode = MODE_S;
  9610.         stru = STRU_F;
  9611.         strcpy(bytename, "8");
  9612.         bytesize = 8;
  9613.  
  9614. #ifdef FTP_SECURITY
  9615.         if (ftp_aut) {
  9616.             if (ftp_auth()) {
  9617.                 if (ftp_cry 
  9618. #ifdef OS2
  9619.                      && ck_crypt_is_installed()
  9620. #endif /* OS2 */
  9621.                      ) {
  9622.                     if (!quiet)
  9623.                       printf("FTP Command channel is Private (encrypted)\n");
  9624.                     ftp_cpl = FPL_PRV;
  9625.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  9626.                         /* a failure here is most likely caused by a mixup */
  9627.                         /* in the session key used by client and server    */
  9628.             printf("?Protection buffer size negotiation failed\n");
  9629.                         return(0);
  9630.                     }
  9631.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  9632.                         if (!quiet)
  9633.                           printf("FTP Data channel is Private (encrypted)\n");
  9634.                         ftp_dpl = FPL_PRV;
  9635.                     } else
  9636.                       printf("?Unable to enable encryption on data channel\n");
  9637.                 } else {
  9638.                     ftp_cpl = FPL_SAF;
  9639.                 }
  9640.             }
  9641.             if (!connected)
  9642.           goto fail;
  9643.         }
  9644. #endif /* FTP_SECURITY */
  9645.         if (ftp_log)            /* ^^^ */
  9646.           ftp_login(remote);
  9647.  
  9648.         if (!connected)
  9649.       goto fail;
  9650.  
  9651. #ifdef CKLOGDIAL
  9652.         dologftp();
  9653. #endif /* CKLOGDIAL */
  9654. #ifdef OS2
  9655.         DialerSend(OPT_KERMIT_CONNECT, 0);
  9656. #endif /* OS2 */
  9657.         passivemode = ftp_psv;
  9658.         sendport = ftp_spc;
  9659.     mdtmok = 1;
  9660.     sizeok = 1;
  9661.     stouarg = 1;
  9662.     typesent = 0;
  9663.  
  9664.         if (ucbuf == NULL) {
  9665.             actualbuf = DEFAULT_PBSZ;
  9666.             while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  9667.               actualbuf >>= 2;
  9668.         }
  9669.         if (!maxbuf)
  9670.           ucbufsiz = actualbuf - FUDGE_FACTOR;
  9671.     debug(F101,"ftpopen ucbufsiz","",ucbufsiz);
  9672.         return(1);
  9673.     }
  9674.   fail:
  9675.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  9676.     ftpcode = -1;
  9677.     return(0);
  9678. }
  9679.  
  9680. #ifdef CK_SSL
  9681. int
  9682. ssl_auth() {
  9683.     int i;
  9684.     char* p;
  9685.  
  9686.     if (ssl_debug_flag) {
  9687.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  9688.         fflush(stderr);
  9689.         /* for the moment I want the output on screen */
  9690.     }
  9691.     if (ssl_ftp_data_con != NULL) {
  9692.         SSL_free(ssl_ftp_data_con);
  9693.         ssl_ftp_data_con = NULL;
  9694.     }
  9695.     if (ssl_ftp_con != NULL) {
  9696.         SSL_free(ssl_ftp_con);
  9697.         ssl_ftp_con=NULL;
  9698.     }
  9699.     if (ssl_ftp_ctx != NULL) {
  9700.         SSL_CTX_free(ssl_ftp_ctx);
  9701.         ssl_ftp_ctx = NULL;
  9702.     }
  9703. #ifdef COMMENT
  9704.     if (auth_type && !strcmp(auth_type,"TLS")) {
  9705.         ssl_ftp_ctx=SSL_CTX_new(TLSv1_client_method());
  9706.         if (!ssl_ftp_ctx)
  9707.           return(0);
  9708.         SSL_CTX_set_options(ssl_ftp_ctx,
  9709.                    SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9710.                             );
  9711.     } else {
  9712.         ssl_ftp_ctx = SSL_CTX_new(SSLv3_client_method());
  9713.         if (!ssl_ftp_ctx)
  9714.           return(0);
  9715.         SSL_CTX_set_options(ssl_ftp_ctx,SSL_OP_ALL);
  9716.     }
  9717. #else /* COMMENT */
  9718.     ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  9719.     if (!ssl_ftp_ctx)
  9720.       return(0);
  9721.     /* The SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 
  9722.      * was added to OpenSSL 0.9.6e and 0.9.7.  It does not exist in previous
  9723.      * versions
  9724.      */
  9725. #ifndef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  9726. #define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0L
  9727. #endif
  9728.  
  9729.     SSL_CTX_set_options(ssl_ftp_ctx,
  9730.                     SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9731.                         );
  9732. #endif /* COMMENT */
  9733.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  9734.                                   (pem_password_cb *)ssl_passwd_callback);
  9735.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  9736.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  9737.  
  9738. #ifdef OS2
  9739. #ifdef NT
  9740.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9741.     {
  9742.         char path[CKMAXPATH];
  9743.         extern char exedir[];
  9744.  
  9745.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9746.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9747.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9748.             if (ssl_debug_flag)
  9749.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9750.         }
  9751.  
  9752.         ckmakmsg(path,CKMAXPATH,
  9753.                  (char *)GetAppData(1),"kermit 95/certs",NULL,NULL);
  9754.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9755.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9756.             if (ssl_debug_flag)
  9757.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9758.         }
  9759.  
  9760.         ckmakmsg(path,CKMAXPATH,
  9761.                  (char *)GetAppData(0),"kermit 95/certs",NULL,NULL);
  9762.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9763.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9764.             if (ssl_debug_flag)
  9765.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9766.         }
  9767.  
  9768.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  9769.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9770.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9771.             if (ssl_debug_flag)
  9772.                 printf("?Unable to load verify-file: %s\r\n",path);
  9773.         }
  9774.  
  9775.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  9776.          "kermit 95/ca_certs.pem",NULL,NULL);
  9777.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9778.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9779.             if (ssl_debug_flag)
  9780.                 printf("?Unable to load verify-file: %s\r\n",path);
  9781.         }
  9782.  
  9783.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  9784.          "kermit 95/ca_certs.pem",NULL,NULL);
  9785.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9786.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9787.             if (ssl_debug_flag)
  9788.                 printf("?Unable to load verify-file: %s\r\n",path);
  9789.         }
  9790.     }
  9791. #else /* NT */
  9792.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9793.     {
  9794.  
  9795.         char path[CKMAXPATH];
  9796.         extern char exedir[];
  9797.  
  9798.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9799.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9800.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9801.             if (ssl_debug_flag)
  9802.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9803.         }
  9804.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  9805.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9806.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9807.             if (ssl_debug_flag)
  9808.                 printf("?Unable to load verify-file: %s\r\n",path);
  9809.         }
  9810.     }
  9811. #endif /* NT */
  9812. #else /* OS2 */
  9813.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  9814. #endif /* OS2 */
  9815.  
  9816.     if (ssl_verify_file &&
  9817.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
  9818.         debug(F110,
  9819.               "ftp ssl auth unable to load ssl_verify_file",
  9820.               ssl_verify_file,
  9821.               0
  9822.               );
  9823.         if (ssl_debug_flag)
  9824.           printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  9825.     }
  9826.     if (ssl_verify_dir &&
  9827.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
  9828.         debug(F110,
  9829.               "ftp ssl auth unable to load ssl_verify_dir",
  9830.               ssl_verify_dir,
  9831.               0
  9832.               );
  9833.         if (ssl_debug_flag)
  9834.           printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  9835.     }
  9836.  
  9837.     /* set up the new CRL Store */
  9838.     crl_store = (X509_STORE *)X509_STORE_new();
  9839.     if (crl_store) {
  9840. #ifdef OS2
  9841.         char path[CKMAXPATH];
  9842.         extern char exedir[];
  9843.  
  9844.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  9845.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9846.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9847.             if (ssl_debug_flag)
  9848.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9849.         }
  9850. #ifdef NT
  9851.         ckmakmsg(path,CKMAXPATH,
  9852.          (char *)GetAppData(1),"kermit 95/crls",NULL,NULL);
  9853.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9854.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9855.             if (ssl_debug_flag)
  9856.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9857.         }
  9858.         ckmakmsg(path,CKMAXPATH,
  9859.          (char *)GetAppData(0),"kermit 95/crls",NULL,NULL);
  9860.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  9861.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  9862.             if (ssl_debug_flag)
  9863.                 printf("?Unable to load crl-dir: %s\r\n",path);
  9864.         }
  9865. #endif /* NT */
  9866.         
  9867.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  9868.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9869.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9870.             if (ssl_debug_flag)
  9871.                 printf("?Unable to load crl-file: %s\r\n",path);
  9872.         }
  9873. #ifdef NT
  9874.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  9875.          "kermit 95/ca_crls.pem",NULL,NULL);
  9876.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9877.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9878.             if (ssl_debug_flag)
  9879.                 printf("?Unable to load crl-file: %s\r\n",path);
  9880.         }
  9881.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  9882.          "kermit 95/ca_crls.pem",NULL,NULL);
  9883.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  9884.             debug(F110,"ftp ssl auth unable to load file",path,0);
  9885.             if (ssl_debug_flag)
  9886.                 printf("?Unable to load crl-file: %s\r\n",path);
  9887.         }
  9888. #endif /* NT */
  9889. #endif /* OS2 */
  9890.  
  9891.         if (ssl_crl_file || ssl_crl_dir) {
  9892.             if (ssl_crl_file &&
  9893.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  9894.                 debug(F110,
  9895.                       "ftp ssl auth unable to load ssl_crl_file",
  9896.                       ssl_crl_file,
  9897.                       0
  9898.                       );
  9899.                 if (ssl_debug_flag)
  9900.                   printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  9901.             }
  9902.             if (ssl_crl_dir &&
  9903.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  9904.                 debug(F110,
  9905.                       "ftp ssl auth unable to load ssl_crl_dir",
  9906.                       ssl_crl_dir,
  9907.                       0
  9908.                       );
  9909.                 if (ssl_debug_flag)
  9910.                   printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  9911.             }
  9912.         } else {
  9913.             X509_STORE_set_default_paths(crl_store);
  9914.         }
  9915.     }
  9916.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  9917.                        ssl_client_verify_callback);
  9918.     ssl_verify_depth = -1;
  9919.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  9920.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  9921.     SSL_set_fd(ssl_ftp_con,csocket);
  9922.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  9923.     if (ssl_cipher_list) {
  9924.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  9925.     } else {
  9926.         char * p;
  9927.         if (p = getenv("SSL_CIPHER")) {
  9928.             SSL_set_cipher_list(ssl_ftp_con,p);
  9929.         } else {
  9930.             SSL_set_cipher_list(ssl_ftp_con,DEFAULT_CIPHER_LIST);
  9931.         }
  9932.     }
  9933.     if (ssl_debug_flag) {
  9934.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  9935.         fflush(stderr);
  9936.     }
  9937.     if (SSL_connect(ssl_ftp_con) <= 0) {
  9938.         static char errbuf[1024];
  9939.         ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  9940.                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  9941.         fprintf(stderr,"%s\n", errbuf);
  9942.         fflush(stderr);
  9943.         ssl_ftp_active_flag=0;
  9944.         SSL_free(ssl_ftp_con);
  9945.         ssl_ftp_con = NULL;
  9946.     } else {
  9947.         ssl_ftp_active_flag = 1;
  9948.  
  9949.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  9950.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  9951.  
  9952.             if (!subject) {
  9953.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  9954.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9955.                     return(ssl_ftp_active_flag = 0);
  9956.                 } else {
  9957.                     if (uq_ok("Warning: Server didn't provide a certificate\n",
  9958.                                "Continue? (Y/N)",3,NULL,0) <= 0) {
  9959.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  9960.                         return(ssl_ftp_active_flag = 0);
  9961.                     }
  9962.                 }
  9963.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_user_host)) {
  9964.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9965.                 return(ssl_ftp_active_flag = 0);
  9966.             }
  9967.         }
  9968.         debug(F110,"ssl_auth","[SSL - OK]",0);
  9969.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  9970.     }
  9971.     if (ssl_debug_flag) {
  9972.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  9973.         fflush(stderr);
  9974.     }
  9975.     return(ssl_ftp_active_flag);
  9976. }
  9977. #endif /* CK_SSL */
  9978.  
  9979. static sigtype
  9980. cmdcancel(sig) int sig; {
  9981. #ifdef OS2
  9982.     /* In Unix we "chain" to trap(), which prints this */
  9983.     printf("^C...\n");
  9984. #endif /* OS2 */
  9985.     debug(F100,"ftp cmdcancel caught SIGINT ","",0);
  9986.     fflush(stdout);
  9987.     secure_getc(0,1);            /* Initialize net input buffers */
  9988.     cancelfile++;
  9989.     cancelgroup++;
  9990.     mlsreset();
  9991. #ifndef OS2
  9992. #ifdef FTP_PROXY
  9993.     if (ptflag)                         /* proxy... */
  9994.       longjmp(ptcancel,1);
  9995. #endif /* FTP_PROXY */
  9996.     debug(F100,"ftp cmdcancel chain to trap()...","",0);
  9997.     trap(SIGINT);
  9998.     /* NOTREACHED */
  9999.     debug(F100,"ftp cmdcancel return from trap()...","",0);
  10000. #else
  10001.     debug(F100,"ftp cmdcancel PostCtrlCSem()...","",0);
  10002.     PostCtrlCSem();
  10003. #endif /* OS2 */
  10004. }
  10005.  
  10006. static int
  10007. #ifdef CK_ANSIC
  10008. scommand(char * s)                      /* Was secure_command() */
  10009. #else
  10010. scommand(s) char * s;
  10011. #endif /* CK_ANSIC */
  10012. {
  10013.     int length = 0, len2;
  10014.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  10015. #ifdef CK_SSL
  10016.     if (ssl_ftp_active_flag) {
  10017.         int error, rc;
  10018.         length = strlen(s) + 2;
  10019.         length = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10020.         rc = SSL_write(ssl_ftp_con,out,length);
  10021.         error = SSL_get_error(ssl_ftp_con,rc);
  10022.         switch (error) {
  10023.           case SSL_ERROR_NONE:
  10024.             return(1);
  10025.           case SSL_ERROR_WANT_WRITE:
  10026.           case SSL_ERROR_WANT_READ:
  10027.           case SSL_ERROR_SYSCALL:
  10028. #ifdef NT
  10029.             {
  10030.                 int gle = GetLastError();
  10031.             }
  10032. #endif /* NT */
  10033.           case SSL_ERROR_WANT_X509_LOOKUP:
  10034.           case SSL_ERROR_SSL:
  10035.           case SSL_ERROR_ZERO_RETURN:
  10036.           default:
  10037.             lostpeer();
  10038.         }
  10039.         return(0);
  10040.     }
  10041. #endif /* CK_SSL */
  10042.  
  10043.     if (auth_type && ftp_cpl != FPL_CLR) {
  10044. #ifdef FTP_SRP
  10045.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  10046.           if ((length = srp_encode(ftp_cpl == FPL_PRV,
  10047.                                    (CHAR *)s,
  10048.                                    (CHAR *)out,
  10049.                                    strlen(s))) < 0) {
  10050.               fprintf(stderr, "SRP failed to encode message\n");
  10051.               return(0);
  10052.           }
  10053. #endif /* FTP_SRP */
  10054. #ifdef FTP_KRB4
  10055.         if (ck_krb4_is_installed() &&
  10056.             (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  10057.             if (ftp_cpl == FPL_PRV) {
  10058.                 length =
  10059.                   krb_mk_priv((CHAR *)s, (CHAR *)out,
  10060.                               strlen(s), ftp_sched,
  10061. #ifdef KRB524
  10062.                               ftp_cred.session,
  10063. #else /* KRB524 */
  10064.                               &ftp_cred.session,
  10065. #endif /* KRB524 */
  10066.                               &myctladdr, &hisctladdr);
  10067.             } else {
  10068.                 length =
  10069.                   krb_mk_safe((CHAR *)s,
  10070.                               (CHAR *)out,
  10071.                               strlen(s),
  10072. #ifdef KRB524
  10073.                               ftp_cred.session,
  10074. #else /* KRB524 */
  10075.                               &ftp_cred.session,
  10076. #endif /* KRB524 */
  10077.                               &myctladdr, &hisctladdr);
  10078.             }
  10079.             if (length == -1) {
  10080.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  10081.                         ftp_cpl == FPL_PRV ? "priv" : "safe");
  10082.                 return(0);
  10083.             }
  10084.         }
  10085. #endif /* FTP_KRB4 */
  10086. #ifdef FTP_GSSAPI
  10087.         /* Scommand (based on level) */
  10088.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  10089.             gss_buffer_desc in_buf, out_buf;
  10090.             OM_uint32 maj_stat, min_stat;
  10091.             int conf_state;
  10092.             in_buf.value = s;
  10093.             in_buf.length = strlen(s) + 1;
  10094.             maj_stat = gss_seal(&min_stat, gcontext,
  10095.                                 (ftp_cpl==FPL_PRV), /* private */
  10096.                                 GSS_C_QOP_DEFAULT,
  10097.                                 &in_buf, &conf_state,
  10098.                                 &out_buf);
  10099.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  10100.                 user_gss_error(maj_stat, min_stat,
  10101.                                (ftp_cpl==FPL_PRV)?
  10102.                                "gss_seal ENC didn't complete":
  10103.                                "gss_seal MIC didn't complete");
  10104.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  10105.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  10106.             } else {
  10107.                 if (ftp_deb)
  10108.                   fprintf(stderr, "sealed (%s) %d bytes\n",
  10109.                           ftp_cpl==FPL_PRV?"ENC":"MIC",
  10110.                           out_buf.length);
  10111.                 memcpy(out, out_buf.value,
  10112.                        length=out_buf.length);
  10113.                 gss_release_buffer(&min_stat, &out_buf);
  10114.             }
  10115.         }
  10116. #endif /* FTP_GSSAPI */
  10117.         /* Other auth types go here ... */
  10118.  
  10119.         len2 = FTP_BUFSIZ;
  10120.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  10121.                                    length, &len2, RADIX_ENCODE))
  10122.             ) {
  10123.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  10124.                     radix_error(kerror));
  10125.             return(0);
  10126.         }
  10127.         if (ftp_deb)
  10128.           fprintf(stderr, "scommand(%s)\nencoding %d bytes\n", s, length);
  10129.         len2 = ckmakmsg(out,
  10130.             FTP_BUFSIZ,
  10131.             ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  10132.                         in,
  10133.             "\r\n",
  10134.             NULL
  10135.             );
  10136.         send(csocket,(SENDARG2TYPE)out,len2,0);
  10137.     } else {
  10138.         char out[FTP_BUFSIZ];
  10139.         int len = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10140.         send(csocket,(SENDARG2TYPE)out,len,0);
  10141.     }
  10142.     return(1);
  10143. }
  10144.  
  10145. static int
  10146. mygetc() {
  10147.     static char inbuf[4096];
  10148.     static int bp = 0, ep = 0;
  10149.     int rc;
  10150.  
  10151.     if (bp == ep) {
  10152.         bp = ep = 0;
  10153. #ifdef CK_SSL
  10154.         if (ssl_ftp_active_flag) {
  10155.             int error;
  10156.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  10157.             error = SSL_get_error(ssl_ftp_con,rc);
  10158.             switch (error) {
  10159.               case SSL_ERROR_NONE:
  10160.                 break;
  10161.               case SSL_ERROR_WANT_WRITE:
  10162.               case SSL_ERROR_WANT_READ:
  10163.                 return(0);
  10164.               case SSL_ERROR_SYSCALL:
  10165.                 if (rc == 0) {          /* EOF */
  10166.                     break;
  10167.                 } else {
  10168. #ifdef NT
  10169.                     int gle = GetLastError();
  10170. #endif /* NT */
  10171.                     break;
  10172.                 }
  10173.               case SSL_ERROR_WANT_X509_LOOKUP:
  10174.               case SSL_ERROR_SSL:
  10175.               case SSL_ERROR_ZERO_RETURN:
  10176.               default:
  10177.                 break;
  10178.             }
  10179.         } else
  10180. #endif /* CK_SSL */
  10181.           rc = recv(csocket,(char *)inbuf,4096,0);
  10182.         if (rc <= 0)
  10183.           return(EOF);
  10184.         ep = rc;
  10185.     }
  10186.     return(inbuf[bp++]);
  10187. }
  10188.  
  10189. /*  x l a t e c  --  Translate a character  */
  10190. /*
  10191.     Call with:
  10192.       fc    = Function code: 0 = translate, 1 = initialize.
  10193.       c     = Character (as int).
  10194.       incs  = Index of charset to translate from.
  10195.       outcs = Index of charset to translate to.
  10196.  
  10197.     Returns:
  10198.       0: OK
  10199.      -1: Error
  10200. */
  10201. static int
  10202. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  10203. #ifdef NOCSETS
  10204.     return(c);
  10205. #else
  10206.     static char buf[128];
  10207.     static int cx;
  10208.     int c0, c1;
  10209.  
  10210.     if (fc == 1) {                      /* Initialize */
  10211.         cx = 0;                         /* Catch-up buffer write index */
  10212.         xgnbp = buf;                    /* Catch-up buffer read pointer */
  10213.         buf[0] = NUL;                   /* Buffer is empty */
  10214.         return(0);
  10215.     }
  10216.     if (cx >= 127) {                    /* Catch-up buffer full */
  10217.         debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  10218.         printf("?Translation buffer overflow\n");
  10219.         return(-1);
  10220.     }
  10221.     /* Add char to buffer. */
  10222.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  10223.  
  10224.     debug(F000,"xlatec buf",ckitoa(cx),c);
  10225.     buf[cx++] = c;
  10226.     buf[cx] = NUL;
  10227.  
  10228.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  10229.         if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)    /* (NULL was xprintc) */
  10230.           return(-1);
  10231.     }
  10232.     /* If we're caught up, reinitialize the buffer */
  10233.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  10234. #endif /* NOCSETS */
  10235. }
  10236.  
  10237.  
  10238. /*  p a r s e f e a t  */
  10239.  
  10240. /* Note: for convenience we align keyword values with table indices */
  10241. /* If you need to insert a new keyword, adjust the SFT_xxx definitions */
  10242.  
  10243. static struct keytab feattab[] = {
  10244.     { "$$$$", 0,        0 },        /* Dummy for sfttab[0] */
  10245.     { "AUTH", SFT_AUTH, 0 },
  10246.     { "LANG", SFT_LANG, 0 },
  10247.     { "MDTM", SFT_MDTM, 0 },
  10248.     { "MLST", SFT_MLST, 0 },
  10249.     { "PBSZ", SFT_PBSZ, 0 },
  10250.     { "PROT", SFT_PROT, 0 },
  10251.     { "REST", SFT_REST, 0 },
  10252.     { "SIZE", SFT_SIZE, 0 },
  10253.     { "TVFS", SFT_TVFS, 0 },
  10254.     { "UTF8", SFT_UTF8, 0 }
  10255. };
  10256. static int nfeattab = (sizeof(feattab) / sizeof(struct keytab));
  10257.  
  10258. #define FACT_CSET  1
  10259. #define FACT_CREA  2
  10260. #define FACT_LANG  3
  10261. #define FACT_MTYP  4
  10262. #define FACT_MDTM  5
  10263. #define FACT_PERM  6
  10264. #define FACT_SIZE  7
  10265. #define FACT_TYPE  8
  10266. #define FACT_UNIQ  9
  10267.  
  10268. static struct keytab facttab[] = {
  10269.     { "CHARSET",    FACT_CSET, 0 },
  10270.     { "CREATE",     FACT_CREA, 0 },
  10271.     { "LANG",       FACT_LANG, 0 },
  10272.     { "MEDIA-TYPE", FACT_MTYP, 0 },
  10273.     { "MODIFY",     FACT_MDTM, 0 },
  10274.     { "PERM",       FACT_PERM, 0 },
  10275.     { "SIZE",       FACT_SIZE, 0 },
  10276.     { "TYPE",       FACT_TYPE, 0 },
  10277.     { "UNIQUE",     FACT_UNIQ, 0 }
  10278. };
  10279. static int nfacttab = (sizeof(facttab) / sizeof(struct keytab));
  10280.  
  10281. static struct keytab ftyptab[] = {
  10282.     { "CDIR", FTYP_CDIR, 0 },
  10283.     { "DIR",  FTYP_DIR,  0 },
  10284.     { "FILE", FTYP_FILE, 0 },
  10285.     { "PDIR", FTYP_PDIR, 0 }
  10286. };
  10287. static int nftyptab = (sizeof(ftyptab) / sizeof(struct keytab));
  10288.  
  10289. static VOID
  10290. parsefeat(s) char * s; {        /* Parse a FEATURE response */
  10291.     char kwbuf[8];
  10292.     int i, x;
  10293.     if (!s) return;
  10294.     if (!*s) return;
  10295.     while (*s < '!')
  10296.       s++;
  10297.     for (i = 0; i < 4; i++) {
  10298.     if (s[i] < '!')
  10299.       break;
  10300.     kwbuf[i] = s[i];
  10301.     }
  10302.     if (s[i] && s[i] != SP)
  10303.       return;
  10304.     kwbuf[i] = NUL;
  10305.     /* xlookup requires a full (but case independent) match */
  10306.     i = xlookup(feattab,kwbuf,nfeattab,&x);
  10307.     debug(F111,"ftp parsefeat",s,i);
  10308.     if (i < 0 || i > 15)
  10309.       return;
  10310.  
  10311.     switch (i) {
  10312.       case SFT_MDTM:            /* Controlled by ENABLE/DISABLE */
  10313.     sfttab[i] = mdtmok;
  10314.     if (mdtmok) sfttab[0]++;
  10315.     break;
  10316.       case SFT_MLST:            /* ditto */
  10317.     sfttab[i] = mlstok;
  10318.     if (mlstok) sfttab[0]++;
  10319.     break;
  10320.       case SFT_SIZE:            /* ditto */
  10321.     sfttab[i] = sizeok;
  10322.     if (sizeok) sfttab[0]++;
  10323.     break;
  10324.       case SFT_AUTH:            /* ditto */
  10325.     sfttab[i] = ftp_aut;
  10326.     if (ftp_aut) sfttab[0]++;
  10327.     break;
  10328.       default:                /* Others */
  10329.     sfttab[0]++;
  10330.     sfttab[i]++;
  10331.     }
  10332. }
  10333.  
  10334. static char *
  10335. parsefacts(s) char * s; {        /* Parse MLS[DT] File Facts */
  10336.     char * p;
  10337.     int i, j, x;
  10338.     if (!s) return(NULL);
  10339.     if (!*s) return(NULL);
  10340.  
  10341.     /* Maybe we should make a copy of s so we can poke it... */
  10342.  
  10343.     while ((p = ckstrchr(s,'='))) {
  10344.     *p = NUL;            /* s points to fact */
  10345.     i = xlookup(facttab,s,nfacttab,&x); 
  10346.     debug(F111,"ftp parsefact fact",s,i);
  10347.     *p = '=';
  10348.     s = p+1;            /* Now s points to arg */
  10349.     p =  ckstrchr(s,';');
  10350.     if (!p) {
  10351.         debug(F110,"ftp parsefact end-of-val search fail",s,0);
  10352.         break;
  10353.     }
  10354.     *p = NUL;
  10355.     debug(F110,"ftp parsefact valu",s,0);
  10356.     switch (i) {
  10357.       case FACT_CSET:        /* Ignore these for now */
  10358.       case FACT_CREA:
  10359.       case FACT_LANG:
  10360.       case FACT_PERM:
  10361.       case FACT_MTYP:
  10362.       case FACT_UNIQ:
  10363.         break;
  10364.       case FACT_MDTM:        /* Modtime */
  10365.         makestr(&havemdtm,s);
  10366.         debug(F110,"ftp parsefact mdtm",havemdtm,0);
  10367.         break;
  10368.       case FACT_SIZE:        /* Size */
  10369.         havesize = atol(s);
  10370.         debug(F101,"ftp parsefact size","",havesize);
  10371.         break;
  10372.       case FACT_TYPE:        /* Type */
  10373.         j = xlookup(ftyptab,s,nftyptab,NULL);
  10374.         debug(F111,"ftp parsefact type",s,j);
  10375.         havetype = (j < 1) ? 0 : j;
  10376.         break;
  10377.     }
  10378.     *p = ';';
  10379.     s = p+1;            /* s points next fact or name */
  10380.     }
  10381.     while (*s == SP)            /* Skip past spaces. */
  10382.       s++;
  10383.     if (!*s)                /* Make sure we still have a name */
  10384.       s = NULL;
  10385.     debug(F110,"ftp parsefact name",s,0);
  10386.     return(s);
  10387. }
  10388.  
  10389. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  10390.  
  10391. /* vbm = 1 (verbose); 0 (quiet except for error messages); 9 (super quiet) */
  10392.  
  10393. static int
  10394. getreply(expecteof,lcs,rcs,vbm,fc) int expecteof, lcs, rcs, vbm, fc; {
  10395.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  10396.     register int i, c, n;
  10397.     register int dig;
  10398.     register char *cp;
  10399.     int xlate = 0;
  10400.     int count = 0;
  10401.     int auth = 0;
  10402.     int originalcode = 0, continuation = 0;
  10403.     sig_t oldintr;
  10404.     int pflag = 0;
  10405.     char *pt = pasv;
  10406.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  10407.     int safe = 0;
  10408.     int xquiet = 0;
  10409.  
  10410.     auth = (fc == GRF_AUTH);
  10411.  
  10412. #ifndef NOCSETS
  10413.     debug(F101,"ftp getreply lcs","",lcs);
  10414.     debug(F101,"ftp getreply rcs","",rcs);
  10415.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  10416.         xlate = 1;
  10417.         initxlate(rcs,lcs);
  10418.         xlatec(1,0,rcs,lcs);
  10419.     }
  10420. #endif /* NOCSETS */
  10421.     debug(F101,"ftp getreply fc","",fc);
  10422.  
  10423.     if (quiet)
  10424.       xquiet = 1;
  10425.     if (vbm == 9) {
  10426.         xquiet = 1;
  10427.         vbm = 0;
  10428.     }
  10429.     if (ftp_deb)                        /* DEBUG */
  10430.       vbm = 1;
  10431.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  10432.       vbm = 0;
  10433.     else if (vbm < 0)                   /* VERBOSE */
  10434.       vbm = ftp_vbm;
  10435.  
  10436.     ibuf[0] = '\0';
  10437.     if (reply_parse)
  10438.       reply_ptr = reply_buf;
  10439.     havesigint = 0;
  10440.     oldintr = signal(SIGINT, cmdcancel);
  10441.     for (count = 0;; count++) {
  10442.         obuf[0] = '\0';
  10443.         dig = n = ftpcode = i = 0;
  10444.         cp = ftp_reply_str;
  10445.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  10446.             if (c == IAC) {             /* Handle telnet commands */
  10447.                 switch (c = mygetc()) {
  10448.                   case WILL:
  10449.                   case WONT:
  10450.                     c = mygetc();
  10451.                     obuf[0] = IAC;
  10452.                     obuf[1] = DONT;
  10453.                     obuf[2] = c;
  10454.                     obuf[3] = NUL;
  10455. #ifdef CK_SSL
  10456.                     if (ssl_ftp_active_flag) {
  10457.                         int error, rc;
  10458.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10459.                         error = SSL_get_error(ssl_ftp_con,rc);
  10460.                         switch (error) {
  10461.                           case SSL_ERROR_NONE:
  10462.                             break;
  10463.                           case SSL_ERROR_WANT_WRITE:
  10464.                           case SSL_ERROR_WANT_READ:
  10465.                             return(0);
  10466.                           case SSL_ERROR_SYSCALL:
  10467.                             if (rc == 0) { /* EOF */
  10468.                                 break;
  10469.                             } else {
  10470. #ifdef NT
  10471.                                 int gle = GetLastError();
  10472. #endif /* NT */
  10473.                                 break;
  10474.                             }
  10475.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10476.                           case SSL_ERROR_SSL:
  10477.                           case SSL_ERROR_ZERO_RETURN:
  10478.                           default:
  10479.                             break;
  10480.                         }
  10481.                     } else
  10482. #endif /* CK_SSL */
  10483.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10484.                     break;
  10485.                   case DO:
  10486.                   case DONT:
  10487.                     c = mygetc();
  10488.                     obuf[0] = IAC;
  10489.                     obuf[1] = WONT;
  10490.                     obuf[2] = c;
  10491.                     obuf[3] = NUL;
  10492. #ifdef CK_SSL
  10493.                     if (ssl_ftp_active_flag) {
  10494.                         int error, rc;
  10495.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10496.                         error = SSL_get_error(ssl_ftp_con,rc);
  10497.                         switch (error) {
  10498.                           case SSL_ERROR_NONE:
  10499.                             break;
  10500.                           case SSL_ERROR_WANT_WRITE:
  10501.                           case SSL_ERROR_WANT_READ:
  10502.                               signal(SIGINT,oldintr);
  10503.                               return(0);
  10504.                           case SSL_ERROR_SYSCALL:
  10505.                             if (rc == 0) { /* EOF */
  10506.                                 break;
  10507.                             } else {
  10508. #ifdef NT
  10509.                                 int gle = GetLastError();
  10510. #endif /* NT */
  10511.                                 break;
  10512.                             }
  10513.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10514.                           case SSL_ERROR_SSL:
  10515.                           case SSL_ERROR_ZERO_RETURN:
  10516.                           default:
  10517.                             break;
  10518.                         }
  10519.                     } else
  10520. #endif /* CK_SSL */
  10521.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10522.                     break;
  10523.                   default:
  10524.                     break;
  10525.                 }
  10526.                 continue;
  10527.             }
  10528.             dig++;
  10529.             if (c == EOF) {
  10530.                 if (expecteof) {
  10531.                     signal(SIGINT,oldintr);
  10532.                     ftpcode = 221;
  10533.                     debug(F101,"ftp getreply EOF","",ftpcode);
  10534.                     return(0);
  10535.                 }
  10536.                 lostpeer();
  10537.                 if (!xquiet) {
  10538.                     if (ftp_deb)
  10539.                       printf("421 ");
  10540.                     printf(
  10541.                       "Service not available, connection closed by server\n");
  10542.                     fflush(stdout);
  10543.                 }
  10544.                 signal(SIGINT,oldintr);
  10545.                 ftpcode = 421;
  10546.                 debug(F101,"ftp getreply EOF","",ftpcode);
  10547.                 return(4);
  10548.             }
  10549.             if (n == 0) {        /* First digit */
  10550.         n = c;            /* Save it */
  10551.         }
  10552.             if (auth_type &&
  10553. #ifdef CK_SSL
  10554.                 !ssl_ftp_active_flag &&
  10555. #endif /* CK_SSL */
  10556.                 !ibuf[0] && (n == '6' || continuation)) {
  10557.                 if (c != '\r' && dig > 4)
  10558.                   obuf[i++] = c;
  10559.             } else {
  10560.                 if (auth_type &&
  10561. #ifdef CK_SSL
  10562.                     !ssl_ftp_active_flag &&
  10563. #endif /* CK_SSL */
  10564.                     !ibuf[0] && dig == 1 && vbm)
  10565.                   printf("Unauthenticated reply received from server:\n");
  10566.                 if (reply_parse) {
  10567.                     *reply_ptr++ = c;
  10568.                     *reply_ptr = NUL;
  10569.                 }
  10570.                 if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  10571.                     ftp_cmdlin < 2) {
  10572.                     if ((c != '\r') &&
  10573.                         (ftp_deb || ((vbm || (!auth && n == '5')) &&
  10574.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
  10575.                         )))))
  10576.                     {
  10577. #ifdef FTP_PROXY
  10578.                         if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  10579.                           printf("%s:",ftp_host);
  10580. #endif /* FTP_PROXY */
  10581.  
  10582.                         if (!xquiet) {
  10583. #ifdef NOCSETS
  10584.                             printf("%c",c);
  10585. #else
  10586.                             if (xlate) {
  10587.                                 xlatec(0,c,rcs,lcs);
  10588.                             } else {
  10589.                                 printf("%c",c);
  10590.                             }
  10591. #endif /* NOCSETS */
  10592.                         }
  10593.                     }
  10594.                 }
  10595.             }
  10596.             if (auth_type &&
  10597. #ifdef CK_SSL
  10598.                 !ssl_ftp_active_flag &&
  10599. #endif /* CK_SSL */
  10600.                 !ibuf[0] && n != '6')
  10601.               continue;
  10602.             if (dig < 4 && isdigit(c))
  10603.               ftpcode = ftpcode * 10 + (c - '0');
  10604.             if (!pflag && ftpcode == 227)
  10605.               pflag = 1;
  10606.             if (dig > 4 && pflag == 1 && isdigit(c))
  10607.               pflag = 2;
  10608.             if (pflag == 2) {
  10609.                 if (c != '\r' && c != ')')
  10610.                   *pt++ = c;
  10611.                 else {
  10612.                     *pt = '\0';
  10613.                     pflag = 3;
  10614.                 }
  10615.             }
  10616.             if (dig == 4 && c == '-' && n != '6') {
  10617.                 if (continuation)
  10618.                   ftpcode = 0;
  10619.                 continuation++;
  10620.             }
  10621.             if (cp < &ftp_reply_str[FTP_BUFSIZ - 1]) {
  10622.                 *cp++ = c;
  10623.                 *cp = NUL;
  10624.             }
  10625.         }
  10626.         if (deblog ||
  10627. #ifdef COMMENT
  10628. /*
  10629.   Sometimes we need to print the server reply.  printlines is nonzero for any
  10630.   command where the results are sent back on the control connection rather
  10631.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  10632.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  10633.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  10634.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  10635.   lines from UNIX.  Further experimentation needed with other servers.  Of
  10636.   course RFC959 is mute as to the format of the server reply.
  10637.  
  10638.   'printlines' is also true for PWD and BYE.
  10639. */
  10640.         (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  10641. #else
  10642. /* No, we can't be that clever -- it breaks other things like RPWD... */
  10643.             (printlines &&
  10644.              (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  10645. #endif /* COMMENT */
  10646.             ) {
  10647.             char * q = cp;
  10648.             char *r = ftp_reply_str;
  10649.             *q-- = NUL;                 /* NUL-terminate */
  10650.             while (*q < '!' && q > r)   /* Strip CR, etc */
  10651.               *q-- = NUL;
  10652.             if (!ftp_deb && printlines) { /* If printing */
  10653.                 if (ftpcode != 0)       /* strip ftpcode if any */
  10654.                   r += 4;
  10655. #ifdef NOCSETS
  10656.                 printf("%s\n",r);       /* and print */
  10657. #else
  10658.                 if (!xlate) {
  10659.                     printf("%s\n",r);
  10660.                 } else {        /* Translating */
  10661.                     xgnbp = r;        /* Set up strgetc() */
  10662.                     while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  10663.                         if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {    /* (xprintc) */
  10664.                             signal(SIGINT,oldintr);
  10665.                             return(-1);
  10666.                         }
  10667.                     }
  10668.                     printf("\n");
  10669.                 }
  10670. #endif /* NOCSETS */
  10671.             }
  10672.         }
  10673.     debug(F110,"FTP RCVD ",ftp_reply_str,0);
  10674.  
  10675.     if (fc == GRF_FEAT) {        /* Parsing FEAT command response? */
  10676.         if (count == 0 && n == '2') {
  10677.         int i;            /* (Re)-init server FEATure table */
  10678.         debug(F100,"ftp getreply clearing feature table","",0);
  10679.         for (i = 0; i < 16; i++)
  10680.           sfttab[i] = 0;
  10681.         } else {
  10682.         parsefeat((char *)ftp_reply_str);
  10683.         }
  10684.     }
  10685.         if (auth_type &&
  10686. #ifdef CK_SSL
  10687.             !ssl_ftp_active_flag &&
  10688. #endif /* CK_SSL */
  10689.              !ibuf[0] && n != '6') {
  10690.             signal(SIGINT,oldintr);
  10691.             return(getreply(expecteof,lcs,rcs,vbm,auth));
  10692.         }
  10693.         ibuf[0] = obuf[i] = '\0';
  10694.         if (ftpcode && n == '6')
  10695.           if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  10696.               printf("Unknown reply: %d %s\n", ftpcode, obuf);
  10697.               n = '5';
  10698.           } else safe = (ftpcode == 631);
  10699.         if (obuf[0]                     /* if there is a string to decode */
  10700. #ifdef CK_SSL
  10701.             && !ssl_ftp_active_flag     /* and not SSL/TLS */
  10702. #endif /* CK_SSL */
  10703.             ) {
  10704.             if (!auth_type) {
  10705.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  10706.                 n = '5';
  10707.             }
  10708. #ifndef CK_ENCRYPTION
  10709.             else if (ftpcode == 632) {
  10710.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10711.                 n = '5';
  10712.             }
  10713. #endif /* CK_ENCRYPTION */
  10714. #ifdef NOCONFIDENTIAL
  10715.             else if (ftpcode == 633) {
  10716.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10717.                 n = '5';
  10718.             }
  10719. #endif /* NOCONFIDENTIAL */
  10720.             else {
  10721.                 int len = FTP_BUFSIZ;
  10722.                 if ((kerror = radix_encode((CHAR *)obuf,
  10723.                                            (CHAR *)ibuf,
  10724.                                            0,
  10725.                                            &len,
  10726.                                            RADIX_DECODE))
  10727.                     ) {
  10728.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  10729.                            ftpcode, radix_error(kerror), obuf);
  10730.                     n = '5';
  10731.                 }
  10732. #ifdef FTP_SRP
  10733.                 else if (strcmp(auth_type, "SRP") == 0) {
  10734.                     int outlen;
  10735.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  10736.                                         (CHAR *) ibuf, len);
  10737.                     if (outlen < 0) {
  10738.                         printf("Warning: %d reply %s!\n",
  10739.                                ftpcode, safe ? "modified" : "garbled");
  10740.                         n = '5';
  10741.                     } else {
  10742.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  10743.                         if (ftp_deb)
  10744.                           printf("%c:", safe ? 'S' : 'P');
  10745.                         continue;
  10746.                     }
  10747.                 }
  10748. #endif /* FTP_SRP */
  10749. #ifdef FTP_KRB4
  10750.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  10751.                     if (safe) {
  10752.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  10753. #ifdef KRB524
  10754.                                              ftp_cred.session,
  10755. #else /* KRB524 */
  10756.                                              &ftp_cred.session,
  10757. #endif /* KRB524 */
  10758.                                              &hisctladdr,
  10759.                                              &myctladdr,
  10760.                                              &ftp_msg_data
  10761.                                              );
  10762.                     } else {
  10763.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  10764.                                              ftp_sched,
  10765. #ifdef KRB524
  10766.                                              ftp_cred.session,
  10767. #else /* KRB524 */
  10768.                                              &ftp_cred.session,
  10769. #endif /* KRB524 */
  10770.                                              &hisctladdr,
  10771.                                              &myctladdr,
  10772.                                              &ftp_msg_data
  10773.                                              );
  10774.                     }
  10775.                     if (kerror != KSUCCESS) {
  10776.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  10777.                                safe ? "modified" : "garbled",
  10778.                                safe ? "safe" : "priv",
  10779.                                krb_get_err_text(kerror));
  10780.                         n = '5';
  10781.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  10782.                         kerror = KFAILURE;
  10783.                         n = '5';
  10784.                         printf("reply data too large for buffer\n");
  10785.                     } else {
  10786.                         if (ftp_deb)
  10787.                           printf("%c:", safe ? 'S' : 'P');
  10788.                         memcpy(ibuf,ftp_msg_data.app_data,
  10789.                                ftp_msg_data.app_length);
  10790.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  10791.                                   FTP_BUFSIZ - ftp_msg_data.app_length);
  10792.                         continue;
  10793.                     }
  10794.                 }
  10795. #endif /* FTP_KRB4 */
  10796. #ifdef FTP_GSSAPI
  10797.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  10798.                     gss_buffer_desc xmit_buf, msg_buf;
  10799.                     OM_uint32 maj_stat, min_stat;
  10800.                     int conf_state;
  10801.                     xmit_buf.value = ibuf;
  10802.                     xmit_buf.length = len;
  10803.                     /* decrypt/verify the message */
  10804.                     conf_state = safe;
  10805.                     maj_stat = gss_unseal(&min_stat, gcontext,
  10806.                                           &xmit_buf, &msg_buf,
  10807.                                           &conf_state, NULL);
  10808.                     if (maj_stat != GSS_S_COMPLETE) {
  10809.                         user_gss_error(maj_stat, min_stat,
  10810.                                        "failed unsealing reply");
  10811.                         n = '5';
  10812.                     } else {
  10813.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  10814.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  10815.                                   FTP_BUFSIZ-msg_buf.length);
  10816.                         gss_release_buffer(&min_stat,&msg_buf);
  10817.                         if (ftp_deb)
  10818.                           printf("%c:", safe ? 'S' : 'P');
  10819.                         continue;
  10820.                     }
  10821.                 }
  10822. #endif /* FTP_GSSAPI */
  10823.                 /* Other auth types go here... */
  10824.             }
  10825.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  10826.                    !xquiet && (vbm || (!auth && (n == '4' || n == '5')))) {
  10827. #ifdef NOCSETS
  10828.             printf("%c",c);
  10829. #else
  10830.             if (xlate) {
  10831.                 xlatec(0,c,rcs,lcs);
  10832.             } else {
  10833.                 printf("%c",c);
  10834.             }
  10835. #endif /* NOCSETS */
  10836.             fflush (stdout);
  10837.         }
  10838.         if (continuation && ftpcode != originalcode) {
  10839.             if (originalcode == 0)
  10840.               originalcode = ftpcode;
  10841.             continue;
  10842.         }
  10843.         *cp = '\0';
  10844.         if (n != '1')
  10845.           cpend = 0;
  10846.         signal(SIGINT,oldintr);
  10847.         if (ftpcode == 421 || originalcode == 421) {
  10848.         lostpeer();
  10849.         if (!xquiet && !ftp_deb)
  10850.           printf("%s\n",reply_buf);
  10851.         }
  10852.         if ((cancelfile != 0) &&
  10853. #ifndef ULTRIX3
  10854.             /* Ultrix 3.0 cc objects violently to this clause */
  10855.             (oldintr != cmdcancel) &&
  10856. #endif /* ULTRIX3 */
  10857.             (oldintr != SIG_IGN)) {
  10858.             if (oldintr)
  10859.               (*oldintr)(SIGINT);
  10860.         }
  10861.         if (reply_parse) {
  10862.             *reply_ptr = '\0';
  10863.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  10864.                 reply_parse = reply_ptr + strlen(reply_parse);
  10865.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  10866.                   *reply_ptr = '\0';
  10867.             } else
  10868.               reply_parse = reply_ptr;
  10869.         }
  10870.         while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
  10871.           *cp-- = NUL;
  10872.         debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  10873.         return(n - '0');
  10874.     } /* for (;;) */
  10875. }
  10876.  
  10877. #ifdef BSDSELECT
  10878. static int
  10879. #ifdef CK_ANSIC
  10880. empty(fd_set * mask, int sec)
  10881. #else
  10882. empty(mask, sec) fd_set * mask; int sec;
  10883. #endif /* CK_ANSIC */
  10884. {
  10885.     struct timeval t;
  10886.     t.tv_sec = (long) sec;
  10887.     t.tv_usec = 0L;
  10888.     debug(F100,"ftp empty calling select...","",0);
  10889. #ifdef INTSELECT
  10890.     x = select(32, (int *)mask, NULL, NULL, &t);
  10891. #else
  10892.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  10893. #endif /* INTSELECT */
  10894.     debug(F101,"ftp empty select","",x);
  10895.     return(x);
  10896. }
  10897. #else /* BSDSELECT */
  10898. #ifdef IBMSELECT
  10899. static int
  10900. empty(mask, cnt, sec) int * mask, sec;
  10901.                       int   cnt;
  10902. {
  10903.     return(select(mask,cnt,0,0,sec*1000));
  10904. }
  10905. #endif /* IBMSELECT */
  10906. #endif /* BSDSELECT */
  10907.  
  10908. static sigtype
  10909. cancelsend(sig) int sig; {
  10910.     havesigint++;
  10911.     cancelgroup++;
  10912.     cancelfile = 0;
  10913.     printf(" Canceled...\n");
  10914.     secure_getc(0,1);            /* Initialize net input buffers */
  10915.     debug(F100,"ftp cancelsend caught SIGINT ","",0);
  10916.     fflush(stdout);
  10917. #ifndef OS2
  10918.     longjmp(sendcancel, 1);
  10919. #else
  10920.     PostCtrlCSem();
  10921. #endif /* OS2 */
  10922. }
  10923.  
  10924. static VOID
  10925. #ifdef CK_ANSIC
  10926. secure_error(char *fmt, ...)
  10927. #else
  10928. /* VARARGS1 */
  10929. secure_error(fmt, p1, p2, p3, p4, p5)
  10930.    char *fmt; int p1, p2, p3, p4, p5;
  10931. #endif /* CK_ANSIC */
  10932. {
  10933. #ifdef CK_ANSIC
  10934.     va_list ap;
  10935.  
  10936.     va_start(ap, fmt);
  10937.     vfprintf(stderr, fmt, ap);
  10938.     va_end(ap);
  10939. #else
  10940.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  10941. #endif
  10942.     fprintf(stderr, "\n");
  10943. }
  10944.  
  10945. /*
  10946.  * Internal form of settype; changes current type in use with server
  10947.  * without changing our notion of the type for data transfers.
  10948.  * Used to change to and from ascii for listings.
  10949.  */
  10950. static VOID
  10951. changetype(newtype, show) int newtype, show; {
  10952.     int rc;
  10953.     char * s;
  10954.  
  10955.     if ((newtype == curtype) && typesent++)
  10956.       return;
  10957.     switch (newtype) {
  10958.       case FTT_ASC:
  10959.         s = "A";
  10960.         break;
  10961.       case FTT_BIN:
  10962.         s = "I";
  10963.         break;
  10964.       case FTT_TEN:
  10965.         s = "L 8";
  10966.         break;
  10967.       default:
  10968.         s = "I";
  10969.         break;
  10970.     }
  10971.     rc = ftpcmd("TYPE",s,-1,-1,show);
  10972.     if (rc == REPLY_COMPLETE)
  10973.       curtype = newtype;
  10974. }
  10975.  
  10976. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  10977.  
  10978. static int ftpsndret = 0;
  10979. static struct _ftpsnd {
  10980.     sig_t oldintr, oldintp;
  10981.     int            reply;
  10982.     int            incs,
  10983.                    outcs;
  10984.     char *         cmd, * local, * remote;
  10985.     int            bytes;
  10986.     int            restart;
  10987.     int            xlate;
  10988.     char *         lmode;
  10989. } ftpsnd;
  10990.  
  10991. static VOID
  10992. #ifdef CK_ANSIC
  10993. doftpsend(void * threadinfo)
  10994. #else
  10995. doftpsend(threadinfo) VOID * threadinfo;
  10996. #endif
  10997. {
  10998. #ifdef NTSIG
  10999.     if (threadinfo) {                   /* Thread local storage... */
  11000.         TlsSetValue(TlsIndex,threadinfo);
  11001.         debug(F100, "doftpsend called with threadinfo block","", 0);
  11002.     } else debug(F100, "doftpsend - threadinfo is NULL", "", 0);
  11003. #endif /* NTSIG */
  11004. #ifdef CK_LOGIN
  11005. #ifdef IKSD
  11006. #ifdef NT
  11007.     if (inserver)
  11008.       setntcreds();
  11009. #endif /* NT */
  11010. #endif /* IKSD */
  11011. #endif /* CK_LOGIN */
  11012.  
  11013.     if (initconn()) {
  11014. #ifndef NOHTTP
  11015.         int y = -1;
  11016.         debug(F101,"doftpsend","tcp_http_proxy",tcp_http_proxy);
  11017.  
  11018.        /*  If the connection failed and we are using an HTTP Proxy
  11019.         *  and the reason for the failure was an authentication
  11020.         *  error, then we need to give the user to ability to
  11021.         *  enter a username and password, just like a browser.
  11022.         *
  11023.         *  I tried to do all of this within the netopen() call
  11024.         *  but it is much too much work.
  11025.         */
  11026.         while (y != 0 && tcp_http_proxy != NULL ) {
  11027.  
  11028.             if (tcp_http_proxy_errno == 401 ||
  11029.                  tcp_http_proxy_errno == 407 ) {
  11030.                 char uid[UIDBUFLEN];
  11031.                 char pwd[PWDSIZ];
  11032.                 struct txtbox tb[2];
  11033.                 int ok;
  11034.  
  11035.                 tb[0].t_buf = uid;
  11036.                 tb[0].t_len = UIDBUFLEN;
  11037.                 tb[0].t_lbl = "Proxy Userid: ";
  11038.                 tb[0].t_dflt = NULL;
  11039.                 tb[0].t_echo = 1;
  11040.                 tb[1].t_buf = pwd;
  11041.                 tb[1].t_len = 256;
  11042.                 tb[1].t_lbl = "Proxy Passphrase: ";
  11043.                 tb[1].t_dflt = NULL;
  11044.                 tb[1].t_echo = 2;
  11045.  
  11046.                 ok = uq_mtxt("Proxy Server Authentication Required\n",
  11047.                               NULL, 2, tb);
  11048.                 if (ok && uid[0]) {
  11049.                     char * proxy_user, * proxy_pwd;
  11050.  
  11051.                     proxy_user = tcp_http_proxy_user;
  11052.                     proxy_pwd  = tcp_http_proxy_pwd;
  11053.  
  11054.                     tcp_http_proxy_user = uid;
  11055.                     tcp_http_proxy_pwd = pwd;
  11056.  
  11057.                     y = initconn();
  11058.  
  11059.                     debug(F101,"doftpsend","initconn",y);
  11060.                     memset(pwd,0,PWDSIZ);
  11061.                     tcp_http_proxy_user = proxy_user;
  11062.                     tcp_http_proxy_pwd = proxy_pwd;
  11063.                 } else
  11064.                     break;
  11065.             } else
  11066.                 break;
  11067.         }
  11068.  
  11069.         if ( y != 0 ) {
  11070. #endif /* NOHTTP */
  11071.             signal(SIGINT, ftpsnd.oldintr);
  11072. #ifdef SIGPIPE
  11073.             if (ftpsnd.oldintp)
  11074.               signal(SIGPIPE, ftpsnd.oldintp);
  11075. #endif /* SIGPIPE */
  11076.             ftpcode = -1;
  11077.             zclose(ZIFILE);
  11078.             ftpsndret = -1;
  11079. #ifdef NTSIG
  11080.             ckThreadEnd(threadinfo);
  11081. #endif /* NTSIG */
  11082.             return;
  11083. #ifndef NOHTTP
  11084.         }
  11085. #endif /* NOHTTP */
  11086.     }
  11087.     ftpsndret = 0;
  11088. #ifdef NTSIG
  11089.      ckThreadEnd(threadinfo);
  11090. #endif /* NTSIG */
  11091. }
  11092.  
  11093. static VOID
  11094. #ifdef CK_ANSIC
  11095. failftpsend(void * threadinfo)
  11096. #else
  11097. failftpsend(threadinfo) VOID * threadinfo;
  11098. #endif /* CK_ANSIC */
  11099. {
  11100. #ifdef NTSIG
  11101.     if (threadinfo) {                   /* Thread local storage... */
  11102.         TlsSetValue(TlsIndex,threadinfo);
  11103.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11104.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11105. #endif /* NTSIG */
  11106. #ifdef CK_LOGIN
  11107. #ifdef IKSD
  11108. #ifdef NT
  11109.     if (inserver)
  11110.       setntcreds();
  11111. #endif /* NT */
  11112. #endif /* IKSD */
  11113. #endif /* CK_LOGIN */
  11114.  
  11115.     while (cpend) {
  11116.         ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11117.         debug(F111,"ftp sendrequest getreply","null command",ftpsnd.reply);
  11118.     }
  11119.     if (data >= 0) {
  11120. #ifdef CK_SSL
  11121.         if (ssl_ftp_data_active_flag) {
  11122.             SSL_shutdown(ssl_ftp_data_con);
  11123.             SSL_free(ssl_ftp_data_con);
  11124.             ssl_ftp_data_active_flag = 0;
  11125.             ssl_ftp_data_con = NULL;
  11126.         }
  11127. #endif /* CK_SSL */
  11128. #ifdef TCPIPLIB
  11129.         socket_close(data);
  11130. #else /* TCPIPLIB */
  11131. #ifdef USE_SHUTDOWN
  11132.         shutdown(data, 1+1);
  11133. #endif /* USE_SHUTDOWN */
  11134.         close(data);
  11135. #endif /* TCPIPLIB */
  11136.         data = -1;
  11137.         globaldin = -1;
  11138.     }
  11139.     if (ftpsnd.oldintr)
  11140.         signal(SIGINT,ftpsnd.oldintr);
  11141. #ifdef SIGPIPE
  11142.     if (ftpsnd.oldintp)
  11143.         signal(SIGPIPE,ftpsnd.oldintp);
  11144. #endif /* SIGPIPE */
  11145.     ftpcode = -1;
  11146. #ifndef OS2
  11147.     /* TEST ME IN K95 */
  11148.     if (havesigint) {
  11149.     havesigint = 0;
  11150.     debug(F100,"ftp failftpsend chain to trap()...","",0);
  11151.     if (ftpsnd.oldintr != SIG_IGN)
  11152.       (*ftpsnd.oldintr)(SIGINT);
  11153.     /* NOTREACHED (I hope!) */
  11154.     debug(F100,"ftp failftpsend return from trap()...","",0);
  11155.     }
  11156. #endif /* OS2 */
  11157. }
  11158.  
  11159. static VOID
  11160. #ifdef CK_ANSIC
  11161. failftpsend2(void * threadinfo)
  11162. #else
  11163. failftpsend2(threadinfo) VOID * threadinfo;
  11164. #endif /* CK_ANSIC */
  11165. {
  11166. #ifdef NTSIG
  11167.     if (threadinfo) {                   /* Thread local storage... */
  11168.         TlsSetValue(TlsIndex,threadinfo);
  11169.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11170.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11171. #endif /* NTSIG */
  11172. #ifdef CK_LOGIN
  11173. #ifdef IKSD
  11174. #ifdef NT
  11175.     if (inserver)
  11176.       setntcreds();
  11177. #endif /* NT */
  11178. #endif /* IKSD */
  11179. #endif /* CK_LOGIN */
  11180.  
  11181.     debug(F101,"ftp sendrequest canceled","",ftpsnd.bytes);
  11182.     tfc += ffc;
  11183. #ifdef GFTIMER
  11184.     fpfsecs = gftimer();
  11185. #endif /* GFTIMER */
  11186.     zclose(ZIFILE);
  11187. #ifdef PIPESEND
  11188.     if (sndfilter)
  11189.       pipesend = 0;
  11190. #endif /* PIPESEND */
  11191.     signal(SIGINT, ftpsnd.oldintr);
  11192. #ifdef SIGPIPE
  11193.     if (ftpsnd.oldintp)
  11194.       signal(SIGPIPE, ftpsnd.oldintp);
  11195. #endif /* SIGPIPE */
  11196.     if (!cpend) {
  11197.         ftpcode = -1;
  11198.         ftpsndret = -1;
  11199. #ifdef NTSIG
  11200.         ckThreadEnd(threadinfo);
  11201. #endif /* NTSIG */
  11202.         return;
  11203.     }
  11204.     if (data >= 0) {
  11205. #ifdef CK_SSL
  11206.         if (ssl_ftp_data_active_flag) {
  11207.             SSL_shutdown(ssl_ftp_data_con);
  11208.             SSL_free(ssl_ftp_data_con);
  11209.             ssl_ftp_data_active_flag = 0;
  11210.             ssl_ftp_data_con = NULL;
  11211.         }
  11212. #endif /* CK_SSL */
  11213. #ifdef TCPIPLIB
  11214.         socket_close(data);
  11215. #else /* TCPIPLIB */
  11216. #ifdef USE_SHUTDOWN
  11217.         shutdown(data, 1+1);
  11218. #endif /* USE_SHUTDOWN */
  11219.         close(data);
  11220. #endif /* TCPIPLIB */
  11221.         data = -1;
  11222.         globaldin = -1;
  11223.     }
  11224.     if (dout) {
  11225. #ifdef TCPIPLIB
  11226.         socket_close(dout);
  11227. #else /* TCPIPLIB */
  11228. #ifdef USE_SHUTDOWN
  11229.         shutdown(dout, 1+1);
  11230. #endif /* USE_SHUTDOWN */
  11231.         close(dout);
  11232. #endif /* TCPIPLIB */
  11233.     }
  11234.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11235.     ftpcode = -1;
  11236.     ftpsndret = -1;
  11237.  
  11238. #ifndef OS2
  11239.     /* TEST ME IN K95 */
  11240.     if (havesigint) {
  11241.     havesigint = 0;
  11242.     debug(F100,"ftp failftpsend2 chain to trap()...","",0);
  11243.     if (ftpsnd.oldintr != SIG_IGN)
  11244.       (*ftpsnd.oldintr)(SIGINT);
  11245.     /* NOTREACHED (I hope!) */
  11246.     debug(F100,"ftp failftpsend2 return from trap()...","",0);
  11247.     }
  11248. #endif /* OS2 */
  11249. }
  11250.  
  11251. static VOID
  11252. #ifdef CK_ANSIC
  11253. doftpsend2(void * threadinfo)
  11254. #else
  11255. doftpsend2(threadinfo) VOID * threadinfo;
  11256. #endif
  11257. {
  11258.     register int c, d = 0;
  11259.     int n, t, x, notafile, unique = 0;
  11260.     char *buf, *bufp;
  11261.     
  11262. #ifdef NTSIG
  11263.     if (threadinfo) {                   /* Thread local storage... */
  11264.         TlsSetValue(TlsIndex,threadinfo);
  11265.         debug(F100, "doftpsend2 called with threadinfo block","", 0);
  11266.     } else debug(F100, "doftpsend2 - threadinfo is NULL", "", 0);
  11267. #endif /* NTSIG */
  11268. #ifdef CK_LOGIN
  11269. #ifdef IKSD
  11270. #ifdef NT
  11271.     if (inserver)
  11272.       setntcreds();
  11273. #endif /* NT */
  11274. #endif /* IKSD */
  11275. #endif /* CK_LOGIN */
  11276.  
  11277.     buf = ftpsndbuf;            /* (not on stack) */
  11278.  
  11279.     unique = strcmp(ftpsnd.cmd,"STOU") ? 0 : 1;
  11280.     notafile = sndarray || pipesend;
  11281.  
  11282. #ifdef FTP_RESTART
  11283.     if (ftpsnd.restart && ((curtype == FTT_BIN) || (alike > 0))) {
  11284.         char * p;
  11285.         changetype(FTT_BIN,0);          /* Change to binary */
  11286.  
  11287.         /* Ask for remote file's size */
  11288.         x = ftpcmd("SIZE",ftpsnd.remote,ftpsnd.incs,ftpsnd.outcs,ftp_vbm);
  11289.  
  11290.         if (x == REPLY_COMPLETE) {      /* Have ftpsnd.reply */
  11291.             p = &ftp_reply_str[4];      /* Parse it */
  11292.             while (isdigit(*p)) {
  11293.                 sendstart = sendstart * 10 + (int)(*p - '0');
  11294.                 p++;
  11295.             }
  11296.             if (*p && *p != CR) {       /* Bad number */
  11297.                 debug(F110,"doftpsend2 bad size",ftp_reply_str,0);
  11298.                 sendstart = 0L;
  11299.             } else if (sendstart > fsize) { /* Remote file bigger than local */
  11300.                 debug(F110,"doftpsend2 big size",ckltoa(fsize),sendstart);
  11301.                 sendstart = 0L;
  11302.             }
  11303.         /* Local is newer */
  11304.             debug(F111,"doftpsend2 size",ftpsnd.remote,sendstart);
  11305.             if (chkmodtime(ftpsnd.local,ftpsnd.remote,0) == 2) {
  11306.                 debug(F110,"doftpsend2 date mismatch",ftp_reply_str,0);
  11307.                 sendstart = 0L;         /* Send the whole file */
  11308.             }
  11309.         }
  11310.         changetype(ftp_typ,0);          /* Change back to appropriate type */
  11311.         if (sendstart > 0L) {           /* Still restarting? */
  11312.             if (sendstart == fsize) {   /* Same size - no need to send */
  11313.                 debug(F111,"doftpsend2 /restart SKIP",fsize,sendstart);
  11314.                 zclose(ZIFILE);
  11315.                 ftpsndret = SKP_RES;
  11316. #ifdef NTSIG
  11317.                 ckThreadEnd(threadinfo);
  11318. #endif /* NTSIG */
  11319.                 return;
  11320.             }
  11321.             errno = 0;                  /* Restart needed, seek to the spot */
  11322.             if (zfseek((long)sendstart) < 0) {
  11323.                 debug(F111,"doftpsend2 zfseek fails",
  11324.               ftpsnd.local,sendstart);
  11325.                 fprintf(stderr, "FSEEK: %s: %s\n", ftpsnd.local, ck_errstr());
  11326.                 sendstart = 0;
  11327.                 zclose(ZIFILE);
  11328.                 ftpsndret = -1;
  11329. #ifdef NTSIG
  11330.                 ckThreadEnd(threadinfo);
  11331. #endif /* NTSIG */
  11332.                 return;
  11333.             }
  11334. #ifdef COMMENT
  11335.             debug(F111,"doftpsend2 zfseek ok",ftpsnd.local,sendstart);
  11336.             x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  11337.             if (x != REPLY_CONTINUE) {
  11338.                 sendstart = 0;
  11339.                 zclose(ZIFILE);
  11340.                 ftpsndret = -1;
  11341. #ifdef NTSIG
  11342.                 ckThreadEnd(threadinfo);
  11343. #endif /* NTSIG */
  11344.                 return;
  11345.             } else {
  11346.                 ftpsnd.cmd = "STOR";
  11347.             }
  11348. #else
  11349.             sendmode = SM_RESEND;
  11350.             ftpsnd.cmd = "APPE";
  11351. #endif /* COMMENT */
  11352.             /* sendstart = 0L; */
  11353.         }
  11354.     }
  11355. #endif /* FTP_RESTART */
  11356.  
  11357.     if (unique && !stouarg)        /* If we know STOU accepts no arg */
  11358.       ftpsnd.remote = NULL;        /* don't include one. */
  11359.  
  11360.     x = ftpcmd(ftpsnd.cmd, ftpsnd.remote, ftpsnd.incs, ftpsnd.outcs, ftp_vbm);
  11361.     debug(F111,"doftpsend2 ftpcode",ftpsnd.cmd,ftpcode);
  11362.  
  11363.     if (x != REPLY_PRELIM && unique) {
  11364.     /*
  11365.       RFC959 says STOU does not take an argument.  But every FTP server
  11366.       I've encountered but one accepts the arg and constructs the unique
  11367.       name from it, which is better than making up a totally random name
  11368.       for the file, which is what RFC959 calls for.  Especially because
  11369.       there is no way for the client to find out the name chosen by the
  11370.       server.  So we try STOU with the argument first, which works with
  11371.       most servers, and if it fails we retry it without the arg, for
  11372.       the benefit of the one picky server that is not "liberal in what
  11373.       it accepts" UNLESS the first STOU got a 502 code ("not implemented")
  11374.       which means STOU is not accepted, period.
  11375.     */
  11376.     if ((x == 5) && stouarg && (ftpcode != 502)) {
  11377.         x = ftpcmd(ftpsnd.cmd,NULL,ftpsnd.incs,ftpsnd.outcs,ftp_vbm); 
  11378.         if (x == REPLY_PRELIM)    /* If accepted */
  11379.           stouarg = 0;        /* flag no STOU arg for this server */
  11380.     }
  11381.     }
  11382.     if (x != REPLY_PRELIM) {
  11383.         signal(SIGINT, ftpsnd.oldintr);
  11384. #ifdef SIGPIPE
  11385.         if (ftpsnd.oldintp)
  11386.           signal(SIGPIPE, ftpsnd.oldintp);
  11387. #endif /* SIGPIPE */
  11388.         zclose(ZIFILE);
  11389. #ifdef PIPESEND
  11390.         if (sndfilter)
  11391.           pipesend = 0;
  11392. #endif /* PIPESEND */
  11393.         ftpsndret = -1;
  11394. #ifdef NTSIG
  11395.         ckThreadEnd(threadinfo);
  11396. #endif /* NTSIG */
  11397.         return;
  11398.     }
  11399.     dout = dataconn(ftpsnd.lmode);             /* Get data connection */
  11400.     if (dout == -1) {
  11401.         failftpsend2(threadinfo);
  11402. #ifdef NTSIG
  11403.         ckThreadEnd(threadinfo);
  11404. #endif /* NTSIG */
  11405.         return;
  11406.     }
  11407.     /* Initialize per-file stats */
  11408.     ffc = 0L;                           /* Character counter */
  11409.     cps = oldcps = 0L;                  /* Thruput */
  11410. #ifdef GFTIMER
  11411.     rftimer();                          /* reset f.p. timer */
  11412. #endif /* GFTIMER */
  11413.  
  11414. #ifdef SIGPIPE
  11415.     ftpsnd.oldintp = signal(SIGPIPE, SIG_IGN);
  11416. #endif /* SIGPIPE */
  11417.     switch (curtype) {
  11418.       case FTT_BIN:                     /* Binary mode */
  11419.       case FTT_TEN:
  11420.         errno = d = 0;
  11421.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ - 1)) > 0 && !cancelfile) {
  11422.             ftpsnd.bytes += n;
  11423.             ffc += n;
  11424.             debug(F111,"doftpsend2 zxin",ckltoa(n),ffc);
  11425.             hexdump("doftpsend2 zxin",buf,16);
  11426. #ifdef CK_SSL
  11427.             if (ssl_ftp_data_active_flag) {
  11428.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11429.                     if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  11430.                       break;
  11431.                     spackets++;
  11432.                     pktnum++;
  11433.                     if (fdispla != XYFD_B) {
  11434.                         spktl = d;
  11435.                         ftscreen(SCR_PT,'D',spackets,NULL);
  11436.                     }
  11437.                 }
  11438.             } else {
  11439. #endif /* CK_SSL */
  11440.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11441.                     if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  11442.                         || iscanceled())
  11443.                       break;
  11444.                     spackets++;
  11445.                     pktnum++;
  11446.                     if (fdispla != XYFD_B) {
  11447.                         spktl = d;
  11448.                         ftscreen(SCR_PT,'D',spackets,NULL);
  11449.                     }
  11450.                 }
  11451. #ifdef CK_SSL
  11452.             }
  11453. #endif /* CK_SSL */
  11454.             if (d <= 0)
  11455.               break;
  11456.         }
  11457.         if (n < 0)
  11458.           fprintf(stderr, "local: %s: %s\n", ftpsnd.local, ck_errstr());
  11459.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  11460.             if (d == -1 && errno && errno != EPIPE)
  11461.               perror("netout");
  11462.             ftpsnd.bytes = -1;
  11463.         }
  11464.         break;
  11465.  
  11466.       case FTT_ASC:                     /* Text mode */
  11467. #ifndef NOCSETS
  11468.         if (ftpsnd.xlate) {             /* With translation */
  11469.             initxlate(ftpsnd.incs,ftpsnd.outcs);
  11470.             while (!cancelfile) {
  11471.                 if ((c0 = xgnbyte(FC_UCS2,ftpsnd.incs,NULL)) < 0) break;
  11472.                 if ((x = xpnbyte(c0,TC_UCS2,ftpsnd.outcs,xxout)) < 0) break;
  11473.             }
  11474.         } else {
  11475. #endif /* NOCSETS */
  11476.             /* Text mode, no translation */
  11477.             while (((c = zminchar()) > -1) && !cancelfile) {
  11478.                 ffc++;
  11479.                 if (c == '\012') {
  11480.                     if (zzout(dout,(CHAR)'\015') < 0)
  11481.                       break;
  11482.                     ftpsnd.bytes++;
  11483.                 }
  11484.                 if (zzout(dout,(CHAR)c) < 0)
  11485.                   break;
  11486.                 ftpsnd.bytes++;
  11487.             }
  11488.             d = 0;
  11489. #ifndef NOCSETS
  11490.         }
  11491. #endif /* NOCSETS */
  11492.         if (dout == -1 || (d = secure_flush(dout)) < 0) {
  11493.             if (d == -1 && errno && errno != EPIPE)
  11494.               perror("netout");
  11495.             ftpsnd.bytes = -1;
  11496.         }
  11497.         break;
  11498.     }
  11499.     tfc += ffc;                         /* Total file chars */
  11500. #ifdef GFTIMER
  11501.     fpfsecs = gftimer();
  11502. #endif /* GFTIMER */
  11503.     zclose(ZIFILE);                     /* Close input file */
  11504. #ifdef PIPESEND
  11505.     if (sndfilter)                      /* Undo this (it's per file) */
  11506.       pipesend = 0;
  11507. #endif /* PIPESEND */
  11508.  
  11509. #ifdef TCPIPLIB
  11510.     socket_close(dout);                 /* Close data connection */
  11511. #else /* TCPIPLIB */
  11512. #ifdef USE_SHUTDOWN
  11513.     shutdown(dout, 1+1);
  11514. #endif /* USE_SHUTDOWN */
  11515.     close(dout);
  11516. #endif /* TCPIPLIB */
  11517.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11518.     signal(SIGINT, ftpsnd.oldintr);            /* Put back interrupts */
  11519. #ifdef SIGPIPE
  11520.     if (ftpsnd.oldintp)
  11521.       signal(SIGPIPE, ftpsnd.oldintp);
  11522. #endif /* SIGPIPE */
  11523.     if (ftpsnd.reply == REPLY_TRANSIENT || ftpsnd.reply == REPLY_ERROR) {
  11524.         debug(F101,"doftpsend2 ftpsnd.reply","",ftpsnd.reply);
  11525.         ftpsndret = -1;
  11526. #ifdef NTSIG
  11527.         ckThreadEnd(threadinfo);
  11528. #endif /* NTSIG */
  11529.         return;
  11530.     } else if (cancelfile) {
  11531.         debug(F101,"doftpsend2 canceled","",ftpsnd.bytes);
  11532.         ftpsndret = -1;
  11533. #ifdef NTSIG
  11534.         ckThreadEnd(threadinfo);
  11535. #endif /* NTSIG */
  11536.         return;
  11537.     }
  11538.     debug(F101,"doftpsend2 ok","",ftpsnd.bytes);
  11539.     ftpsndret = 0;
  11540. #ifdef NTSIG
  11541.      ckThreadEnd(threadinfo);
  11542. #endif /* NTSIG */
  11543. }
  11544.  
  11545. static int
  11546. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  11547.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  11548. {
  11549.     if (!remote) remote = "";           /* Check args */
  11550.     if (!*remote) remote = local;
  11551.     if (!local) local = "";
  11552.     if (!*local) return(-1);
  11553.     if (!cmd) cmd = "";
  11554.     if (!*cmd) cmd = "STOR";
  11555.  
  11556.     debug(F111,"ftp sendrequest restart",local,restart);
  11557.  
  11558.     nout = 0;                           /* Init output buffer count */
  11559.     ftpsnd.bytes = 0;                   /* File input byte count */
  11560.     dout = -1;
  11561.  
  11562. #ifdef FTP_PROXY
  11563.     if (proxy) {
  11564.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  11565.         return(0);
  11566.     }
  11567. #endif /* FTP_PROXY */
  11568.  
  11569.     changetype(ftp_typ,0);              /* Change type for this file */
  11570.  
  11571.     ftpsnd.oldintr = NULL;        /* Set up interrupt handler */
  11572.     ftpsnd.oldintp = NULL;
  11573.     ftpsnd.restart = restart;
  11574.     ftpsnd.xlate = xlate;
  11575.     ftpsnd.lmode = "wb";
  11576.  
  11577. #ifdef PIPESEND                         /* Use Kermit API for file i/o... */
  11578.     if (sndfilter) {
  11579.         char * p = NULL, * q;
  11580. #ifndef NOSPL
  11581.         int n = CKMAXPATH;
  11582.         if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  11583.             q = p;
  11584.             debug(F110,"sendrequest pipesend filter",sndfilter,0);
  11585.             zzstring(sndfilter,&p,&n);
  11586.             debug(F111,"sendrequest pipename",q,n);
  11587.             if (n <= 0) {
  11588.                 printf("?Sorry, send filter + filename too long, %d max.\n",
  11589.                        CKMAXPATH
  11590.                        );
  11591.                 free(q);
  11592.                 return(-1);
  11593.             }
  11594.             ckstrncpy(filnam,q,CKMAXPATH+1);
  11595.             free(q);
  11596.             local = filnam;
  11597.         }
  11598. #endif /* NOSPL */
  11599.     }
  11600.  
  11601.     if (sndfilter)                      /* If sending thru a filter */
  11602.       pipesend = 1;                     /* set this for open and i/o */
  11603. #endif /* PIPESEND */
  11604.     
  11605.     if (openi(local) == 0)              /* Try to open the input file */
  11606.         return(-1);
  11607.  
  11608.     ftpsndret = 0;
  11609.     ftpsnd.incs = incs;
  11610.     ftpsnd.outcs = outcs;
  11611.     ftpsnd.cmd = cmd;
  11612.     ftpsnd.local = local;
  11613.     ftpsnd.remote = remote;
  11614.     ftpsnd.oldintr = signal(SIGINT, cancelsend);
  11615.     havesigint = 0;
  11616.  
  11617.     if (cc_execute(ckjaddr(sendcancel), doftpsend, failftpsend) < 0)
  11618.       return(-1);
  11619.     if (ftpsndret < 0)
  11620.       return(-1);
  11621.     if (cc_execute(ckjaddr(sendcancel), doftpsend2, failftpsend2) < 0)
  11622.       return(-1);
  11623.  
  11624.     return(ftpsndret);
  11625. }
  11626.  
  11627. static sigtype
  11628. cancelrecv(sig) int sig; {
  11629.     havesigint++;
  11630.     cancelfile = 0;
  11631.     cancelgroup++;
  11632.     secure_getc(0,1);            /* Initialize net input buffers */
  11633.     printf(" Canceling...\n");
  11634.     debug(F100,"ftp cancelrecv caught SIGINT","",0);
  11635.     fflush(stdout);
  11636.     if (fp_nml) {
  11637.         if (fp_nml != stdout)
  11638.           fclose(fp_nml);
  11639.         fp_nml = NULL;
  11640.     }
  11641. #ifndef OS2
  11642.     longjmp(recvcancel, 1);
  11643. #else
  11644.     PostCtrlCSem();
  11645. #endif /* OS2 */
  11646. }
  11647.  
  11648. /* Argumentless front-end for secure_getc() */
  11649.  
  11650. static int
  11651. netgetc() {
  11652.     return(secure_getc(globaldin,0));
  11653. }
  11654.  
  11655. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  11656.  
  11657. /*
  11658.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  11659.     -1: interrupted by user.
  11660.     -2: error opening or writing output file (reason in errno).
  11661.     -3: failure to make data connection.
  11662.     -4: network read error (reason in errno).
  11663. */
  11664.  
  11665. struct xx_ftprecv {
  11666.     int reply;
  11667.     int fcs;
  11668.     int rcs;
  11669.     int recover;
  11670.     int xlate;
  11671.     int din;
  11672.     int is_retr;
  11673.     sig_t oldintr, oldintp;
  11674.     char * cmd;
  11675.     char * local;
  11676.     char * remote;
  11677.     char * lmode;
  11678.     char * pipename;
  11679.     int    tcrflag;
  11680.     long   localsize;
  11681. };
  11682. static struct xx_ftprecv ftprecv;
  11683.  
  11684. static int ftprecvret = 0;
  11685.  
  11686. static VOID
  11687. #ifdef CK_ANSIC
  11688. failftprecv(VOID * threadinfo)
  11689. #else
  11690. failftprecv(threadinfo) VOID * threadinfo;
  11691. #endif /* CK_ANSIC */
  11692. {
  11693. #ifdef NTSIG
  11694.     if (threadinfo) {                   /* Thread local storage... */
  11695.         TlsSetValue(TlsIndex,threadinfo);
  11696.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11697.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11698. #endif /* NTSIG */
  11699.  
  11700. #ifdef CK_LOGIN
  11701. #ifdef IKSD
  11702. #ifdef NT
  11703.     if (inserver)
  11704.       setntcreds();
  11705. #endif /* NT */
  11706. #endif /* IKSD */
  11707. #endif /* CK_LOGIN */
  11708.  
  11709.     while (cpend) {
  11710.         ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  11711.     }
  11712.     if (data >= 0) {
  11713. #ifdef CK_SSL
  11714.         if (ssl_ftp_data_active_flag) {
  11715.             SSL_shutdown(ssl_ftp_data_con);
  11716.             SSL_free(ssl_ftp_data_con);
  11717.             ssl_ftp_data_active_flag = 0;
  11718.             ssl_ftp_data_con = NULL;
  11719.         }
  11720. #endif /* CK_SSL */
  11721. #ifdef TCPIPLIB
  11722.         socket_close(data);
  11723. #else /* TCPIPLIB */
  11724. #ifdef USE_SHUTDOWN
  11725.         shutdown(data, 1+1);
  11726. #endif /* USE_SHUTDOWN */
  11727.         close(data);
  11728. #endif /* TCPIPLIB */
  11729.         data = -1;
  11730.         globaldin = -1;
  11731.     }
  11732.     if (ftprecv.oldintr)
  11733.       signal(SIGINT, ftprecv.oldintr);
  11734.     ftpcode = -1;
  11735.     ftprecvret = -1;
  11736.  
  11737. #ifndef OS2
  11738.     /* TEST ME IN K95 */
  11739.     if (havesigint) {
  11740.     havesigint = 0;
  11741.     debug(F100,"ftp failftprecv chain to trap()...","",0);
  11742.     if (ftprecv.oldintr != SIG_IGN)
  11743.       (*ftprecv.oldintr)(SIGINT);
  11744.     /* NOTREACHED (I hope!) */
  11745.     debug(F100,"ftp failftprecv return from trap()...","",0);
  11746.     }
  11747. #endif /* OS2 */
  11748.     return;
  11749. }
  11750.  
  11751. static VOID
  11752. #ifdef CK_ANSIC
  11753. doftprecv(VOID * threadinfo)
  11754. #else
  11755. doftprecv(threadinfo) VOID * threadinfo;
  11756. #endif /* CK_ANSIC */
  11757. {
  11758. #ifdef NTSIG
  11759.     if (threadinfo) {                   /* Thread local storage... */
  11760.         TlsSetValue(TlsIndex,threadinfo);
  11761.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11762.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11763. #endif /* NTSIG */
  11764. #ifdef CK_LOGIN
  11765. #ifdef IKSD
  11766. #ifdef NT
  11767.     if (inserver)
  11768.       setntcreds();
  11769. #endif /* NT */
  11770. #endif /* IKSD */
  11771. #endif /* CK_LOGIN */
  11772.  
  11773. #ifndef COMMENT
  11774.     if (!out2screen && !ftprecv.pipename) {
  11775.     int x;
  11776.     char * local;
  11777.     local = ftprecv.local;
  11778.     x = zchko(local);
  11779.         if (x < 0) {
  11780.             if ((!dpyactive || ftp_deb))
  11781.               fprintf(stderr,
  11782.               "Temporary file %s: %s\n", ftprecv.local, ck_errstr());
  11783.             signal(SIGINT, ftprecv.oldintr);
  11784.             ftpcode = -2;
  11785.             ftprecvret = -1;
  11786. #ifdef NTSIG
  11787.             ckThreadEnd(threadinfo);
  11788. #endif /* NTSIG */
  11789.             return;
  11790.         }
  11791.     }
  11792. #endif /* COMMENT */
  11793.     changetype((!ftprecv.is_retr) ? FTT_ASC : ftp_typ, 0);
  11794.     if (initconn()) {                   /* Initialize the data connection */
  11795.         signal(SIGINT, ftprecv.oldintr);
  11796.         ftpcode = -1;
  11797.         ftprecvret = -3;
  11798. #ifdef NTSIG
  11799.         ckThreadEnd(threadinfo);
  11800. #endif /* NTSIG */
  11801.         return;
  11802.     }
  11803.     secure_getc(0,1);            /* Initialize net input buffers */
  11804.     ftprecvret = 0;
  11805.  
  11806. #ifdef NTSIG
  11807.     ckThreadEnd(threadinfo);
  11808. #endif /* NTSIG */
  11809. }
  11810.  
  11811. static VOID
  11812. #ifdef CK_ANSIC
  11813. failftprecv2(VOID * threadinfo)
  11814. #else
  11815. failftprecv2(threadinfo) VOID * threadinfo;
  11816. #endif /* CK_ANSIC */
  11817. {
  11818. #ifdef NTSIG
  11819.     if (threadinfo) {                   /* Thread local storage... */
  11820.         TlsSetValue(TlsIndex,threadinfo);
  11821.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11822.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11823. #endif /* NTSIG */
  11824. #ifdef CK_LOGIN
  11825. #ifdef IKSD
  11826. #ifdef NT
  11827.     if (inserver)
  11828.       setntcreds();
  11829. #endif /* NT */
  11830. #endif /* IKSD */
  11831. #endif /* CK_LOGIN */
  11832.  
  11833.     /* Cancel using RFC959 recommended IP,SYNC sequence  */
  11834.  
  11835.     debug(F100,"ftp recvrequest CANCEL","",0);
  11836. #ifdef GFTIMER
  11837.     fpfsecs = gftimer();
  11838. #endif /* GFTIMER */
  11839. #ifdef SIGPIPE
  11840.     if (ftprecv.oldintp)
  11841.       signal(SIGPIPE, ftprecv.oldintr);
  11842. #endif /* SIGPIPE */
  11843.     signal(SIGINT, SIG_IGN);
  11844.     if (!cpend) {
  11845.         ftpcode = -1;
  11846.         signal(SIGINT, ftprecv.oldintr);
  11847.         ftprecvret = -1;
  11848. #ifdef NTSIG
  11849.         ckThreadEnd(threadinfo);
  11850. #endif /* NTSIG */
  11851.         return;
  11852.     }
  11853.     cancel_remote(ftprecv.din);
  11854.     if (ftpcode > -1)
  11855.       ftpcode = -1;
  11856.     if (data >= 0) {
  11857. #ifdef CK_SSL
  11858.         if (ssl_ftp_data_active_flag) {
  11859.             SSL_shutdown(ssl_ftp_data_con);
  11860.             SSL_free(ssl_ftp_data_con);
  11861.             ssl_ftp_data_active_flag = 0;
  11862.             ssl_ftp_data_con = NULL;
  11863.         }
  11864. #endif /* CK_SSL */
  11865. #ifdef TCPIPLIB
  11866.         socket_close(data);
  11867. #else /* TCPIPLIB */
  11868. #ifdef USE_SHUTDOWN
  11869.         shutdown(data, 1+1);
  11870. #endif /* USE_SHUTDOWN */
  11871.         close(data);
  11872. #endif /* TCPIPLIB */
  11873.         data = -1;
  11874.         globaldin = -1;
  11875.     }
  11876.     if (!out2screen) {
  11877.     int x = 0;
  11878.     debug(F111,"ftp failrecv2 zclose",ftprecv.local,keep);
  11879.     zclose(ZOFILE);
  11880.     switch (keep) {            /* which is... */
  11881.       case SET_AUTO:        /* AUTO */
  11882.         if (curtype == FTT_ASC)    /* Delete file if TYPE A. */
  11883.           x = 1;
  11884.         break;
  11885.       case SET_OFF:            /* DISCARD */
  11886.         x = 1;            /* Delete file, period. */
  11887.         break;
  11888.       default:            /* KEEP */
  11889.         break;
  11890.     }
  11891.     if (x) {
  11892.         x = zdelet(ftprecv.local);
  11893.         debug(F111,"ftp failrecv2 delete incomplete",ftprecv.local,x);
  11894.     }
  11895.     }
  11896.     if (ftprecv.din) {
  11897. #ifdef TCPIPLIB
  11898.         socket_close(ftprecv.din);
  11899. #else /* TCPIPLIB */
  11900. #ifdef USE_SHUTDOWN
  11901.         shutdown(ftprecv.din, 1+1);
  11902. #endif /* USE_SHUTDOWN */
  11903.         close(ftprecv.din);
  11904. #endif /* TCPIPLIB */
  11905.     }
  11906.     signal(SIGINT, ftprecv.oldintr);
  11907.     ftprecvret = -1;
  11908.  
  11909.     if (havesigint) {
  11910.     havesigint = 0;
  11911.     debug(F100,"FTP failftprecv2 chain to trap()...","",0);
  11912. #ifdef OS2
  11913.         debug(F100,"FTP failftprecv2 PostCtrlCSem()...","",0);
  11914.         PostCtrlCSem();
  11915. #else /* OS2 */
  11916.     if (ftprecv.oldintr != SIG_IGN)
  11917.       (*ftprecv.oldintr)(SIGINT);
  11918.     /* NOTREACHED (I hope!) */
  11919.     debug(F100,"ftp failftprecv2 return from trap()...","",0);
  11920. #endif /* OS2 */
  11921.     }
  11922. }
  11923.  
  11924. static VOID
  11925. #ifdef CK_ANSIC
  11926. doftprecv2(VOID * threadinfo)
  11927. #else
  11928. doftprecv2(threadinfo) VOID * threadinfo;
  11929. #endif /* CK_ANSIC */
  11930. {
  11931.     register int c, d;
  11932.     long bytes = 0L;
  11933.     int bare_lfs = 0;
  11934.     int blksize = 0;
  11935.     ULONG start = 0L, stop;
  11936.     char * p;
  11937.     static char * rcvbuf = NULL;
  11938.     static int rcvbufsiz = 0;
  11939. #ifdef CK_URL
  11940.     char newname[CKMAXPATH+1];        /* For file dialog */
  11941. #endif /* CK_URL */
  11942.     extern int adl_ask;
  11943.  
  11944.     ftprecv.din = -1;
  11945. #ifdef NTSIG
  11946.     if (threadinfo) {                   /* Thread local storage... */
  11947.         TlsSetValue(TlsIndex,threadinfo);
  11948.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11949.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11950. #endif /* NTSIG */
  11951. #ifdef CK_LOGIN
  11952. #ifdef IKSD
  11953. #ifdef NT
  11954.     if (inserver)
  11955.       setntcreds();
  11956. #endif /* NT */
  11957. #endif /* IKSD */
  11958. #endif /* CK_LOGIN */
  11959.  
  11960.     if (ftprecv.recover) {                      /* Initiate recovery */
  11961.         x = ftpcmd("REST",ckltoa(ftprecv.localsize),-1,-1,ftp_vbm);
  11962.         debug(F111,"ftp reply","REST",x);
  11963.         if (x == REPLY_CONTINUE) {
  11964.             ftprecv.lmode = "ab";
  11965.             rs_len = ftprecv.localsize;
  11966.         } else {
  11967.             ftprecv.recover = 0;
  11968.         }
  11969.     }
  11970.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  11971.  
  11972.     debug(F111,"ftp recvrequest recover E",ftprecv.remote,ftprecv.recover);
  11973.  
  11974.     /* Send the command and get reply */
  11975.     debug(F110,"ftp recvrequest cmd",ftprecv.cmd,0);
  11976.     debug(F110,"ftp recvrequest remote",ftprecv.remote,0);
  11977.  
  11978.     if (ftpcmd(ftprecv.cmd,ftprecv.remote,ftprecv.fcs,ftprecv.rcs,ftp_vbm)
  11979.     != REPLY_PRELIM) {
  11980.         signal(SIGINT, ftprecv.oldintr); /* Bad reply, fail. */
  11981.         ftprecvret = -1;        /* ftpcode is set by ftpcmd() */
  11982. #ifdef NTSIG
  11983.         ckThreadEnd(threadinfo);
  11984. #endif /* NTSIG */
  11985.         return;
  11986.     }
  11987.     ftprecv.din = dataconn("r");        /* Good reply, open data connection */
  11988.     globaldin = ftprecv.din;            /* Global copy of file descriptor */
  11989.     if (ftprecv.din == -1) {            /* Check for failure */
  11990.         ftpcode = -3;                   /* Code for no data connection */
  11991.         ftprecvret = -1;
  11992. #ifdef NTSIG
  11993.         ckThreadEnd(threadinfo);
  11994. #endif /* NTSIG */
  11995.         return;
  11996.     }
  11997. #ifdef CK_URL
  11998.     /* In K95 GUI put up a file box */
  11999.     if (haveurl && g_url.pth && adl_ask    ) { /* Downloading from a URL */
  12000.     int x;
  12001.     char * preface =
  12002. "\r\nIncoming file from FTP server...\r\n\
  12003. Please confirm output file specification or supply an alternative:";
  12004.  
  12005.     x = uq_file(preface,        /* K95 GUI: Put up file box. */
  12006.             NULL,
  12007.             4,
  12008.             NULL,
  12009.             ftprecv.local ? ftprecv.local : ftprecv.remote,
  12010.             newname,
  12011.             CKMAXPATH+1
  12012.             );
  12013.     if (x > 0) {
  12014.         ftprecv.local = newname;    /* Substitute user's file name */
  12015.         if (x == 2)            /* And append if user said to */
  12016.           ftprecv.lmode = "ab";
  12017.     }
  12018.     }
  12019. #endif /* CK_URL */
  12020.     x = 1;                              /* Output file open OK? */
  12021.     if (ftprecv.pipename) {        /* Command */
  12022.         x = zxcmd(ZOFILE,ftprecv.pipename);
  12023.         debug(F111,"ftp recvrequest zxcmd",ftprecv.pipename,x);
  12024.     } else if (!out2screen) {           /* File */
  12025.         struct filinfo xx;
  12026.         xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  12027.         xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  12028.     /* Append or New */
  12029.         xx.dsp = !strcmp(ftprecv.lmode,"ab") ? XYFZ_A : XYFZ_N;
  12030.         x = zopeno(ZOFILE,ftprecv.local,NULL,&xx);
  12031.         debug(F111,"ftp recvrequest zopeno",ftprecv.local,x);
  12032.     }
  12033.     if (x < 1) {                        /* Failure to open output file */
  12034.         if ((!dpyactive || ftp_deb))
  12035.           fprintf(stderr, "local(2): %s: %s\n", ftprecv.local, ck_errstr());
  12036.         ftprecvret = -1;
  12037. #ifdef NTSIG
  12038.         ckThreadEnd(threadinfo);
  12039. #endif /* NTSIG */
  12040.         return;
  12041.     }
  12042.     blksize = FTP_BUFSIZ;               /* Allocate input buffer */
  12043.  
  12044.     debug(F101,"ftp recvrequest blksize","",blksize);
  12045.     debug(F101,"ftp recvrequest rcvbufsiz","",rcvbufsiz);
  12046.  
  12047.     if (rcvbufsiz < blksize) {          /* if necessary */
  12048.         if (rcvbuf) {
  12049.             free(rcvbuf);
  12050.             rcvbuf = NULL;
  12051.         }
  12052.         rcvbuf = (char *)malloc((unsigned)blksize);
  12053.         if (!rcvbuf) {
  12054.         debug(F100,"ftp get rcvbuf malloc failed","",0);
  12055.             ftpcode = -2;
  12056. #ifdef ENOMEM
  12057.             errno = ENOMEM;
  12058. #endif /* ENOMEM */
  12059.             if ((!dpyactive || ftp_deb))
  12060.               perror("malloc");
  12061.             rcvbufsiz = 0;
  12062.             ftprecvret = -1;
  12063. #ifdef NTSIG
  12064.             ckThreadEnd(threadinfo);
  12065. #endif /* NTSIG */
  12066.             return;
  12067.         }
  12068.     debug(F101,"ftp get rcvbuf malloc ok","",blksize);
  12069.         rcvbufsiz = blksize;
  12070.     }
  12071.     debug(F111,"ftp get rcvbufsiz",ftprecv.local,rcvbufsiz);
  12072.  
  12073.     ffc = 0L;                           /* Character counter */
  12074.     cps = oldcps = 0L;                  /* Thruput */
  12075.     start = gmstimer();                 /* Start time (msecs) */
  12076. #ifdef GFTIMER
  12077.     rftimer();                          /* Start time (float) */
  12078. #endif /* GFTIMER */
  12079.  
  12080.     debug(F111,"ftp get type",ftprecv.local,curtype);
  12081.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  12082.     switch (curtype) {
  12083.       case FTT_BIN:                     /* Binary mode */
  12084.       case FTT_TEN:                     /* TENEX mode */
  12085.         d = 0;
  12086.         while (1) {
  12087.             errno = 0;
  12088.             c = secure_read(ftprecv.din, rcvbuf, rcvbufsiz);
  12089.             if (cancelfile) {
  12090.                 failftprecv2(threadinfo);
  12091. #ifdef NTSIG
  12092.                 ckThreadEnd(threadinfo);
  12093. #endif /* NTSIG */
  12094.                 return;
  12095.             }
  12096.             if (c < 1)
  12097.               break;
  12098. #ifdef printf                           /* (What if it isn't?) */
  12099.             if (out2screen && !ftprecv.pipename) {
  12100.                 int i;
  12101.                 for (i = 0; i < c; i++)
  12102.                   printf("%c",rcvbuf[i]);
  12103.             } else
  12104. #endif /* printf */
  12105.               {
  12106.                 register int i;
  12107.                 i = 0;
  12108.                 errno = 0;
  12109.                 while (i < c) {
  12110.                     if (zmchout(rcvbuf[i++]) < 0) {
  12111.                         d = i;
  12112.                         break;
  12113.                     }
  12114.                 }
  12115.             }
  12116.             bytes += c;
  12117.             ffc += c;
  12118.         }
  12119.         if (c < 0) {
  12120.             debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  12121.             if (c == -1 && errno != EPIPE)
  12122.               if ((!dpyactive || ftp_deb))
  12123.                 perror("netin");
  12124.             bytes = -1;
  12125.             ftpcode = -4;
  12126.         }
  12127.         if (d < c) {
  12128.             ftpcode = -2;
  12129.             if ((!dpyactive || ftp_deb)) {
  12130.                 char * p;
  12131.                 p = ftprecv.local ? ftprecv.local : ftprecv.pipename;
  12132.                 if (d < 0)
  12133.                   fprintf(stderr,
  12134.               "local(3): %s: %s\n", ftprecv.local, ck_errstr());
  12135.                 else
  12136.                   fprintf(stderr,
  12137.               "%s: short write\n", ftprecv.local);
  12138.             }
  12139.         }
  12140.         break;
  12141.  
  12142.       case FTT_ASC:                     /* Text mode */
  12143.     debug(F101,"ftp recvrequest TYPE A xlate","",ftprecv.xlate);
  12144. #ifndef NOCSETS
  12145.         if (ftprecv.xlate) {
  12146.             int t;
  12147. #ifdef CK_ANSIC
  12148.             int (*fn)(char);
  12149. #else
  12150.             int (*fn)();
  12151. #endif /* CK_ANSIC */
  12152.             debug(F110,"ftp recvrequest (data)","initxlate",0);
  12153.             initxlate(ftprecv.rcs,ftprecv.fcs);         /* (From,To) */
  12154.             if (ftprecv.pipename) {
  12155.                 fn = pipeout;
  12156.                 debug(F110,"ftp recvrequest ASCII","pipeout",0);
  12157.             } else {
  12158.                 fn = out2screen ? scrnout : putfil;
  12159.                 debug(F110,"ftp recvrequest ASCII",
  12160.                       out2screen ? "scrnout" : "putfil",0);
  12161.             }
  12162.             while (1) {
  12163.         /* Get byte from net */
  12164.                 c0 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12165.                 if (cancelfile) {
  12166.                     failftprecv2(threadinfo);
  12167. #ifdef NTSIG
  12168.                     ckThreadEnd(threadinfo);
  12169. #endif /* NTSIG */
  12170.                     return;
  12171.                 }
  12172.                 if (c0 < 0)
  12173.                   break;
  12174.         /* Second byte from net */
  12175.                 c1 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12176.                 if (cancelfile) {
  12177.                     failftprecv2(threadinfo);
  12178. #ifdef NTSIG
  12179.                     ckThreadEnd(threadinfo);
  12180. #endif /* NTSIG */
  12181.                     return;
  12182.                 }
  12183.                 if (c1 < 0)
  12184.                   break;
  12185. #ifdef COMMENT
  12186.         /* K95: Check whether we need this */
  12187.         if (fileorder > 0)    /* Little Endian */
  12188.           bytswap(&c0,&c1);    /* swap bytes*/
  12189. #endif /* COMMENT */
  12190.  
  12191. #ifdef OS2
  12192.                 if ( out2screen &&            /* we're translating to UCS-2 */ 
  12193.                      !k95stdout && !inserver) /* for the real screen... */     
  12194.                 {
  12195.                     union {
  12196.                         USHORT ucs2;
  12197.                         UCHAR  bytes[2];
  12198.                     } output;
  12199.  
  12200.                     output.bytes[0] = c1;
  12201.                     output.bytes[1] = c0;
  12202.  
  12203.                     VscrnWrtUCS2StrAtt(VCMD,
  12204.                                        &output.ucs2,
  12205.                                        1,
  12206.                                        wherey[VCMD],
  12207.                                        wherex[VCMD],
  12208.                                        &colorcmd
  12209.                                        );
  12210.  
  12211.                 } else 
  12212. #endif /* OS2 */
  12213.                 {
  12214.                     if ((x = xpnbyte(c0,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12215.                     if ((x = xpnbyte(c1,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12216.                 }
  12217.             }
  12218.         } else {
  12219. #endif /* NOCSETS */
  12220.             while (1) {
  12221.                 c = secure_getc(ftprecv.din,0);
  12222.                 if (cancelfile) {
  12223.                     failftprecv2(threadinfo);
  12224. #ifdef NTSIG
  12225.                     ckThreadEnd(threadinfo);
  12226. #endif /* NTSIG */
  12227.                     return;
  12228.                 }
  12229.                 if (c < 0 || c == EOF)
  12230.                   break;
  12231. #ifdef UNIX
  12232.         /* Record format conversion for Unix */
  12233.         /* SKIP THIS FOR WINDOWS! */
  12234.                 if (c == '\n')
  12235.                   bare_lfs++;
  12236.                 while (c == '\r') {
  12237.                     bytes++;
  12238.                     if ((c = secure_getc(ftprecv.din,0)) != '\n' ||
  12239.             ftprecv.tcrflag) {
  12240.                         if (cancelfile) {
  12241.                             failftprecv2(threadinfo);
  12242. #ifdef NTSIG
  12243.                             ckThreadEnd(threadinfo);
  12244. #endif /* NTSIG */
  12245.                             return;
  12246.                         }
  12247.                         if (c < 0 || c == EOF)
  12248.                           goto break2;
  12249.                         if (c == '\0') {
  12250.                             bytes++;
  12251.                             goto contin2;
  12252.                         }
  12253.                     }
  12254.                 }
  12255.                 if (c < 0)
  12256.                   break;
  12257. #endif /* UNX */
  12258.  
  12259.                 if (out2screen && !ftprecv.pipename)
  12260. #ifdef printf
  12261.                   printf("%c",(char)c);
  12262. #else
  12263.                   putchar((char)c);
  12264. #endif /* printf */
  12265.                 else
  12266.                   if ((d = zmchout(c)) < 0)
  12267.                     break;
  12268.                 bytes++;
  12269.                 ffc++;
  12270.               contin2:
  12271.                 ;
  12272.             }
  12273.           break2:
  12274.             if (bare_lfs && (!dpyactive || ftp_deb)) {
  12275.                 printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  12276.                        bare_lfs);
  12277.                 printf("File might not have transferred correctly.\n");
  12278.             }
  12279.             if (ftprecv.din == -1) {
  12280.                 bytes = -1;
  12281.             }
  12282.             if (c == -2)
  12283.               bytes = -1;
  12284.             break;
  12285. #ifndef NOCSETS
  12286.         }
  12287. #endif /* NOCSETS */
  12288.     }
  12289.     if (ftprecv.pipename || !out2screen) {
  12290.     zclose(ZOFILE);            /* Close the file */
  12291.     debug(F111,"doftprecv2 zclose ftpcode",ftprecv.local,ftpcode);
  12292.     if (ftpcode < 0) {        /* If download failed */
  12293.         int x = 0;
  12294.         switch (keep) {        /* which is... */
  12295.           case SET_AUTO:        /* AUTO */
  12296.         if (curtype == FTT_ASC) /* Delete file if TYPE A. */
  12297.           x = 1;
  12298.         break;
  12299.           case SET_OFF:        /* DISCARD */
  12300.         x = 1;            /* Delete file, period. */
  12301.         break;
  12302.           default:            /* KEEP */
  12303.         break;
  12304.         }
  12305.         if (x) {
  12306.         x = zdelet(ftprecv.local);
  12307.         debug(F111,"ftp get delete incomplete",ftprecv.local,x);
  12308.         }
  12309.     }
  12310.     }
  12311.     signal(SIGINT, ftprecv.oldintr);
  12312. #ifdef SIGPIPE
  12313.     if (ftprecv.oldintp)
  12314.       signal(SIGPIPE, ftprecv.oldintp);
  12315. #endif /* SIGPIPE */
  12316.     stop = gmstimer();
  12317. #ifdef GFTIMER
  12318.     fpfsecs = gftimer();
  12319. #endif /* GFTIMER */
  12320.     tfc += ffc;
  12321.  
  12322. #ifdef TCPIPLIB
  12323.     socket_close(ftprecv.din);
  12324. #else /* TCPIPLIB */
  12325. #ifdef USE_SHUTDOWN
  12326.     shutdown(ftprecv.din, 1+1);
  12327. #endif /* USE_SHUTDOWN */
  12328.     close(ftprecv.din);
  12329. #endif /* TCPIPLIB */
  12330.     ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  12331.     ftprecvret = ((ftpcode < 0 || ftprecv.reply == REPLY_TRANSIENT || 
  12332.                    ftprecv.reply == REPLY_ERROR) ? -1 : 0);
  12333. #ifdef NTSIG
  12334.      ckThreadEnd(threadinfo);
  12335. #endif /* NTSIG */
  12336. }
  12337.  
  12338. static int
  12339. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  12340.             xlate, fcs, rcs)
  12341.     char *cmd, *local, *remote, *lmode, *pipename;
  12342.     int printnames, recover, xlate, fcs, rcs;
  12343. {
  12344. #ifdef NT
  12345.     struct _stat stbuf;
  12346. #else /* NT */
  12347.     struct stat stbuf;
  12348. #endif /* NT */
  12349.  
  12350. #ifdef DEBUG
  12351.     if (deblog) {
  12352.         debug(F111,"ftp recvrequest cmd",cmd,recover);
  12353.         debug(F110,"ftp recvrequest local ",local,0);
  12354.         debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  12355.         debug(F110,"ftp recvrequest pipename ",pipename,0);
  12356.         debug(F101,"ftp recvrequest xlate","",xlate);
  12357.         debug(F101,"ftp recvrequest fcs","",fcs);
  12358.         debug(F101,"ftp recvrequest rcs","",rcs);
  12359.     }
  12360. #endif /* DEBUG */
  12361.  
  12362.     ftprecv.localsize = 0L;
  12363.  
  12364.     if (remfile) {                      /* See remcfm(), remtxt() */
  12365.         if (rempipe) {
  12366.             pipename = remdest;
  12367.         } else {
  12368.             local = remdest;
  12369.             if (remappd) lmode = "ab";
  12370.         }
  12371.     }
  12372.     out2screen = 0;
  12373.     if (!cmd) cmd = "";                 /* Core dump prevention */
  12374.     if (!remote) remote = "";
  12375.     if (!lmode) lmode = "";
  12376.  
  12377.     if (pipename) {                     /* No recovery for pipes. */
  12378.         recover = 0;
  12379.         if (!local)
  12380.           local = pipename;
  12381.     } else {
  12382.         if (!local)                     /* Output to screen? */
  12383.           local = "-";
  12384.         out2screen = !strcmp(local,"-");
  12385.     }
  12386.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  12387.  
  12388. #ifdef OS2
  12389.     if ( ftp_xla && out2screen && !k95stdout && !inserver )
  12390.         fcs = FC_UCS2;
  12391. #endif /* OS2 */
  12392.  
  12393.     if (out2screen)                     /* No recovery to screen */
  12394.       recover = 0;
  12395.     if (!ftp_typ)                       /* No recovery in text mode */
  12396.       recover = 0;
  12397.     ftprecv.is_retr = (strcmp(cmd, "RETR") == 0);
  12398.  
  12399.     if (!ftprecv.is_retr)               /* No recovery except for RETRieve */
  12400.       recover = 0;
  12401.  
  12402. #ifdef COMMENT
  12403.     if (!out2screen && !pipename && ftprecv.is_retr) { /* To real file */
  12404.         if (recursive && ckstrchr(local,'/')) {
  12405.         
  12406.         }
  12407.     }
  12408. #endif /* COMMENT */
  12409.  
  12410.     ftprecv.localsize = 0L;        /* Local file size */
  12411.     rs_len = 0L;                        /* Recovery point */
  12412.  
  12413.     debug(F101,"ftp recvrequest recover","",recover);
  12414.     if (recover) {                      /* Recovering... */
  12415.         if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
  12416.         debug(F101,"ftp recvrequest recover stat failed","",errno);
  12417.             recover = 0;                /* So cancel recovery */
  12418.         } else {                        /* Have local file info */
  12419.             ftprecv.localsize = stbuf.st_size;  /* Get size */
  12420.         /* Remote file smaller than local */
  12421.             if (fsize < ftprecv.localsize) {
  12422.         debug(F101,"ftp recvrequest recover remote smaller","",fsize);
  12423.                 recover = 0;            /* Recovery can't work */
  12424.             } else if (fsize == ftprecv.localsize) { /* Sizes are equal */
  12425.                 debug(F111,"ftp recvrequest recover equal size",
  12426.               remote,ftprecv.localsize);
  12427.                 return(1);
  12428.             }
  12429. #ifdef COMMENT
  12430. /*
  12431.   The problem here is that the original partial file never got its date
  12432.   set, either because FTP DATES was OFF, or because the partial file was
  12433.   downloaded by some other program that doesn't set local file dates, or
  12434.   because Kermit only sets the file's date when the download was complete
  12435.   and successful.  In all these cases, the local file has a later time
  12436.   than the remote.
  12437. */
  12438.             if (recover) {              /* Remote is bigger */
  12439.                 x = chkmodtime(local,remote,0); /* Check file dates */
  12440.                 debug(F111,"ftp recvrequest chkmodtime",remote,x);
  12441.                 if (x != 1)        /* Dates must be equal! */
  12442.                   recover = 0;          /* If not, get whole file */
  12443.             }
  12444. #endif /* COMMENT */
  12445.         }
  12446.         debug(F111,"ftp recvrequest recover",remote,recover);
  12447.     }
  12448.  
  12449. #ifdef FTP_PROXY
  12450.     if (proxy && ftprecv.is_retr)
  12451.       return(proxtrans(cmd, local ? local : remote, remote));
  12452. #endif /* FTP_PROXY */
  12453.  
  12454.     ftprecv.tcrflag = (feol != CR) && ftprecv.is_retr;
  12455.  
  12456.     ftprecv.reply = 0;
  12457.     ftprecv.fcs = fcs;
  12458.     ftprecv.rcs = rcs;
  12459.     ftprecv.recover = recover;
  12460.     ftprecv.xlate = xlate;
  12461.     ftprecv.cmd = cmd;
  12462.     ftprecv.local = local;
  12463.     ftprecv.remote = remote;
  12464.     ftprecv.lmode = lmode;
  12465.     ftprecv.pipename = pipename;
  12466.     ftprecv.oldintp = NULL;
  12467.     ftpcode = 0;
  12468.  
  12469.     havesigint = 0;
  12470.     ftprecv.oldintr = signal(SIGINT, cancelrecv);
  12471.     if (cc_execute(ckjaddr(recvcancel), doftprecv, failftprecv) < 0)
  12472.       return -1;
  12473.     if (ftprecvret < 0)
  12474.       return -1;
  12475.  
  12476.     if (cc_execute(ckjaddr(recvcancel), doftprecv2, failftprecv2) < 0)
  12477.       return -1;
  12478.     return ftprecvret;
  12479. }
  12480.  
  12481. /*
  12482.  * Need to start a listen on the data channel before we send the command,
  12483.  * otherwise the server's connect may fail.
  12484.  */
  12485. static int
  12486. initconn() {
  12487.     register char *p, *a;
  12488.     int result, tmpno = 0;
  12489.     int on = 1;
  12490.     GSOCKNAME_T len;
  12491.  
  12492. #ifndef NO_PASSIVE_MODE
  12493.     int a1,a2,a3,a4,p1,p2;
  12494.  
  12495.     if (passivemode) {
  12496.         data = socket(AF_INET, SOCK_STREAM, 0);
  12497.         globaldin = data;
  12498.         if (data < 0) {
  12499.             perror("ftp: socket");
  12500.             return(-1);
  12501.         }
  12502.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  12503.             printf("Passive mode refused\n");
  12504.             passivemode = 0;
  12505.             return(initconn());
  12506.         }
  12507. /*
  12508.   Now we have a string of comma-separated one-byte unsigned integer values,
  12509.   The first four are the an IP address.  The fifth is the MSB of the port
  12510.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  12511. */
  12512.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  12513.             printf("Passive mode address scan failure\n");
  12514.             return(-1);
  12515.         };
  12516. #ifndef NOHTTP
  12517.         if (tcp_http_proxy) {
  12518. #ifdef OS2
  12519.             char * agent = "Kermit 95"; /* Default user agent */
  12520. #else
  12521.             char * agent = "C-Kermit";
  12522. #endif /* OS2 */
  12523.             register struct hostent *hp = 0;
  12524.             struct servent *destsp;
  12525.             char host[512], *p, *q;
  12526. #ifdef IP_TOS
  12527. #ifdef IPTOS_THROUGHPUT
  12528.             int tos;
  12529. #endif /* IPTOS_THROUGHPUT */
  12530. #endif /* IP_TOS */
  12531.             int s;
  12532. #ifdef DEBUG
  12533.             extern int debtim;
  12534.             int xdebtim;
  12535.             xdebtim = debtim;
  12536.             debtim = 1;
  12537. #endif /* DEBUG */
  12538.  
  12539.             ckmakxmsg(proxyhost,HTTPCPYL,ckuitoa(a1),".",ckuitoa(a2),
  12540.                       ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  12541.                       NULL,NULL,NULL
  12542.                       );
  12543.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  12544.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  12545.               *q = *p;
  12546.             *q = '\0';
  12547.  
  12548.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  12549.             if (hisctladdr.sin_addr.s_addr != -1) {
  12550.                 debug(F110,"initconn A",host,0);
  12551.                 hisctladdr.sin_family = AF_INET;
  12552.             } else {
  12553.                 debug(F110,"initconn B",host,0);
  12554.                 hp = gethostbyname(host);
  12555. #ifdef HADDRLIST
  12556.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  12557. #endif /* HADDRLIST */
  12558.                 if (hp == NULL) {
  12559.                     fprintf(stderr, "ftp: %s: Unknown host\n", host);
  12560.                     ftpcode = -1;
  12561. #ifdef DEBUG
  12562.                     debtim = xdebtim;
  12563. #endif /* DEBUG */
  12564.                     return(0);
  12565.                 }
  12566.                 hisctladdr.sin_family = hp->h_addrtype;
  12567. #ifdef HADDRLIST
  12568.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  12569.                        sizeof(hisctladdr.sin_addr));
  12570. #else /* HADDRLIST */
  12571.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  12572.                        sizeof(hisctladdr.sin_addr));
  12573. #endif /* HADDRLIST */
  12574.             }
  12575.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12576.             debug(F101,"initconn socket","",data);
  12577.             if (data < 0) {
  12578.                 perror("ftp: socket");
  12579.                 ftpcode = -1;
  12580. #ifdef DEBUG
  12581.                 debtim = xdebtim;
  12582. #endif /* DEBUG */
  12583.                 return(0);
  12584.             }
  12585.             if (*p == ':')
  12586.               p++;
  12587.             else
  12588.               p = "http";
  12589.  
  12590.             destsp = getservbyname(p,"tcp");
  12591.             if (destsp)
  12592.               hisctladdr.sin_port = destsp->s_port;
  12593.             else if (p)
  12594.               hisctladdr.sin_port = htons(atoi(p));
  12595.             else
  12596.               hisctladdr.sin_port = htons(80);
  12597.             errno = 0;
  12598. #ifdef HADDRLIST
  12599.             debug(F100,"initconn HADDRLIST","",0);
  12600.             while
  12601. #else
  12602.             debug(F100,"initconn no HADDRLIST","",0);
  12603.             if
  12604. #endif /* HADDRLIST */
  12605.               (connect(data, (struct sockaddr *)&hisctladdr,
  12606.                        sizeof (hisctladdr)) < 0) {
  12607.                   debug(F101,"initconn connect failed","",errno);
  12608. #ifdef HADDRLIST
  12609.                   if (hp && hp->h_addr_list[1]) {
  12610.                       int oerrno = errno;
  12611.  
  12612.                       fprintf(stderr,
  12613.                               "ftp: connect to address %s: ",
  12614.                               inet_ntoa(hisctladdr.sin_addr)
  12615.                               );
  12616.                       errno = oerrno;
  12617.                       perror((char *)0);
  12618.                       hp->h_addr_list++;
  12619.                       memcpy((char *)&hisctladdr.sin_addr,
  12620.                              hp->h_addr_list[0],
  12621.                              sizeof(hisctladdr.sin_addr));
  12622.                       fprintf(stdout, "Trying %s...\n",
  12623.                               inet_ntoa(hisctladdr.sin_addr));
  12624. #ifdef TCPIPLIB
  12625.                       socket_close(data);
  12626. #else /* TCPIPLIB */
  12627.                       close(data);
  12628. #endif /* TCPIPLIB */
  12629.                       data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12630.                       if (data < 0) {
  12631.                           perror("ftp: socket");
  12632.                           ftpcode = -1;
  12633. #ifdef DEBUG
  12634.                           debtim = xdebtim;
  12635. #endif /* DEBUG */
  12636.                           return(0);
  12637.                       }
  12638.                       continue;
  12639.                   }
  12640. #endif /* HADDRLIST */
  12641.                   perror("ftp: connect");
  12642.                   ftpcode = -1;
  12643.                   goto bad;
  12644.               }
  12645.             if (http_connect(data,agent,NULL,
  12646.                              tcp_http_proxy_user,
  12647.                              tcp_http_proxy_pwd,
  12648.                              0,
  12649.                              proxyhost
  12650.                              ) < 0) {
  12651. #ifdef TCPIPLIB
  12652.                 socket_close(data);
  12653. #else /* TCPIPLIB */
  12654.                 close(data);
  12655. #endif /* TCPIPLIB */
  12656.                 perror("ftp: connect");
  12657.                 ftpcode = -1;
  12658.                 goto bad;
  12659.             }
  12660.         } else
  12661. #endif /* NOHTTP */
  12662.         {
  12663.             data_addr.sin_family = AF_INET;
  12664.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  12665.             data_addr.sin_port = htons((p1<<8)|p2);
  12666.  
  12667.             if (connect(data,
  12668.                         (struct sockaddr *)&data_addr,
  12669.                         sizeof(data_addr)) < 0
  12670.                 ) {
  12671.                 perror("ftp: connect");
  12672.                 return(-1);
  12673.             }
  12674.         }
  12675.         debug(F100,"initconn connect ok","",0);
  12676. #ifdef IP_TOS
  12677. #ifdef IPTOS_THROUGHPUT
  12678.         on = IPTOS_THROUGHPUT;
  12679.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12680.           perror("ftp: setsockopt TOS (ignored)");
  12681. #endif /* IPTOS_THROUGHPUT */
  12682. #endif /* IP_TOS */
  12683.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  12684.         return(0);
  12685.     }
  12686. #endif /* NO_PASSIVE_MODE */
  12687.  
  12688.   noport:
  12689.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  12690.     if (sendport)
  12691.       data_addr.sin_port = 0;   /* let system pick one */
  12692.     if (data != -1) {
  12693. #ifdef TCPIPLIB
  12694.         socket_close(data);
  12695. #else /* TCPIPLIB */
  12696. #ifdef USE_SHUTDOWN
  12697.         shutdown(data, 1+1);
  12698. #endif /* USE_SHUTDOWN */
  12699.         close(data);
  12700. #endif /* TCPIPLIB */
  12701.     }
  12702.     data = socket(AF_INET, SOCK_STREAM, 0);
  12703.     globaldin = data;
  12704.     if (data < 0) {
  12705.         perror("ftp: socket");
  12706.         if (tmpno)
  12707.           sendport = 1;
  12708.         return(-1);
  12709.     }
  12710.     if (!sendport) {
  12711.         if (setsockopt(data,
  12712.                        SOL_SOCKET,
  12713.                        SO_REUSEADDR,
  12714.                        (char *)&on,
  12715.                        sizeof (on)
  12716.                        ) < 0
  12717.             ) {
  12718.             perror("ftp: setsockopt (reuse address)");
  12719.             goto bad;
  12720.         }
  12721.     }
  12722.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  12723.         perror("ftp: bind");
  12724.         goto bad;
  12725.     }
  12726.     len = sizeof (data_addr);
  12727.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  12728.         perror("ftp: getsockname");
  12729.         goto bad;
  12730.     }
  12731.     if (listen(data, 1) < 0) {
  12732.         perror("ftp: listen");
  12733.         goto bad;
  12734.     }
  12735.     if (sendport) {
  12736.         a = (char *)&data_addr.sin_addr;
  12737.         p = (char *)&data_addr.sin_port;
  12738.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  12739.                   UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  12740.                   UC(p[0]),",", UC(p[1]));
  12741.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  12742.         if (result == REPLY_ERROR && sendport) {
  12743.             sendport = 0;
  12744.             tmpno = 1;
  12745.             goto noport;
  12746.         }
  12747.         return(result != REPLY_COMPLETE);
  12748.     }
  12749.     if (tmpno)
  12750.       sendport = 1;
  12751. #ifdef IP_TOS
  12752. #ifdef IPTOS_THROUGHPUT
  12753.     on = IPTOS_THROUGHPUT;
  12754.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12755.       perror("ftp: setsockopt TOS (ignored)");
  12756. #endif
  12757. #endif
  12758.     return(0);
  12759.   bad:
  12760. #ifdef TCPIPLIB
  12761.     socket_close(data);
  12762. #else /* TCPIPLIB */
  12763. #ifdef USE_SHUTDOWN
  12764.     shutdown(data, 1+1);
  12765. #endif /* USE_SHUTDOWN */
  12766.     close(data);
  12767. #endif /* TCPIPLIB */
  12768.     data = -1;
  12769.     globaldin = data;
  12770.     if (tmpno)
  12771.       sendport = 1;
  12772.     return(-1);
  12773. }
  12774.  
  12775. #ifdef CK_SSL
  12776. static int
  12777. ssl_dataconn() {
  12778.     if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
  12779.         SSL_free(ssl_ftp_data_con);
  12780.         ssl_ftp_data_con=NULL;
  12781.     }
  12782.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  12783.  
  12784.     SSL_set_fd(ssl_ftp_data_con,data);
  12785.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  12786.  
  12787.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  12788.  
  12789.     if (ssl_debug_flag) {
  12790.         fprintf(stderr,"=>START SSL connect on DATA\n");
  12791.         fflush(stderr);
  12792.     }
  12793.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  12794.         static char errbuf[1024];
  12795.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  12796.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  12797.         fprintf(stderr,"%s\n", errbuf);
  12798.         fflush(stderr);
  12799. #ifdef TCPIPLIB
  12800.         socket_close(data);
  12801. #else /* TCPIPLIB */
  12802. #ifdef USE_SHUTDOWN
  12803.         shutdown(data, 1+1);
  12804. #endif /* USE_SHUTDOWN */
  12805.         close(data);
  12806. #endif /* TCPIPLIB */
  12807.         data = -1;
  12808.         globaldin = data;
  12809.         return(-1);
  12810.     } else {
  12811.         ssl_ftp_data_active_flag=1;
  12812.  
  12813.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  12814.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  12815.  
  12816.             if (!subject) {
  12817.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  12818.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  12819.  
  12820.                     ssl_ftp_data_active_flag = 0;
  12821. #ifdef TCPIPLIB
  12822.                     socket_close(data);
  12823. #else /* TCPIPLIB */
  12824. #ifdef USE_SHUTDOWN
  12825.                     shutdown(data, 1+1);
  12826. #endif /* USE_SHUTDOWN */
  12827.                     close(data);
  12828. #endif /* TCPIPLIB */
  12829.                     data = -1;
  12830.                     globaldin = data;
  12831.                     return(-1);
  12832.                 } else {
  12833.                     if (!out2screen && displa && fdispla) {
  12834.                         ftscreen(SCR_TC,0,0L,"Display canceled");
  12835.                         /* fdispla = XYFD_B; */
  12836.                     }
  12837.  
  12838.                     if (uq_ok(
  12839.           "Warning: Server didn't provide a certificate on data connection\n",
  12840.                                "Continue with file transfer? (Y/N)",
  12841.                               3,NULL,0) <= 0) {
  12842.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  12843.                         ssl_ftp_data_active_flag = 0;
  12844. #ifdef TCPIPLIB
  12845.                         socket_close(data);
  12846. #else /* TCPIPLIB */
  12847. #ifdef USE_SHUTDOWN
  12848.                         shutdown(data, 1+1);
  12849. #endif /* USE_SHUTDOWN */
  12850.                         close(data);
  12851. #endif /* TCPIPLIB */
  12852.                         data = -1;
  12853.                         globaldin = data;
  12854.                         return(-1);
  12855.                     }
  12856.                 }
  12857.             } else {
  12858.                 if (!out2screen && displa && fdispla == XYFD_C) {
  12859.                     ftscreen(SCR_TC,0,0L,"Display canceled");
  12860.                     /* fdispla = XYFD_B; */
  12861.                 }
  12862.  
  12863.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_user_host)) {
  12864.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  12865.                     ssl_ftp_data_active_flag = 0;
  12866. #ifdef TCPIPLIB
  12867.                     socket_close(data);
  12868. #else /* TCPIPLIB */
  12869. #ifdef USE_SHUTDOWN
  12870.                     shutdown(data, 1+1);
  12871. #endif /* USE_SHUTDOWN */
  12872.                     close(data);
  12873. #endif /* TCPIPLIB */
  12874.                     data = -1;
  12875.                     globaldin = data;
  12876.                     return(-1);
  12877.                 }
  12878.             }
  12879.         }
  12880.         debug(F110,"dataconn","[SSL - OK]",0);
  12881. #ifdef COMMENT
  12882.         /* This messes up the full screen file transfer display */
  12883.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  12884. #endif /* COMMENT */
  12885.     }
  12886.     if (ssl_debug_flag) {
  12887.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  12888.         fflush(stderr);
  12889.     }
  12890.     return(data);
  12891. }
  12892. #endif /* CK_SSL */
  12893.  
  12894. static int
  12895. dataconn(lmode) char *lmode; {
  12896.     int s;
  12897. #ifdef IP_TOS
  12898.     int tos;
  12899. #endif /* IP_TOS */
  12900. #ifdef UCX50
  12901.     static u_int fromlen;
  12902. #else
  12903.     static SOCKOPT_T fromlen;
  12904. #endif /* UCX50 */
  12905.  
  12906.     fromlen = sizeof(hisdataaddr);
  12907.  
  12908. #ifndef NO_PASSIVE_MODE
  12909.     if (passivemode) {
  12910. #ifdef CK_SSL
  12911.         ssl_ftp_data_active_flag=0;
  12912.         if (ssl_ftp_active_flag &&
  12913.             (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  12914.           return(ssl_dataconn());
  12915. #endif /* CK_SSL */
  12916.         return(data);
  12917.     }
  12918. #endif /* NO_PASSIVE_MODE */
  12919.  
  12920.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  12921.     if (s < 0) {
  12922.         perror("ftp: accept");
  12923. #ifdef TCPIPLIB
  12924.         socket_close(data);
  12925. #else /* TCPIPLIB */
  12926. #ifdef USE_SHUTDOWN
  12927.         shutdown(data, 1+1);
  12928. #endif /* USE_SHUTDOWN */
  12929.         close(data);
  12930. #endif /* TCPIPLIB */
  12931.         data = -1;
  12932.         globaldin = data;
  12933.         return(-1);
  12934.     }
  12935. #ifdef TCPIPLIB
  12936.     socket_close(data);
  12937. #else /* TCPIPLIB */
  12938. #ifdef USE_SHUTDOWN
  12939.     shutdown(data, 1+1);
  12940. #endif /* USE_SHUTDOWN */
  12941.     close(data);
  12942. #endif /* TCPIPLIB */
  12943.     data = s;
  12944.     globaldin = data;
  12945. #ifdef IP_TOS
  12946. #ifdef IPTOS_THROUGHPUT
  12947.     tos = IPTOS_THROUGHPUT;
  12948.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  12949.       perror("ftp: setsockopt TOS (ignored)");
  12950. #endif /* IPTOS_THROUGHPUT */
  12951. #endif /* IP_TOS */
  12952.  
  12953. #ifdef CK_SSL
  12954.     ssl_ftp_data_active_flag=0;
  12955.     if (ssl_ftp_active_flag &&
  12956.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  12957.       return(ssl_dataconn());
  12958. #endif /* CK_SSL */
  12959.     return(data);
  12960. }
  12961.  
  12962. #ifdef FTP_PROXY
  12963. static sigtype
  12964. pscancel(sig) int sig; {
  12965.     cancelfile++;
  12966. }
  12967.  
  12968. static VOID
  12969. pswitch(flag) int flag; {
  12970.     extern int proxy;
  12971.     sig_t oldintr;
  12972.     static struct comvars {
  12973.         int connect;
  12974.         char name[MAXHOSTNAMELEN];
  12975.         struct sockaddr_in mctl;
  12976.         struct sockaddr_in hctl;
  12977.         FILE *in;
  12978.         FILE *out;
  12979.         int tpe;
  12980.         int curtpe;
  12981.         int cpnd;
  12982.         int sunqe;
  12983.         int runqe;
  12984.         int mcse;
  12985.         int ntflg;
  12986.         char nti[17];
  12987.         char nto[17];
  12988.         int mapflg;
  12989.         char mi[CKMAXPATH];
  12990.         char mo[CKMAXPATH];
  12991.         char *authtype;
  12992.         int clvl;
  12993.         int dlvl;
  12994. #ifdef FTP_KRB4
  12995.         des_cblock session;
  12996.         des_key_schedule ftp_sched;
  12997. #endif /* FTP_KRB4 */
  12998. #ifdef FTP_GSSAPI
  12999.         gss_ctx_id_t gcontext;
  13000. #endif /* GSSAPI */
  13001.     } proxstruct, tmpstruct;
  13002.     struct comvars *ip, *op;
  13003.  
  13004.     cancelfile = 0;
  13005.     oldintr = signal(SIGINT, pscancel);
  13006.     if (flag) {
  13007.         if (proxy)
  13008.           return;
  13009.         ip = &tmpstruct;
  13010.         op = &proxstruct;
  13011.         proxy++;
  13012.     } else {
  13013.         if (!proxy)
  13014.           return;
  13015.         ip = &proxstruct;
  13016.         op = &tmpstruct;
  13017.         proxy = 0;
  13018.     }
  13019.     ip->connect = connected;
  13020.     connected = op->connect;
  13021.     if (ftp_host) {
  13022.         strncpy(ip->name, ftp_host, MAXHOSTNAMELEN - 1);
  13023.         ip->name[MAXHOSTNAMELEN - 1] = '\0';
  13024.         ip->name[strlen(ip->name)] = '\0';
  13025.     } else
  13026.       ip->name[0] = 0;
  13027.     ftp_host = op->name;
  13028.     ip->hctl = hisctladdr;
  13029.     hisctladdr = op->hctl;
  13030.     ip->mctl = myctladdr;
  13031.     myctladdr = op->mctl;
  13032.     ip->in = csocket;
  13033.     csocket = op->in;
  13034.     ip->out = csocket;
  13035.     csocket = op->out;
  13036.     ip->tpe = ftp_typ;
  13037.     ftp_typ = op->tpe;
  13038.     ip->curtpe = curtype;
  13039.     curtype = op->curtpe;
  13040.     ip->cpnd = cpend;
  13041.     cpend = op->cpnd;
  13042.     ip->sunqe = ftp_usn;
  13043.     ftp_usn = op->sunqe;
  13044.     ip->mcse = mcase;
  13045.     mcase = op->mcse;
  13046.     ip->ntflg = ntflag;
  13047.     ntflag = op->ntflg;
  13048.     strncpy(ip->nti, ntin, 16);
  13049.     (ip->nti)[strlen(ip->nti)] = '\0';
  13050.     strcpy(ntin, op->nti);
  13051.     strncpy(ip->nto, ntout, 16);
  13052.     (ip->nto)[strlen(ip->nto)] = '\0';
  13053.     strcpy(ntout, op->nto);
  13054.     ip->mapflg = mapflag;
  13055.     mapflag = op->mapflg;
  13056.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  13057.     (ip->mi)[strlen(ip->mi)] = '\0';
  13058.     strcpy(mapin, op->mi);
  13059.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  13060.     (ip->mo)[strlen(ip->mo)] = '\0';
  13061.     strcpy(mapout, op->mo);
  13062.     ip->authtype = auth_type;
  13063.     auth_type = op->authtype;
  13064.     ip->clvl = ftp_cpl;
  13065.     ftp_cpl = op->clvl;
  13066.     ip->dlvl = ftp_dpl;
  13067.     ftp_dpl = op->dlvl;
  13068.     if (!ftp_cpl)
  13069.       ftp_cpl = FPL_CLR;
  13070.     if (!ftp_dpl)
  13071.       ftp_dpl = FPL_CLR;
  13072. #ifdef FTP_KRB4
  13073.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  13074.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  13075.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  13076.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  13077. #endif /* FTP_KRB4 */
  13078. #ifdef FTP_GSSAPI
  13079.     ip->gcontext = gcontext;
  13080.     gcontext = op->gcontext;
  13081. #endif /* GSSAPI */
  13082.     signal(SIGINT, oldintr);
  13083.     if (cancelfile) {
  13084.         cancelfile = 0;
  13085.         debug(F101,"pswitch cancelfile B","",cancelfile);
  13086.         (*oldintr)(SIGINT);
  13087.     }
  13088. }
  13089.  
  13090. static sigtype
  13091. cancelpt(sig) int sig; {
  13092.     printf("\n");
  13093.     fflush(stdout);
  13094.     ptabflg++;
  13095.     cancelfile = 0;
  13096. #ifndef OS2
  13097.     longjmp(ptcancel, 1);
  13098. #else
  13099.     PostCtrlCSem();
  13100. #endif /* OS2 */
  13101. }
  13102.  
  13103. void
  13104. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  13105.     sig_t oldintr;
  13106.     int secndflag = 0, prox_type, nfnd;
  13107.     char *cmd2;
  13108. #ifdef BSDSELECT
  13109.     fd_set mask;
  13110. #endif /* BSDSELECT */
  13111.     sigtype cancelpt();
  13112.  
  13113.     if (strcmp(cmd, "RETR"))
  13114.       cmd2 = "RETR";
  13115.     else
  13116.       cmd2 = unique ? "STOU" : "STOR";
  13117.     if ((prox_type = type) == 0) {
  13118.         if (servertype == SYS_UNIX && unix_proxy)
  13119.           prox_type = FTT_BIN;
  13120.         else
  13121.           prox_type = FTT_ASC;
  13122.     }
  13123.     if (curtype != prox_type)
  13124.       changetype(prox_type, 1);
  13125.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  13126.         printf("Proxy server does not support third party transfers.\n");
  13127.         return;
  13128.     }
  13129.     pswitch(0);
  13130.     if (!connected) {
  13131.         printf("No primary connection\n");
  13132.         pswitch(1);
  13133.         ftpcode = -1;
  13134.         return;
  13135.     }
  13136.     if (curtype != prox_type)
  13137.       changetype(prox_type, 1);
  13138.  
  13139.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  13140.         pswitch(1);
  13141.         return;
  13142.     }
  13143.  
  13144.     /* Replace with calls to cc_execute() */
  13145.     if (setjmp(ptcancel))
  13146.       goto cancel;
  13147.     oldintr = signal(SIGINT, cancelpt);
  13148.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  13149.         signal(SIGINT, oldintr);
  13150.         pswitch(1);
  13151.         return;
  13152.     }
  13153.     sleep(2000);
  13154.     pswitch(1);
  13155.     secndflag++;
  13156.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  13157.       goto cancel;
  13158.     ptflag++;
  13159.     getreply(0,-1,-1,ftp_vbm,0);
  13160.     pswitch(0);
  13161.     getreply(0,-1,-1,ftp_vbm,0);
  13162.     signal(SIGINT, oldintr);
  13163.     pswitch(1);
  13164.     ptflag = 0;
  13165.     return;
  13166.  
  13167.   cancel:
  13168.     signal(SIGINT, SIG_IGN);
  13169.     ptflag = 0;
  13170.     if (strcmp(cmd, "RETR") && !proxy)
  13171.       pswitch(1);
  13172.     else if (!strcmp(cmd, "RETR") && proxy)
  13173.       pswitch(0);
  13174.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  13175.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13176.             pswitch(0);
  13177.             if (cpend)
  13178.               cancel_remote(0);
  13179.         }
  13180.         pswitch(1);
  13181.         if (ptabflg)
  13182.           ftpcode = -1;
  13183.         signal(SIGINT, oldintr);
  13184.         return;
  13185.     }
  13186.     if (cpend)
  13187.       cancel_remote(0);
  13188.     pswitch(!proxy);
  13189.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  13190.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13191.             pswitch(0);
  13192.             if (cpend)
  13193.               cancel_remote(0);
  13194.             pswitch(1);
  13195.             if (ptabflg)
  13196.               ftpcode = -1;
  13197.             signal(SIGINT, oldintr);
  13198.             return;
  13199.         }
  13200.     }
  13201.     if (cpend)
  13202.       cancel_remote(0);
  13203.     pswitch(!proxy);
  13204.     if (cpend) {
  13205. #ifdef BSDSELECT
  13206.         FD_ZERO(&mask);
  13207.         FD_SET(csocket, &mask);
  13208.         if ((nfnd = empty(&mask, 10)) <= 0) {
  13209.             if (nfnd < 0) {
  13210.                 perror("cancel");
  13211.             }
  13212.             if (ptabflg)
  13213.               ftpcode = -1;
  13214.             lostpeer();
  13215.         }
  13216. #else /* BSDSELECT */
  13217. #ifdef IBMSELECT
  13218.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  13219.             if (nfnd < 0) {
  13220.                 perror("cancel");
  13221.             }
  13222.             if (ptabflg)
  13223.               ftpcode = -1;
  13224.             lostpeer();
  13225.         }
  13226. #endif /* IBMSELECT */
  13227. #endif /* BSDSELECT */
  13228.         getreply(0,-1,-1,ftp_vbm,0);
  13229.         getreply(0,-1,-1,ftp_vbm,0);
  13230.     }
  13231.     if (proxy)
  13232.       pswitch(0);
  13233.     pswitch(1);
  13234.     if (ptabflg)
  13235.       ftpcode = -1;
  13236.     signal(SIGINT, oldintr);
  13237. }
  13238. #endif /* FTP_PROXY */
  13239.  
  13240. #ifdef FTP_SECURITY
  13241. #ifdef FTP_GSSAPI
  13242.  
  13243. struct {
  13244.     CONST gss_OID_desc * CONST * mech_type;
  13245.     char *service_name;
  13246. } gss_trials[] = {
  13247.     { &gss_mech_krb5, "ftp" },
  13248.     { &gss_mech_krb5, "host" },
  13249. };
  13250.  
  13251. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  13252. #endif /* FTP_GSSAPI */
  13253.  
  13254. static int
  13255. ftp_auth() {
  13256.     extern int setsafe();
  13257.     int j = 0, n;
  13258. #ifdef FTP_KRB4
  13259.     char *service, inst[INST_SZ];
  13260.     ULONG cksum;
  13261.     ULONG checksum = (ULONG) getpid();
  13262.     CHAR out_buf[FTP_BUFSIZ];
  13263.     int i;
  13264. #else /* FTP_KRB4 */
  13265. #ifdef FTP_GSSAPI
  13266.     CHAR out_buf[FTP_BUFSIZ];
  13267.     int i;
  13268. #endif /* FTP_GSSAPI */
  13269. #endif /* FTP_KRB4 */
  13270.  
  13271.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  13272.         return(0);
  13273.  
  13274.     if (auth_type)
  13275.       return(1);                        /* auth already succeeded */
  13276.  
  13277.     /* Try each auth type as specified by the end user */
  13278.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  13279. #ifdef FTP_GSSAPI
  13280.         if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  13281.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  13282.             if (n == REPLY_CONTINUE) {
  13283.                 OM_uint32 maj_stat, min_stat;
  13284.                 gss_name_t target_name;
  13285.                 gss_buffer_desc send_tok, recv_tok, *token_ptr;
  13286.                 char stbuf[FTP_BUFSIZ];
  13287.                 int comcode, trial;
  13288.                 struct gss_channel_bindings_struct chan;
  13289.                 char * realm = NULL;
  13290.                 char tgt[256];
  13291.  
  13292.                 chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  13293.                 chan.initiator_address.length = 4;
  13294.                 chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  13295.                 chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  13296.                 chan.acceptor_address.length = 4;
  13297.                 chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  13298.                 chan.application_data.length = 0;
  13299.                 chan.application_data.value = 0;
  13300.  
  13301.                 if (!quiet)
  13302.                   printf("GSSAPI accepted as authentication type\n");
  13303.  
  13304.                 realm = ck_krb5_realmofhost(ftp_user_host);
  13305.                 if (realm) {
  13306.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  13307.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  13308.                     if ( krb5_autoget &&
  13309.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  13310.                             (ck_krb5_is_tgt_valid() > 0)) )
  13311.                         ck_krb5_autoget_TGT(realm);
  13312.                 }
  13313.  
  13314.                 /* Blob from gss-client */
  13315.                 for (trial = 0; trial < n_gss_trials; trial++) {
  13316.                     /* ftp@hostname first, the host@hostname */
  13317.                     /* the V5 GSSAPI binding canonicalizes this for us... */
  13318.                     ckmakmsg(stbuf,FTP_BUFSIZ,
  13319.                              gss_trials[trial].service_name,
  13320.                              "@",
  13321.                              ftp_user_host,
  13322.                              NULL
  13323.                              );
  13324.                     if (ftp_deb)
  13325.                       fprintf(stderr,
  13326.                               "Authenticating to <%s>...\n", stbuf);
  13327.                     send_tok.value = stbuf;
  13328.                     send_tok.length = strlen(stbuf);
  13329.                     maj_stat = gss_import_name(&min_stat, &send_tok,
  13330.                                                gss_nt_service_name,
  13331.                                                &target_name
  13332.                                                );
  13333.                     if (maj_stat != GSS_S_COMPLETE) {
  13334.                         user_gss_error(maj_stat, min_stat, "parsing name");
  13335.                         secure_error("name parsed <%s>\n", stbuf);
  13336.                         continue;
  13337.                     }
  13338.                     token_ptr = GSS_C_NO_BUFFER;
  13339.                     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  13340.  
  13341.                     do {
  13342.                         if (ftp_deb)
  13343.                           fprintf(stderr, "calling gss_init_sec_context\n");
  13344.                         maj_stat =
  13345.                           gss_init_sec_context(&min_stat,
  13346.                                                GSS_C_NO_CREDENTIAL,
  13347.                                                &gcontext,
  13348.                                                target_name,
  13349.                                                (gss_OID) *
  13350.                                                  gss_trials[trial].mech_type,
  13351.                                                GSS_C_MUTUAL_FLAG |
  13352.                                                GSS_C_REPLAY_FLAG |
  13353.                                                (ftp_cfw ?
  13354.                                                 GSS_C_DELEG_FLAG : 0),
  13355.                                                0,
  13356.                                                 /* channel bindings */
  13357.                                                 (krb5_d_no_addresses ?
  13358.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  13359.                                                   &chan),
  13360.                                                 token_ptr,
  13361.                                                NULL,    /* ignore mech type */
  13362.                                                &send_tok,
  13363.                                                NULL,    /* ignore ret_flags */
  13364.                                                NULL
  13365.                                                );       /* ignore time_rec */
  13366.  
  13367.                         if (maj_stat != GSS_S_COMPLETE &&
  13368.                             maj_stat != GSS_S_CONTINUE_NEEDED) {
  13369.                             if (trial == n_gss_trials-1)
  13370.                               user_gss_error(maj_stat,
  13371.                                              min_stat,
  13372.                                              "initializing context"
  13373.                                              );
  13374.                             gss_release_name(&min_stat, &target_name);
  13375.                             /* maybe we missed on the service name */
  13376.                             goto outer_loop;
  13377.                         }
  13378.                         if (send_tok.length != 0) {
  13379.                             int len;
  13380.                             reply_parse = "ADAT="; /* for ftpcmd() later */
  13381.                             len = FTP_BUFSIZ;
  13382.                             kerror =
  13383.                               radix_encode(send_tok.value,
  13384.                                            out_buf,
  13385.                                            send_tok.length,
  13386.                                            &len,
  13387.                                            RADIX_ENCODE
  13388.                                            );
  13389.                             if (kerror)  {
  13390.                                 fprintf(stderr,
  13391.                                         "Base 64 encoding failed: %s\n",
  13392.                                         radix_error(kerror)
  13393.                                         );
  13394.                                 goto gss_complete_loop;
  13395.                             }
  13396.                             comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  13397.                             if (comcode != REPLY_COMPLETE
  13398.                                 /* && comcode != 3 (335)*/
  13399.                                 ) {
  13400.                                 if (trial == n_gss_trials-1) {
  13401.                                     fprintf(stderr, "GSSAPI ADAT failed\n");
  13402.                                     /* force out of loop */
  13403.                                     maj_stat = GSS_S_FAILURE;
  13404.                                 }
  13405.                                 /*
  13406.                                   Backoff to the v1 gssapi is still possible.
  13407.                                   Send a new AUTH command.  If that fails,
  13408.                                   terminate the loop.
  13409.                                 */
  13410.                                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  13411.                                     != REPLY_CONTINUE) {
  13412.                                     fprintf(stderr,
  13413.                                 "GSSAPI ADAT failed, AUTH restart failed\n");
  13414.                                     /* force out of loop */
  13415.                                     maj_stat = GSS_S_FAILURE;
  13416.                                 }
  13417.                                 goto outer_loop;
  13418.                             }
  13419.                             if (!reply_parse) {
  13420.                                 fprintf(stderr,
  13421.                               "No authentication data received from server\n");
  13422.                                 if (maj_stat == GSS_S_COMPLETE) {
  13423.                                     fprintf(stderr,
  13424.                                             "...but no more was needed\n");
  13425.                                     goto gss_complete_loop;
  13426.                                 } else {
  13427.                                     user_gss_error(maj_stat,
  13428.                                                    min_stat,
  13429.                                                    "no reply, huh?"
  13430.                                                    );
  13431.                                     goto gss_complete_loop;
  13432.                                 }
  13433.                             }
  13434.                             len = FTP_BUFSIZ;
  13435.                             kerror = radix_encode(reply_parse,out_buf,i,&len,
  13436.                                                   RADIX_DECODE);
  13437.                             if (kerror) {
  13438.                                 fprintf(stderr,
  13439.                                         "Base 64 decoding failed: %s\n",
  13440.                                         radix_error(kerror));
  13441.                                 goto gss_complete_loop;
  13442.                             }
  13443.  
  13444.                             /* everything worked */
  13445.                             token_ptr = &recv_tok;
  13446.                             recv_tok.value = out_buf;
  13447.                             recv_tok.length = len;
  13448.                             continue;
  13449.  
  13450.                             /* get out of loop clean */
  13451.                           gss_complete_loop:
  13452.                             trial = n_gss_trials-1;
  13453.                             gss_release_buffer(&min_stat, &send_tok);
  13454.                             gss_release_name(&min_stat, &target_name);
  13455.                             goto outer_loop;
  13456.                         }
  13457.                     } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  13458.  
  13459.                   outer_loop:
  13460.                     if (maj_stat == GSS_S_COMPLETE)
  13461.                       break;
  13462.                 }
  13463.                 if (maj_stat == GSS_S_COMPLETE) {
  13464.                     printf("GSSAPI authentication succeeded\n");
  13465.                     reply_parse = NULL;
  13466.                     auth_type = "GSSAPI";
  13467.                     return(1);
  13468.                 } else {
  13469.                     fprintf(stderr, "GSSAPI authentication failed\n");
  13470.                     reply_parse = NULL;
  13471.                 }
  13472.             } else {
  13473.                 if (ftp_deb)
  13474.                 fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  13475.                 if (ftpcode == 500 || ftpcode == 502)
  13476.                     return(0);
  13477.             }
  13478.         }
  13479. #endif /* FTP_GSSAPI */
  13480. #ifdef FTP_SRP
  13481.         if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  13482.             if (srp_ftp_auth(ftp_user_host,NULL,NULL))
  13483.               return(1);
  13484.             else if (ftpcode == 500 || ftpcode == 502)
  13485.               return(0);
  13486.         }
  13487. #endif /* FTP_SRP */
  13488. #ifdef FTP_KRB4
  13489.         if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  13490.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  13491.             if (n == REPLY_CONTINUE) {
  13492.                 char tgt[4*REALM_SZ+1];
  13493.                 int rc;
  13494.  
  13495.                 if (!quiet)
  13496.                   printf("KERBEROS_V4 accepted as authentication type\n");
  13497.                 ckstrncpy(inst, (char *) krb_get_phost(ftp_user_host),INST_SZ);
  13498.                 ckstrncpy(ftp_realm,
  13499.                           (char *)ck_krb4_realmofhost(ftp_user_host),
  13500.                           REALM_SZ
  13501.                           );
  13502.  
  13503.                 ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  13504.                 rc = ck_krb4_tkt_isvalid(tgt);
  13505.  
  13506.                 if (rc <= 0 && krb4_autoget)
  13507.                   ck_krb4_autoget_TGT(ftp_realm);
  13508.  
  13509.                 service = "ftp";
  13510.                 kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  13511.                 if (kerror == KDC_PR_UNKNOWN) {
  13512.                     service = "rcmd";
  13513.                     kerror = krb_mk_req(&ftp_tkt,
  13514.                                         service,
  13515.                                         inst,
  13516.                                         ftp_realm,
  13517.                                         checksum
  13518.                                         );
  13519.                 }
  13520.                 if (kerror)
  13521.                   fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  13522.                           krb_get_err_text(kerror));
  13523.                 if (!kerror) {
  13524.                     kerror = krb_get_cred(service, inst, ftp_realm,
  13525.                                           (CREDENTIALS *)&ftp_cred);
  13526.                     if (kerror)
  13527.                       fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  13528.                               krb_get_err_text(kerror));
  13529.                 }
  13530.                 if (!kerror) {
  13531.                     int rc;
  13532.                     rc = des_key_sched(ftp_cred.session, ftp_sched);
  13533.                     if (rc == -1) {
  13534.                        printf("?Invalid DES key specified in credentials\r\n");
  13535.                        debug(F110,"ftp_auth",
  13536.                              "invalid DES Key specified in credentials",0);
  13537.                     } else if ( rc == -2 ) {
  13538.                         printf("?Weak DES key specified in credentials\r\n");
  13539.                         debug(F110,"ftp_auth",
  13540.                               "weak DES Key specified in credentials",0);
  13541.                     } else if ( rc != 0 ) {
  13542.                         printf("?DES Key Schedule not set by credentials\r\n");
  13543.                         debug(F110,"ftp_auth",
  13544.                               "DES Key Schedule not set by credentials",0);
  13545.                     }
  13546.                     reply_parse = "ADAT=";
  13547.                     i = FTP_BUFSIZ;
  13548.                     kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  13549.                                           &i, RADIX_ENCODE);
  13550.                     if (kerror) {
  13551.                         fprintf(stderr, "Base 64 encoding failed: %s\n",
  13552.                                 radix_error(kerror));
  13553.                         goto krb4_err;
  13554.                     }
  13555.                     if (i > FTP_BUFSIZ - 6)
  13556.                       printf("?ADAT data too long\n");
  13557.                     if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  13558.                         REPLY_COMPLETE) {
  13559.                         fprintf(stderr, "Kerberos V4 authentication failed\n");
  13560.                         goto krb4_err;
  13561.                     }
  13562.                     if (!reply_parse) {
  13563.                         fprintf(stderr,
  13564.                              "No authentication data received from server\n");
  13565.                         goto krb4_err;
  13566.                     }
  13567.                     i = sizeof(out_buf);
  13568.                     kerror =
  13569.                       radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  13570.                     if (kerror) {
  13571.                         fprintf(stderr, "Base 64 decoding failed: %s\n",
  13572.                                 radix_error(kerror));
  13573.                         goto krb4_err;
  13574.                     }
  13575.                     kerror = krb_rd_safe(out_buf, i,
  13576. #ifdef KRB524
  13577.                                          ftp_cred.session,
  13578. #else /* KRB524 */
  13579.                                          &ftp_cred.session,
  13580. #endif /* KRB524 */
  13581.                                          &hisctladdr,
  13582.                                          &myctladdr,
  13583.                                          &ftp_msg_data
  13584.                                          );
  13585.                     if (kerror) {
  13586.                         fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  13587.                                 krb_get_err_text(kerror));
  13588.                         goto krb4_err;
  13589.                     }
  13590.  
  13591.                     /* fetch the (modified) checksum */
  13592.                     memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  13593.                     if (ntohl(cksum) == checksum + 1) {
  13594.                         if (ftp_vbm)
  13595.                           printf("Kerberos V4 authentication succeeded\n");
  13596.                         reply_parse = NULL;
  13597.                         auth_type = "KERBEROS_V4";
  13598.                         return(1);
  13599.                     } else
  13600.                       fprintf(stderr,
  13601.                               "Kerberos V4 mutual authentication failed\n");
  13602.                   krb4_err:
  13603.                     reply_parse = NULL;
  13604.                 }
  13605.             } else {
  13606.                 if (ftp_deb)
  13607.           fprintf(stderr,
  13608.                       "KERBEROS_V4 rejected as an authentication type\n");
  13609.                 if (ftpcode == 500 || ftpcode == 502)
  13610.                     return(0);
  13611.             }
  13612.         }
  13613. #endif /* FTP_KRB4 */
  13614. #ifdef CK_SSL
  13615.         if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  13616. #ifdef FTPHOST
  13617.             if (!hostcmd) {
  13618.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13619.                 hostcmd = 1;
  13620.             }
  13621. #endif /* FTPHOST */
  13622.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  13623.             if (n != REPLY_COMPLETE)
  13624.               n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  13625.             if (n == REPLY_COMPLETE) {
  13626.                 if (!quiet)
  13627.                   printf("TLS accepted as authentication type\n");
  13628.  
  13629.                 auth_type = "TLS";
  13630.                 ssl_auth();
  13631.                 if (ssl_ftp_active_flag ) {
  13632.                     ftp_dpl = FPL_CLR;
  13633.                     ftp_cpl = FPL_PRV;
  13634.                     return(1);
  13635.                 } else {
  13636.                     fprintf(stderr,"TLS authentication failed\n");
  13637.                     auth_type = NULL;
  13638. #ifdef TCPIPLIB
  13639.                     socket_close(csocket);
  13640. #else /* TCPIPLIB */
  13641. #ifdef USE_SHUTDOWN
  13642.                     shutdown(csocket, 1+1);
  13643. #endif /* USE_SHUTDOWN */
  13644.                     close(csocket);
  13645. #endif /* TCPIPLIB */
  13646.                     csocket = -1;
  13647.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13648.                       return(0);
  13649.                 }
  13650.             } else {
  13651.                 if (ftp_deb)
  13652.           fprintf(stderr,"TLS rejected as an authentication type\n");
  13653.                 if (ftpcode == 500 || ftpcode == 502)
  13654.                     return(0);
  13655.             }
  13656.         }
  13657.         if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  13658. #ifdef FTPHOST
  13659.             if (!hostcmd) {
  13660.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13661.                 hostcmd = 1;
  13662.             }
  13663. #endif /* FTPHOST */
  13664.             n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  13665.             if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  13666.                 if (!quiet)
  13667.                   printf("SSL accepted as authentication type\n");
  13668.                 auth_type = "SSL";
  13669.                 ssl_auth();
  13670.                 if (ssl_ftp_active_flag) {
  13671.                     ftp_dpl = FPL_PRV;
  13672.                     ftp_cpl = FPL_PRV;
  13673.                     setprotbuf(1<<20);
  13674.                     return(1);
  13675.                 } else {
  13676.                     fprintf(stderr,"SSL authentication failed\n");
  13677.                     auth_type = NULL;
  13678. #ifdef TCPIPLIB
  13679.                     socket_close(csocket);
  13680. #else /* TCPIPLIB */
  13681. #ifdef USE_SHUTDOWN
  13682.                     shutdown(csocket, 1+1);
  13683. #endif /* USE_SHUTDOWN */
  13684.                     close(csocket);
  13685. #endif /* TCPIPLIB */
  13686.                     csocket = -1;
  13687.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13688.                       return(0);
  13689.                 }
  13690.         } else {
  13691.                 if (ftp_deb)
  13692.           fprintf(stderr, "SSL rejected as an authentication type\n");
  13693.                 if (ftpcode == 500 || ftpcode == 502)
  13694.           return(0);
  13695.             }
  13696.         }
  13697. #endif /* CK_SSL */
  13698.         /* Other auth types go here ... */
  13699.     } /* for (j;;) */
  13700.     return(0);
  13701. }
  13702. #endif /* FTP_SECURITY */
  13703.  
  13704. static int
  13705. #ifdef CK_ANSIC
  13706. setprotbuf(unsigned int size)
  13707. #else
  13708. setprotbuf(size) unsigned int size;
  13709. #endif /* CK_ANSIC */
  13710. /* setprotbuf */ {
  13711.     if (ucbuf)
  13712.       free(ucbuf);
  13713.     ucbuf = NULL;
  13714.     ucbufsiz = 0;
  13715.     actualbuf = size;
  13716.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  13717.         if (actualbuf)
  13718.           actualbuf /= 2;
  13719.         else
  13720.           return(0);
  13721.     }
  13722.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  13723.     debug(F101,"setprotbuf ucbufsiz","",ucbufsiz);
  13724.     if (ucbufsiz < 128) {
  13725.         printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  13726.     } else if (ucbufsiz < 0) {
  13727.         printf("ERROR: ucbuf allocation failure\n");
  13728.         return(-1);
  13729.     }
  13730.     maxbuf = actualbuf;
  13731.     return(1);
  13732. }
  13733.  
  13734. static int
  13735. #ifdef CK_ANSIC
  13736. setpbsz(unsigned int size)
  13737. #else
  13738. setpbsz(size) unsigned int size;
  13739. #endif /* CK_ANSIC */
  13740. /* setpbsz */ {
  13741.     if (!setprotbuf(size)) {
  13742.         perror("?Error while trying to malloc PROT buffer:");
  13743. #ifdef FTP_SRP
  13744.         srp_reset();
  13745. #endif /* FTP_SRP */
  13746.         ftpclose();
  13747.         return(-1);
  13748.     }
  13749.     reply_parse = "PBSZ=";
  13750.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  13751. #ifdef CK_SSL
  13752.              ssl_ftp_active_flag ? "0" :
  13753. #endif /* CK_SSL */
  13754.              ckuitoa(actualbuf),NULL,NULL);
  13755.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  13756.         if (connected) {
  13757.             printf("?Unable to negotiate PROT buffer size with FTP server\n");
  13758.             ftpclose();
  13759.         }
  13760.         return(-1);
  13761.     }
  13762.     if (reply_parse) {
  13763.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  13764.           maxbuf = actualbuf;
  13765.     } else
  13766.       maxbuf = actualbuf;
  13767.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  13768.     debug(F101,"setpbsz ucbufsiz","",ucbufsiz);    
  13769.     reply_parse = NULL;
  13770.     return(0);
  13771. }
  13772.  
  13773. static VOID
  13774. cancel_remote(din) int din; {
  13775.     CHAR buf[FTP_BUFSIZ];
  13776.     int x, nfnd;
  13777. #ifdef BSDSELECT
  13778.     fd_set mask;
  13779. #endif /* BSDSELECT */
  13780. #ifdef IBMSELECT
  13781.     int fds[2], fdcnt = 0;
  13782. #endif /* IBMSELECT */
  13783. #ifdef DEBUG
  13784.     extern int debtim;
  13785.     int xdebtim;
  13786.     xdebtim = debtim;
  13787.     debtim = 1;
  13788. #endif /* DEBUG */
  13789.     debug(F100,"ftp cancel_remote entry","",0);
  13790. #ifdef CK_SSL
  13791.     if (ssl_ftp_active_flag) {
  13792.         /*
  13793.          * Send Telnet IP, Telnet DM but do so inline and within the
  13794.          * TLS channel
  13795.          */
  13796.         int count, error;
  13797.  
  13798.         buf[0] = IAC;
  13799.         buf[1] = TN_IP;
  13800.         buf[2] = IAC;
  13801.         buf[3] = TN_DM;
  13802.         buf[4] = NUL;
  13803.  
  13804.         count = SSL_write(ssl_ftp_con, buf, 4);
  13805.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  13806.         error = SSL_get_error(ssl_ftp_con,count);
  13807.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  13808.         switch (error) {
  13809.           case SSL_ERROR_NONE:
  13810.             break;
  13811.           case SSL_ERROR_WANT_WRITE:
  13812.           case SSL_ERROR_WANT_READ:
  13813.           case SSL_ERROR_SYSCALL:
  13814. #ifdef NT
  13815.             {
  13816.                 int gle = GetLastError();
  13817.             }
  13818. #endif /* NT */
  13819.           case SSL_ERROR_WANT_X509_LOOKUP:
  13820.           case SSL_ERROR_SSL:
  13821.           case SSL_ERROR_ZERO_RETURN:
  13822.           default:
  13823.             lostpeer();
  13824.             return;
  13825.         }
  13826.     } else
  13827. #endif /* CK_SSL */
  13828.     {
  13829.         /*
  13830.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  13831.          * after urgent byte rather than before as is protocol now.
  13832.          */
  13833.         buf[0] = IAC;
  13834.         buf[1] = TN_IP;
  13835.         buf[2] = IAC;
  13836.         buf[3] = NUL;
  13837.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  13838.           perror("cancel");
  13839.         debug(F101,"ftp cancel_remote send 1","",x);
  13840.         buf[0] = TN_DM;
  13841.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  13842.         debug(F101,"ftp cancel_remote send 2","",x);
  13843.     }
  13844.     x = scommand("ABOR");
  13845.     debug(F101,"ftp cancel_remote scommand","",x);
  13846. #ifdef BSDSELECT
  13847.     FD_ZERO(&mask);
  13848.     FD_SET(csocket, &mask);
  13849.     if (din) {
  13850.         FD_SET(din, &mask);
  13851.     }
  13852.     nfnd = empty(&mask, 10);
  13853.     debug(F101,"ftp cancel_remote empty","",nfnd);
  13854.     if ((nfnd) <= 0) {
  13855.         if (nfnd < 0) {
  13856.             perror("cancel");
  13857.         }
  13858. #ifdef FTP_PROXY
  13859.         if (ptabflg)
  13860.           ftpcode = -1;
  13861. #endif /* FTP_PROXY */
  13862.         lostpeer();
  13863.     }
  13864.     debug(F110,"ftp cancel_remote","D",0);
  13865.     if (din && FD_ISSET(din, &mask)) {
  13866.         /* Security: No threat associated with this read. */
  13867.         /* But you can't simply read the TLS data stream  */
  13868. #ifdef CK_SSL
  13869.         if (ssl_ftp_data_active_flag) {
  13870.             int count, error;
  13871.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  13872.                     /* LOOP */ ;
  13873.         } else
  13874. #endif /* CK_SSL */
  13875.         {
  13876.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  13877.                 /* LOOP */ ;
  13878.         }
  13879.     }
  13880.     debug(F110,"ftp cancel_remote","E",0);
  13881. #else /* BSDSELECT */
  13882. #ifdef IBMSELECT
  13883.     fds[0] = csocket;
  13884.     fdcnt++;
  13885.     if (din) {
  13886.         fds[1] = din;
  13887.         fdcnt++;
  13888.     }
  13889.     nfnd = empty(fds, fdcnt, 10);
  13890.     debug(F101,"ftp cancel_remote empty","",nfnd);
  13891.     if ((nfnd) <= 0) {
  13892.         if (nfnd < 0) {
  13893.             perror("cancel");
  13894.         }
  13895. #ifdef FTP_PROXY
  13896.         if (ptabflg)
  13897.           ftpcode = -1;
  13898. #endif /* FTP_PROXY */
  13899.         lostpeer();
  13900.     }
  13901.     debug(F110,"ftp cancel_remote","D",0);
  13902.     if (din && select(&din, 1,0,0,1) ) {
  13903. #ifdef CK_SSL
  13904.         if (ssl_ftp_data_active_flag) {
  13905.             int count, error;
  13906.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  13907.                     /* LOOP */ ;
  13908.         } else
  13909. #endif /* CK_SSL */
  13910.         {
  13911.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  13912.                 /* LOOP */ ;
  13913.         }
  13914.     }
  13915.     debug(F110,"ftp cancel_remote","E",0);
  13916. #else /* IBMSELECT */
  13917.     Some form of select is required.
  13918. #endif /* IBMSELECT */
  13919. #endif /* BSDSELECT */
  13920.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  13921.         debug(F110,"ftp cancel_remote","F",0);
  13922.         /* 552 needed for NIC style cancel */
  13923.         getreply(0,-1,-1,ftp_vbm,0);
  13924.         debug(F110,"ftp cancel_remote","G",0);
  13925.     }
  13926.     debug(F110,"ftp cancel_remote","H",0);
  13927.     getreply(0,-1,-1,ftp_vbm,0);
  13928.     debug(F110,"ftp cancel_remote","I",0);
  13929. #ifdef DEBUG
  13930.     debtim = xdebtim;
  13931. #endif /* DEBUG */
  13932. }
  13933.  
  13934. static int
  13935. fts_dpl(x) int x; {
  13936.     if (!auth_type
  13937. #ifdef OS2
  13938.          || !ck_crypt_is_installed()
  13939. #endif /* OS2 */
  13940.          ) {
  13941.         switch ( x ) {
  13942.           case FPL_PRV:
  13943.             printf("?Cannot set protection level to PRIVATE\n");
  13944.             return(0);
  13945.           case FPL_SAF:
  13946.             printf("?Cannot set protection level to SAFE\n");
  13947.             return(0);
  13948.         }
  13949.         ftp_dpl = x;
  13950.         return(1);
  13951.     }
  13952.  
  13953. #ifdef CK_SSL
  13954.     if (x == FPL_SAF &&
  13955.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  13956.         printf("Cannot set protection level to safe\n");
  13957.         return(0);
  13958.     }
  13959. #endif /* CK_SSL */
  13960.     /* Start with a PBSZ of 1 meg */
  13961.     if (x != FPL_CLR) {
  13962.         if (setpbsz(DEFAULT_PBSZ) < 0)
  13963.           return(0);
  13964.     }
  13965.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  13966.                (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  13967.     if (y == REPLY_COMPLETE) {
  13968.         ftp_dpl = x;
  13969.         return(1);
  13970.     }
  13971.     return(0);
  13972. }
  13973.  
  13974. static int
  13975. fts_cpl(x) int x; {
  13976.     if (!auth_type 
  13977. #ifdef OS2
  13978.          || !ck_crypt_is_installed()
  13979. #endif /* OS2 */
  13980.          ) {
  13981.         switch ( x ) {
  13982.           case FPL_PRV:
  13983.             printf("?Cannot set protection level to PRIVATE\n");
  13984.             return(0);
  13985.           case FPL_SAF:
  13986.             printf("?Cannot set protection level to SAFE\n");
  13987.             return(0);
  13988.         }
  13989.         ftp_cpl = x;
  13990.         return(1);
  13991.     }
  13992.     if (x == FPL_CLR) {
  13993.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  13994.         if (y == REPLY_COMPLETE) {
  13995.             ftp_cpl = x;
  13996.             return(1);
  13997.         }
  13998.         return(0);
  13999.     }
  14000.     ftp_cpl = x;
  14001.     return(1);
  14002. }
  14003.  
  14004. #ifdef FTP_GSSAPI
  14005. static VOID
  14006. user_gss_error(maj_stat, min_stat, s)
  14007.     OM_uint32 maj_stat, min_stat;
  14008.     char *s;
  14009. {
  14010.     /* a lot of work just to report the error */
  14011.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  14012.     gss_buffer_desc msg;
  14013.     msg_ctx = 0;
  14014.     while (!msg_ctx) {
  14015.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  14016.                                        GSS_C_GSS_CODE,
  14017.                                        GSS_C_NULL_OID,
  14018.                                        &msg_ctx,
  14019.                                        &msg
  14020.                                        );
  14021.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14022.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14023.             fprintf(stderr, "GSSAPI error major: %s\n",
  14024.                     (char*)msg.value);
  14025.             gss_release_buffer(&gmin_stat, &msg);
  14026.         }
  14027.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14028.           break;
  14029.     }
  14030.     msg_ctx = 0;
  14031.     while (!msg_ctx) {
  14032.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  14033.                                        GSS_C_MECH_CODE,
  14034.                                        GSS_C_NULL_OID,
  14035.                                        &msg_ctx,
  14036.                                        &msg
  14037.                                        );
  14038.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14039.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14040.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  14041.             gss_release_buffer(&gmin_stat, &msg);
  14042.         }
  14043.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14044.           break;
  14045.     }
  14046.     fprintf(stderr, "GSSAPI error: %s\n", s);
  14047. }
  14048. #endif /* FTP_GSSAPI */
  14049.  
  14050. #ifndef NOMHHOST
  14051. #ifdef datageneral
  14052. #define NOMHHOST
  14053. #else
  14054. #ifdef HPUX5WINTCP
  14055. #define NOMHHOST
  14056. #endif /* HPUX5WINTCP */
  14057. #endif /* datageneral */
  14058. #endif /* NOMHHOST */
  14059.  
  14060. #ifdef INADDRX
  14061. static struct in_addr inaddrx;
  14062. #endif /* INADDRX */
  14063.  
  14064. static char *
  14065. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  14066.     register struct hostent *hp = 0;
  14067. #ifdef IP_TOS
  14068. #ifdef IPTOS_THROUGHPUT
  14069.     int tos;
  14070. #endif /* IPTOS_THROUGHPUT */
  14071. #endif /* IP_TOS */
  14072.     int s;
  14073.     GSOCKNAME_T len;
  14074.     static char hostnamebuf[MAXHOSTNAMELEN];
  14075.     char hostname[MAXHOSTNAMELEN] /* , *p, *q */ ;
  14076.     int  cport;
  14077. #ifdef DEBUG
  14078.     extern int debtim;
  14079.     int xdebtim;
  14080.     xdebtim = debtim;
  14081.     debtim = 1;
  14082. #endif /* DEBUG */
  14083.  
  14084. #ifndef NOHTTP
  14085.     if (tcp_http_proxy) {
  14086.         struct servent *destsp;
  14087.         char *p, *q;
  14088.  
  14089.         ckmakmsg(proxyhost,HTTPCPYL,host,":",ckuitoa(port),NULL);
  14090.         for (p = tcp_http_proxy, q = hostname;
  14091.              *p != '\0' && *p != ':';
  14092.              p++, q++
  14093.              )
  14094.           *q = *p;
  14095.         *q = '\0';
  14096.  
  14097.         if (*p == ':')
  14098.           p++;
  14099.         else
  14100.           p = "http";
  14101.  
  14102.         destsp = getservbyname(p,"tcp");
  14103.         if (destsp)
  14104.           cport = ntohs(destsp->s_port);
  14105.         else if (p) {
  14106.           cport = atoi(p);
  14107.         } else
  14108.           cport = 80;
  14109.     } else
  14110. #endif /* NOHTTP */
  14111.     {
  14112.         ckstrncpy(hostname,host,MAXHOSTNAMELEN);
  14113.         cport = port;
  14114.     }
  14115.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  14116.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  14117.     if (hisctladdr.sin_addr.s_addr != -1) {
  14118.         debug(F110,"ftp hookup A",hostname,0);
  14119.         hisctladdr.sin_family = AF_INET;
  14120.         ckstrncpy(hostnamebuf, hostname, MAXHOSTNAMELEN);
  14121.     } else {
  14122.         debug(F110,"ftp hookup B",hostname,0);
  14123.         hp = gethostbyname(hostname);
  14124. #ifdef HADDRLIST
  14125.         hp = ck_copyhostent(hp);        /* make safe copy that won't change */
  14126. #endif /* HADDRLIST */
  14127.         if (hp == NULL) {
  14128.             fprintf(stderr, "ftp: %s: Unknown host\n", host);
  14129.             ftpcode = -1;
  14130. #ifdef DEBUG
  14131.             debtim = xdebtim;
  14132. #endif /* DEBUG */
  14133.             return((char *) 0);
  14134.         }
  14135.         hisctladdr.sin_family = hp->h_addrtype;
  14136. #ifdef HADDRLIST
  14137.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  14138.                sizeof(hisctladdr.sin_addr));
  14139. #else /* HADDRLIST */
  14140.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  14141.                sizeof(hisctladdr.sin_addr));
  14142. #endif /* HADDRLIST */
  14143.         ckstrncpy(hostnamebuf, hp->h_name, MAXHOSTNAMELEN);
  14144.     }
  14145.     debug(F110,"ftp hookup C",hostnamebuf,0);
  14146.     ftp_host = hostnamebuf;
  14147.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14148.     debug(F101,"ftp hookup socket","",s);
  14149.     if (s < 0) {
  14150.         perror("ftp: socket");
  14151.         ftpcode = -1;
  14152. #ifdef DEBUG
  14153.         debtim = xdebtim;
  14154. #endif /* DEBUG */
  14155.         return(0);
  14156.     }
  14157.     hisctladdr.sin_port = htons(cport);
  14158.     errno = 0;
  14159. #ifdef HADDRLIST
  14160.     debug(F100,"ftp hookup HADDRLIST","",0);
  14161.     while
  14162. #else
  14163.     debug(F100,"ftp hookup no HADDRLIST","",0);
  14164.     if
  14165. #endif /* HADDRLIST */
  14166.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  14167.           debug(F101,"ftp hookup connect failed","",errno);
  14168. #ifdef HADDRLIST
  14169.           if (hp && hp->h_addr_list[1]) {
  14170.               int oerrno = errno;
  14171.  
  14172.               fprintf(stderr, "ftp: connect to address %s: ",
  14173.                       inet_ntoa(hisctladdr.sin_addr));
  14174.               errno = oerrno;
  14175.               perror((char *) 0);
  14176.               hp->h_addr_list++;
  14177.               memcpy((char *)&hisctladdr.sin_addr,
  14178.                      hp->h_addr_list[0],
  14179.                      sizeof(hisctladdr.sin_addr));
  14180.               fprintf(stdout, "Trying %s...\n",
  14181.                       inet_ntoa(hisctladdr.sin_addr));
  14182. #ifdef TCPIPLIB
  14183.               socket_close(s);
  14184. #else /* TCPIPLIB */
  14185.               close(s);
  14186. #endif /* TCPIPLIB */
  14187.               s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14188.               if (s < 0) {
  14189.                   perror("ftp: socket");
  14190.                   ftpcode = -1;
  14191. #ifdef DEBUG
  14192.                   debtim = xdebtim;
  14193. #endif /* DEBUG */
  14194.                   return(0);
  14195.               }
  14196.               continue;
  14197.           }
  14198. #endif /* HADDRLIST */
  14199.           perror("ftp: connect");
  14200.           ftpcode = -1;
  14201.           goto bad;
  14202.       }
  14203.     debug(F100,"ftp hookup connect ok","",0);
  14204.  
  14205.     len = sizeof (myctladdr);
  14206.     errno = 0;
  14207.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  14208.         debug(F101,"ftp hookup getsockname failed","",errno);
  14209.         perror("ftp: getsockname");
  14210.         ftpcode = -1;
  14211.         goto bad;
  14212.     }
  14213.     debug(F100,"ftp hookup getsockname ok","",0);
  14214.  
  14215. #ifndef NOHTTP
  14216.     if (tcp_http_proxy) {
  14217. #ifdef OS2
  14218.         char * agent = "Kermit 95";     /* Default user agent */
  14219. #else
  14220.         char * agent = "C-Kermit";
  14221. #endif /* OS2 */
  14222.  
  14223.         if (http_connect(s,agent,NULL,
  14224.                          tcp_http_proxy_user,
  14225.                          tcp_http_proxy_pwd,
  14226.                          0,
  14227.                          proxyhost
  14228.                          ) < 0) {
  14229.             char * foo = NULL;
  14230. #ifdef TCPIPLIB
  14231.             socket_close(s);
  14232. #else /* TCPIPLIB */
  14233.             close(s);
  14234. #endif /* TCPIPLIB */
  14235.  
  14236.             while (foo == NULL && tcp_http_proxy != NULL ) {
  14237.  
  14238.                 if (tcp_http_proxy_errno == 401 ||
  14239.                      tcp_http_proxy_errno == 407 ) {
  14240.                     char uid[UIDBUFLEN];
  14241.                     char pwd[PWDSIZ];
  14242.                     struct txtbox tb[2];
  14243.                     int ok;
  14244.  
  14245.                     tb[0].t_buf = uid;
  14246.                     tb[0].t_len = UIDBUFLEN;
  14247.                     tb[0].t_lbl = "Proxy Userid: ";
  14248.                     tb[0].t_dflt = NULL;
  14249.                     tb[0].t_echo = 1;
  14250.                     tb[1].t_buf = pwd;
  14251.                     tb[1].t_len = 256;
  14252.                     tb[1].t_lbl = "Proxy Passphrase: ";
  14253.                     tb[1].t_dflt = NULL;
  14254.                     tb[1].t_echo = 2;
  14255.  
  14256.                     ok = uq_mtxt("Proxy Server Authentication Required\n",
  14257.                                   NULL, 2, tb);
  14258.  
  14259.                     if (ok && uid[0]) {
  14260.                         char * proxy_user, * proxy_pwd;
  14261.  
  14262.                         proxy_user = tcp_http_proxy_user;
  14263.                         proxy_pwd  = tcp_http_proxy_pwd;
  14264.  
  14265.                         tcp_http_proxy_user = uid;
  14266.                         tcp_http_proxy_pwd = pwd;
  14267.  
  14268.                         foo = ftp_hookup(host, port, 0);
  14269.  
  14270.                         debug(F110,"ftp_hookup()",foo,0);
  14271.                         memset(pwd,0,PWDSIZ);
  14272.                         tcp_http_proxy_user = proxy_user;
  14273.                         tcp_http_proxy_pwd = proxy_pwd;
  14274.                     } else
  14275.                         break;
  14276.                 } else
  14277.                     break;
  14278.             }
  14279.             if (foo != NULL)
  14280.               return(foo);
  14281.             perror("ftp: connect");
  14282.             ftpcode = -1;
  14283.             goto bad;
  14284.         }
  14285.         ckstrncpy(hostnamebuf, proxyhost, MAXHOSTNAMELEN);
  14286.     }
  14287. #endif /* NOHTTP */
  14288.  
  14289.     csocket = s;
  14290.  
  14291. #ifdef CK_SSL
  14292.     if (tls) {
  14293.         /* FTP over SSL
  14294.          * If the connection is over an SSL proxy then the
  14295.          * auth_type will be NULL.  However, I'm not sure
  14296.          * whether we should protect the data channel in
  14297.          * that case or not.
  14298.          */
  14299.  
  14300.         debug(F100,"ftp hookup use_tls","",0);
  14301.         if (!ssl_auth()) {
  14302.             debug(F100,"ftp hookup ssl_auth failed","",0);
  14303.             auth_type = NULL;
  14304.             ftpcode = -1;
  14305.             csocket = -1;
  14306.             goto bad;
  14307.         }
  14308.         ssl_ftp_proxy = 1;
  14309.     }
  14310. #endif /* CK_SSL */
  14311.  
  14312. #ifdef IP_TOS
  14313. #ifdef IPTOS_LOWDELAY
  14314.     tos = IPTOS_LOWDELAY;
  14315.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  14316.       perror("ftp: setsockopt TOS (ignored)");
  14317. #endif
  14318. #endif
  14319.     if (!quiet)
  14320.       printf("Connected to %s.\n", host);
  14321.  
  14322.     /* Read greeting from server */
  14323.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  14324.         debug(F100,"ftp hookup bad reply","",0);
  14325. #ifdef TCPIPLIB
  14326.         socket_close(csocket);
  14327. #else /* TCPIPLIB */
  14328.         close(csocket);
  14329. #endif /* TCPIPLIB */
  14330.         ftpcode = -1;
  14331.         goto bad;
  14332.     }
  14333. #ifdef SO_OOBINLINE
  14334.     {
  14335.         int on = 1;
  14336.         errno = 0;
  14337.         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  14338.                sizeof(on)) < 0) {
  14339.             perror("ftp: setsockopt");
  14340.             debug(F101,"ftp hookup setsockopt failed","",errno);
  14341.         }
  14342. #ifdef DEBUG
  14343.         else
  14344.           debug(F100,"ftp hookup setsockopt ok","",0);
  14345. #endif /* DEBUG */
  14346.     }
  14347. #endif /* SO_OOBINLINE */
  14348.  
  14349. #ifdef DEBUG
  14350.     debtim = xdebtim;
  14351. #endif /* DEBUG */
  14352.     return(ftp_host);
  14353.  
  14354.   bad:
  14355.     debug(F100,"ftp hookup bad","",0);
  14356. #ifdef TCPIPLIB
  14357.     socket_close(s);
  14358. #else /* TCPIPLIB */
  14359.     close(s);
  14360. #endif /* TCPIPLIB */
  14361. #ifdef DEBUG
  14362.     debtim = xdebtim;
  14363. #endif /* DEBUG */
  14364.     csocket = -1;
  14365.     return((char *)0);
  14366. }
  14367.  
  14368. static VOID
  14369. ftp_init() {
  14370.     int i, n;
  14371.  
  14372.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  14373.     /* clients do it.  In any case, failure of this command is not a */
  14374.     /* reliable indication that the server does not support Restart. */
  14375.  
  14376.     okrestart = 0;
  14377.     if (!noinit) {
  14378.         n = ftpcmd("REST 0",NULL,0,0,0);
  14379.         if (n == REPLY_COMPLETE)
  14380.           okrestart = 1;
  14381. #ifdef COMMENT
  14382.         else if (ftp_deb)
  14383.           printf("WARNING: Unable to restore file pointer.\n");
  14384. #endif /* COMMENT */
  14385.     }
  14386.     n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
  14387.     if (n == REPLY_COMPLETE) {
  14388.         register char *cp, c = NUL;
  14389.         cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  14390.         if (cp == NULL)
  14391.           cp = ckstrchr(ftp_reply_str+4,'\r');
  14392.         if (cp) {
  14393.             if (cp[-1] == '.')
  14394.               cp--;
  14395.             c = *cp;                    /* Save this char */
  14396.             *cp = '\0';                 /* Replace it with NUL */
  14397.         }
  14398.         if (!quiet)
  14399.           printf("Remote system type is %s.\n",ftp_reply_str+4);
  14400.         ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  14401.         if (cp)                         /* Put back saved char */
  14402.           *cp = c;
  14403.     }
  14404.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  14405.  
  14406.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  14407.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  14408.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  14409.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  14410.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  14411.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  14412.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  14413.  
  14414. #ifdef FTP_PROXY
  14415.     unix_proxy = 0;
  14416.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  14417. #endif /* FTP_PROXY */
  14418.  
  14419.     if (ftp_cmdlin && xfermode == XMODE_M)
  14420.       ftp_typ = binary;                 /* Type given on command line */
  14421.     else                                /* Otherwise set it automatically */
  14422.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  14423.     changetype(ftp_typ,0);              /* Change to this type */
  14424.     g_ftp_typ = ftp_typ;                /* Make it the global type */
  14425.     if (!quiet)
  14426.       printf("Default transfer mode is %s\n",
  14427.              ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  14428.              );
  14429.     for (i = 0; i < 16; i++)        /* Init server FEATure table */
  14430.       sfttab[i] = 0;
  14431.     if (!noinit) {
  14432.         n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
  14433. #ifdef COMMENT
  14434.         if (n != REPLY_COMPLETE)
  14435.           printf("WARNING: Server does not accept MODE S(TREAM)\n");
  14436. #endif /* COMMENT */
  14437.         n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
  14438. #ifdef COMMENT
  14439.         if (n != REPLY_COMPLETE)
  14440.           printf("WARNING: Server does not accept STRU F(ILE)\n");
  14441. #endif /* COMMENT */
  14442.     if (featok) {
  14443.         n = ftpcmd("FEAT",NULL,0,0,0); /* Ask server about features */
  14444.         if (n == REPLY_COMPLETE) {
  14445.         debug(F101,"ftp_init FEAT","",sfttab[0]);
  14446.         if (deblog || ftp_deb) {
  14447.             int i;
  14448.             for (i = 1; i < 16 && i < nfeattab; i++) {
  14449.             debug(F111,"ftp_init FEAT",feattab[i].kwd,sfttab[i]);
  14450.             if (ftp_deb)
  14451.               printf("  Server %s %s\n",
  14452.                  sfttab[i] ? "supports" : "does not support",
  14453.                  feattab[i].kwd
  14454.                  );
  14455.             }
  14456.             /* Deal with disabled MLST opts here if necessary */
  14457.             /* But why would it be? */
  14458.         }
  14459.         }
  14460.     }
  14461.     }
  14462. }
  14463.  
  14464. static int
  14465. ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
  14466.     static char ftppass[PASSBUFSIZ]="";
  14467.     char tmp[PASSBUFSIZ];
  14468.     char *user = NULL, *pass = NULL, *acct = NULL;
  14469.     int n, aflag = 0;
  14470.     extern char uidbuf[];
  14471.     extern char pwbuf[];
  14472.     extern int  pwflg, pwcrypt;
  14473.  
  14474.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  14475.  
  14476.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  14477.       anonymous = 1;
  14478.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  14479.       anonymous = 1;
  14480.  
  14481. #ifdef FTP_SRP
  14482.     if (auth_type && !strcmp(auth_type, "SRP")) {
  14483.         user = srp_user;
  14484.         pass = srp_pass;
  14485.         acct = srp_acct;
  14486.     } else
  14487. #endif /* FTP_SRP */
  14488.       if (anonymous) {
  14489.           user = "anonymous";
  14490.           if (ftp_tmp) {        /* They gave a password */
  14491.               pass = ftp_tmp;
  14492.           } else if (ftp_apw) {        /* SET FTP ANONYMOUS-PASSWORD */
  14493.           pass = ftp_apw;
  14494.       } else {            /* Supply user@host */
  14495.           ckmakmsg(tmp,PASSBUFSIZ,whoami(),"@",myhost,NULL);
  14496.           pass = tmp;
  14497.           }
  14498.       debug(F110,"ftp anonymous",pass,0);
  14499.       } else {
  14500. #ifdef USE_RUSERPASS
  14501.           if (ruserpass(host, &user, &pass, &acct) < 0) {
  14502.               ftpcode = -1;
  14503.               return(0);
  14504.           }
  14505. #endif /* USE_RUSERPASS */
  14506.           if (ftp_logname) {
  14507.               user = ftp_logname;
  14508.               pass = ftp_tmp;
  14509.           } else if (uidbuf[0] && (ftp_tmp || pwbuf[0] && pwflg)) {
  14510.               user = uidbuf;
  14511.               if (ftp_tmp) {
  14512.                   pass = ftp_tmp;
  14513.               } else if (pwbuf[0] && pwflg) {
  14514.                   ckstrncpy(ftppass,pwbuf,PASSBUFSIZ);
  14515. #ifdef OS2
  14516.                   if ( pwcrypt )
  14517.                       ck_encrypt((char *)ftppass);
  14518. #endif /* OS2 */
  14519.                   pass = ftppass;
  14520.               }
  14521.           }
  14522.           acct = ftp_acc;
  14523.           while (user == NULL) {
  14524.               char *myname, prompt[PROMPTSIZ];
  14525.               int ok;
  14526.  
  14527.               myname = whoami();
  14528.               if (myname)
  14529.                 ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  14530.                           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  14531.               else
  14532.                 ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  14533.               tmp[0] = '\0';
  14534.               
  14535.               ok = uq_txt(NULL,prompt,1,NULL,tmp,PASSBUFSIZ,NULL);
  14536.               if (!ok || *tmp == '\0')
  14537.                 user = myname;
  14538.               else
  14539.                 user = brstrip(tmp);
  14540.           }
  14541.       }
  14542.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14543.     if (n == REPLY_COMPLETE) {
  14544.         /* determine if we need to send a dummy password */
  14545.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  14546.           ftpcmd("PASS dummy",NULL,0,0,1);
  14547.     } else if (n == REPLY_CONTINUE) {
  14548. #ifdef CK_ENCRYPTION
  14549.         int oldftp_cpl;
  14550. #endif /* CK_ENCRYPTION */
  14551.  
  14552.         if (pass == NULL) {
  14553.             int ok;
  14554.             setint();
  14555.             ok = uq_txt(NULL," Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL);
  14556.             if (ok)
  14557.                 pass = brstrip(ftppass);
  14558.         }
  14559.  
  14560. #ifdef CK_ENCRYPTION
  14561.         oldftp_cpl = ftp_cpl;
  14562.         ftp_cpl = FPL_PRV;
  14563. #endif /* CK_ENCRYPTION */
  14564.         n = ftpcmd("PASS",pass,-1,-1,1);
  14565.         if (!anonymous && pass) {
  14566.             char * p = pass;
  14567.             while (*p++) *(p-1) = NUL;
  14568.             makestr(&ftp_tmp,NULL);
  14569.         }
  14570. #ifdef CK_ENCRYPTION
  14571.         /* level may have changed */
  14572.         if (ftp_cpl == FPL_PRV)
  14573.           ftp_cpl = oldftp_cpl;
  14574. #endif /* CK_ENCRYPTION */
  14575.     }
  14576.     if (n == REPLY_CONTINUE) {
  14577.         aflag++;
  14578.         if (acct == NULL) {
  14579.             static char ftpacct[80];
  14580.             int ok;
  14581.             setint();
  14582.             ok = uq_txt(NULL," Account: ",2,NULL,ftpacct,80,NULL);
  14583.             if (ok)
  14584.                 acct = brstrip(ftpacct);
  14585.         }
  14586.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14587.     }
  14588.     if (n != REPLY_COMPLETE) {
  14589.         fprintf(stderr, "FTP login failed.\n");
  14590.         if (haveurl)
  14591.           doexit(BAD_EXIT,-1);
  14592.         return(0);
  14593.     }
  14594.     if (!aflag && acct != NULL) {
  14595.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14596.     }
  14597.     makestr(&ftp_logname,user);
  14598.     loggedin = 1;
  14599. #ifdef LOCUS
  14600.     /* Unprefixed file management commands go to server */
  14601.     if (autolocus && !ftp_cmdlin) {
  14602.     setlocus(0,1);
  14603.     }
  14604. #endif /* LOCUS */
  14605.     ftp_init();
  14606.  
  14607.     if (anonymous && !quiet) {
  14608.         printf(" Logged in as anonymous (%s)\n",pass);
  14609.         memset(pass, 0, strlen(pass));
  14610.     }
  14611.     if (ftp_rdir) {
  14612.         if (doftpcwd(ftp_rdir,-1) < 1)
  14613.           doexit(BAD_EXIT,-1);
  14614.     }
  14615.  
  14616. #ifdef FTP_PROXY
  14617.     if (proxy)
  14618.       return(1);
  14619. #endif /* FTP_PROXY */
  14620.     return(1);
  14621. }
  14622.  
  14623. static int
  14624. ftp_reset() {
  14625.     int rc;
  14626. #ifdef BSDSELECT
  14627.     int nfnd = 1;
  14628.     fd_set mask;
  14629.     FD_ZERO(&mask);
  14630.     while (nfnd > 0) {
  14631.         FD_SET(csocket, &mask);
  14632.         if ((nfnd = empty(&mask,0)) < 0) {
  14633.             perror("reset");
  14634.             ftpcode = -1;
  14635.             lostpeer();
  14636.             return(0);
  14637.         } else if (nfnd) {
  14638.             getreply(0,-1,-1,ftp_vbm,0);
  14639.         }
  14640.     }
  14641. #else /* BSDSELECT */
  14642. #ifdef IBMSELECT
  14643.     int nfnd = 1;
  14644.     while (nfnd > 0) {
  14645.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  14646.             perror("reset");
  14647.             ftpcode = -1;
  14648.             lostpeer();
  14649.             return(0);
  14650.         } else if (nfnd) {
  14651.             getreply(0,-1,-1,ftp_vbm,0);
  14652.         }
  14653.     }
  14654. #endif /* IBMSELECT */
  14655. #endif /* BSDSELECT */
  14656.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  14657.     if (rc > 0)
  14658.       loggedin = 0;
  14659.     return(rc);
  14660. }
  14661.  
  14662. static int
  14663. ftp_rename(from, to) char * from, * to; {
  14664.     int lcs = -1, rcs = -1;
  14665. #ifndef NOCSETS
  14666.     if (ftp_xla) {
  14667.         lcs = ftp_csl;
  14668.         if (lcs < 0) lcs = fcharset;
  14669.         rcs = ftp_csx;
  14670.         if (rcs < 0) rcs = ftp_csr;
  14671.     }
  14672. #endif /* NOCSETS */
  14673.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  14674.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  14675.     }
  14676.     return(0);                          /* Failure */
  14677. }
  14678.  
  14679. static int
  14680. ftp_umask(mask) char * mask; {
  14681.     int rc;
  14682.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  14683.     return(rc);
  14684. }
  14685.  
  14686. static int
  14687. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  14688.     int n = 0, aflag = 0;
  14689.     char pwd[PWDSIZ];
  14690.  
  14691.     if (!auth_type && ftp_aut) {
  14692. #ifdef FTP_SRP
  14693.         if (ck_srp_is_installed()) {
  14694.             if (srp_ftp_auth( NULL, user, pass)) {
  14695.                 makestr(&pass,srp_pass);
  14696.             }
  14697.         }
  14698. #endif /* FTP_SRP */
  14699.     }
  14700.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14701.     if (n == REPLY_COMPLETE)
  14702.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  14703.     else if (n == REPLY_CONTINUE) {
  14704. #ifdef CK_ENCRYPTION
  14705.         int oldftp_cpl;
  14706. #endif /* CK_ENCRYPTION */
  14707.         if (pass == NULL || !pass[0]) {
  14708.             int ok;
  14709.             pwd[0] = '\0';
  14710.             setint();
  14711.             ok = uq_txt(NULL," Password: ",2,NULL,pwd,PWDSIZ,NULL);
  14712.             if (ok)
  14713.                 pass = brstrip(pwd);
  14714.         }
  14715.  
  14716. #ifdef CK_ENCRYPTION
  14717.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  14718.           ftp_cpl = PROT_P;
  14719. #endif /* CK_ENCRYPTION */
  14720.         n = ftpcmd("PASS",pass,-1,-1,1);
  14721.         memset(pass, 0, strlen(pass));
  14722. #ifdef CK_ENCRYPTION
  14723.         /* level may have changed */
  14724.         if (ftp_cpl == PROT_P)
  14725.           ftp_cpl = oldftp_cpl;
  14726. #endif /* CK_ENCRYPTION */
  14727.     }
  14728.     if (n == REPLY_CONTINUE) {
  14729.         if (acct == NULL || !acct[0]) {
  14730.             int ok;
  14731.             pwd[0] = '\0';
  14732.             setint();
  14733.             ok = uq_txt(NULL," Account: ",2,NULL,pwd,PWDSIZ,NULL);
  14734.             if (ok)
  14735.                 acct = pwd;
  14736.         }
  14737.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14738.         aflag++;
  14739.     }
  14740.     if (n != REPLY_COMPLETE) {
  14741.         printf("Login failed.\n");
  14742.         return(0);
  14743.     }
  14744.     if (!aflag && acct != NULL && acct[0]) {
  14745.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14746.     }
  14747.     if (n == REPLY_COMPLETE) {
  14748.         makestr(&ftp_logname,user);
  14749.         loggedin = 1;
  14750.         ftp_init();
  14751.         return(1);
  14752.     }
  14753.     return(0);
  14754. }
  14755.  
  14756. char *
  14757. ftp_authtype() {
  14758.     if (!connected)
  14759.       return("NULL");
  14760.     return(auth_type ? auth_type : "NULL");
  14761. }
  14762.  
  14763. char *
  14764. ftp_cpl_mode() {
  14765.     switch (ftp_cpl) {
  14766.       case FPL_CLR:
  14767.         return("clear");
  14768.       case FPL_SAF:
  14769.         return("safe");
  14770.       case FPL_PRV:
  14771.         return("private");
  14772.       case FPL_CON:
  14773.         return("confidential");
  14774.       default:
  14775.         return("(error)");
  14776.     }
  14777. }
  14778.  
  14779. char *
  14780. ftp_dpl_mode() {
  14781.     switch (ftp_dpl) {
  14782.       case FPL_CLR:
  14783.         return("clear");
  14784.       case FPL_SAF:
  14785.         return("safe");
  14786.       case FPL_PRV:
  14787.         return("private");
  14788.       case FPL_CON:
  14789.         return("confidential");
  14790.       default:
  14791.         return("(error)");
  14792.     }
  14793. }
  14794.  
  14795.  
  14796. /* remote_files() */
  14797. /*
  14798.    Returns next remote filename on success;
  14799.    NULL on error or no more files with global rfrc set to:
  14800.      -1: Bad argument
  14801.      -2: Server error response to NLST, e.g. file not found
  14802.      -3: No more files
  14803.      -9: Internal error
  14804. */
  14805. #define FTPNAMBUFLEN CKMAXPATH+1024
  14806.  
  14807. /* Check: ckmaxfiles CKMAXOPEN */
  14808.  
  14809. #define MLSDEPTH 128            /* Stack of open temp files */
  14810. static int mlsdepth = 0;        /* Temp file stack depth */
  14811. static FILE * tmpfilptr[MLSDEPTH+1] = { NULL, NULL }; /* Temp file pointers */
  14812. static char * tmpfilnam[MLSDEPTH+1] = { NULL, NULL }; /* Temp file names */
  14813.  
  14814. static VOID
  14815. mlsreset() {                /* Reset MGET temp-file stack */
  14816.     int i;
  14817.     for (i = 0; i <= mlsdepth; i++) {
  14818.     if (tmpfilptr[i]) {
  14819.         fclose(tmpfilptr[i]);
  14820.         tmpfilptr[i] = NULL;
  14821.         if (tmpfilnam[i]) {
  14822. #ifdef OS2
  14823.         unlink(tmpfilnam[i]);
  14824. #endif /* OS2 */
  14825.         free(tmpfilnam[i]);
  14826.         }
  14827.     }
  14828.     }
  14829.     mlsdepth = 0;
  14830. }
  14831.  
  14832. static CHAR *
  14833. #ifdef CK_ANSIC
  14834. remote_files(int new_query, CHAR * arg, CHAR * pattern, int proxy_switch)
  14835. #else /* CK_ANSIC */
  14836. remote_files(new_query, arg, pattern, proxy_switch)
  14837.     int new_query;
  14838.     CHAR * arg;                /* That we send to the server */
  14839.     CHAR * pattern;            /* That we use locally */
  14840.     int proxy_switch;
  14841. #endif /* CK_ANSIC */
  14842. /* remote_files */ {
  14843.     static CHAR buf[FTPNAMBUFLEN];
  14844.     CHAR *cp, *whicharg;
  14845.     char * cdto = NULL;
  14846.     char * p;
  14847.     int i, x, forced = 0;
  14848.     int lcs = 0, rcs = 0, xlate = 0;
  14849.  
  14850.     debug(F101,"ftp remote_files new_query","",new_query);
  14851.     debug(F110,"ftp remote_files arg",arg,0);
  14852.     debug(F110,"ftp remote_files pattern",pattern,0);
  14853.  
  14854.     rfrc = -1;
  14855.     if (pattern)            /* Treat empty pattern same as NULL */
  14856.       if (!*pattern)
  14857.     pattern = NULL;
  14858.     if (arg)                /* Ditto for arg */
  14859.       if (!*arg)
  14860.     arg = NULL;
  14861.  
  14862.   again:
  14863.  
  14864.     if (new_query) {
  14865.         if (tmpfilptr[mlsdepth]) {
  14866.             fclose(tmpfilptr[mlsdepth]);
  14867.             tmpfilptr[mlsdepth] = NULL;
  14868. #ifdef OS2
  14869.             if (!ftp_deb && !deblog)
  14870.               unlink(tmpfilnam[mlsdepth]);
  14871. #endif /* OS2 */
  14872.         }
  14873.     }
  14874.     if (tmpfilptr[mlsdepth] == NULL) {
  14875.         extern char * tempdir;
  14876.         char * p;
  14877.         debug(F110,"ftp remote_files tempdir",tempdir,0);
  14878.         if (tempdir) {
  14879.             p = tempdir;
  14880.         } else {
  14881. #ifdef OS2
  14882. #ifdef NT
  14883.             p = getenv("K95TMP");
  14884. #else
  14885.             p = getenv("K2TMP");
  14886. #endif /* NT */
  14887.             if (!p)
  14888. #endif /* OS2 */
  14889.               p = getenv("CK_TMP");
  14890.             if (!p)
  14891.               p = getenv("TMPDIR");
  14892.             if (!p) p = getenv("TEMP");
  14893.             if (!p) p = getenv("TMP");
  14894. #ifdef OS2ORUNIX
  14895.             if (p) {
  14896.                 int len = strlen(p);
  14897.                 if (p[len-1] != '/'
  14898. #ifdef OS2
  14899.                     && p[len-1] != '\\'
  14900. #endif /* OS2 */
  14901.                      ) {
  14902.                     static char foo[CKMAXPATH];
  14903.                     ckstrncpy(foo,p,CKMAXPATH);
  14904.                     ckstrncat(foo,"/",CKMAXPATH);
  14905.                     p = foo;
  14906.                 }
  14907.             } else
  14908. #else /* OS2ORUNIX */
  14909.             if (!p)
  14910. #endif /* OS2ORUNIX */
  14911. #ifdef UNIX                             /* Systems that have a standard */
  14912.                 p = "/tmp/";            /* temporary directory... */
  14913. #else
  14914. #ifdef datageneral
  14915.             p = ":TMP:";
  14916. #else
  14917.             p = "";
  14918. #endif /* datageneral */
  14919. #endif /* UNIX */
  14920.         }
  14921.         debug(F110,"ftp remote_files p",p,0);
  14922.  
  14923.     /* Get temp file */
  14924.  
  14925.     if ((tmpfilnam[mlsdepth] = (char *)malloc(CKMAXPATH+1))) {
  14926.         ckmakmsg((char *)tmpfilnam[mlsdepth],
  14927.              CKMAXPATH+1,p,"ckXXXXXX",NULL,NULL);
  14928.     } else {
  14929.         printf("?Malloc failure: remote_files()\n");
  14930.         return(NULL);
  14931.     }
  14932.  
  14933. #ifdef NT
  14934.     {
  14935.         char * tmpfil = mktemp((char *)tmpfilnam[mlsdepth]);
  14936.         if ( tmpfil )
  14937.         ckstrncpy(tmpfilnam[mlsdepth],tmpfil,CKMAXPATH+1);
  14938.     }
  14939. #else /* NT */
  14940. #ifdef MKTEMP
  14941. #ifdef MKSTEMP
  14942.     x = mkstemp((char *)tmpfilnam[mlsdepth]);
  14943.     if (x > -1) close(x);        /* We just want the name. */
  14944. #else
  14945.         mktemp((char *)tmpfilnam[mlsdepth]);
  14946. #endif /* MKSTEMP */
  14947.         /* if no mktmpnam() the name will just be "ckXXXXXX"... */
  14948. #endif /* MKTEMP */
  14949. #endif /* NT */
  14950.  
  14951.     debug(F111,"ftp remote_files tmpfilnam[mlsdepth]",
  14952.           tmpfilnam[mlsdepth],mlsdepth);
  14953.  
  14954. #ifdef FTP_PROXY
  14955.         if (proxy_switch) {
  14956.             pswitch(!proxy);
  14957.         }
  14958. #endif /* FTP_PROXY */
  14959.  
  14960.         debug(F101,"ftp remote_files ftp_xla","",ftp_xla);
  14961.         debug(F101,"ftp remote_files ftp_csl","",ftp_csl);
  14962.         debug(F101,"ftp remote_files ftp_csr","",ftp_csr);
  14963.  
  14964. #ifndef NOCSETS
  14965.         xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
  14966.         if (xlate) {                    /* ON? */
  14967.             lcs = ftp_csl;              /* Local charset */
  14968.             if (lcs < 0) lcs = fcharset;
  14969.             if (lcs < 0) xlate = 0;
  14970.         }
  14971.         if (xlate) {                    /* Still ON? */
  14972.             rcs = ftp_csx;              /* Remote (Server) charset */
  14973.             if (rcs < 0) rcs = ftp_csr;
  14974.             if (rcs < 0) xlate = 0;
  14975.         }
  14976. #endif /* NOCSETS */
  14977.  
  14978.     forced = mgetforced;        /* MGET method forced? */
  14979.     if (!forced || !mgetmethod)    /* Not forced... */
  14980.       mgetmethod = (sfttab[0] && sfttab[SFT_MLST]) ? /* so pick one */
  14981.           SND_MLS :
  14982.           SND_NLS; 
  14983. /*                                           
  14984.   User's Command:                 Result:
  14985.     mget /nlst                     NLST (NULL)
  14986.     mget /nlst foo                 NLST foo
  14987.     mget /nlst *.txt               NLST *.txt 
  14988.     mget /nlst /match:*.txt        NLST (NULL)
  14989.     mget /nlst /match:*.txt  foo   NLST foo   
  14990.     mget /mlsd                     MLSD (NULL)
  14991.     mget /mlsd foo                 MLSD foo
  14992.     mget /mlsd *.txt               MLSD (NULL)
  14993.     mget /mlsd /match:*.txt        MLSD (NULL)
  14994.     mget /mlsd /match:*.txt  foo   MLSD foo
  14995. */
  14996.     x = -1;
  14997.     while (x < 0) {
  14998.         if (pattern) {        /* Don't simplify this! */
  14999.         whicharg = arg;
  15000.         } else if (mgetmethod == SND_MLS) {
  15001.         if (arg)
  15002.           whicharg = iswild((char *)arg) ? NULL : arg;
  15003.         else
  15004.           whicharg = NULL;
  15005.         } else {
  15006.         whicharg = arg;
  15007.         }
  15008.         debug(F110,"ftp remote_files mgetmethod",
  15009.           mgetmethod == SND_MLS ? "MLSD" : "NLST", 0);
  15010.         debug(F110,"ftp remote_files whicharg",whicharg,0);
  15011.  
  15012.         x = recvrequest((mgetmethod == SND_MLS) ? "MLSD" : "NLST",
  15013.                 (char *)tmpfilnam[mlsdepth],
  15014.                 (char *)whicharg,
  15015.                 "wb",
  15016.                 0,
  15017.                 0,
  15018.                 NULL,
  15019.                 xlate,
  15020.                 lcs,
  15021.                 rcs
  15022.                 );
  15023.         if (x < 0) {        /* Chosen method wasn't accepted */
  15024.         if (forced) {
  15025.             if (ftpcode > 500 && ftpcode < 505 && !quiet)
  15026.               printf("?%s: Not supported by server\n",
  15027.                  mgetmethod == SND_MLS ? "MLSD" : "NLST"
  15028.                  );
  15029.             rfrc = -2;        /* Fail */
  15030.             return(NULL);
  15031.         }
  15032.         /* Not forced - if MLSD failed, try NLST */
  15033.         if (mgetmethod == SND_MLS) {  /* Server lied about MLST */
  15034.             sfttab[SFT_MLST] = 0;     /* So disable it */
  15035.             mlstok = 0;              /* and */
  15036.             mgetmethod = SND_NLS;     /* try NLST */
  15037.             continue;
  15038.         }
  15039.         rfrc = -2;
  15040.         return(NULL);
  15041.         }
  15042.     }
  15043. #ifdef FTP_PROXY
  15044.         if (proxy_switch) {
  15045.             pswitch(!proxy);
  15046.         }
  15047. #endif /* FTP_PROXY */
  15048.         tmpfilptr[mlsdepth] = fopen((char *)tmpfilnam[mlsdepth], "r");
  15049. #ifndef OS2
  15050.     if (tmpfilptr[mlsdepth]) {
  15051.         if (!ftp_deb && !deblog)
  15052.           unlink(tmpfilnam[mlsdepth]);
  15053.     }
  15054. #endif /* OS2 */
  15055.       notemp:
  15056.         if (!tmpfilptr[mlsdepth]) {
  15057.             debug(F110,"ftp remote_files open fail",tmpfilnam[mlsdepth],0);
  15058.             if ((!dpyactive || ftp_deb))
  15059.               printf("?Can't find list of remote files, oops\n");
  15060.             rfrc = -9;
  15061.             return(NULL);
  15062.         }
  15063.     if (ftp_deb)
  15064.       printf("LISTFILE: %s\n",tmpfilnam[mlsdepth]);
  15065.     }
  15066.     buf[0] = NUL;
  15067.     buf[FTPNAMBUFLEN-1] = NUL;
  15068.     buf[FTPNAMBUFLEN-2] = NUL;
  15069.  
  15070.     /* We have to redo all this because the first time was only for */
  15071.     /* for getting the file list, now it's for getting each file */
  15072.  
  15073.     if (arg && mgetmethod == SND_MLS) {    /* MLSD */
  15074.     if (!pattern && iswild((char *)arg)) {
  15075.         pattern = arg;        /* Wild arg is really a pattern */
  15076.         if (pattern)
  15077.           if (!*pattern)
  15078.         pattern = NULL;
  15079.         arg = NULL;            /* and not an arg */
  15080.     }
  15081.     if (new_query) {        /* Initial query? */
  15082.         cdto = (char *)arg;        /* (nonwild) arg given? */
  15083.         if (cdto)
  15084.           if (!*cdto)
  15085.         cdto = NULL;
  15086.         if (cdto)            /* If so, then CD to it */
  15087.           doftpcwd(cdto,0);
  15088.     }
  15089.     }
  15090.     new_query = 0;
  15091.  
  15092.     if (fgets((char *)buf, FTPNAMBUFLEN, tmpfilptr[mlsdepth]) == NULL) {
  15093.         fclose(tmpfilptr[mlsdepth]);
  15094.         tmpfilptr[mlsdepth] = NULL;
  15095.  
  15096. #ifdef OS2
  15097.         if (!ftp_deb && !deblog)
  15098.           unlink(tmpfilnam[mlsdepth]);
  15099. #endif /* OS2 */
  15100.         if (ftp_deb && !deblog) {
  15101.             printf("(Temporary file %s NOT deleted)\n",
  15102.            (char *)tmpfilnam[mlsdepth]);
  15103.         }
  15104.     if (mlsdepth <= 0) {        /* EOF at depth 0 */
  15105.         rfrc = -3;            /* means we're done */
  15106.         return(NULL);
  15107.     }
  15108.     printf("POPPING(%d)...\n",mlsdepth-1); 
  15109.     if (tmpfilnam[mlsdepth]) free(tmpfilnam[mlsdepth]);
  15110.     mlsdepth--;
  15111.     doftpcdup();
  15112.     zchdir("..");            /* <-- Not portable */
  15113.     goto again;
  15114.     }
  15115.     if (buf[FTPNAMBUFLEN-1]) {
  15116.     printf("?BUFFER OVERFLOW -- FTP NLST or MLSD string longer than %d\n",
  15117.            FTPNAMBUFLEN
  15118.            );
  15119.     debug(F101,"remote_files buffer overrun","",FTPNAMBUFLEN);
  15120.     return(NULL);
  15121.     }
  15122.     /* debug(F110,"ftp remote_files buf 1",buf,0); */
  15123.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  15124.       *cp = '\0';
  15125.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  15126.       *cp = '\0';
  15127.     debug(F110,"ftp remote_files buf",buf,0);
  15128.     rfrc = 0;
  15129.  
  15130.     if (ftp_deb)
  15131.       printf("[%s]\n",(char *)buf);
  15132.  
  15133.     havesize = -1L;            /* Initialize file facts... */
  15134.     havetype = -0;
  15135.     makestr(&havemdtm,NULL);
  15136.     p = (char *)buf;
  15137.  
  15138.     if (mgetmethod == SND_NLS) {    /* NLST... */
  15139.     if (pattern) {
  15140.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15141.           goto again;
  15142.     }
  15143.     } else {                /* MLSD... */
  15144.     p = parsefacts((char *)buf);
  15145.     switch (havetype) {
  15146.       case FTYP_FILE:        /* File: Get it if it matches */
  15147.         if (pattern) {
  15148.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15149.           goto again;
  15150.         }
  15151.         break;
  15152.       case FTYP_CDIR:        /* Current directory */
  15153.       case FTYP_PDIR:        /* Parent directory */
  15154.         goto again;            /* Skip */
  15155.       case FTYP_DIR:        /* (Sub)Directory */
  15156.         if (!recursive)        /* If not /RECURSIVE */
  15157.           goto again;        /* Skip */
  15158.         if (mlsdepth < MLSDEPTH) {
  15159.         char * p2 = NULL;
  15160.         mlsdepth++;
  15161.         printf("RECURSING [%s](%d)...\n",p,mlsdepth); 
  15162.         if (doftpcwd(p,0) > 0) {
  15163.             int x;
  15164.             if (!ckstrchr(p,'/')) {
  15165.             /* zmkdir() needs dirsep */
  15166.             if ((p2 = (char *)malloc((int)strlen(p) + 2))) {
  15167.                 strcpy(p2,p);    /* SAFE */
  15168.                 strcat(p2,"/");    /* SAFE */
  15169.                 p = p2;
  15170.             }
  15171.             }
  15172. #ifdef NOMKDIR
  15173.             x = -1;
  15174. #else
  15175.             x = zmkdir(p);
  15176. #endif /* NOMKDIR */
  15177.             if (x > -1) {
  15178.             zchdir(p);
  15179.             p = (char *)remote_files(1,arg,pattern,0);
  15180.             if (p2) free(p2);
  15181.             } else {
  15182.             printf("?mkdir failed: [%s] Depth=%d\n",
  15183.                    p,
  15184.                    mlsdepth
  15185.                    );
  15186.             mlsreset();
  15187.             if (p2) free(p2);
  15188.             return(NULL);
  15189.             }
  15190.         } else {
  15191.             printf("?CWD failed: [%s] Depth=%d\n",p,mlsdepth);
  15192.             mlsreset();
  15193.             return(NULL);
  15194.         }
  15195.         } else {
  15196.         printf("MAX DIRECTORY STACK DEPTH EXCEEDED: %d\n",
  15197.                mlsdepth
  15198.                );
  15199.         mlsreset();
  15200.         return(NULL);
  15201.         }
  15202.     }
  15203.     }
  15204.  
  15205. #ifdef DEBUG
  15206.     if (deblog) {
  15207.     debug(F101,"remote_files havesize","",havesize);
  15208.     debug(F101,"remote_files havetype","",havetype);
  15209.     debug(F110,"remote_files havemdtm",havemdtm,0);    
  15210.     debug(F110,"remote_files name",p,0);    
  15211.     }
  15212. #endif /* DEBUG */
  15213.     return((CHAR *)p);
  15214. }
  15215.  
  15216. /* N O T  P O R T A B L E !!! */
  15217.  
  15218. #if (SIZEOF_SHORT == 4)
  15219. typedef unsigned short ftp_uint32;
  15220. typedef short ftp_int32;
  15221. #else
  15222. #if (SIZEOF_INT == 4)
  15223. typedef unsigned int ftp_uint32;
  15224. typedef int ftp_int32;
  15225. #else
  15226. #if (SIZEOF_LONG == 4)
  15227. typedef ULONG ftp_uint32;
  15228. typedef long ftp_int32;
  15229. #endif
  15230. #endif
  15231. #endif
  15232.  
  15233. /* Perhaps use these in general, certainly use them for GSSAPI */
  15234.  
  15235. #ifndef looping_write
  15236. #define ftp_int32 int
  15237. #define ftp_uint32 unsigned int
  15238. static int
  15239. looping_write(fd, buf, len)
  15240.     int fd;
  15241.     register CONST char *buf;
  15242.     int len;
  15243. {
  15244.     int cc;
  15245.     register int wrlen = len;
  15246.     do {
  15247.         cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  15248.         if (cc < 0) {
  15249.             if (errno == EINTR)
  15250.               continue;
  15251.             return(cc);
  15252.         } else {
  15253.             buf += cc;
  15254.             wrlen -= cc;
  15255.         }
  15256.     } while (wrlen > 0);
  15257.     return(len);
  15258. }
  15259. #endif
  15260. #ifndef looping_read
  15261. static int
  15262. looping_read(fd, buf, len)
  15263.     int fd;
  15264.     register char *buf;
  15265.     register int len;
  15266. {
  15267.     int cc, len2 = 0;
  15268.  
  15269.     do {
  15270.         cc = recv(fd, (char *)buf, len,0);
  15271.         if (cc < 0) {
  15272.             if (errno == EINTR)
  15273.               continue;
  15274.             return(cc);                 /* errno is already set */
  15275.         } else if (cc == 0) {
  15276.             return(len2);
  15277.         } else {
  15278.             buf += cc;
  15279.             len2 += cc;
  15280.             len -= cc;
  15281.         }
  15282.     } while (len > 0);
  15283.     return(len2);
  15284. }
  15285. #endif /* looping_read */
  15286.  
  15287. #define ERR -2
  15288.  
  15289. #ifdef COMMENT
  15290. static
  15291. secure_putbyte(fd, c) int fd; CHAR c; {
  15292.     int ret;
  15293.  
  15294.     ucbuf[nout++] = c;
  15295.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  15296.         nout = 0;
  15297.         if (ftp_dpl == FPL_CLR)
  15298.           ret = send(fd, (SENDARG2TYPE)ucbuf,
  15299.                      (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  15300.         else
  15301.           ret = secure_putbuf(fd,
  15302.                               ucbuf,
  15303.                               (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  15304.                               );
  15305.         return(ret?ret:c);
  15306.     }
  15307.     return(c);
  15308. }
  15309. #endif /* COMMENT */
  15310.  
  15311. /* returns:
  15312.  *       0  on success
  15313.  *      -1  on error (errno set)
  15314.  *      -2  on security error
  15315.  */
  15316. static int
  15317. secure_flush(fd) int fd; {
  15318.     int rc = 0;
  15319.     int len = 0;
  15320.  
  15321.     if (nout > 0) {
  15322.         len = nout;
  15323.         if (ftp_dpl == FPL_CLR) {
  15324.             rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  15325.             nout = 0;
  15326.             goto xflush;
  15327.         } else {
  15328.             rc = secure_putbuf(fd, ucbuf, nout);
  15329.             if (rc)
  15330.               goto xflush;
  15331.         }
  15332.     }
  15333.     rc = (ftp_dpl == FPL_CLR) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  15334.  
  15335.   xflush:
  15336.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  15337.     spackets++;
  15338.         spktl = len;
  15339.         ftscreen(SCR_PT,'D',spackets,NULL);
  15340.     }
  15341.     return(rc);
  15342. }
  15343.  
  15344. #ifdef COMMENT                          /* (not used) */
  15345. /* returns:
  15346.  *      c>=0  on success
  15347.  *      -1    on error
  15348.  *      -2    on security error
  15349.  */
  15350. static int
  15351. #ifdef CK_ANSIC
  15352. secure_putc(char c, int fd)
  15353. #else
  15354. secure_putc(c, fd) char c; int fd;
  15355. #endif /* CK_ANSIC */
  15356. /* secure_putc */ {
  15357.     return(secure_putbyte(fd, (CHAR) c));
  15358. }
  15359. #endif /* COMMENT */
  15360.  
  15361. /* returns:
  15362.  *      nbyte on success
  15363.  *      -1  on error (errno set)
  15364.  *      -2  on security error
  15365.  */
  15366. static int
  15367. #ifdef CK_ANSIC
  15368. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  15369. #else
  15370. secure_write(fd, buf, nbyte)
  15371.     int fd;
  15372.     CHAR * buf;
  15373.     unsigned int nbyte;
  15374. #endif /* CK_ANSIC */
  15375. {
  15376.     int ret;
  15377.  
  15378.     if (ftp_dpl == FPL_CLR) {
  15379.         if (nout > 0) {
  15380.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  15381.               return(ret);
  15382.             nout = 0;
  15383.         }
  15384.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  15385.     } else {
  15386.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  15387.         int bsent = 0;
  15388.  
  15389.         while (bsent < nbyte) {
  15390.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  15391.                         (ucbuflen - nout) : (nbyte - bsent));
  15392. #ifdef DEBUG
  15393.         if (deblog) {
  15394.         debug(F101,"secure_write ucbuflen","",ucbuflen);
  15395.         debug(F101,"secure_write ucbufsiz","",ucbufsiz);
  15396.         debug(F101,"secure_write bsent","",bsent);
  15397.         debug(F101,"secure_write b2cp","",b2cp);
  15398.         }
  15399. #endif /* DEBUG */
  15400.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  15401.             nout += b2cp;
  15402.             bsent += b2cp;
  15403.  
  15404.             if (nout == ucbuflen) {
  15405.                 nout = 0;
  15406.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  15407.                 if (ret < 0)
  15408.                   return(ret);
  15409.             }
  15410.         }
  15411.         return(bsent);
  15412.     }
  15413. }
  15414.  
  15415. /* returns:
  15416.  *       0  on success
  15417.  *      -1  on error (errno set)
  15418.  *      -2  on security error
  15419.  */
  15420. static int
  15421. #ifdef CK_ANSIC
  15422. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  15423. #else
  15424. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  15425. #endif /* CK_ANSIC */
  15426. {
  15427.     static char *outbuf = NULL;         /* output ciphertext */
  15428. #ifdef FTP_SECURITY
  15429.     static unsigned int bufsize = 0;    /* size of outbuf */
  15430. #endif /* FTP_SECURITY */
  15431.     ftp_int32 length   = 0;
  15432.     ftp_uint32 net_len = 0;
  15433.  
  15434.     /* Other auth types go here ... */
  15435. #ifdef CK_SSL
  15436.     if (ssl_ftp_data_active_flag) {
  15437.         int count, error;
  15438.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  15439.         error = SSL_get_error(ssl_ftp_data_con,count);
  15440.         switch (error) {
  15441.           case SSL_ERROR_NONE:
  15442.             return(0);
  15443.           case SSL_ERROR_WANT_WRITE:
  15444.           case SSL_ERROR_WANT_READ:
  15445.           case SSL_ERROR_SYSCALL:
  15446. #ifdef NT
  15447.             {
  15448.                 int gle = GetLastError();
  15449.             }
  15450. #endif /* NT */
  15451.           case SSL_ERROR_WANT_X509_LOOKUP:
  15452.           case SSL_ERROR_SSL:
  15453.           case SSL_ERROR_ZERO_RETURN:
  15454.           default:
  15455.             SSL_shutdown(ssl_ftp_data_con);
  15456.             SSL_free(ssl_ftp_data_con);
  15457.             ssl_ftp_data_active_flag = 0;
  15458.             ssl_ftp_data_con = NULL;
  15459. #ifdef TCPIPLIB
  15460.             socket_close(data);
  15461. #else /* TCPIPLIB */
  15462. #ifdef USE_SHUTDOWN
  15463.             shutdown(data, 1+1);
  15464. #endif /* USE_SHUTDOWN */
  15465.             close(data);
  15466. #endif /* TCPIPLIB */
  15467.             data = -1;
  15468.             globaldin = data;
  15469.             return(-1);
  15470.         }
  15471.         return(-1);
  15472.     }
  15473. #endif /* CK_SSL */
  15474.  
  15475. #ifdef FTP_SRP
  15476.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  15477.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15478.             if (outbuf?
  15479.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15480.                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15481.                 bufsize = nbyte + FUDGE_FACTOR;
  15482.             } else {
  15483.                 bufsize = 0;
  15484.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15485.                 return(ERR);
  15486.             }
  15487.         }
  15488.         if ((length =
  15489.              srp_encode(ftp_dpl == FPL_PRV,
  15490.                         (CHAR *) buf,
  15491.                         (CHAR *) outbuf,
  15492.                         nbyte
  15493.                         )
  15494.              ) < 0) {
  15495.             secure_error ("srp_encode failed");
  15496.             return ERR;
  15497.         }
  15498.     }
  15499. #endif /* FTP_SRP */
  15500. #ifdef FTP_KRB4
  15501.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  15502.         struct sockaddr_in myaddr, hisaddr;
  15503.         GSOCKNAME_T len;
  15504.         len = sizeof(myaddr);
  15505.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15506.             secure_error("secure_putbuf: getsockname failed");
  15507.             return(ERR);
  15508.         }
  15509.         len = sizeof(hisaddr);
  15510.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15511.             secure_error("secure_putbuf: getpeername failed");
  15512.             return(ERR);
  15513.         }
  15514.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15515.             if (outbuf ?
  15516.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15517.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15518.                 bufsize = nbyte + FUDGE_FACTOR;
  15519.             } else {
  15520.                 bufsize = 0;
  15521.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15522.                 return(ERR);
  15523.             }
  15524.         }
  15525.         if (ftp_dpl == FPL_PRV) {
  15526.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  15527.                                  ftp_sched,
  15528. #ifdef KRB524
  15529.                                  ftp_cred.session,
  15530. #else /* KRB524 */
  15531.                                  &ftp_cred.session,
  15532. #endif /* KRB524 */
  15533.                                  &myaddr,
  15534.                                  &hisaddr
  15535.                                  );
  15536.         } else {
  15537.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  15538. #ifdef KRB524
  15539.                                  ftp_cred.session,
  15540. #else /* KRB524 */
  15541.                                  &ftp_cred.session,
  15542. #endif /* KRB524 */
  15543.                                  &myaddr,
  15544.                                  &hisaddr
  15545.                                  );
  15546.         }
  15547.         if (length == -1) {
  15548.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  15549.                          ftp_dpl == FPL_PRV ? "priv" : "safe");
  15550.             return(ERR);
  15551.         }
  15552.     }
  15553. #endif /* FTP_KRB4 */
  15554. #ifdef FTP_GSSAPI
  15555.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  15556.         gss_buffer_desc in_buf, out_buf;
  15557.         OM_uint32 maj_stat, min_stat;
  15558.         int conf_state;
  15559.  
  15560.         in_buf.value = buf;
  15561.         in_buf.length = nbyte;
  15562.         maj_stat = gss_seal(&min_stat, gcontext,
  15563.                             (ftp_dpl == FPL_PRV), /* confidential */
  15564.                             GSS_C_QOP_DEFAULT,
  15565.                             &in_buf,
  15566.                             &conf_state,
  15567.                             &out_buf
  15568.                             );
  15569.         if (maj_stat != GSS_S_COMPLETE) {
  15570.             /* generally need to deal */
  15571.             /* ie. should loop, but for now just fail */
  15572.             user_gss_error(maj_stat, min_stat,
  15573.                            ftp_dpl == FPL_PRV?
  15574.                            "GSSAPI seal failed":
  15575.                            "GSSAPI sign failed");
  15576.             return(ERR);
  15577.         }
  15578.         if (bufsize < out_buf.length) {
  15579.             if (outbuf ?
  15580.                 (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  15581.                 (outbuf = malloc((unsigned) out_buf.length))) {
  15582.                 bufsize = out_buf.length;
  15583.             } else {
  15584.                 bufsize = 0;
  15585.                 secure_error("%s (in malloc of PROT buffer)",
  15586.                              ck_errstr());
  15587.                 return(ERR);
  15588.             }
  15589.         }
  15590.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  15591.         gss_release_buffer(&min_stat, &out_buf);
  15592.     }
  15593. #endif /* FTP_GSSAPI */
  15594.     net_len = htonl((ULONG) length);
  15595.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  15596.       return(-1);
  15597.     if (looping_write(fd, outbuf, length) != length)
  15598.       return(-1);
  15599.     return(0);
  15600. }
  15601.  
  15602. /* fc = 0 means to get a byte; nonzero means to initialize buffer pointers */
  15603.  
  15604. static int
  15605. secure_getbyte(fd,fc) int fd,fc; {
  15606.     /* number of chars in ucbuf, pointer into ucbuf */
  15607.     static unsigned int nin = 0, bufp = 0;
  15608.     int kerror;
  15609.     ftp_uint32 length;
  15610.  
  15611.     if (fc) {
  15612.     nin = bufp = 0;
  15613.     ucbuf[0] = NUL;
  15614.     return(0);
  15615.     }
  15616.     if (nin == 0) {
  15617.         if (iscanceled())
  15618.           return(-9);
  15619. #ifdef CK_SSL
  15620.         if (ssl_ftp_data_active_flag) {
  15621.             int count, error;
  15622.             count = SSL_read(ssl_ftp_data_con, ucbuf, ucbufsiz);
  15623.             error = SSL_get_error(ssl_ftp_data_con,count);
  15624.             switch (error) {
  15625.               case SSL_ERROR_NONE:
  15626.                 nin = bufp = count;
  15627.                 rpackets++;
  15628.                 pktnum++;
  15629.                 if (fdispla != XYFD_B) {
  15630.                     rpktl = count;
  15631.                     ftscreen(SCR_PT,'D',rpackets,NULL);
  15632.                 }
  15633.                 break;
  15634.               case SSL_ERROR_WANT_WRITE:
  15635.               case SSL_ERROR_WANT_READ:
  15636.               case SSL_ERROR_SYSCALL:
  15637. #ifdef NT
  15638.                 {
  15639.                     int gle = GetLastError();
  15640.                 }
  15641. #endif /* NT */
  15642.               case SSL_ERROR_WANT_X509_LOOKUP:
  15643.               case SSL_ERROR_SSL:
  15644.               case SSL_ERROR_ZERO_RETURN:
  15645.               default:
  15646.                 nin = bufp = count = 0;
  15647.                 SSL_shutdown(ssl_ftp_data_con);
  15648.                 SSL_free(ssl_ftp_data_con);
  15649.                 ssl_ftp_data_active_flag = 0;
  15650.                 ssl_ftp_data_con = NULL;
  15651. #ifdef TCPIPLIB
  15652.                 socket_close(data);
  15653. #else /* TCPIPLIB */
  15654. #ifdef USE_SHUTDOWN
  15655.                 shutdown(data, 1+1);
  15656. #endif /* USE_SHUTDOWN */
  15657.                 close(data);
  15658. #endif /* TCPIPLIB */
  15659.                 data = -1;
  15660.                 globaldin = data;
  15661.                 break;
  15662.             }
  15663.         } else
  15664. #endif /* CK_SSL */
  15665.           {
  15666.               kerror = looping_read(fd, (char *)&length, sizeof(length));
  15667.               if (kerror != sizeof(length)) {
  15668.                   secure_error("Couldn't read PROT buffer length: %d/%s",
  15669.                                kerror,
  15670.                                kerror == -1 ? ck_errstr()
  15671.                                : "premature EOF"
  15672.                                );
  15673.                   return(ERR);
  15674.               }
  15675.               debug(F101,"secure_getbyte length","",length);
  15676.               debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  15677.  
  15678.               length = (ULONG) ntohl(length);
  15679.               if (length > maxbuf) {
  15680.                   secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  15681.                                length,
  15682.                                maxbuf
  15683.                                );
  15684.                   return(ERR);
  15685.               }
  15686.               if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  15687.                   secure_error("Couldn't read %u byte PROT buffer: %s",
  15688.                                length,
  15689.                                kerror == -1 ? ck_errstr() : "premature EOF"
  15690.                                );
  15691.                   return(ERR);
  15692.               }
  15693.  
  15694.               /* Other auth types go here ... */
  15695. #ifdef FTP_SRP
  15696.               if (strcmp(auth_type, "SRP") == 0) {
  15697.                   if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  15698.                                                 (CHAR *) ucbuf,
  15699.                                                 ucbuf,
  15700.                                                 length
  15701.                                                 )
  15702.                        ) == -1) {
  15703.                       secure_error ("srp_encode failed" );
  15704.                       return ERR;
  15705.                   }
  15706.               }
  15707. #endif /* FTP_SRP */
  15708. #ifdef FTP_KRB4
  15709.               if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  15710.                   struct sockaddr_in myaddr, hisaddr;
  15711.                   GSOCKNAME_T len;
  15712.                   len = sizeof(myaddr);
  15713.                   if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15714.                       secure_error("secure_putbuf: getsockname failed");
  15715.                       return(ERR);
  15716.                   }
  15717.                   len = sizeof(hisaddr);
  15718.                   if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15719.                       secure_error("secure_putbuf: getpeername failed");
  15720.                       return(ERR);
  15721.                   }
  15722.                   if (ftp_dpl) {
  15723.                       kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  15724. #ifdef KRB524
  15725.                                            ftp_cred.session,
  15726. #else /* KRB524 */
  15727.                                            &ftp_cred.session,
  15728. #endif /* KRB524 */
  15729.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15730.                   } else {
  15731.                       kerror = krb_rd_safe(ucbuf, length,
  15732. #ifdef KRB524
  15733.                                            ftp_cred.session,
  15734. #else /* KRB524 */
  15735.                                            &ftp_cred.session,
  15736. #endif /* KRB524 */
  15737.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15738.                   }
  15739.                   if (kerror) {
  15740.                       secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  15741.                                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  15742.                                    krb_get_err_text(kerror));
  15743.                       return(ERR);
  15744.                   }
  15745.                   memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  15746.                   nin = bufp = ftp_msg_data.app_length;
  15747.               }
  15748. #endif /* FTP_KRB4 */
  15749. #ifdef FTP_GSSAPI
  15750.               if (strcmp(auth_type, "GSSAPI") == 0) {
  15751.                   gss_buffer_desc xmit_buf, msg_buf;
  15752.                   OM_uint32 maj_stat, min_stat;
  15753.                   int conf_state;
  15754.  
  15755.                   xmit_buf.value = ucbuf;
  15756.                   xmit_buf.length = length;
  15757.                   conf_state = (ftp_dpl == FPL_PRV);
  15758.                   /* decrypt/verify the message */
  15759.                   maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  15760.                                         &msg_buf, &conf_state, NULL);
  15761.                   if (maj_stat != GSS_S_COMPLETE) {
  15762.                       user_gss_error(maj_stat, min_stat,
  15763.                                      (ftp_dpl == FPL_PRV)?
  15764.                                      "failed unsealing ENC message":
  15765.                                      "failed unsealing MIC message");
  15766.                       return ERR;
  15767.                   }
  15768.                   memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  15769.                   gss_release_buffer(&min_stat, &msg_buf);
  15770.               }
  15771. #endif /* FTP_GSSAPI */
  15772.               /* Other auth types go here ... */
  15773.  
  15774.               /* Update file transfer display */
  15775.               rpackets++;
  15776.               pktnum++;
  15777.               if (fdispla != XYFD_B) {
  15778.                   rpktl = nin;
  15779.                   ftscreen(SCR_PT,'D',rpackets,NULL);
  15780.               }
  15781.           }
  15782.     }
  15783.     if (nin == 0)
  15784.       return(EOF);
  15785.     else
  15786.       return(ucbuf[bufp - nin--]);
  15787. }
  15788.  
  15789. /* secure_getc(fd,fc)
  15790.  * Call with:
  15791.  *   fd = file descriptor for connection.
  15792.  *   fc = 0 to get a character, fc != 0 to initialize buffer pointers.
  15793.  * Returns:
  15794.  *   c>=0 on success (character value)
  15795.  *   -1   on EOF
  15796.  *   -2   on security error
  15797.  */
  15798. static int
  15799. secure_getc(fd,fc) int fd,fc; {        /* file descriptor, function code */
  15800.     if (ftp_dpl == FPL_CLR && !ssl_ftp_proxy) {
  15801.         static unsigned int nin = 0, bufp = 0;
  15802.     if (fc) {
  15803.         nin = bufp = 0;
  15804.         ucbuf[0] = NUL;
  15805.         return(0);
  15806.     }
  15807.         if (nin == 0) {
  15808.             if (iscanceled())
  15809.               return(-9);
  15810.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  15811.             if (nin <= 0) {
  15812.                 debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  15813.                 debug(F101,"secure_getc returns EOF","",EOF);
  15814.                 nin = bufp = 0;
  15815.                 return(EOF);
  15816.             }
  15817.             debug(F101,"ftp secure_getc recv","",nin);
  15818.             hexdump("ftp secure_getc recv",ucbuf,16);
  15819.             rpackets++;
  15820.             pktnum++;
  15821.             if (fdispla != XYFD_B) {
  15822.                 rpktl = nin;
  15823.                 ftscreen(SCR_PT,'D',rpackets,NULL);
  15824.             }
  15825.         }
  15826.         return(ucbuf[bufp - nin--]);
  15827.     } else
  15828.       return(secure_getbyte(fd,fc));
  15829. }
  15830.  
  15831. /* returns:
  15832.  *     n>0  on success (n == # of bytes read)
  15833.  *       0  on EOF
  15834.  *      -1  on error (errno set), only for FPL_CLR
  15835.  *      -2  on security error
  15836.  */
  15837. static int
  15838. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  15839.     static int c = 0;
  15840.     int i;
  15841.  
  15842.     debug(F101,"secure_read bytes requested","",nbyte);
  15843.     if (c == EOF)
  15844.       return(c = 0);
  15845.     for (i = 0; nbyte > 0; nbyte--) {
  15846.         c = secure_getc(fd,0);
  15847.         switch (c) {
  15848.           case -9:                      /* Canceled from keyboard */
  15849.             debug(F101,"ftp secure_read interrupted","",c);
  15850.             return(0);
  15851.           case ERR:
  15852.             debug(F101,"ftp secure_read error","",c);
  15853.             return(c);
  15854.           case EOF:
  15855.             debug(F101,"ftp secure_read EOF","",c);
  15856.             if (!i)
  15857.               c = 0;
  15858.             return(i);
  15859.           default:
  15860.             buf[i++] = c;
  15861.         }
  15862.     }
  15863.     return(i);
  15864. }
  15865.  
  15866. #ifdef USE_RUSERPASS
  15867. /* BEGIN_RUSERPASS
  15868.  *
  15869.  * Copyright (c) 1985 Regents of the University of California.
  15870.  * All rights reserved.
  15871.  *
  15872.  * Redistribution and use in source and binary forms, with or without
  15873.  * modification, are permitted provided that the following conditions
  15874.  * are met:
  15875.  * 1. Redistributions of source code must retain the above copyright
  15876.  *    notice, this list of conditions and the following disclaimer.
  15877.  * 2. Redistributions in binary form must reproduce the above copyright
  15878.  *    notice, this list of conditions and the following disclaimer in the
  15879.  *    documentation and/or other materials provided with the distribution.
  15880.  * 3. All advertising materials mentioning features or use of this software
  15881.  *    must display the following acknowledgement:
  15882.  *      This product includes software developed by the University of
  15883.  *      California, Berkeley and its contributors.
  15884.  * 4. Neither the name of the University nor the names of its contributors
  15885.  *    may be used to endorse or promote products derived from this software
  15886.  *    without specific prior written permission.
  15887.  *
  15888.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  15889.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15890.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15891.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  15892.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15893.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  15894.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  15895.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  15896.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  15897.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  15898.  * SUCH DAMAGE.
  15899.  */
  15900.  
  15901. #ifndef lint
  15902. static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
  15903. #endif /* not lint */
  15904.  
  15905. #include <sys/types.h>
  15906. #include <stdio.h>
  15907. #include <string.h>
  15908. #ifdef HAVE_STDLIB_H
  15909. #include <stdlib.h>
  15910. #endif
  15911. #include <ctype.h>
  15912. #include <sys/stat.h>
  15913. #include <errno.h>
  15914.  
  15915. #ifndef MAXHOSTNAMELEN
  15916. #define MAXHOSTNAMELEN 64
  15917. #endif
  15918.  
  15919. char * renvlook();
  15920. static FILE * cfile;
  15921.  
  15922. #define DEFAULT 1
  15923. #define LOGIN   2
  15924. #define PASSWD  3
  15925. #define ACCOUNT 4
  15926. #define MACDEF  5
  15927. #define ID      10
  15928. #define MACH    11
  15929.  
  15930. static char tokval[100];
  15931.  
  15932. static struct toktab {
  15933.     char *tokstr;
  15934.     int tval;
  15935. } toktab[]= {
  15936.     "default",  DEFAULT,
  15937.     "login",    LOGIN,
  15938.     "password", PASSWD,
  15939.     "passwd",   PASSWD,
  15940.     "account",  ACCOUNT,
  15941.     "machine",  MACH,
  15942.     "macdef",   MACDEF,
  15943.     0,          0
  15944. };
  15945.  
  15946. static int
  15947. token() {
  15948.     char *cp;
  15949.     int c;
  15950.     struct toktab *t;
  15951.  
  15952.     if (feof(cfile))
  15953.       return(0);
  15954.     while ((c = getc(cfile)) != EOF &&
  15955.            (c == '\n' || c == '\t' || c == ' ' || c == ','))
  15956.       continue;
  15957.     if (c == EOF)
  15958.       return(0);
  15959.     cp = tokval;
  15960.     if (c == '"') {
  15961.         while ((c = getc(cfile)) != EOF && c != '"') {
  15962.             if (c == '\\')
  15963.               c = getc(cfile);
  15964.             *cp++ = c;
  15965.         }
  15966.     } else {
  15967.         *cp++ = c;
  15968.         while ((c = getc(cfile)) != EOF
  15969.                && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  15970.             if (c == '\\')
  15971.               c = getc(cfile);
  15972.             *cp++ = c;
  15973.         }
  15974.     }
  15975.     *cp = 0;
  15976.     if (tokval[0] == 0)
  15977.       return(0);
  15978.     for (t = toktab; t->tokstr; t++)
  15979.       if (!strcmp(t->tokstr, tokval))
  15980.         return(t->tval);
  15981.     return(ID);
  15982. }
  15983.  
  15984. ruserpass(host, aname, apass, aacct)
  15985.     char *host, **aname, **apass, **aacct;
  15986. {
  15987.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  15988.     char myname[MAXHOSTNAMELEN], *mydomain;
  15989.     int t, i, c, usedefault = 0;
  15990. #ifdef NT
  15991.     struct _stat stb;
  15992. #else /* NT */
  15993.     struct stat stb;
  15994. #endif /* NT */
  15995.  
  15996.     hdir = getenv("HOME");
  15997.     if (hdir == NULL)
  15998.         hdir = ".";
  15999.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  16000.     cfile = fopen(buf, "r");
  16001.     if (cfile == NULL) {
  16002.         if (errno != ENOENT)
  16003.           perror(buf);
  16004.         return(0);
  16005.     }
  16006.     if (gethostname(myname, MAXHOSTNAMELEN) < 0)
  16007.       myname[0] = '\0';
  16008.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  16009.       mydomain = "";
  16010.  
  16011.   next:
  16012.     while ((t = token())) switch(t) {
  16013.  
  16014.       case DEFAULT:
  16015.         usedefault = 1;
  16016.         /* FALL THROUGH */
  16017.  
  16018.       case MACH:
  16019.         if (!usedefault) {
  16020.             if (token() != ID)
  16021.               continue;
  16022.             /*
  16023.              * Allow match either for user's input host name
  16024.              * or official hostname.  Also allow match of
  16025.              * incompletely-specified host in local domain.
  16026.              */
  16027.             if (ckstrcmp(host, tokval,-1,1) == 0)
  16028.               goto match;
  16029.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  16030.               goto match;
  16031.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  16032.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16033.                 ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  16034.                 tokval[tmp - ftp_host] == '\0')
  16035.               goto match;
  16036.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  16037.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16038.                 ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  16039.                 tokval[tmp - host] == '\0')
  16040.               goto match;
  16041.             continue;
  16042.         }
  16043.  
  16044.       match:
  16045.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  16046.  
  16047.           case LOGIN:
  16048.             if (token())
  16049.               if (*aname == 0) {
  16050.                   *aname = malloc((unsigned) strlen(tokval) + 1);
  16051.                   strcpy(*aname, tokval);      /* safe */
  16052.               } else {
  16053.                   if (strcmp(*aname, tokval))
  16054.                     goto next;
  16055.               }
  16056.             break;
  16057.           case PASSWD:
  16058.             if (strcmp(*aname, "anonymous") &&
  16059.                 fstat(fileno(cfile), &stb) >= 0 &&
  16060.                 (stb.st_mode & 077) != 0) {
  16061.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16062.                 fprintf(stderr, "Remove password or correct mode.\n");
  16063.                 goto bad;
  16064.             }
  16065.             if (token() && *apass == 0) {
  16066.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  16067.                 strcpy(*apass, tokval);          /* safe */
  16068.             }
  16069.             break;
  16070.           case ACCOUNT:
  16071.             if (fstat(fileno(cfile), &stb) >= 0
  16072.                 && (stb.st_mode & 077) != 0) {
  16073.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16074.                 fprintf(stderr, "Remove account or correct mode.\n");
  16075.                 goto bad;
  16076.             }
  16077.             if (token() && *aacct == 0) {
  16078.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  16079.                 strcpy(*aacct, tokval);          /* safe */
  16080.             }
  16081.             break;
  16082.  
  16083.           default:
  16084.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  16085.             break;
  16086.         }
  16087.         goto done;
  16088.     }
  16089.  
  16090.   done:
  16091.     fclose(cfile);
  16092.     return(0);
  16093.  
  16094.   bad:
  16095.     fclose(cfile);
  16096.     return(-1);
  16097. }
  16098. #endif /* USE_RUSERPASS */
  16099.  
  16100. static char *radixN =
  16101.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  16102.  
  16103. static char pad = '=';
  16104.  
  16105. static int
  16106. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  16107.     CHAR inbuf[], outbuf[];
  16108.     int inlen, *outlen, decode;
  16109. {
  16110.     int i, j, D = 0;
  16111.     char *p;
  16112.     CHAR c = NUL;
  16113.  
  16114.     if (decode) {
  16115.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  16116.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  16117.               return(1);
  16118.             D = p - radixN;
  16119.             switch (i&3) {
  16120.               case 0:
  16121.                 outbuf[j] = D<<2;
  16122.                 break;
  16123.               case 1:
  16124.                 outbuf[j++] |= D>>4;
  16125.                 outbuf[j] = (D&15)<<4;
  16126.                 break;
  16127.               case 2:
  16128.                 outbuf[j++] |= D>>2;
  16129.                 outbuf[j] = (D&3)<<6;
  16130.                 break;
  16131.               case 3:
  16132.                 outbuf[j++] |= D;
  16133.             }
  16134.             if (j == *outlen)
  16135.               return(4);
  16136.         }
  16137.         switch (i&3) {
  16138.           case 1: return(3);
  16139.           case 2: if (D&15) return(3);
  16140.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  16141.             break;
  16142.           case 3: if (D&3) return(3);
  16143.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  16144.         }
  16145.         *outlen = j;
  16146.     } else {
  16147.         for (i = 0, j = 0; i < inlen; i++) {
  16148.             switch (i%3) {
  16149.               case 0:
  16150.                 outbuf[j++] = radixN[inbuf[i]>>2];
  16151.                 c = (inbuf[i]&3)<<4;
  16152.                 break;
  16153.               case 1:
  16154.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  16155.                 c = (inbuf[i]&15)<<2;
  16156.                 break;
  16157.               case 2:
  16158.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  16159.                 outbuf[j++] = radixN[inbuf[i]&63];
  16160.                 c = 0;
  16161.             }
  16162.             if (j == *outlen)
  16163.               return(4);
  16164.         }
  16165.         if (i%3) outbuf[j++] = radixN[c];
  16166.         switch (i%3) {
  16167.           case 1: outbuf[j++] = pad;
  16168.           case 2: outbuf[j++] = pad;
  16169.         }
  16170.         outbuf[*outlen = j] = '\0';
  16171.     }
  16172.     return(0);
  16173. }
  16174.  
  16175. static char *
  16176. radix_error(e) int e;
  16177. {
  16178.     switch (e) {
  16179.       case 0:  return("Success");
  16180.       case 1:  return("Bad character in encoding");
  16181.       case 2:  return("Encoding not properly padded");
  16182.       case 3:  return("Decoded # of bits not a multiple of 8");
  16183.       case 4:  return("Output buffer too small");
  16184.       default: return("Unknown error");
  16185.     }
  16186. }
  16187. /* END_RUSERPASS */
  16188.  
  16189. #ifdef FTP_SRP
  16190. /*---------------------------------------------------------------------------+
  16191.  |                                                                           |
  16192.  |   Package: srpftp                                                         |
  16193.  |   Author: Eugene Jhong                                                    |
  16194.  |                                                                           |
  16195.  +---------------------------------------------------------------------------*/
  16196.  
  16197. /*
  16198.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  16199.  * All Rights Reserved.
  16200.  *
  16201.  * Permission is hereby granted, free of charge, to any person obtaining
  16202.  * a copy of this software and associated documentation files (the
  16203.  * "Software"), to deal in the Software without restriction, including
  16204.  * without limitation the rights to use, copy, modify, merge, publish,
  16205.  * distribute, sublicense, and/or sell copies of the Software, and to
  16206.  * permit persons to whom the Software is furnished to do so, subject to
  16207.  * the following conditions:
  16208.  *
  16209.  * The above copyright notice and this permission notice shall be
  16210.  * included in all copies or substantial portions of the Software.
  16211.  *
  16212.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  16213.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16214.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16215.  *
  16216.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  16217.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16218.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  16219.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  16220.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16221.  *
  16222.  * In addition, the following conditions apply:
  16223.  *
  16224.  * 1. Any software that incorporates the SRP authentication technology
  16225.  *    must display the following acknowlegment:
  16226.  *    "This product uses the 'Secure Remote Password' cryptographic
  16227.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  16228.  *
  16229.  * 2. Any software that incorporates all or part of the SRP distribution
  16230.  *    itself must also display the following acknowledgment:
  16231.  *    "This product includes software developed by Tom Wu and Eugene
  16232.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  16233.  *
  16234.  * 3. Redistributions in source or binary form must retain an intact copy
  16235.  *    of this copyright notice and list of conditions.
  16236.  */
  16237.  
  16238. #define SRP_PROT_VERSION        1
  16239.  
  16240. #ifdef CK_ENCRYPTION
  16241. #define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
  16242. #else
  16243. #define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
  16244. #endif /* CK_ENCRYPTION */
  16245.  
  16246. #define SRP_DEFAULT_HASH        HASH_ID_SHA
  16247.  
  16248. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  16249. CHAR srp_pref_hash = HASH_ID_SHA;
  16250.  
  16251. static struct t_client *tc = NULL;
  16252. static CHAR *skey = NULL;
  16253. static krypto_context *incrypt = NULL;
  16254. static krypto_context *outcrypt = NULL;
  16255.  
  16256. typedef unsigned int srp_uint32;
  16257.  
  16258. /*--------------------------------------------------------------+
  16259.  | srp_selcipher: select cipher                                 |
  16260.  +--------------------------------------------------------------*/
  16261. static int
  16262. srp_selcipher (cname) char *cname; {
  16263.     cipher_desc *cd;
  16264.  
  16265.     if (!(cd = cipher_getdescbyname (cname))) {
  16266.         int i;
  16267.         CHAR *list = cipher_getlist ();
  16268.  
  16269.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  16270.         for (i = 0; i < strlen (list); i++)
  16271.           fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  16272.         fprintf (stderr, "\n");
  16273.         return -1;
  16274.     }
  16275.     srp_pref_cipher = cd->id;
  16276.     return 0;
  16277. }
  16278.  
  16279. /*--------------------------------------------------------------+
  16280.  | srp_selhash: select hash                                     |
  16281.  +--------------------------------------------------------------*/
  16282. static int
  16283. srp_selhash (hname) char *hname; {
  16284.     hash_desc *hd;
  16285.  
  16286.     if (!(hd = hash_getdescbyname (hname))) {
  16287.         int i;
  16288.         CHAR *list = hash_getlist ();
  16289.  
  16290.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  16291.         for (i = 0; i < strlen (list); i++)
  16292.           fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  16293.         fprintf (stderr, "\n");
  16294.         return -1;
  16295.     }
  16296.     srp_pref_hash = hd->id;
  16297.     return 0;
  16298. }
  16299.  
  16300. /*--------------------------------------------------------------+
  16301.  | srp_userpass: get username and password                      |
  16302.  +--------------------------------------------------------------*/
  16303. static int
  16304. srp_userpass (host) char *host; {
  16305.     char tmp[BUFSIZ], prompt[PROMPTSIZ];
  16306.     char *user;
  16307.  
  16308.     user = NULL;
  16309. #ifdef USE_RUSERPASS
  16310.     ruserpass (host, &user, &srp_pass, &srp_acct);
  16311. #endif /* USE_RUSERPASS */
  16312.  
  16313.     while (user == NULL)     {
  16314.         char *myname;
  16315.         int ok;
  16316.  
  16317.         myname = whoami();
  16318.         if (!myname) myname = "";
  16319.         if (myname[0])
  16320.           ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  16321.                     NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  16322.         else
  16323.           ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  16324.         tmp[0] = '\0';
  16325.         ok = uq_txt(NULL,prompt,1,NULL,tmp,BUFSIZ,NULL);
  16326.         if (!ok || *tmp == '\0')
  16327.           user = myname;
  16328.         else
  16329.           user = brstrip(tmp);
  16330.     }
  16331.     ckstrncpy (srp_user, user,BUFSIZ);
  16332.     return(0);
  16333. }
  16334.  
  16335. /*--------------------------------------------------------------+
  16336.  | srp_reset: reset srp information                             |
  16337.  +--------------------------------------------------------------*/
  16338. static int
  16339. srp_reset () {
  16340.     if (tc) { t_clientclose (tc); tc = NULL; }
  16341.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  16342.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  16343.     return(0);
  16344. }
  16345.  
  16346. /*--------------------------------------------------------------+
  16347.  | srp_ftp_auth: perform srp authentication                         |
  16348.  +--------------------------------------------------------------*/
  16349. static int
  16350. srp_ftp_auth(host, user, pass)
  16351.     char *host;
  16352.     char *user;
  16353.     char *pass;
  16354. {
  16355.     struct t_num *wp;
  16356.     struct t_num N;
  16357.     struct t_num g;
  16358.     struct t_num s;
  16359.     struct t_num yp;
  16360.     CHAR buf[FTP_BUFSIZ];
  16361.     CHAR tmp[FTP_BUFSIZ];
  16362.     CHAR *bp, *cp;
  16363.     int n, e, clen, blen, len, i;
  16364.     CHAR cid = 0;
  16365.     CHAR hid = 0;
  16366.  
  16367.     srp_pass = srp_acct = 0;
  16368.  
  16369.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  16370.     if (n != REPLY_CONTINUE) {
  16371.         if (ftp_deb)
  16372.             fprintf(stderr, "SRP rejected as an authentication type\n");
  16373.         return(0);
  16374.     } else {                            /* Send protocol version */
  16375.         CHAR vers[4];
  16376.         memset (vers, 0, 4);
  16377.         vers[3] = SRP_PROT_VERSION;
  16378.         if (!quiet)
  16379.           printf ("SRP accepted as authentication type.\n");
  16380.         bp = tmp; blen = 0;
  16381.         srp_put (vers, &bp, 4, &blen);
  16382.         len = FTP_BUFSIZ;
  16383.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16384.           goto encode_error;
  16385.         reply_parse = "ADAT=";
  16386.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16387.     }
  16388.     if (n == REPLY_CONTINUE) {          /* Get protocol version */
  16389.         bp = buf;
  16390.         if (!reply_parse)
  16391.           goto data_error;
  16392.         blen = FTP_BUFSIZ;
  16393.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  16394.           goto decode_error;
  16395.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16396.           goto data_error;
  16397.  
  16398.         if (host) {                     /* Get username/password if needed */
  16399.             srp_userpass (host);
  16400.         } else {
  16401.             ckstrncpy (srp_user, user, BUFSIZ);
  16402.             srp_pass = pass;
  16403.         }
  16404.         bp = tmp; blen = 0;             /* Send username */
  16405.         srp_put (srp_user, &bp, strlen (srp_user), &blen);
  16406.         len = FTP_BUFSIZ;
  16407.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16408.           goto encode_error;
  16409.         reply_parse = "ADAT=";
  16410.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16411.     }
  16412.     if (n == REPLY_CONTINUE) {          /* Get N, g and s */
  16413.         bp = buf;
  16414.         if (!reply_parse)
  16415.           goto data_error;
  16416.         blen = FTP_BUFSIZ;
  16417.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16418.           goto decode_error;
  16419.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  16420.           goto data_error;
  16421.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  16422.           goto data_error;
  16423.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  16424.           goto data_error;
  16425.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  16426.             fprintf (stderr, "Unable to open SRP client structure.\n");
  16427.             goto bad;
  16428.         }
  16429.         wp = t_clientgenexp (tc);       /* Send wp */
  16430.         bp = tmp; blen = 0;
  16431.         srp_put (wp->data, &bp, wp->len, &blen);
  16432.         len = FTP_BUFSIZ;
  16433.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16434.           goto encode_error;
  16435.         reply_parse = "ADAT=";
  16436.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16437.     }
  16438.     if (n == REPLY_CONTINUE) {          /* Get yp */
  16439.         bp = buf;
  16440.         if (!reply_parse)
  16441.           goto data_error;
  16442.         blen = FTP_BUFSIZ;
  16443.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16444.           goto decode_error;
  16445.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  16446.           goto data_error;
  16447.         if (!srp_pass) {
  16448.             static char ftppass[PASSBUFSIZ];
  16449.             int ok;
  16450.             setint();
  16451.             ok = uq_txt(NULL," SRP Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL);
  16452.             if (ok)
  16453.           srp_pass = brstrip(ftppass);
  16454.         }
  16455.         t_clientpasswd (tc, srp_pass);
  16456.         memset (srp_pass, 0, strlen (srp_pass));
  16457.         skey = t_clientgetkey (tc, &yp); /* Send response */
  16458.         bp = tmp; blen = 0;
  16459.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  16460.         len = FTP_BUFSIZ;
  16461.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16462.           goto encode_error;
  16463.         reply_parse = "ADAT=";
  16464.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16465.     }
  16466.     if (n == REPLY_CONTINUE) {          /* Get response */
  16467.         bp = buf;
  16468.         if (!reply_parse)
  16469.           goto data_error;
  16470.         blen = FTP_BUFSIZ;
  16471.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16472.           goto encode_error;
  16473.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  16474.           goto data_error;
  16475.         if (t_clientverify (tc, cp)) {
  16476.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  16477.             goto bad;
  16478.         }
  16479.         bp = tmp; blen = 0;             /* Send nothing */
  16480.         srp_put ("\0", &bp, 1, &blen);
  16481.         len = FTP_BUFSIZ;
  16482.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16483.           goto encode_error;
  16484.         reply_parse = "ADAT=";
  16485.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16486.     }
  16487.     if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
  16488.         CHAR seqnum[4];
  16489.         CHAR *clist;
  16490.         CHAR *hlist;
  16491.         CHAR *p1;
  16492.         int clist_len, hlist_len;
  16493.         bp = buf;
  16494.         if (!reply_parse)
  16495.           goto data_error;
  16496.         blen = FTP_BUFSIZ;
  16497.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16498.           goto encode_error;
  16499.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  16500.           goto data_error;
  16501.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  16502.           goto data_error;
  16503.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16504.           goto data_error;
  16505.         memcpy (seqnum, cp, 4);
  16506.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  16507.           cid = srp_pref_cipher;
  16508.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  16509.           cid = SRP_DEFAULT_CIPHER;
  16510.         if (!cid) {
  16511.             CHAR *loclist = cipher_getlist ();
  16512.             for (i = 0; i < strlen (loclist); i++)
  16513.               if (cipher_supported (clist, loclist[i])) {
  16514.                   cid = loclist[i];
  16515.                   break;
  16516.               }
  16517.         }
  16518.         if (!cid) {
  16519.             fprintf (stderr, "Unable to agree on cipher.\n");
  16520.             goto bad;
  16521.         }
  16522.         /* Choose hash */
  16523.  
  16524.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  16525.           hid = srp_pref_hash;
  16526.  
  16527.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  16528.           hid = SRP_DEFAULT_HASH;
  16529.  
  16530.         if (!hid) {
  16531.             CHAR *loclist = hash_getlist ();
  16532.             for (i = 0; i < strlen (loclist); i++)
  16533.               if (hash_supported (hlist, loclist[i])) {
  16534.                   hid = loclist[i];
  16535.                   break;
  16536.               }
  16537.         }
  16538.         if (!hid) {
  16539.             fprintf (stderr, "Unable to agree on hash.\n");
  16540.             goto bad;
  16541.         }
  16542.         /* Set incrypt */
  16543.  
  16544.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  16545.                                     KRYPTO_DECODE)))
  16546.           goto bad;
  16547.  
  16548.         /* Generate random number for outkey and outseqnum */
  16549.  
  16550.         t_random (seqnum, 4);
  16551.  
  16552.         /* Send cid, hid, outkey, outseqnum */
  16553.  
  16554.         bp = tmp; blen = 0;
  16555.         srp_put (&cid, &bp, 1, &blen);
  16556.         srp_put (&hid, &bp, 1, &blen);
  16557.         srp_put (seqnum, &bp, 4, &blen);
  16558.         len = FTP_BUFSIZ;
  16559.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16560.           goto encode_error;
  16561.         reply_parse = "ADAT=";
  16562.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16563.  
  16564.         /* Set outcrypt */
  16565.  
  16566.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  16567.                                      KRYPTO_ENCODE)))
  16568.           goto bad;
  16569.  
  16570.         t_clientclose (tc);
  16571.         tc = NULL;
  16572.     }
  16573.     if (n != REPLY_COMPLETE)
  16574.       goto bad;
  16575.  
  16576.     if (ftp_vbm) {
  16577.         if (ftp_deb)
  16578.           printf("\n");
  16579.         printf ("SRP authentication succeeded.\n");
  16580.         printf ("Using cipher %s and hash function %s.\n",
  16581.                 (cipher_getdescbyid(cid))->name,
  16582.                 (hash_getdescbyid(hid))->name
  16583.                 );
  16584.     }
  16585.     reply_parse = NULL;
  16586.     auth_type = "SRP";
  16587.     return(1);
  16588.  
  16589.   encode_error:
  16590.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  16591.     goto bad;
  16592.  
  16593.   decode_error:
  16594.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  16595.     goto bad;
  16596.  
  16597.   data_error:
  16598.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  16599.     goto bad;
  16600.  
  16601.   bad:
  16602.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  16603.     reply_parse = NULL;
  16604.     return(0);
  16605. }
  16606.  
  16607. /*--------------------------------------------------------------+
  16608.  | srp_put: put item to send buffer                             |
  16609.  +--------------------------------------------------------------*/
  16610. static int
  16611. srp_put (in, out, inlen, outlen)
  16612.     CHAR *in;
  16613.     CHAR **out;
  16614.     int inlen;
  16615.     int *outlen;
  16616. {
  16617.     srp_uint32 net_len;
  16618.  
  16619.     net_len = htonl (inlen);
  16620.     memcpy (*out, &net_len, 4);
  16621.  
  16622.     *out += 4; *outlen += 4;
  16623.  
  16624.     memcpy (*out, in, inlen);
  16625.  
  16626.     *out += inlen; *outlen += inlen;
  16627.     return(0);
  16628. }
  16629.  
  16630. /*--------------------------------------------------------------+
  16631.  | srp_get: get item from receive buffer                        |
  16632.  +--------------------------------------------------------------*/
  16633. static int
  16634. srp_get (in, out, inlen, outlen)
  16635.     CHAR **in;
  16636.     CHAR **out;
  16637.     int *inlen;
  16638.     int *outlen;
  16639. {
  16640.     srp_uint32 net_len;
  16641.  
  16642.     if (*inlen < 4) return -1;
  16643.  
  16644.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  16645.     *outlen = ntohl (net_len);
  16646.  
  16647.     if (*inlen < *outlen) return -1;
  16648.  
  16649.     *out = *in; *inlen -= *outlen; *in += *outlen;
  16650.  
  16651.     return *outlen;
  16652. }
  16653.  
  16654. /*--------------------------------------------------------------+
  16655.  | srp_encode: encode control message                           |
  16656.  +--------------------------------------------------------------*/
  16657. static int
  16658. srp_encode (private, in, out, len)
  16659.     int private;
  16660.     CHAR *in;
  16661.     CHAR *out;
  16662.     unsigned len;
  16663. {
  16664.     if (private)
  16665.       return krypto_msg_priv (outcrypt, in, out, len);
  16666.     else
  16667.       return krypto_msg_safe (outcrypt, in, out, len);
  16668. }
  16669.  
  16670. /*--------------------------------------------------------------+
  16671.  | srp_decode: decode control message                           |
  16672.  +--------------------------------------------------------------*/
  16673. static int
  16674. srp_decode (private, in, out, len)
  16675.     int private;
  16676.     CHAR *in;
  16677.     CHAR *out;
  16678.     unsigned len;
  16679. {
  16680.     if (private)
  16681.       return krypto_msg_priv (incrypt, in, out, len);
  16682.     else
  16683.       return krypto_msg_safe (incrypt, in, out, len);
  16684. }
  16685.  
  16686. #endif /* FTP_SRP */
  16687.  
  16688.  
  16689.  
  16690. #ifdef NOT_USED
  16691. /*
  16692.   The following code is from the Unix FTP client.  Be sure to
  16693.   make sure that the functionality is not lost.  Especially
  16694.   the Proxy stuff even though we have not yet implemented it.
  16695. */
  16696.  
  16697. /* Send multiple files  */
  16698.  
  16699. static int
  16700. ftp_mput(argc, argv) int argc; char **argv; {
  16701.     register int i;
  16702.     sig_t oldintr;
  16703.     int ointer;
  16704.     char *tp;
  16705.     sigtype mcancel();
  16706.  
  16707.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  16708.         printf("usage: %s local-files\n", argv[0]);
  16709.         ftpcode = -1;
  16710.         return;
  16711.     }
  16712.     mname = argv[0];
  16713.     mflag = 1;
  16714.     oldintr = signal(SIGINT, mcancel);
  16715.  
  16716.     /* Replace with calls to cc_execute() */
  16717.     setjmp(jcancel);
  16718. #ifdef FTP_PROXY
  16719.     if (proxy) {
  16720.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  16721.  
  16722.         while ((cp = remglob(argv,0)) != NULL) {
  16723.             if (*cp == 0) {
  16724.                 mflag = 0;
  16725.                 continue;
  16726.             }
  16727.             if (mflag && confirm(argv[0], cp)) {
  16728.                 tp = cp;
  16729.                 if (mcase) {
  16730.                     while (*tp && !islower(*tp)) {
  16731.                         tp++;
  16732.                     }
  16733.                     if (!*tp) {
  16734.                         tp = cp;
  16735.                         tp2 = tmpbuf;
  16736.                         while ((*tp2 = *tp) != 0) {
  16737.                             if (isupper(*tp2)) {
  16738.                                 *tp2 = 'a' + *tp2 - 'A';
  16739.                             }
  16740.                             tp++;
  16741.                             tp2++;
  16742.                         }
  16743.                     }
  16744.                     tp = tmpbuf;
  16745.                 }
  16746.                 if (ntflag) {
  16747.                     tp = dotrans(tp);
  16748.                 }
  16749.                 if (mapflag) {
  16750.                     tp = domap(tp);
  16751.                 }
  16752.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  16753.                 if (!mflag && fromatty) {
  16754.                     ointer = interactive;
  16755.                     interactive = 1;
  16756.                     if (confirm("Continue with","mput")) {
  16757.                         mflag++;
  16758.                     }
  16759.                     interactive = ointer;
  16760.                 }
  16761.             }
  16762.         }
  16763.         signal(SIGINT, oldintr);
  16764.         mflag = 0;
  16765.         return;
  16766.     }
  16767. #endif /* FTP_PROXY */
  16768.     for (i = 1; i < argc; i++) {
  16769.         register char **cpp, **gargs;
  16770.  
  16771.         if (mflag && confirm(argv[0], argv[i])) {
  16772.             tp = argv[i];
  16773.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  16774.             if (!mflag && fromatty) {
  16775.                 ointer = interactive;
  16776.                 interactive = 1;
  16777.                 if (confirm("Continue with","mput")) {
  16778.                     mflag++;
  16779.                 }
  16780.                 interactive = ointer;
  16781.             }
  16782.         }
  16783.         continue;
  16784.  
  16785.         gargs = ftpglob(argv[i]);
  16786.         if (globerr != NULL) {
  16787.             printf("%s\n", globerr);
  16788.             if (gargs) {
  16789.                 blkfree(gargs);
  16790.                 free((char *)gargs);
  16791.             }
  16792.             continue;
  16793.         }
  16794.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  16795.             if (mflag && confirm(argv[0], *cpp)) {
  16796.                 tp = *cpp;
  16797.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  16798.                 if (!mflag && fromatty) {
  16799.                     ointer = interactive;
  16800.                     interactive = 1;
  16801.                     if (confirm("Continue with","mput")) {
  16802.                         mflag++;
  16803.                     }
  16804.                     interactive = ointer;
  16805.                 }
  16806.             }
  16807.         }
  16808.         if (gargs != NULL) {
  16809.             blkfree(gargs);
  16810.             free((char *)gargs);
  16811.         }
  16812.     }
  16813.     signal(SIGINT, oldintr);
  16814.     mflag = 0;
  16815. }
  16816.  
  16817. /* Get multiple files */
  16818.  
  16819. static int
  16820. ftp_mget(argc, argv) int argc; char **argv; {
  16821.     int rc = -1;
  16822.     sig_t oldintr;
  16823.     int ointer;
  16824.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  16825.     sigtype mcancel();
  16826.  
  16827.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  16828.         printf("usage: %s remote-files\n", argv[0]);
  16829.         ftpcode = -1;
  16830.         return(-1);
  16831.     }
  16832.     mname = argv[0];
  16833.     mflag = 1;
  16834.     oldintr = signal(SIGINT,mcancel);
  16835.     /* Replace with calls to cc_execute() */
  16836.     setjmp(jcancel);
  16837.     while ((cp = remglob(argv,proxy)) != NULL) {
  16838.         if (*cp == '\0') {
  16839.             mflag = 0;
  16840.             continue;
  16841.         }
  16842.         if (mflag && confirm(argv[0], cp)) {
  16843.             tp = cp;
  16844.             if (mcase) {
  16845.                 while (*tp && !islower(*tp)) {
  16846.                     tp++;
  16847.                 }
  16848.                 if (!*tp) {
  16849.                     tp = cp;
  16850.                     tp2 = tmpbuf;
  16851.                     while ((*tp2 = *tp) != 0) {
  16852.                         if (isupper(*tp2)) {
  16853.                             *tp2 = 'a' + *tp2 - 'A';
  16854.                         }
  16855.                         tp++;
  16856.                         tp2++;
  16857.                     }
  16858.                 }
  16859.                 tp = tmpbuf;
  16860.             }
  16861.             rc = (recvrequest("RETR", tp, cp, "wb",
  16862.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  16863.             if (!mflag && fromatty) {
  16864.                 ointer = interactive;
  16865.                 interactive = 1;
  16866.                 if (confirm("Continue with","mget")) {
  16867.                     mflag++;
  16868.                 }
  16869.                 interactive = ointer;
  16870.             }
  16871.         }
  16872.     }
  16873.     signal(SIGINT,oldintr);
  16874.     mflag = 0;
  16875.     return(rc);
  16876. }
  16877.  
  16878. /* Delete multiple files */
  16879.  
  16880. static int
  16881. mdelete(argc, argv) int argc; char **argv; {
  16882.     sig_t oldintr;
  16883.     int ointer;
  16884.     char *cp;
  16885.     sigtype mcancel();
  16886.  
  16887.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  16888.         printf("usage: %s remote-files\n", argv[0]);
  16889.         ftpcode = -1;
  16890.         return(-1);
  16891.     }
  16892.     mname = argv[0];
  16893.     mflag = 1;
  16894.     oldintr = signal(SIGINT, mcancel);
  16895.     /* Replace with calls to cc_execute() */
  16896.     setjmp(jcancel);
  16897.     while ((cp = remglob(argv,0)) != NULL) {
  16898.         if (*cp == '\0') {
  16899.             mflag = 0;
  16900.             continue;
  16901.         }
  16902.         if (mflag && confirm(argv[0], cp)) {
  16903.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  16904.             if (!mflag && fromatty) {
  16905.                 ointer = interactive;
  16906.                 interactive = 1;
  16907.                 if (confirm("Continue with", "mdelete")) {
  16908.                     mflag++;
  16909.                 }
  16910.                 interactive = ointer;
  16911.             }
  16912.         }
  16913.     }
  16914.     signal(SIGINT, oldintr);
  16915.     mflag = 0;
  16916.     return(rc);
  16917. }
  16918.  
  16919. /* Get a directory listing of multiple remote files */
  16920.  
  16921. static int
  16922. mls(argc, argv) int argc; char **argv; {
  16923.     sig_t oldintr;
  16924.     int ointer, i;
  16925.     char *cmd, mode[1], *dest;
  16926.     sigtype mcancel();
  16927.     int rc = -1;
  16928.  
  16929.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  16930.       goto usage;
  16931.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  16932.       usage:
  16933.         printf("usage: %s remote-files local-file\n", argv[0]);
  16934.         ftpcode = -1;
  16935.         return(-1);
  16936.     }
  16937.     dest = argv[argc - 1];
  16938.     argv[argc - 1] = NULL;
  16939.     if (strcmp(dest, "-") && *dest != '|')
  16940.       if (!globulize(&dest) ||
  16941.           !confirm("output to local-file:", dest)) {
  16942.           ftpcode = -1;
  16943.           return(-1);
  16944.       }
  16945.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  16946.     mname = argv[0];
  16947.     mflag = 1;
  16948.     oldintr = signal(SIGINT, mcancel);
  16949.     /* Replace with calls to cc_execute() */
  16950.     setjmp(jcancel);
  16951.     for (i = 1; mflag && i < argc-1; ++i) {
  16952.         *mode = (i == 1) ? 'w' : 'a';
  16953.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  16954.         if (!mflag && fromatty) {
  16955.             ointer = interactive;
  16956.             interactive = 1;
  16957.             if (confirm("Continue with", argv[0])) {
  16958.                 mflag ++;
  16959.             }
  16960.             interactive = ointer;
  16961.         }
  16962.     }
  16963.     signal(SIGINT, oldintr);
  16964.     mflag = 0;
  16965.     return(rc);
  16966. }
  16967.  
  16968. static char *
  16969. remglob(argv,doswitch) char *argv[]; int doswitch; {
  16970.     char temp[16];
  16971.     static char buf[CKMAXPATH];
  16972.     static FILE *ftemp = NULL;
  16973.     static char **args;
  16974.     int oldhash;
  16975.     char *cp, *mode;
  16976.  
  16977.     if (!mflag) {
  16978.         if (!doglob) {
  16979.             args = NULL;
  16980.         } else {
  16981.             if (ftemp) {
  16982.                 (void) fclose(ftemp);
  16983.                 ftemp = NULL;
  16984.             }
  16985.         }
  16986.         return(NULL);
  16987.     }
  16988.     if (!doglob) {
  16989.         if (args == NULL)
  16990.           args = argv;
  16991.         if ((cp = *++args) == NULL)
  16992.           args = NULL;
  16993.         return(cp);
  16994.     }
  16995.     if (ftemp == NULL) {
  16996.         (void) strcpy(temp, _PATH_TMP);
  16997. #ifdef MKTEMP
  16998. #ifndef MKSTEMP
  16999.         (void) mktemp(temp);
  17000. #endif /* MKSTEMP */
  17001. #endif /* MKTEMP */
  17002.         verbose = 0;
  17003.         oldhash = hash, hash = 0;
  17004. #ifdef FTP_PROXY
  17005.         if (doswitch) {
  17006.             pswitch(!proxy);
  17007.         }
  17008. #endif /* FTP_PROXY */
  17009.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  17010.           recvrequest ("NLST", temp, *argv, mode, 0);
  17011. #ifdef FTP_PROXY
  17012.         if (doswitch) {
  17013.             pswitch(!proxy);
  17014.         }
  17015. #endif /* FTP_PROXY */
  17016.         hash = oldhash;
  17017.         ftemp = fopen(temp, "r");
  17018.         unlink(temp);
  17019.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  17020.             printf("Can't find list of remote files, oops\n");
  17021.             return(NULL);
  17022.         }
  17023.     }
  17024.     if (fgets(buf, CKMAXPATH, ftemp) == NULL) {
  17025.         fclose(ftemp), ftemp = NULL;
  17026.         return(NULL);
  17027.     }
  17028.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  17029.       *cp = '\0';
  17030.     return(buf);
  17031. }
  17032. #endif /* NOT_USED */
  17033. #endif /* TCPSOCKET (top of file) */
  17034. #endif /* SYSFTP (top of file) */
  17035. #endif /* NOFTP (top of file) */
  17036.