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