Remove the global "cmdline_auth_info" from source3/lib/util.c
[kai/samba.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
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
34  */
35
36 extern bool AllowDebugChange;
37 extern bool override_logfile;
38
39 static void set_logfile(poptContext con, const char * arg)
40 {
41
42         char *lfile = NULL;
43         const char *pname;
44
45         /* Find out basename of current program */
46         pname = strrchr_m(poptGetInvocationName(con),'/');
47
48         if (!pname)
49                 pname = poptGetInvocationName(con);
50         else
51                 pname++;
52
53         if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
54                 return;
55         }
56         lp_set_logfile(lfile);
57         SAFE_FREE(lfile);
58 }
59
60 static bool PrintSambaVersionString;
61
62 static void popt_common_callback(poptContext con,
63                            enum poptCallbackReason reason,
64                            const struct poptOption *opt,
65                            const char *arg, const void *data)
66 {
67
68         if (reason == POPT_CALLBACK_REASON_PRE) {
69                 set_logfile(con, get_dyn_LOGFILEBASE());
70                 return;
71         }
72
73         if (reason == POPT_CALLBACK_REASON_POST) {
74
75                 if (PrintSambaVersionString) {
76                         printf( "Version %s\n", SAMBA_VERSION_STRING);
77                         exit(0);
78                 }
79
80                 if (is_default_dyn_CONFIGFILE()) {
81                         if(getenv("SMB_CONF_PATH")) {
82                                 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
83                         }
84                 }
85
86                 /* Further 'every Samba program must do this' hooks here. */
87                 return;
88         }
89
90         switch(opt->val) {
91         case 'd':
92                 if (arg) {
93                         debug_parse_levels(arg);
94                         AllowDebugChange = False;
95                 }
96                 break;
97
98         case 'V':
99                 PrintSambaVersionString = True;
100                 break;
101
102         case 'O':
103                 if (arg) {
104                         lp_do_parameter(-1, "socket options", arg);
105                 }
106                 break;
107
108         case 's':
109                 if (arg) {
110                         set_dyn_CONFIGFILE(arg);
111                 }
112                 break;
113
114         case 'n':
115                 if (arg) {
116                         set_global_myname(arg);
117                 }
118                 break;
119
120         case 'l':
121                 if (arg) {
122                         set_logfile(con, arg);
123                         override_logfile = True;
124                         set_dyn_LOGFILEBASE(arg);
125                 }
126                 break;
127
128         case 'i':
129                 if (arg) {
130                           set_global_scope(arg);
131                 }
132                 break;
133
134         case 'W':
135                 if (arg) {
136                         set_global_myworkgroup(arg);
137                 }
138                 break;
139         }
140 }
141
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" },
149
150         POPT_TABLEEND
151 };
152
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
160 };
161
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
166 };
167
168 struct poptOption popt_common_version[] = {
169         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
170         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
171         POPT_TABLEEND
172 };
173
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
178 };
179
180
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
193  */
194
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,
207 };
208
209
210 static void popt_dynconfig_callback(poptContext con,
211                            enum poptCallbackReason reason,
212                            const struct poptOption *opt,
213                            const char *arg, const void *data)
214 {
215
216         switch (opt->val) {
217         case DYN_SBINDIR:
218                 if (arg) {
219                         set_dyn_SBINDIR(arg);
220                 }
221                 break;
222
223         case DYN_BINDIR:
224                 if (arg) {
225                         set_dyn_BINDIR(arg);
226                 }
227                 break;
228
229         case DYN_SWATDIR:
230                 if (arg) {
231                         set_dyn_SWATDIR(arg);
232                 }
233                 break;
234
235         case DYN_LMHOSTSFILE:
236                 if (arg) {
237                         set_dyn_LMHOSTSFILE(arg);
238                 }
239                 break;
240
241         case DYN_LIBDIR:
242                 if (arg) {
243                         set_dyn_LIBDIR(arg);
244                 }
245                 break;
246
247         case DYN_MODULESDIR:
248                 if (arg) {
249                         set_dyn_MODULESDIR(arg);
250                 }
251                 break;
252
253         case DYN_SHLIBEXT:
254                 if (arg) {
255                         set_dyn_SHLIBEXT(arg);
256                 }
257                 break;
258
259         case DYN_LOCKDIR:
260                 if (arg) {
261                         set_dyn_LOCKDIR(arg);
262                 }
263                 break;
264
265         case DYN_PIDDIR:
266                 if (arg) {
267                         set_dyn_PIDDIR(arg);
268                 }
269                 break;
270
271         case DYN_SMB_PASSWD_FILE:
272                 if (arg) {
273                         set_dyn_SMB_PASSWD_FILE(arg);
274                 }
275                 break;
276
277         case DYN_PRIVATE_DIR:
278                 if (arg) {
279                         set_dyn_PRIVATE_DIR(arg);
280                 }
281                 break;
282
283         }
284 }
285
286 const struct poptOption popt_common_dynconfig[] = {
287
288         { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
289
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" },
312
313         POPT_TABLEEND
314 };
315
316 /****************************************************************************
317  * get a password from a a file or file descriptor
318  * exit on failure
319  * ****************************************************************************/
320
321 static void get_password_file(struct user_auth_info *auth_info)
322 {
323         int fd = -1;
324         char *p;
325         bool close_it = False;
326         char *spec = NULL;
327         char pass[128];
328
329         if ((p = getenv("PASSWD_FD")) != NULL) {
330                 if (asprintf(&spec, "descriptor %s", p) < 0) {
331                         return;
332                 }
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);
342                 }
343                 close_it = True;
344         }
345
346         if (fd < 0) {
347                 fprintf(stderr, "fd = %d, < 0\n", fd);
348                 exit(1);
349         }
350
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;
358                         }
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);
369                         }
370
371                 default:
372                         fprintf(stderr, "Error reading password from file %s: %s\n",
373                                         spec, strerror(errno));
374                         SAFE_FREE(spec);
375                         exit(1);
376                 }
377         }
378         SAFE_FREE(spec);
379
380         set_cmdline_auth_info_password(auth_info, pass);
381         if (close_it) {
382                 close(fd);
383         }
384 }
385
386 static void get_credentials_file(struct user_auth_info *auth_info,
387                                  const char *file)
388 {
389         XFILE *auth;
390         fstring buf;
391         uint16 len = 0;
392         char *ptr, *val, *param;
393
394         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
395         {
396                 /* fail if we can't open the credentials file */
397                 d_printf("ERROR: Unable to open credentials file!\n");
398                 exit(-1);
399         }
400
401         while (!x_feof(auth))
402         {
403                 /* get a line from the file */
404                 if (!x_fgets(buf, sizeof(buf), auth))
405                         continue;
406                 len = strlen(buf);
407
408                 if ((len) && (buf[len-1]=='\n'))
409                 {
410                         buf[len-1] = '\0';
411                         len--;
412                 }
413                 if (len == 0)
414                         continue;
415
416                 /* break up the line into parameter & value.
417                  * will need to eat a little whitespace possibly */
418                 param = buf;
419                 if (!(ptr = strchr_m (buf, '=')))
420                         continue;
421
422                 val = ptr+1;
423                 *ptr = '\0';
424
425                 /* eat leading white space */
426                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
427                         val++;
428
429                 if (strwicmp("password", param) == 0) {
430                         set_cmdline_auth_info_password(auth_info, val);
431                 } else if (strwicmp("username", param) == 0) {
432                         set_cmdline_auth_info_username(auth_info, val);
433                 } else if (strwicmp("domain", param) == 0) {
434                         set_global_myworkgroup(val);
435                 }
436                 memset(buf, 0, sizeof(buf));
437         }
438         x_fclose(auth);
439 }
440
441 /* Handle command line options:
442  *              -U,--user
443  *              -A,--authentication-file
444  *              -k,--use-kerberos
445  *              -N,--no-pass
446  *              -S,--signing
447  *              -P --machine-pass
448  *              -e --encrypt
449  */
450
451
452 static void popt_common_credentials_callback(poptContext con,
453                                         enum poptCallbackReason reason,
454                                         const struct poptOption *opt,
455                                         const char *arg, const void *data)
456 {
457         struct user_auth_info *auth_info = talloc_get_type_abort(
458                 *((const char **)data), struct user_auth_info);
459         char *p;
460
461         if (reason == POPT_CALLBACK_REASON_PRE) {
462                 set_cmdline_auth_info_username(auth_info, "GUEST");
463
464                 if (getenv("LOGNAME")) {
465                         set_cmdline_auth_info_username(auth_info,
466                                                        getenv("LOGNAME"));
467                 }
468
469                 if (getenv("USER")) {
470                         char *puser = SMB_STRDUP(getenv("USER"));
471                         if (!puser) {
472                                 exit(ENOMEM);
473                         }
474                         set_cmdline_auth_info_username(auth_info, puser);
475
476                         if ((p = strchr_m(puser,'%'))) {
477                                 size_t len;
478                                 *p = 0;
479                                 len = strlen(p+1);
480                                 set_cmdline_auth_info_password(auth_info, p+1);
481                                 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
482                         }
483                         SAFE_FREE(puser);
484                 }
485
486                 if (getenv("PASSWD")) {
487                         set_cmdline_auth_info_password(auth_info,
488                                                        getenv("PASSWD"));
489                 }
490
491                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
492                         get_password_file(auth_info);
493                 }
494
495                 return;
496         }
497
498         switch(opt->val) {
499         case 'U':
500                 {
501                         char *lp;
502                         char *puser = SMB_STRDUP(arg);
503
504                         if ((lp=strchr_m(puser,'%'))) {
505                                 size_t len;
506                                 *lp = 0;
507                                 set_cmdline_auth_info_username(auth_info,
508                                                                puser);
509                                 set_cmdline_auth_info_password(auth_info,
510                                                                lp+1);
511                                 len = strlen(lp+1);
512                                 memset(strchr_m(arg,'%')+1,'X',len);
513                         } else {
514                                 set_cmdline_auth_info_username(auth_info,
515                                                                puser);
516                         }
517                         SAFE_FREE(puser);
518                 }
519                 break;
520
521         case 'A':
522                 get_credentials_file(auth_info, arg);
523                 break;
524
525         case 'k':
526 #ifndef HAVE_KRB5
527                 d_printf("No kerberos support compiled in\n");
528                 exit(1);
529 #else
530                 set_cmdline_auth_info_use_krb5_ticket(auth_info);
531 #endif
532                 break;
533
534         case 'S':
535                 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
536                         fprintf(stderr, "Unknown signing option %s\n", arg );
537                         exit(1);
538                 }
539                 break;
540         case 'P':
541                 set_cmdline_auth_info_use_machine_account(auth_info);
542                 break;
543         case 'N':
544                 set_cmdline_auth_info_password(auth_info, "");
545                 break;
546         case 'e':
547                 set_cmdline_auth_info_smb_encrypt(auth_info);
548                 break;
549
550         }
551 }
552
553 static struct user_auth_info *global_auth_info;
554
555 void popt_common_set_auth_info(struct user_auth_info *auth_info)
556 {
557         global_auth_info = auth_info;
558 }
559
560 struct poptOption popt_common_credentials[] = {
561         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
562           (void *)popt_common_credentials_callback, 0,
563           (const char *)&global_auth_info },
564         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
565         { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
566         { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
567         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
568         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
569         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
570         {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
571         POPT_TABLEEND
572 };