r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / lib / popt_common.c
blobefffd3c11b4dc461fe4aa1bc4fd42898654a66f9
1 /*
2 Unix SMB/CIFS implementation.
3 Common popt routines
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
7 Copyright (C) James Peach 2006
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.
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.
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/>.
23 #include "includes.h"
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
36 extern pstring user_socket_options;
37 extern BOOL AllowDebugChange;
38 extern BOOL override_logfile;
40 struct user_auth_info cmdline_auth_info;
42 static void set_logfile(poptContext con, const char * arg)
45 pstring logfile;
46 const char *pname;
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
51 if (!pname)
52 pname = poptGetInvocationName(con);
53 else
54 pname++;
56 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
57 lp_set_logfile(logfile);
60 static void popt_common_callback(poptContext con,
61 enum poptCallbackReason reason,
62 const struct poptOption *opt,
63 const char *arg, const void *data)
66 if (reason == POPT_CALLBACK_REASON_PRE) {
67 set_logfile(con, dyn_LOGFILEBASE);
68 return;
71 switch(opt->val) {
72 case 'd':
73 if (arg) {
74 debug_parse_levels(arg);
75 AllowDebugChange = False;
77 break;
79 case 'V':
80 printf( "Version %s\n", SAMBA_VERSION_STRING);
81 exit(0);
82 break;
84 case 'O':
85 if (arg) {
86 pstrcpy(user_socket_options,arg);
88 break;
90 case 's':
91 if (arg) {
92 pstrcpy(dyn_CONFIGFILE, arg);
94 break;
96 case 'n':
97 if (arg) {
98 set_global_myname(arg);
100 break;
102 case 'l':
103 if (arg) {
104 set_logfile(con, arg);
105 override_logfile = True;
106 pstr_sprintf(dyn_LOGFILEBASE, "%s", arg);
108 break;
110 case 'i':
111 if (arg) {
112 set_global_scope(arg);
114 break;
116 case 'W':
117 if (arg) {
118 set_global_myworkgroup(arg);
120 break;
124 struct poptOption popt_common_connection[] = {
125 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
126 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
127 "SOCKETOPTIONS" },
128 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
129 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
130 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
132 POPT_TABLEEND
135 struct poptOption popt_common_samba[] = {
136 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_callback },
137 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
138 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
139 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
140 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
141 POPT_TABLEEND
144 struct poptOption popt_common_version[] = {
145 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
146 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
147 POPT_TABLEEND
151 /* Handle command line options:
152 * --sbindir
153 * --bindir
154 * --swatdir
155 * --lmhostsfile
156 * --libdir
157 * --shlibext
158 * --lockdir
159 * --piddir
160 * --smb-passwd-file
161 * --private-dir
164 enum dyn_item{
165 DYN_SBINDIR = 1,
166 DYN_BINDIR,
167 DYN_SWATDIR,
168 DYN_LMHOSTSFILE,
169 DYN_LIBDIR,
170 DYN_SHLIBEXT,
171 DYN_LOCKDIR,
172 DYN_PIDDIR,
173 DYN_SMB_PASSWD_FILE,
174 DYN_PRIVATE_DIR,
178 static void popt_dynconfig_callback(poptContext con,
179 enum poptCallbackReason reason,
180 const struct poptOption *opt,
181 const char *arg, const void *data)
184 switch (opt->val) {
185 case DYN_SBINDIR:
186 if (arg) {
187 dyn_SBINDIR = SMB_STRDUP(arg);
189 break;
191 case DYN_BINDIR:
192 if (arg) {
193 dyn_BINDIR = SMB_STRDUP(arg);
195 break;
197 case DYN_SWATDIR:
198 if (arg) {
199 dyn_SWATDIR = SMB_STRDUP(arg);
201 break;
203 case DYN_LMHOSTSFILE:
204 if (arg) {
205 pstrcpy(dyn_LMHOSTSFILE, arg);
207 break;
209 case DYN_LIBDIR:
210 if (arg) {
211 pstrcpy(dyn_LIBDIR, arg);
213 break;
215 case DYN_SHLIBEXT:
216 if (arg) {
217 fstrcpy(dyn_SHLIBEXT, arg);
219 break;
221 case DYN_LOCKDIR:
222 if (arg) {
223 pstrcpy(dyn_LOCKDIR, arg);
225 break;
227 case DYN_PIDDIR:
228 if (arg) {
229 pstrcpy(dyn_PIDDIR, arg);
231 break;
233 case DYN_SMB_PASSWD_FILE:
234 if (arg) {
235 pstrcpy(dyn_SMB_PASSWD_FILE, arg);
237 break;
239 case DYN_PRIVATE_DIR:
240 if (arg) {
241 pstrcpy(dyn_PRIVATE_DIR, arg);
243 break;
248 const struct poptOption popt_common_dynconfig[] = {
250 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
252 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
253 "Path to sbin directory", "SBINDIR" },
254 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
255 "Path to bin directory", "BINDIR" },
256 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
257 "Path to SWAT installation directory", "SWATDIR" },
258 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
259 "Path to lmhosts file", "LMHOSTSFILE" },
260 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
261 "Path to shared library directory", "LIBDIR" },
262 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
263 "Shared library extension", "SHLIBEXT" },
264 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
265 "Path to lock file directory", "LOCKDIR" },
266 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
267 "Path to PID file directory", "PIDDIR" },
268 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
269 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
270 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
271 "Path to private data directory", "PRIVATE_DIR" },
273 POPT_TABLEEND
276 /****************************************************************************
277 * get a password from a a file or file descriptor
278 * exit on failure
279 * ****************************************************************************/
280 static void get_password_file(struct user_auth_info *a)
282 int fd = -1;
283 char *p;
284 BOOL close_it = False;
285 pstring spec;
286 char pass[128];
288 if ((p = getenv("PASSWD_FD")) != NULL) {
289 pstrcpy(spec, "descriptor ");
290 pstrcat(spec, p);
291 sscanf(p, "%d", &fd);
292 close_it = False;
293 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
294 fd = sys_open(p, O_RDONLY, 0);
295 pstrcpy(spec, p);
296 if (fd < 0) {
297 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
298 spec, strerror(errno));
299 exit(1);
301 close_it = True;
304 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
305 p && p - pass < sizeof(pass);) {
306 switch (read(fd, p, 1)) {
307 case 1:
308 if (*p != '\n' && *p != '\0') {
309 *++p = '\0'; /* advance p, and null-terminate pass */
310 break;
312 case 0:
313 if (p - pass) {
314 *p = '\0'; /* null-terminate it, just in case... */
315 p = NULL; /* then force the loop condition to become false */
316 break;
317 } else {
318 fprintf(stderr, "Error reading password from file %s: %s\n",
319 spec, "empty password\n");
320 exit(1);
323 default:
324 fprintf(stderr, "Error reading password from file %s: %s\n",
325 spec, strerror(errno));
326 exit(1);
329 pstrcpy(a->password, pass);
330 if (close_it)
331 close(fd);
334 static void get_credentials_file(const char *file, struct user_auth_info *info)
336 XFILE *auth;
337 fstring buf;
338 uint16 len = 0;
339 char *ptr, *val, *param;
341 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
343 /* fail if we can't open the credentials file */
344 d_printf("ERROR: Unable to open credentials file!\n");
345 exit(-1);
348 while (!x_feof(auth))
350 /* get a line from the file */
351 if (!x_fgets(buf, sizeof(buf), auth))
352 continue;
353 len = strlen(buf);
355 if ((len) && (buf[len-1]=='\n'))
357 buf[len-1] = '\0';
358 len--;
360 if (len == 0)
361 continue;
363 /* break up the line into parameter & value.
364 * will need to eat a little whitespace possibly */
365 param = buf;
366 if (!(ptr = strchr_m (buf, '=')))
367 continue;
369 val = ptr+1;
370 *ptr = '\0';
372 /* eat leading white space */
373 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
374 val++;
376 if (strwicmp("password", param) == 0)
378 pstrcpy(info->password, val);
379 info->got_pass = True;
381 else if (strwicmp("username", param) == 0)
382 pstrcpy(info->username, val);
383 else if (strwicmp("domain", param) == 0)
384 set_global_myworkgroup(val);
385 memset(buf, 0, sizeof(buf));
387 x_fclose(auth);
390 /* Handle command line options:
391 * -U,--user
392 * -A,--authentication-file
393 * -k,--use-kerberos
394 * -N,--no-pass
395 * -S,--signing
396 * -P --machine-pass
400 static void popt_common_credentials_callback(poptContext con,
401 enum poptCallbackReason reason,
402 const struct poptOption *opt,
403 const char *arg, const void *data)
405 char *p;
407 if (reason == POPT_CALLBACK_REASON_PRE) {
408 cmdline_auth_info.use_kerberos = False;
409 cmdline_auth_info.got_pass = False;
410 cmdline_auth_info.signing_state = Undefined;
411 pstrcpy(cmdline_auth_info.username, "GUEST");
413 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
415 if (getenv("USER")) {
416 pstrcpy(cmdline_auth_info.username,getenv("USER"));
418 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
419 *p = 0;
420 pstrcpy(cmdline_auth_info.password,p+1);
421 cmdline_auth_info.got_pass = True;
422 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
426 if (getenv("PASSWD")) {
427 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
428 cmdline_auth_info.got_pass = True;
431 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
432 get_password_file(&cmdline_auth_info);
433 cmdline_auth_info.got_pass = True;
436 return;
439 switch(opt->val) {
440 case 'U':
442 char *lp;
444 pstrcpy(cmdline_auth_info.username,arg);
445 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
446 *lp = 0;
447 pstrcpy(cmdline_auth_info.password,lp+1);
448 cmdline_auth_info.got_pass = True;
449 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
452 break;
454 case 'A':
455 get_credentials_file(arg, &cmdline_auth_info);
456 break;
458 case 'k':
459 #ifndef HAVE_KRB5
460 d_printf("No kerberos support compiled in\n");
461 exit(1);
462 #else
463 cmdline_auth_info.use_kerberos = True;
464 cmdline_auth_info.got_pass = True;
465 #endif
466 break;
468 case 'S':
470 cmdline_auth_info.signing_state = -1;
471 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
472 cmdline_auth_info.signing_state = False;
473 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
474 strequal(arg, "auto") )
475 cmdline_auth_info.signing_state = True;
476 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
477 cmdline_auth_info.signing_state = Required;
478 else {
479 fprintf(stderr, "Unknown signing option %s\n", arg );
480 exit(1);
483 break;
484 case 'P':
486 char *opt_password = NULL;
487 /* it is very useful to be able to make ads queries as the
488 machine account for testing purposes and for domain leave */
490 if (!secrets_init()) {
491 d_printf("ERROR: Unable to open secrets database\n");
492 exit(1);
495 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
497 if (!opt_password) {
498 d_printf("ERROR: Unable to fetch machine password\n");
499 exit(1);
501 pstr_sprintf(cmdline_auth_info.username, "%s$",
502 global_myname());
503 pstrcpy(cmdline_auth_info.password,opt_password);
504 SAFE_FREE(opt_password);
506 /* machine accounts only work with kerberos */
507 cmdline_auth_info.use_kerberos = True;
508 cmdline_auth_info.got_pass = True;
510 break;
516 struct poptOption popt_common_credentials[] = {
517 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
518 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
519 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
520 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
521 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
522 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
523 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
524 POPT_TABLEEND