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