RIP BOOL. Convert BOOL -> bool. I found a few interesting
[sfrench/samba-autobuild/.git] / source / 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 pstring user_socket_options;
37 extern bool AllowDebugChange;
38 extern bool override_logfile;
39
40 struct user_auth_info cmdline_auth_info;
41
42 static void set_logfile(poptContext con, const char * arg)
43 {
44
45         pstring logfile;
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         pstr_sprintf(logfile, "%s/log.%s", arg, pname);
57         lp_set_logfile(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, 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                         pstrcpy(user_socket_options,arg);
96                 }
97                 break;
98
99         case 's':
100                 if (arg) {
101                         pstrcpy(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                         pstr_sprintf(dyn_LOGFILEBASE, "%s", 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                         dyn_SBINDIR = SMB_STRDUP(arg);
197                 }
198                 break;
199
200         case DYN_BINDIR:
201                 if (arg) {
202                         dyn_BINDIR = SMB_STRDUP(arg);
203                 }
204                 break;
205
206         case DYN_SWATDIR:
207                 if (arg) {
208                         dyn_SWATDIR = SMB_STRDUP(arg);
209                 }
210                 break;
211
212         case DYN_LMHOSTSFILE:
213                 if (arg) {
214                         pstrcpy(dyn_LMHOSTSFILE, arg);
215                 }
216                 break;
217
218         case DYN_LIBDIR:
219                 if (arg) {
220                         pstrcpy(dyn_LIBDIR, arg);
221                 }
222                 break;
223
224         case DYN_SHLIBEXT:
225                 if (arg) {
226                         fstrcpy(dyn_SHLIBEXT, arg);
227                 }
228                 break;
229
230         case DYN_LOCKDIR:
231                 if (arg) {
232                         pstrcpy(dyn_LOCKDIR, arg);
233                 }
234                 break;
235
236         case DYN_PIDDIR:
237                 if (arg) {
238                         pstrcpy(dyn_PIDDIR, arg);
239                 }
240                 break;
241
242         case DYN_SMB_PASSWD_FILE:
243                 if (arg) {
244                         pstrcpy(dyn_SMB_PASSWD_FILE, arg);
245                 }
246                 break;
247
248         case DYN_PRIVATE_DIR:
249                 if (arg) {
250                         pstrcpy(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 static void get_password_file(struct user_auth_info *a)
290 {
291         int fd = -1;
292         char *p;
293         bool close_it = False;
294         pstring spec;
295         char pass[128];
296
297         if ((p = getenv("PASSWD_FD")) != NULL) {
298                 pstrcpy(spec, "descriptor ");
299                 pstrcat(spec, p);
300                 sscanf(p, "%d", &fd);
301                 close_it = False;
302         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
303                 fd = sys_open(p, O_RDONLY, 0);
304                 pstrcpy(spec, p);
305                 if (fd < 0) {
306                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
307                                         spec, strerror(errno));
308                         exit(1);
309                 }
310                 close_it = True;
311         }
312
313         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
314                 p && p - pass < sizeof(pass);) {
315                 switch (read(fd, p, 1)) {
316                 case 1:
317                         if (*p != '\n' && *p != '\0') {
318                                 *++p = '\0'; /* advance p, and null-terminate pass */
319                                 break;
320                         }
321                 case 0:
322                         if (p - pass) {
323                                 *p = '\0'; /* null-terminate it, just in case... */
324                                 p = NULL; /* then force the loop condition to become false */
325                                 break;
326                         } else {
327                                 fprintf(stderr, "Error reading password from file %s: %s\n",
328                                                 spec, "empty password\n");
329                                 exit(1);
330                         }
331
332                 default:
333                         fprintf(stderr, "Error reading password from file %s: %s\n",
334                                         spec, strerror(errno));
335                         exit(1);
336                 }
337         }
338         pstrcpy(a->password, pass);
339         if (close_it)
340                 close(fd);
341 }
342
343 static void get_credentials_file(const char *file, struct user_auth_info *info) 
344 {
345         XFILE *auth;
346         fstring buf;
347         uint16 len = 0;
348         char *ptr, *val, *param;
349
350         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
351         {
352                 /* fail if we can't open the credentials file */
353                 d_printf("ERROR: Unable to open credentials file!\n");
354                 exit(-1);
355         }
356
357         while (!x_feof(auth))
358         {
359                 /* get a line from the file */
360                 if (!x_fgets(buf, sizeof(buf), auth))
361                         continue;
362                 len = strlen(buf);
363
364                 if ((len) && (buf[len-1]=='\n'))
365                 {
366                         buf[len-1] = '\0';
367                         len--;
368                 }
369                 if (len == 0)
370                         continue;
371
372                 /* break up the line into parameter & value.
373                  * will need to eat a little whitespace possibly */
374                 param = buf;
375                 if (!(ptr = strchr_m (buf, '=')))
376                         continue;
377
378                 val = ptr+1;
379                 *ptr = '\0';
380
381                 /* eat leading white space */
382                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
383                         val++;
384
385                 if (strwicmp("password", param) == 0)
386                 {
387                         pstrcpy(info->password, val);
388                         info->got_pass = True;
389                 }
390                 else if (strwicmp("username", param) == 0)
391                         pstrcpy(info->username, val);
392                 else if (strwicmp("domain", param) == 0)
393                         set_global_myworkgroup(val);
394                 memset(buf, 0, sizeof(buf));
395         }
396         x_fclose(auth);
397 }
398
399 /* Handle command line options:
400  *              -U,--user
401  *              -A,--authentication-file
402  *              -k,--use-kerberos
403  *              -N,--no-pass
404  *              -S,--signing
405  *              -P --machine-pass
406  */
407
408
409 static void popt_common_credentials_callback(poptContext con, 
410                                         enum poptCallbackReason reason,
411                                         const struct poptOption *opt,
412                                         const char *arg, const void *data)
413 {
414         char *p;
415
416         if (reason == POPT_CALLBACK_REASON_PRE) {
417                 cmdline_auth_info.use_kerberos = False;
418                 cmdline_auth_info.got_pass = False;
419                 cmdline_auth_info.signing_state = Undefined;
420                 pstrcpy(cmdline_auth_info.username, "GUEST");   
421
422                 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
423
424                 if (getenv("USER")) {
425                         pstrcpy(cmdline_auth_info.username,getenv("USER"));
426
427                         if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
428                                 *p = 0;
429                                 pstrcpy(cmdline_auth_info.password,p+1);
430                                 cmdline_auth_info.got_pass = True;
431                                 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
432                         }
433                 }
434
435                 if (getenv("PASSWD")) {
436                         pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
437                         cmdline_auth_info.got_pass = True;
438                 }
439
440                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
441                         get_password_file(&cmdline_auth_info);
442                         cmdline_auth_info.got_pass = True;
443                 }
444
445                 return;
446         }
447
448         switch(opt->val) {
449         case 'U':
450                 {
451                         char *lp;
452
453                         pstrcpy(cmdline_auth_info.username,arg);
454                         if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
455                                 *lp = 0;
456                                 pstrcpy(cmdline_auth_info.password,lp+1);
457                                 cmdline_auth_info.got_pass = True;
458                                 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
459                         }
460                 }
461                 break;
462
463         case 'A':
464                 get_credentials_file(arg, &cmdline_auth_info);
465                 break;
466
467         case 'k':
468 #ifndef HAVE_KRB5
469                 d_printf("No kerberos support compiled in\n");
470                 exit(1);
471 #else
472                 cmdline_auth_info.use_kerberos = True;
473                 cmdline_auth_info.got_pass = True;
474 #endif
475                 break;
476
477         case 'S':
478                 {
479                         cmdline_auth_info.signing_state = -1;
480                         if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
481                                 cmdline_auth_info.signing_state = False;
482                         else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
483                                         strequal(arg, "auto") )
484                                 cmdline_auth_info.signing_state = True;
485                         else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
486                                 cmdline_auth_info.signing_state = Required;
487                         else {
488                                 fprintf(stderr, "Unknown signing option %s\n", arg );
489                                 exit(1);
490                         }
491                 }
492                 break;
493         case 'P':
494                 {
495                         char *opt_password = NULL;
496                         /* it is very useful to be able to make ads queries as the
497                            machine account for testing purposes and for domain leave */
498                         
499                         if (!secrets_init()) {
500                                 d_printf("ERROR: Unable to open secrets database\n");
501                                 exit(1);
502                         }
503                         
504                         opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
505                         
506                         if (!opt_password) {
507                                 d_printf("ERROR: Unable to fetch machine password\n");
508                                 exit(1);
509                         }
510                         pstr_sprintf(cmdline_auth_info.username, "%s$", 
511                                      global_myname());
512                         pstrcpy(cmdline_auth_info.password,opt_password);
513                         SAFE_FREE(opt_password);
514
515                         /* machine accounts only work with kerberos */
516                         cmdline_auth_info.use_kerberos = True;
517                         cmdline_auth_info.got_pass = True;
518                 }
519                 break;
520         }
521 }
522
523
524
525 struct poptOption popt_common_credentials[] = {
526         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
527         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
528         { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
529         { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
530         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
531         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
532         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
533         POPT_TABLEEND
534 };