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