Ok here it is my latest work on privileges
[Samba/bb.git] / source / sam / gums.c
blobb71915358451cd94da62b17b1313c8da41141fd2
1 /*
2 Unix SMB/CIFS implementation.
3 Grops and Users Management System initializations.
4 Copyright (C) Simo Sorce 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_SAM
26 #define GMV_MAJOR 0
27 #define GMV_MINOR 1
29 static GUMS_FUNCTIONS *gums_backend = NULL;
31 static struct gums_init_function_entry *backends = NULL;
33 static void lazy_initialize_gums(void)
35 static BOOL initialized = False;
37 if (initialized)
38 return;
40 static_init_gums;
41 initialized = True;
44 static struct gums_init_function_entry *gums_find_backend_entry(const char *name);
46 NTSTATUS gums_register_module(int version, const char *name, gums_init_function init_fn)
48 struct gums_init_function_entry *entry = backends;
50 if (version != GUMS_INTERFACE_VERSION) {
51 DEBUG(0,("Can't register gums backend!\n"
52 "You tried to register a gums module with"
53 "GUMS_INTERFACE_VERSION %d, while this version"
54 "of samba uses version %d\n", version,
55 GUMS_INTERFACE_VERSION));
57 return NT_STATUS_OBJECT_TYPE_MISMATCH;
60 if (!name || !init_fn) {
61 return NT_STATUS_INVALID_PARAMETER;
64 DEBUG(5,("Attempting to register gums backend %s\n", name));
66 /* Check for duplicates */
67 if (gums_find_backend_entry(name)) {
68 DEBUG(0,("There already is a gums backend registered"
69 "with the name %s!\n", name));
70 return NT_STATUS_OBJECT_NAME_COLLISION;
73 entry = smb_xmalloc(sizeof(struct gums_init_function_entry));
74 entry->name = smb_xstrdup(name);
75 entry->init_fn = init_fn;
77 DLIST_ADD(backends, entry);
78 DEBUG(5,("Successfully added gums backend '%s'\n", name));
79 return NT_STATUS_OK;
82 static struct gums_init_function_entry *gums_find_backend_entry(const char *name)
84 struct gums_init_function_entry *entry = backends;
86 while (entry) {
87 if (strcmp(entry->name, name) == 0)
88 return entry;
89 entry = entry->next;
92 return NULL;
95 NTSTATUS gums_setup_backend(const char *backend)
98 TALLOC_CTX *mem_ctx;
99 char *module_name = smb_xstrdup(backend);
100 char *p, *module_data = NULL;
101 struct gums_init_function_entry *entry;
102 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
104 lazy_initialize_gums();
106 p = strchr(module_name, ':');
107 if (p) {
108 *p = 0;
109 module_data = p+1;
110 trim_string(module_data, " ", " ");
113 trim_string(module_name, " ", " ");
115 DEBUG(5,("Attempting to find a gums backend to match %s (%s)\n", backend, module_name));
117 entry = gums_find_backend_entry(module_name);
119 /* Try to find a module that contains this module */
120 if (!entry) {
121 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
122 if(NT_STATUS_IS_OK(smb_probe_module("gums", module_name)) && !(entry = gums_find_backend_entry(module_name))) {
123 DEBUG(0,("Plugin is available, but doesn't register gums backend %s\n", module_name));
124 SAFE_FREE(module_name);
125 return NT_STATUS_UNSUCCESSFUL;
129 /* No such backend found */
130 if(!entry) {
131 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
132 SAFE_FREE(module_name);
133 return NT_STATUS_INVALID_PARAMETER;
136 DEBUG(5,("Found gums backend %s\n", module_name));
138 /* free current functions structure if any */
139 if (gums_backend) {
140 gums_backend->free_private_data(gums_backend->private_data);
141 talloc_destroy(gums_backend->mem_ctx);
142 gums_backend = NULL;
145 /* allocate a new GUMS_FUNCTIONS structure and memory context */
146 mem_ctx = talloc_init("gums_backend (%s)", module_name);
147 if (!mem_ctx)
148 return NT_STATUS_NO_MEMORY;
149 gums_backend = talloc(mem_ctx, sizeof(GUMS_FUNCTIONS));
150 if (!gums_backend)
151 return NT_STATUS_NO_MEMORY;
152 gums_backend->mem_ctx = mem_ctx;
154 /* init the requested backend module */
155 if (NT_STATUS_IS_OK(ret = entry->init_fn(gums_backend, module_data))) {
156 DEBUG(5,("gums backend %s has a valid init\n", backend));
157 } else {
158 DEBUG(0,("gums backend %s did not correctly init (error was %s)\n", backend, nt_errstr(ret)));
160 SAFE_FREE(module_name);
161 return ret;
164 NTSTATUS get_gums_fns(GUMS_FUNCTIONS **fns)
166 if (gums_backend != NULL) {
167 *fns = gums_backend;
168 return NT_STATUS_OK;
171 DEBUG(2, ("get_gums_fns: unable to get gums functions! backend uninitialized?\n"));
172 return NT_STATUS_UNSUCCESSFUL;