r14736: - the ntvfs subsystem should not know about smb_server.h
[jra/samba/.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 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 void *_ops)
48 {
49         const struct ntvfs_ops *ops = _ops;
50         struct ntvfs_ops *new_ops;
51         
52         if (ntvfs_backend_byname(ops->name, ops->type) != NULL) {
53                 /* its already registered! */
54                 DEBUG(0,("NTVFS backend '%s' for type %d already registered\n", 
55                          ops->name, (int)ops->type));
56                 return NT_STATUS_OBJECT_NAME_COLLISION;
57         }
58
59         backends = realloc_p(backends, struct ntvfs_backend, num_backends+1);
60         if (!backends) {
61                 smb_panic("out of memory in ntvfs_register");
62         }
63
64         new_ops = smb_xmemdup(ops, sizeof(*ops));
65         new_ops->name = smb_xstrdup(ops->name);
66
67         backends[num_backends].ops = new_ops;
68
69         num_backends++;
70
71         DEBUG(3,("NTVFS backend '%s' for type %d registered\n", 
72                  ops->name,ops->type));
73
74         return NT_STATUS_OK;
75 }
76
77
78 /*
79   return the operations structure for a named backend of the specified type
80 */
81 _PUBLIC_ const struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
82 {
83         int i;
84
85         for (i=0;i<num_backends;i++) {
86                 if (backends[i].ops->type == type && 
87                     strcmp(backends[i].ops->name, name) == 0) {
88                         return backends[i].ops;
89                 }
90         }
91
92         return NULL;
93 }
94
95
96 /*
97   return the NTVFS interface version, and the size of some critical types
98   This can be used by backends to either detect compilation errors, or provide
99   multiple implementations for different smbd compilation options in one module
100 */
101 static const struct ntvfs_critical_sizes critical_sizes = {
102         .interface_version              = NTVFS_INTERFACE_VERSION,
103         .sizeof_ntvfs_critical_sizes    = sizeof(struct ntvfs_critical_sizes),
104         .sizeof_ntvfs_context           = sizeof(struct ntvfs_context),
105         .sizeof_ntvfs_module_context    = sizeof(struct ntvfs_module_context),
106         .sizeof_ntvfs_ops               = sizeof(struct ntvfs_ops),
107         .sizeof_ntvfs_async_state       = sizeof(struct ntvfs_async_state),
108         .sizeof_ntvfs_request           = sizeof(struct ntvfs_request),
109 };
110
111 _PUBLIC_ const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
112 {
113         return &critical_sizes;
114 }
115
116
117 /*
118   initialise a connection structure to point at a NTVFS backend
119 */
120 NTSTATUS ntvfs_init_connection(TALLOC_CTX *mem_ctx, int snum, enum ntvfs_type type,
121                                enum protocol_types protocol,
122                                struct event_context *ev, struct messaging_context *msg,
123                                uint32_t server_id, struct ntvfs_context **_ctx)
124 {
125         const char **handlers = lp_ntvfs_handler(snum);
126         int i;
127         struct ntvfs_context *ctx;
128
129         if (!handlers) {
130                 return NT_STATUS_INTERNAL_ERROR;
131         }
132
133         ctx = talloc_zero(mem_ctx, struct ntvfs_context);
134         NT_STATUS_HAVE_NO_MEMORY(ctx);
135         ctx->protocol           = protocol;
136         ctx->type               = type;
137         ctx->config.snum        = snum;
138         ctx->event_ctx          = ev;
139         ctx->msg_ctx            = msg;
140         ctx->server_id          = server_id;
141
142         for (i=0; handlers[i]; i++) {
143                 struct ntvfs_module_context *ntvfs;
144
145                 ntvfs = talloc_zero(ctx, struct ntvfs_module_context);
146                 NT_STATUS_HAVE_NO_MEMORY(ntvfs);
147                 ntvfs->ctx = ctx;
148                 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
149                 if (!ntvfs->ops) {
150                         DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
151                                 handlers[i], ctx->type));
152                         return NT_STATUS_INTERNAL_ERROR;
153                 }
154                 ntvfs->depth = i;
155                 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
156         }
157
158         if (!ctx->modules) {
159                 return NT_STATUS_INTERNAL_ERROR;
160         }
161
162         *_ctx = ctx;
163         return NT_STATUS_OK;
164 }
165
166 NTSTATUS ntvfs_init(void)
167 {
168         init_module_fn static_init[] = STATIC_ntvfs_MODULES;
169         init_module_fn *shared_init = load_samba_modules(NULL, "ntvfs");
170
171         run_init_functions(static_init);
172         run_init_functions(shared_init);
173
174         talloc_free(shared_init);
175         
176         return NT_STATUS_OK;
177 }