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"
36 * Is the logging working / configfile read ?
38 static bool SMBC_initialized
= false;
39 static unsigned int initialized_ctx_count
= 0;
40 static void *initialized_ctx_count_mutex
= NULL
;
43 * Do some module- and library-wide intializations
46 SMBC_module_init(void * punused
)
48 bool conf_loaded
= False
;
50 TALLOC_CTX
*frame
= talloc_stackframe();
52 setup_logging("libsmbclient", DEBUG_STDOUT
);
54 /* Here we would open the smb.conf file if needed ... */
56 home
= getenv("HOME");
59 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
60 if (lp_load_client(conf
)) {
63 DEBUG(5, ("Could not load config file: %s\n",
72 * Well, if that failed, try the get_dyn_CONFIGFILE
73 * Which points to the standard locn, and if that
74 * fails, silently ignore it and use the internal
78 if (!lp_load_client(get_dyn_CONFIGFILE())) {
79 DEBUG(5, ("Could not load config file: %s\n",
80 get_dyn_CONFIGFILE()));
84 * We loaded the global config file. Now lets
85 * load user-specific modifications to the
89 "%s/.smb/smb.conf.append",
91 if (!lp_load_client_no_reinit(conf
)) {
93 ("Could not append config file: "
102 load_interfaces(); /* Load the list of interfaces ... */
104 reopen_logs(); /* Get logging working ... */
107 * Block SIGPIPE (from lib/util_sock.c: write())
108 * It is not needed and should not stop execution
110 BlockSignals(True
, SIGPIPE
);
112 /* Create the mutex we'll use to protect initialized_ctx_count */
113 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
114 initialized_ctx_count_mutex
) != 0) {
115 smb_panic("SMBC_module_init: "
116 "failed to create 'initialized_ctx_count' mutex");
125 SMBC_module_terminate(void)
127 TALLOC_CTX
*frame
= talloc_stackframe();
130 SMBC_initialized
= false;
136 * Get a new empty handle to fill in with your own info
139 smbc_new_context(void)
142 TALLOC_CTX
*frame
= talloc_stackframe();
144 /* The first call to this function should initialize the module */
145 SMB_THREAD_ONCE(&SMBC_initialized
, SMBC_module_init
, NULL
);
148 * All newly added context fields should be placed in
149 * SMBC_internal_data, not directly in SMBCCTX.
151 context
= SMB_MALLOC_P(SMBCCTX
);
158 ZERO_STRUCTP(context
);
160 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
161 if (!context
->internal
) {
168 /* Initialize the context and establish reasonable defaults */
169 ZERO_STRUCTP(context
->internal
);
171 smbc_setDebug(context
, 0);
172 smbc_setTimeout(context
, 20000);
173 smbc_setPort(context
, 0);
175 smbc_setOptionFullTimeNames(context
, False
);
176 smbc_setOptionOpenShareMode(context
, SMBC_SHAREMODE_DENY_NONE
);
177 smbc_setOptionSmbEncryptionLevel(context
, SMBC_ENCRYPTLEVEL_DEFAULT
);
178 smbc_setOptionUseCCache(context
, True
);
179 smbc_setOptionCaseSensitive(context
, False
);
180 smbc_setOptionBrowseMaxLmbCount(context
, 3); /* # LMBs to query */
181 smbc_setOptionUrlEncodeReaddirEntries(context
, False
);
182 smbc_setOptionOneSharePerServer(context
, False
);
183 if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL
) {
184 smbc_setOptionUseCCache(context
, false);
187 smbc_setFunctionAuthData(context
, SMBC_get_auth_data
);
188 smbc_setFunctionCheckServer(context
, SMBC_check_server
);
189 smbc_setFunctionRemoveUnusedServer(context
, SMBC_remove_unused_server
);
191 smbc_setOptionUserData(context
, NULL
);
192 smbc_setFunctionAddCachedServer(context
, SMBC_add_cached_server
);
193 smbc_setFunctionGetCachedServer(context
, SMBC_get_cached_server
);
194 smbc_setFunctionRemoveCachedServer(context
, SMBC_remove_cached_server
);
195 smbc_setFunctionPurgeCachedServers(context
, SMBC_purge_cached_servers
);
197 smbc_setFunctionOpen(context
, SMBC_open_ctx
);
198 smbc_setFunctionCreat(context
, SMBC_creat_ctx
);
199 smbc_setFunctionRead(context
, SMBC_read_ctx
);
200 smbc_setFunctionSplice(context
, SMBC_splice_ctx
);
201 smbc_setFunctionWrite(context
, SMBC_write_ctx
);
202 smbc_setFunctionClose(context
, SMBC_close_ctx
);
203 smbc_setFunctionUnlink(context
, SMBC_unlink_ctx
);
204 smbc_setFunctionRename(context
, SMBC_rename_ctx
);
205 smbc_setFunctionLseek(context
, SMBC_lseek_ctx
);
206 smbc_setFunctionFtruncate(context
, SMBC_ftruncate_ctx
);
207 smbc_setFunctionStat(context
, SMBC_stat_ctx
);
208 smbc_setFunctionStatVFS(context
, SMBC_statvfs_ctx
);
209 smbc_setFunctionFstatVFS(context
, SMBC_fstatvfs_ctx
);
210 smbc_setFunctionFstat(context
, SMBC_fstat_ctx
);
211 smbc_setFunctionOpendir(context
, SMBC_opendir_ctx
);
212 smbc_setFunctionClosedir(context
, SMBC_closedir_ctx
);
213 smbc_setFunctionReaddir(context
, SMBC_readdir_ctx
);
214 smbc_setFunctionReaddirPlus(context
, SMBC_readdirplus_ctx
);
215 smbc_setFunctionReaddirPlus2(context
, SMBC_readdirplus2_ctx
);
216 smbc_setFunctionGetdents(context
, SMBC_getdents_ctx
);
217 smbc_setFunctionMkdir(context
, SMBC_mkdir_ctx
);
218 smbc_setFunctionRmdir(context
, SMBC_rmdir_ctx
);
219 smbc_setFunctionTelldir(context
, SMBC_telldir_ctx
);
220 smbc_setFunctionLseekdir(context
, SMBC_lseekdir_ctx
);
221 smbc_setFunctionFstatdir(context
, SMBC_fstatdir_ctx
);
222 smbc_setFunctionNotify(context
, SMBC_notify_ctx
);
223 smbc_setFunctionChmod(context
, SMBC_chmod_ctx
);
224 smbc_setFunctionUtimes(context
, SMBC_utimes_ctx
);
225 smbc_setFunctionSetxattr(context
, SMBC_setxattr_ctx
);
226 smbc_setFunctionGetxattr(context
, SMBC_getxattr_ctx
);
227 smbc_setFunctionRemovexattr(context
, SMBC_removexattr_ctx
);
228 smbc_setFunctionListxattr(context
, SMBC_listxattr_ctx
);
230 smbc_setFunctionOpenPrintJob(context
, SMBC_open_print_job_ctx
);
231 smbc_setFunctionPrintFile(context
, SMBC_print_file_ctx
);
232 smbc_setFunctionListPrintJobs(context
, SMBC_list_print_jobs_ctx
);
233 smbc_setFunctionUnlinkPrintJob(context
, SMBC_unlink_print_job_ctx
);
242 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
243 * and thus you'll be leaking memory if not handled properly.
247 smbc_free_context(SMBCCTX
*context
,
256 frame
= talloc_stackframe();
260 DEBUG(1,("Performing aggressive shutdown.\n"));
262 f
= context
->internal
->files
;
264 SMBCFILE
*next
= f
->next
;
265 smbc_getFunctionClose(context
)(context
, f
);
268 context
->internal
->files
= NULL
;
270 /* First try to remove the servers the nice way. */
271 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
274 DEBUG(1, ("Could not purge all servers, "
275 "Nice way shutdown failed.\n"));
276 s
= context
->internal
->servers
;
278 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
280 cli_shutdown(s
->cli
);
281 smbc_getFunctionRemoveCachedServer(context
)(context
,
284 DLIST_REMOVE(context
->internal
->servers
, s
);
288 context
->internal
->servers
= NULL
;
292 /* This is the polite way */
293 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
294 DEBUG(1, ("Could not purge all servers, "
295 "free_context failed.\n"));
300 if (context
->internal
->servers
) {
301 DEBUG(1, ("Active servers in context, "
302 "free_context failed.\n"));
307 if (context
->internal
->files
) {
308 DEBUG(1, ("Active files in context, "
309 "free_context failed.\n"));
316 /* Things we have to clean up */
317 smbc_setWorkgroup(context
, NULL
);
318 smbc_setNetbiosName(context
, NULL
);
319 smbc_setUser(context
, NULL
);
321 DEBUG(3, ("Context %p successfully freed\n", context
));
323 /* Free any DFS auth context. */
324 TALLOC_FREE(context
->internal
->creds
);
326 SAFE_FREE(context
->internal
);
329 /* Protect access to the count of contexts in use */
330 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
331 smb_panic("error locking 'initialized_ctx_count'");
334 if (initialized_ctx_count
) {
335 initialized_ctx_count
--;
338 if (initialized_ctx_count
== 0) {
339 SMBC_module_terminate();
342 /* Unlock the mutex */
343 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
344 smb_panic("error unlocking 'initialized_ctx_count'");
353 * Deprecated interface. Do not use. Instead, use the various
354 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
357 smbc_option_set(SMBCCTX
*context
,
359 ... /* option_value */)
365 smbc_get_auth_data_with_context_fn auth_fn
;
370 TALLOC_CTX
*frame
= talloc_stackframe();
372 va_start(ap
, option_name
);
374 if (strcmp(option_name
, "debug_to_stderr") == 0) {
375 option_value
.b
= (bool) va_arg(ap
, int);
376 smbc_setOptionDebugToStderr(context
, option_value
.b
);
378 } else if (strcmp(option_name
, "full_time_names") == 0) {
379 option_value
.b
= (bool) va_arg(ap
, int);
380 smbc_setOptionFullTimeNames(context
, option_value
.b
);
382 } else if (strcmp(option_name
, "open_share_mode") == 0) {
383 option_value
.i
= va_arg(ap
, int);
384 smbc_setOptionOpenShareMode(context
, option_value
.i
);
386 } else if (strcmp(option_name
, "auth_function") == 0) {
387 option_value
.auth_fn
=
388 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
389 smbc_setFunctionAuthDataWithContext(context
, option_value
.auth_fn
);
391 } else if (strcmp(option_name
, "user_data") == 0) {
392 option_value
.v
= va_arg(ap
, void *);
393 smbc_setOptionUserData(context
, option_value
.v
);
395 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
396 option_value
.s
= va_arg(ap
, const char *);
397 if (strcmp(option_value
.s
, "none") == 0) {
398 smbc_setOptionSmbEncryptionLevel(context
,
399 SMBC_ENCRYPTLEVEL_NONE
);
400 } else if (strcmp(option_value
.s
, "request") == 0) {
401 smbc_setOptionSmbEncryptionLevel(context
,
402 SMBC_ENCRYPTLEVEL_REQUEST
);
403 } else if (strcmp(option_value
.s
, "require") == 0) {
404 smbc_setOptionSmbEncryptionLevel(context
,
405 SMBC_ENCRYPTLEVEL_REQUIRE
);
408 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
409 option_value
.i
= va_arg(ap
, int);
410 smbc_setOptionBrowseMaxLmbCount(context
, option_value
.i
);
412 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
413 option_value
.b
= (bool) va_arg(ap
, int);
414 smbc_setOptionUrlEncodeReaddirEntries(context
, option_value
.b
);
416 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
417 option_value
.b
= (bool) va_arg(ap
, int);
418 smbc_setOptionOneSharePerServer(context
, option_value
.b
);
420 } else if (strcmp(option_name
, "use_kerberos") == 0) {
421 option_value
.b
= (bool) va_arg(ap
, int);
422 smbc_setOptionUseKerberos(context
, option_value
.b
);
424 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
425 option_value
.b
= (bool) va_arg(ap
, int);
426 smbc_setOptionFallbackAfterKerberos(context
, option_value
.b
);
428 } else if (strcmp(option_name
, "use_ccache") == 0) {
429 option_value
.b
= (bool) va_arg(ap
, int);
430 smbc_setOptionUseCCache(context
, option_value
.b
);
432 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
433 option_value
.b
= (bool) va_arg(ap
, int);
434 smbc_setOptionNoAutoAnonymousLogin(context
, option_value
.b
);
443 * Deprecated interface. Do not use. Instead, use the various
444 * smbc_getOption*() functions.
447 smbc_option_get(SMBCCTX
*context
,
450 if (strcmp(option_name
, "debug_stderr") == 0) {
451 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
452 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context
);
454 return (void *) smbc_getOptionDebugToStderr(context
);
457 } else if (strcmp(option_name
, "full_time_names") == 0) {
458 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
459 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context
);
461 return (void *) smbc_getOptionFullTimeNames(context
);
464 } else if (strcmp(option_name
, "open_share_mode") == 0) {
465 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
466 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context
);
468 return (void *) smbc_getOptionOpenShareMode(context
);
471 } else if (strcmp(option_name
, "auth_function") == 0) {
472 return (void *) smbc_getFunctionAuthDataWithContext(context
);
474 } else if (strcmp(option_name
, "user_data") == 0) {
475 return smbc_getOptionUserData(context
);
477 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
478 switch(smbc_getOptionSmbEncryptionLevel(context
))
480 case SMBC_ENCRYPTLEVEL_DEFAULT
:
481 return discard_const_p(void, "default");
483 return discard_const_p(void, "none");
485 return discard_const_p(void, "request");
487 return discard_const_p(void, "require");
490 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
492 unsigned int num_servers
= 0;
494 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
496 if (!cli_state_is_encryption_on(s
->cli
)) {
497 return (void *)false;
500 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
501 return (void *) (intptr_t) (bool) (num_servers
> 0);
503 return (void *) (bool) (num_servers
> 0);
506 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
507 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
508 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context
);
510 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
513 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
514 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
515 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context
);
517 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
520 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
521 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
522 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context
);
524 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
527 } else if (strcmp(option_name
, "use_kerberos") == 0) {
528 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
529 return (void *) (intptr_t) smbc_getOptionUseKerberos(context
);
531 return (void *) (bool) smbc_getOptionUseKerberos(context
);
534 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
535 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
536 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context
);
538 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
541 } else if (strcmp(option_name
, "use_ccache") == 0) {
542 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
543 return (void *) (intptr_t) smbc_getOptionUseCCache(context
);
545 return (void *) (bool) smbc_getOptionUseCCache(context
);
548 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
549 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
550 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context
);
552 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
561 * Initialize the library, etc.
563 * We accept a struct containing handle information.
564 * valid values for info->debug from 0 to 100,
565 * and insist that info->fn must be non-null.
568 smbc_init_context(SMBCCTX
*context
)
578 /* Do not initialise the same client twice */
579 if (context
->internal
->initialized
) {
583 frame
= talloc_stackframe();
585 if ((!smbc_getFunctionAuthData(context
) &&
586 !smbc_getFunctionAuthDataWithContext(context
)) ||
587 smbc_getDebug(context
) < 0 ||
588 smbc_getDebug(context
) > 100) {
596 if (!smbc_getUser(context
)) {
598 * FIXME: Is this the best way to get the user info?
600 char *user
= getenv("USER");
601 /* walk around as "guest" if no username can be found */
603 user
= SMB_STRDUP("guest");
605 user
= SMB_STRDUP(user
);
614 smbc_setUser(context
, user
);
617 if (!smbc_getUser(context
)) {
624 if (!smbc_getNetbiosName(context
)) {
626 * We try to get our netbios name from the config. If that
627 * fails we fall back on constructing our netbios name from
631 if (lp_netbios_name()) {
632 netbios_name
= SMB_STRDUP(lp_netbios_name());
635 * Hmmm, I want to get hostname as well, but I am too
636 * lazy for the moment
639 netbios_name
= (char *)SMB_MALLOC(17);
645 slprintf(netbios_name
, 16,
646 "smbc%s%d", smbc_getUser(context
), pid
);
655 smbc_setNetbiosName(context
, netbios_name
);
656 SAFE_FREE(netbios_name
);
658 if (!smbc_getNetbiosName(context
)) {
665 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context
)));
667 if (!smbc_getWorkgroup(context
)) {
668 const char *workgroup
;
670 if (lp_workgroup()) {
671 workgroup
= lp_workgroup();
673 /* TODO: Think about a decent default workgroup */
677 smbc_setWorkgroup(context
, workgroup
);
679 if (!smbc_getWorkgroup(context
)) {
686 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context
)));
688 /* shortest timeout is 1 second */
689 if (smbc_getTimeout(context
) > 0 && smbc_getTimeout(context
) < 1000)
690 smbc_setTimeout(context
, 1000);
692 context
->internal
->initialized
= True
;
694 /* Protect access to the count of contexts in use */
695 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
696 smb_panic("error locking 'initialized_ctx_count'");
699 initialized_ctx_count
++;
701 /* Unlock the mutex */
702 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
703 smb_panic("error unlocking 'initialized_ctx_count'");
711 /* Return the version of samba, and thus libsmbclient */
715 return samba_version_string();
719 * Set the credentials so DFS will work when following referrals.
720 * This function is broken and must be removed. No SMBCCTX arg...
725 smbc_set_credentials(const char *workgroup
,
727 const char *password
,
728 smbc_bool use_kerberos
,
729 const char *signing_state
)
731 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
734 void smbc_set_credentials_with_fallback(SMBCCTX
*context
,
735 const char *workgroup
,
737 const char *password
)
739 struct loadparm_context
*lp_ctx
= NULL
;
740 struct cli_credentials
*creds
= NULL
;
741 enum credentials_use_kerberos kerberos_state
=
742 CRED_USE_KERBEROS_DISABLED
;
749 if (! workgroup
|| ! *workgroup
) {
750 workgroup
= smbc_getWorkgroup(context
);
754 user
= smbc_getUser(context
);
761 creds
= cli_credentials_init(NULL
);
763 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
767 lp_ctx
= loadparm_init_s3(creds
, loadparm_s3_helpers());
768 if (lp_ctx
== NULL
) {
773 cli_credentials_set_conf(creds
, lp_ctx
);
775 if (smbc_getOptionUseKerberos(context
)) {
776 kerberos_state
= CRED_USE_KERBEROS_REQUIRED
;
778 if (smbc_getOptionFallbackAfterKerberos(context
)) {
779 kerberos_state
= CRED_USE_KERBEROS_DESIRED
;
783 cli_credentials_set_username(creds
, user
, CRED_SPECIFIED
);
784 cli_credentials_set_password(creds
, password
, CRED_SPECIFIED
);
785 cli_credentials_set_domain(creds
, workgroup
, CRED_SPECIFIED
);
786 cli_credentials_set_kerberos_state(creds
,
789 if (smbc_getOptionUseCCache(context
)) {
790 uint32_t gensec_features
;
792 gensec_features
= cli_credentials_get_gensec_features(creds
);
793 gensec_features
|= GENSEC_FEATURE_NTLM_CCACHE
;
794 cli_credentials_set_gensec_features(creds
,
799 TALLOC_FREE(context
->internal
->creds
);
800 context
->internal
->creds
= creds
;