5cd485d026c1254b22095bed78dc5cb4e09835c3
[sfrench/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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "version.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "param/param.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  *              --realm
36  *              -i,--scope
37  */
38
39 enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL,OPT_DEBUG_STDERR};
40
41 static struct cli_credentials *cmdline_credentials = NULL;
42
43 void popt_set_cmdline_credentials(struct cli_credentials *creds)
44 {
45         cmdline_credentials = creds;
46 }
47
48 struct cli_credentials *popt_get_cmdline_credentials(void)
49 {
50         return cmdline_credentials;
51 }
52
53 void popt_free_cmdline_credentials(void)
54 {
55         TALLOC_FREE(cmdline_credentials);
56 }
57
58 struct loadparm_context *cmdline_lp_ctx = NULL;
59
60 static void popt_version_callback(poptContext con,
61                            enum poptCallbackReason reason,
62                            const struct poptOption *opt,
63                            const char *arg, const void *data)
64 {
65         switch(opt->val) {
66         case 'V':
67                 printf("Version %s\n", SAMBA_VERSION_STRING );
68                 exit(0);
69         }
70 }
71
72 static void popt_s4_talloc_log_fn(const char *message)
73 {
74         DEBUG(0,("%s", message));
75 }
76
77 static void popt_samba_callback(poptContext con, 
78                            enum poptCallbackReason reason,
79                            const struct poptOption *opt,
80                            const char *arg, const void *data)
81 {
82         const char *pname;
83
84         if (reason == POPT_CALLBACK_REASON_POST) {
85                 if (lpcfg_configfile(cmdline_lp_ctx) == NULL) {
86                         lpcfg_load_default(cmdline_lp_ctx);
87                 }
88                 /* Hook any 'every Samba program must do this, after
89                  * the smb.conf is setup' functions here */
90                 return;
91         }
92
93         /* Find out basename of current program */
94         pname = strrchr_m(poptGetInvocationName(con),'/');
95
96         if (!pname)
97                 pname = poptGetInvocationName(con);
98         else 
99                 pname++;
100
101         if (reason == POPT_CALLBACK_REASON_PRE) {
102                 /* Hook for 'almost the first thing to do in a samba program' here */
103                 /* setup for panics */
104                 fault_setup();
105
106                 /* and logging */
107                 setup_logging(pname, DEBUG_DEFAULT_STDOUT);
108                 talloc_set_log_fn(popt_s4_talloc_log_fn);
109                 talloc_set_abort_fn(smb_panic);
110
111                 cmdline_lp_ctx = loadparm_init_global(false);
112                 return;
113         }
114
115         switch(opt->val) {
116
117         case OPT_LEAK_REPORT:
118                 talloc_enable_leak_report();
119                 break;
120
121         case OPT_LEAK_REPORT_FULL:
122                 talloc_enable_leak_report_full();
123                 break;
124
125         case OPT_OPTION:
126                 if (!lpcfg_set_option(cmdline_lp_ctx, arg)) {
127                         fprintf(stderr, "Error setting option '%s'\n", arg);
128                         exit(1);
129                 }
130                 break;
131
132         case 'd':
133                 lpcfg_set_cmdline(cmdline_lp_ctx, "log level", arg);
134                 break;
135
136         case OPT_DEBUG_STDERR:
137                 setup_logging(pname, DEBUG_STDERR);
138                 break;
139
140         case 's':
141                 if (arg) {
142                         lpcfg_load(cmdline_lp_ctx, arg);
143                 }
144                 break;
145
146         case 'l':
147                 if (arg) {
148                         char *new_logfile = talloc_asprintf(NULL, "%s/log.%s", arg, pname);
149                         lpcfg_set_cmdline(cmdline_lp_ctx, "log file", new_logfile);
150                         talloc_free(new_logfile);
151                 }
152                 break;
153         
154
155         }
156
157 }
158
159
160 static void popt_common_callback(poptContext con, 
161                            enum poptCallbackReason reason,
162                            const struct poptOption *opt,
163                            const char *arg, const void *data)
164 {
165         struct loadparm_context *lp_ctx = cmdline_lp_ctx;
166
167         switch(opt->val) {
168         case 'O':
169                 if (arg) {
170                         lpcfg_set_cmdline(lp_ctx, "socket options", arg);
171                 }
172                 break;
173         
174         case 'W':
175                 lpcfg_set_cmdline(lp_ctx, "workgroup", arg);
176                 break;
177
178         case 'r':
179                 lpcfg_set_cmdline(lp_ctx, "realm", arg);
180                 break;
181                 
182         case 'n':
183                 lpcfg_set_cmdline(lp_ctx, "netbios name", arg);
184                 break;
185                 
186         case 'i':
187                 lpcfg_set_cmdline(lp_ctx, "netbios scope", arg);
188                 break;
189
190         case 'm':
191                 lpcfg_set_cmdline(lp_ctx, "client max protocol", arg);
192                 break;
193
194         case 'R':
195                 lpcfg_set_cmdline(lp_ctx, "name resolve order", arg);
196                 break;
197
198         case 'S':
199                 lpcfg_set_cmdline(lp_ctx, "client signing", arg);
200                 break;
201
202         }
203 }
204
205 struct poptOption popt_common_connection4[] = {
206         {
207                 .argInfo    = POPT_ARG_CALLBACK,
208                 .arg        = (void *)popt_common_callback,
209         },
210         {
211                 .longName   = "name-resolve",
212                 .shortName  = 'R',
213                 .argInfo    = POPT_ARG_STRING,
214                 .val        = 'R',
215                 .descrip    = "Use these name resolution services only",
216                 .argDescrip = "NAME-RESOLVE-ORDER",
217         },
218         {
219                 .longName   = "socket-options",
220                 .shortName  = 'O',
221                 .argInfo    = POPT_ARG_STRING,
222                 .val        = 'O',
223                 .descrip    = "socket options to use",
224                 .argDescrip = "SOCKETOPTIONS",
225         },
226         {
227                 .longName   = "netbiosname",
228                 .shortName  = 'n',
229                 .argInfo    = POPT_ARG_STRING,
230                 .val        = 'n',
231                 .descrip    = "Primary netbios name",
232                 .argDescrip = "NETBIOSNAME",
233         },
234         {
235                 .longName   = "signing",
236                 .shortName  = 'S',
237                 .argInfo    = POPT_ARG_STRING,
238                 .val        = 'S',
239                 .descrip    = "Set the client signing state",
240                 .argDescrip = "on|off|required",
241         },
242         {
243                 .longName   = "workgroup",
244                 .shortName  = 'W',
245                 .argInfo    = POPT_ARG_STRING,
246                 .val        = 'W',
247                 .descrip    = "Set the workgroup name",
248                 .argDescrip = "WORKGROUP",
249         },
250         {
251                 .longName   = "realm",
252                 .argInfo    = POPT_ARG_STRING,
253                 .val        = 'r',
254                 .descrip    = "Set the realm name",
255                 .argDescrip = "REALM",
256         },
257         {
258                 .longName   = "scope",
259                 .shortName  = 'i',
260                 .argInfo    = POPT_ARG_STRING,
261                 .val        = 'i',
262                 .descrip    = "Use this Netbios scope",
263                 .argDescrip = "SCOPE",
264         },
265         {
266                 .longName   = "maxprotocol",
267                 .shortName  = 'm',
268                 .argInfo    = POPT_ARG_STRING,
269                 .val        = 'm',
270                 .descrip    = "Set max protocol level",
271                 .argDescrip = "MAXPROTOCOL",
272         },
273         POPT_TABLEEND
274 };
275
276 struct poptOption popt_common_samba4[] = {
277         {
278                 .argInfo    = POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
279                 .arg        = (void *)popt_samba_callback,
280         },
281         {
282                 .longName   = "debuglevel",
283                 .shortName  = 'd',
284                 .argInfo    = POPT_ARG_STRING,
285                 .val        = 'd',
286                 .descrip    = "Set debug level",
287                 .argDescrip = "DEBUGLEVEL",
288         },
289         {
290                 .longName   = "debug-stderr",
291                 .argInfo    = POPT_ARG_NONE,
292                 .val        = OPT_DEBUG_STDERR,
293                 .descrip    = "Send debug output to STDERR",
294         },
295         {
296                 .longName   = "configfile",
297                 .shortName  = 's',
298                 .argInfo    = POPT_ARG_STRING,
299                 .val        = 's',
300                 .descrip    = "Use alternative configuration file",
301                 .argDescrip = "CONFIGFILE",
302         },
303         {
304                 .longName   = "option",
305                 .argInfo    = POPT_ARG_STRING,
306                 .val        = OPT_OPTION,
307                 .descrip    = "Set smb.conf option from command line",
308                 .argDescrip = "name=value",
309         },
310         {
311                 .longName   = "log-basename",
312                 .shortName  = 'l',
313                 .argInfo    = POPT_ARG_STRING,
314                 .val        = 'l',
315                 .descrip    = "Basename for log/debug files",
316                 .argDescrip = "LOGFILEBASE",
317         },
318         {
319                 .longName   = "leak-report",
320                 .argInfo    = POPT_ARG_NONE,
321                 .val        = OPT_LEAK_REPORT,
322                 .descrip    = "enable talloc leak reporting on exit",
323         },
324         {
325                 .longName   = "leak-report-full",
326                 .argInfo    = POPT_ARG_NONE,
327                 .val        = OPT_LEAK_REPORT_FULL,
328                 .descrip    = "enable full talloc leak reporting on exit",
329         },
330         POPT_TABLEEND
331 };
332
333 struct poptOption popt_common_version4[] = {
334         {
335                 .argInfo    = POPT_ARG_CALLBACK,
336                 .arg        = (void *)popt_version_callback,
337         },
338         {
339                 .longName   = "version",
340                 .shortName  = 'V',
341                 .argInfo    = POPT_ARG_NONE,
342                 .val        = 'V',
343                 .descrip    = "Print version",
344         },
345         POPT_TABLEEND
346 };