LCOV - code coverage report
Current view: top level - lib/cmdline - cmdline_s3.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 53 65 81.5 %
Date: 2024-02-28 12:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "lib/replace/replace.h"
      19             : #include <talloc.h>
      20             : #include "lib/param/param.h"
      21             : #include "lib/util/debug.h"
      22             : #include "lib/util/fault.h"
      23             : #include "source3/param/loadparm.h"
      24             : #include "dynconfig/dynconfig.h"
      25             : #include "source3/lib/interface.h"
      26             : #include "auth/credentials/credentials.h"
      27             : #include "dynconfig/dynconfig.h"
      28             : #include "cmdline_private.h"
      29             : #include "source3/include/secrets.h"
      30             : 
      31             : static bool _require_smbconf;
      32             : static enum samba_cmdline_config_type _config_type;
      33             : 
      34       28034 : static bool _samba_cmdline_load_config_s3(void)
      35             : {
      36       28034 :         struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
      37       28034 :         const char *config_file = NULL;
      38       28034 :         bool ok = false;
      39             : 
      40             :         /* Load smb conf */
      41       28034 :         config_file = lpcfg_configfile(lp_ctx);
      42       28034 :         if (config_file == NULL) {
      43       28034 :                 if (is_default_dyn_CONFIGFILE()) {
      44       15945 :                         const char *env = getenv("SMB_CONF_PATH");
      45       15945 :                         if (env != NULL && strlen(env) > 0) {
      46       15945 :                                 set_dyn_CONFIGFILE(env);
      47             :                         }
      48             :                 }
      49             :         }
      50             : 
      51       28034 :         config_file = get_dyn_CONFIGFILE();
      52             : 
      53       28034 :         switch (_config_type) {
      54        3990 :         case SAMBA_CMDLINE_CONFIG_NONE:
      55        3990 :                 return true;
      56       22449 :         case SAMBA_CMDLINE_CONFIG_CLIENT:
      57       22449 :                 ok = lp_load_client(config_file);
      58       22449 :                 break;
      59        1587 :         case SAMBA_CMDLINE_CONFIG_SERVER:
      60             :         {
      61          14 :                 const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg =
      62        1587 :                         samba_cmdline_get_daemon_cfg();
      63             : 
      64        1587 :                 if (!cmdline_daemon_cfg->interactive) {
      65        1584 :                         setup_logging(getprogname(), DEBUG_FILE);
      66             :                 }
      67             : 
      68        1587 :                 ok = lp_load_global(config_file);
      69        1587 :                 break;
      70             :         }
      71             :         }
      72             : 
      73       24036 :         if (!ok) {
      74           0 :                 fprintf(stderr,
      75             :                         "Can't load %s - run testparm to debug it\n",
      76             :                         config_file);
      77             : 
      78           0 :                 if (_require_smbconf) {
      79           0 :                         return false;
      80             :                 }
      81             :         }
      82             : 
      83       24036 :         load_interfaces();
      84             : 
      85       24036 :         return true;
      86             : }
      87             : 
      88         124 : static NTSTATUS _samba_cmd_set_machine_account_s3(
      89             :         struct cli_credentials *cred,
      90             :         struct loadparm_context *lp_ctx)
      91             : {
      92         124 :         struct db_context *db_ctx = secrets_db_ctx();
      93           0 :         NTSTATUS status;
      94             : 
      95         124 :         if (db_ctx == NULL) {
      96           0 :                 DBG_WARNING("failed to open secrets.tdb to obtain our "
      97             :                             "trust credentials for %s\n",
      98           0 :                             lpcfg_workgroup(lp_ctx));;
      99           0 :                 return NT_STATUS_INTERNAL_ERROR;
     100             :         }
     101             : 
     102         124 :         status = cli_credentials_set_machine_account_db_ctx(
     103             :                 cred, lp_ctx, db_ctx);
     104         124 :         if (!NT_STATUS_IS_OK(status)) {
     105          10 :                 DBG_WARNING("cli_credentials_set_machine_account_db_ctx "
     106             :                             "failed: %s\n",
     107             :                             nt_errstr(status));
     108             :         }
     109             : 
     110         124 :         return status;
     111             : }
     112             : 
     113       25467 : bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
     114             :                         enum samba_cmdline_config_type config_type,
     115             :                         bool require_smbconf)
     116             : {
     117       25467 :         struct loadparm_context *lp_ctx = NULL;
     118       25467 :         struct cli_credentials *creds = NULL;
     119          28 :         bool ok;
     120             : 
     121       25467 :         ok = samba_cmdline_init_common(mem_ctx);
     122       25467 :         if (!ok) {
     123           0 :                 return false;
     124             :         }
     125             : 
     126       25467 :         lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
     127       25467 :         if (lp_ctx == NULL) {
     128           0 :                 return false;
     129             :         }
     130       25467 :         ok = samba_cmdline_set_lp_ctx(lp_ctx);
     131       25467 :         if (!ok) {
     132           0 :                 return false;
     133             :         }
     134             : 
     135       25467 :         _require_smbconf = require_smbconf;
     136       25467 :         _config_type = config_type;
     137             : 
     138       25467 :         creds = cli_credentials_init(mem_ctx);
     139       25467 :         if (creds == NULL) {
     140           0 :                 return false;
     141             :         }
     142       25467 :         ok = samba_cmdline_set_creds(creds);
     143       25467 :         if (!ok) {
     144           0 :                 return false;
     145             :         }
     146             : 
     147       25467 :         samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s3);
     148       25467 :         samba_cmdline_set_machine_account_fn(
     149             :                 _samba_cmd_set_machine_account_s3);
     150             : 
     151       25467 :         return true;
     152             : }

Generated by: LCOV version 1.14