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