Line data Source code
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/cmdline.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 "dsdb/common/util.h"
33 : #include "ldb_wrap.h"
34 : #include "popt.h"
35 :
36 :
37 41184 : static bool is_popt_table_end(const struct poptOption *o)
38 : {
39 50130 : if (o->longName == NULL &&
40 19184 : o->shortName =='\0' &&
41 9984 : o->arg == NULL) {
42 2496 : return true;
43 : }
44 :
45 38688 : return false;
46 : }
47 :
48 : /*
49 : work out the length of a popt array
50 : */
51 2496 : static size_t calculate_popt_array_length(struct poptOption *opts)
52 : {
53 2496 : size_t i = 0;
54 :
55 41184 : for (i = 0; i < UINT32_MAX; i++) {
56 41184 : struct poptOption *o = &(opts[i]);
57 :
58 41184 : if (is_popt_table_end(o)) {
59 2496 : break;
60 : }
61 : }
62 :
63 2496 : return i;
64 : }
65 :
66 : /*
67 : called to register additional command line options
68 : */
69 3699 : static int extensions_hook(struct ldb_context *ldb, enum ldb_module_hook_type t)
70 : {
71 3699 : switch (t) {
72 1248 : case LDB_MODULE_HOOK_CMDLINE_OPTIONS: {
73 : size_t len1, len2;
74 1248 : struct poptOption **popt_options = ldb_module_popt_options(ldb);
75 1248 : struct poptOption *new_array = NULL;
76 : bool ok;
77 :
78 7488 : struct poptOption cmdline_extensions[] = {
79 1248 : POPT_COMMON_SAMBA_LDB
80 1248 : POPT_COMMON_CONNECTION
81 1248 : POPT_COMMON_CREDENTIALS
82 1248 : POPT_LEGACY_S4
83 1248 : POPT_COMMON_VERSION
84 : POPT_TABLEEND
85 : };
86 :
87 1248 : ok = samba_cmdline_init(ldb,
88 : SAMBA_CMDLINE_CONFIG_CLIENT,
89 : false /* require_smbconf */);
90 1248 : if (!ok) {
91 0 : return ldb_oom(ldb);
92 : }
93 :
94 1248 : len1 = calculate_popt_array_length(*popt_options);
95 1248 : len2 = calculate_popt_array_length(cmdline_extensions);
96 1248 : new_array = talloc_array(ldb,
97 : struct poptOption,
98 : len1 + len2 + 1);
99 1248 : if (NULL == new_array) {
100 0 : return ldb_oom(ldb);
101 : }
102 :
103 1248 : memcpy(new_array, *popt_options, len1*sizeof(struct poptOption));
104 1248 : memcpy(new_array+len1, cmdline_extensions, (1+len2)*sizeof(struct poptOption));
105 :
106 : #ifdef DEVELOPER
107 1248 : ok = samba_cmdline_sanity_check(new_array);
108 1248 : if (!ok) {
109 0 : talloc_free(new_array);
110 0 : return ldb_error(ldb,
111 : LDB_ERR_OPERATIONS_ERROR,
112 : "Duplicate cmdline options detected!");
113 : }
114 : #endif
115 :
116 1248 : (*popt_options) = new_array;
117 1248 : return LDB_SUCCESS;
118 : }
119 :
120 1238 : case LDB_MODULE_HOOK_CMDLINE_PRECONNECT: {
121 1238 : struct loadparm_context *lp_ctx = NULL;
122 1238 : struct cli_credentials *creds = NULL;
123 :
124 1238 : int r = ldb_register_samba_handlers(ldb);
125 1238 : if (r != LDB_SUCCESS) {
126 0 : return ldb_operr(ldb);
127 : }
128 1238 : gensec_init();
129 :
130 1238 : lp_ctx = samba_cmdline_get_lp_ctx();
131 1238 : creds = samba_cmdline_get_creds();
132 :
133 1238 : if (ldb_set_opaque(
134 : ldb,
135 : DSDB_SESSION_INFO,
136 1238 : system_session(lp_ctx))) {
137 :
138 0 : return ldb_operr(ldb);
139 : }
140 1238 : if (ldb_set_opaque(ldb, "credentials", creds)) {
141 0 : return ldb_operr(ldb);
142 : }
143 1238 : if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
144 0 : return ldb_operr(ldb);
145 : }
146 :
147 1238 : ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
148 1238 : break;
149 : }
150 :
151 1213 : case LDB_MODULE_HOOK_CMDLINE_POSTCONNECT:
152 : /* get the domain SID into the cache for SDDL processing */
153 1213 : samdb_domain_sid(ldb);
154 1213 : break;
155 : }
156 :
157 2451 : return LDB_SUCCESS;
158 : }
159 :
160 :
161 : /*
162 : initialise the module
163 : */
164 4313 : _PUBLIC_ int ldb_samba_extensions_init(const char *ldb_version)
165 : {
166 4313 : ldb_register_hook(extensions_hook);
167 :
168 4313 : return LDB_SUCCESS;
169 : }
|