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