s3-debug Remove 'AllowDebugChange' and use lp_set_cmdline() instead
[sfrench/samba-autobuild/.git] / source3 / lib / popt_common.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Common popt routines
4
5    Copyright (C) Tim Potter 2001,2002
6    Copyright (C) Jelmer Vernooij 2002,2003
7    Copyright (C) James Peach 2006
8
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "popt_common.h"
25
26 /* Handle command line options:
27  *              -d,--debuglevel 
28  *              -s,--configfile 
29  *              -O,--socket-options 
30  *              -V,--version
31  *              -l,--log-base
32  *              -n,--netbios-name
33  *              -W,--workgroup
34  *              -i,--scope
35  */
36
37 enum {OPT_OPTION=1};
38
39 extern bool override_logfile;
40
41 static void set_logfile(poptContext con, const char * arg)
42 {
43
44         char *lfile = NULL;
45         const char *pname;
46
47         /* Find out basename of current program */
48         pname = strrchr_m(poptGetInvocationName(con),'/');
49
50         if (!pname)
51                 pname = poptGetInvocationName(con);
52         else
53                 pname++;
54
55         if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
56                 return;
57         }
58         lp_set_logfile(lfile);
59         SAFE_FREE(lfile);
60 }
61
62 static bool PrintSambaVersionString;
63
64 static void popt_s3_talloc_log_fn(const char *message)
65 {
66         DEBUG(0,("%s", message));
67 }
68
69 static void popt_common_callback(poptContext con,
70                            enum poptCallbackReason reason,
71                            const struct poptOption *opt,
72                            const char *arg, const void *data)
73 {
74
75         if (reason == POPT_CALLBACK_REASON_PRE) {
76                 set_logfile(con, get_dyn_LOGFILEBASE());
77                 talloc_set_log_fn(popt_s3_talloc_log_fn);
78                 talloc_set_abort_fn(smb_panic);
79                 return;
80         }
81
82         if (reason == POPT_CALLBACK_REASON_POST) {
83
84                 if (PrintSambaVersionString) {
85                         printf( "Version %s\n", samba_version_string());
86                         exit(0);
87                 }
88
89                 if (is_default_dyn_CONFIGFILE()) {
90                         if(getenv("SMB_CONF_PATH")) {
91                                 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
92                         }
93                 }
94
95                 /* Further 'every Samba program must do this' hooks here. */
96                 return;
97         }
98
99         switch(opt->val) {
100         case OPT_OPTION:
101                 if (!lp_set_option(arg)) {
102                         fprintf(stderr, "Error setting option '%s'\n", arg);
103                         exit(1);
104                 }
105                 break;
106
107         case 'd':
108                 if (arg) {
109                         lp_set_cmdline("log level", arg);
110                 }
111                 break;
112
113         case 'V':
114                 PrintSambaVersionString = True;
115                 break;
116
117         case 'O':
118                 if (arg) {
119                         lp_do_parameter(-1, "socket options", arg);
120                 }
121                 break;
122
123         case 's':
124                 if (arg) {
125                         set_dyn_CONFIGFILE(arg);
126                 }
127                 break;
128
129         case 'n':
130                 if (arg) {
131                         set_global_myname(arg);
132                 }
133                 break;
134
135         case 'l':
136                 if (arg) {
137                         set_logfile(con, arg);
138                         override_logfile = True;
139                         set_dyn_LOGFILEBASE(arg);
140                 }
141                 break;
142
143         case 'i':
144                 if (arg) {
145                           set_global_scope(arg);
146                 }
147                 break;
148
149         case 'W':
150                 if (arg) {
151                         set_global_myworkgroup(arg);
152                 }
153                 break;
154         }
155 }
156
157 struct poptOption popt_common_connection[] = {
158         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
159         { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
160           "SOCKETOPTIONS" },
161         { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
162         { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
163         { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
164
165         POPT_TABLEEND
166 };
167
168 struct poptOption popt_common_samba[] = {
169         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
170         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
171         { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
172         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
173         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
174         { "option",         0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
175         POPT_TABLEEND
176 };
177
178 struct poptOption popt_common_configfile[] = {
179         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
180         { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
181         POPT_TABLEEND
182 };
183
184 struct poptOption popt_common_version[] = {
185         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
186         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
187         POPT_TABLEEND
188 };
189
190 struct poptOption popt_common_debuglevel[] = {
191         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
192         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
193         POPT_TABLEEND
194 };
195
196
197 /* Handle command line options:
198  *              --sbindir
199  *              --bindir
200  *              --swatdir
201  *              --lmhostsfile
202  *              --libdir
203  *              --modulesdir
204  *              --shlibext
205  *              --lockdir
206  *              --statedir
207  *              --cachedir
208  *              --piddir
209  *              --smb-passwd-file
210  *              --private-dir
211  */
212
213 enum dyn_item{
214         DYN_SBINDIR = 1,
215         DYN_BINDIR,
216         DYN_SWATDIR,
217         DYN_LMHOSTSFILE,
218         DYN_LIBDIR,
219         DYN_MODULESDIR,
220         DYN_SHLIBEXT,
221         DYN_LOCKDIR,
222         DYN_STATEDIR,
223         DYN_CACHEDIR,
224         DYN_PIDDIR,
225         DYN_SMB_PASSWD_FILE,
226         DYN_PRIVATE_DIR,
227 };
228
229
230 static void popt_dynconfig_callback(poptContext con,
231                            enum poptCallbackReason reason,
232                            const struct poptOption *opt,
233                            const char *arg, const void *data)
234 {
235
236         switch (opt->val) {
237         case DYN_SBINDIR:
238                 if (arg) {
239                         set_dyn_SBINDIR(arg);
240                 }
241                 break;
242
243         case DYN_BINDIR:
244                 if (arg) {
245                         set_dyn_BINDIR(arg);
246                 }
247                 break;
248
249         case DYN_SWATDIR:
250                 if (arg) {
251                         set_dyn_SWATDIR(arg);
252                 }
253                 break;
254
255         case DYN_LMHOSTSFILE:
256                 if (arg) {
257                         set_dyn_LMHOSTSFILE(arg);
258                 }
259                 break;
260
261         case DYN_LIBDIR:
262                 if (arg) {
263                         set_dyn_LIBDIR(arg);
264                 }
265                 break;
266
267         case DYN_MODULESDIR:
268                 if (arg) {
269                         set_dyn_MODULESDIR(arg);
270                 }
271                 break;
272
273         case DYN_SHLIBEXT:
274                 if (arg) {
275                         set_dyn_SHLIBEXT(arg);
276                 }
277                 break;
278
279         case DYN_LOCKDIR:
280                 if (arg) {
281                         set_dyn_LOCKDIR(arg);
282                 }
283                 break;
284
285         case DYN_STATEDIR:
286                 if (arg) {
287                         set_dyn_STATEDIR(arg);
288                 }
289                 break;
290
291         case DYN_CACHEDIR:
292                 if (arg) {
293                         set_dyn_CACHEDIR(arg);
294                 }
295                 break;
296
297         case DYN_PIDDIR:
298                 if (arg) {
299                         set_dyn_PIDDIR(arg);
300                 }
301                 break;
302
303         case DYN_SMB_PASSWD_FILE:
304                 if (arg) {
305                         set_dyn_SMB_PASSWD_FILE(arg);
306                 }
307                 break;
308
309         case DYN_PRIVATE_DIR:
310                 if (arg) {
311                         set_dyn_PRIVATE_DIR(arg);
312                 }
313                 break;
314
315         }
316 }
317
318 const struct poptOption popt_common_dynconfig[] = {
319
320         { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
321
322         { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
323             "Path to sbin directory", "SBINDIR" },
324         { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
325             "Path to bin directory", "BINDIR" },
326         { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
327             "Path to SWAT installation directory", "SWATDIR" },
328         { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
329             "Path to lmhosts file", "LMHOSTSFILE" },
330         { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
331             "Path to shared library directory", "LIBDIR" },
332         { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
333             "Path to shared modules directory", "MODULESDIR" },
334         { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
335             "Shared library extension", "SHLIBEXT" },
336         { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
337             "Path to lock file directory", "LOCKDIR" },
338         { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
339             "Path to persistent state file directory", "STATEDIR" },
340         { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
341             "Path to temporary cache file directory", "CACHEDIR" },
342         { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
343             "Path to PID file directory", "PIDDIR" },
344         { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
345             "Path to smbpasswd file", "SMB_PASSWD_FILE" },
346         { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
347             "Path to private data directory", "PRIVATE_DIR" },
348
349         POPT_TABLEEND
350 };
351
352 /****************************************************************************
353  * get a password from a a file or file descriptor
354  * exit on failure
355  * ****************************************************************************/
356
357 static void get_password_file(struct user_auth_info *auth_info)
358 {
359         int fd = -1;
360         char *p;
361         bool close_it = False;
362         char *spec = NULL;
363         char pass[128];
364
365         if ((p = getenv("PASSWD_FD")) != NULL) {
366                 if (asprintf(&spec, "descriptor %s", p) < 0) {
367                         return;
368                 }
369                 sscanf(p, "%d", &fd);
370                 close_it = false;
371         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
372                 fd = sys_open(p, O_RDONLY, 0);
373                 spec = SMB_STRDUP(p);
374                 if (fd < 0) {
375                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
376                                         spec, strerror(errno));
377                         exit(1);
378                 }
379                 close_it = True;
380         }
381
382         if (fd < 0) {
383                 fprintf(stderr, "fd = %d, < 0\n", fd);
384                 exit(1);
385         }
386
387         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
388                 p && p - pass < sizeof(pass);) {
389                 switch (read(fd, p, 1)) {
390                 case 1:
391                         if (*p != '\n' && *p != '\0') {
392                                 *++p = '\0'; /* advance p, and null-terminate pass */
393                                 break;
394                         }
395                 case 0:
396                         if (p - pass) {
397                                 *p = '\0'; /* null-terminate it, just in case... */
398                                 p = NULL; /* then force the loop condition to become false */
399                                 break;
400                         } else {
401                                 fprintf(stderr, "Error reading password from file %s: %s\n",
402                                                 spec, "empty password\n");
403                                 SAFE_FREE(spec);
404                                 exit(1);
405                         }
406
407                 default:
408                         fprintf(stderr, "Error reading password from file %s: %s\n",
409                                         spec, strerror(errno));
410                         SAFE_FREE(spec);
411                         exit(1);
412                 }
413         }
414         SAFE_FREE(spec);
415
416         set_cmdline_auth_info_password(auth_info, pass);
417         if (close_it) {
418                 close(fd);
419         }
420 }
421
422 static void get_credentials_file(struct user_auth_info *auth_info,
423                                  const char *file)
424 {
425         XFILE *auth;
426         fstring buf;
427         uint16 len = 0;
428         char *ptr, *val, *param;
429
430         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
431         {
432                 /* fail if we can't open the credentials file */
433                 d_printf("ERROR: Unable to open credentials file!\n");
434                 exit(-1);
435         }
436
437         while (!x_feof(auth))
438         {
439                 /* get a line from the file */
440                 if (!x_fgets(buf, sizeof(buf), auth))
441                         continue;
442                 len = strlen(buf);
443
444                 if ((len) && (buf[len-1]=='\n'))
445                 {
446                         buf[len-1] = '\0';
447                         len--;
448                 }
449                 if (len == 0)
450                         continue;
451
452                 /* break up the line into parameter & value.
453                  * will need to eat a little whitespace possibly */
454                 param = buf;
455                 if (!(ptr = strchr_m (buf, '=')))
456                         continue;
457
458                 val = ptr+1;
459                 *ptr = '\0';
460
461                 /* eat leading white space */
462                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
463                         val++;
464
465                 if (strwicmp("password", param) == 0) {
466                         set_cmdline_auth_info_password(auth_info, val);
467                 } else if (strwicmp("username", param) == 0) {
468                         set_cmdline_auth_info_username(auth_info, val);
469                 } else if (strwicmp("domain", param) == 0) {
470                         set_global_myworkgroup(val);
471                 }
472                 memset(buf, 0, sizeof(buf));
473         }
474         x_fclose(auth);
475 }
476
477 /* Handle command line options:
478  *              -U,--user
479  *              -A,--authentication-file
480  *              -k,--use-kerberos
481  *              -N,--no-pass
482  *              -S,--signing
483  *              -P --machine-pass
484  *              -e --encrypt
485  *              -C --use-ccache
486  */
487
488
489 static void popt_common_credentials_callback(poptContext con,
490                                         enum poptCallbackReason reason,
491                                         const struct poptOption *opt,
492                                         const char *arg, const void *data)
493 {
494         struct user_auth_info *auth_info = talloc_get_type_abort(
495                 *((const char **)data), struct user_auth_info);
496         char *p;
497
498         if (reason == POPT_CALLBACK_REASON_PRE) {
499                 set_cmdline_auth_info_username(auth_info, "GUEST");
500
501                 if (getenv("LOGNAME")) {
502                         set_cmdline_auth_info_username(auth_info,
503                                                        getenv("LOGNAME"));
504                 }
505
506                 if (getenv("USER")) {
507                         char *puser = SMB_STRDUP(getenv("USER"));
508                         if (!puser) {
509                                 exit(ENOMEM);
510                         }
511                         set_cmdline_auth_info_username(auth_info, puser);
512
513                         if ((p = strchr_m(puser,'%'))) {
514                                 size_t len;
515                                 *p = 0;
516                                 len = strlen(p+1);
517                                 set_cmdline_auth_info_password(auth_info, p+1);
518                                 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
519                         }
520                         SAFE_FREE(puser);
521                 }
522
523                 if (getenv("PASSWD")) {
524                         set_cmdline_auth_info_password(auth_info,
525                                                        getenv("PASSWD"));
526                 }
527
528                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
529                         get_password_file(auth_info);
530                 }
531
532                 return;
533         }
534
535         switch(opt->val) {
536         case 'U':
537                 {
538                         char *lp;
539                         char *puser = SMB_STRDUP(arg);
540
541                         if ((lp=strchr_m(puser,'%'))) {
542                                 size_t len;
543                                 *lp = 0;
544                                 set_cmdline_auth_info_username(auth_info,
545                                                                puser);
546                                 set_cmdline_auth_info_password(auth_info,
547                                                                lp+1);
548                                 len = strlen(lp+1);
549                                 memset(strchr_m(arg,'%')+1,'X',len);
550                         } else {
551                                 set_cmdline_auth_info_username(auth_info,
552                                                                puser);
553                         }
554                         SAFE_FREE(puser);
555                 }
556                 break;
557
558         case 'A':
559                 get_credentials_file(auth_info, arg);
560                 break;
561
562         case 'k':
563 #ifndef HAVE_KRB5
564                 d_printf("No kerberos support compiled in\n");
565                 exit(1);
566 #else
567                 set_cmdline_auth_info_use_krb5_ticket(auth_info);
568 #endif
569                 break;
570
571         case 'S':
572                 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
573                         fprintf(stderr, "Unknown signing option %s\n", arg );
574                         exit(1);
575                 }
576                 break;
577         case 'P':
578                 set_cmdline_auth_info_use_machine_account(auth_info);
579                 break;
580         case 'N':
581                 set_cmdline_auth_info_password(auth_info, "");
582                 break;
583         case 'e':
584                 set_cmdline_auth_info_smb_encrypt(auth_info);
585                 break;
586         case 'C':
587                 set_cmdline_auth_info_use_ccache(auth_info, true);
588                 break;
589         }
590 }
591
592 static struct user_auth_info *global_auth_info;
593
594 void popt_common_set_auth_info(struct user_auth_info *auth_info)
595 {
596         global_auth_info = auth_info;
597 }
598
599 struct poptOption popt_common_credentials[] = {
600         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
601           (void *)popt_common_credentials_callback, 0,
602           (const char *)&global_auth_info },
603         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
604         { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
605         { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
606         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
607         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
608         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
609         {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
610         {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
611          "Use the winbind ccache for authentication" },
612         POPT_TABLEEND
613 };