r10364: Turn gensec:gssapi on by default, except for a login of the form
[bbaumbach/samba-autobuild/.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,2005
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 "version.h"
25 #include "system/filesys.h"
26 #include "system/passwd.h"
27 #include "lib/cmdline/popt_common.h"
28
29 /* Handle command line options:
30  *              -d,--debuglevel 
31  *              -s,--configfile 
32  *              -O,--socket-options 
33  *              -V,--version
34  *              -l,--log-base
35  *              -n,--netbios-name
36  *              -W,--workgroup
37  *              -i,--scope
38  */
39
40 enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL, OPT_DEBUG_STDERR};
41
42 struct cli_credentials *cmdline_credentials = NULL;
43
44 static void popt_common_callback(poptContext con, 
45                            enum poptCallbackReason reason,
46                            const struct poptOption *opt,
47                            const char *arg, const void *data)
48 {
49         const char *pname;
50
51         if (reason == POPT_CALLBACK_REASON_POST) {
52                 /* Hook any 'every Samba program must do this, after
53                  * the smb.conf is setup' functions here */
54                 lp_load();
55                 load_interfaces();
56                 return;
57         }
58
59         /* Find out basename of current program */
60         pname = strrchr_m(poptGetInvocationName(con),'/');
61
62         if (!pname)
63                 pname = poptGetInvocationName(con);
64         else 
65                 pname++;
66
67         if (reason == POPT_CALLBACK_REASON_PRE) {
68                 /* setup for panics */
69                 fault_setup(poptGetInvocationName(con));
70
71                 /* and logging */
72                 setup_logging(pname, DEBUG_STDOUT);
73                 return;
74         }
75
76         switch(opt->val) {
77         case 'd':
78                 lp_set_cmdline("log level", arg);
79                 break;
80
81         case OPT_DEBUG_STDERR:
82                 setup_logging(pname, DEBUG_STDERR);
83                 break;
84
85         case 'V':
86                 printf( "Version %s\n", SAMBA_VERSION_STRING );
87                 exit(0);
88                 break;
89
90         case 'O':
91                 if (arg) {
92                         lp_set_cmdline("socket options", arg);
93                 }
94                 break;
95
96         case 's':
97                 if (arg) {
98                         lp_set_cmdline("config file", arg);
99                 }
100                 break;
101
102         case 'l':
103                 if (arg) {
104                         char *logfile = talloc_asprintf(NULL, "%s/log.%s", arg, pname);
105                         lp_set_cmdline("log file", logfile);
106                         talloc_free(logfile);
107                 }
108                 break;
109                 
110         case 'W':
111                 lp_set_cmdline("workgroup", arg);
112                 break;
113                 
114         case 'n':
115                 lp_set_cmdline("netbios name", arg);
116                 break;
117                 
118         case 'i':
119                 lp_set_cmdline("netbios scope", arg);
120                 break;
121
122         case 'm':
123                 lp_set_cmdline("max protocol", arg);
124                 break;
125
126         case 'R':
127                 lp_set_cmdline("name resolve order", arg);
128                 break;
129
130         case OPT_OPTION:
131                 if (!lp_set_option(arg)) {
132                         fprintf(stderr, "Error setting option '%s'\n", arg);
133                         exit(1);
134                 }
135                 break;
136
137         case OPT_LEAK_REPORT:
138                 talloc_enable_leak_report();
139                 break;
140
141         case OPT_LEAK_REPORT_FULL:
142                 talloc_enable_leak_report_full();
143                 break;
144
145         }
146 }
147
148 struct poptOption popt_common_connection[] = {
149         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
150         { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
151         { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
152         { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
153         { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
154         { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
155         { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
156         POPT_TABLEEND
157 };
158
159 struct poptOption popt_common_samba[] = {
160         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, popt_common_callback },
161         { "debuglevel",   'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
162         { "debug-stderr", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_STDERR, "Send debug output to STDERR", NULL },
163         { "configfile",   's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
164         { "option",         0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
165         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
166         { "leak-report",     0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT, "enable talloc leak reporting on exit", NULL },   
167         { "leak-report-full",0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT_FULL, "enable full talloc leak reporting on exit", NULL },
168         POPT_TABLEEND
169 };
170
171 struct poptOption popt_common_version[] = {
172         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
173         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
174         POPT_TABLEEND
175 };
176
177 /* Handle command line options:
178  *              -U,--user
179  *              -A,--authentication-file
180  *              -k,--use-kerberos
181  *              -N,--no-pass
182  *              -S,--signing
183  *      -P --machine-pass
184  */
185
186
187 static BOOL dont_ask;
188
189 /*
190   disable asking for a password
191 */
192 void popt_common_dont_ask(void)
193 {
194         dont_ask = True;
195 }
196
197 static void popt_common_credentials_callback(poptContext con, 
198                                                 enum poptCallbackReason reason,
199                                                 const struct poptOption *opt,
200                                                 const char *arg, const void *data)
201 {
202         if (reason == POPT_CALLBACK_REASON_PRE) {
203                 cmdline_credentials = cli_credentials_init(talloc_autofree_context());
204                 return;
205         }
206         
207         if (reason == POPT_CALLBACK_REASON_POST) {
208                 cli_credentials_guess(cmdline_credentials);
209
210                 if (!dont_ask) {
211                         cli_credentials_set_cmdline_callbacks(cmdline_credentials);
212                 }
213                 return;
214         }
215
216         switch(opt->val) {
217         case 'U':
218                 {
219                         char *lp;
220
221                         cli_credentials_parse_string(cmdline_credentials, arg, CRED_SPECIFIED);
222                         /* This breaks the abstraction, including the const above */
223                         if ((lp=strchr_m(arg,'%'))) {
224                                 lp[0]='\0';
225                                 lp++;
226                                 memset(lp,0,strlen(lp));
227                         }
228                 }
229                 break;
230
231         case 'A':
232                 cli_credentials_parse_file(cmdline_credentials, arg, CRED_SPECIFIED);
233                 break;
234
235         case 'S':
236                 lp_set_cmdline("client signing", arg);
237                 break;
238
239         case 'P':
240                 /* Later, after this is all over, get the machine account details from the secrets.ldb */
241                 cli_credentials_set_machine_account_pending(cmdline_credentials);
242                 
243                 /* machine accounts only work with kerberos (fall though)*/
244                 break;
245         }
246 }
247
248
249
250 struct poptOption popt_common_credentials[] = {
251         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, popt_common_credentials_callback },
252         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN\\]USERNAME[%PASSWORD]" },
253         { "no-pass", 'N', POPT_ARG_NONE, &dont_ask, True, "Don't ask for a password" },
254         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
255         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
256         { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password (implies -k)" },
257         POPT_TABLEEND
258 };