mcfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /* Virtual File System: Midnight Commander file system.
  2. Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2007 Free Software Foundation, Inc.
  4. Written by Miguel de Icaza
  5. Andrej Borsenkow
  6. Norbert Warmuth
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License
  9. as published by the Free Software Foundation; either version 2 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /**
  19. * \file
  20. * \brief Source: Midnight Commander file system
  21. * \author Miguel de Icaza
  22. * \author Andrej Borsenkow
  23. * \author Norbert Warmuth
  24. *
  25. * Namespace: exports mcfs_vfs_ops, tcp_invalidate_socket
  26. */
  27. #include <config.h>
  28. #ifdef ENABLE_VFS_MCFS
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <pwd.h>
  36. #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
  37. #include <netdb.h> /* struct hostent */
  38. #include <sys/socket.h> /* AF_INET */
  39. #include <netinet/in.h> /* struct in_addr */
  40. #ifdef HAVE_ARPA_INET_H
  41. #include <arpa/inet.h>
  42. #endif
  43. #ifdef HAVE_PMAP_SET
  44. #include <rpc/rpc.h>
  45. #include <rpc/pmap_prot.h>
  46. #ifdef HAVE_RPC_PMAP_CLNT_H
  47. #include <rpc/pmap_clnt.h>
  48. #endif
  49. #endif
  50. #include "../src/global.h"
  51. #include "../src/wtools.h" /* message() */
  52. #include "../src/main.h" /* print_vfs_message */
  53. #include "utilvfs.h"
  54. #include "vfs.h"
  55. #include "vfs-impl.h"
  56. #include "mcfs.h"
  57. #include "mcfsutil.h"
  58. #include "tcputil.h"
  59. #ifndef INADDR_NONE
  60. # define INADDR_NONE (0xffffffffU)
  61. #endif
  62. #define MCFS_MAX_CONNECTIONS 32
  63. static struct _mcfs_connection {
  64. char *host;
  65. char *user;
  66. char *home;
  67. int sock;
  68. int port;
  69. int version;
  70. } mcfs_connections[MCFS_MAX_CONNECTIONS];
  71. #define mcserver_port 9876
  72. typedef struct _mcfs_connection mcfs_connection;
  73. typedef struct {
  74. int handle;
  75. mcfs_connection *conn;
  76. } mcfs_handle;
  77. static char *mcfs_gethome (mcfs_connection * mc);
  78. static int my_errno;
  79. static struct vfs_class vfs_mcfs_ops;
  80. /* Extract the hostname and username from the path */
  81. /* path is in the form: hostname:user/remote-dir */
  82. static char *
  83. mcfs_get_host_and_username (const char *path, char **host, char **user,
  84. int *port, char **pass)
  85. {
  86. return vfs_split_url (path, host, user, port, pass, 0, 0);
  87. }
  88. static void
  89. mcfs_fill_names (struct vfs_class *me, fill_names_f func)
  90. {
  91. int i;
  92. char *name;
  93. (void) me;
  94. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
  95. if (mcfs_connections[i].host == 0)
  96. continue;
  97. name = g_strconcat ("/#mc:", mcfs_connections[i].user,
  98. "@", mcfs_connections[i].host, (char *) NULL);
  99. (*func) (name);
  100. g_free (name);
  101. }
  102. }
  103. /* This routine checks the server RPC version and logs the user in */
  104. static int
  105. mcfs_login_server (int my_socket, char *user, int port,
  106. int port_autodetected, char *netrcpass, int *version)
  107. {
  108. int result;
  109. char *pass;
  110. /* Send the version number */
  111. rpc_send (my_socket, RPC_INT, *version, RPC_END);
  112. if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
  113. return 0;
  114. if (result != MC_VERSION_OK) {
  115. message (D_ERROR, _(" MCFS "),
  116. _(" The server does not support this version "));
  117. close (my_socket);
  118. return 0;
  119. }
  120. /* FIXME: figure out why last_current_dir used to be passed here */
  121. rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
  122. RPC_STRING, user, RPC_END);
  123. if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
  124. return 0;
  125. if (result == MC_NEED_PASSWORD) {
  126. if (port > 1024 && port_autodetected) {
  127. int v;
  128. v = query_dialog (_("Warning"),
  129. _
  130. (" The remote server is not running on a system port \n"
  131. " you need a password to log in, but the information may \n"
  132. " not be safe on the remote side. Continue? \n"),
  133. D_ERROR, 2, _("&Yes"), _("&No"));
  134. if (v == 1) {
  135. close (my_socket);
  136. return 0;
  137. }
  138. }
  139. if (netrcpass != NULL)
  140. pass = g_strdup (netrcpass);
  141. else
  142. pass = vfs_get_password (_(" MCFS Password required "));
  143. if (!pass) {
  144. rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
  145. close (my_socket);
  146. return 0;
  147. }
  148. rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
  149. wipe_password (pass);
  150. if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
  151. return 0;
  152. if (result != MC_LOGINOK) {
  153. message (D_ERROR, _(" MCFS "), _(" Invalid password "));
  154. rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
  155. close (my_socket);
  156. return 0;
  157. }
  158. }
  159. return my_socket;
  160. }
  161. static int
  162. mcfs_get_remote_port (struct sockaddr_in *sin, int *version)
  163. {
  164. #ifdef HAVE_PMAP_GETMAPS
  165. int port;
  166. struct pmaplist *pl;
  167. *version = 1;
  168. port = mcserver_port;
  169. for (pl = pmap_getmaps (sin); pl; pl = pl->pml_next)
  170. if (pl->pml_map.pm_prog == RPC_PROGNUM
  171. && pl->pml_map.pm_prot == IPPROTO_TCP
  172. && pl->pml_map.pm_vers >= (unsigned long) *version) {
  173. *version = (int) pl->pml_map.pm_vers;
  174. port = pl->pml_map.pm_port;
  175. }
  176. return port;
  177. #else
  178. #ifdef HAVE_PMAP_GETPORT
  179. int port;
  180. for (*version = RPC_PROGVER; *version >= 1; (*version)--)
  181. if (port = pmap_getport (sin, RPC_PROGNUM, *version, IPPROTO_TCP))
  182. return port;
  183. #endif /* HAVE_PMAP_GETPORT */
  184. *version = 1;
  185. return mcserver_port;
  186. #endif /* HAVE_PMAP_GETMAPS */
  187. }
  188. /* This used to be in utilvfs.c, but as it deals with portmapper, it
  189. is probably useful for mcfs */
  190. static int
  191. mcfs_create_tcp_link (const char *host, int *port, int *version, const char *caller)
  192. {
  193. struct sockaddr_in server_address;
  194. unsigned long inaddr;
  195. struct hostent *hp;
  196. int my_socket;
  197. if (!*host)
  198. return 0;
  199. memset ((char *) &server_address, 0, sizeof (server_address));
  200. server_address.sin_family = AF_INET;
  201. /* Try to use the dotted decimal number */
  202. if ((inaddr = inet_addr (host)) != INADDR_NONE)
  203. memcpy ((char *) &server_address.sin_addr, (char *) &inaddr,
  204. sizeof (inaddr));
  205. else {
  206. if ((hp = gethostbyname (host)) == NULL) {
  207. message (D_ERROR, caller, _(" Cannot locate hostname: %s "),
  208. host);
  209. return 0;
  210. }
  211. memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr,
  212. hp->h_length);
  213. }
  214. /* Try to contact a remote portmapper to obtain the listening port */
  215. if (*port == 0) {
  216. *port = mcfs_get_remote_port (&server_address, version);
  217. if (*port < 1)
  218. return 0;
  219. } else
  220. *version = 1;
  221. server_address.sin_port = htons (*port);
  222. if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
  223. message (D_ERROR, caller, _(" Cannot create socket: %s "),
  224. unix_error_string (errno));
  225. return 0;
  226. }
  227. if (connect (my_socket, (struct sockaddr *) &server_address,
  228. sizeof (server_address)) < 0) {
  229. message (D_ERROR, caller, _(" Cannot connect to server: %s "),
  230. unix_error_string (errno));
  231. close (my_socket);
  232. return 0;
  233. }
  234. return my_socket;
  235. }
  236. static int
  237. mcfs_open_tcp_link (char *host, char *user,
  238. int *port, char *netrcpass, int *version)
  239. {
  240. int my_socket;
  241. int old_port = *port;
  242. my_socket = mcfs_create_tcp_link (host, port, version, " MCfs ");
  243. if (my_socket <= 0)
  244. return 0;
  245. /* We got the connection to the server, verify if the server
  246. implements our version of the RPC mechanism and then login
  247. the user.
  248. */
  249. return mcfs_login_server (my_socket, user, *port, old_port == 0,
  250. netrcpass, version);
  251. }
  252. static int mcfs_get_free_bucket_init = 1;
  253. static mcfs_connection *
  254. mcfs_get_free_bucket (void)
  255. {
  256. int i;
  257. if (mcfs_get_free_bucket_init) {
  258. mcfs_get_free_bucket_init = 0;
  259. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
  260. mcfs_connections[i].host = 0;
  261. }
  262. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
  263. if (!mcfs_connections[i].host)
  264. return &mcfs_connections[i];
  265. }
  266. /* This can't happend, since we have checked for max connections before */
  267. vfs_die ("Internal error: mcfs_get_free_bucket");
  268. return 0; /* shut up, stupid gcc */
  269. }
  270. /* This routine keeps track of open connections */
  271. /* Returns a connected socket to host */
  272. static mcfs_connection *
  273. mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
  274. {
  275. static int mcfs_open_connections = 0;
  276. int i, sock, version;
  277. mcfs_connection *bucket;
  278. /* Is the link actually open? */
  279. if (mcfs_get_free_bucket_init) {
  280. mcfs_get_free_bucket_init = 0;
  281. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
  282. mcfs_connections[i].host = 0;
  283. } else
  284. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
  285. if (!mcfs_connections[i].host)
  286. continue;
  287. if ((strcmp (host, mcfs_connections[i].host) == 0) &&
  288. (strcmp (user, mcfs_connections[i].user) == 0))
  289. return &mcfs_connections[i];
  290. }
  291. if (mcfs_open_connections == MCFS_MAX_CONNECTIONS) {
  292. message (D_ERROR, MSG_ERROR, _(" Too many open connections "));
  293. return 0;
  294. }
  295. if (!
  296. (sock =
  297. mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
  298. return 0;
  299. bucket = mcfs_get_free_bucket ();
  300. mcfs_open_connections++;
  301. bucket->host = g_strdup (host);
  302. bucket->user = g_strdup (user);
  303. bucket->home = 0;
  304. bucket->port = *port;
  305. bucket->sock = sock;
  306. bucket->version = version;
  307. return bucket;
  308. }
  309. static int
  310. mcfs_is_error (int result, int errno_num)
  311. {
  312. if (!(result == -1))
  313. return my_errno = 0;
  314. else
  315. my_errno = errno_num;
  316. return 1;
  317. }
  318. static int
  319. mcfs_set_error (int result, int errno_num)
  320. {
  321. if (result == -1)
  322. my_errno = errno_num;
  323. else
  324. my_errno = 0;
  325. return result;
  326. }
  327. static char *
  328. mcfs_get_path (mcfs_connection **mc, const char *path)
  329. {
  330. char *user, *host, *remote_path;
  331. char *pass;
  332. int port;
  333. /* An absolute path name, try to determine connection socket */
  334. if (strncmp (path, "/#mc:", 5))
  335. return NULL;
  336. path += 5;
  337. /* Port = 0 means that mcfs_create_tcp_link will try to contact the
  338. * remote portmapper to get the port number
  339. */
  340. port = 0;
  341. if ((remote_path =
  342. mcfs_get_host_and_username (path, &host, &user, &port, &pass)))
  343. if (!(*mc = mcfs_open_link (host, user, &port, pass))) {
  344. g_free (remote_path);
  345. remote_path = NULL;
  346. }
  347. g_free (host);
  348. g_free (user);
  349. if (pass)
  350. wipe_password (pass);
  351. if (!remote_path)
  352. return NULL;
  353. /* NOTE: tildes are deprecated. See ftpfs.c */
  354. {
  355. int f = !strcmp (remote_path, "/~");
  356. if (f || !strncmp (remote_path, "/~/", 3)) {
  357. char *s;
  358. s = concat_dir_and_file (mcfs_gethome (*mc),
  359. remote_path + 3 - f);
  360. g_free (remote_path);
  361. remote_path = s;
  362. }
  363. }
  364. return remote_path;
  365. }
  366. /* Simple function for routines returning only an integer from the server */
  367. static int
  368. mcfs_handle_simple_error (int sock, int return_status)
  369. {
  370. int status, error;
  371. if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
  372. return mcfs_set_error (-1, EIO);
  373. if (mcfs_is_error (status, error))
  374. return -1;
  375. if (return_status)
  376. return status;
  377. return 0;
  378. }
  379. /* Nice wrappers */
  380. static int
  381. mcfs_rpc_two_paths (int command, const char *s1, const char *s2)
  382. {
  383. mcfs_connection *mc;
  384. char *r1, *r2;
  385. if ((r1 = mcfs_get_path (&mc, s1)) == 0)
  386. return -1;
  387. if ((r2 = mcfs_get_path (&mc, s2)) == 0) {
  388. g_free (r1);
  389. return -1;
  390. }
  391. rpc_send (mc->sock,
  392. RPC_INT, command, RPC_STRING, r1, RPC_STRING, r2, RPC_END);
  393. g_free (r1);
  394. g_free (r2);
  395. return mcfs_handle_simple_error (mc->sock, 0);
  396. }
  397. static int
  398. mcfs_rpc_path (int command, const char *path)
  399. {
  400. mcfs_connection *mc;
  401. char *remote_file;
  402. if ((remote_file = mcfs_get_path (&mc, path)) == 0)
  403. return -1;
  404. rpc_send (mc->sock,
  405. RPC_INT, command, RPC_STRING, remote_file, RPC_END);
  406. g_free (remote_file);
  407. return mcfs_handle_simple_error (mc->sock, 0);
  408. }
  409. static int
  410. mcfs_rpc_path_int (int command, const char *path, int data)
  411. {
  412. mcfs_connection *mc;
  413. char *remote_file;
  414. if ((remote_file = mcfs_get_path (&mc, path)) == 0)
  415. return -1;
  416. rpc_send (mc->sock,
  417. RPC_INT, command,
  418. RPC_STRING, remote_file, RPC_INT, data, RPC_END);
  419. g_free (remote_file);
  420. return mcfs_handle_simple_error (mc->sock, 0);
  421. }
  422. static int
  423. mcfs_rpc_path_int_int (int command, const char *path, int n1, int n2)
  424. {
  425. mcfs_connection *mc;
  426. char *remote_file;
  427. if ((remote_file = mcfs_get_path (&mc, path)) == 0)
  428. return -1;
  429. rpc_send (mc->sock,
  430. RPC_INT, command,
  431. RPC_STRING, remote_file, RPC_INT, n1, RPC_INT, n2, RPC_END);
  432. g_free (remote_file);
  433. return mcfs_handle_simple_error (mc->sock, 0);
  434. }
  435. static char *
  436. mcfs_gethome (mcfs_connection *mc)
  437. {
  438. char *buffer;
  439. if (mc->home)
  440. return g_strdup (mc->home);
  441. else {
  442. rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
  443. if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
  444. return g_strdup (PATH_SEP_STR);
  445. mc->home = buffer;
  446. return g_strdup (buffer);
  447. }
  448. }
  449. /* The callbacks */
  450. static void *
  451. mcfs_open (struct vfs_class *me, const char *file, int flags, int mode)
  452. {
  453. char *remote_file;
  454. mcfs_connection *mc;
  455. int result, error_num;
  456. mcfs_handle *remote_handle;
  457. (void) me;
  458. if (!(remote_file = mcfs_get_path (&mc, file)))
  459. return 0;
  460. rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file, RPC_INT,
  461. flags, RPC_INT, mode, RPC_END);
  462. g_free (remote_file);
  463. if (0 ==
  464. rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
  465. return 0;
  466. if (mcfs_is_error (result, error_num))
  467. return 0;
  468. remote_handle = g_new (mcfs_handle, 2);
  469. remote_handle->handle = result;
  470. remote_handle->conn = mc;
  471. return remote_handle;
  472. }
  473. static ssize_t
  474. mcfs_read (void *data, char *buffer, int count)
  475. {
  476. mcfs_handle *info = (mcfs_handle *) data;
  477. int result, error;
  478. int handle;
  479. mcfs_connection *mc;
  480. mc = info->conn;
  481. handle = info->handle;
  482. rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
  483. RPC_INT, count, RPC_END);
  484. if (0 ==
  485. rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
  486. return mcfs_set_error (-1, EIO);
  487. if (mcfs_is_error (result, error))
  488. return 0;
  489. if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
  490. return mcfs_set_error (-1, EIO);
  491. return result;
  492. }
  493. static ssize_t
  494. mcfs_write (void *data, const char *buf, int nbyte)
  495. {
  496. mcfs_handle *info = (mcfs_handle *) data;
  497. mcfs_connection *mc;
  498. int handle;
  499. mc = info->conn;
  500. handle = info->handle;
  501. rpc_send (mc->sock,
  502. RPC_INT, MC_WRITE,
  503. RPC_INT, handle,
  504. RPC_INT, nbyte, RPC_BLOCK, nbyte, buf, RPC_END);
  505. return mcfs_handle_simple_error (mc->sock, 1);
  506. }
  507. static int
  508. mcfs_close (void *data)
  509. {
  510. mcfs_handle *info = (mcfs_handle *) data;
  511. mcfs_connection *mc;
  512. int handle, result, error;
  513. if (!data)
  514. return -1;
  515. handle = info->handle;
  516. mc = info->conn;
  517. rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
  518. if (0 ==
  519. rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
  520. return mcfs_set_error (-1, EIO);
  521. mcfs_is_error (result, error);
  522. g_free (data);
  523. return result;
  524. }
  525. static int
  526. mcfs_errno (struct vfs_class *me)
  527. {
  528. (void) me;
  529. return my_errno;
  530. }
  531. typedef struct dir_entry {
  532. char *text;
  533. struct dir_entry *next;
  534. struct stat my_stat;
  535. int merrno;
  536. } dir_entry;
  537. typedef struct {
  538. mcfs_connection *conn;
  539. int handle;
  540. dir_entry *entries;
  541. dir_entry *current;
  542. } opendir_info;
  543. static void *
  544. mcfs_opendir (struct vfs_class *me, const char *dirname)
  545. {
  546. opendir_info *mcfs_info;
  547. mcfs_connection *mc;
  548. int handle, error_num;
  549. char *remote_dir;
  550. int result;
  551. (void) me;
  552. if (!(remote_dir = mcfs_get_path (&mc, dirname)))
  553. return 0;
  554. rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir,
  555. RPC_END);
  556. g_free (remote_dir);
  557. if (0 ==
  558. rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
  559. return 0;
  560. if (mcfs_is_error (result, error_num))
  561. return 0;
  562. handle = result;
  563. mcfs_info = g_new (opendir_info, 1);
  564. mcfs_info->conn = mc;
  565. mcfs_info->handle = handle;
  566. mcfs_info->entries = 0;
  567. mcfs_info->current = 0;
  568. return mcfs_info;
  569. }
  570. static int mcfs_get_stat_info (mcfs_connection * mc, struct stat *buf);
  571. static int
  572. mcfs_loaddir (opendir_info *mcfs_info)
  573. {
  574. int status, error;
  575. mcfs_connection *mc = mcfs_info->conn;
  576. int link = mc->sock;
  577. int first = 1;
  578. rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle,
  579. RPC_END);
  580. for (;;) {
  581. int entry_len;
  582. dir_entry *new_entry;
  583. if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
  584. return 0;
  585. if (entry_len == 0)
  586. break;
  587. new_entry = g_new (dir_entry, 1);
  588. new_entry->text = g_new0 (char, entry_len + 1);
  589. new_entry->next = 0;
  590. if (first) {
  591. mcfs_info->entries = new_entry;
  592. mcfs_info->current = new_entry;
  593. first = 0;
  594. } else {
  595. mcfs_info->current->next = new_entry;
  596. mcfs_info->current = new_entry;
  597. }
  598. if (!rpc_get
  599. (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
  600. return 0;
  601. /* Then we get the status from the lstat */
  602. if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
  603. return 0;
  604. if (mcfs_is_error (status, error))
  605. new_entry->merrno = error;
  606. else {
  607. new_entry->merrno = 0;
  608. if (!mcfs_get_stat_info (mc, &(new_entry->my_stat)))
  609. return 0;
  610. }
  611. }
  612. mcfs_info->current = mcfs_info->entries;
  613. return 1;
  614. }
  615. static void
  616. mcfs_free_dir (dir_entry *de)
  617. {
  618. if (!de)
  619. return;
  620. mcfs_free_dir (de->next);
  621. g_free (de->text);
  622. g_free (de);
  623. }
  624. static union vfs_dirent mcfs_readdir_data;
  625. /* The readdir routine loads the complete directory */
  626. /* It's too slow to ask the server each time */
  627. /* It now also sends the complete lstat information for each file */
  628. static struct stat *cached_lstat_info;
  629. static void *
  630. mcfs_readdir (void *info)
  631. {
  632. opendir_info *mcfs_info;
  633. char *dirent_dest;
  634. mcfs_info = (opendir_info *) info;
  635. if (!mcfs_info->entries)
  636. if (!mcfs_loaddir (mcfs_info))
  637. return NULL;
  638. if (mcfs_info->current == 0) {
  639. cached_lstat_info = 0;
  640. mcfs_free_dir (mcfs_info->entries);
  641. mcfs_info->entries = 0;
  642. return NULL;
  643. }
  644. dirent_dest = mcfs_readdir_data.dent.d_name;
  645. g_strlcpy (dirent_dest, mcfs_info->current->text, MC_MAXPATHLEN);
  646. cached_lstat_info = &mcfs_info->current->my_stat;
  647. mcfs_info->current = mcfs_info->current->next;
  648. compute_namelen (&mcfs_readdir_data.dent);
  649. return &mcfs_readdir_data;
  650. }
  651. static int
  652. mcfs_closedir (void *info)
  653. {
  654. opendir_info *mcfs_info = (opendir_info *) info;
  655. dir_entry *p, *q;
  656. rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
  657. RPC_INT, mcfs_info->handle, RPC_END);
  658. for (p = mcfs_info->entries; p;) {
  659. q = p;
  660. p = p->next;
  661. g_free (q->text);
  662. g_free (q);
  663. }
  664. g_free (info);
  665. return 0;
  666. }
  667. static time_t
  668. mcfs_get_time (mcfs_connection *mc)
  669. {
  670. int sock = mc->sock;
  671. if (mc->version == 1) {
  672. struct tm tt;
  673. rpc_get (sock,
  674. RPC_INT, &tt.tm_sec,
  675. RPC_INT, &tt.tm_min,
  676. RPC_INT, &tt.tm_hour,
  677. RPC_INT, &tt.tm_mday,
  678. RPC_INT, &tt.tm_year, RPC_INT, &tt.tm_mon, RPC_END);
  679. tt.tm_year -= 1900;
  680. tt.tm_isdst = 0;
  681. return mktime (&tt);
  682. } else {
  683. char *buf;
  684. long tm;
  685. rpc_get (sock, RPC_STRING, &buf, RPC_END);
  686. sscanf (buf, "%lx", &tm);
  687. g_free (buf);
  688. return (time_t) tm;
  689. }
  690. }
  691. static int
  692. mcfs_get_stat_info (mcfs_connection *mc, struct stat *buf)
  693. {
  694. long mylong;
  695. int sock = mc->sock;
  696. buf->st_dev = 0;
  697. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  698. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  699. buf->st_rdev = mylong;
  700. #endif
  701. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  702. buf->st_ino = mylong;
  703. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  704. buf->st_mode = mylong;
  705. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  706. buf->st_nlink = mylong;
  707. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  708. buf->st_uid = mylong;
  709. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  710. buf->st_gid = mylong;
  711. rpc_get (sock, RPC_INT, &mylong, RPC_END);
  712. buf->st_size = mylong;
  713. if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
  714. return 0;
  715. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  716. buf->st_blocks = mylong;
  717. #endif
  718. buf->st_atime = mcfs_get_time (mc);
  719. buf->st_mtime = mcfs_get_time (mc);
  720. buf->st_ctime = mcfs_get_time (mc);
  721. return 1;
  722. }
  723. static int
  724. mcfs_stat_cmd (int cmd, const char *path, struct stat *buf)
  725. {
  726. char *remote_file;
  727. mcfs_connection *mc;
  728. int status, error;
  729. if ((remote_file = mcfs_get_path (&mc, path)) == 0)
  730. return -1;
  731. rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
  732. g_free (remote_file);
  733. if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
  734. return mcfs_set_error (-1, errno);
  735. if (mcfs_is_error (status, error))
  736. return -1;
  737. if (mcfs_get_stat_info (mc, buf))
  738. return 0;
  739. else
  740. return mcfs_set_error (-1, EIO);
  741. }
  742. static int
  743. mcfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
  744. {
  745. (void) me;
  746. return mcfs_stat_cmd (MC_STAT, path, buf);
  747. }
  748. static int
  749. mcfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  750. {
  751. int path_len = strlen (path);
  752. int entry_len = strlen (mcfs_readdir_data.dent.d_name);
  753. (void) me;
  754. /* Hack ... */
  755. if (strcmp (path + path_len - entry_len,
  756. mcfs_readdir_data.dent.d_name) == 0 && cached_lstat_info) {
  757. *buf = *cached_lstat_info;
  758. return 0;
  759. }
  760. return mcfs_stat_cmd (MC_LSTAT, path, buf);
  761. }
  762. static int
  763. mcfs_fstat (void *data, struct stat *buf)
  764. {
  765. mcfs_handle *info = (mcfs_handle *) data;
  766. int result, error;
  767. int handle, sock;
  768. sock = info->conn->sock;
  769. handle = info->handle;
  770. rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
  771. if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
  772. return mcfs_set_error (-1, EIO);
  773. if (mcfs_is_error (result, error))
  774. return -1;
  775. if (mcfs_get_stat_info (info->conn, buf))
  776. return 0;
  777. else
  778. return mcfs_set_error (-1, EIO);
  779. }
  780. static int
  781. mcfs_chmod (struct vfs_class *me, const char *path, int mode)
  782. {
  783. (void) me;
  784. return mcfs_rpc_path_int (MC_CHMOD, path, mode);
  785. }
  786. static int
  787. mcfs_chown (struct vfs_class *me, const char *path, int owner, int group)
  788. {
  789. (void) me;
  790. return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
  791. }
  792. static int
  793. mcfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
  794. {
  795. mcfs_connection *mc;
  796. int status;
  797. char *file;
  798. (void) me;
  799. if (!(file = mcfs_get_path (&mc, path)))
  800. return -1;
  801. status = 0;
  802. if (mc->version >= 2) {
  803. char abuf[BUF_SMALL];
  804. char mbuf[BUF_SMALL];
  805. long atime, mtime;
  806. atime = (long) times->actime;
  807. mtime = (long) times->modtime;
  808. g_snprintf (abuf, sizeof (abuf), "%lx", atime);
  809. g_snprintf (mbuf, sizeof (mbuf), "%lx", mtime);
  810. rpc_send (mc->sock, RPC_INT, MC_UTIME,
  811. RPC_STRING, file,
  812. RPC_STRING, abuf, RPC_STRING, mbuf, RPC_END);
  813. status = mcfs_handle_simple_error (mc->sock, 0);
  814. }
  815. g_free (file);
  816. return (status);
  817. }
  818. static int
  819. mcfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  820. {
  821. char *remote_file, *stat_str;
  822. int status, error;
  823. mcfs_connection *mc;
  824. size_t len;
  825. (void) me;
  826. if (!(remote_file = mcfs_get_path (&mc, path)))
  827. return -1;
  828. rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file,
  829. RPC_END);
  830. g_free (remote_file);
  831. if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
  832. return mcfs_set_error (-1, EIO);
  833. if (mcfs_is_error (status, errno))
  834. return -1;
  835. if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
  836. return mcfs_set_error (-1, EIO);
  837. len = strlen (stat_str);
  838. if (len < size)
  839. size = len;
  840. /* readlink() does not append a NUL character to buf */
  841. memcpy (buf, stat_str, size);
  842. g_free (stat_str);
  843. return size;
  844. }
  845. static int
  846. mcfs_unlink (struct vfs_class *me, const char *path)
  847. {
  848. (void) me;
  849. return mcfs_rpc_path (MC_UNLINK, path);
  850. }
  851. static int
  852. mcfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
  853. {
  854. (void) me;
  855. return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
  856. }
  857. static int
  858. mcfs_rename (struct vfs_class *me, const char *a, const char *b)
  859. {
  860. (void) me;
  861. return mcfs_rpc_two_paths (MC_RENAME, a, b);
  862. }
  863. static int
  864. mcfs_chdir (struct vfs_class *me, const char *path)
  865. {
  866. char *remote_dir;
  867. mcfs_connection *mc;
  868. int status, error;
  869. (void) me;
  870. if (!(remote_dir = mcfs_get_path (&mc, path)))
  871. return -1;
  872. rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir,
  873. RPC_END);
  874. g_free (remote_dir);
  875. if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
  876. return mcfs_set_error (-1, EIO);
  877. if (mcfs_is_error (status, error))
  878. return -1;
  879. return 0;
  880. }
  881. static off_t
  882. mcfs_lseek (void *data, off_t offset, int whence)
  883. {
  884. mcfs_handle *info = (mcfs_handle *) data;
  885. int handle, sock;
  886. sock = info->conn->sock;
  887. handle = info->handle;
  888. /* FIXME: off_t may be too long to fit */
  889. rpc_send (sock, RPC_INT, MC_LSEEK, RPC_INT, handle, RPC_INT,
  890. (int) offset, RPC_INT, whence, RPC_END);
  891. return mcfs_handle_simple_error (sock, 1);
  892. }
  893. static int
  894. mcfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
  895. {
  896. (void) me;
  897. return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
  898. }
  899. static int
  900. mcfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  901. {
  902. (void) me;
  903. return mcfs_rpc_path_int (MC_MKDIR, path, mode);
  904. }
  905. static int
  906. mcfs_rmdir (struct vfs_class *me, const char *path)
  907. {
  908. (void) me;
  909. return mcfs_rpc_path (MC_RMDIR, path);
  910. }
  911. static int
  912. mcfs_link (struct vfs_class *me, const char *p1, const char *p2)
  913. {
  914. (void) me;
  915. return mcfs_rpc_two_paths (MC_LINK, p1, p2);
  916. }
  917. /* Gives up on a socket and reopens the connection, the child own the socket
  918. * now
  919. */
  920. static void
  921. mcfs_forget (const char *path)
  922. {
  923. char *host, *user, *pass, *p;
  924. int port, i, vers;
  925. if (strncmp (path, "/#mc:", 5))
  926. return;
  927. path += 5;
  928. if (path[0] == '/' && path[1] == '/')
  929. path += 2;
  930. if ((p =
  931. mcfs_get_host_and_username (path, &host, &user, &port,
  932. &pass)) == 0) {
  933. g_free (host);
  934. g_free (user);
  935. if (pass)
  936. wipe_password (pass);
  937. return;
  938. }
  939. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
  940. if ((strcmp (host, mcfs_connections[i].host) == 0) &&
  941. (strcmp (user, mcfs_connections[i].user) == 0) &&
  942. (port == mcfs_connections[i].port)) {
  943. /* close socket: the child owns it now */
  944. close (mcfs_connections[i].sock);
  945. /* reopen the connection */
  946. mcfs_connections[i].sock =
  947. mcfs_open_tcp_link (host, user, &port, pass, &vers);
  948. }
  949. }
  950. g_free (p);
  951. g_free (host);
  952. g_free (user);
  953. if (pass)
  954. wipe_password (pass);
  955. }
  956. static int
  957. mcfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  958. {
  959. (void) me;
  960. (void) arg;
  961. switch (ctlop) {
  962. case VFS_SETCTL_FORGET:
  963. mcfs_forget (path);
  964. return 0;
  965. }
  966. return 0;
  967. }
  968. void
  969. init_mcfs (void)
  970. {
  971. vfs_mcfs_ops.name = "mcfs";
  972. vfs_mcfs_ops.prefix = "mc:";
  973. vfs_mcfs_ops.fill_names = mcfs_fill_names;
  974. vfs_mcfs_ops.open = mcfs_open;
  975. vfs_mcfs_ops.close = mcfs_close;
  976. vfs_mcfs_ops.read = mcfs_read;
  977. vfs_mcfs_ops.write = mcfs_write;
  978. vfs_mcfs_ops.opendir = mcfs_opendir;
  979. vfs_mcfs_ops.readdir = mcfs_readdir;
  980. vfs_mcfs_ops.closedir = mcfs_closedir;
  981. vfs_mcfs_ops.stat = mcfs_stat;
  982. vfs_mcfs_ops.lstat = mcfs_lstat;
  983. vfs_mcfs_ops.fstat = mcfs_fstat;
  984. vfs_mcfs_ops.chmod = mcfs_chmod;
  985. vfs_mcfs_ops.chown = mcfs_chown;
  986. vfs_mcfs_ops.utime = mcfs_utime;
  987. vfs_mcfs_ops.readlink = mcfs_readlink;
  988. vfs_mcfs_ops.symlink = mcfs_symlink;
  989. vfs_mcfs_ops.link = mcfs_link;
  990. vfs_mcfs_ops.unlink = mcfs_unlink;
  991. vfs_mcfs_ops.rename = mcfs_rename;
  992. vfs_mcfs_ops.chdir = mcfs_chdir;
  993. vfs_mcfs_ops.ferrno = mcfs_errno;
  994. vfs_mcfs_ops.lseek = mcfs_lseek;
  995. vfs_mcfs_ops.mknod = mcfs_mknod;
  996. vfs_mcfs_ops.mkdir = mcfs_mkdir;
  997. vfs_mcfs_ops.rmdir = mcfs_rmdir;
  998. vfs_mcfs_ops.setctl = mcfs_setctl;
  999. vfs_register_class (&vfs_mcfs_ops);
  1000. }
  1001. static void
  1002. mcfs_free_bucket (int bucket)
  1003. {
  1004. g_free (mcfs_connections[bucket].host);
  1005. g_free (mcfs_connections[bucket].user);
  1006. g_free (mcfs_connections[bucket].home);
  1007. /* Set all the fields to zero */
  1008. mcfs_connections[bucket].host =
  1009. mcfs_connections[bucket].user = mcfs_connections[bucket].home = 0;
  1010. mcfs_connections[bucket].sock = mcfs_connections[bucket].version = 0;
  1011. }
  1012. static int
  1013. mcfs_invalidate_socket (int sock)
  1014. {
  1015. int i, j = -1;
  1016. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
  1017. if (mcfs_connections[i].sock == sock) {
  1018. mcfs_free_bucket (i);
  1019. j = 0;
  1020. }
  1021. if (j == -1)
  1022. return -1; /* It was not our sock */
  1023. /* Break from any possible loop */
  1024. mc_chdir ("/");
  1025. return 0;
  1026. }
  1027. void
  1028. tcp_invalidate_socket (int sock)
  1029. {
  1030. mcfs_invalidate_socket (sock);
  1031. }
  1032. #else
  1033. void mcfs__unused(void)
  1034. {
  1035. /*
  1036. CFLAGS="-ansi -pedantic -Wall -Wextra -Werror"
  1037. */
  1038. }
  1039. #endif /* ENABLE_VFS_MCFS */