smbfs.c 48 KB

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