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