bd46373d581c8ae58a77b3d889e035a7f5094458
[samba.git] / source4 / lib / cmdline / 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
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
37 enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL};
38
39 static struct cmdline_auth_info cmdline_auth_info;
40
41 static void popt_common_callback(poptContext con, 
42                            enum poptCallbackReason reason,
43                            const struct poptOption *opt,
44                            const char *arg, const void *data)
45 {
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         if (reason == POPT_CALLBACK_REASON_PRE) {
57                 char *logfile = talloc_asprintf(NULL, "%s/log.%s", dyn_LOGFILEBASE, pname);
58                 lp_set_cmdline("log file", logfile);
59                 talloc_free(logfile);
60                 return;
61         }
62
63         switch(opt->val) {
64         case 'd':
65                 lp_set_cmdline("log level", arg);
66                 break;
67
68         case 'V':
69                 printf( "Version %s\n", SAMBA_VERSION_STRING );
70                 exit(0);
71                 break;
72
73         case 'O':
74                 if (arg) {
75                         lp_set_cmdline("socket options", arg);
76                 }
77                 break;
78
79         case 's':
80                 if (arg) {
81                         pstrcpy(dyn_CONFIGFILE, arg);
82                 }
83                 break;
84
85         case 'l':
86                 if (arg) {
87                         char *logfile = talloc_asprintf(NULL, "%s/log.%s", arg, pname);
88                         lp_set_cmdline("log file", logfile);
89                         talloc_free(logfile);
90                 }
91                 break;
92                 
93         case 'W':
94                 lp_set_cmdline("workgroup", arg);
95                 break;
96                 
97         case 'n':
98                 lp_set_cmdline("netbios name", arg);
99                 break;
100                 
101         case 'i':
102                 lp_set_cmdline("netbios scope", arg);
103                 break;
104
105         case 'm':
106                 lp_set_cmdline("max protocol", arg);
107                 break;
108
109         case 'R':
110                 lp_set_cmdline("name resolve order", arg);
111                 break;
112
113         case OPT_OPTION:
114                 if (!lp_set_option(arg)) {
115                         fprintf(stderr, "Error setting option '%s'\n", arg);
116                         exit(1);
117                 }
118                 break;
119
120         case OPT_LEAK_REPORT:
121                 talloc_enable_leak_report();
122                 break;
123
124         case OPT_LEAK_REPORT_FULL:
125                 talloc_enable_leak_report_full();
126                 break;
127         }
128 }
129
130 struct poptOption popt_common_connection[] = {
131         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
132         { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
133         { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
134         { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
135         { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
136         { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
137         { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
138         POPT_TABLEEND
139 };
140
141 struct poptOption popt_common_samba[] = {
142         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
143         { "debuglevel",   'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
144         { "configfile",   's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
145         { "option",         0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
146         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
147         { "leak-report",     0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT, "enable talloc leak reporting on exit", NULL },   
148         { "leak-report-full",0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT_FULL, "enable full talloc leak reporting on exit", NULL },
149         POPT_TABLEEND
150 };
151
152 struct poptOption popt_common_version[] = {
153         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
154         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
155         POPT_TABLEEND
156 };
157
158
159
160 /****************************************************************************
161  * get a password from a a file or file descriptor
162  * exit on failure
163  * ****************************************************************************/
164 static void get_password_file(struct cmdline_auth_info *a)
165 {
166         int fd = -1;
167         char *p;
168         BOOL close_it = False;
169         pstring spec;
170         char pass[128];
171
172         if ((p = getenv("PASSWD_FD")) != NULL) {
173                 pstrcpy(spec, "descriptor ");
174                 pstrcat(spec, p);
175                 sscanf(p, "%d", &fd);
176                 close_it = False;
177         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
178                 fd = open(p, O_RDONLY, 0);
179                 pstrcpy(spec, p);
180                 if (fd < 0) {
181                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
182                                         spec, strerror(errno));
183                         exit(1);
184                 }
185                 close_it = True;
186         }
187
188         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
189                 p && p - pass < sizeof(pass);) {
190                 switch (read(fd, p, 1)) {
191                 case 1:
192                         if (*p != '\n' && *p != '\0') {
193                                 *++p = '\0'; /* advance p, and null-terminate pass */
194                                 break;
195                         }
196                 case 0:
197                         if (p - pass) {
198                                 *p = '\0'; /* null-terminate it, just in case... */
199                                 p = NULL; /* then force the loop condition to become false */
200                                 break;
201                         } else {
202                                 fprintf(stderr, "Error reading password from file %s: %s\n",
203                                                 spec, "empty password\n");
204                                 exit(1);
205                         }
206
207                 default:
208                         fprintf(stderr, "Error reading password from file %s: %s\n",
209                                         spec, strerror(errno));
210                         exit(1);
211                 }
212         }
213         pstrcpy(a->password, pass);
214         if (close_it)
215                 close(fd);
216 }
217
218 static void get_credentials_file(const char *file, struct cmdline_auth_info *info) 
219 {
220         XFILE *auth;
221         fstring buf;
222         uint16_t len = 0;
223         char *ptr, *val, *param;
224
225         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
226         {
227                 /* fail if we can't open the credentials file */
228                 d_printf("ERROR: Unable to open credentials file!\n");
229                 exit(-1);
230         }
231
232         while (!x_feof(auth))
233         {
234                 /* get a line from the file */
235                 if (!x_fgets(buf, sizeof(buf), auth))
236                         continue;
237                 len = strlen(buf);
238
239                 if ((len) && (buf[len-1]=='\n'))
240                 {
241                         buf[len-1] = '\0';
242                         len--;
243                 }
244                 if (len == 0)
245                         continue;
246
247                 /* break up the line into parameter & value.
248                  * will need to eat a little whitespace possibly */
249                 param = buf;
250                 if (!(ptr = strchr_m (buf, '=')))
251                         continue;
252
253                 val = ptr+1;
254                 *ptr = '\0';
255
256                 /* eat leading white space */
257                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
258                         val++;
259
260                 if (strwicmp("password", param) == 0) {
261                         pstrcpy(info->password, val);
262                         info->got_pass = True;
263                 } else if (strwicmp("username", param) == 0) {
264                         pstrcpy(info->username, val);
265                 } else if (strwicmp("domain", param) == 0) {
266                         pstrcpy(info->domain,val);
267                         info->got_domain = True;
268                 }
269                 memset(buf, 0, sizeof(buf));
270         }
271         x_fclose(auth);
272 }
273
274 /* Handle command line options:
275  *              -U,--user
276  *              -A,--authentication-file
277  *              -k,--use-kerberos
278  *              -N,--no-pass
279  *              -S,--signing
280  *              -P --machine-pass
281  */
282
283
284 static void popt_common_credentials_callback(poptContext con, 
285                                                 enum poptCallbackReason reason,
286                                                 const struct poptOption *opt,
287                                                 const char *arg, const void *data)
288 {
289         char *p;
290
291         if (reason == POPT_CALLBACK_REASON_PRE) {
292                 cmdline_auth_info.use_kerberos = False;
293                 cmdline_auth_info.got_pass = False;
294                 pstrcpy(cmdline_auth_info.username, "GUEST");   
295
296                 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
297
298                 if (getenv("USER")) {
299                         pstring tmp;
300
301                         pstrcpy(cmdline_auth_info.username,getenv("USER"));
302
303                         pstrcpy(tmp,cmdline_auth_info.username);
304                         if ((p = strchr_m(tmp,'\\'))) {
305                                 *p = 0;
306                                 pstrcpy(cmdline_auth_info.domain,tmp);
307                                 cmdline_auth_info.got_domain = True;
308                                 pstrcpy(cmdline_auth_info.username,p+1);
309                         }
310
311                         if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
312                                 *p = 0;
313                                 pstrcpy(cmdline_auth_info.password,p+1);
314                                 cmdline_auth_info.got_pass = True;
315                                 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
316                         }
317                 }
318
319                 if (getenv("DOMAIN")) {
320                         pstrcpy(cmdline_auth_info.domain,getenv("DOMAIN"));
321                         cmdline_auth_info.got_domain = True;
322                 }
323
324                 if (getenv("PASSWD")) {
325                         pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
326                         cmdline_auth_info.got_pass = True;
327                 }
328
329                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
330                         get_password_file(&cmdline_auth_info);
331                         cmdline_auth_info.got_pass = True;
332                 }
333
334                 return;
335         }
336
337         switch(opt->val) {
338         case 'U':
339                 {
340                         char *lp;
341                         pstring tmp;
342
343                         pstrcpy(cmdline_auth_info.username,arg);
344
345                         pstrcpy(tmp,cmdline_auth_info.username);
346                         if ((p = strchr_m(tmp,'\\'))) {
347                                 *p = 0;
348                                 pstrcpy(cmdline_auth_info.domain,tmp);
349                                 cmdline_auth_info.got_domain = True;
350                                 pstrcpy(cmdline_auth_info.username,p+1);
351                         }
352
353                         if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
354                                 *lp = 0;
355                                 pstrcpy(cmdline_auth_info.password,lp+1);
356                                 cmdline_auth_info.got_pass = True;
357                                 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
358                         }
359                 }
360                 break;
361
362         case 'A':
363                 get_credentials_file(arg, &cmdline_auth_info);
364                 break;
365
366         case 'k':
367 #ifndef HAVE_KRB5
368                 d_printf("No kerberos support compiled in\n");
369                 exit(1);
370 #else
371                 cmdline_auth_info.use_kerberos = True;
372                 cmdline_auth_info.got_pass = True;
373 #endif
374                 break;
375
376         case 'S':
377                 lp_set_cmdline("client signing", arg);
378                 break;
379
380         case 'P':
381                 {
382                         char *opt_password = NULL;
383                         /* it is very useful to be able to make ads queries as the
384                            machine account for testing purposes and for domain leave */
385                         
386                         if (!secrets_init()) {
387                                 d_printf("ERROR: Unable to open secrets database\n");
388                                 exit(1);
389                         }
390                         
391                         opt_password = secrets_fetch_machine_password(lp_workgroup());
392                         
393                         if (!opt_password) {
394                                 d_printf("ERROR: Unable to fetch machine password\n");
395                                 exit(1);
396                         }
397                         snprintf(cmdline_auth_info.username, sizeof(cmdline_auth_info.username), 
398                                  "%s$", lp_netbios_name());
399                         pstrcpy(cmdline_auth_info.password,opt_password);
400                         SAFE_FREE(opt_password);
401                         cmdline_auth_info.got_pass = True;
402
403                         pstrcpy(cmdline_auth_info.domain, lp_workgroup());
404                         cmdline_auth_info.got_domain = True;
405                         
406                         /* machine accounts only work with kerberos */
407                         cmdline_auth_info.use_kerberos = True;
408                 }
409                 break;
410         }
411 }
412
413
414
415 struct poptOption popt_common_credentials[] = {
416         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
417         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
418         { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
419         { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
420         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
421         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
422         { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
423         POPT_TABLEEND
424 };
425
426 void cmdline_set_username(const char *name)
427 {
428         pstrcpy(cmdline_auth_info.username, name);
429 }
430
431 const char *cmdline_get_username(void)
432 {
433         return cmdline_auth_info.username;
434 }
435
436 void cmdline_set_userdomain(const char *domain)
437 {
438         cmdline_auth_info.got_domain = True;
439         pstrcpy(cmdline_auth_info.domain, domain);
440 }
441
442 const char *cmdline_get_userdomain(void)
443 {
444         if (cmdline_auth_info.got_domain) {
445                 return cmdline_auth_info.domain;
446         }
447
448         /* I think this should be lp_netbios_name() 
449          * instead of lp_workgroup(), because if you're logged in 
450          * as domain user the getenv("USER") contains the domain
451          * and this code path isn't used
452          * --metze
453          */
454         return lp_netbios_name();
455 }
456
457 const char *cmdline_get_userpassword(void)
458 {
459         char *prompt;
460         char *ret;
461
462         if (cmdline_auth_info.got_pass) {
463                 return cmdline_auth_info.password;
464         }
465
466         prompt = talloc_asprintf(NULL, "Password for [%s\\%s]:", 
467                                  cmdline_get_userdomain(),
468                                  cmdline_get_username());
469
470         ret = getpass(prompt);
471
472         talloc_free(prompt);
473         return ret;
474 }
475
476 void cmdline_set_userpassword(const char *pass)
477 {
478         cmdline_auth_info.got_pass = True;
479         pstrcpy(cmdline_auth_info.password, pass);
480 }
481
482 void cmdline_set_use_kerberos(BOOL use_kerberos)
483 {
484         cmdline_auth_info.use_kerberos = use_kerberos;
485 }
486
487 BOOL cmdline_get_use_kerberos(void)
488 {
489         return cmdline_auth_info.use_kerberos;
490 }