summaryrefslogtreecommitdiffstats
path: root/source3/lib/cmdline_contexts.c
blob: 627ee4f3164607b43ca641dc193bf0c6c177feaa (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
/*
   Unix SMB/CIFS implementation.
   cmdline context wrapper.

   Copyright (C) Christof Schmitt <cs@samba.org> 2018

   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 "cmdline_contexts.h"
#include "includes.h"
#include "messages.h"
#include "lib/global_contexts.h"

struct messaging_context *cmdline_messaging_context(const char *config_file)
{
	struct messaging_context *msg_ctx = NULL;

	/*
	 * Ensure that a config is loaded, in case the underlying
	 * messaging_init needs to create directories or sockets.
	 */
	if (!lp_loaded()) {
		if (!lp_load_initial_only(config_file)) {
			return NULL;
		}
	}

	/*
	 * Clustered Samba can only work as root due to required
	 * access to the registry and ctdb, which in turn requires
	 * messaging access as root.
	 */
	if (lp_clustering() && geteuid() != 0) {
		fprintf(stderr, "Cluster mode requires running as root.\n");
		exit(1);
	}

	msg_ctx = global_messaging_context();
	if (msg_ctx == NULL) {
		if (geteuid() == 0) {
			fprintf(stderr,
				"Unable to initialize messaging context!\n");
			exit(1);
		} else {
			/*
			 * Non-cluster, non-root: Log error, but leave
			 * it up to the caller how to proceed.
			 */
			DBG_NOTICE("Unable to initialize messaging context.\n");
		}
	}

	return msg_ctx;
}

void cmdline_messaging_context_free(void)
{
	global_messaging_context_free();
}