smbfs.c 51 KB

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