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 :
30 : static bool _require_smbconf;
31 : static enum samba_cmdline_config_type _config_type;
32 :
33 20443 : static bool _samba_cmdline_load_config_s3(void)
34 : {
35 20443 : struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
36 20443 : const char *config_file = NULL;
37 20443 : bool ok = false;
38 :
39 : /* Load smb conf */
40 20443 : config_file = lpcfg_configfile(lp_ctx);
41 20443 : if (config_file == NULL) {
42 20443 : if (is_default_dyn_CONFIGFILE()) {
43 10736 : const char *env = getenv("SMB_CONF_PATH");
44 10736 : if (env != NULL && strlen(env) > 0) {
45 10736 : set_dyn_CONFIGFILE(env);
46 : }
47 : }
48 : }
49 :
50 20443 : config_file = get_dyn_CONFIGFILE();
51 :
52 20443 : switch (_config_type) {
53 3846 : case SAMBA_CMDLINE_CONFIG_NONE:
54 3846 : return true;
55 16289 : case SAMBA_CMDLINE_CONFIG_CLIENT:
56 16289 : ok = lp_load_client(config_file);
57 16289 : break;
58 300 : case SAMBA_CMDLINE_CONFIG_SERVER:
59 : {
60 254 : const struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg =
61 46 : samba_cmdline_get_daemon_cfg();
62 :
63 300 : if (!cmdline_daemon_cfg->interactive) {
64 300 : setup_logging(getprogname(), DEBUG_FILE);
65 : }
66 :
67 300 : ok = lp_load_global(config_file);
68 300 : break;
69 : }
70 : }
71 :
72 16589 : if (!ok) {
73 0 : fprintf(stderr,
74 : "Can't load %s - run testparm to debug it\n",
75 : config_file);
76 :
77 0 : if (_require_smbconf) {
78 0 : return false;
79 : }
80 : }
81 :
82 16589 : load_interfaces();
83 :
84 16589 : return true;
85 : }
86 :
87 17798 : bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
88 : enum samba_cmdline_config_type config_type,
89 : bool require_smbconf)
90 : {
91 17798 : struct loadparm_context *lp_ctx = NULL;
92 17798 : struct cli_credentials *creds = NULL;
93 : bool ok;
94 :
95 17798 : ok = samba_cmdline_init_common(mem_ctx);
96 17798 : if (!ok) {
97 0 : return false;
98 : }
99 :
100 17798 : lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
101 17798 : if (lp_ctx == NULL) {
102 0 : return false;
103 : }
104 17798 : ok = samba_cmdline_set_lp_ctx(lp_ctx);
105 17798 : if (!ok) {
106 0 : return false;
107 : }
108 :
109 17798 : _require_smbconf = require_smbconf;
110 17798 : _config_type = config_type;
111 :
112 17798 : creds = cli_credentials_init(mem_ctx);
113 17798 : if (creds == NULL) {
114 0 : return false;
115 : }
116 17798 : ok = samba_cmdline_set_creds(creds);
117 17798 : if (!ok) {
118 0 : return false;
119 : }
120 :
121 17798 : samba_cmdline_set_load_config_fn(_samba_cmdline_load_config_s3);
122 :
123 17798 : return true;
124 : }
|