ftpfs.c 49 KB

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