mcfs.c 27 KB

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