home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv200.zip / ckcftp.c < prev    next >
C/C++ Source or Header  |  2001-12-09  |  428KB  |  14,908 lines

  1. /*  C K C F T P  --  FTP Client for C-Kermit  */
  2.  
  3. char *ckftpv = "FTP Client, 8.0.140, 8 Dec 2001";
  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) 1985, 2001,
  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 downloads by trying to CD to each filename.
  25.     If it works, it's a directory; if not, it's a file -- GET it.
  26.     But that won't work with wu-ftpd because it doesn't send directory
  27.     names.
  28.  
  29.   . Implement MLST when it reaches RFC status.  This is the real way to
  30.     handle recursive downloads.
  31.  
  32.   . Make syslog entries for session?  Files?
  33.  
  34.   . Messages are printed to stdout and stderr in random fashion.  We should
  35.     either print everything to stdout, or else be systematic about when
  36.     to use stderr.
  37.  
  38.   . Adapt HELP text for SEND, GET, BYE, FINISH, REMOTE, etc, to FTP?
  39.  
  40.   . Get FEATures of server upon connection; set flags & variables.
  41.     Test against ftp.ipswitch.com.
  42.  
  43.   . Implement mail (MAIL, MLFL, MSOM, etc) if any servers support it.
  44.  
  45.   . Adapt to VMS.  Big job because of its record-oriented file system.
  46.     RMS programmer required.  There are probably also some VMS TCP/IP
  47.     product-specific wrinkles, e.g. attribute preservation in VMS-to-VMS
  48.     transfers using special options for Multinet or other FTP servers
  49.     (find out about STRU VMS).
  50.  
  51.   . When using PASV mode, if we receive 0,0,0,0,N,M does that mean
  52.     use the same IP address as the Command channel?
  53.  
  54.   Notes:
  55.  
  56.   . The reason that the default file type is TEXT is that it is required
  57.     by FTP protocol.  When the connection begins the client and server must
  58.     agree to the type.  The default is TEXT.  (But then if we can find out
  59.     what kind of host the server is on, we can and do switch automatically
  60.     to binary if appropriate.)
  61. */
  62.  
  63. /*
  64.   Quick FTP command reference:
  65.  
  66.   RFC765 (1980) and earlier:
  67.     MODE  S(tream), B(lock), C(ompressed)
  68.     STRU  F(ILE), R(ECORD), P(AGE)
  69.     TYPE  A(SCII) <format>,  E(BCDIC) <format>, I(MAGE), L(OCAL) <bytesize>
  70.     PORT  - Port
  71.     PASV  - Passive mode
  72.     USER  - User
  73.     PASS  - Password
  74.     ACCT  - Account
  75.     CWD   - Change Working Directory
  76.     REIN  - Logout but not disconnect
  77.     QUIT  - Bye
  78.     RETR  - Retreive
  79.     STOR  - Store
  80.     APPE  - Append
  81.     ALLO  - Allocate
  82.     REST  - Restart
  83.     RNFR  - Rename from
  84.     RNTO  - Rename to
  85.     ABOR  - Cancel
  86.     DELE  - Delete
  87.     LIST  - Directory
  88.     NLST  - Name List
  89.     SITE  - Site parameters or commands
  90.     STAT  - Status
  91.     HELP  - Help
  92.     NOOP  - Noop
  93.  
  94.   RFC959 (1985):
  95.     CDUP  - Change to Parent Directory
  96.     SMNT  - Structure Mount
  97.     STOU  - Store Unique
  98.     RMD   - Remove Directory
  99.     MKD   - Make Directory
  100.     PWD   - Print Directory
  101.     SYST  - System
  102.  
  103.   RFC2389 (1998):
  104.     FEAT  - List Features (not done yet)
  105.     OPTS  - Send options (not done yet)
  106.  
  107.   RFC2640 (1999):
  108.     LANG  - Specify language for messages (not done)
  109.  
  110.   Pending (Internet Drafts):
  111.     MDTM  - File modification date-time (done)
  112.     MLST  - File name and attribute list (not done yet)
  113.     MAIL, MLFL, MSOM - mail delivery (not done)
  114.  
  115.   Alphabetical syntax list:
  116.     ABOR <CRLF>
  117.     ACCT <SP> <account-information> <CRLF>
  118.     ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
  119.     APPE <SP> <pathname> <CRLF>
  120.     CDUP <CRLF>
  121.     CWD  <SP> <pathname> <CRLF>
  122.     DELE <SP> <pathname> <CRLF>
  123.     FEAT <CRLF>
  124.     HELP [<SP> <string>] <CRLF>
  125.     LANG [<SP> <language-tag> ] <CRLF>
  126.     LIST [<SP> <pathname>] <CRLF>
  127.     MKD  <SP> <pathname> <CRLF>
  128.     MODE <SP> <mode-code> <CRLF>
  129.     NLST [<SP> <pathname>] <CRLF>
  130.     NOOP <CRLF>
  131.     OPTS <SP> <commandname> [ <SP> <command-options> ] <CRLF>
  132.     PASS <SP> <password> <CRLF>
  133.     PASV <CRLF>
  134.     PORT <SP> <host-port> <CRLF>
  135.     PWD  <CRLF>
  136.     QUIT <CRLF>
  137.     REIN <CRLF>
  138.     REST <SP> <marker> <CRLF>
  139.     RETR <SP> <pathname> <CRLF>
  140.     RMD  <SP> <pathname> <CRLF>
  141.     RNFR <SP> <pathname> <CRLF>
  142.     RNTO <SP> <pathname> <CRLF>
  143.     SITE <SP> <string> <CRLF>
  144.     SMNT <SP> <pathname> <CRLF>
  145.     STAT [<SP> <pathname>] <CRLF>
  146.     STOR <SP> <pathname> <CRLF>
  147.     STOU <CRLF>
  148.     STRU <SP> <structure-code> <CRLF>
  149.     SYST <CRLF>
  150.     TYPE <SP> <type-code> <CRLF>
  151.     USER <SP> <username> <CRLF>
  152. */
  153. #include "ckcsym.h"            /* Standard includes */
  154. #include "ckcdeb.h"
  155.  
  156. #ifndef NOFTP                /* NOFTP  = no FTP */
  157. #ifndef SYSFTP                /* SYSFTP = use external ftp client */
  158. #ifdef TCPSOCKET            /* Build only if TCP/IP included */
  159. #define CKCFTP_C
  160.  
  161. /* Note: much of the following duplicates what was done in ckcdeb.h */
  162. /* but let's not mess with it unless it causes trouble. */
  163.  
  164. #ifdef CK_ANSIC
  165. #include <stdarg.h>
  166. #else /* CK_ANSIC */
  167. #include <varargs.h>
  168. #endif /* CK_ANSIC */
  169. #include <signal.h>
  170. #include <setjmp.h>
  171. #include <sys/stat.h>
  172. #include <ctype.h>
  173. #include <errno.h>
  174. #ifndef NOTIMEH
  175. #include <time.h>
  176. #endif /* NOTIMEH */
  177. #ifndef EPIPE
  178. #define EPIPE 32            /* Broken pipe error */
  179. #endif /* EPIPE */
  180.  
  181. /* Kermit includes */
  182.  
  183. #include "ckcasc.h"
  184. #include "ckcker.h"
  185. #include "ckucmd.h"
  186. #include "ckuusr.h"
  187. #include "ckcnet.h"                     /* Includes ckctel.h */
  188. #include "ckctel.h"            /* (then why include it again?) */
  189. #include "ckcxla.h"
  190.  
  191. #ifdef CK_AUTHENTICATION
  192. #include "ckuath.h"
  193. #endif /* CK_AUTHENTICATION */
  194.  
  195. /*
  196.   How to get the struct timeval definition so we can call select().  The
  197.   xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  198.   targets.  The problem is: maybe we have already included some header file
  199.   that defined struct timeval, and maybe we didn't.  If we did, we don't want
  200.   to include another header file that defines it again or the compilation will
  201.   fail.  If we didn't, we have to include the header file where it's defined.
  202.   But in some cases even that won't work because of strict POSIX constraints
  203.   or somesuch, or because this introduces other conflicts (e.g. struct tm
  204.   multiply defined), in which case we have to define it ourselves, but this
  205.   can work only if we didn't already encounter a definition.
  206. */
  207. #ifndef DCLTIMEVAL
  208. #ifdef SV68R3V6
  209. #define DCLTIMEVAL
  210. #else
  211. #ifdef SCO234
  212. #define DCLTIMEVAL
  213. #endif /* SCO234 */
  214. #endif /* SV68R3V6 */
  215. #endif /* DCLTIMEVAL */
  216.  
  217. #ifdef DCLTIMEVAL
  218. /* Also maybe in some places the elements must be unsigned... */
  219. struct timeval {
  220.     long tv_sec;
  221.     long tv_usec;
  222. };
  223. #ifdef COMMENT
  224. /* Currently we don't use this... */
  225. struct timezone {
  226.     int tz_minuteswest;
  227.     int tz_dsttime;
  228. };
  229. #endif /* COMMENT */
  230. #else  /* !DCLTIMEVAL */
  231. #ifndef NOSYSTIMEH
  232. #ifdef SYSTIMEH
  233. #include <sys/time.h>
  234. #endif /* SYSTIMEH */
  235. #endif /* NOSYSTIMEH */
  236. #ifndef NOSYSTIMEBH
  237. #ifdef SYSTIMEBH
  238. #include <sys/timeb.h>
  239. #endif /* SYSTIMEBH */
  240. #endif /* NOSYSTIMEBH */
  241. #endif /* DCLTIMEVAL */
  242.  
  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. #ifdef CK_DES
  414. #ifdef CK_SSL
  415. #ifndef LIBDES
  416. #define LIBDES
  417. #endif /* LIBDES */
  418. #endif /* CK_SSL */
  419. #endif /* CK_DES */
  420.  
  421. #ifdef CRYPT_DLL
  422. #ifndef LIBDES
  423. #define LIBDES
  424. #endif /* LIBDES */
  425. #endif /* CRYPT_DLL */
  426.  
  427. #ifdef FTP_KRB4
  428. #define des_cblock Block
  429. #define des_key_schedule Schedule
  430. #ifdef KRB524
  431. #ifdef NT
  432. #define _WINDOWS
  433. #endif /* NT */
  434. #include "kerberosIV/krb.h"
  435. #else /* KRB524 */
  436. #ifdef SOLARIS
  437. #ifndef sun
  438. /* For some reason lost in history the Makefile Solaris targets have -Usun */
  439. #define sun
  440. #endif /* sun */
  441. #endif /* SOLARIS */
  442. #include "krb.h"
  443. #define krb_get_err_text_entry krb_get_err_text
  444. #endif /* KRB524 */
  445. #endif /* FTP_KRB4 */
  446.  
  447. #ifdef CK_SSL
  448. #ifdef FTP_KRB4
  449. #ifndef HEADER_DES_H
  450. #define HEADER_DES_H
  451. #endif /* HEADER_DES_H */
  452. #endif /* FTP_KRB4 */
  453. #include "ck_ssl.h"
  454. #endif /* CK_SSL */
  455.  
  456. #ifdef FTP_SRP
  457. #ifdef HAVE_PWD_H
  458. #include "pwd.h"
  459. #endif /* HAVE_PWD_H */
  460. #include "t_pwd.h"
  461. #include "t_client.h"
  462. #include "krypto.h"
  463. #endif /* FTP_SRP */
  464.  
  465. #ifdef FTP_GSSAPI
  466. #include <gssapi/gssapi.h>
  467. /*
  468.   Need to include the krb5 file, because we're doing manual fallback
  469.   from the v2 mech to the v1 mech.  Once there's real negotiation,
  470.   we can be generic again.
  471. */
  472. #include <gssapi/gssapi_generic.h>
  473. #include <gssapi/gssapi_krb5.h>
  474. static gss_ctx_id_t gcontext;
  475. #endif /* FTP_GSSAPI */
  476.  
  477. #ifdef OS2
  478. #ifdef FTP_SRP
  479. #define MAP_KRYPTO
  480. #ifdef SRPDLL
  481. #define MAP_SRP
  482. #endif /* SRPDLL */
  483. #endif /* FTP_SRP */
  484. #ifdef FTP_KRB4
  485. #define MAP_KRB4
  486. #define MAP_DES
  487. #endif /* FTP_KRB4 */
  488. #ifdef FTP_GSSAPI
  489. #define MAP_GSSAPI
  490. #endif /* FTP_GSSAPI */
  491. #include "ckoath.h"
  492. #endif /* OS2 */
  493.  
  494. #ifdef FTP_KRB4
  495. static char ftp_realm[REALM_SZ + 1];
  496. static KTEXT_ST ftp_tkt;
  497. #ifdef OS2
  498. static LEASH_CREDENTIALS ftp_cred;
  499. #else /* OS2 */
  500. static CREDENTIALS ftp_cred;
  501. #endif /* OS2 */
  502. static MSG_DAT ftp_msg_data;
  503. static des_key_schedule ftp_sched;
  504. static int foo[4] = {99,99,99,99};
  505. #endif /* FTP_KRB4 */
  506.  
  507. /* FTP_SECURITY is defined if any of the above is selected */
  508.  
  509. #ifndef FTP_SECURITY
  510. #ifdef FTP_GSSAPI
  511. #define FTP_SECURITY
  512. #else
  513. #ifdef FTP_KRB4
  514. #define FTP_SECURITY
  515. #else
  516. #ifdef FTP_SRP
  517. #define FTP_SECURITY
  518. #else
  519. #ifdef CK_SSL
  520. #define FTP_SECURITY
  521. #endif /* CK_SSL */
  522. #endif /* FTP_SRP */
  523. #endif /* FTP_KRB4 */
  524. #endif /* FTP_GSSAPI */
  525. #endif /* FTP_SECURITY */
  526.  
  527. /* Operational definitions */
  528.  
  529. #define DEF_VBM 0            /* Default verbose mode */
  530. /* #define SETVBM */            /* (see getreply) */
  531.  
  532. #define URL_ONEFILE            /* GET, not MGET, for FTP URL */
  533.  
  534. #define FTP_BUFSIZ 10240        /* Max size for FTP cmds & replies */
  535. #define SRVNAMLEN 32            /* Max length for server type name */
  536.  
  537. #ifndef MGETMAX                /* Max operands for MGET command */
  538. #define MGETMAX 1000
  539. #endif /* MGETMAX */
  540.  
  541. #ifdef FTP_SRP
  542. #define FUDGE_FACTOR 100
  543. #endif /* FTP_SRP */
  544.  
  545. /*
  546.   Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  547.   number bytes.  Must be defined for each auth type.
  548.   GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  549.   3DES requires 56 bytes.  Lets use 96 just to be sure.
  550. */
  551. #ifdef FTP_GSSAPI
  552. #ifndef FUDGE_FACTOR
  553. #define FUDGE_FACTOR 96
  554. #endif /* FUDGE_FACTOR */
  555. #endif /* FTP_GSSAPI */
  556.  
  557. #ifdef FTP_KRB4
  558. #ifndef FUDGE_FACTOR
  559. #define FUDGE_FACTOR 32
  560. #endif /* FUDGE_FACTOR */
  561. #endif /* FTP_KRB4 */
  562.  
  563. #ifndef FUDGE_FACTOR            /* In case no auth types define it */
  564. #define FUDGE_FACTOR 0
  565. #endif /* FUDGE_FACTOR */
  566.  
  567. /* Fascist compiler toadying */
  568.  
  569. #ifndef SENDARG2TYPE
  570. #ifdef COMMENT                /* Might be needed here and there */
  571. #define SENDARG2TYPE const char *
  572. #else
  573. #define SENDARG2TYPE char *
  574. #endif /* COMMENT */
  575. #endif /* SENDARG2TYPE */
  576.  
  577. /* Common text messages */
  578.  
  579. static char * nocx = "?No FTP control connection\n";
  580.  
  581. static char *fncnam[] = {
  582.   "rename", "overwrite", "backup", "append", "discard", "ask", "update", ""
  583. };
  584.  
  585. /* Macro definitions */
  586.  
  587. /* Used to speed up text-mode PUTs */
  588. #define zzout(fd,c) \
  589. ((fd<0)?(-1):((nout>ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
  590.  
  591. #define CHECKCONN() if(!connected){printf(nocx);return(-9);}
  592.  
  593. /* Externals */
  594.  
  595. #ifdef DYNAMIC
  596. extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
  597. #else
  598. extern char zinbuffer[], zoutbuffer[];
  599. #endif /* DYNAMIC */
  600. extern char *zinptr, *zoutptr;
  601. extern int zincnt, zoutcnt, zobufsize, fncact;
  602.  
  603. #ifdef CK_TMPDIR
  604. extern int f_tmpdir;            /* Directory changed temporarily */
  605. extern char savdir[];            /* For saving current directory */
  606. extern char * dldir;
  607. #endif /* CK_TMPDIR */
  608.  
  609. extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
  610.  
  611. extern xx_strp xxstring;
  612. extern struct keytab onoff[], txtbin[], rpathtab[], colxtab[];
  613. extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
  614. extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
  615. extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
  616. extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
  617. extern int ncolx, nolinks;
  618. extern long fsize, ffc, tfc, filcnt, xfsecs, tfcps, cps, oldcps;
  619. #ifdef GFTIMER
  620. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  621. #else
  622. extern long xfsecs;
  623. #endif /* GFTIMER */
  624.  
  625. extern char filnam[], * filefile, myhost[];
  626. extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
  627. extern int g_skipbup, skipbup, sendmode;
  628. extern int g_displa, fdispla, displa;
  629.  
  630. #ifndef NOCSETS
  631. extern int nfilc, dcset7, dcset8, fileorder;
  632. extern struct csinfo fcsinfo[];
  633. extern struct keytab fcstab[];
  634. extern int fcharset;
  635. #endif /* NOCSETS */
  636.  
  637. extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
  638. extern char sndnbefore[], sndnafter[], *rcvexcept[];
  639. extern CHAR feol;
  640. extern long sendstart, sndsmaller, sndlarger, rs_len;
  641.  
  642. extern char * remdest;
  643. extern int remfile, remappd, rempipe;
  644.  
  645. #ifndef NOSPL
  646. extern int cmd_quoting;
  647. #ifdef PUTARRAY
  648. extern int sndxlo, sndxhi, sndxin;
  649. extern char sndxnam[];
  650. extern char **a_ptr[];            /* Array pointers */
  651. extern int a_dim[];            /* Array dimensions */
  652. #endif /* PUTARRAY */
  653. #endif /* NOSPL */
  654.  
  655. #ifndef NOMSEND                /* MPUT and ADD SEND-LIST lists */
  656. extern char *msfiles[];
  657. extern int filesinlist;
  658. extern struct filelist * filehead;
  659. extern struct filelist * filetail;
  660. extern struct filelist * filenext;
  661. extern int addlist;
  662. extern char fspec[];            /* Most recent filespec */
  663. extern int fspeclen;            /* Length of fspec[] buffer */
  664. #endif /* NOMSEND */
  665.  
  666. extern int pipesend;
  667. #ifdef PIPESEND
  668. extern char * sndfilter, * rcvfilter;
  669. #endif /* PIPESEND */
  670.  
  671. #ifdef CKROOT
  672. extern int ckrooterr;
  673. #endif /* CKROOT */
  674.  
  675. #ifdef KRB4
  676. extern int krb4_autoget;
  677. #endif /* KRB4 */
  678.  
  679. #ifdef KRB5
  680. extern int krb5_autoget;
  681. extern int krb5_d_no_addresses;
  682. #endif /* KRB5 */
  683.  
  684. #ifdef DCMDBUF
  685. extern char *atmbuf;            /* Atom buffer (malloc'd) */
  686. extern char *cmdbuf;            /* Command buffer (malloc'd) */
  687. extern char *line;            /* Big string buffer #1 */
  688. extern char *tmpbuf;            /* Big string buffer #2 */
  689. #else
  690. extern char atmbuf[];            /* The same, but static */
  691. extern char cmdbuf[];
  692. extern char line[];
  693. extern char tmpbuf[];
  694. #endif /* DCMDBUF */
  695.  
  696. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  697.  
  698. /* Public variables declared here */
  699.  
  700. #ifdef NOXFER
  701. int ftpget  =  1;            /* GET/PUT/REMOTE orientation FTP */
  702. #else
  703. int ftpget  =  2;            /* GET/PUT/REMOTE orientation AUTO */
  704. #endif /* NOXFER */
  705. int ftpcode = -1;            /* Last FTP response code */
  706. int ftp_cmdlin = 0;            /* FTP invoked from command line */
  707. int ftp_fai = 0;            /* FTP failure count */
  708. int ftp_deb = 0;            /* FTP debugging */
  709. int ftp_knf = 0;            /* Keep NLST file */
  710. int ftp_log = 1;            /* FTP Auto-login */
  711. int ftp_action = 0;            /* FTP action from command line */
  712. int ftp_dates = 0;            /* Set file dates from server */
  713.  
  714. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  715. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  716. char * ftp_host = NULL;            /* FTP hostname */
  717. char * ftp_logname = NULL;        /* FTP username */
  718. char * ftp_rdir = NULL;            /* Remote directory from cmdline */
  719.  
  720. /* Static global variables */
  721. /*
  722.   This is just a first stab -- these strings should match how the
  723.   corresponding FTP servers identify themselves.
  724. */
  725. #ifdef UNIX
  726. static char * myostype = "UNIX";
  727. #else
  728. #ifdef VMS
  729. /* not yet... */
  730. static char * myostype = "VMS";
  731. #else
  732. #ifdef OS2
  733. #ifdef NT
  734. static char * myostype = "WIN32";
  735. #else
  736. static char * myostype = "OS/2";
  737. #endif /* NT */
  738. #else
  739. static char * myostype = "UNSUPPORTED";
  740. #endif /* OS2  */
  741. #endif /* VMS */
  742. #endif /* UNIX */
  743.  
  744. static int alike = 0;            /* Client/server like platforms */
  745. static int local = 1;            /* Shadows Kermit global 'local' */
  746. static int dout = -1;            /* Data connection file descriptor */
  747. static int dpyactive = 0;        /* Data transfer is active */
  748. static int globaldin = -1;        /* Data connection f.d. */
  749. static int out2screen = 0;        /* GET output is to screen */
  750. static int forcetype = 0;        /* Force text or binary mode */
  751. static int cancelfile = 0;        /* File canceled */
  752. static int cancelgroup = 0;        /* Group canceled */
  753. static int anonymous = 0;        /* Logging in as anonymous */
  754. static int loggedin = 0;        /* Logged in (or not) */
  755. static int puterror = 0;        /* What to do on PUT error */
  756. static int geterror = 0;        /* What to do on GET error */
  757. static int rfrc = 0;            /* remote_files() return code */
  758. static int okrestart = 0;        /* Server understands REST */
  759. static int printlines = 0;        /* getreply()should print data lines */
  760. static int haveurl = 0;            /* Invoked by command-line FTP URL */
  761.  
  762. static int i, /* j, k, */ x, y, z;    /* Volatile temporaries */
  763. static int c0, c1;            /* Temp variables for characters */
  764.  
  765. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  766. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  767.  
  768. #define RFNBUFSIZ 4096
  769.  
  770. static unsigned int maxbuf = 0, actualbuf = 0;
  771. static CHAR *ucbuf = NULL;
  772. static int ucbufsiz = 0;
  773. static unsigned int nout = 0;        /* Number of chars in ucbuf */
  774.  
  775. static jmp_buf recvcancel;
  776. static jmp_buf sendcancel;
  777. static jmp_buf ptcancel;
  778. static jmp_buf jcancel;
  779. static int ptabflg = 0;
  780.  
  781. /* Protection level symbols */
  782.  
  783. #define FPL_CLR 1            /* Clear */
  784. #define FPL_SAF 2            /* Safe */
  785. #define FPL_PRV 3            /* Private */
  786. #define FPL_CON 4                       /* Confidential */
  787.  
  788. /* File type symbols keyed to the file-type symbols from ckcker.h */
  789.  
  790. #define FTT_ASC XYFT_T            /* ASCII (text) */
  791. #define FTT_BIN XYFT_B            /* Binary (image) */
  792. #define FTT_TEN XYFT_X            /* TENEX (TOPS-20) */
  793.  
  794. /* SET FTP values */
  795.  
  796. static int                /* SET FTP values... */
  797.   ftp_aut = 1,                /* Auto-authentication */
  798. #ifdef FTP_SECURITY
  799.   ftp_cry = 1,                /* Auto-encryption */
  800.   ftp_cfw = 0,                /* Credential forwarding */
  801. #endif /* FTP_SECURITY */
  802.   ftp_cpl = FPL_CLR,            /* Command protection level */
  803.   ftp_dpl = FPL_CLR,            /* Data protection level */
  804. #ifdef FTP_PROXY
  805.   ftp_prx = 0,                          /* Use proxy */
  806. #endif /* FTP_PROXY */
  807.   sav_psv = -1,                /* For saving passive mode */
  808.   ftp_psv = 1,                /* Passive mode */
  809.   ftp_spc = 1,                /* Send port commands */
  810.   ftp_typ = FTT_ASC,            /* Type */
  811.   get_auto = 1,                /* Automatic type switching for GET */
  812.   tenex = 0,                /* Type is Tenex */
  813.   ftp_usn = 0,              /* Unique server names */
  814.   ftp_prm = 0,                /* Permissions */
  815.   ftp_cnv = 2,                /* Filename conversion (2 = auto) */
  816.   ftp_vbm = DEF_VBM,            /* Verbose mode */
  817.   ftp_vbx = DEF_VBM,            /* Sticky version of same */
  818.   ftp_err = 0,                /* Error action */
  819.   ftp_fnc = -1;                /* Filename collision action */
  820.  
  821. static int
  822. #ifdef NOCSETS
  823.   ftp_csr = -1,                /* Remote (server) character set */
  824. #else
  825.   ftp_csr = FC_UTF8,
  826. #endif /* NOCSETS */
  827.   ftp_xla = 0;                /* Character-set translation on/off */
  828. int
  829.   ftp_csx = -1,                /* Remote charset currently in use */
  830.   ftp_csl = -1;                /* Local charset currently in use */
  831.  
  832. static int g_ftp_typ = FTT_ASC;        /* For saving and restoring ftp_typ */
  833.  
  834. char * ftp_nml = NULL;            /* /NAMELIST */
  835. char * ftp_tmp = NULL;            /* Temporary string */
  836. static char * ftp_acc = NULL;        /* Account string */
  837. static char * auth_type = NULL;         /* Authentication type */
  838. static char * srv_renam = NULL;        /* Server-rename string */
  839. FILE * fp_nml = NULL;            /* Namelist file pointer */
  840.  
  841. static int csocket = -1;        /* Control socket */
  842. static int connected = 0;        /* Connected to FTP server */
  843. static short ftp_port = 0;              /* FTP port */
  844. #ifdef FTPHOST
  845. static int hostcmd = 0;                 /* Has HOST command been sent */
  846. #endif /* FTPHOST */
  847. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  848. static char bytename[8];
  849.  
  850. /* For parsing replies to FTP server command */
  851. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  852.  
  853. #ifdef FTP_PROXY
  854. static int proxy, unix_proxy
  855. #endif /* FTP_PROXY */
  856.  
  857. static char pasv[64];                   /* Passive-mode port */
  858. static int passivemode = 0;
  859. static int sendport = 0;
  860. static int servertype = 0;        /* FTP server's OS type */
  861.  
  862. static int testing = 0;
  863. static char ftpcmdbuf[FTP_BUFSIZ];
  864.  
  865. /* Macro definitions */
  866.  
  867. #define    UC(b) ckitoa(((int)b)&0xff)
  868. #define    nz(x) ((x) == 0 ? 1 : (x))
  869.  
  870. /* Command tables and definitions */
  871.  
  872. #define FTP_ACC  1            /* FTP command keyword codes */
  873. #define FTP_APP  2
  874. #define FTP_CWD  3
  875. #define FTP_CHM  4
  876. #define FTP_CLS  5
  877. #define FTP_DEL  6
  878. #define FTP_DIR  7
  879. #define FTP_GET  8
  880. #define FTP_IDL  9
  881. #define FTP_MDE 10
  882. #define FTP_MDI 11
  883. #define FTP_MGE 12
  884. #define FTP_MKD 13
  885. #define FTP_MOD 14
  886. #define FTP_MPU 15
  887. #define FTP_OPN 16
  888. #define FTP_PUT 17
  889. #define FTP_PWD 18
  890. #define FTP_RGE 19
  891. #define FTP_REN 20
  892. #define FTP_RES 21
  893. #define FTP_HLP 22
  894. #define FTP_RMD 23
  895. #define FTP_STA 24
  896. #define FTP_SIT 25
  897. #define FTP_SIZ 26
  898. #define FTP_SYS 27
  899. #define FTP_UMA 28
  900. #define FTP_GUP 29
  901. #define FTP_USR 30
  902. #define FTP_QUO 31
  903. #define FTP_TYP 32
  904. #define FTP_FEA 33
  905. #define FTP_OPT 34
  906. #define FTP_CHK 35
  907. #define FTP_VDI 36
  908.  
  909. struct keytab gprtab[] = {        /* GET-PUT-REMOTE keywords */
  910.     { "auto",    2, 0 },
  911.     { "ftp",     1, 0 },
  912.     {"kermit",   0, 0  }
  913. };
  914.  
  915. static struct keytab qorp[] = {        /* QUIT or PROCEED keywords */
  916.     { "proceed", 0, 0 },        /* 0 = proceed */
  917.     { "quit",    1, 0 }            /* 1 = quit */
  918. };
  919.  
  920. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  921.     { "account",   FTP_ACC, 0 },
  922.     { "append",    FTP_APP, 0 },
  923.     { "bye",       FTP_CLS, 0 },
  924.     { "cd",        FTP_CWD, 0 },
  925.     { "cdup",      FTP_GUP, 0 },
  926.     { "check",     FTP_CHK, 0 },
  927.     { "chmod",     FTP_CHM, 0 },
  928.     { "close",     FTP_CLS, 0 },
  929.     { "cwd",       FTP_CWD, CM_INV },
  930.     { "delete",    FTP_MDE, 0 },
  931.     { "directory", FTP_DIR, 0 },
  932.     { "features",  FTP_FEA, 0 },
  933.     { "get",       FTP_GET, 0 },
  934.     { "help",      FTP_HLP, 0 },
  935.     { "idle",      FTP_IDL, 0 },
  936.     { "login",     FTP_USR, CM_INV },
  937.     { "mdelete",   FTP_MDE, CM_INV },
  938.     { "mget",      FTP_MGE, 0 },
  939.     { "mkdir",     FTP_MKD, 0 },
  940.     { "modtime",   FTP_MOD, 0 },
  941.     { "mput",      FTP_MPU, 0 },
  942.     { "open",      FTP_OPN, 0 },
  943.     { "opts",      FTP_OPT, CM_INV },
  944.     { "options",   FTP_OPT, 0 },
  945.     { "put",       FTP_PUT, 0 },
  946.     { "pwd",       FTP_PWD, 0 },
  947.     { "quit",      FTP_CLS, CM_INV },
  948.     { "quote",     FTP_QUO, 0 },
  949.     { "reget",     FTP_RGE, 0 },
  950.     { "rename",    FTP_REN, 0 },
  951.     { "reset",     FTP_RES, 0 },
  952.     { "rmdir",     FTP_RMD, 0 },
  953.     { "send",      FTP_PUT, CM_INV },
  954.     { "site",      FTP_SIT, 0 },
  955.     { "size",      FTP_SIZ, 0 },
  956.     { "status",    FTP_STA, 0 },
  957.     { "system",    FTP_SYS, 0 },
  958.     { "type",      FTP_TYP, 0 },
  959.     { "umask",     FTP_UMA, 0 },
  960.     { "up",        FTP_GUP, CM_INV },
  961.     { "user",      FTP_USR, 0 },
  962.     { "vdirectory",FTP_VDI, 0 },
  963.     { "", 0, 0 }
  964. };
  965. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  966.  
  967. #define OPN_ANO 1
  968. #define OPN_PSW 2
  969. #define OPN_USR 3
  970. #define OPN_ACC 4
  971. #define OPN_ACT 5
  972. #define OPN_PSV 6
  973. #define OPN_TLS 7
  974.  
  975. #ifdef FTP_SECURITY
  976. #ifdef CK_SSL
  977. #define USETLSTAB
  978. static struct keytab tlstab[] = {    /* FTP SSL/TLS switches */
  979.     { "/ssl",       OPN_TLS, 0    },  
  980.     { "/tls",       OPN_TLS, 0    },
  981.     { "", 0, 0 }
  982. };
  983. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  984. #endif /* CK_SSL */
  985. #endif /* FTP_SECURITY */
  986.  
  987. static struct keytab ftpswitab[] = {    /* FTP command switches */
  988.     { "/account",   OPN_ACC, CM_ARG },
  989.     { "/active",    OPN_ACT, 0      },
  990.     { "/anonymous", OPN_ANO, 0      },
  991.     { "/passive",   OPN_PSV, 0      },
  992.     { "/password",  OPN_PSW, CM_ARG },
  993.     { "/user",      OPN_USR, CM_ARG },
  994.     { "", 0, 0 }
  995. };
  996. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  997.  
  998. /* SET FTP command keyword indices */
  999.  
  1000. #define FTS_AUT  1            /* Autoauthentication */
  1001. #define FTS_CRY  2            /* Encryption */
  1002. #define FTS_LOG  3            /* Autologin */
  1003. #define FTS_CPL  4            /* Command protection level */
  1004. #define FTS_CFW  5            /* Credentials forwarding */
  1005. #define FTS_DPL  6            /* Data protection level */
  1006. #define FTS_DBG  7            /* Debugging */
  1007. #define FTS_PSV  8            /* Passive mode */
  1008. #define FTS_SPC  9            /* Send port commands */
  1009. #define FTS_TYP 10            /* (file) Type */
  1010. #define FTS_USN 11            /* Unique server names (for files) */
  1011. #define FTS_VBM 12            /* Verbose mode */
  1012. #define FTS_ATP 13            /* Authentication type */
  1013. #define FTS_CNV 14            /* Filename conversion */
  1014. #define FTS_TST 15            /* Test (progress) messages */
  1015. #define FTS_PRM 16            /* (file) Permissions */
  1016. #define FTS_XLA 17            /* Charset translation */
  1017. #define FTS_CSR 18            /* Server charset */
  1018. #define FTS_ERR 19            /* Error action */
  1019. #define FTS_FNC 20            /* Collision */
  1020. #define FTS_SRP 21                      /* SRP options */
  1021. #define FTS_GFT 22            /* GET automatic file-type switching */
  1022. #define FTS_DAT 23            /* Set file dates */
  1023.  
  1024. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1025.  
  1026. #define PUT_UPD 1            /* Update */
  1027. #define PUT_RES 2            /* Restart */
  1028. #define PUT_SIM 4            /* Simulation */
  1029.  
  1030. #ifdef FTP_SECURITY
  1031. /* FTP authentication options */
  1032.  
  1033. #define FTA_AUTO 0            /* Auto */
  1034. #define FTA_SRP  1            /* SRP */
  1035. #define FTA_GK5  2            /* Kerberos 5 */
  1036. #define FTA_K4   3            /* Kerberos 4 */
  1037. #define FTA_SSL  4            /* SSL */
  1038. #define FTA_TLS  5            /* TLS */
  1039.  
  1040. /* FTP authentication types */
  1041.  
  1042. #define FTPATYPS 8
  1043. static int ftp_auth_type[FTPATYPS] = {
  1044. #ifdef FTP_GSSAPI
  1045.     FTA_GK5,                /* GSSAPI Kerberos 5 */
  1046. #endif /* FTP_GK5 */
  1047. #ifdef FTP_SRP
  1048.     FTA_SRP,                /* SRP */
  1049. #endif /* FTP_SRP */
  1050. #ifdef FTP_KRB4
  1051.     FTA_K4,                /* Kerberos 4 */
  1052. #endif /* FTP_KRB4 */
  1053. #ifdef CK_SSL
  1054.     FTA_TLS,                /* TLS */
  1055.     FTA_SSL,                /* SSL */
  1056. #endif /* CK_SSL */
  1057.     0
  1058. };
  1059.  
  1060. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1061.     { "automatic", FTA_AUTO,  CM_INV },
  1062. #ifdef FTP_GSSAPI
  1063.     { "gssapi-krb5", FTA_GK5, 0 },
  1064. #endif /* FTP_GSSAPI */
  1065. #ifdef FTP_KRB4
  1066.     { "k4",       FTA_K4,     CM_INV },
  1067. #endif /* FTP_KRB4 */
  1068. #ifdef FTP_GSSAPI
  1069.     { "k5",        FTA_GK5,   CM_INV },
  1070. #endif /* FTP_GSSAPI */
  1071. #ifdef FTP_KRB4
  1072.     { "kerberos4", FTA_K4,    0 },
  1073. #endif /* FTP_KRB4 */
  1074. #ifdef FTP_GSSAPI
  1075.     { "kerberos5", FTA_GK5,   CM_INV },
  1076. #endif /* FTP_GSSAPI */
  1077. #ifdef FTP_KRB4
  1078.     { "kerberos_iv",FTA_K4,   CM_INV },
  1079. #endif /* FTP_KRB4 */
  1080. #ifdef FTP_GSSAPI
  1081.     { "kerberos_v", FTA_GK5,  CM_INV },
  1082. #endif /* FTP_GSSAPI */
  1083. #ifdef FTP_KRB4
  1084.     { "krb4",     FTA_K4,     CM_INV },
  1085. #endif /* FTP_KRB4 */
  1086. #ifdef FTP_GSSAPI
  1087.     { "krb5",     FTA_GK5,    CM_INV },
  1088. #endif /* FTP_GSSAPI */
  1089. #ifdef FTP_SRP
  1090.     { "srp",      FTA_SRP,     0 },
  1091. #endif /* FTP_SRP */
  1092. #ifdef CK_SSL
  1093.     { "ssl",      FTA_SSL,     0 },
  1094.     { "tls",      FTA_TLS,     0 },
  1095. #endif /* CK_SSL */
  1096.     { "", 0, 0 }
  1097. };
  1098. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1099.  
  1100. #ifdef FTP_SRP
  1101. #define SRP_CIPHER 1
  1102. #define SRP_HASH   2
  1103. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1104.     { "cipher",   SRP_CIPHER,     0 },
  1105.     { "hash",     SRP_HASH,       0 },
  1106.     { "", 0, 0 }
  1107. };
  1108. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1109. #endif /* FTP_SRP */
  1110. #endif /* FTP_SECURITY */
  1111.  
  1112. static struct keytab ftpset[] = {    /* SET FTP commmand table */
  1113. #ifdef FTP_SECURITY
  1114.     { "authtype",                 FTS_ATP, 0 },
  1115.     { "autoauthentication",       FTS_AUT, 0 },
  1116.     { "autoencryption",           FTS_CRY, 0 },
  1117. #endif /* FTP_SECURITY */
  1118.     { "autologin",                FTS_LOG, 0 },
  1119. #ifndef NOCSETS
  1120.     { "character-set-translation",FTS_XLA, 0 },
  1121. #endif /* NOCSETS */
  1122.     { "collision",                FTS_FNC, 0 },
  1123. #ifdef FTP_SECURITY
  1124.     { "command-protection-level", FTS_CPL, 0 },
  1125.     { "cpl",                      FTS_CPL, CM_INV },
  1126.     { "credential-forwarding",    FTS_CFW, 0 },
  1127.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1128.     { "data-protection-level",    FTS_DPL, 0 },
  1129. #endif /* FTP_SECURITY */
  1130.     { "dates",                    FTS_DAT, 0 },
  1131.     { "debug",                    FTS_DBG, 0 },
  1132. #ifdef FTP_SECURITY
  1133.     { "dpl",                      FTS_DPL, CM_INV },
  1134. #endif /* FTP_SECURITY */
  1135.     { "error-action",             FTS_ERR, 0 },
  1136.     { "filenames",                FTS_CNV, 0 },
  1137.     { "get-filetype-switching",   FTS_GFT, 0 },
  1138.     { "passive-mode",             FTS_PSV, 0 },
  1139.     { "permissions",              FTS_PRM, 0 },
  1140.     { "progress-messages",        FTS_TST, 0 },
  1141.     { "send-port-commands",       FTS_SPC, 0 },
  1142. #ifndef NOCSETS
  1143.     { "server-character-set",     FTS_CSR, 0 },
  1144. #endif /* NOCSETS */
  1145. #ifdef FTP_SRP
  1146.     { "srp",                      FTS_SRP, 0 },
  1147. #else
  1148.     { "srp",                      FTS_SRP, CM_INV },
  1149. #endif /* FTP_SRP */
  1150.     { "type",                     FTS_TYP, 0 },
  1151.     { "unique-server-names",      FTS_USN, 0 },
  1152.     { "verbose-mode",             FTS_VBM, 0 },
  1153.     { "", 0, 0 }
  1154. };
  1155. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1156.  
  1157. /*
  1158.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1159.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1160.   Don't just make up new ones, since the number of SND_xxx options must be
  1161.   known in advance for the switch-parsing arrays.
  1162. */
  1163. #define SND_USN SND_PRO            /* /UNIQUE instead of /PROTOCOL */
  1164. #define SND_PRM SND_PIP            /* /PERMISSIONS instead of /PIPES */
  1165. #define SND_TEN SND_CAL            /* /TENEX instead of /CALIBRATE */
  1166.  
  1167. static struct keytab putswi[] = {    /* FTP PUT switch table */
  1168.     { "/after",                SND_AFT, CM_ARG },
  1169. #ifdef PUTARRAY
  1170.     { "/array",                SND_ARR, CM_ARG },
  1171. #endif /* PUTARRAY */
  1172.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1173.     { "/as-name",              SND_ASN, CM_ARG },
  1174.     { "/ascii",                SND_TXT, CM_INV },
  1175.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1176.     { "/before",               SND_BEF, CM_ARG },
  1177.     { "/binary",               SND_BIN, 0 },
  1178. #ifdef PUTPIPE
  1179.     { "/command",              SND_CMD, CM_PSH },
  1180. #endif /* PUTPIPE */
  1181.     { "/delete",               SND_DEL, 0 },
  1182. #ifdef UNIXOROSK
  1183.     { "/dotfiles",             SND_DOT, 0 },
  1184. #endif /* UNIXOROSK */
  1185.     { "/error-action",         SND_ERR, CM_ARG },
  1186.     { "/except",               SND_EXC, CM_ARG },
  1187.     { "/filenames",            SND_NAM, CM_ARG },
  1188. #ifdef PIPESEND
  1189. #ifndef NOSPL
  1190.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1191. #endif /* NOSPL */
  1192. #endif /* PIPESEND */
  1193. #ifdef CKSYMLINK
  1194.     { "/followlinks",          SND_LNK, 0 },
  1195. #endif /* CKSYMLINK */
  1196. #ifdef VMS
  1197.     { "/image",                SND_IMG, 0 },
  1198. #else
  1199.     { "/image",                SND_BIN, CM_INV },
  1200. #endif /* VMS */
  1201.     { "/larger-than",          SND_LAR, CM_ARG },
  1202.     { "/listfile",             SND_FIL, CM_ARG },
  1203. #ifndef NOCSETS
  1204.     { "/local-character-set",  SND_CSL, CM_ARG },
  1205. #endif /* NOCSETS */
  1206. #ifdef CK_TMPDIR
  1207.     { "/move-to",              SND_MOV, CM_ARG },
  1208. #endif /* CK_TMPDIR */
  1209.     { "/nobackupfiles",        SND_NOB, 0 },
  1210. #ifdef UNIXOROSK
  1211.     { "/nodotfiles",           SND_NOD, 0 },
  1212. #endif /* UNIXOROSK */
  1213. #ifdef CKSYMLINK
  1214.     { "/nofollowlinks",           SND_NLK, 0 },
  1215. #endif /* CKSYMLINK */
  1216.     { "/not-after",            SND_NAF, CM_ARG },
  1217.     { "/not-before",           SND_NBE, CM_ARG },
  1218. #ifdef UNIX
  1219.     { "/permissions",          SND_PRM, CM_ARG },
  1220. #else
  1221.     { "/permissions",          SND_PRM, CM_INV },
  1222. #endif /* UNIX */
  1223.     { "/quiet",                SND_SHH, 0 },
  1224. #ifdef FTP_RESTART
  1225.     { "/recover",              SND_RES, 0 },
  1226. #endif /* FTP_RESTART */
  1227. #ifdef RECURSIVE
  1228.     { "/recursive",            SND_REC, 0 },
  1229. #endif /* RECURSIVE */
  1230.     { "/rename-to",            SND_REN, CM_ARG },
  1231. #ifdef FTP_RESTART
  1232.     { "/restart",              SND_RES, CM_INV },
  1233. #endif /* FTP_RESTART */
  1234. #ifndef NOCSETS
  1235.     { "/server-character-set", SND_CSR, CM_ARG },
  1236. #endif /* NOCSETS */
  1237.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1238.     { "/simulate",             SND_SIM, 0 },
  1239.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1240.     { "/smaller-than",         SND_SMA, CM_ARG },
  1241. #ifdef COMMENT
  1242.     { "/starting-at",          SND_STA, CM_ARG },
  1243. #endif /* COMMENT */
  1244. #ifdef RECURSIVE
  1245.     { "/subdirectories",       SND_REC, CM_INV },
  1246. #endif /* RECURSIVE */
  1247.     { "/tenex",                SND_TEN, 0 },
  1248.     { "/text",                 SND_TXT, 0 },
  1249. #ifndef NOCSETS
  1250.     { "/transparent",          SND_XPA, 0 },
  1251. #endif /* NOCSETS */
  1252.     { "/type",                 SND_TYP, CM_ARG },
  1253. #ifdef DOUPDATE
  1254.     { "/update",               SND_UPD, 0 },
  1255. #endif /* DOUPDATE */
  1256.     { "/unique-server-names",  SND_USN, 0 },
  1257.     { "", 0, 0 }
  1258. };
  1259. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1260.  
  1261. static struct keytab getswi[] = {    /* FTP GET switch table */
  1262.     { "/after",                SND_AFT, CM_INV },
  1263.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1264.     { "/as-name",              SND_ASN, CM_ARG },
  1265.     { "/ascii",                SND_TXT, CM_INV },
  1266.     { "/before",               SND_BEF, CM_INV },
  1267.     { "/binary",               SND_BIN, 0 },
  1268.     { "/collision",            SND_COL, CM_ARG },
  1269. #ifdef PUTPIPE
  1270.     { "/command",              SND_CMD, CM_PSH },
  1271. #endif /* PUTPIPE */
  1272.     { "/delete",               SND_DEL, 0 },
  1273.     { "/error-action",         SND_ERR, CM_ARG },
  1274.     { "/except",               SND_EXC, CM_ARG },
  1275.     { "/filenames",            SND_NAM, CM_ARG },
  1276. #ifdef PIPESEND
  1277. #ifndef NOSPL
  1278.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1279. #endif /* NOSPL */
  1280. #endif /* PIPESEND */
  1281. #ifdef VMS
  1282.     { "/image",                SND_IMG, 0 },
  1283. #else
  1284.     { "/image",                SND_BIN, CM_INV },
  1285. #endif /* VMS */
  1286.     { "/larger-than",          SND_LAR, CM_ARG },
  1287.     { "/listfile",             SND_FIL, CM_ARG },
  1288. #ifndef NOCSETS
  1289.     { "/local-character-set",  SND_CSL, CM_ARG },
  1290. #endif /* NOCSETS */
  1291. #ifdef CK_TMPDIR
  1292.     { "/move-to",              SND_MOV, CM_ARG },
  1293. #endif /* CK_TMPDIR */
  1294.     { "/namelist",             SND_NML, CM_ARG },
  1295.     { "/nobackupfiles",        SND_NOB, 0 },
  1296. #ifdef UNIXOROSK
  1297.     { "/nodotfiles",           SND_NOD, 0 },
  1298. #endif /* UNIXOROSK */
  1299.     { "/not-after",            SND_NAF, CM_INV },
  1300.     { "/not-before",           SND_NBE, CM_INV },
  1301.     { "/permissions",          SND_PRM, CM_INV },
  1302.     { "/quiet",                SND_SHH, 0 },
  1303. #ifdef FTP_RESTART
  1304.     { "/recover",              SND_RES, 0 },
  1305. #endif /* FTP_RESTART */
  1306. #ifdef RECURSIVE
  1307.     { "/recursive",            SND_REC, 0 },
  1308. #endif /* RECURSIVE */
  1309.     { "/rename-to",            SND_REN, CM_ARG },
  1310. #ifdef FTP_RESTART
  1311.     { "/restart",              SND_RES, CM_INV },
  1312. #endif /* FTP_RESTART */
  1313. #ifndef NOCSETS
  1314.     { "/server-character-set", SND_CSR, CM_ARG },
  1315. #endif /* NOCSETS */
  1316.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1317.     { "/smaller-than",         SND_SMA, CM_ARG },
  1318. #ifdef RECURSIVE
  1319.     { "/subdirectories",       SND_REC, CM_INV },
  1320. #endif /* RECURSIVE */
  1321.     { "/text",                 SND_TXT, 0 },
  1322.     { "/tenex",                SND_TEN, 0 },
  1323. #ifndef NOCSETS
  1324.     { "/transparent",          SND_XPA, 0 },
  1325. #endif /* NOCSETS */
  1326.     { "/to-screen",            SND_MAI, 0 },
  1327. #ifdef DOUPDATE
  1328.     { "/update",               SND_UPD, CM_INV },
  1329. #endif /* DOUPDATE */
  1330.     { "", 0, 0 }
  1331. };
  1332. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1333.  
  1334. static struct keytab delswi[] = {    /* FTP [M]DELETE switch table */
  1335.     { "/error-action",         SND_ERR, CM_ARG },
  1336.     { "/except",               SND_EXC, CM_ARG },
  1337.     { "/filenames",            SND_NAM, CM_ARG },
  1338.     { "/larger-than",          SND_LAR, CM_ARG },
  1339.     { "/nobackupfiles",        SND_NOB, 0 },
  1340. #ifdef UNIXOROSK
  1341.     { "/nodotfiles",           SND_NOD, 0 },
  1342. #endif /* UNIXOROSK */
  1343.     { "/quiet",                SND_SHH, 0 },
  1344. #ifdef RECURSIVE
  1345.     { "/recursive",            SND_REC, 0 },
  1346. #endif /* RECURSIVE */
  1347.     { "/smaller-than",         SND_SMA, CM_ARG },
  1348. #ifdef RECURSIVE
  1349.     { "/subdirectories",       SND_REC, CM_INV },
  1350. #endif /* RECURSIVE */
  1351.     { "", 0, 0 }
  1352. };
  1353. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1354.  
  1355. static struct keytab fntab[] = {    /* Filename conversion keyword table */
  1356.     { "automatic",    2, 0 },
  1357.     { "converted",    1, 0 },
  1358.     { "literal",      0, 0 }
  1359. };
  1360. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1361.  
  1362. static struct keytab ftptyp[] = {    /* SET FTP TYPE table */
  1363.     { "ascii",        FTT_ASC, 0 },
  1364.     { "binary",       FTT_BIN, 0 },
  1365.     { "tenex",        FTT_TEN, 0 },
  1366.     { "text",         FTT_ASC, CM_INV },
  1367.     { "", 0, 0 }
  1368. };
  1369. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1370.  
  1371. #ifdef FTP_SECURITY
  1372. static struct keytab ftppro[] = {    /* SET FTP PROTECTION-LEVEL table */
  1373.     { "clear",        FPL_CLR, 0 },
  1374.     { "confidential", FPL_CON, 0 },
  1375.     { "private",      FPL_PRV, 0 },
  1376.     { "safe",         FPL_SAF, 0 },
  1377.     { "", 0, 0 }
  1378. };
  1379. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1380. #endif /* FTP_SECURITY */
  1381.  
  1382. /* Definitions for FTP from RFC765. */
  1383.  
  1384. /* Reply codes */
  1385.  
  1386. #define REPLY_PRELIM    1        /* Positive preliminary */
  1387. #define REPLY_COMPLETE  2        /* Positive completion */
  1388. #define REPLY_CONTINUE  3        /* Positive intermediate */
  1389. #define REPLY_TRANSIENT 4        /* Transient negative completion */
  1390. #define REPLY_ERROR     5        /* Permanent negative completion */
  1391. #define REPLY_SECURE    6               /* Security encoded message */
  1392.  
  1393. /* Form codes and names */
  1394.  
  1395. #define FORM_N 1            /* Non-print */
  1396. #define FORM_T 2            /* Telnet format effectors */
  1397. #define FORM_C 3            /* Carriage control (ASA) */
  1398.  
  1399. /* Structure codes and names */
  1400.  
  1401. #define STRU_F 1            /* File (no record structure) */
  1402. #define STRU_R 2            /* Record structure */
  1403. #define STRU_P 3            /* Page structure */
  1404.  
  1405. /* Mode types and names */
  1406.  
  1407. #define MODE_S 1            /* Stream */
  1408. #define MODE_B 2            /* Block */
  1409. #define MODE_C 3            /* Compressed */
  1410.  
  1411. /* Protection levels and names */
  1412.  
  1413. #define PROT_C 1            /* Clear */
  1414. #define PROT_S 2            /* Safe */
  1415. #define PROT_P 3            /* Private */
  1416. #define PROT_E 4            /* Confidential */
  1417.  
  1418. #ifdef COMMENT                /* Not used */
  1419. #ifdef FTP_NAMES
  1420. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1421. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1422. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1423. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1424. #endif /* FTP_NAMES */
  1425.  
  1426. /* Record Tokens */
  1427.  
  1428. #define REC_ESC '\377'            /* Record-mode Escape */
  1429. #define REC_EOR '\001'            /* Record-mode End-of-Record */
  1430. #define REC_EOF '\002'            /* Record-mode End-of-File */
  1431.  
  1432. /* Block Header */
  1433.  
  1434. #define BLK_EOR           0x80        /* Block is End-of-Record */
  1435. #define BLK_EOF           0x40        /* Block is End-of-File */
  1436. #define BLK_REPLY_ERRORS  0x20        /* Block might have errors */
  1437. #define BLK_RESTART       0x10        /* Block is Restart Marker */
  1438. #define BLK_BYTECOUNT 2            /* Bytes in this block */
  1439. #endif /* COMMENT */
  1440.  
  1441. #define RADIX_ENCODE 0            /* radix_encode() function codes */
  1442. #define RADIX_DECODE 1
  1443.  
  1444. /*
  1445.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1446.   results in a serious performance degradation due to the increased number
  1447.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1448.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1449.   of the typical TCP window.  Maybe we should add a command to allow the value
  1450.   to be changed.
  1451. */
  1452. #define DEFAULT_PBSZ 1<<13
  1453.  
  1454. /* Definitions and typedefs needed for prototypes */
  1455.  
  1456. #define sig_t my_sig_t
  1457. #define sigtype SIGTYP
  1458. typedef sigtype (*sig_t)();
  1459.  
  1460. /* Prototypes */
  1461.  
  1462. _PROTOTYP(int remtxt, (char **) );
  1463. _PROTOTYP(char * gskreason, (int) );
  1464. _PROTOTYP(static int ftpclose,(void));
  1465. _PROTOTYP(static int zzsend, (int, CHAR));
  1466. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1467. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1468. _PROTOTYP(static int setpbsz,(unsigned int));
  1469. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1470.   int,int,char *,int,int,int));
  1471. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1472. _PROTOTYP(static int fts_cpl,(int));
  1473. _PROTOTYP(static int fts_dpl,(int));
  1474. #ifdef FTP_SECURITY
  1475. _PROTOTYP(static int ftp_auth, (void));
  1476. #endif /* FTP_SECURITY */
  1477. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1478. _PROTOTYP(static int ftp_login, (char *));
  1479. _PROTOTYP(static int ftp_reset, (void));
  1480. _PROTOTYP(static int ftp_rename, (char *, char *));
  1481. _PROTOTYP(static int ftp_umask, (char *));
  1482. _PROTOTYP(static int secure_flush, (int));
  1483. #ifdef COMMENT
  1484. _PROTOTYP(static int secure_putc, (char, int));
  1485. #endif /* COMMENT */
  1486. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1487. _PROTOTYP(static int scommand, (char *));
  1488. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1489. _PROTOTYP(static int secure_getc, (int));
  1490. _PROTOTYP(static int secure_read, (int, char *, int));
  1491. _PROTOTYP(static int initconn, (void));
  1492. _PROTOTYP(static int dataconn, (char *));
  1493. _PROTOTYP(static int setprotbuf,(unsigned int));
  1494. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1495.  
  1496. _PROTOTYP(static char * radix_error,(int));
  1497. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1498. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, int));
  1499.  
  1500. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1501. _PROTOTYP(static VOID lostpeer, (void));
  1502. _PROTOTYP(static VOID cancel_remote, (int));
  1503. _PROTOTYP(static VOID changetype, (int, int));
  1504.  
  1505. _PROTOTYP(static sigtype cmdcancel, (int));
  1506.  
  1507. #ifdef FTP_SRP
  1508. _PROTOTYP(static int srp_reset, ());
  1509. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1510. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1511. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1512. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1513. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1514. _PROTOTYP(static int srp_selcipher, (char *));
  1515. _PROTOTYP(static int srp_selhash, (char *));
  1516. #endif /* FTP_SRP */
  1517.  
  1518. #ifdef FTP_GSSAPI
  1519. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1520. #endif /* FTP_GSSAPI */
  1521.  
  1522. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1523.  
  1524. #ifdef FTP_SECURITY
  1525. #ifndef NOICP
  1526. #define FT_NOGSS   1
  1527. #define FT_NOK4    2
  1528. #define FT_NOSRP   3
  1529. #define FT_NOSSL   4
  1530. #define FT_NOTLS   5
  1531. #define FT_CERTFI  6
  1532. #define FT_OKCERT  7
  1533. #define FT_DEBUG   8
  1534. #define FT_KEY     9
  1535. #define FT_SECURE 10
  1536. #define FT_VERIFY 11
  1537.  
  1538. static struct keytab ftpztab[] = {
  1539.     { "!gss",    FT_NOGSS,  0 },
  1540.     { "!krb4",   FT_NOK4,   0 },
  1541.     { "!srp",    FT_NOSRP,  0 },
  1542.     { "!ssl",    FT_NOSSL,  0 },
  1543.     { "!tls",    FT_NOTLS,  0 },
  1544.     { "cert",    FT_CERTFI, CM_ARG },
  1545.     { "certsok", FT_OKCERT, 0 },
  1546.     { "debug",   FT_DEBUG,  0 },
  1547.     { "key",     FT_KEY,    CM_ARG },
  1548.     { "nogss",   FT_NOGSS,  0 },
  1549.     { "nokrb4",  FT_NOK4,   0 },
  1550.     { "nosrp",   FT_NOSRP,  0 },
  1551.     { "nossl",   FT_NOSSL,  0 },
  1552.     { "notls",   FT_NOTLS,  0 },
  1553. #ifdef COMMENT
  1554.     { "secure",  FT_SECURE, 0 },
  1555. #endif /* COMMENT */
  1556.     { "verify",  FT_VERIFY, CM_ARG },
  1557.     { "", 0, 0 }
  1558. };
  1559. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1560.  
  1561. /*
  1562.   The following cipher and hash tables should be replaced with
  1563.   dynamicly created versions based upon the linked library.
  1564. */
  1565. #define SRP_BLOWFISH_ECB    1
  1566. #define SRP_BLOWFISH_CBC    2
  1567. #define SRP_BLOWFISH_CFB64  3
  1568. #define SRP_BLOWFISH_OFB64  4
  1569. #define SRP_CAST5_ECB       5
  1570. #define SRP_CAST5_CBC       6
  1571. #define SRP_CAST5_CFB64     7
  1572. #define SRP_CAST5_OFB64     8
  1573. #define SRP_DES_ECB         9
  1574. #define SRP_DES_CBC        10
  1575. #define SRP_DES_CFB64      11
  1576. #define SRP_DES_OFB64      12
  1577. #define SRP_DES3_ECB       13
  1578. #define SRP_DES3_CBC       14
  1579. #define SRP_DES3_CFB64     15
  1580. #define SRP_DES3_OFB64     16
  1581.  
  1582. static struct keytab ciphertab[] = {
  1583.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1584.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1585.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1586.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1587.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1588.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1589.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1590.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1591.     { "des_ecb",        SRP_DES_ECB,        0 },
  1592.     { "des_cbc",        SRP_DES_CBC,        0 },
  1593.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1594.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1595.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1596.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1597.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1598.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1599.     { "none",           0, 0 },
  1600.     { "", 0, 0 }
  1601. };
  1602. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1603.  
  1604. #define SRP_MD5  1
  1605. #define SRP_SHA  2
  1606. static struct keytab hashtab[] = {
  1607.     { "md5",              SRP_MD5,        0 },
  1608.     { "none",             0,              0 },
  1609.     { "sha",              SRP_SHA,        0 },
  1610.     { "", 0, 0 }
  1611. };
  1612. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1613. #endif /* NOICP */
  1614. #endif /* FTP_SECURITY */
  1615.  
  1616. static char *
  1617. strval(s1,s2) char * s1, * s2; {
  1618.     if (!s1) s1 = "";
  1619.     if (!s2) s2 = "";
  1620.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1621. }
  1622.  
  1623. #ifndef NOCSETS
  1624. static char * rfnptr = NULL;
  1625. static int rfnlen = 0;
  1626. static char rfnbuf[RFNBUFSIZ];        /* Remote filename translate buffer */
  1627. static char * xgnbp = NULL;
  1628.  
  1629. static int
  1630. strgetc() {                /* Helper function for xgnbyte() */
  1631.     int c;
  1632.     if (!xgnbp)
  1633.       return(-1);
  1634.     if (!*xgnbp)
  1635.       return(-1);
  1636.     c = (unsigned) *xgnbp++;
  1637.     return(((unsigned) c) & 0xff);
  1638. }
  1639.  
  1640. static int                /* Helper function for xpnbyte() */
  1641. #ifdef CK_ANSIC
  1642. strputc(char c)
  1643. #else
  1644. strputc(c) char c;
  1645. #endif /* CK_ANSIC */
  1646. {
  1647.     rfnlen = rfnptr - rfnbuf;
  1648.     if (rfnlen >= (RFNBUFSIZ - 1))
  1649.       return(-1);
  1650.     *rfnptr++ = c;
  1651.     *rfnptr = NUL;
  1652.     return(0);
  1653. }
  1654.  
  1655. static int
  1656. #ifdef CK_ANSIC
  1657. xprintc(char c)
  1658. #else
  1659. xprintc(c) char c;
  1660. #endif /* CK_ANSIC */
  1661. {
  1662.     printf("%c",c);
  1663.     return(0);
  1664. }
  1665. #endif /* NOCSETS */
  1666.  
  1667.  
  1668. #ifdef CKLOGDIAL
  1669. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  1670. int ftplogactive = 0;
  1671. long ftplogprev = 0L;
  1672.  
  1673. VOID
  1674. ftplogend() {
  1675.     extern int dialog;
  1676.     extern char diafil[];
  1677.     long d1, d2, t1, t2;
  1678.     char buf[32], * p;
  1679.  
  1680.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  1681.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  1682.  
  1683.     if (!ftplogactive || !ftplogbuf[0])    /* No active record */
  1684.       return;
  1685.  
  1686.     ftplogactive = 0;            /* Record is not active */
  1687.  
  1688.     d1 = mjd((char *)ftplogbuf);    /* Get start date of this session */
  1689.     ckstrncpy(buf,ckdate(),31);        /* Get current date */
  1690.     d2 = mjd(buf);            /* Convert them to mjds */
  1691.     p = ftplogbuf;            /* Get start time */
  1692.     p[11] = NUL;
  1693.     p[14] = NUL;            /* Convert to seconds */
  1694.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1695.     p[11] = ':';
  1696.     p[14] = ':';
  1697.     p = buf;                /* Get end time */
  1698.     p[11] = NUL;
  1699.     p[14] = NUL;
  1700.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1701.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  1702.     if (t2 > -1L) {
  1703.     ftplogprev = t2;
  1704.     p = hhmmss(t2);
  1705.     strncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  1706.     strncat(ftplogbuf,p,CXLOGBUFL);
  1707.     } else
  1708.       ftplogprev = 0L;
  1709.     debug(F101,"ftp cx log dialog","",dialog);
  1710.     if (dialog) {            /* If logging */
  1711.     int x;
  1712.     x = diaopn(diafil,1,1);        /* Open log in append mode */
  1713.     if (x > 0) {
  1714.         debug(F101,"ftp cx log open","",x);
  1715.         x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  1716.         debug(F101,"ftp cx log write","",x);
  1717.         x = zclose(ZDIFIL);        /* Close the log */
  1718.         debug(F101,"ftp cx log close","",x);
  1719.     }
  1720.     }
  1721. }
  1722.  
  1723. VOID
  1724. dologftp() {
  1725.     ftplogend();            /* Previous session not closed out? */
  1726.     ftplogprev = 0L;
  1727.     ftplogactive = 1;            /* Record is active */
  1728.  
  1729.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  1730.           ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  1731.           " T=FTP N=", strval(ftp_host,NULL)," H=",myhost," ",NULL,NULL);
  1732.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  1733. }
  1734. #endif /* CKLOGDIAL */
  1735.  
  1736. static char * dummy[2] = { NULL, NULL };
  1737.  
  1738. static struct keytab modetab[] = {
  1739.     { "active",  0, 0 },
  1740.     { "passive", 1, 0 }
  1741. };
  1742.  
  1743. #ifndef NOCMDL
  1744. int                    /* Called from ckuusy.c */
  1745. #ifdef CK_ANSIC
  1746. doftparg(char c)
  1747. #else
  1748. doftparg(c) char c;
  1749. #endif /* CK_ANSIC */
  1750. /* doftparg */ {
  1751.     int x, z;
  1752.     char *xp;
  1753.     extern char **xargv, *xarg0;
  1754.     extern int xargc, stayflg, haveftpuid;
  1755.     extern char uidbuf[];
  1756.  
  1757.     xp = *xargv+1;                      /* Pointer for bundled args */
  1758.     while (c) {
  1759.     if (ckstrchr("MuDPkcHzm",c)) {    /* Options that take arguments */
  1760.         if (*(xp+1)) {
  1761.         fatal("?Invalid argument bundling");
  1762.         }
  1763.         xargv++, xargc--;
  1764.         if ((xargc < 1) || (**xargv == '-')) {
  1765.         fatal("?Required argument missing");
  1766.         }
  1767.     }
  1768.         switch (c) {                    /* Big switch on arg */
  1769.       case 'h':            /* help */
  1770.        printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  1771.         printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  1772.         printf("Options:\n");
  1773.             printf("  -h           = help (this message)\n");
  1774.         printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  1775.         printf("  -u name      = username for autologin (or -M)\n");
  1776.         printf("  -P password  = password for autologin (RISKY)\n");
  1777.             printf("  -A           = autologin anonymously\n");
  1778.         printf("  -D directory = cd after autologin\n");
  1779.         printf("  -b           = force binary mode\n");
  1780.         printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  1781.         printf("  -d           = debug (double to add timestamps)\n");
  1782.         printf("  -n           = no autologin\n");
  1783.         printf("  -v           = verbose (default)\n");
  1784.         printf("  -q           = quiet\n");
  1785.         printf("  -S           = Stay (issue command prompt when done)\n");
  1786.         printf("  -Y           = do not execute Kermit init file\n");
  1787.         printf("  -p files     = files to put after autologin (or -s)\n");
  1788.         printf("  -g files     = files to get after autologin\n");
  1789.         printf("  -R           = recursive (for use with -p)\n");
  1790.  
  1791. #ifdef FTP_SECURITY
  1792.         printf("\nSecurity options:\n");
  1793.         printf("  -k realm     = Kerberos 4 realm\n");
  1794.         printf("  -f           = Kerboros 5 credentials forwarding\n");
  1795.         printf("  -x           = autoencryption mode\n");
  1796.         printf("  -c cipher    = SRP cipher type\n");
  1797.         printf("  -H hash      = SRP encryption hash\n");
  1798.             printf("  -z option    = Security options\n");
  1799. #endif /* FTP_SECURITY */
  1800.  
  1801.         printf("\n-p or -g, if given, should be last.  Example:\n");
  1802.         printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  1803.  
  1804.         doexit(GOOD_EXIT,-1);
  1805.         break;
  1806.  
  1807.       case 'R':            /* Recursive */
  1808.         recursive = 1;
  1809.         break;
  1810.  
  1811.       case 'd':            /* Debug */
  1812. #ifdef DEBUG
  1813.         if (deblog) {
  1814.         extern int debtim;
  1815.         debtim = 1;
  1816.         } else {
  1817.         deblog = debopn("debug.log",0);
  1818.         debok = 1;
  1819.         }
  1820. #endif /* DEBUG */
  1821.         /* fall thru on purpose */
  1822.  
  1823.       case 't':            /* Trace */
  1824.         ftp_deb++;
  1825.         break;
  1826.  
  1827.       case 'n':            /* No autologin */
  1828.         ftp_log = 0;
  1829.         break;
  1830.  
  1831.       case 'i':            /* No prompt */
  1832.       case 'v':            /* Verbose */
  1833.         break;            /* (ignored) */
  1834.  
  1835.       case 'q':            /* Quiet */
  1836.         quiet = 1;
  1837.         break;
  1838.  
  1839.       case 'S':            /* Stay */
  1840.         stayflg = 1;
  1841.         break;
  1842.  
  1843.       case 'M':
  1844.       case 'u':            /* My User Name */
  1845.         if ((int)strlen(*xargv) > 63) {
  1846.         fatal("username too long");
  1847.         }
  1848.         ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  1849.         haveftpuid = 1;
  1850.         break;
  1851.  
  1852.       case 'A':
  1853.         ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  1854.         haveftpuid = 1;
  1855.         break;
  1856.  
  1857.       case 'T':            /* Text */
  1858.       case 'a':            /* "ascii" */
  1859.       case 'b':            /* Binary */
  1860.         binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  1861.         xfermode = XMODE_M;
  1862.         filepeek = 0;
  1863.         patterns = 0;
  1864.         break;
  1865.  
  1866.       case 'g':            /* Get */
  1867.       case 'p':            /* Put */
  1868.       case 's': {            /* Send (= Put) */
  1869.           int havefiles, rc;
  1870.           if (ftp_action) {
  1871.           fatal("Only one FTP action at a time please");
  1872.           }
  1873.           if (*(xp+1)) {
  1874.           fatal("invalid argument bundling after -s");
  1875.           }
  1876.           nfils = 0;        /* Initialize file counter */
  1877.           havefiles = 0;        /* Assume nothing to send  */
  1878.           cmlist = xargv + 1;    /* Remember this pointer */
  1879.  
  1880.           while (++xargv, --xargc > 0) { /* Traverse the list */
  1881.           if (c == 'g') {
  1882.               havefiles++;
  1883.               nfils++;
  1884.               continue;
  1885.           }
  1886. #ifdef RECURSIVE
  1887.           if (!strcmp(*xargv,".")) {
  1888.               havefiles = 1;
  1889.               nfils++;
  1890.               recursive = 1;
  1891.           } else
  1892. #endif /* RECURSIVE */
  1893.             if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  1894.             if  (rc != -2)
  1895.               havefiles = 1;
  1896.             nfils++;
  1897.             } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  1898.             havefiles = 1;
  1899.             nfils++;
  1900.             }
  1901.           }
  1902.           xargc++, xargv--;        /* Adjust argv/argc */
  1903.           if (!havefiles) {
  1904.           if (c == 'g') {
  1905.               fatal("No files to put");
  1906.           } else {
  1907.               fatal("No files to get");
  1908.           }
  1909.           }
  1910.           ftp_action = c;
  1911.           break;
  1912.       }
  1913.       case 'D':            /* Directory */
  1914.         makestr(&ftp_rdir,*xargv);
  1915.         break;
  1916.  
  1917.       case 'm':            /* Mode (Active/Passive */
  1918.         ftp_psv = lookup(modetab,*xargv,2,NULL);
  1919.         if (ftp_psv < 0) fatal("Invalid mode");
  1920.         break;
  1921.  
  1922.       case 'P':
  1923.         makestr(&ftp_tmp,*xargv);    /* You-Know-What */
  1924.         break;
  1925.  
  1926.       case 'Y':            /* No initialization file */
  1927.         break;            /* (already done in prescan) */
  1928.  
  1929. #ifdef CK_URL
  1930.       case 'U': {            /* URL */
  1931.           /* These are set by urlparse() - any not set are NULL */
  1932.               extern struct urldata g_url;
  1933.           if (g_url.hos) {
  1934. /*
  1935.   Kermit has accepted host:port notation since many years before URLs were
  1936.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  1937.   host:449" looks like a URL and results in service = host and host = 449.
  1938.   Here we try to catch this situation transparently to the user.
  1939. */
  1940.           if (ckstrcmp(g_url.svc,"ftp",-1,0)
  1941. #ifdef CK_SSL
  1942.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  1943. #endif /* CK_SSL */
  1944.                        ) {
  1945.               if (!g_url.usr &&
  1946.               !g_url.psw &&
  1947.               !g_url.por &&
  1948.               !g_url.pth) {
  1949.               g_url.por = g_url.hos;
  1950.               g_url.hos = g_url.svc;
  1951.               g_url.svc = "ftp";
  1952.               } else {
  1953.               ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  1954.                    g_url.svc," host=",g_url.hos);
  1955.               fatal(tmpbuf);
  1956.               }
  1957.           }
  1958.           makestr(&ftp_host,g_url.hos);
  1959.           if (g_url.usr) {
  1960.               haveftpuid = 1;
  1961.               ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  1962.               makestr(&ftp_logname,uidbuf);
  1963.           }
  1964.           if (g_url.psw) {
  1965.               makestr(&ftp_tmp,g_url.psw);
  1966.           }
  1967.           if (g_url.pth) {
  1968.               if (!g_url.usr) {
  1969.               haveftpuid = 1;
  1970.               ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  1971.               makestr(&ftp_logname,uidbuf);
  1972.               }
  1973.               if (ftp_action) {
  1974.               fatal("Only one FTP action at a time please");
  1975.               }
  1976.               if (!stayflg)
  1977.             quiet = 1;
  1978.               nfils = 1;
  1979.               dummy[0] = g_url.pth;
  1980.               cmlist = dummy;
  1981.               ftp_action = 'g';
  1982.           }
  1983.           xp = NULL;
  1984.           haveurl = 1;
  1985.           }
  1986.           break;
  1987.       }
  1988. #endif /* CK_URL */
  1989.  
  1990. #ifdef FTP_SECURITY
  1991.       case 'k': {            /* K4 Realm */
  1992. #ifdef FTP_KRB4
  1993.           ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  1994. #endif /* FTP_KRB4 */
  1995.           if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  1996.           break;
  1997.       }
  1998.       case 'f': {
  1999. #ifdef FTP_GSSAPI
  2000.           ftp_cfw = 1;
  2001.           if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2002. #else /* FTP_GSSAPI */
  2003.           printf("K5 Credentials Forwarding not supported\n");
  2004. #endif /* FTP_GSSAPI */
  2005.           break;
  2006.       }
  2007.       case 'x': {
  2008.           ftp_cry = 1;
  2009.           if (ftp_deb) printf("Autoencryption\n");
  2010.           break;
  2011.       }
  2012.       case 'c': {            /* Cipher */
  2013. #ifdef FTP_SRP
  2014.               if (!srp_selcipher(*xargv)) {
  2015.           if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2016.           } else
  2017.         printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2018. #else /* FTP_SRP */
  2019.               printf("?SRP not supported\n");
  2020. #endif /* FTP_SRP */
  2021.           break;
  2022.       }
  2023.       case 'H': {
  2024. #ifdef FTP_SRP
  2025.               if (!srp_selhash(*xargv)) {
  2026.           if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2027.           } else
  2028.         printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2029. #else /* FTP_SRP */
  2030.               printf("?SRP not supported\n");
  2031. #endif /* FTP_SRP */
  2032.           break;
  2033.       }
  2034.       case 'z': {
  2035.               /* *xargv contains a value of the form tag=value */
  2036.               /* we need to lookup the tag and save the value  */
  2037.               char * p, * q;
  2038.               makestr(&p,*xargv);
  2039.               y = ckindex("=",p,0,0,1);
  2040.               if (y > 0)
  2041.         p[y-1] = '\0';
  2042.               x = lookup(ftpztab,p,nftpztab,&z);
  2043.           if (x < 0) {
  2044.           printf("?Invalid security option: \"%s\"\n",p);
  2045.           } else {
  2046.           printf("Security option: \"%s",p);
  2047.           if (ftpztab[z].flgs & CM_ARG) {
  2048.                       q = &p[y];
  2049.               if (!*q)
  2050.             fatal("?Missing required value");
  2051.                       if (ftp_deb) printf("=%s\"",q);
  2052.                   }
  2053.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2054.             case FT_NOGSS:
  2055. #ifdef FTP_GSSAPI
  2056.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2057.                           if (ftp_auth_type[z] == FTA_GK5) {
  2058.                               for (y = z;
  2059.                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2060.                    y++
  2061.                    )
  2062.                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2063.                               ftp_auth_type[FTPATYPS-1] = 0;
  2064.                               break;
  2065.                           }
  2066.                       }
  2067. #endif /* FTP_GSSAPI */
  2068.                       break;
  2069.             case FT_NOK4:
  2070. #ifdef FTP_KRB4
  2071.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2072.                           if (ftp_auth_type[z] == FTA_K4) {
  2073.                               for (y = z;
  2074.                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2075.                    y++
  2076.                    )
  2077.                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2078.                               ftp_auth_type[FTPATYPS-1] = 0;
  2079.                               break;
  2080.                           }
  2081.                       }
  2082. #endif /* FTP_KRB4 */
  2083.               break;
  2084.             case FT_NOSRP:
  2085. #ifdef FTP_SRP
  2086.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2087.                           if (ftp_auth_type[z] == FTA_SRP) {
  2088.                               for (y = z;
  2089.                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2090.                    y++
  2091.                    )
  2092.                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2093.                               ftp_auth_type[FTPATYPS-1] = 0;
  2094.                               break;
  2095.                           }
  2096.                       }
  2097. #endif /* FTP_SRP */
  2098.               break;
  2099.             case FT_NOSSL:
  2100. #ifdef CK_SSL
  2101.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2102.                           if (ftp_auth_type[z] == FTA_SSL) {
  2103.                               for (y = z;
  2104.                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2105.                    y++
  2106.                    )
  2107.                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2108.                               ftp_auth_type[FTPATYPS-1] = 0;
  2109.                               break;
  2110.                           }
  2111.                       }
  2112. #endif /* CK_SSL */
  2113.               break;
  2114.             case FT_NOTLS:
  2115. #ifdef CK_SSL
  2116.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2117.                           if (ftp_auth_type[z] == FTA_TLS) {
  2118.                               for (y = z;
  2119.                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2120.                    y++
  2121.                    )
  2122.                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2123.                               ftp_auth_type[FTPATYPS-1] = 0;
  2124.                               break;
  2125.                           }
  2126.                       }
  2127. #endif /* CK_SSL */
  2128.               break;
  2129.             case FT_CERTFI:
  2130. #ifdef CK_SSL
  2131.                       makestr(&ssl_rsa_cert_file,q);
  2132. #endif /* CK_SSL */
  2133.                       break;
  2134.             case FT_OKCERT:
  2135. #ifdef CK_SSL
  2136.               ssl_certsok_flag = 1;
  2137. #endif /* CK_SSL */
  2138.               break;
  2139.             case FT_DEBUG:
  2140. #ifdef DEBUG
  2141.                       if (deblog) {
  2142.                           extern int debtim;
  2143.                           debtim = 1;
  2144.                       } else {
  2145.                           deblog = debopn("debug.log",0);
  2146.                       }
  2147. #endif /* DEBUG */
  2148.                       break;
  2149.             case FT_KEY:
  2150. #ifdef CK_SSL
  2151.                       makestr(&ssl_rsa_key_file,q);
  2152. #endif /* CK_SSL */
  2153.                       break;
  2154.             case FT_SECURE:
  2155.               /* no equivalent */
  2156.               break;
  2157.             case FT_VERIFY:
  2158. #ifdef CK_SSL
  2159.                       if (!rdigits(q))
  2160.             printf("?Bad number: %s\n",q);
  2161.                       ssl_verify_flag = atoi(q);
  2162. #endif /* CK_SSL */
  2163.                       break;
  2164.                   }
  2165.               }
  2166.               if (ftp_deb) printf("\"\n");
  2167.               free(p);
  2168.           break;
  2169.       }
  2170. #endif /* FTP_SECURITY */
  2171.  
  2172.       default:
  2173.         fatal2(*xargv,
  2174.            "unknown command-line option, type \"ftp -h\" for help"
  2175.            );
  2176.     }
  2177.     if (!xp) break;
  2178.     c = *++xp;            /* See if options are bundled */
  2179.     }
  2180.     return(0);
  2181. }
  2182. #endif /* NOCMDL */
  2183.  
  2184. int
  2185. ftpisconnected() {
  2186.     return(connected);
  2187. }
  2188.  
  2189. int
  2190. ftpisloggedin() {
  2191.     return(connected ? loggedin : 0);
  2192. }
  2193.  
  2194. int
  2195. ftpissecure() {
  2196.     return((ftp_dpl == FPL_CLR) ? 0 : 1);
  2197. }
  2198.  
  2199. static VOID
  2200. ftscreen(n, c, z, s) int n; char c; long z; char * s; {
  2201.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2202.     if (!dpyactive) {
  2203.         ckscreen(SCR_PT,'S',0L,"");
  2204.         dpyactive = 1;
  2205.     }
  2206.     ckscreen(n,c,z,s);
  2207.     }
  2208. }
  2209.  
  2210. #ifndef OS2
  2211. /*  g m s t i m e r  --  Millisecond timer */
  2212.  
  2213. long
  2214. gmstimer() {
  2215. #ifdef HAVE_MSECS
  2216.     /* For those versions of ztime() that also set global ztmsec. */
  2217.     char *p = NULL;
  2218.     long z;
  2219.     ztime(&p);
  2220.     if (!p) return(0L);
  2221.     if (!*p) return(0L);
  2222.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2223.     return(z * 1000 + ztmsec);
  2224. #else
  2225.     return((long)time(NULL) * 1000L);
  2226. #endif /* HAVE_MSECS */
  2227. }
  2228. #endif /* OS2 */
  2229.  
  2230. /*  d o s e t f t p  --  The SET FTP command  */
  2231.  
  2232. int
  2233. dosetftp() {
  2234.     int cx;
  2235.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2236.       return(cx);
  2237.     switch (cx) {
  2238.  
  2239.       case FTS_FNC:            /* Filename collision action */
  2240.     if ((x = cmkey(colxtab,ncolx,"","",xxstring)) < 0)
  2241.       return(x);
  2242.     if ((y = cmcfm()) < 0)
  2243.       return(y);
  2244.     ftp_fnc = x;
  2245.     return(1);
  2246.  
  2247.       case FTS_CNV:            /* Filename conversion */
  2248.     if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2249.       return(x);
  2250.     if ((y = cmcfm()) < 0)
  2251.       return(y);
  2252.     ftp_cnv = x;
  2253.     return(1);
  2254.  
  2255.       case FTS_DBG:            /* Debug messages */
  2256.     return(seton(&ftp_deb));
  2257.  
  2258.       case FTS_LOG:            /* Auto-login */
  2259.     return(seton(&ftp_log));
  2260.  
  2261.       case FTS_PSV:            /* Passive mode */
  2262.     x = seton(&ftp_psv);
  2263.     if (x) passivemode = ftp_psv;
  2264.     return(x);
  2265.  
  2266.       case FTS_SPC:            /* Send port commands */
  2267.     x = seton(&ftp_spc);
  2268.         if (x) sendport = ftp_spc;
  2269.         return(x);
  2270.  
  2271.       case FTS_TYP:            /* Type */
  2272.     if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2273.       return(x);
  2274.     if ((y = cmcfm()) < 0) return(y);
  2275.     ftp_typ = x;
  2276.     g_ftp_typ = x;
  2277.     tenex = (ftp_typ == FTT_TEN);
  2278.     return(1);
  2279.  
  2280.       case FTS_USN:            /* Unique server names */
  2281.     return(seton(&ftp_usn));
  2282.  
  2283.       case FTS_VBM:            /* Verbose mode */
  2284.     if ((x = seton(&ftp_vbm)) < 0)    /* Per-command copy */
  2285.       return(x);
  2286.     ftp_vbx = ftp_vbm;        /* Global sticky copy */
  2287.     return(x);
  2288.  
  2289.       case FTS_TST:            /* "if (testing)" messages */
  2290.     return(seton(&testing));
  2291.  
  2292.       case FTS_PRM:            /* Send permissions */
  2293.     return(setonaut(&ftp_prm));
  2294.  
  2295.       case FTS_AUT:            /* Auto-authentication */
  2296.     return(seton(&ftp_aut));
  2297.  
  2298.       case FTS_ERR:            /* Error action */
  2299.     if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2300.       return(x);
  2301.     if ((y = cmcfm()) < 0)
  2302.       return(y);
  2303.     ftp_err = x;
  2304.         return(success = 1);
  2305.  
  2306. #ifndef NOCSETS
  2307.       case FTS_XLA:            /* Translation */
  2308.     return(seton(&ftp_xla));
  2309.  
  2310.       case FTS_CSR:            /* Server charset */
  2311.     if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2312.       return(x);
  2313.     if ((y = cmcfm()) < 0)
  2314.       return(y);
  2315.     ftp_csr = x;
  2316.     ftp_xla = 1;            /* Also enable translation */
  2317.         return(success = 1);
  2318. #endif /* NOCSETS */
  2319.  
  2320.       case FTS_GFT:
  2321.     return(seton(&get_auto));    /* GET-filetype-switching */
  2322.  
  2323.       case FTS_DAT:
  2324.     return(seton(&ftp_dates));    /* Set file dates */
  2325.  
  2326. #ifdef FTP_SECURITY
  2327.       case FTS_CRY:            /* Auto-encryption */
  2328.     return(seton(&ftp_cry));
  2329.  
  2330.       case FTS_CFW:            /* Credential-forwarding */
  2331.     return(seton(&ftp_cfw));
  2332.  
  2333.       case FTS_CPL:            /* Command protection level */
  2334.     if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2335.     if ((y = cmcfm()) < 0) return(y);
  2336.         success = fts_cpl(x);
  2337.         return(success);
  2338.  
  2339.       case FTS_DPL:            /* Data protection level */
  2340.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2341.         if ((y = cmcfm()) < 0) return(y);
  2342.           success = fts_dpl(x);
  2343.           return(success);
  2344.  
  2345.       case FTS_ATP: {                   /* FTP Auth Type */
  2346.       int i, j, atypes[8];
  2347.  
  2348.       for (i = 0; i < 8; i++) {
  2349.           if ((y = cmkey(ftpauth,nftpauth,"",
  2350.                  (i == 0) ? "automatic" : "",
  2351.                  xxstring)) < 0) {
  2352.           if (y == -3)
  2353.                     break;
  2354.           return(y);
  2355.           }
  2356.           if (i > 0 && (y == FTA_AUTO)) {
  2357.           printf("?Choice may only be used in first position.\r\n");
  2358.           return(-9);
  2359.           }
  2360.           for (j = 0; j < i; j++) {
  2361.           if (atypes[j] == y) {
  2362.               printf("\r\n?Choice has already been used.\r\n");
  2363.               return(-9);
  2364.           }
  2365.           }
  2366.           atypes[i] = y;
  2367.           if (y == FTA_AUTO) {
  2368.           i++;
  2369.           break;
  2370.           }
  2371.       }
  2372.       if (i < 8)
  2373.             atypes[i] = 0;
  2374.       if ((z = cmcfm()) < 0)
  2375.             return(z);
  2376.       if (atypes[0] == FTA_AUTO) {
  2377.           i = 0;
  2378. #ifdef FTP_GSSAPI
  2379.           ftp_auth_type[i++] = FTA_GK5;
  2380. #endif /* FTP_GSSAPI */
  2381. #ifdef FTP_SRP
  2382.           ftp_auth_type[i++] = FTA_SRP;
  2383. #endif /* FTP_SRP */
  2384. #ifdef FTP_KRB4
  2385.           ftp_auth_type[i++] = FTA_K4;
  2386. #endif /* FTP_KRB4 */
  2387. #ifdef CK_SSL
  2388.           ftp_auth_type[i++] = FTA_TLS;
  2389.           ftp_auth_type[i++] = FTA_SSL;
  2390. #endif /* CK_SSL */
  2391.           ftp_auth_type[i] = 0;
  2392.       } else {
  2393.           for (i = 0; i < 8; i++)
  2394.         ftp_auth_type[i] = atypes[i];
  2395.       }
  2396.       return(success = 1);
  2397.       }
  2398.  
  2399.       case FTS_SRP:
  2400. #ifdef FTP_SRP
  2401.     if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2402.       return(x);
  2403.     switch (x) {
  2404.           case SRP_CIPHER:
  2405.         if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2406.           return(x);
  2407.         if ((z = cmcfm()) < 0)
  2408.           return(z);
  2409.         success = !srp_selcipher(ciphertab[x].kwd);
  2410.         return(success);
  2411.           case SRP_HASH:
  2412.         if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2413.           return(x);
  2414.         if ((z = cmcfm()) < 0)
  2415.           return(z);
  2416.         success = !srp_selhash(hashtab[x].kwd);
  2417.         return(success = 1);
  2418.           default:
  2419.         if ((z = cmcfm()) < 0)
  2420.           return(z);
  2421.         return(-2);
  2422.     }
  2423. #else /* FTP_SRP */
  2424.     if ((z = cmcfm()) < 0)
  2425.       return(z);
  2426.     return(-2);
  2427. #endif /* FTP_SRP */
  2428. #endif /* FTP_SECURITY */
  2429.  
  2430.       default:
  2431.     return(-2);
  2432.     }
  2433. }
  2434.  
  2435. int
  2436. ftpbye() {
  2437.     int x;
  2438.     if (!connected)
  2439.       return(1);
  2440.     if (testing)
  2441.       printf(" ftp closing %s...\n",ftp_host);
  2442.     x = ftpclose();
  2443.     return((x > -1) ? 1 : 0);
  2444. }
  2445.  
  2446. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2447.  
  2448. static int
  2449. openftp(s,opn_tls) char * s; int opn_tls; {
  2450.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2451.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1;
  2452.     struct FDB sw, fl, cm;
  2453.     extern int nnetdir;            /* Network services directory */
  2454.     extern int nhcount;            /* Lookup result */
  2455.     extern char *nh_p[];        /* Network directory entry pointers */
  2456.     extern char *nh_p2[];        /* Network directory entry nettype */
  2457.  
  2458.     if (!s) return(-2);
  2459.     if (!*s) return(-2);
  2460.  
  2461.     makestr(&hostname,s);
  2462.     hostsave = hostname;
  2463.     makestr(&ftp_logname,NULL);
  2464.  
  2465.     debug(F110,"ftp open",hostname,0);
  2466.  
  2467.     if (sav_psv > -1) {            /* Restore prevailing active/passive */
  2468.     ftp_psv = sav_psv;        /* selection in case it was */
  2469.     sav_psv = -1;            /* temporarily overriden by a switch */
  2470.     }
  2471.     cmfdbi(&sw,                /* Switches */
  2472.        _CMKEY,
  2473.        "Service name or port;\n or switch",
  2474.        "",                /* default */
  2475.        "",                /* addtl string data */
  2476.        nftpswi,            /* addtl numeric data 1: tbl size */
  2477.        4,                /* addtl numeric data 2: none */
  2478.        xxstring,            /* Processing function */
  2479.        ftpswitab,            /* Keyword table */
  2480.        &fl                /* Pointer to next FDB */
  2481.        );
  2482.     cmfdbi(&fl,                /* A host name or address */
  2483.        _CMFLD,            /* fcode */
  2484.        "",                /* help */
  2485.        "xYzBoo",            /* default */
  2486.        "",                /* addtl string data */
  2487.        0,                /* addtl numeric data 1 */
  2488.        0,                /* addtl numeric data 2 */
  2489.        xxstring,
  2490.        NULL,
  2491.        &cm
  2492.        );
  2493.     cmfdbi(&cm,                /* Command confirmation */
  2494.        _CMCFM,
  2495.        "",
  2496.        "",
  2497.        "",
  2498.        0,
  2499.        0,
  2500.        NULL,
  2501.        NULL,
  2502.        NULL
  2503.        );
  2504.  
  2505.     for (n = 0;; n++) {
  2506.     rc = cmfdb(&sw);        /* Parse a service name or a switch */
  2507.     if (rc < 0)
  2508.       goto xopenftp;
  2509.  
  2510.     if (cmresult.fcode == _CMCFM) {    /* Done? */
  2511.         break;
  2512.     } else if (cmresult.fcode == _CMFLD) {    /* Port */
  2513.         if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2514.           makestr(&service,cmresult.sresult);
  2515.         else 
  2516.           makestr(&service,opn_tls?"ftps":"ftp"); 
  2517.     } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2518.         c = cmgbrk();        /* get break character */
  2519.         getval = (c == ':' || c == '=');
  2520.         rc = -9;
  2521.         if (getval && !(cmresult.kflags & CM_ARG)) {
  2522.         printf("?This switch does not take arguments\n");
  2523.         goto xopenftp;
  2524.         }
  2525.         if (!getval && (cmresult.kflags & CM_ARG)) {
  2526.         printf("?This switch requires an argument\n");
  2527.         goto xopenftp;
  2528.         }
  2529.         switch (cmresult.nresult) {    /* Switch */
  2530.           case OPN_ANO:        /* /ANONYMOUS */
  2531.         anonymous++;
  2532.         break;
  2533.           case OPN_PSW:        /* /PASSWORD */
  2534.         if (!anonymous)        /* Don't log real passwords */
  2535.           debok = 0;
  2536.         rc = cmfld("Password for FTP server","",&p,xxstring);
  2537.         if (rc == -3) {
  2538.             makestr(&ftp_tmp,NULL);
  2539.         } else if (rc < 0) {
  2540.             goto xopenftp;
  2541.         } else {
  2542.             makestr(&ftp_tmp,p);
  2543.         }
  2544.         break;
  2545.           case OPN_USR:        /* /USER */
  2546.         rc = cmfld("Username for FTP server","",&p,xxstring);
  2547.         if (rc == -3) {
  2548.             makestr(&ftp_logname,NULL);
  2549.         } else if (rc < 0) {
  2550.             goto xopenftp;
  2551.         } else {
  2552.             anonymous = 0;
  2553.             makestr(&ftp_logname,p);
  2554.         }
  2555.         break;
  2556.           case OPN_ACC:
  2557.         rc = cmfld("Account for FTP server","",&p,xxstring);
  2558.         if (rc == -3) {
  2559.             makestr(&ftp_acc,NULL);
  2560.         } else if (rc < 0) {
  2561.             goto xopenftp;
  2562.         } else {
  2563.             makestr(&ftp_acc,p);
  2564.         }
  2565.         break;
  2566.           case OPN_ACT:
  2567.         opn_psv = 0;
  2568.         break;
  2569.           case OPN_PSV:
  2570.         opn_psv = 1;
  2571.         break;
  2572.               case OPN_TLS:
  2573.                 opn_tls = 1;
  2574.                 break;
  2575.           default:
  2576.         break;
  2577.         }
  2578.     }
  2579.     if (n == 0) {            /* After first time through */
  2580.         cmfdbi(&sw,            /* accept only switches */
  2581.            _CMKEY,
  2582.            "\nCarriage return to confirm to command, or switch",
  2583.            "",
  2584.            "",
  2585.            nftpswi,
  2586.            4,
  2587.            xxstring,
  2588.            ftpswitab,
  2589.            &cm
  2590.            );
  2591.     }
  2592.     }
  2593. #ifdef COMMENT
  2594.     debug(F100,"ftp openftp while exit","",0);
  2595.     rc = cmcfm();
  2596.     debug(F101,"ftp openftp cmcfm rc","",rc);
  2597.     if (rc < 0)
  2598.       goto xopenftp;
  2599. #endif /* COMMENT */
  2600.  
  2601.     if (opn_psv > -1) {            /* /PASSIVE or /ACTIVE switch given */
  2602.     sav_psv = ftp_psv;
  2603.     ftp_psv = opn_psv;
  2604.     }
  2605.     if (*hostname == '=') {        /* Bypass directory lookup */
  2606.     hostname++;            /* if hostname starts with '=' */
  2607.     havehost++;
  2608.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  2609.     havehost++;
  2610.     }
  2611.  
  2612. #ifndef NODIAL
  2613.     if (!havehost && nnetdir > 0) {    /* If there is a networks directory */
  2614.     lunet(hostname);        /* Look up the name */
  2615.     debug(F111,"ftp openftp lunet",hostname,nhcount);
  2616.     if (nhcount == 0) {
  2617.         if (testing)
  2618.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2619.         success = ftpopen(hostname,service,opn_tls);
  2620.         debug(F101,"ftp openftp A ftpopen success","",success);
  2621.         rc = success;
  2622.     } else {
  2623.         int found = 0;
  2624.         for (i = 0; i < nhcount; i++) {
  2625.         if (nh_p2[i])        /* If network type specified */
  2626.           if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  2627.             continue;
  2628.         found++;
  2629.         makestr(&hostname,nh_p[i]);
  2630.         debug(F111,"ftpopen lunet substitution",hostname,i);
  2631.         if (testing)
  2632.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2633.         success = ftpopen(hostname,service,opn_tls);
  2634.         debug(F101,"ftp openftp B ftpopen success","",success);
  2635.         rc = success;
  2636.         if (success)
  2637.           break;
  2638.         }
  2639.         if (!found) {        /* E.g. if no network types match */
  2640.         if (testing)
  2641.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2642.         success = ftpopen(hostname,service,opn_tls);
  2643.         debug(F101,"ftp openftp C ftpopen success","",success);
  2644.         rc = success;
  2645.         }
  2646.     }
  2647.     } else {
  2648. #endif /* NODIAL */
  2649.     if (testing)
  2650.       printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2651.     success = ftpopen(hostname,service,opn_tls);
  2652.     debug(F111,"ftp openftp D ftpopen success",hostname,success);
  2653.     debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  2654.     rc = success;
  2655. #ifndef NODIAL
  2656.     }
  2657. #endif /* NODIAL */
  2658.  
  2659.   xopenftp:
  2660.     debug(F101,"ftp openftp xopenftp rc","",rc);
  2661.     if (hostsave) free(hostsave);
  2662.     if (service) free(service);
  2663.     if (rc < 0 && ftp_logname) {
  2664.     free(ftp_logname);
  2665.     ftp_logname = NULL;
  2666.     }
  2667.     if (ftp_tmp) {
  2668.         free(ftp_tmp);
  2669.         ftp_tmp = NULL;
  2670.     }
  2671.     return(rc);
  2672. }
  2673.  
  2674. int
  2675. doftpusr() {                /* Log in as USER */
  2676.     int x;
  2677.     char *s, * acct = "";
  2678.     debok = 0;                /* Don't log */
  2679.     if ((x = cmfld("Remote username or ID","",&s,xxstring)) < 0)
  2680.       return(x);
  2681.     ckstrncpy(line,s,LINBUFSIZ);
  2682.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0)
  2683.       if (x != -3)
  2684.     return(x);
  2685.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2686.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  2687.            "", &s, xxstring)) < 0)
  2688.       return(x);
  2689.     CHECKCONN();
  2690.     if (*s) {
  2691.     x = strlen(tmpbuf);
  2692.     if (x > 0) {
  2693.         acct = &tmpbuf[x+2];
  2694.         ckstrncpy(acct,s,TMPBUFSIZ - x - 2);
  2695.     }
  2696.     }
  2697.     if (testing)
  2698.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  2699.     success = ftp_user(line,tmpbuf,acct);
  2700. #ifdef CKLOGDIAL
  2701.     dologftp();
  2702. #endif /* CKLOGDIAL */
  2703.     return(success);
  2704. }
  2705.  
  2706. /* DO (various FTP commands)... */
  2707.  
  2708. int
  2709. doftptyp(type) int type; {        /* TYPE */
  2710.     CHECKCONN();
  2711.     ftp_typ = type;
  2712.     changetype(ftp_typ,ftp_vbm);
  2713.     return(1);
  2714. }
  2715.  
  2716. static int
  2717. doftpxmkd(s,vbm) char * s; int vbm; {    /* MKDIR action */
  2718.     int lcs = -1, rcs = -1;
  2719. #ifndef NOCSETS
  2720.     if (ftp_xla) {
  2721.     lcs = ftp_csl;
  2722.     if (lcs < 0) lcs = fcharset;
  2723.     rcs = ftp_csx;
  2724.     if (rcs < 0) rcs = ftp_csr;
  2725.     }
  2726. #endif /* NOCSETS */
  2727.     debug(F110,"ftp doftpmkd",s,0);
  2728.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2729.       return(1);
  2730.     if (ftpcode == 500) {
  2731.     if (!quiet)
  2732.       printf("MKD command not recognized, trying XMKD\n");
  2733.     if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2734.       return(1);
  2735.     }
  2736.     return(0);
  2737. }
  2738.  
  2739. static int
  2740. doftpmkd() {                /* MKDIR parse */
  2741.     int x;
  2742.     char * s;
  2743.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  2744.       return(x);
  2745.     CHECKCONN();
  2746.     ckstrncpy(line,s,LINBUFSIZ);
  2747.     if (testing)
  2748.       printf(" ftp mkdir \"%s\"...\n",line);
  2749.     return(success = doftpxmkd(line,-1));
  2750. }
  2751.  
  2752. static int
  2753. doftprmd() {                /* RMDIR */
  2754.     int x, lcs = -1, rcs = -1;
  2755.     char * s;
  2756.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  2757.       return(x);
  2758.     CHECKCONN();
  2759.     ckstrncpy(line,s,LINBUFSIZ);
  2760.     if (testing)
  2761.       printf(" ftp rmdir \"%s\"...\n",line);
  2762. #ifndef NOCSETS
  2763.     if (ftp_xla) {
  2764.     lcs = ftp_csl;
  2765.     if (lcs < 0) lcs = fcharset;
  2766.     rcs = ftp_csx;
  2767.     if (rcs < 0) rcs = ftp_csr;
  2768.     }
  2769. #endif /* NOCSETS */
  2770.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  2771.       return(success = 1);
  2772.     if (ftpcode == 500) {
  2773.     if (!quiet)
  2774.       printf("RMD command not recognized, trying XMKD\n");
  2775.     success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  2776.     } else
  2777.       success = 0;
  2778.     return(success);
  2779. }
  2780.  
  2781. static int
  2782. doftpren() {                /* RENAME */
  2783.     int x;
  2784.     char * s;
  2785.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  2786.       return(x);
  2787.     ckstrncpy(line,s,LINBUFSIZ);
  2788.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  2789.       return(x);
  2790.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2791.     if ((x = cmcfm()) < 0)
  2792.       return(x);
  2793.     CHECKCONN();
  2794.     if (testing)
  2795.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  2796.     success = ftp_rename(line,tmpbuf);
  2797.     return(success);
  2798. }
  2799.  
  2800. int
  2801. doftpres() {                /* RESET (log out without close) */
  2802.     int x;
  2803.     if ((x = cmcfm()) < 0)
  2804.       return(x);
  2805.     CHECKCONN();
  2806.     if (testing)
  2807.       printf(" ftp reset...\n");
  2808.     return(success = ftp_reset());
  2809. }
  2810.  
  2811. static int
  2812. doftpxhlp() {                /* HELP */
  2813.     int x;
  2814.     char * s;
  2815.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  2816.       return(x);
  2817.     CHECKCONN();
  2818.     ckstrncpy(line,s,LINBUFSIZ);
  2819.     if (testing)
  2820.       printf(" ftp help \"%s\"...\n",line);
  2821.     /* No need to translate -- all FTP commands are ASCII */
  2822.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  2823. }
  2824.  
  2825. static int
  2826. doftpdir(cx) int cx; {            /* [V]DIRECTORY */
  2827.     int x, lcs = 0, rcs = 0, xlate = 0;
  2828.     char * p, * s, * m = "";
  2829.     if (cx == FTP_VDI) {
  2830.     switch (servertype) {
  2831.       case SYS_VMS:
  2832.       case SYS_DOS:
  2833.       case SYS_TOPS10:
  2834.       case SYS_TOPS20:
  2835.         m = "*.*";
  2836.         break;
  2837.       default:
  2838.         m = "*";
  2839.     }
  2840.     }
  2841.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  2842.       return(x);
  2843.     if ((x = remtxt(&s)) < 0)
  2844.       return(x);
  2845. #ifdef NOCSETS
  2846.     xlate = 0;
  2847. #else
  2848.     xlate = ftp_xla;
  2849. #endif /* NOCSETS */
  2850.     line[0] = NUL;
  2851.     ckstrncpy(line,s,LINBUFSIZ);
  2852.     s = line;
  2853.     CHECKCONN();
  2854.  
  2855. #ifndef NOCSETS
  2856.     if (xlate) {            /* SET FTP CHARACTER-SET-TRANSLATION */
  2857.     lcs = ftp_csl;            /* Local charset */
  2858.     if (lcs < 0) lcs = fcharset;
  2859.     if (lcs < 0) xlate = 0;
  2860.     }
  2861.     if (xlate) {            /* Still ON? */
  2862.     rcs = ftp_csx;            /* Remote (Server) charset */
  2863.     if (rcs < 0) rcs = ftp_csr;
  2864.     if (rcs < 0) xlate = 0;
  2865.     }
  2866. #endif /* NOCSETS */
  2867.  
  2868.     if (testing) {
  2869.     p = s;
  2870.     if (!p) p = "";
  2871.     if (*p)
  2872.       printf("Directory of files %s at %s:\n", line, ftp_host);
  2873.     else
  2874.       printf("Directory of files at %s:\n", ftp_host);
  2875.     }
  2876.     debug(F111,"doftpdir",s,cx);
  2877.  
  2878.     if (cx == FTP_DIR) {
  2879.     /* Translation of line[] is done inside recvrequest() */
  2880.     /* when it calls ftpcmd(). */
  2881.     return(success =
  2882.       (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  2883.     }
  2884.     success = 1;            /* VDIR - one file at a time... */
  2885.     p = (char *)remote_files(1,(CHAR *)s,0); /* Get the file list */
  2886.     cancelgroup = 0;
  2887.     if (!ftp_vbm && !quiet)
  2888.       printlines = 1;
  2889.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  2890.     if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  2891.         success = 0;
  2892.         break;
  2893.     }
  2894.     p = (char *)remote_files(0,NULL,0); /* Get next file */
  2895.     debug(F110,"ftp vdir file",s,0);
  2896.     }
  2897.     return(success);
  2898. }
  2899.  
  2900. static int
  2901. doftppwd() {                /* PWD */
  2902.     int x, lcs = -1, rcs = -1;
  2903. #ifndef NOCSETS
  2904.     if (ftp_xla) {
  2905.     lcs = ftp_csl;
  2906.     if (lcs < 0) lcs = fcharset;
  2907.     rcs = ftp_csx;
  2908.     if (rcs < 0) rcs = ftp_csr;
  2909.     }
  2910. #endif /* NOCSETS */
  2911.     if ((x = cmcfm()) < 0)
  2912.       return(x);
  2913.     CHECKCONN();
  2914.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  2915.     success = 1;
  2916.     } else if (ftpcode == 500) {
  2917.     if (ftp_deb)
  2918.       printf("PWD command not recognized, trying XPWD\n");
  2919.     success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  2920.     }
  2921.     return(success);
  2922. }
  2923.  
  2924. static int
  2925. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  2926.     int lcs = -1, rcs = -1;
  2927. #ifndef NOCSETS
  2928.     if (ftp_xla) {
  2929.     lcs = ftp_csl;
  2930.     if (lcs < 0) lcs = fcharset;
  2931.     rcs = ftp_csx;
  2932.     if (rcs < 0) rcs = ftp_csr;
  2933.     }
  2934. #endif /* NOCSETS */
  2935.  
  2936.     debug(F110,"ftp doftpcwd",s,0);
  2937.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2938.       return(1);
  2939.     if (ftpcode == 500) {
  2940.     if (!quiet)
  2941.       printf("CWD command not recognized, trying XCWD\n");
  2942.     if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2943.       return(1);
  2944.     }
  2945.     return(0);
  2946. }
  2947.  
  2948. static int
  2949. doftpcdup() {                /* CDUP */
  2950.     debug(F100,"ftp doftpcdup","",0);
  2951.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  2952.       return(1);
  2953.     if (ftpcode == 500) {
  2954.     if (!quiet)
  2955.       printf("CDUP command not recognized, trying XCUP\n");
  2956.     if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  2957.       return(1);
  2958.     }
  2959.     return(0);
  2960. }
  2961.  
  2962. /* s y n c d i r  --  Synchronizes client & server directories */
  2963.  
  2964. /* Used with recursive PUTs; Returns 0 on failure, 1 on success */
  2965.  
  2966. static int cdlevel = 0, cdsimlvl = 0;
  2967.  
  2968. static int
  2969. syncdir(local,sim) char * local; int sim; {
  2970.     char buf[CKMAXPATH+1];
  2971.     char tmp[CKMAXPATH+1];
  2972.     char msgbuf[CKMAXPATH+64];
  2973.     char c, * p = local, * s = buf, * q = buf;
  2974.     int i, k = 0, done = 0, itsadir = 0, saveq;
  2975.  
  2976.     debug(F110,"ftp syncdir local (new)",local,0);
  2977.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  2978.  
  2979.     itsadir = isdir(local);
  2980.     saveq = quiet;
  2981.  
  2982.     while ((*s = *p)) {            /* Copy the argument filename */
  2983.     if (++k == CKMAXPATH)        /* so we can poke it. */
  2984.       return(-1);
  2985.     if (*s == '/')            /* Pointer to rightmost dirsep */
  2986.       q = s;
  2987.     s++;
  2988.     p++;
  2989.     }
  2990.     if (!itsadir)
  2991.       *q = NUL;                /* Keep just the path part */
  2992.     
  2993.     debug(F110,"ftp syncdir buf",buf,0);
  2994.     if (!strcmp(buf,putpath)) {        /* Same as for previous file? */
  2995.     if (itsadir) {            /* It's a directory? */
  2996.         if (doftpcwd(local,0)) {    /* Try to CD to it */
  2997.         doftpcdup();        /* Worked - CD back up */
  2998.         } else if (sim) {        /* Simulating... */
  2999.         if (fdispla == XYFD_B) {
  3000.             printf("WOULD CREATE DIRECTORY %s\n",local);
  3001.         } else if (fdispla) {
  3002.             ckmakmsg(msgbuf,CKMAXPATH,
  3003.                  "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3004.             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3005.         }
  3006.                 /* See note above */
  3007.         return(0);
  3008.         } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3009.         return(0);
  3010.         } else {
  3011.         if (fdispla == XYFD_B) {
  3012.             printf("CREATED DIRECTORY %s\n",local);
  3013.         } else if (fdispla) {
  3014.             ckmakmsg(msgbuf,CKMAXPATH+64,
  3015.                  "CREATED DIRECTORY ",local,NULL,NULL);
  3016.             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3017.         }
  3018.         }
  3019.     }
  3020.     debug(F110,"ftp syncdir no change",buf,0);
  3021.     return(1);            /* Yes, done. */
  3022.     }
  3023.     ckstrncpy(tmp,buf,CKMAXPATH+1);    /* Make a safe (pre-poked) copy */
  3024.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3025.  
  3026.     p = buf;                /* New */
  3027.     s = putpath;            /* Old */
  3028.  
  3029.     debug(F110,"ftp syncdir A p",p,0);
  3030.     debug(F110,"ftp syncdir A s",s,0);
  3031.  
  3032.     while (*p != NUL && *s != NUL && *p == *s) p++,s++;
  3033.  
  3034.     if (*s == '/' && !*p) s++;        /* Don't count initial slash */
  3035.  
  3036.     debug(F110,"ftp syncdir B p",p,0);
  3037.     debug(F110,"ftp syncdir B s",s,0);
  3038.  
  3039.     /* p and s now point to the leftmost spot where they differ */
  3040.  
  3041.     if (*s) {                /* We have to back up */
  3042.     k = 1;                /* How many levels */
  3043.     while ((c = *s++)) {        /* Count dirseps */
  3044.         if (c == '/' && *s)
  3045.           k++;
  3046.     }
  3047.     for (i = 0; i < k; i++) {     /* Do that many CDUPs */
  3048.         debug(F111,"ftp syncdir up",p,i+1);
  3049.             if (sim && cdsimlvl) {
  3050.                 cdsimlvl--;
  3051.             } else {
  3052.         if (!doftpcdup()) {
  3053.             quiet = saveq;
  3054.             return(0);
  3055.         }
  3056.         }
  3057.         cdlevel--;
  3058.     }
  3059.     if (!*p)            /* If we don't have to go down */
  3060.       goto xcwd;            /* we're done. */
  3061.     }
  3062.     while (p > buf && *p && *p != '/')    /* If in middle of segment */
  3063.       p--;                /* back up to beginning */
  3064.     if (*p == '/')            /* and terminate there */
  3065.       p++;
  3066.  
  3067.     s = p;                /* Point to start of new down path. */
  3068.     while (1) {                /* Loop through characters. */
  3069.     if (*s == '/' || !*s) {        /* Have a segment. */
  3070.         if (!*s)            /* If end of string, */
  3071.           done++;            /* after this segment we're done. */
  3072.         else
  3073.           *s = NUL;            /* NUL out the separator. */
  3074.         if (*p) {            /* If segment is not empty */
  3075.         debug(F110,"ftp syncdir down segment",p,0);
  3076.         if (!doftpcwd(p,0)) {    /* Try to CD to it */
  3077.             if (sim) {
  3078.                         if (fdispla == XYFD_B) {
  3079.                             printf("WOULD CREATE DIRECTORY %s\n",local);
  3080.                         } else if (fdispla) {
  3081.                             ckmakmsg(msgbuf,CKMAXPATH,"WOULD CREATE DIRECTORY",
  3082.                                      local,NULL,NULL);
  3083.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3084.                         }
  3085.                         cdsimlvl++;
  3086.             } else {
  3087.             if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3088. /*
  3089.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3090.   FOO but the remote has a regular file with the same name.  We can't CD
  3091.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3092.   the user handle the problem.
  3093. */
  3094.                 quiet = saveq;
  3095.                 return(0);
  3096.             }
  3097.             if (fdispla == XYFD_B) {
  3098.                 printf("CREATED DIRECTORY %s\n",p);
  3099.             } else if (fdispla) {
  3100.                 ckmakmsg(msgbuf,CKMAXPATH,
  3101.                      "CREATED DIRECTORY ",p,NULL,NULL);
  3102.                 ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3103.             }
  3104.             if (!doftpcwd(p,0)) { /* Try again to CD */
  3105.                 quiet = saveq;
  3106.                 return(0);
  3107.             }
  3108.             }
  3109.         }
  3110.         cdlevel++;
  3111.         }
  3112.         if (done)            /* Quit if no next segment */
  3113.           break;
  3114.         p = s+1;            /* Point to next segment */
  3115.     }
  3116.     s++;                /* Point to next source char */
  3117.     }
  3118.  
  3119.   xcwd:
  3120.     ckstrncpy(putpath,tmp,CKMAXPATH+1);    /* All OK - make this the new path */
  3121.     quiet = saveq;
  3122.     return(1);
  3123. }
  3124.  
  3125. #ifdef DOUPDATE
  3126. #ifdef DEBUG
  3127. static VOID
  3128. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3129.     if (deblog) {
  3130.     debug(F111,"ftp year ",s,xx->tm_year);
  3131.     debug(F111,"ftp month",s,xx->tm_mon);
  3132.     debug(F111,"ftp day  ",s,xx->tm_mday);
  3133.     debug(F111,"ftp hour ",s,xx->tm_hour);
  3134.     debug(F111,"ftp min  ",s,xx->tm_min);
  3135.     debug(F111,"ftp sec  ",s,xx->tm_sec);
  3136.     }
  3137. }
  3138. #endif /* DEBUG */
  3139.  
  3140. /*  t m c o m p a r e  --  Compare two struct tm's */
  3141.  
  3142. /*  Like strcmp() but for struct tm's  */
  3143. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3144.  
  3145. static int
  3146. tmcompare(xx,yy) struct tm * xx, * yy; {
  3147.  
  3148.     if (xx->tm_year < yy->tm_year)    /* First year less than second */
  3149.       return(-1);
  3150.     if (xx->tm_year > yy->tm_year)    /* First year greater than second */
  3151.       return(1);
  3152.  
  3153.     /* Years are equal so compare months */
  3154.  
  3155.     if (xx->tm_mon  < yy->tm_mon)    /* And so on... */
  3156.       return(-1);
  3157.     if (xx->tm_mon  > yy->tm_mon)
  3158.       return(1);
  3159.  
  3160.     if (xx->tm_mday < yy->tm_mday)
  3161.       return(-1);
  3162.     if (xx->tm_mday > yy->tm_mday)
  3163.       return(1);
  3164.  
  3165.     if (xx->tm_hour < yy->tm_hour)
  3166.       return(-1);
  3167.     if (xx->tm_hour > yy->tm_hour)
  3168.       return(1);
  3169.  
  3170.     if (xx->tm_min  < yy->tm_min)
  3171.       return(-1);
  3172.     if (xx->tm_min  > yy->tm_min)
  3173.       return(1);
  3174.  
  3175.     if (xx->tm_sec  < yy->tm_sec)
  3176.       return(-1);
  3177.     if (xx->tm_sec  > yy->tm_sec)
  3178.       return(1);
  3179.  
  3180.     return(0);
  3181. }
  3182. #endif /* DOUPDATE */
  3183.  
  3184. #ifndef HAVE_TIMEGM        /* For platforms that do not have timegm() */
  3185. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3186.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3187. };
  3188.  
  3189. /* Macro for whether a given year is a leap year. */
  3190. #define ISLEAP(year) \
  3191. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3192. #endif /* HAVE_TIMEGM */
  3193.  
  3194. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3195.  
  3196. static time_t
  3197. #ifdef CK_ANSIC
  3198. mkutime(struct tm * tm)
  3199. #else
  3200. mkutime(tm) struct tm * tm;
  3201. #endif /* CK_ANSIC */
  3202. /* mkutime */ {
  3203. #ifdef HAVE_TIMEGM
  3204.     return(timegm(tm));            /* Have system service, use it. */
  3205. #else
  3206. /*
  3207.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3208.   Given a struct tm representing a calendar time in UTC, convert it to
  3209.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3210.   convertable.  Note that this function does not canonicalize the provided
  3211.   struct tm, nor does it allow out-of-range values or years before 1970.
  3212.   Result should be identical with timegm().
  3213. */
  3214.     time_t result = 0;
  3215.     int i;
  3216.     /*
  3217.       We do allow some ill-formed dates, but we don't do anything special
  3218.       with them and our callers really shouldn't pass them to us.  Do
  3219.       explicitly disallow the ones that would cause invalid array accesses
  3220.       or other algorithm problems.
  3221.     */
  3222. #ifdef DEBUG
  3223.     if (deblog) {
  3224.     debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3225.     debug(F101,"mkutime tm_year","",tm->tm_year);
  3226.     }
  3227. #endif /* DEBUG */
  3228.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3229.       return((time_t) -1);
  3230.  
  3231.     /* Convert to time_t. */
  3232.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3233.       result += 365 + ISLEAP(i);
  3234.     for (i = 0; i < tm->tm_mon; i++)
  3235.       result += MONTHDAYS[i];
  3236.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3237.       result++;
  3238.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3239.     result = 60 * result + tm->tm_min;
  3240.     result = 60 * result + tm->tm_sec;
  3241.     debug(F101,"mkutime result","",result);
  3242.     return(result);
  3243. #endif /* HAVE_TIMEGM */
  3244. }
  3245.  
  3246.  
  3247. /*
  3248.   s e t m o d t i m e  --  Set file modification time.
  3249.  
  3250.   f = char * filename;
  3251.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3252.  
  3253.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3254.   Returns:
  3255.     0 on success,
  3256.    -1 on error.
  3257.  
  3258. */
  3259. static int
  3260. #ifdef CK_ANSIC
  3261. setmodtime(char * f, time_t t)
  3262. #else
  3263. setmodtime(f,t) char * f; time_t t;
  3264. #endif /* CK_ANSIC */
  3265. /* setmodtime */ {
  3266. #ifdef NT
  3267.     struct _stat sb;
  3268. #else /* NT */
  3269.     struct stat sb;
  3270. #endif /* NT */
  3271.     int x, rc = 0;
  3272. #ifdef BSD44
  3273.     struct timeval tp[2];
  3274. #else
  3275. #ifdef V7
  3276.     struct utimbuf {
  3277.     time_t timep[2];
  3278.     } tp;
  3279. #else
  3280. #ifdef SYSUTIMEH
  3281. #ifdef NT
  3282.     struct _utimbuf tp;
  3283. #else /* NT */
  3284.     struct utimbuf tp;
  3285. #endif /* NT */
  3286. #else
  3287.     struct utimbuf {
  3288.     time_t atime;
  3289.     time_t mtime;
  3290.     } tp;
  3291. #endif /* SYSUTIMEH */
  3292. #endif /* V7 */
  3293. #endif /* BSD44 */
  3294.  
  3295.     if (stat(f,&sb) < 0) {
  3296.     debug(F111,"setmodtime stat failure",f,errno);
  3297.     return(-1);
  3298.     }
  3299. #ifdef BSD44
  3300.     tp[0].tv_sec = sb.st_atime;        /* Access time first */
  3301.     tp[1].tv_sec = t;            /* Update time second */
  3302.     debug(F111,"setmodtime BSD44",f,t);
  3303. #else
  3304. #ifdef V7
  3305.     tp.timep[0] = t;            /* Set modif. time to creation date */
  3306.     tp.timep[1] = sb.st_atime;        /* Don't change the access time */
  3307.     debug(F111,"setmodtime V7",f,t);
  3308. #else
  3309. #ifdef SYSUTIMEH
  3310.     tp.modtime = t;            /* Set modif. time to creation date */
  3311.     tp.actime = sb.st_atime;        /* Don't change the access time */
  3312.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3313. #else
  3314.     tp.mtime = t;            /* Set modif. time to creation date */
  3315.     tp.atime = sb.st_atime;        /* Don't change the access time */
  3316.     debug(F111,"setmodtime (other)",f,t);
  3317. #endif /* SYSUTIMEH */
  3318. #endif /* V7 */
  3319. #endif /* BSD44 */
  3320.  
  3321.     /* Try to set the file date */
  3322.  
  3323. #ifdef BSD44
  3324.     x = utimes(f,tp);
  3325.     debug(F111,"setmodtime utimes()","BSD44",x);
  3326. #else
  3327. #ifdef IRIX65
  3328.     {
  3329.       /*
  3330.     The following produces the nonsensical warning:
  3331.         Argument  of type "const struct utimbuf *" is incompatible with
  3332.         parameter of type "const struct utimbuf *".  If you can make it
  3333.         go away, be my guest.
  3334.       */
  3335.         const struct utimbuf * t2 = &tp;
  3336.         x = utime(f,t2);
  3337.     }
  3338. #else
  3339.     x = utime(f,&tp);
  3340.     debug(F111,"setmodtime utime()","other",x);
  3341. #endif /* IRIX65 */
  3342. #endif /* BSD44 */
  3343.     if (x)
  3344.       rc = -1;
  3345.  
  3346.     debug(F101,"setmodtime result","",rc);
  3347.     return(rc);
  3348. }
  3349.  
  3350.  
  3351. /*
  3352.   c h k m o d t i m e  --  Check/Set file modification time.
  3353.  
  3354.   fc = function code:
  3355.     0 = Check; returns:
  3356.       -1 on error,
  3357.        0 if local older than remote,
  3358.        1 if modtimes are equal,
  3359.        2 if local newer remote.
  3360.     1 = Set (local file's modtime from remote's); returns:
  3361.       -1 on error,
  3362.        0 on success.
  3363. */
  3364. static int
  3365. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3366. #ifdef NT
  3367.     struct _stat statbuf;
  3368. #else /* NT */
  3369.     struct stat statbuf;
  3370. #endif /* NT */
  3371.     struct tm * tmlocal = NULL;
  3372.     struct tm tmremote;
  3373.     int rc = 0, havedate = 0, lcs = -1, rcs = -1;
  3374.  
  3375. #ifndef NOCSETS
  3376.     if (ftp_xla) {
  3377.     lcs = ftp_csl;
  3378.     if (lcs < 0) lcs = fcharset;
  3379.     rcs = ftp_csx;
  3380.     if (rcs < 0) rcs = ftp_csr;
  3381.     }
  3382. #endif /* NOCSETS */
  3383.  
  3384.     if (fc == 0) {
  3385.     rc = stat(local,&statbuf);
  3386.     if (rc == 0) {            /* Get local file's mod time */
  3387.         tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3388. #ifdef DEBUG
  3389.         if (tmlocal) {
  3390.         dbtime(local,tmlocal);
  3391.  
  3392.         }
  3393. #endif /* DEBUG */
  3394.     }
  3395.     }
  3396.     /* Get remote file's mod time as yyyymmddhhmmss */
  3397.     if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3398.     int flag = 0;            /* Good reply */
  3399.     char c, * s;
  3400.     bzero((char *)&tmremote, sizeof(struct tm));
  3401.     s = ftp_reply_str;
  3402.     while ((c = *s++)) {        /* Skip past response code */
  3403.         if (c == SP) {
  3404.         flag++;
  3405.         break;
  3406.         }
  3407.     }
  3408.     debug(F110,"ftp chkmodtime string",s,0);
  3409.     if (flag) {            /* Convert to struct tm */
  3410.         char * pat;
  3411.         int y2kbug = 0;        /* Seen in Kerberos 4 FTP servers */
  3412.         if (!ckstrcmp(s,"191",3,0)) {
  3413.         pat = "%05d%02d%02d%02d%02d%02d";
  3414.         y2kbug++;
  3415.         debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3416.         } else {
  3417.         pat = "%04d%02d%02d%02d%02d%02d";
  3418.         }
  3419.         if (sscanf(s,        /* Parse into struct tm */
  3420.                pat,
  3421.                &(tmremote.tm_year),
  3422.                &(tmremote.tm_mon),
  3423.                &(tmremote.tm_mday),
  3424.                &(tmremote.tm_hour),
  3425.                &(tmremote.tm_min),
  3426.                &(tmremote.tm_sec)
  3427.                ) == 6) {
  3428.         tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3429.         debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3430.         tmremote.tm_mon--;
  3431. #ifdef DEBUG
  3432.         dbtime(remote,&tmremote);
  3433. #endif /* DEBUG */
  3434.         havedate = 1;
  3435.         }
  3436.     }
  3437.     } else
  3438.       rc = -1;
  3439.  
  3440.     if (fc == 0) {            /* Compare */
  3441.     if (rc == 0 && havedate == 1) {    /* Only if we have both file dates */
  3442.         /*
  3443.           Compare with local file's time.  We don't use
  3444.           clock time (time_t) here in case of signed/unsigned
  3445.           confusion, etc.
  3446.         */
  3447.         return(tmcompare(tmlocal,&tmremote) + 1);
  3448.     }
  3449.     } else if (ftp_dates) {        /* Set */
  3450.     /*
  3451.       Here we must convert struct tm to time_t
  3452.       without applying timezone conversion, for which
  3453.       there is no portable API.  The method is hidden
  3454.           in mkutime(), defined above.
  3455.     */
  3456.     time_t utc;
  3457.     utc = mkutime(&tmremote);
  3458.     debug(F111,"ftp chkmodtime mkutime",remote,utc);
  3459.     if (utc != (time_t)-1)
  3460.       return(setmodtime(local,utc));
  3461.     }
  3462.     return(-1);
  3463. }
  3464.  
  3465. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  3466.  
  3467. static int
  3468. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  3469.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  3470. /* getfile */ {
  3471.     int rc = -1;
  3472.     ULONG t0, t1;
  3473.  
  3474. #ifdef GFTIMER
  3475.     CKFLOAT sec;
  3476. #else
  3477.     int sec = 0;
  3478. #endif /* GFTIMER */
  3479.     char fullname[CKMAXPATH+1];
  3480.  
  3481.     debug(F110,"ftp getfile local A",local,0);
  3482.     debug(F110,"ftp getfile pipename",pipename,0);
  3483.     if (!remote) remote = "";
  3484.  
  3485. #ifdef PATTERNS
  3486.     if (patterns && get_auto && !forcetype) { /* Automatic type switching? */
  3487.     int x;
  3488.     x = matchname(remote,0,servertype);
  3489.     debug(F111,"ftp getfile matchname",remote,x);
  3490.     switch (x) {
  3491.       case 0: ftp_typ = FTT_ASC; break;
  3492.       case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  3493.       default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  3494.     }
  3495.     changetype(ftp_typ,ftp_vbm);
  3496.     binary = ftp_typ;        /* For file-transfer display */
  3497.     }
  3498. #endif /* PATTERNS */
  3499.  
  3500. #ifndef NOCSETS
  3501.     ftp_csx = -1;            /* For file-transfer display */
  3502.     ftp_csl = -1;            /* ... */
  3503.  
  3504.     if (rcs > -1)            /* -1 means no translation */
  3505.       if (ftp_typ == FTT_ASC)        /* File type is "ascii"? */
  3506.     if (fcs < 0)            /* File charset not forced? */
  3507.       fcs = fcharset;        /* use prevailing FILE CHARACTER-SET */
  3508.     if (fcs > -1 && rcs > -1) {        /* Set up translation functions */
  3509.     debug(F110,"ftp getfile","initxlate",0);
  3510.     initxlate(rcs,fcs);        /* NB: opposite order of PUT */
  3511.     ftp_csx = rcs;
  3512.     ftp_csl = fcs;
  3513.     } else
  3514.       xlate = 0;
  3515. #endif /* NOCSETS */
  3516.  
  3517.     if (!pipename && (!local || !local[0]))
  3518.       local = remote;
  3519.  
  3520.     out2screen = !strcmp(local,"-");
  3521.  
  3522.     fullname[0] = NUL;
  3523.     if (pipename) {
  3524.     ckstrncpy(fullname,pipename,CKMAXPATH+1);
  3525.     } else {
  3526.     zfnqfp(local,CKMAXPATH,fullname);
  3527.     if (!fullname[0])
  3528.       ckstrncpy(fullname,local,CKMAXPATH+1);
  3529.     }
  3530.     if (!out2screen && displa && fdispla) { /* Screen */
  3531.     ftscreen(SCR_FN,'F',(long)pktnum,remote);
  3532.     ftscreen(SCR_AN,0,0L,fullname);
  3533.     ftscreen(SCR_FS,0,fsize,"");
  3534.     }
  3535.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  3536.     tlog(F110," as",fullname,0);
  3537.     debug(F111,"ftp getfile size",remote,fsize);
  3538.     debug(F111,"ftp getfile local",local,out2screen);
  3539.  
  3540.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  3541.  
  3542.     t0 = gmstimer();            /* Start time */
  3543.     debug(F111,"ftp getfile t0",remote,t0);
  3544.     rc = recvrequest("RETR",
  3545.              local,
  3546.              remote,
  3547.              append ? "ab" : "wb",
  3548.              0,
  3549.              recover,
  3550.              pipename,
  3551.              xlate,
  3552.              fcs,
  3553.              rcs
  3554.              );
  3555.     t1 = gmstimer();            /* End time */
  3556.     debug(F111,"ftp getfile t1",remote,t1);
  3557.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  3558. #ifdef GFTIMER
  3559.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3560.     fpxfsecs = sec;            /* (for doxlog()) */
  3561. #else
  3562.     sec = (t1 - t0) / 1000;
  3563.     xfsecs = (int)sec;
  3564. #endif /* GFTIMER */
  3565.     debug(F111,"ftp recvrequest rc",remote,rc);
  3566.     if (cancelfile || cancelgroup) {
  3567.     debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  3568.     ftscreen(SCR_ST,ST_INT,0l,"");
  3569.     } else if (rc > 0) {
  3570.     debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  3571.     ftscreen(SCR_ST,ST_SKIP,0l,cmarg);
  3572.     } else if (rc < 0) {
  3573.     switch (ftpcode) {
  3574.       case -4:            /* Network error */
  3575.       case -2:            /* File error */
  3576.         ftscreen(SCR_ST,ST_MSG,0l,ck_errstr());
  3577.         break;
  3578.       case -3:
  3579.         ftscreen(SCR_ST,ST_MSG,0l,"Failure to make data connection");
  3580.         break;
  3581.       case -1:
  3582.         ftscreen(SCR_ST,ST_INT,0l,""); /* (should be covered above) */
  3583.         break;
  3584.       default:
  3585.         ftscreen(SCR_ST,ST_MSG,0l,&ftp_reply_str[4]);
  3586.     }
  3587.     } else {                /* Tudo bem */
  3588.     ftscreen(SCR_PT,'Z',0L,"");
  3589.     if (rc == 0) {
  3590.         ftscreen(SCR_ST,ST_OK,0L,""); /* For screen */
  3591.         makestr(&rrfspec,remote);      /* For WHERE command */
  3592.         makestr(&rfspec,fullname);
  3593.         if (ftp_dates) {          /* If FTP DATES ON... */
  3594.         chkmodtime(local,remote,1); /* set local file date */
  3595.         }
  3596.     }
  3597.     }
  3598.     filcnt++;                /* Used by \v(filenum) */
  3599. #ifdef TLOG
  3600.     if (tralog) {
  3601.     if (rc > 0) {
  3602.         tlog(F100," recovery skipped","",0);
  3603.     } else if (rc == 0) {
  3604.         tlog(F101," complete, size", "", fsize);
  3605.     } else if (cancelfile) {
  3606.         tlog(F100," canceled by user","",0);
  3607.     } else {
  3608.         tlog(F110," failed:",ftp_reply_str,0);
  3609.     }
  3610.     if (!tlogfmt)
  3611.       doxlog(what,local,fsize,ftp_typ,!rc,"");
  3612.     }
  3613. #endif /* TLOG */
  3614.     return(rc);
  3615. }
  3616.  
  3617. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  3618. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  3619.  
  3620. static int
  3621. putfile(cx,
  3622.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  3623.     char * local, * remote, * mvto, *rnto, *srvrn;
  3624.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg;
  3625.  
  3626. /* putfile */ {
  3627.  
  3628.     char asname[CKMAXPATH+1];
  3629.     char fullname[CKMAXPATH+1];
  3630.     int k = -1, x, y, o, rc, nc, xlate = 0, restart = 0, mt = -1;
  3631.     char * s, * cmd;
  3632.     ULONG t0, t1;            /* Times for stats */
  3633.     int ofcs, orcs;
  3634.  
  3635. #ifdef GFTIMER
  3636.     CKFLOAT sec;
  3637. #else
  3638.     int sec = 0;
  3639. #endif /* GFTIMER */
  3640.     debug(F111,"ftp putfile flg",local,flg);
  3641.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  3642.     debug(F101,"ftp putfile fcs","",fcs);
  3643.     debug(F101,"ftp putfile rcs","",rcs);
  3644.  
  3645.     ofcs = fcs;                /* Save charset args */
  3646.     orcs = rcs;
  3647.  
  3648.     sendstart = 0L;
  3649.     restart = flg & PUT_RES;
  3650.     if (!remote)
  3651.       remote = "";
  3652.  
  3653.     /* FTP protocol command to send to server */
  3654.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  3655.  
  3656.     if (x_cnv == SET_AUTO) {        /* Name conversion is auto */
  3657.     if (alike) {             /* If server & client are alike */
  3658.         nc = 0;            /* no conversion */
  3659.     } else {            /* If they are different */
  3660.         if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  3661.           nc = -1;            /* only minimal conversions needed */
  3662.         else            /* otherwise */
  3663.           nc = 1;            /* full conversion */
  3664.     }
  3665.     } else                /* Not auto - do what user said */
  3666.       nc = x_cnv;
  3667.  
  3668.     if (filepeek && !pipesend) {    /* Determine file type */
  3669.     if (isdir(local)) {        /* If it's a directory */
  3670.         k = FT_BIN;            /* skip the file scan */
  3671.     } else {
  3672.         debug(F110,"FTP PUT calling scanfile",local,0);
  3673.         k = scanfile(local,&o,nscanfile); /* Scan the file */
  3674.     }
  3675.     debug(F111,"FTP PUT scanfile",local,k);
  3676.     if (k > -1 && !forcetype) {
  3677.         ftp_typ = (k == FT_BIN) ? 1 : 0;
  3678.         if (xft > -1 && ftp_typ != xft) {
  3679.         if (flg & PUT_SIM)
  3680.           tlog(F110,"ftp put SKIP (Type):", fullname, 0);
  3681.         return(SKP_TYP);
  3682.         }
  3683.         if (ftp_typ == 1 && tenex)    /* User said TENEX? */
  3684.           ftp_typ = FTT_TEN;
  3685.     }
  3686.     }
  3687. #ifndef NOCSETS
  3688.     ftp_csx = -1;            /* For file-transfer display */
  3689.     ftp_csl = -1;            /* ... */
  3690.  
  3691.     if (rcs > -1) {            /* -1 means no translation */
  3692.     if (ftp_typ == 0) {        /* File type is "ascii"? */
  3693.         if (fcs < 0) {        /* File charset not forced? */
  3694.         if (k < 0) {        /* If we didn't scan */
  3695.             fcs = fcharset;    /* use prevailing FILE CHARACTER-SET */
  3696.         } else {        /* If we did scan, use scan result */
  3697.             switch (k) {
  3698.               case FT_TEXT:    /* Unknown text */
  3699.             fcs = fcharset;
  3700.             break;
  3701.               case FT_7BIT:    /* 7-bit text */
  3702.             fcs = dcset7;
  3703.             break;
  3704.               case FT_8BIT:    /* 8-bit text */
  3705.             fcs = dcset8;
  3706.             break;
  3707.               case FT_UTF8:    /* UTF-8 */
  3708.             fcs = FC_UTF8;
  3709.             break;
  3710.               case FT_UCS2:    /* UCS-2 */
  3711.             fcs = FC_UCS2;
  3712.             if (o > -1)    /* Input file byte order */
  3713.               fileorder = o;
  3714.             break;
  3715.               default:
  3716.             rcs = -1;
  3717.             }
  3718.         }
  3719.         }
  3720.     }
  3721.     }
  3722.     if (fcs > -1 && rcs > -1) {        /* Set up translation functions */
  3723.     debug(F110,"ftp putfile","initxlate",0);
  3724.     initxlate(fcs,rcs);
  3725.     debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  3726.     xlate = 1;
  3727.     ftp_csx = rcs;
  3728.     ftp_csl = fcs;
  3729.     }
  3730. #endif /* NOCSETS */
  3731.  
  3732.     binary = ftp_typ;            /* For file-transfer display */
  3733.  
  3734.     if (recursive) {            /* If sending recursively, */
  3735.     if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  3736.       return(-1);            /* Don't PUT if it fails. */
  3737.     else if (isdir(local))        /* It's a directory */
  3738.       return(0);            /* Don't send it! */
  3739.     }
  3740.     if (*remote) {            /* If an as-name template was given */
  3741. #ifndef NOSPL
  3742.     if (cmd_quoting) {        /* and COMMAND QUOTING is ON */
  3743.         y = CKMAXPATH;        /* evaluate it for this file */
  3744.         s = asname;
  3745.         zzstring(remote,&s,&y);
  3746.     } else
  3747. #endif /* NOSPL */
  3748.       ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  3749.     } else {                        /* No as-name */
  3750.     nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  3751.     debug(F110,"FTP PUT nzltor",asname,0);
  3752.     }
  3753.     /* Preliminary messages and log entries */
  3754.  
  3755.     fullname[0] = NUL;
  3756.     zfnqfp(local,CKMAXPATH,fullname);
  3757.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  3758.  
  3759.     if (displa && fdispla) {        /* Screen */
  3760.     ftscreen(SCR_FN,'F',(long)pktnum,local);
  3761.     ftscreen(SCR_AN,0,0L,asname);
  3762.     ftscreen(SCR_FS,0,fsize,"");
  3763.     }
  3764. #ifdef DOUPDATE
  3765.     if (flg & PUT_UPD) {        /* Update mode... */
  3766.     mt = chkmodtime(fullname,asname,0);
  3767.     debug(F111,"ftp putfile chkmodtime",asname,mt);
  3768.     if (mt == 0) {            /* Remote is older */
  3769.         tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  3770.         ftscreen(SCR_ST,ST_SKIP,SKP_DAT,fullname);
  3771.         filcnt++;
  3772.         return(SKP_DAT);
  3773.     } else if (mt == 1) {        /* Times are equal */
  3774.         tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  3775.         ftscreen(SCR_ST,ST_SKIP,SKP_EQU,fullname);
  3776.         filcnt++;
  3777.         return(SKP_DAT);
  3778.     }
  3779.     tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  3780.          "ftp put /update TEXT:", fullname, 0);
  3781.     } else if (flg & PUT_RES) {
  3782.     tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  3783.          "ftp put /recover TEXT:", fullname, 0);
  3784.     } else {
  3785.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  3786.     }
  3787. #else
  3788.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  3789. #endif /* DOUPDATE */
  3790.     tlog(F110," as",asname,0);
  3791.  
  3792. #ifndef NOCSETS
  3793.     if (xlate) {
  3794.     debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  3795.     tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  3796.     tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  3797.     } else if (!ftp_typ) {
  3798.     tlog(F110," character sets:","no conversion",0);
  3799.     fcs = ofcs;            /* Binary file but we still must */
  3800.     rcs = orcs;            /* translate its name */
  3801.     }
  3802. #endif /* NOCSETS */
  3803.  
  3804.     /* PUT THE FILE */
  3805.  
  3806.     t0 = gmstimer();            /* Start time */
  3807.     if (flg & PUT_SIM) {        /* rc > 0 is a skip reason code */
  3808.     if (flg & PUT_UPD) {        /* (see SKP_xxx in ckcker.h) */
  3809.         rc = (mt < 0) ?        /* Update mode... */
  3810.           SKP_XNX :            /* Remote file doesn't exist */
  3811.         SKP_XUP;        /* Remote file is older */
  3812.     } else {
  3813.         rc = SKP_SIM;        /* "Would be sent", period. */
  3814.     }
  3815.     } else {
  3816.     rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  3817.     }
  3818.     t1 = gmstimer();            /* End time */
  3819.     filcnt++;                /* File number */
  3820.  
  3821. #ifdef GFTIMER
  3822.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3823.     fpxfsecs = sec;            /* (for doxlog()) */
  3824. #else
  3825.     sec = (t1 - t0) / 1000;
  3826.     xfsecs = (int)sec;
  3827. #endif /* GFTIMER */
  3828.  
  3829.     debug(F111,"ftp sendrequest rc",local,rc);
  3830.  
  3831.     if (cancelfile || cancelgroup) {
  3832.     debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  3833.     ftscreen(SCR_ST,ST_INT,0l,"");
  3834.     } else if (rc > 0) {
  3835.     debug(F101,"ftp put skipped",local,rc);
  3836.     ftscreen(SCR_ST,ST_SKIP,rc,fullname);
  3837.     } else if (rc < 0) {
  3838.     debug(F111,"ftp put error",local,ftpcode);
  3839.     ftscreen(SCR_ST,ST_MSG,0L,&ftp_reply_str[4]);
  3840.     } else {
  3841.     debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  3842.     ftscreen(SCR_PT,'Z',0L,"");
  3843.     debug(F111,"ftp put ST_OK",local,rc);
  3844.     ftscreen(SCR_ST,ST_OK,0L,"");
  3845.     makestr(&sfspec,fullname);    /* For WHERE command */
  3846.     makestr(&srfspec,asname);
  3847.     }
  3848.  
  3849.     /* Final log entries */
  3850.  
  3851. #ifdef TLOG
  3852.     if (tralog) {
  3853.     if (rc > 0) {
  3854.         if (rc == SKP_XNX)
  3855.           tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  3856.         else if (rc == SKP_XUP)
  3857.           tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  3858.         else if (rc == SKP_SIM)
  3859.           tlog(F100," /simulate: WOULD BE SENT","",0);
  3860.         else
  3861.           tlog(F110," skipped:",gskreason(rc),0);
  3862.     } else if (rc == 0) {
  3863.         tlog(F101," complete, size", "", fsize);
  3864.     } else if (cancelfile) {
  3865.         tlog(F100," canceled by user","",0);
  3866.     } else {
  3867.         tlog(F110," failed:",ftp_reply_str,0);
  3868.     }
  3869.     if (!tlogfmt)
  3870.       doxlog(what,local,fsize,ftp_typ,!rc,"");
  3871.     }
  3872. #endif /* TLOG */
  3873.  
  3874.     if (rc < 0)                /* PUT did not succeed */
  3875.       return(-1);            /* so done. */
  3876.  
  3877.     if (flg & PUT_SIM)            /* Simulating, skip the rest. */
  3878.       return(SKP_SIM);
  3879.  
  3880. #ifdef UNIX
  3881.     /* Set permissions too? */
  3882.  
  3883.     if (prm) {                /* Change permissions? */
  3884.     s = zgperm(local);        /* Get perms of local file */
  3885.     if (!s) s = "";
  3886.     x = strlen(s);
  3887.     if (x > 3) s += (x - 3);
  3888.     if (rdigits(s)) {
  3889.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  3890.         x =
  3891.           ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  3892.         tlog(F110, x ? " chmod" : " chmod failed",
  3893.          s,
  3894.          0
  3895.          );
  3896.         if (!x)
  3897.           return(-1);
  3898.     }
  3899.     }
  3900. #endif /* UNIX */
  3901.  
  3902.     /* Disposition of source file */
  3903.  
  3904.     if (moving) {
  3905.     x = zdelet(local);
  3906.     tlog(F110, (x > -1) ?
  3907.          " deleted" : " failed to delete",
  3908.          local,
  3909.          0
  3910.          );
  3911.     if (x < 0)
  3912.       return(-1);
  3913.     } else if (mvto) {
  3914.     x = zrename(local,mvto);
  3915.     tlog(F110, (x > -1) ?
  3916.          " moved source to" : " failed to move source to",
  3917.          mvto,
  3918.          0
  3919.          );
  3920.     if (x < 0)
  3921.       return(-1);
  3922.     /* ftscreen(SCR_ST,ST_MSG,0L,mvto); */
  3923.  
  3924.     } else if (rnto) {
  3925.     char * s = rnto;
  3926. #ifndef NOSPL
  3927.     int y;                /* Pass it thru the evaluator */
  3928.     extern int cmd_quoting;        /* for \v(filename) */
  3929.     if (cmd_quoting) {        /* But only if cmd_quoting is on */
  3930.         y = CKMAXPATH;
  3931.         s = (char *)asname;
  3932.         zzstring(rnto,&s,&y);
  3933.         s = (char *)asname;
  3934.     }
  3935. #endif /* NOSPL */
  3936.     if (s) if (*s) {
  3937.         int x;
  3938.         x = zrename(local,s);
  3939.         tlog(F110, (x > -1) ?
  3940.          " renamed source file to" :
  3941.          " failed to rename source file to",
  3942.          s,
  3943.          0
  3944.          );
  3945.         if (x < 0)
  3946.           return(-1);
  3947.         /* ftscreen(SCR_ST,ST_MSG,0L,s); */
  3948.     }
  3949.     }
  3950.  
  3951.     /* Disposition of destination file */
  3952.  
  3953.     if (srvrn) {            /* /SERVER-RENAME: */
  3954.     char * s = srvrn;
  3955. #ifndef NOSPL
  3956.     int y;                /* Pass it thru the evaluator */
  3957.     extern int cmd_quoting; /* for \v(filename) */
  3958.     debug(F111,"ftp putfile srvrn",s,1);
  3959.  
  3960.     if (cmd_quoting) {        /* But only if cmd_quoting is on */
  3961.         y = CKMAXPATH;
  3962.         s = (char *)fullname;    /* We can recycle this buffer now */
  3963.         zzstring(srvrn,&s,&y);
  3964.         s = (char *)fullname;
  3965.     }
  3966. #endif /* NOSPL */
  3967.     debug(F111,"ftp putfile srvrn",s,2);
  3968.     if (s) if (*s) {
  3969.         int x;
  3970.         x = ftp_rename(asname,s);
  3971.         debug(F111,"ftp putfile ftp_rename",asname,x);
  3972.         tlog(F110, (x > 0) ?
  3973.          " renamed destination file to" :
  3974.          " failed to rename destination file to",
  3975.          s,
  3976.          0
  3977.          );
  3978.         if (x < 1)
  3979.           return(-1);
  3980.     }
  3981.     }
  3982.     return(0);
  3983. }
  3984.  
  3985. static int
  3986. #ifdef CK_ANSIC
  3987. xxout(char c)
  3988. #else
  3989. xxout(c) char c;
  3990. #endif /* CK_ANSIC */
  3991. {
  3992.     return(zzout(dout,c));
  3993. }
  3994.  
  3995. static int
  3996. #ifdef CK_ANSIC
  3997. scrnout(char c)
  3998. #else
  3999. scrnout(c) char c;
  4000. #endif /* CK_ANSIC */
  4001. {
  4002.     return(putchar(c));
  4003. }
  4004.  
  4005. static int
  4006. #ifdef CK_ANSIC
  4007. pipeout(char c)
  4008. #else
  4009. pipeout(c) char c;
  4010. #endif /* CK_ANSIC */
  4011. {
  4012.     return(zmchout(c));
  4013. }
  4014.  
  4015. static int
  4016. ispathsep(c) int c; {
  4017.     switch (servertype) {
  4018.       case SYS_VMS:
  4019.       case SYS_TOPS10:
  4020.       case SYS_TOPS20:
  4021.     return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4022.       case SYS_OS2:
  4023.       case SYS_WIN32:
  4024.       case SYS_DOS:
  4025.     return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4026.       case SYS_VOS:
  4027.     return((c == '>') ? 1 : 0);
  4028.       default:
  4029.     return((c == '/') ? 1 : 0);
  4030.     }
  4031. }
  4032.  
  4033. static int
  4034. iscanceled() {
  4035. #ifdef CK_CURSES
  4036.     extern int ck_repaint();
  4037. #endif /* CK_CURSES */
  4038.     int x, rc = 0;
  4039.     char c = 0;
  4040.     if (cancelfile)
  4041.       return(1);
  4042.     x = conchk();            /* Any chars waiting at console? */
  4043.     if (x-- > 0) {            /* Yes...  */
  4044.     c = coninc(5);            /* Get one */
  4045.     switch (c) {
  4046.       case 032:            /* Ctrl-X or X */
  4047.       case 'z':
  4048.       case 'Z': cancelgroup++;    /* fall thru on purpose */
  4049.       case 030:            /* Ctrl-Z or Z */
  4050.       case 'x':
  4051.       case 'X': cancelfile++; rc++; break;
  4052. #ifdef CK_CURSES
  4053.       case 'L':
  4054.       case 'l':
  4055.       case 014:            /* Ctrl-L or L or Ctrl-W */
  4056.       case 027:
  4057.         ck_repaint();        /* Refresh screen */
  4058. #endif /* CK_CURSES */
  4059.     }
  4060.     }
  4061.     while (x-- > 0)            /* Soak up any rest */
  4062.       c = coninc(1);
  4063.     return(rc);
  4064. }
  4065.  
  4066. /* zzsend - used by buffered output macros. */
  4067.  
  4068. static int
  4069. #ifdef CK_ANSIC
  4070. zzsend(int fd, CHAR c)
  4071. #else
  4072. zzsend(fd,c) int fd; CHAR c;
  4073. #endif /* CK_ANSIC */
  4074. {
  4075.     int rc;
  4076.     if (iscanceled())            /* Check for cancellation */
  4077.       return(0);
  4078.  
  4079.     rc = (ftp_dpl == FPL_CLR) ?
  4080.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4081.     secure_putbuf(fd, ucbuf, nout);
  4082.  
  4083.     ucbuf[nout] = NUL;
  4084.  
  4085.     nout = 0;
  4086.     ucbuf[nout++] = c;
  4087.     spackets++;
  4088.     pktnum++;
  4089.     if (rc > -1 && fdispla != XYFD_B) {
  4090.     spktl = nout;
  4091.     ftscreen(SCR_PT,'D',spackets,NULL);
  4092.     }
  4093.     return(rc);
  4094. }
  4095.  
  4096. /* c m d l i n p u t  --  Command-line PUT */
  4097.  
  4098. int
  4099. cmdlinput(stay) int stay; {
  4100.     int x, rc = 0, done = 0, good = 0, status = 0;
  4101.     ULONG t0, t1;            /* Times for stats */
  4102. #ifdef GFTIMER
  4103.     CKFLOAT sec;
  4104. #else
  4105.     int sec = 0;
  4106. #endif /* GFTIMER */
  4107.  
  4108.     if (quiet) {            /* -q really means quiet */
  4109.     displa = 0;
  4110.     fdispla = 0;
  4111.     } else {
  4112.     displa = 1;
  4113.     fdispla = XYFD_B;
  4114.     }
  4115.     testing = 0;
  4116.     out2screen = 0;
  4117.     dpyactive = 0;
  4118.     what = W_FTP|W_SEND;
  4119.  
  4120. #ifndef NOSPL
  4121.     cmd_quoting = 0;
  4122. #endif /* NOSPL */
  4123.     sndsrc = nfils;
  4124.  
  4125.     t0 = gmstimer();            /* Record starting time */
  4126.  
  4127.     while (!done && !cancelgroup) {    /* Loop for all files */
  4128.  
  4129.     cancelfile = 0;
  4130.     x = gnfile();            /* Get next file from list(s) */
  4131.     if (x == 0)            /* (see gnfile() comments...) */
  4132.       x = gnferror;
  4133.  
  4134.     switch (x) {
  4135.       case 1:            /* File to send */
  4136.         rc = putfile(FTP_PUT,    /* Function (PUT, APPEND) */
  4137.              filnam,    /* Local file to send */
  4138.              filnam,    /* Remote name for file */
  4139.              forcetype,    /* Text/binary mode forced */
  4140.              0,        /* Not moving */
  4141.              NULL,        /* No move-to */
  4142.              NULL,        /* No rename-to */
  4143.              NULL,        /* No server-rename */
  4144.              ftp_cnv,    /* Filename conversion */
  4145.              0,        /* Unique-server-names */
  4146.              -1,        /* All file types */
  4147.              0,        /* No permissions */
  4148.              -1,        /* No character sets */
  4149.              -1,        /* No character sets */
  4150.              0        /* No update or restart */
  4151.              );
  4152.         if (rc > -1) {
  4153.         good++;
  4154.         status = 1;
  4155.         }
  4156.         if (cancelfile) {
  4157.         continue;        /* Or break? */
  4158.         }
  4159.         if (rc < 0) {
  4160.         ftp_fai++;
  4161.         }
  4162.         continue;            /* Or break? */
  4163.  
  4164.       case 0:            /* No more files, done */
  4165.         done++;
  4166.         continue;
  4167.  
  4168.       case -2:
  4169.       case -1:
  4170.         printf("?%s: file not found - \"%s\"\n",
  4171.            puterror ? "Fatal" : "Warning",
  4172.            filnam
  4173.            );
  4174.         continue;            /* or break? */
  4175.       case -3:
  4176.         printf("?Warning access denied - \"%s\"\n", filnam);
  4177.         continue;            /* or break? */
  4178.       case -5:
  4179.         printf("?Too many files match\n");
  4180.         done++;
  4181.         break;
  4182.       case -6:
  4183.         if (good < 1)
  4184.           printf("?No files selected\n");
  4185.         done++;
  4186.         break;
  4187.       default:
  4188.         printf("?getnextfile() - unknown failure\n");
  4189.         done++;
  4190.     }
  4191.     }
  4192.     if (status > 0) {
  4193.     if (cancelgroup)
  4194.       status = 0;
  4195.     else if (cancelfile && good < 1)
  4196.       status = 0;
  4197.     }
  4198.     success = status;
  4199.     x = success;
  4200.     if (x > -1) {
  4201.     lastxfer = W_FTP|W_SEND;
  4202.     xferstat = success;
  4203.     }
  4204.     t1 = gmstimer();            /* End time */
  4205. #ifdef GFTIMER
  4206.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4207.     if (!sec) sec = 0.001;
  4208.     fptsecs = sec;
  4209. #else
  4210.     sec = (t1 - t0) / 1000;
  4211.     if (!sec) sec = 1;
  4212. #endif /* GFTIMER */
  4213.     tfcps = (long) (tfc / sec);
  4214.     tsecs = (int)sec;
  4215.     lastxfer = W_FTP|W_SEND;
  4216.     xferstat = success;
  4217.     if (dpyactive)
  4218.       ftscreen(SCR_TC,0,0L,"");
  4219.  
  4220.     if (!stay)
  4221.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4222.     return(success);
  4223. }
  4224.  
  4225.  
  4226. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4227.  
  4228. int
  4229. #ifdef CK_ANSIC
  4230. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4231. #else
  4232. doftpput(cx,who) int cx, who;
  4233. #endif /* CK_ANSIC */
  4234. {
  4235.     struct FDB sf, fl, sw, cm;
  4236.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4237.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4238.     char * s, * s2;
  4239.  
  4240.     int x_csl, x_csr = -1;        /* Local and remote charsets */
  4241.     int x_xla = 0;
  4242.     int x_recurse = 0;
  4243.     char c, * p;            /* Workers */
  4244. #ifdef PUTARRAY
  4245.     int range[2];            /* Array range */
  4246.     char ** ap = NULL;            /* Array pointer */
  4247.     int arrayx = -1;            /* Array index */
  4248. #endif /* PUTARRAY */
  4249.     ULONG t0 = 0L, t1 = 0L;        /* Times for stats */
  4250. #ifdef GFTIMER
  4251.     CKFLOAT sec;
  4252. #else
  4253.     int sec = 0;
  4254. #endif /* GFTIMER */
  4255.  
  4256.     struct stringint {            /* Temporary array for switch values */
  4257.     char * sval;
  4258.     int ival;
  4259.     } pv[SND_MAX+1];
  4260.  
  4261.     success = 0;            /* Assume failure */
  4262.     forcetype = 0;            /* No /TEXT or /BINARY given yet */
  4263.     out2screen = 0;            /* Not outputting file to screen */
  4264.     putflags = 0;            /* PUT options */
  4265.     x_cnv = ftp_cnv;            /* Filename conversion */
  4266.     x_usn = ftp_usn;            /* Unique server names */
  4267.     x_prm = ftp_prm;            /* Permissions */
  4268.     if (x_prm == SET_AUTO)        /* Permissions AUTO */
  4269.       x_prm = alike;
  4270.  
  4271. #ifndef NOCSETS
  4272.     x_csr = ftp_csr;            /* Inherit global server charset */
  4273.     x_csl = ftp_csl;
  4274.     if (x_csl < 0)
  4275.       x_csl = fcharset;
  4276.     x_xla = ftp_xla;
  4277. #endif /* NOCSETS */
  4278.  
  4279.     makestr(&filefile,NULL);        /* No filename list file yet. */
  4280.     putpath[0] = NUL;            /* Initialize for syncdir(). */
  4281.     puterror = ftp_err;            /* Inherit global error action. */
  4282.     what = W_SEND|W_FTP;        /* What we're doing (sending w/FTP) */
  4283.     asnambuf[0] = NUL;            /* Clear as-name buffer */
  4284.  
  4285.     if (g_ftp_typ > -1) {        /* Restore TYPE if saved */
  4286.     ftp_typ = g_ftp_typ;
  4287.     /* g_ftp_typ = -1; */
  4288.     }
  4289.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4290.     pv[i].sval = NULL;        /* to null pointers */
  4291.     pv[i].ival = -1;        /* and -1 int values */
  4292.     }
  4293.     if (who == 0) {            /* Called with unprefixed command */
  4294.     switch (cx) {
  4295.       case XXRSEN:  pv[SND_RES].ival = 1; break;
  4296.       case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4297.       case XXMOVE:  pv[SND_MOV].ival = 1; break;
  4298.       case XXMMOVE: pv[SND_MOV].ival = 1; /* fall thru */
  4299.       case XXMSE:   mput++; break;
  4300.     }
  4301.     } else {
  4302.     if (cx == FTP_MPU)
  4303.       mput++;
  4304.     }
  4305.     cmfdbi(&sw,                /* First FDB - command switches */
  4306.        _CMKEY,            /* fcode */
  4307.        "Filename, or switch",    /* hlpmsg */
  4308.        "",                /* default */
  4309.        "",                /* addtl string data */
  4310.        nputswi,            /* addtl numeric data 1: tbl size */
  4311.        4,                /* addtl numeric data 2: 4 = cmswi */
  4312.        xxstring,            /* Processing function */
  4313.        putswi,            /* Keyword table */
  4314.        &sf                /* Pointer to next FDB */
  4315.        );
  4316.     cmfdbi(&fl,                /* 3rd FDB - local filespec */
  4317.        _CMFLD,            /* fcode */
  4318.        "",                /* hlpmsg */
  4319.        "",                /* default */
  4320.        "",                /* addtl string data */
  4321.        0,                /* addtl numeric data 1 */
  4322.        0,                /* addtl numeric data 2 */
  4323.        xxstring,
  4324.        NULL,
  4325.        &cm
  4326.        );
  4327.     cmfdbi(&cm,                /* 4th FDB - Confirmation */
  4328.        _CMCFM,            /* fcode */
  4329.        "",                /* hlpmsg */
  4330.        "",                /* default */
  4331.        "",                /* addtl string data */
  4332.        0,                /* addtl numeric data 1 */
  4333.        0,                /* addtl numeric data 2 */
  4334.        NULL,
  4335.        NULL,
  4336.        NULL
  4337.        );
  4338.  
  4339.   again:
  4340.     cmfdbi(&sf,                /* 2nd FDB - file to send */
  4341.        _CMIFI,            /* fcode */
  4342.        "",                /* hlpmsg */
  4343.        "",                /* default */
  4344.        "",                /* addtl string data */
  4345.        /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  4346.        nolinks | x_recurse,        /* addtl numeric data 1 */
  4347.        0,                /* dirflg 0 means "not dirs only" */
  4348.        xxstring,
  4349.        NULL,
  4350.        mput ? &cm : &fl
  4351.        );
  4352.  
  4353.     while (1) {                /* Parse 0 or more switches */
  4354.     x = cmfdb(&sw);            /* Parse something */
  4355.     debug(F101,"ftp put cmfdb","",x);
  4356.     debug(F101,"ftp put fcode","",cmresult.fcode);
  4357.     if (x < 0)            /* Error */
  4358.       goto xputx;            /* or reparse needed */
  4359.     if (cmresult.fcode != _CMKEY)    /* Break out of loop if not a switch */
  4360.       break;
  4361.     c = cmgbrk();            /* Get break character */
  4362.     getval = (c == ':' || c == '='); /* to see how they ended the switch */
  4363.     if (getval && !(cmresult.kflags & CM_ARG)) {
  4364.         printf("?This switch does not take arguments\n");
  4365.         x = -9;
  4366.         goto xputx;
  4367.     }
  4368.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  4369.         printf("?This switch requires an argument\n");
  4370.         x = -9;
  4371.         goto xputx;
  4372.     }
  4373.     n = cmresult.nresult;        /* Numeric result = switch value */
  4374.     debug(F101,"ftp put switch","",n);
  4375.     switch (n) {            /* Process the switch */
  4376.       case SND_AFT:            /* Send /AFTER:date-time */
  4377.       case SND_BEF:            /* Send /BEFORE:date-time */
  4378.       case SND_NAF:            /* Send /NOT-AFTER:date-time */
  4379.       case SND_NBE:            /* Send /NOT-BEFORE:date-time */
  4380.         if (!getval) break;
  4381.         if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  4382.         if (x == -3) {
  4383.             printf("?Date-time required\n");
  4384.             x = -9;
  4385.         }
  4386.         goto xputx;
  4387.         }
  4388.         pv[n].ival = 1;
  4389.         makestr(&(pv[n].sval),s);
  4390.         break;
  4391.  
  4392.       case SND_ASN:            /* /AS-NAME: */
  4393.         debug(F101,"ftp put /as-name getval","",getval);
  4394.         if (!getval) break;
  4395.         if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  4396.         if (x == -3) {
  4397.             printf("?name required\n");
  4398.             x = -9;
  4399.         }
  4400.         goto xputx;
  4401.         }
  4402.         makestr(&(pv[n].sval),brstrip(s));
  4403.         debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  4404.         if (pv[n].sval) pv[n].ival = 1;
  4405.         break;
  4406.  
  4407. #ifdef PUTARRAY
  4408.       case SND_ARR:            /* /ARRAY */
  4409.         if (!getval) break;
  4410.         ap = NULL;
  4411.         if ((x = cmfld("Array name (a single letter will do)",
  4412.                "",
  4413.                &s,
  4414.                NULL
  4415.                )) < 0) {
  4416.         if (x == -3)
  4417.           break;
  4418.         else
  4419.           return(x);
  4420.         }
  4421.         if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  4422.         printf("?Bad array: %s\n",s);
  4423.         return(-9);
  4424.         }
  4425.         if (!(ap = a_ptr[x])) {
  4426.         printf("?No such array: %s\n",s);
  4427.         return(-9);
  4428.         }
  4429.         pv[n].ival = 1;
  4430.         pv[SND_CMD].ival = 0;    /* Undo any conflicting ones... */
  4431.         pv[SND_RES].ival = 0;
  4432.         pv[SND_FIL].ival = 0;
  4433.         arrayx = x;
  4434.         break;
  4435. #endif /* PUTARRAY */
  4436.  
  4437.       case SND_BIN:            /* /BINARY */
  4438.       case SND_TXT:            /* /TEXT or /ASCII */
  4439.       case SND_TEN:            /* /TENEX */
  4440.         pv[SND_BIN].ival = 0;
  4441.         pv[SND_TXT].ival = 0;
  4442.         pv[SND_TEN].ival = 0;
  4443.         pv[n].ival = 1;
  4444.         break;
  4445.  
  4446. #ifdef PUTPIPE
  4447.       case SND_CMD:            /* These take no args */
  4448.         if (nopush) {
  4449.         printf("?Sorry, system command access is disabled\n");
  4450.         x = -9;
  4451.         goto xputx;
  4452.         }
  4453. #ifdef PIPESEND
  4454.         else if (sndfilter) {
  4455.         printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  4456.         x = -9;
  4457.         goto xputx;
  4458.         }
  4459. #endif /* PIPESEND */
  4460.         sw.hlpmsg = "Command, or switch"; /* Change help message */
  4461.         pv[n].ival = 1;        /* Just set the flag */
  4462.         pv[SND_ARR].ival = 0;
  4463.         break;
  4464. #endif /* PUTPIPE */
  4465.  
  4466. #ifdef CKSYMLINK
  4467.       case SND_LNK:
  4468.         nolinks = 0;
  4469.         goto again;
  4470.       case SND_NLK:
  4471.         nolinks = 2;
  4472.         goto again;
  4473. #endif /* CKSYMLINK */
  4474.  
  4475. #ifdef FTP_RESTART
  4476.       case SND_RES:            /* /RECOVER (resend) */
  4477.         pv[SND_ARR].ival = 0;    /* fall thru on purpose... */
  4478. #endif /* FTP_RESTART */
  4479.  
  4480.       case SND_NOB:
  4481.       case SND_DEL:            /* /DELETE */
  4482.       case SND_SHH:            /* /QUIET */
  4483.       case SND_UPD:            /* /UPDATE */
  4484.       case SND_SIM:            /* /UPDATE */
  4485.       case SND_USN:            /* /UNIQUE */
  4486.         pv[n].ival = 1;        /* Just set the flag */
  4487.         break;
  4488.  
  4489.       case SND_REC:            /* /RECURSIVE */
  4490.         recursive = 2;        /* Must be set before cmifi() */
  4491.         x_recurse = 1;
  4492.         goto again;
  4493.         break;
  4494.  
  4495. #ifdef UNIXOROSK
  4496.       case SND_DOT:            /* /DOTFILES */
  4497.         matchdot = 1;
  4498.         break;
  4499.       case SND_NOD:            /* /NODOTFILES */
  4500.         matchdot = 0;
  4501.         break;
  4502. #endif /* UNIXOROSK */
  4503.  
  4504.       case SND_ERR:            /* /ERROR-ACTION */
  4505.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  4506.           goto xputx;
  4507.         pv[n].ival = x;
  4508.         break;
  4509.  
  4510.       case SND_EXC:            /* Excludes */
  4511.         if (!getval) break;
  4512.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4513.         if (x == -3) {
  4514.             printf("?Pattern required\n");
  4515.             x = -9;
  4516.         }
  4517.         goto xputx;
  4518.         }
  4519.         if (s) if (!*s) s = NULL;
  4520.         makestr(&(pv[n].sval),s);
  4521.         if (pv[n].sval)
  4522.           pv[n].ival = 1;
  4523.         break;
  4524.  
  4525.       case SND_PRM:            /* /PERMISSIONS */
  4526.         if (!getval)
  4527.           x = 1;
  4528.         else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  4529.           goto xputx;
  4530.         pv[SND_PRM].ival = x;
  4531.         break;
  4532.  
  4533. #ifdef PIPESEND
  4534.       case SND_FLT:            /* /FILTER */
  4535.         debug(F101,"ftp put /filter getval","",getval);
  4536.         if (!getval) break;
  4537.         if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  4538.         if (x == -3)
  4539.           s = "";
  4540.         else
  4541.           goto xputx;
  4542.         }
  4543.         s = brstrip(s);
  4544.         y = strlen(s);
  4545.         for (x = 0; x < y; x++) {    /* Make sure they included "\v(...)" */
  4546.         if (s[x] != '\\') continue;
  4547.         if (s[x+1] == 'v') break;
  4548.         }
  4549.         if (x == y) {
  4550.         printf(
  4551.         "?Filter must contain a replacement variable for filename.\n"
  4552.                );
  4553.         x = -9;
  4554.         goto xputx;
  4555.         }
  4556.         if (s) if (!*s) s = NULL;
  4557.         makestr(&(pv[n].sval),s);
  4558.         if (pv[n].sval)
  4559.           pv[n].ival = 1;
  4560.         break;
  4561. #endif /* PIPESEND */
  4562.  
  4563.       case SND_NAM:            /* /FILENAMES */
  4564.         if (!getval) break;
  4565.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  4566.           goto xputx;
  4567.         debug(F101,"ftp put /filenames","",x);
  4568.         pv[n].ival = x;
  4569.         break;
  4570.  
  4571.       case SND_SMA:            /* Smaller / larger than */
  4572.       case SND_LAR:
  4573.         if (!getval) break;
  4574.         if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  4575.           goto xputx;
  4576.         pv[n].ival = y;
  4577.         break;
  4578.  
  4579.       case SND_FIL:            /* Name of file containing filenames */
  4580.         if (!getval) break;
  4581.         if ((x = cmifi("Name of file containing list of filenames",
  4582.                    "",&s,&y,xxstring)) < 0) {
  4583.         if (x == -3) {
  4584.             printf("?Filename required\n");
  4585.             x = -9;
  4586.         }
  4587.         goto xputx;
  4588.         } else if (y && iswild(s)) {
  4589.         printf("?Wildcards not allowed\n");
  4590.         x = -9;
  4591.         goto xputx;
  4592.         }
  4593.         if (s) if (!*s) s = NULL;
  4594.         makestr(&(pv[n].sval),s);
  4595.         if (pv[n].sval) {
  4596.         pv[n].ival = 1;
  4597.         pv[SND_ARR].ival = 0;
  4598.         } else {
  4599.         pv[n].ival = 0;
  4600.         }
  4601.         mput = 0;
  4602.         break;
  4603.  
  4604.       case SND_MOV:            /* MOVE after */
  4605.       case SND_REN:            /* RENAME after */
  4606.       case SND_SRN: {        /* SERVER-RENAME after */
  4607.           char * m = "";
  4608.           switch (n) {
  4609.         case SND_MOV:
  4610.           m = "device and/or directory for source file after sending";
  4611.           break;
  4612.         case SND_REN:
  4613.           m = "new name for source file after sending";
  4614.           break;
  4615.         case SND_SRN:
  4616.           m = "new name for destination file after sending";
  4617.           break;
  4618.           }
  4619.           if (!getval) break;
  4620.           if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  4621.           if (x == -3) {
  4622.               printf("%s\n", n == SND_MOV ?
  4623.                  "?Destination required" :
  4624.                  "?New name required"
  4625.                  );
  4626.               x = -9;
  4627.           }
  4628.           goto xputx;
  4629.           }
  4630.           if (s) if (!*s) s = NULL;
  4631.           makestr(&(pv[n].sval),brstrip(s));
  4632.           pv[n].ival = (pv[n].sval) ? 1 : 0;
  4633.           break;
  4634.       }
  4635.       case SND_STA:            /* Starting position (= PSEND) */
  4636.         if (!getval) break;
  4637.         if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  4638.           goto xputx;
  4639.         pv[n].ival = y;
  4640.         break;
  4641.  
  4642.       case SND_TYP:            /* /TYPE */
  4643.         if (!getval) break;
  4644.         if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  4645.           goto xputx;
  4646.         pv[n].ival = (x == 2) ? -1 : x;
  4647.         break;
  4648.  
  4649. #ifndef NOCSETS
  4650.       case SND_CSL:            /* Local character set */
  4651.       case SND_CSR:            /* Remote (server) charset */
  4652.         if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  4653.           return((x == -3) ? -2 : x);
  4654.         if (n == SND_CSL)
  4655.           x_csl = x;
  4656.         else
  4657.           x_csr = x;
  4658.         x_xla = 1;            /* Overrides global OFF setting */
  4659.         break;
  4660.  
  4661.       case SND_XPA:            /* Transparent */
  4662.         x_xla = 0;
  4663.         x_csr = -1;
  4664.         x_csl = -1;
  4665.         break;
  4666. #endif /* NOCSETS */
  4667.     }
  4668.     }
  4669. #ifdef PIPESEND
  4670.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  4671.     if (sndfilter || pv[SND_FLT].ival > 0) {
  4672.         printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  4673.         x = -9;
  4674.         goto xputx;
  4675.     }
  4676.     }
  4677. #endif /* PIPESEND */
  4678.  
  4679.     cmarg = "";
  4680.     cmarg2 = asnambuf;
  4681.     line[0] = NUL;
  4682.     s = line;
  4683.     wild = 0;
  4684.  
  4685.     switch (cmresult.fcode) {        /* How did we get out of switch loop */
  4686.       case _CMIFI:            /* Input filename */
  4687.     if (pv[SND_FIL].ival > 0) {
  4688.         printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4689.         x = -9;
  4690.         goto xputx;
  4691.     }
  4692.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  4693.     if (pv[SND_ARR].ival > 0)
  4694.       ckstrncpy(asnambuf,line,CKMAXPATH);
  4695.     else
  4696.       wild = cmresult.nresult;    /* Wild flag */
  4697.     debug(F111,"ftp put wild",line,wild);
  4698.     if (!wild && !recursive && !mput)
  4699.       nolinks = 0;
  4700.     break;
  4701.       case _CMFLD:            /* Field */
  4702.     /* Only allowed with /COMMAND and /ARRAY */
  4703.     if (pv[SND_FIL].ival > 0) {
  4704.         printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4705.         x = -9;
  4706.         goto xputx;
  4707.     }
  4708.     if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  4709. #ifdef CKROOT
  4710.         if (ckrooterr)
  4711.           printf("?Off limits: %s\n",cmresult.sresult);
  4712.         else
  4713. #endif /* CKROOT */
  4714.           printf("?%s - \"%s\"\n",
  4715.            iswild(cmresult.sresult) ?
  4716.            "No files match" : "File not found",
  4717.            cmresult.sresult
  4718.            );
  4719.         x = -9;
  4720.         goto xputx;
  4721.     }
  4722.     ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  4723.     if (pv[SND_ARR].ival > 0)
  4724.       ckstrncpy(asnambuf,line,CKMAXPATH);
  4725.     break;
  4726.       case _CMCFM:            /* Confirmation */
  4727.     confirmed = 1;
  4728.     break;
  4729.       default:
  4730.     printf("?Unexpected function code: %d\n",cmresult.fcode);
  4731.     x = -9;
  4732.     goto xputx;
  4733.     }
  4734.     debug(F110,"ftp put string",s,0);
  4735.     debug(F101,"ftp put confirmed","",confirmed);
  4736.  
  4737.     /* Save and change protocol and transfer mode */
  4738.     /* Global values are restored in main parse loop */
  4739.  
  4740.     g_displa = fdispla;
  4741.     g_skipbup = skipbup;
  4742.  
  4743.     if (pv[SND_NOB].ival > -1) {    /* /NOBACKUP (skip backup file) */
  4744.     g_skipbup = skipbup;
  4745.     skipbup = 1;
  4746.     }
  4747.     if (pv[SND_TYP].ival > -1) {    /* /TYPE */
  4748.     xfiletype = pv[SND_TYP].ival;
  4749.     if (xfiletype == 2)
  4750.       xfiletype = -1;
  4751.     }
  4752.     if (pv[SND_BIN].ival > 0) {        /* /BINARY really means binary... */
  4753.     forcetype = 1;            /* So skip file scan */
  4754.     ftp_typ = FTT_BIN;        /* Set binary */
  4755.     } else if (pv[SND_TXT].ival > 0) {    /* Similarly for /TEXT... */
  4756.     forcetype = 1;
  4757.     ftp_typ = FTT_ASC;
  4758.     } else if (pv[SND_TEN].ival > 0) {    /* and /TENEX*/
  4759.     forcetype = 1;
  4760.     ftp_typ = FTT_TEN;
  4761.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  4762.     forcetype = 1;
  4763.     ftp_typ = binary;
  4764.     g_ftp_typ = binary;
  4765.     }
  4766.  
  4767. #ifdef PIPESEND
  4768.     if (pv[SND_CMD].ival > 0) {        /* /COMMAND - strip any braces */
  4769.     debug(F110,"PUT /COMMAND before stripping",s,0);
  4770.     s = brstrip(s);
  4771.     debug(F110,"PUT /COMMAND after stripping",s,0);
  4772.     if (!*s) {
  4773.         printf("?Sorry, a command to send from is required\n");
  4774.         x = -9;
  4775.         goto xputx;
  4776.     }
  4777.     cmarg = s;
  4778.     }
  4779. #endif /* PIPESEND */
  4780.  
  4781. /* Set up /MOVE and /RENAME */
  4782.  
  4783.     if (pv[SND_DEL].ival > 0 &&
  4784.     (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  4785.     printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  4786.     x = -9;
  4787.     goto xputx;
  4788.     }
  4789. #ifdef CK_TMPDIR
  4790.     if (pv[SND_MOV].ival > 0) {
  4791.     int len;
  4792.     char * p = pv[SND_MOV].sval;
  4793.     len = strlen(p);
  4794.     if (!isdir(p)) {        /* Check directory */
  4795. #ifdef CK_MKDIR
  4796.         char * s = NULL;
  4797.         s = (char *)malloc(len + 4);
  4798.         if (s) {
  4799.         strcpy(s,p);        /* safe */
  4800. #ifdef datageneral
  4801.         if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  4802. #else
  4803.         if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  4804. #endif /* datageneral */
  4805.         s[len++] = 'X';
  4806.         s[len] = NUL;
  4807. #ifdef NOMKDIR
  4808.         x = -1;
  4809. #else
  4810.         x = zmkdir(s);
  4811. #endif /* NOMKDIR */
  4812.         free(s);
  4813.         if (x < 0) {
  4814.             printf("?Can't create \"%s\"\n",p);
  4815.             x = -9;
  4816.             goto xputx;
  4817.         }
  4818.         }
  4819. #else
  4820.         printf("?Directory \"%s\" not found\n",p);
  4821.         x = -9;
  4822.         goto xputx;
  4823. #endif /* CK_MKDIR */
  4824.     }
  4825.     makestr(&snd_move,p);
  4826.     }
  4827. #endif /* CK_TMPDIR */
  4828.  
  4829.     if (pv[SND_REN].ival > 0) {        /* /RENAME */
  4830.     char * p = pv[SND_REN].sval;
  4831.     if (!p) p = "";
  4832.     if (!*p) {
  4833.         printf("?New name required for /RENAME\n");
  4834.         x = -9;
  4835.         goto xputx;
  4836.     }
  4837.     p = brstrip(p);
  4838. #ifndef NOSPL
  4839.     /* If name given is wild, rename string must contain variables */
  4840.     if (wild) {
  4841.         char * s = tmpbuf;
  4842.         x = TMPBUFSIZ;
  4843.         zzstring(p,&s,&x);
  4844.         if (!strcmp(tmpbuf,p)) {
  4845.         printf(
  4846.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  4847.                );
  4848.         x = -9;
  4849.         goto xputx;
  4850.         }
  4851.     }
  4852. #endif /* NOSPL */
  4853.     makestr(&snd_rename,p);
  4854.     debug(F110,"FTP snd_rename",snd_rename,0);
  4855.     }
  4856.     if (pv[SND_SRN].ival > 0) {        /* /SERVER-RENAME */
  4857.     char * p = pv[SND_SRN].sval;
  4858.     if (!p) p = "";
  4859.     if (!*p) {
  4860.         printf("?New name required for /SERVER-RENAME\n");
  4861.         x = -9;
  4862.         goto xputx;
  4863.     }
  4864.     p = brstrip(p);
  4865. #ifndef NOSPL
  4866.     if (wild) {
  4867.         char * s = tmpbuf;
  4868.         x = TMPBUFSIZ;
  4869.         zzstring(p,&s,&x);
  4870.         if (!strcmp(tmpbuf,p)) {
  4871.         printf(
  4872. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  4873.                );
  4874.         x = -9;
  4875.         goto xputx;
  4876.         }
  4877.     }
  4878. #endif /* NOSPL */
  4879.     makestr(&srv_renam,p);
  4880.     debug(F110,"ftp put srv_renam",srv_renam,0);
  4881.     }
  4882.     if (!confirmed) {            /* CR not typed yet, get more fields */
  4883.     char * lp;
  4884.     if (mput) {            /* MPUT or MMOVE */
  4885.         nfils = 0;            /* We already have the first one */
  4886. #ifndef NOMSEND
  4887.         msfiles[nfils++] = line;    /* Store pointer */
  4888.         lp = line + (int)strlen(line) + 1; /* Point past it */
  4889.         debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  4890.         while (1) {            /* Get more filenames */
  4891.         if ((x = cmifi("Names of files to send, separated by spaces",
  4892.                    "", &s,&y,xxstring)) < 0) {
  4893.             if (x != -3)
  4894.               goto xputx;
  4895.             if ((x = cmcfm()) < 0)
  4896.               goto xputx;
  4897.             break;
  4898.         }
  4899.         msfiles[nfils++] = lp;    /* Got one, count it, point to it, */
  4900.         p = lp;            /* remember pointer, */
  4901.         while ((*lp++ = *s++))    /* and copy it into buffer */
  4902.           if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  4903.               printf("?MPUT list too long\n");
  4904.               line[0] = NUL;
  4905.               x = -9;
  4906.               goto xputx;
  4907.           }
  4908.         debug(F111,"ftp put mput nfils",msfiles[nfils-1],nfils-1);
  4909.         if (nfils == 1) fspec[0] = NUL; /* Take care of \v(filespec) */
  4910. #ifdef ZFNQFP
  4911.         zfnqfp(p,TMPBUFSIZ,tmpbuf);
  4912.         p = tmpbuf;
  4913. #endif /* ZFNQFP */
  4914.         if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  4915.             strcat(fspec,p);    /* safe */
  4916.             strcat(fspec," ");    /* safe */
  4917.         } else
  4918. #ifdef COMMENT
  4919.           printf("WARNING - \\v(filespec) buffer overflow\n");
  4920. #else
  4921.           debug(F101,"doxput filespec buffer overflow","",0);
  4922. #endif /* COMMENT */
  4923.         }
  4924. #endif /* NOMSEND */
  4925.     } else {            /* Regular SEND */
  4926.         nfils = -1;
  4927.         if ((x = cmtxt(wild ?
  4928. "\nOptional as-name template containing replacement variables \
  4929. like \\v(filename)" :
  4930.                "Optional name to send it with",
  4931.                "",&p,NULL)) < 0)
  4932.           goto xputx;
  4933.  
  4934.         if (p) if (!*p) p = NULL;
  4935.         p = brstrip(p);
  4936.  
  4937.         if (p && *p) {
  4938.         makestr(&(pv[SND_ASN].sval),p);
  4939.         if (pv[SND_ASN].sval)
  4940.           pv[SND_ASN].ival = 1;
  4941.         debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  4942.         }
  4943.     }
  4944.     }
  4945.     /* Set cmarg2 from as-name, however we got it. */
  4946.  
  4947.     CHECKCONN();
  4948.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  4949.     char * p;
  4950.     p = brstrip(pv[SND_ASN].sval);
  4951.     ckstrncpy(asnambuf,p,CKMAXPATH+1);
  4952.     }
  4953.     debug(F110,"ftp put asnambuf",asnambuf,0);
  4954.  
  4955.     if (pv[SND_FIL].ival > 0) {
  4956.     if (confirmed) {
  4957.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  4958.         debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  4959.         printf("?Failure to open %s\n",pv[SND_FIL].sval);
  4960.         x = -9;
  4961.         goto xputx;
  4962.         }
  4963.         makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  4964.         debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  4965.         wild = 1;
  4966.     }
  4967.     }
  4968.     if (confirmed && !line[0] && !filefile) {
  4969. #ifndef NOMSEND
  4970.     if (filehead) {            /* OK if we have a SEND-LIST */
  4971.         nfils = filesinlist;
  4972.         sndsrc = nfils;        /* Like MSEND */
  4973.         addlist = 1;        /* But using a different list... */
  4974.         filenext = filehead;
  4975.         goto doput;
  4976.     }
  4977. #endif /* NOMSEND */
  4978.     printf("?Filename required but not given\n");
  4979.     x = -9;
  4980.     goto xputx;
  4981.     }
  4982. #ifndef NOMSEND
  4983.     addlist = 0;            /* Don't use SEND-LIST. */
  4984. #endif /* NOMSEND */
  4985.  
  4986.     if (mput) {                /* MPUT (rather than PUT) */
  4987. #ifndef NOMSEND
  4988.     cmlist = msfiles;        /* List of filespecs */
  4989.     sndsrc = nfils;            /* rather filespec and as-name */
  4990. #endif /* NOMSEND */
  4991.     pipesend = 0;
  4992.     } else if (filefile) {        /* File contains list of filenames */
  4993.     s = "";
  4994.     cmarg = "";
  4995.     line[0] = NUL;
  4996.     nfils = 1;
  4997.     sndsrc = 1;
  4998.  
  4999.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5000.  
  5001.     /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5002.     nfils = sndsrc = -1;
  5003.     if (!wild) {
  5004.         y = zchki(s);
  5005.         if (y < 0) {
  5006.         printf("?Read access denied - \"%s\"\n", s);
  5007.         x = -9;
  5008.         goto xputx;
  5009.         }
  5010.     }
  5011.     if (s != line)            /* We might already have done this. */
  5012.       ckstrncpy(line,s,LINBUFSIZ);    /* Copy of string just parsed. */
  5013. #ifdef DEBUG
  5014.     else
  5015.       debug(F110,"doxput line=s",line,0);
  5016. #endif /* DEBUG */
  5017.     cmarg = line;            /* File to send */
  5018.     }
  5019. #ifndef NOMSEND
  5020.     zfnqfp(cmarg,fspeclen,fspec);    /* Get full name */
  5021. #endif /* NOMSEND */
  5022.  
  5023.     if (!mput) {            /* For all but MPUT... */
  5024. #ifdef PIPESEND
  5025.     if (pv[SND_CMD].ival > 0)    /* /COMMAND sets pipesend flag */
  5026.       pipesend = 1;
  5027.     debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5028.     if (pipesend && filefile) {
  5029.         printf("?Invalid switch combination\n");
  5030.         x = -9;
  5031.         goto xputx;
  5032.     }
  5033. #endif /* PIPESEND */
  5034.  
  5035. #ifndef NOSPL
  5036.     /* If as-name given and filespec is wild, as-name must contain variables */
  5037.     if ((wild || mput) && asnambuf[0]) {
  5038.         char * s = tmpbuf;
  5039.         x = TMPBUFSIZ;
  5040.         zzstring(asnambuf,&s,&x);
  5041.         if (!strcmp(tmpbuf,asnambuf)) {
  5042.         printf(
  5043.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5044.                );
  5045.         x = -9;
  5046.         goto xputx;
  5047.         }
  5048.     }
  5049. #endif /* NOSPL */
  5050.     }
  5051.  
  5052.   doput:
  5053.  
  5054.     if (pv[SND_SHH].ival > 0) {        /* SEND /QUIET... */
  5055.     g_displa = fdispla;
  5056.     fdispla = 0;
  5057.     debug(F101,"ftp put display","",fdispla);
  5058.     } else {
  5059.     displa = 1;
  5060.         if (ftp_deb) {
  5061.             g_displa = fdispla;
  5062.             fdispla = XYFD_B;
  5063.         }
  5064.     }
  5065.  
  5066. #ifdef PUTARRAY                /* SEND /ARRAY... */
  5067.     if (pv[SND_ARR].ival > 0) {
  5068.     if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5069.     if (range[0] == -1)        /* If low end of range not specified */
  5070.       range[0] = 1;            /* default to 1 */
  5071.     if (range[1] == -1)        /* If high not specified */
  5072.       range[1] = a_dim[arrayx];    /* default to size of array */
  5073.     if ((range[0] < 0) ||        /* Check range */
  5074.         (range[0] > a_dim[arrayx]) ||
  5075.         (range[1] < range[0]) ||
  5076.         (range[1] > a_dim[arrayx])) {
  5077.         printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5078.         x = -9;
  5079.         goto xputx;
  5080.     }
  5081.     sndarray = ap;            /* Array pointer */
  5082.     sndxin = arrayx;        /* Array index */
  5083.     sndxlo = range[0];        /* Array range */
  5084.     sndxhi = range[1];
  5085.     sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5086.     if (!asnambuf[0])
  5087.       ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5088.     cmarg = "";
  5089.     }
  5090. #endif /* PUTARRAY */
  5091.  
  5092.     moving = 0;
  5093.  
  5094.     if (pv[SND_ARR].ival < 1) {        /* File selection & disposition... */
  5095.     if (pv[SND_DEL].ival > 0)    /* /DELETE was specified */
  5096.       moving = 1;
  5097.     if (pv[SND_AFT].ival > 0)    /* Copy SEND criteria */
  5098.       ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5099.     if (pv[SND_BEF].ival > 0)
  5100.       ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5101.     if (pv[SND_NAF].ival > 0)
  5102.       ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5103.     if (pv[SND_NBE].ival > 0)
  5104.       ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5105.     if (pv[SND_EXC].ival > 0)
  5106.       makelist(pv[SND_EXC].sval,sndexcept,8);
  5107.     if (pv[SND_SMA].ival > -1)
  5108.       sndsmaller = pv[SND_SMA].ival;
  5109.     if (pv[SND_LAR].ival > -1)
  5110.       sndlarger = pv[SND_LAR].ival;
  5111.     if (pv[SND_NAM].ival > -1)
  5112.       x_cnv = pv[SND_NAM].ival;
  5113.     if (pv[SND_USN].ival > -1)
  5114.       x_usn = pv[SND_USN].ival;
  5115.     if (pv[SND_ERR].ival > -1)
  5116.       puterror = pv[SND_ERR].ival;
  5117. #ifdef DOUPDATE
  5118.     if (pv[SND_UPD].ival > 0) {
  5119.         if (x_usn) {
  5120.         printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5121.         x = -9;
  5122.         goto xputx;
  5123.         }
  5124.         putflags |= PUT_UPD;
  5125.     }
  5126. #endif /* DOUPDATE */
  5127.  
  5128.     if (pv[SND_SIM].ival > 0)
  5129.       putflags |= PUT_SIM;
  5130.  
  5131. #ifdef UNIX
  5132.     if (pv[SND_PRM].ival > -1) {
  5133.         if (x_usn) {
  5134.         printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5135.         x = -9;
  5136.         goto xputx;
  5137.         }
  5138.         x_prm = pv[SND_PRM].ival;
  5139.     }
  5140. #endif /* UNIX */
  5141. #ifdef FTP_RESTART
  5142.     if (pv[SND_RES].ival > 0) {
  5143.         if (x_usn || putflags) {
  5144.         printf("?Conflicting switches: /RECOVER %s\n",
  5145.                x_usn && putflags ? "/UNIQUE /UPDATE" :
  5146.                (x_usn ? "/UNIQUE" : "/UPDATE")
  5147.                );
  5148.         x = -9;
  5149.         goto xputx;
  5150.         }
  5151. #ifndef NOCSETS
  5152.         if (x_xla &&
  5153.         (x_csl == FC_UCS2 ||
  5154.          x_csl == FC_UTF8 ||
  5155.          x_csr == FC_UCS2 ||
  5156.          x_csr == FC_UTF8)) {
  5157.         printf("?/RECOVER can not be used with Unicode translation\n");
  5158.         x = -9;
  5159.         goto xputx;
  5160.         }
  5161. #endif /* NOCSETS */
  5162.         putflags = PUT_RES;
  5163.     }
  5164. #endif /* FTP_RESTART */
  5165.     }
  5166.     debug(F101,"ftp PUT restart","",putflags == PUT_RES);
  5167.     debug(F101,"ftp PUT update","",putflags == PUT_UPD);
  5168.  
  5169. #ifdef PIPESEND
  5170.     if (pv[SND_FLT].ival > 0) {        /* Have SEND FILTER? */
  5171.     if (!pv[SND_FLT].sval) {
  5172.         sndfilter = NULL;
  5173.     } else {
  5174.         sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5175.         if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5176.     }
  5177.     debug(F110,"ftp put /FILTER", sndfilter, 0);
  5178.     }
  5179.     if (sndfilter || pipesend)        /* No /UPDATE or /RESTART */
  5180.       if (putflags)            /* with pipes or filters */
  5181.     putflags = 0;
  5182. #endif /* PIPESEND */
  5183.  
  5184.     tfc = 0L;                /* Initialize stats and counters */
  5185.     filcnt = 0;
  5186.     pktnum = 0;
  5187.     spackets = 0L;
  5188.  
  5189.     if (wild)                /* (is this necessary?) */
  5190.       cx = FTP_MPU;
  5191.  
  5192.     t0 = gmstimer();            /* Record starting time */
  5193.  
  5194.     done = 0;                /* Loop control */
  5195.     cancelgroup = 0;
  5196.  
  5197.     cdlevel = 0;
  5198.     cdsimlvl = 0;
  5199.     while (!done && !cancelgroup) {    /* Loop for all files */
  5200.                     /* or until canceled. */
  5201. #ifdef FTP_PROXY
  5202.         /*
  5203.        If we are using a proxy, we don't use the local file list;
  5204.            instead we use the list on the remote machine which we want
  5205.            sent to someone else, and we use remglob() to get the names.
  5206.            But in that case we shouldn't even be executing this routine;
  5207.            see ftp_mput().
  5208.         */
  5209. #endif /* FTP_PROXY */
  5210.  
  5211.     cancelfile = 0;
  5212.     x = gnfile();            /* Get next file from list(s) */
  5213.     if (x == 0)            /* (see gnfile() comments...) */
  5214.       x = gnferror;
  5215.     debug(F111,"FTP PUT gnfile",filnam,x);
  5216.  
  5217.     switch (x) {
  5218.       case 1:            /* File to send */
  5219.         s2 = asnambuf;
  5220. #ifndef NOSPL
  5221.         if (asnambuf[0]) {        /* As-name */
  5222.         int n; char *p;        /* to be evaluated... */
  5223.         n = TMPBUFSIZ;
  5224.         p = tmpbuf;
  5225.         zzstring(asnambuf,&p,&n);
  5226.         s2 = tmpbuf;
  5227.         debug(F110,"ftp put asname",s2,0);
  5228.         }
  5229. #endif /* NOSPL */
  5230.         rc = putfile(cx,        /* Function (PUT, APPEND) */
  5231.             filnam, s2,        /* Name to send, as-name */
  5232.             forcetype, moving,    /* Parameters from switches... */
  5233.             snd_move, snd_rename, srv_renam,
  5234.             x_cnv, x_usn, xfiletype, x_prm,
  5235. #ifndef NOCSETS
  5236.             x_csl, (!x_xla ? -1 : x_csr),
  5237. #else
  5238.             -1, -1,
  5239. #endif /* NOCSETS */
  5240.             putflags
  5241.             );
  5242.         debug(F111,"ftp put putfile rc",filnam,rc);
  5243.         debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  5244.         debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  5245.         if (rc > -1) {
  5246.         good++;
  5247.         status = 1;
  5248.         }
  5249.         if (cancelfile)
  5250.           continue;
  5251.         if (rc < 0) {
  5252.         ftp_fai++;
  5253.         if (puterror) {
  5254.             status = 0;
  5255.             printf("?Fatal upload error: %s\n",filnam);
  5256.             done++;
  5257.         }
  5258.         }
  5259.         continue;
  5260.       case 0:            /* No more files, done */
  5261.         done++;
  5262.         continue;
  5263.       case -1:
  5264.         printf("?%s: file not found - \"%s\"\n",
  5265.            puterror ? "Fatal" : "Warning",
  5266.            filnam
  5267.            );
  5268.         if (puterror) {
  5269.         status = 0;
  5270.         done++;
  5271.         break;
  5272.         }
  5273.         continue;
  5274.       case -2:
  5275.         if (puterror) {
  5276.         printf("?Fatal: file not found - \"%s\"\n", filnam);
  5277.         status = 0;
  5278.         done++;
  5279.         break;
  5280.         }
  5281.         continue;            /* Not readable, keep going */
  5282.       case -3:
  5283.         if (puterror) {
  5284.         printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  5285.         status = 0;
  5286.         done++;
  5287.         break;
  5288.         }
  5289.         printf("?Warning access denied - \"%s\"\n", filnam);
  5290.         continue;
  5291. #ifdef COMMENT
  5292.       case -4:            /* Canceled */
  5293.         done++;
  5294.         break;
  5295. #endif /* COMMENT */
  5296.       case -5:
  5297.         printf("?Too many files match\n");
  5298.         done++;
  5299.         break;
  5300.       case -6:
  5301.         if (good < 1)
  5302.           printf("?No files selected\n");
  5303.         done++;
  5304.         break;
  5305.       default:
  5306.         printf("?getnextfile() - unknown failure\n");
  5307.         done++;
  5308.     }
  5309.     }
  5310.     if (cdlevel > 0) {
  5311.     while (cdlevel--) {
  5312.             if (cdsimlvl) {
  5313.                 cdsimlvl--;
  5314.             } else if (!doftpcdup())
  5315.           break;
  5316.         }
  5317.     }
  5318.     if (status > 0) {
  5319.     if (cancelgroup)
  5320.       status = 0;
  5321.     else if (cancelfile && good < 1)
  5322.       status = 0;
  5323.     }
  5324.     success = status;
  5325.     x = success;
  5326.  
  5327.   xputx:
  5328.     if (x > -1) {
  5329. #ifdef GFTIMER
  5330.     t1 = gmstimer();        /* End time */
  5331.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5332.     if (!sec) sec = 0.001;
  5333.     fptsecs = sec;
  5334. #else
  5335.     sec = (t1 - t0) / 1000;
  5336.     if (!sec) sec = 1;
  5337. #endif /* GFTIMER */
  5338.     tfcps = (long) (tfc / sec);
  5339.     tsecs = (int)sec;
  5340.     lastxfer = W_FTP|W_SEND;
  5341.     xferstat = success;
  5342.     if (dpyactive)
  5343.       ftscreen(SCR_TC,0,0L,"");
  5344.     }
  5345.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  5346.     if (pv[i].sval)
  5347.       free(pv[i].sval);
  5348.     }
  5349.     ftreset();                /* Undo switch effects */
  5350.     dpyactive = 0;
  5351.     return(x);
  5352. }
  5353.  
  5354.  
  5355. static char ** mgetlist = NULL;        /* For MGET */
  5356. static int mgetn = 0, mgetx = 0;
  5357. static char xtmpbuf[4096];
  5358.  
  5359. /*
  5360.   c m d l i n g e t
  5361.  
  5362.   Get files specified by -g command-line option.
  5363.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  5364. */
  5365. int
  5366. cmdlinget(stay) int stay; {
  5367.     extern int fncnv;
  5368.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  5369.     int lcs = -1, rcs = -1, xlate = 0;
  5370.     int first = 1;
  5371.     int mget = 1;
  5372.     int nc;
  5373.     char * s, * s2, * s3;
  5374.     ULONG t0, t1;            /* Times for stats */
  5375. #ifdef GFTIMER
  5376.     CKFLOAT sec;
  5377. #else
  5378.     int sec = 0;
  5379. #endif /* GFTIMER */
  5380.  
  5381.     if (quiet) {            /* -q really means quiet */
  5382.     displa = 0;
  5383.     fdispla = 0;
  5384.     } else {
  5385.     displa = 1;
  5386.     fdispla = XYFD_B;
  5387.     }
  5388.     testing = 0;
  5389.     dpyactive = 0;
  5390.     out2screen = 0;
  5391.     what = W_FTP|W_RECV;
  5392.  
  5393. #ifndef NOSPL
  5394.     cmd_quoting = 0;
  5395. #endif /* NOSPL */
  5396.     debug(F101,"ftp cmdlinget nfils","",nfils);
  5397.  
  5398.     if (ftp_cnv == SET_AUTO) {        /* Name conversion is auto */
  5399.     if (alike) {             /* If server & client are alike */
  5400.         nc = 0;            /* no conversion */
  5401.     } else {            /* If they are different */
  5402.         if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  5403.           nc = -1;            /* only minimal conversions needed */
  5404.         else            /* otherwise */
  5405.           nc = 1;            /* full conversion */
  5406.     }
  5407.     } else                /* Not auto - do what user said */
  5408.       nc = ftp_cnv;
  5409.  
  5410.     if (nfils < 1)
  5411.       doexit(BAD_EXIT,-1);
  5412.  
  5413.     t0 = gmstimer();            /* Starting time for this batch */
  5414.  
  5415. #ifndef NOCSETS
  5416.     if (xlate) {            /* SET FTP CHARACTER-SET-TRANSLATION */
  5417.     lcs = ftp_csl;            /* Local charset */
  5418.     if (lcs < 0) lcs = fcharset;
  5419.     if (lcs < 0) xlate = 0;
  5420.     }
  5421.     if (xlate) {            /* Still ON? */
  5422.     rcs = ftp_csx;            /* Remote (Server) charset */
  5423.     if (rcs < 0) rcs = ftp_csr;
  5424.     if (rcs < 0) xlate = 0;
  5425.     }
  5426. #endif /* NOCSETS */
  5427.     /*
  5428.       If we have only one file and it is a directory, then we ask for a
  5429.       listing of its contents, rather than retrieving the directory file
  5430.       itself.  This is what (e.g.) Netscape does.
  5431.     */
  5432.     if (nfils == 1) {
  5433.     if (doftpcwd((CHAR *)cmlist[mgetx],-1)) {
  5434.         /* If we can CD to it, it must be a directory */
  5435.         if (recursive) {
  5436.         cmlist[mgetx] = "*";
  5437.         } else {
  5438.         status =
  5439.           (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  5440.         done = 1;
  5441.         }
  5442.         }
  5443.     }
  5444. /*
  5445.   The following is to work around UNIX servers which, when given a command
  5446.   like "NLST path/blah" (not wild) returns the basename without the path.
  5447. */
  5448.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  5449.     mget = iswild(cmlist[mgetx]);
  5450.     }
  5451.     if (!mget && !done) {        /* Invoked by command-line FTP URL */
  5452.     if (ftp_deb)
  5453.       printf("DOING GET...\n");
  5454.     done++;
  5455.     cancelfile = 0;            /* This file not canceled yet */
  5456.         s = cmlist[mgetx];
  5457.     rc = 0;                /* Initial return code */
  5458.     x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5459.     fsize = -1L;
  5460.     if (x == REPLY_COMPLETE)
  5461.       fsize = atol(&ftp_reply_str[4]);
  5462.     ckstrncpy(filnam,s,CKMAXPATH);    /* For \v(filename) */
  5463.     debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5464.  
  5465.     nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5466.     s2 = tmpbuf;
  5467.  
  5468.     /* If local file already exists, take collision action */
  5469.  
  5470.     x = zchki(s2);
  5471.     if (x > -1) {
  5472.         switch (fncnv) {
  5473.           case XYFX_A:        /* Append */
  5474.         append = 1;
  5475.         break;
  5476.           case XYFX_R:        /* Rename */
  5477.           case XYFX_B: {        /* Backup */
  5478.           char * p = NULL;
  5479.           int x = -1;
  5480.           znewn(s2,&p);        /* Make unique name */
  5481.           debug(F110,"ftp get znewn",p,0);
  5482.           if (fncnv == XYFX_B) { /* Backup existing file */
  5483.               x = zrename(s2,p);
  5484.               debug(F111,"ftp get backup zrename",p,x);
  5485.           } else {        /* Rename incoming file */
  5486.               x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5487.               s2 = tmpbuf;
  5488.               debug(F111,"ftp get rename incoming",p,x);
  5489.           }
  5490.           if (x < 0) {
  5491.               printf("?Backup/Rename failed\n");
  5492.               return(success = 0);
  5493.           }
  5494.           break;
  5495.           }
  5496.           case XYFX_D:        /* Discard */
  5497.         ftscreen(SCR_FN,'F',0L,s);
  5498.         ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5499.         tlog(F100," refused: name","",0);
  5500.         debug(F110,"ftp get skip name",s2,0);
  5501.                 goto xclget;
  5502.  
  5503.           case XYFX_X:        /* Overwrite */
  5504.           case XYFX_U:        /* Update (already handled above) */
  5505.         break;
  5506.         }
  5507.     }
  5508.     rc = getfile(s,            /* Remote name */
  5509.              s2,        /* Local name */
  5510.              0,            /* Recover/Restart */
  5511.              append,        /* Append */
  5512.              NULL,        /* Pipename */
  5513.              0,            /* Translate charsets */
  5514.              -1,        /* File charset (none) */
  5515.              -1            /* Server charset (none) */
  5516.              );
  5517.     debug(F111,"ftp get rc",s,rc);
  5518.     debug(F111,"ftp get cancelfile",s,cancelfile);
  5519.     debug(F111,"ftp get cancelgroup",s,cancelgroup);
  5520.  
  5521.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  5522.             rc = getfile(&s[1],        /* Remote name without leading '/' */
  5523.                          s2,        /* Local name */
  5524.                          0,            /* Recover/Restart */
  5525.                          append,     /* Append */
  5526.                          NULL,        /* Pipename */
  5527.                          0,         /* Translate charsets */
  5528.                          -1,        /* File charset (none) */
  5529.                          -1         /* Server charset (none) */
  5530.                          );
  5531.     if (rc > -1) {
  5532.         good++;
  5533.         status = 1;
  5534.     }
  5535.     if (cancelfile)
  5536.           goto xclget;
  5537.     if (rc < 0) {
  5538.         ftp_fai++;
  5539.         if (geterror) {
  5540.         status = 0;
  5541.         done++;
  5542.         }
  5543.     }
  5544.     }
  5545.     if (ftp_deb && !done)
  5546.       printf("DOING MGET...\n");
  5547.     while (!done && !cancelgroup) {
  5548.     cancelfile = 0;            /* This file not canceled yet */
  5549.     s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],0);
  5550.     if (!s) s = "";
  5551.     if (!*s) {
  5552.         first = 1;
  5553.         mgetx++;
  5554.         if (mgetx < nfils)
  5555.           s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],0);
  5556.         else
  5557.           s = NULL;
  5558.         debug(F111,"ftp mget remote_files B",s,0);
  5559.         if (!s) {
  5560.         done = 1;
  5561.         break;
  5562.         }
  5563.     }
  5564.  
  5565.     /*
  5566.       The semantics of NLST are ill-defined.  Suppose we have just sent
  5567.       NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  5568.       /path/bar, etc.  But some send back only foo and bar, and subsequent
  5569.       RETR commands based on the pathless names are not going to work.
  5570.     */
  5571.     if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  5572.         if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  5573.         int len, left = 4096;
  5574.         char * tmp = xtmpbuf;
  5575.         len = s3 - cmlist[mgetx] + 1;
  5576.         ckstrncpy(tmp,cmlist[mgetx],left);
  5577.         tmp += len;
  5578.         left -= len;
  5579.         ckstrncpy(tmp,s,left);
  5580.         s = xtmpbuf;
  5581.         }
  5582.     }
  5583.     first = 0;            /* Not first any more */
  5584.  
  5585.     rc = 0;                /* Initial return code */
  5586.     x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5587.     fsize = -1L;
  5588.     if (x == REPLY_COMPLETE)
  5589.       fsize = atol(&ftp_reply_str[4]);
  5590.     ckstrncpy(filnam,s,CKMAXPATH);    /* For \v(filename) */
  5591.     debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5592.  
  5593.     nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5594.     s2 = tmpbuf;
  5595.  
  5596.     /* If local file already exists, take collision action */
  5597.  
  5598.     x = zchki(s2);
  5599.     if (x > -1) {
  5600.         switch (fncnv) {
  5601.           case XYFX_A:        /* Append */
  5602.         append = 1;
  5603.         break;
  5604.           case XYFX_R:        /* Rename */
  5605.           case XYFX_B: {        /* Backup */
  5606.           char * p = NULL;
  5607.           int x = -1;
  5608.           znewn(s2,&p);        /* Make unique name */
  5609.           debug(F110,"ftp get znewn",p,0);
  5610.           if (fncnv == XYFX_B) { /* Backup existing file */
  5611.               x = zrename(s2,p);
  5612.               debug(F111,"ftp get backup zrename",p,x);
  5613.           } else {        /* Rename incoming file */
  5614.               x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5615.               s2 = tmpbuf;
  5616.               debug(F111,"ftp get rename incoming",p,x);
  5617.           }
  5618.           if (x < 0) {
  5619.               printf("?Backup/Rename failed\n");
  5620.               return(success = 0);
  5621.           }
  5622.           break;
  5623.           }
  5624.           case XYFX_D:    /* Discard */
  5625.         ftscreen(SCR_FN,'F',0L,s);
  5626.         ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5627.         tlog(F100," refused: name","",0);
  5628.         debug(F110,"ftp get skip name",s2,0);
  5629.                 continue;
  5630.           case XYFX_X:        /* Overwrite */
  5631.           case XYFX_U:        /* Update (already handled above) */
  5632.         break;
  5633.         }
  5634.     }
  5635.                     /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  5636.     rc = getfile(s,            /* Remote name */
  5637.              s2,        /* Local name */
  5638.              0,            /* Recover/Restart */
  5639.              append,        /* Append */
  5640.              NULL,        /* Pipename */
  5641.              0,            /* Translate charsets */
  5642.              -1,        /* File charset (none) */
  5643.              -1            /* Server charset (none) */
  5644.              );
  5645.     debug(F111,"ftp get rc",s,rc);
  5646.     debug(F111,"ftp get cancelfile",s,cancelfile);
  5647.     debug(F111,"ftp get cancelgroup",s,cancelgroup);
  5648.  
  5649.     if (rc > -1) {
  5650.         good++;
  5651.         status = 1;
  5652.     }
  5653.     if (cancelfile)
  5654.       continue;
  5655.     if (rc < 0) {
  5656.         ftp_fai++;
  5657.         if (geterror) {
  5658.         status = 0;
  5659.         done++;
  5660.         }
  5661.     }
  5662.     }
  5663.  
  5664.   xclget:
  5665.     if (status > 0) {
  5666.     if (cancelgroup)
  5667.       status = 0;
  5668.     else if (cancelfile && good < 1)
  5669.       status = 0;
  5670.     }
  5671.     success = status;
  5672.  
  5673. #ifdef GFTIMER
  5674.     t1 = gmstimer();            /* End time */
  5675.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5676.     if (!sec) sec = 0.001;
  5677.     fptsecs = sec;
  5678. #else
  5679.     sec = (t1 - t0) / 1000;
  5680.     if (!sec) sec = 1;
  5681. #endif /* GFTIMER */
  5682.  
  5683.     tfcps = (long) (tfc / sec);
  5684.     tsecs = (int)sec;
  5685.     lastxfer = W_FTP|W_RECV;
  5686.     xferstat = success;
  5687.     if (dpyactive)
  5688.       ftscreen(SCR_TC,0,0L,"");
  5689.     if (!stay)
  5690.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  5691.     return(success);
  5692. }
  5693.  
  5694. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  5695.  
  5696. /*
  5697.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  5698.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  5699.   the user-interface perspective, since the server's directory listings show
  5700.   its own local times and since we don't know what timezone it's in, there's
  5701.   no way to reconcile our local times with the server's.
  5702. */
  5703. int
  5704. doftpget(cx,who) int cx, who; {        /* who == 1 for ftp, 0 for kermit */
  5705.     struct FDB fl, sw, cm;
  5706.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  5707.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  5708.     int x_fnc = 0, first = 0, skipthis = 0, append = 0;
  5709.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  5710.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  5711.     long getlarger = -1, getsmaller = -1;
  5712.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  5713.     char * src = "", * local = "";
  5714.  
  5715.     int x_csl = -1, x_csr = -1;        /* Local and remote charsets */
  5716.     int x_xla = 0;
  5717.     char c;                /* Worker char */
  5718.     ULONG t0 = 0L, t1;            /* Times for stats */
  5719. #ifdef GFTIMER
  5720.     CKFLOAT sec;
  5721. #else
  5722.     int sec = 0;
  5723. #endif /* GFTIMER */
  5724.  
  5725.     struct stringint {            /* Temporary array for switch values */
  5726.     char * sval;
  5727.     int ival;
  5728.     } pv[SND_MAX+1];
  5729.  
  5730.     success = 0;            /* Assume failure */
  5731.     forcetype = 0;            /* No /TEXT or /BINARY given yet */
  5732.     restart = 0;            /* No restart yet */
  5733.     out2screen = 0;
  5734.     x_cnv = ftp_cnv;            /* Filename conversion */
  5735.     if (x_cnv == SET_AUTO)        /* Auto? */
  5736.       x_cnv = alike ? 0 : 1;        /* No conversion if alike */
  5737.  
  5738.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  5739.     x_prm = ftp_prm;            /* Permissions */
  5740.     if (x_prm == SET_AUTO)        /* Permissions AUTO */
  5741.       x_prm = alike;
  5742.  
  5743. #ifndef NOCSETS
  5744.     x_csr = ftp_csr;            /* Inherit global server charset */
  5745.     x_csl = ftp_csl;            /* Inherit global local charset */
  5746.     if (x_csl < 0)            /* If none, use current */
  5747.       x_csl = fcharset;            /* file character-set. */
  5748.     x_xla = ftp_xla;            /* Translation On/Off */
  5749. #endif /* NOCSETS */
  5750.  
  5751.     geterror = ftp_err;            /* Inherit global error action. */
  5752.     asnambuf[0] = NUL;            /* No as-name yet. */
  5753.     pipesave = pipesend;
  5754.     pipesend = 0;
  5755.  
  5756.     if (g_ftp_typ > -1) {        /* Restore TYPE if saved */
  5757.     ftp_typ = g_ftp_typ;
  5758.     /* g_ftp_typ = -1; */
  5759.     }
  5760.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  5761.     pv[i].sval = NULL;        /* to null pointers */
  5762.     pv[i].ival = -1;        /* and -1 int values */
  5763.     }
  5764.     zclose(ZMFILE);            /* In case it was left open */
  5765.  
  5766.     if (fp_nml) {            /* Reset /NAMELIST */
  5767.     if (fp_nml != stdout)
  5768.       fclose(fp_nml);
  5769.     fp_nml = NULL;
  5770.     }
  5771.     makestr(&ftp_nml,NULL);
  5772.  
  5773.     /* Initialize list of remote filespecs */
  5774.  
  5775.     if (!mgetlist) {
  5776.     mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  5777.     if (!mgetlist) {
  5778.         printf("?Memory allocation failure - MGET list\n");
  5779.         return(-9);
  5780.     }
  5781.     for (i = 0; i < MGETMAX; i++)
  5782.       mgetlist[i] = NULL;
  5783.     }
  5784.     mgetn = 0;                /* Items in list */
  5785.     mgetx = 0;                /* Current item */
  5786.  
  5787.     ftp_knf = ftp_deb;            /* Keep NLST file? */
  5788.  
  5789.     if (who == 0) {            /* Called with unprefixed command */
  5790.     if (cx == XXGET || cx == XXREGET)
  5791.       getone++;
  5792.     switch (cx) {
  5793.       case XXREGET: pv[SND_RES].ival = 1; break;
  5794.       case XXRETR:  pv[SND_DEL].ival = 1; break;
  5795.       case XXGET:
  5796.       case XXMGET:  mget++; break;
  5797.     }
  5798.     } else {                /* FTP command */
  5799.     if (cx == FTP_GET || cx == FTP_RGE)
  5800.       getone++;
  5801.     switch (cx) {
  5802.       case FTP_DEL:            /* (fall thru on purpose) */
  5803.       case FTP_MDE: mdel++;        /* (ditto) */
  5804.       case FTP_GET:            /* (ditto) */
  5805.       case FTP_MGE: mget++; break;
  5806.       case FTP_RGE: pv[SND_RES].ival = 1; break;
  5807.     }
  5808.     }
  5809.     cmfdbi(&sw,                /* First FDB - command switches */
  5810.        _CMKEY,            /* fcode */
  5811.        "Remote filename;\n or switch", /* hlpmsg */
  5812.        "",                /* default */
  5813.        "",                /* addtl string data */
  5814.        mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  5815.        4,                /* addtl numeric data 2: 4 = cmswi */
  5816.        xxstring,            /* Processing function */
  5817.        mdel ? delswi : getswi,    /* Keyword table */
  5818.        &fl                /* Pointer to next FDB */
  5819.        );
  5820.     cmfdbi(&fl,                /* 2nd FDB - remote filename */
  5821.        _CMFLD,            /* fcode */
  5822.        "",                /* hlpmsg */
  5823.        "",                /* default */
  5824.        "",                /* addtl string data */
  5825.        0,                /* addtl numeric data 1 */
  5826.        0,                /* addtl numeric data 2 */
  5827.        xxstring,
  5828.        NULL,
  5829.        &cm
  5830.        );
  5831.     cmfdbi(&cm,                /* 3rd FDB - Confirmation */
  5832.        _CMCFM,            /* fcode */
  5833.        "",                /* hlpmsg */
  5834.        "",                /* default */
  5835.        "",                /* addtl string data */
  5836.        0,                /* addtl numeric data 1 */
  5837.        0,                /* addtl numeric data 2 */
  5838.        NULL,
  5839.        NULL,
  5840.        NULL
  5841.        );
  5842.  
  5843.     while (1) {                /* Parse 0 or more switches */
  5844.     x = cmfdb(&sw);            /* Parse something */
  5845.     debug(F101,"ftp get cmfdb","",x);
  5846.     if (x < 0)            /* Error */
  5847.       goto xgetx;            /* or reparse needed */
  5848.     if (cmresult.fcode != _CMKEY)    /* Break out of loop if not a switch */
  5849.       break;
  5850.     c = cmgbrk();            /* Get break character */
  5851.     getval = (c == ':' || c == '='); /* to see how they ended the switch */
  5852.     if (getval && !(cmresult.kflags & CM_ARG)) {
  5853.         printf("?This switch does not take arguments\n");
  5854.         x = -9;
  5855.         goto xgetx;
  5856.     }
  5857.     n = cmresult.nresult;        /* Numeric result = switch value */
  5858.     debug(F101,"ftp get switch","",n);
  5859.  
  5860.     if (!getval && (cmgkwflgs() & CM_ARG)) {
  5861.         printf("?This switch requires an argument\n");
  5862.         x = -9;
  5863.         goto xgetx;
  5864.     }
  5865.     switch (n) {            /* Process the switch */
  5866.       case SND_ASN:            /* /AS-NAME: */
  5867.         debug(F101,"ftp get /as-name getval","",getval);
  5868.         if (!getval) break;
  5869.         if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  5870.         if (x == -3) {
  5871.             printf("?name required\n");
  5872.             x = -9;
  5873.         }
  5874.         goto xgetx;
  5875.         }
  5876.         s = brstrip(s);
  5877.         if (!*s) s = NULL;
  5878.         makestr(&(pv[n].sval),s);
  5879.         pv[n].ival = 1;
  5880.         break;
  5881.  
  5882.       case SND_BIN:            /* /BINARY */
  5883.       case SND_TXT:            /* /TEXT or /ASCII */
  5884.       case SND_TEN:            /* /TENEX */
  5885.         pv[SND_BIN].ival = 0;
  5886.         pv[SND_TXT].ival = 0;
  5887.         pv[SND_TEN].ival = 0;
  5888.         pv[n].ival = 1;
  5889.         break;
  5890.  
  5891. #ifdef PUTPIPE
  5892.       case SND_CMD:            /* These take no args */
  5893.         if (nopush) {
  5894.         printf("?Sorry, system command access is disabled\n");
  5895.         x = -9;
  5896.         goto xgetx;
  5897.         }
  5898. #ifdef PIPESEND
  5899.         else if (rcvfilter) {
  5900.         printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  5901.         x = -9;
  5902.         goto xgetx;
  5903.         }
  5904. #endif /* PIPESEND */
  5905.         sw.hlpmsg = "Command, or switch"; /* Change help message */
  5906.         pv[n].ival = 1;        /* Just set the flag */
  5907.         pv[SND_ARR].ival = 0;
  5908.         break;
  5909. #endif /* PUTPIPE */
  5910.  
  5911.       case SND_SHH:            /* /QUIET */
  5912.       case SND_RES:            /* /RECOVER (reget) */
  5913.       case SND_NOB:            /* /NOBACKUPFILES */
  5914.       case SND_DEL:            /* /DELETE */
  5915.       case SND_UPD:            /* /UPDATE */
  5916.       case SND_USN:            /* /UNIQUE */
  5917.       case SND_NOD:            /* /NODOTFILES */
  5918.       case SND_REC:            /* /NODOTFILES */
  5919.       case SND_MAI:            /* /TO-SCREEN */
  5920.         pv[n].ival = 1;        /* Just set the flag */
  5921.         break;
  5922.  
  5923.       case SND_COL:            /* /COLLISION: */
  5924.         if ((x = cmkey(colxtab,ncolx,"","",xxstring)) < 0)
  5925.           goto xgetx;
  5926.         pv[n].ival = x;
  5927.         break;
  5928.  
  5929.       case SND_ERR:            /* /ERROR-ACTION */
  5930.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  5931.           goto xgetx;
  5932.         pv[n].ival = x;
  5933.         break;
  5934.  
  5935.       case SND_EXC:            /* Exception list */
  5936.         if (!getval) break;
  5937.         if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5938.         if (x == -3) {
  5939.             printf("?Pattern required\n");
  5940.             x = -9;
  5941.         }
  5942.         goto xgetx;
  5943.         }
  5944.         if (s) if (!*s) s = NULL;
  5945.         makestr(&(pv[n].sval),s);
  5946.         if (pv[n].sval)
  5947.           pv[n].ival = 1;
  5948.         break;
  5949.  
  5950. #ifdef PIPESEND
  5951.       case SND_FLT:
  5952.         debug(F101,"ftp get /filter getval","",getval);
  5953.         if (!getval) break;
  5954.         if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  5955.         if (x == -3)
  5956.           s = "";
  5957.         else
  5958.           goto xgetx;
  5959.         }
  5960.         s = brstrip(s);
  5961.         if (pv[SND_MAI].ival < 1) {
  5962.         y = strlen(s);
  5963.         /* Make sure they included "\v(...)" */
  5964.         for (x = 0; x < y; x++) {
  5965.             if (s[x] != '\\') continue;
  5966.             if (s[x+1] == 'v') break;
  5967.         }
  5968.         if (x == y) {
  5969.             printf(
  5970.         "?Filter must contain a replacement variable for filename.\n"
  5971.                );
  5972.             x = -9;
  5973.             goto xgetx;
  5974.         }
  5975.         }
  5976.         if (*s) {
  5977.         pv[n].ival = 1;
  5978.         makestr(&(pv[n].sval),s);
  5979.         } else {
  5980.         pv[n].ival = 0;
  5981.         makestr(&(pv[n].sval),NULL);
  5982.         }
  5983.         break;
  5984. #endif /* PIPESEND */
  5985.  
  5986.       case SND_NAM:
  5987.         if (!getval) break;
  5988.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  5989.           goto xgetx;
  5990.         debug(F101,"ftp get /filenames","",x);
  5991.         pv[n].ival = x;
  5992.         break;
  5993.  
  5994.       case SND_SMA:            /* Smaller / larger than */
  5995.       case SND_LAR:
  5996.         if (!getval) break;
  5997.         if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  5998.           goto xgetx;
  5999.         pv[n].ival = y;
  6000.         break;
  6001.  
  6002.       case SND_FIL:            /* Name of file containing filnames */
  6003.         if (!getval) break;
  6004.         if ((x = cmifi("Name of file containing list of filenames",
  6005.                    "",&s,&y,xxstring)) < 0) {
  6006.         if (x == -3) {
  6007.             printf("?Filename required\n");
  6008.             x = -9;
  6009.         }
  6010.         goto xgetx;
  6011.         } else if (y && iswild(s)) {
  6012.         printf("?Wildcards not allowed BBB\n");
  6013.         x = -9;
  6014.         goto xgetx;
  6015.         }
  6016.         if (s) if (!*s) s = NULL;
  6017.         makestr(&(pv[n].sval),s);
  6018.         if (pv[n].sval)
  6019.           pv[n].ival = 1;
  6020.         break;
  6021.  
  6022.       case SND_MOV:            /* MOVE after */
  6023.       case SND_REN:            /* RENAME after */
  6024.           case SND_SRN: {        /* SERVER-RENAME */
  6025.           char * m = "";
  6026.           switch (n) {
  6027.         case SND_MOV:
  6028.           m =
  6029.            "Device and/or directory for incoming file after reception";
  6030.           break;
  6031.         case SND_REN:
  6032.           m = "New name for incoming file after reception";
  6033.           break;
  6034.         case SND_SRN:
  6035.           m = "New name for source file on server after reception";
  6036.           break;
  6037.           }
  6038.           if (!getval) break;
  6039.           if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6040.           if (x == -3) {
  6041.               printf("%s\n", n == SND_MOV ?
  6042.                  "?Destination required" :
  6043.                  "?New name required"
  6044.                  );
  6045.               x = -9;
  6046.           }
  6047.           goto xgetx;
  6048.           }
  6049.           makestr(&(pv[n].sval),brstrip(s));
  6050.           pv[n].ival = (pv[n].sval) ? 1 : 0;
  6051.           break;
  6052.       }
  6053. #ifndef NOCSETS
  6054.       case SND_CSL:            /* Local character set */
  6055.       case SND_CSR:            /* Remote (server) charset */
  6056.         if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6057.           return((x == -3) ? -2 : x);
  6058.         if (n == SND_CSL)
  6059.           x_csl = x;
  6060.         else
  6061.           x_csr = x;
  6062.         x_xla = 1;            /* Overrides global OFF setting */
  6063.         break;
  6064.  
  6065.       case SND_XPA:            /* Transparent */
  6066.         x_xla =  0;
  6067.         x_csr = -1;
  6068.         x_csl = -1;
  6069.         break;
  6070. #endif /* NOCSETS */
  6071.  
  6072.       case SND_NML:
  6073.         if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6074.           goto xgetx;
  6075.         makestr(&ftp_nml,s);
  6076.         break;
  6077.  
  6078.       default:            /* /AFTER, /PERMISSIONS, etc... */
  6079.         printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6080.         x = -9;
  6081.         goto xgetx;
  6082.     }
  6083.     }
  6084.     line[0] = NUL;
  6085.     cmarg = line;
  6086.     cmarg2 = asnambuf;
  6087.     s = line;
  6088. /*
  6089.   For GET, we want to parse an optional as-name, like with PUT.
  6090.   For MGET, we must parse a list of names, and then send NLST commands
  6091.   for each name separately.
  6092. */
  6093.     switch (cmresult.fcode) {        /* How did we get out of switch loop */
  6094.       case _CMFLD:            /* Field */
  6095.     if (!getone) {
  6096.         s = brstrip(cmresult.sresult);
  6097.         makestr(&(mgetlist[mgetn++]),s);
  6098.         while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6099.         if (x < 0)
  6100.           goto xgetx;
  6101.         makestr(&(mgetlist[mgetn++]),brstrip(s));
  6102.         if (mgetn >= MGETMAX) {
  6103.             printf("?Too many items in MGET list\n");
  6104.             goto xgetx;
  6105.         }
  6106.         }
  6107.         if ((x = cmcfm()) < 0)
  6108.           goto xgetx;
  6109.     } else {
  6110.         s = brstrip(cmresult.sresult);
  6111.         ckstrncpy(line,s,LINBUFSIZ);
  6112.         if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6113.           if (x != -3)
  6114.         goto xgetx;
  6115.         s = brstrip(s);
  6116.         ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6117.         if ((x = cmcfm()) < 0)
  6118.           goto xgetx;
  6119.     }
  6120.     break;
  6121.       case _CMCFM:            /* Confirmation */
  6122.     break;
  6123.       default:
  6124.     printf("?Unexpected function code: %d\n",cmresult.fcode);
  6125.     x = -9;
  6126.     goto xgetx;
  6127.     }
  6128.     if (pv[SND_REC].ival > 0)        /* /RECURSIVE */
  6129.       recursive = 2;
  6130.  
  6131.     if (pv[SND_BIN].ival > 0) {        /* /BINARY really means binary... */
  6132.     forcetype = 1;            /* So skip file scan */
  6133.     ftp_typ = XYFT_B;        /* Set binary */
  6134.     } else if (pv[SND_TXT].ival > 0) {    /* Similarly for /TEXT... */
  6135.     forcetype = 1;
  6136.     ftp_typ = XYFT_T;
  6137.     } else if (pv[SND_TEN].ival > 0) {    /* and /TENEX*/
  6138.     forcetype = 1;
  6139.     ftp_typ = FTT_TEN;
  6140.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  6141.     forcetype = 1;
  6142.     ftp_typ = binary;
  6143.     g_ftp_typ = binary;
  6144.     }
  6145.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  6146.     char * p;
  6147.     p = brstrip(pv[SND_ASN].sval);    /* As-name */
  6148.     ckstrncpy(asnambuf,p,CKMAXPATH+1);
  6149.     }
  6150.     debug(F110,"ftp get asnambuf",asnambuf,0);
  6151.  
  6152. #ifdef PIPESEND
  6153.     if (pv[SND_CMD].ival > 0) {        /* /COMMAND - strip any braces */
  6154.     char * p;
  6155.     p = asnambuf;
  6156.     debug(F110,"GET /COMMAND before stripping",p,0);
  6157.     p = brstrip(p);
  6158.     debug(F110,"GET /COMMAND after stripping",p,0);
  6159.     if (!*p) {
  6160.         printf("?Sorry, a command to write to is required\n");
  6161.         x = -9;
  6162.         goto xgetx;
  6163.     }
  6164.     pipename = p;
  6165.     pipesend = 1;
  6166.     }
  6167. #endif /* PIPESEND */
  6168.  
  6169. /* Set up /MOVE and /RENAME */
  6170.  
  6171.     if (pv[SND_DEL].ival > 0 &&
  6172.     (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  6173.     printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  6174.     x = -9;
  6175.     goto xgetx;
  6176.     }
  6177. #ifdef CK_TMPDIR
  6178.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  6179.     int len;
  6180.     char * p = pv[SND_MOV].sval;
  6181.     len = strlen(p);
  6182.     if (!isdir(p)) {        /* Check directory */
  6183. #ifdef CK_MKDIR
  6184.         char * s = NULL;
  6185.         s = (char *)malloc(len + 4);
  6186.         if (s) {
  6187.         strcpy(s,p);        /* safe */
  6188. #ifdef datageneral
  6189.         if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  6190. #else
  6191.         if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  6192. #endif /* datageneral */
  6193.         s[len++] = 'X';
  6194.         s[len] = NUL;
  6195. #ifdef NOMKDIR
  6196.         x = -1;
  6197. #else
  6198.         x = zmkdir(s);
  6199. #endif /* NOMKDIR */
  6200.         free(s);
  6201.         if (x < 0) {
  6202.             printf("?Can't create \"%s\"\n",p);
  6203.             x = -9;
  6204.             goto xgetx;
  6205.         }
  6206.         }
  6207. #else
  6208.         printf("?Directory \"%s\" not found\n",p);
  6209.         x = -9;
  6210.         goto xgetx;
  6211. #endif /* CK_MKDIR */
  6212.     }
  6213.     makestr(&rcv_move,p);
  6214.     moving = 1;
  6215.     }
  6216. #endif /* CK_TMPDIR */
  6217.  
  6218.     if (pv[SND_REN].ival > 0) {        /* /RENAME */
  6219.     char * p = pv[SND_REN].sval;
  6220.     if (!p) p = "";
  6221.     if (!*p) {
  6222.         printf("?New name required for /RENAME\n");
  6223.         x = -9;
  6224.         goto xgetx;
  6225.     }
  6226.     p = brstrip(p);
  6227. #ifndef NOSPL
  6228.     /* If name given is wild, rename string must contain variables */
  6229.     if (mget && !getone) {
  6230.         char * s = tmpbuf;
  6231.         x = TMPBUFSIZ;
  6232.         zzstring(p,&s,&x);
  6233.         if (!strcmp(tmpbuf,p)) {
  6234.         printf(
  6235.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  6236.                );
  6237.         x = -9;
  6238.         goto xgetx;
  6239.         }
  6240.     }
  6241. #endif /* NOSPL */
  6242.     renaming = 1;
  6243.     makestr(&rcv_rename,p);
  6244.     debug(F110,"FTP rcv_rename",rcv_rename,0);
  6245.     }
  6246.     if (!cmarg[0] && mgetn == 0 && pv[SND_FIL].ival < 1) {
  6247.     printf("?Filename required but not given\n");
  6248.     x = -9;
  6249.     goto xgetx;
  6250.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  6251.     printf("?You can't give both /LISTFILE and a remote filename\n");
  6252.     x = -9;
  6253.     goto xgetx;
  6254.     }
  6255.     CHECKCONN();            /* Check connection */
  6256.  
  6257. #ifndef NOSPL
  6258.     /* If as-name given for MGET, as-name must contain variables */
  6259.     if (mget && !getone && asnambuf[0]) {
  6260.     char * s = tmpbuf;
  6261.     x = TMPBUFSIZ;
  6262.     zzstring(asnambuf,&s,&x);
  6263.     if (!strcmp(tmpbuf,asnambuf)) {
  6264.         printf(
  6265.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  6266.            );
  6267.         x = -9;
  6268.         goto xgetx;
  6269.     }
  6270.     }
  6271. #endif /* NOSPL */
  6272.  
  6273. /* doget: */
  6274.  
  6275.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  6276.     g_displa = fdispla;
  6277.     fdispla = 0;
  6278.     } else {
  6279.     displa = 1;
  6280.     if (mdel || ftp_deb) {
  6281.         g_displa = fdispla;
  6282.         fdispla = XYFD_B;
  6283.     }
  6284.     }
  6285.     deleting = 0;
  6286.  
  6287.     if (pv[SND_DEL].ival > 0)        /* /DELETE was specified */
  6288.       deleting = 1;
  6289.     if (pv[SND_EXC].ival > 0)
  6290.       makelist(pv[SND_EXC].sval,rcvexcept,8);
  6291.     if (pv[SND_SMA].ival > -1)
  6292.       getsmaller = pv[SND_SMA].ival;
  6293.     if (pv[SND_LAR].ival > -1)
  6294.       getlarger = pv[SND_LAR].ival;
  6295.     if (pv[SND_NAM].ival > -1)
  6296.       x_cnv = pv[SND_NAM].ival;
  6297.     if (pv[SND_ERR].ival > -1)
  6298.       geterror = pv[SND_ERR].ival;
  6299.     if (pv[SND_COL].ival > -1)
  6300.       x_fnc = pv[SND_COL].ival;
  6301.     if (pv[SND_MAI].ival > -1)
  6302.       toscreen = 1;
  6303.  
  6304. #ifdef FTP_RESTART
  6305.     if (pv[SND_RES].ival > 0) {
  6306.     if (!ftp_typ) {
  6307.         printf("?Sorry, GET /RECOVER requires binary mode\n");
  6308.         x = -9;
  6309.         goto xgetx;
  6310. #ifdef COMMENT
  6311.     /* Not true - the fact that the initial REST fails does not mean */
  6312.     /* it will fail here.  */
  6313.     } else if (!okrestart) {
  6314.         printf("WARNING: Server might not support restart...\n");
  6315. #endif /* COMMENT */
  6316.     }
  6317.     restart = 1;
  6318.     }
  6319. #endif /* FTP_RESTART */
  6320.  
  6321. #ifdef PIPESEND
  6322.     if (pv[SND_FLT].ival > 0) {        /* Have SEND FILTER? */
  6323.     if (pipesend) {
  6324.         printf("?Switch conflict: /FILTER and /COMMAND\n");
  6325.         x = -9;
  6326.         goto xgetx;
  6327.     }
  6328.     makestr(&rcvfilter,pv[SND_FLT].sval);
  6329.     debug(F110,"ftp get /FILTER", rcvfilter, 0);
  6330.     }
  6331.     if (rcvfilter || pipesend) {    /* /RESTART */
  6332. #ifdef FTP_RESTART
  6333.     if (restart) {            /* with pipes or filters */
  6334.         printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  6335.         x = -9;
  6336.         goto xgetx;
  6337.     }
  6338. #endif /* FTP_RESTART */
  6339.     if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_U) {
  6340.         printf("?Switch conflict: /FILTER or /COMMAND and Update\n");
  6341.         x = -9;
  6342.         goto xgetx;
  6343.     }
  6344.     }
  6345. #endif /* PIPESEND */
  6346.  
  6347.     tfc = 0L;                /* Initialize stats and counters */
  6348.     filcnt = 0;
  6349.     pktnum = 0;
  6350.     rpackets = 0L;
  6351.  
  6352.     if (pv[SND_FIL].ival > 0) {
  6353.     if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  6354.         debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  6355.         printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  6356.         x = -9;
  6357.         goto xgetx;
  6358.     }
  6359.     if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  6360.         zclose(ZMFILE);                      /* Failed */
  6361.         debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  6362.         printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  6363.         x = -9;
  6364.         goto xgetx;
  6365.     }
  6366.     listfile = 1;
  6367.     debug(F110,"ftp get listfile first",tmpbuf,0);
  6368.     makestr(&(mgetlist[0]),tmpbuf);
  6369.     }
  6370.     t0 = gmstimer();            /* Record starting time */
  6371.  
  6372.     updating = pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U);
  6373.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  6374.  
  6375.     cancelgroup = 0;            /* Group not canceled yet */
  6376.     changetype(ftp_typ,0);        /* Change to requested type */
  6377.     binary = ftp_typ;            /* For file-transfer display */
  6378.     first = 1;                /* For MGET list */
  6379.     done = 0;                /* Loop control */
  6380.  
  6381. #ifdef CK_TMPDIR
  6382.     if (dldir && !f_tmpdir) {        /* If they have a download directory */
  6383.     if ((s = zgtdir())) {        /* Get current directory */
  6384.         if (zchdir(dldir)) {    /* Change to download directory */
  6385.         ckstrncpy(savdir,s,TMPDIRLEN);
  6386.         f_tmpdir = 1;        /* Remember that we did this */
  6387.         }
  6388.     }
  6389.     }
  6390. #endif /* CK_TMPDIR */
  6391.  
  6392.     if (ftp_nml) {            /* /NAMELIST */
  6393.     debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  6394.     if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  6395.       fp_nml = stdout;
  6396.     else
  6397.       fp_nml = fopen(ftp_nml, "wb");
  6398.     if (!fp_nml) {
  6399.         printf("?%s: %s\n",ftp_nml,ck_errstr());
  6400.         goto xgetx;
  6401.     }
  6402.     }
  6403.     while (!done && !cancelgroup) {    /* Loop for all files */
  6404.                     /* or until canceled. */
  6405. #ifdef FTP_PROXY
  6406.     /* do something here if proxy */
  6407. #endif /* FTP_PROXY */
  6408.  
  6409.     rs_len = 0L;            /* REGET position */
  6410.     cancelfile = 0;            /* This file not canceled yet */
  6411.     haspath = 0;            /* Recalculate this each time thru */
  6412.  
  6413.     if (getone) {            /* GET */
  6414.         s = line;
  6415.         src = line;
  6416.         done = 1;
  6417.         debug(F111,"ftp get file",s,0);
  6418.     } else if (mget) {        /* MGET */
  6419.         src = mgetlist[mgetx];
  6420.         debug(F111,"ftp mget remote_files A",src,first);
  6421.         s = (char *)remote_files(first,(CHAR *)mgetlist[mgetx],0);
  6422.         if (!s) s = "";
  6423.         if (!*s) {
  6424.         first = 1;
  6425.         if (listfile) {
  6426.           again:
  6427.             tmpbuf[0] = NUL;
  6428.             while (!tmpbuf[0]) {
  6429.             if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  6430.                 zclose(ZMFILE);
  6431.                 debug(F110,"ftp get listfile EOF",
  6432.                   pv[SND_FIL].sval,0);
  6433.                 makestr(&(mgetlist[0]),NULL);
  6434.                 s = NULL;
  6435.                 done = 1;
  6436.                 break;
  6437.             }
  6438.             }
  6439.             if (done)
  6440.               continue;
  6441.  
  6442.             makestr(&(mgetlist[0]),tmpbuf);
  6443.             s = (char *)remote_files(first,(CHAR *)mgetlist[0],0);
  6444.             if (!s) {
  6445.             ftscreen(SCR_FN,'F',0L,s);
  6446.             ftscreen(SCR_ST,ST_MSG,0L,"File not found");
  6447.             tlog(F110,"ftp get file not found:",s,0);
  6448.             goto again;
  6449.             }
  6450.         } else {
  6451.             mgetx++;
  6452.             if (mgetx < mgetn)
  6453.              s = (char *)remote_files(first,(CHAR *)mgetlist[mgetx],0);
  6454.             else
  6455.              s = NULL;
  6456.         }
  6457.         debug(F111,"ftp mget remote_files B",s,0);
  6458.         if (!s) {
  6459.             done = 1;
  6460.             break;
  6461.         }
  6462.         }
  6463.     }
  6464.     /*
  6465.       The semantics of NLST are ill-defined.  Suppose we have just sent
  6466.       NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6467.       /path/bar, etc.  But some send back only foo and bar, and subsequent
  6468.       RETR commands based on the pathless names are not going to work.
  6469.     */
  6470.     if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6471.         char * s3;
  6472.         if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  6473.         int len, left = 4096;
  6474.         char * tmp = xtmpbuf;
  6475.         len = s3 - mgetlist[mgetx] + 1;
  6476.         ckstrncpy(tmp,mgetlist[mgetx],left);
  6477.         tmp += len;
  6478.         left -= len;
  6479.         ckstrncpy(tmp,s,left);
  6480.         s = xtmpbuf;
  6481.         }
  6482.     }
  6483.     first = 0;
  6484.     skipthis = 0;            /* File selection... */
  6485.     msg = "";
  6486.     nam = s;            /* Filename (without path) */
  6487.     rc = 0;                /* Initial return code */
  6488.     s2 = "";
  6489.  
  6490.     if (!getone && !skipthis) {    /* For MGET and MDELETE... */
  6491.         char c, * p = s;
  6492.         int srvpath = 0;
  6493.         int usrpath = 0;
  6494.         int i, k = 0;
  6495. /*
  6496.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  6497.   But if the client did not ask for a recursive list, we have to ignore any
  6498.   server files that include a pathname that extends beyond any path that
  6499.   was included in the user's request.
  6500.  
  6501.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  6502.   get the user's path segment.  Then, for each incoming file, if it begins
  6503.   with the same path segment, we must strip it (point past it).
  6504. */
  6505.         src = mgetlist[mgetx];    /* In case it moved! */
  6506.         for (i = 0; src[i]; i++) {    /* Find rightmost path separator */
  6507.         if (ispathsep(src[i]))    /* in user's pathname */
  6508.           k = i + 1;
  6509.         }
  6510.         usrpath = k;        /* User path segment length */
  6511.         debug(F111,"ftp get usrpath",src,usrpath);
  6512.  
  6513.         p = s;            /* Server filename */
  6514.         while ((c = *p++)) {    /* Look for path in server filename */
  6515.         if (ispathsep(c)) {
  6516.             nam = p;        /* Pathless name (for ckmatch) */
  6517.             srvpath = p - s;    /* Server path segment length */
  6518.         }
  6519.         }
  6520.         debug(F111,"ftp get srvpath",s,srvpath);
  6521. /*
  6522.   If as-name not given and server filename includes path that matches
  6523.   the pathname from the user's file specification, we must trim the common
  6524.   path prefix from the server's name when constructing the local name.
  6525. */
  6526.         if (!asnambuf[0] && (srvpath > 0) && !strncmp(src,s,usrpath)) {
  6527.         s2 = s + usrpath;    /* Local name skips past remote path */
  6528.         if (srvpath > usrpath)    /* If there is still some path left */
  6529.           haspath = 1;        /* in the server name, remember. */
  6530.         }
  6531.         debug(F111,"ftp get haspath",s+usrpath,haspath);
  6532.  
  6533.         if (haspath) {        /* Server file has path segments? */
  6534.         if (!recursive) {    /* [M]GET /RECURSIVE? */
  6535. /*
  6536.   We did not ask for a recursive listing, but the server is sending us one
  6537.   anyway (as wu-ftpd is known to do).  We get here if the current filename
  6538.   includes a path segment beyond any path segment we asked for in our
  6539.   non-recursive [M]GET command.  We MUST skip this file.
  6540. */
  6541.             debug(F111,"ftp get skipping because of path",s,0);
  6542. #ifdef COMMENT
  6543.             skipthis++;
  6544. #else
  6545.             continue;
  6546. #endif /* COMMENT */
  6547.         }
  6548.         }
  6549.     }
  6550.     if (!*nam)            /* Name without path */
  6551.       nam = s;
  6552.  
  6553.     if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  6554.         for (i = 0; i < 8; i++) {
  6555.         if (!rcvexcept[i]) {
  6556.             break;
  6557.         }
  6558.         if (ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1)) {
  6559.             tlog(F100," refused: exception list","",0);
  6560.             skipthis++;
  6561.             break;
  6562.         }
  6563.         }
  6564.     }
  6565.     if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  6566.         if (nam[0] == '.')
  6567.           skipthis++;
  6568.     }
  6569.     if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  6570.         if (ckmatch(
  6571. #ifdef CKREGEX
  6572.             "*.~[0-9]*~"
  6573. #else
  6574.             "*.~*~"
  6575. #endif /* CKREGEX */
  6576.             ,nam,0,1) > 0)
  6577.           skipthis++;
  6578.     }
  6579.     if (!x_xla) {            /* If translation is off */
  6580.         x_csl = -2;            /* unset the charsets */
  6581.         x_csr = -2;
  6582.     }
  6583.  
  6584.     /* Initialize file size to -1 in case server doesn't understand */
  6585.     /* SIZE command, so xxscreen() will know we don't know the size */
  6586.  
  6587.     fsize = -1L;
  6588.  
  6589.     if (!mdel && !skipthis) {    /* Don't need size for DELE... */
  6590.         x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm); /* Remote file's size */
  6591.         if (x == REPLY_COMPLETE) {
  6592.         fsize = atol(&ftp_reply_str[4]);
  6593.         if (getsmaller > -1L && fsize >= getsmaller)
  6594.           skipthis++;
  6595.         if (getlarger > -1L && fsize <= getlarger)
  6596.           skipthis++;
  6597.         if (skipthis) {
  6598.             debug(F111,"ftp get skip size",s,fsize);
  6599.             tlog(F100," refused: size","",0);
  6600.             msg = "Refused: size";
  6601.         }
  6602. /* Here we should see if SIZE is an unknown command to the server and if */
  6603. /* so, set a flag to inhibit further SIZE queries. */
  6604.  
  6605. #ifdef COMMENT
  6606.         } else if (getone) {
  6607.         /* SIZE can fail for many reasons.  Does the file exist? */
  6608.         x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  6609.         if (x != REPLY_COMPLETE) {
  6610.             printf(">>> FILE NOT FOUND: %s\n",s);
  6611.             break;
  6612.         }
  6613. #endif /* COMMENT */
  6614.         }
  6615.     }
  6616.     ckstrncpy(filnam,s,CKMAXPATH);    /* For \v(filename) */
  6617.     if (!*s2)            /* Local name */
  6618.       s2 = asnambuf;        /* As-name */
  6619.     debug(F110,"ftp get filnam ",s,0);
  6620.     debug(F110,"ftp get asnam A",s2,0);
  6621.  
  6622.     /* Receiving to real file */
  6623.     if (!pipesend &&
  6624. #ifdef PIPESEND
  6625.         !rcvfilter &&
  6626. #endif /* PIPESEND */
  6627.         !toscreen) {
  6628. #ifndef NOSPL
  6629.         /* Do this here so we can decide whether to skip */
  6630.         if (cmd_quoting && !skipthis && asnambuf[0]) {
  6631.         int n; char *p;
  6632.         n = TMPBUFSIZ;
  6633.         p = tmpbuf;
  6634.         zzstring(asnambuf,&p,&n);
  6635.         s2 = tmpbuf;
  6636.         debug(F111,"ftp get asname B",s2,updating);
  6637.         }
  6638. #endif /* NOSPL */
  6639. #ifdef DOUPDATE
  6640.         if (!skipthis && updating) { /* If updating and not skipping */
  6641.         local = *s2 ? s2 : s;
  6642.         if (zchki(local) > -1) {
  6643.             x = chkmodtime(local,s,0);
  6644.             debug(F111,"ftp get /update chkmodtime",local,x);
  6645.             if (x == 2) {
  6646.             skipthis++;
  6647.             tlog(F100," refused: date","",0);
  6648.             msg = "Refused: date";
  6649.             debug(F110,"ftp get skip date",local,0);
  6650.             }
  6651.         }
  6652.         }
  6653. #endif /* DOUPDATE */
  6654.     }
  6655.     if (skipthis) {            /* Skipping this file? */
  6656.         ftscreen(SCR_FN,'F',0L,s);
  6657.         if (msg)
  6658.           ftscreen(SCR_ST,ST_ERR,0L,msg);
  6659.         else
  6660.           ftscreen(SCR_ST,ST_SKIP,0L,s);
  6661.         continue;
  6662.     }
  6663.     if (fp_nml) {            /* /NAMELIST only - no transfer */
  6664.         fprintf(fp_nml,"%s\n",s);
  6665.         continue;
  6666.     }
  6667.     if (recursive && haspath && !pipesend
  6668. #ifdef PIPESEND
  6669.         && !rcvfilter
  6670. #endif /* PIPESEND */
  6671.         ) {
  6672.         if (
  6673. #ifdef NOMKDIR
  6674.         1
  6675. #else
  6676.         zmkdir(s2) < 0        /* Try to make the directory */
  6677. #endif /* NOMKDIR */
  6678.         ) {
  6679.         rc = -1;        /* Failure is fatal */
  6680.         if (geterror) {
  6681.             status = 0;
  6682.             ftscreen(SCR_EM,0,0L,"Directory creation failure");
  6683.             break;
  6684.         }
  6685.         }
  6686.     }
  6687.  
  6688.     /* Not skipping */
  6689.  
  6690.     pn = NULL;
  6691.     if (mdel) {            /* [M]DELETE */
  6692.         if (displa && !ftp_vbm)
  6693.           printf(" %s...",s);
  6694.         rc =
  6695.          (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  6696.         if (rc > -1) {
  6697.         tlog(F110,"ftp mdelete",s,0);
  6698.         if (displa && !ftp_vbm)
  6699.           printf("OK\n");
  6700.         } else {
  6701.         tlog(F110,"ftp mdelete failed:",s,0);
  6702.         if (displa)
  6703.           printf("Failed\n");
  6704.         }
  6705. #ifndef NOSPL
  6706. #ifdef PIPESEND
  6707.     } else if (rcvfilter) {        /* [M]GET with filter */
  6708.         int n; char * p;
  6709.         n = CKMAXPATH;
  6710.         p = tmpbuf;            /* Safe - no asname with filter */
  6711.         zzstring(rcvfilter,&p,&n);
  6712.         if (n > -1)
  6713.           pn = tmpbuf;
  6714.         debug(F111,"ftp get rcvfilter",pn,n);
  6715. #endif /* PIPESEND */
  6716. #endif /* NOSPL */
  6717.         if (toscreen) s2 = "-";
  6718.     } else if (pipesend) {        /* [M]GET /COMMAND */
  6719.         int n; char * p;
  6720.         n = CKMAXPATH;
  6721.         p = tmpbuf;            /* Safe - no asname with filter */
  6722.         zzstring(pipename,&p,&n);
  6723.         if (n > -1)
  6724.           pn = tmpbuf;
  6725.         debug(F111,"ftp get pipename",pipename,n);
  6726.         if (toscreen) s2 = "-";
  6727.     } else {            /* [M]GET with no pipes or filters */
  6728.         debug(F111,"ftp get s2 A",s2,x_cnv);
  6729.         if (toscreen) {
  6730.         s2 = "-";        /* (hokey convention for stdout) */
  6731.         } else if (!*s2) {        /* No asname? */
  6732.         if (x_cnv) {        /* If converting */
  6733.             int nc = x_cnv;
  6734.             if (x_cnv == SET_AUTO) {
  6735.             if (alike)
  6736.               nc = 0;
  6737.             } else {
  6738.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6739.               nc = -1;    /* only minimal conversions needed */
  6740.             else        /* otherwise */
  6741.               nc = 1;    /* full conversion */
  6742.             }
  6743.             nzrtol(s,tmpbuf,nc,1,CKMAXPATH); /* convert */
  6744.             s2 = tmpbuf;
  6745.             debug(F110,"ftp get nzrtol",s2,0);
  6746.         } else            /* otherwise */
  6747.           s2 = s;        /* use incoming file's name */
  6748.         }
  6749.         debug(F110,"ftp get s2 B",s2,0);
  6750.  
  6751.         /* If local file already exists, take collision action */
  6752.  
  6753.         if (!pipesend &&
  6754. #ifdef PIPESEND
  6755.         !rcvfilter &&
  6756. #endif /* PIPESEND */
  6757.         !toscreen) {
  6758.         x = zchki(s2);
  6759.         debug(F111,"ftp get zchki",s2,x);
  6760.         debug(F111,"ftp get x_fnc",s2,x_fnc);
  6761.  
  6762.         if (x > -1 && !restart) {
  6763.             switch (x_fnc) {
  6764.               case XYFX_A:    /* Append */
  6765.             append = 1;
  6766.             break;
  6767.               case XYFX_R:    /* Rename */
  6768.               case XYFX_B: {    /* Backup */
  6769.               char * p = NULL;
  6770.               int x = -1;
  6771.               znewn(s2,&p);    /* Make unique name */
  6772.               debug(F110,"ftp get znewn",p,0);
  6773.               if (x_fnc == XYFX_B) { /* Backup existing file */
  6774.                   x = zrename(s2,p);
  6775.                   debug(F111,"ftp get backup zrename",p,x);
  6776.               } else {    /* Rename incoming file */
  6777.                   x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6778.                   s2 = tmpbuf;
  6779.                   debug(F111,"ftp get rename incoming",p,x);
  6780.               }
  6781.               if (x < 0) {
  6782.                   ftscreen(SCR_EM,0,0L,"Backup/Rename failed");
  6783.                   x = 0;
  6784.                   goto xgetx;
  6785.               }
  6786.               break;
  6787.               }
  6788.               case XYFX_D:    /* Discard */
  6789.             ftscreen(SCR_FN,'F',0L,s);
  6790.             ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  6791.             tlog(F100," refused: name","",0);
  6792.             debug(F110,"ftp get skip name",*s2 ? s2 : s,0);
  6793.             continue;
  6794.               case XYFX_X:    /* Overwrite */
  6795.               case XYFX_U:    /* Update (already handled above) */
  6796.             break;
  6797.             }
  6798.         }
  6799.         }
  6800.     }
  6801.     if (!mdel) {
  6802. #ifdef PIPESEND
  6803.         debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  6804. #endif /* PIPESEND */
  6805.         if (pipesend && !toscreen)
  6806.           s2 = NULL;
  6807. #ifdef DEBUG
  6808.         if (deblog) {
  6809.         debug(F101,"ftp get x_xla","",x_xla);
  6810.         debug(F101,"ftp get x_csl","",x_csl);
  6811.         debug(F101,"ftp get x_csr","",x_csr);
  6812.         }
  6813. #endif /* DEBUG */
  6814.  
  6815.         rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  6816.  
  6817. #ifdef DEBUG
  6818.         if (deblog) {
  6819.         debug(F111,"ftp get rc",s,rc);
  6820.         debug(F111,"ftp get cancelfile",s,cancelfile);
  6821.         debug(F111,"ftp get cancelgroup",s,cancelgroup);
  6822.         debug(F111,"ftp get renaming",s,renaming);
  6823.         }
  6824. #endif /* DEBUG */
  6825.     }
  6826.     if (rc > -1) {
  6827.         good++;
  6828.         status = 1;
  6829.         if (!cancelfile) {
  6830.         if (deleting) {        /* GET /DELETE (source file) */
  6831.             rc =
  6832.               (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  6833.             ? 1 : -1;
  6834.             tlog(F110, (rc > -1) ?
  6835.              " deleted" : " failed to delete", s, 0);
  6836.         } else if (renaming && rcv_rename && !toscreen) {
  6837.             char *p;        /* Rename downloaded file */
  6838. #ifndef NOSPL
  6839.             char tmpbuf[CKMAXPATH+1];
  6840.             int n;
  6841.             n = CKMAXPATH;
  6842.             p = tmpbuf;
  6843.             debug(F111,"ftp get /rename",rcv_rename,0);
  6844.             zzstring(rcv_rename,&p,&n);
  6845.             debug(F111,"ftp get /rename",rcv_rename,0);
  6846.             p = tmpbuf;
  6847. #else
  6848.             p = rcv_rename;
  6849. #endif /* NOSPL */
  6850.             rc = (zrename(s2,p) < 0) ? -1 : 1;
  6851.             debug(F111,"doftpget /RENAME zrename",p,rc);
  6852.             tlog(F110, (rc > -1) ?
  6853.              " renamed to" :
  6854.              " failed to rename to",
  6855.              p,
  6856.              0
  6857.              );
  6858.         } else if (moving && rcv_move && !toscreen) {
  6859.             char *p;        /* Move downloaded file */
  6860. #ifndef NOSPL
  6861.             char tmpbuf[CKMAXPATH+1];
  6862.             int n;
  6863.             n = TMPBUFSIZ;
  6864.             p = tmpbuf;
  6865.             debug(F111,"ftp get /move-to",rcv_move,0);
  6866.             zzstring(rcv_move,&p,&n);
  6867.             p = tmpbuf;
  6868. #else
  6869.             p = rcv_move;
  6870. #endif /* NOSPL */
  6871.             debug(F111,"ftp get /move-to",p,0);
  6872.             rc = (zrename(s2,p) < 0) ? -1 : 1;
  6873.             debug(F111,"doftpget /MOVE zrename",p,rc);
  6874.             tlog(F110, (rc > -1) ?
  6875.              " moved to" : " failed to move to", p, 0);
  6876.         }
  6877.         if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  6878.             char * s = pv[SND_SRN].sval;
  6879.             char * srvrn = pv[SND_SRN].sval;
  6880.             char tmpbuf[CKMAXPATH+1];
  6881. #ifndef NOSPL
  6882.             int y;        /* Pass it thru the evaluator */
  6883.             extern int cmd_quoting; /* for \v(filename) */
  6884.             debug(F111,"ftp get srv_renam",s,1);
  6885.  
  6886.             if (cmd_quoting) {
  6887.             y = CKMAXPATH;
  6888.             s = (char *)tmpbuf;
  6889.             zzstring(srvrn,&s,&y);
  6890.             s = (char *)tmpbuf;
  6891.             }
  6892. #endif /* NOSPL */
  6893.             debug(F111,"ftp get srv_renam",s,1);
  6894.             if (s) if (*s) {
  6895.             int x;
  6896.             x = ftp_rename(s2,s);
  6897.             debug(F111,"ftp get ftp_rename",s2,x);
  6898.             tlog(F110, (x > 0) ?
  6899.                  " renamed source file to" :
  6900.                  " failed to rename source file to",
  6901.                  s,
  6902.                  0
  6903.                  );
  6904.             if (x < 1)
  6905.               return(-1);
  6906.             }
  6907.         }
  6908.         }
  6909.     }
  6910.     if (cancelfile)
  6911.       continue;
  6912.     if (rc < 0) {
  6913.         ftp_fai++;
  6914.         if (geterror) {
  6915.         status = 0;
  6916.         ftscreen(SCR_EM,0,0L,"Fatal download error");
  6917.         done++;
  6918.         }
  6919.     }
  6920.     }
  6921.     if (status > 0) {
  6922.     if (cancelgroup)
  6923.       status = 0;
  6924.     else if (cancelfile && good < 1)
  6925.       status = 0;
  6926.     }
  6927.     success = status;
  6928.     x = success;
  6929.  
  6930.   xgetx:
  6931.     pipesend = pipesave;        /* Restore global pipe selection */
  6932.     if (fp_nml) {            /* Close /NAMELIST */
  6933.     if (fp_nml != stdout)
  6934.       fclose(fp_nml);
  6935.     fp_nml = NULL;
  6936.     }
  6937.     if (x > -1) {            /* Download successful */
  6938. #ifdef GFTIMER
  6939.     t1 = gmstimer();        /* End time */
  6940.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6941.     if (!sec) sec = 0.001;
  6942.     fptsecs = sec;
  6943. #else
  6944.     sec = (t1 - t0) / 1000;
  6945.     if (!sec) sec = 1;
  6946. #endif /* GFTIMER */
  6947.     tfcps = (long) (tfc / sec);
  6948.     tsecs = (int)sec;
  6949.     lastxfer = W_FTP|W_RECV;
  6950.     xferstat = success;
  6951.     }
  6952.     if (dpyactive)
  6953.       ftscreen(SCR_TC,0,0L,"");
  6954. #ifdef CK_TMPDIR
  6955.     if (f_tmpdir) {            /* If we changed to download dir */
  6956.     zchdir((char *) savdir);    /* Go back where we came from */
  6957.     f_tmpdir = 0;
  6958.     }
  6959. #endif /* CK_TMPDIR */
  6960.  
  6961.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  6962.     if (pv[i].sval)
  6963.       free(pv[i].sval);
  6964.     }
  6965.     for (i = 0; i < mgetn; i++)        /* MGET list too */
  6966.       makestr(&(mgetlist[i]),NULL);
  6967.  
  6968.     ftreset();                /* Undo switch effects */
  6969.     dpyactive = 0;
  6970.     return(x);
  6971. }
  6972.  
  6973. static struct keytab ftprmt[] = {
  6974.     { "cd",        XZCWD, 0 },
  6975.     { "cdup",      XZCDU, 0 },
  6976.     { "cwd",       XZCWD, CM_INV },
  6977.     { "delete",    XZDEL, 0 },
  6978.     { "directory", XZDIR, 0 },
  6979.     { "exit",      XZXIT, 0 },
  6980.     { "help",      XZHLP, 0 },
  6981.     { "login",     XZLGI, 0 },
  6982.     { "logout",    XZLGO, 0 },
  6983.     { "mkdir",     XZMKD, 0 },
  6984.     { "pwd",       XZPWD, 0 },
  6985.     { "rename",    XZREN, 0 },
  6986.     { "rmdir",     XZRMD, 0 },
  6987.     { "type",      XZTYP, 0 },
  6988.     { "", 0, 0 }
  6989. };
  6990. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  6991.  
  6992. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  6993.  
  6994. int
  6995. doftprmt(cx,who) int cx, who; {        /* who == 1 for ftp, 0 for kermit */
  6996.     /* cx == 0 means REMOTE */
  6997.     /* cx != 0 is a XZxxx value */
  6998.     char * s;
  6999.  
  7000.     if (who != 0)
  7001.       return(0);
  7002.  
  7003.     if (cx == 0) {
  7004.     if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  7005.       return(x);
  7006.     cx = x;
  7007.     }
  7008.     switch (cx) {
  7009.       case XZCDU:            /* CDUP */
  7010.         if ((x = cmcfm()) < 0) return(x);        
  7011.     return(doftpcdup());
  7012.  
  7013.       case XZCWD:            /* RCD */
  7014.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7015.       return(x);
  7016.     ckstrncpy(line,s,LINBUFSIZ);
  7017.     return(doftpcwd((char *)line,1));
  7018.       case XZPWD:            /* RPWD */
  7019.     return(doftppwd());
  7020.       case XZDEL:            /* RDEL */
  7021.     return(doftpget(FTP_MDE,1));
  7022.       case XZDIR:            /* RDIR */
  7023.     return(doftpdir(FTP_DIR));
  7024.       case XZHLP:            /* RHELP */
  7025.     return(doftpxhlp());
  7026.       case XZMKD:            /* RMKDIR */
  7027.     return(doftpmkd());
  7028.       case XZREN:            /* RRENAME */
  7029.     return(doftpren());
  7030.       case XZRMD:            /* RRMDIR */
  7031.     return(doftprmd());
  7032.       case XZLGO:            /* LOGOUT */
  7033.     return(doftpres());
  7034.       case XZXIT:            /* EXIT */
  7035.     return(ftpbye());
  7036.     }
  7037.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  7038.     return(-9);
  7039. }
  7040.  
  7041. int
  7042. doxftp() {                /* Command parser for built-in FTP */
  7043.     int cx, n;
  7044.     struct FDB kw, fl;
  7045.     char * s;
  7046.     int usetls = 0;
  7047.  
  7048.     int lcs = -1, rcs = -1;
  7049. #ifndef NOCSETS
  7050.     if (ftp_xla) {
  7051.     lcs = ftp_csl;
  7052.     if (lcs < 0) lcs = fcharset;
  7053.     rcs = ftp_csx;
  7054.     if (rcs < 0) rcs = ftp_csr;
  7055.     }
  7056. #endif /* NOCSETS */
  7057.  
  7058.     if (inserver)            /* FTP not allowed in IKSD. */
  7059.       return(-2);
  7060.  
  7061.     if (g_ftp_typ > -1) {        /* Restore TYPE if saved */
  7062.     ftp_typ = g_ftp_typ;
  7063.     /* g_ftp_typ = -1; */
  7064.     }
  7065. #ifdef COMMENT
  7066. /*
  7067.   We'll set the collision action locally in doftpget() based on whether
  7068.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  7069. */
  7070.     if (ftp_fnc < 0)            /* Inherit global collision action */
  7071.       ftp_fnc = fncact;            /* if none specified for FTP */
  7072. #endif /* COMMENT */
  7073.  
  7074.     /* Restore global verbose mode */
  7075.     if (ftp_deb)
  7076.       ftp_vbm = 1;
  7077.     else if (quiet)
  7078.       ftp_vbm = 0;
  7079.     else
  7080.       ftp_vbm = ftp_vbx;
  7081.  
  7082.     dpyactive = 0;            /* Reset global transfer-active flag */
  7083.     printlines = 0;            /* Reset printlines */
  7084.  
  7085.     if (fp_nml) {            /* Reset /NAMELIST */
  7086.     if (fp_nml != stdout)
  7087.       fclose(fp_nml);
  7088.     fp_nml = NULL;
  7089.     }
  7090.     makestr(&ftp_nml,NULL);
  7091.  
  7092.     cmfdbi(&kw,                /* First FDB - commands */
  7093.        _CMKEY,            /* fcode */
  7094.        "Hostname; or FTP command",    /* help */
  7095.        "",                /* default */
  7096.        "",                /* addtl string data */
  7097.        nftpcmd,            /* addtl numeric data 1: tbl size */
  7098.        0,                /* addtl numeric data 2: none */
  7099.        xxstring,            /* Processing function */
  7100.        ftpcmdtab,            /* Keyword table */
  7101.        &fl                /* Pointer to next FDB */
  7102.        );
  7103.     cmfdbi(&fl,                /* A host name or address */
  7104.        _CMFLD,            /* fcode */
  7105.        "Hostname or address",    /* help */
  7106.        "",                /* default */
  7107.        "",                /* addtl string data */
  7108.        0,                /* addtl numeric data 1 */
  7109.        0,                /* addtl numeric data 2 */
  7110.        xxstring,
  7111.        NULL,
  7112.        NULL
  7113.        );
  7114.     x = cmfdb(&kw);            /* Parse a hostname or a keyword */
  7115.     if (x == -3) {
  7116.     printf("?ftp what? \"help ftp\" for hints\n");
  7117.     return(-9);
  7118.     }
  7119.     if (x < 0)
  7120.       return(x);
  7121.     if (cmresult.fcode == _CMFLD) {    /* If hostname */
  7122.     return(openftp(cmresult.sresult,0)); /* go open the connection */
  7123.     } else {
  7124.     cx = cmresult.nresult;
  7125.     }
  7126.     switch (cx) {
  7127.       case FTP_ACC:            /* ACCOUNT */
  7128.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  7129.       return(x);
  7130.     CHECKCONN();
  7131.     makestr(&ftp_acc,s);
  7132.     if (testing)
  7133.       printf(" ftp account: \"%s\"\n",ftp_acc);
  7134.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  7135.     return(success);
  7136.  
  7137.       case FTP_GUP:            /* Go UP */
  7138.         if ((x = cmcfm()) < 0) return(x);
  7139.     CHECKCONN();
  7140.     if (testing) printf(" ftp cd: \"(up)\"\n");
  7141.         return(success = doftpcdup());
  7142.  
  7143.       case FTP_CWD:            /* CD */
  7144.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7145.       return(x);
  7146.     CHECKCONN();
  7147.     ckstrncpy(line,s,LINBUFSIZ);
  7148.     if (testing)
  7149.       printf(" ftp cd: \"%s\"\n", line);
  7150.     return(success = doftpcwd(line,1));
  7151.  
  7152.       case FTP_CHM:            /* CHMOD */
  7153.     if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  7154.       return(x);
  7155.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7156.     if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7157.       return(x);
  7158.     CHECKCONN();
  7159.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  7160.     if (testing)
  7161.       printf(" ftp chmod: %s\n",ftpcmdbuf);
  7162.         success =
  7163.       (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7164.     return(success);
  7165.  
  7166.       case FTP_CLS:            /* CLOSE FTP connection */
  7167.     if ((y = cmcfm()) < 0)
  7168.       return(y);
  7169.     CHECKCONN();
  7170.     if (testing)
  7171.       printf(" ftp closing...\n");
  7172.     ftpclose();
  7173.     return(success = 1);
  7174.  
  7175.       case FTP_DIR:            /* DIRECTORY of remote files */
  7176.       case FTP_VDI:
  7177.     return(doftpdir(cx));
  7178.  
  7179.       case FTP_GET:            /* GET a remote file */
  7180.       case FTP_RGE:            /* REGET */
  7181.       case FTP_MGE:            /* MGET */
  7182.       case FTP_MDE:            /* MDELETE */
  7183.     return(doftpget(cx,1));
  7184.  
  7185.       case FTP_IDL:            /* IDLE */
  7186.     if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  7187.       return(x);
  7188.     if ((y = cmcfm()) < 0)
  7189.       return(y);
  7190.     CHECKCONN();
  7191.     if (z < 0)  {            /* Display idle timeout */
  7192.         if (testing)
  7193.           printf(" ftp query idle timeout...\n");
  7194.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  7195.     } else {            /* Set idle timeout */
  7196.         if (testing)
  7197.           printf(" ftp idle timeout set: %d...\n",z);
  7198.             success =
  7199.           (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  7200.     }
  7201.     return(success);
  7202.  
  7203.       case FTP_MKD:            /* MKDIR */
  7204.     return(doftpmkd());
  7205.  
  7206.       case FTP_MOD:            /* MODTIME */
  7207.     if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7208.       return(x);
  7209.     CHECKCONN();
  7210.     ckstrncpy(line,s,LINBUFSIZ);
  7211.     if (testing)
  7212.       printf(" ftp modtime \"%s\"...\n",line);
  7213.     success = 0;
  7214.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  7215.         int flag = 0;
  7216.         char c, * s;
  7217.         struct tm tmremote;
  7218.  
  7219.         bzero((char *)&tmremote, sizeof(struct tm));
  7220.         s = ftp_reply_str;
  7221.         while ((c = *s++)) {
  7222.         if (c == SP) {
  7223.             flag++;
  7224.             break;
  7225.         }
  7226.         }
  7227.         if (flag) {
  7228.         if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  7229.                &tmremote.tm_year,
  7230.                &tmremote.tm_mon,
  7231.                &tmremote.tm_mday,
  7232.                &tmremote.tm_hour,
  7233.                &tmremote.tm_min,
  7234.                &tmremote.tm_sec
  7235.                ) == 6) {
  7236.             success = 1;
  7237.             printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  7238.                line,
  7239.                tmremote.tm_year,
  7240.                tmremote.tm_mon,
  7241.                tmremote.tm_mday,
  7242.                tmremote.tm_hour,
  7243.                tmremote.tm_min,
  7244.                tmremote.tm_sec
  7245.                );
  7246.         }
  7247.         }
  7248.         }
  7249.     return(success);
  7250.  
  7251.       case FTP_OPN:            /* OPEN connection */
  7252. #ifdef COMMENT
  7253.     x = cmfld("IP hostname or address","",&s,xxstring);
  7254.     if (x < 0) {
  7255.         success = 0;
  7256.         return(x);
  7257.     }
  7258.     ckstrncpy(line,s,LINBUFSIZ);
  7259.     s = line;
  7260.     return(openftp(s,0));
  7261. #else
  7262.     {                /* OPEN connection */
  7263.         char name[TTNAMLEN+1], *p;
  7264.         extern int network;
  7265.         extern char ttname[];
  7266.         if (network)        /* If we have a current connection */
  7267.           ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  7268.         else
  7269.           *name = '\0';        /* as default host */
  7270.         for (p = name; *p; p++)    /* Remove ":service" from end. */
  7271.           if (*p == ':') { *p = '\0'; break; }
  7272. #ifndef USETLSTAB
  7273.         x = cmfld("IP hostname or address",name,&s,xxstring);
  7274. #else
  7275.         cmfdbi(&kw,            /* First FDB - commands */
  7276.            _CMKEY,        /* fcode */
  7277.            "Hostname or switch", /* help */
  7278.            "",            /* default */
  7279.            "",            /* addtl string data */
  7280.            ntlstab,        /* addtl numeric data 1: tbl size */
  7281.            0,            /* addtl numeric data 2: none */
  7282.            xxstring,        /* Processing function */
  7283.            tlstab,        /* Keyword table */
  7284.            &fl            /* Pointer to next FDB */
  7285.            );
  7286.         cmfdbi(&fl,            /* A host name or address */
  7287.            _CMFLD,        /* fcode */
  7288.            "Hostname or address", /* help */
  7289.            "",            /* default */
  7290.            "",            /* addtl string data */
  7291.            0,            /* addtl numeric data 1 */
  7292.            0,            /* addtl numeric data 2 */
  7293.            xxstring,
  7294.            NULL,
  7295.            NULL
  7296.            );
  7297.  
  7298.         for (n = 0;; n++) {
  7299.         x = cmfdb(&kw);        /* Parse a hostname or a keyword */
  7300.         if (x == -3) {
  7301.           printf("?ftp open what? \"help ftp\" for hints\n");
  7302.           return(-9);
  7303.         }
  7304.         if (x < 0)
  7305.           break;
  7306.         if (cmresult.fcode == _CMFLD) { /* Hostname */
  7307.             s = cmresult.sresult;
  7308.             break;
  7309.         } else if (cmresult.nresult == OPN_TLS) {
  7310.             usetls = 1;
  7311.         }
  7312.         }
  7313. #endif /* USETLSTAB */
  7314.         if (x < 0) {
  7315.         success = 0;
  7316.         return(x);
  7317.         }
  7318.         ckstrncpy(line,s,LINBUFSIZ);
  7319.         s = line;
  7320.         return(openftp(s,usetls));
  7321.     }
  7322. #endif /* COMMENT */
  7323.  
  7324.       case FTP_PUT:            /* PUT */
  7325.       case FTP_MPU:            /* MPUT */
  7326.       case FTP_APP:            /* APPEND */
  7327.     return(doftpput(cx,1));
  7328.  
  7329.       case FTP_PWD:            /* PWD */
  7330.     x = doftppwd();
  7331.     if (x > -1) success = x;
  7332.     return(x);
  7333.  
  7334.       case FTP_REN:            /* RENAME */
  7335.     return(doftpren());
  7336.  
  7337.       case FTP_RES:            /* RESET */
  7338.     return(doftpres());
  7339.  
  7340.       case FTP_HLP:            /* (remote) HELP */
  7341.     return(doftpxhlp());
  7342.  
  7343.       case FTP_RMD:            /* RMDIR */
  7344.     return(doftprmd());
  7345.  
  7346.       case FTP_STA:            /* STATUS */
  7347.     if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7348.       return(x);
  7349.     CHECKCONN();
  7350.     ckstrncpy(line,s,LINBUFSIZ);
  7351.     if (testing) printf(" ftp status \"%s\"...\n",line);
  7352.     success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  7353.     return(success);
  7354.  
  7355.       case FTP_SIT: {            /* SITE */
  7356.       int reply;
  7357.       if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7358.         return(x);
  7359.       CHECKCONN();
  7360.       ckstrncpy(line,s,LINBUFSIZ);
  7361.       if (testing) printf(" ftp site \"%s\"...\n",line);
  7362.           if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  7363.               do {
  7364.                   reply = getreply(0,lcs,rcs,ftp_vbm,0);
  7365.               } while (reply == REPLY_PRELIM);
  7366.           }
  7367.           return(success = (reply == REPLY_COMPLETE));
  7368.       }
  7369.       case FTP_SIZ:            /* (ask for) SIZE */
  7370.     if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7371.       return(x);
  7372.     CHECKCONN();
  7373.     ckstrncpy(line,s,LINBUFSIZ);
  7374.     if (testing)
  7375.       printf(" ftp size \"%s\"...\n",line);
  7376.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  7377.         return(success);
  7378.  
  7379.       case FTP_SYS:            /* Ask for server's SYSTEM type */
  7380.     if ((x = cmcfm()) < 0) return(x);
  7381.     CHECKCONN();
  7382.     if (testing)
  7383.       printf(" ftp system...\n");
  7384.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  7385.         return(success);
  7386.  
  7387.       case FTP_UMA:            /* Set/query UMASK */
  7388.     if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  7389.       if (x != -3)
  7390.         return(x);
  7391.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7392.     if ((x = cmcfm()) < 0) return(x);
  7393.     CHECKCONN();
  7394.     if (testing) {
  7395.         if (tmpbuf[0])
  7396.           printf(" ftp umask \"%s\"...\n",tmpbuf);
  7397.         else
  7398.           printf(" ftp query umask...\n");
  7399.     }
  7400.         success = ftp_umask(tmpbuf);
  7401.         return(success);
  7402.  
  7403.       case FTP_USR:
  7404.     return(doftpusr());
  7405.  
  7406.       case FTP_QUO:
  7407.     if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  7408.       return(x);
  7409.     CHECKCONN();
  7410.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  7411.         return(success);
  7412.  
  7413.       case FTP_TYP:            /* Type */
  7414.     if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  7415.       return(x);
  7416.     if ((y = cmcfm()) < 0) return(y);
  7417.     CHECKCONN();
  7418.     ftp_typ = x;
  7419.     g_ftp_typ = x;
  7420.     tenex = (ftp_typ == FTT_TEN);
  7421.     changetype(ftp_typ,ftp_vbm);
  7422.     return(1);
  7423.  
  7424.       case FTP_CHK:            /* Check if remote file(s) exist(s) */
  7425.     if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  7426.       return(x);
  7427.     CHECKCONN();
  7428.     success = remote_files(1,(CHAR *)s,0) ? 1 : 0;
  7429.     return(success);
  7430.  
  7431.       /* If there is a useful reply we should parse it & remember results */
  7432.       case FTP_FEA:            /* RFC2389 */
  7433.     if ((y = cmcfm()) < 0)
  7434.       return(y);
  7435.     CHECKCONN();
  7436.         return(success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE));
  7437.  
  7438.       case FTP_OPT:            /* RFC2389 */
  7439.     /* Perhaps this should be a keyword list... */
  7440.     if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  7441.       return(x);
  7442.     CHECKCONN();
  7443.     ckstrncpy(line,s,LINBUFSIZ);
  7444.     if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  7445.       return(x);
  7446.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7447.         return(success);
  7448.     }
  7449.     return(-2);
  7450. }
  7451.  
  7452. #ifndef NOSHOW
  7453. static char *
  7454. shopl(x) int x; {
  7455.     switch (x) {
  7456.       case FPL_CLR: return("clear");
  7457.       case FPL_PRV: return("private");
  7458.       case FPL_SAF: return("safe");
  7459.       case 0:  return("(not set)");
  7460.       default: return("(unknown)");
  7461.     }
  7462. }
  7463.  
  7464. int
  7465. shoftp(brief) {
  7466.     char * s = "?";
  7467.     int n;
  7468.  
  7469.     if (g_ftp_typ > -1) {        /* Restore TYPE if saved */
  7470.     ftp_typ = g_ftp_typ;
  7471.     /* g_ftp_typ = -1; */
  7472.     }
  7473.     printf("\n");
  7474.     printf("FTP connection:                 %s\n",connected ?
  7475.        ftp_host :
  7476.        "(none)"
  7477.        );
  7478.     n = 2;
  7479.     if (connected) {
  7480.     n++;
  7481.     printf("FTP server type:                %s\n",
  7482.            ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  7483.     }
  7484.     if (loggedin)
  7485.       printf("Logged in as:                   %s\n",
  7486.          strval(ftp_logname,"(unknown)"));
  7487.     else
  7488.       printf("Not logged in\n");
  7489.     n++;
  7490.     if (brief) return(0);
  7491.  
  7492.     printf("\nSET FTP values:\n\n");
  7493.     n += 3;
  7494.  
  7495.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  7496.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  7497.     switch (ftp_typ) {
  7498.       case FTT_ASC: s = "text"; break;
  7499.       case FTT_BIN: s = "binary"; break;
  7500.       case FTT_TEN: s = "tenex"; break;
  7501.     }
  7502.     printf(" ftp type:                      %s\n",s);
  7503.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  7504.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  7505.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  7506.     printf(" ftp filenames:                 %s\n",
  7507.        ftp_cnv == 2 ? "auto" : (ftp_cnv ? "converted" : "literal")
  7508.        );
  7509.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  7510.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  7511.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  7512.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  7513.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  7514.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  7515. #ifdef COMMENT
  7516.     /* See note in doxftp() */
  7517.     if (ftp_fnc < 0)
  7518.       ftp_fnc = fncact;
  7519. #endif /* COMMENT */
  7520.     printf(" ftp collision:                 %s\n",
  7521.             fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  7522.     n += 14;
  7523.  
  7524. #ifndef NOCSETS
  7525.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  7526.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7527.  
  7528.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  7529.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7530.  
  7531.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  7532.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7533. #endif /* NOCSETS */
  7534.  
  7535.     switch (fdispla) {
  7536.       case XYFD_N: s = "none"; break;
  7537.       case XYFD_R: s = "serial"; break;
  7538.       case XYFD_C: s = "fullscreen"; break;
  7539.       case XYFD_S: s = "crt"; break;
  7540.       case XYFD_B: s = "brief"; break;
  7541.     }
  7542.     printf(" transfer display:              %s\n",s);
  7543.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7544.     switch (ftpget) {
  7545.       case 0: s = "kermit"; break;
  7546.       case 1: s = "ftp"; break;
  7547.       case 2: s = "auto"; break;
  7548.       default: s = "?";
  7549.     }
  7550.     printf(" get-put-remote:                %s\n",s);
  7551.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7552.  
  7553.     printf("\n");
  7554.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7555.  
  7556. #ifdef FTP_SECURITY
  7557.     printf("Available security methods:    ");
  7558. #ifdef FTP_GSSAPI
  7559.     printf("GSSAPI ");
  7560. #endif /* FTP_GSSAPI */
  7561. #ifdef FTP_KRB4
  7562.     printf("Kerberos4 ");
  7563. #endif /* FTP_KRB4 */
  7564. #ifdef FTP_SRP
  7565.     printf("SRP ");
  7566. #endif /* FTP_SRP */
  7567. #ifdef FTP_SSL
  7568.     printf("SSL ");
  7569. #endif /* FTP_SSL */
  7570.  
  7571.     n++;
  7572.     printf("\n\n");
  7573.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7574.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  7575.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7576.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  7577.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7578.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  7579.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7580.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  7581.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7582.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  7583.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7584.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  7585.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7586. #else
  7587.     printf("Available security methods:     (none)\n");
  7588.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7589. #endif /* FTP_SECURITY */
  7590.  
  7591.     if (n <= cmd_rows - 3)
  7592.       printf("\n");
  7593.     return(0);
  7594. }
  7595. #endif /* NOSHOW */
  7596.  
  7597. #ifndef NOHELP
  7598. /* FTP HELP text strings */
  7599.  
  7600. static char * fhs_ftp[] = {
  7601.     "Syntax: FTP subcommand [ operands ]",
  7602.     "  Makes an FTP connection, or sends a command to the FTP server.",
  7603.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  7604.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  7605.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  7606.     ""
  7607. };
  7608.  
  7609. static char * fhs_acc[] = {        /* ACCOUNT */
  7610.     "Syntax: FTP ACCOUNT text",
  7611.     "  Sends an account designator to an FTP server that needs one.",
  7612.     "  Most FTP servers do not use accounts; some use them for other",
  7613.     "  other purposes, such as disk-access passwords.",
  7614.     ""
  7615. };
  7616. static char * fhs_app[] = {        /* APPEND */
  7617.     "Syntax: FTP APPEND filname",
  7618.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  7619.     ""
  7620. };
  7621. static char * fhs_cls[] = {        /* BYE, CLOSE */
  7622.     "Syntax: [ FTP ] BYE",
  7623.     "  Logs out from the FTP server and closes the FTP connection.",
  7624.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  7625.     ""
  7626. };
  7627. static char * fhs_cwd[] = {        /* CD, CWD */
  7628.     "Syntax: [ FTP ] CD directory",
  7629.     "  Asks the FTP server to change to the given directory.",
  7630.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  7631.     ""
  7632. };
  7633. static char * fhs_gup[] = {        /* CDUP, UP */
  7634.     "Syntax: FTP CDUP",
  7635.     "  Asks the FTP server to change to the parent directory of its current",
  7636.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  7637.     ""
  7638. };
  7639. static char * fhs_chm[] = {        /* CHMOD */
  7640.     "Syntax: FTP CHMOD filename permissions",
  7641.     "  Asks the FTP server to change the permissions, protection, or mode of",
  7642.     "  the given file.  The given permissions must be in the syntax of the",
  7643.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  7644.     "  FTP PUT /PERMISSIONS",
  7645.     ""
  7646. };
  7647. static char * fhs_mde[] = {        /* DELETE */
  7648.     "Syntax: FTP DELETE [ switches ] filespec",
  7649.     "  Asks the FTP server to delete the given file or files.",
  7650.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  7651.     "  multiple file deletion).  Optional switches:",
  7652.     " ",
  7653.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7654.     "  /EXCEPT:pattern",
  7655.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  7656.     "  /LARGER-THAN:number",
  7657. #ifdef UNIXOROSK
  7658.     "  /NODOTFILES",
  7659. #endif /* UNIXOROSK */
  7660.     "  /QUIET",
  7661. #ifdef RECURSIVE
  7662.     "  /RECURSIVE (depends on server)",
  7663.     "  /SUBDIRECTORIES",
  7664. #endif /* RECURSIVE */
  7665.     "  /SMALLER-THAN:number",
  7666.     ""
  7667. };
  7668. static char * fhs_dir[] = {        /* DIRECTORY */
  7669.     "Syntax: FTP DIRECTORY [ filespec ]",
  7670.     "  Asks the server to send a directory listing of the files that match",
  7671.     "  the given filespec, or if none is given, all the files in its current",
  7672.     "  directory.  The filespec, including any wildcards, must be in the",
  7673.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  7674.     "  Synonym: RDIRECTORY.",
  7675.     ""
  7676. };
  7677. static char * fhs_vdi[] = {        /* VDIRECTORY */
  7678.     "Syntax: FTP VDIRECTORY [ filespec ]",
  7679.     "  Asks the server to send a directory listing of the files that match",
  7680.     "  the given filespec, or if none is given, all the files in its current",
  7681.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  7682.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  7683.     "  FTP DIRECTORY lists only filenames without details.",
  7684.     ""
  7685. };
  7686. static char * fhs_fea[] = {        /* FEATURES */
  7687.     "Syntax: FTP FEATURES",
  7688.     "  Asks the FTP server to list its special features.  Most FTP servers",
  7689.     "  do not recognize this command.",
  7690.     ""
  7691. };
  7692. static char * fhs_mge[] = {        /* MGET */
  7693.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  7694.     "  Download a single file or multiple files.  Asks the FTP server to send",
  7695.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  7696.     " ",
  7697.     "  /AS-NAME:text",
  7698.     "    Name under which to store incoming file.",
  7699.     "    Pattern required for for multiple files.",
  7700.     "  /BINARY",            /* /IMAGE */
  7701.     "    Force binary mode.  Synonym: /IMAGE.",
  7702.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  7703.    "    What to do if an incoming file has the same name as an existing file.",
  7704.  
  7705. #ifdef PUTPIPE
  7706.     "  /COMMAND",
  7707.     "    Specifies that the as-name is a command to which the incoming file",
  7708.     "    is to be piped as standard input.",
  7709. #endif /* PUTPIPE */
  7710.     "  /DELETE",
  7711.     "    Specifies that each file is to be deleted from the server after,",
  7712.     "    and only if, it is successfully downloaded.",
  7713.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7714.     "    When downloading a group of files, what to do upon failure to",
  7715.     "    transfer a file: quit or proceed to the next one.",
  7716.     "  /EXCEPT:pattern",
  7717.     "    Exception list: don't download any files that match this pattern.",
  7718.     "    See HELP WILDCARD for pattern syntax.",
  7719.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  7720.     "    Whether to convert incoming filenames to local syntax.",
  7721. #ifdef PIPESEND
  7722. #ifndef NOSPL
  7723.     "  /FILTER:command",
  7724.     "    Pass incoming files through the given command.",
  7725. #endif /* NOSPL */
  7726. #endif /* PIPESEND */
  7727.     "  /LARGER-THAN:number",
  7728.     "    Only download files that are larger than the given number of bytes.",
  7729.     "  /LISTFILE:filename",
  7730.     "    Obtain the list of files to download from the given file.",
  7731. #ifndef NOCSETS
  7732.     "  /LOCAL-CHARACTER-SET:name",
  7733.     "    When downloading in text mode and character-set conversion is",
  7734.     "    desired, this specifies the target set.",
  7735. #endif /* NOCSETS */
  7736. #ifdef CK_TMPDIR
  7737.     "  /MOVE-TO:directory",
  7738.     "    Each file that is downloaded is to be moved to the given local",
  7739.     "    directory immediately after, and only if, it has been received",
  7740.     "    successfully.",
  7741. #endif /* CK_TMPDIR */
  7742.     "  /NAMELIST:filename",
  7743.     "    Instead of downloading the files, stores the list of files that",
  7744.     "    would be downloaded in the given local file, one filename per line.",
  7745.     "  /NOBACKUPFILES",
  7746.     "    Don't download any files whose names end with .~<number>~.",
  7747. #ifdef UNIXOROSK
  7748.     "  /NODOTFILES",
  7749.     "    Don't download any files whose names begin with period (.).",
  7750. #endif /* UNIXOROSK */
  7751.     "  /QUIET",
  7752.     "    Suppress the file-transfer display.",
  7753. #ifdef FTP_RESTART
  7754.     "  /RECOVER",            /* /RESTART */
  7755.     "    Resume a download that was previously interrupted from the point of",
  7756.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  7757.     "    Synonym: /RESTART.",
  7758. #endif /* FTP_RESTART */
  7759. #ifdef RECURSIVE
  7760.     "  /RECURSIVE",            /* /SUBDIRECTORIES */
  7761.     "    Create subdirectories automatically if the server sends files",
  7762.     "    recursively and includes pathnames (most don't).",
  7763. #endif /* RECURSIVE */
  7764.     "  /RENAME-TO:text",
  7765.     "    Each file that is downloaded is to be renamed as indicated just,",
  7766.     "    after, and only if, it has arrived successfully.",
  7767. #ifndef NOCSETS
  7768.     "  /SERVER-CHARACTER-SET:name",
  7769.     "    When downloading in text mode and character-set conversion is desired"
  7770. ,   "    this specifies the original file's character set on the server.",
  7771. #endif /* NOCSETS */
  7772.     "  /SERVER-RENAME:text",
  7773.     "    Each server source file is to be renamed on the server as indicated",
  7774.     "    immediately after, but only if, it has arrived succesfully.",
  7775.     "  /SMALLER-THAN:number",
  7776.     "    Download only those files smaller than the given number of bytes.",
  7777.     "  /TEXT",                /* /ASCII */
  7778.     "    Force text mode.  Synonym: /ASCII.",
  7779.     "  /TENEX",
  7780.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  7781. #ifndef NOCSETS
  7782.     "  /TRANSPARENT",
  7783.     "    When downloading in text mode, do not convert chracter-sets.",
  7784. #endif /* NOCSETS */
  7785.     "  /TO-SCREEN",
  7786.     "    The downloaded file is to be displayed on the screen.",
  7787. #ifdef DOUPDATE
  7788.     "  /UPDATE",
  7789.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  7790.     "    newer than than their local counterparts.",
  7791. #endif /* DOUPDATE */
  7792.     ""
  7793. };
  7794. static char * fhs_hlp[] = {        /* HELP */
  7795.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  7796.     "  Asks the FTP server for help about the given command.  First use",
  7797.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  7798.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  7799.     ""
  7800. };
  7801. static char * fhs_idl[] = {        /* IDLE */
  7802.     "Syntax: FTP IDLE [ number ]",
  7803.     "  If given without a number, this asks the FTP server to tell its",
  7804.     "  current idle-time limit.  If given with a number, it asks the server",
  7805.     "  to change its idle-time limit to the given number of seconds.",
  7806.     ""
  7807. };
  7808. static char * fhs_usr[] = {        /* USER, LOGIN */
  7809.     "Syntax: FTP USER username [ password [ account ] ]",
  7810.     "  Log in to the FTP server.  To be used when connected but not yet",
  7811.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  7812.     "  If you omit the password, and one is required by the server, you are",
  7813.     "  prompted for it.  If you omit the account, no account is sent.",
  7814.     "  Synonym: FTP LOGIN.",
  7815.     ""
  7816. };
  7817. static char * fhs_get[] = {        /* GET */
  7818.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  7819.     "  Download a single file.  Asks the FTP server to send the given file.",
  7820.     "  The optional as-name is the name to store it under when it arrives;",
  7821.     "  if omitted, the file is stored with the name it arrived with, as",
  7822.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  7823.     "  value.  Aside from the file list and as-name, syntax and options are",
  7824.     "  the same as for FTP MGET, which is used for downloading multiple files."
  7825. ,   ""
  7826. };
  7827. static char * fhs_mkd[] = {        /* MKDIR */
  7828.     "Syntax: FTP MKDIR directory",
  7829.     "  Asks the FTP server to create a directory with the given name,",
  7830.     "  which must be in the syntax of the server's file system.  Synonyms:",
  7831.     "  REMOTE MKDIR, RMKDIR.",
  7832.     ""
  7833. };
  7834. static char * fhs_mod[] = {        /* MODTIME */
  7835.     "Syntax: FTP MODTIME filename",
  7836.     "  Asks the FTP server to send the modification time of the given file,",
  7837.     "  to be displayed on the screen.  The date-time format is all numeric:",
  7838.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  7839.     "  fractions of seconds).",
  7840.     ""
  7841. };
  7842. static char * fhs_mpu[] = {        /* MPUT */
  7843.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  7844.     "  Uploads files.  Sends the given file or files to the FTP server.",
  7845.     "  Also see FTP PUT.  Optional switches are:",
  7846.     " ",
  7847.     "  /AFTER:date-time",
  7848.     "    Uploads only those files newer than the given date-time.",
  7849.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  7850. #ifdef PUTARRAY
  7851.     "  /ARRAY:array-designator",
  7852.     "    Tells Kermit to upload the contents of the given array, rather than",
  7853.     "    a file.",
  7854. #endif /* PUTARRAY */
  7855.     "  /AS-NAME:text",
  7856.     "    Name under which to send files.",
  7857.     "    Pattern required for for multiple files.",
  7858.     "  /BEFORE:date-time",
  7859.     "    Upload only those files older than the given date-time.",
  7860.     "  /BINARY",
  7861.     "    Force binary mode.  Synonym: /IMAGE.",
  7862. #ifdef PUTPIPE
  7863.     "  /COMMAND",
  7864.     "    Specifies that the filespec is a command whose standard output is",
  7865.     "    to be sent.",
  7866. #endif /* PUTPIPE */
  7867.     "  /DELETE",
  7868.     "    Specifies that each source file is to be deleted after, and only if,",
  7869.     "    it is successfully uploaded.",
  7870.     "  /DOTFILES",
  7871.     "    Include files whose names begin with period (.).",
  7872.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7873.     "    When uploading a group of files, what to do upon failure to",
  7874.     "    transfer a file: quit or proceed to the next one.",
  7875.     "  /EXCEPT:pattern",
  7876.     "    Exception list: don't upload any files that match this pattern.",
  7877.     "    See HELP WILDCARD for pattern syntax.",
  7878.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  7879.     "    Whether to convert outbound filenames to common syntax.",
  7880. #ifdef PIPESEND
  7881. #ifndef NOSPL
  7882.     "  /FILTER:command",
  7883.     "    Pass outbound files through the given command.",
  7884. #endif /* NOSPL */
  7885. #endif /* PIPESEND */
  7886. #ifdef CKSYMLINK
  7887.     "  /FOLLOWINKS",
  7888.     "    Send files that are pointed to by symbolic links.",
  7889.     "  /NOFOLLOWINKS",
  7890.     "    Skip over symbolic links (default).",
  7891. #endif /* CKSYMLINK */
  7892.     "  /LARGER-THAN:number",
  7893.     "    Only upload files that are larger than the given number of bytes.",
  7894.     "  /LISTFILE:filename",
  7895.     "    Obtain the list of files to upload from the given file.",
  7896. #ifndef NOCSETS
  7897.     "  /LOCAL-CHARACTER-SET:name",
  7898.     "    When uploading in text mode and character-set conversion is",
  7899.     "    desired, this specifies the source-file character set.",
  7900. #endif /* NOCSETS */
  7901. #ifdef CK_TMPDIR
  7902.     "  /MOVE-TO:directory",
  7903.     "    Each source file that is uploaded is to be moved to the given local",
  7904.     "    directory when, and only if, the transfer is successful.",
  7905. #endif /* CK_TMPDIR */
  7906.     "  /NOBACKUPFILES",
  7907.     "    Don't upload any files whose names end with .~<number>~.",
  7908. #ifdef UNIXOROSK
  7909.     "  /NODOTFILES",
  7910.     "    Don't upload any files whose names begin with period (.).",
  7911. #endif /* UNIXOROSK */
  7912.     "  /NOT-AFTER:date-time",
  7913.     "    Upload only files that are not newer than the given date-time",
  7914.     "  /NOT-BEFORE:date-time",
  7915.     "    Upload only files that are not older than the given date-time",
  7916. #ifdef UNIX
  7917.     "  /PERMISSIONS",
  7918.     "    Ask the server to set the permissions of each file it receives",
  7919.     "    according to the source file's permissions.",
  7920. #endif /* UNIX */
  7921.     "  /QUIET",
  7922.     "    Suppress the file-transfer display.",
  7923. #ifdef FTP_RESTART
  7924.     "  /RECOVER",
  7925.     "    Resume an upload that was previously interrupted from the point of",
  7926.     "    failure.  Synonym: /RESTART.",
  7927. #endif /* FTP_RESTART */
  7928. #ifdef RECURSIVE
  7929.     "  /RECURSIVE",
  7930.     "    Send files from the given directory and all the directories beneath",
  7931.     "    it.  Synonym: /SUBDIRECTORIES.",
  7932. #endif /* RECURSIVE */
  7933.     "  /RENAME-TO:text",
  7934.     "    Each source file that is uploaded is to be renamed on the local",
  7935.     "    local computer as indicated when and only if, the transfer completes",
  7936.     "    successfully.",
  7937. #ifndef NOCSETS
  7938.     "  /SERVER-CHARACTER-SET:name",
  7939.     "    When uploading in text mode and character-set conversion is desired,",
  7940.     "    this specifies the character set to which the file should be",
  7941.     "    converted for storage on the server.",
  7942. #endif /* NOCSETS */
  7943.     "  /SERVER-RENAME:text",
  7944.     "    Each file that is uploaded is to be renamed as indicated on the",
  7945.     "    server after, and only if, if arrives successfully.",
  7946.     "  /SIMULATE",
  7947.     "    Show which files would be sent without actually sending them.",
  7948.     "  /SMALLER-THAN:number",
  7949.     "    Upload only those files smaller than the given number of bytes.",
  7950.     "  /TEXT",
  7951.     "    Force text mode.  Synonym: /ASCII.",
  7952.     "  /TENEX",
  7953.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  7954. #ifndef NOCSETS
  7955.     "  /TRANSPARENT",
  7956.     "    When uploading in text mode, do not convert chracter-sets.",
  7957. #endif /* NOCSETS */
  7958.     "  /TYPE:{TEXT,BINARY}",
  7959.     "    Upload only files of the given type.",
  7960. #ifdef DOUPDATE
  7961.     "  /UPDATE",
  7962.     "    If a file of the same name exists on the server, upload only if",
  7963.     "    the local file is newer.",
  7964. #endif /* DOUPDATE */
  7965.     "  /UNIQUE-SERVER-NAMES",
  7966.     "    Ask the server to compute new names for any incoming file that has",
  7967.     "    the same name as an existing file.",
  7968.     ""
  7969. };
  7970. static char * fhs_opn[] = {        /* OPEN */
  7971. #ifdef CK_SSL
  7972.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  7973.     "  Opens a connection to the FTP server on the given host.  The default",
  7974.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  7975.     "  can be supplied if necessary.  Optional switches are:",
  7976. #else /* CK_SSL */
  7977.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  7978.     "  Opens a connection to the FTP server on the given host.  The default",
  7979.     "  TCP port is 21, but a different port number can be supplied if",
  7980.     "  necessary.  Optional switches are:",
  7981. #endif /* CK_SSL */
  7982.     " ",
  7983.     "  /ANONYMOUS",
  7984.     "    Logs you in anonymously.",
  7985.     "  /USER:text",
  7986.     "    Supplies the given text as your username.",
  7987.     "  /PASSWORD:text",
  7988.     "    Supplies the given text as your password.  If you include a username",
  7989.     "    but omit this switch and the server requires a password, you are",
  7990.     "    prompted for it.",
  7991.     "  /ACCOUNT:text",
  7992.     "    Supplies the given text as your account, if required by the server.",
  7993.     "  /ACTIVE",
  7994.     "    Forces an active (rather than passive) connection.",
  7995.     "  /PASSIVE",
  7996.     "    Forces a passive (rather than active) connection.",
  7997.     ""
  7998. };
  7999. static char * fhs_opt[] = {        /* OPTS, OPTIONS */
  8000.     "Syntax: FTP OPTIONS",
  8001.     "  Asks the FTP server to list its current options.  Advanced, new,",
  8002.     "  not supported by most FTP servers.",
  8003.     ""
  8004. };
  8005. static char * fhs_put[] = {        /* PUT, SEND */
  8006.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  8007.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  8008.     "  by an additional field, this is interpreted as the name under which",
  8009.     "  to send the file or files.  See HELP FTP MPUT.",
  8010.     ""
  8011. };
  8012. static char * fhs_pwd[] = {        /* PWD */
  8013.     "Syntax: FTP PWD",
  8014.     "  Asks the FTP server to reveal its current working directory.",
  8015.     "  Synonyms: REMOTE PWD, RPWD.",
  8016.     ""
  8017. };
  8018. static char * fhs_quo[] = {        /* QUOTE */
  8019.     "Syntax: FTP QUOTE text",
  8020.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  8021.     "  for sending commands that Kermit might not support.",
  8022.     ""
  8023. };
  8024. static char * fhs_rge[] = {        /* REGET */
  8025.     "Syntax: FTP REGET",
  8026.     "  Synonym for FTP GET /RECOVER.",
  8027.     ""
  8028. };
  8029. static char * fhs_ren[] = {        /* RENAME */
  8030.     "Syntax: FTP RENAME name1 name1",
  8031.     "  Asks the FTP server to change the name of the file whose name is name1",
  8032.     "  and which resides in the FTP server's file system, to name2.  Works",
  8033.     "  only for single files; wildcards are not accepted.",
  8034.     ""
  8035. };
  8036. static char * fhs_res[] = {        /* RESET */
  8037.     "Syntax: FTP RESET",
  8038.     "  Asks the server to log out your session, terminating your access",
  8039.     "  rights, without closing the connection.",
  8040.     ""
  8041. };
  8042. static char * fhs_rmd[] = {        /* RMDIR */
  8043.     "Syntax: FTP RMDIR directory",
  8044.     "  Asks the FTP server to remove the directory whose name is given.",
  8045.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  8046.     "  RMDIR, RRMDIR.",
  8047.     ""
  8048. };
  8049. static char * fhs_sit[] = {        /* SITE */
  8050.     "Syntax: FTP SITE text",
  8051.     "  Sends a site-specific command to the FTP server.",
  8052.     ""
  8053. };
  8054. static char * fhs_siz[] = {        /* SIZE */
  8055.     "Syntax: FTP SIZE filename",
  8056.     "  Asks the FTP server to send a numeric string representing the size",
  8057.     "  of the given file.",
  8058.     ""
  8059. };
  8060. static char * fhs_sta[] = {        /* STATUS */
  8061.     "Syntax: FTP STATUS [ filename ]",
  8062.     "  Asks the FTP server to report its status.  If a filename is given,",
  8063.     "  the FTP server should report details about the file.",
  8064.     ""
  8065. };
  8066. static char * fhs_sys[] = {        /* SYSTEM */
  8067.     "Syntax: FTP SYSTEM",
  8068.     "  Asks the FTP server to report its operating system type.",
  8069.     ""
  8070. };
  8071. static char * fhs_typ[] = {        /* TYPE */
  8072.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  8073.     "  Puts the client and server in the indicated transfer mode.",
  8074.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  8075.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  8076.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  8077.     "  TENEX mode.",
  8078.     ""
  8079. };
  8080. static char * fhs_uma[] = {        /* UMASK */
  8081.     "Syntax: FTP UMASK number",
  8082.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  8083.     "  only (or mainly) to UNIX-based FTP servers.",
  8084.     ""
  8085. };
  8086. static char * fhs_chk[] = {        /* CHECK */
  8087.     "Syntax: FTP CHECK remote-filespec",
  8088.     "  Asks the FTP server if the given file or files exist.  If the",
  8089.     "  remote-filespec contains wildcards, this command fails if no server",
  8090.     "  files match, and succeeds if at least one file matches.  If the",
  8091.     "  remote-filespec does not contain wildcards, this command succeeds if",
  8092.     "  the given file exists and fails if it does not.",
  8093.     ""
  8094. };
  8095.  
  8096. #endif /* NOHELP */
  8097.  
  8098. int
  8099. doftphlp() {
  8100.     int cx;
  8101.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  8102.       if (cx != -3)
  8103.     return(cx);
  8104.     if ((x = cmcfm()) < 0)
  8105.       return(x);
  8106.  
  8107. #ifdef NOHELP
  8108.     printf("Sorry, no help available\n");
  8109. #else
  8110.     switch (cx) {
  8111.       case -3:
  8112.     return(hmsga(fhs_ftp));
  8113.       case FTP_ACC:            /* ACCOUNT */
  8114.     return(hmsga(fhs_acc));
  8115.       case FTP_APP:            /* APPEND */
  8116.     return(hmsga(fhs_app));
  8117.       case FTP_CLS:            /* BYE, CLOSE */
  8118.     return(hmsga(fhs_cls));
  8119.       case FTP_CWD:            /* CD, CWD */
  8120.     return(hmsga(fhs_cwd));
  8121.       case FTP_GUP:            /* CDUP, UP */
  8122.     return(hmsga(fhs_gup));
  8123.       case FTP_CHM:            /* CHMOD */
  8124.     return(hmsga(fhs_chm));
  8125.       case FTP_MDE:            /* DELETE, MDELETE */
  8126.     return(hmsga(fhs_mde));
  8127.       case FTP_DIR:            /* DIRECTORY */
  8128.     return(hmsga(fhs_dir));
  8129.       case FTP_VDI:            /* VDIRECTORY */
  8130.     return(hmsga(fhs_vdi));
  8131.       case FTP_FEA:            /* FEATURES */
  8132.     return(hmsga(fhs_fea));
  8133.       case FTP_GET:            /* GET */
  8134.     return(hmsga(fhs_get));
  8135.       case FTP_HLP:            /* HELP */
  8136.     return(hmsga(fhs_hlp));
  8137.       case FTP_IDL:            /* IDLE */
  8138.     return(hmsga(fhs_idl));
  8139.       case FTP_USR:            /* USER, LOGIN */
  8140.     return(hmsga(fhs_usr));
  8141.       case FTP_MGE:            /* MGET */
  8142.     return(hmsga(fhs_mge));
  8143.       case FTP_MKD:            /* MKDIR */
  8144.     return(hmsga(fhs_mkd));
  8145.       case FTP_MOD:            /* MODTIME */
  8146.     return(hmsga(fhs_mod));
  8147.       case FTP_MPU:            /* MPUT */
  8148.     return(hmsga(fhs_mpu));
  8149.       case FTP_OPN:            /* OPEN */
  8150.     return(hmsga(fhs_opn));
  8151.       case FTP_OPT:            /* OPTS, OPTIONS */
  8152.     return(hmsga(fhs_opt));
  8153.       case FTP_PUT:            /* PUT, SEND */
  8154.     return(hmsga(fhs_put));
  8155.       case FTP_PWD:            /* PWD */
  8156.     return(hmsga(fhs_pwd));
  8157.       case FTP_QUO:            /* QUOTE */
  8158.     return(hmsga(fhs_quo));
  8159.       case FTP_RGE:            /* REGET */
  8160.     return(hmsga(fhs_rge));
  8161.       case FTP_REN:            /* RENAME */
  8162.     return(hmsga(fhs_ren));
  8163.       case FTP_RES:            /* RESET */
  8164.     return(hmsga(fhs_res));
  8165.       case FTP_RMD:            /* RMDIR */
  8166.     return(hmsga(fhs_rmd));
  8167.       case FTP_SIT:            /* SITE */
  8168.     return(hmsga(fhs_sit));
  8169.       case FTP_SIZ:            /* SIZE */
  8170.     return(hmsga(fhs_siz));
  8171.       case FTP_STA:            /* STATUS */
  8172.     return(hmsga(fhs_sta));
  8173.       case FTP_SYS:            /* SYSTEM */
  8174.     return(hmsga(fhs_sys));
  8175.       case FTP_TYP:            /* TYPE */
  8176.     return(hmsga(fhs_typ));
  8177.       case FTP_UMA:            /* UMASK */
  8178.     return(hmsga(fhs_uma));
  8179.       case FTP_CHK:            /* CHECK */
  8180.     return(hmsga(fhs_chk));
  8181.       default:
  8182.     printf("Sorry, FTP command help not available yet\n");
  8183.     break;
  8184.     }
  8185. #endif /* NOHELP */
  8186.     return(0);
  8187. }
  8188.  
  8189. int
  8190. dosetftphlp() {
  8191.     int cx;
  8192.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  8193.       if (cx != -3)
  8194.     return(cx);
  8195.     if (cx != -3)
  8196.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  8197.     if ((x = cmcfm()) < 0)
  8198.       return(x);
  8199.  
  8200. #ifdef NOHELP
  8201.     printf("Sorry, no help available\n");
  8202. #else
  8203.     switch (cx) {
  8204.       case -3:
  8205.     printf("\nSyntax: SET FTP parameter value\n");
  8206.     printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  8207.     printf("  Type \"help set ftp xxx\" for information about setting\n");
  8208.     printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  8209.     return(0);
  8210.  
  8211. #ifdef FTP_SECURITY
  8212.       case FTS_ATP:            /* "authtype" */
  8213.     printf("\nSyntax: SET FTP AUTHTYPE list\n");
  8214.     printf("  Specifies an ordered list of authentication methods to be\n"
  8215.            );
  8216.     printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  8217.     printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  8218.     return(0);
  8219.  
  8220.       case FTS_AUT:            /* "autoauthentication" */
  8221.     printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  8222.     printf("  Tells whether authentication should be negotiated by the\n");
  8223.     printf("  FTP OPEN command.  Default is ON.\n\n");
  8224.     break;
  8225.  
  8226.       case FTS_CRY:            /* "autoencryption" */
  8227.     printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  8228.     printf("  Tells whether encryption (privacy) should be negotiated\n");
  8229.     printf("  by the FTP OPEN command.  Default is ON.\n\n");
  8230.     break;
  8231.  
  8232. #endif /* FTP_SECURITY */
  8233.       case FTS_LOG:            /* "autologin" */
  8234.     printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  8235.     printf("  Tells Kermit whether to try to log you in automatically\n");
  8236.     printf("  as part of the connection process.\n\n");
  8237.     break;
  8238.  
  8239. #ifndef NOCSETS
  8240.       case FTS_XLA:            /* "character-set-translation" */
  8241.     printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  8242.     printf("  Whether to translate character sets when transferring\n");
  8243.     printf("  text files with FTP.  OFF by default.\n\n");
  8244.     break;
  8245.  
  8246. #endif /* NOCSETS */
  8247.       case FTS_FNC:            /* "collision" */
  8248.     printf("\n");
  8249.     printf(
  8250. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  8251.            );
  8252.     printf("  Tells what do when an incoming file has the same name as\n");
  8253.     printf("  an existing file when downloading with FTP.\n\n");
  8254.     break;
  8255.  
  8256. #ifdef FTP_SECURITY
  8257.       case FTS_CPL:            /* "command-protection-level" */
  8258.     printf("\n");
  8259.     printf(
  8260. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8261.            );
  8262.     printf("\n");
  8263.     printf(
  8264. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  8265.     break;
  8266.       case FTS_CFW:            /* "credential-forwarding" */
  8267.     printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  8268.     printf("  Tells whether end-user credentials are to be forwarded\n");
  8269.     printf("  to the server if supported by the authentication method\n");
  8270.     printf("  (GSSAPI-KRB5 only).\n\n");
  8271.     break;
  8272.       case FTS_DPL:            /* "data-protection-level" */
  8273.     printf("\n");
  8274.     printf(
  8275. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8276.            );
  8277.     printf("\n");
  8278.     printf(
  8279. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  8280.     break;
  8281. #endif /* FTP_SECURITY */
  8282.  
  8283.       case FTS_DBG:            /* "debug" */
  8284.     printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  8285.     printf("  Whether to print FTP protocol messages.\n\n");
  8286.     return(0);
  8287.  
  8288.       case FTS_ERR:            /* "error-action" */
  8289.     printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  8290.     printf("  What to do when an error occurs when transferring a group\n")
  8291.       ;
  8292.     printf("  of files: quit and fail, or proceed to the next file.\n\n");
  8293.     return(0);
  8294.  
  8295.       case FTS_CNV:            /* "filenames" */
  8296.     printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  8297.     printf("  What to do with filenames: convert them, take and use them\n"
  8298.            );
  8299.     printf("  literally; or choose what to do automatically based on the\n"
  8300.            );
  8301.     printf("  OS type of the server.  The default is AUTO.\n\n");
  8302.     return(0);
  8303.  
  8304.       case FTS_PSV:            /* "passive-mode" */
  8305.     printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  8306.     printf("  Whether to use passive mode, which helps to get through\n");
  8307.     printf("  firewalls.  ON by default.\n\n");
  8308.     return(0);
  8309.  
  8310.       case FTS_PRM:            /* "permissions" */
  8311.     printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  8312.     printf("  Whether to try to send file permissions when uploading.\n");
  8313.     printf("  OFF by default.  AUTO means only if client and server\n");
  8314.     printf("  have the same OS type.\n\n");
  8315.     return(0);
  8316.  
  8317.       case FTS_TST:            /* "progress-messages" */
  8318.     printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  8319.     printf("  Whether Kermit should print locally-generated feedback\n");
  8320.     printf("  messages for each non-file-transfer command.");
  8321.     printf("  ON by default.\n\n");
  8322.     return(0);
  8323.  
  8324.       case FTS_SPC:            /* "send-port-commands" */
  8325.     printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  8326.     printf("  Whether Kermit should send a new PORT command for each");
  8327.     printf("  task.\n\n");
  8328.     return(0);
  8329.  
  8330. #ifndef NOCSETS
  8331.       case FTS_CSR:            /* "server-character-set" */
  8332.     printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  8333.     printf("  The name of the character set used for text files on the\n");
  8334.     printf("  server.  Enter a name of '?' for a menu.\n\n");
  8335.     return(0);
  8336. #endif /* NOCSETS */
  8337.  
  8338.       case FTS_TYP:            /* "type" */
  8339.     printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  8340.     printf("  Establishes the default transfer mode.\n");
  8341.     printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  8342.     printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  8343.     printf("  them again.\n");
  8344.     return(0);
  8345.  
  8346. #ifdef PATTERNS
  8347.       case FTS_GFT:
  8348.     printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  8349.     printf("  Tells whether GET and MGET should automatically switch\n");
  8350.     printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  8351.     printf("  matching the name of each incoming file with its list of\n");
  8352.     printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  8353.     printf("  default.  SHOW PATTERNS displays the current pattern\n");
  8354.     printf("  list.  HELP SET FILE to see how to change it.\n");
  8355.     break;
  8356. #endif /* PATTERNS */
  8357.  
  8358.       case FTS_USN:            /* "unique-server-names" */
  8359.     printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  8360.     printf("  Tells whether to ask the server to create unique names\n");
  8361.     printf("  for any uploaded file that has the same name as an\n");
  8362.     printf("  existing file.  Default is OFF.\n\n");
  8363.     return(0);
  8364.  
  8365.       case FTS_VBM:            /* "verbose-mode" */
  8366.     printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  8367.     printf("  Whether to display all responses from the FTP server.\n");
  8368.     printf("  OFF by default.\n\n");
  8369.     return(0);
  8370.  
  8371.       case FTS_DAT:
  8372.     printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  8373.     printf("  Whether to set date of incoming files from the file date\n");
  8374.     printf("  on the server.  OFF by default.  Note: there is no way to\n")
  8375.       ;
  8376.     printf("  set the date on files uploaded to the server.\n\n");
  8377.     return(0);
  8378.  
  8379.       default:
  8380.     printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  8381.     }
  8382. #endif /* NOHELP */
  8383.     return(0);
  8384. }
  8385.  
  8386. #ifndef L_SET
  8387. #define L_SET 0
  8388. #endif /* L_SET */
  8389. #ifndef L_INCR
  8390. #define L_INCR 1
  8391. #endif /* L_INCR */
  8392.  
  8393. #ifdef FTP_SRP
  8394. char srp_user[BUFSIZ];            /* where is BUFSIZ defined? */
  8395. char *srp_pass;
  8396. char *srp_acct;
  8397. #endif /* FTP_SRP */
  8398.  
  8399. static int kerror;            /* Needed for all auth types */
  8400.  
  8401. static struct    sockaddr_in hisctladdr;
  8402. static struct    sockaddr_in hisdataaddr;
  8403. static struct    sockaddr_in data_addr;
  8404. static int    data = -1;
  8405. static int    ptflag = 0;
  8406. static struct    sockaddr_in myctladdr;
  8407.  
  8408. #ifdef COMMENT
  8409. #ifndef OS2
  8410. UID_T getuid();
  8411. #endif /* OS2 */
  8412. #endif /* COMMENT */
  8413.  
  8414. #ifndef MAXHOSTNAMELEN
  8415. #define MAXHOSTNAMELEN 64
  8416. #endif /* MAXHOSTNAMELEN */
  8417.  
  8418. #define herror() printf("Unknown host\n")
  8419.  
  8420. static int cpend = 0;            /* No pending replies */
  8421.  
  8422. #ifdef CK_SSL
  8423. extern SSL *ssl_ftp_con;
  8424. extern SSL_CTX *ssl_ftp_ctx;
  8425. extern SSL *ssl_ftp_data_con;
  8426. extern int ssl_ftp_active_flag;
  8427. extern int ssl_ftp_data_active_flag;
  8428. #endif /* CK_SSL */
  8429.  
  8430. /*  f t p c m d  --  Send a command to the FTP server  */
  8431. /*
  8432.   Call with:
  8433.     char * cmd: The command to send.
  8434.     char * arg: The argument (e.g. a filename).
  8435.     int lcs: The local character set index.
  8436.     int rcs: The remote (server) character set index.
  8437.     int vbm: Verbose mode:
  8438.       0 = force verbosity off
  8439.      >0 = force verbosity on
  8440.  
  8441.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  8442.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  8443.   character set to the remote one before sending the result to the server.
  8444.  
  8445.    Returns:
  8446.     0 on failure with ftpcode = -1
  8447.     >= 0 on success (getreply() result) with ftpcode = 0.
  8448. */
  8449. static char xcmdbuf[RFNBUFSIZ];
  8450.  
  8451. static int
  8452. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  8453.     char * s;
  8454.     int r, x, len = 0, cmdlen = 0;
  8455.     sig_t oldintr;
  8456.  
  8457.     if (ftp_deb)                        /* DEBUG */
  8458.       vbm = 1;
  8459.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  8460.       vbm = 0;
  8461.     else if (vbm < 0)            /* VERBOSE */
  8462.       vbm = ftp_vbm;
  8463.  
  8464.     cancelfile = 0;
  8465.     if (!cmd) cmd = "";
  8466.     if (!arg) arg = "";
  8467.     cmdlen = (int)strlen(cmd);
  8468.     len = cmdlen + (int)strlen(arg) + 1;
  8469.  
  8470.     if (ftp_deb /* && !dpyactive */ ) {
  8471. #ifdef FTP_PROXY
  8472.         if (ftp_prx) printf("%s ", ftp_host);
  8473. #endif /* FTP_PROXY */
  8474.         printf("---> ");
  8475.         if (!anonymous && strcmp("PASS",cmd) == 0)
  8476.       printf("PASS XXXX");
  8477.         else
  8478.       printf("%s %s",cmd,arg);
  8479.         printf("\n");
  8480.     }
  8481.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  8482.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  8483.  
  8484. #ifdef DEBUG
  8485.     if (deblog) {
  8486.     if (!anonymous && strcmp("PASS", cmd) == 0) {
  8487.         debug(F111,"ftpcmd buf1","PASS XXXX",csocket);
  8488.     } else {
  8489.         debug(F111,"ftpcmd buf1",xcmdbuf,csocket);
  8490.     }
  8491.     debug(F101,"ftpcmd lcs","",lcs);
  8492.     debug(F101,"ftpcmd rcs","",rcs);
  8493.     }
  8494. #endif /* DEBUG */
  8495.     if (csocket == -1) {
  8496.         perror("No control connection for command");
  8497.         ftpcode = -1;
  8498.         return(0);
  8499.     }
  8500.     oldintr = signal(SIGINT, cmdcancel);
  8501.  
  8502. #ifndef NOCSETS
  8503.     if (*arg &&                /* If an arg was given */
  8504.     lcs > -1 &&            /* and a local charset */
  8505.     rcs > -1 &&            /* and a remote charset */
  8506.     lcs != rcs &&            /* and the two are not the same */
  8507.     lcs != FC_UCS2 &&        /* and neither one is UCS-2 */
  8508.     rcs != FC_UCS2            /* ... */
  8509.     ) {
  8510.     initxlate(lcs,rcs);        /* Translate arg from lcs to rcs */
  8511.     xgnbp = arg;            /* Global pointer to input string */
  8512.     rfnptr = rfnbuf;        /* Global pointer to output buffer */
  8513.     while (1) {
  8514.         c0 = xgnbyte(FC_UCS2,lcs,strgetc); /* Get byte from name string */
  8515.         if (c0 < 0)
  8516.           break;
  8517.         c1 = xgnbyte(FC_UCS2,lcs,strgetc); /* Second byte from string */
  8518.         if (c1 < 0)
  8519.           break;
  8520.         if ((x = xpnbyte(c0,TC_UCS2,rcs,strputc)) < 0) break;
  8521.         if ((x = xpnbyte(c1,TC_UCS2,rcs,strputc)) < 0) break;
  8522.     }
  8523.     /*
  8524.       We have to copy here instead of translating directly into
  8525.       xcmdbuf[] so strputc() can check length.  Alternatively we could
  8526.       write yet another xpnbyte() output function.
  8527.     */
  8528.     if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  8529.         printf("?FTP command too long: %s + arg\n",cmd,arg);
  8530.         ftpcode = -1;
  8531.         return(0);
  8532.     }
  8533.     x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  8534.     }
  8535. #endif /* NOCSETS */
  8536.  
  8537.     s = xcmdbuf;            /* Command to send to server */
  8538.     debug(F110,"ftpcmd buf2",s,0);
  8539.  
  8540. #ifdef CK_ENCRYPTION
  8541.   again:
  8542. #endif /* CK_ENCRYPTION */
  8543.     if (scommand(s) == 0)        /* Send it. */
  8544.       return(0);
  8545.     cpend = 1;
  8546.  
  8547.     r = getreply(!strcmp(cmd, "QUIT"),lcs,rcs,vbm,
  8548.                  !(strncmp(cmd, "AUTH ", 5)
  8549. #ifdef FTPHOST
  8550.                     && strncmp(cmd, "HOST ",5)
  8551. #endif /* FTPHOST */
  8552.                     ));
  8553. #ifdef CK_ENCRYPTION
  8554.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  8555.         fprintf(stderr,
  8556.            "ENC command not supported at server; retrying under MIC...\n");
  8557.         ftp_cpl = FPL_SAF;
  8558.         goto again;
  8559.     }
  8560. #endif /* CK_ENCRYPTION */
  8561. #ifdef COMMENT
  8562.     if (cancelfile && oldintr != SIG_IGN)
  8563.       (*oldintr)(SIGINT);
  8564.     signal(SIGINT, oldintr);
  8565. #endif /* COMMENT */
  8566.     return(r);
  8567. }
  8568.  
  8569.  
  8570. static VOID
  8571. lostpeer() {
  8572.     debug(F100,"lostpeer","",0);
  8573.     if (connected) {
  8574.         if (csocket != -1) {
  8575. #ifdef CK_SSL
  8576.             if (ssl_ftp_active_flag) {
  8577.                 SSL_shutdown(ssl_ftp_con);
  8578.                 SSL_free(ssl_ftp_con);
  8579.                 ssl_ftp_proxy = 0;
  8580.                 ssl_ftp_active_flag = 0;
  8581.                 ssl_ftp_con = NULL;
  8582.             }
  8583. #endif /* CK_SSL */
  8584. #ifdef TCPIPLIB
  8585.             socket_close(csocket);
  8586. #else /* TCPIPLIB */
  8587. #ifdef USE_SHUTDOWN
  8588.             shutdown(csocket, 1+1);
  8589. #endif /* USE_SHUTDOWN */
  8590.             close(csocket);
  8591. #endif /* TCPIPLIB */
  8592.             csocket = -1;
  8593.         }
  8594.         if (data != -1) {
  8595. #ifdef CK_SSL
  8596.             if (ssl_ftp_data_active_flag) {
  8597.                 SSL_shutdown(ssl_ftp_data_con);
  8598.                 SSL_free(ssl_ftp_data_con);
  8599.                 ssl_ftp_data_active_flag = 0;
  8600.                 ssl_ftp_data_con = NULL;
  8601.             }
  8602. #endif /* CK_SSL */
  8603. #ifdef TCPIPLIB
  8604.             socket_close(data);
  8605. #else /* TCPIPLIB */
  8606. #ifdef USE_SHUTDOWN
  8607.             shutdown(data, 1+1);
  8608. #endif /* USE_SHUTDOWN */
  8609.             close(data);
  8610. #endif /* TCPIPLIB */
  8611.             data = -1;
  8612.         globaldin = -1;
  8613.         }
  8614.         connected = 0;
  8615.     anonymous = 0;
  8616.     loggedin = 0;
  8617.         auth_type = NULL;
  8618.         ftp_cpl = ftp_dpl = FPL_CLR;
  8619. #ifdef CKLOGDIAL
  8620.     ftplogend();
  8621. #endif /* CKLOGDIAL */
  8622.     }
  8623. #ifdef FTP_PROXY
  8624.     pswitch(1);
  8625.     if (connected) {
  8626.         if (csocket != -1) {
  8627. #ifdef TCPIPLIB
  8628.             socket_close(csocket);
  8629. #else /* TCPIPLIB */
  8630. #ifdef USE_SHUTDOWN
  8631.             shutdown(csocket, 1+1);
  8632. #endif /* USE_SHUTDOWN */
  8633.             close(csocket);
  8634. #endif /* TCPIPLIB */
  8635.             csocket = -1;
  8636.         }
  8637.         connected = 0;
  8638.     anonymous = 0;
  8639.     loggedin = 0;
  8640.         auth_type = NULL;
  8641.         ftp_cpl = ftp_dpl = FPL_CLR;
  8642.     }
  8643.     proxflag = 0;
  8644.     pswitch(0);
  8645. #endif /* FTP_PROXY */
  8646. }
  8647.  
  8648. int
  8649. ftpisopen() {
  8650.     return(connected);
  8651. }
  8652.  
  8653. static int
  8654. ftpclose() {
  8655.     if (!connected)
  8656.       return(0);
  8657.     if (!ftp_vbm && !quiet) printlines = 1;
  8658.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  8659.     if (csocket) {
  8660. #ifdef CK_SSL
  8661.         if (ssl_ftp_active_flag) {
  8662.             SSL_shutdown(ssl_ftp_con);
  8663.             SSL_free(ssl_ftp_con);
  8664.         ssl_ftp_proxy = 0;
  8665.             ssl_ftp_active_flag = 0;
  8666.             ssl_ftp_con = NULL;
  8667.         }
  8668. #endif /* CK_SSL */
  8669. #ifdef TCPIPLIB
  8670.         socket_close(csocket);
  8671. #else /* TCPIPLIB */
  8672. #ifdef USE_SHUTDOWN
  8673.         shutdown(csocket, 1+1);
  8674. #endif /* USE_SHUTDOWN */
  8675.         close(csocket);
  8676. #endif /* TCPIPLIB */
  8677.     }
  8678.     csocket = -1;
  8679.     connected = 0;
  8680.     anonymous = 0;
  8681.     loggedin = 0;
  8682.     data = -1;
  8683.     globaldin = -1;
  8684. #ifdef FTP_PROXY
  8685.     if (!proxy)
  8686.       macnum = 0;
  8687. #endif /* FTP_PROXY */
  8688.     auth_type = NULL;
  8689.     ftp_dpl = FPL_CLR;
  8690. #ifdef CKLOGDIAL
  8691.     ftplogend();
  8692. #endif /* CKLOGDIAL */
  8693.     return(0);
  8694. }
  8695.  
  8696. int
  8697. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  8698.     char * host;
  8699.  
  8700.     if (connected) {
  8701.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  8702.         ftpcode = -1;
  8703.         return(0);
  8704.     }
  8705. #ifdef FTPHOST
  8706.     hostcmd = 0;
  8707. #endif /* FTPHOST */
  8708.     alike = 0;
  8709.     ftp_srvtyp[0] = NUL;
  8710.     if (!service) service = "";
  8711.     if (!*service) service = use_tls ? "ftps" : "ftp";
  8712.  
  8713.     if (!isdigit(service[0])) {
  8714.         struct servent *destsp;
  8715.         destsp = getservbyname(service, "tcp");
  8716.         if (!destsp) {
  8717.             if (!ckstrcmp(service,"ftp",-1,0)) {
  8718.         ftp_port = 21;
  8719.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  8720.         ftp_port = 990;
  8721.             } else {
  8722.                 printf("?Bad port name - \"%s\"\n", service);
  8723.                 ftpcode = -1;
  8724.                 return(0);
  8725.             }
  8726.         } else {
  8727.             ftp_port = destsp->s_port;
  8728.             ftp_port = ntohs(ftp_port);
  8729.         }
  8730.     } else
  8731.         ftp_port = atoi(service);
  8732.     if (ftp_port <= 0) {
  8733.     printf("?Bad port name - \"%s\"\n", service);
  8734.         ftpcode = -1;
  8735.         return(0);
  8736.     }
  8737.     host = ftp_hookup(remote, ftp_port, use_tls);
  8738.     if (host) {
  8739.         connected = 1;            /* Set FTP defaults */
  8740.         ftp_cpl = ftp_dpl = FPL_CLR;
  8741.     curtype = FTT_ASC;        /* Server uses ASCII mode */
  8742.         form = FORM_N;
  8743.         mode = MODE_S;
  8744.         stru = STRU_F;
  8745.         strcpy(bytename, "8");
  8746.         bytesize = 8;
  8747.  
  8748.         if (ftp_aut) {
  8749. #ifdef FTP_SECURITY
  8750.             if (ftp_auth()) {
  8751.                 if (ftp_cry) {
  8752.                     if (!quiet)
  8753.               printf("FTP Command channel is Private (encrypted)\n");
  8754.                     ftp_cpl = FPL_PRV;
  8755.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  8756.                         /* a failure here is most likely caused by a mixup */
  8757.                         /* in the session key used by client and server    */
  8758.                         return(0);
  8759.                     }
  8760.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  8761.                         if (!quiet)
  8762.               printf("FTP Data channel is Private (encrypted)\n");
  8763.                         ftp_dpl = FPL_PRV;
  8764.                     } else
  8765.               printf("?Unable to enable encryption on data channel\n");
  8766.                 } else {
  8767.                     ftp_cpl = FPL_SAF;
  8768.                 }
  8769.             }
  8770. #endif /* FTP_SECURITY */
  8771.     }
  8772.     if (ftp_log)
  8773.       ftp_login(remote);
  8774. #ifdef CKLOGDIAL
  8775.     dologftp();
  8776. #endif /* CKLOGDIAL */
  8777.     passivemode = ftp_psv;
  8778.     sendport = ftp_spc;
  8779.  
  8780.     if (ucbuf == NULL) {
  8781.         actualbuf = DEFAULT_PBSZ;
  8782.         while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  8783.           actualbuf >>= 2;
  8784.     }
  8785.     if (!maxbuf)
  8786.       ucbufsiz = actualbuf - FUDGE_FACTOR;
  8787.     return(1);
  8788.     }
  8789.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  8790.     ftpcode = -1;
  8791.     return(0);
  8792. }
  8793.  
  8794. #ifdef CK_SSL
  8795. int
  8796. ssl_auth() {
  8797.     int i;
  8798.     char* p;
  8799.  
  8800.     if (ssl_debug_flag) {
  8801.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  8802.         fflush(stderr);
  8803.         /* for the moment I want the output on screen */
  8804.     }
  8805.     if (ssl_ftp_data_con != NULL) {
  8806.         SSL_free(ssl_ftp_data_con);
  8807.         ssl_ftp_data_con = NULL;
  8808.     }
  8809.     if (ssl_ftp_con != NULL) {
  8810.         SSL_free(ssl_ftp_con);
  8811.         ssl_ftp_con=NULL;
  8812.     }
  8813.     if (ssl_ftp_ctx != NULL) {
  8814.         SSL_CTX_free(ssl_ftp_ctx);
  8815.         ssl_ftp_ctx = NULL;
  8816.     }
  8817. #ifdef COMMENT
  8818.     if (auth_type && !strcmp(auth_type,"TLS")) {
  8819.         ssl_ftp_ctx=SSL_CTX_new(TLSv1_client_method());
  8820.         if (!ssl_ftp_ctx)
  8821.       return(0);
  8822.         SSL_CTX_set_options(ssl_ftp_ctx,
  8823.            SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  8824.                 );
  8825.     } else {
  8826.         ssl_ftp_ctx = SSL_CTX_new(SSLv3_client_method());
  8827.         if (!ssl_ftp_ctx)
  8828.       return(0);
  8829.         SSL_CTX_set_options(ssl_ftp_ctx,SSL_OP_ALL);
  8830.     }
  8831. #else /* COMMENT */
  8832.     ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  8833.     if (!ssl_ftp_ctx)
  8834.       return(0);
  8835.     SSL_CTX_set_options(ssl_ftp_ctx,
  8836.             SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  8837.             );
  8838. #endif /* COMMENT */
  8839.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  8840.                   (pem_password_cb *)ssl_passwd_callback);
  8841.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  8842.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  8843.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  8844.     SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,ssl_verify_dir);
  8845.  
  8846.     /* set up the new CRL Store */
  8847.     crl_store = (X509_STORE *)X509_STORE_new();
  8848.     if (crl_store) {
  8849.         if (ssl_crl_file || ssl_crl_dir) {
  8850.             X509_STORE_load_locations(crl_store,ssl_crl_file,ssl_crl_dir);
  8851.         } else {
  8852.             X509_STORE_set_default_paths(crl_store);
  8853.         }
  8854.     }
  8855.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  8856.                ssl_client_verify_callback);
  8857.     ssl_verify_depth = -1;
  8858.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  8859.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  8860.     SSL_set_fd(ssl_ftp_con,csocket);
  8861.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  8862.     if (ssl_cipher_list) {
  8863.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  8864.     } else {
  8865.         char * p;
  8866.         if (p = getenv("SSL_CIPHER")) {
  8867.             SSL_set_cipher_list(ssl_ftp_con,p);
  8868.         } else {
  8869.             SSL_set_cipher_list(ssl_ftp_con,
  8870.                 "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP"
  8871.                 );
  8872.         }
  8873.     }
  8874.     if (ssl_debug_flag) {
  8875.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  8876.         fflush(stderr);
  8877.     }
  8878.     if (SSL_connect(ssl_ftp_con) <= 0) {
  8879.         static char errbuf[1024];
  8880.     ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  8881.          ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  8882.         fprintf(stderr,"%s\n", errbuf);
  8883.         fflush(stderr);
  8884.         ssl_ftp_active_flag=0;
  8885.         SSL_free(ssl_ftp_con);
  8886.         ssl_ftp_con = NULL;
  8887.     } else {
  8888.         ssl_ftp_active_flag = 1;
  8889.  
  8890.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  8891.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  8892.  
  8893.             if (!subject) {
  8894.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  8895.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  8896.                     auth_finished(AUTH_REJECT);
  8897.                     return(ssl_ftp_active_flag = 0);
  8898.                 } else {
  8899.                     char * prmpt =
  8900.        "Warning: Server didn't provide a certificate, continue? (Y/N) ";
  8901.                     if (!getyesno(prmpt,0)) {
  8902.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  8903.                         return(ssl_ftp_active_flag = 0);
  8904.                     }
  8905.                 }
  8906.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_host)) {
  8907.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  8908.                 return(ssl_ftp_active_flag = 0);
  8909.             }
  8910.         }
  8911.         debug(F110,"ssl_auth","[SSL - OK]",0);
  8912.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  8913.     }
  8914.     if (ssl_debug_flag) {
  8915.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  8916.         fflush(stderr);
  8917.     }
  8918.     return(ssl_ftp_active_flag);
  8919. }
  8920. #endif /* CK_SSL */
  8921.  
  8922. static sigtype
  8923. cmdcancel(sig) int sig; {
  8924.     printf("^C...\n");
  8925.     fflush(stdout);
  8926.     cancelfile++;
  8927. #ifndef OS2
  8928.     if (ptflag)                /* proxy... */
  8929.       longjmp(ptcancel,1);
  8930. #endif /* OS2 */
  8931. }
  8932.  
  8933. static int
  8934. #ifdef CK_ANSIC
  8935. scommand(char * s)            /* Was secure_command() */
  8936. #else
  8937. scommand(s) char * s;
  8938. #endif /* CK_ANSIC */
  8939. {
  8940.     int length = 0, len2;
  8941.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  8942. #ifdef CK_SSL
  8943.     if (ssl_ftp_active_flag) {
  8944.         int error, rc;
  8945.         length = strlen(s) + 2;
  8946.         length = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  8947.         rc = SSL_write(ssl_ftp_con,out,length);
  8948.         error = SSL_get_error(ssl_ftp_con,rc);
  8949.         switch (error) {
  8950.       case SSL_ERROR_NONE:
  8951.             return(1);
  8952.       case SSL_ERROR_WANT_WRITE:
  8953.       case SSL_ERROR_WANT_READ:
  8954.       case SSL_ERROR_SYSCALL:
  8955. #ifdef NT
  8956.             {
  8957.                 int gle = GetLastError();
  8958.             }
  8959. #endif /* NT */
  8960.       case SSL_ERROR_WANT_X509_LOOKUP:
  8961.       case SSL_ERROR_SSL:
  8962.       case SSL_ERROR_ZERO_RETURN:
  8963.       default:
  8964.             lostpeer();
  8965.         }
  8966.     return(0);
  8967.     }
  8968. #endif /* CK_SSL */
  8969.  
  8970.     if (auth_type && ftp_cpl != FPL_CLR) {
  8971. #ifdef FTP_SRP
  8972.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  8973.       if ((length = srp_encode(ftp_cpl == FPL_PRV,
  8974.                    (CHAR *)s,
  8975.                    (CHAR *)out,
  8976.                    strlen(s))) < 0) {
  8977.           fprintf(stderr, "SRP failed to encode message\n");
  8978.           return(0);
  8979.       }
  8980. #endif /* FTP_SRP */
  8981. #ifdef FTP_KRB4
  8982.         if (ck_krb4_is_installed() &&
  8983.         (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  8984.             if (ftp_cpl == FPL_PRV) {
  8985.                 length =
  8986.           krb_mk_priv((CHAR *)s, (CHAR *)out,
  8987.                   strlen(s), ftp_sched,
  8988. #ifdef KRB524
  8989.                   ftp_cred.session,
  8990. #else /* KRB524 */
  8991.                   &ftp_cred.session,
  8992. #endif /* KRB524 */
  8993.                   &myctladdr, &hisctladdr);
  8994.             } else {
  8995.                 length =
  8996.           krb_mk_safe((CHAR *)s,
  8997.                   (CHAR *)out,
  8998.                   strlen(s),
  8999. #ifdef KRB524
  9000.                   ftp_cred.session,
  9001. #else /* KRB524 */
  9002.                   &ftp_cred.session,
  9003. #endif /* KRB524 */
  9004.                   &myctladdr, &hisctladdr);
  9005.             }
  9006.             if (length == -1) {
  9007.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  9008.             ftp_cpl == FPL_PRV ? "priv" : "safe");
  9009.                 return(0);
  9010.             }
  9011.         }
  9012. #endif /* FTP_KRB4 */
  9013. #ifdef FTP_GSSAPI
  9014.         /* Scommand (based on level) */
  9015.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  9016.             gss_buffer_desc in_buf, out_buf;
  9017.             OM_uint32 maj_stat, min_stat;
  9018.             int conf_state;
  9019.             in_buf.value = s;
  9020.             in_buf.length = strlen(s) + 1;
  9021.             maj_stat = gss_seal(&min_stat, gcontext,
  9022.                 (ftp_cpl==FPL_PRV), /* private */
  9023.                 GSS_C_QOP_DEFAULT,
  9024.                 &in_buf, &conf_state,
  9025.                 &out_buf);
  9026.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  9027.                 user_gss_error(maj_stat, min_stat,
  9028.                    (ftp_cpl==FPL_PRV)?
  9029.                    "gss_seal ENC didn't complete":
  9030.                    "gss_seal MIC didn't complete");
  9031.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  9032.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  9033.             } else {
  9034.                 if (ftp_deb)
  9035.           fprintf(stderr, "sealed (%s) %d bytes\n",
  9036.               ftp_cpl==FPL_PRV?"ENC":"MIC",
  9037.               out_buf.length);
  9038.                 memcpy(out, out_buf.value,
  9039.                length=out_buf.length);
  9040.                 gss_release_buffer(&min_stat, &out_buf);
  9041.             }
  9042.         }
  9043. #endif /* FTP_GSSAPI */
  9044.         /* Other auth types go here ... */
  9045.  
  9046.         len2 = FTP_BUFSIZ;
  9047.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  9048.                    length, &len2, RADIX_ENCODE))
  9049.         ) {
  9050.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  9051.             radix_error(kerror));
  9052.             return(0);
  9053.         }
  9054.         if (ftp_deb)
  9055.       fprintf(stderr, "scommand(%s)\nencoding %d bytes\n",
  9056.           s, length);
  9057.         len2 = ckmakmsg(out,sizeof(out),ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  9058.             in, "\r\n", NULL);
  9059.         send(csocket,(SENDARG2TYPE)out,len2,0);
  9060.     } else {
  9061.         char out[FTP_BUFSIZ];
  9062.         int len = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  9063.         send(csocket,(SENDARG2TYPE)out,len,0);
  9064.     }
  9065.     return(1);
  9066. }
  9067.  
  9068. static int
  9069. mygetc() {
  9070.     static char inbuf[4096];
  9071.     static int bp = 0, ep = 0;
  9072.     int rc;
  9073.  
  9074.     if (bp == ep) {
  9075.         bp = ep = 0;
  9076. #ifdef CK_SSL
  9077.         if (ssl_ftp_active_flag) {
  9078.             int error;
  9079.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  9080.             error = SSL_get_error(ssl_ftp_con,rc);
  9081.             switch (error) {
  9082.           case SSL_ERROR_NONE:
  9083.                 break;
  9084.           case SSL_ERROR_WANT_WRITE:
  9085.           case SSL_ERROR_WANT_READ:
  9086.                 return(0);
  9087.           case SSL_ERROR_SYSCALL:
  9088.         if (rc == 0) {        /* EOF */
  9089.             break;
  9090.         } else {
  9091. #ifdef NT
  9092.             int gle = GetLastError();
  9093. #endif /* NT */
  9094.             break;
  9095.         }
  9096.           case SSL_ERROR_WANT_X509_LOOKUP:
  9097.           case SSL_ERROR_SSL:
  9098.           case SSL_ERROR_ZERO_RETURN:
  9099.           default:
  9100.                 break;
  9101.             }
  9102.         } else
  9103. #endif /* CK_SSL */
  9104.       rc = recv(csocket,(char *)inbuf,4096,0);
  9105.         if (rc <= 0)
  9106.       return(EOF);
  9107.         ep = rc;
  9108.     }
  9109.     return(inbuf[bp++]);
  9110. }
  9111.  
  9112. /*  x l a t e c  --  Translate a character  */
  9113. /*
  9114.     Call with:
  9115.       fc    = Function code: 0 = translate, 1 = initialize.
  9116.       c     = Character (as int).
  9117.       incs  = Index of charsets to translate from.
  9118.       outcs = Index of charsets to translate to.
  9119.  
  9120.     Returns:
  9121.       0: OK
  9122.      -1: Error
  9123. */
  9124. static int
  9125. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  9126. #ifdef NOCSETS
  9127.     return(c);
  9128. #else
  9129.     static char buf[128];
  9130.     static int cx;
  9131.     int c0;
  9132.  
  9133.     if (fc == 1) {            /* Initialize */
  9134.     cx = 0;                /* Catch-up buffer write index */
  9135.     xgnbp = buf;            /* Catch-up buffer read pointer */
  9136.     buf[0] = NUL;            /* Buffer is empty */
  9137.     return(0);
  9138.     }
  9139.     if (cx >= 127) {            /* Catch-up buffer full */
  9140.     debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  9141.     printf("?Translation buffer overflow\n");
  9142.     return(-1);
  9143.     }
  9144.     /* Add char to buffer. */
  9145.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  9146.  
  9147.     debug(F000,"xlatec buf",ckitoa(cx),c);
  9148.     buf[cx++] = c;
  9149.     buf[cx] = NUL;
  9150.  
  9151.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  9152.     if (xpnbyte(c0,TC_UCS2,outcs,xprintc) < 0)
  9153.       return(-1);
  9154.     }
  9155.     /* If we're caught up, reinitialize the buffer */
  9156.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  9157. #endif /* NOCSETS */
  9158. }
  9159.  
  9160. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  9161.  
  9162.  
  9163. static int
  9164. getreply(expecteof,lcs,rcs,vbm,auth) int expecteof, lcs, rcs, vbm, auth; {
  9165.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  9166.     register int i, c, n;
  9167.     register int dig;
  9168.     register char *cp;
  9169.     int xlate = 0;
  9170.     int originalcode = 0, continuation = 0;
  9171.     sig_t oldintr;
  9172.     int pflag = 0;
  9173.     char *pt = pasv;
  9174.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  9175.     int safe = 0;
  9176.  
  9177. #ifndef NOCSETS
  9178.     debug(F101,"ftp getreply lcs","",lcs);
  9179.     debug(F101,"ftp getreply rcs","",rcs);
  9180.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  9181.     xlate = 1;
  9182.     initxlate(rcs,lcs);
  9183.     xlatec(1,0,rcs,lcs);
  9184.     }
  9185. #endif /* NOCSETS */
  9186.  
  9187.     if (ftp_deb)                        /* DEBUG */
  9188.       vbm = 1;
  9189.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9190.       vbm = 0;
  9191.     else if (vbm < 0)            /* VERBOSE */
  9192.       vbm = ftp_vbm;
  9193.  
  9194.     ibuf[0] = '\0';
  9195.     if (reply_parse)
  9196.       reply_ptr = reply_buf;
  9197.     oldintr = signal(SIGINT, cmdcancel);
  9198.     for (;;) {
  9199.         obuf[0] = '\0';
  9200.         dig = n = ftpcode = i = 0;
  9201.         cp = ftp_reply_str;
  9202.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  9203.             if (c == IAC) {        /* Handle telnet commands */
  9204.                 switch (c = mygetc()) {
  9205.           case WILL:
  9206.           case WONT:
  9207.                     c = mygetc();
  9208.             obuf[0] = IAC;
  9209.             obuf[1] = DONT;
  9210.             obuf[2] = c;
  9211.             obuf[3] = NUL;
  9212. #ifdef CK_SSL
  9213.                     if (ssl_ftp_active_flag) {
  9214.                         int error, rc;
  9215.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  9216.                         error = SSL_get_error(ssl_ftp_con,rc);
  9217.                         switch (error) {
  9218.               case SSL_ERROR_NONE:
  9219.                             break;
  9220.               case SSL_ERROR_WANT_WRITE:
  9221.               case SSL_ERROR_WANT_READ:
  9222.                             return(0);
  9223.               case SSL_ERROR_SYSCALL:
  9224.                             if (rc == 0) { /* EOF */
  9225.                                 break;
  9226.                             } else {
  9227. #ifdef NT
  9228.                                 int gle = GetLastError();
  9229. #endif /* NT */
  9230.                                 break;
  9231.                             }
  9232.               case SSL_ERROR_WANT_X509_LOOKUP:
  9233.               case SSL_ERROR_SSL:
  9234.               case SSL_ERROR_ZERO_RETURN:
  9235.               default:
  9236.                             break;
  9237.                         }
  9238.                     } else
  9239. #endif /* CK_SSL */
  9240.               send(csocket,(SENDARG2TYPE)obuf,3,0);
  9241.                     break;
  9242.           case DO:
  9243.           case DONT:
  9244.                     c = mygetc();
  9245.             obuf[0] = IAC;
  9246.             obuf[1] = WONT;
  9247.             obuf[2] = c;
  9248.             obuf[3] = NUL;
  9249. #ifdef CK_SSL
  9250.                     if (ssl_ftp_active_flag) {
  9251.                         int error, rc;
  9252.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  9253.                         error = SSL_get_error(ssl_ftp_con,rc);
  9254.                         switch (error) {
  9255.               case SSL_ERROR_NONE:
  9256.                             break;
  9257.               case SSL_ERROR_WANT_WRITE:
  9258.               case SSL_ERROR_WANT_READ:
  9259.                             return(0);
  9260.               case SSL_ERROR_SYSCALL:
  9261.                             if (rc == 0) { /* EOF */
  9262.                                 break;
  9263.                             } else {
  9264. #ifdef NT
  9265.                                 int gle = GetLastError();
  9266. #endif /* NT */
  9267.                                 break;
  9268.                             }
  9269.               case SSL_ERROR_WANT_X509_LOOKUP:
  9270.               case SSL_ERROR_SSL:
  9271.               case SSL_ERROR_ZERO_RETURN:
  9272.               default:
  9273.                             break;
  9274.                         }
  9275.                     } else
  9276. #endif /* CK_SSL */
  9277.               send(csocket,(SENDARG2TYPE)obuf,3,0);
  9278.                     break;
  9279.           default:
  9280.                     break;
  9281.                 }
  9282.                 continue;
  9283.             }
  9284.             dig++;
  9285.             if (c == EOF) {
  9286.                 if (expecteof) {
  9287.                     signal(SIGINT,oldintr);
  9288.                     ftpcode = 221;
  9289.             debug(F101,"ftp getreply EOF","",ftpcode);
  9290.                     return(0);
  9291.                 }
  9292.                 lostpeer();
  9293.                 if (vbm) {
  9294.                     printf(
  9295. "421 Service not available, connection closed by server\n");
  9296.                     fflush(stdout);
  9297.                 }
  9298.                 ftpcode = 421;
  9299.         debug(F101,"ftp getreply EOF","",ftpcode);
  9300.                 return(4);
  9301.             }
  9302.             if (n == 0)
  9303.           n = c;
  9304.             if (auth_type &&
  9305. #ifdef CK_SSL
  9306.         !ssl_ftp_active_flag &&
  9307. #endif /* CK_SSL */
  9308.         !ibuf[0] && (n == '6' || continuation)) {
  9309.                 if (c != '\r' && dig > 4)
  9310.           obuf[i++] = c;
  9311.             } else {
  9312.                 if (auth_type &&
  9313. #ifdef CK_SSL
  9314.             !ssl_ftp_active_flag &&
  9315. #endif /* CK_SSL */
  9316.             !ibuf[0] && dig == 1 && vbm)
  9317.           printf("Unauthenticated reply received from server:\n");
  9318.                 if (reply_parse) {
  9319.             *reply_ptr++ = c;
  9320.             *reply_ptr = NUL;
  9321.         }
  9322.         if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  9323.             ftp_cmdlin < 2) {
  9324.             if ((c != '\r') &&
  9325.             (ftp_deb || ((vbm || (!auth && n == '5')) && 
  9326.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0 
  9327.                         ))))) 
  9328.                     {
  9329. #ifdef FTP_PROXY
  9330.             if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  9331.               printf("%s:",ftp_host);
  9332. #endif /* FTP_PROXY */
  9333.  
  9334.             if (!quiet) {
  9335. #ifdef NOCSETS
  9336.                 printf("%c",c);
  9337. #else
  9338.                 if (xlate) {
  9339.                 xlatec(0,c,rcs,lcs);
  9340.                 } else {
  9341.                 printf("%c",c);
  9342.                 }
  9343. #endif /* NOCSETS */
  9344.             }
  9345.                     }
  9346.         }
  9347.             }
  9348.             if (auth_type &&
  9349. #ifdef CK_SSL
  9350.         !ssl_ftp_active_flag &&
  9351. #endif /* CK_SSL */
  9352.         !ibuf[0] && n != '6')
  9353.           continue;
  9354.             if (dig < 4 && isdigit(c))
  9355.           ftpcode = ftpcode * 10 + (c - '0');
  9356.             if (!pflag && ftpcode == 227)
  9357.           pflag = 1;
  9358.             if (dig > 4 && pflag == 1 && isdigit(c))
  9359.           pflag = 2;
  9360.             if (pflag == 2) {
  9361.                 if (c != '\r' && c != ')')
  9362.           *pt++ = c;
  9363.                 else {
  9364.                     *pt = '\0';
  9365.                     pflag = 3;
  9366.                 }
  9367.             }
  9368.             if (dig == 4 && c == '-' && n != '6') {
  9369.                 if (continuation)
  9370.           ftpcode = 0;
  9371.                 continuation++;
  9372.             }
  9373.             if (cp < &ftp_reply_str[sizeof(ftp_reply_str) - 1]) {
  9374.         *cp++ = c;
  9375.         *cp = NUL;
  9376.         }
  9377.         }
  9378.     if (deblog ||
  9379. #ifdef COMMENT
  9380. /*
  9381.   Sometimes we need to print the server reply.  printlines is nonzero for any
  9382.   command where the results are sent back on the control connection rather
  9383.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  9384.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  9385.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  9386.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  9387.   lines from UNIX.  Further experimentation needed with other servers.  Of
  9388.   course RFC959 is mute as to the format of the server reply.
  9389.  
  9390.   'printlines' is also true for PWD and BYE.
  9391. */
  9392.      (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  9393. #else
  9394. /* No, we can't be that clever -- it breaks other things like RPWD... */
  9395.         (printlines &&
  9396.          (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  9397. #endif /* COMMENT */
  9398.         ) {
  9399.         char * q = cp;
  9400.         char *r = ftp_reply_str;
  9401.         *q-- = NUL;            /* NUL-terminate */
  9402.         while (*q < '!' && q > r)    /* Strip CR, etc */
  9403.           *q-- = NUL;
  9404.         if (!ftp_deb && printlines) { /* If printing */
  9405.         if (ftpcode != 0)    /* strip ftpcode if any */
  9406.           r += 4;
  9407. #ifdef NOCSETS
  9408.         printf("%s\n",r);    /* and print */
  9409. #else
  9410.         if (!xlate) {
  9411.             printf("%s\n",r);
  9412.         } else {
  9413.             xgnbp = r;
  9414.             while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  9415.             if (xpnbyte(c0,TC_UCS2,lcs,xprintc) < 0) {
  9416.                 return(-1);
  9417.             }
  9418.             }
  9419.             printf("\n");
  9420.         }
  9421. #endif /* NOCSETS */
  9422.         }
  9423.         debug(F111,"ftp reply",ftp_reply_str,ftpcode);
  9424.     }
  9425.     if (auth_type &&
  9426. #ifdef CK_SSL
  9427.         !ssl_ftp_active_flag &&
  9428. #endif /* CK_SSL */
  9429.         !ibuf[0] && n != '6')
  9430.       return(getreply(expecteof,lcs,rcs,vbm,auth));
  9431.         ibuf[0] = obuf[i] = '\0';
  9432.         if (ftpcode && n == '6')
  9433.       if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  9434.           printf("Unknown reply: %d %s\n", ftpcode, obuf);
  9435.           n = '5';
  9436.       } else safe = (ftpcode == 631);
  9437.         if (obuf[0]            /* if there is a string to decode */
  9438. #ifdef CK_SSL
  9439.         && !ssl_ftp_active_flag    /* and not SSL/TLS */
  9440. #endif /* CK_SSL */
  9441.         ) {
  9442.             if (!auth_type) {
  9443.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  9444.                 n = '5';
  9445.             }
  9446. #ifndef CK_ENCRYPTION
  9447.             else if (ftpcode == 632) {
  9448.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  9449.                 n = '5';
  9450.             }
  9451. #endif /* CK_ENCRYPTION */
  9452. #ifdef NOCONFIDENTIAL
  9453.             else if (ftpcode == 633) {
  9454.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  9455.                 n = '5';
  9456.             }
  9457. #endif /* NOCONFIDENTIAL */
  9458.             else {
  9459.                 int len = FTP_BUFSIZ;
  9460.                 if ((kerror = radix_encode((CHAR *)obuf,
  9461.                        (CHAR *)ibuf,
  9462.                        0,
  9463.                        &len,
  9464.                        RADIX_DECODE))
  9465.             ) {
  9466.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  9467.                ftpcode, radix_error(kerror), obuf);
  9468.                     n = '5';
  9469.                 }
  9470. #ifdef FTP_SRP
  9471.                 else if (strcmp(auth_type, "SRP") == 0) {
  9472.                     int outlen;
  9473.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  9474.                     (CHAR *) ibuf, len);
  9475.                     if (outlen < 0) {
  9476.                         printf("Warning: %d reply %s!\n",
  9477.                    ftpcode, safe ? "modified" : "garbled");
  9478.                         n = '5';
  9479.                     } else {
  9480.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  9481.                         if (ftp_deb)
  9482.               printf("%c:", safe ? 'S' : 'P');
  9483.                         continue;
  9484.                     }
  9485.                 }
  9486. #endif /* FTP_SRP */
  9487. #ifdef FTP_KRB4
  9488.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  9489.                     if (safe) {
  9490.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  9491. #ifdef KRB524
  9492.                          ftp_cred.session,
  9493. #else /* KRB524 */
  9494.                          &ftp_cred.session,
  9495. #endif /* KRB524 */
  9496.                          &hisctladdr,
  9497.                          &myctladdr,
  9498.                          &ftp_msg_data
  9499.                          );
  9500.                     } else {
  9501.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  9502.                          ftp_sched,
  9503. #ifdef KRB524
  9504.                          ftp_cred.session,
  9505. #else /* KRB524 */
  9506.                          &ftp_cred.session,
  9507. #endif /* KRB524 */
  9508.                          &hisctladdr,
  9509.                          &myctladdr,
  9510.                          &ftp_msg_data
  9511.                          );
  9512.                     }
  9513.                     if (kerror != KSUCCESS) {
  9514.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  9515.                    safe ? "modified" : "garbled",
  9516.                    safe ? "safe" : "priv",
  9517.                    krb_get_err_text(kerror));
  9518.                         n = '5';
  9519.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  9520.                         kerror = KFAILURE;
  9521.                         n = '5';
  9522.                         printf("reply data too large for buffer\n");
  9523.                     } else {
  9524.                         if (ftp_deb)
  9525.               printf("%c:", safe ? 'S' : 'P');
  9526.                         memcpy(ibuf,ftp_msg_data.app_data,
  9527.                    ftp_msg_data.app_length);
  9528.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  9529.                   FTP_BUFSIZ - ftp_msg_data.app_length);
  9530.                         continue;
  9531.                     }
  9532.                 }
  9533. #endif /* FTP_KRB4 */
  9534. #ifdef FTP_GSSAPI
  9535.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  9536.                     gss_buffer_desc xmit_buf, msg_buf;
  9537.                     OM_uint32 maj_stat, min_stat;
  9538.                     int conf_state;
  9539.                     xmit_buf.value = ibuf;
  9540.                     xmit_buf.length = len;
  9541.                     /* decrypt/verify the message */
  9542.                     conf_state = safe;
  9543.                     maj_stat = gss_unseal(&min_stat, gcontext,
  9544.                       &xmit_buf, &msg_buf,
  9545.                       &conf_state, NULL);
  9546.                     if (maj_stat != GSS_S_COMPLETE) {
  9547.                         user_gss_error(maj_stat, min_stat,
  9548.                        "failed unsealing reply");
  9549.                         n = '5';
  9550.                     } else {
  9551.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  9552.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  9553.                   FTP_BUFSIZ-msg_buf.length);
  9554.                         gss_release_buffer(&min_stat,&msg_buf);
  9555.                         if (ftp_deb)
  9556.               printf("%c:", safe ? 'S' : 'P');
  9557.                         continue;
  9558.                     }
  9559.                 }
  9560. #endif /* FTP_GSSAPI */
  9561.                 /* Other auth types go here... */
  9562.             }
  9563.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  9564.            !quiet && (vbm || (!auth && n == '5'))) {
  9565. #ifdef NOCSETS
  9566.         printf("%c",c);
  9567. #else
  9568.         if (xlate) {
  9569.         xlatec(0,c,rcs,lcs);
  9570.         } else {
  9571.         printf("%c",c);
  9572.         }
  9573. #endif /* NOCSETS */
  9574.             fflush (stdout);
  9575.         }
  9576.         if (continuation && ftpcode != originalcode) {
  9577.             if (originalcode == 0)
  9578.           originalcode = ftpcode;
  9579.             continue;
  9580.         }
  9581.         *cp = '\0';
  9582.         if (n != '1')
  9583.       cpend = 0;
  9584.         signal(SIGINT,oldintr);
  9585.         if (ftpcode == 421 || originalcode == 421)
  9586.       lostpeer();
  9587.         if ((cancelfile != 0) &&
  9588. #ifndef ULTRIX3
  9589.         /* Ultrix 3.0 cc objects violently to this clause */
  9590.         (oldintr != cmdcancel) &&
  9591. #endif /* ULTRIX3 */
  9592.         (oldintr != SIG_IGN)) {
  9593.             if (oldintr)
  9594.           (*oldintr)(SIGINT);
  9595.     }
  9596.         if (reply_parse) {
  9597.             *reply_ptr = '\0';
  9598.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  9599.                 reply_parse = reply_ptr + strlen(reply_parse);
  9600.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  9601.           *reply_ptr = '\0';
  9602.             } else
  9603.           reply_parse = reply_ptr;
  9604.         }
  9605.     while (*cp < '!' && cp > ftp_reply_str)    /* Remove trailing junk */
  9606.       *cp-- = NUL;
  9607.     debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  9608.         return(n - '0');
  9609.     } /* for (;;) */
  9610. }
  9611.  
  9612. #ifdef BSDSELECT
  9613. static int
  9614. #ifdef CK_ANSIC
  9615. empty(fd_set * mask, int sec)
  9616. #else
  9617. empty(mask, sec) fd_set * mask; int sec;
  9618. #endif /* CK_ANSIC */
  9619. {
  9620.     struct timeval t;
  9621.     t.tv_sec = (long) sec;
  9622.     t.tv_usec = 0L;
  9623.     debug(F100,"ftp empty calling select...","",0);
  9624. #ifdef INTSELECT
  9625.     x = select(32, (int *)mask, NULL, NULL, &t);
  9626. #else
  9627.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  9628. #endif /* INTSELECT */
  9629.     debug(F101,"ftp empty select","",x);
  9630.     return(x);
  9631. }
  9632. #else /* BSDSELECT */
  9633. #ifdef IBMSELECT
  9634. static int
  9635. empty(mask, cnt, sec) int * mask, sec;
  9636.                       int   cnt;
  9637. {
  9638.     return(select(mask,cnt,0,0,sec*1000));
  9639. }
  9640. #endif /* IBMSELECT */
  9641. #endif /* BSDSELECT */
  9642.  
  9643. static sigtype
  9644. cancelsend(sig) int sig; {
  9645.     cancelgroup++;
  9646.     cancelfile = 0;
  9647.     printf(" Canceled...\n");
  9648.     debug(F101,"ftp cancelsend","",sig);
  9649.     fflush(stdout);
  9650. #ifndef OS2
  9651.     longjmp(sendcancel, 1);
  9652. #endif /* OS2 */
  9653. }
  9654.  
  9655. static
  9656. VOID
  9657. #ifdef CK_ANSIC
  9658. secure_error(char *fmt, ...)
  9659. #else
  9660. /* VARARGS1 */
  9661. secure_error(fmt, p1, p2, p3, p4, p5)
  9662.    char *fmt; int p1, p2, p3, p4, p5;
  9663. #endif /* CK_ANSIC */
  9664. {
  9665. #ifdef CK_ANSIC
  9666.     va_list ap;
  9667.  
  9668.     va_start(ap, fmt);
  9669.     vfprintf(stderr, fmt, ap);
  9670.     va_end(ap);
  9671. #else
  9672.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  9673. #endif
  9674.     fprintf(stderr, "\n");
  9675. }
  9676.  
  9677. /*
  9678.  * Internal form of settype; changes current type in use with server
  9679.  * without changing our notion of the type for data transfers.
  9680.  * Used to change to and from ascii for listings.
  9681.  */
  9682. static VOID
  9683. changetype(newtype, show) int newtype, show; {
  9684.     int comret;
  9685.     char * s;
  9686.  
  9687.     if (newtype == curtype)
  9688.       return;
  9689.     switch (newtype) {
  9690.       case FTT_ASC:
  9691.         s = "A";
  9692.         break;
  9693.       case FTT_BIN:
  9694.         s = "I";
  9695.         break;
  9696.       case FTT_TEN:
  9697.         s = "L 8";
  9698.         break;
  9699.       default:
  9700.         s = "I";
  9701.         break;
  9702.     }
  9703.     comret = ftpcmd("TYPE",s,-1,-1,show);
  9704.     if (comret == REPLY_COMPLETE)
  9705.       curtype = newtype;
  9706. }
  9707.  
  9708. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  9709.  
  9710. static int
  9711. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  9712.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  9713. {
  9714.     ULONG start = 0, stop;
  9715.     register int c, d = 0;
  9716.     int n, t, x, notafile = 0;
  9717.     sig_t oldintr, oldintp;
  9718.     long bytes = 0;
  9719.     char *lmode, buf[FTP_BUFSIZ], *bufp;
  9720.     sigtype cancelsend();
  9721.  
  9722.     if (!remote) remote = "";        /* Check args */
  9723.     if (!*remote) remote = local;
  9724.     if (!local) local = "";
  9725.     if (!*local) return(-1);
  9726.     if (!cmd) cmd = "";
  9727.     if (!*cmd) cmd = "STOR";
  9728.  
  9729.     debug(F111,"ftp sendrequest restart",local,restart);
  9730.  
  9731.     dout = -1;
  9732.  
  9733. #ifdef FTP_PROXY
  9734.     if (proxy) {
  9735.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  9736.         return(0);
  9737.     }
  9738. #endif /* FTP_PROXY */
  9739.  
  9740.     nout = 0;                /* Init output buffer count */
  9741.     bytes = 0;                /* File input byte count */
  9742.     changetype(ftp_typ,0);        /* Change type for this file */
  9743.  
  9744.     oldintr = NULL;            /* Set up interrupt handler */
  9745.     oldintp = NULL;
  9746.     lmode = "wb";
  9747.  
  9748.     /* Replace with calls to cc_execute() */
  9749.     if (setjmp(sendcancel)) {
  9750.         while (cpend) {
  9751.             getreply(0,incs,outcs,ftp_vbm,0);
  9752.         }
  9753.         if (data >= 0) {
  9754. #ifdef CK_SSL
  9755.             if (ssl_ftp_data_active_flag) {
  9756.                 SSL_shutdown(ssl_ftp_data_con);
  9757.                 SSL_free(ssl_ftp_data_con);
  9758.                 ssl_ftp_data_active_flag = 0;
  9759.                 ssl_ftp_data_con = NULL;
  9760.             }
  9761. #endif /* CK_SSL */
  9762. #ifdef TCPIPLIB
  9763.             socket_close(data);
  9764. #else /* TCPIPLIB */
  9765. #ifdef USE_SHUTDOWN
  9766.             shutdown(data, 1+1);
  9767. #endif /* USE_SHUTDOWN */
  9768.             close(data);
  9769. #endif /* TCPIPLIB */
  9770.             data = -1;
  9771.         globaldin = -1;
  9772.         }
  9773.         if (oldintr)
  9774.             signal(SIGINT,oldintr);
  9775. #ifdef SIGPIPE
  9776.         if (oldintp)
  9777.             signal(SIGPIPE,oldintp);
  9778. #endif /* SIGPIPE */
  9779.         ftpcode = -1;
  9780.         return(-1);
  9781.     }
  9782.     oldintr = signal(SIGINT, cancelsend);
  9783.  
  9784. #ifdef PIPESEND                /* Use Kermit API for file i/o... */
  9785.     if (sndfilter) {
  9786.     char * p = NULL, * q;
  9787. #ifndef NOSPL
  9788.     n = CKMAXPATH;
  9789.     if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  9790.         q = p;
  9791.         debug(F110,"sendrequest pipesend filter",sndfilter,0);
  9792.         zzstring(sndfilter,&p,&n);
  9793.         debug(F111,"sendrequest pipename",q,n);
  9794.         if (n <= 0) {
  9795.         printf("?Sorry, send filter + filename too long, %d max.\n",
  9796.                CKMAXPATH
  9797.                );
  9798.         free(q);
  9799.         return(-1);
  9800.         }
  9801.         ckstrncpy(filnam,q,CKMAXPATH+1);
  9802.         free(q);
  9803.         local = filnam;
  9804.     }
  9805. #endif /* NOSPL */
  9806.     }
  9807. #endif /* PIPESEND */
  9808.  
  9809.     notafile = sndarray || pipesend;
  9810.  
  9811. #ifdef PIPESEND
  9812.     if (sndfilter)            /* If sending thru a filter */
  9813.       pipesend = 1;            /* set this for open and i/o */
  9814. #endif /* PIPESEND */
  9815.  
  9816.     if (openi(local) == 0)        /* Try to open the input file */
  9817.       return(-1);
  9818.  
  9819.     if (initconn()) {
  9820. #ifndef NOHTTP
  9821.     int y = -1;
  9822.     debug(F101,"ftp send_request","tcp_http_proxy",tcp_http_proxy);
  9823.  
  9824.        /*  If the connection failed and we are using an HTTP Proxy
  9825.     *  and the reason for the failure was an authentication
  9826.     *  error, then we need to give the user to ability to
  9827.     *  enter a username and password, just like a browser.
  9828.     *
  9829.     *  I tried to do all of this within the netopen() call
  9830.     *  but it is much too much work.
  9831.     */
  9832.     while (y != 0 && tcp_http_proxy != NULL ) {
  9833.  
  9834.         if (tcp_http_proxy_errno == 401 ||
  9835.          tcp_http_proxy_errno == 407 ) 
  9836.         {
  9837.         char uid[UIDBUFLEN];
  9838.         char pwd[256];
  9839.  
  9840.         readtext("Proxy Userid: ",uid,UIDBUFLEN);
  9841.         if (uid[0]) {
  9842.             char * proxy_user, * proxy_pwd;
  9843.  
  9844.             readpass("Proxy Password: ",pwd,256);
  9845.  
  9846.             proxy_user = tcp_http_proxy_user;
  9847.             proxy_pwd  = tcp_http_proxy_pwd;
  9848.  
  9849.             tcp_http_proxy_user = uid;
  9850.             tcp_http_proxy_pwd = pwd;
  9851.  
  9852.             y = initconn();
  9853.  
  9854.             debug(F101,"ftp send_request()","y",y);
  9855.             memset(pwd,0,sizeof(pwd));
  9856.             tcp_http_proxy_user = proxy_user;
  9857.             tcp_http_proxy_pwd = proxy_pwd;
  9858.         } else 
  9859.             break;
  9860.         } else
  9861.         break;
  9862.     }
  9863.  
  9864.     if ( y != 0 ) {
  9865. #endif /* NOHTTP */
  9866.         signal(SIGINT, oldintr);
  9867. #ifdef SIGPIPE
  9868.         if (oldintp)
  9869.           signal(SIGPIPE, oldintp);
  9870. #endif /* SIGPIPE */
  9871.         ftpcode = -1;
  9872.         zclose(ZIFILE);
  9873.         return(-1);
  9874. #ifndef NOHTTP 
  9875.     }
  9876. #endif /* NOHTTP */
  9877.     }
  9878.  
  9879.     /* Replace with calls to cc_execute() */
  9880.     if (setjmp(sendcancel))
  9881.       goto cancel;
  9882.  
  9883. #ifdef FTP_RESTART
  9884.     if (restart && ((curtype == FTT_BIN) || (alike > 0))) {
  9885.     char * p;
  9886.     changetype(FTT_BIN,0);        /* Change to binary */
  9887.  
  9888.     /* Ask for remote file's size */
  9889.     x = ftpcmd("SIZE",remote,incs,outcs,ftp_vbm);
  9890.  
  9891.     if (x == REPLY_COMPLETE) {    /* Have reply */
  9892.         p = &ftp_reply_str[4];    /* Parse it */
  9893.         while (isdigit(*p)) {
  9894.         sendstart = sendstart * 10 + (int)(*p - '0');
  9895.         p++;
  9896.         }
  9897.         if (*p && *p != CR) {     /* Bad number */
  9898.         debug(F110,"ftp sendrequest bad size",ftp_reply_str,0);
  9899.         sendstart = 0L;
  9900.         } else if (sendstart > fsize) { /* Remote file bigger than local */
  9901.         debug(F110,"ftp sendrequest big size",ckltoa(fsize),sendstart);
  9902.         sendstart = 0L;
  9903.         }
  9904.         debug(F111,"ftp sendrequest size",remote,sendstart);
  9905.         if (chkmodtime(local,remote,0) == 2) { /* Local is newer */
  9906.         debug(F110,"ftp sendrequest date mismatch",ftp_reply_str,0);
  9907.         sendstart = 0L;        /* Send the whole file */
  9908.         }
  9909.     }
  9910.     changetype(ftp_typ,0);        /* Change back to appropriate type */
  9911.     if (sendstart > 0L) {        /* Still restarting? */
  9912.         if (sendstart == fsize) {    /* Same size - no need to send */
  9913.         debug(F111,"ftp sendrequest /restart SKIP",fsize,sendstart);
  9914.         zclose(ZIFILE);
  9915.         return(SKP_RES);
  9916.         }
  9917.         errno = 0;            /* Restart needed, seek to the spot */
  9918.         if (zfseek((long)sendstart) < 0) {
  9919.         debug(F111,"ftp sendrequest zfseek fails",local,sendstart);
  9920.         fprintf(stderr, "FSEEK: %s: %s\n", local, ck_errstr());
  9921.         sendstart = 0;
  9922.         zclose(ZIFILE);
  9923.         return(-1);
  9924.         }
  9925. #ifdef COMMENT
  9926.         debug(F111,"ftp sendrequest zfseek ok",local,sendstart);
  9927.         x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  9928.         if (x != REPLY_CONTINUE) {
  9929.         sendstart = 0;
  9930.         zclose(ZIFILE);
  9931.         return(-1);
  9932.         } else {
  9933.         cmd = "STOR";
  9934.         }
  9935. #else
  9936.         sendmode = SM_RESEND;
  9937.         cmd = "APPE";
  9938. #endif /* COMMENT */
  9939.         /* sendstart = 0L; */
  9940.     }
  9941.     }
  9942. #endif /* FTP_RESTART */
  9943.  
  9944.     x = ftpcmd(cmd,remote,incs,outcs,ftp_vbm);
  9945.     if (x != REPLY_PRELIM) {
  9946.     signal(SIGINT, oldintr);
  9947. #ifdef SIGPIPE
  9948.     if (oldintp)
  9949.       signal(SIGPIPE, oldintp);
  9950. #endif /* SIGPIPE */
  9951.     zclose(ZIFILE);
  9952. #ifdef PIPESEND
  9953.     if (sndfilter)
  9954.       pipesend = 0;
  9955. #endif /* PIPESEND */
  9956.     return(-1);
  9957.     }
  9958.     dout = dataconn(lmode);        /* Get data connection */
  9959.     if (dout == -1)
  9960.       goto cancel;
  9961.  
  9962.     /* Initialize per-file stats */
  9963.  
  9964.     ffc = 0L;                /* Character counter */
  9965.     cps = oldcps = 0L;            /* Thruput */
  9966.     start = gmstimer();            /* Start time (msecs) */
  9967. #ifdef GFTIMER
  9968.     rftimer();                /* reset f.p. timer */
  9969. #endif /* GFTIMER */
  9970.  
  9971. #ifdef SIGPIPE
  9972.     oldintp = signal(SIGPIPE, SIG_IGN);
  9973. #endif /* SIGPIPE */
  9974.     switch (curtype) {
  9975.       case FTT_BIN:            /* Binary mode */
  9976.       case FTT_TEN:
  9977.         errno = d = 0;
  9978.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ)) > 0 && !cancelfile) {
  9979.             bytes += n;
  9980.         ffc += n;
  9981.         debug(F111,"ftp sendrequest zxin",ckltoa(n),ffc);
  9982.         hexdump("ftp sendrequest zxin",buf,16);
  9983. #ifdef CK_SSL
  9984.             if (ssl_ftp_data_active_flag) {
  9985.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  9986.             if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  9987.               break;
  9988.             spackets++;
  9989.             pktnum++;
  9990.             if (fdispla != XYFD_B) {
  9991.             spktl = d;
  9992.             ftscreen(SCR_PT,'D',spackets,NULL);
  9993.             }
  9994.         }
  9995.             } else {
  9996. #endif /* CK_SSL */
  9997.         for (bufp = buf; n > 0; n -= d, bufp += d) {
  9998.             if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  9999.             || iscanceled())
  10000.               break;
  10001.             spackets++;
  10002.             pktnum++;
  10003.             if (fdispla != XYFD_B) {
  10004.             spktl = d;
  10005.             ftscreen(SCR_PT,'D',spackets,NULL);
  10006.             }
  10007.         }
  10008. #ifdef CK_SSL
  10009.         }
  10010. #endif /* CK_SSL */
  10011.             if (d <= 0)
  10012.           break;
  10013.         }
  10014.         if (n < 0)
  10015.       fprintf(stderr, "local: %s: %s\n", local, ck_errstr());
  10016.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  10017.             if (d == -1 && errno && errno != EPIPE)
  10018.           perror("netout");
  10019.             bytes = -1;
  10020.         }
  10021.         break;
  10022.  
  10023.       case FTT_ASC:            /* Text mode */
  10024. #ifndef NOCSETS
  10025.     if (xlate) {            /* With translation */
  10026.         initxlate(incs,outcs);
  10027.         while (!cancelfile) {
  10028.         c0 = xgnbyte(FC_UCS2,incs,NULL);
  10029.         if (c0 < 0)        /* EOF */
  10030.           break;
  10031.         c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  10032.         if (c1 < 0)         /* EOF */
  10033.           break;
  10034.         if (fileorder > 0) {    /* Little Endian */
  10035.             t = c1;        /* so swap them */
  10036.             c1 = c0;
  10037.             c0 = t;
  10038.         }
  10039.         if ((x = xpnbyte(c0,TC_UCS2,outcs,xxout)) < 0) break;
  10040.         if ((x = xpnbyte(c1,TC_UCS2,outcs,xxout)) < 0) break;
  10041.         }
  10042.     } else {
  10043. #endif /* NOCSETS */
  10044.         /* Text mode, no translation */
  10045.         while (((c = zminchar()) > -1) && !cancelfile) {
  10046.         ffc++;
  10047.         if (c == '\012') {
  10048.             if (zzout(dout,(CHAR)'\015') < 0)
  10049.               break;
  10050.             bytes++;
  10051.         }
  10052.         if (zzout(dout,(CHAR)c) < 0)
  10053.           break;
  10054.         bytes++;
  10055.         }
  10056.         d = 0;
  10057. #ifndef NOCSETS
  10058.     }
  10059. #endif /* NOCSETS */
  10060.     if (dout == -1 || (d = secure_flush(dout)) < 0) {
  10061.         if (d == -1 && errno && errno != EPIPE)
  10062.           perror("netout");
  10063.         bytes = -1;
  10064.     }
  10065.     break;
  10066.     }
  10067.     tfc += ffc;                /* Total file chars */
  10068.     stop = gmstimer();            /* Timing stats */
  10069. #ifdef GFTIMER
  10070.     fpfsecs = gftimer();
  10071. #endif /* GFTIMER */
  10072.     zclose(ZIFILE);            /* Close input file */
  10073. #ifdef PIPESEND
  10074.     if (sndfilter)            /* Undo this (it's per file) */
  10075.       pipesend = 0;
  10076. #endif /* PIPESEND */
  10077.  
  10078. #ifdef TCPIPLIB
  10079.     socket_close(dout);            /* Close data connection */
  10080. #else /* TCPIPLIB */
  10081. #ifdef USE_SHUTDOWN
  10082.     shutdown(dout, 1+1);
  10083. #endif /* USE_SHUTDOWN */
  10084.     close(dout);
  10085. #endif /* TCPIPLIB */
  10086.     getreply(0,incs,outcs,ftp_vbm,0);
  10087.     signal(SIGINT, oldintr);        /* Put back interrupts */
  10088. #ifdef SIGPIPE
  10089.     if (oldintp)
  10090.       signal(SIGPIPE, oldintp);
  10091. #endif /* SIGPIPE */
  10092.     if (cancelfile) {
  10093.     debug(F101,"ftp sendrequest canceled","",bytes);
  10094.     return(-1);
  10095.     } else {
  10096.     debug(F101,"ftp sendrequest ok","",bytes);
  10097.     return(0);
  10098.     }
  10099.  
  10100.   cancel:
  10101.     debug(F101,"ftp sendrequest canceled","",bytes);
  10102.     tfc += ffc;
  10103.     stop = gmstimer();
  10104. #ifdef GFTIMER
  10105.     fpfsecs = gftimer();
  10106. #endif /* GFTIMER */
  10107.     zclose(ZIFILE);
  10108. #ifdef PIPESEND
  10109.     if (sndfilter)
  10110.       pipesend = 0;
  10111. #endif /* PIPESEND */
  10112.     signal(SIGINT, oldintr);
  10113. #ifdef SIGPIPE
  10114.     if (oldintp)
  10115.       signal(SIGPIPE, oldintp);
  10116. #endif /* SIGPIPE */
  10117.     if (!cpend) {
  10118.         ftpcode = -1;
  10119.         return(-1);
  10120.     }
  10121.     if (data >= 0) {
  10122. #ifdef CK_SSL
  10123.         if (ssl_ftp_data_active_flag) {
  10124.             SSL_shutdown(ssl_ftp_data_con);
  10125.             SSL_free(ssl_ftp_data_con);
  10126.             ssl_ftp_data_active_flag = 0;
  10127.             ssl_ftp_data_con = NULL;
  10128.         }
  10129. #endif /* CK_SSL */
  10130. #ifdef TCPIPLIB
  10131.         socket_close(data);
  10132. #else /* TCPIPLIB */
  10133. #ifdef USE_SHUTDOWN
  10134.         shutdown(data, 1+1);
  10135. #endif /* USE_SHUTDOWN */
  10136.         close(data);
  10137. #endif /* TCPIPLIB */
  10138.         data = -1;
  10139.     globaldin = -1;
  10140.     }
  10141.     if (dout) {
  10142. #ifdef TCPIPLIB
  10143.         socket_close(dout);
  10144. #else /* TCPIPLIB */
  10145. #ifdef USE_SHUTDOWN
  10146.         shutdown(dout, 1+1);
  10147. #endif /* USE_SHUTDOWN */
  10148.         close(dout);
  10149. #endif /* TCPIPLIB */
  10150.     }
  10151.     getreply(0,incs,outcs,ftp_vbm,0);
  10152.     ftpcode = -1;
  10153.     return(-1);
  10154. }
  10155.  
  10156. static sigtype
  10157. cancelrecv(sig) int sig; {
  10158.     cancelfile = 0;
  10159.     printf(" Canceling...\n");
  10160.     fflush(stdout);
  10161.     if (fp_nml) {
  10162.     if (fp_nml != stdout)
  10163.       fclose(fp_nml);
  10164.     fp_nml = NULL;
  10165.     }
  10166. #ifndef OS2
  10167.     longjmp(recvcancel, 1);
  10168. #endif /* OS2 */
  10169. }
  10170.  
  10171. /* Argumentless front-end for secure_getc() */
  10172.  
  10173. static int
  10174. netgetc() {
  10175.     return(secure_getc(globaldin));
  10176. }
  10177.  
  10178. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  10179.  
  10180. /*
  10181.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  10182.     -1: interrupted by user.
  10183.     -2: error opening or writing output file (reason in errno).
  10184.     -3: failure to make data connection.
  10185.     -4: network read error (reason in errno).
  10186. */
  10187. static int
  10188. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  10189.         xlate, fcs, rcs)
  10190.     char *cmd, *local, *remote, *lmode, *pipename;
  10191.     int printnames, recover, xlate, fcs, rcs;
  10192. {
  10193.     int din = -1;
  10194.     sig_t oldintr, oldintp;
  10195.     int is_retr, tcrflag, bare_lfs = 0;
  10196.     int blksize = 0;
  10197.     long bytes = 0L;
  10198.     long localsize = 0L;
  10199.     register int c, d;
  10200.     ULONG start = 0L, stop;
  10201.     sigtype cancelrecv();
  10202. #ifdef NT
  10203.     struct _stat stbuf;
  10204. #else /* NT */
  10205.     struct stat stbuf;
  10206. #endif /* NT */
  10207.     char * p;
  10208.     static char * rcvbuf = NULL;
  10209.     static int rcvbufsiz = 0;
  10210.  
  10211. #ifdef DEBUG
  10212.     if (deblog) {
  10213.     debug(F111,"ftp recvrequest cmd",cmd,recover);
  10214.     debug(F110,"ftp recvrequest local ",local,0);
  10215.     debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  10216.     debug(F110,"ftp recvrequest pipename ",pipename,0);
  10217.     debug(F101,"ftp recvrequest xlate","",xlate);
  10218.     debug(F101,"ftp recvrequest fcs","",fcs);
  10219.     debug(F101,"ftp recvrequest rcs","",rcs);
  10220.     }
  10221. #endif /* DEBUG */
  10222.     if (remfile) {            /* See remcfm(), remtxt() */
  10223.     if (rempipe) {
  10224.         pipename = remdest;
  10225.     } else {
  10226.         local = remdest;
  10227.         if (remappd) lmode = "ab";
  10228.     }
  10229.     }
  10230.     out2screen = 0;
  10231.     if (!cmd) cmd = "";            /* Core dump prevention */
  10232.     if (!remote) remote = "";
  10233.     if (!lmode) lmode = "";
  10234.  
  10235.     if (pipename) {            /* No recovery for pipes. */
  10236.     recover = 0;
  10237.     if (!local)
  10238.       local = pipename;
  10239.     } else {
  10240.     if (!local)            /* Output to screen? */
  10241.       local = "-";
  10242.     out2screen = !strcmp(local,"-");
  10243.     }
  10244.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  10245.  
  10246.     if (out2screen)            /* No recovery to screen */
  10247.       recover = 0;
  10248.     if (!ftp_typ)            /* No recovery in text mode */
  10249.       recover = 0;
  10250.     is_retr = (strcmp(cmd, "RETR") == 0);
  10251.     if (!is_retr)            /* No recovery except for RETRieve */
  10252.       recover = 0;
  10253.  
  10254.     localsize = 0L;            /* Local file size */
  10255.     rs_len = 0L;            /* Recovery point */
  10256.  
  10257.     if (recover) {            /* Recovering... */
  10258.     if (stat(local, &stbuf) < 0) {    /* Can't stat local file */
  10259.         recover = 0;        /* So cancel recovery */
  10260.     } else {            /* Have local file info */
  10261.         localsize = stbuf.st_size;    /* Get size */
  10262.         if (fsize < localsize) {    /* Remote file smaller than local */
  10263.         recover = 0;        /* Recovery can't work */
  10264.         } else if (fsize == localsize) { /* Sizes are equal */
  10265.         debug(F111,"ftp recvrequest skipping",remote,localsize);
  10266.         return(1);
  10267.         }
  10268.         if (recover) {        /* Remote is bigger */
  10269.         x = chkmodtime(local,remote,0); /* It must also be older */
  10270.         debug(F111,"ftp getfile chkmodtime",remote,x);
  10271.         if (x < 2)
  10272.           recover = 0;        /* If not, get whole file */
  10273.         }
  10274.     }
  10275.     debug(F111,"ftp recvrequest recover",remote,recover);
  10276.     }
  10277.  
  10278. #ifdef FTP_PROXY
  10279.     if (proxy && is_retr)
  10280.       return(proxtrans(cmd, local ? local : remote, remote));
  10281. #endif /* FTP_PROXY */
  10282.  
  10283.     oldintr = NULL;
  10284.     oldintp = NULL;
  10285.     tcrflag = (feol != CR) && is_retr;
  10286.     ftpcode = 0;
  10287.  
  10288.     /* Replace with calls to cc_execute() */
  10289.     if (setjmp(recvcancel)) {
  10290.         while (cpend) {
  10291.             getreply(0,fcs,rcs,ftp_vbm,0);
  10292.         }
  10293.         if (data >= 0) {
  10294. #ifdef CK_SSL
  10295.             if (ssl_ftp_data_active_flag) {
  10296.                 SSL_shutdown(ssl_ftp_data_con);
  10297.                 SSL_free(ssl_ftp_data_con);
  10298.                 ssl_ftp_data_active_flag = 0;
  10299.                 ssl_ftp_data_con = NULL;
  10300.             }
  10301. #endif /* CK_SSL */
  10302. #ifdef TCPIPLIB
  10303.             socket_close(data);
  10304. #else /* TCPIPLIB */
  10305. #ifdef USE_SHUTDOWN
  10306.             shutdown(data, 1+1);
  10307. #endif /* USE_SHUTDOWN */
  10308.             close(data);
  10309. #endif /* TCPIPLIB */
  10310.             data = -1;
  10311.         globaldin = -1;
  10312.         }
  10313.         if (oldintr)
  10314.       signal(SIGINT, oldintr);
  10315.         ftpcode = -1;
  10316.         return(-1);
  10317.     }
  10318.     oldintr = signal(SIGINT, cancelrecv);
  10319.  
  10320.     if (!out2screen && !pipename) {
  10321.         if (zchko(local) < 0) {
  10322.         if ((!dpyactive || ftp_deb))
  10323.           fprintf(stderr,"Temporary file %s: %s\n", local, ck_errstr());
  10324.         signal(SIGINT, oldintr);
  10325.         ftpcode = -2;
  10326.         return(-1);
  10327.         }
  10328.     }
  10329.     changetype((!is_retr) ? FTT_ASC : ftp_typ, 0);
  10330.  
  10331.     if (initconn()) {            /* Initialize the data connection */
  10332.         signal(SIGINT, oldintr);
  10333.         ftpcode = -1;
  10334.         return(-3);
  10335.     }
  10336.  
  10337.     /* Replace with calls to cc_execute() */
  10338.     if (setjmp(recvcancel))
  10339.       goto cancel;
  10340.  
  10341.     if (recover) {            /* Initiate recovery */
  10342.     x = ftpcmd("REST",ckltoa(localsize),-1,-1,ftp_vbm);
  10343.     debug(F111,"ftp reply","REST",x);
  10344.     if (x == REPLY_CONTINUE) {
  10345.         lmode = "ab";
  10346.         rs_len = localsize;
  10347.     } else {
  10348.         recover = 0;
  10349.     }
  10350.     }
  10351.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  10352.  
  10353.     debug(F111,"ftp recvrequest recover E",remote,recover);
  10354.  
  10355.     /* Send the command and get reply */
  10356.     debug(F110,"ftp recvrequest cmd",cmd,0);
  10357.     debug(F110,"ftp recvrequest remote",remote,0);
  10358.  
  10359.     if (ftpcmd(cmd,remote,fcs,rcs,ftp_vbm) != REPLY_PRELIM) {
  10360.     signal(SIGINT, oldintr);    /* Bad reply, fail. */
  10361.     return(-1);            /* ftpcode is set by ftpcmd() */
  10362.     }
  10363.     din = dataconn("r");        /* Good reply, open data connection */
  10364.     globaldin = din;            /* Global copy of file descriptor */
  10365.     if (din == -1) {            /* Check for failure */
  10366.     ftpcode = -3;            /* Code for no data connection */
  10367.     goto cancel;
  10368.     }
  10369.     x = 1;                /* Output file open OK? */
  10370.     if (pipename) {            /* Command */
  10371.     x = zxcmd(ZOFILE,pipename);
  10372.     debug(F111,"ftp recvrequest zxcmd",pipename,x);
  10373.     } else if (!out2screen) {        /* File */
  10374.     struct filinfo xx;
  10375.     xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  10376.     xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  10377.     xx.dsp = !strcmp(lmode,"ab") ? XYFZ_A : XYFZ_N; /* Append or New */
  10378.     x = zopeno(ZOFILE,local,NULL,&xx);
  10379.     debug(F111,"ftp recvrequest zopeno",local,x);
  10380.     }
  10381.     if (x < 1) {            /* Failure to open output file */
  10382.     if ((!dpyactive || ftp_deb))
  10383.       fprintf(stderr, "local(2): %s: %s\n", local, ck_errstr());
  10384.     goto cancel;
  10385.     }
  10386.     blksize = FTP_BUFSIZ;        /* Allocate input buffer */
  10387.     if (rcvbufsiz < blksize) {        /* if necessary */
  10388.         if (rcvbuf) {
  10389.         free(rcvbuf);
  10390.         rcvbuf = NULL;
  10391.     }
  10392.     rcvbuf = (char *)malloc((unsigned)blksize);
  10393.         if (!rcvbuf) {
  10394.         ftpcode = -2;
  10395. #ifdef ENOMEM
  10396.         errno = ENOMEM;
  10397. #endif /* ENOMEM */
  10398.         if ((!dpyactive || ftp_deb))
  10399.           perror("malloc");
  10400.             rcvbufsiz = 0;
  10401.             goto cancel;
  10402.         }
  10403.         rcvbufsiz = blksize;
  10404.     }
  10405.     debug(F111,"ftp get rcvbufsiz",local,rcvbufsiz);
  10406.  
  10407.     ffc = 0L;                /* Character counter */
  10408.     cps = oldcps = 0L;            /* Thruput */
  10409.     start = gmstimer();            /* Start time (msecs) */
  10410. #ifdef GFTIMER
  10411.     rftimer();                /* Start time (float) */
  10412. #endif /* GFTIMER */
  10413.  
  10414.     debug(F111,"ftp get type",local,curtype);
  10415.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  10416.     switch (curtype) {
  10417.       case FTT_BIN:            /* Binary mode */
  10418.       case FTT_TEN:            /* TENEX mode */
  10419.         d = 0;
  10420.         while (1) {
  10421.         errno = 0;
  10422.         c = secure_read(din, rcvbuf, rcvbufsiz);
  10423.         if (cancelfile)
  10424.           goto cancel;
  10425.         if (c < 1)
  10426.           break;
  10427. #ifdef printf                /* (What if it isn't?) */
  10428.             if (out2screen && !pipename) {
  10429.                 int i;
  10430.                 for (i = 0; i < c; i++)
  10431.           printf("%c",rcvbuf[i]);
  10432.             } else
  10433. #endif /* printf */
  10434.           {
  10435.         register int i;
  10436.         i = 0;
  10437.         errno = 0;
  10438.         while (i < c) {
  10439.             if (zmchout(rcvbuf[i++]) < 0) {
  10440.             d = i;
  10441.             break;
  10442.             }
  10443.         }
  10444.         }
  10445.             bytes += c;
  10446.         ffc += c;
  10447.         }
  10448.         if (c < 0) {
  10449.         debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  10450.             if (c == -1 && errno != EPIPE)
  10451.           if ((!dpyactive || ftp_deb))
  10452.         perror("netin");
  10453.             bytes = -1;
  10454.         ftpcode = -4;
  10455.         }
  10456.         if (d < c) {
  10457.         ftpcode = -2;
  10458.         if ((!dpyactive || ftp_deb)) {
  10459.         char * p;
  10460.         p = local ? local : pipename;
  10461.         if (d < 0)
  10462.           fprintf(stderr, "local(3): %s: %s\n", local, ck_errstr());
  10463.         else
  10464.           fprintf(stderr, "%s: short write\n", local);
  10465.         }
  10466.         }
  10467.         break;
  10468.  
  10469.       case FTT_ASC:            /* Text mode */
  10470. #ifndef NOCSETS
  10471.     if (xlate) {
  10472.         int t;
  10473. #ifdef CK_ANSIC
  10474.         int (*fn)(char);
  10475. #else
  10476.         int (*fn)();
  10477. #endif /* CK_ANSIC */
  10478.         debug(F110,"ftp recvrequest (data)","initxlate",0);
  10479.         initxlate(rcs,fcs);        /* (From,To) */
  10480.         if (pipename) {
  10481.         fn = pipeout;
  10482.         debug(F110,"ftp recvrequest ASCII","pipeout",0);
  10483.         } else {
  10484.         fn = out2screen ? scrnout : putfil;
  10485.         debug(F110,"ftp recvrequest ASCII",
  10486.               out2screen ? "scrnout" : "putfil",0);
  10487.         }
  10488.         while (1) {
  10489.         c0 = xgnbyte(FC_UCS2,rcs,netgetc); /* Get byte from net */
  10490.         if (cancelfile)
  10491.           goto cancel;
  10492.         if (c0 < 0)
  10493.           break;
  10494.         c1 = xgnbyte(FC_UCS2,rcs,netgetc); /* Second byte from net */
  10495.         if (cancelfile)
  10496.           goto cancel;
  10497.         if (c1 < 0)
  10498.           break;
  10499.         if (fileorder > 0) {    /* Little Endian */
  10500.             t = c1;        /* so swap them */
  10501.             c1 = c0;
  10502.             c0 = t;
  10503.         }
  10504.         if ((x = xpnbyte(c0,TC_UCS2,fcs,fn)) < 0) break;
  10505.         if ((x = xpnbyte(c1,TC_UCS2,fcs,fn)) < 0) break;
  10506.         }
  10507.     } else {
  10508. #endif /* NOCSETS */
  10509.         while (1) {
  10510.         c = secure_getc(din);
  10511.         if (cancelfile)
  10512.           goto cancel;
  10513.         if (c < 0 || c == EOF)
  10514.           break;
  10515.         if (c == '\n')
  10516.           bare_lfs++;
  10517.         while (c == '\r') {
  10518.             bytes++;
  10519.             if ((c = secure_getc(din)) != '\n' || tcrflag) {
  10520.             if (cancelfile)
  10521.               goto cancel;
  10522.             if (c < 0 || c == EOF)
  10523.               goto break2;
  10524.             if (c == '\0') {
  10525.                 bytes++;
  10526.                 goto contin2;
  10527.             }
  10528.             }
  10529.         }
  10530.         if (c < 0)
  10531.           break;
  10532.         if (out2screen && !pipename)
  10533. #ifdef printf
  10534.           printf("%c",(char)c);
  10535. #else
  10536.           putchar((char)c);
  10537. #endif /* printf */
  10538.         else
  10539.           if ((d = zmchout(c)) < 0)
  10540.             break;
  10541.         bytes++;
  10542.         ffc++;
  10543.           contin2:
  10544.         ;
  10545.         }
  10546.       break2:
  10547.         if (bare_lfs && (!dpyactive || ftp_deb)) {
  10548.         printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  10549.                bare_lfs);
  10550.         printf("File might not have transferred correctly.\n");
  10551.         }
  10552.         if (din == -1) {
  10553.         bytes = -1;
  10554.         }
  10555.         if (c == -2)
  10556.           bytes = -1;
  10557.         break;
  10558. #ifndef NOCSETS
  10559.     }
  10560. #endif /* NOCSETS */
  10561.     }
  10562.     if (pipename || !out2screen)
  10563.       zclose(ZOFILE);
  10564.  
  10565.     /* If ftpcode < 0 fail and if keep == 0 delete the partial file */
  10566.  
  10567.     signal(SIGINT, oldintr);
  10568. #ifdef SIGPIPE
  10569.     if (oldintp)
  10570.       signal(SIGPIPE, oldintp);
  10571. #endif /* SIGPIPE */
  10572.     stop = gmstimer();
  10573. #ifdef GFTIMER
  10574.     fpfsecs = gftimer();
  10575. #endif /* GFTIMER */
  10576.     tfc += ffc;
  10577.  
  10578. #ifdef TCPIPLIB
  10579.     socket_close(din);
  10580. #else /* TCPIPLIB */
  10581. #ifdef USE_SHUTDOWN
  10582.     shutdown(din, 1+1);
  10583. #endif /* USE_SHUTDOWN */
  10584.     close(din);
  10585. #endif /* TCPIPLIB */
  10586.     getreply(0,fcs,rcs,ftp_vbm,0);
  10587.     return(ftpcode < 0 ? -1 : 0);
  10588.  
  10589.   cancel:
  10590.  
  10591. /* Cancel using RFC959 recommended IP,SYNC sequence  */
  10592.  
  10593.     debug(F100,"ftp recvrequest CANCEL","",0);
  10594.     stop = gmstimer();
  10595. #ifdef GFTIMER
  10596.     fpfsecs = gftimer();
  10597. #endif /* GFTIMER */
  10598. #ifdef SIGPIPE
  10599.     if (oldintp)
  10600.       signal(SIGPIPE, oldintr);
  10601. #endif /* SIGPIPE */
  10602.     signal(SIGINT, SIG_IGN);
  10603.     if (!cpend) {
  10604.         ftpcode = -1;
  10605.         signal(SIGINT, oldintr);
  10606.         return(-1);
  10607.     }
  10608.     cancel_remote(din);
  10609.     if (ftpcode > -1)
  10610.       ftpcode = -1;
  10611.     if (data >= 0) {
  10612. #ifdef CK_SSL
  10613.         if (ssl_ftp_data_active_flag) {
  10614.             SSL_shutdown(ssl_ftp_data_con);
  10615.             SSL_free(ssl_ftp_data_con);
  10616.             ssl_ftp_data_active_flag = 0;
  10617.             ssl_ftp_data_con = NULL;
  10618.         }
  10619. #endif /* CK_SSL */
  10620. #ifdef TCPIPLIB
  10621.         socket_close(data);
  10622. #else /* TCPIPLIB */
  10623. #ifdef USE_SHUTDOWN
  10624.         shutdown(data, 1+1);
  10625. #endif /* USE_SHUTDOWN */
  10626.         close(data);
  10627. #endif /* TCPIPLIB */
  10628.         data = -1;
  10629.     globaldin = -1;
  10630.     }
  10631.     if (!out2screen)
  10632.       zclose(ZOFILE);
  10633.  
  10634.     if (din) {
  10635. #ifdef TCPIPLIB
  10636.         socket_close(din);
  10637. #else /* TCPIPLIB */
  10638. #ifdef USE_SHUTDOWN
  10639.         shutdown(din, 1+1);
  10640. #endif /* USE_SHUTDOWN */
  10641.         close(din);
  10642. #endif /* TCPIPLIB */
  10643.     }
  10644.     signal(SIGINT, oldintr);
  10645.     return(-1);
  10646. }
  10647.  
  10648. /*
  10649.  * Need to start a listen on the data channel before we send the command,
  10650.  * otherwise the server's connect may fail.
  10651.  */
  10652. static int
  10653. initconn() {
  10654.     register char *p, *a;
  10655.     int result, tmpno = 0;
  10656.     int on = 1;
  10657.     GSOCKNAME_T len;
  10658.  
  10659. #ifndef NO_PASSIVE_MODE
  10660.     int a1,a2,a3,a4,p1,p2;
  10661.  
  10662.     if (passivemode) {
  10663.         data = socket(AF_INET, SOCK_STREAM, 0);
  10664.     globaldin = data;
  10665.         if (data < 0) {
  10666.             perror("ftp: socket");
  10667.             return(-1);
  10668.         }
  10669.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  10670.             printf("Passive mode refused\n");
  10671.             passivemode = 0;
  10672.             return(initconn());
  10673.         }
  10674. /*
  10675.   Now we have a string of comma-separated one-byte unsigned integer values,
  10676.   The first four are the an IP address.  The fifth is the MSB of the port
  10677.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  10678. */
  10679.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  10680.             printf("Passive mode address scan failure\n");
  10681.             return(-1);
  10682.         };
  10683. #ifndef NOHTTP
  10684.         if (tcp_http_proxy) {
  10685. #ifdef OS2
  10686.             char * agent = "Kermit 95";    /* Default user agent */
  10687. #else
  10688.             char * agent = "C-Kermit";
  10689. #endif /* OS2 */
  10690.             register struct hostent *hp = 0;
  10691.             struct servent *destsp;
  10692.             char host[512], *p, *q;
  10693. #ifdef IP_TOS
  10694. #ifdef IPTOS_THROUGHPUT
  10695.             int tos;
  10696. #endif /* IPTOS_THROUGHPUT */
  10697. #endif /* IP_TOS */
  10698.             int s;
  10699. #ifdef DEBUG
  10700.             extern int debtim;
  10701.             int xdebtim;
  10702.             xdebtim = debtim;
  10703.             debtim = 1;
  10704. #endif /* DEBUG */
  10705.  
  10706.             ckmakxmsg(proxyhost,sizeof(proxyhost),ckuitoa(a1),".",ckuitoa(a2),
  10707.               ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  10708.               NULL,NULL,NULL
  10709.               );
  10710.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  10711.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  10712.           *q = *p;
  10713.             *q = '\0';
  10714.  
  10715.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  10716.             if (hisctladdr.sin_addr.s_addr != -1) {
  10717.                 debug(F110,"initconn A",host,0);
  10718.                 hisctladdr.sin_family = AF_INET;
  10719.             } else {
  10720.                 debug(F110,"initconn B",host,0);
  10721.                 hp = gethostbyname(host);
  10722. #ifdef HADDRLIST
  10723.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  10724. #endif /* HADDRLIST */
  10725.                 if (hp == NULL) {
  10726.                     fprintf(stderr, "ftp: %s: ", host);
  10727.                     herror();
  10728.                     ftpcode = -1;
  10729. #ifdef DEBUG
  10730.                     debtim = xdebtim;
  10731. #endif /* DEBUG */
  10732.                     return(0);
  10733.                 }
  10734.                 hisctladdr.sin_family = hp->h_addrtype;
  10735. #ifdef HADDRLIST
  10736.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  10737.                sizeof(hisctladdr.sin_addr));
  10738. #else /* HADDRLIST */
  10739.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  10740.                sizeof(hisctladdr.sin_addr));
  10741. #endif /* HADDRLIST */
  10742.             }
  10743.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  10744.             debug(F101,"initconn socket","",data);
  10745.             if (data < 0) {
  10746.                 perror("ftp: socket");
  10747.                 ftpcode = -1;
  10748. #ifdef DEBUG
  10749.                 debtim = xdebtim;
  10750. #endif /* DEBUG */
  10751.                 return(0);
  10752.             }
  10753.             if (*p == ':')
  10754.           p++;
  10755.             else
  10756.           p = "http";
  10757.  
  10758.             destsp = getservbyname(p,"tcp");
  10759.             if (destsp)
  10760.           hisctladdr.sin_port = destsp->s_port;
  10761.             else if (p)
  10762.           hisctladdr.sin_port = htons(atoi(p));
  10763.             else
  10764.           hisctladdr.sin_port = htons(80);
  10765.             errno = 0;
  10766. #ifdef HADDRLIST
  10767.             debug(F100,"initconn HADDRLIST","",0);
  10768.             while
  10769. #else
  10770.         debug(F100,"initconn no HADDRLIST","",0);
  10771.             if
  10772. #endif /* HADDRLIST */
  10773.               (connect(data, (struct sockaddr *)&hisctladdr,
  10774.                        sizeof (hisctladdr)) < 0) {
  10775.           debug(F101,"initconn connect failed","",errno);
  10776. #ifdef HADDRLIST
  10777.           if (hp && hp->h_addr_list[1]) {
  10778.               int oerrno = errno;
  10779.  
  10780.               fprintf(stderr,
  10781.                   "ftp: connect to address %s: ",
  10782.                   inet_ntoa(hisctladdr.sin_addr)
  10783.                   );
  10784.               errno = oerrno;
  10785.               perror((char *)0);
  10786.               hp->h_addr_list++;
  10787.               memcpy((char *)&hisctladdr.sin_addr,
  10788.                  hp->h_addr_list[0],
  10789.                  sizeof(hisctladdr.sin_addr));
  10790.               fprintf(stdout, "Trying %s...\n",
  10791.                   inet_ntoa(hisctladdr.sin_addr));
  10792. #ifdef TCPIPLIB
  10793.               socket_close(data);
  10794. #else /* TCPIPLIB */
  10795.               close(data);
  10796. #endif /* TCPIPLIB */
  10797.               data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  10798.               if (data < 0) {
  10799.               perror("ftp: socket");
  10800.               ftpcode = -1;
  10801. #ifdef DEBUG
  10802.               debtim = xdebtim;
  10803. #endif /* DEBUG */
  10804.               return(0);
  10805.               }
  10806.               continue;
  10807.           }
  10808. #endif /* HADDRLIST */
  10809.           perror("ftp: connect");
  10810.           ftpcode = -1;
  10811.           goto bad;
  10812.           }
  10813.             if (http_connect(data,agent,NULL,
  10814.                  tcp_http_proxy_user,
  10815.                  tcp_http_proxy_pwd,
  10816.                  0,
  10817.                  proxyhost
  10818.                  ) < 0) {
  10819. #ifdef TCPIPLIB
  10820.                 socket_close(data);
  10821. #else /* TCPIPLIB */
  10822.                 close(data);
  10823. #endif /* TCPIPLIB */
  10824.                 perror("ftp: connect");
  10825.                 ftpcode = -1;
  10826.                 goto bad;
  10827.             }
  10828.         } else
  10829. #endif /* NOHTTP */
  10830.         {
  10831.             data_addr.sin_family = AF_INET;
  10832.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  10833.             data_addr.sin_port = htons((p1<<8)|p2);
  10834.  
  10835.             if (connect(data,
  10836.             (struct sockaddr *)&data_addr,
  10837.             sizeof(data_addr)) < 0
  10838.         ) {
  10839.                 perror("ftp: connect");
  10840.                 return(-1);
  10841.             }
  10842.         }
  10843.         debug(F100,"initconn connect ok","",0);
  10844. #ifdef IP_TOS
  10845. #ifdef IPTOS_THROUGHPUT
  10846.         on = IPTOS_THROUGHPUT;
  10847.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  10848.       perror("ftp: setsockopt TOS (ignored)");
  10849. #endif /* IPTOS_THROUGHPUT */
  10850. #endif /* IP_TOS */
  10851.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  10852.         return(0);
  10853.     }
  10854. #endif /* NO_PASSIVE_MODE */
  10855.  
  10856.   noport:
  10857.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  10858.     if (sendport)
  10859.       data_addr.sin_port = 0;    /* let system pick one */
  10860.     if (data != -1) {
  10861. #ifdef TCPIPLIB
  10862.         socket_close(data);
  10863. #else /* TCPIPLIB */
  10864. #ifdef USE_SHUTDOWN
  10865.         shutdown(data, 1+1);
  10866. #endif /* USE_SHUTDOWN */
  10867.         close(data);
  10868. #endif /* TCPIPLIB */
  10869.     }
  10870.     data = socket(AF_INET, SOCK_STREAM, 0);
  10871.     globaldin = data;
  10872.     if (data < 0) {
  10873.         perror("ftp: socket");
  10874.         if (tmpno)
  10875.       sendport = 1;
  10876.         return(-1);
  10877.     }
  10878.     if (!sendport) {
  10879.     if (setsockopt(data,
  10880.                SOL_SOCKET,
  10881.                SO_REUSEADDR,
  10882.                (char *)&on,
  10883.                sizeof (on)
  10884.                ) < 0
  10885.         ) {
  10886.         perror("ftp: setsockopt (reuse address)");
  10887.         goto bad;
  10888.     }
  10889.     }
  10890.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  10891.         perror("ftp: bind");
  10892.         goto bad;
  10893.     }
  10894.     len = sizeof (data_addr);
  10895.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  10896.         perror("ftp: getsockname");
  10897.         goto bad;
  10898.     }
  10899.     if (listen(data, 1) < 0) {
  10900.         perror("ftp: listen");
  10901.         goto bad;
  10902.     }
  10903.     if (sendport) {
  10904.         a = (char *)&data_addr.sin_addr;
  10905.         p = (char *)&data_addr.sin_port;
  10906.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  10907.           UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  10908.           UC(p[0]),",", UC(p[1]));
  10909.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  10910.         if (result == REPLY_ERROR && sendport) {
  10911.             sendport = 0;
  10912.             tmpno = 1;
  10913.             goto noport;
  10914.         }
  10915.         return(result != REPLY_COMPLETE);
  10916.     }
  10917.     if (tmpno)
  10918.       sendport = 1;
  10919. #ifdef IP_TOS
  10920. #ifdef IPTOS_THROUGHPUT
  10921.     on = IPTOS_THROUGHPUT;
  10922.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  10923.       perror("ftp: setsockopt TOS (ignored)");
  10924. #endif
  10925. #endif
  10926.     return(0);
  10927.   bad:
  10928. #ifdef TCPIPLIB
  10929.     socket_close(data);
  10930. #else /* TCPIPLIB */
  10931. #ifdef USE_SHUTDOWN
  10932.     shutdown(data, 1+1);
  10933. #endif /* USE_SHUTDOWN */
  10934.     close(data);
  10935. #endif /* TCPIPLIB */
  10936.     data = -1;
  10937.     globaldin = data;
  10938.     if (tmpno)
  10939.       sendport = 1;
  10940.     return(-1);
  10941. }
  10942.  
  10943. #ifdef CK_SSL
  10944. static int
  10945. ssl_dataconn() {
  10946.     if (ssl_ftp_data_con!=NULL) {    /* Do SSL */
  10947.         SSL_free(ssl_ftp_data_con);
  10948.         ssl_ftp_data_con=NULL;
  10949.     }
  10950.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  10951.  
  10952.     SSL_set_fd(ssl_ftp_data_con,data);
  10953.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  10954.  
  10955.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  10956.  
  10957.     if (ssl_debug_flag) {
  10958.         fprintf(stderr,"=>START SSL connect on DATA\n");
  10959.         fflush(stderr);
  10960.     }
  10961.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  10962.         static char errbuf[1024];
  10963.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  10964.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  10965.         fprintf(stderr,"%s\n", errbuf);
  10966.         fflush(stderr);
  10967. #ifdef TCPIPLIB
  10968.         socket_close(data);
  10969. #else /* TCPIPLIB */
  10970. #ifdef USE_SHUTDOWN
  10971.         shutdown(data, 1+1);
  10972. #endif /* USE_SHUTDOWN */
  10973.         close(data);
  10974. #endif /* TCPIPLIB */
  10975.         data = -1;
  10976.         globaldin = data;
  10977.         return(-1);
  10978.     } else {
  10979.         ssl_ftp_data_active_flag=1;
  10980.  
  10981.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  10982.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  10983.  
  10984.             if (!subject) {
  10985.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  10986.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  10987.  
  10988.                     auth_finished(AUTH_REJECT);
  10989.                     ssl_ftp_data_active_flag = 0;
  10990. #ifdef TCPIPLIB
  10991.                     socket_close(data);
  10992. #else /* TCPIPLIB */
  10993. #ifdef USE_SHUTDOWN
  10994.                     shutdown(data, 1+1);
  10995. #endif /* USE_SHUTDOWN */
  10996.                     close(data);
  10997. #endif /* TCPIPLIB */
  10998.                     data = -1;
  10999.                     globaldin = data;
  11000.                     return(-1);
  11001.                 } else {
  11002.                     char * prmpt =
  11003.              "Warning: Server didn't provide a certificate, continue? (Y/N) ";
  11004.  
  11005.                     if (!out2screen && displa && fdispla) {
  11006.                         ftscreen(SCR_TC,0,0L,"Display canceled");
  11007.                         /* fdispla = XYFD_B; */
  11008.                     }
  11009.  
  11010.                     if (!getyesno(prmpt,0)) {
  11011.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  11012.                         ssl_ftp_data_active_flag = 0;
  11013. #ifdef TCPIPLIB
  11014.                         socket_close(data);
  11015. #else /* TCPIPLIB */
  11016. #ifdef USE_SHUTDOWN
  11017.                         shutdown(data, 1+1);
  11018. #endif /* USE_SHUTDOWN */
  11019.                         close(data);
  11020. #endif /* TCPIPLIB */
  11021.                         data = -1;
  11022.                         globaldin = data;
  11023.                         return(-1);
  11024.                     }
  11025.                 }
  11026.             } else {
  11027.                 if (!out2screen && displa && fdispla == XYFD_C) {
  11028.                     ftscreen(SCR_TC,0,0L,"Display canceled");
  11029.                     /* fdispla = XYFD_B; */
  11030.                 }
  11031.  
  11032.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_host)) {
  11033.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  11034.                     ssl_ftp_data_active_flag = 0;
  11035. #ifdef TCPIPLIB
  11036.                     socket_close(data);
  11037. #else /* TCPIPLIB */
  11038. #ifdef USE_SHUTDOWN
  11039.                     shutdown(data, 1+1);
  11040. #endif /* USE_SHUTDOWN */
  11041.                     close(data);
  11042. #endif /* TCPIPLIB */
  11043.                     data = -1;
  11044.                     globaldin = data;
  11045.                     return(-1);
  11046.                 }
  11047.             }
  11048.         }
  11049.         debug(F110,"dataconn","[SSL - OK]",0);
  11050. #ifdef COMMENT
  11051.         /* This messes up the full screen file transfer display */
  11052.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  11053. #endif /* COMMENT */
  11054.     }
  11055.     if (ssl_debug_flag) {
  11056.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  11057.         fflush(stderr);
  11058.     }
  11059.     return(data);
  11060. }
  11061. #endif /* CK_SSL */
  11062.  
  11063. static int
  11064. dataconn(lmode) char *lmode; {
  11065.     int s;
  11066. #ifdef IP_TOS
  11067.     int tos;
  11068. #endif /* IP_TOS */
  11069. #ifdef UCX50
  11070.     static u_int fromlen;
  11071. #else
  11072.     static SOCKOPT_T fromlen;
  11073. #endif /* UCX50 */
  11074.  
  11075.     fromlen = sizeof(hisdataaddr);
  11076.  
  11077. #ifndef NO_PASSIVE_MODE
  11078.     if (passivemode) {
  11079. #ifdef CK_SSL
  11080.         ssl_ftp_data_active_flag=0;
  11081.         if (ssl_ftp_active_flag &&
  11082.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  11083.       return(ssl_dataconn());
  11084. #endif /* CK_SSL */
  11085.     return(data);
  11086.     }
  11087. #endif /* NO_PASSIVE_MODE */
  11088.  
  11089.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  11090.     if (s < 0) {
  11091.         perror("ftp: accept");
  11092. #ifdef TCPIPLIB
  11093.         socket_close(data);
  11094. #else /* TCPIPLIB */
  11095. #ifdef USE_SHUTDOWN
  11096.         shutdown(data, 1+1);
  11097. #endif /* USE_SHUTDOWN */
  11098.         close(data);
  11099. #endif /* TCPIPLIB */
  11100.         data = -1;
  11101.     globaldin = data;
  11102.         return(-1);
  11103.     }
  11104. #ifdef TCPIPLIB
  11105.     socket_close(data);
  11106. #else /* TCPIPLIB */
  11107. #ifdef USE_SHUTDOWN
  11108.     shutdown(data, 1+1);
  11109. #endif /* USE_SHUTDOWN */
  11110.     close(data);
  11111. #endif /* TCPIPLIB */
  11112.     data = s;
  11113.     globaldin = data;
  11114. #ifdef IP_TOS
  11115. #ifdef IPTOS_THROUGHPUT
  11116.     tos = IPTOS_THROUGHPUT;
  11117.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  11118.       perror("ftp: setsockopt TOS (ignored)");
  11119. #endif /* IPTOS_THROUGHPUT */
  11120. #endif /* IP_TOS */
  11121.  
  11122. #ifdef CK_SSL
  11123.     ssl_ftp_data_active_flag=0;
  11124.     if (ssl_ftp_active_flag &&
  11125.     (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  11126.       return(ssl_dataconn());
  11127. #endif /* CK_SSL */
  11128.     return(data);
  11129. }
  11130.  
  11131. #ifdef FTP_PROXY
  11132. static sigtype
  11133. pscancel(sig) int sig; {
  11134.     cancelfile++;
  11135. }
  11136.  
  11137. static VOID
  11138. pswitch(flag) int flag; {
  11139.     extern int proxy;
  11140.     sig_t oldintr;
  11141.     static struct comvars {
  11142.         int connect;
  11143.         char name[MAXHOSTNAMELEN];
  11144.         struct sockaddr_in mctl;
  11145.         struct sockaddr_in hctl;
  11146.         FILE *in;
  11147.         FILE *out;
  11148.         int tpe;
  11149.         int curtpe;
  11150.         int cpnd;
  11151.         int sunqe;
  11152.         int runqe;
  11153.         int mcse;
  11154.         int ntflg;
  11155.         char nti[17];
  11156.         char nto[17];
  11157.         int mapflg;
  11158.         char mi[CKMAXPATH];
  11159.         char mo[CKMAXPATH];
  11160.         char *authtype;
  11161.         int clvl;
  11162.         int dlvl;
  11163. #ifdef FTP_KRB4
  11164.         des_cblock session;
  11165.         des_key_schedule ftp_sched;
  11166. #endif /* FTP_KRB4 */
  11167. #ifdef FTP_GSSAPI
  11168.         gss_ctx_id_t gcontext;
  11169. #endif /* GSSAPI */
  11170.     } proxstruct, tmpstruct;
  11171.     struct comvars *ip, *op;
  11172.  
  11173.     cancelfile = 0;
  11174.     oldintr = signal(SIGINT, pscancel);
  11175.     if (flag) {
  11176.         if (proxy)
  11177.       return;
  11178.         ip = &tmpstruct;
  11179.         op = &proxstruct;
  11180.         proxy++;
  11181.     } else {
  11182.         if (!proxy)
  11183.       return;
  11184.         ip = &proxstruct;
  11185.         op = &tmpstruct;
  11186.         proxy = 0;
  11187.     }
  11188.     ip->connect = connected;
  11189.     connected = op->connect;
  11190.     if (ftp_host) {
  11191.         strncpy(ip->name, ftp_host, sizeof(ip->name) - 1);
  11192.         ip->name[strlen(ip->name)] = '\0';
  11193.     } else
  11194.       ip->name[0] = 0;
  11195.     ftp_host = op->name;
  11196.     ip->hctl = hisctladdr;
  11197.     hisctladdr = op->hctl;
  11198.     ip->mctl = myctladdr;
  11199.     myctladdr = op->mctl;
  11200.     ip->in = csocket;
  11201.     csocket = op->in;
  11202.     ip->out = csocket;
  11203.     csocket = op->out;
  11204.     ip->tpe = ftp_typ;
  11205.     ftp_typ = op->tpe;
  11206.     ip->curtpe = curtype;
  11207.     curtype = op->curtpe;
  11208.     ip->cpnd = cpend;
  11209.     cpend = op->cpnd;
  11210.     ip->sunqe = ftp_usn;
  11211.     ftp_usn = op->sunqe;
  11212.     ip->mcse = mcase;
  11213.     mcase = op->mcse;
  11214.     ip->ntflg = ntflag;
  11215.     ntflag = op->ntflg;
  11216.     strncpy(ip->nti, ntin, 16);
  11217.     (ip->nti)[strlen(ip->nti)] = '\0';
  11218.     strcpy(ntin, op->nti);
  11219.     strncpy(ip->nto, ntout, 16);
  11220.     (ip->nto)[strlen(ip->nto)] = '\0';
  11221.     strcpy(ntout, op->nto);
  11222.     ip->mapflg = mapflag;
  11223.     mapflag = op->mapflg;
  11224.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  11225.     (ip->mi)[strlen(ip->mi)] = '\0';
  11226.     strcpy(mapin, op->mi);
  11227.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  11228.     (ip->mo)[strlen(ip->mo)] = '\0';
  11229.     strcpy(mapout, op->mo);
  11230.     ip->authtype = auth_type;
  11231.     auth_type = op->authtype;
  11232.     ip->clvl = ftp_cpl;
  11233.     ftp_cpl = op->clvl;
  11234.     ip->dlvl = ftp_dpl;
  11235.     ftp_dpl = op->dlvl;
  11236.     if (!ftp_cpl)
  11237.       ftp_cpl = FPL_CLR;
  11238.     if (!ftp_dpl)
  11239.       ftp_dpl = FPL_CLR;
  11240. #ifdef FTP_KRB4
  11241.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  11242.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  11243.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  11244.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  11245. #endif /* FTP_KRB4 */
  11246. #ifdef FTP_GSSAPI
  11247.     ip->gcontext = gcontext;
  11248.     gcontext = op->gcontext;
  11249. #endif /* GSSAPI */
  11250.     signal(SIGINT, oldintr);
  11251.     if (cancelfile) {
  11252.         cancelfile = 0;
  11253.     debug(F101,"pswitch cancelfile B","",cancelfile);
  11254.         (*oldintr)(SIGINT);
  11255.     }
  11256. }
  11257.  
  11258. static sigtype
  11259. cancelpt(sig) int sig; {
  11260.     printf("\n");
  11261.     fflush(stdout);
  11262.     ptabflg++;
  11263.     cancelfile = 0;
  11264. #ifndef OS2
  11265.     longjmp(ptcancel, 1);
  11266. #endif /* OS2 */
  11267. }
  11268.  
  11269. void
  11270. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  11271.     sig_t oldintr;
  11272.     int secndflag = 0, prox_type, nfnd;
  11273.     char *cmd2;
  11274. #ifdef BSDSELECT
  11275.     fd_set mask;
  11276. #endif /* BSDSELECT */
  11277.     sigtype cancelpt();
  11278.  
  11279.     if (strcmp(cmd, "RETR"))
  11280.       cmd2 = "RETR";
  11281.     else
  11282.       cmd2 = unique ? "STOU" : "STOR";
  11283.     if ((prox_type = type) == 0) {
  11284.         if (servertype == SYS_UNIX && unix_proxy)
  11285.       prox_type = FTT_BIN;
  11286.         else
  11287.       prox_type = FTT_ASC;
  11288.     }
  11289.     if (curtype != prox_type)
  11290.       changetype(prox_type, 1);
  11291.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  11292.         printf("Proxy server does not support third party transfers.\n");
  11293.         return;
  11294.     }
  11295.     pswitch(0);
  11296.     if (!connected) {
  11297.         printf("No primary connection\n");
  11298.         pswitch(1);
  11299.         ftpcode = -1;
  11300.         return;
  11301.     }
  11302.     if (curtype != prox_type)
  11303.       changetype(prox_type, 1);
  11304.  
  11305.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  11306.         pswitch(1);
  11307.         return;
  11308.     }
  11309.  
  11310.     /* Replace with calls to cc_execute() */
  11311.     if (setjmp(ptcancel))
  11312.       goto cancel;
  11313.     oldintr = signal(SIGINT, cancelpt);
  11314.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  11315.         signal(SIGINT, oldintr);
  11316.         pswitch(1);
  11317.         return;
  11318.     }
  11319.     sleep(2000);
  11320.     pswitch(1);
  11321.     secndflag++;
  11322.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  11323.       goto cancel;
  11324.     ptflag++;
  11325.     getreply(0,-1,-1,ftp_vbm,0);
  11326.     pswitch(0);
  11327.     getreply(0,-1,-1,ftp_vbm,0);
  11328.     signal(SIGINT, oldintr);
  11329.     pswitch(1);
  11330.     ptflag = 0;
  11331.     return;
  11332.  
  11333.   cancel:
  11334.     signal(SIGINT, SIG_IGN);
  11335.     ptflag = 0;
  11336.     if (strcmp(cmd, "RETR") && !proxy)
  11337.       pswitch(1);
  11338.     else if (!strcmp(cmd, "RETR") && proxy)
  11339.       pswitch(0);
  11340.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  11341.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  11342.             pswitch(0);
  11343.             if (cpend)
  11344.           cancel_remote(0);
  11345.         }
  11346.         pswitch(1);
  11347.         if (ptabflg)
  11348.       ftpcode = -1;
  11349.         signal(SIGINT, oldintr);
  11350.         return;
  11351.     }
  11352.     if (cpend)
  11353.       cancel_remote(0);
  11354.     pswitch(!proxy);
  11355.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  11356.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  11357.             pswitch(0);
  11358.             if (cpend)
  11359.           cancel_remote(0);
  11360.             pswitch(1);
  11361.             if (ptabflg)
  11362.           ftpcode = -1;
  11363.             signal(SIGINT, oldintr);
  11364.             return;
  11365.         }
  11366.     }
  11367.     if (cpend)
  11368.       cancel_remote(0);
  11369.     pswitch(!proxy);
  11370.     if (cpend) {
  11371. #ifdef BSDSELECT
  11372.         FD_ZERO(&mask);
  11373.         FD_SET(csocket, &mask);
  11374.         if ((nfnd = empty(&mask, 10)) <= 0) {
  11375.             if (nfnd < 0) {
  11376.                 perror("cancel");
  11377.             }
  11378.             if (ptabflg)
  11379.           ftpcode = -1;
  11380.             lostpeer();
  11381.         }
  11382. #else /* BSDSELECT */
  11383. #ifdef IBMSELECT
  11384.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  11385.             if (nfnd < 0) {
  11386.                 perror("cancel");
  11387.             }
  11388.             if (ptabflg)
  11389.           ftpcode = -1;
  11390.             lostpeer();
  11391.         }
  11392. #endif /* IBMSELECT */
  11393. #endif /* BSDSELECT */
  11394.         getreply(0,-1,-1,ftp_vbm,0);
  11395.         getreply(0,-1,-1,ftp_vbm,0);
  11396.     }
  11397.     if (proxy)
  11398.       pswitch(0);
  11399.     pswitch(1);
  11400.     if (ptabflg)
  11401.       ftpcode = -1;
  11402.     signal(SIGINT, oldintr);
  11403. }
  11404. #endif /* FTP_PROXY */
  11405.  
  11406. #ifdef FTP_SECURITY
  11407. #ifdef FTP_GSSAPI
  11408. #ifdef OS2
  11409. /** exported constants defined in gssapi_krb5{,_nx}.h **/
  11410.  
  11411. /* these are bogus, but will compile */
  11412.  
  11413. /*
  11414.  * The OID of the draft krb5 mechanism, assigned by IETF, is:
  11415.  *      iso(1) org(3) dod(5) internet(1) security(5)
  11416.  *      kerberosv5(2) = 1.3.5.1.5.2
  11417.  * The OID of the krb5_name type is:
  11418.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11419.  *      krb5(2) krb5_name(1) = 1.2.840.113554.1.2.2.1
  11420.  * The OID of the krb5_principal type is:
  11421.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11422.  *      krb5(2) krb5_principal(2) = 1.2.840.113554.1.2.2.2
  11423.  * The OID of the proposed standard krb5 mechanism is:
  11424.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11425.  *      krb5(2) = 1.2.840.113554.1.2.2
  11426.  * The OID of the proposed standard krb5 v2 mechanism is:
  11427.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11428.  *      krb5v2(3) = 1.2.840.113554.1.2.3
  11429.  *
  11430.  */
  11431.  
  11432. /*
  11433.  * Encoding rules: The first two values are encoded in one byte as 40
  11434.  * * value1 + value2.  Subsequent values are encoded base 128, most
  11435.  * significant digit first, with the high bit (\200) set on all octets
  11436.  * except the last in each value's encoding.
  11437.  */
  11438.  
  11439. CONST gss_OID_desc
  11440. ck_krb5_gss_oid_array[] = {
  11441.    /* this is the official, rfc-specified OID */
  11442.    {9, "\052\206\110\206\367\022\001\002\002"},
  11443.    /* this is the unofficial, wrong OID */
  11444.    {5, "\053\005\001\005\002"},
  11445.    /* this is the v2 assigned OID */
  11446.    {9, "\052\206\110\206\367\022\001\002\003"},
  11447.    /* these two are name type OID's */
  11448.    {10, "\052\206\110\206\367\022\001\002\002\001"},
  11449.    {10, "\052\206\110\206\367\022\001\002\002\002"},
  11450.    { 0, 0 }
  11451. };
  11452.  
  11453. CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
  11454. CONST gss_OID_desc * CONST gss_mech_krb5_old = ck_krb5_gss_oid_array+1;
  11455. CONST gss_OID_desc * CONST gss_mech_krb5_v2 = ck_krb5_gss_oid_array+2;
  11456. CONST gss_OID_desc * CONST gss_nt_krb5_name = ck_krb5_gss_oid_array+3;
  11457. CONST gss_OID_desc * CONST gss_nt_krb5_principal = ck_krb5_gss_oid_array+4;
  11458.  
  11459. /*
  11460.  * See krb5/gssapi_krb5.c for a description of the algorithm for
  11461.  * encoding an object identifier.
  11462.  */
  11463.  
  11464. /*
  11465.  * The OID of user_name is:
  11466.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11467.  *      generic(1) user_name(1) = 1.2.840.113554.1.2.1.1
  11468.  * machine_uid_name:
  11469.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11470.  *      generic(1) machine_uid_name(2) = 1.2.840.113554.1.2.1.2
  11471.  * string_uid_name:
  11472.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11473.  *      generic(1) string_uid_name(3) = 1.2.840.113554.1.2.1.3
  11474.  * service_name:
  11475.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  11476.  *      generic(1) service_name(4) = 1.2.840.113554.1.2.1.4
  11477.  * exported_name:
  11478.  *      1(iso), 3(org), 6(dod), 1(internet), 5(security), 6(nametypes),
  11479.  *          4(gss-api-exported-name)
  11480.  * host_based_service_name (v2):
  11481.  *      iso (1) org (3), dod (6), internet (1), security (5), nametypes(6),
  11482.  *      gss-host-based-services(2)
  11483.  */
  11484.  
  11485. static gss_OID_desc oids[] = {
  11486.    {10, "\052\206\110\206\367\022\001\002\001\001"},
  11487.    {10, "\052\206\110\206\367\022\001\002\001\002"},
  11488.    {10, "\052\206\110\206\367\022\001\002\001\003"},
  11489.    {10, "\052\206\110\206\367\022\001\002\001\004"},
  11490.    { 6, "\053\006\001\005\006\004"},
  11491.    { 6, "\053\006\001\005\006\002"},
  11492. };
  11493.  
  11494. static gss_OID ck_gss_nt_user_name = oids+0;
  11495. static gss_OID ck_gss_nt_machine_uid_name = oids+1;
  11496. static gss_OID ck_gss_nt_string_uid_name = oids+2;
  11497. static gss_OID ck_gss_nt_service_name = oids+3;
  11498. static gss_OID ck_gss_nt_exported_name = oids+4;
  11499. static gss_OID ck_gss_nt_service_name_v2 = oids+5;
  11500. #endif /* OS2 */
  11501.  
  11502. struct {
  11503.     CONST gss_OID_desc * CONST * mech_type;
  11504.     char *service_name;
  11505. } gss_trials[] = {
  11506.     { &gss_mech_krb5, "ftp" },
  11507.     { &gss_mech_krb5, "host" },
  11508. };
  11509.  
  11510. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  11511. #endif /* FTP_GSSAPI */
  11512.  
  11513. static int
  11514. ftp_auth() {
  11515.     extern int setsafe();
  11516.     int j = 0, n;
  11517. #ifdef FTP_KRB4
  11518.     char *service, inst[INST_SZ];
  11519.     ULONG cksum;
  11520.     ULONG checksum = (ULONG) getpid();
  11521.     CHAR out_buf[FTP_BUFSIZ];
  11522.     int i;
  11523. #else /* FTP_KRB4 */
  11524. #ifdef FTP_GSSAPI
  11525.     CHAR out_buf[FTP_BUFSIZ];
  11526.     int i;
  11527. #endif /* FTP_GSSAPI */
  11528. #endif /* FTP_KRB4 */
  11529.  
  11530.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  11531.         return(0);
  11532.  
  11533.     if (auth_type)
  11534.       return(1);            /* auth already succeeded */
  11535.  
  11536.     /* Try each auth type as specified by the end user */
  11537.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  11538. #ifdef FTP_GSSAPI
  11539.     if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  11540.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  11541.         if (n == REPLY_CONTINUE) {
  11542.         OM_uint32 maj_stat, min_stat;
  11543.         gss_name_t target_name;
  11544.         gss_buffer_desc send_tok, recv_tok, *token_ptr;
  11545.         char stbuf[FTP_BUFSIZ];
  11546.         int comcode, trial;
  11547.         struct gss_channel_bindings_struct chan;
  11548.                 char * realm = NULL;
  11549.                 char tgt[256];
  11550.  
  11551.         chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  11552.         chan.initiator_address.length = 4;
  11553.         chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  11554.         chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  11555.         chan.acceptor_address.length = 4;
  11556.         chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  11557.         chan.application_data.length = 0;
  11558.         chan.application_data.value = 0;
  11559.  
  11560.         if (!quiet)
  11561.           printf("GSSAPI accepted as authentication type\n");
  11562.  
  11563.                 realm = ck_krb5_realmofhost(ftp_host);
  11564.                 if (realm) {
  11565.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11566.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  11567.                     if ( krb5_autoget &&
  11568.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11569.                             (ck_krb5_is_tgt_valid() > 0)) )
  11570.                         ck_krb5_autoget_TGT(realm);
  11571.         }
  11572.  
  11573.         /* Blob from gss-client */
  11574.         for (trial = 0; trial < n_gss_trials; trial++) {
  11575.             /* ftp@hostname first, the host@hostname */
  11576.             /* the V5 GSSAPI binding canonicalizes this for us... */
  11577.             ckmakmsg(stbuf,FTP_BUFSIZ,
  11578.                  gss_trials[trial].service_name,
  11579.                  "@",
  11580.                  ftp_host,
  11581.                  NULL
  11582.                  );
  11583.             if (ftp_deb)
  11584.               fprintf(stderr,
  11585.                   "Authenticating to <%s>...\n", stbuf);
  11586.             send_tok.value = stbuf;
  11587.             send_tok.length = strlen(stbuf);
  11588.             maj_stat = gss_import_name(&min_stat, &send_tok,
  11589.                            gss_nt_service_name,
  11590.                            &target_name
  11591.                            );
  11592.             if (maj_stat != GSS_S_COMPLETE) {
  11593.             user_gss_error(maj_stat, min_stat, "parsing name");
  11594.             secure_error("name parsed <%s>\n", stbuf);
  11595.             continue;
  11596.             }
  11597.             token_ptr = GSS_C_NO_BUFFER;
  11598.             gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  11599.  
  11600.             do {
  11601.             if (ftp_deb)
  11602.               fprintf(stderr, "calling gss_init_sec_context\n");
  11603.             maj_stat =
  11604.               gss_init_sec_context(&min_stat,
  11605.                            GSS_C_NO_CREDENTIAL,
  11606.                            &gcontext,
  11607.                            target_name,
  11608.                            (gss_OID) *
  11609.                              gss_trials[trial].mech_type,
  11610.                            GSS_C_MUTUAL_FLAG |
  11611.                            GSS_C_REPLAY_FLAG |
  11612.                            (ftp_cfw ?
  11613.                         GSS_C_DELEG_FLAG : 0),
  11614.                            0,
  11615.                                                 /* channel bindings */
  11616.                                                 (krb5_d_no_addresses ?
  11617.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  11618.                                                   &chan),
  11619.                                                 token_ptr,
  11620.                            NULL,    /* ignore mech type */
  11621.                            &send_tok,
  11622.                            NULL,    /* ignore ret_flags */
  11623.                            NULL
  11624.                            );    /* ignore time_rec */
  11625.  
  11626.             if (maj_stat != GSS_S_COMPLETE &&
  11627.                 maj_stat != GSS_S_CONTINUE_NEEDED) {
  11628.                 if (trial == n_gss_trials-1)
  11629.                   user_gss_error(maj_stat,
  11630.                          min_stat,
  11631.                          "initializing context"
  11632.                          );
  11633.                 gss_release_name(&min_stat, &target_name);
  11634.                 /* maybe we missed on the service name */
  11635.                 goto outer_loop;
  11636.             }
  11637.             if (send_tok.length != 0) {
  11638.                 int len;
  11639.                 reply_parse = "ADAT="; /* for ftpcmd() later */
  11640.                 len = sizeof(out_buf);
  11641.                 kerror =
  11642.                   radix_encode(send_tok.value,
  11643.                        out_buf,
  11644.                        send_tok.length,
  11645.                        &len,
  11646.                        RADIX_ENCODE
  11647.                        );
  11648.                 if (kerror)  {
  11649.                 fprintf(stderr,
  11650.                     "Base 64 encoding failed: %s\n",
  11651.                     radix_error(kerror)
  11652.                     );
  11653.                 goto gss_complete_loop;
  11654.                 }
  11655.                 comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  11656.                 if (comcode != REPLY_COMPLETE
  11657.                 /* && comcode != 3 (335)*/
  11658.                 ) {
  11659.                 if (trial == n_gss_trials-1) {
  11660.                     fprintf(stderr, "GSSAPI ADAT failed\n");
  11661.                     /* force out of loop */
  11662.                     maj_stat = GSS_S_FAILURE;
  11663.                 }
  11664.                 /*
  11665.                   Backoff to the v1 gssapi is still possible.
  11666.                   Send a new AUTH command.  If that fails,
  11667.                   terminate the loop.
  11668.                 */
  11669.                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  11670.                     != REPLY_CONTINUE) {
  11671.                     fprintf(stderr,
  11672.                     "GSSAPI ADAT failed, AUTH restart failed\n");
  11673.                     /* force out of loop */
  11674.                     maj_stat = GSS_S_FAILURE;
  11675.                 }
  11676.                 goto outer_loop;
  11677.                 }
  11678.                 if (!reply_parse) {
  11679.                 fprintf(stderr,
  11680.                       "No authentication data received from server\n");
  11681.                 if (maj_stat == GSS_S_COMPLETE) {
  11682.                     fprintf(stderr,
  11683.                         "...but no more was needed\n");
  11684.                     goto gss_complete_loop;
  11685.                 } else {
  11686.                     user_gss_error(maj_stat,
  11687.                            min_stat,
  11688.                            "no reply, huh?"
  11689.                            );
  11690.                     goto gss_complete_loop;
  11691.                 }
  11692.                 }
  11693.                 len = sizeof(out_buf);
  11694.                 kerror = radix_encode(reply_parse,out_buf,i,&len,
  11695.                           RADIX_DECODE);
  11696.                 if (kerror) {
  11697.                 fprintf(stderr,
  11698.                     "Base 64 decoding failed: %s\n",
  11699.                     radix_error(kerror));
  11700.                 goto gss_complete_loop;
  11701.                 }
  11702.  
  11703.                 /* everything worked */
  11704.                 token_ptr = &recv_tok;
  11705.                 recv_tok.value = out_buf;
  11706.                 recv_tok.length = len;
  11707.                 continue;
  11708.  
  11709.                 /* get out of loop clean */
  11710.               gss_complete_loop:
  11711.                 trial = n_gss_trials-1;
  11712.                 gss_release_buffer(&min_stat, &send_tok);
  11713.                 gss_release_name(&min_stat, &target_name);
  11714.                 goto outer_loop;
  11715.             }
  11716.             } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  11717.  
  11718.           outer_loop:
  11719.             if (maj_stat == GSS_S_COMPLETE)
  11720.               break;
  11721.         }
  11722.         if (maj_stat == GSS_S_COMPLETE) {
  11723.             printf("GSSAPI authentication succeeded\n");
  11724.             reply_parse = NULL;
  11725.             auth_type = "GSSAPI";
  11726.             return(1);
  11727.         } else {
  11728.             fprintf(stderr, "GSSAPI authentication failed\n");
  11729.             reply_parse = NULL;
  11730.         }
  11731.         } else if (ftp_deb)
  11732.           fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  11733.     }
  11734. #endif /* FTP_GSSAPI */
  11735. #ifdef FTP_SRP
  11736.     if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  11737.         if (srp_ftp_auth(ftp_host,NULL,NULL))
  11738.           return(1);
  11739.     }
  11740. #endif /* FTP_SRP */
  11741. #ifdef FTP_KRB4
  11742.     if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  11743.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  11744.         if (n == REPLY_CONTINUE) {
  11745.         char tgt[4*REALM_SZ+1];
  11746.         int rc;
  11747.  
  11748.         if (!quiet)
  11749.           printf("KERBEROS_V4 accepted as authentication type\n");
  11750.         ckstrncpy(inst, (char *) krb_get_phost(ftp_host),INST_SZ);
  11751.         ckstrncpy(ftp_realm,
  11752.               (char *)krb_realmofhost(ftp_host),
  11753.               REALM_SZ
  11754.               );
  11755.  
  11756.         ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  11757.         rc = ck_krb4_tkt_isvalid(tgt);
  11758.  
  11759.         if (rc <= 0 && krb4_autoget)
  11760.           ck_krb4_autoget_TGT(ftp_realm);
  11761.  
  11762.         service = "ftp";
  11763.         kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  11764.         if (kerror == KDC_PR_UNKNOWN) {
  11765.             service = "rcmd";
  11766.             kerror = krb_mk_req(&ftp_tkt,
  11767.                     service,
  11768.                     inst,
  11769.                     ftp_realm,
  11770.                     checksum
  11771.                     );
  11772.         }
  11773.         if (kerror)
  11774.           fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  11775.               krb_get_err_text(kerror));
  11776.         if (!kerror) {
  11777.             kerror = krb_get_cred(service, inst, ftp_realm,
  11778.                       (CREDENTIALS *)&ftp_cred);
  11779.             if (kerror)
  11780.               fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  11781.                   krb_get_err_text(kerror));
  11782.         }
  11783.         if (!kerror) {
  11784.             int rc;
  11785.             rc = des_key_sched(ftp_cred.session, ftp_sched);
  11786.             if (rc == -1) {
  11787.                printf("?Invalid DES key specified in credentials\r\n");
  11788.                debug(F110,"ftp_auth",
  11789.                  "invalid DES Key specified in credentials",0);
  11790.             } else if ( rc == -2 ) {
  11791.             printf("?Weak DES key specified in credentials\r\n");
  11792.             debug(F110,"ftp_auth",
  11793.                   "weak DES Key specified in credentials",0);
  11794.             } else if ( rc != 0 ) {
  11795.             printf("?DES Key Schedule not set by credentials\r\n");
  11796.             debug(F110,"ftp_auth",
  11797.                   "DES Key Schedule not set by credentials",0);
  11798.             }
  11799.             reply_parse = "ADAT=";
  11800.             i = sizeof(out_buf);
  11801.             kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  11802.                       &i, RADIX_ENCODE);
  11803.             if (kerror) {
  11804.             fprintf(stderr, "Base 64 encoding failed: %s\n",
  11805.                 radix_error(kerror));
  11806.             goto krb4_err;
  11807.             }
  11808.             if (i > FTP_BUFSIZ - 6)
  11809.               printf("?ADAT data too long\n");
  11810.             if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  11811.             REPLY_COMPLETE) {
  11812.             fprintf(stderr, "Kerberos V4 authentication failed\n");
  11813.             goto krb4_err;
  11814.             }
  11815.             if (!reply_parse) {
  11816.             fprintf(stderr,
  11817.                  "No authentication data received from server\n");
  11818.             goto krb4_err;
  11819.             }
  11820.             i = sizeof(out_buf);
  11821.             kerror =
  11822.               radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  11823.             if (kerror) {
  11824.             fprintf(stderr, "Base 64 decoding failed: %s\n",
  11825.                 radix_error(kerror));
  11826.             goto krb4_err;
  11827.             }
  11828.             kerror = krb_rd_safe(out_buf, i,
  11829. #ifdef KRB524
  11830.                      ftp_cred.session,
  11831. #else /* KRB524 */
  11832.                      &ftp_cred.session,
  11833. #endif /* KRB524 */
  11834.                      &hisctladdr,
  11835.                      &myctladdr,
  11836.                      &ftp_msg_data
  11837.                      );
  11838.             if (kerror) {
  11839.             fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  11840.                 krb_get_err_text(kerror));
  11841.             goto krb4_err;
  11842.             }
  11843.  
  11844.             /* fetch the (modified) checksum */
  11845.             memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  11846.             if (ntohl(cksum) == checksum + 1) {
  11847.             if (ftp_vbm)
  11848.               printf("Kerberos V4 authentication succeeded\n");
  11849.             reply_parse = NULL;
  11850.             auth_type = "KERBEROS_V4";
  11851.             return(1);
  11852.             } else
  11853.               fprintf(stderr,
  11854.                   "Kerberos V4 mutual authentication failed\n");
  11855.           krb4_err:
  11856.             reply_parse = NULL;
  11857.         }
  11858.         } else if (ftp_deb)
  11859.           fprintf(stderr,
  11860.               "KERBEROS_V4 rejected as an authentication type\n");
  11861.     }
  11862. #endif /* FTP_KRB4 */
  11863. #ifdef CK_SSL
  11864.     if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  11865. #ifdef FTPHOST
  11866.             if (!hostcmd) {
  11867.                 ftpcmd("HOST",ftp_host,0,0,0);
  11868.                 hostcmd = 1;
  11869.             }
  11870. #endif /* FTPHOST */
  11871.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  11872.             if (n != REPLY_COMPLETE)
  11873.           n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  11874.         if (n == REPLY_COMPLETE) {
  11875.         if (!quiet)
  11876.           printf("TLS accepted as authentication type\n");
  11877.  
  11878.         auth_type = "TLS";
  11879.         ssl_auth();
  11880.         if (ssl_ftp_active_flag ) {
  11881.             ftp_dpl = FPL_CLR;
  11882.             ftp_cpl = FPL_PRV;
  11883.             return(1);
  11884.         } else {
  11885.             fprintf(stderr,"TLS authentication failed\n");
  11886.             auth_type = NULL;
  11887. #ifdef TCPIPLIB
  11888.             socket_close(csocket);
  11889. #else /* TCPIPLIB */
  11890. #ifdef USE_SHUTDOWN
  11891.             shutdown(csocket, 1+1);
  11892. #endif /* USE_SHUTDOWN */
  11893.             close(csocket);
  11894. #endif /* TCPIPLIB */
  11895.             csocket = -1;
  11896.             if (ftp_hookup(ftp_host,ftp_port,0) == NULL)
  11897.               return(0);
  11898.         }
  11899.         } else if (ftp_deb)
  11900.           fprintf(stderr,"TLS rejected as an authentication type\n");
  11901.     }
  11902.     if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  11903. #ifdef FTPHOST
  11904.             if (!hostcmd) {
  11905.                 ftpcmd("HOST",ftp_host,0,0,0);
  11906.                 hostcmd = 1;
  11907.             }
  11908. #endif /* FTPHOST */
  11909.         n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  11910.         if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  11911.         if (!quiet)
  11912.           printf("SSL accepted as authentication type\n");
  11913.         auth_type = "SSL";
  11914.         ssl_auth();
  11915.         if (ssl_ftp_active_flag) {
  11916.             ftp_dpl = FPL_PRV;
  11917.             ftp_cpl = FPL_PRV;
  11918.             setprotbuf(1<<20);
  11919.             return(1);
  11920.         } else {
  11921.             fprintf(stderr,"SSL authentication failed\n");
  11922.             auth_type = NULL;
  11923. #ifdef TCPIPLIB
  11924.             socket_close(csocket);
  11925. #else /* TCPIPLIB */
  11926. #ifdef USE_SHUTDOWN
  11927.             shutdown(csocket, 1+1);
  11928. #endif /* USE_SHUTDOWN */
  11929.             close(csocket);
  11930. #endif /* TCPIPLIB */
  11931.             csocket = -1;
  11932.             if (ftp_hookup(ftp_host,ftp_port,0) == NULL)
  11933.               return(0);
  11934.         }
  11935.         } else if (ftp_deb)
  11936.       fprintf(stderr, "SSL rejected as an authentication type\n");
  11937.  
  11938.     }
  11939. #endif /* CK_SSL */
  11940.     /* Other auth types go here ... */
  11941.     } /* for (j;;) */
  11942.     return(0);
  11943. }
  11944. #endif /* FTP_SECURITY */
  11945.  
  11946. static int
  11947. #ifdef CK_ANSIC
  11948. setprotbuf(unsigned int size)
  11949. #else
  11950. setprotbuf(size) unsigned int size;
  11951. #endif /* CK_ANSIC */
  11952. /* setprotbuf */ {
  11953.     if (ucbuf)
  11954.       free(ucbuf);
  11955.     ucbuf = NULL;
  11956.     ucbufsiz = 0;
  11957.     actualbuf = size;
  11958.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  11959.     if (actualbuf)
  11960.       actualbuf /= 2;
  11961.     else
  11962.       return(0);
  11963.     }
  11964.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  11965.     if (ucbufsiz < 128) {
  11966.     printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  11967.     } else if (ucbufsiz < 0) {
  11968.     printf("ERROR: ucbuf allocation failure\n");
  11969.     return(-1);
  11970.     }
  11971.     maxbuf = actualbuf;
  11972.     return(1);
  11973. }
  11974.  
  11975. static int
  11976. #ifdef CK_ANSIC
  11977. setpbsz(unsigned int size)
  11978. #else
  11979. setpbsz(size) unsigned int size;
  11980. #endif /* CK_ANSIC */
  11981. /* setpbsz */ {
  11982.     if (!setprotbuf(size)) {
  11983.         perror("?Error while trying to malloc PROT buffer:");
  11984. #ifdef FTP_SRP
  11985.         srp_reset();
  11986. #endif /* FTP_SRP */
  11987.         ftpclose();
  11988.         return(-1);
  11989.     }
  11990.     reply_parse = "PBSZ=";
  11991.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  11992. #ifdef CK_SSL
  11993.          ssl_ftp_active_flag ? "0" :
  11994. #endif /* CK_SSL */
  11995.          ckuitoa(actualbuf),NULL,NULL);
  11996.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  11997.         if (connected) {
  11998.         printf("?Unable to negotiate PROT buffer size with FTP server\n");
  11999.         ftpclose();
  12000.     }
  12001.         return(-1);
  12002.     }
  12003.     if (reply_parse) {
  12004.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  12005.       maxbuf = actualbuf;
  12006.     } else
  12007.       maxbuf = actualbuf;
  12008.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  12009.     reply_parse = NULL;
  12010.     return(0);
  12011. }
  12012.  
  12013. static VOID
  12014. cancel_remote(din) int din; {
  12015.     CHAR buf[FTP_BUFSIZ];
  12016.     int x, nfnd;
  12017. #ifdef BSDSELECT
  12018.     fd_set mask;
  12019. #endif /* BSDSELECT */
  12020. #ifdef IBMSELECT
  12021.     int fds[2], fdcnt = 0;
  12022. #endif /* IBMSELECT */
  12023. #ifdef DEBUG
  12024.     extern int debtim;
  12025.     int xdebtim;
  12026.     xdebtim = debtim;
  12027.     debtim = 1;
  12028. #endif /* DEBUG */
  12029.     debug(F100,"ftp cancel_remote entry","",0);
  12030. #ifdef CK_SSL
  12031.     if (ssl_ftp_active_flag) {
  12032.         /* 
  12033.          * Send Telnet IP, Telnet DM but do so inline and within the 
  12034.          * TLS channel 
  12035.          */
  12036.         int count, error;
  12037.  
  12038.         buf[0] = IAC;
  12039.         buf[1] = TN_IP;
  12040.         buf[2] = IAC;
  12041.         buf[3] = TN_DM;
  12042.         buf[4] = NUL;
  12043.  
  12044.         count = SSL_write(ssl_ftp_con, buf, 4);
  12045.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  12046.         error = SSL_get_error(ssl_ftp_con,count);
  12047.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  12048.         switch (error) {
  12049.       case SSL_ERROR_NONE:
  12050.             break;
  12051.       case SSL_ERROR_WANT_WRITE:
  12052.       case SSL_ERROR_WANT_READ:
  12053.       case SSL_ERROR_SYSCALL:
  12054. #ifdef NT
  12055.         {
  12056.         int gle = GetLastError();
  12057.         }
  12058. #endif /* NT */
  12059.       case SSL_ERROR_WANT_X509_LOOKUP:
  12060.       case SSL_ERROR_SSL:
  12061.       case SSL_ERROR_ZERO_RETURN:
  12062.           default:
  12063.             lostpeer();
  12064.             return;
  12065.         }
  12066.     } else
  12067. #endif /* CK_SSL */
  12068.     {
  12069.         /*
  12070.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  12071.          * after urgent byte rather than before as is protocol now.
  12072.          */
  12073.         buf[0] = IAC;
  12074.         buf[1] = TN_IP;
  12075.         buf[2] = IAC;
  12076.         buf[3] = NUL;
  12077.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  12078.           perror("cancel");
  12079.         debug(F101,"ftp cancel_remote send 1","",x);
  12080.         buf[0] = TN_DM;
  12081.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  12082.         debug(F101,"ftp cancel_remote send 2","",x);
  12083.     }
  12084.     x = scommand("ABOR");
  12085.     debug(F101,"ftp cancel_remote scommand","",x);
  12086. #ifdef BSDSELECT
  12087.     FD_ZERO(&mask);
  12088.     FD_SET(csocket, &mask);
  12089.     if (din) {
  12090.         FD_SET(din, &mask);
  12091.     }
  12092.     nfnd = empty(&mask, 10);
  12093.     debug(F101,"ftp cancel_remote empty","",nfnd);
  12094.     if ((nfnd) <= 0) {
  12095.         if (nfnd < 0) {
  12096.             perror("cancel");
  12097.         }
  12098. #ifdef FTP_PROXY
  12099.         if (ptabflg)
  12100.       ftpcode = -1;
  12101. #endif /* FTP_PROXY */
  12102.         lostpeer();
  12103.     }
  12104.     debug(F110,"ftp cancel_remote","D",0);
  12105.     if (din && FD_ISSET(din, &mask)) {
  12106.         /* Security: No threat associated with this read. */
  12107.         /* But you can't simply read the TLS data stream  */
  12108. #ifdef CK_SSL
  12109.         if (ssl_ftp_data_active_flag) {
  12110.             int count, error;
  12111.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  12112.                     /* LOOP */ ;
  12113.         } else 
  12114. #endif /* CK_SSL */
  12115.         {
  12116.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  12117.                 /* LOOP */ ;
  12118.         }
  12119.     }
  12120.     debug(F110,"ftp cancel_remote","E",0);
  12121. #else /* BSDSELECT */
  12122. #ifdef IBMSELECT
  12123.     fds[0] = csocket;
  12124.     fdcnt++;
  12125.     if (din) {
  12126.         fds[1] = din;
  12127.         fdcnt++;
  12128.     }
  12129.     nfnd = empty(fds, fdcnt, 10);
  12130.     debug(F101,"ftp cancel_remote empty","",nfnd);
  12131.     if ((nfnd) <= 0) {
  12132.         if (nfnd < 0) {
  12133.             perror("cancel");
  12134.         }
  12135. #ifdef FTP_PROXY
  12136.         if (ptabflg)
  12137.       ftpcode = -1;
  12138. #endif /* FTP_PROXY */
  12139.         lostpeer();
  12140.     }
  12141.     debug(F110,"ftp cancel_remote","D",0);
  12142.     if (din && select(&din, 1,0,0,1) ) {
  12143. #ifdef CK_SSL
  12144.         if (ssl_ftp_data_active_flag) {
  12145.             int count, error;
  12146.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  12147.                     /* LOOP */ ;
  12148.         } else 
  12149. #endif /* CK_SSL */
  12150.         {
  12151.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  12152.                 /* LOOP */ ;
  12153.         }
  12154.     }
  12155.     debug(F110,"ftp cancel_remote","E",0);
  12156. #else /* IBMSELECT */
  12157.     Some form of select is required.
  12158. #endif /* IBMSELECT */
  12159. #endif /* BSDSELECT */
  12160.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  12161.     debug(F110,"ftp cancel_remote","F",0);
  12162.         /* 552 needed for NIC style cancel */
  12163.         getreply(0,-1,-1,ftp_vbm,0);
  12164.     debug(F110,"ftp cancel_remote","G",0);
  12165.     }
  12166.     debug(F110,"ftp cancel_remote","H",0);
  12167.     getreply(0,-1,-1,ftp_vbm,0);
  12168.     debug(F110,"ftp cancel_remote","I",0);
  12169. #ifdef DEBUG
  12170.     debtim = xdebtim;
  12171. #endif /* DEBUG */
  12172. }
  12173.  
  12174. static int
  12175. fts_dpl(x) int x; {
  12176.     if (!auth_type) {
  12177.         switch ( x ) {
  12178.       case FPL_PRV:
  12179.             printf("?Cannot set protection level to PRIVATE\n");
  12180.             return(0);
  12181.       case FPL_SAF:
  12182.             printf("?Cannot set protection level to SAFE\n");
  12183.             return(0);
  12184.         }
  12185.         ftp_dpl = x;
  12186.         return(1);
  12187.     }
  12188.  
  12189. #ifdef CK_SSL
  12190.     if (x == FPL_SAF &&
  12191.     (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  12192.         printf("Cannot set protection level to safe\n");
  12193.         return(0);
  12194.     }
  12195. #endif /* CK_SSL */
  12196.     /* Start with a PBSZ of 1 meg */
  12197.     if (x != FPL_CLR) {
  12198.         if (setpbsz(DEFAULT_PBSZ) < 0)
  12199.       return(0);
  12200.     }
  12201.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  12202.            (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  12203.     if (y == REPLY_COMPLETE) {
  12204.         ftp_dpl = x;
  12205.         return(1);
  12206.     }
  12207.     return(0);
  12208. }
  12209.  
  12210. static int
  12211. fts_cpl(x) int x; {
  12212.     if (!auth_type) {
  12213.         switch ( x ) {
  12214.       case FPL_PRV:
  12215.             printf("?Cannot set protection level to PRIVATE\n");
  12216.             return(0);
  12217.       case FPL_SAF:
  12218.             printf("?Cannot set protection level to SAFE\n");
  12219.             return(0);
  12220.         }
  12221.         ftp_cpl = x;
  12222.         return(1);
  12223.     }
  12224.     if (x == FPL_CLR) {
  12225.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  12226.         if (y == REPLY_COMPLETE) {
  12227.             ftp_cpl = x;
  12228.             return(1);
  12229.         }
  12230.         return(0);
  12231.     }
  12232.     ftp_cpl = x;
  12233.     return(1);
  12234. }
  12235.  
  12236. #ifdef FTP_GSSAPI
  12237. static VOID
  12238. user_gss_error(maj_stat, min_stat, s)
  12239.     OM_uint32 maj_stat, min_stat;
  12240.     char *s;
  12241. {
  12242.     /* a lot of work just to report the error */
  12243.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  12244.     gss_buffer_desc msg;
  12245.     msg_ctx = 0;
  12246.     while (!msg_ctx) {
  12247.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  12248.                        GSS_C_GSS_CODE,
  12249.                        GSS_C_NULL_OID,
  12250.                        &msg_ctx,
  12251.                        &msg
  12252.                        );
  12253.         if ((gmaj_stat == GSS_S_COMPLETE)||
  12254.         (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  12255.             fprintf(stderr, "GSSAPI error major: %s\n",
  12256.             (char*)msg.value);
  12257.             gss_release_buffer(&gmin_stat, &msg);
  12258.         }
  12259.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  12260.       break;
  12261.     }
  12262.     msg_ctx = 0;
  12263.     while (!msg_ctx) {
  12264.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  12265.                        GSS_C_MECH_CODE,
  12266.                        GSS_C_NULL_OID,
  12267.                        &msg_ctx,
  12268.                        &msg
  12269.                        );
  12270.         if ((gmaj_stat == GSS_S_COMPLETE)||
  12271.         (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  12272.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  12273.             gss_release_buffer(&gmin_stat, &msg);
  12274.         }
  12275.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  12276.       break;
  12277.     }
  12278.     fprintf(stderr, "GSSAPI error: %s\n", s);
  12279. }
  12280. #endif /* FTP_GSSAPI */
  12281.  
  12282. #ifndef NOMHHOST
  12283. #ifdef datageneral
  12284. #define NOMHHOST
  12285. #else
  12286. #ifdef HPUX5WINTCP
  12287. #define NOMHHOST
  12288. #endif /* HPUX5WINTCP */
  12289. #endif /* datageneral */
  12290. #endif /* NOMHHOST */
  12291.  
  12292. #ifdef INADDRX
  12293. static struct in_addr inaddrx;
  12294. #endif /* INADDRX */
  12295.  
  12296. static char *
  12297. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  12298.     register struct hostent *hp = 0;
  12299. #ifdef IP_TOS
  12300. #ifdef IPTOS_THROUGHPUT
  12301.     int tos;
  12302. #endif /* IPTOS_THROUGHPUT */
  12303. #endif /* IP_TOS */
  12304.     int s;
  12305.     GSOCKNAME_T len;
  12306.     static char hostnamebuf[512];
  12307.     char hostname[512] /* , *p, *q */ ;
  12308.     int  cport;
  12309. #ifdef DEBUG
  12310.     extern int debtim;
  12311.     int xdebtim;
  12312.     xdebtim = debtim;
  12313.     debtim = 1;
  12314. #endif /* DEBUG */
  12315.  
  12316. #ifndef NOHTTP
  12317.     if (tcp_http_proxy) {
  12318.         struct servent *destsp;
  12319.         char *p, *q;
  12320.  
  12321.         ckmakmsg(proxyhost,sizeof(proxyhost),host,":",ckuitoa(port),NULL);
  12322.         for (p = tcp_http_proxy, q = hostname;
  12323.          *p != '\0' && *p != ':';
  12324.          p++, q++
  12325.          )
  12326.       *q = *p;
  12327.         *q = '\0';
  12328.  
  12329.         if (*p == ':')
  12330.       p++;
  12331.         else
  12332.       p = "http";
  12333.  
  12334.         destsp = getservbyname(p,"tcp");
  12335.         if (destsp)
  12336.       cport = ntohs(destsp->s_port);
  12337.         else if (p) {
  12338.           cport = atoi(p);
  12339.         } else
  12340.       cport = 80;
  12341.     } else
  12342. #endif /* NOHTTP */
  12343.     {
  12344.         ckstrncpy(hostname,host,sizeof(hostname));
  12345.         cport = port;
  12346.     }
  12347.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  12348.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  12349.     if (hisctladdr.sin_addr.s_addr != -1) {
  12350.     debug(F110,"ftp hookup A",hostname,0);
  12351.         hisctladdr.sin_family = AF_INET;
  12352.         ckstrncpy(hostnamebuf, hostname, sizeof(hostnamebuf));
  12353.     } else {
  12354.     debug(F110,"ftp hookup B",hostname,0);
  12355.         hp = gethostbyname(hostname);
  12356. #ifdef HADDRLIST
  12357.         hp = ck_copyhostent(hp);    /* make safe copy that won't change */
  12358. #endif /* HADDRLIST */
  12359.         if (hp == NULL) {
  12360.             fprintf(stderr, "ftp: %s: ", host);
  12361.             herror();
  12362.             ftpcode = -1;
  12363. #ifdef DEBUG
  12364.         debtim = xdebtim;
  12365. #endif /* DEBUG */
  12366.             return((char *) 0);
  12367.         }
  12368.         hisctladdr.sin_family = hp->h_addrtype;
  12369. #ifdef HADDRLIST
  12370.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  12371.            sizeof(hisctladdr.sin_addr));
  12372. #else /* HADDRLIST */
  12373.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  12374.            sizeof(hisctladdr.sin_addr));
  12375. #endif /* HADDRLIST */
  12376.         ckstrncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
  12377.     }
  12378.     debug(F110,"ftp hookup C",hostnamebuf,0);
  12379.     ftp_host = hostnamebuf;
  12380.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12381.     debug(F101,"ftp hookup socket","",s);
  12382.     if (s < 0) {
  12383.         perror("ftp: socket");
  12384.         ftpcode = -1;
  12385. #ifdef DEBUG
  12386.     debtim = xdebtim;
  12387. #endif /* DEBUG */
  12388.         return(0);
  12389.     }
  12390.     hisctladdr.sin_port = htons(cport);
  12391.     errno = 0;
  12392. #ifdef HADDRLIST
  12393.     debug(F100,"ftp hookup HADDRLIST","",0);
  12394.     while
  12395. #else
  12396.     debug(F100,"ftp hookup no HADDRLIST","",0);
  12397.     if
  12398. #endif /* HADDRLIST */
  12399.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  12400.       debug(F101,"ftp hookup connect failed","",errno);
  12401. #ifdef HADDRLIST
  12402.       if (hp && hp->h_addr_list[1]) {
  12403.           int oerrno = errno;
  12404.  
  12405.           fprintf(stderr, "ftp: connect to address %s: ",
  12406.               inet_ntoa(hisctladdr.sin_addr));
  12407.           errno = oerrno;
  12408.           perror((char *) 0);
  12409.           hp->h_addr_list++;
  12410.           memcpy((char *)&hisctladdr.sin_addr,
  12411.              hp->h_addr_list[0],
  12412.              sizeof(hisctladdr.sin_addr));
  12413.           fprintf(stdout, "Trying %s...\n",
  12414.               inet_ntoa(hisctladdr.sin_addr));
  12415. #ifdef TCPIPLIB
  12416.           socket_close(s);
  12417. #else /* TCPIPLIB */
  12418.           close(s);
  12419. #endif /* TCPIPLIB */
  12420.           s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12421.           if (s < 0) {
  12422.           perror("ftp: socket");
  12423.           ftpcode = -1;
  12424. #ifdef DEBUG
  12425.           debtim = xdebtim;
  12426. #endif /* DEBUG */
  12427.           return(0);
  12428.           }
  12429.           continue;
  12430.       }
  12431. #endif /* HADDRLIST */
  12432.       perror("ftp: connect");
  12433.       ftpcode = -1;
  12434.       goto bad;
  12435.       }
  12436.     debug(F100,"ftp hookup connect ok","",0);
  12437.  
  12438.     len = sizeof (myctladdr);
  12439.     errno = 0;
  12440.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  12441.     debug(F101,"ftp hookup getsockname failed","",errno);
  12442.         perror("ftp: getsockname");
  12443.         ftpcode = -1;
  12444.         goto bad;
  12445.     }
  12446.     debug(F100,"ftp hookup getsockname ok","",0);
  12447.  
  12448. #ifndef NOHTTP
  12449.     if (tcp_http_proxy) {
  12450. #ifdef OS2
  12451.         char * agent = "Kermit 95";    /* Default user agent */
  12452. #else
  12453.         char * agent = "C-Kermit";
  12454. #endif /* OS2 */
  12455.  
  12456.         if (http_connect(s,agent,NULL,
  12457.                          tcp_http_proxy_user,
  12458.                          tcp_http_proxy_pwd,
  12459.                          0,
  12460.                          proxyhost
  12461.                          ) < 0)    {
  12462.         char * foo = NULL;
  12463. #ifdef TCPIPLIB
  12464.             socket_close(s);
  12465. #else /* TCPIPLIB */
  12466.             close(s);
  12467. #endif /* TCPIPLIB */
  12468.  
  12469.         while (foo == NULL && tcp_http_proxy != NULL ) {
  12470.  
  12471.         if (tcp_http_proxy_errno == 401 ||
  12472.              tcp_http_proxy_errno == 407 ) {
  12473.             char uid[UIDBUFLEN];
  12474.             char pwd[256];
  12475.  
  12476.             readtext("Proxy Userid: ",uid,UIDBUFLEN);
  12477.             if (uid[0]) {
  12478.             char * proxy_user, * proxy_pwd;
  12479.  
  12480.             readpass("Proxy Password: ",pwd,256);
  12481.  
  12482.             proxy_user = tcp_http_proxy_user;
  12483.             proxy_pwd  = tcp_http_proxy_pwd;
  12484.  
  12485.             tcp_http_proxy_user = uid;
  12486.             tcp_http_proxy_pwd = pwd;
  12487.  
  12488.             foo = ftp_hookup(host, port, 0);
  12489.  
  12490.             debug(F110,"ftp_hookup()",foo,0);
  12491.             memset(pwd,0,sizeof(pwd));
  12492.             tcp_http_proxy_user = proxy_user;
  12493.             tcp_http_proxy_pwd = proxy_pwd;
  12494.             } else
  12495.             break;
  12496.         } else
  12497.             break;
  12498.         }
  12499.         if (foo != NULL)
  12500.           return(foo);
  12501.             perror("ftp: connect");
  12502.             ftpcode = -1;
  12503.             goto bad;
  12504.         }
  12505.         ckstrncpy(hostnamebuf, proxyhost, sizeof(hostnamebuf));
  12506.     }
  12507. #endif /* NOHTTP */
  12508.  
  12509.     csocket = s;
  12510.  
  12511. #ifdef CK_SSL
  12512.     if (tls) {
  12513.         /* FTP over SSL
  12514.          * If the connection is over an SSL proxy then the 
  12515.          * auth_type will be NULL.  However, I'm not sure
  12516.          * whether we should protect the data channel in
  12517.          * that case or not.
  12518.          */
  12519.  
  12520.         debug(F100,"ftp hookup use_tls","",0);
  12521.         if (!ssl_auth()) {
  12522.             debug(F100,"ftp hookup ssl_auth failed","",0);
  12523.             auth_type = NULL;
  12524.             ftpcode = -1;
  12525.             csocket = -1;
  12526.             goto bad;
  12527.         }
  12528.         ssl_ftp_proxy = 1;
  12529.     }
  12530. #endif /* CK_SSL */
  12531.  
  12532. #ifdef IP_TOS
  12533. #ifdef IPTOS_LOWDELAY
  12534.     tos = IPTOS_LOWDELAY;
  12535.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  12536.       perror("ftp: setsockopt TOS (ignored)");
  12537. #endif
  12538. #endif
  12539.     if (!quiet)
  12540.       printf("Connected to %s.\n", ftp_host);
  12541.  
  12542.     /* Read greeting from server */
  12543.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  12544.     debug(F100,"ftp hookup bad reply","",0);
  12545. #ifdef TCPIPLIB
  12546.         socket_close(csocket);
  12547. #else /* TCPIPLIB */
  12548.         close(csocket);
  12549. #endif /* TCPIPLIB */
  12550.         ftpcode = -1;
  12551.         goto bad;
  12552.     }
  12553. #ifdef SO_OOBINLINE
  12554.     {
  12555.     int on = 1;
  12556.     errno = 0;
  12557.     if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  12558.                sizeof(on)) < 0) {
  12559.             perror("ftp: setsockopt");
  12560.         debug(F101,"ftp hookup setsockopt failed","",errno);
  12561.         }
  12562. #ifdef DEBUG
  12563.     else
  12564.       debug(F100,"ftp hookup setsockopt ok","",0);
  12565. #endif /* DEBUG */
  12566.     }
  12567. #endif /* SO_OOBINLINE */
  12568.  
  12569. #ifdef DEBUG
  12570.     debtim = xdebtim;
  12571. #endif /* DEBUG */
  12572.     return(ftp_host);
  12573.  
  12574.   bad:
  12575.     debug(F100,"ftp hookup bad","",0);
  12576. #ifdef TCPIPLIB
  12577.     socket_close(s);
  12578. #else /* TCPIPLIB */
  12579.     close(s);
  12580. #endif /* TCPIPLIB */
  12581. #ifdef DEBUG
  12582.     debtim = xdebtim;
  12583. #endif /* DEBUG */
  12584.     csocket = -1;
  12585.     return((char *)0);
  12586. }
  12587.  
  12588. static VOID
  12589. ftp_init() {
  12590.     int n;
  12591.  
  12592.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  12593.     /* clients do it.  In any case, failure of this command is not a */
  12594.     /* reliable indication that the server does not support Restart. */
  12595.  
  12596.     okrestart = 0;
  12597.     n = ftpcmd("REST 0",NULL,0,0,0);
  12598.     if (n == REPLY_COMPLETE)
  12599.       okrestart = 1;
  12600. #ifdef COMMENT
  12601.     else if (ftp_deb)
  12602.       printf("WARNING: Unable to restore file pointer.\n");
  12603. #endif /* COMMENT */
  12604.  
  12605.     n = ftpcmd("SYST",NULL,0,0,0);    /* Get server system type */
  12606.     if (n == REPLY_COMPLETE) {
  12607.     register char *cp, c = NUL;
  12608.     cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  12609.     if (cp == NULL)
  12610.       cp = ckstrchr(ftp_reply_str+4,'\r');
  12611.     if (cp) {
  12612.         if (cp[-1] == '.')
  12613.           cp--;
  12614.         c = *cp;            /* Save this char */
  12615.         *cp = '\0';            /* Replace it with NUL */
  12616.     }
  12617.     if (!quiet)
  12618.       printf("Remote system type is %s.\n",ftp_reply_str+4);
  12619.     ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  12620.     if (cp)                /* Put back saved char */
  12621.       *cp = c;
  12622.     }
  12623.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  12624.  
  12625.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  12626.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  12627.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  12628.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  12629.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  12630.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  12631.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  12632.  
  12633. #ifdef FTP_PROXY
  12634.     unix_proxy = 0;
  12635.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  12636. #endif /* FTP_PROXY */
  12637.  
  12638.     if (ftp_cmdlin && xfermode == XMODE_M)
  12639.       ftp_typ = binary;            /* Type given on command line */
  12640.     else                /* Otherwise set it automatically */
  12641.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  12642.     changetype(ftp_typ,0);        /* Change to this type */
  12643.     g_ftp_typ = ftp_typ;        /* Make it the global type */
  12644.     if (!quiet)
  12645.       printf("Default transfer mode is %s\n",
  12646.          ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  12647.          );
  12648.     n = ftpcmd("MODE S",NULL,0,0,0);    /* We always send in Stream mode */
  12649. #ifdef COMMENT
  12650.     if (n != REPLY_COMPLETE)
  12651.       printf("WARNING: Server does not accept MODE S(TREAM)\n");
  12652. #endif /* COMMENT */
  12653.     n = ftpcmd("STRU F",NULL,0,0,0);    /* STRU File (not Record or Page) */
  12654. #ifdef COMMENT
  12655.     if (n != REPLY_COMPLETE)
  12656.       printf("WARNING: Server does not accept STRU F(ILE)\n");
  12657. #endif /* COMMENT */
  12658. }
  12659.  
  12660.  
  12661. static int
  12662. ftp_login(host) char * host; {        /* (also called from ckuusy.c) */
  12663.     char tmp[256];
  12664.     char *user = NULL, *pass = NULL, *acct = NULL;
  12665.     int n, aflag = 0;
  12666.  
  12667.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  12668.  
  12669.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  12670.       anonymous = 1;
  12671.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  12672.       anonymous = 1;
  12673.  
  12674. #ifdef FTP_SRP
  12675.     if (auth_type && !strcmp(auth_type, "SRP")) {
  12676.         user = srp_user;
  12677.         pass = srp_pass;
  12678.         acct = srp_acct;
  12679.     } else
  12680. #endif /* FTP_SRP */
  12681.       if (anonymous) {
  12682.       user = "anonymous";
  12683.       if (ftp_tmp) {
  12684.           pass = ftp_tmp;
  12685.       } else {
  12686.           ckmakmsg(tmp,256,whoami(),"@",myhost,NULL);
  12687.           pass = tmp;
  12688.       }
  12689.       } else {
  12690. #ifdef USE_RUSERPASS
  12691.       if (ruserpass(host, &user, &pass, &acct) < 0) {
  12692.           ftpcode = -1;
  12693.           return(0);
  12694.       }
  12695. #endif /* USE_RUSERPASS */
  12696.       user = ftp_logname;
  12697.       pass = ftp_tmp;
  12698.       acct = ftp_acc;
  12699.       while (user == NULL) {
  12700.           char *myname, prompt[256];
  12701.  
  12702.           myname = whoami();
  12703.           if (myname)
  12704.                 ckmakxmsg(prompt,256," Name (",host,":",myname,"): ",
  12705.               NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  12706.           else
  12707.                 ckmakmsg(prompt,256," Name (",host,"): ",NULL);
  12708.           tmp[0] = '\0';
  12709.           readtext(prompt,tmp,80);
  12710.           if (*tmp == '\0')
  12711.                 user = myname;
  12712.           else
  12713.                 user = tmp;
  12714.       }
  12715.       }
  12716.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  12717.     if (n == REPLY_COMPLETE) {
  12718.         /* determine if we need to send a dummy password */
  12719.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  12720.       ftpcmd("PASS dummy",NULL,0,0,1);
  12721.     } else if (n == REPLY_CONTINUE) {
  12722. #ifdef CK_ENCRYPTION
  12723.         int oldftp_cpl;
  12724. #endif /* CK_ENCRYPTION */
  12725.         if (pass == NULL) {
  12726.             static char ftppass[80];
  12727.         setint();
  12728.             readpass(" Password:",ftppass,80);
  12729.             pass = ftppass;
  12730.         }
  12731. #ifdef CK_ENCRYPTION
  12732.         oldftp_cpl = ftp_cpl;
  12733.         ftp_cpl = FPL_PRV;
  12734. #endif /* CK_ENCRYPTION */
  12735.         n = ftpcmd("PASS",pass,-1,-1,1);
  12736.     if (!anonymous && pass) {
  12737.         char * p = pass;
  12738.         while (*p++) *(p-1) = NUL;
  12739.         makestr(&ftp_tmp,NULL);
  12740.     }
  12741. #ifdef CK_ENCRYPTION
  12742.         /* level may have changed */
  12743.         if (ftp_cpl == FPL_PRV)
  12744.       ftp_cpl = oldftp_cpl;
  12745. #endif /* CK_ENCRYPTION */
  12746.     }
  12747.     if (n == REPLY_CONTINUE) {
  12748.         aflag++;
  12749.         if (acct == NULL) {
  12750.             static char ftpacct[80];
  12751.         setint();
  12752.             readpass(" Account:",ftpacct,80);
  12753.             acct = ftpacct;
  12754.         }
  12755.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12756.     }
  12757.     if (n != REPLY_COMPLETE) {
  12758.         fprintf(stderr, "FTP login failed.\n");
  12759.     if (haveurl)
  12760.       doexit(BAD_EXIT,-1);
  12761.         return(0);
  12762.     }
  12763.     if (!aflag && acct != NULL) {
  12764.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12765.     }
  12766.     makestr(&ftp_logname,user);
  12767.     loggedin = 1;
  12768.     ftp_init();
  12769.  
  12770.     if (anonymous && !quiet) {
  12771.     printf(" Logged in as anonymous (%s)\n",pass);
  12772.     memset(pass, 0, strlen(pass));
  12773.     }
  12774.     if (ftp_rdir) {
  12775.     if (doftpcwd(ftp_rdir,-1) < 1)
  12776.       doexit(BAD_EXIT,-1);
  12777.     }
  12778.  
  12779. #ifdef FTP_PROXY
  12780.     if (proxy)
  12781.       return(1);
  12782. #endif /* FTP_PROXY */
  12783.     return(1);
  12784. }
  12785.  
  12786. static int
  12787. ftp_reset() {
  12788.     int rc;
  12789. #ifdef BSDSELECT
  12790.     int nfnd = 1;
  12791.     fd_set mask;
  12792.     FD_ZERO(&mask);
  12793.     while (nfnd > 0) {
  12794.         FD_SET(csocket, &mask);
  12795.         if ((nfnd = empty(&mask,0)) < 0) {
  12796.             perror("reset");
  12797.             ftpcode = -1;
  12798.             lostpeer();
  12799.             return(0);
  12800.         } else if (nfnd) {
  12801.             getreply(0,-1,-1,ftp_vbm,0);
  12802.         }
  12803.     }
  12804. #else /* BSDSELECT */
  12805. #ifdef IBMSELECT
  12806.     int nfnd = 1;
  12807.     while (nfnd > 0) {
  12808.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  12809.             perror("reset");
  12810.             ftpcode = -1;
  12811.             lostpeer();
  12812.             return(0);
  12813.         } else if (nfnd) {
  12814.             getreply(0,-1,-1,ftp_vbm,0);
  12815.         }
  12816.     }
  12817. #endif /* IBMSELECT */
  12818. #endif /* BSDSELECT */
  12819.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  12820.     if (rc > 0)
  12821.       loggedin = 0;
  12822.     return(rc);
  12823. }
  12824.  
  12825. static int
  12826. ftp_rename(from, to) char * from, * to; {
  12827.     int lcs = -1, rcs = -1;
  12828. #ifndef NOCSETS
  12829.     if (ftp_xla) {
  12830.     lcs = ftp_csl;
  12831.     if (lcs < 0) lcs = fcharset;
  12832.     rcs = ftp_csx;
  12833.     if (rcs < 0) rcs = ftp_csr;
  12834.     }
  12835. #endif /* NOCSETS */
  12836.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  12837.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  12838.     }
  12839.     return(0);                /* Failure */
  12840. }
  12841.  
  12842. static int
  12843. ftp_umask(mask) char * mask; {
  12844.     int rc;
  12845.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  12846.     return(rc);
  12847. }
  12848.  
  12849. static int
  12850. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  12851.     int n = 0, aflag = 0;
  12852.     char pwd[80];
  12853.  
  12854.     if (!auth_type && ftp_aut) {
  12855. #ifdef FTP_SRP
  12856.         if (ck_srp_is_installed()) {
  12857.             if (srp_ftp_auth( NULL, user, pass)) {
  12858.         makestr(&pass,srp_pass);
  12859.             }
  12860.         }
  12861. #endif /* FTP_SRP */
  12862.     }
  12863.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  12864.     if (n == REPLY_COMPLETE)
  12865.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  12866.     else if (n == REPLY_CONTINUE) {
  12867. #ifdef CK_ENCRYPTION
  12868.         int oldftp_cpl;
  12869. #endif /* CK_ENCRYPTION */
  12870.         if (pass == NULL || !pass[0]) {
  12871.             pwd[0] = '\0';
  12872.         setint();
  12873.             readpass(" Password: ",pwd,sizeof(pwd));
  12874.             pass = pwd;
  12875.         }
  12876.  
  12877. #ifdef CK_ENCRYPTION
  12878.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  12879.       ftp_cpl = PROT_P;
  12880. #endif /* CK_ENCRYPTION */
  12881.         n = ftpcmd("PASS",pass,-1,-1,1);
  12882.         memset(pass, 0, strlen(pass));
  12883. #ifdef CK_ENCRYPTION
  12884.         /* level may have changed */
  12885.         if (ftp_cpl == PROT_P)
  12886.       ftp_cpl = oldftp_cpl;
  12887. #endif /* CK_ENCRYPTION */
  12888.     }
  12889.     if (n == REPLY_CONTINUE) {
  12890.         if (acct == NULL || !acct[0]) {
  12891.             pwd[0] = '\0';
  12892.         setint();
  12893.             readpass(" Account: ",pwd,sizeof(pwd));
  12894.             acct = pwd;
  12895.         }
  12896.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12897.         aflag++;
  12898.     }
  12899.     if (n != REPLY_COMPLETE) {
  12900.         printf("Login failed.\n");
  12901.         return(0);
  12902.     }
  12903.     if (!aflag && acct != NULL && acct[0]) {
  12904.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12905.     }
  12906.     if (n == REPLY_COMPLETE) {
  12907.     makestr(&ftp_logname,user);
  12908.     loggedin = 1;
  12909.     ftp_init();
  12910.     return(1);
  12911.     }
  12912.     return(0);
  12913. }
  12914.  
  12915. char *
  12916. ftp_authtype() {
  12917.     if (!connected)
  12918.       return("NULL");
  12919.     return(auth_type ? auth_type : "NULL");
  12920. }
  12921.  
  12922. char *
  12923. ftp_cpl_mode() {
  12924.     switch (ftp_cpl) {
  12925.       case FPL_CLR:
  12926.         return("clear");
  12927.       case FPL_SAF:
  12928.         return("safe");
  12929.       case FPL_PRV:
  12930.         return("private");
  12931.       case FPL_CON:
  12932.         return("confidential");
  12933.       default:
  12934.         return("(error)");
  12935.     }
  12936. }
  12937.  
  12938. char *
  12939. ftp_dpl_mode() {
  12940.     switch (ftp_dpl) {
  12941.       case FPL_CLR:
  12942.         return("clear");
  12943.       case FPL_SAF:
  12944.         return("safe");
  12945.       case FPL_PRV:
  12946.         return("private");
  12947.       case FPL_CON:
  12948.         return("confidential");
  12949.       default:
  12950.         return("(error)");
  12951.     }
  12952. }
  12953.  
  12954. #ifdef NOT_USED
  12955. /*
  12956.   The following code is from the Unix FTP client.  Be sure to
  12957.   make sure that the functionality is not lost.  Especially
  12958.   the Proxy stuff even though we have not yet implemented it.
  12959. */
  12960.  
  12961. /* Send multiple files  */
  12962.  
  12963. static int
  12964. ftp_mput(argc, argv) int argc; char **argv; {
  12965.     register int i;
  12966.     sig_t oldintr;
  12967.     int ointer;
  12968.     char *tp;
  12969.     sigtype mcancel();
  12970.  
  12971.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  12972.         printf("usage: %s local-files\n", argv[0]);
  12973.         ftpcode = -1;
  12974.         return;
  12975.     }
  12976.     mname = argv[0];
  12977.     mflag = 1;
  12978.     oldintr = signal(SIGINT, mcancel);
  12979.  
  12980.     /* Replace with calls to cc_execute() */
  12981.     setjmp(jcancel);
  12982. #ifdef FTP_PROXY
  12983.     if (proxy) {
  12984.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  12985.  
  12986.         while ((cp = remglob(argv,0)) != NULL) {
  12987.             if (*cp == 0) {
  12988.                 mflag = 0;
  12989.                 continue;
  12990.             }
  12991.             if (mflag && confirm(argv[0], cp)) {
  12992.                 tp = cp;
  12993.                 if (mcase) {
  12994.                     while (*tp && !islower(*tp)) {
  12995.                         tp++;
  12996.                     }
  12997.                     if (!*tp) {
  12998.                         tp = cp;
  12999.                         tp2 = tmpbuf;
  13000.                         while ((*tp2 = *tp) != 0) {
  13001.                             if (isupper(*tp2)) {
  13002.                                 *tp2 = 'a' + *tp2 - 'A';
  13003.                             }
  13004.                             tp++;
  13005.                             tp2++;
  13006.                         }
  13007.                     }
  13008.                     tp = tmpbuf;
  13009.                 }
  13010.                 if (ntflag) {
  13011.                     tp = dotrans(tp);
  13012.                 }
  13013.                 if (mapflag) {
  13014.                     tp = domap(tp);
  13015.                 }
  13016.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  13017.                 if (!mflag && fromatty) {
  13018.                     ointer = interactive;
  13019.                     interactive = 1;
  13020.                     if (confirm("Continue with","mput")) {
  13021.                         mflag++;
  13022.                     }
  13023.                     interactive = ointer;
  13024.                 }
  13025.             }
  13026.         }
  13027.         signal(SIGINT, oldintr);
  13028.         mflag = 0;
  13029.         return;
  13030.     }
  13031. #endif /* FTP_PROXY */
  13032.     for (i = 1; i < argc; i++) {
  13033.         register char **cpp, **gargs;
  13034.  
  13035.         if (mflag && confirm(argv[0], argv[i])) {
  13036.             tp = argv[i];
  13037.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  13038.             if (!mflag && fromatty) {
  13039.                 ointer = interactive;
  13040.                 interactive = 1;
  13041.                 if (confirm("Continue with","mput")) {
  13042.                     mflag++;
  13043.                 }
  13044.                 interactive = ointer;
  13045.             }
  13046.         }
  13047.         continue;
  13048.  
  13049.         gargs = ftpglob(argv[i]);
  13050.         if (globerr != NULL) {
  13051.             printf("%s\n", globerr);
  13052.             if (gargs) {
  13053.                 blkfree(gargs);
  13054.                 free((char *)gargs);
  13055.             }
  13056.             continue;
  13057.         }
  13058.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  13059.             if (mflag && confirm(argv[0], *cpp)) {
  13060.                 tp = *cpp;
  13061.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  13062.                 if (!mflag && fromatty) {
  13063.                     ointer = interactive;
  13064.                     interactive = 1;
  13065.                     if (confirm("Continue with","mput")) {
  13066.                         mflag++;
  13067.                     }
  13068.                     interactive = ointer;
  13069.                 }
  13070.             }
  13071.         }
  13072.         if (gargs != NULL) {
  13073.             blkfree(gargs);
  13074.             free((char *)gargs);
  13075.         }
  13076.     }
  13077.     signal(SIGINT, oldintr);
  13078.     mflag = 0;
  13079. }
  13080.  
  13081. /* Get multiple files */
  13082.  
  13083. static int
  13084. ftp_mget(argc, argv) int argc; char **argv; {
  13085.     int rc = -1;
  13086.     sig_t oldintr;
  13087.     int ointer;
  13088.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  13089.     sigtype mcancel();
  13090.  
  13091.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  13092.         printf("usage: %s remote-files\n", argv[0]);
  13093.         ftpcode = -1;
  13094.         return(-1);
  13095.     }
  13096.     mname = argv[0];
  13097.     mflag = 1;
  13098.     oldintr = signal(SIGINT,mcancel);
  13099.     /* Replace with calls to cc_execute() */
  13100.     setjmp(jcancel);
  13101.     while ((cp = remglob(argv,proxy)) != NULL) {
  13102.         if (*cp == '\0') {
  13103.             mflag = 0;
  13104.             continue;
  13105.         }
  13106.         if (mflag && confirm(argv[0], cp)) {
  13107.             tp = cp;
  13108.             if (mcase) {
  13109.                 while (*tp && !islower(*tp)) {
  13110.                     tp++;
  13111.                 }
  13112.                 if (!*tp) {
  13113.                     tp = cp;
  13114.                     tp2 = tmpbuf;
  13115.                     while ((*tp2 = *tp) != 0) {
  13116.                         if (isupper(*tp2)) {
  13117.                             *tp2 = 'a' + *tp2 - 'A';
  13118.                         }
  13119.                         tp++;
  13120.                         tp2++;
  13121.                     }
  13122.                 }
  13123.                 tp = tmpbuf;
  13124.             }
  13125.             rc = (recvrequest("RETR", tp, cp, "wb",
  13126.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  13127.             if (!mflag && fromatty) {
  13128.                 ointer = interactive;
  13129.                 interactive = 1;
  13130.                 if (confirm("Continue with","mget")) {
  13131.                     mflag++;
  13132.                 }
  13133.                 interactive = ointer;
  13134.             }
  13135.         }
  13136.     }
  13137.     signal(SIGINT,oldintr);
  13138.     mflag = 0;
  13139.     return(rc);
  13140. }
  13141.  
  13142. /* Delete multiple files */
  13143.  
  13144. static int
  13145. mdelete(argc, argv) int argc; char **argv; {
  13146.     sig_t oldintr;
  13147.     int ointer;
  13148.     char *cp;
  13149.     sigtype mcancel();
  13150.  
  13151.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  13152.         printf("usage: %s remote-files\n", argv[0]);
  13153.         ftpcode = -1;
  13154.         return(-1);
  13155.     }
  13156.     mname = argv[0];
  13157.     mflag = 1;
  13158.     oldintr = signal(SIGINT, mcancel);
  13159.     /* Replace with calls to cc_execute() */
  13160.     setjmp(jcancel);
  13161.     while ((cp = remglob(argv,0)) != NULL) {
  13162.         if (*cp == '\0') {
  13163.             mflag = 0;
  13164.             continue;
  13165.         }
  13166.         if (mflag && confirm(argv[0], cp)) {
  13167.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  13168.             if (!mflag && fromatty) {
  13169.                 ointer = interactive;
  13170.                 interactive = 1;
  13171.                 if (confirm("Continue with", "mdelete")) {
  13172.                     mflag++;
  13173.                 }
  13174.                 interactive = ointer;
  13175.             }
  13176.         }
  13177.     }
  13178.     signal(SIGINT, oldintr);
  13179.     mflag = 0;
  13180.     return(rc);
  13181. }
  13182.  
  13183. /* Get a directory listing of multiple remote files */
  13184.  
  13185. static int
  13186. mls(argc, argv) int argc; char **argv; {
  13187.     sig_t oldintr;
  13188.     int ointer, i;
  13189.     char *cmd, mode[1], *dest;
  13190.     sigtype mcancel();
  13191.     int rc = -1;
  13192.  
  13193.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  13194.       goto usage;
  13195.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  13196.       usage:
  13197.         printf("usage: %s remote-files local-file\n", argv[0]);
  13198.         ftpcode = -1;
  13199.         return(-1);
  13200.     }
  13201.     dest = argv[argc - 1];
  13202.     argv[argc - 1] = NULL;
  13203.     if (strcmp(dest, "-") && *dest != '|')
  13204.       if (!globulize(&dest) ||
  13205.       !confirm("output to local-file:", dest)) {
  13206.       ftpcode = -1;
  13207.       return(-1);
  13208.       }
  13209.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  13210.     mname = argv[0];
  13211.     mflag = 1;
  13212.     oldintr = signal(SIGINT, mcancel);
  13213.     /* Replace with calls to cc_execute() */
  13214.     setjmp(jcancel);
  13215.     for (i = 1; mflag && i < argc-1; ++i) {
  13216.         *mode = (i == 1) ? 'w' : 'a';
  13217.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  13218.         if (!mflag && fromatty) {
  13219.             ointer = interactive;
  13220.             interactive = 1;
  13221.             if (confirm("Continue with", argv[0])) {
  13222.                 mflag ++;
  13223.             }
  13224.             interactive = ointer;
  13225.         }
  13226.     }
  13227.     signal(SIGINT, oldintr);
  13228.     mflag = 0;
  13229.     return(rc);
  13230. }
  13231.  
  13232. static char *
  13233. remglob(argv,doswitch) char *argv[]; int doswitch; {
  13234.     char temp[16];
  13235.     static char buf[CKMAXPATH];
  13236.     static FILE *ftemp = NULL;
  13237.     static char **args;
  13238.     int oldhash;
  13239.     char *cp, *mode;
  13240.  
  13241.     if (!mflag) {
  13242.         if (!doglob) {
  13243.             args = NULL;
  13244.         } else {
  13245.             if (ftemp) {
  13246.                 (void) fclose(ftemp);
  13247.                 ftemp = NULL;
  13248.             }
  13249.         }
  13250.         return(NULL);
  13251.     }
  13252.     if (!doglob) {
  13253.         if (args == NULL)
  13254.       args = argv;
  13255.         if ((cp = *++args) == NULL)
  13256.       args = NULL;
  13257.         return(cp);
  13258.     }
  13259.     if (ftemp == NULL) {
  13260.         (void) strcpy(temp, _PATH_TMP);
  13261. #ifdef MKTEMP
  13262. #ifndef MKSTEMP
  13263.         (void) mktemp(temp);
  13264. #endif /* MKSTEMP */
  13265. #endif /* MKTEMP */
  13266.     verbose = 0;
  13267.         oldhash = hash, hash = 0;
  13268. #ifdef FTP_PROXY
  13269.         if (doswitch) {
  13270.             pswitch(!proxy);
  13271.         }
  13272. #endif /* FTP_PROXY */
  13273.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  13274.       recvrequest ("NLST", temp, *argv, mode, 0);
  13275. #ifdef FTP_PROXY
  13276.         if (doswitch) {
  13277.             pswitch(!proxy);
  13278.         }
  13279. #endif /* FTP_PROXY */
  13280.     hash = oldhash;
  13281.         ftemp = fopen(temp, "r");
  13282.         unlink(temp);
  13283.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  13284.             printf("Can't find list of remote files, oops\n");
  13285.             return(NULL);
  13286.         }
  13287.     }
  13288.     if (fgets(buf, sizeof (buf), ftemp) == NULL) {
  13289.         fclose(ftemp), ftemp = NULL;
  13290.         return(NULL);
  13291.     }
  13292.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  13293.       *cp = '\0';
  13294.     return(buf);
  13295. }
  13296. #endif /* NOT_USED */
  13297.  
  13298. /* remote_files() */
  13299. /*
  13300.    Returns next remote filename on success;
  13301.    NULL on error or no more files with global rfrc set to:
  13302.      -1: Bad argument
  13303.      -2: Server error response to NLST, e.g. file not found
  13304.      -3: No more files
  13305.      -9: Internal error
  13306. */
  13307. static CHAR *
  13308. #ifdef CK_ANSIC
  13309. remote_files(int new_query, CHAR * pattern, int proxy_switch)
  13310. #else /* CK_ANSIC */
  13311. remote_files(new_query, pattern, proxy_switch)
  13312.     int new_query;
  13313.     CHAR * pattern;
  13314.     int proxy_switch;
  13315. #endif /* CK_ANSIC */
  13316. /* remote_files */ {
  13317.     static CHAR temp[CKMAXPATH];
  13318.     static CHAR buf[CKMAXPATH];
  13319.     static FILE *ftemp = NULL;
  13320.     CHAR *cp;
  13321.     int x;
  13322.     int lcs = 0, rcs = 0, xlate = 0;
  13323.  
  13324.     debug(F111,"ftp remote_files",pattern,new_query);
  13325.     rfrc = -1;
  13326.     if (new_query) {
  13327.     if (!pattern) pattern = (CHAR *)"";
  13328.     if (!*pattern) return(NULL);
  13329.     }
  13330.     if (new_query) {
  13331.     if (ftemp) {
  13332.         fclose(ftemp);
  13333.         ftemp = NULL;
  13334. #ifdef OS2
  13335.         if (!ftp_knf)
  13336.           unlink((char *)temp);
  13337. #endif /* OS2 */
  13338.     }
  13339.     }
  13340.     if (ftemp == NULL) {
  13341.         extern char * tempdir;
  13342.     char * p;
  13343.     debug(F110,"ftp remote_files tempdir",tempdir,0);
  13344.     if (tempdir) {
  13345.         p = tempdir;
  13346.     } else {
  13347. #ifdef OS2
  13348. #ifdef NT
  13349.         p = getenv("K95TMP");
  13350. #else
  13351.         p = getenv("K2TMP");
  13352. #endif /* NT */
  13353.         if (!p)
  13354. #endif /* OS2 */
  13355.               p = getenv("CK_TMP");
  13356.         if (!p)
  13357.           p = getenv("TMPDIR");
  13358.         if (!p) p = getenv("TEMP");
  13359.         if (!p) p = getenv("TMP");
  13360. #ifdef OS2ORUNIX
  13361.         if (p) {
  13362.         int len = strlen(p);
  13363.         if (p[len-1] != '/'
  13364. #ifdef OS2
  13365.                     && p[len-1] != '\\'
  13366. #endif /* OS2 */
  13367.                      ) {
  13368.             static char foo[CKMAXPATH];
  13369.             ckstrncpy(foo,p,CKMAXPATH);
  13370.             ckstrncat(foo,"/",CKMAXPATH);
  13371.             p = foo;
  13372.         }
  13373.         } else
  13374. #else /* OS2ORUNIX */
  13375.             if (!p)
  13376. #endif /* OS2ORUNIX */
  13377. #ifdef UNIX                             /* Systems that have a standard */
  13378.         p = "/tmp/";        /* temporary directory... */
  13379. #else
  13380. #ifdef datageneral
  13381.         p = ":TMP:";
  13382. #else
  13383.         p = "";
  13384. #endif /* datageneral */
  13385. #endif /* UNIX */
  13386.     }
  13387.     debug(F110,"ftp remote_files p",p,0);
  13388.     ckmakmsg((char *)temp,CKMAXPATH,p,"ckXXXXXX",NULL,NULL);
  13389.     debug(F110,"ftp remote_files temp",temp,0);
  13390. #ifdef MKTEMP
  13391. #ifdef MKSTEMP
  13392.     mkstemp((char *)temp);
  13393. #else
  13394.     mktemp((char *)temp);
  13395. #endif /* MKSTEMP */
  13396.     /* if no mktemp() the name will just be "ckXXXXXX"... */
  13397. #endif /* MKTEMP */
  13398.  
  13399. #ifdef FTP_PROXY
  13400.         if (proxy_switch) {
  13401.             pswitch(!proxy);
  13402.         }
  13403. #endif /* FTP_PROXY */
  13404.  
  13405. #ifndef NOCSETS
  13406.     xlate = ftp_xla;        /* SET FTP CHARACTER-SET-TRANSLATION */
  13407.     if (xlate) {            /* ON? */
  13408.         lcs = ftp_csl;        /* Local charset */
  13409.         if (lcs < 0) lcs = fcharset;
  13410.         if (lcs < 0) xlate = 0;
  13411.     }
  13412.     if (xlate) {            /* Still ON? */
  13413.         rcs = ftp_csx;        /* Remote (Server) charset */
  13414.         if (rcs < 0) rcs = ftp_csr;
  13415.         if (rcs < 0) xlate = 0;
  13416.     }
  13417. #endif /* NOCSETS */
  13418.  
  13419.         x = recvrequest("NLST",
  13420.             (char *)temp,
  13421.             (char *)pattern,
  13422.             "wb",
  13423.             0,
  13424.             0,
  13425.             NULL,
  13426.             xlate,
  13427.             lcs,
  13428.             rcs
  13429.             );
  13430.     if (x < 0) {
  13431.         rfrc = -2;
  13432.         return(NULL);
  13433.     }
  13434. #ifdef FTP_PROXY
  13435.         if (proxy_switch) {
  13436.             pswitch(!proxy);
  13437.         }
  13438. #endif /* FTP_PROXY */
  13439.         ftemp = fopen((char *)temp, "r");
  13440. #ifndef OS2
  13441.     if (!ftp_knf)
  13442.       unlink((char *)temp);
  13443. #endif /* OS2 */
  13444.         if (!ftemp) {
  13445.         debug(F110,"ftp remote_files open fail",temp,0);
  13446.         if ((!dpyactive || ftp_deb))
  13447.           printf("?Can't find list of remote files, oops\n");
  13448.         rfrc = -9;
  13449.             return(NULL);
  13450.         }
  13451.     }
  13452.     if (fgets((char *)buf, sizeof (buf), ftemp) == NULL) {
  13453.         fclose(ftemp);
  13454.     ftemp = NULL;
  13455. #ifdef OS2
  13456.     if (!ftp_knf)
  13457.       unlink((char *)temp);
  13458. #endif /* OS2 */
  13459.     if (ftp_knf) {
  13460.         printf("(Temporary file %s NOT deleted)\n",(char *)temp);
  13461.     }
  13462.     rfrc = -3;
  13463.         return(NULL);
  13464.     }
  13465.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  13466.       *cp = '\0';
  13467.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  13468.       *cp = '\0';
  13469.     rfrc = 0;
  13470.     return(buf);
  13471. }
  13472.  
  13473. /* N O T  P O R T A B L E !!! */
  13474.  
  13475. #if (SIZEOF_SHORT == 4)
  13476. typedef unsigned short ftp_uint32;
  13477. typedef short ftp_int32;
  13478. #else
  13479. #if (SIZEOF_INT == 4)
  13480. typedef unsigned int ftp_uint32;
  13481. typedef int ftp_int32;
  13482. #else
  13483. #if (SIZEOF_LONG == 4)
  13484. typedef ULONG ftp_uint32;
  13485. typedef long ftp_int32;
  13486. #endif
  13487. #endif
  13488. #endif
  13489.  
  13490. /* Perhaps use these in general, certainly use them for GSSAPI */
  13491.  
  13492. #ifndef looping_write
  13493. #define ftp_int32 int
  13494. #define ftp_uint32 unsigned int
  13495. static int
  13496. looping_write(fd, buf, len)
  13497.     int fd;
  13498.     register CONST char *buf;
  13499.     int len;
  13500. {
  13501.     int cc;
  13502.     register int wrlen = len;
  13503.     do {
  13504.     cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  13505.     if (cc < 0) {
  13506.         if (errno == EINTR)
  13507.           continue;
  13508.         return(cc);
  13509.     } else {
  13510.         buf += cc;
  13511.         wrlen -= cc;
  13512.     }
  13513.     } while (wrlen > 0);
  13514.     return(len);
  13515. }
  13516. #endif
  13517. #ifndef looping_read
  13518. static int
  13519. looping_read(fd, buf, len)
  13520.     int fd;
  13521.     register char *buf;
  13522.     register int len;
  13523. {
  13524.     int cc, len2 = 0;
  13525.  
  13526.     do {
  13527.     cc = recv(fd, (char *)buf, len,0);
  13528.     if (cc < 0) {
  13529.         if (errno == EINTR)
  13530.           continue;
  13531.         return(cc);            /* errno is already set */
  13532.     } else if (cc == 0) {
  13533.         return(len2);
  13534.     } else {
  13535.         buf += cc;
  13536.         len2 += cc;
  13537.         len -= cc;
  13538.     }
  13539.     } while (len > 0);
  13540.     return(len2);
  13541. }
  13542. #endif /* looping_read */
  13543.  
  13544. #define ERR -2
  13545.  
  13546. #ifdef COMMENT
  13547. static
  13548. secure_putbyte(fd, c) int fd; CHAR c; {
  13549.     int ret;
  13550.  
  13551.     ucbuf[nout++] = c;
  13552.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  13553.         nout = 0;
  13554.         if (ftp_dpl == FPL_CLR)
  13555.       ret = send(fd, (SENDARG2TYPE)ucbuf,
  13556.              (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  13557.         else
  13558.       ret = secure_putbuf(fd,
  13559.                   ucbuf,
  13560.                   (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  13561.                   );
  13562.         return(ret?ret:c);
  13563.     }
  13564.     return(c);
  13565. }
  13566. #endif /* COMMENT */
  13567.  
  13568. /* returns:
  13569.  *     0  on success
  13570.  *    -1  on error (errno set)
  13571.  *    -2  on security error
  13572.  */
  13573. static int
  13574. secure_flush(fd) int fd; {
  13575.     int rc = 0;
  13576.     int len = 0;
  13577.  
  13578.     if (nout > 0) {
  13579.     len = nout;
  13580.         if (ftp_dpl == FPL_CLR) {
  13581.         rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  13582.         nout = 0;
  13583.         goto xflush;
  13584.     } else {
  13585.         rc = secure_putbuf(fd, ucbuf, nout);
  13586.         if (rc)
  13587.           goto xflush;
  13588.     }
  13589.     }
  13590.     rc = (ftp_dpl == FPL_CLR) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  13591.  
  13592.   xflush:
  13593.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  13594.     spktl = len;
  13595.     ftscreen(SCR_PT,'D',spackets,NULL);
  13596.     }
  13597.     return(rc);
  13598. }
  13599.  
  13600. #ifdef COMMENT                /* (not used) */
  13601. /* returns:
  13602.  *    c>=0  on success
  13603.  *    -1    on error
  13604.  *    -2    on security error
  13605.  */
  13606. static int
  13607. #ifdef CK_ANSIC
  13608. secure_putc(char c, int fd)
  13609. #else
  13610. secure_putc(c, fd) char c; int fd;
  13611. #endif /* CK_ANSIC */
  13612. /* secure_putc */ {
  13613.     return(secure_putbyte(fd, (CHAR) c));
  13614. }
  13615. #endif /* COMMENT */
  13616.  
  13617. /* returns:
  13618.  *    nbyte on success
  13619.  *    -1  on error (errno set)
  13620.  *    -2  on security error
  13621.  */
  13622. static int
  13623. #ifdef CK_ANSIC
  13624. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  13625. #else
  13626. secure_write(fd, buf, nbyte)
  13627.     int fd;
  13628.     CHAR * buf;
  13629.     unsigned int nbyte;
  13630. #endif /* CK_ANSIC */
  13631. {
  13632.     int ret;
  13633.  
  13634.     if (ftp_dpl == FPL_CLR) {
  13635.         if (nout > 0) {
  13636.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  13637.           return(ret);
  13638.             nout = 0;
  13639.         }
  13640.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  13641.     } else {
  13642.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  13643.         int bsent = 0;
  13644.  
  13645.         while (bsent < nbyte) {
  13646.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  13647.             (ucbuflen - nout) : (nbyte - bsent));
  13648.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  13649.             nout += b2cp;
  13650.             bsent += b2cp;
  13651.  
  13652.             if (nout == ucbuflen) {
  13653.                 nout = 0;
  13654.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  13655.                 if (ret < 0)
  13656.           return(ret);
  13657.             }
  13658.         }
  13659.         return(bsent);
  13660.     }
  13661. }
  13662.  
  13663. /* returns:
  13664.  *     0  on success
  13665.  *    -1  on error (errno set)
  13666.  *    -2  on security error
  13667.  */
  13668. static int
  13669. #ifdef CK_ANSIC
  13670. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  13671. #else
  13672. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  13673. #endif /* CK_ANSIC */
  13674. {
  13675.     static char *outbuf = NULL;        /* output ciphertext */
  13676. #ifdef FTP_SECURITY
  13677.     static unsigned int bufsize = 0;    /* size of outbuf */
  13678. #endif /* FTP_SECURITY */
  13679.     ftp_int32 length   = 0;
  13680.     ftp_uint32 net_len = 0;
  13681.  
  13682.     /* Other auth types go here ... */
  13683. #ifdef CK_SSL
  13684.     if (ssl_ftp_data_active_flag) {
  13685.         int count, error;
  13686.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  13687.         error = SSL_get_error(ssl_ftp_data_con,count);
  13688.         switch (error) {
  13689.       case SSL_ERROR_NONE:
  13690.             return(0);
  13691.       case SSL_ERROR_WANT_WRITE:
  13692.       case SSL_ERROR_WANT_READ:
  13693.       case SSL_ERROR_SYSCALL:
  13694. #ifdef NT
  13695.         {
  13696.         int gle = GetLastError();
  13697.         }
  13698. #endif /* NT */
  13699.       case SSL_ERROR_WANT_X509_LOOKUP:
  13700.       case SSL_ERROR_SSL:
  13701.       case SSL_ERROR_ZERO_RETURN:
  13702.       default:
  13703.             SSL_shutdown(ssl_ftp_data_con);
  13704.             SSL_free(ssl_ftp_data_con);
  13705.             ssl_ftp_data_active_flag = 0;
  13706.             ssl_ftp_data_con = NULL;
  13707. #ifdef TCPIPLIB
  13708.             socket_close(data);
  13709. #else /* TCPIPLIB */
  13710. #ifdef USE_SHUTDOWN
  13711.             shutdown(data, 1+1);
  13712. #endif /* USE_SHUTDOWN */
  13713.             close(data);
  13714. #endif /* TCPIPLIB */
  13715.             data = -1;
  13716.         globaldin = data;
  13717.             return(-1);
  13718.         }
  13719.         return(-1);
  13720.     }
  13721. #endif /* CK_SSL */
  13722.  
  13723. #ifdef FTP_SRP
  13724.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  13725.         if (bufsize < nbyte + FUDGE_FACTOR) {
  13726.             if (outbuf?
  13727.         (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  13728.         (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  13729.                 bufsize = nbyte + FUDGE_FACTOR;
  13730.             } else {
  13731.                 bufsize = 0;
  13732.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  13733.                 return(ERR);
  13734.             }
  13735.         }
  13736.         if ((length =
  13737.          srp_encode(ftp_dpl == FPL_PRV,
  13738.             (CHAR *) buf,
  13739.             (CHAR *) outbuf,
  13740.             nbyte
  13741.             )
  13742.          ) < 0) {
  13743.             secure_error ("srp_encode failed");
  13744.             return ERR;
  13745.         }
  13746.     }
  13747. #endif /* FTP_SRP */
  13748. #ifdef FTP_KRB4
  13749.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  13750.         struct sockaddr_in myaddr, hisaddr;
  13751.         GSOCKNAME_T len;
  13752.         len = sizeof(myaddr);
  13753.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  13754.             secure_error("secure_putbuf: getsockname failed");
  13755.             return(ERR);
  13756.         }
  13757.         len = sizeof(hisaddr);
  13758.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  13759.             secure_error("secure_putbuf: getpeername failed");
  13760.             return(ERR);
  13761.         }
  13762.         if (bufsize < nbyte + FUDGE_FACTOR) {
  13763.             if (outbuf ?
  13764.         (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  13765.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  13766.         bufsize = nbyte + FUDGE_FACTOR;
  13767.             } else {
  13768.                 bufsize = 0;
  13769.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  13770.                 return(ERR);
  13771.             }
  13772.         }
  13773.         if (ftp_dpl == FPL_PRV) {
  13774.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  13775.                  ftp_sched,
  13776. #ifdef KRB524
  13777.                  ftp_cred.session,
  13778. #else /* KRB524 */
  13779.                  &ftp_cred.session,
  13780. #endif /* KRB524 */
  13781.                  &myaddr,
  13782.                  &hisaddr
  13783.                  );
  13784.         } else {
  13785.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  13786. #ifdef KRB524
  13787.                  ftp_cred.session,
  13788. #else /* KRB524 */
  13789.                  &ftp_cred.session,
  13790. #endif /* KRB524 */
  13791.                  &myaddr,
  13792.                  &hisaddr
  13793.                  );
  13794.         }
  13795.         if (length == -1) {
  13796.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  13797.              ftp_dpl == FPL_PRV ? "priv" : "safe");
  13798.             return(ERR);
  13799.         }
  13800.     }
  13801. #endif /* FTP_KRB4 */
  13802. #ifdef FTP_GSSAPI
  13803.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  13804.         gss_buffer_desc in_buf, out_buf;
  13805.         OM_uint32 maj_stat, min_stat;
  13806.         int conf_state;
  13807.  
  13808.         in_buf.value = buf;
  13809.         in_buf.length = nbyte;
  13810.         maj_stat = gss_seal(&min_stat, gcontext,
  13811.                 (ftp_dpl == FPL_PRV), /* confidential */
  13812.                 GSS_C_QOP_DEFAULT,
  13813.                 &in_buf,
  13814.                 &conf_state,
  13815.                 &out_buf
  13816.                 );
  13817.         if (maj_stat != GSS_S_COMPLETE) {
  13818.             /* generally need to deal */
  13819.             /* ie. should loop, but for now just fail */
  13820.             user_gss_error(maj_stat, min_stat,
  13821.                ftp_dpl == FPL_PRV?
  13822.                "GSSAPI seal failed":
  13823.                "GSSAPI sign failed");
  13824.             return(ERR);
  13825.         }
  13826.         if (bufsize < out_buf.length) {
  13827.             if (outbuf ?
  13828.         (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  13829.         (outbuf = malloc((unsigned) out_buf.length))) {
  13830.                 bufsize = out_buf.length;
  13831.         } else {
  13832.         bufsize = 0;
  13833.         secure_error("%s (in malloc of PROT buffer)",
  13834.                  ck_errstr());
  13835.         return(ERR);
  13836.         }
  13837.         }
  13838.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  13839.         gss_release_buffer(&min_stat, &out_buf);
  13840.     }
  13841. #endif /* FTP_GSSAPI */
  13842.     net_len = htonl((ULONG) length);
  13843.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  13844.       return(-1);
  13845.     if (looping_write(fd, outbuf, length) != length)
  13846.       return(-1);
  13847.     return(0);
  13848. }
  13849.  
  13850. static int
  13851. secure_getbyte(fd) int fd; {
  13852.     /* number of chars in ucbuf, pointer into ucbuf */
  13853.     static unsigned int nin = 0, bufp = 0;
  13854.     int kerror;
  13855.     ftp_uint32 length;
  13856.  
  13857.     if (nin == 0) {
  13858.     if (iscanceled())
  13859.       return(0);
  13860. #ifdef CK_SSL
  13861.         if (ssl_ftp_data_active_flag) {
  13862.             int count, error;
  13863.             count = SSL_read(ssl_ftp_data_con, ucbuf, sizeof(ucbuf));
  13864.             error = SSL_get_error(ssl_ftp_data_con,count);
  13865.             switch (error) {
  13866.           case SSL_ERROR_NONE:
  13867.                 nin = bufp = count;
  13868.         rpackets++;
  13869.         pktnum++;
  13870.         if (fdispla != XYFD_B) {
  13871.             rpktl = count;
  13872.             ftscreen(SCR_PT,'D',rpackets,NULL);
  13873.         }
  13874.                 break;
  13875.           case SSL_ERROR_WANT_WRITE:
  13876.           case SSL_ERROR_WANT_READ:
  13877.           case SSL_ERROR_SYSCALL:
  13878. #ifdef NT
  13879.                 {
  13880.             int gle = GetLastError();
  13881.                 }
  13882. #endif /* NT */
  13883.           case SSL_ERROR_WANT_X509_LOOKUP:
  13884.           case SSL_ERROR_SSL:
  13885.           case SSL_ERROR_ZERO_RETURN:
  13886.           default:
  13887.                 nin = bufp = count = 0;
  13888.                 SSL_shutdown(ssl_ftp_data_con);
  13889.                 SSL_free(ssl_ftp_data_con);
  13890.                 ssl_ftp_data_active_flag = 0;
  13891.                 ssl_ftp_data_con = NULL;
  13892. #ifdef TCPIPLIB
  13893.                 socket_close(data);
  13894. #else /* TCPIPLIB */
  13895. #ifdef USE_SHUTDOWN
  13896.                 shutdown(data, 1+1);
  13897. #endif /* USE_SHUTDOWN */
  13898.                 close(data);
  13899. #endif /* TCPIPLIB */
  13900.                 data = -1;
  13901.         globaldin = data;
  13902.                 break;
  13903.             }
  13904.         } else
  13905. #endif /* CK_SSL */
  13906.       {
  13907.           kerror = looping_read(fd, (char *)&length, sizeof(length));
  13908.           if (kerror != sizeof(length)) {
  13909.           secure_error("Couldn't read PROT buffer length: %d/%s",
  13910.                    kerror,
  13911.                    kerror == -1 ? ck_errstr()
  13912.                    : "premature EOF"
  13913.                    );
  13914.           return(ERR);
  13915.           }
  13916.           debug(F101,"secure_getbyte length","",length);
  13917.           debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  13918.  
  13919.               length = (ULONG) ntohl(length);
  13920.           if (length > maxbuf) {
  13921.           secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  13922.                    length,
  13923.                    maxbuf
  13924.                    );
  13925.           return(ERR);
  13926.           }
  13927.           if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  13928.           secure_error("Couldn't read %u byte PROT buffer: %s",
  13929.                    length,
  13930.                    kerror == -1 ? ck_errstr() : "premature EOF"
  13931.                    );
  13932.           return(ERR);
  13933.           }
  13934.           /* Other auth types go here ... */
  13935. #ifdef FTP_SRP
  13936.           if (strcmp(auth_type, "SRP") == 0) {
  13937.           if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  13938.                         (CHAR *) ucbuf,
  13939.                         ucbuf,
  13940.                         length
  13941.                         )
  13942.                ) == -1) {
  13943.               secure_error ("srp_encode failed" );
  13944.               return ERR;
  13945.           }
  13946.           }
  13947. #endif /* FTP_SRP */
  13948. #ifdef FTP_KRB4
  13949.           if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  13950.           struct sockaddr_in myaddr, hisaddr;
  13951.           GSOCKNAME_T len;
  13952.           len = sizeof(myaddr);
  13953.           if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  13954.               secure_error("secure_putbuf: getsockname failed");
  13955.               return(ERR);
  13956.           }
  13957.           len = sizeof(hisaddr);
  13958.           if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  13959.               secure_error("secure_putbuf: getpeername failed");
  13960.               return(ERR);
  13961.           }
  13962.           if (ftp_dpl) {
  13963.               kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  13964. #ifdef KRB524
  13965.                        ftp_cred.session,
  13966. #else /* KRB524 */
  13967.                        &ftp_cred.session,
  13968. #endif /* KRB524 */
  13969.                        &hisaddr, &myaddr, &ftp_msg_data);
  13970.           } else {
  13971.               kerror = krb_rd_safe(ucbuf, length,
  13972. #ifdef KRB524
  13973.                        ftp_cred.session,
  13974. #else /* KRB524 */
  13975.                        &ftp_cred.session,
  13976. #endif /* KRB524 */
  13977.                        &hisaddr, &myaddr, &ftp_msg_data);
  13978.           }
  13979.           if (kerror) {
  13980.               secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  13981.                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  13982.                    krb_get_err_text(kerror));
  13983.               return(ERR);
  13984.           }
  13985.           memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  13986.           nin = bufp = ftp_msg_data.app_length;
  13987.           }
  13988. #endif /* FTP_KRB4 */
  13989. #ifdef FTP_GSSAPI
  13990.           if (strcmp(auth_type, "GSSAPI") == 0) {
  13991.           gss_buffer_desc xmit_buf, msg_buf;
  13992.           OM_uint32 maj_stat, min_stat;
  13993.           int conf_state;
  13994.  
  13995.           xmit_buf.value = ucbuf;
  13996.           xmit_buf.length = length;
  13997.           conf_state = (ftp_dpl == FPL_PRV);
  13998.           /* decrypt/verify the message */
  13999.           maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  14000.                     &msg_buf, &conf_state, NULL);
  14001.           if (maj_stat != GSS_S_COMPLETE) {
  14002.               user_gss_error(maj_stat, min_stat,
  14003.                      (ftp_dpl == FPL_PRV)?
  14004.                      "failed unsealing ENC message":
  14005.                      "failed unsealing MIC message");
  14006.               return ERR;
  14007.           }
  14008.           memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  14009.           gss_release_buffer(&min_stat, &msg_buf);
  14010.           }
  14011. #endif /* FTP_GSSAPI */
  14012.           /* Other auth types go here ... */
  14013.       }
  14014.     }
  14015.     if (nin == 0)
  14016.       return(EOF);
  14017.     else
  14018.       return(ucbuf[bufp - nin--]);
  14019. }
  14020.  
  14021. /* returns:
  14022.  *    c>=0 on success
  14023.  *    -1   on EOF
  14024.  *    -2   on security error
  14025.  */
  14026. static int
  14027. secure_getc(fd) int fd; {
  14028.     if (ftp_dpl == FPL_CLR) {
  14029.         static unsigned int nin = 0, bufp = 0;
  14030.         if (nin == 0) {
  14031.         if (iscanceled())
  14032.           return(-9);
  14033.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  14034.             if (nin <= 0) {
  14035.         debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  14036.         debug(F101,"secure_getc returns EOF","",EOF);
  14037.                 nin = bufp = 0;
  14038.                 return(EOF);
  14039.             }
  14040.         debug(F101,"ftp secure_getc recv","",nin);
  14041.         hexdump("ftp secure_getc recv",ucbuf,16);
  14042.         rpackets++;
  14043.         pktnum++;
  14044.         if (fdispla != XYFD_B) {
  14045.         rpktl = nin;
  14046.         ftscreen(SCR_PT,'D',rpackets,NULL);
  14047.         }
  14048.         }
  14049.         return(ucbuf[bufp - nin--]);
  14050.     } else
  14051.       return(secure_getbyte(fd));
  14052. }
  14053.  
  14054. /* returns:
  14055.  *     n>0  on success (n == # of bytes read)
  14056.  *     0  on EOF
  14057.  *    -1  on error (errno set), only for FPL_CLR
  14058.  *    -2  on security error
  14059.  */
  14060. static int
  14061. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  14062.     static int c = 0;
  14063.     int i;
  14064.  
  14065.     if (c == EOF)
  14066.       return(c = 0);
  14067.     for (i = 0; nbyte > 0; nbyte--) {
  14068.     c = secure_getc(fd);
  14069.     switch (c) {
  14070.       case -9:            /* Canceled from keyboard */
  14071.         debug(F101,"ftp secure_read interrupted","",c);
  14072.         return(0);
  14073.       case ERR:
  14074.         debug(F101,"ftp secure_read error","",c);
  14075.         return(c);
  14076.       case EOF:
  14077.         debug(F101,"ftp secure_read EOF","",c);
  14078.         if (!i)
  14079.           c = 0;
  14080.         return(i);
  14081.       default:
  14082.         buf[i++] = c;
  14083.     }
  14084.     }
  14085.     return(i);
  14086. }
  14087.  
  14088. #ifdef USE_RUSERPASS
  14089. /* BEGIN_RUSERPASS
  14090.  *
  14091.  * Copyright (c) 1985 Regents of the University of California.
  14092.  * All rights reserved.
  14093.  *
  14094.  * Redistribution and use in source and binary forms, with or without
  14095.  * modification, are permitted provided that the following conditions
  14096.  * are met:
  14097.  * 1. Redistributions of source code must retain the above copyright
  14098.  *    notice, this list of conditions and the following disclaimer.
  14099.  * 2. Redistributions in binary form must reproduce the above copyright
  14100.  *    notice, this list of conditions and the following disclaimer in the
  14101.  *    documentation and/or other materials provided with the distribution.
  14102.  * 3. All advertising materials mentioning features or use of this software
  14103.  *    must display the following acknowledgement:
  14104.  *    This product includes software developed by the University of
  14105.  *    California, Berkeley and its contributors.
  14106.  * 4. Neither the name of the University nor the names of its contributors
  14107.  *    may be used to endorse or promote products derived from this software
  14108.  *    without specific prior written permission.
  14109.  *
  14110.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  14111.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14112.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14113.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  14114.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  14115.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  14116.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  14117.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14118.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  14119.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  14120.  * SUCH DAMAGE.
  14121.  */
  14122.  
  14123. #ifndef lint
  14124. static char sccsid[] = "@(#)ruserpass.c    5.3 (Berkeley) 3/1/91";
  14125. #endif /* not lint */
  14126.  
  14127. #include <sys/types.h>
  14128. #include <stdio.h>
  14129. #include <string.h>
  14130. #ifdef HAVE_STDLIB_H
  14131. #include <stdlib.h>
  14132. #endif
  14133. #include <ctype.h>
  14134. #include <sys/stat.h>
  14135. #include <errno.h>
  14136.  
  14137. #ifndef MAXHOSTNAMELEN
  14138. #define MAXHOSTNAMELEN 64
  14139. #endif
  14140.  
  14141. char * renvlook();
  14142. static FILE * cfile;
  14143.  
  14144. #define    DEFAULT    1
  14145. #define    LOGIN    2
  14146. #define    PASSWD    3
  14147. #define    ACCOUNT 4
  14148. #define MACDEF  5
  14149. #define    ID    10
  14150. #define    MACH    11
  14151.  
  14152. static char tokval[100];
  14153.  
  14154. static struct toktab {
  14155.     char *tokstr;
  14156.     int tval;
  14157. } toktab[]= {
  14158.     "default",    DEFAULT,
  14159.     "login",    LOGIN,
  14160.     "password",    PASSWD,
  14161.     "passwd",    PASSWD,
  14162.     "account",    ACCOUNT,
  14163.     "machine",    MACH,
  14164.     "macdef",    MACDEF,
  14165.     0,        0
  14166. };
  14167.  
  14168. static int
  14169. token() {
  14170.     char *cp;
  14171.     int c;
  14172.     struct toktab *t;
  14173.  
  14174.     if (feof(cfile))
  14175.       return(0);
  14176.     while ((c = getc(cfile)) != EOF &&
  14177.        (c == '\n' || c == '\t' || c == ' ' || c == ','))
  14178.       continue;
  14179.     if (c == EOF)
  14180.       return(0);
  14181.     cp = tokval;
  14182.     if (c == '"') {
  14183.     while ((c = getc(cfile)) != EOF && c != '"') {
  14184.         if (c == '\\')
  14185.           c = getc(cfile);
  14186.         *cp++ = c;
  14187.     }
  14188.     } else {
  14189.     *cp++ = c;
  14190.     while ((c = getc(cfile)) != EOF
  14191.            && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  14192.         if (c == '\\')
  14193.           c = getc(cfile);
  14194.         *cp++ = c;
  14195.     }
  14196.     }
  14197.     *cp = 0;
  14198.     if (tokval[0] == 0)
  14199.       return(0);
  14200.     for (t = toktab; t->tokstr; t++)
  14201.       if (!strcmp(t->tokstr, tokval))
  14202.     return(t->tval);
  14203.     return(ID);
  14204. }
  14205.  
  14206. ruserpass(host, aname, apass, aacct)
  14207.     char *host, **aname, **apass, **aacct;
  14208. {
  14209.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  14210.     char myname[MAXHOSTNAMELEN], *mydomain;
  14211.     int t, i, c, usedefault = 0;
  14212. #ifdef NT
  14213.     struct _stat stb;
  14214. #else /* NT */
  14215.     struct stat stb;
  14216. #endif /* NT */
  14217.  
  14218.     hdir = getenv("HOME");
  14219.     if (hdir == NULL)
  14220.         hdir = ".";
  14221.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  14222.     cfile = fopen(buf, "r");
  14223.     if (cfile == NULL) {
  14224.         if (errno != ENOENT)
  14225.       perror(buf);
  14226.         return(0);
  14227.     }
  14228.     if (gethostname(myname, sizeof(myname)) < 0)
  14229.       myname[0] = '\0';
  14230.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  14231.       mydomain = "";
  14232.  
  14233.   next:
  14234.     while ((t = token())) switch(t) {
  14235.  
  14236.       case DEFAULT:
  14237.         usedefault = 1;
  14238.         /* FALL THROUGH */
  14239.  
  14240.       case MACH:
  14241.         if (!usedefault) {
  14242.             if (token() != ID)
  14243.           continue;
  14244.             /*
  14245.              * Allow match either for user's input host name
  14246.              * or official hostname.  Also allow match of
  14247.              * incompletely-specified host in local domain.
  14248.              */
  14249.             if (ckstrcmp(host, tokval,-1,1) == 0)
  14250.           goto match;
  14251.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  14252.           goto match;
  14253.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  14254.         ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  14255.         ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  14256.         tokval[tmp - ftp_host] == '\0')
  14257.           goto match;
  14258.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  14259.         ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  14260.         ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  14261.         tokval[tmp - host] == '\0')
  14262.           goto match;
  14263.             continue;
  14264.         }
  14265.  
  14266.       match:
  14267.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  14268.  
  14269.       case LOGIN:
  14270.             if (token())
  14271.           if (*aname == 0) {
  14272.           *aname = malloc((unsigned) strlen(tokval) + 1);
  14273.           strcpy(*aname, tokval);      /* safe */
  14274.           } else {
  14275.           if (strcmp(*aname, tokval))
  14276.             goto next;
  14277.           }
  14278.             break;
  14279.       case PASSWD:
  14280.             if (strcmp(*aname, "anonymous") &&
  14281.         fstat(fileno(cfile), &stb) >= 0 &&
  14282.         (stb.st_mode & 077) != 0) {
  14283.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  14284.                 fprintf(stderr, "Remove password or correct mode.\n");
  14285.                 goto bad;
  14286.             }
  14287.             if (token() && *apass == 0) {
  14288.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  14289.                 strcpy(*apass, tokval);          /* safe */
  14290.             }
  14291.             break;
  14292.       case ACCOUNT:
  14293.             if (fstat(fileno(cfile), &stb) >= 0
  14294.         && (stb.st_mode & 077) != 0) {
  14295.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  14296.                 fprintf(stderr, "Remove account or correct mode.\n");
  14297.                 goto bad;
  14298.             }
  14299.             if (token() && *aacct == 0) {
  14300.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  14301.                 strcpy(*aacct, tokval);          /* safe */
  14302.             }
  14303.             break;
  14304.  
  14305.       default:
  14306.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  14307.             break;
  14308.         }
  14309.         goto done;
  14310.     }
  14311.  
  14312.   done:
  14313.     fclose(cfile);
  14314.     return(0);
  14315.  
  14316.   bad:
  14317.     fclose(cfile);
  14318.     return(-1);
  14319. }
  14320. #endif /* USE_RUSERPASS */
  14321.  
  14322. static char *radixN =
  14323.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  14324.  
  14325. static char pad = '=';
  14326.  
  14327. static int
  14328. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  14329.     CHAR inbuf[], outbuf[];
  14330.     int inlen, *outlen, decode;
  14331. {
  14332.     int i, j, D = 0;
  14333.     char *p;
  14334.     CHAR c = NUL;
  14335.  
  14336.     if (decode) {
  14337.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  14338.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  14339.           return(1);
  14340.             D = p - radixN;
  14341.             switch (i&3) {
  14342.           case 0:
  14343.                 outbuf[j] = D<<2;
  14344.                 break;
  14345.           case 1:
  14346.                 outbuf[j++] |= D>>4;
  14347.                 outbuf[j] = (D&15)<<4;
  14348.                 break;
  14349.           case 2:
  14350.                 outbuf[j++] |= D>>2;
  14351.                 outbuf[j] = (D&3)<<6;
  14352.                 break;
  14353.           case 3:
  14354.                 outbuf[j++] |= D;
  14355.             }
  14356.             if (j == *outlen)
  14357.           return(4);
  14358.         }
  14359.         switch (i&3) {
  14360.       case 1: return(3);
  14361.       case 2: if (D&15) return(3);
  14362.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  14363.             break;
  14364.       case 3: if (D&3) return(3);
  14365.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  14366.         }
  14367.         *outlen = j;
  14368.     } else {
  14369.         for (i = 0, j = 0; i < inlen; i++) {
  14370.             switch (i%3) {
  14371.           case 0:
  14372.                 outbuf[j++] = radixN[inbuf[i]>>2];
  14373.                 c = (inbuf[i]&3)<<4;
  14374.                 break;
  14375.           case 1:
  14376.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  14377.                 c = (inbuf[i]&15)<<2;
  14378.                 break;
  14379.           case 2:
  14380.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  14381.                 outbuf[j++] = radixN[inbuf[i]&63];
  14382.                 c = 0;
  14383.             }
  14384.             if (j == *outlen)
  14385.           return(4);
  14386.         }
  14387.         if (i%3) outbuf[j++] = radixN[c];
  14388.         switch (i%3) {
  14389.       case 1: outbuf[j++] = pad;
  14390.       case 2: outbuf[j++] = pad;
  14391.         }
  14392.         outbuf[*outlen = j] = '\0';
  14393.     }
  14394.     return(0);
  14395. }
  14396.  
  14397. static char *
  14398. radix_error(e) int e;
  14399. {
  14400.     switch (e) {
  14401.       case 0:  return("Success");
  14402.       case 1:  return("Bad character in encoding");
  14403.       case 2:  return("Encoding not properly padded");
  14404.       case 3:  return("Decoded # of bits not a multiple of 8");
  14405.       case 4:  return("Output buffer too small");
  14406.       default: return("Unknown error");
  14407.     }
  14408. }
  14409. /* END_RUSERPASS */
  14410.  
  14411. #ifdef FTP_SRP
  14412. /*---------------------------------------------------------------------------+
  14413.  |                                                                           |
  14414.  |   Package: srpftp                                                         |
  14415.  |   Author: Eugene Jhong                                                    |
  14416.  |                                                                           |
  14417.  +---------------------------------------------------------------------------*/
  14418.  
  14419. /*
  14420.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  14421.  * All Rights Reserved.
  14422.  *
  14423.  * Permission is hereby granted, free of charge, to any person obtaining
  14424.  * a copy of this software and associated documentation files (the
  14425.  * "Software"), to deal in the Software without restriction, including
  14426.  * without limitation the rights to use, copy, modify, merge, publish,
  14427.  * distribute, sublicense, and/or sell copies of the Software, and to
  14428.  * permit persons to whom the Software is furnished to do so, subject to
  14429.  * the following conditions:
  14430.  *
  14431.  * The above copyright notice and this permission notice shall be
  14432.  * included in all copies or substantial portions of the Software.
  14433.  *
  14434.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14435.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14436.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  14437.  *
  14438.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  14439.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  14440.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  14441.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  14442.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14443.  *
  14444.  * In addition, the following conditions apply:
  14445.  *
  14446.  * 1. Any software that incorporates the SRP authentication technology
  14447.  *    must display the following acknowlegment:
  14448.  *    "This product uses the 'Secure Remote Password' cryptographic
  14449.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  14450.  *
  14451.  * 2. Any software that incorporates all or part of the SRP distribution
  14452.  *    itself must also display the following acknowledgment:
  14453.  *    "This product includes software developed by Tom Wu and Eugene
  14454.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  14455.  *
  14456.  * 3. Redistributions in source or binary form must retain an intact copy
  14457.  *    of this copyright notice and list of conditions.
  14458.  */
  14459.  
  14460. #define SRP_PROT_VERSION     1
  14461.  
  14462. #ifdef CK_ENCRYPTION
  14463. #define SRP_DEFAULT_CIPHER     CIPHER_ID_CAST5_CBC
  14464. #else
  14465. #define SRP_DEFAULT_CIPHER     CIPHER_ID_NONE
  14466. #endif /* CK_ENCRYPTION */
  14467.  
  14468. #define SRP_DEFAULT_HASH     HASH_ID_SHA
  14469.  
  14470. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  14471. CHAR srp_pref_hash = HASH_ID_SHA;
  14472.  
  14473. static struct t_client *tc = NULL;
  14474. static CHAR *skey = NULL;
  14475. static krypto_context *incrypt = NULL;
  14476. static krypto_context *outcrypt = NULL;
  14477.  
  14478. typedef unsigned int srp_uint32;
  14479.  
  14480. /*--------------------------------------------------------------+
  14481.  | srp_selcipher: select cipher                                 |
  14482.  +--------------------------------------------------------------*/
  14483. static int
  14484. srp_selcipher (cname) char *cname; {
  14485.     cipher_desc *cd;
  14486.  
  14487.     if (!(cd = cipher_getdescbyname (cname))) {
  14488.         int i;
  14489.         CHAR *list = cipher_getlist ();
  14490.  
  14491.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  14492.         for (i = 0; i < strlen (list); i++)
  14493.       fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  14494.         fprintf (stderr, "\n");
  14495.         return -1;
  14496.     }
  14497.     srp_pref_cipher = cd->id;
  14498.     return 0;
  14499. }
  14500.  
  14501. /*--------------------------------------------------------------+
  14502.  | srp_selhash: select hash                                     |
  14503.  +--------------------------------------------------------------*/
  14504. static int
  14505. srp_selhash (hname) char *hname; {
  14506.     hash_desc *hd;
  14507.  
  14508.     if (!(hd = hash_getdescbyname (hname))) {
  14509.         int i;
  14510.         CHAR *list = hash_getlist ();
  14511.  
  14512.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  14513.         for (i = 0; i < strlen (list); i++)
  14514.       fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  14515.         fprintf (stderr, "\n");
  14516.         return -1;
  14517.     }
  14518.     srp_pref_hash = hd->id;
  14519.     return 0;
  14520. }
  14521.  
  14522. /*--------------------------------------------------------------+
  14523.  | srp_userpass: get username and password                      |
  14524.  +--------------------------------------------------------------*/
  14525. static int
  14526. srp_userpass (host) char *host; {
  14527.     char tmp[BUFSIZ], prompt[256];
  14528.     char *user;
  14529.  
  14530.     user = NULL;
  14531. #ifdef USE_RUSERPASS
  14532.     ruserpass (host, &user, &srp_pass, &srp_acct);
  14533. #endif /* USE_RUSERPASS */
  14534.  
  14535.     while (user == NULL)     {
  14536.         char *myname;
  14537.         myname = whoami();
  14538.     if (!myname) myname = "";
  14539.         if (myname[0])
  14540.       ckmakxmsg(prompt,sizeof(prompt)," Name (",host,":",myname,"): ",
  14541.             NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  14542.         else
  14543.       ckmakmsg(prompt,sizeof(prompt)," Name (",host,"): ",NULL);
  14544.         tmp[0] = '\0';
  14545.         readtext(prompt,tmp,sizeof(tmp));
  14546.         if ((*tmp == '\0'))
  14547.       user = myname;
  14548.         else
  14549.       user = tmp;
  14550.     }
  14551.     ckstrncpy (srp_user, user,BUFSIZ);
  14552.     return(0);
  14553. }
  14554.  
  14555. /*--------------------------------------------------------------+
  14556.  | srp_reset: reset srp information                             |
  14557.  +--------------------------------------------------------------*/
  14558. static int
  14559. srp_reset () {
  14560.     if (tc) { t_clientclose (tc); tc = NULL; }
  14561.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  14562.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  14563.     return(0);
  14564. }
  14565.  
  14566. /*--------------------------------------------------------------+
  14567.  | srp_ftp_auth: perform srp authentication                         |
  14568.  +--------------------------------------------------------------*/
  14569. static int
  14570. srp_ftp_auth(host, user, pass)
  14571.     char *host;
  14572.     char *user;
  14573.     char *pass;
  14574. {
  14575.     struct t_num *wp;
  14576.     struct t_num N;
  14577.     struct t_num g;
  14578.     struct t_num s;
  14579.     struct t_num yp;
  14580.     CHAR buf[FTP_BUFSIZ];
  14581.     CHAR tmp[FTP_BUFSIZ];
  14582.     CHAR *bp, *cp;
  14583.     int n, e, clen, blen, len, i;
  14584.     CHAR cid = 0;
  14585.     CHAR hid = 0;
  14586.  
  14587.     srp_pass = srp_acct = 0;
  14588.  
  14589.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  14590.     if (n != REPLY_CONTINUE) {
  14591.         if (ftp_deb)
  14592.             fprintf(stderr, "SRP rejected as an authentication type\n");
  14593.         return(0);
  14594.     } else {                /* Send protocol version */
  14595.         CHAR vers[4];
  14596.         memset (vers, 0, 4);
  14597.         vers[3] = SRP_PROT_VERSION;
  14598.         if (!quiet)
  14599.       printf ("SRP accepted as authentication type.\n");
  14600.         bp = tmp; blen = 0;
  14601.         srp_put (vers, &bp, 4, &blen);
  14602.         len = FTP_BUFSIZ;
  14603.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14604.       goto encode_error;
  14605.         reply_parse = "ADAT=";
  14606.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14607.     }
  14608.     if (n == REPLY_CONTINUE) {        /* Get protocol version */
  14609.         bp = buf;
  14610.         if (!reply_parse)
  14611.       goto data_error;
  14612.         blen = sizeof(buf);
  14613.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  14614.       goto decode_error;
  14615.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  14616.       goto data_error;
  14617.  
  14618.         if (host) {            /* Get username/password if needed */
  14619.         srp_userpass (host);
  14620.         } else {
  14621.             ckstrncpy (srp_user, user, BUFSIZ);
  14622.             srp_pass = pass;
  14623.         }
  14624.         bp = tmp; blen = 0;        /* Send username */
  14625.         srp_put (srp_user, &bp, strlen (srp_user), &blen);
  14626.         len = sizeof(buf);
  14627.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14628.       goto encode_error;
  14629.         reply_parse = "ADAT=";
  14630.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14631.     }
  14632.     if (n == REPLY_CONTINUE) {        /* Get N, g and s */
  14633.     bp = buf;
  14634.         if (!reply_parse)
  14635.       goto data_error;
  14636.         blen = sizeof(buf);
  14637.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14638.       goto decode_error;
  14639.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  14640.       goto data_error;
  14641.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  14642.       goto data_error;
  14643.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  14644.       goto data_error;
  14645.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  14646.             fprintf (stderr, "Unable to open SRP client structure.\n");
  14647.             goto bad;
  14648.         }
  14649.         wp = t_clientgenexp (tc);    /* Send wp */
  14650.         bp = tmp; blen = 0;
  14651.         srp_put (wp->data, &bp, wp->len, &blen);
  14652.         len = sizeof(buf);
  14653.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14654.       goto encode_error;
  14655.         reply_parse = "ADAT=";
  14656.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14657.     }
  14658.     if (n == REPLY_CONTINUE) {        /* Get yp */
  14659.         bp = buf;
  14660.         if (!reply_parse)
  14661.       goto data_error;
  14662.         blen = sizeof(buf);
  14663.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14664.       goto decode_error;
  14665.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  14666.       goto data_error;
  14667.         if (!srp_pass) {
  14668.             static char ftppass[80];
  14669.         setint();
  14670.             readpass(" SRP Password:",ftppass,80);
  14671.             srp_pass = ftppass;
  14672.         }
  14673.         t_clientpasswd (tc, srp_pass);
  14674.         memset (srp_pass, 0, strlen (srp_pass));
  14675.         skey = t_clientgetkey (tc, &yp); /* Send response */
  14676.         bp = tmp; blen = 0;
  14677.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  14678.         len = sizeof(buf);
  14679.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14680.       goto encode_error;
  14681.         reply_parse = "ADAT=";
  14682.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14683.     }
  14684.     if (n == REPLY_CONTINUE) {        /* Get response */
  14685.         bp = buf;
  14686.         if (!reply_parse)
  14687.       goto data_error;
  14688.         blen = sizeof(buf);
  14689.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14690.       goto encode_error;
  14691.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  14692.       goto data_error;
  14693.         if (t_clientverify (tc, cp)) {
  14694.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  14695.             goto bad;
  14696.         }
  14697.         bp = tmp; blen = 0;        /* Send nothing */
  14698.         srp_put ("\0", &bp, 1, &blen);
  14699.         len = sizeof(buf);
  14700.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14701.       goto encode_error;
  14702.         reply_parse = "ADAT=";
  14703.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14704.     }
  14705.     if (n == REPLY_CONTINUE) {        /* Get cipher & hash lists, seqnum */
  14706.         CHAR seqnum[4];
  14707.         CHAR *clist;
  14708.         CHAR *hlist;
  14709.         CHAR *p1;
  14710.         int clist_len, hlist_len;
  14711.         bp = buf;
  14712.         if (!reply_parse)
  14713.       goto data_error;
  14714.         blen = sizeof(buf);
  14715.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14716.       goto encode_error;
  14717.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  14718.       goto data_error;
  14719.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  14720.       goto data_error;
  14721.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  14722.       goto data_error;
  14723.         memcpy (seqnum, cp, 4);
  14724.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  14725.       cid = srp_pref_cipher;
  14726.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  14727.       cid = SRP_DEFAULT_CIPHER;
  14728.         if (!cid) {
  14729.             CHAR *loclist = cipher_getlist ();
  14730.             for (i = 0; i < strlen (loclist); i++)
  14731.           if (cipher_supported (clist, loclist[i])) {
  14732.           cid = loclist[i];
  14733.           break;
  14734.           }
  14735.         }
  14736.         if (!cid) {
  14737.         fprintf (stderr, "Unable to agree on cipher.\n");
  14738.         goto bad;
  14739.     }
  14740.         /* Choose hash */
  14741.  
  14742.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  14743.       hid = srp_pref_hash;
  14744.  
  14745.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  14746.       hid = SRP_DEFAULT_HASH;
  14747.  
  14748.         if (!hid) {
  14749.             CHAR *loclist = hash_getlist ();
  14750.             for (i = 0; i < strlen (loclist); i++)
  14751.           if (hash_supported (hlist, loclist[i])) {
  14752.           hid = loclist[i];
  14753.           break;
  14754.           }
  14755.         }
  14756.         if (!hid) {
  14757.         fprintf (stderr, "Unable to agree on hash.\n");
  14758.         goto bad;
  14759.     }
  14760.         /* Set incrypt */
  14761.  
  14762.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  14763.                     KRYPTO_DECODE)))
  14764.       goto bad;
  14765.  
  14766.         /* Generate random number for outkey and outseqnum */
  14767.  
  14768.         t_random (seqnum, 4);
  14769.  
  14770.         /* Send cid, hid, outkey, outseqnum */
  14771.  
  14772.         bp = tmp; blen = 0;
  14773.         srp_put (&cid, &bp, 1, &blen);
  14774.         srp_put (&hid, &bp, 1, &blen);
  14775.         srp_put (seqnum, &bp, 4, &blen);
  14776.         len = sizeof(buf);
  14777.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14778.       goto encode_error;
  14779.         reply_parse = "ADAT=";
  14780.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14781.  
  14782.         /* Set outcrypt */
  14783.  
  14784.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  14785.                      KRYPTO_ENCODE)))
  14786.       goto bad;
  14787.  
  14788.         t_clientclose (tc);
  14789.         tc = NULL;
  14790.     }
  14791.     if (n != REPLY_COMPLETE)
  14792.       goto bad;
  14793.  
  14794.     if (ftp_vbm) {
  14795.         if (ftp_deb)
  14796.       printf("\n");
  14797.         printf ("SRP authentication succeeded.\n");
  14798.         printf ("Using cipher %s and hash function %s.\n",
  14799.         (cipher_getdescbyid(cid))->name,
  14800.         (hash_getdescbyid(hid))->name
  14801.         );
  14802.     }
  14803.     reply_parse = NULL;
  14804.     auth_type = "SRP";
  14805.     return(1);
  14806.  
  14807.   encode_error:
  14808.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  14809.     goto bad;
  14810.  
  14811.   decode_error:
  14812.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  14813.     goto bad;
  14814.  
  14815.   data_error:
  14816.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  14817.     goto bad;
  14818.  
  14819.   bad:
  14820.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  14821.     reply_parse = NULL;
  14822.     return(0);
  14823. }
  14824.  
  14825. /*--------------------------------------------------------------+
  14826.  | srp_put: put item to send buffer                             |
  14827.  +--------------------------------------------------------------*/
  14828. static int
  14829. srp_put (in, out, inlen, outlen)
  14830.     CHAR *in;
  14831.     CHAR **out;
  14832.     int inlen;
  14833.     int *outlen;
  14834. {
  14835.     srp_uint32 net_len;
  14836.  
  14837.     net_len = htonl (inlen);
  14838.     memcpy (*out, &net_len, 4);
  14839.  
  14840.     *out += 4; *outlen += 4;
  14841.  
  14842.     memcpy (*out, in, inlen);
  14843.  
  14844.     *out += inlen; *outlen += inlen;
  14845.     return(0);
  14846. }
  14847.  
  14848. /*--------------------------------------------------------------+
  14849.  | srp_get: get item from receive buffer                        |
  14850.  +--------------------------------------------------------------*/
  14851. static int
  14852. srp_get (in, out, inlen, outlen)
  14853.     CHAR **in;
  14854.     CHAR **out;
  14855.     int *inlen;
  14856.     int *outlen;
  14857. {
  14858.     srp_uint32 net_len;
  14859.  
  14860.     if (*inlen < 4) return -1;
  14861.  
  14862.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  14863.     *outlen = ntohl (net_len);
  14864.  
  14865.     if (*inlen < *outlen) return -1;
  14866.  
  14867.     *out = *in; *inlen -= *outlen; *in += *outlen;
  14868.  
  14869.     return *outlen;
  14870. }
  14871.  
  14872. /*--------------------------------------------------------------+
  14873.  | srp_encode: encode control message                           |
  14874.  +--------------------------------------------------------------*/
  14875. static int
  14876. srp_encode (private, in, out, len)
  14877.     int private;
  14878.     CHAR *in;
  14879.     CHAR *out;
  14880.     unsigned len;
  14881. {
  14882.     if (private)
  14883.       return krypto_msg_priv (outcrypt, in, out, len);
  14884.     else
  14885.       return krypto_msg_safe (outcrypt, in, out, len);
  14886. }
  14887.  
  14888. /*--------------------------------------------------------------+
  14889.  | srp_decode: decode control message                           |
  14890.  +--------------------------------------------------------------*/
  14891. static int
  14892. srp_decode (private, in, out, len)
  14893.     int private;
  14894.     CHAR *in;
  14895.     CHAR *out;
  14896.     unsigned len;
  14897. {
  14898.     if (private)
  14899.       return krypto_msg_priv (incrypt, in, out, len);
  14900.     else
  14901.       return krypto_msg_safe (incrypt, in, out, len);
  14902. }
  14903.  
  14904. #endif /* FTP_SRP */
  14905. #endif /* TCPSOCKET (top of file) */
  14906. #endif /* SYSFTP (top of file) */
  14907. #endif /* NOFTP (top of file) */
  14908.