mcfs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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 "lib/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 "netutil.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 lc_link = mc->sock;
  577. int first = 1;
  578. rpc_send (lc_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 (lc_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. (lc_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 (lc_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. tcp_init();
  972. vfs_mcfs_ops.name = "mcfs";
  973. vfs_mcfs_ops.prefix = "mc:";
  974. vfs_mcfs_ops.fill_names = mcfs_fill_names;
  975. vfs_mcfs_ops.open = mcfs_open;
  976. vfs_mcfs_ops.close = mcfs_close;
  977. vfs_mcfs_ops.read = mcfs_read;
  978. vfs_mcfs_ops.write = mcfs_write;
  979. vfs_mcfs_ops.opendir = mcfs_opendir;
  980. vfs_mcfs_ops.readdir = mcfs_readdir;
  981. vfs_mcfs_ops.closedir = mcfs_closedir;
  982. vfs_mcfs_ops.stat = mcfs_stat;
  983. vfs_mcfs_ops.lstat = mcfs_lstat;
  984. vfs_mcfs_ops.fstat = mcfs_fstat;
  985. vfs_mcfs_ops.chmod = mcfs_chmod;
  986. vfs_mcfs_ops.chown = mcfs_chown;
  987. vfs_mcfs_ops.utime = mcfs_utime;
  988. vfs_mcfs_ops.readlink = mcfs_readlink;
  989. vfs_mcfs_ops.symlink = mcfs_symlink;
  990. vfs_mcfs_ops.link = mcfs_link;
  991. vfs_mcfs_ops.unlink = mcfs_unlink;
  992. vfs_mcfs_ops.rename = mcfs_rename;
  993. vfs_mcfs_ops.chdir = mcfs_chdir;
  994. vfs_mcfs_ops.ferrno = mcfs_errno;
  995. vfs_mcfs_ops.lseek = mcfs_lseek;
  996. vfs_mcfs_ops.mknod = mcfs_mknod;
  997. vfs_mcfs_ops.mkdir = mcfs_mkdir;
  998. vfs_mcfs_ops.rmdir = mcfs_rmdir;
  999. vfs_mcfs_ops.setctl = mcfs_setctl;
  1000. vfs_register_class (&vfs_mcfs_ops);
  1001. }
  1002. static void
  1003. mcfs_free_bucket (int bucket)
  1004. {
  1005. g_free (mcfs_connections[bucket].host);
  1006. g_free (mcfs_connections[bucket].user);
  1007. g_free (mcfs_connections[bucket].home);
  1008. /* Set all the fields to zero */
  1009. mcfs_connections[bucket].host =
  1010. mcfs_connections[bucket].user = mcfs_connections[bucket].home = 0;
  1011. mcfs_connections[bucket].sock = mcfs_connections[bucket].version = 0;
  1012. }
  1013. static int
  1014. mcfs_invalidate_socket (int sock)
  1015. {
  1016. int i, j = -1;
  1017. for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
  1018. if (mcfs_connections[i].sock == sock) {
  1019. mcfs_free_bucket (i);
  1020. j = 0;
  1021. }
  1022. if (j == -1)
  1023. return -1; /* It was not our sock */
  1024. /* Break from any possible loop */
  1025. mc_chdir ("/");
  1026. return 0;
  1027. }
  1028. void
  1029. tcp_invalidate_socket (int sock)
  1030. {
  1031. mcfs_invalidate_socket (sock);
  1032. }
  1033. #endif /* ENABLE_VFS_MCFS */