mcfs.c 27 KB

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