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 7262 : static bool _samba_cmdline_load_config_s3(void)
35 : {
36 7262 : struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
37 7262 : const char *config_file = NULL;
38 7262 : bool ok = false;
39 :
40 : /* Load smb conf */
41 7262 : config_file = lpcfg_configfile(lp_ctx);
42 7262 : if (config_file == NULL) {
43 7262 : if (is_default_dyn_CONFIGFILE()) {
44 3091 : const char *env = getenv("SMB_CONF_PATH");
45 3091 : if (env != NULL && strlen(env) > 0) {
46 3091 : set_dyn_CONFIGFILE(env);
47 : }
48 : }
49 : }
50 :
51 7262 : config_file = get_dyn_CONFIGFILE();
52 :
53 7262 : switch (_config_type) {
54 3748 : case SAMBA_CMDLINE_CONFIG_NONE:
55 3748 : return true;
56 2948 : case SAMBA_CMDLINE_CONFIG_CLIENT:
57 2948 : ok = lp_load_client(config_file);
58 2948 : break;
59 566 : case SAMBA_CMDLINE_CONFIG_SERVER:
60 : {
61 392 : const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg =
62 174 : samba_cmdline_get_daemon_cfg();
63 :
64 566 : if (!cmdline_daemon_cfg->interactive) {
65 566 : setup_logging(getprogname(), DEBUG_FILE);
66 : }
67 :
68 566 : ok = lp_load_global(config_file);
69 566 : break;
70 : }
71 : }
72 :
73 3514 : 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 3514 : load_interfaces();
84 :
85 3514 : return true;
86 : }
87 :
88 53 : static NTSTATUS _samba_cmd_set_machine_account_s3(
89 : struct cli_credentials *cred,
90 : struct loadparm_context *lp_ctx)
91 : {
92 53 : struct db_context *db_ctx = secrets_db_ctx();
93 : NTSTATUS status;
94 :
95 53 : if (db_ctx == NULL) {
96 0 : DBG_WARNING("failed to open secrets.tdb to obtain our "
97 : "trust credentials for %s\n",
98 : lpcfg_workgroup(lp_ctx));;
99 0 : return NT_STATUS_INTERNAL_ERROR;
100 : }
101 :
102 53 : status = cli_credentials_set_machine_account_db_ctx(
103 : cred, lp_ctx, db_ctx);
104 53 : if (!NT_STATUS_IS_OK(status)) {
105 0 : DBG_WARNING("cli_credentials_set_machine_account_db_ctx "
106 : "failed: %s\n",
107 : nt_errstr(status));
108 : }
109 :
110 53 : return status;
111 : }
112 :
113 5154 : bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
114 : enum samba_cmdline_config_type config_type,
115 : bool require_smbconf)
116 : {
117 5154 : struct loadparm_context *lp_ctx = NULL;
118 5154 : struct cli_credentials *creds = NULL;
119 : bool ok;
120 :
121 5154 : ok = samba_cmdline_init_common(mem_ctx);
122 5154 : if (!ok) {
123 0 : return false;
124 : }
125 :
126 5154 : lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
127 5154 : if (lp_ctx == NULL) {
128 0 : return false;
129 : }
130 5154 : ok = samba_cmdline_set_lp_ctx(lp_ctx);
131 5154 : if (!ok) {
132 0 : return false;
133 : }
134 :
135 5154 : _require_smbconf = require_smbconf;
136 5154 : _config_type = config_type;
137 :
138 5154 : creds = cli_credentials_init(mem_ctx);
139 5154 : if (creds == NULL) {
140 0 : return false;
141 : }
142 5154 : ok = samba_cmdline_set_creds(creds);
143 5154 : if (!ok) {
144 0 : return false;
145 : }
146 :
147 5154 : samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s3);
148 5154 : samba_cmdline_set_machine_account_fn(
149 : _samba_cmd_set_machine_account_s3);
150 :
151 5154 : return true;
152 : }
|