ftpfs.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. /* Virtual File System: FTP file system.
  2. Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. 2006, 2007 Free Software Foundation, Inc.
  4. Written by:
  5. 1995 Ching Hui
  6. 1995 Jakub Jelinek
  7. 1995, 1996, 1997 Miguel de Icaza
  8. 1997 Norbert Warmuth
  9. 1998 Pavel Machek
  10. 2010 Yury V. Zaytsev
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU Library General Public License
  13. as published by the Free Software Foundation; either version 2 of
  14. the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Library General Public License for more details.
  19. You should have received a copy of the GNU Library General Public
  20. License along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  22. /**
  23. * \file
  24. * \brief Source: Virtual File System: FTP file system
  25. * \author Ching Hui
  26. * \author Jakub Jelinek
  27. * \author Miguel de Icaza
  28. * \author Norbert Warmuth
  29. * \author Pavel Machek
  30. * \date 1995, 1997, 1998
  31. *
  32. * \todo
  33. - make it more robust - all the connects etc. should handle EADDRINUSE and
  34. ERETRY (have I spelled these names correctly?)
  35. - make the user able to flush a connection - all the caches will get empty
  36. etc., (tarfs as well), we should give there a user selectable timeout
  37. and assign a key sequence.
  38. - use hash table instead of linklist to cache ftpfs directory.
  39. What to do with this?
  40. * NOTE: Usage of tildes is deprecated, consider:
  41. * \verbatim
  42. cd /#ftp:pavel@hobit
  43. cd ~
  44. \endverbatim
  45. * And now: what do I want to do? Do I want to go to /home/pavel or to
  46. * /#ftp:hobit/home/pavel? I think first has better sense...
  47. *
  48. \verbatim
  49. {
  50. int f = !strcmp( remote_path, "/~" );
  51. if (f || !strncmp( remote_path, "/~/", 3 )) {
  52. char *s;
  53. s = concat_dir_and_file( qhome (*bucket), remote_path +3-f );
  54. g_free (remote_path);
  55. remote_path = s;
  56. }
  57. }
  58. \endverbatim
  59. */
  60. /* \todo Fix: Namespace pollution: horrible */
  61. #include <config.h>
  62. #include <stdlib.h> /* atoi() */
  63. #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
  64. #include <netdb.h> /* struct hostent */
  65. #include <sys/socket.h> /* AF_INET */
  66. #include <netinet/in.h> /* struct in_addr */
  67. #ifdef HAVE_ARPA_INET_H
  68. #include <arpa/inet.h>
  69. #endif
  70. #include <arpa/ftp.h>
  71. #include <arpa/telnet.h>
  72. #include <sys/param.h>
  73. #include <errno.h>
  74. #include <ctype.h>
  75. #include <fcntl.h>
  76. #include <sys/time.h> /* gettimeofday() */
  77. #include <inttypes.h> /* uintmax_t */
  78. #include "lib/global.h"
  79. #include "lib/util.h"
  80. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  81. #include "lib/widget.h" /* message() */
  82. #include "src/filemanager/layout.h" /* print_vfs_message */
  83. #include "src/history.h"
  84. #include "src/setup.h" /* for load_anon_passwd */
  85. #include "vfs-impl.h"
  86. #include "utilvfs.h"
  87. #include "netutil.h"
  88. #include "xdirentry.h"
  89. #include "gc.h" /* vfs_stamp_create */
  90. #include "ftpfs.h"
  91. /*** global variables ****************************************************************************/
  92. /* Delay to retry a connection */
  93. int ftpfs_retry_seconds = 30;
  94. /* Method to use to connect to ftp sites */
  95. int ftpfs_use_passive_connections = 1;
  96. int ftpfs_use_passive_connections_over_proxy = 0;
  97. /* Method used to get directory listings:
  98. * 1: try 'LIST -la <path>', if it fails
  99. * fall back to CWD <path>; LIST
  100. * 0: always use CWD <path>; LIST
  101. */
  102. int ftpfs_use_unix_list_options = 1;
  103. /* First "CWD <path>", then "LIST -la ." */
  104. int ftpfs_first_cd_then_ls = 1;
  105. /* Use the ~/.netrc */
  106. int ftpfs_use_netrc = 1;
  107. /* Anonymous setup */
  108. char *ftpfs_anonymous_passwd = NULL;
  109. int ftpfs_directory_timeout = 900;
  110. /* Proxy host */
  111. char *ftpfs_proxy_host = NULL;
  112. /* wether we have to use proxy by default? */
  113. int ftpfs_always_use_proxy = 0;
  114. int ftpfs_ignore_chattr_errors = 1;
  115. /*** file scope macro definitions ****************************************************************/
  116. #ifndef MAXHOSTNAMELEN
  117. #define MAXHOSTNAMELEN 64
  118. #endif
  119. #define UPLOAD_ZERO_LENGTH_FILE
  120. #define SUP super->u.ftp
  121. #define FH_SOCK fh->u.ftp.sock
  122. #ifndef INADDR_NONE
  123. #define INADDR_NONE 0xffffffff
  124. #endif
  125. /* for uclibc < 0.9.29 */
  126. #ifndef AI_ADDRCONFIG
  127. #define AI_ADDRCONFIG 0x0020
  128. #endif
  129. #define RFC_AUTODETECT 0
  130. #define RFC_DARING 1
  131. #define RFC_STRICT 2
  132. /* ftpfs_command wait_flag: */
  133. #define NONE 0x00
  134. #define WAIT_REPLY 0x01
  135. #define WANT_STRING 0x02
  136. #define FTP_COMMAND_PORT 21
  137. /* some defines only used by ftpfs_changetype */
  138. /* These two are valid values for the second parameter */
  139. #define TYPE_ASCII 0
  140. #define TYPE_BINARY 1
  141. /* This one is only used to initialize bucket->isbinary, don't use it as
  142. second parameter to ftpfs_changetype. */
  143. #define TYPE_UNKNOWN -1
  144. #define ABORT_TIMEOUT 5
  145. /*** file scope type declarations ****************************************************************/
  146. #ifndef HAVE_SOCKLEN_T
  147. typedef int socklen_t;
  148. #endif
  149. /* This should match the keywords[] array below */
  150. typedef enum
  151. {
  152. NETRC_NONE = 0,
  153. NETRC_DEFAULT,
  154. NETRC_MACHINE,
  155. NETRC_LOGIN,
  156. NETRC_PASSWORD,
  157. NETRC_PASSWD,
  158. NETRC_ACCOUNT,
  159. NETRC_MACDEF,
  160. NETRC_UNKNOWN
  161. } keyword_t;
  162. /*** file scope variables ************************************************************************/
  163. static int ftpfs_errno;
  164. static int code;
  165. #ifdef FIXME_LATER_ALIGATOR
  166. static struct linklist *connections_list;
  167. #endif
  168. static char reply_str[80];
  169. static struct vfs_class vfs_ftpfs_ops;
  170. static struct no_proxy_entry
  171. {
  172. char *domain;
  173. void *next;
  174. } *no_proxy;
  175. static char buffer[BUF_MEDIUM];
  176. static char *netrc;
  177. static const char *netrcp;
  178. /*** file scope functions ************************************************************************/
  179. /* --------------------------------------------------------------------------------------------- */
  180. /* char *ftpfs_translate_path (struct ftpfs_connection *bucket, char *remote_path)
  181. Translate a Unix path, i.e. MC's internal path representation (e.g.
  182. /somedir/somefile) to a path valid for the remote server. Every path
  183. transfered to the remote server has to be mangled by this function
  184. right prior to sending it.
  185. Currently only Amiga ftp servers are handled in a special manner.
  186. When the remote server is an amiga:
  187. a) strip leading slash if necesarry
  188. b) replace first occurance of ":/" with ":"
  189. c) strip trailing "/."
  190. */
  191. static char *ftpfs_get_current_directory (struct vfs_class *me, struct vfs_s_super *super);
  192. static int ftpfs_chdir_internal (struct vfs_class *me, struct vfs_s_super *super,
  193. const char *remote_path);
  194. static int ftpfs_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply,
  195. const char *fmt, ...) __attribute__ ((format (__printf__, 4, 5)));
  196. static int ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super);
  197. static int ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super,
  198. const char *netrcpass);
  199. static int ftpfs_netrc_lookup (const char *host, char **login, char **pass);
  200. /* --------------------------------------------------------------------------------------------- */
  201. static char *
  202. ftpfs_translate_path (struct vfs_class *me, struct vfs_s_super *super, const char *remote_path)
  203. {
  204. if (!SUP.remote_is_amiga)
  205. return g_strdup (remote_path);
  206. else
  207. {
  208. char *ret, *p;
  209. if (MEDATA->logfile)
  210. {
  211. fprintf (MEDATA->logfile, "MC -- ftpfs_translate_path: %s\n", remote_path);
  212. fflush (MEDATA->logfile);
  213. }
  214. /* strip leading slash(es) */
  215. while (*remote_path == '/')
  216. remote_path++;
  217. /*
  218. * Don't change "/" into "", e.g. "CWD " would be
  219. * invalid.
  220. */
  221. if (*remote_path == '\0')
  222. return g_strdup (".");
  223. ret = g_strdup (remote_path);
  224. /* replace first occurance of ":/" with ":" */
  225. p = strchr (ret, ':');
  226. if ((p != NULL) && (*(p + 1) == '/'))
  227. memmove (p + 1, p + 2, strlen (p + 2) + 1);
  228. /* strip trailing "/." */
  229. p = strrchr (ret, '/');
  230. if ((p != NULL) && (*(p + 1) == '.') && (*(p + 2) == '\0'))
  231. *p = '\0';
  232. return ret;
  233. }
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. /** Extract the hostname and username from the path */
  237. /*
  238. * path is in the form: [user@]hostname:port/remote-dir, e.g.:
  239. * ftp://sunsite.unc.edu/pub/linux
  240. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  241. * ftp://tsx-11.mit.edu:8192/
  242. * ftp://joe@foo.edu:11321/private
  243. * If the user is empty, e.g. ftp://@roxanne/private, then your login name
  244. * is supplied.
  245. *
  246. */
  247. static void
  248. ftpfs_split_url (char *path, char **host, char **user, int *port, char **pass)
  249. {
  250. char *p;
  251. p = vfs_split_url (path, host, user, port, pass, FTP_COMMAND_PORT, URL_USE_ANONYMOUS);
  252. if (!*user)
  253. {
  254. /* Look up user and password in netrc */
  255. if (ftpfs_use_netrc)
  256. ftpfs_netrc_lookup (*host, user, pass);
  257. if (!*user)
  258. *user = g_strdup ("anonymous");
  259. }
  260. /* Look up password in netrc for known user */
  261. if (ftpfs_use_netrc && *user && pass && !*pass)
  262. {
  263. char *new_user;
  264. ftpfs_netrc_lookup (*host, &new_user, pass);
  265. /* If user is different, remove password */
  266. if (new_user && strcmp (*user, new_user))
  267. {
  268. g_free (*pass);
  269. *pass = NULL;
  270. }
  271. g_free (new_user);
  272. }
  273. g_free (p);
  274. }
  275. /* --------------------------------------------------------------------------------------------- */
  276. /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
  277. static int
  278. ftpfs_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
  279. {
  280. char answer[BUF_1K];
  281. int i;
  282. for (;;)
  283. {
  284. if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
  285. {
  286. if (string_buf)
  287. *string_buf = 0;
  288. code = 421;
  289. return 4;
  290. }
  291. switch (sscanf (answer, "%d", &code))
  292. {
  293. case 0:
  294. if (string_buf)
  295. g_strlcpy (string_buf, answer, string_len);
  296. code = 500;
  297. return 5;
  298. case 1:
  299. if (answer[3] == '-')
  300. {
  301. while (1)
  302. {
  303. if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
  304. {
  305. if (string_buf)
  306. *string_buf = 0;
  307. code = 421;
  308. return 4;
  309. }
  310. if ((sscanf (answer, "%d", &i) > 0) && (code == i) && (answer[3] == ' '))
  311. break;
  312. }
  313. }
  314. if (string_buf)
  315. g_strlcpy (string_buf, answer, string_len);
  316. return code / 100;
  317. }
  318. }
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. static int
  322. ftpfs_reconnect (struct vfs_class *me, struct vfs_s_super *super)
  323. {
  324. int sock = ftpfs_open_socket (me, super);
  325. if (sock != -1)
  326. {
  327. char *cwdir = SUP.cwdir;
  328. close (SUP.sock);
  329. SUP.sock = sock;
  330. SUP.cwdir = NULL;
  331. if (ftpfs_login_server (me, super, SUP.password))
  332. {
  333. if (!cwdir)
  334. return 1;
  335. sock = ftpfs_chdir_internal (me, super, cwdir);
  336. g_free (cwdir);
  337. return sock == COMPLETE;
  338. }
  339. SUP.cwdir = cwdir;
  340. }
  341. return 0;
  342. }
  343. /* --------------------------------------------------------------------------------------------- */
  344. static int
  345. ftpfs_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt,
  346. ...)
  347. {
  348. va_list ap;
  349. char *cmdstr;
  350. int status, cmdlen;
  351. static int retry = 0;
  352. static int level = 0; /* ftpfs_login_server() use ftpfs_command() */
  353. va_start (ap, fmt);
  354. cmdstr = g_strdup_vprintf (fmt, ap);
  355. va_end (ap);
  356. cmdlen = strlen (cmdstr);
  357. cmdstr = g_realloc (cmdstr, cmdlen + 3);
  358. strcpy (cmdstr + cmdlen, "\r\n");
  359. cmdlen += 2;
  360. if (MEDATA->logfile)
  361. {
  362. if (strncmp (cmdstr, "PASS ", 5) == 0)
  363. {
  364. fputs ("PASS <Password not logged>\r\n", MEDATA->logfile);
  365. }
  366. else
  367. {
  368. size_t ret;
  369. ret = fwrite (cmdstr, cmdlen, 1, MEDATA->logfile);
  370. }
  371. fflush (MEDATA->logfile);
  372. }
  373. got_sigpipe = 0;
  374. tty_enable_interrupt_key ();
  375. status = write (SUP.sock, cmdstr, cmdlen);
  376. if (status < 0)
  377. {
  378. code = 421;
  379. if (errno == EPIPE)
  380. { /* Remote server has closed connection */
  381. if (level == 0)
  382. {
  383. level = 1;
  384. status = ftpfs_reconnect (me, super);
  385. level = 0;
  386. if (status && (write (SUP.sock, cmdstr, cmdlen) > 0))
  387. {
  388. goto ok;
  389. }
  390. }
  391. got_sigpipe = 1;
  392. }
  393. g_free (cmdstr);
  394. tty_disable_interrupt_key ();
  395. return TRANSIENT;
  396. }
  397. retry = 0;
  398. ok:
  399. tty_disable_interrupt_key ();
  400. if (wait_reply)
  401. {
  402. status = ftpfs_get_reply (me, SUP.sock,
  403. (wait_reply & WANT_STRING) ? reply_str : NULL,
  404. sizeof (reply_str) - 1);
  405. if ((wait_reply & WANT_STRING) && !retry && !level && code == 421)
  406. {
  407. retry = 1;
  408. level = 1;
  409. status = ftpfs_reconnect (me, super);
  410. level = 0;
  411. if (status && (write (SUP.sock, cmdstr, cmdlen) > 0))
  412. {
  413. goto ok;
  414. }
  415. }
  416. retry = 0;
  417. g_free (cmdstr);
  418. return status;
  419. }
  420. g_free (cmdstr);
  421. return COMPLETE;
  422. }
  423. /* --------------------------------------------------------------------------------------------- */
  424. static void
  425. ftpfs_free_archive (struct vfs_class *me, struct vfs_s_super *super)
  426. {
  427. if (SUP.sock != -1)
  428. {
  429. print_vfs_message (_("ftpfs: Disconnecting from %s"), SUP.host);
  430. ftpfs_command (me, super, NONE, "QUIT");
  431. close (SUP.sock);
  432. }
  433. g_free (SUP.host);
  434. g_free (SUP.user);
  435. g_free (SUP.cwdir);
  436. g_free (SUP.password);
  437. }
  438. /* --------------------------------------------------------------------------------------------- */
  439. static int
  440. ftpfs_changetype (struct vfs_class *me, struct vfs_s_super *super, int binary)
  441. {
  442. if (binary != SUP.isbinary)
  443. {
  444. if (ftpfs_command (me, super, WAIT_REPLY, "TYPE %c", binary ? 'I' : 'A') != COMPLETE)
  445. ERRNOR (EIO, -1);
  446. SUP.isbinary = binary;
  447. }
  448. return binary;
  449. }
  450. /* --------------------------------------------------------------------------------------------- */
  451. /* This routine logs the user in */
  452. static int
  453. ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super, const char *netrcpass)
  454. {
  455. char *pass;
  456. char *op;
  457. char *name; /* login user name */
  458. int anon = 0;
  459. char reply_string[BUF_MEDIUM];
  460. SUP.isbinary = TYPE_UNKNOWN;
  461. if (SUP.password) /* explicit password */
  462. op = g_strdup (SUP.password);
  463. else if (netrcpass) /* password from netrc */
  464. op = g_strdup (netrcpass);
  465. else if (!strcmp (SUP.user, "anonymous") || !strcmp (SUP.user, "ftp"))
  466. {
  467. if (!ftpfs_anonymous_passwd) /* default anonymous password */
  468. ftpfs_init_passwd ();
  469. op = g_strdup (ftpfs_anonymous_passwd);
  470. anon = 1;
  471. }
  472. else
  473. { /* ask user */
  474. char *p;
  475. p = g_strdup_printf (_("FTP: Password required for %s"), SUP.user);
  476. op = vfs_get_password (p);
  477. g_free (p);
  478. if (op == NULL)
  479. ERRNOR (EPERM, 0);
  480. SUP.password = g_strdup (op);
  481. }
  482. if (!anon || MEDATA->logfile)
  483. pass = op;
  484. else
  485. {
  486. pass = g_strconcat ("-", op, (char *) NULL);
  487. wipe_password (op);
  488. }
  489. /* Proxy server accepts: username@host-we-want-to-connect */
  490. if (SUP.proxy)
  491. {
  492. name =
  493. g_strconcat (SUP.user, "@",
  494. SUP.host[0] == '!' ? SUP.host + 1 : SUP.host, (char *) NULL);
  495. }
  496. else
  497. name = g_strdup (SUP.user);
  498. if (ftpfs_get_reply (me, SUP.sock, reply_string, sizeof (reply_string) - 1) == COMPLETE)
  499. {
  500. char *reply_up;
  501. reply_up = g_ascii_strup (reply_string, -1);
  502. SUP.remote_is_amiga = strstr (reply_up, "AMIGA") != 0;
  503. g_free (reply_up);
  504. if (MEDATA->logfile)
  505. {
  506. fprintf (MEDATA->logfile, "MC -- remote_is_amiga = %d\n", SUP.remote_is_amiga);
  507. fflush (MEDATA->logfile);
  508. }
  509. print_vfs_message (_("ftpfs: sending login name"));
  510. switch (ftpfs_command (me, super, WAIT_REPLY, "USER %s", name))
  511. {
  512. case CONTINUE:
  513. print_vfs_message (_("ftpfs: sending user password"));
  514. code = ftpfs_command (me, super, WAIT_REPLY, "PASS %s", pass);
  515. if (code == CONTINUE)
  516. {
  517. char *p;
  518. p = g_strdup_printf (_("FTP: Account required for user %s"), SUP.user);
  519. op = input_dialog (p, _("Account:"), MC_HISTORY_FTPFS_ACCOUNT, "");
  520. g_free (p);
  521. if (op == NULL)
  522. ERRNOR (EPERM, 0);
  523. print_vfs_message (_("ftpfs: sending user account"));
  524. code = ftpfs_command (me, super, WAIT_REPLY, "ACCT %s", op);
  525. g_free (op);
  526. }
  527. if (code != COMPLETE)
  528. break;
  529. /* fall through */
  530. case COMPLETE:
  531. print_vfs_message (_("ftpfs: logged in"));
  532. wipe_password (pass);
  533. g_free (name);
  534. return 1;
  535. default:
  536. SUP.failed_on_login = 1;
  537. if (SUP.password)
  538. wipe_password (SUP.password);
  539. SUP.password = 0;
  540. goto login_fail;
  541. }
  542. }
  543. message (D_ERROR, MSG_ERROR, _("ftpfs: Login incorrect for user %s "), SUP.user);
  544. login_fail:
  545. wipe_password (pass);
  546. g_free (name);
  547. ERRNOR (EPERM, 0);
  548. }
  549. /* --------------------------------------------------------------------------------------------- */
  550. static void
  551. ftpfs_load_no_proxy_list (void)
  552. {
  553. /* FixMe: shouldn't be hardcoded!!! */
  554. char s[BUF_LARGE]; /* provide for BUF_LARGE characters */
  555. struct no_proxy_entry *np, *current = 0;
  556. FILE *npf;
  557. int c;
  558. char *p;
  559. static char *mc_file;
  560. if (mc_file)
  561. return;
  562. mc_file = concat_dir_and_file (mc_home, "mc.no_proxy");
  563. if (exist_file (mc_file))
  564. {
  565. npf = fopen (mc_file, "r");
  566. if (npf != NULL)
  567. {
  568. while (fgets (s, sizeof (s), npf) != NULL)
  569. {
  570. p = strchr (s, '\n');
  571. if (p == NULL) /* skip bogus entries */
  572. {
  573. while ((c = fgetc (npf)) != EOF && c != '\n')
  574. ;
  575. continue;
  576. }
  577. if (p == s)
  578. continue;
  579. *p = '\0';
  580. np = g_new (struct no_proxy_entry, 1);
  581. np->domain = g_strdup (s);
  582. np->next = NULL;
  583. if (no_proxy)
  584. current->next = np;
  585. else
  586. no_proxy = np;
  587. current = np;
  588. }
  589. fclose (npf);
  590. }
  591. }
  592. g_free (mc_file);
  593. }
  594. /* --------------------------------------------------------------------------------------------- */
  595. /* Return 1 if FTP proxy should be used for this host, 0 otherwise */
  596. static int
  597. ftpfs_check_proxy (const char *host)
  598. {
  599. struct no_proxy_entry *npe;
  600. if (!ftpfs_proxy_host || !*ftpfs_proxy_host || !host || !*host)
  601. return 0; /* sanity check */
  602. if (*host == '!')
  603. return 1;
  604. if (!ftpfs_always_use_proxy)
  605. return 0;
  606. if (!strchr (host, '.'))
  607. return 0;
  608. ftpfs_load_no_proxy_list ();
  609. for (npe = no_proxy; npe; npe = npe->next)
  610. {
  611. char *domain = npe->domain;
  612. if (domain[0] == '.')
  613. {
  614. int ld = strlen (domain);
  615. int lh = strlen (host);
  616. while (ld && lh && host[lh - 1] == domain[ld - 1])
  617. {
  618. ld--;
  619. lh--;
  620. }
  621. if (!ld)
  622. return 0;
  623. }
  624. else if (g_ascii_strcasecmp (host, domain) == 0)
  625. return 0;
  626. }
  627. return 1;
  628. }
  629. /* --------------------------------------------------------------------------------------------- */
  630. static void
  631. ftpfs_get_proxy_host_and_port (const char *proxy, char **host, int *port)
  632. {
  633. char *user, *dir;
  634. dir = vfs_split_url (proxy, host, &user, port, 0, FTP_COMMAND_PORT, URL_USE_ANONYMOUS);
  635. g_free (user);
  636. g_free (dir);
  637. }
  638. /* --------------------------------------------------------------------------------------------- */
  639. static int
  640. ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super)
  641. {
  642. struct addrinfo hints, *res, *curr_res;
  643. int my_socket = 0;
  644. char *host = NULL;
  645. char *port = NULL;
  646. int tmp_port;
  647. int e;
  648. (void) me;
  649. /* Use a proxy host? */
  650. host = g_strdup (SUP.host);
  651. if (!host || !*host)
  652. {
  653. print_vfs_message (_("ftpfs: Invalid host name."));
  654. ftpfs_errno = EINVAL;
  655. g_free (host);
  656. return -1;
  657. }
  658. /* Hosts to connect to that start with a ! should use proxy */
  659. tmp_port = SUP.port;
  660. if (SUP.proxy)
  661. {
  662. ftpfs_get_proxy_host_and_port (ftpfs_proxy_host, &host, &tmp_port);
  663. }
  664. port = g_strdup_printf ("%hu", (unsigned short) tmp_port);
  665. if (port == NULL)
  666. {
  667. g_free (host);
  668. ftpfs_errno = errno;
  669. return -1;
  670. }
  671. tty_enable_interrupt_key (); /* clear the interrupt flag */
  672. memset (&hints, 0, sizeof (struct addrinfo));
  673. hints.ai_socktype = SOCK_STREAM;
  674. hints.ai_flags = AI_ADDRCONFIG;
  675. e = getaddrinfo (host, port, &hints, &res);
  676. g_free (port);
  677. port = NULL;
  678. if (e != 0)
  679. {
  680. tty_disable_interrupt_key ();
  681. print_vfs_message (_("ftpfs: %s"), gai_strerror (e));
  682. g_free (host);
  683. ftpfs_errno = EINVAL;
  684. return -1;
  685. }
  686. for (curr_res = res; curr_res != NULL; curr_res = curr_res->ai_next)
  687. {
  688. my_socket = socket (curr_res->ai_family, curr_res->ai_socktype, curr_res->ai_protocol);
  689. if (my_socket < 0)
  690. {
  691. if (curr_res->ai_next != NULL)
  692. continue;
  693. tty_disable_interrupt_key ();
  694. print_vfs_message (_("ftpfs: %s"), unix_error_string (errno));
  695. g_free (host);
  696. freeaddrinfo (res);
  697. ftpfs_errno = errno;
  698. return -1;
  699. }
  700. print_vfs_message (_("ftpfs: making connection to %s"), host);
  701. g_free (host);
  702. host = NULL;
  703. if (connect (my_socket, curr_res->ai_addr, curr_res->ai_addrlen) >= 0)
  704. break;
  705. ftpfs_errno = errno;
  706. close (my_socket);
  707. if (errno == EINTR && tty_got_interrupt ())
  708. {
  709. print_vfs_message (_("ftpfs: connection interrupted by user"));
  710. }
  711. else if (res->ai_next == NULL)
  712. {
  713. print_vfs_message (_("ftpfs: connection to server failed: %s"),
  714. unix_error_string (errno));
  715. }
  716. else
  717. {
  718. continue;
  719. }
  720. freeaddrinfo (res);
  721. tty_disable_interrupt_key ();
  722. return -1;
  723. }
  724. freeaddrinfo (res);
  725. tty_disable_interrupt_key ();
  726. return my_socket;
  727. }
  728. /* --------------------------------------------------------------------------------------------- */
  729. static int
  730. ftpfs_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
  731. {
  732. int retry_seconds, count_down;
  733. /* We do not want to use the passive if we are using proxies */
  734. if (SUP.proxy)
  735. SUP.use_passive_connection = ftpfs_use_passive_connections_over_proxy;
  736. retry_seconds = 0;
  737. do
  738. {
  739. SUP.failed_on_login = 0;
  740. SUP.sock = ftpfs_open_socket (me, super);
  741. if (SUP.sock == -1)
  742. return -1;
  743. if (ftpfs_login_server (me, super, NULL))
  744. {
  745. /* Logged in, no need to retry the connection */
  746. break;
  747. }
  748. else
  749. {
  750. if (SUP.failed_on_login)
  751. {
  752. /* Close only the socket descriptor */
  753. close (SUP.sock);
  754. }
  755. else
  756. {
  757. return -1;
  758. }
  759. if (ftpfs_retry_seconds)
  760. {
  761. retry_seconds = ftpfs_retry_seconds;
  762. tty_enable_interrupt_key ();
  763. for (count_down = retry_seconds; count_down; count_down--)
  764. {
  765. print_vfs_message (_("Waiting to retry... %d (Control-C to cancel)"),
  766. count_down);
  767. sleep (1);
  768. if (tty_got_interrupt ())
  769. {
  770. /* ftpfs_errno = E; */
  771. tty_disable_interrupt_key ();
  772. return 0;
  773. }
  774. }
  775. tty_disable_interrupt_key ();
  776. }
  777. }
  778. }
  779. while (retry_seconds);
  780. SUP.cwdir = ftpfs_get_current_directory (me, super);
  781. if (!SUP.cwdir)
  782. SUP.cwdir = g_strdup (PATH_SEP_STR);
  783. return 0;
  784. }
  785. /* --------------------------------------------------------------------------------------------- */
  786. static int
  787. ftpfs_open_archive (struct vfs_class *me, struct vfs_s_super *super,
  788. const char *archive_name, char *op)
  789. {
  790. char *host, *user, *password;
  791. int port;
  792. (void) archive_name;
  793. ftpfs_split_url (strchr (op, ':') + 1, &host, &user, &port, &password);
  794. SUP.host = host;
  795. SUP.user = user;
  796. SUP.port = port;
  797. SUP.cwdir = NULL;
  798. SUP.proxy = 0;
  799. if (ftpfs_check_proxy (host))
  800. SUP.proxy = ftpfs_proxy_host;
  801. SUP.password = password;
  802. SUP.use_passive_connection = ftpfs_use_passive_connections;
  803. SUP.strict = ftpfs_use_unix_list_options ? RFC_AUTODETECT : RFC_STRICT;
  804. SUP.isbinary = TYPE_UNKNOWN;
  805. SUP.remote_is_amiga = 0;
  806. super->name = g_strdup ("/");
  807. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  808. return ftpfs_open_archive_int (me, super);
  809. }
  810. /* --------------------------------------------------------------------------------------------- */
  811. static int
  812. ftpfs_archive_same (struct vfs_class *me, struct vfs_s_super *super,
  813. const char *archive_name, char *op, void *cookie)
  814. {
  815. char *host, *user;
  816. int port;
  817. (void) me;
  818. (void) archive_name;
  819. (void) cookie;
  820. ftpfs_split_url (strchr (op, ':') + 1, &host, &user, &port, 0);
  821. port = ((strcmp (host, SUP.host) == 0) && (strcmp (user, SUP.user) == 0) && (port == SUP.port));
  822. g_free (host);
  823. g_free (user);
  824. return port;
  825. }
  826. /* --------------------------------------------------------------------------------------------- */
  827. /* The returned directory should always contain a trailing slash */
  828. static char *
  829. ftpfs_get_current_directory (struct vfs_class *me, struct vfs_s_super *super)
  830. {
  831. char buf[BUF_8K], *bufp, *bufq;
  832. if (ftpfs_command (me, super, NONE, "PWD") == COMPLETE &&
  833. ftpfs_get_reply (me, SUP.sock, buf, sizeof (buf)) == COMPLETE)
  834. {
  835. bufp = NULL;
  836. for (bufq = buf; *bufq; bufq++)
  837. if (*bufq == '"')
  838. {
  839. if (!bufp)
  840. {
  841. bufp = bufq + 1;
  842. }
  843. else
  844. {
  845. *bufq = 0;
  846. if (*bufp)
  847. {
  848. if (*(bufq - 1) != '/')
  849. {
  850. *bufq++ = '/';
  851. *bufq = 0;
  852. }
  853. if (*bufp == '/')
  854. return g_strdup (bufp);
  855. else
  856. {
  857. /* If the remote server is an Amiga a leading slash
  858. might be missing. MC needs it because it is used
  859. as separator between hostname and path internally. */
  860. return g_strconcat ("/", bufp, (char *) NULL);
  861. }
  862. }
  863. else
  864. {
  865. ftpfs_errno = EIO;
  866. return NULL;
  867. }
  868. }
  869. }
  870. }
  871. ftpfs_errno = EIO;
  872. return NULL;
  873. }
  874. /* --------------------------------------------------------------------------------------------- */
  875. /* Setup Passive PASV FTP connection */
  876. static int
  877. ftpfs_setup_passive_pasv (struct vfs_class *me, struct vfs_s_super *super,
  878. int my_socket, struct sockaddr_storage *sa, socklen_t * salen)
  879. {
  880. char *c;
  881. char n[6];
  882. int xa, xb, xc, xd, xe, xf;
  883. if (ftpfs_command (me, super, WAIT_REPLY | WANT_STRING, "PASV") != COMPLETE)
  884. return 0;
  885. /* Parse remote parameters */
  886. for (c = reply_str + 4; (*c) && (!isdigit ((unsigned char) *c)); c++);
  887. if (!*c)
  888. return 0;
  889. if (!isdigit ((unsigned char) *c))
  890. return 0;
  891. if (sscanf (c, "%d,%d,%d,%d,%d,%d", &xa, &xb, &xc, &xd, &xe, &xf) != 6)
  892. return 0;
  893. n[0] = (unsigned char) xa;
  894. n[1] = (unsigned char) xb;
  895. n[2] = (unsigned char) xc;
  896. n[3] = (unsigned char) xd;
  897. n[4] = (unsigned char) xe;
  898. n[5] = (unsigned char) xf;
  899. memcpy (&(((struct sockaddr_in *) sa)->sin_addr.s_addr), (void *) n, 4);
  900. memcpy (&(((struct sockaddr_in *) sa)->sin_port), (void *) &n[4], 2);
  901. if (connect (my_socket, (struct sockaddr *) sa, *salen) < 0)
  902. return 0;
  903. return 1;
  904. }
  905. /* --------------------------------------------------------------------------------------------- */
  906. /* Setup Passive EPSV FTP connection */
  907. static int
  908. ftpfs_setup_passive_epsv (struct vfs_class *me, struct vfs_s_super *super,
  909. int my_socket, struct sockaddr_storage *sa, socklen_t * salen)
  910. {
  911. char *c;
  912. int port;
  913. if (ftpfs_command (me, super, WAIT_REPLY | WANT_STRING, "EPSV") != COMPLETE)
  914. return 0;
  915. /* (|||<port>|) */
  916. c = strchr (reply_str, '|');
  917. if (c == NULL)
  918. return 0;
  919. if (strlen (c) > 3)
  920. c += 3;
  921. else
  922. return 0;
  923. port = atoi (c);
  924. if (port < 0 || port > 65535)
  925. return 0;
  926. port = htons (port);
  927. switch (sa->ss_family)
  928. {
  929. case AF_INET:
  930. ((struct sockaddr_in *) sa)->sin_port = port;
  931. break;
  932. case AF_INET6:
  933. ((struct sockaddr_in6 *) sa)->sin6_port = port;
  934. break;
  935. }
  936. if (connect (my_socket, (struct sockaddr *) sa, *salen) < 0)
  937. return 0;
  938. return 1;
  939. }
  940. /* --------------------------------------------------------------------------------------------- */
  941. /* Setup Passive ftp connection, we use it for source routed connections */
  942. static int
  943. ftpfs_setup_passive (struct vfs_class *me, struct vfs_s_super *super,
  944. int my_socket, struct sockaddr_storage *sa, socklen_t * salen)
  945. {
  946. /* It's IPV4, so try PASV first, some servers and ALGs get confused by EPSV */
  947. if (sa->ss_family == AF_INET)
  948. {
  949. if (!ftpfs_setup_passive_pasv (me, super, my_socket, sa, salen))
  950. /* An IPV4 FTP server might support EPSV, so if PASV fails we can try EPSV anyway */
  951. if (!ftpfs_setup_passive_epsv (me, super, my_socket, sa, salen))
  952. return 0;
  953. }
  954. /* It's IPV6, so EPSV is our only hope */
  955. else
  956. {
  957. if (!ftpfs_setup_passive_epsv (me, super, my_socket, sa, salen))
  958. return 0;
  959. }
  960. return 1;
  961. }
  962. /* --------------------------------------------------------------------------------------------- */
  963. /* Setup Active PORT or EPRT FTP connection */
  964. static int
  965. ftpfs_setup_active (struct vfs_class *me, struct vfs_s_super *super,
  966. struct sockaddr_storage data_addr, socklen_t data_addrlen)
  967. {
  968. unsigned short int port;
  969. char *addr;
  970. unsigned int af;
  971. switch (data_addr.ss_family)
  972. {
  973. case AF_INET:
  974. af = FTP_INET;
  975. port = ((struct sockaddr_in *) &data_addr)->sin_port;
  976. break;
  977. case AF_INET6:
  978. af = FTP_INET6;
  979. port = ((struct sockaddr_in6 *) &data_addr)->sin6_port;
  980. break;
  981. /* Not implemented */
  982. default:
  983. return 0;
  984. }
  985. addr = g_try_malloc (NI_MAXHOST);
  986. if (addr == NULL)
  987. ERRNOR (ENOMEM, -1);
  988. if (getnameinfo
  989. ((struct sockaddr *) &data_addr, data_addrlen, addr, NI_MAXHOST, NULL, 0,
  990. NI_NUMERICHOST) != 0)
  991. {
  992. g_free (addr);
  993. ERRNOR (EIO, -1);
  994. }
  995. /* If we are talking to an IPV4 server, try PORT, and, only if it fails, go for EPRT */
  996. if (af == FTP_INET)
  997. {
  998. unsigned char *a = (unsigned char *) &((struct sockaddr_in *) &data_addr)->sin_addr;
  999. unsigned char *p = (unsigned char *) &port;
  1000. if (ftpfs_command (me, super, WAIT_REPLY,
  1001. "PORT %u,%u,%u,%u,%u,%u", a[0], a[1], a[2], a[3],
  1002. p[0], p[1]) == COMPLETE)
  1003. {
  1004. g_free (addr);
  1005. return 1;
  1006. }
  1007. }
  1008. /*
  1009. * Converts network MSB first order to host byte order (LSB
  1010. * first on i386). If we do it earlier, we will run into an
  1011. * endianness issue, because the server actually expects to see
  1012. * "PORT A,D,D,R,MSB,LSB" in the PORT command.
  1013. */
  1014. port = ntohs (port);
  1015. /* We are talking to an IPV6 server or PORT failed, so we can try EPRT anyway */
  1016. if (ftpfs_command (me, super, WAIT_REPLY, "EPRT |%u|%s|%hu|", af, addr, port) == COMPLETE)
  1017. {
  1018. g_free (addr);
  1019. return 1;
  1020. }
  1021. g_free (addr);
  1022. return 0;
  1023. }
  1024. /* --------------------------------------------------------------------------------------------- */
  1025. /* Initialize a socket for FTP DATA connection */
  1026. static int
  1027. ftpfs_init_data_socket (struct vfs_class *me, struct vfs_s_super *super,
  1028. struct sockaddr_storage *data_addr, socklen_t * data_addrlen)
  1029. {
  1030. int result;
  1031. memset (data_addr, 0, sizeof (struct sockaddr_storage));
  1032. *data_addrlen = sizeof (struct sockaddr_storage);
  1033. if (SUP.use_passive_connection)
  1034. result = getpeername (SUP.sock, (struct sockaddr *) data_addr, data_addrlen);
  1035. else
  1036. result = getsockname (SUP.sock, (struct sockaddr *) data_addr, data_addrlen);
  1037. if (result == -1)
  1038. return -1;
  1039. switch (data_addr->ss_family)
  1040. {
  1041. case AF_INET:
  1042. ((struct sockaddr_in *) data_addr)->sin_port = 0;
  1043. break;
  1044. case AF_INET6:
  1045. ((struct sockaddr_in6 *) data_addr)->sin6_port = 0;
  1046. break;
  1047. default:
  1048. print_vfs_message (_("ftpfs: invalid address family"));
  1049. ERRNOR (EINVAL, -1);
  1050. }
  1051. result = socket (data_addr->ss_family, SOCK_STREAM, IPPROTO_TCP);
  1052. if (result < 0)
  1053. {
  1054. print_vfs_message (_("ftpfs: could not create socket: %s"), unix_error_string (errno));
  1055. return -1;
  1056. }
  1057. else
  1058. return result;
  1059. }
  1060. /* --------------------------------------------------------------------------------------------- */
  1061. /* Initialize FTP DATA connection */
  1062. static int
  1063. ftpfs_initconn (struct vfs_class *me, struct vfs_s_super *super)
  1064. {
  1065. struct sockaddr_storage data_addr;
  1066. socklen_t data_addrlen;
  1067. /*
  1068. * Don't factor socket initialization out of these conditionals,
  1069. * because ftpfs_init_data_socket initializes it in different way
  1070. * depending on use_passive_connection flag.
  1071. */
  1072. /* Try to establish a passive connection first (if requested) */
  1073. if (SUP.use_passive_connection)
  1074. {
  1075. int data_sock;
  1076. data_sock = ftpfs_init_data_socket (me, super, &data_addr, &data_addrlen);
  1077. if (data_sock < 0)
  1078. return -1;
  1079. if (ftpfs_setup_passive (me, super, data_sock, &data_addr, &data_addrlen))
  1080. return data_sock;
  1081. print_vfs_message (_("ftpfs: could not setup passive mode"));
  1082. SUP.use_passive_connection = 0;
  1083. close (data_sock);
  1084. }
  1085. /* If passive setup is diabled or failed, fallback to active connections */
  1086. if (!SUP.use_passive_connection)
  1087. {
  1088. int data_sock;
  1089. data_sock = ftpfs_init_data_socket (me, super, &data_addr, &data_addrlen);
  1090. if (data_sock < 0)
  1091. return -1;
  1092. if ((bind (data_sock, (struct sockaddr *) &data_addr, data_addrlen) == 0) &&
  1093. (getsockname (data_sock, (struct sockaddr *) &data_addr, &data_addrlen) == 0) &&
  1094. (listen (data_sock, 1) == 0))
  1095. {
  1096. if (ftpfs_setup_active (me, super, data_addr, data_addrlen))
  1097. return data_sock;
  1098. }
  1099. close (data_sock);
  1100. }
  1101. /* Restore the initial value of use_passive_connection (for subsequent retries) */
  1102. SUP.use_passive_connection = SUP.proxy ? ftpfs_use_passive_connections_over_proxy :
  1103. ftpfs_use_passive_connections;
  1104. ftpfs_errno = EIO;
  1105. return -1;
  1106. }
  1107. /* --------------------------------------------------------------------------------------------- */
  1108. static int
  1109. ftpfs_open_data_connection (struct vfs_class *me, struct vfs_s_super *super, const char *cmd,
  1110. const char *remote, int isbinary, int reget)
  1111. {
  1112. struct sockaddr_storage from;
  1113. int s, j, data;
  1114. socklen_t fromlen = sizeof (from);
  1115. s = ftpfs_initconn (me, super);
  1116. if (s == -1)
  1117. return -1;
  1118. if (ftpfs_changetype (me, super, isbinary) == -1)
  1119. return -1;
  1120. if (reget > 0)
  1121. {
  1122. j = ftpfs_command (me, super, WAIT_REPLY, "REST %d", reget);
  1123. if (j != CONTINUE)
  1124. return -1;
  1125. }
  1126. if (remote)
  1127. {
  1128. char *remote_path = ftpfs_translate_path (me, super, remote);
  1129. j = ftpfs_command (me, super, WAIT_REPLY, "%s /%s", cmd,
  1130. /* WarFtpD can't STORE //filename */
  1131. (*remote_path == '/') ? remote_path + 1 : remote_path);
  1132. g_free (remote_path);
  1133. }
  1134. else
  1135. j = ftpfs_command (me, super, WAIT_REPLY, "%s", cmd);
  1136. if (j != PRELIM)
  1137. ERRNOR (EPERM, -1);
  1138. tty_enable_interrupt_key ();
  1139. if (SUP.use_passive_connection)
  1140. data = s;
  1141. else
  1142. {
  1143. data = accept (s, (struct sockaddr *) &from, &fromlen);
  1144. if (data < 0)
  1145. {
  1146. ftpfs_errno = errno;
  1147. close (s);
  1148. return -1;
  1149. }
  1150. close (s);
  1151. }
  1152. tty_disable_interrupt_key ();
  1153. return data;
  1154. }
  1155. /* --------------------------------------------------------------------------------------------- */
  1156. static void
  1157. ftpfs_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
  1158. {
  1159. struct vfs_s_super *super = FH_SUPER;
  1160. static unsigned char const ipbuf[3] = { IAC, IP, IAC };
  1161. fd_set mask;
  1162. char buf[1024];
  1163. int dsock = FH_SOCK;
  1164. FH_SOCK = -1;
  1165. SUP.ctl_connection_busy = 0;
  1166. print_vfs_message (_("ftpfs: aborting transfer."));
  1167. if (send (SUP.sock, ipbuf, sizeof (ipbuf), MSG_OOB) != sizeof (ipbuf))
  1168. {
  1169. print_vfs_message (_("ftpfs: abort error: %s"), unix_error_string (errno));
  1170. if (dsock != -1)
  1171. close (dsock);
  1172. return;
  1173. }
  1174. if (ftpfs_command (me, super, NONE, "%cABOR", DM) != COMPLETE)
  1175. {
  1176. print_vfs_message (_("ftpfs: abort failed"));
  1177. if (dsock != -1)
  1178. close (dsock);
  1179. return;
  1180. }
  1181. if (dsock != -1)
  1182. {
  1183. FD_ZERO (&mask);
  1184. FD_SET (dsock, &mask);
  1185. if (select (dsock + 1, &mask, NULL, NULL, NULL) > 0)
  1186. {
  1187. struct timeval start_tim, tim;
  1188. gettimeofday (&start_tim, NULL);
  1189. /* flush the remaining data */
  1190. while (read (dsock, buf, sizeof (buf)) > 0)
  1191. {
  1192. gettimeofday (&tim, NULL);
  1193. if (tim.tv_sec > start_tim.tv_sec + ABORT_TIMEOUT)
  1194. {
  1195. /* server keeps sending, drop the connection and ftpfs_reconnect */
  1196. close (dsock);
  1197. ftpfs_reconnect (me, super);
  1198. return;
  1199. }
  1200. }
  1201. }
  1202. close (dsock);
  1203. }
  1204. if ((ftpfs_get_reply (me, SUP.sock, NULL, 0) == TRANSIENT) && (code == 426))
  1205. ftpfs_get_reply (me, SUP.sock, NULL, 0);
  1206. }
  1207. /* --------------------------------------------------------------------------------------------- */
  1208. #if 0
  1209. static void
  1210. resolve_symlink_without_ls_options (struct vfs_class *me, struct vfs_s_super *super,
  1211. struct vfs_s_inode *dir)
  1212. {
  1213. struct linklist *flist;
  1214. struct direntry *fe, *fel;
  1215. char tmp[MC_MAXPATHLEN];
  1216. int depth;
  1217. dir->symlink_status = FTPFS_RESOLVING_SYMLINKS;
  1218. for (flist = dir->file_list->next; flist != dir->file_list; flist = flist->next)
  1219. {
  1220. /* flist->data->l_stat is alread initialized with 0 */
  1221. fel = flist->data;
  1222. if (S_ISLNK (fel->s.st_mode) && fel->linkname)
  1223. {
  1224. if (fel->linkname[0] == '/')
  1225. {
  1226. if (strlen (fel->linkname) >= MC_MAXPATHLEN)
  1227. continue;
  1228. strcpy (tmp, fel->linkname);
  1229. }
  1230. else
  1231. {
  1232. if ((strlen (dir->remote_path) + strlen (fel->linkname)) >= MC_MAXPATHLEN)
  1233. continue;
  1234. strcpy (tmp, dir->remote_path);
  1235. if (tmp[1] != '\0')
  1236. strcat (tmp, "/");
  1237. strcat (tmp + 1, fel->linkname);
  1238. }
  1239. for (depth = 0; depth < 100; depth++)
  1240. { /* depth protects against recursive symbolic links */
  1241. canonicalize_pathname (tmp);
  1242. fe = _get_file_entry (bucket, tmp, 0, 0);
  1243. if (fe)
  1244. {
  1245. if (S_ISLNK (fe->s.st_mode) && fe->l_stat == 0)
  1246. {
  1247. /* Symlink points to link which isn't resolved, yet. */
  1248. if (fe->linkname[0] == '/')
  1249. {
  1250. if (strlen (fe->linkname) >= MC_MAXPATHLEN)
  1251. break;
  1252. strcpy (tmp, fe->linkname);
  1253. }
  1254. else
  1255. {
  1256. /* at this point tmp looks always like this
  1257. /directory/filename, i.e. no need to check
  1258. strrchr's return value */
  1259. *(strrchr (tmp, '/') + 1) = '\0'; /* dirname */
  1260. if ((strlen (tmp) + strlen (fe->linkname)) >= MC_MAXPATHLEN)
  1261. break;
  1262. strcat (tmp, fe->linkname);
  1263. }
  1264. continue;
  1265. }
  1266. else
  1267. {
  1268. fel->l_stat = g_new (struct stat, 1);
  1269. if (S_ISLNK (fe->s.st_mode))
  1270. *fel->l_stat = *fe->l_stat;
  1271. else
  1272. *fel->l_stat = fe->s;
  1273. (*fel->l_stat).st_ino = bucket->__inode_counter++;
  1274. }
  1275. }
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. dir->symlink_status = FTPFS_RESOLVED_SYMLINKS;
  1281. }
  1282. /* --------------------------------------------------------------------------------------------- */
  1283. static void
  1284. resolve_symlink_with_ls_options (struct vfs_class *me, struct vfs_s_super *super,
  1285. struct vfs_s_inode *dir)
  1286. {
  1287. char buffer[2048] = "", *filename;
  1288. int sock;
  1289. FILE *fp;
  1290. struct stat s;
  1291. struct linklist *flist;
  1292. struct direntry *fe;
  1293. int switch_method = 0;
  1294. dir->symlink_status = FTPFS_RESOLVED_SYMLINKS;
  1295. if (strchr (dir->remote_path, ' '))
  1296. {
  1297. if (ftpfs_chdir_internal (bucket, dir->remote_path) != COMPLETE)
  1298. {
  1299. print_vfs_message (_("ftpfs: CWD failed."));
  1300. return;
  1301. }
  1302. sock = ftpfs_open_data_connection (bucket, "LIST -lLa", ".", TYPE_ASCII, 0);
  1303. }
  1304. else
  1305. sock = ftpfs_open_data_connection (bucket, "LIST -lLa", dir->remote_path, TYPE_ASCII, 0);
  1306. if (sock == -1)
  1307. {
  1308. print_vfs_message (_("ftpfs: couldn't resolve symlink"));
  1309. return;
  1310. }
  1311. fp = fdopen (sock, "r");
  1312. if (fp == NULL)
  1313. {
  1314. close (sock);
  1315. print_vfs_message (_("ftpfs: couldn't resolve symlink"));
  1316. return;
  1317. }
  1318. tty_enable_interrupt_key ();
  1319. flist = dir->file_list->next;
  1320. while (1)
  1321. {
  1322. do
  1323. {
  1324. if (flist == dir->file_list)
  1325. goto done;
  1326. fe = flist->data;
  1327. flist = flist->next;
  1328. }
  1329. while (!S_ISLNK (fe->s.st_mode));
  1330. while (1)
  1331. {
  1332. if (fgets (buffer, sizeof (buffer), fp) == NULL)
  1333. goto done;
  1334. if (MEDATA->logfile)
  1335. {
  1336. fputs (buffer, MEDATA->logfile);
  1337. fflush (MEDATA->logfile);
  1338. }
  1339. vfs_die ("This code should be commented out\n");
  1340. if (vfs_parse_ls_lga (buffer, &s, &filename, NULL))
  1341. {
  1342. int r = strcmp (fe->name, filename);
  1343. g_free (filename);
  1344. if (r == 0)
  1345. {
  1346. if (S_ISLNK (s.st_mode))
  1347. {
  1348. /* This server doesn't understand LIST -lLa */
  1349. switch_method = 1;
  1350. goto done;
  1351. }
  1352. fe->l_stat = g_new (struct stat, 1);
  1353. if (fe->l_stat == NULL)
  1354. goto done;
  1355. *fe->l_stat = s;
  1356. (*fe->l_stat).st_ino = bucket->__inode_counter++;
  1357. break;
  1358. }
  1359. if (r < 0)
  1360. break;
  1361. }
  1362. }
  1363. }
  1364. done:
  1365. while (fgets (buffer, sizeof (buffer), fp) != NULL);
  1366. tty_disable_interrupt_key ();
  1367. fclose (fp);
  1368. ftpfs_get_reply (me, SUP.sock, NULL, 0);
  1369. }
  1370. /* --------------------------------------------------------------------------------------------- */
  1371. static void
  1372. resolve_symlink (struct vfs_class *me, struct vfs_s_super *super, struct vfs_s_inode *dir)
  1373. {
  1374. print_vfs_message (_("Resolving symlink..."));
  1375. if (SUP.strict_rfc959_list_cmd)
  1376. resolve_symlink_without_ls_options (me, super, dir);
  1377. else
  1378. resolve_symlink_with_ls_options (me, super, dir);
  1379. }
  1380. #endif
  1381. /* --------------------------------------------------------------------------------------------- */
  1382. static int
  1383. ftpfs_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
  1384. {
  1385. struct vfs_s_entry *ent;
  1386. struct vfs_s_super *super = dir->super;
  1387. int sock, num_entries = 0;
  1388. char lc_buffer[BUF_8K];
  1389. int cd_first;
  1390. cd_first = ftpfs_first_cd_then_ls || (SUP.strict == RFC_STRICT)
  1391. || (strchr (remote_path, ' ') != NULL);
  1392. again:
  1393. print_vfs_message (_("ftpfs: Reading FTP directory %s... %s%s"),
  1394. remote_path,
  1395. SUP.strict ==
  1396. RFC_STRICT ? _("(strict rfc959)") : "", cd_first ? _("(chdir first)") : "");
  1397. if (cd_first)
  1398. {
  1399. if (ftpfs_chdir_internal (me, super, remote_path) != COMPLETE)
  1400. {
  1401. ftpfs_errno = ENOENT;
  1402. print_vfs_message (_("ftpfs: CWD failed."));
  1403. return -1;
  1404. }
  1405. }
  1406. gettimeofday (&dir->timestamp, NULL);
  1407. dir->timestamp.tv_sec += ftpfs_directory_timeout;
  1408. if (SUP.strict == RFC_STRICT)
  1409. sock = ftpfs_open_data_connection (me, super, "LIST", 0, TYPE_ASCII, 0);
  1410. else if (cd_first)
  1411. /* Dirty hack to avoid autoprepending / to . */
  1412. /* Wu-ftpd produces strange output for '/' if 'LIST -la .' used */
  1413. sock = ftpfs_open_data_connection (me, super, "LIST -la", 0, TYPE_ASCII, 0);
  1414. else
  1415. {
  1416. /* Trailing "/." is necessary if remote_path is a symlink */
  1417. char *path = concat_dir_and_file (remote_path, ".");
  1418. sock = ftpfs_open_data_connection (me, super, "LIST -la", path, TYPE_ASCII, 0);
  1419. g_free (path);
  1420. }
  1421. if (sock == -1)
  1422. goto fallback;
  1423. /* Clear the interrupt flag */
  1424. tty_enable_interrupt_key ();
  1425. while (1)
  1426. {
  1427. int i;
  1428. int res = vfs_s_get_line_interruptible (me, lc_buffer, sizeof (lc_buffer),
  1429. sock);
  1430. if (!res)
  1431. break;
  1432. if (res == EINTR)
  1433. {
  1434. me->verrno = ECONNRESET;
  1435. close (sock);
  1436. tty_disable_interrupt_key ();
  1437. ftpfs_get_reply (me, SUP.sock, NULL, 0);
  1438. print_vfs_message (_("%s: failure"), me->name);
  1439. return -1;
  1440. }
  1441. if (MEDATA->logfile)
  1442. {
  1443. fputs (lc_buffer, MEDATA->logfile);
  1444. fputs ("\n", MEDATA->logfile);
  1445. fflush (MEDATA->logfile);
  1446. }
  1447. ent = vfs_s_generate_entry (me, NULL, dir, 0);
  1448. i = ent->ino->st.st_nlink;
  1449. if (!vfs_parse_ls_lga (lc_buffer, &ent->ino->st, &ent->name, &ent->ino->linkname))
  1450. {
  1451. vfs_s_free_entry (me, ent);
  1452. continue;
  1453. }
  1454. ent->ino->st.st_nlink = i; /* Ouch, we need to preserve our counts :-( */
  1455. num_entries++;
  1456. vfs_s_insert_entry (me, dir, ent);
  1457. }
  1458. close (sock);
  1459. me->verrno = E_REMOTE;
  1460. if ((ftpfs_get_reply (me, SUP.sock, NULL, 0) != COMPLETE))
  1461. goto fallback;
  1462. if (num_entries == 0 && cd_first == 0)
  1463. {
  1464. /* The LIST command may produce an empty output. In such scenario
  1465. it is not clear whether this is caused by `remote_path' being
  1466. a non-existent path or for some other reason (listing emtpy
  1467. directory without the -a option, non-readable directory, etc.).
  1468. Since `dir_load' is a crucial method, when it comes to determine
  1469. whether a given path is a _directory_, the code must try its best
  1470. to determine the type of `remote_path'. The only reliable way to
  1471. achieve this is trough issuing a CWD command. */
  1472. cd_first = 1;
  1473. goto again;
  1474. }
  1475. if (SUP.strict == RFC_AUTODETECT)
  1476. SUP.strict = RFC_DARING;
  1477. print_vfs_message (_("%s: done."), me->name);
  1478. return 0;
  1479. fallback:
  1480. if (SUP.strict == RFC_AUTODETECT)
  1481. {
  1482. /* It's our first attempt to get a directory listing from this
  1483. server (UNIX style LIST command) */
  1484. SUP.strict = RFC_STRICT;
  1485. /* I hate goto, but recursive call needs another 8K on stack */
  1486. /* return ftpfs_dir_load (me, dir, remote_path); */
  1487. cd_first = 1;
  1488. goto again;
  1489. }
  1490. print_vfs_message (_("ftpfs: failed; nowhere to fallback to"));
  1491. ERRNOR (EACCES, -1);
  1492. }
  1493. /* --------------------------------------------------------------------------------------------- */
  1494. static int
  1495. ftpfs_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
  1496. {
  1497. int h, sock, n_read, n_written;
  1498. off_t n_stored;
  1499. #ifdef HAVE_STRUCT_LINGER_L_LINGER
  1500. struct linger li;
  1501. #else
  1502. int flag_one = 1;
  1503. #endif
  1504. char lc_buffer[8192];
  1505. struct stat s;
  1506. char *w_buf;
  1507. struct vfs_s_super *super = FH_SUPER;
  1508. h = open (localname, O_RDONLY);
  1509. if (h == -1)
  1510. ERRNOR (EIO, -1);
  1511. sock =
  1512. ftpfs_open_data_connection (me, super,
  1513. fh->u.ftp.append ? "APPE" : "STOR", name, TYPE_BINARY, 0);
  1514. if (sock < 0 || fstat (h, &s) == -1)
  1515. {
  1516. close (h);
  1517. return -1;
  1518. }
  1519. #ifdef HAVE_STRUCT_LINGER_L_LINGER
  1520. li.l_onoff = 1;
  1521. li.l_linger = 120;
  1522. setsockopt (sock, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof (li));
  1523. #else
  1524. setsockopt (sock, SOL_SOCKET, SO_LINGER, &flag_one, sizeof (flag_one));
  1525. #endif
  1526. n_stored = 0;
  1527. tty_enable_interrupt_key ();
  1528. while (1)
  1529. {
  1530. while ((n_read = read (h, lc_buffer, sizeof (lc_buffer))) == -1)
  1531. {
  1532. if (errno == EINTR)
  1533. {
  1534. if (tty_got_interrupt ())
  1535. {
  1536. ftpfs_errno = EINTR;
  1537. goto error_return;
  1538. }
  1539. else
  1540. continue;
  1541. }
  1542. ftpfs_errno = errno;
  1543. goto error_return;
  1544. }
  1545. if (n_read == 0)
  1546. break;
  1547. n_stored += n_read;
  1548. w_buf = lc_buffer;
  1549. while ((n_written = write (sock, w_buf, n_read)) != n_read)
  1550. {
  1551. if (n_written == -1)
  1552. {
  1553. if (errno == EINTR && !tty_got_interrupt ())
  1554. {
  1555. continue;
  1556. }
  1557. ftpfs_errno = errno;
  1558. goto error_return;
  1559. }
  1560. w_buf += n_written;
  1561. n_read -= n_written;
  1562. }
  1563. print_vfs_message (_("ftpfs: storing file %ju (%ju)"),
  1564. (uintmax_t) n_stored, (uintmax_t) s.st_size);
  1565. }
  1566. tty_disable_interrupt_key ();
  1567. close (sock);
  1568. close (h);
  1569. if (ftpfs_get_reply (me, SUP.sock, NULL, 0) != COMPLETE)
  1570. ERRNOR (EIO, -1);
  1571. return 0;
  1572. error_return:
  1573. tty_disable_interrupt_key ();
  1574. close (sock);
  1575. close (h);
  1576. ftpfs_get_reply (me, SUP.sock, NULL, 0);
  1577. return -1;
  1578. }
  1579. /* --------------------------------------------------------------------------------------------- */
  1580. static int
  1581. ftpfs_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
  1582. {
  1583. char *name = vfs_s_fullpath (me, fh->ino);
  1584. if (!name)
  1585. return 0;
  1586. FH_SOCK = ftpfs_open_data_connection (me, FH_SUPER, "RETR", name, TYPE_BINARY, offset);
  1587. g_free (name);
  1588. if (FH_SOCK == -1)
  1589. ERRNOR (EACCES, 0);
  1590. fh->linear = LS_LINEAR_OPEN;
  1591. FH_SUPER->u.ftp.ctl_connection_busy = 1;
  1592. fh->u.ftp.append = 0;
  1593. return 1;
  1594. }
  1595. /* --------------------------------------------------------------------------------------------- */
  1596. static int
  1597. ftpfs_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t len)
  1598. {
  1599. ssize_t n;
  1600. struct vfs_s_super *super = FH_SUPER;
  1601. while ((n = read (FH_SOCK, buf, len)) < 0)
  1602. {
  1603. if ((errno == EINTR) && !tty_got_interrupt ())
  1604. continue;
  1605. break;
  1606. }
  1607. if (n < 0)
  1608. ftpfs_linear_abort (me, fh);
  1609. if (n == 0)
  1610. {
  1611. SUP.ctl_connection_busy = 0;
  1612. close (FH_SOCK);
  1613. FH_SOCK = -1;
  1614. if ((ftpfs_get_reply (me, SUP.sock, NULL, 0) != COMPLETE))
  1615. ERRNOR (E_REMOTE, -1);
  1616. return 0;
  1617. }
  1618. ERRNOR (errno, n);
  1619. }
  1620. /* --------------------------------------------------------------------------------------------- */
  1621. static void
  1622. ftpfs_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
  1623. {
  1624. if (FH_SOCK != -1)
  1625. ftpfs_linear_abort (me, fh);
  1626. }
  1627. /* --------------------------------------------------------------------------------------------- */
  1628. static int
  1629. ftpfs_ctl (void *fh, int ctlop, void *arg)
  1630. {
  1631. (void) arg;
  1632. switch (ctlop)
  1633. {
  1634. case VFS_CTL_IS_NOTREADY:
  1635. {
  1636. int v;
  1637. if (!FH->linear)
  1638. vfs_die ("You may not do this");
  1639. if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
  1640. return 0;
  1641. v = vfs_s_select_on_two (FH->u.ftp.sock, 0);
  1642. if (((v < 0) && (errno == EINTR)) || v == 0)
  1643. return 1;
  1644. return 0;
  1645. }
  1646. default:
  1647. return 0;
  1648. }
  1649. }
  1650. /* --------------------------------------------------------------------------------------------- */
  1651. static int
  1652. ftpfs_send_command (struct vfs_class *me, const char *filename, const char *cmd, int flags)
  1653. {
  1654. const char *rpath;
  1655. char *p, *mpath = g_strdup (filename);
  1656. struct vfs_s_super *super;
  1657. int r;
  1658. int flush_directory_cache = (flags & OPT_FLUSH);
  1659. rpath = vfs_s_get_path_mangle (me, mpath, &super, 0);
  1660. if (rpath == NULL)
  1661. {
  1662. g_free (mpath);
  1663. return -1;
  1664. }
  1665. p = ftpfs_translate_path (me, super, rpath);
  1666. r = ftpfs_command (me, super, WAIT_REPLY, cmd, p);
  1667. g_free (p);
  1668. vfs_stamp_create (&vfs_ftpfs_ops, super);
  1669. if (flags & OPT_IGNORE_ERROR)
  1670. r = COMPLETE;
  1671. if (r != COMPLETE)
  1672. {
  1673. me->verrno = EPERM;
  1674. g_free (mpath);
  1675. return -1;
  1676. }
  1677. if (flush_directory_cache)
  1678. vfs_s_invalidate (me, super);
  1679. g_free (mpath);
  1680. return 0;
  1681. }
  1682. /* --------------------------------------------------------------------------------------------- */
  1683. static int
  1684. ftpfs_chmod (struct vfs_class *me, const char *path, int mode)
  1685. {
  1686. char buf[BUF_SMALL];
  1687. int ret;
  1688. g_snprintf (buf, sizeof (buf), "SITE CHMOD %4.4o /%%s", mode & 07777);
  1689. ret = ftpfs_send_command (me, path, buf, OPT_FLUSH);
  1690. return ftpfs_ignore_chattr_errors ? 0 : ret;
  1691. }
  1692. /* --------------------------------------------------------------------------------------------- */
  1693. static int
  1694. ftpfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group)
  1695. {
  1696. #if 0
  1697. ftpfs_errno = EPERM;
  1698. return -1;
  1699. #else
  1700. /* Everyone knows it is not possible to chown remotely, so why bother them.
  1701. If someone's root, then copy/move will always try to chown it... */
  1702. (void) me;
  1703. (void) path;
  1704. (void) owner;
  1705. (void) group;
  1706. return 0;
  1707. #endif
  1708. }
  1709. /* --------------------------------------------------------------------------------------------- */
  1710. static int
  1711. ftpfs_unlink (struct vfs_class *me, const char *path)
  1712. {
  1713. return ftpfs_send_command (me, path, "DELE /%s", OPT_FLUSH);
  1714. }
  1715. /* --------------------------------------------------------------------------------------------- */
  1716. /* Return 1 if path is the same directory as the one we are in now */
  1717. static int
  1718. ftpfs_is_same_dir (struct vfs_class *me, struct vfs_s_super *super, const char *path)
  1719. {
  1720. (void) me;
  1721. if (!SUP.cwdir)
  1722. return 0;
  1723. if (strcmp (path, SUP.cwdir) == 0)
  1724. return 1;
  1725. return 0;
  1726. }
  1727. /* --------------------------------------------------------------------------------------------- */
  1728. static int
  1729. ftpfs_chdir_internal (struct vfs_class *me, struct vfs_s_super *super, const char *remote_path)
  1730. {
  1731. int r;
  1732. char *p;
  1733. if (!SUP.cwd_deferred && ftpfs_is_same_dir (me, super, remote_path))
  1734. return COMPLETE;
  1735. p = ftpfs_translate_path (me, super, remote_path);
  1736. r = ftpfs_command (me, super, WAIT_REPLY, "CWD /%s", p);
  1737. g_free (p);
  1738. if (r != COMPLETE)
  1739. {
  1740. ftpfs_errno = EIO;
  1741. }
  1742. else
  1743. {
  1744. g_free (SUP.cwdir);
  1745. SUP.cwdir = g_strdup (remote_path);
  1746. SUP.cwd_deferred = 0;
  1747. }
  1748. return r;
  1749. }
  1750. /* --------------------------------------------------------------------------------------------- */
  1751. static int
  1752. ftpfs_rename (struct vfs_class *me, const char *path1, const char *path2)
  1753. {
  1754. ftpfs_send_command (me, path1, "RNFR /%s", OPT_FLUSH);
  1755. return ftpfs_send_command (me, path2, "RNTO /%s", OPT_FLUSH);
  1756. }
  1757. /* --------------------------------------------------------------------------------------------- */
  1758. static int
  1759. ftpfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  1760. {
  1761. (void) mode; /* FIXME: should be used */
  1762. return ftpfs_send_command (me, path, "MKD /%s", OPT_FLUSH);
  1763. }
  1764. /* --------------------------------------------------------------------------------------------- */
  1765. static int
  1766. ftpfs_rmdir (struct vfs_class *me, const char *path)
  1767. {
  1768. return ftpfs_send_command (me, path, "RMD /%s", OPT_FLUSH);
  1769. }
  1770. /* --------------------------------------------------------------------------------------------- */
  1771. static int
  1772. ftpfs_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode)
  1773. {
  1774. (void) mode;
  1775. fh->u.ftp.append = 0;
  1776. /* File will be written only, so no need to retrieve it from ftp server */
  1777. if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
  1778. {
  1779. #ifdef HAVE_STRUCT_LINGER_L_LINGER
  1780. struct linger li;
  1781. #else
  1782. int li = 1;
  1783. #endif
  1784. char *name;
  1785. /* ftpfs_linear_start() called, so data will be written
  1786. * to local temporary file and stored to ftp server
  1787. * by vfs_s_close later
  1788. */
  1789. if (FH_SUPER->u.ftp.ctl_connection_busy)
  1790. {
  1791. if (!fh->ino->localname)
  1792. {
  1793. int handle = vfs_mkstemps (&fh->ino->localname, me->name,
  1794. fh->ino->ent->name);
  1795. if (handle == -1)
  1796. return -1;
  1797. close (handle);
  1798. fh->u.ftp.append = flags & O_APPEND;
  1799. }
  1800. return 0;
  1801. }
  1802. name = vfs_s_fullpath (me, fh->ino);
  1803. if (!name)
  1804. return -1;
  1805. fh->handle =
  1806. ftpfs_open_data_connection (me, fh->ino->super,
  1807. (flags & O_APPEND) ? "APPE" : "STOR", name, TYPE_BINARY, 0);
  1808. g_free (name);
  1809. if (fh->handle < 0)
  1810. return -1;
  1811. #ifdef HAVE_STRUCT_LINGER_L_LINGER
  1812. li.l_onoff = 1;
  1813. li.l_linger = 120;
  1814. #endif
  1815. setsockopt (fh->handle, SOL_SOCKET, SO_LINGER, &li, sizeof (li));
  1816. if (fh->ino->localname)
  1817. {
  1818. unlink (fh->ino->localname);
  1819. g_free (fh->ino->localname);
  1820. fh->ino->localname = NULL;
  1821. }
  1822. return 0;
  1823. }
  1824. if (!fh->ino->localname)
  1825. if (vfs_s_retrieve_file (me, fh->ino) == -1)
  1826. return -1;
  1827. if (!fh->ino->localname)
  1828. vfs_die ("retrieve_file failed to fill in localname");
  1829. return 0;
  1830. }
  1831. /* --------------------------------------------------------------------------------------------- */
  1832. static int
  1833. ftpfs_fh_close (struct vfs_class *me, struct vfs_s_fh *fh)
  1834. {
  1835. if (fh->handle != -1 && !fh->ino->localname)
  1836. {
  1837. close (fh->handle);
  1838. fh->handle = -1;
  1839. /* File is stored to destination already, so
  1840. * we prevent MEDATA->ftpfs_file_store() call from vfs_s_close ()
  1841. */
  1842. fh->changed = 0;
  1843. if (ftpfs_get_reply (me, fh->ino->SUP.sock, NULL, 0) != COMPLETE)
  1844. ERRNOR (EIO, -1);
  1845. vfs_s_invalidate (me, FH_SUPER);
  1846. }
  1847. return 0;
  1848. }
  1849. /* --------------------------------------------------------------------------------------------- */
  1850. static void
  1851. ftpfs_done (struct vfs_class *me)
  1852. {
  1853. struct no_proxy_entry *np;
  1854. (void) me;
  1855. while (no_proxy)
  1856. {
  1857. np = no_proxy->next;
  1858. g_free (no_proxy->domain);
  1859. g_free (no_proxy);
  1860. no_proxy = np;
  1861. }
  1862. g_free (ftpfs_anonymous_passwd);
  1863. g_free (ftpfs_proxy_host);
  1864. }
  1865. /* --------------------------------------------------------------------------------------------- */
  1866. static void
  1867. ftpfs_fill_names (struct vfs_class *me, fill_names_f func)
  1868. {
  1869. struct vfs_s_super *super = MEDATA->supers;
  1870. char *name;
  1871. while (super)
  1872. {
  1873. name = g_strconcat ("/#ftp:", SUP.user, "@", SUP.host, "/", SUP.cwdir, (char *) NULL);
  1874. (*func) (name);
  1875. g_free (name);
  1876. super = super->next;
  1877. }
  1878. }
  1879. /* --------------------------------------------------------------------------------------------- */
  1880. static keyword_t
  1881. ftpfs_netrc_next (void)
  1882. {
  1883. char *p;
  1884. keyword_t i;
  1885. static const char *const keywords[] = { "default", "machine",
  1886. "login", "password", "passwd", "account", "macdef", NULL
  1887. };
  1888. while (1)
  1889. {
  1890. netrcp = skip_separators (netrcp);
  1891. if (*netrcp != '\n')
  1892. break;
  1893. netrcp++;
  1894. }
  1895. if (!*netrcp)
  1896. return NETRC_NONE;
  1897. p = buffer;
  1898. if (*netrcp == '"')
  1899. {
  1900. for (netrcp++; *netrcp != '"' && *netrcp; netrcp++)
  1901. {
  1902. if (*netrcp == '\\')
  1903. netrcp++;
  1904. *p++ = *netrcp;
  1905. }
  1906. }
  1907. else
  1908. {
  1909. for (; *netrcp != '\n' && *netrcp != '\t' && *netrcp != ' ' &&
  1910. *netrcp != ',' && *netrcp; netrcp++)
  1911. {
  1912. if (*netrcp == '\\')
  1913. netrcp++;
  1914. *p++ = *netrcp;
  1915. }
  1916. }
  1917. *p = 0;
  1918. if (!*buffer)
  1919. return NETRC_NONE;
  1920. i = NETRC_DEFAULT;
  1921. while (keywords[i - 1])
  1922. {
  1923. if (!strcmp (keywords[i - 1], buffer))
  1924. return i;
  1925. i++;
  1926. }
  1927. return NETRC_UNKNOWN;
  1928. }
  1929. /* --------------------------------------------------------------------------------------------- */
  1930. static int
  1931. ftpfs_netrc_bad_mode (const char *netrcname)
  1932. {
  1933. static int be_angry = 1;
  1934. struct stat mystat;
  1935. if (stat (netrcname, &mystat) >= 0 && (mystat.st_mode & 077))
  1936. {
  1937. if (be_angry)
  1938. {
  1939. message (D_ERROR, MSG_ERROR,
  1940. _("~/.netrc file has incorrect mode\nRemove password or correct mode"));
  1941. be_angry = 0;
  1942. }
  1943. return 1;
  1944. }
  1945. return 0;
  1946. }
  1947. /* --------------------------------------------------------------------------------------------- */
  1948. /* Scan .netrc until we find matching "machine" or "default"
  1949. * domain is used for additional matching
  1950. * No search is done after "default" in compliance with "man netrc"
  1951. * Return 0 if found, -1 otherwise */
  1952. static int
  1953. ftpfs_find_machine (const char *host, const char *domain)
  1954. {
  1955. keyword_t keyword;
  1956. if (!host)
  1957. host = "";
  1958. if (!domain)
  1959. domain = "";
  1960. while ((keyword = ftpfs_netrc_next ()) != NETRC_NONE)
  1961. {
  1962. if (keyword == NETRC_DEFAULT)
  1963. return 0;
  1964. if (keyword == NETRC_MACDEF)
  1965. {
  1966. /* Scan for an empty line, which concludes "macdef" */
  1967. do
  1968. {
  1969. while (*netrcp && *netrcp != '\n')
  1970. netrcp++;
  1971. if (*netrcp != '\n')
  1972. break;
  1973. netrcp++;
  1974. }
  1975. while (*netrcp && *netrcp != '\n');
  1976. continue;
  1977. }
  1978. if (keyword != NETRC_MACHINE)
  1979. continue;
  1980. /* Take machine name */
  1981. if (ftpfs_netrc_next () == NETRC_NONE)
  1982. break;
  1983. if (g_ascii_strcasecmp (host, buffer) != 0)
  1984. {
  1985. /* Try adding our domain to short names in .netrc */
  1986. const char *host_domain = strchr (host, '.');
  1987. if (!host_domain)
  1988. continue;
  1989. /* Compare domain part */
  1990. if (g_ascii_strcasecmp (host_domain, domain) != 0)
  1991. continue;
  1992. /* Compare local part */
  1993. if (g_ascii_strncasecmp (host, buffer, host_domain - host) != 0)
  1994. continue;
  1995. }
  1996. return 0;
  1997. }
  1998. /* end of .netrc */
  1999. return -1;
  2000. }
  2001. /* --------------------------------------------------------------------------------------------- */
  2002. /* Extract login and password from .netrc for the host.
  2003. * pass may be NULL.
  2004. * Returns 0 for success, -1 for error */
  2005. static int
  2006. ftpfs_netrc_lookup (const char *host, char **login, char **pass)
  2007. {
  2008. char *netrcname;
  2009. char *tmp_pass = NULL;
  2010. char hostname[MAXHOSTNAMELEN];
  2011. const char *domain;
  2012. keyword_t keyword;
  2013. static struct rupcache
  2014. {
  2015. struct rupcache *next;
  2016. char *host;
  2017. char *login;
  2018. char *pass;
  2019. } *rup_cache = NULL, *rupp;
  2020. /* Initialize *login and *pass */
  2021. if (!login)
  2022. return 0;
  2023. *login = NULL;
  2024. if (pass)
  2025. *pass = NULL;
  2026. /* Look up in the cache first */
  2027. for (rupp = rup_cache; rupp != NULL; rupp = rupp->next)
  2028. {
  2029. if (!strcmp (host, rupp->host))
  2030. {
  2031. if (rupp->login)
  2032. *login = g_strdup (rupp->login);
  2033. if (pass && rupp->pass)
  2034. *pass = g_strdup (rupp->pass);
  2035. return 0;
  2036. }
  2037. }
  2038. /* Load current .netrc */
  2039. netrcname = g_build_filename (home_dir, ".netrc", (char *) NULL);
  2040. if (!g_file_get_contents (netrcname, &netrc, NULL, NULL))
  2041. {
  2042. g_free (netrcname);
  2043. return 0;
  2044. }
  2045. netrcp = netrc;
  2046. /* Find our own domain name */
  2047. if (gethostname (hostname, sizeof (hostname)) < 0)
  2048. *hostname = '\0';
  2049. domain = strchr (hostname, '.');
  2050. if (domain == NULL)
  2051. domain = "";
  2052. /* Scan for "default" and matching "machine" keywords */
  2053. ftpfs_find_machine (host, domain);
  2054. /* Scan for keywords following "default" and "machine" */
  2055. while (1)
  2056. {
  2057. int need_break = 0;
  2058. keyword = ftpfs_netrc_next ();
  2059. switch (keyword)
  2060. {
  2061. case NETRC_LOGIN:
  2062. if (ftpfs_netrc_next () == NETRC_NONE)
  2063. {
  2064. need_break = 1;
  2065. break;
  2066. }
  2067. /* We have another name already - should not happen */
  2068. if (*login)
  2069. {
  2070. need_break = 1;
  2071. break;
  2072. }
  2073. /* We have login name now */
  2074. *login = g_strdup (buffer);
  2075. break;
  2076. case NETRC_PASSWORD:
  2077. case NETRC_PASSWD:
  2078. if (ftpfs_netrc_next () == NETRC_NONE)
  2079. {
  2080. need_break = 1;
  2081. break;
  2082. }
  2083. /* Ignore unsafe passwords */
  2084. if (strcmp (*login, "anonymous") && strcmp (*login, "ftp")
  2085. && ftpfs_netrc_bad_mode (netrcname))
  2086. {
  2087. need_break = 1;
  2088. break;
  2089. }
  2090. /* Remember password. pass may be NULL, so use tmp_pass */
  2091. if (tmp_pass == NULL)
  2092. tmp_pass = g_strdup (buffer);
  2093. break;
  2094. case NETRC_ACCOUNT:
  2095. /* "account" is followed by a token which we ignore */
  2096. if (ftpfs_netrc_next () == NETRC_NONE)
  2097. {
  2098. need_break = 1;
  2099. break;
  2100. }
  2101. /* Ignore account, but warn user anyways */
  2102. ftpfs_netrc_bad_mode (netrcname);
  2103. break;
  2104. default:
  2105. /* Unexpected keyword or end of file */
  2106. need_break = 1;
  2107. break;
  2108. }
  2109. if (need_break)
  2110. break;
  2111. }
  2112. g_free (netrc);
  2113. g_free (netrcname);
  2114. rupp = g_new (struct rupcache, 1);
  2115. rupp->host = g_strdup (host);
  2116. rupp->login = rupp->pass = 0;
  2117. if (*login != NULL)
  2118. {
  2119. rupp->login = g_strdup (*login);
  2120. }
  2121. if (tmp_pass != NULL)
  2122. rupp->pass = g_strdup (tmp_pass);
  2123. rupp->next = rup_cache;
  2124. rup_cache = rupp;
  2125. if (pass)
  2126. *pass = tmp_pass;
  2127. return 0;
  2128. }
  2129. /* --------------------------------------------------------------------------------------------- */
  2130. /*** public functions ****************************************************************************/
  2131. /* --------------------------------------------------------------------------------------------- */
  2132. /** This routine is called as the last step in load_setup */
  2133. void
  2134. ftpfs_init_passwd (void)
  2135. {
  2136. ftpfs_anonymous_passwd = load_anon_passwd ();
  2137. if (ftpfs_anonymous_passwd)
  2138. return;
  2139. /* If there is no anonymous ftp password specified
  2140. * then we'll just use anonymous@
  2141. * We don't send any other thing because:
  2142. * - We want to remain anonymous
  2143. * - We want to stop SPAM
  2144. * - We don't want to let ftp sites to discriminate by the user,
  2145. * host or country.
  2146. */
  2147. ftpfs_anonymous_passwd = g_strdup ("anonymous@");
  2148. }
  2149. /* --------------------------------------------------------------------------------------------- */
  2150. void
  2151. init_ftpfs (void)
  2152. {
  2153. static struct vfs_s_subclass ftpfs_subclass;
  2154. tcp_init ();
  2155. ftpfs_subclass.flags = VFS_S_REMOTE;
  2156. ftpfs_subclass.archive_same = ftpfs_archive_same;
  2157. ftpfs_subclass.open_archive = ftpfs_open_archive;
  2158. ftpfs_subclass.free_archive = ftpfs_free_archive;
  2159. ftpfs_subclass.fh_open = ftpfs_fh_open;
  2160. ftpfs_subclass.fh_close = ftpfs_fh_close;
  2161. ftpfs_subclass.dir_load = ftpfs_dir_load;
  2162. ftpfs_subclass.file_store = ftpfs_file_store;
  2163. ftpfs_subclass.linear_start = ftpfs_linear_start;
  2164. ftpfs_subclass.linear_read = ftpfs_linear_read;
  2165. ftpfs_subclass.linear_close = ftpfs_linear_close;
  2166. vfs_s_init_class (&vfs_ftpfs_ops, &ftpfs_subclass);
  2167. vfs_ftpfs_ops.name = "ftpfs";
  2168. vfs_ftpfs_ops.flags = VFSF_NOLINKS;
  2169. vfs_ftpfs_ops.prefix = "ftp:";
  2170. vfs_ftpfs_ops.done = &ftpfs_done;
  2171. vfs_ftpfs_ops.fill_names = ftpfs_fill_names;
  2172. vfs_ftpfs_ops.chmod = ftpfs_chmod;
  2173. vfs_ftpfs_ops.chown = ftpfs_chown;
  2174. vfs_ftpfs_ops.unlink = ftpfs_unlink;
  2175. vfs_ftpfs_ops.rename = ftpfs_rename;
  2176. vfs_ftpfs_ops.mkdir = ftpfs_mkdir;
  2177. vfs_ftpfs_ops.rmdir = ftpfs_rmdir;
  2178. vfs_ftpfs_ops.ctl = ftpfs_ctl;
  2179. vfs_register_class (&vfs_ftpfs_ops);
  2180. }
  2181. /* --------------------------------------------------------------------------------------------- */