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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Is the logging working / configfile read ?
33 static bool SMBC_initialized
= false;
34 static unsigned int initialized_ctx_count
= 0;
35 static void *initialized_ctx_count_mutex
= NULL
;
38 * Do some module- and library-wide intializations
41 SMBC_module_init(void * punused
)
43 bool conf_loaded
= False
;
45 TALLOC_CTX
*frame
= talloc_stackframe();
49 setup_logging("libsmbclient", True
);
51 /* Here we would open the smb.conf file if needed ... */
53 lp_set_in_client(True
);
55 home
= getenv("HOME");
58 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
59 if (lp_load(conf
, True
, False
, False
, True
)) {
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(get_dyn_CONFIGFILE(), True
, False
, False
, False
)) {
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(conf
, True
, False
, False
, False
)) {
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)
128 SMBC_initialized
= false;
133 * Get a new empty handle to fill in with your own info
136 smbc_new_context(void)
140 /* The first call to this function should initialize the module */
141 SMB_THREAD_ONCE(&SMBC_initialized
, SMBC_module_init
, NULL
);
144 * All newly added context fields should be placed in
145 * SMBC_internal_data, not directly in SMBCCTX.
147 context
= SMB_MALLOC_P(SMBCCTX
);
153 ZERO_STRUCTP(context
);
155 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
156 if (!context
->internal
) {
162 /* Initialize the context and establish reasonable defaults */
163 ZERO_STRUCTP(context
->internal
);
165 smbc_setDebug(context
, 0);
166 smbc_setTimeout(context
, 20000);
168 smbc_setOptionFullTimeNames(context
, False
);
169 smbc_setOptionOpenShareMode(context
, SMBC_SHAREMODE_DENY_NONE
);
170 smbc_setOptionSmbEncryptionLevel(context
, SMBC_ENCRYPTLEVEL_NONE
);
171 smbc_setOptionCaseSensitive(context
, False
);
172 smbc_setOptionBrowseMaxLmbCount(context
, 3); /* # LMBs to query */
173 smbc_setOptionUrlEncodeReaddirEntries(context
, False
);
174 smbc_setOptionOneSharePerServer(context
, False
);
176 smbc_setFunctionAuthData(context
, SMBC_get_auth_data
);
177 smbc_setFunctionCheckServer(context
, SMBC_check_server
);
178 smbc_setFunctionRemoveUnusedServer(context
, SMBC_remove_unused_server
);
180 smbc_setOptionUserData(context
, NULL
);
181 smbc_setFunctionAddCachedServer(context
, SMBC_add_cached_server
);
182 smbc_setFunctionGetCachedServer(context
, SMBC_get_cached_server
);
183 smbc_setFunctionRemoveCachedServer(context
, SMBC_remove_cached_server
);
184 smbc_setFunctionPurgeCachedServers(context
, SMBC_purge_cached_servers
);
186 smbc_setFunctionOpen(context
, SMBC_open_ctx
);
187 smbc_setFunctionCreat(context
, SMBC_creat_ctx
);
188 smbc_setFunctionRead(context
, SMBC_read_ctx
);
189 smbc_setFunctionWrite(context
, SMBC_write_ctx
);
190 smbc_setFunctionClose(context
, SMBC_close_ctx
);
191 smbc_setFunctionUnlink(context
, SMBC_unlink_ctx
);
192 smbc_setFunctionRename(context
, SMBC_rename_ctx
);
193 smbc_setFunctionLseek(context
, SMBC_lseek_ctx
);
194 smbc_setFunctionFtruncate(context
, SMBC_ftruncate_ctx
);
195 smbc_setFunctionStat(context
, SMBC_stat_ctx
);
196 smbc_setFunctionStatVFS(context
, SMBC_statvfs_ctx
);
197 smbc_setFunctionFstatVFS(context
, SMBC_fstatvfs_ctx
);
198 smbc_setFunctionFstat(context
, SMBC_fstat_ctx
);
199 smbc_setFunctionOpendir(context
, SMBC_opendir_ctx
);
200 smbc_setFunctionClosedir(context
, SMBC_closedir_ctx
);
201 smbc_setFunctionReaddir(context
, SMBC_readdir_ctx
);
202 smbc_setFunctionGetdents(context
, SMBC_getdents_ctx
);
203 smbc_setFunctionMkdir(context
, SMBC_mkdir_ctx
);
204 smbc_setFunctionRmdir(context
, SMBC_rmdir_ctx
);
205 smbc_setFunctionTelldir(context
, SMBC_telldir_ctx
);
206 smbc_setFunctionLseekdir(context
, SMBC_lseekdir_ctx
);
207 smbc_setFunctionFstatdir(context
, SMBC_fstatdir_ctx
);
208 smbc_setFunctionChmod(context
, SMBC_chmod_ctx
);
209 smbc_setFunctionUtimes(context
, SMBC_utimes_ctx
);
210 smbc_setFunctionSetxattr(context
, SMBC_setxattr_ctx
);
211 smbc_setFunctionGetxattr(context
, SMBC_getxattr_ctx
);
212 smbc_setFunctionRemovexattr(context
, SMBC_removexattr_ctx
);
213 smbc_setFunctionListxattr(context
, SMBC_listxattr_ctx
);
215 smbc_setFunctionOpenPrintJob(context
, SMBC_open_print_job_ctx
);
216 smbc_setFunctionPrintFile(context
, SMBC_print_file_ctx
);
217 smbc_setFunctionListPrintJobs(context
, SMBC_list_print_jobs_ctx
);
218 smbc_setFunctionUnlinkPrintJob(context
, SMBC_unlink_print_job_ctx
);
226 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
227 * and thus you'll be leaking memory if not handled properly.
231 smbc_free_context(SMBCCTX
*context
,
241 DEBUG(1,("Performing aggressive shutdown.\n"));
243 f
= context
->internal
->files
;
245 smbc_getFunctionClose(context
)(context
, f
);
248 context
->internal
->files
= NULL
;
250 /* First try to remove the servers the nice way. */
251 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
254 DEBUG(1, ("Could not purge all servers, "
255 "Nice way shutdown failed.\n"));
256 s
= context
->internal
->servers
;
258 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
260 cli_shutdown(s
->cli
);
261 smbc_getFunctionRemoveCachedServer(context
)(context
,
264 DLIST_REMOVE(context
->internal
->servers
, s
);
268 context
->internal
->servers
= NULL
;
272 /* This is the polite way */
273 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
274 DEBUG(1, ("Could not purge all servers, "
275 "free_context failed.\n"));
279 if (context
->internal
->servers
) {
280 DEBUG(1, ("Active servers in context, "
281 "free_context failed.\n"));
285 if (context
->internal
->files
) {
286 DEBUG(1, ("Active files in context, "
287 "free_context failed.\n"));
293 /* Things we have to clean up */
294 smbc_setWorkgroup(context
, NULL
);
295 smbc_setNetbiosName(context
, NULL
);
296 smbc_setUser(context
, NULL
);
298 DEBUG(3, ("Context %p successfully freed\n", context
));
300 /* Free any DFS auth context. */
301 TALLOC_FREE(context
->internal
->auth_info
);
303 SAFE_FREE(context
->internal
);
306 /* Protect access to the count of contexts in use */
307 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
308 smb_panic("error locking 'initialized_ctx_count'");
311 if (initialized_ctx_count
) {
312 initialized_ctx_count
--;
315 if (initialized_ctx_count
== 0) {
316 SMBC_module_terminate();
319 /* Unlock the mutex */
320 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
321 smb_panic("error unlocking 'initialized_ctx_count'");
329 * Deprecated interface. Do not use. Instead, use the various
330 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
333 smbc_option_set(SMBCCTX
*context
,
335 ... /* option_value */)
341 smbc_get_auth_data_with_context_fn auth_fn
;
346 va_start(ap
, option_name
);
348 if (strcmp(option_name
, "debug_to_stderr") == 0) {
349 option_value
.b
= (bool) va_arg(ap
, int);
350 smbc_setOptionDebugToStderr(context
, option_value
.b
);
352 } else if (strcmp(option_name
, "full_time_names") == 0) {
353 option_value
.b
= (bool) va_arg(ap
, int);
354 smbc_setOptionFullTimeNames(context
, option_value
.b
);
356 } else if (strcmp(option_name
, "open_share_mode") == 0) {
357 option_value
.i
= va_arg(ap
, int);
358 smbc_setOptionOpenShareMode(context
, option_value
.i
);
360 } else if (strcmp(option_name
, "auth_function") == 0) {
361 option_value
.auth_fn
=
362 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
363 smbc_setFunctionAuthDataWithContext(context
, option_value
.auth_fn
);
365 } else if (strcmp(option_name
, "user_data") == 0) {
366 option_value
.v
= va_arg(ap
, void *);
367 smbc_setOptionUserData(context
, option_value
.v
);
369 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
370 option_value
.s
= va_arg(ap
, const char *);
371 if (strcmp(option_value
.s
, "none") == 0) {
372 smbc_setOptionSmbEncryptionLevel(context
,
373 SMBC_ENCRYPTLEVEL_NONE
);
374 } else if (strcmp(option_value
.s
, "request") == 0) {
375 smbc_setOptionSmbEncryptionLevel(context
,
376 SMBC_ENCRYPTLEVEL_REQUEST
);
377 } else if (strcmp(option_value
.s
, "require") == 0) {
378 smbc_setOptionSmbEncryptionLevel(context
,
379 SMBC_ENCRYPTLEVEL_REQUIRE
);
382 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
383 option_value
.i
= va_arg(ap
, int);
384 smbc_setOptionBrowseMaxLmbCount(context
, option_value
.i
);
386 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
387 option_value
.b
= (bool) va_arg(ap
, int);
388 smbc_setOptionUrlEncodeReaddirEntries(context
, option_value
.b
);
390 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
391 option_value
.b
= (bool) va_arg(ap
, int);
392 smbc_setOptionOneSharePerServer(context
, option_value
.b
);
394 } else if (strcmp(option_name
, "use_kerberos") == 0) {
395 option_value
.b
= (bool) va_arg(ap
, int);
396 smbc_setOptionUseKerberos(context
, option_value
.b
);
398 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
399 option_value
.b
= (bool) va_arg(ap
, int);
400 smbc_setOptionFallbackAfterKerberos(context
, option_value
.b
);
402 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
403 option_value
.b
= (bool) va_arg(ap
, int);
404 smbc_setOptionNoAutoAnonymousLogin(context
, option_value
.b
);
412 * Deprecated interface. Do not use. Instead, use the various
413 * smbc_getOption*() functions.
416 smbc_option_get(SMBCCTX
*context
,
419 if (strcmp(option_name
, "debug_stderr") == 0) {
420 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
421 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context
);
423 return (void *) smbc_getOptionDebugToStderr(context
);
426 } else if (strcmp(option_name
, "full_time_names") == 0) {
427 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
428 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context
);
430 return (void *) smbc_getOptionFullTimeNames(context
);
433 } else if (strcmp(option_name
, "open_share_mode") == 0) {
434 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
435 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context
);
437 return (void *) smbc_getOptionOpenShareMode(context
);
440 } else if (strcmp(option_name
, "auth_function") == 0) {
441 return (void *) smbc_getFunctionAuthDataWithContext(context
);
443 } else if (strcmp(option_name
, "user_data") == 0) {
444 return smbc_getOptionUserData(context
);
446 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
447 switch(smbc_getOptionSmbEncryptionLevel(context
))
450 return (void *) "none";
452 return (void *) "request";
454 return (void *) "require";
457 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
459 unsigned int num_servers
= 0;
461 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
463 if (s
->cli
->trans_enc_state
== NULL
) {
464 return (void *)false;
467 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
468 return (void *) (intptr_t) (bool) (num_servers
> 0);
470 return (void *) (bool) (num_servers
> 0);
473 } else if (strcmp(option_name
, "browse_max_lmb_count") == 0) {
474 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
475 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context
);
477 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
480 } else if (strcmp(option_name
, "urlencode_readdir_entries") == 0) {
481 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
482 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context
);
484 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
487 } else if (strcmp(option_name
, "one_share_per_server") == 0) {
488 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
489 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context
);
491 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
494 } else if (strcmp(option_name
, "use_kerberos") == 0) {
495 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
496 return (void *) (intptr_t) smbc_getOptionUseKerberos(context
);
498 return (void *) (bool) smbc_getOptionUseKerberos(context
);
501 } else if (strcmp(option_name
, "fallback_after_kerberos") == 0) {
502 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
503 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context
);
505 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
508 } else if (strcmp(option_name
, "no_auto_anonymous_login") == 0) {
509 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
510 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context
);
512 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
521 * Initialize the library, etc.
523 * We accept a struct containing handle information.
524 * valid values for info->debug from 0 to 100,
525 * and insist that info->fn must be non-null.
528 smbc_init_context(SMBCCTX
*context
)
537 /* Do not initialise the same client twice */
538 if (context
->internal
->initialized
) {
542 if (context
->internal
->debug_stderr
) {
544 * Hmmm... Do we want a unique dbf per-thread? For now, we'll just
545 * leave it up to the user. If any one context spefies debug to
546 * stderr then all will be.
549 x_setbuf(x_stderr
, NULL
);
552 if ((!smbc_getFunctionAuthData(context
) &&
553 !smbc_getFunctionAuthDataWithContext(context
)) ||
554 smbc_getDebug(context
) < 0 ||
555 smbc_getDebug(context
) > 100) {
562 if (!smbc_getUser(context
)) {
564 * FIXME: Is this the best way to get the user info?
566 char *user
= getenv("USER");
567 /* walk around as "guest" if no username can be found */
569 user
= SMB_STRDUP("guest");
571 user
= SMB_STRDUP(user
);
579 smbc_setUser(context
, user
);
582 if (!smbc_getUser(context
)) {
588 if (!smbc_getNetbiosName(context
)) {
590 * We try to get our netbios name from the config. If that
591 * fails we fall back on constructing our netbios name from
595 if (global_myname()) {
596 netbios_name
= SMB_STRDUP(global_myname());
599 * Hmmm, I want to get hostname as well, but I am too
600 * lazy for the moment
603 netbios_name
= (char *)SMB_MALLOC(17);
608 slprintf(netbios_name
, 16,
609 "smbc%s%d", smbc_getUser(context
), pid
);
617 smbc_setNetbiosName(context
, netbios_name
);
618 SAFE_FREE(netbios_name
);
620 if (!smbc_getNetbiosName(context
)) {
626 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context
)));
628 if (!smbc_getWorkgroup(context
)) {
631 if (lp_workgroup()) {
632 workgroup
= SMB_STRDUP(lp_workgroup());
635 /* TODO: Think about a decent default workgroup */
636 workgroup
= SMB_STRDUP("samba");
644 smbc_setWorkgroup(context
, workgroup
);
645 SAFE_FREE(workgroup
);
647 if (!smbc_getWorkgroup(context
)) {
653 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context
)));
655 /* shortest timeout is 1 second */
656 if (smbc_getTimeout(context
) > 0 && smbc_getTimeout(context
) < 1000)
657 smbc_setTimeout(context
, 1000);
659 context
->internal
->initialized
= True
;
661 /* Protect access to the count of contexts in use */
662 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex
) != 0) {
663 smb_panic("error locking 'initialized_ctx_count'");
666 initialized_ctx_count
++;
668 /* Unlock the mutex */
669 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex
) != 0) {
670 smb_panic("error unlocking 'initialized_ctx_count'");
677 /* Return the verion of samba, and thus libsmbclient */
681 return samba_version_string();
685 * Set the credentials so DFS will work when following referrals.
686 * This function is broken and must be removed. No SMBCCTX arg...
691 smbc_set_credentials(const char *workgroup
,
693 const char *password
,
694 smbc_bool use_kerberos
,
695 const char *signing_state
)
697 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
700 void smbc_set_credentials_with_fallback(SMBCCTX
*context
,
701 const char *workgroup
,
703 const char *password
)
705 smbc_bool use_kerberos
= false;
706 const char *signing_state
= "off";
707 struct user_auth_info
*auth_info
= NULL
;
714 if (! workgroup
|| ! *workgroup
) {
715 workgroup
= smbc_getWorkgroup(context
);
719 user
= smbc_getUser(context
);
726 auth_info
= user_auth_info_init(NULL
);
729 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
733 if (smbc_getOptionUseKerberos(context
)) {
737 if (lp_client_signing()) {
738 signing_state
= "on";
741 if (lp_client_signing() == Required
) {
742 signing_state
= "force";
745 set_cmdline_auth_info_username(auth_info
, user
);
746 set_cmdline_auth_info_password(auth_info
, password
);
747 set_cmdline_auth_info_use_kerberos(auth_info
, use_kerberos
);
748 set_cmdline_auth_info_signing_state(auth_info
, signing_state
);
749 set_cmdline_auth_info_fallback_after_kerberos(auth_info
,
750 smbc_getOptionFallbackAfterKerberos(context
));
751 set_global_myworkgroup(workgroup
);
753 TALLOC_FREE(context
->internal
->auth_info
);
755 context
->internal
->auth_info
= auth_info
;