home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckcftp.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  557KB  |  17,284 lines

  1. /*  C K C F T P  --  FTP Client for C-Kermit  */
  2.  
  3. char *ckftpv = "FTP Client, 8.0.239, 8 May 2006";
  4.  
  5. /*
  6.   Authors:
  7.     Jeffrey E Altman <jaltman@secure-endpoints.com>
  8.       Secure Endpoints Inc., New York City
  9.     Frank da Cruz <fdc@columbia.edu>,
  10.       The Kermit Project, Columbia University.
  11.  
  12.   Copyright (C) 2000, 2006,
  13.     Trustees of Columbia University in the City of New York.
  14.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  15.     copyright text in the ckcmai.c module for disclaimer and permissions.
  16.  
  17.   Portions of conditionally included code Copyright Regents of the
  18.     University of California and The Stanford SRP Authentication Project;
  19.     see notices below.
  20. */
  21.  
  22. /*
  23.   Pending...
  24.  
  25.   . Implement recursive NLST downloads by trying to CD to each filename.
  26.     If it works, it's a directory; if not, it's a file -- GET it.  But
  27.     that won't work with servers like wu-ftpd that don't send directory 
  28.     names.  Recursion with MLSD is done.
  29.  
  30.   . Make syslog entries for session?  Files?
  31.  
  32.   . Messages are printed to stdout and stderr in random fashion.  We should
  33.     either print everything to stdout, or else be systematic about when
  34.     to use stderr.
  35.  
  36.   . Implement mail (MAIL, MLFL, MSOM, etc) if any servers support it.
  37.  
  38.   . Adapt to VMS.  Big job because of its record-oriented file system.
  39.     RMS programmer required.  There are probably also some VMS TCP/IP
  40.     product-specific wrinkles, e.g. attribute preservation in VMS-to-VMS
  41.     transfers using special options for Multinet or other FTP servers
  42.     (find out about STRU VMS).
  43. */
  44.  
  45. /*
  46.   Quick FTP command reference:
  47.  
  48.   RFC765 (1980) and earlier:
  49.     MODE  S(tream), B(lock), C(ompressed)
  50.     STRU  F(ILE), R(ECORD), P(AGE)
  51.     TYPE  A(SCII) <format>,  E(BCDIC) <format>, I(MAGE), L(OCAL) <bytesize>
  52.     PORT  - Port
  53.     PASV  - Passive mode
  54.     USER  - User
  55.     PASS  - Password
  56.     ACCT  - Account
  57.     CWD   - Change Working Directory
  58.     REIN  - Logout but not disconnect
  59.     QUIT  - Bye
  60.     RETR  - Retreive
  61.     STOR  - Store
  62.     APPE  - Append
  63.     ALLO  - Allocate
  64.     REST  - Restart
  65.     RNFR  - Rename from
  66.     RNTO  - Rename to
  67.     ABOR  - Cancel
  68.     DELE  - Delete
  69.     LIST  - Directory
  70.     NLST  - Name List
  71.     SITE  - Site parameters or commands
  72.     STAT  - Status
  73.     HELP  - Help
  74.     NOOP  - Noop
  75.  
  76.   RFC959 (1985):
  77.     CDUP  - Change to Parent Directory
  78.     SMNT  - Structure Mount
  79.     STOU  - Store Unique
  80.     RMD   - Remove Directory
  81.     MKD   - Make Directory
  82.     PWD   - Print Directory
  83.     SYST  - System
  84.  
  85.   RFC2389 (1998):
  86.     FEAT  - List Features (done)
  87.     OPTS  - Send options (done)
  88.  
  89.   RFC2640 (1999):
  90.     LANG  - Specify language for messages (not done)
  91.  
  92.   Pending (Internet Drafts):
  93.     SIZE  - File size (done)
  94.     MDTM  - File modification date-time (done)
  95.     MLST  - File name and attribute list (single file) (not done)
  96.     MLSD  - File list with attributes (multiple files) (done)
  97.     MAIL, MLFL, MSOM - mail delivery (not done)
  98.  
  99.   Alphabetical syntax list:
  100.     ABOR <CRLF>
  101.     ACCT <SP> <account-information> <CRLF>
  102.     ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
  103.     APPE <SP> <pathname> <CRLF>
  104.     CDUP <CRLF>
  105.     CWD  <SP> <pathname> <CRLF>
  106.     DELE <SP> <pathname> <CRLF>
  107.     FEAT <CRLF>
  108.     HELP [<SP> <string>] <CRLF>
  109.     LANG [<SP> <language-tag> ] <CRLF>
  110.     LIST [<SP> <pathname>] <CRLF>
  111.     MKD  <SP> <pathname> <CRLF>
  112.     MLSD [<SP> <pathname>] <CRLF>
  113.     MLST [<SP> <pathname>] <CRLF>
  114.     MODE <SP> <mode-code> <CRLF>
  115.     NLST [<SP> <pathname-or-wildcard>] <CRLF>
  116.     NOOP <CRLF>
  117.     OPTS <SP> <commandname> [ <SP> <command-options> ] <CRLF>
  118.     PASS <SP> <password> <CRLF>
  119.     PASV <CRLF>
  120.     PORT <SP> <host-port> <CRLF>
  121.     PWD  <CRLF>
  122.     QUIT <CRLF>
  123.     REIN <CRLF>
  124.     REST <SP> <marker> <CRLF>
  125.     RETR <SP> <pathname> <CRLF>
  126.     RMD  <SP> <pathname> <CRLF>
  127.     RNFR <SP> <pathname> <CRLF>
  128.     RNTO <SP> <pathname> <CRLF>
  129.     SITE <SP> <string> <CRLF>
  130.     SIZE <SP> <pathname> <CRLF>
  131.     SMNT <SP> <pathname> <CRLF>
  132.     STAT [<SP> <pathname>] <CRLF>
  133.     STOR <SP> <pathname> <CRLF>
  134.     STOU <CRLF>
  135.     STRU <SP> <structure-code> <CRLF>
  136.     SYST <CRLF>
  137.     TYPE <SP> <type-code> <CRLF>
  138.     USER <SP> <username> <CRLF>
  139. */
  140. #include "ckcsym.h"                     /* Standard includes */
  141. #include "ckcdeb.h"
  142.  
  143. #ifndef NOFTP                           /* NOFTP  = no FTP */
  144. #ifndef SYSFTP                          /* SYSFTP = use external ftp client */
  145. #ifdef TCPSOCKET                        /* Build only if TCP/IP included */
  146. #define CKCFTP_C
  147.  
  148. /* Note: much of the following duplicates what was done in ckcdeb.h */
  149. /* but let's not mess with it unless it causes trouble. */
  150.  
  151. #ifdef CK_ANSIC
  152. #include <stdarg.h>
  153. #else /* CK_ANSIC */
  154. #include <varargs.h>
  155. #endif /* CK_ANSIC */
  156. #include <signal.h>
  157. #ifdef OS2
  158. #ifdef OS2ONLY
  159. #include <os2.h>
  160. #endif /* OS2ONLY */
  161. #include "ckowin.h"
  162. #include "ckocon.h"
  163. #endif /* OS2 */
  164. #ifndef ZILOG
  165. #ifdef NT
  166. #include <setjmpex.h>
  167. #ifdef NTSIG
  168. extern int TlsIndex;
  169. #endif /* NTSIG */
  170. #else /* NT */
  171. #include <setjmp.h>
  172. #endif /* NT */
  173. #else
  174. #include <setret.h>
  175. #endif /* ZILOG */
  176. #include "ckcsig.h"
  177. #include <sys/stat.h>
  178. #include <ctype.h>
  179. #include <errno.h>
  180. #ifndef NOTIMEH
  181. #include <time.h>
  182. #endif /* NOTIMEH */
  183. #ifndef EPIPE
  184. #define EPIPE 32                        /* Broken pipe error */
  185. #endif /* EPIPE */
  186.  
  187. /* Kermit includes */
  188.  
  189. #include "ckcasc.h"
  190. #include "ckcker.h"
  191. #include "ckucmd.h"
  192. #include "ckuusr.h"
  193. #include "ckcnet.h"                     /* Includes ckctel.h */
  194. #include "ckctel.h"                     /* (then why include it again?) */
  195. #include "ckcxla.h"
  196.  
  197. /*
  198.   How to get the struct timeval definition so we can call select().  The
  199.   xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  200.   targets.  The problem is: maybe we have already included some header file
  201.   that defined struct timeval, and maybe we didn't.  If we did, we don't want
  202.   to include another header file that defines it again or the compilation will
  203.   fail.  If we didn't, we have to include the header file where it's defined.
  204.   But in some cases even that won't work because of strict POSIX constraints
  205.   or somesuch, or because this introduces other conflicts (e.g. struct tm
  206.   multiply defined), in which case we have to define it ourselves, but this
  207.   can work only if we didn't already encounter a definition.
  208. */
  209. #ifndef DCLTIMEVAL
  210. #ifdef SV68R3V6
  211. #define DCLTIMEVAL
  212. #else
  213. #ifdef SCO234
  214. #define DCLTIMEVAL
  215. #endif /* SCO234 */
  216. #endif /* SV68R3V6 */
  217. #endif /* DCLTIMEVAL */
  218.  
  219. #ifdef DCLTIMEVAL
  220. /* Also maybe in some places the elements must be unsigned... */
  221. struct timeval {
  222.     long tv_sec;
  223.     long tv_usec;
  224. };
  225. #ifdef COMMENT
  226. /* Currently we don't use this... */
  227. struct timezone {
  228.     int tz_minuteswest;
  229.     int tz_dsttime;
  230. };
  231. #endif /* COMMENT */
  232. #else  /* !DCLTIMEVAL */
  233. #ifndef NOSYSTIMEH
  234. #ifdef SYSTIMEH
  235. #include <sys/time.h>
  236. #endif /* SYSTIMEH */
  237. #endif /* NOSYSTIMEH */
  238. #ifndef NOSYSTIMEBH
  239. #ifdef SYSTIMEBH
  240. #include <sys/timeb.h>
  241. #endif /* SYSTIMEBH */
  242. #endif /* NOSYSTIMEBH */
  243. #endif /* DCLTIMEVAL */
  244.  
  245. #include <sys/types.h>
  246. #include <stdio.h>
  247. #include <string.h>
  248. #ifdef HAVE_STDLIB_H
  249. #include <stdlib.h>
  250. #endif /* HAVE_STDLIB_H */
  251.  
  252. #ifndef NOSETTIME
  253. #ifdef COMMENT
  254. /* This section moved to ckcdeb.h */
  255. #ifdef POSIX
  256. #define UTIMEH
  257. #else
  258. #ifdef HPUX9
  259. #define UTIMEH
  260. #else
  261. #ifdef OS2
  262. #define SYSUTIMEH
  263. #endif /* OS2 */
  264. #endif /* HPUX9 */
  265. #endif /* POSIX */
  266. #endif /* COMMENT */
  267.  
  268. #ifdef SYSUTIMEH
  269. #include <sys/utime.h>
  270. #else
  271. #ifdef UTIMEH
  272. #include <utime.h>
  273. #define SYSUTIMEH
  274. #endif /* UTIMEH */
  275. #endif /* SYSUTIMEH */
  276. #endif /* NOSETTIME */
  277.  
  278. #ifndef SCO_OSR504
  279. #ifdef SELECT_H
  280. #include <sys/select.h>
  281. #endif /* SELECT_H */
  282. #endif /* SCO_OSR504 */
  283.  
  284. /* select() dialects... */
  285.  
  286. #ifdef UNIX
  287. #define BSDSELECT                       /* BSD select() syntax/semantics */
  288. #else
  289. #ifdef OS2                              /* OS/2 or Win32 */
  290. #ifdef NT
  291. #define BSDSELECT
  292. #else /* NT */
  293. #define IBMSELECT
  294. #endif /* NT */
  295. #endif /* OS2 */
  296. #endif /* UNIX */
  297.  
  298. /* Other select() peculiarities */
  299.  
  300. #ifdef HPUX
  301. #ifndef HPUX10                          /* HP-UX 9.xx and earlier */
  302. #ifndef HPUX1100
  303. /* The three interior args to select() are (int *) rather than (fd_set *) */
  304. #ifndef INTSELECT
  305. #define INTSELECT
  306. #endif /* INTSELECT */
  307. #endif /* HPUX1100 */
  308. #endif /* HPUX10 */
  309. #endif /* HPUX */
  310.  
  311. #ifdef CK_SOCKS                         /* SOCKS Internet relay package */
  312. #ifdef CK_SOCKS5                        /* SOCKS 5 */
  313. #define accept  SOCKSaccept
  314. #define bind    SOCKSbind
  315. #define connect SOCKSconnect
  316. #define getsockname SOCKSgetsockname
  317. #define listen SOCKSlisten
  318. #else  /* Not SOCKS 5 */
  319. #define accept  Raccept
  320. #define bind    Rbind
  321. #define connect Rconnect
  322. #define getsockname Rgetsockname
  323. #define listen Rlisten
  324. #endif /* CK_SOCKS5 */
  325. #endif /* CK_SOCKS */
  326.  
  327. #ifndef NOHTTP
  328. extern char * tcp_http_proxy;           /* Name[:port] of http proxy server */
  329. extern int    tcp_http_proxy_errno;
  330. extern char * tcp_http_proxy_user;
  331. extern char * tcp_http_proxy_pwd;
  332. extern char * tcp_http_proxy_agent;
  333. #define HTTPCPYL 1024
  334. static char proxyhost[HTTPCPYL];
  335. #endif /* NOHTTP */
  336. int ssl_ftp_proxy = 0;                  /* FTP over SSL/TLS Proxy Server */
  337.  
  338. /* Feature selection */
  339.  
  340. #ifndef USE_SHUTDOWN
  341. /*
  342.   We don't use shutdown() because (a) we always call it just before close()
  343.   so it's redundant and unnecessary, and (b) it introduces a long pause on
  344.   some platforms like SV/68 R3.
  345. */
  346. /* #define USE_SHUTDOWN */
  347. #endif /* USE_SHUTDOWN */
  348.  
  349. #ifndef NORESEND
  350. #ifndef NORESTART                       /* Restart / recover */
  351. #ifndef FTP_RESTART
  352. #define FTP_RESTART
  353. #endif /* FTP_RESTART */
  354. #endif /* NORESTART */
  355. #endif /* NORESEND */
  356.  
  357. #ifndef NOUPDATE                        /* Update mode */
  358. #ifndef DOUPDATE
  359. #define DOUPDATE
  360. #endif /* DOUPDATE */
  361. #endif /* NOUPDATE */
  362.  
  363. #ifndef UNICODE                         /* Unicode required */
  364. #ifndef NOCSETS                         /* for charset translation */
  365. #define NOCSETS
  366. #endif /* NOCSETS */
  367. #endif /* UNICODE */
  368.  
  369. #ifndef OS2
  370. #ifndef HAVE_MSECS                      /* Millisecond timer */
  371. #ifdef UNIX
  372. #ifdef GFTIMER
  373. #define HAVE_MSECS
  374. #endif /* GFTIMER */
  375. #endif /* UNIX */
  376. #endif /* HAVE_MSECS */
  377. #endif /* OS2 */
  378.  
  379. #ifdef PIPESEND                         /* PUT from pipe */
  380. #ifndef PUTPIPE
  381. #define PUTPIPE
  382. #endif /* PUTPIPE */
  383. #endif /* PIPESEND */
  384.  
  385. #ifndef NOSPL                           /* PUT from array */
  386. #ifndef PUTARRAY
  387. #define PUTARRAY
  388. #endif /* PUTARRAY */
  389. #endif /* NOSPL */
  390.  
  391. /* Security... */
  392.  
  393. #ifdef CK_SRP
  394. #define FTP_SRP
  395. #endif /* CK_SRP */
  396.  
  397. #ifdef CK_KERBEROS
  398. #ifdef KRB4
  399. /*
  400.   There is a conflict between the Key Schedule formats used internally
  401.   within the standalone MIT KRB4 library and that used by Eric Young
  402.   in OpenSSL and his standalone DES library.  Therefore, KRB4 FTP AUTH
  403.   cannot be supported when either of those two packages are used.
  404. */
  405. #ifdef KRB524
  406. #define FTP_KRB4
  407. #else /* KRB524 */
  408. #ifndef CK_SSL
  409. #ifndef LIBDES
  410. #define FTP_KRB4
  411. #endif /* LIBDES */
  412. #endif /* CK_SSL */
  413. #endif /* KRB524 */
  414. #endif /* KRB4 */
  415. #ifdef KRB5
  416. #ifndef HEIMDAL
  417. #define FTP_GSSAPI
  418. #endif /* HEIMDAL */
  419. #endif /* KRB5 */
  420. #endif /* CK_KERBEROS */
  421.  
  422. /* FTP_SECURITY is defined if any of the above is selected */
  423. #ifndef FTP_SECURITY
  424. #ifdef FTP_GSSAPI
  425. #define FTP_SECURITY
  426. #else
  427. #ifdef FTP_KRB4
  428. #define FTP_SECURITY
  429. #else
  430. #ifdef FTP_SRP
  431. #define FTP_SECURITY
  432. #else
  433. #ifdef CK_SSL
  434. #define FTP_SECURITY
  435. #endif /* CK_SSL */
  436. #endif /* FTP_SRP */
  437. #endif /* FTP_KRB4 */
  438. #endif /* FTP_GSSAPI */
  439. #endif /* FTP_SECURITY */
  440.  
  441. #ifdef CK_DES
  442. #ifdef CK_SSL
  443. #ifndef LIBDES
  444. #define LIBDES
  445. #endif /* LIBDES */
  446. #endif /* CK_SSL */
  447. #endif /* CK_DES */
  448.  
  449. #ifdef CRYPT_DLL
  450. #ifndef LIBDES
  451. #define LIBDES
  452. #endif /* LIBDES */
  453. #endif /* CRYPT_DLL */
  454.  
  455. #ifdef FTP_KRB4
  456. #define des_cblock Block
  457. #define des_key_schedule Schedule
  458. #ifdef KRB524
  459. #ifdef NT
  460. #define _WINDOWS
  461. #endif /* NT */
  462. #include "kerberosIV/krb.h"
  463. #else /* KRB524 */
  464. #ifdef SOLARIS
  465. #ifndef sun
  466. /* For some reason lost in history the Makefile Solaris targets have -Usun */
  467. #define sun
  468. #endif /* sun */
  469. #endif /* SOLARIS */
  470. #include "krb.h"
  471. #define krb_get_err_text_entry krb_get_err_text
  472. #endif /* KRB524 */
  473. #endif /* FTP_KRB4 */
  474.  
  475. #ifdef CK_SSL
  476. #ifdef FTP_KRB4
  477. #ifndef HEADER_DES_H
  478. #define HEADER_DES_H
  479. #endif /* HEADER_DES_H */
  480. #endif /* FTP_KRB4 */
  481. #include "ck_ssl.h"
  482. #endif /* CK_SSL */
  483.  
  484. #ifdef FTP_SRP
  485. #ifdef HAVE_PWD_H
  486. #include "pwd.h"
  487. #endif /* HAVE_PWD_H */
  488. #include "t_pwd.h"
  489. #include "t_client.h"
  490. #include "krypto.h"
  491. #endif /* FTP_SRP */
  492.  
  493. #ifdef FTP_GSSAPI
  494. #include <gssapi/gssapi.h>
  495. /*
  496.   Need to include the krb5 file, because we're doing manual fallback
  497.   from the v2 mech to the v1 mech.  Once there's real negotiation,
  498.   we can be generic again.
  499. */
  500. #include <gssapi/gssapi_generic.h>
  501. #include <gssapi/gssapi_krb5.h>
  502. static gss_ctx_id_t gcontext;
  503.  
  504. #ifdef MACOSX
  505. /** exported constants defined in gssapi_krb5{,_nx}.h **/
  506.  
  507. /* these are bogus, but will compile */
  508.  
  509. /*
  510.  * The OID of the draft krb5 mechanism, assigned by IETF, is:
  511.  *      iso(1) org(3) dod(5) internet(1) security(5)
  512.  *      kerberosv5(2) = 1.3.5.1.5.2
  513.  * The OID of the krb5_name type is:
  514.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  515.  *      krb5(2) krb5_name(1) = 1.2.840.113554.1.2.2.1
  516.  * The OID of the krb5_principal type is:
  517.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  518.  *      krb5(2) krb5_principal(2) = 1.2.840.113554.1.2.2.2
  519.  * The OID of the proposed standard krb5 mechanism is:
  520.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  521.  *      krb5(2) = 1.2.840.113554.1.2.2
  522.  * The OID of the proposed standard krb5 v2 mechanism is:
  523.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  524.  *      krb5v2(3) = 1.2.840.113554.1.2.3
  525.  *
  526.  */
  527.  
  528. /*
  529.  * Encoding rules: The first two values are encoded in one byte as 40
  530.  * * value1 + value2.  Subsequent values are encoded base 128, most
  531.  * significant digit first, with the high bit (\200) set on all octets
  532.  * except the last in each value's encoding.
  533.  */
  534.  
  535. static CONST gss_OID_desc
  536. ck_krb5_gss_oid_array[] = {
  537.    /* this is the official, rfc-specified OID */
  538.    {9, "\052\206\110\206\367\022\001\002\002"},
  539.    /* this is the unofficial, wrong OID */
  540.    {5, "\053\005\001\005\002"},
  541.    /* this is the v2 assigned OID */
  542.    {9, "\052\206\110\206\367\022\001\002\003"},
  543.    /* these two are name type OID's */
  544.    {10, "\052\206\110\206\367\022\001\002\002\001"},
  545.    {10, "\052\206\110\206\367\022\001\002\002\002"},
  546.    { 0, 0 }
  547. };
  548.  
  549. static CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
  550. static CONST gss_OID_desc * CONST gss_mech_krb5_old = ck_krb5_gss_oid_array+1;
  551. static CONST gss_OID_desc * CONST gss_mech_krb5_v2 = ck_krb5_gss_oid_array+2;
  552. static CONST gss_OID_desc * CONST gss_nt_krb5_name = ck_krb5_gss_oid_array+3;
  553. static CONST gss_OID_desc * CONST gss_nt_krb5_principal = ck_krb5_gss_oid_array+4;
  554.  
  555. /*
  556.  * See krb5/gssapi_krb5.c for a description of the algorithm for
  557.  * encoding an object identifier.
  558.  */
  559.  
  560. /*
  561.  * The OID of user_name is:
  562.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  563.  *      generic(1) user_name(1) = 1.2.840.113554.1.2.1.1
  564.  * machine_uid_name:
  565.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  566.  *      generic(1) machine_uid_name(2) = 1.2.840.113554.1.2.1.2
  567.  * string_uid_name:
  568.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  569.  *      generic(1) string_uid_name(3) = 1.2.840.113554.1.2.1.3
  570.  * service_name:
  571.  *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
  572.  *      generic(1) service_name(4) = 1.2.840.113554.1.2.1.4
  573.  * exported_name:
  574.  *      1(iso), 3(org), 6(dod), 1(internet), 5(security), 6(nametypes),
  575.  *          4(gss-api-exported-name)
  576.  * host_based_service_name (v2):
  577.  *      iso (1) org (3), dod (6), internet (1), security (5), nametypes(6),
  578.  *      gss-host-based-services(2)
  579.  */
  580.  
  581. static gss_OID_desc ck_oids[] = {
  582.    {10, "\052\206\110\206\367\022\001\002\001\001"},
  583.    {10, "\052\206\110\206\367\022\001\002\001\002"},
  584.    {10, "\052\206\110\206\367\022\001\002\001\003"},
  585.    {10, "\052\206\110\206\367\022\001\002\001\004"},
  586.    { 6, "\053\006\001\005\006\004"},
  587.    { 6, "\053\006\001\005\006\002"},
  588. };
  589.  
  590. static gss_OID ck_gss_nt_user_name = ck_oids+0;
  591. static gss_OID ck_gss_nt_machine_uid_name = ck_oids+1;
  592. static gss_OID ck_gss_nt_string_uid_name = ck_oids+2;
  593. static gss_OID ck_gss_nt_service_name = ck_oids+3;
  594. static gss_OID ck_gss_nt_exported_name = ck_oids+4;
  595. static gss_OID ck_gss_nt_service_name_v2 = ck_oids+5;
  596. #endif /* MACOSX */
  597. #endif /* FTP_GSSAPI */
  598.  
  599. #ifdef OS2
  600. #ifdef FTP_SRP
  601. #define MAP_KRYPTO
  602. #ifdef SRPDLL
  603. #define MAP_SRP
  604. #endif /* SRPDLL */
  605. #endif /* FTP_SRP */
  606. #ifdef FTP_KRB4
  607. #define MAP_KRB4
  608. #ifdef CK_ENCRYPTION
  609. #define MAP_DES
  610. #endif /* CK_ENCRYPTION */
  611. #endif /* FTP_KRB4 */
  612. #ifdef FTP_GSSAPI
  613. #define MAP_GSSAPI
  614. #define GSS_OIDS
  615. #endif /* FTP_GSSAPI */
  616. #include "ckoath.h"
  617.  
  618. extern int k95stdout, wherex[], wherey[];
  619. extern unsigned char colorcmd;
  620. #endif /* OS2 */
  621.  
  622. #ifdef FTP_KRB4
  623. static char ftp_realm[REALM_SZ + 1];
  624. static KTEXT_ST ftp_tkt;
  625. #ifdef OS2
  626. static LEASH_CREDENTIALS ftp_cred;
  627. #else /* OS2 */
  628. static CREDENTIALS ftp_cred;
  629. #endif /* OS2 */
  630. static MSG_DAT ftp_msg_data;
  631. static des_key_schedule ftp_sched;
  632. static int foo[4] = {99,99,99,99};
  633. #endif /* FTP_KRB4 */
  634.  
  635. /* getreply() function codes */
  636.  
  637. #define GRF_AUTH 1            /* Reply to AUTH command */
  638. #define GRF_FEAT 2            /* Reply to FEAT command */
  639.  
  640. /* Operational definitions */
  641.  
  642. #define DEF_VBM 0                       /* Default verbose mode */
  643. /* #define SETVBM */                    /* (see getreply) */
  644.  
  645. #define URL_ONEFILE                     /* GET, not MGET, for FTP URL */
  646.  
  647. #define FTP_BUFSIZ 10240                /* Max size for FTP cmds & replies */
  648. #define SRVNAMLEN 32                    /* Max length for server type name */
  649. #define PWDSIZ 256
  650. #define PASSBUFSIZ 256
  651. #define PROMPTSIZ 256
  652.  
  653. #ifndef MGETMAX                         /* Max operands for MGET command */
  654. #define MGETMAX 1000
  655. #endif /* MGETMAX */
  656.  
  657. #ifdef FTP_SRP
  658. #define FUDGE_FACTOR 100
  659. #endif /* FTP_SRP */
  660.  
  661. /*
  662.   Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  663.   number bytes.  Must be defined for each auth type.
  664.   GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  665.   3DES requires 56 bytes.  Lets use 96 just to be sure.
  666. */
  667. #ifdef FTP_GSSAPI
  668. #ifndef FUDGE_FACTOR
  669. #define FUDGE_FACTOR 96
  670. #endif /* FUDGE_FACTOR */
  671. #endif /* FTP_GSSAPI */
  672.  
  673. #ifdef FTP_KRB4
  674. #ifndef FUDGE_FACTOR
  675. #define FUDGE_FACTOR 32
  676. #endif /* FUDGE_FACTOR */
  677. #endif /* FTP_KRB4 */
  678.  
  679. #ifndef FUDGE_FACTOR                    /* In case no auth types define it */
  680. #define FUDGE_FACTOR 0
  681. #endif /* FUDGE_FACTOR */
  682.  
  683. #ifndef MAXHOSTNAMELEN
  684. #define MAXHOSTNAMELEN 64
  685. #endif /* MAXHOSTNAMELEN */
  686. #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
  687.  
  688. /* Fascist compiler toadying */
  689.  
  690. #ifndef SENDARG2TYPE
  691. #ifdef COMMENT                          /* Might be needed here and there */
  692. #define SENDARG2TYPE const char *
  693. #else
  694. #define SENDARG2TYPE char *
  695. #endif /* COMMENT */
  696. #endif /* SENDARG2TYPE */
  697.  
  698. /* Common text messages */
  699.  
  700. static char *nocx = "?No FTP control connection\n";
  701.  
  702. static char *fncnam[] = {
  703.   "rename", "overwrite", "backup", "append", "discard", "ask", "update",
  704.   "dates-differ", ""
  705. };
  706.  
  707. /* Macro definitions */
  708.  
  709. /* Used to speed up text-mode PUTs */
  710. #define zzout(fd,c) \
  711. ((fd<0)?(-1):((nout>=ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
  712.  
  713. #define CHECKCONN() if(!connected){printf(nocx);return(-9);}
  714.  
  715. /* Externals */
  716.  
  717. #ifdef CK_URL
  718. extern struct urldata g_url;
  719. #endif /* CK_URL */
  720.  
  721. #ifdef DYNAMIC
  722. extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
  723. #else
  724. extern char zinbuffer[], zoutbuffer[];
  725. #endif /* DYNAMIC */
  726. extern char *zinptr, *zoutptr;
  727. extern int zincnt, zoutcnt, zobufsize, fncact;
  728.  
  729. #ifdef CK_TMPDIR
  730. extern int f_tmpdir;                    /* Directory changed temporarily */
  731. extern char savdir[];                   /* For saving current directory */
  732. extern char * dldir;
  733. #endif /* CK_TMPDIR */
  734.  
  735. extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
  736.  
  737. extern xx_strp xxstring;
  738. extern struct keytab onoff[], txtbin[], rpathtab[];
  739. extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
  740. extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
  741. extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
  742. extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
  743. extern int nolinks, msgflg, keep;
  744. extern CK_OFF_T fsize, ffc, tfc, sendstart, sndsmaller, sndlarger, rs_len;
  745. extern long filcnt, xfsecs, tfcps, cps, oldcps;
  746. extern char * tcp_address;
  747.  
  748. #ifdef GFTIMER
  749. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  750. #else
  751. extern long xfsecs;
  752. #endif /* GFTIMER */
  753.  
  754. extern char filnam[], * filefile, myhost[];
  755. extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
  756. extern int g_skipbup, skipbup, sendmode;
  757. extern int g_displa, fdispla, displa;
  758.  
  759. #ifdef LOCUS
  760. extern int locus, autolocus;
  761. #endif /* LOCUS */
  762.  
  763. #ifndef NOCSETS
  764. extern int nfilc, dcset7, dcset8, fileorder;
  765. extern struct csinfo fcsinfo[];
  766. extern struct keytab fcstab[];
  767. extern int fcharset;
  768. #endif /* NOCSETS */
  769.  
  770. extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
  771. extern char sndnbefore[], sndnafter[], *rcvexcept[];
  772. extern CHAR feol;
  773.  
  774. extern char * remdest;
  775. extern int remfile, remappd, rempipe;
  776.  
  777. #ifndef NOSPL
  778. extern int cmd_quoting;
  779. #ifdef PUTARRAY
  780. extern int sndxlo, sndxhi, sndxin;
  781. extern char sndxnam[];
  782. extern char **a_ptr[];                  /* Array pointers */
  783. extern int a_dim[];                     /* Array dimensions */
  784. #endif /* PUTARRAY */
  785. #endif /* NOSPL */
  786.  
  787. #ifndef NOMSEND                         /* MPUT and ADD SEND-LIST lists */
  788. extern char *msfiles[];
  789. extern int filesinlist;
  790. extern struct filelist * filehead;
  791. extern struct filelist * filetail;
  792. extern struct filelist * filenext;
  793. extern int addlist;
  794. extern char fspec[];                    /* Most recent filespec */
  795. extern int fspeclen;                    /* Length of fspec[] buffer */
  796. #endif /* NOMSEND */
  797.  
  798. extern int pipesend;
  799. #ifdef PIPESEND
  800. extern char * sndfilter, * rcvfilter;
  801. #endif /* PIPESEND */
  802.  
  803. #ifdef CKROOT
  804. extern int ckrooterr;
  805. #endif /* CKROOT */
  806.  
  807. #ifdef KRB4
  808. extern int krb4_autoget;
  809. _PROTOTYP(char * ck_krb4_realmofhost,(char *));
  810. #endif /* KRB4 */
  811.  
  812. #ifdef KRB5
  813. extern int krb5_autoget;
  814. extern int krb5_d_no_addresses;
  815. _PROTOTYP(char * ck_krb5_realmofhost,(char *));
  816. #endif /* KRB5 */
  817.  
  818. #ifdef DCMDBUF
  819. extern char *atmbuf;                    /* Atom buffer (malloc'd) */
  820. extern char *cmdbuf;                    /* Command buffer (malloc'd) */
  821. extern char *line;                      /* Big string buffer #1 */
  822. extern char *tmpbuf;                    /* Big string buffer #2 */
  823. #else
  824. extern char atmbuf[];                   /* The same, but static */
  825. extern char cmdbuf[];
  826. extern char line[];
  827. extern char tmpbuf[];
  828. #endif /* DCMDBUF */
  829.  
  830. extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
  831.  
  832. /* Public variables declared here */
  833.  
  834. #ifdef NOXFER
  835. int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
  836. #else
  837. int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
  838. #endif /* NOXFER */
  839. int ftpcode = -1;                       /* Last FTP response code */
  840. int ftp_cmdlin = 0;                     /* FTP invoked from command line */
  841. int ftp_fai = 0;                        /* FTP failure count */
  842. int ftp_deb = 0;                        /* FTP debugging */
  843. int ftp_dis = -1;            /* FTP display style */
  844. int ftp_log = 1;                        /* FTP Auto-login */
  845. int sav_log = -1;
  846. int ftp_action = 0;                     /* FTP action from command line */
  847. int ftp_dates = 1;                      /* Set file dates from server */
  848.  
  849. char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
  850. char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
  851. char ftp_user_host[MAX_DNS_NAMELEN]= ""; /* FTP hostname specified by user */
  852. char * ftp_host = NULL;                 /* FTP hostname */
  853. char * ftp_logname = NULL;              /* FTP username */
  854. char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
  855. char * ftp_apw = NULL;            /* Anonymous password */
  856.  
  857. /* Definitions and typedefs needed for prototypes */
  858.  
  859. #define sig_t my_sig_t
  860. #define sigtype SIGTYP
  861. typedef sigtype (*sig_t)();
  862.  
  863. /* Static global variables */
  864.  
  865. static char ftpsndbuf[FTP_BUFSIZ+64];
  866.  
  867. static char * fts_sto = NULL;
  868.  
  869. static int ftpsndret = 0;
  870. static struct _ftpsnd {
  871.     sig_t oldintr, oldintp;
  872.     int            reply;
  873.     int            incs,
  874.                    outcs;
  875.     char *         cmd, * local, * remote;
  876.     int            bytes;
  877.     int            restart;
  878.     int            xlate;
  879.     char *         lmode;
  880. } ftpsnd;
  881.  
  882. /*
  883.   This is just a first stab -- these strings should match how the
  884.   corresponding FTP servers identify themselves.
  885. */
  886. #ifdef UNIX
  887. static char * myostype = "UNIX";
  888. #else
  889. #ifdef VMS
  890. /* not yet... */
  891. static char * myostype = "VMS";
  892. #else
  893. #ifdef OS2
  894. #ifdef NT
  895. static char * myostype = "WIN32";
  896. #else
  897. static char * myostype = "OS/2";
  898. #endif /* NT */
  899. #else
  900. static char * myostype = "UNSUPPORTED";
  901. #endif /* OS2  */
  902. #endif /* VMS */
  903. #endif /* UNIX */
  904.  
  905. static int noinit = 0;                  /* Don't send REST, STRU, MODE */
  906. static int alike = 0;                   /* Client/server like platforms */
  907. static int local = 1;                   /* Shadows Kermit global 'local' */
  908. static int dout = -1;                   /* Data connection file descriptor */
  909. static int dpyactive = 0;               /* Data transfer is active */
  910. static int globaldin = -1;              /* Data connection f.d. */
  911. static int out2screen = 0;              /* GET output is to screen */
  912. static int forcetype = 0;               /* Force text or binary mode */
  913. static int cancelfile = 0;              /* File canceled */
  914. static int cancelgroup = 0;             /* Group canceled */
  915. static int anonymous = 0;               /* Logging in as anonymous */
  916. static int loggedin = 0;                /* Logged in (or not) */
  917. static int puterror = 0;                /* What to do on PUT error */
  918. static int geterror = 0;                /* What to do on GET error */
  919. static int rfrc = 0;                    /* remote_files() return code */
  920. static int okrestart = 0;               /* Server understands REST */
  921. static int printlines = 0;              /* getreply()should print data lines */
  922. static int haveurl = 0;                 /* Invoked by command-line FTP URL */
  923. static int mdtmok = 1;            /* Server supports MDTM */
  924. static int sizeok = 1;
  925. static int featok = 1;
  926. static int mlstok = 1;
  927. static int stouarg = 1;
  928. static int typesent = 0;
  929. static int havesigint = 0;
  930. static long havetype =  0;
  931. static CK_OFF_T havesize = (CK_OFF_T)-1;
  932. static char * havemdtm = NULL;
  933. static int mgetmethod = 0;        /* NLST or MLSD */
  934. static int mgetforced = 0;
  935.  
  936. static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
  937. static int c0, c1;                      /* Temp variables for characters */
  938.  
  939. static char putpath[CKMAXPATH+1] = { NUL, NUL };
  940. static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
  941.  
  942. #define RFNBUFSIZ 4096            /* Remote filename buffer size */
  943.  
  944. static unsigned int maxbuf = 0, actualbuf = 0;
  945. static CHAR *ucbuf = NULL;
  946. static int ucbufsiz = 0;
  947. static unsigned int nout = 0;           /* Number of chars in ucbuf */
  948.  
  949. static jmp_buf recvcancel;
  950. static jmp_buf sendcancel;
  951. static jmp_buf ptcancel;
  952. static jmp_buf jcancel;
  953. static int ptabflg = 0;
  954.  
  955. /* Protection level symbols */
  956.  
  957. #define FPL_CLR 1                       /* Clear */
  958. #define FPL_SAF 2                       /* Safe */
  959. #define FPL_PRV 3                       /* Private */
  960. #define FPL_CON 4                       /* Confidential */
  961.  
  962. /* Symbols for file types returned by MLST/MLSD */
  963.  
  964. #define FTYP_FILE 1            /* Regular file */
  965. #define FTYP_DIR  2            /* Directory */
  966. #define FTYP_CDIR 3            /* Current directory */
  967. #define FTYP_PDIR 4            /* Parent directory */
  968.  
  969. /* File type symbols keyed to the file-type symbols from ckcker.h */
  970.  
  971. #define FTT_ASC XYFT_T                  /* ASCII (text) */
  972. #define FTT_BIN XYFT_B                  /* Binary (image) */
  973. #define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
  974.  
  975. /* Server feature table - sfttab[0] > 0 means server supports FEAT and OPTS */
  976.  
  977. static int sfttab[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  978.  
  979. #define SFT_AUTH  1            /* FTP server feature codes */
  980. #define SFT_LANG  2
  981. #define SFT_MDTM  3
  982. #define SFT_MLST  4
  983. #define SFT_PBSZ  5
  984. #define SFT_PROT  6
  985. #define SFT_REST  7
  986. #define SFT_SIZE  8
  987. #define SFT_TVFS  9
  988. #define SFT_UTF8 10
  989.  
  990. #define CNV_AUTO  2            /* FTP filename conversion */
  991. #define CNV_CNV   1
  992. #define CNV_LIT   0
  993.  
  994. /* SET FTP values */
  995.  
  996. static int                              /* SET FTP values... */
  997.   ftp_aut = 1,                          /* Auto-authentication */
  998. #ifdef FTP_SECURITY
  999.   ftp_cry = 1,                          /* Auto-encryption */
  1000.   ftp_cfw = 0,                          /* Credential forwarding */
  1001. #endif /* FTP_SECURITY */
  1002.   ftp_cpl = FPL_CLR,                    /* Command protection level */
  1003.   ftp_dpl = FPL_CLR,                    /* Data protection level */
  1004. #ifdef FTP_PROXY
  1005.   ftp_prx = 0,                          /* Use proxy */
  1006. #endif /* FTP_PROXY */
  1007.   sav_psv = -1,                         /* For saving passive mode */
  1008.   ftp_psv = 1,                          /* Passive mode */
  1009.   ftp_spc = 1,                          /* Send port commands */
  1010.   ftp_typ = FTT_ASC,                    /* Type */
  1011.   get_auto = 1,                         /* Automatic type switching for GET */
  1012.   tenex = 0,                            /* Type is Tenex */
  1013.   ftp_usn = 0,                          /* Unique server names */
  1014.   ftp_prm = 0,                          /* Permissions */
  1015.   ftp_cnv = CNV_AUTO,            /* Filename conversion (2 = auto) */
  1016.   ftp_vbm = DEF_VBM,                    /* Verbose mode */
  1017.   ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  1018.   ftp_err = 0,                          /* Error action */
  1019.   ftp_fnc = -1;                         /* Filename collision action */
  1020.  
  1021. #ifdef CK_SSL
  1022. static int ftp_bug_use_ssl_v2 = 0;      /* use SSLv2 for AUTH SSL */
  1023. #endif /* CK_SSL */
  1024.  
  1025. static int
  1026. #ifdef NOCSETS
  1027.   ftp_csr = -1,                         /* Remote (server) character set */
  1028. #else
  1029.   ftp_csr = FC_UTF8,
  1030. #endif /* NOCSETS */
  1031.   ftp_xla = 0;                          /* Character-set translation on/off */
  1032. int
  1033.   ftp_csx = -1,                         /* Remote charset currently in use */
  1034.   ftp_csl = -1;                         /* Local charset currently in use */
  1035.  
  1036. static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
  1037.  
  1038. char * ftp_nml = NULL;                  /* /NAMELIST */
  1039. char * ftp_tmp = NULL;                  /* Temporary string */
  1040. static char * ftp_acc = NULL;           /* Account string */
  1041. static char * auth_type = NULL;         /* Authentication type */
  1042. static char * srv_renam = NULL;         /* Server-rename string */
  1043. FILE * fp_nml = NULL;                   /* Namelist file pointer */
  1044.  
  1045. static int csocket = -1;                /* Control socket */
  1046. static int connected = 0;               /* Connected to FTP server */
  1047. static unsigned short ftp_port = 0;    /* FTP port */
  1048. #ifdef FTPHOST
  1049. static int hostcmd = 0;                 /* Has HOST command been sent */
  1050. #endif /* FTPHOST */
  1051. static int form, mode, stru, bytesize, curtype = FTT_ASC;
  1052. static char bytename[8];
  1053.  
  1054. /* For parsing replies to FTP server command */
  1055. static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
  1056.  
  1057. #ifdef FTP_PROXY
  1058. static int proxy, unix_proxy
  1059. #endif /* FTP_PROXY */
  1060.  
  1061. static char pasv[64];                   /* Passive-mode port */
  1062. static int passivemode = 0;
  1063. static int sendport = 0;
  1064. static int servertype = 0;              /* FTP server's OS type */
  1065.  
  1066. static int testing = 0;
  1067. static char ftpcmdbuf[FTP_BUFSIZ];
  1068.  
  1069. /* Macro definitions */
  1070.  
  1071. #define UC(b) ckitoa(((int)b)&0xff)
  1072. #define nz(x) ((x) == 0 ? 1 : (x))
  1073.  
  1074. /* Command tables and definitions */
  1075.  
  1076. #define FTP_ACC  1                      /* FTP command keyword codes */
  1077. #define FTP_APP  2
  1078. #define FTP_CWD  3
  1079. #define FTP_CHM  4
  1080. #define FTP_CLS  5
  1081. #define FTP_DEL  6
  1082. #define FTP_DIR  7
  1083. #define FTP_GET  8
  1084. #define FTP_IDL  9
  1085. #define FTP_MDE 10
  1086. #define FTP_MDI 11
  1087. #define FTP_MGE 12
  1088. #define FTP_MKD 13
  1089. #define FTP_MOD 14
  1090. #define FTP_MPU 15
  1091. #define FTP_OPN 16
  1092. #define FTP_PUT 17
  1093. #define FTP_PWD 18
  1094. #define FTP_RGE 19
  1095. #define FTP_REN 20
  1096. #define FTP_RES 21
  1097. #define FTP_HLP 22
  1098. #define FTP_RMD 23
  1099. #define FTP_STA 24
  1100. #define FTP_SIT 25
  1101. #define FTP_SIZ 26
  1102. #define FTP_SYS 27
  1103. #define FTP_UMA 28
  1104. #define FTP_GUP 29
  1105. #define FTP_USR 30
  1106. #define FTP_QUO 31
  1107. #define FTP_TYP 32
  1108. #define FTP_FEA 33
  1109. #define FTP_OPT 34
  1110. #define FTP_CHK 35
  1111. #define FTP_VDI 36
  1112. #define FTP_ENA 37
  1113. #define FTP_DIS 38
  1114.  
  1115. struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
  1116.     { "auto",    2, 0 },
  1117.     { "ftp",     1, 0 },
  1118.     { "kermit",  0, 0  }
  1119. };
  1120.  
  1121. static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
  1122.     { "proceed", 0, 0 },                /* 0 = proceed */
  1123.     { "quit",    1, 0 }                 /* 1 = quit */
  1124. };
  1125.  
  1126. static struct keytab ftpcmdtab[] = {    /* FTP command table */
  1127.     { "account",   FTP_ACC, 0 },
  1128.     { "append",    FTP_APP, 0 },
  1129.     { "bye",       FTP_CLS, 0 },
  1130.     { "cd",        FTP_CWD, 0 },
  1131.     { "cdup",      FTP_GUP, 0 },
  1132.     { "check",     FTP_CHK, 0 },
  1133.     { "chmod",     FTP_CHM, 0 },
  1134.     { "close",     FTP_CLS, 0 },
  1135.     { "cwd",       FTP_CWD, CM_INV },
  1136.     { "delete",    FTP_MDE, 0 },
  1137.     { "directory", FTP_DIR, 0 },
  1138.     { "disable",   FTP_DIS, 0 },
  1139.     { "enable",    FTP_ENA, 0 },
  1140.     { "features",  FTP_FEA, 0 },
  1141.     { "get",       FTP_GET, 0 },
  1142.     { "help",      FTP_HLP, 0 },
  1143.     { "idle",      FTP_IDL, 0 },
  1144.     { "login",     FTP_USR, CM_INV },
  1145.     { "mdelete",   FTP_MDE, CM_INV },
  1146.     { "mget",      FTP_MGE, 0 },
  1147.     { "mkdir",     FTP_MKD, 0 },
  1148.     { "modtime",   FTP_MOD, 0 },
  1149.     { "mput",      FTP_MPU, 0 },
  1150.     { "open",      FTP_OPN, 0 },
  1151.     { "opt",       FTP_OPT, CM_INV|CM_ABR },
  1152.     { "opts",      FTP_OPT, CM_INV },
  1153.     { "options",   FTP_OPT, 0 },
  1154.     { "put",       FTP_PUT, 0 },
  1155.     { "pwd",       FTP_PWD, 0 },
  1156.     { "quit",      FTP_CLS, CM_INV },
  1157.     { "quote",     FTP_QUO, 0 },
  1158.     { "reget",     FTP_RGE, 0 },
  1159.     { "rename",    FTP_REN, 0 },
  1160.     { "reset",     FTP_RES, 0 },
  1161.     { "rmdir",     FTP_RMD, 0 },
  1162.     { "send",      FTP_PUT, CM_INV },
  1163.     { "site",      FTP_SIT, 0 },
  1164.     { "size",      FTP_SIZ, 0 },
  1165.     { "status",    FTP_STA, 0 },
  1166.     { "system",    FTP_SYS, 0 },
  1167.     { "type",      FTP_TYP, 0 },
  1168.     { "umask",     FTP_UMA, 0 },
  1169.     { "up",        FTP_GUP, CM_INV },
  1170.     { "user",      FTP_USR, 0 },
  1171.     { "vdirectory",FTP_VDI, 0 },
  1172.     { "", 0, 0 }
  1173. };
  1174. static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
  1175.  
  1176. #define OPN_ANO 1            /* FTP OPEN switch codes */
  1177. #define OPN_PSW 2
  1178. #define OPN_USR 3
  1179. #define OPN_ACC 4
  1180. #define OPN_ACT 5
  1181. #define OPN_PSV 6
  1182. #define OPN_TLS 7
  1183. #define OPN_NIN 8
  1184. #define OPN_NOL 9
  1185.  
  1186. #ifdef FTP_SECURITY
  1187. #ifdef CK_SSL
  1188. #define USETLSTAB
  1189. static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
  1190.     { "/ssl",       OPN_TLS, 0    },
  1191.     { "/tls",       OPN_TLS, 0    },
  1192.     { "", 0, 0 }
  1193. };
  1194. static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
  1195. #endif /* CK_SSL */
  1196. #endif /* FTP_SECURITY */
  1197.  
  1198. static struct keytab ftpswitab[] = {    /* FTP command switches */
  1199.     { "/account",   OPN_ACC, CM_ARG },
  1200.     { "/active",    OPN_ACT, 0      },
  1201.     { "/anonymous", OPN_ANO, 0      },
  1202.     { "/noinit",    OPN_NIN, 0      },
  1203.     { "/nologin",   OPN_NOL, 0      },
  1204.     { "/passive",   OPN_PSV, 0      },
  1205.     { "/password",  OPN_PSW, CM_ARG },
  1206.     { "/user",      OPN_USR, CM_ARG },
  1207.     { "", 0, 0 }
  1208. };
  1209. static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
  1210.  
  1211. /* FTP { ENABLE, DISABLE } items */
  1212.  
  1213. #define ENA_FEAT 1
  1214. #define ENA_MDTM 2
  1215. #define ENA_MLST 3
  1216. #define ENA_SIZE 4
  1217. #define ENA_AUTH 5
  1218.  
  1219. static struct keytab ftpenatab[] = {
  1220.     { "AUTH",  ENA_AUTH, 0 },
  1221.     { "FEAT",  ENA_FEAT, 0 },
  1222.     { "MDTM",  ENA_MDTM, 0 },
  1223.     { "ML",    ENA_MLST, CM_INV|CM_ABR },
  1224.     { "MLS",   ENA_MLST, CM_INV|CM_ABR },
  1225.     { "MLSD",  ENA_MLST, CM_INV },
  1226.     { "MLST",  ENA_MLST, 0 },
  1227.     { "SIZE",  ENA_SIZE, 0 },
  1228.     { "", 0, 0 }
  1229. };
  1230. static int nftpena = (sizeof(ftpenatab) / sizeof(struct keytab)) - 1;
  1231.  
  1232. /* SET FTP command keyword indices */
  1233.  
  1234. #define FTS_AUT  1                      /* Autoauthentication */
  1235. #define FTS_CRY  2                      /* Encryption */
  1236. #define FTS_LOG  3                      /* Autologin */
  1237. #define FTS_CPL  4                      /* Command protection level */
  1238. #define FTS_CFW  5                      /* Credentials forwarding */
  1239. #define FTS_DPL  6                      /* Data protection level */
  1240. #define FTS_DBG  7                      /* Debugging */
  1241. #define FTS_PSV  8                      /* Passive mode */
  1242. #define FTS_SPC  9                      /* Send port commands */
  1243. #define FTS_TYP 10                      /* (file) Type */
  1244. #define FTS_USN 11                      /* Unique server names (for files) */
  1245. #define FTS_VBM 12                      /* Verbose mode */
  1246. #define FTS_ATP 13                      /* Authentication type */
  1247. #define FTS_CNV 14                      /* Filename conversion */
  1248. #define FTS_TST 15                      /* Test (progress) messages */
  1249. #define FTS_PRM 16                      /* (file) Permissions */
  1250. #define FTS_XLA 17                      /* Charset translation */
  1251. #define FTS_CSR 18                      /* Server charset */
  1252. #define FTS_ERR 19                      /* Error action */
  1253. #define FTS_FNC 20                      /* Collision */
  1254. #define FTS_SRP 21                      /* SRP options */
  1255. #define FTS_GFT 22                      /* GET automatic file-type switching */
  1256. #define FTS_DAT 23                      /* Set file dates */
  1257. #define FTS_STO 24            /* Server time offset */
  1258. #define FTS_APW 25            /* Anonymous password */
  1259. #define FTS_DIS 26            /* File-transfer display style */
  1260. #define FTS_BUG 27                      /* Bug(s) */
  1261.  
  1262. /* FTP BUGS */
  1263.  
  1264. #define FTB_SV2  1                      /* use SSLv2 */
  1265.  
  1266. static struct keytab ftpbugtab[] = {
  1267.     { "use-ssl-v2",     FTB_SV2,        0 }
  1268. };
  1269. static int nftpbug = (sizeof(ftpbugtab) / sizeof(struct keytab));
  1270.  
  1271. /* FTP PUT options (mutually exclusive, not a bitmask) */
  1272.  
  1273. #define PUT_UPD 1                       /* Update */
  1274. #define PUT_RES 2                       /* Restart */
  1275. #define PUT_SIM 4                       /* Simulation */
  1276. #define PUT_DIF 8            /* Dates Differ */
  1277.  
  1278. static struct keytab ftpcolxtab[] = { /* SET FTP COLLISION options */
  1279. #ifndef MAC
  1280.     { "append",    XYFX_A, 0 },         /* append to old file */
  1281. #endif /* MAC */
  1282. #ifdef COMMENT
  1283.     { "ask",       XYFX_Q, 0 },         /* ask what to do (not implemented) */
  1284. #endif
  1285.     { "backup",    XYFX_B, 0 },         /* rename old file */
  1286. #ifndef MAC
  1287.     { "dates-differ", XYFX_M, 0 },    /* accept if dates differ */
  1288.     { "discard",   XYFX_D, 0 },         /* don't accept new file */
  1289.     { "no-supersede", XYFX_D, CM_INV }, /* ditto (MSK compatibility) */
  1290. #endif /* MAC */
  1291.     { "overwrite", XYFX_X, 0 },         /* overwrite the old file */
  1292.     { "rename",    XYFX_R, 0 },         /* rename the incoming file */
  1293. #ifndef MAC                             /* This crashes Mac Kermit. */
  1294.     { "update",    XYFX_U, 0 },         /* replace if newer */
  1295. #endif /* MAC */
  1296.     { "", 0, 0 }
  1297. };
  1298. static int nftpcolx = (sizeof(ftpcolxtab) / sizeof(struct keytab)) - 1;
  1299.  
  1300.  
  1301. #ifdef FTP_SECURITY
  1302. /* FTP authentication options */
  1303.  
  1304. #define FTA_AUTO 0                      /* Auto */
  1305. #define FTA_SRP  1                      /* SRP */
  1306. #define FTA_GK5  2                      /* Kerberos 5 */
  1307. #define FTA_K4   3                      /* Kerberos 4 */
  1308. #define FTA_SSL  4                      /* SSL */
  1309. #define FTA_TLS  5                      /* TLS */
  1310.  
  1311. /* FTP authentication types */
  1312.  
  1313. #define FTPATYPS 8
  1314. static int ftp_auth_type[FTPATYPS] = {
  1315. #ifdef FTP_GSSAPI
  1316.     FTA_GK5,                            /* GSSAPI Kerberos 5 */
  1317. #endif /* FTP_GK5 */
  1318. #ifdef FTP_SRP
  1319.     FTA_SRP,                            /* SRP */
  1320. #endif /* FTP_SRP */
  1321. #ifdef FTP_KRB4
  1322.     FTA_K4,                             /* Kerberos 4 */
  1323. #endif /* FTP_KRB4 */
  1324. #ifdef CK_SSL
  1325.     FTA_TLS,                            /* TLS */
  1326.     FTA_SSL,                            /* SSL */
  1327. #endif /* CK_SSL */
  1328.     0
  1329. };
  1330.  
  1331. static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
  1332.     { "automatic", FTA_AUTO,  CM_INV },
  1333. #ifdef FTP_GSSAPI
  1334.     { "gssapi-krb5", FTA_GK5, 0 },
  1335. #endif /* FTP_GSSAPI */
  1336. #ifdef FTP_KRB4
  1337.     { "k4",       FTA_K4,     CM_INV },
  1338. #endif /* FTP_KRB4 */
  1339. #ifdef FTP_GSSAPI
  1340.     { "k5",        FTA_GK5,   CM_INV },
  1341. #endif /* FTP_GSSAPI */
  1342. #ifdef FTP_KRB4
  1343.     { "kerberos4", FTA_K4,    0 },
  1344. #endif /* FTP_KRB4 */
  1345. #ifdef FTP_GSSAPI
  1346.     { "kerberos5", FTA_GK5,   CM_INV },
  1347. #endif /* FTP_GSSAPI */
  1348. #ifdef FTP_KRB4
  1349.     { "kerberos_iv",FTA_K4,   CM_INV },
  1350. #endif /* FTP_KRB4 */
  1351. #ifdef FTP_GSSAPI
  1352.     { "kerberos_v", FTA_GK5,  CM_INV },
  1353. #endif /* FTP_GSSAPI */
  1354. #ifdef FTP_KRB4
  1355.     { "krb4",     FTA_K4,     CM_INV },
  1356. #endif /* FTP_KRB4 */
  1357. #ifdef FTP_GSSAPI
  1358.     { "krb5",     FTA_GK5,    CM_INV },
  1359. #endif /* FTP_GSSAPI */
  1360. #ifdef FTP_SRP
  1361.     { "srp",      FTA_SRP,     0 },
  1362. #endif /* FTP_SRP */
  1363. #ifdef CK_SSL
  1364.     { "ssl",      FTA_SSL,     0 },
  1365.     { "tls",      FTA_TLS,     0 },
  1366. #endif /* CK_SSL */
  1367.     { "", 0, 0 }
  1368. };
  1369. static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
  1370.  
  1371. #ifdef FTP_SRP
  1372. #define SRP_CIPHER 1
  1373. #define SRP_HASH   2
  1374. static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
  1375.     { "cipher",   SRP_CIPHER,     0 },
  1376.     { "hash",     SRP_HASH,       0 },
  1377.     { "", 0, 0 }
  1378. };
  1379. static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
  1380. #endif /* FTP_SRP */
  1381. #endif /* FTP_SECURITY */
  1382.  
  1383. static struct keytab ftpset[] = {       /* SET FTP commmand table */
  1384.     { "anonymous-password",       FTS_APW, 0 },
  1385. #ifdef FTP_SECURITY
  1386.     { "authtype",                 FTS_ATP, 0 },
  1387.     { "autoauthentication",       FTS_AUT, 0 },
  1388.     { "autoencryption",           FTS_CRY, 0 },
  1389. #endif /* FTP_SECURITY */
  1390.     { "autologin",                FTS_LOG, 0 },
  1391.     { "bug",                      FTS_BUG, 0 },
  1392. #ifndef NOCSETS
  1393.     { "character-set-translation",FTS_XLA, 0 },
  1394. #endif /* NOCSETS */
  1395.     { "collision",                FTS_FNC, 0 },
  1396. #ifdef FTP_SECURITY
  1397.     { "command-protection-level", FTS_CPL, 0 },
  1398.     { "cpl",                      FTS_CPL, CM_INV },
  1399.     { "credential-forwarding",    FTS_CFW, 0 },
  1400.     { "da",                       FTS_DAT, CM_INV|CM_ABR },
  1401.     { "data-protection-level",    FTS_DPL, 0 },
  1402. #endif /* FTP_SECURITY */
  1403.     { "dates",                    FTS_DAT, 0 },
  1404.     { "debug",                    FTS_DBG, 0 },
  1405.     { "display",                  FTS_DIS, 0 },
  1406. #ifdef FTP_SECURITY
  1407.     { "dpl",                      FTS_DPL, CM_INV },
  1408. #endif /* FTP_SECURITY */
  1409.     { "error-action",             FTS_ERR, 0 },
  1410.     { "filenames",                FTS_CNV, 0 },
  1411.     { "get-filetype-switching",   FTS_GFT, 0 },
  1412.     { "passive-mode",             FTS_PSV, 0 },
  1413.     { "pasv",                     FTS_PSV, CM_INV },
  1414.     { "permissions",              FTS_PRM, 0 },
  1415.     { "progress-messages",        FTS_TST, 0 },
  1416.     { "send-port-commands",       FTS_SPC, 0 },
  1417. #ifndef NOCSETS
  1418.     { "server-character-set",     FTS_CSR, 0 },
  1419. #endif /* NOCSETS */
  1420.     { "server-time-offset",       FTS_STO, 0 },
  1421. #ifdef FTP_SRP
  1422.     { "srp",                      FTS_SRP, 0 },
  1423. #else
  1424.     { "srp",                      FTS_SRP, CM_INV },
  1425. #endif /* FTP_SRP */
  1426.     { "type",                     FTS_TYP, 0 },
  1427.     { "unique-server-names",      FTS_USN, 0 },
  1428.     { "verbose-mode",             FTS_VBM, 0 },
  1429.     { "", 0, 0 }
  1430. };
  1431. static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
  1432.  
  1433. /*
  1434.   GET and PUT switches are approximately the same as Kermit GET and SEND,
  1435.   and use the same SND_xxx definitions, but hijack a couple for FTP use.
  1436.   Don't just make up new ones, since the number of SND_xxx options must be
  1437.   known in advance for the switch-parsing arrays.
  1438. */
  1439. #define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
  1440. #define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
  1441. #define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
  1442.  
  1443. static struct keytab putswi[] = {       /* FTP PUT switch table */
  1444.     { "/after",                SND_AFT, CM_ARG },
  1445. #ifdef PUTARRAY
  1446.     { "/array",                SND_ARR, CM_ARG },
  1447. #endif /* PUTARRAY */
  1448.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1449.     { "/as-name",              SND_ASN, CM_ARG },
  1450.     { "/ascii",                SND_TXT, CM_INV },
  1451.     { "/b",                    SND_BIN, CM_INV|CM_ABR },
  1452.     { "/before",               SND_BEF, CM_ARG },
  1453.     { "/binary",               SND_BIN, 0 },
  1454. #ifdef PUTPIPE
  1455.     { "/command",              SND_CMD, CM_PSH },
  1456. #endif /* PUTPIPE */
  1457. #ifdef COMMENT
  1458. /* This works but it's dangerous */
  1459. #ifdef DOUPDATE
  1460.     { "/dates-differ",         SND_DIF, CM_INV },
  1461. #endif /* DOUPDATE */
  1462. #endif /* COMMENT */
  1463.     { "/delete",               SND_DEL, 0 },
  1464. #ifdef UNIXOROSK
  1465.     { "/dotfiles",             SND_DOT, 0 },
  1466. #endif /* UNIXOROSK */
  1467.     { "/error-action",         SND_ERR, CM_ARG },
  1468.     { "/except",               SND_EXC, CM_ARG },
  1469.     { "/filenames",            SND_NAM, CM_ARG },
  1470. #ifdef PIPESEND
  1471. #ifndef NOSPL
  1472.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1473. #endif /* NOSPL */
  1474. #endif /* PIPESEND */
  1475. #ifdef CKSYMLINK
  1476.     { "/followlinks",          SND_LNK, 0 },
  1477. #endif /* CKSYMLINK */
  1478. #ifdef VMS
  1479.     { "/image",                SND_IMG, 0 },
  1480. #else
  1481.     { "/image",                SND_BIN, CM_INV },
  1482. #endif /* VMS */
  1483.     { "/larger-than",          SND_LAR, CM_ARG },
  1484.     { "/listfile",             SND_FIL, CM_ARG },
  1485. #ifndef NOCSETS
  1486.     { "/local-character-set",  SND_CSL, CM_ARG },
  1487. #endif /* NOCSETS */
  1488. #ifdef CK_TMPDIR
  1489.     { "/move-to",              SND_MOV, CM_ARG },
  1490. #endif /* CK_TMPDIR */
  1491.     { "/nobackupfiles",        SND_NOB, 0 },
  1492. #ifdef UNIXOROSK
  1493.     { "/nodotfiles",           SND_NOD, 0 },
  1494. #endif /* UNIXOROSK */
  1495. #ifdef CKSYMLINK
  1496.     { "/nofollowlinks",        SND_NLK, 0 },
  1497. #endif /* CKSYMLINK */
  1498.  
  1499.     { "/not-after",            SND_NAF, CM_ARG },
  1500.     { "/not-before",           SND_NBE, CM_ARG },
  1501. #ifdef UNIX
  1502.     { "/permissions",          SND_PRM, CM_ARG },
  1503. #else
  1504.     { "/permissions",          SND_PRM, CM_ARG|CM_INV },
  1505. #endif /* UNIX */
  1506.     { "/quiet",                SND_SHH, 0 },
  1507. #ifdef FTP_RESTART
  1508.     { "/recover",              SND_RES, 0 },
  1509. #endif /* FTP_RESTART */
  1510. #ifdef RECURSIVE
  1511.     { "/recursive",            SND_REC, 0 },
  1512. #endif /* RECURSIVE */
  1513.     { "/rename-to",            SND_REN, CM_ARG },
  1514. #ifdef FTP_RESTART
  1515.     { "/restart",              SND_RES, CM_INV },
  1516. #endif /* FTP_RESTART */
  1517. #ifndef NOCSETS
  1518.     { "/server-character-set", SND_CSR, CM_ARG },
  1519. #endif /* NOCSETS */
  1520.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1521.     { "/simulate",             SND_SIM, 0 },
  1522.     { "/since",                SND_AFT, CM_INV|CM_ARG },
  1523.     { "/smaller-than",         SND_SMA, CM_ARG },
  1524. #ifdef COMMENT
  1525.     { "/starting-at",          SND_STA, CM_ARG },
  1526. #endif /* COMMENT */
  1527. #ifdef RECURSIVE
  1528.     { "/subdirectories",       SND_REC, CM_INV },
  1529. #endif /* RECURSIVE */
  1530.     { "/tenex",                SND_TEN, 0 },
  1531.     { "/text",                 SND_TXT, 0 },
  1532. #ifndef NOCSETS
  1533.     { "/transparent",          SND_XPA, 0 },
  1534. #endif /* NOCSETS */
  1535.     { "/type",                 SND_TYP, CM_ARG },
  1536. #ifdef DOUPDATE
  1537.     { "/update",               SND_UPD, 0 },
  1538. #endif /* DOUPDATE */
  1539.     { "/unique-server-names",  SND_USN, 0 },
  1540.     { "", 0, 0 }
  1541. };
  1542. static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
  1543.  
  1544. static struct keytab getswi[] = {       /* FTP [M]GET switch table */
  1545.     { "/after",                SND_AFT, CM_INV },
  1546.     { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
  1547.     { "/as-name",              SND_ASN, CM_ARG },
  1548.     { "/ascii",                SND_TXT, CM_INV },
  1549.     { "/before",               SND_BEF, CM_INV },
  1550.     { "/binary",               SND_BIN, 0 },
  1551.     { "/collision",            SND_COL, CM_ARG },
  1552. #ifdef PUTPIPE
  1553.     { "/command",              SND_CMD, CM_PSH },
  1554. #endif /* PUTPIPE */
  1555.     { "/delete",               SND_DEL, 0 },
  1556.     { "/error-action",         SND_ERR, CM_ARG },
  1557.     { "/except",               SND_EXC, CM_ARG },
  1558.     { "/filenames",            SND_NAM, CM_ARG },
  1559. #ifdef PIPESEND
  1560. #ifndef NOSPL
  1561.     { "/filter",               SND_FLT, CM_ARG|CM_PSH },
  1562. #endif /* NOSPL */
  1563. #endif /* PIPESEND */
  1564. #ifdef VMS
  1565.     { "/image",                SND_IMG, 0 },
  1566. #else
  1567.     { "/image",                SND_BIN, CM_INV },
  1568. #endif /* VMS */
  1569.     { "/larger-than",          SND_LAR, CM_ARG },
  1570.     { "/listfile",             SND_FIL, CM_ARG },
  1571. #ifndef NOCSETS
  1572.     { "/local-character-set",  SND_CSL, CM_ARG },
  1573. #endif /* NOCSETS */
  1574.     { "/match",                SND_PAT, CM_ARG },
  1575.     { "/ml",                   SND_MLS, CM_INV|CM_ABR },
  1576.     { "/mls",                  SND_MLS, CM_INV|CM_ABR },
  1577.     { "/mlsd",                 SND_MLS, 0 },
  1578.     { "/mlst",                 SND_MLS, CM_INV },
  1579. #ifdef CK_TMPDIR
  1580.     { "/move-to",              SND_MOV, CM_ARG },
  1581. #endif /* CK_TMPDIR */
  1582.     { "/namelist",             SND_NML, CM_ARG },
  1583.     { "/nlst",                 SND_NLS, 0 },
  1584.     { "/nobackupfiles",        SND_NOB, 0 },
  1585.     { "/nodotfiles",           SND_NOD, 0 },
  1586. #ifdef DOUPDATE
  1587.     { "/dates-differ",         SND_DIF, CM_INV },
  1588. #endif /* DOUPDATE */
  1589.     { "/not-after",            SND_NAF, CM_INV },
  1590.     { "/not-before",           SND_NBE, CM_INV },
  1591.     { "/permissions",          SND_PRM, CM_INV },
  1592.     { "/quiet",                SND_SHH, 0 },
  1593. #ifdef FTP_RESTART
  1594.     { "/recover",              SND_RES, 0 },
  1595. #endif /* FTP_RESTART */
  1596. #ifdef RECURSIVE
  1597.     { "/recursive",            SND_REC, 0 },
  1598. #endif /* RECURSIVE */
  1599.     { "/rename-to",            SND_REN, CM_ARG },
  1600. #ifdef FTP_RESTART
  1601.     { "/restart",              SND_RES, CM_INV },
  1602. #endif /* FTP_RESTART */
  1603. #ifndef NOCSETS
  1604.     { "/server-character-set", SND_CSR, CM_ARG },
  1605. #endif /* NOCSETS */
  1606.     { "/server-rename-to",     SND_SRN, CM_ARG },
  1607.     { "/smaller-than",         SND_SMA, CM_ARG },
  1608. #ifdef RECURSIVE
  1609.     { "/subdirectories",       SND_REC, CM_INV },
  1610. #endif /* RECURSIVE */
  1611.     { "/text",                 SND_TXT, 0 },
  1612.     { "/tenex",                SND_TEN, 0 },
  1613. #ifndef NOCSETS
  1614.     { "/transparent",          SND_XPA, 0 },
  1615. #endif /* NOCSETS */
  1616.     { "/to-screen",            SND_MAI, 0 },
  1617. #ifdef DOUPDATE
  1618.     { "/update",               SND_UPD, CM_INV },
  1619. #endif /* DOUPDATE */
  1620.     { "", 0, 0 }
  1621. };
  1622. static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
  1623.  
  1624. static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
  1625.     { "/error-action",         SND_ERR, CM_ARG },
  1626.     { "/except",               SND_EXC, CM_ARG },
  1627.     { "/filenames",            SND_NAM, CM_ARG },
  1628.     { "/larger-than",          SND_LAR, CM_ARG },
  1629.     { "/nobackupfiles",        SND_NOB, 0 },
  1630. #ifdef UNIXOROSK
  1631.     { "/nodotfiles",           SND_NOD, 0 },
  1632. #endif /* UNIXOROSK */
  1633.     { "/quiet",                SND_SHH, 0 },
  1634. #ifdef RECURSIVE
  1635.     { "/recursive",            SND_REC, 0 },
  1636. #endif /* RECURSIVE */
  1637.     { "/smaller-than",         SND_SMA, CM_ARG },
  1638. #ifdef RECURSIVE
  1639.     { "/subdirectories",       SND_REC, CM_INV },
  1640. #endif /* RECURSIVE */
  1641.     { "", 0, 0 }
  1642. };
  1643. static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
  1644.  
  1645. static struct keytab fntab[] = {        /* Filename conversion keyword table */
  1646.     { "automatic",    2, CNV_AUTO },
  1647.     { "converted",    1, CNV_CNV  },
  1648.     { "literal",      0, CNV_LIT  }
  1649. };
  1650. static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
  1651.  
  1652. static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
  1653.     { "ascii",        FTT_ASC, 0 },
  1654.     { "binary",       FTT_BIN, 0 },
  1655.     { "tenex",        FTT_TEN, 0 },
  1656.     { "text",         FTT_ASC, CM_INV },
  1657.     { "", 0, 0 }
  1658. };
  1659. static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
  1660.  
  1661. #ifdef FTP_SECURITY
  1662. static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
  1663.     { "clear",        FPL_CLR, 0 },
  1664.     { "confidential", FPL_CON, 0 },
  1665.     { "private",      FPL_PRV, 0 },
  1666.     { "safe",         FPL_SAF, 0 },
  1667.     { "", 0, 0 }
  1668. };
  1669. static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
  1670. #endif /* FTP_SECURITY */
  1671.  
  1672. /* Definitions for FTP from RFC765. */
  1673.  
  1674. /* Reply codes */
  1675.  
  1676. #define REPLY_PRELIM    1               /* Positive preliminary */
  1677. #define REPLY_COMPLETE  2               /* Positive completion */
  1678. #define REPLY_CONTINUE  3               /* Positive intermediate */
  1679. #define REPLY_TRANSIENT 4               /* Transient negative completion */
  1680. #define REPLY_ERROR     5               /* Permanent negative completion */
  1681. #define REPLY_SECURE    6               /* Security encoded message */
  1682.  
  1683. /* Form codes and names */
  1684.  
  1685. #define FORM_N 1                        /* Non-print */
  1686. #define FORM_T 2                        /* Telnet format effectors */
  1687. #define FORM_C 3                        /* Carriage control (ASA) */
  1688.  
  1689. /* Structure codes and names */
  1690.  
  1691. #define STRU_F 1                        /* File (no record structure) */
  1692. #define STRU_R 2                        /* Record structure */
  1693. #define STRU_P 3                        /* Page structure */
  1694.  
  1695. /* Mode types and names */
  1696.  
  1697. #define MODE_S 1                        /* Stream */
  1698. #define MODE_B 2                        /* Block */
  1699. #define MODE_C 3                        /* Compressed */
  1700.  
  1701. /* Protection levels and names */
  1702.  
  1703. #define PROT_C 1                        /* Clear */
  1704. #define PROT_S 2                        /* Safe */
  1705. #define PROT_P 3                        /* Private */
  1706. #define PROT_E 4                        /* Confidential */
  1707.  
  1708. #ifdef COMMENT                          /* Not used */
  1709. #ifdef FTP_NAMES
  1710. char *strunames[]  =  {"0", "File",     "Record", "Page" };
  1711. char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
  1712. char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
  1713. char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
  1714. #endif /* FTP_NAMES */
  1715.  
  1716. /* Record Tokens */
  1717.  
  1718. #define REC_ESC '\377'                  /* Record-mode Escape */
  1719. #define REC_EOR '\001'                  /* Record-mode End-of-Record */
  1720. #define REC_EOF '\002'                  /* Record-mode End-of-File */
  1721.  
  1722. /* Block Header */
  1723.  
  1724. #define BLK_EOR           0x80          /* Block is End-of-Record */
  1725. #define BLK_EOF           0x40          /* Block is End-of-File */
  1726. #define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
  1727. #define BLK_RESTART       0x10          /* Block is Restart Marker */
  1728. #define BLK_BYTECOUNT 2                 /* Bytes in this block */
  1729. #endif /* COMMENT */
  1730.  
  1731. #define RADIX_ENCODE 0                  /* radix_encode() function codes */
  1732. #define RADIX_DECODE 1
  1733.  
  1734. /*
  1735.   The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  1736.   results in a serious performance degradation due to the increased number
  1737.   of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  1738.   network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  1739.   of the typical TCP window.  Maybe we should add a command to allow the value
  1740.   to be changed.
  1741. */
  1742. #define DEFAULT_PBSZ 1<<13
  1743.  
  1744. /* Prototypes */
  1745.  
  1746. _PROTOTYP(int remtxt, (char **) );
  1747. _PROTOTYP(char * gskreason, (int) );
  1748. _PROTOTYP(static int ftpclose,(void));
  1749. _PROTOTYP(static int zzsend, (int, CHAR));
  1750. _PROTOTYP(static int getreply,(int,int,int,int,int));
  1751. _PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
  1752. _PROTOTYP(static int setpbsz,(unsigned int));
  1753. _PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  1754.   int,int,char *,int,int,int));
  1755. _PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
  1756. _PROTOTYP(static int fts_cpl,(int));
  1757. _PROTOTYP(static int fts_dpl,(int));
  1758. #ifdef FTP_SECURITY
  1759. _PROTOTYP(static int ftp_auth, (void));
  1760. #endif /* FTP_SECURITY */
  1761. _PROTOTYP(static int ftp_user, (char *, char *, char *));
  1762. _PROTOTYP(static int ftp_login, (char *));
  1763. _PROTOTYP(static int ftp_reset, (void));
  1764. _PROTOTYP(static int ftp_rename, (char *, char *));
  1765. _PROTOTYP(static int ftp_umask, (char *));
  1766. _PROTOTYP(static int secure_flush, (int));
  1767. #ifdef COMMENT
  1768. _PROTOTYP(static int secure_putc, (char, int));
  1769. #endif /* COMMENT */
  1770. _PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
  1771. _PROTOTYP(static int scommand, (char *));
  1772. _PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
  1773. _PROTOTYP(static int secure_getc, (int, int));
  1774. _PROTOTYP(static int secure_getbyte, (int, int));
  1775. _PROTOTYP(static int secure_read, (int, char *, int));
  1776. _PROTOTYP(static int initconn, (void));
  1777. _PROTOTYP(static int dataconn, (char *));
  1778. _PROTOTYP(static int setprotbuf,(unsigned int));
  1779. _PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
  1780.  
  1781. _PROTOTYP(static char * radix_error,(int));
  1782. _PROTOTYP(static char * ftp_hookup,(char *, int, int));
  1783. _PROTOTYP(static CHAR * remote_files, (int, CHAR *, CHAR *, int));
  1784.  
  1785. _PROTOTYP(static VOID mlsreset, (void));
  1786. _PROTOTYP(static VOID secure_error, (char *fmt, ...));
  1787. _PROTOTYP(static VOID lostpeer, (void));
  1788. _PROTOTYP(static VOID cancel_remote, (int));
  1789. _PROTOTYP(static VOID changetype, (int, int));
  1790.  
  1791. _PROTOTYP(static sigtype cmdcancel, (int));
  1792.  
  1793. #ifdef FTP_SRP
  1794. _PROTOTYP(static int srp_reset, ());
  1795. _PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
  1796. _PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
  1797. _PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
  1798. _PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
  1799. _PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
  1800. _PROTOTYP(static int srp_selcipher, (char *));
  1801. _PROTOTYP(static int srp_selhash, (char *));
  1802. #endif /* FTP_SRP */
  1803.  
  1804. #ifdef FTP_GSSAPI
  1805. _PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
  1806. #endif /* FTP_GSSAPI */
  1807.  
  1808. /*  D O F T P A R G  --  Do an FTP command-line argument.  */
  1809.  
  1810. #ifdef FTP_SECURITY
  1811. #ifndef NOICP
  1812. #define FT_NOGSS   1
  1813. #define FT_NOK4    2
  1814. #define FT_NOSRP   3
  1815. #define FT_NOSSL   4
  1816. #define FT_NOTLS   5
  1817. #define FT_CERTFI  6
  1818. #define FT_OKCERT  7
  1819. #define FT_DEBUG   8
  1820. #define FT_KEY     9
  1821. #define FT_SECURE 10
  1822. #define FT_VERIFY 11
  1823.  
  1824. static struct keytab ftpztab[] = {
  1825.     { "!gss",    FT_NOGSS,  0 },
  1826.     { "!krb4",   FT_NOK4,   0 },
  1827.     { "!srp",    FT_NOSRP,  0 },
  1828.     { "!ssl",    FT_NOSSL,  0 },
  1829.     { "!tls",    FT_NOTLS,  0 },
  1830.     { "cert",    FT_CERTFI, CM_ARG },
  1831.     { "certsok", FT_OKCERT, 0 },
  1832.     { "debug",   FT_DEBUG,  0 },
  1833.     { "key",     FT_KEY,    CM_ARG },
  1834.     { "nogss",   FT_NOGSS,  0 },
  1835.     { "nokrb4",  FT_NOK4,   0 },
  1836.     { "nosrp",   FT_NOSRP,  0 },
  1837.     { "nossl",   FT_NOSSL,  0 },
  1838.     { "notls",   FT_NOTLS,  0 },
  1839. #ifdef COMMENT
  1840.     { "secure",  FT_SECURE, 0 },
  1841. #endif /* COMMENT */
  1842.     { "verify",  FT_VERIFY, CM_ARG },
  1843.     { "", 0, 0 }
  1844. };
  1845. static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
  1846.  
  1847. /*
  1848.   The following cipher and hash tables should be replaced with
  1849.   dynamicly created versions based upon the linked library.
  1850. */
  1851. #define SRP_BLOWFISH_ECB    1
  1852. #define SRP_BLOWFISH_CBC    2
  1853. #define SRP_BLOWFISH_CFB64  3
  1854. #define SRP_BLOWFISH_OFB64  4
  1855. #define SRP_CAST5_ECB       5
  1856. #define SRP_CAST5_CBC       6
  1857. #define SRP_CAST5_CFB64     7
  1858. #define SRP_CAST5_OFB64     8
  1859. #define SRP_DES_ECB         9
  1860. #define SRP_DES_CBC        10
  1861. #define SRP_DES_CFB64      11
  1862. #define SRP_DES_OFB64      12
  1863. #define SRP_DES3_ECB       13
  1864. #define SRP_DES3_CBC       14
  1865. #define SRP_DES3_CFB64     15
  1866. #define SRP_DES3_OFB64     16
  1867.  
  1868. static struct keytab ciphertab[] = {
  1869.     { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
  1870.     { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
  1871.     { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
  1872.     { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
  1873.     { "cast5_ecb",      SRP_CAST5_ECB,      0 },
  1874.     { "cast5_cbc",      SRP_CAST5_CBC,      0 },
  1875.     { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
  1876.     { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
  1877.     { "des_ecb",        SRP_DES_ECB,        0 },
  1878.     { "des_cbc",        SRP_DES_CBC,        0 },
  1879.     { "des_cfb64",      SRP_DES_CFB64,      0 },
  1880.     { "des_ofb64",      SRP_DES_OFB64,      0 },
  1881.     { "des3_ecb",       SRP_DES3_ECB,       0 },
  1882.     { "des3_cbc",       SRP_DES3_CBC,       0 },
  1883.     { "des3_cfb64",     SRP_DES3_CFB64,     0 },
  1884.     { "des3_ofb64",     SRP_DES3_OFB64,     0 },
  1885.     { "none",           0, 0 },
  1886.     { "", 0, 0 }
  1887. };
  1888. static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
  1889.  
  1890. #define SRP_MD5  1
  1891. #define SRP_SHA  2
  1892. static struct keytab hashtab[] = {
  1893.     { "md5",              SRP_MD5,        0 },
  1894.     { "none",             0,              0 },
  1895.     { "sha",              SRP_SHA,        0 },
  1896.     { "", 0, 0 }
  1897. };
  1898. static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
  1899. #endif /* NOICP */
  1900. #endif /* FTP_SECURITY */
  1901.  
  1902. static char *
  1903. strval(s1,s2) char * s1, * s2; {
  1904.     if (!s1) s1 = "";
  1905.     if (!s2) s2 = "";
  1906.     return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
  1907. }
  1908.  
  1909. #ifndef NOCSETS
  1910. static char * rfnptr = NULL;
  1911. static int rfnlen = 0;
  1912. static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
  1913. static char * xgnbp = NULL;
  1914.  
  1915. static int
  1916. strgetc() {                             /* Helper function for xgnbyte() */
  1917.     int c;
  1918.     if (!xgnbp)
  1919.       return(-1);
  1920.     if (!*xgnbp)
  1921.       return(-1);
  1922.     c = (unsigned) *xgnbp++;
  1923.     return(((unsigned) c) & 0xff);
  1924. }
  1925.  
  1926. static int                              /* Helper function for xpnbyte() */
  1927. #ifdef CK_ANSIC
  1928. strputc(char c)
  1929. #else
  1930. strputc(c) char c;
  1931. #endif /* CK_ANSIC */
  1932. {
  1933.     rfnlen = rfnptr - rfnbuf;
  1934.     if (rfnlen >= (RFNBUFSIZ - 1))
  1935.       return(-1);
  1936.     *rfnptr++ = c;
  1937.     *rfnptr = NUL;
  1938.     return(0);
  1939. }
  1940.  
  1941. static int
  1942. #ifdef CK_ANSIC
  1943. xprintc(char c)
  1944. #else
  1945. xprintc(c) char c;
  1946. #endif /* CK_ANSIC */
  1947. {
  1948.     printf("%c",c);
  1949.     return(0);
  1950. }
  1951.  
  1952. static VOID
  1953. bytswap(c0,c1) int * c0, * c1; {
  1954.     int t;
  1955.     t = *c0;
  1956.     *c0 = *c1;
  1957.     *c1 = t;
  1958. }
  1959. #endif /* NOCSETS */
  1960.  
  1961. #ifdef CKLOGDIAL
  1962. char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
  1963. int ftplogactive = 0;
  1964. long ftplogprev = 0L;
  1965.  
  1966. VOID
  1967. ftplogend() {
  1968.     extern int dialog;
  1969.     extern char diafil[];
  1970.     long d1, d2, t1, t2;
  1971.     char buf[32], * p;
  1972.  
  1973.     debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
  1974.     debug(F110,"ftp cx log buf",ftplogbuf,0);
  1975.  
  1976.     if (!ftplogactive || !ftplogbuf[0]) /* No active record */
  1977.       return;
  1978.  
  1979.     ftplogactive = 0;                   /* Record is not active */
  1980.  
  1981.     d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
  1982.     ckstrncpy(buf,ckdate(),31);         /* Get current date */
  1983.     d2 = mjd(buf);                      /* Convert them to mjds */
  1984.     p = ftplogbuf;                      /* Get start time */
  1985.     p[11] = NUL;
  1986.     p[14] = NUL;                        /* Convert to seconds */
  1987.     t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1988.     p[11] = ':';
  1989.     p[14] = ':';
  1990.     p = buf;                            /* Get end time */
  1991.     p[11] = NUL;
  1992.     p[14] = NUL;
  1993.     t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
  1994.     t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
  1995.     if (t2 > -1L) {
  1996.         ftplogprev = t2;
  1997.         p = hhmmss(t2);
  1998.         ckstrncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
  1999.         ckstrncat(ftplogbuf,p,CXLOGBUFL);
  2000.     } else
  2001.       ftplogprev = 0L;
  2002.     debug(F101,"ftp cx log dialog","",dialog);
  2003.     if (dialog) {                       /* If logging */
  2004.         int x;
  2005.         x = diaopn(diafil,1,1);         /* Open log in append mode */
  2006.         if (x > 0) {
  2007.             debug(F101,"ftp cx log open","",x);
  2008.             x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
  2009.             debug(F101,"ftp cx log write","",x);
  2010.             x = zclose(ZDIFIL);         /* Close the log */
  2011.             debug(F101,"ftp cx log close","",x);
  2012.         }
  2013.     }
  2014. }
  2015.  
  2016. VOID
  2017. dologftp() {
  2018.     ftplogend();                        /* Previous session not closed out? */
  2019.     ftplogprev = 0L;
  2020.     ftplogactive = 1;                   /* Record is active */
  2021.  
  2022.     ckmakxmsg(ftplogbuf,CXLOGBUFL,
  2023.               ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
  2024.               " T=FTP N=", strval(ftp_host,NULL)," H=",myhost," ",NULL,NULL);
  2025.     debug(F110,"ftp cx log begin",ftplogbuf,0);
  2026. }
  2027. #endif /* CKLOGDIAL */
  2028.  
  2029. static char * dummy[2] = { NULL, NULL };
  2030.  
  2031. static struct keytab modetab[] = {
  2032.     { "active",  0, 0 },
  2033.     { "passive", 1, 0 }
  2034. };
  2035.  
  2036. #ifndef NOCMDL
  2037. int                                     /* Called from ckuusy.c */
  2038. #ifdef CK_ANSIC
  2039. doftparg(char c)
  2040. #else
  2041. doftparg(c) char c;
  2042. #endif /* CK_ANSIC */
  2043. /* doftparg */ {
  2044.     int x, z;
  2045.     char *xp;
  2046.     extern char **xargv, *xarg0;
  2047.     extern int xargc, stayflg, haveftpuid;
  2048.     extern char uidbuf[];
  2049.  
  2050.     xp = *xargv+1;                      /* Pointer for bundled args */
  2051.     while (c) {
  2052.         if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
  2053.             if (*(xp+1)) {
  2054.                 fatal("?Invalid argument bundling");
  2055.             }
  2056.             xargv++, xargc--;
  2057.             if ((xargc < 1) || (**xargv == '-')) {
  2058.                 fatal("?Required argument missing");
  2059.             }
  2060.         }
  2061.         switch (c) {                    /* Big switch on arg */
  2062.           case 'h':                     /* help */
  2063.            printf("C-Kermit's FTP client command-line personality.  Usage:\n");
  2064.             printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
  2065.             printf("Options:\n");
  2066.             printf("  -h           = help (this message)\n");
  2067.             printf("  -m mode      = \"passive\" (default) or \"active\"\n");
  2068.             printf("  -u name      = username for autologin (or -M)\n");
  2069.             printf("  -P password  = password for autologin (RISKY)\n");
  2070.             printf("  -A           = autologin anonymously\n");
  2071.             printf("  -D directory = cd after autologin\n");
  2072.             printf("  -b           = force binary mode\n");
  2073.             printf("  -a           = force text (\"ascii\") mode (or -T)\n");
  2074.             printf("  -d           = debug (double to add timestamps)\n");
  2075.             printf("  -n           = no autologin\n");
  2076.             printf("  -v           = verbose (default)\n");
  2077.             printf("  -q           = quiet\n");
  2078.             printf("  -S           = Stay (issue command prompt when done)\n");
  2079.             printf("  -Y           = do not execute Kermit init file\n");
  2080.             printf("  -p files     = files to put after autologin (or -s)\n");
  2081.             printf("  -g files     = files to get after autologin\n");
  2082.             printf("  -R           = recursive (for use with -p)\n");
  2083.  
  2084. #ifdef FTP_SECURITY
  2085.             printf("\nSecurity options:\n");
  2086.             printf("  -k realm     = Kerberos 4 realm\n");
  2087.             printf("  -f           = Kerboros 5 credentials forwarding\n");
  2088.             printf("  -x           = autoencryption mode\n");
  2089.             printf("  -c cipher    = SRP cipher type\n");
  2090.             printf("  -H hash      = SRP encryption hash\n");
  2091.             printf("  -z option    = Security options\n");
  2092. #endif /* FTP_SECURITY */
  2093.  
  2094.             printf("\n-p or -g, if given, should be last.  Example:\n");
  2095.             printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
  2096.  
  2097.             doexit(GOOD_EXIT,-1);
  2098.             break;
  2099.  
  2100.           case 'R':                     /* Recursive */
  2101.             recursive = 1;
  2102.             break;
  2103.  
  2104.           case 'd':                     /* Debug */
  2105. #ifdef DEBUG
  2106.             if (deblog) {
  2107.                 extern int debtim;
  2108.                 debtim = 1;
  2109.             } else {
  2110.                 deblog = debopn("debug.log",0);
  2111.                 debok = 1;
  2112.             }
  2113. #endif /* DEBUG */
  2114.             /* fall thru on purpose */
  2115.  
  2116.           case 't':                     /* Trace */
  2117.             ftp_deb++;
  2118.             break;
  2119.  
  2120.           case 'n':                     /* No autologin */
  2121.             ftp_log = 0;
  2122.             break;
  2123.  
  2124.           case 'i':                     /* No prompt */
  2125.           case 'v':                     /* Verbose */
  2126.             break;                      /* (ignored) */
  2127.  
  2128.           case 'q':                     /* Quiet */
  2129.             quiet = 1;
  2130.             break;
  2131.  
  2132.           case 'S':                     /* Stay */
  2133.             stayflg = 1;
  2134.             break;
  2135.  
  2136.           case 'M':
  2137.           case 'u':                     /* My User Name */
  2138.             if ((int)strlen(*xargv) > 63) {
  2139.                 fatal("username too long");
  2140.             }
  2141.             ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
  2142.             haveftpuid = 1;
  2143.             break;
  2144.  
  2145.           case 'A':
  2146.             ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2147.             haveftpuid = 1;
  2148.             break;
  2149.  
  2150.           case 'T':                     /* Text */
  2151.           case 'a':                     /* "ascii" */
  2152.           case 'b':                     /* Binary */
  2153.             binary = (c == 'b') ? FTT_BIN : FTT_ASC;
  2154.             xfermode = XMODE_M;
  2155.             filepeek = 0;
  2156.             patterns = 0;
  2157.             break;
  2158.  
  2159.           case 'g':                     /* Get */
  2160.           case 'p':                     /* Put */
  2161.           case 's': {                   /* Send (= Put) */
  2162.               int havefiles, rc;
  2163.               if (ftp_action) {
  2164.                   fatal("Only one FTP action at a time please");
  2165.               }
  2166.               if (*(xp+1)) {
  2167.                   fatal("invalid argument bundling after -s");
  2168.               }
  2169.               nfils = 0;                /* Initialize file counter */
  2170.               havefiles = 0;            /* Assume nothing to send  */
  2171.               cmlist = xargv + 1;       /* Remember this pointer */
  2172.  
  2173.               while (++xargv, --xargc > 0) { /* Traverse the list */
  2174.                   if (c == 'g') {
  2175.                       havefiles++;
  2176.                       nfils++;
  2177.                       continue;
  2178.                   }
  2179. #ifdef RECURSIVE
  2180.                   if (!strcmp(*xargv,".")) {
  2181.                       havefiles = 1;
  2182.                       nfils++;
  2183.                       recursive = 1;
  2184.                   } else
  2185. #endif /* RECURSIVE */
  2186.                     if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
  2187.                         if  (rc != -2)
  2188.                           havefiles = 1;
  2189.                         nfils++;
  2190.                     } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
  2191.                         havefiles = 1;
  2192.                         nfils++;
  2193.                     }
  2194.               }
  2195.               xargc++, xargv--;         /* Adjust argv/argc */
  2196.               if (!havefiles) {
  2197.                   if (c == 'g') {
  2198.                       fatal("No files to put");
  2199.                   } else {
  2200.                       fatal("No files to get");
  2201.                   }
  2202.               }
  2203.               ftp_action = c;
  2204.               break;
  2205.           }
  2206.           case 'D':                     /* Directory */
  2207.             makestr(&ftp_rdir,*xargv);
  2208.             break;
  2209.  
  2210.           case 'm':                     /* Mode (Active/Passive */
  2211.             ftp_psv = lookup(modetab,*xargv,2,NULL);
  2212.             if (ftp_psv < 0) fatal("Invalid mode");
  2213.             break;
  2214.  
  2215.           case 'P':
  2216.             makestr(&ftp_tmp,*xargv);   /* You-Know-What */
  2217.             break;
  2218.  
  2219.           case 'Y':                     /* No initialization file */
  2220.             break;                      /* (already done in prescan) */
  2221.  
  2222. #ifdef CK_URL
  2223.           case 'U': {                   /* URL */
  2224.               /* These are set by urlparse() - any not set are NULL */
  2225.               if (g_url.hos) {
  2226. /*
  2227.   Kermit has accepted host:port notation since many years before URLs were
  2228.   invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  2229.   host:449" looks like a URL and results in service = host and host = 449.
  2230.   Here we try to catch this situation transparently to the user.
  2231. */
  2232.                   if (ckstrcmp(g_url.svc,"ftp",-1,0)
  2233. #ifdef CK_SSL
  2234.                        && ckstrcmp(g_url.svc,"ftps",-1,0)
  2235. #endif /* CK_SSL */
  2236.                        ) {
  2237.                       if (!g_url.usr &&
  2238.                           !g_url.psw &&
  2239.                           !g_url.por &&
  2240.                           !g_url.pth) {
  2241.                           g_url.por = g_url.hos;
  2242.                           g_url.hos = g_url.svc;
  2243.                           g_url.svc = "ftp";
  2244.                       } else {
  2245.                           ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
  2246.                                    g_url.svc," host=",g_url.hos);
  2247.                           fatal(tmpbuf);
  2248.                       }
  2249.                   }
  2250.                   makestr(&ftp_host,g_url.hos);
  2251.                   if (g_url.usr) {
  2252.                       haveftpuid = 1;
  2253.                       ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
  2254.                       makestr(&ftp_logname,uidbuf);
  2255.                   }
  2256.                   if (g_url.psw) {
  2257.                       makestr(&ftp_tmp,g_url.psw);
  2258.                   }
  2259.                   if (g_url.pth) {
  2260.                       if (!g_url.usr) {
  2261.                           haveftpuid = 1;
  2262.                           ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  2263.                           makestr(&ftp_logname,uidbuf);
  2264.                       }
  2265.                       if (ftp_action) {
  2266.                           fatal("Only one FTP action at a time please");
  2267.                       }
  2268.                       if (!stayflg)
  2269.                         quiet = 1;
  2270.                       nfils = 1;
  2271.                       dummy[0] = g_url.pth;
  2272.                       cmlist = dummy;
  2273.                       ftp_action = 'g';
  2274.                   }
  2275.                   xp = NULL;
  2276.                   haveurl = 1;
  2277.               }
  2278.               break;
  2279.           }
  2280. #endif /* CK_URL */
  2281.  
  2282. #ifdef FTP_SECURITY
  2283.           case 'k': {                   /* K4 Realm */
  2284. #ifdef FTP_KRB4
  2285.               ckstrncpy(ftp_realm,*xargv, REALM_SZ);
  2286. #endif /* FTP_KRB4 */
  2287.               if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
  2288.               break;
  2289.           }
  2290.           case 'f': {
  2291. #ifdef FTP_GSSAPI
  2292.               ftp_cfw = 1;
  2293.               if (ftp_deb) printf("K5 Credentials Forwarding\n");
  2294. #else /* FTP_GSSAPI */
  2295.               printf("K5 Credentials Forwarding not supported\n");
  2296. #endif /* FTP_GSSAPI */
  2297.               break;
  2298.           }
  2299.           case 'x': {
  2300.               ftp_cry = 1;
  2301.               if (ftp_deb) printf("Autoencryption\n");
  2302.               break;
  2303.           }
  2304.           case 'c': {                   /* Cipher */
  2305. #ifdef FTP_SRP
  2306.               if (!srp_selcipher(*xargv)) {
  2307.                   if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
  2308.               } else
  2309.                 printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
  2310. #else /* FTP_SRP */
  2311.               printf("?SRP not supported\n");
  2312. #endif /* FTP_SRP */
  2313.               break;
  2314.           }
  2315.           case 'H': {
  2316. #ifdef FTP_SRP
  2317.               if (!srp_selhash(*xargv)) {
  2318.                   if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
  2319.               } else
  2320.                 printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
  2321. #else /* FTP_SRP */
  2322.               printf("?SRP not supported\n");
  2323. #endif /* FTP_SRP */
  2324.               break;
  2325.           }
  2326.           case 'z': {
  2327.               /* *xargv contains a value of the form tag=value */
  2328.               /* we need to lookup the tag and save the value  */
  2329.               char * p = NULL, * q = NULL;
  2330.               makestr(&p,*xargv);
  2331.               y = ckindex("=",p,0,0,1);
  2332.               if (y > 0)
  2333.                 p[y-1] = '\0';
  2334.               x = lookup(ftpztab,p,nftpztab,&z);
  2335.               if (x < 0) {
  2336.                   printf("?Invalid security option: \"%s\"\n",p);
  2337.               } else {
  2338.                   if (ftp_deb)
  2339.             printf("Security option: \"%s",p);
  2340.                   if (ftpztab[z].flgs & CM_ARG) {
  2341.                       if (y <= 0)
  2342.                         fatal("?Missing required value");
  2343.                       q = &p[y];
  2344.                       if (!*q)
  2345.                         fatal("?Missing required value");
  2346.                       if (ftp_deb)
  2347.             printf("=%s\"",q);
  2348.                   }
  2349.                   switch (ftpztab[z].kwval) { /* -z options w/args */
  2350.                     case FT_NOGSS:
  2351. #ifdef FTP_GSSAPI
  2352.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2353.                           if (ftp_auth_type[z] == FTA_GK5) {
  2354.                               for (y = z;
  2355.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2356.                                    y++
  2357.                                    )
  2358.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2359.                               ftp_auth_type[FTPATYPS-1] = 0;
  2360.                               break;
  2361.                           }
  2362.                       }
  2363. #endif /* FTP_GSSAPI */
  2364.                       break;
  2365.                     case FT_NOK4:
  2366. #ifdef FTP_KRB4
  2367.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2368.                           if (ftp_auth_type[z] == FTA_K4) {
  2369.                               for (y = z;
  2370.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2371.                                    y++
  2372.                                    )
  2373.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2374.                               ftp_auth_type[FTPATYPS-1] = 0;
  2375.                               break;
  2376.                           }
  2377.                       }
  2378. #endif /* FTP_KRB4 */
  2379.                       break;
  2380.                     case FT_NOSRP:
  2381. #ifdef FTP_SRP
  2382.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2383.                           if (ftp_auth_type[z] == FTA_SRP) {
  2384.                               for (y = z;
  2385.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2386.                                    y++
  2387.                                    )
  2388.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2389.                               ftp_auth_type[FTPATYPS-1] = 0;
  2390.                               break;
  2391.                           }
  2392.                       }
  2393. #endif /* FTP_SRP */
  2394.                       break;
  2395.                     case FT_NOSSL:
  2396. #ifdef CK_SSL
  2397.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2398.                           if (ftp_auth_type[z] == FTA_SSL) {
  2399.                               for (y = z;
  2400.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2401.                                    y++
  2402.                                    )
  2403.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2404.                               ftp_auth_type[FTPATYPS-1] = 0;
  2405.                               break;
  2406.                           }
  2407.                       }
  2408. #endif /* CK_SSL */
  2409.                       break;
  2410.                     case FT_NOTLS:
  2411. #ifdef CK_SSL
  2412.                       for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
  2413.                           if (ftp_auth_type[z] == FTA_TLS) {
  2414.                               for (y = z;
  2415.                                    y < (FTPATYPS-1) && ftp_auth_type[y];
  2416.                                    y++
  2417.                                    )
  2418.                                 ftp_auth_type[y] = ftp_auth_type[y+1];
  2419.                               ftp_auth_type[FTPATYPS-1] = 0;
  2420.                               break;
  2421.                           }
  2422.                       }
  2423. #endif /* CK_SSL */
  2424.                       break;
  2425.                     case FT_CERTFI:
  2426. #ifdef CK_SSL
  2427.                       makestr(&ssl_rsa_cert_file,q);
  2428. #endif /* CK_SSL */
  2429.                       break;
  2430.                     case FT_OKCERT:
  2431. #ifdef CK_SSL
  2432.                       ssl_certsok_flag = 1;
  2433. #endif /* CK_SSL */
  2434.                       break;
  2435.                     case FT_DEBUG:
  2436. #ifdef DEBUG
  2437.                       if (deblog) {
  2438.                           extern int debtim;
  2439.                           debtim = 1;
  2440.                       } else {
  2441.                           deblog = debopn("debug.log",0);
  2442.                       }
  2443. #endif /* DEBUG */
  2444.                       break;
  2445.                     case FT_KEY:
  2446. #ifdef CK_SSL
  2447.                       makestr(&ssl_rsa_key_file,q);
  2448. #endif /* CK_SSL */
  2449.                       break;
  2450.                     case FT_SECURE:
  2451.                       /* no equivalent */
  2452.                       break;
  2453.                     case FT_VERIFY:
  2454. #ifdef CK_SSL
  2455.                       if (!rdigits(q))
  2456.                         printf("?Bad number: %s\n",q);
  2457.                       ssl_verify_flag = atoi(q);
  2458. #endif /* CK_SSL */
  2459.                       break;
  2460.                   }
  2461.               }
  2462.               if (ftp_deb) printf("\"\n");
  2463.               free(p);
  2464.               break;
  2465.           }
  2466. #endif /* FTP_SECURITY */
  2467.  
  2468.           default:
  2469.             fatal2(*xargv,
  2470.                    "unknown command-line option, type \"ftp -h\" for help"
  2471.                    );
  2472.         }
  2473.         if (!xp) break;
  2474.         c = *++xp;                      /* See if options are bundled */
  2475.     }
  2476.     return(0);
  2477. }
  2478. #endif /* NOCMDL */
  2479.  
  2480. int
  2481. ftpisconnected() {
  2482.     return(connected);
  2483. }
  2484.  
  2485. int
  2486. ftpisloggedin() {
  2487.     return(connected ? loggedin : 0);
  2488. }
  2489.  
  2490. int
  2491. ftpissecure() {
  2492.     return((ftp_dpl == FPL_CLR && !ssl_ftp_proxy) ? 0 : 1);
  2493. }
  2494.  
  2495. static VOID
  2496. ftscreen(n, c, z, s) int n; char c; CK_OFF_T z; char * s; {
  2497.     if (displa && fdispla && !backgrd && !quiet && !out2screen) {
  2498.         if (!dpyactive) {
  2499.             ckscreen(SCR_PT,'S',(CK_OFF_T)0,"");
  2500.             dpyactive = 1;
  2501.         }
  2502.         ckscreen(n,c,z,s);
  2503.     }
  2504. }
  2505.  
  2506. #ifndef OS2
  2507. /*  g m s t i m e r  --  Millisecond timer */
  2508.  
  2509. long
  2510. gmstimer() {
  2511. #ifdef HAVE_MSECS
  2512.     /* For those versions of ztime() that also set global ztmsec. */
  2513.     char *p = NULL;
  2514.     long z;
  2515.     ztime(&p);
  2516.     if (!p) return(0L);
  2517.     if (!*p) return(0L);
  2518.     z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
  2519.     return(z * 1000 + ztmsec);
  2520. #else
  2521.     return((long)time(NULL) * 1000L);
  2522. #endif /* HAVE_MSECS */
  2523. }
  2524. #endif /* OS2 */
  2525.  
  2526. /*  d o s e t f t p  --  The SET FTP command  */
  2527.  
  2528. int
  2529. dosetftp() {
  2530.     int cx;
  2531.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
  2532.       return(cx);
  2533.     switch (cx) {
  2534.  
  2535.       case FTS_FNC:                     /* Filename collision action */
  2536.         if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  2537.           return(x);
  2538.         if ((y = cmcfm()) < 0)
  2539.           return(y);
  2540.         ftp_fnc = x;
  2541.         return(1);
  2542.  
  2543.       case FTS_CNV:                     /* Filename conversion */
  2544.         if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  2545.           return(x);
  2546.         if ((y = cmcfm()) < 0)
  2547.           return(y);
  2548.         ftp_cnv = x;
  2549.         return(1);
  2550.  
  2551.       case FTS_DBG:                     /* Debug messages */
  2552.         return(seton(&ftp_deb));
  2553.  
  2554.       case FTS_LOG:                     /* Auto-login */
  2555.         return(seton(&ftp_log));
  2556.  
  2557.       case FTS_PSV:                     /* Passive mode */
  2558.     return(dosetftppsv());
  2559.  
  2560.       case FTS_SPC:                     /* Send port commands */
  2561.         x = seton(&ftp_spc);
  2562.         if (x > 0) sendport = ftp_spc;
  2563.         return(x);
  2564.  
  2565.       case FTS_TYP:                     /* Type */
  2566.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  2567.           return(x);
  2568.         if ((y = cmcfm()) < 0) return(y);
  2569.         ftp_typ = x;
  2570.         g_ftp_typ = x;
  2571.         tenex = (ftp_typ == FTT_TEN);
  2572.         return(1);
  2573.  
  2574.       case FTS_USN:                     /* Unique server names */
  2575.         return(seton(&ftp_usn));
  2576.  
  2577.       case FTS_VBM:                     /* Verbose mode */
  2578.         if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
  2579.           return(x);
  2580.         ftp_vbx = ftp_vbm;              /* Global sticky copy */
  2581.         return(x);
  2582.  
  2583.       case FTS_TST:                     /* "if (testing)" messages */
  2584.         return(seton(&testing));
  2585.  
  2586.       case FTS_PRM:                     /* Send permissions */
  2587.         return(setonaut(&ftp_prm));
  2588.  
  2589.       case FTS_AUT:                     /* Auto-authentication */
  2590.         return(seton(&ftp_aut));
  2591.  
  2592.       case FTS_ERR:                     /* Error action */
  2593.         if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  2594.           return(x);
  2595.         if ((y = cmcfm()) < 0)
  2596.           return(y);
  2597.         ftp_err = x;
  2598.         return(success = 1);
  2599.  
  2600. #ifndef NOCSETS
  2601.       case FTS_XLA:                     /* Translation */
  2602.         return(seton(&ftp_xla));
  2603.  
  2604.       case FTS_CSR:                     /* Server charset */
  2605.         if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
  2606.           return(x);
  2607.         if ((y = cmcfm()) < 0)
  2608.           return(y);
  2609.         ftp_csr = x;
  2610.         ftp_xla = 1;                    /* Also enable translation */
  2611.         return(success = 1);
  2612. #endif /* NOCSETS */
  2613.  
  2614.       case FTS_GFT:
  2615.         return(seton(&get_auto));       /* GET-filetype-switching */
  2616.  
  2617.       case FTS_DAT:
  2618.         return(seton(&ftp_dates));      /* Set file dates */
  2619.  
  2620.       case FTS_STO: {            /* Server time offset */
  2621.       char * s, * p = NULL;
  2622.       long k;
  2623.       if ((x = cmfld("[+-]hh[:mm[:ss]]","+0",&s,xxstring)) < 0)
  2624.         return(x);
  2625.       if (!strcmp(s,"+0")) {
  2626.           s = NULL;
  2627.       } else if ((x = delta2sec(s,&k)) < 0) { /* Check format */
  2628.           printf("?Invalid time offset\n");
  2629.           return(-9);
  2630.       }
  2631.       makestr(&p,s);        /* Make a safe copy the string */
  2632.       if ((x = cmcfm()) < 0) {    /* Get confirmation */
  2633.           if (p)
  2634.         makestr(&p,NULL);
  2635.           return(x);
  2636.       }
  2637.       fts_sto = p;            /* Confirmed - set the string. */
  2638.       return(success = 1);
  2639.       }
  2640.       case FTS_APW: {
  2641.       char * s;
  2642.       if ((x = cmtxt("Text", "", &s, xxstring)) < 0)
  2643.         return(x);
  2644.       makestr(&ftp_apw, *s ? s : NULL);
  2645.       return(success = 1);
  2646.       }
  2647.  
  2648.       case FTS_BUG: {
  2649.           if ((x = cmkey(ftpbugtab,nftpbug,"","",xxstring)) < 0) 
  2650.         return(x);
  2651.           switch (x) {
  2652. #ifdef CK_SSL
  2653.           case FTB_SV2:
  2654.         return seton(&ftp_bug_use_ssl_v2);
  2655. #endif /* CK_SSL */
  2656.           default:
  2657.         return(-2);
  2658.           }
  2659.       }
  2660.  
  2661. #ifdef FTP_SECURITY
  2662.       case FTS_CRY:                     /* Auto-encryption */
  2663.         return(seton(&ftp_cry));
  2664.  
  2665.       case FTS_CFW:                     /* Credential-forwarding */
  2666.         return(seton(&ftp_cfw));
  2667.  
  2668.       case FTS_CPL:                     /* Command protection level */
  2669.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2670.         if ((y = cmcfm()) < 0) return(y);
  2671.         success = fts_cpl(x);
  2672.         return(success);
  2673.  
  2674.       case FTS_DPL:                     /* Data protection level */
  2675.         if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
  2676.         if ((y = cmcfm()) < 0) return(y);
  2677.           success = fts_dpl(x);
  2678.           return(success);
  2679.  
  2680.       case FTS_ATP: {                   /* FTP Auth Type */
  2681.           int i, j, atypes[8];
  2682.  
  2683.           for (i = 0; i < 8; i++) {
  2684.               if ((y = cmkey(ftpauth,nftpauth,"",
  2685.                              (i == 0) ? "automatic" : "",
  2686.                              xxstring)) < 0) {
  2687.                   if (y == -3)
  2688.                     break;
  2689.                   return(y);
  2690.               }
  2691.               if (i > 0 && (y == FTA_AUTO)) {
  2692.                   printf("?Choice may only be used in first position.\r\n");
  2693.                   return(-9);
  2694.               }
  2695.               for (j = 0; j < i; j++) {
  2696.                   if (atypes[j] == y) {
  2697.                       printf("\r\n?Choice has already been used.\r\n");
  2698.                       return(-9);
  2699.                   }
  2700.               }
  2701.               atypes[i] = y;
  2702.               if (y == FTA_AUTO) {
  2703.                   i++;
  2704.                   break;
  2705.               }
  2706.           }
  2707.           if (i < 8)
  2708.             atypes[i] = 0;
  2709.           if ((z = cmcfm()) < 0)
  2710.             return(z);
  2711.           if (atypes[0] == FTA_AUTO) {
  2712.               i = 0;
  2713. #ifdef FTP_GSSAPI
  2714.               ftp_auth_type[i++] = FTA_GK5;
  2715. #endif /* FTP_GSSAPI */
  2716. #ifdef FTP_SRP
  2717.               ftp_auth_type[i++] = FTA_SRP;
  2718. #endif /* FTP_SRP */
  2719. #ifdef FTP_KRB4
  2720.               ftp_auth_type[i++] = FTA_K4;
  2721. #endif /* FTP_KRB4 */
  2722. #ifdef CK_SSL
  2723.               ftp_auth_type[i++] = FTA_TLS;
  2724.               ftp_auth_type[i++] = FTA_SSL;
  2725. #endif /* CK_SSL */
  2726.               ftp_auth_type[i] = 0;
  2727.           } else {
  2728.               for (i = 0; i < 8; i++)
  2729.                 ftp_auth_type[i] = atypes[i];
  2730.           }
  2731.           return(success = 1);
  2732.       }
  2733.  
  2734.       case FTS_SRP:
  2735. #ifdef FTP_SRP
  2736.         if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
  2737.           return(x);
  2738.         switch (x) {
  2739.           case SRP_CIPHER:
  2740.             if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
  2741.               return(x);
  2742.             if ((z = cmcfm()) < 0)
  2743.               return(z);
  2744.             success = !srp_selcipher(ciphertab[x].kwd);
  2745.             return(success);
  2746.           case SRP_HASH:
  2747.             if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
  2748.               return(x);
  2749.             if ((z = cmcfm()) < 0)
  2750.               return(z);
  2751.             success = !srp_selhash(hashtab[x].kwd);
  2752.             return(success = 1);
  2753.           default:
  2754.             if ((z = cmcfm()) < 0)
  2755.               return(z);
  2756.             return(-2);
  2757.         }
  2758. #else /* FTP_SRP */
  2759.         if ((z = cmcfm()) < 0)
  2760.           return(z);
  2761.         return(-2);
  2762. #endif /* FTP_SRP */
  2763. #endif /* FTP_SECURITY */
  2764.  
  2765.       case FTS_DIS:
  2766.     doxdis(2);            /* 2 == ftp */
  2767.         return(success = 1);
  2768.  
  2769.       default:
  2770.         return(-2);
  2771.     }
  2772. }
  2773.  
  2774. int
  2775. ftpbye() {
  2776.     int x;
  2777.     if (!connected)
  2778.       return(1);
  2779.     if (testing)
  2780.       printf(" ftp closing %s...\n",ftp_host);
  2781.     x = ftpclose();
  2782.     return((x > -1) ? 1 : 0);
  2783. }
  2784.  
  2785. /*  o p e n f t p  --  Parse FTP hostname & port and open */
  2786.  
  2787. static int
  2788. openftp(s,opn_tls) char * s; int opn_tls; {
  2789.     char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
  2790.     int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1, nologin = 0;
  2791.     int haveuser = 0;
  2792.     struct FDB sw, fl, cm;
  2793.     extern int nnetdir;                 /* Network services directory */
  2794.     extern int nhcount;                 /* Lookup result */
  2795.     extern char *nh_p[];                /* Network directory entry pointers */
  2796.     extern char *nh_p2[];               /* Network directory entry nettype */
  2797.  
  2798.     if (!s) return(-2);
  2799.     if (!*s) return(-2);
  2800.  
  2801.     makestr(&hostname,s);
  2802.     hostsave = hostname;
  2803.     makestr(&ftp_logname,NULL);
  2804.     anonymous = 0;
  2805.     noinit = 0;
  2806.  
  2807.     debug(F110,"ftp open",hostname,0);
  2808.  
  2809.     if (sav_psv > -1) {                 /* Restore prevailing active/passive */
  2810.         ftp_psv = sav_psv;              /* selection in case it was */
  2811.         sav_psv = -1;                   /* temporarily overriden by a switch */
  2812.     }
  2813.     if (sav_log > -1) {                 /* Ditto for autologin */
  2814.         ftp_log = sav_log;
  2815.         sav_log = -1;
  2816.     }
  2817.     cmfdbi(&sw,                         /* Switches */
  2818.            _CMKEY,
  2819.            "Service name or port;\n or switch",
  2820.            "",                          /* default */
  2821.            "",                          /* addtl string data */
  2822.            nftpswi,                     /* addtl numeric data 1: tbl size */
  2823.            4,                           /* addtl numeric data 2: none */
  2824.            xxstring,                    /* Processing function */
  2825.            ftpswitab,                   /* Keyword table */
  2826.            &fl                          /* Pointer to next FDB */
  2827.            );
  2828.     cmfdbi(&fl,                         /* A host name or address */
  2829.            _CMFLD,                      /* fcode */
  2830.            "",                          /* help */
  2831.            "xYzBoo",                    /* default */
  2832.            "",                          /* addtl string data */
  2833.            0,                           /* addtl numeric data 1 */
  2834.            0,                           /* addtl numeric data 2 */
  2835.            xxstring,
  2836.            NULL,
  2837.            &cm
  2838.            );
  2839.     cmfdbi(&cm,                         /* Command confirmation */
  2840.            _CMCFM,
  2841.            "",
  2842.            "",
  2843.            "",
  2844.            0,
  2845.            0,
  2846.            NULL,
  2847.            NULL,
  2848.            NULL
  2849.            );
  2850.  
  2851.     for (n = 0;; n++) {
  2852.         rc = cmfdb(&sw);                /* Parse a service name or a switch */
  2853.         if (rc < 0)
  2854.           goto xopenftp;
  2855.  
  2856.         if (cmresult.fcode == _CMCFM) { /* Done? */
  2857.             break;
  2858.         } else if (cmresult.fcode == _CMFLD) {  /* Port */
  2859.             if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
  2860.               makestr(&service,cmresult.sresult);
  2861.             else
  2862.               makestr(&service,opn_tls?"ftps":"ftp");
  2863.         } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
  2864.             c = cmgbrk();               /* get break character */
  2865.             getval = (c == ':' || c == '=');
  2866.             rc = -9;
  2867.             if (getval && !(cmresult.kflags & CM_ARG)) {
  2868.                 printf("?This switch does not take arguments\n");
  2869.                 goto xopenftp;
  2870.             }
  2871.             if (!getval && (cmresult.kflags & CM_ARG)) {
  2872.                 printf("?This switch requires an argument\n");
  2873.                 goto xopenftp;
  2874.             }
  2875.             switch (cmresult.nresult) { /* Switch */
  2876.               case OPN_ANO:             /* /ANONYMOUS */
  2877.                 anonymous++;
  2878.         nologin = 0;
  2879.                 break;
  2880.               case OPN_NIN:             /* /NOINIT */
  2881.                 noinit++;
  2882.                 break;
  2883.               case OPN_NOL:             /* /NOLOGIN */
  2884.                 nologin++;
  2885.         anonymous = 0;
  2886.         makestr(&ftp_logname,NULL);
  2887.                 break;
  2888.               case OPN_PSW:             /* /PASSWORD */
  2889.                 if (!anonymous)         /* Don't log real passwords */
  2890.                   debok = 0;
  2891.                 rc = cmfld("Password for FTP server","",&p,xxstring);
  2892.                 if (rc == -3) {
  2893.                     makestr(&ftp_tmp,NULL);
  2894.                 } else if (rc < 0) {
  2895.                     goto xopenftp;
  2896.                 } else {
  2897.                     makestr(&ftp_tmp,brstrip(p));
  2898.             nologin = 0;
  2899.                 }
  2900.                 break;
  2901.               case OPN_USR:             /* /USER */
  2902.                 rc = cmfld("Username for FTP server","",&p,xxstring);
  2903.                 if (rc == -3) {
  2904.                     makestr(&ftp_logname,NULL);
  2905.                 } else if (rc < 0) {
  2906.                     goto xopenftp;
  2907.                 } else {
  2908.             nologin = 0;
  2909.                     anonymous = 0;
  2910.             haveuser = 1;
  2911.                     makestr(&ftp_logname,brstrip(p));
  2912.                 }
  2913.                 break;
  2914.               case OPN_ACC:
  2915.                 rc = cmfld("Account for FTP server","",&p,xxstring);
  2916.                 if (rc == -3) {
  2917.                     makestr(&ftp_acc,NULL);
  2918.                 } else if (rc < 0) {
  2919.                     goto xopenftp;
  2920.                 } else {
  2921.                     makestr(&ftp_acc,brstrip(p));
  2922.                 }
  2923.                 break;
  2924.               case OPN_ACT:
  2925.                 opn_psv = 0;
  2926.                 break;
  2927.               case OPN_PSV:
  2928.                 opn_psv = 1;
  2929.                 break;
  2930.               case OPN_TLS:
  2931.                 opn_tls = 1;
  2932.                 break;
  2933.               default:
  2934.                 break;
  2935.             }
  2936.         }
  2937.         if (n == 0) {                   /* After first time through */
  2938.             cmfdbi(&sw,                 /* accept only switches */
  2939.                    _CMKEY,
  2940.                    "\nCarriage return to confirm to command, or switch",
  2941.                    "",
  2942.                    "",
  2943.                    nftpswi,
  2944.                    4,
  2945.                    xxstring,
  2946.                    ftpswitab,
  2947.                    &cm
  2948.                    );
  2949.         }
  2950.     }
  2951. #ifdef COMMENT
  2952.     debug(F100,"ftp openftp while exit","",0);
  2953.     rc = cmcfm();
  2954.     debug(F101,"ftp openftp cmcfm rc","",rc);
  2955.     if (rc < 0)
  2956.       goto xopenftp;
  2957. #endif /* COMMENT */
  2958.  
  2959.     if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
  2960.         sav_psv = ftp_psv;
  2961.         ftp_psv = opn_psv;
  2962.     }
  2963.     if (nologin || haveuser) {        /* /NOLOGIN or /USER switch given */
  2964.     sav_log = ftp_log;
  2965.     ftp_log = haveuser ? 1 : 0;
  2966.     }
  2967.     if (*hostname == '=') {             /* Bypass directory lookup */
  2968.         hostname++;                     /* if hostname starts with '=' */
  2969.         havehost++;
  2970.     } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
  2971.         havehost++;
  2972.     }
  2973.     if (!service)
  2974.       makestr(&service,opn_tls?"ftps":"ftp");
  2975.  
  2976. #ifndef NODIAL
  2977.     if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
  2978.         lunet(hostname);                /* Look up the name */
  2979.         debug(F111,"ftp openftp lunet",hostname,nhcount);
  2980.         if (nhcount == 0) {
  2981.             if (testing)
  2982.               printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2983.             success = ftpopen(hostname,service,opn_tls);
  2984.             debug(F101,"ftp openftp A ftpopen success","",success);
  2985.             rc = success;
  2986.         } else {
  2987.             int found = 0;
  2988.             for (i = 0; i < nhcount; i++) {
  2989.                 if (nh_p2[i])           /* If network type specified */
  2990.                   if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
  2991.                     continue;
  2992.                 found++;
  2993.                 makestr(&hostname,nh_p[i]);
  2994.                 debug(F111,"ftpopen lunet substitution",hostname,i);
  2995.                 if (testing)
  2996.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  2997.                 success = ftpopen(hostname,service,opn_tls);
  2998.                 debug(F101,"ftp openftp B ftpopen success","",success);
  2999.                 rc = success;
  3000.                 if (success)
  3001.                   break;
  3002.             }
  3003.             if (!found) {               /* E.g. if no network types match */
  3004.                 if (testing)
  3005.                   printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3006.                 success = ftpopen(hostname,service,opn_tls);
  3007.                 debug(F101,"ftp openftp C ftpopen success","",success);
  3008.                 rc = success;
  3009.             }
  3010.         }
  3011.     } else {
  3012. #endif /* NODIAL */
  3013.         if (testing)
  3014.           printf(" ftp open trying \"%s %s\"...\n",hostname,service);
  3015.         success = ftpopen(hostname,service,opn_tls);
  3016.         debug(F111,"ftp openftp D ftpopen success",hostname,success);
  3017.         debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
  3018.         rc = success;
  3019. #ifndef NODIAL
  3020.     }
  3021. #endif /* NODIAL */
  3022.  
  3023.   xopenftp:
  3024.     debug(F101,"ftp openftp xopenftp rc","",rc);
  3025.     if (hostsave) free(hostsave);
  3026.     if (service) free(service);
  3027.     if (rc < 0 && ftp_logname) {
  3028.         free(ftp_logname);
  3029.         ftp_logname = NULL;
  3030.     }
  3031.     if (ftp_tmp) {
  3032.         free(ftp_tmp);
  3033.         ftp_tmp = NULL;
  3034.     }
  3035.     return(rc);
  3036. }
  3037.  
  3038. int
  3039. doftpacct() {
  3040.     int x;
  3041.     char * s;
  3042.     if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  3043.       return(x);
  3044.     CHECKCONN();
  3045.     makestr(&ftp_acc,brstrip(s));
  3046.     if (testing)
  3047.       printf(" ftp account: \"%s\"\n",ftp_acc);
  3048.     success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  3049.     return(success);
  3050. }
  3051.  
  3052. int
  3053. doftpusr() {                            /* Log in as USER */
  3054.     extern char uidbuf[];
  3055.     extern char pwbuf[];
  3056.     extern int  pwflg, pwcrypt;
  3057.     int x;
  3058.     char *s, * acct = "";
  3059.  
  3060.     debok = 0;                          /* Don't log */
  3061.  
  3062.     if ((x = cmfld("Remote username or ID",uidbuf,&s,xxstring)) < 0)
  3063.       return(x);
  3064.     ckstrncpy(line,brstrip(s),LINBUFSIZ); /* brstrip: 15 Jan 2003 */
  3065.     if ((x = cmfld("Remote password","",&s,xxstring)) < 0) {
  3066.         if (x == -3) { /* no input */
  3067.             if ( pwbuf[0] && pwflg ) {
  3068.                 ckstrncpy(tmpbuf,(char *)pwbuf,TMPBUFSIZ);
  3069. #ifdef OS2
  3070.                 if ( pwcrypt )
  3071.                     ck_encrypt((char *)tmpbuf);
  3072. #endif /* OS2 */
  3073.             }
  3074.         } else {
  3075.             return(x);
  3076.         }
  3077.     } else {
  3078.         ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
  3079.     }
  3080.     if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
  3081.                    "", &s, xxstring)) < 0)
  3082.       return(x);
  3083.     CHECKCONN();
  3084.     if (*s) {
  3085.         x = strlen(tmpbuf);
  3086.         if (x > 0) {
  3087.             acct = &tmpbuf[x+2];
  3088.             ckstrncpy(acct,brstrip(s),TMPBUFSIZ - x - 2);
  3089.         }
  3090.     }
  3091.     if (testing)
  3092.       printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
  3093.     success = ftp_user(line,tmpbuf,acct);
  3094. #ifdef CKLOGDIAL
  3095.     dologftp();
  3096. #endif /* CKLOGDIAL */
  3097.     return(success);
  3098. }
  3099.  
  3100. /* DO (various FTP commands)... */
  3101.  
  3102. int
  3103. doftptyp(type) int type; {              /* TYPE */
  3104.     CHECKCONN();
  3105.     ftp_typ = type;
  3106.     changetype(ftp_typ,ftp_vbm);
  3107.     return(1);
  3108. }
  3109.  
  3110. static int
  3111. doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
  3112.     int lcs = -1, rcs = -1;
  3113. #ifndef NOCSETS
  3114.     if (ftp_xla) {
  3115.         lcs = ftp_csl;
  3116.         if (lcs < 0) lcs = fcharset;
  3117.         rcs = ftp_csx;
  3118.         if (rcs < 0) rcs = ftp_csr;
  3119.     }
  3120. #endif /* NOCSETS */
  3121.     debug(F110,"ftp doftpmkd",s,0);
  3122.     if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3123.       return(success = 1);
  3124.     if (ftpcode == 500 || ftpcode == 502) {
  3125.         if (!quiet)
  3126.           printf("MKD command not recognized, trying XMKD\n");
  3127.         if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3128.           return(success = 1);
  3129.     }
  3130.     return(success = 0);
  3131. }
  3132.  
  3133. static int
  3134. doftpmkd() {                            /* MKDIR parse */
  3135.     int x;
  3136.     char * s;
  3137.     if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
  3138.       return(x);
  3139.     CHECKCONN();
  3140.     ckstrncpy(line,s,LINBUFSIZ);
  3141.     if (testing)
  3142.       printf(" ftp mkdir \"%s\"...\n",line);
  3143.     return(success = doftpxmkd(line,-1));
  3144. }
  3145.  
  3146. static int
  3147. doftprmd() {                            /* RMDIR */
  3148.     int x, lcs = -1, rcs = -1;
  3149.     char * s;
  3150.     if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  3151.       return(x);
  3152.     CHECKCONN();
  3153.     ckstrncpy(line,s,LINBUFSIZ);
  3154.     if (testing)
  3155.       printf(" ftp rmdir \"%s\"...\n",line);
  3156. #ifndef NOCSETS
  3157.     if (ftp_xla) {
  3158.         lcs = ftp_csl;
  3159.         if (lcs < 0) lcs = fcharset;
  3160.         rcs = ftp_csx;
  3161.         if (rcs < 0) rcs = ftp_csr;
  3162.     }
  3163. #endif /* NOCSETS */
  3164.     if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
  3165.       return(success = 1);
  3166.     if (ftpcode == 500 || ftpcode == 502) {
  3167.         if (!quiet)
  3168.           printf("RMD command not recognized, trying XMKD\n");
  3169.         success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  3170.     } else
  3171.       success = 0;
  3172.     return(success);
  3173. }
  3174.  
  3175. static int
  3176. doftpren() {                            /* RENAME */
  3177.     int x;
  3178.     char * s;
  3179.     if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
  3180.       return(x);
  3181.     ckstrncpy(line,s,LINBUFSIZ);
  3182.     if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
  3183.       return(x);
  3184.     ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  3185.     if ((x = cmcfm()) < 0)
  3186.       return(x);
  3187.     CHECKCONN();
  3188.     if (testing)
  3189.       printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
  3190.     success = ftp_rename(line,tmpbuf);
  3191.     return(success);
  3192. }
  3193.  
  3194. int
  3195. doftpres() {                            /* RESET (log out without close) */
  3196.     int x;
  3197.     if ((x = cmcfm()) < 0)
  3198.       return(x);
  3199.     CHECKCONN();
  3200.     if (testing)
  3201.       printf(" ftp reset...\n");
  3202.     return(success = ftp_reset());
  3203. }
  3204.  
  3205. static int
  3206. doftpxhlp() {                           /* HELP */
  3207.     int x;
  3208.     char * s;
  3209.     if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
  3210.       return(x);
  3211.     CHECKCONN();
  3212.     ckstrncpy(line,s,LINBUFSIZ);
  3213.     if (testing)
  3214.       printf(" ftp help \"%s\"...\n",line);
  3215.     /* No need to translate -- all FTP commands are ASCII */
  3216.     return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
  3217. }
  3218.  
  3219. static int
  3220. doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
  3221.     int x, lcs = 0, rcs = 0, xlate = 0;
  3222.     char * p, * s, * m = "";
  3223.     if (cx == FTP_VDI) {
  3224.         switch (servertype) {
  3225.           case SYS_VMS:
  3226.           case SYS_DOS:
  3227.           case SYS_TOPS10:
  3228.           case SYS_TOPS20:
  3229.             m = "*.*";
  3230.             break;
  3231.           default:
  3232.             m = "*";
  3233.         }
  3234.     }
  3235.     if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
  3236.       return(x);
  3237.     if ((x = remtxt(&s)) < 0)
  3238.       return(x);
  3239. #ifdef NOCSETS
  3240.     xlate = 0;
  3241. #else
  3242.     xlate = ftp_xla;
  3243. #endif /* NOCSETS */
  3244.     line[0] = NUL;
  3245.     ckstrncpy(line,s,LINBUFSIZ);
  3246.     s = line;
  3247.     CHECKCONN();
  3248.  
  3249. #ifndef NOCSETS
  3250.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  3251.         lcs = ftp_csl;                  /* Local charset */
  3252.         if (lcs < 0) lcs = fcharset;
  3253.         if (lcs < 0) xlate = 0;
  3254.     }
  3255.     if (xlate) {                        /* Still ON? */
  3256.         rcs = ftp_csx;                  /* Remote (Server) charset */
  3257.         if (rcs < 0) rcs = ftp_csr;
  3258.         if (rcs < 0) xlate = 0;
  3259.     }
  3260. #endif /* NOCSETS */
  3261.  
  3262.     if (testing) {
  3263.         p = s;
  3264.         if (!p) p = "";
  3265.         if (*p)
  3266.           printf("Directory of files %s at %s:\n", line, ftp_host);
  3267.         else
  3268.           printf("Directory of files at %s:\n", ftp_host);
  3269.     }
  3270.     debug(F111,"doftpdir",s,cx);
  3271.  
  3272.     if (cx == FTP_DIR) {
  3273.         /* Translation of line[] is done inside recvrequest() */
  3274.         /* when it calls ftpcmd(). */
  3275.         return(success =
  3276.           (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
  3277.     }
  3278.     success = 1;                        /* VDIR - one file at a time... */
  3279.     p = (char *)remote_files(1,(CHAR *)s,NULL,0); /* Get the file list */
  3280.     cancelgroup = 0;
  3281.     if (!ftp_vbm && !quiet)
  3282.       printlines = 1;
  3283.     while (p && !cancelfile && !cancelgroup) { /* STAT one file */
  3284.         if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
  3285.             success = 0;
  3286.             break;
  3287.         }
  3288.         p = (char *)remote_files(0,NULL,NULL,0); /* Get next file */
  3289.         debug(F110,"ftp vdir file",s,0);
  3290.     }
  3291.     return(success);
  3292. }
  3293.  
  3294. static int
  3295. doftppwd() {                            /* PWD */
  3296.     int x, lcs = -1, rcs = -1;
  3297. #ifndef NOCSETS
  3298.     if (ftp_xla) {
  3299.         lcs = ftp_csl;
  3300.         if (lcs < 0) lcs = fcharset;
  3301.         rcs = ftp_csx;
  3302.         if (rcs < 0) rcs = ftp_csr;
  3303.     }
  3304. #endif /* NOCSETS */
  3305.     if ((x = cmcfm()) < 0)
  3306.       return(x);
  3307.     CHECKCONN();
  3308.     if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
  3309.         success = 1;
  3310.     } else if (ftpcode == 500 || ftpcode == 502) {
  3311.         if (ftp_deb)
  3312.           printf("PWD command not recognized, trying XPWD\n");
  3313.         success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
  3314.     }
  3315.     return(success);
  3316. }
  3317.  
  3318. static int
  3319. doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
  3320.     int lcs = -1, rcs = -1;
  3321. #ifndef NOCSETS
  3322.     if (ftp_xla) {
  3323.         lcs = ftp_csl;
  3324.         if (lcs < 0) lcs = fcharset;
  3325.         rcs = ftp_csx;
  3326.         if (rcs < 0) rcs = ftp_csr;
  3327.     }
  3328. #endif /* NOCSETS */
  3329.  
  3330.     debug(F110,"ftp doftpcwd",s,0);
  3331.     if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3332.       return(success = 1);
  3333.     if (ftpcode == 500 || ftpcode == 502) {
  3334.         if (!quiet)
  3335.           printf("CWD command not recognized, trying XCWD\n");
  3336.         if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
  3337.           return(success = 1);
  3338.     }
  3339.     return(success = 0);
  3340. }
  3341.  
  3342. static int
  3343. doftpcdup() {                           /* CDUP */
  3344.     debug(F100,"ftp doftpcdup","",0);
  3345.     if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
  3346.       return(success = 1);
  3347.     if (ftpcode == 500 || ftpcode == 502) {
  3348.         if (!quiet)
  3349.           printf("CDUP command not recognized, trying XCUP\n");
  3350.         if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
  3351.           return(success = 1);
  3352.     }
  3353.     return(success = 0);
  3354. }
  3355.  
  3356. /* s y n c d i r  --  Synchronizes client & server directories */
  3357.  
  3358. /* Used with recursive PUTs; Returns 0 on failure, 1 on success */
  3359.  
  3360. static int cdlevel = 0, cdsimlvl = 0;
  3361.  
  3362. static int
  3363. syncdir(local,sim) char * local; int sim; {
  3364.     char buf[CKMAXPATH+1];
  3365.     char tmp[CKMAXPATH+1];
  3366.     char msgbuf[CKMAXPATH+64];
  3367.     char c, * p = local, * s = buf, * q = buf;
  3368.     int i, k = 0, done = 0, itsadir = 0, saveq;
  3369.  
  3370.     debug(F110,"ftp syncdir local (new)",local,0);
  3371.     debug(F110,"ftp syncdir putpath (old)",putpath,0);
  3372.  
  3373.     itsadir = isdir(local);
  3374.     saveq = quiet;
  3375.  
  3376.     while ((*s = *p)) {                 /* Copy the argument filename */
  3377.         if (++k == CKMAXPATH)           /* so we can poke it. */
  3378.           return(-1);
  3379.         if (*s == '/')                  /* Pointer to rightmost dirsep */
  3380.           q = s;
  3381.         s++;
  3382.         p++;
  3383.     }
  3384.     if (!itsadir)
  3385.       *q = NUL;                         /* Keep just the path part */
  3386.  
  3387.     debug(F110,"ftp syncdir buf",buf,0);
  3388.     if (!strcmp(buf,putpath)) {         /* Same as for previous file? */
  3389.         if (itsadir) {                  /* It's a directory? */
  3390.             if (doftpcwd(local,0)) {    /* Try to CD to it */
  3391.                 doftpcdup();            /* Worked - CD back up */
  3392.             } else if (sim) {           /* Simulating... */
  3393.                 if (fdispla == XYFD_B) {
  3394.                     printf("WOULD CREATE DIRECTORY %s\n",local);
  3395.                 } else if (fdispla) {
  3396.                     ckmakmsg(msgbuf,CKMAXPATH,
  3397.                              "WOULD CREATE DIRECTORY",local,NULL,NULL);
  3398.                     ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3399.                 }
  3400.                 /* See note above */
  3401.                 return(0);
  3402.             } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
  3403.                 return(0);
  3404.             } else {
  3405.                 if (fdispla == XYFD_B) {
  3406.                     printf("CREATED DIRECTORY %s\n",local);
  3407.                 } else if (fdispla) {
  3408.                     ckmakmsg(msgbuf,CKMAXPATH+64,
  3409.                              "CREATED DIRECTORY ",local,NULL,NULL);
  3410.                     ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3411.                 }
  3412.             }
  3413.         }
  3414.         debug(F110,"ftp syncdir no change",buf,0);
  3415.         return(1);                      /* Yes, done. */
  3416.     }
  3417.     ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
  3418.     debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
  3419.  
  3420.     p = buf;                            /* New */
  3421.     s = putpath;                        /* Old */
  3422.  
  3423.     debug(F110,"ftp syncdir A p",p,0);
  3424.     debug(F110,"ftp syncdir A s",s,0);
  3425.  
  3426.     while (*p != NUL && *s != NUL && *p == *s) p++,s++;
  3427.  
  3428.     if (*s == '/' && !*p) s++;          /* Don't count initial slash */
  3429.  
  3430.     debug(F110,"ftp syncdir B p",p,0);
  3431.     debug(F110,"ftp syncdir B s",s,0);
  3432.  
  3433.     /* p and s now point to the leftmost spot where they differ */
  3434.  
  3435.     if (*s) {                           /* We have to back up */
  3436.         k = 1;                          /* How many levels */
  3437.         while ((c = *s++)) {            /* Count dirseps */
  3438.             if (c == '/' && *s)
  3439.               k++;
  3440.         }
  3441.         for (i = 0; i < k; i++) {       /* Do that many CDUPs */
  3442.             debug(F111,"ftp syncdir up",p,i+1);
  3443.             if (sim && cdsimlvl) {
  3444.                 cdsimlvl--;
  3445.             } else {
  3446.                 if (!doftpcdup()) {
  3447.                     quiet = saveq;
  3448.                     return(0);
  3449.                 }
  3450.             }
  3451.             cdlevel--;
  3452.         }
  3453.         if (!*p)                        /* If we don't have to go down */
  3454.           goto xcwd;                    /* we're done. */
  3455.     }
  3456.     while (p > buf && *p && *p != '/')  /* If in middle of segment */
  3457.       p--;                              /* back up to beginning */
  3458.     if (*p == '/')                      /* and terminate there */
  3459.       p++;
  3460.  
  3461.     s = p;                              /* Point to start of new down path. */
  3462.     while (1) {                         /* Loop through characters. */
  3463.         if (*s == '/' || !*s) {         /* Have a segment. */
  3464.             if (!*s)                    /* If end of string, */
  3465.               done++;                   /* after this segment we're done. */
  3466.             else
  3467.               *s = NUL;                 /* NUL out the separator. */
  3468.             if (*p) {                   /* If segment is not empty */
  3469.                 debug(F110,"ftp syncdir down segment",p,0);
  3470.                 if (!doftpcwd(p,0)) {   /* Try to CD to it */
  3471.                     if (sim) {
  3472.                         if (fdispla == XYFD_B) {
  3473.                             printf("WOULD CREATE DIRECTORY %s\n",local);
  3474.                         } else if (fdispla) {
  3475.                             ckmakmsg(msgbuf,CKMAXPATH,"WOULD CREATE DIRECTORY",
  3476.                                      local,NULL,NULL);
  3477.                             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3478.                         }
  3479.                         cdsimlvl++;
  3480.                     } else {
  3481.                         if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
  3482. /*
  3483.   Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  3484.   FOO but the remote has a regular file with the same name.  We can't CD
  3485.   to it, can't MKDIR it either.  There's no way out but to fail and let
  3486.   the user handle the problem.
  3487. */
  3488.                             quiet = saveq;
  3489.                             return(0);
  3490.                         }
  3491.                         if (fdispla == XYFD_B) {
  3492.                             printf("CREATED DIRECTORY %s\n",p);
  3493.                         } else if (fdispla) {
  3494.                             ckmakmsg(msgbuf,CKMAXPATH,
  3495.                                      "CREATED DIRECTORY ",p,NULL,NULL);
  3496.                             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
  3497.                         }
  3498.                         if (!doftpcwd(p,0)) { /* Try again to CD */
  3499.                             quiet = saveq;
  3500.                             return(0);
  3501.                         }
  3502.                     }
  3503.                 }
  3504.                 cdlevel++;
  3505.             }
  3506.             if (done)                   /* Quit if no next segment */
  3507.               break;
  3508.             p = s+1;                    /* Point to next segment */
  3509.         }
  3510.         s++;                            /* Point to next source char */
  3511.     }
  3512.  
  3513.   xcwd:
  3514.     ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
  3515.     quiet = saveq;
  3516.     return(1);
  3517. }
  3518.  
  3519. #ifdef DOUPDATE
  3520. #ifdef DEBUG
  3521. static VOID
  3522. dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
  3523.     if (deblog) {
  3524.         debug(F111,"ftp year ",s,xx->tm_year);
  3525.         debug(F111,"ftp month",s,xx->tm_mon);
  3526.         debug(F111,"ftp day  ",s,xx->tm_mday);
  3527.         debug(F111,"ftp hour ",s,xx->tm_hour);
  3528.         debug(F111,"ftp min  ",s,xx->tm_min);
  3529.         debug(F111,"ftp sec  ",s,xx->tm_sec);
  3530.     }
  3531. }
  3532. #endif /* DEBUG */
  3533.  
  3534. /*  t m c o m p a r e  --  Compare two struct tm's */
  3535.  
  3536. /*  Like strcmp() but for struct tm's  */
  3537. /*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
  3538.  
  3539. static int
  3540. tmcompare(xx,yy) struct tm * xx, * yy; {
  3541.  
  3542.     if (xx->tm_year < yy->tm_year)      /* First year less than second */
  3543.       return(-1);
  3544.     if (xx->tm_year > yy->tm_year)      /* First year greater than second */
  3545.       return(1);
  3546.  
  3547.     /* Years are equal so compare months */
  3548.  
  3549.     if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
  3550.       return(-1);
  3551.     if (xx->tm_mon  > yy->tm_mon)
  3552.       return(1);
  3553.  
  3554.     if (xx->tm_mday < yy->tm_mday)
  3555.       return(-1);
  3556.     if (xx->tm_mday > yy->tm_mday)
  3557.       return(1);
  3558.  
  3559.     if (xx->tm_hour < yy->tm_hour)
  3560.       return(-1);
  3561.     if (xx->tm_hour > yy->tm_hour)
  3562.       return(1);
  3563.  
  3564.     if (xx->tm_min  < yy->tm_min)
  3565.       return(-1);
  3566.     if (xx->tm_min  > yy->tm_min)
  3567.       return(1);
  3568.  
  3569.     if (xx->tm_sec  < yy->tm_sec)
  3570.       return(-1);
  3571.     if (xx->tm_sec  > yy->tm_sec)
  3572.       return(1);
  3573.  
  3574.     return(0);
  3575. }
  3576. #endif /* DOUPDATE */
  3577.  
  3578. #ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
  3579. static CONST int MONTHDAYS[] = { /* Number of days in each month. */
  3580.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3581. };
  3582.  
  3583. /* Macro for whether a given year is a leap year. */
  3584. #define ISLEAP(year) \
  3585. (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
  3586. #endif /* HAVE_TIMEGM */
  3587.  
  3588. /*  m k u t i m e  --  Like mktime() but argument is already UTC */
  3589.  
  3590. static time_t
  3591. #ifdef CK_ANSIC
  3592. mkutime(struct tm * tm)
  3593. #else
  3594. mkutime(tm) struct tm * tm;
  3595. #endif /* CK_ANSIC */
  3596. /* mkutime */ {
  3597. #ifdef HAVE_TIMEGM
  3598.     return(timegm(tm));                 /* Have system service, use it. */
  3599. #else
  3600. /*
  3601.   Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  3602.   Given a struct tm representing a calendar time in UTC, convert it to
  3603.   seconds since epoch.  Returns (time_t) -1 if the time is not
  3604.   convertable.  Note that this function does not canonicalize the provided
  3605.   struct tm, nor does it allow out-of-range values or years before 1970.
  3606.   Result should be identical with timegm().
  3607. */
  3608.     time_t result = 0;
  3609.     int i;
  3610.     /*
  3611.       We do allow some ill-formed dates, but we don't do anything special
  3612.       with them and our callers really shouldn't pass them to us.  Do
  3613.       explicitly disallow the ones that would cause invalid array accesses
  3614.       or other algorithm problems.
  3615.     */
  3616. #ifdef DEBUG
  3617.     if (deblog) {
  3618.         debug(F101,"mkutime tm_mon","",tm->tm_mon);
  3619.         debug(F101,"mkutime tm_year","",tm->tm_year);
  3620.     }
  3621. #endif /* DEBUG */
  3622.     if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
  3623.       return((time_t) -1);
  3624.  
  3625.     /* Convert to time_t. */
  3626.     for (i = 1970; i < tm->tm_year + 1900; i++)
  3627.       result += 365 + ISLEAP(i);
  3628.     for (i = 0; i < tm->tm_mon; i++)
  3629.       result += MONTHDAYS[i];
  3630.     if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
  3631.       result++;
  3632.     result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
  3633.     result = 60 * result + tm->tm_min;
  3634.     result = 60 * result + tm->tm_sec;
  3635.     debug(F101,"mkutime result","",result);
  3636.     return(result);
  3637. #endif /* HAVE_TIMEGM */
  3638. }
  3639.  
  3640.  
  3641. /*
  3642.   s e t m o d t i m e  --  Set file modification time.
  3643.  
  3644.   f = char * filename;
  3645.   t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  3646.  
  3647.   UNIX-specific; isolates mainline code from hideous #ifdefs.
  3648.   Returns:
  3649.     0 on success,
  3650.    -1 on error.
  3651.  
  3652. */
  3653. static int
  3654. #ifdef CK_ANSIC
  3655. setmodtime(char * f, time_t t)
  3656. #else
  3657. setmodtime(f,t) char * f; time_t t;
  3658. #endif /* CK_ANSIC */
  3659. /* setmodtime */ {
  3660.     struct stat sb;
  3661.     int x, rc = 0;
  3662. #ifdef BSD44
  3663.     struct timeval tp[2];
  3664. #else
  3665. #ifdef V7
  3666.     struct utimbuf {
  3667.         time_t timep[2];
  3668.     } tp;
  3669. #else
  3670. #ifdef SYSUTIMEH
  3671.     struct utimbuf tp;
  3672. #else
  3673.     struct utimbuf {
  3674.         time_t atime;
  3675.         time_t mtime;
  3676.     } tp;
  3677. #endif /* SYSUTIMEH */
  3678. #endif /* V7 */
  3679. #endif /* BSD44 */
  3680.  
  3681.     if (stat(f,&sb) < 0) {
  3682.         debug(F111,"setmodtime stat failure",f,errno);
  3683.         return(-1);
  3684.     }
  3685. #ifdef BSD44
  3686.     tp[0].tv_sec = sb.st_atime;         /* Access time first */
  3687.     tp[1].tv_sec = t;                   /* Update time second */
  3688.     debug(F111,"setmodtime BSD44",f,t);
  3689. #else
  3690. #ifdef V7
  3691.     tp.timep[0] = t;                    /* Set modif. time to creation date */
  3692.     tp.timep[1] = sb.st_atime;          /* Don't change the access time */
  3693.     debug(F111,"setmodtime V7",f,t);
  3694. #else
  3695. #ifdef SYSUTIMEH
  3696.     tp.modtime = t;                     /* Set modif. time to creation date */
  3697.     tp.actime = sb.st_atime;            /* Don't change the access time */
  3698.     debug(F111,"setmodtime SYSUTIMEH",f,t);
  3699. #else
  3700.     tp.mtime = t;                       /* Set modif. time to creation date */
  3701.     tp.atime = sb.st_atime;             /* Don't change the access time */
  3702.     debug(F111,"setmodtime (other)",f,t);
  3703. #endif /* SYSUTIMEH */
  3704. #endif /* V7 */
  3705. #endif /* BSD44 */
  3706.  
  3707.     /* Try to set the file date */
  3708.  
  3709. #ifdef BSD44
  3710.     x = utimes(f,tp);
  3711.     debug(F111,"setmodtime utimes()","BSD44",x);
  3712. #else
  3713. #ifdef IRIX65
  3714.     {
  3715.       /*
  3716.         The following produces the nonsensical warning:
  3717.         Argument  of type "const struct utimbuf *" is incompatible with
  3718.         parameter of type "const struct utimbuf *".  If you can make it
  3719.         go away, be my guest.
  3720.       */
  3721.         const struct utimbuf * t2 = &tp;
  3722.         x = utime(f,t2);
  3723.     }
  3724. #else
  3725.     x = utime(f,&tp);
  3726.     debug(F111,"setmodtime utime()","other",x);
  3727. #endif /* IRIX65 */
  3728. #endif /* BSD44 */
  3729.     if (x)
  3730.       rc = -1;
  3731.  
  3732.     debug(F101,"setmodtime result","",rc);
  3733.     return(rc);
  3734. }
  3735.  
  3736.  
  3737. /*
  3738.   c h k m o d t i m e  --  Check/Set file modification time.
  3739.  
  3740.   fc = function code:
  3741.     0 = Check; returns:
  3742.       -1 on error,
  3743.        0 if local older than remote,
  3744.        1 if modtimes are equal,
  3745.        2 if local newer than remote.
  3746.     1 = Set (local file's modtime from remote's); returns:
  3747.       -1 on error,
  3748.        0 on success.
  3749. */
  3750. static int
  3751. chkmodtime(local,remote,fc) char * local, * remote; int fc; {
  3752.     struct stat statbuf;
  3753.     struct tm * tmlocal = NULL;
  3754.     struct tm tmremote;
  3755.     int rc = 0, havedate = 0, lcs = -1, rcs = -1, flag = 0;
  3756.     char * s, timebuf[64];
  3757.  
  3758.     debug(F111,"chkmodtime",local,mdtmok);
  3759.     if (!mdtmok)            /* Server supports MDTM? */
  3760.       return(-1);            /* No don't bother. */
  3761.  
  3762. #ifndef NOCSETS
  3763.     if (ftp_xla) {
  3764.         lcs = ftp_csl;
  3765.         if (lcs < 0) lcs = fcharset;
  3766.         rcs = ftp_csx;
  3767.         if (rcs < 0) rcs = ftp_csr;
  3768.     }
  3769. #endif /* NOCSETS */
  3770.  
  3771.     if (fc == 0) {
  3772.         rc = stat(local,&statbuf);
  3773.         if (rc == 0) {                  /* Get local file's mod time */
  3774.             tmlocal = gmtime(&statbuf.st_mtime); /* Convert to struct tm */
  3775. #ifdef DEBUG
  3776.             if (tmlocal) {
  3777.                 dbtime(local,tmlocal);
  3778.             }
  3779. #endif /* DEBUG */
  3780.         }
  3781.     }
  3782.     /* Get remote file's mod time as yyyymmddhhmmss */
  3783.  
  3784.     if (havemdtm) {            /* Already got it from MLSD? */
  3785.     s = havemdtm;
  3786.     flag++;
  3787.     } else if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
  3788.         char c;
  3789.         bzero((char *)&tmremote, sizeof(struct tm));
  3790.         s = ftp_reply_str;
  3791.         while ((c = *s++)) {            /* Skip past response code */
  3792.             if (c == SP) {
  3793.                 flag++;
  3794.                 break;
  3795.             }
  3796.         }
  3797.     }
  3798.     if (flag) {
  3799.     debug(F111,"ftp chkmodtime string",s,flag);
  3800.     if (fts_sto) {            /* User gave server time offset? */
  3801.         char * p;
  3802.         debug(F110,"ftp chkmodtime offset",fts_sto,0);
  3803.         ckmakmsg(timebuf,64,s," ",fts_sto,NULL); /* Build delta time */
  3804.         if ((p = cmcvtdate(timebuf,1))) { /* Apply delta time */
  3805.         ckstrncpy(timebuf,p,64);      /* Convert to MDTM format */
  3806.         timebuf[8]  = timebuf[9];  /* h */
  3807.         timebuf[9]  = timebuf[10]; /* h */
  3808.         timebuf[10] = timebuf[12]; /* m */
  3809.         timebuf[11] = timebuf[13]; /* m */
  3810.         timebuf[12] = timebuf[12]; /* s */
  3811.         timebuf[13] = timebuf[13]; /* s */
  3812.         timebuf[14] = NUL;
  3813.         s = timebuf;
  3814.         debug(F110,"ftp chkmodtime adjust",s,0);
  3815.         }
  3816.     }
  3817.         if (flag) {                     /* Convert to struct tm */
  3818.             char * pat;
  3819.             int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
  3820.             if (!ckstrcmp(s,"191",3,0)) {
  3821.                 pat = "%05d%02d%02d%02d%02d%02d";
  3822.                 y2kbug++;
  3823.                 debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
  3824.             } else {
  3825.                 pat = "%04d%02d%02d%02d%02d%02d";
  3826.             }
  3827.             if (sscanf(s,               /* Parse into struct tm */
  3828.                        pat,
  3829.                        &(tmremote.tm_year),
  3830.                        &(tmremote.tm_mon),
  3831.                        &(tmremote.tm_mday),
  3832.                        &(tmremote.tm_hour),
  3833.                        &(tmremote.tm_min),
  3834.                        &(tmremote.tm_sec)
  3835.                        ) == 6) {
  3836.                 tmremote.tm_year -= (y2kbug ? 19000 : 1900);
  3837.                 debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
  3838.                 tmremote.tm_mon--;
  3839.  
  3840. #ifdef DEBUG
  3841.         debug(F100,"SERVER TIME FOLLOWS:","",0);
  3842.                 dbtime(remote,&tmremote);
  3843. #endif /* DEBUG */
  3844.  
  3845.                 if (havedate > -1)
  3846.           havedate = 1;
  3847.             }
  3848.         }
  3849.     } else {                /* Failed */
  3850.     debug(F101,"ftp chkmodtime ftpcode","",ftpcode);
  3851.     if (ftpcode == 500 ||        /* Command unrecognized */
  3852.         ftpcode == 502 ||        /* Command not implemented */
  3853.         ftpcode == 202)        /* Command superfluous */
  3854.       mdtmok = 0;            /* Don't ask this server again */
  3855.     return(-1);
  3856.     }
  3857.     if (fc == 0) {                      /* Compare */
  3858.         if (havedate == 1) {        /* Only if we have both file dates */
  3859.             /*
  3860.               Compare with local file's time.  We don't use
  3861.               clock time (time_t) here in case of signed/unsigned
  3862.               confusion, etc.
  3863.             */
  3864.         int xx;
  3865. #ifdef COMMENT
  3866. #ifdef DEBUG        
  3867.         if (deblog) {
  3868.         dbtime("LOCAL",tmlocal);
  3869.         dbtime("REMOT",&tmremote);
  3870.         }
  3871. #endif /* DEBUG */
  3872. #endif /* COMMENT */
  3873.         xx = tmcompare(tmlocal,&tmremote);
  3874.         debug(F101,"chkmodtime tmcompare","",xx);
  3875.             return(xx + 1);
  3876.         }
  3877.     } else if (ftp_dates) {             /* Set */
  3878.         /*
  3879.           Here we must convert struct tm to time_t
  3880.           without applying timezone conversion, for which
  3881.           there is no portable API.  The method is hidden
  3882.           in mkutime(), defined above.
  3883.         */
  3884.         time_t utc;
  3885.         utc = mkutime(&tmremote);
  3886.         debug(F111,"ftp chkmodtime mkutime",remote,utc);
  3887.         if (utc != (time_t)-1)
  3888.           return(setmodtime(local,utc));
  3889.     }
  3890.     return(-1);
  3891. }
  3892.  
  3893. /* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
  3894.  
  3895. static int
  3896. getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
  3897.     char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
  3898. /* getfile */ {
  3899.     int rc = -1;
  3900.     ULONG t0, t1;
  3901.  
  3902. #ifdef GFTIMER
  3903.     CKFLOAT sec;
  3904. #else
  3905.     int sec = 0;
  3906. #endif /* GFTIMER */
  3907.     char fullname[CKMAXPATH+1];
  3908.  
  3909.     debug(F110,"ftp getfile remote A",remote,0);
  3910.     debug(F110,"ftp getfile local A",local,0);
  3911.     debug(F110,"ftp getfile pipename",pipename,0);
  3912.     if (!remote) remote = "";
  3913.  
  3914. #ifdef PATTERNS
  3915.     /* Automatic type switching? */
  3916.     if (xfermode == XMODE_A && patterns && get_auto && !forcetype) {
  3917.         int x;
  3918.         x = matchname(remote,0,servertype);
  3919.         debug(F111,"ftp getfile matchname",remote,x);
  3920.         switch (x) {
  3921.           case 0: ftp_typ = FTT_ASC; break;
  3922.           case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
  3923.           default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
  3924.         }
  3925.         changetype(ftp_typ,ftp_vbm);
  3926.         binary = ftp_typ;               /* For file-transfer display */
  3927.     }
  3928. #endif /* PATTERNS */
  3929.  
  3930. #ifndef NOCSETS
  3931.     ftp_csx = -1;                       /* For file-transfer display */
  3932.     ftp_csl = -1;                       /* ... */
  3933.  
  3934.     if (rcs > -1)                       /* -1 means no translation */
  3935.       if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
  3936.         if (fcs < 0)                    /* File charset not forced? */
  3937.           fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
  3938.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  3939.         debug(F110,"ftp getfile","initxlate",0);
  3940.         initxlate(rcs,fcs);             /* NB: opposite order of PUT */
  3941.         ftp_csx = rcs;
  3942.         ftp_csl = fcs;
  3943.     } else
  3944.       xlate = 0;
  3945. #endif /* NOCSETS */
  3946.  
  3947.     if (!local) local = "";
  3948.     if (!pipename && !*local)
  3949.       local = remote;
  3950.  
  3951.     out2screen = !strcmp(local,"-");
  3952.  
  3953.     fullname[0] = NUL;
  3954.     if (pipename) {
  3955.         ckstrncpy(fullname,pipename,CKMAXPATH+1);
  3956.     } else {
  3957.         zfnqfp(local,CKMAXPATH,fullname);
  3958.         if (!fullname[0])
  3959.           ckstrncpy(fullname,local,CKMAXPATH+1);
  3960.     }
  3961.     if (!out2screen && displa && fdispla) { /* Screen */
  3962.         ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,remote);
  3963.         ftscreen(SCR_AN,0,(CK_OFF_T)0,fullname);
  3964.         ftscreen(SCR_FS,0,fsize,"");
  3965.     }
  3966.     tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
  3967.     tlog(F110," as",fullname,0);
  3968.     debug(F111,"ftp getfile size",remote,fsize);
  3969.     debug(F111,"ftp getfile local",local,out2screen);
  3970.  
  3971.     ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
  3972.  
  3973.     t0 = gmstimer();                    /* Start time */
  3974.     debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
  3975.     rc = recvrequest("RETR",
  3976.                      local,
  3977.                      remote,
  3978.                      append ? "ab" : "wb",
  3979.                      0,
  3980.                      recover,
  3981.                      pipename,
  3982.                      xlate,
  3983.                      fcs,
  3984.                      rcs
  3985.                      );
  3986.     t1 = gmstimer();                    /* End time */
  3987.     debug(F111,"ftp getfile t1",remote,t1);
  3988.     debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
  3989. #ifdef GFTIMER
  3990.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  3991.     fpxfsecs = sec;                     /* (for doxlog()) */
  3992. #else
  3993.     sec = (t1 - t0) / 1000;
  3994.     xfsecs = (int)sec;
  3995. #endif /* GFTIMER */
  3996.     debug(F111,"ftp recvrequest rc",remote,rc);
  3997.     if (cancelfile || cancelgroup) {
  3998.         debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
  3999.         ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4000.     } else if (rc > 0) {
  4001.         debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
  4002.         ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,cmarg);
  4003.     } else if (rc < 0) {
  4004.         switch (ftpcode) {
  4005.           case -4:                      /* Network error */
  4006.           case -2:                      /* File error */
  4007.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,ck_errstr());
  4008.             break;
  4009.           case -3:
  4010.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,
  4011.              "Failure to make data connection");
  4012.             break;
  4013.           case -1:            /* (should be covered above) */
  4014.             ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4015.             break;
  4016.           default:
  4017.             ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
  4018.         }
  4019.     } else {                            /* Tudo bem */
  4020.         ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
  4021.         if (rc == 0) {
  4022.             ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,""); /* For screen */
  4023.             makestr(&rrfspec,remote);     /* For WHERE command */
  4024.             makestr(&rfspec,fullname);
  4025.         }
  4026.     }
  4027.     if (ftp_dates)            /* If FTP DATES ON... */
  4028.       if (!pipename && !out2screen)    /* and it's a real file */
  4029.     if (rc < 1 && rc != -3)        /* and it wasn't skipped */
  4030.       if (connected)        /* and we still have a connection */
  4031.         if (zchki(local) > -1) {    /* and the file wasn't discarded */
  4032.         chkmodtime(local,remote,1); /* set local file date */
  4033.         debug(F110,"ftp get set date",local,0);
  4034.         }
  4035.     filcnt++;                           /* Used by \v(filenum) */
  4036. #ifdef TLOG
  4037.     if (tralog) {
  4038.         if (rc > 0) {
  4039.             tlog(F100," recovery skipped","",0);
  4040.         } else if (rc == 0) {
  4041.             tlog(F101," complete, size", "", fsize);
  4042.         } else if (cancelfile) {
  4043.             tlog(F100," canceled by user","",0);
  4044.         } else {
  4045.             tlog(F110," failed:",ftp_reply_str,0);
  4046.         }
  4047.         if (!tlogfmt)
  4048.           doxlog(what,local,fsize,ftp_typ,rc,"");
  4049.     }
  4050. #endif /* TLOG */
  4051.     return(rc);
  4052. }
  4053.  
  4054. /* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
  4055. /* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
  4056.  
  4057. static int
  4058. putfile(cx,
  4059.     local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
  4060.     char * local, * remote, * mvto, *rnto, *srvrn;
  4061.     int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg, prm;
  4062.  
  4063. /* putfile */ {
  4064.  
  4065.     char asname[CKMAXPATH+1];
  4066.     char fullname[CKMAXPATH+1];
  4067.     int k = -1, x = 0, y = 0, o = -1, rc = 0, nc = 0;
  4068.     int xlate = 0, restart = 0, mt = -1;
  4069.     char * s = NULL, * cmd = NULL;
  4070.     ULONG t0 = 0, t1 = 0;        /* Times for stats */
  4071.     int ofcs = 0, orcs = 0;
  4072.  
  4073. #ifdef GFTIMER
  4074.     CKFLOAT sec = 0.0;
  4075. #else
  4076.     int sec = 0;
  4077. #endif /* GFTIMER */
  4078.     debug(F111,"ftp putfile flg",local,flg);
  4079.     debug(F110,"ftp putfile srv_renam",srvrn,0);
  4080.     debug(F101,"ftp putfile fcs","",fcs);
  4081.     debug(F101,"ftp putfile rcs","",rcs);
  4082.  
  4083.     ofcs = fcs;                         /* Save charset args */
  4084.     orcs = rcs;
  4085.  
  4086.     sendstart = (CK_OFF_T)0;
  4087.     restart = flg & PUT_RES;
  4088.     if (!remote)
  4089.       remote = "";
  4090.  
  4091.     /* FTP protocol command to send to server */
  4092.     cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
  4093.  
  4094.     if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
  4095.         if (alike) {                    /* If server & client are alike */
  4096.             nc = 0;                     /* no conversion */
  4097.         } else {                        /* If they are different */
  4098.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  4099.               nc = -1;                  /* only minimal conversions needed */
  4100.             else                        /* otherwise */
  4101.               nc = 1;                   /* full conversion */
  4102.         }
  4103.     } else                              /* Not auto - do what user said */
  4104.       nc = x_cnv;
  4105.  
  4106.     /* If Transfer Mode is Automatic, determine file type */
  4107.     if (xfermode == XMODE_A && filepeek && !pipesend) {
  4108.         if (isdir(local)) {             /* If it's a directory */
  4109.             k = FT_BIN;                 /* skip the file scan */
  4110.         } else {
  4111.             debug(F110,"FTP PUT calling scanfile",local,0);
  4112.             k = scanfile(local,&o,nscanfile); /* Scan the file */
  4113.         }
  4114.         debug(F111,"FTP PUT scanfile",local,k);
  4115.         if (k > -1 && !forcetype) {
  4116.             ftp_typ = (k == FT_BIN) ? 1 : 0;
  4117.             if (xft > -1 && ftp_typ != xft) {
  4118.                 if (flg & PUT_SIM)
  4119.                   tlog(F110,"ftp put SKIP (Type):", local, 0);
  4120.                 return(SKP_TYP);
  4121.             }
  4122.             if (ftp_typ == 1 && tenex)  /* User said TENEX? */
  4123.               ftp_typ = FTT_TEN;
  4124.         }
  4125.     }
  4126. #ifndef NOCSETS
  4127.     ftp_csx = -1;                       /* For file-transfer display */
  4128.     ftp_csl = -1;                       /* ... */
  4129.  
  4130.     if (rcs > -1) {                     /* -1 means no translation */
  4131.         if (ftp_typ == 0) {             /* File type is "ascii"? */
  4132.             if (fcs < 0) {              /* File charset not forced? */
  4133.                 if (k < 0) {            /* If we didn't scan */
  4134.                     fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
  4135.                 } else {                /* If we did scan, use scan result */
  4136.                     switch (k) {
  4137.                       case FT_TEXT:     /* Unknown text */
  4138.                         fcs = fcharset;
  4139.                         break;
  4140.                       case FT_7BIT:     /* 7-bit text */
  4141.                         fcs = dcset7;
  4142.                         break;
  4143.                       case FT_8BIT:     /* 8-bit text */
  4144.                         fcs = dcset8;
  4145.                         break;
  4146.                       case FT_UTF8:     /* UTF-8 */
  4147.                         fcs = FC_UTF8;
  4148.                         break;
  4149.                       case FT_UCS2:     /* UCS-2 */
  4150.                         fcs = FC_UCS2;
  4151.                         if (o > -1)     /* Input file byte order */
  4152.                           fileorder = o;
  4153.                         break;
  4154.                       default:
  4155.                         rcs = -1;
  4156.                     }
  4157.                 }
  4158.             }
  4159.         }
  4160.     }
  4161.     if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
  4162.         debug(F110,"ftp putfile","initxlate",0);
  4163.         initxlate(fcs,rcs);
  4164.         debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
  4165.         xlate = 1;
  4166.         ftp_csx = rcs;
  4167.         ftp_csl = fcs;
  4168.     }
  4169. #endif /* NOCSETS */
  4170.  
  4171.     binary = ftp_typ;                   /* For file-transfer display */
  4172.     asname[0] = NUL;
  4173.  
  4174.     if (recursive) {                    /* If sending recursively, */
  4175.         if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
  4176.           return(-1);                   /* Don't PUT if it fails. */
  4177.         else if (isdir(local))          /* It's a directory */
  4178.           return(0);                    /* Don't send it! */
  4179.     }
  4180.     if (*remote) {                      /* If an as-name template was given */
  4181. #ifndef NOSPL
  4182.         if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
  4183.             y = CKMAXPATH;              /* evaluate it for this file */
  4184.             s = asname;
  4185.             zzstring(remote,&s,&y);
  4186.         } else
  4187. #endif /* NOSPL */
  4188.           ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
  4189.     } else {                                    /* No as-name */
  4190.         nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
  4191.         debug(F110,"FTP PUT nzltor",asname,0);
  4192.     }
  4193.     /* Preliminary messages and log entries */
  4194.  
  4195.     fullname[0] = NUL;
  4196.     zfnqfp(local,CKMAXPATH,fullname);
  4197.     if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
  4198.     fullname[CKMAXPATH] = NUL;
  4199.  
  4200.     if (displa && fdispla) {            /* Screen */
  4201.         ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,local);
  4202.         ftscreen(SCR_AN,0,(CK_OFF_T)0,asname);
  4203.         ftscreen(SCR_FS,0,fsize,"");
  4204.     }
  4205. #ifdef DOUPDATE
  4206.     if (flg & (PUT_UPD|PUT_DIF)) {    /* Date-checking modes... */
  4207.         mt = chkmodtime(fullname,asname,0);
  4208.         debug(F111,"ftp putfile chkmodtime",asname,mt);
  4209.         if (mt == 0 && ((flg & PUT_DIF) == 0)) { /* Local is older */
  4210.             tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
  4211.         /* Skip this one */
  4212.             ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_DAT,fullname);
  4213.             filcnt++;
  4214.             return(SKP_DAT);
  4215.         } else if (mt == 1) {           /* Times are equal */
  4216.             tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
  4217.             ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_EQU,fullname); /* Skip it */
  4218.             filcnt++;
  4219.             return(SKP_DAT);
  4220.         }
  4221.     /* Local file is newer */
  4222.         tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
  4223.              "ftp put /update TEXT:", fullname, 0);
  4224.     } else if (flg & PUT_RES) {
  4225.         tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
  4226.              "ftp put /recover TEXT:", fullname, 0);
  4227.     } else {
  4228.         tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4229.     }
  4230. #else
  4231.     tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
  4232. #endif /* DOUPDATE */
  4233.     tlog(F110," as",asname,0);
  4234.  
  4235. #ifndef NOCSETS
  4236.     if (xlate) {
  4237.         debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
  4238.         tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
  4239.         tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
  4240.     } else if (!ftp_typ) {
  4241.         tlog(F110," character sets:","no conversion",0);
  4242.         fcs = ofcs;                     /* Binary file but we still must */
  4243.         rcs = orcs;                     /* translate its name */
  4244.     }
  4245. #endif /* NOCSETS */
  4246.  
  4247.     /* PUT THE FILE */
  4248.  
  4249.     t0 = gmstimer();                    /* Start time */
  4250.     if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
  4251.         if (flg & (PUT_UPD|PUT_DIF)) {    /* (see SKP_xxx in ckcker.h) */
  4252.             rc = (mt < 0) ?             /* Update mode... */
  4253.               SKP_XNX :                 /* Remote file doesn't exist */
  4254.                 SKP_XUP;                /* Remote file is older */
  4255.         } else {
  4256.             rc = SKP_SIM;               /* "Would be sent", period. */
  4257.         }
  4258.     } else {
  4259.         rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
  4260.     }
  4261.     t1 = gmstimer();                    /* End time */
  4262.     filcnt++;                           /* File number */
  4263.  
  4264. #ifdef GFTIMER
  4265.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4266.     fpxfsecs = sec;                     /* (for doxlog()) */
  4267. #else
  4268.     sec = (t1 - t0) / 1000;
  4269.     xfsecs = (int)sec;
  4270. #endif /* GFTIMER */
  4271.  
  4272.     debug(F111,"ftp sendrequest rc",local,rc);
  4273.  
  4274.     if (cancelfile || cancelgroup) {
  4275.         debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
  4276.         ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
  4277.     } else if (rc > 0) {
  4278.         debug(F101,"ftp put skipped",local,rc);
  4279.         ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)rc,fullname);
  4280.     } else if (rc < 0) {
  4281.         debug(F111,"ftp put error",local,ftpcode);
  4282.         ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
  4283.     } else {
  4284.         debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
  4285.         ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
  4286.         debug(F111,"ftp put ST_OK",local,rc);
  4287.         ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,"");
  4288.         debug(F110,"ftp put old sfspec",sfspec,0);
  4289.         makestr(&sfspec,fullname);      /* For WHERE command */
  4290.         debug(F110,"ftp put new sfspec",sfspec,0);
  4291.         debug(F110,"ftp put old srfspec",srfspec,0);
  4292.         makestr(&srfspec,asname);
  4293.         debug(F110,"ftp put new srfspec",srfspec,0);
  4294.     }
  4295.  
  4296.     /* Final log entries */
  4297.  
  4298. #ifdef TLOG
  4299.     if (tralog) {
  4300.         if (rc > 0) {
  4301.             if (rc == SKP_XNX)
  4302.               tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
  4303.             else if (rc == SKP_XUP)
  4304.               tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
  4305.             else if (rc == SKP_SIM)
  4306.               tlog(F100," /simulate: WOULD BE SENT","",0);
  4307.             else
  4308.               tlog(F110," skipped:",gskreason(rc),0);
  4309.         } else if (rc == 0) {
  4310.             tlog(F101," complete, size", "", fsize);
  4311.         } else if (cancelfile) {
  4312.             tlog(F100," canceled by user","",0);
  4313.         } else {
  4314.             tlog(F110," failed:",ftp_reply_str,0);
  4315.         }
  4316.         if (!tlogfmt)
  4317.           doxlog(what,local,fsize,ftp_typ,rc,"");
  4318.     }
  4319. #endif /* TLOG */
  4320.  
  4321.     if (rc < 0)                         /* PUT did not succeed */
  4322.       return(-1);                       /* so done. */
  4323.  
  4324.     if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
  4325.       return(SKP_SIM);
  4326.  
  4327. #ifdef UNIX
  4328.     /* Set permissions too? */
  4329.  
  4330.     if (prm) {                          /* Change permissions? */
  4331.         s = zgperm(local);              /* Get perms of local file */
  4332.         if (!s) s = "";
  4333.         x = strlen(s);
  4334.         if (x > 3) s += (x - 3);
  4335.         if (rdigits(s)) {
  4336.             ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
  4337.             x =
  4338.               ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
  4339.             tlog(F110, x ? " chmod" : " chmod failed",
  4340.                  s,
  4341.                  0
  4342.                  );
  4343.             if (!x)
  4344.               return(-1);
  4345.         }
  4346.     }
  4347. #endif /* UNIX */
  4348.  
  4349.     /* Disposition of source file */
  4350.  
  4351.     if (moving) {
  4352.         x = zdelet(local);
  4353.         tlog(F110, (x > -1) ?
  4354.              " deleted" : " failed to delete",
  4355.              local,
  4356.              0
  4357.              );
  4358.         if (x < 0)
  4359.           return(-1);
  4360.     } else if (mvto) {
  4361.         x = zrename(local,mvto);
  4362.         tlog(F110, (x > -1) ?
  4363.              " moved source to" : " failed to move source to",
  4364.              mvto,
  4365.              0
  4366.              );
  4367.         if (x < 0)
  4368.           return(-1);
  4369.         /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,mvto); */
  4370.  
  4371.     } else if (rnto) {
  4372.         char * s = rnto;
  4373. #ifndef NOSPL
  4374.         int y;                          /* Pass it thru the evaluator */
  4375.         extern int cmd_quoting;         /* for \v(filename) */
  4376.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4377.             y = CKMAXPATH;
  4378.             s = (char *)asname;
  4379.             zzstring(rnto,&s,&y);
  4380.             s = (char *)asname;
  4381.         }
  4382. #endif /* NOSPL */
  4383.         if (s) if (*s) {
  4384.             int x;
  4385.             x = zrename(local,s);
  4386.             tlog(F110, (x > -1) ?
  4387.                  " renamed source file to" :
  4388.                  " failed to rename source file to",
  4389.                  s,
  4390.                  0
  4391.                  );
  4392.             if (x < 0)
  4393.               return(-1);
  4394.             /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,s); */
  4395.         }
  4396.     }
  4397.  
  4398.     /* Disposition of destination file */
  4399.  
  4400.     if (srvrn) {                        /* /SERVER-RENAME: */
  4401.         char * s = srvrn;
  4402. #ifndef NOSPL
  4403.         int y;                          /* Pass it thru the evaluator */
  4404.         extern int cmd_quoting; /* for \v(filename) */
  4405.         debug(F111,"ftp putfile srvrn",s,1);
  4406.  
  4407.         if (cmd_quoting) {              /* But only if cmd_quoting is on */
  4408.             y = CKMAXPATH;
  4409.             s = (char *)fullname;       /* We can recycle this buffer now */
  4410.             zzstring(srvrn,&s,&y);
  4411.             s = (char *)fullname;
  4412.         }
  4413. #endif /* NOSPL */
  4414.         debug(F111,"ftp putfile srvrn",s,2);
  4415.         if (s) if (*s) {
  4416.             int x;
  4417.             x = ftp_rename(asname,s);
  4418.             debug(F111,"ftp putfile ftp_rename",asname,x);
  4419.             tlog(F110, (x > 0) ?
  4420.                  " renamed destination file to" :
  4421.                  " failed to rename destination file to",
  4422.                  s,
  4423.                  0
  4424.                  );
  4425.             if (x < 1)
  4426.               return(-1);
  4427.         }
  4428.     }
  4429.     return(0);
  4430. }
  4431.  
  4432. /* xxout must only be used for ASCII transfers */
  4433. static int
  4434. #ifdef CK_ANSIC
  4435. xxout(char c)
  4436. #else
  4437. xxout(c) char c;
  4438. #endif /* CK_ANSIC */
  4439. {
  4440. #ifndef OS2
  4441. #ifndef VMS
  4442. #ifndef MAC
  4443. #ifndef OSK
  4444.     /* For Unix, DG, Stratus, Amiga, Gemdos, other */
  4445.     if (c == '\012') {
  4446.     if (zzout(dout,(CHAR)'\015') < 0)
  4447.       return(-1);
  4448.     ftpsnd.bytes++;
  4449.     }
  4450. #else /* OSK */
  4451.     if (c == '\015') {
  4452.     c = '\012';
  4453.     if (zzout(dout,(CHAR)'\015') < 0)
  4454.       return(-1);
  4455.     ftpsnd.bytes++;
  4456.     }
  4457. #endif /* OSK */
  4458. #else /* MAC */
  4459.     if (c == '\015') {
  4460.     c = '\012';
  4461.     if (zzout(dout,(CHAR)'\015') < 0)
  4462.       return(-1);
  4463.     ftpsnd.bytes++;
  4464.     }
  4465. #endif /* MAC */
  4466. #endif /* VMS */
  4467. #endif /* OS2 */
  4468.     if (zzout(dout,(CHAR)c) < 0)
  4469.       return(-1);
  4470.     ftpsnd.bytes++;
  4471.     return(0);
  4472. }
  4473.  
  4474. static int
  4475. #ifdef CK_ANSIC
  4476. scrnout(char c)
  4477. #else
  4478. scrnout(c) char c;
  4479. #endif /* CK_ANSIC */
  4480. {
  4481.     return(putchar(c));
  4482. }
  4483.  
  4484. static int
  4485. #ifdef CK_ANSIC
  4486. pipeout(char c)
  4487. #else
  4488. pipeout(c) char c;
  4489. #endif /* CK_ANSIC */
  4490. {
  4491.     return(zmchout(c));
  4492. }
  4493.  
  4494. static int
  4495. ispathsep(c) int c; {
  4496.     switch (servertype) {
  4497.       case SYS_VMS:
  4498.       case SYS_TOPS10:
  4499.       case SYS_TOPS20:
  4500.         return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
  4501.       case SYS_OS2:
  4502.       case SYS_WIN32:
  4503.       case SYS_DOS:
  4504.         return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
  4505.       case SYS_VOS:
  4506.         return((c == '>') ? 1 : 0);
  4507.       default:
  4508.         return((c == '/') ? 1 : 0);
  4509.     }
  4510. }
  4511.  
  4512. static int
  4513. iscanceled() {
  4514. #ifdef CK_CURSES
  4515.     extern int ck_repaint();
  4516. #endif /* CK_CURSES */
  4517.     int x, rc = 0;
  4518.     char c = 0;
  4519.     if (cancelfile)
  4520.       return(1);
  4521.     x = conchk();                       /* Any chars waiting at console? */
  4522.     if (x-- > 0) {                      /* Yes...  */
  4523.         c = coninc(5);                  /* Get one */
  4524.         switch (c) {
  4525.           case 032:                     /* Ctrl-X or X */
  4526.           case 'z':
  4527.           case 'Z': cancelgroup++;      /* fall thru on purpose */
  4528.           case 030:                     /* Ctrl-Z or Z */
  4529.           case 'x':
  4530.           case 'X': cancelfile++; rc++; break;
  4531. #ifdef CK_CURSES
  4532.           case 'L':
  4533.           case 'l':
  4534.           case 014:                     /* Ctrl-L or L or Ctrl-W */
  4535.           case 027:
  4536.             ck_repaint();               /* Refresh screen */
  4537. #endif /* CK_CURSES */
  4538.         }
  4539.     }
  4540.     while (x-- > 0)                     /* Soak up any rest */
  4541.       c = coninc(1);
  4542.     return(rc);
  4543. }
  4544.  
  4545. /* zzsend - used by buffered output macros. */
  4546.  
  4547. static int
  4548. #ifdef CK_ANSIC
  4549. zzsend(int fd, CHAR c)
  4550. #else
  4551. zzsend(fd,c) int fd; CHAR c;
  4552. #endif /* CK_ANSIC */
  4553. {
  4554.     int rc;
  4555.  
  4556.     debug(F101,"zzsend ucbufsiz","",ucbufsiz);
  4557.     debug(F101,"zzsend nout","",nout);
  4558.     debug(F111,"zzsend","secure?",ftpissecure());
  4559.  
  4560.     if (iscanceled())                   /* Check for cancellation */
  4561.       return(-9);
  4562.     rc = (!ftpissecure()) ?
  4563.       send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
  4564.         secure_putbuf(fd, ucbuf, nout);
  4565.     ucbuf[nout] = NUL;
  4566.     nout = 0;
  4567.     ucbuf[nout++] = c;
  4568.     spackets++;
  4569.     pktnum++;
  4570.     if (rc > -1 && fdispla != XYFD_B) {
  4571.         spktl = nout;
  4572.         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  4573.     }
  4574.     return(rc);
  4575. }
  4576.  
  4577. /* c m d l i n p u t  --  Command-line PUT */
  4578.  
  4579. int
  4580. cmdlinput(stay) int stay; {
  4581.     int x, rc = 0, done = 0, good = 0, status = 0;
  4582.     ULONG t0, t1;                       /* Times for stats */
  4583. #ifdef GFTIMER
  4584.     CKFLOAT sec;
  4585. #else
  4586.     int sec = 0;
  4587. #endif /* GFTIMER */
  4588.  
  4589.     if (quiet) {                        /* -q really means quiet */
  4590.         displa = 0;
  4591.         fdispla = 0;
  4592.     } else {
  4593.         displa = 1;
  4594.         fdispla = XYFD_B;
  4595.     }
  4596.     testing = 0;
  4597.     out2screen = 0;
  4598.     dpyactive = 0;
  4599.     what = W_FTP|W_SEND;
  4600.  
  4601. #ifndef NOSPL
  4602.     cmd_quoting = 0;
  4603. #endif /* NOSPL */
  4604.     sndsrc = nfils;
  4605.  
  4606.     t0 = gmstimer();                    /* Record starting time */
  4607.  
  4608.     while (!done && !cancelgroup) {     /* Loop for all files */
  4609.  
  4610.         cancelfile = 0;
  4611.         x = gnfile();                   /* Get next file from list(s) */
  4612.         if (x == 0)                     /* (see gnfile() comments...) */
  4613.           x = gnferror;
  4614.  
  4615.         switch (x) {
  4616.           case 1:                       /* File to send */
  4617.             rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
  4618.                          filnam,        /* Local file to send */
  4619.                          filnam,        /* Remote name for file */
  4620.                          forcetype,     /* Text/binary mode forced */
  4621.                          0,             /* Not moving */
  4622.                          NULL,          /* No move-to */
  4623.                          NULL,          /* No rename-to */
  4624.                          NULL,          /* No server-rename */
  4625.                          ftp_cnv,       /* Filename conversion */
  4626.                          0,             /* Unique-server-names */
  4627.                          -1,            /* All file types */
  4628.                          0,             /* No permissions */
  4629.                          -1,            /* No character sets */
  4630.                          -1,            /* No character sets */
  4631.                          0              /* No update or restart */
  4632.                          );
  4633.             if (rc > -1) {
  4634.                 good++;
  4635.                 status = 1;
  4636.             }
  4637.             if (cancelfile) {
  4638.                 continue;               /* Or break? */
  4639.             }
  4640.             if (rc < 0) {
  4641.                 ftp_fai++;
  4642.             }
  4643.             continue;                   /* Or break? */
  4644.  
  4645.           case 0:                       /* No more files, done */
  4646.             done++;
  4647.             continue;
  4648.  
  4649.           case -2:
  4650.           case -1:
  4651.             printf("?%s: file not found - \"%s\"\n",
  4652.                    puterror ? "Fatal" : "Warning",
  4653.                    filnam
  4654.                    );
  4655.             continue;                   /* or break? */
  4656.           case -3:
  4657.             printf("?Warning access denied - \"%s\"\n", filnam);
  4658.             continue;                   /* or break? */
  4659.           case -5:
  4660.             printf("?Too many files match\n");
  4661.             done++;
  4662.             break;
  4663.           case -6:
  4664.             if (good < 1)
  4665.               printf("?No files selected\n");
  4666.             done++;
  4667.             break;
  4668.           default:
  4669.             printf("?getnextfile() - unknown failure\n");
  4670.             done++;
  4671.         }
  4672.     }
  4673.     if (status > 0) {
  4674.         if (cancelgroup)
  4675.           status = 0;
  4676.         else if (cancelfile && good < 1)
  4677.           status = 0;
  4678.     }
  4679.     success = status;
  4680.     x = success;
  4681.     if (x > -1) {
  4682.         lastxfer = W_FTP|W_SEND;
  4683.         xferstat = success;
  4684.     }
  4685.     t1 = gmstimer();                    /* End time */
  4686. #ifdef GFTIMER
  4687.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  4688.     if (!sec) sec = 0.001;
  4689.     fptsecs = sec;
  4690. #else
  4691.     sec = (t1 - t0) / 1000;
  4692.     if (!sec) sec = 1;
  4693. #endif /* GFTIMER */
  4694.     tfcps = (long) (tfc / sec);
  4695.     tsecs = (int)sec;
  4696.     lastxfer = W_FTP|W_SEND;
  4697.     xferstat = success;
  4698.     if (dpyactive)
  4699.       ftscreen(SCR_TC,0,(CK_OFF_T)0,"");
  4700.  
  4701.     if (!stay)
  4702.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  4703.     return(success);
  4704. }
  4705.  
  4706.  
  4707. /*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
  4708.  
  4709. int
  4710. #ifdef CK_ANSIC
  4711. doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
  4712. #else
  4713. doftpput(cx,who) int cx, who;
  4714. #endif /* CK_ANSIC */
  4715. {
  4716.     struct FDB sf, fl, sw, cm;
  4717.     int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
  4718.     int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
  4719.     char * s, * s2;
  4720.  
  4721.     int x_csl, x_csr = -1;              /* Local and remote charsets */
  4722.     int x_xla = 0;
  4723.     int x_recurse = 0;
  4724.     char c, * p;                        /* Workers */
  4725. #ifdef PUTARRAY
  4726.     int range[2];                       /* Array range */
  4727.     char ** ap = NULL;                  /* Array pointer */
  4728.     int arrayx = -1;                    /* Array index */
  4729. #endif /* PUTARRAY */
  4730.     ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
  4731. #ifdef GFTIMER
  4732.     CKFLOAT sec;
  4733. #else
  4734.     int sec = 0;
  4735. #endif /* GFTIMER */
  4736.  
  4737.  
  4738.     struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
  4739.     success = 0;                        /* Assume failure */
  4740.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  4741.     out2screen = 0;                     /* Not outputting file to screen */
  4742.     putflags = 0;                       /* PUT options */
  4743.     x_cnv = ftp_cnv;                    /* Filename conversion */
  4744.     x_usn = ftp_usn;                    /* Unique server names */
  4745.     x_prm = ftp_prm;                    /* Permissions */
  4746.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  4747.       x_prm = alike;
  4748.  
  4749. #ifndef NOCSETS
  4750.     x_csr = ftp_csr;                    /* Inherit global server charset */
  4751.     x_csl = ftp_csl;
  4752.     if (x_csl < 0)
  4753.       x_csl = fcharset;
  4754.     x_xla = ftp_xla;
  4755. #endif /* NOCSETS */
  4756.  
  4757.     makestr(&filefile,NULL);            /* No filename list file yet. */
  4758.     makestr(&srv_renam,NULL);        /* Clear /SERVER-RENAME: */
  4759.     makestr(&snd_rename,NULL);        /*  PUT /RENAME */
  4760.     makestr(&snd_move,NULL);        /*  PUT /MOVE */
  4761.     putpath[0] = NUL;                   /* Initialize for syncdir(). */
  4762.     puterror = ftp_err;                 /* Inherit global error action. */
  4763.     what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
  4764.     asnambuf[0] = NUL;                  /* Clear as-name buffer */
  4765.  
  4766.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  4767.         ftp_typ = g_ftp_typ;
  4768.         /* g_ftp_typ = -1; */
  4769.     }
  4770.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  4771.         pv[i].sval = NULL;              /* to null pointers */
  4772.         pv[i].ival = -1;                /* and -1 int values */
  4773.         pv[i].wval = (CK_OFF_T)-1;    /* and -1 wide values */
  4774.     }
  4775.     if (who == 0) {                     /* Called with unprefixed command */
  4776.         switch (cx) {
  4777.           case XXRSEN:  pv[SND_RES].ival = 1; break;
  4778.           case XXCSEN:  pv[SND_CMD].ival = 1; break;
  4779.           case XXMOVE:  pv[SND_DEL].ival = 1; break;
  4780.           case XXMMOVE: pv[SND_DEL].ival = 1; /* fall thru */
  4781.           case XXMSE:   mput++; break;
  4782.         }
  4783.     } else {
  4784.         if (cx == FTP_MPU)
  4785.           mput++;
  4786.     }
  4787.     cmfdbi(&sw,                         /* First FDB - command switches */
  4788.            _CMKEY,                      /* fcode */
  4789.            "Filename, or switch",       /* hlpmsg */
  4790.            "",                          /* default */
  4791.            "",                          /* addtl string data */
  4792.            nputswi,                     /* addtl numeric data 1: tbl size */
  4793.            4,                           /* addtl numeric data 2: 4 = cmswi */
  4794.            xxstring,                    /* Processing function */
  4795.            putswi,                      /* Keyword table */
  4796.            &sf                          /* Pointer to next FDB */
  4797.            );
  4798.     cmfdbi(&fl,                         /* 3rd FDB - local filespec */
  4799.            _CMFLD,                      /* fcode */
  4800.            "",                          /* hlpmsg */
  4801.            "",                          /* default */
  4802.            "",                          /* addtl string data */
  4803.            0,                           /* addtl numeric data 1 */
  4804.            0,                           /* addtl numeric data 2 */
  4805.            xxstring,
  4806.            NULL,
  4807.            &cm
  4808.            );
  4809.     cmfdbi(&cm,                         /* 4th FDB - Confirmation */
  4810.            _CMCFM,                      /* fcode */
  4811.            "",                          /* hlpmsg */
  4812.            "",                          /* default */
  4813.            "",                          /* addtl string data */
  4814.            0,                           /* addtl numeric data 1 */
  4815.            0,                           /* addtl numeric data 2 */
  4816.            NULL,
  4817.            NULL,
  4818.            NULL
  4819.            );
  4820.  
  4821.   again:
  4822.     cmfdbi(&sf,                         /* 2nd FDB - file to send */
  4823.            _CMIFI,                      /* fcode */
  4824.            "",                          /* hlpmsg */
  4825.            "",                          /* default */
  4826.            "",                          /* addtl string data */
  4827.            /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
  4828.            nolinks | x_recurse,         /* addtl numeric data 1 */
  4829.            0,                           /* dirflg 0 means "not dirs only" */
  4830.            xxstring,
  4831.            NULL,
  4832. #ifdef COMMENT
  4833.            mput ? &cm : &fl
  4834. #else
  4835.        &fl
  4836. #endif /* COMMENT */
  4837.            );
  4838.  
  4839.     while (1) {                         /* Parse zero or more switches */
  4840.         x = cmfdb(&sw);                 /* Parse something */
  4841.         debug(F101,"ftp put cmfdb A","",x);
  4842.         debug(F101,"ftp put fcode A","",cmresult.fcode);
  4843.         if (x < 0)                      /* Error */
  4844.           goto xputx;                   /* or reparse needed */
  4845.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  4846.           break;
  4847.         c = cmgbrk();                   /* Get break character */
  4848.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  4849.         if (getval && !(cmresult.kflags & CM_ARG)) {
  4850.             printf("?This switch does not take arguments\n");
  4851.             x = -9;
  4852.             goto xputx;
  4853.         }
  4854.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  4855.             printf("?This switch requires an argument\n");
  4856.             x = -9;
  4857.             goto xputx;
  4858.         }
  4859.         n = cmresult.nresult;           /* Numeric result = switch value */
  4860.         debug(F101,"ftp put switch","",n);
  4861.  
  4862.         switch (n) {                    /* Process the switch */
  4863.           case SND_AFT:                 /* Send /AFTER:date-time */
  4864.           case SND_BEF:                 /* Send /BEFORE:date-time */
  4865.           case SND_NAF:                 /* Send /NOT-AFTER:date-time */
  4866.           case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
  4867.             if (!getval) break;
  4868.             if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
  4869.                 if (x == -3) {
  4870.                     printf("?Date-time required\n");
  4871.                     x = -9;
  4872.                 }
  4873.                 goto xputx;
  4874.             }
  4875.             pv[n].ival = 1;
  4876.             makestr(&(pv[n].sval),s);
  4877.             break;
  4878.  
  4879.           case SND_ASN:                 /* /AS-NAME: */
  4880.             debug(F101,"ftp put /as-name getval","",getval);
  4881.             if (!getval) break;
  4882.             if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
  4883.                 if (x == -3) {
  4884.                     printf("?name required\n");
  4885.                     x = -9;
  4886.                 }
  4887.                 goto xputx;
  4888.             }
  4889.             makestr(&(pv[n].sval),brstrip(s));
  4890.             debug(F110,"ftp put /as-name 1",pv[n].sval,0);
  4891.             if (pv[n].sval) pv[n].ival = 1;
  4892.             break;
  4893.  
  4894. #ifdef PUTARRAY
  4895.           case SND_ARR:                 /* /ARRAY */
  4896.             if (!getval) break;
  4897.             ap = NULL;
  4898.             if ((x = cmfld("Array name (a single letter will do)",
  4899.                            "",
  4900.                            &s,
  4901.                            NULL
  4902.                            )) < 0) {
  4903.                 if (x == -3)
  4904.           break;
  4905.         else
  4906.           return(x);
  4907.             }
  4908.             if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
  4909.                 printf("?Bad array: %s\n",s);
  4910.                 return(-9);
  4911.             }
  4912.             if (!(ap = a_ptr[x])) {
  4913.                 printf("?No such array: %s\n",s);
  4914.                 return(-9);
  4915.             }
  4916.             pv[n].ival = 1;
  4917.             pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
  4918.             pv[SND_RES].ival = 0;
  4919.             pv[SND_FIL].ival = 0;
  4920.             arrayx = x;
  4921.             break;
  4922. #endif /* PUTARRAY */
  4923.  
  4924.           case SND_BIN:                 /* /BINARY */
  4925.           case SND_TXT:                 /* /TEXT or /ASCII */
  4926.           case SND_TEN:                 /* /TENEX */
  4927.             pv[SND_BIN].ival = 0;
  4928.             pv[SND_TXT].ival = 0;
  4929.             pv[SND_TEN].ival = 0;
  4930.             pv[n].ival = 1;
  4931.             break;
  4932.  
  4933. #ifdef PUTPIPE
  4934.           case SND_CMD:                 /* These take no args */
  4935.             if (nopush) {
  4936.                 printf("?Sorry, system command access is disabled\n");
  4937.                 x = -9;
  4938.                 goto xputx;
  4939.             }
  4940. #ifdef PIPESEND
  4941.             else if (sndfilter) {
  4942.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  4943.                 x = -9;
  4944.                 goto xputx;
  4945.             }
  4946. #endif /* PIPESEND */
  4947.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  4948.             pv[n].ival = 1;             /* Just set the flag */
  4949.             pv[SND_ARR].ival = 0;
  4950.             break;
  4951. #endif /* PUTPIPE */
  4952.  
  4953. #ifdef CKSYMLINK
  4954.           case SND_LNK:
  4955.             nolinks = 0;
  4956.             goto again;            /* Because CMIFI params changed... */
  4957.           case SND_NLK:
  4958.             nolinks = 2;
  4959.             goto again;
  4960. #endif /* CKSYMLINK */
  4961.  
  4962. #ifdef FTP_RESTART
  4963.           case SND_RES:                 /* /RECOVER (resend) */
  4964.             pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
  4965. #endif /* FTP_RESTART */
  4966.  
  4967.           case SND_NOB:
  4968.           case SND_DEL:                 /* /DELETE */
  4969.           case SND_SHH:                 /* /QUIET */
  4970.           case SND_UPD:                 /* /UPDATE */
  4971.           case SND_SIM:                 /* /UPDATE */
  4972.           case SND_USN:                 /* /UNIQUE */
  4973.             pv[n].ival = 1;             /* Just set the flag */
  4974.             break;
  4975.  
  4976.           case SND_REC:                 /* /RECURSIVE */
  4977.             recursive = 2;              /* Must be set before cmifi() */
  4978.             x_recurse = 1;
  4979.             goto again;            /* Because CMIFI params changed... */
  4980.             break;
  4981.  
  4982. #ifdef UNIXOROSK
  4983.           case SND_DOT:                 /* /DOTFILES */
  4984.             matchdot = 1;
  4985.             break;
  4986.           case SND_NOD:                 /* /NODOTFILES */
  4987.             matchdot = 0;
  4988.             break;
  4989. #endif /* UNIXOROSK */
  4990.  
  4991.           case SND_ERR:                 /* /ERROR-ACTION */
  4992.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  4993.               goto xputx;
  4994.             pv[n].ival = x;
  4995.             break;
  4996.  
  4997.           case SND_EXC:                 /* Excludes */
  4998.             if (!getval) break;
  4999.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  5000.                 if (x == -3) {
  5001.                     printf("?Pattern required\n");
  5002.                     x = -9;
  5003.                 }
  5004.                 goto xputx;
  5005.             }
  5006.             if (s) if (!*s) s = NULL;
  5007.             makestr(&(pv[n].sval),s);
  5008.             if (pv[n].sval)
  5009.               pv[n].ival = 1;
  5010.             break;
  5011.  
  5012.           case SND_PRM:                 /* /PERMISSIONS */
  5013.             if (!getval)
  5014.               x = 1;
  5015.             else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
  5016.               goto xputx;
  5017.             pv[SND_PRM].ival = x;
  5018.             break;
  5019.  
  5020. #ifdef PIPESEND
  5021.           case SND_FLT:                 /* /FILTER */
  5022.             debug(F101,"ftp put /filter getval","",getval);
  5023.             if (!getval) break;
  5024.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  5025.                 if (x == -3)
  5026.                   s = "";
  5027.                 else
  5028.                   goto xputx;
  5029.             }
  5030.             if (*s) s = brstrip(s);
  5031.             y = strlen(s);
  5032.             for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
  5033.                 if (s[x] != '\\') continue;
  5034.                 if (s[x+1] == 'v') break;
  5035.             }
  5036.             if (x == y) {
  5037.                 printf(
  5038.                 "?Filter must contain a replacement variable for filename.\n"
  5039.                        );
  5040.                 x = -9;
  5041.                 goto xputx;
  5042.             }
  5043.             if (s) if (!*s) s = NULL;
  5044.             makestr(&(pv[n].sval),s);
  5045.             if (pv[n].sval)
  5046.               pv[n].ival = 1;
  5047.             break;
  5048. #endif /* PIPESEND */
  5049.  
  5050.           case SND_NAM:                 /* /FILENAMES */
  5051.             if (!getval) break;
  5052.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  5053.               goto xputx;
  5054.             debug(F101,"ftp put /filenames","",x);
  5055.             pv[n].ival = x;
  5056.             break;
  5057.  
  5058.           case SND_SMA:                 /* Smaller / larger than */
  5059.           case SND_LAR: {
  5060.           CK_OFF_T y;
  5061.           if (!getval) break;
  5062.           if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
  5063.         goto xputx;
  5064.           pv[n].wval = y;
  5065.           break;
  5066.       }
  5067.           case SND_FIL:                 /* Name of file containing filenames */
  5068.             if (!getval) break;
  5069.             if ((x = cmifi("Name of file containing list of filenames",
  5070.                                "",&s,&y,xxstring)) < 0) {
  5071.                 if (x == -3) {
  5072.                     printf("?Filename required\n");
  5073.                     x = -9;
  5074.                 }
  5075.                 goto xputx;
  5076.             } else if (y && iswild(s)) {
  5077.                 printf("?Wildcards not allowed\n");
  5078.                 x = -9;
  5079.                 goto xputx;
  5080.             }
  5081.             if (s) if (!*s) s = NULL;
  5082.             makestr(&(pv[n].sval),s);
  5083.             if (pv[n].sval) {
  5084.                 pv[n].ival = 1;
  5085.                 pv[SND_ARR].ival = 0;
  5086.             } else {
  5087.                 pv[n].ival = 0;
  5088.             }
  5089.             mput = 0;
  5090.             break;
  5091.  
  5092.           case SND_MOV:                 /* MOVE after */
  5093.           case SND_REN:                 /* RENAME after */
  5094.           case SND_SRN: {               /* SERVER-RENAME after */
  5095.               char * m = "";
  5096.               switch (n) {
  5097.                 case SND_MOV:
  5098.                   m = "device and/or directory for source file after sending";
  5099.                   break;
  5100.                 case SND_REN:
  5101.                   m = "new name for source file after sending";
  5102.                   break;
  5103.                 case SND_SRN:
  5104.                   m = "new name for destination file after sending";
  5105.                   break;
  5106.               }
  5107.               if (!getval) break;
  5108.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  5109.                   if (x == -3) {
  5110.                       printf("%s\n", n == SND_MOV ?
  5111.                              "?Destination required" :
  5112.                              "?New name required"
  5113.                              );
  5114.                       x = -9;
  5115.                   }
  5116.                   goto xputx;
  5117.               }
  5118.               if (s) if (!*s) s = NULL;
  5119.               makestr(&(pv[n].sval),s ? brstrip(s) : NULL);
  5120.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  5121.               break;
  5122.           }
  5123.           case SND_STA:                 /* Starting position (= PSEND) */
  5124.             if (!getval) break;
  5125.             if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
  5126.               goto xputx;
  5127.             pv[n].ival = y;
  5128.             break;
  5129.  
  5130.           case SND_TYP:                 /* /TYPE */
  5131.             if (!getval) break;
  5132.             if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
  5133.               goto xputx;
  5134.             pv[n].ival = (x == 2) ? -1 : x;
  5135.             break;
  5136.  
  5137. #ifndef NOCSETS
  5138.           case SND_CSL:                 /* Local character set */
  5139.           case SND_CSR:                 /* Remote (server) charset */
  5140.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0) {
  5141.         return((x == -3) ? -2 : x);
  5142.             }
  5143.         if (n == SND_CSL)
  5144.               x_csl = x;
  5145.             else
  5146.               x_csr = x;
  5147.             x_xla = 1;                  /* Overrides global OFF setting */
  5148.             break;
  5149.  
  5150.           case SND_XPA:                 /* Transparent */
  5151.             x_xla = 0;
  5152.             x_csr = -1;
  5153.             x_csl = -1;
  5154.             break;
  5155. #endif /* NOCSETS */
  5156.         }
  5157.     }
  5158. #ifdef PIPESEND
  5159.     if (pv[SND_RES].ival > 0) { /* /RECOVER */
  5160.         if (sndfilter || pv[SND_FLT].ival > 0) {
  5161.             printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
  5162.             x = -9;
  5163.             goto xputx;
  5164.         }
  5165.     if (sfttab[0] > 0 && sfttab[SFT_REST] == 0)
  5166.       printf("WARNING: Server says it doesn't support REST.\n");
  5167.     }
  5168. #endif /* PIPESEND */
  5169.  
  5170.     cmarg = "";
  5171.     cmarg2 = asnambuf;
  5172.     line[0] = NUL;
  5173.     s = line;
  5174.     wild = 0;
  5175.  
  5176.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  5177.       case _CMIFI:                      /* Input filename */
  5178.         if (pv[SND_FIL].ival > 0) {
  5179.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  5180.             x = -9;
  5181.             goto xputx;
  5182.         }
  5183.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
  5184.         if (pv[SND_ARR].ival > 0)
  5185.           ckstrncpy(asnambuf,line,CKMAXPATH);
  5186.         else
  5187.           wild = cmresult.nresult;      /* Wild flag */
  5188.         debug(F111,"ftp put wild",line,wild);
  5189.         if (!wild && !recursive && !mput)
  5190.           nolinks = 0;
  5191.         break;
  5192.       case _CMFLD:                      /* Field */
  5193.         /* Only allowed with /COMMAND and /ARRAY */
  5194.         if (pv[SND_FIL].ival > 0) {
  5195.             printf("?You may not give a PUT filespec and a /LISTFILE\n");
  5196.             x = -9;
  5197.             goto xputx;
  5198.         }
  5199.     /* For MPUT it's OK to have filespecs that don't match any files */
  5200.     if (mput)
  5201.       break;
  5202.         if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
  5203. #ifdef CKROOT
  5204.             if (ckrooterr)
  5205.               printf("?Off limits: %s\n",cmresult.sresult);
  5206.             else
  5207. #endif /* CKROOT */
  5208.               printf("?%s - \"%s\"\n",
  5209.                    iswild(cmresult.sresult) ?
  5210.                    "No files match" : "File not found",
  5211.                    cmresult.sresult
  5212.                    );
  5213.             x = -9;
  5214.             goto xputx;
  5215.         }
  5216.         ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
  5217.         if (pv[SND_ARR].ival > 0)
  5218.           ckstrncpy(asnambuf,line,CKMAXPATH);
  5219.         break;
  5220.       case _CMCFM:                      /* Confirmation */
  5221.         confirmed = 1;
  5222.         break;
  5223.       default:
  5224.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  5225.         x = -9;
  5226.         goto xputx;
  5227.     }
  5228.     debug(F110,"ftp put string",s,0);
  5229.     debug(F101,"ftp put confirmed","",confirmed);
  5230.  
  5231.     /* Save and change protocol and transfer mode */
  5232.     /* Global values are restored in main parse loop */
  5233.  
  5234.     g_displa = fdispla;
  5235.     if (ftp_dis > -1)
  5236.       fdispla = ftp_dis;
  5237.     g_skipbup = skipbup;
  5238.  
  5239.     if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
  5240.         g_skipbup = skipbup;
  5241.         skipbup = 1;
  5242.     }
  5243.     if (pv[SND_TYP].ival > -1) {        /* /TYPE */
  5244.         xfiletype = pv[SND_TYP].ival;
  5245.         if (xfiletype == 2)
  5246.           xfiletype = -1;
  5247.     }
  5248.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  5249.         forcetype = 1;                  /* So skip file scan */
  5250.         ftp_typ = FTT_BIN;              /* Set binary */
  5251.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  5252.         forcetype = 1;
  5253.         ftp_typ = FTT_ASC;
  5254.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  5255.         forcetype = 1;
  5256.         ftp_typ = FTT_TEN;
  5257.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  5258.         forcetype = 1;
  5259.         ftp_typ = binary;
  5260.         g_ftp_typ = binary;
  5261.     }
  5262.  
  5263. #ifdef PIPESEND
  5264.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  5265.         debug(F110,"PUT /COMMAND before stripping",s,0);
  5266.         s = brstrip(s);
  5267.         debug(F110,"PUT /COMMAND after stripping",s,0);
  5268.         if (!*s) {
  5269.             printf("?Sorry, a command to send from is required\n");
  5270.             x = -9;
  5271.             goto xputx;
  5272.         }
  5273.         cmarg = s;
  5274.     }
  5275. #endif /* PIPESEND */
  5276.  
  5277. /* Set up /MOVE and /RENAME */
  5278.  
  5279.     if (pv[SND_DEL].ival > 0 &&
  5280.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  5281.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  5282.         x = -9;
  5283.         goto xputx;
  5284.     }
  5285. #ifdef CK_TMPDIR
  5286.     if (pv[SND_MOV].ival > 0) {
  5287.         int len;
  5288.         char * p = pv[SND_MOV].sval;
  5289.         len = strlen(p);
  5290.         if (!isdir(p)) {                /* Check directory */
  5291. #ifdef CK_MKDIR
  5292.             char * s = NULL;
  5293.             s = (char *)malloc(len + 4);
  5294.             if (s) {
  5295.                 strcpy(s,p);            /* safe */
  5296. #ifdef datageneral
  5297.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  5298. #else
  5299.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  5300. #endif /* datageneral */
  5301.                 s[len++] = 'X';
  5302.                 s[len] = NUL;
  5303. #ifdef NOMKDIR
  5304.                 x = -1;
  5305. #else
  5306.                 x = zmkdir(s);
  5307. #endif /* NOMKDIR */
  5308.                 free(s);
  5309.                 if (x < 0) {
  5310.                     printf("?Can't create \"%s\"\n",p);
  5311.                     x = -9;
  5312.                     goto xputx;
  5313.                 }
  5314.             }
  5315. #else
  5316.             printf("?Directory \"%s\" not found\n",p);
  5317.             x = -9;
  5318.             goto xputx;
  5319. #endif /* CK_MKDIR */
  5320.         }
  5321.         makestr(&snd_move,p);
  5322.     }
  5323. #endif /* CK_TMPDIR */
  5324.  
  5325.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  5326.         char * p = pv[SND_REN].sval;
  5327.         if (!p) p = "";
  5328.         if (!*p) {
  5329.             printf("?New name required for /RENAME\n");
  5330.             x = -9;
  5331.             goto xputx;
  5332.         }
  5333.         p = brstrip(p);
  5334. #ifndef NOSPL
  5335.     /* If name given is wild, rename string must contain variables */
  5336.         if (wild) {
  5337.             char * s = tmpbuf;
  5338.             x = TMPBUFSIZ;
  5339.             zzstring(p,&s,&x);
  5340.             if (!strcmp(tmpbuf,p)) {
  5341.                 printf(
  5342.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  5343.                        );
  5344.                 x = -9;
  5345.                 goto xputx;
  5346.             }
  5347.         }
  5348. #endif /* NOSPL */
  5349.         makestr(&snd_rename,p);
  5350.         debug(F110,"FTP snd_rename",snd_rename,0);
  5351.     }
  5352.     if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
  5353.         char * p = pv[SND_SRN].sval;
  5354.         if (!p) p = "";
  5355.         if (!*p) {
  5356.             printf("?New name required for /SERVER-RENAME\n");
  5357.             x = -9;
  5358.             goto xputx;
  5359.         }
  5360.         p = brstrip(p);
  5361. #ifndef NOSPL
  5362.         if (wild) {
  5363.             char * s = tmpbuf;
  5364.             x = TMPBUFSIZ;
  5365.             zzstring(p,&s,&x);
  5366.             if (!strcmp(tmpbuf,p)) {
  5367.                 printf(
  5368. "?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
  5369.                        );
  5370.                 x = -9;
  5371.                 goto xputx;
  5372.             }
  5373.         }
  5374. #endif /* NOSPL */
  5375.         makestr(&srv_renam,p);
  5376.         debug(F110,"ftp put srv_renam",srv_renam,0);
  5377.     }
  5378.     if (!confirmed) {                   /* CR not typed yet, get more fields */
  5379.         char * lp;
  5380.         if (mput) {                     /* MPUT or MMOVE */
  5381.             nfils = 0;                  /* We already have the first one */
  5382. #ifndef NOMSEND
  5383.         if (cmresult.fcode == _CMIFI) {
  5384.         /* First filespec is valid */
  5385.         msfiles[nfils++] = line;    /* Store pointer */
  5386.         lp = line + (int)strlen(line) + 1; /* Point past it */
  5387.         debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
  5388.         } else {
  5389.         /* First filespec matches no files */
  5390.         debug(F110,"ftp put mput skipping first filespec",
  5391.               cmresult.sresult,
  5392.               0
  5393.               );
  5394.         lp = line;
  5395.         }
  5396.         /* Parse a filespec, a "field", or confirmation */
  5397.  
  5398.         cmfdbi(&sf,            /* 1st FDB - file to send */
  5399.            _CMIFI,        /* fcode */
  5400.            "",            /* hlpmsg */
  5401.            "",            /* default */
  5402.            "",            /* addtl string data */
  5403.            nolinks | x_recurse,    /* addtl numeric data 1 */
  5404.            0,            /* dirflg 0 means "not dirs only" */
  5405.            xxstring,
  5406.            NULL,
  5407.            &fl
  5408.            );
  5409.         cmfdbi(&fl,            /* 2nd FDB - local filespec */
  5410.            _CMFLD,        /* fcode */
  5411.            "",            /* hlpmsg */
  5412.            "",            /* default */
  5413.            "",            /* addtl string data */
  5414.            0,            /* addtl numeric data 1 */
  5415.            0,            /* addtl numeric data 2 */
  5416.            xxstring,
  5417.            NULL,
  5418.            &cm
  5419.            );
  5420.         cmfdbi(&cm,            /* 3rd FDB - Confirmation */
  5421.            _CMCFM,        /* fcode */
  5422.            "",
  5423.            "",
  5424.            "",
  5425.            0,
  5426.            0,
  5427.            NULL,
  5428.            NULL,
  5429.            NULL
  5430.            );
  5431.  
  5432.             while (!confirmed) {    /* Get more filenames */
  5433.         x = cmfdb(&sf);        /* Parse something */
  5434.         debug(F101,"ftp put cmfdb B","",x);
  5435.         debug(F101,"ftp put fcode B","",cmresult.fcode);
  5436.         if (x < 0)        /* Error */
  5437.           goto xputx;        /* or reparse needed */
  5438.         switch (cmresult.fcode) {
  5439.           case _CMCFM:        /* End of command */
  5440.             confirmed++;
  5441.             if (nfils < 1) {
  5442.             debug(F100,"ftp put mput no files match","",0);
  5443.             printf("?No files match MPUT list\n");
  5444.             x = -9;
  5445.             goto xputx;
  5446.             }
  5447.             break;
  5448.           case _CMFLD:        /* No match */
  5449.             debug(F110,"ftp put mput skipping",cmresult.sresult,0);
  5450.             continue;
  5451.           case _CMIFI:        /* Good match */
  5452.             s = cmresult.sresult;
  5453.             msfiles[nfils++] = lp; /* Got one, count, point to it, */
  5454.             p = lp;           /* remember pointer, */
  5455.             while ((*lp++ = *s++)) /* and copy it into buffer */
  5456.               if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
  5457.               printf("?MPUT list too long\n");
  5458.               line[0] = NUL;
  5459.               x = -9;
  5460.               goto xputx;
  5461.               }
  5462.             debug(F111,"ftp put mput adding",msfiles[nfils-1],nfils-1);
  5463.             if (nfils == 1)    /* Take care of \v(filespec) */
  5464.               fspec[0] = NUL;
  5465. #ifdef ZFNQFP
  5466.             zfnqfp(p,TMPBUFSIZ,tmpbuf);
  5467.             p = tmpbuf;
  5468. #endif /* ZFNQFP */
  5469.             if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
  5470.             strcat(fspec,p);    /* safe */
  5471.             strcat(fspec," ");  /* safe */
  5472.             } else {
  5473. #ifdef COMMENT
  5474.             printf("WARNING - \\v(filespec) buffer overflow\n");
  5475. #else
  5476.             debug(F101,"doxput filespec buffer overflow","",0);
  5477. #endif /* COMMENT */
  5478.             }
  5479.         }
  5480.         }
  5481. #endif /* NOMSEND */
  5482.         } else {                        /* Regular PUT */
  5483.             nfils = -1;
  5484.             if ((x = cmtxt(wild ?
  5485. "\nOptional as-name template containing replacement variables \
  5486. like \\v(filename)" :
  5487.                            "Optional name to send it with",
  5488.                            "",&p,NULL)) < 0)
  5489.               goto xputx;
  5490.  
  5491.             if (p) if (!*p) p = NULL;
  5492.             p = brstrip(p);
  5493.  
  5494.             if (p && *p) {
  5495.                 makestr(&(pv[SND_ASN].sval),p);
  5496.                 if (pv[SND_ASN].sval)
  5497.                   pv[SND_ASN].ival = 1;
  5498.                 debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
  5499.             }
  5500.         }
  5501.     }
  5502.     /* Set cmarg2 from as-name, however we got it. */
  5503.  
  5504.     CHECKCONN();
  5505.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  5506.         char * p;
  5507.         p = brstrip(pv[SND_ASN].sval);
  5508.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  5509.     }
  5510.     debug(F110,"ftp put asnambuf",asnambuf,0);
  5511.  
  5512.     if (pv[SND_FIL].ival > 0) {
  5513.         if (confirmed) {
  5514.             if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  5515.                 debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
  5516.                 printf("?Failure to open %s\n",pv[SND_FIL].sval);
  5517.                 x = -9;
  5518.                 goto xputx;
  5519.             }
  5520.             makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
  5521.             debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
  5522.             wild = 1;
  5523.         }
  5524.     }
  5525.     if (confirmed && !line[0] && !filefile) {
  5526. #ifndef NOMSEND
  5527.         if (filehead) {                 /* OK if we have a SEND-LIST */
  5528.             nfils = filesinlist;
  5529.             sndsrc = nfils;             /* Like MSEND */
  5530.             addlist = 1;                /* But using a different list... */
  5531.             filenext = filehead;
  5532.             goto doput;
  5533.         }
  5534. #endif /* NOMSEND */
  5535.         printf("?Filename required but not given\n");
  5536.         x = -9;
  5537.         goto xputx;
  5538.     }
  5539. #ifndef NOMSEND
  5540.     addlist = 0;                        /* Don't use SEND-LIST. */
  5541. #endif /* NOMSEND */
  5542.  
  5543.     if (mput) {                         /* MPUT (rather than PUT) */
  5544. #ifndef NOMSEND
  5545.         cmlist = msfiles;               /* List of filespecs */
  5546.         sndsrc = nfils;                 /* rather filespec and as-name */
  5547. #endif /* NOMSEND */
  5548.         pipesend = 0;
  5549.     } else if (filefile) {              /* File contains list of filenames */
  5550.         s = "";
  5551.         cmarg = "";
  5552.         line[0] = NUL;
  5553.         nfils = 1;
  5554.         sndsrc = 1;
  5555.  
  5556.     } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
  5557.  
  5558.         /* Not MSEND, MMOVE, /LIST, or /ARRAY */
  5559.         nfils = sndsrc = -1;
  5560.         if (!wild) {
  5561.             if (zchki(s) < 0) {
  5562.                 printf("?Read access denied - \"%s\"\n", s);
  5563.                 x = -9;
  5564.                 goto xputx;
  5565.             }
  5566.         }
  5567.         if (s != line)                  /* We might already have done this. */
  5568.           ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
  5569. #ifdef DEBUG
  5570.         else
  5571.           debug(F110,"doxput line=s",line,0);
  5572. #endif /* DEBUG */
  5573.         cmarg = line;                   /* File to send */
  5574.     }
  5575. #ifndef NOMSEND
  5576.     zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
  5577. #endif /* NOMSEND */
  5578.  
  5579.     if (!mput) {                        /* For all but MPUT... */
  5580. #ifdef PIPESEND
  5581.         if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
  5582.           pipesend = 1;
  5583.         debug(F101,"ftp put /COMMAND pipesend","",pipesend);
  5584.         if (pipesend && filefile) {
  5585.             printf("?Invalid switch combination\n");
  5586.             x = -9;
  5587.             goto xputx;
  5588.         }
  5589. #endif /* PIPESEND */
  5590.  
  5591. #ifndef NOSPL
  5592.     /* If as-name given and filespec is wild, as-name must contain variables */
  5593.         if ((wild || mput) && asnambuf[0]) {
  5594.             char * s = tmpbuf;
  5595.             x = TMPBUFSIZ;
  5596.             zzstring(asnambuf,&s,&x);
  5597.             if (!strcmp(tmpbuf,asnambuf)) {
  5598.                 printf(
  5599.     "?As-name for file group must contain variables such as \\v(filename)\n"
  5600.                        );
  5601.                 x = -9;
  5602.                 goto xputx;
  5603.             }
  5604.         }
  5605. #endif /* NOSPL */
  5606.     }
  5607.  
  5608.   doput:
  5609.  
  5610.     if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
  5611.         fdispla = 0;
  5612.         debug(F101,"ftp put display","",fdispla);
  5613.     } else {
  5614.         displa = 1;
  5615.         if (ftp_deb)
  5616.       fdispla = XYFD_B;
  5617.     }
  5618.  
  5619. #ifdef PUTARRAY                         /* SEND /ARRAY... */
  5620.     if (pv[SND_ARR].ival > 0) {
  5621.         if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
  5622.         if (range[0] == -1)             /* If low end of range not specified */
  5623.           range[0] = 1;                 /* default to 1 */
  5624.         if (range[1] == -1)             /* If high not specified */
  5625.           range[1] = a_dim[arrayx];     /* default to size of array */
  5626.         if ((range[0] < 0) ||           /* Check range */
  5627.             (range[0] > a_dim[arrayx]) ||
  5628.             (range[1] < range[0]) ||
  5629.             (range[1] > a_dim[arrayx])) {
  5630.             printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
  5631.             x = -9;
  5632.             goto xputx;
  5633.         }
  5634.         sndarray = ap;                  /* Array pointer */
  5635.         sndxin = arrayx;                /* Array index */
  5636.         sndxlo = range[0];              /* Array range */
  5637.         sndxhi = range[1];
  5638.         sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
  5639.         if (!asnambuf[0])
  5640.           ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
  5641.         cmarg = "";
  5642.     }
  5643. #endif /* PUTARRAY */
  5644.  
  5645.     moving = 0;
  5646.  
  5647.     if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
  5648.         if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
  5649.           moving = 1;
  5650.         if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
  5651.           ckstrncpy(sndafter,pv[SND_AFT].sval,19);
  5652.         if (pv[SND_BEF].ival > 0)
  5653.           ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
  5654.         if (pv[SND_NAF].ival > 0)
  5655.           ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
  5656.         if (pv[SND_NBE].ival > 0)
  5657.           ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
  5658.         if (pv[SND_EXC].ival > 0)
  5659.           makelist(pv[SND_EXC].sval,sndexcept,NSNDEXCEPT);
  5660.         if (pv[SND_SMA].ival > -1)
  5661.           sndsmaller = pv[SND_SMA].wval;
  5662.         if (pv[SND_LAR].ival > -1)
  5663.           sndlarger = pv[SND_LAR].wval;
  5664.         if (pv[SND_NAM].ival > -1)
  5665.           x_cnv = pv[SND_NAM].ival;
  5666.         if (pv[SND_USN].ival > -1)
  5667.           x_usn = pv[SND_USN].ival;
  5668.         if (pv[SND_ERR].ival > -1)
  5669.           puterror = pv[SND_ERR].ival;
  5670.  
  5671. #ifdef DOUPDATE
  5672.         if (pv[SND_UPD].ival > 0) {
  5673.             if (x_usn) {
  5674.                 printf("?Conflicting switches: /UPDATE /UNIQUE\n");
  5675.                 x = -9;
  5676.                 goto xputx;
  5677.             }
  5678.             putflags |= PUT_UPD;
  5679.         ftp_dates |= 2;
  5680.         }
  5681. #ifdef COMMENT
  5682.     /* This works but it's useless, maybe dangerous */
  5683.         if (pv[SND_DIF].ival > 0) {
  5684.             if (x_usn) {
  5685.                 printf("?Conflicting switches: /DATES-DIFFER /UNIQUE\n");
  5686.                 x = -9;
  5687.                 goto xputx;
  5688.             }
  5689.             putflags |= PUT_DIF;
  5690.         ftp_dates |= 2;
  5691.         }
  5692. #endif /* COMMENT */
  5693. #endif /* DOUPDATE */
  5694.  
  5695.         if (pv[SND_SIM].ival > 0)
  5696.           putflags |= PUT_SIM;
  5697.  
  5698.         if (pv[SND_PRM].ival > -1) {
  5699. #ifdef UNIX
  5700.             if (x_usn) {
  5701.                 printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
  5702.                 x = -9;
  5703.                 goto xputx;
  5704.             }
  5705.             x_prm = pv[SND_PRM].ival;
  5706. #else /* UNIX */
  5707.             printf("?/PERMISSIONS switch is not supported\n");
  5708. #endif /* UNIX */
  5709.         }
  5710. #ifdef FTP_RESTART
  5711.         if (pv[SND_RES].ival > 0) {
  5712.         if (!sizeok) {
  5713.         printf("?PUT /RESTART can't be used because SIZE disabled.\n");
  5714.                 x = -9;
  5715.                 goto xputx;
  5716.         }
  5717.             if (x_usn || putflags) {
  5718.                 printf("?Conflicting switches: /RECOVER %s\n",
  5719.                        x_usn && putflags ? "/UNIQUE /UPDATE" :
  5720.                        (x_usn ? "/UNIQUE" : "/UPDATE")
  5721.                        );
  5722.                 x = -9;
  5723.                 goto xputx;
  5724.             }
  5725. #ifndef NOCSETS
  5726.             if (x_xla &&
  5727.                 (x_csl == FC_UCS2 ||
  5728.                  x_csl == FC_UTF8 ||
  5729.                  x_csr == FC_UCS2 ||
  5730.                  x_csr == FC_UTF8)) {
  5731.                 printf("?/RECOVER can not be used with Unicode translation\n");
  5732.                 x = -9;
  5733.                 goto xputx;
  5734.             }
  5735. #endif /* NOCSETS */
  5736.             putflags = PUT_RES;
  5737.         }
  5738. #endif /* FTP_RESTART */
  5739.     }
  5740.     debug(F101,"ftp PUT restart","",putflags & PUT_RES);
  5741.     debug(F101,"ftp PUT update","",putflags & PUT_UPD);
  5742.  
  5743. #ifdef PIPESEND
  5744.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  5745.         if (!pv[SND_FLT].sval) {
  5746.             sndfilter = NULL;
  5747.         } else {
  5748.             sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
  5749.             if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
  5750.         }
  5751.         debug(F110,"ftp put /FILTER", sndfilter, 0);
  5752.     }
  5753.     if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
  5754.       if (putflags)                     /* with pipes or filters */
  5755.         putflags = 0;
  5756. #endif /* PIPESEND */
  5757.  
  5758.     tfc = (CK_OFF_T)0;            /* Initialize stats and counters */
  5759.     filcnt = 0;
  5760.     pktnum = 0;
  5761.     spackets = 0L;
  5762.  
  5763.     if (wild)                           /* (is this necessary?) */
  5764.       cx = FTP_MPU;
  5765.  
  5766.     t0 = gmstimer();                    /* Record starting time */
  5767.  
  5768.     done = 0;                           /* Loop control */
  5769.     cancelgroup = 0;
  5770.  
  5771.     cdlevel = 0;
  5772.     cdsimlvl = 0;
  5773.     while (!done && !cancelgroup) {     /* Loop for all files */
  5774.                                         /* or until canceled. */
  5775. #ifdef FTP_PROXY
  5776.         /*
  5777.            If we are using a proxy, we don't use the local file list;
  5778.            instead we use the list on the remote machine which we want
  5779.            sent to someone else, and we use remglob() to get the names.
  5780.            But in that case we shouldn't even be executing this routine;
  5781.            see ftp_mput().
  5782.         */
  5783. #endif /* FTP_PROXY */
  5784.  
  5785.         cancelfile = 0;
  5786.         x = gnfile();                   /* Get next file from list(s) */
  5787.  
  5788.         if (x == 0)                     /* (see gnfile() comments...) */
  5789.           x = gnferror;
  5790.         debug(F111,"FTP PUT gnfile",filnam,x);
  5791.  
  5792.         switch (x) {
  5793.           case 1:                       /* File to send */
  5794.             s2 = asnambuf;
  5795. #ifndef NOSPL
  5796.             if (asnambuf[0]) {          /* As-name */
  5797.                 int n; char *p;         /* to be evaluated... */
  5798.                 n = TMPBUFSIZ;
  5799.                 p = tmpbuf;
  5800.                 zzstring(asnambuf,&p,&n);
  5801.                 s2 = tmpbuf;
  5802.                 debug(F110,"ftp put asname",s2,0);
  5803.             }
  5804. #endif /* NOSPL */
  5805.             rc = putfile(cx,            /* Function (PUT, APPEND) */
  5806.                     filnam, s2,         /* Name to send, as-name */
  5807.                     forcetype, moving,  /* Parameters from switches... */
  5808.                     snd_move, snd_rename, srv_renam,
  5809.                     x_cnv, x_usn, xfiletype, x_prm,
  5810. #ifndef NOCSETS
  5811.                     x_csl, (!x_xla ? -1 : x_csr),
  5812. #else
  5813.                     -1, -1,
  5814. #endif /* NOCSETS */
  5815.                     putflags
  5816.                     );
  5817.             debug(F111,"ftp put putfile rc",filnam,rc);
  5818.             debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
  5819.             debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
  5820.             if (rc > -1) {
  5821.                 good++;
  5822.                 status = 1;
  5823.             }
  5824.             if (cancelfile)
  5825.               continue;
  5826.             if (rc < 0) {
  5827.                 ftp_fai++;
  5828.                 if (puterror) {
  5829.                     status = 0;
  5830.                     printf("?Fatal upload error: %s\n",filnam);
  5831.                     done++;
  5832.                 }
  5833.             }
  5834.             continue;
  5835.           case 0:                       /* No more files, done */
  5836.             done++;
  5837.             continue;
  5838.           case -1:
  5839.             printf("?%s: file not found - \"%s\"\n",
  5840.                    puterror ? "Fatal" : "Warning",
  5841.                    filnam
  5842.                    );
  5843.             if (puterror) {
  5844.                 status = 0;
  5845.                 done++;
  5846.                 break;
  5847.             }
  5848.             continue;
  5849.           case -2:
  5850.             if (puterror) {
  5851.                 printf("?Fatal: file not found - \"%s\"\n", filnam);
  5852.                 status = 0;
  5853.                 done++;
  5854.                 break;
  5855.             }
  5856.             continue;                   /* Not readable, keep going */
  5857.           case -3:
  5858.             if (puterror) {
  5859.                 printf("?Fatal: Read access denied - \"%s\"\n", filnam);
  5860.                 status = 0;
  5861.                 done++;
  5862.                 break;
  5863.             }
  5864.             printf("?Warning access denied - \"%s\"\n", filnam);
  5865.             continue;
  5866. #ifdef COMMENT
  5867.           case -4:                      /* Canceled */
  5868.             done++;
  5869.             break;
  5870. #endif /* COMMENT */
  5871.           case -5:
  5872.             printf("?Too many files match\n");
  5873.             done++;
  5874.             break;
  5875.           case -6:
  5876.             if (good < 1)
  5877.               printf("?No files selected\n");
  5878.             done++;
  5879.             break;
  5880.           default:
  5881.             printf("?getnextfile() - unknown failure\n");
  5882.             done++;
  5883.         }
  5884.     }
  5885.     if (cdlevel > 0) {
  5886.         while (cdlevel--) {
  5887.             if (cdsimlvl) {
  5888.                 cdsimlvl--;
  5889.             } else if (!doftpcdup())
  5890.               break;
  5891.         }
  5892.     }
  5893.     if (status > 0) {
  5894.         if (cancelgroup)
  5895.           status = 0;
  5896.         else if (cancelfile && good < 1)
  5897.           status = 0;
  5898.     }
  5899.     success = status;
  5900.     x = success;
  5901.  
  5902.   xputx:
  5903.     if (x > -1) {
  5904. #ifdef GFTIMER
  5905.         t1 = gmstimer();                /* End time */
  5906.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  5907.         if (!sec) sec = 0.001;
  5908.         fptsecs = sec;
  5909. #else
  5910.         sec = (t1 - t0) / 1000;
  5911.         if (!sec) sec = 1;
  5912. #endif /* GFTIMER */
  5913.         tfcps = (long) (tfc / sec);
  5914.         tsecs = (int)sec;
  5915.         lastxfer = W_FTP|W_SEND;
  5916.         xferstat = success;
  5917.         if (dpyactive)
  5918.           ftscreen(SCR_TC,0,(CK_OFF_T)0,"");
  5919.     }
  5920.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  5921.         if (pv[i].sval)
  5922.           free(pv[i].sval);
  5923.     }
  5924.     ftreset();                          /* Undo switch effects */
  5925.     dpyactive = 0;
  5926.     return(x);
  5927. }
  5928.  
  5929.  
  5930. static char ** mgetlist = NULL;         /* For MGET */
  5931. static int mgetn = 0, mgetx = 0;
  5932. static char xtmpbuf[4096];
  5933.  
  5934. /*
  5935.   c m d l i n g e t
  5936.  
  5937.   Get files specified by -g command-line option.
  5938.   File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
  5939. */
  5940. int
  5941. cmdlinget(stay) int stay; {
  5942.     int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
  5943.     int lcs = -1, rcs = -1, xlate = 0;
  5944.     int first = 1;
  5945.     int mget = 1;
  5946.     int nc;
  5947.     char * s, * s2, * s3;
  5948.     ULONG t0, t1;                       /* Times for stats */
  5949. #ifdef GFTIMER
  5950.     CKFLOAT sec;
  5951. #else
  5952.     int sec = 0;
  5953. #endif /* GFTIMER */
  5954.  
  5955.     if (quiet) {                        /* -q really means quiet */
  5956.         displa = 0;
  5957.         fdispla = 0;
  5958.     } else {
  5959.         displa = 1;
  5960.         fdispla = XYFD_B;
  5961.     }
  5962.     testing = 0;
  5963.     dpyactive = 0;
  5964.     out2screen = 0;
  5965.     what = W_FTP|W_RECV;
  5966.     mgetmethod = 0;
  5967.     mgetforced = 0;
  5968.  
  5969.     havetype = 0;
  5970.     havesize = (CK_OFF_T)-1;
  5971.     makestr(&havemdtm,NULL);
  5972.  
  5973.     if (ftp_fnc < 0)
  5974.       ftp_fnc = fncact;
  5975.  
  5976. #ifndef NOSPL
  5977.     cmd_quoting = 0;
  5978. #endif /* NOSPL */
  5979.     debug(F101,"ftp cmdlinget nfils","",nfils);
  5980.  
  5981.     if (ftp_cnv == CNV_AUTO) {          /* Name conversion is auto */
  5982.         if (alike) {                    /* If server & client are alike */
  5983.             nc = 0;                     /* no conversion */
  5984.         } else {                        /* If they are different */
  5985.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  5986.               nc = -1;                  /* only minimal conversions needed */
  5987.             else                        /* otherwise */
  5988.               nc = 1;                   /* full conversion */
  5989.         }
  5990.     } else                              /* Not auto - do what user said */
  5991.       nc = ftp_cnv;
  5992.  
  5993.     if (nfils < 1)
  5994.       doexit(BAD_EXIT,-1);
  5995.  
  5996.     t0 = gmstimer();                    /* Starting time for this batch */
  5997.  
  5998. #ifndef NOCSETS
  5999.     if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
  6000.         lcs = ftp_csl;                  /* Local charset */
  6001.         if (lcs < 0) lcs = fcharset;
  6002.         if (lcs < 0) xlate = 0;
  6003.     }
  6004.     if (xlate) {                        /* Still ON? */
  6005.         rcs = ftp_csx;                  /* Remote (Server) charset */
  6006.         if (rcs < 0) rcs = ftp_csr;
  6007.         if (rcs < 0) xlate = 0;
  6008.     }
  6009. #endif /* NOCSETS */
  6010.     /*
  6011.       If we have only one file and it is a directory, then we ask for a
  6012.       listing of its contents, rather than retrieving the directory file
  6013.       itself.  This is what (e.g.) Netscape does.
  6014.     */
  6015.     if (nfils == 1) {
  6016.         if (doftpcwd((char *)cmlist[mgetx],-1)) {
  6017.             /* If we can CD to it, it must be a directory */
  6018.             if (recursive) {
  6019.                 cmlist[mgetx] = "*";
  6020.             } else {
  6021.                 status =
  6022.                   (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
  6023.                 done = 1;
  6024.             }
  6025.         }
  6026.     }
  6027. /*
  6028.   The following is to work around UNIX servers which, when given a command
  6029.   like "NLST path/blah" (not wild) returns the basename without the path.
  6030. */
  6031.     if (!done && servertype == SYS_UNIX && nfils == 1) {
  6032.         mget = iswild(cmlist[mgetx]);
  6033.     }
  6034.     if (!mget && !done) {               /* Invoked by command-line FTP URL */
  6035.         if (ftp_deb)
  6036.           printf("DOING GET...\n");
  6037.         done++;
  6038.         cancelfile = 0;                 /* This file not canceled yet */
  6039.         s = cmlist[mgetx];
  6040.         rc = 0;                         /* Initial return code */
  6041.     fsize = (CK_OFF_T)-1;
  6042.     if (sizeok) {
  6043.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
  6044.         if (x == REPLY_COMPLETE)
  6045.           fsize = ckatofs(&ftp_reply_str[4]);
  6046.     }
  6047.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6048.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  6049.  
  6050.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  6051.         s2 = tmpbuf;
  6052.  
  6053.         /* If local file already exists, take collision action */
  6054.  
  6055.         if (zchki(s2) > -1) {
  6056.             switch (ftp_fnc) {
  6057.               case XYFX_A:              /* Append */
  6058.                 append = 1;
  6059.                 break;
  6060.               case XYFX_R:              /* Rename */
  6061.               case XYFX_B: {            /* Backup */
  6062.                   char * p = NULL;
  6063.                   int x = -1;
  6064.                   znewn(s2,&p);         /* Make unique name */
  6065.                   debug(F110,"ftp cmdlinget znewn",p,0);
  6066.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  6067.                       x = zrename(s2,p);
  6068.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  6069.                   } else {              /* Rename incoming file */
  6070.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6071.                       s2 = tmpbuf;
  6072.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  6073.                   }
  6074.                   if (x < 0) {
  6075.                       printf("?Backup/Rename failed\n");
  6076.                       return(success = 0);
  6077.                   }
  6078.                   break;
  6079.               }
  6080.               case XYFX_D:              /* Discard */
  6081.                 ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  6082.                 ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
  6083.                 tlog(F100," refused: name","",0);
  6084.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  6085.                 goto xclget;
  6086.  
  6087.               case XYFX_X:              /* Overwrite */
  6088.               case XYFX_U:              /* Update (already handled above) */
  6089.           case XYFX_M:        /* ditto */
  6090.                 break;
  6091.             }
  6092.         }
  6093.         rc = getfile(s,                 /* Remote name */
  6094.                      s2,                /* Local name */
  6095.                      0,                 /* Recover/Restart */
  6096.                      append,            /* Append */
  6097.                      NULL,              /* Pipename */
  6098.                      0,                 /* Translate charsets */
  6099.                      -1,                /* File charset (none) */
  6100.                      -1                 /* Server charset (none) */
  6101.                      );
  6102.         debug(F111,"ftp cmdlinget rc",s,rc);
  6103.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  6104.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  6105.  
  6106.         if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
  6107.             rc = getfile(&s[1],         /* Remote name without leading '/' */
  6108.                          s2,            /* Local name */
  6109.                          0,             /* Recover/Restart */
  6110.                          append,        /* Append */
  6111.                          NULL,          /* Pipename */
  6112.                          0,             /* Translate charsets */
  6113.                          -1,            /* File charset (none) */
  6114.                          -1             /* Server charset (none) */
  6115.                          );
  6116.         if (rc > -1) {
  6117.             good++;
  6118.             status = 1;
  6119.         }
  6120.         if (cancelfile)
  6121.           goto xclget;
  6122.         if (rc < 0) {
  6123.             ftp_fai++;
  6124.             if (geterror) {
  6125.                 status = 0;
  6126.                 done++;
  6127.             }
  6128.         }
  6129.     }
  6130.     if (ftp_deb && !done)
  6131.       printf("DOING MGET...\n");
  6132.     while (!done && !cancelgroup) {
  6133.         cancelfile = 0;                 /* This file not canceled yet */
  6134.         s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  6135.         if (!s) s = "";
  6136.         if (!*s) {
  6137.             first = 1;
  6138.             mgetx++;
  6139.             if (mgetx < nfils)
  6140.               s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
  6141.             else
  6142.               s = NULL;
  6143.             debug(F111,"ftp cmdlinget remote_files B",s,0);
  6144.             if (!s) {
  6145.                 done = 1;
  6146.                 break;
  6147.             }
  6148.         }
  6149.         /*
  6150.           The semantics of NLST are ill-defined.  Suppose we have just sent
  6151.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  6152.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  6153.           RETR commands based on the pathless names are not going to work.
  6154.         */
  6155.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  6156.             if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
  6157.                 int len, left = 4096;
  6158.                 char * tmp = xtmpbuf;
  6159.                 len = s3 - cmlist[mgetx] + 1;
  6160.                 ckstrncpy(tmp,cmlist[mgetx],left);
  6161.                 tmp += len;
  6162.                 left -= len;
  6163.                 ckstrncpy(tmp,s,left);
  6164.                 s = xtmpbuf;
  6165.         debug(F111,"ftp cmdlinget remote_files X",s,0);
  6166.             }
  6167.         }
  6168.         first = 0;                      /* Not first any more */
  6169.  
  6170.     debug(F111,"ftp cmdlinget havetype",s,havetype);
  6171.     if (havetype > 0 && havetype != FTYP_FILE) { /* Server says not file */
  6172.         debug(F110,"ftp cmdlinget not-a-file",s,0);
  6173.         continue;
  6174.     }
  6175.         rc = 0;                         /* Initial return code */
  6176.     if (havesize > (CK_OFF_T)-1) {    /* Already have file size? */
  6177.         fsize = havesize;
  6178.     } else {            /* No - must ask server */
  6179.         /*
  6180.           Prior to sending the NLST command we necessarily put the
  6181.           server into ASCII mode.  We must now put it back into the
  6182.           the requested mode so the upcoming SIZE command returns
  6183.           right kind of size; this is especially important for
  6184.           GET /RECOVER; otherwise the server returns the "ASCII" size
  6185.           of the file, rather than its true size.
  6186.         */
  6187.         changetype(ftp_typ,0);    /* Change to requested type */
  6188.         fsize = (CK_OFF_T)-1;
  6189.         if (sizeok) {
  6190.         x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm);
  6191.         if (x == REPLY_COMPLETE)
  6192.           fsize = ckatofs(&ftp_reply_str[4]);
  6193.         }
  6194.     }
  6195.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  6196.         debug(F111,"ftp cmdlinget filnam",filnam,fsize);
  6197.  
  6198.         nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
  6199.         s2 = tmpbuf;
  6200.  
  6201.         /* If local file already exists, take collision action */
  6202.  
  6203.         if (zchki(s2) > -1) {
  6204.             switch (ftp_fnc) {
  6205.               case XYFX_A:              /* Append */
  6206.                 append = 1;
  6207.                 break;
  6208.               case XYFX_R:              /* Rename */
  6209.               case XYFX_B: {            /* Backup */
  6210.                   char * p = NULL;
  6211.                   int x = -1;
  6212.                   znewn(s2,&p);         /* Make unique name */
  6213.                   debug(F110,"ftp cmdlinget znewn",p,0);
  6214.                   if (ftp_fnc == XYFX_B) { /* Backup existing file */
  6215.                       x = zrename(s2,p);
  6216.                       debug(F111,"ftp cmdlinget backup zrename",p,x);
  6217.                   } else {              /* Rename incoming file */
  6218.                       x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
  6219.                       s2 = tmpbuf;
  6220.                       debug(F111,"ftp cmdlinget rename incoming",p,x);
  6221.                   }
  6222.                   if (x < 0) {
  6223.                       printf("?Backup/Rename failed\n");
  6224.                       return(success = 0);
  6225.                   }
  6226.                   break;
  6227.               }
  6228.               case XYFX_D:      /* Discard */
  6229.                 ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  6230.                 ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
  6231.                 tlog(F100," refused: name","",0);
  6232.                 debug(F110,"ftp cmdlinget skip name",s2,0);
  6233.                 continue;
  6234.               case XYFX_X:              /* Overwrite */
  6235.               case XYFX_U:              /* Update (already handled above) */
  6236.               case XYFX_M:              /* ditto */
  6237.                 break;
  6238.             }
  6239.         }
  6240.                                         /* ^^^ ADD CHARSET STUFF HERE ^^^ */
  6241.         rc = getfile(s,                 /* Remote name */
  6242.                      s2,                /* Local name */
  6243.                      0,                 /* Recover/Restart */
  6244.                      append,            /* Append */
  6245.                      NULL,              /* Pipename */
  6246.                      0,                 /* Translate charsets */
  6247.                      -1,                /* File charset (none) */
  6248.                      -1                 /* Server charset (none) */
  6249.                      );
  6250.         debug(F111,"ftp cmdlinget rc",s,rc);
  6251.         debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
  6252.         debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
  6253.  
  6254.         if (rc > -1) {
  6255.             good++;
  6256.             status = 1;
  6257.         }
  6258.         if (cancelfile)
  6259.           continue;
  6260.         if (rc < 0) {
  6261.             ftp_fai++;
  6262.             if (geterror) {
  6263.                 status = 0;
  6264.                 done++;
  6265.             }
  6266.         }
  6267.     }
  6268.  
  6269.   xclget:
  6270.     if (cancelgroup)
  6271.       mlsreset();
  6272.     if (status > 0) {
  6273.         if (cancelgroup)
  6274.           status = 0;
  6275.         else if (cancelfile && good < 1)
  6276.           status = 0;
  6277.     }
  6278.     success = status;
  6279.  
  6280. #ifdef GFTIMER
  6281.     t1 = gmstimer();                    /* End time */
  6282.     sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  6283.     if (!sec) sec = 0.001;
  6284.     fptsecs = sec;
  6285. #else
  6286.     sec = (t1 - t0) / 1000;
  6287.     if (!sec) sec = 1;
  6288. #endif /* GFTIMER */
  6289.  
  6290.     tfcps = (long) (tfc / sec);
  6291.     tsecs = (int)sec;
  6292.     lastxfer = W_FTP|W_RECV;
  6293.     xferstat = success;
  6294.     if (dpyactive)
  6295.       ftscreen(SCR_TC,0,(CK_OFF_T)0,"");
  6296.     if (!stay)
  6297.       doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
  6298.     return(success);
  6299. }
  6300.  
  6301. /*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
  6302.  
  6303. /*
  6304.   Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  6305.   zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  6306.   the user-interface perspective, since the server's directory listings show
  6307.   its own local times and since we don't know what timezone it's in, there's
  6308.   no way to reconcile our local times with the server's.
  6309. */
  6310. int
  6311. doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  6312.     struct FDB fl, sw, cm;
  6313.     int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
  6314.     int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
  6315.     int x_fnc = 0, first = 0, skipthis = 0, append = 0, selected = 0;
  6316.     int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
  6317.     int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
  6318.     int gotsize = 0;
  6319.     int matchdot = 0;
  6320.     CK_OFF_T getlarger = (CK_OFF_T)-1, getsmaller = (CK_OFF_T)-1;
  6321.     char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
  6322.     char * src = "", * local = "";
  6323.     char * pat = "";
  6324.  
  6325.     int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
  6326.     int x_xla = 0;
  6327.     char c;                             /* Worker char */
  6328.     ULONG t0 = 0L, t1;                  /* Times for stats */
  6329. #ifdef GFTIMER
  6330.     CKFLOAT sec;
  6331. #else
  6332.     int sec = 0;
  6333. #endif /* GFTIMER */
  6334.  
  6335.     struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
  6336.  
  6337.     success = 0;                        /* Assume failure */
  6338.     forcetype = 0;                      /* No /TEXT or /BINARY given yet */
  6339.     restart = 0;                        /* No restart yet */
  6340.     out2screen = 0;            /* No TO-SCREEN switch given yet */
  6341.     mgetmethod = 0;            /* No NLST or MLSD switch yet */
  6342.     mgetforced = 0;
  6343.  
  6344.     g_displa = fdispla;
  6345.     if (ftp_dis > -1)
  6346.       fdispla = ftp_dis;
  6347.  
  6348.     x_cnv = ftp_cnv;                    /* Filename conversion */
  6349.     if (x_cnv == CNV_AUTO) {        /* Name conversion is auto */
  6350.         if (alike) {                    /* If server & client are alike */
  6351.             x_cnv = 0;            /* no conversion */
  6352.         } else {                        /* If they are different */
  6353.             if (servertype == SYS_UNIX || servertype == SYS_WIN32)
  6354.               x_cnv = -1;        /* only minimal conversions needed */
  6355.             else                        /* otherwise */
  6356.               x_cnv = 1;        /* full conversion */
  6357.         }
  6358.     } else                              /* Not auto - do what user said */
  6359.       x_cnv = ftp_cnv;
  6360.  
  6361.     x_prm = ftp_prm;                    /* Permissions */
  6362.     if (x_prm == SET_AUTO)              /* Permissions AUTO */
  6363.       x_prm = alike;
  6364.  
  6365. #ifndef NOCSETS
  6366.     x_csr = ftp_csr;                    /* Inherit global server charset */
  6367.     x_csl = ftp_csl;                    /* Inherit global local charset */
  6368.     if (x_csl < 0)                      /* If none, use current */
  6369.       x_csl = fcharset;                 /* file character-set. */
  6370.     x_xla = ftp_xla;                    /* Translation On/Off */
  6371. #endif /* NOCSETS */
  6372.  
  6373.     geterror = ftp_err;                 /* Inherit global error action. */
  6374.     asnambuf[0] = NUL;                  /* No as-name yet. */
  6375.     pipesave = pipesend;
  6376.     pipesend = 0;
  6377.  
  6378.     havetype = 0;
  6379.     havesize = (CK_OFF_T)-1;
  6380.     makestr(&havemdtm,NULL);
  6381.  
  6382.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  6383.         ftp_typ = g_ftp_typ;
  6384.         /* g_ftp_typ = -1; */
  6385.     }
  6386.     for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
  6387.         pv[i].sval = NULL;              /* to null pointers */
  6388.         pv[i].ival = -1;                /* and -1 int values */
  6389.     }
  6390.     zclose(ZMFILE);                     /* In case it was left open */
  6391.  
  6392.     x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
  6393.  
  6394.     if (fp_nml) {                       /* Reset /NAMELIST */
  6395.         if (fp_nml != stdout)
  6396.           fclose(fp_nml);
  6397.         fp_nml = NULL;
  6398.     }
  6399.     makestr(&ftp_nml,NULL);
  6400.  
  6401.     /* Initialize list of remote filespecs */
  6402.  
  6403.     if (!mgetlist) {
  6404.         mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
  6405.         if (!mgetlist) {
  6406.             printf("?Memory allocation failure - MGET list\n");
  6407.             return(-9);
  6408.         }
  6409.         for (i = 0; i < MGETMAX; i++)
  6410.           mgetlist[i] = NULL;
  6411.     }
  6412.     mgetn = 0;                          /* Number of mget arguments */
  6413.     mgetx = 0;                          /* Current arg */
  6414.  
  6415.     if (who == 0) {                     /* Called with unprefixed command */
  6416.         if (cx == XXGET || cx == XXREGET || cx == XXRETR)
  6417.           getone++;
  6418.         switch (cx) {
  6419.           case XXREGET: pv[SND_RES].ival = 1; break;
  6420.           case XXRETR:  pv[SND_DEL].ival = 1; break;
  6421.           case XXGET:
  6422.           case XXMGET:  mget++; break;
  6423.         }
  6424.     } else {                            /* FTP command */
  6425.         if (cx == FTP_GET || cx == FTP_RGE)
  6426.           getone++;
  6427.         switch (cx) {
  6428.           case FTP_DEL:                 /* (fall thru on purpose) */
  6429.           case FTP_MDE: mdel++;         /* (ditto) */
  6430.           case FTP_GET:                 /* (ditto) */
  6431.           case FTP_MGE: mget++; break;
  6432.           case FTP_RGE: pv[SND_RES].ival = 1; break;
  6433.         }
  6434.     }
  6435.     cmfdbi(&sw,                         /* First FDB - command switches */
  6436.            _CMKEY,                      /* fcode */
  6437.            "Remote filename;\n or switch", /* hlpmsg */
  6438.            "",                          /* default */
  6439.            "",                          /* addtl string data */
  6440.            mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
  6441.            4,                           /* addtl numeric data 2: 4 = cmswi */
  6442.            xxstring,                    /* Processing function */
  6443.            mdel ? delswi : getswi,      /* Keyword table */
  6444.            &fl                          /* Pointer to next FDB */
  6445.            );
  6446.     cmfdbi(&fl,                         /* 2nd FDB - remote filename */
  6447.            _CMFLD,                      /* fcode */
  6448.            "",                          /* hlpmsg */
  6449.            "",                          /* default */
  6450.            "",                          /* addtl string data */
  6451.            0,                           /* addtl numeric data 1 */
  6452.            0,                           /* addtl numeric data 2 */
  6453.            xxstring,
  6454.            NULL,
  6455.            &cm
  6456.            );
  6457.     cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
  6458.            _CMCFM,                      /* fcode */
  6459.            "",                          /* hlpmsg */
  6460.            "",                          /* default */
  6461.            "",                          /* addtl string data */
  6462.            0,                           /* addtl numeric data 1 */
  6463.            0,                           /* addtl numeric data 2 */
  6464.            NULL,
  6465.            NULL,
  6466.            NULL
  6467.            );
  6468.  
  6469.     while (1) {                         /* Parse 0 or more switches */
  6470.         x = cmfdb(&sw);                 /* Parse something */
  6471.         debug(F101,"ftp get cmfdb","",x);
  6472.         if (x < 0)                      /* Error */
  6473.           goto xgetx;                   /* or reparse needed */
  6474.         if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
  6475.           break;
  6476.         c = cmgbrk();                   /* Get break character */
  6477.         getval = (c == ':' || c == '='); /* to see how they ended the switch */
  6478.         if (getval && !(cmresult.kflags & CM_ARG)) {
  6479.             printf("?This switch does not take arguments\n");
  6480.             x = -9;
  6481.             goto xgetx;
  6482.         }
  6483.         n = cmresult.nresult;           /* Numeric result = switch value */
  6484.         debug(F101,"ftp get switch","",n);
  6485.  
  6486.         if (!getval && (cmgkwflgs() & CM_ARG)) {
  6487.             printf("?This switch requires an argument\n");
  6488.             x = -9;
  6489.             goto xgetx;
  6490.         }
  6491.         switch (n) {                    /* Process the switch */
  6492.           case SND_ASN:                 /* /AS-NAME: */
  6493.             debug(F101,"ftp get /as-name getval","",getval);
  6494.             if (!getval) break;
  6495.             if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
  6496.                 if (x == -3) {
  6497.                     printf("?name required\n");
  6498.                     x = -9;
  6499.                 }
  6500.                 goto xgetx;
  6501.             }
  6502.             s = brstrip(s);
  6503.             if (!*s) s = NULL;
  6504.             makestr(&(pv[n].sval),s);
  6505.             pv[n].ival = 1;
  6506.             break;
  6507.  
  6508.           case SND_BIN:                 /* /BINARY */
  6509.           case SND_TXT:                 /* /TEXT or /ASCII */
  6510.           case SND_TEN:                 /* /TENEX */
  6511.             pv[SND_BIN].ival = 0;
  6512.             pv[SND_TXT].ival = 0;
  6513.             pv[SND_TEN].ival = 0;
  6514.             pv[n].ival = 1;
  6515.             break;
  6516.  
  6517. #ifdef PUTPIPE
  6518.           case SND_CMD:                 /* These take no args */
  6519.             if (nopush) {
  6520.                 printf("?Sorry, system command access is disabled\n");
  6521.                 x = -9;
  6522.                 goto xgetx;
  6523.             }
  6524. #ifdef PIPESEND
  6525.             else if (rcvfilter) {
  6526.                 printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
  6527.                 x = -9;
  6528.                 goto xgetx;
  6529.             }
  6530. #endif /* PIPESEND */
  6531.             sw.hlpmsg = "Command, or switch"; /* Change help message */
  6532.             pv[n].ival = 1;             /* Just set the flag */
  6533.             pv[SND_ARR].ival = 0;
  6534.             break;
  6535. #endif /* PUTPIPE */
  6536.  
  6537.           case SND_SHH:                 /* /QUIET */
  6538.           case SND_RES:                 /* /RECOVER (reget) */
  6539.           case SND_NOB:                 /* /NOBACKUPFILES */
  6540.           case SND_DEL:                 /* /DELETE */
  6541.           case SND_UPD:                 /* /UPDATE */
  6542.           case SND_USN:                 /* /UNIQUE */
  6543.           case SND_NOD:                 /* /NODOTFILES */
  6544.           case SND_REC:                 /* /RECOVER */
  6545.           case SND_MAI:                 /* /TO-SCREEN */
  6546.             pv[n].ival = 1;             /* Just set the flag */
  6547.             break;
  6548.  
  6549.           case SND_DIF:                 /* /DATES-DIFFER */
  6550.         pv[SND_COL].ival = XYFX_M;    /* Now it's a collision option */
  6551.         pv[n].ival = 1;
  6552.         break;
  6553.  
  6554.           case SND_COL:                 /* /COLLISION: */
  6555.             if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
  6556.               goto xgetx;
  6557.         if (x == XYFX_M)
  6558.           pv[SND_DIF].ival = 1;    /* (phase this out) */
  6559.         pv[n].ival = x;        /* this should be sufficient */
  6560.             break;
  6561.  
  6562.           case SND_ERR:                 /* /ERROR-ACTION */
  6563.             if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
  6564.               goto xgetx;
  6565.             pv[n].ival = x;
  6566.             break;
  6567.  
  6568.           case SND_EXC:                 /* Exception list */
  6569.             if (!getval) break;
  6570.             if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
  6571.                 if (x == -3) {
  6572.                     printf("?Pattern required\n");
  6573.                     x = -9;
  6574.                 }
  6575.                 goto xgetx;
  6576.             }
  6577.             if (s) if (!*s) s = NULL;
  6578.             makestr(&(pv[n].sval),s);
  6579.             if (pv[n].sval)
  6580.               pv[n].ival = 1;
  6581.             break;
  6582.  
  6583. #ifdef PIPESEND
  6584.           case SND_FLT:
  6585.             debug(F101,"ftp get /filter getval","",getval);
  6586.             if (!getval) break;
  6587.             if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
  6588.                 if (x == -3)
  6589.                   s = "";
  6590.                 else
  6591.                   goto xgetx;
  6592.             }
  6593.             s = brstrip(s);
  6594.             if (pv[SND_MAI].ival < 1) {
  6595.                 y = strlen(s);
  6596.                 /* Make sure they included "\v(...)" */
  6597.                 for (x = 0; x < y; x++) {
  6598.                     if (s[x] != '\\') continue;
  6599.                     if (s[x+1] == 'v') break;
  6600.                 }
  6601.                 if (x == y) {
  6602.                     printf(
  6603.                 "?Filter must contain a replacement variable for filename.\n"
  6604.                            );
  6605.                     x = -9;
  6606.                     goto xgetx;
  6607.                 }
  6608.             }
  6609.             if (*s) {
  6610.                 pv[n].ival = 1;
  6611.                 makestr(&(pv[n].sval),s);
  6612.             } else {
  6613.                 pv[n].ival = 0;
  6614.                 makestr(&(pv[n].sval),NULL);
  6615.             }
  6616.             break;
  6617. #endif /* PIPESEND */
  6618.  
  6619.           case SND_NAM:
  6620.             if (!getval) break;
  6621.             if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
  6622.               goto xgetx;
  6623.             debug(F101,"ftp get /filenames","",x);
  6624.             pv[n].ival = x;
  6625.             break;
  6626.  
  6627.           case SND_SMA:                 /* Smaller / larger than */
  6628.           case SND_LAR: {
  6629.           CK_OFF_T y;
  6630.           if (!getval) break;
  6631.           if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
  6632.         goto xgetx;
  6633.           pv[n].wval = y;
  6634.           break;
  6635.       }
  6636.           case SND_FIL:                 /* Name of file containing filnames */
  6637.             if (!getval) break;
  6638.             if ((x = cmifi("Name of file containing list of filenames",
  6639.                                "",&s,&y,xxstring)) < 0) {
  6640.                 if (x == -3) {
  6641.                     printf("?Filename required\n");
  6642.                     x = -9;
  6643.                 }
  6644.                 goto xgetx;
  6645.             } else if (y && iswild(s)) {
  6646.                 printf("?Wildcards not allowed BBB\n");
  6647.                 x = -9;
  6648.                 goto xgetx;
  6649.             }
  6650.             if (s) if (!*s) s = NULL;
  6651.             makestr(&(pv[n].sval),s);
  6652.             if (pv[n].sval)
  6653.               pv[n].ival = 1;
  6654.             break;
  6655.  
  6656.           case SND_MOV:                 /* MOVE after */
  6657.           case SND_REN:                 /* RENAME after */
  6658.           case SND_SRN: {               /* SERVER-RENAME */
  6659.               char * m = "";
  6660.               switch (n) {
  6661.                 case SND_MOV:
  6662.                   m =
  6663.                    "Device and/or directory for incoming file after reception";
  6664.                   break;
  6665.                 case SND_REN:
  6666.                   m = "New name for incoming file after reception";
  6667.                   break;
  6668.                 case SND_SRN:
  6669.                   m = "New name for source file on server after reception";
  6670.                   break;
  6671.               }
  6672.               if (!getval) break;
  6673.               if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
  6674.                   if (x == -3) {
  6675.                       printf("%s\n", n == SND_MOV ?
  6676.                              "?Destination required" :
  6677.                              "?New name required"
  6678.                              );
  6679.                       x = -9;
  6680.                   }
  6681.                   goto xgetx;
  6682.               }
  6683.               makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6684.               pv[n].ival = (pv[n].sval) ? 1 : 0;
  6685.               break;
  6686.           }
  6687. #ifndef NOCSETS
  6688.           case SND_CSL:                 /* Local character set */
  6689.           case SND_CSR:                 /* Remote (server) charset */
  6690.             if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
  6691.               return((x == -3) ? -2 : x);
  6692.             if (n == SND_CSL)
  6693.               x_csl = x;
  6694.             else
  6695.               x_csr = x;
  6696.             x_xla = 1;                  /* Overrides global OFF setting */
  6697.             break;
  6698.  
  6699.           case SND_XPA:                 /* Transparent */
  6700.             x_xla =  0;
  6701.             x_csr = -1;
  6702.             x_csl = -1;
  6703.             break;
  6704. #endif /* NOCSETS */
  6705.  
  6706.           case SND_NML:
  6707.             if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
  6708.               goto xgetx;
  6709.             makestr(&ftp_nml,s);
  6710.             break;
  6711.  
  6712.       case SND_PAT:            /* /PATTERN: */
  6713.         if (!getval) break;
  6714.         if ((x = cmfld("Pattern","*", &s, xxstring)) < 0)
  6715.           goto xgetx;
  6716.         makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
  6717.         pv[n].ival = (pv[n].sval) ? 1 : 0;
  6718.         break;
  6719.  
  6720.       case SND_NLS:            /* /NLST */
  6721.             pv[n].ival = 1;        /* Use NLST */
  6722.         pv[SND_MLS].ival = 0;    /* Don't use MLSD */
  6723.         break;
  6724.  
  6725.       case SND_MLS:            /* /MLSD */
  6726.             pv[n].ival = 1;        /* Use MLSD */
  6727.         pv[SND_NLS].ival = 0;    /* Don't use NLST */
  6728.         break;
  6729.  
  6730.           default:                      /* /AFTER, /PERMISSIONS, etc... */
  6731.             printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
  6732.             x = -9;
  6733.             goto xgetx;
  6734.         }
  6735.     }
  6736.     line[0] = NUL;
  6737.     cmarg = line;
  6738.     cmarg2 = asnambuf;
  6739.     s = line;
  6740. /*
  6741.   For GET, we want to parse an optional as-name, like with PUT.
  6742.   For MGET, we must parse a list of names, and then send NLST or MLSD
  6743.   commands for each name separately.
  6744. */
  6745.     switch (cmresult.fcode) {           /* How did we get out of switch loop */
  6746.       case _CMFLD:                      /* Field */
  6747.         if (!getone) {
  6748.             s = brstrip(cmresult.sresult);
  6749.             makestr(&(mgetlist[mgetn++]),s);
  6750.             while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
  6751.                 if (x < 0)
  6752.                   goto xgetx;
  6753.                 makestr(&(mgetlist[mgetn++]),brstrip(s));
  6754.                 if (mgetn >= MGETMAX) {
  6755.                     printf("?Too many items in MGET list\n");
  6756.                     goto xgetx;
  6757.                 }
  6758.             }
  6759.             if ((x = cmcfm()) < 0)
  6760.               goto xgetx;
  6761.         } else {
  6762.             s = brstrip(cmresult.sresult);
  6763.             ckstrncpy(line,s,LINBUFSIZ);
  6764.             if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
  6765.               if (x != -3)
  6766.                 goto xgetx;
  6767.             s = brstrip(s);
  6768.             ckstrncpy(asnambuf,s,CKMAXPATH+1);
  6769.             if ((x = cmcfm()) < 0)
  6770.               goto xgetx;
  6771.         }
  6772.         break;
  6773.       case _CMCFM:                      /* Confirmation */
  6774.         break;
  6775.       default:
  6776.         printf("?Unexpected function code: %d\n",cmresult.fcode);
  6777.         x = -9;
  6778.         goto xgetx;
  6779.     }
  6780.     if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
  6781.       recursive = 2;
  6782.  
  6783.     if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
  6784.         forcetype = 1;                  /* So skip the name-pattern match */
  6785.         ftp_typ = XYFT_B;               /* Set binary */
  6786.     } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
  6787.         forcetype = 1;
  6788.         ftp_typ = XYFT_T;
  6789.     } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
  6790.         forcetype = 1;
  6791.         ftp_typ = FTT_TEN;
  6792.     } else if (ftp_cmdlin && xfermode == XMODE_M) {
  6793.         forcetype = 1;
  6794.         ftp_typ = binary;
  6795.         g_ftp_typ = binary;
  6796.     }
  6797.     if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
  6798.         char * p;
  6799.         p = brstrip(pv[SND_ASN].sval);  /* As-name */
  6800.         ckstrncpy(asnambuf,p,CKMAXPATH+1);
  6801.     }
  6802.     debug(F110,"ftp get asnambuf",asnambuf,0);
  6803.  
  6804. #ifdef PIPESEND
  6805.     if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
  6806.         char * p;
  6807.         p = asnambuf;
  6808.         debug(F110,"GET /COMMAND before stripping",p,0);
  6809.         p = brstrip(p);
  6810.         debug(F110,"GET /COMMAND after stripping",p,0);
  6811.         if (!*p) {
  6812.             printf("?Sorry, a command to write to is required\n");
  6813.             x = -9;
  6814.             goto xgetx;
  6815.         }
  6816.         pipename = p;
  6817.         pipesend = 1;
  6818.     }
  6819. #endif /* PIPESEND */
  6820.  
  6821. /* Set up /MOVE and /RENAME */
  6822.  
  6823.     if (pv[SND_DEL].ival > 0 &&
  6824.         (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
  6825.         printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
  6826.         x = -9;
  6827.         goto xgetx;
  6828.     }
  6829. #ifdef CK_TMPDIR
  6830.     if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
  6831.         int len;
  6832.         char * p = pv[SND_MOV].sval;
  6833.         len = strlen(p);
  6834.         if (!isdir(p)) {                /* Check directory */
  6835. #ifdef CK_MKDIR
  6836.             char * s = NULL;
  6837.             s = (char *)malloc(len + 4);
  6838.             if (s) {
  6839.                 strcpy(s,p);            /* safe */
  6840. #ifdef datageneral
  6841.                 if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
  6842. #else
  6843.                 if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
  6844. #endif /* datageneral */
  6845.                 s[len++] = 'X';
  6846.                 s[len] = NUL;
  6847. #ifdef NOMKDIR
  6848.                 x = -1;
  6849. #else
  6850.                 x = zmkdir(s);
  6851. #endif /* NOMKDIR */
  6852.                 free(s);
  6853.                 if (x < 0) {
  6854.                     printf("?Can't create \"%s\"\n",p);
  6855.                     x = -9;
  6856.                     goto xgetx;
  6857.                 }
  6858.             }
  6859. #else
  6860.             printf("?Directory \"%s\" not found\n",p);
  6861.             x = -9;
  6862.             goto xgetx;
  6863. #endif /* CK_MKDIR */
  6864.         }
  6865.         makestr(&rcv_move,p);
  6866.         moving = 1;
  6867.     }
  6868. #endif /* CK_TMPDIR */
  6869.  
  6870.     if (pv[SND_REN].ival > 0) {         /* /RENAME */
  6871.         char * p = pv[SND_REN].sval;
  6872.         if (!p) p = "";
  6873.         if (!*p) {
  6874.             printf("?New name required for /RENAME\n");
  6875.             x = -9;
  6876.             goto xgetx;
  6877.         }
  6878.         p = brstrip(p);
  6879. #ifndef NOSPL
  6880.     /* If name given is wild, rename string must contain variables */
  6881.         if (mget && !getone) {
  6882.             char * s = tmpbuf;
  6883.             x = TMPBUFSIZ;
  6884.             zzstring(p,&s,&x);
  6885.             if (!strcmp(tmpbuf,p)) {
  6886.                 printf(
  6887.     "?/RENAME for file group must contain variables such as \\v(filename)\n"
  6888.                        );
  6889.                 x = -9;
  6890.                 goto xgetx;
  6891.             }
  6892.         }
  6893. #endif /* NOSPL */
  6894.         renaming = 1;
  6895.         makestr(&rcv_rename,p);
  6896.         debug(F110,"FTP rcv_rename",rcv_rename,0);
  6897.     }
  6898.     if (!cmarg[0] && mgetn == 0 && getone && pv[SND_FIL].ival < 1) {
  6899.         printf("?Filename required but not given\n");
  6900.         x = -9;
  6901.         goto xgetx;
  6902.     } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
  6903.         printf("?You can't give both /LISTFILE and a remote filename\n");
  6904.         x = -9;
  6905.         goto xgetx;
  6906.     }
  6907.     CHECKCONN();                        /* Check connection */
  6908.  
  6909.     if (pv[SND_COL].ival > -1)
  6910.       x_fnc = pv[SND_COL].ival;
  6911.  
  6912. #ifndef NOSPL
  6913.     /* If as-name given for MGET, as-name must contain variables */
  6914.     if (mget && !getone && asnambuf[0] && x_fnc != XYFX_A) {
  6915.         char * s = tmpbuf;
  6916.         x = TMPBUFSIZ;
  6917.         zzstring(asnambuf,&s,&x);
  6918.         if (!strcmp(tmpbuf,asnambuf)) {
  6919.             printf(
  6920.     "?As-name for MGET must contain variables such as \\v(filename)\n"
  6921.                    );
  6922.             x = -9;
  6923.             goto xgetx;
  6924.         }
  6925.     }
  6926. #endif /* NOSPL */
  6927.  
  6928. /* doget: */
  6929.  
  6930.     if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
  6931.         fdispla = 0;
  6932.     } else {
  6933.         displa = 1;
  6934.         if (mdel || ftp_deb)
  6935.       fdispla = XYFD_B;
  6936.     }
  6937.     deleting = 0;
  6938.     if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
  6939.       deleting = 1;
  6940.     if (pv[SND_EXC].ival > 0)
  6941.       makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
  6942.     if (pv[SND_SMA].wval > -1)
  6943.       getsmaller = pv[SND_SMA].wval;
  6944.     if (pv[SND_LAR].wval > -1)
  6945.       getlarger = pv[SND_LAR].wval;
  6946.     if (pv[SND_NAM].ival > -1)
  6947.       x_cnv = pv[SND_NAM].ival;
  6948.     if (pv[SND_ERR].ival > -1)
  6949.       geterror = pv[SND_ERR].ival;
  6950.     if (pv[SND_MAI].ival > -1)
  6951.       toscreen = 1;
  6952.  
  6953.     if (pv[SND_NLS].ival > 0) {        /* Force NLST or MLSD? */
  6954.     mgetmethod = SND_NLS;
  6955.     mgetforced = 1;
  6956.     } else if (pv[SND_MLS].ival > 0) {
  6957.     mgetmethod = SND_MLS;
  6958.     mgetforced = 1;
  6959.     }
  6960.  
  6961. #ifdef FTP_RESTART
  6962.     if (pv[SND_RES].ival > 0) {
  6963.         if (!ftp_typ) {
  6964.             printf("?Sorry, GET /RECOVER requires binary mode\n");
  6965.             x = -9;
  6966.             goto xgetx;
  6967. #ifdef COMMENT
  6968.         /* Not true - the fact that the initial REST fails does not mean */
  6969.         /* it will fail here.  */
  6970.         } else if (!okrestart) {
  6971.             printf("WARNING: Server might not support restart...\n");
  6972. #endif /* COMMENT */
  6973.         }
  6974.         restart = 1;
  6975.     }
  6976. #endif /* FTP_RESTART */
  6977.  
  6978. #ifdef PIPESEND
  6979.     if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
  6980.         if (pipesend) {
  6981.             printf("?Switch conflict: /FILTER and /COMMAND\n");
  6982.             x = -9;
  6983.             goto xgetx;
  6984.         }
  6985.         makestr(&rcvfilter,pv[SND_FLT].sval);
  6986.         debug(F110,"ftp get /FILTER", rcvfilter, 0);
  6987.     }
  6988.     if (rcvfilter || pipesend) {        /* /RESTART */
  6989. #ifdef FTP_RESTART
  6990.         if (restart) {                  /* with pipes or filters */
  6991.             printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
  6992.             x = -9;
  6993.             goto xgetx;
  6994.         }
  6995. #endif /* FTP_RESTART */
  6996.         if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_M || x_fnc == XYFX_U) {
  6997.             printf(
  6998.         "?Switch conflict: /FILTER or /COMMAND and Date Checking\n");
  6999.             x = -9;
  7000.             goto xgetx;
  7001.         }
  7002.     }
  7003. #endif /* PIPESEND */
  7004.  
  7005.     tfc = (CK_OFF_T)0;            /* Initialize stats and counters */
  7006.     filcnt = 0;
  7007.     pktnum = 0;
  7008.     rpackets = 0L;
  7009.  
  7010.     if (pv[SND_FIL].ival > 0) {
  7011.         if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
  7012.             debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
  7013.             printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
  7014.             x = -9;
  7015.             goto xgetx;
  7016.         }
  7017.         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
  7018.             zclose(ZMFILE);                       /* Failed */
  7019.             debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
  7020.             printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
  7021.             x = -9;
  7022.             goto xgetx;
  7023.         }
  7024.         listfile = 1;
  7025.         debug(F110,"ftp get listfile first",tmpbuf,0);
  7026.         makestr(&(mgetlist[0]),tmpbuf);
  7027.     }
  7028.     t0 = gmstimer();                    /* Record starting time */
  7029.  
  7030.     updating = 0;            /* Checking dates? */
  7031.     if (pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U))
  7032.       updating = 1;
  7033.     if (pv[SND_DIF].ival > 0 || x_fnc == XYFX_M)
  7034.       updating = 2;
  7035.     if (updating)            /* These switches force FTP DATES ON */
  7036.       ftp_dates |= 2;
  7037.  
  7038.     what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
  7039.  
  7040.     cancelgroup = 0;                    /* Group not canceled yet */
  7041.     if (!(xfermode == XMODE_A && patterns && get_auto && !forcetype))
  7042.       changetype(ftp_typ,0);        /* Change to requested type */
  7043.     binary = ftp_typ;                   /* For file-transfer display */
  7044.     first = 1;                          /* For MGET list */
  7045.     done = 0;                           /* Loop control */
  7046.  
  7047. #ifdef CK_TMPDIR
  7048.     if (dldir && !f_tmpdir) {           /* If they have a download directory */
  7049.         if ((s = zgtdir())) {           /* Get current directory */
  7050.             if (zchdir(dldir)) {        /* Change to download directory */
  7051.                 ckstrncpy(savdir,s,TMPDIRLEN);
  7052.                 f_tmpdir = 1;           /* Remember that we did this */
  7053.             }
  7054.         }
  7055.     }
  7056. #endif /* CK_TMPDIR */
  7057.  
  7058.     if (ftp_nml) {                      /* /NAMELIST */
  7059.         debug(F110,"ftp GET ftp_nml",ftp_nml,0);
  7060.         if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
  7061.           fp_nml = stdout;
  7062.         else
  7063.           fp_nml = fopen(ftp_nml, "wb");
  7064.         if (!fp_nml) {
  7065.             printf("?%s: %s\n",ftp_nml,ck_errstr());
  7066.             goto xgetx;
  7067.         }
  7068.     }
  7069.     while (!done && !cancelgroup) {     /* Loop for all files */
  7070.                                         /* or until canceled. */
  7071. #ifdef FTP_PROXY
  7072.         /* do something here if proxy */
  7073. #endif /* FTP_PROXY */
  7074.  
  7075.         rs_len = (CK_OFF_T)0;        /* REGET position */
  7076.         cancelfile = 0;                 /* This file not canceled yet */
  7077.         haspath = 0;                    /* Recalculate this each time thru */
  7078.  
  7079.         if (getone) {                   /* GET */
  7080.             char * p;
  7081.             s = line;
  7082.             src = line;                 /* Server name */
  7083.             done = 1;
  7084.             debug(F111,"ftp get file",s,0);
  7085.         } else if (mget) {              /* MGET */
  7086.             src = mgetlist[mgetx];
  7087.             debug(F111,"ftp mget remote_files A",src,first);
  7088.             s = (char *)remote_files(first,
  7089.                      (CHAR *)mgetlist[mgetx],
  7090.                      (CHAR *)pv[SND_PAT].sval,
  7091.                      0
  7092.                      );
  7093.             debug(F110,"ftp mget remote_files B",s,0);
  7094.             if (!s) s = "";
  7095.             if (!*s) {
  7096.                 first = 1;
  7097.                 if (listfile) {        /* Names from listfile */
  7098.                   again:
  7099.                     tmpbuf[0] = NUL;
  7100.                     while (!tmpbuf[0]) {
  7101.                         if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
  7102.                             zclose(ZMFILE);
  7103.                             debug(F110,"ftp get listfile EOF",
  7104.                                   pv[SND_FIL].sval,0);
  7105.                             makestr(&(mgetlist[0]),NULL);
  7106.                             s = NULL;
  7107.                             done = 1;
  7108.                             break;
  7109.                         }
  7110.                     }
  7111.                     if (done)
  7112.                       continue;
  7113.  
  7114.                     makestr(&(mgetlist[0]),tmpbuf);
  7115.             debug(F110,"ftp get listfile next",tmpbuf,0);
  7116.                     s = (char *)remote_files(first,
  7117.                          (CHAR *)mgetlist[0],
  7118.                          (CHAR *)pv[SND_PAT].sval,
  7119.                          0
  7120.                          );
  7121.             debug(F110,"ftp mget remote_files C",s,0);
  7122.                     if (!s) {
  7123.                         ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  7124.                         ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,"File not found");
  7125.                         tlog(F110,"ftp get file not found:",s,0);
  7126.                         goto again;
  7127.                     }
  7128.                 } else {        /* Names from command line */
  7129.                     mgetx++;
  7130.                     if (mgetx < mgetn)
  7131.               s = (char *)remote_files(first,
  7132.                            (CHAR *)mgetlist[mgetx],
  7133.                            (CHAR *)pv[SND_PAT].sval,
  7134.                            0
  7135.                            );
  7136.                     else
  7137.               s = NULL;
  7138.             if (!s) mgetx++;
  7139.             debug(F111,"ftp mget remote_files D",s,mgetx);
  7140.                 }
  7141.                 if (!s) {
  7142.             if (!first || mgetx >= mgetn) {
  7143.             done = 1;
  7144.             break;
  7145.             } else if (geterror) {
  7146.             status = 0;
  7147.             done = 1;
  7148.             break;
  7149.             } else {
  7150.             continue;
  7151.             }
  7152.                 }
  7153.             }
  7154.         }
  7155.     debug(F111,"ftp mget remote_files E",s,0);
  7156.         /*
  7157.           The semantics of NLST are ill-defined.  Suppose we have just sent
  7158.           NLST /path/[a-z]*.  Most servers send back names like /path/foo,
  7159.           /path/bar, etc.  But some send back only foo and bar, and subsequent
  7160.           RETR commands based on the pathless names are not going to work.
  7161.         */
  7162.         if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
  7163.             char * s3;
  7164.             if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
  7165.                 int len, left = 4096;
  7166.                 char * tmp = xtmpbuf;
  7167.                 len = s3 - mgetlist[mgetx] + 1;
  7168.                 ckstrncpy(tmp,mgetlist[mgetx],left);
  7169.                 tmp += len;
  7170.                 left -= len;
  7171.                 ckstrncpy(tmp,s,left);
  7172.                 s = xtmpbuf;
  7173.         debug(F111,"ftp mget remote_files F",s,0);
  7174.             }
  7175.         }
  7176.         first = 0;
  7177.         skipthis = 0;                   /* File selection... */
  7178.         msg = "";
  7179.         nam = s;                        /* Filename (without path) */
  7180.         rc = 0;                         /* Initial return code */
  7181.         s2 = "";
  7182.  
  7183.         if (!getone && !skipthis) {     /* For MGET and MDELETE... */
  7184.             char c, * p = s;
  7185.             int srvpath = 0;
  7186.             int usrpath = 0;
  7187.             int i, k = 0;
  7188.  
  7189.         debug(F111,"ftp mget havetype",s,havetype);
  7190.         if (havetype > 0 && havetype != FTYP_FILE) {
  7191.         /* Server says it's not file... */
  7192.         debug(F110,"ftp mget not-a-file",s,0);
  7193.         continue;
  7194.         }
  7195. /*
  7196.   Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  7197.   But if the client did not ask for a recursive list, we have to ignore any
  7198.   server files that include a pathname that extends beyond any path that
  7199.   was included in the user's request.
  7200.  
  7201.   User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  7202.   get the user's path segment.  Then, for each incoming file, if it begins
  7203.   with the same path segment, we must strip it (point past it).
  7204. */
  7205.             src = mgetlist[mgetx];      /* In case it moved! */
  7206.         if (src) {
  7207.         for (i = 0; src[i]; i++) { /* Find rightmost path separator */
  7208.             if (ispathsep(src[i])) /* in user's pathname */
  7209.               k = i + 1;
  7210.         }
  7211.         } else {
  7212.         src = "";
  7213.         }
  7214.             usrpath = k;                /* User path segment length */
  7215.             debug(F111,"ftp get usrpath",src,usrpath);
  7216.  
  7217.             p = s;                      /* Server filename */
  7218.             while ((c = *p++)) {        /* Look for path in server filename */
  7219.                 if (ispathsep(c)) {
  7220.             /* haspath++; */
  7221.                     nam = p;            /* Pathless name (for ckmatch) */
  7222.                     srvpath = p - s;    /* Server path segment length */
  7223.                 }
  7224.             }
  7225.             debug(F111,"ftp get srvpath",s,srvpath);
  7226.  
  7227.         if (usrpath == 0) {
  7228. /*
  7229.   Here we handle the case where the user said "mget foo" where foo is a
  7230.   directory name, and the server is sending back names like "foo/file1",
  7231.   "foo/file2", etc.  This is a nasty trick but it's necessary because the
  7232.   user can't compensate by typing "mget foo/" because then the server is
  7233.   likely to send back "foo//file1, foo//file2" etc, and we still won't
  7234.   get a match...
  7235. */
  7236.         int srclen = 0, srvlen = 0;
  7237.         if (src) srclen = strlen(src);
  7238.         if (s) srvlen = strlen(s);
  7239.         if (src && (srvlen > srclen)) {
  7240.             if (!strncmp(src,s,srclen) && ispathsep(s[srclen])) {
  7241.             char * tmpsrc = NULL;
  7242.             tmpsrc = (char *)malloc(srclen + 2);
  7243.             strncpy(tmpsrc,src,srclen);
  7244.             tmpsrc[srclen] = s[srclen];
  7245.             tmpsrc[srclen+1] = NUL;
  7246.             free(mgetlist[mgetx]);
  7247.             mgetlist[mgetx] = tmpsrc;
  7248.             tmpsrc = NULL;
  7249.             src = mgetlist[mgetx];
  7250.             usrpath = srclen+1;
  7251.             }                  
  7252.         }
  7253.         }
  7254. /*
  7255.   If as-name not given and server filename includes path that matches
  7256.   the pathname from the user's file specification, we must trim the common
  7257.   path prefix from the server's name when constructing the local name.
  7258. */
  7259.             if (src &&            /* Wed Sep 25 17:27:48 2002 */
  7260.         !asnambuf[0] &&
  7261.         !recursive &&        /* Thu Sep 19 16:11:59 2002 */
  7262.         (srvpath > 0) &&
  7263.         !strncmp(src,s,usrpath)) {
  7264.                 s2 = s + usrpath;       /* Local name skips past remote path */
  7265.             }
  7266. #ifdef COMMENT
  7267.         /* This doesn't work if the path prefix contains wildcards! */
  7268.         haspath = (srvpath > usrpath);
  7269. #else
  7270.         {                /* Count path segments instead */
  7271.         int x1 = 0, x2 = 0;
  7272.         char *p;
  7273.         for (p = s; *p; p++)
  7274.           if (ispathsep(*p)) x1++;
  7275.         for (p = src; *p; p++) {
  7276.             if (ispathsep(*p)) x2++;
  7277.         }
  7278.         haspath = recursive ? x1 || x2 : x1 > x2;
  7279.         debug(F111,"ftp get server path segments",s,x1);
  7280.         debug(F111,"ftp get user   path segments",src,x2);
  7281.         }
  7282.  
  7283. #endif /* COMMENT */
  7284.             debug(F111,"ftp get haspath",s+usrpath,haspath);
  7285.  
  7286.             if (haspath) {              /* Server file has path segments? */
  7287.                 if (!recursive) {       /* [M]GET /RECURSIVE? */
  7288. /*
  7289.   We did not ask for a recursive listing, but the server is sending us one
  7290.   anyway (as wu-ftpd is wont to do).  We get here if the current filename
  7291.   includes a path segment beyond any path segment we asked for in our
  7292.   non-recursive [M]GET command.  We MUST skip this file.
  7293. */
  7294.                     debug(F111,"ftp get skipping because of path",s,0);
  7295.                     continue;
  7296.                 }
  7297.             }
  7298.         } else if (getone && !skipthis) { /* GET (not MGET) */
  7299.             char * p = nam;
  7300.         while ((c = *p++)) {    /* Handle path in local name */
  7301.         if (ispathsep(c)) {
  7302.             if (recursive) {    /* If recursive, keep it */
  7303.             haspath = 1;
  7304.             break;
  7305.             } else {        /* Otherwise lose it. */
  7306.               nam = p;
  7307.             }
  7308.         }
  7309.             }
  7310.             s2 = nam;
  7311.         }
  7312.         if (!*nam)                      /* Name without path */
  7313.           nam = s;
  7314.  
  7315.         if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
  7316.             if (nam[0] == '.')
  7317.           continue;
  7318.         }
  7319.         if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
  7320.         int xx;
  7321.             for (i = 0; i < NSNDEXCEPT; i++) {
  7322.                 if (!rcvexcept[i]) {
  7323.                     break;
  7324.                 }
  7325.         xx = ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1);
  7326.         debug(F111,"ftp mget /except match",rcvexcept[i],xx);
  7327.                 if (xx) {
  7328.                     tlog(F100," refused: exception list","",0);
  7329.             msg = "Refused: Exception List";
  7330.                     skipthis++;
  7331.                     break;
  7332.                 }
  7333.             }
  7334.         }
  7335.         if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
  7336.             if (ckmatch(
  7337. #ifdef CKREGEX
  7338.                         "*.~[0-9]*~"
  7339. #else
  7340.                         "*.~*~"
  7341. #endif /* CKREGEX */
  7342.                         ,nam,0,1) > 0)
  7343.               continue;
  7344.         }
  7345.         if (!x_xla) {                   /* If translation is off */
  7346.             x_csl = -2;                 /* unset the charsets */
  7347.             x_csr = -2;
  7348.         }
  7349.         ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
  7350.         if (!*s2)                       /* Local name */
  7351.           s2 = asnambuf;                /* As-name */
  7352.  
  7353.     if (!*s2)            /* Sat Nov 16 19:19:39 2002 */
  7354.       s2 = recursive ? s : nam;    /* Fri Jan 10 13:15:19 2003 */
  7355.  
  7356.         debug(F110,"ftp get filnam  ",s,0);
  7357.         debug(F110,"ftp get asname A",s2,0);
  7358.  
  7359.         /* Receiving to real file */
  7360.         if (!pipesend &&
  7361. #ifdef PIPESEND
  7362.             !rcvfilter &&
  7363. #endif /* PIPESEND */
  7364.             !toscreen) {
  7365. #ifndef NOSPL
  7366.             /* Do this here so we can decide whether to skip */
  7367.             if (cmd_quoting && !skipthis && asnambuf[0]) {
  7368.                 int n; char *p;
  7369.                 n = TMPBUFSIZ;
  7370.                 p = tmpbuf;
  7371.                 zzstring(asnambuf,&p,&n);
  7372.                 s2 = tmpbuf;
  7373.                 debug(F111,"ftp get asname B",s2,updating);
  7374.             }
  7375. #endif /* NOSPL */
  7376.  
  7377.         local = *s2 ? s2 : s;
  7378.  
  7379.         if (!skipthis && x_fnc == XYFX_D) { /* File Collision = Discard */
  7380.         CK_OFF_T x;
  7381.         x = zchki(local);
  7382.         debug(F111,"ftp get DISCARD zchki",local,x);
  7383.         if (x > -1) {
  7384.             skipthis++;
  7385.             debug(F110,"ftp get skip name",local,0);
  7386.             tlog(F100," refused: name","",0);
  7387.             msg = "Refused: Name";
  7388.         }
  7389.         }
  7390.  
  7391. #ifdef DOUPDATE
  7392.             if (!skipthis && updating) { /* If updating and not yet skipping */
  7393.                 if (zchki(local) > -1) {
  7394.                     x = chkmodtime(local,s,0);
  7395. #ifdef DEBUG
  7396.             if (deblog) {
  7397.             if (updating == 2)
  7398.               debug(F111,"ftp get /dates-diff chkmodtime",local,x);
  7399.             else
  7400.               debug(F111,"ftp get /update chkmodtime",local,x);
  7401.             }
  7402. #endif /* DEBUG */
  7403.             if ((updating == 1 && x > 0) ||  /* /UPDATE */
  7404.             (updating == 2 && x == 1)) { /* /DATES-DIFFER */
  7405.             skipthis++;
  7406.             tlog(F100," refused: date","",0);
  7407.             msg = "Refused: Date";
  7408.                         debug(F110,"ftp get skip date",local,0);
  7409.                     }
  7410.                 }
  7411.             }
  7412. #endif /* DOUPDATE */
  7413.         }
  7414.         /* Initialize file size to -1 in case server doesn't understand */
  7415.         /* SIZE command, so xxscreen() will know we don't know the size */
  7416.  
  7417.         fsize = (CK_OFF_T)-1;
  7418.  
  7419.     /* Ask for size now only if we need it for selection */
  7420.     /* because if you're going thru a list 100,000 files to select */
  7421.     /* a small subset, 100,000 SIZE commands can take hours... */
  7422.  
  7423.     gotsize = 0;
  7424.         if (!mdel && !skipthis &&        /* Don't need size for DELE... */
  7425.         (getsmaller > (CK_OFF_T)-1 || getlarger > (CK_OFF_T)-1)) {
  7426.         if (havesize > (CK_OFF_T)-1) { /* Already have file size? */
  7427.         fsize = havesize;
  7428.         gotsize = 1;
  7429.         } else {            /* No - must ask server */
  7430.         /*
  7431.           Prior to sending the NLST command we necessarily put the
  7432.           server into ASCII mode.  We must now put it back into the
  7433.           the requested mode so the upcoming SIZE command returns
  7434.           right kind of size; this is especially important for
  7435.           GET /RECOVER; otherwise the server returns the "ASCII" size
  7436.           of the file, rather than its true size.
  7437.         */
  7438.         changetype(ftp_typ,0);    /* Change to requested type */
  7439.         fsize = (CK_OFF_T)-1;
  7440.         if (sizeok) {
  7441.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7442.             if (x == REPLY_COMPLETE) {
  7443.             fsize = ckatofs(&ftp_reply_str[4]);
  7444.             gotsize = 1;
  7445.             }
  7446.         }
  7447.         }
  7448.             if (gotsize) {
  7449.                 if (getsmaller > (CK_OFF_T)-1 && fsize >= getsmaller)
  7450.                   skipthis++;
  7451.                 if (getlarger > (CK_OFF_T)-1 && fsize <= getlarger)
  7452.                   skipthis++;
  7453.                 if (skipthis) {
  7454.                     debug(F111,"ftp get skip size",s,fsize);
  7455.                     tlog(F100," refused: size","",0);
  7456.                     msg = "Refused: Size";
  7457.                 }
  7458. #ifdef COMMENT
  7459.             } else if (getone) {
  7460.                 /* SIZE can fail for many reasons.  Does the file exist? */
  7461.                 x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
  7462.                 if (x != REPLY_COMPLETE) {
  7463.                     printf(">>> FILE NOT FOUND: %s\n",s);
  7464.                     break;
  7465.                 }
  7466. #endif /* COMMENT */
  7467.             }
  7468.         }
  7469.         if (skipthis) {                 /* Skipping this file? */
  7470.             ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
  7471.             if (msg)
  7472.               ftscreen(SCR_ST,ST_ERR,(CK_OFF_T)0,msg);
  7473.             else
  7474.               ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,s);
  7475.             continue;
  7476.         }
  7477.         if (fp_nml) {                   /* /NAMELIST only - no transfer */
  7478.             fprintf(fp_nml,"%s\n",s);
  7479.             continue;
  7480.         }
  7481.         if (recursive && haspath && !pipesend
  7482. #ifdef PIPESEND
  7483.             && !rcvfilter
  7484. #endif /* PIPESEND */
  7485.             ) {
  7486.         int x;
  7487.  
  7488. #ifdef NOMKDIR
  7489.         x = -1;
  7490. #else
  7491.             x = zmkdir(s);        /* Try to make the directory */
  7492. #endif /* NOMKDIR */
  7493.  
  7494.             if (x < 0) {
  7495.                 rc = -1;                /* Failure is fatal */
  7496.                 if (geterror) {
  7497.                     status = 0;
  7498.                     ftscreen(SCR_EM,0,(CK_OFF_T)0,
  7499.                  "Directory creation failure");
  7500.                     break;
  7501.                 }
  7502.             }
  7503.         }
  7504.  
  7505.         /* Not skipping */
  7506.  
  7507.     selected++;            /* Count this file as selected */
  7508.         pn = NULL;
  7509.  
  7510.     if (!gotsize && !mdel) {    /* Didn't get size yet */
  7511.         if (havesize > (CK_OFF_T)-1) { /* Already have file size? */
  7512.         fsize = havesize;
  7513.         gotsize = 1;
  7514.         } else {            /* No - must ask server */
  7515.         fsize = (CK_OFF_T)-1;
  7516.         if (sizeok) {
  7517.             x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
  7518.             if (x == REPLY_COMPLETE) {
  7519.             fsize = ckatofs(&ftp_reply_str[4]);
  7520.             gotsize = 1;
  7521.             }
  7522.         }
  7523.         }
  7524.     }
  7525.         if (mdel) {                     /* [M]DELETE */
  7526.             if (displa && !ftp_vbm)
  7527.               printf(" %s...",s);
  7528.             rc =
  7529.              (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
  7530.             if (rc > -1) {
  7531.                 tlog(F110,"ftp mdelete",s,0);
  7532.                 if (displa && !ftp_vbm)
  7533.                   printf("OK\n");
  7534.             } else {
  7535.                 tlog(F110,"ftp mdelete failed:",s,0);
  7536.                 if (displa)
  7537.                   printf("Failed\n");
  7538.             }
  7539. #ifndef NOSPL
  7540. #ifdef PIPESEND
  7541.         } else if (rcvfilter) {         /* [M]GET with filter */
  7542.             int n; char * p;
  7543.             n = CKMAXPATH;
  7544.             p = tmpbuf;                 /* Safe - no asname with filter */
  7545.             zzstring(rcvfilter,&p,&n);
  7546.             if (n > -1)
  7547.               pn = tmpbuf;
  7548.             debug(F111,"ftp get rcvfilter",pn,n);
  7549. #endif /* PIPESEND */
  7550. #endif /* NOSPL */
  7551.             if (toscreen) s2 = "-";
  7552.         } else if (pipesend) {          /* [M]GET /COMMAND */
  7553.             int n; char * p;
  7554.             n = CKMAXPATH;
  7555.             p = tmpbuf;                 /* Safe - no asname with filter */
  7556.             zzstring(pipename,&p,&n);
  7557.             if (n > -1)
  7558.               pn = tmpbuf;
  7559.             debug(F111,"ftp get pipename",pipename,n);
  7560.             if (toscreen) s2 = "-";
  7561.         } else {                        /* [M]GET with no pipes or filters */
  7562.             debug(F111,"ftp get s2 A",s2,x_cnv);
  7563.             if (toscreen) {
  7564.                 s2 = "-";               /* (hokey convention for stdout) */
  7565.             } else if (!*s2) {          /* No asname? */
  7566.                 if (x_cnv) {            /* If converting */
  7567.                     nzrtol(s,tmpbuf,x_cnv,1,CKMAXPATH); /* convert */
  7568.                     s2 = tmpbuf;
  7569.                     debug(F110,"ftp get nzrtol",s2,0);
  7570.                 } else                  /* otherwise */
  7571.                   s2 = s;               /* use incoming file's name */
  7572.             }
  7573.             debug(F110,"ftp get s2 B",s2,0);
  7574.  
  7575.             /* If local file already exists, take collision action */
  7576.  
  7577.             if (!pipesend &&
  7578. #ifdef PIPESEND
  7579.                 !rcvfilter &&
  7580. #endif /* PIPESEND */
  7581.                 !toscreen) {
  7582.         CK_OFF_T x;
  7583.                 x = zchki(s2);
  7584.                 debug(F111,"ftp get zchki",s2,x);
  7585.                 debug(F111,"ftp get x_fnc",s2,x_fnc);
  7586.  
  7587.                 if (x > (CK_OFF_T)-1 && !restart) {
  7588.             int x = -1;
  7589.             char * newname = NULL;
  7590.  
  7591.                     switch (x_fnc) {
  7592.                       case XYFX_A:      /* Append */
  7593.                         append = 1;
  7594.                         break;
  7595.                       case XYFX_R:      /* Rename */
  7596.                       case XYFX_B:    /* Backup */
  7597.             znewn(s2,&newname); /* Make unique name */
  7598.             debug(F110,"ftp get znewn",newname,0);
  7599.             if (x_fnc == XYFX_B) { /* Backup existing file */
  7600.                 x = zrename(s2,newname);
  7601.                 debug(F111,"ftp get backup zrename",newname,x);
  7602.             } else {      /* Rename incoming file */
  7603.                 x = ckstrncpy(tmpbuf,newname,CKMAXPATH+1);
  7604.                 s2 = tmpbuf;
  7605.                 debug(F111,"ftp get rename incoming",newname,x);
  7606.             }
  7607.             if (x < 0) {
  7608.                 ftscreen(SCR_EM,0,(CK_OFF_T)0,
  7609.                      "Backup/Rename failed");
  7610.                 x = 0;
  7611.                 goto xgetx;
  7612.             }
  7613.             break;
  7614.                       case XYFX_D:      /* Discard (already handled above) */
  7615.                       case XYFX_U:      /* Update (ditto) */
  7616.                       case XYFX_M:      /* Update (ditto) */
  7617.                       case XYFX_X:      /* Overwrite */
  7618.                         break;
  7619.                     }
  7620.                 }
  7621.             }
  7622.         }
  7623.         if (!mdel) {
  7624. #ifdef PIPESEND
  7625.             debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
  7626. #endif /* PIPESEND */
  7627.             if (pipesend && !toscreen)
  7628.               s2 = NULL;
  7629. #ifdef DEBUG
  7630.             if (deblog) {
  7631.                 debug(F101,"ftp get x_xla","",x_xla);
  7632.                 debug(F101,"ftp get x_csl","",x_csl);
  7633.                 debug(F101,"ftp get x_csr","",x_csr);
  7634.                 debug(F101,"ftp get append","",append);
  7635.             }
  7636. #endif /* DEBUG */
  7637.  
  7638.             rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
  7639.  
  7640. #ifdef DEBUG
  7641.             if (deblog) {
  7642.                 debug(F111,"ftp get rc",s,rc);
  7643.                 debug(F111,"ftp get cancelfile",s,cancelfile);
  7644.                 debug(F111,"ftp get cancelgroup",s,cancelgroup);
  7645.                 debug(F111,"ftp get renaming",s,renaming);
  7646.             }
  7647. #endif /* DEBUG */
  7648.         }
  7649.         if (rc > -1) {
  7650.             good++;
  7651.             status = 1;
  7652.             if (!cancelfile) {
  7653.                 if (deleting) {         /* GET /DELETE (source file) */
  7654.                     rc =
  7655.                       (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
  7656.                         ? 1 : -1;
  7657.                     tlog(F110, (rc > -1) ?
  7658.                          " deleted" : " failed to delete", s, 0);
  7659.                 } else if (renaming && rcv_rename && !toscreen) {
  7660.                     char *p;            /* Rename downloaded file */
  7661. #ifndef NOSPL
  7662.                     char tmpbuf[CKMAXPATH+1];
  7663.                     int n;
  7664.                     n = CKMAXPATH;
  7665.                     p = tmpbuf;
  7666.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7667.                     zzstring(rcv_rename,&p,&n);
  7668.                     debug(F111,"ftp get /rename",rcv_rename,0);
  7669.                     p = tmpbuf;
  7670. #else
  7671.                     p = rcv_rename;
  7672. #endif /* NOSPL */
  7673.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7674.                     debug(F111,"doftpget /RENAME zrename",p,rc);
  7675.                     tlog(F110, (rc > -1) ?
  7676.                          " renamed to" :
  7677.                          " failed to rename to",
  7678.                          p,
  7679.                          0
  7680.                          );
  7681.                 } else if (moving && rcv_move && !toscreen) {
  7682.                     char *p;            /* Move downloaded file */
  7683. #ifndef NOSPL
  7684.                     char tmpbuf[CKMAXPATH+1];
  7685.                     int n;
  7686.                     n = TMPBUFSIZ;
  7687.                     p = tmpbuf;
  7688.                     debug(F111,"ftp get /move-to",rcv_move,0);
  7689.                     zzstring(rcv_move,&p,&n);
  7690.                     p = tmpbuf;
  7691. #else
  7692.                     p = rcv_move;
  7693. #endif /* NOSPL */
  7694.                     debug(F111,"ftp get /move-to",p,0);
  7695.                     rc = (zrename(s2,p) < 0) ? -1 : 1;
  7696.                     debug(F111,"doftpget /MOVE zrename",p,rc);
  7697.                     tlog(F110, (rc > -1) ?
  7698.                          " moved to" : " failed to move to", p, 0);
  7699.                 }
  7700.                 if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
  7701.                     char * s = pv[SND_SRN].sval;
  7702.                     char * srvrn = pv[SND_SRN].sval;
  7703.                     char tmpbuf[CKMAXPATH+1];
  7704. #ifndef NOSPL
  7705.                     int y;              /* Pass it thru the evaluator */
  7706.                     extern int cmd_quoting; /* for \v(filename) */
  7707.                     debug(F111,"ftp get srv_renam",s,1);
  7708.  
  7709.                     if (cmd_quoting) {
  7710.                         y = CKMAXPATH;
  7711.                         s = (char *)tmpbuf;
  7712.                         zzstring(srvrn,&s,&y);
  7713.                         s = (char *)tmpbuf;
  7714.                     }
  7715. #endif /* NOSPL */
  7716.                     debug(F111,"ftp get srv_renam",s,1);
  7717.                     if (s) if (*s) {
  7718.                         int x;
  7719.                         x = ftp_rename(s2,s);
  7720.                         debug(F111,"ftp get ftp_rename",s2,x);
  7721.                         tlog(F110, (x > 0) ?
  7722.                              " renamed source file to" :
  7723.                              " failed to rename source file to",
  7724.                              s,
  7725.                              0
  7726.                              );
  7727.                         if (x < 1)
  7728.               return(-1);
  7729.                     }
  7730.                 }
  7731.             }
  7732.         }
  7733.         if (cancelfile)
  7734.           continue;
  7735.         if (rc < 0) {
  7736.             ftp_fai++;
  7737.             if (geterror) {
  7738.                 status = 0;
  7739.                 ftscreen(SCR_EM,0,(CK_OFF_T)0,"Fatal download error");
  7740.                 done++;
  7741.             }
  7742.         }
  7743.     }
  7744. #ifdef DEBUG
  7745.     if (deblog) {
  7746.     debug(F101,"ftp get status","",status);
  7747.     debug(F101,"ftp get cancelgroup","",cancelgroup);
  7748.     debug(F101,"ftp get cancelfile","",cancelfile);
  7749.     debug(F101,"ftp get selected","",selected);
  7750.     debug(F101,"ftp get good","",good);
  7751.     }
  7752. #endif /* DEBUG */
  7753.  
  7754.     if (selected == 0) {        /* No files met selection criteria */
  7755.     status = 1;            /* which is a kind of success. */
  7756.     } else if (status > 0) {        /* Some files were selected */
  7757.         if (cancelgroup)        /* but MGET was canceled */
  7758.           status = 0;            /* so MGET failed */
  7759.         else if (cancelfile && good < 1) /* If file was canceled */
  7760.           status = 0;            /* MGET failed if it got no files */
  7761.     }
  7762.     success = status;
  7763.     x = success;
  7764.     debug(F101,"ftp get success","",success);
  7765.  
  7766.   xgetx:
  7767.     pipesend = pipesave;                /* Restore global pipe selection */
  7768.     if (fp_nml) {                       /* Close /NAMELIST */
  7769.         if (fp_nml != stdout)
  7770.           fclose(fp_nml);
  7771.         fp_nml = NULL;
  7772.     }
  7773.     if (x > -1) {                       /* Download successful */
  7774. #ifdef GFTIMER
  7775.         t1 = gmstimer();                /* End time */
  7776.         sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
  7777.         if (!sec) sec = 0.001;
  7778.         fptsecs = sec;
  7779. #else
  7780.         sec = (t1 - t0) / 1000;
  7781.         if (!sec) sec = 1;
  7782. #endif /* GFTIMER */
  7783.         tfcps = (long) (tfc / sec);
  7784.         tsecs = (int)sec;
  7785.         lastxfer = W_FTP|W_RECV;
  7786.         xferstat = success;
  7787.     }
  7788.     if (dpyactive)
  7789.       ftscreen(SCR_TC,0,(CK_OFF_T)0,"");
  7790. #ifdef CK_TMPDIR
  7791.     if (f_tmpdir) {                     /* If we changed to download dir */
  7792.         zchdir((char *) savdir);        /* Go back where we came from */
  7793.         f_tmpdir = 0;
  7794.     }
  7795. #endif /* CK_TMPDIR */
  7796.  
  7797.     for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
  7798.         if (pv[i].sval)
  7799.           free(pv[i].sval);
  7800.     }
  7801.     for (i = 0; i < mgetn; i++)         /* MGET list too */
  7802.       makestr(&(mgetlist[i]),NULL);
  7803.  
  7804.     if (cancelgroup)            /* Clear temp-file stack */
  7805.       mlsreset();
  7806.  
  7807.     ftreset();                          /* Undo switch effects */
  7808.     dpyactive = 0;
  7809.     return(x);
  7810. }
  7811.  
  7812. static struct keytab ftprmt[] = {
  7813.     { "cd",        XZCWD, 0 },
  7814.     { "cdup",      XZCDU, 0 },
  7815.     { "cwd",       XZCWD, CM_INV },
  7816.     { "delete",    XZDEL, 0 },
  7817.     { "directory", XZDIR, 0 },
  7818.     { "exit",      XZXIT, 0 },
  7819.     { "help",      XZHLP, 0 },
  7820.     { "login",     XZLGI, 0 },
  7821.     { "logout",    XZLGO, 0 },
  7822.     { "mkdir",     XZMKD, 0 },
  7823.     { "pwd",       XZPWD, 0 },
  7824.     { "rename",    XZREN, 0 },
  7825.     { "rmdir",     XZRMD, 0 },
  7826.     { "type",      XZTYP, 0 },
  7827.     { "", 0, 0 }
  7828. };
  7829. static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
  7830.  
  7831. int
  7832. doftpsite() {                /* Send a SITE command */
  7833.     int reply;
  7834.     char * s;
  7835.     int lcs = -1, rcs = -1;
  7836.     int save_vbm = ftp_vbm;
  7837.  
  7838. #ifndef NOCSETS
  7839.     if (ftp_xla) {
  7840.         lcs = ftp_csl;
  7841.         if (lcs < 0) lcs = fcharset;
  7842.         rcs = ftp_csx;
  7843.         if (rcs < 0) rcs = ftp_csr;
  7844.     }
  7845. #endif /* NOCSETS */
  7846.     if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  7847.       return(x);
  7848.     CHECKCONN();
  7849.     ckstrncpy(line,s,LINBUFSIZ);
  7850.     if (testing) printf(" ftp site \"%s\"...\n",line);
  7851.     if (!ftp_vbm)
  7852.         ftp_vbm = !ckstrcmp("HELP",line,4,0);
  7853.     if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
  7854.     do {
  7855.         reply = getreply(0,lcs,rcs,ftp_vbm,0);
  7856.     } while (reply == REPLY_PRELIM);
  7857.     }
  7858.     ftp_vbm = save_vbm;
  7859.     return(success = (reply == REPLY_COMPLETE));
  7860. }
  7861.  
  7862.  
  7863. int
  7864. dosetftppsv() {                /* Passive mode */
  7865.     x = seton(&ftp_psv);
  7866.     if (x > 0) passivemode = ftp_psv;
  7867.     return(x);
  7868. }
  7869.  
  7870. /*  d o f t p r m t  --  Parse and execute REMOTE commands  */
  7871.  
  7872. int
  7873. doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
  7874.     /* cx == 0 means REMOTE */
  7875.     /* cx != 0 is a XZxxx value */
  7876.     char * s;
  7877.  
  7878.     if (who != 0)
  7879.       return(0);
  7880.  
  7881.     if (cx == 0) {
  7882.         if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
  7883.           return(x);
  7884.         cx = x;
  7885.     }
  7886.     switch (cx) {
  7887.       case XZCDU:                       /* CDUP */
  7888.         if ((x = cmcfm()) < 0) return(x);
  7889.         return(doftpcdup());
  7890.  
  7891.       case XZCWD:                       /* RCD */
  7892.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  7893.           return(x);
  7894.         ckstrncpy(line,s,LINBUFSIZ);
  7895.     s = brstrip(line);
  7896.         return(doftpcwd(s,1));
  7897.       case XZPWD:                       /* RPWD */
  7898.         return(doftppwd());
  7899.       case XZDEL:                       /* RDEL */
  7900.         return(doftpget(FTP_MDE,1));
  7901.       case XZDIR:                       /* RDIR */
  7902.         return(doftpdir(FTP_DIR));
  7903.       case XZHLP:                       /* RHELP */
  7904.         return(doftpxhlp());
  7905.       case XZMKD:                       /* RMKDIR */
  7906.         return(doftpmkd());
  7907.       case XZREN:                       /* RRENAME */
  7908.         return(doftpren());
  7909.       case XZRMD:                       /* RRMDIR */
  7910.         return(doftprmd());
  7911.       case XZLGO:                       /* LOGOUT */
  7912.         return(doftpres());
  7913.       case XZXIT:                       /* EXIT */
  7914.         return(ftpbye());
  7915.     }
  7916.     printf("?Not usable with FTP - \"%s\"\n", atmbuf);
  7917.     return(-9);
  7918. }
  7919.  
  7920. int
  7921. doxftp() {                              /* Command parser for built-in FTP */
  7922.     int cx, n;
  7923.     struct FDB kw, fl;
  7924.     char * s;
  7925.     int usetls = 0;
  7926.     int lcs = -1, rcs = -1;
  7927.  
  7928. #ifndef NOCSETS
  7929.     if (ftp_xla) {
  7930.         lcs = ftp_csl;
  7931.         if (lcs < 0) lcs = fcharset;
  7932.         rcs = ftp_csx;
  7933.         if (rcs < 0) rcs = ftp_csr;
  7934.     }
  7935. #endif /* NOCSETS */
  7936.  
  7937.     if (inserver)                       /* FTP not allowed in IKSD. */
  7938.       return(-2);
  7939.  
  7940.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  7941.         ftp_typ = g_ftp_typ;
  7942.         /* g_ftp_typ = -1; */
  7943.     }
  7944. #ifdef COMMENT
  7945. /*
  7946.   We'll set the collision action locally in doftpget() based on whether
  7947.   ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
  7948. */
  7949.     if (ftp_fnc < 0)                    /* Inherit global collision action */
  7950.       ftp_fnc = fncact;                 /* if none specified for FTP */
  7951. #endif /* COMMENT */
  7952.  
  7953.     /* Restore global verbose mode */
  7954.     if (ftp_deb)
  7955.       ftp_vbm = 1;
  7956.     else if (quiet)
  7957.       ftp_vbm = 0;
  7958.     else
  7959.       ftp_vbm = ftp_vbx;
  7960.  
  7961.     ftp_dates &= 1;            /* Undo any previous /UPDATE switch */
  7962.  
  7963.     dpyactive = 0;                      /* Reset global transfer-active flag */
  7964.     printlines = 0;                     /* Reset printlines */
  7965.  
  7966.     if (fp_nml) {                       /* Reset /NAMELIST */
  7967.         if (fp_nml != stdout)
  7968.           fclose(fp_nml);
  7969.         fp_nml = NULL;
  7970.     }
  7971.     makestr(&ftp_nml,NULL);
  7972.  
  7973.     cmfdbi(&kw,                         /* First FDB - commands */
  7974.            _CMKEY,                      /* fcode */
  7975.            "Hostname; or FTP command",  /* help */
  7976.            "",                          /* default */
  7977.            "",                          /* addtl string data */
  7978.            nftpcmd,                     /* addtl numeric data 1: tbl size */
  7979.            0,                           /* addtl numeric data 2: none */
  7980.            xxstring,                    /* Processing function */
  7981.            ftpcmdtab,                   /* Keyword table */
  7982.            &fl                          /* Pointer to next FDB */
  7983.            );
  7984.     cmfdbi(&fl,                         /* A host name or address */
  7985.            _CMFLD,                      /* fcode */
  7986.            "Hostname or address",       /* help */
  7987.            "",                          /* default */
  7988.            "",                          /* addtl string data */
  7989.            0,                           /* addtl numeric data 1 */
  7990.            0,                           /* addtl numeric data 2 */
  7991.            xxstring,
  7992.            NULL,
  7993.            NULL
  7994.            );
  7995.     x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
  7996.     if (x == -3) {
  7997.         printf("?ftp what? \"help ftp\" for hints\n");
  7998.         return(-9);
  7999.     }
  8000.     if (x < 0)
  8001.       return(x);
  8002.     if (cmresult.fcode == _CMFLD) {     /* If hostname */
  8003.         return(openftp(cmresult.sresult,0)); /* go open the connection */
  8004.     } else {
  8005.         cx = cmresult.nresult;
  8006.     }
  8007.     switch (cx) {
  8008.       case FTP_ACC:                     /* ACCOUNT */
  8009.         if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
  8010.           return(x);
  8011.         CHECKCONN();
  8012.         makestr(&ftp_acc,s);
  8013.         if (testing)
  8014.           printf(" ftp account: \"%s\"\n",ftp_acc);
  8015.         success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  8016.         return(success);
  8017.  
  8018.       case FTP_GUP:                     /* Go UP */
  8019.         if ((x = cmcfm()) < 0) return(x);
  8020.         CHECKCONN();
  8021.         if (testing) printf(" ftp cd: \"(up)\"\n");
  8022.         return(success = doftpcdup());
  8023.  
  8024.       case FTP_CWD:                     /* CD */
  8025.         if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
  8026.           return(x);
  8027.         CHECKCONN();
  8028.         ckstrncpy(line,s,LINBUFSIZ);
  8029.         if (testing)
  8030.           printf(" ftp cd: \"%s\"\n", line);
  8031.         return(success = doftpcwd(line,1));
  8032.  
  8033.       case FTP_CHM:                     /* CHMOD */
  8034.         if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
  8035.           return(x);
  8036.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8037.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8038.           return(x);
  8039.         CHECKCONN();
  8040.         ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
  8041.         if (testing)
  8042.           printf(" ftp chmod: %s\n",ftpcmdbuf);
  8043.         success =
  8044.           (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8045.         return(success);
  8046.  
  8047.       case FTP_CLS:                     /* CLOSE FTP connection */
  8048.         if ((y = cmcfm()) < 0)
  8049.           return(y);
  8050.         CHECKCONN();
  8051.         if (testing)
  8052.           printf(" ftp closing...\n");
  8053.         ftpclose();
  8054.         return(success = 1);
  8055.  
  8056.       case FTP_DIR:                     /* DIRECTORY of remote files */
  8057.       case FTP_VDI:
  8058.         return(doftpdir(cx));
  8059.  
  8060.       case FTP_GET:                     /* GET a remote file */
  8061.       case FTP_RGE:                     /* REGET */
  8062.       case FTP_MGE:                     /* MGET */
  8063.       case FTP_MDE:                     /* MDELETE */
  8064.         return(doftpget(cx,1));
  8065.  
  8066.       case FTP_IDL:                     /* IDLE */
  8067.         if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
  8068.           return(x);
  8069.         if ((y = cmcfm()) < 0)
  8070.           return(y);
  8071.         CHECKCONN();
  8072.         if (z < 0)  {                   /* Display idle timeout */
  8073.             if (testing)
  8074.               printf(" ftp query idle timeout...\n");
  8075.             success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
  8076.         } else {                        /* Set idle timeout */
  8077.             if (testing)
  8078.               printf(" ftp idle timeout set: %d...\n",z);
  8079.             success =
  8080.               (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
  8081.         }
  8082.         return(success);
  8083.  
  8084.       case FTP_MKD:                     /* MKDIR */
  8085.         return(doftpmkd());
  8086.  
  8087.       case FTP_MOD:                     /* MODTIME */
  8088.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8089.           return(x);
  8090.         CHECKCONN();
  8091.         ckstrncpy(line,s,LINBUFSIZ);
  8092.         if (testing)
  8093.           printf(" ftp modtime \"%s\"...\n",line);
  8094.         success = 0;
  8095.         if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
  8096.         success = 1;
  8097.         mdtmok = 1;
  8098.         if (!quiet) {
  8099.         int flag = 0;
  8100.         char c, * s;
  8101.         struct tm tmremote;
  8102.  
  8103.         bzero((char *)&tmremote, sizeof(struct tm));
  8104.         s = ftp_reply_str;
  8105.         while ((c = *s++)) {
  8106.             if (c == SP) {
  8107.             flag++;
  8108.             break;
  8109.             }
  8110.         }
  8111.         if (flag) {
  8112.             if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
  8113.                    &tmremote.tm_year,
  8114.                    &tmremote.tm_mon,
  8115.                    &tmremote.tm_mday,
  8116.                    &tmremote.tm_hour,
  8117.                    &tmremote.tm_min,
  8118.                    &tmremote.tm_sec
  8119.                    ) == 6) {
  8120.             printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
  8121.                    line,
  8122.                    tmremote.tm_year,
  8123.                    tmremote.tm_mon,
  8124.                    tmremote.tm_mday,
  8125.                    tmremote.tm_hour,
  8126.                    tmremote.tm_min,
  8127.                    tmremote.tm_sec
  8128.                    );
  8129.             } else {
  8130.             success = 0;
  8131.             }
  8132.         }
  8133.         }
  8134.         }
  8135.         return(success);
  8136.  
  8137.       case FTP_OPN:                     /* OPEN connection */
  8138. #ifdef COMMENT
  8139.         x = cmfld("IP hostname or address","",&s,xxstring);
  8140.         if (x < 0) {
  8141.             success = 0;
  8142.             return(x);
  8143.         }
  8144.         ckstrncpy(line,s,LINBUFSIZ);
  8145.         s = line;
  8146.         return(openftp(s,0));
  8147. #else
  8148.         {                               /* OPEN connection */
  8149.             char name[TTNAMLEN+1], *p;
  8150.             extern int network;
  8151.             extern char ttname[];
  8152.             if (network)                /* If we have a current connection */
  8153.               ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
  8154.             else
  8155.               *name = '\0';             /* as default host */
  8156.             for (p = name; *p; p++)     /* Remove ":service" from end. */
  8157.               if (*p == ':') { *p = '\0'; break; }
  8158. #ifndef USETLSTAB
  8159.             x = cmfld("IP hostname or address",name,&s,xxstring);
  8160. #else
  8161.             cmfdbi(&kw,                 /* First FDB - commands */
  8162.                    _CMKEY,              /* fcode */
  8163.                    "Hostname or switch", /* help */
  8164.                    "",                  /* default */
  8165.                    "",                  /* addtl string data */
  8166.                    ntlstab,             /* addtl numeric data 1: tbl size */
  8167.                    0,                   /* addtl numeric data 2: none */
  8168.                    xxstring,            /* Processing function */
  8169.                    tlstab,              /* Keyword table */
  8170.                    &fl                  /* Pointer to next FDB */
  8171.                    );
  8172.             cmfdbi(&fl,                 /* A host name or address */
  8173.                    _CMFLD,              /* fcode */
  8174.                    "Hostname or address", /* help */
  8175.                    "",                  /* default */
  8176.                    "",                  /* addtl string data */
  8177.                    0,                   /* addtl numeric data 1 */
  8178.                    0,                   /* addtl numeric data 2 */
  8179.                    xxstring,
  8180.                    NULL,
  8181.                    NULL
  8182.                    );
  8183.  
  8184.             for (n = 0;; n++) {
  8185.                 x = cmfdb(&kw);         /* Parse a hostname or a keyword */
  8186.                 if (x == -3) {
  8187.                   printf("?ftp open what? \"help ftp\" for hints\n");
  8188.                   return(-9);
  8189.                 }
  8190.                 if (x < 0)
  8191.                   break;
  8192.                 if (cmresult.fcode == _CMFLD) { /* Hostname */
  8193.                     s = cmresult.sresult;
  8194.                     break;
  8195.                 } else if (cmresult.nresult == OPN_TLS) {
  8196.                     usetls = 1;
  8197.                 }
  8198.             }
  8199. #endif /* USETLSTAB */
  8200.             if (x < 0) {
  8201.                 success = 0;
  8202.                 return(x);
  8203.             }
  8204.             ckstrncpy(line,s,LINBUFSIZ);
  8205.             s = line;
  8206.             return(openftp(s,usetls));
  8207.         }
  8208. #endif /* COMMENT */
  8209.  
  8210.       case FTP_PUT:                     /* PUT */
  8211.       case FTP_MPU:                     /* MPUT */
  8212.       case FTP_APP:                     /* APPEND */
  8213.         return(doftpput(cx,1));
  8214.  
  8215.       case FTP_PWD:                     /* PWD */
  8216.         x = doftppwd();
  8217.         if (x > -1) success = x;
  8218.         return(x);
  8219.  
  8220.       case FTP_REN:                     /* RENAME */
  8221.         return(doftpren());
  8222.  
  8223.       case FTP_RES:                     /* RESET */
  8224.         return(doftpres());
  8225.  
  8226.       case FTP_HLP:                     /* (remote) HELP */
  8227.         return(doftpxhlp());
  8228.  
  8229.       case FTP_RMD:                     /* RMDIR */
  8230.         return(doftprmd());
  8231.  
  8232.       case FTP_STA:                     /* STATUS */
  8233.         if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
  8234.           return(x);
  8235.         CHECKCONN();
  8236.         ckstrncpy(line,s,LINBUFSIZ);
  8237.         if (testing) printf(" ftp status \"%s\"...\n",line);
  8238.         success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
  8239.         return(success);
  8240.  
  8241.       case FTP_SIT: {                   /* SITE */
  8242.       return(doftpsite());
  8243.       }
  8244.  
  8245.       case FTP_SIZ:                     /* (ask for) SIZE */
  8246.         if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
  8247.           return(x);
  8248.         CHECKCONN();
  8249.         ckstrncpy(line,s,LINBUFSIZ);
  8250.         if (testing)
  8251.           printf(" ftp size \"%s\"...\n",line);
  8252.         success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
  8253.     if (success)
  8254.       sizeok = 1;
  8255.         return(success);
  8256.  
  8257.       case FTP_SYS:                     /* Ask for server's SYSTEM type */
  8258.         if ((x = cmcfm()) < 0) return(x);
  8259.         CHECKCONN();
  8260.         if (testing)
  8261.           printf(" ftp system...\n");
  8262.         success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
  8263.         return(success);
  8264.  
  8265.       case FTP_UMA:                     /* Set/query UMASK */
  8266.         if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
  8267.           if (x != -3)
  8268.             return(x);
  8269.         ckstrncpy(tmpbuf,s,TMPBUFSIZ);
  8270.         if ((x = cmcfm()) < 0) return(x);
  8271.         CHECKCONN();
  8272.         if (testing) {
  8273.             if (tmpbuf[0])
  8274.               printf(" ftp umask \"%s\"...\n",tmpbuf);
  8275.             else
  8276.               printf(" ftp query umask...\n");
  8277.         }
  8278.         success = ftp_umask(tmpbuf);
  8279.         return(success);
  8280.  
  8281.       case FTP_USR:
  8282.         return(doftpusr());
  8283.  
  8284.       case FTP_QUO:
  8285.         if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
  8286.           return(x);
  8287.         CHECKCONN();
  8288.         success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  8289.         return(success);
  8290.  
  8291.       case FTP_TYP:                     /* Type */
  8292.         if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
  8293.           return(x);
  8294.         if ((y = cmcfm()) < 0) return(y);
  8295.         CHECKCONN();
  8296.         ftp_typ = x;
  8297.         g_ftp_typ = x;
  8298.         tenex = (ftp_typ == FTT_TEN);
  8299.         changetype(ftp_typ,ftp_vbm);
  8300.         return(1);
  8301.  
  8302.       case FTP_CHK:                     /* Check if remote file(s) exist(s) */
  8303.         if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
  8304.           return(x);
  8305.         CHECKCONN();
  8306.         success = remote_files(1,(CHAR *)s,NULL,0) ? 1 : 0;
  8307.         return(success);
  8308.  
  8309.       case FTP_FEA:                     /* RFC2389 */
  8310.         if ((y = cmcfm()) < 0)
  8311.           return(y);
  8312.         CHECKCONN();
  8313.     success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE);
  8314.     if (success) {
  8315.         if (sfttab[0] > 0) {
  8316.         ftp_aut = sfttab[SFT_AUTH];
  8317.         sizeok  = sfttab[SFT_SIZE];
  8318.         mdtmok  = sfttab[SFT_MDTM];
  8319.         mlstok  = sfttab[SFT_MLST];
  8320.         }
  8321.     }
  8322.     return(success);
  8323.  
  8324.       case FTP_OPT:                     /* RFC2389 */
  8325.         /* Perhaps this should be a keyword list... */
  8326.         if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
  8327.           return(x);
  8328.         CHECKCONN();
  8329.         ckstrncpy(line,s,LINBUFSIZ);
  8330.         if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
  8331.           return(x);
  8332.         success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  8333.         return(success);
  8334.  
  8335.       case FTP_ENA:            /* FTP ENABLE */
  8336.       case FTP_DIS:            /* FTP DISABLE */
  8337.         if ((x = cmkey(ftpenatab,nftpena,"","",xxstring)) < 0)
  8338.           return(x);
  8339.         if ((y = cmcfm()) < 0) return(y);
  8340.     switch (x) {
  8341.       case ENA_AUTH:        /* OK to use autoauthentication */
  8342.         ftp_aut = (cx == FTP_ENA) ? 1 : 0;
  8343.         sfttab[SFT_AUTH] = ftp_aut;
  8344.         break;
  8345.       case ENA_FEAT:        /* OK to send FEAT command */
  8346.         featok = (cx == FTP_ENA) ? 1 : 0;
  8347.         break;
  8348.       case ENA_MLST:        /* OK to use MLST/MLSD */
  8349.         mlstok = (cx == FTP_ENA) ? 1 : 0;
  8350.         sfttab[SFT_MLST] = mlstok;
  8351.         break;
  8352.       case ENA_MDTM:        /* OK to use MDTM */
  8353.         mdtmok = (cx == FTP_ENA) ? 1 : 0;
  8354.         sfttab[SFT_MDTM] = mdtmok;
  8355.         break;
  8356.       case ENA_SIZE:        /* OK to use SIZE */
  8357.         sizeok = (cx == FTP_ENA) ? 1 : 0;
  8358.         sfttab[SFT_SIZE] = sizeok;
  8359.         break;
  8360.     }
  8361.     return(success = 1);
  8362.     }
  8363.     return(-2);
  8364. }
  8365.  
  8366. #ifndef NOSHOW
  8367. static char *
  8368. shopl(x) int x; {
  8369.     switch (x) {
  8370.       case FPL_CLR: return("clear");
  8371.       case FPL_PRV: return("private");
  8372.       case FPL_SAF: return("safe");
  8373.       case 0:  return("(not set)");
  8374.       default: return("(unknown)");
  8375.     }
  8376. }
  8377.  
  8378. int
  8379. shoftp(brief) int brief; {
  8380.     char * s = "?";
  8381.     int n, x;
  8382.  
  8383.     if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
  8384.         ftp_typ = g_ftp_typ;
  8385.         /* g_ftp_typ = -1; */
  8386.     }
  8387.     printf("\n");
  8388.     printf("FTP connection:                 %s\n",connected ?
  8389.            ftp_host :
  8390.            "(none)"
  8391.            );
  8392.     n = 2;
  8393.     if (connected) {
  8394.         n++;
  8395.         printf("FTP server type:                %s\n",
  8396.                ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
  8397.     }
  8398.     if (loggedin)
  8399.       printf("Logged in as:                   %s\n",
  8400.              strval(ftp_logname,"(unknown)"));
  8401.     else
  8402.       printf("Not logged in\n");
  8403.     n++;
  8404.     if (brief) return(0);
  8405.  
  8406.     printf("\nSET FTP values:\n\n");
  8407.     n += 3;
  8408.  
  8409.     printf(" ftp anonymous-password:        %s\n",
  8410.        ftp_apw ? ftp_apw : "(default)"
  8411.        );
  8412.     printf(" ftp auto-login:                %s\n",showoff(ftp_log));
  8413.     printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
  8414.     switch (ftp_typ) {
  8415.       case FTT_ASC: s = "text"; break;
  8416.       case FTT_BIN: s = "binary"; break;
  8417.       case FTT_TEN: s = "tenex"; break;
  8418.     }
  8419.     printf(" ftp type:                      %s\n",s);
  8420.     printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
  8421.     printf(" ftp dates:                     %s\n",showoff(ftp_dates));
  8422.     printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
  8423.     printf(" ftp filenames:                 %s\n",
  8424.            ftp_cnv == CNV_AUTO ? "auto" : (ftp_cnv ? "converted" : "literal")
  8425.            );
  8426.     printf(" ftp debug                      %s\n",showoff(ftp_deb));
  8427.  
  8428.     printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
  8429.     printf(" ftp permissions:               %s\n",showooa(ftp_prm));
  8430.     printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
  8431.     printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
  8432.     printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
  8433. #ifdef COMMENT
  8434.     /* See note in doxftp() */
  8435.     if (ftp_fnc < 0)
  8436.       ftp_fnc = fncact;
  8437. #endif /* COMMENT */
  8438.     printf(" ftp collision:                 %s\n",
  8439.        fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
  8440.     printf(" ftp server-time-offset:        %s\n",
  8441.        fts_sto ? fts_sto : "(none)");
  8442.     n += 15;
  8443.  
  8444. #ifndef NOCSETS
  8445.     printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
  8446.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8447.  
  8448.     printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
  8449.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8450.  
  8451.     printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
  8452.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8453. #endif /* NOCSETS */
  8454.  
  8455.     x = ftp_dis;
  8456.     if (x < 0)
  8457.       x = fdispla;
  8458.     switch (x) {
  8459.       case XYFD_N: s = "none"; break;
  8460.       case XYFD_R: s = "serial"; break;
  8461.       case XYFD_C: s = "fullscreen"; break;
  8462.       case XYFD_S: s = "crt"; break;
  8463.       case XYFD_B: s = "brief"; break;
  8464.     }
  8465.     printf(" ftp display:                   %s\n",s);
  8466.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8467.  
  8468.     if (mlstok || featok || mdtmok || sizeok || ftp_aut) {
  8469.     printf(" enabled:                      ");
  8470.     if (ftp_aut) printf(" AUTH");
  8471.     if (featok)  printf(" FEAT");
  8472.     if (mdtmok)  printf(" MDTM");
  8473.     if (mlstok)  printf(" MLST");
  8474.     if (sizeok)  printf(" SIZE");
  8475.     printf("\n");
  8476.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8477.     }
  8478.     if (!mlstok || !featok || !mdtmok || !sizeok || !ftp_aut) {
  8479.     printf(" disabled:                     ");
  8480.     if (!ftp_aut) printf(" AUTH");
  8481.     if (!featok)  printf(" FEAT");
  8482.     if (!mdtmok)  printf(" MDTM");
  8483.     if (!mlstok)  printf(" MLST");
  8484.     if (!sizeok)  printf(" SIZE");
  8485.     printf("\n");
  8486.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8487.     }
  8488.     switch (ftpget) {
  8489.       case 0: s = "kermit"; break;
  8490.       case 1: s = "ftp"; break;
  8491.       case 2: s = "auto"; break;
  8492.       default: s = "?";
  8493.     }
  8494.     printf(" get-put-remote:                %s\n",s);
  8495.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8496.  
  8497.     printf("\n");
  8498.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8499.  
  8500. #ifdef FTP_SECURITY
  8501.     printf("Available security methods:    ");
  8502. #ifdef FTP_GSSAPI
  8503.     printf("GSSAPI ");
  8504. #endif /* FTP_GSSAPI */
  8505. #ifdef FTP_KRB4
  8506.     printf("Kerberos4 ");
  8507. #endif /* FTP_KRB4 */
  8508. #ifdef FTP_SRP
  8509.     printf("SRP ");
  8510. #endif /* FTP_SRP */
  8511. #ifdef FTP_SSL
  8512.     printf("SSL ");
  8513. #endif /* FTP_SSL */
  8514.  
  8515.     n++;
  8516.     printf("\n\n");
  8517.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8518.     printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
  8519.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8520.     printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
  8521.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8522.     printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
  8523.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8524.     printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
  8525.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8526.     printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
  8527.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8528.     printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
  8529.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8530. #else
  8531.     printf("Available security methods:     (none)\n");
  8532.     if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
  8533. #endif /* FTP_SECURITY */
  8534.  
  8535.     if (n <= cmd_rows - 3)
  8536.       printf("\n");
  8537.     return(0);
  8538. }
  8539. #endif /* NOSHOW */
  8540.  
  8541. #ifndef NOHELP
  8542. /* FTP HELP text strings */
  8543.  
  8544. static char * fhs_ftp[] = {
  8545.     "Syntax: FTP subcommand [ operands ]",
  8546.     "  Makes an FTP connection, or sends a command to the FTP server.",
  8547.     "  To see a list of available FTP subcommands, type \"ftp ?\".",
  8548.     "  and then use HELP FTP xxx to get help about subcommand xxx.",
  8549.     "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
  8550.     ""
  8551. };
  8552.  
  8553. static char * fhs_acc[] = {             /* ACCOUNT */
  8554.     "Syntax: FTP ACCOUNT text",
  8555.     "  Sends an account designator to an FTP server that needs one.",
  8556.     "  Most FTP servers do not use accounts; some use them for other",
  8557.     "  other purposes, such as disk-access passwords.",
  8558.     ""
  8559. };
  8560. static char * fhs_app[] = {             /* APPEND */
  8561.     "Syntax: FTP APPEND filname",
  8562.     "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
  8563.     ""
  8564. };
  8565. static char * fhs_cls[] = {             /* BYE, CLOSE */
  8566.     "Syntax: [ FTP ] BYE",
  8567.     "  Logs out from the FTP server and closes the FTP connection.",
  8568.     "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
  8569.     ""
  8570. };
  8571. static char * fhs_cwd[] = {             /* CD, CWD */
  8572.     "Syntax: [ FTP ] CD directory",
  8573.     "  Asks the FTP server to change to the given directory.",
  8574.     "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
  8575.     ""
  8576. };
  8577. static char * fhs_gup[] = {             /* CDUP, UP */
  8578.     "Syntax: FTP CDUP",
  8579.     "  Asks the FTP server to change to the parent directory of its current",
  8580.     "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
  8581.     ""
  8582. };
  8583. static char * fhs_chm[] = {             /* CHMOD */
  8584.     "Syntax: FTP CHMOD filename permissions",
  8585.     "  Asks the FTP server to change the permissions, protection, or mode of",
  8586.     "  the given file.  The given permissions must be in the syntax of the",
  8587.     "  the server's file system, e.g. an octal number for UNIX.  Also see",
  8588.     "  FTP PUT /PERMISSIONS",
  8589.     ""
  8590. };
  8591. static char * fhs_mde[] = {             /* DELETE */
  8592.     "Syntax: FTP DELETE [ switches ] filespec",
  8593.     "  Asks the FTP server to delete the given file or files.",
  8594.     "  Synonym: MDELETE (Kermit makes no distinction between single and",
  8595.     "  multiple file deletion).  Optional switches:",
  8596.     " ",
  8597.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8598.     "  /EXCEPT:pattern",
  8599.     "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
  8600.     "  /LARGER-THAN:number",
  8601. #ifdef UNIXOROSK
  8602.     "  /NODOTFILES",
  8603. #endif /* UNIXOROSK */
  8604.     "  /QUIET",
  8605. #ifdef RECURSIVE
  8606.     "  /RECURSIVE (depends on server)",
  8607.     "  /SUBDIRECTORIES",
  8608. #endif /* RECURSIVE */
  8609.     "  /SMALLER-THAN:number",
  8610.     ""
  8611. };
  8612. static char * fhs_dir[] = {             /* DIRECTORY */
  8613.     "Syntax: FTP DIRECTORY [ filespec ]",
  8614.     "  Asks the server to send a directory listing of the files that match",
  8615.     "  the given filespec, or if none is given, all the files in its current",
  8616.     "  directory.  The filespec, including any wildcards, must be in the",
  8617.     "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
  8618.     "  Synonym: RDIRECTORY.",
  8619.     ""
  8620. };
  8621. static char * fhs_vdi[] = {             /* VDIRECTORY */
  8622.     "Syntax: FTP VDIRECTORY [ filespec ]",
  8623.     "  Asks the server to send a directory listing of the files that match",
  8624.     "  the given filespec, or if none is given, all the files in its current",
  8625.     "  directory.  VDIRECTORY is needed for getting verbose directory",
  8626.     "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
  8627.     "  FTP DIRECTORY lists only filenames without details.",
  8628.     ""
  8629. };
  8630. static char * fhs_fea[] = {             /* FEATURES */
  8631.     "Syntax: FTP FEATURES",
  8632.     "  Asks the FTP server to list its special features.  Most FTP servers",
  8633.     "  do not recognize this command.",
  8634.     ""
  8635. };
  8636. static char * fhs_mge[] = {             /* MGET */
  8637.     "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
  8638.     "  Download a single file or multiple files.  Asks the FTP server to send",
  8639.     "  the given file or files.  Also see FTP GET.  Optional switches:",
  8640.     " ",
  8641.     "  /AS-NAME:text",
  8642.     "    Name under which to store incoming file.",
  8643.     "    Pattern required for for multiple files.",
  8644.     "  /BINARY",                        /* /IMAGE */
  8645.     "    Force binary mode.  Synonym: /IMAGE.",
  8646.     "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
  8647.    "    What to do if an incoming file has the same name as an existing file.",
  8648.  
  8649. #ifdef PUTPIPE
  8650.     "  /COMMAND",
  8651.     "    Specifies that the as-name is a command to which the incoming file",
  8652.     "    is to be piped as standard input.",
  8653. #endif /* PUTPIPE */
  8654.  
  8655. #ifdef DOUPDATE
  8656.     "  /DATES-DIFFER",
  8657.     "    Download only those files whose modification date-times differ from",
  8658.     "    those of the corresponding local files, or that do not already",
  8659.     "    exist on the local computer.",
  8660. #endif /* DOUPDATE */
  8661.  
  8662.     "  /DELETE",
  8663.     "    Specifies that each file is to be deleted from the server after,",
  8664.     "    and only if, it is successfully downloaded.",
  8665.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8666.     "    When downloading a group of files, what to do upon failure to",
  8667.     "    transfer a file: quit or proceed to the next one.",
  8668.     "  /EXCEPT:pattern",
  8669.     "    Exception list: don't download any files that match this pattern.",
  8670.     "    See HELP WILDCARD for pattern syntax.",
  8671.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8672.     "    Whether to convert incoming filenames to local syntax.",
  8673. #ifdef PIPESEND
  8674. #ifndef NOSPL
  8675.     "  /FILTER:command",
  8676.     "    Pass incoming files through the given command.",
  8677. #endif /* NOSPL */
  8678. #endif /* PIPESEND */
  8679.     "  /LARGER-THAN:number",
  8680.     "    Only download files that are larger than the given number of bytes.",
  8681.     "  /LISTFILE:filename",
  8682.     "    Obtain the list of files to download from the given file.",
  8683. #ifndef NOCSETS
  8684.     "  /LOCAL-CHARACTER-SET:name",
  8685.     "    When downloading in text mode and character-set conversion is",
  8686.     "    desired, this specifies the target set.",
  8687. #endif /* NOCSETS */
  8688.     "  /MATCH:pattern",
  8689.     "    Specifies a pattern to be used to select filenames locally from the",
  8690.     "    server's list.",
  8691.     "  /MLSD",
  8692.     "    Forces sending of MLSD (rather than NLST) to get the file list.",
  8693. #ifdef CK_TMPDIR
  8694.     "  /MOVE-TO:directory",
  8695.     "    Each file that is downloaded is to be moved to the given local",
  8696.     "    directory immediately after, and only if, it has been received",
  8697.     "    successfully.",
  8698. #endif /* CK_TMPDIR */
  8699.     "  /NAMELIST:filename",
  8700.     "    Instead of downloading the files, stores the list of files that",
  8701.     "    would be downloaded in the given local file, one filename per line.",
  8702.     "  /NLST",
  8703.     "    Forces sending of NLST (rather than MLSD) to get the file list.",
  8704.     "  /NOBACKUPFILES",
  8705.     "    Don't download any files whose names end with .~<number>~.",
  8706.     "  /NODOTFILES",
  8707.     "    Don't download any files whose names begin with period (.).",
  8708.     "  /QUIET",
  8709.     "    Suppress the file-transfer display.",
  8710. #ifdef FTP_RESTART
  8711.     "  /RECOVER",                       /* /RESTART */
  8712.     "    Resume a download that was previously interrupted from the point of",
  8713.     "    failure.  Works only in binary mode.  Not supported by all servers.",
  8714.     "    Synonym: /RESTART.",
  8715. #endif /* FTP_RESTART */
  8716. #ifdef RECURSIVE
  8717.     "  /RECURSIVE",                     /* /SUBDIRECTORIES */
  8718.     "    Create subdirectories automatically if the server sends files",
  8719.     "    recursively and includes pathnames (most don't).",
  8720. #endif /* RECURSIVE */
  8721.     "  /RENAME-TO:text",
  8722.     "    Each file that is downloaded is to be renamed as indicated just,",
  8723.     "    after, and only if, it has arrived successfully.",
  8724. #ifndef NOCSETS
  8725.     "  /SERVER-CHARACTER-SET:name",
  8726.     "    When downloading in text mode and character-set conversion is desired"
  8727. ,   "    this specifies the original file's character set on the server.",
  8728. #endif /* NOCSETS */
  8729.     "  /SERVER-RENAME:text",
  8730.     "    Each server source file is to be renamed on the server as indicated",
  8731.     "    immediately after, but only if, it has arrived succesfully.",
  8732.     "  /SMALLER-THAN:number",
  8733.     "    Download only those files smaller than the given number of bytes.",
  8734.     "  /TEXT",                          /* /ASCII */
  8735.     "    Force text mode.  Synonym: /ASCII.",
  8736.     "  /TENEX",
  8737.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8738. #ifndef NOCSETS
  8739.     "  /TRANSPARENT",
  8740.     "    When downloading in text mode, do not convert chracter-sets.",
  8741. #endif /* NOCSETS */
  8742.     "  /TO-SCREEN",
  8743.     "    The downloaded file is to be displayed on the screen.",
  8744. #ifdef DOUPDATE
  8745.     "  /UPDATE",
  8746.     "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
  8747.     "    newer than than their local counterparts, or that do not exist on",
  8748.     "    the local computer.",
  8749. #endif /* DOUPDATE */
  8750.     ""
  8751. };
  8752. static char * fhs_hlp[] = {             /* HELP */
  8753.     "Syntax: FTP HELP [ command [ subcommand... ] ]",
  8754.     "  Asks the FTP server for help about the given command.  First use",
  8755.     "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
  8756.     "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
  8757.     ""
  8758. };
  8759. static char * fhs_idl[] = {             /* IDLE */
  8760.     "Syntax: FTP IDLE [ number ]",
  8761.     "  If given without a number, this asks the FTP server to tell its",
  8762.     "  current idle-time limit.  If given with a number, it asks the server",
  8763.     "  to change its idle-time limit to the given number of seconds.",
  8764.     ""
  8765. };
  8766. static char * fhs_usr[] = {             /* USER, LOGIN */
  8767.     "Syntax: FTP USER username [ password [ account ] ]",
  8768.     "  Log in to the FTP server.  To be used when connected but not yet",
  8769.     "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
  8770.     "  If you omit the password, and one is required by the server, you are",
  8771.     "  prompted for it.  If you omit the account, no account is sent.",
  8772.     "  Synonym: FTP LOGIN.",
  8773.     ""
  8774. };
  8775. static char * fhs_get[] = {             /* GET */
  8776.     "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
  8777.     "  Download a single file.  Asks the FTP server to send the given file.",
  8778.     "  The optional as-name is the name to store it under when it arrives;",
  8779.     "  if omitted, the file is stored with the name it arrived with, as",
  8780.     "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
  8781.     "  value.  Aside from the file list and as-name, syntax and options are",
  8782.     "  the same as for FTP MGET, which is used for downloading multiple files."
  8783. ,   ""
  8784. };
  8785. static char * fhs_mkd[] = {             /* MKDIR */
  8786.     "Syntax: FTP MKDIR directory",
  8787.     "  Asks the FTP server to create a directory with the given name,",
  8788.     "  which must be in the syntax of the server's file system.  Synonyms:",
  8789.     "  REMOTE MKDIR, RMKDIR.",
  8790.     ""
  8791. };
  8792. static char * fhs_mod[] = {             /* MODTIME */
  8793.     "Syntax: FTP MODTIME filename",
  8794.     "  Asks the FTP server to send the modification time of the given file,",
  8795.     "  to be displayed on the screen.  The date-time format is all numeric:",
  8796.     "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
  8797.     "  fractions of seconds).",
  8798.     ""
  8799. };
  8800. static char * fhs_mpu[] = {             /* MPUT */
  8801.     "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
  8802.     "  Uploads files.  Sends the given file or files to the FTP server.",
  8803.     "  Also see FTP PUT.  Optional switches are:",
  8804.     " ",
  8805.     "  /AFTER:date-time",
  8806.     "    Uploads only those files newer than the given date-time.",
  8807.     "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
  8808. #ifdef PUTARRAY
  8809.     "  /ARRAY:array-designator",
  8810.     "    Tells Kermit to upload the contents of the given array, rather than",
  8811.     "    a file.",
  8812. #endif /* PUTARRAY */
  8813.     "  /AS-NAME:text",
  8814.     "    Name under which to send files.",
  8815.     "    Pattern required for for multiple files.",
  8816.     "  /BEFORE:date-time",
  8817.     "    Upload only those files older than the given date-time.",
  8818.     "  /BINARY",
  8819.     "    Force binary mode.  Synonym: /IMAGE.",
  8820. #ifdef PUTPIPE
  8821.     "  /COMMAND",
  8822.     "    Specifies that the filespec is a command whose standard output is",
  8823.     "    to be sent.",
  8824. #endif /* PUTPIPE */
  8825.  
  8826. #ifdef COMMENT
  8827. #ifdef DOUPDATE
  8828.     "  /DATES-DIFFER",
  8829.     "    Upload only those files whose modification date-times differ from",
  8830.     "    those on the server, or that don't exist on the server at all.",
  8831. #endif /* DOUPDATE */
  8832. #endif /* COMMENT */
  8833.  
  8834.     "  /DELETE",
  8835.     "    Specifies that each source file is to be deleted after, and only if,",
  8836.     "    it is successfully uploaded.",
  8837.     "  /DOTFILES",
  8838.     "    Include files whose names begin with period (.).",
  8839.     "  /ERROR-ACTION:{PROCEED,QUIT}",
  8840.     "    When uploading a group of files, what to do upon failure to",
  8841.     "    transfer a file: quit or proceed to the next one.",
  8842.     "  /EXCEPT:pattern",
  8843.     "    Exception list: don't upload any files that match this pattern.",
  8844.     "    See HELP WILDCARD for pattern syntax.",
  8845.     "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
  8846.     "    Whether to convert outbound filenames to common syntax.",
  8847. #ifdef PIPESEND
  8848. #ifndef NOSPL
  8849.     "  /FILTER:command",
  8850.     "    Pass outbound files through the given command.",
  8851. #endif /* NOSPL */
  8852. #endif /* PIPESEND */
  8853. #ifdef CKSYMLINK
  8854.     "  /FOLLOWINKS",
  8855.     "    Send files that are pointed to by symbolic links.",
  8856.     "  /NOFOLLOWINKS",
  8857.     "    Skip over symbolic links (default).",
  8858. #endif /* CKSYMLINK */
  8859.     "  /LARGER-THAN:number",
  8860.     "    Only upload files that are larger than the given number of bytes.",
  8861.     "  /LISTFILE:filename",
  8862.     "    Obtain the list of files to upload from the given file.",
  8863. #ifndef NOCSETS
  8864.     "  /LOCAL-CHARACTER-SET:name",
  8865.     "    When uploading in text mode and character-set conversion is",
  8866.     "    desired, this specifies the source-file character set.",
  8867. #endif /* NOCSETS */
  8868. #ifdef CK_TMPDIR
  8869.     "  /MOVE-TO:directory",
  8870.     "    Each source file that is uploaded is to be moved to the given local",
  8871.     "    directory when, and only if, the transfer is successful.",
  8872. #endif /* CK_TMPDIR */
  8873.     "  /NOBACKUPFILES",
  8874.     "    Don't upload any files whose names end with .~<number>~.",
  8875. #ifdef UNIXOROSK
  8876.     "  /NODOTFILES",
  8877.     "    Don't upload any files whose names begin with period (.).",
  8878. #endif /* UNIXOROSK */
  8879.     "  /NOT-AFTER:date-time",
  8880.     "    Upload only files that are not newer than the given date-time",
  8881.     "  /NOT-BEFORE:date-time",
  8882.     "    Upload only files that are not older than the given date-time",
  8883. #ifdef UNIX
  8884.     "  /PERMISSIONS",
  8885.     "    Ask the server to set the permissions of each file it receives",
  8886.     "    according to the source file's permissions.",
  8887. #endif /* UNIX */
  8888.     "  /QUIET",
  8889.     "    Suppress the file-transfer display.",
  8890. #ifdef FTP_RESTART
  8891.     "  /RECOVER",
  8892.     "    Resume an upload that was previously interrupted from the point of",
  8893.     "    failure.  Synonym: /RESTART.",
  8894. #endif /* FTP_RESTART */
  8895. #ifdef RECURSIVE
  8896.     "  /RECURSIVE",
  8897.     "    Send files from the given directory and all the directories beneath",
  8898.     "    it.  Synonym: /SUBDIRECTORIES.",
  8899. #endif /* RECURSIVE */
  8900.     "  /RENAME-TO:text",
  8901.     "    Each source file that is uploaded is to be renamed on the local",
  8902.     "    local computer as indicated when and only if, the transfer completes",
  8903.     "    successfully.",
  8904. #ifndef NOCSETS
  8905.     "  /SERVER-CHARACTER-SET:name",
  8906.     "    When uploading in text mode and character-set conversion is desired,",
  8907.     "    this specifies the character set to which the file should be",
  8908.     "    converted for storage on the server.",
  8909. #endif /* NOCSETS */
  8910.     "  /SERVER-RENAME:text",
  8911.     "    Each file that is uploaded is to be renamed as indicated on the",
  8912.     "    server after, and only if, if arrives successfully.",
  8913.     "  /SIMULATE",
  8914.     "    Show which files would be sent without actually sending them.",
  8915.     "  /SMALLER-THAN:number",
  8916.     "    Upload only those files smaller than the given number of bytes.",
  8917.     "  /TEXT",
  8918.     "    Force text mode.  Synonym: /ASCII.",
  8919.     "  /TENEX",
  8920.     "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
  8921. #ifndef NOCSETS
  8922.     "  /TRANSPARENT",
  8923.     "    When uploading in text mode, do not convert chracter-sets.",
  8924. #endif /* NOCSETS */
  8925.     "  /TYPE:{TEXT,BINARY}",
  8926.     "    Upload only files of the given type.",
  8927. #ifdef DOUPDATE
  8928.     "  /UPDATE",
  8929.     "    If a file of the same name exists on the server, upload only if",
  8930.     "    the local file is newer.",
  8931. #endif /* DOUPDATE */
  8932.     "  /UNIQUE-SERVER-NAMES",
  8933.     "    Ask the server to compute new names for any incoming file that has",
  8934.     "    the same name as an existing file.",
  8935.     ""
  8936. };
  8937. static char * fhs_opn[] = {             /* OPEN */
  8938. #ifdef CK_SSL
  8939.     "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
  8940.     "  Opens a connection to the FTP server on the given host.  The default",
  8941.     "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
  8942.     "  can be supplied if necessary.  Optional switches are:",
  8943. #else /* CK_SSL */
  8944.     "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
  8945.     "  Opens a connection to the FTP server on the given host.  The default",
  8946.     "  TCP port is 21, but a different port number can be supplied if",
  8947.     "  necessary.  Optional switches are:",
  8948. #endif /* CK_SSL */
  8949.     " ",
  8950.     "  /ANONYMOUS",
  8951.     "    Logs you in anonymously.",
  8952.     "  /USER:text",
  8953.     "    Supplies the given text as your username.",
  8954.     "  /PASSWORD:text",
  8955.     "    Supplies the given text as your password.  If you include a username",
  8956.     "    but omit this switch and the server requires a password, you are",
  8957.     "    prompted for it.",
  8958.     "  /ACCOUNT:text",
  8959.     "    Supplies the given text as your account, if required by the server.",
  8960.     "  /ACTIVE",
  8961.     "    Forces an active (rather than passive) connection.",
  8962.     "  /PASSIVE",
  8963.     "    Forces a passive (rather than active) connection.",
  8964.     "  /NOINIT",
  8965.     "    Inhibits sending initial REST, STRU, and MODE commands, which are",
  8966.     "    well-known standard commands, but to which some servers react badly.",
  8967.     "  /NOLOGIN",
  8968.     "    Inhibits autologin for this connection only.",
  8969.     ""
  8970. };
  8971. static char * fhs_opt[] = {             /* OPTS, OPTIONS */
  8972.     "Syntax: FTP OPTIONS",
  8973.     "  Asks the FTP server to list its current options.  Advanced, new,",
  8974.     "  not supported by most FTP servers.",
  8975.     ""
  8976. };
  8977. static char * fhs_put[] = {             /* PUT, SEND */
  8978.     "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
  8979.     "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
  8980.     "  by an additional field, this is interpreted as the name under which",
  8981.     "  to send the file or files.  See HELP FTP MPUT.",
  8982.     ""
  8983. };
  8984. static char * fhs_pwd[] = {             /* PWD */
  8985.     "Syntax: FTP PWD",
  8986.     "  Asks the FTP server to reveal its current working directory.",
  8987.     "  Synonyms: REMOTE PWD, RPWD.",
  8988.     ""
  8989. };
  8990. static char * fhs_quo[] = {             /* QUOTE */
  8991.     "Syntax: FTP QUOTE text",
  8992.     "  Sends an FTP protocol command to the FTP server.  Use this command",
  8993.     "  for sending commands that Kermit might not support.",
  8994.     ""
  8995. };
  8996. static char * fhs_rge[] = {             /* REGET */
  8997.     "Syntax: FTP REGET",
  8998.     "  Synonym for FTP GET /RECOVER.",
  8999.     ""
  9000. };
  9001. static char * fhs_ren[] = {             /* RENAME */
  9002.     "Syntax: FTP RENAME name1 name1",
  9003.     "  Asks the FTP server to change the name of the file whose name is name1",
  9004.     "  and which resides in the FTP server's file system, to name2.  Works",
  9005.     "  only for single files; wildcards are not accepted.",
  9006.     ""
  9007. };
  9008. static char * fhs_res[] = {             /* RESET */
  9009.     "Syntax: FTP RESET",
  9010.     "  Asks the server to log out your session, terminating your access",
  9011.     "  rights, without closing the connection.",
  9012.     ""
  9013. };
  9014. static char * fhs_rmd[] = {             /* RMDIR */
  9015.     "Syntax: FTP RMDIR directory",
  9016.     "  Asks the FTP server to remove the directory whose name is given.",
  9017.     "  This usually requires the directory to be empty.  Synonyms: REMOTE",
  9018.     "  RMDIR, RRMDIR.",
  9019.     ""
  9020. };
  9021. static char * fhs_sit[] = {             /* SITE */
  9022.     "Syntax: FTP SITE text",
  9023.     "  Sends a site-specific command to the FTP server.",
  9024.     ""
  9025. };
  9026. static char * fhs_siz[] = {             /* SIZE */
  9027.     "Syntax: FTP SIZE filename",
  9028.     "  Asks the FTP server to send a numeric string representing the size",
  9029.     "  of the given file.",
  9030.     ""
  9031. };
  9032. static char * fhs_sta[] = {             /* STATUS */
  9033.     "Syntax: FTP STATUS [ filename ]",
  9034.     "  Asks the FTP server to report its status.  If a filename is given,",
  9035.     "  the FTP server should report details about the file.",
  9036.     ""
  9037. };
  9038. static char * fhs_sys[] = {             /* SYSTEM */
  9039.     "Syntax: FTP SYSTEM",
  9040.     "  Asks the FTP server to report its operating system type.",
  9041.     ""
  9042. };
  9043. static char * fhs_typ[] = {             /* TYPE */
  9044.     "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
  9045.     "  Puts the client and server in the indicated transfer mode.",
  9046.     "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
  9047.     "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
  9048.     "  downloading files from TENEX or TOPS-20 that have been uploaded in",
  9049.     "  TENEX mode.",
  9050.     ""
  9051. };
  9052. static char * fhs_uma[] = {             /* UMASK */
  9053.     "Syntax: FTP UMASK number",
  9054.     "  Asks the FTP server to set its file creation mode mask.  Applies",
  9055.     "  only (or mainly) to UNIX-based FTP servers.",
  9056.     ""
  9057. };
  9058. static char * fhs_chk[] = {             /* CHECK */
  9059.     "Syntax: FTP CHECK remote-filespec",
  9060.     "  Asks the FTP server if the given file or files exist.  If the",
  9061.     "  remote-filespec contains wildcards, this command fails if no server",
  9062.     "  files match, and succeeds if at least one file matches.  If the",
  9063.     "  remote-filespec does not contain wildcards, this command succeeds if",
  9064.     "  the given file exists and fails if it does not.",
  9065.     ""
  9066. };
  9067. static char * fhs_ena[] = {        /* ENABLE */
  9068.     "Syntax: FTP ENABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  9069.     "  Enables the use of the given FTP protocol command in case it has been",
  9070.     "  disabled (but this is no guarantee that the FTP server understands it)."
  9071. ,
  9072.     "  Use SHOW FTP to see which of these commands is enabled and disabled.",
  9073.     "  Also see FTP DISABLE.",
  9074.     ""
  9075. };
  9076. static char * fhs_dis[] = {        /* DISABLE */
  9077.     "Syntax: FTP DISABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
  9078.     "  Disables the use of the given FTP protocol command.",
  9079.     "  Also see FTP ENABLE.",
  9080.     ""
  9081. };
  9082.  
  9083. #endif /* NOHELP */
  9084.  
  9085. int
  9086. doftphlp() {
  9087.     int cx;
  9088.     if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
  9089.       if (cx != -3)
  9090.         return(cx);
  9091.     if ((x = cmcfm()) < 0)
  9092.       return(x);
  9093.  
  9094. #ifdef NOHELP
  9095.     printf("Sorry, no help available\n");
  9096. #else
  9097.     switch (cx) {
  9098.       case -3:
  9099.         return(hmsga(fhs_ftp));
  9100.       case FTP_ACC:                     /* ACCOUNT */
  9101.         return(hmsga(fhs_acc));
  9102.       case FTP_APP:                     /* APPEND */
  9103.         return(hmsga(fhs_app));
  9104.       case FTP_CLS:                     /* BYE, CLOSE */
  9105.         return(hmsga(fhs_cls));
  9106.       case FTP_CWD:                     /* CD, CWD */
  9107.         return(hmsga(fhs_cwd));
  9108.       case FTP_GUP:                     /* CDUP, UP */
  9109.         return(hmsga(fhs_gup));
  9110.       case FTP_CHM:                     /* CHMOD */
  9111.         return(hmsga(fhs_chm));
  9112.       case FTP_MDE:                     /* DELETE, MDELETE */
  9113.         return(hmsga(fhs_mde));
  9114.       case FTP_DIR:                     /* DIRECTORY */
  9115.         return(hmsga(fhs_dir));
  9116.       case FTP_VDI:                     /* VDIRECTORY */
  9117.         return(hmsga(fhs_vdi));
  9118.       case FTP_FEA:                     /* FEATURES */
  9119.         return(hmsga(fhs_fea));
  9120.       case FTP_GET:                     /* GET */
  9121.         return(hmsga(fhs_get));
  9122.       case FTP_HLP:                     /* HELP */
  9123.         return(hmsga(fhs_hlp));
  9124.       case FTP_IDL:                     /* IDLE */
  9125.         return(hmsga(fhs_idl));
  9126.       case FTP_USR:                     /* USER, LOGIN */
  9127.         return(hmsga(fhs_usr));
  9128.       case FTP_MGE:                     /* MGET */
  9129.         return(hmsga(fhs_mge));
  9130.       case FTP_MKD:                     /* MKDIR */
  9131.         return(hmsga(fhs_mkd));
  9132.       case FTP_MOD:                     /* MODTIME */
  9133.         return(hmsga(fhs_mod));
  9134.       case FTP_MPU:                     /* MPUT */
  9135.         return(hmsga(fhs_mpu));
  9136.       case FTP_OPN:                     /* OPEN */
  9137.         return(hmsga(fhs_opn));
  9138.       case FTP_OPT:                     /* OPTS, OPTIONS */
  9139.         return(hmsga(fhs_opt));
  9140.       case FTP_PUT:                     /* PUT, SEND */
  9141.         return(hmsga(fhs_put));
  9142.       case FTP_PWD:                     /* PWD */
  9143.         return(hmsga(fhs_pwd));
  9144.       case FTP_QUO:                     /* QUOTE */
  9145.         return(hmsga(fhs_quo));
  9146.       case FTP_RGE:                     /* REGET */
  9147.         return(hmsga(fhs_rge));
  9148.       case FTP_REN:                     /* RENAME */
  9149.         return(hmsga(fhs_ren));
  9150.       case FTP_RES:                     /* RESET */
  9151.         return(hmsga(fhs_res));
  9152.       case FTP_RMD:                     /* RMDIR */
  9153.         return(hmsga(fhs_rmd));
  9154.       case FTP_SIT:                     /* SITE */
  9155.         return(hmsga(fhs_sit));
  9156.       case FTP_SIZ:                     /* SIZE */
  9157.         return(hmsga(fhs_siz));
  9158.       case FTP_STA:                     /* STATUS */
  9159.         return(hmsga(fhs_sta));
  9160.       case FTP_SYS:                     /* SYSTEM */
  9161.         return(hmsga(fhs_sys));
  9162.       case FTP_TYP:                     /* TYPE */
  9163.         return(hmsga(fhs_typ));
  9164.       case FTP_UMA:                     /* UMASK */
  9165.         return(hmsga(fhs_uma));
  9166.       case FTP_CHK:                     /* CHECK */
  9167.         return(hmsga(fhs_chk));
  9168.       case FTP_ENA:
  9169.         return(hmsga(fhs_ena));
  9170.       case FTP_DIS:
  9171.         return(hmsga(fhs_dis));
  9172.       default:
  9173.         printf("Sorry, help available for this command.\n");
  9174.         break;
  9175.     }
  9176. #endif /* NOHELP */
  9177.     return(success = 0);
  9178. }
  9179.  
  9180. int
  9181. dosetftphlp() {
  9182.     int cx;
  9183.     if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
  9184.       if (cx != -3)
  9185.         return(cx);
  9186.     if (cx != -3)
  9187.       ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
  9188.     if ((x = cmcfm()) < 0)
  9189.       return(x);
  9190.  
  9191. #ifdef NOHELP
  9192.     printf("Sorry, no help available\n");
  9193. #else
  9194.     switch (cx) {
  9195.       case -3:
  9196.         printf("\nSyntax: SET FTP parameter value\n");
  9197.         printf("  Type \"help set ftp ?\" for a list of parameters.\n");
  9198.         printf("  Type \"help set ftp xxx\" for information about setting\n");
  9199.         printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
  9200.         return(0);
  9201.  
  9202.       case FTS_BUG:
  9203.     printf("\nSyntax: SET FTP BUG <name> {ON, OFF}\n");
  9204.     printf(
  9205.         "  Activates a workaround for the named bug in the FTP server.\n");
  9206.     printf("  Type SET FTP BUG ? for a list of names.\n");
  9207.     printf("  For each bug, the default is OFF\n\n");
  9208.     return(0);
  9209.  
  9210. #ifdef FTP_SECURITY
  9211.       case FTS_ATP:                     /* "authtype" */
  9212.         printf("\nSyntax: SET FTP AUTHTYPE list\n");
  9213.         printf("  Specifies an ordered list of authentication methods to be\n"
  9214.                );
  9215.         printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
  9216.         printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
  9217.         return(0);
  9218.  
  9219.       case FTS_AUT:                     /* "autoauthentication" */
  9220.         printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
  9221.         printf("  Tells whether authentication should be negotiated by the\n");
  9222.         printf("  FTP OPEN command.  Default is ON.\n\n");
  9223.         break;
  9224.  
  9225.       case FTS_CRY:                     /* "autoencryption" */
  9226.         printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
  9227.         printf("  Tells whether encryption (privacy) should be negotiated\n");
  9228.         printf("  by the FTP OPEN command.  Default is ON.\n\n");
  9229.         break;
  9230. #endif /* FTP_SECURITY */
  9231.  
  9232.       case FTS_LOG:                     /* "autologin" */
  9233.         printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
  9234.         printf("  Tells Kermit whether to try to log you in automatically\n");
  9235.         printf("  as part of the connection process.\n\n");
  9236.         break;
  9237.  
  9238.       case FTS_DIS:
  9239.         printf("\nSET FTP DISPLAY { BRIEF, FULLSCREEN, CRT, ... }\n");
  9240.     printf("  Chooses the file-transfer display style for FTP.\n");
  9241.         printf("  Like SET TRANSFER DISPLAY but applies only to FTP.\n\n");
  9242.         break;
  9243.  
  9244. #ifndef NOCSETS
  9245.       case FTS_XLA:                     /* "character-set-translation" */
  9246.         printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
  9247.         printf("  Whether to translate character sets when transferring\n");
  9248.         printf("  text files with FTP.  OFF by default.\n\n");
  9249.         break;
  9250.  
  9251. #endif /* NOCSETS */
  9252.       case FTS_FNC:                     /* "collision" */
  9253.         printf("\n");
  9254.         printf(
  9255. "Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
  9256.                );
  9257.         printf("  Tells what do when an incoming file has the same name as\n");
  9258.         printf("  an existing file when downloading with FTP.\n\n");
  9259.         break;
  9260.  
  9261. #ifdef FTP_SECURITY
  9262.       case FTS_CPL:                     /* "command-protection-level" */
  9263.         printf("\n");
  9264.         printf(
  9265. "Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9266.                );
  9267.         printf("\n");
  9268.         printf(
  9269. "  Tells what level of protection is applied to the FTP command channel.\n\n");
  9270.         break;
  9271.       case FTS_CFW:                     /* "credential-forwarding" */
  9272.         printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
  9273.         printf("  Tells whether end-user credentials are to be forwarded\n");
  9274.         printf("  to the server if supported by the authentication method\n");
  9275.         printf("  (GSSAPI-KRB5 only).\n\n");
  9276.         break;
  9277.       case FTS_DPL:                     /* "data-protection-level" */
  9278.         printf("\n");
  9279.         printf(
  9280. "Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
  9281.                );
  9282.         printf("\n");
  9283.         printf(
  9284. "  Tells what level of protection is applied to the FTP data channel.\n\n");
  9285.         break;
  9286. #endif /* FTP_SECURITY */
  9287.  
  9288.       case FTS_DBG:                     /* "debug" */
  9289.         printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
  9290.         printf("  Whether to print FTP protocol messages.\n\n");
  9291.         return(0);
  9292.  
  9293.       case FTS_ERR:                     /* "error-action" */
  9294.         printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
  9295.         printf("  What to do when an error occurs when transferring a group\n")
  9296.           ;
  9297.         printf("  of files: quit and fail, or proceed to the next file.\n\n");
  9298.         return(0);
  9299.  
  9300.       case FTS_CNV:                     /* "filenames" */
  9301.         printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
  9302.         printf("  What to do with filenames: convert them, take and use them\n"
  9303.                );
  9304.         printf("  literally; or choose what to do automatically based on the\n"
  9305.                );
  9306.         printf("  OS type of the server.  The default is AUTO.\n\n");
  9307.         return(0);
  9308.  
  9309.       case FTS_PSV:                     /* "passive-mode" */
  9310.         printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
  9311.         printf("  Whether to use passive mode, which helps to get through\n");
  9312.         printf("  firewalls.  ON by default.\n\n");
  9313.         return(0);
  9314.  
  9315.       case FTS_PRM:                     /* "permissions" */
  9316.         printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
  9317.         printf("  Whether to try to send file permissions when uploading.\n");
  9318.         printf("  OFF by default.  AUTO means only if client and server\n");
  9319.         printf("  have the same OS type.\n\n");
  9320.         return(0);
  9321.  
  9322.       case FTS_TST:                     /* "progress-messages" */
  9323.         printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
  9324.         printf("  Whether Kermit should print locally-generated feedback\n");
  9325.         printf("  messages for each non-file-transfer command.");
  9326.         printf("  ON by default.\n\n");
  9327.         return(0);
  9328.  
  9329.       case FTS_SPC:                     /* "send-port-commands" */
  9330.         printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
  9331.         printf("  Whether Kermit should send a new PORT command for each");
  9332.         printf("  task.\n\n");
  9333.         return(0);
  9334.  
  9335. #ifndef NOCSETS
  9336.       case FTS_CSR:                     /* "server-character-set" */
  9337.         printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
  9338.         printf("  The name of the character set used for text files on the\n");
  9339.         printf("  server.  Enter a name of '?' for a menu.\n\n");
  9340.         return(0);
  9341. #endif /* NOCSETS */
  9342.  
  9343.       case FTS_STO:            /* "server-time-offset */
  9344.     printf(
  9345. "\nSyntax: SET FTP SERVER-TIME-OFFSET +hh[:mm[:ss]] or -hh[:mm[:ss]]\n");
  9346.         printf(
  9347. "  Specifies an offset to apply to the server's file timestamps.\n");
  9348.         printf(
  9349. "  Use this to correct for misconfigured server time or timezone.\n");
  9350.         printf(
  9351. "  Format: must begin with + or - sign.  Hours must be given; minutes\n");
  9352.         printf(
  9353. "  and seconds are optional: +4 = +4:00 = +4:00:00 (add 4 hours).\n\n");
  9354.         return(0);
  9355.  
  9356.       case FTS_TYP:                     /* "type" */
  9357.         printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
  9358.         printf("  Establishes the default transfer mode.\n");
  9359.         printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
  9360.         printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
  9361.         printf("  them again.\n\n");
  9362.         return(0);
  9363.  
  9364. #ifdef PATTERNS
  9365.       case FTS_GFT:
  9366.         printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
  9367.         printf("  Tells whether GET and MGET should automatically switch\n");
  9368.         printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
  9369.         printf("  matching the name of each incoming file with its list of\n");
  9370.         printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
  9371.         printf("  default.  SHOW PATTERNS displays the current pattern\n");
  9372.         printf("  list.  HELP SET FILE to see how to change it.\n");
  9373.         break;
  9374. #endif /* PATTERNS */
  9375.  
  9376.       case FTS_USN:                     /* "unique-server-names" */
  9377.         printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
  9378.         printf("  Tells whether to ask the server to create unique names\n");
  9379.         printf("  for any uploaded file that has the same name as an\n");
  9380.         printf("  existing file.  Default is OFF.\n\n");
  9381.         return(0);
  9382.  
  9383.       case FTS_VBM:                     /* "verbose-mode" */
  9384.         printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
  9385.         printf("  Whether to display all responses from the FTP server.\n");
  9386.         printf("  OFF by default.\n\n");
  9387.         return(0);
  9388.  
  9389.       case FTS_DAT:
  9390.         printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
  9391.         printf("  Whether to set date of incoming files from the file date\n");
  9392.         printf("  on the server.  ON by default.  Note: there is no way to\n")
  9393.           ;
  9394.         printf("  set the date on files uploaded to the server.  Also note\n");
  9395.     printf("  that not all servers support this feature.\n\n");
  9396.         return(0);
  9397.  
  9398.       case FTS_APW:
  9399.     printf("\nSyntax: SET FTP ANONYMOUS-PASSWORD [ text ]\n");
  9400.     printf("  Password to supply automatically on anonymous FTP\n");
  9401.     printf("  connections instead of the default user@host.\n");
  9402.     printf("  Omit optional text to restore default.\n\n");
  9403.     return(0);
  9404.  
  9405.       default:
  9406.         printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
  9407.     }
  9408. #endif /* NOHELP */
  9409.     return(0);
  9410. }
  9411.  
  9412. #ifndef L_SET
  9413. #define L_SET 0
  9414. #endif /* L_SET */
  9415. #ifndef L_INCR
  9416. #define L_INCR 1
  9417. #endif /* L_INCR */
  9418.  
  9419. #ifdef FTP_SRP
  9420. char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
  9421. char *srp_pass;
  9422. char *srp_acct;
  9423. #endif /* FTP_SRP */
  9424.  
  9425. static int kerror;                      /* Needed for all auth types */
  9426.  
  9427. static struct   sockaddr_in hisctladdr;
  9428. static struct   sockaddr_in hisdataaddr;
  9429. static struct   sockaddr_in data_addr;
  9430. static int      data = -1;
  9431. static int      ptflag = 0;
  9432. static struct   sockaddr_in myctladdr;
  9433.  
  9434. #ifdef COMMENT
  9435. #ifndef OS2
  9436. UID_T getuid();
  9437. #endif /* OS2 */
  9438. #endif /* COMMENT */
  9439.  
  9440.  
  9441. static int cpend = 0;                   /* No pending replies */
  9442.  
  9443. #ifdef CK_SSL
  9444. extern SSL *ssl_ftp_con;
  9445. extern SSL_CTX *ssl_ftp_ctx;
  9446. extern SSL *ssl_ftp_data_con;
  9447. extern int ssl_ftp_active_flag;
  9448. extern int ssl_ftp_data_active_flag;
  9449. #endif /* CK_SSL */
  9450.  
  9451. /*  f t p c m d  --  Send a command to the FTP server  */
  9452. /*
  9453.   Call with:
  9454.     char * cmd: The command to send.
  9455.     char * arg: The argument (e.g. a filename).
  9456.     int lcs: The local character set index.
  9457.     int rcs: The remote (server) character set index.
  9458.     int vbm: Verbose mode:
  9459.       0 = force verbosity off
  9460.      >0 = force verbosity on
  9461.  
  9462.   If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  9463.   and neither lcs or rcs is UCS-2, the arg is translated from the local
  9464.   character set to the remote one before sending the result to the server.
  9465.  
  9466.    Returns:
  9467.     0 on failure with ftpcode = -1
  9468.     >= 0 on success (getreply() result) with ftpcode = 0.
  9469. */
  9470. static char xcmdbuf[RFNBUFSIZ];
  9471.  
  9472. static int
  9473. ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
  9474.     char * s = NULL;
  9475.     int r = 0, x = 0, fc = 0, len = 0, cmdlen = 0, q = -1;
  9476.     sig_t oldintr;
  9477.  
  9478.     if (ftp_deb)                        /* DEBUG */
  9479.       vbm = 1;
  9480.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  9481.       vbm = 0;
  9482.     else if (vbm < 0)                   /* VERBOSE */
  9483.       vbm = ftp_vbm;
  9484.  
  9485.     cancelfile = 0;
  9486.     if (!cmd) cmd = "";
  9487.     if (!arg) arg = "";
  9488.     cmdlen = (int)strlen(cmd);
  9489.     len = cmdlen + (int)strlen(arg) + 1;
  9490.  
  9491.     if (ftp_deb /* && !dpyactive */ ) {
  9492. #ifdef FTP_PROXY
  9493.         if (ftp_prx) printf("%s ", ftp_host);
  9494. #endif /* FTP_PROXY */
  9495.         printf("---> ");
  9496.         if (!anonymous && strcmp("PASS",cmd) == 0)
  9497.           printf("PASS XXXX");
  9498.         else
  9499.           printf("%s %s",cmd,arg);
  9500.         printf("\n");
  9501.     }
  9502.     /* bzero(xcmdbuf,RFNBUFSIZ); */
  9503.     ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
  9504.  
  9505. #ifdef DEBUG
  9506.     if (deblog) {
  9507.         debug(F110,"ftpcmd cmd",cmd,0);
  9508.         debug(F110,"ftpcmd arg",arg,0);
  9509.         debug(F101,"ftpcmd lcs","",lcs);
  9510.         debug(F101,"ftpcmd rcs","",rcs);
  9511.     }
  9512. #endif /* DEBUG */
  9513.  
  9514.     if (csocket == -1) {
  9515.         perror("No control connection for command");
  9516.         ftpcode = -1;
  9517.         return(0);
  9518.     }
  9519.     havesigint = 0;
  9520.     oldintr = signal(SIGINT, cmdcancel);
  9521.  
  9522. #ifndef NOCSETS
  9523.     if (*arg &&                         /* If an arg was given */
  9524.         lcs > -1 &&                     /* and a local charset */
  9525.         rcs > -1 &&                     /* and a remote charset */
  9526.         lcs != rcs &&                   /* and the two are not the same */
  9527.         lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
  9528.         rcs != FC_UCS2                  /* ... */
  9529.         ) {
  9530.         initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
  9531.         xgnbp = arg;                    /* Global pointer to input string */
  9532.         rfnptr = rfnbuf;                /* Global pointer to output buffer */
  9533.  
  9534.         while (1) {
  9535.             if ((c0 = xgnbyte(FC_UCS2,lcs,strgetc)) < 0) break;
  9536.             if (xpnbyte(c0,TC_UCS2,rcs,strputc) < 0) break;
  9537.         }
  9538.         /*
  9539.           We have to copy here instead of translating directly into
  9540.           xcmdbuf[] so strputc() can check length.  Alternatively we could
  9541.           write yet another xpnbyte() output function.
  9542.         */
  9543.         if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
  9544.             printf("?FTP command too long: %s + arg\n",cmd);
  9545.             ftpcode = -1;
  9546.             return(0);
  9547.         }
  9548.         x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
  9549.     }
  9550. #endif /* NOCSETS */
  9551.  
  9552.     s = xcmdbuf;                        /* Command to send to server */
  9553.  
  9554. #ifdef DEBUG
  9555.     if (deblog) {            /* Log it */
  9556.     if (!anonymous && !ckstrcmp(s,"PASS ",5,0)) {
  9557.         /* But don't log passwords */
  9558.         debug(F110,"FTP SENT ","PASS XXXX",0);
  9559.     } else {
  9560.         debug(F110,"FTP SENT ",s,0);
  9561.     }
  9562.     }
  9563. #endif /* DEBUG */
  9564.  
  9565. #ifdef CK_ENCRYPTION
  9566.   again:
  9567. #endif /* CK_ENCRYPTION */
  9568.     if (scommand(s) == 0) {              /* Send it. */
  9569.       signal(SIGINT, oldintr);
  9570.       return(0);
  9571.     }
  9572.     cpend = 1;
  9573.     x = !strcmp(cmd,"QUIT");        /* Is it the QUIT command? */
  9574.     if (x)                /* In case we're interrupted */
  9575.       connected = 0;            /* while waiting for the reply... */
  9576.  
  9577.     fc = 0;                /* Function code for getreply() */
  9578.     if (!strncmp(cmd,"AUTH ",5)        /* Must parse AUTH reply */
  9579. #ifdef FTPHOST
  9580.     && strncmp(cmd, "HOST ",5)
  9581. #endif /* FTPHOST */
  9582.     ) {
  9583.     fc = GRF_AUTH;
  9584.     } else if (!ckstrcmp(cmd,"FEAT",-1,0)) { /* Must parse FEAT reply */
  9585.     fc = GRF_FEAT;            /* But FEAT not widely understood */
  9586.     if (!ftp_deb)            /* So suppress error messages */
  9587.       vbm = 9;
  9588.     }
  9589.     r = getreply(x,            /* Expect connection to close */
  9590.          lcs,rcs,        /* Charsets */
  9591.          vbm,            /* Verbosity */
  9592.          fc            /* Function code */
  9593.          );
  9594.     if (q > -1)
  9595.       quiet = q;
  9596.  
  9597. #ifdef CK_ENCRYPTION
  9598.     if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
  9599.         fprintf(stderr,
  9600.                "ENC command not supported at server; retrying under MIC...\n");
  9601.         ftp_cpl = FPL_SAF;
  9602.         goto again;
  9603.     }
  9604. #endif /* CK_ENCRYPTION */
  9605. #ifdef COMMENT
  9606.     if (cancelfile && oldintr != SIG_IGN)
  9607.       (*oldintr)(SIGINT);
  9608. #endif /* COMMENT */
  9609.     signal(SIGINT, oldintr);
  9610.     return(r);
  9611. }
  9612.  
  9613. static VOID
  9614. lostpeer() {
  9615.     debug(F100,"lostpeer","",0);
  9616.     if (connected) {
  9617.         if (csocket != -1) {
  9618. #ifdef CK_SSL
  9619.             if (ssl_ftp_active_flag) {
  9620.                 SSL_shutdown(ssl_ftp_con);
  9621.                 SSL_free(ssl_ftp_con);
  9622.                 ssl_ftp_proxy = 0;
  9623.                 ssl_ftp_active_flag = 0;
  9624.                 ssl_ftp_con = NULL;
  9625.             }
  9626. #endif /* CK_SSL */
  9627. #ifdef TCPIPLIB
  9628.             socket_close(csocket);
  9629. #else /* TCPIPLIB */
  9630. #ifdef USE_SHUTDOWN
  9631.             shutdown(csocket, 1+1);
  9632. #endif /* USE_SHUTDOWN */
  9633.             close(csocket);
  9634. #endif /* TCPIPLIB */
  9635.             csocket = -1;
  9636.         }
  9637.         if (data != -1) {
  9638. #ifdef CK_SSL
  9639.             if (ssl_ftp_data_active_flag) {
  9640.                 SSL_shutdown(ssl_ftp_data_con);
  9641.                 SSL_free(ssl_ftp_data_con);
  9642.                 ssl_ftp_data_active_flag = 0;
  9643.                 ssl_ftp_data_con = NULL;
  9644.             }
  9645. #endif /* CK_SSL */
  9646. #ifdef TCPIPLIB
  9647.             socket_close(data);
  9648. #else /* TCPIPLIB */
  9649. #ifdef USE_SHUTDOWN
  9650.             shutdown(data, 1+1);
  9651. #endif /* USE_SHUTDOWN */
  9652.             close(data);
  9653. #endif /* TCPIPLIB */
  9654.             data = -1;
  9655.             globaldin = -1;
  9656.         }
  9657.         connected = 0;
  9658.         anonymous = 0;
  9659.         loggedin = 0;
  9660.         auth_type = NULL;
  9661.         ftp_cpl = ftp_dpl = FPL_CLR;
  9662. #ifdef CKLOGDIAL
  9663.         ftplogend();
  9664. #endif /* CKLOGDIAL */
  9665.  
  9666. #ifdef LOCUS
  9667.     if (autolocus)            /* Auotomatic locus switching... */
  9668.       setlocus(1,1);        /* Switch locus to local. */
  9669. #endif /* LOCUS */
  9670. #ifdef OS2
  9671.         DialerSend(OPT_KERMIT_HANGUP, 0);
  9672. #endif /* OS2 */
  9673.     }
  9674. #ifdef FTP_PROXY
  9675.     pswitch(1);
  9676.     if (connected) {
  9677.         if (csocket != -1) {
  9678. #ifdef TCPIPLIB
  9679.             socket_close(csocket);
  9680. #else /* TCPIPLIB */
  9681. #ifdef USE_SHUTDOWN
  9682.             shutdown(csocket, 1+1);
  9683. #endif /* USE_SHUTDOWN */
  9684.             close(csocket);
  9685. #endif /* TCPIPLIB */
  9686.             csocket = -1;
  9687.         }
  9688.         connected = 0;
  9689.         anonymous = 0;
  9690.         loggedin = 0;
  9691.         auth_type = NULL;
  9692.         ftp_cpl = ftp_dpl = FPL_CLR;
  9693.     }
  9694.     proxflag = 0;
  9695.     pswitch(0);
  9696. #endif /* FTP_PROXY */
  9697. }
  9698.  
  9699. int
  9700. ftpisopen() {
  9701.     return(connected);
  9702. }
  9703.  
  9704. static int
  9705. ftpclose() {
  9706.     extern int quitting;
  9707.     if (!connected)
  9708.       return(0);
  9709.     if (!ftp_vbm && !quiet) printlines = 1;
  9710.     ftpcmd("QUIT",NULL,0,0,ftp_vbm);
  9711.     if (csocket) {
  9712. #ifdef CK_SSL
  9713.         if (ssl_ftp_active_flag) {
  9714.             SSL_shutdown(ssl_ftp_con);
  9715.             SSL_free(ssl_ftp_con);
  9716.             ssl_ftp_proxy = 0;
  9717.             ssl_ftp_active_flag = 0;
  9718.             ssl_ftp_con = NULL;
  9719.         }
  9720. #endif /* CK_SSL */
  9721. #ifdef TCPIPLIB
  9722.         socket_close(csocket);
  9723. #else /* TCPIPLIB */
  9724. #ifdef USE_SHUTDOWN
  9725.         shutdown(csocket, 1+1);
  9726. #endif /* USE_SHUTDOWN */
  9727.         close(csocket);
  9728. #endif /* TCPIPLIB */
  9729.     }
  9730.     csocket = -1;
  9731.     connected = 0;
  9732.     anonymous = 0;
  9733.     loggedin = 0;
  9734.     mdtmok = 1;
  9735.     sizeok = 1;
  9736.     featok = 1;
  9737.     stouarg = 1;
  9738.     typesent = 0;
  9739.     data = -1;
  9740.     globaldin = -1;
  9741. #ifdef FTP_PROXY
  9742.     if (!proxy)
  9743.       macnum = 0;
  9744. #endif /* FTP_PROXY */
  9745.     auth_type = NULL;
  9746.     ftp_dpl = FPL_CLR;
  9747. #ifdef CKLOGDIAL
  9748.     ftplogend();
  9749. #endif /* CKLOGDIAL */
  9750. #ifdef LOCUS
  9751.     /* Unprefixed file management commands are executed locally */
  9752.     if (autolocus && !ftp_cmdlin && !quitting) {
  9753.         setlocus(1,1);
  9754.     }
  9755. #endif /* LOCUS */
  9756. #ifdef OS2
  9757.     DialerSend(OPT_KERMIT_HANGUP, 0);
  9758. #endif /* OS2 */
  9759.     return(0);
  9760. }
  9761.  
  9762. int
  9763. ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
  9764.     char * host;
  9765.  
  9766.     if (connected) {
  9767.         printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
  9768.         ftpcode = -1;
  9769.         return(0);
  9770.     }
  9771. #ifdef FTPHOST
  9772.     hostcmd = 0;
  9773. #endif /* FTPHOST */
  9774.     alike = 0;
  9775.     ftp_srvtyp[0] = NUL;
  9776.     if (!service) service = "";
  9777.     if (!*service) service = use_tls ? "ftps" : "ftp";
  9778.  
  9779.     if (!isdigit(service[0])) {
  9780.         struct servent *destsp;
  9781.         destsp = getservbyname(service, "tcp");
  9782.         if (!destsp) {
  9783.             if (!ckstrcmp(service,"ftp",-1,0)) {
  9784.                 ftp_port = 21;
  9785.             } else if (!ckstrcmp(service,"ftps",-1,0)) {
  9786.                 ftp_port = 990;
  9787.             } else {
  9788.                 printf("?Bad port name - \"%s\"\n", service);
  9789.                 ftpcode = -1;
  9790.                 return(0);
  9791.             }
  9792.         } else {
  9793.             ftp_port = destsp->s_port;
  9794.             ftp_port = ntohs(ftp_port);
  9795.         }
  9796.     } else
  9797.         ftp_port = atoi(service);
  9798.     if (ftp_port <= 0) {
  9799.         printf("?Bad port name - \"%s\"\n", service);
  9800.         ftpcode = -1;
  9801.         return(0);
  9802.     }
  9803.     host = ftp_hookup(remote, ftp_port, use_tls);
  9804.     if (host) {
  9805.         ckstrncpy(ftp_user_host,remote,MAX_DNS_NAMELEN);
  9806.         connected = 1;                  /* Set FTP defaults */
  9807.         ftp_cpl = ftp_dpl = FPL_CLR;
  9808.         curtype = FTT_ASC;              /* Server uses ASCII mode */
  9809.         form = FORM_N;
  9810.         mode = MODE_S;
  9811.         stru = STRU_F;
  9812.         strcpy(bytename, "8");
  9813.         bytesize = 8;
  9814.  
  9815. #ifdef FTP_SECURITY
  9816.         if (ftp_aut) {
  9817.             if (ftp_auth()) {
  9818.                 if (ftp_cry 
  9819. #ifdef OS2
  9820.                      && ck_crypt_is_installed()
  9821. #endif /* OS2 */
  9822.                      ) {
  9823.                     if (!quiet)
  9824.                       printf("FTP Command channel is Private (encrypted)\n");
  9825.                     ftp_cpl = FPL_PRV;
  9826.                     if (setpbsz(DEFAULT_PBSZ) < 0) {
  9827.                         /* a failure here is most likely caused by a mixup */
  9828.                         /* in the session key used by client and server    */
  9829.             printf("?Protection buffer size negotiation failed\n");
  9830.                         return(0);
  9831.                     }
  9832.                     if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
  9833.                         if (!quiet)
  9834.                           printf("FTP Data channel is Private (encrypted)\n");
  9835.                         ftp_dpl = FPL_PRV;
  9836.                     } else
  9837.                       printf("?Unable to enable encryption on data channel\n");
  9838.                 } else {
  9839.                     ftp_cpl = FPL_SAF;
  9840.                 }
  9841.             }
  9842.             if (!connected)
  9843.           goto fail;
  9844.         }
  9845. #endif /* FTP_SECURITY */
  9846.         if (ftp_log)            /* ^^^ */
  9847.           ftp_login(remote);
  9848.  
  9849.         if (!connected)
  9850.       goto fail;
  9851.  
  9852. #ifdef CKLOGDIAL
  9853.         dologftp();
  9854. #endif /* CKLOGDIAL */
  9855. #ifdef OS2
  9856.         DialerSend(OPT_KERMIT_CONNECT, 0);
  9857. #endif /* OS2 */
  9858.         passivemode = ftp_psv;
  9859.         sendport = ftp_spc;
  9860.     mdtmok = 1;
  9861.     sizeok = 1;
  9862.     stouarg = 1;
  9863.     typesent = 0;
  9864.  
  9865.         if (ucbuf == NULL) {
  9866.             actualbuf = DEFAULT_PBSZ;
  9867.             while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
  9868.               actualbuf >>= 2;
  9869.         }
  9870.         if (!maxbuf)
  9871.           ucbufsiz = actualbuf - FUDGE_FACTOR;
  9872.     debug(F101,"ftpopen ucbufsiz","",ucbufsiz);
  9873.         return(1);
  9874.     }
  9875.   fail:
  9876.     printf("?Can't FTP connect to %s:%s\n",remote,service);
  9877.     ftpcode = -1;
  9878.     return(0);
  9879. }
  9880.  
  9881. #ifdef CK_SSL
  9882. int
  9883. ssl_auth() {
  9884.     int i;
  9885.     char* p;
  9886.  
  9887.     if (ssl_debug_flag) {
  9888.         fprintf(stderr,"SSL DEBUG ACTIVE\n");
  9889.         fflush(stderr);
  9890.         /* for the moment I want the output on screen */
  9891.     }
  9892.     if (ssl_ftp_data_con != NULL) {
  9893.         SSL_free(ssl_ftp_data_con);
  9894.         ssl_ftp_data_con = NULL;
  9895.     }
  9896.     if (ssl_ftp_con != NULL) {
  9897.         SSL_free(ssl_ftp_con);
  9898.         ssl_ftp_con=NULL;
  9899.     }
  9900.     if (ssl_ftp_ctx != NULL) {
  9901.         SSL_CTX_free(ssl_ftp_ctx);
  9902.         ssl_ftp_ctx = NULL;
  9903.     }
  9904.  
  9905.     /* The SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 
  9906.      * was added to OpenSSL 0.9.6e and 0.9.7.  It does not exist in previous
  9907.      * versions
  9908.      */
  9909. #ifndef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  9910. #define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0L
  9911. #endif
  9912.     if (auth_type && !strcmp(auth_type,"TLS")) {
  9913.         ssl_ftp_ctx=SSL_CTX_new(SSLv3_client_method());
  9914.         if (!ssl_ftp_ctx)
  9915.           return(0);
  9916.         SSL_CTX_set_options(ssl_ftp_ctx,
  9917.                             SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9918.                             );
  9919.     } else {
  9920.         ssl_ftp_ctx = SSL_CTX_new(ftp_bug_use_ssl_v2 ? SSLv23_client_method() : 
  9921.                                   SSLv3_client_method());
  9922.         if (!ssl_ftp_ctx)
  9923.           return(0);
  9924.         SSL_CTX_set_options(ssl_ftp_ctx,
  9925.                             (ftp_bug_use_ssl_v2 ? 0 : SSL_OP_NO_SSLv2)|
  9926.                             SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
  9927.                             );
  9928.     }
  9929.     SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
  9930.                                   (pem_password_cb *)ssl_passwd_callback);
  9931.     SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
  9932.     SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
  9933.  
  9934. #ifdef OS2
  9935. #ifdef NT
  9936.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9937.     {
  9938.         char path[CKMAXPATH];
  9939.         extern char exedir[];
  9940.  
  9941.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9942.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9943.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9944.             if (ssl_debug_flag)
  9945.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9946.         }
  9947.  
  9948.         ckmakmsg(path,CKMAXPATH,
  9949.                  (char *)GetAppData(1),"kermit 95/certs",NULL,NULL);
  9950.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9951.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9952.             if (ssl_debug_flag)
  9953.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9954.         }
  9955.  
  9956.         ckmakmsg(path,CKMAXPATH,
  9957.                  (char *)GetAppData(0),"kermit 95/certs",NULL,NULL);
  9958.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9959.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9960.             if (ssl_debug_flag)
  9961.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9962.         }
  9963.  
  9964.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  9965.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9966.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9967.             if (ssl_debug_flag)
  9968.                 printf("?Unable to load verify-file: %s\r\n",path);
  9969.         }
  9970.  
  9971.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  9972.          "kermit 95/ca_certs.pem",NULL,NULL);
  9973.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9974.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9975.             if (ssl_debug_flag)
  9976.                 printf("?Unable to load verify-file: %s\r\n",path);
  9977.         }
  9978.  
  9979.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  9980.          "kermit 95/ca_certs.pem",NULL,NULL);
  9981.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  9982.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9983.             if (ssl_debug_flag)
  9984.                 printf("?Unable to load verify-file: %s\r\n",path);
  9985.         }
  9986.     }
  9987. #else /* NT */
  9988.     /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  9989.     {
  9990.  
  9991.         char path[CKMAXPATH];
  9992.         extern char exedir[];
  9993.  
  9994.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  9995.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
  9996.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  9997.             if (ssl_debug_flag)
  9998.                 printf("?Unable to load verify-dir: %s\r\n",path);
  9999.         }
  10000.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  10001.         if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
  10002.             debug(F110,"ftp ssl_auth unable to load path",path,0);
  10003.             if (ssl_debug_flag)
  10004.                 printf("?Unable to load verify-file: %s\r\n",path);
  10005.         }
  10006.     }
  10007. #endif /* NT */
  10008. #else /* OS2 */
  10009.     SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
  10010. #endif /* OS2 */
  10011.  
  10012.     if (ssl_verify_file &&
  10013.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
  10014.         debug(F110,
  10015.               "ftp ssl auth unable to load ssl_verify_file",
  10016.               ssl_verify_file,
  10017.               0
  10018.               );
  10019.         if (ssl_debug_flag)
  10020.           printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  10021.     }
  10022.     if (ssl_verify_dir &&
  10023.         SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
  10024.         debug(F110,
  10025.               "ftp ssl auth unable to load ssl_verify_dir",
  10026.               ssl_verify_dir,
  10027.               0
  10028.               );
  10029.         if (ssl_debug_flag)
  10030.           printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  10031.     }
  10032.  
  10033.     /* set up the new CRL Store */
  10034.     crl_store = (X509_STORE *)X509_STORE_new();
  10035.     if (crl_store) {
  10036. #ifdef OS2
  10037.         char path[CKMAXPATH];
  10038.         extern char exedir[];
  10039.  
  10040.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  10041.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10042.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10043.             if (ssl_debug_flag)
  10044.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10045.         }
  10046. #ifdef NT
  10047.         ckmakmsg(path,CKMAXPATH,
  10048.          (char *)GetAppData(1),"kermit 95/crls",NULL,NULL);
  10049.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10050.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10051.             if (ssl_debug_flag)
  10052.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10053.         }
  10054.         ckmakmsg(path,CKMAXPATH,
  10055.          (char *)GetAppData(0),"kermit 95/crls",NULL,NULL);
  10056.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  10057.             debug(F110,"ftp ssl auth unable to load dir",path,0);
  10058.             if (ssl_debug_flag)
  10059.                 printf("?Unable to load crl-dir: %s\r\n",path);
  10060.         }
  10061. #endif /* NT */
  10062.         
  10063.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  10064.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10065.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10066.             if (ssl_debug_flag)
  10067.                 printf("?Unable to load crl-file: %s\r\n",path);
  10068.         }
  10069. #ifdef NT
  10070.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
  10071.          "kermit 95/ca_crls.pem",NULL,NULL);
  10072.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10073.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10074.             if (ssl_debug_flag)
  10075.                 printf("?Unable to load crl-file: %s\r\n",path);
  10076.         }
  10077.         ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
  10078.          "kermit 95/ca_crls.pem",NULL,NULL);
  10079.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  10080.             debug(F110,"ftp ssl auth unable to load file",path,0);
  10081.             if (ssl_debug_flag)
  10082.                 printf("?Unable to load crl-file: %s\r\n",path);
  10083.         }
  10084. #endif /* NT */
  10085. #endif /* OS2 */
  10086.  
  10087.         if (ssl_crl_file || ssl_crl_dir) {
  10088.             if (ssl_crl_file &&
  10089.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  10090.                 debug(F110,
  10091.                       "ftp ssl auth unable to load ssl_crl_file",
  10092.                       ssl_crl_file,
  10093.                       0
  10094.                       );
  10095.                 if (ssl_debug_flag)
  10096.                   printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  10097.             }
  10098.             if (ssl_crl_dir &&
  10099.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  10100.                 debug(F110,
  10101.                       "ftp ssl auth unable to load ssl_crl_dir",
  10102.                       ssl_crl_dir,
  10103.                       0
  10104.                       );
  10105.                 if (ssl_debug_flag)
  10106.                   printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  10107.             }
  10108.         } else {
  10109.             X509_STORE_set_default_paths(crl_store);
  10110.         }
  10111.     }
  10112.     SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
  10113.                        ssl_client_verify_callback);
  10114.     ssl_verify_depth = -1;
  10115.     ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
  10116.     tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
  10117.     SSL_set_fd(ssl_ftp_con,csocket);
  10118.     SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
  10119.     if (ssl_cipher_list) {
  10120.         SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
  10121.     } else {
  10122.         char * p;
  10123.         if (p = getenv("SSL_CIPHER")) {
  10124.             SSL_set_cipher_list(ssl_ftp_con,p);
  10125.         } else {
  10126.             SSL_set_cipher_list(ssl_ftp_con,DEFAULT_CIPHER_LIST);
  10127.         }
  10128.     }
  10129.     if (ssl_debug_flag) {
  10130.         fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
  10131.         fflush(stderr);
  10132.     }
  10133.     if (SSL_connect(ssl_ftp_con) <= 0) {
  10134.         static char errbuf[1024];
  10135.         ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
  10136.                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  10137.         fprintf(stderr,"%s\n", errbuf);
  10138.         fflush(stderr);
  10139.         ssl_ftp_active_flag=0;
  10140.         SSL_free(ssl_ftp_con);
  10141.         ssl_ftp_con = NULL;
  10142.     } else {
  10143.         ssl_ftp_active_flag = 1;
  10144.  
  10145.         if (!ssl_certsok_flag && !tls_is_krb5(1)) {
  10146.             char *subject = ssl_get_subject_name(ssl_ftp_con);
  10147.  
  10148.             if (!subject) {
  10149.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  10150.                     debug(F110,"ssl_auth","[SSL - FAILED]",0);
  10151.                     return(ssl_ftp_active_flag = 0);
  10152.                 } else {
  10153.                     if (uq_ok("Warning: Server didn't provide a certificate\n",
  10154.                                "Continue? (Y/N)",3,NULL,0) <= 0) {
  10155.                         debug(F110, "ssl_auth","[SSL - FAILED]",0);
  10156.                         return(ssl_ftp_active_flag = 0);
  10157.                     }
  10158.                 }
  10159.             } else if (ssl_check_server_name(ssl_ftp_con, ftp_user_host)) {
  10160.                 debug(F110,"ssl_auth","[SSL - FAILED]",0);
  10161.                 return(ssl_ftp_active_flag = 0);
  10162.             }
  10163.         }
  10164.         debug(F110,"ssl_auth","[SSL - OK]",0);
  10165.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  10166.     }
  10167.     if (ssl_debug_flag) {
  10168.         fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
  10169.         fflush(stderr);
  10170.     }
  10171.     return(ssl_ftp_active_flag);
  10172. }
  10173. #endif /* CK_SSL */
  10174.  
  10175. static sigtype
  10176. cmdcancel(sig) int sig; {
  10177. #ifdef OS2
  10178.     /* In Unix we "chain" to trap(), which prints this */
  10179.     printf("^C...\n");
  10180. #endif /* OS2 */
  10181.     debug(F100,"ftp cmdcancel caught SIGINT ","",0);
  10182.     fflush(stdout);
  10183.     secure_getc(0,1);            /* Initialize net input buffers */
  10184.     cancelfile++;
  10185.     cancelgroup++;
  10186.     mlsreset();
  10187. #ifndef OS2
  10188. #ifdef FTP_PROXY
  10189.     if (ptflag)                         /* proxy... */
  10190.       longjmp(ptcancel,1);
  10191. #endif /* FTP_PROXY */
  10192.     debug(F100,"ftp cmdcancel chain to trap()...","",0);
  10193.     trap(SIGINT);
  10194.     /* NOTREACHED */
  10195.     debug(F100,"ftp cmdcancel return from trap()...","",0);
  10196. #else
  10197.     debug(F100,"ftp cmdcancel PostCtrlCSem()...","",0);
  10198.     PostCtrlCSem();
  10199. #endif /* OS2 */
  10200. }
  10201.  
  10202. static int
  10203. #ifdef CK_ANSIC
  10204. scommand(char * s)                      /* Was secure_command() */
  10205. #else
  10206. scommand(s) char * s;
  10207. #endif /* CK_ANSIC */
  10208. {
  10209.     int length = 0, len2;
  10210.     char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
  10211. #ifdef CK_SSL
  10212.     if (ssl_ftp_active_flag) {
  10213.         int error, rc;
  10214.         length = strlen(s) + 2;
  10215.         length = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10216.         rc = SSL_write(ssl_ftp_con,out,length);
  10217.         error = SSL_get_error(ssl_ftp_con,rc);
  10218.         switch (error) {
  10219.           case SSL_ERROR_NONE:
  10220.             return(1);
  10221.           case SSL_ERROR_WANT_WRITE:
  10222.           case SSL_ERROR_WANT_READ:
  10223.           case SSL_ERROR_SYSCALL:
  10224. #ifdef NT
  10225.             {
  10226.                 int gle = GetLastError();
  10227.             }
  10228. #endif /* NT */
  10229.           case SSL_ERROR_WANT_X509_LOOKUP:
  10230.           case SSL_ERROR_SSL:
  10231.           case SSL_ERROR_ZERO_RETURN:
  10232.           default:
  10233.             lostpeer();
  10234.         }
  10235.         return(0);
  10236.     }
  10237. #endif /* CK_SSL */
  10238.  
  10239.     if (auth_type && ftp_cpl != FPL_CLR) {
  10240. #ifdef FTP_SRP
  10241.         if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
  10242.           if ((length = srp_encode(ftp_cpl == FPL_PRV,
  10243.                                    (CHAR *)s,
  10244.                                    (CHAR *)out,
  10245.                                    strlen(s))) < 0) {
  10246.               fprintf(stderr, "SRP failed to encode message\n");
  10247.               return(0);
  10248.           }
  10249. #endif /* FTP_SRP */
  10250. #ifdef FTP_KRB4
  10251.         if (ck_krb4_is_installed() &&
  10252.             (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  10253.             if (ftp_cpl == FPL_PRV) {
  10254.                 length =
  10255.                   krb_mk_priv((CHAR *)s, (CHAR *)out,
  10256.                               strlen(s), ftp_sched,
  10257. #ifdef KRB524
  10258.                               ftp_cred.session,
  10259. #else /* KRB524 */
  10260.                               &ftp_cred.session,
  10261. #endif /* KRB524 */
  10262.                               &myctladdr, &hisctladdr);
  10263.             } else {
  10264.                 length =
  10265.                   krb_mk_safe((CHAR *)s,
  10266.                               (CHAR *)out,
  10267.                               strlen(s),
  10268. #ifdef KRB524
  10269.                               ftp_cred.session,
  10270. #else /* KRB524 */
  10271.                               &ftp_cred.session,
  10272. #endif /* KRB524 */
  10273.                               &myctladdr, &hisctladdr);
  10274.             }
  10275.             if (length == -1) {
  10276.                 fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
  10277.                         ftp_cpl == FPL_PRV ? "priv" : "safe");
  10278.                 return(0);
  10279.             }
  10280.         }
  10281. #endif /* FTP_KRB4 */
  10282. #ifdef FTP_GSSAPI
  10283.         /* Scommand (based on level) */
  10284.         if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  10285.             gss_buffer_desc in_buf, out_buf;
  10286.             OM_uint32 maj_stat, min_stat;
  10287.             int conf_state;
  10288.             in_buf.value = s;
  10289.             in_buf.length = strlen(s) + 1;
  10290.             maj_stat = gss_seal(&min_stat, gcontext,
  10291.                                 (ftp_cpl==FPL_PRV), /* private */
  10292.                                 GSS_C_QOP_DEFAULT,
  10293.                                 &in_buf, &conf_state,
  10294.                                 &out_buf);
  10295.             if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
  10296.                 user_gss_error(maj_stat, min_stat,
  10297.                                (ftp_cpl==FPL_PRV)?
  10298.                                "gss_seal ENC didn't complete":
  10299.                                "gss_seal MIC didn't complete");
  10300.             } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
  10301.                 fprintf(stderr, "GSSAPI didn't encrypt message");
  10302.             } else {
  10303.                 if (ftp_deb)
  10304.                   fprintf(stderr, "sealed (%s) %d bytes\n",
  10305.                           ftp_cpl==FPL_PRV?"ENC":"MIC",
  10306.                           out_buf.length);
  10307.                 memcpy(out, out_buf.value,
  10308.                        length=out_buf.length);
  10309.                 gss_release_buffer(&min_stat, &out_buf);
  10310.             }
  10311.         }
  10312. #endif /* FTP_GSSAPI */
  10313.         /* Other auth types go here ... */
  10314.  
  10315.         len2 = FTP_BUFSIZ;
  10316.         if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
  10317.                                    length, &len2, RADIX_ENCODE))
  10318.             ) {
  10319.             fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
  10320.                     radix_error(kerror));
  10321.             return(0);
  10322.         }
  10323.         if (ftp_deb)
  10324.           fprintf(stderr, "scommand(%s)\nencoding %d bytes\n", s, length);
  10325.         len2 = ckmakmsg(out,
  10326.             FTP_BUFSIZ,
  10327.             ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
  10328.                         in,
  10329.             "\r\n",
  10330.             NULL
  10331.             );
  10332.         send(csocket,(SENDARG2TYPE)out,len2,0);
  10333.     } else {
  10334.         char out[FTP_BUFSIZ];
  10335.         int len = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
  10336.         send(csocket,(SENDARG2TYPE)out,len,0);
  10337.     }
  10338.     return(1);
  10339. }
  10340.  
  10341. static int
  10342. mygetc() {
  10343.     static char inbuf[4096];
  10344.     static int bp = 0, ep = 0;
  10345.     int rc;
  10346.  
  10347.     if (bp == ep) {
  10348.         bp = ep = 0;
  10349. #ifdef CK_SSL
  10350.         if (ssl_ftp_active_flag) {
  10351.             int error;
  10352.             rc = SSL_read(ssl_ftp_con,inbuf,4096);
  10353.             error = SSL_get_error(ssl_ftp_con,rc);
  10354.             switch (error) {
  10355.               case SSL_ERROR_NONE:
  10356.                 break;
  10357.               case SSL_ERROR_WANT_WRITE:
  10358.               case SSL_ERROR_WANT_READ:
  10359.                 return(0);
  10360.               case SSL_ERROR_SYSCALL:
  10361.                 if (rc == 0) {          /* EOF */
  10362.                     break;
  10363.                 } else {
  10364. #ifdef NT
  10365.                     int gle = GetLastError();
  10366. #endif /* NT */
  10367.                     break;
  10368.                 }
  10369.               case SSL_ERROR_WANT_X509_LOOKUP:
  10370.               case SSL_ERROR_SSL:
  10371.               case SSL_ERROR_ZERO_RETURN:
  10372.               default:
  10373.                 break;
  10374.             }
  10375.         } else
  10376. #endif /* CK_SSL */
  10377.           rc = recv(csocket,(char *)inbuf,4096,0);
  10378.         if (rc <= 0)
  10379.           return(EOF);
  10380.         ep = rc;
  10381.     }
  10382.     return(inbuf[bp++]);
  10383. }
  10384.  
  10385. /*  x l a t e c  --  Translate a character  */
  10386. /*
  10387.     Call with:
  10388.       fc    = Function code: 0 = translate, 1 = initialize.
  10389.       c     = Character (as int).
  10390.       incs  = Index of charset to translate from.
  10391.       outcs = Index of charset to translate to.
  10392.  
  10393.     Returns:
  10394.       0: OK
  10395.      -1: Error
  10396. */
  10397. static int
  10398. xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
  10399. #ifdef NOCSETS
  10400.     return(c);
  10401. #else
  10402.     static char buf[128];
  10403.     static int cx;
  10404.     int c0, c1;
  10405.  
  10406.     if (fc == 1) {                      /* Initialize */
  10407.         cx = 0;                         /* Catch-up buffer write index */
  10408.         xgnbp = buf;                    /* Catch-up buffer read pointer */
  10409.         buf[0] = NUL;                   /* Buffer is empty */
  10410.         return(0);
  10411.     }
  10412.     if (cx >= 127) {                    /* Catch-up buffer full */
  10413.         debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
  10414.         printf("?Translation buffer overflow\n");
  10415.         return(-1);
  10416.     }
  10417.     /* Add char to buffer. */
  10418.     /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
  10419.  
  10420.     debug(F000,"xlatec buf",ckitoa(cx),c);
  10421.     buf[cx++] = c;
  10422.     buf[cx] = NUL;
  10423.  
  10424.     while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
  10425.         if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)    /* (NULL was xprintc) */
  10426.           return(-1);
  10427.     }
  10428.     /* If we're caught up, reinitialize the buffer */
  10429.     return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
  10430. #endif /* NOCSETS */
  10431. }
  10432.  
  10433.  
  10434. /*  p a r s e f e a t  */
  10435.  
  10436. /* Note: for convenience we align keyword values with table indices */
  10437. /* If you need to insert a new keyword, adjust the SFT_xxx definitions */
  10438.  
  10439. static struct keytab feattab[] = {
  10440.     { "$$$$", 0,        0 },        /* Dummy for sfttab[0] */
  10441.     { "AUTH", SFT_AUTH, 0 },
  10442.     { "LANG", SFT_LANG, 0 },
  10443.     { "MDTM", SFT_MDTM, 0 },
  10444.     { "MLST", SFT_MLST, 0 },
  10445.     { "PBSZ", SFT_PBSZ, 0 },
  10446.     { "PROT", SFT_PROT, 0 },
  10447.     { "REST", SFT_REST, 0 },
  10448.     { "SIZE", SFT_SIZE, 0 },
  10449.     { "TVFS", SFT_TVFS, 0 },
  10450.     { "UTF8", SFT_UTF8, 0 }
  10451. };
  10452. static int nfeattab = (sizeof(feattab) / sizeof(struct keytab));
  10453.  
  10454. #define FACT_CSET  1
  10455. #define FACT_CREA  2
  10456. #define FACT_LANG  3
  10457. #define FACT_MTYP  4
  10458. #define FACT_MDTM  5
  10459. #define FACT_PERM  6
  10460. #define FACT_SIZE  7
  10461. #define FACT_TYPE  8
  10462. #define FACT_UNIQ  9
  10463.  
  10464. static struct keytab facttab[] = {
  10465.     { "CHARSET",    FACT_CSET, 0 },
  10466.     { "CREATE",     FACT_CREA, 0 },
  10467.     { "LANG",       FACT_LANG, 0 },
  10468.     { "MEDIA-TYPE", FACT_MTYP, 0 },
  10469.     { "MODIFY",     FACT_MDTM, 0 },
  10470.     { "PERM",       FACT_PERM, 0 },
  10471.     { "SIZE",       FACT_SIZE, 0 },
  10472.     { "TYPE",       FACT_TYPE, 0 },
  10473.     { "UNIQUE",     FACT_UNIQ, 0 }
  10474. };
  10475. static int nfacttab = (sizeof(facttab) / sizeof(struct keytab));
  10476.  
  10477. static struct keytab ftyptab[] = {
  10478.     { "CDIR", FTYP_CDIR, 0 },
  10479.     { "DIR",  FTYP_DIR,  0 },
  10480.     { "FILE", FTYP_FILE, 0 },
  10481.     { "PDIR", FTYP_PDIR, 0 }
  10482. };
  10483. static int nftyptab = (sizeof(ftyptab) / sizeof(struct keytab));
  10484.  
  10485. static VOID
  10486. parsefeat(s) char * s; {        /* Parse a FEATURE response */
  10487.     char kwbuf[8];
  10488.     int i, x;
  10489.     if (!s) return;
  10490.     if (!*s) return;
  10491.     while (*s < '!')
  10492.       s++;
  10493.     for (i = 0; i < 4; i++) {
  10494.     if (s[i] < '!')
  10495.       break;
  10496.     kwbuf[i] = s[i];
  10497.     }
  10498.     if (s[i] && s[i] != SP && s[i] != CR && s[i] != LF)
  10499.       return;
  10500.     kwbuf[i] = NUL;
  10501.     /* xlookup requires a full (but case independent) match */
  10502.     i = xlookup(feattab,kwbuf,nfeattab,&x);
  10503.     debug(F111,"ftp parsefeat",s,i);
  10504.     if (i < 0 || i > 15)
  10505.       return;
  10506.  
  10507.     switch (i) {
  10508.       case SFT_MDTM:            /* Controlled by ENABLE/DISABLE */
  10509.     sfttab[i] = mdtmok;
  10510.     if (mdtmok) sfttab[0]++;
  10511.     break;
  10512.       case SFT_MLST:            /* ditto */
  10513.     sfttab[i] = mlstok;
  10514.     if (mlstok) sfttab[0]++;
  10515.     break;
  10516.       case SFT_SIZE:            /* ditto */
  10517.     sfttab[i] = sizeok;
  10518.     if (sizeok) sfttab[0]++;
  10519.     break;
  10520.       case SFT_AUTH:            /* ditto */
  10521.     sfttab[i] = ftp_aut;
  10522.     if (ftp_aut) sfttab[0]++;
  10523.     break;
  10524.       default:                /* Others */
  10525.     sfttab[0]++;
  10526.     sfttab[i]++;
  10527.     }
  10528. }
  10529.  
  10530. static char *
  10531. parsefacts(s) char * s; {        /* Parse MLS[DT] File Facts */
  10532.     char * p;
  10533.     int i, j, x;
  10534.     if (!s) return(NULL);
  10535.     if (!*s) return(NULL);
  10536.  
  10537.     /* Maybe we should make a copy of s so we can poke it... */
  10538.  
  10539.     while ((p = ckstrchr(s,'='))) {
  10540.     *p = NUL;            /* s points to fact */
  10541.     i = xlookup(facttab,s,nfacttab,&x); 
  10542.     debug(F111,"ftp parsefact fact",s,i);
  10543.     *p = '=';
  10544.     s = p+1;            /* Now s points to arg */
  10545.     p = ckstrchr(s,';');
  10546.         if (!p)
  10547.       p = ckstrchr(s,SP);
  10548.     if (!p) {
  10549.         debug(F110,"ftp parsefact end-of-val search fail",s,0);
  10550.         break;
  10551.     }
  10552.     *p = NUL;
  10553.     debug(F110,"ftp parsefact valu",s,0);
  10554.     switch (i) {
  10555.       case FACT_CSET:        /* Ignore these for now */
  10556.       case FACT_CREA:
  10557.       case FACT_LANG:
  10558.       case FACT_PERM:
  10559.       case FACT_MTYP:
  10560.       case FACT_UNIQ:
  10561.         break;
  10562.       case FACT_MDTM:        /* Modtime */
  10563.         makestr(&havemdtm,s);
  10564.         debug(F110,"ftp parsefact mdtm",havemdtm,0);
  10565.         break;
  10566.       case FACT_SIZE:        /* Size */
  10567.         havesize = ckatofs(s);
  10568.         debug(F101,"ftp parsefact size","",havesize);
  10569.         break;
  10570.       case FACT_TYPE:        /* Type */
  10571.         j = xlookup(ftyptab,s,nftyptab,NULL);
  10572.         debug(F111,"ftp parsefact type",s,j);
  10573.         havetype = (j < 1) ? 0 : j;
  10574.         break;
  10575.     }
  10576.     *p = ';';
  10577.     s = p+1;            /* s points next fact or name */
  10578.     }
  10579.     while (*s == SP)            /* Skip past spaces. */
  10580.       s++;
  10581.     if (!*s)                /* Make sure we still have a name */
  10582.       s = NULL;
  10583.     debug(F110,"ftp parsefact name",s,0);
  10584.     return(s);
  10585. }
  10586.  
  10587. /*  g e t r e p l y  --  (to an FTP command sent to server)  */
  10588.  
  10589. /* vbm = 1 (verbose); 0 (quiet except for error messages); 9 (super quiet) */
  10590.  
  10591. static int
  10592. getreply(expecteof,lcs,rcs,vbm,fc) int expecteof, lcs, rcs, vbm, fc; {
  10593.     /* lcs, rcs, vbm parameters as in ftpcmd() */
  10594.     register int i, c, n;
  10595.     register int dig;
  10596.     register char *cp;
  10597.     int xlate = 0;
  10598.     int count = 0;
  10599.     int auth = 0;
  10600.     int originalcode = 0, continuation = 0;
  10601.     sig_t oldintr;
  10602.     int pflag = 0;
  10603.     char *pt = pasv;
  10604.     char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
  10605.     int safe = 0;
  10606.     int xquiet = 0;
  10607.  
  10608.     auth = (fc == GRF_AUTH);
  10609.  
  10610. #ifndef NOCSETS
  10611.     debug(F101,"ftp getreply lcs","",lcs);
  10612.     debug(F101,"ftp getreply rcs","",rcs);
  10613.     if (lcs > -1 && rcs > -1 && lcs != rcs) {
  10614.         xlate = 1;
  10615.         initxlate(rcs,lcs);
  10616.         xlatec(1,0,rcs,lcs);
  10617.     }
  10618. #endif /* NOCSETS */
  10619.     debug(F101,"ftp getreply fc","",fc);
  10620.  
  10621.     if (quiet)
  10622.       xquiet = 1;
  10623.     if (vbm == 9) {
  10624.         xquiet = 1;
  10625.         vbm = 0;
  10626.     }
  10627.     if (ftp_deb)                        /* DEBUG */
  10628.       vbm = 1;
  10629.     else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
  10630.       vbm = 0;
  10631.     else if (vbm < 0)                   /* VERBOSE */
  10632.       vbm = ftp_vbm;
  10633.  
  10634.     ibuf[0] = '\0';
  10635.     if (reply_parse)
  10636.       reply_ptr = reply_buf;
  10637.     havesigint = 0;
  10638.     oldintr = signal(SIGINT, cmdcancel);
  10639.     for (count = 0;; count++) {
  10640.         obuf[0] = '\0';
  10641.         dig = n = ftpcode = i = 0;
  10642.         cp = ftp_reply_str;
  10643.         while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
  10644.             if (c == IAC) {             /* Handle telnet commands */
  10645.                 switch (c = mygetc()) {
  10646.                   case WILL:
  10647.                   case WONT:
  10648.                     c = mygetc();
  10649.                     obuf[0] = IAC;
  10650.                     obuf[1] = DONT;
  10651.                     obuf[2] = c;
  10652.                     obuf[3] = NUL;
  10653. #ifdef CK_SSL
  10654.                     if (ssl_ftp_active_flag) {
  10655.                         int error, rc;
  10656.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10657.                         error = SSL_get_error(ssl_ftp_con,rc);
  10658.                         switch (error) {
  10659.                           case SSL_ERROR_NONE:
  10660.                             break;
  10661.                           case SSL_ERROR_WANT_WRITE:
  10662.                           case SSL_ERROR_WANT_READ:
  10663.                             return(0);
  10664.                           case SSL_ERROR_SYSCALL:
  10665.                             if (rc == 0) { /* EOF */
  10666.                                 break;
  10667.                             } else {
  10668. #ifdef NT
  10669.                                 int gle = GetLastError();
  10670. #endif /* NT */
  10671.                                 break;
  10672.                             }
  10673.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10674.                           case SSL_ERROR_SSL:
  10675.                           case SSL_ERROR_ZERO_RETURN:
  10676.                           default:
  10677.                             break;
  10678.                         }
  10679.                     } else
  10680. #endif /* CK_SSL */
  10681.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10682.                     break;
  10683.                   case DO:
  10684.                   case DONT:
  10685.                     c = mygetc();
  10686.                     obuf[0] = IAC;
  10687.                     obuf[1] = WONT;
  10688.                     obuf[2] = c;
  10689.                     obuf[3] = NUL;
  10690. #ifdef CK_SSL
  10691.                     if (ssl_ftp_active_flag) {
  10692.                         int error, rc;
  10693.                         rc = SSL_write(ssl_ftp_con,obuf,3);
  10694.                         error = SSL_get_error(ssl_ftp_con,rc);
  10695.                         switch (error) {
  10696.                           case SSL_ERROR_NONE:
  10697.                             break;
  10698.                           case SSL_ERROR_WANT_WRITE:
  10699.                           case SSL_ERROR_WANT_READ:
  10700.                               signal(SIGINT,oldintr);
  10701.                               return(0);
  10702.                           case SSL_ERROR_SYSCALL:
  10703.                             if (rc == 0) { /* EOF */
  10704.                                 break;
  10705.                             } else {
  10706. #ifdef NT
  10707.                                 int gle = GetLastError();
  10708. #endif /* NT */
  10709.                                 break;
  10710.                             }
  10711.                           case SSL_ERROR_WANT_X509_LOOKUP:
  10712.                           case SSL_ERROR_SSL:
  10713.                           case SSL_ERROR_ZERO_RETURN:
  10714.                           default:
  10715.                             break;
  10716.                         }
  10717.                     } else
  10718. #endif /* CK_SSL */
  10719.                       send(csocket,(SENDARG2TYPE)obuf,3,0);
  10720.                     break;
  10721.                   default:
  10722.                     break;
  10723.                 }
  10724.                 continue;
  10725.             }
  10726.             dig++;
  10727.             if (c == EOF) {
  10728.                 if (expecteof) {
  10729.                     signal(SIGINT,oldintr);
  10730.                     ftpcode = 221;
  10731.                     debug(F101,"ftp getreply EOF","",ftpcode);
  10732.                     return(0);
  10733.                 }
  10734.                 lostpeer();
  10735.                 if (!xquiet) {
  10736.                     if (ftp_deb)
  10737.                       printf("421 ");
  10738.                     printf(
  10739.                       "Service not available, connection closed by server\n");
  10740.                     fflush(stdout);
  10741.                 }
  10742.                 signal(SIGINT,oldintr);
  10743.                 ftpcode = 421;
  10744.                 debug(F101,"ftp getreply EOF","",ftpcode);
  10745.                 return(4);
  10746.             }
  10747.             if (n == 0) {        /* First digit */
  10748.         n = c;            /* Save it */
  10749.         }
  10750.             if (auth_type &&
  10751. #ifdef CK_SSL
  10752.                 !ssl_ftp_active_flag &&
  10753. #endif /* CK_SSL */
  10754.                 !ibuf[0] && (n == '6' || continuation)) {
  10755.                 if (c != '\r' && dig > 4)
  10756.                   obuf[i++] = c;
  10757.             } else {
  10758.                 if (auth_type &&
  10759. #ifdef CK_SSL
  10760.                     !ssl_ftp_active_flag &&
  10761. #endif /* CK_SSL */
  10762.                     !ibuf[0] && dig == 1 && vbm)
  10763.                   printf("Unauthenticated reply received from server:\n");
  10764.                 if (reply_parse) {
  10765.                     *reply_ptr++ = c;
  10766.                     *reply_ptr = NUL;
  10767.                 }
  10768.                 if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
  10769.                     ftp_cmdlin < 2) {
  10770.                     if ((c != '\r') &&
  10771.                         (ftp_deb || ((vbm || (!auth && n == '5')) &&
  10772.                         (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
  10773.                         )))))
  10774.                     {
  10775. #ifdef FTP_PROXY
  10776.                         if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
  10777.                           printf("%s:",ftp_host);
  10778. #endif /* FTP_PROXY */
  10779.  
  10780.                         if (!xquiet) {
  10781. #ifdef NOCSETS
  10782.                             printf("%c",c);
  10783. #else
  10784.                             if (xlate) {
  10785.                                 xlatec(0,c,rcs,lcs);
  10786.                             } else {
  10787.                                 printf("%c",c);
  10788.                             }
  10789. #endif /* NOCSETS */
  10790.                         }
  10791.                     }
  10792.                 }
  10793.             }
  10794.             if (auth_type &&
  10795. #ifdef CK_SSL
  10796.                 !ssl_ftp_active_flag &&
  10797. #endif /* CK_SSL */
  10798.                 !ibuf[0] && n != '6')
  10799.               continue;
  10800.             if (dig < 4 && isdigit(c))
  10801.               ftpcode = ftpcode * 10 + (c - '0');
  10802.             if (!pflag && ftpcode == 227)
  10803.               pflag = 1;
  10804.             if (dig > 4 && pflag == 1 && isdigit(c))
  10805.               pflag = 2;
  10806.             if (pflag == 2) {
  10807.                 if (c != '\r' && c != ')')
  10808.                   *pt++ = c;
  10809.                 else {
  10810.                     *pt = '\0';
  10811.                     pflag = 3;
  10812.                 }
  10813.             }
  10814.             if (dig == 4 && c == '-' && n != '6') {
  10815.                 if (continuation)
  10816.                   ftpcode = 0;
  10817.                 continuation++;
  10818.             }
  10819.             if (cp < &ftp_reply_str[FTP_BUFSIZ - 1]) {
  10820.                 *cp++ = c;
  10821.                 *cp = NUL;
  10822.             }
  10823.         }
  10824.         if (deblog ||
  10825. #ifdef COMMENT
  10826. /*
  10827.   Sometimes we need to print the server reply.  printlines is nonzero for any
  10828.   command where the results are sent back on the control connection rather
  10829.   than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  10830.   has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  10831.   STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  10832.   is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  10833.   lines from UNIX.  Further experimentation needed with other servers.  Of
  10834.   course RFC959 is mute as to the format of the server reply.
  10835.  
  10836.   'printlines' is also true for PWD and BYE.
  10837. */
  10838.         (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
  10839. #else
  10840. /* No, we can't be that clever -- it breaks other things like RPWD... */
  10841.             (printlines &&
  10842.              (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
  10843. #endif /* COMMENT */
  10844.             ) {
  10845.             char * q = cp;
  10846.             char *r = ftp_reply_str;
  10847.             *q-- = NUL;                 /* NUL-terminate */
  10848.             while (*q < '!' && q > r)   /* Strip CR, etc */
  10849.               *q-- = NUL;
  10850.             if (!ftp_deb && printlines) { /* If printing */
  10851.                 if (ftpcode != 0)       /* strip ftpcode if any */
  10852.                   r += 4;
  10853. #ifdef NOCSETS
  10854.                 printf("%s\n",r);       /* and print */
  10855. #else
  10856.                 if (!xlate) {
  10857.                     printf("%s\n",r);
  10858.                 } else {        /* Translating */
  10859.                     xgnbp = r;        /* Set up strgetc() */
  10860.                     while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
  10861.                         if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {    /* (xprintc) */
  10862.                             signal(SIGINT,oldintr);
  10863.                             return(-1);
  10864.                         }
  10865.                     }
  10866.                     printf("\n");
  10867.                 }
  10868. #endif /* NOCSETS */
  10869.             }
  10870.         }
  10871.     debug(F110,"FTP RCVD ",ftp_reply_str,0);
  10872.  
  10873.     if (fc == GRF_FEAT) {        /* Parsing FEAT command response? */
  10874.         if (count == 0 && n == '2') {
  10875.         int i;            /* (Re)-init server FEATure table */
  10876.         debug(F100,"ftp getreply clearing feature table","",0);
  10877.         for (i = 0; i < 16; i++)
  10878.           sfttab[i] = 0;
  10879.         } else {
  10880.         parsefeat((char *)ftp_reply_str);
  10881.         }
  10882.     }
  10883.         if (auth_type &&
  10884. #ifdef CK_SSL
  10885.             !ssl_ftp_active_flag &&
  10886. #endif /* CK_SSL */
  10887.              !ibuf[0] && n != '6') {
  10888.             signal(SIGINT,oldintr);
  10889.             return(getreply(expecteof,lcs,rcs,vbm,auth));
  10890.         }
  10891.         ibuf[0] = obuf[i] = '\0';
  10892.         if (ftpcode && n == '6')
  10893.           if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
  10894.               printf("Unknown reply: %d %s\n", ftpcode, obuf);
  10895.               n = '5';
  10896.           } else safe = (ftpcode == 631);
  10897.         if (obuf[0]                     /* if there is a string to decode */
  10898. #ifdef CK_SSL
  10899.             && !ssl_ftp_active_flag     /* and not SSL/TLS */
  10900. #endif /* CK_SSL */
  10901.             ) {
  10902.             if (!auth_type) {
  10903.                 printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
  10904.                 n = '5';
  10905.             }
  10906. #ifndef CK_ENCRYPTION
  10907.             else if (ftpcode == 632) {
  10908.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10909.                 n = '5';
  10910.             }
  10911. #endif /* CK_ENCRYPTION */
  10912. #ifdef NOCONFIDENTIAL
  10913.             else if (ftpcode == 633) {
  10914.                 printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
  10915.                 n = '5';
  10916.             }
  10917. #endif /* NOCONFIDENTIAL */
  10918.             else {
  10919.                 int len = FTP_BUFSIZ;
  10920.                 if ((kerror = radix_encode((CHAR *)obuf,
  10921.                                            (CHAR *)ibuf,
  10922.                                            0,
  10923.                                            &len,
  10924.                                            RADIX_DECODE))
  10925.                     ) {
  10926.                     printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
  10927.                            ftpcode, radix_error(kerror), obuf);
  10928.                     n = '5';
  10929.                 }
  10930. #ifdef FTP_SRP
  10931.                 else if (strcmp(auth_type, "SRP") == 0) {
  10932.                     int outlen;
  10933.                     outlen = srp_decode(!safe, (CHAR *)ibuf,
  10934.                                         (CHAR *) ibuf, len);
  10935.                     if (outlen < 0) {
  10936.                         printf("Warning: %d reply %s!\n",
  10937.                                ftpcode, safe ? "modified" : "garbled");
  10938.                         n = '5';
  10939.                     } else {
  10940.                         ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
  10941.                         if (ftp_deb)
  10942.                           printf("%c:", safe ? 'S' : 'P');
  10943.                         continue;
  10944.                     }
  10945.                 }
  10946. #endif /* FTP_SRP */
  10947. #ifdef FTP_KRB4
  10948.                 else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  10949.                     if (safe) {
  10950.                         kerror = krb_rd_safe((CHAR *)ibuf, len,
  10951. #ifdef KRB524
  10952.                                              ftp_cred.session,
  10953. #else /* KRB524 */
  10954.                                              &ftp_cred.session,
  10955. #endif /* KRB524 */
  10956.                                              &hisctladdr,
  10957.                                              &myctladdr,
  10958.                                              &ftp_msg_data
  10959.                                              );
  10960.                     } else {
  10961.                         kerror = krb_rd_priv((CHAR *)ibuf, len,
  10962.                                              ftp_sched,
  10963. #ifdef KRB524
  10964.                                              ftp_cred.session,
  10965. #else /* KRB524 */
  10966.                                              &ftp_cred.session,
  10967. #endif /* KRB524 */
  10968.                                              &hisctladdr,
  10969.                                              &myctladdr,
  10970.                                              &ftp_msg_data
  10971.                                              );
  10972.                     }
  10973.                     if (kerror != KSUCCESS) {
  10974.                         printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
  10975.                                safe ? "modified" : "garbled",
  10976.                                safe ? "safe" : "priv",
  10977.                                krb_get_err_text(kerror));
  10978.                         n = '5';
  10979.                     } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
  10980.                         kerror = KFAILURE;
  10981.                         n = '5';
  10982.                         printf("reply data too large for buffer\n");
  10983.                     } else {
  10984.                         if (ftp_deb)
  10985.                           printf("%c:", safe ? 'S' : 'P');
  10986.                         memcpy(ibuf,ftp_msg_data.app_data,
  10987.                                ftp_msg_data.app_length);
  10988.                         ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
  10989.                                   FTP_BUFSIZ - ftp_msg_data.app_length);
  10990.                         continue;
  10991.                     }
  10992.                 }
  10993. #endif /* FTP_KRB4 */
  10994. #ifdef FTP_GSSAPI
  10995.                 else if (strcmp(auth_type, "GSSAPI") == 0) {
  10996.                     gss_buffer_desc xmit_buf, msg_buf;
  10997.                     OM_uint32 maj_stat, min_stat;
  10998.                     int conf_state;
  10999.                     xmit_buf.value = ibuf;
  11000.                     xmit_buf.length = len;
  11001.                     /* decrypt/verify the message */
  11002.                     conf_state = safe;
  11003.                     maj_stat = gss_unseal(&min_stat, gcontext,
  11004.                                           &xmit_buf, &msg_buf,
  11005.                                           &conf_state, NULL);
  11006.                     if (maj_stat != GSS_S_COMPLETE) {
  11007.                         user_gss_error(maj_stat, min_stat,
  11008.                                        "failed unsealing reply");
  11009.                         n = '5';
  11010.                     } else {
  11011.                         memcpy(ibuf, msg_buf.value, msg_buf.length);
  11012.                         ckstrncpy(&ibuf[msg_buf.length], "\r\n",
  11013.                                   FTP_BUFSIZ-msg_buf.length);
  11014.                         gss_release_buffer(&min_stat,&msg_buf);
  11015.                         if (ftp_deb)
  11016.                           printf("%c:", safe ? 'S' : 'P');
  11017.                         continue;
  11018.                     }
  11019.                 }
  11020. #endif /* FTP_GSSAPI */
  11021.                 /* Other auth types go here... */
  11022.             }
  11023.         } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
  11024.                    !xquiet && (vbm || (!auth && (n == '4' || n == '5')))) {
  11025. #ifdef NOCSETS
  11026.             printf("%c",c);
  11027. #else
  11028.             if (xlate) {
  11029.                 xlatec(0,c,rcs,lcs);
  11030.             } else {
  11031.                 printf("%c",c);
  11032.             }
  11033. #endif /* NOCSETS */
  11034.             fflush (stdout);
  11035.         }
  11036.         if (continuation && ftpcode != originalcode) {
  11037.             if (originalcode == 0)
  11038.               originalcode = ftpcode;
  11039.             continue;
  11040.         }
  11041.         *cp = '\0';
  11042.         if (n != '1')
  11043.           cpend = 0;
  11044.         signal(SIGINT,oldintr);
  11045.         if (ftpcode == 421 || originalcode == 421) {
  11046.         lostpeer();
  11047.         if (!xquiet && !ftp_deb)
  11048.           printf("%s\n",reply_buf);
  11049.         }
  11050.         if ((cancelfile != 0) &&
  11051. #ifndef ULTRIX3
  11052.             /* Ultrix 3.0 cc objects violently to this clause */
  11053.             (oldintr != cmdcancel) &&
  11054. #endif /* ULTRIX3 */
  11055.             (oldintr != SIG_IGN)) {
  11056.             if (oldintr)
  11057.               (*oldintr)(SIGINT);
  11058.         }
  11059.         if (reply_parse) {
  11060.             *reply_ptr = '\0';
  11061.             if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
  11062.                 reply_parse = reply_ptr + strlen(reply_parse);
  11063.                 if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
  11064.                   *reply_ptr = '\0';
  11065.             } else
  11066.               reply_parse = reply_ptr;
  11067.         }
  11068.         while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
  11069.           *cp-- = NUL;
  11070.         debug(F111,"ftp getreply",ftp_reply_str,n - '0');
  11071.         return(n - '0');
  11072.     } /* for (;;) */
  11073. }
  11074.  
  11075. #ifdef BSDSELECT
  11076. static int
  11077. #ifdef CK_ANSIC
  11078. empty(fd_set * mask, int sec)
  11079. #else
  11080. empty(mask, sec) fd_set * mask; int sec;
  11081. #endif /* CK_ANSIC */
  11082. {
  11083.     struct timeval t;
  11084.     t.tv_sec = (long) sec;
  11085.     t.tv_usec = 0L;
  11086.     debug(F100,"ftp empty calling select...","",0);
  11087. #ifdef INTSELECT
  11088.     x = select(32, (int *)mask, NULL, NULL, &t);
  11089. #else
  11090.     x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
  11091. #endif /* INTSELECT */
  11092.     debug(F101,"ftp empty select","",x);
  11093.     return(x);
  11094. }
  11095. #else /* BSDSELECT */
  11096. #ifdef IBMSELECT
  11097. static int
  11098. empty(mask, cnt, sec) int * mask, sec;
  11099.                       int   cnt;
  11100. {
  11101.     return(select(mask,cnt,0,0,sec*1000));
  11102. }
  11103. #endif /* IBMSELECT */
  11104. #endif /* BSDSELECT */
  11105.  
  11106. static sigtype
  11107. cancelsend(sig) int sig; {
  11108.     havesigint++;
  11109.     cancelgroup++;
  11110.     cancelfile = 0;
  11111.     printf(" Canceled...\n");
  11112.     secure_getc(0,1);            /* Initialize net input buffers */
  11113.     debug(F100,"ftp cancelsend caught SIGINT ","",0);
  11114.     fflush(stdout);
  11115. #ifndef OS2
  11116.     longjmp(sendcancel, 1);
  11117. #else
  11118.     PostCtrlCSem();
  11119. #endif /* OS2 */
  11120. }
  11121.  
  11122. static VOID
  11123. #ifdef CK_ANSIC
  11124. secure_error(char *fmt, ...)
  11125. #else
  11126. /* VARARGS1 */
  11127. secure_error(fmt, p1, p2, p3, p4, p5)
  11128.    char *fmt; int p1, p2, p3, p4, p5;
  11129. #endif /* CK_ANSIC */
  11130. {
  11131. #ifdef CK_ANSIC
  11132.     va_list ap;
  11133.  
  11134.     va_start(ap, fmt);
  11135.     vfprintf(stderr, fmt, ap);
  11136.     va_end(ap);
  11137. #else
  11138.     fprintf(stderr, fmt, p1, p2, p3, p4, p5);
  11139. #endif
  11140.     fprintf(stderr, "\n");
  11141. }
  11142.  
  11143. /*
  11144.  * Internal form of settype; changes current type in use with server
  11145.  * without changing our notion of the type for data transfers.
  11146.  * Used to change to and from ascii for listings.
  11147.  */
  11148. static VOID
  11149. changetype(newtype, show) int newtype, show; {
  11150.     int rc;
  11151.     char * s;
  11152.  
  11153.     if ((newtype == curtype) && typesent++)
  11154.       return;
  11155.     switch (newtype) {
  11156.       case FTT_ASC:
  11157.         s = "A";
  11158.         break;
  11159.       case FTT_BIN:
  11160.         s = "I";
  11161.         break;
  11162.       case FTT_TEN:
  11163.         s = "L 8";
  11164.         break;
  11165.       default:
  11166.         s = "I";
  11167.         break;
  11168.     }
  11169.     rc = ftpcmd("TYPE",s,-1,-1,show);
  11170.     if (rc == REPLY_COMPLETE)
  11171.       curtype = newtype;
  11172. }
  11173.  
  11174. /* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
  11175.  
  11176. static VOID
  11177. #ifdef CK_ANSIC
  11178. doftpsend(void * threadinfo)
  11179. #else
  11180. doftpsend(threadinfo) VOID * threadinfo;
  11181. #endif
  11182. {
  11183. #ifdef NTSIG
  11184.     if (threadinfo) {                   /* Thread local storage... */
  11185.         TlsSetValue(TlsIndex,threadinfo);
  11186.         debug(F100, "doftpsend called with threadinfo block","", 0);
  11187.     } else debug(F100, "doftpsend - threadinfo is NULL", "", 0);
  11188. #endif /* NTSIG */
  11189. #ifdef CK_LOGIN
  11190. #ifdef IKSD
  11191. #ifdef NT
  11192.     if (inserver)
  11193.       setntcreds();
  11194. #endif /* NT */
  11195. #endif /* IKSD */
  11196. #endif /* CK_LOGIN */
  11197.  
  11198.     if (initconn()) {
  11199. #ifndef NOHTTP
  11200.         int y = -1;
  11201.         /* debug(F101,"doftpsend","tcp_http_proxy",tcp_http_proxy); */
  11202.  
  11203.        /*  If the connection failed and we are using an HTTP Proxy
  11204.         *  and the reason for the failure was an authentication
  11205.         *  error, then we need to give the user to ability to
  11206.         *  enter a username and password, just like a browser.
  11207.         *
  11208.         *  I tried to do all of this within the netopen() call
  11209.         *  but it is much too much work.
  11210.         */
  11211.         while (y != 0 && tcp_http_proxy != NULL ) {
  11212.  
  11213.             if (tcp_http_proxy_errno == 401 ||
  11214.                  tcp_http_proxy_errno == 407 ) {
  11215.                 char uid[UIDBUFLEN];
  11216.                 char pwd[PWDSIZ];
  11217.                 struct txtbox tb[2];
  11218.                 int ok;
  11219.  
  11220.                 tb[0].t_buf = uid;
  11221.                 tb[0].t_len = UIDBUFLEN;
  11222.                 tb[0].t_lbl = "Proxy Userid: ";
  11223.                 tb[0].t_dflt = NULL;
  11224.                 tb[0].t_echo = 1;
  11225.                 tb[1].t_buf = pwd;
  11226.                 tb[1].t_len = 256;
  11227.                 tb[1].t_lbl = "Proxy Passphrase: ";
  11228.                 tb[1].t_dflt = NULL;
  11229.                 tb[1].t_echo = 2;
  11230.  
  11231.                 ok = uq_mtxt("Proxy Server Authentication Required\n",
  11232.                               NULL, 2, tb);
  11233.                 if (ok && uid[0]) {
  11234.                     char * proxy_user, * proxy_pwd;
  11235.  
  11236.                     proxy_user = tcp_http_proxy_user;
  11237.                     proxy_pwd  = tcp_http_proxy_pwd;
  11238.  
  11239.                     tcp_http_proxy_user = uid;
  11240.                     tcp_http_proxy_pwd = pwd;
  11241.  
  11242.                     y = initconn();
  11243.  
  11244.                     debug(F101,"doftpsend","initconn",y);
  11245.                     memset(pwd,0,PWDSIZ);
  11246.                     tcp_http_proxy_user = proxy_user;
  11247.                     tcp_http_proxy_pwd = proxy_pwd;
  11248.                 } else
  11249.                     break;
  11250.             } else
  11251.                 break;
  11252.         }
  11253.  
  11254.         if ( y != 0 ) {
  11255. #endif /* NOHTTP */
  11256.             signal(SIGINT, ftpsnd.oldintr);
  11257. #ifdef SIGPIPE
  11258.             if (ftpsnd.oldintp)
  11259.               signal(SIGPIPE, ftpsnd.oldintp);
  11260. #endif /* SIGPIPE */
  11261.             ftpcode = -1;
  11262.             zclose(ZIFILE);
  11263.             ftpsndret = -1;
  11264. #ifdef NTSIG
  11265.             ckThreadEnd(threadinfo);
  11266. #endif /* NTSIG */
  11267.             return;
  11268. #ifndef NOHTTP
  11269.         }
  11270. #endif /* NOHTTP */
  11271.     }
  11272.     ftpsndret = 0;
  11273. #ifdef NTSIG
  11274.      ckThreadEnd(threadinfo);
  11275. #endif /* NTSIG */
  11276. }
  11277.  
  11278. static VOID
  11279. #ifdef CK_ANSIC
  11280. failftpsend(void * threadinfo)
  11281. #else
  11282. failftpsend(threadinfo) VOID * threadinfo;
  11283. #endif /* CK_ANSIC */
  11284. {
  11285. #ifdef NTSIG
  11286.     if (threadinfo) {                   /* Thread local storage... */
  11287.         TlsSetValue(TlsIndex,threadinfo);
  11288.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11289.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11290. #endif /* NTSIG */
  11291. #ifdef CK_LOGIN
  11292. #ifdef IKSD
  11293. #ifdef NT
  11294.     if (inserver)
  11295.       setntcreds();
  11296. #endif /* NT */
  11297. #endif /* IKSD */
  11298. #endif /* CK_LOGIN */
  11299.  
  11300.     while (cpend) {
  11301.         ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11302.         debug(F111,"ftp sendrequest getreply","null command",ftpsnd.reply);
  11303.     }
  11304.     if (data >= 0) {
  11305. #ifdef CK_SSL
  11306.         if (ssl_ftp_data_active_flag) {
  11307.             SSL_shutdown(ssl_ftp_data_con);
  11308.             SSL_free(ssl_ftp_data_con);
  11309.             ssl_ftp_data_active_flag = 0;
  11310.             ssl_ftp_data_con = NULL;
  11311.         }
  11312. #endif /* CK_SSL */
  11313. #ifdef TCPIPLIB
  11314.         socket_close(data);
  11315. #else /* TCPIPLIB */
  11316. #ifdef USE_SHUTDOWN
  11317.         shutdown(data, 1+1);
  11318. #endif /* USE_SHUTDOWN */
  11319.         close(data);
  11320. #endif /* TCPIPLIB */
  11321.         data = -1;
  11322.         globaldin = -1;
  11323.     }
  11324.     if (ftpsnd.oldintr)
  11325.         signal(SIGINT,ftpsnd.oldintr);
  11326. #ifdef SIGPIPE
  11327.     if (ftpsnd.oldintp)
  11328.         signal(SIGPIPE,ftpsnd.oldintp);
  11329. #endif /* SIGPIPE */
  11330.     ftpcode = -1;
  11331. #ifndef OS2
  11332.     /* TEST ME IN K95 */
  11333.     if (havesigint) {
  11334.     havesigint = 0;
  11335.     debug(F100,"ftp failftpsend chain to trap()...","",0);
  11336.     if (ftpsnd.oldintr != SIG_IGN)
  11337.       (*ftpsnd.oldintr)(SIGINT);
  11338.     /* NOTREACHED (I hope!) */
  11339.     debug(F100,"ftp failftpsend return from trap()...","",0);
  11340.     }
  11341. #endif /* OS2 */
  11342. }
  11343.  
  11344. static VOID
  11345. #ifdef CK_ANSIC
  11346. failftpsend2(void * threadinfo)
  11347. #else
  11348. failftpsend2(threadinfo) VOID * threadinfo;
  11349. #endif /* CK_ANSIC */
  11350. {
  11351. #ifdef NTSIG
  11352.     if (threadinfo) {                   /* Thread local storage... */
  11353.         TlsSetValue(TlsIndex,threadinfo);
  11354.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11355.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11356. #endif /* NTSIG */
  11357. #ifdef CK_LOGIN
  11358. #ifdef IKSD
  11359. #ifdef NT
  11360.     if (inserver)
  11361.       setntcreds();
  11362. #endif /* NT */
  11363. #endif /* IKSD */
  11364. #endif /* CK_LOGIN */
  11365.  
  11366.     debug(F101,"ftp sendrequest canceled","",ftpsnd.bytes);
  11367.     tfc += ffc;
  11368. #ifdef GFTIMER
  11369.     fpfsecs = gftimer();
  11370. #endif /* GFTIMER */
  11371.     zclose(ZIFILE);
  11372. #ifdef PIPESEND
  11373.     if (sndfilter)
  11374.       pipesend = 0;
  11375. #endif /* PIPESEND */
  11376.     signal(SIGINT, ftpsnd.oldintr);
  11377. #ifdef SIGPIPE
  11378.     if (ftpsnd.oldintp)
  11379.       signal(SIGPIPE, ftpsnd.oldintp);
  11380. #endif /* SIGPIPE */
  11381.     if (!cpend) {
  11382.         ftpcode = -1;
  11383.         ftpsndret = -1;
  11384. #ifdef NTSIG
  11385.         ckThreadEnd(threadinfo);
  11386. #endif /* NTSIG */
  11387.         return;
  11388.     }
  11389.     if (data >= 0) {
  11390. #ifdef CK_SSL
  11391.         if (ssl_ftp_data_active_flag) {
  11392.             SSL_shutdown(ssl_ftp_data_con);
  11393.             SSL_free(ssl_ftp_data_con);
  11394.             ssl_ftp_data_active_flag = 0;
  11395.             ssl_ftp_data_con = NULL;
  11396.         }
  11397. #endif /* CK_SSL */
  11398. #ifdef TCPIPLIB
  11399.         socket_close(data);
  11400. #else /* TCPIPLIB */
  11401. #ifdef USE_SHUTDOWN
  11402.         shutdown(data, 1+1);
  11403. #endif /* USE_SHUTDOWN */
  11404.         close(data);
  11405. #endif /* TCPIPLIB */
  11406.         data = -1;
  11407.         globaldin = -1;
  11408.     }
  11409.     if (dout) {
  11410. #ifdef TCPIPLIB
  11411.         socket_close(dout);
  11412. #else /* TCPIPLIB */
  11413. #ifdef USE_SHUTDOWN
  11414.         shutdown(dout, 1+1);
  11415. #endif /* USE_SHUTDOWN */
  11416.         close(dout);
  11417. #endif /* TCPIPLIB */
  11418.     }
  11419.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11420.     ftpcode = -1;
  11421.     ftpsndret = -1;
  11422.  
  11423. #ifndef OS2
  11424.     /* TEST ME IN K95 */
  11425.     if (havesigint) {
  11426.     havesigint = 0;
  11427.     debug(F100,"ftp failftpsend2 chain to trap()...","",0);
  11428.     if (ftpsnd.oldintr != SIG_IGN)
  11429.       (*ftpsnd.oldintr)(SIGINT);
  11430.     /* NOTREACHED (I hope!) */
  11431.     debug(F100,"ftp failftpsend2 return from trap()...","",0);
  11432.     }
  11433. #endif /* OS2 */
  11434. }
  11435.  
  11436. static VOID
  11437. #ifdef CK_ANSIC
  11438. doftpsend2(void * threadinfo)
  11439. #else
  11440. doftpsend2(threadinfo) VOID * threadinfo;
  11441. #endif
  11442. {
  11443.     register int c, d = 0;
  11444.     int n, t, x, notafile, unique = 0;
  11445.     char *buf, *bufp;
  11446.     
  11447. #ifdef NTSIG
  11448.     if (threadinfo) {                   /* Thread local storage... */
  11449.         TlsSetValue(TlsIndex,threadinfo);
  11450.         debug(F100, "doftpsend2 called with threadinfo block","", 0);
  11451.     } else debug(F100, "doftpsend2 - threadinfo is NULL", "", 0);
  11452. #endif /* NTSIG */
  11453. #ifdef CK_LOGIN
  11454. #ifdef IKSD
  11455. #ifdef NT
  11456.     if (inserver)
  11457.       setntcreds();
  11458. #endif /* NT */
  11459. #endif /* IKSD */
  11460. #endif /* CK_LOGIN */
  11461.  
  11462.     buf = ftpsndbuf;            /* (not on stack) */
  11463.  
  11464.     unique = strcmp(ftpsnd.cmd,"STOU") ? 0 : 1;
  11465.     notafile = sndarray || pipesend;
  11466.  
  11467. #ifdef FTP_RESTART
  11468.     if (ftpsnd.restart && ((curtype == FTT_BIN) || (alike > 0))) {
  11469.         char * p;
  11470.         changetype(FTT_BIN,0);          /* Change to binary */
  11471.  
  11472.         /* Ask for remote file's size */
  11473.         x = ftpcmd("SIZE",ftpsnd.remote,ftpsnd.incs,ftpsnd.outcs,ftp_vbm);
  11474.  
  11475.         if (x == REPLY_COMPLETE) {      /* Have ftpsnd.reply */
  11476.             p = &ftp_reply_str[4];      /* Parse it */
  11477.             while (isdigit(*p)) {
  11478.                 sendstart = sendstart * 10 + (int)(*p - '0');
  11479.                 p++;
  11480.             }
  11481.             if (*p && *p != CR) {       /* Bad number */
  11482.                 debug(F110,"doftpsend2 bad size",ftp_reply_str,0);
  11483.                 sendstart = (CK_OFF_T)0;
  11484.             } else if (sendstart > fsize) { /* Remote file bigger than local */
  11485.                 debug(F110,"doftpsend2 big size",ckfstoa(fsize),sendstart);
  11486.                 sendstart = (CK_OFF_T)0;
  11487.             }
  11488.         /* Local is newer */
  11489.             debug(F111,"doftpsend2 size",ftpsnd.remote,sendstart);
  11490.             if (chkmodtime(ftpsnd.local,ftpsnd.remote,0) == 2) {
  11491.                 debug(F110,"doftpsend2 date mismatch",ftp_reply_str,0);
  11492.                 sendstart = (CK_OFF_T)0; /* Send the whole file */
  11493.             }
  11494.         }
  11495.         changetype(ftp_typ,0);          /* Change back to appropriate type */
  11496.         if (sendstart > (CK_OFF_T)0) {    /* Still restarting? */
  11497.             if (sendstart == fsize) {   /* Same size - no need to send */
  11498.                 debug(F111,"doftpsend2 /restart SKIP",
  11499.               ckfstoa(fsize),sendstart);
  11500.                 zclose(ZIFILE);
  11501.                 ftpsndret = SKP_RES;
  11502. #ifdef NTSIG
  11503.                 ckThreadEnd(threadinfo);
  11504. #endif /* NTSIG */
  11505.                 return;
  11506.             }
  11507.             errno = 0;                  /* Restart needed, seek to the spot */
  11508.             if (zfseek((long)sendstart) < 0) {
  11509.                 debug(F111,"doftpsend2 zfseek fails",
  11510.               ftpsnd.local,sendstart);
  11511.                 fprintf(stderr, "FSEEK: %s: %s\n", ftpsnd.local, ck_errstr());
  11512.                 sendstart = 0;
  11513.                 zclose(ZIFILE);
  11514.                 ftpsndret = -1;
  11515. #ifdef NTSIG
  11516.                 ckThreadEnd(threadinfo);
  11517. #endif /* NTSIG */
  11518.                 return;
  11519.             }
  11520. #ifdef COMMENT
  11521.             debug(F111,"doftpsend2 zfseek ok",ftpsnd.local,sendstart);
  11522.             x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
  11523.             if (x != REPLY_CONTINUE) {
  11524.                 sendstart = 0;
  11525.                 zclose(ZIFILE);
  11526.                 ftpsndret = -1;
  11527. #ifdef NTSIG
  11528.                 ckThreadEnd(threadinfo);
  11529. #endif /* NTSIG */
  11530.                 return;
  11531.             } else {
  11532.                 ftpsnd.cmd = "STOR";
  11533.             }
  11534. #else
  11535.             sendmode = SM_RESEND;
  11536.             ftpsnd.cmd = "APPE";
  11537. #endif /* COMMENT */
  11538.             /* sendstart = (CK_OFF_T)0; */
  11539.         }
  11540.     }
  11541. #endif /* FTP_RESTART */
  11542.  
  11543.     if (unique && !stouarg)        /* If we know STOU accepts no arg */
  11544.       ftpsnd.remote = NULL;        /* don't include one. */
  11545.  
  11546.     x = ftpcmd(ftpsnd.cmd, ftpsnd.remote, ftpsnd.incs, ftpsnd.outcs, ftp_vbm);
  11547.     debug(F111,"doftpsend2 ftpcode",ftpsnd.cmd,ftpcode);
  11548.  
  11549.     if (x != REPLY_PRELIM && unique) {
  11550.     /*
  11551.       RFC959 says STOU does not take an argument.  But every FTP server
  11552.       I've encountered but one accepts the arg and constructs the unique
  11553.       name from it, which is better than making up a totally random name
  11554.       for the file, which is what RFC959 calls for.  Especially because
  11555.       there is no way for the client to find out the name chosen by the
  11556.       server.  So we try STOU with the argument first, which works with
  11557.       most servers, and if it fails we retry it without the arg, for
  11558.       the benefit of the one picky server that is not "liberal in what
  11559.       it accepts" UNLESS the first STOU got a 502 code ("not implemented")
  11560.       which means STOU is not accepted, period.
  11561.     */
  11562.     if ((x == 5) && stouarg && (ftpcode != 502)) {
  11563.         x = ftpcmd(ftpsnd.cmd,NULL,ftpsnd.incs,ftpsnd.outcs,ftp_vbm); 
  11564.         if (x == REPLY_PRELIM)    /* If accepted */
  11565.           stouarg = 0;        /* flag no STOU arg for this server */
  11566.     }
  11567.     }
  11568.     if (x != REPLY_PRELIM) {
  11569.         signal(SIGINT, ftpsnd.oldintr);
  11570. #ifdef SIGPIPE
  11571.         if (ftpsnd.oldintp)
  11572.           signal(SIGPIPE, ftpsnd.oldintp);
  11573. #endif /* SIGPIPE */
  11574.         zclose(ZIFILE);
  11575. #ifdef PIPESEND
  11576.         if (sndfilter)
  11577.           pipesend = 0;
  11578. #endif /* PIPESEND */
  11579.         ftpsndret = -1;
  11580. #ifdef NTSIG
  11581.         ckThreadEnd(threadinfo);
  11582. #endif /* NTSIG */
  11583.         return;
  11584.     }
  11585.     dout = dataconn(ftpsnd.lmode);             /* Get data connection */
  11586.     if (dout == -1) {
  11587.         failftpsend2(threadinfo);
  11588. #ifdef NTSIG
  11589.         ckThreadEnd(threadinfo);
  11590. #endif /* NTSIG */
  11591.         return;
  11592.     }
  11593.     /* Initialize per-file stats */
  11594.     ffc = (CK_OFF_T)0;            /* Character counter */
  11595.     cps = oldcps = 0L;                  /* Thruput */
  11596. #ifdef GFTIMER
  11597.     rftimer();                          /* reset f.p. timer */
  11598. #endif /* GFTIMER */
  11599.  
  11600. #ifdef SIGPIPE
  11601.     ftpsnd.oldintp = signal(SIGPIPE, SIG_IGN);
  11602. #endif /* SIGPIPE */
  11603.     switch (curtype) {
  11604.       case FTT_BIN:                     /* Binary mode */
  11605.       case FTT_TEN:
  11606.         errno = d = 0;
  11607.         while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ - 1)) > 0 && !cancelfile) {
  11608.             ftpsnd.bytes += n;
  11609.             ffc += n;
  11610.             debug(F111,"doftpsend2 zxin",ckltoa(n),ffc);
  11611.             hexdump("doftpsend2 zxin",buf,16);
  11612. #ifdef CK_SSL
  11613.             if (ssl_ftp_data_active_flag) {
  11614.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11615.                     if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
  11616.                       break;
  11617.                     spackets++;
  11618.                     pktnum++;
  11619.                     if (fdispla != XYFD_B) {
  11620.                         spktl = d;
  11621.                         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  11622.                     }
  11623.                 }
  11624.             } else {
  11625. #endif /* CK_SSL */
  11626.                 for (bufp = buf; n > 0; n -= d, bufp += d) {
  11627.                     if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
  11628.                         || iscanceled())
  11629.                       break;
  11630.                     spackets++;
  11631.                     pktnum++;
  11632.                     if (fdispla != XYFD_B) {
  11633.                         spktl = d;
  11634.                         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  11635.                     }
  11636.                 }
  11637. #ifdef CK_SSL
  11638.             }
  11639. #endif /* CK_SSL */
  11640.             if (d <= 0)
  11641.               break;
  11642.         }
  11643.         if (n < 0)
  11644.           fprintf(stderr, "local: %s: %s\n", ftpsnd.local, ck_errstr());
  11645.         if (d < 0 || (d = secure_flush(dout)) < 0) {
  11646.             if (d == -1 && errno && errno != EPIPE)
  11647.               perror("netout");
  11648.             ftpsnd.bytes = -1;
  11649.         }
  11650.         break;
  11651.  
  11652.       case FTT_ASC:                     /* Text mode */
  11653. #ifndef NOCSETS
  11654.         if (ftpsnd.xlate) {             /* With translation */
  11655.             initxlate(ftpsnd.incs,ftpsnd.outcs);
  11656.             while (!cancelfile) {
  11657.                 if ((c0 = xgnbyte(FC_UCS2,ftpsnd.incs,NULL)) < 0) break;
  11658.                 if ((x = xpnbyte(c0,TC_UCS2,ftpsnd.outcs,xxout)) < 0) break;
  11659.             }
  11660.         } else {
  11661. #endif /* NOCSETS */
  11662.             /* Text mode, no translation */
  11663.             while (((c = zminchar()) > -1) && !cancelfile) {
  11664.                 ffc++;
  11665.         if (xxout(c) < 0)
  11666.           break;
  11667.             }
  11668.             d = 0;
  11669. #ifndef NOCSETS
  11670.         }
  11671. #endif /* NOCSETS */
  11672.         if (dout == -1 || (d = secure_flush(dout)) < 0) {
  11673.             if (d == -1 && errno && errno != EPIPE)
  11674.               perror("netout");
  11675.             ftpsnd.bytes = -1;
  11676.         }
  11677.         break;
  11678.     }
  11679.     tfc += ffc;                         /* Total file chars */
  11680. #ifdef GFTIMER
  11681.     fpfsecs = gftimer();
  11682. #endif /* GFTIMER */
  11683.     zclose(ZIFILE);                     /* Close input file */
  11684. #ifdef PIPESEND
  11685.     if (sndfilter)                      /* Undo this (it's per file) */
  11686.       pipesend = 0;
  11687. #endif /* PIPESEND */
  11688.  
  11689. #ifdef CK_SSL
  11690.         if (ssl_ftp_data_active_flag) {
  11691.             SSL_shutdown(ssl_ftp_data_con);
  11692.             SSL_free(ssl_ftp_data_con);
  11693.             ssl_ftp_data_active_flag = 0;
  11694.             ssl_ftp_data_con = NULL;
  11695.         }
  11696. #endif /* CK_SSL */
  11697.  
  11698. #ifdef TCPIPLIB
  11699.     socket_close(dout);                 /* Close data connection */
  11700. #else /* TCPIPLIB */
  11701. #ifdef USE_SHUTDOWN
  11702.     shutdown(dout, 1+1);
  11703. #endif /* USE_SHUTDOWN */
  11704.     close(dout);
  11705. #endif /* TCPIPLIB */
  11706.     ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
  11707.     signal(SIGINT, ftpsnd.oldintr);            /* Put back interrupts */
  11708. #ifdef SIGPIPE
  11709.     if (ftpsnd.oldintp)
  11710.       signal(SIGPIPE, ftpsnd.oldintp);
  11711. #endif /* SIGPIPE */
  11712.     if (ftpsnd.reply == REPLY_TRANSIENT || ftpsnd.reply == REPLY_ERROR) {
  11713.         debug(F101,"doftpsend2 ftpsnd.reply","",ftpsnd.reply);
  11714.         ftpsndret = -1;
  11715. #ifdef NTSIG
  11716.         ckThreadEnd(threadinfo);
  11717. #endif /* NTSIG */
  11718.         return;
  11719.     } else if (cancelfile) {
  11720.         debug(F101,"doftpsend2 canceled","",ftpsnd.bytes);
  11721.         ftpsndret = -1;
  11722. #ifdef NTSIG
  11723.         ckThreadEnd(threadinfo);
  11724. #endif /* NTSIG */
  11725.         return;
  11726.     }
  11727.     debug(F101,"doftpsend2 ok","",ftpsnd.bytes);
  11728.     ftpsndret = 0;
  11729. #ifdef NTSIG
  11730.      ckThreadEnd(threadinfo);
  11731. #endif /* NTSIG */
  11732. }
  11733.  
  11734. static int
  11735. sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
  11736.     char *cmd, *local, *remote; int xlate, incs, outcs, restart;
  11737. {
  11738.     if (!remote) remote = "";           /* Check args */
  11739.     if (!*remote) remote = local;
  11740.     if (!local) local = "";
  11741.     if (!*local) return(-1);
  11742.     if (!cmd) cmd = "";
  11743.     if (!*cmd) cmd = "STOR";
  11744.  
  11745.     debug(F111,"ftp sendrequest restart",local,restart);
  11746.  
  11747.     nout = 0;                           /* Init output buffer count */
  11748.     ftpsnd.bytes = 0;                   /* File input byte count */
  11749.     dout = -1;
  11750.  
  11751. #ifdef FTP_PROXY
  11752.     if (proxy) {
  11753.         proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
  11754.         return(0);
  11755.     }
  11756. #endif /* FTP_PROXY */
  11757.  
  11758.     changetype(ftp_typ,0);              /* Change type for this file */
  11759.  
  11760.     ftpsnd.oldintr = NULL;        /* Set up interrupt handler */
  11761.     ftpsnd.oldintp = NULL;
  11762.     ftpsnd.restart = restart;
  11763.     ftpsnd.xlate = xlate;
  11764.     ftpsnd.lmode = "wb";
  11765.  
  11766. #ifdef PIPESEND                         /* Use Kermit API for file i/o... */
  11767.     if (sndfilter) {
  11768.         char * p = NULL, * q;
  11769. #ifndef NOSPL
  11770.         int n = CKMAXPATH;
  11771.         if (cmd_quoting && (p = (char *) malloc(n + 1))) {
  11772.             q = p;
  11773.             debug(F110,"sendrequest pipesend filter",sndfilter,0);
  11774.             zzstring(sndfilter,&p,&n);
  11775.             debug(F111,"sendrequest pipename",q,n);
  11776.             if (n <= 0) {
  11777.                 printf("?Sorry, send filter + filename too long, %d max.\n",
  11778.                        CKMAXPATH
  11779.                        );
  11780.                 free(q);
  11781.                 return(-1);
  11782.             }
  11783.             ckstrncpy(filnam,q,CKMAXPATH+1);
  11784.             free(q);
  11785.             local = filnam;
  11786.         }
  11787. #endif /* NOSPL */
  11788.     }
  11789.  
  11790.     if (sndfilter)                      /* If sending thru a filter */
  11791.       pipesend = 1;                     /* set this for open and i/o */
  11792. #endif /* PIPESEND */
  11793.     
  11794.     if (openi(local) == 0)              /* Try to open the input file */
  11795.         return(-1);
  11796.  
  11797.     ftpsndret = 0;
  11798.     ftpsnd.incs = incs;
  11799.     ftpsnd.outcs = outcs;
  11800.     ftpsnd.cmd = cmd;
  11801.     ftpsnd.local = local;
  11802.     ftpsnd.remote = remote;
  11803.     ftpsnd.oldintr = signal(SIGINT, cancelsend);
  11804.     havesigint = 0;
  11805.  
  11806.     if (cc_execute(ckjaddr(sendcancel), doftpsend, failftpsend) < 0)
  11807.       return(-1);
  11808.     if (ftpsndret < 0)
  11809.       return(-1);
  11810.     if (cc_execute(ckjaddr(sendcancel), doftpsend2, failftpsend2) < 0)
  11811.       return(-1);
  11812.  
  11813.     return(ftpsndret);
  11814. }
  11815.  
  11816. static sigtype
  11817. cancelrecv(sig) int sig; {
  11818.     havesigint++;
  11819.     cancelfile = 0;
  11820.     cancelgroup++;
  11821.     secure_getc(0,1);            /* Initialize net input buffers */
  11822.     printf(" Canceling...\n");
  11823.     debug(F100,"ftp cancelrecv caught SIGINT","",0);
  11824.     fflush(stdout);
  11825.     if (fp_nml) {
  11826.         if (fp_nml != stdout)
  11827.           fclose(fp_nml);
  11828.         fp_nml = NULL;
  11829.     }
  11830. #ifndef OS2
  11831.     longjmp(recvcancel, 1);
  11832. #else
  11833.     PostCtrlCSem();
  11834. #endif /* OS2 */
  11835. }
  11836.  
  11837. /* Argumentless front-end for secure_getc() */
  11838.  
  11839. static int
  11840. netgetc() {
  11841.     return(secure_getc(globaldin,0));
  11842. }
  11843.  
  11844. /* Returns -1 on failure, 0 on success, 1 if file skipped */
  11845.  
  11846. /*
  11847.   Sets ftpcode < 0 on failure if failure reason is not server reply code:
  11848.     -1: interrupted by user.
  11849.     -2: error opening or writing output file (reason in errno).
  11850.     -3: failure to make data connection.
  11851.     -4: network read error (reason in errno).
  11852. */
  11853.  
  11854. struct xx_ftprecv {
  11855.     int reply;
  11856.     int fcs;
  11857.     int rcs;
  11858.     int recover;
  11859.     int xlate;
  11860.     int din;
  11861.     int is_retr;
  11862.     sig_t oldintr, oldintp;
  11863.     char * cmd;
  11864.     char * local;
  11865.     char * remote;
  11866.     char * lmode;
  11867.     char * pipename;
  11868.     int    tcrflag;
  11869.     CK_OFF_T localsize;
  11870. };
  11871. static struct xx_ftprecv ftprecv;
  11872.  
  11873. static int ftprecvret = 0;
  11874.  
  11875. static VOID
  11876. #ifdef CK_ANSIC
  11877. failftprecv(VOID * threadinfo)
  11878. #else
  11879. failftprecv(threadinfo) VOID * threadinfo;
  11880. #endif /* CK_ANSIC */
  11881. {
  11882. #ifdef NTSIG
  11883.     if (threadinfo) {                   /* Thread local storage... */
  11884.         TlsSetValue(TlsIndex,threadinfo);
  11885.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11886.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11887. #endif /* NTSIG */
  11888.  
  11889. #ifdef CK_LOGIN
  11890. #ifdef IKSD
  11891. #ifdef NT
  11892.     if (inserver)
  11893.       setntcreds();
  11894. #endif /* NT */
  11895. #endif /* IKSD */
  11896. #endif /* CK_LOGIN */
  11897.  
  11898.     while (cpend) {
  11899.         ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  11900.     }
  11901.     if (data >= 0) {
  11902. #ifdef CK_SSL
  11903.         if (ssl_ftp_data_active_flag) {
  11904.             SSL_shutdown(ssl_ftp_data_con);
  11905.             SSL_free(ssl_ftp_data_con);
  11906.             ssl_ftp_data_active_flag = 0;
  11907.             ssl_ftp_data_con = NULL;
  11908.         }
  11909. #endif /* CK_SSL */
  11910. #ifdef TCPIPLIB
  11911.         socket_close(data);
  11912. #else /* TCPIPLIB */
  11913. #ifdef USE_SHUTDOWN
  11914.         shutdown(data, 1+1);
  11915. #endif /* USE_SHUTDOWN */
  11916.         close(data);
  11917. #endif /* TCPIPLIB */
  11918.         data = -1;
  11919.         globaldin = -1;
  11920.     }
  11921.     if (ftprecv.oldintr)
  11922.       signal(SIGINT, ftprecv.oldintr);
  11923.     ftpcode = -1;
  11924.     ftprecvret = -1;
  11925.  
  11926. #ifndef OS2
  11927.     /* TEST ME IN K95 */
  11928.     if (havesigint) {
  11929.     havesigint = 0;
  11930.     debug(F100,"ftp failftprecv chain to trap()...","",0);
  11931.     if (ftprecv.oldintr != SIG_IGN)
  11932.       (*ftprecv.oldintr)(SIGINT);
  11933.     /* NOTREACHED (I hope!) */
  11934.     debug(F100,"ftp failftprecv return from trap()...","",0);
  11935.     }
  11936. #endif /* OS2 */
  11937.     return;
  11938. }
  11939.  
  11940. static VOID
  11941. #ifdef CK_ANSIC
  11942. doftprecv(VOID * threadinfo)
  11943. #else
  11944. doftprecv(threadinfo) VOID * threadinfo;
  11945. #endif /* CK_ANSIC */
  11946. {
  11947. #ifdef NTSIG
  11948.     if (threadinfo) {                   /* Thread local storage... */
  11949.         TlsSetValue(TlsIndex,threadinfo);
  11950.         debug(F100, "docmdfile called with threadinfo block","", 0);
  11951.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  11952. #endif /* NTSIG */
  11953. #ifdef CK_LOGIN
  11954. #ifdef IKSD
  11955. #ifdef NT
  11956.     if (inserver)
  11957.       setntcreds();
  11958. #endif /* NT */
  11959. #endif /* IKSD */
  11960. #endif /* CK_LOGIN */
  11961.  
  11962. #ifndef COMMENT
  11963.     if (!out2screen && !ftprecv.pipename) {
  11964.     int x;
  11965.     char * local;
  11966.     local = ftprecv.local;
  11967.     x = zchko(local);
  11968.         if (x < 0) {
  11969.             if ((!dpyactive || ftp_deb))
  11970.               fprintf(stderr,
  11971.               "Temporary file %s: %s\n", ftprecv.local, ck_errstr());
  11972.             signal(SIGINT, ftprecv.oldintr);
  11973.             ftpcode = -2;
  11974.             ftprecvret = -1;
  11975. #ifdef NTSIG
  11976.             ckThreadEnd(threadinfo);
  11977. #endif /* NTSIG */
  11978.             return;
  11979.         }
  11980.     }
  11981. #endif /* COMMENT */
  11982.     changetype((!ftprecv.is_retr) ? FTT_ASC : ftp_typ, 0);
  11983.     if (initconn()) {                   /* Initialize the data connection */
  11984.         signal(SIGINT, ftprecv.oldintr);
  11985.         ftpcode = -1;
  11986.         ftprecvret = -3;
  11987. #ifdef NTSIG
  11988.         ckThreadEnd(threadinfo);
  11989. #endif /* NTSIG */
  11990.         return;
  11991.     }
  11992.     secure_getc(0,1);            /* Initialize net input buffers */
  11993.     ftprecvret = 0;
  11994.  
  11995. #ifdef NTSIG
  11996.     ckThreadEnd(threadinfo);
  11997. #endif /* NTSIG */
  11998. }
  11999.  
  12000. static VOID
  12001. #ifdef CK_ANSIC
  12002. failftprecv2(VOID * threadinfo)
  12003. #else
  12004. failftprecv2(threadinfo) VOID * threadinfo;
  12005. #endif /* CK_ANSIC */
  12006. {
  12007. #ifdef NTSIG
  12008.     if (threadinfo) {                   /* Thread local storage... */
  12009.         TlsSetValue(TlsIndex,threadinfo);
  12010.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12011.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12012. #endif /* NTSIG */
  12013. #ifdef CK_LOGIN
  12014. #ifdef IKSD
  12015. #ifdef NT
  12016.     if (inserver)
  12017.       setntcreds();
  12018. #endif /* NT */
  12019. #endif /* IKSD */
  12020. #endif /* CK_LOGIN */
  12021.  
  12022.     /* Cancel using RFC959 recommended IP,SYNC sequence  */
  12023.  
  12024.     debug(F100,"ftp recvrequest CANCEL","",0);
  12025. #ifdef GFTIMER
  12026.     fpfsecs = gftimer();
  12027. #endif /* GFTIMER */
  12028. #ifdef SIGPIPE
  12029.     if (ftprecv.oldintp)
  12030.       signal(SIGPIPE, ftprecv.oldintr);
  12031. #endif /* SIGPIPE */
  12032.     signal(SIGINT, SIG_IGN);
  12033.     if (!cpend) {
  12034.         ftpcode = -1;
  12035.         signal(SIGINT, ftprecv.oldintr);
  12036.         ftprecvret = -1;
  12037. #ifdef NTSIG
  12038.         ckThreadEnd(threadinfo);
  12039. #endif /* NTSIG */
  12040.         return;
  12041.     }
  12042.     cancel_remote(ftprecv.din);
  12043.     if (ftpcode > -1)
  12044.       ftpcode = -1;
  12045.     if (data >= 0) {
  12046. #ifdef CK_SSL
  12047.         if (ssl_ftp_data_active_flag) {
  12048.             SSL_shutdown(ssl_ftp_data_con);
  12049.             SSL_free(ssl_ftp_data_con);
  12050.             ssl_ftp_data_active_flag = 0;
  12051.             ssl_ftp_data_con = NULL;
  12052.         }
  12053. #endif /* CK_SSL */
  12054. #ifdef TCPIPLIB
  12055.         socket_close(data);
  12056. #else /* TCPIPLIB */
  12057. #ifdef USE_SHUTDOWN
  12058.         shutdown(data, 1+1);
  12059. #endif /* USE_SHUTDOWN */
  12060.         close(data);
  12061. #endif /* TCPIPLIB */
  12062.         data = -1;
  12063.         globaldin = -1;
  12064.     }
  12065.     if (!out2screen) {
  12066.     int x = 0;
  12067.     debug(F111,"ftp failrecv2 zclose",ftprecv.local,keep);
  12068.     zclose(ZOFILE);
  12069.     switch (keep) {            /* which is... */
  12070.       case SET_AUTO:        /* AUTO */
  12071.         if (curtype == FTT_ASC)    /* Delete file if TYPE A. */
  12072.           x = 1;
  12073.         break;
  12074.       case SET_OFF:            /* DISCARD */
  12075.         x = 1;            /* Delete file, period. */
  12076.         break;
  12077.       default:            /* KEEP */
  12078.         break;
  12079.     }
  12080.     if (x) {
  12081.         x = zdelet(ftprecv.local);
  12082.         debug(F111,"ftp failrecv2 delete incomplete",ftprecv.local,x);
  12083.     }
  12084.     }
  12085.     if (ftprecv.din) {
  12086. #ifdef TCPIPLIB
  12087.         socket_close(ftprecv.din);
  12088. #else /* TCPIPLIB */
  12089. #ifdef USE_SHUTDOWN
  12090.         shutdown(ftprecv.din, 1+1);
  12091. #endif /* USE_SHUTDOWN */
  12092.         close(ftprecv.din);
  12093. #endif /* TCPIPLIB */
  12094.     }
  12095.     signal(SIGINT, ftprecv.oldintr);
  12096.     ftprecvret = -1;
  12097.  
  12098.     if (havesigint) {
  12099.     havesigint = 0;
  12100.     debug(F100,"FTP failftprecv2 chain to trap()...","",0);
  12101. #ifdef OS2
  12102.         debug(F100,"FTP failftprecv2 PostCtrlCSem()...","",0);
  12103.         PostCtrlCSem();
  12104. #else /* OS2 */
  12105.     if (ftprecv.oldintr != SIG_IGN)
  12106.       (*ftprecv.oldintr)(SIGINT);
  12107.     /* NOTREACHED (I hope!) */
  12108.     debug(F100,"ftp failftprecv2 return from trap()...","",0);
  12109. #endif /* OS2 */
  12110.     }
  12111. }
  12112.  
  12113. static VOID
  12114. #ifdef CK_ANSIC
  12115. doftprecv2(VOID * threadinfo)
  12116. #else
  12117. doftprecv2(threadinfo) VOID * threadinfo;
  12118. #endif /* CK_ANSIC */
  12119. {
  12120.     register int c, d;
  12121.     CK_OFF_T bytes = (CK_OFF_T)0;
  12122.     int bare_lfs = 0;
  12123.     int blksize = 0;
  12124.     ULONG start = 0L, stop;
  12125.     char * p;
  12126.     static char * rcvbuf = NULL;
  12127.     static int rcvbufsiz = 0;
  12128. #ifdef CK_URL
  12129.     char newname[CKMAXPATH+1];        /* For file dialog */
  12130. #endif /* CK_URL */
  12131.     extern int adl_ask;
  12132.  
  12133.     ftprecv.din = -1;
  12134. #ifdef NTSIG
  12135.     if (threadinfo) {                   /* Thread local storage... */
  12136.         TlsSetValue(TlsIndex,threadinfo);
  12137.         debug(F100, "docmdfile called with threadinfo block","", 0);
  12138.     } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
  12139. #endif /* NTSIG */
  12140. #ifdef CK_LOGIN
  12141. #ifdef IKSD
  12142. #ifdef NT
  12143.     if (inserver)
  12144.       setntcreds();
  12145. #endif /* NT */
  12146. #endif /* IKSD */
  12147. #endif /* CK_LOGIN */
  12148.  
  12149.     if (ftprecv.recover) {                      /* Initiate recovery */
  12150.         x = ftpcmd("REST",ckfstoa(ftprecv.localsize),-1,-1,ftp_vbm);
  12151.         debug(F111,"ftp reply","REST",x);
  12152.         if (x == REPLY_CONTINUE) {
  12153.             ftprecv.lmode = "ab";
  12154.             rs_len = ftprecv.localsize;
  12155.         } else {
  12156.             ftprecv.recover = 0;
  12157.         }
  12158.     }
  12159.     /* IMPORTANT: No FTP commands can come between REST and RETR! */
  12160.  
  12161.     debug(F111,"ftp recvrequest recover E",ftprecv.remote,ftprecv.recover);
  12162.  
  12163.     /* Send the command and get reply */
  12164.     debug(F110,"ftp recvrequest cmd",ftprecv.cmd,0);
  12165.     debug(F110,"ftp recvrequest remote",ftprecv.remote,0);
  12166.  
  12167.     if (ftpcmd(ftprecv.cmd,ftprecv.remote,ftprecv.fcs,ftprecv.rcs,ftp_vbm)
  12168.     != REPLY_PRELIM) {
  12169.         signal(SIGINT, ftprecv.oldintr); /* Bad reply, fail. */
  12170.         ftprecvret = -1;        /* ftpcode is set by ftpcmd() */
  12171. #ifdef NTSIG
  12172.         ckThreadEnd(threadinfo);
  12173. #endif /* NTSIG */
  12174.         return;
  12175.     }
  12176.     ftprecv.din = dataconn("r");        /* Good reply, open data connection */
  12177.     globaldin = ftprecv.din;            /* Global copy of file descriptor */
  12178.     if (ftprecv.din == -1) {            /* Check for failure */
  12179.         ftpcode = -3;                   /* Code for no data connection */
  12180.         ftprecvret = -1;
  12181. #ifdef NTSIG
  12182.         ckThreadEnd(threadinfo);
  12183. #endif /* NTSIG */
  12184.         return;
  12185.     }
  12186. #ifdef CK_URL
  12187.     /* In K95 GUI put up a file box */
  12188.     if (haveurl && g_url.pth && adl_ask    ) { /* Downloading from a URL */
  12189.     int x;
  12190.     char * preface =
  12191. "\r\nIncoming file from FTP server...\r\n\
  12192. Please confirm output file specification or supply an alternative:";
  12193.  
  12194.     x = uq_file(preface,        /* K95 GUI: Put up file box. */
  12195.             NULL,
  12196.             4,
  12197.             NULL,
  12198.             ftprecv.local ? ftprecv.local : ftprecv.remote,
  12199.             newname,
  12200.             CKMAXPATH+1
  12201.             );
  12202.     if (x > 0) {
  12203.         ftprecv.local = newname;    /* Substitute user's file name */
  12204.         if (x == 2)            /* And append if user said to */
  12205.           ftprecv.lmode = "ab";
  12206.     }
  12207.     }
  12208. #endif /* CK_URL */
  12209.     x = 1;                              /* Output file open OK? */
  12210.     if (ftprecv.pipename) {        /* Command */
  12211.         x = zxcmd(ZOFILE,ftprecv.pipename);
  12212.         debug(F111,"ftp recvrequest zxcmd",ftprecv.pipename,x);
  12213.     } else if (!out2screen) {           /* File */
  12214.         struct filinfo xx;
  12215.         xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  12216.         xx.typ = 0; xx.os_specific = NUL; xx.lblopts = 0;
  12217.     /* Append or New */
  12218.         xx.dsp = !strcmp(ftprecv.lmode,"ab") ? XYFZ_A : XYFZ_N;
  12219.         x = zopeno(ZOFILE,ftprecv.local,NULL,&xx);
  12220.         debug(F111,"ftp recvrequest zopeno",ftprecv.local,x);
  12221.     }
  12222.     if (x < 1) {                        /* Failure to open output file */
  12223.         if ((!dpyactive || ftp_deb))
  12224.           fprintf(stderr, "local(2): %s: %s\n", ftprecv.local, ck_errstr());
  12225.         ftprecvret = -1;
  12226. #ifdef NTSIG
  12227.         ckThreadEnd(threadinfo);
  12228. #endif /* NTSIG */
  12229.         return;
  12230.     }
  12231.     blksize = FTP_BUFSIZ;               /* Allocate input buffer */
  12232.  
  12233.     debug(F101,"ftp recvrequest blksize","",blksize);
  12234.     debug(F101,"ftp recvrequest rcvbufsiz","",rcvbufsiz);
  12235.  
  12236.     if (rcvbufsiz < blksize) {          /* if necessary */
  12237.         if (rcvbuf) {
  12238.             free(rcvbuf);
  12239.             rcvbuf = NULL;
  12240.         }
  12241.         rcvbuf = (char *)malloc((unsigned)blksize);
  12242.         if (!rcvbuf) {
  12243.         debug(F100,"ftp get rcvbuf malloc failed","",0);
  12244.             ftpcode = -2;
  12245. #ifdef ENOMEM
  12246.             errno = ENOMEM;
  12247. #endif /* ENOMEM */
  12248.             if ((!dpyactive || ftp_deb))
  12249.               perror("malloc");
  12250.             rcvbufsiz = 0;
  12251.             ftprecvret = -1;
  12252. #ifdef NTSIG
  12253.             ckThreadEnd(threadinfo);
  12254. #endif /* NTSIG */
  12255.             return;
  12256.         }
  12257.     debug(F101,"ftp get rcvbuf malloc ok","",blksize);
  12258.         rcvbufsiz = blksize;
  12259.     }
  12260.     debug(F111,"ftp get rcvbufsiz",ftprecv.local,rcvbufsiz);
  12261.  
  12262.     ffc = (CK_OFF_T)0;            /* Character counter */
  12263.     cps = oldcps = 0L;                  /* Thruput */
  12264.     start = gmstimer();                 /* Start time (msecs) */
  12265. #ifdef GFTIMER
  12266.     rftimer();                          /* Start time (float) */
  12267. #endif /* GFTIMER */
  12268.  
  12269.     debug(F111,"ftp get type",ftprecv.local,curtype);
  12270.     debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
  12271.     switch (curtype) {
  12272.       case FTT_BIN:                     /* Binary mode */
  12273.       case FTT_TEN:                     /* TENEX mode */
  12274.         d = 0;
  12275.         while (1) {
  12276.             errno = 0;
  12277.             c = secure_read(ftprecv.din, rcvbuf, rcvbufsiz);
  12278.             if (cancelfile) {
  12279.                 failftprecv2(threadinfo);
  12280. #ifdef NTSIG
  12281.                 ckThreadEnd(threadinfo);
  12282. #endif /* NTSIG */
  12283.                 return;
  12284.             }
  12285.             if (c < 1)
  12286.               break;
  12287. #ifdef printf                           /* (What if it isn't?) */
  12288.             if (out2screen && !ftprecv.pipename) {
  12289.                 int i;
  12290.                 for (i = 0; i < c; i++)
  12291.                   printf("%c",rcvbuf[i]);
  12292.             } else
  12293. #endif /* printf */
  12294.               {
  12295.                 register int i;
  12296.                 i = 0;
  12297.                 errno = 0;
  12298.                 while (i < c) {
  12299.                     if (zmchout(rcvbuf[i++]) < 0) {
  12300.                         d = i;
  12301.                         break;
  12302.                     }
  12303.                 }
  12304.             }
  12305.             bytes += c;
  12306.             ffc += c;
  12307.         }
  12308.         if (c < 0) {
  12309.             debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
  12310.             if (c == -1 && errno != EPIPE)
  12311.               if ((!dpyactive || ftp_deb))
  12312.                 perror("netin");
  12313.             bytes = (CK_OFF_T)-1;
  12314.             ftpcode = -4;
  12315.         }
  12316.         if (d < c) {
  12317.             ftpcode = -2;
  12318.             if ((!dpyactive || ftp_deb)) {
  12319.                 char * p;
  12320.                 p = ftprecv.local ? ftprecv.local : ftprecv.pipename;
  12321.                 if (d < 0)
  12322.                   fprintf(stderr,
  12323.               "local(3): %s: %s\n", ftprecv.local, ck_errstr());
  12324.                 else
  12325.                   fprintf(stderr,
  12326.               "%s: short write\n", ftprecv.local);
  12327.             }
  12328.         }
  12329.         break;
  12330.  
  12331.       case FTT_ASC:                     /* Text mode */
  12332.     debug(F101,"ftp recvrequest TYPE A xlate","",ftprecv.xlate);
  12333. #ifndef NOCSETS
  12334.         if (ftprecv.xlate) {
  12335.             int t;
  12336. #ifdef CK_ANSIC
  12337.             int (*fn)(char);
  12338. #else
  12339.             int (*fn)();
  12340. #endif /* CK_ANSIC */
  12341.             debug(F110,"ftp recvrequest (data)","initxlate",0);
  12342.             initxlate(ftprecv.rcs,ftprecv.fcs);         /* (From,To) */
  12343.             if (ftprecv.pipename) {
  12344.                 fn = pipeout;
  12345.                 debug(F110,"ftp recvrequest ASCII","pipeout",0);
  12346.             } else {
  12347.                 fn = out2screen ? scrnout : putfil;
  12348.                 debug(F110,"ftp recvrequest ASCII",
  12349.                       out2screen ? "scrnout" : "putfil",0);
  12350.             }
  12351.             while (1) {
  12352.         /* Get byte from net */
  12353.                 c0 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12354.                 if (cancelfile) {
  12355.                     failftprecv2(threadinfo);
  12356. #ifdef NTSIG
  12357.                     ckThreadEnd(threadinfo);
  12358. #endif /* NTSIG */
  12359.                     return;
  12360.                 }
  12361.                 if (c0 < 0)
  12362.                   break;
  12363.         /* Second byte from net */
  12364.                 c1 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
  12365.                 if (cancelfile) {
  12366.                     failftprecv2(threadinfo);
  12367. #ifdef NTSIG
  12368.                     ckThreadEnd(threadinfo);
  12369. #endif /* NTSIG */
  12370.                     return;
  12371.                 }
  12372.                 if (c1 < 0)
  12373.                   break;
  12374. #ifdef COMMENT
  12375.         /* K95: Check whether we need this */
  12376.         if (fileorder > 0)    /* Little Endian */
  12377.           bytswap(&c0,&c1);    /* swap bytes*/
  12378. #endif /* COMMENT */
  12379.  
  12380. #ifdef OS2
  12381.                 if ( out2screen &&            /* we're translating to UCS-2 */ 
  12382.                      !k95stdout && !inserver) /* for the real screen... */     
  12383.                 {
  12384.                     union {
  12385.                         USHORT ucs2;
  12386.                         UCHAR  bytes[2];
  12387.                     } output;
  12388.  
  12389.                     output.bytes[0] = c1;
  12390.                     output.bytes[1] = c0;
  12391.  
  12392.                     VscrnWrtUCS2StrAtt(VCMD,
  12393.                                        &output.ucs2,
  12394.                                        1,
  12395.                                        wherey[VCMD],
  12396.                                        wherex[VCMD],
  12397.                                        &colorcmd
  12398.                                        );
  12399.  
  12400.                 } else 
  12401. #endif /* OS2 */
  12402.                 {
  12403.                     if ((x = xpnbyte(c0,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12404.                     if ((x = xpnbyte(c1,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
  12405.                 }
  12406.             }
  12407.         } else {
  12408. #endif /* NOCSETS */
  12409.             while (1) {
  12410.                 c = secure_getc(ftprecv.din,0);
  12411.                 if (cancelfile) {
  12412.                     failftprecv2(threadinfo);
  12413. #ifdef NTSIG
  12414.                     ckThreadEnd(threadinfo);
  12415. #endif /* NTSIG */
  12416.                     return;
  12417.                 }
  12418.                 if (c < 0 || c == EOF)
  12419.                   break;
  12420. #ifdef UNIX
  12421.         /* Record format conversion for Unix */
  12422.         /* SKIP THIS FOR WINDOWS! */
  12423.                 if (c == '\n')
  12424.                   bare_lfs++;
  12425.                 while (c == '\r') {
  12426.                     bytes++;
  12427.                     if ((c = secure_getc(ftprecv.din,0)) != '\n' ||
  12428.             ftprecv.tcrflag) {
  12429.                         if (cancelfile) {
  12430.                             failftprecv2(threadinfo);
  12431. #ifdef NTSIG
  12432.                             ckThreadEnd(threadinfo);
  12433. #endif /* NTSIG */
  12434.                             return;
  12435.                         }
  12436.                         if (c < 0 || c == EOF)
  12437.                           goto break2;
  12438.                         if (c == '\0') {
  12439.                             bytes++;
  12440.                             goto contin2;
  12441.                         }
  12442.                     }
  12443.                 }
  12444.                 if (c < 0)
  12445.                   break;
  12446. #endif /* UNX */
  12447.  
  12448.                 if (out2screen && !ftprecv.pipename)
  12449. #ifdef printf
  12450.                   printf("%c",(char)c);
  12451. #else
  12452.                   putchar((char)c);
  12453. #endif /* printf */
  12454.                 else
  12455.                   if ((d = zmchout(c)) < 0)
  12456.                     break;
  12457.                 bytes++;
  12458.                 ffc++;
  12459.               contin2:
  12460.                 ;
  12461.             }
  12462.           break2:
  12463.             if (bare_lfs && (!dpyactive || ftp_deb)) {
  12464.                 printf("WARNING! %d bare linefeeds received in ASCII mode\n",
  12465.                        bare_lfs);
  12466.                 printf("File might not have transferred correctly.\n");
  12467.             }
  12468.             if (ftprecv.din == -1) {
  12469.                 bytes = (CK_OFF_T)-1;
  12470.             }
  12471.             if (c == -2)
  12472.               bytes = (CK_OFF_T)-1;
  12473.             break;
  12474. #ifndef NOCSETS
  12475.         }
  12476. #endif /* NOCSETS */
  12477.     }
  12478.     if (ftprecv.pipename || !out2screen) {
  12479.     zclose(ZOFILE);            /* Close the file */
  12480.     debug(F111,"doftprecv2 zclose ftpcode",ftprecv.local,ftpcode);
  12481.     if (ftpcode < 0) {        /* If download failed */
  12482.         int x = 0;
  12483.         switch (keep) {        /* which is... */
  12484.           case SET_AUTO:        /* AUTO */
  12485.         if (curtype == FTT_ASC) /* Delete file if TYPE A. */
  12486.           x = 1;
  12487.         break;
  12488.           case SET_OFF:        /* DISCARD */
  12489.         x = 1;            /* Delete file, period. */
  12490.         break;
  12491.           default:            /* KEEP */
  12492.         break;
  12493.         }
  12494.         if (x) {
  12495.         x = zdelet(ftprecv.local);
  12496.         debug(F111,"ftp get delete incomplete",ftprecv.local,x);
  12497.         }
  12498.     }
  12499.     }
  12500.     signal(SIGINT, ftprecv.oldintr);
  12501. #ifdef SIGPIPE
  12502.     if (ftprecv.oldintp)
  12503.       signal(SIGPIPE, ftprecv.oldintp);
  12504. #endif /* SIGPIPE */
  12505.     stop = gmstimer();
  12506. #ifdef GFTIMER
  12507.     fpfsecs = gftimer();
  12508. #endif /* GFTIMER */
  12509.     tfc += ffc;
  12510.  
  12511. #ifdef TCPIPLIB
  12512.     socket_close(ftprecv.din);
  12513. #else /* TCPIPLIB */
  12514. #ifdef USE_SHUTDOWN
  12515.     shutdown(ftprecv.din, 1+1);
  12516. #endif /* USE_SHUTDOWN */
  12517.     close(ftprecv.din);
  12518. #endif /* TCPIPLIB */
  12519.     ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
  12520.     ftprecvret = ((ftpcode < 0 || ftprecv.reply == REPLY_TRANSIENT || 
  12521.                    ftprecv.reply == REPLY_ERROR) ? -1 : 0);
  12522. #ifdef NTSIG
  12523.      ckThreadEnd(threadinfo);
  12524. #endif /* NTSIG */
  12525. }
  12526.  
  12527. static int
  12528. recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
  12529.             xlate, fcs, rcs)
  12530.     char *cmd, *local, *remote, *lmode, *pipename;
  12531.     int printnames, recover, xlate, fcs, rcs;
  12532. {
  12533.     struct stat stbuf;
  12534.  
  12535. #ifdef DEBUG
  12536.     if (deblog) {
  12537.         debug(F111,"ftp recvrequest cmd",cmd,recover);
  12538.         debug(F110,"ftp recvrequest local ",local,0);
  12539.         debug(F111,"ftp recvrequest remote",remote,ftp_typ);
  12540.         debug(F110,"ftp recvrequest pipename ",pipename,0);
  12541.         debug(F101,"ftp recvrequest xlate","",xlate);
  12542.         debug(F101,"ftp recvrequest fcs","",fcs);
  12543.         debug(F101,"ftp recvrequest rcs","",rcs);
  12544.     }
  12545. #endif /* DEBUG */
  12546.  
  12547.     ftprecv.localsize = (CK_OFF_T)0;
  12548.  
  12549.     if (remfile) {                      /* See remcfm(), remtxt() */
  12550.         if (rempipe) {
  12551.             pipename = remdest;
  12552.         } else {
  12553.             local = remdest;
  12554.             if (remappd) lmode = "ab";
  12555.         }
  12556.     }
  12557.     out2screen = 0;
  12558.     if (!cmd) cmd = "";                 /* Core dump prevention */
  12559.     if (!remote) remote = "";
  12560.     if (!lmode) lmode = "";
  12561.  
  12562.     if (pipename) {                     /* No recovery for pipes. */
  12563.         recover = 0;
  12564.         if (!local)
  12565.           local = pipename;
  12566.     } else {
  12567.         if (!local)                     /* Output to screen? */
  12568.           local = "-";
  12569.         out2screen = !strcmp(local,"-");
  12570.     }
  12571.     debug(F101,"ftp recvrequest out2screen","",out2screen);
  12572.  
  12573. #ifdef OS2
  12574.     if ( ftp_xla && out2screen && !k95stdout && !inserver )
  12575.         fcs = FC_UCS2;
  12576. #endif /* OS2 */
  12577.  
  12578.     if (out2screen)                     /* No recovery to screen */
  12579.       recover = 0;
  12580.     if (!ftp_typ)                       /* No recovery in text mode */
  12581.       recover = 0;
  12582.     ftprecv.is_retr = (strcmp(cmd, "RETR") == 0);
  12583.  
  12584.     if (!ftprecv.is_retr)               /* No recovery except for RETRieve */
  12585.       recover = 0;
  12586.  
  12587. #ifdef COMMENT
  12588.     if (!out2screen && !pipename && ftprecv.is_retr) { /* To real file */
  12589.         if (recursive && ckstrchr(local,'/')) {
  12590.         
  12591.         }
  12592.     }
  12593. #endif /* COMMENT */
  12594.  
  12595.     ftprecv.localsize = (CK_OFF_T)0;    /* Local file size */
  12596.     rs_len = (CK_OFF_T)0;        /* Recovery point */
  12597.  
  12598.     debug(F101,"ftp recvrequest recover","",recover);
  12599.     if (recover) {                      /* Recovering... */
  12600.         if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
  12601.         debug(F101,"ftp recvrequest recover stat failed","",errno);
  12602.             recover = 0;                /* So cancel recovery */
  12603.         } else {                        /* Have local file info */
  12604.             ftprecv.localsize = stbuf.st_size;  /* Get size */
  12605.         /* Remote file smaller than local */
  12606.             if (fsize < ftprecv.localsize) {
  12607.         debug(F101,"ftp recvrequest recover remote smaller","",fsize);
  12608.                 recover = 0;            /* Recovery can't work */
  12609.             } else if (fsize == ftprecv.localsize) { /* Sizes are equal */
  12610.                 debug(F111,"ftp recvrequest recover equal size",
  12611.               remote,ftprecv.localsize);
  12612.                 return(1);
  12613.             }
  12614. #ifdef COMMENT
  12615. /*
  12616.   The problem here is that the original partial file never got its date
  12617.   set, either because FTP DATES was OFF, or because the partial file was
  12618.   downloaded by some other program that doesn't set local file dates, or
  12619.   because Kermit only sets the file's date when the download was complete
  12620.   and successful.  In all these cases, the local file has a later time
  12621.   than the remote.
  12622. */
  12623.             if (recover) {              /* Remote is bigger */
  12624.                 x = chkmodtime(local,remote,0); /* Check file dates */
  12625.                 debug(F111,"ftp recvrequest chkmodtime",remote,x);
  12626.                 if (x != 1)        /* Dates must be equal! */
  12627.                   recover = 0;          /* If not, get whole file */
  12628.             }
  12629. #endif /* COMMENT */
  12630.         }
  12631.         debug(F111,"ftp recvrequest recover",remote,recover);
  12632.     }
  12633.  
  12634. #ifdef FTP_PROXY
  12635.     if (proxy && ftprecv.is_retr)
  12636.       return(proxtrans(cmd, local ? local : remote, remote));
  12637. #endif /* FTP_PROXY */
  12638.  
  12639.     ftprecv.tcrflag = (feol != CR) && ftprecv.is_retr;
  12640.  
  12641.     ftprecv.reply = 0;
  12642.     ftprecv.fcs = fcs;
  12643.     ftprecv.rcs = rcs;
  12644.     ftprecv.recover = recover;
  12645.     ftprecv.xlate = xlate;
  12646.     ftprecv.cmd = cmd;
  12647.     ftprecv.local = local;
  12648.     ftprecv.remote = remote;
  12649.     ftprecv.lmode = lmode;
  12650.     ftprecv.pipename = pipename;
  12651.     ftprecv.oldintp = NULL;
  12652.     ftpcode = 0;
  12653.  
  12654.     havesigint = 0;
  12655.     ftprecv.oldintr = signal(SIGINT, cancelrecv);
  12656.     if (cc_execute(ckjaddr(recvcancel), doftprecv, failftprecv) < 0)
  12657.       return -1;
  12658.     if (ftprecvret < 0)
  12659.       return -1;
  12660.  
  12661.     if (cc_execute(ckjaddr(recvcancel), doftprecv2, failftprecv2) < 0)
  12662.       return -1;
  12663.     return ftprecvret;
  12664. }
  12665.  
  12666. /*
  12667.  * Need to start a listen on the data channel before we send the command,
  12668.  * otherwise the server's connect may fail.
  12669.  */
  12670. static int
  12671. initconn() {
  12672.     register char *p, *a;
  12673.     int result, tmpno = 0;
  12674.     int on = 1;
  12675.     GSOCKNAME_T len;
  12676.  
  12677. #ifndef NO_PASSIVE_MODE
  12678.     int a1,a2,a3,a4,p1,p2;
  12679.  
  12680.     if (passivemode) {
  12681.         data = socket(AF_INET, SOCK_STREAM, 0);
  12682.         globaldin = data;
  12683.         if (data < 0) {
  12684.             perror("ftp: socket");
  12685.             return(-1);
  12686.         }
  12687.         if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  12688.             printf("Passive mode refused\n");
  12689.             passivemode = 0;
  12690.             return(initconn());
  12691.         }
  12692. /*
  12693.   Now we have a string of comma-separated one-byte unsigned integer values,
  12694.   The first four are the an IP address.  The fifth is the MSB of the port
  12695.   number, the sixth is the LSB.  From that we can make a sockaddr_in.
  12696. */
  12697.         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
  12698.             printf("Passive mode address scan failure\n");
  12699.             return(-1);
  12700.         };
  12701. #ifndef NOHTTP
  12702.         if (tcp_http_proxy) {
  12703. #ifdef OS2
  12704.             char * agent = "Kermit 95"; /* Default user agent */
  12705. #else
  12706.             char * agent = "C-Kermit";
  12707. #endif /* OS2 */
  12708.             register struct hostent *hp = 0;
  12709.             struct servent *destsp;
  12710.             char host[512], *p, *q;
  12711. #ifdef IP_TOS
  12712. #ifdef IPTOS_THROUGHPUT
  12713.             int tos;
  12714. #endif /* IPTOS_THROUGHPUT */
  12715. #endif /* IP_TOS */
  12716.             int s;
  12717. #ifdef DEBUG
  12718.             extern int debtim;
  12719.             int xdebtim;
  12720.             xdebtim = debtim;
  12721.             debtim = 1;
  12722. #endif /* DEBUG */
  12723.  
  12724.             ckmakxmsg(proxyhost,HTTPCPYL,ckuitoa(a1),".",ckuitoa(a2),
  12725.                       ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
  12726.                       NULL,NULL,NULL
  12727.                       );
  12728.             memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  12729.             for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
  12730.               *q = *p;
  12731.             *q = '\0';
  12732.  
  12733.             hisctladdr.sin_addr.s_addr = inet_addr(host);
  12734.             if (hisctladdr.sin_addr.s_addr != -1) {
  12735.                 debug(F110,"initconn A",host,0);
  12736.                 hisctladdr.sin_family = AF_INET;
  12737.             } else {
  12738.                 debug(F110,"initconn B",host,0);
  12739.                 hp = gethostbyname(host);
  12740. #ifdef HADDRLIST
  12741.                 hp = ck_copyhostent(hp); /* make safe copy that won't change */
  12742. #endif /* HADDRLIST */
  12743.                 if (hp == NULL) {
  12744.                     fprintf(stderr, "ftp: %s: Unknown host\n", host);
  12745.                     ftpcode = -1;
  12746. #ifdef DEBUG
  12747.                     debtim = xdebtim;
  12748. #endif /* DEBUG */
  12749.                     return(0);
  12750.                 }
  12751.                 hisctladdr.sin_family = hp->h_addrtype;
  12752. #ifdef HADDRLIST
  12753.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  12754.                        sizeof(hisctladdr.sin_addr));
  12755. #else /* HADDRLIST */
  12756.                 memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  12757.                        sizeof(hisctladdr.sin_addr));
  12758. #endif /* HADDRLIST */
  12759.             }
  12760.             data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12761.             debug(F101,"initconn socket","",data);
  12762.             if (data < 0) {
  12763.                 perror("ftp: socket");
  12764.                 ftpcode = -1;
  12765. #ifdef DEBUG
  12766.                 debtim = xdebtim;
  12767. #endif /* DEBUG */
  12768.                 return(0);
  12769.             }
  12770.             if (*p == ':')
  12771.               p++;
  12772.             else
  12773.               p = "http";
  12774.  
  12775.             destsp = getservbyname(p,"tcp");
  12776.             if (destsp)
  12777.               hisctladdr.sin_port = destsp->s_port;
  12778.             else if (p)
  12779.               hisctladdr.sin_port = htons(atoi(p));
  12780.             else
  12781.               hisctladdr.sin_port = htons(80);
  12782.             errno = 0;
  12783. #ifdef HADDRLIST
  12784.             debug(F100,"initconn HADDRLIST","",0);
  12785.             while
  12786. #else
  12787.             debug(F100,"initconn no HADDRLIST","",0);
  12788.             if
  12789. #endif /* HADDRLIST */
  12790.               (connect(data, (struct sockaddr *)&hisctladdr,
  12791.                        sizeof (hisctladdr)) < 0) {
  12792.                   debug(F101,"initconn connect failed","",errno);
  12793. #ifdef HADDRLIST
  12794.                   if (hp && hp->h_addr_list[1]) {
  12795.                       int oerrno = errno;
  12796.  
  12797.                       fprintf(stderr,
  12798.                               "ftp: connect to address %s: ",
  12799.                               inet_ntoa(hisctladdr.sin_addr)
  12800.                               );
  12801.                       errno = oerrno;
  12802.                       perror((char *)0);
  12803.                       hp->h_addr_list++;
  12804.                       memcpy((char *)&hisctladdr.sin_addr,
  12805.                              hp->h_addr_list[0],
  12806.                              sizeof(hisctladdr.sin_addr));
  12807.                       fprintf(stdout, "Trying %s...\n",
  12808.                               inet_ntoa(hisctladdr.sin_addr));
  12809. #ifdef TCPIPLIB
  12810.                       socket_close(data);
  12811. #else /* TCPIPLIB */
  12812.                       close(data);
  12813. #endif /* TCPIPLIB */
  12814.                       data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  12815.                       if (data < 0) {
  12816.                           perror("ftp: socket");
  12817.                           ftpcode = -1;
  12818. #ifdef DEBUG
  12819.                           debtim = xdebtim;
  12820. #endif /* DEBUG */
  12821.                           return(0);
  12822.                       }
  12823.                       continue;
  12824.                   }
  12825. #endif /* HADDRLIST */
  12826.                   perror("ftp: connect");
  12827.                   ftpcode = -1;
  12828.                   goto bad;
  12829.               }
  12830.             if (http_connect(data,
  12831.                              tcp_http_proxy_agent ?
  12832.                    tcp_http_proxy_agent :
  12833.                      agent,
  12834.                  NULL,
  12835.                              tcp_http_proxy_user,
  12836.                              tcp_http_proxy_pwd,
  12837.                              0,
  12838.                              proxyhost
  12839.                              ) < 0) {
  12840. #ifdef TCPIPLIB
  12841.                 socket_close(data);
  12842. #else /* TCPIPLIB */
  12843.                 close(data);
  12844. #endif /* TCPIPLIB */
  12845.                 perror("ftp: connect");
  12846.                 ftpcode = -1;
  12847.                 goto bad;
  12848.             }
  12849.         } else
  12850. #endif /* NOHTTP */
  12851.         {
  12852.             data_addr.sin_family = AF_INET;
  12853.             data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
  12854.             data_addr.sin_port = htons((p1<<8)|p2);
  12855.  
  12856.             if (connect(data,
  12857.                         (struct sockaddr *)&data_addr,
  12858.                         sizeof(data_addr)) < 0
  12859.                 ) {
  12860.                 perror("ftp: connect");
  12861.                 return(-1);
  12862.             }
  12863.         }
  12864.         debug(F100,"initconn connect ok","",0);
  12865. #ifdef IP_TOS
  12866. #ifdef IPTOS_THROUGHPUT
  12867.         on = IPTOS_THROUGHPUT;
  12868.         if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12869.           perror("ftp: setsockopt TOS (ignored)");
  12870. #endif /* IPTOS_THROUGHPUT */
  12871. #endif /* IP_TOS */
  12872.         memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
  12873.         return(0);
  12874.     }
  12875. #endif /* NO_PASSIVE_MODE */
  12876.  
  12877.   noport:
  12878.     memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
  12879.     if (sendport)
  12880.       data_addr.sin_port = 0;   /* let system pick one */
  12881.     if (data != -1) {
  12882. #ifdef TCPIPLIB
  12883.         socket_close(data);
  12884. #else /* TCPIPLIB */
  12885. #ifdef USE_SHUTDOWN
  12886.         shutdown(data, 1+1);
  12887. #endif /* USE_SHUTDOWN */
  12888.         close(data);
  12889. #endif /* TCPIPLIB */
  12890.     }
  12891.     data = socket(AF_INET, SOCK_STREAM, 0);
  12892.     globaldin = data;
  12893.     if (data < 0) {
  12894.         perror("ftp: socket");
  12895.         if (tmpno)
  12896.           sendport = 1;
  12897.         return(-1);
  12898.     }
  12899.     if (!sendport) {
  12900.         if (setsockopt(data,
  12901.                        SOL_SOCKET,
  12902.                        SO_REUSEADDR,
  12903.                        (char *)&on,
  12904.                        sizeof (on)
  12905.                        ) < 0
  12906.             ) {
  12907.             perror("ftp: setsockopt (reuse address)");
  12908.             goto bad;
  12909.         }
  12910.     }
  12911.     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
  12912.         perror("ftp: bind");
  12913.         goto bad;
  12914.     }
  12915.     len = sizeof (data_addr);
  12916.     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
  12917.         perror("ftp: getsockname");
  12918.         goto bad;
  12919.     }
  12920.     if (listen(data, 1) < 0) {
  12921.         perror("ftp: listen");
  12922.         goto bad;
  12923.     }
  12924.     if (sendport) {
  12925.         a = (char *)&data_addr.sin_addr;
  12926.         p = (char *)&data_addr.sin_port;
  12927.         ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
  12928.                   UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
  12929.                   UC(p[0]),",", UC(p[1]));
  12930.         result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
  12931.         if (result == REPLY_ERROR && sendport) {
  12932.             sendport = 0;
  12933.             tmpno = 1;
  12934.             goto noport;
  12935.         }
  12936.         return(result != REPLY_COMPLETE);
  12937.     }
  12938.     if (tmpno)
  12939.       sendport = 1;
  12940. #ifdef IP_TOS
  12941. #ifdef IPTOS_THROUGHPUT
  12942.     on = IPTOS_THROUGHPUT;
  12943.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
  12944.       perror("ftp: setsockopt TOS (ignored)");
  12945. #endif
  12946. #endif
  12947.     return(0);
  12948.   bad:
  12949. #ifdef TCPIPLIB
  12950.     socket_close(data);
  12951. #else /* TCPIPLIB */
  12952. #ifdef USE_SHUTDOWN
  12953.     shutdown(data, 1+1);
  12954. #endif /* USE_SHUTDOWN */
  12955.     close(data);
  12956. #endif /* TCPIPLIB */
  12957.     data = -1;
  12958.     globaldin = data;
  12959.     if (tmpno)
  12960.       sendport = 1;
  12961.     return(-1);
  12962. }
  12963.  
  12964. #ifdef CK_SSL
  12965. static int
  12966. ssl_dataconn() {
  12967.     if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
  12968.         SSL_free(ssl_ftp_data_con);
  12969.         ssl_ftp_data_con=NULL;
  12970.     }
  12971.     ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
  12972.  
  12973.     SSL_set_fd(ssl_ftp_data_con,data);
  12974.     SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
  12975.  
  12976.     SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
  12977.  
  12978.     if (ssl_debug_flag) {
  12979.         fprintf(stderr,"=>START SSL connect on DATA\n");
  12980.         fflush(stderr);
  12981.     }
  12982.     if (SSL_connect(ssl_ftp_data_con) <= 0) {
  12983.         static char errbuf[1024];
  12984.         ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
  12985.                   ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
  12986.         fprintf(stderr,"%s\n", errbuf);
  12987.         fflush(stderr);
  12988. #ifdef TCPIPLIB
  12989.         socket_close(data);
  12990. #else /* TCPIPLIB */
  12991. #ifdef USE_SHUTDOWN
  12992.         shutdown(data, 1+1);
  12993. #endif /* USE_SHUTDOWN */
  12994.         close(data);
  12995. #endif /* TCPIPLIB */
  12996.         data = -1;
  12997.         globaldin = data;
  12998.         return(-1);
  12999.     } else {
  13000.         ssl_ftp_data_active_flag=1;
  13001.  
  13002.         if (!ssl_certsok_flag && !tls_is_krb5(2)) {
  13003.             char *subject = ssl_get_subject_name(ssl_ftp_data_con);
  13004.  
  13005.             if (!subject) {
  13006.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  13007.                     debug(F110,"dataconn","[SSL _- FAILED]",0);
  13008.  
  13009.                     ssl_ftp_data_active_flag = 0;
  13010. #ifdef TCPIPLIB
  13011.                     socket_close(data);
  13012. #else /* TCPIPLIB */
  13013. #ifdef USE_SHUTDOWN
  13014.                     shutdown(data, 1+1);
  13015. #endif /* USE_SHUTDOWN */
  13016.                     close(data);
  13017. #endif /* TCPIPLIB */
  13018.                     data = -1;
  13019.                     globaldin = data;
  13020.                     return(-1);
  13021.                 } else {
  13022.                     if (!out2screen && displa && fdispla) {
  13023.                         ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
  13024.                         /* fdispla = XYFD_B; */
  13025.                     }
  13026.  
  13027.                     if (uq_ok(
  13028.           "Warning: Server didn't provide a certificate on data connection\n",
  13029.                                "Continue with file transfer? (Y/N)",
  13030.                               3,NULL,0) <= 0) {
  13031.                         debug(F110, "dataconn","[SSL - FAILED]",0);
  13032.                         ssl_ftp_data_active_flag = 0;
  13033. #ifdef TCPIPLIB
  13034.                         socket_close(data);
  13035. #else /* TCPIPLIB */
  13036. #ifdef USE_SHUTDOWN
  13037.                         shutdown(data, 1+1);
  13038. #endif /* USE_SHUTDOWN */
  13039.                         close(data);
  13040. #endif /* TCPIPLIB */
  13041.                         data = -1;
  13042.                         globaldin = data;
  13043.                         return(-1);
  13044.                     }
  13045.                 }
  13046.             } else {
  13047.                 if (!out2screen && displa && fdispla == XYFD_C) {
  13048.                     ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
  13049.                     /* fdispla = XYFD_B; */
  13050.                 }
  13051.  
  13052.                 if (ssl_check_server_name(ssl_ftp_data_con,ftp_user_host)) {
  13053.                     debug(F110,"dataconn","[SSL - FAILED]",0);
  13054.                     ssl_ftp_data_active_flag = 0;
  13055. #ifdef TCPIPLIB
  13056.                     socket_close(data);
  13057. #else /* TCPIPLIB */
  13058. #ifdef USE_SHUTDOWN
  13059.                     shutdown(data, 1+1);
  13060. #endif /* USE_SHUTDOWN */
  13061.                     close(data);
  13062. #endif /* TCPIPLIB */
  13063.                     data = -1;
  13064.                     globaldin = data;
  13065.                     return(-1);
  13066.                 }
  13067.             }
  13068.         }
  13069.         debug(F110,"dataconn","[SSL - OK]",0);
  13070. #ifdef COMMENT
  13071.         /* This messes up the full screen file transfer display */
  13072.         ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
  13073. #endif /* COMMENT */
  13074.     }
  13075.     if (ssl_debug_flag) {
  13076.         fprintf(stderr,"=>DONE SSL connect on DATA\n");
  13077.         fflush(stderr);
  13078.     }
  13079.     return(data);
  13080. }
  13081. #endif /* CK_SSL */
  13082.  
  13083. static int
  13084. dataconn(lmode) char *lmode; {
  13085.     int s;
  13086. #ifdef IP_TOS
  13087.     int tos;
  13088. #endif /* IP_TOS */
  13089. #ifdef UCX50
  13090.     static u_int fromlen;
  13091. #else
  13092.     static SOCKOPT_T fromlen;
  13093. #endif /* UCX50 */
  13094.  
  13095.     fromlen = sizeof(hisdataaddr);
  13096.  
  13097. #ifndef NO_PASSIVE_MODE
  13098.     if (passivemode) {
  13099. #ifdef CK_SSL
  13100.         ssl_ftp_data_active_flag=0;
  13101.         if (ssl_ftp_active_flag &&
  13102.             (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  13103.           return(ssl_dataconn());
  13104. #endif /* CK_SSL */
  13105.         return(data);
  13106.     }
  13107. #endif /* NO_PASSIVE_MODE */
  13108.  
  13109.     s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
  13110.     if (s < 0) {
  13111.         perror("ftp: accept");
  13112. #ifdef TCPIPLIB
  13113.         socket_close(data);
  13114. #else /* TCPIPLIB */
  13115. #ifdef USE_SHUTDOWN
  13116.         shutdown(data, 1+1);
  13117. #endif /* USE_SHUTDOWN */
  13118.         close(data);
  13119. #endif /* TCPIPLIB */
  13120.         data = -1;
  13121.         globaldin = data;
  13122.         return(-1);
  13123.     }
  13124. #ifdef TCPIPLIB
  13125.     socket_close(data);
  13126. #else /* TCPIPLIB */
  13127. #ifdef USE_SHUTDOWN
  13128.     shutdown(data, 1+1);
  13129. #endif /* USE_SHUTDOWN */
  13130.     close(data);
  13131. #endif /* TCPIPLIB */
  13132.     data = s;
  13133.     globaldin = data;
  13134. #ifdef IP_TOS
  13135. #ifdef IPTOS_THROUGHPUT
  13136.     tos = IPTOS_THROUGHPUT;
  13137.     if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  13138.       perror("ftp: setsockopt TOS (ignored)");
  13139. #endif /* IPTOS_THROUGHPUT */
  13140. #endif /* IP_TOS */
  13141.  
  13142. #ifdef CK_SSL
  13143.     ssl_ftp_data_active_flag=0;
  13144.     if (ssl_ftp_active_flag &&
  13145.         (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
  13146.       return(ssl_dataconn());
  13147. #endif /* CK_SSL */
  13148.     return(data);
  13149. }
  13150.  
  13151. #ifdef FTP_PROXY
  13152. static sigtype
  13153. pscancel(sig) int sig; {
  13154.     cancelfile++;
  13155. }
  13156.  
  13157. static VOID
  13158. pswitch(flag) int flag; {
  13159.     extern int proxy;
  13160.     sig_t oldintr;
  13161.     static struct comvars {
  13162.         int connect;
  13163.         char name[MAXHOSTNAMELEN];
  13164.         struct sockaddr_in mctl;
  13165.         struct sockaddr_in hctl;
  13166.         FILE *in;
  13167.         FILE *out;
  13168.         int tpe;
  13169.         int curtpe;
  13170.         int cpnd;
  13171.         int sunqe;
  13172.         int runqe;
  13173.         int mcse;
  13174.         int ntflg;
  13175.         char nti[17];
  13176.         char nto[17];
  13177.         int mapflg;
  13178.         char mi[CKMAXPATH];
  13179.         char mo[CKMAXPATH];
  13180.         char *authtype;
  13181.         int clvl;
  13182.         int dlvl;
  13183. #ifdef FTP_KRB4
  13184.         des_cblock session;
  13185.         des_key_schedule ftp_sched;
  13186. #endif /* FTP_KRB4 */
  13187. #ifdef FTP_GSSAPI
  13188.         gss_ctx_id_t gcontext;
  13189. #endif /* GSSAPI */
  13190.     } proxstruct, tmpstruct;
  13191.     struct comvars *ip, *op;
  13192.  
  13193.     cancelfile = 0;
  13194.     oldintr = signal(SIGINT, pscancel);
  13195.     if (flag) {
  13196.         if (proxy)
  13197.           return;
  13198.         ip = &tmpstruct;
  13199.         op = &proxstruct;
  13200.         proxy++;
  13201.     } else {
  13202.         if (!proxy)
  13203.           return;
  13204.         ip = &proxstruct;
  13205.         op = &tmpstruct;
  13206.         proxy = 0;
  13207.     }
  13208.     ip->connect = connected;
  13209.     connected = op->connect;
  13210.     if (ftp_host) {
  13211.         strncpy(ip->name, ftp_host, MAXHOSTNAMELEN - 1);
  13212.         ip->name[MAXHOSTNAMELEN - 1] = '\0';
  13213.         ip->name[strlen(ip->name)] = '\0';
  13214.     } else
  13215.       ip->name[0] = 0;
  13216.     ftp_host = op->name;
  13217.     ip->hctl = hisctladdr;
  13218.     hisctladdr = op->hctl;
  13219.     ip->mctl = myctladdr;
  13220.     myctladdr = op->mctl;
  13221.     ip->in = csocket;
  13222.     csocket = op->in;
  13223.     ip->out = csocket;
  13224.     csocket = op->out;
  13225.     ip->tpe = ftp_typ;
  13226.     ftp_typ = op->tpe;
  13227.     ip->curtpe = curtype;
  13228.     curtype = op->curtpe;
  13229.     ip->cpnd = cpend;
  13230.     cpend = op->cpnd;
  13231.     ip->sunqe = ftp_usn;
  13232.     ftp_usn = op->sunqe;
  13233.     ip->mcse = mcase;
  13234.     mcase = op->mcse;
  13235.     ip->ntflg = ntflag;
  13236.     ntflag = op->ntflg;
  13237.     strncpy(ip->nti, ntin, 16);
  13238.     (ip->nti)[strlen(ip->nti)] = '\0';
  13239.     strcpy(ntin, op->nti);
  13240.     strncpy(ip->nto, ntout, 16);
  13241.     (ip->nto)[strlen(ip->nto)] = '\0';
  13242.     strcpy(ntout, op->nto);
  13243.     ip->mapflg = mapflag;
  13244.     mapflag = op->mapflg;
  13245.     strncpy(ip->mi, mapin, CKMAXPATH - 1);
  13246.     (ip->mi)[strlen(ip->mi)] = '\0';
  13247.     strcpy(mapin, op->mi);
  13248.     strncpy(ip->mo, mapout, CKMAXPATH - 1);
  13249.     (ip->mo)[strlen(ip->mo)] = '\0';
  13250.     strcpy(mapout, op->mo);
  13251.     ip->authtype = auth_type;
  13252.     auth_type = op->authtype;
  13253.     ip->clvl = ftp_cpl;
  13254.     ftp_cpl = op->clvl;
  13255.     ip->dlvl = ftp_dpl;
  13256.     ftp_dpl = op->dlvl;
  13257.     if (!ftp_cpl)
  13258.       ftp_cpl = FPL_CLR;
  13259.     if (!ftp_dpl)
  13260.       ftp_dpl = FPL_CLR;
  13261. #ifdef FTP_KRB4
  13262.     memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
  13263.     memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
  13264.     memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
  13265.     memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
  13266. #endif /* FTP_KRB4 */
  13267. #ifdef FTP_GSSAPI
  13268.     ip->gcontext = gcontext;
  13269.     gcontext = op->gcontext;
  13270. #endif /* GSSAPI */
  13271.     signal(SIGINT, oldintr);
  13272.     if (cancelfile) {
  13273.         cancelfile = 0;
  13274.         debug(F101,"pswitch cancelfile B","",cancelfile);
  13275.         (*oldintr)(SIGINT);
  13276.     }
  13277. }
  13278.  
  13279. static sigtype
  13280. cancelpt(sig) int sig; {
  13281.     printf("\n");
  13282.     fflush(stdout);
  13283.     ptabflg++;
  13284.     cancelfile = 0;
  13285. #ifndef OS2
  13286.     longjmp(ptcancel, 1);
  13287. #else
  13288.     PostCtrlCSem();
  13289. #endif /* OS2 */
  13290. }
  13291.  
  13292. void
  13293. proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
  13294.     sig_t oldintr;
  13295.     int secndflag = 0, prox_type, nfnd;
  13296.     char *cmd2;
  13297. #ifdef BSDSELECT
  13298.     fd_set mask;
  13299. #endif /* BSDSELECT */
  13300.     sigtype cancelpt();
  13301.  
  13302.     if (strcmp(cmd, "RETR"))
  13303.       cmd2 = "RETR";
  13304.     else
  13305.       cmd2 = unique ? "STOU" : "STOR";
  13306.     if ((prox_type = type) == 0) {
  13307.         if (servertype == SYS_UNIX && unix_proxy)
  13308.           prox_type = FTT_BIN;
  13309.         else
  13310.           prox_type = FTT_ASC;
  13311.     }
  13312.     if (curtype != prox_type)
  13313.       changetype(prox_type, 1);
  13314.     if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
  13315.         printf("Proxy server does not support third party transfers.\n");
  13316.         return;
  13317.     }
  13318.     pswitch(0);
  13319.     if (!connected) {
  13320.         printf("No primary connection\n");
  13321.         pswitch(1);
  13322.         ftpcode = -1;
  13323.         return;
  13324.     }
  13325.     if (curtype != prox_type)
  13326.       changetype(prox_type, 1);
  13327.  
  13328.     if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
  13329.         pswitch(1);
  13330.         return;
  13331.     }
  13332.  
  13333.     /* Replace with calls to cc_execute() */
  13334.     if (setjmp(ptcancel))
  13335.       goto cancel;
  13336.     oldintr = signal(SIGINT, cancelpt);
  13337.     if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
  13338.         signal(SIGINT, oldintr);
  13339.         pswitch(1);
  13340.         return;
  13341.     }
  13342.     sleep(2000);
  13343.     pswitch(1);
  13344.     secndflag++;
  13345.     if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
  13346.       goto cancel;
  13347.     ptflag++;
  13348.     getreply(0,-1,-1,ftp_vbm,0);
  13349.     pswitch(0);
  13350.     getreply(0,-1,-1,ftp_vbm,0);
  13351.     signal(SIGINT, oldintr);
  13352.     pswitch(1);
  13353.     ptflag = 0;
  13354.     return;
  13355.  
  13356.   cancel:
  13357.     signal(SIGINT, SIG_IGN);
  13358.     ptflag = 0;
  13359.     if (strcmp(cmd, "RETR") && !proxy)
  13360.       pswitch(1);
  13361.     else if (!strcmp(cmd, "RETR") && proxy)
  13362.       pswitch(0);
  13363.     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
  13364.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13365.             pswitch(0);
  13366.             if (cpend)
  13367.               cancel_remote(0);
  13368.         }
  13369.         pswitch(1);
  13370.         if (ptabflg)
  13371.           ftpcode = -1;
  13372.         signal(SIGINT, oldintr);
  13373.         return;
  13374.     }
  13375.     if (cpend)
  13376.       cancel_remote(0);
  13377.     pswitch(!proxy);
  13378.     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
  13379.         if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
  13380.             pswitch(0);
  13381.             if (cpend)
  13382.               cancel_remote(0);
  13383.             pswitch(1);
  13384.             if (ptabflg)
  13385.               ftpcode = -1;
  13386.             signal(SIGINT, oldintr);
  13387.             return;
  13388.         }
  13389.     }
  13390.     if (cpend)
  13391.       cancel_remote(0);
  13392.     pswitch(!proxy);
  13393.     if (cpend) {
  13394. #ifdef BSDSELECT
  13395.         FD_ZERO(&mask);
  13396.         FD_SET(csocket, &mask);
  13397.         if ((nfnd = empty(&mask, 10)) <= 0) {
  13398.             if (nfnd < 0) {
  13399.                 perror("cancel");
  13400.             }
  13401.             if (ptabflg)
  13402.               ftpcode = -1;
  13403.             lostpeer();
  13404.         }
  13405. #else /* BSDSELECT */
  13406. #ifdef IBMSELECT
  13407.         if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
  13408.             if (nfnd < 0) {
  13409.                 perror("cancel");
  13410.             }
  13411.             if (ptabflg)
  13412.               ftpcode = -1;
  13413.             lostpeer();
  13414.         }
  13415. #endif /* IBMSELECT */
  13416. #endif /* BSDSELECT */
  13417.         getreply(0,-1,-1,ftp_vbm,0);
  13418.         getreply(0,-1,-1,ftp_vbm,0);
  13419.     }
  13420.     if (proxy)
  13421.       pswitch(0);
  13422.     pswitch(1);
  13423.     if (ptabflg)
  13424.       ftpcode = -1;
  13425.     signal(SIGINT, oldintr);
  13426. }
  13427. #endif /* FTP_PROXY */
  13428.  
  13429. #ifdef FTP_SECURITY
  13430. #ifdef FTP_GSSAPI
  13431.  
  13432. struct {
  13433.     CONST gss_OID_desc * CONST * mech_type;
  13434.     char *service_name;
  13435. } gss_trials[] = {
  13436.     { &ck_gss_mech_krb5, "ftp" },
  13437.     { &ck_gss_mech_krb5, "host" },
  13438. };
  13439.  
  13440. int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
  13441. #endif /* FTP_GSSAPI */
  13442.  
  13443. static int
  13444. ftp_auth() {
  13445.     extern int setsafe();
  13446.     int j = 0, n;
  13447. #ifdef FTP_KRB4
  13448.     char *service, inst[INST_SZ];
  13449.     ULONG cksum;
  13450.     ULONG checksum = (ULONG) getpid();
  13451.     CHAR out_buf[FTP_BUFSIZ];
  13452.     int i;
  13453. #else /* FTP_KRB4 */
  13454. #ifdef FTP_GSSAPI
  13455.     CHAR out_buf[FTP_BUFSIZ];
  13456.     int i;
  13457. #endif /* FTP_GSSAPI */
  13458. #endif /* FTP_KRB4 */
  13459.  
  13460.     if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
  13461.         return(0);
  13462.  
  13463.     if (auth_type)
  13464.       return(1);                        /* auth already succeeded */
  13465.  
  13466.     /* Try each auth type as specified by the end user */
  13467.     for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
  13468. #ifdef FTP_GSSAPI
  13469.         if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
  13470.             n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
  13471.             if (n == REPLY_CONTINUE) {
  13472.                 OM_uint32 maj_stat, min_stat;
  13473.                 gss_name_t target_name;
  13474.                 gss_buffer_desc send_tok, recv_tok, *token_ptr;
  13475.                 char stbuf[FTP_BUFSIZ];
  13476.                 int comcode, trial;
  13477.                 struct gss_channel_bindings_struct chan;
  13478.                 char * realm = NULL;
  13479.                 char tgt[256];
  13480.  
  13481.                 chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
  13482.                 chan.initiator_address.length = 4;
  13483.                 chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
  13484.                 chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
  13485.                 chan.acceptor_address.length = 4;
  13486.                 chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
  13487.                 chan.application_data.length = 0;
  13488.                 chan.application_data.value = 0;
  13489.  
  13490.                 if (!quiet)
  13491.                   printf("GSSAPI accepted as authentication type\n");
  13492.  
  13493.                 realm = ck_krb5_realmofhost(ftp_user_host);
  13494.                 if (realm) {
  13495.                     ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
  13496.                     debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
  13497.                     if ( krb5_autoget &&
  13498.                          !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
  13499.                             (ck_krb5_is_tgt_valid() > 0)) )
  13500.                         ck_krb5_autoget_TGT(realm);
  13501.                 }
  13502.  
  13503.                 /* Blob from gss-client */
  13504.                 for (trial = 0; trial < n_gss_trials; trial++) {
  13505.                     /* ftp@hostname first, the host@hostname */
  13506.                     /* the V5 GSSAPI binding canonicalizes this for us... */
  13507.                     ckmakmsg(stbuf,FTP_BUFSIZ,
  13508.                              gss_trials[trial].service_name,
  13509.                              "@",
  13510.                              ftp_user_host,
  13511.                              NULL
  13512.                              );
  13513.                     if (ftp_deb)
  13514.                       fprintf(stderr,
  13515.                               "Authenticating to <%s>...\n", stbuf);
  13516.                     send_tok.value = stbuf;
  13517.                     send_tok.length = strlen(stbuf);
  13518.                     maj_stat = gss_import_name(&min_stat, &send_tok,
  13519.                                                gss_nt_service_name,
  13520.                                                &target_name
  13521.                                                );
  13522.                     if (maj_stat != GSS_S_COMPLETE) {
  13523.                         user_gss_error(maj_stat, min_stat, "parsing name");
  13524.                         secure_error("name parsed <%s>\n", stbuf);
  13525.                         continue;
  13526.                     }
  13527.                     token_ptr = GSS_C_NO_BUFFER;
  13528.                     gcontext = GSS_C_NO_CONTEXT; /* structure copy */
  13529.  
  13530.                     do {
  13531.                         if (ftp_deb)
  13532.                           fprintf(stderr, "calling gss_init_sec_context\n");
  13533.                         maj_stat =
  13534.                           gss_init_sec_context(&min_stat,
  13535.                                                GSS_C_NO_CREDENTIAL,
  13536.                                                &gcontext,
  13537.                                                target_name,
  13538.                                                (gss_OID) *
  13539.                                                  gss_trials[trial].mech_type,
  13540.                                                GSS_C_MUTUAL_FLAG |
  13541.                                                GSS_C_REPLAY_FLAG |
  13542.                                                (ftp_cfw ?
  13543.                                                 GSS_C_DELEG_FLAG : 0),
  13544.                                                0,
  13545.                                                 /* channel bindings */
  13546.                                                 (krb5_d_no_addresses ?
  13547.                                                   GSS_C_NO_CHANNEL_BINDINGS :
  13548.                                                   &chan),
  13549.                                                 token_ptr,
  13550.                                                NULL,    /* ignore mech type */
  13551.                                                &send_tok,
  13552.                                                NULL,    /* ignore ret_flags */
  13553.                                                NULL
  13554.                                                );       /* ignore time_rec */
  13555.  
  13556.                         if (maj_stat != GSS_S_COMPLETE &&
  13557.                             maj_stat != GSS_S_CONTINUE_NEEDED) {
  13558.                             if (trial == n_gss_trials-1)
  13559.                               user_gss_error(maj_stat,
  13560.                                              min_stat,
  13561.                                              "initializing context"
  13562.                                              );
  13563.                             gss_release_name(&min_stat, &target_name);
  13564.                             /* maybe we missed on the service name */
  13565.                             goto outer_loop;
  13566.                         }
  13567.                         if (send_tok.length != 0) {
  13568.                             int len;
  13569.                             reply_parse = "ADAT="; /* for ftpcmd() later */
  13570.                             len = FTP_BUFSIZ;
  13571.                             kerror =
  13572.                               radix_encode(send_tok.value,
  13573.                                            out_buf,
  13574.                                            send_tok.length,
  13575.                                            &len,
  13576.                                            RADIX_ENCODE
  13577.                                            );
  13578.                             if (kerror)  {
  13579.                                 fprintf(stderr,
  13580.                                         "Base 64 encoding failed: %s\n",
  13581.                                         radix_error(kerror)
  13582.                                         );
  13583.                                 goto gss_complete_loop;
  13584.                             }
  13585.                             comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
  13586.                             if (comcode != REPLY_COMPLETE
  13587.                                 && comcode != REPLY_CONTINUE /* (335) */
  13588.                                 ) {
  13589.                                 if (trial == n_gss_trials-1) {
  13590.                                     fprintf(stderr, "GSSAPI ADAT failed\n");
  13591.                                     /* force out of loop */
  13592.                                     maj_stat = GSS_S_FAILURE;
  13593.                                 }
  13594.                                 /*
  13595.                                   Backoff to the v1 gssapi is still possible.
  13596.                                   Send a new AUTH command.  If that fails,
  13597.                                   terminate the loop.
  13598.                                 */
  13599.                                 if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
  13600.                                     != REPLY_CONTINUE) {
  13601.                                     fprintf(stderr,
  13602.                                 "GSSAPI ADAT failed, AUTH restart failed\n");
  13603.                                     /* force out of loop */
  13604.                                     maj_stat = GSS_S_FAILURE;
  13605.                                 }
  13606.                                 goto outer_loop;
  13607.                             }
  13608.                             if (!reply_parse) {
  13609.                                 fprintf(stderr,
  13610.                               "No authentication data received from server\n");
  13611.                                 if (maj_stat == GSS_S_COMPLETE) {
  13612.                                     fprintf(stderr,
  13613.                                             "...but no more was needed\n");
  13614.                                     goto gss_complete_loop;
  13615.                                 } else {
  13616.                                     user_gss_error(maj_stat,
  13617.                                                    min_stat,
  13618.                                                    "no reply, huh?"
  13619.                                                    );
  13620.                                     goto gss_complete_loop;
  13621.                                 }
  13622.                             }
  13623.                             len = FTP_BUFSIZ;
  13624.                             kerror = radix_encode(reply_parse,out_buf,i,&len,
  13625.                                                   RADIX_DECODE);
  13626.                             if (kerror) {
  13627.                                 fprintf(stderr,
  13628.                                         "Base 64 decoding failed: %s\n",
  13629.                                         radix_error(kerror));
  13630.                                 goto gss_complete_loop;
  13631.                             }
  13632.  
  13633.                             /* everything worked */
  13634.                             token_ptr = &recv_tok;
  13635.                             recv_tok.value = out_buf;
  13636.                             recv_tok.length = len;
  13637.                             continue;
  13638.  
  13639.                             /* get out of loop clean */
  13640.                           gss_complete_loop:
  13641.                             trial = n_gss_trials-1;
  13642.                             gss_release_buffer(&min_stat, &send_tok);
  13643.                             gss_release_name(&min_stat, &target_name);
  13644.                             goto outer_loop;
  13645.                         }
  13646.                     } while (maj_stat == GSS_S_CONTINUE_NEEDED);
  13647.  
  13648.                   outer_loop:
  13649.                     if (maj_stat == GSS_S_COMPLETE)
  13650.                       break;
  13651.                 }
  13652.                 if (maj_stat == GSS_S_COMPLETE) {
  13653.                     printf("GSSAPI authentication succeeded\n");
  13654.                     reply_parse = NULL;
  13655.                     auth_type = "GSSAPI";
  13656.                     return(1);
  13657.                 } else {
  13658.                     fprintf(stderr, "GSSAPI authentication failed\n");
  13659.                     reply_parse = NULL;
  13660.                 }
  13661.             } else {
  13662.                 if (ftp_deb)
  13663.                 fprintf(stderr, "GSSAPI rejected as an authentication type\n");
  13664.                 if (ftpcode == 500 || ftpcode == 502)
  13665.                     return(0);
  13666.             }
  13667.         }
  13668. #endif /* FTP_GSSAPI */
  13669. #ifdef FTP_SRP
  13670.         if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
  13671.             if (srp_ftp_auth(ftp_user_host,NULL,NULL))
  13672.               return(1);
  13673.             else if (ftpcode == 500 || ftpcode == 502)
  13674.               return(0);
  13675.         }
  13676. #endif /* FTP_SRP */
  13677. #ifdef FTP_KRB4
  13678.         if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
  13679.             n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
  13680.             if (n == REPLY_CONTINUE) {
  13681.                 char tgt[4*REALM_SZ+1];
  13682.                 int rc;
  13683.  
  13684.                 if (!quiet)
  13685.                   printf("KERBEROS_V4 accepted as authentication type\n");
  13686.                 ckstrncpy(inst, (char *) krb_get_phost(ftp_user_host),INST_SZ);
  13687.                 ckstrncpy(ftp_realm,
  13688.                           (char *)ck_krb4_realmofhost(ftp_user_host),
  13689.                           REALM_SZ
  13690.                           );
  13691.  
  13692.                 ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
  13693.                 rc = ck_krb4_tkt_isvalid(tgt);
  13694.  
  13695.                 if (rc <= 0 && krb4_autoget)
  13696.                   ck_krb4_autoget_TGT(ftp_realm);
  13697.  
  13698.                 service = "ftp";
  13699.                 kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
  13700.                 if (kerror == KDC_PR_UNKNOWN) {
  13701.                     service = "rcmd";
  13702.                     kerror = krb_mk_req(&ftp_tkt,
  13703.                                         service,
  13704.                                         inst,
  13705.                                         ftp_realm,
  13706.                                         checksum
  13707.                                         );
  13708.                 }
  13709.                 if (kerror)
  13710.                   fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
  13711.                           krb_get_err_text(kerror));
  13712.                 if (!kerror) {
  13713.                     kerror = krb_get_cred(service, inst, ftp_realm,&ftp_cred);
  13714.                     if (kerror)
  13715.                       fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
  13716.                               krb_get_err_text(kerror));
  13717.                 }
  13718.                 if (!kerror) {
  13719.                     int rc;
  13720.                     rc = des_key_sched(ftp_cred.session, ftp_sched);
  13721.                     if (rc == -1) {
  13722.                        printf("?Invalid DES key specified in credentials\r\n");
  13723.                        debug(F110,"ftp_auth",
  13724.                              "invalid DES Key specified in credentials",0);
  13725.                     } else if ( rc == -2 ) {
  13726.                         printf("?Weak DES key specified in credentials\r\n");
  13727.                         debug(F110,"ftp_auth",
  13728.                               "weak DES Key specified in credentials",0);
  13729.                     } else if ( rc != 0 ) {
  13730.                         printf("?DES Key Schedule not set by credentials\r\n");
  13731.                         debug(F110,"ftp_auth",
  13732.                               "DES Key Schedule not set by credentials",0);
  13733.                     }
  13734.                     reply_parse = "ADAT=";
  13735.                     i = FTP_BUFSIZ;
  13736.                     kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
  13737.                                           &i, RADIX_ENCODE);
  13738.                     if (kerror) {
  13739.                         fprintf(stderr, "Base 64 encoding failed: %s\n",
  13740.                                 radix_error(kerror));
  13741.                         goto krb4_err;
  13742.                     }
  13743.                     if (i > FTP_BUFSIZ - 6)
  13744.                       printf("?ADAT data too long\n");
  13745.                     if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
  13746.                         REPLY_COMPLETE) {
  13747.                         fprintf(stderr, "Kerberos V4 authentication failed\n");
  13748.                         goto krb4_err;
  13749.                     }
  13750.                     if (!reply_parse) {
  13751.                         fprintf(stderr,
  13752.                              "No authentication data received from server\n");
  13753.                         goto krb4_err;
  13754.                     }
  13755.                     i = sizeof(out_buf);
  13756.                     kerror =
  13757.                       radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
  13758.                     if (kerror) {
  13759.                         fprintf(stderr, "Base 64 decoding failed: %s\n",
  13760.                                 radix_error(kerror));
  13761.                         goto krb4_err;
  13762.                     }
  13763.                     kerror = krb_rd_safe(out_buf, i,
  13764. #ifdef KRB524
  13765.                                          ftp_cred.session,
  13766. #else /* KRB524 */
  13767.                                          &ftp_cred.session,
  13768. #endif /* KRB524 */
  13769.                                          &hisctladdr,
  13770.                                          &myctladdr,
  13771.                                          &ftp_msg_data
  13772.                                          );
  13773.                     if (kerror) {
  13774.                         fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
  13775.                                 krb_get_err_text(kerror));
  13776.                         goto krb4_err;
  13777.                     }
  13778.  
  13779.                     /* fetch the (modified) checksum */
  13780.                     memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
  13781.                     if (ntohl(cksum) == checksum + 1) {
  13782.                         if (ftp_vbm)
  13783.                           printf("Kerberos V4 authentication succeeded\n");
  13784.                         reply_parse = NULL;
  13785.                         auth_type = "KERBEROS_V4";
  13786.                         return(1);
  13787.                     } else
  13788.                       fprintf(stderr,
  13789.                               "Kerberos V4 mutual authentication failed\n");
  13790.                   krb4_err:
  13791.                     reply_parse = NULL;
  13792.                 }
  13793.             } else {
  13794.                 if (ftp_deb)
  13795.           fprintf(stderr,
  13796.                       "KERBEROS_V4 rejected as an authentication type\n");
  13797.                 if (ftpcode == 500 || ftpcode == 502)
  13798.                     return(0);
  13799.             }
  13800.         }
  13801. #endif /* FTP_KRB4 */
  13802. #ifdef CK_SSL
  13803.         if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
  13804. #ifdef FTPHOST
  13805.             if (!hostcmd) {
  13806.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13807.                 hostcmd = 1;
  13808.             }
  13809. #endif /* FTPHOST */
  13810.             n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
  13811.             if (n != REPLY_COMPLETE)
  13812.               n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
  13813.             if (n == REPLY_COMPLETE) {
  13814.                 if (!quiet)
  13815.                   printf("TLS accepted as authentication type\n");
  13816.  
  13817.                 auth_type = "TLS";
  13818.                 ssl_auth();
  13819.                 if (ssl_ftp_active_flag ) {
  13820.                     ftp_dpl = FPL_CLR;
  13821.                     ftp_cpl = FPL_PRV;
  13822.                     return(1);
  13823.                 } else {
  13824.                     fprintf(stderr,"TLS authentication failed\n");
  13825.                     auth_type = NULL;
  13826. #ifdef TCPIPLIB
  13827.                     socket_close(csocket);
  13828. #else /* TCPIPLIB */
  13829. #ifdef USE_SHUTDOWN
  13830.                     shutdown(csocket, 1+1);
  13831. #endif /* USE_SHUTDOWN */
  13832.                     close(csocket);
  13833. #endif /* TCPIPLIB */
  13834.                     csocket = -1;
  13835.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13836.                       return(0);
  13837.                 }
  13838.             } else {
  13839.                 if (ftp_deb)
  13840.           fprintf(stderr,"TLS rejected as an authentication type\n");
  13841.                 if (ftpcode == 500 || ftpcode == 502)
  13842.                     return(0);
  13843.             }
  13844.         }
  13845.         if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
  13846. #ifdef FTPHOST
  13847.             if (!hostcmd) {
  13848.                 ftpcmd("HOST",ftp_user_host,0,0,0);
  13849.                 hostcmd = 1;
  13850.             }
  13851. #endif /* FTPHOST */
  13852.             n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
  13853.             if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
  13854.                 if (!quiet)
  13855.                   printf("SSL accepted as authentication type\n");
  13856.                 auth_type = "SSL";
  13857.                 ssl_auth();
  13858.                 if (ssl_ftp_active_flag) {
  13859.                     ftp_dpl = FPL_PRV;
  13860.                     ftp_cpl = FPL_PRV;
  13861.                     setprotbuf(1<<20);
  13862.                     return(1);
  13863.                 } else {
  13864.                     fprintf(stderr,"SSL authentication failed\n");
  13865.                     auth_type = NULL;
  13866. #ifdef TCPIPLIB
  13867.                     socket_close(csocket);
  13868. #else /* TCPIPLIB */
  13869. #ifdef USE_SHUTDOWN
  13870.                     shutdown(csocket, 1+1);
  13871. #endif /* USE_SHUTDOWN */
  13872.                     close(csocket);
  13873. #endif /* TCPIPLIB */
  13874.                     csocket = -1;
  13875.                     if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
  13876.                       return(0);
  13877.                 }
  13878.         } else {
  13879.                 if (ftp_deb)
  13880.           fprintf(stderr, "SSL rejected as an authentication type\n");
  13881.                 if (ftpcode == 500 || ftpcode == 502)
  13882.           return(0);
  13883.             }
  13884.         }
  13885. #endif /* CK_SSL */
  13886.         /* Other auth types go here ... */
  13887.     } /* for (j;;) */
  13888.     return(0);
  13889. }
  13890. #endif /* FTP_SECURITY */
  13891.  
  13892. static int
  13893. #ifdef CK_ANSIC
  13894. setprotbuf(unsigned int size)
  13895. #else
  13896. setprotbuf(size) unsigned int size;
  13897. #endif /* CK_ANSIC */
  13898. /* setprotbuf */ {
  13899.     if (ucbuf)
  13900.       free(ucbuf);
  13901.     ucbuf = NULL;
  13902.     ucbufsiz = 0;
  13903.     actualbuf = size;
  13904.     while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
  13905.         if (actualbuf)
  13906.           actualbuf /= 2;
  13907.         else
  13908.           return(0);
  13909.     }
  13910.     ucbufsiz = actualbuf - FUDGE_FACTOR;
  13911.     debug(F101,"setprotbuf ucbufsiz","",ucbufsiz);
  13912.     if (ucbufsiz < 128) {
  13913.         printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
  13914.     } else if (ucbufsiz < 0) {
  13915.         printf("ERROR: ucbuf allocation failure\n");
  13916.         return(-1);
  13917.     }
  13918.     maxbuf = actualbuf;
  13919.     return(1);
  13920. }
  13921.  
  13922. static int
  13923. #ifdef CK_ANSIC
  13924. setpbsz(unsigned int size)
  13925. #else
  13926. setpbsz(size) unsigned int size;
  13927. #endif /* CK_ANSIC */
  13928. /* setpbsz */ {
  13929.     if (!setprotbuf(size)) {
  13930.         perror("?Error while trying to malloc PROT buffer:");
  13931. #ifdef FTP_SRP
  13932.         srp_reset();
  13933. #endif /* FTP_SRP */
  13934.         ftpclose();
  13935.         return(-1);
  13936.     }
  13937.     reply_parse = "PBSZ=";
  13938.     ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
  13939. #ifdef CK_SSL
  13940.              ssl_ftp_active_flag ? "0" :
  13941. #endif /* CK_SSL */
  13942.              ckuitoa(actualbuf),NULL,NULL);
  13943.     if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
  13944.         if (connected) {
  13945.             printf("?Unable to negotiate PROT buffer size with FTP server\n");
  13946.             ftpclose();
  13947.         }
  13948.         return(-1);
  13949.     }
  13950.     if (reply_parse) {
  13951.         if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
  13952.           maxbuf = actualbuf;
  13953.     } else
  13954.       maxbuf = actualbuf;
  13955.     ucbufsiz = maxbuf - FUDGE_FACTOR;
  13956.     debug(F101,"setpbsz ucbufsiz","",ucbufsiz);    
  13957.     reply_parse = NULL;
  13958.     return(0);
  13959. }
  13960.  
  13961. static VOID
  13962. cancel_remote(din) int din; {
  13963.     CHAR buf[FTP_BUFSIZ];
  13964.     int x, nfnd;
  13965. #ifdef BSDSELECT
  13966.     fd_set mask;
  13967. #endif /* BSDSELECT */
  13968. #ifdef IBMSELECT
  13969.     int fds[2], fdcnt = 0;
  13970. #endif /* IBMSELECT */
  13971. #ifdef DEBUG
  13972.     extern int debtim;
  13973.     int xdebtim;
  13974.     xdebtim = debtim;
  13975.     debtim = 1;
  13976. #endif /* DEBUG */
  13977.     debug(F100,"ftp cancel_remote entry","",0);
  13978. #ifdef CK_SSL
  13979.     if (ssl_ftp_active_flag) {
  13980.         /*
  13981.          * Send Telnet IP, Telnet DM but do so inline and within the
  13982.          * TLS channel
  13983.          */
  13984.         int count, error;
  13985.  
  13986.         buf[0] = IAC;
  13987.         buf[1] = TN_IP;
  13988.         buf[2] = IAC;
  13989.         buf[3] = TN_DM;
  13990.         buf[4] = NUL;
  13991.  
  13992.         count = SSL_write(ssl_ftp_con, buf, 4);
  13993.         debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
  13994.         error = SSL_get_error(ssl_ftp_con,count);
  13995.         debug(F111,"ftp cancel_remote","SSL_get_error()",error);
  13996.         switch (error) {
  13997.           case SSL_ERROR_NONE:
  13998.             break;
  13999.           case SSL_ERROR_WANT_WRITE:
  14000.           case SSL_ERROR_WANT_READ:
  14001.           case SSL_ERROR_SYSCALL:
  14002. #ifdef NT
  14003.             {
  14004.                 int gle = GetLastError();
  14005.             }
  14006. #endif /* NT */
  14007.           case SSL_ERROR_WANT_X509_LOOKUP:
  14008.           case SSL_ERROR_SSL:
  14009.           case SSL_ERROR_ZERO_RETURN:
  14010.           default:
  14011.             lostpeer();
  14012.             return;
  14013.         }
  14014.     } else
  14015. #endif /* CK_SSL */
  14016.     {
  14017.         /*
  14018.          * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
  14019.          * after urgent byte rather than before as is protocol now.
  14020.          */
  14021.         buf[0] = IAC;
  14022.         buf[1] = TN_IP;
  14023.         buf[2] = IAC;
  14024.         buf[3] = NUL;
  14025.         if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
  14026.           perror("cancel");
  14027.         debug(F101,"ftp cancel_remote send 1","",x);
  14028.         buf[0] = TN_DM;
  14029.         x = send(csocket,(SENDARG2TYPE)buf,1,0);
  14030.         debug(F101,"ftp cancel_remote send 2","",x);
  14031.     }
  14032.     x = scommand("ABOR");
  14033.     debug(F101,"ftp cancel_remote scommand","",x);
  14034. #ifdef BSDSELECT
  14035.     FD_ZERO(&mask);
  14036.     FD_SET(csocket, &mask);
  14037.     if (din) {
  14038.         FD_SET(din, &mask);
  14039.     }
  14040.     nfnd = empty(&mask, 10);
  14041.     debug(F101,"ftp cancel_remote empty","",nfnd);
  14042.     if ((nfnd) <= 0) {
  14043.         if (nfnd < 0) {
  14044.             perror("cancel");
  14045.         }
  14046. #ifdef FTP_PROXY
  14047.         if (ptabflg)
  14048.           ftpcode = -1;
  14049. #endif /* FTP_PROXY */
  14050.         lostpeer();
  14051.     }
  14052.     debug(F110,"ftp cancel_remote","D",0);
  14053.     if (din && FD_ISSET(din, &mask)) {
  14054.         /* Security: No threat associated with this read. */
  14055.         /* But you can't simply read the TLS data stream  */
  14056. #ifdef CK_SSL
  14057.         if (ssl_ftp_data_active_flag) {
  14058.             int count, error;
  14059.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  14060.                     /* LOOP */ ;
  14061.         } else
  14062. #endif /* CK_SSL */
  14063.         {
  14064.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  14065.                 /* LOOP */ ;
  14066.         }
  14067.     }
  14068.     debug(F110,"ftp cancel_remote","E",0);
  14069. #else /* BSDSELECT */
  14070. #ifdef IBMSELECT
  14071.     fds[0] = csocket;
  14072.     fdcnt++;
  14073.     if (din) {
  14074.         fds[1] = din;
  14075.         fdcnt++;
  14076.     }
  14077.     nfnd = empty(fds, fdcnt, 10);
  14078.     debug(F101,"ftp cancel_remote empty","",nfnd);
  14079.     if ((nfnd) <= 0) {
  14080.         if (nfnd < 0) {
  14081.             perror("cancel");
  14082.         }
  14083. #ifdef FTP_PROXY
  14084.         if (ptabflg)
  14085.           ftpcode = -1;
  14086. #endif /* FTP_PROXY */
  14087.         lostpeer();
  14088.     }
  14089.     debug(F110,"ftp cancel_remote","D",0);
  14090.     if (din && select(&din, 1,0,0,1) ) {
  14091. #ifdef CK_SSL
  14092.         if (ssl_ftp_data_active_flag) {
  14093.             int count, error;
  14094.             while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
  14095.                     /* LOOP */ ;
  14096.         } else
  14097. #endif /* CK_SSL */
  14098.         {
  14099.             while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
  14100.                 /* LOOP */ ;
  14101.         }
  14102.     }
  14103.     debug(F110,"ftp cancel_remote","E",0);
  14104. #else /* IBMSELECT */
  14105.     Some form of select is required.
  14106. #endif /* IBMSELECT */
  14107. #endif /* BSDSELECT */
  14108.     if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
  14109.         debug(F110,"ftp cancel_remote","F",0);
  14110.         /* 552 needed for NIC style cancel */
  14111.         getreply(0,-1,-1,ftp_vbm,0);
  14112.         debug(F110,"ftp cancel_remote","G",0);
  14113.     }
  14114.     debug(F110,"ftp cancel_remote","H",0);
  14115.     getreply(0,-1,-1,ftp_vbm,0);
  14116.     debug(F110,"ftp cancel_remote","I",0);
  14117. #ifdef DEBUG
  14118.     debtim = xdebtim;
  14119. #endif /* DEBUG */
  14120. }
  14121.  
  14122. static int
  14123. fts_dpl(x) int x; {
  14124.     if (!auth_type
  14125. #ifdef OS2
  14126.          || !ck_crypt_is_installed()
  14127. #endif /* OS2 */
  14128.          ) {
  14129.         switch ( x ) {
  14130.           case FPL_PRV:
  14131.             printf("?Cannot set protection level to PRIVATE\n");
  14132.             return(0);
  14133.           case FPL_SAF:
  14134.             printf("?Cannot set protection level to SAFE\n");
  14135.             return(0);
  14136.         }
  14137.         ftp_dpl = x;
  14138.         return(1);
  14139.     }
  14140.  
  14141. #ifdef CK_SSL
  14142.     if (x == FPL_SAF &&
  14143.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  14144.         printf("Cannot set protection level to safe\n");
  14145.         return(0);
  14146.     }
  14147. #endif /* CK_SSL */
  14148.     /* Start with a PBSZ of 1 meg */
  14149.     if (x != FPL_CLR) {
  14150.         if (setpbsz(DEFAULT_PBSZ) < 0)
  14151.           return(0);
  14152.     }
  14153.     y = ftpcmd(x == FPL_CLR ? "PROT C" :
  14154.                (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
  14155.     if (y == REPLY_COMPLETE) {
  14156.         ftp_dpl = x;
  14157.         return(1);
  14158.     }
  14159.     return(0);
  14160. }
  14161.  
  14162. static int
  14163. fts_cpl(x) int x; {
  14164.     if (!auth_type 
  14165. #ifdef OS2
  14166.          || !ck_crypt_is_installed()
  14167. #endif /* OS2 */
  14168.          ) {
  14169.         switch ( x ) {
  14170.           case FPL_PRV:
  14171.             printf("?Cannot set protection level to PRIVATE\n");
  14172.             return(0);
  14173.           case FPL_SAF:
  14174.             printf("?Cannot set protection level to SAFE\n");
  14175.             return(0);
  14176.         }
  14177.         ftp_cpl = x;
  14178.         return(1);
  14179.     }
  14180. #ifdef CK_SSL
  14181.     if (x == FPL_SAF &&
  14182.         (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
  14183.         printf("Cannot set protection level to safe\n");
  14184.         return(0);
  14185.     }
  14186. #endif /* CK_SSL */
  14187.     if (x == FPL_CLR) {
  14188.         y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
  14189.         if (y == REPLY_COMPLETE) {
  14190. #ifdef CK_SSL
  14191.         if (ssl_ftp_active_flag) {
  14192.         /* must perform an SSL shutdown */
  14193.         SSL_shutdown(ssl_ftp_con);
  14194.         SSL_free(ssl_ftp_con);
  14195.         ssl_ftp_proxy = 0;
  14196.         ssl_ftp_active_flag = 0;
  14197.         ssl_ftp_con = NULL;
  14198.         }
  14199. #endif /* CK_SSL */
  14200.             ftp_cpl = x;
  14201.             return(1);
  14202.         }
  14203.         return(0);
  14204.     }
  14205.     ftp_cpl = x;
  14206.     return(1);
  14207. }
  14208.  
  14209. #ifdef FTP_GSSAPI
  14210. static VOID
  14211. user_gss_error(maj_stat, min_stat, s)
  14212.     OM_uint32 maj_stat, min_stat;
  14213.     char *s;
  14214. {
  14215.     /* a lot of work just to report the error */
  14216.     OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
  14217.     gss_buffer_desc msg;
  14218.     msg_ctx = 0;
  14219.     while (!msg_ctx) {
  14220.         gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
  14221.                                        GSS_C_GSS_CODE,
  14222.                                        GSS_C_NULL_OID,
  14223.                                        &msg_ctx,
  14224.                                        &msg
  14225.                                        );
  14226.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14227.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14228.             fprintf(stderr, "GSSAPI error major: %s\n",
  14229.                     (char*)msg.value);
  14230.             gss_release_buffer(&gmin_stat, &msg);
  14231.         }
  14232.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14233.           break;
  14234.     }
  14235.     msg_ctx = 0;
  14236.     while (!msg_ctx) {
  14237.         gmaj_stat = gss_display_status(&gmin_stat, min_stat,
  14238.                                        GSS_C_MECH_CODE,
  14239.                                        GSS_C_NULL_OID,
  14240.                                        &msg_ctx,
  14241.                                        &msg
  14242.                                        );
  14243.         if ((gmaj_stat == GSS_S_COMPLETE)||
  14244.             (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
  14245.             fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
  14246.             gss_release_buffer(&gmin_stat, &msg);
  14247.         }
  14248.         if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
  14249.           break;
  14250.     }
  14251.     fprintf(stderr, "GSSAPI error: %s\n", s);
  14252. }
  14253. #endif /* FTP_GSSAPI */
  14254.  
  14255. #ifndef NOMHHOST
  14256. #ifdef datageneral
  14257. #define NOMHHOST
  14258. #else
  14259. #ifdef HPUX5WINTCP
  14260. #define NOMHHOST
  14261. #endif /* HPUX5WINTCP */
  14262. #endif /* datageneral */
  14263. #endif /* NOMHHOST */
  14264.  
  14265. #ifdef INADDRX
  14266. static struct in_addr inaddrx;
  14267. #endif /* INADDRX */
  14268.  
  14269. static char *
  14270. ftp_hookup(host, port, tls) char * host; int port; int tls; {
  14271.     register struct hostent *hp = 0;
  14272. #ifdef IP_TOS
  14273. #ifdef IPTOS_THROUGHPUT
  14274.     int tos;
  14275. #endif /* IPTOS_THROUGHPUT */
  14276. #endif /* IP_TOS */
  14277.     int s;
  14278.     GSOCKNAME_T len;
  14279.     static char hostnamebuf[MAXHOSTNAMELEN];
  14280.     char hostname[MAXHOSTNAMELEN] /* , *p, *q */ ;
  14281.     int  cport;
  14282. #ifdef DEBUG
  14283.     extern int debtim;
  14284.     int xdebtim;
  14285.     xdebtim = debtim;
  14286.     debtim = 1;
  14287. #endif /* DEBUG */
  14288.  
  14289. #ifndef NOHTTP
  14290.     if (tcp_http_proxy) {
  14291.         struct servent *destsp;
  14292.         char *p, *q;
  14293.  
  14294.         ckmakmsg(proxyhost,HTTPCPYL,host,":",ckuitoa(port),NULL);
  14295.         for (p = tcp_http_proxy, q = hostname;
  14296.              *p != '\0' && *p != ':';
  14297.              p++, q++
  14298.              )
  14299.           *q = *p;
  14300.         *q = '\0';
  14301.  
  14302.         if (*p == ':')
  14303.           p++;
  14304.         else
  14305.           p = "http";
  14306.  
  14307.         destsp = getservbyname(p,"tcp");
  14308.         if (destsp)
  14309.           cport = ntohs(destsp->s_port);
  14310.         else if (p) {
  14311.           cport = atoi(p);
  14312.         } else
  14313.           cport = 80;
  14314.     } else
  14315. #endif /* NOHTTP */
  14316.     {
  14317.         ckstrncpy(hostname,host,MAXHOSTNAMELEN);
  14318.         cport = port;
  14319.     }
  14320.     memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
  14321.     hisctladdr.sin_addr.s_addr = inet_addr(host);
  14322.     if (hisctladdr.sin_addr.s_addr != -1) {
  14323.         debug(F110,"ftp hookup A",hostname,0);
  14324.         hisctladdr.sin_family = AF_INET;
  14325.         ckstrncpy(hostnamebuf, hostname, MAXHOSTNAMELEN);
  14326.     } else {
  14327.         debug(F110,"ftp hookup B",hostname,0);
  14328.         hp = gethostbyname(hostname);
  14329. #ifdef HADDRLIST
  14330.         hp = ck_copyhostent(hp);        /* make safe copy that won't change */
  14331. #endif /* HADDRLIST */
  14332.         if (hp == NULL) {
  14333.             fprintf(stderr, "ftp: %s: Unknown host\n", host);
  14334.             ftpcode = -1;
  14335. #ifdef DEBUG
  14336.             debtim = xdebtim;
  14337. #endif /* DEBUG */
  14338.             return((char *) 0);
  14339.         }
  14340.         hisctladdr.sin_family = hp->h_addrtype;
  14341. #ifdef HADDRLIST
  14342.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
  14343.                sizeof(hisctladdr.sin_addr));
  14344. #else /* HADDRLIST */
  14345.         memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
  14346.                sizeof(hisctladdr.sin_addr));
  14347. #endif /* HADDRLIST */
  14348.         ckstrncpy(hostnamebuf, hp->h_name, MAXHOSTNAMELEN);
  14349.     }
  14350.     debug(F110,"ftp hookup C",hostnamebuf,0);
  14351.     ftp_host = hostnamebuf;
  14352.     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14353.     debug(F101,"ftp hookup socket","",s);
  14354.     if (s < 0) {
  14355.         perror("ftp: socket");
  14356.         ftpcode = -1;
  14357. #ifdef DEBUG
  14358.         debtim = xdebtim;
  14359. #endif /* DEBUG */
  14360.         return(0);
  14361.     }
  14362.     hisctladdr.sin_port = htons(cport);
  14363.  
  14364.     /* If a specific TCP address on the local host is desired we */
  14365.     /* must bind it to the socket.                               */
  14366. #ifndef datageneral
  14367.     if (tcp_address) {
  14368.     int s_errno;
  14369.  
  14370.     debug(F110,"ftp_hookup binding socket to",tcp_address,0);
  14371. #ifdef INADDRX
  14372.     inaddrx = inet_addr(tcp_address);
  14373.     hisctladdr.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  14374. #else
  14375.     hisctladdr.sin_addr.s_addr = inet_addr(tcp_address);
  14376. #endif /* INADDRX */
  14377.     if (bind(s, (struct sockaddr *)&hisctladdr, sizeof(hisctladdr)) < 0) {
  14378.         s_errno = socket_errno; /* Save error code */
  14379. #ifdef TCPIPLIB
  14380.         socket_close(s);
  14381. #else /* TCPIPLIB */
  14382.         close(s);
  14383. #endif /* TCPIPLIB */
  14384.         errno = s_errno;       /* and report this error */
  14385.         debug(F101,"ftp_hookup bind errno","",errno);
  14386.         perror("ftp: bind");
  14387.         ftpcode = -1;
  14388.         goto bad;
  14389.     }
  14390.     }
  14391. #endif /* datageneral */
  14392.  
  14393.     errno = 0;
  14394. #ifdef HADDRLIST
  14395.     debug(F100,"ftp hookup HADDRLIST","",0);
  14396.     while
  14397. #else
  14398.     debug(F100,"ftp hookup no HADDRLIST","",0);
  14399.     if
  14400. #endif /* HADDRLIST */
  14401.       (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
  14402.           debug(F101,"ftp hookup connect failed","",errno);
  14403. #ifdef HADDRLIST
  14404.           if (hp && hp->h_addr_list[1]) {
  14405.               int oerrno = errno;
  14406.  
  14407.               fprintf(stderr, "ftp: connect to address %s: ",
  14408.                       inet_ntoa(hisctladdr.sin_addr));
  14409.               errno = oerrno;
  14410.               perror((char *) 0);
  14411.               hp->h_addr_list++;
  14412.               memcpy((char *)&hisctladdr.sin_addr,
  14413.                      hp->h_addr_list[0],
  14414.                      sizeof(hisctladdr.sin_addr));
  14415.               fprintf(stdout, "Trying %s...\n",
  14416.                       inet_ntoa(hisctladdr.sin_addr));
  14417. #ifdef TCPIPLIB
  14418.               socket_close(s);
  14419. #else /* TCPIPLIB */
  14420.               close(s);
  14421. #endif /* TCPIPLIB */
  14422.               s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
  14423.               if (s < 0) {
  14424.                   perror("ftp: socket");
  14425.                   ftpcode = -1;
  14426. #ifdef DEBUG
  14427.                   debtim = xdebtim;
  14428. #endif /* DEBUG */
  14429.                   return(0);
  14430.               }
  14431.               continue;
  14432.           }
  14433. #endif /* HADDRLIST */
  14434.           perror("ftp: connect");
  14435.           ftpcode = -1;
  14436.           goto bad;
  14437.       }
  14438.     debug(F100,"ftp hookup connect ok","",0);
  14439.  
  14440.     len = sizeof (myctladdr);
  14441.     errno = 0;
  14442.     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
  14443.         debug(F101,"ftp hookup getsockname failed","",errno);
  14444.         perror("ftp: getsockname");
  14445.         ftpcode = -1;
  14446.         goto bad;
  14447.     }
  14448.     debug(F100,"ftp hookup getsockname ok","",0);
  14449.  
  14450. #ifndef NOHTTP
  14451.     if (tcp_http_proxy) {
  14452. #ifdef OS2
  14453.         char * agent = "Kermit 95";     /* Default user agent */
  14454. #else
  14455.         char * agent = "C-Kermit";
  14456. #endif /* OS2 */
  14457.  
  14458.         if (http_connect(s,agent,NULL,
  14459.                          tcp_http_proxy_user,
  14460.                          tcp_http_proxy_pwd,
  14461.                          0,
  14462.                          proxyhost
  14463.                          ) < 0) {
  14464.             char * foo = NULL;
  14465. #ifdef TCPIPLIB
  14466.             socket_close(s);
  14467. #else /* TCPIPLIB */
  14468.             close(s);
  14469. #endif /* TCPIPLIB */
  14470.  
  14471.             while (foo == NULL && tcp_http_proxy != NULL ) {
  14472.  
  14473.                 if (tcp_http_proxy_errno == 401 ||
  14474.                      tcp_http_proxy_errno == 407 ) {
  14475.                     char uid[UIDBUFLEN];
  14476.                     char pwd[PWDSIZ];
  14477.                     struct txtbox tb[2];
  14478.                     int ok;
  14479.  
  14480.                     tb[0].t_buf = uid;
  14481.                     tb[0].t_len = UIDBUFLEN;
  14482.                     tb[0].t_lbl = "Proxy Userid: ";
  14483.                     tb[0].t_dflt = NULL;
  14484.                     tb[0].t_echo = 1;
  14485.                     tb[1].t_buf = pwd;
  14486.                     tb[1].t_len = 256;
  14487.                     tb[1].t_lbl = "Proxy Passphrase: ";
  14488.                     tb[1].t_dflt = NULL;
  14489.                     tb[1].t_echo = 2;
  14490.  
  14491.                     ok = uq_mtxt("Proxy Server Authentication Required\n",
  14492.                                   NULL, 2, tb);
  14493.  
  14494.                     if (ok && uid[0]) {
  14495.                         char * proxy_user, * proxy_pwd;
  14496.  
  14497.                         proxy_user = tcp_http_proxy_user;
  14498.                         proxy_pwd  = tcp_http_proxy_pwd;
  14499.  
  14500.                         tcp_http_proxy_user = uid;
  14501.                         tcp_http_proxy_pwd = pwd;
  14502.  
  14503.                         foo = ftp_hookup(host, port, 0);
  14504.  
  14505.                         debug(F110,"ftp_hookup()",foo,0);
  14506.                         memset(pwd,0,PWDSIZ);
  14507.                         tcp_http_proxy_user = proxy_user;
  14508.                         tcp_http_proxy_pwd = proxy_pwd;
  14509.                     } else
  14510.                         break;
  14511.                 } else
  14512.                     break;
  14513.             }
  14514.             if (foo != NULL)
  14515.               return(foo);
  14516.             perror("ftp: connect");
  14517.             ftpcode = -1;
  14518.             goto bad;
  14519.         }
  14520.         ckstrncpy(hostnamebuf, proxyhost, MAXHOSTNAMELEN);
  14521.     }
  14522. #endif /* NOHTTP */
  14523.  
  14524.     csocket = s;
  14525.  
  14526. #ifdef CK_SSL
  14527.     if (tls) {
  14528.         /* FTP over SSL
  14529.          * If the connection is over an SSL proxy then the
  14530.          * auth_type will be NULL.  However, I'm not sure
  14531.          * whether we should protect the data channel in
  14532.          * that case or not.
  14533.          */
  14534.  
  14535.         debug(F100,"ftp hookup use_tls","",0);
  14536.         if (!ssl_auth()) {
  14537.             debug(F100,"ftp hookup ssl_auth failed","",0);
  14538.             auth_type = NULL;
  14539.             ftpcode = -1;
  14540.             csocket = -1;
  14541.             goto bad;
  14542.         }
  14543.         ssl_ftp_proxy = 1;
  14544.     }
  14545. #endif /* CK_SSL */
  14546.  
  14547. #ifdef IP_TOS
  14548. #ifdef IPTOS_LOWDELAY
  14549.     tos = IPTOS_LOWDELAY;
  14550.     if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
  14551.       perror("ftp: setsockopt TOS (ignored)");
  14552. #endif
  14553. #endif
  14554.     if (!quiet)
  14555.       printf("Connected to %s.\n", host);
  14556.  
  14557.     /* Read greeting from server */
  14558.     if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
  14559.         debug(F100,"ftp hookup bad reply","",0);
  14560. #ifdef TCPIPLIB
  14561.         socket_close(csocket);
  14562. #else /* TCPIPLIB */
  14563.         close(csocket);
  14564. #endif /* TCPIPLIB */
  14565.         ftpcode = -1;
  14566.         goto bad;
  14567.     }
  14568. #ifdef SO_OOBINLINE
  14569.     {
  14570.         int on = 1;
  14571.         errno = 0;
  14572.         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
  14573.                sizeof(on)) < 0) {
  14574.             perror("ftp: setsockopt");
  14575.             debug(F101,"ftp hookup setsockopt failed","",errno);
  14576.         }
  14577. #ifdef DEBUG
  14578.         else
  14579.           debug(F100,"ftp hookup setsockopt ok","",0);
  14580. #endif /* DEBUG */
  14581.     }
  14582. #endif /* SO_OOBINLINE */
  14583.  
  14584. #ifdef DEBUG
  14585.     debtim = xdebtim;
  14586. #endif /* DEBUG */
  14587.     return(ftp_host);
  14588.  
  14589.   bad:
  14590.     debug(F100,"ftp hookup bad","",0);
  14591. #ifdef TCPIPLIB
  14592.     socket_close(s);
  14593. #else /* TCPIPLIB */
  14594.     close(s);
  14595. #endif /* TCPIPLIB */
  14596. #ifdef DEBUG
  14597.     debtim = xdebtim;
  14598. #endif /* DEBUG */
  14599.     csocket = -1;
  14600.     return((char *)0);
  14601. }
  14602.  
  14603. static VOID
  14604. ftp_init() {
  14605.     int i, n;
  14606.  
  14607.     /* The purpose of the initial REST 0 is not clear, but other FTP */
  14608.     /* clients do it.  In any case, failure of this command is not a */
  14609.     /* reliable indication that the server does not support Restart. */
  14610.  
  14611.     okrestart = 0;
  14612.     if (!noinit) {
  14613.         n = ftpcmd("REST 0",NULL,0,0,0);
  14614.         if (n == REPLY_COMPLETE)
  14615.           okrestart = 1;
  14616. #ifdef COMMENT
  14617.         else if (ftp_deb)
  14618.           printf("WARNING: Unable to restore file pointer.\n");
  14619. #endif /* COMMENT */
  14620.     }
  14621.     n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
  14622.     if (n == REPLY_COMPLETE) {
  14623.         register char *cp, c = NUL;
  14624.         cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
  14625.         if (cp == NULL)
  14626.           cp = ckstrchr(ftp_reply_str+4,'\r');
  14627.         if (cp) {
  14628.             if (cp[-1] == '.')
  14629.               cp--;
  14630.             c = *cp;                    /* Save this char */
  14631.             *cp = '\0';                 /* Replace it with NUL */
  14632.         }
  14633.         if (!quiet)
  14634.           printf("Remote system type is %s.\n",ftp_reply_str+4);
  14635.         ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
  14636.         if (cp)                         /* Put back saved char */
  14637.           *cp = c;
  14638.     }
  14639.     alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
  14640.  
  14641.     if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
  14642.     else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
  14643.     else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
  14644.     else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
  14645.     else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
  14646.     else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
  14647.     else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
  14648.  
  14649. #ifdef FTP_PROXY
  14650.     unix_proxy = 0;
  14651.     if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
  14652. #endif /* FTP_PROXY */
  14653.  
  14654.     if (ftp_cmdlin && xfermode == XMODE_M)
  14655.       ftp_typ = binary;                 /* Type given on command line */
  14656.     else                                /* Otherwise set it automatically */
  14657.       ftp_typ = alike ? FTT_BIN : FTT_ASC;
  14658.     changetype(ftp_typ,0);              /* Change to this type */
  14659.     g_ftp_typ = ftp_typ;                /* Make it the global type */
  14660.     if (!quiet)
  14661.       printf("Default transfer mode is %s\n",
  14662.              ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
  14663.              );
  14664.     for (i = 0; i < 16; i++)        /* Init server FEATure table */
  14665.       sfttab[i] = 0;
  14666.     if (!noinit) {
  14667.         n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
  14668. #ifdef COMMENT
  14669.         if (n != REPLY_COMPLETE)
  14670.           printf("WARNING: Server does not accept MODE S(TREAM)\n");
  14671. #endif /* COMMENT */
  14672.         n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
  14673. #ifdef COMMENT
  14674.         if (n != REPLY_COMPLETE)
  14675.           printf("WARNING: Server does not accept STRU F(ILE)\n");
  14676. #endif /* COMMENT */
  14677.     if (featok) {
  14678.         n = ftpcmd("FEAT",NULL,0,0,0); /* Ask server about features */
  14679.         if (n == REPLY_COMPLETE) {
  14680.         debug(F101,"ftp_init FEAT","",sfttab[0]);
  14681.         if (deblog || ftp_deb) {
  14682.             int i;
  14683.             for (i = 1; i < 16 && i < nfeattab; i++) {
  14684.             debug(F111,"ftp_init FEAT",feattab[i].kwd,sfttab[i]);
  14685.             if (ftp_deb)
  14686.               printf("  Server %s %s\n",
  14687.                  sfttab[i] ? "supports" : "does not support",
  14688.                  feattab[i].kwd
  14689.                  );
  14690.             }
  14691.             /* Deal with disabled MLST opts here if necessary */
  14692.             /* But why would it be? */
  14693.         }
  14694.         }
  14695.     }
  14696.     }
  14697. }
  14698.  
  14699. static int
  14700. ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
  14701.     static char ftppass[PASSBUFSIZ]="";
  14702.     char tmp[PASSBUFSIZ];
  14703.     char *user = NULL, *pass = NULL, *acct = NULL;
  14704.     int n, aflag = 0;
  14705.     extern char uidbuf[];
  14706.     extern char pwbuf[];
  14707.     extern int  pwflg, pwcrypt;
  14708.  
  14709.     debug(F111,"ftp_login",ftp_logname,ftp_log);
  14710.  
  14711.     if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
  14712.       anonymous = 1;
  14713.     if (!ckstrcmp(ftp_logname,"ftp",-1,0))
  14714.       anonymous = 1;
  14715.  
  14716. #ifdef FTP_SRP
  14717.     if (auth_type && !strcmp(auth_type, "SRP")) {
  14718.         user = srp_user;
  14719.         pass = srp_pass;
  14720.         acct = srp_acct;
  14721.     } else
  14722. #endif /* FTP_SRP */
  14723.       if (anonymous) {
  14724.           user = "anonymous";
  14725.           if (ftp_tmp) {        /* They gave a password */
  14726.               pass = ftp_tmp;
  14727.           } else if (ftp_apw) {        /* SET FTP ANONYMOUS-PASSWORD */
  14728.           pass = ftp_apw;
  14729.       } else {            /* Supply user@host */
  14730.           ckmakmsg(tmp,PASSBUFSIZ,whoami(),"@",myhost,NULL);
  14731.           pass = tmp;
  14732.           }
  14733.       debug(F110,"ftp anonymous",pass,0);
  14734.       } else {
  14735. #ifdef USE_RUSERPASS
  14736.           if (ruserpass(host, &user, &pass, &acct) < 0) {
  14737.               ftpcode = -1;
  14738.               return(0);
  14739.           }
  14740. #endif /* USE_RUSERPASS */
  14741.           if (ftp_logname) {
  14742.               user = ftp_logname;
  14743.               pass = ftp_tmp;
  14744.           } else if (uidbuf[0] && (ftp_tmp || pwbuf[0] && pwflg)) {
  14745.               user = uidbuf;
  14746.               if (ftp_tmp) {
  14747.                   pass = ftp_tmp;
  14748.               } else if (pwbuf[0] && pwflg) {
  14749.                   ckstrncpy(ftppass,pwbuf,PASSBUFSIZ);
  14750. #ifdef OS2
  14751.                   if ( pwcrypt )
  14752.                       ck_encrypt((char *)ftppass);
  14753. #endif /* OS2 */
  14754.                   pass = ftppass;
  14755.               }
  14756.           }
  14757.           acct = ftp_acc;
  14758.           while (user == NULL) {
  14759.               char *myname, prompt[PROMPTSIZ];
  14760.               int ok;
  14761.  
  14762.               myname = whoami();
  14763.               if (myname)
  14764.                 ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  14765.                           NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  14766.               else
  14767.                 ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  14768.               tmp[0] = '\0';
  14769.               
  14770.               ok = uq_txt(NULL,prompt,1,NULL,tmp,PASSBUFSIZ,NULL,
  14771.                           DEFAULT_UQ_TIMEOUT);
  14772.               if (!ok || *tmp == '\0')
  14773.                 user = myname;
  14774.               else
  14775.                 user = brstrip(tmp);
  14776.           }
  14777.       }
  14778.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14779.     if (n == REPLY_COMPLETE) {
  14780.         /* determine if we need to send a dummy password */
  14781.         if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
  14782.           ftpcmd("PASS dummy",NULL,0,0,1);
  14783.     } else if (n == REPLY_CONTINUE) {
  14784. #ifdef CK_ENCRYPTION
  14785.         int oldftp_cpl;
  14786. #endif /* CK_ENCRYPTION */
  14787.  
  14788.         if (pass == NULL) {
  14789.             int ok;
  14790.             setint();
  14791.             ok = uq_txt(NULL," Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
  14792.                         DEFAULT_UQ_TIMEOUT);
  14793.             if (ok)
  14794.                 pass = brstrip(ftppass);
  14795.         }
  14796.  
  14797. #ifdef CK_ENCRYPTION
  14798.         oldftp_cpl = ftp_cpl;
  14799.         ftp_cpl = FPL_PRV;
  14800. #endif /* CK_ENCRYPTION */
  14801.         n = ftpcmd("PASS",pass,-1,-1,1);
  14802.         if (!anonymous && pass) {
  14803.             char * p = pass;
  14804.             while (*p++) *(p-1) = NUL;
  14805.             makestr(&ftp_tmp,NULL);
  14806.         }
  14807. #ifdef CK_ENCRYPTION
  14808.         /* level may have changed */
  14809.         if (ftp_cpl == FPL_PRV)
  14810.           ftp_cpl = oldftp_cpl;
  14811. #endif /* CK_ENCRYPTION */
  14812.     }
  14813.     if (n == REPLY_CONTINUE) {
  14814.         aflag++;
  14815.         if (acct == NULL) {
  14816.             static char ftpacct[80];
  14817.             int ok;
  14818.             setint();
  14819.             ok = uq_txt(NULL," Account: ",2,NULL,ftpacct,80,NULL,
  14820.                         DEFAULT_UQ_TIMEOUT);
  14821.             if (ok)
  14822.                 acct = brstrip(ftpacct);
  14823.         }
  14824.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14825.     }
  14826.     if (n != REPLY_COMPLETE) {
  14827.         fprintf(stderr, "FTP login failed.\n");
  14828.         if (haveurl)
  14829.           doexit(BAD_EXIT,-1);
  14830.         return(0);
  14831.     }
  14832.     if (!aflag && acct != NULL) {
  14833.         ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14834.     }
  14835.     makestr(&ftp_logname,user);
  14836.     loggedin = 1;
  14837. #ifdef LOCUS
  14838.     /* Unprefixed file management commands go to server */
  14839.     if (autolocus && !ftp_cmdlin) {
  14840.     setlocus(0,1);
  14841.     }
  14842. #endif /* LOCUS */
  14843.     ftp_init();
  14844.  
  14845.     if (anonymous && !quiet) {
  14846.         printf(" Logged in as anonymous (%s)\n",pass);
  14847.         memset(pass, 0, strlen(pass));
  14848.     }
  14849.     if (ftp_rdir) {
  14850.         if (doftpcwd(ftp_rdir,-1) < 1)
  14851.           doexit(BAD_EXIT,-1);
  14852.     }
  14853.  
  14854. #ifdef FTP_PROXY
  14855.     if (proxy)
  14856.       return(1);
  14857. #endif /* FTP_PROXY */
  14858.     return(1);
  14859. }
  14860.  
  14861. static int
  14862. ftp_reset() {
  14863.     int rc;
  14864. #ifdef BSDSELECT
  14865.     int nfnd = 1;
  14866.     fd_set mask;
  14867.     FD_ZERO(&mask);
  14868.     while (nfnd > 0) {
  14869.         FD_SET(csocket, &mask);
  14870.         if ((nfnd = empty(&mask,0)) < 0) {
  14871.             perror("reset");
  14872.             ftpcode = -1;
  14873.             lostpeer();
  14874.             return(0);
  14875.         } else if (nfnd) {
  14876.             getreply(0,-1,-1,ftp_vbm,0);
  14877.         }
  14878.     }
  14879. #else /* BSDSELECT */
  14880. #ifdef IBMSELECT
  14881.     int nfnd = 1;
  14882.     while (nfnd > 0) {
  14883.         if ((nfnd = empty(&csocket,1,0)) < 0) {
  14884.             perror("reset");
  14885.             ftpcode = -1;
  14886.             lostpeer();
  14887.             return(0);
  14888.         } else if (nfnd) {
  14889.             getreply(0,-1,-1,ftp_vbm,0);
  14890.         }
  14891.     }
  14892. #endif /* IBMSELECT */
  14893. #endif /* BSDSELECT */
  14894.     rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
  14895.     if (rc > 0)
  14896.       loggedin = 0;
  14897.     return(rc);
  14898. }
  14899.  
  14900. static int
  14901. ftp_rename(from, to) char * from, * to; {
  14902.     int lcs = -1, rcs = -1;
  14903. #ifndef NOCSETS
  14904.     if (ftp_xla) {
  14905.         lcs = ftp_csl;
  14906.         if (lcs < 0) lcs = fcharset;
  14907.         rcs = ftp_csx;
  14908.         if (rcs < 0) rcs = ftp_csr;
  14909.     }
  14910. #endif /* NOCSETS */
  14911.     if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
  14912.         return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
  14913.     }
  14914.     return(0);                          /* Failure */
  14915. }
  14916.  
  14917. static int
  14918. ftp_umask(mask) char * mask; {
  14919.     int rc;
  14920.     rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
  14921.     return(rc);
  14922. }
  14923.  
  14924. static int
  14925. ftp_user(user,pass,acct) char * user, * pass, * acct; {
  14926.     int n = 0, aflag = 0;
  14927.     char pwd[PWDSIZ];
  14928.  
  14929.     if (!auth_type && ftp_aut) {
  14930. #ifdef FTP_SRP
  14931.         if (ck_srp_is_installed()) {
  14932.             if (srp_ftp_auth( NULL, user, pass)) {
  14933.                 makestr(&pass,srp_pass);
  14934.             }
  14935.         }
  14936. #endif /* FTP_SRP */
  14937.     }
  14938.     n = ftpcmd("USER",user,-1,-1,ftp_vbm);
  14939.     if (n == REPLY_COMPLETE)
  14940.       n = ftpcmd("PASS dummy",NULL,0,0,1);
  14941.     else if (n == REPLY_CONTINUE) {
  14942. #ifdef CK_ENCRYPTION
  14943.         int oldftp_cpl;
  14944. #endif /* CK_ENCRYPTION */
  14945.         if (pass == NULL || !pass[0]) {
  14946.             int ok;
  14947.             pwd[0] = '\0';
  14948.             setint();
  14949.             ok = uq_txt(NULL," Password: ",2,NULL,pwd,PWDSIZ,NULL,
  14950.                         DEFAULT_UQ_TIMEOUT);
  14951.             if (ok)
  14952.                 pass = brstrip(pwd);
  14953.         }
  14954.  
  14955. #ifdef CK_ENCRYPTION
  14956.         if ((oldftp_cpl = ftp_cpl) == PROT_S)
  14957.           ftp_cpl = PROT_P;
  14958. #endif /* CK_ENCRYPTION */
  14959.         n = ftpcmd("PASS",pass,-1,-1,1);
  14960.         memset(pass, 0, strlen(pass));
  14961. #ifdef CK_ENCRYPTION
  14962.         /* level may have changed */
  14963.         if (ftp_cpl == PROT_P)
  14964.           ftp_cpl = oldftp_cpl;
  14965. #endif /* CK_ENCRYPTION */
  14966.     }
  14967.     if (n == REPLY_CONTINUE) {
  14968.         if (acct == NULL || !acct[0]) {
  14969.             int ok;
  14970.             pwd[0] = '\0';
  14971.             setint();
  14972.             ok = uq_txt(NULL," Account: ",2,NULL,pwd,PWDSIZ,NULL,
  14973.                         DEFAULT_UQ_TIMEOUT);
  14974.             if (ok)
  14975.                 acct = pwd;
  14976.         }
  14977.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14978.         aflag++;
  14979.     }
  14980.     if (n != REPLY_COMPLETE) {
  14981.         printf("Login failed.\n");
  14982.         return(0);
  14983.     }
  14984.     if (!aflag && acct != NULL && acct[0]) {
  14985.         n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
  14986.     }
  14987.     if (n == REPLY_COMPLETE) {
  14988.         makestr(&ftp_logname,user);
  14989.         loggedin = 1;
  14990.         ftp_init();
  14991.         return(1);
  14992.     }
  14993.     return(0);
  14994. }
  14995.  
  14996. char *
  14997. ftp_authtype() {
  14998.     if (!connected)
  14999.       return("NULL");
  15000.     return(auth_type ? auth_type : "NULL");
  15001. }
  15002.  
  15003. char *
  15004. ftp_cpl_mode() {
  15005.     switch (ftp_cpl) {
  15006.       case FPL_CLR:
  15007.         return("clear");
  15008.       case FPL_SAF:
  15009.         return("safe");
  15010.       case FPL_PRV:
  15011.         return("private");
  15012.       case FPL_CON:
  15013.         return("confidential");
  15014.       default:
  15015.         return("(error)");
  15016.     }
  15017. }
  15018.  
  15019. char *
  15020. ftp_dpl_mode() {
  15021.     switch (ftp_dpl) {
  15022.       case FPL_CLR:
  15023.         return("clear");
  15024.       case FPL_SAF:
  15025.         return("safe");
  15026.       case FPL_PRV:
  15027.         return("private");
  15028.       case FPL_CON:
  15029.         return("confidential");
  15030.       default:
  15031.         return("(error)");
  15032.     }
  15033. }
  15034.  
  15035.  
  15036. /* remote_files() */
  15037. /*
  15038.    Returns next remote filename on success;
  15039.    NULL on error or no more files with global rfrc set to:
  15040.      -1: Bad argument
  15041.      -2: Server error response to NLST, e.g. file not found
  15042.      -3: No more files
  15043.      -9: Internal error
  15044. */
  15045. #define FTPNAMBUFLEN CKMAXPATH+1024
  15046.  
  15047. /* Check: ckmaxfiles CKMAXOPEN */
  15048.  
  15049. #define MLSDEPTH 128            /* Stack of open temp files */
  15050. static int mlsdepth = 0;        /* Temp file stack depth */
  15051. static FILE * tmpfilptr[MLSDEPTH+1] = { NULL, NULL }; /* Temp file pointers */
  15052. static char * tmpfilnam[MLSDEPTH+1] = { NULL, NULL }; /* Temp file names */
  15053.  
  15054. static VOID
  15055. mlsreset() {                /* Reset MGET temp-file stack */
  15056.     int i;
  15057.     for (i = 0; i <= mlsdepth; i++) {
  15058.     if (tmpfilptr[i]) {
  15059.         fclose(tmpfilptr[i]);
  15060.         tmpfilptr[i] = NULL;
  15061.         if (tmpfilnam[i]) {
  15062. #ifdef OS2
  15063.         unlink(tmpfilnam[i]);
  15064. #endif /* OS2 */
  15065.         free(tmpfilnam[i]);
  15066.         }
  15067.     }
  15068.     }
  15069.     mlsdepth = 0;
  15070. }
  15071.  
  15072. static CHAR *
  15073. #ifdef CK_ANSIC
  15074. remote_files(int new_query, CHAR * arg, CHAR * pattern, int proxy_switch)
  15075. #else /* CK_ANSIC */
  15076. remote_files(new_query, arg, pattern, proxy_switch)
  15077.     int new_query;
  15078.     CHAR * arg;                /* That we send to the server */
  15079.     CHAR * pattern;            /* That we use locally */
  15080.     int proxy_switch;
  15081. #endif /* CK_ANSIC */
  15082. /* remote_files */ {
  15083.     static CHAR buf[FTPNAMBUFLEN];
  15084.     CHAR *cp, *whicharg;
  15085.     char * cdto = NULL;
  15086.     char * p;
  15087.     int i, x, forced = 0;
  15088.     int lcs = 0, rcs = 0, xlate = 0;
  15089.  
  15090.     debug(F101,"ftp remote_files new_query","",new_query);
  15091.     debug(F110,"ftp remote_files arg",arg,0);
  15092.     debug(F110,"ftp remote_files pattern",pattern,0);
  15093.  
  15094.     rfrc = -1;
  15095.     if (pattern)            /* Treat empty pattern same as NULL */
  15096.       if (!*pattern)
  15097.     pattern = NULL;
  15098.     if (arg)                /* Ditto for arg */
  15099.       if (!*arg)
  15100.     arg = NULL;
  15101.  
  15102.   again:
  15103.  
  15104.     if (new_query) {
  15105.         if (tmpfilptr[mlsdepth]) {
  15106.             fclose(tmpfilptr[mlsdepth]);
  15107.             tmpfilptr[mlsdepth] = NULL;
  15108. #ifdef OS2
  15109.             if (!ftp_deb && !deblog)
  15110.               unlink(tmpfilnam[mlsdepth]);
  15111. #endif /* OS2 */
  15112.         }
  15113.     }
  15114.     if (tmpfilptr[mlsdepth] == NULL) {
  15115.         extern char * tempdir;
  15116.         char * p;
  15117.         debug(F110,"ftp remote_files tempdir",tempdir,0);
  15118.         if (tempdir) {
  15119.             p = tempdir;
  15120.         } else {
  15121. #ifdef OS2
  15122. #ifdef NT
  15123.             p = getenv("K95TMP");
  15124. #else
  15125.             p = getenv("K2TMP");
  15126. #endif /* NT */
  15127.             if (!p)
  15128. #endif /* OS2 */
  15129.               p = getenv("CK_TMP");
  15130.             if (!p)
  15131.               p = getenv("TMPDIR");
  15132.             if (!p) p = getenv("TEMP");
  15133.             if (!p) p = getenv("TMP");
  15134. #ifdef OS2ORUNIX
  15135.             if (p) {
  15136.                 int len = strlen(p);
  15137.                 if (p[len-1] != '/'
  15138. #ifdef OS2
  15139.                     && p[len-1] != '\\'
  15140. #endif /* OS2 */
  15141.                      ) {
  15142.                     static char foo[CKMAXPATH];
  15143.                     ckstrncpy(foo,p,CKMAXPATH);
  15144.                     ckstrncat(foo,"/",CKMAXPATH);
  15145.                     p = foo;
  15146.                 }
  15147.             } else
  15148. #else /* OS2ORUNIX */
  15149.             if (!p)
  15150. #endif /* OS2ORUNIX */
  15151. #ifdef UNIX                             /* Systems that have a standard */
  15152.                 p = "/tmp/";            /* temporary directory... */
  15153. #else
  15154. #ifdef datageneral
  15155.             p = ":TMP:";
  15156. #else
  15157.             p = "";
  15158. #endif /* datageneral */
  15159. #endif /* UNIX */
  15160.         }
  15161.         debug(F110,"ftp remote_files p",p,0);
  15162.  
  15163.     /* Get temp file */
  15164.  
  15165.     if ((tmpfilnam[mlsdepth] = (char *)malloc(CKMAXPATH+1))) {
  15166.         ckmakmsg((char *)tmpfilnam[mlsdepth],
  15167.              CKMAXPATH+1,p,"ckXXXXXX",NULL,NULL);
  15168.     } else {
  15169.         printf("?Malloc failure: remote_files()\n");
  15170.         return(NULL);
  15171.     }
  15172.  
  15173. #ifdef NT
  15174.     {
  15175.         char * tmpfil = mktemp((char *)tmpfilnam[mlsdepth]);
  15176.         if ( tmpfil )
  15177.         ckstrncpy(tmpfilnam[mlsdepth],tmpfil,CKMAXPATH+1);
  15178.     }
  15179. #else /* NT */
  15180. #ifdef MKTEMP
  15181. #ifdef MKSTEMP
  15182.     x = mkstemp((char *)tmpfilnam[mlsdepth]);
  15183.     if (x > -1) close(x);        /* We just want the name. */
  15184. #else
  15185.         mktemp((char *)tmpfilnam[mlsdepth]);
  15186. #endif /* MKSTEMP */
  15187.         /* if no mktmpnam() the name will just be "ckXXXXXX"... */
  15188. #endif /* MKTEMP */
  15189. #endif /* NT */
  15190.  
  15191.     debug(F111,"ftp remote_files tmpfilnam[mlsdepth]",
  15192.           tmpfilnam[mlsdepth],mlsdepth);
  15193.  
  15194. #ifdef FTP_PROXY
  15195.         if (proxy_switch) {
  15196.             pswitch(!proxy);
  15197.         }
  15198. #endif /* FTP_PROXY */
  15199.  
  15200.         debug(F101,"ftp remote_files ftp_xla","",ftp_xla);
  15201.         debug(F101,"ftp remote_files ftp_csl","",ftp_csl);
  15202.         debug(F101,"ftp remote_files ftp_csr","",ftp_csr);
  15203.  
  15204. #ifndef NOCSETS
  15205.         xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
  15206.         if (xlate) {                    /* ON? */
  15207.             lcs = ftp_csl;              /* Local charset */
  15208.             if (lcs < 0) lcs = fcharset;
  15209.             if (lcs < 0) xlate = 0;
  15210.         }
  15211.         if (xlate) {                    /* Still ON? */
  15212.             rcs = ftp_csx;              /* Remote (Server) charset */
  15213.             if (rcs < 0) rcs = ftp_csr;
  15214.             if (rcs < 0) xlate = 0;
  15215.         }
  15216. #endif /* NOCSETS */
  15217.  
  15218.     forced = mgetforced;        /* MGET method forced? */
  15219.     if (!forced || !mgetmethod)    /* Not forced... */
  15220.       mgetmethod = (sfttab[0] && sfttab[SFT_MLST]) ? /* so pick one */
  15221.           SND_MLS :
  15222.           SND_NLS; 
  15223. /*                                           
  15224.   User's Command:                 Result:
  15225.     mget /nlst                     NLST (NULL)
  15226.     mget /nlst foo                 NLST foo
  15227.     mget /nlst *.txt               NLST *.txt 
  15228.     mget /nlst /match:*.txt        NLST (NULL)
  15229.     mget /nlst /match:*.txt  foo   NLST foo   
  15230.     mget /mlsd                     MLSD (NULL)
  15231.     mget /mlsd foo                 MLSD foo
  15232.     mget /mlsd *.txt               MLSD (NULL)
  15233.     mget /mlsd /match:*.txt        MLSD (NULL)
  15234.     mget /mlsd /match:*.txt  foo   MLSD foo
  15235. */
  15236.     x = -1;
  15237.     while (x < 0) {
  15238.         if (pattern) {        /* Don't simplify this! */
  15239.         whicharg = arg;
  15240.         } else if (mgetmethod == SND_MLS) {
  15241.         if (arg)
  15242.           whicharg = iswild((char *)arg) ? NULL : arg;
  15243.         else
  15244.           whicharg = NULL;
  15245.         } else {
  15246.         whicharg = arg;
  15247.         }
  15248.         debug(F110,"ftp remote_files mgetmethod",
  15249.           mgetmethod == SND_MLS ? "MLSD" : "NLST", 0);
  15250.         debug(F110,"ftp remote_files whicharg",whicharg,0);
  15251.  
  15252.         x = recvrequest((mgetmethod == SND_MLS) ? "MLSD" : "NLST",
  15253.                 (char *)tmpfilnam[mlsdepth],
  15254.                 (char *)whicharg,
  15255.                 "wb",
  15256.                 0,
  15257.                 0,
  15258.                 NULL,
  15259.                 xlate,
  15260.                 lcs,
  15261.                 rcs
  15262.                 );
  15263.         if (x < 0) {        /* Chosen method wasn't accepted */
  15264.         if (forced) {
  15265.             if (ftpcode > 500 && ftpcode < 505 && !quiet)
  15266.               printf("?%s: Not supported by server\n",
  15267.                  mgetmethod == SND_MLS ? "MLSD" : "NLST"
  15268.                  );
  15269.             rfrc = -2;        /* Fail */
  15270.             return(NULL);
  15271.         }
  15272.         /* Not forced - if MLSD failed, try NLST */
  15273.         if (mgetmethod == SND_MLS) {  /* Server lied about MLST */
  15274.             sfttab[SFT_MLST] = 0;     /* So disable it */
  15275.             mlstok = 0;              /* and */
  15276.             mgetmethod = SND_NLS;     /* try NLST */
  15277.             continue;
  15278.         }
  15279.         rfrc = -2;
  15280.         return(NULL);
  15281.         }
  15282.     }
  15283. #ifdef FTP_PROXY
  15284.         if (proxy_switch) {
  15285.             pswitch(!proxy);
  15286.         }
  15287. #endif /* FTP_PROXY */
  15288.         tmpfilptr[mlsdepth] = fopen((char *)tmpfilnam[mlsdepth], "r");
  15289. #ifndef OS2
  15290.     if (tmpfilptr[mlsdepth]) {
  15291.         if (!ftp_deb && !deblog)
  15292.           unlink(tmpfilnam[mlsdepth]);
  15293.     }
  15294. #endif /* OS2 */
  15295.       notemp:
  15296.         if (!tmpfilptr[mlsdepth]) {
  15297.             debug(F110,"ftp remote_files open fail",tmpfilnam[mlsdepth],0);
  15298.             if ((!dpyactive || ftp_deb))
  15299.               printf("?Can't find list of remote files, oops\n");
  15300.             rfrc = -9;
  15301.             return(NULL);
  15302.         }
  15303.     if (ftp_deb)
  15304.       printf("LISTFILE: %s\n",tmpfilnam[mlsdepth]);
  15305.     }
  15306.     buf[0] = NUL;
  15307.     buf[FTPNAMBUFLEN-1] = NUL;
  15308.     buf[FTPNAMBUFLEN-2] = NUL;
  15309.  
  15310.     /* We have to redo all this because the first time was only for */
  15311.     /* for getting the file list, now it's for getting each file */
  15312.  
  15313.     if (arg && mgetmethod == SND_MLS) {    /* MLSD */
  15314.     if (!pattern && iswild((char *)arg)) {
  15315.         pattern = arg;        /* Wild arg is really a pattern */
  15316.         if (pattern)
  15317.           if (!*pattern)
  15318.         pattern = NULL;
  15319.         arg = NULL;            /* and not an arg */
  15320.     }
  15321.     if (new_query) {        /* Initial query? */
  15322.         cdto = (char *)arg;        /* (nonwild) arg given? */
  15323.         if (cdto)
  15324.           if (!*cdto)
  15325.         cdto = NULL;
  15326.         if (cdto)            /* If so, then CD to it */
  15327.           doftpcwd(cdto,0);
  15328.     }
  15329.     }
  15330.     new_query = 0;
  15331.  
  15332.     if (fgets((char *)buf, FTPNAMBUFLEN, tmpfilptr[mlsdepth]) == NULL) {
  15333.         fclose(tmpfilptr[mlsdepth]);
  15334.         tmpfilptr[mlsdepth] = NULL;
  15335.  
  15336. #ifdef OS2
  15337.         if (!ftp_deb && !deblog)
  15338.           unlink(tmpfilnam[mlsdepth]);
  15339. #endif /* OS2 */
  15340.         if (ftp_deb && !deblog) {
  15341.             printf("(Temporary file %s NOT deleted)\n",
  15342.            (char *)tmpfilnam[mlsdepth]);
  15343.         }
  15344.     if (mlsdepth <= 0) {        /* EOF at depth 0 */
  15345.         rfrc = -3;            /* means we're done */
  15346.         return(NULL);
  15347.     }
  15348.     printf("POPPING(%d)...\n",mlsdepth-1); 
  15349.     if (tmpfilnam[mlsdepth]) free(tmpfilnam[mlsdepth]);
  15350.     mlsdepth--;
  15351.     doftpcdup();
  15352.     zchdir("..");            /* <-- Not portable */
  15353.     goto again;
  15354.     }
  15355.     if (buf[FTPNAMBUFLEN-1]) {
  15356.     printf("?BUFFER OVERFLOW -- FTP NLST or MLSD string longer than %d\n",
  15357.            FTPNAMBUFLEN
  15358.            );
  15359.     debug(F101,"remote_files buffer overrun","",FTPNAMBUFLEN);
  15360.     return(NULL);
  15361.     }
  15362.     /* debug(F110,"ftp remote_files buf 1",buf,0); */
  15363.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
  15364.       *cp = '\0';
  15365.     if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
  15366.       *cp = '\0';
  15367.     debug(F110,"ftp remote_files buf",buf,0);
  15368.     rfrc = 0;
  15369.  
  15370.     if (ftp_deb)
  15371.       printf("[%s]\n",(char *)buf);
  15372.  
  15373.     havesize = (CK_OFF_T)-1;        /* Initialize file facts... */
  15374.     havetype = 0;
  15375.     makestr(&havemdtm,NULL);
  15376.     p = (char *)buf;
  15377.  
  15378.     if (mgetmethod == SND_NLS) {    /* NLST... */
  15379.     if (pattern) {
  15380.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15381.           goto again;
  15382.     }
  15383.     } else {                /* MLSD... */
  15384.     p = parsefacts((char *)buf);
  15385.     switch (havetype) {
  15386.       case FTYP_FILE:        /* File: Get it if it matches */
  15387.         if (pattern) {
  15388.         if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
  15389.           goto again;
  15390.         }
  15391.         break;
  15392.       case FTYP_CDIR:        /* Current directory */
  15393.       case FTYP_PDIR:        /* Parent directory */
  15394.         goto again;            /* Skip */
  15395.       case FTYP_DIR:        /* (Sub)Directory */
  15396.         if (!recursive)        /* If not /RECURSIVE */
  15397.           goto again;        /* Skip */
  15398.         if (mlsdepth < MLSDEPTH) {
  15399.         char * p2 = NULL;
  15400.         mlsdepth++;
  15401.         printf("RECURSING [%s](%d)...\n",p,mlsdepth); 
  15402.         if (doftpcwd(p,0) > 0) {
  15403.             int x;
  15404.             if (!ckstrchr(p,'/')) {
  15405.             /* zmkdir() needs dirsep */
  15406.             if ((p2 = (char *)malloc((int)strlen(p) + 2))) {
  15407.                 strcpy(p2,p);    /* SAFE */
  15408.                 strcat(p2,"/");    /* SAFE */
  15409.                 p = p2;
  15410.             }
  15411.             }
  15412. #ifdef NOMKDIR
  15413.             x = -1;
  15414. #else
  15415.             x = zmkdir(p);
  15416. #endif /* NOMKDIR */
  15417.             if (x > -1) {
  15418.             zchdir(p);
  15419.             p = (char *)remote_files(1,arg,pattern,0);
  15420.             if (p2) free(p2);
  15421.             } else {
  15422.             printf("?mkdir failed: [%s] Depth=%d\n",
  15423.                    p,
  15424.                    mlsdepth
  15425.                    );
  15426.             mlsreset();
  15427.             if (p2) free(p2);
  15428.             return(NULL);
  15429.             }
  15430.         } else {
  15431.             printf("?CWD failed: [%s] Depth=%d\n",p,mlsdepth);
  15432.             mlsreset();
  15433.             return(NULL);
  15434.         }
  15435.         } else {
  15436.         printf("MAX DIRECTORY STACK DEPTH EXCEEDED: %d\n",
  15437.                mlsdepth
  15438.                );
  15439.         mlsreset();
  15440.         return(NULL);
  15441.         }
  15442.     }
  15443.     }
  15444.  
  15445. #ifdef DEBUG
  15446.     if (deblog) {
  15447.     debug(F101,"remote_files havesize","",havesize);
  15448.     debug(F101,"remote_files havetype","",havetype);
  15449.     debug(F110,"remote_files havemdtm",havemdtm,0);    
  15450.     debug(F110,"remote_files name",p,0);    
  15451.     }
  15452. #endif /* DEBUG */
  15453.     return((CHAR *)p);
  15454. }
  15455.  
  15456. /* N O T  P O R T A B L E !!! */
  15457.  
  15458. #if (SIZEOF_SHORT == 4)
  15459. typedef unsigned short ftp_uint32;
  15460. typedef short ftp_int32;
  15461. #else
  15462. #if (SIZEOF_INT == 4)
  15463. typedef unsigned int ftp_uint32;
  15464. typedef int ftp_int32;
  15465. #else
  15466. #if (SIZEOF_LONG == 4)
  15467. typedef ULONG ftp_uint32;
  15468. typedef long ftp_int32;
  15469. #endif
  15470. #endif
  15471. #endif
  15472.  
  15473. /* Perhaps use these in general, certainly use them for GSSAPI */
  15474.  
  15475. #ifndef looping_write
  15476. #define ftp_int32 int
  15477. #define ftp_uint32 unsigned int
  15478. static int
  15479. looping_write(fd, buf, len)
  15480.     int fd;
  15481.     register CONST char *buf;
  15482.     int len;
  15483. {
  15484.     int cc;
  15485.     register int wrlen = len;
  15486.     do {
  15487.         cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
  15488.         if (cc < 0) {
  15489.             if (errno == EINTR)
  15490.               continue;
  15491.             return(cc);
  15492.         } else {
  15493.             buf += cc;
  15494.             wrlen -= cc;
  15495.         }
  15496.     } while (wrlen > 0);
  15497.     return(len);
  15498. }
  15499. #endif
  15500. #ifndef looping_read
  15501. static int
  15502. looping_read(fd, buf, len)
  15503.     int fd;
  15504.     register char *buf;
  15505.     register int len;
  15506. {
  15507.     int cc, len2 = 0;
  15508.  
  15509.     do {
  15510.         cc = recv(fd, (char *)buf, len,0);
  15511.         if (cc < 0) {
  15512.             if (errno == EINTR)
  15513.               continue;
  15514.             return(cc);                 /* errno is already set */
  15515.         } else if (cc == 0) {
  15516.             return(len2);
  15517.         } else {
  15518.             buf += cc;
  15519.             len2 += cc;
  15520.             len -= cc;
  15521.         }
  15522.     } while (len > 0);
  15523.     return(len2);
  15524. }
  15525. #endif /* looping_read */
  15526.  
  15527. #define ERR -2
  15528.  
  15529. #ifdef COMMENT
  15530. static
  15531. secure_putbyte(fd, c) int fd; CHAR c; {
  15532.     int ret;
  15533.  
  15534.     ucbuf[nout++] = c;
  15535.     if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
  15536.         nout = 0;
  15537.         if (!ftpissecure())
  15538.           ret = send(fd, (SENDARG2TYPE)ucbuf,
  15539.                      (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
  15540.         else
  15541.           ret = secure_putbuf(fd,
  15542.                               ucbuf,
  15543.                               (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
  15544.                               );
  15545.         return(ret?ret:c);
  15546.     }
  15547.     return(c);
  15548. }
  15549. #endif /* COMMENT */
  15550.  
  15551. /* returns:
  15552.  *       0  on success
  15553.  *      -1  on error (errno set)
  15554.  *      -2  on security error
  15555.  */
  15556. static int
  15557. secure_flush(fd) int fd; {
  15558.     int rc = 0;
  15559.     int len = 0;
  15560.  
  15561.     if (nout > 0) {
  15562.         len = nout;
  15563.         if (!ftpissecure()) {
  15564.             rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
  15565.             nout = 0;
  15566.             goto xflush;
  15567.         } else {
  15568.             rc = secure_putbuf(fd, ucbuf, nout);
  15569.             if (rc)
  15570.               goto xflush;
  15571.         }
  15572.     }
  15573.     rc = (!ftpissecure()) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  15574.  
  15575.   xflush:
  15576.     if (rc > -1 && len > 0 && fdispla != XYFD_B) {
  15577.     spackets++;
  15578.         spktl = len;
  15579.         ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
  15580.     }
  15581.     return(rc);
  15582. }
  15583.  
  15584. #ifdef COMMENT                          /* (not used) */
  15585. /* returns:
  15586.  *      c>=0  on success
  15587.  *      -1    on error
  15588.  *      -2    on security error
  15589.  */
  15590. static int
  15591. #ifdef CK_ANSIC
  15592. secure_putc(char c, int fd)
  15593. #else
  15594. secure_putc(c, fd) char c; int fd;
  15595. #endif /* CK_ANSIC */
  15596. /* secure_putc */ {
  15597.     return(secure_putbyte(fd, (CHAR) c));
  15598. }
  15599. #endif /* COMMENT */
  15600.  
  15601. /* returns:
  15602.  *      nbyte on success
  15603.  *      -1  on error (errno set)
  15604.  *      -2  on security error
  15605.  */
  15606. static int
  15607. #ifdef CK_ANSIC
  15608. secure_write(int fd, CHAR * buf, unsigned int nbyte)
  15609. #else
  15610. secure_write(fd, buf, nbyte)
  15611.     int fd;
  15612.     CHAR * buf;
  15613.     unsigned int nbyte;
  15614. #endif /* CK_ANSIC */
  15615. {
  15616.     int ret;
  15617.  
  15618.     if (!ftpissecure()) {
  15619.         if (nout > 0) {
  15620.             if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
  15621.               return(ret);
  15622.             nout = 0;
  15623.         }
  15624.         return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
  15625.     } else {
  15626.         int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
  15627.         int bsent = 0;
  15628.  
  15629.         while (bsent < nbyte) {
  15630.             int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
  15631.                         (ucbuflen - nout) : (nbyte - bsent));
  15632. #ifdef DEBUG
  15633.         if (deblog) {
  15634.         debug(F101,"secure_write ucbuflen","",ucbuflen);
  15635.         debug(F101,"secure_write ucbufsiz","",ucbufsiz);
  15636.         debug(F101,"secure_write bsent","",bsent);
  15637.         debug(F101,"secure_write b2cp","",b2cp);
  15638.         }
  15639. #endif /* DEBUG */
  15640.             memcpy(&ucbuf[nout],&buf[bsent],b2cp);
  15641.             nout += b2cp;
  15642.             bsent += b2cp;
  15643.  
  15644.             if (nout == ucbuflen) {
  15645.                 nout = 0;
  15646.                 ret = secure_putbuf(fd, ucbuf, ucbuflen);
  15647.                 if (ret < 0)
  15648.                   return(ret);
  15649.             }
  15650.         }
  15651.         return(bsent);
  15652.     }
  15653. }
  15654.  
  15655. /* returns:
  15656.  *       0  on success
  15657.  *      -1  on error (errno set)
  15658.  *      -2  on security error
  15659.  */
  15660. static int
  15661. #ifdef CK_ANSIC
  15662. secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
  15663. #else
  15664. secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
  15665. #endif /* CK_ANSIC */
  15666. {
  15667.     static char *outbuf = NULL;         /* output ciphertext */
  15668. #ifdef FTP_SECURITY
  15669.     static unsigned int bufsize = 0;    /* size of outbuf */
  15670. #endif /* FTP_SECURITY */
  15671.     ftp_int32 length   = 0;
  15672.     ftp_uint32 net_len = 0;
  15673.  
  15674.     /* Other auth types go here ... */
  15675. #ifdef CK_SSL
  15676.     if (ssl_ftp_data_active_flag) {
  15677.         int count, error;
  15678.  
  15679.         /* there is no need to send an empty buffer when using SSL/TLS */
  15680.         if ( nbyte == 0 )
  15681.       return(0);
  15682.  
  15683.         count = SSL_write(ssl_ftp_data_con, buf, nbyte);
  15684.         error = SSL_get_error(ssl_ftp_data_con,count);
  15685.         switch (error) {
  15686.           case SSL_ERROR_NONE:
  15687.             return(0);
  15688.           case SSL_ERROR_WANT_WRITE:
  15689.           case SSL_ERROR_WANT_READ:
  15690.           case SSL_ERROR_SYSCALL:
  15691. #ifdef NT
  15692.             {
  15693.                 int gle = GetLastError();
  15694.         if (gle == 0)
  15695.           return(0);
  15696.         debug(F111,"secure_putbuf","SSL_ERROR_SYSCALL",gle);
  15697.             }
  15698. #endif /* NT */
  15699.           case SSL_ERROR_WANT_X509_LOOKUP:
  15700.           case SSL_ERROR_SSL:
  15701.           case SSL_ERROR_ZERO_RETURN:
  15702.           default:
  15703.             SSL_shutdown(ssl_ftp_data_con);
  15704.             SSL_free(ssl_ftp_data_con);
  15705.             ssl_ftp_data_active_flag = 0;
  15706.             ssl_ftp_data_con = NULL;
  15707. #ifdef TCPIPLIB
  15708.             socket_close(data);
  15709. #else /* TCPIPLIB */
  15710. #ifdef USE_SHUTDOWN
  15711.             shutdown(data, 1+1);
  15712. #endif /* USE_SHUTDOWN */
  15713.             close(data);
  15714. #endif /* TCPIPLIB */
  15715.             data = -1;
  15716.             globaldin = data;
  15717.             return(-1);
  15718.         }
  15719.         return(-1);
  15720.     }
  15721. #endif /* CK_SSL */
  15722.  
  15723. #ifdef FTP_SRP
  15724.     if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
  15725.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15726.             if (outbuf?
  15727.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15728.                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15729.                 bufsize = nbyte + FUDGE_FACTOR;
  15730.             } else {
  15731.                 bufsize = 0;
  15732.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15733.                 return(ERR);
  15734.             }
  15735.         }
  15736.         if ((length =
  15737.              srp_encode(ftp_dpl == FPL_PRV,
  15738.                         (CHAR *) buf,
  15739.                         (CHAR *) outbuf,
  15740.                         nbyte
  15741.                         )
  15742.              ) < 0) {
  15743.             secure_error ("srp_encode failed");
  15744.             return ERR;
  15745.         }
  15746.     }
  15747. #endif /* FTP_SRP */
  15748. #ifdef FTP_KRB4
  15749.     if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
  15750.         struct sockaddr_in myaddr, hisaddr;
  15751.         GSOCKNAME_T len;
  15752.         len = sizeof(myaddr);
  15753.         if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15754.             secure_error("secure_putbuf: getsockname failed");
  15755.             return(ERR);
  15756.         }
  15757.         len = sizeof(hisaddr);
  15758.         if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15759.             secure_error("secure_putbuf: getpeername failed");
  15760.             return(ERR);
  15761.         }
  15762.         if (bufsize < nbyte + FUDGE_FACTOR) {
  15763.             if (outbuf ?
  15764.                 (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
  15765.                  (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
  15766.                 bufsize = nbyte + FUDGE_FACTOR;
  15767.             } else {
  15768.                 bufsize = 0;
  15769.                 secure_error("%s (in malloc of PROT buffer)", ck_errstr());
  15770.                 return(ERR);
  15771.             }
  15772.         }
  15773.         if (ftp_dpl == FPL_PRV) {
  15774.             length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
  15775.                                  ftp_sched,
  15776. #ifdef KRB524
  15777.                                  ftp_cred.session,
  15778. #else /* KRB524 */
  15779.                                  &ftp_cred.session,
  15780. #endif /* KRB524 */
  15781.                                  &myaddr,
  15782.                                  &hisaddr
  15783.                                  );
  15784.         } else {
  15785.             length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
  15786. #ifdef KRB524
  15787.                                  ftp_cred.session,
  15788. #else /* KRB524 */
  15789.                                  &ftp_cred.session,
  15790. #endif /* KRB524 */
  15791.                                  &myaddr,
  15792.                                  &hisaddr
  15793.                                  );
  15794.         }
  15795.         if (length == -1) {
  15796.             secure_error("krb_mk_%s failed for KERBEROS_V4",
  15797.                          ftp_dpl == FPL_PRV ? "priv" : "safe");
  15798.             return(ERR);
  15799.         }
  15800.     }
  15801. #endif /* FTP_KRB4 */
  15802. #ifdef FTP_GSSAPI
  15803.     if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
  15804.         gss_buffer_desc in_buf, out_buf;
  15805.         OM_uint32 maj_stat, min_stat;
  15806.         int conf_state;
  15807.  
  15808.         in_buf.value = buf;
  15809.         in_buf.length = nbyte;
  15810.         maj_stat = gss_seal(&min_stat, gcontext,
  15811.                             (ftp_dpl == FPL_PRV), /* confidential */
  15812.                             GSS_C_QOP_DEFAULT,
  15813.                             &in_buf,
  15814.                             &conf_state,
  15815.                             &out_buf
  15816.                             );
  15817.         if (maj_stat != GSS_S_COMPLETE) {
  15818.             /* generally need to deal */
  15819.             /* ie. should loop, but for now just fail */
  15820.             user_gss_error(maj_stat, min_stat,
  15821.                            ftp_dpl == FPL_PRV?
  15822.                            "GSSAPI seal failed":
  15823.                            "GSSAPI sign failed");
  15824.             return(ERR);
  15825.         }
  15826.         if (bufsize < out_buf.length) {
  15827.             if (outbuf ?
  15828.                 (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
  15829.                 (outbuf = malloc((unsigned) out_buf.length))) {
  15830.                 bufsize = out_buf.length;
  15831.             } else {
  15832.                 bufsize = 0;
  15833.                 secure_error("%s (in malloc of PROT buffer)",
  15834.                              ck_errstr());
  15835.                 return(ERR);
  15836.             }
  15837.         }
  15838.         memcpy(outbuf, out_buf.value, length=out_buf.length);
  15839.         gss_release_buffer(&min_stat, &out_buf);
  15840.     }
  15841. #endif /* FTP_GSSAPI */
  15842.     net_len = htonl((ULONG) length);
  15843.     if (looping_write(fd, (char *)&net_len, 4) == -1)
  15844.       return(-1);
  15845.     if (looping_write(fd, outbuf, length) != length)
  15846.       return(-1);
  15847.     return(0);
  15848. }
  15849.  
  15850. /* fc = 0 means to get a byte; nonzero means to initialize buffer pointers */
  15851.  
  15852. static int
  15853. secure_getbyte(fd,fc) int fd,fc; {
  15854.     /* number of chars in ucbuf, pointer into ucbuf */
  15855.     static unsigned int nin = 0, bufp = 0;
  15856.     int kerror;
  15857.     ftp_uint32 length;
  15858.  
  15859.     if (fc) {
  15860.     nin = bufp = 0;
  15861.     ucbuf[0] = NUL;
  15862.     return(0);
  15863.     }
  15864.     if (nin == 0) {
  15865.         if (iscanceled())
  15866.           return(-9);
  15867. #ifdef CK_SSL
  15868.         if (ssl_ftp_data_active_flag) {
  15869.             int count, error;
  15870.             count = SSL_read(ssl_ftp_data_con, ucbuf, ucbufsiz);
  15871.             error = SSL_get_error(ssl_ftp_data_con,count);
  15872. #ifdef DEBUG
  15873.         if (error != SSL_ERROR_NONE)
  15874.           debug(F101,"ftp secure_getbyte error","",error);
  15875.         if (count == 0)
  15876.           debug(F101,"ftp secure_getbyte count","",count);
  15877. #endif    /* DEBUG */
  15878.             switch (error) {
  15879.               case SSL_ERROR_NONE:
  15880.         if (count > 0) {
  15881.             nin = bufp = count;
  15882.             rpackets++;
  15883.             pktnum++;
  15884.             if (fdispla != XYFD_B) {
  15885.             rpktl = count;
  15886.             ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  15887.             }
  15888.             break;
  15889.         }
  15890.               case SSL_ERROR_WANT_WRITE:
  15891.               case SSL_ERROR_WANT_READ:
  15892.               case SSL_ERROR_SYSCALL:
  15893. #ifdef NT
  15894.                 {
  15895.                     int gle = GetLastError();
  15896.                 }
  15897. #endif /* NT */
  15898.               case SSL_ERROR_WANT_X509_LOOKUP:
  15899.               case SSL_ERROR_SSL:
  15900.               case SSL_ERROR_ZERO_RETURN:
  15901.               default:
  15902.                 nin = bufp = count = 0;
  15903.                 SSL_shutdown(ssl_ftp_data_con);
  15904.                 SSL_free(ssl_ftp_data_con);
  15905.                 ssl_ftp_data_active_flag = 0;
  15906.                 ssl_ftp_data_con = NULL;
  15907. #ifdef TCPIPLIB
  15908.                 socket_close(data);
  15909. #else /* TCPIPLIB */
  15910. #ifdef USE_SHUTDOWN
  15911.                 shutdown(data, 1+1);
  15912. #endif /* USE_SHUTDOWN */
  15913.                 close(data);
  15914. #endif /* TCPIPLIB */
  15915.                 data = -1;
  15916.                 globaldin = data;
  15917.                 break;
  15918.             }
  15919.         } else
  15920. #endif /* CK_SSL */
  15921.           {
  15922.               kerror = looping_read(fd, (char *)&length, sizeof(length));
  15923.               if (kerror != sizeof(length)) {
  15924.                   secure_error("Couldn't read PROT buffer length: %d/%s",
  15925.                                kerror,
  15926.                                kerror == -1 ? ck_errstr()
  15927.                                : "premature EOF"
  15928.                                );
  15929.                   return(ERR);
  15930.               }
  15931.               debug(F101,"secure_getbyte length","",length);
  15932.               debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
  15933.  
  15934.               length = (ULONG) ntohl(length);
  15935.               if (length > maxbuf) {
  15936.                   secure_error("Length (%d) of PROT buffer > PBSZ=%u",
  15937.                                length,
  15938.                                maxbuf
  15939.                                );
  15940.                   return(ERR);
  15941.               }
  15942.               if ((kerror = looping_read(fd, ucbuf, length)) != length) {
  15943.                   secure_error("Couldn't read %u byte PROT buffer: %s",
  15944.                                length,
  15945.                                kerror == -1 ? ck_errstr() : "premature EOF"
  15946.                                );
  15947.                   return(ERR);
  15948.               }
  15949.  
  15950.               /* Other auth types go here ... */
  15951. #ifdef FTP_SRP
  15952.               if (strcmp(auth_type, "SRP") == 0) {
  15953.                   if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
  15954.                                                 (CHAR *) ucbuf,
  15955.                                                 ucbuf,
  15956.                                                 length
  15957.                                                 )
  15958.                        ) == -1) {
  15959.                       secure_error ("srp_encode failed" );
  15960.                       return ERR;
  15961.                   }
  15962.               }
  15963. #endif /* FTP_SRP */
  15964. #ifdef FTP_KRB4
  15965.               if (strcmp(auth_type, "KERBEROS_V4") == 0) {
  15966.                   struct sockaddr_in myaddr, hisaddr;
  15967.                   GSOCKNAME_T len;
  15968.                   len = sizeof(myaddr);
  15969.                   if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
  15970.                       secure_error("secure_putbuf: getsockname failed");
  15971.                       return(ERR);
  15972.                   }
  15973.                   len = sizeof(hisaddr);
  15974.                   if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
  15975.                       secure_error("secure_putbuf: getpeername failed");
  15976.                       return(ERR);
  15977.                   }
  15978.                   if (ftp_dpl) {
  15979.                       kerror = krb_rd_priv(ucbuf, length, ftp_sched,
  15980. #ifdef KRB524
  15981.                                            ftp_cred.session,
  15982. #else /* KRB524 */
  15983.                                            &ftp_cred.session,
  15984. #endif /* KRB524 */
  15985.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15986.                   } else {
  15987.                       kerror = krb_rd_safe(ucbuf, length,
  15988. #ifdef KRB524
  15989.                                            ftp_cred.session,
  15990. #else /* KRB524 */
  15991.                                            &ftp_cred.session,
  15992. #endif /* KRB524 */
  15993.                                            &hisaddr, &myaddr, &ftp_msg_data);
  15994.                   }
  15995.                   if (kerror) {
  15996.                       secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
  15997.                                    ftp_dpl == FPL_PRV ? "priv" : "safe",
  15998.                                    krb_get_err_text(kerror));
  15999.                       return(ERR);
  16000.                   }
  16001.                   memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
  16002.                   nin = bufp = ftp_msg_data.app_length;
  16003.               }
  16004. #endif /* FTP_KRB4 */
  16005. #ifdef FTP_GSSAPI
  16006.               if (strcmp(auth_type, "GSSAPI") == 0) {
  16007.                   gss_buffer_desc xmit_buf, msg_buf;
  16008.                   OM_uint32 maj_stat, min_stat;
  16009.                   int conf_state;
  16010.  
  16011.                   xmit_buf.value = ucbuf;
  16012.                   xmit_buf.length = length;
  16013.                   conf_state = (ftp_dpl == FPL_PRV);
  16014.                   /* decrypt/verify the message */
  16015.                   maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
  16016.                                         &msg_buf, &conf_state, NULL);
  16017.                   if (maj_stat != GSS_S_COMPLETE) {
  16018.                       user_gss_error(maj_stat, min_stat,
  16019.                                      (ftp_dpl == FPL_PRV)?
  16020.                                      "failed unsealing ENC message":
  16021.                                      "failed unsealing MIC message");
  16022.                       return ERR;
  16023.                   }
  16024.                   memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
  16025.                   gss_release_buffer(&min_stat, &msg_buf);
  16026.               }
  16027. #endif /* FTP_GSSAPI */
  16028.               /* Other auth types go here ... */
  16029.  
  16030.               /* Update file transfer display */
  16031.               rpackets++;
  16032.               pktnum++;
  16033.               if (fdispla != XYFD_B) {
  16034.                   rpktl = nin;
  16035.                   ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  16036.               }
  16037.           }
  16038.     }
  16039.     if (nin == 0)
  16040.       return(EOF);
  16041.     else
  16042.       return(ucbuf[bufp - nin--]);
  16043. }
  16044.  
  16045. /* secure_getc(fd,fc)
  16046.  * Call with:
  16047.  *   fd = file descriptor for connection.
  16048.  *   fc = 0 to get a character, fc != 0 to initialize buffer pointers.
  16049.  * Returns:
  16050.  *   c>=0 on success (character value)
  16051.  *   -1   on EOF
  16052.  *   -2   on security error
  16053.  */
  16054. static int
  16055. secure_getc(fd,fc) int fd,fc; {        /* file descriptor, function code */
  16056.     if (!ftpissecure()) {
  16057.         static unsigned int nin = 0, bufp = 0;
  16058.     if (fc) {
  16059.         nin = bufp = 0;
  16060.         ucbuf[0] = NUL;
  16061.         return(0);
  16062.     }
  16063.         if (nin == 0) {
  16064.             if (iscanceled())
  16065.               return(-9);
  16066.             nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
  16067.             if (nin <= 0) {
  16068.                 debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
  16069.                 debug(F101,"secure_getc returns EOF","",EOF);
  16070.                 nin = bufp = 0;
  16071.                 return(EOF);
  16072.             }
  16073.             debug(F101,"ftp secure_getc recv","",nin);
  16074.             hexdump("ftp secure_getc recv",ucbuf,16);
  16075.             rpackets++;
  16076.             pktnum++;
  16077.             if (fdispla != XYFD_B) {
  16078.                 rpktl = nin;
  16079.                 ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
  16080.             }
  16081.         }
  16082.         return(ucbuf[bufp - nin--]);
  16083.     } else
  16084.       return(secure_getbyte(fd,fc));
  16085. }
  16086.  
  16087. /* returns:
  16088.  *     n>0  on success (n == # of bytes read)
  16089.  *       0  on EOF
  16090.  *      -1  on error (errno set), only for FPL_CLR
  16091.  *      -2  on security error
  16092.  */
  16093. static int
  16094. secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
  16095.     static int c = 0;
  16096.     int i;
  16097.  
  16098.     debug(F101,"secure_read bytes requested","",nbyte);
  16099.     if (c == EOF)
  16100.       return(c = 0);
  16101.     for (i = 0; nbyte > 0; nbyte--) {
  16102.         c = secure_getc(fd,0);
  16103.         switch (c) {
  16104.           case -9:                      /* Canceled from keyboard */
  16105.             debug(F101,"ftp secure_read interrupted","",c);
  16106.             return(0);
  16107.           case ERR:
  16108.             debug(F101,"ftp secure_read error","",c);
  16109.             return(c);
  16110.           case EOF:
  16111.             debug(F101,"ftp secure_read EOF","",c);
  16112.             if (!i)
  16113.               c = 0;
  16114.             return(i);
  16115.           default:
  16116.             buf[i++] = c;
  16117.         }
  16118.     }
  16119.     return(i);
  16120. }
  16121.  
  16122. #ifdef USE_RUSERPASS
  16123. /* BEGIN_RUSERPASS
  16124.  *
  16125.  * Copyright (c) 1985 Regents of the University of California.
  16126.  * All rights reserved.
  16127.  *
  16128.  * Redistribution and use in source and binary forms, with or without
  16129.  * modification, are permitted provided that the following conditions
  16130.  * are met:
  16131.  * 1. Redistributions of source code must retain the above copyright
  16132.  *    notice, this list of conditions and the following disclaimer.
  16133.  * 2. Redistributions in binary form must reproduce the above copyright
  16134.  *    notice, this list of conditions and the following disclaimer in the
  16135.  *    documentation and/or other materials provided with the distribution.
  16136.  * 3. All advertising materials mentioning features or use of this software
  16137.  *    must display the following acknowledgement:
  16138.  *      This product includes software developed by the University of
  16139.  *      California, Berkeley and its contributors.
  16140.  * 4. Neither the name of the University nor the names of its contributors
  16141.  *    may be used to endorse or promote products derived from this software
  16142.  *    without specific prior written permission.
  16143.  *
  16144.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16145.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16146.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16147.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  16148.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16149.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  16150.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16151.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16152.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16153.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16154.  * SUCH DAMAGE.
  16155.  */
  16156.  
  16157. #ifndef lint
  16158. static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
  16159. #endif /* not lint */
  16160.  
  16161. #ifndef MAXHOSTNAMELEN
  16162. #define MAXHOSTNAMELEN 64
  16163. #endif
  16164.  
  16165. char * renvlook();
  16166. static FILE * cfile;
  16167.  
  16168. #define DEFAULT 1
  16169. #define LOGIN   2
  16170. #define PASSWD  3
  16171. #define ACCOUNT 4
  16172. #define MACDEF  5
  16173. #define ID      10
  16174. #define MACH    11
  16175.  
  16176. static char tokval[100];
  16177.  
  16178. static struct toktab {
  16179.     char *tokstr;
  16180.     int tval;
  16181. } toktab[]= {
  16182.     "default",  DEFAULT,
  16183.     "login",    LOGIN,
  16184.     "password", PASSWD,
  16185.     "passwd",   PASSWD,
  16186.     "account",  ACCOUNT,
  16187.     "machine",  MACH,
  16188.     "macdef",   MACDEF,
  16189.     0,          0
  16190. };
  16191.  
  16192. static int
  16193. token() {
  16194.     char *cp;
  16195.     int c;
  16196.     struct toktab *t;
  16197.  
  16198.     if (feof(cfile))
  16199.       return(0);
  16200.     while ((c = getc(cfile)) != EOF &&
  16201.            (c == '\n' || c == '\t' || c == ' ' || c == ','))
  16202.       continue;
  16203.     if (c == EOF)
  16204.       return(0);
  16205.     cp = tokval;
  16206.     if (c == '"') {
  16207.         while ((c = getc(cfile)) != EOF && c != '"') {
  16208.             if (c == '\\')
  16209.               c = getc(cfile);
  16210.             *cp++ = c;
  16211.         }
  16212.     } else {
  16213.         *cp++ = c;
  16214.         while ((c = getc(cfile)) != EOF
  16215.                && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  16216.             if (c == '\\')
  16217.               c = getc(cfile);
  16218.             *cp++ = c;
  16219.         }
  16220.     }
  16221.     *cp = 0;
  16222.     if (tokval[0] == 0)
  16223.       return(0);
  16224.     for (t = toktab; t->tokstr; t++)
  16225.       if (!strcmp(t->tokstr, tokval))
  16226.         return(t->tval);
  16227.     return(ID);
  16228. }
  16229.  
  16230. ruserpass(host, aname, apass, aacct)
  16231.     char *host, **aname, **apass, **aacct;
  16232. {
  16233.     char *hdir, buf[FTP_BUFSIZ], *tmp;
  16234.     char myname[MAXHOSTNAMELEN], *mydomain;
  16235.     int t, i, c, usedefault = 0;
  16236. #ifdef NT
  16237.     struct _stat stb;
  16238. #else /* NT */
  16239.     struct stat stb;
  16240. #endif /* NT */
  16241.  
  16242.     hdir = getenv("HOME");
  16243.     if (hdir == NULL)
  16244.         hdir = ".";
  16245.     ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
  16246.     cfile = fopen(buf, "r");
  16247.     if (cfile == NULL) {
  16248.         if (errno != ENOENT)
  16249.           perror(buf);
  16250.         return(0);
  16251.     }
  16252.     if (gethostname(myname, MAXHOSTNAMELEN) < 0)
  16253.       myname[0] = '\0';
  16254.     if ((mydomain = ckstrchr(myname, '.')) == NULL)
  16255.       mydomain = "";
  16256.  
  16257.   next:
  16258.     while ((t = token())) switch(t) {
  16259.  
  16260.       case DEFAULT:
  16261.         usedefault = 1;
  16262.         /* FALL THROUGH */
  16263.  
  16264.       case MACH:
  16265.         if (!usedefault) {
  16266.             if (token() != ID)
  16267.               continue;
  16268.             /*
  16269.              * Allow match either for user's input host name
  16270.              * or official hostname.  Also allow match of
  16271.              * incompletely-specified host in local domain.
  16272.              */
  16273.             if (ckstrcmp(host, tokval,-1,1) == 0)
  16274.               goto match;
  16275.             if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
  16276.               goto match;
  16277.             if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
  16278.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16279.                 ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
  16280.                 tokval[tmp - ftp_host] == '\0')
  16281.               goto match;
  16282.             if ((tmp = ckstrchr(host, '.')) != NULL &&
  16283.                 ckstrcmp(tmp, mydomain,-1,1) == 0 &&
  16284.                 ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
  16285.                 tokval[tmp - host] == '\0')
  16286.               goto match;
  16287.             continue;
  16288.         }
  16289.  
  16290.       match:
  16291.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  16292.  
  16293.           case LOGIN:
  16294.             if (token())
  16295.               if (*aname == 0) {
  16296.                   *aname = malloc((unsigned) strlen(tokval) + 1);
  16297.                   strcpy(*aname, tokval);      /* safe */
  16298.               } else {
  16299.                   if (strcmp(*aname, tokval))
  16300.                     goto next;
  16301.               }
  16302.             break;
  16303.           case PASSWD:
  16304.             if (strcmp(*aname, "anonymous") &&
  16305.                 fstat(fileno(cfile), &stb) >= 0 &&
  16306.                 (stb.st_mode & 077) != 0) {
  16307.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16308.                 fprintf(stderr, "Remove password or correct mode.\n");
  16309.                 goto bad;
  16310.             }
  16311.             if (token() && *apass == 0) {
  16312.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  16313.                 strcpy(*apass, tokval);          /* safe */
  16314.             }
  16315.             break;
  16316.           case ACCOUNT:
  16317.             if (fstat(fileno(cfile), &stb) >= 0
  16318.                 && (stb.st_mode & 077) != 0) {
  16319.                 fprintf(stderr, "Error - .netrc file not correct mode.\n");
  16320.                 fprintf(stderr, "Remove account or correct mode.\n");
  16321.                 goto bad;
  16322.             }
  16323.             if (token() && *aacct == 0) {
  16324.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  16325.                 strcpy(*aacct, tokval);          /* safe */
  16326.             }
  16327.             break;
  16328.  
  16329.           default:
  16330.             fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  16331.             break;
  16332.         }
  16333.         goto done;
  16334.     }
  16335.  
  16336.   done:
  16337.     fclose(cfile);
  16338.     return(0);
  16339.  
  16340.   bad:
  16341.     fclose(cfile);
  16342.     return(-1);
  16343. }
  16344. #endif /* USE_RUSERPASS */
  16345.  
  16346. static char *radixN =
  16347.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  16348.  
  16349. static char pad = '=';
  16350.  
  16351. static int
  16352. radix_encode(inbuf, outbuf, inlen, outlen, decode)
  16353.     CHAR inbuf[], outbuf[];
  16354.     int inlen, *outlen, decode;
  16355. {
  16356.     int i, j, D = 0;
  16357.     char *p;
  16358.     CHAR c = NUL;
  16359.  
  16360.     if (decode) {
  16361.         for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
  16362.             if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
  16363.               return(1);
  16364.             D = p - radixN;
  16365.             switch (i&3) {
  16366.               case 0:
  16367.                 outbuf[j] = D<<2;
  16368.                 break;
  16369.               case 1:
  16370.                 outbuf[j++] |= D>>4;
  16371.                 outbuf[j] = (D&15)<<4;
  16372.                 break;
  16373.               case 2:
  16374.                 outbuf[j++] |= D>>2;
  16375.                 outbuf[j] = (D&3)<<6;
  16376.                 break;
  16377.               case 3:
  16378.                 outbuf[j++] |= D;
  16379.             }
  16380.             if (j == *outlen)
  16381.               return(4);
  16382.         }
  16383.         switch (i&3) {
  16384.           case 1: return(3);
  16385.           case 2: if (D&15) return(3);
  16386.             if (strcmp((char *)&inbuf[i], "==")) return(2);
  16387.             break;
  16388.           case 3: if (D&3) return(3);
  16389.             if (strcmp((char *)&inbuf[i], "="))  return(2);
  16390.         }
  16391.         *outlen = j;
  16392.     } else {
  16393.         for (i = 0, j = 0; i < inlen; i++) {
  16394.             switch (i%3) {
  16395.               case 0:
  16396.                 outbuf[j++] = radixN[inbuf[i]>>2];
  16397.                 c = (inbuf[i]&3)<<4;
  16398.                 break;
  16399.               case 1:
  16400.                 outbuf[j++] = radixN[c|inbuf[i]>>4];
  16401.                 c = (inbuf[i]&15)<<2;
  16402.                 break;
  16403.               case 2:
  16404.                 outbuf[j++] = radixN[c|inbuf[i]>>6];
  16405.                 outbuf[j++] = radixN[inbuf[i]&63];
  16406.                 c = 0;
  16407.             }
  16408.             if (j == *outlen)
  16409.               return(4);
  16410.         }
  16411.         if (i%3) outbuf[j++] = radixN[c];
  16412.         switch (i%3) {
  16413.           case 1: outbuf[j++] = pad;
  16414.           case 2: outbuf[j++] = pad;
  16415.         }
  16416.         outbuf[*outlen = j] = '\0';
  16417.     }
  16418.     return(0);
  16419. }
  16420.  
  16421. static char *
  16422. radix_error(e) int e;
  16423. {
  16424.     switch (e) {
  16425.       case 0:  return("Success");
  16426.       case 1:  return("Bad character in encoding");
  16427.       case 2:  return("Encoding not properly padded");
  16428.       case 3:  return("Decoded # of bits not a multiple of 8");
  16429.       case 4:  return("Output buffer too small");
  16430.       default: return("Unknown error");
  16431.     }
  16432. }
  16433. /* END_RUSERPASS */
  16434.  
  16435. #ifdef FTP_SRP
  16436. /*---------------------------------------------------------------------------+
  16437.  |                                                                           |
  16438.  |   Package: srpftp                                                         |
  16439.  |   Author: Eugene Jhong                                                    |
  16440.  |                                                                           |
  16441.  +---------------------------------------------------------------------------*/
  16442.  
  16443. /*
  16444.  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
  16445.  * All Rights Reserved.
  16446.  *
  16447.  * Permission is hereby granted, free of charge, to any person obtaining
  16448.  * a copy of this software and associated documentation files (the
  16449.  * "Software"), to deal in the Software without restriction, including
  16450.  * without limitation the rights to use, copy, modify, merge, publish,
  16451.  * distribute, sublicense, and/or sell copies of the Software, and to
  16452.  * permit persons to whom the Software is furnished to do so, subject to
  16453.  * the following conditions:
  16454.  *
  16455.  * The above copyright notice and this permission notice shall be
  16456.  * included in all copies or substantial portions of the Software.
  16457.  *
  16458.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  16459.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16460.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16461.  *
  16462.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  16463.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16464.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  16465.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  16466.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16467.  *
  16468.  * In addition, the following conditions apply:
  16469.  *
  16470.  * 1. Any software that incorporates the SRP authentication technology
  16471.  *    must display the following acknowlegment:
  16472.  *    "This product uses the 'Secure Remote Password' cryptographic
  16473.  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
  16474.  *
  16475.  * 2. Any software that incorporates all or part of the SRP distribution
  16476.  *    itself must also display the following acknowledgment:
  16477.  *    "This product includes software developed by Tom Wu and Eugene
  16478.  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
  16479.  *
  16480.  * 3. Redistributions in source or binary form must retain an intact copy
  16481.  *    of this copyright notice and list of conditions.
  16482.  */
  16483.  
  16484. #define SRP_PROT_VERSION        1
  16485.  
  16486. #ifdef CK_ENCRYPTION
  16487. #define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
  16488. #else
  16489. #define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
  16490. #endif /* CK_ENCRYPTION */
  16491.  
  16492. #define SRP_DEFAULT_HASH        HASH_ID_SHA
  16493.  
  16494. CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
  16495. CHAR srp_pref_hash = HASH_ID_SHA;
  16496.  
  16497. static struct t_client *tc = NULL;
  16498. static CHAR *skey = NULL;
  16499. static krypto_context *incrypt = NULL;
  16500. static krypto_context *outcrypt = NULL;
  16501.  
  16502. typedef unsigned int srp_uint32;
  16503.  
  16504. /*--------------------------------------------------------------+
  16505.  | srp_selcipher: select cipher                                 |
  16506.  +--------------------------------------------------------------*/
  16507. static int
  16508. srp_selcipher (cname) char *cname; {
  16509.     cipher_desc *cd;
  16510.  
  16511.     if (!(cd = cipher_getdescbyname (cname))) {
  16512.         int i;
  16513.         CHAR *list = cipher_getlist ();
  16514.  
  16515.         fprintf (stderr, "ftp: supported ciphers:\n\n");
  16516.         for (i = 0; i < strlen (list); i++)
  16517.           fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
  16518.         fprintf (stderr, "\n");
  16519.         return -1;
  16520.     }
  16521.     srp_pref_cipher = cd->id;
  16522.     return 0;
  16523. }
  16524.  
  16525. /*--------------------------------------------------------------+
  16526.  | srp_selhash: select hash                                     |
  16527.  +--------------------------------------------------------------*/
  16528. static int
  16529. srp_selhash (hname) char *hname; {
  16530.     hash_desc *hd;
  16531.  
  16532.     if (!(hd = hash_getdescbyname (hname))) {
  16533.         int i;
  16534.         CHAR *list = hash_getlist ();
  16535.  
  16536.         fprintf (stderr, "ftp: supported hash functions:\n\n");
  16537.         for (i = 0; i < strlen (list); i++)
  16538.           fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
  16539.         fprintf (stderr, "\n");
  16540.         return -1;
  16541.     }
  16542.     srp_pref_hash = hd->id;
  16543.     return 0;
  16544. }
  16545.  
  16546. /*--------------------------------------------------------------+
  16547.  | srp_userpass: get username and password                      |
  16548.  +--------------------------------------------------------------*/
  16549. static int
  16550. srp_userpass (host) char *host; {
  16551.     char tmp[BUFSIZ], prompt[PROMPTSIZ];
  16552.     char *user;
  16553.  
  16554.     user = NULL;
  16555. #ifdef USE_RUSERPASS
  16556.     ruserpass (host, &user, &srp_pass, &srp_acct);
  16557. #endif /* USE_RUSERPASS */
  16558.  
  16559.     while (user == NULL)     {
  16560.         char *myname;
  16561.         int ok;
  16562.  
  16563.         myname = whoami();
  16564.         if (!myname) myname = "";
  16565.         if (myname[0])
  16566.           ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
  16567.                     NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  16568.         else
  16569.           ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
  16570.         tmp[0] = '\0';
  16571.         ok = uq_txt(NULL,prompt,1,NULL,tmp,BUFSIZ,NULL,
  16572.                     DEFAULT_UQ_TIMEOUT);
  16573.         if (!ok || *tmp == '\0')
  16574.           user = myname;
  16575.         else
  16576.           user = brstrip(tmp);
  16577.     }
  16578.     ckstrncpy (srp_user, user,BUFSIZ);
  16579.     return(0);
  16580. }
  16581.  
  16582. /*--------------------------------------------------------------+
  16583.  | srp_reset: reset srp information                             |
  16584.  +--------------------------------------------------------------*/
  16585. static int
  16586. srp_reset () {
  16587.     if (tc) { t_clientclose (tc); tc = NULL; }
  16588.     if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
  16589.     if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
  16590.     return(0);
  16591. }
  16592.  
  16593. /*--------------------------------------------------------------+
  16594.  | srp_ftp_auth: perform srp authentication                         |
  16595.  +--------------------------------------------------------------*/
  16596. static int
  16597. srp_ftp_auth(host, user, pass)
  16598.     char *host;
  16599.     char *user;
  16600.     char *pass;
  16601. {
  16602.     struct t_num *wp;
  16603.     struct t_num N;
  16604.     struct t_num g;
  16605.     struct t_num s;
  16606.     struct t_num yp;
  16607.     CHAR buf[FTP_BUFSIZ];
  16608.     CHAR tmp[FTP_BUFSIZ];
  16609.     CHAR *bp, *cp;
  16610.     int n, e, clen, blen, len, i;
  16611.     CHAR cid = 0;
  16612.     CHAR hid = 0;
  16613.  
  16614.     srp_pass = srp_acct = 0;
  16615.  
  16616.     n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
  16617.     if (n != REPLY_CONTINUE) {
  16618.         if (ftp_deb)
  16619.             fprintf(stderr, "SRP rejected as an authentication type\n");
  16620.         return(0);
  16621.     } else {                            /* Send protocol version */
  16622.         CHAR vers[4];
  16623.         memset (vers, 0, 4);
  16624.         vers[3] = SRP_PROT_VERSION;
  16625.         if (!quiet)
  16626.           printf ("SRP accepted as authentication type.\n");
  16627.         bp = tmp; blen = 0;
  16628.         srp_put (vers, &bp, 4, &blen);
  16629.         len = FTP_BUFSIZ;
  16630.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16631.           goto encode_error;
  16632.         reply_parse = "ADAT=";
  16633.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16634.     }
  16635.     if (n == REPLY_CONTINUE) {          /* Get protocol version */
  16636.         bp = buf;
  16637.         if (!reply_parse)
  16638.           goto data_error;
  16639.         blen = FTP_BUFSIZ;
  16640.         if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
  16641.           goto decode_error;
  16642.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16643.           goto data_error;
  16644.  
  16645.         if (host) {                     /* Get username/password if needed */
  16646.             srp_userpass (host);
  16647.         } else {
  16648.             ckstrncpy (srp_user, user, BUFSIZ);
  16649.             srp_pass = pass;
  16650.         }
  16651.         bp = tmp; blen = 0;             /* Send username */
  16652.         srp_put (srp_user, &bp, strlen (srp_user), &blen);
  16653.         len = FTP_BUFSIZ;
  16654.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16655.           goto encode_error;
  16656.         reply_parse = "ADAT=";
  16657.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16658.     }
  16659.     if (n == REPLY_CONTINUE) {          /* Get N, g and s */
  16660.         bp = buf;
  16661.         if (!reply_parse)
  16662.           goto data_error;
  16663.         blen = FTP_BUFSIZ;
  16664.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16665.           goto decode_error;
  16666.         if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
  16667.           goto data_error;
  16668.         if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
  16669.           goto data_error;
  16670.         if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
  16671.           goto data_error;
  16672.         if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
  16673.             fprintf (stderr, "Unable to open SRP client structure.\n");
  16674.             goto bad;
  16675.         }
  16676.         wp = t_clientgenexp (tc);       /* Send wp */
  16677.         bp = tmp; blen = 0;
  16678.         srp_put (wp->data, &bp, wp->len, &blen);
  16679.         len = FTP_BUFSIZ;
  16680.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16681.           goto encode_error;
  16682.         reply_parse = "ADAT=";
  16683.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16684.     }
  16685.     if (n == REPLY_CONTINUE) {          /* Get yp */
  16686.         bp = buf;
  16687.         if (!reply_parse)
  16688.           goto data_error;
  16689.         blen = FTP_BUFSIZ;
  16690.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16691.           goto decode_error;
  16692.         if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
  16693.           goto data_error;
  16694.         if (!srp_pass) {
  16695.             static char ftppass[PASSBUFSIZ];
  16696.             int ok;
  16697.             setint();
  16698.             ok = uq_txt(NULL," SRP Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
  16699.                         DEFAULT_UQ_TIMEOUT);
  16700.             if (ok)
  16701.           srp_pass = brstrip(ftppass);
  16702.         }
  16703.         t_clientpasswd (tc, srp_pass);
  16704.         memset (srp_pass, 0, strlen (srp_pass));
  16705.         skey = t_clientgetkey (tc, &yp); /* Send response */
  16706.         bp = tmp; blen = 0;
  16707.         srp_put (t_clientresponse (tc), &bp, 20, &blen);
  16708.         len = FTP_BUFSIZ;
  16709.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16710.           goto encode_error;
  16711.         reply_parse = "ADAT=";
  16712.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16713.     }
  16714.     if (n == REPLY_CONTINUE) {          /* Get response */
  16715.         bp = buf;
  16716.         if (!reply_parse)
  16717.           goto data_error;
  16718.         blen = FTP_BUFSIZ;
  16719.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16720.           goto encode_error;
  16721.         if (srp_get (&bp, &cp, &blen, &clen) != 20)
  16722.           goto data_error;
  16723.         if (t_clientverify (tc, cp)) {
  16724.             fprintf (stderr, "WARNING: bad response to client challenge.\n");
  16725.             goto bad;
  16726.         }
  16727.         bp = tmp; blen = 0;             /* Send nothing */
  16728.         srp_put ("\0", &bp, 1, &blen);
  16729.         len = FTP_BUFSIZ;
  16730.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16731.           goto encode_error;
  16732.         reply_parse = "ADAT=";
  16733.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16734.     }
  16735.     if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
  16736.         CHAR seqnum[4];
  16737.         CHAR *clist;
  16738.         CHAR *hlist;
  16739.         CHAR *p1;
  16740.         int clist_len, hlist_len;
  16741.         bp = buf;
  16742.         if (!reply_parse)
  16743.           goto data_error;
  16744.         blen = FTP_BUFSIZ;
  16745.         if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
  16746.           goto encode_error;
  16747.         if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
  16748.           goto data_error;
  16749.         if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
  16750.           goto data_error;
  16751.         if (srp_get (&bp, &cp, &blen, &clen) != 4)
  16752.           goto data_error;
  16753.         memcpy (seqnum, cp, 4);
  16754.         if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
  16755.           cid = srp_pref_cipher;
  16756.         if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
  16757.           cid = SRP_DEFAULT_CIPHER;
  16758.         if (!cid) {
  16759.             CHAR *loclist = cipher_getlist ();
  16760.             for (i = 0; i < strlen (loclist); i++)
  16761.               if (cipher_supported (clist, loclist[i])) {
  16762.                   cid = loclist[i];
  16763.                   break;
  16764.               }
  16765.         }
  16766.         if (!cid) {
  16767.             fprintf (stderr, "Unable to agree on cipher.\n");
  16768.             goto bad;
  16769.         }
  16770.         /* Choose hash */
  16771.  
  16772.         if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
  16773.           hid = srp_pref_hash;
  16774.  
  16775.         if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
  16776.           hid = SRP_DEFAULT_HASH;
  16777.  
  16778.         if (!hid) {
  16779.             CHAR *loclist = hash_getlist ();
  16780.             for (i = 0; i < strlen (loclist); i++)
  16781.               if (hash_supported (hlist, loclist[i])) {
  16782.                   hid = loclist[i];
  16783.                   break;
  16784.               }
  16785.         }
  16786.         if (!hid) {
  16787.             fprintf (stderr, "Unable to agree on hash.\n");
  16788.             goto bad;
  16789.         }
  16790.         /* Set incrypt */
  16791.  
  16792.         if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
  16793.                                     KRYPTO_DECODE)))
  16794.           goto bad;
  16795.  
  16796.         /* Generate random number for outkey and outseqnum */
  16797.  
  16798.         t_random (seqnum, 4);
  16799.  
  16800.         /* Send cid, hid, outkey, outseqnum */
  16801.  
  16802.         bp = tmp; blen = 0;
  16803.         srp_put (&cid, &bp, 1, &blen);
  16804.         srp_put (&hid, &bp, 1, &blen);
  16805.         srp_put (seqnum, &bp, 4, &blen);
  16806.         len = FTP_BUFSIZ;
  16807.         if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
  16808.           goto encode_error;
  16809.         reply_parse = "ADAT=";
  16810.         n = ftpcmd("ADAT",buf,-1,-1,0);
  16811.  
  16812.         /* Set outcrypt */
  16813.  
  16814.         if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
  16815.                                      KRYPTO_ENCODE)))
  16816.           goto bad;
  16817.  
  16818.         t_clientclose (tc);
  16819.         tc = NULL;
  16820.     }
  16821.     if (n != REPLY_COMPLETE)
  16822.       goto bad;
  16823.  
  16824.     if (ftp_vbm) {
  16825.         if (ftp_deb)
  16826.           printf("\n");
  16827.         printf ("SRP authentication succeeded.\n");
  16828.         printf ("Using cipher %s and hash function %s.\n",
  16829.                 (cipher_getdescbyid(cid))->name,
  16830.                 (hash_getdescbyid(hid))->name
  16831.                 );
  16832.     }
  16833.     reply_parse = NULL;
  16834.     auth_type = "SRP";
  16835.     return(1);
  16836.  
  16837.   encode_error:
  16838.     fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
  16839.     goto bad;
  16840.  
  16841.   decode_error:
  16842.     fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
  16843.     goto bad;
  16844.  
  16845.   data_error:
  16846.     fprintf (stderr, "Unable to unmarshal authentication data.\n");
  16847.     goto bad;
  16848.  
  16849.   bad:
  16850.     fprintf (stderr, "SRP authentication failed, trying regular login.\n");
  16851.     reply_parse = NULL;
  16852.     return(0);
  16853. }
  16854.  
  16855. /*--------------------------------------------------------------+
  16856.  | srp_put: put item to send buffer                             |
  16857.  +--------------------------------------------------------------*/
  16858. static int
  16859. srp_put (in, out, inlen, outlen)
  16860.     CHAR *in;
  16861.     CHAR **out;
  16862.     int inlen;
  16863.     int *outlen;
  16864. {
  16865.     srp_uint32 net_len;
  16866.  
  16867.     net_len = htonl (inlen);
  16868.     memcpy (*out, &net_len, 4);
  16869.  
  16870.     *out += 4; *outlen += 4;
  16871.  
  16872.     memcpy (*out, in, inlen);
  16873.  
  16874.     *out += inlen; *outlen += inlen;
  16875.     return(0);
  16876. }
  16877.  
  16878. /*--------------------------------------------------------------+
  16879.  | srp_get: get item from receive buffer                        |
  16880.  +--------------------------------------------------------------*/
  16881. static int
  16882. srp_get (in, out, inlen, outlen)
  16883.     CHAR **in;
  16884.     CHAR **out;
  16885.     int *inlen;
  16886.     int *outlen;
  16887. {
  16888.     srp_uint32 net_len;
  16889.  
  16890.     if (*inlen < 4) return -1;
  16891.  
  16892.     memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
  16893.     *outlen = ntohl (net_len);
  16894.  
  16895.     if (*inlen < *outlen) return -1;
  16896.  
  16897.     *out = *in; *inlen -= *outlen; *in += *outlen;
  16898.  
  16899.     return *outlen;
  16900. }
  16901.  
  16902. /*--------------------------------------------------------------+
  16903.  | srp_encode: encode control message                           |
  16904.  +--------------------------------------------------------------*/
  16905. static int
  16906. srp_encode (private, in, out, len)
  16907.     int private;
  16908.     CHAR *in;
  16909.     CHAR *out;
  16910.     unsigned len;
  16911. {
  16912.     if (private)
  16913.       return krypto_msg_priv (outcrypt, in, out, len);
  16914.     else
  16915.       return krypto_msg_safe (outcrypt, in, out, len);
  16916. }
  16917.  
  16918. /*--------------------------------------------------------------+
  16919.  | srp_decode: decode control message                           |
  16920.  +--------------------------------------------------------------*/
  16921. static int
  16922. srp_decode (private, in, out, len)
  16923.     int private;
  16924.     CHAR *in;
  16925.     CHAR *out;
  16926.     unsigned len;
  16927. {
  16928.     if (private)
  16929.       return krypto_msg_priv (incrypt, in, out, len);
  16930.     else
  16931.       return krypto_msg_safe (incrypt, in, out, len);
  16932. }
  16933.  
  16934. #endif /* FTP_SRP */
  16935.  
  16936.  
  16937.  
  16938. #ifdef NOT_USED
  16939. /*
  16940.   The following code is from the Unix FTP client.  Be sure to
  16941.   make sure that the functionality is not lost.  Especially
  16942.   the Proxy stuff even though we have not yet implemented it.
  16943. */
  16944.  
  16945. /* Send multiple files  */
  16946.  
  16947. static int
  16948. ftp_mput(argc, argv) int argc; char **argv; {
  16949.     register int i;
  16950.     sig_t oldintr;
  16951.     int ointer;
  16952.     char *tp;
  16953.     sigtype mcancel();
  16954.  
  16955.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  16956.         printf("usage: %s local-files\n", argv[0]);
  16957.         ftpcode = -1;
  16958.         return;
  16959.     }
  16960.     mname = argv[0];
  16961.     mflag = 1;
  16962.     oldintr = signal(SIGINT, mcancel);
  16963.  
  16964.     /* Replace with calls to cc_execute() */
  16965.     setjmp(jcancel);
  16966. #ifdef FTP_PROXY
  16967.     if (proxy) {
  16968.         char *cp, *tp2, tmpbuf[CKMAXPATH];
  16969.  
  16970.         while ((cp = remglob(argv,0)) != NULL) {
  16971.             if (*cp == 0) {
  16972.                 mflag = 0;
  16973.                 continue;
  16974.             }
  16975.             if (mflag && confirm(argv[0], cp)) {
  16976.                 tp = cp;
  16977.                 if (mcase) {
  16978.                     while (*tp && !islower(*tp)) {
  16979.                         tp++;
  16980.                     }
  16981.                     if (!*tp) {
  16982.                         tp = cp;
  16983.                         tp2 = tmpbuf;
  16984.                         while ((*tp2 = *tp) != 0) {
  16985.                             if (isupper(*tp2)) {
  16986.                                 *tp2 = 'a' + *tp2 - 'A';
  16987.                             }
  16988.                             tp++;
  16989.                             tp2++;
  16990.                         }
  16991.                     }
  16992.                     tp = tmpbuf;
  16993.                 }
  16994.                 if (ntflag) {
  16995.                     tp = dotrans(tp);
  16996.                 }
  16997.                 if (mapflag) {
  16998.                     tp = domap(tp);
  16999.                 }
  17000.                 sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
  17001.                 if (!mflag && fromatty) {
  17002.                     ointer = interactive;
  17003.                     interactive = 1;
  17004.                     if (confirm("Continue with","mput")) {
  17005.                         mflag++;
  17006.                     }
  17007.                     interactive = ointer;
  17008.                 }
  17009.             }
  17010.         }
  17011.         signal(SIGINT, oldintr);
  17012.         mflag = 0;
  17013.         return;
  17014.     }
  17015. #endif /* FTP_PROXY */
  17016.     for (i = 1; i < argc; i++) {
  17017.         register char **cpp, **gargs;
  17018.  
  17019.         if (mflag && confirm(argv[0], argv[i])) {
  17020.             tp = argv[i];
  17021.             sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
  17022.             if (!mflag && fromatty) {
  17023.                 ointer = interactive;
  17024.                 interactive = 1;
  17025.                 if (confirm("Continue with","mput")) {
  17026.                     mflag++;
  17027.                 }
  17028.                 interactive = ointer;
  17029.             }
  17030.         }
  17031.         continue;
  17032.  
  17033.         gargs = ftpglob(argv[i]);
  17034.         if (globerr != NULL) {
  17035.             printf("%s\n", globerr);
  17036.             if (gargs) {
  17037.                 blkfree(gargs);
  17038.                 free((char *)gargs);
  17039.             }
  17040.             continue;
  17041.         }
  17042.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  17043.             if (mflag && confirm(argv[0], *cpp)) {
  17044.                 tp = *cpp;
  17045.                 sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
  17046.                 if (!mflag && fromatty) {
  17047.                     ointer = interactive;
  17048.                     interactive = 1;
  17049.                     if (confirm("Continue with","mput")) {
  17050.                         mflag++;
  17051.                     }
  17052.                     interactive = ointer;
  17053.                 }
  17054.             }
  17055.         }
  17056.         if (gargs != NULL) {
  17057.             blkfree(gargs);
  17058.             free((char *)gargs);
  17059.         }
  17060.     }
  17061.     signal(SIGINT, oldintr);
  17062.     mflag = 0;
  17063. }
  17064.  
  17065. /* Get multiple files */
  17066.  
  17067. static int
  17068. ftp_mget(argc, argv) int argc; char **argv; {
  17069.     int rc = -1;
  17070.     sig_t oldintr;
  17071.     int ointer;
  17072.     char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
  17073.     sigtype mcancel();
  17074.  
  17075.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  17076.         printf("usage: %s remote-files\n", argv[0]);
  17077.         ftpcode = -1;
  17078.         return(-1);
  17079.     }
  17080.     mname = argv[0];
  17081.     mflag = 1;
  17082.     oldintr = signal(SIGINT,mcancel);
  17083.     /* Replace with calls to cc_execute() */
  17084.     setjmp(jcancel);
  17085.     while ((cp = remglob(argv,proxy)) != NULL) {
  17086.         if (*cp == '\0') {
  17087.             mflag = 0;
  17088.             continue;
  17089.         }
  17090.         if (mflag && confirm(argv[0], cp)) {
  17091.             tp = cp;
  17092.             if (mcase) {
  17093.                 while (*tp && !islower(*tp)) {
  17094.                     tp++;
  17095.                 }
  17096.                 if (!*tp) {
  17097.                     tp = cp;
  17098.                     tp2 = tmpbuf;
  17099.                     while ((*tp2 = *tp) != 0) {
  17100.                         if (isupper(*tp2)) {
  17101.                             *tp2 = 'a' + *tp2 - 'A';
  17102.                         }
  17103.                         tp++;
  17104.                         tp2++;
  17105.                     }
  17106.                 }
  17107.                 tp = tmpbuf;
  17108.             }
  17109.             rc = (recvrequest("RETR", tp, cp, "wb",
  17110.                                tp != cp || !interactive) == 0,0,NULL,0,0,0);
  17111.             if (!mflag && fromatty) {
  17112.                 ointer = interactive;
  17113.                 interactive = 1;
  17114.                 if (confirm("Continue with","mget")) {
  17115.                     mflag++;
  17116.                 }
  17117.                 interactive = ointer;
  17118.             }
  17119.         }
  17120.     }
  17121.     signal(SIGINT,oldintr);
  17122.     mflag = 0;
  17123.     return(rc);
  17124. }
  17125.  
  17126. /* Delete multiple files */
  17127.  
  17128. static int
  17129. mdelete(argc, argv) int argc; char **argv; {
  17130.     sig_t oldintr;
  17131.     int ointer;
  17132.     char *cp;
  17133.     sigtype mcancel();
  17134.  
  17135.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  17136.         printf("usage: %s remote-files\n", argv[0]);
  17137.         ftpcode = -1;
  17138.         return(-1);
  17139.     }
  17140.     mname = argv[0];
  17141.     mflag = 1;
  17142.     oldintr = signal(SIGINT, mcancel);
  17143.     /* Replace with calls to cc_execute() */
  17144.     setjmp(jcancel);
  17145.     while ((cp = remglob(argv,0)) != NULL) {
  17146.         if (*cp == '\0') {
  17147.             mflag = 0;
  17148.             continue;
  17149.         }
  17150.         if (mflag && confirm(argv[0], cp)) {
  17151.             rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
  17152.             if (!mflag && fromatty) {
  17153.                 ointer = interactive;
  17154.                 interactive = 1;
  17155.                 if (confirm("Continue with", "mdelete")) {
  17156.                     mflag++;
  17157.                 }
  17158.                 interactive = ointer;
  17159.             }
  17160.         }
  17161.     }
  17162.     signal(SIGINT, oldintr);
  17163.     mflag = 0;
  17164.     return(rc);
  17165. }
  17166.  
  17167. /* Get a directory listing of multiple remote files */
  17168.  
  17169. static int
  17170. mls(argc, argv) int argc; char **argv; {
  17171.     sig_t oldintr;
  17172.     int ointer, i;
  17173.     char *cmd, mode[1], *dest;
  17174.     sigtype mcancel();
  17175.     int rc = -1;
  17176.  
  17177.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  17178.       goto usage;
  17179.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  17180.       usage:
  17181.         printf("usage: %s remote-files local-file\n", argv[0]);
  17182.         ftpcode = -1;
  17183.         return(-1);
  17184.     }
  17185.     dest = argv[argc - 1];
  17186.     argv[argc - 1] = NULL;
  17187.     if (strcmp(dest, "-") && *dest != '|')
  17188.       if (!globulize(&dest) ||
  17189.           !confirm("output to local-file:", dest)) {
  17190.           ftpcode = -1;
  17191.           return(-1);
  17192.       }
  17193.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  17194.     mname = argv[0];
  17195.     mflag = 1;
  17196.     oldintr = signal(SIGINT, mcancel);
  17197.     /* Replace with calls to cc_execute() */
  17198.     setjmp(jcancel);
  17199.     for (i = 1; mflag && i < argc-1; ++i) {
  17200.         *mode = (i == 1) ? 'w' : 'a';
  17201.         rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
  17202.         if (!mflag && fromatty) {
  17203.             ointer = interactive;
  17204.             interactive = 1;
  17205.             if (confirm("Continue with", argv[0])) {
  17206.                 mflag ++;
  17207.             }
  17208.             interactive = ointer;
  17209.         }
  17210.     }
  17211.     signal(SIGINT, oldintr);
  17212.     mflag = 0;
  17213.     return(rc);
  17214. }
  17215.  
  17216. static char *
  17217. remglob(argv,doswitch) char *argv[]; int doswitch; {
  17218.     char temp[16];
  17219.     static char buf[CKMAXPATH];
  17220.     static FILE *ftemp = NULL;
  17221.     static char **args;
  17222.     int oldhash;
  17223.     char *cp, *mode;
  17224.  
  17225.     if (!mflag) {
  17226.         if (!doglob) {
  17227.             args = NULL;
  17228.         } else {
  17229.             if (ftemp) {
  17230.                 (void) fclose(ftemp);
  17231.                 ftemp = NULL;
  17232.             }
  17233.         }
  17234.         return(NULL);
  17235.     }
  17236.     if (!doglob) {
  17237.         if (args == NULL)
  17238.           args = argv;
  17239.         if ((cp = *++args) == NULL)
  17240.           args = NULL;
  17241.         return(cp);
  17242.     }
  17243.     if (ftemp == NULL) {
  17244.         (void) strcpy(temp, _PATH_TMP);
  17245. #ifdef MKTEMP
  17246. #ifndef MKSTEMP
  17247.         (void) mktemp(temp);
  17248. #endif /* MKSTEMP */
  17249. #endif /* MKTEMP */
  17250.         verbose = 0;
  17251.         oldhash = hash, hash = 0;
  17252. #ifdef FTP_PROXY
  17253.         if (doswitch) {
  17254.             pswitch(!proxy);
  17255.         }
  17256. #endif /* FTP_PROXY */
  17257.         for (mode = "wb"; *++argv != NULL; mode = "ab")
  17258.           recvrequest ("NLST", temp, *argv, mode, 0);
  17259. #ifdef FTP_PROXY
  17260.         if (doswitch) {
  17261.             pswitch(!proxy);
  17262.         }
  17263. #endif /* FTP_PROXY */
  17264.         hash = oldhash;
  17265.         ftemp = fopen(temp, "r");
  17266.         unlink(temp);
  17267.         if (ftemp == NULL && (!dpyactive || ftp_deb)) {
  17268.             printf("Can't find list of remote files, oops\n");
  17269.             return(NULL);
  17270.         }
  17271.     }
  17272.     if (fgets(buf, CKMAXPATH, ftemp) == NULL) {
  17273.         fclose(ftemp), ftemp = NULL;
  17274.         return(NULL);
  17275.     }
  17276.     if ((cp = ckstrchr(buf,'\n')) != NULL)
  17277.       *cp = '\0';
  17278.     return(buf);
  17279. }
  17280. #endif /* NOT_USED */
  17281. #endif /* TCPSOCKET (top of file) */
  17282. #endif /* SYSFTP (top of file) */
  17283. #endif /* NOFTP (top of file) */
  17284.