9b89f8a6fc9ab7bb0e6ef91769c93bbdb0736a29
[samba.git] / source4 / ntptr / ntptr_base.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NTPTR base code
5
6    Copyright (C) Stefan (metze) Metzmacher 2005
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 NTPTR modules. Backends register themselves here.
23 */
24
25 #include "includes.h"
26 #include "ntptr/ntptr.h"
27 #include "param/param.h"
28 #include "lib/util/samba_module.h"
29
30 /* the list of currently registered NTPTR backends */
31 static struct ntptr_backend {
32         const struct ntptr_ops *ops;
33 } *backends = NULL;
34 static int num_backends;
35
36 /*
37   register a NTPTR backend. 
38
39   The 'name' can be later used by other backends to find the operations
40   structure for this backend.
41 */
42 NTSTATUS ntptr_register(const void *_ops)
43 {
44         const struct ntptr_ops *ops = _ops;
45         struct ntptr_ops *new_ops;
46
47         if (ntptr_backend_byname(ops->name) != NULL) {
48                 /* its already registered! */
49                 DEBUG(0,("NTPTR backend '%s' already registered\n", 
50                          ops->name));
51                 return NT_STATUS_OBJECT_NAME_COLLISION;
52         }
53
54         backends = realloc_p(backends, struct ntptr_backend, num_backends+1);
55         if (!backends) {
56                 smb_panic("out of memory in ntptr_register");
57         }
58
59         new_ops = smb_xmemdup(ops, sizeof(*ops));
60         new_ops->name = smb_xstrdup(ops->name);
61
62         backends[num_backends].ops = new_ops;
63
64         num_backends++;
65
66         DEBUG(3,("NTPTR backend '%s'\n", 
67                  ops->name));
68
69         return NT_STATUS_OK;
70 }
71
72 NTSTATUS ntptr_init(void)
73 {
74 #define _MODULE_PROTO(init) extern NTSTATUS init(void);
75         STATIC_ntptr_MODULES_PROTO;
76         samba_module_init_fn static_init[] = { STATIC_ntptr_MODULES };
77         samba_module_init_fn *shared_init = samba_module_init_fns_for_subsystem(NULL, "ntptr");
78
79         samba_module_init_fns_run(static_init);
80         samba_module_init_fns_run(shared_init);
81
82         talloc_free(shared_init);
83         
84         return NT_STATUS_OK;    
85 }
86
87
88 /*
89   return the operations structure for a named backend
90 */
91 const struct ntptr_ops *ntptr_backend_byname(const char *name)
92 {
93         int i;
94
95         for (i=0;i<num_backends;i++) {
96                 if (strcmp(backends[i].ops->name, name) == 0) {
97                         return backends[i].ops;
98                 }
99         }
100
101         return NULL;
102 }
103
104
105 /*
106   return the NTPTR interface version, and the size of some critical types
107   This can be used by backends to either detect compilation errors, or provide
108   multiple implementations for different smbd compilation options in one module
109 */
110 static const struct ntptr_critical_sizes critical_sizes = {
111         .interface_version              = NTPTR_INTERFACE_VERSION,
112         .sizeof_ntptr_critical_sizes    = sizeof(struct ntptr_critical_sizes),
113         .sizeof_ntptr_context           = sizeof(struct ntptr_context),
114         .sizeof_ntptr_ops               = sizeof(struct ntptr_ops),
115 };
116 const struct ntptr_critical_sizes *ntptr_interface_version(void)
117 {
118         return &critical_sizes;
119 }
120
121 /*
122   create a ntptr_context with a specified NTPTR backend
123 */
124 NTSTATUS ntptr_init_context(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx,
125                             struct loadparm_context *lp_ctx,
126                             const char *providor, struct ntptr_context **_ntptr)
127 {
128         NTSTATUS status;
129         struct ntptr_context *ntptr;
130
131         if (!providor) {
132                 return NT_STATUS_INTERNAL_ERROR;
133         }
134
135         ntptr = talloc(mem_ctx, struct ntptr_context);
136         NT_STATUS_HAVE_NO_MEMORY(ntptr);
137         ntptr->private_data     = NULL;
138         ntptr->ops              = ntptr_backend_byname(providor);
139         ntptr->ev_ctx           = ev_ctx;
140         ntptr->lp_ctx           = lp_ctx;
141
142         if (!ntptr->ops) {
143                 DEBUG(1,("ntptr_init_context: failed to find NTPTR providor='%s'\n",
144                          providor));
145                 return NT_STATUS_INTERNAL_ERROR;
146         }
147
148         status = ntptr->ops->init_context(ntptr);
149         NT_STATUS_NOT_OK_RETURN(status);
150
151         *_ntptr = ntptr;
152         return NT_STATUS_OK;
153 }