smbfs.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /* Virtual File System: Midnight Commander file system.
  2. Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
  3. Free Software Foundation, Inc.
  4. Written by Wayne Roberts <wroberts1@home.com>, 1997
  5. Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #undef USE_NCURSES /* Don't include *curses.h */
  22. #include "../src/global.h"
  23. #include "../src/tty.h" /* enable/disable interrupt key */
  24. #include "../src/wtools.h" /* message() */
  25. #include "../src/main.h" /* print_vfs_message */
  26. #include "utilvfs.h"
  27. #undef PACKAGE_BUGREPORT
  28. #undef PACKAGE_NAME
  29. #undef PACKAGE_STRING
  30. #undef PACKAGE_TARNAME
  31. #undef PACKAGE_VERSION
  32. #include "samba/include/config.h"
  33. /* don't load crap in "samba/include/includes.h" we don't use and which
  34. conflicts with definitions in other includes */
  35. #undef HAVE_LIBREADLINE
  36. #define NO_CONFIG_H
  37. #undef VERSION
  38. #include "samba/include/includes.h"
  39. #include <string.h>
  40. #include "vfs.h"
  41. #include "vfs-impl.h"
  42. #include "smbfs.h"
  43. #define SMBFS_MAX_CONNECTIONS 16
  44. static const char * const IPC = "IPC$";
  45. static const char * const URL_HEADER = "/#smb:";
  46. #define HEADER_LEN 6
  47. static int my_errno;
  48. static uint32 err;
  49. /* stuff that is same with each connection */
  50. extern int DEBUGLEVEL;
  51. extern pstring myhostname;
  52. extern struct in_addr ipzero;
  53. static mode_t myumask = 0755;
  54. extern pstring global_myname;
  55. static int smbfs_open_connections = 0;
  56. static gboolean got_user = FALSE;
  57. static gboolean got_pass = FALSE;
  58. static pstring password;
  59. static pstring username;
  60. static struct vfs_class vfs_smbfs_ops;
  61. static struct _smbfs_connection {
  62. struct cli_state *cli;
  63. struct in_addr dest_ip;
  64. BOOL have_ip;
  65. char *host; /* server name */
  66. char *service; /* share name */
  67. char *domain;
  68. char *user;
  69. char *home;
  70. char *password;
  71. int port;
  72. int name_type;
  73. time_t last_use;
  74. } smbfs_connections [SMBFS_MAX_CONNECTIONS];
  75. /* unique to each connection */
  76. /* modifies *share */
  77. static struct cli_state * smbfs_do_connect (const char *server, char *share);
  78. typedef struct _smbfs_connection smbfs_connection;
  79. static smbfs_connection *current_bucket;
  80. typedef struct {
  81. struct cli_state *cli;
  82. int fnum;
  83. off_t nread;
  84. uint16 attr;
  85. } smbfs_handle;
  86. static GSList *auth_list;
  87. /* this function allows you to write:
  88. * char *s = g_strdup("hello, world");
  89. * s = free_after(g_strconcat(s, s, (char *)0), s);
  90. */
  91. static inline char *
  92. free_after (char *result, char *string_to_free)
  93. {
  94. g_free(string_to_free);
  95. return result;
  96. }
  97. static void
  98. smbfs_auth_free (struct smb_authinfo const *a)
  99. {
  100. g_free (a->host);
  101. g_free (a->share);
  102. g_free (a->domain);
  103. g_free (a->user);
  104. wipe_password (a->password);
  105. }
  106. static void
  107. smbfs_auth_free_all (void)
  108. {
  109. if (auth_list) {
  110. g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
  111. g_slist_free (auth_list);
  112. auth_list = 0;
  113. }
  114. }
  115. static gint
  116. smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
  117. {
  118. struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
  119. struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
  120. if (!a->host || !a->share || !b->host || !b->share)
  121. return 1;
  122. if (strcmp (a->host, b->host) != 0)
  123. return 1;
  124. if (strcmp (a->share, b->share) != 0)
  125. return 1;
  126. return 0;
  127. }
  128. static gint
  129. smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
  130. {
  131. struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
  132. struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
  133. if (!a->host || !b->host)
  134. return 1;
  135. if (strcmp (a->host, b->host) != 0)
  136. return 1;
  137. if (strcmp (a->share, IPC) != 0)
  138. return 1;
  139. return 0;
  140. }
  141. static void
  142. smbfs_auth_add (const char *host, const char *share, const char *domain,
  143. const char *user, const char *password)
  144. {
  145. struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
  146. if (!auth)
  147. return;
  148. /* Don't check for NULL, g_strdup already does. */
  149. auth->host = g_strdup (host);
  150. auth->share = g_strdup (share);
  151. auth->domain = g_strdup (domain);
  152. auth->user = g_strdup (user);
  153. auth->password = g_strdup (password);
  154. auth_list = g_slist_prepend (auth_list, auth);
  155. }
  156. static void
  157. smbfs_auth_remove (const char *host, const char *share)
  158. {
  159. struct smb_authinfo data;
  160. struct smb_authinfo *auth;
  161. GSList *list;
  162. data.host = g_strdup (host);
  163. data.share = g_strdup (share);
  164. list = g_slist_find_custom (auth_list,
  165. &data,
  166. smbfs_auth_cmp_host_and_share);
  167. g_free (data.host);
  168. g_free (data.share);
  169. if (!list)
  170. return;
  171. auth = list->data;
  172. auth_list = g_slist_remove (auth_list, auth);
  173. smbfs_auth_free (auth);
  174. }
  175. /* Set authentication information in bucket. Return 1 if successful, else 0 */
  176. /* Information in auth_list overrides user if pass is NULL. */
  177. /* bucket->host and bucket->service must be valid. */
  178. static int
  179. smbfs_bucket_set_authinfo (smbfs_connection *bucket,
  180. const char *domain, const char *user, const char *pass,
  181. int fallback_to_host)
  182. {
  183. struct smb_authinfo data;
  184. struct smb_authinfo *auth;
  185. GSList *list;
  186. if (domain && user && pass) {
  187. g_free (bucket->domain);
  188. g_free (bucket->user);
  189. g_free (bucket->password);
  190. bucket->domain = g_strdup (domain);
  191. bucket->user = g_strdup (user);
  192. bucket->password = g_strdup (pass);
  193. smbfs_auth_remove (bucket->host, bucket->service);
  194. smbfs_auth_add (bucket->host, bucket->service,
  195. domain, user, pass);
  196. return 1;
  197. }
  198. data.host = bucket->host;
  199. data.share = bucket->service;
  200. list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
  201. if (!list && fallback_to_host)
  202. list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
  203. if (list) {
  204. auth = list->data;
  205. bucket->domain = g_strdup (auth->domain);
  206. bucket->user = g_strdup (auth->user);
  207. bucket->password = g_strdup (auth->password);
  208. return 1;
  209. }
  210. if (got_pass) {
  211. bucket->domain = g_strdup (lp_workgroup ());
  212. bucket->user = g_strdup (got_user ? username : user);
  213. bucket->password = g_strdup (password);
  214. return 1;
  215. }
  216. auth = vfs_smb_get_authinfo (bucket->host,
  217. bucket->service,
  218. (domain ? domain : lp_workgroup ()),
  219. user);
  220. if (auth) {
  221. g_free (bucket->domain);
  222. g_free (bucket->user);
  223. g_free (bucket->password);
  224. bucket->domain = g_strdup (auth->domain);
  225. bucket->user = g_strdup (auth->user);
  226. bucket->password = g_strdup (auth->password);
  227. smbfs_auth_remove (bucket->host, bucket->service);
  228. auth_list = g_slist_prepend (auth_list, auth);
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. void
  234. smbfs_set_debug (int arg)
  235. {
  236. DEBUGLEVEL = arg;
  237. }
  238. void
  239. smbfs_set_debugf (const char *filename)
  240. {
  241. extern pstring debugf;
  242. extern FILE *dbf;
  243. if (DEBUGLEVEL > 0) {
  244. FILE *outfile = fopen (filename, "w");
  245. if (outfile) {
  246. setup_logging ("", True); /* No needs for timestamp for each message */
  247. dbf = outfile;
  248. setbuf (dbf, NULL);
  249. pstrcpy (debugf, filename);
  250. }
  251. }
  252. }
  253. /********************** The callbacks ******************************/
  254. static int
  255. smbfs_init (struct vfs_class * me)
  256. {
  257. const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
  258. /* DEBUGLEVEL = 4; */
  259. TimeInit ();
  260. charset_initialise ();
  261. DEBUG (3, ("smbfs_init(%s)\n", me->name));
  262. if (!get_myname (myhostname, NULL))
  263. DEBUG (0, ("Failed to get my hostname.\n"));
  264. if (!lp_load (servicesf, True, False, False))
  265. DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
  266. codepage_initialise (lp_client_code_page ());
  267. load_interfaces ();
  268. myumask = umask (0);
  269. umask (myumask);
  270. myumask = ~myumask;
  271. if (getenv ("USER")) {
  272. char *p;
  273. pstrcpy (username, getenv ("USER"));
  274. got_user = TRUE;
  275. DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
  276. if ((p = strchr (username, '%'))) {
  277. *p = 0;
  278. pstrcpy (password, p + 1);
  279. got_pass = TRUE;
  280. memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
  281. DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
  282. username, password));
  283. }
  284. strupper (username);
  285. }
  286. if (getenv ("PASSWD")) {
  287. pstrcpy (password, getenv ("PASSWD"));
  288. got_pass = TRUE;
  289. }
  290. return 1;
  291. }
  292. static void
  293. smbfs_fill_names (struct vfs_class *me, fill_names_f func)
  294. {
  295. int i;
  296. char *path;
  297. (void) me;
  298. for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
  299. if (smbfs_connections [i].cli) {
  300. path = g_strconcat (URL_HEADER,
  301. smbfs_connections[i].user, "@",
  302. smbfs_connections[i].host,
  303. "/", smbfs_connections[i].service,
  304. NULL);
  305. (*func)(path);
  306. g_free (path);
  307. }
  308. }
  309. }
  310. #define CNV_LANG(s) dos_to_unix(s,False)
  311. #define GNAL_VNC(s) unix_to_dos(s,False)
  312. /* does same as do_get() in client.c */
  313. /* called from vfs.c:1080, count = buffer size */
  314. static int
  315. smbfs_read (void *data, char *buffer, int count)
  316. {
  317. smbfs_handle *info = (smbfs_handle *) data;
  318. int n;
  319. DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
  320. info->fnum, (int)info->nread, count));
  321. n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
  322. if (n > 0)
  323. info->nread += n;
  324. return n;
  325. }
  326. static int
  327. smbfs_write (void *data, const char *buf, int nbyte)
  328. {
  329. smbfs_handle *info = (smbfs_handle *) data;
  330. int n;
  331. DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
  332. info->fnum, (int)info->nread, nbyte));
  333. n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
  334. if (n > 0)
  335. info->nread += n;
  336. return n;
  337. }
  338. static int
  339. smbfs_close (void *data)
  340. {
  341. smbfs_handle *info = (smbfs_handle *) data;
  342. DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
  343. /* FIXME: Why too different cli have the same outbuf
  344. * if file is copied to share
  345. */
  346. if (info->cli->outbuf == NULL) {
  347. my_errno = EINVAL;
  348. return -1;
  349. }
  350. #if 0
  351. /* if imlementing archive_level: add rname to smbfs_handle */
  352. if (archive_level >= 2 && (inf->attr & aARCH)) {
  353. cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
  354. }
  355. #endif
  356. return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
  357. }
  358. static int
  359. smbfs_errno (struct vfs_class *me)
  360. {
  361. (void) me;
  362. DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
  363. return my_errno;
  364. }
  365. typedef struct dir_entry {
  366. char *text;
  367. struct dir_entry *next;
  368. struct stat my_stat;
  369. int merrno;
  370. } dir_entry;
  371. typedef struct {
  372. gboolean server_list;
  373. char *dirname;
  374. char *path; /* the dir originally passed to smbfs_opendir */
  375. smbfs_connection *conn;
  376. dir_entry *entries;
  377. dir_entry *current;
  378. } opendir_info;
  379. static opendir_info
  380. *previous_info,
  381. *current_info,
  382. *current_share_info,
  383. *current_server_info;
  384. static gboolean first_direntry;
  385. static dir_entry *
  386. smbfs_new_dir_entry (const char *name)
  387. {
  388. static int inode_counter;
  389. dir_entry *new_entry;
  390. new_entry = g_new0 (dir_entry, 1);
  391. new_entry->text = dos_to_unix (g_strdup (name), 1);
  392. if (first_direntry) {
  393. current_info->entries = new_entry;
  394. first_direntry = FALSE;
  395. } else {
  396. current_info->current->next = new_entry;
  397. }
  398. current_info->current = new_entry;
  399. new_entry->my_stat.st_ino = inode_counter++;
  400. return new_entry;
  401. }
  402. /* browse for shares on server */
  403. static void
  404. smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
  405. {
  406. const char *typestr = "";
  407. dir_entry *new_entry = smbfs_new_dir_entry (name);
  408. (void) state;
  409. switch (type) {
  410. case STYPE_DISKTREE:
  411. typestr = "Disk";
  412. /* show this as dir */
  413. new_entry->my_stat.st_mode =
  414. (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
  415. S_IXOTH) & myumask;
  416. break;
  417. case STYPE_PRINTQ:
  418. typestr = "Printer";
  419. break;
  420. case STYPE_DEVICE:
  421. typestr = "Device";
  422. break;
  423. case STYPE_IPC:
  424. typestr = "IPC";
  425. break;
  426. }
  427. DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
  428. }
  429. static void
  430. smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
  431. {
  432. dir_entry *new_entry = (dir_entry *) entry;
  433. time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
  434. (void) mask;
  435. #if 0 /* I want to see dot files */
  436. if (finfo->mode & aHIDDEN)
  437. return; /* don't bother with hidden files, "~$" screws up mc */
  438. #endif
  439. if (!entry)
  440. new_entry = smbfs_new_dir_entry (finfo->name);
  441. new_entry->my_stat.st_size = finfo->size;
  442. new_entry->my_stat.st_mtime = finfo->mtime;
  443. new_entry->my_stat.st_atime = finfo->atime;
  444. new_entry->my_stat.st_ctime = finfo->ctime;
  445. new_entry->my_stat.st_uid = finfo->uid;
  446. new_entry->my_stat.st_gid = finfo->gid;
  447. new_entry->my_stat.st_mode = /* rw-rw-rw */
  448. S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
  449. /* if (finfo->mode & aVOLID); nothing similar in real world */
  450. if (finfo->mode & aDIR)
  451. new_entry->my_stat.st_mode |= /* drwxrwxrwx */
  452. S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  453. else
  454. new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
  455. /* if (finfo->mode & aARCH); DOS archive */
  456. /* if (finfo->mode & aHIDDEN); like a dot file? */
  457. /* if (finfo->mode & aSYSTEM); like a kernel? */
  458. if (finfo->mode & aRONLY)
  459. new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  460. new_entry->my_stat.st_mode &= myumask;
  461. DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
  462. CNV_LANG (finfo->name),
  463. attrib_string (finfo->mode),
  464. (double) finfo->size,
  465. asctime (LocalTime (&t))));
  466. }
  467. /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
  468. static char *
  469. smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
  470. {
  471. const char *p, *my_remote;
  472. char *result;
  473. my_remote = remote_file;
  474. if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
  475. my_remote += HEADER_LEN;
  476. if (*my_remote == '/') /* from server browsing */
  477. my_remote++;
  478. p = strchr(my_remote, '/');
  479. if (p)
  480. my_remote = p+1; /* advance to end of server name */
  481. }
  482. if (*my_remote == '/')
  483. my_remote++; /* strip off leading '/' */
  484. p = strchr(my_remote, '/');
  485. if (p)
  486. my_remote = p; /* strip off share/service name */
  487. /* create remote filename as understood by smb clientgen */
  488. result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
  489. unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
  490. str_replace(result, '/', '\\');
  491. return result;
  492. }
  493. static void
  494. smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
  495. void *state)
  496. {
  497. dir_entry *new_entry = smbfs_new_dir_entry (name);
  498. (void) m;
  499. (void) state;
  500. /* show this as dir */
  501. new_entry->my_stat.st_mode =
  502. (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
  503. S_IXOTH) & myumask;
  504. DEBUG (3, ("\t%-16.16s %s\n", name, comment));
  505. }
  506. static BOOL
  507. smbfs_reconnect(smbfs_connection *conn, int *retries)
  508. {
  509. char *host;
  510. DEBUG(3, ("RECONNECT\n"));
  511. if (*(conn->host) == 0)
  512. host = g_strdup(conn->cli->desthost); /* server browsing */
  513. else
  514. host = g_strdup(conn->host);
  515. cli_shutdown(conn->cli);
  516. if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
  517. message (1, MSG_ERROR,
  518. _(" reconnect to %s failed\n "), conn->host);
  519. g_free(host);
  520. return False;
  521. }
  522. g_free(host);
  523. if (++(*retries) == 2)
  524. return False;
  525. return True;
  526. }
  527. static BOOL
  528. smbfs_send(struct cli_state *cli)
  529. {
  530. size_t len;
  531. size_t nwritten=0;
  532. ssize_t ret;
  533. len = smb_len(cli->outbuf) + 4;
  534. while (nwritten < len) {
  535. ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
  536. if (ret <= 0) {
  537. if (errno == EPIPE)
  538. return False;
  539. } else
  540. nwritten += ret;
  541. }
  542. return True;
  543. }
  544. /****************************************************************************
  545. See if server has cut us off by checking for EPIPE when writing.
  546. Taken from cli_chkpath()
  547. ****************************************************************************/
  548. static BOOL
  549. smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
  550. {
  551. fstring path2;
  552. char *p;
  553. fstrcpy(path2,path);
  554. unix_to_dos (path2, 1);
  555. trim_string(path2,NULL,"\\");
  556. if (!*path2) *path2 = '\\';
  557. memset(cli->outbuf,'\0',smb_size);
  558. set_message(cli->outbuf,0,4 + strlen(path2),True);
  559. SCVAL(cli->outbuf,smb_com,SMBchkpth);
  560. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  561. cli->rap_error = 0;
  562. cli->nt_error = 0;
  563. SSVAL(cli->outbuf,smb_pid,cli->pid);
  564. SSVAL(cli->outbuf,smb_uid,cli->vuid);
  565. SSVAL(cli->outbuf,smb_mid,cli->mid);
  566. if (cli->protocol > PROTOCOL_CORE) {
  567. SCVAL(cli->outbuf,smb_flg,0x8);
  568. SSVAL(cli->outbuf,smb_flg2,0x1);
  569. }
  570. p = smb_buf(cli->outbuf);
  571. *p++ = 4;
  572. fstrcpy(p,path2);
  573. if (!smbfs_send(cli)) {
  574. DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
  575. return False;
  576. }
  577. if (send_only) {
  578. client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
  579. DEBUG(3, ("smbfs_chkpath: send only OK\n"));
  580. return True; /* just testing for EPIPE */
  581. }
  582. if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
  583. DEBUG(3, ("smbfs_chkpath: receive error\n"));
  584. return False;
  585. }
  586. if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
  587. if (my_errno == 20 || my_errno == 13)
  588. return True; /* ignore if 'not a directory' error */
  589. DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
  590. return False;
  591. }
  592. return True;
  593. }
  594. #if 1
  595. static int
  596. smbfs_fs (const char *text)
  597. {
  598. const char *p = text;
  599. int count = 0;
  600. while ((p = strchr(p, '/')) != NULL) {
  601. count++;
  602. p++;
  603. }
  604. if (count == 1)
  605. return strlen(text);
  606. return count;
  607. }
  608. #endif
  609. static int
  610. smbfs_loaddir (opendir_info *smbfs_info)
  611. {
  612. uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
  613. int servlen = strlen (smbfs_info->conn->service);
  614. const char *info_dirname = smbfs_info->dirname;
  615. char *my_dirname;
  616. DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
  617. first_direntry = TRUE;
  618. if (current_info) {
  619. DEBUG (3,
  620. ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
  621. current_info->dirname));
  622. /* if new desired dir is longer than cached in current_info */
  623. if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
  624. DEBUG (3, ("saving to previous_info\n"));
  625. previous_info = current_info;
  626. }
  627. }
  628. current_info = smbfs_info;
  629. if (strcmp (info_dirname, "/") == 0) {
  630. if (!strcmp (smbfs_info->path, URL_HEADER)) {
  631. DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
  632. /* browse for servers */
  633. if (!cli_NetServerEnum
  634. (smbfs_info->conn->cli, smbfs_info->conn->domain,
  635. SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
  636. return 0;
  637. else
  638. current_server_info = smbfs_info;
  639. smbfs_info->server_list = TRUE;
  640. } else {
  641. /* browse for shares */
  642. if (cli_RNetShareEnum
  643. (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
  644. return 0;
  645. else
  646. current_share_info = smbfs_info;
  647. }
  648. goto done;
  649. }
  650. /* do regular directory listing */
  651. if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
  652. /* strip share name from dir */
  653. my_dirname = g_strdup (info_dirname + servlen);
  654. *my_dirname = '/';
  655. my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
  656. } else
  657. my_dirname = smbfs_convert_path (info_dirname, TRUE);
  658. DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
  659. DEBUG (6,
  660. ("smbfs_loaddir: cli->share: %s\n",
  661. smbfs_info->conn->cli->share));
  662. DEBUG (6,
  663. ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
  664. /* do file listing: cli_list returns number of files */
  665. if (cli_list
  666. (smbfs_info->conn->cli, my_dirname, attribute,
  667. smbfs_loaddir_helper, NULL) < 0) {
  668. /* cli_list returns -1 if directory empty or cannot read socket */
  669. my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
  670. g_free (my_dirname);
  671. return 0;
  672. }
  673. if (*(my_dirname) == 0)
  674. smbfs_info->dirname = smbfs_info->conn->service;
  675. g_free (my_dirname);
  676. /* do_dskattr(); */
  677. done:
  678. /* current_info->parent = smbfs_info->dirname; */
  679. smbfs_info->current = smbfs_info->entries;
  680. return 1; /* 1 = ok */
  681. }
  682. #ifdef SMBFS_FREE_DIR
  683. static void
  684. smbfs_free_dir (dir_entry *de)
  685. {
  686. if (!de) return;
  687. smbfs_free_dir (de->next);
  688. g_free (de->text);
  689. g_free (de);
  690. }
  691. #endif
  692. /* The readdir routine loads the complete directory */
  693. /* It's too slow to ask the server each time */
  694. /* It now also sends the complete lstat information for each file */
  695. static void *
  696. smbfs_readdir(void *info)
  697. {
  698. static union vfs_dirent smbfs_readdir_data;
  699. static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
  700. opendir_info *smbfs_info = (opendir_info *) info;
  701. DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
  702. if (!smbfs_info->entries)
  703. if (!smbfs_loaddir(smbfs_info))
  704. return NULL;
  705. if (smbfs_info->current == 0) { /* reached end of dir entries */
  706. DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
  707. #ifdef SMBFS_FREE_DIR
  708. smbfs_free_dir(smbfs_info->entries);
  709. smbfs_info->entries = 0;
  710. #endif
  711. return NULL;
  712. }
  713. g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
  714. smbfs_info->current = smbfs_info->current->next;
  715. compute_namelen(&smbfs_readdir_data.dent);
  716. return &smbfs_readdir_data;
  717. }
  718. static int
  719. smbfs_closedir (void *info)
  720. {
  721. opendir_info *smbfs_info = (opendir_info *) info;
  722. /* dir_entry *p, *q; */
  723. DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
  724. /* CLOSE HERE */
  725. /* for (p = smbfs_info->entries; p;){
  726. q = p;
  727. p = p->next;
  728. g_free (q->text);
  729. g_free (q);
  730. }
  731. g_free (info); */
  732. return 0;
  733. }
  734. static int
  735. smbfs_chmod (struct vfs_class *me, const char *path, int mode)
  736. {
  737. (void) me;
  738. DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
  739. /* my_errno = EOPNOTSUPP;
  740. return -1; */ /* cannot chmod on smb filesystem */
  741. return 0; /* make mc happy */
  742. }
  743. static int
  744. smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
  745. {
  746. (void) me;
  747. DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
  748. my_errno = EOPNOTSUPP; /* ready for your labotomy? */
  749. return -1;
  750. }
  751. static int
  752. smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
  753. {
  754. (void) me;
  755. (void) times;
  756. DEBUG(3, ("smbfs_utime(path:%s)\n", path));
  757. my_errno = EOPNOTSUPP;
  758. return -1;
  759. }
  760. static int
  761. smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  762. {
  763. (void) me;
  764. DEBUG (3,
  765. ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
  766. (int) size));
  767. my_errno = EOPNOTSUPP;
  768. return -1; /* no symlinks on smb filesystem? */
  769. }
  770. static int
  771. smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
  772. {
  773. (void) me;
  774. DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
  775. my_errno = EOPNOTSUPP;
  776. return -1; /* no symlinks on smb filesystem? */
  777. }
  778. /* Extract the hostname and username from the path */
  779. /* path is in the form: [user@]hostname/share/remote-dir */
  780. #define smbfs_get_host_and_username(path, host, user, port, pass) \
  781. vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
  782. /*****************************************************
  783. return a connection to a SMB server
  784. current_bucket needs to be set before calling
  785. *******************************************************/
  786. static struct cli_state *
  787. smbfs_do_connect (const char *server, char *share)
  788. {
  789. struct cli_state *c;
  790. struct nmb_name called, calling;
  791. struct in_addr ip;
  792. DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
  793. if (*share == '\\') {
  794. server = share+2;
  795. share = strchr(server,'\\');
  796. if (!share) return NULL;
  797. *share = 0;
  798. share++;
  799. }
  800. make_nmb_name(&calling, global_myname, 0x0);
  801. make_nmb_name(&called , server, current_bucket->name_type);
  802. for (;;) {
  803. ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
  804. /* have to open a new connection */
  805. if (!(c = cli_initialise(NULL))) {
  806. my_errno = ENOMEM;
  807. return NULL;
  808. }
  809. pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
  810. pwd_set_cleartext(&(c->pwd), current_bucket->password);
  811. if ((cli_set_port(c, current_bucket->port) == 0) ||
  812. !cli_connect(c, server, &ip)) {
  813. DEBUG(1, ("Connection to %s failed\n", server));
  814. break;
  815. }
  816. if (!cli_session_request(c, &calling, &called)) {
  817. my_errno = cli_error(c, NULL, &err, NULL);
  818. DEBUG(1, ("session request to %s failed\n", called.name));
  819. cli_shutdown(c);
  820. if (strcmp(called.name, "*SMBSERVER")) {
  821. make_nmb_name(&called , "*SMBSERVER", 0x20);
  822. continue;
  823. }
  824. return NULL;
  825. }
  826. DEBUG(3, (" session request ok\n"));
  827. if (!cli_negprot(c)) {
  828. DEBUG(1, ("protocol negotiation failed\n"));
  829. break;
  830. }
  831. if (!cli_session_setup(c, current_bucket->user,
  832. current_bucket->password, strlen(current_bucket->password),
  833. current_bucket->password, strlen(current_bucket->password),
  834. current_bucket->domain)) {
  835. DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
  836. smbfs_auth_remove (server, share);
  837. break;
  838. }
  839. if (*c->server_domain || *c->server_os || *c->server_type)
  840. DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
  841. c->server_domain,c->server_os,c->server_type));
  842. DEBUG(3, (" session setup ok\n"));
  843. if (!cli_send_tconX(c, share, "?????",
  844. current_bucket->password, strlen(current_bucket->password)+1)) {
  845. DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
  846. break;
  847. }
  848. DEBUG(3, (" tconx ok\n"));
  849. my_errno = 0;
  850. return c;
  851. }
  852. my_errno = cli_error(c, NULL, &err, NULL);
  853. cli_shutdown(c);
  854. return NULL;
  855. }
  856. static int
  857. smbfs_get_master_browser(char **host)
  858. {
  859. static char so_broadcast[] = "SO_BROADCAST";
  860. int count;
  861. struct in_addr *ip_list, bcast_addr;
  862. /* does port = 137 for win95 master browser? */
  863. int fd= open_socket_in( SOCK_DGRAM, 0, 3,
  864. interpret_addr(lp_socket_address()), True );
  865. if (fd == -1)
  866. return 0;
  867. set_socket_options(fd, so_broadcast);
  868. ip_list = iface_bcast(ipzero);
  869. bcast_addr = *ip_list;
  870. if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
  871. True, bcast_addr, &count, NULL))) {
  872. if (!count)
  873. return 0;
  874. /* just return first master browser */
  875. *host = g_strdup(inet_ntoa(ip_list[0]));
  876. return 1;
  877. }
  878. return 0;
  879. }
  880. static void
  881. smbfs_free_bucket (smbfs_connection *bucket)
  882. {
  883. g_free (bucket->host);
  884. g_free (bucket->service);
  885. g_free (bucket->domain);
  886. g_free (bucket->user);
  887. wipe_password (bucket->password);
  888. g_free (bucket->home);
  889. memset (bucket, 0, sizeof (smbfs_connection));
  890. }
  891. static smbfs_connection *
  892. smbfs_get_free_bucket (void)
  893. {
  894. int i;
  895. for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
  896. if (!smbfs_connections [i].cli) return &smbfs_connections [i];
  897. { /* search for most dormant connection */
  898. int oldest = 0; /* index */
  899. time_t oldest_time = smbfs_connections[0].last_use;
  900. for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
  901. if (smbfs_connections[i].last_use < oldest_time) {
  902. oldest_time = smbfs_connections[i].last_use;
  903. oldest = i;
  904. }
  905. }
  906. cli_shutdown(smbfs_connections[oldest].cli);
  907. smbfs_free_bucket (&smbfs_connections[oldest]);
  908. return &smbfs_connections[oldest];
  909. }
  910. /* This can't happend, since we have checked for max connections before */
  911. vfs_die("Internal error: smbfs_get_free_bucket");
  912. return 0; /* shut up, stupid gcc */
  913. }
  914. /* This routine keeps track of open connections */
  915. /* Returns a connected socket to host */
  916. static smbfs_connection *
  917. smbfs_open_link (char *host, char *path, const char *user, int *port,
  918. char *this_pass)
  919. {
  920. int i;
  921. smbfs_connection *bucket;
  922. pstring service;
  923. struct in_addr *dest_ip = NULL;
  924. DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
  925. if (strcmp (host, path) == 0) /* if host & path are same: */
  926. pstrcpy (service, IPC); /* setup for browse */
  927. else { /* get share name from path, path starts with server name */
  928. char *p;
  929. if ((p = strchr (path, '/'))) /* get share aka */
  930. pstrcpy (service, ++p); /* service name from path */
  931. else
  932. pstrcpy (service, "");
  933. /* now check for trailing directory/filenames */
  934. p = strchr (service, '/');
  935. if (p)
  936. *p = 0; /* cut off dir/files: sharename only */
  937. if (!*service)
  938. pstrcpy (service, IPC); /* setup for browse */
  939. DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
  940. }
  941. if (got_user)
  942. user = username; /* global from getenv */
  943. /* Is the link actually open? */
  944. for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
  945. if (!smbfs_connections[i].cli)
  946. continue;
  947. if ((strcmp (host, smbfs_connections[i].host) == 0) &&
  948. (strcmp (user, smbfs_connections[i].user) == 0) &&
  949. (strcmp (service, smbfs_connections[i].service) == 0)) {
  950. int retries = 0;
  951. BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
  952. /* check if this connection has died */
  953. while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
  954. if (!smbfs_reconnect (&smbfs_connections[i], &retries))
  955. return 0;
  956. }
  957. DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
  958. current_bucket = &smbfs_connections[i];
  959. smbfs_connections[i].last_use = time (NULL);
  960. return &smbfs_connections[i];
  961. }
  962. /* connection not found, find if we have ip for new connection */
  963. if (strcmp (host, smbfs_connections[i].host) == 0)
  964. dest_ip = &smbfs_connections[i].cli->dest_ip;
  965. }
  966. /* make new connection */
  967. bucket = smbfs_get_free_bucket ();
  968. bucket->name_type = 0x20;
  969. bucket->home = 0;
  970. bucket->port = *port;
  971. bucket->have_ip = False;
  972. if (dest_ip) {
  973. bucket->have_ip = True;
  974. bucket->dest_ip = *dest_ip;
  975. }
  976. current_bucket = bucket;
  977. bucket->user = g_strdup (user);
  978. bucket->service = g_strdup (service);
  979. if (!(*host)) { /* if blank host name, browse for servers */
  980. if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
  981. return 0; /* could not find master browser? */
  982. g_free (host);
  983. bucket->host = g_strdup (""); /* blank host means master browser */
  984. } else
  985. bucket->host = g_strdup (host);
  986. if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
  987. user, this_pass, 1))
  988. return 0;
  989. /* connect to share */
  990. while (!(bucket->cli = smbfs_do_connect (host, service))) {
  991. if (my_errno != EPERM)
  992. return 0;
  993. message (1, MSG_ERROR, _(" Authentication failed "));
  994. /* authentication failed, try again */
  995. smbfs_auth_remove (bucket->host, bucket->service);
  996. if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
  997. return 0;
  998. }
  999. smbfs_open_connections++;
  1000. DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
  1001. smbfs_open_connections));
  1002. return bucket;
  1003. }
  1004. static char *
  1005. smbfs_get_path (smbfs_connection ** sc, const char *path)
  1006. {
  1007. char *user, *host, *remote_path, *pass;
  1008. int port = SMB_PORT;
  1009. DEBUG (3, ("smbfs_get_path(%s)\n", path));
  1010. if (strncmp (path, URL_HEADER, HEADER_LEN))
  1011. return NULL;
  1012. path += HEADER_LEN;
  1013. if (*path == '/') /* '/' leading server name */
  1014. path++; /* probably came from server browsing */
  1015. if ((remote_path =
  1016. smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
  1017. if ((*sc =
  1018. smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
  1019. g_free (remote_path);
  1020. remote_path = NULL;
  1021. }
  1022. g_free (host);
  1023. g_free (user);
  1024. if (pass)
  1025. wipe_password (pass);
  1026. if (!remote_path)
  1027. return NULL;
  1028. /* NOTE: tildes are deprecated. See ftpfs.c */
  1029. {
  1030. int f = !strcmp (remote_path, "/~");
  1031. if (f || !strncmp (remote_path, "/~/", 3)) {
  1032. char *s;
  1033. s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
  1034. g_free (remote_path);
  1035. return s;
  1036. }
  1037. }
  1038. return remote_path;
  1039. }
  1040. #if 0
  1041. static int
  1042. is_error (int result, int errno_num)
  1043. {
  1044. if (!(result == -1))
  1045. return my_errno = 0;
  1046. else
  1047. my_errno = errno_num;
  1048. return 1;
  1049. }
  1050. #endif
  1051. static void *
  1052. smbfs_opendir (struct vfs_class *me, const char *dirname)
  1053. {
  1054. opendir_info *smbfs_info;
  1055. smbfs_connection *sc;
  1056. char *remote_dir;
  1057. (void) me;
  1058. DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
  1059. if (!(remote_dir = smbfs_get_path (&sc, dirname)))
  1060. return NULL;
  1061. /* FIXME: where freed? */
  1062. smbfs_info = g_new (opendir_info, 1);
  1063. smbfs_info->server_list = FALSE;
  1064. smbfs_info->path = g_strdup(dirname); /* keep original */
  1065. smbfs_info->dirname = remote_dir;
  1066. smbfs_info->conn = sc;
  1067. smbfs_info->entries = 0;
  1068. smbfs_info->current = 0;
  1069. return smbfs_info;
  1070. }
  1071. static int
  1072. smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
  1073. {
  1074. dir_entry *dentry;
  1075. const char *p;
  1076. (void) server_url;
  1077. if ((p = strrchr (path, '/')))
  1078. path = p + 1; /* advance until last '/' */
  1079. if (!current_info->entries) {
  1080. if (!smbfs_loaddir (current_info)) /* browse host */
  1081. return -1;
  1082. }
  1083. if (current_info->server_list == True) {
  1084. dentry = current_info->entries;
  1085. DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
  1086. while (dentry) {
  1087. if (strcmp (dentry->text, path) == 0) {
  1088. DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
  1089. dentry->text, (int)dentry->my_stat.st_mode));
  1090. memcpy (buf, &dentry->my_stat, sizeof (struct stat));
  1091. return 0;
  1092. }
  1093. dentry = dentry->next;
  1094. }
  1095. }
  1096. my_errno = ENOENT;
  1097. return -1;
  1098. }
  1099. static int
  1100. smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
  1101. {
  1102. dir_entry *dentry;
  1103. if (strlen (path) < strlen (server_url))
  1104. return -1;
  1105. if (!current_share_info) { /* Server was not stat()ed */
  1106. /* Make sure there is such share at server */
  1107. smbfs_connection *sc;
  1108. char *p;
  1109. p = smbfs_get_path (&sc, path);
  1110. g_free (p);
  1111. if (p) {
  1112. memset (buf, 0, sizeof (*buf));
  1113. /* show this as dir */
  1114. buf->st_mode =
  1115. (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
  1116. S_IXOTH) & myumask;
  1117. return 0;
  1118. }
  1119. return -1;
  1120. }
  1121. path += strlen (server_url); /* we only want share name */
  1122. path++;
  1123. if (*path == '/') /* '/' leading server name */
  1124. path++; /* probably came from server browsing */
  1125. if (!current_share_info->entries) {
  1126. if (!smbfs_loaddir (current_share_info)) /* browse host */
  1127. return -1;
  1128. }
  1129. dentry = current_share_info->entries;
  1130. DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
  1131. while (dentry) {
  1132. if (strcmp (dentry->text, path) == 0) {
  1133. DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
  1134. dentry->text, (int) dentry->my_stat.st_mode));
  1135. memcpy (buf, &dentry->my_stat, sizeof (struct stat));
  1136. return 0;
  1137. }
  1138. dentry = dentry->next;
  1139. }
  1140. my_errno = ENOENT;
  1141. return -1;
  1142. }
  1143. /* stat a single file, smbfs_get_remote_stat callback */
  1144. static dir_entry *single_entry;
  1145. /* stat a single file */
  1146. static int
  1147. smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
  1148. {
  1149. uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
  1150. char *mypath;
  1151. DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
  1152. mypath = smbfs_convert_path (path, FALSE);
  1153. #if 0 /* single_entry is never free()d now. And only my_stat is used */
  1154. single_entry = g_new (dir_entry, 1);
  1155. single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
  1156. single_entry->next = 0;
  1157. #endif
  1158. if (!single_entry)
  1159. single_entry = g_new0 (dir_entry, 1);
  1160. if (cli_list
  1161. (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
  1162. my_errno = ENOENT;
  1163. g_free (mypath);
  1164. return -1; /* cli_list returns number of files */
  1165. }
  1166. memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
  1167. /* don't free here, use for smbfs_fstat() */
  1168. /* g_free(single_entry->text);
  1169. g_free(single_entry); */
  1170. g_free (mypath);
  1171. return 0;
  1172. }
  1173. static int
  1174. smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
  1175. {
  1176. while (dentry) {
  1177. if (strcmp(text, dentry->text) == 0) {
  1178. memcpy(buf, &dentry->my_stat, sizeof(struct stat));
  1179. memcpy(&single_entry->my_stat, &dentry->my_stat,
  1180. sizeof(struct stat));
  1181. return 0;
  1182. }
  1183. dentry = dentry->next;
  1184. }
  1185. return -1;
  1186. }
  1187. static int
  1188. smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
  1189. {
  1190. char *p;
  1191. #if 0
  1192. dir_entry *dentry = current_info->entries;
  1193. #endif
  1194. const char *mypath = path;
  1195. mypath++; /* cut off leading '/' */
  1196. if ((p = strrchr (mypath, '/')))
  1197. mypath = p + 1; /* advance until last file/dir name */
  1198. DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
  1199. mypath, current_info->dirname));
  1200. #if 0
  1201. if (!dentry) {
  1202. DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
  1203. current_info->dirname, path));
  1204. return -1;
  1205. }
  1206. #endif
  1207. if (!single_entry) /* when found, this will be written too */
  1208. single_entry = g_new (dir_entry, 1);
  1209. if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
  1210. return 0;
  1211. }
  1212. /* now try to identify mypath as PARENT dir */
  1213. {
  1214. char *mdp;
  1215. char *mydir;
  1216. mdp = mydir = g_strdup (current_info->dirname);
  1217. if ((p = strrchr (mydir, '/')))
  1218. *p = 0; /* advance util last '/' */
  1219. if ((p = strrchr (mydir, '/')))
  1220. mydir = p + 1; /* advance util last '/' */
  1221. if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
  1222. memset (buf, 0, sizeof (struct stat));
  1223. buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
  1224. memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
  1225. g_free (mdp);
  1226. DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
  1227. return 0;
  1228. }
  1229. g_free (mdp);
  1230. }
  1231. /* now try to identify as CURRENT dir? */
  1232. {
  1233. char *dnp = current_info->dirname;
  1234. DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
  1235. mypath, current_info->dirname));
  1236. if (*dnp == '/')
  1237. dnp++;
  1238. else {
  1239. return -1;
  1240. }
  1241. if (strcmp (mypath, dnp) == 0) {
  1242. memset (buf, 0, sizeof (struct stat));
  1243. buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
  1244. memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
  1245. DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
  1246. return 0;
  1247. }
  1248. }
  1249. DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
  1250. current_info->dirname));
  1251. /* try to find this in the PREVIOUS listing */
  1252. if (previous_info) {
  1253. if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
  1254. return 0;
  1255. DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
  1256. previous_info->dirname));
  1257. }
  1258. /* try to find this in the SHARE listing */
  1259. if (current_share_info) {
  1260. if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
  1261. return 0;
  1262. DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
  1263. current_share_info->dirname));
  1264. }
  1265. /* try to find this in the SERVER listing */
  1266. if (current_server_info) {
  1267. if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
  1268. return 0;
  1269. DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
  1270. current_server_info->dirname));
  1271. }
  1272. /* nothing found. get stat file info from server */
  1273. return smbfs_get_remote_stat (sc, path, buf);
  1274. }
  1275. static int
  1276. smbfs_chdir (struct vfs_class *me, const char *path)
  1277. {
  1278. char *remote_dir;
  1279. smbfs_connection *sc;
  1280. (void) me;
  1281. DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
  1282. if (!(remote_dir = smbfs_get_path (&sc, path)))
  1283. return -1;
  1284. g_free (remote_dir);
  1285. return 0;
  1286. }
  1287. static int
  1288. smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
  1289. {
  1290. void *info;
  1291. char *mypath, *p;
  1292. mypath = g_strdup(path);
  1293. p = strrchr(mypath, '/');
  1294. if (p > mypath)
  1295. *p = 0;
  1296. DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
  1297. smbfs_chdir(me, mypath);
  1298. info = smbfs_opendir (me, mypath);
  1299. g_free(mypath);
  1300. if (!info)
  1301. return -1;
  1302. smbfs_readdir(info);
  1303. smbfs_loaddir(info);
  1304. return 0;
  1305. }
  1306. static int
  1307. smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
  1308. {
  1309. smbfs_connection *sc;
  1310. pstring server_url;
  1311. char *service, *pp, *at;
  1312. const char *p;
  1313. DEBUG (3, ("smbfs_stat(path:%s)\n", path));
  1314. if (!current_info) {
  1315. DEBUG (1, ("current_info = NULL: "));
  1316. if (smbfs_loaddir_by_name (me, path) < 0)
  1317. return -1;
  1318. }
  1319. /* check if stating server */
  1320. p = path;
  1321. if (strncmp (p, URL_HEADER, HEADER_LEN)) {
  1322. DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
  1323. p, URL_HEADER, HEADER_LEN));
  1324. return -1;
  1325. }
  1326. p += HEADER_LEN;
  1327. if (*p == '/')
  1328. p++;
  1329. pp = strchr (p, '/'); /* advance past next '/' */
  1330. at = strchr (p, '@');
  1331. pstrcpy (server_url, URL_HEADER);
  1332. if (at && at < pp) { /* user@server */
  1333. char *z = &(server_url[sizeof (server_url) - 1]);
  1334. const char *s = p;
  1335. at = &(server_url [HEADER_LEN]) + (at - p + 1);
  1336. if (z > at)
  1337. z = at;
  1338. at = &(server_url [HEADER_LEN]);
  1339. while (at < z)
  1340. *at++ = *s++;
  1341. *z = 0;
  1342. }
  1343. pstrcat (server_url, current_bucket->host);
  1344. if (!pp) {
  1345. if (!current_info->server_list) {
  1346. if (smbfs_loaddir_by_name (me, path) < 0)
  1347. return -1;
  1348. }
  1349. return smbfs_fake_server_stat (server_url, path, buf);
  1350. }
  1351. if (!strchr (++pp, '/')) {
  1352. return smbfs_fake_share_stat (server_url, path, buf);
  1353. }
  1354. /* stating inside share at this point */
  1355. if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
  1356. return -1;
  1357. {
  1358. int hostlen = strlen (current_bucket->host);
  1359. char *ppp = service + strlen (service) - hostlen;
  1360. char *sp = server_url + strlen (server_url) - hostlen;
  1361. if (strcmp (sp, ppp) == 0) {
  1362. /* make server name appear as directory */
  1363. DEBUG (1, ("smbfs_stat: showing server as directory\n"));
  1364. memset (buf, 0, sizeof (struct stat));
  1365. buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
  1366. g_free (service);
  1367. return 0;
  1368. }
  1369. }
  1370. /* check if current_info is in share requested */
  1371. p = service;
  1372. pp = strchr (p, '/');
  1373. if (pp) {
  1374. p = ++pp; /* advance past server name */
  1375. pp = strchr (p, '/');
  1376. }
  1377. if (pp)
  1378. *pp = 0; /* cut off everthing after service name */
  1379. else
  1380. p = IPC; /* browsing for services */
  1381. pp = current_info->dirname;
  1382. if (*pp == '/')
  1383. pp++;
  1384. if (strncmp (p, pp, strlen (p)) != 0) {
  1385. DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
  1386. if (smbfs_loaddir_by_name (me, path) < 0) {
  1387. g_free (service);
  1388. return -1;
  1389. }
  1390. DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
  1391. }
  1392. g_free (service);
  1393. /* stat dirs & files under shares now */
  1394. return smbfs_get_stat_info (sc, path, buf);
  1395. }
  1396. #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
  1397. static int
  1398. smbfs_lseek (void *data, off_t offset, int whence)
  1399. {
  1400. smbfs_handle *info = (smbfs_handle *) data;
  1401. size_t size;
  1402. DEBUG (3,
  1403. ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
  1404. (int) info->nread, (int) offset, whence));
  1405. switch (whence) {
  1406. case SEEK_SET:
  1407. info->nread = offset;
  1408. break;
  1409. case SEEK_CUR:
  1410. info->nread += offset;
  1411. break;
  1412. case SEEK_END:
  1413. if (!cli_qfileinfo (info->cli, info->fnum,
  1414. NULL, &size, NULL, NULL, NULL,
  1415. NULL, NULL) &&
  1416. !cli_getattrE (info->cli, info->fnum,
  1417. NULL, &size, NULL, NULL, NULL)) {
  1418. errno = EINVAL;
  1419. return -1;
  1420. }
  1421. info->nread = size + offset;
  1422. break;
  1423. }
  1424. return info->nread;
  1425. }
  1426. static int
  1427. smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
  1428. {
  1429. (void) me;
  1430. DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
  1431. my_errno = EOPNOTSUPP;
  1432. return -1;
  1433. }
  1434. static int
  1435. smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
  1436. {
  1437. smbfs_connection *sc;
  1438. char *remote_file;
  1439. char *cpath;
  1440. (void) me;
  1441. DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
  1442. if ((remote_file = smbfs_get_path (&sc, path)) == 0)
  1443. return -1;
  1444. g_free (remote_file);
  1445. cpath = smbfs_convert_path (path, FALSE);
  1446. if (!cli_mkdir (sc->cli, cpath)) {
  1447. my_errno = cli_error (sc->cli, NULL, &err, NULL);
  1448. message (1, MSG_ERROR, _(" Error %s creating directory %s "),
  1449. cli_errstr (sc->cli), CNV_LANG (cpath));
  1450. g_free (cpath);
  1451. return -1;
  1452. }
  1453. g_free (cpath);
  1454. return 0;
  1455. }
  1456. static int
  1457. smbfs_rmdir (struct vfs_class *me, const char *path)
  1458. {
  1459. smbfs_connection *sc;
  1460. char *remote_file;
  1461. char *cpath;
  1462. (void) me;
  1463. DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
  1464. if ((remote_file = smbfs_get_path (&sc, path)) == 0)
  1465. return -1;
  1466. g_free (remote_file);
  1467. cpath = smbfs_convert_path (path, FALSE);
  1468. if (!cli_rmdir(sc->cli, cpath)) {
  1469. my_errno = cli_error(sc->cli, NULL, &err, NULL);
  1470. message (1, MSG_ERROR, _(" Error %s removing directory %s "),
  1471. cli_errstr(sc->cli), CNV_LANG(cpath));
  1472. g_free (cpath);
  1473. return -1;
  1474. }
  1475. g_free (cpath);
  1476. return 0;
  1477. }
  1478. static int
  1479. smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
  1480. {
  1481. (void) me;
  1482. DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
  1483. my_errno = EOPNOTSUPP;
  1484. return -1;
  1485. }
  1486. static void
  1487. smbfs_free (vfsid id)
  1488. {
  1489. DEBUG (3, ("smbfs_free(%p)\n", id));
  1490. smbfs_auth_free_all ();
  1491. }
  1492. /* Gives up on a socket and reopens the connection, the child own the socket
  1493. * now
  1494. */
  1495. static void
  1496. smbfs_forget (const char *path)
  1497. {
  1498. char *host, *user, *p;
  1499. int port, i;
  1500. if (strncmp (path, URL_HEADER, HEADER_LEN))
  1501. return;
  1502. DEBUG (3, ("smbfs_forget(path:%s)\n", path));
  1503. path += 6;
  1504. if (path[0] == '/' && path[1] == '/')
  1505. path += 2;
  1506. if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
  1507. g_free (p);
  1508. for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
  1509. if (smbfs_connections[i].cli
  1510. && (strcmp (host, smbfs_connections[i].host) == 0)
  1511. && (strcmp (user, smbfs_connections[i].user) == 0)
  1512. && (port == smbfs_connections[i].port)) {
  1513. /* close socket: the child owns it now */
  1514. cli_shutdown (smbfs_connections[i].cli);
  1515. /* reopen the connection */
  1516. smbfs_connections[i].cli =
  1517. smbfs_do_connect (host, smbfs_connections[i].service);
  1518. }
  1519. }
  1520. }
  1521. g_free (host);
  1522. g_free (user);
  1523. }
  1524. static int
  1525. smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  1526. {
  1527. (void) me;
  1528. (void) arg;
  1529. DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
  1530. switch (ctlop) {
  1531. case VFS_SETCTL_FORGET:
  1532. smbfs_forget (path);
  1533. return 0;
  1534. }
  1535. return 0;
  1536. }
  1537. static smbfs_handle *
  1538. smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
  1539. {
  1540. size_t size;
  1541. (void) mode;
  1542. if (flags & O_TRUNC) /* if it exists truncate to zero */
  1543. DEBUG (3, ("smbfs_open: O_TRUNC\n"));
  1544. remote_handle->fnum =
  1545. #if 1 /* Don't play with flags, it is cli_open() headache */
  1546. cli_open (remote_handle->cli, rname, flags, DENY_NONE);
  1547. #else /* What's a reasons to has this code ? */
  1548. cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
  1549. || (flags ==
  1550. (O_WRONLY | O_APPEND))) ?
  1551. flags : O_RDONLY, DENY_NONE);
  1552. #endif
  1553. if (remote_handle->fnum == -1) {
  1554. message (1, MSG_ERROR, _(" %s opening remote file %s "),
  1555. cli_errstr (remote_handle->cli), CNV_LANG (rname));
  1556. DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
  1557. rname, cli_errstr (remote_handle->cli)));
  1558. my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
  1559. return NULL;
  1560. }
  1561. if (flags & O_CREAT)
  1562. return remote_handle;
  1563. if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
  1564. &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
  1565. NULL)
  1566. && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
  1567. &remote_handle->attr, &size, NULL, NULL, NULL)) {
  1568. message (1, MSG_ERROR, " getattrib: %s ",
  1569. cli_errstr (remote_handle->cli));
  1570. DEBUG (1,
  1571. ("smbfs_open(rname:%s) getattrib:%s\n", rname,
  1572. cli_errstr (remote_handle->cli)));
  1573. my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
  1574. cli_close (remote_handle->cli, remote_handle->fnum);
  1575. return NULL;
  1576. }
  1577. if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
  1578. && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
  1579. cli_close (remote_handle->cli, remote_handle->fnum);
  1580. return NULL;
  1581. }
  1582. return remote_handle;
  1583. }
  1584. static void *
  1585. smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
  1586. {
  1587. char *remote_file;
  1588. void *ret;
  1589. smbfs_connection *sc;
  1590. smbfs_handle *remote_handle;
  1591. (void) me;
  1592. DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
  1593. if (!(remote_file = smbfs_get_path (&sc, file)))
  1594. return 0;
  1595. remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
  1596. remote_handle = g_new (smbfs_handle, 2);
  1597. remote_handle->cli = sc->cli;
  1598. remote_handle->nread = 0;
  1599. ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
  1600. g_free (remote_file);
  1601. if (!ret)
  1602. g_free (remote_handle);
  1603. return ret;
  1604. }
  1605. static int
  1606. smbfs_unlink (struct vfs_class *me, const char *path)
  1607. {
  1608. smbfs_connection *sc;
  1609. char *remote_file;
  1610. (void) me;
  1611. if ((remote_file = smbfs_get_path (&sc, path)) == 0)
  1612. return -1;
  1613. remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
  1614. if (!cli_unlink(sc->cli, remote_file)) {
  1615. message (1, MSG_ERROR, _(" %s removing remote file %s "),
  1616. cli_errstr(sc->cli), CNV_LANG(remote_file));
  1617. g_free (remote_file);
  1618. return -1;
  1619. }
  1620. g_free (remote_file);
  1621. return 0;
  1622. }
  1623. static int
  1624. smbfs_rename (struct vfs_class *me, const char *a, const char *b)
  1625. {
  1626. smbfs_connection *sc;
  1627. char *ra, *rb;
  1628. int retval;
  1629. (void) me;
  1630. if ((ra = smbfs_get_path (&sc, a)) == 0)
  1631. return -1;
  1632. if ((rb = smbfs_get_path (&sc, b)) == 0) {
  1633. g_free (ra);
  1634. return -1;
  1635. }
  1636. ra = free_after (smbfs_convert_path (ra, FALSE), ra);
  1637. rb = free_after (smbfs_convert_path (rb, FALSE), rb);
  1638. retval = cli_rename(sc->cli, ra, rb);
  1639. g_free (ra);
  1640. g_free (rb);
  1641. if (!retval) {
  1642. message (1, MSG_ERROR, _(" %s renaming files\n"),
  1643. cli_errstr(sc->cli));
  1644. return -1;
  1645. }
  1646. return 0;
  1647. }
  1648. static int
  1649. smbfs_fstat (void *data, struct stat *buf)
  1650. {
  1651. smbfs_handle *remote_handle = (smbfs_handle *)data;
  1652. DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
  1653. /* use left over from previous smbfs_get_remote_stat, if available */
  1654. if (single_entry)
  1655. memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
  1656. else { /* single_entry not set up: bug */
  1657. my_errno = EFAULT;
  1658. return -EFAULT;
  1659. }
  1660. return 0;
  1661. }
  1662. void
  1663. init_smbfs (void)
  1664. {
  1665. vfs_smbfs_ops.name = "smbfs";
  1666. vfs_smbfs_ops.prefix = "smb:";
  1667. vfs_smbfs_ops.flags = VFSF_NOLINKS;
  1668. vfs_smbfs_ops.init = smbfs_init;
  1669. vfs_smbfs_ops.fill_names = smbfs_fill_names;
  1670. vfs_smbfs_ops.open = smbfs_open;
  1671. vfs_smbfs_ops.close = smbfs_close;
  1672. vfs_smbfs_ops.read = smbfs_read;
  1673. vfs_smbfs_ops.write = smbfs_write;
  1674. vfs_smbfs_ops.opendir = smbfs_opendir;
  1675. vfs_smbfs_ops.readdir = smbfs_readdir;
  1676. vfs_smbfs_ops.closedir = smbfs_closedir;
  1677. vfs_smbfs_ops.stat = smbfs_stat;
  1678. vfs_smbfs_ops.lstat = smbfs_lstat;
  1679. vfs_smbfs_ops.fstat = smbfs_fstat;
  1680. vfs_smbfs_ops.chmod = smbfs_chmod;
  1681. vfs_smbfs_ops.chown = smbfs_chown;
  1682. vfs_smbfs_ops.utime = smbfs_utime;
  1683. vfs_smbfs_ops.readlink = smbfs_readlink;
  1684. vfs_smbfs_ops.symlink = smbfs_symlink;
  1685. vfs_smbfs_ops.link = smbfs_link;
  1686. vfs_smbfs_ops.unlink = smbfs_unlink;
  1687. vfs_smbfs_ops.rename = smbfs_rename;
  1688. vfs_smbfs_ops.chdir = smbfs_chdir;
  1689. vfs_smbfs_ops.ferrno = smbfs_errno;
  1690. vfs_smbfs_ops.lseek = smbfs_lseek;
  1691. vfs_smbfs_ops.mknod = smbfs_mknod;
  1692. vfs_smbfs_ops.free = smbfs_free;
  1693. vfs_smbfs_ops.mkdir = smbfs_mkdir;
  1694. vfs_smbfs_ops.rmdir = smbfs_rmdir;
  1695. vfs_smbfs_ops.setctl = smbfs_setctl;
  1696. vfs_register_class (&vfs_smbfs_ops);
  1697. }