ftpfs.c 50 KB

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