home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp4 / ckcftp.c < prev    next >
C/C++ Source or Header  |  2009-10-16  |  584KB  |  17,620 lines

  1. #define FTP_TIMEOUT
  2.  
  3. /*  C K C F T P  --  FTP Client for C-Kermit  */
  4.  
  5. char *ckftpv = "FTP Client, 9.0.253, 16 Oct 2009";
  6.  
  7. /*
  8.   Authors:
  9.     Jeffrey E Altman <jaltman@secure-endpoints.com>
  10.       Secure Endpoints Inc., New York City
  11.     Frank da Cruz <fdc@columbia.edu>,
  12.       The Kermit Project, Columbia University.
  13.  
  14.   Copyright (C) 2000, 2009,
  15.     Trustees of Columbia University in the City of New York.
  16.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  17.     copyright text in the ckcmai.c module for disclaimer and permissions.
  18.  
  19.   Portions of conditionally included code Copyright Regents of the
  20.     University of California and The Stanford SRP Authentication Project;
  21.     see notices below.
  22. */
  23.  
  24. /*
  25.   Pending...
  26.  
  27.   . Implement recursive NLST downloads by trying to CD to each filename.
  28.     If it works, it's a directory; if not, it's a file -- GET it.  But
  29.     that won't work with servers like wu-ftpd that don't send directory 
  30.     names.  Recursion with MLSD is done.
  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.   . Implement mail (MAIL, MLFL, MSOM, etc) if any servers support it.
  39.  
  40.   . Adapt to VMS.  Big job because of its record-oriented file system.
  41.     RMS programmer required.  There are probably also some VMS TCP/IP
  42.     product-specific wrinkles, e.g. attribute preservation in VMS-to-VMS
  43.     transfers using special options for Multinet or other FTP servers
  44.     (find out about STRU VMS).
  45. */
  46.  
  47. /*
  48.   Quick FTP command reference:
  49.  
  50.   RFC765 (1980) and earlier:
  51.     MODE  S(tream), B(lock), C(ompressed)
  52.     STRU  F(ILE), R(ECORD), P(AGE)
  53.     TYPE  A(SCII) <format>,  E(BCDIC) <format>, I(MAGE), L(OCAL) <bytesize>
  54.     PORT  - Port
  55.     PASV  - Passive mode
  56.     USER  - User
  57.     PASS  - Password
  58.     ACCT  - Account
  59.     CWD   - Change Working Directory
  60.     REIN  - Logout but not disconnect
  61.     QUIT  - Bye
  62.     RETR  - Retreive
  63.     STOR  - Store
  64.     APPE  - Append
  65.     ALLO  - Allocate
  66.     REST  - Restart
  67.     RNFR  - Rename from
  68.     RNTO  - Rename to
  69.     ABOR  - Cancel
  70.     DELE  - Delete
  71.     LIST  - Directory
  72.     NLST  - Name List
  73.     SITE  - Site parameters or commands
  74.     STAT  - Status
  75.     HELP  - Help
  76.     NOOP  - Noop
  77.  
  78.   RFC959 (1985):
  79.     CDUP  - Change to Parent Directory
  80.     SMNT  - Structure Mount
  81.     STOU  - Store Unique
  82.     RMD   - Remove Directory
  83.     MKD   - Make Directory
  84.     PWD   - Print Directory
  85.     SYST  - System
  86.  
  87.   RFC2389 (1998):
  88.     FEAT  - List Features (done)
  89.     OPTS  - Send options (done)
  90.  
  91.   RFC2640 (1999):
  92.     LANG  - Specify language for messages (not done)
  93.  
  94.   Pending (Internet Drafts):
  95.     SIZE  - File size (done)
  96.     MDTM  - File modification date-time (done)
  97.     MLST  - File name and attribute list (single file) (not done)
  98.     MLSD  - File list with attributes (multiple files) (done)
  99.     MAIL, MLFL, MSOM - mail delivery (not done)
  100.  
  101.   Alphabetical syntax list:
  102.     ABOR <CRLF>
  103.     ACCT <SP> <account-information> <CRLF>
  104.     ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
  105.     APPE <SP> <pathname> <CRLF>
  106.     CDUP <CRLF>
  107.     CWD  <SP> <pathname> <CRLF>
  108.     DELE <SP> <pathname> <CRLF>
  109.     FEAT <CRLF>
  110.     HELP [<SP> <string>] <CRLF>
  111.     LANG [<SP> <language-tag> ] <CRLF>
  112.     LIST [<SP> <pathname>] <CRLF>
  113.     MKD  <SP> <pathname> <CRLF>
  114.     MLSD [<SP> <pathname>] <CRLF>
  115.     MLST [<SP> <pathname>] <CRLF>
  116.     MODE <SP> <mode-code> <CRLF>
  117.     NLST [<SP> <pathname-or-wildcard>] <CRLF>
  118.     NOOP <CRLF>
  119.     OPTS <SP> <commandname> [ <SP> <command-options> ] <CRLF>
  120.     PASS <SP> <password> <CRLF>
  121.     PASV <CRLF>
  122.     PORT <SP> <host-port> <CRLF>
  123.     PWD  <CRLF>
  124.     QUIT <CRLF>
  125.     REIN <CRLF>
  126.     REST <SP> <marker> <CRLF>
  127.     RETR <SP> <pathname> <CRLF>
  128.     RMD  <SP> <pathname> <CRLF>
  129.     RNFR <SP> <pathname> <CRLF>
  130.     RNTO <SP> <pathname> <CRLF>
  131.     SITE <SP> <string> <CRLF>
  132.     SIZE <SP> <pathname> <CRLF>
  133.     SMNT <SP> <pathname> <CRLF>
  134.     STAT [<SP> <pathname>] <CRLF>
  135.     STOR <SP> <pathname> <CRLF>
  136.     STOU <CRLF>
  137.     STRU <SP> <structure-code> <CRLF>
  138.     SYST <CRLF>
  139.     TYPE <SP> <type-code> <CRLF>
  140.     USER <SP> <username> <CRLF>
  141. */
  142. #include "ckcsym.h"                     /* Standard includes */
  143. #include "ckcdeb.h"
  144.  
  145. #ifndef NOFTP                           /* NOFTP  = no FTP */
  146. #ifndef SYSFTP                          /* SYSFTP = use external ftp client */
  147. #ifdef TCPSOCKET                        /* Build only if TCP/IP included */
  148. #define CKCFTP_C
  149.  
  150. /* Note: much of the following duplicates what was done in ckcdeb.h */
  151. /* but let's not mess with it unless it causes trouble. */
  152.  
  153. #ifdef CK_ANSIC
  154. #include <stdarg.h>
  155. #else /* CK_ANSIC */
  156. #include <varargs.h>
  157. #endif /* CK_ANSIC */
  158. #include <signal.h>
  159. #ifdef OS2
  160. #ifdef OS2ONLY
  161. #include <os2.h>
  162. #endif /* OS2ONLY */
  163. #include "ckowin.h"
  164. #include "ckocon.h"
  165. #endif /* OS2 */
  166. #ifndef ZILOG
  167. #ifdef NT
  168. #include <setjmpex.h>
  169. #ifdef NTSIG
  170. extern int TlsIndex;
  171. #endif /* NTSIG */
  172. #else /* NT */
  173. #include <setjmp.h>
  174. #endif /* NT */
  175. #else
  176. #include <setret.h>
  177. #endif /* ZILOG */
  178. #include "ckcsig.h"
  179. #include <sys/stat.h>
  180. #include <ctype.h>
  181. #include <errno.h>
  182. #ifndef NOTIMEH
  183. #include <time.h>
  184. #endif /* NOTIMEH */
  185. #ifndef EPIPE
  186. #define EPIPE 32                        /* Broken pipe error */
  187. #endif /* EPIPE */
  188.  
  189. /* Kermit includes */
  190.  
  191. #include "ckcasc.h"
  192. #include "ckcker.h"
  193. #include "ckucmd.h"
  194. #include "ckuusr.h"
  195. #include "ckcnet.h"                     /* Includes ckctel.h */
  196. #include "ckctel.h"                     /* (then why include it again?) */
  197. #include "ckcxla.h"
  198.  
  199. #ifdef CK_SSL
  200. #include "ckuath.h"            /* SMS 2007/02/15 */
  201. #endif /* def CK_SSL */
  202.  
  203. /*
  204.   How to get the struct timeval definition so we can call select().  The
  205.   xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  206.   targets.  The problem is: maybe we have already included some header file
  207.   that defined struct timeval, and maybe we didn't.  If we did, we don't want
  208.   to include another header file that defines it again or the compilation will
  209.   fail.  If we didn't, we have to include the header file where it's defined.
  210.   But in some cases even that won't work because of strict POSIX constraints
  211.   or somesuch, or because this introduces other conflicts (e.g. struct tm
  212.   multiply defined), in which case we have to define it ourselves, but this
  213.   can work only if we didn't already encounter a definition.
  214. */
  215. #ifndef DCLTIMEVAL
  216. #ifdef SV68R3V6
  217. #define DCLTIMEVAL
  218. #else
  219. #ifdef SCO234
  220. #define DCLTIMEVAL
  221. #endif /* SCO234 */
  222. #endif /* SV68R3V6 */
  223. #endif /* DCLTIMEVAL */
  224.  
  225. #ifdef DCLTIMEVAL
  226. /* Also maybe in some places the elements must be unsigned... */
  227. struct timeval {
  228.     long tv_sec;
  229.     long tv_usec;
  230. };
  231. #ifdef COMMENT
  232. /* Currently we don't use this... */
  233. struct timezone {
  234.     int tz_minuteswest;
  235.     int tz_dsttime;
  236. };
  237. #endif /* COMMENT */
  238. #else  /* !DCLTIMEVAL */
  239. #ifndef NOSYSTIMEH
  240. #ifdef SYSTIMEH
  241. #include <sys/time.h>
  242. #endif /* SYSTIMEH */
  243. #endif /* NOSYSTIMEH */
  244. #ifndef NOSYSTIMEBH
  245. #ifdef SYSTIMEBH
  246. #include <sys/timeb.h>
  247. #endif /* SYSTIMEBH */
  248. #endif /* NOSYSTIMEBH */
  249. #endif /* DCLTIMEVAL */
  250.  
  251. #include <sys/types.h>
  252. #include <stdio.h>
  253. #include <string.h>
  254. #ifdef HAVE_STDLIB_H
  255. #include <stdlib.h>
  256. #endif /* HAVE_STDLIB_H */
  257.  
  258. #ifndef NOSETTIME
  259. #ifdef COMMENT
  260. /* This section moved to ckcdeb.h */
  261. #ifdef POSIX
  262. #define UTIMEH
  263. #else
  264. #ifdef HPUX9
  265. #define UTIMEH
  266. #else
  267. #ifdef OS2
  268. #define SYSUTIMEH
  269. #endif /* OS2 */
  270. #endif /* HPUX9 */
  271. #endif /* POSIX */
  272. #endif /* COMMENT */
  273.  
  274. #ifdef VMS                /* SMS 2007/02/15 */
  275. #include "ckvrtl.h"            /* for utime() */
  276. #else  /* def VMS */
  277. #ifdef SYSUTIMEH
  278. #include <sys/utime.h>
  279. #else
  280. #ifdef UTIMEH
  281. #include <utime.h>
  282. #define SYSUTIMEH
  283. #endif /* UTIMEH */
  284. #endif /* SYSUTIMEH */
  285. #endif /* def VMS */
  286. #endif /* NOSETTIME */
  287.  
  288. #ifndef SCO_OSR504
  289. #ifdef SELECT_H
  290. #include <sys/select.h>
  291. #endif /* SELECT_H */
  292. #endif /* SCO_OSR504 */
  293.  
  294. /* select() dialects... */
  295.  
  296. #ifdef UNIX
  297. #define BSDSELECT                       /* BSD select() syntax/semantics */
  298. #ifndef FD_SETSIZE
  299. #define FD_SETSIZE 128
  300. #endif    /* FD_SETSIZE */
  301. #ifdef HPUX6                /* For HP-UX 6.5 circa 1989 */
  302. typedef long fd_mask;
  303. #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
  304. #ifndef howmany
  305. #define howmany(x, y)   (((x)+((y)-1))/(y))
  306. #endif    /* howmany */
  307. #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  308. #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  309. #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  310. #define FD_COPY(f, t)   bcopy(f, t, sizeof(*(f)))
  311. #define FD_ZERO(p)      bzero(p, sizeof(*(p)))
  312. #endif    /* HPUX6 */
  313.  
  314. #else
  315. #ifdef OS2                              /* OS/2 or Win32 */
  316. #ifdef NT
  317. #define BSDSELECT
  318. #else /* NT */
  319. #define IBMSELECT
  320. #endif /* NT */
  321. #endif /* OS2 */
  322. #endif /* UNIX */
  323.  
  324. #ifdef VMS
  325. #define BSDSELECT            /* SMS 2007/02/15 */
  326. #endif /* def VMS */
  327.  
  328. /* Other select() peculiarities */
  329.  
  330. #ifdef HPUX
  331. #ifndef HPUX10                          /* HP-UX 9.xx and earlier */
  332. #ifndef HPUX1100
  333. /* The three interior args to select() are (int *) rather than (fd_set *) */
  334. #ifndef INTSELECT
  335. #define INTSELECT
  336. #endif /* INTSELECT */
  337. #endif /* HPUX1100 */
  338. #endif /* HPUX10 */
  339. #endif /* HPUX */
  340.  
  341. #ifdef CK_SOCKS                         /* SOCKS Internet relay package */
  342. #ifdef CK_SOCKS5                        /* SOCKS 5 */
  343. #define accept  SOCKSaccept
  344. #define bind    SOCKSbind
  345. #define connect SOCKSconnect
  346. #define getsockname SOCKSgetsockname
  347. #define listen SOCKSlisten
  348. #else  /* Not SOCKS 5 */
  349. #define accept  Raccept
  350. #define bind    Rbind
  351. #define connect Rconnect
  352. #define getsockname Rgetsockname
  353. #define listen Rlisten
  354. #endif /* CK_SOCKS5 */
  355. #endif /* CK_SOCKS */
  356.  
  357. #ifndef NOHTTP
  358. extern char * tcp_http_proxy;           /* Name[:port] of http proxy server */
  359. extern int    tcp_http_proxy_errno;
  360. extern char * tcp_http_proxy_user;
  361. extern char * tcp_http_proxy_pwd;
  362. extern char * tcp_http_proxy_agent;
  363. #define HTTPCPYL 1024
  364. static char proxyhost[HTTPCPYL];
  365. #endif /* NOHTTP */
  366. int ssl_ftp_proxy = 0;                  /* FTP over SSL/TLS Proxy Server */
  367.  
  368. /* Feature selection */
  369.  
  370. #ifndef USE_SHUTDOWN
  371. /*
  372.   We don't use shutdown() because (a) we always call it just before close()
  373.   so it's redundant and unnecessary, and (b) it introduces a long pause on
  374.   some platforms like SV/68 R3.
  375. */
  376. /* #define USE_SHUTDOWN */
  377. #endif /* USE_SHUTDOWN */
  378.  
  379. #ifndef NORESEND
  380. #ifndef NORESTART                       /* Restart / recover */
  381. #ifndef FTP_RESTART
  382. #define FTP_RESTART
  383. #endif /* FTP_RESTART */
  384. #endif /* NORESTART */
  385. #endif /* NORESEND */
  386.  
  387. #ifndef NOUPDATE                        /* Update mode */
  388. #ifndef DOUPDATE
  389. #define DOUPDATE
  390. #endif /* DOUPDATE */
  391. #endif /* NOUPDATE */
  392.  
  393. #ifndef UNICODE                         /* Unicode required */
  394. #ifndef NOCSETS                         /* for charset translation */
  395. #define NOCSETS
  396. #endif /* NOCSETS */
  397. #endif /* UNICODE */
  398.  
  399. #ifndef OS2
  400. #ifndef HAVE_MSECS                      /* Millisecond timer */
  401. #ifdef UNIX
  402. #ifdef GFTIMER
  403. #define HAVE_MSECS
  404. #endif /* GFTIMER */
  405. #endif /* UNIX */
  406. #endif /* HAVE_MSECS */
  407. #endif /* OS2 */
  408.  
  409. #ifdef PIPESEND                         /* PUT from pipe */
  410. #ifndef PUTPIPE
  411. #define PUTPIPE
  412. #endif /* PUTPIPE */
  413. #endif /* PIPESEND */
  414.  
  415. #ifndef NOSPL                           /* PUT from array */
  416. #ifndef PUTARRAY
  417. #define PUTARRAY
  418. #endif /* PUTARRAY */
  419. #endif /* NOSPL */
  420.  
  421. /* Security... */
  422.  
  423. #ifdef CK_SRP
  424. #define FTP_SRP
  425. #endif /* CK_SRP */
  426.  
  427. #ifdef CK_KERBEROS
  428. #ifdef KRB4
  429. /*
  430.   There is a conflict between the Key Schedule formats used internally
  431.   within the standalone MIT KRB4 library and that used by Eric Young
  432.   in OpenSSL and his standalone DES library.  Therefore, KRB4 FTP AUTH
  433.   cannot be supported when either of those two packages are used.
  434. */
  435. #ifdef KRB524
  436. #define FTP_KRB4
  437. #else /* KRB524 */
  438. #ifndef CK_SSL
  439. #ifndef LIBDES
  440. #define FTP_KRB4
  441. #endif /* LIBDES */
  442. #endif /* CK_SSL */
  443. #endif /* KRB524 */
  444. #endif /* KRB4 */
  445. #ifdef KRB5
  446. #ifndef HEIMDAL
  447. #define FTP_GSSAPI
  448. #endif /* HEIMDAL */
  449. #endif /* KRB5 */
  450. #endif /* CK_KERBEROS */
  451.  
  452. /* FTP_SECURITY is defined if any of the above is selected */
  453. #ifndef FTP_SECURITY
  454. #ifdef FTP_GSSAPI
  455. #define FTP_SECURITY
  456. #else
  457. #ifdef FTP_KRB4
  458. #define FTP_SECURITY
  459. #else
  460. #ifdef FTP_SRP
  461. #define FTP_SECURITY
  462. #else
  463. #ifdef CK_SSL
  464. #define FTP_SECURITY
  465. #endif /* CK_SSL */
  466. #endif /* FTP_SRP */
  467. #endif /* FTP_KRB4 */
  468. #endif /* FTP_GSSAPI */
  469. #endif /* FTP_SECURITY */
  470.  
  471. #ifdef CK_DES
  472. #ifdef CK_SSL
  473. #ifndef LIBDES
  474. #define LIBDES
  475. #endif /* LIBDES */
  476. #endif /* CK_SSL */
  477. #endif /* CK_DES */
  478.  
  479. #ifdef CRYPT_DLL
  480. #ifndef LIBDES
  481. #define LIBDES
  482. #endif /* LIBDES */
  483. #endif /* CRYPT_DLL */
  484.  
  485. #ifdef FTP_KRB4
  486. #define des_cblock Block
  487. #define des_key_schedule Schedule
  488. #ifdef KRB524
  489. #ifdef NT
  490. #define _WINDOWS
  491. #endif /* NT */
  492. #include "kerberosIV/krb.h"
  493. #else /* KRB524 */
  494. #ifdef SOLARIS
  495. #ifndef sun
  496. /* For some reason lost in history the Makefile Solaris targets have -Usun */
  497. #define sun
  498. #endif /* sun */
  499. #endif /* SOLARIS */
  500. #include "krb.h"
  501. #define krb_get_err_text_entry krb_get_err_text
  502. #endif /* KRB524 */
  503. #endif /* FTP_KRB4 */
  504.  
  505. #ifdef CK_SSL
  506. #ifdef FTP_KRB4
  507. #ifndef HEADER_DES_H
  508. #define HEADER_DES_H
  509. #endif /* HEADER_DES_H */
  510. #endif /* FTP_KRB4 */
  511. #include "ck_ssl.h"
  512. #endif /* CK_SSL */
  513.  
  514. #ifdef FTP_SRP
  515. #ifdef HAVE_PWD_H
  516. #include "pwd.h"
  517. #endif /* HAVE_PWD_H */
  518. #include "t_pwd.h"
  519. #include "t_client.h"
  520. #include "krypto.h"
  521. #endif /* FTP_SRP */
  522.  
  523. #ifdef FTP_GSSAPI
  524. #include <gssapi/gssapi.h>
  525. /*
  526.   Need to include the krb5 file, because we're doing manual fallback
  527.   from the v2 mech to the v1 mech.  Once there's real negotiation,
  528.   we can be generic again.
  529. */
  530. #include <gssapi/gssapi_generic.h>
  531. #include <gssapi/gssapi_krb5.h>
  532. static gss_ctx_id_t gcontext;
  533.  
  534. #ifdef MACOSX
  535. /** exported constants defined in gssapi_krb5{,_nx}.h **/
  536.  
  537. /* these are bogus, but will compile */
  538.  
  539. /*
  540.  * The OID of the draft krb5 mechanism, assigned by IETF, is:
  541.  *      iso(1) org(3) dod(5) internet(1) security(5)
  542.  *      kerberosv5(2) = 1.3.5.1.5.2
  543.  * The OID of the krb5_name type is:
  544.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  545.  *      krb5(2) krb5_name(1) = 1.2.840.113554.1.2.2.1
  546.  * The OID of the krb5_principal type is:
  547.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  548.  *      krb5(2) krb5_principal(2) = 1.2.840.113554.1.2.2.2
  549.  * The OID of the proposed standard krb5 mechanism is:
  550.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  551.  *      krb5(2) = 1.2.840.113554.1.2.2
  552.  * The OID of the proposed standard krb5 v2 mechanism is:
  553.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  554.  *      krb5v2(3) = 1.2.840.113554.1.2.3
  555.  *
  556.  */
  557.  
  558. /*
  559.  * Encoding rules: The first two values are encoded in one byte as 40
  560.  * * value1 + value2.  Subsequent values are encoded base 128, most
  561.  * significant digit first, with the high bit (\200) set on all octets
  562.  * except the last in each value's encoding.
  563.  */
  564.  
  565. static CONST gss_OID_desc
  566. ck_krb5_gss_oid_array[] = {
  567.    /* this is the official, rfc-specified OID */
  568.    {9, "\052\206\110\206\367\022\001\002\002"},
  569.    /* this is the unofficial, wrong OID */
  570.    {5, "\053\005\001\005\002"},
  571.    /* this is the v2 assigned OID */
  572.    {9, "\052\206\110\206\367\022\001\002\003"},
  573.    /* these two are name type OID's */
  574.    {10, "\052\206\110\206\367\022\001\002\002\001"},
  575.    {10, "\052\206\110\206\367\022\001\002\002\002"},
  576.    { 0, 0 }
  577. };
  578.  
  579. static
  580. CONST gss_OID_desc * CONST gss_mech_krb5_v2 = ck_krb5_gss_oid_array+2;
  581.  
  582. #ifdef MACOSX103
  583. static
  584. CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
  585. #endif /* MACOSX103 */
  586.  
  587. #ifndef MACOSX
  588. static
  589. CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
  590. static
  591. CONST gss_OID_desc * CONST gss_mech_krb5_old = ck_krb5_gss_oid_array+1;
  592. static
  593. CONST gss_OID_desc * CONST gss_nt_krb5_name = ck_krb5_gss_oid_array+3;
  594. static
  595. CONST gss_OID_desc * CONST gss_nt_krb5_principal = ck_krb5_gss_oid_array+4;
  596. #endif    /* MACOSX */
  597.  
  598. /*
  599.  * See krb5/gssapi_krb5.c for a description of the algorithm for
  600.  * encoding an object identifier.
  601.  */
  602.  
  603. /*
  604.  * The OID of user_name is:
  605.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  606.  *      generic(1) user_name(1) = 1.2.840.113554.1.2.1.1
  607.  * machine_uid_name:
  608.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  609.  *      generic(1) machine_uid_name(2) = 1.2.840.113554.1.2.1.2
  610.  * string_uid_name:
  611.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  612.  *      generic(1) string_uid_name(3) = 1.2.840.113554.1.2.1.3
  613.  * service_name:
  614.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  615.  *      generic(1) service_name(4) = 1.2.840.113554.1.2.1.4
  616.  * exported_name:
  617.  *      1(iso), 3(org), 6(dod), 1(internet), 5(security), 6(nametypes),
  618.  *          4(gss-api-exported-name)
  619.  * host_based_service_name (v2):
  620.  *      iso (1) org (3), dod (6), internet (1), security (5), nametypes(6),
  621.  *      gss-host-based-services(2)
  622.  */
  623.  
  624. static gss_OID_desc ck_oids[] = {
  625.    {10, "\052\206\110\206\367\022\001\002\001\001"},
  626.    {10, "\052\206\110\206\367\022\001\002\001\002"},
  627.    {10, "\052\206\110\206\367\022\001\002\001\003"},
  628.    {10, "\052\206\110\206\367\022\001\002\001\004"},
  629.    { 6, "\053\006\001\005\006\004"},
  630.    { 6, "\053\006\001\005\006\002"},
  631. };
  632.  
  633. static gss_OID ck_gss_nt_user_name = ck_oids+0;
  634. static gss_OID ck_gss_nt_machine_uid_name = ck_oids+1;
  635. static gss_OID ck_gss_nt_string_uid_name = ck_oids+2;
  636. static gss_OID ck_gss_nt_service_name = ck_oids+3;
  637. static gss_OID ck_gss_nt_exported_name = ck_oids+4;
  638. static gss_OID ck_gss_nt_service_name_v2 = ck_oids+5;
  639. #endif /* MACOSX */
  640. #endif /* FTP_GSSAPI */
  641.  
  642. #ifdef OS2
  643. #ifdef FTP_SRP
  644. #define MAP_KRYPTO
  645. #ifdef SRPDLL
  646. #define MAP_SRP
  647. #endif /* SRPDLL */
  648. #endif /* FTP_SRP */
  649. #ifdef FTP_KRB4
  650. #define MAP_KRB4
  651. #ifdef CK_ENCRYPTION
  652. #define MAP_DES
  653. #endif /* CK_ENCRYPTION */
  654. #endif /* FTP_KRB4 */
  655. #ifdef FTP_GSSAPI
  656. #define MAP_GSSAPI
  657. #define GSS_OIDS
  658. #endif /* FTP_GSSAPI */
  659. #include "ckoath.h"
  660.  
  661. extern int k95stdout, wherex[], wherey[];
  662. extern unsigned char colorcmd;
  663. #endif /* OS2 */
  664.  
  665. #ifdef FTP_KRB4
  666. static char ftp_realm[REALM_SZ + 1];
  667. static KTEXT_ST ftp_tkt;
  668. #ifdef OS2
  669. static LEASH_CREDENTIALS ftp_cred;
  670. #else /* OS2 */
  671. static CREDENTIALS ftp_cred;
  672. #endif /* OS2 */
  673. static MSG_DAT ftp_msg_data;
  674. static des_key_schedule ftp_sched;
  675. static int foo[4] = {99,99,99,99};
  676. #endif /* FTP_KRB4 */
  677.  
  678. /* getreply() function codes */
  679.  
  680. #define GRF_AUTH 1            /* Reply to AUTH command */
  681. #define GRF_FEAT 2            /* Reply to FEAT command */
  682.  
  683. /* Operational definitions */
  684.  
  685. #define DEF_VBM 0                       /* Default verbose mode */
  686. /* #define SETVBM */                    /* (see getreply) */
  687.  
  688. #define URL_ONEFILE                     /* GET, not MGET, for FTP URL */
  689.  
  690. #define FTP_BUFSIZ 10240                /* Max size for FTP cmds & replies */
  691. #define SRVNAMLEN 32                    /* Max length for server type name */
  692. #define PWDSIZ 256
  693. #define PASSBUFSIZ 256
  694. #define PROMPTSIZ 256
  695.  
  696. #ifndef MGETMAX                         /* Max operands for MGET command */
  697. #define MGETMAX 1000
  698. #endif /* MGETMAX */
  699.  
  700. #ifdef FTP_SRP
  701. #define FUDGE_FACTOR 100
  702. #endif /* FTP_SRP */
  703.  
  704. /*
  705.   Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  706.   number bytes.  Must be defined for each auth type.
  707.   GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  708.   3DES requires 56 bytes.  Lets use 96 just to be sure.
  709. */
  710. #ifdef FTP_GSSAPI
  711. #ifndef FUDGE_FACTOR
  712. #define FUDGE_FACTOR 96
  713. #endif /* FUDGE_FACTOR */
  714. #endif /* FTP_GSSAPI */
  715.  
  716. #ifdef FTP_KRB4
  717. #ifndef FUDGE_FACTOR
  718. #define FUDGE_FACTOR 32
  719. #endif /* FUDGE_FACTOR */
  720. #endif /* FTP_KRB4 */
  721.  
  722. #ifndef FUDGE_FACTOR                    /* In case no auth types define it */
  723. #define FUDGE_FACTOR 0
  724. #endif /* FUDGE_FACTOR */
  725.  
  726. #ifndef MAXHOSTNAMELEN
  727. #define MAXHOSTNAMELEN 64
  728. #endif /* MAXHOSTNAMELEN */
  729. #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
  730.  
  731. /* Fascist compiler toadying */
  732.  
  733. #ifndef SENDARG2TYPE
  734. #ifdef COMMENT                          /* Might be needed here and there */
  735. #define SENDARG2TYPE const char *
  736. #else
  737. #define SENDARG2TYPE char *
  738. #endif /* COMMENT */
  739. #endif /* SENDARG2TYPE */
  740.  
  741. /* Common text messages */
  742.  
  743. static char *nocx = "?No FTP control connection\n";
  744.  
  745. static char *fncnam[] = {
  746.   "rename", "overwrite", "backup", "append", "discard", "ask", "update",
  747.   "dates-differ", ""
  748. };
  749.  
  750. /* Macro definitions */
  751.  
  752. /* Used to speed up text-mode PUTs */
  753. #define zzout(fd,c) \
  754. ((fd<0)?(-1):((nout>=ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
  755.  
  756. #define CHECKCONN() if(!connected){printf(nocx);return(-9);}
  757.  
  758. /* Externals */
  759.  
  760. #ifdef CK_URL
  761. extern struct urldata g_url;
  762. #endif /* CK_URL */
  763.  
  764. #ifdef DYNAMIC
  765. extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
  766. #else
  767. extern char zinbuffer[], zoutbuffer[];
  768. #endif /* DYNAMIC */
  769. extern char *zinptr, *zoutptr;
  770. extern int zincnt, zoutcnt, zobufsize, fncact;
  771.  
  772. #ifdef CK_TMPDIR
  773. extern int f_tmpdir;                    /* Directory changed temporarily */
  774. extern char savdir[];                   /* For saving current directory */
  775. extern char * dldir;
  776. #endif /* CK_TMPDIR */
  777.  
  778. extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
  779.  
  780. extern xx_strp xxstring;
  781. extern struct keytab onoff[], txtbin[], rpathtab[];
  782. extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
  783. extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
  784. extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
  785. extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
  786. extern int nolinks, msgflg, keep;
  787. extern CK_OFF_T fsize, ffc, tfc, sendstart, sndsmaller, sndlarger, rs_len;
  788. extern long filcnt, xfsecs, tfcps, cps, oldcps;
  789.  
  790. #ifdef FTP_TIMEOUT
  791. int ftp_timed_out = 0;
  792. long ftp_timeout = 0;
  793. #endif    /* FTP_TIMEOUT */
  794.  
  795. #ifdef GFTIMER
  796. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  797. #else
  798. extern long xfsecs;
  799. #endif /* GFTIMER */
  800.  
  801. extern char filnam[], * filefile, myhost[];
  802. extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
  803. extern int g_skipbup, skipbup, sendmode;
  804. extern int g_displa, fdispla, displa;
  805.  
  806. #ifdef LOCUS
  807. extern int locus, autolocus;
  808. #endif /* LOCUS */
  809.  
  810. #ifndef NOCSETS
  811. extern int nfilc, dcset7, dcset8, fileorder;
  812. extern struct csinfo fcsinfo[];
  813. extern struct keytab fcstab[];
  814. extern int fcharset;
  815. #endif /* NOCSETS */
  816.  
  817. extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
  818. extern char sndnbefore[], sndnafter[], *rcvexcept[];
  819. extern CHAR feol;
  820.  
  821. extern char * remdest;
  822. extern int remfile, remappd, rempipe;
  823.  
  824. #ifndef NOSPL
  825. extern int cmd_quoting;
  826. #ifdef PUTARRAY
  827. extern int sndxlo, sndxhi, sndxin;
  828. extern char sndxnam[];
  829. extern char **a_ptr[];                  /* Array pointers */
  830. extern int a_dim[];                     /* Array dimensions */
  831. #endif /* PUTARRAY */
  832. #endif /* NOSPL */
  833.  
  834. #ifndef NOMSEND                         /* MPUT and ADD SEND-LIST lists */
  835. extern char *msfiles[];
  836. extern int filesinlist;
  837. extern struct filelist * filehead;
  838. extern struct filelist * filetail;
  839. extern struct filelist * filenext;
  840. extern int addlist;
  841. extern char fspec[];                    /* Most recent filespec */
  842. extern int fspeclen;                    /* Length of fspec[] buffer */
  843. #endif /* NOMSEND */
  844.  
  845. extern int pipesend;
  846. #ifdef PIPESEND
  847. extern char * sndfilter, * rcvfilter;
  848. #endif /* PIPESEND */
  849.  
  850. #ifdef CKROOT
  851. extern int ckrooterr;
  852. #endif /* CKROOT */
  853.  
  854. #ifdef KRB4
  855. extern int krb4_autoget;
  856. _PROTOTYP(char * ck_krb4_realmofhost,(char *));
  857. #endif /* KRB4 */
  858.  
  859. #ifdef KRB5
  860. extern int krb5_autoget;
  861. extern int krb5_d_no_addresses;
  862. _PROTOTYP(char * ck_krb5_realmofhost,(char *));
  863. #endif /* KRB5 */
  864.  
  865. #ifdef DCMDBUF
  866. extern char *atmbuf;                    /* Atom buffer (malloc'd) */
  867. extern char *cmdbuf;                    /* Command buffer (malloc'd) */
  868. extern char *line;                      /* Big string buffer #1 */
  869. extern char *tmpbuf;                    /* Big string buffer #2 */
  870. #else
  871. extern char atmbuf[];                   /* The same, but static */
  872. extern char cmdbuf[];
  873. extern char line[];
  874. extern char tmpbuf[];
  875. #endif /* DCMDBUF */
  876.  
  877. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  878.  
  879. /* Public variables declared here */
  880.  
  881. #ifdef NOXFER
  882. int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
  883. #else
  884. int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
  885. #endif /* NOXFER */
  886. int ftpcode = -1;                       /* Last FTP response code */
  887. int ftp_cmdlin = 0;                     /* FTP invoked from command line */
  888. int ftp_fai = 0;                        /* FTP failure count */
  889. int ftp_deb = 0;                        /* FTP debugging */
  890. int ftp_dis = -1;            /* FTP display style */
  891. int ftp_log = 1;                        /* FTP Auto-login */
  892. int sav_log = -1;
  893. int ftp_action = 0;                     /* FTP action from command line */
  894. int ftp_dates = 1;                      /* Set file dates from server */
  895. int ftp_xfermode = XMODE_A;        /* FTP-specific transfer mode */
  896.  
  897. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  898. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  899. char ftp_user_host[MAX_DNS_NAMELEN]= ""; /* FTP hostname specified by user */
  900. char * ftp_host = NULL;                 /* FTP hostname */
  901. char * ftp_logname = NULL;              /* FTP username */
  902. char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
  903. char * ftp_apw = NULL;            /* Anonymous password */
  904.  
  905. /* Definitions and typedefs needed for prototypes */
  906.  
  907. #define sig_t my_sig_t
  908. #define sigtype SIGTYP
  909. typedef sigtype (*sig_t)();
  910.  
  911. /* Static global variables */
  912.  
  913. static char ftpsndbuf[FTP_BUFSIZ+64];
  914.  
  915. static char * fts_sto = NULL;
  916.  
  917. static int ftpsndret = 0;
  918. static struct _ftpsnd {
  919.     sig_t oldintr, oldintp;
  920.     int            reply;
  921.     int            incs,
  922.                    outcs;
  923.     char *         cmd, * local, * remote;
  924.     int            bytes;
  925.     int            restart;
  926.     int            xlate;
  927.     char *         lmode;
  928. } ftpsnd;
  929.  
  930. /*
  931.   This is just a first stab -- these strings should match how the
  932.   corresponding FTP servers identify themselves.
  933. */
  934. #ifdef UNIX
  935. static char * myostype = "UNIX";
  936. #else
  937. #ifdef VMS
  938. /* not yet... */
  939. static char * myostype = "VMS";
  940. #else
  941. #ifdef OS2
  942. #ifdef NT
  943. static char * myostype = "WIN32";
  944. #else
  945. static char * myostype = "OS/2";
  946. #endif /* NT */
  947. #else
  948. static char * myostype = "UNSUPPORTED";
  949. #endif /* OS2  */
  950. #endif /* VMS */
  951. #endif /* UNIX */
  952.  
  953. static int noinit = 0;                  /* Don't send REST, STRU, MODE */
  954. static int alike = 0;                   /* Client/server like platforms */
  955. static int local = 1;                   /* Shadows Kermit global 'local' */
  956. static int dout = -1;                   /* Data connection file descriptor */
  957. static int dpyactive = 0;               /* Data transfer is active */
  958. static int globaldin = -1;              /* Data connection f.d. */
  959. static int out2screen = 0;              /* GET output is to screen */
  960. static int forcetype = 0;               /* Force text or binary mode */
  961. static int cancelfile = 0;              /* File canceled */
  962. static int cancelgroup = 0;             /* Group canceled */
  963. static int anonymous = 0;               /* Logging in as anonymous */
  964. static int loggedin = 0;                /* Logged in (or not) */
  965. static int puterror = 0;                /* What to do on PUT error */
  966. static int geterror = 0;                /* What to do on GET error */
  967. static int rfrc = 0;                    /* remote_files() return code */
  968. static int okrestart = 0;               /* Server understands REST */
  969. static int printlines = 0;              /* getreply()should print data lines */
  970. static int haveurl = 0;                 /* Invoked by command-line FTP URL */
  971. static int mdtmok = 1;            /* Server supports MDTM */
  972. static int sizeok = 1;
  973. static int featok = 1;
  974. static int mlstok = 1;
  975. static int stouarg = 1;
  976. static int typesent = 0;
  977. static int havesigint = 0;
  978. static long havetype =  0;
  979. static CK_OFF_T havesize = (CK_OFF_T)-1;
  980. static char * havemdtm = NULL;
  981. static int mgetmethod = 0;        /* NLST or MLSD */
  982. static int mgetforced = 0;
  983.  
  984. static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
  985. static int c0, c1;                      /* Temp variables for characters */
  986.  
  987. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  988. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  989.  
  990. #define RFNBUFSIZ 4096            /* Remote filename buffer size */
  991.  
  992. static unsigned int maxbuf = 0, actualbuf = 0;
  993. static CHAR *ucbuf = NULL;
  994. static int ucbufsiz = 0;
  995. static unsigned int nout = 0;           /* Number of chars in ucbuf */
  996.  
  997. static jmp_buf recvcancel;
  998. static jmp_buf sendcancel;
  999. static jmp_buf ptcancel;
  1000. static jmp_buf jcancel;
  1001. static int ptabflg = 0;
  1002.  
  1003. /* Protection level symbols */
  1004.  
  1005. #define FPL_CLR 1                       /* Clear */
  1006. #define FPL_SAF 2                       /* Safe */
  1007. #define FPL_PRV 3                       /* Private */
  1008. #define FPL_CON 4                       /* Confidential */
  1009.  
  1010. /* Symbols for file types returned by MLST/MLSD */
  1011.  
  1012. #define FTYP_FILE 1            /* Regular file */
  1013. #define FTYP_DIR  2            /* Directory */
  1014. #define FTYP_CDIR 3            /* Current directory */
  1015. #define FTYP_PDIR 4            /* Parent directory */
  1016.  
  1017. /* File type symbols keyed to the file-type symbols from ckcker.h */
  1018.  
  1019. #define FTT_ASC XYFT_T                  /* ASCII (text) */
  1020. #define FTT_BIN XYFT_B                  /* Binary (image) */
  1021. #define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
  1022.  
  1023. /* Server feature table - sfttab[0] > 0 means server supports FEAT and OPTS */
  1024.  
  1025. static int sfttab[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  1026.  
  1027. #define SFT_AUTH  1            /* FTP server feature codes */
  1028. #define SFT_LANG  2
  1029. #define SFT_MDTM  3
  1030. #define SFT_MLST  4
  1031. #define SFT_PBSZ  5
  1032. #define SFT_PROT  6
  1033. #define SFT_REST  7
  1034. #define SFT_SIZE  8
  1035. #define SFT_TVFS  9
  1036. #define SFT_UTF8 10
  1037.  
  1038. #define CNV_AUTO  2            /* FTP filename conversion */
  1039. #define CNV_CNV   1
  1040. #define CNV_LIT   0
  1041.  
  1042. /* SET FTP values */
  1043.  
  1044. static int                              /* SET FTP values... */
  1045.   ftp_aut = 1,                          /* Auto-authentication */
  1046. #ifdef FTP_SECURITY
  1047.   ftp_cry = 1,                          /* Auto-encryption */
  1048.   ftp_cfw = 0,                          /* Credential forwarding */
  1049. #endif /* FTP_SECURITY */
  1050.   ftp_cpl = FPL_CLR,                    /* Command protection level */
  1051.   ftp_dpl = FPL_CLR,                    /* Data protection level */
  1052. #ifdef FTP_PROXY
  1053.   ftp_prx = 0,                          /* Use proxy */
  1054. #endif /* FTP_PROXY */
  1055.   sav_psv = -1,                         /* For saving passive mode */
  1056.   ftp_psv = 1,                          /* Passive mode */
  1057.   ftp_spc = 1,                          /* Send port commands */
  1058.   ftp_typ = FTT_ASC,                    /* Type */
  1059.   get_auto = 1,                         /* Automatic type switching for GET */
  1060.   tenex = 0,                            /* Type is Tenex */
  1061.   ftp_usn = 0,                          /* Unique server names */
  1062.   ftp_prm = 0,                          /* Permissions */
  1063.   ftp_cnv = CNV_AUTO,            /* Filename conversion (2 = auto) */
  1064.   ftp_vbm = DEF_VBM,                    /* Verbose mode */
  1065.   ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  1066.   ftp_err = 0,                          /* Error action */
  1067.   ftp_fnc = -1;                         /* Filename collision action */
  1068.  
  1069. #ifdef CK_SSL
  1070. static int ftp_bug_use_ssl_v2 = 0;      /* use SSLv2 for AUTH SSL */
  1071. #endif /* CK_SSL */
  1072.  
  1073. static int
  1074. #ifdef NOCSETS
  1075.   ftp_csr = -1,                         /* Remote (server) character set */
  1076. #else
  1077.   ftp_csr = FC_UTF8,
  1078. #endif /* NOCSETS */
  1079.   ftp_xla = 0;                          /* Character-set translation on/off */
  1080. int
  1081.   ftp_csx = -1,                         /* Remote charset currently in use */
  1082.   ftp_csl = -1;                         /* Local charset currently in use */
  1083.  
  1084. static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
  1085.  
  1086. char * ftp_nml = NULL;                  /* /NAMELIST */
  1087. char * ftp_tmp = NULL;                  /* Temporary string */
  1088. static char * ftp_acc = NULL;           /* Account string */
  1089. static char * auth_type = NULL;         /* Authentication type */
  1090. static char * srv_renam = NULL;         /* Server-rename string */
  1091. FILE * fp_nml = NULL;                   /* Namelist file pointer */
  1092.  
  1093. static int csocket = -1;                /* Control socket */
  1094. static int connected = 0;               /* Connected to FTP server */
  1095. /* static unsigned short ftp_port = 0; */ /* FTP port */ 
  1096. /* static int ftp_port = 0; */        /* SMS 2007/02/15 */
  1097. static int ftp_port = 0;        /* fdc 2007/08/30 */
  1098. #ifdef FTPHOST
  1099. static int hostcmd = 0;                 /* Has HOST command been sent */
  1100. #endif /* FTPHOST */
  1101. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  1102. static char bytename[8];
  1103.  
  1104. /* For parsing replies to FTP server command */
  1105. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  1106.  
  1107. #ifdef FTP_PROXY
  1108. static int proxy, unix_proxy
  1109. #endif /* FTP_PROXY */
  1110.  
  1111. static char pasv[64];                   /* Passive-mode port */
  1112. static int passivemode = 0;
  1113. static int sendport = 0;
  1114. static int servertype = 0;              /* FTP server's OS type */
  1115.  
  1116. static int testing = 0;
  1117. static char ftpcmdbuf[FTP_BUFSIZ];
  1118.  
  1119. /* Macro definitions */
  1120.  
  1121. #define UC(b) ckitoa(((int)b)&0xff)
  1122. #define nz(x) ((x) == 0 ? 1 : (x))
  1123.  
  1124. /* Command tables and definitions */
  1125.  
  1126. #define FTP_ACC  1                      /* FTP command keyword codes */
  1127. #define FTP_APP  2
  1128. #define FTP_CWD  3
  1129. #define FTP_CHM  4
  1130. #define FTP_CLS  5
  1131. #define FTP_DEL  6
  1132. #define FTP_DIR  7
  1133. #define FTP_GET  8
  1134. #define FTP_IDL  9
  1135. #define FTP_MDE 10
  1136. #define FTP_MDI 11
  1137. #define FTP_MGE 12
  1138. #define FTP_MKD 13
  1139. #define FTP_MOD 14
  1140. #define FTP_MPU 15
  1141. #define FTP_OPN 16
  1142. #define FTP_PUT 17
  1143. #define FTP_PWD 18
  1144. #define FTP_RGE 19
  1145. #define FTP_REN 20
  1146. #define FTP_RES 21
  1147. #define FTP_HLP 22
  1148. #define FTP_RMD 23
  1149. #define FTP_STA 24
  1150. #define FTP_SIT 25
  1151. #define FTP_SIZ 26
  1152. #define FTP_SYS 27
  1153. #define FTP_UMA 28
  1154. #define FTP_GUP 29
  1155. #define FTP_USR 30
  1156. #define FTP_QUO 31
  1157. #define FTP_TYP 32
  1158. #define FTP_FEA 33
  1159. #define FTP_OPT 34
  1160. #define FTP_CHK 35
  1161. #define FTP_VDI 36
  1162. #define FTP_ENA 37
  1163. #define FTP_DIS 38
  1164. #define FTP_REP 39
  1165.  
  1166. struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
  1167.     { "auto",    2, 0 },
  1168.     { "ftp",     1, 0 },
  1169.     { "kermit",  0, 0  }
  1170. };
  1171.  
  1172. static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
  1173.     { "proceed", 0, 0 },                /* 0 = proceed */
  1174.     { "quit",    1, 0 }                 /* 1 = quit */
  1175. };
  1176.  
  1177. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  1178.     { "account",   FTP_ACC, 0 },
  1179.     { "append",    FTP_APP, 0 },
  1180.     { "bye",       FTP_CLS, 0 },
  1181.     { "cd",        FTP_CWD, 0 },
  1182.     { "cdup",      FTP_GUP, 0 },
  1183.     { "check",     FTP_CHK, 0 },
  1184.     { "chmod",     FTP_CHM, 0 },
  1185.     { "close",     FTP_CLS, 0 },
  1186.     { "cwd",       FTP_CWD, CM_INV },
  1187.     { "delete",    FTP_MDE, 0 },
  1188.     { "directory", FTP_DIR, 0 },
  1189.     { "disable",   FTP_DIS, 0 },
  1190.     { "enable",    FTP_ENA, 0 },
  1191.     { "features",  FTP_FEA, 0 },
  1192.     { "get",       FTP_GET, 0 },
  1193.     { "help",      FTP_HLP, 0 },
  1194.     { "idle",      FTP_IDL, 0 },
  1195.     { "login",     FTP_USR, CM_INV },
  1196.     { "mdelete",   FTP_MDE, CM_INV },
  1197.     { "mget",      FTP_MGE, 0 },
  1198.     { "mkdir",     FTP_MKD, 0 },
  1199.     { "modtime",   FTP_MOD, 0 },
  1200.     { "mput",      FTP_MPU, 0 },
  1201.     { "open",      FTP_OPN, 0 },
  1202.     { "opt",       FTP_OPT, CM_INV|CM_ABR },
  1203.     { "opts",      FTP_OPT, CM_INV },
  1204.     { "options",   FTP_OPT, 0 },
  1205.     { "put",       FTP_PUT, 0 },
  1206.     { "pwd",       FTP_PWD, 0 },
  1207.     { "quit",      FTP_CLS, CM_INV },
  1208.     { "quote",     FTP_QUO, 0 },
  1209.     { "reget",     FTP_RGE, 0 },
  1210.     { "rename",    FTP_REN, 0 },
  1211.     { "reput",     FTP_REP, 0 },
  1212.     { "resend",    FTP_REP, CM_INV },
  1213.     { "reset",     FTP_RES, 0 },
  1214.     { "rmdir",     FTP_RMD, 0 },
  1215.     { "send",      FTP_PUT, CM_INV },
  1216.     { "site",      FTP_SIT, 0 },
  1217.     { "size",      FTP_SIZ, 0 },
  1218.     { "status",    FTP_STA, 0 },
  1219.     { "system",    FTP_SYS, 0 },
  1220.     { "type",      FTP_TYP, 0 },
  1221.     { "umask",     FTP_UMA, 0 },
  1222.     { "up",        FTP_GUP, CM_INV },
  1223.     { "user",      FTP_USR, 0 },
  1224.     { "vdirectory",FTP_VDI, 0 },
  1225.     { "", 0, 0 }
  1226. };
  1227. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  1228.  
  1229. #define OPN_ANO 1            /* FTP OPEN switch codes */
  1230. #define OPN_PSW 2
  1231. #define OPN_USR 3
  1232. #define OPN_ACC 4
  1233. #define OPN_ACT 5
  1234. #define OPN_PSV 6
  1235. #define OPN_TLS 7
  1236. #define OPN_NIN 8
  1237. #define OPN_NOL 9
  1238.  
  1239. #ifdef FTP_SECURITY
  1240. #ifdef CK_SSL
  1241. #define USETLSTAB
  1242. static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
  1243.     { "/ssl",       OPN_TLS, 0    },
  1244.     { "/tls",       OPN_TLS, 0    },
  1245.     { "", 0, 0 }
  1246. };
  1247. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  1248. #endif /* CK_SSL */
  1249. #endif /* FTP_SECURITY */
  1250.  
  1251. static struct keytab ftpswitab[] = {    /* FTP command switches */
  1252.     { "/account",   OPN_ACC, CM_ARG },
  1253.     { "/active",    OPN_ACT, 0      },
  1254.     { "/anonymous", OPN_ANO, 0      },
  1255.     { "/noinit",    OPN_NIN, 0      },
  1256.     { "/nologin",   OPN_NOL, 0      },
  1257.     { "/passive",   OPN_PSV, 0      },
  1258.     { "/password",  OPN_PSW, CM_ARG },
  1259.     { "/user",      OPN_USR, CM_ARG },
  1260.     { "", 0, 0 }
  1261. };
  1262. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  1263.  
  1264. /* FTP { ENABLE, DISABLE } items */
  1265.  
  1266. #define ENA_FEAT 1
  1267. #define ENA_MDTM 2
  1268. #define ENA_MLST 3
  1269. #define ENA_SIZE 4
  1270. #define ENA_AUTH 5
  1271.  
  1272. static struct keytab ftpenatab[] = {
  1273.     { "AUTH",  ENA_AUTH, 0 },
  1274.     { "FEAT",  ENA_FEAT, 0 },
  1275.     { "MDTM",  ENA_MDTM, 0 },
  1276.     { "ML",    ENA_MLST, CM_INV|CM_ABR },
  1277.     { "MLS",   ENA_MLST, CM_INV|CM_ABR },
  1278.     { "MLSD",  ENA_MLST, CM_INV },
  1279.     { "MLST",  ENA_MLST, 0 },
  1280.     { "SIZE",  ENA_SIZE, 0 },
  1281.     { "", 0, 0 }
  1282. };
  1283. static int nftpena = (sizeof(ftpenatab) / sizeof(struct keytab)) - 1;
  1284.  
  1285. /* SET FTP command keyword indices */
  1286.  
  1287. #define FTS_AUT  1                      /* Autoauthentication */
  1288. #define FTS_CRY  2                      /* Encryption */
  1289. #define FTS_LOG  3                      /* Autologin */
  1290. #define FTS_CPL  4                      /* Command protection level */
  1291. #define FTS_CFW  5                      /* Credentials forwarding */
  1292. #define FTS_DPL  6                      /* Data protection level */
  1293. #define FTS_DBG  7                      /* Debugging */
  1294. #define FTS_PSV  8                      /* Passive mode */
  1295. #define FTS_SPC  9                      /* Send port commands */
  1296. #define FTS_TYP 10                      /* (file) Type */
  1297. #define FTS_USN 11                      /* Unique server names (for files) */
  1298. #define FTS_VBM 12                      /* Verbose mode */
  1299. #define FTS_ATP 13                      /* Authentication type */
  1300. #define FTS_CNV 14                      /* Filename conversion */
  1301. #define FTS_TST 15                      /* Test (progress) messages */
  1302. #define FTS_PRM 16                      /* (file) Permissions */
  1303. #define FTS_XLA 17                      /* Charset translation */
  1304. #define FTS_CSR 18                      /* Server charset */
  1305. #define FTS_ERR 19                      /* Error action */
  1306. #define FTS_FNC 20                      /* Collision */
  1307. #define FTS_SRP 21                      /* SRP options */
  1308. #define FTS_GFT 22                      /* GET automatic file-type switching */
  1309. #define FTS_DAT 23                      /* Set file dates */
  1310. #define FTS_STO 24            /* Server time offset */
  1311. #define FTS_APW 25            /* Anonymous password */
  1312. #define FTS_DIS 26            /* File-transfer display style */
  1313. #define FTS_BUG 27                      /* Bug(s) */
  1314. #define FTS_TMO 28            /* Timeout */
  1315.  
  1316. /* FTP BUGS */
  1317.  
  1318. #define FTB_SV2  1                      /* use SSLv2 */
  1319.  
  1320. static struct keytab ftpbugtab[] = {
  1321.     { "use-ssl-v2",     FTB_SV2,        0 }
  1322. };
  1323. static int nftpbug = (sizeof(ftpbugtab) / sizeof(struct keytab));
  1324.  
  1325. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1326.  
  1327. #define PUT_UPD 1                       /* Update */
  1328. #define PUT_RES 2                       /* Restart */
  1329. #define PUT_SIM 4                       /* Simulation */
  1330. #define PUT_DIF 8            /* Dates Differ */
  1331.  
  1332. static struct keytab ftpcolxtab[] = { /* SET FTP COLLISION options */
  1333. #ifndef MAC
  1334.     { "append",    XYFX_A, 0 },         /* append to old file */
  1335. #endif /* MAC */
  1336. #ifdef COMMENT
  1337.     { "ask",       XYFX_Q, 0 },         /* ask what to do (not implemented) */
  1338. #endif
  1339.     { "backup",    XYFX_B, 0 },         /* rename old file */
  1340. #ifndef MAC
  1341.     { "dates-differ", XYFX_M, 0 },    /* accept if dates differ */
  1342.     { "discard",   XYFX_D, 0 },         /* don't accept new file */
  1343.     { "no-supersede", XYFX_D, CM_INV }, /* ditto (MSK compatibility) */
  1344. #endif /* MAC */
  1345.     { "overwrite", XYFX_X, 0 },         /* overwrite the old file */
  1346.     { "rename",    XYFX_R, 0 },         /* rename the incoming file */
  1347. #ifndef MAC                             /* This crashes Mac Kermit. */
  1348.     { "update",    XYFX_U, 0 },         /* replace if newer */
  1349. #endif /* MAC */
  1350.     { "", 0, 0 }
  1351. };
  1352. static int nftpcolx = (sizeof(ftpcolxtab) / sizeof(struct keytab)) - 1;
  1353.  
  1354.  
  1355. #ifdef FTP_SECURITY
  1356. /* FTP authentication options */
  1357.  
  1358. #define FTA_AUTO 0                      /* Auto */
  1359. #define FTA_SRP  1                      /* SRP */
  1360. #define FTA_GK5  2                      /* Kerberos 5 */
  1361. #define FTA_K4   3                      /* Kerberos 4 */
  1362. #define FTA_SSL  4                      /* SSL */
  1363. #define FTA_TLS  5                      /* TLS */
  1364.  
  1365. /* FTP authentication types */
  1366.  
  1367. #define FTPATYPS 8
  1368. static int ftp_auth_type[FTPATYPS] = {
  1369. #ifdef FTP_GSSAPI
  1370.     FTA_GK5,                            /* GSSAPI Kerberos 5 */
  1371. #endif /* FTP_GK5 */
  1372. #ifdef FTP_SRP
  1373.     FTA_SRP,                            /* SRP */
  1374. #endif /* FTP_SRP */
  1375. #ifdef FTP_KRB4
  1376.     FTA_K4,                             /* Kerberos 4 */
  1377. #endif /* FTP_KRB4 */
  1378. #ifdef CK_SSL
  1379.     FTA_TLS,                            /* TLS */
  1380.     FTA_SSL,                            /* SSL */
  1381. #endif /* CK_SSL */
  1382.     0
  1383. };
  1384.  
  1385. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1386.     { "automatic", FTA_AUTO,  CM_INV },
  1387. #ifdef FTP_GSSAPI
  1388.     { "gssapi-krb5", FTA_GK5, 0 },
  1389. #endif /* FTP_GSSAPI */
  1390. #ifdef FTP_KRB4
  1391.     { "k4",       FTA_K4,     CM_INV },
  1392. #endif /* FTP_KRB4 */
  1393. #ifdef FTP_GSSAPI
  1394.     { "k5",        FTA_GK5,   CM_INV },
  1395. #endif /* FTP_GSSAPI */
  1396. #ifdef FTP_KRB4
  1397.     { "kerberos4", FTA_K4,    0 },
  1398. #endif /* FTP_KRB4 */
  1399. #ifdef FTP_GSSAPI
  1400.     { "kerberos5", FTA_GK5,   CM_INV },
  1401. #endif /* FTP_GSSAPI */
  1402. #ifdef FTP_KRB4
  1403.     { "kerberos_iv",FTA_K4,   CM_INV },
  1404. #endif /* FTP_KRB4 */
  1405. #ifdef FTP_GSSAPI
  1406.     { "kerberos_v", FTA_GK5,  CM_INV },
  1407. #endif /* FTP_GSSAPI */
  1408. #ifdef FTP_KRB4
  1409.     { "krb4",     FTA_K4,     CM_INV },
  1410. #endif /* FTP_KRB4 */
  1411. #ifdef FTP_GSSAPI
  1412.     { "krb5",     FTA_GK5,    CM_INV },
  1413. #endif /* FTP_GSSAPI */
  1414. #ifdef FTP_SRP
  1415.     { "srp",      FTA_SRP,     0 },
  1416. #endif /* FTP_SRP */
  1417. #ifdef CK_SSL
  1418.     { "ssl",      FTA_SSL,     0 },
  1419.     { "tls",      FTA_TLS,     0 },
  1420. #endif /* CK_SSL */
  1421.     { "", 0, 0 }
  1422. };
  1423. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1424.  
  1425. #ifdef FTP_SRP
  1426. #define SRP_CIPHER 1
  1427. #define SRP_HASH   2
  1428. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1429.     { "cipher",   SRP_CIPHER,     0 },
  1430.     { "hash",     SRP_HASH,       0 },
  1431.     { "", 0, 0 }
  1432. };
  1433. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1434. #endif /* FTP_SRP */
  1435. #endif /* FTP_SECURITY */
  1436.  
  1437. static struct keytab ftpset[] = {       /* SET FTP commmand table */
  1438.     { "anonymous-password",       FTS_APW, 0 },
  1439. #ifdef FTP_SECURITY
  1440.     { "authtype",                 FTS_ATP, 0 },
  1441.     { "autoauthentication",       FTS_AUT, 0 },
  1442.     { "autoencryption",           FTS_CRY, 0 },
  1443. #endif /* FTP_SECURITY */
  1444.     { "autologin",                FTS_LOG, 0 },
  1445.     { "bug",                      FTS_BUG, 0 },
  1446. #ifndef NOCSETS
  1447.     { "character-set-translation",FTS_XLA, 0 },
  1448. #endif /* NOCSETS */
  1449.     { "collision",                FTS_FNC, 0 },
  1450. #ifdef FTP_SECURITY
  1451.     { "command-protection-level", FTS_CPL, 0 },
  1452.     { "cpl",                      FTS_CPL, CM_INV },
  1453.     { "credential-forwarding",    FTS_CFW, 0 },
  1454.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1455.     { "data-protection-level",    FTS_DPL, 0 },
  1456. #endif /* FTP_SECURITY */
  1457.     { "dates",                    FTS_DAT, 0 },
  1458.     { "debug",                    FTS_DBG, 0 },
  1459.     { "display",                  FTS_DIS, 0 },
  1460. #ifdef FTP_SECURITY
  1461.     { "dpl",                      FTS_DPL, CM_INV },
  1462. #endif /* FTP_SECURITY */
  1463.     { "error-action",             FTS_ERR, 0 },
  1464.     { "filenames",                FTS_CNV, 0 },
  1465.     { "get-filetype-switching",   FTS_GFT, 0 },
  1466.     { "passive-mode",             FTS_PSV, 0 },
  1467.     { "pasv",                     FTS_PSV, CM_INV },
  1468.     { "permissions",              FTS_PRM, 0 },
  1469.     { "progress-messages",        FTS_TST, 0 },
  1470.     { "send-port-commands",       FTS_SPC, 0 },
  1471. #ifndef NOCSETS
  1472.     { "server-character-set",     FTS_CSR, 0 },
  1473. #endif /* NOCSETS */
  1474.     { "server-time-offset",       FTS_STO, 0 },
  1475. #ifdef FTP_SRP
  1476.     { "srp",                      FTS_SRP, 0 },
  1477. #else
  1478.     { "srp",                      FTS_SRP, CM_INV },
  1479. #endif /* FTP_SRP */
  1480. #ifdef FTP_TIMEOUT
  1481.     { "timeout",                  FTS_TMO, 0 },
  1482. #endif    /* FTP_TIMEOUT */
  1483.     { "type",                     FTS_TYP, 0 },
  1484.     { "unique-server-names",      FTS_USN, 0 },
  1485.     { "verbose-mode",             FTS_VBM, 0 },
  1486.     { "", 0, 0 }
  1487. };
  1488. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1489.  
  1490. /*
  1491.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1492.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1493.   Don't just make up new ones, since the number of SND_xxx options must be
  1494.   known in advance for the switch-parsing arrays.
  1495. */
  1496. #define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
  1497. #define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
  1498. #define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
  1499.  
  1500. static struct keytab putswi[] = {       /* FTP PUT switch table */
  1501.     { "/after",                SND_AFT, CM_ARG },
  1502. #ifdef PUTARRAY
  1503.     { "/array",                SND_ARR, CM_ARG },
  1504. #endif /* PUTARRAY */
  1505.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1506.     { "/as-name",              SND_ASN, CM_ARG },
  1507.     { "/ascii",                SND_TXT, CM_INV },
  1508.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1509.     { "/before",               SND_BEF, CM_ARG },
  1510.     { "/binary",               SND_BIN, 0 },
  1511. #ifdef PUTPIPE
  1512.     { "/command",              SND_CMD, CM_PSH },
  1513. #endif /* PUTPIPE */
  1514. #ifdef COMMENT
  1515. /* This works but it's dangerous */
  1516. #ifdef DOUPDATE
  1517.     { "/dates-differ",         SND_DIF, CM_INV },
  1518. #endif /* DOUPDATE */
  1519. #endif /* COMMENT */
  1520.     { "/delete",               SND_DEL, 0 },
  1521. #ifdef UNIXOROSK
  1522.     { "/dotfiles",             SND_DOT, 0 },
  1523. #endif /* UNIXOROSK */
  1524.     { "/error-action",         SND_ERR, CM_ARG },
  1525.     { "/except",               SND_EXC, CM_ARG },
  1526.     { "/filenames",            SND_NAM, CM_ARG },
  1527. #ifdef PIPESEND
  1528. #ifndef NOSPL
  1529.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1530. #endif /* NOSPL */
  1531. #endif /* PIPESEND */
  1532. #ifdef CKSYMLINK
  1533.     { "/followlinks",          SND_LNK, 0 },
  1534. #endif /* CKSYMLINK */
  1535. #ifdef VMS
  1536.     { "/image",                SND_IMG, 0 },
  1537. #else
  1538.     { "/image",                SND_BIN, CM_INV },
  1539. #endif /* VMS */
  1540.     { "/larger-than",          SND_LAR, CM_ARG },
  1541.     { "/listfile",             SND_FIL, CM_ARG },
  1542. #ifndef NOCSETS
  1543.     { "/local-character-set",  SND_CSL, CM_ARG },
  1544. #endif /* NOCSETS */
  1545. #ifdef CK_TMPDIR
  1546.     { "/move-to",              SND_MOV, CM_ARG },
  1547. #endif /* CK_TMPDIR */
  1548.     { "/nobackupfiles",        SND_NOB, 0 },
  1549. #ifdef UNIXOROSK
  1550.     { "/nodotfiles",           SND_NOD, 0 },
  1551. #endif /* UNIXOROSK */
  1552. #ifdef CKSYMLINK
  1553.     { "/nofollowlinks",        SND_NLK, 0 },
  1554. #endif /* CKSYMLINK */
  1555.  
  1556.     { "/not-after",            SND_NAF, CM_ARG },
  1557.     { "/not-before",           SND_NBE, CM_ARG },
  1558. #ifdef UNIX
  1559.     { "/permissions",          SND_PRM, CM_ARG },
  1560. #else
  1561.     { "/permissions",          SND_PRM, CM_ARG|CM_INV },
  1562. #endif /* UNIX */
  1563.     { "/quiet",                SND_SHH, 0 },
  1564. #ifdef FTP_RESTART
  1565.     { "/recover",              SND_RES, 0 },
  1566. #endif /* FTP_RESTART */
  1567. #ifdef RECURSIVE
  1568.     { "/recursive",            SND_REC, 0 },
  1569. #endif /* RECURSIVE */
  1570.     { "/rename-to",            SND_REN, CM_ARG },
  1571. #ifdef FTP_RESTART
  1572.     { "/restart",              SND_RES, CM_INV },
  1573. #endif /* FTP_RESTART */
  1574. #ifndef NOCSETS
  1575.     { "/server-character-set", SND_CSR, CM_ARG },
  1576. #endif /* NOCSETS */
  1577.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1578.     { "/simulate",             SND_SIM, 0 },
  1579.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1580.     { "/smaller-than",         SND_SMA, CM_ARG },
  1581. #ifdef COMMENT
  1582.     { "/starting-at",          SND_STA, CM_ARG },
  1583. #endif /* COMMENT */
  1584. #ifdef RECURSIVE
  1585.     { "/subdirectories",       SND_REC, CM_INV },
  1586. #endif /* RECURSIVE */
  1587.     { "/tenex",                SND_TEN, 0 },
  1588.     { "/text",                 SND_TXT, 0 },
  1589. #ifndef NOCSETS
  1590.     { "/transparent",          SND_XPA, 0 },
  1591. #endif /* NOCSETS */
  1592.     { "/type",                 SND_TYP, CM_ARG },
  1593. #ifdef DOUPDATE
  1594.     { "/update",               SND_UPD, 0 },
  1595. #endif /* DOUPDATE */
  1596.     { "/unique-server-names",  SND_USN, 0 },
  1597.     { "", 0, 0 }
  1598. };
  1599. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1600.  
  1601. static struct keytab getswi[] = {       /* FTP [M]GET switch table */
  1602.     { "/after",                SND_AFT, CM_INV },
  1603.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1604.     { "/as-name",              SND_ASN, CM_ARG },
  1605.     { "/ascii",                SND_TXT, CM_INV },
  1606.     { "/before",               SND_BEF, CM_INV },
  1607.     { "/binary",               SND_BIN, 0 },
  1608.     { "/collision",            SND_COL, CM_ARG },
  1609. #ifdef PUTPIPE
  1610.     { "/command",              SND_CMD, CM_PSH },
  1611. #endif /* PUTPIPE */
  1612.     { "/delete",               SND_DEL, 0 },
  1613.     { "/error-action",         SND_ERR, CM_ARG },
  1614.     { "/except",               SND_EXC, CM_ARG },
  1615.     { "/filenames",            SND_NAM, CM_ARG },
  1616. #ifdef PIPESEND
  1617. #ifndef NOSPL
  1618.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1619. #endif /* NOSPL */
  1620. #endif /* PIPESEND */
  1621. #ifdef VMS
  1622.     { "/image",                SND_IMG, 0 },
  1623. #else
  1624.     { "/image",                SND_BIN, CM_INV },
  1625. #endif /* VMS */
  1626.     { "/larger-than",          SND_LAR, CM_ARG },
  1627.     { "/listfile",             SND_FIL, CM_ARG },
  1628. #ifndef NOCSETS
  1629.     { "/local-character-set",  SND_CSL, CM_ARG },
  1630. #endif /* NOCSETS */
  1631.     { "/match",                SND_PAT, CM_ARG },
  1632.     { "/ml",                   SND_MLS, CM_INV|CM_ABR },
  1633.     { "/mls",                  SND_MLS, CM_INV|CM_ABR },
  1634.     { "/mlsd",                 SND_MLS, 0 },
  1635.     { "/mlst",                 SND_MLS, CM_INV },
  1636. #ifdef CK_TMPDIR
  1637.     { "/move-to",              SND_MOV, CM_ARG },
  1638. #endif /* CK_TMPDIR */
  1639.     { "/namelist",             SND_NML, CM_ARG },
  1640.     { "/nlst",                 SND_NLS, 0 },
  1641.     { "/nobackupfiles",        SND_NOB, 0 },
  1642.     { "/nodotfiles",           SND_NOD, 0 },
  1643. #ifdef DOUPDATE
  1644.     { "/dates-differ",         SND_DIF, CM_INV },
  1645. #endif /* DOUPDATE */
  1646.     { "/not-after",            SND_NAF, CM_INV },
  1647.     { "/not-before",           SND_NBE, CM_INV },
  1648.     { "/permissions",          SND_PRM, CM_INV },
  1649.     { "/quiet",                SND_SHH, 0 },
  1650. #ifdef FTP_RESTART
  1651.     { "/recover",              SND_RES, 0 },
  1652. #endif /* FTP_RESTART */
  1653. #ifdef RECURSIVE
  1654.     { "/recursive",            SND_REC, 0 },
  1655. #endif /* RECURSIVE */
  1656.     { "/rename-to",            SND_REN, CM_ARG },
  1657. #ifdef FTP_RESTART
  1658.     { "/restart",              SND_RES, CM_INV },
  1659. #endif /* FTP_RESTART */
  1660. #ifndef NOCSETS
  1661.     { "/server-character-set", SND_CSR, CM_ARG },
  1662. #endif /* NOCSETS */
  1663.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1664.     { "/smaller-than",         SND_SMA, CM_ARG },
  1665. #ifdef RECURSIVE
  1666.     { "/subdirectories",       SND_REC, CM_INV },
  1667. #endif /* RECURSIVE */
  1668.     { "/text",                 SND_TXT, 0 },
  1669.     { "/tenex",                SND_TEN, 0 },
  1670. #ifndef NOCSETS
  1671.     { "/transparent",          SND_XPA, 0 },
  1672. #endif /* NOCSETS */
  1673.     { "/to-screen",            SND_MAI, 0 },
  1674. #ifdef DOUPDATE
  1675.     { "/update",               SND_UPD, CM_INV },
  1676. #endif /* DOUPDATE */
  1677.     { "", 0, 0 }
  1678. };
  1679. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1680.  
  1681. static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
  1682.     { "/error-action",         SND_ERR, CM_ARG },
  1683.     { "/except",               SND_EXC, CM_ARG },
  1684.     { "/filenames",            SND_NAM, CM_ARG },
  1685.     { "/larger-than",          SND_LAR, CM_ARG },
  1686.     { "/nobackupfiles",        SND_NOB, 0 },
  1687. #ifdef UNIXOROSK
  1688.     { "/nodotfiles",           SND_NOD, 0 },
  1689. #endif /* UNIXOROSK */
  1690.     { "/quiet",                SND_SHH, 0 },
  1691. #ifdef RECURSIVE
  1692.     { "/recursive",            SND_REC, 0 },
  1693. #endif /* RECURSIVE */
  1694.     { "/smaller-than",         SND_SMA, CM_ARG },
  1695. #ifdef RECURSIVE
  1696.     { "/subdirectories",       SND_REC, CM_INV },
  1697. #endif /* RECURSIVE */
  1698.     { "", 0, 0 }
  1699. };
  1700. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1701.  
  1702. static struct keytab fntab[] = {        /* Filename conversion keyword table */
  1703.     { "automatic",    2, CNV_AUTO },
  1704.     { "converted",    1, CNV_CNV  },
  1705.     { "literal",      0, CNV_LIT  }
  1706. };
  1707. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1708.  
  1709. static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
  1710.     { "ascii",        FTT_ASC, 0 },
  1711.     { "binary",       FTT_BIN, 0 },
  1712.     { "tenex",        FTT_TEN, 0 },
  1713.     { "text",         FTT_ASC, CM_INV },
  1714.     { "", 0, 0 }
  1715. };
  1716. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1717.  
  1718. #ifdef FTP_SECURITY
  1719. static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
  1720.     { "clear",        FPL_CLR, 0 },
  1721.     { "confidential", FPL_CON, 0 },
  1722.     { "private",      FPL_PRV, 0 },
  1723.     { "safe",         FPL_SAF, 0 },
  1724.     { "", 0, 0 }
  1725. };
  1726. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1727. #endif /* FTP_SECURITY */
  1728.  
  1729. /* Definitions for FTP from RFC765. */
  1730.  
  1731. /* Reply codes */
  1732.  
  1733. #define REPLY_PRELIM    1               /* Positive preliminary */
  1734. #define REPLY_COMPLETE  2               /* Positive completion */
  1735. #define REPLY_CONTINUE  3               /* Positive intermediate */
  1736. #define REPLY_TRANSIENT 4               /* Transient negative completion */
  1737. #define REPLY_ERROR     5               /* Permanent negative completion */
  1738. #define REPLY_SECURE    6               /* Security encoded message */
  1739.  
  1740. /* Form codes and names */
  1741.  
  1742. #define FORM_N 1                        /* Non-print */
  1743. #define FORM_T 2                        /* Telnet format effectors */
  1744. #define FORM_C 3                        /* Carriage control (ASA) */
  1745.  
  1746. /* Structure codes and names */
  1747.  
  1748. #define STRU_F 1                        /* File (no record structure) */
  1749. #define STRU_R 2                        /* Record structure */
  1750. #define STRU_P 3                        /* Page structure */
  1751.  
  1752. /* Mode types and names */
  1753.  
  1754. #define MODE_S 1                        /* Stream */
  1755. #define MODE_B 2                        /* Block */
  1756. #define MODE_C 3                        /* Compressed */
  1757.  
  1758. /* Protection levels and names */
  1759.  
  1760. #define PROT_C 1                        /* Clear */
  1761. #define PROT_S 2                        /* Safe */
  1762. #define PROT_P 3                        /* Private */
  1763. #define PROT_E 4                        /* Confidential */
  1764.  
  1765. #ifdef COMMENT                          /* Not used */
  1766. #ifdef FTP_NAMES
  1767. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1768. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1769. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1770. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1771. #endif /* FTP_NAMES */
  1772.  
  1773. /* Record Tokens */
  1774.  
  1775. #define REC_ESC '\377'                  /* Record-mode Escape */
  1776. #define REC_EOR '\001'                  /* Record-mode End-of-Record */
  1777. #define REC_EOF '\002'                  /* Record-mode End-of-File */
  1778.  
  1779. /* Block Header */
  1780.  
  1781. #define BLK_EOR           0x80          /* Block is End-of-Record */
  1782. #define BLK_EOF           0x40          /* Block is End-of-File */
  1783. #define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
  1784. #define BLK_RESTART       0x10          /* Block is Restart Marker */
  1785. #define BLK_BYTECOUNT 2                 /* Bytes in this block */
  1786. #endif /* COMMENT */
  1787.  
  1788. #define RADIX_ENCODE 0                  /* radix_encode() function codes */
  1789. #define RADIX_DECODE 1
  1790.  
  1791. /*
  1792.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1793.   results in a serious performance degradation due to the increased number
  1794.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1795.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1796.   of the typical TCP window.  Maybe we should add a command to allow the value
  1797.   to be changed.
  1798. */
  1799. #define DEFAULT_PBSZ 1<<13
  1800.  
  1801. /* Prototypes */
  1802.  
  1803. _PROTOTYP(int remtxt, (char **) );
  1804. _PROTOTYP(char * gskreason, (int) );
  1805. _PROTOTYP(static int ftpclose,(void));
  1806. _PROTOTYP(static int zzsend, (int, CHAR));
  1807. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1808. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1809. _PROTOTYP(static int setpbsz,(unsigned int));
  1810. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1811.   int,int,char *,int,int,int));
  1812. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1813. _PROTOTYP(static int fts_cpl,(int));
  1814. _PROTOTYP(static int fts_dpl,(int));
  1815. #ifdef FTP_SECURITY
  1816. _PROTOTYP(static int ftp_auth, (void));
  1817. #endif /* FTP_SECURITY */
  1818. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1819. _PROTOTYP(static int ftp_login, (char *));
  1820. _PROTOTYP(static int ftp_reset, (void));
  1821. _PROTOTYP(static int ftp_rename, (char *, char *));
  1822. _PROTOTYP(static int ftp_umask, (char *));
  1823. _PROTOTYP(static int secure_flush, (int));
  1824. #ifdef COMMENT
  1825. _PROTOTYP(static int secure_putc, (char, int));
  1826. #endif /* COMMENT */
  1827. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1828. _PROTOTYP(static int scommand, (char *));
  1829. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1830. _PROTOTYP(static int secure_getc, (int, int));
  1831. _PROTOTYP(static int secure_getbyte, (int, int));
  1832. _PROTOTYP(static int secure_read, (int, char *, int));
  1833. _PROTOTYP(static int initconn, (void));
  1834. _PROTOTYP(static int dataconn, (char *));
  1835. _PROTOTYP(static int setprotbuf,(unsigned int));
  1836. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1837.  
  1838. _PROTOTYP(static char * radix_error,(int));
  1839. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1840. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, CHAR *, int));
  1841.  
  1842. _PROTOTYP(static VOID mlsreset, (void));
  1843. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1844. _PROTOTYP(static VOID lostpeer, (void));
  1845. _PROTOTYP(static VOID cancel_remote, (int));
  1846. _PROTOTYP(static VOID changetype, (int, int));
  1847.  
  1848. _PROTOTYP(static sigtype cmdcancel, (int));
  1849.  
  1850. #ifdef FTP_SRP
  1851. _PROTOTYP(static int srp_reset, ());
  1852. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1853. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1854. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1855. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1856. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1857. _PROTOTYP(static int srp_selcipher, (char *));
  1858. _PROTOTYP(static int srp_selhash, (char *));
  1859. #endif /* FTP_SRP */
  1860.  
  1861. #ifdef FTP_GSSAPI
  1862. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1863. #endif /* FTP_GSSAPI */
  1864.  
  1865. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1866.  
  1867. #ifdef FTP_SECURITY
  1868. #ifndef NOICP
  1869. #define FT_NOGSS   1
  1870. #define FT_NOK4    2
  1871. #define FT_NOSRP   3
  1872. #define FT_NOSSL   4
  1873. #define FT_NOTLS   5
  1874. #define FT_CERTFI  6
  1875. #define FT_OKCERT  7
  1876. #define FT_DEBUG   8
  1877. #define FT_KEY     9
  1878. #define FT_SECURE 10
  1879. #define FT_VERIFY 11
  1880.  
  1881. static struct keytab ftpztab[] = {
  1882.     { "!gss",    FT_NOGSS,  0 },
  1883.     { "!krb4",   FT_NOK4,   0 },
  1884.     { "!srp",    FT_NOSRP,  0 },
  1885.     { "!ssl",    FT_NOSSL,  0 },
  1886.     { "!tls",    FT_NOTLS,  0 },
  1887.     { "cert",    FT_CERTFI, CM_ARG },
  1888.     { "certsok", FT_OKCERT, 0 },
  1889.     { "debug",   FT_DEBUG,  0 },
  1890.     { "key",     FT_KEY,    CM_ARG },
  1891.     { "nogss",   FT_NOGSS,  0 },
  1892.     { "nokrb4",  FT_NOK4,   0 },
  1893.     { "nosrp",   FT_NOSRP,  0 },
  1894.     { "nossl",   FT_NOSSL,  0 },
  1895.     { "notls",   FT_NOTLS,  0 },
  1896. #ifdef COMMENT
  1897.     { "secure",  FT_SECURE, 0 },
  1898. #endif /* COMMENT */
  1899.     { "verify",  FT_VERIFY, CM_ARG },
  1900.     { "", 0, 0 }
  1901. };
  1902. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1903.  
  1904. /*
  1905.   The following cipher and hash tables should be replaced with
  1906.   dynamicly created versions based upon the linked library.
  1907. */
  1908. #define SRP_BLOWFISH_ECB    1
  1909. #define SRP_BLOWFISH_CBC    2
  1910. #define SRP_BLOWFISH_CFB64  3
  1911. #define SRP_BLOWFISH_OFB64  4
  1912. #define SRP_CAST5_ECB       5
  1913. #define SRP_CAST5_CBC       6
  1914. #define SRP_CAST5_CFB64     7
  1915. #define SRP_CAST5_OFB64     8
  1916. #define SRP_DES_ECB         9
  1917. #define SRP_DES_CBC        10
  1918. #define SRP_DES_CFB64      11
  1919. #define SRP_DES_OFB64      12
  1920. #define SRP_DES3_ECB       13
  1921. #define SRP_DES3_CBC       14
  1922. #define SRP_DES3_CFB64     15
  1923. #define SRP_DES3_OFB64     16
  1924.  
  1925. static struct keytab ciphertab[] = {
  1926.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1927.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1928.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1929.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1930.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1931.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1932.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1933.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1934.     { "des_ecb",        SRP_DES_ECB,        0 },
  1935.     { "des_cbc",        SRP_DES_CBC,        0 },
  1936.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1937.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1938.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1939.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1940.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1941.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1942.     { "none",           0, 0 },
  1943.     { "", 0, 0 }
  1944. };
  1945. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1946.  
  1947. #define SRP_MD5  1
  1948. #define SRP_SHA  2
  1949. static struct keytab hashtab[] = {
  1950.     { "md5",              SRP_MD5,        0 },
  1951.     { "none",             0,              0 },
  1952.     { "sha",              SRP_SHA,        0 },
  1953.     { "", 0, 0 }
  1954. };
  1955. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1956. #endif /* NOICP */
  1957. #endif /* FTP_SECURITY */
  1958.  
  1959. static char *
  1960. strval(s1,s2) char * s1, * s2; {
  1961.     if (!s1) s1 = "";
  1962.     if (!s2) s2 = "";
  1963.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1964. }
  1965.  
  1966. #ifndef NOCSETS
  1967. static char * rfnptr = NULL;
  1968. static int rfnlen = 0;
  1969. static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
  1970. static char * xgnbp = NULL;
  1971.  
  1972. static int
  1973. strgetc() {                             /* Helper function for xgnbyte() */
  1974.     int c;
  1975.     if (!xgnbp)
  1976.       return(-1);
  1977.     if (!*xgnbp)
  1978.       return(-1);
  1979.     c = (unsigned) *xgnbp++;
  1980.     return(((unsigned) c) & 0xff);
  1981. }
  1982.  
  1983. static int                              /* Helper function for xpnbyte() */
  1984. #ifdef CK_ANSIC
  1985. strputc(char c)
  1986. #else
  1987. strputc(c) char c;
  1988. #endif /* CK_ANSIC */
  1989. {
  1990.     rfnlen = rfnptr - rfnbuf;
  1991.     if (rfnlen >= (RFNBUFSIZ - 1))
  1992.       return(-1);
  1993.     *rfnptr++ = c;
  1994.     *rfnptr = NUL;
  1995.     return(0);
  1996. }
  1997.  
  1998. static int
  1999. #ifdef CK_ANSIC
  2000. xprintc(char c)
  2001. #else
  2002. xprintc(c) char c;
  2003. #endif /* CK_ANSIC */
  2004. {
  2005.     printf("%c",c);
  2006.     return(0);
  2007. }
  2008.  
  2009. static VOID
  2010. bytswap(c0,c1) int * c0, * c1; {
  2011.     int t;
  2012.     t = *c0;
  2013.     *c0 = *c1;
  2014.     *c1 = t;
  2015. }
  2016. #endif /* NOCSETS */
  2017.  
  2018. #ifdef CKLOGDIAL
  2019. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  2020. int ftplogactive = 0;
  2021. long ftplogprev = 0L;
  2022.  
  2023. VOID
  2024. ftplogend() {
  2025.     extern int dialog;
  2026.     extern char diafil[];
  2027.     long d1, d2, t1, t2;
  2028.     char buf[32], * p;
  2029.  
  2030.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  2031.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  2032.  
  2033.     if (!ftplogactive || !ftplogbuf[0]) /* No active record */
  2034.       return;
  2035.  
  2036.     ftplogactive = 0;                   /* Record is not active */
  2037.  
  2038.     d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
  2039.     ckstrncpy(buf,ckdate(),31);         /* Get current date */
  2040.     d2 = mjd(buf);                      /* Convert them to mjds */
  2041.     p = ftplogbuf;                      /* Get start time */
  2042.     p[11] = NUL;
  2043.     p[14] = NUL;                        /* Convert to seconds */
  2044.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  2045.     p[11] = ':';
  2046.     p[14] = ':';
  2047.     p = buf;                            /* Get end time */
  2048.     p[11] = NUL;
  2049.     p[14] = NUL;
  2050.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  2051.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  2052.     if (t2 > -1L) {
  2053.         ftplogprev = t2;
  2054.         p = hhmmss(t2);
  2055.         ckstrncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  2056.         ckstrncat(ftplogbuf,p,CXLOGBUFL);
  2057.     } else
  2058.       ftplogprev = 0L;
  2059.     debug(F101,"ftp cx log dialog","",dialog);
  2060.     if (dialog) {                       /* If logging */
  2061.         int x;
  2062.         x = diaopn(diafil,1,1);         /* Open log in append mode */
  2063.         if (x > 0) {
  2064.             debug(F101,"ftp cx log open","",x);
  2065.             x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  2066.             debug(F101,"ftp cx log write","",x);
  2067.             x = zclose(ZDIFIL);         /* Close the log */
  2068.             debug(F101,"ftp cx log close","",x);
  2069.         }
  2070.     }
  2071. }
  2072.  
  2073. VOID
  2074. dologftp() {
  2075.     ftplogend();                        /* Previous session not closed out? */
  2076.     ftplogprev = 0L;
  2077.     ftplogactive = 1;                   /* Record is active */
  2078.  
  2079.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  2080.               ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  2081.               " T=FTP N=", strval(ftp_host,NULL)," H=",myhost,
  2082.               " P=", ckitoa(ftp_port)," "); /* SMS 2007/02/15 */
  2083.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  2084. }
  2085. #endif /* CKLOGDIAL */
  2086.  
  2087. static char * dummy[2] = { NULL, NULL };
  2088.  
  2089. static struct keytab modetab[] = {
  2090.     { "active",  0, 0 },
  2091.     { "passive", 1, 0 }
  2092. };
  2093.  
  2094. #ifndef NOCMDL
  2095. int                                     /* Called from ckuusy.c */
  2096. #ifdef CK_ANSIC
  2097. doftparg(char c)
  2098. #else
  2099. doftparg(c) char c;
  2100. #endif /* CK_ANSIC */
  2101. /* doftparg */ {
  2102.     int x, z;
  2103.     char *xp;
  2104.     extern char **xargv, *xarg0;
  2105.     extern int xargc, stayflg, haveftpuid;
  2106.     extern char uidbuf[];
  2107.  
  2108.     xp = *xargv+1;                      /* Pointer for bundled args */
  2109.     while (c) {
  2110.         if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
  2111.             if (*(xp+1)) {
  2112.                 fatal("?Invalid argument bundling");
  2113.             }
  2114.             xargv++, xargc--;
  2115.             if ((xargc < 1) || (**xargv == '-')) {
  2116.                 fatal("?Required argument missing");
  2117.             }
  2118.         }
  2119.         switch (c) {                    /* Big switch on arg */
  2120.           case 'h':                     /* help */
  2121.            printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  2122.             printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  2123.             printf("Options:\n");
  2124.             printf("  -h           = help (this message)\n");
  2125.             printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  2126.             printf("  -u name      = username for autologin (or -M)\n");
  2127.             printf("  -P password  = password for autologin (RISKY)\n");
  2128.             printf("  -A           = autologin anonymously\n");
  2129.             printf("  -D directory = cd after autologin\n");
  2130.             printf("  -b           = force binary mode\n");
  2131.             printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  2132.             printf("  -d           = debug (double to add timestamps)\n");
  2133.             printf("  -n           = no autologin\n");
  2134.             printf("  -v           = verbose (default)\n");
  2135.             printf("  -q           = quiet\n");
  2136.             printf("  -S           = Stay (issue command prompt when done)\n");
  2137.             printf("  -Y           = do not execute Kermit init file\n");
  2138.             printf("  -p files     = files to put after autologin (or -s)\n");
  2139.             printf("  -g files     = files to get after autologin\n");
  2140.             printf("  -R           = recursive (for use with -p)\n");
  2141.  
  2142. #ifdef FTP_SECURITY
  2143.             printf("\nSecurity options:\n");
  2144.             printf("  -k realm     = Kerberos 4 realm\n");
  2145.             printf("  -f           = Kerboros 5 credentials forwarding\n");
  2146.             printf("  -x           = autoencryption mode\n");
  2147.             printf("  -c cipher    = SRP cipher type\n");
  2148.             printf("  -H hash      = SRP encryption hash\n");
  2149.             printf("  -z option    = Security options\n");
  2150. #endif /* FTP_SECURITY */
  2151.  
  2152.             printf("\n-p or -g, if given, should be last.  Example:\n");
  2153.             printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  2154.  
  2155.             doexit(GOOD_EXIT,-1);
  2156.             break;
  2157.  
  2158.           case 'R':                     /* Recursive */
  2159.             recursive = 1;
  2160.             break;
  2161.  
  2162.           case 'd':                     /* Debug */
  2163. #ifdef DEBUG
  2164.             if (deblog) {
  2165.                 extern int debtim;
  2166.                 debtim = 1;
  2167.             } else {
  2168.                 deblog = debopn("debug.log",0);
  2169.                 debok = 1;
  2170.             }
  2171. #endif /* DEBUG */
  2172.             /* fall thru on purpose */
  2173.  
  2174.           case 't':                     /* Trace */
  2175.             ftp_deb++;
  2176.             break;
  2177.  
  2178.           case 'n':                     /* No autologin */
  2179.             ftp_log = 0;
  2180.             break;
  2181.  
  2182.           case 'i':                     /* No prompt */
  2183.           case 'v':                     /* Verbose */
  2184.             break;                      /* (ignored) */
  2185.  
  2186.           case 'q':                     /* Quiet */
  2187.             quiet = 1;
  2188.             break;
  2189.  
  2190.           case 'S':                     /* Stay */
  2191.             stayflg = 1;
  2192.             break;
  2193.  
  2194.           case 'M':
  2195.           case 'u':                     /* My User Name */
  2196.             if ((int)strlen(*xargv) > 63) {
  2197.                 fatal("username too long");
  2198.             }
  2199.             ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  2200.             haveftpuid = 1;
  2201.             break;
  2202.  
  2203.           case 'A':
  2204.             ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2205.             haveftpuid = 1;
  2206.             break;
  2207.  
  2208.           case 'T':                     /* Text */
  2209.           case 'a':                     /* "ascii" */
  2210.           case 'b':                     /* Binary */
  2211.             binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  2212.             ftp_xfermode = XMODE_M;
  2213.             filepeek = 0;
  2214.             patterns = 0;
  2215.             break;
  2216.  
  2217.           case 'g':                     /* Get */
  2218.           case 'p':                     /* Put */
  2219.           case 's': {                   /* Send (= Put) */
  2220.               int havefiles, rc;
  2221.               if (ftp_action) {
  2222.                   fatal("Only one FTP action at a time please");
  2223.               }
  2224.               if (*(xp+1)) {
  2225.                   fatal("invalid argument bundling after -s");
  2226.               }
  2227.               nfils = 0;                /* Initialize file counter */
  2228.               havefiles = 0;            /* Assume nothing to send  */
  2229.               cmlist = xargv + 1;       /* Remember this pointer */
  2230.  
  2231.               while (++xargv, --xargc > 0) { /* Traverse the list */
  2232.                   if (c == 'g') {
  2233.                       havefiles++;
  2234.                       nfils++;
  2235.                       continue;
  2236.                   }
  2237. #ifdef RECURSIVE
  2238.                   if (!strcmp(*xargv,".")) {
  2239.                       havefiles = 1;
  2240.                       nfils++;
  2241.                       recursive = 1;
  2242.                   } else
  2243. #endif /* RECURSIVE */
  2244.                     if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  2245.                         if  (rc != -2)
  2246.                           havefiles = 1;
  2247.                         nfils++;
  2248.                     } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  2249.                         havefiles = 1;
  2250.                         nfils++;
  2251.                     }
  2252.               }
  2253.               xargc++, xargv--;         /* Adjust argv/argc */
  2254.               if (!havefiles) {
  2255.                   if (c == 'g') {
  2256.                       fatal("No files to put");
  2257.                   } else {
  2258.                       fatal("No files to get");
  2259.                   }
  2260.               }
  2261.               ftp_action = c;
  2262.               break;
  2263.           }
  2264.           case 'D':                     /* Directory */
  2265.             makestr(&ftp_rdir,*xargv);
  2266.             break;
  2267.  
  2268.           case 'm':                     /* Mode (Active/Passive */
  2269.             ftp_psv = lookup(modetab,*xargv,2,NULL);
  2270.             if (ftp_psv < 0) fatal("Invalid mode");
  2271.             break;
  2272.  
  2273.           case 'P':
  2274.             makestr(&ftp_tmp,*xargv);   /* You-Know-What */
  2275.             break;
  2276.  
  2277.           case 'Y':                     /* No initialization file */
  2278.             break;                      /* (already done in prescan) */
  2279.  
  2280. #ifdef CK_URL
  2281.           case 'U': {                   /* URL */
  2282.               /* These are set by urlparse() - any not set are NULL */
  2283.               if (g_url.hos) {
  2284. /*
  2285.   Kermit has accepted host:port notation since many years before URLs were
  2286.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  2287.   host:449" looks like a URL and results in service = host and host = 449.
  2288.   Here we try to catch this situation transparently to the user.
  2289. */
  2290.                   if (ckstrcmp(g_url.svc,"ftp",-1,0)
  2291. #ifdef CK_SSL
  2292.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  2293. #endif /* CK_SSL */
  2294.                        ) {
  2295.                       if (!g_url.usr &&
  2296.                           !g_url.psw &&
  2297.                           !g_url.por &&
  2298.                           !g_url.pth) {
  2299.                           g_url.por = g_url.hos;
  2300.                           g_url.hos = g_url.svc;
  2301.                           g_url.svc = "ftp";
  2302.                       } else {
  2303.                           ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  2304.                                    g_url.svc," host=",g_url.hos);
  2305.                           fatal(tmpbuf);
  2306.                       }
  2307.                   }
  2308.                   makestr(&ftp_host,g_url.hos);
  2309.                   if (g_url.usr) {
  2310.                       haveftpuid = 1;
  2311.                       ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  2312.                       makestr(&ftp_logname,uidbuf);
  2313.                   }
  2314.                   if (g_url.psw) {
  2315.                       makestr(&ftp_tmp,g_url.psw);
  2316.                   }
  2317.                   if (g_url.pth) {
  2318.                       if (!g_url.usr) {
  2319.                           haveftpuid = 1;
  2320.                           ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2321.                           makestr(&ftp_logname,uidbuf);
  2322.                       }
  2323.                       if (ftp_action) {
  2324.                           fatal("Only one FTP action at a time please");
  2325.                       }
  2326.                       if (!stayflg)
  2327.                         quiet = 1;
  2328.                       nfils = 1;
  2329.                       dummy[0] = g_url.pth;
  2330.                       cmlist = dummy;
  2331.                       ftp_action = 'g';
  2332.                   }
  2333.                   xp = NULL;
  2334.                   haveurl = 1;
  2335.               }
  2336.               break;
  2337.           }
  2338. #endif /* CK_URL */
  2339.  
  2340. #ifdef FTP_SECURITY
  2341.           case 'k': {                   /* K4 Realm */
  2342. #ifdef FTP_KRB4
  2343.               ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  2344. #endif /* FTP_KRB4 */
  2345.               if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  2346.               break;
  2347.           }
  2348.           case 'f': {
  2349. #ifdef FTP_GSSAPI
  2350.               ftp_cfw = 1;
  2351.               if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2352. #else /* FTP_GSSAPI */
  2353.               printf("K5 Credentials Forwarding not supported\n");
  2354. #endif /* FTP_GSSAPI */
  2355.               break;
  2356.           }
  2357.           case 'x': {
  2358.               ftp_cry = 1;
  2359.               if (ftp_deb) printf("Autoencryption\n");
  2360.               break;
  2361.           }
  2362.           case 'c': {                   /* Cipher */
  2363. #ifdef FTP_SRP
  2364.               if (!srp_selcipher(*xargv)) {
  2365.                   if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2366.               } else
  2367.                 printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2368. #else /* FTP_SRP */
  2369.               printf("?SRP not supported\n");
  2370. #endif /* FTP_SRP */
  2371.               break;
  2372.           }
  2373.           case 'H': {
  2374. #ifdef FTP_SRP
  2375.               if (!srp_selhash(*xargv)) {
  2376.                   if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2377.               } else
  2378.                 printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2379. #else /* FTP_SRP */
  2380.               printf("?SRP not supported\n");
  2381. #endif /* FTP_SRP */
  2382.               break;
  2383.           }
  2384.           case 'z': {
  2385.               /* *xargv contains a value of the form tag=value */
  2386.               /* we need to lookup the tag and save the value  */
  2387.               char * p = NULL, * q = NULL;
  2388.               makestr(&p,*xargv);
  2389.               y = ckindex("=",p,0,0,1);
  2390.               if (y > 0)
  2391.                 p[y-1] = '\0';
  2392.               x = lookup(ftpztab,p,nftpztab,&z);
  2393.               if (x < 0) {
  2394.                   printf("?Invalid security option: \"%s\"\n",p);
  2395.               } else {
  2396.                   if (ftp_deb)
  2397.             printf("Security option: \"%s",p);
  2398.                   if (ftpztab[z].flgs & CM_ARG) {
  2399.                       if (y <= 0)
  2400.                         fatal("?Missing required value");
  2401.                       q = &p[y];
  2402.                       if (!*q)
  2403.                         fatal("?Missing required value");
  2404.                       if (ftp_deb)
  2405.             printf("=%s\"",q);
  2406.                   }
  2407.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2408.                     case FT_NOGSS:
  2409. #ifdef FTP_GSSAPI
  2410.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2411.                           if (ftp_auth_type[z] == FTA_GK5) {
  2412.                               for (y = z;
  2413.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2414.                                    y++
  2415.                                    )
  2416.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2417.                               ftp_auth_type[FTPATYPS-1] = 0;
  2418.                               break;
  2419.                           }
  2420.                       }
  2421. #endif /* FTP_GSSAPI */
  2422.                       break;
  2423.                     case FT_NOK4:
  2424. #ifdef FTP_KRB4
  2425.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2426.                           if (ftp_auth_type[z] == FTA_K4) {
  2427.                               for (y = z;
  2428.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2429.                                    y++
  2430.                                    )
  2431.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2432.                               ftp_auth_type[FTPATYPS-1] = 0;
  2433.                               break;
  2434.                           }
  2435.                       }
  2436. #endif /* FTP_KRB4 */
  2437.                       break;
  2438.                     case FT_NOSRP:
  2439. #ifdef FTP_SRP
  2440.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2441.                           if (ftp_auth_type[z] == FTA_SRP) {
  2442.                               for (y = z;
  2443.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2444.                                    y++
  2445.                                    )
  2446.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2447.                               ftp_auth_type[FTPATYPS-1] = 0;
  2448.                               break;
  2449.                           }
  2450.                       }
  2451. #endif /* FTP_SRP */
  2452.                       break;
  2453.                     case FT_NOSSL:
  2454. #ifdef CK_SSL
  2455.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2456.                           if (ftp_auth_type[z] == FTA_SSL) {
  2457.                               for (y = z;
  2458.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2459.                                    y++
  2460.                                    )
  2461.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2462.                               ftp_auth_type[FTPATYPS-1] = 0;
  2463.                               break;
  2464.                           }
  2465.                       }
  2466. #endif /* CK_SSL */
  2467.                       break;
  2468.                     case FT_NOTLS:
  2469. #ifdef CK_SSL
  2470.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2471.                           if (ftp_auth_type[z] == FTA_TLS) {
  2472.                               for (y = z;
  2473.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2474.                                    y++
  2475.                                    )
  2476.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2477.                               ftp_auth_type[FTPATYPS-1] = 0;
  2478.                               break;
  2479.                           }
  2480.                       }
  2481. #endif /* CK_SSL */
  2482.                       break;
  2483.                     case FT_CERTFI:
  2484. #ifdef CK_SSL
  2485.                       makestr(&ssl_rsa_cert_file,q);
  2486. #endif /* CK_SSL */
  2487.                       break;
  2488.                     case FT_OKCERT:
  2489. #ifdef CK_SSL
  2490.                       ssl_certsok_flag = 1;
  2491. #endif /* CK_SSL */
  2492.                       break;
  2493.                     case FT_DEBUG:
  2494. #ifdef DEBUG
  2495.                       if (deblog) {
  2496.                           extern int debtim;
  2497.                           debtim = 1;
  2498.                       } else {
  2499.                           deblog = debopn("debug.log",0);
  2500.                       }
  2501. #endif /* DEBUG */
  2502.                       break;
  2503.                     case FT_KEY:
  2504. #ifdef CK_SSL
  2505.                       makestr(&ssl_rsa_key_file,q);
  2506. #endif /* CK_SSL */
  2507.                       break;
  2508.                     case FT_SECURE:
  2509.                       /* no equivalent */
  2510.                       break;
  2511.                     case FT_VERIFY:
  2512. #ifdef CK_SSL
  2513.                       if (!rdigits(q))
  2514.                         printf("?Bad number: %s\n",q);
  2515.                       ssl_verify_flag = atoi(q);
  2516. #endif /* CK_SSL */
  2517.                       break;
  2518.                   }
  2519.               }
  2520.               if (ftp_deb) printf("\"\n");
  2521.               free(p);
  2522.               break;
  2523.           }
  2524. #endif /* FTP_SECURITY */
  2525.  
  2526.           default:
  2527.             fatal2(*xargv,
  2528.                    "unknown command-line option, type \"ftp -h\" for help"
  2529.                    );
  2530.         }
  2531.         if (!xp) break;
  2532.         c = *++xp;                      /* See if options are bundled */
  2533.     }
  2534.     return(0);
  2535. }
  2536. #endif /* NOCMDL */
  2537.  
  2538. int
  2539. ftpisconnected() {
  2540.     return(connected);
  2541. }
  2542.  
  2543. int
  2544. ftpisloggedin() {
  2545.     return(connected ? loggedin : 0);
  2546. }
  2547.  
  2548. int
  2549. ftpissecure() {
  2550.     return((ftp_dpl == FPL_CLR && !ssl_ftp_proxy) ? 0 : 1);
  2551. }
  2552.  
  2553. static VOID
  2554. ftscreen(n, c, z, s) int n; char c; CK_OFF_T z; char * s; {
  2555.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2556.         if (!dpyactive) {
  2557.             ckscreen(SCR_PT,'S',(CK_OFF_T)0,"");
  2558.             dpyactive = 1;
  2559.         }
  2560.         ckscreen(n,c,z,s);
  2561.     }
  2562. }
  2563.  
  2564. #ifndef OS2
  2565. /*  g m s t i m e r  --  Millisecond timer */
  2566.  
  2567. long
  2568. gmstimer() {
  2569. #ifdef HAVE_MSECS
  2570.     /* For those versions of ztime() that also set global ztmsec. */
  2571.     char *p = NULL;
  2572.     long z;
  2573.     ztime(&p);
  2574.     if (!p) return(0L);
  2575.     if (!*p) return(0L);
  2576.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2577.     return(z * 1000 + ztmsec);
  2578. #else
  2579.     return((long)time(NULL) * 1000L);
  2580. #endif /* HAVE_MSECS */
  2581. }
  2582. #endif /* OS2 */
  2583.  
  2584. /*  d o s e t f t p  --  The SET FTP command  */
  2585.  
  2586. int
  2587. dosetftp() {
  2588.     int cx;
  2589.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2590.       return(cx);
  2591.     switch (cx) {
  2592.  
  2593.       case FTS_FNC:                     /* Filename collision action */
  2594.         if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  2595.           return(x);
  2596.         if ((y = cmcfm()) < 0)
  2597.           return(y);
  2598.         ftp_fnc = x;
  2599.         return(1);
  2600.  
  2601.       case FTS_CNV:                     /* Filename conversion */
  2602.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2603.           return(x);
  2604.         if ((y = cmcfm()) < 0)
  2605.           return(y);
  2606.         ftp_cnv = x;
  2607.         return(1);
  2608.  
  2609.       case FTS_DBG:                     /* Debug messages */
  2610.         return(seton(&ftp_deb));
  2611.  
  2612.       case FTS_LOG:                     /* Auto-login */
  2613.         return(seton(&ftp_log));
  2614.  
  2615.       case FTS_PSV:                     /* Passive mode */
  2616.     return(dosetftppsv());
  2617.  
  2618.       case FTS_SPC:                     /* Send port commands */
  2619.         x = seton(&ftp_spc);
  2620.         if (x > 0) sendport = ftp_spc;
  2621.         return(x);
  2622.  
  2623.       case FTS_TYP:                     /* Type */
  2624.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2625.           return(x);
  2626.         if ((y = cmcfm()) < 0) return(y);
  2627.     ftp_typ = x;
  2628.     g_ftp_typ = x;
  2629.     tenex = (ftp_typ == FTT_TEN);
  2630.         return(1);
  2631.  
  2632.       case FTS_USN:                     /* Unique server names */
  2633.         return(seton(&ftp_usn));
  2634.  
  2635.       case FTS_VBM:                     /* Verbose mode */
  2636.         if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
  2637.           return(x);
  2638.         ftp_vbx = ftp_vbm;              /* Global sticky copy */
  2639.         return(x);
  2640.  
  2641.       case FTS_TST:                     /* "if (testing)" messages */
  2642.         return(seton(&testing));
  2643.  
  2644.       case FTS_PRM:                     /* Send permissions */
  2645.         return(setonaut(&ftp_prm));
  2646.  
  2647.       case FTS_AUT:                     /* Auto-authentication */
  2648.         return(seton(&ftp_aut));
  2649.  
  2650.       case FTS_ERR:                     /* Error action */
  2651.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2652.           return(x);
  2653.         if ((y = cmcfm()) < 0)
  2654.           return(y);
  2655.         ftp_err = x;
  2656.         return(success = 1);
  2657.  
  2658. #ifndef NOCSETS
  2659.       case FTS_XLA:                     /* Translation */
  2660.         return(seton(&ftp_xla));
  2661.  
  2662.       case FTS_CSR:                     /* Server charset */
  2663.         if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2664.           return(x);
  2665.         if ((y = cmcfm()) < 0)
  2666.           return(y);
  2667.         ftp_csr = x;
  2668.         ftp_xla = 1;                    /* Also enable translation */
  2669.         return(success = 1);
  2670. #endif /* NOCSETS */
  2671.  
  2672.       case FTS_GFT:
  2673.         return(seton(&get_auto));       /* GET-filetype-switching */
  2674.  
  2675.       case FTS_DAT:
  2676.         return(seton(&ftp_dates));      /* Set file dates */
  2677.  
  2678. #ifdef FTP_TIMEOUT
  2679.       case FTS_TMO:            /* Timeout */
  2680.         if ((x = cmnum("Number of seconds","0",10,&z,xxstring)) < 0)
  2681.           return(x);
  2682.         if ((y = cmcfm()) < 0)
  2683.           return(y);
  2684.     ftp_timeout = z;
  2685.     return(success = 1);
  2686. #endif    /* FTP_TIMEOUT */
  2687.  
  2688.       case FTS_STO: {            /* Server time offset */
  2689.       char * s, * p = NULL;
  2690.       long k;
  2691.       if ((x = cmfld("[+-]hh[:mm[:ss]]","+0",&s,xxstring)) < 0)
  2692.         return(x);
  2693.       if (!strcmp(s,"+0")) {
  2694.           s = NULL;
  2695.       } else if ((x = delta2sec(s,&k)) < 0) { /* Check format */
  2696.           printf("?Invalid time offset\n");
  2697.           return(-9);
  2698.       }
  2699.       makestr(&p,s);        /* Make a safe copy the string */
  2700.       if ((x = cmcfm()) < 0) {    /* Get confirmation */
  2701.           if (p)
  2702.         makestr(&p,NULL);
  2703.           return(x);
  2704.       }
  2705.       fts_sto = p;            /* Confirmed - set the string. */
  2706.       return(success = 1);
  2707.       }
  2708.       case FTS_APW: {
  2709.       char * s;
  2710.       if ((x = cmtxt("Text", "", &s, xxstring)) < 0)
  2711.         return(x);
  2712.       makestr(&ftp_apw, *s ? s : NULL);
  2713.       return(success = 1);
  2714.       }
  2715.  
  2716.       case FTS_BUG: {
  2717.           if ((x = cmkey(ftpbugtab,nftpbug,"","",xxstring)) < 0) 
  2718.         return(x);
  2719.           switch (x) {
  2720. #ifdef CK_SSL
  2721.           case FTB_SV2:
  2722.         return seton(&ftp_bug_use_ssl_v2);
  2723. #endif /* CK_SSL */
  2724.           default:
  2725.         return(-2);
  2726.           }
  2727.       }
  2728.  
  2729. #ifdef FTP_SECURITY
  2730.       case FTS_CRY:                     /* Auto-encryption */
  2731.         return(seton(&ftp_cry));
  2732.  
  2733.       case FTS_CFW:                     /* Credential-forwarding */
  2734.         return(seton(&ftp_cfw));
  2735.  
  2736.       case FTS_CPL:                     /* Command protection level */
  2737.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2738.         if ((y = cmcfm()) < 0) return(y);
  2739.         success = fts_cpl(x);
  2740.         return(success);
  2741.  
  2742.       case FTS_DPL:                     /* Data protection level */
  2743.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2744.         if ((y = cmcfm()) < 0) return(y);
  2745.           success = fts_dpl(x);
  2746.           return(success);
  2747.  
  2748.       case FTS_ATP: {                   /* FTP Auth Type */
  2749.           int i, j, atypes[8];
  2750.  
  2751.           for (i = 0; i < 8; i++) {
  2752.               if ((y = cmkey(ftpauth,nftpauth,"",
  2753.                              (i == 0) ? "automatic" : "",
  2754.                              xxstring)) < 0) {
  2755.                   if (y == -3)
  2756.                     break;
  2757.                   return(y);
  2758.               }
  2759.               if (i > 0 && (y == FTA_AUTO)) {
  2760.                   printf("?Choice may only be used in first position.\r\n");
  2761.                   return(-9);
  2762.               }
  2763.               for (j = 0; j < i; j++) {
  2764.                   if (atypes[j] == y) {
  2765.                       printf("\r\n?Choice has already been used.\r\n");
  2766.                       return(-9);
  2767.                   }
  2768.               }
  2769.               atypes[i] = y;
  2770.               if (y == FTA_AUTO) {
  2771.                   i++;
  2772.                   break;
  2773.               }
  2774.           }
  2775.           if (i < 8)
  2776.             atypes[i] = 0;
  2777.           if ((z = cmcfm()) < 0)
  2778.             return(z);
  2779.           if (atypes[0] == FTA_AUTO) {
  2780.               i = 0;
  2781. #ifdef FTP_GSSAPI
  2782.               ftp_auth_type[i++] = FTA_GK5;
  2783. #endif /* FTP_GSSAPI */
  2784. #ifdef FTP_SRP
  2785.               ftp_auth_type[i++] = FTA_SRP;
  2786. #endif /* FTP_SRP */
  2787. #ifdef FTP_KRB4
  2788.               ftp_auth_type[i++] = FTA_K4;
  2789. #endif /* FTP_KRB4 */
  2790. #ifdef CK_SSL
  2791.               ftp_auth_type[i++] = FTA_TLS;
  2792.               ftp_auth_type[i++] = FTA_SSL;
  2793. #endif /* CK_SSL */
  2794.               ftp_auth_type[i] = 0;
  2795.           } else {
  2796.               for (i = 0; i < 8; i++)
  2797.                 ftp_auth_type[i] = atypes[i];
  2798.           }
  2799.           return(success = 1);
  2800.       }
  2801.  
  2802.       case FTS_SRP:
  2803. #ifdef FTP_SRP
  2804.         if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2805.           return(x);
  2806.         switch (x) {
  2807.           case SRP_CIPHER:
  2808.             if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2809.               return(x);
  2810.             if ((z = cmcfm()) < 0)
  2811.               return(z);
  2812.             success = !srp_selcipher(ciphertab[x].kwd);
  2813.             return(success);
  2814.           case SRP_HASH:
  2815.             if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2816.               return(x);
  2817.             if ((z = cmcfm()) < 0)
  2818.               return(z);
  2819.             success = !srp_selhash(hashtab[x].kwd);
  2820.             return(success = 1);
  2821.           default:
  2822.             if ((z = cmcfm()) < 0)
  2823.               return(z);
  2824.             return(-2);
  2825.         }
  2826. #else /* FTP_SRP */
  2827.         if ((z = cmcfm()) < 0)
  2828.           return(z);
  2829.         return(-2);
  2830. #endif /* FTP_SRP */
  2831. #endif /* FTP_SECURITY */
  2832.  
  2833.       case FTS_DIS:
  2834.     doxdis(2);            /* 2 == ftp */
  2835.         return(success = 1);
  2836.  
  2837.       default:
  2838.         return(-2);
  2839.     }
  2840. }
  2841.  
  2842. int
  2843. ftpbye() {
  2844.     int x;
  2845.     if (!connected)
  2846.       return(1);
  2847.     if (testing)
  2848.       printf(" ftp closing %s...\n",ftp_host);
  2849.     x = ftpclose();
  2850.     return((x > -1) ? 1 : 0);
  2851. }
  2852.  
  2853. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2854.  
  2855. static int
  2856. openftp(s,opn_tls) char * s; int opn_tls; {
  2857.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2858.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1, nologin = 0;
  2859.     int haveuser = 0;
  2860.     struct FDB sw, fl, cm;
  2861.     extern int nnetdir;                 /* Network services directory */
  2862.     extern int nhcount;                 /* Lookup result */
  2863.     extern char *nh_p[];                /* Network directory entry pointers */
  2864.     extern char *nh_p2[];               /* Network directory entry nettype */
  2865.  
  2866.     if (!s) return(-2);
  2867.     if (!*s) return(-2);
  2868.  
  2869.     makestr(&hostname,s);
  2870.     hostsave = hostname;
  2871.     makestr(&ftp_logname,NULL);
  2872.     anonymous = 0;
  2873.     noinit = 0;
  2874.  
  2875.     debug(F110,"ftp open",hostname,0);
  2876.  
  2877.     if (sav_psv > -1) {                 /* Restore prevailing active/passive */
  2878.         ftp_psv = sav_psv;              /* selection in case it was */
  2879.         sav_psv = -1;                   /* temporarily overriden by a switch */
  2880.     }
  2881.     if (sav_log > -1) {                 /* Ditto for autologin */
  2882.         ftp_log = sav_log;
  2883.         sav_log = -1;
  2884.     }
  2885.     cmfdbi(&sw,                         /* Switches */
  2886.            _CMKEY,
  2887.            "Service name or port;\n or switch",
  2888.            "",                          /* default */
  2889.            "",                          /* addtl string data */
  2890.            nftpswi,                     /* addtl numeric data 1: tbl size */
  2891.            4,                           /* addtl numeric data 2: none */
  2892.            xxstring,                    /* Processing function */
  2893.            ftpswitab,                   /* Keyword table */
  2894.            &fl                          /* Pointer to next FDB */
  2895.            );
  2896.     cmfdbi(&fl,                         /* A host name or address */
  2897.            _CMFLD,                      /* fcode */
  2898.            "",                          /* help */
  2899.            "xYzBoo",                    /* default */
  2900.            "",                          /* addtl string data */
  2901.            0,                           /* addtl numeric data 1 */
  2902.            0,                           /* addtl numeric data 2 */
  2903.            xxstring,
  2904.            NULL,
  2905.            &cm
  2906.            );
  2907.     cmfdbi(&cm,                         /* Command confirmation */
  2908.            _CMCFM,
  2909.            "",
  2910.            "",
  2911.            "",
  2912.            0,
  2913.            0,
  2914.            NULL,
  2915.            NULL,
  2916.            NULL
  2917.            );
  2918.  
  2919.     for (n = 0;; n++) {
  2920.         rc = cmfdb(&sw);                /* Parse a service name or a switch */
  2921.         if (rc < 0)
  2922.           goto xopenftp;
  2923.  
  2924.         if (cmresult.fcode == _CMCFM) { /* Done? */
  2925.             break;
  2926.         } else if (cmresult.fcode == _CMFLD) {  /* Port */
  2927.             if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2928.               makestr(&service,cmresult.sresult);
  2929.             else
  2930.               makestr(&service,opn_tls?"ftps":"ftp");
  2931.         } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2932.             c = cmgbrk();               /* get break character */
  2933.             getval = (c == ':' || c == '=');
  2934.             rc = -9;
  2935.             if (getval && !(cmresult.kflags & CM_ARG)) {
  2936.                 printf("?This switch does not take arguments\n");
  2937.                 goto xopenftp;
  2938.             }
  2939.             if (!getval && (cmresult.kflags & CM_ARG)) {
  2940.                 printf("?This switch requires an argument\n");
  2941.                 goto xopenftp;
  2942.             }
  2943.             switch (cmresult.nresult) { /* Switch */
  2944.               case OPN_ANO:             /* /ANONYMOUS */
  2945.                 anonymous++;
  2946.         nologin = 0;
  2947.                 break;
  2948.               case OPN_NIN:             /* /NOINIT */
  2949.                 noinit++;
  2950.                 break;
  2951.               case OPN_NOL:             /* /NOLOGIN */
  2952.                 nologin++;
  2953.         anonymous = 0;
  2954.         makestr(&ftp_logname,NULL);
  2955.                 break;
  2956.               case OPN_PSW:             /* /PASSWORD */
  2957.                 if (!anonymous)         /* Don't log real passwords */
  2958.                   debok = 0;
  2959.                 rc = cmfld("Password for FTP server","",&p,xxstring);
  2960.                 if (rc == -3) {
  2961.                     makestr(&ftp_tmp,NULL);
  2962.                 } else if (rc < 0) {
  2963.                     goto xopenftp;
  2964.                 } else {
  2965.                     makestr(&ftp_tmp,brstrip(p));
  2966.             nologin = 0;
  2967.                 }
  2968.                 break;
  2969.               case OPN_USR:             /* /USER */
  2970.                 rc = cmfld("Username for FTP server","",&p,xxstring);
  2971.                 if (rc == -3) {
  2972.                     makestr(&ftp_logname,NULL);
  2973.                 } else if (rc < 0) {
  2974.                     goto xopenftp;
  2975.                 } else {
  2976.             nologin = 0;
  2977.                     anonymous = 0;
  2978.             haveuser = 1;
  2979.                     makestr(&ftp_logname,brstrip(p));
  2980.                 }
  2981.                 break;
  2982.               case OPN_ACC:
  2983.                 rc = cmfld("Account for FTP server","",&p,xxstring);
  2984.                 if (rc == -3) {
  2985.                     makestr(&ftp_acc,NULL);
  2986.                 } else if (rc < 0) {
  2987.                     goto xopenftp;
  2988.                 } else {
  2989.                     makestr(&ftp_acc,brstrip(p));
  2990.                 }
  2991.                 break;
  2992.               case OPN_ACT:
  2993.                 opn_psv = 0;
  2994.                 break;
  2995.               case OPN_PSV:
  2996.                 opn_psv = 1;
  2997.                 break;
  2998.               case OPN_TLS:
  2999.                 opn_tls = 1;
  3000.                 break;
  3001.               default:
  3002.                 break;
  3003.             }
  3004.         }
  3005.         if (n == 0) {                   /* After first time through */
  3006.             cmfdbi(&sw,                 /* accept only switches */
  3007.                    _CMKEY,
  3008.                    "\nCarriage return to confirm to command, or switch",
  3009.                    "",
  3010.                    "",
  3011.                    nftpswi,
  3012.                    4,
  3013.                    xxstring,
  3014.                    ftpswitab,
  3015.                    &cm
  3016.                    );
  3017.         }
  3018.     }
  3019. #ifdef COMMENT
  3020.     debug(F100,"ftp openftp while exit","",0);
  3021.     rc = cmcfm();
  3022.     debug(F101,"ftp openftp cmcfm rc","",rc);
  3023.     if (rc < 0)
  3024.       goto xopenftp;
  3025. #endif /* COMMENT */
  3026.  
  3027.     if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
  3028.         sav_psv = ftp_psv;
  3029.         ftp_psv = opn_psv;
  3030.     }
  3031.     if (nologin || haveuser) {        /* /NOLOGIN or /USER switch given */
  3032.     sav_log = ftp_log;
  3033.     ftp_log = haveuser ? 1 : 0;
  3034.     }
  3035.     if (*hostname == '=') {             /* Bypass directory lookup */
  3036.         hostname++;                     /* if hostname starts with '=' */
  3037.         havehost++;
  3038.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  3039.         havehost++;
  3040.     }
  3041.     if (!service)
  3042.       makestr(&service,opn_tls?"ftps":"ftp");
  3043.  
  3044. #ifndef NODIAL
  3045.     if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
  3046.         lunet(hostname);                /* Look up the name */
  3047.         debug(F111,"ftp openftp lunet",hostname,nhcount);
  3048.         if (nhcount == 0) {
  3049.             if (testing)
  3050.               printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3051.             success = ftpopen(hostname,service,opn_tls);
  3052.             debug(F101,"ftp openftp A ftpopen success","",success);
  3053.             rc = success;
  3054.         } else {
  3055.             int found = 0;
  3056.             for (i = 0; i < nhcount; i++) {
  3057.                 if (nh_p2[i])           /* If network type specified */
  3058.                   if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  3059.                     continue;
  3060.                 found++;
  3061.                 makestr(&hostname,nh_p[i]);
  3062.                 debug(F111,"ftpopen lunet substitution",hostname,i);
  3063.                 if (testing)
  3064.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3065.                 success = ftpopen(hostname,service,opn_tls);
  3066.                 debug(F101,"ftp openftp B ftpopen success","",success);
  3067.                 rc = success;
  3068.                 if (success)
  3069.                   break;
  3070.             }
  3071.             if (!found) {               /* E.g. if no network types match */
  3072.                 if (testing)
  3073.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3074.                 success = ftpopen(hostname,service,opn_tls);
  3075.                 debug(F101,"ftp openftp C ftpopen success","",success);
  3076.                 rc = success;
  3077.             }
  3078.         }
  3079.     } else {
  3080. #endif /* NODIAL */
  3081.         if (testing)
  3082.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3083.         success = ftpopen(hostname,service,opn_tls);
  3084.         debug(F111,"ftp openftp D ftpopen success",hostname,success);
  3085.         debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  3086.         rc = success;
  3087. #ifndef NODIAL
  3088.     }
  3089. #endif /* NODIAL */
  3090.  
  3091.   xopenftp:
  3092.     debug(F101,"ftp openftp xopenftp rc","",rc);
  3093.     if (hostsave) free(hostsave);
  3094.     if (service) free(service);
  3095.     if (rc < 0 && ftp_logname) {
  3096.         free(ftp_logname);
  3097.         ftp_logname = NULL;
  3098.     }
  3099.     if (ftp_tmp) {
  3100.         free(ftp_tmp);
  3101.         ftp_tmp = NULL;
  3102.     }
  3103.     return(rc);
  3104. }
  3105.  
  3106. VOID                    /* 12 Aug 2007 */
  3107. doftpglobaltype(x) int x; {
  3108.     ftp_xfermode = XMODE_M;        /* Set manual FTP transfer mode */
  3109.     ftp_typ = x;            /* Used by top-level BINARY and */
  3110.     g_ftp_typ = x;            /* ASCII commands. */
  3111.     get_auto = 0;
  3112.     forcetype = 1;
  3113. }
  3114.  
  3115. int
  3116. doftpacct() {
  3117.     int x;
  3118.     char * s;
  3119.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  3120.       return(x);
  3121.     CHECKCONN();
  3122.     makestr(&ftp_acc,brstrip(s));
  3123.     if (testing)
  3124.       printf(" ftp account: \"%s\"\n",ftp_acc);
  3125.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  3126.     return(success);
  3127. }
  3128.  
  3129. int
  3130. doftpusr() {                            /* Log in as USER */
  3131.     extern char uidbuf[];
  3132.     extern char pwbuf[];
  3133.     extern int  pwflg, pwcrypt;
  3134.     int x;
  3135.     char *s, * acct = "";
  3136.  
  3137.     debok = 0;                          /* Don't log */
  3138.  
  3139.     if ((x = cmfld("Remote username or ID",uidbuf,&s,xxstring)) < 0)
  3140.       return(x);
  3141.     ckstrncpy(line,brstrip(s),LINBUFSIZ); /* brstrip: 15 Jan 2003 */
  3142.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0) {
  3143.         if (x == -3) { /* no input */
  3144.             if ( pwbuf[0] && pwflg ) {
  3145.                 ckstrncpy(tmpbuf,(char *)pwbuf,TMPBUFSIZ);
  3146. #ifdef OS2
  3147.                 if ( pwcrypt )
  3148.                     ck_encrypt((char *)tmpbuf);
  3149. #endif /* OS2 */
  3150.             }
  3151.         } else {
  3152.             return(x);
  3153.         }
  3154.     } else {
  3155.         ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  3156.     }
  3157.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  3158.                    "", &s, xxstring)) < 0)
  3159.       return(x);
  3160.     CHECKCONN();
  3161.     if (*s) {
  3162.         x = strlen(tmpbuf);
  3163.         if (x > 0) {
  3164.             acct = &tmpbuf[x+2];
  3165.             ckstrncpy(acct,brstrip(s),TMPBUFSIZ - x - 2);
  3166.         }
  3167.     }
  3168.     if (testing)
  3169.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  3170.     success = ftp_user(line,tmpbuf,acct);
  3171. #ifdef CKLOGDIAL
  3172.     dologftp();
  3173. #endif /* CKLOGDIAL */
  3174.     return(success);
  3175. }
  3176.  
  3177. /* DO (various FTP commands)... */
  3178.  
  3179. int
  3180. doftptyp(type) int type; {              /* TYPE */
  3181.     CHECKCONN();
  3182.     ftp_typ = type;
  3183.     changetype(ftp_typ,ftp_vbm);
  3184.     debug(F101,"doftptyp changed type","",type);
  3185.     return(1);
  3186. }
  3187.  
  3188. static int
  3189. doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
  3190.     int lcs = -1, rcs = -1;
  3191. #ifndef NOCSETS
  3192.     if (ftp_xla) {
  3193.         lcs = ftp_csl;
  3194.         if (lcs < 0) lcs = fcharset;
  3195.         rcs = ftp_csx;
  3196.         if (rcs < 0) rcs = ftp_csr;
  3197.     }
  3198. #endif /* NOCSETS */
  3199.     debug(F110,"ftp doftpmkd",s,0);
  3200.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3201.       return(success = 1);
  3202.     if (ftpcode == 500 || ftpcode == 502) {
  3203.         if (!quiet)
  3204.           printf("MKD command not recognized, trying XMKD\n");
  3205.         if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3206.           return(success = 1);
  3207.     }
  3208.     return(success = 0);
  3209. }
  3210.  
  3211. static int
  3212. doftpmkd() {                            /* MKDIR parse */
  3213.     int x;
  3214.     char * s;
  3215.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  3216.       return(x);
  3217.     CHECKCONN();
  3218.     ckstrncpy(line,s,LINBUFSIZ);
  3219.     if (testing)
  3220.       printf(" ftp mkdir \"%s\"...\n",line);
  3221.     return(success = doftpxmkd(line,-1));
  3222. }
  3223.  
  3224. static int
  3225. doftprmd() {                            /* RMDIR */
  3226.     int x, lcs = -1, rcs = -1;
  3227.     char * s;
  3228.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  3229.       return(x);
  3230.     CHECKCONN();
  3231.     ckstrncpy(line,s,LINBUFSIZ);
  3232.     if (testing)
  3233.       printf(" ftp rmdir \"%s\"...\n",line);
  3234. #ifndef NOCSETS
  3235.     if (ftp_xla) {
  3236.         lcs = ftp_csl;
  3237.         if (lcs < 0) lcs = fcharset;
  3238.         rcs = ftp_csx;
  3239.         if (rcs < 0) rcs = ftp_csr;
  3240.     }
  3241. #endif /* NOCSETS */
  3242.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  3243.       return(success = 1);
  3244.     if (ftpcode == 500 || ftpcode == 502) {
  3245.         if (!quiet)
  3246.           printf("RMD command not recognized, trying XMKD\n");
  3247.         success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  3248.     } else
  3249.       success = 0;
  3250.     return(success);
  3251. }
  3252.  
  3253. static int
  3254. doftpren() {                            /* RENAME */
  3255.     int x;
  3256.     char * s;
  3257.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  3258.       return(x);
  3259.     ckstrncpy(line,s,LINBUFSIZ);
  3260.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  3261.       return(x);
  3262.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  3263.     if ((x = cmcfm()) < 0)
  3264.       return(x);
  3265.     CHECKCONN();
  3266.     if (testing)
  3267.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  3268.     success = ftp_rename(line,tmpbuf);
  3269.     return(success);
  3270. }
  3271.  
  3272. int
  3273. doftpres() {                            /* RESET (log out without close) */
  3274.     int x;
  3275.     if ((x = cmcfm()) < 0)
  3276.       return(x);
  3277.     CHECKCONN();
  3278.     if (testing)
  3279.       printf(" ftp reset...\n");
  3280.     return(success = ftp_reset());
  3281. }
  3282.  
  3283. static int
  3284. doftpxhlp() {                           /* HELP */
  3285.     int x;
  3286.     char * s;
  3287.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  3288.       return(x);
  3289.     CHECKCONN();
  3290.     ckstrncpy(line,s,LINBUFSIZ);
  3291.     if (testing)
  3292.       printf(" ftp help \"%s\"...\n",line);
  3293.     /* No need to translate -- all FTP commands are ASCII */
  3294.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  3295. }
  3296.  
  3297. static int
  3298. doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
  3299.     int x, lcs = 0, rcs = 0, xlate = 0;
  3300.     char * p, * s, * m = "";
  3301.     if (cx == FTP_VDI) {
  3302.         switch (servertype) {
  3303.           case SYS_VMS:
  3304.           case SYS_DOS:
  3305.           case SYS_TOPS10:
  3306.           case SYS_TOPS20:
  3307.             m = "*.*";
  3308.             break;
  3309.           default:
  3310.             m = "*";
  3311.         }
  3312.     }
  3313.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  3314.       return(x);
  3315.     if ((x = remtxt(&s)) < 0)
  3316.       return(x);
  3317. #ifdef NOCSETS
  3318.     xlate = 0;
  3319. #else
  3320.     xlate = ftp_xla;
  3321. #endif /* NOCSETS */
  3322.     line[0] = NUL;
  3323.     ckstrncpy(line,s,LINBUFSIZ);
  3324.     s = line;
  3325.     CHECKCONN();
  3326.  
  3327. #ifndef NOCSETS
  3328.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  3329.         lcs = ftp_csl;                  /* Local charset */
  3330.         if (lcs < 0) lcs = fcharset;
  3331.         if (lcs < 0) xlate = 0;
  3332.     }
  3333.     if (xlate) {                        /* Still ON? */
  3334.         rcs = ftp_csx;                  /* Remote (Server) charset */
  3335.         if (rcs < 0) rcs = ftp_csr;
  3336.         if (rcs < 0) xlate = 0;
  3337.     }
  3338. #endif /* NOCSETS */
  3339.  
  3340.     if (testing) {
  3341.         p = s;
  3342.         if (!p) p = "";
  3343.         if (*p)
  3344.           printf("Directory of files %s at %s:\n", line, ftp_host);
  3345.         else
  3346.           printf("Directory of files at %s:\n", ftp_host);
  3347.     }
  3348.     debug(F111,"doftpdir",s,cx);
  3349.  
  3350.     if (cx == FTP_DIR) {
  3351.         /* Translation of line[] is done inside recvrequest() */
  3352.         /* when it calls ftpcmd(). */
  3353.         return(success =
  3354.           (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  3355.     }
  3356.     success = 1;                        /* VDIR - one file at a time... */
  3357.     p = (char *)remote_files(1,(CHAR *)s,NULL,0); /* Get the file list */
  3358.     cancelgroup = 0;
  3359.     if (!ftp_vbm && !quiet)
  3360.       printlines = 1;
  3361.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  3362.         if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  3363.             success = 0;
  3364.             break;
  3365.         }
  3366.         p = (char *)remote_files(0,NULL,NULL,0); /* Get next file */
  3367.         debug(F110,"ftp vdir file",s,0);
  3368.     }
  3369.     return(success);
  3370. }
  3371.  
  3372. static int
  3373. doftppwd() {                            /* PWD */
  3374.     int x, lcs = -1, rcs = -1;
  3375. #ifndef NOCSETS
  3376.     if (ftp_xla) {
  3377.         lcs = ftp_csl;
  3378.         if (lcs < 0) lcs = fcharset;
  3379.         rcs = ftp_csx;
  3380.         if (rcs < 0) rcs = ftp_csr;
  3381.     }
  3382. #endif /* NOCSETS */
  3383.     if ((x = cmcfm()) < 0)
  3384.       return(x);
  3385.     CHECKCONN();
  3386.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  3387.         success = 1;
  3388.     } else if (ftpcode == 500 || ftpcode == 502) {
  3389.         if (ftp_deb)
  3390.           printf("PWD command not recognized, trying XPWD\n");
  3391.         success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  3392.     }
  3393.     return(success);
  3394. }
  3395.  
  3396. static int
  3397. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  3398.     int lcs = -1, rcs = -1;
  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.     debug(F110,"ftp doftpcwd",s,0);
  3409.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3410.       return(success = 1);
  3411.     if (ftpcode == 500 || ftpcode == 502) {
  3412.         if (!quiet)
  3413.           printf("CWD command not recognized, trying XCWD\n");
  3414.         if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3415.           return(success = 1);
  3416.     }
  3417.     return(success = 0);
  3418. }
  3419.  
  3420. static int
  3421. doftpcdup() {                           /* CDUP */
  3422.     debug(F100,"ftp doftpcdup","",0);
  3423.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  3424.       return(success = 1);
  3425.     if (ftpcode == 500 || ftpcode == 502) {
  3426.         if (!quiet)
  3427.           printf("CDUP command not recognized, trying XCUP\n");
  3428.         if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  3429.           return(success = 1);
  3430.     }
  3431.     return(success = 0);
  3432. }
  3433.  
  3434. /* s y n c d i r  --  Synchronizes client & server directories */
  3435.  
  3436. /*
  3437.   Call with:
  3438.     local = pointer to pathname of local file to be sent.
  3439.     sim   = 1 for simulation, 0 for real uploading.
  3440.   Returns 0 on failure, 1 on success.
  3441.  
  3442.   The 'local' argument is relative to the initial directory of the MPUT,
  3443.   i.e. the root of the tree being uploaded.  If the directory of the
  3444.   argument file is different from the directory of the previous file
  3445.   (which is stored in global putpath[]), this routine does the appropriate
  3446.   CWDs, CDUPs, and/or MKDIRs to position the FTP server in the same place.
  3447. */
  3448. static int cdlevel = 0, cdsimlvl = 0;    /* Tree-level trackers */
  3449.  
  3450. static int
  3451. syncdir(local,sim) char * local; int sim; {
  3452.     char buf[CKMAXPATH+1];
  3453.     char tmp[CKMAXPATH+1];
  3454.     char msgbuf[CKMAXPATH+64];
  3455.     char c, * p = local, * s = buf, * q = buf, * psep, * ssep;
  3456.     int i, k = 0, done = 0, itsadir = 0, saveq;
  3457.  
  3458.     debug(F110,"ftp syncdir local (new)",local,0);
  3459.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  3460.  
  3461.     itsadir = isdir(local);        /* Is the local file a directory? */
  3462.     saveq = quiet;
  3463.  
  3464.     while ((*s = *p)) {                 /* Copy the argument filename */
  3465.         if (++k == CKMAXPATH)           /* so we can poke it. */
  3466.           return(-1);
  3467.         if (*s == '/')                  /* Pointer to rightmost dirsep */
  3468.           q = s;
  3469.         s++;
  3470.         p++;
  3471.     }
  3472.     if (!itsadir)            /* If it's a regular file */
  3473.       *q = NUL;                         /* keep just the path part */
  3474.  
  3475.     debug(F110,"ftp syncdir buf",buf,0);
  3476.     if (!strcmp(buf,putpath)) {         /* Same path as previous file? */
  3477.         if (itsadir) {                  /* This file is a directory? */
  3478.             if (doftpcwd(local,0)) {    /* Try to CD to it */
  3479.                 doftpcdup();            /* Worked - CD back up */
  3480.             } else if (sim) {           /* Simulating... */
  3481.                 if (fdispla == XYFD_B) {
  3482.                     printf("WOULD CREATE DIRECTORY %s\n",local);
  3483.                 } else if (fdispla) {
  3484.                     ckmakmsg(msgbuf,CKMAXPATH,
  3485.                              "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3486.                     ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3487.                 }
  3488.                 /* See note above */
  3489.                 return(0);
  3490.             } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3491.                 return(0);
  3492.             } else {            /* Remote directory created OK */
  3493.                 if (fdispla == XYFD_B) {
  3494.                     printf("CREATED DIRECTORY %s\n",local);
  3495.                 } else if (fdispla) {
  3496.                     ckmakmsg(msgbuf,CKMAXPATH+64,
  3497.                              "CREATED DIRECTORY ",local,NULL,NULL);
  3498.                     ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3499.                 }
  3500.             }
  3501.         }
  3502.         debug(F110,"ftp syncdir no change",buf,0);
  3503.         return(1);                      /* Yes, done. */
  3504.     }
  3505.     ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
  3506.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3507.  
  3508.     p = buf;                            /* New */
  3509.     s = putpath;                        /* Old */
  3510.  
  3511.     debug(F110,"ftp syncdir A (old) s",s,0); /* Previous */
  3512.     debug(F110,"ftp syncdir A (new) p",p,0); /* New */
  3513.  
  3514.     psep = buf;
  3515.     ssep = putpath;
  3516.     while (*p != NUL && *s != NUL && *p == *s) {
  3517.     if (*p == '/') { psep = p+1; ssep = s+1; }
  3518.     p++,s++;
  3519.     }
  3520.     /*
  3521.       psep and ssep point to the first path segment that differs.
  3522.       We have to do as many CDUPs as there are path segments in ssep.
  3523.       then we have to do as many MKDs and CWDs as there are segments in psep.
  3524.     */
  3525.     s = ssep;
  3526.     p = psep;
  3527.  
  3528.     debug(F110,"ftp syncdir B (old) s",s,0); /* Previous */
  3529.     debug(F110,"ftp syncdir B (new) p",p,0); /* New */
  3530.  
  3531.     /* p and s now point to the leftmost spot where the paths differ */
  3532.  
  3533.     if (*s) {                           /* We have to back up */
  3534.         k = 1;                          /* How many levels counting this one */
  3535.         while ((c = *s++)) {            /* Count dirseps remaining in prev */
  3536.             if (c == '/' && *s)
  3537.               k++;
  3538.         }
  3539.     debug(F101,"ftp syncdir levels up","",k);
  3540.  
  3541.         for (i = 1; i <= k; i++) {       /* Do that many CDUPs */
  3542.             debug(F111,"ftp syncdir CDUP A",p,i);
  3543.         if (fdispla == XYFD_B)
  3544.           printf(" CDUP\n");
  3545.             if (sim && cdsimlvl) {
  3546.                 cdsimlvl--;
  3547.             } else {
  3548.                 if (!doftpcdup()) {
  3549.                     quiet = saveq;
  3550.                     return(0);
  3551.                 }
  3552.             }
  3553.             cdlevel--;
  3554.         }
  3555.         if (!*p)                        /* If we don't have to go down */
  3556.           goto xcwd;                    /* we're done. */
  3557.     }
  3558. #ifdef COMMENT
  3559.     while (p > buf && *p && *p != '/')  /* If in middle of segment */
  3560.       p--;                              /* back up to beginning */
  3561.     if (*p == '/')                      /* and terminate there */
  3562.       p++;
  3563. #endif    /* COMMENT */
  3564.  
  3565.     debug(F110,"ftp syncdir NEW PATH",p,0);
  3566.  
  3567.     s = p;                              /* Point to start of new down path. */
  3568.     while (1) {                         /* Loop through characters. */
  3569.         if (*s == '/' || !*s) {         /* Have a segment. */
  3570.             if (!*s)                    /* If end of string, */
  3571.               done++;                   /* after this segment we're done. */
  3572.             else
  3573.               *s = NUL;                 /* NUL out the separator. */
  3574.             if (*p) {                   /* If segment is not empty */
  3575.                 debug(F110,"ftp syncdir down segment",p,0);
  3576.                 if (!doftpcwd(p,0)) {   /* Try to CD to it */
  3577.                     if (sim) {
  3578.                         if (fdispla == XYFD_B) {
  3579.                             printf(" WOULD CREATE DIRECTORY %s\n",local);
  3580.                         } else if (fdispla) {
  3581.                             ckmakmsg(msgbuf,CKMAXPATH,
  3582.                      "WOULD CREATE DIRECTORY",
  3583.                                      local,NULL,NULL);
  3584.                             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3585.                         }
  3586.                         cdsimlvl++;
  3587.                     } else {
  3588.                         if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3589.                 debug(F110,"ftp syncdir mkdir failed",p,0); 
  3590. /*
  3591.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3592.   FOO but the remote has a regular file with the same name.  We can't CD
  3593.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3594.   the user handle the problem.
  3595. */
  3596.                             quiet = saveq;
  3597.                             return(0);
  3598.                         }
  3599.             debug(F110,"ftp syncdir mkdir OK",p,0); 
  3600.                         if (fdispla == XYFD_B) {
  3601.                             printf(" CREATED DIRECTORY %s\n",p);
  3602.                         } else if (fdispla) {
  3603.                             ckmakmsg(msgbuf,CKMAXPATH,
  3604.                                      "CREATED DIRECTORY ",p,NULL,NULL);
  3605.                             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3606.                         }
  3607.                         if (!doftpcwd(p,0)) { /* Try again to CD */
  3608.                 debug(F110,"ftp syncdir CD failed",p,0); 
  3609.                             quiet = saveq;
  3610.                             return(0);
  3611.                         }
  3612.                         if (fdispla == XYFD_B) printf(" CWD %s\n",p);
  3613.             debug(F110,"ftp syncdir CD OK",p,0); 
  3614.                     }
  3615.                 }
  3616.                 cdlevel++;
  3617.             }
  3618.             if (done)                   /* Quit if no next segment */
  3619.               break;
  3620.             p = s+1;                    /* Point to next segment */
  3621.         }
  3622.         s++;                            /* Point to next source char */
  3623.     }
  3624.  
  3625.   xcwd:
  3626.     ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
  3627.     quiet = saveq;
  3628.     return(1);
  3629. }
  3630.  
  3631. #ifdef DOUPDATE
  3632. #ifdef DEBUG
  3633. static VOID
  3634. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3635.     if (deblog) {
  3636.         debug(F111,"ftp year ",s,xx->tm_year);
  3637.         debug(F111,"ftp month",s,xx->tm_mon);
  3638.         debug(F111,"ftp day  ",s,xx->tm_mday);
  3639.         debug(F111,"ftp hour ",s,xx->tm_hour);
  3640.         debug(F111,"ftp min  ",s,xx->tm_min);
  3641.         debug(F111,"ftp sec  ",s,xx->tm_sec);
  3642.     }
  3643. }
  3644. #endif /* DEBUG */
  3645.  
  3646. /*  t m c o m p a r e  --  Compare two struct tm's */
  3647.  
  3648. /*  Like strcmp() but for struct tm's  */
  3649. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3650.  
  3651. static int
  3652. tmcompare(xx,yy) struct tm * xx, * yy; {
  3653.  
  3654.     if (xx->tm_year < yy->tm_year)      /* First year less than second */
  3655.       return(-1);
  3656.     if (xx->tm_year > yy->tm_year)      /* First year greater than second */
  3657.       return(1);
  3658.  
  3659.     /* Years are equal so compare months */
  3660.  
  3661.     if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
  3662.       return(-1);
  3663.     if (xx->tm_mon  > yy->tm_mon)
  3664.       return(1);
  3665.  
  3666.     if (xx->tm_mday < yy->tm_mday)
  3667.       return(-1);
  3668.     if (xx->tm_mday > yy->tm_mday)
  3669.       return(1);
  3670.  
  3671.     if (xx->tm_hour < yy->tm_hour)
  3672.       return(-1);
  3673.     if (xx->tm_hour > yy->tm_hour)
  3674.       return(1);
  3675.  
  3676.     if (xx->tm_min  < yy->tm_min)
  3677.       return(-1);
  3678.     if (xx->tm_min  > yy->tm_min)
  3679.       return(1);
  3680.  
  3681.     if (xx->tm_sec  < yy->tm_sec)
  3682.       return(-1);
  3683.     if (xx->tm_sec  > yy->tm_sec)
  3684.       return(1);
  3685.  
  3686.     return(0);
  3687. }
  3688. #endif /* DOUPDATE */
  3689.  
  3690. #ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
  3691. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3692.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3693. };
  3694.  
  3695. /* Macro for whether a given year is a leap year. */
  3696. #define ISLEAP(year) \
  3697. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3698. #endif /* HAVE_TIMEGM */
  3699.  
  3700. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3701.  
  3702. static time_t
  3703. #ifdef CK_ANSIC
  3704. mkutime(struct tm * tm)
  3705. #else
  3706. mkutime(tm) struct tm * tm;
  3707. #endif /* CK_ANSIC */
  3708. /* mkutime */ {
  3709. #ifdef HAVE_TIMEGM
  3710.     return(timegm(tm));                 /* Have system service, use it. */
  3711. #else
  3712. /*
  3713.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3714.   Given a struct tm representing a calendar time in UTC, convert it to
  3715.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3716.   convertable.  Note that this function does not canonicalize the provided
  3717.   struct tm, nor does it allow out-of-range values or years before 1970.
  3718.   Result should be identical with timegm().
  3719. */
  3720.     time_t result = 0;
  3721.     int i;
  3722.     /*
  3723.       We do allow some ill-formed dates, but we don't do anything special
  3724.       with them and our callers really shouldn't pass them to us.  Do
  3725.       explicitly disallow the ones that would cause invalid array accesses
  3726.       or other algorithm problems.
  3727.     */
  3728. #ifdef DEBUG
  3729.     if (deblog) {
  3730.         debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3731.         debug(F101,"mkutime tm_year","",tm->tm_year);
  3732.     }
  3733. #endif /* DEBUG */
  3734.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3735.       return((time_t) -1);
  3736.  
  3737.     /* Convert to time_t. */
  3738.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3739.       result += 365 + ISLEAP(i);
  3740.     for (i = 0; i < tm->tm_mon; i++)
  3741.       result += MONTHDAYS[i];
  3742.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3743.       result++;
  3744.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3745.     result = 60 * result + tm->tm_min;
  3746.     result = 60 * result + tm->tm_sec;
  3747.     debug(F101,"mkutime result","",result);
  3748.     return(result);
  3749. #endif /* HAVE_TIMEGM */
  3750. }
  3751.  
  3752.  
  3753. /*
  3754.   s e t m o d t i m e  --  Set file modification time.
  3755.  
  3756.   f = char * filename;
  3757.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3758.  
  3759.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3760.   Returns:
  3761.     0 on success,
  3762.    -1 on error.
  3763.  
  3764. */
  3765. static int
  3766. #ifdef CK_ANSIC
  3767. setmodtime(char * f, time_t t)
  3768. #else
  3769. setmodtime(f,t) char * f; time_t t;
  3770. #endif /* CK_ANSIC */
  3771. /* setmodtime */ {
  3772. #ifdef NT
  3773.     struct _stat sb;
  3774. #else /* NT */
  3775.     struct stat sb;
  3776. #endif /* NT */
  3777.     int x, rc = 0;
  3778. #ifdef BSD44
  3779.     struct timeval tp[2];
  3780. #else  /* def BSD44 */
  3781. #ifdef V7
  3782.     struct utimbuf {
  3783.         time_t timep[2];
  3784.     } tp;
  3785. #else  /* def V7 */
  3786. #ifdef SYSUTIMEH
  3787. #ifdef NT
  3788.     struct _utimbuf tp;
  3789. #else /* NT */
  3790.     struct utimbuf tp;
  3791. #endif /* NT */
  3792. #else /* def SYSUTIMEH */
  3793. #ifdef VMS
  3794.     struct utimbuf tp;
  3795. #define SYSUTIMEH               /* Our utimbuf matches this one. */
  3796. #else /* def VMS */
  3797.     struct utimbuf {
  3798.         time_t atime;
  3799.         time_t mtime;
  3800.     } tp;
  3801. #endif /* def VMS [else] */
  3802. #endif /* def SYSUTIMEH [else] */
  3803. #endif /* def V7 [else] */
  3804. #endif /* def BSD44 [else] */
  3805.  
  3806.     if (stat(f,&sb) < 0) {
  3807.         debug(F111,"setmodtime stat failure",f,errno);
  3808.         return(-1);
  3809.     }
  3810. #ifdef BSD44
  3811.     tp[0].tv_sec = sb.st_atime;         /* Access time first */
  3812.     tp[1].tv_sec = t;                   /* Update time second */
  3813.     debug(F111,"setmodtime BSD44",f,t);
  3814. #else
  3815. #ifdef V7
  3816.     tp.timep[0] = t;                    /* Set modif. time to creation date */
  3817.     tp.timep[1] = sb.st_atime;          /* Don't change the access time */
  3818.     debug(F111,"setmodtime V7",f,t);
  3819. #else
  3820. #ifdef SYSUTIMEH
  3821.     tp.modtime = t;                     /* Set modif. time to creation date */
  3822.     tp.actime = sb.st_atime;            /* Don't change the access time */
  3823.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3824. #else
  3825.     tp.mtime = t;                       /* Set modif. time to creation date */
  3826.     tp.atime = sb.st_atime;             /* Don't change the access time */
  3827.     debug(F111,"setmodtime (other)",f,t);
  3828. #endif /* SYSUTIMEH */
  3829. #endif /* V7 */
  3830. #endif /* BSD44 */
  3831.  
  3832.     /* Try to set the file date */
  3833.  
  3834. #ifdef BSD44
  3835.     x = utimes(f,tp);
  3836.     debug(F111,"setmodtime utimes()","BSD44",x);
  3837. #else
  3838. #ifdef IRIX65
  3839.     {
  3840.       /*
  3841.         The following produces the nonsensical warning:
  3842.         Argument  of type "const struct utimbuf *" is incompatible with
  3843.         parameter of type "const struct utimbuf *".  If you can make it
  3844.         go away, be my guest.
  3845.       */
  3846.         const struct utimbuf * t2 = &tp;
  3847.         x = utime(f,t2);
  3848.     }
  3849. #else
  3850.     x = utime(f,&tp);
  3851.     debug(F111,"setmodtime utime()","other",x);
  3852. #endif /* IRIX65 */
  3853. #endif /* BSD44 */
  3854.     if (x)
  3855.       rc = -1;
  3856.  
  3857.     debug(F101,"setmodtime result","",rc);
  3858.     return(rc);
  3859. }
  3860.  
  3861.  
  3862. /*
  3863.   c h k m o d t i m e  --  Check/Set file modification time.
  3864.  
  3865.   fc = function code:
  3866.     0 = Check; returns:
  3867.       -1 on error,
  3868.        0 if local older than remote,
  3869.        1 if modtimes are equal,
  3870.        2 if local newer than remote.
  3871.     1 = Set (local file's modtime from remote's); returns:
  3872.       -1 on error,
  3873.        0 on success.
  3874. */
  3875. static int
  3876. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3877. #ifdef NT
  3878.     struct _stat statbuf;
  3879. #else /* NT */
  3880.     struct stat statbuf;
  3881. #endif /* NT */
  3882.     struct tm * tmlocal = NULL;
  3883.     struct tm tmremote;
  3884.     int rc = 0, havedate = 0, lcs = -1, rcs = -1, flag = 0;
  3885.     char * s, timebuf[64];
  3886.  
  3887.     debug(F111,"chkmodtime",local,mdtmok);
  3888.     if (!mdtmok)            /* Server supports MDTM? */
  3889.       return(-1);            /* No don't bother. */
  3890.  
  3891. #ifndef NOCSETS
  3892.     if (ftp_xla) {
  3893.         lcs = ftp_csl;
  3894.         if (lcs < 0) lcs = fcharset;
  3895.         rcs = ftp_csx;
  3896.         if (rcs < 0) rcs = ftp_csr;
  3897.     }
  3898. #endif /* NOCSETS */
  3899.  
  3900.     if (fc == 0) {
  3901.         rc = stat(local,&statbuf);
  3902.         if (rc == 0) {                  /* Get local file's mod time */
  3903.             tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3904. #ifdef DEBUG
  3905.             if (tmlocal) {
  3906.                 dbtime(local,tmlocal);
  3907.             }
  3908. #endif /* DEBUG */
  3909.         }
  3910.     }
  3911.     /* Get remote file's mod time as yyyymmddhhmmss */
  3912.  
  3913.     if (havemdtm) {            /* Already got it from MLSD? */
  3914.     s = havemdtm;
  3915.     flag++;
  3916.     } else if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3917.         char c;
  3918.         bzero((char *)&tmremote, sizeof(struct tm));
  3919.         s = ftp_reply_str;
  3920.         while ((c = *s++)) {            /* Skip past response code */
  3921.             if (c == SP) {
  3922.                 flag++;
  3923.                 break;
  3924.             }
  3925.         }
  3926.     }
  3927.     if (flag) {
  3928.     debug(F111,"ftp chkmodtime string",s,flag);
  3929.     if (fts_sto) {            /* User gave server time offset? */
  3930.         char * p;
  3931.         debug(F110,"ftp chkmodtime offset",fts_sto,0);
  3932.         ckmakmsg(timebuf,64,s," ",fts_sto,NULL); /* Build delta time */
  3933.         if ((p = cmcvtdate(timebuf,1))) { /* Apply delta time */
  3934.         ckstrncpy(timebuf,p,64);      /* Convert to MDTM format */
  3935.         timebuf[8]  = timebuf[9];  /* h */
  3936.         timebuf[9]  = timebuf[10]; /* h */
  3937.         timebuf[10] = timebuf[12]; /* m */
  3938.         timebuf[11] = timebuf[13]; /* m */
  3939.         timebuf[12] = timebuf[12]; /* s */
  3940.         timebuf[13] = timebuf[13]; /* s */
  3941.         timebuf[14] = NUL;
  3942.         s = timebuf;
  3943.         debug(F110,"ftp chkmodtime adjust",s,0);
  3944.         }
  3945.     }
  3946.         if (flag) {                     /* Convert to struct tm */
  3947.             char * pat;
  3948.             int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
  3949.             if (!ckstrcmp(s,"191",3,0)) {
  3950.                 pat = "%05d%02d%02d%02d%02d%02d";
  3951.                 y2kbug++;
  3952.                 debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3953.             } else {
  3954.                 pat = "%04d%02d%02d%02d%02d%02d";
  3955.             }
  3956.             if (sscanf(s,               /* Parse into struct tm */
  3957.                        pat,
  3958.                        &(tmremote.tm_year),
  3959.                        &(tmremote.tm_mon),
  3960.                        &(tmremote.tm_mday),
  3961.                        &(tmremote.tm_hour),
  3962.                        &(tmremote.tm_min),
  3963.                        &(tmremote.tm_sec)
  3964.                        ) == 6) {
  3965.                 tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3966.                 debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3967.                 tmremote.tm_mon--;
  3968.  
  3969. #ifdef DEBUG
  3970.         debug(F100,"SERVER TIME FOLLOWS:","",0);
  3971.                 dbtime(remote,&tmremote);
  3972. #endif /* DEBUG */
  3973.  
  3974.                 if (havedate > -1)
  3975.           havedate = 1;
  3976.             }
  3977.         }
  3978.     } else {                /* Failed */
  3979.     debug(F101,"ftp chkmodtime ftpcode","",ftpcode);
  3980.     if (ftpcode == 500 ||        /* Command unrecognized */
  3981.         ftpcode == 502 ||        /* Command not implemented */
  3982.         ftpcode == 202)        /* Command superfluous */
  3983.       mdtmok = 0;            /* Don't ask this server again */
  3984.     return(-1);
  3985.     }
  3986.     if (fc == 0) {                      /* Compare */
  3987.         if (havedate == 1) {        /* Only if we have both file dates */
  3988.             /*
  3989.               Compare with local file's time.  We don't use
  3990.               clock time (time_t) here in case of signed/unsigned
  3991.               confusion, etc.
  3992.             */
  3993.         int xx;
  3994. #ifdef COMMENT
  3995. #ifdef DEBUG        
  3996.         if (deblog) {
  3997.         dbtime("LOCAL",tmlocal);
  3998.         dbtime("REMOT",&tmremote);
  3999.         }
  4000. #endif /* DEBUG */
  4001. #endif /* COMMENT */
  4002.         xx = tmcompare(tmlocal,&tmremote);
  4003.         debug(F101,"chkmodtime tmcompare","",xx);
  4004.             return(xx + 1);
  4005.         }
  4006.     } else if (ftp_dates) {             /* Set */
  4007.         /*
  4008.           Here we must convert struct tm to time_t
  4009.           without applying timezone conversion, for which
  4010.           there is no portable API.  The method is hidden
  4011.           in mkutime(), defined above.
  4012.         */
  4013.         time_t utc;
  4014.         utc = mkutime(&tmremote);
  4015.         debug(F111,"ftp chkmodtime mkutime",remote,utc);
  4016.         if (utc != (time_t)-1)
  4017.           return(setmodtime(local,utc));
  4018.     }
  4019.     return(-1);
  4020. }
  4021.  
  4022. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  4023.  
  4024. static int
  4025. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  4026.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  4027. /* getfile */ {
  4028.     int rc = -1;
  4029.     ULONG t0, t1;
  4030.  
  4031. #ifdef GFTIMER
  4032.     CKFLOAT sec;
  4033. #else
  4034.     int sec = 0;
  4035. #endif /* GFTIMER */
  4036.     char fullname[CKMAXPATH+1];
  4037.  
  4038.     debug(F110,"ftp getfile remote A",remote,0);
  4039.     debug(F110,"ftp getfile local A",local,0);
  4040.     debug(F110,"ftp getfile pipename",pipename,0);
  4041.     if (!remote) remote = "";
  4042.  
  4043. #ifdef PATTERNS
  4044.     /* Automatic type switching? */
  4045.     if (ftp_xfermode == XMODE_A && patterns && get_auto && !forcetype) {
  4046.         int x;
  4047.         x = matchname(remote,0,servertype);
  4048.         debug(F111,"ftp getfile matchname",remote,x);
  4049.         switch (x) {
  4050.           case 0: ftp_typ = FTT_ASC; break;
  4051.           case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  4052.           default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  4053.         }
  4054.         changetype(ftp_typ,ftp_vbm);
  4055.         binary = ftp_typ;               /* For file-transfer display */
  4056.     }
  4057. #endif /* PATTERNS */
  4058.  
  4059. #ifndef NOCSETS
  4060.     ftp_csx = -1;                       /* For file-transfer display */
  4061.     ftp_csl = -1;                       /* ... */
  4062.  
  4063.     if (rcs > -1)                       /* -1 means no translation */
  4064.       if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
  4065.         if (fcs < 0)                    /* File charset not forced? */
  4066.           fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
  4067.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  4068.         debug(F110,"ftp getfile","initxlate",0);
  4069.         initxlate(rcs,fcs);             /* NB: opposite order of PUT */
  4070.         ftp_csx = rcs;
  4071.         ftp_csl = fcs;
  4072.     } else
  4073.       xlate = 0;
  4074. #endif /* NOCSETS */
  4075.  
  4076.     if (!local) local = "";
  4077.     if (!pipename && !*local)
  4078.       local = remote;
  4079.  
  4080.     out2screen = !strcmp(local,"-");
  4081.  
  4082.     fullname[0] = NUL;
  4083.     if (pipename) {
  4084.         ckstrncpy(fullname,pipename,CKMAXPATH+1);
  4085.     } else {
  4086.         zfnqfp(local,CKMAXPATH,fullname);
  4087.         if (!fullname[0])
  4088.           ckstrncpy(fullname,local,CKMAXPATH+1);
  4089.     }
  4090.     if (!out2screen && displa && fdispla) { /* Screen */
  4091.         ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,remote);
  4092.         ftscreen(SCR_AN,0,(CK_OFF_T)0,fullname);
  4093.         ftscreen(SCR_FS,0,fsize,"");
  4094.     }
  4095.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  4096.     tlog(F110," as",fullname,0);
  4097.     debug(F111,"ftp getfile size",remote,fsize);
  4098.     debug(F111,"ftp getfile local",local,out2screen);
  4099.  
  4100.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  4101.  
  4102.     t0 = gmstimer();                    /* Start time */
  4103.     debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
  4104.     rc = recvrequest("RETR",
  4105.                      local,
  4106.                      remote,
  4107.                      append ? "ab" : "wb",
  4108.                      0,
  4109.                      recover,
  4110.                      pipename,
  4111.                      xlate,
  4112.                      fcs,
  4113.                      rcs
  4114.                      );
  4115.     t1 = gmstimer();                    /* End time */
  4116.     debug(F111,"ftp getfile t1",remote,t1);
  4117.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  4118. #ifdef GFTIMER
  4119.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4120.     fpxfsecs = sec;                     /* (for doxlog()) */
  4121. #else
  4122.     sec = (t1 - t0) / 1000;
  4123.     xfsecs = (int)sec;
  4124. #endif /* GFTIMER */
  4125.  
  4126. #ifdef FTP_TIMEOUT
  4127.     if (ftp_timed_out)
  4128.       rc = -4;
  4129. #endif    /* FTP_TIMEOUT */
  4130.  
  4131.     debug(F111,"ftp recvrequest rc",remote,rc);
  4132.     if (cancelfile || cancelgroup) {
  4133.         debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  4134.         ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4135.     } else if (rc > 0) {
  4136.         debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  4137.         ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,cmarg);
  4138.     } else if (rc < 0) {
  4139.         switch (ftpcode) {
  4140.           case -4:                      /* Network error */
  4141.           case -2:                      /* File error */
  4142.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,ck_errstr());
  4143.             break;
  4144.           case -3:
  4145.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,
  4146.              "Failure to make data connection");
  4147.             break;
  4148.           case -1:            /* (should be covered above) */
  4149.             ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4150.             break;
  4151.           default:
  4152.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
  4153.         }
  4154.     } else {                            /* Tudo bem */
  4155.         ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
  4156.         if (rc == 0) {
  4157.             ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,""); /* For screen */
  4158.             makestr(&rrfspec,remote);     /* For WHERE command */
  4159.             makestr(&rfspec,fullname);
  4160.         }
  4161.     }
  4162.     if (rc > -1) {
  4163.     if (ftp_dates)            /* If FTP DATES ON... */
  4164.       if (!pipename && !out2screen)    /* and it's a real file */
  4165.         if (rc < 1 && rc != -3)    /* and it wasn't skipped */
  4166.           if (connected)        /* and we still have a connection */
  4167.         if (zchki(local) > -1) { /* and the file wasn't discarded */
  4168.             chkmodtime(local,remote,1); /* set local file date */
  4169.             debug(F110,"ftp get set date",local,0);
  4170.         }
  4171.     filcnt++;            /* Used by \v(filenum) */
  4172.     }
  4173. #ifdef TLOG
  4174.     if (tralog) {
  4175.         if (rc > 0) {
  4176.             tlog(F100," recovery skipped","",0);
  4177.         } else if (rc == 0) {
  4178.             tlog(F101," complete, size", "", fsize);
  4179.         } else if (cancelfile) {
  4180.             tlog(F100," canceled by user","",0);
  4181. #ifdef FTP_TIMEOUT
  4182.         } else if (ftp_timed_out) {
  4183.             tlog(F100," timed out","",0);
  4184. #endif    /* FTP_TIMEOUT */
  4185.         } else {
  4186.             tlog(F110," failed:",ftp_reply_str,0);
  4187.         }
  4188.         if (!tlogfmt)
  4189.           doxlog(what,local,fsize,ftp_typ,rc,"");
  4190.     }
  4191. #endif /* TLOG */
  4192.     return(rc);
  4193. }
  4194.  
  4195. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  4196. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  4197.  
  4198. static int
  4199. putfile(cx,
  4200.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  4201.     char * local, * remote, * mvto, *rnto, *srvrn;
  4202.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg, prm;
  4203.  
  4204. /* putfile */ {
  4205.  
  4206.     char asname[CKMAXPATH+1];
  4207.     char fullname[CKMAXPATH+1];
  4208.     int k = -1, x = 0, y = 0, o = -1, rc = 0, nc = 0;
  4209.     int xlate = 0, restart = 0, mt = -1;
  4210.     char * s = NULL, * cmd = NULL;
  4211.     ULONG t0 = 0, t1 = 0;        /* Times for stats */
  4212.     int ofcs = 0, orcs = 0;
  4213.  
  4214. #ifdef GFTIMER
  4215.     CKFLOAT sec = 0.0;
  4216. #else
  4217.     int sec = 0;
  4218. #endif /* GFTIMER */
  4219.     debug(F111,"ftp putfile flg",local,flg);
  4220.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  4221.     debug(F101,"ftp putfile fcs","",fcs);
  4222.     debug(F101,"ftp putfile rcs","",rcs);
  4223.  
  4224.     ofcs = fcs;                         /* Save charset args */
  4225.     orcs = rcs;
  4226.  
  4227.     sendstart = (CK_OFF_T)0;
  4228.     restart = flg & PUT_RES;
  4229.     if (!remote)
  4230.       remote = "";
  4231.  
  4232.     /* FTP protocol command to send to server */
  4233.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  4234.  
  4235.     if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
  4236.         if (alike) {                    /* If server & client are alike */
  4237.             nc = 0;                     /* no conversion */
  4238.         } else {                        /* If they are different */
  4239.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  4240.               nc = -1;                  /* only minimal conversions needed */
  4241.             else                        /* otherwise */
  4242.               nc = 1;                   /* full conversion */
  4243.         }
  4244.     } else                              /* Not auto - do what user said */
  4245.       nc = x_cnv;
  4246.  
  4247.     /* If Transfer Mode is Automatic, determine file type */
  4248.     if (ftp_xfermode == XMODE_A && filepeek && !pipesend) {
  4249.         if (isdir(local)) {             /* If it's a directory */
  4250.             k = FT_BIN;                 /* skip the file scan */
  4251.         } else {
  4252.             debug(F110,"FTP PUT calling scanfile",local,0);
  4253.             k = scanfile(local,&o,nscanfile); /* Scan the file */
  4254.         }
  4255.         debug(F111,"FTP PUT scanfile",local,k);
  4256.         if (k > -1 && !forcetype) {
  4257.             ftp_typ = (k == FT_BIN) ? 1 : 0;
  4258.             if (xft > -1 && ftp_typ != xft) {
  4259.                 if (flg & PUT_SIM)
  4260.                   tlog(F110,"ftp put SKIP (Type):", local, 0);
  4261.                 return(SKP_TYP);
  4262.             }
  4263.             if (ftp_typ == 1 && tenex)  /* User said TENEX? */
  4264.               ftp_typ = FTT_TEN;
  4265.         }
  4266.     }
  4267. #ifndef NOCSETS
  4268.     ftp_csx = -1;                       /* For file-transfer display */
  4269.     ftp_csl = -1;                       /* ... */
  4270.  
  4271.     if (rcs > -1) {                     /* -1 means no translation */
  4272.         if (ftp_typ == 0) {             /* File type is "ascii"? */
  4273.             if (fcs < 0) {              /* File charset not forced? */
  4274.                 if (k < 0) {            /* If we didn't scan */
  4275.                     fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
  4276.                 } else {                /* If we did scan, use scan result */
  4277.                     switch (k) {
  4278.                       case FT_TEXT:     /* Unknown text */
  4279.                         fcs = fcharset;
  4280.                         break;
  4281.                       case FT_7BIT:     /* 7-bit text */
  4282.                         fcs = dcset7;
  4283.                         break;
  4284.                       case FT_8BIT:     /* 8-bit text */
  4285.                         fcs = dcset8;
  4286.                         break;
  4287.                       case FT_UTF8:     /* UTF-8 */
  4288.                         fcs = FC_UTF8;
  4289.                         break;
  4290.                       case FT_UCS2:     /* UCS-2 */
  4291.                         fcs = FC_UCS2;
  4292.                         if (o > -1)     /* Input file byte order */
  4293.                           fileorder = o;
  4294.                         break;
  4295.                       default:
  4296.                         rcs = -1;
  4297.                     }
  4298.                 }
  4299.             }
  4300.         }
  4301.     }
  4302.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  4303.         debug(F110,"ftp putfile","initxlate",0);
  4304.         initxlate(fcs,rcs);
  4305.         debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  4306.         xlate = 1;
  4307.         ftp_csx = rcs;
  4308.         ftp_csl = fcs;
  4309.     }
  4310. #endif /* NOCSETS */
  4311.  
  4312.     binary = ftp_typ;                   /* For file-transfer display */
  4313.     asname[0] = NUL;
  4314.  
  4315.     if (recursive) {                    /* If sending recursively, */
  4316.         if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  4317.           return(-1);                   /* Don't PUT if it fails. */
  4318.         else if (isdir(local))          /* It's a directory */
  4319.           return(0);                    /* Don't send it! */
  4320.     }
  4321.     if (*remote) {                      /* If an as-name template was given */
  4322. #ifndef NOSPL
  4323.         if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
  4324.             y = CKMAXPATH;              /* evaluate it for this file */
  4325.             s = asname;
  4326.             zzstring(remote,&s,&y);
  4327.         } else
  4328. #endif /* NOSPL */
  4329.           ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  4330.     } else {                                    /* No as-name */
  4331.         nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  4332.         debug(F110,"FTP PUT nzltor",asname,0);
  4333.     }
  4334.     /* Preliminary messages and log entries */
  4335.  
  4336.     fullname[0] = NUL;
  4337.     zfnqfp(local,CKMAXPATH,fullname);
  4338.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  4339.     fullname[CKMAXPATH] = NUL;
  4340.  
  4341.     if (displa && fdispla) {            /* Screen */
  4342.         ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,local);
  4343.         ftscreen(SCR_AN,0,(CK_OFF_T)0,asname);
  4344.         ftscreen(SCR_FS,0,fsize,"");
  4345.     }
  4346. #ifdef DOUPDATE
  4347.     if (flg & (PUT_UPD|PUT_DIF)) {    /* Date-checking modes... */
  4348.         mt = chkmodtime(fullname,asname,0);
  4349.         debug(F111,"ftp putfile chkmodtime",asname,mt);
  4350.         if (mt == 0 && ((flg & PUT_DIF) == 0)) { /* Local is older */
  4351.             tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  4352.         /* Skip this one */
  4353.             ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_DAT,fullname);
  4354.             filcnt++;
  4355.             return(SKP_DAT);
  4356.         } else if (mt == 1) {           /* Times are equal */
  4357.             tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  4358.             ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_EQU,fullname); /* Skip it */
  4359.             filcnt++;
  4360.             return(SKP_DAT);
  4361.         }
  4362.     /* Local file is newer */
  4363.         tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  4364.              "ftp put /update TEXT:", fullname, 0);
  4365.     } else if (flg & PUT_RES) {
  4366.         tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  4367.              "ftp put /recover TEXT:", fullname, 0);
  4368.     } else {
  4369.         tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4370.     }
  4371. #else
  4372.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4373. #endif /* DOUPDATE */
  4374.     tlog(F110," as",asname,0);
  4375.  
  4376. #ifndef NOCSETS
  4377.     if (xlate) {
  4378.         debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  4379.         tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  4380.         tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  4381.     } else if (!ftp_typ) {
  4382.         tlog(F110," character sets:","no conversion",0);
  4383.         fcs = ofcs;                     /* Binary file but we still must */
  4384.         rcs = orcs;                     /* translate its name */
  4385.     }
  4386. #endif /* NOCSETS */
  4387.  
  4388.     /* PUT THE FILE */
  4389.  
  4390.     t0 = gmstimer();                    /* Start time */
  4391.     if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
  4392.         if (flg & (PUT_UPD|PUT_DIF)) {    /* (see SKP_xxx in ckcker.h) */
  4393.             rc = (mt < 0) ?             /* Update mode... */
  4394.               SKP_XNX :                 /* Remote file doesn't exist */
  4395.                 SKP_XUP;                /* Remote file is older */
  4396.         } else {
  4397.             rc = SKP_SIM;               /* "Would be sent", period. */
  4398.         }
  4399.     } else {
  4400.         rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  4401.     }
  4402.     t1 = gmstimer();                    /* End time */
  4403.     filcnt++;                           /* File number */
  4404.  
  4405. #ifdef GFTIMER
  4406.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4407.     fpxfsecs = sec;                     /* (for doxlog()) */
  4408. #else
  4409.     sec = (t1 - t0) / 1000;
  4410.     xfsecs = (int)sec;
  4411. #endif /* GFTIMER */
  4412.  
  4413.     debug(F111,"ftp sendrequest rc",local,rc);
  4414.  
  4415.     if (cancelfile || cancelgroup) {
  4416.         debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  4417.         ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4418.     } else if (rc > 0) {
  4419.         debug(F101,"ftp put skipped",local,rc);
  4420.         ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)rc,fullname);
  4421.     } else if (rc < 0) {
  4422.         debug(F111,"ftp put error",local,ftpcode);
  4423.         ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
  4424.     } else {
  4425.         debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  4426.         ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
  4427.         debug(F111,"ftp put ST_OK",local,rc);
  4428.         ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,"");
  4429.         debug(F110,"ftp put old sfspec",sfspec,0);
  4430.         makestr(&sfspec,fullname);      /* For WHERE command */
  4431.         debug(F110,"ftp put new sfspec",sfspec,0);
  4432.         debug(F110,"ftp put old srfspec",srfspec,0);
  4433.         makestr(&srfspec,asname);
  4434.         debug(F110,"ftp put new srfspec",srfspec,0);
  4435.     }
  4436.  
  4437.     /* Final log entries */
  4438.  
  4439. #ifdef TLOG
  4440.     if (tralog) {
  4441.         if (rc > 0) {
  4442.             if (rc == SKP_XNX)
  4443.               tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  4444.             else if (rc == SKP_XUP)
  4445.               tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  4446.             else if (rc == SKP_SIM)
  4447.               tlog(F100," /simulate: WOULD BE SENT","",0);
  4448.             else
  4449.               tlog(F110," skipped:",gskreason(rc),0);
  4450.         } else if (rc == 0) {
  4451.             tlog(F101," complete, size", "", fsize);
  4452.         } else if (cancelfile) {
  4453.             tlog(F100," canceled by user","",0);
  4454.         } else {
  4455.             tlog(F110," failed:",ftp_reply_str,0);
  4456.         }
  4457.         if (!tlogfmt)
  4458.           doxlog(what,local,fsize,ftp_typ,rc,"");
  4459.     }
  4460. #endif /* TLOG */
  4461.  
  4462.     if (rc < 0)                         /* PUT did not succeed */
  4463.       return(-1);                       /* so done. */
  4464.  
  4465.     if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
  4466.       return(SKP_SIM);
  4467.  
  4468. #ifdef UNIX
  4469.     /* Set permissions too? */
  4470.  
  4471.     if (prm) {                          /* Change permissions? */
  4472.         s = zgperm(local);              /* Get perms of local file */
  4473.         if (!s) s = "";
  4474.         x = strlen(s);
  4475.         if (x > 3) s += (x - 3);
  4476.         if (rdigits(s)) {
  4477.             ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  4478.             x =
  4479.               ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  4480.             tlog(F110, x ? " chmod" : " chmod failed",
  4481.                  s,
  4482.                  0
  4483.                  );
  4484.             if (!x)
  4485.               return(-1);
  4486.         }
  4487.     }
  4488. #endif /* UNIX */
  4489.  
  4490.     /* Disposition of source file */
  4491.  
  4492.     if (moving) {
  4493.         x = zdelet(local);
  4494.         tlog(F110, (x > -1) ?
  4495.              " deleted" : " failed to delete",
  4496.              local,
  4497.              0
  4498.              );
  4499.         if (x < 0)
  4500.           return(-1);
  4501.     } else if (mvto) {
  4502.         x = zrename(local,mvto);
  4503.         tlog(F110, (x > -1) ?
  4504.              " moved source to" : " failed to move source to",
  4505.              mvto,
  4506.              0
  4507.              );
  4508.         if (x < 0)
  4509.           return(-1);
  4510.         /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,mvto); */
  4511.  
  4512.     } else if (rnto) {
  4513.         char * s = rnto;
  4514. #ifndef NOSPL
  4515.         int y;                          /* Pass it thru the evaluator */
  4516.         extern int cmd_quoting;         /* for \v(filename) */
  4517.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4518.             y = CKMAXPATH;
  4519.             s = (char *)asname;
  4520.             zzstring(rnto,&s,&y);
  4521.             s = (char *)asname;
  4522.         }
  4523. #endif /* NOSPL */
  4524.         if (s) if (*s) {
  4525.             int x;
  4526.             x = zrename(local,s);
  4527.             tlog(F110, (x > -1) ?
  4528.                  " renamed source file to" :
  4529.                  " failed to rename source file to",
  4530.                  s,
  4531.                  0
  4532.                  );
  4533.             if (x < 0)
  4534.               return(-1);
  4535.             /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,s); */
  4536.         }
  4537.     }
  4538.  
  4539.     /* Disposition of destination file */
  4540.  
  4541.     if (srvrn) {                        /* /SERVER-RENAME: */
  4542.         char * s = srvrn;
  4543. #ifndef NOSPL
  4544.         int y;                          /* Pass it thru the evaluator */
  4545.         extern int cmd_quoting; /* for \v(filename) */
  4546.         debug(F111,"ftp putfile srvrn",s,1);
  4547.  
  4548.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4549.             y = CKMAXPATH;
  4550.             s = (char *)fullname;       /* We can recycle this buffer now */
  4551.             zzstring(srvrn,&s,&y);
  4552.             s = (char *)fullname;
  4553.         }
  4554. #endif /* NOSPL */
  4555.         debug(F111,"ftp putfile srvrn",s,2);
  4556.         if (s) if (*s) {
  4557.             int x;
  4558.             x = ftp_rename(asname,s);
  4559.             debug(F111,"ftp putfile ftp_rename",asname,x);
  4560.             tlog(F110, (x > 0) ?
  4561.                  " renamed destination file to" :
  4562.                  " failed to rename destination file to",
  4563.                  s,
  4564.                  0
  4565.                  );
  4566.             if (x < 1)
  4567.               return(-1);
  4568.         }
  4569.     }
  4570.     return(0);
  4571. }
  4572.  
  4573. /* xxout must only be used for ASCII transfers */
  4574. static int
  4575. #ifdef CK_ANSIC
  4576. xxout(char c)
  4577. #else
  4578. xxout(c) char c;
  4579. #endif /* CK_ANSIC */
  4580. {
  4581. #ifndef OS2
  4582. #ifndef VMS
  4583. #ifndef MAC
  4584. #ifndef OSK
  4585.     /* For Unix, DG, Stratus, Amiga, Gemdos, other */
  4586.     if (c == '\012') {
  4587.     if (zzout(dout,(CHAR)'\015') < 0)
  4588.       return(-1);
  4589.     ftpsnd.bytes++;
  4590.     }
  4591. #else /* OSK */
  4592.     if (c == '\015') {
  4593.     c = '\012';
  4594.     if (zzout(dout,(CHAR)'\015') < 0)
  4595.       return(-1);
  4596.     ftpsnd.bytes++;
  4597.     }
  4598. #endif /* OSK */
  4599. #else /* MAC */
  4600.     if (c == '\015') {
  4601.     c = '\012';
  4602.     if (zzout(dout,(CHAR)'\015') < 0)
  4603.       return(-1);
  4604.     ftpsnd.bytes++;
  4605.     }
  4606. #endif /* MAC */
  4607. #endif /* VMS */
  4608. #endif /* OS2 */
  4609.     if (zzout(dout,(CHAR)c) < 0)
  4610.       return(-1);
  4611.     ftpsnd.bytes++;
  4612.     return(0);
  4613. }
  4614.  
  4615. static int
  4616. #ifdef CK_ANSIC
  4617. scrnout(char c)
  4618. #else
  4619. scrnout(c) char c;
  4620. #endif /* CK_ANSIC */
  4621. {
  4622.     return(putchar(c));
  4623. }
  4624.  
  4625. static int
  4626. #ifdef CK_ANSIC
  4627. pipeout(char c)
  4628. #else
  4629. pipeout(c) char c;
  4630. #endif /* CK_ANSIC */
  4631. {
  4632.     return(zmchout(c));
  4633. }
  4634.  
  4635. static int
  4636. ispathsep(c) int c; {
  4637.     switch (servertype) {
  4638.       case SYS_VMS:
  4639.       case SYS_TOPS10:
  4640.       case SYS_TOPS20:
  4641.         return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4642.       case SYS_OS2:
  4643.       case SYS_WIN32:
  4644.       case SYS_DOS:
  4645.         return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4646.       case SYS_VOS:
  4647.         return((c == '>') ? 1 : 0);
  4648.       default:
  4649.         return((c == '/') ? 1 : 0);
  4650.     }
  4651. }
  4652.  
  4653. static int
  4654. iscanceled() {
  4655. #ifdef CK_CURSES
  4656.     extern int ck_repaint();
  4657. #endif /* CK_CURSES */
  4658.     int x, rc = 0;
  4659.     char c = 0;
  4660.     if (cancelfile)
  4661.       return(1);
  4662.     x = conchk();                       /* Any chars waiting at console? */
  4663.     if (x-- > 0) {                      /* Yes...  */
  4664.         c = coninc(5);                  /* Get one */
  4665.         switch (c) {
  4666.           case 032:                     /* Ctrl-X or X */
  4667.           case 'z':
  4668.           case 'Z': cancelgroup++;      /* fall thru on purpose */
  4669.           case 030:                     /* Ctrl-Z or Z */
  4670.           case 'x':
  4671.           case 'X': cancelfile++; rc++; break;
  4672. #ifdef CK_CURSES
  4673.           case 'L':
  4674.           case 'l':
  4675.           case 014:                     /* Ctrl-L or L or Ctrl-W */
  4676.           case 027:
  4677.             ck_repaint();               /* Refresh screen */
  4678. #endif /* CK_CURSES */
  4679.         }
  4680.     }
  4681.     while (x-- > 0)                     /* Soak up any rest */
  4682.       c = coninc(1);
  4683.     return(rc);
  4684. }
  4685.  
  4686. #ifdef FTP_TIMEOUT
  4687. /* fc = 0 for read; 1 for write */
  4688. static int
  4689. check_data_connection(fd,fc) int fd, fc; {
  4690.     int x;
  4691.     struct timeval tv;
  4692.     fd_set in, out, err;
  4693.  
  4694.     if (ftp_timeout < 1L)
  4695.       return(0);
  4696.  
  4697.     FD_ZERO(&in);
  4698.     FD_ZERO(&out);
  4699.     FD_ZERO(&err);
  4700.     FD_SET(fd,fc ? &out : &in);
  4701.     tv.tv_sec = ftp_timeout;        /* Time limit */
  4702.     tv.tv_usec = 0L;
  4703.  
  4704. #ifdef INTSELECT
  4705.     x = select(FD_SETSIZE,(int *)&in,(int *)&out,(int *)&err,&tv);
  4706. #else
  4707.     x = select(FD_SETSIZE,&in,&out,&err,&tv);
  4708. #endif /* INTSELECT */
  4709.  
  4710.     if (x == 0) {
  4711. #ifdef EWOULDBLOCK
  4712.     errno = EWOULDBLOCK;
  4713. #else
  4714. #ifdef EAGAIN
  4715.     errno = EAGAIN;
  4716. #else
  4717.     errno = 11;
  4718. #endif    /* EAGAIN */
  4719. #endif    /* EWOULDBLOCK */
  4720.     debug(F100,"ftp check_data_connection TIMOUT","",0);
  4721.     return(-3);
  4722.     }
  4723.     return(0);
  4724. }
  4725. #endif    /* FTP_TIMEOUT */
  4726.  
  4727. /* zzsend - used by buffered output macros. */
  4728.  
  4729. static int
  4730. #ifdef CK_ANSIC
  4731. zzsend(int fd, CHAR c)
  4732. #else
  4733. zzsend(fd,c) int fd; CHAR c;
  4734. #endif /* CK_ANSIC */
  4735. {
  4736.     int rc;
  4737.  
  4738.     debug(F101,"zzsend ucbufsiz","",ucbufsiz);
  4739.     debug(F101,"zzsend nout","",nout);
  4740.     debug(F111,"zzsend","secure?",ftpissecure());
  4741.  
  4742.     if (iscanceled())                   /* Check for cancellation */
  4743.       return(-9);
  4744.  
  4745. #ifdef FTP_TIMEOUT    
  4746.     ftp_timed_out = 0;
  4747.     if (check_data_connection(fd,1) < 0) {
  4748.     ftp_timed_out = 1;
  4749.     return(-3);
  4750.     }
  4751. #endif    /* FTP_TIMEOUT */
  4752.  
  4753.     rc = (!ftpissecure()) ?
  4754.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4755.         secure_putbuf(fd, ucbuf, nout);
  4756.     ucbuf[nout] = NUL;
  4757.     nout = 0;
  4758.     ucbuf[nout++] = c;
  4759.     spackets++;
  4760.     pktnum++;
  4761.     if (rc > -1 && fdispla != XYFD_B) {
  4762.         spktl = nout;
  4763.         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  4764.     }
  4765.     return(rc);
  4766. }
  4767.  
  4768. /* c m d l i n p u t  --  Command-line PUT */
  4769.  
  4770. int
  4771. cmdlinput(stay) int stay; {
  4772.     int x, rc = 0, done = 0, good = 0, status = 0;
  4773.     ULONG t0, t1;                       /* Times for stats */
  4774. #ifdef GFTIMER
  4775.     CKFLOAT sec;
  4776. #else
  4777.     int sec = 0;
  4778. #endif /* GFTIMER */
  4779.  
  4780.     if (quiet) {                        /* -q really means quiet */
  4781.         displa = 0;
  4782.         fdispla = 0;
  4783.     } else {
  4784.         displa = 1;
  4785.         fdispla = XYFD_B;
  4786.     }
  4787.     testing = 0;
  4788.     out2screen = 0;
  4789.     dpyactive = 0;
  4790.     what = W_FTP|W_SEND;
  4791.  
  4792. #ifndef NOSPL
  4793.     cmd_quoting = 0;
  4794. #endif /* NOSPL */
  4795.     sndsrc = nfils;
  4796.  
  4797.     t0 = gmstimer();                    /* Record starting time */
  4798.  
  4799.     while (!done && !cancelgroup) {     /* Loop for all files */
  4800.  
  4801.         cancelfile = 0;
  4802.         x = gnfile();                   /* Get next file from list(s) */
  4803.         if (x == 0)                     /* (see gnfile() comments...) */
  4804.           x = gnferror;
  4805.  
  4806.         switch (x) {
  4807.           case 1:                       /* File to send */
  4808.             rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
  4809.                          filnam,        /* Local file to send */
  4810.                          filnam,        /* Remote name for file */
  4811.                          forcetype,     /* Text/binary mode forced */
  4812.                          0,             /* Not moving */
  4813.                          NULL,          /* No move-to */
  4814.                          NULL,          /* No rename-to */
  4815.                          NULL,          /* No server-rename */
  4816.                          ftp_cnv,       /* Filename conversion */
  4817.                          0,             /* Unique-server-names */
  4818.                          -1,            /* All file types */
  4819.                          0,             /* No permissions */
  4820.                          -1,            /* No character sets */
  4821.                          -1,            /* No character sets */
  4822.                          0              /* No update or restart */
  4823.                          );
  4824.             if (rc > -1) {
  4825.                 good++;
  4826.                 status = 1;
  4827.             }
  4828.             if (cancelfile) {
  4829.                 continue;               /* Or break? */
  4830.             }
  4831.             if (rc < 0) {
  4832.                 ftp_fai++;
  4833.             }
  4834.             continue;                   /* Or break? */
  4835.  
  4836.           case 0:                       /* No more files, done */
  4837.             done++;
  4838.             continue;
  4839.  
  4840.           case -2:
  4841.           case -1:
  4842.             printf("?%s: file not found - \"%s\"\n",
  4843.                    puterror ? "Fatal" : "Warning",
  4844.                    filnam
  4845.                    );
  4846.             continue;                   /* or break? */
  4847.           case -3:
  4848.             printf("?Warning access denied - \"%s\"\n", filnam);
  4849.             continue;                   /* or break? */
  4850.           case -5:
  4851.             printf("?Too many files match\n");
  4852.             done++;
  4853.             break;
  4854.           case -6:
  4855.             if (good < 1)
  4856.               printf("?No files selected\n");
  4857.             done++;
  4858.             break;
  4859.           default:
  4860.             printf("?getnextfile() - unknown failure\n");
  4861.             done++;
  4862.         }
  4863.     }
  4864.     if (status > 0) {
  4865.         if (cancelgroup)
  4866.           status = 0;
  4867.         else if (cancelfile && good < 1)
  4868.           status = 0;
  4869.     }
  4870.     success = status;
  4871.     x = success;
  4872.     if (x > -1) {
  4873.         lastxfer = W_FTP|W_SEND;
  4874.         xferstat = success;
  4875.     }
  4876.     t1 = gmstimer();                    /* End time */
  4877. #ifdef GFTIMER
  4878.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4879.     if (!sec) sec = 0.001;
  4880.     fptsecs = sec;
  4881. #else
  4882.     sec = (t1 - t0) / 1000;
  4883.     if (!sec) sec = 1;
  4884. #endif /* GFTIMER */
  4885.     tfcps = (long) (tfc / sec);
  4886.     tsecs = (int)sec;
  4887.     lastxfer = W_FTP|W_SEND;
  4888.     xferstat = success;
  4889.     if (dpyactive)
  4890.       ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
  4891.     if (!stay)
  4892.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4893.     return(success);
  4894. }
  4895.  
  4896.  
  4897. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4898.  
  4899. int
  4900. #ifdef CK_ANSIC
  4901. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4902. #else
  4903. doftpput(cx,who) int cx, who;
  4904. #endif /* CK_ANSIC */
  4905. {
  4906.     struct FDB sf, fl, sw, cm;
  4907.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4908.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4909.     char * s, * s2;
  4910.  
  4911.     int x_csl, x_csr = -1;              /* Local and remote charsets */
  4912.     int x_xla = 0;
  4913.     int x_recurse = 0;
  4914.     char c, * p;                        /* Workers */
  4915. #ifdef PUTARRAY
  4916.     int range[2];                       /* Array range */
  4917.     char ** ap = NULL;                  /* Array pointer */
  4918.     int arrayx = -1;                    /* Array index */
  4919. #endif /* PUTARRAY */
  4920.     ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
  4921. #ifdef GFTIMER
  4922.     CKFLOAT sec;
  4923. #else
  4924.     int sec = 0;
  4925. #endif /* GFTIMER */
  4926.  
  4927.     struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
  4928.     success = 0;                        /* Assume failure */
  4929.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  4930.     out2screen = 0;                     /* Not outputting file to screen */
  4931.     putflags = 0;                       /* PUT options */
  4932.     x_cnv = ftp_cnv;                    /* Filename conversion */
  4933.     x_usn = ftp_usn;                    /* Unique server names */
  4934.     x_prm = ftp_prm;                    /* Permissions */
  4935.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  4936.       x_prm = alike;
  4937.  
  4938. #ifndef NOCSETS
  4939.     x_csr = ftp_csr;                    /* Inherit global server charset */
  4940.     x_csl = ftp_csl;
  4941.     if (x_csl < 0)
  4942.       x_csl = fcharset;
  4943.     x_xla = ftp_xla;
  4944. #endif /* NOCSETS */
  4945.  
  4946.     makestr(&filefile,NULL);            /* No filename list file yet. */
  4947.     makestr(&srv_renam,NULL);        /* Clear /SERVER-RENAME: */
  4948.     makestr(&snd_rename,NULL);        /*  PUT /RENAME */
  4949.     makestr(&snd_move,NULL);        /*  PUT /MOVE */
  4950.     putpath[0] = NUL;                   /* Initialize for syncdir(). */
  4951.     puterror = ftp_err;                 /* Inherit global error action. */
  4952.     what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
  4953.     asnambuf[0] = NUL;                  /* Clear as-name buffer */
  4954.  
  4955.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  4956.         ftp_typ = g_ftp_typ;
  4957.         /* g_ftp_typ = -1; */
  4958.     }
  4959.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4960.         pv[i].sval = NULL;              /* to null pointers */
  4961.         pv[i].ival = -1;                /* and -1 int values */
  4962.         pv[i].wval = (CK_OFF_T)-1;    /* and -1 wide values */
  4963.     }
  4964.     if (who == 0) {                     /* Called with unprefixed command */
  4965.         switch (cx) {
  4966.           case XXRSEN:  pv[SND_RES].ival = 1; break;
  4967.           case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4968.           case XXMOVE:  pv[SND_DEL].ival = 1; break;
  4969.           case XXMMOVE: pv[SND_DEL].ival = 1; /* fall thru */
  4970.           case XXMSE:   mput++; break;
  4971.         }
  4972.     } else {
  4973.     if (cx == FTP_REP)
  4974.       pv[SND_RES].ival = 1;
  4975.         if (cx == FTP_MPU)
  4976.           mput++;
  4977.     }
  4978.     cmfdbi(&sw,                         /* First FDB - command switches */
  4979.            _CMKEY,                      /* fcode */
  4980.            "Filename, or switch",       /* hlpmsg */
  4981.            "",                          /* default */
  4982.            "",                          /* addtl string data */
  4983.            nputswi,                     /* addtl numeric data 1: tbl size */
  4984.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4985.            xxstring,                    /* Processing function */
  4986.            putswi,                      /* Keyword table */
  4987.            &sf                          /* Pointer to next FDB */
  4988.            );
  4989.     cmfdbi(&fl,                         /* 3rd FDB - local filespec */
  4990.            _CMFLD,                      /* fcode */
  4991.            "",                          /* hlpmsg */
  4992.            "",                          /* default */
  4993.            "",                          /* addtl string data */
  4994.            0,                           /* addtl numeric data 1 */
  4995.            0,                           /* addtl numeric data 2 */
  4996.            xxstring,
  4997.            NULL,
  4998.            &cm
  4999.            );
  5000.     cmfdbi(&cm,                         /* 4th FDB - Confirmation */
  5001.            _CMCFM,                      /* fcode */
  5002.            "",                          /* hlpmsg */
  5003.            "",                          /* default */
  5004.            "",                          /* addtl string data */
  5005.            0,                           /* addtl numeric data 1 */
  5006.            0,                           /* addtl numeric data 2 */
  5007.            NULL,
  5008.            NULL,
  5009.            NULL
  5010.            );
  5011.  
  5012.   again:
  5013.     cmfdbi(&sf,                         /* 2nd FDB - file to send */
  5014.            _CMIFI,                      /* fcode */
  5015.            "",                          /* hlpmsg */
  5016.            "",                          /* default */
  5017.            "",                          /* addtl string data */
  5018.            /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  5019.            nolinks | x_recurse,         /* addtl numeric data 1 */
  5020.            0,                           /* dirflg 0 means "not dirs only" */
  5021.            xxstring,
  5022.            NULL,
  5023. #ifdef COMMENT
  5024.            mput ? &cm : &fl
  5025. #else
  5026.        &fl
  5027. #endif /* COMMENT */
  5028.            );
  5029.  
  5030.     while (1) {                         /* Parse zero or more switches */
  5031.         x = cmfdb(&sw);                 /* Parse something */
  5032.         debug(F101,"ftp put cmfdb A","",x);
  5033.         debug(F101,"ftp put fcode A","",cmresult.fcode);
  5034.         if (x < 0)                      /* Error */
  5035.           goto xputx;                   /* or reparse needed */
  5036.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  5037.           break;
  5038.         c = cmgbrk();                   /* Get break character */
  5039.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  5040.         if (getval && !(cmresult.kflags & CM_ARG)) {
  5041.             printf("?This switch does not take arguments\n");
  5042.             x = -9;
  5043.             goto xputx;
  5044.         }
  5045.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  5046.             printf("?This switch requires an argument\n");
  5047.             x = -9;
  5048.             goto xputx;
  5049.         }
  5050.         n = cmresult.nresult;           /* Numeric result = switch value */
  5051.         debug(F101,"ftp put switch","",n);
  5052.  
  5053.         switch (n) {                    /* Process the switch */
  5054.           case SND_AFT:                 /* Send /AFTER:date-time */
  5055.           case SND_BEF:                 /* Send /BEFORE:date-time */
  5056.           case SND_NAF:                 /* Send /NOT-AFTER:date-time */
  5057.           case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
  5058.             if (!getval) break;
  5059.             if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  5060.                 if (x == -3) {
  5061.                     printf("?Date-time required\n");
  5062.                     x = -9;
  5063.                 }
  5064.                 goto xputx;
  5065.             }
  5066.             pv[n].ival = 1;
  5067.             makestr(&(pv[n].sval),s);
  5068.             break;
  5069.  
  5070.           case SND_ASN:                 /* /AS-NAME: */
  5071.             debug(F101,"ftp put /as-name getval","",getval);
  5072.             if (!getval) break;
  5073.             if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  5074.                 if (x == -3) {
  5075.                     printf("?name required\n");
  5076.                     x = -9;
  5077.                 }
  5078.                 goto xputx;
  5079.             }
  5080.             makestr(&(pv[n].sval),brstrip(s));
  5081.             debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  5082.             if (pv[n].sval) pv[n].ival = 1;
  5083.             break;
  5084.  
  5085. #ifdef PUTARRAY
  5086.           case SND_ARR:                 /* /ARRAY */
  5087.             if (!getval) break;
  5088.             ap = NULL;
  5089.             if ((x = cmfld("Array name (a single letter will do)",
  5090.                            "",
  5091.                            &s,
  5092.                            NULL
  5093.                            )) < 0) {
  5094.                 if (x == -3)
  5095.           break;
  5096.         else
  5097.           return(x);
  5098.             }
  5099.             if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  5100.                 printf("?Bad array: %s\n",s);
  5101.                 return(-9);
  5102.             }
  5103.             if (!(ap = a_ptr[x])) {
  5104.                 printf("?No such array: %s\n",s);
  5105.                 return(-9);
  5106.             }
  5107.             pv[n].ival = 1;
  5108.             pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
  5109.             pv[SND_RES].ival = 0;
  5110.             pv[SND_FIL].ival = 0;
  5111.             arrayx = x;
  5112.             break;
  5113. #endif /* PUTARRAY */
  5114.  
  5115.           case SND_BIN:                 /* /BINARY */
  5116.           case SND_TXT:                 /* /TEXT or /ASCII */
  5117.           case SND_TEN:                 /* /TENEX */
  5118.             pv[SND_BIN].ival = 0;
  5119.             pv[SND_TXT].ival = 0;
  5120.             pv[SND_TEN].ival = 0;
  5121.             pv[n].ival = 1;
  5122.             break;
  5123.  
  5124. #ifdef PUTPIPE
  5125.           case SND_CMD:                 /* These take no args */
  5126.             if (nopush) {
  5127.                 printf("?Sorry, system command access is disabled\n");
  5128.                 x = -9;
  5129.                 goto xputx;
  5130.             }
  5131. #ifdef PIPESEND
  5132.             else if (sndfilter) {
  5133.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  5134.                 x = -9;
  5135.                 goto xputx;
  5136.             }
  5137. #endif /* PIPESEND */
  5138.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  5139.             pv[n].ival = 1;             /* Just set the flag */
  5140.             pv[SND_ARR].ival = 0;
  5141.             break;
  5142. #endif /* PUTPIPE */
  5143.  
  5144. #ifdef CKSYMLINK
  5145.           case SND_LNK:
  5146.             nolinks = 0;
  5147.             goto again;            /* Because CMIFI params changed... */
  5148.           case SND_NLK:
  5149.             nolinks = 2;
  5150.             goto again;
  5151. #endif /* CKSYMLINK */
  5152.  
  5153. #ifdef FTP_RESTART
  5154.           case SND_RES:                 /* /RECOVER (resend) */
  5155.             pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
  5156. #endif /* FTP_RESTART */
  5157.  
  5158.           case SND_NOB:
  5159.           case SND_DEL:                 /* /DELETE */
  5160.           case SND_SHH:                 /* /QUIET */
  5161.           case SND_UPD:                 /* /UPDATE */
  5162.           case SND_SIM:                 /* /UPDATE */
  5163.           case SND_USN:                 /* /UNIQUE */
  5164.             pv[n].ival = 1;             /* Just set the flag */
  5165.             break;
  5166.  
  5167.           case SND_REC:                 /* /RECURSIVE */
  5168.             recursive = 2;              /* Must be set before cmifi() */
  5169.             x_recurse = 1;
  5170.             goto again;            /* Because CMIFI params changed... */
  5171.             break;
  5172.  
  5173. #ifdef UNIXOROSK
  5174.           case SND_DOT:                 /* /DOTFILES */
  5175.             matchdot = 1;
  5176.             break;
  5177.           case SND_NOD:                 /* /NODOTFILES */
  5178.             matchdot = 0;
  5179.             break;
  5180. #endif /* UNIXOROSK */
  5181.  
  5182.           case SND_ERR:                 /* /ERROR-ACTION */
  5183.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  5184.               goto xputx;
  5185.             pv[n].ival = x;
  5186.             break;
  5187.  
  5188.           case SND_EXC:                 /* Excludes */
  5189.             if (!getval) break;
  5190.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5191.                 if (x == -3) {
  5192.                     printf("?Pattern required\n");
  5193.                     x = -9;
  5194.                 }
  5195.                 goto xputx;
  5196.             }
  5197.             if (s) if (!*s) s = NULL;
  5198.             makestr(&(pv[n].sval),s);
  5199.             if (pv[n].sval)
  5200.               pv[n].ival = 1;
  5201.             break;
  5202.  
  5203.           case SND_PRM:                 /* /PERMISSIONS */
  5204.             if (!getval)
  5205.               x = 1;
  5206.             else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  5207.               goto xputx;
  5208.             pv[SND_PRM].ival = x;
  5209.             break;
  5210.  
  5211. #ifdef PIPESEND
  5212.           case SND_FLT:                 /* /FILTER */
  5213.             debug(F101,"ftp put /filter getval","",getval);
  5214.             if (!getval) break;
  5215.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  5216.                 if (x == -3)
  5217.                   s = "";
  5218.                 else
  5219.                   goto xputx;
  5220.             }
  5221.             if (*s) s = brstrip(s);
  5222.             y = strlen(s);
  5223.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  5224.                 if (s[x] != '\\') continue;
  5225.                 if (s[x+1] == 'v') break;
  5226.             }
  5227.             if (x == y) {
  5228.                 printf(
  5229.                 "?Filter must contain a replacement variable for filename.\n"
  5230.                        );
  5231.                 x = -9;
  5232.                 goto xputx;
  5233.             }
  5234.             if (s) if (!*s) s = NULL;
  5235.             makestr(&(pv[n].sval),s);
  5236.             if (pv[n].sval)
  5237.               pv[n].ival = 1;
  5238.             break;
  5239. #endif /* PIPESEND */
  5240.  
  5241.           case SND_NAM:                 /* /FILENAMES */
  5242.             if (!getval) break;
  5243.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  5244.               goto xputx;
  5245.             debug(F101,"ftp put /filenames","",x);
  5246.             pv[n].ival = x;
  5247.             break;
  5248.  
  5249.           case SND_SMA:                 /* Smaller / larger than */
  5250.           case SND_LAR: {
  5251.           CK_OFF_T y;
  5252.           if (!getval) break;
  5253.           if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
  5254.         goto xputx;
  5255.           pv[n].wval = y;
  5256.           break;
  5257.       }
  5258.           case SND_FIL:                 /* Name of file containing filenames */
  5259.             if (!getval) break;
  5260.             if ((x = cmifi("Name of file containing list of filenames",
  5261.                                "",&s,&y,xxstring)) < 0) {
  5262.                 if (x == -3) {
  5263.                     printf("?Filename required\n");
  5264.                     x = -9;
  5265.                 }
  5266.                 goto xputx;
  5267.             } else if (y && iswild(s)) {
  5268.                 printf("?Wildcards not allowed\n");
  5269.                 x = -9;
  5270.                 goto xputx;
  5271.             }
  5272.             if (s) if (!*s) s = NULL;
  5273.             makestr(&(pv[n].sval),s);
  5274.             if (pv[n].sval) {
  5275.                 pv[n].ival = 1;
  5276.                 pv[SND_ARR].ival = 0;
  5277.             } else {
  5278.                 pv[n].ival = 0;
  5279.             }
  5280.             mput = 0;
  5281.             break;
  5282.  
  5283.           case SND_MOV:                 /* MOVE after */
  5284.           case SND_REN:                 /* RENAME after */
  5285.           case SND_SRN: {               /* SERVER-RENAME after */
  5286.               char * m = "";
  5287.               switch (n) {
  5288.                 case SND_MOV:
  5289.                   m = "device and/or directory for source file after sending";
  5290.                   break;
  5291.                 case SND_REN:
  5292.                   m = "new name for source file after sending";
  5293.                   break;
  5294.                 case SND_SRN:
  5295.                   m = "new name for destination file after sending";
  5296.                   break;
  5297.               }
  5298.               if (!getval) break;
  5299.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  5300.                   if (x == -3) {
  5301.                       printf("%s\n", n == SND_MOV ?
  5302.                              "?Destination required" :
  5303.                              "?New name required"
  5304.                              );
  5305.                       x = -9;
  5306.                   }
  5307.                   goto xputx;
  5308.               }
  5309.               if (s) if (!*s) s = NULL;
  5310.               makestr(&(pv[n].sval),s ? brstrip(s) : NULL);
  5311.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  5312.               break;
  5313.           }
  5314.           case SND_STA:                 /* Starting position (= PSEND) */
  5315.             if (!getval) break;
  5316.             if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  5317.               goto xputx;
  5318.             pv[n].ival = y;
  5319.             break;
  5320.  
  5321.           case SND_TYP:                 /* /TYPE */
  5322.             if (!getval) break;
  5323.             if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  5324.               goto xputx;
  5325.             pv[n].ival = (x == 2) ? -1 : x;
  5326.             break;
  5327.  
  5328. #ifndef NOCSETS
  5329.           case SND_CSL:                 /* Local character set */
  5330.           case SND_CSR:                 /* Remote (server) charset */
  5331.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0) {
  5332.         return((x == -3) ? -2 : x);
  5333.             }
  5334.         if (n == SND_CSL)
  5335.               x_csl = x;
  5336.             else
  5337.               x_csr = x;
  5338.             x_xla = 1;                  /* Overrides global OFF setting */
  5339.             break;
  5340.  
  5341.           case SND_XPA:                 /* Transparent */
  5342.             x_xla = 0;
  5343.             x_csr = -1;
  5344.             x_csl = -1;
  5345.             break;
  5346. #endif /* NOCSETS */
  5347.         }
  5348.     }
  5349. #ifdef PIPESEND
  5350.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  5351.         if (sndfilter || pv[SND_FLT].ival > 0) {
  5352.             printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  5353.             x = -9;
  5354.             goto xputx;
  5355.         }
  5356.     if (sfttab[0] > 0 && sfttab[SFT_REST] == 0)
  5357.       printf("WARNING: Server says it doesn't support REST.\n");
  5358.     }
  5359. #endif /* PIPESEND */
  5360.  
  5361.     cmarg = "";
  5362.     cmarg2 = asnambuf;
  5363.     line[0] = NUL;
  5364.     s = line;
  5365.     wild = 0;
  5366.  
  5367.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  5368.       case _CMIFI:                      /* Input filename */
  5369.         if (pv[SND_FIL].ival > 0) {
  5370.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  5371.             x = -9;
  5372.             goto xputx;
  5373.         }
  5374.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  5375.         if (pv[SND_ARR].ival > 0)
  5376.           ckstrncpy(asnambuf,line,CKMAXPATH);
  5377.         else
  5378.           wild = cmresult.nresult;      /* Wild flag */
  5379.         debug(F111,"ftp put wild",line,wild);
  5380.         if (!wild && !recursive && !mput)
  5381.           nolinks = 0;
  5382.         break;
  5383.       case _CMFLD:                      /* Field */
  5384.         /* Only allowed with /COMMAND and /ARRAY */
  5385.         if (pv[SND_FIL].ival > 0) {
  5386.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  5387.             x = -9;
  5388.             goto xputx;
  5389.         }
  5390.     /* For MPUT it's OK to have filespecs that don't match any files */
  5391.     if (mput)
  5392.       break;
  5393.         if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  5394. #ifdef CKROOT
  5395.             if (ckrooterr)
  5396.               printf("?Off limits: %s\n",cmresult.sresult);
  5397.             else
  5398. #endif /* CKROOT */
  5399.               printf("?%s - \"%s\"\n",
  5400.                    iswild(cmresult.sresult) ?
  5401.                    "No files match" : "File not found",
  5402.                    cmresult.sresult
  5403.                    );
  5404.             x = -9;
  5405.             goto xputx;
  5406.         }
  5407.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  5408.         if (pv[SND_ARR].ival > 0)
  5409.           ckstrncpy(asnambuf,line,CKMAXPATH);
  5410.         break;
  5411.       case _CMCFM:                      /* Confirmation */
  5412.         confirmed = 1;
  5413.         break;
  5414.       default:
  5415.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  5416.         x = -9;
  5417.         goto xputx;
  5418.     }
  5419.     debug(F110,"ftp put string",s,0);
  5420.     debug(F101,"ftp put confirmed","",confirmed);
  5421.  
  5422.     /* Save and change protocol and transfer mode */
  5423.     /* Global values are restored in main parse loop */
  5424.  
  5425.     g_displa = fdispla;
  5426.     if (ftp_dis > -1)
  5427.       fdispla = ftp_dis;
  5428.     g_skipbup = skipbup;
  5429.  
  5430.     if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
  5431.         g_skipbup = skipbup;
  5432.         skipbup = 1;
  5433.     }
  5434.     if (pv[SND_TYP].ival > -1) {        /* /TYPE */
  5435.         xfiletype = pv[SND_TYP].ival;
  5436.         if (xfiletype == 2)
  5437.           xfiletype = -1;
  5438.     }
  5439.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  5440.         forcetype = 1;                  /* So skip file scan */
  5441.         ftp_typ = FTT_BIN;              /* Set binary */
  5442.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  5443.         forcetype = 1;
  5444.         ftp_typ = FTT_ASC;
  5445.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  5446.         forcetype = 1;
  5447.         ftp_typ = FTT_TEN;
  5448.     } else if (ftp_cmdlin && ftp_xfermode == XMODE_M) {
  5449.         forcetype = 1;
  5450.         ftp_typ = binary;
  5451.         g_ftp_typ = binary;
  5452.     }
  5453.  
  5454. #ifdef PIPESEND
  5455.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  5456.         debug(F110,"PUT /COMMAND before stripping",s,0);
  5457.         s = brstrip(s);
  5458.         debug(F110,"PUT /COMMAND after stripping",s,0);
  5459.         if (!*s) {
  5460.             printf("?Sorry, a command to send from is required\n");
  5461.             x = -9;
  5462.             goto xputx;
  5463.         }
  5464.         cmarg = s;
  5465.     }
  5466. #endif /* PIPESEND */
  5467.  
  5468. /* Set up /MOVE and /RENAME */
  5469.  
  5470.     if (pv[SND_DEL].ival > 0 &&
  5471.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  5472.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  5473.         x = -9;
  5474.         goto xputx;
  5475.     }
  5476. #ifdef CK_TMPDIR
  5477.     if (pv[SND_MOV].ival > 0) {
  5478.         int len;
  5479.         char * p = pv[SND_MOV].sval;
  5480.         len = strlen(p);
  5481.         if (!isdir(p)) {                /* Check directory */
  5482. #ifdef CK_MKDIR
  5483.             char * s = NULL;
  5484.             s = (char *)malloc(len + 4);
  5485.             if (s) {
  5486.                 strcpy(s,p);            /* safe */
  5487. #ifdef datageneral
  5488.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  5489. #else
  5490.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  5491. #endif /* datageneral */
  5492.                 s[len++] = 'X';
  5493.                 s[len] = NUL;
  5494. #ifdef NOMKDIR
  5495.                 x = -1;
  5496. #else
  5497.                 x = zmkdir(s);
  5498. #endif /* NOMKDIR */
  5499.                 free(s);
  5500.                 if (x < 0) {
  5501.                     printf("?Can't create \"%s\"\n",p);
  5502.                     x = -9;
  5503.                     goto xputx;
  5504.                 }
  5505.             }
  5506. #else
  5507.             printf("?Directory \"%s\" not found\n",p);
  5508.             x = -9;
  5509.             goto xputx;
  5510. #endif /* CK_MKDIR */
  5511.         }
  5512.         makestr(&snd_move,p);
  5513.     }
  5514. #endif /* CK_TMPDIR */
  5515.  
  5516.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  5517.         char * p = pv[SND_REN].sval;
  5518.         if (!p) p = "";
  5519.         if (!*p) {
  5520.             printf("?New name required for /RENAME\n");
  5521.             x = -9;
  5522.             goto xputx;
  5523.         }
  5524.         p = brstrip(p);
  5525. #ifndef NOSPL
  5526.     /* If name given is wild, rename string must contain variables */
  5527.         if (wild) {
  5528.             char * s = tmpbuf;
  5529.             x = TMPBUFSIZ;
  5530.             zzstring(p,&s,&x);
  5531.             if (!strcmp(tmpbuf,p)) {
  5532.                 printf(
  5533.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  5534.                        );
  5535.                 x = -9;
  5536.                 goto xputx;
  5537.             }
  5538.         }
  5539. #endif /* NOSPL */
  5540.         makestr(&snd_rename,p);
  5541.         debug(F110,"FTP snd_rename",snd_rename,0);
  5542.     }
  5543.     if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
  5544.         char * p = pv[SND_SRN].sval;
  5545.         if (!p) p = "";
  5546.         if (!*p) {
  5547.             printf("?New name required for /SERVER-RENAME\n");
  5548.             x = -9;
  5549.             goto xputx;
  5550.         }
  5551.         p = brstrip(p);
  5552. #ifndef NOSPL
  5553.         if (wild) {
  5554.             char * s = tmpbuf;
  5555.             x = TMPBUFSIZ;
  5556.             zzstring(p,&s,&x);
  5557.             if (!strcmp(tmpbuf,p)) {
  5558.                 printf(
  5559. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  5560.                        );
  5561.                 x = -9;
  5562.                 goto xputx;
  5563.             }
  5564.         }
  5565. #endif /* NOSPL */
  5566.         makestr(&srv_renam,p);
  5567.         debug(F110,"ftp put srv_renam",srv_renam,0);
  5568.     }
  5569.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  5570.         char * lp;
  5571.         if (mput) {                     /* MPUT or MMOVE */
  5572.             nfils = 0;                  /* We already have the first one */
  5573. #ifndef NOMSEND
  5574.         if (cmresult.fcode == _CMIFI) {
  5575.         /* First filespec is valid */
  5576.         msfiles[nfils++] = line;    /* Store pointer */
  5577.         lp = line + (int)strlen(line) + 1; /* Point past it */
  5578.         debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  5579.         } else {
  5580.         /* First filespec matches no files */
  5581.         debug(F110,"ftp put mput skipping first filespec",
  5582.               cmresult.sresult,
  5583.               0
  5584.               );
  5585.         lp = line;
  5586.         }
  5587.         /* Parse a filespec, a "field", or confirmation */
  5588.  
  5589.         cmfdbi(&sf,            /* 1st FDB - file to send */
  5590.            _CMIFI,        /* fcode */
  5591.            "",            /* hlpmsg */
  5592.            "",            /* default */
  5593.            "",            /* addtl string data */
  5594.            nolinks | x_recurse,    /* addtl numeric data 1 */
  5595.            0,            /* dirflg 0 means "not dirs only" */
  5596.            xxstring,
  5597.            NULL,
  5598.            &fl
  5599.            );
  5600.         cmfdbi(&fl,            /* 2nd FDB - local filespec */
  5601.            _CMFLD,        /* fcode */
  5602.            "",            /* hlpmsg */
  5603.            "",            /* default */
  5604.            "",            /* addtl string data */
  5605.            0,            /* addtl numeric data 1 */
  5606.            0,            /* addtl numeric data 2 */
  5607.            xxstring,
  5608.            NULL,
  5609.            &cm
  5610.            );
  5611.         cmfdbi(&cm,            /* 3rd FDB - Confirmation */
  5612.            _CMCFM,        /* fcode */
  5613.            "",
  5614.            "",
  5615.            "",
  5616.            0,
  5617.            0,
  5618.            NULL,
  5619.            NULL,
  5620.            NULL
  5621.            );
  5622.  
  5623.             while (!confirmed) {    /* Get more filenames */
  5624.         x = cmfdb(&sf);        /* Parse something */
  5625.         debug(F101,"ftp put cmfdb B","",x);
  5626.         debug(F101,"ftp put fcode B","",cmresult.fcode);
  5627.         if (x < 0)        /* Error */
  5628.           goto xputx;        /* or reparse needed */
  5629.         switch (cmresult.fcode) {
  5630.           case _CMCFM:        /* End of command */
  5631.             confirmed++;
  5632.             if (nfils < 1) {
  5633.             debug(F100,"ftp put mput no files match","",0);
  5634.             printf("?No files match MPUT list\n");
  5635.             x = -9;
  5636.             goto xputx;
  5637.             }
  5638.             break;
  5639.           case _CMFLD:        /* No match */
  5640.             debug(F110,"ftp put mput skipping",cmresult.sresult,0);
  5641.             continue;
  5642.           case _CMIFI:        /* Good match */
  5643.             s = cmresult.sresult;
  5644.             msfiles[nfils++] = lp; /* Got one, count, point to it, */
  5645.             p = lp;           /* remember pointer, */
  5646.             while ((*lp++ = *s++)) /* and copy it into buffer */
  5647.               if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  5648.               printf("?MPUT list too long\n");
  5649.               line[0] = NUL;
  5650.               x = -9;
  5651.               goto xputx;
  5652.               }
  5653.             debug(F111,"ftp put mput adding",msfiles[nfils-1],nfils-1);
  5654.             if (nfils == 1)    /* Take care of \v(filespec) */
  5655.               fspec[0] = NUL;
  5656. #ifdef ZFNQFP
  5657.             zfnqfp(p,TMPBUFSIZ,tmpbuf);
  5658.             p = tmpbuf;
  5659. #endif /* ZFNQFP */
  5660.             if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  5661.             strcat(fspec,p);    /* safe */
  5662.             strcat(fspec," ");  /* safe */
  5663.             } else {
  5664. #ifdef COMMENT
  5665.             printf("WARNING - \\v(filespec) buffer overflow\n");
  5666. #else
  5667.             debug(F101,"doxput filespec buffer overflow","",0);
  5668. #endif /* COMMENT */
  5669.             }
  5670.         }
  5671.         }
  5672. #endif /* NOMSEND */
  5673.         } else {                        /* Regular PUT */
  5674.             nfils = -1;
  5675.             if ((x = cmtxt(wild ?
  5676. "\nOptional as-name template containing replacement variables \
  5677. like \\v(filename)" :
  5678.                            "Optional name to send it with",
  5679.                            "",&p,NULL)) < 0)
  5680.               goto xputx;
  5681.  
  5682.             if (p) if (!*p) p = NULL;
  5683.             p = brstrip(p);
  5684.  
  5685.             if (p && *p) {
  5686.                 makestr(&(pv[SND_ASN].sval),p);
  5687.                 if (pv[SND_ASN].sval)
  5688.                   pv[SND_ASN].ival = 1;
  5689.                 debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  5690.             }
  5691.         }
  5692.     }
  5693.     /* Set cmarg2 from as-name, however we got it. */
  5694.  
  5695.     CHECKCONN();
  5696.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  5697.         char * p;
  5698.         p = brstrip(pv[SND_ASN].sval);
  5699.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  5700.     }
  5701.     debug(F110,"ftp put asnambuf",asnambuf,0);
  5702.  
  5703.     if (pv[SND_FIL].ival > 0) {
  5704.         if (confirmed) {
  5705.             if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  5706.                 debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  5707.                 printf("?Failure to open %s\n",pv[SND_FIL].sval);
  5708.                 x = -9;
  5709.                 goto xputx;
  5710.             }
  5711.             makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  5712.             debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  5713.             wild = 1;
  5714.         }
  5715.     }
  5716.     if (confirmed && !line[0] && !filefile) {
  5717. #ifndef NOMSEND
  5718.         if (filehead) {                 /* OK if we have a SEND-LIST */
  5719.             nfils = filesinlist;
  5720.             sndsrc = nfils;             /* Like MSEND */
  5721.             addlist = 1;                /* But using a different list... */
  5722.             filenext = filehead;
  5723.             goto doput;
  5724.         }
  5725. #endif /* NOMSEND */
  5726.         printf("?Filename required but not given\n");
  5727.         x = -9;
  5728.         goto xputx;
  5729.     }
  5730. #ifndef NOMSEND
  5731.     addlist = 0;                        /* Don't use SEND-LIST. */
  5732. #endif /* NOMSEND */
  5733.  
  5734.     if (mput) {                         /* MPUT (rather than PUT) */
  5735. #ifndef NOMSEND
  5736.         cmlist = msfiles;               /* List of filespecs */
  5737.         sndsrc = nfils;                 /* rather filespec and as-name */
  5738. #endif /* NOMSEND */
  5739.         pipesend = 0;
  5740.     } else if (filefile) {              /* File contains list of filenames */
  5741.         s = "";
  5742.         cmarg = "";
  5743.         line[0] = NUL;
  5744.         nfils = 1;
  5745.         sndsrc = 1;
  5746.  
  5747.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5748.  
  5749.         /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5750.         nfils = sndsrc = -1;
  5751.         if (!wild) {
  5752.             if (zchki(s) < 0) {
  5753.                 printf("?Read access denied - \"%s\"\n", s);
  5754.                 x = -9;
  5755.                 goto xputx;
  5756.             }
  5757.         }
  5758.         if (s != line)                  /* We might already have done this. */
  5759.           ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
  5760. #ifdef DEBUG
  5761.         else
  5762.           debug(F110,"doxput line=s",line,0);
  5763. #endif /* DEBUG */
  5764.         cmarg = line;                   /* File to send */
  5765.     }
  5766. #ifndef NOMSEND
  5767.     zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
  5768. #endif /* NOMSEND */
  5769.  
  5770.     if (!mput) {                        /* For all but MPUT... */
  5771. #ifdef PIPESEND
  5772.         if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
  5773.           pipesend = 1;
  5774.         debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5775.         if (pipesend && filefile) {
  5776.             printf("?Invalid switch combination\n");
  5777.             x = -9;
  5778.             goto xputx;
  5779.         }
  5780. #endif /* PIPESEND */
  5781.  
  5782. #ifndef NOSPL
  5783.     /* If as-name given and filespec is wild, as-name must contain variables */
  5784.         if ((wild || mput) && asnambuf[0]) {
  5785.             char * s = tmpbuf;
  5786.             x = TMPBUFSIZ;
  5787.             zzstring(asnambuf,&s,&x);
  5788.             if (!strcmp(tmpbuf,asnambuf)) {
  5789.                 printf(
  5790.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5791.                        );
  5792.                 x = -9;
  5793.                 goto xputx;
  5794.             }
  5795.         }
  5796. #endif /* NOSPL */
  5797.     }
  5798.  
  5799.   doput:
  5800.  
  5801.     if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
  5802.         fdispla = 0;
  5803.         debug(F101,"ftp put display","",fdispla);
  5804.     } else {
  5805.         displa = 1;
  5806.         if (ftp_deb)
  5807.       fdispla = XYFD_B;
  5808.     }
  5809.  
  5810. #ifdef PUTARRAY                         /* SEND /ARRAY... */
  5811.     if (pv[SND_ARR].ival > 0) {
  5812.         if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5813.         if (range[0] == -1)             /* If low end of range not specified */
  5814.           range[0] = 1;                 /* default to 1 */
  5815.         if (range[1] == -1)             /* If high not specified */
  5816.           range[1] = a_dim[arrayx];     /* default to size of array */
  5817.         if ((range[0] < 0) ||           /* Check range */
  5818.             (range[0] > a_dim[arrayx]) ||
  5819.             (range[1] < range[0]) ||
  5820.             (range[1] > a_dim[arrayx])) {
  5821.             printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5822.             x = -9;
  5823.             goto xputx;
  5824.         }
  5825.         sndarray = ap;                  /* Array pointer */
  5826.         sndxin = arrayx;                /* Array index */
  5827.         sndxlo = range[0];              /* Array range */
  5828.         sndxhi = range[1];
  5829.         sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5830.         if (!asnambuf[0])
  5831.           ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5832.         cmarg = "";
  5833.     }
  5834. #endif /* PUTARRAY */
  5835.  
  5836.     moving = 0;
  5837.  
  5838.     if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
  5839.         if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
  5840.           moving = 1;
  5841.         if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
  5842.           ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5843.         if (pv[SND_BEF].ival > 0)
  5844.           ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5845.         if (pv[SND_NAF].ival > 0)
  5846.           ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5847.         if (pv[SND_NBE].ival > 0)
  5848.           ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5849.         if (pv[SND_EXC].ival > 0)
  5850.           makelist(pv[SND_EXC].sval,sndexcept,NSNDEXCEPT);
  5851.         if (pv[SND_SMA].ival > -1)
  5852.           sndsmaller = pv[SND_SMA].wval;
  5853.         if (pv[SND_LAR].ival > -1)
  5854.           sndlarger = pv[SND_LAR].wval;
  5855.         if (pv[SND_NAM].ival > -1)
  5856.           x_cnv = pv[SND_NAM].ival;
  5857.         if (pv[SND_USN].ival > -1)
  5858.           x_usn = pv[SND_USN].ival;
  5859.         if (pv[SND_ERR].ival > -1)
  5860.           puterror = pv[SND_ERR].ival;
  5861.  
  5862. #ifdef DOUPDATE
  5863.         if (pv[SND_UPD].ival > 0) {
  5864.             if (x_usn) {
  5865.                 printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5866.                 x = -9;
  5867.                 goto xputx;
  5868.             }
  5869.             putflags |= PUT_UPD;
  5870.         ftp_dates |= 2;
  5871.         }
  5872. #ifdef COMMENT
  5873.     /* This works but it's useless, maybe dangerous */
  5874.         if (pv[SND_DIF].ival > 0) {
  5875.             if (x_usn) {
  5876.                 printf("?Conflicting switches: /DATES-DIFFER /UNIQUE\n");
  5877.                 x = -9;
  5878.                 goto xputx;
  5879.             }
  5880.             putflags |= PUT_DIF;
  5881.         ftp_dates |= 2;
  5882.         }
  5883. #endif /* COMMENT */
  5884. #endif /* DOUPDATE */
  5885.  
  5886.         if (pv[SND_SIM].ival > 0)
  5887.           putflags |= PUT_SIM;
  5888.  
  5889.         if (pv[SND_PRM].ival > -1) {
  5890. #ifdef UNIX
  5891.             if (x_usn) {
  5892.                 printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5893.                 x = -9;
  5894.                 goto xputx;
  5895.             }
  5896.             x_prm = pv[SND_PRM].ival;
  5897. #else /* UNIX */
  5898.             printf("?/PERMISSIONS switch is not supported\n");
  5899. #endif /* UNIX */
  5900.         }
  5901. #ifdef FTP_RESTART
  5902.         if (pv[SND_RES].ival > 0) {
  5903.         if (!sizeok) {
  5904.         printf("?PUT /RESTART can't be used because SIZE disabled.\n");
  5905.                 x = -9;
  5906.                 goto xputx;
  5907.         }
  5908.             if (x_usn || putflags) {
  5909.                 printf("?Conflicting switches: /RECOVER %s\n",
  5910.                        x_usn && putflags ? "/UNIQUE /UPDATE" :
  5911.                        (x_usn ? "/UNIQUE" : "/UPDATE")
  5912.                        );
  5913.                 x = -9;
  5914.                 goto xputx;
  5915.             }
  5916. #ifndef NOCSETS
  5917.             if (x_xla &&
  5918.                 (x_csl == FC_UCS2 ||
  5919.                  x_csl == FC_UTF8 ||
  5920.                  x_csr == FC_UCS2 ||
  5921.                  x_csr == FC_UTF8)) {
  5922.                 printf("?/RECOVER can not be used with Unicode translation\n");
  5923.                 x = -9;
  5924.                 goto xputx;
  5925.             }
  5926. #endif /* NOCSETS */
  5927.             putflags = PUT_RES;
  5928.         }
  5929. #endif /* FTP_RESTART */
  5930.     }
  5931.     debug(F101,"ftp PUT restart","",putflags & PUT_RES);
  5932.     debug(F101,"ftp PUT update","",putflags & PUT_UPD);
  5933.  
  5934. #ifdef PIPESEND
  5935.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  5936.         if (!pv[SND_FLT].sval) {
  5937.             sndfilter = NULL;
  5938.         } else {
  5939.             sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5940.             if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5941.         }
  5942.         debug(F110,"ftp put /FILTER", sndfilter, 0);
  5943.     }
  5944.     if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
  5945.       if (putflags)                     /* with pipes or filters */
  5946.         putflags = 0;
  5947. #endif /* PIPESEND */
  5948.  
  5949.     tfc = (CK_OFF_T)0;            /* Initialize stats and counters */
  5950.     filcnt = 0;
  5951.     pktnum = 0;
  5952.     spackets = 0L;
  5953.  
  5954.     if (wild)                           /* (is this necessary?) */
  5955.       cx = FTP_MPU;
  5956.  
  5957.     t0 = gmstimer();                    /* Record starting time */
  5958.  
  5959.     done = 0;                           /* Loop control */
  5960.     cancelgroup = 0;
  5961.  
  5962.     cdlevel = 0;
  5963.     cdsimlvl = 0;
  5964.     while (!done && !cancelgroup) {     /* Loop for all files */
  5965.                                         /* or until canceled. */
  5966. #ifdef FTP_PROXY
  5967.         /*
  5968.            If we are using a proxy, we don't use the local file list;
  5969.            instead we use the list on the remote machine which we want
  5970.            sent to someone else, and we use remglob() to get the names.
  5971.            But in that case we shouldn't even be executing this routine;
  5972.            see ftp_mput().
  5973.         */
  5974. #endif /* FTP_PROXY */
  5975.  
  5976.         cancelfile = 0;
  5977.         x = gnfile();                   /* Get next file from list(s) */
  5978.  
  5979.         if (x == 0)                     /* (see gnfile() comments...) */
  5980.           x = gnferror;
  5981.         debug(F111,"FTP PUT gnfile",filnam,x);
  5982.         debug(F111,"FTP PUT binary",filnam,binary);
  5983.  
  5984.         switch (x) {
  5985.           case 1:                       /* File to send */
  5986.             s2 = asnambuf;
  5987. #ifndef NOSPL
  5988.             if (asnambuf[0]) {          /* As-name */
  5989.                 int n; char *p;         /* to be evaluated... */
  5990.                 n = TMPBUFSIZ;
  5991.                 p = tmpbuf;
  5992.                 zzstring(asnambuf,&p,&n);
  5993.                 s2 = tmpbuf;
  5994.                 debug(F110,"ftp put asname",s2,0);
  5995.             }
  5996. #endif /* NOSPL */
  5997.             rc = putfile(cx,            /* Function (PUT, APPEND) */
  5998.                     filnam, s2,         /* Name to send, as-name */
  5999.                     forcetype, moving,  /* Parameters from switches... */
  6000.                     snd_move, snd_rename, srv_renam,
  6001.                     x_cnv, x_usn, xfiletype, x_prm,
  6002. #ifndef NOCSETS
  6003.                     x_csl, (!x_xla ? -1 : x_csr),
  6004. #else
  6005.                     -1, -1,
  6006. #endif /* NOCSETS */
  6007.                     putflags
  6008.                     );
  6009.             debug(F111,"ftp put putfile rc",filnam,rc);
  6010.             debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  6011.             debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  6012.             if (rc > -1) {
  6013.                 good++;
  6014.                 status = 1;
  6015.             }
  6016.             if (cancelfile)
  6017.               continue;
  6018.             if (rc < 0) {
  6019.                 ftp_fai++;
  6020.                 if (puterror) {
  6021.                     status = 0;
  6022.                     printf("?Fatal upload error: %s\n",filnam);
  6023.                     done++;
  6024.                 }
  6025.             }
  6026.             continue;
  6027.           case 0:                       /* No more files, done */
  6028.             done++;
  6029.             continue;
  6030.           case -1:
  6031.             printf("?%s: file not found - \"%s\"\n",
  6032.                    puterror ? "Fatal" : "Warning",
  6033.                    filnam
  6034.                    );
  6035.             if (puterror) {
  6036.                 status = 0;
  6037.                 done++;
  6038.                 break;
  6039.             }
  6040.             continue;
  6041.           case -2:
  6042.             if (puterror) {
  6043.                 printf("?Fatal: file not found - \"%s\"\n", filnam);
  6044.                 status = 0;
  6045.                 done++;
  6046.                 break;
  6047.             }
  6048.             continue;                   /* Not readable, keep going */
  6049.           case -3:
  6050.             if (puterror) {
  6051.                 printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  6052.                 status = 0;
  6053.                 done++;
  6054.                 break;
  6055.             }
  6056.             printf("?Warning access denied - \"%s\"\n", filnam);
  6057.             continue;
  6058. #ifdef COMMENT
  6059.           case -4:                      /* Canceled */
  6060.             done++;
  6061.             break;
  6062. #endif /* COMMENT */
  6063.           case -5:
  6064.             printf("?Too many files match\n");
  6065.             done++;
  6066.             break;
  6067.           case -6:
  6068.             if (good < 1)
  6069.               printf("?No files selected\n");
  6070.             done++;
  6071.             break;
  6072.           default:
  6073.             printf("?getnextfile() - unknown failure\n");
  6074.             done++;
  6075.         }
  6076.     }
  6077.     if (cdlevel > 0) {
  6078.         while (cdlevel--) {
  6079.             if (cdsimlvl) {
  6080.                 cdsimlvl--;
  6081.             } else if (!doftpcdup())
  6082.               break;
  6083.         }
  6084.     }
  6085.     if (status > 0) {
  6086.         if (cancelgroup)
  6087.           status = 0;
  6088.         else if (cancelfile && good < 1)
  6089.           status = 0;
  6090.     }
  6091.     success = status;
  6092.     x = success;
  6093.  
  6094.   xputx:
  6095.     if (x > -1) {
  6096. #ifdef GFTIMER
  6097.         t1 = gmstimer();                /* End time */
  6098.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6099.         if (!sec) sec = 0.001;
  6100.         fptsecs = sec;
  6101. #else
  6102.         sec = (t1 - t0) / 1000;
  6103.         if (!sec) sec = 1;
  6104. #endif /* GFTIMER */
  6105.         tfcps = (long) (tfc / sec);
  6106.         tsecs = (int)sec;
  6107.         lastxfer = W_FTP|W_SEND;
  6108.         xferstat = success;
  6109.         if (dpyactive)
  6110.       ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
  6111.     }
  6112.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  6113.         if (pv[i].sval)
  6114.           free(pv[i].sval);
  6115.     }
  6116.     ftreset();                          /* Undo switch effects */
  6117.     dpyactive = 0;
  6118.     return(x);
  6119. }
  6120.  
  6121.  
  6122. static char ** mgetlist = NULL;         /* For MGET */
  6123. static int mgetn = 0, mgetx = 0;
  6124. static char xtmpbuf[4096];
  6125.  
  6126. /*
  6127.   c m d l i n g e t
  6128.  
  6129.   Get files specified by -g command-line option.
  6130.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  6131. */
  6132. int
  6133. cmdlinget(stay) int stay; {
  6134.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  6135.     int lcs = -1, rcs = -1, xlate = 0;
  6136.     int first = 1;
  6137.     int mget = 1;
  6138.     int nc;
  6139.     char * s, * s2, * s3;
  6140.     ULONG t0, t1;                       /* Times for stats */
  6141. #ifdef GFTIMER
  6142.     CKFLOAT sec;
  6143. #else
  6144.     int sec = 0;
  6145. #endif /* GFTIMER */
  6146.  
  6147.     if (quiet) {                        /* -q really means quiet */
  6148.         displa = 0;
  6149.         fdispla = 0;
  6150.     } else {
  6151.         displa = 1;
  6152.         fdispla = XYFD_B;
  6153.     }
  6154.     testing = 0;
  6155.     dpyactive = 0;
  6156.     out2screen = 0;
  6157.     what = W_FTP|W_RECV;
  6158.     mgetmethod = 0;
  6159.     mgetforced = 0;
  6160.  
  6161.     havetype = 0;
  6162.     havesize = (CK_OFF_T)-1;
  6163.     makestr(&havemdtm,NULL);
  6164.  
  6165.     if (ftp_fnc < 0)
  6166.       ftp_fnc = fncact;
  6167.  
  6168. #ifndef NOSPL
  6169.     cmd_quoting = 0;
  6170. #endif /* NOSPL */
  6171.     debug(F101,"ftp cmdlinget nfils","",nfils);
  6172.  
  6173.     if (ftp_cnv == CNV_AUTO) {          /* Name conversion is auto */
  6174.         if (alike) {                    /* If server & client are alike */
  6175.             nc = 0;                     /* no conversion */
  6176.         } else {                        /* If they are different */
  6177.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6178.               nc = -1;                  /* only minimal conversions needed */
  6179.             else                        /* otherwise */
  6180.               nc = 1;                   /* full conversion */
  6181.         }
  6182.     } else                              /* Not auto - do what user said */
  6183.       nc = ftp_cnv;
  6184.  
  6185.     if (nfils < 1)
  6186.       doexit(BAD_EXIT,-1);
  6187.  
  6188.     t0 = gmstimer();                    /* Starting time for this batch */
  6189.  
  6190. #ifndef NOCSETS
  6191.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  6192.         lcs = ftp_csl;                  /* Local charset */
  6193.         if (lcs < 0) lcs = fcharset;
  6194.         if (lcs < 0) xlate = 0;
  6195.     }
  6196.     if (xlate) {                        /* Still ON? */
  6197.         rcs = ftp_csx;                  /* Remote (Server) charset */
  6198.         if (rcs < 0) rcs = ftp_csr;
  6199.         if (rcs < 0) xlate = 0;
  6200.     }
  6201. #endif /* NOCSETS */
  6202.     /*
  6203.       If we have only one file and it is a directory, then we ask for a
  6204.       listing of its contents, rather than retrieving the directory file
  6205.       itself.  This is what (e.g.) Netscape does.
  6206.     */
  6207.     if (nfils == 1) {
  6208.         if (doftpcwd((char *)cmlist[mgetx],-1)) {
  6209.             /* If we can CD to it, it must be a directory */
  6210.             if (recursive) {
  6211.                 cmlist[mgetx] = "*";
  6212.             } else {
  6213.                 status =
  6214.                   (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  6215.                 done = 1;
  6216.             }
  6217.         }
  6218.     }
  6219. /*
  6220.   The following is to work around UNIX servers which, when given a command
  6221.   like "NLST path/blah" (not wild) returns the basename without the path.
  6222. */
  6223.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  6224.         mget = iswild(cmlist[mgetx]);
  6225.     }
  6226.     if (!mget && !done) {               /* Invoked by command-line FTP URL */
  6227.         if (ftp_deb)
  6228.           printf("DOING GET...\n");
  6229.         done++;
  6230.         cancelfile = 0;                 /* This file not canceled yet */
  6231.         s = cmlist[mgetx];
  6232.         rc = 0;                         /* Initial return code */
  6233.     fsize = (CK_OFF_T)-1;
  6234.     if (sizeok) {
  6235.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  6236.         if (x == REPLY_COMPLETE)
  6237.           fsize = ckatofs(&ftp_reply_str[4]);
  6238.     }
  6239.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6240.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  6241.  
  6242.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  6243.         s2 = tmpbuf;
  6244.  
  6245.         /* If local file already exists, take collision action */
  6246.  
  6247.         if (zchki(s2) > -1) {
  6248.             switch (ftp_fnc) {
  6249.               case XYFX_A:              /* Append */
  6250.                 append = 1;
  6251.                 break;
  6252.               case XYFX_R:              /* Rename */
  6253.               case XYFX_B: {            /* Backup */
  6254.                   char * p = NULL;
  6255.                   int x = -1;
  6256.                   znewn(s2,&p);         /* Make unique name */
  6257.                   debug(F110,"ftp cmdlinget znewn",p,0);
  6258.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  6259.                       x = zrename(s2,p);
  6260.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  6261.                   } else {              /* Rename incoming file */
  6262.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6263.                       s2 = tmpbuf;
  6264.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  6265.                   }
  6266.                   if (x < 0) {
  6267.                       printf("?Backup/Rename failed\n");
  6268.                       return(success = 0);
  6269.                   }
  6270.                   break;
  6271.               }
  6272.               case XYFX_D:              /* Discard */
  6273.                 ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  6274.                 ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
  6275.                 tlog(F100," refused: name","",0);
  6276.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  6277.                 goto xclget;
  6278.  
  6279.               case XYFX_X:              /* Overwrite */
  6280.               case XYFX_U:              /* Update (already handled above) */
  6281.           case XYFX_M:        /* ditto */
  6282.                 break;
  6283.             }
  6284.         }
  6285.         rc = getfile(s,                 /* Remote name */
  6286.                      s2,                /* Local name */
  6287.                      0,                 /* Recover/Restart */
  6288.                      append,            /* Append */
  6289.                      NULL,              /* Pipename */
  6290.                      0,                 /* Translate charsets */
  6291.                      -1,                /* File charset (none) */
  6292.                      -1                 /* Server charset (none) */
  6293.                      );
  6294.         debug(F111,"ftp cmdlinget rc",s,rc);
  6295.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  6296.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  6297.  
  6298.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  6299.             rc = getfile(&s[1],         /* Remote name without leading '/' */
  6300.                          s2,            /* Local name */
  6301.                          0,             /* Recover/Restart */
  6302.                          append,        /* Append */
  6303.                          NULL,          /* Pipename */
  6304.                          0,             /* Translate charsets */
  6305.                          -1,            /* File charset (none) */
  6306.                          -1             /* Server charset (none) */
  6307.                          );
  6308.         if (rc > -1) {
  6309.             good++;
  6310.             status = 1;
  6311.         }
  6312.         if (cancelfile)
  6313.           goto xclget;
  6314.         if (rc < 0) {
  6315.             ftp_fai++;
  6316. #ifdef FTP_TIMEOUT
  6317.         if (ftp_timed_out)
  6318.           status = 0;
  6319. #endif    /* FTP_TIMEOUT */
  6320.             if (geterror) {
  6321.                 status = 0;
  6322.                 done++;
  6323.             }
  6324.         }
  6325.     }
  6326.     if (ftp_deb && !done)
  6327.       printf("DOING MGET...\n");
  6328.     while (!done && !cancelgroup) {
  6329.         cancelfile = 0;                 /* This file not canceled yet */
  6330.         s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  6331.         if (!s) s = "";
  6332.         if (!*s) {
  6333.             first = 1;
  6334.             mgetx++;
  6335.             if (mgetx < nfils)
  6336.               s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  6337.             else
  6338.               s = NULL;
  6339.             debug(F111,"ftp cmdlinget remote_files B",s,0);
  6340.             if (!s) {
  6341.                 done = 1;
  6342.                 break;
  6343.             }
  6344.         }
  6345.         /*
  6346.           The semantics of NLST are ill-defined.  Suppose we have just sent
  6347.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6348.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  6349.           RETR commands based on the pathless names are not going to work.
  6350.         */
  6351.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6352.             if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  6353.                 int len, left = 4096;
  6354.                 char * tmp = xtmpbuf;
  6355.                 len = s3 - cmlist[mgetx] + 1;
  6356.                 ckstrncpy(tmp,cmlist[mgetx],left);
  6357.                 tmp += len;
  6358.                 left -= len;
  6359.                 ckstrncpy(tmp,s,left);
  6360.                 s = xtmpbuf;
  6361.         debug(F111,"ftp cmdlinget remote_files X",s,0);
  6362.             }
  6363.         }
  6364.         first = 0;                      /* Not first any more */
  6365.  
  6366.     debug(F111,"ftp cmdlinget havetype",s,havetype);
  6367.     if (havetype > 0 && havetype != FTYP_FILE) { /* Server says not file */
  6368.         debug(F110,"ftp cmdlinget not-a-file",s,0);
  6369.         continue;
  6370.     }
  6371.         rc = 0;                         /* Initial return code */
  6372.     if (havesize > (CK_OFF_T)-1) {    /* Already have file size? */
  6373.         fsize = havesize;
  6374.     } else {            /* No - must ask server */
  6375.         /*
  6376.           Prior to sending the NLST command we necessarily put the
  6377.           server into ASCII mode.  We must now put it back into the
  6378.           the requested mode so the upcoming SIZE command returns
  6379.           right kind of size; this is especially important for
  6380.           GET /RECOVER; otherwise the server returns the "ASCII" size
  6381.           of the file, rather than its true size.
  6382.         */
  6383.         changetype(ftp_typ,0);    /* Change to requested type */
  6384.         fsize = (CK_OFF_T)-1;
  6385.         if (sizeok) {
  6386.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm);
  6387.         if (x == REPLY_COMPLETE)
  6388.           fsize = ckatofs(&ftp_reply_str[4]);
  6389.         }
  6390.     }
  6391.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6392.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  6393.  
  6394.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  6395.         s2 = tmpbuf;
  6396.  
  6397.         /* If local file already exists, take collision action */
  6398.  
  6399.         if (zchki(s2) > -1) {
  6400.             switch (ftp_fnc) {
  6401.               case XYFX_A:              /* Append */
  6402.                 append = 1;
  6403.                 break;
  6404.               case XYFX_R:              /* Rename */
  6405.               case XYFX_B: {            /* Backup */
  6406.                   char * p = NULL;
  6407.                   int x = -1;
  6408.                   znewn(s2,&p);         /* Make unique name */
  6409.                   debug(F110,"ftp cmdlinget znewn",p,0);
  6410.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  6411.                       x = zrename(s2,p);
  6412.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  6413.                   } else {              /* Rename incoming file */
  6414.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6415.                       s2 = tmpbuf;
  6416.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  6417.                   }
  6418.                   if (x < 0) {
  6419.                       printf("?Backup/Rename failed\n");
  6420.                       return(success = 0);
  6421.                   }
  6422.                   break;
  6423.               }
  6424.               case XYFX_D:      /* Discard */
  6425.                 ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  6426.                 ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
  6427.                 tlog(F100," refused: name","",0);
  6428.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  6429.                 continue;
  6430.               case XYFX_X:              /* Overwrite */
  6431.               case XYFX_U:              /* Update (already handled above) */
  6432.               case XYFX_M:              /* ditto */
  6433.                 break;
  6434.             }
  6435.         }
  6436.                                         /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  6437.         rc = getfile(s,                 /* Remote name */
  6438.                      s2,                /* Local name */
  6439.                      0,                 /* Recover/Restart */
  6440.                      append,            /* Append */
  6441.                      NULL,              /* Pipename */
  6442.                      0,                 /* Translate charsets */
  6443.                      -1,                /* File charset (none) */
  6444.                      -1                 /* Server charset (none) */
  6445.                      );
  6446.         debug(F111,"ftp cmdlinget rc",s,rc);
  6447.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  6448.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  6449.  
  6450.         if (rc > -1) {
  6451.             good++;
  6452.             status = 1;
  6453.         }
  6454.         if (cancelfile)
  6455.           continue;
  6456.         if (rc < 0) {
  6457.             ftp_fai++;
  6458. #ifdef FTP_TIMEOUT
  6459.         if (ftp_timed_out)
  6460.           status = 0;
  6461. #endif    /* FTP_TIMEOUT */
  6462.             if (geterror) {
  6463.                 status = 0;
  6464.                 done++;
  6465.             }
  6466.         }
  6467.     }
  6468.  
  6469.   xclget:
  6470.     if (cancelgroup)
  6471.       mlsreset();
  6472.     if (status > 0) {
  6473.         if (cancelgroup)
  6474.           status = 0;
  6475.         else if (cancelfile && good < 1)
  6476.           status = 0;
  6477.     }
  6478.     success = status;
  6479.  
  6480. #ifdef GFTIMER
  6481.     t1 = gmstimer();                    /* End time */
  6482.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6483.     if (!sec) sec = 0.001;
  6484.     fptsecs = sec;
  6485. #else
  6486.     sec = (t1 - t0) / 1000;
  6487.     if (!sec) sec = 1;
  6488. #endif /* GFTIMER */
  6489.  
  6490.     tfcps = (long) (tfc / sec);
  6491.     tsecs = (int)sec;
  6492.     lastxfer = W_FTP|W_RECV;
  6493.     xferstat = success;
  6494.     if (dpyactive)
  6495.       ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
  6496.     if (!stay)
  6497.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  6498.     return(success);
  6499. }
  6500.  
  6501. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  6502.  
  6503. /*
  6504.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  6505.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  6506.   the user-interface perspective, since the server's directory listings show
  6507.   its own local times and since we don't know what timezone it's in, there's
  6508.   no way to reconcile our local times with the server's.
  6509. */
  6510. int
  6511. doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  6512.     struct FDB fl, sw, cm;
  6513.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  6514.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  6515.     int x_fnc = 0, first = 0, skipthis = 0, append = 0, selected = 0;
  6516.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  6517.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  6518.     int gotsize = 0;
  6519.     int matchdot = 0;
  6520.     CK_OFF_T getlarger = (CK_OFF_T)-1;
  6521.     CK_OFF_T getsmaller = (CK_OFF_T)-1;
  6522.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  6523.     char * src = "", * local = "";
  6524.     char * pat = "";
  6525.  
  6526.     int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
  6527.     int x_xla = 0;
  6528.     char c;                             /* Worker char */
  6529.     ULONG t0 = 0L, t1;                  /* Times for stats */
  6530. #ifdef GFTIMER
  6531.     CKFLOAT sec;
  6532. #else
  6533.     int sec = 0;
  6534. #endif /* GFTIMER */
  6535.  
  6536.     struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
  6537.  
  6538.     success = 0;                        /* Assume failure */
  6539.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  6540.     restart = 0;                        /* No restart yet */
  6541.     out2screen = 0;            /* No TO-SCREEN switch given yet */
  6542.     mgetmethod = 0;            /* No NLST or MLSD switch yet */
  6543.     mgetforced = 0;
  6544.  
  6545.     g_displa = fdispla;
  6546.     if (ftp_dis > -1)
  6547.       fdispla = ftp_dis;
  6548.  
  6549.     x_cnv = ftp_cnv;                    /* Filename conversion */
  6550.     if (x_cnv == CNV_AUTO) {        /* Name conversion is auto */
  6551.         if (alike) {                    /* If server & client are alike */
  6552.             x_cnv = 0;            /* no conversion */
  6553.         } else {                        /* If they are different */
  6554.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6555.               x_cnv = -1;        /* only minimal conversions needed */
  6556.             else                        /* otherwise */
  6557.               x_cnv = 1;        /* full conversion */
  6558.         }
  6559.     } else                              /* Not auto - do what user said */
  6560.       x_cnv = ftp_cnv;
  6561.  
  6562.     x_prm = ftp_prm;                    /* Permissions */
  6563.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  6564.       x_prm = alike;
  6565.  
  6566. #ifndef NOCSETS
  6567.     x_csr = ftp_csr;                    /* Inherit global server charset */
  6568.     x_csl = ftp_csl;                    /* Inherit global local charset */
  6569.     if (x_csl < 0)                      /* If none, use current */
  6570.       x_csl = fcharset;                 /* file character-set. */
  6571.     x_xla = ftp_xla;                    /* Translation On/Off */
  6572. #endif /* NOCSETS */
  6573.  
  6574.     geterror = ftp_err;                 /* Inherit global error action. */
  6575.     asnambuf[0] = NUL;                  /* No as-name yet. */
  6576.     pipesave = pipesend;
  6577.     pipesend = 0;
  6578.  
  6579.     havetype = 0;
  6580.     havesize = (CK_OFF_T)-1;
  6581.     makestr(&havemdtm,NULL);
  6582.  
  6583.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  6584.         ftp_typ = g_ftp_typ;
  6585.         /* g_ftp_typ = -1; */
  6586.     }
  6587.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  6588.         pv[i].sval = NULL;              /* to null pointers */
  6589.         pv[i].ival = -1;                /* and -1 int values */
  6590.         pv[i].wval = (CK_OFF_T)-1;    /* and -1 wide values */
  6591.     }
  6592.     zclose(ZMFILE);                     /* In case it was left open */
  6593.  
  6594.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  6595.  
  6596.     if (fp_nml) {                       /* Reset /NAMELIST */
  6597.         if (fp_nml != stdout)
  6598.           fclose(fp_nml);
  6599.         fp_nml = NULL;
  6600.     }
  6601.     makestr(&ftp_nml,NULL);
  6602.  
  6603.     /* Initialize list of remote filespecs */
  6604.  
  6605.     if (!mgetlist) {
  6606.         mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  6607.         if (!mgetlist) {
  6608.             printf("?Memory allocation failure - MGET list\n");
  6609.             return(-9);
  6610.         }
  6611.         for (i = 0; i < MGETMAX; i++)
  6612.           mgetlist[i] = NULL;
  6613.     }
  6614.     mgetn = 0;                          /* Number of mget arguments */
  6615.     mgetx = 0;                          /* Current arg */
  6616.  
  6617.     if (who == 0) {                     /* Called with unprefixed command */
  6618.         if (cx == XXGET || cx == XXREGET || cx == XXRETR)
  6619.           getone++;
  6620.         switch (cx) {
  6621.           case XXREGET: pv[SND_RES].ival = 1; break;
  6622.           case XXRETR:  pv[SND_DEL].ival = 1; break;
  6623.           case XXGET:
  6624.           case XXMGET:  mget++; break;
  6625.         }
  6626.     } else {                            /* FTP command */
  6627.         if (cx == FTP_GET || cx == FTP_RGE)
  6628.           getone++;
  6629.         switch (cx) {
  6630.           case FTP_DEL:                 /* (fall thru on purpose) */
  6631.           case FTP_MDE: mdel++;         /* (ditto) */
  6632.           case FTP_GET:                 /* (ditto) */
  6633.           case FTP_MGE: mget++; break;
  6634.           case FTP_RGE: pv[SND_RES].ival = 1; break;
  6635.         }
  6636.     }
  6637.     cmfdbi(&sw,                         /* First FDB - command switches */
  6638.            _CMKEY,                      /* fcode */
  6639.            "Remote filename;\n or switch", /* hlpmsg */
  6640.            "",                          /* default */
  6641.            "",                          /* addtl string data */
  6642.            mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  6643.            4,                           /* addtl numeric data 2: 4 = cmswi */
  6644.            xxstring,                    /* Processing function */
  6645.            mdel ? delswi : getswi,      /* Keyword table */
  6646.            &fl                          /* Pointer to next FDB */
  6647.            );
  6648.     cmfdbi(&fl,                         /* 2nd FDB - remote filename */
  6649.            _CMFLD,                      /* fcode */
  6650.            "",                          /* hlpmsg */
  6651.            "",                          /* default */
  6652.            "",                          /* addtl string data */
  6653.            0,                           /* addtl numeric data 1 */
  6654.            0,                           /* addtl numeric data 2 */
  6655.            xxstring,
  6656.            NULL,
  6657.            &cm
  6658.            );
  6659.     cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
  6660.            _CMCFM,                      /* fcode */
  6661.            "",                          /* hlpmsg */
  6662.            "",                          /* default */
  6663.            "",                          /* addtl string data */
  6664.            0,                           /* addtl numeric data 1 */
  6665.            0,                           /* addtl numeric data 2 */
  6666.            NULL,
  6667.            NULL,
  6668.            NULL
  6669.            );
  6670.  
  6671.     while (1) {                         /* Parse 0 or more switches */
  6672.         x = cmfdb(&sw);                 /* Parse something */
  6673.         debug(F101,"ftp get cmfdb","",x);
  6674.         if (x < 0)                      /* Error */
  6675.           goto xgetx;                   /* or reparse needed */
  6676.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  6677.           break;
  6678.         c = cmgbrk();                   /* Get break character */
  6679.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  6680.         if (getval && !(cmresult.kflags & CM_ARG)) {
  6681.             printf("?This switch does not take arguments\n");
  6682.             x = -9;
  6683.             goto xgetx;
  6684.         }
  6685.         n = cmresult.nresult;           /* Numeric result = switch value */
  6686.         debug(F101,"ftp get switch","",n);
  6687.  
  6688.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6689.             printf("?This switch requires an argument\n");
  6690.             x = -9;
  6691.             goto xgetx;
  6692.         }
  6693.         switch (n) {                    /* Process the switch */
  6694.           case SND_ASN:                 /* /AS-NAME: */
  6695.             debug(F101,"ftp get /as-name getval","",getval);
  6696.             if (!getval) break;
  6697.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  6698.                 if (x == -3) {
  6699.                     printf("?name required\n");
  6700.                     x = -9;
  6701.                 }
  6702.                 goto xgetx;
  6703.             }
  6704.             s = brstrip(s);
  6705.             if (!*s) s = NULL;
  6706.             makestr(&(pv[n].sval),s);
  6707.             pv[n].ival = 1;
  6708.             break;
  6709.  
  6710.           case SND_BIN:                 /* /BINARY */
  6711.           case SND_TXT:                 /* /TEXT or /ASCII */
  6712.           case SND_TEN:                 /* /TENEX */
  6713.             pv[SND_BIN].ival = 0;
  6714.             pv[SND_TXT].ival = 0;
  6715.             pv[SND_TEN].ival = 0;
  6716.             pv[n].ival = 1;
  6717.             break;
  6718.  
  6719. #ifdef PUTPIPE
  6720.           case SND_CMD:                 /* These take no args */
  6721.             if (nopush) {
  6722.                 printf("?Sorry, system command access is disabled\n");
  6723.                 x = -9;
  6724.                 goto xgetx;
  6725.             }
  6726. #ifdef PIPESEND
  6727.             else if (rcvfilter) {
  6728.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  6729.                 x = -9;
  6730.                 goto xgetx;
  6731.             }
  6732. #endif /* PIPESEND */
  6733.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  6734.             pv[n].ival = 1;             /* Just set the flag */
  6735.             pv[SND_ARR].ival = 0;
  6736.             break;
  6737. #endif /* PUTPIPE */
  6738.  
  6739.           case SND_SHH:                 /* /QUIET */
  6740.           case SND_RES:                 /* /RECOVER (reget) */
  6741.           case SND_NOB:                 /* /NOBACKUPFILES */
  6742.           case SND_DEL:                 /* /DELETE */
  6743.           case SND_UPD:                 /* /UPDATE */
  6744.           case SND_USN:                 /* /UNIQUE */
  6745.           case SND_NOD:                 /* /NODOTFILES */
  6746.           case SND_REC:                 /* /RECOVER */
  6747.           case SND_MAI:                 /* /TO-SCREEN */
  6748.             pv[n].ival = 1;             /* Just set the flag */
  6749.             break;
  6750.  
  6751.           case SND_DIF:                 /* /DATES-DIFFER */
  6752.         pv[SND_COL].ival = XYFX_M;    /* Now it's a collision option */
  6753.         pv[n].ival = 1;
  6754.         break;
  6755.  
  6756.           case SND_COL:                 /* /COLLISION: */
  6757.             if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  6758.               goto xgetx;
  6759.         if (x == XYFX_M)
  6760.           pv[SND_DIF].ival = 1;    /* (phase this out) */
  6761.         pv[n].ival = x;        /* this should be sufficient */
  6762.             break;
  6763.  
  6764.           case SND_ERR:                 /* /ERROR-ACTION */
  6765.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  6766.               goto xgetx;
  6767.             pv[n].ival = x;
  6768.             break;
  6769.  
  6770.           case SND_EXC:                 /* Exception list */
  6771.             if (!getval) break;
  6772.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  6773.                 if (x == -3) {
  6774.                     printf("?Pattern required\n");
  6775.                     x = -9;
  6776.                 }
  6777.                 goto xgetx;
  6778.             }
  6779.             if (s) if (!*s) s = NULL;
  6780.             makestr(&(pv[n].sval),s);
  6781.             if (pv[n].sval)
  6782.               pv[n].ival = 1;
  6783.             break;
  6784.  
  6785. #ifdef PIPESEND
  6786.           case SND_FLT:
  6787.             debug(F101,"ftp get /filter getval","",getval);
  6788.             if (!getval) break;
  6789.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  6790.                 if (x == -3)
  6791.                   s = "";
  6792.                 else
  6793.                   goto xgetx;
  6794.             }
  6795.             s = brstrip(s);
  6796.             if (pv[SND_MAI].ival < 1) {
  6797.                 y = strlen(s);
  6798.                 /* Make sure they included "\v(...)" */
  6799.                 for (x = 0; x < y; x++) {
  6800.                     if (s[x] != '\\') continue;
  6801.                     if (s[x+1] == 'v') break;
  6802.                 }
  6803.                 if (x == y) {
  6804.                     printf(
  6805.                 "?Filter must contain a replacement variable for filename.\n"
  6806.                            );
  6807.                     x = -9;
  6808.                     goto xgetx;
  6809.                 }
  6810.             }
  6811.             if (*s) {
  6812.                 pv[n].ival = 1;
  6813.                 makestr(&(pv[n].sval),s);
  6814.             } else {
  6815.                 pv[n].ival = 0;
  6816.                 makestr(&(pv[n].sval),NULL);
  6817.             }
  6818.             break;
  6819. #endif /* PIPESEND */
  6820.  
  6821.           case SND_NAM:
  6822.             if (!getval) break;
  6823.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  6824.               goto xgetx;
  6825.             debug(F101,"ftp get /filenames","",x);
  6826.             pv[n].ival = x;
  6827.             break;
  6828.  
  6829.           case SND_SMA:                 /* Smaller / larger than */
  6830.           case SND_LAR: {
  6831.           CK_OFF_T y;
  6832.           if (!getval) break;
  6833.           if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
  6834.         goto xgetx;
  6835.           pv[n].wval = y;
  6836.           break;
  6837.       }
  6838.           case SND_FIL:                 /* Name of file containing filnames */
  6839.             if (!getval) break;
  6840.             if ((x = cmifi("Name of file containing list of filenames",
  6841.                                "",&s,&y,xxstring)) < 0) {
  6842.                 if (x == -3) {
  6843.                     printf("?Filename required\n");
  6844.                     x = -9;
  6845.                 }
  6846.                 goto xgetx;
  6847.             } else if (y && iswild(s)) {
  6848.                 printf("?Wildcards not allowed BBB\n");
  6849.                 x = -9;
  6850.                 goto xgetx;
  6851.             }
  6852.             if (s) if (!*s) s = NULL;
  6853.             makestr(&(pv[n].sval),s);
  6854.             if (pv[n].sval)
  6855.               pv[n].ival = 1;
  6856.             break;
  6857.  
  6858.           case SND_MOV:                 /* MOVE after */
  6859.           case SND_REN:                 /* RENAME after */
  6860.           case SND_SRN: {               /* SERVER-RENAME */
  6861.               char * m = "";
  6862.               switch (n) {
  6863.                 case SND_MOV:
  6864.                   m =
  6865.                    "Device and/or directory for incoming file after reception";
  6866.                   break;
  6867.                 case SND_REN:
  6868.                   m = "New name for incoming file after reception";
  6869.                   break;
  6870.                 case SND_SRN:
  6871.                   m = "New name for source file on server after reception";
  6872.                   break;
  6873.               }
  6874.               if (!getval) break;
  6875.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6876.                   if (x == -3) {
  6877.                       printf("%s\n", n == SND_MOV ?
  6878.                              "?Destination required" :
  6879.                              "?New name required"
  6880.                              );
  6881.                       x = -9;
  6882.                   }
  6883.                   goto xgetx;
  6884.               }
  6885.               makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6886.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  6887.               break;
  6888.           }
  6889. #ifndef NOCSETS
  6890.           case SND_CSL:                 /* Local character set */
  6891.           case SND_CSR:                 /* Remote (server) charset */
  6892.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6893.               return((x == -3) ? -2 : x);
  6894.             if (n == SND_CSL)
  6895.               x_csl = x;
  6896.             else
  6897.               x_csr = x;
  6898.             x_xla = 1;                  /* Overrides global OFF setting */
  6899.             break;
  6900.  
  6901.           case SND_XPA:                 /* Transparent */
  6902.             x_xla =  0;
  6903.             x_csr = -1;
  6904.             x_csl = -1;
  6905.             break;
  6906. #endif /* NOCSETS */
  6907.  
  6908.           case SND_NML:
  6909.             if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6910.               goto xgetx;
  6911.             makestr(&ftp_nml,s);
  6912.             break;
  6913.  
  6914.       case SND_PAT:            /* /PATTERN: */
  6915.         if (!getval) break;
  6916.         if ((x = cmfld("Pattern","*", &s, xxstring)) < 0)
  6917.           goto xgetx;
  6918.         makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6919.         pv[n].ival = (pv[n].sval) ? 1 : 0;
  6920.         break;
  6921.  
  6922.       case SND_NLS:            /* /NLST */
  6923.             pv[n].ival = 1;        /* Use NLST */
  6924.         pv[SND_MLS].ival = 0;    /* Don't use MLSD */
  6925.         break;
  6926.  
  6927.       case SND_MLS:            /* /MLSD */
  6928.             pv[n].ival = 1;        /* Use MLSD */
  6929.         pv[SND_NLS].ival = 0;    /* Don't use NLST */
  6930.         break;
  6931.  
  6932.           default:                      /* /AFTER, /PERMISSIONS, etc... */
  6933.             printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6934.             x = -9;
  6935.             goto xgetx;
  6936.         }
  6937.     }
  6938.     line[0] = NUL;
  6939.     cmarg = line;
  6940.     cmarg2 = asnambuf;
  6941.     s = line;
  6942. /*
  6943.   For GET, we want to parse an optional as-name, like with PUT.
  6944.   For MGET, we must parse a list of names, and then send NLST or MLSD
  6945.   commands for each name separately.
  6946. */
  6947.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  6948.       case _CMFLD:                      /* Field */
  6949.         if (!getone) {
  6950.             s = brstrip(cmresult.sresult);
  6951.             makestr(&(mgetlist[mgetn++]),s);
  6952.             while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6953.                 if (x < 0)
  6954.                   goto xgetx;
  6955.                 makestr(&(mgetlist[mgetn++]),brstrip(s));
  6956.                 if (mgetn >= MGETMAX) {
  6957.                     printf("?Too many items in MGET list\n");
  6958.                     goto xgetx;
  6959.                 }
  6960.             }
  6961.             if ((x = cmcfm()) < 0)
  6962.               goto xgetx;
  6963.         } else {
  6964.             s = brstrip(cmresult.sresult);
  6965.             ckstrncpy(line,s,LINBUFSIZ);
  6966.             if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6967.               if (x != -3)
  6968.                 goto xgetx;
  6969.             s = brstrip(s);
  6970.             ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6971.             if ((x = cmcfm()) < 0)
  6972.               goto xgetx;
  6973.         }
  6974.         break;
  6975.       case _CMCFM:                      /* Confirmation */
  6976.         break;
  6977.       default:
  6978.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  6979.         x = -9;
  6980.         goto xgetx;
  6981.     }
  6982.     if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
  6983.       recursive = 2;
  6984.  
  6985.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  6986.         forcetype = 1;                  /* So skip the name-pattern match */
  6987.         ftp_typ = XYFT_B;               /* Set binary */
  6988.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  6989.         forcetype = 1;
  6990.         ftp_typ = XYFT_T;
  6991.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  6992.         forcetype = 1;
  6993.         ftp_typ = FTT_TEN;
  6994.     } else if (ftp_cmdlin && ftp_xfermode == XMODE_M) {
  6995.         forcetype = 1;
  6996.         ftp_typ = binary;
  6997.         g_ftp_typ = binary;
  6998.     }
  6999.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  7000.         char * p;
  7001.         p = brstrip(pv[SND_ASN].sval);  /* As-name */
  7002.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  7003.     }
  7004.     debug(F110,"ftp get asnambuf",asnambuf,0);
  7005.  
  7006. #ifdef PIPESEND
  7007.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  7008.         char * p;
  7009.         p = asnambuf;
  7010.         debug(F110,"GET /COMMAND before stripping",p,0);
  7011.         p = brstrip(p);
  7012.         debug(F110,"GET /COMMAND after stripping",p,0);
  7013.         if (!*p) {
  7014.             printf("?Sorry, a command to write to is required\n");
  7015.             x = -9;
  7016.             goto xgetx;
  7017.         }
  7018.         pipename = p;
  7019.         pipesend = 1;
  7020.     }
  7021. #endif /* PIPESEND */
  7022.  
  7023. /* Set up /MOVE and /RENAME */
  7024.  
  7025. #ifdef COMMENT
  7026.     /* Conflict exists only for PUT - removed 13 Mar 2006 - fdc */
  7027.     if (pv[SND_DEL].ival > 0 &&
  7028.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  7029.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  7030.         x = -9;
  7031.         goto xgetx;
  7032.     }
  7033. #endif    /* COMMENT */
  7034. #ifdef CK_TMPDIR
  7035.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  7036.         int len;
  7037.         char * p = pv[SND_MOV].sval;
  7038.         len = strlen(p);
  7039.         if (!isdir(p)) {                /* Check directory */
  7040. #ifdef CK_MKDIR
  7041.             char * s = NULL;
  7042.             s = (char *)malloc(len + 4);
  7043.             if (s) {
  7044.                 strcpy(s,p);            /* safe */
  7045. #ifdef datageneral
  7046.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  7047. #else
  7048.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  7049. #endif /* datageneral */
  7050.                 s[len++] = 'X';
  7051.                 s[len] = NUL;
  7052. #ifdef NOMKDIR
  7053.                 x = -1;
  7054. #else
  7055.                 x = zmkdir(s);
  7056. #endif /* NOMKDIR */
  7057.                 free(s);
  7058.                 if (x < 0) {
  7059.                     printf("?Can't create \"%s\"\n",p);
  7060.                     x = -9;
  7061.                     goto xgetx;
  7062.                 }
  7063.             }
  7064. #else
  7065.             printf("?Directory \"%s\" not found\n",p);
  7066.             x = -9;
  7067.             goto xgetx;
  7068. #endif /* CK_MKDIR */
  7069.         }
  7070.         makestr(&rcv_move,p);
  7071.         moving = 1;
  7072.     }
  7073. #endif /* CK_TMPDIR */
  7074.  
  7075.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  7076.         char * p = pv[SND_REN].sval;
  7077.         if (!p) p = "";
  7078.         if (!*p) {
  7079.             printf("?New name required for /RENAME\n");
  7080.             x = -9;
  7081.             goto xgetx;
  7082.         }
  7083.         p = brstrip(p);
  7084. #ifndef NOSPL
  7085.     /* If name given is wild, rename string must contain variables */
  7086.         if (mget && !getone) {
  7087.             char * s = tmpbuf;
  7088.             x = TMPBUFSIZ;
  7089.             zzstring(p,&s,&x);
  7090.             if (!strcmp(tmpbuf,p)) {
  7091.                 printf(
  7092.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  7093.                        );
  7094.                 x = -9;
  7095.                 goto xgetx;
  7096.             }
  7097.         }
  7098. #endif /* NOSPL */
  7099.         renaming = 1;
  7100.         makestr(&rcv_rename,p);
  7101.         debug(F110,"FTP rcv_rename",rcv_rename,0);
  7102.     }
  7103.     if (!cmarg[0] && mgetn == 0 && getone && pv[SND_FIL].ival < 1) {
  7104.         printf("?Filename required but not given\n");
  7105.         x = -9;
  7106.         goto xgetx;
  7107.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  7108.         printf("?You can't give both /LISTFILE and a remote filename\n");
  7109.         x = -9;
  7110.         goto xgetx;
  7111.     }
  7112.     CHECKCONN();                        /* Check connection */
  7113.  
  7114.     if (pv[SND_COL].ival > -1)
  7115.       x_fnc = pv[SND_COL].ival;
  7116.  
  7117. #ifndef NOSPL
  7118.     /* If as-name given for MGET, as-name must contain variables */
  7119.     if (mget && !getone && asnambuf[0] && x_fnc != XYFX_A) {
  7120.         char * s = tmpbuf;
  7121.         x = TMPBUFSIZ;
  7122.         zzstring(asnambuf,&s,&x);
  7123.         if (!strcmp(tmpbuf,asnambuf)) {
  7124.             printf(
  7125.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  7126.                    );
  7127.             x = -9;
  7128.             goto xgetx;
  7129.         }
  7130.     }
  7131. #endif /* NOSPL */
  7132.  
  7133. /* doget: */
  7134.  
  7135.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  7136.         fdispla = 0;
  7137.     } else {
  7138.         displa = 1;
  7139.         if (mdel || ftp_deb)
  7140.       fdispla = XYFD_B;
  7141.     }
  7142.     deleting = 0;
  7143.     if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
  7144.       deleting = 1;
  7145.     if (pv[SND_EXC].ival > 0)
  7146.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  7147.     if (pv[SND_SMA].wval > -1)
  7148.       getsmaller = pv[SND_SMA].wval;
  7149.     if (pv[SND_LAR].wval > -1)
  7150.       getlarger = pv[SND_LAR].wval;
  7151.     if (pv[SND_NAM].ival > -1)
  7152.       x_cnv = pv[SND_NAM].ival;
  7153.     if (pv[SND_ERR].ival > -1)
  7154.       geterror = pv[SND_ERR].ival;
  7155.     if (pv[SND_MAI].ival > -1)
  7156.       toscreen = 1;
  7157.  
  7158.     if (pv[SND_NLS].ival > 0) {        /* Force NLST or MLSD? */
  7159.     mgetmethod = SND_NLS;
  7160.     mgetforced = 1;
  7161.     } else if (pv[SND_MLS].ival > 0) {
  7162.     mgetmethod = SND_MLS;
  7163.     mgetforced = 1;
  7164.     }
  7165.  
  7166. #ifdef FTP_RESTART
  7167.     if (pv[SND_RES].ival > 0) {
  7168.         if (!ftp_typ) {
  7169.             printf("?Sorry, GET /RECOVER requires binary mode\n");
  7170.             x = -9;
  7171.             goto xgetx;
  7172. #ifdef COMMENT
  7173.         /* Not true - the fact that the initial REST fails does not mean */
  7174.         /* it will fail here.  */
  7175.         } else if (!okrestart) {
  7176.             printf("WARNING: Server might not support restart...\n");
  7177. #endif /* COMMENT */
  7178.         }
  7179.         restart = 1;
  7180.     }
  7181. #endif /* FTP_RESTART */
  7182.  
  7183. #ifdef PIPESEND
  7184.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  7185.         if (pipesend) {
  7186.             printf("?Switch conflict: /FILTER and /COMMAND\n");
  7187.             x = -9;
  7188.             goto xgetx;
  7189.         }
  7190.         makestr(&rcvfilter,pv[SND_FLT].sval);
  7191.         debug(F110,"ftp get /FILTER", rcvfilter, 0);
  7192.     }
  7193.     if (rcvfilter || pipesend) {        /* /RESTART */
  7194. #ifdef FTP_RESTART
  7195.         if (restart) {                  /* with pipes or filters */
  7196.             printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  7197.             x = -9;
  7198.             goto xgetx;
  7199.         }
  7200. #endif /* FTP_RESTART */
  7201.         if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_M || x_fnc == XYFX_U) {
  7202.             printf(
  7203.         "?Switch conflict: /FILTER or /COMMAND and Date Checking\n");
  7204.             x = -9;
  7205.             goto xgetx;
  7206.         }
  7207.     }
  7208. #endif /* PIPESEND */
  7209.  
  7210.     tfc = (CK_OFF_T)0;            /* Initialize stats and counters */
  7211.     filcnt = 0;
  7212.     pktnum = 0;
  7213.     rpackets = 0L;
  7214.  
  7215.     if (pv[SND_FIL].ival > 0) {
  7216.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  7217.             debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  7218.             printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  7219.             x = -9;
  7220.             goto xgetx;
  7221.         }
  7222.         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  7223.             zclose(ZMFILE);                       /* Failed */
  7224.             debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  7225.             printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  7226.             x = -9;
  7227.             goto xgetx;
  7228.         }
  7229.         listfile = 1;
  7230.         debug(F110,"ftp get listfile first",tmpbuf,0);
  7231.         makestr(&(mgetlist[0]),tmpbuf);
  7232.     }
  7233.     t0 = gmstimer();                    /* Record starting time */
  7234.  
  7235.     updating = 0;            /* Checking dates? */
  7236.     if (pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U))
  7237.       updating = 1;
  7238.     if (pv[SND_DIF].ival > 0 || x_fnc == XYFX_M)
  7239.       updating = 2;
  7240.     if (updating)            /* These switches force FTP DATES ON */
  7241.       ftp_dates |= 2;
  7242.  
  7243.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  7244.  
  7245.     cancelgroup = 0;                    /* Group not canceled yet */
  7246.     if (!(ftp_xfermode == XMODE_A && patterns && get_auto && !forcetype))
  7247.       changetype(ftp_typ,0);        /* Change to requested type */
  7248.     binary = ftp_typ;                   /* For file-transfer display */
  7249.     first = 1;                          /* For MGET list */
  7250.     done = 0;                           /* Loop control */
  7251.  
  7252. #ifdef CK_TMPDIR
  7253.     if (dldir && !f_tmpdir) {           /* If they have a download directory */
  7254.         if ((s = zgtdir())) {           /* Get current directory */
  7255.             if (zchdir(dldir)) {        /* Change to download directory */
  7256.                 ckstrncpy(savdir,s,TMPDIRLEN);
  7257.                 f_tmpdir = 1;           /* Remember that we did this */
  7258.             }
  7259.         }
  7260.     }
  7261. #endif /* CK_TMPDIR */
  7262.  
  7263.     if (ftp_nml) {                      /* /NAMELIST */
  7264.         debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  7265.         if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  7266.           fp_nml = stdout;
  7267.         else
  7268.           fp_nml = fopen(ftp_nml, "wb");
  7269.         if (!fp_nml) {
  7270.             printf("?%s: %s\n",ftp_nml,ck_errstr());
  7271.             goto xgetx;
  7272.         }
  7273.     }
  7274.     while (!done && !cancelgroup) {     /* Loop for all files */
  7275.                                         /* or until canceled. */
  7276. #ifdef FTP_PROXY
  7277.         /* do something here if proxy */
  7278. #endif /* FTP_PROXY */
  7279.  
  7280.         rs_len = (CK_OFF_T)0;        /* REGET position */
  7281.         cancelfile = 0;                 /* This file not canceled yet */
  7282.         haspath = 0;                    /* Recalculate this each time thru */
  7283.  
  7284.         if (getone) {                   /* GET */
  7285.             char * p;
  7286.             s = line;
  7287.             src = line;                 /* Server name */
  7288.             done = 1;
  7289.             debug(F111,"ftp get file",s,0);
  7290.         } else if (mget) {              /* MGET */
  7291.             src = mgetlist[mgetx];
  7292.             debug(F111,"ftp mget remote_files A",src,first);
  7293.             s = (char *)remote_files(first,
  7294.                      (CHAR *)mgetlist[mgetx],
  7295.                      (CHAR *)pv[SND_PAT].sval,
  7296.                      0
  7297.                      );
  7298.             debug(F110,"ftp mget remote_files B",s,0);
  7299.             if (!s) s = "";
  7300.             if (!*s) {
  7301.                 first = 1;
  7302.                 if (listfile) {        /* Names from listfile */
  7303.                   again:
  7304.                     tmpbuf[0] = NUL;
  7305.                     while (!tmpbuf[0]) {
  7306.                         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  7307.                             zclose(ZMFILE);
  7308.                             debug(F110,"ftp get listfile EOF",
  7309.                                   pv[SND_FIL].sval,0);
  7310.                             makestr(&(mgetlist[0]),NULL);
  7311.                             s = NULL;
  7312.                             done = 1;
  7313.                             break;
  7314.                         }
  7315.                     }
  7316.                     if (done)
  7317.                       continue;
  7318.  
  7319.                     makestr(&(mgetlist[0]),tmpbuf);
  7320.             debug(F110,"ftp get listfile next",tmpbuf,0);
  7321.                     s = (char *)remote_files(first,
  7322.                          (CHAR *)mgetlist[0],
  7323.                          (CHAR *)pv[SND_PAT].sval,
  7324.                          0
  7325.                          );
  7326.             debug(F110,"ftp mget remote_files C",s,0);
  7327.                     if (!s) {
  7328.                         ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  7329.                         ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,"File not found");
  7330.                         tlog(F110,"ftp get file not found:",s,0);
  7331.                         goto again;
  7332.                     }
  7333.                 } else {        /* Names from command line */
  7334.                     mgetx++;
  7335.                     if (mgetx < mgetn)
  7336.               s = (char *)remote_files(first,
  7337.                            (CHAR *)mgetlist[mgetx],
  7338.                            (CHAR *)pv[SND_PAT].sval,
  7339.                            0
  7340.                            );
  7341.                     else
  7342.               s = NULL;
  7343.             if (!s) mgetx++;
  7344.             debug(F111,"ftp mget remote_files D",s,mgetx);
  7345.                 }
  7346.                 if (!s) {
  7347.             if (!first || mgetx >= mgetn) {
  7348.             done = 1;
  7349.             break;
  7350.             } else if (geterror) {
  7351.             status = 0;
  7352.             done = 1;
  7353.             break;
  7354.             } else {
  7355.             continue;
  7356.             }
  7357.                 }
  7358.             }
  7359.         }
  7360.     debug(F111,"ftp mget remote_files E",s,0);
  7361.         /*
  7362.           The semantics of NLST are ill-defined.  Suppose we have just sent
  7363.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  7364.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  7365.           RETR commands based on the pathless names are not going to work.
  7366.         */
  7367.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  7368.             char * s3;
  7369.             if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  7370.                 int len, left = 4096;
  7371.                 char * tmp = xtmpbuf;
  7372.                 len = s3 - mgetlist[mgetx] + 1;
  7373.                 ckstrncpy(tmp,mgetlist[mgetx],left);
  7374.                 tmp += len;
  7375.                 left -= len;
  7376.                 ckstrncpy(tmp,s,left);
  7377.                 s = xtmpbuf;
  7378.         debug(F111,"ftp mget remote_files F",s,0);
  7379.             }
  7380.         }
  7381.         first = 0;
  7382.         skipthis = 0;                   /* File selection... */
  7383.         msg = "";
  7384.         nam = s;                        /* Filename (without path) */
  7385.         rc = 0;                         /* Initial return code */
  7386.         s2 = "";
  7387.  
  7388.         if (!getone && !skipthis) {     /* For MGET and MDELETE... */
  7389.             char c, * p = s;
  7390.             int srvpath = 0;
  7391.             int usrpath = 0;
  7392.             int i, k = 0;
  7393.  
  7394.         debug(F111,"ftp mget havetype",s,havetype);
  7395.         if (havetype > 0 && havetype != FTYP_FILE) {
  7396.         /* Server says it's not file... */
  7397.         debug(F110,"ftp mget not-a-file",s,0);
  7398.         continue;
  7399.         }
  7400. /*
  7401.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  7402.   But if the client did not ask for a recursive list, we have to ignore any
  7403.   server files that include a pathname that extends beyond any path that
  7404.   was included in the user's request.
  7405.  
  7406.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  7407.   get the user's path segment.  Then, for each incoming file, if it begins
  7408.   with the same path segment, we must strip it (point past it).
  7409. */
  7410.             src = mgetlist[mgetx];      /* In case it moved! */
  7411.         if (src) {
  7412.         for (i = 0; src[i]; i++) { /* Find rightmost path separator */
  7413.             if (ispathsep(src[i])) /* in user's pathname */
  7414.               k = i + 1;
  7415.         }
  7416.         } else {
  7417.         src = "";
  7418.         }
  7419.             usrpath = k;                /* User path segment length */
  7420.             debug(F111,"ftp get usrpath",src,usrpath);
  7421.  
  7422.             p = s;                      /* Server filename */
  7423.             while ((c = *p++)) {        /* Look for path in server filename */
  7424.                 if (ispathsep(c)) {
  7425.             /* haspath++; */
  7426.                     nam = p;            /* Pathless name (for ckmatch) */
  7427.                     srvpath = p - s;    /* Server path segment length */
  7428.                 }
  7429.             }
  7430.             debug(F111,"ftp get srvpath",s,srvpath);
  7431.  
  7432.         if (usrpath == 0) {
  7433. /*
  7434.   Here we handle the case where the user said "mget foo" where foo is a
  7435.   directory name, and the server is sending back names like "foo/file1",
  7436.   "foo/file2", etc.  This is a nasty trick but it's necessary because the
  7437.   user can't compensate by typing "mget foo/" because then the server is
  7438.   likely to send back "foo//file1, foo//file2" etc, and we still won't
  7439.   get a match...
  7440. */
  7441.         int srclen = 0, srvlen = 0;
  7442.         if (src) srclen = strlen(src);
  7443.         if (s) srvlen = strlen(s);
  7444.         if (src && (srvlen > srclen)) {
  7445.             if (!strncmp(src,s,srclen) && ispathsep(s[srclen])) {
  7446.             char * tmpsrc = NULL;
  7447.             tmpsrc = (char *)malloc(srclen + 2);
  7448.             strncpy(tmpsrc,src,srclen);
  7449.             tmpsrc[srclen] = s[srclen];
  7450.             tmpsrc[srclen+1] = NUL;
  7451.             free(mgetlist[mgetx]);
  7452.             mgetlist[mgetx] = tmpsrc;
  7453.             tmpsrc = NULL;
  7454.             src = mgetlist[mgetx];
  7455.             usrpath = srclen+1;
  7456.             }                  
  7457.         }
  7458.         }
  7459. /*
  7460.   If as-name not given and server filename includes path that matches
  7461.   the pathname from the user's file specification, we must trim the common
  7462.   path prefix from the server's name when constructing the local name.
  7463. */
  7464.             if (src &&            /* Wed Sep 25 17:27:48 2002 */
  7465.         !asnambuf[0] &&
  7466.         !recursive &&        /* Thu Sep 19 16:11:59 2002 */
  7467.         (srvpath > 0) &&
  7468.         !strncmp(src,s,usrpath)) {
  7469.                 s2 = s + usrpath;       /* Local name skips past remote path */
  7470.             }
  7471. #ifdef COMMENT
  7472.         /* This doesn't work if the path prefix contains wildcards! */
  7473.         haspath = (srvpath > usrpath);
  7474. #else
  7475.         {                /* Count path segments instead */
  7476.         int x1 = 0, x2 = 0;
  7477.         char *p;
  7478.         for (p = s; *p; p++)
  7479.           if (ispathsep(*p)) x1++;
  7480.         for (p = src; *p; p++) {
  7481.             if (ispathsep(*p)) x2++;
  7482.         }
  7483.         haspath = recursive ? x1 || x2 : x1 > x2;
  7484.         debug(F111,"ftp get server path segments",s,x1);
  7485.         debug(F111,"ftp get user   path segments",src,x2);
  7486.         }
  7487.  
  7488. #endif /* COMMENT */
  7489.             debug(F111,"ftp get haspath",s+usrpath,haspath);
  7490.  
  7491.             if (haspath) {              /* Server file has path segments? */
  7492.                 if (!recursive) {       /* [M]GET /RECURSIVE? */
  7493. /*
  7494.   We did not ask for a recursive listing, but the server is sending us one
  7495.   anyway (as wu-ftpd is wont to do).  We get here if the current filename
  7496.   includes a path segment beyond any path segment we asked for in our
  7497.   non-recursive [M]GET command.  We MUST skip this file.
  7498. */
  7499.                     debug(F111,"ftp get skipping because of path",s,0);
  7500.                     continue;
  7501.                 }
  7502.             }
  7503.         } else if (getone && !skipthis) { /* GET (not MGET) */
  7504.             char * p = nam;
  7505.         while ((c = *p++)) {    /* Handle path in local name */
  7506.         if (ispathsep(c)) {
  7507.             if (recursive) {    /* If recursive, keep it */
  7508.             haspath = 1;
  7509.             break;
  7510.             } else {        /* Otherwise lose it. */
  7511.               nam = p;
  7512.             }
  7513.         }
  7514.             }
  7515.             s2 = nam;
  7516.         }
  7517.         if (!*nam)                      /* Name without path */
  7518.           nam = s;
  7519.  
  7520.         if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  7521.             if (nam[0] == '.')
  7522.           continue;
  7523.         }
  7524.         if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  7525.         int xx;
  7526.             for (i = 0; i < NSNDEXCEPT; i++) {
  7527.                 if (!rcvexcept[i]) {
  7528.                     break;
  7529.                 }
  7530.         xx = ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1);
  7531.         debug(F111,"ftp mget /except match",rcvexcept[i],xx);
  7532.                 if (xx) {
  7533.                     tlog(F100," refused: exception list","",0);
  7534.             msg = "Refused: Exception List";
  7535.                     skipthis++;
  7536.                     break;
  7537.                 }
  7538.             }
  7539.         }
  7540.         if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  7541.             if (ckmatch(
  7542. #ifdef CKREGEX
  7543.                         "*.~[0-9]*~"
  7544. #else
  7545.                         "*.~*~"
  7546. #endif /* CKREGEX */
  7547.                         ,nam,0,1) > 0)
  7548.               continue;
  7549.         }
  7550.         if (!x_xla) {                   /* If translation is off */
  7551.             x_csl = -2;                 /* unset the charsets */
  7552.             x_csr = -2;
  7553.         }
  7554.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  7555.         if (!*s2)                       /* Local name */
  7556.           s2 = asnambuf;                /* As-name */
  7557.  
  7558.     if (!*s2)            /* Sat Nov 16 19:19:39 2002 */
  7559.       s2 = recursive ? s : nam;    /* Fri Jan 10 13:15:19 2003 */
  7560.  
  7561.         debug(F110,"ftp get filnam  ",s,0);
  7562.         debug(F110,"ftp get asname A",s2,0);
  7563.  
  7564.         /* Receiving to real file */
  7565.         if (!pipesend &&
  7566. #ifdef PIPESEND
  7567.             !rcvfilter &&
  7568. #endif /* PIPESEND */
  7569.             !toscreen) {
  7570. #ifndef NOSPL
  7571.             /* Do this here so we can decide whether to skip */
  7572.             if (cmd_quoting && !skipthis && asnambuf[0]) {
  7573.                 int n; char *p;
  7574.                 n = TMPBUFSIZ;
  7575.                 p = tmpbuf;
  7576.                 zzstring(asnambuf,&p,&n);
  7577.                 s2 = tmpbuf;
  7578.                 debug(F111,"ftp get asname B",s2,updating);
  7579.             }
  7580. #endif /* NOSPL */
  7581.  
  7582.         local = *s2 ? s2 : s;
  7583.  
  7584.         if (!skipthis && x_fnc == XYFX_D) { /* File Collision = Discard */
  7585.         CK_OFF_T x;
  7586.         x = zchki(local);
  7587.         debug(F111,"ftp get DISCARD zchki",local,x);
  7588.         if (x > -1) {
  7589.             skipthis++;
  7590.             debug(F110,"ftp get skip name",local,0);
  7591.             tlog(F100," refused: name","",0);
  7592.             msg = "Refused: Name";
  7593.         }
  7594.         }
  7595.  
  7596. #ifdef DOUPDATE
  7597.             if (!skipthis && updating) { /* If updating and not yet skipping */
  7598.                 if (zchki(local) > -1) {
  7599.                     x = chkmodtime(local,s,0);
  7600. #ifdef DEBUG
  7601.             if (deblog) {
  7602.             if (updating == 2)
  7603.               debug(F111,"ftp get /dates-diff chkmodtime",local,x);
  7604.             else
  7605.               debug(F111,"ftp get /update chkmodtime",local,x);
  7606.             }
  7607. #endif /* DEBUG */
  7608.             if ((updating == 1 && x > 0) ||  /* /UPDATE */
  7609.             (updating == 2 && x == 1)) { /* /DATES-DIFFER */
  7610.             skipthis++;
  7611.             tlog(F100," refused: date","",0);
  7612.             msg = "Refused: Date";
  7613.                         debug(F110,"ftp get skip date",local,0);
  7614.                     }
  7615.                 }
  7616.             }
  7617. #endif /* DOUPDATE */
  7618.         }
  7619.         /* Initialize file size to -1 in case server doesn't understand */
  7620.         /* SIZE command, so xxscreen() will know we don't know the size */
  7621.  
  7622.         fsize = (CK_OFF_T)-1;
  7623.  
  7624.     /* Ask for size now only if we need it for selection */
  7625.     /* because if you're going thru a list 100,000 files to select */
  7626.     /* a small subset, 100,000 SIZE commands can take hours... */
  7627.  
  7628.     gotsize = 0;
  7629.         if (!mdel && !skipthis &&        /* Don't need size for DELE... */
  7630.         (getsmaller >= (CK_OFF_T)0  || getlarger >= (CK_OFF_T)0)) {
  7631.         if (havesize >= (CK_OFF_T)0) { /* Already have file size? */
  7632.         fsize = havesize;
  7633.         gotsize = 1;
  7634.         } else {            /* No - must ask server */
  7635.         /*
  7636.           Prior to sending the NLST command we necessarily put the
  7637.           server into ASCII mode.  We must now put it back into the
  7638.           the requested mode so the upcoming SIZE command returns
  7639.           right kind of size; this is especially important for
  7640.           GET /RECOVER; otherwise the server returns the "ASCII" size
  7641.           of the file, rather than its true size.
  7642.         */
  7643.         changetype(ftp_typ,0);    /* Change to requested type */
  7644.         fsize = (CK_OFF_T)-1;
  7645.         if (sizeok) {
  7646.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7647.             if (x == REPLY_COMPLETE) {
  7648.             fsize = ckatofs(&ftp_reply_str[4]);
  7649.             gotsize = 1;
  7650.             }
  7651.         }
  7652.         }
  7653.             if (gotsize) {
  7654.                 if (getsmaller >= (CK_OFF_T)0 && fsize >= getsmaller)
  7655.                   skipthis++;
  7656.                 if (getlarger >= (CK_OFF_T)0 && fsize <= getlarger)
  7657.                   skipthis++;
  7658.                 if (skipthis) {
  7659.                     debug(F111,"ftp get skip size",s,fsize);
  7660.                     tlog(F100," refused: size","",0);
  7661.                     msg = "Refused: Size";
  7662.                 }
  7663. #ifdef COMMENT
  7664.             } else if (getone) {
  7665.                 /* SIZE can fail for many reasons.  Does the file exist? */
  7666.                 x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  7667.                 if (x != REPLY_COMPLETE) {
  7668.                     printf(">>> FILE NOT FOUND: %s\n",s);
  7669.                     break;
  7670.                 }
  7671. #endif /* COMMENT */
  7672.             }
  7673.         }
  7674.         if (skipthis) {                 /* Skipping this file? */
  7675.             ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  7676.             if (msg)
  7677.               ftscreen(SCR_ST,ST_ERR,(CK_OFF_T)0,msg);
  7678.             else
  7679.               ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,s);
  7680.             continue;
  7681.         }
  7682.         if (fp_nml) {                   /* /NAMELIST only - no transfer */
  7683.             fprintf(fp_nml,"%s\n",s);
  7684.             continue;
  7685.         }
  7686.         if (recursive && haspath && !pipesend
  7687. #ifdef PIPESEND
  7688.             && !rcvfilter
  7689. #endif /* PIPESEND */
  7690.             ) {
  7691.         int x;
  7692.  
  7693. #ifdef NOMKDIR
  7694.         x = -1;
  7695. #else
  7696.             x = zmkdir(s);        /* Try to make the directory */
  7697. #endif /* NOMKDIR */
  7698.  
  7699.             if (x < 0) {
  7700.                 rc = -1;                /* Failure is fatal */
  7701.                 if (geterror) {
  7702.                     status = 0;
  7703.                     ftscreen(SCR_EM,0,(CK_OFF_T)0,
  7704.                  "Directory creation failure");
  7705.                     break;
  7706.                 }
  7707.             }
  7708.         }
  7709.  
  7710.         /* Not skipping */
  7711.  
  7712.     selected++;            /* Count this file as selected */
  7713.         pn = NULL;
  7714.  
  7715.     if (!gotsize && !mdel) {    /* Didn't get size yet */
  7716.         if (havesize > (CK_OFF_T)-1) { /* Already have file size? */
  7717.         fsize = havesize;
  7718.         gotsize = 1;
  7719.         } else {            /* No - must ask server */
  7720.         fsize = (CK_OFF_T)-1;
  7721.         if (sizeok) {
  7722.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7723.             if (x == REPLY_COMPLETE) {
  7724.             fsize = ckatofs(&ftp_reply_str[4]);
  7725.             gotsize = 1;
  7726.             }
  7727.         }
  7728.         }
  7729.     }
  7730.         if (mdel) {                     /* [M]DELETE */
  7731.             if (displa && !ftp_vbm)
  7732.               printf(" %s...",s);
  7733.             rc =
  7734.              (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  7735.             if (rc > -1) {
  7736.                 tlog(F110,"ftp mdelete",s,0);
  7737.                 if (displa && !ftp_vbm)
  7738.                   printf("OK\n");
  7739.             } else {
  7740.                 tlog(F110,"ftp mdelete failed:",s,0);
  7741.                 if (displa)
  7742.                   printf("Failed\n");
  7743.             }
  7744. #ifndef NOSPL
  7745. #ifdef PIPESEND
  7746.         } else if (rcvfilter) {         /* [M]GET with filter */
  7747.             int n; char * p;
  7748.             n = CKMAXPATH;
  7749.             p = tmpbuf;                 /* Safe - no asname with filter */
  7750.             zzstring(rcvfilter,&p,&n);
  7751.             if (n > -1)
  7752.               pn = tmpbuf;
  7753.             debug(F111,"ftp get rcvfilter",pn,n);
  7754. #endif /* PIPESEND */
  7755. #endif /* NOSPL */
  7756.             if (toscreen) s2 = "-";
  7757.         } else if (pipesend) {          /* [M]GET /COMMAND */
  7758.             int n; char * p;
  7759.             n = CKMAXPATH;
  7760.             p = tmpbuf;                 /* Safe - no asname with filter */
  7761.             zzstring(pipename,&p,&n);
  7762.             if (n > -1)
  7763.               pn = tmpbuf;
  7764.             debug(F111,"ftp get pipename",pipename,n);
  7765.             if (toscreen) s2 = "-";
  7766.         } else {                        /* [M]GET with no pipes or filters */
  7767.             debug(F111,"ftp get s2 A",s2,x_cnv);
  7768.             if (toscreen) {
  7769.                 s2 = "-";               /* (hokey convention for stdout) */
  7770.             } else if (!*s2) {          /* No asname? */
  7771.                 if (x_cnv) {            /* If converting */
  7772.                     nzrtol(s,tmpbuf,x_cnv,1,CKMAXPATH); /* convert */
  7773.                     s2 = tmpbuf;
  7774.                     debug(F110,"ftp get nzrtol",s2,0);
  7775.                 } else                  /* otherwise */
  7776.                   s2 = s;               /* use incoming file's name */
  7777.             }
  7778.             debug(F110,"ftp get s2 B",s2,0);
  7779.  
  7780.             /* If local file already exists, take collision action */
  7781.  
  7782.             if (!pipesend &&
  7783. #ifdef PIPESEND
  7784.                 !rcvfilter &&
  7785. #endif /* PIPESEND */
  7786.                 !toscreen) {
  7787.         CK_OFF_T x;
  7788.                 x = zchki(s2);
  7789.                 debug(F111,"ftp get zchki",s2,x);
  7790.                 debug(F111,"ftp get x_fnc",s2,x_fnc);
  7791.  
  7792.                 if (x > (CK_OFF_T)-1 && !restart) {
  7793.             int x = -1;
  7794.             char * newname = NULL;
  7795.  
  7796.                     switch (x_fnc) {
  7797.                       case XYFX_A:      /* Append */
  7798.                         append = 1;
  7799.                         break;
  7800.                       case XYFX_R:      /* Rename */
  7801.                       case XYFX_B:    /* Backup */
  7802.             znewn(s2,&newname); /* Make unique name */
  7803.             debug(F110,"ftp get znewn",newname,0);
  7804.             if (x_fnc == XYFX_B) { /* Backup existing file */
  7805.                 x = zrename(s2,newname);
  7806.                 debug(F111,"ftp get backup zrename",newname,x);
  7807.             } else {      /* Rename incoming file */
  7808.                 x = ckstrncpy(tmpbuf,newname,CKMAXPATH+1);
  7809.                 s2 = tmpbuf;
  7810.                 debug(F111,"ftp get rename incoming",newname,x);
  7811.             }
  7812.             if (x < 0) {
  7813.                 ftscreen(SCR_EM,0,(CK_OFF_T)0,
  7814.                      "Backup/Rename failed");
  7815.                 x = 0;
  7816.                 goto xgetx;
  7817.             }
  7818.             break;
  7819.                       case XYFX_D:      /* Discard (already handled above) */
  7820.                       case XYFX_U:      /* Update (ditto) */
  7821.                       case XYFX_M:      /* Update (ditto) */
  7822.                       case XYFX_X:      /* Overwrite */
  7823.                         break;
  7824.                     }
  7825.                 }
  7826.             }
  7827.         }
  7828.         if (!mdel) {
  7829. #ifdef PIPESEND
  7830.             debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  7831. #endif /* PIPESEND */
  7832.             if (pipesend && !toscreen)
  7833.               s2 = NULL;
  7834. #ifdef DEBUG
  7835.             if (deblog) {
  7836.                 debug(F101,"ftp get x_xla","",x_xla);
  7837.                 debug(F101,"ftp get x_csl","",x_csl);
  7838.                 debug(F101,"ftp get x_csr","",x_csr);
  7839.                 debug(F101,"ftp get append","",append);
  7840.             }
  7841. #endif /* DEBUG */
  7842.  
  7843.             rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  7844.  
  7845. #ifdef DEBUG
  7846.             if (deblog) {
  7847.                 debug(F111,"ftp get rc",s,rc);
  7848.                 debug(F111,"ftp get ftp_timed_out",s,ftp_timed_out);
  7849.                 debug(F111,"ftp get cancelfile",s,cancelfile);
  7850.                 debug(F111,"ftp get cancelgroup",s,cancelgroup);
  7851.                 debug(F111,"ftp get renaming",s,renaming);
  7852.                 debug(F111,"ftp get moving",s,moving);
  7853.             }
  7854. #endif /* DEBUG */
  7855.         }
  7856.         if (rc > -1) {
  7857.             good++;
  7858.             status = 1;
  7859.             if (!cancelfile) {
  7860.                 if (deleting) {         /* GET /DELETE (source file) */
  7861.                     rc =
  7862.                       (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  7863.                         ? 1 : -1;
  7864.                     tlog(F110, (rc > -1) ?
  7865.                          " deleted" : " failed to delete", s, 0);
  7866.                 }
  7867.         if (renaming && rcv_rename && !toscreen) {
  7868.                     char *p;            /* Rename downloaded file */
  7869. #ifndef NOSPL
  7870.                     char tmpbuf[CKMAXPATH+1];
  7871.                     int n;
  7872.                     n = CKMAXPATH;
  7873.                     p = tmpbuf;
  7874.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7875.                     zzstring(rcv_rename,&p,&n);
  7876.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7877.                     p = tmpbuf;
  7878. #else
  7879.                     p = rcv_rename;
  7880. #endif /* NOSPL */
  7881.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7882.                     debug(F111,"doftpget /RENAME zrename",p,rc);
  7883.                     tlog(F110, (rc > -1) ?
  7884.                          " renamed to" :
  7885.                          " failed to rename to",
  7886.                          p,
  7887.                          0
  7888.                          );
  7889.                 } else if (moving && rcv_move && !toscreen) {
  7890.                     char *p;            /* Move downloaded file */
  7891. #ifndef NOSPL
  7892.                     char tmpbuf[CKMAXPATH+1];
  7893.                     int n;
  7894.                     n = TMPBUFSIZ;
  7895.                     p = tmpbuf;
  7896.                     debug(F111,"ftp get /move-to",rcv_move,0);
  7897.                     zzstring(rcv_move,&p,&n);
  7898.                     p = tmpbuf;
  7899. #else
  7900.                     p = rcv_move;
  7901. #endif /* NOSPL */
  7902.                     debug(F111,"ftp get /move-to",p,0);
  7903.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7904.                     debug(F111,"doftpget /MOVE zrename",p,rc);
  7905.                     tlog(F110, (rc > -1) ?
  7906.                          " moved to" : " failed to move to", p, 0);
  7907.                 }
  7908.                 if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  7909.                     char * s = pv[SND_SRN].sval;
  7910.                     char * srvrn = pv[SND_SRN].sval;
  7911.                     char tmpbuf[CKMAXPATH+1];
  7912. #ifndef NOSPL
  7913.                     int y;              /* Pass it thru the evaluator */
  7914.                     extern int cmd_quoting; /* for \v(filename) */
  7915.                     debug(F111,"ftp get srv_renam",s,1);
  7916.  
  7917.                     if (cmd_quoting) {
  7918.                         y = CKMAXPATH;
  7919.                         s = (char *)tmpbuf;
  7920.                         zzstring(srvrn,&s,&y);
  7921.                         s = (char *)tmpbuf;
  7922.                     }
  7923. #endif /* NOSPL */
  7924.                     debug(F111,"ftp get srv_renam",s,1);
  7925.                     if (s) if (*s) {
  7926.                         int x;
  7927.                         x = ftp_rename(s2,s);
  7928.                         debug(F111,"ftp get ftp_rename",s2,x);
  7929.                         tlog(F110, (x > 0) ?
  7930.                              " renamed source file to" :
  7931.                              " failed to rename source file to",
  7932.                              s,
  7933.                              0
  7934.                              );
  7935.                         if (x < 1)
  7936.               return(-1);
  7937.                     }
  7938.                 }
  7939.             }
  7940.         }
  7941.         if (cancelfile)
  7942.           continue;
  7943.         if (rc < 0) {
  7944.             ftp_fai++;
  7945. #ifdef FTP_TIMEOUT
  7946.         debug(F101,"ftp get ftp_timed_out","",ftp_timed_out);
  7947.         if (ftp_timed_out) {
  7948.         status = 0;
  7949.                 ftscreen(SCR_EM,0,(CK_OFF_T)0,"GET timed out");
  7950.         }
  7951. #endif    /* FTP_TIMEOUT */
  7952.             if (geterror) {
  7953.                 status = 0;
  7954.                 ftscreen(SCR_EM,0,(CK_OFF_T)0,"Fatal download error");
  7955.                 done++;
  7956.             }
  7957.         }
  7958.     }
  7959. #ifdef DEBUG
  7960.     if (deblog) {
  7961.     debug(F101,"ftp get status","",status);
  7962.     debug(F101,"ftp get cancelgroup","",cancelgroup);
  7963.     debug(F101,"ftp get cancelfile","",cancelfile);
  7964.     debug(F101,"ftp get selected","",selected);
  7965.     debug(F101,"ftp get good","",good);
  7966.     }
  7967. #endif /* DEBUG */
  7968.  
  7969.     if (selected == 0) {        /* No files met selection criteria */
  7970.     status = 1;            /* which is a kind of success. */
  7971.     } else if (status > 0) {        /* Some files were selected */
  7972.         if (cancelgroup)        /* but MGET was canceled */
  7973.           status = 0;            /* so MGET failed */
  7974.         else if (cancelfile && good < 1) /* If file was canceled */
  7975.           status = 0;            /* MGET failed if it got no files */
  7976.     }
  7977.     success = status;
  7978.     x = success;
  7979.     debug(F101,"ftp get success","",success);
  7980.  
  7981.   xgetx:
  7982.     pipesend = pipesave;                /* Restore global pipe selection */
  7983.     if (fp_nml) {                       /* Close /NAMELIST */
  7984.         if (fp_nml != stdout)
  7985.           fclose(fp_nml);
  7986.         fp_nml = NULL;
  7987.     }
  7988.     if (
  7989. #ifdef COMMENT
  7990.     x > -1
  7991. #else
  7992.     success
  7993. #endif    /* COMMENT */
  7994.     ) {                /* Download successful */
  7995. #ifdef GFTIMER
  7996.         t1 = gmstimer();                /* End time */
  7997.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  7998.         if (!sec) sec = 0.001;
  7999.         fptsecs = sec;
  8000. #else
  8001.         sec = (t1 - t0) / 1000;
  8002.         if (!sec) sec = 1;
  8003. #endif /* GFTIMER */
  8004.         tfcps = (long) (tfc / sec);
  8005.         tsecs = (int)sec;
  8006.         lastxfer = W_FTP|W_RECV;
  8007.         xferstat = success;
  8008.     }
  8009.     if (dpyactive)
  8010.       ftscreen(success > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
  8011. #ifdef CK_TMPDIR
  8012.     if (f_tmpdir) {                     /* If we changed to download dir */
  8013.         zchdir((char *) savdir);        /* Go back where we came from */
  8014.         f_tmpdir = 0;
  8015.     }
  8016. #endif /* CK_TMPDIR */
  8017.  
  8018.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  8019.         if (pv[i].sval)
  8020.           free(pv[i].sval);
  8021.     }
  8022.     for (i = 0; i < mgetn; i++)         /* MGET list too */
  8023.       makestr(&(mgetlist[i]),NULL);
  8024.  
  8025.     if (cancelgroup)            /* Clear temp-file stack */
  8026.       mlsreset();
  8027.  
  8028.     ftreset();                          /* Undo switch effects */
  8029.     dpyactive = 0;
  8030.     return(x);
  8031. }
  8032.  
  8033. static struct keytab ftprmt[] = {
  8034.     { "cd",        XZCWD, 0 },
  8035.     { "cdup",      XZCDU, 0 },
  8036.     { "cwd",       XZCWD, CM_INV },
  8037.     { "delete",    XZDEL, 0 },
  8038.     { "directory", XZDIR, 0 },
  8039.     { "exit",      XZXIT, 0 },
  8040.     { "help",      XZHLP, 0 },
  8041.     { "login",     XZLGI, 0 },
  8042.     { "logout",    XZLGO, 0 },
  8043.     { "mkdir",     XZMKD, 0 },
  8044.     { "pwd",       XZPWD, 0 },
  8045.     { "rename",    XZREN, 0 },
  8046.     { "rmdir",     XZRMD, 0 },
  8047.     { "type",      XZTYP, 0 },
  8048.     { "", 0, 0 }
  8049. };
  8050. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  8051.  
  8052. int
  8053. doftpsite() {                /* Send a SITE command */
  8054.     int reply;
  8055.     char * s;
  8056.     int lcs = -1, rcs = -1;
  8057.     int save_vbm = ftp_vbm;
  8058.  
  8059. #ifndef NOCSETS
  8060.     if (ftp_xla) {
  8061.         lcs = ftp_csl;
  8062.         if (lcs < 0) lcs = fcharset;
  8063.         rcs = ftp_csx;
  8064.         if (rcs < 0) rcs = ftp_csr;
  8065.     }
  8066. #endif /* NOCSETS */
  8067.     if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  8068.       return(x);
  8069.     CHECKCONN();
  8070.     ckstrncpy(line,s,LINBUFSIZ);
  8071.     if (testing) printf(" ftp site \"%s\"...\n",line);
  8072.     if (!ftp_vbm)
  8073.         ftp_vbm = !ckstrcmp("HELP",line,4,0);
  8074.     if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  8075.     do {
  8076.         reply = getreply(0,lcs,rcs,ftp_vbm,0);
  8077.     } while (reply == REPLY_PRELIM);
  8078.     }
  8079.     ftp_vbm = save_vbm;
  8080.     return(success = (reply == REPLY_COMPLETE));
  8081. }
  8082.  
  8083.  
  8084. int
  8085. dosetftppsv() {                /* Passive mode */
  8086.     x = seton(&ftp_psv);
  8087.     if (x > 0) passivemode = ftp_psv;
  8088.     return(x);
  8089. }
  8090.  
  8091. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  8092.  
  8093. int
  8094. doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  8095.     /* cx == 0 means REMOTE */
  8096.     /* cx != 0 is a XZxxx value */
  8097.     char * s;
  8098.  
  8099.     if (who != 0)
  8100.       return(0);
  8101.  
  8102.     if (cx == 0) {
  8103.         if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  8104.           return(x);
  8105.         cx = x;
  8106.     }
  8107.     switch (cx) {
  8108.       case XZCDU:                       /* CDUP */
  8109.         if ((x = cmcfm()) < 0) return(x);
  8110.         return(doftpcdup());
  8111.  
  8112.       case XZCWD:                       /* RCD */
  8113.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  8114.           return(x);
  8115.         ckstrncpy(line,s,LINBUFSIZ);
  8116.     s = brstrip(line);
  8117.         return(doftpcwd(s,1));
  8118.       case XZPWD:                       /* RPWD */
  8119.         return(doftppwd());
  8120.       case XZDEL:                       /* RDEL */
  8121.         return(doftpget(FTP_MDE,1));
  8122.       case XZDIR:                       /* RDIR */
  8123.         return(doftpdir(FTP_DIR));
  8124.       case XZHLP:                       /* RHELP */
  8125.         return(doftpxhlp());
  8126.       case XZMKD:                       /* RMKDIR */
  8127.         return(doftpmkd());
  8128.       case XZREN:                       /* RRENAME */
  8129.         return(doftpren());
  8130.       case XZRMD:                       /* RRMDIR */
  8131.         return(doftprmd());
  8132.       case XZLGO:                       /* LOGOUT */
  8133.         return(doftpres());
  8134.       case XZXIT:                       /* EXIT */
  8135.         return(ftpbye());
  8136.     }
  8137.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  8138.     return(-9);
  8139. }
  8140.  
  8141. int
  8142. doxftp() {                              /* Command parser for built-in FTP */
  8143.     int cx, n;
  8144.     struct FDB kw, fl;
  8145.     char * s;
  8146.     int usetls = 0;
  8147.     int lcs = -1, rcs = -1;
  8148.  
  8149. #ifndef NOCSETS
  8150.     if (ftp_xla) {
  8151.         lcs = ftp_csl;
  8152.         if (lcs < 0) lcs = fcharset;
  8153.         rcs = ftp_csx;
  8154.         if (rcs < 0) rcs = ftp_csr;
  8155.     }
  8156. #endif /* NOCSETS */
  8157.  
  8158.     if (inserver)                       /* FTP not allowed in IKSD. */
  8159.       return(-2);
  8160.  
  8161.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  8162.         ftp_typ = g_ftp_typ;
  8163.         /* g_ftp_typ = -1; */
  8164.     }
  8165. #ifdef COMMENT
  8166. /*
  8167.   We'll set the collision action locally in doftpget() based on whether
  8168.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  8169. */
  8170.     if (ftp_fnc < 0)                    /* Inherit global collision action */
  8171.       ftp_fnc = fncact;                 /* if none specified for FTP */
  8172. #endif /* COMMENT */
  8173.  
  8174.     /* Restore global verbose mode */
  8175.     if (ftp_deb)
  8176.       ftp_vbm = 1;
  8177.     else if (quiet)
  8178.       ftp_vbm = 0;
  8179.     else
  8180.       ftp_vbm = ftp_vbx;
  8181.  
  8182.     ftp_dates &= 1;            /* Undo any previous /UPDATE switch */
  8183.  
  8184.     dpyactive = 0;                      /* Reset global transfer-active flag */
  8185.     printlines = 0;                     /* Reset printlines */
  8186.  
  8187.     if (fp_nml) {                       /* Reset /NAMELIST */
  8188.         if (fp_nml != stdout)
  8189.           fclose(fp_nml);
  8190.         fp_nml = NULL;
  8191.     }
  8192.     makestr(&ftp_nml,NULL);
  8193.  
  8194.     cmfdbi(&kw,                         /* First FDB - commands */
  8195.            _CMKEY,                      /* fcode */
  8196.            "Hostname; or FTP command",  /* help */
  8197.            "",                          /* default */
  8198.            "",                          /* addtl string data */
  8199.            nftpcmd,                     /* addtl numeric data 1: tbl size */
  8200.            0,                           /* addtl numeric data 2: none */
  8201.            xxstring,                    /* Processing function */
  8202.            ftpcmdtab,                   /* Keyword table */
  8203.            &fl                          /* Pointer to next FDB */
  8204.            );
  8205.     cmfdbi(&fl,                         /* A host name or address */
  8206.            _CMFLD,                      /* fcode */
  8207.            "Hostname or address",       /* help */
  8208.            "",                          /* default */
  8209.            "",                          /* addtl string data */
  8210.            0,                           /* addtl numeric data 1 */
  8211.            0,                           /* addtl numeric data 2 */
  8212.            xxstring,
  8213.            NULL,
  8214.            NULL
  8215.            );
  8216.     x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
  8217.     if (x == -3) {
  8218.         printf("?ftp what? \"help ftp\" for hints\n");
  8219.         return(-9);
  8220.     }
  8221.     if (x < 0)
  8222.       return(x);
  8223.     if (cmresult.fcode == _CMFLD) {     /* If hostname */
  8224.         return(openftp(cmresult.sresult,0)); /* go open the connection */
  8225.     } else {
  8226.         cx = cmresult.nresult;
  8227.     }
  8228.     switch (cx) {
  8229.       case FTP_ACC:                     /* ACCOUNT */
  8230.         if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  8231.           return(x);
  8232.         CHECKCONN();
  8233.         makestr(&ftp_acc,s);
  8234.         if (testing)
  8235.           printf(" ftp account: \"%s\"\n",ftp_acc);
  8236.         success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  8237.         return(success);
  8238.  
  8239.       case FTP_GUP:                     /* Go UP */
  8240.         if ((x = cmcfm()) < 0) return(x);
  8241.         CHECKCONN();
  8242.         if (testing) printf(" ftp cd: \"(up)\"\n");
  8243.         return(success = doftpcdup());
  8244.  
  8245.       case FTP_CWD:                     /* CD */
  8246.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  8247.           return(x);
  8248.         CHECKCONN();
  8249.         ckstrncpy(line,s,LINBUFSIZ);
  8250.         if (testing)
  8251.           printf(" ftp cd: \"%s\"\n", line);
  8252.         return(success = doftpcwd(line,1));
  8253.  
  8254.       case FTP_CHM:                     /* CHMOD */
  8255.         if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  8256.           return(x);
  8257.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8258.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8259.           return(x);
  8260.         CHECKCONN();
  8261.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  8262.         if (testing)
  8263.           printf(" ftp chmod: %s\n",ftpcmdbuf);
  8264.         success =
  8265.           (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8266.         return(success);
  8267.  
  8268.       case FTP_CLS:                     /* CLOSE FTP connection */
  8269.         if ((y = cmcfm()) < 0)
  8270.           return(y);
  8271.         CHECKCONN();
  8272.         if (testing)
  8273.           printf(" ftp closing...\n");
  8274.         ftpclose();
  8275.         return(success = 1);
  8276.  
  8277.       case FTP_DIR:                     /* DIRECTORY of remote files */
  8278.       case FTP_VDI:
  8279.         return(doftpdir(cx));
  8280.  
  8281.       case FTP_GET:                     /* GET a remote file */
  8282.       case FTP_RGE:                     /* REGET */
  8283.       case FTP_MGE:                     /* MGET */
  8284.       case FTP_MDE:                     /* MDELETE */
  8285.         return(doftpget(cx,1));
  8286.  
  8287.       case FTP_IDL:                     /* IDLE */
  8288.         if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  8289.           return(x);
  8290.         if ((y = cmcfm()) < 0)
  8291.           return(y);
  8292.         CHECKCONN();
  8293.         if (z < 0)  {                   /* Display idle timeout */
  8294.             if (testing)
  8295.               printf(" ftp query idle timeout...\n");
  8296.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  8297.         } else {                        /* Set idle timeout */
  8298.             if (testing)
  8299.               printf(" ftp idle timeout set: %d...\n",z);
  8300.             success =
  8301.               (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  8302.         }
  8303.         return(success);
  8304.  
  8305.       case FTP_MKD:                     /* MKDIR */
  8306.         return(doftpmkd());
  8307.  
  8308.       case FTP_MOD:                     /* MODTIME */
  8309.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8310.           return(x);
  8311.         CHECKCONN();
  8312.         ckstrncpy(line,s,LINBUFSIZ);
  8313.         if (testing)
  8314.           printf(" ftp modtime \"%s\"...\n",line);
  8315.         success = 0;
  8316.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  8317.         success = 1;
  8318.         mdtmok = 1;
  8319.         if (!quiet) {
  8320.         int flag = 0;
  8321.         char c, * s;
  8322.         struct tm tmremote;
  8323.  
  8324.         bzero((char *)&tmremote, sizeof(struct tm));
  8325.         s = ftp_reply_str;
  8326.         while ((c = *s++)) {
  8327.             if (c == SP) {
  8328.             flag++;
  8329.             break;
  8330.             }
  8331.         }
  8332.         if (flag) {
  8333.             if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  8334.                    &tmremote.tm_year,
  8335.                    &tmremote.tm_mon,
  8336.                    &tmremote.tm_mday,
  8337.                    &tmremote.tm_hour,
  8338.                    &tmremote.tm_min,
  8339.                    &tmremote.tm_sec
  8340.                    ) == 6) {
  8341.             printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  8342.                    line,
  8343.                    tmremote.tm_year,
  8344.                    tmremote.tm_mon,
  8345.                    tmremote.tm_mday,
  8346.                    tmremote.tm_hour,
  8347.                    tmremote.tm_min,
  8348.                    tmremote.tm_sec
  8349.                    );
  8350.             } else {
  8351.             success = 0;
  8352.             }
  8353.         }
  8354.         }
  8355.         }
  8356.         return(success);
  8357.  
  8358.       case FTP_OPN:                     /* OPEN connection */
  8359. #ifdef COMMENT
  8360.         x = cmfld("IP hostname or address","",&s,xxstring);
  8361.         if (x < 0) {
  8362.             success = 0;
  8363.             return(x);
  8364.         }
  8365.         ckstrncpy(line,s,LINBUFSIZ);
  8366.         s = line;
  8367.         return(openftp(s,0));
  8368. #else
  8369.         {                               /* OPEN connection */
  8370.             char name[TTNAMLEN+1], *p;
  8371.             extern int network;
  8372.             extern char ttname[];
  8373.             if (network)                /* If we have a current connection */
  8374.               ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  8375.             else
  8376.               *name = '\0';             /* as default host */
  8377.             for (p = name; *p; p++)     /* Remove ":service" from end. */
  8378.               if (*p == ':') { *p = '\0'; break; }
  8379. #ifndef USETLSTAB
  8380.             x = cmfld("IP hostname or address",name,&s,xxstring);
  8381. #else
  8382.             cmfdbi(&kw,                 /* First FDB - commands */
  8383.                    _CMKEY,              /* fcode */
  8384.                    "Hostname or switch", /* help */
  8385.                    "",                  /* default */
  8386.                    "",                  /* addtl string data */
  8387.                    ntlstab,             /* addtl numeric data 1: tbl size */
  8388.                    0,                   /* addtl numeric data 2: none */
  8389.                    xxstring,            /* Processing function */
  8390.                    tlstab,              /* Keyword table */
  8391.                    &fl                  /* Pointer to next FDB */
  8392.                    );
  8393.             cmfdbi(&fl,                 /* A host name or address */
  8394.                    _CMFLD,              /* fcode */
  8395.                    "Hostname or address", /* help */
  8396.                    "",                  /* default */
  8397.                    "",                  /* addtl string data */
  8398.                    0,                   /* addtl numeric data 1 */
  8399.                    0,                   /* addtl numeric data 2 */
  8400.                    xxstring,
  8401.                    NULL,
  8402.                    NULL
  8403.                    );
  8404.  
  8405.             for (n = 0;; n++) {
  8406.                 x = cmfdb(&kw);         /* Parse a hostname or a keyword */
  8407.                 if (x == -3) {
  8408.                   printf("?ftp open what? \"help ftp\" for hints\n");
  8409.                   return(-9);
  8410.                 }
  8411.                 if (x < 0)
  8412.                   break;
  8413.                 if (cmresult.fcode == _CMFLD) { /* Hostname */
  8414.                     s = cmresult.sresult;
  8415.                     break;
  8416.                 } else if (cmresult.nresult == OPN_TLS) {
  8417.                     usetls = 1;
  8418.                 }
  8419.             }
  8420. #endif /* USETLSTAB */
  8421.             if (x < 0) {
  8422.                 success = 0;
  8423.                 return(x);
  8424.             }
  8425.             ckstrncpy(line,s,LINBUFSIZ);
  8426.             s = line;
  8427.             return(openftp(s,usetls));
  8428.         }
  8429. #endif /* COMMENT */
  8430.  
  8431.       case FTP_PUT:                     /* PUT */
  8432.       case FTP_MPU:                     /* MPUT */
  8433.       case FTP_APP:                     /* APPEND */
  8434.       case FTP_REP:            /* REPUT */
  8435.         return(doftpput(cx,1));
  8436.  
  8437.       case FTP_PWD:                     /* PWD */
  8438.         x = doftppwd();
  8439.         if (x > -1) success = x;
  8440.         return(x);
  8441.  
  8442.       case FTP_REN:                     /* RENAME */
  8443.         return(doftpren());
  8444.  
  8445.       case FTP_RES:                     /* RESET */
  8446.         return(doftpres());
  8447.  
  8448.       case FTP_HLP:                     /* (remote) HELP */
  8449.         return(doftpxhlp());
  8450.  
  8451.       case FTP_RMD:                     /* RMDIR */
  8452.         return(doftprmd());
  8453.  
  8454.       case FTP_STA:                     /* STATUS */
  8455.         if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  8456.           return(x);
  8457.         CHECKCONN();
  8458.         ckstrncpy(line,s,LINBUFSIZ);
  8459.         if (testing) printf(" ftp status \"%s\"...\n",line);
  8460.         success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  8461.         return(success);
  8462.  
  8463.       case FTP_SIT: {                   /* SITE */
  8464.       return(doftpsite());
  8465.       }
  8466.  
  8467.       case FTP_SIZ:                     /* (ask for) SIZE */
  8468.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8469.           return(x);
  8470.         CHECKCONN();
  8471.         ckstrncpy(line,s,LINBUFSIZ);
  8472.         if (testing)
  8473.           printf(" ftp size \"%s\"...\n",line);
  8474.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  8475.     if (success)
  8476.       sizeok = 1;
  8477.         return(success);
  8478.  
  8479.       case FTP_SYS:                     /* Ask for server's SYSTEM type */
  8480.         if ((x = cmcfm()) < 0) return(x);
  8481.         CHECKCONN();
  8482.         if (testing)
  8483.           printf(" ftp system...\n");
  8484.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  8485.         return(success);
  8486.  
  8487.       case FTP_UMA:                     /* Set/query UMASK */
  8488.         if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  8489.           if (x != -3)
  8490.             return(x);
  8491.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8492.         if ((x = cmcfm()) < 0) return(x);
  8493.         CHECKCONN();
  8494.         if (testing) {
  8495.             if (tmpbuf[0])
  8496.               printf(" ftp umask \"%s\"...\n",tmpbuf);
  8497.             else
  8498.               printf(" ftp query umask...\n");
  8499.         }
  8500.         success = ftp_umask(tmpbuf);
  8501.         return(success);
  8502.  
  8503.       case FTP_USR:
  8504.         return(doftpusr());
  8505.  
  8506.       case FTP_QUO:
  8507.         if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  8508.           return(x);
  8509.         CHECKCONN();
  8510.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  8511.         return(success);
  8512.  
  8513.       case FTP_TYP:                     /* Type */
  8514.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  8515.           return(x);
  8516.         if ((y = cmcfm()) < 0) return(y);
  8517.         CHECKCONN();
  8518.         ftp_typ = x;
  8519.         g_ftp_typ = x;
  8520.         tenex = (ftp_typ == FTT_TEN);
  8521.         changetype(ftp_typ,ftp_vbm);
  8522.         return(1);
  8523.  
  8524.       case FTP_CHK:                     /* Check if remote file(s) exist(s) */
  8525.         if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  8526.           return(x);
  8527.         CHECKCONN();
  8528.         success = remote_files(1,(CHAR *)s,(CHAR *)s,0) ? 1 : 0;
  8529.         return(success);
  8530.  
  8531.       case FTP_FEA:                     /* RFC2389 */
  8532.         if ((y = cmcfm()) < 0)
  8533.           return(y);
  8534.         CHECKCONN();
  8535.     success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE);
  8536.     if (success) {
  8537.         if (sfttab[0] > 0) {
  8538.         ftp_aut = sfttab[SFT_AUTH];
  8539.         sizeok  = sfttab[SFT_SIZE];
  8540.         mdtmok  = sfttab[SFT_MDTM];
  8541.         mlstok  = sfttab[SFT_MLST];
  8542.         }
  8543.     }
  8544.     return(success);
  8545.  
  8546.       case FTP_OPT:                     /* RFC2389 */
  8547.         /* Perhaps this should be a keyword list... */
  8548.         if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  8549.           return(x);
  8550.         CHECKCONN();
  8551.         ckstrncpy(line,s,LINBUFSIZ);
  8552.         if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  8553.           return(x);
  8554.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8555.         return(success);
  8556.  
  8557.       case FTP_ENA:            /* FTP ENABLE */
  8558.       case FTP_DIS:            /* FTP DISABLE */
  8559.         if ((x = cmkey(ftpenatab,nftpena,"","",xxstring)) < 0)
  8560.           return(x);
  8561.         if ((y = cmcfm()) < 0) return(y);
  8562.     switch (x) {
  8563.       case ENA_AUTH:        /* OK to use autoauthentication */
  8564.         ftp_aut = (cx == FTP_ENA) ? 1 : 0;
  8565.         sfttab[SFT_AUTH] = ftp_aut;
  8566.         break;
  8567.       case ENA_FEAT:        /* OK to send FEAT command */
  8568.         featok = (cx == FTP_ENA) ? 1 : 0;
  8569.         break;
  8570.       case ENA_MLST:        /* OK to use MLST/MLSD */
  8571.         mlstok = (cx == FTP_ENA) ? 1 : 0;
  8572.         sfttab[SFT_MLST] = mlstok;
  8573.         break;
  8574.       case ENA_MDTM:        /* OK to use MDTM */
  8575.         mdtmok = (cx == FTP_ENA) ? 1 : 0;
  8576.         sfttab[SFT_MDTM] = mdtmok;
  8577.         break;
  8578.       case ENA_SIZE:        /* OK to use SIZE */
  8579.         sizeok = (cx == FTP_ENA) ? 1 : 0;
  8580.         sfttab[SFT_SIZE] = sizeok;
  8581.         break;
  8582.     }
  8583.     return(success = 1);
  8584.     }
  8585.     return(-2);
  8586. }
  8587.  
  8588. #ifndef NOSHOW
  8589. static char *
  8590. shopl(x) int x; {
  8591.     switch (x) {
  8592.       case FPL_CLR: return("clear");
  8593.       case FPL_PRV: return("private");
  8594.       case FPL_SAF: return("safe");
  8595.       case 0:  return("(not set)");
  8596.       default: return("(unknown)");
  8597.     }
  8598. }
  8599.  
  8600. int
  8601. shoftp(brief) int brief; {
  8602.     char * s = "?";
  8603.     int n, x;
  8604.  
  8605.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  8606.         ftp_typ = g_ftp_typ;
  8607.         /* g_ftp_typ = -1; */
  8608.     }
  8609.     printf("\n");
  8610.     printf("FTP connection:                 %s\n",connected ?
  8611.            ftp_host :
  8612.            "(none)"
  8613.            );
  8614.     n = 2;
  8615.     if (connected) {
  8616.         n++;
  8617.         printf("FTP server type:                %s\n",
  8618.                ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  8619.     }
  8620.     if (loggedin)
  8621.       printf("Logged in as:                   %s\n",
  8622.              strval(ftp_logname,"(unknown)"));
  8623.     else
  8624.       printf("Not logged in\n");
  8625.     n++;
  8626.     if (brief) return(0);
  8627.  
  8628.     printf("\nSET FTP values:\n\n");
  8629.     n += 3;
  8630.  
  8631.     printf(" ftp anonymous-password:        %s\n",
  8632.        ftp_apw ? ftp_apw : "(default)"
  8633.        );
  8634.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  8635.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  8636.     switch (ftp_typ) {
  8637.       case FTT_ASC: s = "text"; break;
  8638.       case FTT_BIN: s = "binary"; break;
  8639.       case FTT_TEN: s = "tenex"; break;
  8640.     }
  8641. #ifdef FTP_TIMEOUT
  8642.     printf(" ftp timeout:                   %ld\n",ftp_timeout);
  8643. #endif    /* FTP_TIMEOUT */
  8644.     printf(" ftp type:                      %s\n",s);
  8645.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  8646.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  8647.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  8648.     printf(" ftp filenames:                 %s\n",
  8649.            ftp_cnv == CNV_AUTO ? "auto" : (ftp_cnv ? "converted" : "literal")
  8650.            );
  8651.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  8652.  
  8653.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  8654.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  8655.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  8656.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  8657.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  8658. #ifdef COMMENT
  8659.     /* See note in doxftp() */
  8660.     if (ftp_fnc < 0)
  8661.       ftp_fnc = fncact;
  8662. #endif /* COMMENT */
  8663.     printf(" ftp collision:                 %s\n",
  8664.        fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  8665.     printf(" ftp server-time-offset:        %s\n",
  8666.        fts_sto ? fts_sto : "(none)");
  8667.     n += 15;
  8668.  
  8669. #ifndef NOCSETS
  8670.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  8671.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8672.  
  8673.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  8674.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8675.  
  8676.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  8677.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8678. #endif /* NOCSETS */
  8679.  
  8680.     x = ftp_dis;
  8681.     if (x < 0)
  8682.       x = fdispla;
  8683.     switch (x) {
  8684.       case XYFD_N: s = "none"; break;
  8685.       case XYFD_R: s = "serial"; break;
  8686.       case XYFD_C: s = "fullscreen"; break;
  8687.       case XYFD_S: s = "crt"; break;
  8688.       case XYFD_B: s = "brief"; break;
  8689.     }
  8690.     printf(" ftp display:                   %s\n",s);
  8691.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8692.  
  8693.     if (mlstok || featok || mdtmok || sizeok || ftp_aut) {
  8694.     printf(" enabled:                      ");
  8695.     if (ftp_aut) printf(" AUTH");
  8696.     if (featok)  printf(" FEAT");
  8697.     if (mdtmok)  printf(" MDTM");
  8698.     if (mlstok)  printf(" MLST");
  8699.     if (sizeok)  printf(" SIZE");
  8700.     printf("\n");
  8701.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8702.     }
  8703.     if (!mlstok || !featok || !mdtmok || !sizeok || !ftp_aut) {
  8704.     printf(" disabled:                     ");
  8705.     if (!ftp_aut) printf(" AUTH");
  8706.     if (!featok)  printf(" FEAT");
  8707.     if (!mdtmok)  printf(" MDTM");
  8708.     if (!mlstok)  printf(" MLST");
  8709.     if (!sizeok)  printf(" SIZE");
  8710.     printf("\n");
  8711.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8712.     }
  8713.     switch (ftpget) {
  8714.       case 0: s = "kermit"; break;
  8715.       case 1: s = "ftp"; break;
  8716.       case 2: s = "auto"; break;
  8717.       default: s = "?";
  8718.     }
  8719.     printf(" get-put-remote:                %s\n",s);
  8720.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8721.  
  8722.     printf("\n");
  8723.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8724.  
  8725. #ifdef FTP_SECURITY
  8726.     printf("Available security methods:    ");
  8727. #ifdef FTP_GSSAPI
  8728.     printf("GSSAPI ");
  8729. #endif /* FTP_GSSAPI */
  8730. #ifdef FTP_KRB4
  8731.     printf("Kerberos4 ");
  8732. #endif /* FTP_KRB4 */
  8733. #ifdef FTP_SRP
  8734.     printf("SRP ");
  8735. #endif /* FTP_SRP */
  8736. #ifdef FTP_SSL
  8737.     printf("SSL ");
  8738. #endif /* FTP_SSL */
  8739.  
  8740.     n++;
  8741.     printf("\n\n");
  8742.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8743.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  8744.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8745.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  8746.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8747.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  8748.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8749.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  8750.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8751.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  8752.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8753.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  8754.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8755. #else
  8756.     printf("Available security methods:     (none)\n");
  8757.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8758. #endif /* FTP_SECURITY */
  8759.  
  8760.     if (n <= cmd_rows - 3)
  8761.       printf("\n");
  8762.     return(0);
  8763. }
  8764. #endif /* NOSHOW */
  8765.  
  8766. #ifndef NOHELP
  8767. /* FTP HELP text strings */
  8768.  
  8769. static char * fhs_ftp[] = {
  8770.     "Syntax: FTP subcommand [ operands ]",
  8771.     "  Makes an FTP connection, or sends a command to the FTP server.",
  8772.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  8773.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  8774.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  8775.     ""
  8776. };
  8777.  
  8778. static char * fhs_acc[] = {             /* ACCOUNT */
  8779.     "Syntax: FTP ACCOUNT text",
  8780.     "  Sends an account designator to an FTP server that needs one.",
  8781.     "  Most FTP servers do not use accounts; some use them for other",
  8782.     "  other purposes, such as disk-access passwords.",
  8783.     ""
  8784. };
  8785. static char * fhs_app[] = {             /* APPEND */
  8786.     "Syntax: FTP APPEND filname",
  8787.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  8788.     ""
  8789. };
  8790. static char * fhs_cls[] = {             /* BYE, CLOSE */
  8791.     "Syntax: [ FTP ] BYE",
  8792.     "  Logs out from the FTP server and closes the FTP connection.",
  8793.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  8794.     ""
  8795. };
  8796. static char * fhs_cwd[] = {             /* CD, CWD */
  8797.     "Syntax: [ FTP ] CD directory",
  8798.     "  Asks the FTP server to change to the given directory.",
  8799.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  8800.     ""
  8801. };
  8802. static char * fhs_gup[] = {             /* CDUP, UP */
  8803.     "Syntax: FTP CDUP",
  8804.     "  Asks the FTP server to change to the parent directory of its current",
  8805.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  8806.     ""
  8807. };
  8808. static char * fhs_chm[] = {             /* CHMOD */
  8809.     "Syntax: FTP CHMOD filename permissions",
  8810.     "  Asks the FTP server to change the permissions, protection, or mode of",
  8811.     "  the given file.  The given permissions must be in the syntax of the",
  8812.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  8813.     "  FTP PUT /PERMISSIONS",
  8814.     ""
  8815. };
  8816. static char * fhs_mde[] = {             /* DELETE */
  8817.     "Syntax: FTP DELETE [ switches ] filespec",
  8818.     "  Asks the FTP server to delete the given file or files.",
  8819.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  8820.     "  multiple file deletion).  Optional switches:",
  8821.     " ",
  8822.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8823.     "  /EXCEPT:pattern",
  8824.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  8825.     "  /LARGER-THAN:number",
  8826. #ifdef UNIXOROSK
  8827.     "  /NODOTFILES",
  8828. #endif /* UNIXOROSK */
  8829.     "  /QUIET",
  8830. #ifdef RECURSIVE
  8831.     "  /RECURSIVE (depends on server)",
  8832.     "  /SUBDIRECTORIES",
  8833. #endif /* RECURSIVE */
  8834.     "  /SMALLER-THAN:number",
  8835.     ""
  8836. };
  8837. static char * fhs_dir[] = {             /* DIRECTORY */
  8838.     "Syntax: FTP DIRECTORY [ filespec ]",
  8839.     "  Asks the server to send a directory listing of the files that match",
  8840.     "  the given filespec, or if none is given, all the files in its current",
  8841.     "  directory.  The filespec, including any wildcards, must be in the",
  8842.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  8843.     "  Synonym: RDIRECTORY.",
  8844.     ""
  8845. };
  8846. static char * fhs_vdi[] = {             /* VDIRECTORY */
  8847.     "Syntax: FTP VDIRECTORY [ filespec ]",
  8848.     "  Asks the server to send a directory listing of the files that match",
  8849.     "  the given filespec, or if none is given, all the files in its current",
  8850.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  8851.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  8852.     "  FTP DIRECTORY lists only filenames without details.",
  8853.     ""
  8854. };
  8855. static char * fhs_fea[] = {             /* FEATURES */
  8856.     "Syntax: FTP FEATURES",
  8857.     "  Asks the FTP server to list its special features.  Most FTP servers",
  8858.     "  do not recognize this command.",
  8859.     ""
  8860. };
  8861. static char * fhs_mge[] = {             /* MGET */
  8862.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  8863.     "  Download a single file or multiple files.  Asks the FTP server to send",
  8864.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  8865.     " ",
  8866.     "  /AS-NAME:text",
  8867.     "    Name under which to store incoming file.",
  8868.     "    Pattern required for for multiple files.",
  8869.     "  /BINARY",                        /* /IMAGE */
  8870.     "    Force binary mode.  Synonym: /IMAGE.",
  8871.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  8872.    "    What to do if an incoming file has the same name as an existing file.",
  8873.  
  8874. #ifdef PUTPIPE
  8875.     "  /COMMAND",
  8876.     "    Specifies that the as-name is a command to which the incoming file",
  8877.     "    is to be piped as standard input.",
  8878. #endif /* PUTPIPE */
  8879.  
  8880. #ifdef DOUPDATE
  8881.     "  /DATES-DIFFER",
  8882.     "    Download only those files whose modification date-times differ from",
  8883.     "    those of the corresponding local files, or that do not already",
  8884.     "    exist on the local computer.",
  8885. #endif /* DOUPDATE */
  8886.  
  8887.     "  /DELETE",
  8888.     "    Specifies that each file is to be deleted from the server after,",
  8889.     "    and only if, it is successfully downloaded.",
  8890.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8891.     "    When downloading a group of files, what to do upon failure to",
  8892.     "    transfer a file: quit or proceed to the next one.",
  8893.     "  /EXCEPT:pattern",
  8894.     "    Exception list: don't download any files that match this pattern.",
  8895.     "    See HELP WILDCARD for pattern syntax.",
  8896.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8897.     "    Whether to convert incoming filenames to local syntax.",
  8898. #ifdef PIPESEND
  8899. #ifndef NOSPL
  8900.     "  /FILTER:command",
  8901.     "    Pass incoming files through the given command.",
  8902. #endif /* NOSPL */
  8903. #endif /* PIPESEND */
  8904.     "  /LARGER-THAN:number",
  8905.     "    Only download files that are larger than the given number of bytes.",
  8906.     "  /LISTFILE:filename",
  8907.     "    Obtain the list of files to download from the given file.",
  8908. #ifndef NOCSETS
  8909.     "  /LOCAL-CHARACTER-SET:name",
  8910.     "    When downloading in text mode and character-set conversion is",
  8911.     "    desired, this specifies the target set.",
  8912. #endif /* NOCSETS */
  8913.     "  /MATCH:pattern",
  8914.     "    Specifies a pattern to be used to select filenames locally from the",
  8915.     "    server's list.",
  8916.     "  /MLSD",
  8917.     "    Forces sending of MLSD (rather than NLST) to get the file list.",
  8918. #ifdef CK_TMPDIR
  8919.     "  /MOVE-TO:directory",
  8920.     "    Each file that is downloaded is to be moved to the given local",
  8921.     "    directory immediately after, and only if, it has been received",
  8922.     "    successfully.",
  8923. #endif /* CK_TMPDIR */
  8924.     "  /NAMELIST:filename",
  8925.     "    Instead of downloading the files, stores the list of files that",
  8926.     "    would be downloaded in the given local file, one filename per line.",
  8927.     "  /NLST",
  8928.     "    Forces sending of NLST (rather than MLSD) to get the file list.",
  8929.     "  /NOBACKUPFILES",
  8930.     "    Don't download any files whose names end with .~<number>~.",
  8931.     "  /NODOTFILES",
  8932.     "    Don't download any files whose names begin with period (.).",
  8933.     "  /QUIET",
  8934.     "    Suppress the file-transfer display.",
  8935. #ifdef FTP_RESTART
  8936.     "  /RECOVER",                       /* /RESTART */
  8937.     "    Resume a download that was previously interrupted from the point of",
  8938.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  8939.     "    Synonym: /RESTART.",
  8940. #endif /* FTP_RESTART */
  8941. #ifdef RECURSIVE
  8942.     "  /RECURSIVE",                     /* /SUBDIRECTORIES */
  8943.     "    Create subdirectories automatically if the server sends files",
  8944.     "    recursively and includes pathnames (most don't).",
  8945. #endif /* RECURSIVE */
  8946.     "  /RENAME-TO:text",
  8947.     "    Each file that is downloaded is to be renamed as indicated just,",
  8948.     "    after, and only if, it has arrived successfully.",
  8949. #ifndef NOCSETS
  8950.     "  /SERVER-CHARACTER-SET:name",
  8951.     "    When downloading in text mode and character-set conversion is desired"
  8952. ,   "    this specifies the original file's character set on the server.",
  8953. #endif /* NOCSETS */
  8954.     "  /SERVER-RENAME:text",
  8955.     "    Each server source file is to be renamed on the server as indicated",
  8956.     "    immediately after, but only if, it has arrived succesfully.",
  8957.     "  /SMALLER-THAN:number",
  8958.     "    Download only those files smaller than the given number of bytes.",
  8959.     "  /TEXT",                          /* /ASCII */
  8960.     "    Force text mode.  Synonym: /ASCII.",
  8961.     "  /TENEX",
  8962.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8963. #ifndef NOCSETS
  8964.     "  /TRANSPARENT",
  8965.     "    When downloading in text mode, do not convert chracter-sets.",
  8966. #endif /* NOCSETS */
  8967.     "  /TO-SCREEN",
  8968.     "    The downloaded file is to be displayed on the screen.",
  8969. #ifdef DOUPDATE
  8970.     "  /UPDATE",
  8971.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  8972.     "    newer than than their local counterparts, or that do not exist on",
  8973.     "    the local computer.",
  8974. #endif /* DOUPDATE */
  8975.     ""
  8976. };
  8977. static char * fhs_hlp[] = {             /* HELP */
  8978.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  8979.     "  Asks the FTP server for help about the given command.  First use",
  8980.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  8981.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  8982.     ""
  8983. };
  8984. static char * fhs_idl[] = {             /* IDLE */
  8985.     "Syntax: FTP IDLE [ number ]",
  8986.     "  If given without a number, this asks the FTP server to tell its",
  8987.     "  current idle-time limit.  If given with a number, it asks the server",
  8988.     "  to change its idle-time limit to the given number of seconds.",
  8989.     ""
  8990. };
  8991. static char * fhs_usr[] = {             /* USER, LOGIN */
  8992.     "Syntax: FTP USER username [ password [ account ] ]",
  8993.     "  Log in to the FTP server.  To be used when connected but not yet",
  8994.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  8995.     "  If you omit the password, and one is required by the server, you are",
  8996.     "  prompted for it.  If you omit the account, no account is sent.",
  8997.     "  Synonym: FTP LOGIN.",
  8998.     ""
  8999. };
  9000. static char * fhs_get[] = {             /* GET */
  9001.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  9002.     "  Download a single file.  Asks the FTP server to send the given file.",
  9003.     "  The optional as-name is the name to store it under when it arrives;",
  9004.     "  if omitted, the file is stored with the name it arrived with, as",
  9005.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  9006.     "  value.  Aside from the file list and as-name, syntax and options are",
  9007.     "  the same as for FTP MGET, which is used for downloading multiple files."
  9008. ,   ""
  9009. };
  9010. static char * fhs_mkd[] = {             /* MKDIR */
  9011.     "Syntax: FTP MKDIR directory",
  9012.     "  Asks the FTP server to create a directory with the given name,",
  9013.     "  which must be in the syntax of the server's file system.  Synonyms:",
  9014.     "  REMOTE MKDIR, RMKDIR.",
  9015.     ""
  9016. };
  9017. static char * fhs_mod[] = {             /* MODTIME */
  9018.     "Syntax: FTP MODTIME filename",
  9019.     "  Asks the FTP server to send the modification time of the given file,",
  9020.     "  to be displayed on the screen.  The date-time format is all numeric:",
  9021.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  9022.     "  fractions of seconds).",
  9023.     ""
  9024. };
  9025. static char * fhs_mpu[] = {             /* MPUT */
  9026.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  9027.     "  Uploads files.  Sends the given file or files to the FTP server.",
  9028.     "  Also see FTP PUT.  Optional switches are:",
  9029.     " ",
  9030.     "  /AFTER:date-time",
  9031.     "    Uploads only those files newer than the given date-time.",
  9032.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  9033. #ifdef PUTARRAY
  9034.     "  /ARRAY:array-designator",
  9035.     "    Tells Kermit to upload the contents of the given array, rather than",
  9036.     "    a file.",
  9037. #endif /* PUTARRAY */
  9038.     "  /AS-NAME:text",
  9039.     "    Name under which to send files.",
  9040.     "    Pattern required for for multiple files.",
  9041.     "  /BEFORE:date-time",
  9042.     "    Upload only those files older than the given date-time.",
  9043.     "  /BINARY",
  9044.     "    Force binary mode.  Synonym: /IMAGE.",
  9045. #ifdef PUTPIPE
  9046.     "  /COMMAND",
  9047.     "    Specifies that the filespec is a command whose standard output is",
  9048.     "    to be sent.",
  9049. #endif /* PUTPIPE */
  9050.  
  9051. #ifdef COMMENT
  9052. #ifdef DOUPDATE
  9053.     "  /DATES-DIFFER",
  9054.     "    Upload only those files whose modification date-times differ from",
  9055.     "    those on the server, or that don't exist on the server at all.",
  9056. #endif /* DOUPDATE */
  9057. #endif /* COMMENT */
  9058.  
  9059.     "  /DELETE",
  9060.     "    Specifies that each source file is to be deleted after, and only if,",
  9061.     "    it is successfully uploaded.",
  9062.     "  /DOTFILES",
  9063.     "    Include files whose names begin with period (.).",
  9064.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  9065.     "    When uploading a group of files, what to do upon failure to",
  9066.     "    transfer a file: quit or proceed to the next one.",
  9067.     "  /EXCEPT:pattern",
  9068.     "    Exception list: don't upload any files that match this pattern.",
  9069.     "    See HELP WILDCARD for pattern syntax.",
  9070.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  9071.     "    Whether to convert outbound filenames to common syntax.",
  9072. #ifdef PIPESEND
  9073. #ifndef NOSPL
  9074.     "  /FILTER:command",
  9075.     "    Pass outbound files through the given command.",
  9076. #endif /* NOSPL */
  9077. #endif /* PIPESEND */
  9078. #ifdef CKSYMLINK
  9079.     "  /FOLLOWINKS",
  9080.     "    Send files that are pointed to by symbolic links.",
  9081.     "  /NOFOLLOWINKS",
  9082.     "    Skip over symbolic links (default).",
  9083. #endif /* CKSYMLINK */
  9084.     "  /LARGER-THAN:number",
  9085.     "    Only upload files that are larger than the given number of bytes.",
  9086.     "  /LISTFILE:filename",
  9087.     "    Obtain the list of files to upload from the given file.",
  9088. #ifndef NOCSETS
  9089.     "  /LOCAL-CHARACTER-SET:name",
  9090.     "    When uploading in text mode and character-set conversion is",
  9091.     "    desired, this specifies the source-file character set.",
  9092. #endif /* NOCSETS */
  9093. #ifdef CK_TMPDIR
  9094.     "  /MOVE-TO:directory",
  9095.     "    Each source file that is uploaded is to be moved to the given local",
  9096.     "    directory when, and only if, the transfer is successful.",
  9097. #endif /* CK_TMPDIR */
  9098.     "  /NOBACKUPFILES",
  9099.     "    Don't upload any files whose names end with .~<number>~.",
  9100. #ifdef UNIXOROSK
  9101.     "  /NODOTFILES",
  9102.     "    Don't upload any files whose names begin with period (.).",
  9103. #endif /* UNIXOROSK */
  9104.     "  /NOT-AFTER:date-time",
  9105.     "    Upload only files that are not newer than the given date-time",
  9106.     "  /NOT-BEFORE:date-time",
  9107.     "    Upload only files that are not older than the given date-time",
  9108. #ifdef UNIX
  9109.     "  /PERMISSIONS",
  9110.     "    Ask the server to set the permissions of each file it receives",
  9111.     "    according to the source file's permissions.",
  9112. #endif /* UNIX */
  9113.     "  /QUIET",
  9114.     "    Suppress the file-transfer display.",
  9115. #ifdef FTP_RESTART
  9116.     "  /RECOVER",
  9117.     "    Resume an upload that was previously interrupted from the point of",
  9118.     "    failure.  Synonym: /RESTART.",
  9119. #endif /* FTP_RESTART */
  9120. #ifdef RECURSIVE
  9121.     "  /RECURSIVE",
  9122.     "    Send files from the given directory and all the directories beneath",
  9123.     "    it.  Synonym: /SUBDIRECTORIES.",
  9124. #endif /* RECURSIVE */
  9125.     "  /RENAME-TO:text",
  9126.     "    Each source file that is uploaded is to be renamed on the local",
  9127.     "    local computer as indicated when and only if, the transfer completes",
  9128.     "    successfully.",
  9129. #ifndef NOCSETS
  9130.     "  /SERVER-CHARACTER-SET:name",
  9131.     "    When uploading in text mode and character-set conversion is desired,",
  9132.     "    this specifies the character set to which the file should be",
  9133.     "    converted for storage on the server.",
  9134. #endif /* NOCSETS */
  9135.     "  /SERVER-RENAME:text",
  9136.     "    Each file that is uploaded is to be renamed as indicated on the",
  9137.     "    server after, and only if, if arrives successfully.",
  9138.     "  /SIMULATE",
  9139.     "    Show which files would be sent without actually sending them.",
  9140.     "  /SMALLER-THAN:number",
  9141.     "    Upload only those files smaller than the given number of bytes.",
  9142.     "  /TEXT",
  9143.     "    Force text mode.  Synonym: /ASCII.",
  9144.     "  /TENEX",
  9145.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  9146. #ifndef NOCSETS
  9147.     "  /TRANSPARENT",
  9148.     "    When uploading in text mode, do not convert chracter-sets.",
  9149. #endif /* NOCSETS */
  9150.     "  /TYPE:{TEXT,BINARY}",
  9151.     "    Upload only files of the given type.",
  9152. #ifdef DOUPDATE
  9153.     "  /UPDATE",
  9154.     "    If a file of the same name exists on the server, upload only if",
  9155.     "    the local file is newer.",
  9156. #endif /* DOUPDATE */
  9157.     "  /UNIQUE-SERVER-NAMES",
  9158.     "    Ask the server to compute new names for any incoming file that has",
  9159.     "    the same name as an existing file.",
  9160.     ""
  9161. };
  9162. static char * fhs_opn[] = {             /* OPEN */
  9163. #ifdef CK_SSL
  9164.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  9165.     "  Opens a connection to the FTP server on the given host.  The default",
  9166.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  9167.     "  can be supplied if necessary.  Optional switches are:",
  9168. #else /* CK_SSL */
  9169.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  9170.     "  Opens a connection to the FTP server on the given host.  The default",
  9171.     "  TCP port is 21, but a different port number can be supplied if",
  9172.     "  necessary.  Optional switches are:",
  9173. #endif /* CK_SSL */
  9174.     " ",
  9175.     "  /ANONYMOUS",
  9176.     "    Logs you in anonymously.",
  9177.     "  /USER:text",
  9178.     "    Supplies the given text as your username.",
  9179.     "  /PASSWORD:text",
  9180.     "    Supplies the given text as your password.  If you include a username",
  9181.     "    but omit this switch and the server requires a password, you are",
  9182.     "    prompted for it.",
  9183.     "  /ACCOUNT:text",
  9184.     "    Supplies the given text as your account, if required by the server.",
  9185.     "  /ACTIVE",
  9186.     "    Forces an active (rather than passive) connection.",
  9187.     "  /PASSIVE",
  9188.     "    Forces a passive (rather than active) connection.",
  9189.     "  /NOINIT",
  9190.     "    Inhibits sending initial REST, STRU, and MODE commands, which are",
  9191.     "    well-known standard commands, but to which some servers react badly.",
  9192.     "  /NOLOGIN",
  9193.     "    Inhibits autologin for this connection only.",
  9194.     ""
  9195. };
  9196. static char * fhs_opt[] = {             /* OPTS, OPTIONS */
  9197.     "Syntax: FTP OPTIONS",
  9198.     "  Asks the FTP server to list its current options.  Advanced, new,",
  9199.     "  not supported by most FTP servers.",
  9200.     ""
  9201. };
  9202. static char * fhs_put[] = {             /* PUT, SEND */
  9203.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  9204.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  9205.     "  by an additional field, this is interpreted as the name under which",
  9206.     "  to send the file or files.  See HELP FTP MPUT.",
  9207.     ""
  9208. };
  9209. static char * fhs_reput[] = {        /* REPUT, RESEND */
  9210.     "Syntax: [ FTP ] REPUT [ switches ] filespec [ as-name ]",
  9211.     "  Synonym for FTP PUT /RECOVER.  Recovers an interrupted binary-mode",
  9212.     "  upload from the point of failure if the FTP server supports recovery.",
  9213.     "  Synonym: [ FTP ] RESEND.  For details see HELP FTP MPUT.",
  9214.     ""
  9215. };
  9216. static char * fhs_pwd[] = {             /* PWD */
  9217.     "Syntax: FTP PWD",
  9218.     "  Asks the FTP server to reveal its current working directory.",
  9219.     "  Synonyms: REMOTE PWD, RPWD.",
  9220.     ""
  9221. };
  9222. static char * fhs_quo[] = {             /* QUOTE */
  9223.     "Syntax: FTP QUOTE text",
  9224.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  9225.     "  for sending commands that Kermit might not support.",
  9226.     ""
  9227. };
  9228. static char * fhs_rge[] = {             /* REGET */
  9229.     "Syntax: FTP REGET",
  9230.     "  Synonym for FTP GET /RECOVER.",
  9231.     ""
  9232. };
  9233. static char * fhs_ren[] = {             /* RENAME */
  9234.     "Syntax: FTP RENAME name1 name1",
  9235.     "  Asks the FTP server to change the name of the file whose name is name1",
  9236.     "  and which resides in the FTP server's file system, to name2.  Works",
  9237.     "  only for single files; wildcards are not accepted.",
  9238.     ""
  9239. };
  9240. static char * fhs_res[] = {             /* RESET */
  9241.     "Syntax: FTP RESET",
  9242.     "  Asks the server to log out your session, terminating your access",
  9243.     "  rights, without closing the connection.",
  9244.     ""
  9245. };
  9246. static char * fhs_rmd[] = {             /* RMDIR */
  9247.     "Syntax: FTP RMDIR directory",
  9248.     "  Asks the FTP server to remove the directory whose name is given.",
  9249.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  9250.     "  RMDIR, RRMDIR.",
  9251.     ""
  9252. };
  9253. static char * fhs_sit[] = {             /* SITE */
  9254.     "Syntax: FTP SITE text",
  9255.     "  Sends a site-specific command to the FTP server.",
  9256.     ""
  9257. };
  9258. static char * fhs_siz[] = {             /* SIZE */
  9259.     "Syntax: FTP SIZE filename",
  9260.     "  Asks the FTP server to send a numeric string representing the size",
  9261.     "  of the given file.",
  9262.     ""
  9263. };
  9264. static char * fhs_sta[] = {             /* STATUS */
  9265.     "Syntax: FTP STATUS [ filename ]",
  9266.     "  Asks the FTP server to report its status.  If a filename is given,",
  9267.     "  the FTP server should report details about the file.",
  9268.     ""
  9269. };
  9270. static char * fhs_sys[] = {             /* SYSTEM */
  9271.     "Syntax: FTP SYSTEM",
  9272.     "  Asks the FTP server to report its operating system type.",
  9273.     ""
  9274. };
  9275. static char * fhs_typ[] = {             /* TYPE */
  9276.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  9277.     "  Puts the client and server in the indicated transfer mode.",
  9278.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  9279.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  9280.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  9281.     "  TENEX mode.",
  9282.     ""
  9283. };
  9284. static char * fhs_uma[] = {             /* UMASK */
  9285.     "Syntax: FTP UMASK number",
  9286.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  9287.     "  only (or mainly) to UNIX-based FTP servers.",
  9288.     ""
  9289. };
  9290. static char * fhs_chk[] = {             /* CHECK */
  9291.     "Syntax: FTP CHECK remote-filespec",
  9292.     "  Asks the FTP server if the given file or files exist.  If the",
  9293.     "  remote-filespec contains wildcards, this command fails if no server",
  9294.     "  files match, and succeeds if at least one file matches.  If the",
  9295.     "  remote-filespec does not contain wildcards, this command succeeds if",
  9296.     "  the given file exists and fails if it does not.",
  9297.     ""
  9298. };
  9299. static char * fhs_ena[] = {        /* ENABLE */
  9300.     "Syntax: FTP ENABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  9301.     "  Enables the use of the given FTP protocol command in case it has been",
  9302.     "  disabled (but this is no guarantee that the FTP server understands it)."
  9303. ,
  9304.     "  Use SHOW FTP to see which of these commands is enabled and disabled.",
  9305.     "  Also see FTP DISABLE.",
  9306.     ""
  9307. };
  9308. static char * fhs_dis[] = {        /* DISABLE */
  9309.     "Syntax: FTP DISABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  9310.     "  Disables the use of the given FTP protocol command.",
  9311.     "  Also see FTP ENABLE.",
  9312.     ""
  9313. };
  9314.  
  9315. #endif /* NOHELP */
  9316.  
  9317. int
  9318. doftphlp() {
  9319.     int cx;
  9320.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  9321.       if (cx != -3)
  9322.         return(cx);
  9323.     if ((x = cmcfm()) < 0)
  9324.       return(x);
  9325.  
  9326. #ifdef NOHELP
  9327.     printf("Sorry, no help available\n");
  9328. #else
  9329.     switch (cx) {
  9330.       case -3:
  9331.         return(hmsga(fhs_ftp));
  9332.       case FTP_ACC:                     /* ACCOUNT */
  9333.         return(hmsga(fhs_acc));
  9334.       case FTP_APP:                     /* APPEND */
  9335.         return(hmsga(fhs_app));
  9336.       case FTP_CLS:                     /* BYE, CLOSE */
  9337.         return(hmsga(fhs_cls));
  9338.       case FTP_CWD:                     /* CD, CWD */
  9339.         return(hmsga(fhs_cwd));
  9340.       case FTP_GUP:                     /* CDUP, UP */
  9341.         return(hmsga(fhs_gup));
  9342.       case FTP_CHM:                     /* CHMOD */
  9343.         return(hmsga(fhs_chm));
  9344.       case FTP_MDE:                     /* DELETE, MDELETE */
  9345.         return(hmsga(fhs_mde));
  9346.       case FTP_DIR:                     /* DIRECTORY */
  9347.         return(hmsga(fhs_dir));
  9348.       case FTP_VDI:                     /* VDIRECTORY */
  9349.         return(hmsga(fhs_vdi));
  9350.       case FTP_FEA:                     /* FEATURES */
  9351.         return(hmsga(fhs_fea));
  9352.       case FTP_GET:                     /* GET */
  9353.         return(hmsga(fhs_get));
  9354.       case FTP_HLP:                     /* HELP */
  9355.         return(hmsga(fhs_hlp));
  9356.       case FTP_IDL:                     /* IDLE */
  9357.         return(hmsga(fhs_idl));
  9358.       case FTP_USR:                     /* USER, LOGIN */
  9359.         return(hmsga(fhs_usr));
  9360.       case FTP_MGE:                     /* MGET */
  9361.         return(hmsga(fhs_mge));
  9362.       case FTP_MKD:                     /* MKDIR */
  9363.         return(hmsga(fhs_mkd));
  9364.       case FTP_MOD:                     /* MODTIME */
  9365.         return(hmsga(fhs_mod));
  9366.       case FTP_MPU:                     /* MPUT */
  9367.         return(hmsga(fhs_mpu));
  9368.       case FTP_OPN:                     /* OPEN */
  9369.         return(hmsga(fhs_opn));
  9370.       case FTP_OPT:                     /* OPTS, OPTIONS */
  9371.         return(hmsga(fhs_opt));
  9372.       case FTP_PUT:                     /* PUT, SEND */
  9373.         return(hmsga(fhs_put));
  9374.       case FTP_REP:                     /* REPUT, RESEND */
  9375.         return(hmsga(fhs_reput));
  9376.       case FTP_PWD:                     /* PWD */
  9377.         return(hmsga(fhs_pwd));
  9378.       case FTP_QUO:                     /* QUOTE */
  9379.         return(hmsga(fhs_quo));
  9380.       case FTP_RGE:                     /* REGET */
  9381.         return(hmsga(fhs_rge));
  9382.       case FTP_REN:                     /* RENAME */
  9383.         return(hmsga(fhs_ren));
  9384.       case FTP_RES:                     /* RESET */
  9385.         return(hmsga(fhs_res));
  9386.       case FTP_RMD:                     /* RMDIR */
  9387.         return(hmsga(fhs_rmd));
  9388.       case FTP_SIT:                     /* SITE */
  9389.         return(hmsga(fhs_sit));
  9390.       case FTP_SIZ:                     /* SIZE */
  9391.         return(hmsga(fhs_siz));
  9392.       case FTP_STA:                     /* STATUS */
  9393.         return(hmsga(fhs_sta));
  9394.       case FTP_SYS:                     /* SYSTEM */
  9395.         return(hmsga(fhs_sys));
  9396.       case FTP_TYP:                     /* TYPE */
  9397.         return(hmsga(fhs_typ));
  9398.       case FTP_UMA:                     /* UMASK */
  9399.         return(hmsga(fhs_uma));
  9400.       case FTP_CHK:                     /* CHECK */
  9401.         return(hmsga(fhs_chk));
  9402.       case FTP_ENA:
  9403.         return(hmsga(fhs_ena));
  9404.       case FTP_DIS:
  9405.         return(hmsga(fhs_dis));
  9406.       default:
  9407.         printf("Sorry, help available for this command.\n");
  9408.         break;
  9409.     }
  9410. #endif /* NOHELP */
  9411.     return(success = 0);
  9412. }
  9413.  
  9414. int
  9415. dosetftphlp() {
  9416.     int cx;
  9417.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  9418.       if (cx != -3)
  9419.         return(cx);
  9420.     if (cx != -3)
  9421.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  9422.     if ((x = cmcfm()) < 0)
  9423.       return(x);
  9424.  
  9425. #ifdef NOHELP
  9426.     printf("Sorry, no help available\n");
  9427. #else
  9428.     switch (cx) {
  9429.       case -3:
  9430.         printf("\nSyntax: SET FTP parameter value\n");
  9431.         printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  9432.         printf("  Type \"help set ftp xxx\" for information about setting\n");
  9433.         printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  9434.         return(0);
  9435.  
  9436.       case FTS_BUG:
  9437.     printf("\nSyntax: SET FTP BUG <name> {ON, OFF}\n");
  9438.     printf(
  9439.         "  Activates a workaround for the named bug in the FTP server.\n");
  9440.     printf("  Type SET FTP BUG ? for a list of names.\n");
  9441.     printf("  For each bug, the default is OFF\n\n");
  9442.     return(0);
  9443.  
  9444. #ifdef FTP_SECURITY
  9445.       case FTS_ATP:                     /* "authtype" */
  9446.         printf("\nSyntax: SET FTP AUTHTYPE list\n");
  9447.         printf("  Specifies an ordered list of authentication methods to be\n"
  9448.                );
  9449.         printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  9450.         printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  9451.         return(0);
  9452.  
  9453.       case FTS_AUT:                     /* "autoauthentication" */
  9454.         printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  9455.         printf("  Tells whether authentication should be negotiated by the\n");
  9456.         printf("  FTP OPEN command.  Default is ON.\n\n");
  9457.         break;
  9458.  
  9459.       case FTS_CRY:                     /* "autoencryption" */
  9460.         printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  9461.         printf("  Tells whether encryption (privacy) should be negotiated\n");
  9462.         printf("  by the FTP OPEN command.  Default is ON.\n\n");
  9463.         break;
  9464. #endif /* FTP_SECURITY */
  9465.  
  9466.       case FTS_LOG:                     /* "autologin" */
  9467.         printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  9468.         printf("  Tells Kermit whether to try to log you in automatically\n");
  9469.         printf("  as part of the connection process.\n\n");
  9470.         break;
  9471.  
  9472.       case FTS_DIS:
  9473.         printf("\nSET FTP DISPLAY { BRIEF, FULLSCREEN, CRT, ... }\n");
  9474.     printf("  Chooses the file-transfer display style for FTP.\n");
  9475.         printf("  Like SET TRANSFER DISPLAY but applies only to FTP.\n\n");
  9476.         break;
  9477.  
  9478. #ifndef NOCSETS
  9479.       case FTS_XLA:                     /* "character-set-translation" */
  9480.         printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  9481.         printf("  Whether to translate character sets when transferring\n");
  9482.         printf("  text files with FTP.  OFF by default.\n\n");
  9483.         break;
  9484.  
  9485. #endif /* NOCSETS */
  9486.       case FTS_FNC:                     /* "collision" */
  9487.         printf("\n");
  9488.         printf(
  9489. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  9490.                );
  9491.         printf("  Tells what do when an incoming file has the same name as\n");
  9492.         printf("  an existing file when downloading with FTP.\n\n");
  9493.         break;
  9494.  
  9495. #ifdef FTP_SECURITY
  9496.       case FTS_CPL:                     /* "command-protection-level" */
  9497.         printf("\n");
  9498.         printf(
  9499. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9500.                );
  9501.         printf("\n");
  9502.         printf(
  9503. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  9504.         break;
  9505.       case FTS_CFW:                     /* "credential-forwarding" */
  9506.         printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  9507.         printf("  Tells whether end-user credentials are to be forwarded\n");
  9508.         printf("  to the server if supported by the authentication method\n");
  9509.         printf("  (GSSAPI-KRB5 only).\n\n");
  9510.         break;
  9511.       case FTS_DPL:                     /* "data-protection-level" */
  9512.         printf("\n");
  9513.         printf(
  9514. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9515.                );
  9516.         printf("\n");
  9517.         printf(
  9518. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  9519.         break;
  9520. #endif /* FTP_SECURITY */
  9521.  
  9522.       case FTS_DBG:                     /* "debug" */
  9523.         printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  9524.         printf("  Whether to print FTP protocol messages.\n\n");
  9525.         return(0);
  9526.  
  9527.       case FTS_ERR:                     /* "error-action" */
  9528.         printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  9529.         printf("  What to do when an error occurs when transferring a group\n")
  9530.           ;
  9531.         printf("  of files: quit and fail, or proceed to the next file.\n\n");
  9532.         return(0);
  9533.  
  9534.       case FTS_CNV:                     /* "filenames" */
  9535.         printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  9536.         printf("  What to do with filenames: convert them, take and use them\n"
  9537.                );
  9538.         printf("  literally; or choose what to do automatically based on the\n"
  9539.                );
  9540.         printf("  OS type of the server.  The default is AUTO.\n\n");
  9541.         return(0);
  9542.  
  9543.       case FTS_PSV:                     /* "passive-mode" */
  9544.         printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  9545.         printf("  Whether to use passive mode, which helps to get through\n");
  9546.         printf("  firewalls.  ON by default.\n\n");
  9547.         return(0);
  9548.  
  9549.       case FTS_PRM:                     /* "permissions" */
  9550.         printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  9551.         printf("  Whether to try to send file permissions when uploading.\n");
  9552.         printf("  OFF by default.  AUTO means only if client and server\n");
  9553.         printf("  have the same OS type.\n\n");
  9554.         return(0);
  9555.  
  9556.       case FTS_TST:                     /* "progress-messages" */
  9557.         printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  9558.         printf("  Whether Kermit should print locally-generated feedback\n");
  9559.         printf("  messages for each non-file-transfer command.");
  9560.         printf("  ON by default.\n\n");
  9561.         return(0);
  9562.  
  9563.       case FTS_SPC:                     /* "send-port-commands" */
  9564.         printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  9565.         printf("  Whether Kermit should send a new PORT command for each");
  9566.         printf("  task.\n\n");
  9567.         return(0);
  9568.  
  9569. #ifndef NOCSETS
  9570.       case FTS_CSR:                     /* "server-character-set" */
  9571.         printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  9572.         printf("  The name of the character set used for text files on the\n");
  9573.         printf("  server.  Enter a name of '?' for a menu.\n\n");
  9574.         return(0);
  9575. #endif /* NOCSETS */
  9576.  
  9577.       case FTS_STO:            /* "server-time-offset */
  9578.     printf(
  9579. "\nSyntax: SET FTP SERVER-TIME-OFFSET +hh[:mm[:ss]] or -hh[:mm[:ss]]\n");
  9580.         printf(
  9581. "  Specifies an offset to apply to the server's file timestamps.\n");
  9582.         printf(
  9583. "  Use this to correct for misconfigured server time or timezone.\n");
  9584.         printf(
  9585. "  Format: must begin with + or - sign.  Hours must be given; minutes\n");
  9586.         printf(
  9587. "  and seconds are optional: +4 = +4:00 = +4:00:00 (add 4 hours).\n\n");
  9588.         return(0);
  9589.  
  9590.       case FTS_TYP:                     /* "type" */
  9591.         printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  9592.         printf("  Establishes the default transfer mode.\n");
  9593.         printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  9594.         printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  9595.         printf("  them again.  ASCII is a synonym for TEXT.  Normally each\n");
  9596.         printf("  file's type is determined automatically from its contents\n"
  9597.            );
  9598.         printf("  or its name; SET FTP TYPE does not prevent that, it only\n");
  9599.         printf("  tells which mode to use when the type can't be determined\n"
  9600.            );
  9601.         printf("  automatically.  To completely disable automatic transfer-\n"
  9602.            );
  9603.         printf("  mode switching and force either text or binary mode, give\n"
  9604.            );
  9605.         printf("  the top-level command ASCII or BINARY, as in traditional\n");
  9606.         printf("  FTP clients.\n\n");
  9607.         return(0);
  9608.  
  9609. #ifdef FTP_TIMEOUT
  9610.       case FTS_TMO:
  9611.        printf("\nSyntax: SET FTP TIMEOUT number-of-seconds\n");
  9612.        printf("  Establishes a timeout for FTP transfers.\n");
  9613.        printf("  The timeout applies per network read or write on the data\n");
  9614.        printf("  connection, not to the whole transfer.  By default the\n");
  9615.        printf("  timeout value is 0, meaning no timeout.  Use a positive\n");
  9616.        printf("  number to escape gracefully from hung data connections or\n");
  9617.        printf("  directory listings.\n\n");
  9618.         return(0);
  9619. #endif    /* FTP_TIMEOUT */
  9620.  
  9621. #ifdef PATTERNS
  9622.       case FTS_GFT:
  9623.         printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  9624.         printf("  Tells whether GET and MGET should automatically switch\n");
  9625.         printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  9626.         printf("  matching the name of each incoming file with its list of\n");
  9627.         printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  9628.         printf("  default.  SHOW PATTERNS displays the current pattern\n");
  9629.         printf("  list.  HELP SET FILE to see how to change it.\n");
  9630.         break;
  9631. #endif /* PATTERNS */
  9632.  
  9633.       case FTS_USN:                     /* "unique-server-names" */
  9634.         printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  9635.         printf("  Tells whether to ask the server to create unique names\n");
  9636.         printf("  for any uploaded file that has the same name as an\n");
  9637.         printf("  existing file.  Default is OFF.\n\n");
  9638.         return(0);
  9639.  
  9640.       case FTS_VBM:                     /* "verbose-mode" */
  9641.         printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  9642.         printf("  Whether to display all responses from the FTP server.\n");
  9643.         printf("  OFF by default.\n\n");
  9644.         return(0);
  9645.  
  9646.       case FTS_DAT:
  9647.         printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  9648.         printf("  Whether to set date of incoming files from the file date\n");
  9649.         printf("  on the server.  ON by default.  Note: there is no way to\n")
  9650.           ;
  9651.         printf("  set the date on files uploaded to the server.  Also note\n");
  9652.     printf("  that not all servers support this feature.\n\n");
  9653.         return(0);
  9654.  
  9655.       case FTS_APW:
  9656.     printf("\nSyntax: SET FTP ANONYMOUS-PASSWORD [ text ]\n");
  9657.     printf("  Password to supply automatically on anonymous FTP\n");
  9658.     printf("  connections instead of the default user@host.\n");
  9659.     printf("  Omit optional text to restore default.\n\n");
  9660.     return(0);
  9661.  
  9662.       default:
  9663.         printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  9664.     }
  9665. #endif /* NOHELP */
  9666.     return(0);
  9667. }
  9668.  
  9669. #ifndef L_SET
  9670. #define L_SET 0
  9671. #endif /* L_SET */
  9672. #ifndef L_INCR
  9673. #define L_INCR 1
  9674. #endif /* L_INCR */
  9675.  
  9676. #ifdef FTP_SRP
  9677. char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
  9678. char *srp_pass;
  9679. char *srp_acct;
  9680. #endif /* FTP_SRP */
  9681.  
  9682. static int kerror;                      /* Needed for all auth types */
  9683.  
  9684. static struct   sockaddr_in hisctladdr;
  9685. static struct   sockaddr_in hisdataaddr;
  9686. static struct   sockaddr_in data_addr;
  9687. static int      data = -1;
  9688. static int      ptflag = 0;
  9689. static struct   sockaddr_in myctladdr;
  9690.  
  9691. #ifdef COMMENT
  9692. #ifndef OS2
  9693. UID_T getuid();
  9694. #endif /* OS2 */
  9695. #endif /* COMMENT */
  9696.  
  9697.  
  9698. static int cpend = 0;                   /* No pending replies */
  9699.  
  9700. #ifdef CK_SSL
  9701. extern SSL *ssl_ftp_con;
  9702. extern SSL_CTX *ssl_ftp_ctx;
  9703. extern SSL *ssl_ftp_data_con;
  9704. extern int ssl_ftp_active_flag;
  9705. extern int ssl_ftp_data_active_flag;
  9706. #endif /* CK_SSL */
  9707.  
  9708. /*  f t p c m d  --  Send a command to the FTP server  */
  9709. /*
  9710.   Call with:
  9711.     char * cmd: The command to send.
  9712.     char * arg: The argument (e.g. a filename).
  9713.     int lcs: The local character set index.
  9714.     int rcs: The remote (server) character set index.
  9715.     int vbm: Verbose mode:
  9716.       0 = force verbosity off
  9717.      >0 = force verbosity on
  9718.  
  9719.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  9720.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  9721.   character set to the remote one before sending the result to the server.
  9722.  
  9723.    Returns:
  9724.     0 on failure with ftpcode = -1
  9725.     >= 0 on success (getreply() result) with ftpcode = 0.
  9726. */
  9727. static char xcmdbuf[RFNBUFSIZ];
  9728.  
  9729. static int
  9730. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  9731.     char * s = NULL;
  9732.     int r = 0, x = 0, fc = 0, len = 0, cmdlen = 0, q = -1;
  9733.     sig_t oldintr;
  9734.  
  9735.     if (ftp_deb)                        /* DEBUG */
  9736.       vbm = 1;
  9737.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9738.       vbm = 0;
  9739.     else if (vbm < 0)                   /* VERBOSE */
  9740.       vbm = ftp_vbm;
  9741.  
  9742.     cancelfile = 0;
  9743.     if (!cmd) cmd = "";
  9744.     if (!arg) arg = "";
  9745.     cmdlen = (int)strlen(cmd);
  9746.     len = cmdlen + (int)strlen(arg) + 1;
  9747.  
  9748.     if (ftp_deb /* && !dpyactive */ ) {
  9749. #ifdef FTP_PROXY
  9750.         if (ftp_prx) printf("%s ", ftp_host);
  9751. #endif /* FTP_PROXY */
  9752.         printf("---> ");
  9753.         if (!anonymous && strcmp("PASS",cmd) == 0)
  9754.           printf("PASS XXXX");
  9755.         else
  9756.           printf("%s %s",cmd,arg);
  9757.         printf("\n");
  9758.     }
  9759.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  9760.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  9761.  
  9762. #ifdef DEBUG
  9763.     if (deblog) {
  9764.         debug(F110,"ftpcmd cmd",cmd,0);
  9765.         debug(F110,"ftpcmd arg",arg,0);
  9766.         debug(F101,"ftpcmd lcs","",lcs);
  9767.         debug(F101,"ftpcmd rcs","",rcs);
  9768.     }
  9769. #endif /* DEBUG */
  9770.  
  9771.     if (csocket == -1) {
  9772.         perror("No control connection for command");
  9773.         ftpcode = -1;
  9774.         return(0);
  9775.     }
  9776.     havesigint = 0;
  9777.     oldintr = signal(SIGINT, cmdcancel);
  9778.  
  9779. #ifndef NOCSETS
  9780.     if (*arg &&                         /* If an arg was given */
  9781.         lcs > -1 &&                     /* and a local charset */
  9782.         rcs > -1 &&                     /* and a remote charset */
  9783.         lcs != rcs &&                   /* and the two are not the same */
  9784.         lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
  9785.         rcs != FC_UCS2                  /* ... */
  9786.         ) {
  9787.         initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
  9788.         xgnbp = arg;                    /* Global pointer to input string */
  9789.         rfnptr = rfnbuf;                /* Global pointer to output buffer */
  9790.  
  9791.         while (1) {
  9792.             if ((c0 = xgnbyte(FC_UCS2,lcs,strgetc)) < 0) break;
  9793.             if (xpnbyte(c0,TC_UCS2,rcs,strputc) < 0) break;
  9794.         }
  9795.         /*
  9796.           We have to copy here instead of translating directly into
  9797.           xcmdbuf[] so strputc() can check length.  Alternatively we could
  9798.           write yet another xpnbyte() output function.
  9799.         */
  9800.         if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  9801.             printf("?FTP command too long: %s + arg\n",cmd);
  9802.             ftpcode = -1;
  9803.             return(0);
  9804.         }
  9805.         x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  9806.     }
  9807. #endif /* NOCSETS */
  9808.  
  9809.     s = xcmdbuf;                        /* Command to send to server */
  9810.  
  9811. #ifdef DEBUG
  9812.     if (deblog) {            /* Log it */
  9813.     if (!anonymous && !ckstrcmp(s,"PASS ",5,0)) {
  9814.         /* But don't log passwords */
  9815.         debug(F110,"FTP SENT ","PASS XXXX",0);
  9816.     } else {
  9817.         debug(F110,"FTP SENT ",s,0);
  9818.     }
  9819.     }
  9820. #endif /* DEBUG */
  9821.  
  9822. #ifdef CK_ENCRYPTION
  9823.   again:
  9824. #endif /* CK_ENCRYPTION */
  9825.     if (scommand(s) == 0) {              /* Send it. */
  9826.       signal(SIGINT, oldintr);
  9827.       return(0);
  9828.     }
  9829.     cpend = 1;
  9830.     x = !strcmp(cmd,"QUIT");        /* Is it the QUIT command? */
  9831.     if (x)                /* In case we're interrupted */
  9832.       connected = 0;            /* while waiting for the reply... */
  9833.  
  9834.     fc = 0;                /* Function code for getreply() */
  9835.     if (!strncmp(cmd,"AUTH ",5)        /* Must parse AUTH reply */
  9836. #ifdef FTPHOST
  9837.     && strncmp(cmd, "HOST ",5)
  9838. #endif /* FTPHOST */
  9839.     ) {
  9840.     fc = GRF_AUTH;
  9841.     } else if (!ckstrcmp(cmd,"FEAT",-1,0)) { /* Must parse FEAT reply */
  9842.     fc = GRF_FEAT;            /* But FEAT not widely understood */
  9843.     if (!ftp_deb)            /* So suppress error messages */
  9844.       vbm = 9;
  9845.     }
  9846.     r = getreply(x,            /* Expect connection to close */
  9847.          lcs,rcs,        /* Charsets */
  9848.          vbm,            /* Verbosity */
  9849.          fc            /* Function code */
  9850.          );
  9851.     if (q > -1)
  9852.       quiet = q;
  9853.  
  9854. #ifdef CK_ENCRYPTION
  9855.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  9856.         fprintf(stderr,
  9857.                "ENC command not supported at server; retrying under MIC...\n");
  9858.         ftp_cpl = FPL_SAF;
  9859.         goto again;
  9860.     }
  9861. #endif /* CK_ENCRYPTION */
  9862. #ifdef COMMENT
  9863.     if (cancelfile && oldintr != SIG_IGN)
  9864.       (*oldintr)(SIGINT);
  9865. #endif /* COMMENT */
  9866.     signal(SIGINT, oldintr);
  9867.     return(r);
  9868. }
  9869.  
  9870. static VOID
  9871. lostpeer() {
  9872.     debug(F100,"lostpeer","",0);
  9873.     if (connected) {
  9874.         if (csocket != -1) {
  9875. #ifdef CK_SSL
  9876.             if (ssl_ftp_active_flag) {
  9877.                 SSL_shutdown(ssl_ftp_con);
  9878.                 SSL_free(ssl_ftp_con);
  9879.                 ssl_ftp_proxy = 0;
  9880.                 ssl_ftp_active_flag = 0;
  9881.                 ssl_ftp_con = NULL;
  9882.             }
  9883. #endif /* CK_SSL */
  9884. #ifdef TCPIPLIB
  9885.             socket_close(csocket);
  9886. #else /* TCPIPLIB */
  9887. #ifdef USE_SHUTDOWN
  9888.             shutdown(csocket, 1+1);
  9889. #endif /* USE_SHUTDOWN */
  9890.             close(csocket);
  9891. #endif /* TCPIPLIB */
  9892.             csocket = -1;
  9893.         }
  9894.         if (data != -1) {
  9895. #ifdef CK_SSL
  9896.             if (ssl_ftp_data_active_flag) {
  9897.                 SSL_shutdown(ssl_ftp_data_con);
  9898.                 SSL_free(ssl_ftp_data_con);
  9899.                 ssl_ftp_data_active_flag = 0;
  9900.                 ssl_ftp_data_con = NULL;
  9901.             }
  9902. #endif /* CK_SSL */
  9903. #ifdef TCPIPLIB
  9904.             socket_close(data);
  9905. #else /* TCPIPLIB */
  9906. #ifdef USE_SHUTDOWN
  9907.             shutdown(data, 1+1);
  9908. #endif /* USE_SHUTDOWN */
  9909.             close(data);
  9910. #endif /* TCPIPLIB */
  9911.             data = -1;
  9912.             globaldin = -1;
  9913.         }
  9914.         connected = 0;
  9915.         anonymous = 0;
  9916.         loggedin = 0;
  9917.         auth_type = NULL;
  9918.         ftp_cpl = ftp_dpl = FPL_CLR;
  9919. #ifdef CKLOGDIAL
  9920.         ftplogend();
  9921. #endif /* CKLOGDIAL */
  9922.  
  9923. #ifdef LOCUS
  9924.     if (autolocus)            /* Auotomatic locus switching... */
  9925.       setlocus(1,1);        /* Switch locus to local. */
  9926. #endif /* LOCUS */
  9927. #ifdef OS2
  9928.         DialerSend(OPT_KERMIT_HANGUP, 0);
  9929. #endif /* OS2 */
  9930.     }
  9931. #ifdef FTP_PROXY
  9932.     pswitch(1);
  9933.     if (connected) {
  9934.         if (csocket != -1) {
  9935. #ifdef TCPIPLIB
  9936.             socket_close(csocket);
  9937. #else /* TCPIPLIB */
  9938. #ifdef USE_SHUTDOWN
  9939.             shutdown(csocket, 1+1);
  9940. #endif /* USE_SHUTDOWN */
  9941.             close(csocket);
  9942. #endif /* TCPIPLIB */
  9943.             csocket = -1;
  9944.         }
  9945.         connected = 0;
  9946.         anonymous = 0;
  9947.         loggedin = 0;
  9948.         auth_type = NULL;
  9949.         ftp_cpl = ftp_dpl = FPL_CLR;
  9950.     }
  9951.     proxflag = 0;
  9952.     pswitch(0);
  9953. #endif /* FTP_PROXY */
  9954. }
  9955.  
  9956. int
  9957. ftpisopen() {
  9958.     return(connected);
  9959. }
  9960.  
  9961. static int
  9962. ftpclose() {
  9963.     extern int quitting;
  9964.     if (!connected)
  9965.       return(0);
  9966.     ftp_xfermode = xfermode;
  9967.     if (!ftp_vbm && !quiet)
  9968.       printlines = 1;
  9969.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  9970.     if (csocket) {
  9971. #ifdef CK_SSL
  9972.         if (ssl_ftp_active_flag) {
  9973.             SSL_shutdown(ssl_ftp_con);
  9974.             SSL_free(ssl_ftp_con);
  9975.             ssl_ftp_proxy = 0;
  9976.             ssl_ftp_active_flag = 0;
  9977.             ssl_ftp_con = NULL;
  9978.         }
  9979. #endif /* CK_SSL */
  9980. #ifdef TCPIPLIB
  9981.         socket_close(csocket);
  9982. #else /* TCPIPLIB */
  9983. #ifdef USE_SHUTDOWN
  9984.         shutdown(csocket, 1+1);
  9985. #endif /* USE_SHUTDOWN */
  9986.         close(csocket);
  9987. #endif /* TCPIPLIB */
  9988.     }
  9989.     csocket = -1;
  9990.     connected = 0;
  9991.     anonymous = 0;
  9992.     loggedin = 0;
  9993.     mdtmok = 1;
  9994.     sizeok = 1;
  9995.     featok = 1;
  9996.     stouarg = 1;
  9997.     typesent = 0;
  9998.     data = -1;
  9999.     globaldin = -1;
  10000. #ifdef FTP_PROXY
  10001.     if (!proxy)
  10002.       macnum = 0;
  10003. #endif /* FTP_PROXY */
  10004.     auth_type = NULL;
  10005.     ftp_dpl = FPL_CLR;
  10006. #ifdef CKLOGDIAL
  10007.     ftplogend();
  10008. #endif /* CKLOGDIAL */
  10009. #ifdef LOCUS
  10010.     /* Unprefixed file management commands are executed locally */
  10011.     if (autolocus && !ftp_cmdlin && !quitting) {
  10012.         setlocus(1,1);
  10013.     }
  10014. #endif /* LOCUS */
  10015. #ifdef OS2
  10016.     DialerSend(OPT_KERMIT_HANGUP, 0);
  10017. #endif /* OS2 */
  10018.     return(0);
  10019. }
  10020.  
  10021. int
  10022. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  10023.     char * host;
  10024.  
  10025.     if (connected) {
  10026.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  10027.         ftpcode = -1;
  10028.         return(0);
  10029.     }
  10030. #ifdef FTPHOST
  10031.     hostcmd = 0;
  10032. #endif /* FTPHOST */
  10033.     alike = 0;
  10034.     ftp_srvtyp[0] = NUL;
  10035.     if (!service) service = "";
  10036.     if (!*service) service = use_tls ? "ftps" : "ftp";
  10037.  
  10038.     if (!isdigit(service[0])) {
  10039.         struct servent *destsp;
  10040.         destsp = getservbyname(service, "tcp");
  10041.         if (!destsp) {
  10042.             if (!ckstrcmp(service,"ftp",-1,0)) {
  10043.                 ftp_port = 21;
  10044.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  10045.                 ftp_port = 990;
  10046.             } else {
  10047.                 printf("?Bad port name - \"%s\"\n", service);
  10048.                 ftpcode = -1;
  10049.                 return(0);
  10050.             }
  10051.         } else {
  10052.             ftp_port = destsp->s_port;
  10053.             ftp_port = ntohs((unsigned short)ftp_port);    /* SMS 2007/02/15 */
  10054.         }
  10055.     } else
  10056.         ftp_port = atoi(service);
  10057.     if (ftp_port <= 0) {
  10058.         printf("?Bad port name - \"%s\"\n", service);
  10059.         ftpcode = -1;
  10060.         return(0);
  10061.     }
  10062.     host = ftp_hookup(remote, ftp_port, use_tls);
  10063.     if (host) {
  10064.         ckstrncpy(ftp_user_host,remote,MAX_DNS_NAMELEN);
  10065.         connected = 1;                  /* Set FTP defaults */
  10066.         ftp_cpl = ftp_dpl = FPL_CLR;
  10067.         curtype = FTT_ASC;              /* Server uses ASCII mode */
  10068.         form = FORM_N;
  10069.         mode = MODE_S;
  10070.         stru = STRU_F;
  10071.         strcpy(bytename, "8");
  10072.         bytesize = 8;
  10073.  
  10074. #ifdef FTP_SECURITY
  10075.         if (ftp_aut) {
  10076.             if (ftp_auth()) {
  10077.                 if (ftp_cry 
  10078. #ifdef OS2
  10079.                      && ck_crypt_is_installed()
  10080. #endif /* OS2 */
  10081.                      ) {
  10082.                     if (!quiet)
  10083.                       printf("FTP Command channel is Private (encrypted)\n");
  10084.                     ftp_cpl = FPL_PRV;
  10085.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  10086.                         /* a failure here is most likely caused by a mixup */
  10087.                         /* in the session key used by client and server    */
  10088.             printf("?Protection buffer size negotiation failed\n");
  10089.                         return(0);
  10090.                     }
  10091.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  10092.                         if (!quiet)
  10093.                           printf("FTP Data channel is Private (encrypted)\n");
  10094.                         ftp_dpl = FPL_PRV;
  10095.                     } else
  10096.                       printf("?Unable to enable encryption on data channel\n");
  10097.                 } else {
  10098.                     ftp_cpl = FPL_SAF;
  10099.                 }
  10100.             }
  10101.             if (!connected)
  10102.           goto fail;
  10103.         }
  10104. #endif /* FTP_SECURITY */
  10105.         if (ftp_log)            /* ^^^ */
  10106.           ftp_login(remote);
  10107.  
  10108.         if (!connected)
  10109.       goto fail;
  10110.  
  10111.     ftp_xfermode = xfermode;
  10112.  
  10113. #ifdef CKLOGDIAL
  10114.         dologftp();
  10115. #endif /* CKLOGDIAL */
  10116. #ifdef OS2
  10117.         DialerSend(OPT_KERMIT_CONNECT, 0);
  10118. #endif /* OS2 */
  10119.         passivemode = ftp_psv;
  10120.         sendport = ftp_spc;
  10121.     mdtmok = 1;
  10122.     sizeok = 1;
  10123.     stouarg = 1;
  10124.     typesent = 0;
  10125.  
  10126.         if (ucbuf == NULL) {
  10127.             actualbuf = DEFAULT_PBSZ;
  10128.             while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  10129.               actualbuf >>= 2;
  10130.         }
  10131.         if (!maxbuf)
  10132.           ucbufsiz = actualbuf - FUDGE_FACTOR;
  10133.     debug(F101,"ftpopen ucbufsiz","",ucbufsiz);
  10134.         return(1);
  10135.     }
  10136.   fail:
  10137.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  10138.     ftpcode = -1;
  10139.     return(0);
  10140. }
  10141.  
  10142. #ifdef CK_SSL
  10143. int
  10144. ssl_auth() {
  10145.     int i;
  10146.     char* p;
  10147.  
  10148.     if (ssl_debug_flag) {
  10149.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  10150.         fflush(stderr);
  10151.         /* for the moment I want the output on screen */
  10152.     }
  10153.     if (ssl_ftp_data_con != NULL) {
  10154.         SSL_free(ssl_ftp_data_con);
  10155.         ssl_ftp_data_con = NULL;
  10156.     }
  10157.     if (ssl_ftp_con != NULL) {
  10158.         SSL_free(ssl_ftp_con);
  10159.         ssl_ftp_con=NULL;
  10160.     }
  10161.     if (ssl_ftp_ctx != NULL) {
  10162.         SSL_CTX_free(ssl_ftp_ctx);
  10163.         ssl_ftp_ctx = NULL;
  10164.     }
  10165.  
  10166.     /* The SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 
  10167.      * was added to OpenSSL 0.9.6e and 0.9.7.  It does not exist in previous
  10168.      * versions
  10169.      */
  10170. #ifndef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  10171. #define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0L
  10172. #endif
  10173.     if (auth_type && !strcmp(auth_type,"TLS")) {
  10174.         ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  10175.         if (!ssl_ftp_ctx)
  10176.           return(0);
  10177.         SSL_CTX_set_options(ssl_ftp_ctx,
  10178.                             SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  10179.                             );
  10180.     } else {
  10181.         ssl_ftp_ctx = SSL_CTX_new(ftp_bug_use_ssl_v2 ? SSLv23_client_method() : 
  10182.                                   SSLv3_client_method());
  10183.         if (!ssl_ftp_ctx)
  10184.           return(0);
  10185.         SSL_CTX_set_options(ssl_ftp_ctx,
  10186.                             (ftp_bug_use_ssl_v2 ? 0 : SSL_OP_NO_SSLv2)|
  10187.                             SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  10188.                             );
  10189.     }
  10190.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  10191.                                   (pem_password_cb *)ssl_passwd_callback);
  10192.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  10193.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  10194.  
  10195. #ifdef OS2
  10196. #ifdef NT
  10197.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  10198.     {
  10199.         char path[CKMAXPATH];
  10200.         extern char exedir[];
  10201.  
  10202.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  10203.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  10204.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10205.             if (ssl_debug_flag)
  10206.                 printf("?Unable to load verify-dir: %s\r\n",path);
  10207.         }
  10208.  
  10209.         ckmakmsg(path,CKMAXPATH,
  10210.                  (char *)GetAppData(1),"kermit 95/certs",NULL,NULL);
  10211.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  10212.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10213.             if (ssl_debug_flag)
  10214.                 printf("?Unable to load verify-dir: %s\r\n",path);
  10215.         }
  10216.  
  10217.         ckmakmsg(path,CKMAXPATH,
  10218.                  (char *)GetAppData(0),"kermit 95/certs",NULL,NULL);
  10219.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  10220.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10221.             if (ssl_debug_flag)
  10222.                 printf("?Unable to load verify-dir: %s\r\n",path);
  10223.         }
  10224.  
  10225.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  10226.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  10227.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10228.             if (ssl_debug_flag)
  10229.                 printf("?Unable to load verify-file: %s\r\n",path);
  10230.         }
  10231.  
  10232.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  10233.          "kermit 95/ca_certs.pem",NULL,NULL);
  10234.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  10235.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10236.             if (ssl_debug_flag)
  10237.                 printf("?Unable to load verify-file: %s\r\n",path);
  10238.         }
  10239.  
  10240.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  10241.          "kermit 95/ca_certs.pem",NULL,NULL);
  10242.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  10243.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10244.             if (ssl_debug_flag)
  10245.                 printf("?Unable to load verify-file: %s\r\n",path);
  10246.         }
  10247.     }
  10248. #else /* NT */
  10249.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  10250.     {
  10251.  
  10252.         char path[CKMAXPATH];
  10253.         extern char exedir[];
  10254.  
  10255.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  10256.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  10257.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10258.             if (ssl_debug_flag)
  10259.                 printf("?Unable to load verify-dir: %s\r\n",path);
  10260.         }
  10261.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  10262.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  10263.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10264.             if (ssl_debug_flag)
  10265.                 printf("?Unable to load verify-file: %s\r\n",path);
  10266.         }
  10267.     }
  10268. #endif /* NT */
  10269. #else /* OS2 */
  10270.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  10271. #endif /* OS2 */
  10272.  
  10273.     if (ssl_verify_file &&
  10274.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
  10275.         debug(F110,
  10276.               "ftp ssl auth unable to load ssl_verify_file",
  10277.               ssl_verify_file,
  10278.               0
  10279.               );
  10280.         if (ssl_debug_flag)
  10281.           printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  10282.     }
  10283.     if (ssl_verify_dir &&
  10284.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
  10285.         debug(F110,
  10286.               "ftp ssl auth unable to load ssl_verify_dir",
  10287.               ssl_verify_dir,
  10288.               0
  10289.               );
  10290.         if (ssl_debug_flag)
  10291.           printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  10292.     }
  10293.  
  10294.     /* set up the new CRL Store */
  10295.     crl_store = (X509_STORE *)X509_STORE_new();
  10296.     if (crl_store) {
  10297. #ifdef OS2
  10298.         char path[CKMAXPATH];
  10299.         extern char exedir[];
  10300.  
  10301.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  10302.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10303.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10304.             if (ssl_debug_flag)
  10305.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10306.         }
  10307. #ifdef NT
  10308.         ckmakmsg(path,CKMAXPATH,
  10309.          (char *)GetAppData(1),"kermit 95/crls",NULL,NULL);
  10310.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10311.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10312.             if (ssl_debug_flag)
  10313.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10314.         }
  10315.         ckmakmsg(path,CKMAXPATH,
  10316.          (char *)GetAppData(0),"kermit 95/crls",NULL,NULL);
  10317.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10318.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10319.             if (ssl_debug_flag)
  10320.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10321.         }
  10322. #endif /* NT */
  10323.         
  10324.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  10325.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10326.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10327.             if (ssl_debug_flag)
  10328.                 printf("?Unable to load crl-file: %s\r\n",path);
  10329.         }
  10330. #ifdef NT
  10331.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  10332.          "kermit 95/ca_crls.pem",NULL,NULL);
  10333.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10334.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10335.             if (ssl_debug_flag)
  10336.                 printf("?Unable to load crl-file: %s\r\n",path);
  10337.         }
  10338.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  10339.          "kermit 95/ca_crls.pem",NULL,NULL);
  10340.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10341.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10342.             if (ssl_debug_flag)
  10343.                 printf("?Unable to load crl-file: %s\r\n",path);
  10344.         }
  10345. #endif /* NT */
  10346. #endif /* OS2 */
  10347.  
  10348.         if (ssl_crl_file || ssl_crl_dir) {
  10349.             if (ssl_crl_file &&
  10350.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  10351.                 debug(F110,
  10352.                       "ftp ssl auth unable to load ssl_crl_file",
  10353.                       ssl_crl_file,
  10354.                       0
  10355.                       );
  10356.                 if (ssl_debug_flag)
  10357.                   printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  10358.             }
  10359.             if (ssl_crl_dir &&
  10360.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  10361.                 debug(F110,
  10362.                       "ftp ssl auth unable to load ssl_crl_dir",
  10363.                       ssl_crl_dir,
  10364.                       0
  10365.                       );
  10366.                 if (ssl_debug_flag)
  10367.                   printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  10368.             }
  10369.         } else {
  10370.             X509_STORE_set_default_paths(crl_store);
  10371.         }
  10372.     }
  10373.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  10374.                        ssl_client_verify_callback);
  10375.     ssl_verify_depth = -1;
  10376.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  10377.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  10378.     SSL_set_fd(ssl_ftp_con,csocket);
  10379.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  10380.     if (ssl_cipher_list) {
  10381.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  10382.     } else {
  10383.         char * p;
  10384.         if (p = getenv("SSL_CIPHER")) {
  10385.             SSL_set_cipher_list(ssl_ftp_con,p);
  10386.         } else {
  10387.             SSL_set_cipher_list(ssl_ftp_con,DEFAULT_CIPHER_LIST);
  10388.         }
  10389.     }
  10390.     if (ssl_debug_flag) {
  10391.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  10392.         fflush(stderr);
  10393.     }
  10394.     if (SSL_connect(ssl_ftp_con) <= 0) {
  10395.         static char errbuf[1024];
  10396.         ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  10397.                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  10398.         fprintf(stderr,"%s\n", errbuf);
  10399.         fflush(stderr);
  10400.         ssl_ftp_active_flag=0;
  10401.         SSL_free(ssl_ftp_con);
  10402.         ssl_ftp_con = NULL;
  10403.     } else {
  10404.         ssl_ftp_active_flag = 1;
  10405.  
  10406.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  10407.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  10408.  
  10409.             if (!subject) {
  10410.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  10411.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  10412.                     return(ssl_ftp_active_flag = 0);
  10413.                 } else {
  10414.                     if (uq_ok("Warning: Server didn't provide a certificate\n",
  10415.                                "Continue? (Y/N)",3,NULL,0) <= 0) {
  10416.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  10417.                         return(ssl_ftp_active_flag = 0);
  10418.                     }
  10419.                 }
  10420.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_user_host)) {
  10421.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  10422.                 return(ssl_ftp_active_flag = 0);
  10423.             }
  10424.         }
  10425.         debug(F110,"ssl_auth","[SSL - OK]",0);
  10426.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  10427.     }
  10428.     if (ssl_debug_flag) {
  10429.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  10430.         fflush(stderr);
  10431.     }
  10432.     return(ssl_ftp_active_flag);
  10433. }
  10434. #endif /* CK_SSL */
  10435.  
  10436. static sigtype
  10437. cmdcancel(sig) int sig; {
  10438. #ifdef OS2
  10439.     /* In Unix we "chain" to trap(), which prints this */
  10440.     printf("^C...\n");
  10441. #endif /* OS2 */
  10442.     debug(F100,"ftp cmdcancel caught SIGINT ","",0);
  10443.     fflush(stdout);
  10444.     secure_getc(0,1);            /* Initialize net input buffers */
  10445.     cancelfile++;
  10446.     cancelgroup++;
  10447.     mlsreset();
  10448. #ifndef OS2
  10449. #ifdef FTP_PROXY
  10450.     if (ptflag)                         /* proxy... */
  10451.       longjmp(ptcancel,1);
  10452. #endif /* FTP_PROXY */
  10453.     debug(F100,"ftp cmdcancel chain to trap()...","",0);
  10454.     trap(SIGINT);
  10455.     /* NOTREACHED */
  10456.     debug(F100,"ftp cmdcancel return from trap()...","",0);
  10457. #else
  10458.     debug(F100,"ftp cmdcancel PostCtrlCSem()...","",0);
  10459.     PostCtrlCSem();
  10460. #endif /* OS2 */
  10461. }
  10462.  
  10463. static int
  10464. #ifdef CK_ANSIC
  10465. scommand(char * s)                      /* Was secure_command() */
  10466. #else
  10467. scommand(s) char * s;
  10468. #endif /* CK_ANSIC */
  10469. {
  10470.     int length = 0, len2;
  10471.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  10472. #ifdef CK_SSL
  10473.     if (ssl_ftp_active_flag) {
  10474.         int error, rc;
  10475.         length = strlen(s) + 2;
  10476.         length = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10477.         rc = SSL_write(ssl_ftp_con,out,length);
  10478.         error = SSL_get_error(ssl_ftp_con,rc);
  10479.         switch (error) {
  10480.           case SSL_ERROR_NONE:
  10481.             return(1);
  10482.           case SSL_ERROR_WANT_WRITE:
  10483.           case SSL_ERROR_WANT_READ:
  10484.           case SSL_ERROR_SYSCALL:
  10485. #ifdef NT
  10486.             {
  10487.                 int gle = GetLastError();
  10488.             }
  10489. #endif /* NT */
  10490.           case SSL_ERROR_WANT_X509_LOOKUP:
  10491.           case SSL_ERROR_SSL:
  10492.           case SSL_ERROR_ZERO_RETURN:
  10493.           default:
  10494.             lostpeer();
  10495.         }
  10496.         return(0);
  10497.     }
  10498. #endif /* CK_SSL */
  10499.  
  10500.     if (auth_type && ftp_cpl != FPL_CLR) {
  10501. #ifdef FTP_SRP
  10502.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  10503.           if ((length = srp_encode(ftp_cpl == FPL_PRV,
  10504.                                    (CHAR *)s,
  10505.                                    (CHAR *)out,
  10506.                                    strlen(s))) < 0) {
  10507.               fprintf(stderr, "SRP failed to encode message\n");
  10508.               return(0);
  10509.           }
  10510. #endif /* FTP_SRP */
  10511. #ifdef FTP_KRB4
  10512.         if (ck_krb4_is_installed() &&
  10513.             (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  10514.             if (ftp_cpl == FPL_PRV) {
  10515.                 length =
  10516.                   krb_mk_priv((CHAR *)s, (CHAR *)out,
  10517.                               strlen(s), ftp_sched,
  10518. #ifdef KRB524
  10519.                               ftp_cred.session,
  10520. #else /* KRB524 */
  10521.                               &ftp_cred.session,
  10522. #endif /* KRB524 */
  10523.                               &myctladdr, &hisctladdr);
  10524.             } else {
  10525.                 length =
  10526.                   krb_mk_safe((CHAR *)s,
  10527.                               (CHAR *)out,
  10528.                               strlen(s),
  10529. #ifdef KRB524
  10530.                               ftp_cred.session,
  10531. #else /* KRB524 */
  10532.                               &ftp_cred.session,
  10533. #endif /* KRB524 */
  10534.                               &myctladdr, &hisctladdr);
  10535.             }
  10536.             if (length == -1) {
  10537.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  10538.                         ftp_cpl == FPL_PRV ? "priv" : "safe");
  10539.                 return(0);
  10540.             }
  10541.         }
  10542. #endif /* FTP_KRB4 */
  10543. #ifdef FTP_GSSAPI
  10544.         /* Scommand (based on level) */
  10545.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  10546.             gss_buffer_desc in_buf, out_buf;
  10547.             OM_uint32 maj_stat, min_stat;
  10548.             int conf_state;
  10549.             in_buf.value = s;
  10550.             in_buf.length = strlen(s) + 1;
  10551.             maj_stat = gss_seal(&min_stat, gcontext,
  10552.                                 (ftp_cpl==FPL_PRV), /* private */
  10553.                                 GSS_C_QOP_DEFAULT,
  10554.                                 &in_buf, &conf_state,
  10555.                                 &out_buf);
  10556.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  10557.                 user_gss_error(maj_stat, min_stat,
  10558.                                (ftp_cpl==FPL_PRV)?
  10559.                                "gss_seal ENC didn't complete":
  10560.                                "gss_seal MIC didn't complete");
  10561.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  10562.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  10563.             } else {
  10564.                 if (ftp_deb)
  10565.                   fprintf(stderr, "sealed (%s) %d bytes\n",
  10566.                           ftp_cpl==FPL_PRV?"ENC":"MIC",
  10567.                           out_buf.length);
  10568.                 memcpy(out, out_buf.value,
  10569.                        length=out_buf.length);
  10570.                 gss_release_buffer(&min_stat, &out_buf);
  10571.             }
  10572.         }
  10573. #endif /* FTP_GSSAPI */
  10574.         /* Other auth types go here ... */
  10575.  
  10576.         len2 = FTP_BUFSIZ;
  10577.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  10578.                                    length, &len2, RADIX_ENCODE))
  10579.             ) {
  10580.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  10581.                     radix_error(kerror));
  10582.             return(0);
  10583.         }
  10584.         if (ftp_deb)
  10585.           fprintf(stderr, "scommand(%s)\nencoding %d bytes\n", s, length);
  10586.         len2 = ckmakmsg(out,
  10587.             FTP_BUFSIZ,
  10588.             ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  10589.                         in,
  10590.             "\r\n",
  10591.             NULL
  10592.             );
  10593.         send(csocket,(SENDARG2TYPE)out,len2,0);
  10594.     } else {
  10595.         char out[FTP_BUFSIZ];
  10596.         int len = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10597.         send(csocket,(SENDARG2TYPE)out,len,0);
  10598.     }
  10599.     return(1);
  10600. }
  10601.  
  10602. static int
  10603. mygetc() {
  10604.     static char inbuf[4096];
  10605.     static int bp = 0, ep = 0;
  10606.     int rc;
  10607.  
  10608.     if (bp == ep) {
  10609.         bp = ep = 0;
  10610. #ifdef CK_SSL
  10611.         if (ssl_ftp_active_flag) {
  10612.             int error;
  10613.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  10614.             error = SSL_get_error(ssl_ftp_con,rc);
  10615.             switch (error) {
  10616.               case SSL_ERROR_NONE:
  10617.                 break;
  10618.               case SSL_ERROR_WANT_WRITE:
  10619.               case SSL_ERROR_WANT_READ:
  10620.                 return(0);
  10621.               case SSL_ERROR_SYSCALL:
  10622.                 if (rc == 0) {          /* EOF */
  10623.                     break;
  10624.                 } else {
  10625. #ifdef NT
  10626.                     int gle = GetLastError();
  10627. #endif /* NT */
  10628.                     break;
  10629.                 }
  10630.               case SSL_ERROR_WANT_X509_LOOKUP:
  10631.               case SSL_ERROR_SSL:
  10632.               case SSL_ERROR_ZERO_RETURN:
  10633.               default:
  10634.                 break;
  10635.             }
  10636.         } else
  10637. #endif /* CK_SSL */
  10638.           rc = recv(csocket,(char *)inbuf,4096,0);
  10639.         if (rc <= 0)
  10640.           return(EOF);
  10641.         ep = rc;
  10642.     }
  10643.     return(inbuf[bp++]);
  10644. }
  10645.  
  10646. /*  x l a t e c  --  Translate a character  */
  10647. /*
  10648.     Call with:
  10649.       fc    = Function code: 0 = translate, 1 = initialize.
  10650.       c     = Character (as int).
  10651.       incs  = Index of charset to translate from.
  10652.       outcs = Index of charset to translate to.
  10653.  
  10654.     Returns:
  10655.       0: OK
  10656.      -1: Error
  10657. */
  10658. static int
  10659. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  10660. #ifdef NOCSETS
  10661.     return(c);
  10662. #else
  10663.     static char buf[128];
  10664.     static int cx;
  10665.     int c0, c1;
  10666.  
  10667.     if (fc == 1) {                      /* Initialize */
  10668.         cx = 0;                         /* Catch-up buffer write index */
  10669.         xgnbp = buf;                    /* Catch-up buffer read pointer */
  10670.         buf[0] = NUL;                   /* Buffer is empty */
  10671.         return(0);
  10672.     }
  10673.     if (cx >= 127) {                    /* Catch-up buffer full */
  10674.         debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  10675.         printf("?Translation buffer overflow\n");
  10676.         return(-1);
  10677.     }
  10678.     /* Add char to buffer. */
  10679.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  10680.  
  10681.     debug(F000,"xlatec buf",ckitoa(cx),c);
  10682.     buf[cx++] = c;
  10683.     buf[cx] = NUL;
  10684.  
  10685.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  10686.         if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)    /* (NULL was xprintc) */
  10687.           return(-1);
  10688.     }
  10689.     /* If we're caught up, reinitialize the buffer */
  10690.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  10691. #endif /* NOCSETS */
  10692. }
  10693.  
  10694.  
  10695. /*  p a r s e f e a t  */
  10696.  
  10697. /* Note: for convenience we align keyword values with table indices */
  10698. /* If you need to insert a new keyword, adjust the SFT_xxx definitions */
  10699.  
  10700. static struct keytab feattab[] = {
  10701.     { "$$$$", 0,        0 },        /* Dummy for sfttab[0] */
  10702.     { "AUTH", SFT_AUTH, 0 },
  10703.     { "LANG", SFT_LANG, 0 },
  10704.     { "MDTM", SFT_MDTM, 0 },
  10705.     { "MLST", SFT_MLST, 0 },
  10706.     { "PBSZ", SFT_PBSZ, 0 },
  10707.     { "PROT", SFT_PROT, 0 },
  10708.     { "REST", SFT_REST, 0 },
  10709.     { "SIZE", SFT_SIZE, 0 },
  10710.     { "TVFS", SFT_TVFS, 0 },
  10711.     { "UTF8", SFT_UTF8, 0 }
  10712. };
  10713. static int nfeattab = (sizeof(feattab) / sizeof(struct keytab));
  10714.  
  10715. #define FACT_CSET  1
  10716. #define FACT_CREA  2
  10717. #define FACT_LANG  3
  10718. #define FACT_MTYP  4
  10719. #define FACT_MDTM  5
  10720. #define FACT_PERM  6
  10721. #define FACT_SIZE  7
  10722. #define FACT_TYPE  8
  10723. #define FACT_UNIQ  9
  10724.  
  10725. static struct keytab facttab[] = {
  10726.     { "CHARSET",    FACT_CSET, 0 },
  10727.     { "CREATE",     FACT_CREA, 0 },
  10728.     { "LANG",       FACT_LANG, 0 },
  10729.     { "MEDIA-TYPE", FACT_MTYP, 0 },
  10730.     { "MODIFY",     FACT_MDTM, 0 },
  10731.     { "PERM",       FACT_PERM, 0 },
  10732.     { "SIZE",       FACT_SIZE, 0 },
  10733.     { "TYPE",       FACT_TYPE, 0 },
  10734.     { "UNIQUE",     FACT_UNIQ, 0 }
  10735. };
  10736. static int nfacttab = (sizeof(facttab) / sizeof(struct keytab));
  10737.  
  10738. static struct keytab ftyptab[] = {
  10739.     { "CDIR", FTYP_CDIR, 0 },
  10740.     { "DIR",  FTYP_DIR,  0 },
  10741.     { "FILE", FTYP_FILE, 0 },
  10742.     { "PDIR", FTYP_PDIR, 0 }
  10743. };
  10744. static int nftyptab = (sizeof(ftyptab) / sizeof(struct keytab));
  10745.  
  10746. static VOID
  10747. parsefeat(s) char * s; {        /* Parse a FEATURE response */
  10748.     char kwbuf[8];
  10749.     int i, x;
  10750.     if (!s) return;
  10751.     if (!*s) return;
  10752.     while (*s < '!')
  10753.       s++;
  10754.     for (i = 0; i < 4; i++) {
  10755.     if (s[i] < '!')
  10756.       break;
  10757.     kwbuf[i] = s[i];
  10758.     }
  10759.     if (s[i] && s[i] != SP && s[i] != CR && s[i] != LF)
  10760.       return;
  10761.     kwbuf[i] = NUL;
  10762.     /* xlookup requires a full (but case independent) match */
  10763.     i = xlookup(feattab,kwbuf,nfeattab,&x);
  10764.     debug(F111,"ftp parsefeat",s,i);
  10765.     if (i < 0 || i > 15)
  10766.       return;
  10767.  
  10768.     switch (i) {
  10769.       case SFT_MDTM:            /* Controlled by ENABLE/DISABLE */
  10770.     sfttab[i] = mdtmok;
  10771.     if (mdtmok) sfttab[0]++;
  10772.     break;
  10773.       case SFT_MLST:            /* ditto */
  10774.     sfttab[i] = mlstok;
  10775.     if (mlstok) sfttab[0]++;
  10776.     break;
  10777.       case SFT_SIZE:            /* ditto */
  10778.     sfttab[i] = sizeok;
  10779.     if (sizeok) sfttab[0]++;
  10780.     break;
  10781.       case SFT_AUTH:            /* ditto */
  10782.     sfttab[i] = ftp_aut;
  10783.     if (ftp_aut) sfttab[0]++;
  10784.     break;
  10785.       default:                /* Others */
  10786.     sfttab[0]++;
  10787.     sfttab[i]++;
  10788.     }
  10789. }
  10790.  
  10791. static char *
  10792. parsefacts(s) char * s; {        /* Parse MLS[DT] File Facts */
  10793.     char * p;
  10794.     int i, j, x;
  10795.     if (!s) return(NULL);
  10796.     if (!*s) return(NULL);
  10797.  
  10798.     /* Maybe we should make a copy of s so we can poke it... */
  10799.  
  10800.     while ((p = ckstrchr(s,'='))) {
  10801.     *p = NUL;            /* s points to fact */
  10802.     i = xlookup(facttab,s,nfacttab,&x); 
  10803.     debug(F111,"ftp parsefact fact",s,i);
  10804.     *p = '=';
  10805.     s = p+1;            /* Now s points to arg */
  10806.     p = ckstrchr(s,';');
  10807.         if (!p)
  10808.       p = ckstrchr(s,SP);
  10809.     if (!p) {
  10810.         debug(F110,"ftp parsefact end-of-val search fail",s,0);
  10811.         break;
  10812.     }
  10813.     *p = NUL;
  10814.     debug(F110,"ftp parsefact valu",s,0);
  10815.     switch (i) {
  10816.       case FACT_CSET:        /* Ignore these for now */
  10817.       case FACT_CREA:
  10818.       case FACT_LANG:
  10819.       case FACT_PERM:
  10820.       case FACT_MTYP:
  10821.       case FACT_UNIQ:
  10822.         break;
  10823.       case FACT_MDTM:        /* Modtime */
  10824.         makestr(&havemdtm,s);
  10825.         debug(F110,"ftp parsefact mdtm",havemdtm,0);
  10826.         break;
  10827.       case FACT_SIZE:        /* Size */
  10828.         havesize = ckatofs(s);
  10829.         debug(F101,"ftp parsefact size","",havesize);
  10830.         break;
  10831.       case FACT_TYPE:        /* Type */
  10832.         j = xlookup(ftyptab,s,nftyptab,NULL);
  10833.         debug(F111,"ftp parsefact type",s,j);
  10834.         havetype = (j < 1) ? 0 : j;
  10835.         break;
  10836.     }
  10837.     *p = ';';
  10838.     s = p+1;            /* s points next fact or name */
  10839.     }
  10840.     while (*s == SP)            /* Skip past spaces. */
  10841.       s++;
  10842.     if (!*s)                /* Make sure we still have a name */
  10843.       s = NULL;
  10844.     debug(F110,"ftp parsefact name",s,0);
  10845.     return(s);
  10846. }
  10847.  
  10848. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  10849.  
  10850. /* vbm = 1 (verbose); 0 (quiet except for error messages); 9 (super quiet) */
  10851.  
  10852. static int
  10853. getreply(expecteof,lcs,rcs,vbm,fc) int expecteof, lcs, rcs, vbm, fc; {
  10854.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  10855.     register int i, c, n;
  10856.     register int dig;
  10857.     register char *cp;
  10858.     int xlate = 0;
  10859.     int count = 0;
  10860.     int auth = 0;
  10861.     int originalcode = 0, continuation = 0;
  10862.     sig_t oldintr;
  10863.     int pflag = 0;
  10864.     char *pt = pasv;
  10865.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  10866.     int safe = 0;
  10867.     int xquiet = 0;
  10868.  
  10869.     auth = (fc == GRF_AUTH);
  10870.  
  10871. #ifndef NOCSETS
  10872.     debug(F101,"ftp getreply lcs","",lcs);
  10873.     debug(F101,"ftp getreply rcs","",rcs);
  10874.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  10875.         xlate = 1;
  10876.         initxlate(rcs,lcs);
  10877.         xlatec(1,0,rcs,lcs);
  10878.     }
  10879. #endif /* NOCSETS */
  10880.     debug(F101,"ftp getreply fc","",fc);
  10881.  
  10882.     if (quiet)
  10883.       xquiet = 1;
  10884.     if (vbm == 9) {
  10885.         xquiet = 1;
  10886.         vbm = 0;
  10887.     }
  10888.     if (ftp_deb)                        /* DEBUG */
  10889.       vbm = 1;
  10890.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  10891.       vbm = 0;
  10892.     else if (vbm < 0)                   /* VERBOSE */
  10893.       vbm = ftp_vbm;
  10894.  
  10895.     ibuf[0] = '\0';
  10896.     if (reply_parse)
  10897.       reply_ptr = reply_buf;
  10898.     havesigint = 0;
  10899.     oldintr = signal(SIGINT, cmdcancel);
  10900.     for (count = 0;; count++) {
  10901.         obuf[0] = '\0';
  10902.         dig = n = ftpcode = i = 0;
  10903.         cp = ftp_reply_str;
  10904.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  10905.             if (c == IAC) {             /* Handle telnet commands */
  10906.                 switch (c = mygetc()) {
  10907.                   case WILL:
  10908.                   case WONT:
  10909.                     c = mygetc();
  10910.                     obuf[0] = IAC;
  10911.                     obuf[1] = DONT;
  10912.                     obuf[2] = c;
  10913.                     obuf[3] = NUL;
  10914. #ifdef CK_SSL
  10915.                     if (ssl_ftp_active_flag) {
  10916.                         int error, rc;
  10917.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10918.                         error = SSL_get_error(ssl_ftp_con,rc);
  10919.                         switch (error) {
  10920.                           case SSL_ERROR_NONE:
  10921.                             break;
  10922.                           case SSL_ERROR_WANT_WRITE:
  10923.                           case SSL_ERROR_WANT_READ:
  10924.                             return(0);
  10925.                           case SSL_ERROR_SYSCALL:
  10926.                             if (rc == 0) { /* EOF */
  10927.                                 break;
  10928.                             } else {
  10929. #ifdef NT
  10930.                                 int gle = GetLastError();
  10931. #endif /* NT */
  10932.                                 break;
  10933.                             }
  10934.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10935.                           case SSL_ERROR_SSL:
  10936.                           case SSL_ERROR_ZERO_RETURN:
  10937.                           default:
  10938.                             break;
  10939.                         }
  10940.                     } else
  10941. #endif /* CK_SSL */
  10942.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10943.                     break;
  10944.                   case DO:
  10945.                   case DONT:
  10946.                     c = mygetc();
  10947.                     obuf[0] = IAC;
  10948.                     obuf[1] = WONT;
  10949.                     obuf[2] = c;
  10950.                     obuf[3] = NUL;
  10951. #ifdef CK_SSL
  10952.                     if (ssl_ftp_active_flag) {
  10953.                         int error, rc;
  10954.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10955.                         error = SSL_get_error(ssl_ftp_con,rc);
  10956.                         switch (error) {
  10957.                           case SSL_ERROR_NONE:
  10958.                             break;
  10959.                           case SSL_ERROR_WANT_WRITE:
  10960.                           case SSL_ERROR_WANT_READ:
  10961.                               signal(SIGINT,oldintr);
  10962.                               return(0);
  10963.                           case SSL_ERROR_SYSCALL:
  10964.                             if (rc == 0) { /* EOF */
  10965.                                 break;
  10966.                             } else {
  10967. #ifdef NT
  10968.                                 int gle = GetLastError();
  10969. #endif /* NT */
  10970.                                 break;
  10971.                             }
  10972.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10973.                           case SSL_ERROR_SSL:
  10974.                           case SSL_ERROR_ZERO_RETURN:
  10975.                           default:
  10976.                             break;
  10977.                         }
  10978.                     } else
  10979. #endif /* CK_SSL */
  10980.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10981.                     break;
  10982.                   default:
  10983.                     break;
  10984.                 }
  10985.                 continue;
  10986.             }
  10987.             dig++;
  10988.             if (c == EOF) {
  10989.                 if (expecteof) {
  10990.                     signal(SIGINT,oldintr);
  10991.                     ftpcode = 221;
  10992.                     debug(F101,"ftp getreply EOF","",ftpcode);
  10993.                     return(0);
  10994.                 }
  10995.                 lostpeer();
  10996.                 if (!xquiet) {
  10997.                     if (ftp_deb)
  10998.                       printf("421 ");
  10999.                     printf(
  11000.                       "Service not available, connection closed by server\n");
  11001.                     fflush(stdout);
  11002.                 }
  11003.                 signal(SIGINT,oldintr);
  11004.                 ftpcode = 421;
  11005.                 debug(F101,"ftp getreply EOF","",ftpcode);
  11006.                 return(4);
  11007.             }
  11008.             if (n == 0) {        /* First digit */
  11009.         n = c;            /* Save it */
  11010.         }
  11011.             if (auth_type &&
  11012. #ifdef CK_SSL
  11013.                 !ssl_ftp_active_flag &&
  11014. #endif /* CK_SSL */
  11015.                 !ibuf[0] && (n == '6' || continuation)) {
  11016.                 if (c != '\r' && dig > 4)
  11017.                   obuf[i++] = c;
  11018.             } else {
  11019.                 if (auth_type &&
  11020. #ifdef CK_SSL
  11021.                     !ssl_ftp_active_flag &&
  11022. #endif /* CK_SSL */
  11023.                     !ibuf[0] && dig == 1 && vbm)
  11024.                   printf("Unauthenticated reply received from server:\n");
  11025.                 if (reply_parse) {
  11026.                     *reply_ptr++ = c;
  11027.                     *reply_ptr = NUL;
  11028.                 }
  11029.                 if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  11030.                     ftp_cmdlin < 2) {
  11031.                     if ((c != '\r') &&
  11032.                         (ftp_deb || ((vbm || (!auth && n == '5')) &&
  11033.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
  11034.                         )))))
  11035.                     {
  11036. #ifdef FTP_PROXY
  11037.                         if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  11038.                           printf("%s:",ftp_host);
  11039. #endif /* FTP_PROXY */
  11040.  
  11041.                         if (!xquiet) {
  11042. #ifdef NOCSETS
  11043.                             printf("%c",c);
  11044. #else
  11045.                             if (xlate) {
  11046.                                 xlatec(0,c,rcs,lcs);
  11047.                             } else {
  11048.                                 printf("%c",c);
  11049.                             }
  11050. #endif /* NOCSETS */
  11051.                         }
  11052.                     }
  11053.                 }
  11054.             }
  11055.             if (auth_type &&
  11056. #ifdef CK_SSL
  11057.                 !ssl_ftp_active_flag &&
  11058. #endif /* CK_SSL */
  11059.                 !ibuf[0] && n != '6')
  11060.               continue;
  11061.             if (dig < 4 && isdigit(c))
  11062.               ftpcode = ftpcode * 10 + (c - '0');
  11063.             if (!pflag && ftpcode == 227)
  11064.               pflag = 1;
  11065.             if (dig > 4 && pflag == 1 && isdigit(c))
  11066.               pflag = 2;
  11067.             if (pflag == 2) {
  11068.                 if (c != '\r' && c != ')')
  11069.                   *pt++ = c;
  11070.                 else {
  11071.                     *pt = '\0';
  11072.                     pflag = 3;
  11073.                 }
  11074.             }
  11075.             if (dig == 4 && c == '-' && n != '6') {
  11076.                 if (continuation)
  11077.                   ftpcode = 0;
  11078.                 continuation++;
  11079.             }
  11080.             if (cp < &ftp_reply_str[FTP_BUFSIZ - 1]) {
  11081.                 *cp++ = c;
  11082.                 *cp = NUL;
  11083.             }
  11084.         }
  11085.         if (deblog ||
  11086. #ifdef COMMENT
  11087. /*
  11088.   Sometimes we need to print the server reply.  printlines is nonzero for any
  11089.   command where the results are sent back on the control connection rather
  11090.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  11091.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  11092.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  11093.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  11094.   lines from UNIX.  Further experimentation needed with other servers.  Of
  11095.   course RFC959 is mute as to the format of the server reply.
  11096.  
  11097.   'printlines' is also true for PWD and BYE.
  11098. */
  11099.         (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  11100. #else
  11101. /* No, we can't be that clever -- it breaks other things like RPWD... */
  11102.             (printlines &&
  11103.              (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  11104. #endif /* COMMENT */
  11105.             ) {
  11106.             char * q = cp;
  11107.             char *r = ftp_reply_str;
  11108.             *q-- = NUL;                 /* NUL-terminate */
  11109.             while (*q < '!' && q > r)   /* Strip CR, etc */
  11110.               *q-- = NUL;
  11111.             if (!ftp_deb && printlines) { /* If printing */
  11112.                 if (ftpcode != 0)       /* strip ftpcode if any */
  11113.                   r += 4;
  11114. #ifdef NOCSETS
  11115.                 printf("%s\n",r);       /* and print */
  11116. #else
  11117.                 if (!xlate) {
  11118.                     printf("%s\n",r);
  11119.                 } else {        /* Translating */
  11120.                     xgnbp = r;        /* Set up strgetc() */
  11121.                     while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  11122.                         if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {    /* (xprintc) */
  11123.                             signal(SIGINT,oldintr);
  11124.                             return(-1);
  11125.                         }
  11126.                     }
  11127.                     printf("\n");
  11128.                 }
  11129. #endif /* NOCSETS */
  11130.             }
  11131.         }
  11132.     debug(F110,"FTP RCVD ",ftp_reply_str,0);
  11133.  
  11134.     if (fc == GRF_FEAT) {        /* Parsing FEAT command response? */
  11135.         if (count == 0 && n == '2') {
  11136.         int i;            /* (Re)-init server FEATure table */
  11137.         debug(F100,"ftp getreply clearing feature table","",0);
  11138.         for (i = 0; i < 16; i++)
  11139.           sfttab[i] = 0;
  11140.         } else {
  11141.         parsefeat((char *)ftp_reply_str);
  11142.         }
  11143.     }
  11144.         if (auth_type &&
  11145. #ifdef CK_SSL
  11146.             !ssl_ftp_active_flag &&
  11147. #endif /* CK_SSL */
  11148.              !ibuf[0] && n != '6') {
  11149.             signal(SIGINT,oldintr);
  11150.             return(getreply(expecteof,lcs,rcs,vbm,auth));
  11151.         }
  11152.         ibuf[0] = obuf[i] = '\0';
  11153.         if (ftpcode && n == '6')
  11154.           if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  11155.               printf("Unknown reply: %d %s\n", ftpcode, obuf);
  11156.               n = '5';
  11157.           } else safe = (ftpcode == 631);
  11158.         if (obuf[0]                     /* if there is a string to decode */
  11159. #ifdef CK_SSL
  11160.             && !ssl_ftp_active_flag     /* and not SSL/TLS */
  11161. #endif /* CK_SSL */
  11162.             ) {
  11163.             if (!auth_type) {
  11164.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  11165.                 n = '5';
  11166.             }
  11167. #ifndef CK_ENCRYPTION
  11168.             else if (ftpcode == 632) {
  11169.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  11170.                 n = '5';
  11171.             }
  11172. #endif /* CK_ENCRYPTION */
  11173. #ifdef NOCONFIDENTIAL
  11174.             else if (ftpcode == 633) {
  11175.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  11176.                 n = '5';
  11177.             }
  11178. #endif /* NOCONFIDENTIAL */
  11179.             else {
  11180.                 int len = FTP_BUFSIZ;
  11181.                 if ((kerror = radix_encode((CHAR *)obuf,
  11182.                                            (CHAR *)ibuf,
  11183.                                            0,
  11184.                                            &len,
  11185.                                            RADIX_DECODE))
  11186.                     ) {
  11187.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  11188.                            ftpcode, radix_error(kerror), obuf);
  11189.                     n = '5';
  11190.                 }
  11191. #ifdef FTP_SRP
  11192.                 else if (strcmp(auth_type, "SRP") == 0) {
  11193.                     int outlen;
  11194.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  11195.                                         (CHAR *) ibuf, len);
  11196.                     if (outlen < 0) {
  11197.                         printf("Warning: %d reply %s!\n",
  11198.                                ftpcode, safe ? "modified" : "garbled");
  11199.                         n = '5';
  11200.                     } else {
  11201.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  11202.                         if (ftp_deb)
  11203.                           printf("%c:", safe ? 'S' : 'P');
  11204.                         continue;
  11205.                     }
  11206.                 }
  11207. #endif /* FTP_SRP */
  11208. #ifdef FTP_KRB4
  11209.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  11210.                     if (safe) {
  11211.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  11212. #ifdef KRB524
  11213.                                              ftp_cred.session,
  11214. #else /* KRB524 */
  11215.                                              &ftp_cred.session,
  11216. #endif /* KRB524 */
  11217.                                              &hisctladdr,
  11218.                                              &myctladdr,
  11219.                                              &ftp_msg_data
  11220.                                              );
  11221.                     } else {
  11222.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  11223.                                              ftp_sched,
  11224. #ifdef KRB524
  11225.                                              ftp_cred.session,
  11226. #else /* KRB524 */
  11227.                                              &ftp_cred.session,
  11228. #endif /* KRB524 */
  11229.                                              &hisctladdr,
  11230.                                              &myctladdr,
  11231.                                              &ftp_msg_data
  11232.                                              );
  11233.                     }
  11234.                     if (kerror != KSUCCESS) {
  11235.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  11236.                                safe ? "modified" : "garbled",
  11237.                                safe ? "safe" : "priv",
  11238.                                krb_get_err_text(kerror));
  11239.                         n = '5';
  11240.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  11241.                         kerror = KFAILURE;
  11242.                         n = '5';
  11243.                         printf("reply data too large for buffer\n");
  11244.                     } else {
  11245.                         if (ftp_deb)
  11246.                           printf("%c:", safe ? 'S' : 'P');
  11247.                         memcpy(ibuf,ftp_msg_data.app_data,
  11248.                                ftp_msg_data.app_length);
  11249.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  11250.                                   FTP_BUFSIZ - ftp_msg_data.app_length);
  11251.                         continue;
  11252.                     }
  11253.                 }
  11254. #endif /* FTP_KRB4 */
  11255. #ifdef FTP_GSSAPI
  11256.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  11257.                     gss_buffer_desc xmit_buf, msg_buf;
  11258.                     OM_uint32 maj_stat, min_stat;
  11259.                     int conf_state;
  11260.                     xmit_buf.value = ibuf;
  11261.                     xmit_buf.length = len;
  11262.                     /* decrypt/verify the message */
  11263.                     conf_state = safe;
  11264.                     maj_stat = gss_unseal(&min_stat, gcontext,
  11265.                                           &xmit_buf, &msg_buf,
  11266.                                           &conf_state, NULL);
  11267.                     if (maj_stat != GSS_S_COMPLETE) {
  11268.                         user_gss_error(maj_stat, min_stat,
  11269.                                        "failed unsealing reply");
  11270.                         n = '5';
  11271.                     } else {
  11272.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  11273.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  11274.                                   FTP_BUFSIZ-msg_buf.length);
  11275.                         gss_release_buffer(&min_stat,&msg_buf);
  11276.                         if (ftp_deb)
  11277.                           printf("%c:", safe ? 'S' : 'P');
  11278.                         continue;
  11279.                     }
  11280.                 }
  11281. #endif /* FTP_GSSAPI */
  11282.                 /* Other auth types go here... */
  11283.             }
  11284.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  11285.                    !xquiet && (vbm || (!auth && (n == '4' || n == '5')))) {
  11286. #ifdef NOCSETS
  11287.             printf("%c",c);
  11288. #else
  11289.             if (xlate) {
  11290.                 xlatec(0,c,rcs,lcs);
  11291.             } else {
  11292.                 printf("%c",c);
  11293.             }
  11294. #endif /* NOCSETS */
  11295.             fflush (stdout);
  11296.         }
  11297.         if (continuation && ftpcode != originalcode) {
  11298.             if (originalcode == 0)
  11299.               originalcode = ftpcode;
  11300.             continue;
  11301.         }
  11302.         *cp = '\0';
  11303.         if (n != '1')
  11304.           cpend = 0;
  11305.         signal(SIGINT,oldintr);
  11306.         if (ftpcode == 421 || originalcode == 421) {
  11307.         lostpeer();
  11308.         if (!xquiet && !ftp_deb)
  11309.           printf("%s\n",reply_buf);
  11310.         }
  11311.         if ((cancelfile != 0) &&
  11312. #ifndef ULTRIX3
  11313.             /* Ultrix 3.0 cc objects violently to this clause */
  11314.             (oldintr != cmdcancel) &&
  11315. #endif /* ULTRIX3 */
  11316.             (oldintr != SIG_IGN)) {
  11317.             if (oldintr)
  11318.               (*oldintr)(SIGINT);
  11319.         }
  11320.         if (reply_parse) {
  11321.             *reply_ptr = '\0';
  11322.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  11323.                 reply_parse = reply_ptr + strlen(reply_parse);
  11324.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  11325.                   *reply_ptr = '\0';
  11326.             } else
  11327.               reply_parse = reply_ptr;
  11328.         }
  11329.         while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
  11330.           *cp-- = NUL;
  11331.         debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  11332.         return(n - '0');
  11333.     } /* for (;;) */
  11334. }
  11335.  
  11336. #ifdef BSDSELECT
  11337. static int
  11338. #ifdef CK_ANSIC
  11339. empty(fd_set * mask, int sec)
  11340. #else
  11341. empty(mask, sec) fd_set * mask; int sec;
  11342. #endif /* CK_ANSIC */
  11343. {
  11344.     struct timeval t;
  11345.     t.tv_sec = (long) sec;
  11346.     t.tv_usec = 0L;
  11347.     debug(F100,"ftp empty calling select...","",0);
  11348. #ifdef INTSELECT
  11349.     x = select(32, (int *)mask, NULL, NULL, &t);
  11350. #else
  11351.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  11352. #endif /* INTSELECT */
  11353.     debug(F101,"ftp empty select","",x);
  11354.     return(x);
  11355. }
  11356. #else /* BSDSELECT */
  11357. #ifdef IBMSELECT
  11358. static int
  11359. empty(mask, cnt, sec) int * mask, sec;
  11360.                       int   cnt;
  11361. {
  11362.     return(select(mask,cnt,0,0,sec*1000));
  11363. }
  11364. #endif /* IBMSELECT */
  11365. #endif /* BSDSELECT */
  11366.  
  11367. static sigtype
  11368. cancelsend(sig) int sig; {
  11369.     havesigint++;
  11370.     cancelgroup++;
  11371.     cancelfile = 0;
  11372.     printf(" Canceled...\n");
  11373.     secure_getc(0,1);            /* Initialize net input buffers */
  11374.     debug(F100,"ftp cancelsend caught SIGINT ","",0);
  11375.     fflush(stdout);
  11376. #ifndef OS2
  11377.     longjmp(sendcancel, 1);
  11378. #else
  11379.     PostCtrlCSem();
  11380. #endif /* OS2 */
  11381. }
  11382.  
  11383. static VOID
  11384. #ifdef CK_ANSIC
  11385. secure_error(char *fmt, ...)
  11386. #else
  11387. /* VARARGS1 */
  11388. secure_error(fmt, p1, p2, p3, p4, p5)
  11389.    char *fmt; int p1, p2, p3, p4, p5;
  11390. #endif /* CK_ANSIC */
  11391. {
  11392. #ifdef CK_ANSIC
  11393.     va_list ap;
  11394.  
  11395.     va_start(ap, fmt);
  11396.     vfprintf(stderr, fmt, ap);
  11397.     va_end(ap);
  11398. #else
  11399.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  11400. #endif
  11401.     fprintf(stderr, "\n");
  11402. }
  11403.  
  11404. /*
  11405.  * Internal form of settype; changes current type in use with server
  11406.  * without changing our notion of the type for data transfers.
  11407.  * Used to change to and from ascii for listings.
  11408.  */
  11409. static VOID
  11410. changetype(newtype, show) int newtype, show; {
  11411.     int rc;
  11412.     char * s;
  11413.  
  11414.     if ((newtype == curtype) && typesent++)
  11415.       return;
  11416.     switch (newtype) {
  11417.       case FTT_ASC:
  11418.         s = "A";
  11419.         break;
  11420.       case FTT_BIN:
  11421.         s = "I";
  11422.         break;
  11423.       case FTT_TEN:
  11424.         s = "L 8";
  11425.         break;
  11426.       default:
  11427.         s = "I";
  11428.         break;
  11429.     }
  11430.     rc = ftpcmd("TYPE",s,-1,-1,show);
  11431.     if (rc == REPLY_COMPLETE)
  11432.       curtype = newtype;
  11433. }
  11434.  
  11435. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  11436.  
  11437. static VOID
  11438. #ifdef CK_ANSIC
  11439. doftpsend(void * threadinfo)
  11440. #else
  11441. doftpsend(threadinfo) VOID * threadinfo;
  11442. #endif
  11443. {
  11444. #ifdef NTSIG
  11445.     if (threadinfo) {                   /* Thread local storage... */
  11446.         TlsSetValue(TlsIndex,threadinfo);
  11447.         debug(F100, "doftpsend called with threadinfo block","", 0);
  11448.     } else debug(F100, "doftpsend - threadinfo is NULL", "", 0);
  11449. #endif /* NTSIG */
  11450. #ifdef CK_LOGIN
  11451. #ifdef IKSD
  11452. #ifdef NT
  11453.     if (inserver)
  11454.       setntcreds();
  11455. #endif /* NT */
  11456. #endif /* IKSD */
  11457. #endif /* CK_LOGIN */
  11458.  
  11459.     if (initconn()) {
  11460. #ifndef NOHTTP
  11461.         int y = -1;
  11462.         /* debug(F101,"doftpsend","tcp_http_proxy",tcp_http_proxy); */
  11463.  
  11464.        /*  If the connection failed and we are using an HTTP Proxy
  11465.         *  and the reason for the failure was an authentication
  11466.         *  error, then we need to give the user to ability to
  11467.         *  enter a username and password, just like a browser.
  11468.         *
  11469.         *  I tried to do all of this within the netopen() call
  11470.         *  but it is much too much work.
  11471.         */
  11472.         while (y != 0 && tcp_http_proxy != NULL ) {
  11473.  
  11474.             if (tcp_http_proxy_errno == 401 ||
  11475.                  tcp_http_proxy_errno == 407 ) {
  11476.                 char uid[UIDBUFLEN];
  11477.                 char pwd[PWDSIZ];
  11478.                 struct txtbox tb[2];
  11479.                 int ok;
  11480.  
  11481.                 tb[0].t_buf = uid;
  11482.                 tb[0].t_len = UIDBUFLEN;
  11483.                 tb[0].t_lbl = "Proxy Userid: ";
  11484.                 tb[0].t_dflt = NULL;
  11485.                 tb[0].t_echo = 1;
  11486.                 tb[1].t_buf = pwd;
  11487.                 tb[1].t_len = 256;
  11488.                 tb[1].t_lbl = "Proxy Passphrase: ";
  11489.                 tb[1].t_dflt = NULL;
  11490.                 tb[1].t_echo = 2;
  11491.  
  11492.                 ok = uq_mtxt("Proxy Server Authentication Required\n",
  11493.                               NULL, 2, tb);
  11494.                 if (ok && uid[0]) {
  11495.                     char * proxy_user, * proxy_pwd;
  11496.  
  11497.                     proxy_user = tcp_http_proxy_user;
  11498.                     proxy_pwd  = tcp_http_proxy_pwd;
  11499.  
  11500.                     tcp_http_proxy_user = uid;
  11501.                     tcp_http_proxy_pwd = pwd;
  11502.  
  11503.                     y = initconn();
  11504.  
  11505.                     debug(F101,"doftpsend","initconn",y);
  11506.                     memset(pwd,0,PWDSIZ);
  11507.                     tcp_http_proxy_user = proxy_user;
  11508.                     tcp_http_proxy_pwd = proxy_pwd;
  11509.                 } else
  11510.                     break;
  11511.             } else
  11512.                 break;
  11513.         }
  11514.  
  11515.         if ( y != 0 ) {
  11516. #endif /* NOHTTP */
  11517.             signal(SIGINT, ftpsnd.oldintr);
  11518. #ifdef SIGPIPE
  11519.             if (ftpsnd.oldintp)
  11520.               signal(SIGPIPE, ftpsnd.oldintp);
  11521. #endif /* SIGPIPE */
  11522.             ftpcode = -1;
  11523.             zclose(ZIFILE);
  11524.             ftpsndret = -1;
  11525. #ifdef NTSIG
  11526.             ckThreadEnd(threadinfo);
  11527. #endif /* NTSIG */
  11528.             return;
  11529. #ifndef NOHTTP
  11530.         }
  11531. #endif /* NOHTTP */
  11532.     }
  11533.     ftpsndret = 0;
  11534. #ifdef NTSIG
  11535.      ckThreadEnd(threadinfo);
  11536. #endif /* NTSIG */
  11537. }
  11538.  
  11539. static VOID
  11540. #ifdef CK_ANSIC
  11541. failftpsend(void * threadinfo)
  11542. #else
  11543. failftpsend(threadinfo) VOID * threadinfo;
  11544. #endif /* CK_ANSIC */
  11545. {
  11546. #ifdef NTSIG
  11547.     if (threadinfo) {                   /* Thread local storage... */
  11548.         TlsSetValue(TlsIndex,threadinfo);
  11549.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11550.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11551. #endif /* NTSIG */
  11552. #ifdef CK_LOGIN
  11553. #ifdef IKSD
  11554. #ifdef NT
  11555.     if (inserver)
  11556.       setntcreds();
  11557. #endif /* NT */
  11558. #endif /* IKSD */
  11559. #endif /* CK_LOGIN */
  11560.  
  11561.     while (cpend) {
  11562.         ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11563.         debug(F111,"ftp sendrequest getreply","null command",ftpsnd.reply);
  11564.     }
  11565.     if (data >= 0) {
  11566. #ifdef CK_SSL
  11567.         if (ssl_ftp_data_active_flag) {
  11568.             SSL_shutdown(ssl_ftp_data_con);
  11569.             SSL_free(ssl_ftp_data_con);
  11570.             ssl_ftp_data_active_flag = 0;
  11571.             ssl_ftp_data_con = NULL;
  11572.         }
  11573. #endif /* CK_SSL */
  11574. #ifdef TCPIPLIB
  11575.         socket_close(data);
  11576. #else /* TCPIPLIB */
  11577. #ifdef USE_SHUTDOWN
  11578.         shutdown(data, 1+1);
  11579. #endif /* USE_SHUTDOWN */
  11580.         close(data);
  11581. #endif /* TCPIPLIB */
  11582.         data = -1;
  11583.         globaldin = -1;
  11584.     }
  11585.     if (ftpsnd.oldintr)
  11586.         signal(SIGINT,ftpsnd.oldintr);
  11587. #ifdef SIGPIPE
  11588.     if (ftpsnd.oldintp)
  11589.         signal(SIGPIPE,ftpsnd.oldintp);
  11590. #endif /* SIGPIPE */
  11591.     ftpcode = -1;
  11592. #ifndef OS2
  11593.     /* TEST ME IN K95 */
  11594.     if (havesigint) {
  11595.     havesigint = 0;
  11596.     debug(F100,"ftp failftpsend chain to trap()...","",0);
  11597.     if (ftpsnd.oldintr != SIG_IGN)
  11598.       (*ftpsnd.oldintr)(SIGINT);
  11599.     /* NOTREACHED (I hope!) */
  11600.     debug(F100,"ftp failftpsend return from trap()...","",0);
  11601.     }
  11602. #endif /* OS2 */
  11603. }
  11604.  
  11605. static VOID
  11606. #ifdef CK_ANSIC
  11607. failftpsend2(void * threadinfo)
  11608. #else
  11609. failftpsend2(threadinfo) VOID * threadinfo;
  11610. #endif /* CK_ANSIC */
  11611. {
  11612. #ifdef NTSIG
  11613.     if (threadinfo) {                   /* Thread local storage... */
  11614.         TlsSetValue(TlsIndex,threadinfo);
  11615.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11616.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11617. #endif /* NTSIG */
  11618. #ifdef CK_LOGIN
  11619. #ifdef IKSD
  11620. #ifdef NT
  11621.     if (inserver)
  11622.       setntcreds();
  11623. #endif /* NT */
  11624. #endif /* IKSD */
  11625. #endif /* CK_LOGIN */
  11626.  
  11627.     debug(F101,"ftp sendrequest canceled","",ftpsnd.bytes);
  11628.     tfc += ffc;
  11629. #ifdef GFTIMER
  11630.     fpfsecs = gftimer();
  11631. #endif /* GFTIMER */
  11632.     zclose(ZIFILE);
  11633. #ifdef PIPESEND
  11634.     if (sndfilter)
  11635.       pipesend = 0;
  11636. #endif /* PIPESEND */
  11637.     signal(SIGINT, ftpsnd.oldintr);
  11638. #ifdef SIGPIPE
  11639.     if (ftpsnd.oldintp)
  11640.       signal(SIGPIPE, ftpsnd.oldintp);
  11641. #endif /* SIGPIPE */
  11642.     if (!cpend) {
  11643.         ftpcode = -1;
  11644.         ftpsndret = -1;
  11645. #ifdef NTSIG
  11646.         ckThreadEnd(threadinfo);
  11647. #endif /* NTSIG */
  11648.         return;
  11649.     }
  11650.     if (data >= 0) {
  11651. #ifdef CK_SSL
  11652.         if (ssl_ftp_data_active_flag) {
  11653.             SSL_shutdown(ssl_ftp_data_con);
  11654.             SSL_free(ssl_ftp_data_con);
  11655.             ssl_ftp_data_active_flag = 0;
  11656.             ssl_ftp_data_con = NULL;
  11657.         }
  11658. #endif /* CK_SSL */
  11659. #ifdef TCPIPLIB
  11660.         socket_close(data);
  11661. #else /* TCPIPLIB */
  11662. #ifdef USE_SHUTDOWN
  11663.         shutdown(data, 1+1);
  11664. #endif /* USE_SHUTDOWN */
  11665.         close(data);
  11666. #endif /* TCPIPLIB */
  11667.         data = -1;
  11668.         globaldin = -1;
  11669.     }
  11670.     if (dout) {
  11671. #ifdef TCPIPLIB
  11672.         socket_close(dout);
  11673. #else /* TCPIPLIB */
  11674. #ifdef USE_SHUTDOWN
  11675.         shutdown(dout, 1+1);
  11676. #endif /* USE_SHUTDOWN */
  11677.         close(dout);
  11678. #endif /* TCPIPLIB */
  11679.     }
  11680.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11681.     ftpcode = -1;
  11682.     ftpsndret = -1;
  11683.  
  11684. #ifndef OS2
  11685.     /* TEST ME IN K95 */
  11686.     if (havesigint) {
  11687.     havesigint = 0;
  11688.     debug(F100,"ftp failftpsend2 chain to trap()...","",0);
  11689.     if (ftpsnd.oldintr != SIG_IGN)
  11690.       (*ftpsnd.oldintr)(SIGINT);
  11691.     /* NOTREACHED (I hope!) */
  11692.     debug(F100,"ftp failftpsend2 return from trap()...","",0);
  11693.     }
  11694. #endif /* OS2 */
  11695. }
  11696.  
  11697. static VOID
  11698. #ifdef CK_ANSIC
  11699. doftpsend2(void * threadinfo)
  11700. #else
  11701. doftpsend2(threadinfo) VOID * threadinfo;
  11702. #endif
  11703. {
  11704.     register int c, d = 0;
  11705.     int n, t, x, notafile, unique = 0;
  11706.     char *buf, *bufp;
  11707.     
  11708. #ifdef NTSIG
  11709.     if (threadinfo) {                   /* Thread local storage... */
  11710.         TlsSetValue(TlsIndex,threadinfo);
  11711.         debug(F100, "doftpsend2 called with threadinfo block","", 0);
  11712.     } else debug(F100, "doftpsend2 - threadinfo is NULL", "", 0);
  11713. #endif /* NTSIG */
  11714. #ifdef CK_LOGIN
  11715. #ifdef IKSD
  11716. #ifdef NT
  11717.     if (inserver)
  11718.       setntcreds();
  11719. #endif /* NT */
  11720. #endif /* IKSD */
  11721. #endif /* CK_LOGIN */
  11722.  
  11723.     buf = ftpsndbuf;            /* (not on stack) */
  11724.  
  11725.     unique = strcmp(ftpsnd.cmd,"STOU") ? 0 : 1;
  11726.     notafile = sndarray || pipesend;
  11727.  
  11728. #ifdef FTP_RESTART
  11729.     if (ftpsnd.restart && ((curtype == FTT_BIN) || (alike > 0))) {
  11730.         char * p;
  11731.         changetype(FTT_BIN,0);          /* Change to binary */
  11732.  
  11733.         /* Ask for remote file's size */
  11734.         x = ftpcmd("SIZE",ftpsnd.remote,ftpsnd.incs,ftpsnd.outcs,ftp_vbm);
  11735.  
  11736.         if (x == REPLY_COMPLETE) {      /* Have ftpsnd.reply */
  11737.             p = &ftp_reply_str[4];      /* Parse it */
  11738.             while (isdigit(*p)) {
  11739.                 sendstart = sendstart * 10 + (int)(*p - '0');
  11740.                 p++;
  11741.             }
  11742.             if (*p && *p != CR) {       /* Bad number */
  11743.                 debug(F110,"doftpsend2 bad size",ftp_reply_str,0);
  11744.                 sendstart = (CK_OFF_T)0;
  11745.             } else if (sendstart > fsize) { /* Remote file bigger than local */
  11746.                 debug(F110,"doftpsend2 big size",ckfstoa(fsize),sendstart);
  11747.                 sendstart = (CK_OFF_T)0;
  11748.             }
  11749.         /* Local is newer */
  11750.             debug(F111,"doftpsend2 size",ftpsnd.remote,sendstart);
  11751.             if (chkmodtime(ftpsnd.local,ftpsnd.remote,0) == 2) {
  11752.                 debug(F110,"doftpsend2 date mismatch",ftp_reply_str,0);
  11753.                 sendstart = (CK_OFF_T)0; /* Send the whole file */
  11754.             }
  11755.         }
  11756.         changetype(ftp_typ,0);          /* Change back to appropriate type */
  11757.         if (sendstart > (CK_OFF_T)0) {    /* Still restarting? */
  11758.             if (sendstart == fsize) {   /* Same size - no need to send */
  11759.                 debug(F111,"doftpsend2 /restart SKIP",
  11760.               ckfstoa(fsize),sendstart);
  11761.                 zclose(ZIFILE);
  11762.                 ftpsndret = SKP_RES;
  11763. #ifdef NTSIG
  11764.                 ckThreadEnd(threadinfo);
  11765. #endif /* NTSIG */
  11766.                 return;
  11767.             }
  11768.             errno = 0;                  /* Restart needed, seek to the spot */
  11769.             if (zfseek((long)sendstart) < 0) {
  11770.                 debug(F111,"doftpsend2 zfseek fails",
  11771.               ftpsnd.local,sendstart);
  11772.                 fprintf(stderr, "FSEEK: %s: %s\n", ftpsnd.local, ck_errstr());
  11773.                 sendstart = 0;
  11774.                 zclose(ZIFILE);
  11775.                 ftpsndret = -1;
  11776. #ifdef NTSIG
  11777.                 ckThreadEnd(threadinfo);
  11778. #endif /* NTSIG */
  11779.                 return;
  11780.             }
  11781. #ifdef COMMENT
  11782.             debug(F111,"doftpsend2 zfseek ok",ftpsnd.local,sendstart);
  11783.             x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  11784.             if (x != REPLY_CONTINUE) {
  11785.                 sendstart = 0;
  11786.                 zclose(ZIFILE);
  11787.                 ftpsndret = -1;
  11788. #ifdef NTSIG
  11789.                 ckThreadEnd(threadinfo);
  11790. #endif /* NTSIG */
  11791.                 return;
  11792.             } else {
  11793.                 ftpsnd.cmd = "STOR";
  11794.             }
  11795. #else
  11796.             sendmode = SM_RESEND;
  11797.             ftpsnd.cmd = "APPE";
  11798. #endif /* COMMENT */
  11799.             /* sendstart = (CK_OFF_T)0; */
  11800.         }
  11801.     }
  11802. #endif /* FTP_RESTART */
  11803.  
  11804.     if (unique && !stouarg)        /* If we know STOU accepts no arg */
  11805.       ftpsnd.remote = NULL;        /* don't include one. */
  11806.  
  11807.     x = ftpcmd(ftpsnd.cmd, ftpsnd.remote, ftpsnd.incs, ftpsnd.outcs, ftp_vbm);
  11808.     debug(F111,"doftpsend2 ftpcode",ftpsnd.cmd,ftpcode);
  11809.     debug(F101,"doftpsend2 ftpcmd","",x);
  11810.  
  11811.     if (x != REPLY_PRELIM && unique) {
  11812.     /*
  11813.       RFC959 says STOU does not take an argument.  But every FTP server
  11814.       I've encountered but one accepts the arg and constructs the unique
  11815.       name from it, which is better than making up a totally random name
  11816.       for the file, which is what RFC959 calls for.  Especially because
  11817.       there is no way for the client to find out the name chosen by the
  11818.       server.  So we try STOU with the argument first, which works with
  11819.       most servers, and if it fails we retry it without the arg, for
  11820.       the benefit of the one picky server that is not "liberal in what
  11821.       it accepts" UNLESS the first STOU got a 502 code ("not implemented")
  11822.       which means STOU is not accepted, period.
  11823.     */
  11824.     if ((x == 5) && stouarg && (ftpcode != 502)) {
  11825.         x = ftpcmd(ftpsnd.cmd,NULL,ftpsnd.incs,ftpsnd.outcs,ftp_vbm); 
  11826.         if (x == REPLY_PRELIM)    /* If accepted */
  11827.           stouarg = 0;        /* flag no STOU arg for this server */
  11828.     }
  11829.     }
  11830.     if (x != REPLY_PRELIM) {
  11831.         signal(SIGINT, ftpsnd.oldintr);
  11832. #ifdef SIGPIPE
  11833.         if (ftpsnd.oldintp)
  11834.           signal(SIGPIPE, ftpsnd.oldintp);
  11835. #endif /* SIGPIPE */
  11836.     debug(F101,"doftpsend2 not REPLY_PRELIM","",x);
  11837.         zclose(ZIFILE);
  11838. #ifdef PIPESEND
  11839.         if (sndfilter)
  11840.           pipesend = 0;
  11841. #endif /* PIPESEND */
  11842.         ftpsndret = -1;
  11843. #ifdef NTSIG
  11844.         ckThreadEnd(threadinfo);
  11845. #endif /* NTSIG */
  11846.         return;
  11847.     }
  11848.     debug(F100,"doftpsend2 getting data connection...","",0);
  11849.     dout = dataconn(ftpsnd.lmode);             /* Get data connection */
  11850.     debug(F101,"doftpsend2 dataconn","",dout);
  11851.     if (dout == -1) {
  11852.         failftpsend2(threadinfo);
  11853. #ifdef NTSIG
  11854.         ckThreadEnd(threadinfo);
  11855. #endif /* NTSIG */
  11856.         return;
  11857.     }
  11858.     /* Initialize per-file stats */
  11859.     ffc = (CK_OFF_T)0;            /* Character counter */
  11860.     cps = oldcps = 0L;                  /* Thruput */
  11861.     n = 0;
  11862. #ifdef GFTIMER
  11863.     rftimer();                          /* reset f.p. timer */
  11864. #endif /* GFTIMER */
  11865.  
  11866. #ifdef SIGPIPE
  11867.     ftpsnd.oldintp = signal(SIGPIPE, SIG_IGN);
  11868. #endif /* SIGPIPE */
  11869.     debug(F101,"doftpsend2 curtype","",curtype);
  11870.     switch (curtype) {
  11871.       case FTT_BIN:                     /* Binary mode */
  11872.       case FTT_TEN:
  11873.         errno = d = 0;
  11874. #ifdef VMS
  11875.     /*
  11876.       This is because VMS zxin() is C-Library fread() 
  11877.           but the file was opened with zopeni(), which is RMS.
  11878.     */
  11879.     while (((c = zminchar()) > -1) && !cancelfile) {
  11880.         ffc++;
  11881.         if (zzout(dout,c) < 0)
  11882.           break;
  11883.     }
  11884. #else  /* VMS */
  11885.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ - 1)) > 0 && !cancelfile) {
  11886.             ftpsnd.bytes += n;
  11887.             ffc += n;
  11888.             debug(F111,"doftpsend2 zxin",ckltoa(n),ffc);
  11889.             hexdump("doftpsend2 zxin",buf,16);
  11890. #ifdef CK_SSL
  11891.             if (ssl_ftp_data_active_flag) {
  11892.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11893.                     if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  11894.                       break;
  11895.                     spackets++;
  11896.                     pktnum++;
  11897.                     if (fdispla != XYFD_B) {
  11898.                         spktl = d;
  11899.                         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  11900.                     }
  11901.                 }
  11902.             } else {
  11903. #endif /* CK_SSL */
  11904.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11905.                     if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  11906.                         || iscanceled())
  11907.                       break;
  11908.                     spackets++;
  11909.                     pktnum++;
  11910.                     if (fdispla != XYFD_B) {
  11911.                         spktl = d;
  11912.                         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  11913.                     }
  11914.                 }
  11915. #ifdef CK_SSL
  11916.             }
  11917. #endif /* CK_SSL */
  11918.             if (d <= 0)
  11919.               break;
  11920.         }
  11921. #endif    /* VMS */
  11922.  
  11923.     debug(F111,"doftpsend2 XX zxin",ckltoa(n),ffc);
  11924.         if (n < 0)
  11925.           fprintf(stderr, "local: %s: %s\n", ftpsnd.local, ck_errstr());
  11926.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  11927.             if (d == -1 && errno && errno != EPIPE)
  11928.               perror("netout");
  11929.             ftpsnd.bytes = -1;
  11930.         }
  11931.         break;
  11932.  
  11933.       case FTT_ASC:                     /* Text mode */
  11934. #ifndef NOCSETS
  11935.         if (ftpsnd.xlate) {             /* With translation */
  11936.             initxlate(ftpsnd.incs,ftpsnd.outcs);
  11937.             while (!cancelfile) {
  11938.                 if ((c0 = xgnbyte(FC_UCS2,ftpsnd.incs,NULL)) < 0) break;
  11939.                 if ((x = xpnbyte(c0,TC_UCS2,ftpsnd.outcs,xxout)) < 0) break;
  11940.             }
  11941.         } else {
  11942. #endif /* NOCSETS */
  11943.             /* Text mode, no translation */
  11944.             while (((c = zminchar()) > -1) && !cancelfile) {
  11945.                 ffc++;
  11946.         if (xxout(c) < 0)
  11947.           break;
  11948.             }
  11949.             d = 0;
  11950. #ifndef NOCSETS
  11951.         }
  11952. #endif /* NOCSETS */
  11953.         if (dout == -1 || (d = secure_flush(dout)) < 0) {
  11954.             if (d == -1 && errno && errno != EPIPE)
  11955.               perror("netout");
  11956.             ftpsnd.bytes = -1;
  11957.         }
  11958.         break;
  11959.     }
  11960.     tfc += ffc;                         /* Total file chars */
  11961. #ifdef GFTIMER
  11962.     fpfsecs = gftimer();
  11963. #endif /* GFTIMER */
  11964.     zclose(ZIFILE);                     /* Close input file */
  11965. #ifdef PIPESEND
  11966.     if (sndfilter)                      /* Undo this (it's per file) */
  11967.       pipesend = 0;
  11968. #endif /* PIPESEND */
  11969.  
  11970. #ifdef CK_SSL
  11971.         if (ssl_ftp_data_active_flag) {
  11972.             SSL_shutdown(ssl_ftp_data_con);
  11973.             SSL_free(ssl_ftp_data_con);
  11974.             ssl_ftp_data_active_flag = 0;
  11975.             ssl_ftp_data_con = NULL;
  11976.         }
  11977. #endif /* CK_SSL */
  11978.  
  11979. #ifdef TCPIPLIB
  11980.     socket_close(dout);                 /* Close data connection */
  11981. #else /* TCPIPLIB */
  11982. #ifdef USE_SHUTDOWN
  11983.     shutdown(dout, 1+1);
  11984. #endif /* USE_SHUTDOWN */
  11985.     close(dout);
  11986. #endif /* TCPIPLIB */
  11987.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11988.     signal(SIGINT, ftpsnd.oldintr);            /* Put back interrupts */
  11989. #ifdef SIGPIPE
  11990.     if (ftpsnd.oldintp)
  11991.       signal(SIGPIPE, ftpsnd.oldintp);
  11992. #endif /* SIGPIPE */
  11993.     if (ftpsnd.reply == REPLY_TRANSIENT || ftpsnd.reply == REPLY_ERROR) {
  11994.         debug(F101,"doftpsend2 ftpsnd.reply","",ftpsnd.reply);
  11995.         ftpsndret = -1;
  11996. #ifdef NTSIG
  11997.         ckThreadEnd(threadinfo);
  11998. #endif /* NTSIG */
  11999.         return;
  12000.     } else if (cancelfile) {
  12001.         debug(F101,"doftpsend2 canceled","",ftpsnd.bytes);
  12002.         ftpsndret = -1;
  12003. #ifdef NTSIG
  12004.         ckThreadEnd(threadinfo);
  12005. #endif /* NTSIG */
  12006.         return;
  12007.     }
  12008.     debug(F101,"doftpsend2 ok","",ftpsnd.bytes);
  12009.     ftpsndret = 0;
  12010. #ifdef NTSIG
  12011.      ckThreadEnd(threadinfo);
  12012. #endif /* NTSIG */
  12013. }
  12014.  
  12015. static int
  12016. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  12017.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  12018. {
  12019.     if (!remote) remote = "";           /* Check args */
  12020.     if (!*remote) remote = local;
  12021.     if (!local) local = "";
  12022.     if (!*local) return(-1);
  12023.     if (!cmd) cmd = "";
  12024.     if (!*cmd) cmd = "STOR";
  12025.  
  12026.     debug(F111,"ftp sendrequest restart",local,restart);
  12027.  
  12028.     nout = 0;                           /* Init output buffer count */
  12029.     ftpsnd.bytes = 0;                   /* File input byte count */
  12030.     dout = -1;
  12031.  
  12032. #ifdef FTP_PROXY
  12033.     if (proxy) {
  12034.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  12035.         return(0);
  12036.     }
  12037. #endif /* FTP_PROXY */
  12038.  
  12039.     changetype(ftp_typ,0);              /* Change type for this file */
  12040.  
  12041.     ftpsnd.oldintr = NULL;        /* Set up interrupt handler */
  12042.     ftpsnd.oldintp = NULL;
  12043.     ftpsnd.restart = restart;
  12044.     ftpsnd.xlate = xlate;
  12045.     ftpsnd.lmode = "wb";
  12046.  
  12047. #ifdef PIPESEND                         /* Use Kermit API for file i/o... */
  12048.     if (sndfilter) {
  12049.         char * p = NULL, * q;
  12050. #ifndef NOSPL
  12051.         int n = CKMAXPATH;
  12052.         if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  12053.             q = p;
  12054.             debug(F110,"sendrequest pipesend filter",sndfilter,0);
  12055.             zzstring(sndfilter,&p,&n);
  12056.             debug(F111,"sendrequest pipename",q,n);
  12057.             if (n <= 0) {
  12058.                 printf("?Sorry, send filter + filename too long, %d max.\n",
  12059.                        CKMAXPATH
  12060.                        );
  12061.                 free(q);
  12062.                 return(-1);
  12063.             }
  12064.             ckstrncpy(filnam,q,CKMAXPATH+1);
  12065.             free(q);
  12066.             local = filnam;
  12067.         }
  12068. #endif /* NOSPL */
  12069.     }
  12070.  
  12071.     if (sndfilter)                      /* If sending thru a filter */
  12072.       pipesend = 1;                     /* set this for open and i/o */
  12073. #endif /* PIPESEND */
  12074.     
  12075. #ifdef VMS
  12076.     debug(F101,"XXX before openi binary","",binary);
  12077.     debug(F101,"XXX before openi ftp_typ","",ftp_typ);
  12078. #endif    /* VMS */
  12079.  
  12080.     if (openi(local) == 0)        /* Try to open the input file */
  12081.       return(-1);
  12082.  
  12083. #ifdef VMS
  12084.     debug(F101,"XXX after openi binary","",binary);
  12085.     debug(F101,"XXX after openi ftp_typ","",ftp_typ);
  12086.     if (!forcetype) {
  12087.     if (binary != ftp_typ) {    /* VMS zopeni() sets binary */
  12088.         debug(F101,"XXX changing type","",binary);
  12089.         doftptyp(binary);
  12090.         debug(F101,"XXX after doftptyp","",ftp_typ);
  12091.  
  12092.         /* **** */
  12093.         if (displa && fdispla) {    /* Update file type display */
  12094.         ftscreen(SCR_FN,'F',(CK_OFF_T)0,local);
  12095.         }
  12096.     }
  12097.     }
  12098. #endif    /* VMS */
  12099.     ftpsndret = 0;
  12100.     ftpsnd.incs = incs;
  12101.     ftpsnd.outcs = outcs;
  12102.     ftpsnd.cmd = cmd;
  12103.     ftpsnd.local = local;
  12104.     ftpsnd.remote = remote;
  12105.     ftpsnd.oldintr = signal(SIGINT, cancelsend);
  12106.     havesigint = 0;
  12107.  
  12108.     if (cc_execute(ckjaddr(sendcancel), doftpsend, failftpsend) < 0)
  12109.       return(-1);
  12110.     if (ftpsndret < 0)
  12111.       return(-1);
  12112.     if (cc_execute(ckjaddr(sendcancel), doftpsend2, failftpsend2) < 0)
  12113.       return(-1);
  12114.  
  12115.     return(ftpsndret);
  12116. }
  12117.  
  12118. static sigtype
  12119. cancelrecv(sig) int sig; {
  12120.     havesigint++;
  12121.     cancelfile = 0;
  12122.     cancelgroup++;
  12123.     secure_getc(0,1);            /* Initialize net input buffers */
  12124.     printf(" Canceling...\n");
  12125.     debug(F100,"ftp cancelrecv caught SIGINT","",0);
  12126.     fflush(stdout);
  12127.     if (fp_nml) {
  12128.         if (fp_nml != stdout)
  12129.           fclose(fp_nml);
  12130.         fp_nml = NULL;
  12131.     }
  12132. #ifndef OS2
  12133.     longjmp(recvcancel, 1);
  12134. #else
  12135.     PostCtrlCSem();
  12136. #endif /* OS2 */
  12137. }
  12138.  
  12139. /* Argumentless front-end for secure_getc() */
  12140.  
  12141. static int
  12142. netgetc() {
  12143.     return(secure_getc(globaldin,0));
  12144. }
  12145.  
  12146. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  12147.  
  12148. /*
  12149.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  12150.     -1: interrupted by user.
  12151.     -2: error opening or writing output file (reason in errno).
  12152.     -3: failure to make data connection.
  12153.     -4: network read error (reason in errno).
  12154. */
  12155.  
  12156. struct xx_ftprecv {
  12157.     int reply;
  12158.     int fcs;
  12159.     int rcs;
  12160.     int recover;
  12161.     int xlate;
  12162.     int din;
  12163.     int is_retr;
  12164.     sig_t oldintr, oldintp;
  12165.     char * cmd;
  12166.     char * local;
  12167.     char * remote;
  12168.     char * lmode;
  12169.     char * pipename;
  12170.     int    tcrflag;
  12171.     CK_OFF_T localsize;
  12172. };
  12173. static struct xx_ftprecv ftprecv;
  12174.  
  12175. static int ftprecvret = 0;
  12176.  
  12177. static VOID
  12178. #ifdef CK_ANSIC
  12179. failftprecv(VOID * threadinfo)
  12180. #else
  12181. failftprecv(threadinfo) VOID * threadinfo;
  12182. #endif /* CK_ANSIC */
  12183. {
  12184. #ifdef NTSIG
  12185.     if (threadinfo) {                   /* Thread local storage... */
  12186.         TlsSetValue(TlsIndex,threadinfo);
  12187.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12188.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12189. #endif /* NTSIG */
  12190.  
  12191. #ifdef CK_LOGIN
  12192. #ifdef IKSD
  12193. #ifdef NT
  12194.     if (inserver)
  12195.       setntcreds();
  12196. #endif /* NT */
  12197. #endif /* IKSD */
  12198. #endif /* CK_LOGIN */
  12199.  
  12200.     while (cpend) {
  12201.         ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  12202.     }
  12203.     if (data >= 0) {
  12204. #ifdef CK_SSL
  12205.         if (ssl_ftp_data_active_flag) {
  12206.             SSL_shutdown(ssl_ftp_data_con);
  12207.             SSL_free(ssl_ftp_data_con);
  12208.             ssl_ftp_data_active_flag = 0;
  12209.             ssl_ftp_data_con = NULL;
  12210.         }
  12211. #endif /* CK_SSL */
  12212. #ifdef TCPIPLIB
  12213.         socket_close(data);
  12214. #else /* TCPIPLIB */
  12215. #ifdef USE_SHUTDOWN
  12216.         shutdown(data, 1+1);
  12217. #endif /* USE_SHUTDOWN */
  12218.         close(data);
  12219. #endif /* TCPIPLIB */
  12220.         data = -1;
  12221.         globaldin = -1;
  12222.     }
  12223.     if (ftprecv.oldintr)
  12224.       signal(SIGINT, ftprecv.oldintr);
  12225.     ftpcode = -1;
  12226.     ftprecvret = -1;
  12227.  
  12228. #ifndef OS2
  12229.     /* TEST ME IN K95 */
  12230.     if (havesigint) {
  12231.     havesigint = 0;
  12232.     debug(F100,"ftp failftprecv chain to trap()...","",0);
  12233.     if (ftprecv.oldintr != SIG_IGN)
  12234.       (*ftprecv.oldintr)(SIGINT);
  12235.     /* NOTREACHED (I hope!) */
  12236.     debug(F100,"ftp failftprecv return from trap()...","",0);
  12237.     }
  12238. #endif /* OS2 */
  12239.     return;
  12240. }
  12241.  
  12242. static VOID
  12243. #ifdef CK_ANSIC
  12244. doftprecv(VOID * threadinfo)
  12245. #else
  12246. doftprecv(threadinfo) VOID * threadinfo;
  12247. #endif /* CK_ANSIC */
  12248. {
  12249. #ifdef NTSIG
  12250.     if (threadinfo) {                   /* Thread local storage... */
  12251.         TlsSetValue(TlsIndex,threadinfo);
  12252.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12253.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12254. #endif /* NTSIG */
  12255. #ifdef CK_LOGIN
  12256. #ifdef IKSD
  12257. #ifdef NT
  12258.     if (inserver)
  12259.       setntcreds();
  12260. #endif /* NT */
  12261. #endif /* IKSD */
  12262. #endif /* CK_LOGIN */
  12263.  
  12264. #ifndef COMMENT
  12265.     if (!out2screen && !ftprecv.pipename) {
  12266.     int x;
  12267.     char * local;
  12268.     local = ftprecv.local;
  12269.     x = zchko(local);
  12270.         if (x < 0) {
  12271.             if ((!dpyactive || ftp_deb))
  12272.               fprintf(stderr,
  12273.               "Temporary file %s: %s\n", ftprecv.local, ck_errstr());
  12274.             signal(SIGINT, ftprecv.oldintr);
  12275.             ftpcode = -2;
  12276.             ftprecvret = -1;
  12277. #ifdef NTSIG
  12278.             ckThreadEnd(threadinfo);
  12279. #endif /* NTSIG */
  12280.             return;
  12281.         }
  12282.     }
  12283. #endif /* COMMENT */
  12284.     changetype((!ftprecv.is_retr) ? FTT_ASC : ftp_typ, 0);
  12285.     if (initconn()) {                   /* Initialize the data connection */
  12286.         signal(SIGINT, ftprecv.oldintr);
  12287.         ftpcode = -1;
  12288.         ftprecvret = -3;
  12289. #ifdef NTSIG
  12290.         ckThreadEnd(threadinfo);
  12291. #endif /* NTSIG */
  12292.         return;
  12293.     }
  12294.     secure_getc(0,1);            /* Initialize net input buffers */
  12295.     ftprecvret = 0;
  12296.  
  12297. #ifdef NTSIG
  12298.     ckThreadEnd(threadinfo);
  12299. #endif /* NTSIG */
  12300. }
  12301.  
  12302. static VOID
  12303. #ifdef CK_ANSIC
  12304. failftprecv2(VOID * threadinfo)
  12305. #else
  12306. failftprecv2(threadinfo) VOID * threadinfo;
  12307. #endif /* CK_ANSIC */
  12308. {
  12309. #ifdef NTSIG
  12310.     if (threadinfo) {                   /* Thread local storage... */
  12311.         TlsSetValue(TlsIndex,threadinfo);
  12312.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12313.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12314. #endif /* NTSIG */
  12315. #ifdef CK_LOGIN
  12316. #ifdef IKSD
  12317. #ifdef NT
  12318.     if (inserver)
  12319.       setntcreds();
  12320. #endif /* NT */
  12321. #endif /* IKSD */
  12322. #endif /* CK_LOGIN */
  12323.  
  12324.     /* Cancel using RFC959 recommended IP,SYNC sequence  */
  12325.  
  12326.     debug(F100,"ftp recvrequest CANCEL","",0);
  12327. #ifdef GFTIMER
  12328.     fpfsecs = gftimer();
  12329. #endif /* GFTIMER */
  12330. #ifdef SIGPIPE
  12331.     if (ftprecv.oldintp)
  12332.       signal(SIGPIPE, ftprecv.oldintr);
  12333. #endif /* SIGPIPE */
  12334.     signal(SIGINT, SIG_IGN);
  12335.     if (!cpend) {
  12336.         ftpcode = -1;
  12337.         signal(SIGINT, ftprecv.oldintr);
  12338.         ftprecvret = -1;
  12339. #ifdef NTSIG
  12340.         ckThreadEnd(threadinfo);
  12341. #endif /* NTSIG */
  12342.         return;
  12343.     }
  12344.     cancel_remote(ftprecv.din);
  12345.  
  12346. #ifdef FTP_TIMEOUT
  12347.     if (ftp_timed_out && out2screen && !quiet)
  12348.       printf("\n?Timed out.\n");
  12349. #endif    /* FTP_TIMEOUT */
  12350.  
  12351.     if (ftpcode > -1)
  12352.       ftpcode = -1;
  12353.     if (data >= 0) {
  12354. #ifdef CK_SSL
  12355.         if (ssl_ftp_data_active_flag) {
  12356.             SSL_shutdown(ssl_ftp_data_con);
  12357.             SSL_free(ssl_ftp_data_con);
  12358.             ssl_ftp_data_active_flag = 0;
  12359.             ssl_ftp_data_con = NULL;
  12360.         }
  12361. #endif /* CK_SSL */
  12362. #ifdef TCPIPLIB
  12363.         socket_close(data);
  12364. #else /* TCPIPLIB */
  12365. #ifdef USE_SHUTDOWN
  12366.         shutdown(data, 1+1);
  12367. #endif /* USE_SHUTDOWN */
  12368.         close(data);
  12369. #endif /* TCPIPLIB */
  12370.         data = -1;
  12371.         globaldin = -1;
  12372.     }
  12373.     if (!out2screen) {
  12374.     int x = 0;
  12375.     debug(F111,"ftp failrecv2 zclose",ftprecv.local,keep);
  12376.     zclose(ZOFILE);
  12377.     switch (keep) {            /* which is... */
  12378.       case SET_AUTO:        /* AUTO */
  12379.         if (curtype == FTT_ASC)    /* Delete file if TYPE A. */
  12380.           x = 1;
  12381.         break;
  12382.       case SET_OFF:            /* DISCARD */
  12383.         x = 1;            /* Delete file, period. */
  12384.         break;
  12385.       default:            /* KEEP */
  12386.         break;
  12387.     }
  12388.     if (x) {
  12389.         x = zdelet(ftprecv.local);
  12390.         debug(F111,"ftp failrecv2 delete incomplete",ftprecv.local,x);
  12391.     }
  12392.     }
  12393.     if (ftprecv.din) {
  12394. #ifdef TCPIPLIB
  12395.         socket_close(ftprecv.din);
  12396. #else /* TCPIPLIB */
  12397. #ifdef USE_SHUTDOWN
  12398.         shutdown(ftprecv.din, 1+1);
  12399. #endif /* USE_SHUTDOWN */
  12400.         close(ftprecv.din);
  12401. #endif /* TCPIPLIB */
  12402.     }
  12403.     signal(SIGINT, ftprecv.oldintr);
  12404.     ftprecvret = -1;
  12405.  
  12406.     if (havesigint) {
  12407.     havesigint = 0;
  12408.     debug(F100,"FTP failftprecv2 chain to trap()...","",0);
  12409. #ifdef OS2
  12410.         debug(F100,"FTP failftprecv2 PostCtrlCSem()...","",0);
  12411.         PostCtrlCSem();
  12412. #else /* OS2 */
  12413.     if (ftprecv.oldintr != SIG_IGN)
  12414.       (*ftprecv.oldintr)(SIGINT);
  12415.     /* NOTREACHED (I hope!) */
  12416.     debug(F100,"ftp failftprecv2 return from trap()...","",0);
  12417. #endif /* OS2 */
  12418.     }
  12419. }
  12420.  
  12421. static VOID
  12422. #ifdef CK_ANSIC
  12423. doftprecv2(VOID * threadinfo)
  12424. #else
  12425. doftprecv2(threadinfo) VOID * threadinfo;
  12426. #endif /* CK_ANSIC */
  12427. {
  12428.     register int c, d;
  12429.     CK_OFF_T bytes = (CK_OFF_T)0;
  12430.     int bare_lfs = 0;
  12431.     int blksize = 0;
  12432.     ULONG start = 0L, stop;
  12433.     char * p;
  12434.     static char * rcvbuf = NULL;
  12435.     static int rcvbufsiz = 0;
  12436. #ifdef CK_URL
  12437.     char newname[CKMAXPATH+1];        /* For file dialog */
  12438. #endif /* CK_URL */
  12439.     extern int adl_ask;
  12440.  
  12441. #ifdef FTP_TIMEOUT
  12442.     ftp_timed_out = 0;
  12443. #endif    /* FTP_TIMEOUT */
  12444.  
  12445.     ftprecv.din = -1;
  12446. #ifdef NTSIG
  12447.     if (threadinfo) {                   /* Thread local storage... */
  12448.         TlsSetValue(TlsIndex,threadinfo);
  12449.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12450.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12451. #endif /* NTSIG */
  12452. #ifdef CK_LOGIN
  12453. #ifdef IKSD
  12454. #ifdef NT
  12455.     if (inserver)
  12456.       setntcreds();
  12457. #endif /* NT */
  12458. #endif /* IKSD */
  12459. #endif /* CK_LOGIN */
  12460.  
  12461.     if (ftprecv.recover) {                      /* Initiate recovery */
  12462.         x = ftpcmd("REST",ckfstoa(ftprecv.localsize),-1,-1,ftp_vbm);
  12463.         debug(F111,"ftp reply","REST",x);
  12464.         if (x == REPLY_CONTINUE) {
  12465.             ftprecv.lmode = "ab";
  12466.             rs_len = ftprecv.localsize;
  12467.         } else {
  12468.             ftprecv.recover = 0;
  12469.         }
  12470.     }
  12471.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  12472.  
  12473.     debug(F111,"ftp recvrequest recover E",ftprecv.remote,ftprecv.recover);
  12474.  
  12475.     /* Send the command and get reply */
  12476.     debug(F110,"ftp recvrequest cmd",ftprecv.cmd,0);
  12477.     debug(F110,"ftp recvrequest remote",ftprecv.remote,0);
  12478.  
  12479.     if (ftpcmd(ftprecv.cmd,ftprecv.remote,ftprecv.fcs,ftprecv.rcs,ftp_vbm)
  12480.     != REPLY_PRELIM) {
  12481.         signal(SIGINT, ftprecv.oldintr); /* Bad reply, fail. */
  12482.         ftprecvret = -1;        /* ftpcode is set by ftpcmd() */
  12483. #ifdef NTSIG
  12484.         ckThreadEnd(threadinfo);
  12485. #endif /* NTSIG */
  12486.         return;
  12487.     }
  12488.     ftprecv.din = dataconn("r");        /* Good reply, open data connection */
  12489.     globaldin = ftprecv.din;            /* Global copy of file descriptor */
  12490.     if (ftprecv.din == -1) {            /* Check for failure */
  12491.         ftpcode = -3;                   /* Code for no data connection */
  12492.         ftprecvret = -1;
  12493. #ifdef NTSIG
  12494.         ckThreadEnd(threadinfo);
  12495. #endif /* NTSIG */
  12496.         return;
  12497.     }
  12498. #ifdef CK_URL
  12499.     /* In K95 GUI put up a file box */
  12500.     if (haveurl && g_url.pth && adl_ask    ) { /* Downloading from a URL */
  12501.     int x;
  12502.     char * preface =
  12503. "\r\nIncoming file from FTP server...\r\n\
  12504. Please confirm output file specification or supply an alternative:";
  12505.  
  12506.     x = uq_file(preface,        /* K95 GUI: Put up file box. */
  12507.             NULL,
  12508.             4,
  12509.             NULL,
  12510.             ftprecv.local ? ftprecv.local : ftprecv.remote,
  12511.             newname,
  12512.             CKMAXPATH+1
  12513.             );
  12514.     if (x > 0) {
  12515.         ftprecv.local = newname;    /* Substitute user's file name */
  12516.         if (x == 2)            /* And append if user said to */
  12517.           ftprecv.lmode = "ab";
  12518.     }
  12519.     }
  12520. #endif /* CK_URL */
  12521.     x = 1;                              /* Output file open OK? */
  12522.     if (ftprecv.pipename) {        /* Command */
  12523.         x = zxcmd(ZOFILE,ftprecv.pipename);
  12524.         debug(F111,"ftp recvrequest zxcmd",ftprecv.pipename,x);
  12525.     } else if (!out2screen) {           /* File */
  12526.         struct filinfo xx;
  12527.         xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  12528.         xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  12529.     /* Append or New */
  12530.         xx.dsp = !strcmp(ftprecv.lmode,"ab") ? XYFZ_A : XYFZ_N;
  12531.         x = zopeno(ZOFILE,ftprecv.local,NULL,&xx);
  12532.         debug(F111,"ftp recvrequest zopeno",ftprecv.local,x);
  12533.     }
  12534.     if (x < 1) {                        /* Failure to open output file */
  12535.         if ((!dpyactive || ftp_deb))
  12536.           fprintf(stderr, "local(2): %s: %s\n", ftprecv.local, ck_errstr());
  12537.         ftprecvret = -1;
  12538. #ifdef NTSIG
  12539.         ckThreadEnd(threadinfo);
  12540. #endif /* NTSIG */
  12541.         return;
  12542.     }
  12543.     blksize = FTP_BUFSIZ;               /* Allocate input buffer */
  12544.  
  12545.     debug(F101,"ftp recvrequest blksize","",blksize);
  12546.     debug(F101,"ftp recvrequest rcvbufsiz","",rcvbufsiz);
  12547.  
  12548.     if (rcvbufsiz < blksize) {          /* if necessary */
  12549.         if (rcvbuf) {
  12550.             free(rcvbuf);
  12551.             rcvbuf = NULL;
  12552.         }
  12553.         rcvbuf = (char *)malloc((unsigned)blksize);
  12554.         if (!rcvbuf) {
  12555.         debug(F100,"ftp get rcvbuf malloc failed","",0);
  12556.             ftpcode = -2;
  12557. #ifdef ENOMEM
  12558.             errno = ENOMEM;
  12559. #endif /* ENOMEM */
  12560.             if ((!dpyactive || ftp_deb))
  12561.               perror("malloc");
  12562.             rcvbufsiz = 0;
  12563.             ftprecvret = -1;
  12564. #ifdef NTSIG
  12565.             ckThreadEnd(threadinfo);
  12566. #endif /* NTSIG */
  12567.             return;
  12568.         }
  12569.     debug(F101,"ftp get rcvbuf malloc ok","",blksize);
  12570.         rcvbufsiz = blksize;
  12571.     }
  12572.     debug(F111,"ftp get rcvbufsiz",ftprecv.local,rcvbufsiz);
  12573.  
  12574.     ffc = (CK_OFF_T)0;            /* Character counter */
  12575.     cps = oldcps = 0L;                  /* Thruput */
  12576.     start = gmstimer();                 /* Start time (msecs) */
  12577. #ifdef GFTIMER
  12578.     rftimer();                          /* Start time (float) */
  12579. #endif /* GFTIMER */
  12580.  
  12581.     debug(F111,"ftp get type",ftprecv.local,curtype);
  12582.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  12583.     switch (curtype) {
  12584.       case FTT_BIN:                     /* Binary mode */
  12585.       case FTT_TEN:                     /* TENEX mode */
  12586.         d = 0;
  12587.         while (1) {
  12588.             errno = 0;
  12589.             c = secure_read(ftprecv.din, rcvbuf, rcvbufsiz);
  12590.             if (cancelfile) {
  12591.                 failftprecv2(threadinfo);
  12592. #ifdef NTSIG
  12593.                 ckThreadEnd(threadinfo);
  12594. #endif /* NTSIG */
  12595.                 return;
  12596.             }
  12597.             if (c < 1)
  12598.               break;
  12599. #ifdef printf                           /* (What if it isn't?) */
  12600.             if (out2screen && !ftprecv.pipename) {
  12601.                 int i;
  12602.                 for (i = 0; i < c; i++)
  12603.                   printf("%c",rcvbuf[i]);
  12604.             } else
  12605. #endif /* printf */
  12606.               {
  12607.                 register int i;
  12608.                 i = 0;
  12609.                 errno = 0;
  12610.                 while (i < c) {
  12611.                     if (zmchout(rcvbuf[i++]) < 0) {
  12612.                         d = i;
  12613.                         break;
  12614.                     }
  12615.                 }
  12616.             }
  12617.             bytes += c;
  12618.             ffc += c;
  12619.         }
  12620. #ifdef FTP_TIMEOUT
  12621.     if (c == -3) {
  12622.             debug(F100,"ftp recvrequest timeout","",0); 
  12623.             bytes = (CK_OFF_T)-1;
  12624.         ftp_timed_out = 1;
  12625.         ftpcode = -3;
  12626.     } else
  12627. #endif    /* FTP_TIMEOUT */
  12628.         if (c < 0) {
  12629.             debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  12630.             if (c == -1 && errno != EPIPE)
  12631.               if ((!dpyactive || ftp_deb))
  12632.                 perror("netin");
  12633.             bytes = (CK_OFF_T)-1;
  12634.             ftpcode = -4;
  12635.         }
  12636.         if (d < c) {
  12637.             ftpcode = -2;
  12638.             if ((!dpyactive || ftp_deb)) {
  12639.                 char * p;
  12640.                 p = ftprecv.local ? ftprecv.local : ftprecv.pipename;
  12641.                 if (d < 0)
  12642.                   fprintf(stderr,
  12643.               "local(3): %s: %s\n", ftprecv.local, ck_errstr());
  12644.                 else
  12645.                   fprintf(stderr,
  12646.               "%s: short write\n", ftprecv.local);
  12647.             }
  12648.         }
  12649.         break;
  12650.  
  12651.       case FTT_ASC:                     /* Text mode */
  12652.     debug(F101,"ftp recvrequest TYPE A xlate","",ftprecv.xlate);
  12653. #ifndef NOCSETS
  12654.         if (ftprecv.xlate) {
  12655.             int t;
  12656. #ifdef CK_ANSIC
  12657.             int (*fn)(char);
  12658. #else
  12659.             int (*fn)();
  12660. #endif /* CK_ANSIC */
  12661.             debug(F110,"ftp recvrequest (data)","initxlate",0);
  12662.             initxlate(ftprecv.rcs,ftprecv.fcs);         /* (From,To) */
  12663.             if (ftprecv.pipename) {
  12664.                 fn = pipeout;
  12665.                 debug(F110,"ftp recvrequest ASCII","pipeout",0);
  12666.             } else {
  12667.                 fn = out2screen ? scrnout : putfil;
  12668.                 debug(F110,"ftp recvrequest ASCII",
  12669.                       out2screen ? "scrnout" : "putfil",0);
  12670.             }
  12671.             while (1) {
  12672.         /* Get byte from net */
  12673.                 c0 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12674.                 if (cancelfile) {
  12675.                     failftprecv2(threadinfo);
  12676. #ifdef NTSIG
  12677.                     ckThreadEnd(threadinfo);
  12678. #endif /* NTSIG */
  12679.                     return;
  12680.                 }
  12681.                 if (c0 < 0)
  12682.                   break;
  12683.         /* Second byte from net */
  12684.                 c1 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12685.                 if (cancelfile) {
  12686.                     failftprecv2(threadinfo);
  12687. #ifdef NTSIG
  12688.                     ckThreadEnd(threadinfo);
  12689. #endif /* NTSIG */
  12690.                     return;
  12691.                 }
  12692.                 if (c1 < 0)
  12693.                   break;
  12694. #ifdef COMMENT
  12695.         /* K95: Check whether we need this */
  12696.         if (fileorder > 0)    /* Little Endian */
  12697.           bytswap(&c0,&c1);    /* swap bytes*/
  12698. #endif /* COMMENT */
  12699.  
  12700. #ifdef OS2
  12701.                 if ( out2screen &&            /* we're translating to UCS-2 */ 
  12702.                      !k95stdout && !inserver) /* for the real screen... */     
  12703.                 {
  12704.                     union {
  12705.                         USHORT ucs2;
  12706.                         UCHAR  bytes[2];
  12707.                     } output;
  12708.  
  12709.                     output.bytes[0] = c1;
  12710.                     output.bytes[1] = c0;
  12711.  
  12712.                     VscrnWrtUCS2StrAtt(VCMD,
  12713.                                        &output.ucs2,
  12714.                                        1,
  12715.                                        wherey[VCMD],
  12716.                                        wherex[VCMD],
  12717.                                        &colorcmd
  12718.                                        );
  12719.  
  12720.                 } else 
  12721. #endif /* OS2 */
  12722.                 {
  12723.                     if ((x = xpnbyte(c0,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12724.                     if ((x = xpnbyte(c1,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12725.                 }
  12726.             }
  12727.         } else {
  12728. #endif /* NOCSETS */
  12729.             while (1) {
  12730.                 c = secure_getc(ftprecv.din,0);
  12731.                 if (cancelfile
  12732. #ifdef FTP_TIMEOUT
  12733.             || ftp_timed_out
  12734. #endif    /* FTP_TIMEOUT */
  12735.             ) {
  12736.                     failftprecv2(threadinfo);
  12737. #ifdef NTSIG
  12738.                     ckThreadEnd(threadinfo);
  12739. #endif /* NTSIG */
  12740.                     return;
  12741.                 }
  12742.                 if (c < 0 || c == EOF)
  12743.                   break;
  12744. #ifdef UNIX
  12745.         /* Record format conversion for Unix */
  12746.         /* SKIP THIS FOR WINDOWS! */
  12747.                 if (c == '\n')
  12748.                   bare_lfs++;
  12749.                 while (c == '\r') {
  12750.                     bytes++;
  12751.                     if ((c = secure_getc(ftprecv.din,0)) != '\n' ||
  12752.             ftprecv.tcrflag) {
  12753.                         if (cancelfile) {
  12754.                             failftprecv2(threadinfo);
  12755. #ifdef NTSIG
  12756.                             ckThreadEnd(threadinfo);
  12757. #endif /* NTSIG */
  12758.                             return;
  12759.                         }
  12760.                         if (c < 0 || c == EOF)
  12761.                           goto break2;
  12762.                         if (c == '\0') {
  12763.                             bytes++;
  12764.                             goto contin2;
  12765.                         }
  12766.                     }
  12767.                 }
  12768.                 if (c < 0)
  12769.                   break;
  12770. #endif /* UNX */
  12771.  
  12772.                 if (out2screen && !ftprecv.pipename)
  12773. #ifdef printf
  12774.                   printf("%c",(char)c);
  12775. #else
  12776.                   putchar((char)c);
  12777. #endif /* printf */
  12778.                 else
  12779.                   if ((d = zmchout(c)) < 0)
  12780.                     break;
  12781.                 bytes++;
  12782.                 ffc++;
  12783.               contin2:
  12784.                 ;
  12785.             }
  12786.           break2:
  12787.             if (bare_lfs && (!dpyactive || ftp_deb)) {
  12788.                 printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  12789.                        bare_lfs);
  12790.                 printf("File might not have transferred correctly.\n");
  12791.             }
  12792.             if (ftprecv.din == -1) {
  12793.                 bytes = (CK_OFF_T)-1;
  12794.             }
  12795.             if (c == -2)
  12796.               bytes = (CK_OFF_T)-1;
  12797.             break;
  12798. #ifndef NOCSETS
  12799.         }
  12800. #endif /* NOCSETS */
  12801.     }
  12802.     if (ftprecv.pipename || !out2screen) {
  12803.     zclose(ZOFILE);            /* Close the file */
  12804.     debug(F111,"doftprecv2 zclose ftpcode",ftprecv.local,ftpcode);
  12805.     if (ftpcode < 0) {        /* If download failed */
  12806.         int x = 0;
  12807.         switch (keep) {        /* which is... */
  12808.           case SET_AUTO:        /* AUTO */
  12809.         if (curtype == FTT_ASC) /* Delete file if TYPE A. */
  12810.           x = 1;
  12811.         break;
  12812.           case SET_OFF:        /* DISCARD */
  12813.         x = 1;            /* Delete file, period. */
  12814.         break;
  12815.           default:            /* KEEP */
  12816.         break;
  12817.         }
  12818.         if (x) {
  12819.         x = zdelet(ftprecv.local);
  12820.         debug(F111,"ftp get delete incomplete",ftprecv.local,x);
  12821.         }
  12822.     }
  12823.     }
  12824.     signal(SIGINT, ftprecv.oldintr);
  12825. #ifdef SIGPIPE
  12826.     if (ftprecv.oldintp)
  12827.       signal(SIGPIPE, ftprecv.oldintp);
  12828. #endif /* SIGPIPE */
  12829.     stop = gmstimer();
  12830. #ifdef GFTIMER
  12831.     fpfsecs = gftimer();
  12832. #endif /* GFTIMER */
  12833.     tfc += ffc;
  12834.  
  12835. #ifdef TCPIPLIB
  12836.     socket_close(ftprecv.din);
  12837. #else /* TCPIPLIB */
  12838. #ifdef USE_SHUTDOWN
  12839.     shutdown(ftprecv.din, 1+1);
  12840. #endif /* USE_SHUTDOWN */
  12841.     close(ftprecv.din);
  12842. #endif /* TCPIPLIB */
  12843.     ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  12844.     ftprecvret = ((ftpcode < 0 || ftprecv.reply == REPLY_TRANSIENT || 
  12845.                    ftprecv.reply == REPLY_ERROR) ? -1 : 0);
  12846. #ifdef NTSIG
  12847.      ckThreadEnd(threadinfo);
  12848. #endif /* NTSIG */
  12849. }
  12850.  
  12851. static int
  12852. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  12853.             xlate, fcs, rcs)
  12854.     char *cmd, *local, *remote, *lmode, *pipename;
  12855.     int printnames, recover, xlate, fcs, rcs;
  12856. {
  12857. #ifdef NT
  12858.     struct _stat stbuf;
  12859. #else /* NT */
  12860.     struct stat stbuf;
  12861. #endif /* NT */
  12862.  
  12863. #ifdef DEBUG
  12864.     if (deblog) {
  12865.         debug(F111,"ftp recvrequest cmd",cmd,recover);
  12866.         debug(F110,"ftp recvrequest local ",local,0);
  12867.         debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  12868.         debug(F110,"ftp recvrequest pipename ",pipename,0);
  12869.         debug(F101,"ftp recvrequest xlate","",xlate);
  12870.         debug(F101,"ftp recvrequest fcs","",fcs);
  12871.         debug(F101,"ftp recvrequest rcs","",rcs);
  12872.     }
  12873. #endif /* DEBUG */
  12874.  
  12875.     ftprecv.localsize = (CK_OFF_T)0;
  12876.  
  12877.     if (remfile) {                      /* See remcfm(), remtxt() */
  12878.         if (rempipe) {
  12879.             pipename = remdest;
  12880.         } else {
  12881.             local = remdest;
  12882.             if (remappd) lmode = "ab";
  12883.         }
  12884.     }
  12885.     out2screen = 0;
  12886.     if (!cmd) cmd = "";                 /* Core dump prevention */
  12887.     if (!remote) remote = "";
  12888.     if (!lmode) lmode = "";
  12889.  
  12890.     if (pipename) {                     /* No recovery for pipes. */
  12891.         recover = 0;
  12892.         if (!local)
  12893.           local = pipename;
  12894.     } else {
  12895.         if (!local)                     /* Output to screen? */
  12896.           local = "-";
  12897.         out2screen = !strcmp(local,"-");
  12898.     }
  12899.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  12900.  
  12901. #ifdef OS2
  12902.     if ( ftp_xla && out2screen && !k95stdout && !inserver )
  12903.         fcs = FC_UCS2;
  12904. #endif /* OS2 */
  12905.  
  12906.     if (out2screen)                     /* No recovery to screen */
  12907.       recover = 0;
  12908.     if (!ftp_typ)                       /* No recovery in text mode */
  12909.       recover = 0;
  12910.     ftprecv.is_retr = (strcmp(cmd, "RETR") == 0);
  12911.  
  12912.     if (!ftprecv.is_retr)               /* No recovery except for RETRieve */
  12913.       recover = 0;
  12914.  
  12915. #ifdef COMMENT
  12916.     if (!out2screen && !pipename && ftprecv.is_retr) { /* To real file */
  12917.         if (recursive && ckstrchr(local,'/')) {
  12918.         
  12919.         }
  12920.     }
  12921. #endif /* COMMENT */
  12922.  
  12923.     ftprecv.localsize = (CK_OFF_T)0;    /* Local file size */
  12924.     rs_len = (CK_OFF_T)0;        /* Recovery point */
  12925.  
  12926.     debug(F101,"ftp recvrequest recover","",recover);
  12927.     if (recover) {                      /* Recovering... */
  12928.         if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
  12929.         debug(F101,"ftp recvrequest recover stat failed","",errno);
  12930.             recover = 0;                /* So cancel recovery */
  12931.         } else {                        /* Have local file info */
  12932.             ftprecv.localsize = stbuf.st_size;  /* Get size */
  12933.         /* Remote file smaller than local */
  12934.             if (fsize < ftprecv.localsize) {
  12935.         debug(F101,"ftp recvrequest recover remote smaller","",fsize);
  12936.                 recover = 0;            /* Recovery can't work */
  12937.             } else if (fsize == ftprecv.localsize) { /* Sizes are equal */
  12938.                 debug(F111,"ftp recvrequest recover equal size",
  12939.               remote,ftprecv.localsize);
  12940.                 return(1);
  12941.             }
  12942. #ifdef COMMENT
  12943. /*
  12944.   The problem here is that the original partial file never got its date
  12945.   set, either because FTP DATES was OFF, or because the partial file was
  12946.   downloaded by some other program that doesn't set local file dates, or
  12947.   because Kermit only sets the file's date when the download was complete
  12948.   and successful.  In all these cases, the local file has a later time
  12949.   than the remote.
  12950. */
  12951.             if (recover) {              /* Remote is bigger */
  12952.                 x = chkmodtime(local,remote,0); /* Check file dates */
  12953.                 debug(F111,"ftp recvrequest chkmodtime",remote,x);
  12954.                 if (x != 1)        /* Dates must be equal! */
  12955.                   recover = 0;          /* If not, get whole file */
  12956.             }
  12957. #endif /* COMMENT */
  12958.         }
  12959.         debug(F111,"ftp recvrequest recover",remote,recover);
  12960.     }
  12961.  
  12962. #ifdef FTP_PROXY
  12963.     if (proxy && ftprecv.is_retr)
  12964.       return(proxtrans(cmd, local ? local : remote, remote));
  12965. #endif /* FTP_PROXY */
  12966.  
  12967.     ftprecv.tcrflag = (feol != CR) && ftprecv.is_retr;
  12968.  
  12969.     ftprecv.reply = 0;
  12970.     ftprecv.fcs = fcs;
  12971.     ftprecv.rcs = rcs;
  12972.     ftprecv.recover = recover;
  12973.     ftprecv.xlate = xlate;
  12974.     ftprecv.cmd = cmd;
  12975.     ftprecv.local = local;
  12976.     ftprecv.remote = remote;
  12977.     ftprecv.lmode = lmode;
  12978.     ftprecv.pipename = pipename;
  12979.     ftprecv.oldintp = NULL;
  12980.     ftpcode = 0;
  12981.  
  12982.     havesigint = 0;
  12983.     ftprecv.oldintr = signal(SIGINT, cancelrecv);
  12984.     if (cc_execute(ckjaddr(recvcancel), doftprecv, failftprecv) < 0)
  12985.       return -1;
  12986.  
  12987. #ifdef FTP_TIMEOUT
  12988.     debug(F111,"ftp recvrequest ftprecvret",remote,ftprecvret);
  12989.     debug(F111,"ftp recvrequest ftp_timed_out",remote,ftp_timed_out);
  12990.     if (ftp_timed_out)
  12991.       ftprecvret = -1;
  12992. #endif    /* FTP_TIMEOUT */
  12993.  
  12994.     if (ftprecvret < 0)
  12995.       return -1;
  12996.  
  12997.     if (cc_execute(ckjaddr(recvcancel), doftprecv2, failftprecv2) < 0)
  12998.       return -1;
  12999.     return ftprecvret;
  13000. }
  13001.  
  13002. /*
  13003.  * Need to start a listen on the data channel before we send the command,
  13004.  * otherwise the server's connect may fail.
  13005.  */
  13006. static int
  13007. initconn() {
  13008.     register char *p, *a;
  13009.     int result, tmpno = 0;
  13010.     int on = 1;
  13011.     GSOCKNAME_T len;
  13012.  
  13013. #ifndef NO_PASSIVE_MODE
  13014.     int a1,a2,a3,a4,p1,p2;
  13015.  
  13016.     if (passivemode) {
  13017.         data = socket(AF_INET, SOCK_STREAM, 0);
  13018.         globaldin = data;
  13019.         if (data < 0) {
  13020.             perror("ftp: socket");
  13021.             return(-1);
  13022.         }
  13023.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  13024.             printf("Passive mode refused\n");
  13025.             passivemode = 0;
  13026.             return(initconn());
  13027.         }
  13028. /*
  13029.   Now we have a string of comma-separated one-byte unsigned integer values,
  13030.   The first four are the an IP address.  The fifth is the MSB of the port
  13031.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  13032. */
  13033.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  13034.             printf("Passive mode address scan failure\n");
  13035.             return(-1);
  13036.         };
  13037. #ifndef NOHTTP
  13038.         if (tcp_http_proxy) {
  13039. #ifdef OS2
  13040.             char * agent = "Kermit 95"; /* Default user agent */
  13041. #else
  13042.             char * agent = "C-Kermit";
  13043. #endif /* OS2 */
  13044.             register struct hostent *hp = 0;
  13045.             struct servent *destsp;
  13046.             char host[512], *p, *q;
  13047. #ifdef IP_TOS
  13048. #ifdef IPTOS_THROUGHPUT
  13049.             int tos;
  13050. #endif /* IPTOS_THROUGHPUT */
  13051. #endif /* IP_TOS */
  13052.             int s;
  13053. #ifdef DEBUG
  13054.             extern int debtim;
  13055.             int xdebtim;
  13056.             xdebtim = debtim;
  13057.             debtim = 1;
  13058. #endif /* DEBUG */
  13059.  
  13060.             ckmakxmsg(proxyhost,HTTPCPYL,ckuitoa(a1),".",ckuitoa(a2),
  13061.                       ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  13062.                       NULL,NULL,NULL
  13063.                       );
  13064.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  13065.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  13066.               *q = *p;
  13067.             *q = '\0';
  13068.  
  13069.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  13070.             if (hisctladdr.sin_addr.s_addr != (unsigned long) -1) {
  13071.                 debug(F110,"initconn A",host,0);
  13072.                 hisctladdr.sin_family = AF_INET;
  13073.             } else {
  13074.                 debug(F110,"initconn B",host,0);
  13075.                 hp = gethostbyname(host);
  13076. #ifdef HADDRLIST
  13077.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  13078. #endif /* HADDRLIST */
  13079.                 if (hp == NULL) {
  13080.                     fprintf(stderr, "ftp: %s: Unknown host\n", host);
  13081.                     ftpcode = -1;
  13082. #ifdef DEBUG
  13083.                     debtim = xdebtim;
  13084. #endif /* DEBUG */
  13085.                     return(0);
  13086.                 }
  13087.                 hisctladdr.sin_family = hp->h_addrtype;
  13088. #ifdef HADDRLIST
  13089.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  13090.                        sizeof(hisctladdr.sin_addr));
  13091. #else /* HADDRLIST */
  13092.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  13093.                        sizeof(hisctladdr.sin_addr));
  13094. #endif /* HADDRLIST */
  13095.             }
  13096.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  13097.             debug(F101,"initconn socket","",data);
  13098.             if (data < 0) {
  13099.                 perror("ftp: socket");
  13100.                 ftpcode = -1;
  13101. #ifdef DEBUG
  13102.                 debtim = xdebtim;
  13103. #endif /* DEBUG */
  13104.                 return(0);
  13105.             }
  13106.             if (*p == ':')
  13107.               p++;
  13108.             else
  13109.               p = "http";
  13110.  
  13111.             destsp = getservbyname(p,"tcp");
  13112.             if (destsp)
  13113.               hisctladdr.sin_port = destsp->s_port;
  13114.             else if (p)
  13115.               hisctladdr.sin_port = htons(atoi(p));
  13116.             else
  13117.               hisctladdr.sin_port = htons(80);
  13118.             errno = 0;
  13119. #ifdef HADDRLIST
  13120.             debug(F100,"initconn HADDRLIST","",0);
  13121.             while
  13122. #else
  13123.             debug(F100,"initconn no HADDRLIST","",0);
  13124.             if
  13125. #endif /* HADDRLIST */
  13126.               (connect(data, (struct sockaddr *)&hisctladdr,
  13127.                        sizeof (hisctladdr)) < 0) {
  13128.                   debug(F101,"initconn connect failed","",errno);
  13129. #ifdef HADDRLIST
  13130.                   if (hp && hp->h_addr_list[1]) {
  13131.                       int oerrno = errno;
  13132.  
  13133.                       fprintf(stderr,
  13134.                               "ftp: connect to address %s: ",
  13135.                               inet_ntoa(hisctladdr.sin_addr)
  13136.                               );
  13137.                       errno = oerrno;
  13138.                       perror((char *)0);
  13139.                       hp->h_addr_list++;
  13140.                       memcpy((char *)&hisctladdr.sin_addr,
  13141.                              hp->h_addr_list[0],
  13142.                              sizeof(hisctladdr.sin_addr));
  13143.                       fprintf(stdout, "Trying %s...\n",
  13144.                               inet_ntoa(hisctladdr.sin_addr));
  13145. #ifdef TCPIPLIB
  13146.                       socket_close(data);
  13147. #else /* TCPIPLIB */
  13148.                       close(data);
  13149. #endif /* TCPIPLIB */
  13150.                       data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  13151.                       if (data < 0) {
  13152.                           perror("ftp: socket");
  13153.                           ftpcode = -1;
  13154. #ifdef DEBUG
  13155.                           debtim = xdebtim;
  13156. #endif /* DEBUG */
  13157.                           return(0);
  13158.                       }
  13159.                       continue;
  13160.                   }
  13161. #endif /* HADDRLIST */
  13162.                   perror("ftp: connect");
  13163.                   ftpcode = -1;
  13164.                   goto bad;
  13165.               }
  13166.             if (http_connect(data,
  13167.                              tcp_http_proxy_agent ?
  13168.                    tcp_http_proxy_agent :
  13169.                      agent,
  13170.                  NULL,
  13171.                              tcp_http_proxy_user,
  13172.                              tcp_http_proxy_pwd,
  13173.                              0,
  13174.                              proxyhost
  13175.                              ) < 0) {
  13176. #ifdef TCPIPLIB
  13177.                 socket_close(data);
  13178. #else /* TCPIPLIB */
  13179.                 close(data);
  13180. #endif /* TCPIPLIB */
  13181.                 perror("ftp: connect");
  13182.                 ftpcode = -1;
  13183.                 goto bad;
  13184.             }
  13185.         } else
  13186. #endif /* NOHTTP */
  13187.         {
  13188.             data_addr.sin_family = AF_INET;
  13189.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  13190.             data_addr.sin_port = htons((p1<<8)|p2);
  13191.  
  13192.             if (connect(data,
  13193.                         (struct sockaddr *)&data_addr,
  13194.                         sizeof(data_addr)) < 0
  13195.                 ) {
  13196.                 perror("ftp: connect");
  13197.                 return(-1);
  13198.             }
  13199.         }
  13200.         debug(F100,"initconn connect ok","",0);
  13201. #ifdef IP_TOS
  13202. #ifdef IPTOS_THROUGHPUT
  13203.         on = IPTOS_THROUGHPUT;
  13204.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  13205.           perror("ftp: setsockopt TOS (ignored)");
  13206. #endif /* IPTOS_THROUGHPUT */
  13207. #endif /* IP_TOS */
  13208.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  13209.         return(0);
  13210.     }
  13211. #endif /* NO_PASSIVE_MODE */
  13212.  
  13213.   noport:
  13214.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  13215.     if (sendport)
  13216.       data_addr.sin_port = 0;   /* let system pick one */
  13217.     if (data != -1) {
  13218. #ifdef TCPIPLIB
  13219.         socket_close(data);
  13220. #else /* TCPIPLIB */
  13221. #ifdef USE_SHUTDOWN
  13222.         shutdown(data, 1+1);
  13223. #endif /* USE_SHUTDOWN */
  13224.         close(data);
  13225. #endif /* TCPIPLIB */
  13226.     }
  13227.     data = socket(AF_INET, SOCK_STREAM, 0);
  13228.     globaldin = data;
  13229.     if (data < 0) {
  13230.         perror("ftp: socket");
  13231.         if (tmpno)
  13232.           sendport = 1;
  13233.         return(-1);
  13234.     }
  13235.     if (!sendport) {
  13236.         if (setsockopt(data,
  13237.                        SOL_SOCKET,
  13238.                        SO_REUSEADDR,
  13239.                        (char *)&on,
  13240.                        sizeof (on)
  13241.                        ) < 0
  13242.             ) {
  13243.             perror("ftp: setsockopt (reuse address)");
  13244.             goto bad;
  13245.         }
  13246.     }
  13247.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  13248.         perror("ftp: bind");
  13249.         goto bad;
  13250.     }
  13251.     len = sizeof (data_addr);
  13252.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  13253.         perror("ftp: getsockname");
  13254.         goto bad;
  13255.     }
  13256.     if (listen(data, 1) < 0) {
  13257.         perror("ftp: listen");
  13258.         goto bad;
  13259.     }
  13260.     if (sendport) {
  13261.         a = (char *)&data_addr.sin_addr;
  13262.         p = (char *)&data_addr.sin_port;
  13263.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  13264.                   UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  13265.                   UC(p[0]),",", UC(p[1]));
  13266.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  13267.         if (result == REPLY_ERROR && sendport) {
  13268.             sendport = 0;
  13269.             tmpno = 1;
  13270.             goto noport;
  13271.         }
  13272.         return(result != REPLY_COMPLETE);
  13273.     }
  13274.     if (tmpno)
  13275.       sendport = 1;
  13276. #ifdef IP_TOS
  13277. #ifdef IPTOS_THROUGHPUT
  13278.     on = IPTOS_THROUGHPUT;
  13279.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  13280.       perror("ftp: setsockopt TOS (ignored)");
  13281. #endif
  13282. #endif
  13283.     return(0);
  13284.   bad:
  13285. #ifdef TCPIPLIB
  13286.     socket_close(data);
  13287. #else /* TCPIPLIB */
  13288. #ifdef USE_SHUTDOWN
  13289.     shutdown(data, 1+1);
  13290. #endif /* USE_SHUTDOWN */
  13291.     close(data);
  13292. #endif /* TCPIPLIB */
  13293.     data = -1;
  13294.     globaldin = data;
  13295.     if (tmpno)
  13296.       sendport = 1;
  13297.     return(-1);
  13298. }
  13299.  
  13300. #ifdef CK_SSL
  13301. static int
  13302. ssl_dataconn() {
  13303.     if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
  13304.         SSL_free(ssl_ftp_data_con);
  13305.         ssl_ftp_data_con=NULL;
  13306.     }
  13307.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  13308.  
  13309.     SSL_set_fd(ssl_ftp_data_con,data);
  13310.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  13311.  
  13312.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  13313.  
  13314.     if (ssl_debug_flag) {
  13315.         fprintf(stderr,"=>START SSL connect on DATA\n");
  13316.         fflush(stderr);
  13317.     }
  13318.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  13319.         static char errbuf[1024];
  13320.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  13321.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  13322.         fprintf(stderr,"%s\n", errbuf);
  13323.         fflush(stderr);
  13324. #ifdef TCPIPLIB
  13325.         socket_close(data);
  13326. #else /* TCPIPLIB */
  13327. #ifdef USE_SHUTDOWN
  13328.         shutdown(data, 1+1);
  13329. #endif /* USE_SHUTDOWN */
  13330.         close(data);
  13331. #endif /* TCPIPLIB */
  13332.         data = -1;
  13333.         globaldin = data;
  13334.         return(-1);
  13335.     } else {
  13336.         ssl_ftp_data_active_flag=1;
  13337.  
  13338.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  13339.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  13340.  
  13341.             if (!subject) {
  13342.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  13343.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  13344.  
  13345.                     ssl_ftp_data_active_flag = 0;
  13346. #ifdef TCPIPLIB
  13347.                     socket_close(data);
  13348. #else /* TCPIPLIB */
  13349. #ifdef USE_SHUTDOWN
  13350.                     shutdown(data, 1+1);
  13351. #endif /* USE_SHUTDOWN */
  13352.                     close(data);
  13353. #endif /* TCPIPLIB */
  13354.                     data = -1;
  13355.                     globaldin = data;
  13356.                     return(-1);
  13357.                 } else {
  13358.                     if (!out2screen && displa && fdispla) {
  13359.                         ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
  13360.                         /* fdispla = XYFD_B; */
  13361.                     }
  13362.  
  13363.                     if (uq_ok(
  13364.           "Warning: Server didn't provide a certificate on data connection\n",
  13365.                                "Continue with file transfer? (Y/N)",
  13366.                               3,NULL,0) <= 0) {
  13367.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  13368.                         ssl_ftp_data_active_flag = 0;
  13369. #ifdef TCPIPLIB
  13370.                         socket_close(data);
  13371. #else /* TCPIPLIB */
  13372. #ifdef USE_SHUTDOWN
  13373.                         shutdown(data, 1+1);
  13374. #endif /* USE_SHUTDOWN */
  13375.                         close(data);
  13376. #endif /* TCPIPLIB */
  13377.                         data = -1;
  13378.                         globaldin = data;
  13379.                         return(-1);
  13380.                     }
  13381.                 }
  13382.             } else {
  13383.                 if (!out2screen && displa && fdispla == XYFD_C) {
  13384.                     ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
  13385.                     /* fdispla = XYFD_B; */
  13386.                 }
  13387.  
  13388.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_user_host)) {
  13389.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  13390.                     ssl_ftp_data_active_flag = 0;
  13391. #ifdef TCPIPLIB
  13392.                     socket_close(data);
  13393. #else /* TCPIPLIB */
  13394. #ifdef USE_SHUTDOWN
  13395.                     shutdown(data, 1+1);
  13396. #endif /* USE_SHUTDOWN */
  13397.                     close(data);
  13398. #endif /* TCPIPLIB */
  13399.                     data = -1;
  13400.                     globaldin = data;
  13401.                     return(-1);
  13402.                 }
  13403.             }
  13404.         }
  13405.         debug(F110,"dataconn","[SSL - OK]",0);
  13406. #ifdef COMMENT
  13407.         /* This messes up the full screen file transfer display */
  13408.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  13409. #endif /* COMMENT */
  13410.     }
  13411.     if (ssl_debug_flag) {
  13412.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  13413.         fflush(stderr);
  13414.     }
  13415.     return(data);
  13416. }
  13417. #endif /* CK_SSL */
  13418.  
  13419. static int
  13420. dataconn(lmode) char *lmode; {
  13421.     int s;
  13422. #ifdef IP_TOS
  13423.     int tos;
  13424. #endif /* IP_TOS */
  13425. #ifdef UCX50
  13426.     static u_int fromlen;
  13427. #else
  13428.     static SOCKOPT_T fromlen;
  13429. #endif /* UCX50 */
  13430.  
  13431.     fromlen = sizeof(hisdataaddr);
  13432.  
  13433. #ifndef NO_PASSIVE_MODE
  13434.     if (passivemode) {
  13435. #ifdef CK_SSL
  13436.         ssl_ftp_data_active_flag=0;
  13437.         if (ssl_ftp_active_flag &&
  13438.             (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  13439.           return(ssl_dataconn());
  13440. #endif /* CK_SSL */
  13441.         return(data);
  13442.     }
  13443. #endif /* NO_PASSIVE_MODE */
  13444.  
  13445.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  13446.     if (s < 0) {
  13447.         perror("ftp: accept");
  13448. #ifdef TCPIPLIB
  13449.         socket_close(data);
  13450. #else /* TCPIPLIB */
  13451. #ifdef USE_SHUTDOWN
  13452.         shutdown(data, 1+1);
  13453. #endif /* USE_SHUTDOWN */
  13454.         close(data);
  13455. #endif /* TCPIPLIB */
  13456.         data = -1;
  13457.         globaldin = data;
  13458.         return(-1);
  13459.     }
  13460. #ifdef TCPIPLIB
  13461.     socket_close(data);
  13462. #else /* TCPIPLIB */
  13463. #ifdef USE_SHUTDOWN
  13464.     shutdown(data, 1+1);
  13465. #endif /* USE_SHUTDOWN */
  13466.     close(data);
  13467. #endif /* TCPIPLIB */
  13468.     data = s;
  13469.     globaldin = data;
  13470. #ifdef IP_TOS
  13471. #ifdef IPTOS_THROUGHPUT
  13472.     tos = IPTOS_THROUGHPUT;
  13473.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  13474.       perror("ftp: setsockopt TOS (ignored)");
  13475. #endif /* IPTOS_THROUGHPUT */
  13476. #endif /* IP_TOS */
  13477.  
  13478. #ifdef CK_SSL
  13479.     ssl_ftp_data_active_flag=0;
  13480.     if (ssl_ftp_active_flag &&
  13481.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  13482.       return(ssl_dataconn());
  13483. #endif /* CK_SSL */
  13484.     return(data);
  13485. }
  13486.  
  13487. #ifdef FTP_PROXY
  13488. static sigtype
  13489. pscancel(sig) int sig; {
  13490.     cancelfile++;
  13491. }
  13492.  
  13493. static VOID
  13494. pswitch(flag) int flag; {
  13495.     extern int proxy;
  13496.     sig_t oldintr;
  13497.     static struct comvars {
  13498.         int connect;
  13499.         char name[MAXHOSTNAMELEN];
  13500.         struct sockaddr_in mctl;
  13501.         struct sockaddr_in hctl;
  13502.         FILE *in;
  13503.         FILE *out;
  13504.         int tpe;
  13505.         int curtpe;
  13506.         int cpnd;
  13507.         int sunqe;
  13508.         int runqe;
  13509.         int mcse;
  13510.         int ntflg;
  13511.         char nti[17];
  13512.         char nto[17];
  13513.         int mapflg;
  13514.         char mi[CKMAXPATH];
  13515.         char mo[CKMAXPATH];
  13516.         char *authtype;
  13517.         int clvl;
  13518.         int dlvl;
  13519. #ifdef FTP_KRB4
  13520.         des_cblock session;
  13521.         des_key_schedule ftp_sched;
  13522. #endif /* FTP_KRB4 */
  13523. #ifdef FTP_GSSAPI
  13524.         gss_ctx_id_t gcontext;
  13525. #endif /* GSSAPI */
  13526.     } proxstruct, tmpstruct;
  13527.     struct comvars *ip, *op;
  13528.  
  13529.     cancelfile = 0;
  13530.     oldintr = signal(SIGINT, pscancel);
  13531.     if (flag) {
  13532.         if (proxy)
  13533.           return;
  13534.         ip = &tmpstruct;
  13535.         op = &proxstruct;
  13536.         proxy++;
  13537.     } else {
  13538.         if (!proxy)
  13539.           return;
  13540.         ip = &proxstruct;
  13541.         op = &tmpstruct;
  13542.         proxy = 0;
  13543.     }
  13544.     ip->connect = connected;
  13545.     connected = op->connect;
  13546.     if (ftp_host) {
  13547.         strncpy(ip->name, ftp_host, MAXHOSTNAMELEN - 1);
  13548.         ip->name[MAXHOSTNAMELEN - 1] = '\0';
  13549.         ip->name[strlen(ip->name)] = '\0';
  13550.     } else
  13551.       ip->name[0] = 0;
  13552.     ftp_host = op->name;
  13553.     ip->hctl = hisctladdr;
  13554.     hisctladdr = op->hctl;
  13555.     ip->mctl = myctladdr;
  13556.     myctladdr = op->mctl;
  13557.     ip->in = csocket;
  13558.     csocket = op->in;
  13559.     ip->out = csocket;
  13560.     csocket = op->out;
  13561.     ip->tpe = ftp_typ;
  13562.     ftp_typ = op->tpe;
  13563.     ip->curtpe = curtype;
  13564.     curtype = op->curtpe;
  13565.     ip->cpnd = cpend;
  13566.     cpend = op->cpnd;
  13567.     ip->sunqe = ftp_usn;
  13568.     ftp_usn = op->sunqe;
  13569.     ip->mcse = mcase;
  13570.     mcase = op->mcse;
  13571.     ip->ntflg = ntflag;
  13572.     ntflag = op->ntflg;
  13573.     strncpy(ip->nti, ntin, 16);
  13574.     (ip->nti)[strlen(ip->nti)] = '\0';
  13575.     strcpy(ntin, op->nti);
  13576.     strncpy(ip->nto, ntout, 16);
  13577.     (ip->nto)[strlen(ip->nto)] = '\0';
  13578.     strcpy(ntout, op->nto);
  13579.     ip->mapflg = mapflag;
  13580.     mapflag = op->mapflg;
  13581.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  13582.     (ip->mi)[strlen(ip->mi)] = '\0';
  13583.     strcpy(mapin, op->mi);
  13584.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  13585.     (ip->mo)[strlen(ip->mo)] = '\0';
  13586.     strcpy(mapout, op->mo);
  13587.     ip->authtype = auth_type;
  13588.     auth_type = op->authtype;
  13589.     ip->clvl = ftp_cpl;
  13590.     ftp_cpl = op->clvl;
  13591.     ip->dlvl = ftp_dpl;
  13592.     ftp_dpl = op->dlvl;
  13593.     if (!ftp_cpl)
  13594.       ftp_cpl = FPL_CLR;
  13595.     if (!ftp_dpl)
  13596.       ftp_dpl = FPL_CLR;
  13597. #ifdef FTP_KRB4
  13598.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  13599.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  13600.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  13601.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  13602. #endif /* FTP_KRB4 */
  13603. #ifdef FTP_GSSAPI
  13604.     ip->gcontext = gcontext;
  13605.     gcontext = op->gcontext;
  13606. #endif /* GSSAPI */
  13607.     signal(SIGINT, oldintr);
  13608.     if (cancelfile) {
  13609.         cancelfile = 0;
  13610.         debug(F101,"pswitch cancelfile B","",cancelfile);
  13611.         (*oldintr)(SIGINT);
  13612.     }
  13613. }
  13614.  
  13615. static sigtype
  13616. cancelpt(sig) int sig; {
  13617.     printf("\n");
  13618.     fflush(stdout);
  13619.     ptabflg++;
  13620.     cancelfile = 0;
  13621. #ifndef OS2
  13622.     longjmp(ptcancel, 1);
  13623. #else
  13624.     PostCtrlCSem();
  13625. #endif /* OS2 */
  13626. }
  13627.  
  13628. void
  13629. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  13630.     sig_t oldintr;
  13631.     int secndflag = 0, prox_type, nfnd;
  13632.     char *cmd2;
  13633. #ifdef BSDSELECT
  13634.     fd_set mask;
  13635. #endif /* BSDSELECT */
  13636.     sigtype cancelpt();
  13637.  
  13638.     if (strcmp(cmd, "RETR"))
  13639.       cmd2 = "RETR";
  13640.     else
  13641.       cmd2 = unique ? "STOU" : "STOR";
  13642.     if ((prox_type = type) == 0) {
  13643.         if (servertype == SYS_UNIX && unix_proxy)
  13644.           prox_type = FTT_BIN;
  13645.         else
  13646.           prox_type = FTT_ASC;
  13647.     }
  13648.     if (curtype != prox_type)
  13649.       changetype(prox_type, 1);
  13650.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  13651.         printf("Proxy server does not support third party transfers.\n");
  13652.         return;
  13653.     }
  13654.     pswitch(0);
  13655.     if (!connected) {
  13656.         printf("No primary connection\n");
  13657.         pswitch(1);
  13658.         ftpcode = -1;
  13659.         return;
  13660.     }
  13661.     if (curtype != prox_type)
  13662.       changetype(prox_type, 1);
  13663.  
  13664.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  13665.         pswitch(1);
  13666.         return;
  13667.     }
  13668.  
  13669.     /* Replace with calls to cc_execute() */
  13670.     if (setjmp(ptcancel))
  13671.       goto cancel;
  13672.     oldintr = signal(SIGINT, cancelpt);
  13673.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  13674.         signal(SIGINT, oldintr);
  13675.         pswitch(1);
  13676.         return;
  13677.     }
  13678.     sleep(2000);
  13679.     pswitch(1);
  13680.     secndflag++;
  13681.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  13682.       goto cancel;
  13683.     ptflag++;
  13684.     getreply(0,-1,-1,ftp_vbm,0);
  13685.     pswitch(0);
  13686.     getreply(0,-1,-1,ftp_vbm,0);
  13687.     signal(SIGINT, oldintr);
  13688.     pswitch(1);
  13689.     ptflag = 0;
  13690.     return;
  13691.  
  13692.   cancel:
  13693.     signal(SIGINT, SIG_IGN);
  13694.     ptflag = 0;
  13695.     if (strcmp(cmd, "RETR") && !proxy)
  13696.       pswitch(1);
  13697.     else if (!strcmp(cmd, "RETR") && proxy)
  13698.       pswitch(0);
  13699.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  13700.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13701.             pswitch(0);
  13702.             if (cpend)
  13703.               cancel_remote(0);
  13704.         }
  13705.         pswitch(1);
  13706.         if (ptabflg)
  13707.           ftpcode = -1;
  13708.         signal(SIGINT, oldintr);
  13709.         return;
  13710.     }
  13711.     if (cpend)
  13712.       cancel_remote(0);
  13713.     pswitch(!proxy);
  13714.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  13715.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13716.             pswitch(0);
  13717.             if (cpend)
  13718.               cancel_remote(0);
  13719.             pswitch(1);
  13720.             if (ptabflg)
  13721.               ftpcode = -1;
  13722.             signal(SIGINT, oldintr);
  13723.             return;
  13724.         }
  13725.     }
  13726.     if (cpend)
  13727.       cancel_remote(0);
  13728.     pswitch(!proxy);
  13729.     if (cpend) {
  13730. #ifdef BSDSELECT
  13731.         FD_ZERO(&mask);
  13732.         FD_SET(csocket, &mask);
  13733.         if ((nfnd = empty(&mask, 10)) <= 0) {
  13734.             if (nfnd < 0) {
  13735.                 perror("cancel");
  13736.             }
  13737.             if (ptabflg)
  13738.               ftpcode = -1;
  13739.             lostpeer();
  13740.         }
  13741. #else /* BSDSELECT */
  13742. #ifdef IBMSELECT
  13743.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  13744.             if (nfnd < 0) {
  13745.                 perror("cancel");
  13746.             }
  13747.             if (ptabflg)
  13748.               ftpcode = -1;
  13749.             lostpeer();
  13750.         }
  13751. #endif /* IBMSELECT */
  13752. #endif /* BSDSELECT */
  13753.         getreply(0,-1,-1,ftp_vbm,0);
  13754.         getreply(0,-1,-1,ftp_vbm,0);
  13755.     }
  13756.     if (proxy)
  13757.       pswitch(0);
  13758.     pswitch(1);
  13759.     if (ptabflg)
  13760.       ftpcode = -1;
  13761.     signal(SIGINT, oldintr);
  13762. }
  13763. #endif /* FTP_PROXY */
  13764.  
  13765. #ifdef FTP_SECURITY
  13766. #ifdef FTP_GSSAPI
  13767.  
  13768. #ifdef COMMENT
  13769. /* ck_gss_mech_krb5 is not declared anywhere */
  13770. struct {
  13771.     CONST gss_OID_desc * CONST * mech_type;
  13772.     char *service_name;
  13773. } gss_trials[] = {
  13774.     { &ck_gss_mech_krb5, "ftp" },
  13775.     { &ck_gss_mech_krb5, "host" },
  13776. };
  13777. #else
  13778. /* This matches what is declared above */
  13779. struct {
  13780.     CONST gss_OID_desc * CONST * mech_type;
  13781.     char *service_name;
  13782. } gss_trials[] = {
  13783.     { &gss_mech_krb5, "ftp" },
  13784.     { &gss_mech_krb5, "host" },
  13785. };
  13786. #endif    /* COMMENT */
  13787.  
  13788.  
  13789. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  13790. #endif /* FTP_GSSAPI */
  13791.  
  13792. static int
  13793. ftp_auth() {
  13794.     extern int setsafe();
  13795.     int j = 0, n;
  13796. #ifdef FTP_KRB4
  13797.     char *service, inst[INST_SZ];
  13798.     ULONG cksum;
  13799.     ULONG checksum = (ULONG) getpid();
  13800.     CHAR out_buf[FTP_BUFSIZ];
  13801.     int i;
  13802. #else /* FTP_KRB4 */
  13803. #ifdef FTP_GSSAPI
  13804.     CHAR out_buf[FTP_BUFSIZ];
  13805.     int i;
  13806. #endif /* FTP_GSSAPI */
  13807. #endif /* FTP_KRB4 */
  13808.  
  13809.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  13810.         return(0);
  13811.  
  13812.     if (auth_type)
  13813.       return(1);                        /* auth already succeeded */
  13814.  
  13815.     /* Try each auth type as specified by the end user */
  13816.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  13817. #ifdef FTP_GSSAPI
  13818.         if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  13819.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  13820.             if (n == REPLY_CONTINUE) {
  13821.                 OM_uint32 maj_stat, min_stat;
  13822.                 gss_name_t target_name;
  13823.                 gss_buffer_desc send_tok, recv_tok, *token_ptr;
  13824.                 char stbuf[FTP_BUFSIZ];
  13825.                 int comcode, trial;
  13826.                 struct gss_channel_bindings_struct chan;
  13827.                 char * realm = NULL;
  13828.                 char tgt[256];
  13829.  
  13830.                 chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  13831.                 chan.initiator_address.length = 4;
  13832.                 chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  13833.                 chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  13834.                 chan.acceptor_address.length = 4;
  13835.                 chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  13836.                 chan.application_data.length = 0;
  13837.                 chan.application_data.value = 0;
  13838.  
  13839.                 if (!quiet)
  13840.                   printf("GSSAPI accepted as authentication type\n");
  13841.  
  13842.                 realm = ck_krb5_realmofhost(ftp_user_host);
  13843.                 if (realm) {
  13844.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  13845.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  13846.                     if ( krb5_autoget &&
  13847.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  13848.                             (ck_krb5_is_tgt_valid() > 0)) )
  13849.                         ck_krb5_autoget_TGT(realm);
  13850.                 }
  13851.  
  13852.                 /* Blob from gss-client */
  13853.                 for (trial = 0; trial < n_gss_trials; trial++) {
  13854.                     /* ftp@hostname first, the host@hostname */
  13855.                     /* the V5 GSSAPI binding canonicalizes this for us... */
  13856.                     ckmakmsg(stbuf,FTP_BUFSIZ,
  13857.                              gss_trials[trial].service_name,
  13858.                              "@",
  13859.                              ftp_user_host,
  13860.                              NULL
  13861.                              );
  13862.                     if (ftp_deb)
  13863.                       fprintf(stderr,
  13864.                               "Authenticating to <%s>...\n", stbuf);
  13865.                     send_tok.value = stbuf;
  13866.                     send_tok.length = strlen(stbuf);
  13867.                     maj_stat = gss_import_name(&min_stat, &send_tok,
  13868.                                                gss_nt_service_name,
  13869.                                                &target_name
  13870.                                                );
  13871.                     if (maj_stat != GSS_S_COMPLETE) {
  13872.                         user_gss_error(maj_stat, min_stat, "parsing name");
  13873.                         secure_error("name parsed <%s>\n", stbuf);
  13874.                         continue;
  13875.                     }
  13876.                     token_ptr = GSS_C_NO_BUFFER;
  13877.                     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  13878.  
  13879.                     do {
  13880.                         if (ftp_deb)
  13881.                           fprintf(stderr, "calling gss_init_sec_context\n");
  13882.                         maj_stat =
  13883.                           gss_init_sec_context(&min_stat,
  13884.                                                GSS_C_NO_CREDENTIAL,
  13885.                                                &gcontext,
  13886.                                                target_name,
  13887.                                                (gss_OID) *
  13888.                                                  gss_trials[trial].mech_type,
  13889.                                                GSS_C_MUTUAL_FLAG |
  13890.                                                GSS_C_REPLAY_FLAG |
  13891.                                                (ftp_cfw ?
  13892.                                                 GSS_C_DELEG_FLAG : 0),
  13893.                                                0,
  13894.                                                 /* channel bindings */
  13895.                                                 (krb5_d_no_addresses ?
  13896.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  13897.                                                   &chan),
  13898.                                                 token_ptr,
  13899.                                                NULL,    /* ignore mech type */
  13900.                                                &send_tok,
  13901.                                                NULL,    /* ignore ret_flags */
  13902.                                                NULL
  13903.                                                );       /* ignore time_rec */
  13904.  
  13905.                         if (maj_stat != GSS_S_COMPLETE &&
  13906.                             maj_stat != GSS_S_CONTINUE_NEEDED) {
  13907.                             if (trial == n_gss_trials-1)
  13908.                               user_gss_error(maj_stat,
  13909.                                              min_stat,
  13910.                                              "initializing context"
  13911.                                              );
  13912.                             gss_release_name(&min_stat, &target_name);
  13913.                             /* maybe we missed on the service name */
  13914.                             goto outer_loop;
  13915.                         }
  13916.                         if (send_tok.length != 0) {
  13917.                             int len;
  13918.                             reply_parse = "ADAT="; /* for ftpcmd() later */
  13919.                             len = FTP_BUFSIZ;
  13920.                             kerror =
  13921.                               radix_encode(send_tok.value,
  13922.                                            out_buf,
  13923.                                            send_tok.length,
  13924.                                            &len,
  13925.                                            RADIX_ENCODE
  13926.                                            );
  13927.                             if (kerror)  {
  13928.                                 fprintf(stderr,
  13929.                                         "Base 64 encoding failed: %s\n",
  13930.                                         radix_error(kerror)
  13931.                                         );
  13932.                                 goto gss_complete_loop;
  13933.                             }
  13934.                             comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  13935.                             if (comcode != REPLY_COMPLETE
  13936.                                 && comcode != REPLY_CONTINUE /* (335) */
  13937.                                 ) {
  13938.                                 if (trial == n_gss_trials-1) {
  13939.                                     fprintf(stderr, "GSSAPI ADAT failed\n");
  13940.                                     /* force out of loop */
  13941.                                     maj_stat = GSS_S_FAILURE;
  13942.                                 }
  13943.                                 /*
  13944.                                   Backoff to the v1 gssapi is still possible.
  13945.                                   Send a new AUTH command.  If that fails,
  13946.                                   terminate the loop.
  13947.                                 */
  13948.                                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  13949.                                     != REPLY_CONTINUE) {
  13950.                                     fprintf(stderr,
  13951.                                 "GSSAPI ADAT failed, AUTH restart failed\n");
  13952.                                     /* force out of loop */
  13953.                                     maj_stat = GSS_S_FAILURE;
  13954.                                 }
  13955.                                 goto outer_loop;
  13956.                             }
  13957.                             if (!reply_parse) {
  13958.                                 fprintf(stderr,
  13959.                               "No authentication data received from server\n");
  13960.                                 if (maj_stat == GSS_S_COMPLETE) {
  13961.                                     fprintf(stderr,
  13962.                                             "...but no more was needed\n");
  13963.                                     goto gss_complete_loop;
  13964.                                 } else {
  13965.                                     user_gss_error(maj_stat,
  13966.                                                    min_stat,
  13967.                                                    "no reply, huh?"
  13968.                                                    );
  13969.                                     goto gss_complete_loop;
  13970.                                 }
  13971.                             }
  13972.                             len = FTP_BUFSIZ;
  13973.                             kerror = radix_encode(reply_parse,out_buf,i,&len,
  13974.                                                   RADIX_DECODE);
  13975.                             if (kerror) {
  13976.                                 fprintf(stderr,
  13977.                                         "Base 64 decoding failed: %s\n",
  13978.                                         radix_error(kerror));
  13979.                                 goto gss_complete_loop;
  13980.                             }
  13981.  
  13982.                             /* everything worked */
  13983.                             token_ptr = &recv_tok;
  13984.                             recv_tok.value = out_buf;
  13985.                             recv_tok.length = len;
  13986.                             continue;
  13987.  
  13988.                             /* get out of loop clean */
  13989.                           gss_complete_loop:
  13990.                             trial = n_gss_trials-1;
  13991.                             gss_release_buffer(&min_stat, &send_tok);
  13992.                             gss_release_name(&min_stat, &target_name);
  13993.                             goto outer_loop;
  13994.                         }
  13995.                     } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  13996.  
  13997.                   outer_loop:
  13998.                     if (maj_stat == GSS_S_COMPLETE)
  13999.                       break;
  14000.                 }
  14001.                 if (maj_stat == GSS_S_COMPLETE) {
  14002.                     printf("GSSAPI authentication succeeded\n");
  14003.                     reply_parse = NULL;
  14004.                     auth_type = "GSSAPI";
  14005.                     return(1);
  14006.                 } else {
  14007.                     fprintf(stderr, "GSSAPI authentication failed\n");
  14008.                     reply_parse = NULL;
  14009.                 }
  14010.             } else {
  14011.                 if (ftp_deb)
  14012.                 fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  14013.                 if (ftpcode == 500 || ftpcode == 502)
  14014.                     return(0);
  14015.             }
  14016.         }
  14017. #endif /* FTP_GSSAPI */
  14018. #ifdef FTP_SRP
  14019.         if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  14020.             if (srp_ftp_auth(ftp_user_host,NULL,NULL))
  14021.               return(1);
  14022.             else if (ftpcode == 500 || ftpcode == 502)
  14023.               return(0);
  14024.         }
  14025. #endif /* FTP_SRP */
  14026. #ifdef FTP_KRB4
  14027.         if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  14028.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  14029.             if (n == REPLY_CONTINUE) {
  14030.                 char tgt[4*REALM_SZ+1];
  14031.                 int rc;
  14032.  
  14033.                 if (!quiet)
  14034.                   printf("KERBEROS_V4 accepted as authentication type\n");
  14035.                 ckstrncpy(inst, (char *) krb_get_phost(ftp_user_host),INST_SZ);
  14036.                 ckstrncpy(ftp_realm,
  14037.                           (char *)ck_krb4_realmofhost(ftp_user_host),
  14038.                           REALM_SZ
  14039.                           );
  14040.  
  14041.                 ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  14042.                 rc = ck_krb4_tkt_isvalid(tgt);
  14043.  
  14044.                 if (rc <= 0 && krb4_autoget)
  14045.                   ck_krb4_autoget_TGT(ftp_realm);
  14046.  
  14047.                 service = "ftp";
  14048.                 kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  14049.                 if (kerror == KDC_PR_UNKNOWN) {
  14050.                     service = "rcmd";
  14051.                     kerror = krb_mk_req(&ftp_tkt,
  14052.                                         service,
  14053.                                         inst,
  14054.                                         ftp_realm,
  14055.                                         checksum
  14056.                                         );
  14057.                 }
  14058.                 if (kerror)
  14059.                   fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  14060.                           krb_get_err_text(kerror));
  14061.                 if (!kerror) {
  14062.                     kerror = krb_get_cred(service, inst, ftp_realm,&ftp_cred);
  14063.                     if (kerror)
  14064.                       fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  14065.                               krb_get_err_text(kerror));
  14066.                 }
  14067.                 if (!kerror) {
  14068.                     int rc;
  14069.                     rc = des_key_sched(ftp_cred.session, ftp_sched);
  14070.                     if (rc == -1) {
  14071.                        printf("?Invalid DES key specified in credentials\r\n");
  14072.                        debug(F110,"ftp_auth",
  14073.                              "invalid DES Key specified in credentials",0);
  14074.                     } else if ( rc == -2 ) {
  14075.                         printf("?Weak DES key specified in credentials\r\n");
  14076.                         debug(F110,"ftp_auth",
  14077.                               "weak DES Key specified in credentials",0);
  14078.                     } else if ( rc != 0 ) {
  14079.                         printf("?DES Key Schedule not set by credentials\r\n");
  14080.                         debug(F110,"ftp_auth",
  14081.                               "DES Key Schedule not set by credentials",0);
  14082.                     }
  14083.                     reply_parse = "ADAT=";
  14084.                     i = FTP_BUFSIZ;
  14085.                     kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  14086.                                           &i, RADIX_ENCODE);
  14087.                     if (kerror) {
  14088.                         fprintf(stderr, "Base 64 encoding failed: %s\n",
  14089.                                 radix_error(kerror));
  14090.                         goto krb4_err;
  14091.                     }
  14092.                     if (i > FTP_BUFSIZ - 6)
  14093.                       printf("?ADAT data too long\n");
  14094.                     if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  14095.                         REPLY_COMPLETE) {
  14096.                         fprintf(stderr, "Kerberos V4 authentication failed\n");
  14097.                         goto krb4_err;
  14098.                     }
  14099.                     if (!reply_parse) {
  14100.                         fprintf(stderr,
  14101.                              "No authentication data received from server\n");
  14102.                         goto krb4_err;
  14103.                     }
  14104.                     i = sizeof(out_buf);
  14105.                     kerror =
  14106.                       radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  14107.                     if (kerror) {
  14108.                         fprintf(stderr, "Base 64 decoding failed: %s\n",
  14109.                                 radix_error(kerror));
  14110.                         goto krb4_err;
  14111.                     }
  14112.                     kerror = krb_rd_safe(out_buf, i,
  14113. #ifdef KRB524
  14114.                                          ftp_cred.session,
  14115. #else /* KRB524 */
  14116.                                          &ftp_cred.session,
  14117. #endif /* KRB524 */
  14118.                                          &hisctladdr,
  14119.                                          &myctladdr,
  14120.                                          &ftp_msg_data
  14121.                                          );
  14122.                     if (kerror) {
  14123.                         fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  14124.                                 krb_get_err_text(kerror));
  14125.                         goto krb4_err;
  14126.                     }
  14127.  
  14128.                     /* fetch the (modified) checksum */
  14129.                     memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  14130.                     if (ntohl(cksum) == checksum + 1) {
  14131.                         if (ftp_vbm)
  14132.                           printf("Kerberos V4 authentication succeeded\n");
  14133.                         reply_parse = NULL;
  14134.                         auth_type = "KERBEROS_V4";
  14135.                         return(1);
  14136.                     } else
  14137.                       fprintf(stderr,
  14138.                               "Kerberos V4 mutual authentication failed\n");
  14139.                   krb4_err:
  14140.                     reply_parse = NULL;
  14141.                 }
  14142.             } else {
  14143.                 if (ftp_deb)
  14144.           fprintf(stderr,
  14145.                       "KERBEROS_V4 rejected as an authentication type\n");
  14146.                 if (ftpcode == 500 || ftpcode == 502)
  14147.                     return(0);
  14148.             }
  14149.         }
  14150. #endif /* FTP_KRB4 */
  14151. #ifdef CK_SSL
  14152.         if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  14153. #ifdef FTPHOST
  14154.             if (!hostcmd) {
  14155.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  14156.                 hostcmd = 1;
  14157.             }
  14158. #endif /* FTPHOST */
  14159.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  14160.             if (n != REPLY_COMPLETE)
  14161.               n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  14162.             if (n == REPLY_COMPLETE) {
  14163.                 if (!quiet)
  14164.                   printf("TLS accepted as authentication type\n");
  14165.  
  14166.                 auth_type = "TLS";
  14167.                 ssl_auth();
  14168.                 if (ssl_ftp_active_flag ) {
  14169.                     ftp_dpl = FPL_CLR;
  14170.                     ftp_cpl = FPL_PRV;
  14171.                     return(1);
  14172.                 } else {
  14173.                     fprintf(stderr,"TLS authentication failed\n");
  14174.                     auth_type = NULL;
  14175. #ifdef TCPIPLIB
  14176.                     socket_close(csocket);
  14177. #else /* TCPIPLIB */
  14178. #ifdef USE_SHUTDOWN
  14179.                     shutdown(csocket, 1+1);
  14180. #endif /* USE_SHUTDOWN */
  14181.                     close(csocket);
  14182. #endif /* TCPIPLIB */
  14183.                     csocket = -1;
  14184.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  14185.                       return(0);
  14186.                 }
  14187.             } else {
  14188.                 if (ftp_deb)
  14189.           fprintf(stderr,"TLS rejected as an authentication type\n");
  14190.                 if (ftpcode == 500 || ftpcode == 502)
  14191.                     return(0);
  14192.             }
  14193.         }
  14194.         if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  14195. #ifdef FTPHOST
  14196.             if (!hostcmd) {
  14197.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  14198.                 hostcmd = 1;
  14199.             }
  14200. #endif /* FTPHOST */
  14201.             n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  14202.             if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  14203.                 if (!quiet)
  14204.                   printf("SSL accepted as authentication type\n");
  14205.                 auth_type = "SSL";
  14206.                 ssl_auth();
  14207.                 if (ssl_ftp_active_flag) {
  14208.                     ftp_dpl = FPL_PRV;
  14209.                     ftp_cpl = FPL_PRV;
  14210.                     setprotbuf(1<<20);
  14211.                     return(1);
  14212.                 } else {
  14213.                     fprintf(stderr,"SSL authentication failed\n");
  14214.                     auth_type = NULL;
  14215. #ifdef TCPIPLIB
  14216.                     socket_close(csocket);
  14217. #else /* TCPIPLIB */
  14218. #ifdef USE_SHUTDOWN
  14219.                     shutdown(csocket, 1+1);
  14220. #endif /* USE_SHUTDOWN */
  14221.                     close(csocket);
  14222. #endif /* TCPIPLIB */
  14223.                     csocket = -1;
  14224.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  14225.                       return(0);
  14226.                 }
  14227.         } else {
  14228.                 if (ftp_deb)
  14229.           fprintf(stderr, "SSL rejected as an authentication type\n");
  14230.                 if (ftpcode == 500 || ftpcode == 502)
  14231.           return(0);
  14232.             }
  14233.         }
  14234. #endif /* CK_SSL */
  14235.         /* Other auth types go here ... */
  14236.     } /* for (j;;) */
  14237.     return(0);
  14238. }
  14239. #endif /* FTP_SECURITY */
  14240.  
  14241. static int
  14242. #ifdef CK_ANSIC
  14243. setprotbuf(unsigned int size)
  14244. #else
  14245. setprotbuf(size) unsigned int size;
  14246. #endif /* CK_ANSIC */
  14247. /* setprotbuf */ {
  14248.     if (ucbuf)
  14249.       free(ucbuf);
  14250.     ucbuf = NULL;
  14251.     ucbufsiz = 0;
  14252.     actualbuf = size;
  14253.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  14254.         if (actualbuf)
  14255.           actualbuf /= 2;
  14256.         else
  14257.           return(0);
  14258.     }
  14259.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  14260.     debug(F101,"setprotbuf ucbufsiz","",ucbufsiz);
  14261.     if (ucbufsiz < 128) {
  14262.         printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  14263.     } else if (ucbufsiz < 0) {
  14264.         printf("ERROR: ucbuf allocation failure\n");
  14265.         return(-1);
  14266.     }
  14267.     maxbuf = actualbuf;
  14268.     return(1);
  14269. }
  14270.  
  14271. static int
  14272. #ifdef CK_ANSIC
  14273. setpbsz(unsigned int size)
  14274. #else
  14275. setpbsz(size) unsigned int size;
  14276. #endif /* CK_ANSIC */
  14277. /* setpbsz */ {
  14278.     if (!setprotbuf(size)) {
  14279.         perror("?Error while trying to malloc PROT buffer:");
  14280. #ifdef FTP_SRP
  14281.         srp_reset();
  14282. #endif /* FTP_SRP */
  14283.         ftpclose();
  14284.         return(-1);
  14285.     }
  14286.     reply_parse = "PBSZ=";
  14287.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  14288. #ifdef CK_SSL
  14289.              ssl_ftp_active_flag ? "0" :
  14290. #endif /* CK_SSL */
  14291.              ckuitoa(actualbuf),NULL,NULL);
  14292.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  14293.         if (connected) {
  14294.             printf("?Unable to negotiate PROT buffer size with FTP server\n");
  14295.             ftpclose();
  14296.         }
  14297.         return(-1);
  14298.     }
  14299.     if (reply_parse) {
  14300.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  14301.           maxbuf = actualbuf;
  14302.     } else
  14303.       maxbuf = actualbuf;
  14304.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  14305.     debug(F101,"setpbsz ucbufsiz","",ucbufsiz);    
  14306.     reply_parse = NULL;
  14307.     return(0);
  14308. }
  14309.  
  14310. static VOID
  14311. cancel_remote(din) int din; {
  14312.     CHAR buf[FTP_BUFSIZ];
  14313.     int x, nfnd;
  14314. #ifdef BSDSELECT
  14315.     fd_set mask;
  14316. #endif /* BSDSELECT */
  14317. #ifdef IBMSELECT
  14318.     int fds[2], fdcnt = 0;
  14319. #endif /* IBMSELECT */
  14320. #ifdef DEBUG
  14321.     extern int debtim;
  14322.     int xdebtim;
  14323.     xdebtim = debtim;
  14324.     debtim = 1;
  14325. #endif /* DEBUG */
  14326.     debug(F100,"ftp cancel_remote entry","",0);
  14327. #ifdef CK_SSL
  14328.     if (ssl_ftp_active_flag) {
  14329.         /*
  14330.          * Send Telnet IP, Telnet DM but do so inline and within the
  14331.          * TLS channel
  14332.          */
  14333.         int count, error;
  14334.  
  14335.         buf[0] = IAC;
  14336.         buf[1] = TN_IP;
  14337.         buf[2] = IAC;
  14338.         buf[3] = TN_DM;
  14339.         buf[4] = NUL;
  14340.  
  14341.         count = SSL_write(ssl_ftp_con, buf, 4);
  14342.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  14343.         error = SSL_get_error(ssl_ftp_con,count);
  14344.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  14345.         switch (error) {
  14346.           case SSL_ERROR_NONE:
  14347.             break;
  14348.           case SSL_ERROR_WANT_WRITE:
  14349.           case SSL_ERROR_WANT_READ:
  14350.           case SSL_ERROR_SYSCALL:
  14351. #ifdef NT
  14352.             {
  14353.                 int gle = GetLastError();
  14354.             }
  14355. #endif /* NT */
  14356.           case SSL_ERROR_WANT_X509_LOOKUP:
  14357.           case SSL_ERROR_SSL:
  14358.           case SSL_ERROR_ZERO_RETURN:
  14359.           default:
  14360.             lostpeer();
  14361.             return;
  14362.         }
  14363.     } else
  14364. #endif /* CK_SSL */
  14365.     {
  14366.         /*
  14367.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  14368.          * after urgent byte rather than before as is protocol now.
  14369.          */
  14370.         buf[0] = IAC;
  14371.         buf[1] = TN_IP;
  14372.         buf[2] = IAC;
  14373.         buf[3] = NUL;
  14374.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  14375.           perror("cancel");
  14376.         debug(F101,"ftp cancel_remote send 1","",x);
  14377.         buf[0] = TN_DM;
  14378.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  14379.         debug(F101,"ftp cancel_remote send 2","",x);
  14380.     }
  14381.     x = scommand("ABOR");
  14382.     debug(F101,"ftp cancel_remote scommand","",x);
  14383. #ifdef BSDSELECT
  14384.     FD_ZERO(&mask);
  14385.     FD_SET(csocket, &mask);
  14386.     if (din) {
  14387.         FD_SET(din, &mask);
  14388.     }
  14389.     nfnd = empty(&mask, 10);
  14390.     debug(F101,"ftp cancel_remote empty","",nfnd);
  14391.     if ((nfnd) <= 0) {
  14392.         if (nfnd < 0) {
  14393.             perror("cancel");
  14394.         }
  14395. #ifdef FTP_PROXY
  14396.         if (ptabflg)
  14397.           ftpcode = -1;
  14398. #endif /* FTP_PROXY */
  14399.         lostpeer();
  14400.     }
  14401.     debug(F110,"ftp cancel_remote","D",0);
  14402.     if (din && FD_ISSET(din, &mask)) {
  14403.         /* Security: No threat associated with this read. */
  14404.         /* But you can't simply read the TLS data stream  */
  14405. #ifdef CK_SSL
  14406.         if (ssl_ftp_data_active_flag) {
  14407.             int count, error;
  14408.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  14409.                     /* LOOP */ ;
  14410.         } else
  14411. #endif /* CK_SSL */
  14412.         {
  14413.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  14414.                 /* LOOP */ ;
  14415.         }
  14416.     }
  14417.     debug(F110,"ftp cancel_remote","E",0);
  14418. #else /* BSDSELECT */
  14419. #ifdef IBMSELECT
  14420.     fds[0] = csocket;
  14421.     fdcnt++;
  14422.     if (din) {
  14423.         fds[1] = din;
  14424.         fdcnt++;
  14425.     }
  14426.     nfnd = empty(fds, fdcnt, 10);
  14427.     debug(F101,"ftp cancel_remote empty","",nfnd);
  14428.     if ((nfnd) <= 0) {
  14429.         if (nfnd < 0) {
  14430.             perror("cancel");
  14431.         }
  14432. #ifdef FTP_PROXY
  14433.         if (ptabflg)
  14434.           ftpcode = -1;
  14435. #endif /* FTP_PROXY */
  14436.         lostpeer();
  14437.     }
  14438.     debug(F110,"ftp cancel_remote","D",0);
  14439.     if (din && select(&din, 1,0,0,1) ) {
  14440. #ifdef CK_SSL
  14441.         if (ssl_ftp_data_active_flag) {
  14442.             int count, error;
  14443.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  14444.                     /* LOOP */ ;
  14445.         } else
  14446. #endif /* CK_SSL */
  14447.         {
  14448.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  14449.                 /* LOOP */ ;
  14450.         }
  14451.     }
  14452.     debug(F110,"ftp cancel_remote","E",0);
  14453. #else /* IBMSELECT */
  14454.     Some form of select is required.
  14455. #endif /* IBMSELECT */
  14456. #endif /* BSDSELECT */
  14457.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  14458.         debug(F110,"ftp cancel_remote","F",0);
  14459.         /* 552 needed for NIC style cancel */
  14460.         getreply(0,-1,-1,ftp_vbm,0);
  14461.         debug(F110,"ftp cancel_remote","G",0);
  14462.     }
  14463.     debug(F110,"ftp cancel_remote","H",0);
  14464.     getreply(0,-1,-1,ftp_vbm,0);
  14465.     debug(F110,"ftp cancel_remote","I",0);
  14466. #ifdef DEBUG
  14467.     debtim = xdebtim;
  14468. #endif /* DEBUG */
  14469. }
  14470.  
  14471. static int
  14472. fts_dpl(x) int x; {
  14473.     if (!auth_type
  14474. #ifdef OS2
  14475.          || !ck_crypt_is_installed()
  14476. #endif /* OS2 */
  14477.          ) {
  14478.         switch ( x ) {
  14479.           case FPL_PRV:
  14480.             printf("?Cannot set protection level to PRIVATE\n");
  14481.             return(0);
  14482.           case FPL_SAF:
  14483.             printf("?Cannot set protection level to SAFE\n");
  14484.             return(0);
  14485.         }
  14486.         ftp_dpl = x;
  14487.         return(1);
  14488.     }
  14489.  
  14490. #ifdef CK_SSL
  14491.     if (x == FPL_SAF &&
  14492.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  14493.         printf("Cannot set protection level to safe\n");
  14494.         return(0);
  14495.     }
  14496. #endif /* CK_SSL */
  14497.     /* Start with a PBSZ of 1 meg */
  14498.     if (x != FPL_CLR) {
  14499.         if (setpbsz(DEFAULT_PBSZ) < 0)
  14500.           return(0);
  14501.     }
  14502.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  14503.                (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  14504.     if (y == REPLY_COMPLETE) {
  14505.         ftp_dpl = x;
  14506.         return(1);
  14507.     }
  14508.     return(0);
  14509. }
  14510.  
  14511. static int
  14512. fts_cpl(x) int x; {
  14513.     if (!auth_type 
  14514. #ifdef OS2
  14515.          || !ck_crypt_is_installed()
  14516. #endif /* OS2 */
  14517.          ) {
  14518.         switch ( x ) {
  14519.           case FPL_PRV:
  14520.             printf("?Cannot set protection level to PRIVATE\n");
  14521.             return(0);
  14522.           case FPL_SAF:
  14523.             printf("?Cannot set protection level to SAFE\n");
  14524.             return(0);
  14525.         }
  14526.         ftp_cpl = x;
  14527.         return(1);
  14528.     }
  14529.     if (x == FPL_CLR) {
  14530.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  14531.         if (y == REPLY_COMPLETE) {
  14532.             ftp_cpl = x;
  14533.             return(1);
  14534.         }
  14535.         return(0);
  14536.     }
  14537.     ftp_cpl = x;
  14538.     return(1);
  14539. }
  14540.  
  14541. #ifdef FTP_GSSAPI
  14542. static VOID
  14543. user_gss_error(maj_stat, min_stat, s)
  14544.     OM_uint32 maj_stat, min_stat;
  14545.     char *s;
  14546. {
  14547.     /* a lot of work just to report the error */
  14548.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  14549.     gss_buffer_desc msg;
  14550.     msg_ctx = 0;
  14551.     while (!msg_ctx) {
  14552.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  14553.                                        GSS_C_GSS_CODE,
  14554.                                        GSS_C_NULL_OID,
  14555.                                        &msg_ctx,
  14556.                                        &msg
  14557.                                        );
  14558.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14559.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14560.             fprintf(stderr, "GSSAPI error major: %s\n",
  14561.                     (char*)msg.value);
  14562.             gss_release_buffer(&gmin_stat, &msg);
  14563.         }
  14564.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14565.           break;
  14566.     }
  14567.     msg_ctx = 0;
  14568.     while (!msg_ctx) {
  14569.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  14570.                                        GSS_C_MECH_CODE,
  14571.                                        GSS_C_NULL_OID,
  14572.                                        &msg_ctx,
  14573.                                        &msg
  14574.                                        );
  14575.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14576.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14577.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  14578.             gss_release_buffer(&gmin_stat, &msg);
  14579.         }
  14580.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14581.           break;
  14582.     }
  14583.     fprintf(stderr, "GSSAPI error: %s\n", s);
  14584. }
  14585. #endif /* FTP_GSSAPI */
  14586.  
  14587. #ifndef NOMHHOST
  14588. #ifdef datageneral
  14589. #define NOMHHOST
  14590. #else
  14591. #ifdef HPUX5WINTCP
  14592. #define NOMHHOST
  14593. #endif /* HPUX5WINTCP */
  14594. #endif /* datageneral */
  14595. #endif /* NOMHHOST */
  14596.  
  14597. #ifdef INADDRX
  14598. static struct in_addr inaddrx;
  14599. #endif /* INADDRX */
  14600.  
  14601. static char *
  14602. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  14603.     register struct hostent *hp = 0;
  14604. #ifdef IP_TOS
  14605. #ifdef IPTOS_THROUGHPUT
  14606.     int tos;
  14607. #endif /* IPTOS_THROUGHPUT */
  14608. #endif /* IP_TOS */
  14609.     int s;
  14610.     GSOCKNAME_T len;
  14611.     static char hostnamebuf[MAXHOSTNAMELEN];
  14612.     char hostname[MAXHOSTNAMELEN] /* , *p, *q */ ;
  14613.     int  cport;
  14614. #ifdef DEBUG
  14615.     extern int debtim;
  14616.     int xdebtim;
  14617.     xdebtim = debtim;
  14618.     debtim = 1;
  14619. #endif /* DEBUG */
  14620.  
  14621.     debug(F111,"ftp_hookup",host,port);
  14622.  
  14623. #ifndef NOHTTP
  14624.     if (tcp_http_proxy) {
  14625.         struct servent *destsp;
  14626.         char *p, *q;
  14627.  
  14628.         ckmakmsg(proxyhost,HTTPCPYL,host,":",ckuitoa(port),NULL);
  14629.         for (p = tcp_http_proxy, q = hostname;
  14630.              *p != '\0' && *p != ':';
  14631.              p++, q++
  14632.              )
  14633.           *q = *p;
  14634.         *q = '\0';
  14635.  
  14636.         if (*p == ':')
  14637.           p++;
  14638.         else
  14639.           p = "http";
  14640.  
  14641.         destsp = getservbyname(p,"tcp");
  14642.         if (destsp)
  14643.           cport = ntohs(destsp->s_port);
  14644.         else if (p) {
  14645.           cport = atoi(p);
  14646.         } else
  14647.           cport = 80;
  14648.     } else
  14649. #endif /* NOHTTP */
  14650.     {
  14651.         ckstrncpy(hostname,host,MAXHOSTNAMELEN);
  14652.         cport = port;
  14653.     }
  14654.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  14655.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  14656.     if (hisctladdr.sin_addr.s_addr != (unsigned long) -1) {
  14657.         debug(F110,"ftp hookup A",hostname,0);
  14658.         hisctladdr.sin_family = AF_INET;
  14659.         ckstrncpy(hostnamebuf, hostname, MAXHOSTNAMELEN);
  14660.     } else {
  14661.         debug(F110,"ftp hookup B",hostname,0);
  14662.         hp = gethostbyname(hostname);
  14663. #ifdef HADDRLIST
  14664.         hp = ck_copyhostent(hp);        /* make safe copy that won't change */
  14665. #endif /* HADDRLIST */
  14666.         if (hp == NULL) {
  14667.             fprintf(stderr, "ftp: %s: Unknown host\n", host);
  14668.             ftpcode = -1;
  14669. #ifdef DEBUG
  14670.             debtim = xdebtim;
  14671. #endif /* DEBUG */
  14672.             return((char *) 0);
  14673.         }
  14674.         hisctladdr.sin_family = hp->h_addrtype;
  14675. #ifdef HADDRLIST
  14676.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  14677.                sizeof(hisctladdr.sin_addr));
  14678. #else /* HADDRLIST */
  14679.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  14680.                sizeof(hisctladdr.sin_addr));
  14681. #endif /* HADDRLIST */
  14682.         ckstrncpy(hostnamebuf, hp->h_name, MAXHOSTNAMELEN);
  14683.     }
  14684.     debug(F110,"ftp hookup C",hostnamebuf,0);
  14685.     ftp_host = hostnamebuf;
  14686.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14687.     debug(F101,"ftp hookup socket","",s);
  14688.     if (s < 0) {
  14689.         perror("ftp: socket");
  14690.         ftpcode = -1;
  14691. #ifdef DEBUG
  14692.         debtim = xdebtim;
  14693. #endif /* DEBUG */
  14694.         return(0);
  14695.     }
  14696.     hisctladdr.sin_port = htons(cport);
  14697.     errno = 0;
  14698. #ifdef HADDRLIST
  14699.     debug(F100,"ftp hookup HADDRLIST","",0);
  14700.     while
  14701. #else
  14702.     debug(F100,"ftp hookup no HADDRLIST","",0);
  14703.     if
  14704. #endif /* HADDRLIST */
  14705.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  14706.           debug(F101,"ftp hookup connect failed","",errno);
  14707. #ifdef HADDRLIST
  14708.           if (hp && hp->h_addr_list[1]) {
  14709.               int oerrno = errno;
  14710.  
  14711.               fprintf(stderr, "ftp: connect to address %s: ",
  14712.                       inet_ntoa(hisctladdr.sin_addr));
  14713.               errno = oerrno;
  14714.               perror((char *) 0);
  14715.               hp->h_addr_list++;
  14716.               memcpy((char *)&hisctladdr.sin_addr,
  14717.                      hp->h_addr_list[0],
  14718.                      sizeof(hisctladdr.sin_addr));
  14719.               fprintf(stdout, "Trying %s...\n",
  14720.                       inet_ntoa(hisctladdr.sin_addr));
  14721. #ifdef TCPIPLIB
  14722.               socket_close(s);
  14723. #else /* TCPIPLIB */
  14724.               close(s);
  14725. #endif /* TCPIPLIB */
  14726.               s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14727.               if (s < 0) {
  14728.                   perror("ftp: socket");
  14729.                   ftpcode = -1;
  14730. #ifdef DEBUG
  14731.                   debtim = xdebtim;
  14732. #endif /* DEBUG */
  14733.                   return(0);
  14734.               }
  14735.               continue;
  14736.           }
  14737. #endif /* HADDRLIST */
  14738.           perror("ftp: connect");
  14739.           ftpcode = -1;
  14740.           goto bad;
  14741.       }
  14742.     debug(F100,"ftp hookup connect ok","",0);
  14743.  
  14744.     len = sizeof (myctladdr);
  14745.     errno = 0;
  14746.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  14747.         debug(F101,"ftp hookup getsockname failed","",errno);
  14748.         perror("ftp: getsockname");
  14749.         ftpcode = -1;
  14750.         goto bad;
  14751.     }
  14752.     debug(F100,"ftp hookup getsockname ok","",0);
  14753.  
  14754. #ifndef NOHTTP
  14755.     if (tcp_http_proxy) {
  14756. #ifdef OS2
  14757.         char * agent = "Kermit 95";     /* Default user agent */
  14758. #else
  14759.         char * agent = "C-Kermit";
  14760. #endif /* OS2 */
  14761.  
  14762.         if (http_connect(s,agent,NULL,
  14763.                          tcp_http_proxy_user,
  14764.                          tcp_http_proxy_pwd,
  14765.                          0,
  14766.                          proxyhost
  14767.                          ) < 0) {
  14768.             char * foo = NULL;
  14769. #ifdef TCPIPLIB
  14770.             socket_close(s);
  14771. #else /* TCPIPLIB */
  14772.             close(s);
  14773. #endif /* TCPIPLIB */
  14774.  
  14775.             while (foo == NULL && tcp_http_proxy != NULL ) {
  14776.  
  14777.                 if (tcp_http_proxy_errno == 401 ||
  14778.                      tcp_http_proxy_errno == 407 ) {
  14779.                     char uid[UIDBUFLEN];
  14780.                     char pwd[PWDSIZ];
  14781.                     struct txtbox tb[2];
  14782.                     int ok;
  14783.  
  14784.                     tb[0].t_buf = uid;
  14785.                     tb[0].t_len = UIDBUFLEN;
  14786.                     tb[0].t_lbl = "Proxy Userid: ";
  14787.                     tb[0].t_dflt = NULL;
  14788.                     tb[0].t_echo = 1;
  14789.                     tb[1].t_buf = pwd;
  14790.                     tb[1].t_len = 256;
  14791.                     tb[1].t_lbl = "Proxy Passphrase: ";
  14792.                     tb[1].t_dflt = NULL;
  14793.                     tb[1].t_echo = 2;
  14794.  
  14795.                     ok = uq_mtxt("Proxy Server Authentication Required\n",
  14796.                                   NULL, 2, tb);
  14797.  
  14798.                     if (ok && uid[0]) {
  14799.                         char * proxy_user, * proxy_pwd;
  14800.  
  14801.                         proxy_user = tcp_http_proxy_user;
  14802.                         proxy_pwd  = tcp_http_proxy_pwd;
  14803.  
  14804.                         tcp_http_proxy_user = uid;
  14805.                         tcp_http_proxy_pwd = pwd;
  14806.  
  14807.                         foo = ftp_hookup(host, port, 0);
  14808.  
  14809.                         debug(F110,"ftp_hookup()",foo,0);
  14810.                         memset(pwd,0,PWDSIZ);
  14811.                         tcp_http_proxy_user = proxy_user;
  14812.                         tcp_http_proxy_pwd = proxy_pwd;
  14813.                     } else
  14814.                         break;
  14815.                 } else
  14816.                     break;
  14817.             }
  14818.             if (foo != NULL)
  14819.               return(foo);
  14820.             perror("ftp: connect");
  14821.             ftpcode = -1;
  14822.             goto bad;
  14823.         }
  14824.         ckstrncpy(hostnamebuf, proxyhost, MAXHOSTNAMELEN);
  14825.     }
  14826. #endif /* NOHTTP */
  14827.  
  14828.     csocket = s;
  14829.  
  14830. #ifdef CK_SSL
  14831.     if (tls) {
  14832.         /* FTP over SSL
  14833.          * If the connection is over an SSL proxy then the
  14834.          * auth_type will be NULL.  However, I'm not sure
  14835.          * whether we should protect the data channel in
  14836.          * that case or not.
  14837.          */
  14838.  
  14839.         debug(F100,"ftp hookup use_tls","",0);
  14840.         if (!ssl_auth()) {
  14841.             debug(F100,"ftp hookup ssl_auth failed","",0);
  14842.             auth_type = NULL;
  14843.             ftpcode = -1;
  14844.             csocket = -1;
  14845.             goto bad;
  14846.         }
  14847.         ssl_ftp_proxy = 1;
  14848.     }
  14849. #endif /* CK_SSL */
  14850.  
  14851. #ifdef IP_TOS
  14852. #ifdef IPTOS_LOWDELAY
  14853.     tos = IPTOS_LOWDELAY;
  14854.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  14855.       perror("ftp: setsockopt TOS (ignored)");
  14856. #endif
  14857. #endif
  14858.     if (!quiet)
  14859.       printf("Connected to %s.\n", host);
  14860.  
  14861.     /* Read greeting from server */
  14862.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  14863.         debug(F100,"ftp hookup bad reply","",0);
  14864. #ifdef TCPIPLIB
  14865.         socket_close(csocket);
  14866. #else /* TCPIPLIB */
  14867.         close(csocket);
  14868. #endif /* TCPIPLIB */
  14869.         ftpcode = -1;
  14870.         goto bad;
  14871.     }
  14872. #ifdef SO_OOBINLINE
  14873.     {
  14874.         int on = 1;
  14875.         errno = 0;
  14876.         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  14877.                sizeof(on)) < 0) {
  14878.             perror("ftp: setsockopt");
  14879.             debug(F101,"ftp hookup setsockopt failed","",errno);
  14880.         }
  14881. #ifdef DEBUG
  14882.         else
  14883.           debug(F100,"ftp hookup setsockopt ok","",0);
  14884. #endif /* DEBUG */
  14885.     }
  14886. #endif /* SO_OOBINLINE */
  14887.  
  14888. #ifdef DEBUG
  14889.     debtim = xdebtim;
  14890. #endif /* DEBUG */
  14891.     return(ftp_host);
  14892.  
  14893.   bad:
  14894.     debug(F100,"ftp hookup bad","",0);
  14895. #ifdef TCPIPLIB
  14896.     socket_close(s);
  14897. #else /* TCPIPLIB */
  14898.     close(s);
  14899. #endif /* TCPIPLIB */
  14900. #ifdef DEBUG
  14901.     debtim = xdebtim;
  14902. #endif /* DEBUG */
  14903.     csocket = -1;
  14904.     return((char *)0);
  14905. }
  14906.  
  14907. static VOID
  14908. ftp_init() {
  14909.     int i, n;
  14910.  
  14911.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  14912.     /* clients do it.  In any case, failure of this command is not a */
  14913.     /* reliable indication that the server does not support Restart. */
  14914.  
  14915.     okrestart = 0;
  14916.     if (!noinit) {
  14917.         n = ftpcmd("REST 0",NULL,0,0,0);
  14918.         if (n == REPLY_COMPLETE)
  14919.           okrestart = 1;
  14920. #ifdef COMMENT
  14921.         else if (ftp_deb)
  14922.           printf("WARNING: Unable to restore file pointer.\n");
  14923. #endif /* COMMENT */
  14924.     }
  14925.     n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
  14926.     if (n == REPLY_COMPLETE) {
  14927.         register char *cp, c = NUL;
  14928.         cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  14929.         if (cp == NULL)
  14930.           cp = ckstrchr(ftp_reply_str+4,'\r');
  14931.         if (cp) {
  14932.             if (cp[-1] == '.')
  14933.               cp--;
  14934.             c = *cp;                    /* Save this char */
  14935.             *cp = '\0';                 /* Replace it with NUL */
  14936.         }
  14937.         if (!quiet)
  14938.           printf("Remote system type is %s.\n",ftp_reply_str+4);
  14939.         ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  14940.         if (cp)                         /* Put back saved char */
  14941.           *cp = c;
  14942.     }
  14943.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  14944.  
  14945.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  14946.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  14947.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  14948.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  14949.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  14950.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  14951.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  14952.  
  14953. #ifdef FTP_PROXY
  14954.     unix_proxy = 0;
  14955.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  14956. #endif /* FTP_PROXY */
  14957.  
  14958.     if (ftp_cmdlin && ftp_xfermode == XMODE_M)
  14959.       ftp_typ = binary;                 /* Type given on command line */
  14960.     else                                /* Otherwise set it automatically */
  14961.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  14962.     changetype(ftp_typ,0);              /* Change to this type */
  14963.     g_ftp_typ = ftp_typ;                /* Make it the global type */
  14964.     if (!quiet)
  14965.       printf("Default transfer mode is %s\n",
  14966.              ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  14967.              );
  14968.     for (i = 0; i < 16; i++)        /* Init server FEATure table */
  14969.       sfttab[i] = 0;
  14970.     if (!noinit) {
  14971.         n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
  14972. #ifdef COMMENT
  14973.         if (n != REPLY_COMPLETE)
  14974.           printf("WARNING: Server does not accept MODE S(TREAM)\n");
  14975. #endif /* COMMENT */
  14976.         n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
  14977. #ifdef COMMENT
  14978.         if (n != REPLY_COMPLETE)
  14979.           printf("WARNING: Server does not accept STRU F(ILE)\n");
  14980. #endif /* COMMENT */
  14981.     if (featok) {
  14982.         n = ftpcmd("FEAT",NULL,0,0,0); /* Ask server about features */
  14983.         if (n == REPLY_COMPLETE) {
  14984.         debug(F101,"ftp_init FEAT","",sfttab[0]);
  14985.         if (deblog || ftp_deb) {
  14986.             int i;
  14987.             for (i = 1; i < 16 && i < nfeattab; i++) {
  14988.             debug(F111,"ftp_init FEAT",feattab[i].kwd,sfttab[i]);
  14989.             if (ftp_deb)
  14990.               printf("  Server %s %s\n",
  14991.                  sfttab[i] ? "supports" : "does not support",
  14992.                  feattab[i].kwd
  14993.                  );
  14994.             }
  14995.             /* Deal with disabled MLST opts here if necessary */
  14996.             /* But why would it be? */
  14997.         }
  14998.         }
  14999.     }
  15000.     }
  15001. }
  15002.  
  15003. static int
  15004. ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
  15005.     static char ftppass[PASSBUFSIZ]="";
  15006.     char tmp[PASSBUFSIZ];
  15007.     char *user = NULL, *pass = NULL, *acct = NULL;
  15008.     int n, aflag = 0;
  15009.     extern char uidbuf[];
  15010.     extern char pwbuf[];
  15011.     extern int  pwflg, pwcrypt;
  15012.  
  15013.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  15014.  
  15015.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  15016.       anonymous = 1;
  15017.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  15018.       anonymous = 1;
  15019.  
  15020. #ifdef FTP_SRP
  15021.     if (auth_type && !strcmp(auth_type, "SRP")) {
  15022.         user = srp_user;
  15023.         pass = srp_pass;
  15024.         acct = srp_acct;
  15025.     } else
  15026. #endif /* FTP_SRP */
  15027.       if (anonymous) {
  15028.           user = "anonymous";
  15029.           if (ftp_tmp) {        /* They gave a password */
  15030.               pass = ftp_tmp;
  15031.           } else if (ftp_apw) {        /* SET FTP ANONYMOUS-PASSWORD */
  15032.           pass = ftp_apw;
  15033.       } else {            /* Supply user@host */
  15034.           ckmakmsg(tmp,PASSBUFSIZ,whoami(),"@",myhost,NULL);
  15035.           pass = tmp;
  15036.           }
  15037.       debug(F110,"ftp anonymous",pass,0);
  15038.       } else {
  15039. #ifdef USE_RUSERPASS
  15040.           if (ruserpass(host, &user, &pass, &acct) < 0) {
  15041.               ftpcode = -1;
  15042.               return(0);
  15043.           }
  15044. #endif /* USE_RUSERPASS */
  15045.           if (ftp_logname) {
  15046.               user = ftp_logname;
  15047.               pass = ftp_tmp;
  15048.           } else if (uidbuf[0] && (ftp_tmp || pwbuf[0] && pwflg)) {
  15049.               user = uidbuf;
  15050.               if (ftp_tmp) {
  15051.                   pass = ftp_tmp;
  15052.               } else if (pwbuf[0] && pwflg) {
  15053.                   ckstrncpy(ftppass,pwbuf,PASSBUFSIZ);
  15054. #ifdef OS2
  15055.                   if ( pwcrypt )
  15056.                       ck_encrypt((char *)ftppass);
  15057. #endif /* OS2 */
  15058.                   pass = ftppass;
  15059.               }
  15060.           }
  15061.           acct = ftp_acc;
  15062.           while (user == NULL) {
  15063.               char *myname, prompt[PROMPTSIZ];
  15064.               int ok;
  15065.  
  15066.               myname = whoami();
  15067.               if (myname)
  15068.                 ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  15069.                           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  15070.               else
  15071.                 ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  15072.               tmp[0] = '\0';
  15073.               
  15074.               ok = uq_txt(NULL,prompt,1,NULL,tmp,PASSBUFSIZ,NULL,
  15075.                           DEFAULT_UQ_TIMEOUT);
  15076.               if (!ok || *tmp == '\0')
  15077.                 user = myname;
  15078.               else
  15079.                 user = brstrip(tmp);
  15080.           }
  15081.       }
  15082.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  15083.     if (n == REPLY_COMPLETE) {
  15084.         /* determine if we need to send a dummy password */
  15085.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  15086.           ftpcmd("PASS dummy",NULL,0,0,1);
  15087.     } else if (n == REPLY_CONTINUE) {
  15088. #ifdef CK_ENCRYPTION
  15089.         int oldftp_cpl;
  15090. #endif /* CK_ENCRYPTION */
  15091.  
  15092.         if (pass == NULL) {
  15093.             int ok;
  15094.             setint();
  15095.             ok = uq_txt(NULL," Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
  15096.                         DEFAULT_UQ_TIMEOUT);
  15097.             if (ok)
  15098.                 pass = brstrip(ftppass);
  15099.         }
  15100.  
  15101. #ifdef CK_ENCRYPTION
  15102.         oldftp_cpl = ftp_cpl;
  15103.         ftp_cpl = FPL_PRV;
  15104. #endif /* CK_ENCRYPTION */
  15105.         n = ftpcmd("PASS",pass,-1,-1,1);
  15106.         if (!anonymous && pass) {
  15107.             char * p = pass;
  15108.             while (*p++) *(p-1) = NUL;
  15109.             makestr(&ftp_tmp,NULL);
  15110.         }
  15111. #ifdef CK_ENCRYPTION
  15112.         /* level may have changed */
  15113.         if (ftp_cpl == FPL_PRV)
  15114.           ftp_cpl = oldftp_cpl;
  15115. #endif /* CK_ENCRYPTION */
  15116.     }
  15117.     if (n == REPLY_CONTINUE) {
  15118.         aflag++;
  15119.         if (acct == NULL) {
  15120.             static char ftpacct[80];
  15121.             int ok;
  15122.             setint();
  15123.             ok = uq_txt(NULL," Account: ",2,NULL,ftpacct,80,NULL,
  15124.                         DEFAULT_UQ_TIMEOUT);
  15125.             if (ok)
  15126.                 acct = brstrip(ftpacct);
  15127.         }
  15128.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  15129.     }
  15130.     if (n != REPLY_COMPLETE) {
  15131.         fprintf(stderr, "FTP login failed.\n");
  15132.         if (haveurl)
  15133.           doexit(BAD_EXIT,-1);
  15134.         return(0);
  15135.     }
  15136.     if (!aflag && acct != NULL) {
  15137.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  15138.     }
  15139.     makestr(&ftp_logname,user);
  15140.     loggedin = 1;
  15141. #ifdef LOCUS
  15142.     /* Unprefixed file management commands go to server */
  15143.     if (autolocus && !ftp_cmdlin) {
  15144.     setlocus(0,1);
  15145.     }
  15146. #endif /* LOCUS */
  15147.     ftp_init();
  15148.  
  15149.     if (anonymous && !quiet) {
  15150.         printf(" Logged in as anonymous (%s)\n",pass);
  15151.         memset(pass, 0, strlen(pass));
  15152.     }
  15153.     if (ftp_rdir) {
  15154.         if (doftpcwd(ftp_rdir,-1) < 1)
  15155.           doexit(BAD_EXIT,-1);
  15156.     }
  15157.  
  15158. #ifdef FTP_PROXY
  15159.     if (proxy)
  15160.       return(1);
  15161. #endif /* FTP_PROXY */
  15162.     return(1);
  15163. }
  15164.  
  15165. static int
  15166. ftp_reset() {
  15167.     int rc;
  15168. #ifdef BSDSELECT
  15169.     int nfnd = 1;
  15170.     fd_set mask;
  15171.     FD_ZERO(&mask);
  15172.     while (nfnd > 0) {
  15173.         FD_SET(csocket, &mask);
  15174.         if ((nfnd = empty(&mask,0)) < 0) {
  15175.             perror("reset");
  15176.             ftpcode = -1;
  15177.             lostpeer();
  15178.             return(0);
  15179.         } else if (nfnd) {
  15180.             getreply(0,-1,-1,ftp_vbm,0);
  15181.         }
  15182.     }
  15183. #else /* BSDSELECT */
  15184. #ifdef IBMSELECT
  15185.     int nfnd = 1;
  15186.     while (nfnd > 0) {
  15187.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  15188.             perror("reset");
  15189.             ftpcode = -1;
  15190.             lostpeer();
  15191.             return(0);
  15192.         } else if (nfnd) {
  15193.             getreply(0,-1,-1,ftp_vbm,0);
  15194.         }
  15195.     }
  15196. #endif /* IBMSELECT */
  15197. #endif /* BSDSELECT */
  15198.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  15199.     if (rc > 0)
  15200.       loggedin = 0;
  15201.     return(rc);
  15202. }
  15203.  
  15204. static int
  15205. ftp_rename(from, to) char * from, * to; {
  15206.     int lcs = -1, rcs = -1;
  15207. #ifndef NOCSETS
  15208.     if (ftp_xla) {
  15209.         lcs = ftp_csl;
  15210.         if (lcs < 0) lcs = fcharset;
  15211.         rcs = ftp_csx;
  15212.         if (rcs < 0) rcs = ftp_csr;
  15213.     }
  15214. #endif /* NOCSETS */
  15215.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  15216.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  15217.     }
  15218.     return(0);                          /* Failure */
  15219. }
  15220.  
  15221. static int
  15222. ftp_umask(mask) char * mask; {
  15223.     int rc;
  15224.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  15225.     return(rc);
  15226. }
  15227.  
  15228. static int
  15229. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  15230.     int n = 0, aflag = 0;
  15231.     char pwd[PWDSIZ];
  15232.  
  15233.     if (!auth_type && ftp_aut) {
  15234. #ifdef FTP_SRP
  15235.         if (ck_srp_is_installed()) {
  15236.             if (srp_ftp_auth( NULL, user, pass)) {
  15237.                 makestr(&pass,srp_pass);
  15238.             }
  15239.         }
  15240. #endif /* FTP_SRP */
  15241.     }
  15242.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  15243.     if (n == REPLY_COMPLETE)
  15244.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  15245.     else if (n == REPLY_CONTINUE) {
  15246. #ifdef CK_ENCRYPTION
  15247.         int oldftp_cpl;
  15248. #endif /* CK_ENCRYPTION */
  15249.         if (pass == NULL || !pass[0]) {
  15250.             int ok;
  15251.             pwd[0] = '\0';
  15252.             setint();
  15253.             ok = uq_txt(NULL," Password: ",2,NULL,pwd,PWDSIZ,NULL,
  15254.                         DEFAULT_UQ_TIMEOUT);
  15255.             if (ok)
  15256.                 pass = brstrip(pwd);
  15257.         }
  15258.  
  15259. #ifdef CK_ENCRYPTION
  15260.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  15261.           ftp_cpl = PROT_P;
  15262. #endif /* CK_ENCRYPTION */
  15263.         n = ftpcmd("PASS",pass,-1,-1,1);
  15264.         memset(pass, 0, strlen(pass));
  15265. #ifdef CK_ENCRYPTION
  15266.         /* level may have changed */
  15267.         if (ftp_cpl == PROT_P)
  15268.           ftp_cpl = oldftp_cpl;
  15269. #endif /* CK_ENCRYPTION */
  15270.     }
  15271.     if (n == REPLY_CONTINUE) {
  15272.         if (acct == NULL || !acct[0]) {
  15273.             int ok;
  15274.             pwd[0] = '\0';
  15275.             setint();
  15276.             ok = uq_txt(NULL," Account: ",2,NULL,pwd,PWDSIZ,NULL,
  15277.                         DEFAULT_UQ_TIMEOUT);
  15278.             if (ok)
  15279.                 acct = pwd;
  15280.         }
  15281.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  15282.         aflag++;
  15283.     }
  15284.     if (n != REPLY_COMPLETE) {
  15285.         printf("Login failed.\n");
  15286.         return(0);
  15287.     }
  15288.     if (!aflag && acct != NULL && acct[0]) {
  15289.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  15290.     }
  15291.     if (n == REPLY_COMPLETE) {
  15292.         makestr(&ftp_logname,user);
  15293.         loggedin = 1;
  15294.         ftp_init();
  15295.         return(1);
  15296.     }
  15297.     return(0);
  15298. }
  15299.  
  15300. char *
  15301. ftp_authtype() {
  15302.     if (!connected)
  15303.       return("NULL");
  15304.     return(auth_type ? auth_type : "NULL");
  15305. }
  15306.  
  15307. char *
  15308. ftp_cpl_mode() {
  15309.     switch (ftp_cpl) {
  15310.       case FPL_CLR:
  15311.         return("clear");
  15312.       case FPL_SAF:
  15313.         return("safe");
  15314.       case FPL_PRV:
  15315.         return("private");
  15316.       case FPL_CON:
  15317.         return("confidential");
  15318.       default:
  15319.         return("(error)");
  15320.     }
  15321. }
  15322.  
  15323. char *
  15324. ftp_dpl_mode() {
  15325.     switch (ftp_dpl) {
  15326.       case FPL_CLR:
  15327.         return("clear");
  15328.       case FPL_SAF:
  15329.         return("safe");
  15330.       case FPL_PRV:
  15331.         return("private");
  15332.       case FPL_CON:
  15333.         return("confidential");
  15334.       default:
  15335.         return("(error)");
  15336.     }
  15337. }
  15338.  
  15339.  
  15340. /* remote_files() */
  15341. /*
  15342.    Returns next remote filename on success;
  15343.    NULL on error or no more files with global rfrc set to:
  15344.      -1: Bad argument
  15345.      -2: Server error response to NLST, e.g. file not found
  15346.      -3: No more files
  15347.      -9: Internal error
  15348. */
  15349. #define FTPNAMBUFLEN CKMAXPATH+1024
  15350.  
  15351. /* Check: ckmaxfiles CKMAXOPEN */
  15352.  
  15353. #define MLSDEPTH 128            /* Stack of open temp files */
  15354. static int mlsdepth = 0;        /* Temp file stack depth */
  15355. static FILE * tmpfilptr[MLSDEPTH+1] = { NULL, NULL }; /* Temp file pointers */
  15356. static char * tmpfilnam[MLSDEPTH+1] = { NULL, NULL }; /* Temp file names */
  15357.  
  15358. static VOID
  15359. mlsreset() {                /* Reset MGET temp-file stack */
  15360.     int i;
  15361.     for (i = 0; i <= mlsdepth; i++) {
  15362.     if (tmpfilptr[i]) {
  15363.         fclose(tmpfilptr[i]);
  15364.         tmpfilptr[i] = NULL;
  15365.         if (tmpfilnam[i]) {
  15366. #ifdef OS2
  15367.         unlink(tmpfilnam[i]);
  15368. #endif /* OS2 */
  15369.         free(tmpfilnam[i]);
  15370.         }
  15371.     }
  15372.     }
  15373.     mlsdepth = 0;
  15374. }
  15375.  
  15376. static CHAR *
  15377. #ifdef CK_ANSIC
  15378. remote_files(int new_query, CHAR * arg, CHAR * pattern, int proxy_switch)
  15379. #else /* CK_ANSIC */
  15380. remote_files(new_query, arg, pattern, proxy_switch)
  15381.     int new_query;
  15382.     CHAR * arg;                /* That we send to the server */
  15383.     CHAR * pattern;            /* That we use locally */
  15384.     int proxy_switch;
  15385. #endif /* CK_ANSIC */
  15386. /* remote_files */ {
  15387.     static CHAR buf[FTPNAMBUFLEN];
  15388.     CHAR *cp, *whicharg;
  15389.     char * cdto = NULL;
  15390.     char * p;
  15391.     int i, x, forced = 0;
  15392.     int lcs = 0, rcs = 0, xlate = 0;
  15393.  
  15394.     debug(F101,"ftp remote_files new_query","",new_query);
  15395.     debug(F110,"ftp remote_files arg",arg,0);
  15396.     debug(F110,"ftp remote_files pattern",pattern,0);
  15397.  
  15398.     rfrc = -1;
  15399.     if (pattern)            /* Treat empty pattern same as NULL */
  15400.       if (!*pattern)
  15401.     pattern = NULL;
  15402.     if (arg)                /* Ditto for arg */
  15403.       if (!*arg)
  15404.     arg = NULL;
  15405.  
  15406.   again:
  15407.  
  15408.     if (new_query) {
  15409.         if (tmpfilptr[mlsdepth]) {
  15410.             fclose(tmpfilptr[mlsdepth]);
  15411.             tmpfilptr[mlsdepth] = NULL;
  15412. #ifdef OS2
  15413.             if (!ftp_deb && !deblog)
  15414.               unlink(tmpfilnam[mlsdepth]);
  15415. #endif /* OS2 */
  15416.         }
  15417.     }
  15418.     if (tmpfilptr[mlsdepth] == NULL) {
  15419.         extern char * tempdir;
  15420.         char * p;
  15421.         debug(F110,"ftp remote_files tempdir",tempdir,0);
  15422.         if (tempdir) {
  15423.             p = tempdir;
  15424.         } else {
  15425. #ifdef OS2
  15426. #ifdef NT
  15427.             p = getenv("K95TMP");
  15428. #else
  15429.             p = getenv("K2TMP");
  15430. #endif /* NT */
  15431.             if (!p)
  15432. #endif /* OS2 */
  15433.               p = getenv("CK_TMP");
  15434.             if (!p)
  15435.               p = getenv("TMPDIR");
  15436.             if (!p) p = getenv("TEMP");
  15437.             if (!p) p = getenv("TMP");
  15438. #ifdef OS2ORUNIX
  15439.             if (p) {
  15440.                 int len = strlen(p);
  15441.                 if (p[len-1] != '/'
  15442. #ifdef OS2
  15443.                     && p[len-1] != '\\'
  15444. #endif /* OS2 */
  15445.                      ) {
  15446.                     static char foo[CKMAXPATH];
  15447.                     ckstrncpy(foo,p,CKMAXPATH);
  15448.                     ckstrncat(foo,"/",CKMAXPATH);
  15449.                     p = foo;
  15450.                 }
  15451.             } else
  15452. #else /* OS2ORUNIX */
  15453.             if (!p)
  15454. #endif /* OS2ORUNIX */
  15455. #ifdef UNIX                             /* Systems that have a standard */
  15456.                 p = "/tmp/";            /* temporary directory... */
  15457. #else
  15458. #ifdef datageneral
  15459.             p = ":TMP:";
  15460. #else
  15461.             p = "";
  15462. #endif /* datageneral */
  15463. #endif /* UNIX */
  15464.         }
  15465.         debug(F110,"ftp remote_files p",p,0);
  15466.  
  15467.     /* Get temp file */
  15468.  
  15469.     if ((tmpfilnam[mlsdepth] = (char *)malloc(CKMAXPATH+1))) {
  15470.         ckmakmsg((char *)tmpfilnam[mlsdepth],
  15471.              CKMAXPATH+1,p,"ckXXXXXX",NULL,NULL);
  15472.     } else {
  15473.         printf("?Malloc failure: remote_files()\n");
  15474.         return(NULL);
  15475.     }
  15476.  
  15477. #ifdef NT
  15478.     {
  15479.         char * tmpfil = mktemp((char *)tmpfilnam[mlsdepth]);
  15480.         if ( tmpfil )
  15481.         ckstrncpy(tmpfilnam[mlsdepth],tmpfil,CKMAXPATH+1);
  15482.     }
  15483. #else /* NT */
  15484. #ifdef MKTEMP
  15485. #ifdef MKSTEMP
  15486.     x = mkstemp((char *)tmpfilnam[mlsdepth]);
  15487.     if (x > -1) close(x);        /* We just want the name. */
  15488. #else
  15489.         mktemp((char *)tmpfilnam[mlsdepth]);
  15490. #endif /* MKSTEMP */
  15491.         /* if no mktmpnam() the name will just be "ckXXXXXX"... */
  15492. #endif /* MKTEMP */
  15493. #endif /* NT */
  15494.  
  15495.     debug(F111,"ftp remote_files tmpfilnam[mlsdepth]",
  15496.           tmpfilnam[mlsdepth],mlsdepth);
  15497.  
  15498. #ifdef FTP_PROXY
  15499.         if (proxy_switch) {
  15500.             pswitch(!proxy);
  15501.         }
  15502. #endif /* FTP_PROXY */
  15503.  
  15504.         debug(F101,"ftp remote_files ftp_xla","",ftp_xla);
  15505.         debug(F101,"ftp remote_files ftp_csl","",ftp_csl);
  15506.         debug(F101,"ftp remote_files ftp_csr","",ftp_csr);
  15507.  
  15508. #ifndef NOCSETS
  15509.         xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
  15510.         if (xlate) {                    /* ON? */
  15511.             lcs = ftp_csl;              /* Local charset */
  15512.             if (lcs < 0) lcs = fcharset;
  15513.             if (lcs < 0) xlate = 0;
  15514.         }
  15515.         if (xlate) {                    /* Still ON? */
  15516.             rcs = ftp_csx;              /* Remote (Server) charset */
  15517.             if (rcs < 0) rcs = ftp_csr;
  15518.             if (rcs < 0) xlate = 0;
  15519.         }
  15520. #endif /* NOCSETS */
  15521.  
  15522.     forced = mgetforced;        /* MGET method forced? */
  15523.     if (!forced || !mgetmethod)    /* Not forced... */
  15524.       mgetmethod = (sfttab[0] && sfttab[SFT_MLST]) ? /* so pick one */
  15525.           SND_MLS :
  15526.           SND_NLS; 
  15527. /*                                           
  15528.   User's Command:                 Result:
  15529.     mget /nlst                     NLST (NULL)
  15530.     mget /nlst foo                 NLST foo
  15531.     mget /nlst *.txt               NLST *.txt 
  15532.     mget /nlst /match:*.txt        NLST (NULL)
  15533.     mget /nlst /match:*.txt  foo   NLST foo   
  15534.     mget /mlsd                     MLSD (NULL)
  15535.     mget /mlsd foo                 MLSD foo
  15536.     mget /mlsd *.txt               MLSD (NULL)
  15537.     mget /mlsd /match:*.txt        MLSD (NULL)
  15538.     mget /mlsd /match:*.txt  foo   MLSD foo
  15539. */
  15540.     x = -1;
  15541.     while (x < 0) {
  15542.         if (pattern) {        /* Don't simplify this! */
  15543.         whicharg = arg;
  15544.         } else if (mgetmethod == SND_MLS) {
  15545.         if (arg)
  15546.           whicharg = iswild((char *)arg) ? NULL : arg;
  15547.         else
  15548.           whicharg = NULL;
  15549.         } else {
  15550.         whicharg = arg;
  15551.         }
  15552.         debug(F110,"ftp remote_files mgetmethod",
  15553.           mgetmethod == SND_MLS ? "MLSD" : "NLST", 0);
  15554.         debug(F110,"ftp remote_files whicharg",whicharg,0);
  15555.  
  15556.         x = recvrequest((mgetmethod == SND_MLS) ? "MLSD" : "NLST",
  15557.                 (char *)tmpfilnam[mlsdepth],
  15558.                 (char *)whicharg,
  15559.                 "wb",
  15560.                 0,
  15561.                 0,
  15562.                 NULL,
  15563.                 xlate,
  15564.                 lcs,
  15565.                 rcs
  15566.                 );
  15567.         if (x < 0) {        /* Chosen method wasn't accepted */
  15568.         if (forced) {
  15569.             if (ftpcode > 500 && ftpcode < 505 && !quiet)
  15570.               printf("?%s: Not supported by server\n",
  15571.                  mgetmethod == SND_MLS ? "MLSD" : "NLST"
  15572.                  );
  15573.             rfrc = -2;        /* Fail */
  15574.             return(NULL);
  15575.         }
  15576.         /* Not forced - if MLSD failed, try NLST */
  15577.         if (mgetmethod == SND_MLS) {  /* Server lied about MLST */
  15578.             sfttab[SFT_MLST] = 0;     /* So disable it */
  15579.             mlstok = 0;              /* and */
  15580.             mgetmethod = SND_NLS;     /* try NLST */
  15581.             continue;
  15582.         }
  15583.         rfrc = -2;
  15584.         return(NULL);
  15585.         }
  15586.     }
  15587. #ifdef FTP_PROXY
  15588.         if (proxy_switch) {
  15589.             pswitch(!proxy);
  15590.         }
  15591. #endif /* FTP_PROXY */
  15592.         tmpfilptr[mlsdepth] = fopen((char *)tmpfilnam[mlsdepth], "r");
  15593. #ifndef OS2
  15594.     if (tmpfilptr[mlsdepth]) {
  15595.         if (!ftp_deb && !deblog)
  15596.           unlink(tmpfilnam[mlsdepth]);
  15597.     }
  15598. #endif /* OS2 */
  15599.       notemp:
  15600.         if (!tmpfilptr[mlsdepth]) {
  15601.             debug(F110,"ftp remote_files open fail",tmpfilnam[mlsdepth],0);
  15602.             if ((!dpyactive || ftp_deb))
  15603.               printf("?Can't find list of remote files, oops\n");
  15604.             rfrc = -9;
  15605.             return(NULL);
  15606.         }
  15607.     if (ftp_deb)
  15608.       printf("LISTFILE: %s\n",tmpfilnam[mlsdepth]);
  15609.     }
  15610.     buf[0] = NUL;
  15611.     buf[FTPNAMBUFLEN-1] = NUL;
  15612.     buf[FTPNAMBUFLEN-2] = NUL;
  15613.  
  15614.     /* We have to redo all this because the first time was only for */
  15615.     /* for getting the file list, now it's for getting each file */
  15616.  
  15617.     if (arg && mgetmethod == SND_MLS) {    /* MLSD */
  15618.     if (!pattern && iswild((char *)arg)) {
  15619.         pattern = arg;        /* Wild arg is really a pattern */
  15620.         if (pattern)
  15621.           if (!*pattern)
  15622.         pattern = NULL;
  15623.         arg = NULL;            /* and not an arg */
  15624.     }
  15625.     if (new_query) {        /* Initial query? */
  15626.         cdto = (char *)arg;        /* (nonwild) arg given? */
  15627.         if (cdto)
  15628.           if (!*cdto)
  15629.         cdto = NULL;
  15630.         if (cdto)            /* If so, then CD to it */
  15631.           doftpcwd(cdto,0);
  15632.     }
  15633.     }
  15634.     new_query = 0;
  15635.  
  15636.     if (fgets((char *)buf, FTPNAMBUFLEN, tmpfilptr[mlsdepth]) == NULL) {
  15637.         fclose(tmpfilptr[mlsdepth]);
  15638.         tmpfilptr[mlsdepth] = NULL;
  15639.  
  15640. #ifdef OS2
  15641.         if (!ftp_deb && !deblog)
  15642.           unlink(tmpfilnam[mlsdepth]);
  15643. #endif /* OS2 */
  15644.         if (ftp_deb && !deblog) {
  15645.             printf("(Temporary file %s NOT deleted)\n",
  15646.            (char *)tmpfilnam[mlsdepth]);
  15647.         }
  15648.     if (mlsdepth <= 0) {        /* EOF at depth 0 */
  15649.         rfrc = -3;            /* means we're done */
  15650.         return(NULL);
  15651.     }
  15652.     printf("POPPING(%d)...\n",mlsdepth-1); 
  15653.     if (tmpfilnam[mlsdepth]) free(tmpfilnam[mlsdepth]);
  15654.     mlsdepth--;
  15655.     doftpcdup();
  15656.     zchdir("..");            /* <-- Not portable */
  15657.     goto again;
  15658.     }
  15659.     if (buf[FTPNAMBUFLEN-1]) {
  15660.     printf("?BUFFER OVERFLOW -- FTP NLST or MLSD string longer than %d\n",
  15661.            FTPNAMBUFLEN
  15662.            );
  15663.     debug(F101,"remote_files buffer overrun","",FTPNAMBUFLEN);
  15664.     return(NULL);
  15665.     }
  15666.     /* debug(F110,"ftp remote_files buf 1",buf,0); */
  15667.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  15668.       *cp = '\0';
  15669.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  15670.       *cp = '\0';
  15671.     debug(F110,"ftp remote_files buf",buf,0);
  15672.     rfrc = 0;
  15673.  
  15674.     if (ftp_deb)
  15675.       printf("[%s]\n",(char *)buf);
  15676.  
  15677.     havesize = (CK_OFF_T)-1;        /* Initialize file facts... */
  15678.     havetype = 0;
  15679.     makestr(&havemdtm,NULL);
  15680.     p = (char *)buf;
  15681.  
  15682.     if (mgetmethod == SND_NLS) {    /* NLST... */
  15683.     if (pattern) {
  15684.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15685.           goto again;
  15686.     }
  15687.     } else {                /* MLSD... */
  15688.     p = parsefacts((char *)buf);
  15689.     switch (havetype) {
  15690.       case FTYP_FILE:        /* File: Get it if it matches */
  15691.         if (pattern) {
  15692.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15693.           goto again;
  15694.         }
  15695.         break;
  15696.       case FTYP_CDIR:        /* Current directory */
  15697.       case FTYP_PDIR:        /* Parent directory */
  15698.         goto again;            /* Skip */
  15699.       case FTYP_DIR:        /* (Sub)Directory */
  15700.         if (!recursive)        /* If not /RECURSIVE */
  15701.           goto again;        /* Skip */
  15702.         if (mlsdepth < MLSDEPTH) {
  15703.         char * p2 = NULL;
  15704.         mlsdepth++;
  15705.         printf("RECURSING [%s](%d)...\n",p,mlsdepth); 
  15706.         if (doftpcwd(p,0) > 0) {
  15707.             int x;
  15708.             if (!ckstrchr(p,'/')) {
  15709.             /* zmkdir() needs dirsep */
  15710.             if ((p2 = (char *)malloc((int)strlen(p) + 2))) {
  15711.                 strcpy(p2,p);    /* SAFE */
  15712.                 strcat(p2,"/");    /* SAFE */
  15713.                 p = p2;
  15714.             }
  15715.             }
  15716. #ifdef NOMKDIR
  15717.             x = -1;
  15718. #else
  15719.             x = zmkdir(p);
  15720. #endif /* NOMKDIR */
  15721.             if (x > -1) {
  15722.             zchdir(p);
  15723.             p = (char *)remote_files(1,arg,pattern,0);
  15724.             if (p2) free(p2);
  15725.             } else {
  15726.             printf("?mkdir failed: [%s] Depth=%d\n",
  15727.                    p,
  15728.                    mlsdepth
  15729.                    );
  15730.             mlsreset();
  15731.             if (p2) free(p2);
  15732.             return(NULL);
  15733.             }
  15734.         } else {
  15735.             printf("?CWD failed: [%s] Depth=%d\n",p,mlsdepth);
  15736.             mlsreset();
  15737.             return(NULL);
  15738.         }
  15739.         } else {
  15740.         printf("MAX DIRECTORY STACK DEPTH EXCEEDED: %d\n",
  15741.                mlsdepth
  15742.                );
  15743.         mlsreset();
  15744.         return(NULL);
  15745.         }
  15746.     }
  15747.     }
  15748.  
  15749. #ifdef DEBUG
  15750.     if (deblog) {
  15751.     debug(F101,"remote_files havesize","",havesize);
  15752.     debug(F101,"remote_files havetype","",havetype);
  15753.     debug(F110,"remote_files havemdtm",havemdtm,0);    
  15754.     debug(F110,"remote_files name",p,0);    
  15755.     }
  15756. #endif /* DEBUG */
  15757.     return((CHAR *)p);
  15758. }
  15759.  
  15760. /* N O T  P O R T A B L E !!! */
  15761.  
  15762. #if (SIZEOF_SHORT == 4)
  15763. typedef unsigned short ftp_uint32;
  15764. typedef short ftp_int32;
  15765. #else
  15766. #if (SIZEOF_INT == 4)
  15767. typedef unsigned int ftp_uint32;
  15768. typedef int ftp_int32;
  15769. #else
  15770. #if (SIZEOF_LONG == 4)
  15771. typedef ULONG ftp_uint32;
  15772. typedef long ftp_int32;
  15773. #endif
  15774. #endif
  15775. #endif
  15776.  
  15777. /* Perhaps use these in general, certainly use them for GSSAPI */
  15778.  
  15779. #ifndef looping_write
  15780. #define ftp_int32 int
  15781. #define ftp_uint32 unsigned int
  15782. static int
  15783. looping_write(fd, buf, len)
  15784.     int fd;
  15785.     register CONST char *buf;
  15786.     int len;
  15787. {
  15788.     int cc;
  15789.     register int wrlen = len;
  15790.     do {
  15791.         cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  15792.         if (cc < 0) {
  15793.             if (errno == EINTR)
  15794.               continue;
  15795.             return(cc);
  15796.         } else {
  15797.             buf += cc;
  15798.             wrlen -= cc;
  15799.         }
  15800.     } while (wrlen > 0);
  15801.     return(len);
  15802. }
  15803. #endif
  15804. #ifndef looping_read
  15805. static int
  15806. looping_read(fd, buf, len)
  15807.     int fd;
  15808.     register char *buf;
  15809.     register int len;
  15810. {
  15811.     int cc, len2 = 0;
  15812.  
  15813.     do {
  15814.         cc = recv(fd, (char *)buf, len,0);
  15815.         if (cc < 0) {
  15816.             if (errno == EINTR)
  15817.               continue;
  15818.             return(cc);                 /* errno is already set */
  15819.         } else if (cc == 0) {
  15820.             return(len2);
  15821.         } else {
  15822.             buf += cc;
  15823.             len2 += cc;
  15824.             len -= cc;
  15825.         }
  15826.     } while (len > 0);
  15827.     return(len2);
  15828. }
  15829. #endif /* looping_read */
  15830.  
  15831. #define ERR -2
  15832.  
  15833. #ifdef COMMENT
  15834. static
  15835. secure_putbyte(fd, c) int fd; CHAR c; {
  15836.     int ret;
  15837.  
  15838.     ucbuf[nout++] = c;
  15839.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  15840.         nout = 0;
  15841.         if (!ftpissecure())
  15842.           ret = send(fd, (SENDARG2TYPE)ucbuf,
  15843.                      (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  15844.         else
  15845.           ret = secure_putbuf(fd,
  15846.                               ucbuf,
  15847.                               (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  15848.                               );
  15849.         return(ret?ret:c);
  15850.     }
  15851.     return(c);
  15852. }
  15853. #endif /* COMMENT */
  15854.  
  15855. /* returns:
  15856.  *       0  on success
  15857.  *      -1  on error (errno set)
  15858.  *      -2  on security error
  15859.  */
  15860. static int
  15861. secure_flush(fd) int fd; {
  15862.     int rc = 0;
  15863.     int len = 0;
  15864.  
  15865.     if (nout > 0) {
  15866.         len = nout;
  15867.         if (!ftpissecure()) {
  15868.             rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  15869.             nout = 0;
  15870.             goto xflush;
  15871.         } else {
  15872.             rc = secure_putbuf(fd, ucbuf, nout);
  15873.             if (rc)
  15874.               goto xflush;
  15875.         }
  15876.     }
  15877.     rc = (!ftpissecure()) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  15878.  
  15879.   xflush:
  15880.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  15881.     spackets++;
  15882.         spktl = len;
  15883.         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  15884.     }
  15885.     return(rc);
  15886. }
  15887.  
  15888. #ifdef COMMENT                          /* (not used) */
  15889. /* returns:
  15890.  *      c>=0  on success
  15891.  *      -1    on error
  15892.  *      -2    on security error
  15893.  */
  15894. static int
  15895. #ifdef CK_ANSIC
  15896. secure_putc(char c, int fd)
  15897. #else
  15898. secure_putc(c, fd) char c; int fd;
  15899. #endif /* CK_ANSIC */
  15900. /* secure_putc */ {
  15901.     return(secure_putbyte(fd, (CHAR) c));
  15902. }
  15903. #endif /* COMMENT */
  15904.  
  15905. /* returns:
  15906.  *      nbyte on success
  15907.  *      -1  on error (errno set)
  15908.  *      -2  on security error
  15909.  */
  15910. static int
  15911. #ifdef CK_ANSIC
  15912. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  15913. #else
  15914. secure_write(fd, buf, nbyte)
  15915.     int fd;
  15916.     CHAR * buf;
  15917.     unsigned int nbyte;
  15918. #endif /* CK_ANSIC */
  15919. {
  15920.     int ret;
  15921.  
  15922. #ifdef FTP_TIMEOUT    
  15923.     ftp_timed_out = 0;
  15924.     if (check_data_connection(fd,1) < 0) {
  15925.     ftp_timed_out = 1;
  15926.     return(-3);
  15927.     }
  15928. #endif    /* FTP_TIMEOUT */
  15929.  
  15930.     if (!ftpissecure()) {
  15931.         if (nout > 0) {
  15932.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  15933.               return(ret);
  15934.             nout = 0;
  15935.         }
  15936.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  15937.     } else {
  15938.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  15939.         int bsent = 0;
  15940.  
  15941.         while (bsent < nbyte) {
  15942.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  15943.                         (ucbuflen - nout) : (nbyte - bsent));
  15944. #ifdef DEBUG
  15945.         if (deblog) {
  15946.         debug(F101,"secure_write ucbuflen","",ucbuflen);
  15947.         debug(F101,"secure_write ucbufsiz","",ucbufsiz);
  15948.         debug(F101,"secure_write bsent","",bsent);
  15949.         debug(F101,"secure_write b2cp","",b2cp);
  15950.         }
  15951. #endif /* DEBUG */
  15952.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  15953.             nout += b2cp;
  15954.             bsent += b2cp;
  15955.  
  15956.             if (nout == ucbuflen) {
  15957.                 nout = 0;
  15958.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  15959.                 if (ret < 0)
  15960.                   return(ret);
  15961.             }
  15962.         }
  15963.         return(bsent);
  15964.     }
  15965. }
  15966.  
  15967. /* returns:
  15968.  *       0  on success
  15969.  *      -1  on error (errno set)
  15970.  *      -2  on security error
  15971.  */
  15972. static int
  15973. #ifdef CK_ANSIC
  15974. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  15975. #else
  15976. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  15977. #endif /* CK_ANSIC */
  15978. {
  15979.     static char *outbuf = NULL;         /* output ciphertext */
  15980. #ifdef FTP_SECURITY
  15981.     static unsigned int bufsize = 0;    /* size of outbuf */
  15982. #endif /* FTP_SECURITY */
  15983.     ftp_int32 length   = 0;
  15984.     ftp_uint32 net_len = 0;
  15985.  
  15986.     /* Other auth types go here ... */
  15987. #ifdef CK_SSL
  15988.     if (ssl_ftp_data_active_flag) {
  15989.         int count, error;
  15990.  
  15991.         /* there is no need to send an empty buffer when using SSL/TLS */
  15992.         if ( nbyte == 0 )
  15993.       return(0);
  15994.  
  15995.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  15996.         error = SSL_get_error(ssl_ftp_data_con,count);
  15997.         switch (error) {
  15998.           case SSL_ERROR_NONE:
  15999.             return(0);
  16000.           case SSL_ERROR_WANT_WRITE:
  16001.           case SSL_ERROR_WANT_READ:
  16002.           case SSL_ERROR_SYSCALL:
  16003. #ifdef NT
  16004.             {
  16005.                 int gle = GetLastError();
  16006.         if (gle == 0)
  16007.           return(0);
  16008.         debug(F111,"secure_putbuf","SSL_ERROR_SYSCALL",gle);
  16009.             }
  16010. #endif /* NT */
  16011.           case SSL_ERROR_WANT_X509_LOOKUP:
  16012.           case SSL_ERROR_SSL:
  16013.           case SSL_ERROR_ZERO_RETURN:
  16014.           default:
  16015.             SSL_shutdown(ssl_ftp_data_con);
  16016.             SSL_free(ssl_ftp_data_con);
  16017.             ssl_ftp_data_active_flag = 0;
  16018.             ssl_ftp_data_con = NULL;
  16019. #ifdef TCPIPLIB
  16020.             socket_close(data);
  16021. #else /* TCPIPLIB */
  16022. #ifdef USE_SHUTDOWN
  16023.             shutdown(data, 1+1);
  16024. #endif /* USE_SHUTDOWN */
  16025.             close(data);
  16026. #endif /* TCPIPLIB */
  16027.             data = -1;
  16028.             globaldin = data;
  16029.             return(-1);
  16030.         }
  16031.         return(-1);
  16032.     }
  16033. #endif /* CK_SSL */
  16034.  
  16035. #ifdef FTP_SRP
  16036.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  16037.         if (bufsize < nbyte + FUDGE_FACTOR) {
  16038.             if (outbuf?
  16039.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  16040.                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  16041.                 bufsize = nbyte + FUDGE_FACTOR;
  16042.             } else {
  16043.                 bufsize = 0;
  16044.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  16045.                 return(ERR);
  16046.             }
  16047.         }
  16048.         if ((length =
  16049.              srp_encode(ftp_dpl == FPL_PRV,
  16050.                         (CHAR *) buf,
  16051.                         (CHAR *) outbuf,
  16052.                         nbyte
  16053.                         )
  16054.              ) < 0) {
  16055.             secure_error ("srp_encode failed");
  16056.             return ERR;
  16057.         }
  16058.     }
  16059. #endif /* FTP_SRP */
  16060. #ifdef FTP_KRB4
  16061.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  16062.         struct sockaddr_in myaddr, hisaddr;
  16063.         GSOCKNAME_T len;
  16064.         len = sizeof(myaddr);
  16065.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  16066.             secure_error("secure_putbuf: getsockname failed");
  16067.             return(ERR);
  16068.         }
  16069.         len = sizeof(hisaddr);
  16070.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  16071.             secure_error("secure_putbuf: getpeername failed");
  16072.             return(ERR);
  16073.         }
  16074.         if (bufsize < nbyte + FUDGE_FACTOR) {
  16075.             if (outbuf ?
  16076.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  16077.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  16078.                 bufsize = nbyte + FUDGE_FACTOR;
  16079.             } else {
  16080.                 bufsize = 0;
  16081.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  16082.                 return(ERR);
  16083.             }
  16084.         }
  16085.         if (ftp_dpl == FPL_PRV) {
  16086.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  16087.                                  ftp_sched,
  16088. #ifdef KRB524
  16089.                                  ftp_cred.session,
  16090. #else /* KRB524 */
  16091.                                  &ftp_cred.session,
  16092. #endif /* KRB524 */
  16093.                                  &myaddr,
  16094.                                  &hisaddr
  16095.                                  );
  16096.         } else {
  16097.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  16098. #ifdef KRB524
  16099.                                  ftp_cred.session,
  16100. #else /* KRB524 */
  16101.                                  &ftp_cred.session,
  16102. #endif /* KRB524 */
  16103.                                  &myaddr,
  16104.                                  &hisaddr
  16105.                                  );
  16106.         }
  16107.         if (length == -1) {
  16108.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  16109.                          ftp_dpl == FPL_PRV ? "priv" : "safe");
  16110.             return(ERR);
  16111.         }
  16112.     }
  16113. #endif /* FTP_KRB4 */
  16114. #ifdef FTP_GSSAPI
  16115.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  16116.         gss_buffer_desc in_buf, out_buf;
  16117.         OM_uint32 maj_stat, min_stat;
  16118.         int conf_state;
  16119.  
  16120.         in_buf.value = buf;
  16121.         in_buf.length = nbyte;
  16122.         maj_stat = gss_seal(&min_stat, gcontext,
  16123.                             (ftp_dpl == FPL_PRV), /* confidential */
  16124.                             GSS_C_QOP_DEFAULT,
  16125.                             &in_buf,
  16126.                             &conf_state,
  16127.                             &out_buf
  16128.                             );
  16129.         if (maj_stat != GSS_S_COMPLETE) {
  16130.             /* generally need to deal */
  16131.             /* ie. should loop, but for now just fail */
  16132.             user_gss_error(maj_stat, min_stat,
  16133.                            ftp_dpl == FPL_PRV?
  16134.                            "GSSAPI seal failed":
  16135.                            "GSSAPI sign failed");
  16136.             return(ERR);
  16137.         }
  16138.         if (bufsize < out_buf.length) {
  16139.             if (outbuf ?
  16140.                 (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  16141.                 (outbuf = malloc((unsigned) out_buf.length))) {
  16142.                 bufsize = out_buf.length;
  16143.             } else {
  16144.                 bufsize = 0;
  16145.                 secure_error("%s (in malloc of PROT buffer)",
  16146.                              ck_errstr());
  16147.                 return(ERR);
  16148.             }
  16149.         }
  16150.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  16151.         gss_release_buffer(&min_stat, &out_buf);
  16152.     }
  16153. #endif /* FTP_GSSAPI */
  16154.     net_len = htonl((ULONG) length);
  16155.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  16156.       return(-1);
  16157.     if (looping_write(fd, outbuf, length) != length)
  16158.       return(-1);
  16159.     return(0);
  16160. }
  16161.  
  16162.  
  16163. /* fc = 0 means to get a byte; nonzero means to initialize buffer pointers */
  16164.  
  16165. static int
  16166. secure_getbyte(fd,fc) int fd,fc; {
  16167.     /* number of chars in ucbuf, pointer into ucbuf */
  16168.     static unsigned int nin = 0, bufp = 0;
  16169.     int kerror;
  16170.     ftp_uint32 length;
  16171.  
  16172.     if (fc) {
  16173.     nin = bufp = 0;
  16174.     ucbuf[0] = NUL;
  16175.     return(0);
  16176.     }
  16177.     if (nin == 0) {
  16178.         if (iscanceled())
  16179.           return(-9);
  16180.  
  16181. #ifdef FTP_TIMEOUT
  16182.     if (check_data_connection(fd,0) < 0)
  16183.       return(-3);
  16184. #endif    /* FTP_TIMEOUT */
  16185.  
  16186. #ifdef CK_SSL
  16187.         if (ssl_ftp_data_active_flag) {
  16188.             int count, error;
  16189.             count = SSL_read(ssl_ftp_data_con, ucbuf, ucbufsiz);
  16190.             error = SSL_get_error(ssl_ftp_data_con,count);
  16191. #ifdef DEBUG
  16192.         if (error != SSL_ERROR_NONE)
  16193.           debug(F101,"ftp secure_getbyte error","",error);
  16194.         if (count == 0)
  16195.           debug(F101,"ftp secure_getbyte count","",count);
  16196. #endif    /* DEBUG */
  16197.             switch (error) {
  16198.               case SSL_ERROR_NONE:
  16199.         if (count > 0) {
  16200.             nin = bufp = count;
  16201.             rpackets++;
  16202.             pktnum++;
  16203.             if (fdispla != XYFD_B) {
  16204.             rpktl = count;
  16205.             ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  16206.             }
  16207.             break;
  16208.         }
  16209.               case SSL_ERROR_WANT_WRITE:
  16210.               case SSL_ERROR_WANT_READ:
  16211.               case SSL_ERROR_SYSCALL:
  16212. #ifdef NT
  16213.                 {
  16214.                     int gle = GetLastError();
  16215.                 }
  16216. #endif /* NT */
  16217.               case SSL_ERROR_WANT_X509_LOOKUP:
  16218.               case SSL_ERROR_SSL:
  16219.               case SSL_ERROR_ZERO_RETURN:
  16220.               default:
  16221.                 nin = bufp = count = 0;
  16222.                 SSL_shutdown(ssl_ftp_data_con);
  16223.                 SSL_free(ssl_ftp_data_con);
  16224.                 ssl_ftp_data_active_flag = 0;
  16225.                 ssl_ftp_data_con = NULL;
  16226. #ifdef TCPIPLIB
  16227.                 socket_close(data);
  16228. #else /* TCPIPLIB */
  16229. #ifdef USE_SHUTDOWN
  16230.                 shutdown(data, 1+1);
  16231. #endif /* USE_SHUTDOWN */
  16232.                 close(data);
  16233. #endif /* TCPIPLIB */
  16234.                 data = -1;
  16235.                 globaldin = data;
  16236.                 break;
  16237.             }
  16238.         } else
  16239. #endif /* CK_SSL */
  16240.           {
  16241.               kerror = looping_read(fd, (char *)&length, sizeof(length));
  16242.               if (kerror != sizeof(length)) {
  16243.                   secure_error("Couldn't read PROT buffer length: %d/%s",
  16244.                                kerror,
  16245.                                kerror == -1 ? ck_errstr()
  16246.                                : "premature EOF"
  16247.                                );
  16248.                   return(ERR);
  16249.               }
  16250.               debug(F101,"secure_getbyte length","",length);
  16251.               debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  16252.  
  16253.               length = (ULONG) ntohl(length);
  16254.               if (length > maxbuf) {
  16255.                   secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  16256.                                length,
  16257.                                maxbuf
  16258.                                );
  16259.                   return(ERR);
  16260.               }
  16261.               if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  16262.                   secure_error("Couldn't read %u byte PROT buffer: %s",
  16263.                                length,
  16264.                                kerror == -1 ? ck_errstr() : "premature EOF"
  16265.                                );
  16266.                   return(ERR);
  16267.               }
  16268.  
  16269.               /* Other auth types go here ... */
  16270. #ifdef FTP_SRP
  16271.               if (strcmp(auth_type, "SRP") == 0) {
  16272.                   if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  16273.                                                 (CHAR *) ucbuf,
  16274.                                                 ucbuf,
  16275.                                                 length
  16276.                                                 )
  16277.                        ) == -1) {
  16278.                       secure_error ("srp_encode failed" );
  16279.                       return ERR;
  16280.                   }
  16281.               }
  16282. #endif /* FTP_SRP */
  16283. #ifdef FTP_KRB4
  16284.               if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  16285.                   struct sockaddr_in myaddr, hisaddr;
  16286.                   GSOCKNAME_T len;
  16287.                   len = sizeof(myaddr);
  16288.                   if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  16289.                       secure_error("secure_putbuf: getsockname failed");
  16290.                       return(ERR);
  16291.                   }
  16292.                   len = sizeof(hisaddr);
  16293.                   if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  16294.                       secure_error("secure_putbuf: getpeername failed");
  16295.                       return(ERR);
  16296.                   }
  16297.                   if (ftp_dpl) {
  16298.                       kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  16299. #ifdef KRB524
  16300.                                            ftp_cred.session,
  16301. #else /* KRB524 */
  16302.                                            &ftp_cred.session,
  16303. #endif /* KRB524 */
  16304.                                            &hisaddr, &myaddr, &ftp_msg_data);
  16305.                   } else {
  16306.                       kerror = krb_rd_safe(ucbuf, length,
  16307. #ifdef KRB524
  16308.                                            ftp_cred.session,
  16309. #else /* KRB524 */
  16310.                                            &ftp_cred.session,
  16311. #endif /* KRB524 */
  16312.                                            &hisaddr, &myaddr, &ftp_msg_data);
  16313.                   }
  16314.                   if (kerror) {
  16315.                       secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  16316.                                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  16317.                                    krb_get_err_text(kerror));
  16318.                       return(ERR);
  16319.                   }
  16320.                   memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  16321.                   nin = bufp = ftp_msg_data.app_length;
  16322.               }
  16323. #endif /* FTP_KRB4 */
  16324. #ifdef FTP_GSSAPI
  16325.               if (strcmp(auth_type, "GSSAPI") == 0) {
  16326.                   gss_buffer_desc xmit_buf, msg_buf;
  16327.                   OM_uint32 maj_stat, min_stat;
  16328.                   int conf_state;
  16329.  
  16330.                   xmit_buf.value = ucbuf;
  16331.                   xmit_buf.length = length;
  16332.                   conf_state = (ftp_dpl == FPL_PRV);
  16333.                   /* decrypt/verify the message */
  16334.                   maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  16335.                                         &msg_buf, &conf_state, NULL);
  16336.                   if (maj_stat != GSS_S_COMPLETE) {
  16337.                       user_gss_error(maj_stat, min_stat,
  16338.                                      (ftp_dpl == FPL_PRV)?
  16339.                                      "failed unsealing ENC message":
  16340.                                      "failed unsealing MIC message");
  16341.                       return ERR;
  16342.                   }
  16343.                   memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  16344.                   gss_release_buffer(&min_stat, &msg_buf);
  16345.               }
  16346. #endif /* FTP_GSSAPI */
  16347.               /* Other auth types go here ... */
  16348.  
  16349.               /* Update file transfer display */
  16350.               rpackets++;
  16351.               pktnum++;
  16352.               if (fdispla != XYFD_B) {
  16353.                   rpktl = nin;
  16354.                   ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  16355.               }
  16356.           }
  16357.     }
  16358.     if (nin == 0)
  16359.       return(EOF);
  16360.     else
  16361.       return(ucbuf[bufp - nin--]);
  16362. }
  16363.  
  16364. /* secure_getc(fd,fc)
  16365.  * Call with:
  16366.  *   fd = file descriptor for connection.
  16367.  *   fc = 0 to get a character, fc != 0 to initialize buffer pointers.
  16368.  * Returns:
  16369.  *   c>=0 on success (character value)
  16370.  *   -1   on EOF
  16371.  *   -2   on security error
  16372.  *   -3   on timeout (if built with FTP_TIMEOUT defined)
  16373.  */
  16374. static int
  16375. secure_getc(fd,fc) int fd,fc; {        /* file descriptor, function code */
  16376.  
  16377.     if (!ftpissecure()) {
  16378.         static unsigned int nin = 0, bufp = 0;
  16379.     if (fc) {
  16380.         nin = bufp = 0;
  16381.         ucbuf[0] = NUL;
  16382.         return(0);
  16383.     }
  16384.         if (nin == 0) {
  16385.             if (iscanceled())
  16386.               return(-9);
  16387.  
  16388. #ifdef FTP_TIMEOUT
  16389.         if (check_data_connection(fd,0) < 0) {
  16390.                 debug(F100,"secure_getc TIMEOUT","",0);
  16391.                 nin = bufp = 0;
  16392.         ftp_timed_out = 1;
  16393.         return(-3);
  16394.         }        
  16395. #endif    /* FTP_TIMEOUT */
  16396.  
  16397.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  16398.             if ((nin == 0) || (nin == (unsigned int)-1)) {
  16399.                 debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  16400.                 debug(F101,"secure_getc returns EOF","",EOF);
  16401.                 nin = bufp = 0;
  16402.                 return(EOF);
  16403.             }
  16404.             debug(F101,"ftp secure_getc recv","",nin);
  16405.             hexdump("ftp secure_getc recv",ucbuf,16);
  16406.             rpackets++;
  16407.             pktnum++;
  16408.             if (fdispla != XYFD_B) {
  16409.                 rpktl = nin;
  16410.                 ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  16411.             }
  16412.         }
  16413.         return(ucbuf[bufp - nin--]);
  16414.     } else
  16415.       return(secure_getbyte(fd,fc));
  16416. }
  16417.  
  16418. /* returns:
  16419.  *     n>0  on success (n == # of bytes read)
  16420.  *       0  on EOF
  16421.  *      -1  on error (errno set), only for FPL_CLR
  16422.  *      -2  on security error
  16423.  */
  16424. static int
  16425. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  16426.     static int c = 0;
  16427.     int i;
  16428.  
  16429.     debug(F101,"secure_read bytes requested","",nbyte);
  16430.     if (c == EOF)
  16431.       return(c = 0);
  16432.     for (i = 0; nbyte > 0; nbyte--) {
  16433.         c = secure_getc(fd,0);
  16434.         switch (c) {
  16435.           case -9:                      /* Canceled from keyboard */
  16436.             debug(F101,"ftp secure_read interrupted","",c);
  16437.             return(0);
  16438.           case ERR:
  16439.             debug(F101,"ftp secure_read error","",c);
  16440.             return(c);
  16441.           case EOF:
  16442.             debug(F101,"ftp secure_read EOF","",c);
  16443.             if (!i)
  16444.               c = 0;
  16445.             return(i);
  16446. #ifdef FTP_TIMEOUT
  16447.           case -3:
  16448.             debug(F101,"ftp secure_read timeout","",c);
  16449.         return(c);
  16450. #endif    /* FTP_TIMEOUT */
  16451.           default:
  16452.             buf[i++] = c;
  16453.         }
  16454.     }
  16455.     return(i);
  16456. }
  16457.  
  16458. #ifdef USE_RUSERPASS
  16459. /* BEGIN_RUSERPASS
  16460.  *
  16461.  * Copyright (c) 1985 Regents of the University of California.
  16462.  * All rights reserved.
  16463.  *
  16464.  * Redistribution and use in source and binary forms, with or without
  16465.  * modification, are permitted provided that the following conditions
  16466.  * are met:
  16467.  * 1. Redistributions of source code must retain the above copyright
  16468.  *    notice, this list of conditions and the following disclaimer.
  16469.  * 2. Redistributions in binary form must reproduce the above copyright
  16470.  *    notice, this list of conditions and the following disclaimer in the
  16471.  *    documentation and/or other materials provided with the distribution.
  16472.  * 3. All advertising materials mentioning features or use of this software
  16473.  *    must display the following acknowledgement:
  16474.  *      This product includes software developed by the University of
  16475.  *      California, Berkeley and its contributors.
  16476.  * 4. Neither the name of the University nor the names of its contributors
  16477.  *    may be used to endorse or promote products derived from this software
  16478.  *    without specific prior written permission.
  16479.  *
  16480.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16481.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16482.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16483.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  16484.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16485.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  16486.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16487.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16488.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16489.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16490.  * SUCH DAMAGE.
  16491.  */
  16492.  
  16493. #ifndef lint
  16494. static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
  16495. #endif /* not lint */
  16496.  
  16497. #ifndef MAXHOSTNAMELEN
  16498. #define MAXHOSTNAMELEN 64
  16499. #endif
  16500.  
  16501. char * renvlook();
  16502. static FILE * cfile;
  16503.  
  16504. #define DEFAULT 1
  16505. #define LOGIN   2
  16506. #define PASSWD  3
  16507. #define ACCOUNT 4
  16508. #define MACDEF  5
  16509. #define ID      10
  16510. #define MACH    11
  16511.  
  16512. static char tokval[100];
  16513.  
  16514. static struct toktab {
  16515.     char *tokstr;
  16516.     int tval;
  16517. } toktab[]= {
  16518.     "default",  DEFAULT,
  16519.     "login",    LOGIN,
  16520.     "password", PASSWD,
  16521.     "passwd",   PASSWD,
  16522.     "account",  ACCOUNT,
  16523.     "machine",  MACH,
  16524.     "macdef",   MACDEF,
  16525.     0,          0
  16526. };
  16527.  
  16528. static int
  16529. token() {
  16530.     char *cp;
  16531.     int c;
  16532.     struct toktab *t;
  16533.  
  16534.     if (feof(cfile))
  16535.       return(0);
  16536.     while ((c = getc(cfile)) != EOF &&
  16537.            (c == '\n' || c == '\t' || c == ' ' || c == ','))
  16538.       continue;
  16539.     if (c == EOF)
  16540.       return(0);
  16541.     cp = tokval;
  16542.     if (c == '"') {
  16543.         while ((c = getc(cfile)) != EOF && c != '"') {
  16544.             if (c == '\\')
  16545.               c = getc(cfile);
  16546.             *cp++ = c;
  16547.         }
  16548.     } else {
  16549.         *cp++ = c;
  16550.         while ((c = getc(cfile)) != EOF
  16551.                && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  16552.             if (c == '\\')
  16553.               c = getc(cfile);
  16554.             *cp++ = c;
  16555.         }
  16556.     }
  16557.     *cp = 0;
  16558.     if (tokval[0] == 0)
  16559.       return(0);
  16560.     for (t = toktab; t->tokstr; t++)
  16561.       if (!strcmp(t->tokstr, tokval))
  16562.         return(t->tval);
  16563.     return(ID);
  16564. }
  16565.  
  16566. ruserpass(host, aname, apass, aacct)
  16567.     char *host, **aname, **apass, **aacct;
  16568. {
  16569.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  16570.     char myname[MAXHOSTNAMELEN], *mydomain;
  16571.     int t, i, c, usedefault = 0;
  16572. #ifdef NT
  16573.     struct _stat stb;
  16574. #else /* NT */
  16575.     struct stat stb;
  16576. #endif /* NT */
  16577.  
  16578.     hdir = getenv("HOME");
  16579.     if (hdir == NULL)
  16580.         hdir = ".";
  16581.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  16582.     cfile = fopen(buf, "r");
  16583.     if (cfile == NULL) {
  16584.         if (errno != ENOENT)
  16585.           perror(buf);
  16586.         return(0);
  16587.     }
  16588.     if (gethostname(myname, MAXHOSTNAMELEN) < 0)
  16589.       myname[0] = '\0';
  16590.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  16591.       mydomain = "";
  16592.  
  16593.   next:
  16594.     while ((t = token())) switch(t) {
  16595.  
  16596.       case DEFAULT:
  16597.         usedefault = 1;
  16598.         /* FALL THROUGH */
  16599.  
  16600.       case MACH:
  16601.         if (!usedefault) {
  16602.             if (token() != ID)
  16603.               continue;
  16604.             /*
  16605.              * Allow match either for user's input host name
  16606.              * or official hostname.  Also allow match of
  16607.              * incompletely-specified host in local domain.
  16608.              */
  16609.             if (ckstrcmp(host, tokval,-1,1) == 0)
  16610.               goto match;
  16611.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  16612.               goto match;
  16613.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  16614.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16615.                 ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  16616.                 tokval[tmp - ftp_host] == '\0')
  16617.               goto match;
  16618.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  16619.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16620.                 ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  16621.                 tokval[tmp - host] == '\0')
  16622.               goto match;
  16623.             continue;
  16624.         }
  16625.  
  16626.       match:
  16627.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  16628.  
  16629.           case LOGIN:
  16630.             if (token())
  16631.               if (*aname == 0) {
  16632.                   *aname = malloc((unsigned) strlen(tokval) + 1);
  16633.                   strcpy(*aname, tokval);      /* safe */
  16634.               } else {
  16635.                   if (strcmp(*aname, tokval))
  16636.                     goto next;
  16637.               }
  16638.             break;
  16639.           case PASSWD:
  16640.             if (strcmp(*aname, "anonymous") &&
  16641.                 fstat(fileno(cfile), &stb) >= 0 &&
  16642.                 (stb.st_mode & 077) != 0) {
  16643.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16644.                 fprintf(stderr, "Remove password or correct mode.\n");
  16645.                 goto bad;
  16646.             }
  16647.             if (token() && *apass == 0) {
  16648.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  16649.                 strcpy(*apass, tokval);          /* safe */
  16650.             }
  16651.             break;
  16652.           case ACCOUNT:
  16653.             if (fstat(fileno(cfile), &stb) >= 0
  16654.                 && (stb.st_mode & 077) != 0) {
  16655.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16656.                 fprintf(stderr, "Remove account or correct mode.\n");
  16657.                 goto bad;
  16658.             }
  16659.             if (token() && *aacct == 0) {
  16660.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  16661.                 strcpy(*aacct, tokval);          /* safe */
  16662.             }
  16663.             break;
  16664.  
  16665.           default:
  16666.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  16667.             break;
  16668.         }
  16669.         goto done;
  16670.     }
  16671.  
  16672.   done:
  16673.     fclose(cfile);
  16674.     return(0);
  16675.  
  16676.   bad:
  16677.     fclose(cfile);
  16678.     return(-1);
  16679. }
  16680. #endif /* USE_RUSERPASS */
  16681.  
  16682. static char *radixN =
  16683.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  16684.  
  16685. static char pad = '=';
  16686.  
  16687. static int
  16688. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  16689.     CHAR inbuf[], outbuf[];
  16690.     int inlen, *outlen, decode;
  16691. {
  16692.     int i, j, D = 0;
  16693.     char *p;
  16694.     CHAR c = NUL;
  16695.  
  16696.     if (decode) {
  16697.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  16698.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  16699.               return(1);
  16700.             D = p - radixN;
  16701.             switch (i&3) {
  16702.               case 0:
  16703.                 outbuf[j] = D<<2;
  16704.                 break;
  16705.               case 1:
  16706.                 outbuf[j++] |= D>>4;
  16707.                 outbuf[j] = (D&15)<<4;
  16708.                 break;
  16709.               case 2:
  16710.                 outbuf[j++] |= D>>2;
  16711.                 outbuf[j] = (D&3)<<6;
  16712.                 break;
  16713.               case 3:
  16714.                 outbuf[j++] |= D;
  16715.             }
  16716.             if (j == *outlen)
  16717.               return(4);
  16718.         }
  16719.         switch (i&3) {
  16720.           case 1: return(3);
  16721.           case 2: if (D&15) return(3);
  16722.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  16723.             break;
  16724.           case 3: if (D&3) return(3);
  16725.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  16726.         }
  16727.         *outlen = j;
  16728.     } else {
  16729.         for (i = 0, j = 0; i < inlen; i++) {
  16730.             switch (i%3) {
  16731.               case 0:
  16732.                 outbuf[j++] = radixN[inbuf[i]>>2];
  16733.                 c = (inbuf[i]&3)<<4;
  16734.                 break;
  16735.               case 1:
  16736.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  16737.                 c = (inbuf[i]&15)<<2;
  16738.                 break;
  16739.               case 2:
  16740.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  16741.                 outbuf[j++] = radixN[inbuf[i]&63];
  16742.                 c = 0;
  16743.             }
  16744.             if (j == *outlen)
  16745.               return(4);
  16746.         }
  16747.         if (i%3) outbuf[j++] = radixN[c];
  16748.         switch (i%3) {
  16749.           case 1: outbuf[j++] = pad;
  16750.           case 2: outbuf[j++] = pad;
  16751.         }
  16752.         outbuf[*outlen = j] = '\0';
  16753.     }
  16754.     return(0);
  16755. }
  16756.  
  16757. static char *
  16758. radix_error(e) int e;
  16759. {
  16760.     switch (e) {
  16761.       case 0:  return("Success");
  16762.       case 1:  return("Bad character in encoding");
  16763.       case 2:  return("Encoding not properly padded");
  16764.       case 3:  return("Decoded # of bits not a multiple of 8");
  16765.       case 4:  return("Output buffer too small");
  16766.       default: return("Unknown error");
  16767.     }
  16768. }
  16769. /* END_RUSERPASS */
  16770.  
  16771. #ifdef FTP_SRP
  16772. /*---------------------------------------------------------------------------+
  16773.  |                                                                           |
  16774.  |   Package: srpftp                                                         |
  16775.  |   Author: Eugene Jhong                                                    |
  16776.  |                                                                           |
  16777.  +---------------------------------------------------------------------------*/
  16778.  
  16779. /*
  16780.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  16781.  * All Rights Reserved.
  16782.  *
  16783.  * Permission is hereby granted, free of charge, to any person obtaining
  16784.  * a copy of this software and associated documentation files (the
  16785.  * "Software"), to deal in the Software without restriction, including
  16786.  * without limitation the rights to use, copy, modify, merge, publish,
  16787.  * distribute, sublicense, and/or sell copies of the Software, and to
  16788.  * permit persons to whom the Software is furnished to do so, subject to
  16789.  * the following conditions:
  16790.  *
  16791.  * The above copyright notice and this permission notice shall be
  16792.  * included in all copies or substantial portions of the Software.
  16793.  *
  16794.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  16795.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16796.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16797.  *
  16798.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  16799.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16800.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  16801.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  16802.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16803.  *
  16804.  * In addition, the following conditions apply:
  16805.  *
  16806.  * 1. Any software that incorporates the SRP authentication technology
  16807.  *    must display the following acknowlegment:
  16808.  *    "This product uses the 'Secure Remote Password' cryptographic
  16809.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  16810.  *
  16811.  * 2. Any software that incorporates all or part of the SRP distribution
  16812.  *    itself must also display the following acknowledgment:
  16813.  *    "This product includes software developed by Tom Wu and Eugene
  16814.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  16815.  *
  16816.  * 3. Redistributions in source or binary form must retain an intact copy
  16817.  *    of this copyright notice and list of conditions.
  16818.  */
  16819.  
  16820. #define SRP_PROT_VERSION        1
  16821.  
  16822. #ifdef CK_ENCRYPTION
  16823. #define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
  16824. #else
  16825. #define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
  16826. #endif /* CK_ENCRYPTION */
  16827.  
  16828. #define SRP_DEFAULT_HASH        HASH_ID_SHA
  16829.  
  16830. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  16831. CHAR srp_pref_hash = HASH_ID_SHA;
  16832.  
  16833. static struct t_client *tc = NULL;
  16834. static CHAR *skey = NULL;
  16835. static krypto_context *incrypt = NULL;
  16836. static krypto_context *outcrypt = NULL;
  16837.  
  16838. typedef unsigned int srp_uint32;
  16839.  
  16840. /*--------------------------------------------------------------+
  16841.  | srp_selcipher: select cipher                                 |
  16842.  +--------------------------------------------------------------*/
  16843. static int
  16844. srp_selcipher (cname) char *cname; {
  16845.     cipher_desc *cd;
  16846.  
  16847.     if (!(cd = cipher_getdescbyname (cname))) {
  16848.         int i;
  16849.         CHAR *list = cipher_getlist ();
  16850.  
  16851.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  16852.         for (i = 0; i < strlen (list); i++)
  16853.           fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  16854.         fprintf (stderr, "\n");
  16855.         return -1;
  16856.     }
  16857.     srp_pref_cipher = cd->id;
  16858.     return 0;
  16859. }
  16860.  
  16861. /*--------------------------------------------------------------+
  16862.  | srp_selhash: select hash                                     |
  16863.  +--------------------------------------------------------------*/
  16864. static int
  16865. srp_selhash (hname) char *hname; {
  16866.     hash_desc *hd;
  16867.  
  16868.     if (!(hd = hash_getdescbyname (hname))) {
  16869.         int i;
  16870.         CHAR *list = hash_getlist ();
  16871.  
  16872.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  16873.         for (i = 0; i < strlen (list); i++)
  16874.           fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  16875.         fprintf (stderr, "\n");
  16876.         return -1;
  16877.     }
  16878.     srp_pref_hash = hd->id;
  16879.     return 0;
  16880. }
  16881.  
  16882. /*--------------------------------------------------------------+
  16883.  | srp_userpass: get username and password                      |
  16884.  +--------------------------------------------------------------*/
  16885. static int
  16886. srp_userpass (host) char *host; {
  16887.     char tmp[BUFSIZ], prompt[PROMPTSIZ];
  16888.     char *user;
  16889.  
  16890.     user = NULL;
  16891. #ifdef USE_RUSERPASS
  16892.     ruserpass (host, &user, &srp_pass, &srp_acct);
  16893. #endif /* USE_RUSERPASS */
  16894.  
  16895.     while (user == NULL)     {
  16896.         char *myname;
  16897.         int ok;
  16898.  
  16899.         myname = whoami();
  16900.         if (!myname) myname = "";
  16901.         if (myname[0])
  16902.           ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  16903.                     NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  16904.         else
  16905.           ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  16906.         tmp[0] = '\0';
  16907.         ok = uq_txt(NULL,prompt,1,NULL,tmp,BUFSIZ,NULL,
  16908.                     DEFAULT_UQ_TIMEOUT);
  16909.         if (!ok || *tmp == '\0')
  16910.           user = myname;
  16911.         else
  16912.           user = brstrip(tmp);
  16913.     }
  16914.     ckstrncpy (srp_user, user,BUFSIZ);
  16915.     return(0);
  16916. }
  16917.  
  16918. /*--------------------------------------------------------------+
  16919.  | srp_reset: reset srp information                             |
  16920.  +--------------------------------------------------------------*/
  16921. static int
  16922. srp_reset () {
  16923.     if (tc) { t_clientclose (tc); tc = NULL; }
  16924.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  16925.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  16926.     return(0);
  16927. }
  16928.  
  16929. /*--------------------------------------------------------------+
  16930.  | srp_ftp_auth: perform srp authentication                         |
  16931.  +--------------------------------------------------------------*/
  16932. static int
  16933. srp_ftp_auth(host, user, pass)
  16934.     char *host;
  16935.     char *user;
  16936.     char *pass;
  16937. {
  16938.     struct t_num *wp;
  16939.     struct t_num N;
  16940.     struct t_num g;
  16941.     struct t_num s;
  16942.     struct t_num yp;
  16943.     CHAR buf[FTP_BUFSIZ];
  16944.     CHAR tmp[FTP_BUFSIZ];
  16945.     CHAR *bp, *cp;
  16946.     int n, e, clen, blen, len, i;
  16947.     CHAR cid = 0;
  16948.     CHAR hid = 0;
  16949.  
  16950.     srp_pass = srp_acct = 0;
  16951.  
  16952.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  16953.     if (n != REPLY_CONTINUE) {
  16954.         if (ftp_deb)
  16955.             fprintf(stderr, "SRP rejected as an authentication type\n");
  16956.         return(0);
  16957.     } else {                            /* Send protocol version */
  16958.         CHAR vers[4];
  16959.         memset (vers, 0, 4);
  16960.         vers[3] = SRP_PROT_VERSION;
  16961.         if (!quiet)
  16962.           printf ("SRP accepted as authentication type.\n");
  16963.         bp = tmp; blen = 0;
  16964.         srp_put (vers, &bp, 4, &blen);
  16965.         len = FTP_BUFSIZ;
  16966.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16967.           goto encode_error;
  16968.         reply_parse = "ADAT=";
  16969.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16970.     }
  16971.     if (n == REPLY_CONTINUE) {          /* Get protocol version */
  16972.         bp = buf;
  16973.         if (!reply_parse)
  16974.           goto data_error;
  16975.         blen = FTP_BUFSIZ;
  16976.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  16977.           goto decode_error;
  16978.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16979.           goto data_error;
  16980.  
  16981.         if (host) {                     /* Get username/password if needed */
  16982.             srp_userpass (host);
  16983.         } else {
  16984.             ckstrncpy (srp_user, user, BUFSIZ);
  16985.             srp_pass = pass;
  16986.         }
  16987.         bp = tmp; blen = 0;             /* Send username */
  16988.         srp_put (srp_user, &bp, strlen (srp_user), &blen);
  16989.         len = FTP_BUFSIZ;
  16990.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16991.           goto encode_error;
  16992.         reply_parse = "ADAT=";
  16993.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16994.     }
  16995.     if (n == REPLY_CONTINUE) {          /* Get N, g and s */
  16996.         bp = buf;
  16997.         if (!reply_parse)
  16998.           goto data_error;
  16999.         blen = FTP_BUFSIZ;
  17000.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  17001.           goto decode_error;
  17002.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  17003.           goto data_error;
  17004.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  17005.           goto data_error;
  17006.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  17007.           goto data_error;
  17008.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  17009.             fprintf (stderr, "Unable to open SRP client structure.\n");
  17010.             goto bad;
  17011.         }
  17012.         wp = t_clientgenexp (tc);       /* Send wp */
  17013.         bp = tmp; blen = 0;
  17014.         srp_put (wp->data, &bp, wp->len, &blen);
  17015.         len = FTP_BUFSIZ;
  17016.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  17017.           goto encode_error;
  17018.         reply_parse = "ADAT=";
  17019.         n = ftpcmd("ADAT",buf,-1,-1,0);
  17020.     }
  17021.     if (n == REPLY_CONTINUE) {          /* Get yp */
  17022.         bp = buf;
  17023.         if (!reply_parse)
  17024.           goto data_error;
  17025.         blen = FTP_BUFSIZ;
  17026.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  17027.           goto decode_error;
  17028.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  17029.           goto data_error;
  17030.         if (!srp_pass) {
  17031.             static char ftppass[PASSBUFSIZ];
  17032.             int ok;
  17033.             setint();
  17034.             ok = uq_txt(NULL," SRP Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
  17035.                         DEFAULT_UQ_TIMEOUT);
  17036.             if (ok)
  17037.           srp_pass = brstrip(ftppass);
  17038.         }
  17039.         t_clientpasswd (tc, srp_pass);
  17040.         memset (srp_pass, 0, strlen (srp_pass));
  17041.         skey = t_clientgetkey (tc, &yp); /* Send response */
  17042.         bp = tmp; blen = 0;
  17043.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  17044.         len = FTP_BUFSIZ;
  17045.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  17046.           goto encode_error;
  17047.         reply_parse = "ADAT=";
  17048.         n = ftpcmd("ADAT",buf,-1,-1,0);
  17049.     }
  17050.     if (n == REPLY_CONTINUE) {          /* Get response */
  17051.         bp = buf;
  17052.         if (!reply_parse)
  17053.           goto data_error;
  17054.         blen = FTP_BUFSIZ;
  17055.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  17056.           goto encode_error;
  17057.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  17058.           goto data_error;
  17059.         if (t_clientverify (tc, cp)) {
  17060.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  17061.             goto bad;
  17062.         }
  17063.         bp = tmp; blen = 0;             /* Send nothing */
  17064.         srp_put ("\0", &bp, 1, &blen);
  17065.         len = FTP_BUFSIZ;
  17066.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  17067.           goto encode_error;
  17068.         reply_parse = "ADAT=";
  17069.         n = ftpcmd("ADAT",buf,-1,-1,0);
  17070.     }
  17071.     if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
  17072.         CHAR seqnum[4];
  17073.         CHAR *clist;
  17074.         CHAR *hlist;
  17075.         CHAR *p1;
  17076.         int clist_len, hlist_len;
  17077.         bp = buf;
  17078.         if (!reply_parse)
  17079.           goto data_error;
  17080.         blen = FTP_BUFSIZ;
  17081.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  17082.           goto encode_error;
  17083.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  17084.           goto data_error;
  17085.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  17086.           goto data_error;
  17087.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  17088.           goto data_error;
  17089.         memcpy (seqnum, cp, 4);
  17090.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  17091.           cid = srp_pref_cipher;
  17092.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  17093.           cid = SRP_DEFAULT_CIPHER;
  17094.         if (!cid) {
  17095.             CHAR *loclist = cipher_getlist ();
  17096.             for (i = 0; i < strlen (loclist); i++)
  17097.               if (cipher_supported (clist, loclist[i])) {
  17098.                   cid = loclist[i];
  17099.                   break;
  17100.               }
  17101.         }
  17102.         if (!cid) {
  17103.             fprintf (stderr, "Unable to agree on cipher.\n");
  17104.             goto bad;
  17105.         }
  17106.         /* Choose hash */
  17107.  
  17108.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  17109.           hid = srp_pref_hash;
  17110.  
  17111.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  17112.           hid = SRP_DEFAULT_HASH;
  17113.  
  17114.         if (!hid) {
  17115.             CHAR *loclist = hash_getlist ();
  17116.             for (i = 0; i < strlen (loclist); i++)
  17117.               if (hash_supported (hlist, loclist[i])) {
  17118.                   hid = loclist[i];
  17119.                   break;
  17120.               }
  17121.         }
  17122.         if (!hid) {
  17123.             fprintf (stderr, "Unable to agree on hash.\n");
  17124.             goto bad;
  17125.         }
  17126.         /* Set incrypt */
  17127.  
  17128.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  17129.                                     KRYPTO_DECODE)))
  17130.           goto bad;
  17131.  
  17132.         /* Generate random number for outkey and outseqnum */
  17133.  
  17134.         t_random (seqnum, 4);
  17135.  
  17136.         /* Send cid, hid, outkey, outseqnum */
  17137.  
  17138.         bp = tmp; blen = 0;
  17139.         srp_put (&cid, &bp, 1, &blen);
  17140.         srp_put (&hid, &bp, 1, &blen);
  17141.         srp_put (seqnum, &bp, 4, &blen);
  17142.         len = FTP_BUFSIZ;
  17143.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  17144.           goto encode_error;
  17145.         reply_parse = "ADAT=";
  17146.         n = ftpcmd("ADAT",buf,-1,-1,0);
  17147.  
  17148.         /* Set outcrypt */
  17149.  
  17150.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  17151.                                      KRYPTO_ENCODE)))
  17152.           goto bad;
  17153.  
  17154.         t_clientclose (tc);
  17155.         tc = NULL;
  17156.     }
  17157.     if (n != REPLY_COMPLETE)
  17158.       goto bad;
  17159.  
  17160.     if (ftp_vbm) {
  17161.         if (ftp_deb)
  17162.           printf("\n");
  17163.         printf ("SRP authentication succeeded.\n");
  17164.         printf ("Using cipher %s and hash function %s.\n",
  17165.                 (cipher_getdescbyid(cid))->name,
  17166.                 (hash_getdescbyid(hid))->name
  17167.                 );
  17168.     }
  17169.     reply_parse = NULL;
  17170.     auth_type = "SRP";
  17171.     return(1);
  17172.  
  17173.   encode_error:
  17174.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  17175.     goto bad;
  17176.  
  17177.   decode_error:
  17178.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  17179.     goto bad;
  17180.  
  17181.   data_error:
  17182.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  17183.     goto bad;
  17184.  
  17185.   bad:
  17186.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  17187.     reply_parse = NULL;
  17188.     return(0);
  17189. }
  17190.  
  17191. /*--------------------------------------------------------------+
  17192.  | srp_put: put item to send buffer                             |
  17193.  +--------------------------------------------------------------*/
  17194. static int
  17195. srp_put (in, out, inlen, outlen)
  17196.     CHAR *in;
  17197.     CHAR **out;
  17198.     int inlen;
  17199.     int *outlen;
  17200. {
  17201.     srp_uint32 net_len;
  17202.  
  17203.     net_len = htonl (inlen);
  17204.     memcpy (*out, &net_len, 4);
  17205.  
  17206.     *out += 4; *outlen += 4;
  17207.  
  17208.     memcpy (*out, in, inlen);
  17209.  
  17210.     *out += inlen; *outlen += inlen;
  17211.     return(0);
  17212. }
  17213.  
  17214. /*--------------------------------------------------------------+
  17215.  | srp_get: get item from receive buffer                        |
  17216.  +--------------------------------------------------------------*/
  17217. static int
  17218. srp_get (in, out, inlen, outlen)
  17219.     CHAR **in;
  17220.     CHAR **out;
  17221.     int *inlen;
  17222.     int *outlen;
  17223. {
  17224.     srp_uint32 net_len;
  17225.  
  17226.     if (*inlen < 4) return -1;
  17227.  
  17228.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  17229.     *outlen = ntohl (net_len);
  17230.  
  17231.     if (*inlen < *outlen) return -1;
  17232.  
  17233.     *out = *in; *inlen -= *outlen; *in += *outlen;
  17234.  
  17235.     return *outlen;
  17236. }
  17237.  
  17238. /*--------------------------------------------------------------+
  17239.  | srp_encode: encode control message                           |
  17240.  +--------------------------------------------------------------*/
  17241. static int
  17242. srp_encode (private, in, out, len)
  17243.     int private;
  17244.     CHAR *in;
  17245.     CHAR *out;
  17246.     unsigned len;
  17247. {
  17248.     if (private)
  17249.       return krypto_msg_priv (outcrypt, in, out, len);
  17250.     else
  17251.       return krypto_msg_safe (outcrypt, in, out, len);
  17252. }
  17253.  
  17254. /*--------------------------------------------------------------+
  17255.  | srp_decode: decode control message                           |
  17256.  +--------------------------------------------------------------*/
  17257. static int
  17258. srp_decode (private, in, out, len)
  17259.     int private;
  17260.     CHAR *in;
  17261.     CHAR *out;
  17262.     unsigned len;
  17263. {
  17264.     if (private)
  17265.       return krypto_msg_priv (incrypt, in, out, len);
  17266.     else
  17267.       return krypto_msg_safe (incrypt, in, out, len);
  17268. }
  17269.  
  17270. #endif /* FTP_SRP */
  17271.  
  17272.  
  17273.  
  17274. #ifdef NOT_USED
  17275. /*
  17276.   The following code is from the Unix FTP client.  Be sure to
  17277.   make sure that the functionality is not lost.  Especially
  17278.   the Proxy stuff even though we have not yet implemented it.
  17279. */
  17280.  
  17281. /* Send multiple files  */
  17282.  
  17283. static int
  17284. ftp_mput(argc, argv) int argc; char **argv; {
  17285.     register int i;
  17286.     sig_t oldintr;
  17287.     int ointer;
  17288.     char *tp;
  17289.     sigtype mcancel();
  17290.  
  17291.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  17292.         printf("usage: %s local-files\n", argv[0]);
  17293.         ftpcode = -1;
  17294.         return;
  17295.     }
  17296.     mname = argv[0];
  17297.     mflag = 1;
  17298.     oldintr = signal(SIGINT, mcancel);
  17299.  
  17300.     /* Replace with calls to cc_execute() */
  17301.     setjmp(jcancel);
  17302. #ifdef FTP_PROXY
  17303.     if (proxy) {
  17304.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  17305.  
  17306.         while ((cp = remglob(argv,0)) != NULL) {
  17307.             if (*cp == 0) {
  17308.                 mflag = 0;
  17309.                 continue;
  17310.             }
  17311.             if (mflag && confirm(argv[0], cp)) {
  17312.                 tp = cp;
  17313.                 if (mcase) {
  17314.                     while (*tp && !islower(*tp)) {
  17315.                         tp++;
  17316.                     }
  17317.                     if (!*tp) {
  17318.                         tp = cp;
  17319.                         tp2 = tmpbuf;
  17320.                         while ((*tp2 = *tp) != 0) {
  17321.                             if (isupper(*tp2)) {
  17322.                                 *tp2 = 'a' + *tp2 - 'A';
  17323.                             }
  17324.                             tp++;
  17325.                             tp2++;
  17326.                         }
  17327.                     }
  17328.                     tp = tmpbuf;
  17329.                 }
  17330.                 if (ntflag) {
  17331.                     tp = dotrans(tp);
  17332.                 }
  17333.                 if (mapflag) {
  17334.                     tp = domap(tp);
  17335.                 }
  17336.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  17337.                 if (!mflag && fromatty) {
  17338.                     ointer = interactive;
  17339.                     interactive = 1;
  17340.                     if (confirm("Continue with","mput")) {
  17341.                         mflag++;
  17342.                     }
  17343.                     interactive = ointer;
  17344.                 }
  17345.             }
  17346.         }
  17347.         signal(SIGINT, oldintr);
  17348.         mflag = 0;
  17349.         return;
  17350.     }
  17351. #endif /* FTP_PROXY */
  17352.     for (i = 1; i < argc; i++) {
  17353.         register char **cpp, **gargs;
  17354.  
  17355.         if (mflag && confirm(argv[0], argv[i])) {
  17356.             tp = argv[i];
  17357.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  17358.             if (!mflag && fromatty) {
  17359.                 ointer = interactive;
  17360.                 interactive = 1;
  17361.                 if (confirm("Continue with","mput")) {
  17362.                     mflag++;
  17363.                 }
  17364.                 interactive = ointer;
  17365.             }
  17366.         }
  17367.         continue;
  17368.  
  17369.         gargs = ftpglob(argv[i]);
  17370.         if (globerr != NULL) {
  17371.             printf("%s\n", globerr);
  17372.             if (gargs) {
  17373.                 blkfree(gargs);
  17374.                 free((char *)gargs);
  17375.             }
  17376.             continue;
  17377.         }
  17378.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  17379.             if (mflag && confirm(argv[0], *cpp)) {
  17380.                 tp = *cpp;
  17381.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  17382.                 if (!mflag && fromatty) {
  17383.                     ointer = interactive;
  17384.                     interactive = 1;
  17385.                     if (confirm("Continue with","mput")) {
  17386.                         mflag++;
  17387.                     }
  17388.                     interactive = ointer;
  17389.                 }
  17390.             }
  17391.         }
  17392.         if (gargs != NULL) {
  17393.             blkfree(gargs);
  17394.             free((char *)gargs);
  17395.         }
  17396.     }
  17397.     signal(SIGINT, oldintr);
  17398.     mflag = 0;
  17399. }
  17400.  
  17401. /* Get multiple files */
  17402.  
  17403. static int
  17404. ftp_mget(argc, argv) int argc; char **argv; {
  17405.     int rc = -1;
  17406.     sig_t oldintr;
  17407.     int ointer;
  17408.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  17409.     sigtype mcancel();
  17410.  
  17411.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  17412.         printf("usage: %s remote-files\n", argv[0]);
  17413.         ftpcode = -1;
  17414.         return(-1);
  17415.     }
  17416.     mname = argv[0];
  17417.     mflag = 1;
  17418.     oldintr = signal(SIGINT,mcancel);
  17419.     /* Replace with calls to cc_execute() */
  17420.     setjmp(jcancel);
  17421.     while ((cp = remglob(argv,proxy)) != NULL) {
  17422.         if (*cp == '\0') {
  17423.             mflag = 0;
  17424.             continue;
  17425.         }
  17426.         if (mflag && confirm(argv[0], cp)) {
  17427.             tp = cp;
  17428.             if (mcase) {
  17429.                 while (*tp && !islower(*tp)) {
  17430.                     tp++;
  17431.                 }
  17432.                 if (!*tp) {
  17433.                     tp = cp;
  17434.                     tp2 = tmpbuf;
  17435.                     while ((*tp2 = *tp) != 0) {
  17436.                         if (isupper(*tp2)) {
  17437.                             *tp2 = 'a' + *tp2 - 'A';
  17438.                         }
  17439.                         tp++;
  17440.                         tp2++;
  17441.                     }
  17442.                 }
  17443.                 tp = tmpbuf;
  17444.             }
  17445.             rc = (recvrequest("RETR", tp, cp, "wb",
  17446.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  17447.             if (!mflag && fromatty) {
  17448.                 ointer = interactive;
  17449.                 interactive = 1;
  17450.                 if (confirm("Continue with","mget")) {
  17451.                     mflag++;
  17452.                 }
  17453.                 interactive = ointer;
  17454.             }
  17455.         }
  17456.     }
  17457.     signal(SIGINT,oldintr);
  17458.     mflag = 0;
  17459.     return(rc);
  17460. }
  17461.  
  17462. /* Delete multiple files */
  17463.  
  17464. static int
  17465. mdelete(argc, argv) int argc; char **argv; {
  17466.     sig_t oldintr;
  17467.     int ointer;
  17468.     char *cp;
  17469.     sigtype mcancel();
  17470.  
  17471.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  17472.         printf("usage: %s remote-files\n", argv[0]);
  17473.         ftpcode = -1;
  17474.         return(-1);
  17475.     }
  17476.     mname = argv[0];
  17477.     mflag = 1;
  17478.     oldintr = signal(SIGINT, mcancel);
  17479.     /* Replace with calls to cc_execute() */
  17480.     setjmp(jcancel);
  17481.     while ((cp = remglob(argv,0)) != NULL) {
  17482.         if (*cp == '\0') {
  17483.             mflag = 0;
  17484.             continue;
  17485.         }
  17486.         if (mflag && confirm(argv[0], cp)) {
  17487.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  17488.             if (!mflag && fromatty) {
  17489.                 ointer = interactive;
  17490.                 interactive = 1;
  17491.                 if (confirm("Continue with", "mdelete")) {
  17492.                     mflag++;
  17493.                 }
  17494.                 interactive = ointer;
  17495.             }
  17496.         }
  17497.     }
  17498.     signal(SIGINT, oldintr);
  17499.     mflag = 0;
  17500.     return(rc);
  17501. }
  17502.  
  17503. /* Get a directory listing of multiple remote files */
  17504.  
  17505. static int
  17506. mls(argc, argv) int argc; char **argv; {
  17507.     sig_t oldintr;
  17508.     int ointer, i;
  17509.     char *cmd, mode[1], *dest;
  17510.     sigtype mcancel();
  17511.     int rc = -1;
  17512.  
  17513.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  17514.       goto usage;
  17515.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  17516.       usage:
  17517.         printf("usage: %s remote-files local-file\n", argv[0]);
  17518.         ftpcode = -1;
  17519.         return(-1);
  17520.     }
  17521.     dest = argv[argc - 1];
  17522.     argv[argc - 1] = NULL;
  17523.     if (strcmp(dest, "-") && *dest != '|')
  17524.       if (!globulize(&dest) ||
  17525.           !confirm("output to local-file:", dest)) {
  17526.           ftpcode = -1;
  17527.           return(-1);
  17528.       }
  17529.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  17530.     mname = argv[0];
  17531.     mflag = 1;
  17532.     oldintr = signal(SIGINT, mcancel);
  17533.     /* Replace with calls to cc_execute() */
  17534.     setjmp(jcancel);
  17535.     for (i = 1; mflag && i < argc-1; ++i) {
  17536.         *mode = (i == 1) ? 'w' : 'a';
  17537.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  17538.         if (!mflag && fromatty) {
  17539.             ointer = interactive;
  17540.             interactive = 1;
  17541.             if (confirm("Continue with", argv[0])) {
  17542.                 mflag ++;
  17543.             }
  17544.             interactive = ointer;
  17545.         }
  17546.     }
  17547.     signal(SIGINT, oldintr);
  17548.     mflag = 0;
  17549.     return(rc);
  17550. }
  17551.  
  17552. static char *
  17553. remglob(argv,doswitch) char *argv[]; int doswitch; {
  17554.     char temp[16];
  17555.     static char buf[CKMAXPATH];
  17556.     static FILE *ftemp = NULL;
  17557.     static char **args;
  17558.     int oldhash;
  17559.     char *cp, *mode;
  17560.  
  17561.     if (!mflag) {
  17562.         if (!doglob) {
  17563.             args = NULL;
  17564.         } else {
  17565.             if (ftemp) {
  17566.                 (void) fclose(ftemp);
  17567.                 ftemp = NULL;
  17568.             }
  17569.         }
  17570.         return(NULL);
  17571.     }
  17572.     if (!doglob) {
  17573.         if (args == NULL)
  17574.           args = argv;
  17575.         if ((cp = *++args) == NULL)
  17576.           args = NULL;
  17577.         return(cp);
  17578.     }
  17579.     if (ftemp == NULL) {
  17580.         (void) strcpy(temp, _PATH_TMP);
  17581. #ifdef MKTEMP
  17582. #ifndef MKSTEMP
  17583.         (void) mktemp(temp);
  17584. #endif /* MKSTEMP */
  17585. #endif /* MKTEMP */
  17586.         verbose = 0;
  17587.         oldhash = hash, hash = 0;
  17588. #ifdef FTP_PROXY
  17589.         if (doswitch) {
  17590.             pswitch(!proxy);
  17591.         }
  17592. #endif /* FTP_PROXY */
  17593.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  17594.           recvrequest ("NLST", temp, *argv, mode, 0);
  17595. #ifdef FTP_PROXY
  17596.         if (doswitch) {
  17597.             pswitch(!proxy);
  17598.         }
  17599. #endif /* FTP_PROXY */
  17600.         hash = oldhash;
  17601.         ftemp = fopen(temp, "r");
  17602.         unlink(temp);
  17603.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  17604.             printf("Can't find list of remote files, oops\n");
  17605.             return(NULL);
  17606.         }
  17607.     }
  17608.     if (fgets(buf, CKMAXPATH, ftemp) == NULL) {
  17609.         fclose(ftemp), ftemp = NULL;
  17610.         return(NULL);
  17611.     }
  17612.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  17613.       *cp = '\0';
  17614.     return(buf);
  17615. }
  17616. #endif /* NOT_USED */
  17617. #endif /* TCPSOCKET (top of file) */
  17618. #endif /* SYSFTP (top of file) */
  17619. #endif /* NOFTP (top of file) */
  17620.