03c5aff01c7cb9310063ec08f0989d9e4430b716
[gd/samba-autobuild/.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   this implements the core code for all NTVFS modules. Backends register themselves here.
23 */
24
25 #include "includes.h"
26 #include "lib/util/dlinklist.h"
27 #include "build.h"
28 #include "ntvfs/ntvfs.h"
29
30 /* the list of currently registered NTVFS backends, note that there
31  * can be more than one backend with the same name, as long as they
32  * have different typesx */
33 static struct ntvfs_backend {
34         const struct ntvfs_ops *ops;
35 } *backends = NULL;
36 static int num_backends;
37
38 /*
39   register a NTVFS backend. 
40
41   The 'name' can be later used by other backends to find the operations
42   structure for this backend.  
43
44   The 'type' is used to specify whether this is for a disk, printer or IPC$ share
45 */
46 _PUBLIC_ NTSTATUS ntvfs_register(const struct ntvfs_ops *ops,
47                                  const struct ntvfs_critical_sizes *const sizes)
48 {
49         struct ntvfs_ops *new_ops;
50
51         if (ntvfs_interface_differs(sizes)) {
52                 DEBUG(0, ("NTVFS backend '%s' for type %d "
53                           "failed version check\n",
54                           ops->name, (int)ops->type));
55                 return NT_STATUS_BAD_FUNCTION_TABLE;
56         }
57
58         if (ntvfs_backend_byname(ops->name, ops->type) != NULL) {
59                 /* its already registered! */
60                 DEBUG(0,("NTVFS backend '%s' for type %d already registered\n", 
61                          ops->name, (int)ops->type));
62                 return NT_STATUS_OBJECT_NAME_COLLISION;
63         }
64
65         backends = realloc_p(backends, struct ntvfs_backend, num_backends+1);
66         if (!backends) {
67                 smb_panic("out of memory in ntvfs_register");
68         }
69
70         new_ops = smb_xmemdup(ops, sizeof(*ops));
71         new_ops->name = smb_xstrdup(ops->name);
72
73         backends[num_backends].ops = new_ops;
74
75         num_backends++;
76
77         DEBUG(3,("NTVFS backend '%s' for type %d registered\n", 
78                  ops->name,ops->type));
79
80         return NT_STATUS_OK;
81 }
82
83
84 /*
85   return the operations structure for a named backend of the specified type
86 */
87 _PUBLIC_ const struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
88 {
89         int i;
90
91         for (i=0;i<num_backends;i++) {
92                 if (backends[i].ops->type == type && 
93                     strcmp(backends[i].ops->name, name) == 0) {
94                         return backends[i].ops;
95                 }
96         }
97
98         return NULL;
99 }
100
101
102 /*
103   return the NTVFS interface version, and the size of some critical types
104   This can be used by backends to either detect compilation errors, or provide
105   multiple implementations for different smbd compilation options in one module
106 */
107
108 static const NTVFS_CURRENT_CRITICAL_SIZES(critical_sizes);
109
110 _PUBLIC_ const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
111 {
112         return &critical_sizes;
113 }
114
115 _PUBLIC_ BOOL ntvfs_interface_differs(const struct ntvfs_critical_sizes *const iface)
116 {
117         /* The comparison would be easier with memcmp, but compiler-interset
118          * alignment padding is not guaranteed to be zeroed.
119          */
120
121 #define FIELD_DIFFERS(field) (iface->field != critical_sizes.field)
122
123         if (FIELD_DIFFERS(interface_version))
124                 return True;
125
126         if (FIELD_DIFFERS(sizeof_ntvfs_critical_sizes))
127                 return True;
128
129         if (FIELD_DIFFERS(sizeof_ntvfs_context))
130                 return True;
131
132         if (FIELD_DIFFERS(sizeof_ntvfs_module_context))
133                 return True;
134
135         if (FIELD_DIFFERS(sizeof_ntvfs_ops))
136                 return True;
137
138         if (FIELD_DIFFERS(sizeof_ntvfs_async_state))
139                 return True;
140
141         if (FIELD_DIFFERS(sizeof_ntvfs_request))
142                 return True;
143
144         /* Versions match. */
145         return False;
146
147 #undef FIELD_DIFFERS
148 }
149
150 /*
151   initialise a connection structure to point at a NTVFS backend
152 */
153 NTSTATUS ntvfs_init_connection(TALLOC_CTX *mem_ctx, struct share_config *scfg, enum ntvfs_type type,
154                                enum protocol_types protocol,
155                                struct event_context *ev, struct messaging_context *msg,
156                                struct server_id server_id, struct ntvfs_context **_ctx)
157 {
158         const char **handlers = share_string_list_option(mem_ctx, scfg, SHARE_NTVFS_HANDLER);
159         int i;
160         struct ntvfs_context *ctx;
161
162         if (!handlers) {
163                 return NT_STATUS_INTERNAL_ERROR;
164         }
165
166         ctx = talloc_zero(mem_ctx, struct ntvfs_context);
167         NT_STATUS_HAVE_NO_MEMORY(ctx);
168         ctx->protocol           = protocol;
169         ctx->type               = type;
170         ctx->config             = talloc_steal(ctx, scfg);
171         ctx->event_ctx          = ev;
172         ctx->msg_ctx            = msg;
173         ctx->server_id          = server_id;
174
175         for (i=0; handlers[i]; i++) {
176                 struct ntvfs_module_context *ntvfs;
177
178                 ntvfs = talloc_zero(ctx, struct ntvfs_module_context);
179                 NT_STATUS_HAVE_NO_MEMORY(ntvfs);
180                 ntvfs->ctx = ctx;
181                 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
182                 if (!ntvfs->ops) {
183                         DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
184                                 handlers[i], ctx->type));
185                         return NT_STATUS_INTERNAL_ERROR;
186                 }
187                 ntvfs->depth = i;
188                 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
189         }
190
191         if (!ctx->modules) {
192                 return NT_STATUS_INTERNAL_ERROR;
193         }
194
195         *_ctx = ctx;
196         return NT_STATUS_OK;
197 }
198
199 NTSTATUS ntvfs_init(void)
200 {
201         init_module_fn static_init[] = STATIC_ntvfs_MODULES;
202         init_module_fn *shared_init = load_samba_modules(NULL, "ntvfs");
203
204         run_init_functions(static_init);
205         run_init_functions(shared_init);
206
207         talloc_free(shared_init);
208         
209         return NT_STATUS_OK;
210 }