2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/gensec/gensec.h"
33 #include "lib/param/param.h"
34 #include "../lib/util/smb_threads.h"
35 #include "../lib/util/smb_threads_internal.h"
38 * Is the logging working / configfile read ?
40 static bool SMBC_initialized
= false;
41 static unsigned int initialized_ctx_count
= 0;
42 static void *initialized_ctx_count_mutex
= NULL
;
45 * Do some module- and library-wide initializations
48 SMBC_module_init(void * punused
)
50 bool conf_loaded
= False
;
52 TALLOC_CTX
*frame
= talloc_stackframe();
54 setup_logging("libsmbclient", DEBUG_STDOUT
);
56 /* Here we would open the smb.conf file if needed ... */
58 home
= getenv("HOME");
61 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
62 if (lp_load_client(conf
)) {
65 DEBUG(5, ("Could not load config file: %s\n",
74 * Well, if that failed, try the get_dyn_CONFIGFILE
75 * Which points to the standard locn, and if that
76 * fails, silently ignore it and use the internal
80 if (!lp_load_client(get_dyn_CONFIGFILE())) {
81 DEBUG(5, ("Could not load config file: %s\n",
82 get_dyn_CONFIGFILE()));
86 * We loaded the global config file. Now lets
87 * load user-specific modifications to the
91 "%s/.smb/smb.conf.append",
93 if (!lp_load_client_no_reinit(conf
)) {
95 ("Could not append config file: "
104 load_interfaces(); /* Load the list of interfaces ... */
106 reopen_logs(); /* Get logging working ... */
109 * Block SIGPIPE (from lib/util_sock.c: write())
110 * It is not needed and should not stop execution
112 BlockSignals(True
, SIGPIPE
);
114 /* Create the mutex we'll use to protect initialized_ctx_count */
115 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
116 initialized_ctx_count_mutex
) != 0) {
117 smb_panic("SMBC_module_init: "
118 "failed to create 'initialized_ctx_count' mutex");
126 SMBC_module_terminate(void)
128 TALLOC_CTX
*frame
= talloc_stackframe();
131 SMBC_initialized
= false;
137 * Get a new empty handle to fill in with your own info
140 smbc_new_context(void)
143 TALLOC_CTX
*frame
= talloc_stackframe();
145 /* The first call to this function should initialize the module */
146 SMB_THREAD_ONCE(&SMBC_initialized
, SMBC_module_init
, NULL
);
149 * All newly added context fields should be placed in
150 * SMBC_internal_data, not directly in SMBCCTX.
152 context
= SMB_MALLOC_P(SMBCCTX
);
159 ZERO_STRUCTP(context
);
161 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
162 if (!context
->internal
) {
169 /* Initialize the context and establish reasonable defaults */
170 ZERO_STRUCTP(context
->internal
);
172 context
->internal
->lp_ctx
= loadparm_init_s3(NULL
,
173 loadparm_s3_helpers());
174 if (context
->internal
->lp_ctx
== NULL
) {
175 SAFE_FREE(context
->internal
);
182 smbc_setDebug(context
, 0);
183 smbc_setTimeout(context
, 20000);
184 smbc_setPort(context
, 0);
186 smbc_setOptionFullTimeNames(context
, False
);
187 smbc_setOptionOpenShareMode(context
, SMBC_SHAREMODE_DENY_NONE
);
188 smbc_setOptionSmbEncryptionLevel(context
, SMBC_ENCRYPTLEVEL_DEFAULT
);
189 smbc_setOptionUseCCache(context
, True
);
190 smbc_setOptionCaseSensitive(context
, False
);
191 smbc_setOptionBrowseMaxLmbCount(context
, 3); /* # LMBs to query */
192 smbc_setOptionUrlEncodeReaddirEntries(context
, False
);
193 smbc_setOptionOneSharePerServer(context
, False
);
194 smbc_setOptionPosixExtensions(context
, false);
195 if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL
) {
196 smbc_setOptionUseCCache(context
, false);
199 smbc_setFunctionAuthData(context
, SMBC_get_auth_data
);
200 smbc_setFunctionCheckServer(context
, SMBC_check_server
);
201 smbc_setFunctionRemoveUnusedServer(context
, SMBC_remove_unused_server
);
203 smbc_setOptionUserData(context
, NULL
);
204 smbc_setFunctionAddCachedServer(context
, SMBC_add_cached_server
);
205 smbc_setFunctionGetCachedServer(context
, SMBC_get_cached_server
);
206 smbc_setFunctionRemoveCachedServer(context
, SMBC_remove_cached_server
);
207 smbc_setFunctionPurgeCachedServers(context
, SMBC_purge_cached_servers
);
209 smbc_setFunctionOpen(context
, SMBC_open_ctx
);
210 smbc_setFunctionCreat(context
, SMBC_creat_ctx
);
211 smbc_setFunctionRead(context
, SMBC_read_ctx
);
212 smbc_setFunctionSplice(context
, SMBC_splice_ctx
);
213 smbc_setFunctionWrite(context
, SMBC_write_ctx
);
214 smbc_setFunctionClose(context
, SMBC_close_ctx
);
215 smbc_setFunctionUnlink(context
, SMBC_unlink_ctx
);
216 smbc_setFunctionRename(context
, SMBC_rename_ctx
);
217 smbc_setFunctionLseek(context
, SMBC_lseek_ctx
);
218 smbc_setFunctionFtruncate(context
, SMBC_ftruncate_ctx
);
219 smbc_setFunctionStat(context
, SMBC_stat_ctx
);
220 smbc_setFunctionStatVFS(context
, SMBC_statvfs_ctx
);
221 smbc_setFunctionFstatVFS(context
, SMBC_fstatvfs_ctx
);
222 smbc_setFunctionFstat(context
, SMBC_fstat_ctx
);
223 smbc_setFunctionOpendir(context
, SMBC_opendir_ctx
);
224 smbc_setFunctionClosedir(context
, SMBC_closedir_ctx
);
225 smbc_setFunctionReaddir(context
, SMBC_readdir_ctx
);
226 smbc_setFunctionReaddirPlus(context
, SMBC_readdirplus_ctx
);
227 smbc_setFunctionReaddirPlus2(context
, SMBC_readdirplus2_ctx
);
228 smbc_setFunctionGetdents(context
, SMBC_getdents_ctx
);
229 smbc_setFunctionMkdir(context
, SMBC_mkdir_ctx
);
230 smbc_setFunctionRmdir(context
, SMBC_rmdir_ctx
);
231 smbc_setFunctionTelldir(context
, SMBC_telldir_ctx
);
232 smbc_setFunctionLseekdir(context
, SMBC_lseekdir_ctx
);
233 smbc_setFunctionFstatdir(context
, SMBC_fstatdir_ctx
);
234 smbc_setFunctionNotify(context
, SMBC_notify_ctx
);
235 smbc_setFunctionChmod(context
, SMBC_chmod_ctx
);
236 smbc_setFunctionUtimes(context
, SMBC_utimes_ctx
);
237 smbc_setFunctionSetxattr(context
, SMBC_setxattr_ctx
);
238 smbc_setFunctionGetxattr(context
, SMBC_getxattr_ctx
);
239 smbc_setFunctionRemovexattr(context
, SMBC_removexattr_ctx
);
240 smbc_setFunctionListxattr(context
, SMBC_listxattr_ctx
);
242 smbc_setFunctionOpenPrintJob(context
, SMBC_open_print_job_ctx
);
243 smbc_setFunctionPrintFile(context
, SMBC_print_file_ctx
);
244 smbc_setFunctionListPrintJobs(context
, SMBC_list_print_jobs_ctx
);
245 smbc_setFunctionUnlinkPrintJob(context
, SMBC_unlink_print_job_ctx
);
254 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
255 * and thus you'll be leaking memory if not handled properly.
259 smbc_free_context(SMBCCTX
*context
,
268 frame
= talloc_stackframe();
272 DEBUG(1,("Performing aggressive shutdown.\n"));
274 f
= context
->internal
->files
;
276 SMBCFILE
*next
= f
->next
;
277 smbc_getFunctionClose(context
)(context
, f
);
280 context
->internal
->files
= NULL
;
282 /* First try to remove the servers the nice way. */
283 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
286 DEBUG(1, ("Could not purge all servers, "
287 "Nice way shutdown failed.\n"));
288 s
= context
->internal
->servers
;
290 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
292 cli_shutdown(s
->cli
);
293 smbc_getFunctionRemoveCachedServer(context
)(context
,
296 DLIST_REMOVE(context
->internal
->servers
, s
);
300 context
->internal
->servers
= NULL
;
304 /* This is the polite way */
305 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
306 DEBUG(1, ("Could not purge all servers, "
307 "free_context failed.\n"));
312 if (context
->internal
->servers
) {
313 DEBUG(1, ("Active servers in context, "
314 "free_context failed.\n"));
319 if (context
->internal
->files
) {
320 DEBUG(1, ("Active files in context, "
321 "free_context failed.\n"));
328 /* Things we have to clean up */
329 smbc_setWorkgroup(context
, NULL
);
330 smbc_setNetbiosName(context
, NULL
);
331 smbc_setUser(context
, NULL
);
333 DEBUG(3, ("Context %p successfully freed\n", context
));
335 /* Free any DFS auth context. */
336 TALLOC_FREE(context
->internal
->creds
);
338 TALLOC_FREE(context
->internal
->lp_ctx
);
339 SAFE_FREE(context
->internal
);
342 /* Protect access to the count of contexts in use */
343 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
344 smb_panic("error locking 'initialized_ctx_count'");
347 if (initialized_ctx_count
) {
348 initialized_ctx_count
--;
351 if (initialized_ctx_count
== 0) {
352 SMBC_module_terminate();
355 /* Unlock the mutex */
356 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
357 smb_panic("error unlocking 'initialized_ctx_count'");
366 * Deprecated interface. Do not use. Instead, use the various
367 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
370 smbc_option_set(SMBCCTX
*context
,
372 ... /* option_value */)
378 smbc_get_auth_data_with_context_fn auth_fn
;
383 TALLOC_CTX
*frame
= talloc_stackframe();
385 va_start(ap
, option_name
);
387 if (strcmp(option_name
, "debug_to_stderr") == 0) {
388 option_value
.b
= (bool) va_arg(ap
, int);
389 smbc_setOptionDebugToStderr(context
, option_value
.b
);
391 } else if (strcmp(option_name
, "full_time_names") == 0) {
392 option_value
.b
= (bool) va_arg(ap
, int);
393 smbc_setOptionFullTimeNames(context
, option_value
.b
);
395 } else if (strcmp(option_name
, "open_share_mode") == 0) {
396 option_value
.i
= va_arg(ap
, int);
397 smbc_setOptionOpenShareMode(context
, option_value
.i
);
399 } else if (strcmp(option_name
, "auth_function") == 0) {
400 option_value
.auth_fn
=
401 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
402 smbc_setFunctionAuthDataWithContext(context
, option_value
.auth_fn
);
404 } else if (strcmp(option_name
, "user_data") == 0) {
405 option_value
.v
= va_arg(ap
, void *);
406 smbc_setOptionUserData(context
, option_value
.v
);
408 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
409 option_value
.s
= va_arg(ap
, const char *);
410 if (strcmp(option_value
.s
, "none") == 0) {
411 smbc_setOptionSmbEncryptionLevel(context
,
412 SMBC_ENCRYPTLEVEL_NONE
);
413 } else if (strcmp(option_value
.s
, "request") == 0) {
414 smbc_setOptionSmbEncryptionLevel(context
,
415 SMBC_ENCRYPTLEVEL_REQUEST
);
416 } else if (strcmp(option_value
.s
, "require") == 0) {
417 smbc_setOptionSmbEncryptionLevel(context
,
418 SMBC_ENCRYPTLEVEL_REQUIRE
);
421 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
422 option_value
.i
= va_arg(ap
, int);
423 smbc_setOptionBrowseMaxLmbCount(context
, option_value
.i
);
425 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
426 option_value
.b
= (bool) va_arg(ap
, int);
427 smbc_setOptionUrlEncodeReaddirEntries(context
, option_value
.b
);
429 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
430 option_value
.b
= (bool) va_arg(ap
, int);
431 smbc_setOptionOneSharePerServer(context
, option_value
.b
);
433 } else if (strcmp(option_name
, "use_kerberos") == 0) {
434 option_value
.b
= (bool) va_arg(ap
, int);
435 smbc_setOptionUseKerberos(context
, option_value
.b
);
437 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
438 option_value
.b
= (bool) va_arg(ap
, int);
439 smbc_setOptionFallbackAfterKerberos(context
, option_value
.b
);
441 } else if (strcmp(option_name
, "use_ccache") == 0) {
442 option_value
.b
= (bool) va_arg(ap
, int);
443 smbc_setOptionUseCCache(context
, option_value
.b
);
445 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
446 option_value
.b
= (bool) va_arg(ap
, int);
447 smbc_setOptionNoAutoAnonymousLogin(context
, option_value
.b
);
456 * Deprecated interface. Do not use. Instead, use the various
457 * smbc_getOption*() functions.
460 smbc_option_get(SMBCCTX
*context
,
463 if (strcmp(option_name
, "debug_stderr") == 0) {
464 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
465 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context
);
467 return (void *) smbc_getOptionDebugToStderr(context
);
470 } else if (strcmp(option_name
, "full_time_names") == 0) {
471 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
472 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context
);
474 return (void *) smbc_getOptionFullTimeNames(context
);
477 } else if (strcmp(option_name
, "open_share_mode") == 0) {
478 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
479 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context
);
481 return (void *) smbc_getOptionOpenShareMode(context
);
484 } else if (strcmp(option_name
, "auth_function") == 0) {
485 return (void *) smbc_getFunctionAuthDataWithContext(context
);
487 } else if (strcmp(option_name
, "user_data") == 0) {
488 return smbc_getOptionUserData(context
);
490 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
491 switch(smbc_getOptionSmbEncryptionLevel(context
))
493 case SMBC_ENCRYPTLEVEL_DEFAULT
:
494 return discard_const_p(void, "default");
496 return discard_const_p(void, "none");
498 return discard_const_p(void, "request");
500 return discard_const_p(void, "require");
503 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
505 unsigned int num_servers
= 0;
507 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
509 if (!cli_state_is_encryption_on(s
->cli
)) {
510 return (void *)false;
513 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
514 return (void *) (intptr_t) (bool) (num_servers
> 0);
516 return (void *) (bool) (num_servers
> 0);
519 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
520 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
521 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context
);
523 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
526 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
527 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
528 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context
);
530 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
533 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
534 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
535 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context
);
537 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
540 } else if (strcmp(option_name
, "use_kerberos") == 0) {
541 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
542 return (void *) (intptr_t) smbc_getOptionUseKerberos(context
);
544 return (void *) (bool) smbc_getOptionUseKerberos(context
);
547 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
548 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
549 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context
);
551 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
554 } else if (strcmp(option_name
, "use_ccache") == 0) {
555 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
556 return (void *) (intptr_t) smbc_getOptionUseCCache(context
);
558 return (void *) (bool) smbc_getOptionUseCCache(context
);
561 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
562 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
563 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context
);
565 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
574 * Initialize the library, etc.
576 * We accept a struct containing handle information.
577 * valid values for info->debug from 0 to 100,
578 * and insist that info->fn must be non-null.
581 smbc_init_context(SMBCCTX
*context
)
591 /* Do not initialise the same client twice */
592 if (context
->internal
->initialized
) {
596 frame
= talloc_stackframe();
598 if ((!smbc_getFunctionAuthData(context
) &&
599 !smbc_getFunctionAuthDataWithContext(context
)) ||
600 smbc_getDebug(context
) < 0 ||
601 smbc_getDebug(context
) > 100) {
609 if (!smbc_getUser(context
)) {
611 * FIXME: Is this the best way to get the user info?
613 char *user
= getenv("USER");
614 /* walk around as "guest" if no username can be found */
616 user
= SMB_STRDUP("guest");
618 user
= SMB_STRDUP(user
);
627 smbc_setUser(context
, user
);
630 if (!smbc_getUser(context
)) {
637 if (!smbc_getNetbiosName(context
)) {
639 * We try to get our netbios name from the config. If that
640 * fails we fall back on constructing our netbios name from
644 if (lp_netbios_name()) {
645 netbios_name
= SMB_STRDUP(lp_netbios_name());
648 * Hmmm, I want to get hostname as well, but I am too
649 * lazy for the moment
652 netbios_name
= (char *)SMB_MALLOC(17);
658 slprintf(netbios_name
, 16,
659 "smbc%s%d", smbc_getUser(context
), pid
);
668 smbc_setNetbiosName(context
, netbios_name
);
669 SAFE_FREE(netbios_name
);
671 if (!smbc_getNetbiosName(context
)) {
678 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context
)));
680 if (!smbc_getWorkgroup(context
)) {
681 const char *workgroup
;
683 if (lp_workgroup()) {
684 workgroup
= lp_workgroup();
686 /* TODO: Think about a decent default workgroup */
690 smbc_setWorkgroup(context
, workgroup
);
692 if (!smbc_getWorkgroup(context
)) {
699 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context
)));
701 /* shortest timeout is 1 second */
702 if (smbc_getTimeout(context
) > 0 && smbc_getTimeout(context
) < 1000)
703 smbc_setTimeout(context
, 1000);
705 context
->internal
->initialized
= True
;
707 /* Protect access to the count of contexts in use */
708 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
709 smb_panic("error locking 'initialized_ctx_count'");
712 initialized_ctx_count
++;
714 /* Unlock the mutex */
715 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
716 smb_panic("error unlocking 'initialized_ctx_count'");
724 /* Return the version of samba, and thus libsmbclient */
728 return samba_version_string();
732 * Set the credentials so DFS will work when following referrals.
733 * This function is broken and must be removed. No SMBCCTX arg...
738 smbc_set_credentials(const char *workgroup
,
740 const char *password
,
741 smbc_bool use_kerberos
,
742 const char *signing_state
)
744 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
747 void smbc_set_credentials_with_fallback(SMBCCTX
*context
,
748 const char *workgroup
,
750 const char *password
)
752 struct cli_credentials
*creds
= NULL
;
753 enum credentials_use_kerberos kerberos_state
=
754 CRED_USE_KERBEROS_DISABLED
;
761 if (! workgroup
|| ! *workgroup
) {
762 workgroup
= smbc_getWorkgroup(context
);
766 user
= smbc_getUser(context
);
773 creds
= cli_credentials_init(NULL
);
775 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
779 cli_credentials_set_conf(creds
, context
->internal
->lp_ctx
);
781 if (smbc_getOptionUseKerberos(context
)) {
782 kerberos_state
= CRED_USE_KERBEROS_REQUIRED
;
784 if (smbc_getOptionFallbackAfterKerberos(context
)) {
785 kerberos_state
= CRED_USE_KERBEROS_DESIRED
;
789 cli_credentials_set_username(creds
, user
, CRED_SPECIFIED
);
790 cli_credentials_set_password(creds
, password
, CRED_SPECIFIED
);
791 cli_credentials_set_domain(creds
, workgroup
, CRED_SPECIFIED
);
792 cli_credentials_set_kerberos_state(creds
,
795 if (smbc_getOptionUseCCache(context
)) {
796 cli_credentials_add_gensec_features(creds
,
797 GENSEC_FEATURE_NTLM_CCACHE
,
801 TALLOC_FREE(context
->internal
->creds
);
802 context
->internal
->creds
= creds
;