mcfs.c 29 KB

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