---
 smb.c      |  30 +++++++++++----
 smb.h      |   9 +++--
 smbfs.c    |  31 ++++++++--------
 smbnetfs.c | 105 ++++++++++++++++++++++++++++-------------------------
 4 files changed, 98 insertions(+), 77 deletions(-)

diff --git a/smb.c b/smb.c
index 6d0fa90ce..3b5aa4a3a 100644
--- a/smb.c
+++ b/smb.c
@@ -23,16 +23,30 @@ auth_data_fn (const char *server, const char *share, char 
*workgroup,
               int wgmaxlen, char *username, int unmaxlen, char *password,
               int pwmaxlen)
 {
-  if (strcmp (server, credentials.server))
+  if (strcmp (server, opts.server))
     return;
-  strncpy (workgroup, credentials.workgroup, wgmaxlen);
-  strncpy (username, credentials.username, unmaxlen);
-  strncpy (password, credentials.password, pwmaxlen);
+  strncpy (workgroup, opts.workgroup, wgmaxlen);
+  strncpy (username, opts.username, unmaxlen);
+  strncpy (password, opts.password, pwmaxlen);
 }
 
-int
-init_smb ()
+void
+init_smb (const char *min_proto)
 {
-  int ret = smbc_init (auth_data_fn, 10);
-  return ret;
+  int ret;
+
+  ctx = smbc_new_context();
+  if (!ctx)
+    error(EXIT_FAILURE, errno, "Failed to get new smbc context");
+
+  ctx = smbc_init_context(ctx);
+  if (!ctx)
+    error(EXIT_FAILURE, errno, "Failed to init smbc context");
+
+  smbc_setFunctionAuthData(ctx, auth_data_fn);
+  ret = smbc_setOptionProtocols(ctx, min_proto, "SMB3_11");
+  if (!ret)
+    error(EXIT_FAILURE, 1, "Cannot set minimum protocol version to %s", 
min_proto);
+
+  smbc_setOptionPosixExtensions(ctx, true);
 }
diff --git a/smb.h b/smb.h
index 90bf4cc97..94575fc92 100644
--- a/smb.h
+++ b/smb.h
@@ -29,15 +29,16 @@
 #include <hurd/netfs.h>
 #include <libsmbclient.h>
 
-struct smb_credentials
+struct smb_opts
 {
   char *server;
   char *share;
   char *workgroup;
   char *username;
   char *password;
+  char *min_proto;
 };
-extern struct smb_credentials credentials;
+extern struct smb_opts opts;
+extern SMBCCTX *ctx;
 
-extern int init_smb ();
-extern void stop_netsmb ();
+extern void init_smb (const char *min_proto);
diff --git a/smbfs.c b/smbfs.c
index 71435156c..161c57c17 100644
--- a/smbfs.c
+++ b/smbfs.c
@@ -27,8 +27,6 @@ static char doc[] = "smbfs - SMB file system translator" \
   "`smb://[WORKGROUP/]HOST/SHARE'.";
 static char args_doc[] = "SHARE";
 
-
-
 extern void smbfs_init ();
 extern void smbfs_terminate ();
 static struct argp_option options[] = 
@@ -38,6 +36,7 @@ static struct argp_option options[] =
        {"password",'p',"PWD",0, "password to use (default: empty password)"},
        {"username",'u',"USR",0, "user name to use (default: `$USER')"},
        {"workgroup",'w',"WKG",0, "workgroup to use (default: `WORKGROUP')"},
+       {"minproto",'m',"MPT",0, "minimum protocol version (default: 
`SMB3_11')"},
        {0}
 };
 static error_t parse_opt(int key, char *arg, struct argp_state *state)
@@ -45,19 +44,22 @@ static error_t parse_opt(int key, char *arg, struct 
argp_state *state)
   switch(key)
     {
       case 's':
-        credentials.server = arg;        
+        opts.server = arg;
         break;
       case 'r':
-        credentials.share = arg;        
+        opts.share = arg;
         break;
       case 'w':
-        credentials.workgroup = arg;
+        opts.workgroup = arg;
         break;
       case 'u':
-        credentials.username = arg;
+        opts.username = arg;
         break;
       case 'p':
-        credentials.password = arg;
+        opts.password = arg;
+        break;
+      case 'm':
+        opts.min_proto = arg;
         break;
       case ARGP_KEY_ARG:
         break;
@@ -75,22 +77,21 @@ int
 main (int argc, char *argv[])
 {
   mach_port_t bootstrap;
-  int err;
 
   /* Default user name.  */
-  credentials.username = getenv ("USER");
+  opts.username = getenv ("USER");
 
-  argp_parse (&smb_argp, argc, argv, 0, 0, &credentials);
+  argp_parse (&smb_argp, argc, argv, 0, 0, &opts);
 
-  if(!credentials.server  || !credentials.share || !credentials.workgroup
-     || !credentials.username || !credentials.password)
+  if (!opts.server || !opts.share || !opts.workgroup
+   || !opts.username || !opts.password)
     error (EXIT_FAILURE, 0, "Please specify a server, share, workgroup, "
           "user name, and password.");
 
-  err = init_smb ();
+  if (!opts.min_proto)
+    opts.min_proto = strdup("SMB3_11");
 
-  if (err < 0)
-    error (EXIT_FAILURE, errno, "failed to initialize SMB client");
+  init_smb ((const char *)opts.min_proto);
 
   task_get_bootstrap_port (mach_task_self (), &bootstrap);
   if (bootstrap == MACH_PORT_NULL)
diff --git a/smbnetfs.c b/smbnetfs.c
index be7e34ed2..fc436c4c4 100644
--- a/smbnetfs.c
+++ b/smbnetfs.c
@@ -39,7 +39,7 @@
 #define DIRENT_LEN(name_len)    ((DIRENT_NAME_OFFS + (name_len) + 1 \
                                + (DIRENT_ALIGN - 1)) & ~(DIRENT_ALIGN - 1))
 
-struct smb_credentials credentials =
+struct smb_opts opts =
   {
     .workgroup = "WORKGROUP",
     .password = ""
@@ -48,6 +48,7 @@ struct smb_credentials credentials =
 static volatile struct mapped_time_value *maptime;
 static pthread_mutex_t smb_mutex;
 
+SMBCCTX *ctx;
 char *netfs_server_name = "smbfs";
 char *netfs_server_version = "0.1";
 int netfs_maxsymlinks = 0;
@@ -169,7 +170,7 @@ create_root_node ()
     return;
 
   netfs_root_node = node;
-  node->nn->abs_file_name = strdup (credentials.share);
+  node->nn->abs_file_name = strdup (opts.share);
 
   netfs_validate_stat (node, 0);
 }
@@ -209,7 +210,7 @@ add_node (const char *filename, struct node *top, struct 
netnode **nn)
 
   /* Make sure FILENAME actually exists.  */
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_stat (n->abs_file_name, &n->node->nn_stat);
+  err = smbc_getFunctionStat(ctx) (ctx, n->abs_file_name, &n->node->nn_stat);
   pthread_mutex_unlock (&smb_mutex);
 
   if (err != 0)
@@ -232,7 +233,7 @@ netfs_validate_stat (struct node *np, struct iouser *cred)
   np->nn_stat.st_ino = (uintptr_t) np >> 3UL;
 
   pthread_mutex_lock (&smb_mutex);
-  int err = smbc_stat (np->nn->abs_file_name, &np->nn_stat);
+  int err = smbc_getFunctionStat(ctx) (ctx, np->nn->abs_file_name, 
&np->nn_stat);
   pthread_mutex_unlock (&smb_mutex);
   if (err)
     return errno;
@@ -260,7 +261,7 @@ netfs_attempt_chmod (struct iouser * cred, struct node * 
np, mode_t mode)
 {
   int err;
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_chmod (np->nn->abs_file_name, mode);
+  err = smbc_getFunctionChmod(ctx) (ctx, np->nn->abs_file_name, mode);
   pthread_mutex_unlock (&smb_mutex);
 
   if (err)
@@ -312,7 +313,7 @@ netfs_attempt_utimes (struct iouser * cred, struct node * 
np,
     maptime_read (maptime, &tv);
 
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_utimes (np->nn->abs_file_name, &tv);
+  err = smbc_getFunctionUtimes(ctx) (ctx, np->nn->abs_file_name, &tv);
   pthread_mutex_unlock (&smb_mutex);
 
   if(err)
@@ -324,19 +325,20 @@ netfs_attempt_utimes (struct iouser * cred, struct node * 
np,
 error_t
 netfs_attempt_set_size (struct iouser *cred, struct node *np, loff_t size)
 {
-  int fd, ret, saved_errno;
+  int ret, saved_errno;
+  SMBCFILE *fd;
 
   pthread_mutex_lock (&smb_mutex);
-  fd = smbc_open (np->nn->abs_file_name, O_WRONLY, 0);
+  fd = smbc_getFunctionOpen(ctx) (ctx, np->nn->abs_file_name, O_WRONLY, 0);
   pthread_mutex_unlock (&smb_mutex);
 
-  if (fd < 0)
+  if (!fd)
     return errno;
 
   pthread_mutex_lock (&smb_mutex);
-  ret = smbc_ftruncate (fd, size);
+  ret = smbc_getFunctionFtruncate(ctx) (ctx, fd, size);
   saved_errno = ret != 0 ? errno : 0;
-  smbc_close (fd);
+  smbc_getFunctionClose(ctx) (ctx, fd);
   pthread_mutex_unlock (&smb_mutex);
 
   return saved_errno;
@@ -424,7 +426,7 @@ netfs_attempt_unlink (struct iouser * user, struct node * 
dir, const char *name)
     return ENOMEM;
 
   pthread_mutex_lock (&smb_mutex);
-  error_t err = smbc_unlink (filename);
+  error_t err = smbc_getFunctionUnlink(ctx) (ctx, filename);
   pthread_mutex_unlock (&smb_mutex);  
   
   free (filename);
@@ -455,7 +457,7 @@ netfs_attempt_rename (struct iouser * user, struct node * 
fromdir,
     }
 
   pthread_mutex_lock (&smb_mutex);
-  error_t err = smbc_rename (filename, filename2);
+  error_t err = smbc_getFunctionRename(ctx) (ctx, filename, ctx, filename2);
   pthread_mutex_unlock (&smb_mutex);
 
   free (filename);
@@ -475,7 +477,7 @@ netfs_attempt_mkdir (struct iouser * user, struct node * 
dir, const char *name,
     return ENOMEM;
 
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_mkdir (filename, mode);
+  err = smbc_getFunctionMkdir(ctx) (ctx, filename, mode);
   pthread_mutex_unlock (&smb_mutex);  
   
   free (filename);
@@ -493,7 +495,7 @@ netfs_attempt_rmdir (struct iouser * user, struct node * 
dir, const char *name)
     return ENOMEM;
 
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_rmdir (filename);
+  err = smbc_getFunctionRmdir(ctx) (ctx, filename);
   pthread_mutex_unlock (&smb_mutex);  
 
   free(filename);
@@ -522,7 +524,7 @@ netfs_attempt_create_file (struct iouser * user, struct 
node * dir,
   error_t err = 0;
   char *filename;
   struct netnode *nn;
-  int fd;
+  SMBCFILE *fd;
 
   *np = 0;
 
@@ -531,14 +533,14 @@ netfs_attempt_create_file (struct iouser * user, struct 
node * dir,
     return ENOMEM;
 
   pthread_mutex_lock (&smb_mutex);
-  fd = smbc_open (filename, O_WRONLY | O_CREAT, mode);
-  if (fd < 0)
+  fd = smbc_getFunctionOpen(ctx) (ctx, filename, O_WRONLY | O_CREAT, mode);
+  if (!fd)
     {
       pthread_mutex_unlock (&smb_mutex);  
       pthread_mutex_unlock (&dir->lock);
       return errno;
     }
-  smbc_close (fd);
+  smbc_getFunctionClose(ctx) (ctx, fd);
   pthread_mutex_unlock (&smb_mutex);    
 
   err = add_node (name, dir, &nn);
@@ -569,7 +571,7 @@ netfs_check_open_permissions (struct iouser * user, struct 
node * np,
   io_statbuf_t  nn_stat;
   
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_stat (np->nn->abs_file_name, &nn_stat);
+  err = smbc_getFunctionStat(ctx) (ctx, np->nn->abs_file_name, &nn_stat);
   pthread_mutex_unlock (&smb_mutex);
    
   if (err)
@@ -589,48 +591,48 @@ error_t
 netfs_attempt_read (struct iouser * cred, struct node * np, loff_t offset,
                    size_t * len, void *data)
 {
-  int fd;
+  SMBCFILE *fd;
   int ret = 0;
 
   pthread_mutex_lock (&smb_mutex);
-  fd = smbc_open (np->nn->abs_file_name, O_RDONLY, 0);
+  fd = smbc_getFunctionOpen(ctx) (ctx, np->nn->abs_file_name, O_RDONLY, 0);
   pthread_mutex_unlock (&smb_mutex);
 
-  if (fd < 0)
+  if (!fd)
     {
       *len = 0;
       return errno;
     }
 
   pthread_mutex_lock (&smb_mutex);
-  ret = smbc_lseek (fd, offset, SEEK_SET);
+  ret = smbc_getFunctionLseek(ctx) (ctx, fd, offset, SEEK_SET);
   pthread_mutex_unlock (&smb_mutex);
   
   if ((ret < 0) || (ret != offset))
     {
       *len = 0;
       pthread_mutex_lock (&smb_mutex);
-      smbc_close (fd);
+      smbc_getFunctionClose(ctx) (ctx, fd);
       pthread_mutex_unlock (&smb_mutex);
       return errno;
     }
 
   pthread_mutex_lock (&smb_mutex);
-  ret = smbc_read (fd, data, *len);
+  ret = smbc_getFunctionRead(ctx) (ctx, fd, data, *len);
   pthread_mutex_unlock (&smb_mutex);
 
   if (ret < 0)
     {
       *len = 0;
       pthread_mutex_lock (&smb_mutex);
-      smbc_close (fd);
+      smbc_getFunctionClose(ctx) (ctx, fd);
       pthread_mutex_unlock (&smb_mutex);
       return errno;
     }
 
   *len = ret;
   pthread_mutex_lock (&smb_mutex);
-  smbc_close (fd);
+  smbc_getFunctionClose(ctx) (ctx, fd);
   pthread_mutex_unlock (&smb_mutex);
   return 0;
 }
@@ -640,45 +642,45 @@ netfs_attempt_write (struct iouser * cred, struct node * 
np, loff_t offset,
                     size_t * len, const void *data)
 {
   int ret = 0;
-  int fd;
+  SMBCFILE *fd;
 
   pthread_mutex_lock (&smb_mutex);
-  fd = smbc_open (np->nn->abs_file_name, O_WRONLY, 0);
+  fd = smbc_getFunctionOpen(ctx) (ctx, np->nn->abs_file_name, O_WRONLY, 0);
   pthread_mutex_unlock (&smb_mutex);
 
-  if (fd < 0)
+  if (!fd)
     {
       *len = 0;
       return errno;
     }
   pthread_mutex_lock (&smb_mutex);
-  ret = smbc_lseek (fd, offset, SEEK_SET);
+  ret = smbc_getFunctionLseek(ctx) (ctx, fd, offset, SEEK_SET);
   pthread_mutex_unlock (&smb_mutex);
   
   if ((ret < 0) || (ret != offset))
     {
       *len = 0;
       pthread_mutex_lock (&smb_mutex);
-      smbc_close (fd);
+      smbc_getFunctionClose(ctx) (ctx, fd);
       pthread_mutex_unlock (&smb_mutex);
       return errno;
     }
   pthread_mutex_lock (&smb_mutex);
-  ret = smbc_write (fd, data, *len);
+  ret = smbc_getFunctionWrite(ctx) (ctx, fd, data, *len);
   pthread_mutex_unlock (&smb_mutex);
   
   if (ret < 0)
     {
       *len = 0;
       pthread_mutex_lock (&smb_mutex);
-      smbc_close (fd);
+      smbc_getFunctionClose(ctx) (ctx, fd);
       pthread_mutex_unlock (&smb_mutex);      
       return errno;
     }
 
   *len = ret;
   pthread_mutex_lock (&smb_mutex);
-  smbc_close (fd);
+  smbc_getFunctionClose(ctx) (ctx, fd);
   pthread_mutex_unlock (&smb_mutex);      
 
   return 0;
@@ -719,7 +721,8 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
 {
   io_statbuf_t  st; 
   struct smbc_dirent * dirent;  
-  int size = 0,  dd;
+  int size = 0;
+  SMBCFILE *dd;
   int nreturningentries = 0;
   int err = 0;
   int add_dir_entry_size = 0;
@@ -729,14 +732,14 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
     return ENOTDIR;
 
   pthread_mutex_lock (&smb_mutex);
-  dd = smbc_opendir (dir->nn->abs_file_name);
+  dd = smbc_getFunctionOpendir(ctx) (ctx, dir->nn->abs_file_name);
   pthread_mutex_unlock (&smb_mutex);
   
-  if (dd < 0)
+  if (!dd)
     return ENOTDIR;  
   
   pthread_mutex_lock (&smb_mutex);
-  err = smbc_lseekdir (dd, entry);
+  err = smbc_getFunctionLseekdir(ctx) (ctx, dd, entry);
   pthread_mutex_unlock (&smb_mutex);
   
   if(err)
@@ -746,7 +749,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
           *datacnt = 0;
           *amt = 0;
           pthread_mutex_lock (&smb_mutex);
-          smbc_closedir (dd);
+          smbc_getFunctionClosedir(ctx) (ctx, dd);
           pthread_mutex_unlock (&smb_mutex);
           return 0;
         }
@@ -772,7 +775,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
   for(;;)
     {
       pthread_mutex_lock (&smb_mutex);      
-      dirent = smbc_readdir (dd);
+      dirent = smbc_getFunctionReaddir(ctx) (ctx, dd);
       pthread_mutex_unlock (&smb_mutex);
 
       if(!dirent)
@@ -791,13 +794,13 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
   if (!(*data) ||  (*data == (void *) -1))
     {
       pthread_mutex_lock (&smb_mutex);
-      smbc_closedir(dd);
+      smbc_getFunctionClosedir(ctx) (ctx, dd);
       pthread_mutex_unlock (&smb_mutex);
       return ENOMEM;    
     }
 
   pthread_mutex_lock (&smb_mutex);
-  err=smbc_lseekdir (dd, entry);
+  err = smbc_getFunctionLseekdir(ctx) (ctx, dd, entry);
   pthread_mutex_unlock (&smb_mutex);
 
   if (err)
@@ -807,7 +810,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
           *datacnt = 0;
           *amt = 0;
           pthread_mutex_lock (&smb_mutex);
-          smbc_closedir (dd);
+          smbc_getFunctionClosedir(ctx) (ctx, dd);
           pthread_mutex_unlock (&smb_mutex);
           return 0;
         }
@@ -851,7 +854,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
     for(;;)
       {
         pthread_mutex_lock (&smb_mutex);
-        dirent = smbc_readdir (dd);
+        dirent = smbc_getFunctionReaddir(ctx) (ctx, dd);
         pthread_mutex_unlock (&smb_mutex);
         if (!dirent)
           break;
@@ -866,7 +869,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
         if (!strcmp (dirent->name, "."))
           {
             pthread_mutex_lock (&smb_mutex);
-            err = smbc_stat (dir->nn->abs_file_name, &st);
+            err = smbc_getFunctionStat(ctx) (ctx, dir->nn->abs_file_name, &st);
             pthread_mutex_unlock (&smb_mutex);
           }
         else if (!strcmp (dirent->name, ".."))
@@ -884,7 +887,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
              return ENOMEM;
 
             pthread_mutex_lock (&smb_mutex);
-            err = smbc_stat (stat_file_name, &st);
+            err = smbc_getFunctionStat(ctx) (ctx, stat_file_name, &st);
             pthread_mutex_unlock (&smb_mutex);
 
            if (err)
@@ -898,7 +901,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
         if (err)
           {
             pthread_mutex_lock (&smb_mutex);
-            smbc_closedir(dd);
+            smbc_getFunctionClosedir(ctx) (ctx, dd);
             pthread_mutex_unlock (&smb_mutex);
             return errno;
           }
@@ -911,7 +914,7 @@ netfs_get_dirents (struct iouser *cred, struct node *dir, 
int entry,
   *datacnt = size;
   *amt = nreturningentries;
   pthread_mutex_lock (&smb_mutex);
-  smbc_closedir (dd);
+  smbc_getFunctionClosedir(ctx) (ctx, dd);
   pthread_mutex_unlock (&smb_mutex);
   return 0;
 }
@@ -933,4 +936,6 @@ smbfs_init ()
 void
 smbfs_terminate ()
 {
+  if (smbc_free_context(ctx, 1))
+    error(EXIT_FAILURE, errno, "WARNING: Server connections are still used, 
quitting anyway");
 }
-- 
2.51.0



Reply via email to