Removed the OO-style mount and credentials; conffile exposes one function to retrieve all required functions from the configuration file via XPath. XML functions take/output char * rather than xmlChar *, for laziness reasons. Version 0x20070126. - 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 020a5e9c64b9834132498218e64a93e91bd386d9
 (DIR) parent 7c1c8269f8152a0b6b67183f01b24a80d3b3c37d
 (HTM) Author: geoff <devnull@localhost>
       Date:   Fri, 26 Jan 2007 22:38:09 +0000
       
       Removed the OO-style mount and credentials; conffile exposes one function to
       retrieve all required functions from the configuration file via XPath.
       XML functions take/output char * rather than xmlChar *, for laziness reasons.
       Version 0x20070126.
       
       Diffstat:
         M conffile.c                          |     224 +++++++++----------------------
         M conffile.h                          |      30 +++++-------------------------
         M usmb.c                              |      76 +++++++++++++++----------------
         M version.h                           |       2 +-
         M xml.c                               |      18 +++++++++---------
         M xml.h                               |       8 ++++----
       
       6 files changed, 119 insertions(+), 239 deletions(-)
       ---
 (DIR) diff --git a/conffile.c b/conffile.c
       @@ -20,206 +20,108 @@
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       -#include "conffile.h"
        #include "utils.h"
        #include "xml.h"
        #include "config.rng.h"
        
        
       -struct conffile {
       -  xmlDocPtr doc;
       -  xmlXPathContextPtr ctx;
       -};
       -
       -
       -struct credentials * credentials_new (const char *domain,
       -                                      const char *username,
       -                                      const char *password)
       -{
       -  struct credentials *creds = malloc (sizeof (struct credentials));
       -
       -  if (NULL == creds)
       -    return NULL;
       -  
       -  creds->domain = domain;
       -  creds->username = username;
       -  creds->password = password;
       -
       -  return creds;
       -}
       -
       -
       -void credentials_destroy (struct credentials *creds)
       -{
       -  assert (creds != NULL);
       -  free (creds);
       -}
       -
       -
       -struct mount * mount_new (const char *server, const char *share,
       -                          const char *mountpoint,
       -                          const struct credentials *credentials,
       -                          const char *options)
       -{
       -  struct mount *mount = malloc (sizeof (struct mount));
       -
       -  assert (NULL != credentials);
       -
       -  if (NULL == mount)
       -    return NULL;
       -
       -  mount->server = server;
       -  mount->share = share;
       -  mount->mountpoint = mountpoint;
       -  mount->options = options;
       -  mount->credentials = credentials;
       -
       -  return mount;
       -}
       -
       -
       -void mount_destroy (struct mount *mount)
       -{
       -  assert (NULL != mount);
       -  free (mount);
       -}
       -
       -
       -struct conffile * conffile_new (const char *filename)
       +static bool conffile_read (const char *filename,
       +                           xmlDocPtr *doc,
       +                           xmlXPathContextPtr *ctx)
        {
       -  xmlDocPtr doc;
       -  xmlXPathContextPtr ctx;
       -  struct conffile *cf;
       -  
       -  doc = xmlParseFile (filename);
       -  if (NULL == doc)
       +  *doc = xmlParseFile (filename);
       +  if (NULL == *doc)
          {
            fprintf (stderr, "Cannot parse %s\n", filename);
       -    return NULL;
       +    return false;
          }
        
       -  if (!xml_validate_relaxng (doc, rng_config))
       +  if (!xml_validate_relaxng (*doc, rng_config))
          {
            fprintf (stderr, "%s isn't a valid USMB configuration\n", filename);
       -    xmlFreeDoc (doc);
       -    return NULL;
       +    xmlFreeDoc (*doc);
       +    return false;
          }
        
       -  ctx = xmlXPathNewContext (doc);
       -  if (NULL == ctx)
       +  *ctx = xmlXPathNewContext (*doc);
       +  if (NULL == *ctx)
          {
            fputs ("Cannot create XPath context\n", stderr);
       -    xmlFreeDoc (doc);
       -    return NULL;
       +    xmlFreeDoc (*doc);
       +    return false;
          }
          
       -  cf = malloc (sizeof (struct conffile));
       -
       -  if (NULL == cf)
       -  {
       -    xmlXPathFreeContext (ctx);
       -    xmlFreeDoc (doc);
       -    return NULL;
       -  }
       -
       -  cf->doc = doc;
       -  cf->ctx = ctx;
       -
       -  return cf;
       +  return true;
        }
        
        
       -void conffile_destroy (struct conffile *cf)
       +static bool do_xpath_text (xmlXPathContextPtr ctx,
       +                           const char *parent, const char *id,
       +                           const char *child, char **out)
        {
       -  assert (NULL != cf);
       -
       -  xmlXPathFreeContext (cf->ctx);
       -  xmlFreeDoc (cf->doc);
       -  free (cf);
       +  char xpath[2048];
       +  snprintf (xpath, sizeof (xpath),
       +            "/usmbconfig/%s[@id='%s']/%s/text()", parent, id, child);
       +  return xml_xpath_text (ctx, xpath, (void *)out);
        }
        
        
       -struct mount * conffile_get_mount (struct conffile *cf, const char *key)
       +bool conffile_get_mount (const char *filename, const char *key,
       +                         char **server, char **share,
       +                         char **mountpoint, char **options,
       +                         char **domain, char **username,
       +                         char **password)
        {
       -  char xpath[2048];
       +  xmlDocPtr doc;
       +  xmlXPathContextPtr ctx;
       +  char xp[2048];
          char *creds = NULL;
       -  void *server = NULL, *share = NULL, *mountpoint = NULL, *options = NULL;
       -  void *domain = NULL, *username = NULL, *password = NULL;
       -  bool ok = true;
       -  struct credentials *credentials = NULL;
       -  struct mount *ret = NULL;
        
       -  assert (NULL != cf);
       -  assert (NULL != key);
       +  *server = *share = *mountpoint = *options = NULL;
       +  *domain = *username = *password = NULL;
        
          if (strchr (key, '\''))
       -    return NULL;
       -
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/mount[@id='%s']/server/text()", key);
       -  xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&server) || (ok = false);
       -
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/mount[@id='%s']/share/text()", key);
       -  xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&share) || (ok = false);
       -
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/mount[@id='%s']/mountpoint/text()", key);
       -  xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&mountpoint) || (ok = false);
       +  {
       +    fprintf (stderr, "Invalid share name: %s\n", key);
       +    return false;
       +  }
        
       -  snprintf (xpath, sizeof (xpath), "/usmbconfig/mount[@id='%s']", key);
       -  xml_xpath_attr_value (cf->ctx, BAD_CAST xpath, BAD_CAST "credentials",
       -                        (xmlChar **)&creds) || (ok = false);
       +  if (!conffile_read (filename, &doc, &ctx))
       +    return false;
        
       -  if (!ok)
       -  {
       -    xfree (creds);
       -    xfree (mountpoint);
       -    xfree (share);
       -    xfree (server);
       +  do {
       +    if (!do_xpath_text (ctx, "mount", key, "server", server)) break;
       +    if (!do_xpath_text (ctx, "mount", key, "share", share)) break;
       +    if (!do_xpath_text (ctx, "mount", key, "mountpoint", mountpoint)) break;
       +    do_xpath_text (ctx, "mount", key, "options", options);
        
       -    return NULL;
       -  }
       +    snprintf (xp, sizeof (xp), "/usmbconfig/mount[@id='%s']", key);
       +    if (!xml_xpath_attr_value (ctx, xp, "credentials", &creds)) break;
        
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/credentials[@id='%s']/domain/text()", creds);
       -  (void)xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&domain);
       +    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)) break;
        
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/credentials[@id='%s']/username/text()", creds);
       -  xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&username) || (ok = false);
       +    xmlXPathFreeContext (ctx);
       +    xmlFreeDoc (doc);
        
       -  snprintf (xpath, sizeof (xpath),
       -            "/usmbconfig/credentials[@id='%s']/password/text()", creds);
       -  xml_xpath_text (cf->ctx, BAD_CAST xpath, (void *)&password) || (ok = false);
       +    return true;
       +  } while (false);
        
       -  if (ok)
       -  {
       -    credentials = credentials_new (domain, username, password);
       +  fputs ("Invalid configuration.\n", stderr);
        
       -    if (NULL != credentials)
       -    {
       -      ret = mount_new (server, share, mountpoint, credentials, options);
       -      ok = (NULL != ret);
       -    }
       -  }
       +  xfree (*username);
       +  xfree (*password);
       +  xfree (*domain);
       +  xfree (creds);
       +  xfree (*options);
       +  xfree (*mountpoint);
       +  xfree (*share);
       +  xfree (*server);
        
       -  if (!ok)
       -  {
       -    if (NULL != credentials)
       -      credentials_destroy (credentials);
       -
       -    xfree (password);
       -    xfree (domain);
       -    xfree (username);
       -    xfree (creds);
       -    xfree (mountpoint);
       -    xfree (share);
       -    xfree (server);
       -
       -    return NULL;
       -  }
       +  xmlXPathFreeContext (ctx);
       +  xmlFreeDoc (doc);
        
       -  return ret;
       +  return false;
        }
        
 (DIR) diff --git a/conffile.h b/conffile.h
       @@ -21,29 +21,9 @@
        
          #include <stdbool.h>
        
       -  struct credentials {
       -    const char *domain;
       -    const char *username;
       -    const char *password;
       -  };
       -
       -
       -  struct mount {
       -    const char *server;
       -    const char *share;
       -    const char *mountpoint;
       -    const struct credentials *credentials;
       -    const char *options;
       -  };
       -
       -
       -  struct conffile;
       -
       -
       -  struct conffile * conffile_new (const char *filename);
       -  void conffile_destroy (struct conffile *cf);
       -
       -  struct mount * conffile_get_mount (struct conffile *cf, const char *key);
       -  void mount_destroy (struct mount *mount);
       -
       +  bool conffile_get_mount (const char *filename, const char *key,
       +                           char **server, char **share,
       +                           char **mountpoint, char **options,
       +                           char **domain, char **username,
       +                           char **password);
        #endif
 (DIR) diff --git a/usmb.c b/usmb.c
       @@ -40,7 +40,8 @@
        
        
        SMBCCTX *ctx;
       -static char *share = NULL;
       +static char *server, *share, *mountpoint, *options,
       +            *domain, *username, *password;
        
        
        char * make_url (const char *path)
       @@ -61,20 +62,20 @@ static inline void do_strncpy (char *to, const char *from, int tolen)
        }
        
        
       -static struct mount *mount;
       -
        static void auth_fn (const char *srv, const char *shr, char *wg, int wglen,
                             char *un, int unlen, char *pw, int pwlen)
        {
          (void)srv;
          (void)shr;
          DEBUG (fprintf (stderr, "Authenticating for \\\\%s\\%s\n", srv, shr));
       +  DEBUG (fprintf (stderr, "Domain: %s; User: %s; Password:%s\n",
       +                  domain, username, password));
        
       -  if (NULL != mount->credentials->domain)
       -    do_strncpy (wg, mount->credentials->domain, wglen);
       +  if (NULL != domain)
       +    do_strncpy (wg, domain, wglen);
        
       -  do_strncpy (un, mount->credentials->username, unlen);
       -  do_strncpy (pw, mount->credentials->password, pwlen);
       +  do_strncpy (un, username, unlen);
       +  do_strncpy (pw, password, pwlen);
        }
        
        
       @@ -200,13 +201,11 @@ static bool check_conf_perms (const char *conffile)
        }
        
        
       -static bool create_share_name (struct mount *mount)
       +static bool create_share_name (const char *server, const char *sharename)
        {
       -  assert (NULL != mount->credentials);
       -
          size_t len = strlen ("smb:///") +
       -               strlen (mount->server) +
       -               strlen (mount->share) + 1;
       +               strlen (server) +
       +               strlen (sharename) + 1;
        
          if (NULL == (share = malloc (len)))
          {
       @@ -214,16 +213,29 @@ static bool create_share_name (struct mount *mount)
            return false;
          }
        
       -  snprintf (share, len, "smb://%s/%s", mount->server, mount->share);
       +  snprintf (share, len, "smb://%s/%s", server, sharename);
          DEBUG (fprintf (stderr, "Share URL: %s\n", share));
          return true;
        }
        
        
       +static void free_strings (char *sharename)
       +{
       +  xfree (sharename);
       +  xfree (password);
       +  xfree (username);
       +  xfree (domain);
       +  xfree (options);
       +  xfree (mountpoint);
       +  xfree (share);
       +  xfree (server);
       +}
       +
       +
        int main (int argc, char **argv)
        {
       -  const char *conffile;
       -  const char *mountid;
       +  const char *conffile, *mountid;
       +  char *sharename = NULL;
        
          {
            static char conf[256];
       @@ -234,43 +246,29 @@ int main (int argc, char **argv)
          if (!parse_args (&argc, &argv, &mountid, &conffile))
            return EXIT_FAILURE;
        
       -  if (!check_conf_perms (conffile))
       +  if (!check_conf_perms (conffile) ||
       +      !conffile_get_mount (conffile, mountid,
       +                           &server, &sharename, &mountpoint, &options,
       +                           &domain, &username, &password))
            return EXIT_FAILURE;
        
       -  struct conffile *cf = conffile_new (conffile);
       -  if (NULL == cf)
       -    return EXIT_FAILURE;
       -
       -  mount = conffile_get_mount (cf, mountid);
       -  if (NULL == mount)
       -  {
       -    fprintf (stderr, "Mount %s not defined in configuration file %s\n",
       -             mountid, conffile);
       -    conffile_destroy (cf);
       -    return EXIT_FAILURE;
       -  }
       -
       -  if (!create_share_name (mount) ||
       -      !create_smb_context ((char *)mount->credentials->domain,
       -                           (char *)mount->credentials->username, &ctx))
       +  if (!create_share_name (server, sharename) ||
       +      !create_smb_context (domain, username, &ctx))
          {
       -    mount_destroy (mount);
       -    conffile_destroy (cf);
       +    free_strings (sharename);
            return EXIT_FAILURE;
          }
        
       -  DEBUG (fprintf (stderr, "Username: %s\\%s\n",
       -                  mount->credentials->domain, mount->credentials->username));
       +  DEBUG (fprintf (stderr, "Username: %s\\%s\n", domain, username));
          show_about (stdout);
        
          int fuse_argc;
          char **fuse_argv;
       -  build_fuse_args (mount->options, mount->mountpoint, &fuse_argc, &fuse_argv);
       +  build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
          int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops);
        
          smbc_free_context (ctx, 1);
       -  mount_destroy (mount);
       -  conffile_destroy (cf);
       +  free_strings (sharename);
        
          return ret;
        }
 (DIR) diff --git a/version.h b/version.h
       @@ -21,7 +21,7 @@
        
          #include <stdio.h>
        
       -  #define USMB_VERSION 0x20070102
       +  #define USMB_VERSION 0x20070126
        
          // a - alpha, b - beta, p - pre-release, s - stable
          #define USMB_VERSION_STATUS 'a'
 (DIR) diff --git a/xml.c b/xml.c
       @@ -38,9 +38,9 @@ bool xml_validate_relaxng (xmlDocPtr doc, const char *schema)
        
        
        bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
       -                           xmlChar *xpath,
       -                           const xmlChar *attribute,
       -                           xmlChar **out)
       +                           char *xpath,
       +                           const char *attribute,
       +                           char **out)
        {
          xmlXPathObjectPtr obj;
          xmlChar *tmp;
       @@ -51,7 +51,7 @@ bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
        
          *out = NULL;
        
       -  obj = xmlXPathEval (xpath, ctx);
       +  obj = xmlXPathEval (BAD_CAST xpath, ctx);
          if (NULL == obj)
          {
            DEBUG (fputs ("XPath evaluation error\n", stderr));
       @@ -78,11 +78,11 @@ bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
              break;
            }
        
       -    tmp = xmlGetProp (obj->nodesetval->nodeTab[0], attribute);
       +    tmp = xmlGetProp (obj->nodesetval->nodeTab[0], BAD_CAST attribute);
            if (NULL == tmp)
              break;
        
       -    *out = (xmlChar *)xstrdup ((char *)tmp);
       +    *out = xstrdup ((char *)tmp);
            if (NULL == *out)
              break;
        
       @@ -97,7 +97,7 @@ bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
        }
        
        
       -bool xml_xpath_text (xmlXPathContextPtr ctx, xmlChar *xpath, xmlChar **out)
       +bool xml_xpath_text (xmlXPathContextPtr ctx, char *xpath, char **out)
        {
          xmlXPathObjectPtr obj;
        
       @@ -109,7 +109,7 @@ bool xml_xpath_text (xmlXPathContextPtr ctx, xmlChar *xpath, xmlChar **out)
        
          DEBUG (fprintf (stderr, "xml_xpath_text (%s)\n", xpath));
        
       -  obj = xmlXPathEval (xpath, ctx);
       +  obj = xmlXPathEval (BAD_CAST xpath, ctx);
          if (NULL == obj)
          {
            DEBUG (fputs ("XPath evaluation error\n", stderr));
       @@ -142,7 +142,7 @@ bool xml_xpath_text (xmlXPathContextPtr ctx, xmlChar *xpath, xmlChar **out)
              break;
            }
        
       -    *out = (xmlChar *)xstrdup ((char *)obj->nodesetval->nodeTab[0]->content);
       +    *out = xstrdup ((char *)obj->nodesetval->nodeTab[0]->content);
            if (NULL == *out)
              break;
        
 (DIR) diff --git a/xml.h b/xml.h
       @@ -27,9 +27,9 @@
        
          bool xml_validate_relaxng (xmlDocPtr doc, const char *schema);
          bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
       -                             xmlChar *xpath,
       -                             const xmlChar *attribute,
       -                             xmlChar **out);
       -  bool xml_xpath_text (xmlXPathContextPtr ctx, xmlChar *xpath, xmlChar **out);
       +                             char *xpath,
       +                             const char *attribute,
       +                             char **out);
       +  bool xml_xpath_text (xmlXPathContextPtr ctx, char *xpath, char **out);
        
        #endif