2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Stefan (metze) Metzmacher 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 this implements the core code for all NTVFS modules. Backends register themselves here.
26 #include "lib/util/dlinklist.h"
28 #include "ntvfs/ntvfs.h"
29 #include "param/param.h"
31 /* the list of currently registered NTVFS backends, note that there
32 * can be more than one backend with the same name, as long as they
33 * have different typesx */
34 static struct ntvfs_backend {
35 const struct ntvfs_ops *ops;
37 static int num_backends;
40 register a NTVFS backend.
42 The 'name' can be later used by other backends to find the operations
43 structure for this backend.
45 The 'type' is used to specify whether this is for a disk, printer or IPC$ share
47 _PUBLIC_ NTSTATUS ntvfs_register(const struct ntvfs_ops *ops,
48 const struct ntvfs_critical_sizes *const sizes)
50 struct ntvfs_ops *new_ops;
52 if (ntvfs_interface_differs(sizes)) {
53 DEBUG(0, ("NTVFS backend '%s' for type %d "
54 "failed version check\n",
55 ops->name, (int)ops->type));
56 return NT_STATUS_BAD_FUNCTION_TABLE;
59 if (ntvfs_backend_byname(ops->name, ops->type) != NULL) {
60 /* its already registered! */
61 DEBUG(0,("NTVFS backend '%s' for type %d already registered\n",
62 ops->name, (int)ops->type));
63 return NT_STATUS_OBJECT_NAME_COLLISION;
66 backends = realloc_p(backends, struct ntvfs_backend, num_backends+1);
68 smb_panic("out of memory in ntvfs_register");
71 new_ops = smb_xmemdup(ops, sizeof(*ops));
72 new_ops->name = smb_xstrdup(ops->name);
74 backends[num_backends].ops = new_ops;
78 DEBUG(3,("NTVFS backend '%s' for type %d registered\n",
79 ops->name,ops->type));
86 return the operations structure for a named backend of the specified type
88 _PUBLIC_ const struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
92 for (i=0;i<num_backends;i++) {
93 if (backends[i].ops->type == type &&
94 strcmp(backends[i].ops->name, name) == 0) {
95 return backends[i].ops;
104 return the NTVFS interface version, and the size of some critical types
105 This can be used by backends to either detect compilation errors, or provide
106 multiple implementations for different smbd compilation options in one module
109 static const NTVFS_CURRENT_CRITICAL_SIZES(critical_sizes);
111 _PUBLIC_ const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
113 return &critical_sizes;
116 _PUBLIC_ bool ntvfs_interface_differs(const struct ntvfs_critical_sizes *const iface)
118 /* The comparison would be easier with memcmp, but compiler-interset
119 * alignment padding is not guaranteed to be zeroed.
122 #define FIELD_DIFFERS(field) (iface->field != critical_sizes.field)
124 if (FIELD_DIFFERS(interface_version))
127 if (FIELD_DIFFERS(sizeof_ntvfs_critical_sizes))
130 if (FIELD_DIFFERS(sizeof_ntvfs_context))
133 if (FIELD_DIFFERS(sizeof_ntvfs_module_context))
136 if (FIELD_DIFFERS(sizeof_ntvfs_ops))
139 if (FIELD_DIFFERS(sizeof_ntvfs_async_state))
142 if (FIELD_DIFFERS(sizeof_ntvfs_request))
145 /* Versions match. */
152 initialise a connection structure to point at a NTVFS backend
154 NTSTATUS ntvfs_init_connection(TALLOC_CTX *mem_ctx, struct share_config *scfg, enum ntvfs_type type,
155 enum protocol_types protocol,
156 struct event_context *ev, struct messaging_context *msg,
157 struct loadparm_context *lp_ctx,
158 struct server_id server_id, struct ntvfs_context **_ctx)
160 const char **handlers = share_string_list_option(mem_ctx, scfg, SHARE_NTVFS_HANDLER);
162 struct ntvfs_context *ctx;
165 return NT_STATUS_INTERNAL_ERROR;
168 ctx = talloc_zero(mem_ctx, struct ntvfs_context);
169 NT_STATUS_HAVE_NO_MEMORY(ctx);
170 ctx->protocol = protocol;
172 ctx->config = talloc_steal(ctx, scfg);
175 ctx->server_id = server_id;
176 ctx->lp_ctx = lp_ctx;
178 for (i=0; handlers[i]; i++) {
179 struct ntvfs_module_context *ntvfs;
181 ntvfs = talloc_zero(ctx, struct ntvfs_module_context);
182 NT_STATUS_HAVE_NO_MEMORY(ntvfs);
184 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
186 DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
187 handlers[i], ctx->type));
188 return NT_STATUS_INTERNAL_ERROR;
191 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
195 return NT_STATUS_INTERNAL_ERROR;
202 NTSTATUS ntvfs_init(struct loadparm_context *lp_ctx)
204 static bool initialized = false;
205 init_module_fn static_init[] = STATIC_ntvfs_MODULES;
206 init_module_fn *shared_init;
208 if (initialized) return NT_STATUS_OK;
211 shared_init = load_samba_modules(NULL, lp_ctx, "ntvfs");
213 run_init_functions(static_init);
214 run_init_functions(shared_init);
216 talloc_free(shared_init);