ftpfs.c 54 KB

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