ftpfs.c 49 KB

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