summaryrefslogtreecommitdiff
path: root/lib/cmdline/cmdline_s3.c
blob: 6e2c154c756baa961db6d68ea7c97bf18a223298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "lib/replace/replace.h"
#include <talloc.h>
#include "lib/param/param.h"
#include "lib/util/debug.h"
#include "lib/util/fault.h"
#include "source3/param/loadparm.h"
#include "dynconfig/dynconfig.h"
#include "source3/lib/interface.h"
#include "auth/credentials/credentials.h"
#include "dynconfig/dynconfig.h"
#include "cmdline_private.h"
#include "source3/include/secrets.h"

static bool _require_smbconf;
static enum samba_cmdline_config_type _config_type;

static bool _samba_cmdline_load_config_s3(void)
{
	struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
	const char *config_file = NULL;
	bool ok = false;

	/* Load smb conf */
	config_file = lpcfg_configfile(lp_ctx);
	if (config_file == NULL) {
		if (is_default_dyn_CONFIGFILE()) {
			const char *env = getenv("SMB_CONF_PATH");
			if (env != NULL && strlen(env) > 0) {
				set_dyn_CONFIGFILE(env);
			}
		}
	}

	config_file = get_dyn_CONFIGFILE();

	switch (_config_type) {
	case SAMBA_CMDLINE_CONFIG_NONE:
		return true;
	case SAMBA_CMDLINE_CONFIG_CLIENT:
		ok = lp_load_client(config_file);
		break;
	case SAMBA_CMDLINE_CONFIG_SERVER:
	{
		const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg =
			samba_cmdline_get_daemon_cfg();

		if (!cmdline_daemon_cfg->interactive) {
			setup_logging(getprogname(), DEBUG_FILE);
		}

		ok = lp_load_global(config_file);
		break;
	}
	}

	if (!ok) {
		fprintf(stderr,
			"Can't load %s - run testparm to debug it\n",
			config_file);

		if (_require_smbconf) {
			return false;
		}
	}

	load_interfaces();

	return true;
}

static NTSTATUS _samba_cmd_set_machine_account_s3(
	struct cli_credentials *cred,
	struct loadparm_context *lp_ctx)
{
	struct db_context *db_ctx = secrets_db_ctx();
	NTSTATUS status;

	if (db_ctx == NULL) {
		DBG_WARNING("failed to open secrets.tdb to obtain our "
			    "trust credentials for %s\n",
			    lpcfg_workgroup(lp_ctx));;
		return NT_STATUS_INTERNAL_ERROR;
	}

	status = cli_credentials_set_machine_account_db_ctx(
		cred, lp_ctx, db_ctx);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_WARNING("cli_credentials_set_machine_account_db_ctx "
			    "failed: %s\n",
			    nt_errstr(status));
	}

	return status;
}

bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
			enum samba_cmdline_config_type config_type,
			bool require_smbconf)
{
	struct loadparm_context *lp_ctx = NULL;
	struct cli_credentials *creds = NULL;
	bool ok;

	ok = samba_cmdline_init_common(mem_ctx);
	if (!ok) {
		return false;
	}

	lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
	if (lp_ctx == NULL) {
		return false;
	}
	ok = samba_cmdline_set_lp_ctx(lp_ctx);
	if (!ok) {
		return false;
	}

	_require_smbconf = require_smbconf;
	_config_type = config_type;

	creds = cli_credentials_init(mem_ctx);
	if (creds == NULL) {
		return false;
	}
	ok = samba_cmdline_set_creds(creds);
	if (!ok) {
		return false;
	}

	samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s3);
	samba_cmdline_set_machine_account_fn(
		_samba_cmd_set_machine_account_s3);

	return true;
}