ftpfs.c 49 KB

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