ftpfs.c 63 KB

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