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 24448 : static bool _samba_cmdline_load_config_s3(void) 35 : { 36 24448 : struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx(); 37 24448 : const char *config_file = NULL; 38 24448 : bool ok = false; 39 : 40 : /* Load smb conf */ 41 24448 : config_file = lpcfg_configfile(lp_ctx); 42 24448 : if (config_file == NULL) { 43 24448 : if (is_default_dyn_CONFIGFILE()) { 44 12780 : const char *env = getenv("SMB_CONF_PATH"); 45 12780 : if (env != NULL && strlen(env) > 0) { 46 12780 : set_dyn_CONFIGFILE(env); 47 : } 48 : } 49 : } 50 : 51 24448 : config_file = get_dyn_CONFIGFILE(); 52 : 53 24448 : switch (_config_type) { 54 3990 : case SAMBA_CMDLINE_CONFIG_NONE: 55 3990 : return true; 56 18897 : case SAMBA_CMDLINE_CONFIG_CLIENT: 57 18897 : ok = lp_load_client(config_file); 58 18897 : break; 59 1553 : case SAMBA_CMDLINE_CONFIG_SERVER: 60 : { 61 14 : const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg = 62 1553 : samba_cmdline_get_daemon_cfg(); 63 : 64 1553 : if (!cmdline_daemon_cfg->interactive) { 65 1550 : setup_logging(getprogname(), DEBUG_FILE); 66 : } 67 : 68 1553 : ok = lp_load_global(config_file); 69 1553 : break; 70 : } 71 : } 72 : 73 20450 : 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 20450 : load_interfaces(); 84 : 85 20450 : 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 21881 : bool samba_cmdline_init(TALLOC_CTX *mem_ctx, 114 : enum samba_cmdline_config_type config_type, 115 : bool require_smbconf) 116 : { 117 21881 : struct loadparm_context *lp_ctx = NULL; 118 21881 : struct cli_credentials *creds = NULL; 119 28 : bool ok; 120 : 121 21881 : ok = samba_cmdline_init_common(mem_ctx); 122 21881 : if (!ok) { 123 0 : return false; 124 : } 125 : 126 21881 : lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers()); 127 21881 : if (lp_ctx == NULL) { 128 0 : return false; 129 : } 130 21881 : ok = samba_cmdline_set_lp_ctx(lp_ctx); 131 21881 : if (!ok) { 132 0 : return false; 133 : } 134 : 135 21881 : _require_smbconf = require_smbconf; 136 21881 : _config_type = config_type; 137 : 138 21881 : creds = cli_credentials_init(mem_ctx); 139 21881 : if (creds == NULL) { 140 0 : return false; 141 : } 142 21881 : ok = samba_cmdline_set_creds(creds); 143 21881 : if (!ok) { 144 0 : return false; 145 : } 146 : 147 21881 : samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s3); 148 21881 : samba_cmdline_set_machine_account_fn( 149 : _samba_cmd_set_machine_account_s3); 150 : 151 21881 : return true; 152 : }