ftpfs.c 54 KB

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