smbfs.c 52 KB

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