ftpfs.c 72 KB

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