LCOV - code coverage report
Current view: top level - lib/ldb-samba - samba_extensions.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 57 65 87.7 %
Date: 2021-08-25 13:27:56 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.13