mcfs.c 29 KB

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