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