Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[sfrench/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 "ntvfs/ntvfs.h"
28 #include "param/param.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 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 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 const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
111 {
112         return &critical_sizes;
113 }
114
115 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                                uint64_t ntvfs_client_caps,
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)
159 {
160         const char **handlers = share_string_list_option(mem_ctx, scfg, SHARE_NTVFS_HANDLER);
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->client_caps        = ntvfs_client_caps;
172         ctx->type               = type;
173         ctx->config             = talloc_steal(ctx, scfg);
174         ctx->event_ctx          = ev;
175         ctx->msg_ctx            = msg;
176         ctx->server_id          = server_id;
177         ctx->lp_ctx             = lp_ctx;
178
179         for (i=0; handlers[i]; i++) {
180                 struct ntvfs_module_context *ntvfs;
181
182                 ntvfs = talloc_zero(ctx, struct ntvfs_module_context);
183                 NT_STATUS_HAVE_NO_MEMORY(ntvfs);
184                 ntvfs->ctx = ctx;
185                 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
186                 if (!ntvfs->ops) {
187                         DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
188                                 handlers[i], ctx->type));
189                         return NT_STATUS_INTERNAL_ERROR;
190                 }
191                 ntvfs->depth = i;
192                 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
193         }
194
195         if (!ctx->modules) {
196                 return NT_STATUS_INTERNAL_ERROR;
197         }
198
199         *_ctx = ctx;
200         return NT_STATUS_OK;
201 }
202
203 NTSTATUS ntvfs_init(struct loadparm_context *lp_ctx)
204 {
205         static bool initialized = false;
206         extern NTSTATUS ntvfs_posix_init(void);
207         extern NTSTATUS ntvfs_cifs_init(void);
208         extern NTSTATUS ntvfs_nbench_init(void);
209         extern NTSTATUS ntvfs_unixuid_init(void);
210         extern NTSTATUS ntvfs_ipc_init(void);
211         extern NTSTATUS pvfs_acl_nfs4_init(void);
212         extern NTSTATUS pvfs_acl_xattr_init(void);
213         extern NTSTATUS ntvfs_print_init(void);
214         extern NTSTATUS ntvfs_simple_init(void);
215         extern NTSTATUS ntvfs_cifs_posix_init(void);
216         init_module_fn static_init[] = { STATIC_ntvfs_MODULES };
217         init_module_fn *shared_init;
218
219         if (initialized) return NT_STATUS_OK;
220         initialized = true;
221         
222         shared_init = load_samba_modules(NULL, lp_ctx, "ntvfs");
223
224         run_init_functions(static_init);
225         run_init_functions(shared_init);
226
227         talloc_free(shared_init);
228         
229         return NT_STATUS_OK;
230 }