ftpfs.c 54 KB

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