s3: popt: Change to NULL from talloc_autofree_context() now we correctly free on...
[Samba.git] / source3 / lib / popt_common.c
blob1c1e3d7c9d43b2050c21cb2702cba7b911190859
1 /*
2 Unix SMB/CIFS implementation.
3 Common popt routines
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
7 Copyright (C) James Peach 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "lib/param/param.h"
27 /* Handle command line options:
28 * -d,--debuglevel
29 * -s,--configfile
30 * -O,--socket-options
31 * -V,--version
32 * -l,--log-base
33 * -n,--netbios-name
34 * -W,--workgroup
35 * -i,--scope
38 enum {OPT_OPTION=1};
40 extern bool override_logfile;
42 static void set_logfile(poptContext con, const char * arg)
45 char *lfile = NULL;
46 const char *pname;
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
51 if (!pname)
52 pname = poptGetInvocationName(con);
53 else
54 pname++;
56 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
57 return;
59 lp_set_logfile(lfile);
60 SAFE_FREE(lfile);
63 static bool PrintSambaVersionString;
65 static void popt_s3_talloc_log_fn(const char *message)
67 DEBUG(0,("%s", message));
70 static void popt_common_callback(poptContext con,
71 enum poptCallbackReason reason,
72 const struct poptOption *opt,
73 const char *arg, const void *data)
76 if (reason == POPT_CALLBACK_REASON_PRE) {
77 set_logfile(con, get_dyn_LOGFILEBASE());
78 talloc_set_log_fn(popt_s3_talloc_log_fn);
79 talloc_set_abort_fn(smb_panic);
80 return;
83 if (reason == POPT_CALLBACK_REASON_POST) {
85 if (PrintSambaVersionString) {
86 printf( "Version %s\n", samba_version_string());
87 exit(0);
90 if (is_default_dyn_CONFIGFILE()) {
91 if(getenv("SMB_CONF_PATH")) {
92 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
96 /* Further 'every Samba program must do this' hooks here. */
97 return;
100 switch(opt->val) {
101 case OPT_OPTION:
103 struct loadparm_context *lp_ctx;
105 lp_ctx = loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
106 if (lp_ctx == NULL) {
107 fprintf(stderr, "loadparm_init_s3() failed!\n");
108 exit(1);
111 if (!lpcfg_set_option(lp_ctx, arg)) {
112 fprintf(stderr, "Error setting option '%s'\n", arg);
113 exit(1);
115 TALLOC_FREE(lp_ctx);
116 break;
118 case 'd':
119 if (arg) {
120 lp_set_cmdline("log level", arg);
122 break;
124 case 'V':
125 PrintSambaVersionString = True;
126 break;
128 case 'O':
129 if (arg) {
130 lp_set_cmdline("socket options", arg);
132 break;
134 case 's':
135 if (arg) {
136 set_dyn_CONFIGFILE(arg);
138 break;
140 case 'n':
141 if (arg) {
142 lp_set_cmdline("netbios name", arg);
144 break;
146 case 'l':
147 if (arg) {
148 set_logfile(con, arg);
149 override_logfile = True;
150 set_dyn_LOGFILEBASE(arg);
152 break;
154 case 'i':
155 if (arg) {
156 lp_set_cmdline("netbios scope", arg);
158 break;
160 case 'W':
161 if (arg) {
162 lp_set_cmdline("workgroup", arg);
164 break;
168 struct poptOption popt_common_connection[] = {
169 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
170 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
171 "SOCKETOPTIONS" },
172 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
173 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
174 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
176 POPT_TABLEEND
179 struct poptOption popt_common_samba[] = {
180 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
181 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
182 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
183 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
184 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
185 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
186 POPT_TABLEEND
189 struct poptOption popt_common_configfile[] = {
190 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
191 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
192 POPT_TABLEEND
195 struct poptOption popt_common_version[] = {
196 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
197 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
198 POPT_TABLEEND
201 struct poptOption popt_common_debuglevel[] = {
202 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
203 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
204 POPT_TABLEEND
207 struct poptOption popt_common_option[] = {
208 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
209 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
210 POPT_TABLEEND
213 /* Handle command line options:
214 * -U,--user
215 * -A,--authentication-file
216 * -k,--use-kerberos
217 * -N,--no-pass
218 * -S,--signing
219 * -P --machine-pass
220 * -e --encrypt
221 * -C --use-ccache
224 static struct user_auth_info *cmdline_auth_info;
226 struct user_auth_info *popt_get_cmdline_auth_info(void)
228 return cmdline_auth_info;
230 void popt_free_cmdline_auth_info(void)
232 TALLOC_FREE(cmdline_auth_info);
235 static bool popt_common_credentials_ignore_missing_conf;
236 static bool popt_common_credentials_delay_post;
238 void popt_common_credentials_set_ignore_missing_conf(void)
240 popt_common_credentials_delay_post = true;
243 void popt_common_credentials_set_delay_post(void)
245 popt_common_credentials_delay_post = true;
248 void popt_common_credentials_post(void)
250 if (get_cmdline_auth_info_use_machine_account(cmdline_auth_info) &&
251 !set_cmdline_auth_info_machine_account_creds(cmdline_auth_info))
253 fprintf(stderr,
254 "Failed to use machine account credentials\n");
255 exit(1);
258 set_cmdline_auth_info_getpass(cmdline_auth_info);
261 static void popt_common_credentials_callback(poptContext con,
262 enum poptCallbackReason reason,
263 const struct poptOption *opt,
264 const char *arg, const void *data)
266 if (reason == POPT_CALLBACK_REASON_PRE) {
267 struct user_auth_info *auth_info =
268 user_auth_info_init(NULL);
269 if (auth_info == NULL) {
270 fprintf(stderr, "user_auth_info_init() failed\n");
271 exit(1);
273 cmdline_auth_info = auth_info;
274 return;
277 if (reason == POPT_CALLBACK_REASON_POST) {
278 bool ok;
280 if (override_logfile) {
281 setup_logging(lp_logfile(talloc_tos()), DEBUG_FILE );
284 ok = lp_load_client(get_dyn_CONFIGFILE());
285 if (!ok) {
286 const char *pname = poptGetInvocationName(con);
288 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
289 pname, get_dyn_CONFIGFILE());
290 if (!popt_common_credentials_ignore_missing_conf) {
291 exit(1);
295 load_interfaces();
297 set_cmdline_auth_info_guess(cmdline_auth_info);
299 if (popt_common_credentials_delay_post) {
300 return;
303 popt_common_credentials_post();
304 return;
307 switch(opt->val) {
308 case 'U':
309 set_cmdline_auth_info_username(cmdline_auth_info, arg);
310 break;
312 case 'A':
313 set_cmdline_auth_info_from_file(cmdline_auth_info, arg);
314 break;
316 case 'k':
317 #ifndef HAVE_KRB5
318 d_printf("No kerberos support compiled in\n");
319 exit(1);
320 #else
321 set_cmdline_auth_info_use_krb5_ticket(cmdline_auth_info);
322 #endif
323 break;
325 case 'S':
326 if (!set_cmdline_auth_info_signing_state(cmdline_auth_info,
327 arg)) {
328 fprintf(stderr, "Unknown signing option %s\n", arg );
329 exit(1);
331 break;
332 case 'P':
333 set_cmdline_auth_info_use_machine_account(cmdline_auth_info);
334 break;
335 case 'N':
336 set_cmdline_auth_info_password(cmdline_auth_info, "");
337 break;
338 case 'e':
339 set_cmdline_auth_info_smb_encrypt(cmdline_auth_info);
340 break;
341 case 'C':
342 set_cmdline_auth_info_use_ccache(cmdline_auth_info, true);
343 break;
344 case 'H':
345 set_cmdline_auth_info_use_pw_nt_hash(cmdline_auth_info, true);
346 break;
351 * @brief Burn the commandline password.
353 * This function removes the password from the command line so we
354 * don't leak the password e.g. in 'ps aux'.
356 * It should be called after processing the options and you should pass down
357 * argv from main().
359 * @param[in] argc The number of arguments.
361 * @param[in] argv[] The argument array we will find the array.
363 void popt_burn_cmdline_password(int argc, char *argv[])
365 bool found = false;
366 char *p = NULL;
367 int i, ulen = 0;
369 for (i = 0; i < argc; i++) {
370 p = argv[i];
371 if (strncmp(p, "-U", 2) == 0) {
372 ulen = 2;
373 found = true;
374 } else if (strncmp(p, "--user", 6) == 0) {
375 ulen = 6;
376 found = true;
379 if (found) {
380 if (p == NULL) {
381 return;
384 if (strlen(p) == ulen) {
385 continue;
388 p = strchr_m(p, '%');
389 if (p != NULL) {
390 memset(p, '\0', strlen(p));
392 found = false;
397 struct poptOption popt_common_credentials[] = {
398 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
399 (void *)popt_common_credentials_callback, 0, NULL },
400 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
401 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
402 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
403 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
404 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
405 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
406 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
407 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
408 "Use the winbind ccache for authentication" },
409 {"pw-nt-hash", '\0', POPT_ARG_NONE, NULL, 'H',
410 "The supplied password is the NT hash" },
411 POPT_TABLEEND