source: branches/samba-3.0/source/lib/popt_common.c

Last change on this file was 367, checked in by Herwig Bauernfeind, 16 years ago

_divers os2_getExePath applied to BINDIR, SBINDIR and SWATDIR in 3.0.x

File size: 13.3 KB
Line 
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include "includes.h"
25
26/* Handle command line options:
27 * -d,--debuglevel
28 * -s,--configfile
29 * -O,--socket-options
30 * -V,--version
31 * -l,--log-base
32 * -n,--netbios-name
33 * -W,--workgroup
34 * -i,--scope
35 */
36
37extern pstring user_socket_options;
38extern BOOL AllowDebugChange;
39extern BOOL override_logfile;
40
41struct user_auth_info cmdline_auth_info;
42
43static void set_logfile(poptContext con, const char * arg)
44{
45
46 pstring logfile;
47 const char *pname;
48
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con),'/');
51
52 if (!pname)
53 pname = poptGetInvocationName(con);
54 else
55 pname++;
56
57 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
58 lp_set_logfile(logfile);
59}
60
61static void popt_common_callback(poptContext con,
62 enum poptCallbackReason reason,
63 const struct poptOption *opt,
64 const char *arg, const void *data)
65{
66
67 if (reason == POPT_CALLBACK_REASON_PRE) {
68 set_logfile(con, dyn_LOGFILEBASE);
69 return;
70 }
71
72 switch(opt->val) {
73 case 'd':
74 if (arg) {
75 debug_parse_levels(arg);
76 AllowDebugChange = False;
77 }
78 break;
79
80 case 'V':
81 printf( "Version %s\n", SAMBA_VERSION_STRING);
82 exit(0);
83 break;
84
85 case 'O':
86 if (arg) {
87 pstrcpy(user_socket_options,arg);
88 }
89 break;
90
91 case 's':
92 if (arg) {
93 pstrcpy(dyn_CONFIGFILE, arg);
94 }
95 break;
96
97 case 'n':
98 if (arg) {
99 set_global_myname(arg);
100 }
101 break;
102
103 case 'l':
104 if (arg) {
105 set_logfile(con, arg);
106 override_logfile = True;
107 pstr_sprintf(dyn_LOGFILEBASE, "%s", arg);
108 }
109 break;
110
111 case 'i':
112 if (arg) {
113 set_global_scope(arg);
114 }
115 break;
116
117 case 'W':
118 if (arg) {
119 set_global_myworkgroup(arg);
120 }
121 break;
122 }
123}
124
125struct poptOption popt_common_connection[] = {
126 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
127 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
128 "SOCKETOPTIONS" },
129 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
130 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
131 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
132
133 POPT_TABLEEND
134};
135
136struct poptOption popt_common_samba[] = {
137 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_callback },
138 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
139 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
140 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
141 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
142 POPT_TABLEEND
143};
144
145struct poptOption popt_common_version[] = {
146 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
147 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
148 POPT_TABLEEND
149};
150
151
152/* Handle command line options:
153 * --sbindir
154 * --bindir
155 * --swatdir
156 * --lmhostsfile
157 * --libdir
158 * --shlibext
159 * --lockdir
160 * --piddir
161 * --smb-passwd-file
162 * --private-dir
163 */
164
165enum dyn_item{
166 DYN_SBINDIR = 1,
167 DYN_BINDIR,
168 DYN_SWATDIR,
169 DYN_LMHOSTSFILE,
170 DYN_LIBDIR,
171 DYN_SHLIBEXT,
172 DYN_LOCKDIR,
173 DYN_PIDDIR,
174 DYN_SMB_PASSWD_FILE,
175 DYN_PRIVATE_DIR,
176};
177
178
179static void popt_dynconfig_callback(poptContext con,
180 enum poptCallbackReason reason,
181 const struct poptOption *opt,
182 const char *arg, const void *data)
183{
184
185 switch (opt->val) {
186 case DYN_SBINDIR:
187 if (arg) {
188#ifdef __OS2__
189 pstrcpy(dyn_SBINDIR, arg);
190#else
191 dyn_SBINDIR = SMB_STRDUP(arg);
192#endif
193 }
194 break;
195
196 case DYN_BINDIR:
197 if (arg) {
198#ifdef __OS2__
199 pstrcpy(dyn_BINDIR, arg);
200#else
201 dyn_BINDIR = SMB_STRDUP(arg);
202#endif
203 }
204 break;
205
206 case DYN_SWATDIR:
207 if (arg) {
208#ifdef __OS2__
209 pstrcpy(dyn_SWATDIR, arg);
210#else
211 dyn_SWATDIR = SMB_STRDUP(arg);
212#endif
213 }
214 break;
215
216 case DYN_LMHOSTSFILE:
217 if (arg) {
218 pstrcpy(dyn_LMHOSTSFILE, arg);
219 }
220 break;
221
222 case DYN_LIBDIR:
223 if (arg) {
224 pstrcpy(dyn_LIBDIR, arg);
225 }
226 break;
227
228 case DYN_SHLIBEXT:
229 if (arg) {
230 fstrcpy(dyn_SHLIBEXT, arg);
231 }
232 break;
233
234 case DYN_LOCKDIR:
235 if (arg) {
236 pstrcpy(dyn_LOCKDIR, arg);
237 }
238 break;
239
240 case DYN_PIDDIR:
241 if (arg) {
242 pstrcpy(dyn_PIDDIR, arg);
243 }
244 break;
245
246 case DYN_SMB_PASSWD_FILE:
247 if (arg) {
248 pstrcpy(dyn_SMB_PASSWD_FILE, arg);
249 }
250 break;
251
252 case DYN_PRIVATE_DIR:
253 if (arg) {
254 pstrcpy(dyn_PRIVATE_DIR, arg);
255 }
256 break;
257
258 }
259}
260
261const struct poptOption popt_common_dynconfig[] = {
262
263 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
264
265 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
266 "Path to sbin directory", "SBINDIR" },
267 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
268 "Path to bin directory", "BINDIR" },
269 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
270 "Path to SWAT installation directory", "SWATDIR" },
271 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
272 "Path to lmhosts file", "LMHOSTSFILE" },
273 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
274 "Path to shared library directory", "LIBDIR" },
275 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
276 "Shared library extension", "SHLIBEXT" },
277 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
278 "Path to lock file directory", "LOCKDIR" },
279 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
280 "Path to PID file directory", "PIDDIR" },
281 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
282 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
283 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
284 "Path to private data directory", "PRIVATE_DIR" },
285
286 POPT_TABLEEND
287};
288
289/****************************************************************************
290 * get a password from a a file or file descriptor
291 * exit on failure
292 * ****************************************************************************/
293static void get_password_file(struct user_auth_info *a)
294{
295 int fd = -1;
296 char *p;
297 BOOL close_it = False;
298 pstring spec;
299 char pass[128];
300
301 if ((p = getenv("PASSWD_FD")) != NULL) {
302 pstrcpy(spec, "descriptor ");
303 pstrcat(spec, p);
304 sscanf(p, "%d", &fd);
305 close_it = False;
306 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
307 fd = sys_open(p, O_RDONLY, 0);
308 pstrcpy(spec, p);
309 if (fd < 0) {
310 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
311 spec, strerror(errno));
312 exit(1);
313 }
314 close_it = True;
315 }
316
317 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
318 p && p - pass < sizeof(pass);) {
319 switch (read(fd, p, 1)) {
320 case 1:
321 if (*p != '\n' && *p != '\0') {
322 *++p = '\0'; /* advance p, and null-terminate pass */
323 break;
324 }
325 case 0:
326 if (p - pass) {
327 *p = '\0'; /* null-terminate it, just in case... */
328 p = NULL; /* then force the loop condition to become false */
329 break;
330 } else {
331 fprintf(stderr, "Error reading password from file %s: %s\n",
332 spec, "empty password\n");
333 exit(1);
334 }
335
336 default:
337 fprintf(stderr, "Error reading password from file %s: %s\n",
338 spec, strerror(errno));
339 exit(1);
340 }
341 }
342 pstrcpy(a->password, pass);
343 if (close_it)
344 close(fd);
345}
346
347static void get_credentials_file(const char *file, struct user_auth_info *info)
348{
349 XFILE *auth;
350 fstring buf;
351 uint16 len = 0;
352 char *ptr, *val, *param;
353
354 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
355 {
356 /* fail if we can't open the credentials file */
357 d_printf("ERROR: Unable to open credentials file!\n");
358 exit(-1);
359 }
360
361 while (!x_feof(auth))
362 {
363 /* get a line from the file */
364 if (!x_fgets(buf, sizeof(buf), auth))
365 continue;
366 len = strlen(buf);
367
368 if ((len) && (buf[len-1]=='\n'))
369 {
370 buf[len-1] = '\0';
371 len--;
372 }
373 if (len == 0)
374 continue;
375
376 /* break up the line into parameter & value.
377 * will need to eat a little whitespace possibly */
378 param = buf;
379 if (!(ptr = strchr_m (buf, '=')))
380 continue;
381
382 val = ptr+1;
383 *ptr = '\0';
384
385 /* eat leading white space */
386 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
387 val++;
388
389 if (strwicmp("password", param) == 0)
390 {
391 pstrcpy(info->password, val);
392 info->got_pass = True;
393 }
394 else if (strwicmp("username", param) == 0)
395 pstrcpy(info->username, val);
396 else if (strwicmp("domain", param) == 0)
397 set_global_myworkgroup(val);
398 memset(buf, 0, sizeof(buf));
399 }
400 x_fclose(auth);
401}
402
403/* Handle command line options:
404 * -U,--user
405 * -A,--authentication-file
406 * -k,--use-kerberos
407 * -N,--no-pass
408 * -S,--signing
409 * -P --machine-pass
410 */
411
412
413static void popt_common_credentials_callback(poptContext con,
414 enum poptCallbackReason reason,
415 const struct poptOption *opt,
416 const char *arg, const void *data)
417{
418 char *p;
419
420 if (reason == POPT_CALLBACK_REASON_PRE) {
421 cmdline_auth_info.use_kerberos = False;
422 cmdline_auth_info.got_pass = False;
423 cmdline_auth_info.signing_state = Undefined;
424 pstrcpy(cmdline_auth_info.username, "GUEST");
425
426 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
427
428 if (getenv("USER")) {
429 pstrcpy(cmdline_auth_info.username,getenv("USER"));
430
431 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
432 *p = 0;
433 pstrcpy(cmdline_auth_info.password,p+1);
434 cmdline_auth_info.got_pass = True;
435 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
436 }
437 }
438
439 if (getenv("PASSWD")) {
440 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
441 cmdline_auth_info.got_pass = True;
442 }
443
444 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
445 get_password_file(&cmdline_auth_info);
446 cmdline_auth_info.got_pass = True;
447 }
448
449 return;
450 }
451
452 switch(opt->val) {
453 case 'U':
454 {
455 char *lp;
456
457 pstrcpy(cmdline_auth_info.username,arg);
458 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
459 *lp = 0;
460 pstrcpy(cmdline_auth_info.password,lp+1);
461 cmdline_auth_info.got_pass = True;
462 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
463 }
464 }
465 break;
466
467 case 'A':
468 get_credentials_file(arg, &cmdline_auth_info);
469 break;
470
471 case 'k':
472#ifndef HAVE_KRB5
473 d_printf("No kerberos support compiled in\n");
474 exit(1);
475#else
476 cmdline_auth_info.use_kerberos = True;
477 cmdline_auth_info.got_pass = True;
478#endif
479 break;
480
481 case 'S':
482 {
483 cmdline_auth_info.signing_state = -1;
484 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
485 cmdline_auth_info.signing_state = False;
486 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
487 strequal(arg, "auto") )
488 cmdline_auth_info.signing_state = True;
489 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
490 cmdline_auth_info.signing_state = Required;
491 else {
492 fprintf(stderr, "Unknown signing option %s\n", arg );
493 exit(1);
494 }
495 }
496 break;
497 case 'P':
498 {
499 char *opt_password = NULL;
500 /* it is very useful to be able to make ads queries as the
501 machine account for testing purposes and for domain leave */
502
503 if (!secrets_init()) {
504 d_printf("ERROR: Unable to open secrets database\n");
505 exit(1);
506 }
507
508 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
509
510 if (!opt_password) {
511 d_printf("ERROR: Unable to fetch machine password\n");
512 exit(1);
513 }
514 pstr_sprintf(cmdline_auth_info.username, "%s$",
515 global_myname());
516 pstrcpy(cmdline_auth_info.password,opt_password);
517 SAFE_FREE(opt_password);
518
519 /* machine accounts only work with kerberos */
520 cmdline_auth_info.use_kerberos = True;
521 cmdline_auth_info.got_pass = True;
522 }
523 break;
524 }
525}
526
527
528
529struct poptOption popt_common_credentials[] = {
530 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
531 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
532 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
533 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
534 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
535 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
536 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
537 POPT_TABLEEND
538};
Note: See TracBrowser for help on using the repository browser.