Remove the char[1024] strings from dynconfig. Replace
[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 *logfile = 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(&logfile, "%s/log.%s", arg, pname) < 0) {
54                 return;
55         }
56         lp_set_logfile(logfile);
57         SAFE_FREE(logfile);
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                 if (!PrintSambaVersionString) return;
75
76                 printf( "Version %s\n", SAMBA_VERSION_STRING);
77                 exit(0);
78                 return;
79         }
80
81         switch(opt->val) {
82         case 'd':
83                 if (arg) {
84                         debug_parse_levels(arg);
85                         AllowDebugChange = False;
86                 }
87                 break;
88
89         case 'V':
90                 PrintSambaVersionString = True;
91                 break;
92
93         case 'O':
94                 if (arg) {
95                         lp_do_parameter(-1, "socket options", arg);
96                 }
97                 break;
98
99         case 's':
100                 if (arg) {
101                         set_dyn_CONFIGFILE(arg);
102                 }
103                 break;
104
105         case 'n':
106                 if (arg) {
107                         set_global_myname(arg);
108                 }
109                 break;
110
111         case 'l':
112                 if (arg) {
113                         set_logfile(con, arg);
114                         override_logfile = True;
115                         set_dyn_LOGFILEBASE(arg);
116                 }
117                 break;
118
119         case 'i':
120                 if (arg) {
121                           set_global_scope(arg);
122                 }
123                 break;
124
125         case 'W':
126                 if (arg) {
127                         set_global_myworkgroup(arg);
128                 }
129                 break;
130         }
131 }
132
133 struct poptOption popt_common_connection[] = {
134         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
135         { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
136           "SOCKETOPTIONS" },
137         { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
138         { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
139         { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
140
141         POPT_TABLEEND
142 };
143
144 struct poptOption popt_common_samba[] = {
145         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
146         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
147         { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
148         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
149         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
150         POPT_TABLEEND
151 };
152
153 struct poptOption popt_common_version[] = {
154         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
155         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
156         POPT_TABLEEND
157 };
158
159
160 /* Handle command line options:
161  *              --sbindir
162  *              --bindir
163  *              --swatdir
164  *              --lmhostsfile
165  *              --libdir
166  *              --shlibext
167  *              --lockdir
168  *              --piddir
169  *              --smb-passwd-file
170  *              --private-dir
171  */
172
173 enum dyn_item{
174         DYN_SBINDIR = 1,
175         DYN_BINDIR,
176         DYN_SWATDIR,
177         DYN_LMHOSTSFILE,
178         DYN_LIBDIR,
179         DYN_SHLIBEXT,
180         DYN_LOCKDIR,
181         DYN_PIDDIR,
182         DYN_SMB_PASSWD_FILE,
183         DYN_PRIVATE_DIR,
184 };
185
186
187 static void popt_dynconfig_callback(poptContext con,
188                            enum poptCallbackReason reason,
189                            const struct poptOption *opt,
190                            const char *arg, const void *data)
191 {
192
193         switch (opt->val) {
194         case DYN_SBINDIR:
195                 if (arg) {
196                         set_dyn_SBINDIR(arg);
197                 }
198                 break;
199
200         case DYN_BINDIR:
201                 if (arg) {
202                         set_dyn_BINDIR(arg);
203                 }
204                 break;
205
206         case DYN_SWATDIR:
207                 if (arg) {
208                         set_dyn_SWATDIR(arg);
209                 }
210                 break;
211
212         case DYN_LMHOSTSFILE:
213                 if (arg) {
214                         set_dyn_LMHOSTSFILE(arg);
215                 }
216                 break;
217
218         case DYN_LIBDIR:
219                 if (arg) {
220                         set_dyn_LIBDIR(arg);
221                 }
222                 break;
223
224         case DYN_SHLIBEXT:
225                 if (arg) {
226                         set_dyn_SHLIBEXT(arg);
227                 }
228                 break;
229
230         case DYN_LOCKDIR:
231                 if (arg) {
232                         set_dyn_LOCKDIR(arg);
233                 }
234                 break;
235
236         case DYN_PIDDIR:
237                 if (arg) {
238                         set_dyn_PIDDIR(arg);
239                 }
240                 break;
241
242         case DYN_SMB_PASSWD_FILE:
243                 if (arg) {
244                         set_dyn_SMB_PASSWD_FILE(arg);
245                 }
246                 break;
247
248         case DYN_PRIVATE_DIR:
249                 if (arg) {
250                         set_dyn_PRIVATE_DIR(arg);
251                 }
252                 break;
253
254         }
255 }
256
257 const struct poptOption popt_common_dynconfig[] = {
258
259         { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
260
261         { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
262             "Path to sbin directory", "SBINDIR" },
263         { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
264             "Path to bin directory", "BINDIR" },
265         { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
266             "Path to SWAT installation directory", "SWATDIR" },
267         { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
268             "Path to lmhosts file", "LMHOSTSFILE" },
269         { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
270             "Path to shared library directory", "LIBDIR" },
271         { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
272             "Shared library extension", "SHLIBEXT" },
273         { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
274             "Path to lock file directory", "LOCKDIR" },
275         { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
276             "Path to PID file directory", "PIDDIR" },
277         { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
278             "Path to smbpasswd file", "SMB_PASSWD_FILE" },
279         { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
280             "Path to private data directory", "PRIVATE_DIR" },
281
282         POPT_TABLEEND
283 };
284
285 /****************************************************************************
286  * get a password from a a file or file descriptor
287  * exit on failure
288  * ****************************************************************************/
289
290 static void get_password_file(void)
291 {
292         int fd = -1;
293         char *p;
294         bool close_it = False;
295         char *spec = NULL;
296         char pass[128];
297
298         if ((p = getenv("PASSWD_FD")) != NULL) {
299                 if (asprintf(&spec, "descriptor %s", p) < 0) {
300                         return;
301                 }
302                 sscanf(p, "%d", &fd);
303                 close_it = false;
304         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
305                 fd = sys_open(p, O_RDONLY, 0);
306                 spec = SMB_STRDUP(p);
307                 if (fd < 0) {
308                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
309                                         spec, strerror(errno));
310                         exit(1);
311                 }
312                 close_it = True;
313         }
314
315         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
316                 p && p - pass < sizeof(pass);) {
317                 switch (read(fd, p, 1)) {
318                 case 1:
319                         if (*p != '\n' && *p != '\0') {
320                                 *++p = '\0'; /* advance p, and null-terminate pass */
321                                 break;
322                         }
323                 case 0:
324                         if (p - pass) {
325                                 *p = '\0'; /* null-terminate it, just in case... */
326                                 p = NULL; /* then force the loop condition to become false */
327                                 break;
328                         } else {
329                                 fprintf(stderr, "Error reading password from file %s: %s\n",
330                                                 spec, "empty password\n");
331                                 SAFE_FREE(spec);
332                                 exit(1);
333                         }
334
335                 default:
336                         fprintf(stderr, "Error reading password from file %s: %s\n",
337                                         spec, strerror(errno));
338                         SAFE_FREE(spec);
339                         exit(1);
340                 }
341         }
342         SAFE_FREE(spec);
343
344         set_cmdline_auth_info_password(pass);
345         if (close_it) {
346                 close(fd);
347         }
348 }
349
350 static void get_credentials_file(const char *file)
351 {
352         XFILE *auth;
353         fstring buf;
354         uint16 len = 0;
355         char *ptr, *val, *param;
356
357         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
358         {
359                 /* fail if we can't open the credentials file */
360                 d_printf("ERROR: Unable to open credentials file!\n");
361                 exit(-1);
362         }
363
364         while (!x_feof(auth))
365         {
366                 /* get a line from the file */
367                 if (!x_fgets(buf, sizeof(buf), auth))
368                         continue;
369                 len = strlen(buf);
370
371                 if ((len) && (buf[len-1]=='\n'))
372                 {
373                         buf[len-1] = '\0';
374                         len--;
375                 }
376                 if (len == 0)
377                         continue;
378
379                 /* break up the line into parameter & value.
380                  * will need to eat a little whitespace possibly */
381                 param = buf;
382                 if (!(ptr = strchr_m (buf, '=')))
383                         continue;
384
385                 val = ptr+1;
386                 *ptr = '\0';
387
388                 /* eat leading white space */
389                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
390                         val++;
391
392                 if (strwicmp("password", param) == 0) {
393                         set_cmdline_auth_info_password(val);
394                 } else if (strwicmp("username", param) == 0) {
395                         set_cmdline_auth_info_username(val);
396                 } else if (strwicmp("domain", param) == 0) {
397                         set_global_myworkgroup(val);
398                 }
399                 memset(buf, 0, sizeof(buf));
400         }
401         x_fclose(auth);
402 }
403
404 /* Handle command line options:
405  *              -U,--user
406  *              -A,--authentication-file
407  *              -k,--use-kerberos
408  *              -N,--no-pass
409  *              -S,--signing
410  *              -P --machine-pass
411  */
412
413
414 static void popt_common_credentials_callback(poptContext con,
415                                         enum poptCallbackReason reason,
416                                         const struct poptOption *opt,
417                                         const char *arg, const void *data)
418 {
419         char *p;
420
421         if (reason == POPT_CALLBACK_REASON_PRE) {
422                 set_cmdline_auth_info_username("GUEST");
423
424                 if (getenv("LOGNAME")) {
425                         set_cmdline_auth_info_username(getenv("LOGNAME"));
426                 }
427
428                 if (getenv("USER")) {
429                         char *puser = SMB_STRDUP(getenv("USER"));
430                         if (!puser) {
431                                 exit(ENOMEM);
432                         }
433                         set_cmdline_auth_info_username(puser);
434
435                         if ((p = strchr_m(puser,'%'))) {
436                                 size_t len;
437                                 *p = 0;
438                                 len = strlen(p+1);
439                                 set_cmdline_auth_info_password(p+1);
440                                 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
441                         }
442                         SAFE_FREE(puser);
443                 }
444
445                 if (getenv("PASSWD")) {
446                         set_cmdline_auth_info_password(getenv("PASSWD"));
447                 }
448
449                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
450                         get_password_file();
451                 }
452
453                 return;
454         }
455
456         switch(opt->val) {
457         case 'U':
458                 {
459                         char *lp;
460                         char *puser = SMB_STRDUP(arg);
461
462                         if ((lp=strchr_m(puser,'%'))) {
463                                 size_t len;
464                                 *lp = 0;
465                                 set_cmdline_auth_info_username(puser);
466                                 set_cmdline_auth_info_password(lp+1);
467                                 len = strlen(lp+1);
468                                 memset(strchr_m(arg,'%')+1,'X',len);
469                         } else {
470                                 set_cmdline_auth_info_username(puser);
471                         }
472                         SAFE_FREE(puser);
473                 }
474                 break;
475
476         case 'A':
477                 get_credentials_file(arg);
478                 break;
479
480         case 'k':
481 #ifndef HAVE_KRB5
482                 d_printf("No kerberos support compiled in\n");
483                 exit(1);
484 #else
485                 set_cmdline_auth_info_use_krb5_ticket();
486 #endif
487                 break;
488
489         case 'S':
490                 if (!set_cmdline_auth_info_signing_state(arg)) {
491                         fprintf(stderr, "Unknown signing option %s\n", arg );
492                         exit(1);
493                 }
494                 break;
495         case 'P':
496                 {
497                         char *opt_password = NULL;
498                         char *pwd = NULL;
499
500                         /* it is very useful to be able to make ads queries as the
501                            machine account for testing purposes and for domain leave */
502
503                         if (!secrets_init()) {
504                                 d_printf("ERROR: Unable to open secrets database\n");
505                                 exit(1);
506                         }
507
508                         opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
509
510                         if (!opt_password) {
511                                 d_printf("ERROR: Unable to fetch machine password\n");
512                                 exit(1);
513                         }
514                         if (asprintf(&pwd, "%s$", global_myname()) < 0) {
515                                 exit(ENOMEM);
516                         }
517                         set_cmdline_auth_info_username(pwd);
518                         set_cmdline_auth_info_password(opt_password);
519                         SAFE_FREE(pwd);
520                         SAFE_FREE(opt_password);
521
522                         /* machine accounts only work with kerberos */
523                         set_cmdline_auth_info_use_krb5_ticket();
524                 }
525                 break;
526         case 'N':
527                 set_cmdline_auth_info_password("");
528                 break;
529         }
530 }
531
532 struct poptOption popt_common_credentials[] = {
533         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
534         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
535         { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
536         { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
537         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
538         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
539         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
540         POPT_TABLEEND
541 };