ftpfs.c 46 KB

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