home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv201.zip / ckcftp.c < prev    next >
C/C++ Source or Header  |  2002-02-09  |  506KB  |  14,981 lines

  1. /*  C K C F T P  --  FTP Client for C-Kermit  */
  2.  
  3. char *ckftpv = "FTP Client, 8.0.151, 7 Feb 2002";
  4.  
  5. /*
  6.   Authors:
  7.     Jeff Altman <jaltman@columbia.edu>
  8.     Frank da Cruz <fdc@columbia.edu>,
  9.     The Kermit Project, Columbia University.
  10.  
  11.   Copyright (C) 1985, 2002,
  12.     Trustees of Columbia University in the City of New York.
  13.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  14.     copyright text in the ckcmai.c module for disclaimer and permissions.
  15.  
  16.   Portions of conditionally included code Copyright Regents of the
  17.     University of California and The Stanford SRP Authentication Project;
  18.     see notices below.
  19. */
  20.  
  21. /*
  22.   Pending...
  23.  
  24.   . Implement recursive 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. /*
  192.   How to get the struct timeval definition so we can call select().  The
  193.   xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  194.   targets.  The problem is: maybe we have already included some header file
  195.   that defined struct timeval, and maybe we didn't.  If we did, we don't want
  196.   to include another header file that defines it again or the compilation will
  197.   fail.  If we didn't, we have to include the header file where it's defined.
  198.   But in some cases even that won't work because of strict POSIX constraints
  199.   or somesuch, or because this introduces other conflicts (e.g. struct tm
  200.   multiply defined), in which case we have to define it ourselves, but this
  201.   can work only if we didn't already encounter a definition.
  202. */
  203. #ifndef DCLTIMEVAL
  204. #ifdef SV68R3V6
  205. #define DCLTIMEVAL
  206. #else
  207. #ifdef SCO234
  208. #define DCLTIMEVAL
  209. #endif /* SCO234 */
  210. #endif /* SV68R3V6 */
  211. #endif /* DCLTIMEVAL */
  212.  
  213. #ifdef DCLTIMEVAL
  214. /* Also maybe in some places the elements must be unsigned... */
  215. struct timeval {
  216.     long tv_sec;
  217.     long tv_usec;
  218. };
  219. #ifdef COMMENT
  220. /* Currently we don't use this... */
  221. struct timezone {
  222.     int tz_minuteswest;
  223.     int tz_dsttime;
  224. };
  225. #endif /* COMMENT */
  226. #else  /* !DCLTIMEVAL */
  227. #ifndef NOSYSTIMEH
  228. #ifdef SYSTIMEH
  229. #include <sys/time.h>
  230. #endif /* SYSTIMEH */
  231. #endif /* NOSYSTIMEH */
  232. #ifndef NOSYSTIMEBH
  233. #ifdef SYSTIMEBH
  234. #include <sys/timeb.h>
  235. #endif /* SYSTIMEBH */
  236. #endif /* NOSYSTIMEBH */
  237. #endif /* DCLTIMEVAL */
  238.  
  239.  
  240. #ifndef NOSETTIME
  241. #ifdef COMMENT
  242. /* This section moved to ckcdeb.h */
  243. #ifdef POSIX
  244. #define UTIMEH
  245. #else
  246. #ifdef HPUX9
  247. #define UTIMEH
  248. #else
  249. #ifdef OS2
  250. #define SYSUTIMEH
  251. #endif /* OS2 */
  252. #endif /* HPUX9 */
  253. #endif /* POSIX */
  254. #endif /* COMMENT */
  255.  
  256. #ifdef SYSUTIMEH
  257. #include <sys/utime.h>
  258. #else
  259. #ifdef UTIMEH
  260. #include <utime.h>
  261. #define SYSUTIMEH
  262. #endif /* UTIMEH */
  263. #endif /* SYSUTIMEH */
  264. #endif /* NOSETTIME */
  265.  
  266. #ifndef SCO_OSR504
  267. #ifdef SELECT_H
  268. #include <sys/select.h>
  269. #endif /* SELECT_H */
  270. #endif /* SCO_OSR504 */
  271.  
  272. /* select() dialects... */
  273.  
  274. #ifdef UNIX
  275. #define BSDSELECT                       /* BSD select() syntax/semantics */
  276. #else
  277. #ifdef OS2                              /* OS/2 or Win32 */
  278. #ifdef NT
  279. #define BSDSELECT
  280. #else /* NT */
  281. #define IBMSELECT
  282. #endif /* NT */
  283. #endif /* OS2 */
  284. #endif /* UNIX */
  285.  
  286. /* Other select() peculiarities */
  287.  
  288. #ifdef HPUX
  289. #ifndef HPUX10                          /* HP-UX 9.xx and earlier */
  290. #ifndef HPUX1100
  291. /* The three interior args to select() are (int *) rather than (fd_set *) */
  292. #ifndef INTSELECT
  293. #define INTSELECT
  294. #endif /* INTSELECT */
  295. #endif /* HPUX1100 */
  296. #endif /* HPUX10 */
  297. #endif /* HPUX */
  298.  
  299. #ifdef CK_SOCKS                         /* SOCKS Internet relay package */
  300. #ifdef CK_SOCKS5                        /* SOCKS 5 */
  301. #define accept  SOCKSaccept
  302. #define bind    SOCKSbind
  303. #define connect SOCKSconnect
  304. #define getsockname SOCKSgetsockname
  305. #define listen SOCKSlisten
  306. #else  /* Not SOCKS 5 */
  307. #define accept  Raccept
  308. #define bind    Rbind
  309. #define connect Rconnect
  310. #define getsockname Rgetsockname
  311. #define listen Rlisten
  312. #endif /* CK_SOCKS5 */
  313. #endif /* CK_SOCKS */
  314.  
  315. #ifndef NOHTTP
  316. extern char * tcp_http_proxy;           /* Name[:port] of http proxy server */
  317. extern int    tcp_http_proxy_errno;
  318. extern char * tcp_http_proxy_user;
  319. extern char * tcp_http_proxy_pwd;
  320. #define HTTPCPYL 1024
  321. static char proxyhost[HTTPCPYL];
  322. #endif /* NOHTTP */
  323. int ssl_ftp_proxy = 0;                  /* FTP over SSL/TLS Proxy Server */
  324.  
  325. /* Feature selection */
  326.  
  327. #ifndef USE_SHUTDOWN
  328. /*
  329.   We don't use shutdown() because (a) we always call it just before close()
  330.   so it's redundant and unnecessary, and (b) it introduces a long pause on
  331.   some platforms like SV/68 R3.
  332. */
  333. /* #define USE_SHUTDOWN */
  334. #endif /* USE_SHUTDOWN */
  335.  
  336. #ifndef NORESEND
  337. #ifndef NORESTART                       /* Restart / recover */
  338. #ifndef FTP_RESTART
  339. #define FTP_RESTART
  340. #endif /* FTP_RESTART */
  341. #endif /* NORESTART */
  342. #endif /* NORESEND */
  343.  
  344. #ifndef NOUPDATE                        /* Update mode */
  345. #ifndef DOUPDATE
  346. #define DOUPDATE
  347. #endif /* DOUPDATE */
  348. #endif /* NOUPDATE */
  349.  
  350. #ifndef UNICODE                         /* Unicode required */
  351. #ifndef NOCSETS                         /* for charset translation */
  352. #define NOCSETS
  353. #endif /* NOCSETS */
  354. #endif /* UNICODE */
  355.  
  356. #ifndef OS2
  357. #ifndef HAVE_MSECS                      /* Millisecond timer */
  358. #ifdef UNIX
  359. #ifdef GFTIMER
  360. #define HAVE_MSECS
  361. #endif /* GFTIMER */
  362. #endif /* UNIX */
  363. #endif /* HAVE_MSECS */
  364. #endif /* OS2 */
  365.  
  366. #ifdef PIPESEND                         /* PUT from pipe */
  367. #ifndef PUTPIPE
  368. #define PUTPIPE
  369. #endif /* PUTPIPE */
  370. #endif /* PIPESEND */
  371.  
  372. #ifndef NOSPL                           /* PUT from array */
  373. #ifndef PUTARRAY
  374. #define PUTARRAY
  375. #endif /* PUTARRAY */
  376. #endif /* NOSPL */
  377.  
  378. /* Security... */
  379.  
  380. #ifdef CK_SRP
  381. #define FTP_SRP
  382. #endif /* CK_SRP */
  383.  
  384. #ifdef CK_KERBEROS
  385. #ifdef KRB4
  386. /*
  387.   There is a conflict between the Key Schedule formats used internally
  388.   within the standalone MIT KRB4 library and that used by Eric Young
  389.   in OpenSSL and his standalone DES library.  Therefore, KRB4 FTP AUTH
  390.   cannot be supported when either of those two packages are used.
  391. */
  392. #ifdef KRB524
  393. #define FTP_KRB4
  394. #else /* KRB524 */
  395. #ifndef CK_SSL
  396. #ifndef LIBDES
  397. #define FTP_KRB4
  398. #endif /* LIBDES */
  399. #endif /* CK_SSL */
  400. #endif /* KRB524 */
  401. #endif /* KRB4 */
  402. #ifdef KRB5
  403. #ifndef HEIMDAL
  404. #define FTP_GSSAPI
  405. #endif /* HEIMDAL */
  406. #endif /* KRB5 */
  407. #endif /* CK_KERBEROS */
  408.  
  409. /* FTP_SECURITY is defined if any of the above is selected */
  410. #ifndef FTP_SECURITY
  411. #ifdef FTP_GSSAPI
  412. #define FTP_SECURITY
  413. #else
  414. #ifdef FTP_KRB4
  415. #define FTP_SECURITY
  416. #else
  417. #ifdef FTP_SRP
  418. #define FTP_SECURITY
  419. #else
  420. #ifdef CK_SSL
  421. #define FTP_SECURITY
  422. #endif /* CK_SSL */
  423. #endif /* FTP_SRP */
  424. #endif /* FTP_KRB4 */
  425. #endif /* FTP_GSSAPI */
  426. #endif /* FTP_SECURITY */
  427.  
  428. #ifdef CK_DES
  429. #ifdef CK_SSL
  430. #ifndef LIBDES
  431. #define LIBDES
  432. #endif /* LIBDES */
  433. #endif /* CK_SSL */
  434. #endif /* CK_DES */
  435.  
  436. #ifdef CRYPT_DLL
  437. #ifndef LIBDES
  438. #define LIBDES
  439. #endif /* LIBDES */
  440. #endif /* CRYPT_DLL */
  441.  
  442. #ifdef FTP_KRB4
  443. #define des_cblock Block
  444. #define des_key_schedule Schedule
  445. #ifdef KRB524
  446. #ifdef NT
  447. #define _WINDOWS
  448. #endif /* NT */
  449. #include "kerberosIV/krb.h"
  450. #else /* KRB524 */
  451. #ifdef SOLARIS
  452. #ifndef sun
  453. /* For some reason lost in history the Makefile Solaris targets have -Usun */
  454. #define sun
  455. #endif /* sun */
  456. #endif /* SOLARIS */
  457. #include "krb.h"
  458. #define krb_get_err_text_entry krb_get_err_text
  459. #endif /* KRB524 */
  460. #endif /* FTP_KRB4 */
  461.  
  462. #ifdef CK_SSL
  463. #ifdef FTP_KRB4
  464. #ifndef HEADER_DES_H
  465. #define HEADER_DES_H
  466. #endif /* HEADER_DES_H */
  467. #endif /* FTP_KRB4 */
  468. #include "ck_ssl.h"
  469. #endif /* CK_SSL */
  470.  
  471. #ifdef FTP_SRP
  472. #ifdef HAVE_PWD_H
  473. #include "pwd.h"
  474. #endif /* HAVE_PWD_H */
  475. #include "t_pwd.h"
  476. #include "t_client.h"
  477. #include "krypto.h"
  478. #endif /* FTP_SRP */
  479.  
  480. #ifdef FTP_GSSAPI
  481. #include <gssapi/gssapi.h>
  482. /*
  483.   Need to include the krb5 file, because we're doing manual fallback
  484.   from the v2 mech to the v1 mech.  Once there's real negotiation,
  485.   we can be generic again.
  486. */
  487. #include <gssapi/gssapi_generic.h>
  488. #include <gssapi/gssapi_krb5.h>
  489. static gss_ctx_id_t gcontext;
  490. #endif /* FTP_GSSAPI */
  491.  
  492. #ifdef OS2
  493. #ifdef FTP_SRP
  494. #define MAP_KRYPTO
  495. #ifdef SRPDLL
  496. #define MAP_SRP
  497. #endif /* SRPDLL */
  498. #endif /* FTP_SRP */
  499. #ifdef FTP_KRB4
  500. #define MAP_KRB4
  501. #define MAP_DES
  502. #endif /* FTP_KRB4 */
  503. #ifdef FTP_GSSAPI
  504. #define MAP_GSSAPI
  505. #define GSS_OIDS
  506. #endif /* FTP_GSSAPI */
  507. #include "ckoath.h"
  508.  
  509. extern int k95stdout, wherex[], wherey[];
  510. extern unsigned char colorcmd;
  511. #include "ckocon.h"
  512. #endif /* OS2 */
  513.  
  514. #ifdef FTP_KRB4
  515. static char ftp_realm[REALM_SZ + 1];
  516. static KTEXT_ST ftp_tkt;
  517. #ifdef OS2
  518. static LEASH_CREDENTIALS ftp_cred;
  519. #else /* OS2 */
  520. static CREDENTIALS ftp_cred;
  521. #endif /* OS2 */
  522. static MSG_DAT ftp_msg_data;
  523. static des_key_schedule ftp_sched;
  524. static int foo[4] = {99,99,99,99};
  525. #endif /* FTP_KRB4 */
  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, msgflg;
  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. _PROTOTYP(char * ck_krb4_realmofhost,(char *));
  678. #endif /* KRB4 */
  679.  
  680. #ifdef KRB5
  681. extern int krb5_autoget;
  682. extern int krb5_d_no_addresses;
  683. _PROTOTYP(char * ck_krb5_realmofhost,(char *));
  684. #endif /* KRB5 */
  685.  
  686. #ifdef DCMDBUF
  687. extern char *atmbuf;                    /* Atom buffer (malloc'd) */
  688. extern char *cmdbuf;                    /* Command buffer (malloc'd) */
  689. extern char *line;                      /* Big string buffer #1 */
  690. extern char *tmpbuf;                    /* Big string buffer #2 */
  691. #else
  692. extern char atmbuf[];                   /* The same, but static */
  693. extern char cmdbuf[];
  694. extern char line[];
  695. extern char tmpbuf[];
  696. #endif /* DCMDBUF */
  697.  
  698. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  699.  
  700. /* Public variables declared here */
  701.  
  702. #ifdef NOXFER
  703. int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
  704. #else
  705. int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
  706. #endif /* NOXFER */
  707. int ftpcode = -1;                       /* Last FTP response code */
  708. int ftp_cmdlin = 0;                     /* FTP invoked from command line */
  709. int ftp_fai = 0;                        /* FTP failure count */
  710. int ftp_deb = 0;                        /* FTP debugging */
  711. int ftp_knf = 0;                        /* Keep NLST file */
  712. int ftp_log = 1;                        /* FTP Auto-login */
  713. int ftp_action = 0;                     /* FTP action from command line */
  714. int ftp_dates = 0;                      /* Set file dates from server */
  715.  
  716. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  717. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  718. char * ftp_host = NULL;                 /* FTP hostname */
  719. char * ftp_logname = NULL;              /* FTP username */
  720. char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
  721.  
  722. /* Static global variables */
  723. /*
  724.   This is just a first stab -- these strings should match how the
  725.   corresponding FTP servers identify themselves.
  726. */
  727. #ifdef UNIX
  728. static char * myostype = "UNIX";
  729. #else
  730. #ifdef VMS
  731. /* not yet... */
  732. static char * myostype = "VMS";
  733. #else
  734. #ifdef OS2
  735. #ifdef NT
  736. static char * myostype = "WIN32";
  737. #else
  738. static char * myostype = "OS/2";
  739. #endif /* NT */
  740. #else
  741. static char * myostype = "UNSUPPORTED";
  742. #endif /* OS2  */
  743. #endif /* VMS */
  744. #endif /* UNIX */
  745.  
  746. static int noinit = 0;                  /* Don't send REST, STRU, MODE */
  747. static int alike = 0;                   /* Client/server like platforms */
  748. static int local = 1;                   /* Shadows Kermit global 'local' */
  749. static int dout = -1;                   /* Data connection file descriptor */
  750. static int dpyactive = 0;               /* Data transfer is active */
  751. static int globaldin = -1;              /* Data connection f.d. */
  752. static int out2screen = 0;              /* GET output is to screen */
  753. static int forcetype = 0;               /* Force text or binary mode */
  754. static int cancelfile = 0;              /* File canceled */
  755. static int cancelgroup = 0;             /* Group canceled */
  756. static int anonymous = 0;               /* Logging in as anonymous */
  757. static int loggedin = 0;                /* Logged in (or not) */
  758. static int puterror = 0;                /* What to do on PUT error */
  759. static int geterror = 0;                /* What to do on GET error */
  760. static int rfrc = 0;                    /* remote_files() return code */
  761. static int okrestart = 0;               /* Server understands REST */
  762. static int printlines = 0;              /* getreply()should print data lines */
  763. static int haveurl = 0;                 /* Invoked by command-line FTP URL */
  764.  
  765. static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
  766. static int c0, c1;                      /* Temp variables for characters */
  767.  
  768. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  769. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  770.  
  771. #define RFNBUFSIZ 4096
  772.  
  773. static unsigned int maxbuf = 0, actualbuf = 0;
  774. static CHAR *ucbuf = NULL;
  775. static int ucbufsiz = 0;
  776. static unsigned int nout = 0;           /* Number of chars in ucbuf */
  777.  
  778. static jmp_buf recvcancel;
  779. static jmp_buf sendcancel;
  780. static jmp_buf ptcancel;
  781. static jmp_buf jcancel;
  782. static int ptabflg = 0;
  783.  
  784. /* Protection level symbols */
  785.  
  786. #define FPL_CLR 1                       /* Clear */
  787. #define FPL_SAF 2                       /* Safe */
  788. #define FPL_PRV 3                       /* Private */
  789. #define FPL_CON 4                       /* Confidential */
  790.  
  791. /* File type symbols keyed to the file-type symbols from ckcker.h */
  792.  
  793. #define FTT_ASC XYFT_T                  /* ASCII (text) */
  794. #define FTT_BIN XYFT_B                  /* Binary (image) */
  795. #define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
  796.  
  797. /* SET FTP values */
  798.  
  799. static int                              /* SET FTP values... */
  800.   ftp_aut = 1,                          /* Auto-authentication */
  801. #ifdef FTP_SECURITY
  802.   ftp_cry = 1,                          /* Auto-encryption */
  803.   ftp_cfw = 0,                          /* Credential forwarding */
  804. #endif /* FTP_SECURITY */
  805.   ftp_cpl = FPL_CLR,                    /* Command protection level */
  806.   ftp_dpl = FPL_CLR,                    /* Data protection level */
  807. #ifdef FTP_PROXY
  808.   ftp_prx = 0,                          /* Use proxy */
  809. #endif /* FTP_PROXY */
  810.   sav_psv = -1,                         /* For saving passive mode */
  811.   ftp_psv = 1,                          /* Passive mode */
  812.   ftp_spc = 1,                          /* Send port commands */
  813.   ftp_typ = FTT_ASC,                    /* Type */
  814.   get_auto = 1,                         /* Automatic type switching for GET */
  815.   tenex = 0,                            /* Type is Tenex */
  816.   ftp_usn = 0,                          /* Unique server names */
  817.   ftp_prm = 0,                          /* Permissions */
  818.   ftp_cnv = 2,                          /* Filename conversion (2 = auto) */
  819.   ftp_vbm = DEF_VBM,                    /* Verbose mode */
  820.   ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  821.   ftp_err = 0,                          /* Error action */
  822.   ftp_fnc = -1;                         /* Filename collision action */
  823.  
  824. static int
  825. #ifdef NOCSETS
  826.   ftp_csr = -1,                         /* Remote (server) character set */
  827. #else
  828.   ftp_csr = FC_UTF8,
  829. #endif /* NOCSETS */
  830.   ftp_xla = 0;                          /* Character-set translation on/off */
  831. int
  832.   ftp_csx = -1,                         /* Remote charset currently in use */
  833.   ftp_csl = -1;                         /* Local charset currently in use */
  834.  
  835. static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
  836.  
  837. char * ftp_nml = NULL;                  /* /NAMELIST */
  838. char * ftp_tmp = NULL;                  /* Temporary string */
  839. static char * ftp_acc = NULL;           /* Account string */
  840. static char * auth_type = NULL;         /* Authentication type */
  841. static char * srv_renam = NULL;         /* Server-rename string */
  842. FILE * fp_nml = NULL;                   /* Namelist file pointer */
  843.  
  844. static int csocket = -1;                /* Control socket */
  845. static int connected = 0;               /* Connected to FTP server */
  846. static short ftp_port = 0;              /* FTP port */
  847. #ifdef FTPHOST
  848. static int hostcmd = 0;                 /* Has HOST command been sent */
  849. #endif /* FTPHOST */
  850. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  851. static char bytename[8];
  852.  
  853. /* For parsing replies to FTP server command */
  854. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  855.  
  856. #ifdef FTP_PROXY
  857. static int proxy, unix_proxy
  858. #endif /* FTP_PROXY */
  859.  
  860. static char pasv[64];                   /* Passive-mode port */
  861. static int passivemode = 0;
  862. static int sendport = 0;
  863. static int servertype = 0;              /* FTP server's OS type */
  864.  
  865. static int testing = 0;
  866. static char ftpcmdbuf[FTP_BUFSIZ];
  867.  
  868. /* Macro definitions */
  869.  
  870. #define UC(b) ckitoa(((int)b)&0xff)
  871. #define nz(x) ((x) == 0 ? 1 : (x))
  872.  
  873. /* Command tables and definitions */
  874.  
  875. #define FTP_ACC  1                      /* FTP command keyword codes */
  876. #define FTP_APP  2
  877. #define FTP_CWD  3
  878. #define FTP_CHM  4
  879. #define FTP_CLS  5
  880. #define FTP_DEL  6
  881. #define FTP_DIR  7
  882. #define FTP_GET  8
  883. #define FTP_IDL  9
  884. #define FTP_MDE 10
  885. #define FTP_MDI 11
  886. #define FTP_MGE 12
  887. #define FTP_MKD 13
  888. #define FTP_MOD 14
  889. #define FTP_MPU 15
  890. #define FTP_OPN 16
  891. #define FTP_PUT 17
  892. #define FTP_PWD 18
  893. #define FTP_RGE 19
  894. #define FTP_REN 20
  895. #define FTP_RES 21
  896. #define FTP_HLP 22
  897. #define FTP_RMD 23
  898. #define FTP_STA 24
  899. #define FTP_SIT 25
  900. #define FTP_SIZ 26
  901. #define FTP_SYS 27
  902. #define FTP_UMA 28
  903. #define FTP_GUP 29
  904. #define FTP_USR 30
  905. #define FTP_QUO 31
  906. #define FTP_TYP 32
  907. #define FTP_FEA 33
  908. #define FTP_OPT 34
  909. #define FTP_CHK 35
  910. #define FTP_VDI 36
  911.  
  912. struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
  913.     { "auto",    2, 0 },
  914.     { "ftp",     1, 0 },
  915.     {"kermit",   0, 0  }
  916. };
  917.  
  918. static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
  919.     { "proceed", 0, 0 },                /* 0 = proceed */
  920.     { "quit",    1, 0 }                 /* 1 = quit */
  921. };
  922.  
  923. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  924.     { "account",   FTP_ACC, 0 },
  925.     { "append",    FTP_APP, 0 },
  926.     { "bye",       FTP_CLS, 0 },
  927.     { "cd",        FTP_CWD, 0 },
  928.     { "cdup",      FTP_GUP, 0 },
  929.     { "check",     FTP_CHK, 0 },
  930.     { "chmod",     FTP_CHM, 0 },
  931.     { "close",     FTP_CLS, 0 },
  932.     { "cwd",       FTP_CWD, CM_INV },
  933.     { "delete",    FTP_MDE, 0 },
  934.     { "directory", FTP_DIR, 0 },
  935.     { "features",  FTP_FEA, 0 },
  936.     { "get",       FTP_GET, 0 },
  937.     { "help",      FTP_HLP, 0 },
  938.     { "idle",      FTP_IDL, 0 },
  939.     { "login",     FTP_USR, CM_INV },
  940.     { "mdelete",   FTP_MDE, CM_INV },
  941.     { "mget",      FTP_MGE, 0 },
  942.     { "mkdir",     FTP_MKD, 0 },
  943.     { "modtime",   FTP_MOD, 0 },
  944.     { "mput",      FTP_MPU, 0 },
  945.     { "open",      FTP_OPN, 0 },
  946.     { "opts",      FTP_OPT, CM_INV },
  947.     { "options",   FTP_OPT, 0 },
  948.     { "put",       FTP_PUT, 0 },
  949.     { "pwd",       FTP_PWD, 0 },
  950.     { "quit",      FTP_CLS, CM_INV },
  951.     { "quote",     FTP_QUO, 0 },
  952.     { "reget",     FTP_RGE, 0 },
  953.     { "rename",    FTP_REN, 0 },
  954.     { "reset",     FTP_RES, 0 },
  955.     { "rmdir",     FTP_RMD, 0 },
  956.     { "send",      FTP_PUT, CM_INV },
  957.     { "site",      FTP_SIT, 0 },
  958.     { "size",      FTP_SIZ, 0 },
  959.     { "status",    FTP_STA, 0 },
  960.     { "system",    FTP_SYS, 0 },
  961.     { "type",      FTP_TYP, 0 },
  962.     { "umask",     FTP_UMA, 0 },
  963.     { "up",        FTP_GUP, CM_INV },
  964.     { "user",      FTP_USR, 0 },
  965.     { "vdirectory",FTP_VDI, 0 },
  966.     { "", 0, 0 }
  967. };
  968. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  969.  
  970. #define OPN_ANO 1
  971. #define OPN_PSW 2
  972. #define OPN_USR 3
  973. #define OPN_ACC 4
  974. #define OPN_ACT 5
  975. #define OPN_PSV 6
  976. #define OPN_TLS 7
  977. #define OPN_NIN 8
  978.  
  979. #ifdef FTP_SECURITY
  980. #ifdef CK_SSL
  981. #define USETLSTAB
  982. static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
  983.     { "/ssl",       OPN_TLS, 0    },
  984.     { "/tls",       OPN_TLS, 0    },
  985.     { "", 0, 0 }
  986. };
  987. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  988. #endif /* CK_SSL */
  989. #endif /* FTP_SECURITY */
  990.  
  991. static struct keytab ftpswitab[] = {    /* FTP command switches */
  992.     { "/account",   OPN_ACC, CM_ARG },
  993.     { "/active",    OPN_ACT, 0      },
  994.     { "/anonymous", OPN_ANO, 0      },
  995.     { "/noinit",    OPN_NIN, 0      },
  996.     { "/passive",   OPN_PSV, 0      },
  997.     { "/password",  OPN_PSW, CM_ARG },
  998.     { "/user",      OPN_USR, CM_ARG },
  999.     { "", 0, 0 }
  1000. };
  1001. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  1002.  
  1003. /* SET FTP command keyword indices */
  1004.  
  1005. #define FTS_AUT  1                      /* Autoauthentication */
  1006. #define FTS_CRY  2                      /* Encryption */
  1007. #define FTS_LOG  3                      /* Autologin */
  1008. #define FTS_CPL  4                      /* Command protection level */
  1009. #define FTS_CFW  5                      /* Credentials forwarding */
  1010. #define FTS_DPL  6                      /* Data protection level */
  1011. #define FTS_DBG  7                      /* Debugging */
  1012. #define FTS_PSV  8                      /* Passive mode */
  1013. #define FTS_SPC  9                      /* Send port commands */
  1014. #define FTS_TYP 10                      /* (file) Type */
  1015. #define FTS_USN 11                      /* Unique server names (for files) */
  1016. #define FTS_VBM 12                      /* Verbose mode */
  1017. #define FTS_ATP 13                      /* Authentication type */
  1018. #define FTS_CNV 14                      /* Filename conversion */
  1019. #define FTS_TST 15                      /* Test (progress) messages */
  1020. #define FTS_PRM 16                      /* (file) Permissions */
  1021. #define FTS_XLA 17                      /* Charset translation */
  1022. #define FTS_CSR 18                      /* Server charset */
  1023. #define FTS_ERR 19                      /* Error action */
  1024. #define FTS_FNC 20                      /* Collision */
  1025. #define FTS_SRP 21                      /* SRP options */
  1026. #define FTS_GFT 22                      /* GET automatic file-type switching */
  1027. #define FTS_DAT 23                      /* Set file dates */
  1028.  
  1029. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1030.  
  1031. #define PUT_UPD 1                       /* Update */
  1032. #define PUT_RES 2                       /* Restart */
  1033. #define PUT_SIM 4                       /* Simulation */
  1034.  
  1035. #ifdef FTP_SECURITY
  1036. /* FTP authentication options */
  1037.  
  1038. #define FTA_AUTO 0                      /* Auto */
  1039. #define FTA_SRP  1                      /* SRP */
  1040. #define FTA_GK5  2                      /* Kerberos 5 */
  1041. #define FTA_K4   3                      /* Kerberos 4 */
  1042. #define FTA_SSL  4                      /* SSL */
  1043. #define FTA_TLS  5                      /* TLS */
  1044.  
  1045. /* FTP authentication types */
  1046.  
  1047. #define FTPATYPS 8
  1048. static int ftp_auth_type[FTPATYPS] = {
  1049. #ifdef FTP_GSSAPI
  1050.     FTA_GK5,                            /* GSSAPI Kerberos 5 */
  1051. #endif /* FTP_GK5 */
  1052. #ifdef FTP_SRP
  1053.     FTA_SRP,                            /* SRP */
  1054. #endif /* FTP_SRP */
  1055. #ifdef FTP_KRB4
  1056.     FTA_K4,                             /* Kerberos 4 */
  1057. #endif /* FTP_KRB4 */
  1058. #ifdef CK_SSL
  1059.     FTA_TLS,                            /* TLS */
  1060.     FTA_SSL,                            /* SSL */
  1061. #endif /* CK_SSL */
  1062.     0
  1063. };
  1064.  
  1065. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1066.     { "automatic", FTA_AUTO,  CM_INV },
  1067. #ifdef FTP_GSSAPI
  1068.     { "gssapi-krb5", FTA_GK5, 0 },
  1069. #endif /* FTP_GSSAPI */
  1070. #ifdef FTP_KRB4
  1071.     { "k4",       FTA_K4,     CM_INV },
  1072. #endif /* FTP_KRB4 */
  1073. #ifdef FTP_GSSAPI
  1074.     { "k5",        FTA_GK5,   CM_INV },
  1075. #endif /* FTP_GSSAPI */
  1076. #ifdef FTP_KRB4
  1077.     { "kerberos4", FTA_K4,    0 },
  1078. #endif /* FTP_KRB4 */
  1079. #ifdef FTP_GSSAPI
  1080.     { "kerberos5", FTA_GK5,   CM_INV },
  1081. #endif /* FTP_GSSAPI */
  1082. #ifdef FTP_KRB4
  1083.     { "kerberos_iv",FTA_K4,   CM_INV },
  1084. #endif /* FTP_KRB4 */
  1085. #ifdef FTP_GSSAPI
  1086.     { "kerberos_v", FTA_GK5,  CM_INV },
  1087. #endif /* FTP_GSSAPI */
  1088. #ifdef FTP_KRB4
  1089.     { "krb4",     FTA_K4,     CM_INV },
  1090. #endif /* FTP_KRB4 */
  1091. #ifdef FTP_GSSAPI
  1092.     { "krb5",     FTA_GK5,    CM_INV },
  1093. #endif /* FTP_GSSAPI */
  1094. #ifdef FTP_SRP
  1095.     { "srp",      FTA_SRP,     0 },
  1096. #endif /* FTP_SRP */
  1097. #ifdef CK_SSL
  1098.     { "ssl",      FTA_SSL,     0 },
  1099.     { "tls",      FTA_TLS,     0 },
  1100. #endif /* CK_SSL */
  1101.     { "", 0, 0 }
  1102. };
  1103. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1104.  
  1105. #ifdef FTP_SRP
  1106. #define SRP_CIPHER 1
  1107. #define SRP_HASH   2
  1108. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1109.     { "cipher",   SRP_CIPHER,     0 },
  1110.     { "hash",     SRP_HASH,       0 },
  1111.     { "", 0, 0 }
  1112. };
  1113. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1114. #endif /* FTP_SRP */
  1115. #endif /* FTP_SECURITY */
  1116.  
  1117. static struct keytab ftpset[] = {       /* SET FTP commmand table */
  1118. #ifdef FTP_SECURITY
  1119.     { "authtype",                 FTS_ATP, 0 },
  1120.     { "autoauthentication",       FTS_AUT, 0 },
  1121.     { "autoencryption",           FTS_CRY, 0 },
  1122. #endif /* FTP_SECURITY */
  1123.     { "autologin",                FTS_LOG, 0 },
  1124. #ifndef NOCSETS
  1125.     { "character-set-translation",FTS_XLA, 0 },
  1126. #endif /* NOCSETS */
  1127.     { "collision",                FTS_FNC, 0 },
  1128. #ifdef FTP_SECURITY
  1129.     { "command-protection-level", FTS_CPL, 0 },
  1130.     { "cpl",                      FTS_CPL, CM_INV },
  1131.     { "credential-forwarding",    FTS_CFW, 0 },
  1132.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1133.     { "data-protection-level",    FTS_DPL, 0 },
  1134. #endif /* FTP_SECURITY */
  1135.     { "dates",                    FTS_DAT, 0 },
  1136.     { "debug",                    FTS_DBG, 0 },
  1137. #ifdef FTP_SECURITY
  1138.     { "dpl",                      FTS_DPL, CM_INV },
  1139. #endif /* FTP_SECURITY */
  1140.     { "error-action",             FTS_ERR, 0 },
  1141.     { "filenames",                FTS_CNV, 0 },
  1142.     { "get-filetype-switching",   FTS_GFT, 0 },
  1143.     { "passive-mode",             FTS_PSV, 0 },
  1144.     { "permissions",              FTS_PRM, 0 },
  1145.     { "progress-messages",        FTS_TST, 0 },
  1146.     { "send-port-commands",       FTS_SPC, 0 },
  1147. #ifndef NOCSETS
  1148.     { "server-character-set",     FTS_CSR, 0 },
  1149. #endif /* NOCSETS */
  1150. #ifdef FTP_SRP
  1151.     { "srp",                      FTS_SRP, 0 },
  1152. #else
  1153.     { "srp",                      FTS_SRP, CM_INV },
  1154. #endif /* FTP_SRP */
  1155.     { "type",                     FTS_TYP, 0 },
  1156.     { "unique-server-names",      FTS_USN, 0 },
  1157.     { "verbose-mode",             FTS_VBM, 0 },
  1158.     { "", 0, 0 }
  1159. };
  1160. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1161.  
  1162. /*
  1163.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1164.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1165.   Don't just make up new ones, since the number of SND_xxx options must be
  1166.   known in advance for the switch-parsing arrays.
  1167. */
  1168. #define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
  1169. #define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
  1170. #define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
  1171.  
  1172. static struct keytab putswi[] = {       /* FTP PUT switch table */
  1173.     { "/after",                SND_AFT, CM_ARG },
  1174. #ifdef PUTARRAY
  1175.     { "/array",                SND_ARR, CM_ARG },
  1176. #endif /* PUTARRAY */
  1177.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1178.     { "/as-name",              SND_ASN, CM_ARG },
  1179.     { "/ascii",                SND_TXT, CM_INV },
  1180.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1181.     { "/before",               SND_BEF, CM_ARG },
  1182.     { "/binary",               SND_BIN, 0 },
  1183. #ifdef PUTPIPE
  1184.     { "/command",              SND_CMD, CM_PSH },
  1185. #endif /* PUTPIPE */
  1186.     { "/delete",               SND_DEL, 0 },
  1187. #ifdef UNIXOROSK
  1188.     { "/dotfiles",             SND_DOT, 0 },
  1189. #endif /* UNIXOROSK */
  1190.     { "/error-action",         SND_ERR, CM_ARG },
  1191.     { "/except",               SND_EXC, CM_ARG },
  1192.     { "/filenames",            SND_NAM, CM_ARG },
  1193. #ifdef PIPESEND
  1194. #ifndef NOSPL
  1195.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1196. #endif /* NOSPL */
  1197. #endif /* PIPESEND */
  1198. #ifdef CKSYMLINK
  1199.     { "/followlinks",          SND_LNK, 0 },
  1200. #endif /* CKSYMLINK */
  1201. #ifdef VMS
  1202.     { "/image",                SND_IMG, 0 },
  1203. #else
  1204.     { "/image",                SND_BIN, CM_INV },
  1205. #endif /* VMS */
  1206.     { "/larger-than",          SND_LAR, CM_ARG },
  1207.     { "/listfile",             SND_FIL, CM_ARG },
  1208. #ifndef NOCSETS
  1209.     { "/local-character-set",  SND_CSL, CM_ARG },
  1210. #endif /* NOCSETS */
  1211. #ifdef CK_TMPDIR
  1212.     { "/move-to",              SND_MOV, CM_ARG },
  1213. #endif /* CK_TMPDIR */
  1214.     { "/nobackupfiles",        SND_NOB, 0 },
  1215. #ifdef UNIXOROSK
  1216.     { "/nodotfiles",           SND_NOD, 0 },
  1217. #endif /* UNIXOROSK */
  1218. #ifdef CKSYMLINK
  1219.     { "/nofollowlinks",        SND_NLK, 0 },
  1220. #endif /* CKSYMLINK */
  1221.     { "/not-after",            SND_NAF, CM_ARG },
  1222.     { "/not-before",           SND_NBE, CM_ARG },
  1223. #ifdef UNIX
  1224.     { "/permissions",          SND_PRM, CM_ARG },
  1225. #else
  1226.     { "/permissions",          SND_PRM, CM_INV },
  1227. #endif /* UNIX */
  1228.     { "/quiet",                SND_SHH, 0 },
  1229. #ifdef FTP_RESTART
  1230.     { "/recover",              SND_RES, 0 },
  1231. #endif /* FTP_RESTART */
  1232. #ifdef RECURSIVE
  1233.     { "/recursive",            SND_REC, 0 },
  1234. #endif /* RECURSIVE */
  1235.     { "/rename-to",            SND_REN, CM_ARG },
  1236. #ifdef FTP_RESTART
  1237.     { "/restart",              SND_RES, CM_INV },
  1238. #endif /* FTP_RESTART */
  1239. #ifndef NOCSETS
  1240.     { "/server-character-set", SND_CSR, CM_ARG },
  1241. #endif /* NOCSETS */
  1242.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1243.     { "/simulate",             SND_SIM, 0 },
  1244.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1245.     { "/smaller-than",         SND_SMA, CM_ARG },
  1246. #ifdef COMMENT
  1247.     { "/starting-at",          SND_STA, CM_ARG },
  1248. #endif /* COMMENT */
  1249. #ifdef RECURSIVE
  1250.     { "/subdirectories",       SND_REC, CM_INV },
  1251. #endif /* RECURSIVE */
  1252.     { "/tenex",                SND_TEN, 0 },
  1253.     { "/text",                 SND_TXT, 0 },
  1254. #ifndef NOCSETS
  1255.     { "/transparent",          SND_XPA, 0 },
  1256. #endif /* NOCSETS */
  1257.     { "/type",                 SND_TYP, CM_ARG },
  1258. #ifdef DOUPDATE
  1259.     { "/update",               SND_UPD, 0 },
  1260. #endif /* DOUPDATE */
  1261.     { "/unique-server-names",  SND_USN, 0 },
  1262.     { "", 0, 0 }
  1263. };
  1264. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1265.  
  1266. static struct keytab getswi[] = {       /* FTP GET switch table */
  1267.     { "/after",                SND_AFT, CM_INV },
  1268.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1269.     { "/as-name",              SND_ASN, CM_ARG },
  1270.     { "/ascii",                SND_TXT, CM_INV },
  1271.     { "/before",               SND_BEF, CM_INV },
  1272.     { "/binary",               SND_BIN, 0 },
  1273.     { "/collision",            SND_COL, CM_ARG },
  1274. #ifdef PUTPIPE
  1275.     { "/command",              SND_CMD, CM_PSH },
  1276. #endif /* PUTPIPE */
  1277.     { "/delete",               SND_DEL, 0 },
  1278.     { "/error-action",         SND_ERR, CM_ARG },
  1279.     { "/except",               SND_EXC, CM_ARG },
  1280.     { "/filenames",            SND_NAM, CM_ARG },
  1281. #ifdef PIPESEND
  1282. #ifndef NOSPL
  1283.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1284. #endif /* NOSPL */
  1285. #endif /* PIPESEND */
  1286. #ifdef VMS
  1287.     { "/image",                SND_IMG, 0 },
  1288. #else
  1289.     { "/image",                SND_BIN, CM_INV },
  1290. #endif /* VMS */
  1291.     { "/larger-than",          SND_LAR, CM_ARG },
  1292.     { "/listfile",             SND_FIL, CM_ARG },
  1293. #ifndef NOCSETS
  1294.     { "/local-character-set",  SND_CSL, CM_ARG },
  1295. #endif /* NOCSETS */
  1296. #ifdef CK_TMPDIR
  1297.     { "/move-to",              SND_MOV, CM_ARG },
  1298. #endif /* CK_TMPDIR */
  1299.     { "/namelist",             SND_NML, CM_ARG },
  1300.     { "/nobackupfiles",        SND_NOB, 0 },
  1301. #ifdef UNIXOROSK
  1302.     { "/nodotfiles",           SND_NOD, 0 },
  1303. #endif /* UNIXOROSK */
  1304.     { "/not-after",            SND_NAF, CM_INV },
  1305.     { "/not-before",           SND_NBE, CM_INV },
  1306.     { "/permissions",          SND_PRM, CM_INV },
  1307.     { "/quiet",                SND_SHH, 0 },
  1308. #ifdef FTP_RESTART
  1309.     { "/recover",              SND_RES, 0 },
  1310. #endif /* FTP_RESTART */
  1311. #ifdef RECURSIVE
  1312.     { "/recursive",            SND_REC, 0 },
  1313. #endif /* RECURSIVE */
  1314.     { "/rename-to",            SND_REN, CM_ARG },
  1315. #ifdef FTP_RESTART
  1316.     { "/restart",              SND_RES, CM_INV },
  1317. #endif /* FTP_RESTART */
  1318. #ifndef NOCSETS
  1319.     { "/server-character-set", SND_CSR, CM_ARG },
  1320. #endif /* NOCSETS */
  1321.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1322.     { "/smaller-than",         SND_SMA, CM_ARG },
  1323. #ifdef RECURSIVE
  1324.     { "/subdirectories",       SND_REC, CM_INV },
  1325. #endif /* RECURSIVE */
  1326.     { "/text",                 SND_TXT, 0 },
  1327.     { "/tenex",                SND_TEN, 0 },
  1328. #ifndef NOCSETS
  1329.     { "/transparent",          SND_XPA, 0 },
  1330. #endif /* NOCSETS */
  1331.     { "/to-screen",            SND_MAI, 0 },
  1332. #ifdef DOUPDATE
  1333.     { "/update",               SND_UPD, CM_INV },
  1334. #endif /* DOUPDATE */
  1335.     { "", 0, 0 }
  1336. };
  1337. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1338.  
  1339. static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
  1340.     { "/error-action",         SND_ERR, CM_ARG },
  1341.     { "/except",               SND_EXC, CM_ARG },
  1342.     { "/filenames",            SND_NAM, CM_ARG },
  1343.     { "/larger-than",          SND_LAR, CM_ARG },
  1344.     { "/nobackupfiles",        SND_NOB, 0 },
  1345. #ifdef UNIXOROSK
  1346.     { "/nodotfiles",           SND_NOD, 0 },
  1347. #endif /* UNIXOROSK */
  1348.     { "/quiet",                SND_SHH, 0 },
  1349. #ifdef RECURSIVE
  1350.     { "/recursive",            SND_REC, 0 },
  1351. #endif /* RECURSIVE */
  1352.     { "/smaller-than",         SND_SMA, CM_ARG },
  1353. #ifdef RECURSIVE
  1354.     { "/subdirectories",       SND_REC, CM_INV },
  1355. #endif /* RECURSIVE */
  1356.     { "", 0, 0 }
  1357. };
  1358. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1359.  
  1360. static struct keytab fntab[] = {        /* Filename conversion keyword table */
  1361.     { "automatic",    2, 0 },
  1362.     { "converted",    1, 0 },
  1363.     { "literal",      0, 0 }
  1364. };
  1365. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1366.  
  1367. static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
  1368.     { "ascii",        FTT_ASC, 0 },
  1369.     { "binary",       FTT_BIN, 0 },
  1370.     { "tenex",        FTT_TEN, 0 },
  1371.     { "text",         FTT_ASC, CM_INV },
  1372.     { "", 0, 0 }
  1373. };
  1374. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1375.  
  1376. #ifdef FTP_SECURITY
  1377. static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
  1378.     { "clear",        FPL_CLR, 0 },
  1379.     { "confidential", FPL_CON, 0 },
  1380.     { "private",      FPL_PRV, 0 },
  1381.     { "safe",         FPL_SAF, 0 },
  1382.     { "", 0, 0 }
  1383. };
  1384. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1385. #endif /* FTP_SECURITY */
  1386.  
  1387. /* Definitions for FTP from RFC765. */
  1388.  
  1389. /* Reply codes */
  1390.  
  1391. #define REPLY_PRELIM    1               /* Positive preliminary */
  1392. #define REPLY_COMPLETE  2               /* Positive completion */
  1393. #define REPLY_CONTINUE  3               /* Positive intermediate */
  1394. #define REPLY_TRANSIENT 4               /* Transient negative completion */
  1395. #define REPLY_ERROR     5               /* Permanent negative completion */
  1396. #define REPLY_SECURE    6               /* Security encoded message */
  1397.  
  1398. /* Form codes and names */
  1399.  
  1400. #define FORM_N 1                        /* Non-print */
  1401. #define FORM_T 2                        /* Telnet format effectors */
  1402. #define FORM_C 3                        /* Carriage control (ASA) */
  1403.  
  1404. /* Structure codes and names */
  1405.  
  1406. #define STRU_F 1                        /* File (no record structure) */
  1407. #define STRU_R 2                        /* Record structure */
  1408. #define STRU_P 3                        /* Page structure */
  1409.  
  1410. /* Mode types and names */
  1411.  
  1412. #define MODE_S 1                        /* Stream */
  1413. #define MODE_B 2                        /* Block */
  1414. #define MODE_C 3                        /* Compressed */
  1415.  
  1416. /* Protection levels and names */
  1417.  
  1418. #define PROT_C 1                        /* Clear */
  1419. #define PROT_S 2                        /* Safe */
  1420. #define PROT_P 3                        /* Private */
  1421. #define PROT_E 4                        /* Confidential */
  1422.  
  1423. #ifdef COMMENT                          /* Not used */
  1424. #ifdef FTP_NAMES
  1425. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1426. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1427. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1428. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1429. #endif /* FTP_NAMES */
  1430.  
  1431. /* Record Tokens */
  1432.  
  1433. #define REC_ESC '\377'                  /* Record-mode Escape */
  1434. #define REC_EOR '\001'                  /* Record-mode End-of-Record */
  1435. #define REC_EOF '\002'                  /* Record-mode End-of-File */
  1436.  
  1437. /* Block Header */
  1438.  
  1439. #define BLK_EOR           0x80          /* Block is End-of-Record */
  1440. #define BLK_EOF           0x40          /* Block is End-of-File */
  1441. #define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
  1442. #define BLK_RESTART       0x10          /* Block is Restart Marker */
  1443. #define BLK_BYTECOUNT 2                 /* Bytes in this block */
  1444. #endif /* COMMENT */
  1445.  
  1446. #define RADIX_ENCODE 0                  /* radix_encode() function codes */
  1447. #define RADIX_DECODE 1
  1448.  
  1449. /*
  1450.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1451.   results in a serious performance degradation due to the increased number
  1452.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1453.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1454.   of the typical TCP window.  Maybe we should add a command to allow the value
  1455.   to be changed.
  1456. */
  1457. #define DEFAULT_PBSZ 1<<13
  1458.  
  1459. /* Definitions and typedefs needed for prototypes */
  1460.  
  1461. #define sig_t my_sig_t
  1462. #define sigtype SIGTYP
  1463. typedef sigtype (*sig_t)();
  1464.  
  1465. /* Prototypes */
  1466.  
  1467. _PROTOTYP(int remtxt, (char **) );
  1468. _PROTOTYP(char * gskreason, (int) );
  1469. _PROTOTYP(static int ftpclose,(void));
  1470. _PROTOTYP(static int zzsend, (int, CHAR));
  1471. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1472. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1473. _PROTOTYP(static int setpbsz,(unsigned int));
  1474. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1475.   int,int,char *,int,int,int));
  1476. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1477. _PROTOTYP(static int fts_cpl,(int));
  1478. _PROTOTYP(static int fts_dpl,(int));
  1479. #ifdef FTP_SECURITY
  1480. _PROTOTYP(static int ftp_auth, (void));
  1481. #endif /* FTP_SECURITY */
  1482. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1483. _PROTOTYP(static int ftp_login, (char *));
  1484. _PROTOTYP(static int ftp_reset, (void));
  1485. _PROTOTYP(static int ftp_rename, (char *, char *));
  1486. _PROTOTYP(static int ftp_umask, (char *));
  1487. _PROTOTYP(static int secure_flush, (int));
  1488. #ifdef COMMENT
  1489. _PROTOTYP(static int secure_putc, (char, int));
  1490. #endif /* COMMENT */
  1491. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1492. _PROTOTYP(static int scommand, (char *));
  1493. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1494. _PROTOTYP(static int secure_getc, (int));
  1495. _PROTOTYP(static int secure_read, (int, char *, int));
  1496. _PROTOTYP(static int initconn, (void));
  1497. _PROTOTYP(static int dataconn, (char *));
  1498. _PROTOTYP(static int setprotbuf,(unsigned int));
  1499. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1500.  
  1501. _PROTOTYP(static char * radix_error,(int));
  1502. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1503. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, int));
  1504.  
  1505. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1506. _PROTOTYP(static VOID lostpeer, (void));
  1507. _PROTOTYP(static VOID cancel_remote, (int));
  1508. _PROTOTYP(static VOID changetype, (int, int));
  1509.  
  1510. _PROTOTYP(static sigtype cmdcancel, (int));
  1511.  
  1512. #ifdef FTP_SRP
  1513. _PROTOTYP(static int srp_reset, ());
  1514. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1515. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1516. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1517. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1518. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1519. _PROTOTYP(static int srp_selcipher, (char *));
  1520. _PROTOTYP(static int srp_selhash, (char *));
  1521. #endif /* FTP_SRP */
  1522.  
  1523. #ifdef FTP_GSSAPI
  1524. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1525. #endif /* FTP_GSSAPI */
  1526.  
  1527. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1528.  
  1529. #ifdef FTP_SECURITY
  1530. #ifndef NOICP
  1531. #define FT_NOGSS   1
  1532. #define FT_NOK4    2
  1533. #define FT_NOSRP   3
  1534. #define FT_NOSSL   4
  1535. #define FT_NOTLS   5
  1536. #define FT_CERTFI  6
  1537. #define FT_OKCERT  7
  1538. #define FT_DEBUG   8
  1539. #define FT_KEY     9
  1540. #define FT_SECURE 10
  1541. #define FT_VERIFY 11
  1542.  
  1543. static struct keytab ftpztab[] = {
  1544.     { "!gss",    FT_NOGSS,  0 },
  1545.     { "!krb4",   FT_NOK4,   0 },
  1546.     { "!srp",    FT_NOSRP,  0 },
  1547.     { "!ssl",    FT_NOSSL,  0 },
  1548.     { "!tls",    FT_NOTLS,  0 },
  1549.     { "cert",    FT_CERTFI, CM_ARG },
  1550.     { "certsok", FT_OKCERT, 0 },
  1551.     { "debug",   FT_DEBUG,  0 },
  1552.     { "key",     FT_KEY,    CM_ARG },
  1553.     { "nogss",   FT_NOGSS,  0 },
  1554.     { "nokrb4",  FT_NOK4,   0 },
  1555.     { "nosrp",   FT_NOSRP,  0 },
  1556.     { "nossl",   FT_NOSSL,  0 },
  1557.     { "notls",   FT_NOTLS,  0 },
  1558. #ifdef COMMENT
  1559.     { "secure",  FT_SECURE, 0 },
  1560. #endif /* COMMENT */
  1561.     { "verify",  FT_VERIFY, CM_ARG },
  1562.     { "", 0, 0 }
  1563. };
  1564. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1565.  
  1566. /*
  1567.   The following cipher and hash tables should be replaced with
  1568.   dynamicly created versions based upon the linked library.
  1569. */
  1570. #define SRP_BLOWFISH_ECB    1
  1571. #define SRP_BLOWFISH_CBC    2
  1572. #define SRP_BLOWFISH_CFB64  3
  1573. #define SRP_BLOWFISH_OFB64  4
  1574. #define SRP_CAST5_ECB       5
  1575. #define SRP_CAST5_CBC       6
  1576. #define SRP_CAST5_CFB64     7
  1577. #define SRP_CAST5_OFB64     8
  1578. #define SRP_DES_ECB         9
  1579. #define SRP_DES_CBC        10
  1580. #define SRP_DES_CFB64      11
  1581. #define SRP_DES_OFB64      12
  1582. #define SRP_DES3_ECB       13
  1583. #define SRP_DES3_CBC       14
  1584. #define SRP_DES3_CFB64     15
  1585. #define SRP_DES3_OFB64     16
  1586.  
  1587. static struct keytab ciphertab[] = {
  1588.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1589.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1590.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1591.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1592.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1593.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1594.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1595.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1596.     { "des_ecb",        SRP_DES_ECB,        0 },
  1597.     { "des_cbc",        SRP_DES_CBC,        0 },
  1598.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1599.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1600.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1601.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1602.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1603.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1604.     { "none",           0, 0 },
  1605.     { "", 0, 0 }
  1606. };
  1607. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1608.  
  1609. #define SRP_MD5  1
  1610. #define SRP_SHA  2
  1611. static struct keytab hashtab[] = {
  1612.     { "md5",              SRP_MD5,        0 },
  1613.     { "none",             0,              0 },
  1614.     { "sha",              SRP_SHA,        0 },
  1615.     { "", 0, 0 }
  1616. };
  1617. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1618. #endif /* NOICP */
  1619. #endif /* FTP_SECURITY */
  1620.  
  1621. static char *
  1622. strval(s1,s2) char * s1, * s2; {
  1623.     if (!s1) s1 = "";
  1624.     if (!s2) s2 = "";
  1625.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1626. }
  1627.  
  1628. #ifndef NOCSETS
  1629. static char * rfnptr = NULL;
  1630. static int rfnlen = 0;
  1631. static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
  1632. static char * xgnbp = NULL;
  1633.  
  1634. static int
  1635. strgetc() {                             /* Helper function for xgnbyte() */
  1636.     int c;
  1637.     if (!xgnbp)
  1638.       return(-1);
  1639.     if (!*xgnbp)
  1640.       return(-1);
  1641.     c = (unsigned) *xgnbp++;
  1642.     return(((unsigned) c) & 0xff);
  1643. }
  1644.  
  1645. static int                              /* Helper function for xpnbyte() */
  1646. #ifdef CK_ANSIC
  1647. strputc(char c)
  1648. #else
  1649. strputc(c) char c;
  1650. #endif /* CK_ANSIC */
  1651. {
  1652.     rfnlen = rfnptr - rfnbuf;
  1653.     if (rfnlen >= (RFNBUFSIZ - 1))
  1654.       return(-1);
  1655.     *rfnptr++ = c;
  1656.     *rfnptr = NUL;
  1657.     return(0);
  1658. }
  1659.  
  1660. static int
  1661. #ifdef CK_ANSIC
  1662. xprintc(char c)
  1663. #else
  1664. xprintc(c) char c;
  1665. #endif /* CK_ANSIC */
  1666. {
  1667.     printf("%c",c);
  1668.     return(0);
  1669. }
  1670. #endif /* NOCSETS */
  1671.  
  1672.  
  1673. #ifdef CKLOGDIAL
  1674. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  1675. int ftplogactive = 0;
  1676. long ftplogprev = 0L;
  1677.  
  1678. VOID
  1679. ftplogend() {
  1680.     extern int dialog;
  1681.     extern char diafil[];
  1682.     long d1, d2, t1, t2;
  1683.     char buf[32], * p;
  1684.  
  1685.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  1686.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  1687.  
  1688.     if (!ftplogactive || !ftplogbuf[0]) /* No active record */
  1689.       return;
  1690.  
  1691.     ftplogactive = 0;                   /* Record is not active */
  1692.  
  1693.     d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
  1694.     ckstrncpy(buf,ckdate(),31);         /* Get current date */
  1695.     d2 = mjd(buf);                      /* Convert them to mjds */
  1696.     p = ftplogbuf;                      /* Get start time */
  1697.     p[11] = NUL;
  1698.     p[14] = NUL;                        /* Convert to seconds */
  1699.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1700.     p[11] = ':';
  1701.     p[14] = ':';
  1702.     p = buf;                            /* Get end time */
  1703.     p[11] = NUL;
  1704.     p[14] = NUL;
  1705.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1706.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  1707.     if (t2 > -1L) {
  1708.         ftplogprev = t2;
  1709.         p = hhmmss(t2);
  1710.         strncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  1711.         strncat(ftplogbuf,p,CXLOGBUFL);
  1712.     } else
  1713.       ftplogprev = 0L;
  1714.     debug(F101,"ftp cx log dialog","",dialog);
  1715.     if (dialog) {                       /* If logging */
  1716.         int x;
  1717.         x = diaopn(diafil,1,1);         /* Open log in append mode */
  1718.         if (x > 0) {
  1719.             debug(F101,"ftp cx log open","",x);
  1720.             x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  1721.             debug(F101,"ftp cx log write","",x);
  1722.             x = zclose(ZDIFIL);         /* Close the log */
  1723.             debug(F101,"ftp cx log close","",x);
  1724.         }
  1725.     }
  1726. }
  1727.  
  1728. VOID
  1729. dologftp() {
  1730.     ftplogend();                        /* Previous session not closed out? */
  1731.     ftplogprev = 0L;
  1732.     ftplogactive = 1;                   /* Record is active */
  1733.  
  1734.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  1735.               ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  1736.               " T=FTP N=", strval(ftp_host,NULL)," H=",myhost," ",NULL,NULL);
  1737.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  1738. }
  1739. #endif /* CKLOGDIAL */
  1740.  
  1741. static char * dummy[2] = { NULL, NULL };
  1742.  
  1743. static struct keytab modetab[] = {
  1744.     { "active",  0, 0 },
  1745.     { "passive", 1, 0 }
  1746. };
  1747.  
  1748. #ifndef NOCMDL
  1749. int                                     /* Called from ckuusy.c */
  1750. #ifdef CK_ANSIC
  1751. doftparg(char c)
  1752. #else
  1753. doftparg(c) char c;
  1754. #endif /* CK_ANSIC */
  1755. /* doftparg */ {
  1756.     int x, z;
  1757.     char *xp;
  1758.     extern char **xargv, *xarg0;
  1759.     extern int xargc, stayflg, haveftpuid;
  1760.     extern char uidbuf[];
  1761.  
  1762.     xp = *xargv+1;                      /* Pointer for bundled args */
  1763.     while (c) {
  1764.         if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
  1765.             if (*(xp+1)) {
  1766.                 fatal("?Invalid argument bundling");
  1767.             }
  1768.             xargv++, xargc--;
  1769.             if ((xargc < 1) || (**xargv == '-')) {
  1770.                 fatal("?Required argument missing");
  1771.             }
  1772.         }
  1773.         switch (c) {                    /* Big switch on arg */
  1774.           case 'h':                     /* help */
  1775.            printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  1776.             printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  1777.             printf("Options:\n");
  1778.             printf("  -h           = help (this message)\n");
  1779.             printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  1780.             printf("  -u name      = username for autologin (or -M)\n");
  1781.             printf("  -P password  = password for autologin (RISKY)\n");
  1782.             printf("  -A           = autologin anonymously\n");
  1783.             printf("  -D directory = cd after autologin\n");
  1784.             printf("  -b           = force binary mode\n");
  1785.             printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  1786.             printf("  -d           = debug (double to add timestamps)\n");
  1787.             printf("  -n           = no autologin\n");
  1788.             printf("  -v           = verbose (default)\n");
  1789.             printf("  -q           = quiet\n");
  1790.             printf("  -S           = Stay (issue command prompt when done)\n");
  1791.             printf("  -Y           = do not execute Kermit init file\n");
  1792.             printf("  -p files     = files to put after autologin (or -s)\n");
  1793.             printf("  -g files     = files to get after autologin\n");
  1794.             printf("  -R           = recursive (for use with -p)\n");
  1795.  
  1796. #ifdef FTP_SECURITY
  1797.             printf("\nSecurity options:\n");
  1798.             printf("  -k realm     = Kerberos 4 realm\n");
  1799.             printf("  -f           = Kerboros 5 credentials forwarding\n");
  1800.             printf("  -x           = autoencryption mode\n");
  1801.             printf("  -c cipher    = SRP cipher type\n");
  1802.             printf("  -H hash      = SRP encryption hash\n");
  1803.             printf("  -z option    = Security options\n");
  1804. #endif /* FTP_SECURITY */
  1805.  
  1806.             printf("\n-p or -g, if given, should be last.  Example:\n");
  1807.             printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  1808.  
  1809.             doexit(GOOD_EXIT,-1);
  1810.             break;
  1811.  
  1812.           case 'R':                     /* Recursive */
  1813.             recursive = 1;
  1814.             break;
  1815.  
  1816.           case 'd':                     /* Debug */
  1817. #ifdef DEBUG
  1818.             if (deblog) {
  1819.                 extern int debtim;
  1820.                 debtim = 1;
  1821.             } else {
  1822.                 deblog = debopn("debug.log",0);
  1823.                 debok = 1;
  1824.             }
  1825. #endif /* DEBUG */
  1826.             /* fall thru on purpose */
  1827.  
  1828.           case 't':                     /* Trace */
  1829.             ftp_deb++;
  1830.             break;
  1831.  
  1832.           case 'n':                     /* No autologin */
  1833.             ftp_log = 0;
  1834.             break;
  1835.  
  1836.           case 'i':                     /* No prompt */
  1837.           case 'v':                     /* Verbose */
  1838.             break;                      /* (ignored) */
  1839.  
  1840.           case 'q':                     /* Quiet */
  1841.             quiet = 1;
  1842.             break;
  1843.  
  1844.           case 'S':                     /* Stay */
  1845.             stayflg = 1;
  1846.             break;
  1847.  
  1848.           case 'M':
  1849.           case 'u':                     /* My User Name */
  1850.             if ((int)strlen(*xargv) > 63) {
  1851.                 fatal("username too long");
  1852.             }
  1853.             ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  1854.             haveftpuid = 1;
  1855.             break;
  1856.  
  1857.           case 'A':
  1858.             ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  1859.             haveftpuid = 1;
  1860.             break;
  1861.  
  1862.           case 'T':                     /* Text */
  1863.           case 'a':                     /* "ascii" */
  1864.           case 'b':                     /* Binary */
  1865.             binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  1866.             xfermode = XMODE_M;
  1867.             filepeek = 0;
  1868.             patterns = 0;
  1869.             break;
  1870.  
  1871.           case 'g':                     /* Get */
  1872.           case 'p':                     /* Put */
  1873.           case 's': {                   /* Send (= Put) */
  1874.               int havefiles, rc;
  1875.               if (ftp_action) {
  1876.                   fatal("Only one FTP action at a time please");
  1877.               }
  1878.               if (*(xp+1)) {
  1879.                   fatal("invalid argument bundling after -s");
  1880.               }
  1881.               nfils = 0;                /* Initialize file counter */
  1882.               havefiles = 0;            /* Assume nothing to send  */
  1883.               cmlist = xargv + 1;       /* Remember this pointer */
  1884.  
  1885.               while (++xargv, --xargc > 0) { /* Traverse the list */
  1886.                   if (c == 'g') {
  1887.                       havefiles++;
  1888.                       nfils++;
  1889.                       continue;
  1890.                   }
  1891. #ifdef RECURSIVE
  1892.                   if (!strcmp(*xargv,".")) {
  1893.                       havefiles = 1;
  1894.                       nfils++;
  1895.                       recursive = 1;
  1896.                   } else
  1897. #endif /* RECURSIVE */
  1898.                     if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  1899.                         if  (rc != -2)
  1900.                           havefiles = 1;
  1901.                         nfils++;
  1902.                     } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  1903.                         havefiles = 1;
  1904.                         nfils++;
  1905.                     }
  1906.               }
  1907.               xargc++, xargv--;         /* Adjust argv/argc */
  1908.               if (!havefiles) {
  1909.                   if (c == 'g') {
  1910.                       fatal("No files to put");
  1911.                   } else {
  1912.                       fatal("No files to get");
  1913.                   }
  1914.               }
  1915.               ftp_action = c;
  1916.               break;
  1917.           }
  1918.           case 'D':                     /* Directory */
  1919.             makestr(&ftp_rdir,*xargv);
  1920.             break;
  1921.  
  1922.           case 'm':                     /* Mode (Active/Passive */
  1923.             ftp_psv = lookup(modetab,*xargv,2,NULL);
  1924.             if (ftp_psv < 0) fatal("Invalid mode");
  1925.             break;
  1926.  
  1927.           case 'P':
  1928.             makestr(&ftp_tmp,*xargv);   /* You-Know-What */
  1929.             break;
  1930.  
  1931.           case 'Y':                     /* No initialization file */
  1932.             break;                      /* (already done in prescan) */
  1933.  
  1934. #ifdef CK_URL
  1935.           case 'U': {                   /* URL */
  1936.               /* These are set by urlparse() - any not set are NULL */
  1937.               extern struct urldata g_url;
  1938.               if (g_url.hos) {
  1939. /*
  1940.   Kermit has accepted host:port notation since many years before URLs were
  1941.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  1942.   host:449" looks like a URL and results in service = host and host = 449.
  1943.   Here we try to catch this situation transparently to the user.
  1944. */
  1945.                   if (ckstrcmp(g_url.svc,"ftp",-1,0)
  1946. #ifdef CK_SSL
  1947.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  1948. #endif /* CK_SSL */
  1949.                        ) {
  1950.                       if (!g_url.usr &&
  1951.                           !g_url.psw &&
  1952.                           !g_url.por &&
  1953.                           !g_url.pth) {
  1954.                           g_url.por = g_url.hos;
  1955.                           g_url.hos = g_url.svc;
  1956.                           g_url.svc = "ftp";
  1957.                       } else {
  1958.                           ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  1959.                                    g_url.svc," host=",g_url.hos);
  1960.                           fatal(tmpbuf);
  1961.                       }
  1962.                   }
  1963.                   makestr(&ftp_host,g_url.hos);
  1964.                   if (g_url.usr) {
  1965.                       haveftpuid = 1;
  1966.                       ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  1967.                       makestr(&ftp_logname,uidbuf);
  1968.                   }
  1969.                   if (g_url.psw) {
  1970.                       makestr(&ftp_tmp,g_url.psw);
  1971.                   }
  1972.                   if (g_url.pth) {
  1973.                       if (!g_url.usr) {
  1974.                           haveftpuid = 1;
  1975.                           ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  1976.                           makestr(&ftp_logname,uidbuf);
  1977.                       }
  1978.                       if (ftp_action) {
  1979.                           fatal("Only one FTP action at a time please");
  1980.                       }
  1981.                       if (!stayflg)
  1982.                         quiet = 1;
  1983.                       nfils = 1;
  1984.                       dummy[0] = g_url.pth;
  1985.                       cmlist = dummy;
  1986.                       ftp_action = 'g';
  1987.                   }
  1988.                   xp = NULL;
  1989.                   haveurl = 1;
  1990.               }
  1991.               break;
  1992.           }
  1993. #endif /* CK_URL */
  1994.  
  1995. #ifdef FTP_SECURITY
  1996.           case 'k': {                   /* K4 Realm */
  1997. #ifdef FTP_KRB4
  1998.               ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  1999. #endif /* FTP_KRB4 */
  2000.               if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  2001.               break;
  2002.           }
  2003.           case 'f': {
  2004. #ifdef FTP_GSSAPI
  2005.               ftp_cfw = 1;
  2006.               if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2007. #else /* FTP_GSSAPI */
  2008.               printf("K5 Credentials Forwarding not supported\n");
  2009. #endif /* FTP_GSSAPI */
  2010.               break;
  2011.           }
  2012.           case 'x': {
  2013.               ftp_cry = 1;
  2014.               if (ftp_deb) printf("Autoencryption\n");
  2015.               break;
  2016.           }
  2017.           case 'c': {                   /* Cipher */
  2018. #ifdef FTP_SRP
  2019.               if (!srp_selcipher(*xargv)) {
  2020.                   if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2021.               } else
  2022.                 printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2023. #else /* FTP_SRP */
  2024.               printf("?SRP not supported\n");
  2025. #endif /* FTP_SRP */
  2026.               break;
  2027.           }
  2028.           case 'H': {
  2029. #ifdef FTP_SRP
  2030.               if (!srp_selhash(*xargv)) {
  2031.                   if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2032.               } else
  2033.                 printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2034. #else /* FTP_SRP */
  2035.               printf("?SRP not supported\n");
  2036. #endif /* FTP_SRP */
  2037.               break;
  2038.           }
  2039.           case 'z': {
  2040.               /* *xargv contains a value of the form tag=value */
  2041.               /* we need to lookup the tag and save the value  */
  2042.               char * p, * q;
  2043.               makestr(&p,*xargv);
  2044.               y = ckindex("=",p,0,0,1);
  2045.               if (y > 0)
  2046.                 p[y-1] = '\0';
  2047.               x = lookup(ftpztab,p,nftpztab,&z);
  2048.               if (x < 0) {
  2049.                   printf("?Invalid security option: \"%s\"\n",p);
  2050.               } else {
  2051.                   printf("Security option: \"%s",p);
  2052.                   if (ftpztab[z].flgs & CM_ARG) {
  2053.                       q = &p[y];
  2054.                       if (!*q)
  2055.                         fatal("?Missing required value");
  2056.                       if (ftp_deb) printf("=%s\"",q);
  2057.                   }
  2058.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2059.                     case FT_NOGSS:
  2060. #ifdef FTP_GSSAPI
  2061.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2062.                           if (ftp_auth_type[z] == FTA_GK5) {
  2063.                               for (y = z;
  2064.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2065.                                    y++
  2066.                                    )
  2067.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2068.                               ftp_auth_type[FTPATYPS-1] = 0;
  2069.                               break;
  2070.                           }
  2071.                       }
  2072. #endif /* FTP_GSSAPI */
  2073.                       break;
  2074.                     case FT_NOK4:
  2075. #ifdef FTP_KRB4
  2076.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2077.                           if (ftp_auth_type[z] == FTA_K4) {
  2078.                               for (y = z;
  2079.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2080.                                    y++
  2081.                                    )
  2082.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2083.                               ftp_auth_type[FTPATYPS-1] = 0;
  2084.                               break;
  2085.                           }
  2086.                       }
  2087. #endif /* FTP_KRB4 */
  2088.                       break;
  2089.                     case FT_NOSRP:
  2090. #ifdef FTP_SRP
  2091.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2092.                           if (ftp_auth_type[z] == FTA_SRP) {
  2093.                               for (y = z;
  2094.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2095.                                    y++
  2096.                                    )
  2097.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2098.                               ftp_auth_type[FTPATYPS-1] = 0;
  2099.                               break;
  2100.                           }
  2101.                       }
  2102. #endif /* FTP_SRP */
  2103.                       break;
  2104.                     case FT_NOSSL:
  2105. #ifdef CK_SSL
  2106.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2107.                           if (ftp_auth_type[z] == FTA_SSL) {
  2108.                               for (y = z;
  2109.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2110.                                    y++
  2111.                                    )
  2112.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2113.                               ftp_auth_type[FTPATYPS-1] = 0;
  2114.                               break;
  2115.                           }
  2116.                       }
  2117. #endif /* CK_SSL */
  2118.                       break;
  2119.                     case FT_NOTLS:
  2120. #ifdef CK_SSL
  2121.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2122.                           if (ftp_auth_type[z] == FTA_TLS) {
  2123.                               for (y = z;
  2124.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2125.                                    y++
  2126.                                    )
  2127.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2128.                               ftp_auth_type[FTPATYPS-1] = 0;
  2129.                               break;
  2130.                           }
  2131.                       }
  2132. #endif /* CK_SSL */
  2133.                       break;
  2134.                     case FT_CERTFI:
  2135. #ifdef CK_SSL
  2136.                       makestr(&ssl_rsa_cert_file,q);
  2137. #endif /* CK_SSL */
  2138.                       break;
  2139.                     case FT_OKCERT:
  2140. #ifdef CK_SSL
  2141.                       ssl_certsok_flag = 1;
  2142. #endif /* CK_SSL */
  2143.                       break;
  2144.                     case FT_DEBUG:
  2145. #ifdef DEBUG
  2146.                       if (deblog) {
  2147.                           extern int debtim;
  2148.                           debtim = 1;
  2149.                       } else {
  2150.                           deblog = debopn("debug.log",0);
  2151.                       }
  2152. #endif /* DEBUG */
  2153.                       break;
  2154.                     case FT_KEY:
  2155. #ifdef CK_SSL
  2156.                       makestr(&ssl_rsa_key_file,q);
  2157. #endif /* CK_SSL */
  2158.                       break;
  2159.                     case FT_SECURE:
  2160.                       /* no equivalent */
  2161.                       break;
  2162.                     case FT_VERIFY:
  2163. #ifdef CK_SSL
  2164.                       if (!rdigits(q))
  2165.                         printf("?Bad number: %s\n",q);
  2166.                       ssl_verify_flag = atoi(q);
  2167. #endif /* CK_SSL */
  2168.                       break;
  2169.                   }
  2170.               }
  2171.               if (ftp_deb) printf("\"\n");
  2172.               free(p);
  2173.               break;
  2174.           }
  2175. #endif /* FTP_SECURITY */
  2176.  
  2177.           default:
  2178.             fatal2(*xargv,
  2179.                    "unknown command-line option, type \"ftp -h\" for help"
  2180.                    );
  2181.         }
  2182.         if (!xp) break;
  2183.         c = *++xp;                      /* See if options are bundled */
  2184.     }
  2185.     return(0);
  2186. }
  2187. #endif /* NOCMDL */
  2188.  
  2189. int
  2190. ftpisconnected() {
  2191.     return(connected);
  2192. }
  2193.  
  2194. int
  2195. ftpisloggedin() {
  2196.     return(connected ? loggedin : 0);
  2197. }
  2198.  
  2199. int
  2200. ftpissecure() {
  2201.     return((ftp_dpl == FPL_CLR) ? 0 : 1);
  2202. }
  2203.  
  2204. static VOID
  2205. ftscreen(n, c, z, s) int n; char c; long z; char * s; {
  2206.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2207.         if (!dpyactive) {
  2208.             ckscreen(SCR_PT,'S',0L,"");
  2209.             dpyactive = 1;
  2210.         }
  2211.         ckscreen(n,c,z,s);
  2212.     }
  2213. }
  2214.  
  2215. #ifndef OS2
  2216. /*  g m s t i m e r  --  Millisecond timer */
  2217.  
  2218. long
  2219. gmstimer() {
  2220. #ifdef HAVE_MSECS
  2221.     /* For those versions of ztime() that also set global ztmsec. */
  2222.     char *p = NULL;
  2223.     long z;
  2224.     ztime(&p);
  2225.     if (!p) return(0L);
  2226.     if (!*p) return(0L);
  2227.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2228.     return(z * 1000 + ztmsec);
  2229. #else
  2230.     return((long)time(NULL) * 1000L);
  2231. #endif /* HAVE_MSECS */
  2232. }
  2233. #endif /* OS2 */
  2234.  
  2235. /*  d o s e t f t p  --  The SET FTP command  */
  2236.  
  2237. int
  2238. dosetftp() {
  2239.     int cx;
  2240.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2241.       return(cx);
  2242.     switch (cx) {
  2243.  
  2244.       case FTS_FNC:                     /* Filename collision action */
  2245.         if ((x = cmkey(colxtab,ncolx,"","",xxstring)) < 0)
  2246.           return(x);
  2247.         if ((y = cmcfm()) < 0)
  2248.           return(y);
  2249.         ftp_fnc = x;
  2250.         return(1);
  2251.  
  2252.       case FTS_CNV:                     /* Filename conversion */
  2253.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2254.           return(x);
  2255.         if ((y = cmcfm()) < 0)
  2256.           return(y);
  2257.         ftp_cnv = x;
  2258.         return(1);
  2259.  
  2260.       case FTS_DBG:                     /* Debug messages */
  2261.         return(seton(&ftp_deb));
  2262.  
  2263.       case FTS_LOG:                     /* Auto-login */
  2264.         return(seton(&ftp_log));
  2265.  
  2266.       case FTS_PSV:                     /* Passive mode */
  2267.         x = seton(&ftp_psv);
  2268.         if (x) passivemode = ftp_psv;
  2269.         return(x);
  2270.  
  2271.       case FTS_SPC:                     /* Send port commands */
  2272.         x = seton(&ftp_spc);
  2273.         if (x) sendport = ftp_spc;
  2274.         return(x);
  2275.  
  2276.       case FTS_TYP:                     /* Type */
  2277.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2278.           return(x);
  2279.         if ((y = cmcfm()) < 0) return(y);
  2280.         ftp_typ = x;
  2281.         g_ftp_typ = x;
  2282.         tenex = (ftp_typ == FTT_TEN);
  2283.         return(1);
  2284.  
  2285.       case FTS_USN:                     /* Unique server names */
  2286.         return(seton(&ftp_usn));
  2287.  
  2288.       case FTS_VBM:                     /* Verbose mode */
  2289.         if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
  2290.           return(x);
  2291.         ftp_vbx = ftp_vbm;              /* Global sticky copy */
  2292.         return(x);
  2293.  
  2294.       case FTS_TST:                     /* "if (testing)" messages */
  2295.         return(seton(&testing));
  2296.  
  2297.       case FTS_PRM:                     /* Send permissions */
  2298.         return(setonaut(&ftp_prm));
  2299.  
  2300.       case FTS_AUT:                     /* Auto-authentication */
  2301.         return(seton(&ftp_aut));
  2302.  
  2303.       case FTS_ERR:                     /* Error action */
  2304.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2305.           return(x);
  2306.         if ((y = cmcfm()) < 0)
  2307.           return(y);
  2308.         ftp_err = x;
  2309.         return(success = 1);
  2310.  
  2311. #ifndef NOCSETS
  2312.       case FTS_XLA:                     /* Translation */
  2313.         return(seton(&ftp_xla));
  2314.  
  2315.       case FTS_CSR:                     /* Server charset */
  2316.         if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2317.           return(x);
  2318.         if ((y = cmcfm()) < 0)
  2319.           return(y);
  2320.         ftp_csr = x;
  2321.         ftp_xla = 1;                    /* Also enable translation */
  2322.         return(success = 1);
  2323. #endif /* NOCSETS */
  2324.  
  2325.       case FTS_GFT:
  2326.         return(seton(&get_auto));       /* GET-filetype-switching */
  2327.  
  2328.       case FTS_DAT:
  2329.         return(seton(&ftp_dates));      /* Set file dates */
  2330.  
  2331. #ifdef FTP_SECURITY
  2332.       case FTS_CRY:                     /* Auto-encryption */
  2333.         return(seton(&ftp_cry));
  2334.  
  2335.       case FTS_CFW:                     /* Credential-forwarding */
  2336.         return(seton(&ftp_cfw));
  2337.  
  2338.       case FTS_CPL:                     /* Command protection level */
  2339.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2340.         if ((y = cmcfm()) < 0) return(y);
  2341.         success = fts_cpl(x);
  2342.         return(success);
  2343.  
  2344.       case FTS_DPL:                     /* Data protection level */
  2345.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2346.         if ((y = cmcfm()) < 0) return(y);
  2347.           success = fts_dpl(x);
  2348.           return(success);
  2349.  
  2350.       case FTS_ATP: {                   /* FTP Auth Type */
  2351.           int i, j, atypes[8];
  2352.  
  2353.           for (i = 0; i < 8; i++) {
  2354.               if ((y = cmkey(ftpauth,nftpauth,"",
  2355.                              (i == 0) ? "automatic" : "",
  2356.                              xxstring)) < 0) {
  2357.                   if (y == -3)
  2358.                     break;
  2359.                   return(y);
  2360.               }
  2361.               if (i > 0 && (y == FTA_AUTO)) {
  2362.                   printf("?Choice may only be used in first position.\r\n");
  2363.                   return(-9);
  2364.               }
  2365.               for (j = 0; j < i; j++) {
  2366.                   if (atypes[j] == y) {
  2367.                       printf("\r\n?Choice has already been used.\r\n");
  2368.                       return(-9);
  2369.                   }
  2370.               }
  2371.               atypes[i] = y;
  2372.               if (y == FTA_AUTO) {
  2373.                   i++;
  2374.                   break;
  2375.               }
  2376.           }
  2377.           if (i < 8)
  2378.             atypes[i] = 0;
  2379.           if ((z = cmcfm()) < 0)
  2380.             return(z);
  2381.           if (atypes[0] == FTA_AUTO) {
  2382.               i = 0;
  2383. #ifdef FTP_GSSAPI
  2384.               ftp_auth_type[i++] = FTA_GK5;
  2385. #endif /* FTP_GSSAPI */
  2386. #ifdef FTP_SRP
  2387.               ftp_auth_type[i++] = FTA_SRP;
  2388. #endif /* FTP_SRP */
  2389. #ifdef FTP_KRB4
  2390.               ftp_auth_type[i++] = FTA_K4;
  2391. #endif /* FTP_KRB4 */
  2392. #ifdef CK_SSL
  2393.               ftp_auth_type[i++] = FTA_TLS;
  2394.               ftp_auth_type[i++] = FTA_SSL;
  2395. #endif /* CK_SSL */
  2396.               ftp_auth_type[i] = 0;
  2397.           } else {
  2398.               for (i = 0; i < 8; i++)
  2399.                 ftp_auth_type[i] = atypes[i];
  2400.           }
  2401.           return(success = 1);
  2402.       }
  2403.  
  2404.       case FTS_SRP:
  2405. #ifdef FTP_SRP
  2406.         if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2407.           return(x);
  2408.         switch (x) {
  2409.           case SRP_CIPHER:
  2410.             if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2411.               return(x);
  2412.             if ((z = cmcfm()) < 0)
  2413.               return(z);
  2414.             success = !srp_selcipher(ciphertab[x].kwd);
  2415.             return(success);
  2416.           case SRP_HASH:
  2417.             if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2418.               return(x);
  2419.             if ((z = cmcfm()) < 0)
  2420.               return(z);
  2421.             success = !srp_selhash(hashtab[x].kwd);
  2422.             return(success = 1);
  2423.           default:
  2424.             if ((z = cmcfm()) < 0)
  2425.               return(z);
  2426.             return(-2);
  2427.         }
  2428. #else /* FTP_SRP */
  2429.         if ((z = cmcfm()) < 0)
  2430.           return(z);
  2431.         return(-2);
  2432. #endif /* FTP_SRP */
  2433. #endif /* FTP_SECURITY */
  2434.  
  2435.       default:
  2436.         return(-2);
  2437.     }
  2438. }
  2439.  
  2440. int
  2441. ftpbye() {
  2442.     int x;
  2443.     if (!connected)
  2444.       return(1);
  2445.     if (testing)
  2446.       printf(" ftp closing %s...\n",ftp_host);
  2447.     x = ftpclose();
  2448.     return((x > -1) ? 1 : 0);
  2449. }
  2450.  
  2451. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2452.  
  2453. static int
  2454. openftp(s,opn_tls) char * s; int opn_tls; {
  2455.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2456.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1;
  2457.     struct FDB sw, fl, cm;
  2458.     extern int nnetdir;                 /* Network services directory */
  2459.     extern int nhcount;                 /* Lookup result */
  2460.     extern char *nh_p[];                /* Network directory entry pointers */
  2461.     extern char *nh_p2[];               /* Network directory entry nettype */
  2462.  
  2463.     if (!s) return(-2);
  2464.     if (!*s) return(-2);
  2465.  
  2466.     makestr(&hostname,s);
  2467.     hostsave = hostname;
  2468.     makestr(&ftp_logname,NULL);
  2469.     anonymous = 0;
  2470.     noinit = 0;
  2471.  
  2472.     debug(F110,"ftp open",hostname,0);
  2473.  
  2474.     if (sav_psv > -1) {                 /* Restore prevailing active/passive */
  2475.         ftp_psv = sav_psv;              /* selection in case it was */
  2476.         sav_psv = -1;                   /* temporarily overriden by a switch */
  2477.     }
  2478.     cmfdbi(&sw,                         /* Switches */
  2479.            _CMKEY,
  2480.            "Service name or port;\n or switch",
  2481.            "",                          /* default */
  2482.            "",                          /* addtl string data */
  2483.            nftpswi,                     /* addtl numeric data 1: tbl size */
  2484.            4,                           /* addtl numeric data 2: none */
  2485.            xxstring,                    /* Processing function */
  2486.            ftpswitab,                   /* Keyword table */
  2487.            &fl                          /* Pointer to next FDB */
  2488.            );
  2489.     cmfdbi(&fl,                         /* A host name or address */
  2490.            _CMFLD,                      /* fcode */
  2491.            "",                          /* help */
  2492.            "xYzBoo",                    /* default */
  2493.            "",                          /* addtl string data */
  2494.            0,                           /* addtl numeric data 1 */
  2495.            0,                           /* addtl numeric data 2 */
  2496.            xxstring,
  2497.            NULL,
  2498.            &cm
  2499.            );
  2500.     cmfdbi(&cm,                         /* Command confirmation */
  2501.            _CMCFM,
  2502.            "",
  2503.            "",
  2504.            "",
  2505.            0,
  2506.            0,
  2507.            NULL,
  2508.            NULL,
  2509.            NULL
  2510.            );
  2511.  
  2512.     for (n = 0;; n++) {
  2513.         rc = cmfdb(&sw);                /* Parse a service name or a switch */
  2514.         if (rc < 0)
  2515.           goto xopenftp;
  2516.  
  2517.         if (cmresult.fcode == _CMCFM) { /* Done? */
  2518.             break;
  2519.         } else if (cmresult.fcode == _CMFLD) {  /* Port */
  2520.             if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2521.               makestr(&service,cmresult.sresult);
  2522.             else
  2523.               makestr(&service,opn_tls?"ftps":"ftp");
  2524.         } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2525.             c = cmgbrk();               /* get break character */
  2526.             getval = (c == ':' || c == '=');
  2527.             rc = -9;
  2528.             if (getval && !(cmresult.kflags & CM_ARG)) {
  2529.                 printf("?This switch does not take arguments\n");
  2530.                 goto xopenftp;
  2531.             }
  2532.             if (!getval && (cmresult.kflags & CM_ARG)) {
  2533.                 printf("?This switch requires an argument\n");
  2534.                 goto xopenftp;
  2535.             }
  2536.             switch (cmresult.nresult) { /* Switch */
  2537.               case OPN_ANO:             /* /ANONYMOUS */
  2538.                 anonymous++;
  2539.                 break;
  2540.               case OPN_NIN:             /* /NOINIT */
  2541.                 noinit++;
  2542.                 break;
  2543.               case OPN_PSW:             /* /PASSWORD */
  2544.                 if (!anonymous)         /* Don't log real passwords */
  2545.                   debok = 0;
  2546.                 rc = cmfld("Password for FTP server","",&p,xxstring);
  2547.                 if (rc == -3) {
  2548.                     makestr(&ftp_tmp,NULL);
  2549.                 } else if (rc < 0) {
  2550.                     goto xopenftp;
  2551.                 } else {
  2552.                     makestr(&ftp_tmp,p);
  2553.                 }
  2554.                 break;
  2555.               case OPN_USR:             /* /USER */
  2556.                 rc = cmfld("Username for FTP server","",&p,xxstring);
  2557.                 if (rc == -3) {
  2558.                     makestr(&ftp_logname,NULL);
  2559.                 } else if (rc < 0) {
  2560.                     goto xopenftp;
  2561.                 } else {
  2562.                     anonymous = 0;
  2563.                     makestr(&ftp_logname,p);
  2564.                 }
  2565.                 break;
  2566.               case OPN_ACC:
  2567.                 rc = cmfld("Account for FTP server","",&p,xxstring);
  2568.                 if (rc == -3) {
  2569.                     makestr(&ftp_acc,NULL);
  2570.                 } else if (rc < 0) {
  2571.                     goto xopenftp;
  2572.                 } else {
  2573.                     makestr(&ftp_acc,p);
  2574.                 }
  2575.                 break;
  2576.               case OPN_ACT:
  2577.                 opn_psv = 0;
  2578.                 break;
  2579.               case OPN_PSV:
  2580.                 opn_psv = 1;
  2581.                 break;
  2582.               case OPN_TLS:
  2583.                 opn_tls = 1;
  2584.                 break;
  2585.               default:
  2586.                 break;
  2587.             }
  2588.         }
  2589.         if (n == 0) {                   /* After first time through */
  2590.             cmfdbi(&sw,                 /* accept only switches */
  2591.                    _CMKEY,
  2592.                    "\nCarriage return to confirm to command, or switch",
  2593.                    "",
  2594.                    "",
  2595.                    nftpswi,
  2596.                    4,
  2597.                    xxstring,
  2598.                    ftpswitab,
  2599.                    &cm
  2600.                    );
  2601.         }
  2602.     }
  2603. #ifdef COMMENT
  2604.     debug(F100,"ftp openftp while exit","",0);
  2605.     rc = cmcfm();
  2606.     debug(F101,"ftp openftp cmcfm rc","",rc);
  2607.     if (rc < 0)
  2608.       goto xopenftp;
  2609. #endif /* COMMENT */
  2610.  
  2611.     if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
  2612.         sav_psv = ftp_psv;
  2613.         ftp_psv = opn_psv;
  2614.     }
  2615.     if (*hostname == '=') {             /* Bypass directory lookup */
  2616.         hostname++;                     /* if hostname starts with '=' */
  2617.         havehost++;
  2618.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  2619.         havehost++;
  2620.     }
  2621.  
  2622. #ifndef NODIAL
  2623.     if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
  2624.         lunet(hostname);                /* Look up the name */
  2625.         debug(F111,"ftp openftp lunet",hostname,nhcount);
  2626.         if (nhcount == 0) {
  2627.             if (testing)
  2628.               printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2629.             success = ftpopen(hostname,service,opn_tls);
  2630.             debug(F101,"ftp openftp A ftpopen success","",success);
  2631.             rc = success;
  2632.         } else {
  2633.             int found = 0;
  2634.             for (i = 0; i < nhcount; i++) {
  2635.                 if (nh_p2[i])           /* If network type specified */
  2636.                   if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  2637.                     continue;
  2638.                 found++;
  2639.                 makestr(&hostname,nh_p[i]);
  2640.                 debug(F111,"ftpopen lunet substitution",hostname,i);
  2641.                 if (testing)
  2642.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2643.                 success = ftpopen(hostname,service,opn_tls);
  2644.                 debug(F101,"ftp openftp B ftpopen success","",success);
  2645.                 rc = success;
  2646.                 if (success)
  2647.                   break;
  2648.             }
  2649.             if (!found) {               /* E.g. if no network types match */
  2650.                 if (testing)
  2651.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2652.                 success = ftpopen(hostname,service,opn_tls);
  2653.                 debug(F101,"ftp openftp C ftpopen success","",success);
  2654.                 rc = success;
  2655.             }
  2656.         }
  2657.     } else {
  2658. #endif /* NODIAL */
  2659.         if (testing)
  2660.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2661.         success = ftpopen(hostname,service,opn_tls);
  2662.         debug(F111,"ftp openftp D ftpopen success",hostname,success);
  2663.         debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  2664.         rc = success;
  2665. #ifndef NODIAL
  2666.     }
  2667. #endif /* NODIAL */
  2668.  
  2669.   xopenftp:
  2670.     debug(F101,"ftp openftp xopenftp rc","",rc);
  2671.     if (hostsave) free(hostsave);
  2672.     if (service) free(service);
  2673.     if (rc < 0 && ftp_logname) {
  2674.         free(ftp_logname);
  2675.         ftp_logname = NULL;
  2676.     }
  2677.     if (ftp_tmp) {
  2678.         free(ftp_tmp);
  2679.         ftp_tmp = NULL;
  2680.     }
  2681.     return(rc);
  2682. }
  2683.  
  2684. int
  2685. doftpacct() {
  2686.     int x;
  2687.     char * s;
  2688.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  2689.       return(x);
  2690.     CHECKCONN();
  2691.     makestr(&ftp_acc,s);
  2692.     if (testing)
  2693.       printf(" ftp account: \"%s\"\n",ftp_acc);
  2694.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  2695.     return(success);
  2696. }
  2697.  
  2698. int
  2699. doftpusr() {                            /* Log in as USER */
  2700.     int x;
  2701.     char *s, * acct = "";
  2702.     debok = 0;                          /* Don't log */
  2703.     if ((x = cmfld("Remote username or ID","",&s,xxstring)) < 0)
  2704.       return(x);
  2705.     ckstrncpy(line,s,LINBUFSIZ);
  2706.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0)
  2707.       if (x != -3)
  2708.         return(x);
  2709.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2710.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  2711.                    "", &s, xxstring)) < 0)
  2712.       return(x);
  2713.     CHECKCONN();
  2714.     if (*s) {
  2715.         x = strlen(tmpbuf);
  2716.         if (x > 0) {
  2717.             acct = &tmpbuf[x+2];
  2718.             ckstrncpy(acct,s,TMPBUFSIZ - x - 2);
  2719.         }
  2720.     }
  2721.     if (testing)
  2722.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  2723.     success = ftp_user(line,tmpbuf,acct);
  2724. #ifdef CKLOGDIAL
  2725.     dologftp();
  2726. #endif /* CKLOGDIAL */
  2727.     return(success);
  2728. }
  2729.  
  2730. /* DO (various FTP commands)... */
  2731.  
  2732. int
  2733. doftptyp(type) int type; {              /* TYPE */
  2734.     CHECKCONN();
  2735.     ftp_typ = type;
  2736.     changetype(ftp_typ,ftp_vbm);
  2737.     return(1);
  2738. }
  2739.  
  2740. static int
  2741. doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
  2742.     int lcs = -1, rcs = -1;
  2743. #ifndef NOCSETS
  2744.     if (ftp_xla) {
  2745.         lcs = ftp_csl;
  2746.         if (lcs < 0) lcs = fcharset;
  2747.         rcs = ftp_csx;
  2748.         if (rcs < 0) rcs = ftp_csr;
  2749.     }
  2750. #endif /* NOCSETS */
  2751.     debug(F110,"ftp doftpmkd",s,0);
  2752.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2753.       return(1);
  2754.     if (ftpcode == 500) {
  2755.         if (!quiet)
  2756.           printf("MKD command not recognized, trying XMKD\n");
  2757.         if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2758.           return(1);
  2759.     }
  2760.     return(0);
  2761. }
  2762.  
  2763. static int
  2764. doftpmkd() {                            /* MKDIR parse */
  2765.     int x;
  2766.     char * s;
  2767.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  2768.       return(x);
  2769.     CHECKCONN();
  2770.     ckstrncpy(line,s,LINBUFSIZ);
  2771.     if (testing)
  2772.       printf(" ftp mkdir \"%s\"...\n",line);
  2773.     return(success = doftpxmkd(line,-1));
  2774. }
  2775.  
  2776. static int
  2777. doftprmd() {                            /* RMDIR */
  2778.     int x, lcs = -1, rcs = -1;
  2779.     char * s;
  2780.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  2781.       return(x);
  2782.     CHECKCONN();
  2783.     ckstrncpy(line,s,LINBUFSIZ);
  2784.     if (testing)
  2785.       printf(" ftp rmdir \"%s\"...\n",line);
  2786. #ifndef NOCSETS
  2787.     if (ftp_xla) {
  2788.         lcs = ftp_csl;
  2789.         if (lcs < 0) lcs = fcharset;
  2790.         rcs = ftp_csx;
  2791.         if (rcs < 0) rcs = ftp_csr;
  2792.     }
  2793. #endif /* NOCSETS */
  2794.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  2795.       return(success = 1);
  2796.     if (ftpcode == 500) {
  2797.         if (!quiet)
  2798.           printf("RMD command not recognized, trying XMKD\n");
  2799.         success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  2800.     } else
  2801.       success = 0;
  2802.     return(success);
  2803. }
  2804.  
  2805. static int
  2806. doftpren() {                            /* RENAME */
  2807.     int x;
  2808.     char * s;
  2809.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  2810.       return(x);
  2811.     ckstrncpy(line,s,LINBUFSIZ);
  2812.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  2813.       return(x);
  2814.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  2815.     if ((x = cmcfm()) < 0)
  2816.       return(x);
  2817.     CHECKCONN();
  2818.     if (testing)
  2819.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  2820.     success = ftp_rename(line,tmpbuf);
  2821.     return(success);
  2822. }
  2823.  
  2824. int
  2825. doftpres() {                            /* RESET (log out without close) */
  2826.     int x;
  2827.     if ((x = cmcfm()) < 0)
  2828.       return(x);
  2829.     CHECKCONN();
  2830.     if (testing)
  2831.       printf(" ftp reset...\n");
  2832.     return(success = ftp_reset());
  2833. }
  2834.  
  2835. static int
  2836. doftpxhlp() {                           /* HELP */
  2837.     int x;
  2838.     char * s;
  2839.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  2840.       return(x);
  2841.     CHECKCONN();
  2842.     ckstrncpy(line,s,LINBUFSIZ);
  2843.     if (testing)
  2844.       printf(" ftp help \"%s\"...\n",line);
  2845.     /* No need to translate -- all FTP commands are ASCII */
  2846.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  2847. }
  2848.  
  2849. static int
  2850. doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
  2851.     int x, lcs = 0, rcs = 0, xlate = 0;
  2852.     char * p, * s, * m = "";
  2853.     if (cx == FTP_VDI) {
  2854.         switch (servertype) {
  2855.           case SYS_VMS:
  2856.           case SYS_DOS:
  2857.           case SYS_TOPS10:
  2858.           case SYS_TOPS20:
  2859.             m = "*.*";
  2860.             break;
  2861.           default:
  2862.             m = "*";
  2863.         }
  2864.     }
  2865.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  2866.       return(x);
  2867.     if ((x = remtxt(&s)) < 0)
  2868.       return(x);
  2869. #ifdef NOCSETS
  2870.     xlate = 0;
  2871. #else
  2872.     xlate = ftp_xla;
  2873. #endif /* NOCSETS */
  2874.     line[0] = NUL;
  2875.     ckstrncpy(line,s,LINBUFSIZ);
  2876.     s = line;
  2877.     CHECKCONN();
  2878.  
  2879. #ifndef NOCSETS
  2880.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  2881.         lcs = ftp_csl;                  /* Local charset */
  2882.         if (lcs < 0) lcs = fcharset;
  2883.         if (lcs < 0) xlate = 0;
  2884.     }
  2885.     if (xlate) {                        /* Still ON? */
  2886.         rcs = ftp_csx;                  /* Remote (Server) charset */
  2887.         if (rcs < 0) rcs = ftp_csr;
  2888.         if (rcs < 0) xlate = 0;
  2889.     }
  2890. #endif /* NOCSETS */
  2891.  
  2892.     if (testing) {
  2893.         p = s;
  2894.         if (!p) p = "";
  2895.         if (*p)
  2896.           printf("Directory of files %s at %s:\n", line, ftp_host);
  2897.         else
  2898.           printf("Directory of files at %s:\n", ftp_host);
  2899.     }
  2900.     debug(F111,"doftpdir",s,cx);
  2901.  
  2902.     if (cx == FTP_DIR) {
  2903.         /* Translation of line[] is done inside recvrequest() */
  2904.         /* when it calls ftpcmd(). */
  2905.         return(success =
  2906.           (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  2907.     }
  2908.     success = 1;                        /* VDIR - one file at a time... */
  2909.     p = (char *)remote_files(1,(CHAR *)s,0); /* Get the file list */
  2910.     cancelgroup = 0;
  2911.     if (!ftp_vbm && !quiet)
  2912.       printlines = 1;
  2913.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  2914.         if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  2915.             success = 0;
  2916.             break;
  2917.         }
  2918.         p = (char *)remote_files(0,NULL,0); /* Get next file */
  2919.         debug(F110,"ftp vdir file",s,0);
  2920.     }
  2921.     return(success);
  2922. }
  2923.  
  2924. static int
  2925. doftppwd() {                            /* PWD */
  2926.     int x, 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.     if ((x = cmcfm()) < 0)
  2936.       return(x);
  2937.     CHECKCONN();
  2938.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  2939.         success = 1;
  2940.     } else if (ftpcode == 500) {
  2941.         if (ftp_deb)
  2942.           printf("PWD command not recognized, trying XPWD\n");
  2943.         success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  2944.     }
  2945.     return(success);
  2946. }
  2947.  
  2948. static int
  2949. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  2950.     int lcs = -1, rcs = -1;
  2951. #ifndef NOCSETS
  2952.     if (ftp_xla) {
  2953.         lcs = ftp_csl;
  2954.         if (lcs < 0) lcs = fcharset;
  2955.         rcs = ftp_csx;
  2956.         if (rcs < 0) rcs = ftp_csr;
  2957.     }
  2958. #endif /* NOCSETS */
  2959.  
  2960.     debug(F110,"ftp doftpcwd",s,0);
  2961.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2962.       return(1);
  2963.     if (ftpcode == 500) {
  2964.         if (!quiet)
  2965.           printf("CWD command not recognized, trying XCWD\n");
  2966.         if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  2967.           return(1);
  2968.     }
  2969.     return(0);
  2970. }
  2971.  
  2972. static int
  2973. doftpcdup() {                           /* CDUP */
  2974.     debug(F100,"ftp doftpcdup","",0);
  2975.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  2976.       return(1);
  2977.     if (ftpcode == 500) {
  2978.         if (!quiet)
  2979.           printf("CDUP command not recognized, trying XCUP\n");
  2980.         if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  2981.           return(1);
  2982.     }
  2983.     return(0);
  2984. }
  2985.  
  2986. /* s y n c d i r  --  Synchronizes client & server directories */
  2987.  
  2988. /* Used with recursive PUTs; Returns 0 on failure, 1 on success */
  2989.  
  2990. static int cdlevel = 0, cdsimlvl = 0;
  2991.  
  2992. static int
  2993. syncdir(local,sim) char * local; int sim; {
  2994.     char buf[CKMAXPATH+1];
  2995.     char tmp[CKMAXPATH+1];
  2996.     char msgbuf[CKMAXPATH+64];
  2997.     char c, * p = local, * s = buf, * q = buf;
  2998.     int i, k = 0, done = 0, itsadir = 0, saveq;
  2999.  
  3000.     debug(F110,"ftp syncdir local (new)",local,0);
  3001.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  3002.  
  3003.     itsadir = isdir(local);
  3004.     saveq = quiet;
  3005.  
  3006.     while ((*s = *p)) {                 /* Copy the argument filename */
  3007.         if (++k == CKMAXPATH)           /* so we can poke it. */
  3008.           return(-1);
  3009.         if (*s == '/')                  /* Pointer to rightmost dirsep */
  3010.           q = s;
  3011.         s++;
  3012.         p++;
  3013.     }
  3014.     if (!itsadir)
  3015.       *q = NUL;                         /* Keep just the path part */
  3016.  
  3017.     debug(F110,"ftp syncdir buf",buf,0);
  3018.     if (!strcmp(buf,putpath)) {         /* Same as for previous file? */
  3019.         if (itsadir) {                  /* It's a directory? */
  3020.             if (doftpcwd(local,0)) {    /* Try to CD to it */
  3021.                 doftpcdup();            /* Worked - CD back up */
  3022.             } else if (sim) {           /* Simulating... */
  3023.                 if (fdispla == XYFD_B) {
  3024.                     printf("WOULD CREATE DIRECTORY %s\n",local);
  3025.                 } else if (fdispla) {
  3026.                     ckmakmsg(msgbuf,CKMAXPATH,
  3027.                              "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3028.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3029.                 }
  3030.                 /* See note above */
  3031.                 return(0);
  3032.             } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3033.                 return(0);
  3034.             } else {
  3035.                 if (fdispla == XYFD_B) {
  3036.                     printf("CREATED DIRECTORY %s\n",local);
  3037.                 } else if (fdispla) {
  3038.                     ckmakmsg(msgbuf,CKMAXPATH+64,
  3039.                              "CREATED DIRECTORY ",local,NULL,NULL);
  3040.                     ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3041.                 }
  3042.             }
  3043.         }
  3044.         debug(F110,"ftp syncdir no change",buf,0);
  3045.         return(1);                      /* Yes, done. */
  3046.     }
  3047.     ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
  3048.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3049.  
  3050.     p = buf;                            /* New */
  3051.     s = putpath;                        /* Old */
  3052.  
  3053.     debug(F110,"ftp syncdir A p",p,0);
  3054.     debug(F110,"ftp syncdir A s",s,0);
  3055.  
  3056.     while (*p != NUL && *s != NUL && *p == *s) p++,s++;
  3057.  
  3058.     if (*s == '/' && !*p) s++;          /* Don't count initial slash */
  3059.  
  3060.     debug(F110,"ftp syncdir B p",p,0);
  3061.     debug(F110,"ftp syncdir B s",s,0);
  3062.  
  3063.     /* p and s now point to the leftmost spot where they differ */
  3064.  
  3065.     if (*s) {                           /* We have to back up */
  3066.         k = 1;                          /* How many levels */
  3067.         while ((c = *s++)) {            /* Count dirseps */
  3068.             if (c == '/' && *s)
  3069.               k++;
  3070.         }
  3071.         for (i = 0; i < k; i++) {       /* Do that many CDUPs */
  3072.             debug(F111,"ftp syncdir up",p,i+1);
  3073.             if (sim && cdsimlvl) {
  3074.                 cdsimlvl--;
  3075.             } else {
  3076.                 if (!doftpcdup()) {
  3077.                     quiet = saveq;
  3078.                     return(0);
  3079.                 }
  3080.             }
  3081.             cdlevel--;
  3082.         }
  3083.         if (!*p)                        /* If we don't have to go down */
  3084.           goto xcwd;                    /* we're done. */
  3085.     }
  3086.     while (p > buf && *p && *p != '/')  /* If in middle of segment */
  3087.       p--;                              /* back up to beginning */
  3088.     if (*p == '/')                      /* and terminate there */
  3089.       p++;
  3090.  
  3091.     s = p;                              /* Point to start of new down path. */
  3092.     while (1) {                         /* Loop through characters. */
  3093.         if (*s == '/' || !*s) {         /* Have a segment. */
  3094.             if (!*s)                    /* If end of string, */
  3095.               done++;                   /* after this segment we're done. */
  3096.             else
  3097.               *s = NUL;                 /* NUL out the separator. */
  3098.             if (*p) {                   /* If segment is not empty */
  3099.                 debug(F110,"ftp syncdir down segment",p,0);
  3100.                 if (!doftpcwd(p,0)) {   /* Try to CD to it */
  3101.                     if (sim) {
  3102.                         if (fdispla == XYFD_B) {
  3103.                             printf("WOULD CREATE DIRECTORY %s\n",local);
  3104.                         } else if (fdispla) {
  3105.                             ckmakmsg(msgbuf,CKMAXPATH,"WOULD CREATE DIRECTORY",
  3106.                                      local,NULL,NULL);
  3107.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3108.                         }
  3109.                         cdsimlvl++;
  3110.                     } else {
  3111.                         if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3112. /*
  3113.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3114.   FOO but the remote has a regular file with the same name.  We can't CD
  3115.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3116.   the user handle the problem.
  3117. */
  3118.                             quiet = saveq;
  3119.                             return(0);
  3120.                         }
  3121.                         if (fdispla == XYFD_B) {
  3122.                             printf("CREATED DIRECTORY %s\n",p);
  3123.                         } else if (fdispla) {
  3124.                             ckmakmsg(msgbuf,CKMAXPATH,
  3125.                                      "CREATED DIRECTORY ",p,NULL,NULL);
  3126.                             ftscreen(SCR_ST,ST_MSG,0l,msgbuf);
  3127.                         }
  3128.                         if (!doftpcwd(p,0)) { /* Try again to CD */
  3129.                             quiet = saveq;
  3130.                             return(0);
  3131.                         }
  3132.                     }
  3133.                 }
  3134.                 cdlevel++;
  3135.             }
  3136.             if (done)                   /* Quit if no next segment */
  3137.               break;
  3138.             p = s+1;                    /* Point to next segment */
  3139.         }
  3140.         s++;                            /* Point to next source char */
  3141.     }
  3142.  
  3143.   xcwd:
  3144.     ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
  3145.     quiet = saveq;
  3146.     return(1);
  3147. }
  3148.  
  3149. #ifdef DOUPDATE
  3150. #ifdef DEBUG
  3151. static VOID
  3152. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3153.     if (deblog) {
  3154.         debug(F111,"ftp year ",s,xx->tm_year);
  3155.         debug(F111,"ftp month",s,xx->tm_mon);
  3156.         debug(F111,"ftp day  ",s,xx->tm_mday);
  3157.         debug(F111,"ftp hour ",s,xx->tm_hour);
  3158.         debug(F111,"ftp min  ",s,xx->tm_min);
  3159.         debug(F111,"ftp sec  ",s,xx->tm_sec);
  3160.     }
  3161. }
  3162. #endif /* DEBUG */
  3163.  
  3164. /*  t m c o m p a r e  --  Compare two struct tm's */
  3165.  
  3166. /*  Like strcmp() but for struct tm's  */
  3167. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3168.  
  3169. static int
  3170. tmcompare(xx,yy) struct tm * xx, * yy; {
  3171.  
  3172.     if (xx->tm_year < yy->tm_year)      /* First year less than second */
  3173.       return(-1);
  3174.     if (xx->tm_year > yy->tm_year)      /* First year greater than second */
  3175.       return(1);
  3176.  
  3177.     /* Years are equal so compare months */
  3178.  
  3179.     if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
  3180.       return(-1);
  3181.     if (xx->tm_mon  > yy->tm_mon)
  3182.       return(1);
  3183.  
  3184.     if (xx->tm_mday < yy->tm_mday)
  3185.       return(-1);
  3186.     if (xx->tm_mday > yy->tm_mday)
  3187.       return(1);
  3188.  
  3189.     if (xx->tm_hour < yy->tm_hour)
  3190.       return(-1);
  3191.     if (xx->tm_hour > yy->tm_hour)
  3192.       return(1);
  3193.  
  3194.     if (xx->tm_min  < yy->tm_min)
  3195.       return(-1);
  3196.     if (xx->tm_min  > yy->tm_min)
  3197.       return(1);
  3198.  
  3199.     if (xx->tm_sec  < yy->tm_sec)
  3200.       return(-1);
  3201.     if (xx->tm_sec  > yy->tm_sec)
  3202.       return(1);
  3203.  
  3204.     return(0);
  3205. }
  3206. #endif /* DOUPDATE */
  3207.  
  3208. #ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
  3209. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3210.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3211. };
  3212.  
  3213. /* Macro for whether a given year is a leap year. */
  3214. #define ISLEAP(year) \
  3215. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3216. #endif /* HAVE_TIMEGM */
  3217.  
  3218. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3219.  
  3220. static time_t
  3221. #ifdef CK_ANSIC
  3222. mkutime(struct tm * tm)
  3223. #else
  3224. mkutime(tm) struct tm * tm;
  3225. #endif /* CK_ANSIC */
  3226. /* mkutime */ {
  3227. #ifdef HAVE_TIMEGM
  3228.     return(timegm(tm));                 /* Have system service, use it. */
  3229. #else
  3230. /*
  3231.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3232.   Given a struct tm representing a calendar time in UTC, convert it to
  3233.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3234.   convertable.  Note that this function does not canonicalize the provided
  3235.   struct tm, nor does it allow out-of-range values or years before 1970.
  3236.   Result should be identical with timegm().
  3237. */
  3238.     time_t result = 0;
  3239.     int i;
  3240.     /*
  3241.       We do allow some ill-formed dates, but we don't do anything special
  3242.       with them and our callers really shouldn't pass them to us.  Do
  3243.       explicitly disallow the ones that would cause invalid array accesses
  3244.       or other algorithm problems.
  3245.     */
  3246. #ifdef DEBUG
  3247.     if (deblog) {
  3248.         debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3249.         debug(F101,"mkutime tm_year","",tm->tm_year);
  3250.     }
  3251. #endif /* DEBUG */
  3252.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3253.       return((time_t) -1);
  3254.  
  3255.     /* Convert to time_t. */
  3256.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3257.       result += 365 + ISLEAP(i);
  3258.     for (i = 0; i < tm->tm_mon; i++)
  3259.       result += MONTHDAYS[i];
  3260.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3261.       result++;
  3262.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3263.     result = 60 * result + tm->tm_min;
  3264.     result = 60 * result + tm->tm_sec;
  3265.     debug(F101,"mkutime result","",result);
  3266.     return(result);
  3267. #endif /* HAVE_TIMEGM */
  3268. }
  3269.  
  3270.  
  3271. /*
  3272.   s e t m o d t i m e  --  Set file modification time.
  3273.  
  3274.   f = char * filename;
  3275.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3276.  
  3277.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3278.   Returns:
  3279.     0 on success,
  3280.    -1 on error.
  3281.  
  3282. */
  3283. static int
  3284. #ifdef CK_ANSIC
  3285. setmodtime(char * f, time_t t)
  3286. #else
  3287. setmodtime(f,t) char * f; time_t t;
  3288. #endif /* CK_ANSIC */
  3289. /* setmodtime */ {
  3290. #ifdef NT
  3291.     struct _stat sb;
  3292. #else /* NT */
  3293.     struct stat sb;
  3294. #endif /* NT */
  3295.     int x, rc = 0;
  3296. #ifdef BSD44
  3297.     struct timeval tp[2];
  3298. #else
  3299. #ifdef V7
  3300.     struct utimbuf {
  3301.         time_t timep[2];
  3302.     } tp;
  3303. #else
  3304. #ifdef SYSUTIMEH
  3305. #ifdef NT
  3306.     struct _utimbuf tp;
  3307. #else /* NT */
  3308.     struct utimbuf tp;
  3309. #endif /* NT */
  3310. #else
  3311.     struct utimbuf {
  3312.         time_t atime;
  3313.         time_t mtime;
  3314.     } tp;
  3315. #endif /* SYSUTIMEH */
  3316. #endif /* V7 */
  3317. #endif /* BSD44 */
  3318.  
  3319.     if (stat(f,&sb) < 0) {
  3320.         debug(F111,"setmodtime stat failure",f,errno);
  3321.         return(-1);
  3322.     }
  3323. #ifdef BSD44
  3324.     tp[0].tv_sec = sb.st_atime;         /* Access time first */
  3325.     tp[1].tv_sec = t;                   /* Update time second */
  3326.     debug(F111,"setmodtime BSD44",f,t);
  3327. #else
  3328. #ifdef V7
  3329.     tp.timep[0] = t;                    /* Set modif. time to creation date */
  3330.     tp.timep[1] = sb.st_atime;          /* Don't change the access time */
  3331.     debug(F111,"setmodtime V7",f,t);
  3332. #else
  3333. #ifdef SYSUTIMEH
  3334.     tp.modtime = t;                     /* Set modif. time to creation date */
  3335.     tp.actime = sb.st_atime;            /* Don't change the access time */
  3336.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3337. #else
  3338.     tp.mtime = t;                       /* Set modif. time to creation date */
  3339.     tp.atime = sb.st_atime;             /* Don't change the access time */
  3340.     debug(F111,"setmodtime (other)",f,t);
  3341. #endif /* SYSUTIMEH */
  3342. #endif /* V7 */
  3343. #endif /* BSD44 */
  3344.  
  3345.     /* Try to set the file date */
  3346.  
  3347. #ifdef BSD44
  3348.     x = utimes(f,tp);
  3349.     debug(F111,"setmodtime utimes()","BSD44",x);
  3350. #else
  3351. #ifdef IRIX65
  3352.     {
  3353.       /*
  3354.         The following produces the nonsensical warning:
  3355.         Argument  of type "const struct utimbuf *" is incompatible with
  3356.         parameter of type "const struct utimbuf *".  If you can make it
  3357.         go away, be my guest.
  3358.       */
  3359.         const struct utimbuf * t2 = &tp;
  3360.         x = utime(f,t2);
  3361.     }
  3362. #else
  3363.     x = utime(f,&tp);
  3364.     debug(F111,"setmodtime utime()","other",x);
  3365. #endif /* IRIX65 */
  3366. #endif /* BSD44 */
  3367.     if (x)
  3368.       rc = -1;
  3369.  
  3370.     debug(F101,"setmodtime result","",rc);
  3371.     return(rc);
  3372. }
  3373.  
  3374.  
  3375. /*
  3376.   c h k m o d t i m e  --  Check/Set file modification time.
  3377.  
  3378.   fc = function code:
  3379.     0 = Check; returns:
  3380.       -1 on error,
  3381.        0 if local older than remote,
  3382.        1 if modtimes are equal,
  3383.        2 if local newer remote.
  3384.     1 = Set (local file's modtime from remote's); returns:
  3385.       -1 on error,
  3386.        0 on success.
  3387. */
  3388. static int
  3389. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3390. #ifdef NT
  3391.     struct _stat statbuf;
  3392. #else /* NT */
  3393.     struct stat statbuf;
  3394. #endif /* NT */
  3395.     struct tm * tmlocal = NULL;
  3396.     struct tm tmremote;
  3397.     int rc = 0, havedate = 0, lcs = -1, rcs = -1;
  3398.  
  3399. #ifndef NOCSETS
  3400.     if (ftp_xla) {
  3401.         lcs = ftp_csl;
  3402.         if (lcs < 0) lcs = fcharset;
  3403.         rcs = ftp_csx;
  3404.         if (rcs < 0) rcs = ftp_csr;
  3405.     }
  3406. #endif /* NOCSETS */
  3407.  
  3408.     if (fc == 0) {
  3409.         rc = stat(local,&statbuf);
  3410.         if (rc == 0) {                  /* Get local file's mod time */
  3411.             tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3412. #ifdef DEBUG
  3413.             if (tmlocal) {
  3414.                 dbtime(local,tmlocal);
  3415.  
  3416.             }
  3417. #endif /* DEBUG */
  3418.         }
  3419.     }
  3420.     /* Get remote file's mod time as yyyymmddhhmmss */
  3421.     if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3422.         int flag = 0;                   /* Good reply */
  3423.         char c, * s;
  3424.         bzero((char *)&tmremote, sizeof(struct tm));
  3425.         s = ftp_reply_str;
  3426.         while ((c = *s++)) {            /* Skip past response code */
  3427.             if (c == SP) {
  3428.                 flag++;
  3429.                 break;
  3430.             }
  3431.         }
  3432.         debug(F110,"ftp chkmodtime string",s,0);
  3433.         if (flag) {                     /* Convert to struct tm */
  3434.             char * pat;
  3435.             int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
  3436.             if (!ckstrcmp(s,"191",3,0)) {
  3437.                 pat = "%05d%02d%02d%02d%02d%02d";
  3438.                 y2kbug++;
  3439.                 debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3440.             } else {
  3441.                 pat = "%04d%02d%02d%02d%02d%02d";
  3442.             }
  3443.             if (sscanf(s,               /* Parse into struct tm */
  3444.                        pat,
  3445.                        &(tmremote.tm_year),
  3446.                        &(tmremote.tm_mon),
  3447.                        &(tmremote.tm_mday),
  3448.                        &(tmremote.tm_hour),
  3449.                        &(tmremote.tm_min),
  3450.                        &(tmremote.tm_sec)
  3451.                        ) == 6) {
  3452.                 tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3453.                 debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3454.                 tmremote.tm_mon--;
  3455. #ifdef DEBUG
  3456.                 dbtime(remote,&tmremote);
  3457. #endif /* DEBUG */
  3458.                 havedate = 1;
  3459.             }
  3460.         }
  3461.     } else
  3462.       rc = -1;
  3463.  
  3464.     if (fc == 0) {                      /* Compare */
  3465.         if (rc == 0 && havedate == 1) { /* Only if we have both file dates */
  3466.             /*
  3467.               Compare with local file's time.  We don't use
  3468.               clock time (time_t) here in case of signed/unsigned
  3469.               confusion, etc.
  3470.             */
  3471.             return(tmcompare(tmlocal,&tmremote) + 1);
  3472.         }
  3473.     } else if (ftp_dates) {             /* Set */
  3474.         /*
  3475.           Here we must convert struct tm to time_t
  3476.           without applying timezone conversion, for which
  3477.           there is no portable API.  The method is hidden
  3478.           in mkutime(), defined above.
  3479.         */
  3480.         time_t utc;
  3481.         utc = mkutime(&tmremote);
  3482.         debug(F111,"ftp chkmodtime mkutime",remote,utc);
  3483.         if (utc != (time_t)-1)
  3484.           return(setmodtime(local,utc));
  3485.     }
  3486.     return(-1);
  3487. }
  3488.  
  3489. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  3490.  
  3491. static int
  3492. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  3493.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  3494. /* getfile */ {
  3495.     int rc = -1;
  3496.     ULONG t0, t1;
  3497.  
  3498. #ifdef GFTIMER
  3499.     CKFLOAT sec;
  3500. #else
  3501.     int sec = 0;
  3502. #endif /* GFTIMER */
  3503.     char fullname[CKMAXPATH+1];
  3504.  
  3505.     debug(F110,"ftp getfile local A",local,0);
  3506.     debug(F110,"ftp getfile pipename",pipename,0);
  3507.     if (!remote) remote = "";
  3508.  
  3509. #ifdef PATTERNS
  3510.     if (patterns && get_auto && !forcetype) { /* Automatic type switching? */
  3511.         int x;
  3512.         x = matchname(remote,0,servertype);
  3513.         debug(F111,"ftp getfile matchname",remote,x);
  3514.         switch (x) {
  3515.           case 0: ftp_typ = FTT_ASC; break;
  3516.           case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  3517.           default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  3518.         }
  3519.         changetype(ftp_typ,ftp_vbm);
  3520.         binary = ftp_typ;               /* For file-transfer display */
  3521.     }
  3522. #endif /* PATTERNS */
  3523.  
  3524. #ifndef NOCSETS
  3525.     ftp_csx = -1;                       /* For file-transfer display */
  3526.     ftp_csl = -1;                       /* ... */
  3527.  
  3528.     if (rcs > -1)                       /* -1 means no translation */
  3529.       if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
  3530.         if (fcs < 0)                    /* File charset not forced? */
  3531.           fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
  3532.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3533.         debug(F110,"ftp getfile","initxlate",0);
  3534.         initxlate(rcs,fcs);             /* NB: opposite order of PUT */
  3535.         ftp_csx = rcs;
  3536.         ftp_csl = fcs;
  3537.     } else
  3538.       xlate = 0;
  3539. #endif /* NOCSETS */
  3540.  
  3541.     if (!pipename && (!local || !local[0]))
  3542.       local = remote;
  3543.  
  3544.     out2screen = !strcmp(local,"-");
  3545.  
  3546.     fullname[0] = NUL;
  3547.     if (pipename) {
  3548.         ckstrncpy(fullname,pipename,CKMAXPATH+1);
  3549.     } else {
  3550.         zfnqfp(local,CKMAXPATH,fullname);
  3551.         if (!fullname[0])
  3552.           ckstrncpy(fullname,local,CKMAXPATH+1);
  3553.     }
  3554.     if (!out2screen && displa && fdispla) { /* Screen */
  3555.         ftscreen(SCR_FN,'F',(long)pktnum,remote);
  3556.         ftscreen(SCR_AN,0,0L,fullname);
  3557.         ftscreen(SCR_FS,0,fsize,"");
  3558.     }
  3559.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  3560.     tlog(F110," as",fullname,0);
  3561.     debug(F111,"ftp getfile size",remote,fsize);
  3562.     debug(F111,"ftp getfile local",local,out2screen);
  3563.  
  3564.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  3565.  
  3566.     t0 = gmstimer();                    /* Start time */
  3567.     debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
  3568.     rc = recvrequest("RETR",
  3569.                      local,
  3570.                      remote,
  3571.                      append ? "ab" : "wb",
  3572.                      0,
  3573.                      recover,
  3574.                      pipename,
  3575.                      xlate,
  3576.                      fcs,
  3577.                      rcs
  3578.                      );
  3579.     t1 = gmstimer();                    /* End time */
  3580.     debug(F111,"ftp getfile t1",remote,t1);
  3581.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  3582. #ifdef GFTIMER
  3583.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3584.     fpxfsecs = sec;                     /* (for doxlog()) */
  3585. #else
  3586.     sec = (t1 - t0) / 1000;
  3587.     xfsecs = (int)sec;
  3588. #endif /* GFTIMER */
  3589.     debug(F111,"ftp recvrequest rc",remote,rc);
  3590.     if (cancelfile || cancelgroup) {
  3591.         debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  3592.         ftscreen(SCR_ST,ST_INT,0l,"");
  3593.     } else if (rc > 0) {
  3594.         debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  3595.         ftscreen(SCR_ST,ST_SKIP,0l,cmarg);
  3596.     } else if (rc < 0) {
  3597.         switch (ftpcode) {
  3598.           case -4:                      /* Network error */
  3599.           case -2:                      /* File error */
  3600.             ftscreen(SCR_ST,ST_MSG,0l,ck_errstr());
  3601.             break;
  3602.           case -3:
  3603.             ftscreen(SCR_ST,ST_MSG,0l,"Failure to make data connection");
  3604.             break;
  3605.           case -1:
  3606.             ftscreen(SCR_ST,ST_INT,0l,""); /* (should be covered above) */
  3607.             break;
  3608.           default:
  3609.             ftscreen(SCR_ST,ST_MSG,0l,&ftp_reply_str[4]);
  3610.         }
  3611.     } else {                            /* Tudo bem */
  3612.         ftscreen(SCR_PT,'Z',0L,"");
  3613.         if (rc == 0) {
  3614.             ftscreen(SCR_ST,ST_OK,0L,""); /* For screen */
  3615.             makestr(&rrfspec,remote);     /* For WHERE command */
  3616.             makestr(&rfspec,fullname);
  3617.             if (ftp_dates) {              /* If FTP DATES ON... */
  3618.                 chkmodtime(local,remote,1); /* set local file date */
  3619.             }
  3620.         }
  3621.     }
  3622.     filcnt++;                           /* Used by \v(filenum) */
  3623. #ifdef TLOG
  3624.     if (tralog) {
  3625.         if (rc > 0) {
  3626.             tlog(F100," recovery skipped","",0);
  3627.         } else if (rc == 0) {
  3628.             tlog(F101," complete, size", "", fsize);
  3629.         } else if (cancelfile) {
  3630.             tlog(F100," canceled by user","",0);
  3631.         } else {
  3632.             tlog(F110," failed:",ftp_reply_str,0);
  3633.         }
  3634.         if (!tlogfmt)
  3635.           doxlog(what,local,fsize,ftp_typ,!rc,"");
  3636.     }
  3637. #endif /* TLOG */
  3638.     return(rc);
  3639. }
  3640.  
  3641. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  3642. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  3643.  
  3644. static int
  3645. putfile(cx,
  3646.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  3647.     char * local, * remote, * mvto, *rnto, *srvrn;
  3648.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg;
  3649.  
  3650. /* putfile */ {
  3651.  
  3652.     char asname[CKMAXPATH+1];
  3653.     char fullname[CKMAXPATH+1];
  3654.     int k = -1, x, y, o, rc, nc, xlate = 0, restart = 0, mt = -1;
  3655.     char * s, * cmd;
  3656.     ULONG t0, t1;                       /* Times for stats */
  3657.     int ofcs, orcs;
  3658.  
  3659. #ifdef GFTIMER
  3660.     CKFLOAT sec;
  3661. #else
  3662.     int sec = 0;
  3663. #endif /* GFTIMER */
  3664.     debug(F111,"ftp putfile flg",local,flg);
  3665.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  3666.     debug(F101,"ftp putfile fcs","",fcs);
  3667.     debug(F101,"ftp putfile rcs","",rcs);
  3668.  
  3669.     ofcs = fcs;                         /* Save charset args */
  3670.     orcs = rcs;
  3671.  
  3672.     sendstart = 0L;
  3673.     restart = flg & PUT_RES;
  3674.     if (!remote)
  3675.       remote = "";
  3676.  
  3677.     /* FTP protocol command to send to server */
  3678.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  3679.  
  3680.     if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
  3681.         if (alike) {                    /* If server & client are alike */
  3682.             nc = 0;                     /* no conversion */
  3683.         } else {                        /* If they are different */
  3684.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  3685.               nc = -1;                  /* only minimal conversions needed */
  3686.             else                        /* otherwise */
  3687.               nc = 1;                   /* full conversion */
  3688.         }
  3689.     } else                              /* Not auto - do what user said */
  3690.       nc = x_cnv;
  3691.  
  3692.     if (filepeek && !pipesend) {        /* Determine file type */
  3693.         if (isdir(local)) {             /* If it's a directory */
  3694.             k = FT_BIN;                 /* skip the file scan */
  3695.         } else {
  3696.             debug(F110,"FTP PUT calling scanfile",local,0);
  3697.             k = scanfile(local,&o,nscanfile); /* Scan the file */
  3698.         }
  3699.         debug(F111,"FTP PUT scanfile",local,k);
  3700.         if (k > -1 && !forcetype) {
  3701.             ftp_typ = (k == FT_BIN) ? 1 : 0;
  3702.             if (xft > -1 && ftp_typ != xft) {
  3703.                 if (flg & PUT_SIM)
  3704.                   tlog(F110,"ftp put SKIP (Type):", fullname, 0);
  3705.                 return(SKP_TYP);
  3706.             }
  3707.             if (ftp_typ == 1 && tenex)  /* User said TENEX? */
  3708.               ftp_typ = FTT_TEN;
  3709.         }
  3710.     }
  3711. #ifndef NOCSETS
  3712.     ftp_csx = -1;                       /* For file-transfer display */
  3713.     ftp_csl = -1;                       /* ... */
  3714.  
  3715.     if (rcs > -1) {                     /* -1 means no translation */
  3716.         if (ftp_typ == 0) {             /* File type is "ascii"? */
  3717.             if (fcs < 0) {              /* File charset not forced? */
  3718.                 if (k < 0) {            /* If we didn't scan */
  3719.                     fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
  3720.                 } else {                /* If we did scan, use scan result */
  3721.                     switch (k) {
  3722.                       case FT_TEXT:     /* Unknown text */
  3723.                         fcs = fcharset;
  3724.                         break;
  3725.                       case FT_7BIT:     /* 7-bit text */
  3726.                         fcs = dcset7;
  3727.                         break;
  3728.                       case FT_8BIT:     /* 8-bit text */
  3729.                         fcs = dcset8;
  3730.                         break;
  3731.                       case FT_UTF8:     /* UTF-8 */
  3732.                         fcs = FC_UTF8;
  3733.                         break;
  3734.                       case FT_UCS2:     /* UCS-2 */
  3735.                         fcs = FC_UCS2;
  3736.                         if (o > -1)     /* Input file byte order */
  3737.                           fileorder = o;
  3738.                         break;
  3739.                       default:
  3740.                         rcs = -1;
  3741.                     }
  3742.                 }
  3743.             }
  3744.         }
  3745.     }
  3746.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3747.         debug(F110,"ftp putfile","initxlate",0);
  3748.         initxlate(fcs,rcs);
  3749.         debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  3750.         xlate = 1;
  3751.         ftp_csx = rcs;
  3752.         ftp_csl = fcs;
  3753.     }
  3754. #endif /* NOCSETS */
  3755.  
  3756.     binary = ftp_typ;                   /* For file-transfer display */
  3757.  
  3758.     if (recursive) {                    /* If sending recursively, */
  3759.         if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  3760.           return(-1);                   /* Don't PUT if it fails. */
  3761.         else if (isdir(local))          /* It's a directory */
  3762.           return(0);                    /* Don't send it! */
  3763.     }
  3764.     if (*remote) {                      /* If an as-name template was given */
  3765. #ifndef NOSPL
  3766.         if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
  3767.             y = CKMAXPATH;              /* evaluate it for this file */
  3768.             s = asname;
  3769.             zzstring(remote,&s,&y);
  3770.         } else
  3771. #endif /* NOSPL */
  3772.           ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  3773.     } else {                                    /* No as-name */
  3774.         nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  3775.         debug(F110,"FTP PUT nzltor",asname,0);
  3776.     }
  3777.     /* Preliminary messages and log entries */
  3778.  
  3779.     fullname[0] = NUL;
  3780.     zfnqfp(local,CKMAXPATH,fullname);
  3781.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  3782.  
  3783.     if (displa && fdispla) {            /* Screen */
  3784.         ftscreen(SCR_FN,'F',(long)pktnum,local);
  3785.         ftscreen(SCR_AN,0,0L,asname);
  3786.         ftscreen(SCR_FS,0,fsize,"");
  3787.     }
  3788. #ifdef DOUPDATE
  3789.     if (flg & PUT_UPD) {                /* Update mode... */
  3790.         mt = chkmodtime(fullname,asname,0);
  3791.         debug(F111,"ftp putfile chkmodtime",asname,mt);
  3792.         if (mt == 0) {                  /* Remote is older */
  3793.             tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  3794.             ftscreen(SCR_ST,ST_SKIP,SKP_DAT,fullname);
  3795.             filcnt++;
  3796.             return(SKP_DAT);
  3797.         } else if (mt == 1) {           /* Times are equal */
  3798.             tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  3799.             ftscreen(SCR_ST,ST_SKIP,SKP_EQU,fullname);
  3800.             filcnt++;
  3801.             return(SKP_DAT);
  3802.         }
  3803.         tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  3804.              "ftp put /update TEXT:", fullname, 0);
  3805.     } else if (flg & PUT_RES) {
  3806.         tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  3807.              "ftp put /recover TEXT:", fullname, 0);
  3808.     } else {
  3809.         tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  3810.     }
  3811. #else
  3812.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  3813. #endif /* DOUPDATE */
  3814.     tlog(F110," as",asname,0);
  3815.  
  3816. #ifndef NOCSETS
  3817.     if (xlate) {
  3818.         debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  3819.         tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  3820.         tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  3821.     } else if (!ftp_typ) {
  3822.         tlog(F110," character sets:","no conversion",0);
  3823.         fcs = ofcs;                     /* Binary file but we still must */
  3824.         rcs = orcs;                     /* translate its name */
  3825.     }
  3826. #endif /* NOCSETS */
  3827.  
  3828.     /* PUT THE FILE */
  3829.  
  3830.     t0 = gmstimer();                    /* Start time */
  3831.     if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
  3832.         if (flg & PUT_UPD) {            /* (see SKP_xxx in ckcker.h) */
  3833.             rc = (mt < 0) ?             /* Update mode... */
  3834.               SKP_XNX :                 /* Remote file doesn't exist */
  3835.                 SKP_XUP;                /* Remote file is older */
  3836.         } else {
  3837.             rc = SKP_SIM;               /* "Would be sent", period. */
  3838.         }
  3839.     } else {
  3840.         rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  3841.     }
  3842.     t1 = gmstimer();                    /* End time */
  3843.     filcnt++;                           /* File number */
  3844.  
  3845. #ifdef GFTIMER
  3846.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3847.     fpxfsecs = sec;                     /* (for doxlog()) */
  3848. #else
  3849.     sec = (t1 - t0) / 1000;
  3850.     xfsecs = (int)sec;
  3851. #endif /* GFTIMER */
  3852.  
  3853.     debug(F111,"ftp sendrequest rc",local,rc);
  3854.  
  3855.     if (cancelfile || cancelgroup) {
  3856.         debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  3857.         ftscreen(SCR_ST,ST_INT,0l,"");
  3858.     } else if (rc > 0) {
  3859.         debug(F101,"ftp put skipped",local,rc);
  3860.         ftscreen(SCR_ST,ST_SKIP,rc,fullname);
  3861.     } else if (rc < 0) {
  3862.         debug(F111,"ftp put error",local,ftpcode);
  3863.         ftscreen(SCR_ST,ST_MSG,0L,&ftp_reply_str[4]);
  3864.     } else {
  3865.         debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  3866.         ftscreen(SCR_PT,'Z',0L,"");
  3867.         debug(F111,"ftp put ST_OK",local,rc);
  3868.         ftscreen(SCR_ST,ST_OK,0L,"");
  3869.         makestr(&sfspec,fullname);      /* For WHERE command */
  3870.         makestr(&srfspec,asname);
  3871.     }
  3872.  
  3873.     /* Final log entries */
  3874.  
  3875. #ifdef TLOG
  3876.     if (tralog) {
  3877.         if (rc > 0) {
  3878.             if (rc == SKP_XNX)
  3879.               tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  3880.             else if (rc == SKP_XUP)
  3881.               tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  3882.             else if (rc == SKP_SIM)
  3883.               tlog(F100," /simulate: WOULD BE SENT","",0);
  3884.             else
  3885.               tlog(F110," skipped:",gskreason(rc),0);
  3886.         } else if (rc == 0) {
  3887.             tlog(F101," complete, size", "", fsize);
  3888.         } else if (cancelfile) {
  3889.             tlog(F100," canceled by user","",0);
  3890.         } else {
  3891.             tlog(F110," failed:",ftp_reply_str,0);
  3892.         }
  3893.         if (!tlogfmt)
  3894.           doxlog(what,local,fsize,ftp_typ,!rc,"");
  3895.     }
  3896. #endif /* TLOG */
  3897.  
  3898.     if (rc < 0)                         /* PUT did not succeed */
  3899.       return(-1);                       /* so done. */
  3900.  
  3901.     if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
  3902.       return(SKP_SIM);
  3903.  
  3904. #ifdef UNIX
  3905.     /* Set permissions too? */
  3906.  
  3907.     if (prm) {                          /* Change permissions? */
  3908.         s = zgperm(local);              /* Get perms of local file */
  3909.         if (!s) s = "";
  3910.         x = strlen(s);
  3911.         if (x > 3) s += (x - 3);
  3912.         if (rdigits(s)) {
  3913.             ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  3914.             x =
  3915.               ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  3916.             tlog(F110, x ? " chmod" : " chmod failed",
  3917.                  s,
  3918.                  0
  3919.                  );
  3920.             if (!x)
  3921.               return(-1);
  3922.         }
  3923.     }
  3924. #endif /* UNIX */
  3925.  
  3926.     /* Disposition of source file */
  3927.  
  3928.     if (moving) {
  3929.         x = zdelet(local);
  3930.         tlog(F110, (x > -1) ?
  3931.              " deleted" : " failed to delete",
  3932.              local,
  3933.              0
  3934.              );
  3935.         if (x < 0)
  3936.           return(-1);
  3937.     } else if (mvto) {
  3938.         x = zrename(local,mvto);
  3939.         tlog(F110, (x > -1) ?
  3940.              " moved source to" : " failed to move source to",
  3941.              mvto,
  3942.              0
  3943.              );
  3944.         if (x < 0)
  3945.           return(-1);
  3946.         /* ftscreen(SCR_ST,ST_MSG,0L,mvto); */
  3947.  
  3948.     } else if (rnto) {
  3949.         char * s = rnto;
  3950. #ifndef NOSPL
  3951.         int y;                          /* Pass it thru the evaluator */
  3952.         extern int cmd_quoting;         /* for \v(filename) */
  3953.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  3954.             y = CKMAXPATH;
  3955.             s = (char *)asname;
  3956.             zzstring(rnto,&s,&y);
  3957.             s = (char *)asname;
  3958.         }
  3959. #endif /* NOSPL */
  3960.         if (s) if (*s) {
  3961.             int x;
  3962.             x = zrename(local,s);
  3963.             tlog(F110, (x > -1) ?
  3964.                  " renamed source file to" :
  3965.                  " failed to rename source file to",
  3966.                  s,
  3967.                  0
  3968.                  );
  3969.             if (x < 0)
  3970.               return(-1);
  3971.             /* ftscreen(SCR_ST,ST_MSG,0L,s); */
  3972.         }
  3973.     }
  3974.  
  3975.     /* Disposition of destination file */
  3976.  
  3977.     if (srvrn) {                        /* /SERVER-RENAME: */
  3978.         char * s = srvrn;
  3979. #ifndef NOSPL
  3980.         int y;                          /* Pass it thru the evaluator */
  3981.         extern int cmd_quoting; /* for \v(filename) */
  3982.         debug(F111,"ftp putfile srvrn",s,1);
  3983.  
  3984.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  3985.             y = CKMAXPATH;
  3986.             s = (char *)fullname;       /* We can recycle this buffer now */
  3987.             zzstring(srvrn,&s,&y);
  3988.             s = (char *)fullname;
  3989.         }
  3990. #endif /* NOSPL */
  3991.         debug(F111,"ftp putfile srvrn",s,2);
  3992.         if (s) if (*s) {
  3993.             int x;
  3994.             x = ftp_rename(asname,s);
  3995.             debug(F111,"ftp putfile ftp_rename",asname,x);
  3996.             tlog(F110, (x > 0) ?
  3997.                  " renamed destination file to" :
  3998.                  " failed to rename destination file to",
  3999.                  s,
  4000.                  0
  4001.                  );
  4002.             if (x < 1)
  4003.               return(-1);
  4004.         }
  4005.     }
  4006.     return(0);
  4007. }
  4008.  
  4009. static int
  4010. #ifdef CK_ANSIC
  4011. xxout(char c)
  4012. #else
  4013. xxout(c) char c;
  4014. #endif /* CK_ANSIC */
  4015. {
  4016.     return(zzout(dout,c));
  4017. }
  4018.  
  4019. static int
  4020. #ifdef CK_ANSIC
  4021. scrnout(char c)
  4022. #else
  4023. scrnout(c) char c;
  4024. #endif /* CK_ANSIC */
  4025. {
  4026.     return(putchar(c));
  4027. }
  4028.  
  4029. static int
  4030. #ifdef CK_ANSIC
  4031. pipeout(char c)
  4032. #else
  4033. pipeout(c) char c;
  4034. #endif /* CK_ANSIC */
  4035. {
  4036.     return(zmchout(c));
  4037. }
  4038.  
  4039. static int
  4040. ispathsep(c) int c; {
  4041.     switch (servertype) {
  4042.       case SYS_VMS:
  4043.       case SYS_TOPS10:
  4044.       case SYS_TOPS20:
  4045.         return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4046.       case SYS_OS2:
  4047.       case SYS_WIN32:
  4048.       case SYS_DOS:
  4049.         return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4050.       case SYS_VOS:
  4051.         return((c == '>') ? 1 : 0);
  4052.       default:
  4053.         return((c == '/') ? 1 : 0);
  4054.     }
  4055. }
  4056.  
  4057. static int
  4058. iscanceled() {
  4059. #ifdef CK_CURSES
  4060.     extern int ck_repaint();
  4061. #endif /* CK_CURSES */
  4062.     int x, rc = 0;
  4063.     char c = 0;
  4064.     if (cancelfile)
  4065.       return(1);
  4066.     x = conchk();                       /* Any chars waiting at console? */
  4067.     if (x-- > 0) {                      /* Yes...  */
  4068.         c = coninc(5);                  /* Get one */
  4069.         switch (c) {
  4070.           case 032:                     /* Ctrl-X or X */
  4071.           case 'z':
  4072.           case 'Z': cancelgroup++;      /* fall thru on purpose */
  4073.           case 030:                     /* Ctrl-Z or Z */
  4074.           case 'x':
  4075.           case 'X': cancelfile++; rc++; break;
  4076. #ifdef CK_CURSES
  4077.           case 'L':
  4078.           case 'l':
  4079.           case 014:                     /* Ctrl-L or L or Ctrl-W */
  4080.           case 027:
  4081.             ck_repaint();               /* Refresh screen */
  4082. #endif /* CK_CURSES */
  4083.         }
  4084.     }
  4085.     while (x-- > 0)                     /* Soak up any rest */
  4086.       c = coninc(1);
  4087.     return(rc);
  4088. }
  4089.  
  4090. /* zzsend - used by buffered output macros. */
  4091.  
  4092. static int
  4093. #ifdef CK_ANSIC
  4094. zzsend(int fd, CHAR c)
  4095. #else
  4096. zzsend(fd,c) int fd; CHAR c;
  4097. #endif /* CK_ANSIC */
  4098. {
  4099.     int rc;
  4100.     if (iscanceled())                   /* Check for cancellation */
  4101.       return(0);
  4102.  
  4103.     rc = (ftp_dpl == FPL_CLR) ?
  4104.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4105.         secure_putbuf(fd, ucbuf, nout);
  4106.  
  4107.     ucbuf[nout] = NUL;
  4108.  
  4109.     nout = 0;
  4110.     ucbuf[nout++] = c;
  4111.     spackets++;
  4112.     pktnum++;
  4113.     if (rc > -1 && fdispla != XYFD_B) {
  4114.         spktl = nout;
  4115.         ftscreen(SCR_PT,'D',spackets,NULL);
  4116.     }
  4117.     return(rc);
  4118. }
  4119.  
  4120. /* c m d l i n p u t  --  Command-line PUT */
  4121.  
  4122. int
  4123. cmdlinput(stay) int stay; {
  4124.     int x, rc = 0, done = 0, good = 0, status = 0;
  4125.     ULONG t0, t1;                       /* Times for stats */
  4126. #ifdef GFTIMER
  4127.     CKFLOAT sec;
  4128. #else
  4129.     int sec = 0;
  4130. #endif /* GFTIMER */
  4131.  
  4132.     if (quiet) {                        /* -q really means quiet */
  4133.         displa = 0;
  4134.         fdispla = 0;
  4135.     } else {
  4136.         displa = 1;
  4137.         fdispla = XYFD_B;
  4138.     }
  4139.     testing = 0;
  4140.     out2screen = 0;
  4141.     dpyactive = 0;
  4142.     what = W_FTP|W_SEND;
  4143.  
  4144. #ifndef NOSPL
  4145.     cmd_quoting = 0;
  4146. #endif /* NOSPL */
  4147.     sndsrc = nfils;
  4148.  
  4149.     t0 = gmstimer();                    /* Record starting time */
  4150.  
  4151.     while (!done && !cancelgroup) {     /* Loop for all files */
  4152.  
  4153.         cancelfile = 0;
  4154.         x = gnfile();                   /* Get next file from list(s) */
  4155.         if (x == 0)                     /* (see gnfile() comments...) */
  4156.           x = gnferror;
  4157.  
  4158.         switch (x) {
  4159.           case 1:                       /* File to send */
  4160.             rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
  4161.                          filnam,        /* Local file to send */
  4162.                          filnam,        /* Remote name for file */
  4163.                          forcetype,     /* Text/binary mode forced */
  4164.                          0,             /* Not moving */
  4165.                          NULL,          /* No move-to */
  4166.                          NULL,          /* No rename-to */
  4167.                          NULL,          /* No server-rename */
  4168.                          ftp_cnv,       /* Filename conversion */
  4169.                          0,             /* Unique-server-names */
  4170.                          -1,            /* All file types */
  4171.                          0,             /* No permissions */
  4172.                          -1,            /* No character sets */
  4173.                          -1,            /* No character sets */
  4174.                          0              /* No update or restart */
  4175.                          );
  4176.             if (rc > -1) {
  4177.                 good++;
  4178.                 status = 1;
  4179.             }
  4180.             if (cancelfile) {
  4181.                 continue;               /* Or break? */
  4182.             }
  4183.             if (rc < 0) {
  4184.                 ftp_fai++;
  4185.             }
  4186.             continue;                   /* Or break? */
  4187.  
  4188.           case 0:                       /* No more files, done */
  4189.             done++;
  4190.             continue;
  4191.  
  4192.           case -2:
  4193.           case -1:
  4194.             printf("?%s: file not found - \"%s\"\n",
  4195.                    puterror ? "Fatal" : "Warning",
  4196.                    filnam
  4197.                    );
  4198.             continue;                   /* or break? */
  4199.           case -3:
  4200.             printf("?Warning access denied - \"%s\"\n", filnam);
  4201.             continue;                   /* or break? */
  4202.           case -5:
  4203.             printf("?Too many files match\n");
  4204.             done++;
  4205.             break;
  4206.           case -6:
  4207.             if (good < 1)
  4208.               printf("?No files selected\n");
  4209.             done++;
  4210.             break;
  4211.           default:
  4212.             printf("?getnextfile() - unknown failure\n");
  4213.             done++;
  4214.         }
  4215.     }
  4216.     if (status > 0) {
  4217.         if (cancelgroup)
  4218.           status = 0;
  4219.         else if (cancelfile && good < 1)
  4220.           status = 0;
  4221.     }
  4222.     success = status;
  4223.     x = success;
  4224.     if (x > -1) {
  4225.         lastxfer = W_FTP|W_SEND;
  4226.         xferstat = success;
  4227.     }
  4228.     t1 = gmstimer();                    /* End time */
  4229. #ifdef GFTIMER
  4230.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4231.     if (!sec) sec = 0.001;
  4232.     fptsecs = sec;
  4233. #else
  4234.     sec = (t1 - t0) / 1000;
  4235.     if (!sec) sec = 1;
  4236. #endif /* GFTIMER */
  4237.     tfcps = (long) (tfc / sec);
  4238.     tsecs = (int)sec;
  4239.     lastxfer = W_FTP|W_SEND;
  4240.     xferstat = success;
  4241.     if (dpyactive)
  4242.       ftscreen(SCR_TC,0,0L,"");
  4243.  
  4244.     if (!stay)
  4245.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4246.     return(success);
  4247. }
  4248.  
  4249.  
  4250. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4251.  
  4252. int
  4253. #ifdef CK_ANSIC
  4254. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4255. #else
  4256. doftpput(cx,who) int cx, who;
  4257. #endif /* CK_ANSIC */
  4258. {
  4259.     struct FDB sf, fl, sw, cm;
  4260.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4261.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4262.     char * s, * s2;
  4263.  
  4264.     int x_csl, x_csr = -1;              /* Local and remote charsets */
  4265.     int x_xla = 0;
  4266.     int x_recurse = 0;
  4267.     char c, * p;                        /* Workers */
  4268. #ifdef PUTARRAY
  4269.     int range[2];                       /* Array range */
  4270.     char ** ap = NULL;                  /* Array pointer */
  4271.     int arrayx = -1;                    /* Array index */
  4272. #endif /* PUTARRAY */
  4273.     ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
  4274. #ifdef GFTIMER
  4275.     CKFLOAT sec;
  4276. #else
  4277.     int sec = 0;
  4278. #endif /* GFTIMER */
  4279.  
  4280.     struct stringint {                  /* Temporary array for switch values */
  4281.         char * sval;
  4282.         int ival;
  4283.     } pv[SND_MAX+1];
  4284.  
  4285.     success = 0;                        /* Assume failure */
  4286.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  4287.     out2screen = 0;                     /* Not outputting file to screen */
  4288.     putflags = 0;                       /* PUT options */
  4289.     x_cnv = ftp_cnv;                    /* Filename conversion */
  4290.     x_usn = ftp_usn;                    /* Unique server names */
  4291.     x_prm = ftp_prm;                    /* Permissions */
  4292.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  4293.       x_prm = alike;
  4294.  
  4295. #ifndef NOCSETS
  4296.     x_csr = ftp_csr;                    /* Inherit global server charset */
  4297.     x_csl = ftp_csl;
  4298.     if (x_csl < 0)
  4299.       x_csl = fcharset;
  4300.     x_xla = ftp_xla;
  4301. #endif /* NOCSETS */
  4302.  
  4303.     makestr(&filefile,NULL);            /* No filename list file yet. */
  4304.     putpath[0] = NUL;                   /* Initialize for syncdir(). */
  4305.     puterror = ftp_err;                 /* Inherit global error action. */
  4306.     what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
  4307.     asnambuf[0] = NUL;                  /* Clear as-name buffer */
  4308.  
  4309.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  4310.         ftp_typ = g_ftp_typ;
  4311.         /* g_ftp_typ = -1; */
  4312.     }
  4313.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4314.         pv[i].sval = NULL;              /* to null pointers */
  4315.         pv[i].ival = -1;                /* and -1 int values */
  4316.     }
  4317.     if (who == 0) {                     /* Called with unprefixed command */
  4318.         switch (cx) {
  4319.           case XXRSEN:  pv[SND_RES].ival = 1; break;
  4320.           case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4321.           case XXMOVE:  pv[SND_MOV].ival = 1; break;
  4322.           case XXMMOVE: pv[SND_MOV].ival = 1; /* fall thru */
  4323.           case XXMSE:   mput++; break;
  4324.         }
  4325.     } else {
  4326.         if (cx == FTP_MPU)
  4327.           mput++;
  4328.     }
  4329.     cmfdbi(&sw,                         /* First FDB - command switches */
  4330.            _CMKEY,                      /* fcode */
  4331.            "Filename, or switch",       /* hlpmsg */
  4332.            "",                          /* default */
  4333.            "",                          /* addtl string data */
  4334.            nputswi,                     /* addtl numeric data 1: tbl size */
  4335.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4336.            xxstring,                    /* Processing function */
  4337.            putswi,                      /* Keyword table */
  4338.            &sf                          /* Pointer to next FDB */
  4339.            );
  4340.     cmfdbi(&fl,                         /* 3rd FDB - local filespec */
  4341.            _CMFLD,                      /* fcode */
  4342.            "",                          /* hlpmsg */
  4343.            "",                          /* default */
  4344.            "",                          /* addtl string data */
  4345.            0,                           /* addtl numeric data 1 */
  4346.            0,                           /* addtl numeric data 2 */
  4347.            xxstring,
  4348.            NULL,
  4349.            &cm
  4350.            );
  4351.     cmfdbi(&cm,                         /* 4th FDB - Confirmation */
  4352.            _CMCFM,                      /* fcode */
  4353.            "",                          /* hlpmsg */
  4354.            "",                          /* default */
  4355.            "",                          /* addtl string data */
  4356.            0,                           /* addtl numeric data 1 */
  4357.            0,                           /* addtl numeric data 2 */
  4358.            NULL,
  4359.            NULL,
  4360.            NULL
  4361.            );
  4362.  
  4363.   again:
  4364.     cmfdbi(&sf,                         /* 2nd FDB - file to send */
  4365.            _CMIFI,                      /* fcode */
  4366.            "",                          /* hlpmsg */
  4367.            "",                          /* default */
  4368.            "",                          /* addtl string data */
  4369.            /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  4370.            nolinks | x_recurse,         /* addtl numeric data 1 */
  4371.            0,                           /* dirflg 0 means "not dirs only" */
  4372.            xxstring,
  4373.            NULL,
  4374.            mput ? &cm : &fl
  4375.            );
  4376.  
  4377.     while (1) {                         /* Parse 0 or more switches */
  4378.         x = cmfdb(&sw);                 /* Parse something */
  4379.         debug(F101,"ftp put cmfdb","",x);
  4380.         debug(F101,"ftp put fcode","",cmresult.fcode);
  4381.         if (x < 0)                      /* Error */
  4382.           goto xputx;                   /* or reparse needed */
  4383.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  4384.           break;
  4385.         c = cmgbrk();                   /* Get break character */
  4386.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  4387.         if (getval && !(cmresult.kflags & CM_ARG)) {
  4388.             printf("?This switch does not take arguments\n");
  4389.             x = -9;
  4390.             goto xputx;
  4391.         }
  4392.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4393.             printf("?This switch requires an argument\n");
  4394.             x = -9;
  4395.             goto xputx;
  4396.         }
  4397.         n = cmresult.nresult;           /* Numeric result = switch value */
  4398.         debug(F101,"ftp put switch","",n);
  4399.         switch (n) {                    /* Process the switch */
  4400.           case SND_AFT:                 /* Send /AFTER:date-time */
  4401.           case SND_BEF:                 /* Send /BEFORE:date-time */
  4402.           case SND_NAF:                 /* Send /NOT-AFTER:date-time */
  4403.           case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
  4404.             if (!getval) break;
  4405.             if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  4406.                 if (x == -3) {
  4407.                     printf("?Date-time required\n");
  4408.                     x = -9;
  4409.                 }
  4410.                 goto xputx;
  4411.             }
  4412.             pv[n].ival = 1;
  4413.             makestr(&(pv[n].sval),s);
  4414.             break;
  4415.  
  4416.           case SND_ASN:                 /* /AS-NAME: */
  4417.             debug(F101,"ftp put /as-name getval","",getval);
  4418.             if (!getval) break;
  4419.             if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  4420.                 if (x == -3) {
  4421.                     printf("?name required\n");
  4422.                     x = -9;
  4423.                 }
  4424.                 goto xputx;
  4425.             }
  4426.             makestr(&(pv[n].sval),brstrip(s));
  4427.             debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  4428.             if (pv[n].sval) pv[n].ival = 1;
  4429.             break;
  4430.  
  4431. #ifdef PUTARRAY
  4432.           case SND_ARR:                 /* /ARRAY */
  4433.             if (!getval) break;
  4434.             ap = NULL;
  4435.             if ((x = cmfld("Array name (a single letter will do)",
  4436.                            "",
  4437.                            &s,
  4438.                            NULL
  4439.                            )) < 0) {
  4440.                 if (x == -3)
  4441.                   break;
  4442.                 else
  4443.                   return(x);
  4444.             }
  4445.             if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  4446.                 printf("?Bad array: %s\n",s);
  4447.                 return(-9);
  4448.             }
  4449.             if (!(ap = a_ptr[x])) {
  4450.                 printf("?No such array: %s\n",s);
  4451.                 return(-9);
  4452.             }
  4453.             pv[n].ival = 1;
  4454.             pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
  4455.             pv[SND_RES].ival = 0;
  4456.             pv[SND_FIL].ival = 0;
  4457.             arrayx = x;
  4458.             break;
  4459. #endif /* PUTARRAY */
  4460.  
  4461.           case SND_BIN:                 /* /BINARY */
  4462.           case SND_TXT:                 /* /TEXT or /ASCII */
  4463.           case SND_TEN:                 /* /TENEX */
  4464.             pv[SND_BIN].ival = 0;
  4465.             pv[SND_TXT].ival = 0;
  4466.             pv[SND_TEN].ival = 0;
  4467.             pv[n].ival = 1;
  4468.             break;
  4469.  
  4470. #ifdef PUTPIPE
  4471.           case SND_CMD:                 /* These take no args */
  4472.             if (nopush) {
  4473.                 printf("?Sorry, system command access is disabled\n");
  4474.                 x = -9;
  4475.                 goto xputx;
  4476.             }
  4477. #ifdef PIPESEND
  4478.             else if (sndfilter) {
  4479.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  4480.                 x = -9;
  4481.                 goto xputx;
  4482.             }
  4483. #endif /* PIPESEND */
  4484.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  4485.             pv[n].ival = 1;             /* Just set the flag */
  4486.             pv[SND_ARR].ival = 0;
  4487.             break;
  4488. #endif /* PUTPIPE */
  4489.  
  4490. #ifdef CKSYMLINK
  4491.           case SND_LNK:
  4492.             nolinks = 0;
  4493.             goto again;
  4494.           case SND_NLK:
  4495.             nolinks = 2;
  4496.             goto again;
  4497. #endif /* CKSYMLINK */
  4498.  
  4499. #ifdef FTP_RESTART
  4500.           case SND_RES:                 /* /RECOVER (resend) */
  4501.             pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
  4502. #endif /* FTP_RESTART */
  4503.  
  4504.           case SND_NOB:
  4505.           case SND_DEL:                 /* /DELETE */
  4506.           case SND_SHH:                 /* /QUIET */
  4507.           case SND_UPD:                 /* /UPDATE */
  4508.           case SND_SIM:                 /* /UPDATE */
  4509.           case SND_USN:                 /* /UNIQUE */
  4510.             pv[n].ival = 1;             /* Just set the flag */
  4511.             break;
  4512.  
  4513.           case SND_REC:                 /* /RECURSIVE */
  4514.             recursive = 2;              /* Must be set before cmifi() */
  4515.             x_recurse = 1;
  4516.             goto again;
  4517.             break;
  4518.  
  4519. #ifdef UNIXOROSK
  4520.           case SND_DOT:                 /* /DOTFILES */
  4521.             matchdot = 1;
  4522.             break;
  4523.           case SND_NOD:                 /* /NODOTFILES */
  4524.             matchdot = 0;
  4525.             break;
  4526. #endif /* UNIXOROSK */
  4527.  
  4528.           case SND_ERR:                 /* /ERROR-ACTION */
  4529.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  4530.               goto xputx;
  4531.             pv[n].ival = x;
  4532.             break;
  4533.  
  4534.           case SND_EXC:                 /* Excludes */
  4535.             if (!getval) break;
  4536.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  4537.                 if (x == -3) {
  4538.                     printf("?Pattern required\n");
  4539.                     x = -9;
  4540.                 }
  4541.                 goto xputx;
  4542.             }
  4543.             if (s) if (!*s) s = NULL;
  4544.             makestr(&(pv[n].sval),s);
  4545.             if (pv[n].sval)
  4546.               pv[n].ival = 1;
  4547.             break;
  4548.  
  4549.           case SND_PRM:                 /* /PERMISSIONS */
  4550.             if (!getval)
  4551.               x = 1;
  4552.             else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  4553.               goto xputx;
  4554.             pv[SND_PRM].ival = x;
  4555.             break;
  4556.  
  4557. #ifdef PIPESEND
  4558.           case SND_FLT:                 /* /FILTER */
  4559.             debug(F101,"ftp put /filter getval","",getval);
  4560.             if (!getval) break;
  4561.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  4562.                 if (x == -3)
  4563.                   s = "";
  4564.                 else
  4565.                   goto xputx;
  4566.             }
  4567.             s = brstrip(s);
  4568.             y = strlen(s);
  4569.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  4570.                 if (s[x] != '\\') continue;
  4571.                 if (s[x+1] == 'v') break;
  4572.             }
  4573.             if (x == y) {
  4574.                 printf(
  4575.                 "?Filter must contain a replacement variable for filename.\n"
  4576.                        );
  4577.                 x = -9;
  4578.                 goto xputx;
  4579.             }
  4580.             if (s) if (!*s) s = NULL;
  4581.             makestr(&(pv[n].sval),s);
  4582.             if (pv[n].sval)
  4583.               pv[n].ival = 1;
  4584.             break;
  4585. #endif /* PIPESEND */
  4586.  
  4587.           case SND_NAM:                 /* /FILENAMES */
  4588.             if (!getval) break;
  4589.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  4590.               goto xputx;
  4591.             debug(F101,"ftp put /filenames","",x);
  4592.             pv[n].ival = x;
  4593.             break;
  4594.  
  4595.           case SND_SMA:                 /* Smaller / larger than */
  4596.           case SND_LAR:
  4597.             if (!getval) break;
  4598.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  4599.               goto xputx;
  4600.             pv[n].ival = y;
  4601.             break;
  4602.  
  4603.           case SND_FIL:                 /* Name of file containing filenames */
  4604.             if (!getval) break;
  4605.             if ((x = cmifi("Name of file containing list of filenames",
  4606.                                "",&s,&y,xxstring)) < 0) {
  4607.                 if (x == -3) {
  4608.                     printf("?Filename required\n");
  4609.                     x = -9;
  4610.                 }
  4611.                 goto xputx;
  4612.             } else if (y && iswild(s)) {
  4613.                 printf("?Wildcards not allowed\n");
  4614.                 x = -9;
  4615.                 goto xputx;
  4616.             }
  4617.             if (s) if (!*s) s = NULL;
  4618.             makestr(&(pv[n].sval),s);
  4619.             if (pv[n].sval) {
  4620.                 pv[n].ival = 1;
  4621.                 pv[SND_ARR].ival = 0;
  4622.             } else {
  4623.                 pv[n].ival = 0;
  4624.             }
  4625.             mput = 0;
  4626.             break;
  4627.  
  4628.           case SND_MOV:                 /* MOVE after */
  4629.           case SND_REN:                 /* RENAME after */
  4630.           case SND_SRN: {               /* SERVER-RENAME after */
  4631.               char * m = "";
  4632.               switch (n) {
  4633.                 case SND_MOV:
  4634.                   m = "device and/or directory for source file after sending";
  4635.                   break;
  4636.                 case SND_REN:
  4637.                   m = "new name for source file after sending";
  4638.                   break;
  4639.                 case SND_SRN:
  4640.                   m = "new name for destination file after sending";
  4641.                   break;
  4642.               }
  4643.               if (!getval) break;
  4644.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  4645.                   if (x == -3) {
  4646.                       printf("%s\n", n == SND_MOV ?
  4647.                              "?Destination required" :
  4648.                              "?New name required"
  4649.                              );
  4650.                       x = -9;
  4651.                   }
  4652.                   goto xputx;
  4653.               }
  4654.               if (s) if (!*s) s = NULL;
  4655.               makestr(&(pv[n].sval),brstrip(s));
  4656.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  4657.               break;
  4658.           }
  4659.           case SND_STA:                 /* Starting position (= PSEND) */
  4660.             if (!getval) break;
  4661.             if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  4662.               goto xputx;
  4663.             pv[n].ival = y;
  4664.             break;
  4665.  
  4666.           case SND_TYP:                 /* /TYPE */
  4667.             if (!getval) break;
  4668.             if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  4669.               goto xputx;
  4670.             pv[n].ival = (x == 2) ? -1 : x;
  4671.             break;
  4672.  
  4673. #ifndef NOCSETS
  4674.           case SND_CSL:                 /* Local character set */
  4675.           case SND_CSR:                 /* Remote (server) charset */
  4676.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  4677.               return((x == -3) ? -2 : x);
  4678.             if (n == SND_CSL)
  4679.               x_csl = x;
  4680.             else
  4681.               x_csr = x;
  4682.             x_xla = 1;                  /* Overrides global OFF setting */
  4683.             break;
  4684.  
  4685.           case SND_XPA:                 /* Transparent */
  4686.             x_xla = 0;
  4687.             x_csr = -1;
  4688.             x_csl = -1;
  4689.             break;
  4690. #endif /* NOCSETS */
  4691.         }
  4692.     }
  4693. #ifdef PIPESEND
  4694.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  4695.         if (sndfilter || pv[SND_FLT].ival > 0) {
  4696.             printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  4697.             x = -9;
  4698.             goto xputx;
  4699.         }
  4700.     }
  4701. #endif /* PIPESEND */
  4702.  
  4703.     cmarg = "";
  4704.     cmarg2 = asnambuf;
  4705.     line[0] = NUL;
  4706.     s = line;
  4707.     wild = 0;
  4708.  
  4709.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  4710.       case _CMIFI:                      /* Input filename */
  4711.         if (pv[SND_FIL].ival > 0) {
  4712.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4713.             x = -9;
  4714.             goto xputx;
  4715.         }
  4716.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  4717.         if (pv[SND_ARR].ival > 0)
  4718.           ckstrncpy(asnambuf,line,CKMAXPATH);
  4719.         else
  4720.           wild = cmresult.nresult;      /* Wild flag */
  4721.         debug(F111,"ftp put wild",line,wild);
  4722.         if (!wild && !recursive && !mput)
  4723.           nolinks = 0;
  4724.         break;
  4725.       case _CMFLD:                      /* Field */
  4726.         /* Only allowed with /COMMAND and /ARRAY */
  4727.         if (pv[SND_FIL].ival > 0) {
  4728.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  4729.             x = -9;
  4730.             goto xputx;
  4731.         }
  4732.         if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  4733. #ifdef CKROOT
  4734.             if (ckrooterr)
  4735.               printf("?Off limits: %s\n",cmresult.sresult);
  4736.             else
  4737. #endif /* CKROOT */
  4738.               printf("?%s - \"%s\"\n",
  4739.                    iswild(cmresult.sresult) ?
  4740.                    "No files match" : "File not found",
  4741.                    cmresult.sresult
  4742.                    );
  4743.             x = -9;
  4744.             goto xputx;
  4745.         }
  4746.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  4747.         if (pv[SND_ARR].ival > 0)
  4748.           ckstrncpy(asnambuf,line,CKMAXPATH);
  4749.         break;
  4750.       case _CMCFM:                      /* Confirmation */
  4751.         confirmed = 1;
  4752.         break;
  4753.       default:
  4754.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  4755.         x = -9;
  4756.         goto xputx;
  4757.     }
  4758.     debug(F110,"ftp put string",s,0);
  4759.     debug(F101,"ftp put confirmed","",confirmed);
  4760.  
  4761.     /* Save and change protocol and transfer mode */
  4762.     /* Global values are restored in main parse loop */
  4763.  
  4764.     g_displa = fdispla;
  4765.     g_skipbup = skipbup;
  4766.  
  4767.     if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
  4768.         g_skipbup = skipbup;
  4769.         skipbup = 1;
  4770.     }
  4771.     if (pv[SND_TYP].ival > -1) {        /* /TYPE */
  4772.         xfiletype = pv[SND_TYP].ival;
  4773.         if (xfiletype == 2)
  4774.           xfiletype = -1;
  4775.     }
  4776.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  4777.         forcetype = 1;                  /* So skip file scan */
  4778.         ftp_typ = FTT_BIN;              /* Set binary */
  4779.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  4780.         forcetype = 1;
  4781.         ftp_typ = FTT_ASC;
  4782.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  4783.         forcetype = 1;
  4784.         ftp_typ = FTT_TEN;
  4785.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  4786.         forcetype = 1;
  4787.         ftp_typ = binary;
  4788.         g_ftp_typ = binary;
  4789.     }
  4790.  
  4791. #ifdef PIPESEND
  4792.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  4793.         debug(F110,"PUT /COMMAND before stripping",s,0);
  4794.         s = brstrip(s);
  4795.         debug(F110,"PUT /COMMAND after stripping",s,0);
  4796.         if (!*s) {
  4797.             printf("?Sorry, a command to send from is required\n");
  4798.             x = -9;
  4799.             goto xputx;
  4800.         }
  4801.         cmarg = s;
  4802.     }
  4803. #endif /* PIPESEND */
  4804.  
  4805. /* Set up /MOVE and /RENAME */
  4806.  
  4807.     if (pv[SND_DEL].ival > 0 &&
  4808.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  4809.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  4810.         x = -9;
  4811.         goto xputx;
  4812.     }
  4813. #ifdef CK_TMPDIR
  4814.     if (pv[SND_MOV].ival > 0) {
  4815.         int len;
  4816.         char * p = pv[SND_MOV].sval;
  4817.         len = strlen(p);
  4818.         if (!isdir(p)) {                /* Check directory */
  4819. #ifdef CK_MKDIR
  4820.             char * s = NULL;
  4821.             s = (char *)malloc(len + 4);
  4822.             if (s) {
  4823.                 strcpy(s,p);            /* safe */
  4824. #ifdef datageneral
  4825.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  4826. #else
  4827.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  4828. #endif /* datageneral */
  4829.                 s[len++] = 'X';
  4830.                 s[len] = NUL;
  4831. #ifdef NOMKDIR
  4832.                 x = -1;
  4833. #else
  4834.                 x = zmkdir(s);
  4835. #endif /* NOMKDIR */
  4836.                 free(s);
  4837.                 if (x < 0) {
  4838.                     printf("?Can't create \"%s\"\n",p);
  4839.                     x = -9;
  4840.                     goto xputx;
  4841.                 }
  4842.             }
  4843. #else
  4844.             printf("?Directory \"%s\" not found\n",p);
  4845.             x = -9;
  4846.             goto xputx;
  4847. #endif /* CK_MKDIR */
  4848.         }
  4849.         makestr(&snd_move,p);
  4850.     }
  4851. #endif /* CK_TMPDIR */
  4852.  
  4853.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  4854.         char * p = pv[SND_REN].sval;
  4855.         if (!p) p = "";
  4856.         if (!*p) {
  4857.             printf("?New name required for /RENAME\n");
  4858.             x = -9;
  4859.             goto xputx;
  4860.         }
  4861.         p = brstrip(p);
  4862. #ifndef NOSPL
  4863.     /* If name given is wild, rename string must contain variables */
  4864.         if (wild) {
  4865.             char * s = tmpbuf;
  4866.             x = TMPBUFSIZ;
  4867.             zzstring(p,&s,&x);
  4868.             if (!strcmp(tmpbuf,p)) {
  4869.                 printf(
  4870.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  4871.                        );
  4872.                 x = -9;
  4873.                 goto xputx;
  4874.             }
  4875.         }
  4876. #endif /* NOSPL */
  4877.         makestr(&snd_rename,p);
  4878.         debug(F110,"FTP snd_rename",snd_rename,0);
  4879.     }
  4880.     if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
  4881.         char * p = pv[SND_SRN].sval;
  4882.         if (!p) p = "";
  4883.         if (!*p) {
  4884.             printf("?New name required for /SERVER-RENAME\n");
  4885.             x = -9;
  4886.             goto xputx;
  4887.         }
  4888.         p = brstrip(p);
  4889. #ifndef NOSPL
  4890.         if (wild) {
  4891.             char * s = tmpbuf;
  4892.             x = TMPBUFSIZ;
  4893.             zzstring(p,&s,&x);
  4894.             if (!strcmp(tmpbuf,p)) {
  4895.                 printf(
  4896. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  4897.                        );
  4898.                 x = -9;
  4899.                 goto xputx;
  4900.             }
  4901.         }
  4902. #endif /* NOSPL */
  4903.         makestr(&srv_renam,p);
  4904.         debug(F110,"ftp put srv_renam",srv_renam,0);
  4905.     }
  4906.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  4907.         char * lp;
  4908.         if (mput) {                     /* MPUT or MMOVE */
  4909.             nfils = 0;                  /* We already have the first one */
  4910. #ifndef NOMSEND
  4911.             msfiles[nfils++] = line;    /* Store pointer */
  4912.             lp = line + (int)strlen(line) + 1; /* Point past it */
  4913.             debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  4914.             while (1) {                 /* Get more filenames */
  4915.                 if ((x = cmifi("Names of files to send, separated by spaces",
  4916.                                "", &s,&y,xxstring)) < 0) {
  4917.                     if (x != -3)
  4918.                       goto xputx;
  4919.                     if ((x = cmcfm()) < 0)
  4920.                       goto xputx;
  4921.                     break;
  4922.                 }
  4923.                 msfiles[nfils++] = lp;  /* Got one, count it, point to it, */
  4924.                 p = lp;                 /* remember pointer, */
  4925.                 while ((*lp++ = *s++))  /* and copy it into buffer */
  4926.                   if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  4927.                       printf("?MPUT list too long\n");
  4928.                       line[0] = NUL;
  4929.                       x = -9;
  4930.                       goto xputx;
  4931.                   }
  4932.                 debug(F111,"ftp put mput nfils",msfiles[nfils-1],nfils-1);
  4933.                 if (nfils == 1) fspec[0] = NUL; /* Take care of \v(filespec) */
  4934. #ifdef ZFNQFP
  4935.                 zfnqfp(p,TMPBUFSIZ,tmpbuf);
  4936.                 p = tmpbuf;
  4937. #endif /* ZFNQFP */
  4938.                 if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  4939.                     strcat(fspec,p);    /* safe */
  4940.                     strcat(fspec," ");  /* safe */
  4941.                 } else
  4942. #ifdef COMMENT
  4943.                   printf("WARNING - \\v(filespec) buffer overflow\n");
  4944. #else
  4945.                   debug(F101,"doxput filespec buffer overflow","",0);
  4946. #endif /* COMMENT */
  4947.             }
  4948. #endif /* NOMSEND */
  4949.         } else {                        /* Regular SEND */
  4950.             nfils = -1;
  4951.             if ((x = cmtxt(wild ?
  4952. "\nOptional as-name template containing replacement variables \
  4953. like \\v(filename)" :
  4954.                            "Optional name to send it with",
  4955.                            "",&p,NULL)) < 0)
  4956.               goto xputx;
  4957.  
  4958.             if (p) if (!*p) p = NULL;
  4959.             p = brstrip(p);
  4960.  
  4961.             if (p && *p) {
  4962.                 makestr(&(pv[SND_ASN].sval),p);
  4963.                 if (pv[SND_ASN].sval)
  4964.                   pv[SND_ASN].ival = 1;
  4965.                 debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  4966.             }
  4967.         }
  4968.     }
  4969.     /* Set cmarg2 from as-name, however we got it. */
  4970.  
  4971.     CHECKCONN();
  4972.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  4973.         char * p;
  4974.         p = brstrip(pv[SND_ASN].sval);
  4975.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  4976.     }
  4977.     debug(F110,"ftp put asnambuf",asnambuf,0);
  4978.  
  4979.     if (pv[SND_FIL].ival > 0) {
  4980.         if (confirmed) {
  4981.             if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  4982.                 debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  4983.                 printf("?Failure to open %s\n",pv[SND_FIL].sval);
  4984.                 x = -9;
  4985.                 goto xputx;
  4986.             }
  4987.             makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  4988.             debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  4989.             wild = 1;
  4990.         }
  4991.     }
  4992.     if (confirmed && !line[0] && !filefile) {
  4993. #ifndef NOMSEND
  4994.         if (filehead) {                 /* OK if we have a SEND-LIST */
  4995.             nfils = filesinlist;
  4996.             sndsrc = nfils;             /* Like MSEND */
  4997.             addlist = 1;                /* But using a different list... */
  4998.             filenext = filehead;
  4999.             goto doput;
  5000.         }
  5001. #endif /* NOMSEND */
  5002.         printf("?Filename required but not given\n");
  5003.         x = -9;
  5004.         goto xputx;
  5005.     }
  5006. #ifndef NOMSEND
  5007.     addlist = 0;                        /* Don't use SEND-LIST. */
  5008. #endif /* NOMSEND */
  5009.  
  5010.     if (mput) {                         /* MPUT (rather than PUT) */
  5011. #ifndef NOMSEND
  5012.         cmlist = msfiles;               /* List of filespecs */
  5013.         sndsrc = nfils;                 /* rather filespec and as-name */
  5014. #endif /* NOMSEND */
  5015.         pipesend = 0;
  5016.     } else if (filefile) {              /* File contains list of filenames */
  5017.         s = "";
  5018.         cmarg = "";
  5019.         line[0] = NUL;
  5020.         nfils = 1;
  5021.         sndsrc = 1;
  5022.  
  5023.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5024.  
  5025.         /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5026.         nfils = sndsrc = -1;
  5027.         if (!wild) {
  5028.             y = zchki(s);
  5029.             if (y < 0) {
  5030.                 printf("?Read access denied - \"%s\"\n", s);
  5031.                 x = -9;
  5032.                 goto xputx;
  5033.             }
  5034.         }
  5035.         if (s != line)                  /* We might already have done this. */
  5036.           ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
  5037. #ifdef DEBUG
  5038.         else
  5039.           debug(F110,"doxput line=s",line,0);
  5040. #endif /* DEBUG */
  5041.         cmarg = line;                   /* File to send */
  5042.     }
  5043. #ifndef NOMSEND
  5044.     zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
  5045. #endif /* NOMSEND */
  5046.  
  5047.     if (!mput) {                        /* For all but MPUT... */
  5048. #ifdef PIPESEND
  5049.         if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
  5050.           pipesend = 1;
  5051.         debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5052.         if (pipesend && filefile) {
  5053.             printf("?Invalid switch combination\n");
  5054.             x = -9;
  5055.             goto xputx;
  5056.         }
  5057. #endif /* PIPESEND */
  5058.  
  5059. #ifndef NOSPL
  5060.     /* If as-name given and filespec is wild, as-name must contain variables */
  5061.         if ((wild || mput) && asnambuf[0]) {
  5062.             char * s = tmpbuf;
  5063.             x = TMPBUFSIZ;
  5064.             zzstring(asnambuf,&s,&x);
  5065.             if (!strcmp(tmpbuf,asnambuf)) {
  5066.                 printf(
  5067.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5068.                        );
  5069.                 x = -9;
  5070.                 goto xputx;
  5071.             }
  5072.         }
  5073. #endif /* NOSPL */
  5074.     }
  5075.  
  5076.   doput:
  5077.  
  5078.     if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
  5079.         g_displa = fdispla;
  5080.         fdispla = 0;
  5081.         debug(F101,"ftp put display","",fdispla);
  5082.     } else {
  5083.         displa = 1;
  5084.         if (ftp_deb) {
  5085.             g_displa = fdispla;
  5086.             fdispla = XYFD_B;
  5087.         }
  5088.     }
  5089.  
  5090. #ifdef PUTARRAY                         /* SEND /ARRAY... */
  5091.     if (pv[SND_ARR].ival > 0) {
  5092.         if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5093.         if (range[0] == -1)             /* If low end of range not specified */
  5094.           range[0] = 1;                 /* default to 1 */
  5095.         if (range[1] == -1)             /* If high not specified */
  5096.           range[1] = a_dim[arrayx];     /* default to size of array */
  5097.         if ((range[0] < 0) ||           /* Check range */
  5098.             (range[0] > a_dim[arrayx]) ||
  5099.             (range[1] < range[0]) ||
  5100.             (range[1] > a_dim[arrayx])) {
  5101.             printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5102.             x = -9;
  5103.             goto xputx;
  5104.         }
  5105.         sndarray = ap;                  /* Array pointer */
  5106.         sndxin = arrayx;                /* Array index */
  5107.         sndxlo = range[0];              /* Array range */
  5108.         sndxhi = range[1];
  5109.         sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5110.         if (!asnambuf[0])
  5111.           ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5112.         cmarg = "";
  5113.     }
  5114. #endif /* PUTARRAY */
  5115.  
  5116.     moving = 0;
  5117.  
  5118.     if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
  5119.         if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
  5120.           moving = 1;
  5121.         if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
  5122.           ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5123.         if (pv[SND_BEF].ival > 0)
  5124.           ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5125.         if (pv[SND_NAF].ival > 0)
  5126.           ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5127.         if (pv[SND_NBE].ival > 0)
  5128.           ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5129.         if (pv[SND_EXC].ival > 0)
  5130.           makelist(pv[SND_EXC].sval,sndexcept,8);
  5131.         if (pv[SND_SMA].ival > -1)
  5132.           sndsmaller = pv[SND_SMA].ival;
  5133.         if (pv[SND_LAR].ival > -1)
  5134.           sndlarger = pv[SND_LAR].ival;
  5135.         if (pv[SND_NAM].ival > -1)
  5136.           x_cnv = pv[SND_NAM].ival;
  5137.         if (pv[SND_USN].ival > -1)
  5138.           x_usn = pv[SND_USN].ival;
  5139.         if (pv[SND_ERR].ival > -1)
  5140.           puterror = pv[SND_ERR].ival;
  5141. #ifdef DOUPDATE
  5142.         if (pv[SND_UPD].ival > 0) {
  5143.             if (x_usn) {
  5144.                 printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5145.                 x = -9;
  5146.                 goto xputx;
  5147.             }
  5148.             putflags |= PUT_UPD;
  5149.         }
  5150. #endif /* DOUPDATE */
  5151.  
  5152.         if (pv[SND_SIM].ival > 0)
  5153.           putflags |= PUT_SIM;
  5154.  
  5155. #ifdef UNIX
  5156.         if (pv[SND_PRM].ival > -1) {
  5157.             if (x_usn) {
  5158.                 printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5159.                 x = -9;
  5160.                 goto xputx;
  5161.             }
  5162.             x_prm = pv[SND_PRM].ival;
  5163.         }
  5164. #endif /* UNIX */
  5165. #ifdef FTP_RESTART
  5166.         if (pv[SND_RES].ival > 0) {
  5167.             if (x_usn || putflags) {
  5168.                 printf("?Conflicting switches: /RECOVER %s\n",
  5169.                        x_usn && putflags ? "/UNIQUE /UPDATE" :
  5170.                        (x_usn ? "/UNIQUE" : "/UPDATE")
  5171.                        );
  5172.                 x = -9;
  5173.                 goto xputx;
  5174.             }
  5175. #ifndef NOCSETS
  5176.             if (x_xla &&
  5177.                 (x_csl == FC_UCS2 ||
  5178.                  x_csl == FC_UTF8 ||
  5179.                  x_csr == FC_UCS2 ||
  5180.                  x_csr == FC_UTF8)) {
  5181.                 printf("?/RECOVER can not be used with Unicode translation\n");
  5182.                 x = -9;
  5183.                 goto xputx;
  5184.             }
  5185. #endif /* NOCSETS */
  5186.             putflags = PUT_RES;
  5187.         }
  5188. #endif /* FTP_RESTART */
  5189.     }
  5190.     debug(F101,"ftp PUT restart","",putflags == PUT_RES);
  5191.     debug(F101,"ftp PUT update","",putflags == PUT_UPD);
  5192.  
  5193. #ifdef PIPESEND
  5194.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  5195.         if (!pv[SND_FLT].sval) {
  5196.             sndfilter = NULL;
  5197.         } else {
  5198.             sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5199.             if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5200.         }
  5201.         debug(F110,"ftp put /FILTER", sndfilter, 0);
  5202.     }
  5203.     if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
  5204.       if (putflags)                     /* with pipes or filters */
  5205.         putflags = 0;
  5206. #endif /* PIPESEND */
  5207.  
  5208.     tfc = 0L;                           /* Initialize stats and counters */
  5209.     filcnt = 0;
  5210.     pktnum = 0;
  5211.     spackets = 0L;
  5212.  
  5213.     if (wild)                           /* (is this necessary?) */
  5214.       cx = FTP_MPU;
  5215.  
  5216.     t0 = gmstimer();                    /* Record starting time */
  5217.  
  5218.     done = 0;                           /* Loop control */
  5219.     cancelgroup = 0;
  5220.  
  5221.     cdlevel = 0;
  5222.     cdsimlvl = 0;
  5223.     while (!done && !cancelgroup) {     /* Loop for all files */
  5224.                                         /* or until canceled. */
  5225. #ifdef FTP_PROXY
  5226.         /*
  5227.            If we are using a proxy, we don't use the local file list;
  5228.            instead we use the list on the remote machine which we want
  5229.            sent to someone else, and we use remglob() to get the names.
  5230.            But in that case we shouldn't even be executing this routine;
  5231.            see ftp_mput().
  5232.         */
  5233. #endif /* FTP_PROXY */
  5234.  
  5235.         cancelfile = 0;
  5236.         x = gnfile();                   /* Get next file from list(s) */
  5237.         if (x == 0)                     /* (see gnfile() comments...) */
  5238.           x = gnferror;
  5239.         debug(F111,"FTP PUT gnfile",filnam,x);
  5240.  
  5241.         switch (x) {
  5242.           case 1:                       /* File to send */
  5243.             s2 = asnambuf;
  5244. #ifndef NOSPL
  5245.             if (asnambuf[0]) {          /* As-name */
  5246.                 int n; char *p;         /* to be evaluated... */
  5247.                 n = TMPBUFSIZ;
  5248.                 p = tmpbuf;
  5249.                 zzstring(asnambuf,&p,&n);
  5250.                 s2 = tmpbuf;
  5251.                 debug(F110,"ftp put asname",s2,0);
  5252.             }
  5253. #endif /* NOSPL */
  5254.             rc = putfile(cx,            /* Function (PUT, APPEND) */
  5255.                     filnam, s2,         /* Name to send, as-name */
  5256.                     forcetype, moving,  /* Parameters from switches... */
  5257.                     snd_move, snd_rename, srv_renam,
  5258.                     x_cnv, x_usn, xfiletype, x_prm,
  5259. #ifndef NOCSETS
  5260.                     x_csl, (!x_xla ? -1 : x_csr),
  5261. #else
  5262.                     -1, -1,
  5263. #endif /* NOCSETS */
  5264.                     putflags
  5265.                     );
  5266.             debug(F111,"ftp put putfile rc",filnam,rc);
  5267.             debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  5268.             debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  5269.             if (rc > -1) {
  5270.                 good++;
  5271.                 status = 1;
  5272.             }
  5273.             if (cancelfile)
  5274.               continue;
  5275.             if (rc < 0) {
  5276.                 ftp_fai++;
  5277.                 if (puterror) {
  5278.                     status = 0;
  5279.                     printf("?Fatal upload error: %s\n",filnam);
  5280.                     done++;
  5281.                 }
  5282.             }
  5283.             continue;
  5284.           case 0:                       /* No more files, done */
  5285.             done++;
  5286.             continue;
  5287.           case -1:
  5288.             printf("?%s: file not found - \"%s\"\n",
  5289.                    puterror ? "Fatal" : "Warning",
  5290.                    filnam
  5291.                    );
  5292.             if (puterror) {
  5293.                 status = 0;
  5294.                 done++;
  5295.                 break;
  5296.             }
  5297.             continue;
  5298.           case -2:
  5299.             if (puterror) {
  5300.                 printf("?Fatal: file not found - \"%s\"\n", filnam);
  5301.                 status = 0;
  5302.                 done++;
  5303.                 break;
  5304.             }
  5305.             continue;                   /* Not readable, keep going */
  5306.           case -3:
  5307.             if (puterror) {
  5308.                 printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  5309.                 status = 0;
  5310.                 done++;
  5311.                 break;
  5312.             }
  5313.             printf("?Warning access denied - \"%s\"\n", filnam);
  5314.             continue;
  5315. #ifdef COMMENT
  5316.           case -4:                      /* Canceled */
  5317.             done++;
  5318.             break;
  5319. #endif /* COMMENT */
  5320.           case -5:
  5321.             printf("?Too many files match\n");
  5322.             done++;
  5323.             break;
  5324.           case -6:
  5325.             if (good < 1)
  5326.               printf("?No files selected\n");
  5327.             done++;
  5328.             break;
  5329.           default:
  5330.             printf("?getnextfile() - unknown failure\n");
  5331.             done++;
  5332.         }
  5333.     }
  5334.     if (cdlevel > 0) {
  5335.         while (cdlevel--) {
  5336.             if (cdsimlvl) {
  5337.                 cdsimlvl--;
  5338.             } else if (!doftpcdup())
  5339.               break;
  5340.         }
  5341.     }
  5342.     if (status > 0) {
  5343.         if (cancelgroup)
  5344.           status = 0;
  5345.         else if (cancelfile && good < 1)
  5346.           status = 0;
  5347.     }
  5348.     success = status;
  5349.     x = success;
  5350.  
  5351.   xputx:
  5352.     if (x > -1) {
  5353. #ifdef GFTIMER
  5354.         t1 = gmstimer();                /* End time */
  5355.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5356.         if (!sec) sec = 0.001;
  5357.         fptsecs = sec;
  5358. #else
  5359.         sec = (t1 - t0) / 1000;
  5360.         if (!sec) sec = 1;
  5361. #endif /* GFTIMER */
  5362.         tfcps = (long) (tfc / sec);
  5363.         tsecs = (int)sec;
  5364.         lastxfer = W_FTP|W_SEND;
  5365.         xferstat = success;
  5366.         if (dpyactive)
  5367.           ftscreen(SCR_TC,0,0L,"");
  5368.     }
  5369.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  5370.         if (pv[i].sval)
  5371.           free(pv[i].sval);
  5372.     }
  5373.     ftreset();                          /* Undo switch effects */
  5374.     dpyactive = 0;
  5375.     return(x);
  5376. }
  5377.  
  5378.  
  5379. static char ** mgetlist = NULL;         /* For MGET */
  5380. static int mgetn = 0, mgetx = 0;
  5381. static char xtmpbuf[4096];
  5382.  
  5383. /*
  5384.   c m d l i n g e t
  5385.  
  5386.   Get files specified by -g command-line option.
  5387.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  5388. */
  5389. int
  5390. cmdlinget(stay) int stay; {
  5391.     extern int fncnv;
  5392.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  5393.     int lcs = -1, rcs = -1, xlate = 0;
  5394.     int first = 1;
  5395.     int mget = 1;
  5396.     int nc;
  5397.     char * s, * s2, * s3;
  5398.     ULONG t0, t1;                       /* Times for stats */
  5399. #ifdef GFTIMER
  5400.     CKFLOAT sec;
  5401. #else
  5402.     int sec = 0;
  5403. #endif /* GFTIMER */
  5404.  
  5405.     if (quiet) {                        /* -q really means quiet */
  5406.         displa = 0;
  5407.         fdispla = 0;
  5408.     } else {
  5409.         displa = 1;
  5410.         fdispla = XYFD_B;
  5411.     }
  5412.     testing = 0;
  5413.     dpyactive = 0;
  5414.     out2screen = 0;
  5415.     what = W_FTP|W_RECV;
  5416.  
  5417. #ifndef NOSPL
  5418.     cmd_quoting = 0;
  5419. #endif /* NOSPL */
  5420.     debug(F101,"ftp cmdlinget nfils","",nfils);
  5421.  
  5422.     if (ftp_cnv == SET_AUTO) {          /* Name conversion is auto */
  5423.         if (alike) {                    /* If server & client are alike */
  5424.             nc = 0;                     /* no conversion */
  5425.         } else {                        /* If they are different */
  5426.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  5427.               nc = -1;                  /* only minimal conversions needed */
  5428.             else                        /* otherwise */
  5429.               nc = 1;                   /* full conversion */
  5430.         }
  5431.     } else                              /* Not auto - do what user said */
  5432.       nc = ftp_cnv;
  5433.  
  5434.     if (nfils < 1)
  5435.       doexit(BAD_EXIT,-1);
  5436.  
  5437.     t0 = gmstimer();                    /* Starting time for this batch */
  5438.  
  5439. #ifndef NOCSETS
  5440.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  5441.         lcs = ftp_csl;                  /* Local charset */
  5442.         if (lcs < 0) lcs = fcharset;
  5443.         if (lcs < 0) xlate = 0;
  5444.     }
  5445.     if (xlate) {                        /* Still ON? */
  5446.         rcs = ftp_csx;                  /* Remote (Server) charset */
  5447.         if (rcs < 0) rcs = ftp_csr;
  5448.         if (rcs < 0) xlate = 0;
  5449.     }
  5450. #endif /* NOCSETS */
  5451.     /*
  5452.       If we have only one file and it is a directory, then we ask for a
  5453.       listing of its contents, rather than retrieving the directory file
  5454.       itself.  This is what (e.g.) Netscape does.
  5455.     */
  5456.     if (nfils == 1) {
  5457.         if (doftpcwd((CHAR *)cmlist[mgetx],-1)) {
  5458.             /* If we can CD to it, it must be a directory */
  5459.             if (recursive) {
  5460.                 cmlist[mgetx] = "*";
  5461.             } else {
  5462.                 status =
  5463.                   (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  5464.                 done = 1;
  5465.             }
  5466.         }
  5467.     }
  5468. /*
  5469.   The following is to work around UNIX servers which, when given a command
  5470.   like "NLST path/blah" (not wild) returns the basename without the path.
  5471. */
  5472.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  5473.         mget = iswild(cmlist[mgetx]);
  5474.     }
  5475.     if (!mget && !done) {               /* Invoked by command-line FTP URL */
  5476.         if (ftp_deb)
  5477.           printf("DOING GET...\n");
  5478.         done++;
  5479.         cancelfile = 0;                 /* This file not canceled yet */
  5480.         s = cmlist[mgetx];
  5481.         rc = 0;                         /* Initial return code */
  5482.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5483.         fsize = -1L;
  5484.         if (x == REPLY_COMPLETE)
  5485.           fsize = atol(&ftp_reply_str[4]);
  5486.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  5487.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5488.  
  5489.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5490.         s2 = tmpbuf;
  5491.  
  5492.         /* If local file already exists, take collision action */
  5493.  
  5494.         x = zchki(s2);
  5495.         if (x > -1) {
  5496.             switch (fncnv) {
  5497.               case XYFX_A:              /* Append */
  5498.                 append = 1;
  5499.                 break;
  5500.               case XYFX_R:              /* Rename */
  5501.               case XYFX_B: {            /* Backup */
  5502.                   char * p = NULL;
  5503.                   int x = -1;
  5504.                   znewn(s2,&p);         /* Make unique name */
  5505.                   debug(F110,"ftp get znewn",p,0);
  5506.                   if (fncnv == XYFX_B) { /* Backup existing file */
  5507.                       x = zrename(s2,p);
  5508.                       debug(F111,"ftp get backup zrename",p,x);
  5509.                   } else {              /* Rename incoming file */
  5510.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5511.                       s2 = tmpbuf;
  5512.                       debug(F111,"ftp get rename incoming",p,x);
  5513.                   }
  5514.                   if (x < 0) {
  5515.                       printf("?Backup/Rename failed\n");
  5516.                       return(success = 0);
  5517.                   }
  5518.                   break;
  5519.               }
  5520.               case XYFX_D:              /* Discard */
  5521.                 ftscreen(SCR_FN,'F',0L,s);
  5522.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5523.                 tlog(F100," refused: name","",0);
  5524.                 debug(F110,"ftp get skip name",s2,0);
  5525.                 goto xclget;
  5526.  
  5527.               case XYFX_X:              /* Overwrite */
  5528.               case XYFX_U:              /* Update (already handled above) */
  5529.                 break;
  5530.             }
  5531.         }
  5532.         rc = getfile(s,                 /* Remote name */
  5533.                      s2,                /* Local name */
  5534.                      0,                 /* Recover/Restart */
  5535.                      append,            /* Append */
  5536.                      NULL,              /* Pipename */
  5537.                      0,                 /* Translate charsets */
  5538.                      -1,                /* File charset (none) */
  5539.                      -1                 /* Server charset (none) */
  5540.                      );
  5541.         debug(F111,"ftp get rc",s,rc);
  5542.         debug(F111,"ftp get cancelfile",s,cancelfile);
  5543.         debug(F111,"ftp get cancelgroup",s,cancelgroup);
  5544.  
  5545.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  5546.             rc = getfile(&s[1],         /* Remote name without leading '/' */
  5547.                          s2,            /* Local name */
  5548.                          0,             /* Recover/Restart */
  5549.                          append,        /* Append */
  5550.                          NULL,          /* Pipename */
  5551.                          0,             /* Translate charsets */
  5552.                          -1,            /* File charset (none) */
  5553.                          -1             /* Server charset (none) */
  5554.                          );
  5555.         if (rc > -1) {
  5556.             good++;
  5557.             status = 1;
  5558.         }
  5559.         if (cancelfile)
  5560.           goto xclget;
  5561.         if (rc < 0) {
  5562.             ftp_fai++;
  5563.             if (geterror) {
  5564.                 status = 0;
  5565.                 done++;
  5566.             }
  5567.         }
  5568.     }
  5569.     if (ftp_deb && !done)
  5570.       printf("DOING MGET...\n");
  5571.     while (!done && !cancelgroup) {
  5572.         cancelfile = 0;                 /* This file not canceled yet */
  5573.         s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],0);
  5574.         if (!s) s = "";
  5575.         if (!*s) {
  5576.             first = 1;
  5577.             mgetx++;
  5578.             if (mgetx < nfils)
  5579.               s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],0);
  5580.             else
  5581.               s = NULL;
  5582.             debug(F111,"ftp mget remote_files B",s,0);
  5583.             if (!s) {
  5584.                 done = 1;
  5585.                 break;
  5586.             }
  5587.         }
  5588.  
  5589.         /*
  5590.           The semantics of NLST are ill-defined.  Suppose we have just sent
  5591.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  5592.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  5593.           RETR commands based on the pathless names are not going to work.
  5594.         */
  5595.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  5596.             if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  5597.                 int len, left = 4096;
  5598.                 char * tmp = xtmpbuf;
  5599.                 len = s3 - cmlist[mgetx] + 1;
  5600.                 ckstrncpy(tmp,cmlist[mgetx],left);
  5601.                 tmp += len;
  5602.                 left -= len;
  5603.                 ckstrncpy(tmp,s,left);
  5604.                 s = xtmpbuf;
  5605.             }
  5606.         }
  5607.         first = 0;                      /* Not first any more */
  5608.  
  5609.         rc = 0;                         /* Initial return code */
  5610.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  5611.         fsize = -1L;
  5612.         if (x == REPLY_COMPLETE)
  5613.           fsize = atol(&ftp_reply_str[4]);
  5614.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  5615.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  5616.  
  5617.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  5618.         s2 = tmpbuf;
  5619.  
  5620.         /* If local file already exists, take collision action */
  5621.  
  5622.         x = zchki(s2);
  5623.         if (x > -1) {
  5624.             switch (fncnv) {
  5625.               case XYFX_A:              /* Append */
  5626.                 append = 1;
  5627.                 break;
  5628.               case XYFX_R:              /* Rename */
  5629.               case XYFX_B: {            /* Backup */
  5630.                   char * p = NULL;
  5631.                   int x = -1;
  5632.                   znewn(s2,&p);         /* Make unique name */
  5633.                   debug(F110,"ftp get znewn",p,0);
  5634.                   if (fncnv == XYFX_B) { /* Backup existing file */
  5635.                       x = zrename(s2,p);
  5636.                       debug(F111,"ftp get backup zrename",p,x);
  5637.                   } else {              /* Rename incoming file */
  5638.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  5639.                       s2 = tmpbuf;
  5640.                       debug(F111,"ftp get rename incoming",p,x);
  5641.                   }
  5642.                   if (x < 0) {
  5643.                       printf("?Backup/Rename failed\n");
  5644.                       return(success = 0);
  5645.                   }
  5646.                   break;
  5647.               }
  5648.               case XYFX_D:      /* Discard */
  5649.                 ftscreen(SCR_FN,'F',0L,s);
  5650.                 ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  5651.                 tlog(F100," refused: name","",0);
  5652.                 debug(F110,"ftp get skip name",s2,0);
  5653.                 continue;
  5654.               case XYFX_X:              /* Overwrite */
  5655.               case XYFX_U:              /* Update (already handled above) */
  5656.                 break;
  5657.             }
  5658.         }
  5659.                                         /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  5660.         rc = getfile(s,                 /* Remote name */
  5661.                      s2,                /* Local name */
  5662.                      0,                 /* Recover/Restart */
  5663.                      append,            /* Append */
  5664.                      NULL,              /* Pipename */
  5665.                      0,                 /* Translate charsets */
  5666.                      -1,                /* File charset (none) */
  5667.                      -1                 /* Server charset (none) */
  5668.                      );
  5669.         debug(F111,"ftp get rc",s,rc);
  5670.         debug(F111,"ftp get cancelfile",s,cancelfile);
  5671.         debug(F111,"ftp get cancelgroup",s,cancelgroup);
  5672.  
  5673.         if (rc > -1) {
  5674.             good++;
  5675.             status = 1;
  5676.         }
  5677.         if (cancelfile)
  5678.           continue;
  5679.         if (rc < 0) {
  5680.             ftp_fai++;
  5681.             if (geterror) {
  5682.                 status = 0;
  5683.                 done++;
  5684.             }
  5685.         }
  5686.     }
  5687.  
  5688.   xclget:
  5689.     if (status > 0) {
  5690.         if (cancelgroup)
  5691.           status = 0;
  5692.         else if (cancelfile && good < 1)
  5693.           status = 0;
  5694.     }
  5695.     success = status;
  5696.  
  5697. #ifdef GFTIMER
  5698.     t1 = gmstimer();                    /* End time */
  5699.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5700.     if (!sec) sec = 0.001;
  5701.     fptsecs = sec;
  5702. #else
  5703.     sec = (t1 - t0) / 1000;
  5704.     if (!sec) sec = 1;
  5705. #endif /* GFTIMER */
  5706.  
  5707.     tfcps = (long) (tfc / sec);
  5708.     tsecs = (int)sec;
  5709.     lastxfer = W_FTP|W_RECV;
  5710.     xferstat = success;
  5711.     if (dpyactive)
  5712.       ftscreen(SCR_TC,0,0L,"");
  5713.     if (!stay)
  5714.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  5715.     return(success);
  5716. }
  5717.  
  5718. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  5719.  
  5720. /*
  5721.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  5722.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  5723.   the user-interface perspective, since the server's directory listings show
  5724.   its own local times and since we don't know what timezone it's in, there's
  5725.   no way to reconcile our local times with the server's.
  5726. */
  5727. int
  5728. doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  5729.     struct FDB fl, sw, cm;
  5730.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  5731.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  5732.     int x_fnc = 0, first = 0, skipthis = 0, append = 0;
  5733.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  5734.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  5735.     long getlarger = -1, getsmaller = -1;
  5736.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  5737.     char * src = "", * local = "";
  5738.  
  5739.     int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
  5740.     int x_xla = 0;
  5741.     char c;                             /* Worker char */
  5742.     ULONG t0 = 0L, t1;                  /* Times for stats */
  5743. #ifdef GFTIMER
  5744.     CKFLOAT sec;
  5745. #else
  5746.     int sec = 0;
  5747. #endif /* GFTIMER */
  5748.  
  5749.     struct stringint {                  /* Temporary array for switch values */
  5750.         char * sval;
  5751.         int ival;
  5752.     } pv[SND_MAX+1];
  5753.  
  5754.     success = 0;                        /* Assume failure */
  5755.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  5756.     restart = 0;                        /* No restart yet */
  5757.     out2screen = 0;
  5758.     x_cnv = ftp_cnv;                    /* Filename conversion */
  5759.     if (x_cnv == SET_AUTO)              /* Auto? */
  5760.       x_cnv = alike ? 0 : 1;            /* No conversion if alike */
  5761.  
  5762.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  5763.     x_prm = ftp_prm;                    /* Permissions */
  5764.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  5765.       x_prm = alike;
  5766.  
  5767. #ifndef NOCSETS
  5768.     x_csr = ftp_csr;                    /* Inherit global server charset */
  5769.     x_csl = ftp_csl;                    /* Inherit global local charset */
  5770.     if (x_csl < 0)                      /* If none, use current */
  5771.       x_csl = fcharset;                 /* file character-set. */
  5772.     x_xla = ftp_xla;                    /* Translation On/Off */
  5773. #endif /* NOCSETS */
  5774.  
  5775.     geterror = ftp_err;                 /* Inherit global error action. */
  5776.     asnambuf[0] = NUL;                  /* No as-name yet. */
  5777.     pipesave = pipesend;
  5778.     pipesend = 0;
  5779.  
  5780.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  5781.         ftp_typ = g_ftp_typ;
  5782.         /* g_ftp_typ = -1; */
  5783.     }
  5784.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  5785.         pv[i].sval = NULL;              /* to null pointers */
  5786.         pv[i].ival = -1;                /* and -1 int values */
  5787.     }
  5788.     zclose(ZMFILE);                     /* In case it was left open */
  5789.  
  5790.     if (fp_nml) {                       /* Reset /NAMELIST */
  5791.         if (fp_nml != stdout)
  5792.           fclose(fp_nml);
  5793.         fp_nml = NULL;
  5794.     }
  5795.     makestr(&ftp_nml,NULL);
  5796.  
  5797.     /* Initialize list of remote filespecs */
  5798.  
  5799.     if (!mgetlist) {
  5800.         mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  5801.         if (!mgetlist) {
  5802.             printf("?Memory allocation failure - MGET list\n");
  5803.             return(-9);
  5804.         }
  5805.         for (i = 0; i < MGETMAX; i++)
  5806.           mgetlist[i] = NULL;
  5807.     }
  5808.     mgetn = 0;                          /* Items in list */
  5809.     mgetx = 0;                          /* Current item */
  5810.  
  5811.     ftp_knf = ftp_deb;                  /* Keep NLST file? */
  5812.  
  5813.     if (who == 0) {                     /* Called with unprefixed command */
  5814.         if (cx == XXGET || cx == XXREGET)
  5815.           getone++;
  5816.         switch (cx) {
  5817.           case XXREGET: pv[SND_RES].ival = 1; break;
  5818.           case XXRETR:  pv[SND_DEL].ival = 1; break;
  5819.           case XXGET:
  5820.           case XXMGET:  mget++; break;
  5821.         }
  5822.     } else {                            /* FTP command */
  5823.         if (cx == FTP_GET || cx == FTP_RGE)
  5824.           getone++;
  5825.         switch (cx) {
  5826.           case FTP_DEL:                 /* (fall thru on purpose) */
  5827.           case FTP_MDE: mdel++;         /* (ditto) */
  5828.           case FTP_GET:                 /* (ditto) */
  5829.           case FTP_MGE: mget++; break;
  5830.           case FTP_RGE: pv[SND_RES].ival = 1; break;
  5831.         }
  5832.     }
  5833.     cmfdbi(&sw,                         /* First FDB - command switches */
  5834.            _CMKEY,                      /* fcode */
  5835.            "Remote filename;\n or switch", /* hlpmsg */
  5836.            "",                          /* default */
  5837.            "",                          /* addtl string data */
  5838.            mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  5839.            4,                           /* addtl numeric data 2: 4 = cmswi */
  5840.            xxstring,                    /* Processing function */
  5841.            mdel ? delswi : getswi,      /* Keyword table */
  5842.            &fl                          /* Pointer to next FDB */
  5843.            );
  5844.     cmfdbi(&fl,                         /* 2nd FDB - remote filename */
  5845.            _CMFLD,                      /* fcode */
  5846.            "",                          /* hlpmsg */
  5847.            "",                          /* default */
  5848.            "",                          /* addtl string data */
  5849.            0,                           /* addtl numeric data 1 */
  5850.            0,                           /* addtl numeric data 2 */
  5851.            xxstring,
  5852.            NULL,
  5853.            &cm
  5854.            );
  5855.     cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
  5856.            _CMCFM,                      /* fcode */
  5857.            "",                          /* hlpmsg */
  5858.            "",                          /* default */
  5859.            "",                          /* addtl string data */
  5860.            0,                           /* addtl numeric data 1 */
  5861.            0,                           /* addtl numeric data 2 */
  5862.            NULL,
  5863.            NULL,
  5864.            NULL
  5865.            );
  5866.  
  5867.     while (1) {                         /* Parse 0 or more switches */
  5868.         x = cmfdb(&sw);                 /* Parse something */
  5869.         debug(F101,"ftp get cmfdb","",x);
  5870.         if (x < 0)                      /* Error */
  5871.           goto xgetx;                   /* or reparse needed */
  5872.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  5873.           break;
  5874.         c = cmgbrk();                   /* Get break character */
  5875.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  5876.         if (getval && !(cmresult.kflags & CM_ARG)) {
  5877.             printf("?This switch does not take arguments\n");
  5878.             x = -9;
  5879.             goto xgetx;
  5880.         }
  5881.         n = cmresult.nresult;           /* Numeric result = switch value */
  5882.         debug(F101,"ftp get switch","",n);
  5883.  
  5884.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5885.             printf("?This switch requires an argument\n");
  5886.             x = -9;
  5887.             goto xgetx;
  5888.         }
  5889.         switch (n) {                    /* Process the switch */
  5890.           case SND_ASN:                 /* /AS-NAME: */
  5891.             debug(F101,"ftp get /as-name getval","",getval);
  5892.             if (!getval) break;
  5893.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  5894.                 if (x == -3) {
  5895.                     printf("?name required\n");
  5896.                     x = -9;
  5897.                 }
  5898.                 goto xgetx;
  5899.             }
  5900.             s = brstrip(s);
  5901.             if (!*s) s = NULL;
  5902.             makestr(&(pv[n].sval),s);
  5903.             pv[n].ival = 1;
  5904.             break;
  5905.  
  5906.           case SND_BIN:                 /* /BINARY */
  5907.           case SND_TXT:                 /* /TEXT or /ASCII */
  5908.           case SND_TEN:                 /* /TENEX */
  5909.             pv[SND_BIN].ival = 0;
  5910.             pv[SND_TXT].ival = 0;
  5911.             pv[SND_TEN].ival = 0;
  5912.             pv[n].ival = 1;
  5913.             break;
  5914.  
  5915. #ifdef PUTPIPE
  5916.           case SND_CMD:                 /* These take no args */
  5917.             if (nopush) {
  5918.                 printf("?Sorry, system command access is disabled\n");
  5919.                 x = -9;
  5920.                 goto xgetx;
  5921.             }
  5922. #ifdef PIPESEND
  5923.             else if (rcvfilter) {
  5924.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  5925.                 x = -9;
  5926.                 goto xgetx;
  5927.             }
  5928. #endif /* PIPESEND */
  5929.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  5930.             pv[n].ival = 1;             /* Just set the flag */
  5931.             pv[SND_ARR].ival = 0;
  5932.             break;
  5933. #endif /* PUTPIPE */
  5934.  
  5935.           case SND_SHH:                 /* /QUIET */
  5936.           case SND_RES:                 /* /RECOVER (reget) */
  5937.           case SND_NOB:                 /* /NOBACKUPFILES */
  5938.           case SND_DEL:                 /* /DELETE */
  5939.           case SND_UPD:                 /* /UPDATE */
  5940.           case SND_USN:                 /* /UNIQUE */
  5941.           case SND_NOD:                 /* /NODOTFILES */
  5942.           case SND_REC:                 /* /NODOTFILES */
  5943.           case SND_MAI:                 /* /TO-SCREEN */
  5944.             pv[n].ival = 1;             /* Just set the flag */
  5945.             break;
  5946.  
  5947.           case SND_COL:                 /* /COLLISION: */
  5948.             if ((x = cmkey(colxtab,ncolx,"","",xxstring)) < 0)
  5949.               goto xgetx;
  5950.             pv[n].ival = x;
  5951.             break;
  5952.  
  5953.           case SND_ERR:                 /* /ERROR-ACTION */
  5954.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  5955.               goto xgetx;
  5956.             pv[n].ival = x;
  5957.             break;
  5958.  
  5959.           case SND_EXC:                 /* Exception list */
  5960.             if (!getval) break;
  5961.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5962.                 if (x == -3) {
  5963.                     printf("?Pattern required\n");
  5964.                     x = -9;
  5965.                 }
  5966.                 goto xgetx;
  5967.             }
  5968.             if (s) if (!*s) s = NULL;
  5969.             makestr(&(pv[n].sval),s);
  5970.             if (pv[n].sval)
  5971.               pv[n].ival = 1;
  5972.             break;
  5973.  
  5974. #ifdef PIPESEND
  5975.           case SND_FLT:
  5976.             debug(F101,"ftp get /filter getval","",getval);
  5977.             if (!getval) break;
  5978.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  5979.                 if (x == -3)
  5980.                   s = "";
  5981.                 else
  5982.                   goto xgetx;
  5983.             }
  5984.             s = brstrip(s);
  5985.             if (pv[SND_MAI].ival < 1) {
  5986.                 y = strlen(s);
  5987.                 /* Make sure they included "\v(...)" */
  5988.                 for (x = 0; x < y; x++) {
  5989.                     if (s[x] != '\\') continue;
  5990.                     if (s[x+1] == 'v') break;
  5991.                 }
  5992.                 if (x == y) {
  5993.                     printf(
  5994.                 "?Filter must contain a replacement variable for filename.\n"
  5995.                            );
  5996.                     x = -9;
  5997.                     goto xgetx;
  5998.                 }
  5999.             }
  6000.             if (*s) {
  6001.                 pv[n].ival = 1;
  6002.                 makestr(&(pv[n].sval),s);
  6003.             } else {
  6004.                 pv[n].ival = 0;
  6005.                 makestr(&(pv[n].sval),NULL);
  6006.             }
  6007.             break;
  6008. #endif /* PIPESEND */
  6009.  
  6010.           case SND_NAM:
  6011.             if (!getval) break;
  6012.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  6013.               goto xgetx;
  6014.             debug(F101,"ftp get /filenames","",x);
  6015.             pv[n].ival = x;
  6016.             break;
  6017.  
  6018.           case SND_SMA:                 /* Smaller / larger than */
  6019.           case SND_LAR:
  6020.             if (!getval) break;
  6021.             if ((x = cmnum("Size in bytes","0",10,&y,xxstring)) < 0)
  6022.               goto xgetx;
  6023.             pv[n].ival = y;
  6024.             break;
  6025.  
  6026.           case SND_FIL:                 /* Name of file containing filnames */
  6027.             if (!getval) break;
  6028.             if ((x = cmifi("Name of file containing list of filenames",
  6029.                                "",&s,&y,xxstring)) < 0) {
  6030.                 if (x == -3) {
  6031.                     printf("?Filename required\n");
  6032.                     x = -9;
  6033.                 }
  6034.                 goto xgetx;
  6035.             } else if (y && iswild(s)) {
  6036.                 printf("?Wildcards not allowed BBB\n");
  6037.                 x = -9;
  6038.                 goto xgetx;
  6039.             }
  6040.             if (s) if (!*s) s = NULL;
  6041.             makestr(&(pv[n].sval),s);
  6042.             if (pv[n].sval)
  6043.               pv[n].ival = 1;
  6044.             break;
  6045.  
  6046.           case SND_MOV:                 /* MOVE after */
  6047.           case SND_REN:                 /* RENAME after */
  6048.           case SND_SRN: {               /* SERVER-RENAME */
  6049.               char * m = "";
  6050.               switch (n) {
  6051.                 case SND_MOV:
  6052.                   m =
  6053.                    "Device and/or directory for incoming file after reception";
  6054.                   break;
  6055.                 case SND_REN:
  6056.                   m = "New name for incoming file after reception";
  6057.                   break;
  6058.                 case SND_SRN:
  6059.                   m = "New name for source file on server after reception";
  6060.                   break;
  6061.               }
  6062.               if (!getval) break;
  6063.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6064.                   if (x == -3) {
  6065.                       printf("%s\n", n == SND_MOV ?
  6066.                              "?Destination required" :
  6067.                              "?New name required"
  6068.                              );
  6069.                       x = -9;
  6070.                   }
  6071.                   goto xgetx;
  6072.               }
  6073.               makestr(&(pv[n].sval),brstrip(s));
  6074.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  6075.               break;
  6076.           }
  6077. #ifndef NOCSETS
  6078.           case SND_CSL:                 /* Local character set */
  6079.           case SND_CSR:                 /* Remote (server) charset */
  6080.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6081.               return((x == -3) ? -2 : x);
  6082.             if (n == SND_CSL)
  6083.               x_csl = x;
  6084.             else
  6085.               x_csr = x;
  6086.             x_xla = 1;                  /* Overrides global OFF setting */
  6087.             break;
  6088.  
  6089.           case SND_XPA:                 /* Transparent */
  6090.             x_xla =  0;
  6091.             x_csr = -1;
  6092.             x_csl = -1;
  6093.             break;
  6094. #endif /* NOCSETS */
  6095.  
  6096.           case SND_NML:
  6097.             if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6098.               goto xgetx;
  6099.             makestr(&ftp_nml,s);
  6100.             break;
  6101.  
  6102.           default:                      /* /AFTER, /PERMISSIONS, etc... */
  6103.             printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6104.             x = -9;
  6105.             goto xgetx;
  6106.         }
  6107.     }
  6108.     line[0] = NUL;
  6109.     cmarg = line;
  6110.     cmarg2 = asnambuf;
  6111.     s = line;
  6112. /*
  6113.   For GET, we want to parse an optional as-name, like with PUT.
  6114.   For MGET, we must parse a list of names, and then send NLST commands
  6115.   for each name separately.
  6116. */
  6117.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  6118.       case _CMFLD:                      /* Field */
  6119.         if (!getone) {
  6120.             s = brstrip(cmresult.sresult);
  6121.             makestr(&(mgetlist[mgetn++]),s);
  6122.             while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6123.                 if (x < 0)
  6124.                   goto xgetx;
  6125.                 makestr(&(mgetlist[mgetn++]),brstrip(s));
  6126.                 if (mgetn >= MGETMAX) {
  6127.                     printf("?Too many items in MGET list\n");
  6128.                     goto xgetx;
  6129.                 }
  6130.             }
  6131.             if ((x = cmcfm()) < 0)
  6132.               goto xgetx;
  6133.         } else {
  6134.             s = brstrip(cmresult.sresult);
  6135.             ckstrncpy(line,s,LINBUFSIZ);
  6136.             if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6137.               if (x != -3)
  6138.                 goto xgetx;
  6139.             s = brstrip(s);
  6140.             ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6141.             if ((x = cmcfm()) < 0)
  6142.               goto xgetx;
  6143.         }
  6144.         break;
  6145.       case _CMCFM:                      /* Confirmation */
  6146.         break;
  6147.       default:
  6148.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  6149.         x = -9;
  6150.         goto xgetx;
  6151.     }
  6152.     if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
  6153.       recursive = 2;
  6154.  
  6155.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  6156.         forcetype = 1;                  /* So skip file scan */
  6157.         ftp_typ = XYFT_B;               /* Set binary */
  6158.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  6159.         forcetype = 1;
  6160.         ftp_typ = XYFT_T;
  6161.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  6162.         forcetype = 1;
  6163.         ftp_typ = FTT_TEN;
  6164.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  6165.         forcetype = 1;
  6166.         ftp_typ = binary;
  6167.         g_ftp_typ = binary;
  6168.     }
  6169.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  6170.         char * p;
  6171.         p = brstrip(pv[SND_ASN].sval);  /* As-name */
  6172.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  6173.     }
  6174.     debug(F110,"ftp get asnambuf",asnambuf,0);
  6175.  
  6176. #ifdef PIPESEND
  6177.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  6178.         char * p;
  6179.         p = asnambuf;
  6180.         debug(F110,"GET /COMMAND before stripping",p,0);
  6181.         p = brstrip(p);
  6182.         debug(F110,"GET /COMMAND after stripping",p,0);
  6183.         if (!*p) {
  6184.             printf("?Sorry, a command to write to is required\n");
  6185.             x = -9;
  6186.             goto xgetx;
  6187.         }
  6188.         pipename = p;
  6189.         pipesend = 1;
  6190.     }
  6191. #endif /* PIPESEND */
  6192.  
  6193. /* Set up /MOVE and /RENAME */
  6194.  
  6195.     if (pv[SND_DEL].ival > 0 &&
  6196.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  6197.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  6198.         x = -9;
  6199.         goto xgetx;
  6200.     }
  6201. #ifdef CK_TMPDIR
  6202.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  6203.         int len;
  6204.         char * p = pv[SND_MOV].sval;
  6205.         len = strlen(p);
  6206.         if (!isdir(p)) {                /* Check directory */
  6207. #ifdef CK_MKDIR
  6208.             char * s = NULL;
  6209.             s = (char *)malloc(len + 4);
  6210.             if (s) {
  6211.                 strcpy(s,p);            /* safe */
  6212. #ifdef datageneral
  6213.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  6214. #else
  6215.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  6216. #endif /* datageneral */
  6217.                 s[len++] = 'X';
  6218.                 s[len] = NUL;
  6219. #ifdef NOMKDIR
  6220.                 x = -1;
  6221. #else
  6222.                 x = zmkdir(s);
  6223. #endif /* NOMKDIR */
  6224.                 free(s);
  6225.                 if (x < 0) {
  6226.                     printf("?Can't create \"%s\"\n",p);
  6227.                     x = -9;
  6228.                     goto xgetx;
  6229.                 }
  6230.             }
  6231. #else
  6232.             printf("?Directory \"%s\" not found\n",p);
  6233.             x = -9;
  6234.             goto xgetx;
  6235. #endif /* CK_MKDIR */
  6236.         }
  6237.         makestr(&rcv_move,p);
  6238.         moving = 1;
  6239.     }
  6240. #endif /* CK_TMPDIR */
  6241.  
  6242.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  6243.         char * p = pv[SND_REN].sval;
  6244.         if (!p) p = "";
  6245.         if (!*p) {
  6246.             printf("?New name required for /RENAME\n");
  6247.             x = -9;
  6248.             goto xgetx;
  6249.         }
  6250.         p = brstrip(p);
  6251. #ifndef NOSPL
  6252.     /* If name given is wild, rename string must contain variables */
  6253.         if (mget && !getone) {
  6254.             char * s = tmpbuf;
  6255.             x = TMPBUFSIZ;
  6256.             zzstring(p,&s,&x);
  6257.             if (!strcmp(tmpbuf,p)) {
  6258.                 printf(
  6259.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  6260.                        );
  6261.                 x = -9;
  6262.                 goto xgetx;
  6263.             }
  6264.         }
  6265. #endif /* NOSPL */
  6266.         renaming = 1;
  6267.         makestr(&rcv_rename,p);
  6268.         debug(F110,"FTP rcv_rename",rcv_rename,0);
  6269.     }
  6270.     if (!cmarg[0] && mgetn == 0 && pv[SND_FIL].ival < 1) {
  6271.         printf("?Filename required but not given\n");
  6272.         x = -9;
  6273.         goto xgetx;
  6274.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  6275.         printf("?You can't give both /LISTFILE and a remote filename\n");
  6276.         x = -9;
  6277.         goto xgetx;
  6278.     }
  6279.     CHECKCONN();                        /* Check connection */
  6280.  
  6281. #ifndef NOSPL
  6282.     /* If as-name given for MGET, as-name must contain variables */
  6283.     if (mget && !getone && asnambuf[0]) {
  6284.         char * s = tmpbuf;
  6285.         x = TMPBUFSIZ;
  6286.         zzstring(asnambuf,&s,&x);
  6287.         if (!strcmp(tmpbuf,asnambuf)) {
  6288.             printf(
  6289.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  6290.                    );
  6291.             x = -9;
  6292.             goto xgetx;
  6293.         }
  6294.     }
  6295. #endif /* NOSPL */
  6296.  
  6297. /* doget: */
  6298.  
  6299.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  6300.         g_displa = fdispla;
  6301.         fdispla = 0;
  6302.     } else {
  6303.         displa = 1;
  6304.         if (mdel || ftp_deb) {
  6305.             g_displa = fdispla;
  6306.             fdispla = XYFD_B;
  6307.         }
  6308.     }
  6309.     deleting = 0;
  6310.  
  6311.     if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
  6312.       deleting = 1;
  6313.     if (pv[SND_EXC].ival > 0)
  6314.       makelist(pv[SND_EXC].sval,rcvexcept,8);
  6315.     if (pv[SND_SMA].ival > -1)
  6316.       getsmaller = pv[SND_SMA].ival;
  6317.     if (pv[SND_LAR].ival > -1)
  6318.       getlarger = pv[SND_LAR].ival;
  6319.     if (pv[SND_NAM].ival > -1)
  6320.       x_cnv = pv[SND_NAM].ival;
  6321.     if (pv[SND_ERR].ival > -1)
  6322.       geterror = pv[SND_ERR].ival;
  6323.     if (pv[SND_COL].ival > -1)
  6324.       x_fnc = pv[SND_COL].ival;
  6325.     if (pv[SND_MAI].ival > -1)
  6326.       toscreen = 1;
  6327.  
  6328. #ifdef FTP_RESTART
  6329.     if (pv[SND_RES].ival > 0) {
  6330.         if (!ftp_typ) {
  6331.             printf("?Sorry, GET /RECOVER requires binary mode\n");
  6332.             x = -9;
  6333.             goto xgetx;
  6334. #ifdef COMMENT
  6335.         /* Not true - the fact that the initial REST fails does not mean */
  6336.         /* it will fail here.  */
  6337.         } else if (!okrestart) {
  6338.             printf("WARNING: Server might not support restart...\n");
  6339. #endif /* COMMENT */
  6340.         }
  6341.         restart = 1;
  6342.     }
  6343. #endif /* FTP_RESTART */
  6344.  
  6345. #ifdef PIPESEND
  6346.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  6347.         if (pipesend) {
  6348.             printf("?Switch conflict: /FILTER and /COMMAND\n");
  6349.             x = -9;
  6350.             goto xgetx;
  6351.         }
  6352.         makestr(&rcvfilter,pv[SND_FLT].sval);
  6353.         debug(F110,"ftp get /FILTER", rcvfilter, 0);
  6354.     }
  6355.     if (rcvfilter || pipesend) {        /* /RESTART */
  6356. #ifdef FTP_RESTART
  6357.         if (restart) {                  /* with pipes or filters */
  6358.             printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  6359.             x = -9;
  6360.             goto xgetx;
  6361.         }
  6362. #endif /* FTP_RESTART */
  6363.         if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_U) {
  6364.             printf("?Switch conflict: /FILTER or /COMMAND and Update\n");
  6365.             x = -9;
  6366.             goto xgetx;
  6367.         }
  6368.     }
  6369. #endif /* PIPESEND */
  6370.  
  6371.     tfc = 0L;                           /* Initialize stats and counters */
  6372.     filcnt = 0;
  6373.     pktnum = 0;
  6374.     rpackets = 0L;
  6375.  
  6376.     if (pv[SND_FIL].ival > 0) {
  6377.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  6378.             debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  6379.             printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  6380.             x = -9;
  6381.             goto xgetx;
  6382.         }
  6383.         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  6384.             zclose(ZMFILE);                       /* Failed */
  6385.             debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  6386.             printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  6387.             x = -9;
  6388.             goto xgetx;
  6389.         }
  6390.         listfile = 1;
  6391.         debug(F110,"ftp get listfile first",tmpbuf,0);
  6392.         makestr(&(mgetlist[0]),tmpbuf);
  6393.     }
  6394.     t0 = gmstimer();                    /* Record starting time */
  6395.  
  6396.     updating = pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U);
  6397.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  6398.  
  6399.     cancelgroup = 0;                    /* Group not canceled yet */
  6400.     changetype(ftp_typ,0);              /* Change to requested type */
  6401.     binary = ftp_typ;                   /* For file-transfer display */
  6402.     first = 1;                          /* For MGET list */
  6403.     done = 0;                           /* Loop control */
  6404.  
  6405. #ifdef CK_TMPDIR
  6406.     if (dldir && !f_tmpdir) {           /* If they have a download directory */
  6407.         if ((s = zgtdir())) {           /* Get current directory */
  6408.             if (zchdir(dldir)) {        /* Change to download directory */
  6409.                 ckstrncpy(savdir,s,TMPDIRLEN);
  6410.                 f_tmpdir = 1;           /* Remember that we did this */
  6411.             }
  6412.         }
  6413.     }
  6414. #endif /* CK_TMPDIR */
  6415.  
  6416.     if (ftp_nml) {                      /* /NAMELIST */
  6417.         debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  6418.         if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  6419.           fp_nml = stdout;
  6420.         else
  6421.           fp_nml = fopen(ftp_nml, "wb");
  6422.         if (!fp_nml) {
  6423.             printf("?%s: %s\n",ftp_nml,ck_errstr());
  6424.             goto xgetx;
  6425.         }
  6426.     }
  6427.     while (!done && !cancelgroup) {     /* Loop for all files */
  6428.                                         /* or until canceled. */
  6429. #ifdef FTP_PROXY
  6430.         /* do something here if proxy */
  6431. #endif /* FTP_PROXY */
  6432.  
  6433.         rs_len = 0L;                    /* REGET position */
  6434.         cancelfile = 0;                 /* This file not canceled yet */
  6435.         haspath = 0;                    /* Recalculate this each time thru */
  6436.  
  6437.         if (getone) {                   /* GET */
  6438.             char * p;
  6439.             s = line;
  6440.             src = line;                 /* Server name */
  6441.             done = 1;
  6442.             debug(F111,"ftp get file",s,0);
  6443.         } else if (mget) {              /* MGET */
  6444.             src = mgetlist[mgetx];
  6445.             debug(F111,"ftp mget remote_files A",src,first);
  6446.             s = (char *)remote_files(first,(CHAR *)mgetlist[mgetx],0);
  6447.             if (!s) s = "";
  6448.             if (!*s) {
  6449.                 first = 1;
  6450.                 if (listfile) {
  6451.                   again:
  6452.                     tmpbuf[0] = NUL;
  6453.                     while (!tmpbuf[0]) {
  6454.                         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  6455.                             zclose(ZMFILE);
  6456.                             debug(F110,"ftp get listfile EOF",
  6457.                                   pv[SND_FIL].sval,0);
  6458.                             makestr(&(mgetlist[0]),NULL);
  6459.                             s = NULL;
  6460.                             done = 1;
  6461.                             break;
  6462.                         }
  6463.                     }
  6464.                     if (done)
  6465.                       continue;
  6466.  
  6467.                     makestr(&(mgetlist[0]),tmpbuf);
  6468.                     s = (char *)remote_files(first,(CHAR *)mgetlist[0],0);
  6469.                     if (!s) {
  6470.                         ftscreen(SCR_FN,'F',0L,s);
  6471.                         ftscreen(SCR_ST,ST_MSG,0L,"File not found");
  6472.                         tlog(F110,"ftp get file not found:",s,0);
  6473.                         goto again;
  6474.                     }
  6475.                 } else {
  6476.                     mgetx++;
  6477.                     if (mgetx < mgetn)
  6478.                      s = (char *)remote_files(first,(CHAR *)mgetlist[mgetx],0);
  6479.                     else
  6480.                      s = NULL;
  6481.                 }
  6482.                 debug(F111,"ftp mget remote_files B",s,0);
  6483.                 if (!s) {
  6484.                     done = 1;
  6485.                     break;
  6486.                 }
  6487.             }
  6488.         }
  6489.         /*
  6490.           The semantics of NLST are ill-defined.  Suppose we have just sent
  6491.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6492.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  6493.           RETR commands based on the pathless names are not going to work.
  6494.         */
  6495.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6496.             char * s3;
  6497.             if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  6498.                 int len, left = 4096;
  6499.                 char * tmp = xtmpbuf;
  6500.                 len = s3 - mgetlist[mgetx] + 1;
  6501.                 ckstrncpy(tmp,mgetlist[mgetx],left);
  6502.                 tmp += len;
  6503.                 left -= len;
  6504.                 ckstrncpy(tmp,s,left);
  6505.                 s = xtmpbuf;
  6506.             }
  6507.         }
  6508.         first = 0;
  6509.         skipthis = 0;                   /* File selection... */
  6510.         msg = "";
  6511.         nam = s;                        /* Filename (without path) */
  6512.         rc = 0;                         /* Initial return code */
  6513.         s2 = "";
  6514.  
  6515.         if (!getone && !skipthis) {     /* For MGET and MDELETE... */
  6516.             char c, * p = s;
  6517.             int srvpath = 0;
  6518.             int usrpath = 0;
  6519.             int i, k = 0;
  6520. /*
  6521.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  6522.   But if the client did not ask for a recursive list, we have to ignore any
  6523.   server files that include a pathname that extends beyond any path that
  6524.   was included in the user's request.
  6525.  
  6526.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  6527.   get the user's path segment.  Then, for each incoming file, if it begins
  6528.   with the same path segment, we must strip it (point past it).
  6529. */
  6530.             src = mgetlist[mgetx];      /* In case it moved! */
  6531.             for (i = 0; src[i]; i++) {  /* Find rightmost path separator */
  6532.                 if (ispathsep(src[i]))  /* in user's pathname */
  6533.                   k = i + 1;
  6534.             }
  6535.             usrpath = k;                /* User path segment length */
  6536.             debug(F111,"ftp get usrpath",src,usrpath);
  6537.  
  6538.             p = s;                      /* Server filename */
  6539.             while ((c = *p++)) {        /* Look for path in server filename */
  6540.                 if (ispathsep(c)) {
  6541.                     nam = p;            /* Pathless name (for ckmatch) */
  6542.                     srvpath = p - s;    /* Server path segment length */
  6543.                 }
  6544.             }
  6545.             debug(F111,"ftp get srvpath",s,srvpath);
  6546. /*
  6547.   If as-name not given and server filename includes path that matches
  6548.   the pathname from the user's file specification, we must trim the common
  6549.   path prefix from the server's name when constructing the local name.
  6550. */
  6551.             if (!asnambuf[0] && (srvpath > 0) && !strncmp(src,s,usrpath)) {
  6552.                 s2 = s + usrpath;       /* Local name skips past remote path */
  6553.                 if (srvpath > usrpath)  /* If there is still some path left */
  6554.                   haspath = 1;          /* in the server name, remember. */
  6555.             }
  6556.             debug(F111,"ftp get haspath",s+usrpath,haspath);
  6557.  
  6558.             if (haspath) {              /* Server file has path segments? */
  6559.                 if (!recursive) {       /* [M]GET /RECURSIVE? */
  6560. /*
  6561.   We did not ask for a recursive listing, but the server is sending us one
  6562.   anyway (as wu-ftpd is known to do).  We get here if the current filename
  6563.   includes a path segment beyond any path segment we asked for in our
  6564.   non-recursive [M]GET command.  We MUST skip this file.
  6565. */
  6566.                     debug(F111,"ftp get skipping because of path",s,0);
  6567. #ifdef COMMENT
  6568.                     skipthis++;
  6569. #else
  6570.                     continue;
  6571. #endif /* COMMENT */
  6572.                 }
  6573.             }
  6574.         } else if (getone && !skipthis) {
  6575.             char * p = nam;
  6576.             debug(F110,"XXX nam 1",nam,0);
  6577.             while ((c = *p++)) {        /* Strip path from local name */
  6578.                 if (ispathsep(c))
  6579.                   nam = p;
  6580.             }
  6581.             debug(F110,"XXX nam 2",nam,0);
  6582.             s2 = nam;
  6583.             debug(F110,"XXX s2",s2,0);
  6584.         }
  6585.         if (!*nam)                      /* Name without path */
  6586.           nam = s;
  6587.  
  6588.         if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  6589.             for (i = 0; i < 8; i++) {
  6590.                 if (!rcvexcept[i]) {
  6591.                     break;
  6592.                 }
  6593.                 if (ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1)) {
  6594.                     tlog(F100," refused: exception list","",0);
  6595.                     skipthis++;
  6596.                     break;
  6597.                 }
  6598.             }
  6599.         }
  6600.         if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  6601.             if (nam[0] == '.')
  6602.               skipthis++;
  6603.         }
  6604.         if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  6605.             if (ckmatch(
  6606. #ifdef CKREGEX
  6607.                         "*.~[0-9]*~"
  6608. #else
  6609.                         "*.~*~"
  6610. #endif /* CKREGEX */
  6611.                         ,nam,0,1) > 0)
  6612.               skipthis++;
  6613.         }
  6614.         if (!x_xla) {                   /* If translation is off */
  6615.             x_csl = -2;                 /* unset the charsets */
  6616.             x_csr = -2;
  6617.         }
  6618.  
  6619.         /* Initialize file size to -1 in case server doesn't understand */
  6620.         /* SIZE command, so xxscreen() will know we don't know the size */
  6621.  
  6622.         fsize = -1L;
  6623.  
  6624.         if (!mdel && !skipthis) {       /* Don't need size for DELE... */
  6625.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm); /* Remote file's size */
  6626.             if (x == REPLY_COMPLETE) {
  6627.                 fsize = atol(&ftp_reply_str[4]);
  6628.                 if (getsmaller > -1L && fsize >= getsmaller)
  6629.                   skipthis++;
  6630.                 if (getlarger > -1L && fsize <= getlarger)
  6631.                   skipthis++;
  6632.                 if (skipthis) {
  6633.                     debug(F111,"ftp get skip size",s,fsize);
  6634.                     tlog(F100," refused: size","",0);
  6635.                     msg = "Refused: size";
  6636.                 }
  6637. /* Here we should see if SIZE is an unknown command to the server and if */
  6638. /* so, set a flag to inhibit further SIZE queries. */
  6639.  
  6640. #ifdef COMMENT
  6641.             } else if (getone) {
  6642.                 /* SIZE can fail for many reasons.  Does the file exist? */
  6643.                 x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  6644.                 if (x != REPLY_COMPLETE) {
  6645.                     printf(">>> FILE NOT FOUND: %s\n",s);
  6646.                     break;
  6647.                 }
  6648. #endif /* COMMENT */
  6649.             }
  6650.         }
  6651.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6652.         if (!*s2)                       /* Local name */
  6653.           s2 = asnambuf;                /* As-name */
  6654.         debug(F110,"ftp get filnam ",s,0);
  6655.         debug(F110,"ftp get asnam A",s2,0);
  6656.  
  6657.         /* Receiving to real file */
  6658.         if (!pipesend &&
  6659. #ifdef PIPESEND
  6660.             !rcvfilter &&
  6661. #endif /* PIPESEND */
  6662.             !toscreen) {
  6663. #ifndef NOSPL
  6664.             /* Do this here so we can decide whether to skip */
  6665.             if (cmd_quoting && !skipthis && asnambuf[0]) {
  6666.                 int n; char *p;
  6667.                 n = TMPBUFSIZ;
  6668.                 p = tmpbuf;
  6669.                 zzstring(asnambuf,&p,&n);
  6670.                 s2 = tmpbuf;
  6671.                 debug(F111,"ftp get asname B",s2,updating);
  6672.             }
  6673. #endif /* NOSPL */
  6674. #ifdef DOUPDATE
  6675.             if (!skipthis && updating) { /* If updating and not skipping */
  6676.                 local = *s2 ? s2 : s;
  6677.                 if (zchki(local) > -1) {
  6678.                     x = chkmodtime(local,s,0);
  6679.                     debug(F111,"ftp get /update chkmodtime",local,x);
  6680.                     if (x == 2) {
  6681.                         skipthis++;
  6682.                         tlog(F100," refused: date","",0);
  6683.                         msg = "Refused: date";
  6684.                         debug(F110,"ftp get skip date",local,0);
  6685.                     }
  6686.                 }
  6687.             }
  6688. #endif /* DOUPDATE */
  6689.         }
  6690.         if (skipthis) {                 /* Skipping this file? */
  6691.             ftscreen(SCR_FN,'F',0L,s);
  6692.             if (msg)
  6693.               ftscreen(SCR_ST,ST_ERR,0L,msg);
  6694.             else
  6695.               ftscreen(SCR_ST,ST_SKIP,0L,s);
  6696.             continue;
  6697.         }
  6698.         if (fp_nml) {                   /* /NAMELIST only - no transfer */
  6699.             fprintf(fp_nml,"%s\n",s);
  6700.             continue;
  6701.         }
  6702.         if (recursive && haspath && !pipesend
  6703. #ifdef PIPESEND
  6704.             && !rcvfilter
  6705. #endif /* PIPESEND */
  6706.             ) {
  6707.             if (
  6708. #ifdef NOMKDIR
  6709.                 1
  6710. #else
  6711.                 zmkdir(s2) < 0          /* Try to make the directory */
  6712. #endif /* NOMKDIR */
  6713.                 ) {
  6714.                 rc = -1;                /* Failure is fatal */
  6715.                 if (geterror) {
  6716.                     status = 0;
  6717.                     ftscreen(SCR_EM,0,0L,"Directory creation failure");
  6718.                     break;
  6719.                 }
  6720.             }
  6721.         }
  6722.  
  6723.         /* Not skipping */
  6724.  
  6725.         pn = NULL;
  6726.         if (mdel) {                     /* [M]DELETE */
  6727.             if (displa && !ftp_vbm)
  6728.               printf(" %s...",s);
  6729.             rc =
  6730.              (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  6731.             if (rc > -1) {
  6732.                 tlog(F110,"ftp mdelete",s,0);
  6733.                 if (displa && !ftp_vbm)
  6734.                   printf("OK\n");
  6735.             } else {
  6736.                 tlog(F110,"ftp mdelete failed:",s,0);
  6737.                 if (displa)
  6738.                   printf("Failed\n");
  6739.             }
  6740. #ifndef NOSPL
  6741. #ifdef PIPESEND
  6742.         } else if (rcvfilter) {         /* [M]GET with filter */
  6743.             int n; char * p;
  6744.             n = CKMAXPATH;
  6745.             p = tmpbuf;                 /* Safe - no asname with filter */
  6746.             zzstring(rcvfilter,&p,&n);
  6747.             if (n > -1)
  6748.               pn = tmpbuf;
  6749.             debug(F111,"ftp get rcvfilter",pn,n);
  6750. #endif /* PIPESEND */
  6751. #endif /* NOSPL */
  6752.             if (toscreen) s2 = "-";
  6753.         } else if (pipesend) {          /* [M]GET /COMMAND */
  6754.             int n; char * p;
  6755.             n = CKMAXPATH;
  6756.             p = tmpbuf;                 /* Safe - no asname with filter */
  6757.             zzstring(pipename,&p,&n);
  6758.             if (n > -1)
  6759.               pn = tmpbuf;
  6760.             debug(F111,"ftp get pipename",pipename,n);
  6761.             if (toscreen) s2 = "-";
  6762.         } else {                        /* [M]GET with no pipes or filters */
  6763.             debug(F111,"ftp get s2 A",s2,x_cnv);
  6764.             if (toscreen) {
  6765.                 s2 = "-";               /* (hokey convention for stdout) */
  6766.             } else if (!*s2) {          /* No asname? */
  6767.                 if (x_cnv) {            /* If converting */
  6768.                     int nc = x_cnv;
  6769.                     if (x_cnv == SET_AUTO) {
  6770.                         if (alike)
  6771.                           nc = 0;
  6772.                     } else {
  6773.                         if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6774.                           nc = -1;      /* only minimal conversions needed */
  6775.                         else            /* otherwise */
  6776.                           nc = 1;       /* full conversion */
  6777.                     }
  6778.                     nzrtol(s,tmpbuf,nc,1,CKMAXPATH); /* convert */
  6779.                     s2 = tmpbuf;
  6780.                     debug(F110,"ftp get nzrtol",s2,0);
  6781.                 } else                  /* otherwise */
  6782.                   s2 = s;               /* use incoming file's name */
  6783.             }
  6784.             debug(F110,"ftp get s2 B",s2,0); /* ^^^ */
  6785.  
  6786.             /* If local file already exists, take collision action */
  6787.  
  6788.             if (!pipesend &&
  6789. #ifdef PIPESEND
  6790.                 !rcvfilter &&
  6791. #endif /* PIPESEND */
  6792.                 !toscreen) {
  6793.                 x = zchki(s2);
  6794.                 debug(F111,"ftp get zchki",s2,x);
  6795.                 debug(F111,"ftp get x_fnc",s2,x_fnc);
  6796.  
  6797.                 if (x > -1 && !restart) {
  6798.                     switch (x_fnc) {
  6799.                       case XYFX_A:      /* Append */
  6800.                         append = 1;
  6801.                         break;
  6802.                       case XYFX_R:      /* Rename */
  6803.                       case XYFX_B: {    /* Backup */
  6804.                           char * p = NULL;
  6805.                           int x = -1;
  6806.                           znewn(s2,&p); /* Make unique name */
  6807.                           debug(F110,"ftp get znewn",p,0);
  6808.                           if (x_fnc == XYFX_B) { /* Backup existing file */
  6809.                               x = zrename(s2,p);
  6810.                               debug(F111,"ftp get backup zrename",p,x);
  6811.                           } else {      /* Rename incoming file */
  6812.                               x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6813.                               s2 = tmpbuf;
  6814.                               debug(F111,"ftp get rename incoming",p,x);
  6815.                           }
  6816.                           if (x < 0) {
  6817.                               ftscreen(SCR_EM,0,0L,"Backup/Rename failed");
  6818.                               x = 0;
  6819.                               goto xgetx;
  6820.                           }
  6821.                           break;
  6822.                       }
  6823.                       case XYFX_D:      /* Discard */
  6824.                         ftscreen(SCR_FN,'F',0L,s);
  6825.                         ftscreen(SCR_ST,ST_SKIP,SKP_NAM,s);
  6826.                         tlog(F100," refused: name","",0);
  6827.                         debug(F110,"ftp get skip name",*s2 ? s2 : s,0);
  6828.                         continue;
  6829.                       case XYFX_X:      /* Overwrite */
  6830.                       case XYFX_U:      /* Update (already handled above) */
  6831.                         break;
  6832.                     }
  6833.                 }
  6834.             }
  6835.         }
  6836.         if (!mdel) {
  6837. #ifdef PIPESEND
  6838.             debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  6839. #endif /* PIPESEND */
  6840.             if (pipesend && !toscreen)
  6841.               s2 = NULL;
  6842. #ifdef DEBUG
  6843.             if (deblog) {
  6844.                 debug(F101,"ftp get x_xla","",x_xla);
  6845.                 debug(F101,"ftp get x_csl","",x_csl);
  6846.                 debug(F101,"ftp get x_csr","",x_csr);
  6847.             }
  6848. #endif /* DEBUG */
  6849.  
  6850.             rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  6851.  
  6852. #ifdef DEBUG
  6853.             if (deblog) {
  6854.                 debug(F111,"ftp get rc",s,rc);
  6855.                 debug(F111,"ftp get cancelfile",s,cancelfile);
  6856.                 debug(F111,"ftp get cancelgroup",s,cancelgroup);
  6857.                 debug(F111,"ftp get renaming",s,renaming);
  6858.             }
  6859. #endif /* DEBUG */
  6860.         }
  6861.         if (rc > -1) {
  6862.             good++;
  6863.             status = 1;
  6864.             if (!cancelfile) {
  6865.                 if (deleting) {         /* GET /DELETE (source file) */
  6866.                     rc =
  6867.                       (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  6868.                         ? 1 : -1;
  6869.                     tlog(F110, (rc > -1) ?
  6870.                          " deleted" : " failed to delete", s, 0);
  6871.                 } else if (renaming && rcv_rename && !toscreen) {
  6872.                     char *p;            /* Rename downloaded file */
  6873. #ifndef NOSPL
  6874.                     char tmpbuf[CKMAXPATH+1];
  6875.                     int n;
  6876.                     n = CKMAXPATH;
  6877.                     p = tmpbuf;
  6878.                     debug(F111,"ftp get /rename",rcv_rename,0);
  6879.                     zzstring(rcv_rename,&p,&n);
  6880.                     debug(F111,"ftp get /rename",rcv_rename,0);
  6881.                     p = tmpbuf;
  6882. #else
  6883.                     p = rcv_rename;
  6884. #endif /* NOSPL */
  6885.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  6886.                     debug(F111,"doftpget /RENAME zrename",p,rc);
  6887.                     tlog(F110, (rc > -1) ?
  6888.                          " renamed to" :
  6889.                          " failed to rename to",
  6890.                          p,
  6891.                          0
  6892.                          );
  6893.                 } else if (moving && rcv_move && !toscreen) {
  6894.                     char *p;            /* Move downloaded file */
  6895. #ifndef NOSPL
  6896.                     char tmpbuf[CKMAXPATH+1];
  6897.                     int n;
  6898.                     n = TMPBUFSIZ;
  6899.                     p = tmpbuf;
  6900.                     debug(F111,"ftp get /move-to",rcv_move,0);
  6901.                     zzstring(rcv_move,&p,&n);
  6902.                     p = tmpbuf;
  6903. #else
  6904.                     p = rcv_move;
  6905. #endif /* NOSPL */
  6906.                     debug(F111,"ftp get /move-to",p,0);
  6907.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  6908.                     debug(F111,"doftpget /MOVE zrename",p,rc);
  6909.                     tlog(F110, (rc > -1) ?
  6910.                          " moved to" : " failed to move to", p, 0);
  6911.                 }
  6912.                 if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  6913.                     char * s = pv[SND_SRN].sval;
  6914.                     char * srvrn = pv[SND_SRN].sval;
  6915.                     char tmpbuf[CKMAXPATH+1];
  6916. #ifndef NOSPL
  6917.                     int y;              /* Pass it thru the evaluator */
  6918.                     extern int cmd_quoting; /* for \v(filename) */
  6919.                     debug(F111,"ftp get srv_renam",s,1);
  6920.  
  6921.                     if (cmd_quoting) {
  6922.                         y = CKMAXPATH;
  6923.                         s = (char *)tmpbuf;
  6924.                         zzstring(srvrn,&s,&y);
  6925.                         s = (char *)tmpbuf;
  6926.                     }
  6927. #endif /* NOSPL */
  6928.                     debug(F111,"ftp get srv_renam",s,1);
  6929.                     if (s) if (*s) {
  6930.                         int x;
  6931.                         x = ftp_rename(s2,s);
  6932.                         debug(F111,"ftp get ftp_rename",s2,x);
  6933.                         tlog(F110, (x > 0) ?
  6934.                              " renamed source file to" :
  6935.                              " failed to rename source file to",
  6936.                              s,
  6937.                              0
  6938.                              );
  6939.                         if (x < 1)
  6940.                           return(-1);
  6941.                     }
  6942.                 }
  6943.             }
  6944.         }
  6945.         if (cancelfile)
  6946.           continue;
  6947.         if (rc < 0) {
  6948.             ftp_fai++;
  6949.             if (geterror) {
  6950.                 status = 0;
  6951.                 ftscreen(SCR_EM,0,0L,"Fatal download error");
  6952.                 done++;
  6953.             }
  6954.         }
  6955.     }
  6956.     if (status > 0) {
  6957.         if (cancelgroup)
  6958.           status = 0;
  6959.         else if (cancelfile && good < 1)
  6960.           status = 0;
  6961.     }
  6962.     success = status;
  6963.     x = success;
  6964.  
  6965.   xgetx:
  6966.     pipesend = pipesave;                /* Restore global pipe selection */
  6967.     if (fp_nml) {                       /* Close /NAMELIST */
  6968.         if (fp_nml != stdout)
  6969.           fclose(fp_nml);
  6970.         fp_nml = NULL;
  6971.     }
  6972.     if (x > -1) {                       /* Download successful */
  6973. #ifdef GFTIMER
  6974.         t1 = gmstimer();                /* End time */
  6975.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6976.         if (!sec) sec = 0.001;
  6977.         fptsecs = sec;
  6978. #else
  6979.         sec = (t1 - t0) / 1000;
  6980.         if (!sec) sec = 1;
  6981. #endif /* GFTIMER */
  6982.         tfcps = (long) (tfc / sec);
  6983.         tsecs = (int)sec;
  6984.         lastxfer = W_FTP|W_RECV;
  6985.         xferstat = success;
  6986.     }
  6987.     if (dpyactive)
  6988.       ftscreen(SCR_TC,0,0L,"");
  6989. #ifdef CK_TMPDIR
  6990.     if (f_tmpdir) {                     /* If we changed to download dir */
  6991.         zchdir((char *) savdir);        /* Go back where we came from */
  6992.         f_tmpdir = 0;
  6993.     }
  6994. #endif /* CK_TMPDIR */
  6995.  
  6996.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  6997.         if (pv[i].sval)
  6998.           free(pv[i].sval);
  6999.     }
  7000.     for (i = 0; i < mgetn; i++)         /* MGET list too */
  7001.       makestr(&(mgetlist[i]),NULL);
  7002.  
  7003.     ftreset();                          /* Undo switch effects */
  7004.     dpyactive = 0;
  7005.     return(x);
  7006. }
  7007.  
  7008. static struct keytab ftprmt[] = {
  7009.     { "cd",        XZCWD, 0 },
  7010.     { "cdup",      XZCDU, 0 },
  7011.     { "cwd",       XZCWD, CM_INV },
  7012.     { "delete",    XZDEL, 0 },
  7013.     { "directory", XZDIR, 0 },
  7014.     { "exit",      XZXIT, 0 },
  7015.     { "help",      XZHLP, 0 },
  7016.     { "login",     XZLGI, 0 },
  7017.     { "logout",    XZLGO, 0 },
  7018.     { "mkdir",     XZMKD, 0 },
  7019.     { "pwd",       XZPWD, 0 },
  7020.     { "rename",    XZREN, 0 },
  7021.     { "rmdir",     XZRMD, 0 },
  7022.     { "type",      XZTYP, 0 },
  7023.     { "", 0, 0 }
  7024. };
  7025. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  7026.  
  7027. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  7028.  
  7029. int
  7030. doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  7031.     /* cx == 0 means REMOTE */
  7032.     /* cx != 0 is a XZxxx value */
  7033.     char * s;
  7034.  
  7035.     if (who != 0)
  7036.       return(0);
  7037.  
  7038.     if (cx == 0) {
  7039.         if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  7040.           return(x);
  7041.         cx = x;
  7042.     }
  7043.     switch (cx) {
  7044.       case XZCDU:                       /* CDUP */
  7045.         if ((x = cmcfm()) < 0) return(x);
  7046.         return(doftpcdup());
  7047.  
  7048.       case XZCWD:                       /* RCD */
  7049.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7050.           return(x);
  7051.         ckstrncpy(line,s,LINBUFSIZ);
  7052.         return(doftpcwd((char *)line,1));
  7053.       case XZPWD:                       /* RPWD */
  7054.         return(doftppwd());
  7055.       case XZDEL:                       /* RDEL */
  7056.         return(doftpget(FTP_MDE,1));
  7057.       case XZDIR:                       /* RDIR */
  7058.         return(doftpdir(FTP_DIR));
  7059.       case XZHLP:                       /* RHELP */
  7060.         return(doftpxhlp());
  7061.       case XZMKD:                       /* RMKDIR */
  7062.         return(doftpmkd());
  7063.       case XZREN:                       /* RRENAME */
  7064.         return(doftpren());
  7065.       case XZRMD:                       /* RRMDIR */
  7066.         return(doftprmd());
  7067.       case XZLGO:                       /* LOGOUT */
  7068.         return(doftpres());
  7069.       case XZXIT:                       /* EXIT */
  7070.         return(ftpbye());
  7071.     }
  7072.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  7073.     return(-9);
  7074. }
  7075.  
  7076. int
  7077. doxftp() {                              /* Command parser for built-in FTP */
  7078.     int cx, n;
  7079.     struct FDB kw, fl;
  7080.     char * s;
  7081.     int usetls = 0;
  7082.  
  7083.     int lcs = -1, rcs = -1;
  7084. #ifndef NOCSETS
  7085.     if (ftp_xla) {
  7086.         lcs = ftp_csl;
  7087.         if (lcs < 0) lcs = fcharset;
  7088.         rcs = ftp_csx;
  7089.         if (rcs < 0) rcs = ftp_csr;
  7090.     }
  7091. #endif /* NOCSETS */
  7092.  
  7093.     if (inserver)                       /* FTP not allowed in IKSD. */
  7094.       return(-2);
  7095.  
  7096.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  7097.         ftp_typ = g_ftp_typ;
  7098.         /* g_ftp_typ = -1; */
  7099.     }
  7100. #ifdef COMMENT
  7101. /*
  7102.   We'll set the collision action locally in doftpget() based on whether
  7103.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  7104. */
  7105.     if (ftp_fnc < 0)                    /* Inherit global collision action */
  7106.       ftp_fnc = fncact;                 /* if none specified for FTP */
  7107. #endif /* COMMENT */
  7108.  
  7109.     /* Restore global verbose mode */
  7110.     if (ftp_deb)
  7111.       ftp_vbm = 1;
  7112.     else if (quiet)
  7113.       ftp_vbm = 0;
  7114.     else
  7115.       ftp_vbm = ftp_vbx;
  7116.  
  7117.     dpyactive = 0;                      /* Reset global transfer-active flag */
  7118.     printlines = 0;                     /* Reset printlines */
  7119.  
  7120.     if (fp_nml) {                       /* Reset /NAMELIST */
  7121.         if (fp_nml != stdout)
  7122.           fclose(fp_nml);
  7123.         fp_nml = NULL;
  7124.     }
  7125.     makestr(&ftp_nml,NULL);
  7126.  
  7127.     cmfdbi(&kw,                         /* First FDB - commands */
  7128.            _CMKEY,                      /* fcode */
  7129.            "Hostname; or FTP command",  /* help */
  7130.            "",                          /* default */
  7131.            "",                          /* addtl string data */
  7132.            nftpcmd,                     /* addtl numeric data 1: tbl size */
  7133.            0,                           /* addtl numeric data 2: none */
  7134.            xxstring,                    /* Processing function */
  7135.            ftpcmdtab,                   /* Keyword table */
  7136.            &fl                          /* Pointer to next FDB */
  7137.            );
  7138.     cmfdbi(&fl,                         /* A host name or address */
  7139.            _CMFLD,                      /* fcode */
  7140.            "Hostname or address",       /* help */
  7141.            "",                          /* default */
  7142.            "",                          /* addtl string data */
  7143.            0,                           /* addtl numeric data 1 */
  7144.            0,                           /* addtl numeric data 2 */
  7145.            xxstring,
  7146.            NULL,
  7147.            NULL
  7148.            );
  7149.     x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
  7150.     if (x == -3) {
  7151.         printf("?ftp what? \"help ftp\" for hints\n");
  7152.         return(-9);
  7153.     }
  7154.     if (x < 0)
  7155.       return(x);
  7156.     if (cmresult.fcode == _CMFLD) {     /* If hostname */
  7157.         return(openftp(cmresult.sresult,0)); /* go open the connection */
  7158.     } else {
  7159.         cx = cmresult.nresult;
  7160.     }
  7161.     switch (cx) {
  7162.       case FTP_ACC:                     /* ACCOUNT */
  7163.         if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  7164.           return(x);
  7165.         CHECKCONN();
  7166.         makestr(&ftp_acc,s);
  7167.         if (testing)
  7168.           printf(" ftp account: \"%s\"\n",ftp_acc);
  7169.         success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  7170.         return(success);
  7171.  
  7172.       case FTP_GUP:                     /* Go UP */
  7173.         if ((x = cmcfm()) < 0) return(x);
  7174.         CHECKCONN();
  7175.         if (testing) printf(" ftp cd: \"(up)\"\n");
  7176.         return(success = doftpcdup());
  7177.  
  7178.       case FTP_CWD:                     /* CD */
  7179.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7180.           return(x);
  7181.         CHECKCONN();
  7182.         ckstrncpy(line,s,LINBUFSIZ);
  7183.         if (testing)
  7184.           printf(" ftp cd: \"%s\"\n", line);
  7185.         return(success = doftpcwd(line,1));
  7186.  
  7187.       case FTP_CHM:                     /* CHMOD */
  7188.         if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  7189.           return(x);
  7190.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7191.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7192.           return(x);
  7193.         CHECKCONN();
  7194.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  7195.         if (testing)
  7196.           printf(" ftp chmod: %s\n",ftpcmdbuf);
  7197.         success =
  7198.           (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7199.         return(success);
  7200.  
  7201.       case FTP_CLS:                     /* CLOSE FTP connection */
  7202.         if ((y = cmcfm()) < 0)
  7203.           return(y);
  7204.         CHECKCONN();
  7205.         if (testing)
  7206.           printf(" ftp closing...\n");
  7207.         ftpclose();
  7208.         return(success = 1);
  7209.  
  7210.       case FTP_DIR:                     /* DIRECTORY of remote files */
  7211.       case FTP_VDI:
  7212.         return(doftpdir(cx));
  7213.  
  7214.       case FTP_GET:                     /* GET a remote file */
  7215.       case FTP_RGE:                     /* REGET */
  7216.       case FTP_MGE:                     /* MGET */
  7217.       case FTP_MDE:                     /* MDELETE */
  7218.         return(doftpget(cx,1));
  7219.  
  7220.       case FTP_IDL:                     /* IDLE */
  7221.         if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  7222.           return(x);
  7223.         if ((y = cmcfm()) < 0)
  7224.           return(y);
  7225.         CHECKCONN();
  7226.         if (z < 0)  {                   /* Display idle timeout */
  7227.             if (testing)
  7228.               printf(" ftp query idle timeout...\n");
  7229.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  7230.         } else {                        /* Set idle timeout */
  7231.             if (testing)
  7232.               printf(" ftp idle timeout set: %d...\n",z);
  7233.             success =
  7234.               (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  7235.         }
  7236.         return(success);
  7237.  
  7238.       case FTP_MKD:                     /* MKDIR */
  7239.         return(doftpmkd());
  7240.  
  7241.       case FTP_MOD:                     /* MODTIME */
  7242.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7243.           return(x);
  7244.         CHECKCONN();
  7245.         ckstrncpy(line,s,LINBUFSIZ);
  7246.         if (testing)
  7247.           printf(" ftp modtime \"%s\"...\n",line);
  7248.         success = 0;
  7249.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  7250.             int flag = 0;
  7251.             char c, * s;
  7252.             struct tm tmremote;
  7253.  
  7254.             bzero((char *)&tmremote, sizeof(struct tm));
  7255.             s = ftp_reply_str;
  7256.             while ((c = *s++)) {
  7257.                 if (c == SP) {
  7258.                     flag++;
  7259.                     break;
  7260.                 }
  7261.             }
  7262.             if (flag) {
  7263.                 if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  7264.                            &tmremote.tm_year,
  7265.                            &tmremote.tm_mon,
  7266.                            &tmremote.tm_mday,
  7267.                            &tmremote.tm_hour,
  7268.                            &tmremote.tm_min,
  7269.                            &tmremote.tm_sec
  7270.                            ) == 6) {
  7271.                     success = 1;
  7272.                     printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  7273.                            line,
  7274.                            tmremote.tm_year,
  7275.                            tmremote.tm_mon,
  7276.                            tmremote.tm_mday,
  7277.                            tmremote.tm_hour,
  7278.                            tmremote.tm_min,
  7279.                            tmremote.tm_sec
  7280.                            );
  7281.                 }
  7282.             }
  7283.         }
  7284.         return(success);
  7285.  
  7286.       case FTP_OPN:                     /* OPEN connection */
  7287. #ifdef COMMENT
  7288.         x = cmfld("IP hostname or address","",&s,xxstring);
  7289.         if (x < 0) {
  7290.             success = 0;
  7291.             return(x);
  7292.         }
  7293.         ckstrncpy(line,s,LINBUFSIZ);
  7294.         s = line;
  7295.         return(openftp(s,0));
  7296. #else
  7297.         {                               /* OPEN connection */
  7298.             char name[TTNAMLEN+1], *p;
  7299.             extern int network;
  7300.             extern char ttname[];
  7301.             if (network)                /* If we have a current connection */
  7302.               ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  7303.             else
  7304.               *name = '\0';             /* as default host */
  7305.             for (p = name; *p; p++)     /* Remove ":service" from end. */
  7306.               if (*p == ':') { *p = '\0'; break; }
  7307. #ifndef USETLSTAB
  7308.             x = cmfld("IP hostname or address",name,&s,xxstring);
  7309. #else
  7310.             cmfdbi(&kw,                 /* First FDB - commands */
  7311.                    _CMKEY,              /* fcode */
  7312.                    "Hostname or switch", /* help */
  7313.                    "",                  /* default */
  7314.                    "",                  /* addtl string data */
  7315.                    ntlstab,             /* addtl numeric data 1: tbl size */
  7316.                    0,                   /* addtl numeric data 2: none */
  7317.                    xxstring,            /* Processing function */
  7318.                    tlstab,              /* Keyword table */
  7319.                    &fl                  /* Pointer to next FDB */
  7320.                    );
  7321.             cmfdbi(&fl,                 /* A host name or address */
  7322.                    _CMFLD,              /* fcode */
  7323.                    "Hostname or address", /* help */
  7324.                    "",                  /* default */
  7325.                    "",                  /* addtl string data */
  7326.                    0,                   /* addtl numeric data 1 */
  7327.                    0,                   /* addtl numeric data 2 */
  7328.                    xxstring,
  7329.                    NULL,
  7330.                    NULL
  7331.                    );
  7332.  
  7333.             for (n = 0;; n++) {
  7334.                 x = cmfdb(&kw);         /* Parse a hostname or a keyword */
  7335.                 if (x == -3) {
  7336.                   printf("?ftp open what? \"help ftp\" for hints\n");
  7337.                   return(-9);
  7338.                 }
  7339.                 if (x < 0)
  7340.                   break;
  7341.                 if (cmresult.fcode == _CMFLD) { /* Hostname */
  7342.                     s = cmresult.sresult;
  7343.                     break;
  7344.                 } else if (cmresult.nresult == OPN_TLS) {
  7345.                     usetls = 1;
  7346.                 }
  7347.             }
  7348. #endif /* USETLSTAB */
  7349.             if (x < 0) {
  7350.                 success = 0;
  7351.                 return(x);
  7352.             }
  7353.             ckstrncpy(line,s,LINBUFSIZ);
  7354.             s = line;
  7355.             return(openftp(s,usetls));
  7356.         }
  7357. #endif /* COMMENT */
  7358.  
  7359.       case FTP_PUT:                     /* PUT */
  7360.       case FTP_MPU:                     /* MPUT */
  7361.       case FTP_APP:                     /* APPEND */
  7362.         return(doftpput(cx,1));
  7363.  
  7364.       case FTP_PWD:                     /* PWD */
  7365.         x = doftppwd();
  7366.         if (x > -1) success = x;
  7367.         return(x);
  7368.  
  7369.       case FTP_REN:                     /* RENAME */
  7370.         return(doftpren());
  7371.  
  7372.       case FTP_RES:                     /* RESET */
  7373.         return(doftpres());
  7374.  
  7375.       case FTP_HLP:                     /* (remote) HELP */
  7376.         return(doftpxhlp());
  7377.  
  7378.       case FTP_RMD:                     /* RMDIR */
  7379.         return(doftprmd());
  7380.  
  7381.       case FTP_STA:                     /* STATUS */
  7382.         if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7383.           return(x);
  7384.         CHECKCONN();
  7385.         ckstrncpy(line,s,LINBUFSIZ);
  7386.         if (testing) printf(" ftp status \"%s\"...\n",line);
  7387.         success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  7388.         return(success);
  7389.  
  7390.       case FTP_SIT: {                   /* SITE */
  7391.           int reply;
  7392.           if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7393.             return(x);
  7394.           CHECKCONN();
  7395.           ckstrncpy(line,s,LINBUFSIZ);
  7396.           if (testing) printf(" ftp site \"%s\"...\n",line);
  7397.           if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  7398.               do {
  7399.                   reply = getreply(0,lcs,rcs,ftp_vbm,0);
  7400.               } while (reply == REPLY_PRELIM);
  7401.           }
  7402.           return(success = (reply == REPLY_COMPLETE));
  7403.       }
  7404.       case FTP_SIZ:                     /* (ask for) SIZE */
  7405.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  7406.           return(x);
  7407.         CHECKCONN();
  7408.         ckstrncpy(line,s,LINBUFSIZ);
  7409.         if (testing)
  7410.           printf(" ftp size \"%s\"...\n",line);
  7411.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  7412.         return(success);
  7413.  
  7414.       case FTP_SYS:                     /* Ask for server's SYSTEM type */
  7415.         if ((x = cmcfm()) < 0) return(x);
  7416.         CHECKCONN();
  7417.         if (testing)
  7418.           printf(" ftp system...\n");
  7419.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  7420.         return(success);
  7421.  
  7422.       case FTP_UMA:                     /* Set/query UMASK */
  7423.         if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  7424.           if (x != -3)
  7425.             return(x);
  7426.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  7427.         if ((x = cmcfm()) < 0) return(x);
  7428.         CHECKCONN();
  7429.         if (testing) {
  7430.             if (tmpbuf[0])
  7431.               printf(" ftp umask \"%s\"...\n",tmpbuf);
  7432.             else
  7433.               printf(" ftp query umask...\n");
  7434.         }
  7435.         success = ftp_umask(tmpbuf);
  7436.         return(success);
  7437.  
  7438.       case FTP_USR:
  7439.         return(doftpusr());
  7440.  
  7441.       case FTP_QUO:
  7442.         if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  7443.           return(x);
  7444.         CHECKCONN();
  7445.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  7446.         return(success);
  7447.  
  7448.       case FTP_TYP:                     /* Type */
  7449.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  7450.           return(x);
  7451.         if ((y = cmcfm()) < 0) return(y);
  7452.         CHECKCONN();
  7453.         ftp_typ = x;
  7454.         g_ftp_typ = x;
  7455.         tenex = (ftp_typ == FTT_TEN);
  7456.         changetype(ftp_typ,ftp_vbm);
  7457.         return(1);
  7458.  
  7459.       case FTP_CHK:                     /* Check if remote file(s) exist(s) */
  7460.         if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  7461.           return(x);
  7462.         CHECKCONN();
  7463.         success = remote_files(1,(CHAR *)s,0) ? 1 : 0;
  7464.         return(success);
  7465.  
  7466.       /* If there is a useful reply we should parse it & remember results */
  7467.       case FTP_FEA:                     /* RFC2389 */
  7468.         if ((y = cmcfm()) < 0)
  7469.           return(y);
  7470.         CHECKCONN();
  7471.         return(success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE));
  7472.  
  7473.       case FTP_OPT:                     /* RFC2389 */
  7474.         /* Perhaps this should be a keyword list... */
  7475.         if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  7476.           return(x);
  7477.         CHECKCONN();
  7478.         ckstrncpy(line,s,LINBUFSIZ);
  7479.         if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  7480.           return(x);
  7481.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  7482.         return(success);
  7483.     }
  7484.     return(-2);
  7485. }
  7486.  
  7487. #ifndef NOSHOW
  7488. static char *
  7489. shopl(x) int x; {
  7490.     switch (x) {
  7491.       case FPL_CLR: return("clear");
  7492.       case FPL_PRV: return("private");
  7493.       case FPL_SAF: return("safe");
  7494.       case 0:  return("(not set)");
  7495.       default: return("(unknown)");
  7496.     }
  7497. }
  7498.  
  7499. int
  7500. shoftp(brief) {
  7501.     char * s = "?";
  7502.     int n;
  7503.  
  7504.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  7505.         ftp_typ = g_ftp_typ;
  7506.         /* g_ftp_typ = -1; */
  7507.     }
  7508.     printf("\n");
  7509.     printf("FTP connection:                 %s\n",connected ?
  7510.            ftp_host :
  7511.            "(none)"
  7512.            );
  7513.     n = 2;
  7514.     if (connected) {
  7515.         n++;
  7516.         printf("FTP server type:                %s\n",
  7517.                ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  7518.     }
  7519.     if (loggedin)
  7520.       printf("Logged in as:                   %s\n",
  7521.              strval(ftp_logname,"(unknown)"));
  7522.     else
  7523.       printf("Not logged in\n");
  7524.     n++;
  7525.     if (brief) return(0);
  7526.  
  7527.     printf("\nSET FTP values:\n\n");
  7528.     n += 3;
  7529.  
  7530.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  7531.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  7532.     switch (ftp_typ) {
  7533.       case FTT_ASC: s = "text"; break;
  7534.       case FTT_BIN: s = "binary"; break;
  7535.       case FTT_TEN: s = "tenex"; break;
  7536.     }
  7537.     printf(" ftp type:                      %s\n",s);
  7538.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  7539.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  7540.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  7541.     printf(" ftp filenames:                 %s\n",
  7542.            ftp_cnv == 2 ? "auto" : (ftp_cnv ? "converted" : "literal")
  7543.            );
  7544.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  7545.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  7546.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  7547.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  7548.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  7549.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  7550. #ifdef COMMENT
  7551.     /* See note in doxftp() */
  7552.     if (ftp_fnc < 0)
  7553.       ftp_fnc = fncact;
  7554. #endif /* COMMENT */
  7555.     printf(" ftp collision:                 %s\n",
  7556.             fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  7557.     n += 14;
  7558.  
  7559. #ifndef NOCSETS
  7560.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  7561.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7562.  
  7563.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  7564.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7565.  
  7566.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  7567.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7568. #endif /* NOCSETS */
  7569.  
  7570.     switch (fdispla) {
  7571.       case XYFD_N: s = "none"; break;
  7572.       case XYFD_R: s = "serial"; break;
  7573.       case XYFD_C: s = "fullscreen"; break;
  7574.       case XYFD_S: s = "crt"; break;
  7575.       case XYFD_B: s = "brief"; break;
  7576.     }
  7577.     printf(" transfer display:              %s\n",s);
  7578.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7579.     switch (ftpget) {
  7580.       case 0: s = "kermit"; break;
  7581.       case 1: s = "ftp"; break;
  7582.       case 2: s = "auto"; break;
  7583.       default: s = "?";
  7584.     }
  7585.     printf(" get-put-remote:                %s\n",s);
  7586.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7587.  
  7588.     printf("\n");
  7589.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7590.  
  7591. #ifdef FTP_SECURITY
  7592.     printf("Available security methods:    ");
  7593. #ifdef FTP_GSSAPI
  7594.     printf("GSSAPI ");
  7595. #endif /* FTP_GSSAPI */
  7596. #ifdef FTP_KRB4
  7597.     printf("Kerberos4 ");
  7598. #endif /* FTP_KRB4 */
  7599. #ifdef FTP_SRP
  7600.     printf("SRP ");
  7601. #endif /* FTP_SRP */
  7602. #ifdef FTP_SSL
  7603.     printf("SSL ");
  7604. #endif /* FTP_SSL */
  7605.  
  7606.     n++;
  7607.     printf("\n\n");
  7608.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7609.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  7610.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7611.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  7612.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7613.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  7614.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7615.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  7616.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7617.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  7618.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7619.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  7620.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7621. #else
  7622.     printf("Available security methods:     (none)\n");
  7623.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  7624. #endif /* FTP_SECURITY */
  7625.  
  7626.     if (n <= cmd_rows - 3)
  7627.       printf("\n");
  7628.     return(0);
  7629. }
  7630. #endif /* NOSHOW */
  7631.  
  7632. #ifndef NOHELP
  7633. /* FTP HELP text strings */
  7634.  
  7635. static char * fhs_ftp[] = {
  7636.     "Syntax: FTP subcommand [ operands ]",
  7637.     "  Makes an FTP connection, or sends a command to the FTP server.",
  7638.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  7639.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  7640.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  7641.     ""
  7642. };
  7643.  
  7644. static char * fhs_acc[] = {             /* ACCOUNT */
  7645.     "Syntax: FTP ACCOUNT text",
  7646.     "  Sends an account designator to an FTP server that needs one.",
  7647.     "  Most FTP servers do not use accounts; some use them for other",
  7648.     "  other purposes, such as disk-access passwords.",
  7649.     ""
  7650. };
  7651. static char * fhs_app[] = {             /* APPEND */
  7652.     "Syntax: FTP APPEND filname",
  7653.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  7654.     ""
  7655. };
  7656. static char * fhs_cls[] = {             /* BYE, CLOSE */
  7657.     "Syntax: [ FTP ] BYE",
  7658.     "  Logs out from the FTP server and closes the FTP connection.",
  7659.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  7660.     ""
  7661. };
  7662. static char * fhs_cwd[] = {             /* CD, CWD */
  7663.     "Syntax: [ FTP ] CD directory",
  7664.     "  Asks the FTP server to change to the given directory.",
  7665.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  7666.     ""
  7667. };
  7668. static char * fhs_gup[] = {             /* CDUP, UP */
  7669.     "Syntax: FTP CDUP",
  7670.     "  Asks the FTP server to change to the parent directory of its current",
  7671.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  7672.     ""
  7673. };
  7674. static char * fhs_chm[] = {             /* CHMOD */
  7675.     "Syntax: FTP CHMOD filename permissions",
  7676.     "  Asks the FTP server to change the permissions, protection, or mode of",
  7677.     "  the given file.  The given permissions must be in the syntax of the",
  7678.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  7679.     "  FTP PUT /PERMISSIONS",
  7680.     ""
  7681. };
  7682. static char * fhs_mde[] = {             /* DELETE */
  7683.     "Syntax: FTP DELETE [ switches ] filespec",
  7684.     "  Asks the FTP server to delete the given file or files.",
  7685.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  7686.     "  multiple file deletion).  Optional switches:",
  7687.     " ",
  7688.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7689.     "  /EXCEPT:pattern",
  7690.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  7691.     "  /LARGER-THAN:number",
  7692. #ifdef UNIXOROSK
  7693.     "  /NODOTFILES",
  7694. #endif /* UNIXOROSK */
  7695.     "  /QUIET",
  7696. #ifdef RECURSIVE
  7697.     "  /RECURSIVE (depends on server)",
  7698.     "  /SUBDIRECTORIES",
  7699. #endif /* RECURSIVE */
  7700.     "  /SMALLER-THAN:number",
  7701.     ""
  7702. };
  7703. static char * fhs_dir[] = {             /* DIRECTORY */
  7704.     "Syntax: FTP DIRECTORY [ filespec ]",
  7705.     "  Asks the server to send a directory listing of the files that match",
  7706.     "  the given filespec, or if none is given, all the files in its current",
  7707.     "  directory.  The filespec, including any wildcards, must be in the",
  7708.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  7709.     "  Synonym: RDIRECTORY.",
  7710.     ""
  7711. };
  7712. static char * fhs_vdi[] = {             /* VDIRECTORY */
  7713.     "Syntax: FTP VDIRECTORY [ filespec ]",
  7714.     "  Asks the server to send a directory listing of the files that match",
  7715.     "  the given filespec, or if none is given, all the files in its current",
  7716.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  7717.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  7718.     "  FTP DIRECTORY lists only filenames without details.",
  7719.     ""
  7720. };
  7721. static char * fhs_fea[] = {             /* FEATURES */
  7722.     "Syntax: FTP FEATURES",
  7723.     "  Asks the FTP server to list its special features.  Most FTP servers",
  7724.     "  do not recognize this command.",
  7725.     ""
  7726. };
  7727. static char * fhs_mge[] = {             /* MGET */
  7728.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  7729.     "  Download a single file or multiple files.  Asks the FTP server to send",
  7730.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  7731.     " ",
  7732.     "  /AS-NAME:text",
  7733.     "    Name under which to store incoming file.",
  7734.     "    Pattern required for for multiple files.",
  7735.     "  /BINARY",                        /* /IMAGE */
  7736.     "    Force binary mode.  Synonym: /IMAGE.",
  7737.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  7738.    "    What to do if an incoming file has the same name as an existing file.",
  7739.  
  7740. #ifdef PUTPIPE
  7741.     "  /COMMAND",
  7742.     "    Specifies that the as-name is a command to which the incoming file",
  7743.     "    is to be piped as standard input.",
  7744. #endif /* PUTPIPE */
  7745.     "  /DELETE",
  7746.     "    Specifies that each file is to be deleted from the server after,",
  7747.     "    and only if, it is successfully downloaded.",
  7748.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7749.     "    When downloading a group of files, what to do upon failure to",
  7750.     "    transfer a file: quit or proceed to the next one.",
  7751.     "  /EXCEPT:pattern",
  7752.     "    Exception list: don't download any files that match this pattern.",
  7753.     "    See HELP WILDCARD for pattern syntax.",
  7754.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  7755.     "    Whether to convert incoming filenames to local syntax.",
  7756. #ifdef PIPESEND
  7757. #ifndef NOSPL
  7758.     "  /FILTER:command",
  7759.     "    Pass incoming files through the given command.",
  7760. #endif /* NOSPL */
  7761. #endif /* PIPESEND */
  7762.     "  /LARGER-THAN:number",
  7763.     "    Only download files that are larger than the given number of bytes.",
  7764.     "  /LISTFILE:filename",
  7765.     "    Obtain the list of files to download from the given file.",
  7766. #ifndef NOCSETS
  7767.     "  /LOCAL-CHARACTER-SET:name",
  7768.     "    When downloading in text mode and character-set conversion is",
  7769.     "    desired, this specifies the target set.",
  7770. #endif /* NOCSETS */
  7771. #ifdef CK_TMPDIR
  7772.     "  /MOVE-TO:directory",
  7773.     "    Each file that is downloaded is to be moved to the given local",
  7774.     "    directory immediately after, and only if, it has been received",
  7775.     "    successfully.",
  7776. #endif /* CK_TMPDIR */
  7777.     "  /NAMELIST:filename",
  7778.     "    Instead of downloading the files, stores the list of files that",
  7779.     "    would be downloaded in the given local file, one filename per line.",
  7780.     "  /NOBACKUPFILES",
  7781.     "    Don't download any files whose names end with .~<number>~.",
  7782. #ifdef UNIXOROSK
  7783.     "  /NODOTFILES",
  7784.     "    Don't download any files whose names begin with period (.).",
  7785. #endif /* UNIXOROSK */
  7786.     "  /QUIET",
  7787.     "    Suppress the file-transfer display.",
  7788. #ifdef FTP_RESTART
  7789.     "  /RECOVER",                       /* /RESTART */
  7790.     "    Resume a download that was previously interrupted from the point of",
  7791.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  7792.     "    Synonym: /RESTART.",
  7793. #endif /* FTP_RESTART */
  7794. #ifdef RECURSIVE
  7795.     "  /RECURSIVE",                     /* /SUBDIRECTORIES */
  7796.     "    Create subdirectories automatically if the server sends files",
  7797.     "    recursively and includes pathnames (most don't).",
  7798. #endif /* RECURSIVE */
  7799.     "  /RENAME-TO:text",
  7800.     "    Each file that is downloaded is to be renamed as indicated just,",
  7801.     "    after, and only if, it has arrived successfully.",
  7802. #ifndef NOCSETS
  7803.     "  /SERVER-CHARACTER-SET:name",
  7804.     "    When downloading in text mode and character-set conversion is desired"
  7805. ,   "    this specifies the original file's character set on the server.",
  7806. #endif /* NOCSETS */
  7807.     "  /SERVER-RENAME:text",
  7808.     "    Each server source file is to be renamed on the server as indicated",
  7809.     "    immediately after, but only if, it has arrived succesfully.",
  7810.     "  /SMALLER-THAN:number",
  7811.     "    Download only those files smaller than the given number of bytes.",
  7812.     "  /TEXT",                          /* /ASCII */
  7813.     "    Force text mode.  Synonym: /ASCII.",
  7814.     "  /TENEX",
  7815.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  7816. #ifndef NOCSETS
  7817.     "  /TRANSPARENT",
  7818.     "    When downloading in text mode, do not convert chracter-sets.",
  7819. #endif /* NOCSETS */
  7820.     "  /TO-SCREEN",
  7821.     "    The downloaded file is to be displayed on the screen.",
  7822. #ifdef DOUPDATE
  7823.     "  /UPDATE",
  7824.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  7825.     "    newer than than their local counterparts.",
  7826. #endif /* DOUPDATE */
  7827.     ""
  7828. };
  7829. static char * fhs_hlp[] = {             /* HELP */
  7830.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  7831.     "  Asks the FTP server for help about the given command.  First use",
  7832.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  7833.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  7834.     ""
  7835. };
  7836. static char * fhs_idl[] = {             /* IDLE */
  7837.     "Syntax: FTP IDLE [ number ]",
  7838.     "  If given without a number, this asks the FTP server to tell its",
  7839.     "  current idle-time limit.  If given with a number, it asks the server",
  7840.     "  to change its idle-time limit to the given number of seconds.",
  7841.     ""
  7842. };
  7843. static char * fhs_usr[] = {             /* USER, LOGIN */
  7844.     "Syntax: FTP USER username [ password [ account ] ]",
  7845.     "  Log in to the FTP server.  To be used when connected but not yet",
  7846.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  7847.     "  If you omit the password, and one is required by the server, you are",
  7848.     "  prompted for it.  If you omit the account, no account is sent.",
  7849.     "  Synonym: FTP LOGIN.",
  7850.     ""
  7851. };
  7852. static char * fhs_get[] = {             /* GET */
  7853.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  7854.     "  Download a single file.  Asks the FTP server to send the given file.",
  7855.     "  The optional as-name is the name to store it under when it arrives;",
  7856.     "  if omitted, the file is stored with the name it arrived with, as",
  7857.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  7858.     "  value.  Aside from the file list and as-name, syntax and options are",
  7859.     "  the same as for FTP MGET, which is used for downloading multiple files."
  7860. ,   ""
  7861. };
  7862. static char * fhs_mkd[] = {             /* MKDIR */
  7863.     "Syntax: FTP MKDIR directory",
  7864.     "  Asks the FTP server to create a directory with the given name,",
  7865.     "  which must be in the syntax of the server's file system.  Synonyms:",
  7866.     "  REMOTE MKDIR, RMKDIR.",
  7867.     ""
  7868. };
  7869. static char * fhs_mod[] = {             /* MODTIME */
  7870.     "Syntax: FTP MODTIME filename",
  7871.     "  Asks the FTP server to send the modification time of the given file,",
  7872.     "  to be displayed on the screen.  The date-time format is all numeric:",
  7873.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  7874.     "  fractions of seconds).",
  7875.     ""
  7876. };
  7877. static char * fhs_mpu[] = {             /* MPUT */
  7878.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  7879.     "  Uploads files.  Sends the given file or files to the FTP server.",
  7880.     "  Also see FTP PUT.  Optional switches are:",
  7881.     " ",
  7882.     "  /AFTER:date-time",
  7883.     "    Uploads only those files newer than the given date-time.",
  7884.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  7885. #ifdef PUTARRAY
  7886.     "  /ARRAY:array-designator",
  7887.     "    Tells Kermit to upload the contents of the given array, rather than",
  7888.     "    a file.",
  7889. #endif /* PUTARRAY */
  7890.     "  /AS-NAME:text",
  7891.     "    Name under which to send files.",
  7892.     "    Pattern required for for multiple files.",
  7893.     "  /BEFORE:date-time",
  7894.     "    Upload only those files older than the given date-time.",
  7895.     "  /BINARY",
  7896.     "    Force binary mode.  Synonym: /IMAGE.",
  7897. #ifdef PUTPIPE
  7898.     "  /COMMAND",
  7899.     "    Specifies that the filespec is a command whose standard output is",
  7900.     "    to be sent.",
  7901. #endif /* PUTPIPE */
  7902.     "  /DELETE",
  7903.     "    Specifies that each source file is to be deleted after, and only if,",
  7904.     "    it is successfully uploaded.",
  7905.     "  /DOTFILES",
  7906.     "    Include files whose names begin with period (.).",
  7907.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  7908.     "    When uploading a group of files, what to do upon failure to",
  7909.     "    transfer a file: quit or proceed to the next one.",
  7910.     "  /EXCEPT:pattern",
  7911.     "    Exception list: don't upload any files that match this pattern.",
  7912.     "    See HELP WILDCARD for pattern syntax.",
  7913.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  7914.     "    Whether to convert outbound filenames to common syntax.",
  7915. #ifdef PIPESEND
  7916. #ifndef NOSPL
  7917.     "  /FILTER:command",
  7918.     "    Pass outbound files through the given command.",
  7919. #endif /* NOSPL */
  7920. #endif /* PIPESEND */
  7921. #ifdef CKSYMLINK
  7922.     "  /FOLLOWINKS",
  7923.     "    Send files that are pointed to by symbolic links.",
  7924.     "  /NOFOLLOWINKS",
  7925.     "    Skip over symbolic links (default).",
  7926. #endif /* CKSYMLINK */
  7927.     "  /LARGER-THAN:number",
  7928.     "    Only upload files that are larger than the given number of bytes.",
  7929.     "  /LISTFILE:filename",
  7930.     "    Obtain the list of files to upload from the given file.",
  7931. #ifndef NOCSETS
  7932.     "  /LOCAL-CHARACTER-SET:name",
  7933.     "    When uploading in text mode and character-set conversion is",
  7934.     "    desired, this specifies the source-file character set.",
  7935. #endif /* NOCSETS */
  7936. #ifdef CK_TMPDIR
  7937.     "  /MOVE-TO:directory",
  7938.     "    Each source file that is uploaded is to be moved to the given local",
  7939.     "    directory when, and only if, the transfer is successful.",
  7940. #endif /* CK_TMPDIR */
  7941.     "  /NOBACKUPFILES",
  7942.     "    Don't upload any files whose names end with .~<number>~.",
  7943. #ifdef UNIXOROSK
  7944.     "  /NODOTFILES",
  7945.     "    Don't upload any files whose names begin with period (.).",
  7946. #endif /* UNIXOROSK */
  7947.     "  /NOT-AFTER:date-time",
  7948.     "    Upload only files that are not newer than the given date-time",
  7949.     "  /NOT-BEFORE:date-time",
  7950.     "    Upload only files that are not older than the given date-time",
  7951. #ifdef UNIX
  7952.     "  /PERMISSIONS",
  7953.     "    Ask the server to set the permissions of each file it receives",
  7954.     "    according to the source file's permissions.",
  7955. #endif /* UNIX */
  7956.     "  /QUIET",
  7957.     "    Suppress the file-transfer display.",
  7958. #ifdef FTP_RESTART
  7959.     "  /RECOVER",
  7960.     "    Resume an upload that was previously interrupted from the point of",
  7961.     "    failure.  Synonym: /RESTART.",
  7962. #endif /* FTP_RESTART */
  7963. #ifdef RECURSIVE
  7964.     "  /RECURSIVE",
  7965.     "    Send files from the given directory and all the directories beneath",
  7966.     "    it.  Synonym: /SUBDIRECTORIES.",
  7967. #endif /* RECURSIVE */
  7968.     "  /RENAME-TO:text",
  7969.     "    Each source file that is uploaded is to be renamed on the local",
  7970.     "    local computer as indicated when and only if, the transfer completes",
  7971.     "    successfully.",
  7972. #ifndef NOCSETS
  7973.     "  /SERVER-CHARACTER-SET:name",
  7974.     "    When uploading in text mode and character-set conversion is desired,",
  7975.     "    this specifies the character set to which the file should be",
  7976.     "    converted for storage on the server.",
  7977. #endif /* NOCSETS */
  7978.     "  /SERVER-RENAME:text",
  7979.     "    Each file that is uploaded is to be renamed as indicated on the",
  7980.     "    server after, and only if, if arrives successfully.",
  7981.     "  /SIMULATE",
  7982.     "    Show which files would be sent without actually sending them.",
  7983.     "  /SMALLER-THAN:number",
  7984.     "    Upload only those files smaller than the given number of bytes.",
  7985.     "  /TEXT",
  7986.     "    Force text mode.  Synonym: /ASCII.",
  7987.     "  /TENEX",
  7988.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  7989. #ifndef NOCSETS
  7990.     "  /TRANSPARENT",
  7991.     "    When uploading in text mode, do not convert chracter-sets.",
  7992. #endif /* NOCSETS */
  7993.     "  /TYPE:{TEXT,BINARY}",
  7994.     "    Upload only files of the given type.",
  7995. #ifdef DOUPDATE
  7996.     "  /UPDATE",
  7997.     "    If a file of the same name exists on the server, upload only if",
  7998.     "    the local file is newer.",
  7999. #endif /* DOUPDATE */
  8000.     "  /UNIQUE-SERVER-NAMES",
  8001.     "    Ask the server to compute new names for any incoming file that has",
  8002.     "    the same name as an existing file.",
  8003.     ""
  8004. };
  8005. static char * fhs_opn[] = {             /* OPEN */
  8006. #ifdef CK_SSL
  8007.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  8008.     "  Opens a connection to the FTP server on the given host.  The default",
  8009.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  8010.     "  can be supplied if necessary.  Optional switches are:",
  8011. #else /* CK_SSL */
  8012.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  8013.     "  Opens a connection to the FTP server on the given host.  The default",
  8014.     "  TCP port is 21, but a different port number can be supplied if",
  8015.     "  necessary.  Optional switches are:",
  8016. #endif /* CK_SSL */
  8017.     " ",
  8018.     "  /ANONYMOUS",
  8019.     "    Logs you in anonymously.",
  8020.     "  /USER:text",
  8021.     "    Supplies the given text as your username.",
  8022.     "  /PASSWORD:text",
  8023.     "    Supplies the given text as your password.  If you include a username",
  8024.     "    but omit this switch and the server requires a password, you are",
  8025.     "    prompted for it.",
  8026.     "  /ACCOUNT:text",
  8027.     "    Supplies the given text as your account, if required by the server.",
  8028.     "  /ACTIVE",
  8029.     "    Forces an active (rather than passive) connection.",
  8030.     "  /PASSIVE",
  8031.     "    Forces a passive (rather than active) connection.",
  8032.     "  /NOINIT",
  8033.     "    Inhibits sending initial REST, STRU, and MODE commands, which are",
  8034.     "    well-known standard commands, but to which some servers react badly.",
  8035.     ""
  8036. };
  8037. static char * fhs_opt[] = {             /* OPTS, OPTIONS */
  8038.     "Syntax: FTP OPTIONS",
  8039.     "  Asks the FTP server to list its current options.  Advanced, new,",
  8040.     "  not supported by most FTP servers.",
  8041.     ""
  8042. };
  8043. static char * fhs_put[] = {             /* PUT, SEND */
  8044.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  8045.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  8046.     "  by an additional field, this is interpreted as the name under which",
  8047.     "  to send the file or files.  See HELP FTP MPUT.",
  8048.     ""
  8049. };
  8050. static char * fhs_pwd[] = {             /* PWD */
  8051.     "Syntax: FTP PWD",
  8052.     "  Asks the FTP server to reveal its current working directory.",
  8053.     "  Synonyms: REMOTE PWD, RPWD.",
  8054.     ""
  8055. };
  8056. static char * fhs_quo[] = {             /* QUOTE */
  8057.     "Syntax: FTP QUOTE text",
  8058.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  8059.     "  for sending commands that Kermit might not support.",
  8060.     ""
  8061. };
  8062. static char * fhs_rge[] = {             /* REGET */
  8063.     "Syntax: FTP REGET",
  8064.     "  Synonym for FTP GET /RECOVER.",
  8065.     ""
  8066. };
  8067. static char * fhs_ren[] = {             /* RENAME */
  8068.     "Syntax: FTP RENAME name1 name1",
  8069.     "  Asks the FTP server to change the name of the file whose name is name1",
  8070.     "  and which resides in the FTP server's file system, to name2.  Works",
  8071.     "  only for single files; wildcards are not accepted.",
  8072.     ""
  8073. };
  8074. static char * fhs_res[] = {             /* RESET */
  8075.     "Syntax: FTP RESET",
  8076.     "  Asks the server to log out your session, terminating your access",
  8077.     "  rights, without closing the connection.",
  8078.     ""
  8079. };
  8080. static char * fhs_rmd[] = {             /* RMDIR */
  8081.     "Syntax: FTP RMDIR directory",
  8082.     "  Asks the FTP server to remove the directory whose name is given.",
  8083.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  8084.     "  RMDIR, RRMDIR.",
  8085.     ""
  8086. };
  8087. static char * fhs_sit[] = {             /* SITE */
  8088.     "Syntax: FTP SITE text",
  8089.     "  Sends a site-specific command to the FTP server.",
  8090.     ""
  8091. };
  8092. static char * fhs_siz[] = {             /* SIZE */
  8093.     "Syntax: FTP SIZE filename",
  8094.     "  Asks the FTP server to send a numeric string representing the size",
  8095.     "  of the given file.",
  8096.     ""
  8097. };
  8098. static char * fhs_sta[] = {             /* STATUS */
  8099.     "Syntax: FTP STATUS [ filename ]",
  8100.     "  Asks the FTP server to report its status.  If a filename is given,",
  8101.     "  the FTP server should report details about the file.",
  8102.     ""
  8103. };
  8104. static char * fhs_sys[] = {             /* SYSTEM */
  8105.     "Syntax: FTP SYSTEM",
  8106.     "  Asks the FTP server to report its operating system type.",
  8107.     ""
  8108. };
  8109. static char * fhs_typ[] = {             /* TYPE */
  8110.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  8111.     "  Puts the client and server in the indicated transfer mode.",
  8112.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  8113.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  8114.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  8115.     "  TENEX mode.",
  8116.     ""
  8117. };
  8118. static char * fhs_uma[] = {             /* UMASK */
  8119.     "Syntax: FTP UMASK number",
  8120.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  8121.     "  only (or mainly) to UNIX-based FTP servers.",
  8122.     ""
  8123. };
  8124. static char * fhs_chk[] = {             /* CHECK */
  8125.     "Syntax: FTP CHECK remote-filespec",
  8126.     "  Asks the FTP server if the given file or files exist.  If the",
  8127.     "  remote-filespec contains wildcards, this command fails if no server",
  8128.     "  files match, and succeeds if at least one file matches.  If the",
  8129.     "  remote-filespec does not contain wildcards, this command succeeds if",
  8130.     "  the given file exists and fails if it does not.",
  8131.     ""
  8132. };
  8133.  
  8134. #endif /* NOHELP */
  8135.  
  8136. int
  8137. doftphlp() {
  8138.     int cx;
  8139.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  8140.       if (cx != -3)
  8141.         return(cx);
  8142.     if ((x = cmcfm()) < 0)
  8143.       return(x);
  8144.  
  8145. #ifdef NOHELP
  8146.     printf("Sorry, no help available\n");
  8147. #else
  8148.     switch (cx) {
  8149.       case -3:
  8150.         return(hmsga(fhs_ftp));
  8151.       case FTP_ACC:                     /* ACCOUNT */
  8152.         return(hmsga(fhs_acc));
  8153.       case FTP_APP:                     /* APPEND */
  8154.         return(hmsga(fhs_app));
  8155.       case FTP_CLS:                     /* BYE, CLOSE */
  8156.         return(hmsga(fhs_cls));
  8157.       case FTP_CWD:                     /* CD, CWD */
  8158.         return(hmsga(fhs_cwd));
  8159.       case FTP_GUP:                     /* CDUP, UP */
  8160.         return(hmsga(fhs_gup));
  8161.       case FTP_CHM:                     /* CHMOD */
  8162.         return(hmsga(fhs_chm));
  8163.       case FTP_MDE:                     /* DELETE, MDELETE */
  8164.         return(hmsga(fhs_mde));
  8165.       case FTP_DIR:                     /* DIRECTORY */
  8166.         return(hmsga(fhs_dir));
  8167.       case FTP_VDI:                     /* VDIRECTORY */
  8168.         return(hmsga(fhs_vdi));
  8169.       case FTP_FEA:                     /* FEATURES */
  8170.         return(hmsga(fhs_fea));
  8171.       case FTP_GET:                     /* GET */
  8172.         return(hmsga(fhs_get));
  8173.       case FTP_HLP:                     /* HELP */
  8174.         return(hmsga(fhs_hlp));
  8175.       case FTP_IDL:                     /* IDLE */
  8176.         return(hmsga(fhs_idl));
  8177.       case FTP_USR:                     /* USER, LOGIN */
  8178.         return(hmsga(fhs_usr));
  8179.       case FTP_MGE:                     /* MGET */
  8180.         return(hmsga(fhs_mge));
  8181.       case FTP_MKD:                     /* MKDIR */
  8182.         return(hmsga(fhs_mkd));
  8183.       case FTP_MOD:                     /* MODTIME */
  8184.         return(hmsga(fhs_mod));
  8185.       case FTP_MPU:                     /* MPUT */
  8186.         return(hmsga(fhs_mpu));
  8187.       case FTP_OPN:                     /* OPEN */
  8188.         return(hmsga(fhs_opn));
  8189.       case FTP_OPT:                     /* OPTS, OPTIONS */
  8190.         return(hmsga(fhs_opt));
  8191.       case FTP_PUT:                     /* PUT, SEND */
  8192.         return(hmsga(fhs_put));
  8193.       case FTP_PWD:                     /* PWD */
  8194.         return(hmsga(fhs_pwd));
  8195.       case FTP_QUO:                     /* QUOTE */
  8196.         return(hmsga(fhs_quo));
  8197.       case FTP_RGE:                     /* REGET */
  8198.         return(hmsga(fhs_rge));
  8199.       case FTP_REN:                     /* RENAME */
  8200.         return(hmsga(fhs_ren));
  8201.       case FTP_RES:                     /* RESET */
  8202.         return(hmsga(fhs_res));
  8203.       case FTP_RMD:                     /* RMDIR */
  8204.         return(hmsga(fhs_rmd));
  8205.       case FTP_SIT:                     /* SITE */
  8206.         return(hmsga(fhs_sit));
  8207.       case FTP_SIZ:                     /* SIZE */
  8208.         return(hmsga(fhs_siz));
  8209.       case FTP_STA:                     /* STATUS */
  8210.         return(hmsga(fhs_sta));
  8211.       case FTP_SYS:                     /* SYSTEM */
  8212.         return(hmsga(fhs_sys));
  8213.       case FTP_TYP:                     /* TYPE */
  8214.         return(hmsga(fhs_typ));
  8215.       case FTP_UMA:                     /* UMASK */
  8216.         return(hmsga(fhs_uma));
  8217.       case FTP_CHK:                     /* CHECK */
  8218.         return(hmsga(fhs_chk));
  8219.       default:
  8220.         printf("Sorry, FTP command help not available yet\n");
  8221.         break;
  8222.     }
  8223. #endif /* NOHELP */
  8224.     return(0);
  8225. }
  8226.  
  8227. int
  8228. dosetftphlp() {
  8229.     int cx;
  8230.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  8231.       if (cx != -3)
  8232.         return(cx);
  8233.     if (cx != -3)
  8234.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  8235.     if ((x = cmcfm()) < 0)
  8236.       return(x);
  8237.  
  8238. #ifdef NOHELP
  8239.     printf("Sorry, no help available\n");
  8240. #else
  8241.     switch (cx) {
  8242.       case -3:
  8243.         printf("\nSyntax: SET FTP parameter value\n");
  8244.         printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  8245.         printf("  Type \"help set ftp xxx\" for information about setting\n");
  8246.         printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  8247.         return(0);
  8248.  
  8249. #ifdef FTP_SECURITY
  8250.       case FTS_ATP:                     /* "authtype" */
  8251.         printf("\nSyntax: SET FTP AUTHTYPE list\n");
  8252.         printf("  Specifies an ordered list of authentication methods to be\n"
  8253.                );
  8254.         printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  8255.         printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  8256.         return(0);
  8257.  
  8258.       case FTS_AUT:                     /* "autoauthentication" */
  8259.         printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  8260.         printf("  Tells whether authentication should be negotiated by the\n");
  8261.         printf("  FTP OPEN command.  Default is ON.\n\n");
  8262.         break;
  8263.  
  8264.       case FTS_CRY:                     /* "autoencryption" */
  8265.         printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  8266.         printf("  Tells whether encryption (privacy) should be negotiated\n");
  8267.         printf("  by the FTP OPEN command.  Default is ON.\n\n");
  8268.         break;
  8269.  
  8270. #endif /* FTP_SECURITY */
  8271.       case FTS_LOG:                     /* "autologin" */
  8272.         printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  8273.         printf("  Tells Kermit whether to try to log you in automatically\n");
  8274.         printf("  as part of the connection process.\n\n");
  8275.         break;
  8276.  
  8277. #ifndef NOCSETS
  8278.       case FTS_XLA:                     /* "character-set-translation" */
  8279.         printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  8280.         printf("  Whether to translate character sets when transferring\n");
  8281.         printf("  text files with FTP.  OFF by default.\n\n");
  8282.         break;
  8283.  
  8284. #endif /* NOCSETS */
  8285.       case FTS_FNC:                     /* "collision" */
  8286.         printf("\n");
  8287.         printf(
  8288. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  8289.                );
  8290.         printf("  Tells what do when an incoming file has the same name as\n");
  8291.         printf("  an existing file when downloading with FTP.\n\n");
  8292.         break;
  8293.  
  8294. #ifdef FTP_SECURITY
  8295.       case FTS_CPL:                     /* "command-protection-level" */
  8296.         printf("\n");
  8297.         printf(
  8298. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8299.                );
  8300.         printf("\n");
  8301.         printf(
  8302. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  8303.         break;
  8304.       case FTS_CFW:                     /* "credential-forwarding" */
  8305.         printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  8306.         printf("  Tells whether end-user credentials are to be forwarded\n");
  8307.         printf("  to the server if supported by the authentication method\n");
  8308.         printf("  (GSSAPI-KRB5 only).\n\n");
  8309.         break;
  8310.       case FTS_DPL:                     /* "data-protection-level" */
  8311.         printf("\n");
  8312.         printf(
  8313. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  8314.                );
  8315.         printf("\n");
  8316.         printf(
  8317. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  8318.         break;
  8319. #endif /* FTP_SECURITY */
  8320.  
  8321.       case FTS_DBG:                     /* "debug" */
  8322.         printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  8323.         printf("  Whether to print FTP protocol messages.\n\n");
  8324.         return(0);
  8325.  
  8326.       case FTS_ERR:                     /* "error-action" */
  8327.         printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  8328.         printf("  What to do when an error occurs when transferring a group\n")
  8329.           ;
  8330.         printf("  of files: quit and fail, or proceed to the next file.\n\n");
  8331.         return(0);
  8332.  
  8333.       case FTS_CNV:                     /* "filenames" */
  8334.         printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  8335.         printf("  What to do with filenames: convert them, take and use them\n"
  8336.                );
  8337.         printf("  literally; or choose what to do automatically based on the\n"
  8338.                );
  8339.         printf("  OS type of the server.  The default is AUTO.\n\n");
  8340.         return(0);
  8341.  
  8342.       case FTS_PSV:                     /* "passive-mode" */
  8343.         printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  8344.         printf("  Whether to use passive mode, which helps to get through\n");
  8345.         printf("  firewalls.  ON by default.\n\n");
  8346.         return(0);
  8347.  
  8348.       case FTS_PRM:                     /* "permissions" */
  8349.         printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  8350.         printf("  Whether to try to send file permissions when uploading.\n");
  8351.         printf("  OFF by default.  AUTO means only if client and server\n");
  8352.         printf("  have the same OS type.\n\n");
  8353.         return(0);
  8354.  
  8355.       case FTS_TST:                     /* "progress-messages" */
  8356.         printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  8357.         printf("  Whether Kermit should print locally-generated feedback\n");
  8358.         printf("  messages for each non-file-transfer command.");
  8359.         printf("  ON by default.\n\n");
  8360.         return(0);
  8361.  
  8362.       case FTS_SPC:                     /* "send-port-commands" */
  8363.         printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  8364.         printf("  Whether Kermit should send a new PORT command for each");
  8365.         printf("  task.\n\n");
  8366.         return(0);
  8367.  
  8368. #ifndef NOCSETS
  8369.       case FTS_CSR:                     /* "server-character-set" */
  8370.         printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  8371.         printf("  The name of the character set used for text files on the\n");
  8372.         printf("  server.  Enter a name of '?' for a menu.\n\n");
  8373.         return(0);
  8374. #endif /* NOCSETS */
  8375.  
  8376.       case FTS_TYP:                     /* "type" */
  8377.         printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  8378.         printf("  Establishes the default transfer mode.\n");
  8379.         printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  8380.         printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  8381.         printf("  them again.\n");
  8382.         return(0);
  8383.  
  8384. #ifdef PATTERNS
  8385.       case FTS_GFT:
  8386.         printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  8387.         printf("  Tells whether GET and MGET should automatically switch\n");
  8388.         printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  8389.         printf("  matching the name of each incoming file with its list of\n");
  8390.         printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  8391.         printf("  default.  SHOW PATTERNS displays the current pattern\n");
  8392.         printf("  list.  HELP SET FILE to see how to change it.\n");
  8393.         break;
  8394. #endif /* PATTERNS */
  8395.  
  8396.       case FTS_USN:                     /* "unique-server-names" */
  8397.         printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  8398.         printf("  Tells whether to ask the server to create unique names\n");
  8399.         printf("  for any uploaded file that has the same name as an\n");
  8400.         printf("  existing file.  Default is OFF.\n\n");
  8401.         return(0);
  8402.  
  8403.       case FTS_VBM:                     /* "verbose-mode" */
  8404.         printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  8405.         printf("  Whether to display all responses from the FTP server.\n");
  8406.         printf("  OFF by default.\n\n");
  8407.         return(0);
  8408.  
  8409.       case FTS_DAT:
  8410.         printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  8411.         printf("  Whether to set date of incoming files from the file date\n");
  8412.         printf("  on the server.  OFF by default.  Note: there is no way to\n")
  8413.           ;
  8414.         printf("  set the date on files uploaded to the server.\n\n");
  8415.         return(0);
  8416.  
  8417.       default:
  8418.         printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  8419.     }
  8420. #endif /* NOHELP */
  8421.     return(0);
  8422. }
  8423.  
  8424. #ifndef L_SET
  8425. #define L_SET 0
  8426. #endif /* L_SET */
  8427. #ifndef L_INCR
  8428. #define L_INCR 1
  8429. #endif /* L_INCR */
  8430.  
  8431. #ifdef FTP_SRP
  8432. char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
  8433. char *srp_pass;
  8434. char *srp_acct;
  8435. #endif /* FTP_SRP */
  8436.  
  8437. static int kerror;                      /* Needed for all auth types */
  8438.  
  8439. static struct   sockaddr_in hisctladdr;
  8440. static struct   sockaddr_in hisdataaddr;
  8441. static struct   sockaddr_in data_addr;
  8442. static int      data = -1;
  8443. static int      ptflag = 0;
  8444. static struct   sockaddr_in myctladdr;
  8445.  
  8446. #ifdef COMMENT
  8447. #ifndef OS2
  8448. UID_T getuid();
  8449. #endif /* OS2 */
  8450. #endif /* COMMENT */
  8451.  
  8452. #ifndef MAXHOSTNAMELEN
  8453. #define MAXHOSTNAMELEN 64
  8454. #endif /* MAXHOSTNAMELEN */
  8455.  
  8456. #define herror() printf("Unknown host\n")
  8457.  
  8458. static int cpend = 0;                   /* No pending replies */
  8459.  
  8460. #ifdef CK_SSL
  8461. extern SSL *ssl_ftp_con;
  8462. extern SSL_CTX *ssl_ftp_ctx;
  8463. extern SSL *ssl_ftp_data_con;
  8464. extern int ssl_ftp_active_flag;
  8465. extern int ssl_ftp_data_active_flag;
  8466. #endif /* CK_SSL */
  8467.  
  8468. /*  f t p c m d  --  Send a command to the FTP server  */
  8469. /*
  8470.   Call with:
  8471.     char * cmd: The command to send.
  8472.     char * arg: The argument (e.g. a filename).
  8473.     int lcs: The local character set index.
  8474.     int rcs: The remote (server) character set index.
  8475.     int vbm: Verbose mode:
  8476.       0 = force verbosity off
  8477.      >0 = force verbosity on
  8478.  
  8479.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  8480.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  8481.   character set to the remote one before sending the result to the server.
  8482.  
  8483.    Returns:
  8484.     0 on failure with ftpcode = -1
  8485.     >= 0 on success (getreply() result) with ftpcode = 0.
  8486. */
  8487. static char xcmdbuf[RFNBUFSIZ];
  8488.  
  8489. static int
  8490. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  8491.     char * s;
  8492.     int r, x, len = 0, cmdlen = 0;
  8493.     sig_t oldintr;
  8494.  
  8495.     if (ftp_deb)                        /* DEBUG */
  8496.       vbm = 1;
  8497.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  8498.       vbm = 0;
  8499.     else if (vbm < 0)                   /* VERBOSE */
  8500.       vbm = ftp_vbm;
  8501.  
  8502.     cancelfile = 0;
  8503.     if (!cmd) cmd = "";
  8504.     if (!arg) arg = "";
  8505.     cmdlen = (int)strlen(cmd);
  8506.     len = cmdlen + (int)strlen(arg) + 1;
  8507.  
  8508.     if (ftp_deb /* && !dpyactive */ ) {
  8509. #ifdef FTP_PROXY
  8510.         if (ftp_prx) printf("%s ", ftp_host);
  8511. #endif /* FTP_PROXY */
  8512.         printf("---> ");
  8513.         if (!anonymous && strcmp("PASS",cmd) == 0)
  8514.           printf("PASS XXXX");
  8515.         else
  8516.           printf("%s %s",cmd,arg);
  8517.         printf("\n");
  8518.     }
  8519.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  8520.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  8521.  
  8522. #ifdef DEBUG
  8523.     if (deblog) {
  8524.         if (!anonymous && strcmp("PASS", cmd) == 0) {
  8525.             debug(F111,"ftpcmd buf1","PASS XXXX",csocket);
  8526.         } else {
  8527.             debug(F111,"ftpcmd buf1",xcmdbuf,csocket);
  8528.         }
  8529.         debug(F101,"ftpcmd lcs","",lcs);
  8530.         debug(F101,"ftpcmd rcs","",rcs);
  8531.     }
  8532. #endif /* DEBUG */
  8533.     if (csocket == -1) {
  8534.         perror("No control connection for command");
  8535.         ftpcode = -1;
  8536.         return(0);
  8537.     }
  8538.     oldintr = signal(SIGINT, cmdcancel);
  8539.  
  8540. #ifndef NOCSETS
  8541.     if (*arg &&                         /* If an arg was given */
  8542.         lcs > -1 &&                     /* and a local charset */
  8543.         rcs > -1 &&                     /* and a remote charset */
  8544.         lcs != rcs &&                   /* and the two are not the same */
  8545.         lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
  8546.         rcs != FC_UCS2                  /* ... */
  8547.         ) {
  8548.         initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
  8549.         xgnbp = arg;                    /* Global pointer to input string */
  8550.         rfnptr = rfnbuf;                /* Global pointer to output buffer */
  8551.         while (1) {
  8552.             c0 = xgnbyte(FC_UCS2,lcs,strgetc); /* Get byte from name string */
  8553.             if (c0 < 0)
  8554.               break;
  8555.             c1 = xgnbyte(FC_UCS2,lcs,strgetc); /* Second byte from string */
  8556.             if (c1 < 0)
  8557.               break;
  8558.             if ((x = xpnbyte(c0,TC_UCS2,rcs,strputc)) < 0) break;
  8559.             if ((x = xpnbyte(c1,TC_UCS2,rcs,strputc)) < 0) break;
  8560.         }
  8561.         /*
  8562.           We have to copy here instead of translating directly into
  8563.           xcmdbuf[] so strputc() can check length.  Alternatively we could
  8564.           write yet another xpnbyte() output function.
  8565.         */
  8566.         if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  8567.             printf("?FTP command too long: %s + arg\n",cmd,arg);
  8568.             ftpcode = -1;
  8569.             return(0);
  8570.         }
  8571.         x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  8572.     }
  8573. #endif /* NOCSETS */
  8574.  
  8575.     s = xcmdbuf;                        /* Command to send to server */
  8576.     debug(F110,"ftpcmd buf2",s,0);
  8577.  
  8578. #ifdef CK_ENCRYPTION
  8579.   again:
  8580. #endif /* CK_ENCRYPTION */
  8581.     if (scommand(s) == 0)               /* Send it. */
  8582.       return(0);
  8583.     cpend = 1;
  8584.  
  8585.     r = getreply(!strcmp(cmd, "QUIT"),lcs,rcs,vbm,
  8586.                  !(strncmp(cmd, "AUTH ", 5)
  8587. #ifdef FTPHOST
  8588.                     && strncmp(cmd, "HOST ",5)
  8589. #endif /* FTPHOST */
  8590.                     ));
  8591. #ifdef CK_ENCRYPTION
  8592.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  8593.         fprintf(stderr,
  8594.                "ENC command not supported at server; retrying under MIC...\n");
  8595.         ftp_cpl = FPL_SAF;
  8596.         goto again;
  8597.     }
  8598. #endif /* CK_ENCRYPTION */
  8599. #ifdef COMMENT
  8600.     if (cancelfile && oldintr != SIG_IGN)
  8601.       (*oldintr)(SIGINT);
  8602.     signal(SIGINT, oldintr);
  8603. #endif /* COMMENT */
  8604.     return(r);
  8605. }
  8606.  
  8607.  
  8608. static VOID
  8609. lostpeer() {
  8610.     debug(F100,"lostpeer","",0);
  8611.     if (connected) {
  8612.         if (csocket != -1) {
  8613. #ifdef CK_SSL
  8614.             if (ssl_ftp_active_flag) {
  8615.                 SSL_shutdown(ssl_ftp_con);
  8616.                 SSL_free(ssl_ftp_con);
  8617.                 ssl_ftp_proxy = 0;
  8618.                 ssl_ftp_active_flag = 0;
  8619.                 ssl_ftp_con = NULL;
  8620.             }
  8621. #endif /* CK_SSL */
  8622. #ifdef TCPIPLIB
  8623.             socket_close(csocket);
  8624. #else /* TCPIPLIB */
  8625. #ifdef USE_SHUTDOWN
  8626.             shutdown(csocket, 1+1);
  8627. #endif /* USE_SHUTDOWN */
  8628.             close(csocket);
  8629. #endif /* TCPIPLIB */
  8630.             csocket = -1;
  8631.         }
  8632.         if (data != -1) {
  8633. #ifdef CK_SSL
  8634.             if (ssl_ftp_data_active_flag) {
  8635.                 SSL_shutdown(ssl_ftp_data_con);
  8636.                 SSL_free(ssl_ftp_data_con);
  8637.                 ssl_ftp_data_active_flag = 0;
  8638.                 ssl_ftp_data_con = NULL;
  8639.             }
  8640. #endif /* CK_SSL */
  8641. #ifdef TCPIPLIB
  8642.             socket_close(data);
  8643. #else /* TCPIPLIB */
  8644. #ifdef USE_SHUTDOWN
  8645.             shutdown(data, 1+1);
  8646. #endif /* USE_SHUTDOWN */
  8647.             close(data);
  8648. #endif /* TCPIPLIB */
  8649.             data = -1;
  8650.             globaldin = -1;
  8651.         }
  8652.         connected = 0;
  8653.         anonymous = 0;
  8654.         loggedin = 0;
  8655.         auth_type = NULL;
  8656.         ftp_cpl = ftp_dpl = FPL_CLR;
  8657. #ifdef CKLOGDIAL
  8658.         ftplogend();
  8659. #endif /* CKLOGDIAL */
  8660.     }
  8661. #ifdef FTP_PROXY
  8662.     pswitch(1);
  8663.     if (connected) {
  8664.         if (csocket != -1) {
  8665. #ifdef TCPIPLIB
  8666.             socket_close(csocket);
  8667. #else /* TCPIPLIB */
  8668. #ifdef USE_SHUTDOWN
  8669.             shutdown(csocket, 1+1);
  8670. #endif /* USE_SHUTDOWN */
  8671.             close(csocket);
  8672. #endif /* TCPIPLIB */
  8673.             csocket = -1;
  8674.         }
  8675.         connected = 0;
  8676.         anonymous = 0;
  8677.         loggedin = 0;
  8678.         auth_type = NULL;
  8679.         ftp_cpl = ftp_dpl = FPL_CLR;
  8680.     }
  8681.     proxflag = 0;
  8682.     pswitch(0);
  8683. #endif /* FTP_PROXY */
  8684. }
  8685.  
  8686. int
  8687. ftpisopen() {
  8688.     return(connected);
  8689. }
  8690.  
  8691. static int
  8692. ftpclose() {
  8693. #ifdef LOCUS
  8694.     extern int locus, autolocus;
  8695. #endif /* LOCUS */
  8696.     extern int quitting;
  8697.     if (!connected)
  8698.       return(0);
  8699.     if (!ftp_vbm && !quiet) printlines = 1;
  8700.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  8701.     if (csocket) {
  8702. #ifdef CK_SSL
  8703.         if (ssl_ftp_active_flag) {
  8704.             SSL_shutdown(ssl_ftp_con);
  8705.             SSL_free(ssl_ftp_con);
  8706.             ssl_ftp_proxy = 0;
  8707.             ssl_ftp_active_flag = 0;
  8708.             ssl_ftp_con = NULL;
  8709.         }
  8710. #endif /* CK_SSL */
  8711. #ifdef TCPIPLIB
  8712.         socket_close(csocket);
  8713. #else /* TCPIPLIB */
  8714. #ifdef USE_SHUTDOWN
  8715.         shutdown(csocket, 1+1);
  8716. #endif /* USE_SHUTDOWN */
  8717.         close(csocket);
  8718. #endif /* TCPIPLIB */
  8719.     }
  8720.     csocket = -1;
  8721.     connected = 0;
  8722.     anonymous = 0;
  8723.     loggedin = 0;
  8724.     data = -1;
  8725.     globaldin = -1;
  8726. #ifdef FTP_PROXY
  8727.     if (!proxy)
  8728.       macnum = 0;
  8729. #endif /* FTP_PROXY */
  8730.     auth_type = NULL;
  8731.     ftp_dpl = FPL_CLR;
  8732. #ifdef CKLOGDIAL
  8733.     ftplogend();
  8734. #endif /* CKLOGDIAL */
  8735. #ifdef LOCUS
  8736.     /* Unprefixed file management commands are executed locally */
  8737.     if (autolocus && !ftp_cmdlin && !quitting) {
  8738.         if (!locus && msgflg)
  8739.           printf("Switching LOCUS for file-management commands to LOCAL.\n");
  8740.         locus = 1;
  8741.     }
  8742. #endif /* LOCUS */
  8743.     return(0);
  8744. }
  8745.  
  8746. int
  8747. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  8748.     char * host;
  8749. #ifdef LOCUS
  8750.     extern int locus, autolocus;
  8751. #endif /* LOCUS */
  8752.  
  8753.     if (connected) {
  8754.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  8755.         ftpcode = -1;
  8756.         return(0);
  8757.     }
  8758. #ifdef FTPHOST
  8759.     hostcmd = 0;
  8760. #endif /* FTPHOST */
  8761.     alike = 0;
  8762.     ftp_srvtyp[0] = NUL;
  8763.     if (!service) service = "";
  8764.     if (!*service) service = use_tls ? "ftps" : "ftp";
  8765.  
  8766.     if (!isdigit(service[0])) {
  8767.         struct servent *destsp;
  8768.         destsp = getservbyname(service, "tcp");
  8769.         if (!destsp) {
  8770.             if (!ckstrcmp(service,"ftp",-1,0)) {
  8771.                 ftp_port = 21;
  8772.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  8773.                 ftp_port = 990;
  8774.             } else {
  8775.                 printf("?Bad port name - \"%s\"\n", service);
  8776.                 ftpcode = -1;
  8777.                 return(0);
  8778.             }
  8779.         } else {
  8780.             ftp_port = destsp->s_port;
  8781.             ftp_port = ntohs(ftp_port);
  8782.         }
  8783.     } else
  8784.         ftp_port = atoi(service);
  8785.     if (ftp_port <= 0) {
  8786.         printf("?Bad port name - \"%s\"\n", service);
  8787.         ftpcode = -1;
  8788.         return(0);
  8789.     }
  8790.     host = ftp_hookup(remote, ftp_port, use_tls);
  8791.     if (host) {
  8792.         connected = 1;                  /* Set FTP defaults */
  8793.         ftp_cpl = ftp_dpl = FPL_CLR;
  8794.         curtype = FTT_ASC;              /* Server uses ASCII mode */
  8795.         form = FORM_N;
  8796.         mode = MODE_S;
  8797.         stru = STRU_F;
  8798.         strcpy(bytename, "8");
  8799.         bytesize = 8;
  8800.  
  8801. #ifdef FTP_SECURITY
  8802.         if (ftp_aut) {
  8803.             if (ftp_auth()) {
  8804.                 if (ftp_cry) {
  8805.                     if (!quiet)
  8806.                       printf("FTP Command channel is Private (encrypted)\n");
  8807.                     ftp_cpl = FPL_PRV;
  8808.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  8809.                         /* a failure here is most likely caused by a mixup */
  8810.                         /* in the session key used by client and server    */
  8811.                         return(0);
  8812.                     }
  8813.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  8814.                         if (!quiet)
  8815.                           printf("FTP Data channel is Private (encrypted)\n");
  8816.                         ftp_dpl = FPL_PRV;
  8817.                     } else
  8818.                       printf("?Unable to enable encryption on data channel\n");
  8819.                 } else {
  8820.                     ftp_cpl = FPL_SAF;
  8821.                 }
  8822.             }
  8823.             if (!connected)
  8824.           goto fail;
  8825.         }
  8826. #endif /* FTP_SECURITY */
  8827.         if (ftp_log)
  8828.           ftp_login(remote);
  8829.  
  8830.         if (!connected)
  8831.       goto fail;
  8832.  
  8833. #ifdef CKLOGDIAL
  8834.         dologftp();
  8835. #endif /* CKLOGDIAL */
  8836.         passivemode = ftp_psv;
  8837.         sendport = ftp_spc;
  8838.  
  8839.         if (ucbuf == NULL) {
  8840.             actualbuf = DEFAULT_PBSZ;
  8841.             while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  8842.               actualbuf >>= 2;
  8843.         }
  8844.         if (!maxbuf)
  8845.           ucbufsiz = actualbuf - FUDGE_FACTOR;
  8846. #ifdef LOCUS
  8847.         /* Unprefixed file management commands go to server */
  8848.         if (autolocus && !ftp_cmdlin) {
  8849.             if (locus && msgflg)
  8850.               printf(
  8851.                   "Switching LOCUS for file-management commands to REMOTE.\n"
  8852.                   );
  8853.             locus = 0;
  8854.         }
  8855. #endif /* LOCUS */
  8856.         return(1);
  8857.     }
  8858.   fail:
  8859.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  8860.     ftpcode = -1;
  8861.     return(0);
  8862. }
  8863.  
  8864. #ifdef CK_SSL
  8865. int
  8866. ssl_auth() {
  8867.     int i;
  8868.     char* p;
  8869.  
  8870.     if (ssl_debug_flag) {
  8871.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  8872.         fflush(stderr);
  8873.         /* for the moment I want the output on screen */
  8874.     }
  8875.     if (ssl_ftp_data_con != NULL) {
  8876.         SSL_free(ssl_ftp_data_con);
  8877.         ssl_ftp_data_con = NULL;
  8878.     }
  8879.     if (ssl_ftp_con != NULL) {
  8880.         SSL_free(ssl_ftp_con);
  8881.         ssl_ftp_con=NULL;
  8882.     }
  8883.     if (ssl_ftp_ctx != NULL) {
  8884.         SSL_CTX_free(ssl_ftp_ctx);
  8885.         ssl_ftp_ctx = NULL;
  8886.     }
  8887. #ifdef COMMENT
  8888.     if (auth_type && !strcmp(auth_type,"TLS")) {
  8889.         ssl_ftp_ctx=SSL_CTX_new(TLSv1_client_method());
  8890.         if (!ssl_ftp_ctx)
  8891.           return(0);
  8892.         SSL_CTX_set_options(ssl_ftp_ctx,
  8893.                    SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  8894.                             );
  8895.     } else {
  8896.         ssl_ftp_ctx = SSL_CTX_new(SSLv3_client_method());
  8897.         if (!ssl_ftp_ctx)
  8898.           return(0);
  8899.         SSL_CTX_set_options(ssl_ftp_ctx,SSL_OP_ALL);
  8900.     }
  8901. #else /* COMMENT */
  8902.     ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  8903.     if (!ssl_ftp_ctx)
  8904.       return(0);
  8905.     SSL_CTX_set_options(ssl_ftp_ctx,
  8906.                     SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  8907.                         );
  8908. #endif /* COMMENT */
  8909.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  8910.                                   (pem_password_cb *)ssl_passwd_callback);
  8911.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  8912.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  8913.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  8914.     if (ssl_verify_file &&
  8915.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
  8916.         debug(F110,
  8917.               "ftp ssl auth unable to load ssl_verify_file",
  8918.               ssl_verify_file,
  8919.               0
  8920.               );
  8921.         if (ssl_debug_flag)
  8922.           printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  8923.     }
  8924.     if (ssl_verify_dir &&
  8925.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
  8926.         debug(F110,
  8927.               "ftp ssl auth unable to load ssl_verify_dir",
  8928.               ssl_verify_dir,
  8929.               0
  8930.               );
  8931.         if (ssl_debug_flag)
  8932.           printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  8933.     }
  8934.  
  8935.     /* set up the new CRL Store */
  8936.     crl_store = (X509_STORE *)X509_STORE_new();
  8937.     if (crl_store) {
  8938.         if (ssl_crl_file || ssl_crl_dir) {
  8939.             if (ssl_crl_file &&
  8940.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  8941.                 debug(F110,
  8942.                       "ftp ssl auth unable to load ssl_crl_file",
  8943.                       ssl_crl_file,
  8944.                       0
  8945.                       );
  8946.                 if (ssl_debug_flag)
  8947.                   printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  8948.             }
  8949.             if (ssl_crl_dir &&
  8950.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  8951.                 debug(F110,
  8952.                       "ftp ssl auth unable to load ssl_crl_dir",
  8953.                       ssl_crl_dir,
  8954.                       0
  8955.                       );
  8956.                 if (ssl_debug_flag)
  8957.                   printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  8958.             }
  8959.         } else {
  8960.             X509_STORE_set_default_paths(crl_store);
  8961.         }
  8962.     }
  8963.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  8964.                        ssl_client_verify_callback);
  8965.     ssl_verify_depth = -1;
  8966.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  8967.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  8968.     SSL_set_fd(ssl_ftp_con,csocket);
  8969.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  8970.     if (ssl_cipher_list) {
  8971.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  8972.     } else {
  8973.         char * p;
  8974.         if (p = getenv("SSL_CIPHER")) {
  8975.             SSL_set_cipher_list(ssl_ftp_con,p);
  8976.         } else {
  8977.             SSL_set_cipher_list(ssl_ftp_con,
  8978.                                 "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP"
  8979.                                 );
  8980.         }
  8981.     }
  8982.     if (ssl_debug_flag) {
  8983.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  8984.         fflush(stderr);
  8985.     }
  8986.     if (SSL_connect(ssl_ftp_con) <= 0) {
  8987.         static char errbuf[1024];
  8988.         ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  8989.                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  8990.         fprintf(stderr,"%s\n", errbuf);
  8991.         fflush(stderr);
  8992.         ssl_ftp_active_flag=0;
  8993.         SSL_free(ssl_ftp_con);
  8994.         ssl_ftp_con = NULL;
  8995.     } else {
  8996.         ssl_ftp_active_flag = 1;
  8997.  
  8998.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  8999.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  9000.  
  9001.             if (!subject) {
  9002.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  9003.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9004.                     return(ssl_ftp_active_flag = 0);
  9005.                 } else {
  9006.                     char * prmpt =
  9007.            "Warning: Server didn't provide a certificate, continue? (Y/N) ";
  9008.                     if (!getyesno(prmpt,0)) {
  9009.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  9010.                         return(ssl_ftp_active_flag = 0);
  9011.                     }
  9012.                 }
  9013.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_host)) {
  9014.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  9015.                 return(ssl_ftp_active_flag = 0);
  9016.             }
  9017.         }
  9018.         debug(F110,"ssl_auth","[SSL - OK]",0);
  9019.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  9020.     }
  9021.     if (ssl_debug_flag) {
  9022.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  9023.         fflush(stderr);
  9024.     }
  9025.     return(ssl_ftp_active_flag);
  9026. }
  9027. #endif /* CK_SSL */
  9028.  
  9029. static sigtype
  9030. cmdcancel(sig) int sig; {
  9031.     printf("^C...\n");
  9032.     fflush(stdout);
  9033.     cancelfile++;
  9034. #ifndef OS2
  9035.     if (ptflag)                         /* proxy... */
  9036.       longjmp(ptcancel,1);
  9037. #endif /* OS2 */
  9038. }
  9039.  
  9040. static int
  9041. #ifdef CK_ANSIC
  9042. scommand(char * s)                      /* Was secure_command() */
  9043. #else
  9044. scommand(s) char * s;
  9045. #endif /* CK_ANSIC */
  9046. {
  9047.     int length = 0, len2;
  9048.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  9049. #ifdef CK_SSL
  9050.     if (ssl_ftp_active_flag) {
  9051.         int error, rc;
  9052.         length = strlen(s) + 2;
  9053.         length = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  9054.         rc = SSL_write(ssl_ftp_con,out,length);
  9055.         error = SSL_get_error(ssl_ftp_con,rc);
  9056.         switch (error) {
  9057.           case SSL_ERROR_NONE:
  9058.             return(1);
  9059.           case SSL_ERROR_WANT_WRITE:
  9060.           case SSL_ERROR_WANT_READ:
  9061.           case SSL_ERROR_SYSCALL:
  9062. #ifdef NT
  9063.             {
  9064.                 int gle = GetLastError();
  9065.             }
  9066. #endif /* NT */
  9067.           case SSL_ERROR_WANT_X509_LOOKUP:
  9068.           case SSL_ERROR_SSL:
  9069.           case SSL_ERROR_ZERO_RETURN:
  9070.           default:
  9071.             lostpeer();
  9072.         }
  9073.         return(0);
  9074.     }
  9075. #endif /* CK_SSL */
  9076.  
  9077.     if (auth_type && ftp_cpl != FPL_CLR) {
  9078. #ifdef FTP_SRP
  9079.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  9080.           if ((length = srp_encode(ftp_cpl == FPL_PRV,
  9081.                                    (CHAR *)s,
  9082.                                    (CHAR *)out,
  9083.                                    strlen(s))) < 0) {
  9084.               fprintf(stderr, "SRP failed to encode message\n");
  9085.               return(0);
  9086.           }
  9087. #endif /* FTP_SRP */
  9088. #ifdef FTP_KRB4
  9089.         if (ck_krb4_is_installed() &&
  9090.             (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  9091.             if (ftp_cpl == FPL_PRV) {
  9092.                 length =
  9093.                   krb_mk_priv((CHAR *)s, (CHAR *)out,
  9094.                               strlen(s), ftp_sched,
  9095. #ifdef KRB524
  9096.                               ftp_cred.session,
  9097. #else /* KRB524 */
  9098.                               &ftp_cred.session,
  9099. #endif /* KRB524 */
  9100.                               &myctladdr, &hisctladdr);
  9101.             } else {
  9102.                 length =
  9103.                   krb_mk_safe((CHAR *)s,
  9104.                               (CHAR *)out,
  9105.                               strlen(s),
  9106. #ifdef KRB524
  9107.                               ftp_cred.session,
  9108. #else /* KRB524 */
  9109.                               &ftp_cred.session,
  9110. #endif /* KRB524 */
  9111.                               &myctladdr, &hisctladdr);
  9112.             }
  9113.             if (length == -1) {
  9114.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  9115.                         ftp_cpl == FPL_PRV ? "priv" : "safe");
  9116.                 return(0);
  9117.             }
  9118.         }
  9119. #endif /* FTP_KRB4 */
  9120. #ifdef FTP_GSSAPI
  9121.         /* Scommand (based on level) */
  9122.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  9123.             gss_buffer_desc in_buf, out_buf;
  9124.             OM_uint32 maj_stat, min_stat;
  9125.             int conf_state;
  9126.             in_buf.value = s;
  9127.             in_buf.length = strlen(s) + 1;
  9128.             maj_stat = gss_seal(&min_stat, gcontext,
  9129.                                 (ftp_cpl==FPL_PRV), /* private */
  9130.                                 GSS_C_QOP_DEFAULT,
  9131.                                 &in_buf, &conf_state,
  9132.                                 &out_buf);
  9133.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  9134.                 user_gss_error(maj_stat, min_stat,
  9135.                                (ftp_cpl==FPL_PRV)?
  9136.                                "gss_seal ENC didn't complete":
  9137.                                "gss_seal MIC didn't complete");
  9138.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  9139.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  9140.             } else {
  9141.                 if (ftp_deb)
  9142.                   fprintf(stderr, "sealed (%s) %d bytes\n",
  9143.                           ftp_cpl==FPL_PRV?"ENC":"MIC",
  9144.                           out_buf.length);
  9145.                 memcpy(out, out_buf.value,
  9146.                        length=out_buf.length);
  9147.                 gss_release_buffer(&min_stat, &out_buf);
  9148.             }
  9149.         }
  9150. #endif /* FTP_GSSAPI */
  9151.         /* Other auth types go here ... */
  9152.  
  9153.         len2 = FTP_BUFSIZ;
  9154.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  9155.                                    length, &len2, RADIX_ENCODE))
  9156.             ) {
  9157.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  9158.                     radix_error(kerror));
  9159.             return(0);
  9160.         }
  9161.         if (ftp_deb)
  9162.           fprintf(stderr, "scommand(%s)\nencoding %d bytes\n",
  9163.                   s, length);
  9164.         len2 = ckmakmsg(out,sizeof(out),ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  9165.                         in, "\r\n", NULL);
  9166.         send(csocket,(SENDARG2TYPE)out,len2,0);
  9167.     } else {
  9168.         char out[FTP_BUFSIZ];
  9169.         int len = ckmakmsg(out,sizeof(out),s,"\r\n",NULL,NULL);
  9170.         send(csocket,(SENDARG2TYPE)out,len,0);
  9171.     }
  9172.     return(1);
  9173. }
  9174.  
  9175. static int
  9176. mygetc() {
  9177.     static char inbuf[4096];
  9178.     static int bp = 0, ep = 0;
  9179.     int rc;
  9180.  
  9181.     if (bp == ep) {
  9182.         bp = ep = 0;
  9183. #ifdef CK_SSL
  9184.         if (ssl_ftp_active_flag) {
  9185.             int error;
  9186.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  9187.             error = SSL_get_error(ssl_ftp_con,rc);
  9188.             switch (error) {
  9189.               case SSL_ERROR_NONE:
  9190.                 break;
  9191.               case SSL_ERROR_WANT_WRITE:
  9192.               case SSL_ERROR_WANT_READ:
  9193.                 return(0);
  9194.               case SSL_ERROR_SYSCALL:
  9195.                 if (rc == 0) {          /* EOF */
  9196.                     break;
  9197.                 } else {
  9198. #ifdef NT
  9199.                     int gle = GetLastError();
  9200. #endif /* NT */
  9201.                     break;
  9202.                 }
  9203.               case SSL_ERROR_WANT_X509_LOOKUP:
  9204.               case SSL_ERROR_SSL:
  9205.               case SSL_ERROR_ZERO_RETURN:
  9206.               default:
  9207.                 break;
  9208.             }
  9209.         } else
  9210. #endif /* CK_SSL */
  9211.           rc = recv(csocket,(char *)inbuf,4096,0);
  9212.         if (rc <= 0)
  9213.           return(EOF);
  9214.         ep = rc;
  9215.     }
  9216.     return(inbuf[bp++]);
  9217. }
  9218.  
  9219. /*  x l a t e c  --  Translate a character  */
  9220. /*
  9221.     Call with:
  9222.       fc    = Function code: 0 = translate, 1 = initialize.
  9223.       c     = Character (as int).
  9224.       incs  = Index of charset to translate from.
  9225.       outcs = Index of charset to translate to.
  9226.  
  9227.     Returns:
  9228.       0: OK
  9229.      -1: Error
  9230. */
  9231. static int
  9232. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  9233. #ifdef NOCSETS
  9234.     return(c);
  9235. #else
  9236.     static char buf[128];
  9237.     static int cx;
  9238.     int c0;
  9239.  
  9240.     if (fc == 1) {                      /* Initialize */
  9241.         cx = 0;                         /* Catch-up buffer write index */
  9242.         xgnbp = buf;                    /* Catch-up buffer read pointer */
  9243.         buf[0] = NUL;                   /* Buffer is empty */
  9244.         return(0);
  9245.     }
  9246.     if (cx >= 127) {                    /* Catch-up buffer full */
  9247.         debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  9248.         printf("?Translation buffer overflow\n");
  9249.         return(-1);
  9250.     }
  9251.     /* Add char to buffer. */
  9252.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  9253.  
  9254.     debug(F000,"xlatec buf",ckitoa(cx),c);
  9255.     buf[cx++] = c;
  9256.     buf[cx] = NUL;
  9257.  
  9258.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  9259.         if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)    /* (NULL was xprintc) */
  9260.           return(-1);
  9261.     }
  9262.     /* If we're caught up, reinitialize the buffer */
  9263.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  9264. #endif /* NOCSETS */
  9265. }
  9266.  
  9267. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  9268.  
  9269.  
  9270. static int
  9271. getreply(expecteof,lcs,rcs,vbm,auth) int expecteof, lcs, rcs, vbm, auth; {
  9272.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  9273.     register int i, c, n;
  9274.     register int dig;
  9275.     register char *cp;
  9276.     int xlate = 0;
  9277.     int originalcode = 0, continuation = 0;
  9278.     sig_t oldintr;
  9279.     int pflag = 0;
  9280.     char *pt = pasv;
  9281.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  9282.     int safe = 0;
  9283.  
  9284. #ifndef NOCSETS
  9285.     debug(F101,"ftp getreply lcs","",lcs);
  9286.     debug(F101,"ftp getreply rcs","",rcs);
  9287.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  9288.         xlate = 1;
  9289.         initxlate(rcs,lcs);
  9290.         xlatec(1,0,rcs,lcs);
  9291.     }
  9292. #endif /* NOCSETS */
  9293.  
  9294.     if (ftp_deb)                        /* DEBUG */
  9295.       vbm = 1;
  9296.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9297.       vbm = 0;
  9298.     else if (vbm < 0)                   /* VERBOSE */
  9299.       vbm = ftp_vbm;
  9300.  
  9301.     ibuf[0] = '\0';
  9302.     if (reply_parse)
  9303.       reply_ptr = reply_buf;
  9304.     oldintr = signal(SIGINT, cmdcancel);
  9305.     for (;;) {
  9306.         obuf[0] = '\0';
  9307.         dig = n = ftpcode = i = 0;
  9308.         cp = ftp_reply_str;
  9309.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  9310.             if (c == IAC) {             /* Handle telnet commands */
  9311.                 switch (c = mygetc()) {
  9312.                   case WILL:
  9313.                   case WONT:
  9314.                     c = mygetc();
  9315.                     obuf[0] = IAC;
  9316.                     obuf[1] = DONT;
  9317.                     obuf[2] = c;
  9318.                     obuf[3] = NUL;
  9319. #ifdef CK_SSL
  9320.                     if (ssl_ftp_active_flag) {
  9321.                         int error, rc;
  9322.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  9323.                         error = SSL_get_error(ssl_ftp_con,rc);
  9324.                         switch (error) {
  9325.                           case SSL_ERROR_NONE:
  9326.                             break;
  9327.                           case SSL_ERROR_WANT_WRITE:
  9328.                           case SSL_ERROR_WANT_READ:
  9329.                             return(0);
  9330.                           case SSL_ERROR_SYSCALL:
  9331.                             if (rc == 0) { /* EOF */
  9332.                                 break;
  9333.                             } else {
  9334. #ifdef NT
  9335.                                 int gle = GetLastError();
  9336. #endif /* NT */
  9337.                                 break;
  9338.                             }
  9339.                           case SSL_ERROR_WANT_X509_LOOKUP:
  9340.                           case SSL_ERROR_SSL:
  9341.                           case SSL_ERROR_ZERO_RETURN:
  9342.                           default:
  9343.                             break;
  9344.                         }
  9345.                     } else
  9346. #endif /* CK_SSL */
  9347.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  9348.                     break;
  9349.                   case DO:
  9350.                   case DONT:
  9351.                     c = mygetc();
  9352.                     obuf[0] = IAC;
  9353.                     obuf[1] = WONT;
  9354.                     obuf[2] = c;
  9355.                     obuf[3] = NUL;
  9356. #ifdef CK_SSL
  9357.                     if (ssl_ftp_active_flag) {
  9358.                         int error, rc;
  9359.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  9360.                         error = SSL_get_error(ssl_ftp_con,rc);
  9361.                         switch (error) {
  9362.                           case SSL_ERROR_NONE:
  9363.                             break;
  9364.                           case SSL_ERROR_WANT_WRITE:
  9365.                           case SSL_ERROR_WANT_READ:
  9366.                             return(0);
  9367.                           case SSL_ERROR_SYSCALL:
  9368.                             if (rc == 0) { /* EOF */
  9369.                                 break;
  9370.                             } else {
  9371. #ifdef NT
  9372.                                 int gle = GetLastError();
  9373. #endif /* NT */
  9374.                                 break;
  9375.                             }
  9376.                           case SSL_ERROR_WANT_X509_LOOKUP:
  9377.                           case SSL_ERROR_SSL:
  9378.                           case SSL_ERROR_ZERO_RETURN:
  9379.                           default:
  9380.                             break;
  9381.                         }
  9382.                     } else
  9383. #endif /* CK_SSL */
  9384.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  9385.                     break;
  9386.                   default:
  9387.                     break;
  9388.                 }
  9389.                 continue;
  9390.             }
  9391.             dig++;
  9392.             if (c == EOF) {
  9393.                 if (expecteof) {
  9394.                     signal(SIGINT,oldintr);
  9395.                     ftpcode = 221;
  9396.                     debug(F101,"ftp getreply EOF","",ftpcode);
  9397.                     return(0);
  9398.                 }
  9399.                 lostpeer();
  9400.                 if (!quiet) {
  9401.                     if (ftp_deb)
  9402.                       printf("421 ");
  9403.                     printf(
  9404.                       "Service not available, connection closed by server\n");
  9405.                     fflush(stdout);
  9406.                 }
  9407.                 ftpcode = 421;
  9408.                 debug(F101,"ftp getreply EOF","",ftpcode);
  9409.                 return(4);
  9410.             }
  9411.             if (n == 0)
  9412.               n = c;
  9413.             if (auth_type &&
  9414. #ifdef CK_SSL
  9415.                 !ssl_ftp_active_flag &&
  9416. #endif /* CK_SSL */
  9417.                 !ibuf[0] && (n == '6' || continuation)) {
  9418.                 if (c != '\r' && dig > 4)
  9419.                   obuf[i++] = c;
  9420.             } else {
  9421.                 if (auth_type &&
  9422. #ifdef CK_SSL
  9423.                     !ssl_ftp_active_flag &&
  9424. #endif /* CK_SSL */
  9425.                     !ibuf[0] && dig == 1 && vbm)
  9426.                   printf("Unauthenticated reply received from server:\n");
  9427.                 if (reply_parse) {
  9428.                     *reply_ptr++ = c;
  9429.                     *reply_ptr = NUL;
  9430.                 }
  9431.                 if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  9432.                     ftp_cmdlin < 2) {
  9433.                     if ((c != '\r') &&
  9434.                         (ftp_deb || ((vbm || (!auth && n == '5')) &&
  9435.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
  9436.                         )))))
  9437.                     {
  9438. #ifdef FTP_PROXY
  9439.                         if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  9440.                           printf("%s:",ftp_host);
  9441. #endif /* FTP_PROXY */
  9442.  
  9443.                         if (!quiet) {
  9444. #ifdef NOCSETS
  9445.                             printf("%c",c);
  9446. #else
  9447.                             if (xlate) {
  9448.                                 xlatec(0,c,rcs,lcs);
  9449.                             } else {
  9450.                                 printf("%c",c);
  9451.                             }
  9452. #endif /* NOCSETS */
  9453.                         }
  9454.                     }
  9455.                 }
  9456.             }
  9457.             if (auth_type &&
  9458. #ifdef CK_SSL
  9459.                 !ssl_ftp_active_flag &&
  9460. #endif /* CK_SSL */
  9461.                 !ibuf[0] && n != '6')
  9462.               continue;
  9463.             if (dig < 4 && isdigit(c))
  9464.               ftpcode = ftpcode * 10 + (c - '0');
  9465.             if (!pflag && ftpcode == 227)
  9466.               pflag = 1;
  9467.             if (dig > 4 && pflag == 1 && isdigit(c))
  9468.               pflag = 2;
  9469.             if (pflag == 2) {
  9470.                 if (c != '\r' && c != ')')
  9471.                   *pt++ = c;
  9472.                 else {
  9473.                     *pt = '\0';
  9474.                     pflag = 3;
  9475.                 }
  9476.             }
  9477.             if (dig == 4 && c == '-' && n != '6') {
  9478.                 if (continuation)
  9479.                   ftpcode = 0;
  9480.                 continuation++;
  9481.             }
  9482.             if (cp < &ftp_reply_str[sizeof(ftp_reply_str) - 1]) {
  9483.                 *cp++ = c;
  9484.                 *cp = NUL;
  9485.             }
  9486.         }
  9487.         if (deblog ||
  9488. #ifdef COMMENT
  9489. /*
  9490.   Sometimes we need to print the server reply.  printlines is nonzero for any
  9491.   command where the results are sent back on the control connection rather
  9492.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  9493.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  9494.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  9495.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  9496.   lines from UNIX.  Further experimentation needed with other servers.  Of
  9497.   course RFC959 is mute as to the format of the server reply.
  9498.  
  9499.   'printlines' is also true for PWD and BYE.
  9500. */
  9501.      (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  9502. #else
  9503. /* No, we can't be that clever -- it breaks other things like RPWD... */
  9504.             (printlines &&
  9505.              (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  9506. #endif /* COMMENT */
  9507.             ) {
  9508.             char * q = cp;
  9509.             char *r = ftp_reply_str;
  9510.             *q-- = NUL;                 /* NUL-terminate */
  9511.             while (*q < '!' && q > r)   /* Strip CR, etc */
  9512.               *q-- = NUL;
  9513.             if (!ftp_deb && printlines) { /* If printing */
  9514.                 if (ftpcode != 0)       /* strip ftpcode if any */
  9515.                   r += 4;
  9516. #ifdef NOCSETS
  9517.                 printf("%s\n",r);       /* and print */
  9518. #else
  9519.                 if (!xlate) {
  9520.                     printf("%s\n",r);
  9521.                 } else {
  9522.                     xgnbp = r;
  9523.                     while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  9524.                         if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {    /* (xprintc) */
  9525.                             return(-1);
  9526.                         }
  9527.                     }
  9528.                     printf("\n");
  9529.                 }
  9530. #endif /* NOCSETS */
  9531.             }
  9532.             debug(F111,"ftp reply",ftp_reply_str,ftpcode);
  9533.         }
  9534.         if (auth_type &&
  9535. #ifdef CK_SSL
  9536.             !ssl_ftp_active_flag &&
  9537. #endif /* CK_SSL */
  9538.             !ibuf[0] && n != '6')
  9539.           return(getreply(expecteof,lcs,rcs,vbm,auth));
  9540.         ibuf[0] = obuf[i] = '\0';
  9541.         if (ftpcode && n == '6')
  9542.           if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  9543.               printf("Unknown reply: %d %s\n", ftpcode, obuf);
  9544.               n = '5';
  9545.           } else safe = (ftpcode == 631);
  9546.         if (obuf[0]                     /* if there is a string to decode */
  9547. #ifdef CK_SSL
  9548.             && !ssl_ftp_active_flag     /* and not SSL/TLS */
  9549. #endif /* CK_SSL */
  9550.             ) {
  9551.             if (!auth_type) {
  9552.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  9553.                 n = '5';
  9554.             }
  9555. #ifndef CK_ENCRYPTION
  9556.             else if (ftpcode == 632) {
  9557.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  9558.                 n = '5';
  9559.             }
  9560. #endif /* CK_ENCRYPTION */
  9561. #ifdef NOCONFIDENTIAL
  9562.             else if (ftpcode == 633) {
  9563.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  9564.                 n = '5';
  9565.             }
  9566. #endif /* NOCONFIDENTIAL */
  9567.             else {
  9568.                 int len = FTP_BUFSIZ;
  9569.                 if ((kerror = radix_encode((CHAR *)obuf,
  9570.                                            (CHAR *)ibuf,
  9571.                                            0,
  9572.                                            &len,
  9573.                                            RADIX_DECODE))
  9574.                     ) {
  9575.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  9576.                            ftpcode, radix_error(kerror), obuf);
  9577.                     n = '5';
  9578.                 }
  9579. #ifdef FTP_SRP
  9580.                 else if (strcmp(auth_type, "SRP") == 0) {
  9581.                     int outlen;
  9582.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  9583.                                         (CHAR *) ibuf, len);
  9584.                     if (outlen < 0) {
  9585.                         printf("Warning: %d reply %s!\n",
  9586.                                ftpcode, safe ? "modified" : "garbled");
  9587.                         n = '5';
  9588.                     } else {
  9589.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  9590.                         if (ftp_deb)
  9591.                           printf("%c:", safe ? 'S' : 'P');
  9592.                         continue;
  9593.                     }
  9594.                 }
  9595. #endif /* FTP_SRP */
  9596. #ifdef FTP_KRB4
  9597.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  9598.                     if (safe) {
  9599.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  9600. #ifdef KRB524
  9601.                                              ftp_cred.session,
  9602. #else /* KRB524 */
  9603.                                              &ftp_cred.session,
  9604. #endif /* KRB524 */
  9605.                                              &hisctladdr,
  9606.                                              &myctladdr,
  9607.                                              &ftp_msg_data
  9608.                                              );
  9609.                     } else {
  9610.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  9611.                                              ftp_sched,
  9612. #ifdef KRB524
  9613.                                              ftp_cred.session,
  9614. #else /* KRB524 */
  9615.                                              &ftp_cred.session,
  9616. #endif /* KRB524 */
  9617.                                              &hisctladdr,
  9618.                                              &myctladdr,
  9619.                                              &ftp_msg_data
  9620.                                              );
  9621.                     }
  9622.                     if (kerror != KSUCCESS) {
  9623.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  9624.                                safe ? "modified" : "garbled",
  9625.                                safe ? "safe" : "priv",
  9626.                                krb_get_err_text(kerror));
  9627.                         n = '5';
  9628.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  9629.                         kerror = KFAILURE;
  9630.                         n = '5';
  9631.                         printf("reply data too large for buffer\n");
  9632.                     } else {
  9633.                         if (ftp_deb)
  9634.                           printf("%c:", safe ? 'S' : 'P');
  9635.                         memcpy(ibuf,ftp_msg_data.app_data,
  9636.                                ftp_msg_data.app_length);
  9637.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  9638.                                   FTP_BUFSIZ - ftp_msg_data.app_length);
  9639.                         continue;
  9640.                     }
  9641.                 }
  9642. #endif /* FTP_KRB4 */
  9643. #ifdef FTP_GSSAPI
  9644.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  9645.                     gss_buffer_desc xmit_buf, msg_buf;
  9646.                     OM_uint32 maj_stat, min_stat;
  9647.                     int conf_state;
  9648.                     xmit_buf.value = ibuf;
  9649.                     xmit_buf.length = len;
  9650.                     /* decrypt/verify the message */
  9651.                     conf_state = safe;
  9652.                     maj_stat = gss_unseal(&min_stat, gcontext,
  9653.                                           &xmit_buf, &msg_buf,
  9654.                                           &conf_state, NULL);
  9655.                     if (maj_stat != GSS_S_COMPLETE) {
  9656.                         user_gss_error(maj_stat, min_stat,
  9657.                                        "failed unsealing reply");
  9658.                         n = '5';
  9659.                     } else {
  9660.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  9661.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  9662.                                   FTP_BUFSIZ-msg_buf.length);
  9663.                         gss_release_buffer(&min_stat,&msg_buf);
  9664.                         if (ftp_deb)
  9665.                           printf("%c:", safe ? 'S' : 'P');
  9666.                         continue;
  9667.                     }
  9668.                 }
  9669. #endif /* FTP_GSSAPI */
  9670.                 /* Other auth types go here... */
  9671.             }
  9672.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  9673.                    !quiet && (vbm || (!auth && (n == '4' || n == '5')))) {
  9674. #ifdef NOCSETS
  9675.             printf("%c",c);
  9676. #else
  9677.             if (xlate) {
  9678.                 xlatec(0,c,rcs,lcs);
  9679.             } else {
  9680.                 printf("%c",c);
  9681.             }
  9682. #endif /* NOCSETS */
  9683.             fflush (stdout);
  9684.         }
  9685.         if (continuation && ftpcode != originalcode) {
  9686.             if (originalcode == 0)
  9687.               originalcode = ftpcode;
  9688.             continue;
  9689.         }
  9690.         *cp = '\0';
  9691.         if (n != '1')
  9692.           cpend = 0;
  9693.         signal(SIGINT,oldintr);
  9694.         if (ftpcode == 421 || originalcode == 421)
  9695.           lostpeer();
  9696.         if ((cancelfile != 0) &&
  9697. #ifndef ULTRIX3
  9698.             /* Ultrix 3.0 cc objects violently to this clause */
  9699.             (oldintr != cmdcancel) &&
  9700. #endif /* ULTRIX3 */
  9701.             (oldintr != SIG_IGN)) {
  9702.             if (oldintr)
  9703.               (*oldintr)(SIGINT);
  9704.         }
  9705.         if (reply_parse) {
  9706.             *reply_ptr = '\0';
  9707.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  9708.                 reply_parse = reply_ptr + strlen(reply_parse);
  9709.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  9710.                   *reply_ptr = '\0';
  9711.             } else
  9712.               reply_parse = reply_ptr;
  9713.         }
  9714.         while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
  9715.           *cp-- = NUL;
  9716.         debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  9717.         return(n - '0');
  9718.     } /* for (;;) */
  9719. }
  9720.  
  9721. #ifdef BSDSELECT
  9722. static int
  9723. #ifdef CK_ANSIC
  9724. empty(fd_set * mask, int sec)
  9725. #else
  9726. empty(mask, sec) fd_set * mask; int sec;
  9727. #endif /* CK_ANSIC */
  9728. {
  9729.     struct timeval t;
  9730.     t.tv_sec = (long) sec;
  9731.     t.tv_usec = 0L;
  9732.     debug(F100,"ftp empty calling select...","",0);
  9733. #ifdef INTSELECT
  9734.     x = select(32, (int *)mask, NULL, NULL, &t);
  9735. #else
  9736.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  9737. #endif /* INTSELECT */
  9738.     debug(F101,"ftp empty select","",x);
  9739.     return(x);
  9740. }
  9741. #else /* BSDSELECT */
  9742. #ifdef IBMSELECT
  9743. static int
  9744. empty(mask, cnt, sec) int * mask, sec;
  9745.                       int   cnt;
  9746. {
  9747.     return(select(mask,cnt,0,0,sec*1000));
  9748. }
  9749. #endif /* IBMSELECT */
  9750. #endif /* BSDSELECT */
  9751.  
  9752. static sigtype
  9753. cancelsend(sig) int sig; {
  9754.     cancelgroup++;
  9755.     cancelfile = 0;
  9756.     printf(" Canceled...\n");
  9757.     debug(F101,"ftp cancelsend","",sig);
  9758.     fflush(stdout);
  9759. #ifndef OS2
  9760.     longjmp(sendcancel, 1);
  9761. #endif /* OS2 */
  9762. }
  9763.  
  9764. static
  9765. VOID
  9766. #ifdef CK_ANSIC
  9767. secure_error(char *fmt, ...)
  9768. #else
  9769. /* VARARGS1 */
  9770. secure_error(fmt, p1, p2, p3, p4, p5)
  9771.    char *fmt; int p1, p2, p3, p4, p5;
  9772. #endif /* CK_ANSIC */
  9773. {
  9774. #ifdef CK_ANSIC
  9775.     va_list ap;
  9776.  
  9777.     va_start(ap, fmt);
  9778.     vfprintf(stderr, fmt, ap);
  9779.     va_end(ap);
  9780. #else
  9781.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  9782. #endif
  9783.     fprintf(stderr, "\n");
  9784. }
  9785.  
  9786. /*
  9787.  * Internal form of settype; changes current type in use with server
  9788.  * without changing our notion of the type for data transfers.
  9789.  * Used to change to and from ascii for listings.
  9790.  */
  9791. static VOID
  9792. changetype(newtype, show) int newtype, show; {
  9793.     int comret;
  9794.     char * s;
  9795.  
  9796.     if (newtype == curtype)
  9797.       return;
  9798.     switch (newtype) {
  9799.       case FTT_ASC:
  9800.         s = "A";
  9801.         break;
  9802.       case FTT_BIN:
  9803.         s = "I";
  9804.         break;
  9805.       case FTT_TEN:
  9806.         s = "L 8";
  9807.         break;
  9808.       default:
  9809.         s = "I";
  9810.         break;
  9811.     }
  9812.     comret = ftpcmd("TYPE",s,-1,-1,show);
  9813.     if (comret == REPLY_COMPLETE)
  9814.       curtype = newtype;
  9815. }
  9816.  
  9817. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  9818.  
  9819. static int
  9820. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  9821.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  9822. {
  9823.     ULONG start = 0, stop;
  9824.     register int c, d = 0;
  9825.     int n, t, x, notafile = 0;
  9826.     sig_t oldintr, oldintp;
  9827.     long bytes = 0;
  9828.     char *lmode, buf[FTP_BUFSIZ], *bufp;
  9829.     sigtype cancelsend();
  9830.     int reply;
  9831.  
  9832.     if (!remote) remote = "";           /* Check args */
  9833.     if (!*remote) remote = local;
  9834.     if (!local) local = "";
  9835.     if (!*local) return(-1);
  9836.     if (!cmd) cmd = "";
  9837.     if (!*cmd) cmd = "STOR";
  9838.  
  9839.     debug(F111,"ftp sendrequest restart",local,restart);
  9840.  
  9841.     dout = -1;
  9842.  
  9843. #ifdef FTP_PROXY
  9844.     if (proxy) {
  9845.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  9846.         return(0);
  9847.     }
  9848. #endif /* FTP_PROXY */
  9849.  
  9850.     nout = 0;                           /* Init output buffer count */
  9851.     bytes = 0;                          /* File input byte count */
  9852.     changetype(ftp_typ,0);              /* Change type for this file */
  9853.  
  9854.     oldintr = NULL;                     /* Set up interrupt handler */
  9855.     oldintp = NULL;
  9856.     lmode = "wb";
  9857.  
  9858.     /* Replace with calls to cc_execute() */
  9859.     if (setjmp(sendcancel)) {
  9860.         while (cpend) {
  9861.             reply = getreply(0,incs,outcs,ftp_vbm,0);
  9862.             debug(F111,"ftp sendrequest getreply","null command",reply);
  9863.         }
  9864.         if (data >= 0) {
  9865. #ifdef CK_SSL
  9866.             if (ssl_ftp_data_active_flag) {
  9867.                 SSL_shutdown(ssl_ftp_data_con);
  9868.                 SSL_free(ssl_ftp_data_con);
  9869.                 ssl_ftp_data_active_flag = 0;
  9870.                 ssl_ftp_data_con = NULL;
  9871.             }
  9872. #endif /* CK_SSL */
  9873. #ifdef TCPIPLIB
  9874.             socket_close(data);
  9875. #else /* TCPIPLIB */
  9876. #ifdef USE_SHUTDOWN
  9877.             shutdown(data, 1+1);
  9878. #endif /* USE_SHUTDOWN */
  9879.             close(data);
  9880. #endif /* TCPIPLIB */
  9881.             data = -1;
  9882.             globaldin = -1;
  9883.         }
  9884.         if (oldintr)
  9885.             signal(SIGINT,oldintr);
  9886. #ifdef SIGPIPE
  9887.         if (oldintp)
  9888.             signal(SIGPIPE,oldintp);
  9889. #endif /* SIGPIPE */
  9890.         ftpcode = -1;
  9891.         return(-1);
  9892.     }
  9893.     oldintr = signal(SIGINT, cancelsend);
  9894.  
  9895. #ifdef PIPESEND                         /* Use Kermit API for file i/o... */
  9896.     if (sndfilter) {
  9897.         char * p = NULL, * q;
  9898. #ifndef NOSPL
  9899.         n = CKMAXPATH;
  9900.         if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  9901.             q = p;
  9902.             debug(F110,"sendrequest pipesend filter",sndfilter,0);
  9903.             zzstring(sndfilter,&p,&n);
  9904.             debug(F111,"sendrequest pipename",q,n);
  9905.             if (n <= 0) {
  9906.                 printf("?Sorry, send filter + filename too long, %d max.\n",
  9907.                        CKMAXPATH
  9908.                        );
  9909.                 free(q);
  9910.                 return(-1);
  9911.             }
  9912.             ckstrncpy(filnam,q,CKMAXPATH+1);
  9913.             free(q);
  9914.             local = filnam;
  9915.         }
  9916. #endif /* NOSPL */
  9917.     }
  9918. #endif /* PIPESEND */
  9919.  
  9920.     notafile = sndarray || pipesend;
  9921.  
  9922. #ifdef PIPESEND
  9923.     if (sndfilter)                      /* If sending thru a filter */
  9924.       pipesend = 1;                     /* set this for open and i/o */
  9925. #endif /* PIPESEND */
  9926.  
  9927.     if (openi(local) == 0)              /* Try to open the input file */
  9928.       return(-1);
  9929.  
  9930.     if (initconn()) {
  9931. #ifndef NOHTTP
  9932.         int y = -1;
  9933.         debug(F101,"ftp send_request","tcp_http_proxy",tcp_http_proxy);
  9934.  
  9935.        /*  If the connection failed and we are using an HTTP Proxy
  9936.         *  and the reason for the failure was an authentication
  9937.         *  error, then we need to give the user to ability to
  9938.         *  enter a username and password, just like a browser.
  9939.         *
  9940.         *  I tried to do all of this within the netopen() call
  9941.         *  but it is much too much work.
  9942.         */
  9943.         while (y != 0 && tcp_http_proxy != NULL ) {
  9944.  
  9945.             if (tcp_http_proxy_errno == 401 ||
  9946.                  tcp_http_proxy_errno == 407 ) {
  9947.                 char uid[UIDBUFLEN];
  9948.                 char pwd[256];
  9949.  
  9950.                 readtext("Proxy Userid: ",uid,UIDBUFLEN);
  9951.                 if (uid[0]) {
  9952.                     char * proxy_user, * proxy_pwd;
  9953.  
  9954.                     readpass("Proxy Password: ",pwd,256);
  9955.  
  9956.                     proxy_user = tcp_http_proxy_user;
  9957.                     proxy_pwd  = tcp_http_proxy_pwd;
  9958.  
  9959.                     tcp_http_proxy_user = uid;
  9960.                     tcp_http_proxy_pwd = pwd;
  9961.  
  9962.                     y = initconn();
  9963.  
  9964.                     debug(F101,"ftp send_request()","y",y);
  9965.                     memset(pwd,0,sizeof(pwd));
  9966.                     tcp_http_proxy_user = proxy_user;
  9967.                     tcp_http_proxy_pwd = proxy_pwd;
  9968.                 } else
  9969.                     break;
  9970.             } else
  9971.                 break;
  9972.         }
  9973.  
  9974.         if ( y != 0 ) {
  9975. #endif /* NOHTTP */
  9976.             signal(SIGINT, oldintr);
  9977. #ifdef SIGPIPE
  9978.             if (oldintp)
  9979.               signal(SIGPIPE, oldintp);
  9980. #endif /* SIGPIPE */
  9981.             ftpcode = -1;
  9982.             zclose(ZIFILE);
  9983.             return(-1);
  9984. #ifndef NOHTTP
  9985.         }
  9986. #endif /* NOHTTP */
  9987.     }
  9988.  
  9989.     /* Replace with calls to cc_execute() */
  9990.     if (setjmp(sendcancel))
  9991.       goto cancel;
  9992.  
  9993. #ifdef FTP_RESTART
  9994.     if (restart && ((curtype == FTT_BIN) || (alike > 0))) {
  9995.         char * p;
  9996.         changetype(FTT_BIN,0);          /* Change to binary */
  9997.  
  9998.         /* Ask for remote file's size */
  9999.         x = ftpcmd("SIZE",remote,incs,outcs,ftp_vbm);
  10000.  
  10001.         if (x == REPLY_COMPLETE) {      /* Have reply */
  10002.             p = &ftp_reply_str[4];      /* Parse it */
  10003.             while (isdigit(*p)) {
  10004.                 sendstart = sendstart * 10 + (int)(*p - '0');
  10005.                 p++;
  10006.             }
  10007.             if (*p && *p != CR) {       /* Bad number */
  10008.                 debug(F110,"ftp sendrequest bad size",ftp_reply_str,0);
  10009.                 sendstart = 0L;
  10010.             } else if (sendstart > fsize) { /* Remote file bigger than local */
  10011.                 debug(F110,"ftp sendrequest big size",ckltoa(fsize),sendstart);
  10012.                 sendstart = 0L;
  10013.             }
  10014.             debug(F111,"ftp sendrequest size",remote,sendstart);
  10015.             if (chkmodtime(local,remote,0) == 2) { /* Local is newer */
  10016.                 debug(F110,"ftp sendrequest date mismatch",ftp_reply_str,0);
  10017.                 sendstart = 0L;         /* Send the whole file */
  10018.             }
  10019.         }
  10020.         changetype(ftp_typ,0);          /* Change back to appropriate type */
  10021.         if (sendstart > 0L) {           /* Still restarting? */
  10022.             if (sendstart == fsize) {   /* Same size - no need to send */
  10023.                 debug(F111,"ftp sendrequest /restart SKIP",fsize,sendstart);
  10024.                 zclose(ZIFILE);
  10025.                 return(SKP_RES);
  10026.             }
  10027.             errno = 0;                  /* Restart needed, seek to the spot */
  10028.             if (zfseek((long)sendstart) < 0) {
  10029.                 debug(F111,"ftp sendrequest zfseek fails",local,sendstart);
  10030.                 fprintf(stderr, "FSEEK: %s: %s\n", local, ck_errstr());
  10031.                 sendstart = 0;
  10032.                 zclose(ZIFILE);
  10033.                 return(-1);
  10034.             }
  10035. #ifdef COMMENT
  10036.             debug(F111,"ftp sendrequest zfseek ok",local,sendstart);
  10037.             x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  10038.             if (x != REPLY_CONTINUE) {
  10039.                 sendstart = 0;
  10040.                 zclose(ZIFILE);
  10041.                 return(-1);
  10042.             } else {
  10043.                 cmd = "STOR";
  10044.             }
  10045. #else
  10046.             sendmode = SM_RESEND;
  10047.             cmd = "APPE";
  10048. #endif /* COMMENT */
  10049.             /* sendstart = 0L; */
  10050.         }
  10051.     }
  10052. #endif /* FTP_RESTART */
  10053.  
  10054.     x = ftpcmd(cmd,remote,incs,outcs,ftp_vbm);
  10055.     if (x != REPLY_PRELIM) {
  10056.         signal(SIGINT, oldintr);
  10057. #ifdef SIGPIPE
  10058.         if (oldintp)
  10059.           signal(SIGPIPE, oldintp);
  10060. #endif /* SIGPIPE */
  10061.         zclose(ZIFILE);
  10062. #ifdef PIPESEND
  10063.         if (sndfilter)
  10064.           pipesend = 0;
  10065. #endif /* PIPESEND */
  10066.         return(-1);
  10067.     }
  10068.     dout = dataconn(lmode);             /* Get data connection */
  10069.     if (dout == -1)
  10070.       goto cancel;
  10071.  
  10072.     /* Initialize per-file stats */
  10073.  
  10074.     ffc = 0L;                           /* Character counter */
  10075.     cps = oldcps = 0L;                  /* Thruput */
  10076.     start = gmstimer();                 /* Start time (msecs) */
  10077. #ifdef GFTIMER
  10078.     rftimer();                          /* reset f.p. timer */
  10079. #endif /* GFTIMER */
  10080.  
  10081. #ifdef SIGPIPE
  10082.     oldintp = signal(SIGPIPE, SIG_IGN);
  10083. #endif /* SIGPIPE */
  10084.     switch (curtype) {
  10085.       case FTT_BIN:                     /* Binary mode */
  10086.       case FTT_TEN:
  10087.         errno = d = 0;
  10088.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ)) > 0 && !cancelfile) {
  10089.             bytes += n;
  10090.             ffc += n;
  10091.             debug(F111,"ftp sendrequest zxin",ckltoa(n),ffc);
  10092.             hexdump("ftp sendrequest zxin",buf,16);
  10093. #ifdef CK_SSL
  10094.             if (ssl_ftp_data_active_flag) {
  10095.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  10096.                     if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  10097.                       break;
  10098.                     spackets++;
  10099.                     pktnum++;
  10100.                     if (fdispla != XYFD_B) {
  10101.                         spktl = d;
  10102.                         ftscreen(SCR_PT,'D',spackets,NULL);
  10103.                     }
  10104.                 }
  10105.             } else {
  10106. #endif /* CK_SSL */
  10107.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  10108.                     if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  10109.                         || iscanceled())
  10110.                       break;
  10111.                     spackets++;
  10112.                     pktnum++;
  10113.                     if (fdispla != XYFD_B) {
  10114.                         spktl = d;
  10115.                         ftscreen(SCR_PT,'D',spackets,NULL);
  10116.                     }
  10117.                 }
  10118. #ifdef CK_SSL
  10119.             }
  10120. #endif /* CK_SSL */
  10121.             if (d <= 0)
  10122.               break;
  10123.         }
  10124.         if (n < 0)
  10125.           fprintf(stderr, "local: %s: %s\n", local, ck_errstr());
  10126.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  10127.             if (d == -1 && errno && errno != EPIPE)
  10128.               perror("netout");
  10129.             bytes = -1;
  10130.         }
  10131.         break;
  10132.  
  10133.       case FTT_ASC:                     /* Text mode */
  10134. #ifndef NOCSETS
  10135.         if (xlate) {                    /* With translation */
  10136.             initxlate(incs,outcs);
  10137.             while (!cancelfile) {
  10138.                 c0 = xgnbyte(FC_UCS2,incs,NULL);
  10139.                 if (c0 < 0)             /* EOF */
  10140.                   break;
  10141.                 c1 = xgnbyte(FC_UCS2,incs,NULL); /* Convert to UCS-2 */
  10142.                 if (c1 < 0)             /* EOF */
  10143.                   break;
  10144.                 if (fileorder > 0) {    /* Little Endian */
  10145.                     t = c1;             /* so swap them */
  10146.                     c1 = c0;
  10147.                     c0 = t;
  10148.                 }
  10149.                 if ((x = xpnbyte(c0,TC_UCS2,outcs,xxout)) < 0) break;
  10150.                 if ((x = xpnbyte(c1,TC_UCS2,outcs,xxout)) < 0) break;
  10151.             }
  10152.         } else {
  10153. #endif /* NOCSETS */
  10154.             /* Text mode, no translation */
  10155.             while (((c = zminchar()) > -1) && !cancelfile) {
  10156.                 ffc++;
  10157.                 if (c == '\012') {
  10158.                     if (zzout(dout,(CHAR)'\015') < 0)
  10159.                       break;
  10160.                     bytes++;
  10161.                 }
  10162.                 if (zzout(dout,(CHAR)c) < 0)
  10163.                   break;
  10164.                 bytes++;
  10165.             }
  10166.             d = 0;
  10167. #ifndef NOCSETS
  10168.         }
  10169. #endif /* NOCSETS */
  10170.         if (dout == -1 || (d = secure_flush(dout)) < 0) {
  10171.             if (d == -1 && errno && errno != EPIPE)
  10172.               perror("netout");
  10173.             bytes = -1;
  10174.         }
  10175.         break;
  10176.     }
  10177.     tfc += ffc;                         /* Total file chars */
  10178.     stop = gmstimer();                  /* Timing stats */
  10179. #ifdef GFTIMER
  10180.     fpfsecs = gftimer();
  10181. #endif /* GFTIMER */
  10182.     zclose(ZIFILE);                     /* Close input file */
  10183. #ifdef PIPESEND
  10184.     if (sndfilter)                      /* Undo this (it's per file) */
  10185.       pipesend = 0;
  10186. #endif /* PIPESEND */
  10187.  
  10188. #ifdef TCPIPLIB
  10189.     socket_close(dout);                 /* Close data connection */
  10190. #else /* TCPIPLIB */
  10191. #ifdef USE_SHUTDOWN
  10192.     shutdown(dout, 1+1);
  10193. #endif /* USE_SHUTDOWN */
  10194.     close(dout);
  10195. #endif /* TCPIPLIB */
  10196.     reply = getreply(0,incs,outcs,ftp_vbm,0);
  10197.     signal(SIGINT, oldintr);            /* Put back interrupts */
  10198. #ifdef SIGPIPE
  10199.     if (oldintp)
  10200.       signal(SIGPIPE, oldintp);
  10201. #endif /* SIGPIPE */
  10202.     if (reply == REPLY_TRANSIENT || reply == REPLY_ERROR) {
  10203.         debug(F101,"ftp sendrequest reply","",reply);
  10204.         return(-1);
  10205.     } else if (cancelfile) {
  10206.         debug(F101,"ftp sendrequest canceled","",bytes);
  10207.         return(-1);
  10208.     } else {
  10209.         debug(F101,"ftp sendrequest ok","",bytes);
  10210.         return(0);
  10211.     }
  10212.  
  10213.   cancel:
  10214.     debug(F101,"ftp sendrequest canceled","",bytes);
  10215.     tfc += ffc;
  10216.     stop = gmstimer();
  10217. #ifdef GFTIMER
  10218.     fpfsecs = gftimer();
  10219. #endif /* GFTIMER */
  10220.     zclose(ZIFILE);
  10221. #ifdef PIPESEND
  10222.     if (sndfilter)
  10223.       pipesend = 0;
  10224. #endif /* PIPESEND */
  10225.     signal(SIGINT, oldintr);
  10226. #ifdef SIGPIPE
  10227.     if (oldintp)
  10228.       signal(SIGPIPE, oldintp);
  10229. #endif /* SIGPIPE */
  10230.     if (!cpend) {
  10231.         ftpcode = -1;
  10232.         return(-1);
  10233.     }
  10234.     if (data >= 0) {
  10235. #ifdef CK_SSL
  10236.         if (ssl_ftp_data_active_flag) {
  10237.             SSL_shutdown(ssl_ftp_data_con);
  10238.             SSL_free(ssl_ftp_data_con);
  10239.             ssl_ftp_data_active_flag = 0;
  10240.             ssl_ftp_data_con = NULL;
  10241.         }
  10242. #endif /* CK_SSL */
  10243. #ifdef TCPIPLIB
  10244.         socket_close(data);
  10245. #else /* TCPIPLIB */
  10246. #ifdef USE_SHUTDOWN
  10247.         shutdown(data, 1+1);
  10248. #endif /* USE_SHUTDOWN */
  10249.         close(data);
  10250. #endif /* TCPIPLIB */
  10251.         data = -1;
  10252.         globaldin = -1;
  10253.     }
  10254.     if (dout) {
  10255. #ifdef TCPIPLIB
  10256.         socket_close(dout);
  10257. #else /* TCPIPLIB */
  10258. #ifdef USE_SHUTDOWN
  10259.         shutdown(dout, 1+1);
  10260. #endif /* USE_SHUTDOWN */
  10261.         close(dout);
  10262. #endif /* TCPIPLIB */
  10263.     }
  10264.     reply = getreply(0,incs,outcs,ftp_vbm,0);
  10265.     ftpcode = -1;
  10266.     return(-1);
  10267. }
  10268.  
  10269. static sigtype
  10270. cancelrecv(sig) int sig; {
  10271.     cancelfile = 0;
  10272.     printf(" Canceling...\n");
  10273.     fflush(stdout);
  10274.     if (fp_nml) {
  10275.         if (fp_nml != stdout)
  10276.           fclose(fp_nml);
  10277.         fp_nml = NULL;
  10278.     }
  10279. #ifndef OS2
  10280.     longjmp(recvcancel, 1);
  10281. #endif /* OS2 */
  10282. }
  10283.  
  10284. /* Argumentless front-end for secure_getc() */
  10285.  
  10286. static int
  10287. netgetc() {
  10288.     return(secure_getc(globaldin));
  10289. }
  10290.  
  10291. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  10292.  
  10293. /*
  10294.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  10295.     -1: interrupted by user.
  10296.     -2: error opening or writing output file (reason in errno).
  10297.     -3: failure to make data connection.
  10298.     -4: network read error (reason in errno).
  10299. */
  10300. static int
  10301. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  10302.             xlate, fcs, rcs)
  10303.     char *cmd, *local, *remote, *lmode, *pipename;
  10304.     int printnames, recover, xlate, fcs, rcs;
  10305. {
  10306.     int din = -1;
  10307.     sig_t oldintr, oldintp;
  10308.     int is_retr, tcrflag, bare_lfs = 0, reply;
  10309.     int blksize = 0;
  10310.     long bytes = 0L;
  10311.     long localsize = 0L;
  10312.     register int c, d;
  10313.     ULONG start = 0L, stop;
  10314.     sigtype cancelrecv();
  10315. #ifdef NT
  10316.     struct _stat stbuf;
  10317. #else /* NT */
  10318.     struct stat stbuf;
  10319. #endif /* NT */
  10320.     char * p;
  10321.     static char * rcvbuf = NULL;
  10322.     static int rcvbufsiz = 0;
  10323.  
  10324. #ifdef DEBUG
  10325.     if (deblog) {
  10326.         debug(F111,"ftp recvrequest cmd",cmd,recover);
  10327.         debug(F110,"ftp recvrequest local ",local,0);
  10328.         debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  10329.         debug(F110,"ftp recvrequest pipename ",pipename,0);
  10330.         debug(F101,"ftp recvrequest xlate","",xlate);
  10331.         debug(F101,"ftp recvrequest fcs","",fcs);
  10332.         debug(F101,"ftp recvrequest rcs","",rcs);
  10333.     }
  10334. #endif /* DEBUG */
  10335.     if (remfile) {                      /* See remcfm(), remtxt() */
  10336.         if (rempipe) {
  10337.             pipename = remdest;
  10338.         } else {
  10339.             local = remdest;
  10340.             if (remappd) lmode = "ab";
  10341.         }
  10342.     }
  10343.     out2screen = 0;
  10344.     if (!cmd) cmd = "";                 /* Core dump prevention */
  10345.     if (!remote) remote = "";
  10346.     if (!lmode) lmode = "";
  10347.  
  10348.     if (pipename) {                     /* No recovery for pipes. */
  10349.         recover = 0;
  10350.         if (!local)
  10351.           local = pipename;
  10352.     } else {
  10353.         if (!local)                     /* Output to screen? */
  10354.           local = "-";
  10355.         out2screen = !strcmp(local,"-");
  10356.     }
  10357.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  10358.  
  10359. #ifdef OS2
  10360.     if ( ftp_xla && out2screen && !k95stdout && !inserver )
  10361.         fcs = FC_UCS2;
  10362. #endif /* OS2 */
  10363.  
  10364.     if (out2screen)                     /* No recovery to screen */
  10365.       recover = 0;
  10366.     if (!ftp_typ)                       /* No recovery in text mode */
  10367.       recover = 0;
  10368.     is_retr = (strcmp(cmd, "RETR") == 0);
  10369.     if (!is_retr)                       /* No recovery except for RETRieve */
  10370.       recover = 0;
  10371.  
  10372.     localsize = 0L;                     /* Local file size */
  10373.     rs_len = 0L;                        /* Recovery point */
  10374.  
  10375.     if (recover) {                      /* Recovering... */
  10376.         if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
  10377.             recover = 0;                /* So cancel recovery */
  10378.         } else {                        /* Have local file info */
  10379.             localsize = stbuf.st_size;  /* Get size */
  10380.             if (fsize < localsize) {    /* Remote file smaller than local */
  10381.                 recover = 0;            /* Recovery can't work */
  10382.             } else if (fsize == localsize) { /* Sizes are equal */
  10383.                 debug(F111,"ftp recvrequest skipping",remote,localsize);
  10384.                 return(1);
  10385.             }
  10386.             if (recover) {              /* Remote is bigger */
  10387.                 x = chkmodtime(local,remote,0); /* It must also be older */
  10388.                 debug(F111,"ftp getfile chkmodtime",remote,x);
  10389.                 if (x < 2)
  10390.                   recover = 0;          /* If not, get whole file */
  10391.             }
  10392.         }
  10393.         debug(F111,"ftp recvrequest recover",remote,recover);
  10394.     }
  10395.  
  10396. #ifdef FTP_PROXY
  10397.     if (proxy && is_retr)
  10398.       return(proxtrans(cmd, local ? local : remote, remote));
  10399. #endif /* FTP_PROXY */
  10400.  
  10401.     oldintr = NULL;
  10402.     oldintp = NULL;
  10403.     tcrflag = (feol != CR) && is_retr;
  10404.     ftpcode = 0;
  10405.  
  10406.     /* Replace with calls to cc_execute() */
  10407.     if (setjmp(recvcancel)) {
  10408.         while (cpend) {
  10409.             reply = getreply(0,fcs,rcs,ftp_vbm,0);
  10410.         }
  10411.         if (data >= 0) {
  10412. #ifdef CK_SSL
  10413.             if (ssl_ftp_data_active_flag) {
  10414.                 SSL_shutdown(ssl_ftp_data_con);
  10415.                 SSL_free(ssl_ftp_data_con);
  10416.                 ssl_ftp_data_active_flag = 0;
  10417.                 ssl_ftp_data_con = NULL;
  10418.             }
  10419. #endif /* CK_SSL */
  10420. #ifdef TCPIPLIB
  10421.             socket_close(data);
  10422. #else /* TCPIPLIB */
  10423. #ifdef USE_SHUTDOWN
  10424.             shutdown(data, 1+1);
  10425. #endif /* USE_SHUTDOWN */
  10426.             close(data);
  10427. #endif /* TCPIPLIB */
  10428.             data = -1;
  10429.             globaldin = -1;
  10430.         }
  10431.         if (oldintr)
  10432.           signal(SIGINT, oldintr);
  10433.         ftpcode = -1;
  10434.         return(-1);
  10435.     }
  10436.     oldintr = signal(SIGINT, cancelrecv);
  10437.  
  10438.     if (!out2screen && !pipename) {
  10439.         if (zchko(local) < 0) {
  10440.             if ((!dpyactive || ftp_deb))
  10441.               fprintf(stderr,"Temporary file %s: %s\n", local, ck_errstr());
  10442.             signal(SIGINT, oldintr);
  10443.             ftpcode = -2;
  10444.             return(-1);
  10445.         }
  10446.     }
  10447.     changetype((!is_retr) ? FTT_ASC : ftp_typ, 0);
  10448.  
  10449.     if (initconn()) {                   /* Initialize the data connection */
  10450.         signal(SIGINT, oldintr);
  10451.         ftpcode = -1;
  10452.         return(-3);
  10453.     }
  10454.  
  10455.     /* Replace with calls to cc_execute() */
  10456.     if (setjmp(recvcancel))
  10457.       goto cancel;
  10458.  
  10459.     if (recover) {                      /* Initiate recovery */
  10460.         x = ftpcmd("REST",ckltoa(localsize),-1,-1,ftp_vbm);
  10461.         debug(F111,"ftp reply","REST",x);
  10462.         if (x == REPLY_CONTINUE) {
  10463.             lmode = "ab";
  10464.             rs_len = localsize;
  10465.         } else {
  10466.             recover = 0;
  10467.         }
  10468.     }
  10469.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  10470.  
  10471.     debug(F111,"ftp recvrequest recover E",remote,recover);
  10472.  
  10473.     /* Send the command and get reply */
  10474.     debug(F110,"ftp recvrequest cmd",cmd,0);
  10475.     debug(F110,"ftp recvrequest remote",remote,0);
  10476.  
  10477.     if (ftpcmd(cmd,remote,fcs,rcs,ftp_vbm) != REPLY_PRELIM) {
  10478.         signal(SIGINT, oldintr);        /* Bad reply, fail. */
  10479.         return(-1);                     /* ftpcode is set by ftpcmd() */
  10480.     }
  10481.     din = dataconn("r");                /* Good reply, open data connection */
  10482.     globaldin = din;                    /* Global copy of file descriptor */
  10483.     if (din == -1) {                    /* Check for failure */
  10484.         ftpcode = -3;                   /* Code for no data connection */
  10485.         goto cancel;
  10486.     }
  10487.     x = 1;                              /* Output file open OK? */
  10488.     if (pipename) {                     /* Command */
  10489.         x = zxcmd(ZOFILE,pipename);
  10490.         debug(F111,"ftp recvrequest zxcmd",pipename,x);
  10491.     } else if (!out2screen) {           /* File */
  10492.         struct filinfo xx;
  10493.         xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  10494.         xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  10495.         xx.dsp = !strcmp(lmode,"ab") ? XYFZ_A : XYFZ_N; /* Append or New */
  10496.         x = zopeno(ZOFILE,local,NULL,&xx);
  10497.         debug(F111,"ftp recvrequest zopeno",local,x);
  10498.     }
  10499.     if (x < 1) {                        /* Failure to open output file */
  10500.         if ((!dpyactive || ftp_deb))
  10501.           fprintf(stderr, "local(2): %s: %s\n", local, ck_errstr());
  10502.         goto cancel;
  10503.     }
  10504.     blksize = FTP_BUFSIZ;               /* Allocate input buffer */
  10505.     if (rcvbufsiz < blksize) {          /* if necessary */
  10506.         if (rcvbuf) {
  10507.             free(rcvbuf);
  10508.             rcvbuf = NULL;
  10509.         }
  10510.         rcvbuf = (char *)malloc((unsigned)blksize);
  10511.         if (!rcvbuf) {
  10512.             ftpcode = -2;
  10513. #ifdef ENOMEM
  10514.             errno = ENOMEM;
  10515. #endif /* ENOMEM */
  10516.             if ((!dpyactive || ftp_deb))
  10517.               perror("malloc");
  10518.             rcvbufsiz = 0;
  10519.             goto cancel;
  10520.         }
  10521.         rcvbufsiz = blksize;
  10522.     }
  10523.     debug(F111,"ftp get rcvbufsiz",local,rcvbufsiz);
  10524.  
  10525.     ffc = 0L;                           /* Character counter */
  10526.     cps = oldcps = 0L;                  /* Thruput */
  10527.     start = gmstimer();                 /* Start time (msecs) */
  10528. #ifdef GFTIMER
  10529.     rftimer();                          /* Start time (float) */
  10530. #endif /* GFTIMER */
  10531.  
  10532.     debug(F111,"ftp get type",local,curtype);
  10533.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  10534.     switch (curtype) {
  10535.       case FTT_BIN:                     /* Binary mode */
  10536.       case FTT_TEN:                     /* TENEX mode */
  10537.         d = 0;
  10538.         while (1) {
  10539.             errno = 0;
  10540.             c = secure_read(din, rcvbuf, rcvbufsiz);
  10541.             if (cancelfile)
  10542.               goto cancel;
  10543.             if (c < 1)
  10544.               break;
  10545. #ifdef printf                           /* (What if it isn't?) */
  10546.             if (out2screen && !pipename) {
  10547.                 int i;
  10548.                 for (i = 0; i < c; i++)
  10549.                   printf("%c",rcvbuf[i]);
  10550.             } else
  10551. #endif /* printf */
  10552.               {
  10553.                 register int i;
  10554.                 i = 0;
  10555.                 errno = 0;
  10556.                 while (i < c) {
  10557.                     if (zmchout(rcvbuf[i++]) < 0) {
  10558.                         d = i;
  10559.                         break;
  10560.                     }
  10561.                 }
  10562.             }
  10563.             bytes += c;
  10564.             ffc += c;
  10565.         }
  10566.         if (c < 0) {
  10567.             debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  10568.             if (c == -1 && errno != EPIPE)
  10569.               if ((!dpyactive || ftp_deb))
  10570.                 perror("netin");
  10571.             bytes = -1;
  10572.             ftpcode = -4;
  10573.         }
  10574.         if (d < c) {
  10575.             ftpcode = -2;
  10576.             if ((!dpyactive || ftp_deb)) {
  10577.                 char * p;
  10578.                 p = local ? local : pipename;
  10579.                 if (d < 0)
  10580.                   fprintf(stderr, "local(3): %s: %s\n", local, ck_errstr());
  10581.                 else
  10582.                   fprintf(stderr, "%s: short write\n", local);
  10583.             }
  10584.         }
  10585.         break;
  10586.  
  10587.       case FTT_ASC:                     /* Text mode */
  10588. #ifndef NOCSETS
  10589.         if (xlate) {
  10590.             int t;
  10591. #ifdef CK_ANSIC
  10592.             int (*fn)(char);
  10593. #else
  10594.             int (*fn)();
  10595. #endif /* CK_ANSIC */
  10596.             debug(F110,"ftp recvrequest (data)","initxlate",0);
  10597.             initxlate(rcs,fcs);         /* (From,To) */
  10598.             if (pipename) {
  10599.                 fn = pipeout;
  10600.                 debug(F110,"ftp recvrequest ASCII","pipeout",0);
  10601.             } else {
  10602.                 fn = out2screen ? scrnout : putfil;
  10603.                 debug(F110,"ftp recvrequest ASCII",
  10604.                       out2screen ? "scrnout" : "putfil",0);
  10605.             }
  10606.             while (1) {
  10607.                 c0 = xgnbyte(FC_UCS2,rcs,netgetc); /* Get byte from net */
  10608.                 if (cancelfile)
  10609.                   goto cancel;
  10610.                 if (c0 < 0)
  10611.                   break;
  10612.                 c1 = xgnbyte(FC_UCS2,rcs,netgetc); /* Second byte from net */
  10613.                 if (cancelfile)
  10614.                   goto cancel;
  10615.                 if (c1 < 0)
  10616.                   break;
  10617.                 if (fileorder > 0) {    /* Little Endian */
  10618.                     t = c1;             /* so swap them */
  10619.                     c1 = c0;
  10620.                     c0 = t;
  10621.                 }
  10622.  
  10623. #ifdef OS2
  10624.                 if ( out2screen &&            /* we're translating to UCS-2 */ 
  10625.                      !k95stdout && !inserver) /* for the real screen... */     
  10626.                 {
  10627.                     union {
  10628.                         USHORT ucs2;
  10629.                         UCHAR  bytes[2];
  10630.                     } output;
  10631.  
  10632.                     output.bytes[0] = c1;
  10633.                     output.bytes[1] = c0;
  10634.  
  10635.                     VscrnWrtUCS2StrAtt(VCMD,
  10636.                                        &output.ucs2,
  10637.                                        1,
  10638.                                        wherey[VCMD],
  10639.                                        wherex[VCMD],
  10640.                                        &colorcmd
  10641.                                        );
  10642.  
  10643.                 } else 
  10644. #endif /* OS2 */
  10645.                 {
  10646.                     if ((x = xpnbyte(c0,TC_UCS2,fcs,fn)) < 0) break;
  10647.                     if ((x = xpnbyte(c1,TC_UCS2,fcs,fn)) < 0) break;
  10648.                 }
  10649.             }
  10650.         } else {
  10651. #endif /* NOCSETS */
  10652.             while (1) {
  10653.                 c = secure_getc(din);
  10654.                 if (cancelfile)
  10655.                   goto cancel;
  10656.                 if (c < 0 || c == EOF)
  10657.                   break;
  10658.                 if (c == '\n')
  10659.                   bare_lfs++;
  10660.                 while (c == '\r') {
  10661.                     bytes++;
  10662.                     if ((c = secure_getc(din)) != '\n' || tcrflag) {
  10663.                         if (cancelfile)
  10664.                           goto cancel;
  10665.                         if (c < 0 || c == EOF)
  10666.                           goto break2;
  10667.                         if (c == '\0') {
  10668.                             bytes++;
  10669.                             goto contin2;
  10670.                         }
  10671.                     }
  10672.                 }
  10673.                 if (c < 0)
  10674.                   break;
  10675.                 if (out2screen && !pipename)
  10676. #ifdef printf
  10677.                   printf("%c",(char)c);
  10678. #else
  10679.                   putchar((char)c);
  10680. #endif /* printf */
  10681.                 else
  10682.                   if ((d = zmchout(c)) < 0)
  10683.                     break;
  10684.                 bytes++;
  10685.                 ffc++;
  10686.               contin2:
  10687.                 ;
  10688.             }
  10689.           break2:
  10690.             if (bare_lfs && (!dpyactive || ftp_deb)) {
  10691.                 printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  10692.                        bare_lfs);
  10693.                 printf("File might not have transferred correctly.\n");
  10694.             }
  10695.             if (din == -1) {
  10696.                 bytes = -1;
  10697.             }
  10698.             if (c == -2)
  10699.               bytes = -1;
  10700.             break;
  10701. #ifndef NOCSETS
  10702.         }
  10703. #endif /* NOCSETS */
  10704.     }
  10705.     if (pipename || !out2screen)
  10706.       zclose(ZOFILE);
  10707.  
  10708.     /* If ftpcode < 0 fail and if keep == 0 delete the partial file */
  10709.  
  10710.     signal(SIGINT, oldintr);
  10711. #ifdef SIGPIPE
  10712.     if (oldintp)
  10713.       signal(SIGPIPE, oldintp);
  10714. #endif /* SIGPIPE */
  10715.     stop = gmstimer();
  10716. #ifdef GFTIMER
  10717.     fpfsecs = gftimer();
  10718. #endif /* GFTIMER */
  10719.     tfc += ffc;
  10720.  
  10721. #ifdef TCPIPLIB
  10722.     socket_close(din);
  10723. #else /* TCPIPLIB */
  10724. #ifdef USE_SHUTDOWN
  10725.     shutdown(din, 1+1);
  10726. #endif /* USE_SHUTDOWN */
  10727.     close(din);
  10728. #endif /* TCPIPLIB */
  10729.     reply = getreply(0,fcs,rcs,ftp_vbm,0);
  10730.     return((ftpcode < 0 || reply == REPLY_TRANSIENT || 
  10731.             reply == REPLY_ERROR) ? -1 : 0);
  10732.  
  10733.   cancel:
  10734.  
  10735. /* Cancel using RFC959 recommended IP,SYNC sequence  */
  10736.  
  10737.     debug(F100,"ftp recvrequest CANCEL","",0);
  10738.     stop = gmstimer();
  10739. #ifdef GFTIMER
  10740.     fpfsecs = gftimer();
  10741. #endif /* GFTIMER */
  10742. #ifdef SIGPIPE
  10743.     if (oldintp)
  10744.       signal(SIGPIPE, oldintr);
  10745. #endif /* SIGPIPE */
  10746.     signal(SIGINT, SIG_IGN);
  10747.     if (!cpend) {
  10748.         ftpcode = -1;
  10749.         signal(SIGINT, oldintr);
  10750.         return(-1);
  10751.     }
  10752.     cancel_remote(din);
  10753.     if (ftpcode > -1)
  10754.       ftpcode = -1;
  10755.     if (data >= 0) {
  10756. #ifdef CK_SSL
  10757.         if (ssl_ftp_data_active_flag) {
  10758.             SSL_shutdown(ssl_ftp_data_con);
  10759.             SSL_free(ssl_ftp_data_con);
  10760.             ssl_ftp_data_active_flag = 0;
  10761.             ssl_ftp_data_con = NULL;
  10762.         }
  10763. #endif /* CK_SSL */
  10764. #ifdef TCPIPLIB
  10765.         socket_close(data);
  10766. #else /* TCPIPLIB */
  10767. #ifdef USE_SHUTDOWN
  10768.         shutdown(data, 1+1);
  10769. #endif /* USE_SHUTDOWN */
  10770.         close(data);
  10771. #endif /* TCPIPLIB */
  10772.         data = -1;
  10773.         globaldin = -1;
  10774.     }
  10775.     if (!out2screen)
  10776.       zclose(ZOFILE);
  10777.  
  10778.     if (din) {
  10779. #ifdef TCPIPLIB
  10780.         socket_close(din);
  10781. #else /* TCPIPLIB */
  10782. #ifdef USE_SHUTDOWN
  10783.         shutdown(din, 1+1);
  10784. #endif /* USE_SHUTDOWN */
  10785.         close(din);
  10786. #endif /* TCPIPLIB */
  10787.     }
  10788.     signal(SIGINT, oldintr);
  10789.     return(-1);
  10790. }
  10791.  
  10792. /*
  10793.  * Need to start a listen on the data channel before we send the command,
  10794.  * otherwise the server's connect may fail.
  10795.  */
  10796. static int
  10797. initconn() {
  10798.     register char *p, *a;
  10799.     int result, tmpno = 0;
  10800.     int on = 1;
  10801.     GSOCKNAME_T len;
  10802.  
  10803. #ifndef NO_PASSIVE_MODE
  10804.     int a1,a2,a3,a4,p1,p2;
  10805.  
  10806.     if (passivemode) {
  10807.         data = socket(AF_INET, SOCK_STREAM, 0);
  10808.         globaldin = data;
  10809.         if (data < 0) {
  10810.             perror("ftp: socket");
  10811.             return(-1);
  10812.         }
  10813.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  10814.             printf("Passive mode refused\n");
  10815.             passivemode = 0;
  10816.             return(initconn());
  10817.         }
  10818. /*
  10819.   Now we have a string of comma-separated one-byte unsigned integer values,
  10820.   The first four are the an IP address.  The fifth is the MSB of the port
  10821.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  10822. */
  10823.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  10824.             printf("Passive mode address scan failure\n");
  10825.             return(-1);
  10826.         };
  10827. #ifndef NOHTTP
  10828.         if (tcp_http_proxy) {
  10829. #ifdef OS2
  10830.             char * agent = "Kermit 95"; /* Default user agent */
  10831. #else
  10832.             char * agent = "C-Kermit";
  10833. #endif /* OS2 */
  10834.             register struct hostent *hp = 0;
  10835.             struct servent *destsp;
  10836.             char host[512], *p, *q;
  10837. #ifdef IP_TOS
  10838. #ifdef IPTOS_THROUGHPUT
  10839.             int tos;
  10840. #endif /* IPTOS_THROUGHPUT */
  10841. #endif /* IP_TOS */
  10842.             int s;
  10843. #ifdef DEBUG
  10844.             extern int debtim;
  10845.             int xdebtim;
  10846.             xdebtim = debtim;
  10847.             debtim = 1;
  10848. #endif /* DEBUG */
  10849.  
  10850.             ckmakxmsg(proxyhost,sizeof(proxyhost),ckuitoa(a1),".",ckuitoa(a2),
  10851.                       ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  10852.                       NULL,NULL,NULL
  10853.                       );
  10854.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  10855.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  10856.               *q = *p;
  10857.             *q = '\0';
  10858.  
  10859.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  10860.             if (hisctladdr.sin_addr.s_addr != -1) {
  10861.                 debug(F110,"initconn A",host,0);
  10862.                 hisctladdr.sin_family = AF_INET;
  10863.             } else {
  10864.                 debug(F110,"initconn B",host,0);
  10865.                 hp = gethostbyname(host);
  10866. #ifdef HADDRLIST
  10867.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  10868. #endif /* HADDRLIST */
  10869.                 if (hp == NULL) {
  10870.                     fprintf(stderr, "ftp: %s: ", host);
  10871.                     herror();
  10872.                     ftpcode = -1;
  10873. #ifdef DEBUG
  10874.                     debtim = xdebtim;
  10875. #endif /* DEBUG */
  10876.                     return(0);
  10877.                 }
  10878.                 hisctladdr.sin_family = hp->h_addrtype;
  10879. #ifdef HADDRLIST
  10880.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  10881.                        sizeof(hisctladdr.sin_addr));
  10882. #else /* HADDRLIST */
  10883.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  10884.                        sizeof(hisctladdr.sin_addr));
  10885. #endif /* HADDRLIST */
  10886.             }
  10887.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  10888.             debug(F101,"initconn socket","",data);
  10889.             if (data < 0) {
  10890.                 perror("ftp: socket");
  10891.                 ftpcode = -1;
  10892. #ifdef DEBUG
  10893.                 debtim = xdebtim;
  10894. #endif /* DEBUG */
  10895.                 return(0);
  10896.             }
  10897.             if (*p == ':')
  10898.               p++;
  10899.             else
  10900.               p = "http";
  10901.  
  10902.             destsp = getservbyname(p,"tcp");
  10903.             if (destsp)
  10904.               hisctladdr.sin_port = destsp->s_port;
  10905.             else if (p)
  10906.               hisctladdr.sin_port = htons(atoi(p));
  10907.             else
  10908.               hisctladdr.sin_port = htons(80);
  10909.             errno = 0;
  10910. #ifdef HADDRLIST
  10911.             debug(F100,"initconn HADDRLIST","",0);
  10912.             while
  10913. #else
  10914.             debug(F100,"initconn no HADDRLIST","",0);
  10915.             if
  10916. #endif /* HADDRLIST */
  10917.               (connect(data, (struct sockaddr *)&hisctladdr,
  10918.                        sizeof (hisctladdr)) < 0) {
  10919.                   debug(F101,"initconn connect failed","",errno);
  10920. #ifdef HADDRLIST
  10921.                   if (hp && hp->h_addr_list[1]) {
  10922.                       int oerrno = errno;
  10923.  
  10924.                       fprintf(stderr,
  10925.                               "ftp: connect to address %s: ",
  10926.                               inet_ntoa(hisctladdr.sin_addr)
  10927.                               );
  10928.                       errno = oerrno;
  10929.                       perror((char *)0);
  10930.                       hp->h_addr_list++;
  10931.                       memcpy((char *)&hisctladdr.sin_addr,
  10932.                              hp->h_addr_list[0],
  10933.                              sizeof(hisctladdr.sin_addr));
  10934.                       fprintf(stdout, "Trying %s...\n",
  10935.                               inet_ntoa(hisctladdr.sin_addr));
  10936. #ifdef TCPIPLIB
  10937.                       socket_close(data);
  10938. #else /* TCPIPLIB */
  10939.                       close(data);
  10940. #endif /* TCPIPLIB */
  10941.                       data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  10942.                       if (data < 0) {
  10943.                           perror("ftp: socket");
  10944.                           ftpcode = -1;
  10945. #ifdef DEBUG
  10946.                           debtim = xdebtim;
  10947. #endif /* DEBUG */
  10948.                           return(0);
  10949.                       }
  10950.                       continue;
  10951.                   }
  10952. #endif /* HADDRLIST */
  10953.                   perror("ftp: connect");
  10954.                   ftpcode = -1;
  10955.                   goto bad;
  10956.               }
  10957.             if (http_connect(data,agent,NULL,
  10958.                              tcp_http_proxy_user,
  10959.                              tcp_http_proxy_pwd,
  10960.                              0,
  10961.                              proxyhost
  10962.                              ) < 0) {
  10963. #ifdef TCPIPLIB
  10964.                 socket_close(data);
  10965. #else /* TCPIPLIB */
  10966.                 close(data);
  10967. #endif /* TCPIPLIB */
  10968.                 perror("ftp: connect");
  10969.                 ftpcode = -1;
  10970.                 goto bad;
  10971.             }
  10972.         } else
  10973. #endif /* NOHTTP */
  10974.         {
  10975.             data_addr.sin_family = AF_INET;
  10976.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  10977.             data_addr.sin_port = htons((p1<<8)|p2);
  10978.  
  10979.             if (connect(data,
  10980.                         (struct sockaddr *)&data_addr,
  10981.                         sizeof(data_addr)) < 0
  10982.                 ) {
  10983.                 perror("ftp: connect");
  10984.                 return(-1);
  10985.             }
  10986.         }
  10987.         debug(F100,"initconn connect ok","",0);
  10988. #ifdef IP_TOS
  10989. #ifdef IPTOS_THROUGHPUT
  10990.         on = IPTOS_THROUGHPUT;
  10991.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  10992.           perror("ftp: setsockopt TOS (ignored)");
  10993. #endif /* IPTOS_THROUGHPUT */
  10994. #endif /* IP_TOS */
  10995.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  10996.         return(0);
  10997.     }
  10998. #endif /* NO_PASSIVE_MODE */
  10999.  
  11000.   noport:
  11001.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  11002.     if (sendport)
  11003.       data_addr.sin_port = 0;   /* let system pick one */
  11004.     if (data != -1) {
  11005. #ifdef TCPIPLIB
  11006.         socket_close(data);
  11007. #else /* TCPIPLIB */
  11008. #ifdef USE_SHUTDOWN
  11009.         shutdown(data, 1+1);
  11010. #endif /* USE_SHUTDOWN */
  11011.         close(data);
  11012. #endif /* TCPIPLIB */
  11013.     }
  11014.     data = socket(AF_INET, SOCK_STREAM, 0);
  11015.     globaldin = data;
  11016.     if (data < 0) {
  11017.         perror("ftp: socket");
  11018.         if (tmpno)
  11019.           sendport = 1;
  11020.         return(-1);
  11021.     }
  11022.     if (!sendport) {
  11023.         if (setsockopt(data,
  11024.                        SOL_SOCKET,
  11025.                        SO_REUSEADDR,
  11026.                        (char *)&on,
  11027.                        sizeof (on)
  11028.                        ) < 0
  11029.             ) {
  11030.             perror("ftp: setsockopt (reuse address)");
  11031.             goto bad;
  11032.         }
  11033.     }
  11034.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  11035.         perror("ftp: bind");
  11036.         goto bad;
  11037.     }
  11038.     len = sizeof (data_addr);
  11039.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  11040.         perror("ftp: getsockname");
  11041.         goto bad;
  11042.     }
  11043.     if (listen(data, 1) < 0) {
  11044.         perror("ftp: listen");
  11045.         goto bad;
  11046.     }
  11047.     if (sendport) {
  11048.         a = (char *)&data_addr.sin_addr;
  11049.         p = (char *)&data_addr.sin_port;
  11050.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  11051.                   UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  11052.                   UC(p[0]),",", UC(p[1]));
  11053.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  11054.         if (result == REPLY_ERROR && sendport) {
  11055.             sendport = 0;
  11056.             tmpno = 1;
  11057.             goto noport;
  11058.         }
  11059.         return(result != REPLY_COMPLETE);
  11060.     }
  11061.     if (tmpno)
  11062.       sendport = 1;
  11063. #ifdef IP_TOS
  11064. #ifdef IPTOS_THROUGHPUT
  11065.     on = IPTOS_THROUGHPUT;
  11066.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  11067.       perror("ftp: setsockopt TOS (ignored)");
  11068. #endif
  11069. #endif
  11070.     return(0);
  11071.   bad:
  11072. #ifdef TCPIPLIB
  11073.     socket_close(data);
  11074. #else /* TCPIPLIB */
  11075. #ifdef USE_SHUTDOWN
  11076.     shutdown(data, 1+1);
  11077. #endif /* USE_SHUTDOWN */
  11078.     close(data);
  11079. #endif /* TCPIPLIB */
  11080.     data = -1;
  11081.     globaldin = data;
  11082.     if (tmpno)
  11083.       sendport = 1;
  11084.     return(-1);
  11085. }
  11086.  
  11087. #ifdef CK_SSL
  11088. static int
  11089. ssl_dataconn() {
  11090.     if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
  11091.         SSL_free(ssl_ftp_data_con);
  11092.         ssl_ftp_data_con=NULL;
  11093.     }
  11094.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  11095.  
  11096.     SSL_set_fd(ssl_ftp_data_con,data);
  11097.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  11098.  
  11099.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  11100.  
  11101.     if (ssl_debug_flag) {
  11102.         fprintf(stderr,"=>START SSL connect on DATA\n");
  11103.         fflush(stderr);
  11104.     }
  11105.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  11106.         static char errbuf[1024];
  11107.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  11108.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  11109.         fprintf(stderr,"%s\n", errbuf);
  11110.         fflush(stderr);
  11111. #ifdef TCPIPLIB
  11112.         socket_close(data);
  11113. #else /* TCPIPLIB */
  11114. #ifdef USE_SHUTDOWN
  11115.         shutdown(data, 1+1);
  11116. #endif /* USE_SHUTDOWN */
  11117.         close(data);
  11118. #endif /* TCPIPLIB */
  11119.         data = -1;
  11120.         globaldin = data;
  11121.         return(-1);
  11122.     } else {
  11123.         ssl_ftp_data_active_flag=1;
  11124.  
  11125.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  11126.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  11127.  
  11128.             if (!subject) {
  11129.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  11130.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  11131.  
  11132.                     ssl_ftp_data_active_flag = 0;
  11133. #ifdef TCPIPLIB
  11134.                     socket_close(data);
  11135. #else /* TCPIPLIB */
  11136. #ifdef USE_SHUTDOWN
  11137.                     shutdown(data, 1+1);
  11138. #endif /* USE_SHUTDOWN */
  11139.                     close(data);
  11140. #endif /* TCPIPLIB */
  11141.                     data = -1;
  11142.                     globaldin = data;
  11143.                     return(-1);
  11144.                 } else {
  11145.                     char * prmpt =
  11146.              "Warning: Server didn't provide a certificate, continue? (Y/N) ";
  11147.  
  11148.                     if (!out2screen && displa && fdispla) {
  11149.                         ftscreen(SCR_TC,0,0L,"Display canceled");
  11150.                         /* fdispla = XYFD_B; */
  11151.                     }
  11152.  
  11153.                     if (!getyesno(prmpt,0)) {
  11154.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  11155.                         ssl_ftp_data_active_flag = 0;
  11156. #ifdef TCPIPLIB
  11157.                         socket_close(data);
  11158. #else /* TCPIPLIB */
  11159. #ifdef USE_SHUTDOWN
  11160.                         shutdown(data, 1+1);
  11161. #endif /* USE_SHUTDOWN */
  11162.                         close(data);
  11163. #endif /* TCPIPLIB */
  11164.                         data = -1;
  11165.                         globaldin = data;
  11166.                         return(-1);
  11167.                     }
  11168.                 }
  11169.             } else {
  11170.                 if (!out2screen && displa && fdispla == XYFD_C) {
  11171.                     ftscreen(SCR_TC,0,0L,"Display canceled");
  11172.                     /* fdispla = XYFD_B; */
  11173.                 }
  11174.  
  11175.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_host)) {
  11176.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  11177.                     ssl_ftp_data_active_flag = 0;
  11178. #ifdef TCPIPLIB
  11179.                     socket_close(data);
  11180. #else /* TCPIPLIB */
  11181. #ifdef USE_SHUTDOWN
  11182.                     shutdown(data, 1+1);
  11183. #endif /* USE_SHUTDOWN */
  11184.                     close(data);
  11185. #endif /* TCPIPLIB */
  11186.                     data = -1;
  11187.                     globaldin = data;
  11188.                     return(-1);
  11189.                 }
  11190.             }
  11191.         }
  11192.         debug(F110,"dataconn","[SSL - OK]",0);
  11193. #ifdef COMMENT
  11194.         /* This messes up the full screen file transfer display */
  11195.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  11196. #endif /* COMMENT */
  11197.     }
  11198.     if (ssl_debug_flag) {
  11199.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  11200.         fflush(stderr);
  11201.     }
  11202.     return(data);
  11203. }
  11204. #endif /* CK_SSL */
  11205.  
  11206. static int
  11207. dataconn(lmode) char *lmode; {
  11208.     int s;
  11209. #ifdef IP_TOS
  11210.     int tos;
  11211. #endif /* IP_TOS */
  11212. #ifdef UCX50
  11213.     static u_int fromlen;
  11214. #else
  11215.     static SOCKOPT_T fromlen;
  11216. #endif /* UCX50 */
  11217.  
  11218.     fromlen = sizeof(hisdataaddr);
  11219.  
  11220. #ifndef NO_PASSIVE_MODE
  11221.     if (passivemode) {
  11222. #ifdef CK_SSL
  11223.         ssl_ftp_data_active_flag=0;
  11224.         if (ssl_ftp_active_flag &&
  11225.             (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  11226.           return(ssl_dataconn());
  11227. #endif /* CK_SSL */
  11228.         return(data);
  11229.     }
  11230. #endif /* NO_PASSIVE_MODE */
  11231.  
  11232.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  11233.     if (s < 0) {
  11234.         perror("ftp: accept");
  11235. #ifdef TCPIPLIB
  11236.         socket_close(data);
  11237. #else /* TCPIPLIB */
  11238. #ifdef USE_SHUTDOWN
  11239.         shutdown(data, 1+1);
  11240. #endif /* USE_SHUTDOWN */
  11241.         close(data);
  11242. #endif /* TCPIPLIB */
  11243.         data = -1;
  11244.         globaldin = data;
  11245.         return(-1);
  11246.     }
  11247. #ifdef TCPIPLIB
  11248.     socket_close(data);
  11249. #else /* TCPIPLIB */
  11250. #ifdef USE_SHUTDOWN
  11251.     shutdown(data, 1+1);
  11252. #endif /* USE_SHUTDOWN */
  11253.     close(data);
  11254. #endif /* TCPIPLIB */
  11255.     data = s;
  11256.     globaldin = data;
  11257. #ifdef IP_TOS
  11258. #ifdef IPTOS_THROUGHPUT
  11259.     tos = IPTOS_THROUGHPUT;
  11260.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  11261.       perror("ftp: setsockopt TOS (ignored)");
  11262. #endif /* IPTOS_THROUGHPUT */
  11263. #endif /* IP_TOS */
  11264.  
  11265. #ifdef CK_SSL
  11266.     ssl_ftp_data_active_flag=0;
  11267.     if (ssl_ftp_active_flag &&
  11268.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  11269.       return(ssl_dataconn());
  11270. #endif /* CK_SSL */
  11271.     return(data);
  11272. }
  11273.  
  11274. #ifdef FTP_PROXY
  11275. static sigtype
  11276. pscancel(sig) int sig; {
  11277.     cancelfile++;
  11278. }
  11279.  
  11280. static VOID
  11281. pswitch(flag) int flag; {
  11282.     extern int proxy;
  11283.     sig_t oldintr;
  11284.     static struct comvars {
  11285.         int connect;
  11286.         char name[MAXHOSTNAMELEN];
  11287.         struct sockaddr_in mctl;
  11288.         struct sockaddr_in hctl;
  11289.         FILE *in;
  11290.         FILE *out;
  11291.         int tpe;
  11292.         int curtpe;
  11293.         int cpnd;
  11294.         int sunqe;
  11295.         int runqe;
  11296.         int mcse;
  11297.         int ntflg;
  11298.         char nti[17];
  11299.         char nto[17];
  11300.         int mapflg;
  11301.         char mi[CKMAXPATH];
  11302.         char mo[CKMAXPATH];
  11303.         char *authtype;
  11304.         int clvl;
  11305.         int dlvl;
  11306. #ifdef FTP_KRB4
  11307.         des_cblock session;
  11308.         des_key_schedule ftp_sched;
  11309. #endif /* FTP_KRB4 */
  11310. #ifdef FTP_GSSAPI
  11311.         gss_ctx_id_t gcontext;
  11312. #endif /* GSSAPI */
  11313.     } proxstruct, tmpstruct;
  11314.     struct comvars *ip, *op;
  11315.  
  11316.     cancelfile = 0;
  11317.     oldintr = signal(SIGINT, pscancel);
  11318.     if (flag) {
  11319.         if (proxy)
  11320.           return;
  11321.         ip = &tmpstruct;
  11322.         op = &proxstruct;
  11323.         proxy++;
  11324.     } else {
  11325.         if (!proxy)
  11326.           return;
  11327.         ip = &proxstruct;
  11328.         op = &tmpstruct;
  11329.         proxy = 0;
  11330.     }
  11331.     ip->connect = connected;
  11332.     connected = op->connect;
  11333.     if (ftp_host) {
  11334.         strncpy(ip->name, ftp_host, sizeof(ip->name) - 1);
  11335.         ip->name[strlen(ip->name)] = '\0';
  11336.     } else
  11337.       ip->name[0] = 0;
  11338.     ftp_host = op->name;
  11339.     ip->hctl = hisctladdr;
  11340.     hisctladdr = op->hctl;
  11341.     ip->mctl = myctladdr;
  11342.     myctladdr = op->mctl;
  11343.     ip->in = csocket;
  11344.     csocket = op->in;
  11345.     ip->out = csocket;
  11346.     csocket = op->out;
  11347.     ip->tpe = ftp_typ;
  11348.     ftp_typ = op->tpe;
  11349.     ip->curtpe = curtype;
  11350.     curtype = op->curtpe;
  11351.     ip->cpnd = cpend;
  11352.     cpend = op->cpnd;
  11353.     ip->sunqe = ftp_usn;
  11354.     ftp_usn = op->sunqe;
  11355.     ip->mcse = mcase;
  11356.     mcase = op->mcse;
  11357.     ip->ntflg = ntflag;
  11358.     ntflag = op->ntflg;
  11359.     strncpy(ip->nti, ntin, 16);
  11360.     (ip->nti)[strlen(ip->nti)] = '\0';
  11361.     strcpy(ntin, op->nti);
  11362.     strncpy(ip->nto, ntout, 16);
  11363.     (ip->nto)[strlen(ip->nto)] = '\0';
  11364.     strcpy(ntout, op->nto);
  11365.     ip->mapflg = mapflag;
  11366.     mapflag = op->mapflg;
  11367.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  11368.     (ip->mi)[strlen(ip->mi)] = '\0';
  11369.     strcpy(mapin, op->mi);
  11370.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  11371.     (ip->mo)[strlen(ip->mo)] = '\0';
  11372.     strcpy(mapout, op->mo);
  11373.     ip->authtype = auth_type;
  11374.     auth_type = op->authtype;
  11375.     ip->clvl = ftp_cpl;
  11376.     ftp_cpl = op->clvl;
  11377.     ip->dlvl = ftp_dpl;
  11378.     ftp_dpl = op->dlvl;
  11379.     if (!ftp_cpl)
  11380.       ftp_cpl = FPL_CLR;
  11381.     if (!ftp_dpl)
  11382.       ftp_dpl = FPL_CLR;
  11383. #ifdef FTP_KRB4
  11384.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  11385.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  11386.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  11387.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  11388. #endif /* FTP_KRB4 */
  11389. #ifdef FTP_GSSAPI
  11390.     ip->gcontext = gcontext;
  11391.     gcontext = op->gcontext;
  11392. #endif /* GSSAPI */
  11393.     signal(SIGINT, oldintr);
  11394.     if (cancelfile) {
  11395.         cancelfile = 0;
  11396.         debug(F101,"pswitch cancelfile B","",cancelfile);
  11397.         (*oldintr)(SIGINT);
  11398.     }
  11399. }
  11400.  
  11401. static sigtype
  11402. cancelpt(sig) int sig; {
  11403.     printf("\n");
  11404.     fflush(stdout);
  11405.     ptabflg++;
  11406.     cancelfile = 0;
  11407. #ifndef OS2
  11408.     longjmp(ptcancel, 1);
  11409. #endif /* OS2 */
  11410. }
  11411.  
  11412. void
  11413. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  11414.     sig_t oldintr;
  11415.     int secndflag = 0, prox_type, nfnd;
  11416.     char *cmd2;
  11417. #ifdef BSDSELECT
  11418.     fd_set mask;
  11419. #endif /* BSDSELECT */
  11420.     sigtype cancelpt();
  11421.  
  11422.     if (strcmp(cmd, "RETR"))
  11423.       cmd2 = "RETR";
  11424.     else
  11425.       cmd2 = unique ? "STOU" : "STOR";
  11426.     if ((prox_type = type) == 0) {
  11427.         if (servertype == SYS_UNIX && unix_proxy)
  11428.           prox_type = FTT_BIN;
  11429.         else
  11430.           prox_type = FTT_ASC;
  11431.     }
  11432.     if (curtype != prox_type)
  11433.       changetype(prox_type, 1);
  11434.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  11435.         printf("Proxy server does not support third party transfers.\n");
  11436.         return;
  11437.     }
  11438.     pswitch(0);
  11439.     if (!connected) {
  11440.         printf("No primary connection\n");
  11441.         pswitch(1);
  11442.         ftpcode = -1;
  11443.         return;
  11444.     }
  11445.     if (curtype != prox_type)
  11446.       changetype(prox_type, 1);
  11447.  
  11448.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  11449.         pswitch(1);
  11450.         return;
  11451.     }
  11452.  
  11453.     /* Replace with calls to cc_execute() */
  11454.     if (setjmp(ptcancel))
  11455.       goto cancel;
  11456.     oldintr = signal(SIGINT, cancelpt);
  11457.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  11458.         signal(SIGINT, oldintr);
  11459.         pswitch(1);
  11460.         return;
  11461.     }
  11462.     sleep(2000);
  11463.     pswitch(1);
  11464.     secndflag++;
  11465.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  11466.       goto cancel;
  11467.     ptflag++;
  11468.     getreply(0,-1,-1,ftp_vbm,0);
  11469.     pswitch(0);
  11470.     getreply(0,-1,-1,ftp_vbm,0);
  11471.     signal(SIGINT, oldintr);
  11472.     pswitch(1);
  11473.     ptflag = 0;
  11474.     return;
  11475.  
  11476.   cancel:
  11477.     signal(SIGINT, SIG_IGN);
  11478.     ptflag = 0;
  11479.     if (strcmp(cmd, "RETR") && !proxy)
  11480.       pswitch(1);
  11481.     else if (!strcmp(cmd, "RETR") && proxy)
  11482.       pswitch(0);
  11483.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  11484.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  11485.             pswitch(0);
  11486.             if (cpend)
  11487.               cancel_remote(0);
  11488.         }
  11489.         pswitch(1);
  11490.         if (ptabflg)
  11491.           ftpcode = -1;
  11492.         signal(SIGINT, oldintr);
  11493.         return;
  11494.     }
  11495.     if (cpend)
  11496.       cancel_remote(0);
  11497.     pswitch(!proxy);
  11498.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  11499.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  11500.             pswitch(0);
  11501.             if (cpend)
  11502.               cancel_remote(0);
  11503.             pswitch(1);
  11504.             if (ptabflg)
  11505.               ftpcode = -1;
  11506.             signal(SIGINT, oldintr);
  11507.             return;
  11508.         }
  11509.     }
  11510.     if (cpend)
  11511.       cancel_remote(0);
  11512.     pswitch(!proxy);
  11513.     if (cpend) {
  11514. #ifdef BSDSELECT
  11515.         FD_ZERO(&mask);
  11516.         FD_SET(csocket, &mask);
  11517.         if ((nfnd = empty(&mask, 10)) <= 0) {
  11518.             if (nfnd < 0) {
  11519.                 perror("cancel");
  11520.             }
  11521.             if (ptabflg)
  11522.               ftpcode = -1;
  11523.             lostpeer();
  11524.         }
  11525. #else /* BSDSELECT */
  11526. #ifdef IBMSELECT
  11527.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  11528.             if (nfnd < 0) {
  11529.                 perror("cancel");
  11530.             }
  11531.             if (ptabflg)
  11532.               ftpcode = -1;
  11533.             lostpeer();
  11534.         }
  11535. #endif /* IBMSELECT */
  11536. #endif /* BSDSELECT */
  11537.         getreply(0,-1,-1,ftp_vbm,0);
  11538.         getreply(0,-1,-1,ftp_vbm,0);
  11539.     }
  11540.     if (proxy)
  11541.       pswitch(0);
  11542.     pswitch(1);
  11543.     if (ptabflg)
  11544.       ftpcode = -1;
  11545.     signal(SIGINT, oldintr);
  11546. }
  11547. #endif /* FTP_PROXY */
  11548.  
  11549. #ifdef FTP_SECURITY
  11550. #ifdef FTP_GSSAPI
  11551.  
  11552. struct {
  11553.     CONST gss_OID_desc * CONST * mech_type;
  11554.     char *service_name;
  11555. } gss_trials[] = {
  11556.     { &gss_mech_krb5, "ftp" },
  11557.     { &gss_mech_krb5, "host" },
  11558. };
  11559.  
  11560. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  11561. #endif /* FTP_GSSAPI */
  11562.  
  11563. static int
  11564. ftp_auth() {
  11565.     extern int setsafe();
  11566.     int j = 0, n;
  11567. #ifdef FTP_KRB4
  11568.     char *service, inst[INST_SZ];
  11569.     ULONG cksum;
  11570.     ULONG checksum = (ULONG) getpid();
  11571.     CHAR out_buf[FTP_BUFSIZ];
  11572.     int i;
  11573. #else /* FTP_KRB4 */
  11574. #ifdef FTP_GSSAPI
  11575.     CHAR out_buf[FTP_BUFSIZ];
  11576.     int i;
  11577. #endif /* FTP_GSSAPI */
  11578. #endif /* FTP_KRB4 */
  11579.  
  11580.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  11581.         return(0);
  11582.  
  11583.     if (auth_type)
  11584.       return(1);                        /* auth already succeeded */
  11585.  
  11586.     /* Try each auth type as specified by the end user */
  11587.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  11588. #ifdef FTP_GSSAPI
  11589.         if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  11590.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  11591.             if (n == REPLY_CONTINUE) {
  11592.                 OM_uint32 maj_stat, min_stat;
  11593.                 gss_name_t target_name;
  11594.                 gss_buffer_desc send_tok, recv_tok, *token_ptr;
  11595.                 char stbuf[FTP_BUFSIZ];
  11596.                 int comcode, trial;
  11597.                 struct gss_channel_bindings_struct chan;
  11598.                 char * realm = NULL;
  11599.                 char tgt[256];
  11600.  
  11601.                 chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  11602.                 chan.initiator_address.length = 4;
  11603.                 chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  11604.                 chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  11605.                 chan.acceptor_address.length = 4;
  11606.                 chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  11607.                 chan.application_data.length = 0;
  11608.                 chan.application_data.value = 0;
  11609.  
  11610.                 if (!quiet)
  11611.                   printf("GSSAPI accepted as authentication type\n");
  11612.  
  11613.                 realm = ck_krb5_realmofhost(ftp_host);
  11614.                 if (realm) {
  11615.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  11616.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  11617.                     if ( krb5_autoget &&
  11618.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  11619.                             (ck_krb5_is_tgt_valid() > 0)) )
  11620.                         ck_krb5_autoget_TGT(realm);
  11621.                 }
  11622.  
  11623.                 /* Blob from gss-client */
  11624.                 for (trial = 0; trial < n_gss_trials; trial++) {
  11625.                     /* ftp@hostname first, the host@hostname */
  11626.                     /* the V5 GSSAPI binding canonicalizes this for us... */
  11627.                     ckmakmsg(stbuf,FTP_BUFSIZ,
  11628.                              gss_trials[trial].service_name,
  11629.                              "@",
  11630.                              ftp_host,
  11631.                              NULL
  11632.                              );
  11633.                     if (ftp_deb)
  11634.                       fprintf(stderr,
  11635.                               "Authenticating to <%s>...\n", stbuf);
  11636.                     send_tok.value = stbuf;
  11637.                     send_tok.length = strlen(stbuf);
  11638.                     maj_stat = gss_import_name(&min_stat, &send_tok,
  11639.                                                gss_nt_service_name,
  11640.                                                &target_name
  11641.                                                );
  11642.                     if (maj_stat != GSS_S_COMPLETE) {
  11643.                         user_gss_error(maj_stat, min_stat, "parsing name");
  11644.                         secure_error("name parsed <%s>\n", stbuf);
  11645.                         continue;
  11646.                     }
  11647.                     token_ptr = GSS_C_NO_BUFFER;
  11648.                     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  11649.  
  11650.                     do {
  11651.                         if (ftp_deb)
  11652.                           fprintf(stderr, "calling gss_init_sec_context\n");
  11653.                         maj_stat =
  11654.                           gss_init_sec_context(&min_stat,
  11655.                                                GSS_C_NO_CREDENTIAL,
  11656.                                                &gcontext,
  11657.                                                target_name,
  11658.                                                (gss_OID) *
  11659.                                                  gss_trials[trial].mech_type,
  11660.                                                GSS_C_MUTUAL_FLAG |
  11661.                                                GSS_C_REPLAY_FLAG |
  11662.                                                (ftp_cfw ?
  11663.                                                 GSS_C_DELEG_FLAG : 0),
  11664.                                                0,
  11665.                                                 /* channel bindings */
  11666.                                                 (krb5_d_no_addresses ?
  11667.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  11668.                                                   &chan),
  11669.                                                 token_ptr,
  11670.                                                NULL,    /* ignore mech type */
  11671.                                                &send_tok,
  11672.                                                NULL,    /* ignore ret_flags */
  11673.                                                NULL
  11674.                                                );       /* ignore time_rec */
  11675.  
  11676.                         if (maj_stat != GSS_S_COMPLETE &&
  11677.                             maj_stat != GSS_S_CONTINUE_NEEDED) {
  11678.                             if (trial == n_gss_trials-1)
  11679.                               user_gss_error(maj_stat,
  11680.                                              min_stat,
  11681.                                              "initializing context"
  11682.                                              );
  11683.                             gss_release_name(&min_stat, &target_name);
  11684.                             /* maybe we missed on the service name */
  11685.                             goto outer_loop;
  11686.                         }
  11687.                         if (send_tok.length != 0) {
  11688.                             int len;
  11689.                             reply_parse = "ADAT="; /* for ftpcmd() later */
  11690.                             len = sizeof(out_buf);
  11691.                             kerror =
  11692.                               radix_encode(send_tok.value,
  11693.                                            out_buf,
  11694.                                            send_tok.length,
  11695.                                            &len,
  11696.                                            RADIX_ENCODE
  11697.                                            );
  11698.                             if (kerror)  {
  11699.                                 fprintf(stderr,
  11700.                                         "Base 64 encoding failed: %s\n",
  11701.                                         radix_error(kerror)
  11702.                                         );
  11703.                                 goto gss_complete_loop;
  11704.                             }
  11705.                             comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  11706.                             if (comcode != REPLY_COMPLETE
  11707.                                 /* && comcode != 3 (335)*/
  11708.                                 ) {
  11709.                                 if (trial == n_gss_trials-1) {
  11710.                                     fprintf(stderr, "GSSAPI ADAT failed\n");
  11711.                                     /* force out of loop */
  11712.                                     maj_stat = GSS_S_FAILURE;
  11713.                                 }
  11714.                                 /*
  11715.                                   Backoff to the v1 gssapi is still possible.
  11716.                                   Send a new AUTH command.  If that fails,
  11717.                                   terminate the loop.
  11718.                                 */
  11719.                                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  11720.                                     != REPLY_CONTINUE) {
  11721.                                     fprintf(stderr,
  11722.                                 "GSSAPI ADAT failed, AUTH restart failed\n");
  11723.                                     /* force out of loop */
  11724.                                     maj_stat = GSS_S_FAILURE;
  11725.                                 }
  11726.                                 goto outer_loop;
  11727.                             }
  11728.                             if (!reply_parse) {
  11729.                                 fprintf(stderr,
  11730.                               "No authentication data received from server\n");
  11731.                                 if (maj_stat == GSS_S_COMPLETE) {
  11732.                                     fprintf(stderr,
  11733.                                             "...but no more was needed\n");
  11734.                                     goto gss_complete_loop;
  11735.                                 } else {
  11736.                                     user_gss_error(maj_stat,
  11737.                                                    min_stat,
  11738.                                                    "no reply, huh?"
  11739.                                                    );
  11740.                                     goto gss_complete_loop;
  11741.                                 }
  11742.                             }
  11743.                             len = sizeof(out_buf);
  11744.                             kerror = radix_encode(reply_parse,out_buf,i,&len,
  11745.                                                   RADIX_DECODE);
  11746.                             if (kerror) {
  11747.                                 fprintf(stderr,
  11748.                                         "Base 64 decoding failed: %s\n",
  11749.                                         radix_error(kerror));
  11750.                                 goto gss_complete_loop;
  11751.                             }
  11752.  
  11753.                             /* everything worked */
  11754.                             token_ptr = &recv_tok;
  11755.                             recv_tok.value = out_buf;
  11756.                             recv_tok.length = len;
  11757.                             continue;
  11758.  
  11759.                             /* get out of loop clean */
  11760.                           gss_complete_loop:
  11761.                             trial = n_gss_trials-1;
  11762.                             gss_release_buffer(&min_stat, &send_tok);
  11763.                             gss_release_name(&min_stat, &target_name);
  11764.                             goto outer_loop;
  11765.                         }
  11766.                     } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  11767.  
  11768.                   outer_loop:
  11769.                     if (maj_stat == GSS_S_COMPLETE)
  11770.                       break;
  11771.                 }
  11772.                 if (maj_stat == GSS_S_COMPLETE) {
  11773.                     printf("GSSAPI authentication succeeded\n");
  11774.                     reply_parse = NULL;
  11775.                     auth_type = "GSSAPI";
  11776.                     return(1);
  11777.                 } else {
  11778.                     fprintf(stderr, "GSSAPI authentication failed\n");
  11779.                     reply_parse = NULL;
  11780.                 }
  11781.             } else if (ftp_deb)
  11782.               fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  11783.         }
  11784. #endif /* FTP_GSSAPI */
  11785. #ifdef FTP_SRP
  11786.         if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  11787.             if (srp_ftp_auth(ftp_host,NULL,NULL))
  11788.               return(1);
  11789.         }
  11790. #endif /* FTP_SRP */
  11791. #ifdef FTP_KRB4
  11792.         if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  11793.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  11794.             if (n == REPLY_CONTINUE) {
  11795.                 char tgt[4*REALM_SZ+1];
  11796.                 int rc;
  11797.  
  11798.                 if (!quiet)
  11799.                   printf("KERBEROS_V4 accepted as authentication type\n");
  11800.                 ckstrncpy(inst, (char *) krb_get_phost(ftp_host),INST_SZ);
  11801.                 ckstrncpy(ftp_realm,
  11802.                           (char *)ck_krb4_realmofhost(ftp_host),
  11803.                           REALM_SZ
  11804.                           );
  11805.  
  11806.                 ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  11807.                 rc = ck_krb4_tkt_isvalid(tgt);
  11808.  
  11809.                 if (rc <= 0 && krb4_autoget)
  11810.                   ck_krb4_autoget_TGT(ftp_realm);
  11811.  
  11812.                 service = "ftp";
  11813.                 kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  11814.                 if (kerror == KDC_PR_UNKNOWN) {
  11815.                     service = "rcmd";
  11816.                     kerror = krb_mk_req(&ftp_tkt,
  11817.                                         service,
  11818.                                         inst,
  11819.                                         ftp_realm,
  11820.                                         checksum
  11821.                                         );
  11822.                 }
  11823.                 if (kerror)
  11824.                   fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  11825.                           krb_get_err_text(kerror));
  11826.                 if (!kerror) {
  11827.                     kerror = krb_get_cred(service, inst, ftp_realm,
  11828.                                           (CREDENTIALS *)&ftp_cred);
  11829.                     if (kerror)
  11830.                       fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  11831.                               krb_get_err_text(kerror));
  11832.                 }
  11833.                 if (!kerror) {
  11834.                     int rc;
  11835.                     rc = des_key_sched(ftp_cred.session, ftp_sched);
  11836.                     if (rc == -1) {
  11837.                        printf("?Invalid DES key specified in credentials\r\n");
  11838.                        debug(F110,"ftp_auth",
  11839.                              "invalid DES Key specified in credentials",0);
  11840.                     } else if ( rc == -2 ) {
  11841.                         printf("?Weak DES key specified in credentials\r\n");
  11842.                         debug(F110,"ftp_auth",
  11843.                               "weak DES Key specified in credentials",0);
  11844.                     } else if ( rc != 0 ) {
  11845.                         printf("?DES Key Schedule not set by credentials\r\n");
  11846.                         debug(F110,"ftp_auth",
  11847.                               "DES Key Schedule not set by credentials",0);
  11848.                     }
  11849.                     reply_parse = "ADAT=";
  11850.                     i = sizeof(out_buf);
  11851.                     kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  11852.                                           &i, RADIX_ENCODE);
  11853.                     if (kerror) {
  11854.                         fprintf(stderr, "Base 64 encoding failed: %s\n",
  11855.                                 radix_error(kerror));
  11856.                         goto krb4_err;
  11857.                     }
  11858.                     if (i > FTP_BUFSIZ - 6)
  11859.                       printf("?ADAT data too long\n");
  11860.                     if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  11861.                         REPLY_COMPLETE) {
  11862.                         fprintf(stderr, "Kerberos V4 authentication failed\n");
  11863.                         goto krb4_err;
  11864.                     }
  11865.                     if (!reply_parse) {
  11866.                         fprintf(stderr,
  11867.                              "No authentication data received from server\n");
  11868.                         goto krb4_err;
  11869.                     }
  11870.                     i = sizeof(out_buf);
  11871.                     kerror =
  11872.                       radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  11873.                     if (kerror) {
  11874.                         fprintf(stderr, "Base 64 decoding failed: %s\n",
  11875.                                 radix_error(kerror));
  11876.                         goto krb4_err;
  11877.                     }
  11878.                     kerror = krb_rd_safe(out_buf, i,
  11879. #ifdef KRB524
  11880.                                          ftp_cred.session,
  11881. #else /* KRB524 */
  11882.                                          &ftp_cred.session,
  11883. #endif /* KRB524 */
  11884.                                          &hisctladdr,
  11885.                                          &myctladdr,
  11886.                                          &ftp_msg_data
  11887.                                          );
  11888.                     if (kerror) {
  11889.                         fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  11890.                                 krb_get_err_text(kerror));
  11891.                         goto krb4_err;
  11892.                     }
  11893.  
  11894.                     /* fetch the (modified) checksum */
  11895.                     memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  11896.                     if (ntohl(cksum) == checksum + 1) {
  11897.                         if (ftp_vbm)
  11898.                           printf("Kerberos V4 authentication succeeded\n");
  11899.                         reply_parse = NULL;
  11900.                         auth_type = "KERBEROS_V4";
  11901.                         return(1);
  11902.                     } else
  11903.                       fprintf(stderr,
  11904.                               "Kerberos V4 mutual authentication failed\n");
  11905.                   krb4_err:
  11906.                     reply_parse = NULL;
  11907.                 }
  11908.             } else if (ftp_deb)
  11909.               fprintf(stderr,
  11910.                       "KERBEROS_V4 rejected as an authentication type\n");
  11911.         }
  11912. #endif /* FTP_KRB4 */
  11913. #ifdef CK_SSL
  11914.         if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  11915. #ifdef FTPHOST
  11916.             if (!hostcmd) {
  11917.                 ftpcmd("HOST",ftp_host,0,0,0);
  11918.                 hostcmd = 1;
  11919.             }
  11920. #endif /* FTPHOST */
  11921.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  11922.             if (n != REPLY_COMPLETE)
  11923.               n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  11924.             if (n == REPLY_COMPLETE) {
  11925.                 if (!quiet)
  11926.                   printf("TLS accepted as authentication type\n");
  11927.  
  11928.                 auth_type = "TLS";
  11929.                 ssl_auth();
  11930.                 if (ssl_ftp_active_flag ) {
  11931.                     ftp_dpl = FPL_CLR;
  11932.                     ftp_cpl = FPL_PRV;
  11933.                     return(1);
  11934.                 } else {
  11935.                     fprintf(stderr,"TLS authentication failed\n");
  11936.                     auth_type = NULL;
  11937. #ifdef TCPIPLIB
  11938.                     socket_close(csocket);
  11939. #else /* TCPIPLIB */
  11940. #ifdef USE_SHUTDOWN
  11941.                     shutdown(csocket, 1+1);
  11942. #endif /* USE_SHUTDOWN */
  11943.                     close(csocket);
  11944. #endif /* TCPIPLIB */
  11945.                     csocket = -1;
  11946.                     if (ftp_hookup(ftp_host,ftp_port,0) == NULL)
  11947.                       return(0);
  11948.                 }
  11949.             } else if (ftp_deb)
  11950.               fprintf(stderr,"TLS rejected as an authentication type\n");
  11951.         }
  11952.         if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  11953. #ifdef FTPHOST
  11954.             if (!hostcmd) {
  11955.                 ftpcmd("HOST",ftp_host,0,0,0);
  11956.                 hostcmd = 1;
  11957.             }
  11958. #endif /* FTPHOST */
  11959.             n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  11960.             if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  11961.                 if (!quiet)
  11962.                   printf("SSL accepted as authentication type\n");
  11963.                 auth_type = "SSL";
  11964.                 ssl_auth();
  11965.                 if (ssl_ftp_active_flag) {
  11966.                     ftp_dpl = FPL_PRV;
  11967.                     ftp_cpl = FPL_PRV;
  11968.                     setprotbuf(1<<20);
  11969.                     return(1);
  11970.                 } else {
  11971.                     fprintf(stderr,"SSL authentication failed\n");
  11972.                     auth_type = NULL;
  11973. #ifdef TCPIPLIB
  11974.                     socket_close(csocket);
  11975. #else /* TCPIPLIB */
  11976. #ifdef USE_SHUTDOWN
  11977.                     shutdown(csocket, 1+1);
  11978. #endif /* USE_SHUTDOWN */
  11979.                     close(csocket);
  11980. #endif /* TCPIPLIB */
  11981.                     csocket = -1;
  11982.                     if (ftp_hookup(ftp_host,ftp_port,0) == NULL)
  11983.                       return(0);
  11984.                 }
  11985.         } else if (ftp_deb)
  11986.           fprintf(stderr, "SSL rejected as an authentication type\n");
  11987.  
  11988.         }
  11989. #endif /* CK_SSL */
  11990.         /* Other auth types go here ... */
  11991.     } /* for (j;;) */
  11992.     return(0);
  11993. }
  11994. #endif /* FTP_SECURITY */
  11995.  
  11996. static int
  11997. #ifdef CK_ANSIC
  11998. setprotbuf(unsigned int size)
  11999. #else
  12000. setprotbuf(size) unsigned int size;
  12001. #endif /* CK_ANSIC */
  12002. /* setprotbuf */ {
  12003.     if (ucbuf)
  12004.       free(ucbuf);
  12005.     ucbuf = NULL;
  12006.     ucbufsiz = 0;
  12007.     actualbuf = size;
  12008.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  12009.         if (actualbuf)
  12010.           actualbuf /= 2;
  12011.         else
  12012.           return(0);
  12013.     }
  12014.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  12015.     if (ucbufsiz < 128) {
  12016.         printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  12017.     } else if (ucbufsiz < 0) {
  12018.         printf("ERROR: ucbuf allocation failure\n");
  12019.         return(-1);
  12020.     }
  12021.     maxbuf = actualbuf;
  12022.     return(1);
  12023. }
  12024.  
  12025. static int
  12026. #ifdef CK_ANSIC
  12027. setpbsz(unsigned int size)
  12028. #else
  12029. setpbsz(size) unsigned int size;
  12030. #endif /* CK_ANSIC */
  12031. /* setpbsz */ {
  12032.     if (!setprotbuf(size)) {
  12033.         perror("?Error while trying to malloc PROT buffer:");
  12034. #ifdef FTP_SRP
  12035.         srp_reset();
  12036. #endif /* FTP_SRP */
  12037.         ftpclose();
  12038.         return(-1);
  12039.     }
  12040.     reply_parse = "PBSZ=";
  12041.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  12042. #ifdef CK_SSL
  12043.              ssl_ftp_active_flag ? "0" :
  12044. #endif /* CK_SSL */
  12045.              ckuitoa(actualbuf),NULL,NULL);
  12046.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  12047.         if (connected) {
  12048.             printf("?Unable to negotiate PROT buffer size with FTP server\n");
  12049.             ftpclose();
  12050.         }
  12051.         return(-1);
  12052.     }
  12053.     if (reply_parse) {
  12054.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  12055.           maxbuf = actualbuf;
  12056.     } else
  12057.       maxbuf = actualbuf;
  12058.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  12059.     reply_parse = NULL;
  12060.     return(0);
  12061. }
  12062.  
  12063. static VOID
  12064. cancel_remote(din) int din; {
  12065.     CHAR buf[FTP_BUFSIZ];
  12066.     int x, nfnd;
  12067. #ifdef BSDSELECT
  12068.     fd_set mask;
  12069. #endif /* BSDSELECT */
  12070. #ifdef IBMSELECT
  12071.     int fds[2], fdcnt = 0;
  12072. #endif /* IBMSELECT */
  12073. #ifdef DEBUG
  12074.     extern int debtim;
  12075.     int xdebtim;
  12076.     xdebtim = debtim;
  12077.     debtim = 1;
  12078. #endif /* DEBUG */
  12079.     debug(F100,"ftp cancel_remote entry","",0);
  12080. #ifdef CK_SSL
  12081.     if (ssl_ftp_active_flag) {
  12082.         /*
  12083.          * Send Telnet IP, Telnet DM but do so inline and within the
  12084.          * TLS channel
  12085.          */
  12086.         int count, error;
  12087.  
  12088.         buf[0] = IAC;
  12089.         buf[1] = TN_IP;
  12090.         buf[2] = IAC;
  12091.         buf[3] = TN_DM;
  12092.         buf[4] = NUL;
  12093.  
  12094.         count = SSL_write(ssl_ftp_con, buf, 4);
  12095.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  12096.         error = SSL_get_error(ssl_ftp_con,count);
  12097.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  12098.         switch (error) {
  12099.           case SSL_ERROR_NONE:
  12100.             break;
  12101.           case SSL_ERROR_WANT_WRITE:
  12102.           case SSL_ERROR_WANT_READ:
  12103.           case SSL_ERROR_SYSCALL:
  12104. #ifdef NT
  12105.             {
  12106.                 int gle = GetLastError();
  12107.             }
  12108. #endif /* NT */
  12109.           case SSL_ERROR_WANT_X509_LOOKUP:
  12110.           case SSL_ERROR_SSL:
  12111.           case SSL_ERROR_ZERO_RETURN:
  12112.           default:
  12113.             lostpeer();
  12114.             return;
  12115.         }
  12116.     } else
  12117. #endif /* CK_SSL */
  12118.     {
  12119.         /*
  12120.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  12121.          * after urgent byte rather than before as is protocol now.
  12122.          */
  12123.         buf[0] = IAC;
  12124.         buf[1] = TN_IP;
  12125.         buf[2] = IAC;
  12126.         buf[3] = NUL;
  12127.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  12128.           perror("cancel");
  12129.         debug(F101,"ftp cancel_remote send 1","",x);
  12130.         buf[0] = TN_DM;
  12131.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  12132.         debug(F101,"ftp cancel_remote send 2","",x);
  12133.     }
  12134.     x = scommand("ABOR");
  12135.     debug(F101,"ftp cancel_remote scommand","",x);
  12136. #ifdef BSDSELECT
  12137.     FD_ZERO(&mask);
  12138.     FD_SET(csocket, &mask);
  12139.     if (din) {
  12140.         FD_SET(din, &mask);
  12141.     }
  12142.     nfnd = empty(&mask, 10);
  12143.     debug(F101,"ftp cancel_remote empty","",nfnd);
  12144.     if ((nfnd) <= 0) {
  12145.         if (nfnd < 0) {
  12146.             perror("cancel");
  12147.         }
  12148. #ifdef FTP_PROXY
  12149.         if (ptabflg)
  12150.           ftpcode = -1;
  12151. #endif /* FTP_PROXY */
  12152.         lostpeer();
  12153.     }
  12154.     debug(F110,"ftp cancel_remote","D",0);
  12155.     if (din && FD_ISSET(din, &mask)) {
  12156.         /* Security: No threat associated with this read. */
  12157.         /* But you can't simply read the TLS data stream  */
  12158. #ifdef CK_SSL
  12159.         if (ssl_ftp_data_active_flag) {
  12160.             int count, error;
  12161.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  12162.                     /* LOOP */ ;
  12163.         } else
  12164. #endif /* CK_SSL */
  12165.         {
  12166.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  12167.                 /* LOOP */ ;
  12168.         }
  12169.     }
  12170.     debug(F110,"ftp cancel_remote","E",0);
  12171. #else /* BSDSELECT */
  12172. #ifdef IBMSELECT
  12173.     fds[0] = csocket;
  12174.     fdcnt++;
  12175.     if (din) {
  12176.         fds[1] = din;
  12177.         fdcnt++;
  12178.     }
  12179.     nfnd = empty(fds, fdcnt, 10);
  12180.     debug(F101,"ftp cancel_remote empty","",nfnd);
  12181.     if ((nfnd) <= 0) {
  12182.         if (nfnd < 0) {
  12183.             perror("cancel");
  12184.         }
  12185. #ifdef FTP_PROXY
  12186.         if (ptabflg)
  12187.           ftpcode = -1;
  12188. #endif /* FTP_PROXY */
  12189.         lostpeer();
  12190.     }
  12191.     debug(F110,"ftp cancel_remote","D",0);
  12192.     if (din && select(&din, 1,0,0,1) ) {
  12193. #ifdef CK_SSL
  12194.         if (ssl_ftp_data_active_flag) {
  12195.             int count, error;
  12196.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  12197.                     /* LOOP */ ;
  12198.         } else
  12199. #endif /* CK_SSL */
  12200.         {
  12201.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  12202.                 /* LOOP */ ;
  12203.         }
  12204.     }
  12205.     debug(F110,"ftp cancel_remote","E",0);
  12206. #else /* IBMSELECT */
  12207.     Some form of select is required.
  12208. #endif /* IBMSELECT */
  12209. #endif /* BSDSELECT */
  12210.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  12211.         debug(F110,"ftp cancel_remote","F",0);
  12212.         /* 552 needed for NIC style cancel */
  12213.         getreply(0,-1,-1,ftp_vbm,0);
  12214.         debug(F110,"ftp cancel_remote","G",0);
  12215.     }
  12216.     debug(F110,"ftp cancel_remote","H",0);
  12217.     getreply(0,-1,-1,ftp_vbm,0);
  12218.     debug(F110,"ftp cancel_remote","I",0);
  12219. #ifdef DEBUG
  12220.     debtim = xdebtim;
  12221. #endif /* DEBUG */
  12222. }
  12223.  
  12224. static int
  12225. fts_dpl(x) int x; {
  12226.     if (!auth_type) {
  12227.         switch ( x ) {
  12228.           case FPL_PRV:
  12229.             printf("?Cannot set protection level to PRIVATE\n");
  12230.             return(0);
  12231.           case FPL_SAF:
  12232.             printf("?Cannot set protection level to SAFE\n");
  12233.             return(0);
  12234.         }
  12235.         ftp_dpl = x;
  12236.         return(1);
  12237.     }
  12238.  
  12239. #ifdef CK_SSL
  12240.     if (x == FPL_SAF &&
  12241.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  12242.         printf("Cannot set protection level to safe\n");
  12243.         return(0);
  12244.     }
  12245. #endif /* CK_SSL */
  12246.     /* Start with a PBSZ of 1 meg */
  12247.     if (x != FPL_CLR) {
  12248.         if (setpbsz(DEFAULT_PBSZ) < 0)
  12249.           return(0);
  12250.     }
  12251.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  12252.                (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  12253.     if (y == REPLY_COMPLETE) {
  12254.         ftp_dpl = x;
  12255.         return(1);
  12256.     }
  12257.     return(0);
  12258. }
  12259.  
  12260. static int
  12261. fts_cpl(x) int x; {
  12262.     if (!auth_type) {
  12263.         switch ( x ) {
  12264.           case FPL_PRV:
  12265.             printf("?Cannot set protection level to PRIVATE\n");
  12266.             return(0);
  12267.           case FPL_SAF:
  12268.             printf("?Cannot set protection level to SAFE\n");
  12269.             return(0);
  12270.         }
  12271.         ftp_cpl = x;
  12272.         return(1);
  12273.     }
  12274.     if (x == FPL_CLR) {
  12275.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  12276.         if (y == REPLY_COMPLETE) {
  12277.             ftp_cpl = x;
  12278.             return(1);
  12279.         }
  12280.         return(0);
  12281.     }
  12282.     ftp_cpl = x;
  12283.     return(1);
  12284. }
  12285.  
  12286. #ifdef FTP_GSSAPI
  12287. static VOID
  12288. user_gss_error(maj_stat, min_stat, s)
  12289.     OM_uint32 maj_stat, min_stat;
  12290.     char *s;
  12291. {
  12292.     /* a lot of work just to report the error */
  12293.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  12294.     gss_buffer_desc msg;
  12295.     msg_ctx = 0;
  12296.     while (!msg_ctx) {
  12297.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  12298.                                        GSS_C_GSS_CODE,
  12299.                                        GSS_C_NULL_OID,
  12300.                                        &msg_ctx,
  12301.                                        &msg
  12302.                                        );
  12303.         if ((gmaj_stat == GSS_S_COMPLETE)||
  12304.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  12305.             fprintf(stderr, "GSSAPI error major: %s\n",
  12306.                     (char*)msg.value);
  12307.             gss_release_buffer(&gmin_stat, &msg);
  12308.         }
  12309.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  12310.           break;
  12311.     }
  12312.     msg_ctx = 0;
  12313.     while (!msg_ctx) {
  12314.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  12315.                                        GSS_C_MECH_CODE,
  12316.                                        GSS_C_NULL_OID,
  12317.                                        &msg_ctx,
  12318.                                        &msg
  12319.                                        );
  12320.         if ((gmaj_stat == GSS_S_COMPLETE)||
  12321.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  12322.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  12323.             gss_release_buffer(&gmin_stat, &msg);
  12324.         }
  12325.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  12326.           break;
  12327.     }
  12328.     fprintf(stderr, "GSSAPI error: %s\n", s);
  12329. }
  12330. #endif /* FTP_GSSAPI */
  12331.  
  12332. #ifndef NOMHHOST
  12333. #ifdef datageneral
  12334. #define NOMHHOST
  12335. #else
  12336. #ifdef HPUX5WINTCP
  12337. #define NOMHHOST
  12338. #endif /* HPUX5WINTCP */
  12339. #endif /* datageneral */
  12340. #endif /* NOMHHOST */
  12341.  
  12342. #ifdef INADDRX
  12343. static struct in_addr inaddrx;
  12344. #endif /* INADDRX */
  12345.  
  12346. static char *
  12347. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  12348.     register struct hostent *hp = 0;
  12349. #ifdef IP_TOS
  12350. #ifdef IPTOS_THROUGHPUT
  12351.     int tos;
  12352. #endif /* IPTOS_THROUGHPUT */
  12353. #endif /* IP_TOS */
  12354.     int s;
  12355.     GSOCKNAME_T len;
  12356.     static char hostnamebuf[512];
  12357.     char hostname[512] /* , *p, *q */ ;
  12358.     int  cport;
  12359. #ifdef DEBUG
  12360.     extern int debtim;
  12361.     int xdebtim;
  12362.     xdebtim = debtim;
  12363.     debtim = 1;
  12364. #endif /* DEBUG */
  12365.  
  12366. #ifndef NOHTTP
  12367.     if (tcp_http_proxy) {
  12368.         struct servent *destsp;
  12369.         char *p, *q;
  12370.  
  12371.         ckmakmsg(proxyhost,sizeof(proxyhost),host,":",ckuitoa(port),NULL);
  12372.         for (p = tcp_http_proxy, q = hostname;
  12373.              *p != '\0' && *p != ':';
  12374.              p++, q++
  12375.              )
  12376.           *q = *p;
  12377.         *q = '\0';
  12378.  
  12379.         if (*p == ':')
  12380.           p++;
  12381.         else
  12382.           p = "http";
  12383.  
  12384.         destsp = getservbyname(p,"tcp");
  12385.         if (destsp)
  12386.           cport = ntohs(destsp->s_port);
  12387.         else if (p) {
  12388.           cport = atoi(p);
  12389.         } else
  12390.           cport = 80;
  12391.     } else
  12392. #endif /* NOHTTP */
  12393.     {
  12394.         ckstrncpy(hostname,host,sizeof(hostname));
  12395.         cport = port;
  12396.     }
  12397.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  12398.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  12399.     if (hisctladdr.sin_addr.s_addr != -1) {
  12400.         debug(F110,"ftp hookup A",hostname,0);
  12401.         hisctladdr.sin_family = AF_INET;
  12402.         ckstrncpy(hostnamebuf, hostname, sizeof(hostnamebuf));
  12403.     } else {
  12404.         debug(F110,"ftp hookup B",hostname,0);
  12405.         hp = gethostbyname(hostname);
  12406. #ifdef HADDRLIST
  12407.         hp = ck_copyhostent(hp);        /* make safe copy that won't change */
  12408. #endif /* HADDRLIST */
  12409.         if (hp == NULL) {
  12410.             fprintf(stderr, "ftp: %s: ", host);
  12411.             herror();
  12412.             ftpcode = -1;
  12413. #ifdef DEBUG
  12414.             debtim = xdebtim;
  12415. #endif /* DEBUG */
  12416.             return((char *) 0);
  12417.         }
  12418.         hisctladdr.sin_family = hp->h_addrtype;
  12419. #ifdef HADDRLIST
  12420.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  12421.                sizeof(hisctladdr.sin_addr));
  12422. #else /* HADDRLIST */
  12423.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  12424.                sizeof(hisctladdr.sin_addr));
  12425. #endif /* HADDRLIST */
  12426.         ckstrncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
  12427.     }
  12428.     debug(F110,"ftp hookup C",hostnamebuf,0);
  12429.     ftp_host = hostnamebuf;
  12430.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12431.     debug(F101,"ftp hookup socket","",s);
  12432.     if (s < 0) {
  12433.         perror("ftp: socket");
  12434.         ftpcode = -1;
  12435. #ifdef DEBUG
  12436.         debtim = xdebtim;
  12437. #endif /* DEBUG */
  12438.         return(0);
  12439.     }
  12440.     hisctladdr.sin_port = htons(cport);
  12441.     errno = 0;
  12442. #ifdef HADDRLIST
  12443.     debug(F100,"ftp hookup HADDRLIST","",0);
  12444.     while
  12445. #else
  12446.     debug(F100,"ftp hookup no HADDRLIST","",0);
  12447.     if
  12448. #endif /* HADDRLIST */
  12449.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  12450.           debug(F101,"ftp hookup connect failed","",errno);
  12451. #ifdef HADDRLIST
  12452.           if (hp && hp->h_addr_list[1]) {
  12453.               int oerrno = errno;
  12454.  
  12455.               fprintf(stderr, "ftp: connect to address %s: ",
  12456.                       inet_ntoa(hisctladdr.sin_addr));
  12457.               errno = oerrno;
  12458.               perror((char *) 0);
  12459.               hp->h_addr_list++;
  12460.               memcpy((char *)&hisctladdr.sin_addr,
  12461.                      hp->h_addr_list[0],
  12462.                      sizeof(hisctladdr.sin_addr));
  12463.               fprintf(stdout, "Trying %s...\n",
  12464.                       inet_ntoa(hisctladdr.sin_addr));
  12465. #ifdef TCPIPLIB
  12466.               socket_close(s);
  12467. #else /* TCPIPLIB */
  12468.               close(s);
  12469. #endif /* TCPIPLIB */
  12470.               s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12471.               if (s < 0) {
  12472.                   perror("ftp: socket");
  12473.                   ftpcode = -1;
  12474. #ifdef DEBUG
  12475.                   debtim = xdebtim;
  12476. #endif /* DEBUG */
  12477.                   return(0);
  12478.               }
  12479.               continue;
  12480.           }
  12481. #endif /* HADDRLIST */
  12482.           perror("ftp: connect");
  12483.           ftpcode = -1;
  12484.           goto bad;
  12485.       }
  12486.     debug(F100,"ftp hookup connect ok","",0);
  12487.  
  12488.     len = sizeof (myctladdr);
  12489.     errno = 0;
  12490.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  12491.         debug(F101,"ftp hookup getsockname failed","",errno);
  12492.         perror("ftp: getsockname");
  12493.         ftpcode = -1;
  12494.         goto bad;
  12495.     }
  12496.     debug(F100,"ftp hookup getsockname ok","",0);
  12497.  
  12498. #ifndef NOHTTP
  12499.     if (tcp_http_proxy) {
  12500. #ifdef OS2
  12501.         char * agent = "Kermit 95";     /* Default user agent */
  12502. #else
  12503.         char * agent = "C-Kermit";
  12504. #endif /* OS2 */
  12505.  
  12506.         if (http_connect(s,agent,NULL,
  12507.                          tcp_http_proxy_user,
  12508.                          tcp_http_proxy_pwd,
  12509.                          0,
  12510.                          proxyhost
  12511.                          ) < 0) {
  12512.             char * foo = NULL;
  12513. #ifdef TCPIPLIB
  12514.             socket_close(s);
  12515. #else /* TCPIPLIB */
  12516.             close(s);
  12517. #endif /* TCPIPLIB */
  12518.  
  12519.             while (foo == NULL && tcp_http_proxy != NULL ) {
  12520.  
  12521.                 if (tcp_http_proxy_errno == 401 ||
  12522.                      tcp_http_proxy_errno == 407 ) {
  12523.                     char uid[UIDBUFLEN];
  12524.                     char pwd[256];
  12525.  
  12526.                     readtext("Proxy Userid: ",uid,UIDBUFLEN);
  12527.                     if (uid[0]) {
  12528.                         char * proxy_user, * proxy_pwd;
  12529.  
  12530.                         readpass("Proxy Password: ",pwd,256);
  12531.  
  12532.                         proxy_user = tcp_http_proxy_user;
  12533.                         proxy_pwd  = tcp_http_proxy_pwd;
  12534.  
  12535.                         tcp_http_proxy_user = uid;
  12536.                         tcp_http_proxy_pwd = pwd;
  12537.  
  12538.                         foo = ftp_hookup(host, port, 0);
  12539.  
  12540.                         debug(F110,"ftp_hookup()",foo,0);
  12541.                         memset(pwd,0,sizeof(pwd));
  12542.                         tcp_http_proxy_user = proxy_user;
  12543.                         tcp_http_proxy_pwd = proxy_pwd;
  12544.                     } else
  12545.                         break;
  12546.                 } else
  12547.                     break;
  12548.             }
  12549.             if (foo != NULL)
  12550.               return(foo);
  12551.             perror("ftp: connect");
  12552.             ftpcode = -1;
  12553.             goto bad;
  12554.         }
  12555.         ckstrncpy(hostnamebuf, proxyhost, sizeof(hostnamebuf));
  12556.     }
  12557. #endif /* NOHTTP */
  12558.  
  12559.     csocket = s;
  12560.  
  12561. #ifdef CK_SSL
  12562.     if (tls) {
  12563.         /* FTP over SSL
  12564.          * If the connection is over an SSL proxy then the
  12565.          * auth_type will be NULL.  However, I'm not sure
  12566.          * whether we should protect the data channel in
  12567.          * that case or not.
  12568.          */
  12569.  
  12570.         debug(F100,"ftp hookup use_tls","",0);
  12571.         if (!ssl_auth()) {
  12572.             debug(F100,"ftp hookup ssl_auth failed","",0);
  12573.             auth_type = NULL;
  12574.             ftpcode = -1;
  12575.             csocket = -1;
  12576.             goto bad;
  12577.         }
  12578.         ssl_ftp_proxy = 1;
  12579.     }
  12580. #endif /* CK_SSL */
  12581.  
  12582. #ifdef IP_TOS
  12583. #ifdef IPTOS_LOWDELAY
  12584.     tos = IPTOS_LOWDELAY;
  12585.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  12586.       perror("ftp: setsockopt TOS (ignored)");
  12587. #endif
  12588. #endif
  12589.     if (!quiet)
  12590.       printf("Connected to %s.\n", ftp_host);
  12591.  
  12592.     /* Read greeting from server */
  12593.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  12594.         debug(F100,"ftp hookup bad reply","",0);
  12595. #ifdef TCPIPLIB
  12596.         socket_close(csocket);
  12597. #else /* TCPIPLIB */
  12598.         close(csocket);
  12599. #endif /* TCPIPLIB */
  12600.         ftpcode = -1;
  12601.         goto bad;
  12602.     }
  12603. #ifdef SO_OOBINLINE
  12604.     {
  12605.         int on = 1;
  12606.         errno = 0;
  12607.         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  12608.                        sizeof(on)) < 0) {
  12609.             perror("ftp: setsockopt");
  12610.             debug(F101,"ftp hookup setsockopt failed","",errno);
  12611.         }
  12612. #ifdef DEBUG
  12613.         else
  12614.           debug(F100,"ftp hookup setsockopt ok","",0);
  12615. #endif /* DEBUG */
  12616.     }
  12617. #endif /* SO_OOBINLINE */
  12618.  
  12619. #ifdef DEBUG
  12620.     debtim = xdebtim;
  12621. #endif /* DEBUG */
  12622.     return(ftp_host);
  12623.  
  12624.   bad:
  12625.     debug(F100,"ftp hookup bad","",0);
  12626. #ifdef TCPIPLIB
  12627.     socket_close(s);
  12628. #else /* TCPIPLIB */
  12629.     close(s);
  12630. #endif /* TCPIPLIB */
  12631. #ifdef DEBUG
  12632.     debtim = xdebtim;
  12633. #endif /* DEBUG */
  12634.     csocket = -1;
  12635.     return((char *)0);
  12636. }
  12637.  
  12638. static VOID
  12639. ftp_init() {
  12640.     int n;
  12641.  
  12642.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  12643.     /* clients do it.  In any case, failure of this command is not a */
  12644.     /* reliable indication that the server does not support Restart. */
  12645.  
  12646.     okrestart = 0;
  12647.     if (!noinit) {
  12648.         n = ftpcmd("REST 0",NULL,0,0,0);
  12649.         if (n == REPLY_COMPLETE)
  12650.           okrestart = 1;
  12651. #ifdef COMMENT
  12652.         else if (ftp_deb)
  12653.           printf("WARNING: Unable to restore file pointer.\n");
  12654. #endif /* COMMENT */
  12655.     }
  12656.  
  12657.     n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
  12658.     if (n == REPLY_COMPLETE) {
  12659.         register char *cp, c = NUL;
  12660.         cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  12661.         if (cp == NULL)
  12662.           cp = ckstrchr(ftp_reply_str+4,'\r');
  12663.         if (cp) {
  12664.             if (cp[-1] == '.')
  12665.               cp--;
  12666.             c = *cp;                    /* Save this char */
  12667.             *cp = '\0';                 /* Replace it with NUL */
  12668.         }
  12669.         if (!quiet)
  12670.           printf("Remote system type is %s.\n",ftp_reply_str+4);
  12671.         ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  12672.         if (cp)                         /* Put back saved char */
  12673.           *cp = c;
  12674.     }
  12675.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  12676.  
  12677.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  12678.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  12679.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  12680.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  12681.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  12682.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  12683.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  12684.  
  12685. #ifdef FTP_PROXY
  12686.     unix_proxy = 0;
  12687.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  12688. #endif /* FTP_PROXY */
  12689.  
  12690.     if (ftp_cmdlin && xfermode == XMODE_M)
  12691.       ftp_typ = binary;                 /* Type given on command line */
  12692.     else                                /* Otherwise set it automatically */
  12693.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  12694.     changetype(ftp_typ,0);              /* Change to this type */
  12695.     g_ftp_typ = ftp_typ;                /* Make it the global type */
  12696.     if (!quiet)
  12697.       printf("Default transfer mode is %s\n",
  12698.              ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  12699.              );
  12700.     if (!noinit) {
  12701.         n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
  12702. #ifdef COMMENT
  12703.         if (n != REPLY_COMPLETE)
  12704.           printf("WARNING: Server does not accept MODE S(TREAM)\n");
  12705. #endif /* COMMENT */
  12706.         n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
  12707. #ifdef COMMENT
  12708.         if (n != REPLY_COMPLETE)
  12709.           printf("WARNING: Server does not accept STRU F(ILE)\n");
  12710. #endif /* COMMENT */
  12711.     }
  12712. }
  12713.  
  12714.  
  12715. static int
  12716. ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
  12717.     static char ftppass[80]="";
  12718.     char tmp[256];
  12719.     char *user = NULL, *pass = NULL, *acct = NULL;
  12720.     int n, aflag = 0;
  12721.     extern char uidbuf[];
  12722.     extern char pwbuf[];
  12723.     extern int  pwflg, pwcrypt;
  12724.  
  12725.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  12726.  
  12727.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  12728.       anonymous = 1;
  12729.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  12730.       anonymous = 1;
  12731.  
  12732. #ifdef FTP_SRP
  12733.     if (auth_type && !strcmp(auth_type, "SRP")) {
  12734.         user = srp_user;
  12735.         pass = srp_pass;
  12736.         acct = srp_acct;
  12737.     } else
  12738. #endif /* FTP_SRP */
  12739.       if (anonymous) {
  12740.           user = "anonymous";
  12741.           if (ftp_tmp) {
  12742.               pass = ftp_tmp;
  12743.           } else {
  12744.               ckmakmsg(tmp,256,whoami(),"@",myhost,NULL);
  12745.               pass = tmp;
  12746.           }
  12747.       } else {
  12748. #ifdef USE_RUSERPASS
  12749.           if (ruserpass(host, &user, &pass, &acct) < 0) {
  12750.               ftpcode = -1;
  12751.               return(0);
  12752.           }
  12753. #endif /* USE_RUSERPASS */
  12754.           if (ftp_logname) {
  12755.               user = ftp_logname;
  12756.               pass = ftp_tmp;
  12757.           } else if (uidbuf[0]) {
  12758.               user = uidbuf;
  12759.               if (ftp_tmp) {
  12760.                   pass = ftp_tmp;
  12761.               } else if (pwbuf[0] && pwflg) {
  12762.                   ckstrncpy(ftppass,pwbuf,sizeof(ftppass));
  12763. #ifdef OS2
  12764.                   if ( pwcrypt )
  12765.                       ck_encrypt((char *)ftppass);
  12766. #endif /* OS2 */
  12767.                   pass = ftppass;
  12768.               }
  12769.           }
  12770.           acct = ftp_acc;
  12771.           while (user == NULL) {
  12772.               char *myname, prompt[256];
  12773.  
  12774.               myname = whoami();
  12775.               if (myname)
  12776.                 ckmakxmsg(prompt,256," Name (",host,":",myname,"): ",
  12777.                           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  12778.               else
  12779.                 ckmakmsg(prompt,256," Name (",host,"): ",NULL);
  12780.               tmp[0] = '\0';
  12781.               readtext(prompt,tmp,80);
  12782.               if (*tmp == '\0')
  12783.                 user = myname;
  12784.               else
  12785.                 user = tmp;
  12786.           }
  12787.       }
  12788.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  12789.     if (n == REPLY_COMPLETE) {
  12790.         /* determine if we need to send a dummy password */
  12791.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  12792.           ftpcmd("PASS dummy",NULL,0,0,1);
  12793.     } else if (n == REPLY_CONTINUE) {
  12794. #ifdef CK_ENCRYPTION
  12795.         int oldftp_cpl;
  12796. #endif /* CK_ENCRYPTION */
  12797.  
  12798.         if (pass == NULL) {
  12799.             setint();
  12800.             readpass(" Password:",ftppass,80);
  12801.             pass = ftppass;
  12802.         }
  12803.  
  12804. #ifdef CK_ENCRYPTION
  12805.         oldftp_cpl = ftp_cpl;
  12806.         ftp_cpl = FPL_PRV;
  12807. #endif /* CK_ENCRYPTION */
  12808.         n = ftpcmd("PASS",pass,-1,-1,1);
  12809.         if (!anonymous && pass) {
  12810.             char * p = pass;
  12811.             while (*p++) *(p-1) = NUL;
  12812.             makestr(&ftp_tmp,NULL);
  12813.         }
  12814. #ifdef CK_ENCRYPTION
  12815.         /* level may have changed */
  12816.         if (ftp_cpl == FPL_PRV)
  12817.           ftp_cpl = oldftp_cpl;
  12818. #endif /* CK_ENCRYPTION */
  12819.     }
  12820.     if (n == REPLY_CONTINUE) {
  12821.         aflag++;
  12822.         if (acct == NULL) {
  12823.             static char ftpacct[80];
  12824.             setint();
  12825.             readpass(" Account:",ftpacct,80);
  12826.             acct = ftpacct;
  12827.         }
  12828.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12829.     }
  12830.     if (n != REPLY_COMPLETE) {
  12831.         fprintf(stderr, "FTP login failed.\n");
  12832.         if (haveurl)
  12833.           doexit(BAD_EXIT,-1);
  12834.         return(0);
  12835.     }
  12836.     if (!aflag && acct != NULL) {
  12837.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12838.     }
  12839.     makestr(&ftp_logname,user);
  12840.     loggedin = 1;
  12841.     ftp_init();
  12842.  
  12843.     if (anonymous && !quiet) {
  12844.         printf(" Logged in as anonymous (%s)\n",pass);
  12845.         memset(pass, 0, strlen(pass));
  12846.     }
  12847.     if (ftp_rdir) {
  12848.         if (doftpcwd(ftp_rdir,-1) < 1)
  12849.           doexit(BAD_EXIT,-1);
  12850.     }
  12851.  
  12852. #ifdef FTP_PROXY
  12853.     if (proxy)
  12854.       return(1);
  12855. #endif /* FTP_PROXY */
  12856.     return(1);
  12857. }
  12858.  
  12859. static int
  12860. ftp_reset() {
  12861.     int rc;
  12862. #ifdef BSDSELECT
  12863.     int nfnd = 1;
  12864.     fd_set mask;
  12865.     FD_ZERO(&mask);
  12866.     while (nfnd > 0) {
  12867.         FD_SET(csocket, &mask);
  12868.         if ((nfnd = empty(&mask,0)) < 0) {
  12869.             perror("reset");
  12870.             ftpcode = -1;
  12871.             lostpeer();
  12872.             return(0);
  12873.         } else if (nfnd) {
  12874.             getreply(0,-1,-1,ftp_vbm,0);
  12875.         }
  12876.     }
  12877. #else /* BSDSELECT */
  12878. #ifdef IBMSELECT
  12879.     int nfnd = 1;
  12880.     while (nfnd > 0) {
  12881.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  12882.             perror("reset");
  12883.             ftpcode = -1;
  12884.             lostpeer();
  12885.             return(0);
  12886.         } else if (nfnd) {
  12887.             getreply(0,-1,-1,ftp_vbm,0);
  12888.         }
  12889.     }
  12890. #endif /* IBMSELECT */
  12891. #endif /* BSDSELECT */
  12892.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  12893.     if (rc > 0)
  12894.       loggedin = 0;
  12895.     return(rc);
  12896. }
  12897.  
  12898. static int
  12899. ftp_rename(from, to) char * from, * to; {
  12900.     int lcs = -1, rcs = -1;
  12901. #ifndef NOCSETS
  12902.     if (ftp_xla) {
  12903.         lcs = ftp_csl;
  12904.         if (lcs < 0) lcs = fcharset;
  12905.         rcs = ftp_csx;
  12906.         if (rcs < 0) rcs = ftp_csr;
  12907.     }
  12908. #endif /* NOCSETS */
  12909.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  12910.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  12911.     }
  12912.     return(0);                          /* Failure */
  12913. }
  12914.  
  12915. static int
  12916. ftp_umask(mask) char * mask; {
  12917.     int rc;
  12918.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  12919.     return(rc);
  12920. }
  12921.  
  12922. static int
  12923. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  12924.     int n = 0, aflag = 0;
  12925.     char pwd[80];
  12926.  
  12927.     if (!auth_type && ftp_aut) {
  12928. #ifdef FTP_SRP
  12929.         if (ck_srp_is_installed()) {
  12930.             if (srp_ftp_auth( NULL, user, pass)) {
  12931.                 makestr(&pass,srp_pass);
  12932.             }
  12933.         }
  12934. #endif /* FTP_SRP */
  12935.     }
  12936.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  12937.     if (n == REPLY_COMPLETE)
  12938.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  12939.     else if (n == REPLY_CONTINUE) {
  12940. #ifdef CK_ENCRYPTION
  12941.         int oldftp_cpl;
  12942. #endif /* CK_ENCRYPTION */
  12943.         if (pass == NULL || !pass[0]) {
  12944.             pwd[0] = '\0';
  12945.             setint();
  12946.             readpass(" Password: ",pwd,sizeof(pwd));
  12947.             pass = pwd;
  12948.         }
  12949.  
  12950. #ifdef CK_ENCRYPTION
  12951.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  12952.           ftp_cpl = PROT_P;
  12953. #endif /* CK_ENCRYPTION */
  12954.         n = ftpcmd("PASS",pass,-1,-1,1);
  12955.         memset(pass, 0, strlen(pass));
  12956. #ifdef CK_ENCRYPTION
  12957.         /* level may have changed */
  12958.         if (ftp_cpl == PROT_P)
  12959.           ftp_cpl = oldftp_cpl;
  12960. #endif /* CK_ENCRYPTION */
  12961.     }
  12962.     if (n == REPLY_CONTINUE) {
  12963.         if (acct == NULL || !acct[0]) {
  12964.             pwd[0] = '\0';
  12965.             setint();
  12966.             readpass(" Account: ",pwd,sizeof(pwd));
  12967.             acct = pwd;
  12968.         }
  12969.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12970.         aflag++;
  12971.     }
  12972.     if (n != REPLY_COMPLETE) {
  12973.         printf("Login failed.\n");
  12974.         return(0);
  12975.     }
  12976.     if (!aflag && acct != NULL && acct[0]) {
  12977.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  12978.     }
  12979.     if (n == REPLY_COMPLETE) {
  12980.         makestr(&ftp_logname,user);
  12981.         loggedin = 1;
  12982.         ftp_init();
  12983.         return(1);
  12984.     }
  12985.     return(0);
  12986. }
  12987.  
  12988. char *
  12989. ftp_authtype() {
  12990.     if (!connected)
  12991.       return("NULL");
  12992.     return(auth_type ? auth_type : "NULL");
  12993. }
  12994.  
  12995. char *
  12996. ftp_cpl_mode() {
  12997.     switch (ftp_cpl) {
  12998.       case FPL_CLR:
  12999.         return("clear");
  13000.       case FPL_SAF:
  13001.         return("safe");
  13002.       case FPL_PRV:
  13003.         return("private");
  13004.       case FPL_CON:
  13005.         return("confidential");
  13006.       default:
  13007.         return("(error)");
  13008.     }
  13009. }
  13010.  
  13011. char *
  13012. ftp_dpl_mode() {
  13013.     switch (ftp_dpl) {
  13014.       case FPL_CLR:
  13015.         return("clear");
  13016.       case FPL_SAF:
  13017.         return("safe");
  13018.       case FPL_PRV:
  13019.         return("private");
  13020.       case FPL_CON:
  13021.         return("confidential");
  13022.       default:
  13023.         return("(error)");
  13024.     }
  13025. }
  13026.  
  13027. #ifdef NOT_USED
  13028. /*
  13029.   The following code is from the Unix FTP client.  Be sure to
  13030.   make sure that the functionality is not lost.  Especially
  13031.   the Proxy stuff even though we have not yet implemented it.
  13032. */
  13033.  
  13034. /* Send multiple files  */
  13035.  
  13036. static int
  13037. ftp_mput(argc, argv) int argc; char **argv; {
  13038.     register int i;
  13039.     sig_t oldintr;
  13040.     int ointer;
  13041.     char *tp;
  13042.     sigtype mcancel();
  13043.  
  13044.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  13045.         printf("usage: %s local-files\n", argv[0]);
  13046.         ftpcode = -1;
  13047.         return;
  13048.     }
  13049.     mname = argv[0];
  13050.     mflag = 1;
  13051.     oldintr = signal(SIGINT, mcancel);
  13052.  
  13053.     /* Replace with calls to cc_execute() */
  13054.     setjmp(jcancel);
  13055. #ifdef FTP_PROXY
  13056.     if (proxy) {
  13057.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  13058.  
  13059.         while ((cp = remglob(argv,0)) != NULL) {
  13060.             if (*cp == 0) {
  13061.                 mflag = 0;
  13062.                 continue;
  13063.             }
  13064.             if (mflag && confirm(argv[0], cp)) {
  13065.                 tp = cp;
  13066.                 if (mcase) {
  13067.                     while (*tp && !islower(*tp)) {
  13068.                         tp++;
  13069.                     }
  13070.                     if (!*tp) {
  13071.                         tp = cp;
  13072.                         tp2 = tmpbuf;
  13073.                         while ((*tp2 = *tp) != 0) {
  13074.                             if (isupper(*tp2)) {
  13075.                                 *tp2 = 'a' + *tp2 - 'A';
  13076.                             }
  13077.                             tp++;
  13078.                             tp2++;
  13079.                         }
  13080.                     }
  13081.                     tp = tmpbuf;
  13082.                 }
  13083.                 if (ntflag) {
  13084.                     tp = dotrans(tp);
  13085.                 }
  13086.                 if (mapflag) {
  13087.                     tp = domap(tp);
  13088.                 }
  13089.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  13090.                 if (!mflag && fromatty) {
  13091.                     ointer = interactive;
  13092.                     interactive = 1;
  13093.                     if (confirm("Continue with","mput")) {
  13094.                         mflag++;
  13095.                     }
  13096.                     interactive = ointer;
  13097.                 }
  13098.             }
  13099.         }
  13100.         signal(SIGINT, oldintr);
  13101.         mflag = 0;
  13102.         return;
  13103.     }
  13104. #endif /* FTP_PROXY */
  13105.     for (i = 1; i < argc; i++) {
  13106.         register char **cpp, **gargs;
  13107.  
  13108.         if (mflag && confirm(argv[0], argv[i])) {
  13109.             tp = argv[i];
  13110.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  13111.             if (!mflag && fromatty) {
  13112.                 ointer = interactive;
  13113.                 interactive = 1;
  13114.                 if (confirm("Continue with","mput")) {
  13115.                     mflag++;
  13116.                 }
  13117.                 interactive = ointer;
  13118.             }
  13119.         }
  13120.         continue;
  13121.  
  13122.         gargs = ftpglob(argv[i]);
  13123.         if (globerr != NULL) {
  13124.             printf("%s\n", globerr);
  13125.             if (gargs) {
  13126.                 blkfree(gargs);
  13127.                 free((char *)gargs);
  13128.             }
  13129.             continue;
  13130.         }
  13131.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  13132.             if (mflag && confirm(argv[0], *cpp)) {
  13133.                 tp = *cpp;
  13134.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  13135.                 if (!mflag && fromatty) {
  13136.                     ointer = interactive;
  13137.                     interactive = 1;
  13138.                     if (confirm("Continue with","mput")) {
  13139.                         mflag++;
  13140.                     }
  13141.                     interactive = ointer;
  13142.                 }
  13143.             }
  13144.         }
  13145.         if (gargs != NULL) {
  13146.             blkfree(gargs);
  13147.             free((char *)gargs);
  13148.         }
  13149.     }
  13150.     signal(SIGINT, oldintr);
  13151.     mflag = 0;
  13152. }
  13153.  
  13154. /* Get multiple files */
  13155.  
  13156. static int
  13157. ftp_mget(argc, argv) int argc; char **argv; {
  13158.     int rc = -1;
  13159.     sig_t oldintr;
  13160.     int ointer;
  13161.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  13162.     sigtype mcancel();
  13163.  
  13164.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  13165.         printf("usage: %s remote-files\n", argv[0]);
  13166.         ftpcode = -1;
  13167.         return(-1);
  13168.     }
  13169.     mname = argv[0];
  13170.     mflag = 1;
  13171.     oldintr = signal(SIGINT,mcancel);
  13172.     /* Replace with calls to cc_execute() */
  13173.     setjmp(jcancel);
  13174.     while ((cp = remglob(argv,proxy)) != NULL) {
  13175.         if (*cp == '\0') {
  13176.             mflag = 0;
  13177.             continue;
  13178.         }
  13179.         if (mflag && confirm(argv[0], cp)) {
  13180.             tp = cp;
  13181.             if (mcase) {
  13182.                 while (*tp && !islower(*tp)) {
  13183.                     tp++;
  13184.                 }
  13185.                 if (!*tp) {
  13186.                     tp = cp;
  13187.                     tp2 = tmpbuf;
  13188.                     while ((*tp2 = *tp) != 0) {
  13189.                         if (isupper(*tp2)) {
  13190.                             *tp2 = 'a' + *tp2 - 'A';
  13191.                         }
  13192.                         tp++;
  13193.                         tp2++;
  13194.                     }
  13195.                 }
  13196.                 tp = tmpbuf;
  13197.             }
  13198.             rc = (recvrequest("RETR", tp, cp, "wb",
  13199.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  13200.             if (!mflag && fromatty) {
  13201.                 ointer = interactive;
  13202.                 interactive = 1;
  13203.                 if (confirm("Continue with","mget")) {
  13204.                     mflag++;
  13205.                 }
  13206.                 interactive = ointer;
  13207.             }
  13208.         }
  13209.     }
  13210.     signal(SIGINT,oldintr);
  13211.     mflag = 0;
  13212.     return(rc);
  13213. }
  13214.  
  13215. /* Delete multiple files */
  13216.  
  13217. static int
  13218. mdelete(argc, argv) int argc; char **argv; {
  13219.     sig_t oldintr;
  13220.     int ointer;
  13221.     char *cp;
  13222.     sigtype mcancel();
  13223.  
  13224.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  13225.         printf("usage: %s remote-files\n", argv[0]);
  13226.         ftpcode = -1;
  13227.         return(-1);
  13228.     }
  13229.     mname = argv[0];
  13230.     mflag = 1;
  13231.     oldintr = signal(SIGINT, mcancel);
  13232.     /* Replace with calls to cc_execute() */
  13233.     setjmp(jcancel);
  13234.     while ((cp = remglob(argv,0)) != NULL) {
  13235.         if (*cp == '\0') {
  13236.             mflag = 0;
  13237.             continue;
  13238.         }
  13239.         if (mflag && confirm(argv[0], cp)) {
  13240.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  13241.             if (!mflag && fromatty) {
  13242.                 ointer = interactive;
  13243.                 interactive = 1;
  13244.                 if (confirm("Continue with", "mdelete")) {
  13245.                     mflag++;
  13246.                 }
  13247.                 interactive = ointer;
  13248.             }
  13249.         }
  13250.     }
  13251.     signal(SIGINT, oldintr);
  13252.     mflag = 0;
  13253.     return(rc);
  13254. }
  13255.  
  13256. /* Get a directory listing of multiple remote files */
  13257.  
  13258. static int
  13259. mls(argc, argv) int argc; char **argv; {
  13260.     sig_t oldintr;
  13261.     int ointer, i;
  13262.     char *cmd, mode[1], *dest;
  13263.     sigtype mcancel();
  13264.     int rc = -1;
  13265.  
  13266.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  13267.       goto usage;
  13268.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  13269.       usage:
  13270.         printf("usage: %s remote-files local-file\n", argv[0]);
  13271.         ftpcode = -1;
  13272.         return(-1);
  13273.     }
  13274.     dest = argv[argc - 1];
  13275.     argv[argc - 1] = NULL;
  13276.     if (strcmp(dest, "-") && *dest != '|')
  13277.       if (!globulize(&dest) ||
  13278.           !confirm("output to local-file:", dest)) {
  13279.           ftpcode = -1;
  13280.           return(-1);
  13281.       }
  13282.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  13283.     mname = argv[0];
  13284.     mflag = 1;
  13285.     oldintr = signal(SIGINT, mcancel);
  13286.     /* Replace with calls to cc_execute() */
  13287.     setjmp(jcancel);
  13288.     for (i = 1; mflag && i < argc-1; ++i) {
  13289.         *mode = (i == 1) ? 'w' : 'a';
  13290.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  13291.         if (!mflag && fromatty) {
  13292.             ointer = interactive;
  13293.             interactive = 1;
  13294.             if (confirm("Continue with", argv[0])) {
  13295.                 mflag ++;
  13296.             }
  13297.             interactive = ointer;
  13298.         }
  13299.     }
  13300.     signal(SIGINT, oldintr);
  13301.     mflag = 0;
  13302.     return(rc);
  13303. }
  13304.  
  13305. static char *
  13306. remglob(argv,doswitch) char *argv[]; int doswitch; {
  13307.     char temp[16];
  13308.     static char buf[CKMAXPATH];
  13309.     static FILE *ftemp = NULL;
  13310.     static char **args;
  13311.     int oldhash;
  13312.     char *cp, *mode;
  13313.  
  13314.     if (!mflag) {
  13315.         if (!doglob) {
  13316.             args = NULL;
  13317.         } else {
  13318.             if (ftemp) {
  13319.                 (void) fclose(ftemp);
  13320.                 ftemp = NULL;
  13321.             }
  13322.         }
  13323.         return(NULL);
  13324.     }
  13325.     if (!doglob) {
  13326.         if (args == NULL)
  13327.           args = argv;
  13328.         if ((cp = *++args) == NULL)
  13329.           args = NULL;
  13330.         return(cp);
  13331.     }
  13332.     if (ftemp == NULL) {
  13333.         (void) strcpy(temp, _PATH_TMP);
  13334. #ifdef MKTEMP
  13335. #ifndef MKSTEMP
  13336.         (void) mktemp(temp);
  13337. #endif /* MKSTEMP */
  13338. #endif /* MKTEMP */
  13339.         verbose = 0;
  13340.         oldhash = hash, hash = 0;
  13341. #ifdef FTP_PROXY
  13342.         if (doswitch) {
  13343.             pswitch(!proxy);
  13344.         }
  13345. #endif /* FTP_PROXY */
  13346.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  13347.           recvrequest ("NLST", temp, *argv, mode, 0);
  13348. #ifdef FTP_PROXY
  13349.         if (doswitch) {
  13350.             pswitch(!proxy);
  13351.         }
  13352. #endif /* FTP_PROXY */
  13353.         hash = oldhash;
  13354.         ftemp = fopen(temp, "r");
  13355.         unlink(temp);
  13356.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  13357.             printf("Can't find list of remote files, oops\n");
  13358.             return(NULL);
  13359.         }
  13360.     }
  13361.     if (fgets(buf, sizeof (buf), ftemp) == NULL) {
  13362.         fclose(ftemp), ftemp = NULL;
  13363.         return(NULL);
  13364.     }
  13365.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  13366.       *cp = '\0';
  13367.     return(buf);
  13368. }
  13369. #endif /* NOT_USED */
  13370.  
  13371. /* remote_files() */
  13372. /*
  13373.    Returns next remote filename on success;
  13374.    NULL on error or no more files with global rfrc set to:
  13375.      -1: Bad argument
  13376.      -2: Server error response to NLST, e.g. file not found
  13377.      -3: No more files
  13378.      -9: Internal error
  13379. */
  13380. static CHAR *
  13381. #ifdef CK_ANSIC
  13382. remote_files(int new_query, CHAR * pattern, int proxy_switch)
  13383. #else /* CK_ANSIC */
  13384. remote_files(new_query, pattern, proxy_switch)
  13385.     int new_query;
  13386.     CHAR * pattern;
  13387.     int proxy_switch;
  13388. #endif /* CK_ANSIC */
  13389. /* remote_files */ {
  13390.     static CHAR temp[CKMAXPATH];
  13391.     static CHAR buf[CKMAXPATH];
  13392.     static FILE *ftemp = NULL;
  13393.     CHAR *cp;
  13394.     int x;
  13395.     int lcs = 0, rcs = 0, xlate = 0;
  13396.  
  13397.     debug(F111,"ftp remote_files",pattern,new_query);
  13398.     rfrc = -1;
  13399.     if (new_query) {
  13400.         if (!pattern) pattern = (CHAR *)"";
  13401.         if (!*pattern) return(NULL);
  13402.     }
  13403.     if (new_query) {
  13404.         if (ftemp) {
  13405.             fclose(ftemp);
  13406.             ftemp = NULL;
  13407. #ifdef OS2
  13408.             if (!ftp_knf)
  13409.               unlink((char *)temp);
  13410. #endif /* OS2 */
  13411.         }
  13412.     }
  13413.     if (ftemp == NULL) {
  13414.         extern char * tempdir;
  13415.         char * p;
  13416.         debug(F110,"ftp remote_files tempdir",tempdir,0);
  13417.         if (tempdir) {
  13418.             p = tempdir;
  13419.         } else {
  13420. #ifdef OS2
  13421. #ifdef NT
  13422.             p = getenv("K95TMP");
  13423. #else
  13424.             p = getenv("K2TMP");
  13425. #endif /* NT */
  13426.             if (!p)
  13427. #endif /* OS2 */
  13428.               p = getenv("CK_TMP");
  13429.             if (!p)
  13430.               p = getenv("TMPDIR");
  13431.             if (!p) p = getenv("TEMP");
  13432.             if (!p) p = getenv("TMP");
  13433. #ifdef OS2ORUNIX
  13434.             if (p) {
  13435.                 int len = strlen(p);
  13436.                 if (p[len-1] != '/'
  13437. #ifdef OS2
  13438.                     && p[len-1] != '\\'
  13439. #endif /* OS2 */
  13440.                      ) {
  13441.                     static char foo[CKMAXPATH];
  13442.                     ckstrncpy(foo,p,CKMAXPATH);
  13443.                     ckstrncat(foo,"/",CKMAXPATH);
  13444.                     p = foo;
  13445.                 }
  13446.             } else
  13447. #else /* OS2ORUNIX */
  13448.             if (!p)
  13449. #endif /* OS2ORUNIX */
  13450. #ifdef UNIX                             /* Systems that have a standard */
  13451.                 p = "/tmp/";            /* temporary directory... */
  13452. #else
  13453. #ifdef datageneral
  13454.             p = ":TMP:";
  13455. #else
  13456.             p = "";
  13457. #endif /* datageneral */
  13458. #endif /* UNIX */
  13459.         }
  13460.         debug(F110,"ftp remote_files p",p,0);
  13461.         ckmakmsg((char *)temp,CKMAXPATH,p,"ckXXXXXX",NULL,NULL);
  13462.         debug(F110,"ftp remote_files temp",temp,0);
  13463. #ifdef MKTEMP
  13464. #ifdef MKSTEMP
  13465.         mkstemp((char *)temp);
  13466. #else
  13467.         mktemp((char *)temp);
  13468. #endif /* MKSTEMP */
  13469.         /* if no mktemp() the name will just be "ckXXXXXX"... */
  13470. #endif /* MKTEMP */
  13471.  
  13472. #ifdef FTP_PROXY
  13473.         if (proxy_switch) {
  13474.             pswitch(!proxy);
  13475.         }
  13476. #endif /* FTP_PROXY */
  13477.  
  13478. #ifndef NOCSETS
  13479.         xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
  13480.         if (xlate) {                    /* ON? */
  13481.             lcs = ftp_csl;              /* Local charset */
  13482.             if (lcs < 0) lcs = fcharset;
  13483.             if (lcs < 0) xlate = 0;
  13484.         }
  13485.         if (xlate) {                    /* Still ON? */
  13486.             rcs = ftp_csx;              /* Remote (Server) charset */
  13487.             if (rcs < 0) rcs = ftp_csr;
  13488.             if (rcs < 0) xlate = 0;
  13489.         }
  13490. #endif /* NOCSETS */
  13491.  
  13492.         x = recvrequest("NLST",
  13493.                         (char *)temp,
  13494.                         (char *)pattern,
  13495.                         "wb",
  13496.                         0,
  13497.                         0,
  13498.                         NULL,
  13499.                         xlate,
  13500.                         lcs,
  13501.                         rcs
  13502.                         );
  13503.         if (x < 0) {
  13504.             rfrc = -2;
  13505.             return(NULL);
  13506.         }
  13507. #ifdef FTP_PROXY
  13508.         if (proxy_switch) {
  13509.             pswitch(!proxy);
  13510.         }
  13511. #endif /* FTP_PROXY */
  13512.         ftemp = fopen((char *)temp, "r");
  13513. #ifndef OS2
  13514.         if (!ftp_knf)
  13515.           unlink((char *)temp);
  13516. #endif /* OS2 */
  13517.         if (!ftemp) {
  13518.             debug(F110,"ftp remote_files open fail",temp,0);
  13519.             if ((!dpyactive || ftp_deb))
  13520.               printf("?Can't find list of remote files, oops\n");
  13521.             rfrc = -9;
  13522.             return(NULL);
  13523.         }
  13524.     }
  13525.     if (fgets((char *)buf, sizeof (buf), ftemp) == NULL) {
  13526.         fclose(ftemp);
  13527.         ftemp = NULL;
  13528. #ifdef OS2
  13529.         if (!ftp_knf)
  13530.           unlink((char *)temp);
  13531. #endif /* OS2 */
  13532.         if (ftp_knf) {
  13533.             printf("(Temporary file %s NOT deleted)\n",(char *)temp);
  13534.         }
  13535.         rfrc = -3;
  13536.         return(NULL);
  13537.     }
  13538.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  13539.       *cp = '\0';
  13540.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  13541.       *cp = '\0';
  13542.     rfrc = 0;
  13543.     return(buf);
  13544. }
  13545.  
  13546. /* N O T  P O R T A B L E !!! */
  13547.  
  13548. #if (SIZEOF_SHORT == 4)
  13549. typedef unsigned short ftp_uint32;
  13550. typedef short ftp_int32;
  13551. #else
  13552. #if (SIZEOF_INT == 4)
  13553. typedef unsigned int ftp_uint32;
  13554. typedef int ftp_int32;
  13555. #else
  13556. #if (SIZEOF_LONG == 4)
  13557. typedef ULONG ftp_uint32;
  13558. typedef long ftp_int32;
  13559. #endif
  13560. #endif
  13561. #endif
  13562.  
  13563. /* Perhaps use these in general, certainly use them for GSSAPI */
  13564.  
  13565. #ifndef looping_write
  13566. #define ftp_int32 int
  13567. #define ftp_uint32 unsigned int
  13568. static int
  13569. looping_write(fd, buf, len)
  13570.     int fd;
  13571.     register CONST char *buf;
  13572.     int len;
  13573. {
  13574.     int cc;
  13575.     register int wrlen = len;
  13576.     do {
  13577.         cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  13578.         if (cc < 0) {
  13579.             if (errno == EINTR)
  13580.               continue;
  13581.             return(cc);
  13582.         } else {
  13583.             buf += cc;
  13584.             wrlen -= cc;
  13585.         }
  13586.     } while (wrlen > 0);
  13587.     return(len);
  13588. }
  13589. #endif
  13590. #ifndef looping_read
  13591. static int
  13592. looping_read(fd, buf, len)
  13593.     int fd;
  13594.     register char *buf;
  13595.     register int len;
  13596. {
  13597.     int cc, len2 = 0;
  13598.  
  13599.     do {
  13600.         cc = recv(fd, (char *)buf, len,0);
  13601.         if (cc < 0) {
  13602.             if (errno == EINTR)
  13603.               continue;
  13604.             return(cc);                 /* errno is already set */
  13605.         } else if (cc == 0) {
  13606.             return(len2);
  13607.         } else {
  13608.             buf += cc;
  13609.             len2 += cc;
  13610.             len -= cc;
  13611.         }
  13612.     } while (len > 0);
  13613.     return(len2);
  13614. }
  13615. #endif /* looping_read */
  13616.  
  13617. #define ERR -2
  13618.  
  13619. #ifdef COMMENT
  13620. static
  13621. secure_putbyte(fd, c) int fd; CHAR c; {
  13622.     int ret;
  13623.  
  13624.     ucbuf[nout++] = c;
  13625.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  13626.         nout = 0;
  13627.         if (ftp_dpl == FPL_CLR)
  13628.           ret = send(fd, (SENDARG2TYPE)ucbuf,
  13629.                      (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  13630.         else
  13631.           ret = secure_putbuf(fd,
  13632.                               ucbuf,
  13633.                               (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  13634.                               );
  13635.         return(ret?ret:c);
  13636.     }
  13637.     return(c);
  13638. }
  13639. #endif /* COMMENT */
  13640.  
  13641. /* returns:
  13642.  *       0  on success
  13643.  *      -1  on error (errno set)
  13644.  *      -2  on security error
  13645.  */
  13646. static int
  13647. secure_flush(fd) int fd; {
  13648.     int rc = 0;
  13649.     int len = 0;
  13650.  
  13651.     if (nout > 0) {
  13652.         len = nout;
  13653.         if (ftp_dpl == FPL_CLR) {
  13654.             rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  13655.             nout = 0;
  13656.             goto xflush;
  13657.         } else {
  13658.             rc = secure_putbuf(fd, ucbuf, nout);
  13659.             if (rc)
  13660.               goto xflush;
  13661.         }
  13662.     }
  13663.     rc = (ftp_dpl == FPL_CLR) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  13664.  
  13665.   xflush:
  13666.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  13667.         spktl = len;
  13668.         ftscreen(SCR_PT,'D',spackets,NULL);
  13669.     }
  13670.     return(rc);
  13671. }
  13672.  
  13673. #ifdef COMMENT                          /* (not used) */
  13674. /* returns:
  13675.  *      c>=0  on success
  13676.  *      -1    on error
  13677.  *      -2    on security error
  13678.  */
  13679. static int
  13680. #ifdef CK_ANSIC
  13681. secure_putc(char c, int fd)
  13682. #else
  13683. secure_putc(c, fd) char c; int fd;
  13684. #endif /* CK_ANSIC */
  13685. /* secure_putc */ {
  13686.     return(secure_putbyte(fd, (CHAR) c));
  13687. }
  13688. #endif /* COMMENT */
  13689.  
  13690. /* returns:
  13691.  *      nbyte on success
  13692.  *      -1  on error (errno set)
  13693.  *      -2  on security error
  13694.  */
  13695. static int
  13696. #ifdef CK_ANSIC
  13697. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  13698. #else
  13699. secure_write(fd, buf, nbyte)
  13700.     int fd;
  13701.     CHAR * buf;
  13702.     unsigned int nbyte;
  13703. #endif /* CK_ANSIC */
  13704. {
  13705.     int ret;
  13706.  
  13707.     if (ftp_dpl == FPL_CLR) {
  13708.         if (nout > 0) {
  13709.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  13710.               return(ret);
  13711.             nout = 0;
  13712.         }
  13713.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  13714.     } else {
  13715.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  13716.         int bsent = 0;
  13717.  
  13718.         while (bsent < nbyte) {
  13719.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  13720.                         (ucbuflen - nout) : (nbyte - bsent));
  13721.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  13722.             nout += b2cp;
  13723.             bsent += b2cp;
  13724.  
  13725.             if (nout == ucbuflen) {
  13726.                 nout = 0;
  13727.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  13728.                 if (ret < 0)
  13729.                   return(ret);
  13730.             }
  13731.         }
  13732.         return(bsent);
  13733.     }
  13734. }
  13735.  
  13736. /* returns:
  13737.  *       0  on success
  13738.  *      -1  on error (errno set)
  13739.  *      -2  on security error
  13740.  */
  13741. static int
  13742. #ifdef CK_ANSIC
  13743. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  13744. #else
  13745. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  13746. #endif /* CK_ANSIC */
  13747. {
  13748.     static char *outbuf = NULL;         /* output ciphertext */
  13749. #ifdef FTP_SECURITY
  13750.     static unsigned int bufsize = 0;    /* size of outbuf */
  13751. #endif /* FTP_SECURITY */
  13752.     ftp_int32 length   = 0;
  13753.     ftp_uint32 net_len = 0;
  13754.  
  13755.     /* Other auth types go here ... */
  13756. #ifdef CK_SSL
  13757.     if (ssl_ftp_data_active_flag) {
  13758.         int count, error;
  13759.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  13760.         error = SSL_get_error(ssl_ftp_data_con,count);
  13761.         switch (error) {
  13762.           case SSL_ERROR_NONE:
  13763.             return(0);
  13764.           case SSL_ERROR_WANT_WRITE:
  13765.           case SSL_ERROR_WANT_READ:
  13766.           case SSL_ERROR_SYSCALL:
  13767. #ifdef NT
  13768.             {
  13769.                 int gle = GetLastError();
  13770.             }
  13771. #endif /* NT */
  13772.           case SSL_ERROR_WANT_X509_LOOKUP:
  13773.           case SSL_ERROR_SSL:
  13774.           case SSL_ERROR_ZERO_RETURN:
  13775.           default:
  13776.             SSL_shutdown(ssl_ftp_data_con);
  13777.             SSL_free(ssl_ftp_data_con);
  13778.             ssl_ftp_data_active_flag = 0;
  13779.             ssl_ftp_data_con = NULL;
  13780. #ifdef TCPIPLIB
  13781.             socket_close(data);
  13782. #else /* TCPIPLIB */
  13783. #ifdef USE_SHUTDOWN
  13784.             shutdown(data, 1+1);
  13785. #endif /* USE_SHUTDOWN */
  13786.             close(data);
  13787. #endif /* TCPIPLIB */
  13788.             data = -1;
  13789.             globaldin = data;
  13790.             return(-1);
  13791.         }
  13792.         return(-1);
  13793.     }
  13794. #endif /* CK_SSL */
  13795.  
  13796. #ifdef FTP_SRP
  13797.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  13798.         if (bufsize < nbyte + FUDGE_FACTOR) {
  13799.             if (outbuf?
  13800.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  13801.                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  13802.                 bufsize = nbyte + FUDGE_FACTOR;
  13803.             } else {
  13804.                 bufsize = 0;
  13805.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  13806.                 return(ERR);
  13807.             }
  13808.         }
  13809.         if ((length =
  13810.              srp_encode(ftp_dpl == FPL_PRV,
  13811.                         (CHAR *) buf,
  13812.                         (CHAR *) outbuf,
  13813.                         nbyte
  13814.                         )
  13815.              ) < 0) {
  13816.             secure_error ("srp_encode failed");
  13817.             return ERR;
  13818.         }
  13819.     }
  13820. #endif /* FTP_SRP */
  13821. #ifdef FTP_KRB4
  13822.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  13823.         struct sockaddr_in myaddr, hisaddr;
  13824.         GSOCKNAME_T len;
  13825.         len = sizeof(myaddr);
  13826.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  13827.             secure_error("secure_putbuf: getsockname failed");
  13828.             return(ERR);
  13829.         }
  13830.         len = sizeof(hisaddr);
  13831.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  13832.             secure_error("secure_putbuf: getpeername failed");
  13833.             return(ERR);
  13834.         }
  13835.         if (bufsize < nbyte + FUDGE_FACTOR) {
  13836.             if (outbuf ?
  13837.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  13838.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  13839.                 bufsize = nbyte + FUDGE_FACTOR;
  13840.             } else {
  13841.                 bufsize = 0;
  13842.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  13843.                 return(ERR);
  13844.             }
  13845.         }
  13846.         if (ftp_dpl == FPL_PRV) {
  13847.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  13848.                                  ftp_sched,
  13849. #ifdef KRB524
  13850.                                  ftp_cred.session,
  13851. #else /* KRB524 */
  13852.                                  &ftp_cred.session,
  13853. #endif /* KRB524 */
  13854.                                  &myaddr,
  13855.                                  &hisaddr
  13856.                                  );
  13857.         } else {
  13858.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  13859. #ifdef KRB524
  13860.                                  ftp_cred.session,
  13861. #else /* KRB524 */
  13862.                                  &ftp_cred.session,
  13863. #endif /* KRB524 */
  13864.                                  &myaddr,
  13865.                                  &hisaddr
  13866.                                  );
  13867.         }
  13868.         if (length == -1) {
  13869.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  13870.                          ftp_dpl == FPL_PRV ? "priv" : "safe");
  13871.             return(ERR);
  13872.         }
  13873.     }
  13874. #endif /* FTP_KRB4 */
  13875. #ifdef FTP_GSSAPI
  13876.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  13877.         gss_buffer_desc in_buf, out_buf;
  13878.         OM_uint32 maj_stat, min_stat;
  13879.         int conf_state;
  13880.  
  13881.         in_buf.value = buf;
  13882.         in_buf.length = nbyte;
  13883.         maj_stat = gss_seal(&min_stat, gcontext,
  13884.                             (ftp_dpl == FPL_PRV), /* confidential */
  13885.                             GSS_C_QOP_DEFAULT,
  13886.                             &in_buf,
  13887.                             &conf_state,
  13888.                             &out_buf
  13889.                             );
  13890.         if (maj_stat != GSS_S_COMPLETE) {
  13891.             /* generally need to deal */
  13892.             /* ie. should loop, but for now just fail */
  13893.             user_gss_error(maj_stat, min_stat,
  13894.                            ftp_dpl == FPL_PRV?
  13895.                            "GSSAPI seal failed":
  13896.                            "GSSAPI sign failed");
  13897.             return(ERR);
  13898.         }
  13899.         if (bufsize < out_buf.length) {
  13900.             if (outbuf ?
  13901.                 (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  13902.                 (outbuf = malloc((unsigned) out_buf.length))) {
  13903.                 bufsize = out_buf.length;
  13904.             } else {
  13905.                 bufsize = 0;
  13906.                 secure_error("%s (in malloc of PROT buffer)",
  13907.                              ck_errstr());
  13908.                 return(ERR);
  13909.             }
  13910.         }
  13911.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  13912.         gss_release_buffer(&min_stat, &out_buf);
  13913.     }
  13914. #endif /* FTP_GSSAPI */
  13915.     net_len = htonl((ULONG) length);
  13916.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  13917.       return(-1);
  13918.     if (looping_write(fd, outbuf, length) != length)
  13919.       return(-1);
  13920.     return(0);
  13921. }
  13922.  
  13923. static int
  13924. secure_getbyte(fd) int fd; {
  13925.     /* number of chars in ucbuf, pointer into ucbuf */
  13926.     static unsigned int nin = 0, bufp = 0;
  13927.     int kerror;
  13928.     ftp_uint32 length;
  13929.  
  13930.     if (nin == 0) {
  13931.         if (iscanceled())
  13932.           return(0);
  13933. #ifdef CK_SSL
  13934.         if (ssl_ftp_data_active_flag) {
  13935.             int count, error;
  13936.             count = SSL_read(ssl_ftp_data_con, ucbuf, sizeof(ucbuf));
  13937.             error = SSL_get_error(ssl_ftp_data_con,count);
  13938.             switch (error) {
  13939.               case SSL_ERROR_NONE:
  13940.                 nin = bufp = count;
  13941.                 rpackets++;
  13942.                 pktnum++;
  13943.                 if (fdispla != XYFD_B) {
  13944.                     rpktl = count;
  13945.                     ftscreen(SCR_PT,'D',rpackets,NULL);
  13946.                 }
  13947.                 break;
  13948.               case SSL_ERROR_WANT_WRITE:
  13949.               case SSL_ERROR_WANT_READ:
  13950.               case SSL_ERROR_SYSCALL:
  13951. #ifdef NT
  13952.                 {
  13953.                     int gle = GetLastError();
  13954.                 }
  13955. #endif /* NT */
  13956.               case SSL_ERROR_WANT_X509_LOOKUP:
  13957.               case SSL_ERROR_SSL:
  13958.               case SSL_ERROR_ZERO_RETURN:
  13959.               default:
  13960.                 nin = bufp = count = 0;
  13961.                 SSL_shutdown(ssl_ftp_data_con);
  13962.                 SSL_free(ssl_ftp_data_con);
  13963.                 ssl_ftp_data_active_flag = 0;
  13964.                 ssl_ftp_data_con = NULL;
  13965. #ifdef TCPIPLIB
  13966.                 socket_close(data);
  13967. #else /* TCPIPLIB */
  13968. #ifdef USE_SHUTDOWN
  13969.                 shutdown(data, 1+1);
  13970. #endif /* USE_SHUTDOWN */
  13971.                 close(data);
  13972. #endif /* TCPIPLIB */
  13973.                 data = -1;
  13974.                 globaldin = data;
  13975.                 break;
  13976.             }
  13977.         } else
  13978. #endif /* CK_SSL */
  13979.           {
  13980.               kerror = looping_read(fd, (char *)&length, sizeof(length));
  13981.               if (kerror != sizeof(length)) {
  13982.                   secure_error("Couldn't read PROT buffer length: %d/%s",
  13983.                                kerror,
  13984.                                kerror == -1 ? ck_errstr()
  13985.                                : "premature EOF"
  13986.                                );
  13987.                   return(ERR);
  13988.               }
  13989.               debug(F101,"secure_getbyte length","",length);
  13990.               debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  13991.  
  13992.               length = (ULONG) ntohl(length);
  13993.               if (length > maxbuf) {
  13994.                   secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  13995.                                length,
  13996.                                maxbuf
  13997.                                );
  13998.                   return(ERR);
  13999.               }
  14000.               if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  14001.                   secure_error("Couldn't read %u byte PROT buffer: %s",
  14002.                                length,
  14003.                                kerror == -1 ? ck_errstr() : "premature EOF"
  14004.                                );
  14005.                   return(ERR);
  14006.               }
  14007.               /* Other auth types go here ... */
  14008. #ifdef FTP_SRP
  14009.               if (strcmp(auth_type, "SRP") == 0) {
  14010.                   if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  14011.                                                 (CHAR *) ucbuf,
  14012.                                                 ucbuf,
  14013.                                                 length
  14014.                                                 )
  14015.                        ) == -1) {
  14016.                       secure_error ("srp_encode failed" );
  14017.                       return ERR;
  14018.                   }
  14019.               }
  14020. #endif /* FTP_SRP */
  14021. #ifdef FTP_KRB4
  14022.               if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  14023.                   struct sockaddr_in myaddr, hisaddr;
  14024.                   GSOCKNAME_T len;
  14025.                   len = sizeof(myaddr);
  14026.                   if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  14027.                       secure_error("secure_putbuf: getsockname failed");
  14028.                       return(ERR);
  14029.                   }
  14030.                   len = sizeof(hisaddr);
  14031.                   if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  14032.                       secure_error("secure_putbuf: getpeername failed");
  14033.                       return(ERR);
  14034.                   }
  14035.                   if (ftp_dpl) {
  14036.                       kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  14037. #ifdef KRB524
  14038.                                            ftp_cred.session,
  14039. #else /* KRB524 */
  14040.                                            &ftp_cred.session,
  14041. #endif /* KRB524 */
  14042.                                            &hisaddr, &myaddr, &ftp_msg_data);
  14043.                   } else {
  14044.                       kerror = krb_rd_safe(ucbuf, length,
  14045. #ifdef KRB524
  14046.                                            ftp_cred.session,
  14047. #else /* KRB524 */
  14048.                                            &ftp_cred.session,
  14049. #endif /* KRB524 */
  14050.                                            &hisaddr, &myaddr, &ftp_msg_data);
  14051.                   }
  14052.                   if (kerror) {
  14053.                       secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  14054.                                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  14055.                                    krb_get_err_text(kerror));
  14056.                       return(ERR);
  14057.                   }
  14058.                   memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  14059.                   nin = bufp = ftp_msg_data.app_length;
  14060.               }
  14061. #endif /* FTP_KRB4 */
  14062. #ifdef FTP_GSSAPI
  14063.               if (strcmp(auth_type, "GSSAPI") == 0) {
  14064.                   gss_buffer_desc xmit_buf, msg_buf;
  14065.                   OM_uint32 maj_stat, min_stat;
  14066.                   int conf_state;
  14067.  
  14068.                   xmit_buf.value = ucbuf;
  14069.                   xmit_buf.length = length;
  14070.                   conf_state = (ftp_dpl == FPL_PRV);
  14071.                   /* decrypt/verify the message */
  14072.                   maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  14073.                                         &msg_buf, &conf_state, NULL);
  14074.                   if (maj_stat != GSS_S_COMPLETE) {
  14075.                       user_gss_error(maj_stat, min_stat,
  14076.                                      (ftp_dpl == FPL_PRV)?
  14077.                                      "failed unsealing ENC message":
  14078.                                      "failed unsealing MIC message");
  14079.                       return ERR;
  14080.                   }
  14081.                   memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  14082.                   gss_release_buffer(&min_stat, &msg_buf);
  14083.               }
  14084. #endif /* FTP_GSSAPI */
  14085.               /* Other auth types go here ... */
  14086.           }
  14087.     }
  14088.     if (nin == 0)
  14089.       return(EOF);
  14090.     else
  14091.       return(ucbuf[bufp - nin--]);
  14092. }
  14093.  
  14094. /* returns:
  14095.  *      c>=0 on success
  14096.  *      -1   on EOF
  14097.  *      -2   on security error
  14098.  */
  14099. static int
  14100. secure_getc(fd) int fd; {
  14101.     if (ftp_dpl == FPL_CLR) {
  14102.         static unsigned int nin = 0, bufp = 0;
  14103.         if (nin == 0) {
  14104.             if (iscanceled())
  14105.               return(-9);
  14106.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  14107.             if (nin <= 0) {
  14108.                 debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  14109.                 debug(F101,"secure_getc returns EOF","",EOF);
  14110.                 nin = bufp = 0;
  14111.                 return(EOF);
  14112.             }
  14113.             debug(F101,"ftp secure_getc recv","",nin);
  14114.             hexdump("ftp secure_getc recv",ucbuf,16);
  14115.             rpackets++;
  14116.             pktnum++;
  14117.             if (fdispla != XYFD_B) {
  14118.                 rpktl = nin;
  14119.                 ftscreen(SCR_PT,'D',rpackets,NULL);
  14120.             }
  14121.         }
  14122.         return(ucbuf[bufp - nin--]);
  14123.     } else
  14124.       return(secure_getbyte(fd));
  14125. }
  14126.  
  14127. /* returns:
  14128.  *     n>0  on success (n == # of bytes read)
  14129.  *       0  on EOF
  14130.  *      -1  on error (errno set), only for FPL_CLR
  14131.  *      -2  on security error
  14132.  */
  14133. static int
  14134. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  14135.     static int c = 0;
  14136.     int i;
  14137.  
  14138.     if (c == EOF)
  14139.       return(c = 0);
  14140.     for (i = 0; nbyte > 0; nbyte--) {
  14141.         c = secure_getc(fd);
  14142.         switch (c) {
  14143.           case -9:                      /* Canceled from keyboard */
  14144.             debug(F101,"ftp secure_read interrupted","",c);
  14145.             return(0);
  14146.           case ERR:
  14147.             debug(F101,"ftp secure_read error","",c);
  14148.             return(c);
  14149.           case EOF:
  14150.             debug(F101,"ftp secure_read EOF","",c);
  14151.             if (!i)
  14152.               c = 0;
  14153.             return(i);
  14154.           default:
  14155.             buf[i++] = c;
  14156.         }
  14157.     }
  14158.     return(i);
  14159. }
  14160.  
  14161. #ifdef USE_RUSERPASS
  14162. /* BEGIN_RUSERPASS
  14163.  *
  14164.  * Copyright (c) 1985 Regents of the University of California.
  14165.  * All rights reserved.
  14166.  *
  14167.  * Redistribution and use in source and binary forms, with or without
  14168.  * modification, are permitted provided that the following conditions
  14169.  * are met:
  14170.  * 1. Redistributions of source code must retain the above copyright
  14171.  *    notice, this list of conditions and the following disclaimer.
  14172.  * 2. Redistributions in binary form must reproduce the above copyright
  14173.  *    notice, this list of conditions and the following disclaimer in the
  14174.  *    documentation and/or other materials provided with the distribution.
  14175.  * 3. All advertising materials mentioning features or use of this software
  14176.  *    must display the following acknowledgement:
  14177.  *      This product includes software developed by the University of
  14178.  *      California, Berkeley and its contributors.
  14179.  * 4. Neither the name of the University nor the names of its contributors
  14180.  *    may be used to endorse or promote products derived from this software
  14181.  *    without specific prior written permission.
  14182.  *
  14183.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  14184.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14185.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14186.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  14187.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  14188.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  14189.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  14190.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14191.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  14192.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  14193.  * SUCH DAMAGE.
  14194.  */
  14195.  
  14196. #ifndef lint
  14197. static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
  14198. #endif /* not lint */
  14199.  
  14200. #include <sys/types.h>
  14201. #include <stdio.h>
  14202. #include <string.h>
  14203. #ifdef HAVE_STDLIB_H
  14204. #include <stdlib.h>
  14205. #endif
  14206. #include <ctype.h>
  14207. #include <sys/stat.h>
  14208. #include <errno.h>
  14209.  
  14210. #ifndef MAXHOSTNAMELEN
  14211. #define MAXHOSTNAMELEN 64
  14212. #endif
  14213.  
  14214. char * renvlook();
  14215. static FILE * cfile;
  14216.  
  14217. #define DEFAULT 1
  14218. #define LOGIN   2
  14219. #define PASSWD  3
  14220. #define ACCOUNT 4
  14221. #define MACDEF  5
  14222. #define ID      10
  14223. #define MACH    11
  14224.  
  14225. static char tokval[100];
  14226.  
  14227. static struct toktab {
  14228.     char *tokstr;
  14229.     int tval;
  14230. } toktab[]= {
  14231.     "default",  DEFAULT,
  14232.     "login",    LOGIN,
  14233.     "password", PASSWD,
  14234.     "passwd",   PASSWD,
  14235.     "account",  ACCOUNT,
  14236.     "machine",  MACH,
  14237.     "macdef",   MACDEF,
  14238.     0,          0
  14239. };
  14240.  
  14241. static int
  14242. token() {
  14243.     char *cp;
  14244.     int c;
  14245.     struct toktab *t;
  14246.  
  14247.     if (feof(cfile))
  14248.       return(0);
  14249.     while ((c = getc(cfile)) != EOF &&
  14250.            (c == '\n' || c == '\t' || c == ' ' || c == ','))
  14251.       continue;
  14252.     if (c == EOF)
  14253.       return(0);
  14254.     cp = tokval;
  14255.     if (c == '"') {
  14256.         while ((c = getc(cfile)) != EOF && c != '"') {
  14257.             if (c == '\\')
  14258.               c = getc(cfile);
  14259.             *cp++ = c;
  14260.         }
  14261.     } else {
  14262.         *cp++ = c;
  14263.         while ((c = getc(cfile)) != EOF
  14264.                && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  14265.             if (c == '\\')
  14266.               c = getc(cfile);
  14267.             *cp++ = c;
  14268.         }
  14269.     }
  14270.     *cp = 0;
  14271.     if (tokval[0] == 0)
  14272.       return(0);
  14273.     for (t = toktab; t->tokstr; t++)
  14274.       if (!strcmp(t->tokstr, tokval))
  14275.         return(t->tval);
  14276.     return(ID);
  14277. }
  14278.  
  14279. ruserpass(host, aname, apass, aacct)
  14280.     char *host, **aname, **apass, **aacct;
  14281. {
  14282.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  14283.     char myname[MAXHOSTNAMELEN], *mydomain;
  14284.     int t, i, c, usedefault = 0;
  14285. #ifdef NT
  14286.     struct _stat stb;
  14287. #else /* NT */
  14288.     struct stat stb;
  14289. #endif /* NT */
  14290.  
  14291.     hdir = getenv("HOME");
  14292.     if (hdir == NULL)
  14293.         hdir = ".";
  14294.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  14295.     cfile = fopen(buf, "r");
  14296.     if (cfile == NULL) {
  14297.         if (errno != ENOENT)
  14298.           perror(buf);
  14299.         return(0);
  14300.     }
  14301.     if (gethostname(myname, sizeof(myname)) < 0)
  14302.       myname[0] = '\0';
  14303.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  14304.       mydomain = "";
  14305.  
  14306.   next:
  14307.     while ((t = token())) switch(t) {
  14308.  
  14309.       case DEFAULT:
  14310.         usedefault = 1;
  14311.         /* FALL THROUGH */
  14312.  
  14313.       case MACH:
  14314.         if (!usedefault) {
  14315.             if (token() != ID)
  14316.               continue;
  14317.             /*
  14318.              * Allow match either for user's input host name
  14319.              * or official hostname.  Also allow match of
  14320.              * incompletely-specified host in local domain.
  14321.              */
  14322.             if (ckstrcmp(host, tokval,-1,1) == 0)
  14323.               goto match;
  14324.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  14325.               goto match;
  14326.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  14327.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  14328.                 ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  14329.                 tokval[tmp - ftp_host] == '\0')
  14330.               goto match;
  14331.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  14332.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  14333.                 ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  14334.                 tokval[tmp - host] == '\0')
  14335.               goto match;
  14336.             continue;
  14337.         }
  14338.  
  14339.       match:
  14340.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  14341.  
  14342.           case LOGIN:
  14343.             if (token())
  14344.               if (*aname == 0) {
  14345.                   *aname = malloc((unsigned) strlen(tokval) + 1);
  14346.                   strcpy(*aname, tokval);      /* safe */
  14347.               } else {
  14348.                   if (strcmp(*aname, tokval))
  14349.                     goto next;
  14350.               }
  14351.             break;
  14352.           case PASSWD:
  14353.             if (strcmp(*aname, "anonymous") &&
  14354.                 fstat(fileno(cfile), &stb) >= 0 &&
  14355.                 (stb.st_mode & 077) != 0) {
  14356.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  14357.                 fprintf(stderr, "Remove password or correct mode.\n");
  14358.                 goto bad;
  14359.             }
  14360.             if (token() && *apass == 0) {
  14361.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  14362.                 strcpy(*apass, tokval);          /* safe */
  14363.             }
  14364.             break;
  14365.           case ACCOUNT:
  14366.             if (fstat(fileno(cfile), &stb) >= 0
  14367.                 && (stb.st_mode & 077) != 0) {
  14368.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  14369.                 fprintf(stderr, "Remove account or correct mode.\n");
  14370.                 goto bad;
  14371.             }
  14372.             if (token() && *aacct == 0) {
  14373.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  14374.                 strcpy(*aacct, tokval);          /* safe */
  14375.             }
  14376.             break;
  14377.  
  14378.           default:
  14379.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  14380.             break;
  14381.         }
  14382.         goto done;
  14383.     }
  14384.  
  14385.   done:
  14386.     fclose(cfile);
  14387.     return(0);
  14388.  
  14389.   bad:
  14390.     fclose(cfile);
  14391.     return(-1);
  14392. }
  14393. #endif /* USE_RUSERPASS */
  14394.  
  14395. static char *radixN =
  14396.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  14397.  
  14398. static char pad = '=';
  14399.  
  14400. static int
  14401. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  14402.     CHAR inbuf[], outbuf[];
  14403.     int inlen, *outlen, decode;
  14404. {
  14405.     int i, j, D = 0;
  14406.     char *p;
  14407.     CHAR c = NUL;
  14408.  
  14409.     if (decode) {
  14410.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  14411.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  14412.               return(1);
  14413.             D = p - radixN;
  14414.             switch (i&3) {
  14415.               case 0:
  14416.                 outbuf[j] = D<<2;
  14417.                 break;
  14418.               case 1:
  14419.                 outbuf[j++] |= D>>4;
  14420.                 outbuf[j] = (D&15)<<4;
  14421.                 break;
  14422.               case 2:
  14423.                 outbuf[j++] |= D>>2;
  14424.                 outbuf[j] = (D&3)<<6;
  14425.                 break;
  14426.               case 3:
  14427.                 outbuf[j++] |= D;
  14428.             }
  14429.             if (j == *outlen)
  14430.               return(4);
  14431.         }
  14432.         switch (i&3) {
  14433.           case 1: return(3);
  14434.           case 2: if (D&15) return(3);
  14435.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  14436.             break;
  14437.           case 3: if (D&3) return(3);
  14438.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  14439.         }
  14440.         *outlen = j;
  14441.     } else {
  14442.         for (i = 0, j = 0; i < inlen; i++) {
  14443.             switch (i%3) {
  14444.               case 0:
  14445.                 outbuf[j++] = radixN[inbuf[i]>>2];
  14446.                 c = (inbuf[i]&3)<<4;
  14447.                 break;
  14448.               case 1:
  14449.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  14450.                 c = (inbuf[i]&15)<<2;
  14451.                 break;
  14452.               case 2:
  14453.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  14454.                 outbuf[j++] = radixN[inbuf[i]&63];
  14455.                 c = 0;
  14456.             }
  14457.             if (j == *outlen)
  14458.               return(4);
  14459.         }
  14460.         if (i%3) outbuf[j++] = radixN[c];
  14461.         switch (i%3) {
  14462.           case 1: outbuf[j++] = pad;
  14463.           case 2: outbuf[j++] = pad;
  14464.         }
  14465.         outbuf[*outlen = j] = '\0';
  14466.     }
  14467.     return(0);
  14468. }
  14469.  
  14470. static char *
  14471. radix_error(e) int e;
  14472. {
  14473.     switch (e) {
  14474.       case 0:  return("Success");
  14475.       case 1:  return("Bad character in encoding");
  14476.       case 2:  return("Encoding not properly padded");
  14477.       case 3:  return("Decoded # of bits not a multiple of 8");
  14478.       case 4:  return("Output buffer too small");
  14479.       default: return("Unknown error");
  14480.     }
  14481. }
  14482. /* END_RUSERPASS */
  14483.  
  14484. #ifdef FTP_SRP
  14485. /*---------------------------------------------------------------------------+
  14486.  |                                                                           |
  14487.  |   Package: srpftp                                                         |
  14488.  |   Author: Eugene Jhong                                                    |
  14489.  |                                                                           |
  14490.  +---------------------------------------------------------------------------*/
  14491.  
  14492. /*
  14493.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  14494.  * All Rights Reserved.
  14495.  *
  14496.  * Permission is hereby granted, free of charge, to any person obtaining
  14497.  * a copy of this software and associated documentation files (the
  14498.  * "Software"), to deal in the Software without restriction, including
  14499.  * without limitation the rights to use, copy, modify, merge, publish,
  14500.  * distribute, sublicense, and/or sell copies of the Software, and to
  14501.  * permit persons to whom the Software is furnished to do so, subject to
  14502.  * the following conditions:
  14503.  *
  14504.  * The above copyright notice and this permission notice shall be
  14505.  * included in all copies or substantial portions of the Software.
  14506.  *
  14507.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14508.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14509.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  14510.  *
  14511.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  14512.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  14513.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  14514.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  14515.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14516.  *
  14517.  * In addition, the following conditions apply:
  14518.  *
  14519.  * 1. Any software that incorporates the SRP authentication technology
  14520.  *    must display the following acknowlegment:
  14521.  *    "This product uses the 'Secure Remote Password' cryptographic
  14522.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  14523.  *
  14524.  * 2. Any software that incorporates all or part of the SRP distribution
  14525.  *    itself must also display the following acknowledgment:
  14526.  *    "This product includes software developed by Tom Wu and Eugene
  14527.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  14528.  *
  14529.  * 3. Redistributions in source or binary form must retain an intact copy
  14530.  *    of this copyright notice and list of conditions.
  14531.  */
  14532.  
  14533. #define SRP_PROT_VERSION        1
  14534.  
  14535. #ifdef CK_ENCRYPTION
  14536. #define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
  14537. #else
  14538. #define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
  14539. #endif /* CK_ENCRYPTION */
  14540.  
  14541. #define SRP_DEFAULT_HASH        HASH_ID_SHA
  14542.  
  14543. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  14544. CHAR srp_pref_hash = HASH_ID_SHA;
  14545.  
  14546. static struct t_client *tc = NULL;
  14547. static CHAR *skey = NULL;
  14548. static krypto_context *incrypt = NULL;
  14549. static krypto_context *outcrypt = NULL;
  14550.  
  14551. typedef unsigned int srp_uint32;
  14552.  
  14553. /*--------------------------------------------------------------+
  14554.  | srp_selcipher: select cipher                                 |
  14555.  +--------------------------------------------------------------*/
  14556. static int
  14557. srp_selcipher (cname) char *cname; {
  14558.     cipher_desc *cd;
  14559.  
  14560.     if (!(cd = cipher_getdescbyname (cname))) {
  14561.         int i;
  14562.         CHAR *list = cipher_getlist ();
  14563.  
  14564.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  14565.         for (i = 0; i < strlen (list); i++)
  14566.           fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  14567.         fprintf (stderr, "\n");
  14568.         return -1;
  14569.     }
  14570.     srp_pref_cipher = cd->id;
  14571.     return 0;
  14572. }
  14573.  
  14574. /*--------------------------------------------------------------+
  14575.  | srp_selhash: select hash                                     |
  14576.  +--------------------------------------------------------------*/
  14577. static int
  14578. srp_selhash (hname) char *hname; {
  14579.     hash_desc *hd;
  14580.  
  14581.     if (!(hd = hash_getdescbyname (hname))) {
  14582.         int i;
  14583.         CHAR *list = hash_getlist ();
  14584.  
  14585.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  14586.         for (i = 0; i < strlen (list); i++)
  14587.           fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  14588.         fprintf (stderr, "\n");
  14589.         return -1;
  14590.     }
  14591.     srp_pref_hash = hd->id;
  14592.     return 0;
  14593. }
  14594.  
  14595. /*--------------------------------------------------------------+
  14596.  | srp_userpass: get username and password                      |
  14597.  +--------------------------------------------------------------*/
  14598. static int
  14599. srp_userpass (host) char *host; {
  14600.     char tmp[BUFSIZ], prompt[256];
  14601.     char *user;
  14602.  
  14603.     user = NULL;
  14604. #ifdef USE_RUSERPASS
  14605.     ruserpass (host, &user, &srp_pass, &srp_acct);
  14606. #endif /* USE_RUSERPASS */
  14607.  
  14608.     while (user == NULL)     {
  14609.         char *myname;
  14610.         myname = whoami();
  14611.         if (!myname) myname = "";
  14612.         if (myname[0])
  14613.           ckmakxmsg(prompt,sizeof(prompt)," Name (",host,":",myname,"): ",
  14614.                     NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  14615.         else
  14616.           ckmakmsg(prompt,sizeof(prompt)," Name (",host,"): ",NULL);
  14617.         tmp[0] = '\0';
  14618.         readtext(prompt,tmp,sizeof(tmp));
  14619.         if ((*tmp == '\0'))
  14620.           user = myname;
  14621.         else
  14622.           user = tmp;
  14623.     }
  14624.     ckstrncpy (srp_user, user,BUFSIZ);
  14625.     return(0);
  14626. }
  14627.  
  14628. /*--------------------------------------------------------------+
  14629.  | srp_reset: reset srp information                             |
  14630.  +--------------------------------------------------------------*/
  14631. static int
  14632. srp_reset () {
  14633.     if (tc) { t_clientclose (tc); tc = NULL; }
  14634.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  14635.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  14636.     return(0);
  14637. }
  14638.  
  14639. /*--------------------------------------------------------------+
  14640.  | srp_ftp_auth: perform srp authentication                         |
  14641.  +--------------------------------------------------------------*/
  14642. static int
  14643. srp_ftp_auth(host, user, pass)
  14644.     char *host;
  14645.     char *user;
  14646.     char *pass;
  14647. {
  14648.     struct t_num *wp;
  14649.     struct t_num N;
  14650.     struct t_num g;
  14651.     struct t_num s;
  14652.     struct t_num yp;
  14653.     CHAR buf[FTP_BUFSIZ];
  14654.     CHAR tmp[FTP_BUFSIZ];
  14655.     CHAR *bp, *cp;
  14656.     int n, e, clen, blen, len, i;
  14657.     CHAR cid = 0;
  14658.     CHAR hid = 0;
  14659.  
  14660.     srp_pass = srp_acct = 0;
  14661.  
  14662.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  14663.     if (n != REPLY_CONTINUE) {
  14664.         if (ftp_deb)
  14665.             fprintf(stderr, "SRP rejected as an authentication type\n");
  14666.         return(0);
  14667.     } else {                            /* Send protocol version */
  14668.         CHAR vers[4];
  14669.         memset (vers, 0, 4);
  14670.         vers[3] = SRP_PROT_VERSION;
  14671.         if (!quiet)
  14672.           printf ("SRP accepted as authentication type.\n");
  14673.         bp = tmp; blen = 0;
  14674.         srp_put (vers, &bp, 4, &blen);
  14675.         len = FTP_BUFSIZ;
  14676.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14677.           goto encode_error;
  14678.         reply_parse = "ADAT=";
  14679.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14680.     }
  14681.     if (n == REPLY_CONTINUE) {          /* Get protocol version */
  14682.         bp = buf;
  14683.         if (!reply_parse)
  14684.           goto data_error;
  14685.         blen = sizeof(buf);
  14686.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  14687.           goto decode_error;
  14688.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  14689.           goto data_error;
  14690.  
  14691.         if (host) {                     /* Get username/password if needed */
  14692.             srp_userpass (host);
  14693.         } else {
  14694.             ckstrncpy (srp_user, user, BUFSIZ);
  14695.             srp_pass = pass;
  14696.         }
  14697.         bp = tmp; blen = 0;             /* Send username */
  14698.         srp_put (srp_user, &bp, strlen (srp_user), &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 N, g and s */
  14706.         bp = buf;
  14707.         if (!reply_parse)
  14708.           goto data_error;
  14709.         blen = sizeof(buf);
  14710.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14711.           goto decode_error;
  14712.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  14713.           goto data_error;
  14714.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  14715.           goto data_error;
  14716.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  14717.           goto data_error;
  14718.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  14719.             fprintf (stderr, "Unable to open SRP client structure.\n");
  14720.             goto bad;
  14721.         }
  14722.         wp = t_clientgenexp (tc);       /* Send wp */
  14723.         bp = tmp; blen = 0;
  14724.         srp_put (wp->data, &bp, wp->len, &blen);
  14725.         len = sizeof(buf);
  14726.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14727.           goto encode_error;
  14728.         reply_parse = "ADAT=";
  14729.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14730.     }
  14731.     if (n == REPLY_CONTINUE) {          /* Get yp */
  14732.         bp = buf;
  14733.         if (!reply_parse)
  14734.           goto data_error;
  14735.         blen = sizeof(buf);
  14736.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14737.           goto decode_error;
  14738.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  14739.           goto data_error;
  14740.         if (!srp_pass) {
  14741.             static char ftppass[80];
  14742.             setint();
  14743.             readpass(" SRP Password:",ftppass,80);
  14744.             srp_pass = ftppass;
  14745.         }
  14746.         t_clientpasswd (tc, srp_pass);
  14747.         memset (srp_pass, 0, strlen (srp_pass));
  14748.         skey = t_clientgetkey (tc, &yp); /* Send response */
  14749.         bp = tmp; blen = 0;
  14750.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  14751.         len = sizeof(buf);
  14752.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14753.           goto encode_error;
  14754.         reply_parse = "ADAT=";
  14755.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14756.     }
  14757.     if (n == REPLY_CONTINUE) {          /* Get response */
  14758.         bp = buf;
  14759.         if (!reply_parse)
  14760.           goto data_error;
  14761.         blen = sizeof(buf);
  14762.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14763.           goto encode_error;
  14764.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  14765.           goto data_error;
  14766.         if (t_clientverify (tc, cp)) {
  14767.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  14768.             goto bad;
  14769.         }
  14770.         bp = tmp; blen = 0;             /* Send nothing */
  14771.         srp_put ("\0", &bp, 1, &blen);
  14772.         len = sizeof(buf);
  14773.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14774.           goto encode_error;
  14775.         reply_parse = "ADAT=";
  14776.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14777.     }
  14778.     if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
  14779.         CHAR seqnum[4];
  14780.         CHAR *clist;
  14781.         CHAR *hlist;
  14782.         CHAR *p1;
  14783.         int clist_len, hlist_len;
  14784.         bp = buf;
  14785.         if (!reply_parse)
  14786.           goto data_error;
  14787.         blen = sizeof(buf);
  14788.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  14789.           goto encode_error;
  14790.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  14791.           goto data_error;
  14792.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  14793.           goto data_error;
  14794.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  14795.           goto data_error;
  14796.         memcpy (seqnum, cp, 4);
  14797.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  14798.           cid = srp_pref_cipher;
  14799.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  14800.           cid = SRP_DEFAULT_CIPHER;
  14801.         if (!cid) {
  14802.             CHAR *loclist = cipher_getlist ();
  14803.             for (i = 0; i < strlen (loclist); i++)
  14804.               if (cipher_supported (clist, loclist[i])) {
  14805.                   cid = loclist[i];
  14806.                   break;
  14807.               }
  14808.         }
  14809.         if (!cid) {
  14810.             fprintf (stderr, "Unable to agree on cipher.\n");
  14811.             goto bad;
  14812.         }
  14813.         /* Choose hash */
  14814.  
  14815.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  14816.           hid = srp_pref_hash;
  14817.  
  14818.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  14819.           hid = SRP_DEFAULT_HASH;
  14820.  
  14821.         if (!hid) {
  14822.             CHAR *loclist = hash_getlist ();
  14823.             for (i = 0; i < strlen (loclist); i++)
  14824.               if (hash_supported (hlist, loclist[i])) {
  14825.                   hid = loclist[i];
  14826.                   break;
  14827.               }
  14828.         }
  14829.         if (!hid) {
  14830.             fprintf (stderr, "Unable to agree on hash.\n");
  14831.             goto bad;
  14832.         }
  14833.         /* Set incrypt */
  14834.  
  14835.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  14836.                                     KRYPTO_DECODE)))
  14837.           goto bad;
  14838.  
  14839.         /* Generate random number for outkey and outseqnum */
  14840.  
  14841.         t_random (seqnum, 4);
  14842.  
  14843.         /* Send cid, hid, outkey, outseqnum */
  14844.  
  14845.         bp = tmp; blen = 0;
  14846.         srp_put (&cid, &bp, 1, &blen);
  14847.         srp_put (&hid, &bp, 1, &blen);
  14848.         srp_put (seqnum, &bp, 4, &blen);
  14849.         len = sizeof(buf);
  14850.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  14851.           goto encode_error;
  14852.         reply_parse = "ADAT=";
  14853.         n = ftpcmd("ADAT",buf,-1,-1,0);
  14854.  
  14855.         /* Set outcrypt */
  14856.  
  14857.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  14858.                                      KRYPTO_ENCODE)))
  14859.           goto bad;
  14860.  
  14861.         t_clientclose (tc);
  14862.         tc = NULL;
  14863.     }
  14864.     if (n != REPLY_COMPLETE)
  14865.       goto bad;
  14866.  
  14867.     if (ftp_vbm) {
  14868.         if (ftp_deb)
  14869.           printf("\n");
  14870.         printf ("SRP authentication succeeded.\n");
  14871.         printf ("Using cipher %s and hash function %s.\n",
  14872.                 (cipher_getdescbyid(cid))->name,
  14873.                 (hash_getdescbyid(hid))->name
  14874.                 );
  14875.     }
  14876.     reply_parse = NULL;
  14877.     auth_type = "SRP";
  14878.     return(1);
  14879.  
  14880.   encode_error:
  14881.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  14882.     goto bad;
  14883.  
  14884.   decode_error:
  14885.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  14886.     goto bad;
  14887.  
  14888.   data_error:
  14889.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  14890.     goto bad;
  14891.  
  14892.   bad:
  14893.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  14894.     reply_parse = NULL;
  14895.     return(0);
  14896. }
  14897.  
  14898. /*--------------------------------------------------------------+
  14899.  | srp_put: put item to send buffer                             |
  14900.  +--------------------------------------------------------------*/
  14901. static int
  14902. srp_put (in, out, inlen, outlen)
  14903.     CHAR *in;
  14904.     CHAR **out;
  14905.     int inlen;
  14906.     int *outlen;
  14907. {
  14908.     srp_uint32 net_len;
  14909.  
  14910.     net_len = htonl (inlen);
  14911.     memcpy (*out, &net_len, 4);
  14912.  
  14913.     *out += 4; *outlen += 4;
  14914.  
  14915.     memcpy (*out, in, inlen);
  14916.  
  14917.     *out += inlen; *outlen += inlen;
  14918.     return(0);
  14919. }
  14920.  
  14921. /*--------------------------------------------------------------+
  14922.  | srp_get: get item from receive buffer                        |
  14923.  +--------------------------------------------------------------*/
  14924. static int
  14925. srp_get (in, out, inlen, outlen)
  14926.     CHAR **in;
  14927.     CHAR **out;
  14928.     int *inlen;
  14929.     int *outlen;
  14930. {
  14931.     srp_uint32 net_len;
  14932.  
  14933.     if (*inlen < 4) return -1;
  14934.  
  14935.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  14936.     *outlen = ntohl (net_len);
  14937.  
  14938.     if (*inlen < *outlen) return -1;
  14939.  
  14940.     *out = *in; *inlen -= *outlen; *in += *outlen;
  14941.  
  14942.     return *outlen;
  14943. }
  14944.  
  14945. /*--------------------------------------------------------------+
  14946.  | srp_encode: encode control message                           |
  14947.  +--------------------------------------------------------------*/
  14948. static int
  14949. srp_encode (private, in, out, len)
  14950.     int private;
  14951.     CHAR *in;
  14952.     CHAR *out;
  14953.     unsigned len;
  14954. {
  14955.     if (private)
  14956.       return krypto_msg_priv (outcrypt, in, out, len);
  14957.     else
  14958.       return krypto_msg_safe (outcrypt, in, out, len);
  14959. }
  14960.  
  14961. /*--------------------------------------------------------------+
  14962.  | srp_decode: decode control message                           |
  14963.  +--------------------------------------------------------------*/
  14964. static int
  14965. srp_decode (private, in, out, len)
  14966.     int private;
  14967.     CHAR *in;
  14968.     CHAR *out;
  14969.     unsigned len;
  14970. {
  14971.     if (private)
  14972.       return krypto_msg_priv (incrypt, in, out, len);
  14973.     else
  14974.       return krypto_msg_safe (incrypt, in, out, len);
  14975. }
  14976.  
  14977. #endif /* FTP_SRP */
  14978. #endif /* TCPSOCKET (top of file) */
  14979. #endif /* SYSFTP (top of file) */
  14980. #endif /* NOFTP (top of file) */
  14981.