r3466: split out request.h, signing.h, and smb_server.h
[bbaumbach/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 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 "smb_server/smb_server.h"
29
30
31
32 /* the list of currently registered NTVFS backends, note that there
33  * can be more than one backend with the same name, as long as they
34  * have different typesx */
35 static struct {
36         const struct ntvfs_ops *ops;
37 } *backends = NULL;
38 static int num_backends;
39
40 /*
41   register a NTVFS backend. 
42
43   The 'name' can be later used by other backends to find the operations
44   structure for this backend.  
45
46   The 'type' is used to specify whether this is for a disk, printer or IPC$ share
47 */
48 static NTSTATUS ntvfs_register(const void *_ops)
49 {
50         const struct ntvfs_ops *ops = _ops;
51         struct ntvfs_ops *new_ops;
52         
53         if (ntvfs_backend_byname(ops->name, ops->type) != NULL) {
54                 /* its already registered! */
55                 DEBUG(0,("NTVFS backend '%s' for type %d already registered\n", 
56                          ops->name, (int)ops->type));
57                 return NT_STATUS_OBJECT_NAME_COLLISION;
58         }
59
60         backends = Realloc(backends, sizeof(backends[0]) * (num_backends+1));
61         if (!backends) {
62                 smb_panic("out of memory in ntvfs_register");
63         }
64
65         new_ops = smb_xmemdup(ops, sizeof(*ops));
66         new_ops->name = smb_xstrdup(ops->name);
67
68         backends[num_backends].ops = new_ops;
69
70         num_backends++;
71
72         DEBUG(3,("NTVFS backend '%s' for type %d registered\n", 
73                  ops->name,ops->type));
74
75         return NT_STATUS_OK;
76 }
77
78
79 /*
80   return the operations structure for a named backend of the specified type
81 */
82 const struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
83 {
84         int i;
85
86         for (i=0;i<num_backends;i++) {
87                 if (backends[i].ops->type == type && 
88                     strcmp(backends[i].ops->name, name) == 0) {
89                         return backends[i].ops;
90                 }
91         }
92
93         return NULL;
94 }
95
96
97 /*
98   return the NTVFS interface version, and the size of some critical types
99   This can be used by backends to either detect compilation errors, or provide
100   multiple implementations for different smbd compilation options in one module
101 */
102 const struct ntvfs_critical_sizes *ntvfs_interface_version(void)
103 {
104         static const struct ntvfs_critical_sizes critical_sizes = {
105                 NTVFS_INTERFACE_VERSION,
106                 sizeof(struct ntvfs_critical_sizes),
107                 sizeof(struct ntvfs_context),
108                 sizeof(struct ntvfs_module_context),
109                 sizeof(struct ntvfs_ops),
110                 sizeof(struct ntvfs_async_state),
111                 sizeof(struct smbsrv_tcon),
112                 sizeof(struct smbsrv_request),
113         };
114
115         return &critical_sizes;
116 }
117
118
119 /*
120   initialise the NTVFS subsystem
121 */
122 BOOL ntvfs_init(void)
123 {
124         NTSTATUS status;
125
126         status = register_subsystem("ntvfs", ntvfs_register); 
127         if (!NT_STATUS_IS_OK(status)) {
128                 return False;
129         }
130
131         /* FIXME: Perhaps panic if a basic backend, such as IPC, fails to initialise? */
132         static_init_ntvfs;
133
134         DEBUG(3,("NTVFS subsystem version %d initialised\n", NTVFS_INTERFACE_VERSION));
135         return True;
136 }
137
138
139 /*
140   initialise a connection structure to point at a NTVFS backend
141 */
142 NTSTATUS ntvfs_init_connection(struct smbsrv_request *req, enum ntvfs_type type)
143 {
144         const char **handlers = lp_ntvfs_handler(req->tcon->service);
145         int i;
146         struct ntvfs_context *ctx;
147
148         if (!handlers) {
149                 return NT_STATUS_FOOBAR;
150         }
151
152         ctx = talloc_p(req->tcon, struct ntvfs_context);
153         if (!ctx) {
154                 return NT_STATUS_NO_MEMORY;
155         }
156         ctx->type = type;
157         ctx->modules = NULL;
158
159         for (i=0; handlers[i]; i++) {
160                 struct ntvfs_module_context *ntvfs;
161
162                 ntvfs = talloc_p(ctx, struct ntvfs_module_context);
163                 if (!ntvfs) {
164                         return NT_STATUS_NO_MEMORY;
165                 }
166
167                 ntvfs->ops = ntvfs_backend_byname(handlers[i], ctx->type);
168                 if (!ntvfs->ops) {
169                         DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n",
170                                 handlers[i], ctx->type));
171                         return NT_STATUS_UNSUCCESSFUL;
172                 }
173                 ntvfs->depth = i;
174                 DLIST_ADD_END(ctx->modules, ntvfs, struct ntvfs_module_context *);
175         }
176
177         if (!ctx->modules) {
178                 talloc_free(ctx);
179                 return NT_STATUS_FOOBAR;
180         }
181
182         req->tcon->ntvfs_ctx = ctx;
183
184         return NT_STATUS_OK;
185 }