first public release of samba4 code
[samba.git] / source4 / ntvfs / ntvfs_base.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NTVFS base code
4    Copyright (C) Andrew Tridgell 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   this implements the core code for all NTVFS modules. Backends register themselves here.
22 */
23
24 #include "includes.h"
25
26
27 /* the list of currently registered NTVFS backends, note that there
28  * can be more than one backend with the same name, as long as they
29  * have different typesx */
30 static struct {
31         const char *name;
32         enum ntvfs_type type;
33         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 BOOL ntvfs_register(const char *name, enum ntvfs_type type, struct ntvfs_ops *ops)
46 {
47         if (ntvfs_backend_byname(name, type) != NULL) {
48                 /* its already registered! */
49                 DEBUG(2,("NTVFS backend '%s' for type %d already registered\n", 
50                          name, (int)type));
51                 return False;
52         }
53
54         backends = Realloc(backends, sizeof(backends[0]) * (num_backends+1));
55         if (!backends) {
56                 smb_panic("out of memory in ntvfs_register");
57         }
58
59         backends[num_backends].name = smb_xstrdup(name);
60         backends[num_backends].type = type;
61         backends[num_backends].ops = smb_xmemdup(ops, sizeof(*ops));
62
63         num_backends++;
64
65         return True;
66 }
67
68
69 /*
70   return the operations structure for a named backend of the specified type
71 */
72 struct ntvfs_ops *ntvfs_backend_byname(const char *name, enum ntvfs_type type)
73 {
74         int i;
75
76         for (i=0;i<num_backends;i++) {
77                 if (backends[i].type == type && 
78                     strcmp(backends[i].name, name) == 0) {
79                         return backends[i].ops;
80                 }
81         }
82
83         return NULL;
84 }
85
86
87 /*
88   return the NTVFS interface version, and the size of some critical types
89   This can be used by backends to either detect compilation errors, or provide
90   multiple implementations for different smbd compilation options in one module
91 */
92 int ntvfs_interface_version(struct ntvfs_critical_sizes *sizes)
93 {
94         sizes->sizeof_ntvfs_ops = sizeof(struct ntvfs_ops);
95         sizes->sizeof_SMB_OFF_T = sizeof(SMB_OFF_T);
96         sizes->sizeof_tcon_context = sizeof(struct tcon_context);
97         sizes->sizeof_request_context = sizeof(struct request_context);
98
99         return NTVFS_INTERFACE_VERSION;
100 }
101
102
103 /*
104   initialise the NTVFS subsystem
105 */
106 BOOL ntvfs_init(void)
107 {
108         /* initialise our 3 basic backends. These are assumed to be
109          * present and are always built in */
110         if (!posix_vfs_init() ||
111             !ipc_vfs_init() ||
112             !print_vfs_init()) {
113                 return False;
114         }
115         /* initialize optional backends, e.g. CIFS. We allow failures here. */
116         cifs_vfs_init();
117
118 #if WITH_NTVFS_STFS
119         tank_vfs_init();
120 #endif
121
122         DEBUG(3,("NTVFS version %d initialised\n", NTVFS_INTERFACE_VERSION));
123         return True;
124 }
125
126
127 /*
128   initialise a connection structure to point at a NTVFS backend
129 */
130 NTSTATUS ntvfs_init_connection(struct request_context *req)
131 {
132         const char *handler = lp_ntvfs_handler(req->conn->service);
133         
134         if (strequal(handler, "default"))
135                 handler = "ipc";
136
137         req->conn->ntvfs_ops = ntvfs_backend_byname(handler, req->conn->type);
138
139         if (!req->conn->ntvfs_ops) {
140                 DEBUG(1,("ntvfs_init_connection: failed to find backend=%s, type=%d\n", handler, req->conn->type));
141                 return NT_STATUS_UNSUCCESSFUL;
142         }
143
144         return NT_STATUS_OK;
145 }