mcfs.c 28 KB

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