7
7
Copyright (C) Tom Jansen (Ninja ISD) 2002
8
8
Copyright (C) Derrell Lipman 2003-2008
9
9
Copyright (C) Jeremy Allison 2007, 2008
11
11
This program is free software; you can redistribute it and/or modify
12
12
it under the terms of the GNU General Public License as published by
13
13
the Free Software Foundation; either version 3 of the License, or
14
14
(at your option) any later version.
16
16
This program is distributed in the hope that it will be useful,
17
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
19
GNU General Public License for more details.
21
21
You should have received a copy of the GNU General Public License
22
22
along with this program. If not, see <http://www.gnu.org/licenses/>.
25
25
#include "includes.h"
26
#include "libsmb/libsmb.h"
26
27
#include "libsmbclient.h"
27
28
#include "libsmb_internal.h"
43
45
bool conf_loaded = False;
45
47
TALLOC_CTX *frame = talloc_stackframe();
49
setup_logging("libsmbclient", True);
49
load_case_tables_library();
51
setup_logging("libsmbclient", DEBUG_STDOUT);
51
53
/* Here we would open the smb.conf file if needed ... */
53
55
lp_set_in_client(True);
55
57
home = getenv("HOME");
73
75
* fails, silently ignore it and use the internal
77
79
if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
78
80
DEBUG(5, ("Could not load config file: %s\n",
79
81
get_dyn_CONFIGFILE()));
101
103
load_interfaces(); /* Load the list of interfaces ... */
103
105
reopen_logs(); /* Get logging working ... */
106
108
* Block SIGPIPE (from lib/util_sock.c: write())
107
109
* It is not needed and should not stop execution
109
111
BlockSignals(True, SIGPIPE);
111
113
/* Create the mutex we'll use to protect initialized_ctx_count */
112
114
if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
113
115
initialized_ctx_count_mutex) != 0) {
153
155
ZERO_STRUCTP(context);
155
157
context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
156
158
if (!context->internal) {
157
159
SAFE_FREE(context);
162
164
/* Initialize the context and establish reasonable defaults */
163
165
ZERO_STRUCTP(context->internal);
165
167
smbc_setDebug(context, 0);
166
168
smbc_setTimeout(context, 20000);
168
170
smbc_setOptionFullTimeNames(context, False);
169
171
smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
170
172
smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
176
178
if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
177
179
smbc_setOptionUseCCache(context, true);
180
182
smbc_setFunctionAuthData(context, SMBC_get_auth_data);
181
183
smbc_setFunctionCheckServer(context, SMBC_check_server);
182
184
smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
184
186
smbc_setOptionUserData(context, NULL);
185
187
smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
186
188
smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
187
189
smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
188
190
smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
190
192
smbc_setFunctionOpen(context, SMBC_open_ctx);
191
193
smbc_setFunctionCreat(context, SMBC_creat_ctx);
192
194
smbc_setFunctionRead(context, SMBC_read_ctx);
215
217
smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
216
218
smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
217
219
smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
219
221
smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
220
222
smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
221
223
smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
222
224
smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
243
245
if (shutdown_ctx) {
245
247
DEBUG(1,("Performing aggressive shutdown.\n"));
247
249
f = context->internal->files;
249
251
smbc_getFunctionClose(context)(context, f);
252
254
context->internal->files = NULL;
254
256
/* First try to remove the servers the nice way. */
255
257
if (smbc_getFunctionPurgeCachedServers(context)(context)) {
297
299
/* Things we have to clean up */
298
300
smbc_setWorkgroup(context, NULL);
299
301
smbc_setNetbiosName(context, NULL);
300
302
smbc_setUser(context, NULL);
302
304
DEBUG(3, ("Context %p successfully freed\n", context));
304
306
/* Free any DFS auth context. */
350
352
va_start(ap, option_name);
352
354
if (strcmp(option_name, "debug_to_stderr") == 0) {
353
355
option_value.b = (bool) va_arg(ap, int);
354
356
smbc_setOptionDebugToStderr(context, option_value.b);
356
358
} else if (strcmp(option_name, "full_time_names") == 0) {
357
359
option_value.b = (bool) va_arg(ap, int);
358
360
smbc_setOptionFullTimeNames(context, option_value.b);
360
362
} else if (strcmp(option_name, "open_share_mode") == 0) {
361
363
option_value.i = va_arg(ap, int);
362
364
smbc_setOptionOpenShareMode(context, option_value.i);
364
366
} else if (strcmp(option_name, "auth_function") == 0) {
365
367
option_value.auth_fn =
366
368
va_arg(ap, smbc_get_auth_data_with_context_fn);
367
369
smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
369
371
} else if (strcmp(option_name, "user_data") == 0) {
370
372
option_value.v = va_arg(ap, void *);
371
373
smbc_setOptionUserData(context, option_value.v);
373
375
} else if (strcmp(option_name, "smb_encrypt_level") == 0) {
374
376
option_value.s = va_arg(ap, const char *);
375
377
if (strcmp(option_value.s, "none") == 0) {
382
384
smbc_setOptionSmbEncryptionLevel(context,
383
385
SMBC_ENCRYPTLEVEL_REQUIRE);
386
388
} else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
387
389
option_value.i = va_arg(ap, int);
388
390
smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
390
392
} else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
391
393
option_value.b = (bool) va_arg(ap, int);
392
394
smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
394
396
} else if (strcmp(option_name, "one_share_per_server") == 0) {
395
397
option_value.b = (bool) va_arg(ap, int);
396
398
smbc_setOptionOneSharePerServer(context, option_value.b);
398
400
} else if (strcmp(option_name, "use_kerberos") == 0) {
399
401
option_value.b = (bool) va_arg(ap, int);
400
402
smbc_setOptionUseKerberos(context, option_value.b);
402
404
} else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
403
405
option_value.b = (bool) va_arg(ap, int);
404
406
smbc_setOptionFallbackAfterKerberos(context, option_value.b);
406
408
} else if (strcmp(option_name, "use_ccache") == 0) {
407
409
option_value.b = (bool) va_arg(ap, int);
408
410
smbc_setOptionUseCCache(context, option_value.b);
431
433
return (void *) smbc_getOptionDebugToStderr(context);
434
436
} else if (strcmp(option_name, "full_time_names") == 0) {
435
437
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
436
438
return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
438
440
return (void *) smbc_getOptionFullTimeNames(context);
441
443
} else if (strcmp(option_name, "open_share_mode") == 0) {
442
444
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
443
445
return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
445
447
return (void *) smbc_getOptionOpenShareMode(context);
448
450
} else if (strcmp(option_name, "auth_function") == 0) {
449
451
return (void *) smbc_getFunctionAuthDataWithContext(context);
451
453
} else if (strcmp(option_name, "user_data") == 0) {
452
454
return smbc_getOptionUserData(context);
454
456
} else if (strcmp(option_name, "smb_encrypt_level") == 0) {
455
457
switch(smbc_getOptionSmbEncryptionLevel(context))
462
464
return (void *) "require";
465
467
} else if (strcmp(option_name, "smb_encrypt_on") == 0) {
467
469
unsigned int num_servers = 0;
469
471
for (s = context->internal->servers; s; s = s->next) {
471
473
if (s->cli->trans_enc_state == NULL) {
478
480
return (void *) (bool) (num_servers > 0);
481
483
} else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
482
484
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
483
485
return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
485
487
return (void *) smbc_getOptionBrowseMaxLmbCount(context);
488
490
} else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
489
491
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
490
492
return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
492
494
return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
495
497
} else if (strcmp(option_name, "one_share_per_server") == 0) {
496
498
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
497
499
return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
499
501
return (void *) (bool) smbc_getOptionOneSharePerServer(context);
502
504
} else if (strcmp(option_name, "use_kerberos") == 0) {
503
505
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
504
506
return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
506
508
return (void *) (bool) smbc_getOptionUseKerberos(context);
509
511
} else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
510
512
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
511
513
return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
513
515
return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
516
518
} else if (strcmp(option_name, "use_ccache") == 0) {
517
519
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
518
520
return (void *) (intptr_t) smbc_getOptionUseCCache(context);
543
545
smbc_init_context(SMBCCTX *context)
552
554
/* Do not initialise the same client twice */
553
555
if (context->internal->initialized) {
557
if (context->internal->debug_stderr) {
559
* Hmmm... Do we want a unique dbf per-thread? For now, we'll just
560
* leave it up to the user. If any one context spefies debug to
561
* stderr then all will be.
564
x_setbuf(x_stderr, NULL);
567
559
if ((!smbc_getFunctionAuthData(context) &&
568
560
!smbc_getFunctionAuthDataWithContext(context)) ||
569
561
smbc_getDebug(context) < 0 ||
570
562
smbc_getDebug(context) > 100) {
577
569
if (!smbc_getUser(context)) {
579
571
* FIXME: Is this the best way to get the user info?
581
char *user = getenv("USER");
573
char *user = getenv("USER");
582
574
/* walk around as "guest" if no username can be found */
584
576
user = SMB_STRDUP("guest");
594
586
smbc_setUser(context, user);
597
if (!smbc_getUser(context)) {
589
if (!smbc_getUser(context)) {
603
595
if (!smbc_getNetbiosName(context)) {
605
597
* We try to get our netbios name from the config. If that
668
660
DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
670
662
/* shortest timeout is 1 second */
671
663
if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
672
664
smbc_setTimeout(context, 1000);
674
666
context->internal->initialized = True;
676
668
/* Protect access to the count of contexts in use */