summaryrefslogtreecommitdiff
path: root/source3/sam/gums.c
blob: b71915358451cd94da62b17b1313c8da41141fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
   Unix SMB/CIFS implementation.
   Grops and Users Management System initializations.
   Copyright (C) Simo Sorce 2002

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_SAM

#define GMV_MAJOR 0
#define GMV_MINOR 1

static GUMS_FUNCTIONS *gums_backend = NULL;

static struct gums_init_function_entry *backends = NULL;

static void lazy_initialize_gums(void)
{
	static BOOL initialized = False;
	
	if (initialized)
		return;

	static_init_gums;
	initialized = True;
}

static struct gums_init_function_entry *gums_find_backend_entry(const char *name);

NTSTATUS gums_register_module(int version, const char *name, gums_init_function init_fn)
{
	struct gums_init_function_entry *entry = backends;

	if (version != GUMS_INTERFACE_VERSION) {
		DEBUG(0,("Can't register gums backend!\n"
			 "You tried to register a gums module with"
			 "GUMS_INTERFACE_VERSION %d, while this version"
			 "of samba uses version %d\n", version,
			 GUMS_INTERFACE_VERSION));

		return NT_STATUS_OBJECT_TYPE_MISMATCH;
	}

	if (!name || !init_fn) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	DEBUG(5,("Attempting to register gums backend %s\n", name));

	/* Check for duplicates */
	if (gums_find_backend_entry(name)) {
		DEBUG(0,("There already is a gums backend registered"
			 "with the name %s!\n", name));
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	entry = smb_xmalloc(sizeof(struct gums_init_function_entry));
	entry->name = smb_xstrdup(name);
	entry->init_fn = init_fn;

	DLIST_ADD(backends, entry);
	DEBUG(5,("Successfully added gums backend '%s'\n", name));
	return NT_STATUS_OK;
}

static struct gums_init_function_entry *gums_find_backend_entry(const char *name)
{
	struct gums_init_function_entry *entry = backends;

	while (entry) {
		if (strcmp(entry->name, name) == 0)
			return entry;
		entry = entry->next;
	}

	return NULL;
}

NTSTATUS gums_setup_backend(const char *backend)
{

	TALLOC_CTX *mem_ctx;
	char *module_name = smb_xstrdup(backend);
	char *p, *module_data = NULL;
	struct gums_init_function_entry *entry;
	NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;

	lazy_initialize_gums();

	p = strchr(module_name, ':');
	if (p) {
		*p = 0;
		module_data = p+1;
		trim_string(module_data, " ", " ");
	}

	trim_string(module_name, " ", " ");

	DEBUG(5,("Attempting to find a gums backend to match %s (%s)\n", backend, module_name));

	entry = gums_find_backend_entry(module_name);

	/* Try to find a module that contains this module */
	if (!entry) {
		DEBUG(2,("No builtin backend found, trying to load plugin\n"));
		if(NT_STATUS_IS_OK(smb_probe_module("gums", module_name)) && !(entry = gums_find_backend_entry(module_name))) {
			DEBUG(0,("Plugin is available, but doesn't register gums backend %s\n", module_name));
			SAFE_FREE(module_name);
			return NT_STATUS_UNSUCCESSFUL;
		}
	}

	/* No such backend found */
	if(!entry) {
		DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
		SAFE_FREE(module_name);
		return NT_STATUS_INVALID_PARAMETER;
	}

	DEBUG(5,("Found gums backend %s\n", module_name));

	/* free current functions structure if any */
	if (gums_backend) {
		gums_backend->free_private_data(gums_backend->private_data);
		talloc_destroy(gums_backend->mem_ctx);
		gums_backend = NULL;
	}

	/* allocate a new GUMS_FUNCTIONS structure and memory context */
	mem_ctx = talloc_init("gums_backend (%s)", module_name);
	if (!mem_ctx)
		return NT_STATUS_NO_MEMORY;
	gums_backend = talloc(mem_ctx, sizeof(GUMS_FUNCTIONS));
	if (!gums_backend)
		return NT_STATUS_NO_MEMORY;
	gums_backend->mem_ctx = mem_ctx;

	/* init the requested backend module */
	if (NT_STATUS_IS_OK(ret = entry->init_fn(gums_backend, module_data))) {
		DEBUG(5,("gums backend %s has a valid init\n", backend));
	} else {
		DEBUG(0,("gums backend %s did not correctly init (error was %s)\n", backend, nt_errstr(ret)));
	}
	SAFE_FREE(module_name);
	return ret;
}

NTSTATUS get_gums_fns(GUMS_FUNCTIONS **fns)
{
	if (gums_backend != NULL) {
		*fns = gums_backend;
		return NT_STATUS_OK;
	}

	DEBUG(2, ("get_gums_fns: unable to get gums functions! backend uninitialized?\n"));
	return NT_STATUS_UNSUCCESSFUL;
}