smbfs.c 51 KB

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