Check credentials at startup, prompting three times for a password. Version 20081220 (stable). hg ignore usmb. - susmb - mounting of SMB/CIFS shares via FUSE
 (HTM) git clone git://git.codemadness.org/susmb
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 51e3a8d601ab4dd00f31d332c4b752c7294c5aad
 (DIR) parent e0773f60e1fbeda2e34d77b43e6eb4758728fd73
 (HTM) Author: Geoff Johnstone <geoffSHEEP.johnstoneFROG@googlemail.com>
       Date:   Sat, 20 Dec 2008 12:27:00 +0000
       
       Check credentials at startup, prompting three times for a password.
       Version 20081220 (stable).
       hg ignore usmb.
       
       Diffstat:
         M .hgignore                           |       1 +
         M conffile.c                          |       6 ++----
         M usmb.c                              |      73 ++++++++++++++++++++++++++++---
         M version.h                           |       4 ++--
       
       4 files changed, 73 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/.hgignore b/.hgignore
       @@ -1 +1,2 @@
        config.rng.h
       +usmb
 (DIR) diff --git a/conffile.c b/conffile.c
       @@ -23,7 +23,6 @@
        #include <sys/stat.h>
        #include <fcntl.h>
        #include <unistd.h>
       -#include "password.h"
        #include "utils.h"
        #include "xml.h"
        #include "config.rng.h"
       @@ -151,9 +150,8 @@ bool conffile_get_mount (const char *filename, const char *key,
            (void)do_xpath_text (ctx, "credentials", creds, "domain", domain);
            if (!do_xpath_text (ctx, "credentials", creds, "username", username)) break;
        
       -    if (!do_xpath_text (ctx, "credentials", creds, "password", password) &&
       -        !password_read (password))
       -      break;
       +    if (!do_xpath_text (ctx, "credentials", creds, "password", password))
       +      *password = NULL;
        
            xmlXPathFreeContext (ctx);
            xmlFreeDoc (doc);
 (DIR) diff --git a/usmb.c b/usmb.c
       @@ -20,6 +20,7 @@
        #include "samba30-compat.h"
        #include <fuse.h>
        #include <assert.h>
       +#include <errno.h>
        #include <stdarg.h>
        #include <stdbool.h>
        #include <stddef.h>
       @@ -28,6 +29,7 @@
        #include <string.h>
        #include "conffile.h"
        #include "options.h"
       +#include "password.h"
        #include "usmb.h"
        #include "usmb_dir.h"
        #include "usmb_file.h"
       @@ -63,7 +65,7 @@ static void auth_fn (const char *srv UNUSED, const char *shr UNUSED,
                             char *pw, int pwlen)
        {
          DEBUG (fprintf (stderr, "Authenticating for \\\\%s\\%s\n", srv, shr));
       -  DEBUG (fprintf (stderr, "Domain: %s; User: %s; Password:%s\n",
       +  DEBUG (fprintf (stderr, "Domain: %s; User: %s; Password: %s\n",
                          domain, username, password));
        
          if (NULL != domain)
       @@ -74,6 +76,15 @@ static void auth_fn (const char *srv UNUSED, const char *shr UNUSED,
        }
        
        
       +static void destroy_smb_context (SMBCCTX *ctx, int shutdown)
       +{
       +  // Samba frees the workgroup and user strings but we want to persist them.
       +  smbc_setWorkgroup (ctx, NULL);
       +  smbc_setUser (ctx, NULL);
       +  smbc_free_context (ctx, shutdown);
       +}
       +
       +
        static bool create_smb_context (char *domain, char *username, SMBCCTX **pctx)
        {
          *pctx = smbc_new_context();
       @@ -92,7 +103,7 @@ static bool create_smb_context (char *domain, char *username, SMBCCTX **pctx)
          if (NULL == smbc_init_context (*pctx))
          {
            perror ("Cannot initialise SMB context");
       -    smbc_free_context (*pctx, 1);
       +    destroy_smb_context (*pctx, 1);
            return false;
          }
        
       @@ -215,6 +226,59 @@ static void free_strings (char *sharename)
        }
        
        
       +static bool check_credentials (void)
       +{
       +  char *url = make_url ("");
       +  if (NULL == url)
       +  {
       +    errno = ENOMEM;
       +    return false;
       +  }
       +
       +  DEBUG (fprintf (stderr, "URL: %s\n", url));
       +
       +  struct stat stat;
       +  bool ret = (0 == (smbc_getFunctionStat (ctx) (ctx, url, &stat)));
       +
       +  free_errno (url);
       +  return ret;
       +}
       +
       +
       +static bool get_context (void)
       +{
       +  ctx = NULL;
       +
       +  unsigned attempts = 3;
       +
       +  while (0 != attempts--)
       +  {
       +    if ((NULL == password) && !password_read (&password))
       +      break;
       +
       +    if (!create_smb_context (domain, username, &ctx))
       +    {
       +      clear_and_free (password);
       +      password = NULL;
       +      ctx = NULL;
       +      break;
       +    }
       +
       +    if (check_credentials())
       +      break;
       +
       +    perror ("Connection failed");
       +    clear_and_free (password);
       +    password = NULL;
       +
       +    destroy_smb_context (ctx, 1);
       +    ctx = NULL;
       +  }
       +
       +  return (NULL != ctx);
       +}
       +
       +
        int main (int argc, char **argv)
        {
          const char *conffile, *mountid;
       @@ -242,8 +306,7 @@ int main (int argc, char **argv)
                                   &domain, &username, &password))
            return EXIT_FAILURE;
        
       -  if (!create_share_name (server, sharename) ||
       -      !create_smb_context (domain, username, &ctx))
       +  if (!create_share_name (server, sharename) || !get_context())
          {
            free_strings (sharename);
            return EXIT_FAILURE;
       @@ -256,7 +319,7 @@ int main (int argc, char **argv)
          build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
          int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops, NULL);
        
       -  smbc_free_context (ctx, 1);
       +  destroy_smb_context (ctx, 1);
          free_strings (sharename);
        
          return ret;
 (DIR) diff --git a/version.h b/version.h
       @@ -19,10 +19,10 @@
        
          #include <stdio.h>
        
       -  #define USMB_VERSION 0x20080626
       +  #define USMB_VERSION 0x20081220
        
          // a - alpha, b - beta, p - pre-release, s - stable
       -  #define USMB_VERSION_STATUS 'b'
       +  #define USMB_VERSION_STATUS 's'
        
          void show_about (FILE *fp);
          void show_version (FILE *fp);