ftpfs.c 64 KB

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