Subject: | Cannot use more than one set of credentials |
Due to the use of a single set of global variables in libauthSamba.c, Filesys::SmbClient always uses the credentials from the last connection object constructed.
To reproduce:
my $Network1 = Filesys::SmbClient->new(
'username' => 'network1user',
'password' => 'secret1',
'workgroup' => 'network1',
);
my $Network2 = Filesys::SmbClient->new(
'username' => 'network2user',
'password' => 'secret2',
'workgroup' => 'network2',
);
my $Dir = $Network1->opendir("smb://server/dir"); # Uses network2user/secret2
With some trepidation (disclaimer: I don't know C), I've attached the hack I used to allow my script to work for now. It uses the newer smbc_setFunctionAuthDataWithContext, and allows the construction of up to 5 different connections (and then does something terrible and undefined if a sixth is initialized). Obviously not suitable for production code, but maybe you'll find something useful in it.
Subject: | smbclient-auth-by-context-hack.diff |
diff a/SmbClient.c b/SmbClient.c
--- a/SmbClient.c
+++ b/SmbClient.c
@@ -114,8 +114,8 @@ if (!context) {
XSRETURN_UNDEF;
}
smbc_setDebug(context, 4); //4 gives a good level of trace.
-set_fn(workgroup, user, password);
-smbc_setFunctionAuthData(context, auth_fn);
+set_fn(context, workgroup, user, password);
+smbc_setFunctionAuthDataWithContext(context, auth_fn);
smbc_setDebug(context, debug);
if (smbc_init_context(context) == 0) {
smbc_free_context(context, 1);
diff a/SmbClient.xs b/SmbClient.xs
--- a/SmbClient.xs
+++ b/SmbClient.xs
@@ -44,8 +44,8 @@ if (!context) {
XSRETURN_UNDEF;
}
smbc_setDebug(context, 4); //4 gives a good level of trace.
-set_fn(workgroup, user, password);
-smbc_setFunctionAuthData(context, auth_fn);
+set_fn(context, workgroup, user, password);
+smbc_setFunctionAuthDataWithContext(context, auth_fn);
smbc_setDebug(context, debug);
if (smbc_init_context(context) == 0) {
smbc_free_context(context, 1);
diff a/libauthSamba.c b/libauthSamba.c
--- a/libauthSamba.c
+++ b/libauthSamba.c
@@ -2,14 +2,17 @@
#include <string.h>
#include "libauthSamba.h"
-char User[30];
-char Password[30];
-char Workgroup[30];
+SMBCCTX* context[5];
+char User[5][30];
+char Password[5][30];
+char Workgroup[5][30];
+int contexts_set = 0;
/*-----------------------------------------------------------------------------
* set_fn
*---------------------------------------------------------------------------*/
-void set_fn(char *workgroup,
+void set_fn(SMBCCTX *c,
+ char *workgroup,
char *username,
char *password)
{
@@ -17,21 +20,24 @@ void set_fn(char *workgroup,
printf("set_fn\n");
#endif
- strcpy(User, username);
- strcpy(Password, password);
+ context[contexts_set] = c;
+ strcpy(User[contexts_set], username);
+ strcpy(Password[contexts_set], password);
/* set workgroup only when set */
if (workgroup[0] && workgroup[0] != 0) {
#ifdef VERBOSE
fprintf("Workgroup is set to %s", workgroup);
#endif
- strcpy(Workgroup, workgroup);
+ strcpy(Workgroup[contexts_set], workgroup);
}
+ contexts_set++;
}
/*-----------------------------------------------------------------------------
* auth_fn
*---------------------------------------------------------------------------*/
-void auth_fn(const char *server,
+void auth_fn(SMBCCTX *c,
+ const char *server,
const char *share,
char *workgroup, int wgmaxlen,
char *username, int unmaxlen,
@@ -41,18 +47,24 @@ void auth_fn(const char *server,
printf("auth_fn\n");
#endif
- /* set workgroup only when set */
- if (Workgroup[0] && Workgroup[0] != 0) {
+ int context_index;
+ for (context_index = 0; context_index < contexts_set; context_index++) {
+ if (context[context_index] == c) {
+ /* set workgroup only when set */
+ if (Workgroup[context_index][0] && Workgroup[context_index][0] != 0) {
#ifdef VERBOSE
- fprintf("Workgroup is set to %s", Workgroup);
+ fprintf("Workgroup is set to %s", Workgroup[context_index]);
#endif
- strcpy(workgroup, Workgroup);
- wgmaxlen = 30;
+ strcpy(workgroup, Workgroup[context_index]);
+ wgmaxlen = 30;
+ }
+ strcpy(username, User[context_index]);
+ unmaxlen = 30;
+ strcpy(password, Password[context_index]);
+ pwmaxlen = 30;
+ return;
+ }
}
- strcpy(username, User);
- unmaxlen = 30;
- strcpy(password, Password);
- pwmaxlen = 30;
#ifdef VERBOSE
fprintf(stdout, "username: [%s]\n", username);
diff a/libauthSamba.h b/libauthSamba.h
--- a/libauthSamba.h
+++ b/libauthSamba.h
@@ -1,9 +1,13 @@
-void set_fn( char *workgroup,
- char *username,
- char *password);
+#include "libsmbclient.h"
+void set_fn(SMBCCTX *c,
+ char *workgroup,
+ char *username,
+ char *password);
-void auth_fn(const char *server,
+
+void auth_fn(SMBCCTX *c,
+ const char *server,
const char *share,
char *workgroup, int wgmaxlen,
char *username, int umaxlen,