Refactor the dns_open_connection code so that duplicate code is removed and ensure...
[sfrench/samba-autobuild/.git] / lib / ldb-samba / samba_extensions.c
1 /*
2    ldb database library - samba extensions
3
4    Copyright (C) Andrew Tridgell  2010
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24
25 #include "includes.h"
26 #include "ldb_module.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/auth.h"
30 #include "param/param.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "ldb_wrap.h"
33 #include "popt.h"
34
35
36
37 /*
38   work out the length of a popt array
39  */
40 static unsigned calculate_popt_array_length(struct poptOption *opts)
41 {
42         unsigned i;
43         struct poptOption zero_opt = { NULL };
44         for (i=0; memcmp(&zero_opt, &opts[i], sizeof(zero_opt)) != 0; i++) ;
45         return i;
46 }
47
48 static struct poptOption cmdline_extensions[] = {
49         POPT_COMMON_SAMBA
50         POPT_COMMON_CREDENTIALS
51         POPT_COMMON_CONNECTION
52         POPT_COMMON_VERSION
53         { NULL }
54 };
55
56 /*
57   called to register additional command line options
58  */
59 static int extensions_hook(struct ldb_context *ldb, enum ldb_module_hook_type t)
60 {
61         switch (t) {
62         case LDB_MODULE_HOOK_CMDLINE_OPTIONS: {
63                 unsigned len1, len2;
64                 struct poptOption **popt_options = ldb_module_popt_options(ldb);
65                 struct poptOption *new_array;
66
67                 len1 = calculate_popt_array_length(*popt_options);
68                 len2 = calculate_popt_array_length(cmdline_extensions);
69                 new_array = talloc_array(NULL, struct poptOption, len1+len2+1);
70                 if (NULL == new_array) {
71                         return ldb_oom(ldb);
72                 }
73
74                 memcpy(new_array, *popt_options, len1*sizeof(struct poptOption));
75                 memcpy(new_array+len1, cmdline_extensions, (1+len2)*sizeof(struct poptOption));
76                 (*popt_options) = new_array;
77                 return LDB_SUCCESS;
78         }
79
80         case LDB_MODULE_HOOK_CMDLINE_PRECONNECT: {
81                 int r = ldb_register_samba_handlers(ldb);
82                 if (r != LDB_SUCCESS) {
83                         return ldb_operr(ldb);
84                 }
85                 gensec_init();
86
87                 if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
88                         return ldb_operr(ldb);
89                 }
90                 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
91                         return ldb_operr(ldb);
92                 }
93                 if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
94                         return ldb_operr(ldb);
95                 }
96
97                 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
98                 break;
99         }
100
101         case LDB_MODULE_HOOK_CMDLINE_POSTCONNECT:
102                 /* get the domain SID into the cache for SDDL processing */
103                 samdb_domain_sid(ldb);
104                 break;
105         }
106
107         return LDB_SUCCESS;
108 }
109
110
111 /*
112   initialise the module
113  */
114 _PUBLIC_ int ldb_samba_extensions_init(const char *ldb_version)
115 {
116         ldb_register_hook(extensions_hook);
117
118         return LDB_SUCCESS;
119 }