fix the broken generation of the smb.conf.5.html page
[Samba/id10ts.git] / source3 / lib / popt_common.c
blob8f0f7c62bb9e6ace3982774dfd07533ea99ae663
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"
25 /* Handle command line options:
26 * -d,--debuglevel
27 * -s,--configfile
28 * -O,--socket-options
29 * -V,--version
30 * -l,--log-base
31 * -n,--netbios-name
32 * -W,--workgroup
33 * -i,--scope
36 extern bool AllowDebugChange;
37 extern bool override_logfile;
39 static void set_logfile(poptContext con, const char * arg)
42 char *logfile = NULL;
43 const char *pname;
45 /* Find out basename of current program */
46 pname = strrchr_m(poptGetInvocationName(con),'/');
48 if (!pname)
49 pname = poptGetInvocationName(con);
50 else
51 pname++;
53 if (asprintf(&logfile, "%s/log.%s", arg, pname) < 0) {
54 return;
56 lp_set_logfile(logfile);
57 SAFE_FREE(logfile);
60 static bool PrintSambaVersionString;
62 static void popt_common_callback(poptContext con,
63 enum poptCallbackReason reason,
64 const struct poptOption *opt,
65 const char *arg, const void *data)
68 if (reason == POPT_CALLBACK_REASON_PRE) {
69 set_logfile(con, get_dyn_LOGFILEBASE());
70 return;
73 if (reason == POPT_CALLBACK_REASON_POST) {
75 if (PrintSambaVersionString) {
76 printf( "Version %s\n", SAMBA_VERSION_STRING);
77 exit(0);
80 if (is_default_dyn_CONFIGFILE()) {
81 if(getenv("SMB_CONF_PATH")) {
82 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
86 /* Further 'every Samba program must do this' hooks here. */
87 return;
90 switch(opt->val) {
91 case 'd':
92 if (arg) {
93 debug_parse_levels(arg);
94 AllowDebugChange = False;
96 break;
98 case 'V':
99 PrintSambaVersionString = True;
100 break;
102 case 'O':
103 if (arg) {
104 lp_do_parameter(-1, "socket options", arg);
106 break;
108 case 's':
109 if (arg) {
110 set_dyn_CONFIGFILE(arg);
112 break;
114 case 'n':
115 if (arg) {
116 set_global_myname(arg);
118 break;
120 case 'l':
121 if (arg) {
122 set_logfile(con, arg);
123 override_logfile = True;
124 set_dyn_LOGFILEBASE(arg);
126 break;
128 case 'i':
129 if (arg) {
130 set_global_scope(arg);
132 break;
134 case 'W':
135 if (arg) {
136 set_global_myworkgroup(arg);
138 break;
142 struct poptOption popt_common_connection[] = {
143 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
144 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
145 "SOCKETOPTIONS" },
146 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
147 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
148 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
150 POPT_TABLEEND
153 struct poptOption popt_common_samba[] = {
154 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
155 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
156 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
157 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
158 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
159 POPT_TABLEEND
162 struct poptOption popt_common_configfile[] = {
163 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
164 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
165 POPT_TABLEEND
168 struct poptOption popt_common_version[] = {
169 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
170 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
171 POPT_TABLEEND
175 /* Handle command line options:
176 * --sbindir
177 * --bindir
178 * --swatdir
179 * --lmhostsfile
180 * --libdir
181 * --shlibext
182 * --lockdir
183 * --piddir
184 * --smb-passwd-file
185 * --private-dir
188 enum dyn_item{
189 DYN_SBINDIR = 1,
190 DYN_BINDIR,
191 DYN_SWATDIR,
192 DYN_LMHOSTSFILE,
193 DYN_LIBDIR,
194 DYN_SHLIBEXT,
195 DYN_LOCKDIR,
196 DYN_PIDDIR,
197 DYN_SMB_PASSWD_FILE,
198 DYN_PRIVATE_DIR,
202 static void popt_dynconfig_callback(poptContext con,
203 enum poptCallbackReason reason,
204 const struct poptOption *opt,
205 const char *arg, const void *data)
208 switch (opt->val) {
209 case DYN_SBINDIR:
210 if (arg) {
211 set_dyn_SBINDIR(arg);
213 break;
215 case DYN_BINDIR:
216 if (arg) {
217 set_dyn_BINDIR(arg);
219 break;
221 case DYN_SWATDIR:
222 if (arg) {
223 set_dyn_SWATDIR(arg);
225 break;
227 case DYN_LMHOSTSFILE:
228 if (arg) {
229 set_dyn_LMHOSTSFILE(arg);
231 break;
233 case DYN_LIBDIR:
234 if (arg) {
235 set_dyn_LIBDIR(arg);
237 break;
239 case DYN_SHLIBEXT:
240 if (arg) {
241 set_dyn_SHLIBEXT(arg);
243 break;
245 case DYN_LOCKDIR:
246 if (arg) {
247 set_dyn_LOCKDIR(arg);
249 break;
251 case DYN_PIDDIR:
252 if (arg) {
253 set_dyn_PIDDIR(arg);
255 break;
257 case DYN_SMB_PASSWD_FILE:
258 if (arg) {
259 set_dyn_SMB_PASSWD_FILE(arg);
261 break;
263 case DYN_PRIVATE_DIR:
264 if (arg) {
265 set_dyn_PRIVATE_DIR(arg);
267 break;
272 const struct poptOption popt_common_dynconfig[] = {
274 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
276 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
277 "Path to sbin directory", "SBINDIR" },
278 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
279 "Path to bin directory", "BINDIR" },
280 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
281 "Path to SWAT installation directory", "SWATDIR" },
282 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
283 "Path to lmhosts file", "LMHOSTSFILE" },
284 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
285 "Path to shared library directory", "LIBDIR" },
286 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
287 "Shared library extension", "SHLIBEXT" },
288 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
289 "Path to lock file directory", "LOCKDIR" },
290 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
291 "Path to PID file directory", "PIDDIR" },
292 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
293 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
294 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
295 "Path to private data directory", "PRIVATE_DIR" },
297 POPT_TABLEEND
300 /****************************************************************************
301 * get a password from a a file or file descriptor
302 * exit on failure
303 * ****************************************************************************/
305 static void get_password_file(void)
307 int fd = -1;
308 char *p;
309 bool close_it = False;
310 char *spec = NULL;
311 char pass[128];
313 if ((p = getenv("PASSWD_FD")) != NULL) {
314 if (asprintf(&spec, "descriptor %s", p) < 0) {
315 return;
317 sscanf(p, "%d", &fd);
318 close_it = false;
319 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
320 fd = sys_open(p, O_RDONLY, 0);
321 spec = SMB_STRDUP(p);
322 if (fd < 0) {
323 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
324 spec, strerror(errno));
325 exit(1);
327 close_it = True;
330 if (fd < 0) {
331 fprintf(stderr, "fd = %d, < 0\n", fd);
332 exit(1);
335 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
336 p && p - pass < sizeof(pass);) {
337 switch (read(fd, p, 1)) {
338 case 1:
339 if (*p != '\n' && *p != '\0') {
340 *++p = '\0'; /* advance p, and null-terminate pass */
341 break;
343 case 0:
344 if (p - pass) {
345 *p = '\0'; /* null-terminate it, just in case... */
346 p = NULL; /* then force the loop condition to become false */
347 break;
348 } else {
349 fprintf(stderr, "Error reading password from file %s: %s\n",
350 spec, "empty password\n");
351 SAFE_FREE(spec);
352 exit(1);
355 default:
356 fprintf(stderr, "Error reading password from file %s: %s\n",
357 spec, strerror(errno));
358 SAFE_FREE(spec);
359 exit(1);
362 SAFE_FREE(spec);
364 set_cmdline_auth_info_password(pass);
365 if (close_it) {
366 close(fd);
370 static void get_credentials_file(const char *file)
372 XFILE *auth;
373 fstring buf;
374 uint16 len = 0;
375 char *ptr, *val, *param;
377 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
379 /* fail if we can't open the credentials file */
380 d_printf("ERROR: Unable to open credentials file!\n");
381 exit(-1);
384 while (!x_feof(auth))
386 /* get a line from the file */
387 if (!x_fgets(buf, sizeof(buf), auth))
388 continue;
389 len = strlen(buf);
391 if ((len) && (buf[len-1]=='\n'))
393 buf[len-1] = '\0';
394 len--;
396 if (len == 0)
397 continue;
399 /* break up the line into parameter & value.
400 * will need to eat a little whitespace possibly */
401 param = buf;
402 if (!(ptr = strchr_m (buf, '=')))
403 continue;
405 val = ptr+1;
406 *ptr = '\0';
408 /* eat leading white space */
409 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
410 val++;
412 if (strwicmp("password", param) == 0) {
413 set_cmdline_auth_info_password(val);
414 } else if (strwicmp("username", param) == 0) {
415 set_cmdline_auth_info_username(val);
416 } else if (strwicmp("domain", param) == 0) {
417 set_global_myworkgroup(val);
419 memset(buf, 0, sizeof(buf));
421 x_fclose(auth);
424 /* Handle command line options:
425 * -U,--user
426 * -A,--authentication-file
427 * -k,--use-kerberos
428 * -N,--no-pass
429 * -S,--signing
430 * -P --machine-pass
431 * -e --encrypt
435 static void popt_common_credentials_callback(poptContext con,
436 enum poptCallbackReason reason,
437 const struct poptOption *opt,
438 const char *arg, const void *data)
440 char *p;
442 if (reason == POPT_CALLBACK_REASON_PRE) {
443 set_cmdline_auth_info_username("GUEST");
445 if (getenv("LOGNAME")) {
446 set_cmdline_auth_info_username(getenv("LOGNAME"));
449 if (getenv("USER")) {
450 char *puser = SMB_STRDUP(getenv("USER"));
451 if (!puser) {
452 exit(ENOMEM);
454 set_cmdline_auth_info_username(puser);
456 if ((p = strchr_m(puser,'%'))) {
457 size_t len;
458 *p = 0;
459 len = strlen(p+1);
460 set_cmdline_auth_info_password(p+1);
461 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
463 SAFE_FREE(puser);
466 if (getenv("PASSWD")) {
467 set_cmdline_auth_info_password(getenv("PASSWD"));
470 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
471 get_password_file();
474 return;
477 switch(opt->val) {
478 case 'U':
480 char *lp;
481 char *puser = SMB_STRDUP(arg);
483 if ((lp=strchr_m(puser,'%'))) {
484 size_t len;
485 *lp = 0;
486 set_cmdline_auth_info_username(puser);
487 set_cmdline_auth_info_password(lp+1);
488 len = strlen(lp+1);
489 memset(strchr_m(arg,'%')+1,'X',len);
490 } else {
491 set_cmdline_auth_info_username(puser);
493 SAFE_FREE(puser);
495 break;
497 case 'A':
498 get_credentials_file(arg);
499 break;
501 case 'k':
502 #ifndef HAVE_KRB5
503 d_printf("No kerberos support compiled in\n");
504 exit(1);
505 #else
506 set_cmdline_auth_info_use_krb5_ticket();
507 #endif
508 break;
510 case 'S':
511 if (!set_cmdline_auth_info_signing_state(arg)) {
512 fprintf(stderr, "Unknown signing option %s\n", arg );
513 exit(1);
515 break;
516 case 'P':
518 char *opt_password = NULL;
519 char *pwd = NULL;
521 /* it is very useful to be able to make ads queries as the
522 machine account for testing purposes and for domain leave */
524 if (!secrets_init()) {
525 d_printf("ERROR: Unable to open secrets database\n");
526 exit(1);
529 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
531 if (!opt_password) {
532 d_printf("ERROR: Unable to fetch machine password\n");
533 exit(1);
535 if (asprintf(&pwd, "%s$", global_myname()) < 0) {
536 exit(ENOMEM);
538 set_cmdline_auth_info_username(pwd);
539 set_cmdline_auth_info_password(opt_password);
540 SAFE_FREE(pwd);
541 SAFE_FREE(opt_password);
543 /* machine accounts only work with kerberos */
544 set_cmdline_auth_info_use_krb5_ticket();
546 break;
547 case 'N':
548 set_cmdline_auth_info_password("");
549 break;
550 case 'e':
551 set_cmdline_auth_info_smb_encrypt();
552 break;
557 struct poptOption popt_common_credentials[] = {
558 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
559 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
560 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
561 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
562 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
563 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
564 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
565 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
566 POPT_TABLEEND