Hi Andrew,

Here's a patch to add a smb_register_passdb() function. It puts the
list of available backends in a double linked list, and lets you add 
backends with smb_register_passdb().

In this patch, the double linked list contains a name and a pointer
to an init function that allocates and fills a pdb_methods structure.
A different approach might be to add an init() pointer to the
pdb_methods structure and put a name and pointer to a pdb_methods
structure in the double linked list.

Could you please take a look at it and tell me whether you
have any comments?

Btw. Good luck with your exam today!

Jelmer
Index: include/passdb.h
===================================================================
RCS file: /home/cvs/samba/source/include/passdb.h,v
retrieving revision 1.21
diff -u -3 -p -d -r1.21 passdb.h
--- include/passdb.h    21 Oct 2002 19:28:55 -0000      1.21
+++ include/passdb.h    14 Nov 2002 19:20:21 -0000
@@ -156,6 +156,7 @@ struct pdb_init_function_entry {
        char *name;
        /* Function to create a member of the pdb_methods list */
        pdb_init_function init;
+       struct pdb_init_function_entry *prev, *next;
 };
 
 #endif /* _PASSDB_H */
Index: passdb/pdb_interface.c
===================================================================
RCS file: /home/cvs/samba/source/passdb/pdb_interface.c,v
retrieving revision 1.29
diff -u -3 -p -d -r1.29 pdb_interface.c
--- passdb/pdb_interface.c      14 Nov 2002 18:16:17 -0000      1.29
+++ passdb/pdb_interface.c      14 Nov 2002 19:20:22 -0000
@@ -25,8 +25,11 @@
 #define DBGC_CLASS DBGC_PASSDB
 
 /** List of various built-in passdb modules */
-
-const struct pdb_init_function_entry builtin_pdb_init_functions[] = {
+const struct {
+    char *name;
+    /* Function to create a member of the pdb_methods list */
+    pdb_init_function init;
+} builtin_pdb_init_functions[] = {
        { "smbpasswd", pdb_init_smbpasswd },
        { "smbpasswd_nua", pdb_init_smbpasswd_nua },
        { "tdbsam", pdb_init_tdbsam },
@@ -39,6 +42,50 @@ const struct pdb_init_function_entry bui
        { NULL, NULL}
 };
 
+static struct pdb_init_function_entry *backends;
+static void lazy_initialize_passdb(void);
+
+static void lazy_initialize_passdb()
+{
+       int i;
+       static BOOL initialised = False;
+       
+       if(!initialised) {
+               initialised = True;
+
+               for(i = 0; builtin_pdb_init_functions[i].name; i++) {
+                       smb_register_passdb(builtin_pdb_init_functions[i].name, 
+builtin_pdb_init_functions[i].init, PASSDB_INTERFACE_VERSION);
+               }
+       }
+}
+
+BOOL smb_register_passdb(char *name, pdb_init_function init, int version) 
+{
+       struct pdb_init_function_entry *entry = backends;
+
+       if(version != PASSDB_INTERFACE_VERSION)
+               return False;
+
+       DEBUG(5,("Attempting to register passdb backend %s\n", name));
+
+       /* Check for duplicates */
+       while(entry) { 
+               if(strcasecmp(name, entry->name) == 0) { 
+                       DEBUG(0,("There already is a passdb backend registered with 
+the name %s!\n", name));
+                       return False;
+               }
+               entry = entry->next;
+       }
+
+       entry = smb_xmalloc(sizeof(struct pdb_init_function_entry));
+       entry->name = name;
+       entry->init = init;
+
+       DLIST_ADD(backends, entry);
+       DEBUG(5,("Successfully added passdb backend '%s'\n", name));
+       return True;
+}
+
 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update)
 {
        NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
@@ -371,8 +418,12 @@ static NTSTATUS make_pdb_methods_name(st
 {
        char *module_name = smb_xstrdup(selected);
        char *module_location = NULL, *p;
+       struct pdb_init_function_entry *entry;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-       int i;
+
+       lazy_initialize_passdb();
+
+       entry = backends;
 
        p = strchr(module_name, ':');
 
@@ -385,12 +436,11 @@ static NTSTATUS make_pdb_methods_name(st
        trim_string(module_name, " ", " ");
 
        DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, 
module_name));
-       for (i = 0; builtin_pdb_init_functions[i].name; i++)
-       {
-               if (strequal(builtin_pdb_init_functions[i].name, module_name))
+       while(entry) {
+               if (strequal(entry->name, module_name))
                {
-                       DEBUG(5,("Found pdb backend %s (at pos %d)\n", module_name, 
i));
-                       nt_status = builtin_pdb_init_functions[i].init(context, 
methods, module_location);
+                       DEBUG(5,("Found pdb backend %s\n", module_name));
+                       nt_status = entry->init(context, methods, module_location);
                        if (NT_STATUS_IS_OK(nt_status)) {
                                DEBUG(5,("pdb backend %s has a valid init\n", 
selected));
                        } else {
@@ -400,6 +450,7 @@ static NTSTATUS make_pdb_methods_name(st
                        return nt_status;
                        break; /* unreached */
                }
+               entry = entry->next;
        }
 
        /* No such backend found */

Attachment: msg04452/pgp00000.pgp
Description: PGP signature

Reply via email to