summaryrefslogtreecommitdiff
path: root/lib/cmdline/cmdline.h
blob: 5cd58c3ddbb3c334489be327d2079ec854637f89 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * 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/>.
 */

#ifndef _CMDLINE_H
#define _CMDLINE_H

#include "auth/credentials/credentials.h"
#include <popt.h>

#ifndef POPT_TABLEEND
#define POPT_TABLEEND { \
	.longName   = NULL, \
	.shortName  = 0, \
	.argInfo    = 0, \
	.arg        = NULL, \
	.val        = 0, \
	.descrip    = NULL, \
	.argDescrip = NULL }
#endif

enum samba_cmdline_config_type {
	SAMBA_CMDLINE_CONFIG_NONE = 0,
	SAMBA_CMDLINE_CONFIG_CLIENT,
	SAMBA_CMDLINE_CONFIG_SERVER,
};

enum smb_cmdline_popt_options {
	SAMBA_CMDLINE_POPT_OPT_DEBUG_ONLY = 1,
	SAMBA_CMDLINE_POPT_OPT_OPTION_ONLY,
	SAMBA_CMDLINE_POPT_OPT_CONFIG_ONLY,
	SAMBA_CMDLINE_POPT_OPT_SAMBA,
	SAMBA_CMDLINE_POPT_OPT_CONNECTION,
	SAMBA_CMDLINE_POPT_OPT_CREDENTIALS,
	SAMBA_CMDLINE_POPT_OPT_VERSION,
	SAMBA_CMDLINE_POPT_OPT_DAEMON,
	SAMBA_CMDLINE_POPT_OPT_SAMBA_LDB,
	SAMBA_CMDLINE_POPT_OPT_LEGACY_S3,
	SAMBA_CMDLINE_POPT_OPT_LEGACY_S4,
};

struct samba_cmdline_daemon_cfg {
	bool daemon;
	bool interactive;
	bool fork;
	bool no_process_group;
};

/**
 * @brief Initialize the commandline interface for parsing options.
 *
 * This initializes the interface for parsing options given on the command
 * line. It sets up the loadparm and client credentials contexts.
 * The function will also setup fault handler, set logging to STDERR by
 * default, setup talloc logging and the panic handler.
 *
 * The function also setups a callback for loading the smb.conf file, the
 * config file will be parsed after the commandline options have been parsed
 * by popt. This is done by one of the following options parser:
 *
 *     POPT_COMMON_DEBUG_ONLY
 *     POPT_COMMON_OPTION_ONLY
 *     POPT_COMMON_CONFIG_ONLY
 *     POPT_COMMON_SAMBA
 *
 * @param[in]  mem_ctx  The talloc memory context to use for allocating memory.
 *                      This should be a long living context till the client
 *                      exits.
 *
 * @param[in]  require_smbconf  Wether the smb.conf file is required to be
 *                              present or not?
 *
 * @return true on success, false if an error occured.
 */
bool samba_cmdline_init(TALLOC_CTX *mem_ctx,
			enum samba_cmdline_config_type config_type,
			bool require_smbconf);

/**
 * @brief Get a pointer of loadparm context used for the command line interface.
 *
 * @return The loadparm context.
 */
struct loadparm_context *samba_cmdline_get_lp_ctx(void);

/**
 * @brief Get the client credentials of the command line interface.
 *
 * @return A pointer to the client credentials.
 */
struct cli_credentials *samba_cmdline_get_creds(void);

/**
 * @brief Get a pointer to the poptOption for the given option section.
 *
 * You should not directly use this function, but the macros.
 *
 * @param[in]  opt  The options to retrieve.
 *
 * @return A pointer to the poptOption array.
 *
 * @see POPT_COMMON_DEBUG_ONLY
 * @see POPT_COMMON_OPTION_ONLY
 * @see POPT_COMMON_CONFIG_ONLY
 * @see POPT_COMMON_SAMBA
 * @see POPT_COMMON_CONNECTION
 * @see POPT_COMMON_CREDENTIALS
 * @see POPT_COMMON_VERSION
 */
struct poptOption *samba_cmdline_get_popt(enum smb_cmdline_popt_options opt);

/**
 * @brief Get a pointer to the poptOptions for daemons
 *
 * @return A pointer to the daemon options
 *
 * @see POPT_COMMON_DAEMON
 */
struct samba_cmdline_daemon_cfg *samba_cmdline_get_daemon_cfg(void);

void samba_cmdline_set_machine_account_fn(
	NTSTATUS (*fn) (struct cli_credentials *cred,
			struct loadparm_context *lp_ctx));

/**
 * @brief Burn secrets on the command line.
 *
 * This function removes secrets from the command line so we don't leak e.g.
 * passwords on 'ps aux' output.
 *
 * It should be called after processing the options and you should pass down
 * argv from main().
 *
 * @param[in]  argc     The number of arguments.
 *
 * @param[in]  argv[]   The argument array we should remove secrets from.
 */
void samba_cmdline_burn(int argc, char *argv[]);

/**
 * @brief Sanity check the command line options.
 *
 * This checks for duplicates in short and long options.
 *
 * @param[in]  opts    The options array to check.
 *
 * @return true if valid, false otherwise.
 */
bool samba_cmdline_sanity_check(const struct poptOption *opts);

/**
 * @brief This is a wrapper for the poptGetContext() which initializes the popt
 *        context.
 *
 * If Samba is build in developer mode, this will call
 * samba_cmdline_sanity_check() before poptGetContext().
 *
 * @param[in] name     The context name (usually argv[0] program name or
 *                     getprogname())
 *
 * @param[in] argc     Number of arguments
 *
 * @param[in] argv     The argument array
 *
 * @param[in] options  The address of popt option table
 *
 * @param[in] flags    The OR'd POPT_CONTEXT_* bits
 *
 * @return The initialized popt context or NULL on error.
 */
poptContext samba_popt_get_context(const char * name,
				   int argc, const char ** argv,
				   const struct poptOption * options,
				   unsigned int flags);

/**
 * @brief A popt structure for common debug options only.
 */
#define POPT_COMMON_DEBUG_ONLY { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_DEBUG_ONLY), \
	.val        = 0, \
	.descrip    = "Common debug options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for --option only.
 */
#define POPT_COMMON_OPTION_ONLY { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_OPTION_ONLY), \
	.val        = 0, \
	.descrip    = "Options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for --configfile only.
 */
#define POPT_COMMON_CONFIG_ONLY { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_CONFIG_ONLY), \
	.val        = 0, \
	.descrip    = "Config file:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for common samba options.
 */
#define POPT_COMMON_SAMBA { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_SAMBA), \
	.val        = 0, \
	.descrip    = "Common Samba options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for connection options.
 */
#define POPT_COMMON_CONNECTION { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_CONNECTION), \
	.val        = 0, \
	.descrip    = "Connection options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for credential options.
 */
#define POPT_COMMON_CREDENTIALS { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_CREDENTIALS), \
	.val        = 0, \
	.descrip    = "Credential options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for version options.
 */
#define POPT_COMMON_VERSION { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_VERSION), \
	.val        = 0, \
	.descrip    = "Version options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for daemon options.
 */
#define POPT_COMMON_DAEMON { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_DAEMON), \
	.val        = 0, \
	.descrip    = "Daemon options:", \
	.argDescrip = NULL },

/**
 * @brief A popt structure for common samba options.
 */
#define POPT_COMMON_SAMBA_LDB { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_SAMBA_LDB), \
	.val        = 0, \
	.descrip    = "Common Samba options:", \
	.argDescrip = NULL },

/* TODO Get rid of me! */
#define POPT_LEGACY_S3 { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_LEGACY_S3), \
	.val        = 0, \
	.descrip    = "Deprecated legcacy options:", \
	.argDescrip = NULL },

/* TODO Get rid of me! */
#define POPT_LEGACY_S4 { \
	.longName   = NULL, \
	.shortName  = '\0', \
	.argInfo    = POPT_ARG_INCLUDE_TABLE, \
	.arg        = samba_cmdline_get_popt(SAMBA_CMDLINE_POPT_OPT_LEGACY_S4), \
	.val        = 0, \
	.descrip    = "Deprecated legcacy options:", \
	.argDescrip = NULL },

#endif /* _CMDLINE_H */