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"
33 * Is the logging working / configfile read ?
35 static bool SMBC_initialized
= false;
36 static unsigned int initialized_ctx_count
= 0;
37 static void *initialized_ctx_count_mutex
= NULL
;
40 * Do some module- and library-wide intializations
43 SMBC_module_init(void * punused
)
45 bool conf_loaded
= False
;
47 TALLOC_CTX
*frame
= talloc_stackframe();
49 load_case_tables_library();
51 setup_logging("libsmbclient", DEBUG_STDOUT
);
53 /* Here we would open the smb.conf file if needed ... */
55 home
= getenv("HOME");
58 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
59 if (lp_load_client(conf
)) {
62 DEBUG(5, ("Could not load config file: %s\n",
71 * Well, if that failed, try the get_dyn_CONFIGFILE
72 * Which points to the standard locn, and if that
73 * fails, silently ignore it and use the internal
77 if (!lp_load_client(get_dyn_CONFIGFILE())) {
78 DEBUG(5, ("Could not load config file: %s\n",
79 get_dyn_CONFIGFILE()));
83 * We loaded the global config file. Now lets
84 * load user-specific modifications to the
88 "%s/.smb/smb.conf.append",
90 if (!lp_load_client_no_reinit(conf
)) {
92 ("Could not append config file: "
101 load_interfaces(); /* Load the list of interfaces ... */
103 reopen_logs(); /* Get logging working ... */
106 * Block SIGPIPE (from lib/util_sock.c: write())
107 * It is not needed and should not stop execution
109 BlockSignals(True
, SIGPIPE
);
111 /* Create the mutex we'll use to protect initialized_ctx_count */
112 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
113 initialized_ctx_count_mutex
) != 0) {
114 smb_panic("SMBC_module_init: "
115 "failed to create 'initialized_ctx_count' mutex");
124 SMBC_module_terminate(void)
126 TALLOC_CTX
*frame
= talloc_stackframe();
129 SMBC_initialized
= false;
135 * Get a new empty handle to fill in with your own info
138 smbc_new_context(void)
141 TALLOC_CTX
*frame
= talloc_stackframe();
143 /* The first call to this function should initialize the module */
144 SMB_THREAD_ONCE(&SMBC_initialized
, SMBC_module_init
, NULL
);
147 * All newly added context fields should be placed in
148 * SMBC_internal_data, not directly in SMBCCTX.
150 context
= SMB_MALLOC_P(SMBCCTX
);
157 ZERO_STRUCTP(context
);
159 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
160 if (!context
->internal
) {
167 /* Initialize the context and establish reasonable defaults */
168 ZERO_STRUCTP(context
->internal
);
170 smbc_setDebug(context
, 0);
171 smbc_setTimeout(context
, 20000);
172 smbc_setPort(context
, 0);
174 smbc_setOptionFullTimeNames(context
, False
);
175 smbc_setOptionOpenShareMode(context
, SMBC_SHAREMODE_DENY_NONE
);
176 smbc_setOptionSmbEncryptionLevel(context
, SMBC_ENCRYPTLEVEL_NONE
);
177 smbc_setOptionUseCCache(context
, True
);
178 smbc_setOptionCaseSensitive(context
, False
);
179 smbc_setOptionBrowseMaxLmbCount(context
, 3); /* # LMBs to query */
180 smbc_setOptionUrlEncodeReaddirEntries(context
, False
);
181 smbc_setOptionOneSharePerServer(context
, False
);
182 if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL
) {
183 smbc_setOptionUseCCache(context
, true);
186 smbc_setFunctionAuthData(context
, SMBC_get_auth_data
);
187 smbc_setFunctionCheckServer(context
, SMBC_check_server
);
188 smbc_setFunctionRemoveUnusedServer(context
, SMBC_remove_unused_server
);
190 smbc_setOptionUserData(context
, NULL
);
191 smbc_setFunctionAddCachedServer(context
, SMBC_add_cached_server
);
192 smbc_setFunctionGetCachedServer(context
, SMBC_get_cached_server
);
193 smbc_setFunctionRemoveCachedServer(context
, SMBC_remove_cached_server
);
194 smbc_setFunctionPurgeCachedServers(context
, SMBC_purge_cached_servers
);
196 smbc_setFunctionOpen(context
, SMBC_open_ctx
);
197 smbc_setFunctionCreat(context
, SMBC_creat_ctx
);
198 smbc_setFunctionRead(context
, SMBC_read_ctx
);
199 smbc_setFunctionWrite(context
, SMBC_write_ctx
);
200 smbc_setFunctionClose(context
, SMBC_close_ctx
);
201 smbc_setFunctionUnlink(context
, SMBC_unlink_ctx
);
202 smbc_setFunctionRename(context
, SMBC_rename_ctx
);
203 smbc_setFunctionLseek(context
, SMBC_lseek_ctx
);
204 smbc_setFunctionFtruncate(context
, SMBC_ftruncate_ctx
);
205 smbc_setFunctionStat(context
, SMBC_stat_ctx
);
206 smbc_setFunctionStatVFS(context
, SMBC_statvfs_ctx
);
207 smbc_setFunctionFstatVFS(context
, SMBC_fstatvfs_ctx
);
208 smbc_setFunctionFstat(context
, SMBC_fstat_ctx
);
209 smbc_setFunctionOpendir(context
, SMBC_opendir_ctx
);
210 smbc_setFunctionClosedir(context
, SMBC_closedir_ctx
);
211 smbc_setFunctionReaddir(context
, SMBC_readdir_ctx
);
212 smbc_setFunctionGetdents(context
, SMBC_getdents_ctx
);
213 smbc_setFunctionMkdir(context
, SMBC_mkdir_ctx
);
214 smbc_setFunctionRmdir(context
, SMBC_rmdir_ctx
);
215 smbc_setFunctionTelldir(context
, SMBC_telldir_ctx
);
216 smbc_setFunctionLseekdir(context
, SMBC_lseekdir_ctx
);
217 smbc_setFunctionFstatdir(context
, SMBC_fstatdir_ctx
);
218 smbc_setFunctionChmod(context
, SMBC_chmod_ctx
);
219 smbc_setFunctionUtimes(context
, SMBC_utimes_ctx
);
220 smbc_setFunctionSetxattr(context
, SMBC_setxattr_ctx
);
221 smbc_setFunctionGetxattr(context
, SMBC_getxattr_ctx
);
222 smbc_setFunctionRemovexattr(context
, SMBC_removexattr_ctx
);
223 smbc_setFunctionListxattr(context
, SMBC_listxattr_ctx
);
225 smbc_setFunctionOpenPrintJob(context
, SMBC_open_print_job_ctx
);
226 smbc_setFunctionPrintFile(context
, SMBC_print_file_ctx
);
227 smbc_setFunctionListPrintJobs(context
, SMBC_list_print_jobs_ctx
);
228 smbc_setFunctionUnlinkPrintJob(context
, SMBC_unlink_print_job_ctx
);
237 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
238 * and thus you'll be leaking memory if not handled properly.
242 smbc_free_context(SMBCCTX
*context
,
251 frame
= talloc_stackframe();
255 DEBUG(1,("Performing aggressive shutdown.\n"));
257 f
= context
->internal
->files
;
259 smbc_getFunctionClose(context
)(context
, f
);
262 context
->internal
->files
= NULL
;
264 /* First try to remove the servers the nice way. */
265 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
268 DEBUG(1, ("Could not purge all servers, "
269 "Nice way shutdown failed.\n"));
270 s
= context
->internal
->servers
;
272 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
274 cli_shutdown(s
->cli
);
275 smbc_getFunctionRemoveCachedServer(context
)(context
,
278 DLIST_REMOVE(context
->internal
->servers
, s
);
282 context
->internal
->servers
= NULL
;
286 /* This is the polite way */
287 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
288 DEBUG(1, ("Could not purge all servers, "
289 "free_context failed.\n"));
294 if (context
->internal
->servers
) {
295 DEBUG(1, ("Active servers in context, "
296 "free_context failed.\n"));
301 if (context
->internal
->files
) {
302 DEBUG(1, ("Active files in context, "
303 "free_context failed.\n"));
310 /* Things we have to clean up */
311 smbc_setWorkgroup(context
, NULL
);
312 smbc_setNetbiosName(context
, NULL
);
313 smbc_setUser(context
, NULL
);
315 DEBUG(3, ("Context %p successfully freed\n", context
));
317 /* Free any DFS auth context. */
318 TALLOC_FREE(context
->internal
->auth_info
);
320 SAFE_FREE(context
->internal
);
323 /* Protect access to the count of contexts in use */
324 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
325 smb_panic("error locking 'initialized_ctx_count'");
328 if (initialized_ctx_count
) {
329 initialized_ctx_count
--;
332 if (initialized_ctx_count
== 0) {
333 SMBC_module_terminate();
336 /* Unlock the mutex */
337 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
338 smb_panic("error unlocking 'initialized_ctx_count'");
347 * Deprecated interface. Do not use. Instead, use the various
348 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
351 smbc_option_set(SMBCCTX
*context
,
353 ... /* option_value */)
359 smbc_get_auth_data_with_context_fn auth_fn
;
364 TALLOC_CTX
*frame
= talloc_stackframe();
366 va_start(ap
, option_name
);
368 if (strcmp(option_name
, "debug_to_stderr") == 0) {
369 option_value
.b
= (bool) va_arg(ap
, int);
370 smbc_setOptionDebugToStderr(context
, option_value
.b
);
372 } else if (strcmp(option_name
, "full_time_names") == 0) {
373 option_value
.b
= (bool) va_arg(ap
, int);
374 smbc_setOptionFullTimeNames(context
, option_value
.b
);
376 } else if (strcmp(option_name
, "open_share_mode") == 0) {
377 option_value
.i
= va_arg(ap
, int);
378 smbc_setOptionOpenShareMode(context
, option_value
.i
);
380 } else if (strcmp(option_name
, "auth_function") == 0) {
381 option_value
.auth_fn
=
382 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
383 smbc_setFunctionAuthDataWithContext(context
, option_value
.auth_fn
);
385 } else if (strcmp(option_name
, "user_data") == 0) {
386 option_value
.v
= va_arg(ap
, void *);
387 smbc_setOptionUserData(context
, option_value
.v
);
389 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
390 option_value
.s
= va_arg(ap
, const char *);
391 if (strcmp(option_value
.s
, "none") == 0) {
392 smbc_setOptionSmbEncryptionLevel(context
,
393 SMBC_ENCRYPTLEVEL_NONE
);
394 } else if (strcmp(option_value
.s
, "request") == 0) {
395 smbc_setOptionSmbEncryptionLevel(context
,
396 SMBC_ENCRYPTLEVEL_REQUEST
);
397 } else if (strcmp(option_value
.s
, "require") == 0) {
398 smbc_setOptionSmbEncryptionLevel(context
,
399 SMBC_ENCRYPTLEVEL_REQUIRE
);
402 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
403 option_value
.i
= va_arg(ap
, int);
404 smbc_setOptionBrowseMaxLmbCount(context
, option_value
.i
);
406 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
407 option_value
.b
= (bool) va_arg(ap
, int);
408 smbc_setOptionUrlEncodeReaddirEntries(context
, option_value
.b
);
410 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
411 option_value
.b
= (bool) va_arg(ap
, int);
412 smbc_setOptionOneSharePerServer(context
, option_value
.b
);
414 } else if (strcmp(option_name
, "use_kerberos") == 0) {
415 option_value
.b
= (bool) va_arg(ap
, int);
416 smbc_setOptionUseKerberos(context
, option_value
.b
);
418 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
419 option_value
.b
= (bool) va_arg(ap
, int);
420 smbc_setOptionFallbackAfterKerberos(context
, option_value
.b
);
422 } else if (strcmp(option_name
, "use_ccache") == 0) {
423 option_value
.b
= (bool) va_arg(ap
, int);
424 smbc_setOptionUseCCache(context
, option_value
.b
);
426 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
427 option_value
.b
= (bool) va_arg(ap
, int);
428 smbc_setOptionNoAutoAnonymousLogin(context
, option_value
.b
);
437 * Deprecated interface. Do not use. Instead, use the various
438 * smbc_getOption*() functions.
441 smbc_option_get(SMBCCTX
*context
,
444 if (strcmp(option_name
, "debug_stderr") == 0) {
445 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
446 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context
);
448 return (void *) smbc_getOptionDebugToStderr(context
);
451 } else if (strcmp(option_name
, "full_time_names") == 0) {
452 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
453 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context
);
455 return (void *) smbc_getOptionFullTimeNames(context
);
458 } else if (strcmp(option_name
, "open_share_mode") == 0) {
459 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
460 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context
);
462 return (void *) smbc_getOptionOpenShareMode(context
);
465 } else if (strcmp(option_name
, "auth_function") == 0) {
466 return (void *) smbc_getFunctionAuthDataWithContext(context
);
468 } else if (strcmp(option_name
, "user_data") == 0) {
469 return smbc_getOptionUserData(context
);
471 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
472 switch(smbc_getOptionSmbEncryptionLevel(context
))
475 return discard_const_p(void, "none");
477 return discard_const_p(void, "request");
479 return discard_const_p(void, "require");
482 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
484 unsigned int num_servers
= 0;
486 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
488 if (!smb1cli_conn_encryption_on(s
->cli
->conn
)) {
489 return (void *)false;
492 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
493 return (void *) (intptr_t) (bool) (num_servers
> 0);
495 return (void *) (bool) (num_servers
> 0);
498 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
499 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
500 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context
);
502 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
505 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
506 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
507 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context
);
509 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
512 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
513 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
514 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context
);
516 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
519 } else if (strcmp(option_name
, "use_kerberos") == 0) {
520 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
521 return (void *) (intptr_t) smbc_getOptionUseKerberos(context
);
523 return (void *) (bool) smbc_getOptionUseKerberos(context
);
526 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
527 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
528 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context
);
530 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
533 } else if (strcmp(option_name
, "use_ccache") == 0) {
534 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
535 return (void *) (intptr_t) smbc_getOptionUseCCache(context
);
537 return (void *) (bool) smbc_getOptionUseCCache(context
);
540 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
541 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
542 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context
);
544 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
553 * Initialize the library, etc.
555 * We accept a struct containing handle information.
556 * valid values for info->debug from 0 to 100,
557 * and insist that info->fn must be non-null.
560 smbc_init_context(SMBCCTX
*context
)
569 /* Do not initialise the same client twice */
570 if (context
->internal
->initialized
) {
574 if ((!smbc_getFunctionAuthData(context
) &&
575 !smbc_getFunctionAuthDataWithContext(context
)) ||
576 smbc_getDebug(context
) < 0 ||
577 smbc_getDebug(context
) > 100) {
584 if (!smbc_getUser(context
)) {
586 * FIXME: Is this the best way to get the user info?
588 char *user
= getenv("USER");
589 /* walk around as "guest" if no username can be found */
591 user
= SMB_STRDUP("guest");
593 user
= SMB_STRDUP(user
);
601 smbc_setUser(context
, user
);
604 if (!smbc_getUser(context
)) {
610 if (!smbc_getNetbiosName(context
)) {
612 * We try to get our netbios name from the config. If that
613 * fails we fall back on constructing our netbios name from
617 if (lp_netbios_name()) {
618 netbios_name
= SMB_STRDUP(lp_netbios_name());
621 * Hmmm, I want to get hostname as well, but I am too
622 * lazy for the moment
625 netbios_name
= (char *)SMB_MALLOC(17);
630 slprintf(netbios_name
, 16,
631 "smbc%s%d", smbc_getUser(context
), pid
);
639 smbc_setNetbiosName(context
, netbios_name
);
640 SAFE_FREE(netbios_name
);
642 if (!smbc_getNetbiosName(context
)) {
648 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context
)));
650 if (!smbc_getWorkgroup(context
)) {
653 if (lp_workgroup()) {
654 workgroup
= SMB_STRDUP(lp_workgroup());
657 /* TODO: Think about a decent default workgroup */
658 workgroup
= SMB_STRDUP("samba");
666 smbc_setWorkgroup(context
, workgroup
);
667 SAFE_FREE(workgroup
);
669 if (!smbc_getWorkgroup(context
)) {
675 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context
)));
677 /* shortest timeout is 1 second */
678 if (smbc_getTimeout(context
) > 0 && smbc_getTimeout(context
) < 1000)
679 smbc_setTimeout(context
, 1000);
681 context
->internal
->initialized
= True
;
683 /* Protect access to the count of contexts in use */
684 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
685 smb_panic("error locking 'initialized_ctx_count'");
688 initialized_ctx_count
++;
690 /* Unlock the mutex */
691 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
692 smb_panic("error unlocking 'initialized_ctx_count'");
699 /* Return the verion of samba, and thus libsmbclient */
703 return samba_version_string();
707 * Set the credentials so DFS will work when following referrals.
708 * This function is broken and must be removed. No SMBCCTX arg...
713 smbc_set_credentials(const char *workgroup
,
715 const char *password
,
716 smbc_bool use_kerberos
,
717 const char *signing_state
)
719 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
722 void smbc_set_credentials_with_fallback(SMBCCTX
*context
,
723 const char *workgroup
,
725 const char *password
)
727 smbc_bool use_kerberos
= false;
728 const char *signing_state
= "off";
729 struct user_auth_info
*auth_info
= NULL
;
736 if (! workgroup
|| ! *workgroup
) {
737 workgroup
= smbc_getWorkgroup(context
);
741 user
= smbc_getUser(context
);
748 auth_info
= user_auth_info_init(NULL
);
751 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
755 if (smbc_getOptionUseKerberos(context
)) {
759 if (lp_client_signing() != SMB_SIGNING_OFF
) {
760 signing_state
= "if_required";
763 if (lp_client_signing() == SMB_SIGNING_REQUIRED
) {
764 signing_state
= "required";
767 set_cmdline_auth_info_username(auth_info
, user
);
768 set_cmdline_auth_info_domain(auth_info
, workgroup
);
769 set_cmdline_auth_info_password(auth_info
, password
);
770 set_cmdline_auth_info_use_kerberos(auth_info
, use_kerberos
);
771 set_cmdline_auth_info_signing_state(auth_info
, signing_state
);
772 set_cmdline_auth_info_fallback_after_kerberos(auth_info
,
773 smbc_getOptionFallbackAfterKerberos(context
));
774 set_cmdline_auth_info_use_ccache(
775 auth_info
, smbc_getOptionUseCCache(context
));
777 TALLOC_FREE(context
->internal
->auth_info
);
779 context
->internal
->auth_info
= auth_info
;