source: branches/samba-3.3.x/source/lib/popt_common.c

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

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

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