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 setup_logging("libsmbclient", DEBUG_STDOUT
);
51 /* Here we would open the smb.conf file if needed ... */
53 home
= getenv("HOME");
56 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
57 if (lp_load_client(conf
)) {
60 DEBUG(5, ("Could not load config file: %s\n",
69 * Well, if that failed, try the get_dyn_CONFIGFILE
70 * Which points to the standard locn, and if that
71 * fails, silently ignore it and use the internal
75 if (!lp_load_client(get_dyn_CONFIGFILE())) {
76 DEBUG(5, ("Could not load config file: %s\n",
77 get_dyn_CONFIGFILE()));
81 * We loaded the global config file. Now lets
82 * load user-specific modifications to the
86 "%s/.smb/smb.conf.append",
88 if (!lp_load_client_no_reinit(conf
)) {
90 ("Could not append config file: "
99 load_interfaces(); /* Load the list of interfaces ... */
101 reopen_logs(); /* Get logging working ... */
104 * Block SIGPIPE (from lib/util_sock.c: write())
105 * It is not needed and should not stop execution
107 BlockSignals(True
, SIGPIPE
);
109 /* Create the mutex we'll use to protect initialized_ctx_count */
110 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
111 initialized_ctx_count_mutex
) != 0) {
112 smb_panic("SMBC_module_init: "
113 "failed to create 'initialized_ctx_count' mutex");
122 SMBC_module_terminate(void)
124 TALLOC_CTX
*frame
= talloc_stackframe();
127 SMBC_initialized
= false;
133 * Get a new empty handle to fill in with your own info
136 smbc_new_context(void)
139 TALLOC_CTX
*frame
= talloc_stackframe();
141 /* The first call to this function should initialize the module */
142 SMB_THREAD_ONCE(&SMBC_initialized
, SMBC_module_init
, NULL
);
145 * All newly added context fields should be placed in
146 * SMBC_internal_data, not directly in SMBCCTX.
148 context
= SMB_MALLOC_P(SMBCCTX
);
155 ZERO_STRUCTP(context
);
157 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
158 if (!context
->internal
) {
165 /* Initialize the context and establish reasonable defaults */
166 ZERO_STRUCTP(context
->internal
);
168 smbc_setDebug(context
, 0);
169 smbc_setTimeout(context
, 20000);
170 smbc_setPort(context
, 0);
172 smbc_setOptionFullTimeNames(context
, False
);
173 smbc_setOptionOpenShareMode(context
, SMBC_SHAREMODE_DENY_NONE
);
174 smbc_setOptionSmbEncryptionLevel(context
, SMBC_ENCRYPTLEVEL_NONE
);
175 smbc_setOptionUseCCache(context
, True
);
176 smbc_setOptionCaseSensitive(context
, False
);
177 smbc_setOptionBrowseMaxLmbCount(context
, 3); /* # LMBs to query */
178 smbc_setOptionUrlEncodeReaddirEntries(context
, False
);
179 smbc_setOptionOneSharePerServer(context
, False
);
180 if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL
) {
181 smbc_setOptionUseCCache(context
, true);
184 smbc_setFunctionAuthData(context
, SMBC_get_auth_data
);
185 smbc_setFunctionCheckServer(context
, SMBC_check_server
);
186 smbc_setFunctionRemoveUnusedServer(context
, SMBC_remove_unused_server
);
188 smbc_setOptionUserData(context
, NULL
);
189 smbc_setFunctionAddCachedServer(context
, SMBC_add_cached_server
);
190 smbc_setFunctionGetCachedServer(context
, SMBC_get_cached_server
);
191 smbc_setFunctionRemoveCachedServer(context
, SMBC_remove_cached_server
);
192 smbc_setFunctionPurgeCachedServers(context
, SMBC_purge_cached_servers
);
194 smbc_setFunctionOpen(context
, SMBC_open_ctx
);
195 smbc_setFunctionCreat(context
, SMBC_creat_ctx
);
196 smbc_setFunctionRead(context
, SMBC_read_ctx
);
197 smbc_setFunctionSplice(context
, SMBC_splice_ctx
);
198 smbc_setFunctionWrite(context
, SMBC_write_ctx
);
199 smbc_setFunctionClose(context
, SMBC_close_ctx
);
200 smbc_setFunctionUnlink(context
, SMBC_unlink_ctx
);
201 smbc_setFunctionRename(context
, SMBC_rename_ctx
);
202 smbc_setFunctionLseek(context
, SMBC_lseek_ctx
);
203 smbc_setFunctionFtruncate(context
, SMBC_ftruncate_ctx
);
204 smbc_setFunctionStat(context
, SMBC_stat_ctx
);
205 smbc_setFunctionStatVFS(context
, SMBC_statvfs_ctx
);
206 smbc_setFunctionFstatVFS(context
, SMBC_fstatvfs_ctx
);
207 smbc_setFunctionFstat(context
, SMBC_fstat_ctx
);
208 smbc_setFunctionOpendir(context
, SMBC_opendir_ctx
);
209 smbc_setFunctionClosedir(context
, SMBC_closedir_ctx
);
210 smbc_setFunctionReaddir(context
, SMBC_readdir_ctx
);
211 smbc_setFunctionGetdents(context
, SMBC_getdents_ctx
);
212 smbc_setFunctionMkdir(context
, SMBC_mkdir_ctx
);
213 smbc_setFunctionRmdir(context
, SMBC_rmdir_ctx
);
214 smbc_setFunctionTelldir(context
, SMBC_telldir_ctx
);
215 smbc_setFunctionLseekdir(context
, SMBC_lseekdir_ctx
);
216 smbc_setFunctionFstatdir(context
, SMBC_fstatdir_ctx
);
217 smbc_setFunctionNotify(context
, SMBC_notify_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 SMBCFILE
*next
= f
->next
;
260 smbc_getFunctionClose(context
)(context
, f
);
263 context
->internal
->files
= NULL
;
265 /* First try to remove the servers the nice way. */
266 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
269 DEBUG(1, ("Could not purge all servers, "
270 "Nice way shutdown failed.\n"));
271 s
= context
->internal
->servers
;
273 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
275 cli_shutdown(s
->cli
);
276 smbc_getFunctionRemoveCachedServer(context
)(context
,
279 DLIST_REMOVE(context
->internal
->servers
, s
);
283 context
->internal
->servers
= NULL
;
287 /* This is the polite way */
288 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
289 DEBUG(1, ("Could not purge all servers, "
290 "free_context failed.\n"));
295 if (context
->internal
->servers
) {
296 DEBUG(1, ("Active servers in context, "
297 "free_context failed.\n"));
302 if (context
->internal
->files
) {
303 DEBUG(1, ("Active files in context, "
304 "free_context failed.\n"));
311 /* Things we have to clean up */
312 smbc_setWorkgroup(context
, NULL
);
313 smbc_setNetbiosName(context
, NULL
);
314 smbc_setUser(context
, NULL
);
316 DEBUG(3, ("Context %p successfully freed\n", context
));
318 /* Free any DFS auth context. */
319 TALLOC_FREE(context
->internal
->auth_info
);
321 SAFE_FREE(context
->internal
);
324 /* Protect access to the count of contexts in use */
325 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
326 smb_panic("error locking 'initialized_ctx_count'");
329 if (initialized_ctx_count
) {
330 initialized_ctx_count
--;
333 if (initialized_ctx_count
== 0) {
334 SMBC_module_terminate();
337 /* Unlock the mutex */
338 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
339 smb_panic("error unlocking 'initialized_ctx_count'");
348 * Deprecated interface. Do not use. Instead, use the various
349 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
352 smbc_option_set(SMBCCTX
*context
,
354 ... /* option_value */)
360 smbc_get_auth_data_with_context_fn auth_fn
;
365 TALLOC_CTX
*frame
= talloc_stackframe();
367 va_start(ap
, option_name
);
369 if (strcmp(option_name
, "debug_to_stderr") == 0) {
370 option_value
.b
= (bool) va_arg(ap
, int);
371 smbc_setOptionDebugToStderr(context
, option_value
.b
);
373 } else if (strcmp(option_name
, "full_time_names") == 0) {
374 option_value
.b
= (bool) va_arg(ap
, int);
375 smbc_setOptionFullTimeNames(context
, option_value
.b
);
377 } else if (strcmp(option_name
, "open_share_mode") == 0) {
378 option_value
.i
= va_arg(ap
, int);
379 smbc_setOptionOpenShareMode(context
, option_value
.i
);
381 } else if (strcmp(option_name
, "auth_function") == 0) {
382 option_value
.auth_fn
=
383 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
384 smbc_setFunctionAuthDataWithContext(context
, option_value
.auth_fn
);
386 } else if (strcmp(option_name
, "user_data") == 0) {
387 option_value
.v
= va_arg(ap
, void *);
388 smbc_setOptionUserData(context
, option_value
.v
);
390 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
391 option_value
.s
= va_arg(ap
, const char *);
392 if (strcmp(option_value
.s
, "none") == 0) {
393 smbc_setOptionSmbEncryptionLevel(context
,
394 SMBC_ENCRYPTLEVEL_NONE
);
395 } else if (strcmp(option_value
.s
, "request") == 0) {
396 smbc_setOptionSmbEncryptionLevel(context
,
397 SMBC_ENCRYPTLEVEL_REQUEST
);
398 } else if (strcmp(option_value
.s
, "require") == 0) {
399 smbc_setOptionSmbEncryptionLevel(context
,
400 SMBC_ENCRYPTLEVEL_REQUIRE
);
403 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
404 option_value
.i
= va_arg(ap
, int);
405 smbc_setOptionBrowseMaxLmbCount(context
, option_value
.i
);
407 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
408 option_value
.b
= (bool) va_arg(ap
, int);
409 smbc_setOptionUrlEncodeReaddirEntries(context
, option_value
.b
);
411 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
412 option_value
.b
= (bool) va_arg(ap
, int);
413 smbc_setOptionOneSharePerServer(context
, option_value
.b
);
415 } else if (strcmp(option_name
, "use_kerberos") == 0) {
416 option_value
.b
= (bool) va_arg(ap
, int);
417 smbc_setOptionUseKerberos(context
, option_value
.b
);
419 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
420 option_value
.b
= (bool) va_arg(ap
, int);
421 smbc_setOptionFallbackAfterKerberos(context
, option_value
.b
);
423 } else if (strcmp(option_name
, "use_ccache") == 0) {
424 option_value
.b
= (bool) va_arg(ap
, int);
425 smbc_setOptionUseCCache(context
, option_value
.b
);
427 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
428 option_value
.b
= (bool) va_arg(ap
, int);
429 smbc_setOptionNoAutoAnonymousLogin(context
, option_value
.b
);
438 * Deprecated interface. Do not use. Instead, use the various
439 * smbc_getOption*() functions.
442 smbc_option_get(SMBCCTX
*context
,
445 if (strcmp(option_name
, "debug_stderr") == 0) {
446 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
447 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context
);
449 return (void *) smbc_getOptionDebugToStderr(context
);
452 } else if (strcmp(option_name
, "full_time_names") == 0) {
453 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
454 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context
);
456 return (void *) smbc_getOptionFullTimeNames(context
);
459 } else if (strcmp(option_name
, "open_share_mode") == 0) {
460 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
461 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context
);
463 return (void *) smbc_getOptionOpenShareMode(context
);
466 } else if (strcmp(option_name
, "auth_function") == 0) {
467 return (void *) smbc_getFunctionAuthDataWithContext(context
);
469 } else if (strcmp(option_name
, "user_data") == 0) {
470 return smbc_getOptionUserData(context
);
472 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
473 switch(smbc_getOptionSmbEncryptionLevel(context
))
476 return discard_const_p(void, "none");
478 return discard_const_p(void, "request");
480 return discard_const_p(void, "require");
483 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
485 unsigned int num_servers
= 0;
487 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
489 if (!smb1cli_conn_encryption_on(s
->cli
->conn
)) {
490 return (void *)false;
493 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
494 return (void *) (intptr_t) (bool) (num_servers
> 0);
496 return (void *) (bool) (num_servers
> 0);
499 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
500 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
501 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context
);
503 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
506 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
507 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
508 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context
);
510 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
513 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
514 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
515 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context
);
517 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
520 } else if (strcmp(option_name
, "use_kerberos") == 0) {
521 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
522 return (void *) (intptr_t) smbc_getOptionUseKerberos(context
);
524 return (void *) (bool) smbc_getOptionUseKerberos(context
);
527 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
528 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
529 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context
);
531 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
534 } else if (strcmp(option_name
, "use_ccache") == 0) {
535 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
536 return (void *) (intptr_t) smbc_getOptionUseCCache(context
);
538 return (void *) (bool) smbc_getOptionUseCCache(context
);
541 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
542 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
543 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context
);
545 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
554 * Initialize the library, etc.
556 * We accept a struct containing handle information.
557 * valid values for info->debug from 0 to 100,
558 * and insist that info->fn must be non-null.
561 smbc_init_context(SMBCCTX
*context
)
571 /* Do not initialise the same client twice */
572 if (context
->internal
->initialized
) {
576 frame
= talloc_stackframe();
578 if ((!smbc_getFunctionAuthData(context
) &&
579 !smbc_getFunctionAuthDataWithContext(context
)) ||
580 smbc_getDebug(context
) < 0 ||
581 smbc_getDebug(context
) > 100) {
589 if (!smbc_getUser(context
)) {
591 * FIXME: Is this the best way to get the user info?
593 char *user
= getenv("USER");
594 /* walk around as "guest" if no username can be found */
596 user
= SMB_STRDUP("guest");
598 user
= SMB_STRDUP(user
);
607 smbc_setUser(context
, user
);
610 if (!smbc_getUser(context
)) {
617 if (!smbc_getNetbiosName(context
)) {
619 * We try to get our netbios name from the config. If that
620 * fails we fall back on constructing our netbios name from
624 if (lp_netbios_name()) {
625 netbios_name
= SMB_STRDUP(lp_netbios_name());
628 * Hmmm, I want to get hostname as well, but I am too
629 * lazy for the moment
632 netbios_name
= (char *)SMB_MALLOC(17);
638 slprintf(netbios_name
, 16,
639 "smbc%s%d", smbc_getUser(context
), pid
);
648 smbc_setNetbiosName(context
, netbios_name
);
649 SAFE_FREE(netbios_name
);
651 if (!smbc_getNetbiosName(context
)) {
658 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context
)));
660 if (!smbc_getWorkgroup(context
)) {
663 if (lp_workgroup()) {
664 workgroup
= SMB_STRDUP(lp_workgroup());
667 /* TODO: Think about a decent default workgroup */
668 workgroup
= SMB_STRDUP("samba");
677 smbc_setWorkgroup(context
, workgroup
);
678 SAFE_FREE(workgroup
);
680 if (!smbc_getWorkgroup(context
)) {
687 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context
)));
689 /* shortest timeout is 1 second */
690 if (smbc_getTimeout(context
) > 0 && smbc_getTimeout(context
) < 1000)
691 smbc_setTimeout(context
, 1000);
693 context
->internal
->initialized
= True
;
695 /* Protect access to the count of contexts in use */
696 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
697 smb_panic("error locking 'initialized_ctx_count'");
700 initialized_ctx_count
++;
702 /* Unlock the mutex */
703 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
704 smb_panic("error unlocking 'initialized_ctx_count'");
712 /* Return the verion of samba, and thus libsmbclient */
716 return samba_version_string();
720 * Set the credentials so DFS will work when following referrals.
721 * This function is broken and must be removed. No SMBCCTX arg...
726 smbc_set_credentials(const char *workgroup
,
728 const char *password
,
729 smbc_bool use_kerberos
,
730 const char *signing_state
)
732 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
735 void smbc_set_credentials_with_fallback(SMBCCTX
*context
,
736 const char *workgroup
,
738 const char *password
)
740 smbc_bool use_kerberos
= false;
741 const char *signing_state
= "off";
742 struct user_auth_info
*auth_info
= NULL
;
750 frame
= talloc_stackframe();
752 if (! workgroup
|| ! *workgroup
) {
753 workgroup
= smbc_getWorkgroup(context
);
757 user
= smbc_getUser(context
);
764 auth_info
= user_auth_info_init(NULL
);
767 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
772 if (smbc_getOptionUseKerberos(context
)) {
776 if (lp_client_signing() != SMB_SIGNING_OFF
) {
777 signing_state
= "if_required";
780 if (lp_client_signing() == SMB_SIGNING_REQUIRED
) {
781 signing_state
= "required";
784 set_cmdline_auth_info_username(auth_info
, user
);
785 set_cmdline_auth_info_domain(auth_info
, workgroup
);
786 set_cmdline_auth_info_password(auth_info
, password
);
787 set_cmdline_auth_info_use_kerberos(auth_info
, use_kerberos
);
788 set_cmdline_auth_info_signing_state(auth_info
, signing_state
);
789 set_cmdline_auth_info_fallback_after_kerberos(auth_info
,
790 smbc_getOptionFallbackAfterKerberos(context
));
791 set_cmdline_auth_info_use_ccache(
792 auth_info
, smbc_getOptionUseCCache(context
));
794 TALLOC_FREE(context
->internal
->auth_info
);
796 context
->internal
->auth_info
= auth_info
;