s3: fix a few "shadows a global declaration" warnings
[Samba.git] / source / lib / popt_common.c
blob2e6d3b3cb10538809021bf8957ed52eaa83a4914
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 *lfile = 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(&lfile, "%s/log.%s", arg, pname) < 0) {
54 return;
56 lp_set_logfile(lfile);
57 SAFE_FREE(lfile);
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
174 struct poptOption popt_common_debuglevel[] = {
175 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
176 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
177 POPT_TABLEEND
181 /* Handle command line options:
182 * --sbindir
183 * --bindir
184 * --swatdir
185 * --lmhostsfile
186 * --libdir
187 * --modulesdir
188 * --shlibext
189 * --lockdir
190 * --piddir
191 * --smb-passwd-file
192 * --private-dir
195 enum dyn_item{
196 DYN_SBINDIR = 1,
197 DYN_BINDIR,
198 DYN_SWATDIR,
199 DYN_LMHOSTSFILE,
200 DYN_LIBDIR,
201 DYN_MODULESDIR,
202 DYN_SHLIBEXT,
203 DYN_LOCKDIR,
204 DYN_PIDDIR,
205 DYN_SMB_PASSWD_FILE,
206 DYN_PRIVATE_DIR,
210 static void popt_dynconfig_callback(poptContext con,
211 enum poptCallbackReason reason,
212 const struct poptOption *opt,
213 const char *arg, const void *data)
216 switch (opt->val) {
217 case DYN_SBINDIR:
218 if (arg) {
219 set_dyn_SBINDIR(arg);
221 break;
223 case DYN_BINDIR:
224 if (arg) {
225 set_dyn_BINDIR(arg);
227 break;
229 case DYN_SWATDIR:
230 if (arg) {
231 set_dyn_SWATDIR(arg);
233 break;
235 case DYN_LMHOSTSFILE:
236 if (arg) {
237 set_dyn_LMHOSTSFILE(arg);
239 break;
241 case DYN_LIBDIR:
242 if (arg) {
243 set_dyn_LIBDIR(arg);
245 break;
247 case DYN_MODULESDIR:
248 if (arg) {
249 set_dyn_MODULESDIR(arg);
251 break;
253 case DYN_SHLIBEXT:
254 if (arg) {
255 set_dyn_SHLIBEXT(arg);
257 break;
259 case DYN_LOCKDIR:
260 if (arg) {
261 set_dyn_LOCKDIR(arg);
263 break;
265 case DYN_PIDDIR:
266 if (arg) {
267 set_dyn_PIDDIR(arg);
269 break;
271 case DYN_SMB_PASSWD_FILE:
272 if (arg) {
273 set_dyn_SMB_PASSWD_FILE(arg);
275 break;
277 case DYN_PRIVATE_DIR:
278 if (arg) {
279 set_dyn_PRIVATE_DIR(arg);
281 break;
286 const struct poptOption popt_common_dynconfig[] = {
288 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
290 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
291 "Path to sbin directory", "SBINDIR" },
292 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
293 "Path to bin directory", "BINDIR" },
294 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
295 "Path to SWAT installation directory", "SWATDIR" },
296 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
297 "Path to lmhosts file", "LMHOSTSFILE" },
298 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
299 "Path to shared library directory", "LIBDIR" },
300 { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
301 "Path to shared modules directory", "MODULESDIR" },
302 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
303 "Shared library extension", "SHLIBEXT" },
304 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
305 "Path to lock file directory", "LOCKDIR" },
306 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
307 "Path to PID file directory", "PIDDIR" },
308 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
309 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
310 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
311 "Path to private data directory", "PRIVATE_DIR" },
313 POPT_TABLEEND
316 /****************************************************************************
317 * get a password from a a file or file descriptor
318 * exit on failure
319 * ****************************************************************************/
321 static void get_password_file(void)
323 int fd = -1;
324 char *p;
325 bool close_it = False;
326 char *spec = NULL;
327 char pass[128];
329 if ((p = getenv("PASSWD_FD")) != NULL) {
330 if (asprintf(&spec, "descriptor %s", p) < 0) {
331 return;
333 sscanf(p, "%d", &fd);
334 close_it = false;
335 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
336 fd = sys_open(p, O_RDONLY, 0);
337 spec = SMB_STRDUP(p);
338 if (fd < 0) {
339 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
340 spec, strerror(errno));
341 exit(1);
343 close_it = True;
346 if (fd < 0) {
347 fprintf(stderr, "fd = %d, < 0\n", fd);
348 exit(1);
351 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
352 p && p - pass < sizeof(pass);) {
353 switch (read(fd, p, 1)) {
354 case 1:
355 if (*p != '\n' && *p != '\0') {
356 *++p = '\0'; /* advance p, and null-terminate pass */
357 break;
359 case 0:
360 if (p - pass) {
361 *p = '\0'; /* null-terminate it, just in case... */
362 p = NULL; /* then force the loop condition to become false */
363 break;
364 } else {
365 fprintf(stderr, "Error reading password from file %s: %s\n",
366 spec, "empty password\n");
367 SAFE_FREE(spec);
368 exit(1);
371 default:
372 fprintf(stderr, "Error reading password from file %s: %s\n",
373 spec, strerror(errno));
374 SAFE_FREE(spec);
375 exit(1);
378 SAFE_FREE(spec);
380 set_cmdline_auth_info_password(pass);
381 if (close_it) {
382 close(fd);
386 static void get_credentials_file(const char *file)
388 XFILE *auth;
389 fstring buf;
390 uint16 len = 0;
391 char *ptr, *val, *param;
393 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
395 /* fail if we can't open the credentials file */
396 d_printf("ERROR: Unable to open credentials file!\n");
397 exit(-1);
400 while (!x_feof(auth))
402 /* get a line from the file */
403 if (!x_fgets(buf, sizeof(buf), auth))
404 continue;
405 len = strlen(buf);
407 if ((len) && (buf[len-1]=='\n'))
409 buf[len-1] = '\0';
410 len--;
412 if (len == 0)
413 continue;
415 /* break up the line into parameter & value.
416 * will need to eat a little whitespace possibly */
417 param = buf;
418 if (!(ptr = strchr_m (buf, '=')))
419 continue;
421 val = ptr+1;
422 *ptr = '\0';
424 /* eat leading white space */
425 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
426 val++;
428 if (strwicmp("password", param) == 0) {
429 set_cmdline_auth_info_password(val);
430 } else if (strwicmp("username", param) == 0) {
431 set_cmdline_auth_info_username(val);
432 } else if (strwicmp("domain", param) == 0) {
433 set_global_myworkgroup(val);
435 memset(buf, 0, sizeof(buf));
437 x_fclose(auth);
440 /* Handle command line options:
441 * -U,--user
442 * -A,--authentication-file
443 * -k,--use-kerberos
444 * -N,--no-pass
445 * -S,--signing
446 * -P --machine-pass
447 * -e --encrypt
451 static void popt_common_credentials_callback(poptContext con,
452 enum poptCallbackReason reason,
453 const struct poptOption *opt,
454 const char *arg, const void *data)
456 char *p;
458 if (reason == POPT_CALLBACK_REASON_PRE) {
459 set_cmdline_auth_info_username("GUEST");
461 if (getenv("LOGNAME")) {
462 set_cmdline_auth_info_username(getenv("LOGNAME"));
465 if (getenv("USER")) {
466 char *puser = SMB_STRDUP(getenv("USER"));
467 if (!puser) {
468 exit(ENOMEM);
470 set_cmdline_auth_info_username(puser);
472 if ((p = strchr_m(puser,'%'))) {
473 size_t len;
474 *p = 0;
475 len = strlen(p+1);
476 set_cmdline_auth_info_password(p+1);
477 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
479 SAFE_FREE(puser);
482 if (getenv("PASSWD")) {
483 set_cmdline_auth_info_password(getenv("PASSWD"));
486 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
487 get_password_file();
490 return;
493 switch(opt->val) {
494 case 'U':
496 char *lp;
497 char *puser = SMB_STRDUP(arg);
499 if ((lp=strchr_m(puser,'%'))) {
500 size_t len;
501 *lp = 0;
502 set_cmdline_auth_info_username(puser);
503 set_cmdline_auth_info_password(lp+1);
504 len = strlen(lp+1);
505 memset(strchr_m(arg,'%')+1,'X',len);
506 } else {
507 set_cmdline_auth_info_username(puser);
509 SAFE_FREE(puser);
511 break;
513 case 'A':
514 get_credentials_file(arg);
515 break;
517 case 'k':
518 #ifndef HAVE_KRB5
519 d_printf("No kerberos support compiled in\n");
520 exit(1);
521 #else
522 set_cmdline_auth_info_use_krb5_ticket();
523 #endif
524 break;
526 case 'S':
527 if (!set_cmdline_auth_info_signing_state(arg)) {
528 fprintf(stderr, "Unknown signing option %s\n", arg );
529 exit(1);
531 break;
532 case 'P':
533 set_cmdline_auth_info_use_machine_account();
534 break;
535 case 'N':
536 set_cmdline_auth_info_password("");
537 break;
538 case 'e':
539 set_cmdline_auth_info_smb_encrypt();
540 break;
545 struct poptOption popt_common_credentials[] = {
546 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
547 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
548 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
549 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
550 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
551 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
552 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
553 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
554 POPT_TABLEEND