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