]> git.samba.org - jelmer/samba4-debian.git/blob - source/ntvfs/ntvfs_base.c
r15185: Force all NTVFS modules to provide a critical sizes structure so
[jelmer/samba4-debian.git] / source / ntvfs / ntvfs_base.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NTVFS base code
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Stefan (metze) Metzmacher 2004
7
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements the core code for all NTVFS modules. Backends register themselves here.
24 */
25
26 #include "includes.h"
27 #include "dlinklist.h"
28 #include "build.h"
29 #include "ntvfs/ntvfs.h"
30
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;
36 } *backends = NULL;
37 static int num_backends;
38
39 /*
40   register a NTVFS backend. 
41
42   The 'name' can be later used by other backends to find the operations
43   structure for this backend.  
44
45   The 'type' is used to specify whether this is for a disk, printer or IPC$ share
46 */
47 _PUBLIC_ NTSTATUS ntvfs_register(const struct ntvfs_ops *ops,
48                                  const struct ntvfs_critical_sizes *const sizes)
49 {
50         struct ntvfs_ops *new_ops;
51
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;
57         }
58
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;
64         }
65
66         backends = realloc_p(backends, struct ntvfs_backend, num_backends+1);
67         if (!backends) {
68                 smb_panic("out of memory in ntvfs_register");
69         }
70
71         new_ops = smb_xmemdup(ops, sizeof(*ops));
72         new_ops->name = smb_xstrdup(ops->name);
73
74         backends[num_backends].ops = new_ops;
75
76         num_backends++;
77
78         DEBUG(3,("NTVFS backend '%s' for type %d registered\n", 
79                  ops->name,ops->type));
80
81         return NT_STATUS_OK;
82 }
83
84
85 /*
86   return the operations structure for a named backend of the specified type
87 */
88 _PUBLIC_ const struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
89 {
90         int i;
91
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;
96                 }
97         }
98
99         return NULL;
100 }
101
102
103 /*
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
107 */
108
109 static const NTVFS_CURRENT_CRITICAL_SIZES(critical_sizes);
110
111 _PUBLIC_ const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
112 {
113         return &critical_sizes;
114 }
115
116 _PUBLIC_ BOOL ntvfs_interface_differs(const struct ntvfs_critical_sizes *const iface)
117 {
118         /* The comparison would be easier with memcmp, but compiler-interset
119          * alignment padding is not guaranteed to be zeroed.
120          */
121
122 #define FIELD_DIFFERS(field) (iface->field != critical_sizes.field)
123
124         if (FIELD_DIFFERS(interface_version))
125                 return True;
126
127         if (FIELD_DIFFERS(sizeof_ntvfs_critical_sizes))
128                 return True;
129
130         if (FIELD_DIFFERS(sizeof_ntvfs_context))
131                 return True;
132
133         if (FIELD_DIFFERS(sizeof_ntvfs_module_context))
134                 return True;
135
136         if (FIELD_DIFFERS(sizeof_ntvfs_ops))
137                 return True;
138
139         if (FIELD_DIFFERS(sizeof_ntvfs_async_state))
140                 return True;
141
142         if (FIELD_DIFFERS(sizeof_ntvfs_request))
143                 return True;
144
145         /* Versions match. */
146         return False;
147
148 #undef FIELD_DIFFERS
149 }
150
151
152 /*
153   initialise a connection structure to point at a NTVFS backend
154 */
155 NTSTATUS ntvfs_init_connection(TALLOC_CTX *mem_ctx, int snum, enum ntvfs_type type,
156                                enum protocol_types protocol,
157                                struct event_context *ev, struct messaging_context *msg,
158                                uint32_t server_id, struct ntvfs_context **_ctx)
159 {
160         const char **handlers = lp_ntvfs_handler(snum);
161         int i;
162         struct ntvfs_context *ctx;
163
164         if (!handlers) {
165                 return NT_STATUS_INTERNAL_ERROR;
166         }
167
168         ctx = talloc_zero(mem_ctx, struct ntvfs_context);
169         NT_STATUS_HAVE_NO_MEMORY(ctx);
170         ctx->protocol           = protocol;
171         ctx->type               = type;
172         ctx->config.snum        = snum;
173         ctx->event_ctx          = ev;
174         ctx->msg_ctx            = msg;
175         ctx->server_id          = server_id;
176
177         for (i=0; handlers[i]; i++) {
178                 struct ntvfs_module_context *ntvfs;
179
180                 ntvfs = talloc_zero(ctx, struct ntvfs_module_context);
181                 NT_STATUS_HAVE_NO_MEMORY(ntvfs);
182                 ntvfs->ctx = ctx;
183                 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
184                 if (!ntvfs->ops) {
185                         DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
186                                 handlers[i], ctx->type));
187                         return NT_STATUS_INTERNAL_ERROR;
188                 }
189                 ntvfs->depth = i;
190                 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
191         }
192
193         if (!ctx->modules) {
194                 return NT_STATUS_INTERNAL_ERROR;
195         }
196
197         *_ctx = ctx;
198         return NT_STATUS_OK;
199 }
200
201 NTSTATUS ntvfs_init(void)
202 {
203         init_module_fn static_init[] = STATIC_ntvfs_MODULES;
204         init_module_fn *shared_init = load_samba_modules(NULL, "ntvfs");
205
206         run_init_functions(static_init);
207         run_init_functions(shared_init);
208
209         talloc_free(shared_init);
210         
211         return NT_STATUS_OK;
212 }