I found lots of places where we assume error will be set when calling
[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
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, (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  *              --shlibext
188  *              --lockdir
189  *              --piddir
190  *              --smb-passwd-file
191  *              --private-dir
192  */
193
194 enum dyn_item{
195         DYN_SBINDIR = 1,
196         DYN_BINDIR,
197         DYN_SWATDIR,
198         DYN_LMHOSTSFILE,
199         DYN_LIBDIR,
200         DYN_SHLIBEXT,
201         DYN_LOCKDIR,
202         DYN_PIDDIR,
203         DYN_SMB_PASSWD_FILE,
204         DYN_PRIVATE_DIR,
205 };
206
207
208 static void popt_dynconfig_callback(poptContext con,
209                            enum poptCallbackReason reason,
210                            const struct poptOption *opt,
211                            const char *arg, const void *data)
212 {
213
214         switch (opt->val) {
215         case DYN_SBINDIR:
216                 if (arg) {
217                         set_dyn_SBINDIR(arg);
218                 }
219                 break;
220
221         case DYN_BINDIR:
222                 if (arg) {
223                         set_dyn_BINDIR(arg);
224                 }
225                 break;
226
227         case DYN_SWATDIR:
228                 if (arg) {
229                         set_dyn_SWATDIR(arg);
230                 }
231                 break;
232
233         case DYN_LMHOSTSFILE:
234                 if (arg) {
235                         set_dyn_LMHOSTSFILE(arg);
236                 }
237                 break;
238
239         case DYN_LIBDIR:
240                 if (arg) {
241                         set_dyn_LIBDIR(arg);
242                 }
243                 break;
244
245         case DYN_SHLIBEXT:
246                 if (arg) {
247                         set_dyn_SHLIBEXT(arg);
248                 }
249                 break;
250
251         case DYN_LOCKDIR:
252                 if (arg) {
253                         set_dyn_LOCKDIR(arg);
254                 }
255                 break;
256
257         case DYN_PIDDIR:
258                 if (arg) {
259                         set_dyn_PIDDIR(arg);
260                 }
261                 break;
262
263         case DYN_SMB_PASSWD_FILE:
264                 if (arg) {
265                         set_dyn_SMB_PASSWD_FILE(arg);
266                 }
267                 break;
268
269         case DYN_PRIVATE_DIR:
270                 if (arg) {
271                         set_dyn_PRIVATE_DIR(arg);
272                 }
273                 break;
274
275         }
276 }
277
278 const struct poptOption popt_common_dynconfig[] = {
279
280         { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
281
282         { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
283             "Path to sbin directory", "SBINDIR" },
284         { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
285             "Path to bin directory", "BINDIR" },
286         { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
287             "Path to SWAT installation directory", "SWATDIR" },
288         { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
289             "Path to lmhosts file", "LMHOSTSFILE" },
290         { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
291             "Path to shared library directory", "LIBDIR" },
292         { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
293             "Shared library extension", "SHLIBEXT" },
294         { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
295             "Path to lock file directory", "LOCKDIR" },
296         { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
297             "Path to PID file directory", "PIDDIR" },
298         { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
299             "Path to smbpasswd file", "SMB_PASSWD_FILE" },
300         { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
301             "Path to private data directory", "PRIVATE_DIR" },
302
303         POPT_TABLEEND
304 };
305
306 /****************************************************************************
307  * get a password from a a file or file descriptor
308  * exit on failure
309  * ****************************************************************************/
310
311 static void get_password_file(void)
312 {
313         int fd = -1;
314         char *p;
315         bool close_it = False;
316         char *spec = NULL;
317         char pass[128];
318
319         if ((p = getenv("PASSWD_FD")) != NULL) {
320                 if (asprintf(&spec, "descriptor %s", p) < 0) {
321                         return;
322                 }
323                 sscanf(p, "%d", &fd);
324                 close_it = false;
325         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
326                 fd = sys_open(p, O_RDONLY, 0);
327                 spec = SMB_STRDUP(p);
328                 if (fd < 0) {
329                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
330                                         spec, strerror(errno));
331                         exit(1);
332                 }
333                 close_it = True;
334         }
335
336         if (fd < 0) {
337                 fprintf(stderr, "fd = %d, < 0\n", fd);
338                 exit(1);
339         }
340
341         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
342                 p && p - pass < sizeof(pass);) {
343                 switch (read(fd, p, 1)) {
344                 case 1:
345                         if (*p != '\n' && *p != '\0') {
346                                 *++p = '\0'; /* advance p, and null-terminate pass */
347                                 break;
348                         }
349                 case 0:
350                         if (p - pass) {
351                                 *p = '\0'; /* null-terminate it, just in case... */
352                                 p = NULL; /* then force the loop condition to become false */
353                                 break;
354                         } else {
355                                 fprintf(stderr, "Error reading password from file %s: %s\n",
356                                                 spec, "empty password\n");
357                                 SAFE_FREE(spec);
358                                 exit(1);
359                         }
360
361                 default:
362                         fprintf(stderr, "Error reading password from file %s: %s\n",
363                                         spec, strerror(errno));
364                         SAFE_FREE(spec);
365                         exit(1);
366                 }
367         }
368         SAFE_FREE(spec);
369
370         set_cmdline_auth_info_password(pass);
371         if (close_it) {
372                 close(fd);
373         }
374 }
375
376 static void get_credentials_file(const char *file)
377 {
378         XFILE *auth;
379         fstring buf;
380         uint16 len = 0;
381         char *ptr, *val, *param;
382
383         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
384         {
385                 /* fail if we can't open the credentials file */
386                 d_printf("ERROR: Unable to open credentials file!\n");
387                 exit(-1);
388         }
389
390         while (!x_feof(auth))
391         {
392                 /* get a line from the file */
393                 if (!x_fgets(buf, sizeof(buf), auth))
394                         continue;
395                 len = strlen(buf);
396
397                 if ((len) && (buf[len-1]=='\n'))
398                 {
399                         buf[len-1] = '\0';
400                         len--;
401                 }
402                 if (len == 0)
403                         continue;
404
405                 /* break up the line into parameter & value.
406                  * will need to eat a little whitespace possibly */
407                 param = buf;
408                 if (!(ptr = strchr_m (buf, '=')))
409                         continue;
410
411                 val = ptr+1;
412                 *ptr = '\0';
413
414                 /* eat leading white space */
415                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
416                         val++;
417
418                 if (strwicmp("password", param) == 0) {
419                         set_cmdline_auth_info_password(val);
420                 } else if (strwicmp("username", param) == 0) {
421                         set_cmdline_auth_info_username(val);
422                 } else if (strwicmp("domain", param) == 0) {
423                         set_global_myworkgroup(val);
424                 }
425                 memset(buf, 0, sizeof(buf));
426         }
427         x_fclose(auth);
428 }
429
430 /* Handle command line options:
431  *              -U,--user
432  *              -A,--authentication-file
433  *              -k,--use-kerberos
434  *              -N,--no-pass
435  *              -S,--signing
436  *              -P --machine-pass
437  *              -e --encrypt
438  */
439
440
441 static void popt_common_credentials_callback(poptContext con,
442                                         enum poptCallbackReason reason,
443                                         const struct poptOption *opt,
444                                         const char *arg, const void *data)
445 {
446         char *p;
447
448         if (reason == POPT_CALLBACK_REASON_PRE) {
449                 set_cmdline_auth_info_username("GUEST");
450
451                 if (getenv("LOGNAME")) {
452                         set_cmdline_auth_info_username(getenv("LOGNAME"));
453                 }
454
455                 if (getenv("USER")) {
456                         char *puser = SMB_STRDUP(getenv("USER"));
457                         if (!puser) {
458                                 exit(ENOMEM);
459                         }
460                         set_cmdline_auth_info_username(puser);
461
462                         if ((p = strchr_m(puser,'%'))) {
463                                 size_t len;
464                                 *p = 0;
465                                 len = strlen(p+1);
466                                 set_cmdline_auth_info_password(p+1);
467                                 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
468                         }
469                         SAFE_FREE(puser);
470                 }
471
472                 if (getenv("PASSWD")) {
473                         set_cmdline_auth_info_password(getenv("PASSWD"));
474                 }
475
476                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
477                         get_password_file();
478                 }
479
480                 return;
481         }
482
483         switch(opt->val) {
484         case 'U':
485                 {
486                         char *lp;
487                         char *puser = SMB_STRDUP(arg);
488
489                         if ((lp=strchr_m(puser,'%'))) {
490                                 size_t len;
491                                 *lp = 0;
492                                 set_cmdline_auth_info_username(puser);
493                                 set_cmdline_auth_info_password(lp+1);
494                                 len = strlen(lp+1);
495                                 memset(strchr_m(arg,'%')+1,'X',len);
496                         } else {
497                                 set_cmdline_auth_info_username(puser);
498                         }
499                         SAFE_FREE(puser);
500                 }
501                 break;
502
503         case 'A':
504                 get_credentials_file(arg);
505                 break;
506
507         case 'k':
508 #ifndef HAVE_KRB5
509                 d_printf("No kerberos support compiled in\n");
510                 exit(1);
511 #else
512                 set_cmdline_auth_info_use_krb5_ticket();
513 #endif
514                 break;
515
516         case 'S':
517                 if (!set_cmdline_auth_info_signing_state(arg)) {
518                         fprintf(stderr, "Unknown signing option %s\n", arg );
519                         exit(1);
520                 }
521                 break;
522         case 'P':
523                 set_cmdline_auth_info_use_machine_account();
524                 break;
525         case 'N':
526                 set_cmdline_auth_info_password("");
527                 break;
528         case 'e':
529                 set_cmdline_auth_info_smb_encrypt();
530                 break;
531
532         }
533 }
534
535 struct poptOption popt_common_credentials[] = {
536         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
537         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
538         { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
539         { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
540         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
541         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
542         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
543         {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
544         POPT_TABLEEND
545 };