sync machine password to keytab: handle FreeIPA use case
[Samba.git] / lib / ldb-samba / samba_extensions.c
blobaecc2d70deacc57741f2fd81be9c4428aeb9321f
1 /*
2 ldb database library - samba extensions
4 Copyright (C) Andrew Tridgell 2010
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
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.
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.
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/>.
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"
37 static bool is_popt_table_end(const struct poptOption *o)
39 if (o->longName == NULL &&
40 o->shortName =='\0' &&
41 o->arg == NULL) {
42 return true;
45 return false;
49 work out the length of a popt array
51 static size_t calculate_popt_array_length(struct poptOption *opts)
53 size_t i = 0;
55 for (i = 0; i < UINT32_MAX; i++) {
56 struct poptOption *o = &(opts[i]);
58 if (is_popt_table_end(o)) {
59 break;
63 return i;
67 called to register additional command line options
69 static int extensions_hook(struct ldb_context *ldb, enum ldb_module_hook_type t)
71 switch (t) {
72 case LDB_MODULE_HOOK_CMDLINE_OPTIONS: {
73 size_t len1, len2;
74 struct poptOption **popt_options = ldb_module_popt_options(ldb);
75 struct poptOption *new_array = NULL;
76 bool ok;
78 struct poptOption cmdline_extensions[] = {
79 POPT_COMMON_SAMBA_LDB
80 POPT_COMMON_CONNECTION
81 POPT_COMMON_CREDENTIALS
82 POPT_LEGACY_S4
83 POPT_COMMON_VERSION
84 POPT_TABLEEND
87 ok = samba_cmdline_init(ldb,
88 SAMBA_CMDLINE_CONFIG_CLIENT,
89 false /* require_smbconf */);
90 if (!ok) {
91 return ldb_oom(ldb);
94 len1 = calculate_popt_array_length(*popt_options);
95 len2 = calculate_popt_array_length(cmdline_extensions);
96 new_array = talloc_array(ldb,
97 struct poptOption,
98 len1 + len2 + 1);
99 if (NULL == new_array) {
100 return ldb_oom(ldb);
103 memcpy(new_array, *popt_options, len1*sizeof(struct poptOption));
104 memcpy(new_array+len1, cmdline_extensions, (1+len2)*sizeof(struct poptOption));
106 #ifdef DEVELOPER
107 ok = samba_cmdline_sanity_check(new_array);
108 if (!ok) {
109 talloc_free(new_array);
110 return ldb_error(ldb,
111 LDB_ERR_OPERATIONS_ERROR,
112 "Duplicate cmdline options detected!");
114 #endif
116 (*popt_options) = new_array;
117 return LDB_SUCCESS;
120 case LDB_MODULE_HOOK_CMDLINE_PRECONNECT: {
121 struct loadparm_context *lp_ctx = NULL;
122 struct cli_credentials *creds = NULL;
124 int r = ldb_register_samba_handlers(ldb);
125 if (r != LDB_SUCCESS) {
126 return ldb_operr(ldb);
128 gensec_init();
130 lp_ctx = samba_cmdline_get_lp_ctx();
131 creds = samba_cmdline_get_creds();
133 if (ldb_set_opaque(
134 ldb,
135 DSDB_SESSION_INFO,
136 system_session(lp_ctx))) {
138 return ldb_operr(ldb);
140 if (ldb_set_opaque(ldb, "credentials", creds)) {
141 return ldb_operr(ldb);
143 if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
144 return ldb_operr(ldb);
147 ldb_set_utf8_functions(ldb, NULL, wrap_casefold, ldb_comparison_fold_utf8);
148 break;
151 case LDB_MODULE_HOOK_CMDLINE_POSTCONNECT:
152 /* get the domain SID into the cache for SDDL processing */
153 samdb_domain_sid(ldb);
154 break;
157 return LDB_SUCCESS;
162 initialise the module
164 _PUBLIC_ int ldb_samba_extensions_init(const char *ldb_version)
166 ldb_register_hook(extensions_hook);
168 return LDB_SUCCESS;