lib: Remove "struct sid_parse_ret" again
[bbaumbach/samba-autobuild/.git] / source3 / lib / 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    Copyright (C) James Peach 2006
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "lib/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  *              -i,--scope
36  */
37
38 enum {OPT_OPTION=1};
39
40 extern bool override_logfile;
41
42 static void set_logfile(poptContext con, const char * arg)
43 {
44
45         char lfile[PATH_MAX];
46         const char *pname;
47         int ret;
48
49         /* Find out basename of current program */
50         pname = strrchr_m(poptGetInvocationName(con), '/');
51         if (pname == NULL) {
52                 pname = poptGetInvocationName(con);
53         } else {
54                 pname++;
55         }
56
57         ret = snprintf(lfile, sizeof(lfile), "%s/log.%s", arg, pname);
58         if (ret >= sizeof(lfile)) {
59                 return;
60         }
61         lp_set_logfile(lfile);
62 }
63
64 static bool PrintSambaVersionString;
65
66 static void popt_s3_talloc_log_fn(const char *message)
67 {
68         DEBUG(0,("%s", message));
69 }
70
71 static void popt_common_callback(poptContext con,
72                            enum poptCallbackReason reason,
73                            const struct poptOption *opt,
74                            const char *arg, const void *data)
75 {
76         TALLOC_CTX *mem_ctx = talloc_new(NULL);
77         if (mem_ctx == NULL) {
78                 exit(1);
79         }
80
81         if (reason == POPT_CALLBACK_REASON_PRE) {
82                 set_logfile(con, get_dyn_LOGFILEBASE());
83                 talloc_set_log_fn(popt_s3_talloc_log_fn);
84                 talloc_set_abort_fn(smb_panic);
85                 talloc_free(mem_ctx);
86                 return;
87         }
88
89         if (reason == POPT_CALLBACK_REASON_POST) {
90
91                 if (PrintSambaVersionString) {
92                         printf( "Version %s\n", samba_version_string());
93                         talloc_free(mem_ctx);
94                         exit(0);
95                 }
96
97                 if (is_default_dyn_CONFIGFILE()) {
98                         if (getenv("SMB_CONF_PATH")) {
99                                 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
100                         }
101                 }
102
103                 if (override_logfile) {
104                         char *logfile = lp_logfile(mem_ctx);
105                         if (logfile == NULL) {
106                                 talloc_free(mem_ctx);
107                                 exit(1);
108                         }
109                         setup_logging(logfile, DEBUG_FILE);
110                 }
111
112                 /* Further 'every Samba program must do this' hooks here. */
113                 talloc_free(mem_ctx);
114                 return;
115         }
116
117         switch(opt->val) {
118         case OPT_OPTION:
119         {
120                 struct loadparm_context *lp_ctx;
121                 bool ok;
122
123                 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
124                 if (lp_ctx == NULL) {
125                         fprintf(stderr, "loadparm_init_s3() failed!\n");
126                         talloc_free(mem_ctx);
127                         exit(1);
128                 }
129
130                 ok = lpcfg_set_option(lp_ctx, arg);
131                 if (!ok) {
132                         fprintf(stderr, "Error setting option '%s'\n", arg);
133                         talloc_free(mem_ctx);
134                         exit(1);
135                 }
136                 break;
137         }
138         case 'd':
139                 if (arg) {
140                         lp_set_cmdline("log level", arg);
141                 }
142                 break;
143
144         case 'V':
145                 PrintSambaVersionString = True;
146                 break;
147
148         case 'O':
149                 if (arg) {
150                         lp_set_cmdline("socket options", arg);
151                 }
152                 break;
153
154         case 's':
155                 if (arg) {
156                         set_dyn_CONFIGFILE(arg);
157                 }
158                 break;
159
160         case 'n':
161                 if (arg) {
162                         lp_set_cmdline("netbios name", arg);
163                 }
164                 break;
165
166         case 'l':
167                 if (arg) {
168                         set_logfile(con, arg);
169                         override_logfile = True;
170                         set_dyn_LOGFILEBASE(arg);
171                 }
172                 break;
173
174         case 'i':
175                 if (arg) {
176                         lp_set_cmdline("netbios scope", arg);
177                 }
178                 break;
179
180         case 'W':
181                 if (arg) {
182                         lp_set_cmdline("workgroup", arg);
183                 }
184                 break;
185         }
186
187         talloc_free(mem_ctx);
188 }
189
190 struct poptOption popt_common_connection[] = {
191         {
192                 .argInfo    = POPT_ARG_CALLBACK,
193                 .arg        = (void *)popt_common_callback,
194         },
195         {
196                 .longName   = "socket-options",
197                 .shortName  = 'O',
198                 .argInfo    = POPT_ARG_STRING,
199                 .val        = 'O',
200                 .descrip    = "socket options to use",
201                 .argDescrip = "SOCKETOPTIONS",
202         },
203         {
204                 .longName   = "netbiosname",
205                 .shortName  = 'n',
206                 .argInfo    = POPT_ARG_STRING,
207                 .val        = 'n',
208                 .descrip    = "Primary netbios name",
209                 .argDescrip = "NETBIOSNAME"
210         },
211         {
212                 .longName   = "workgroup",
213                 .shortName  = 'W',
214                 .argInfo    = POPT_ARG_STRING,
215                 .val        = 'W',
216                 .descrip    = "Set the workgroup name",
217                 .argDescrip = "WORKGROUP"
218         },
219         {
220                 .longName   = "scope",
221                 .shortName  = 'i',
222                 .argInfo    = POPT_ARG_STRING,
223                 .val        = 'i',
224                 .descrip    = "Use this Netbios scope",
225                 .argDescrip = "SCOPE"
226         },
227         POPT_TABLEEND
228 };
229
230 struct poptOption popt_common_samba[] = {
231         {
232                 .argInfo    = POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
233                 .arg        = (void *)popt_common_callback,
234         },
235         {
236                 .longName   = "debuglevel",
237                 .shortName  = 'd',
238                 .argInfo    = POPT_ARG_STRING,
239                 .val        = 'd',
240                 .descrip    = "Set debug level",
241                 .argDescrip = "DEBUGLEVEL",
242         },
243         {
244                 .longName   = "configfile",
245                 .shortName  = 's',
246                 .argInfo    = POPT_ARG_STRING,
247                 .val        = 's',
248                 .descrip    = "Use alternate configuration file",
249                 .argDescrip = "CONFIGFILE",
250         },
251         {
252                 .longName   = "log-basename",
253                 .shortName  = 'l',
254                 .argInfo    = POPT_ARG_STRING,
255                 .val        = 'l',
256                 .descrip    = "Base name for log files",
257                 .argDescrip = "LOGFILEBASE",
258         },
259         {
260                 .longName   = "version",
261                 .shortName  = 'V',
262                 .argInfo    = POPT_ARG_NONE,
263                 .val        = 'V',
264                 .descrip    = "Print version",
265         },
266         {
267                 .longName   = "option",
268                 .argInfo    = POPT_ARG_STRING,
269                 .val        = OPT_OPTION,
270                 .descrip    = "Set smb.conf option from command line",
271                 .argDescrip = "name=value",
272         },
273         POPT_TABLEEND
274 };
275
276 struct poptOption popt_common_configfile[] = {
277         {
278                 .argInfo    = POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
279                 .arg        = (void *)popt_common_callback,
280         },
281         {
282                 .longName   = "configfile",
283                 .argInfo    = POPT_ARG_STRING,
284                 .val        = 's',
285                 .descrip    = "Use alternate configuration file",
286                 .argDescrip = "CONFIGFILE",
287         },
288         POPT_TABLEEND
289 };
290
291 struct poptOption popt_common_version[] = {
292         {
293                 .argInfo    = POPT_ARG_CALLBACK|POPT_CBFLAG_POST,
294                 .arg        = (void *)popt_common_callback
295         },
296         {
297                 .longName   = "version",
298                 .shortName  = 'V',
299                 .argInfo    = POPT_ARG_NONE,
300                 .val        = 'V',
301                 .descrip    = "Print version",
302         },
303         POPT_TABLEEND
304 };
305
306 struct poptOption popt_common_debuglevel[] = {
307         {
308                 .argInfo    = POPT_ARG_CALLBACK,
309                 .arg        = (void *)popt_common_callback,
310         },
311         {
312                 .longName   = "debuglevel",
313                 .shortName  = 'd',
314                 .argInfo    = POPT_ARG_STRING,
315                 .val        = 'd',
316                 .descrip    = "Set debug level",
317                 .argDescrip = "DEBUGLEVEL",
318         },
319         POPT_TABLEEND
320 };
321
322 struct poptOption popt_common_option[] = {
323         {
324                 .argInfo    = POPT_ARG_CALLBACK|POPT_CBFLAG_POST,
325                 .arg        = (void *)popt_common_callback,
326         },
327         {
328                 .longName   = "option",
329                 .argInfo    = POPT_ARG_STRING,
330                 .val        = OPT_OPTION,
331                 .descrip    = "Set smb.conf option from command line",
332                 .argDescrip = "name=value",
333         },
334         POPT_TABLEEND
335 };