ftpfs.c 53 KB

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