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 "auth/credentials/credentials.h"
24 : #include "dynconfig/dynconfig.h"
25 : #include "cmdline_private.h"
26 :
27 : static bool _require_smbconf;
28 : static enum samba_cmdline_config_type _config_type;
29 :
30 3087 : static bool _samba_cmdline_load_config_s4(void)
31 : {
32 3087 : struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
33 3087 : const char *config_file = NULL;
34 2350 : const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg = \
35 737 : samba_cmdline_get_daemon_cfg();
36 : bool ok;
37 :
38 : /* Load smb conf */
39 3087 : config_file = lpcfg_configfile(lp_ctx);
40 3087 : if (config_file == NULL) {
41 3087 : if (is_default_dyn_CONFIGFILE()) {
42 1153 : const char *env = getenv("SMB_CONF_PATH");
43 1153 : if (env != NULL && strlen(env) > 0) {
44 1111 : set_dyn_CONFIGFILE(env);
45 : }
46 : }
47 : }
48 :
49 3087 : switch (_config_type) {
50 53 : case SAMBA_CMDLINE_CONFIG_SERVER:
51 53 : if (!cmdline_daemon_cfg->interactive) {
52 0 : setup_logging(getprogname(), DEBUG_FILE);
53 : }
54 53 : break;
55 3034 : default:
56 3034 : break;
57 : }
58 :
59 3087 : config_file = get_dyn_CONFIGFILE();
60 3087 : ok = lpcfg_load(lp_ctx, config_file);
61 3087 : if (!ok) {
62 93 : fprintf(stderr,
63 : "Can't load %s - run testparm to debug it\n",
64 : config_file);
65 :
66 93 : if (_require_smbconf) {
67 0 : return false;
68 : }
69 : }
70 :
71 3087 : switch (_config_type) {
72 53 : case SAMBA_CMDLINE_CONFIG_SERVER:
73 : /*
74 : * We need to setup_logging *again* to ensure multi-file
75 : * logging is set up as specified in smb.conf.
76 : */
77 53 : if (!cmdline_daemon_cfg->interactive) {
78 0 : setup_logging(getprogname(), DEBUG_FILE);
79 : }
80 53 : break;
81 3034 : default:
82 3034 : break;
83 : }
84 :
85 3087 : return true;
86 : }
87 :
88 3099 : bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
89 : enum samba_cmdline_config_type config_type,
90 : bool require_smbconf)
91 : {
92 3099 : struct loadparm_context *lp_ctx = NULL;
93 3099 : struct cli_credentials *creds = NULL;
94 : bool ok;
95 :
96 3099 : ok = samba_cmdline_init_common(mem_ctx);
97 3099 : if (!ok) {
98 0 : return false;
99 : }
100 :
101 3099 : lp_ctx = loadparm_init_global(false);
102 3099 : if (lp_ctx == NULL) {
103 0 : return false;
104 : }
105 :
106 3099 : ok = samba_cmdline_set_lp_ctx(lp_ctx);
107 3099 : if (!ok) {
108 0 : return false;
109 : }
110 3099 : _require_smbconf = require_smbconf;
111 3099 : _config_type = config_type;
112 :
113 3099 : creds = cli_credentials_init(mem_ctx);
114 3099 : if (creds == NULL) {
115 0 : return false;
116 : }
117 3099 : ok = samba_cmdline_set_creds(creds);
118 3099 : if (!ok) {
119 0 : return false;
120 : }
121 :
122 3099 : samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s4);
123 :
124 3099 : return true;
125 : }
|