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
10 Copyright (C) SATOH Fumiyasu <fumiyas@osstech.co.jp> 2009.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
32 * Check a server for being alive and well.
33 * returns 0 if the server is in shape. Returns 1 on error
35 * Also useable outside libsmbclient to enable external cache
36 * to do some checks too.
39 SMBC_check_server(SMBCCTX * context,
46 return (getpeername(server->cli->fd, &addr, &size) == -1);
50 * Remove a server from the cached server list it's unused.
51 * On success, 0 is returned. 1 is returned if the server could not be removed.
53 * Also useable outside libsmbclient
56 SMBC_remove_unused_server(SMBCCTX * context,
61 /* are we being fooled ? */
62 if (!context || !context->internal->initialized || !srv) {
66 /* Check all open files/directories for a relation with this server */
67 for (file = context->internal->files; file; file = file->next) {
68 if (file->srv == srv) {
70 DEBUG(3, ("smbc_remove_usused_server: "
71 "%p still used by %p.\n",
77 DLIST_REMOVE(context->internal->servers, srv);
79 cli_shutdown(srv->cli);
82 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
84 smbc_getFunctionRemoveCachedServer(context)(context, srv);
90 /****************************************************************
91 * Call the auth_fn with fixed size (fstring) buffers.
92 ***************************************************************/
94 SMBC_call_auth_fn(TALLOC_CTX *ctx,
105 smbc_get_auth_data_with_context_fn auth_with_context_fn;
107 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
108 strlcpy(username, *pp_username, sizeof(username));
109 strlcpy(password, *pp_password, sizeof(password));
111 /* See if there's an authentication with context function provided */
112 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
113 if (auth_with_context_fn)
115 (* auth_with_context_fn)(context,
117 workgroup, sizeof(workgroup),
118 username, sizeof(username),
119 password, sizeof(password));
123 smbc_getFunctionAuthData(context)(server, share,
124 workgroup, sizeof(workgroup),
125 username, sizeof(username),
126 password, sizeof(password));
129 TALLOC_FREE(*pp_workgroup);
130 TALLOC_FREE(*pp_username);
131 TALLOC_FREE(*pp_password);
133 *pp_workgroup = talloc_strdup(ctx, workgroup);
134 *pp_username = talloc_strdup(ctx, username);
135 *pp_password = talloc_strdup(ctx, password);
140 SMBC_get_auth_data(const char *server, const char *share,
141 char *workgroup_buf, int workgroup_buf_len,
142 char *username_buf, int username_buf_len,
143 char *password_buf, int password_buf_len)
145 /* Default function just uses provided data. Nothing to do. */
151 SMBC_find_server(TALLOC_CTX *ctx,
162 if (!pp_workgroup || !pp_username || !pp_password) {
168 srv = smbc_getFunctionGetCachedServer(context)(context,
173 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
174 !*pp_password || !(*pp_password)[0])) {
175 SMBC_call_auth_fn(ctx, context, server, share,
176 pp_workgroup, pp_username, pp_password);
179 * However, smbc_auth_fn may have picked up info relating to
180 * an existing connection, so try for an existing connection
184 goto check_server_cache;
189 if (smbc_getFunctionCheckServer(context)(context, srv)) {
191 * This server is no good anymore
192 * Try to remove it and check for more possible
193 * servers in the cache
195 if (smbc_getFunctionRemoveUnusedServer(context)(context,
198 * We could not remove the server completely,
199 * remove it from the cache so we will not get
200 * it again. It will be removed when the last
201 * file/dir is closed.
203 smbc_getFunctionRemoveCachedServer(context)(context,
208 * Maybe there are more cached connections to this
211 goto check_server_cache;
221 * Connect to a server, possibly on an existing connection
223 * Here, what we want to do is: If the server and username
224 * match an existing connection, reuse that, otherwise, establish a
227 * If we have to create a new connection, call the auth_fn to get the
228 * info we need, unless the username and password were passed in.
232 SMBC_server_internal(TALLOC_CTX *ctx,
234 bool connect_if_not_found,
243 char *workgroup = NULL;
245 struct nmb_name called, calling;
246 const char *server_n = server;
247 struct sockaddr_storage ss;
248 int tried_reverse = 0;
251 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
253 const char *username_used;
255 char *newserver, *newshare;
261 if (server[0] == 0) {
266 /* Look for a cached connection */
267 srv = SMBC_find_server(ctx, context, server, share,
268 pp_workgroup, pp_username, pp_password);
271 * If we found a connection and we're only allowed one share per
276 smbc_getOptionOneSharePerServer(context)) {
279 * ... then if there's no current connection to the share,
280 * connect to it. SMBC_find_server(), or rather the function
281 * pointed to by context->get_cached_srv_fn which
282 * was called by SMBC_find_server(), will have issued a tree
283 * disconnect if the requested share is not the same as the
284 * one that was already connected.
288 * Use srv->cli->desthost and srv->cli->share instead of
289 * server and share below to connect to the actual share,
290 * i.e., a normal share or a referred share from
291 * 'msdfs proxy' share.
293 if (srv->cli->cnum == (uint16) -1) {
294 /* Ensure we have accurate auth info */
295 SMBC_call_auth_fn(ctx, context,
302 if (!*pp_workgroup || !*pp_username || !*pp_password) {
304 cli_shutdown(srv->cli);
306 smbc_getFunctionRemoveCachedServer(context)(context,
312 * We don't need to renegotiate encryption
313 * here as the encryption context is not per
317 status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
319 strlen(*pp_password)+1);
320 if (!NT_STATUS_IS_OK(status)) {
321 errno = map_errno_from_nt_status(status);
322 cli_shutdown(srv->cli);
324 smbc_getFunctionRemoveCachedServer(context)(context,
329 /* Determine if this share supports case sensitivity */
332 ("IPC$ so ignore case sensitivity\n"));
333 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
334 DEBUG(4, ("Could not retrieve "
335 "case sensitivity flag: %s.\n",
339 * We can't determine the case sensitivity of
340 * the share. We have no choice but to use the
341 * user-specified case sensitivity setting.
343 if (smbc_getOptionCaseSensitive(context)) {
344 cli_set_case_sensitive(c, True);
346 cli_set_case_sensitive(c, False);
350 ("Case sensitive: %s\n",
351 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
354 cli_set_case_sensitive(
356 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
362 * Regenerate the dev value since it's based on both
366 srv->dev = (dev_t)(str_checksum(srv->cli->desthost) ^
367 str_checksum(srv->cli->share));
372 /* If we have a connection... */
375 /* ... then we're done here. Give 'em what they came for. */
380 /* If we're not asked to connect when a connection doesn't exist... */
381 if (! connect_if_not_found) {
382 /* ... then we're done here. */
386 if (!*pp_workgroup || !*pp_username || !*pp_password) {
391 make_nmb_name(&calling, smbc_getNetbiosName(context), 0x0);
392 make_nmb_name(&called , server, 0x20);
394 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
396 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
402 /* have to open a new connection */
403 if ((c = cli_initialise()) == NULL) {
408 if (smbc_getOptionUseKerberos(context)) {
409 c->use_kerberos = True;
412 if (smbc_getOptionFallbackAfterKerberos(context)) {
413 c->fallback_after_kerberos = True;
416 c->timeout = smbc_getTimeout(context);
419 * Force use of port 139 for first try if share is $IPC, empty, or
420 * null, so browse lists can work
422 if (share == NULL || *share == '\0' || is_ipc) {
423 port_try_first = 139;
426 port_try_first = 445;
430 c->port = port_try_first;
432 status = cli_connect(c, server_n, &ss);
433 if (!NT_STATUS_IS_OK(status)) {
435 /* First connection attempt failed. Try alternate port. */
436 c->port = port_try_next;
438 status = cli_connect(c, server_n, &ss);
439 if (!NT_STATUS_IS_OK(status)) {
446 if (!cli_session_request(c, &calling, &called)) {
448 if (strcmp(called.name, "*SMBSERVER")) {
449 make_nmb_name(&called , "*SMBSERVER", 0x20);
451 } else { /* Try one more time, but ensure we don't loop */
453 /* Only try this if server is an IP address ... */
455 if (is_ipaddress(server) && !tried_reverse) {
457 struct sockaddr_storage rem_ss;
459 if (!interpret_string_addr(&rem_ss, server,
461 DEBUG(4, ("Could not convert IP address "
462 "%s to struct sockaddr_storage\n",
468 tried_reverse++; /* Yuck */
470 if (name_status_find("*", 0, 0,
471 &rem_ss, remote_name)) {
472 make_nmb_name(&called,
483 DEBUG(4,(" session request ok\n"));
485 status = cli_negprot(c);
487 if (!NT_STATUS_IS_OK(status)) {
493 username_used = *pp_username;
495 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
497 strlen(*pp_password),
499 strlen(*pp_password),
502 /* Failed. Try an anonymous login, if allowed by flags. */
505 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
506 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
517 status = cli_init_creds(c, username_used,
518 *pp_workgroup, *pp_password);
519 if (!NT_STATUS_IS_OK(status)) {
520 errno = map_errno_from_nt_status(status);
525 DEBUG(4,(" session setup ok\n"));
527 /* here's the fun part....to support 'msdfs proxy' shares
528 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
529 here before trying to connect to the original share.
530 cli_check_msdfs_proxy() will fail if it is a normal share. */
532 if ((c->capabilities & CAP_DFS) &&
533 cli_check_msdfs_proxy(ctx, c, share,
534 &newserver, &newshare,
535 /* FIXME: cli_check_msdfs_proxy() does
536 not support smbc_smb_encrypt_level type */
537 context->internal->smb_encryption_level ?
543 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
544 newserver, newshare, pp_workgroup,
545 pp_username, pp_password, in_cache);
546 TALLOC_FREE(newserver);
547 TALLOC_FREE(newshare);
551 /* must be a normal share */
553 status = cli_tcon_andx(c, share, "?????", *pp_password,
554 strlen(*pp_password)+1);
555 if (!NT_STATUS_IS_OK(status)) {
556 errno = map_errno_from_nt_status(status);
561 DEBUG(4,(" tconx ok\n"));
563 /* Determine if this share supports case sensitivity */
565 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
566 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
567 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
571 * We can't determine the case sensitivity of the share. We
572 * have no choice but to use the user-specified case
573 * sensitivity setting.
575 if (smbc_getOptionCaseSensitive(context)) {
576 cli_set_case_sensitive(c, True);
578 cli_set_case_sensitive(c, False);
581 DEBUG(4, ("Case sensitive: %s\n",
582 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
585 cli_set_case_sensitive(c,
586 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
591 if (context->internal->smb_encryption_level) {
592 /* Attempt UNIX smb encryption. */
593 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
599 * context->smb_encryption_level == 1
600 * means don't fail if encryption can't be negotiated,
601 * == 2 means fail if encryption can't be negotiated.
604 DEBUG(4,(" SMB encrypt failed\n"));
606 if (context->internal->smb_encryption_level == 2) {
612 DEBUG(4,(" SMB encrypt ok\n"));
616 * Ok, we have got a nice connection
617 * Let's allocate a server structure.
620 srv = SMB_MALLOC_P(SMBCSRV);
629 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
630 srv->no_pathinfo = False;
631 srv->no_pathinfo2 = False;
632 srv->no_nt_session = False;
635 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
636 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
638 workgroup = *pp_workgroup;
644 /* set the credentials to make DFS work */
645 smbc_set_credentials_with_fallback(context,
654 SMBC_server(TALLOC_CTX *ctx,
656 bool connect_if_not_found,
664 bool in_cache = false;
666 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
667 server, share, pp_workgroup,
668 pp_username, pp_password, &in_cache);
677 /* Now add it to the cache (internal or external) */
678 /* Let the cache function set errno if it wants to */
680 if (smbc_getFunctionAddCachedServer(context)(context, srv,
684 int saved_errno = errno;
685 DEBUG(3, (" Failed to add server to cache\n"));
694 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
695 server, share, srv));
697 DLIST_ADD(context->internal->servers, srv);
702 * Connect to a server for getting/setting attributes, possibly on an existing
703 * connection. This works similarly to SMBC_server().
706 SMBC_attr_server(TALLOC_CTX *ctx,
715 struct sockaddr_storage ss;
716 struct cli_state *ipc_cli = NULL;
717 struct rpc_pipe_client *pipe_hnd = NULL;
720 SMBCSRV *ipc_srv=NULL;
723 * Use srv->cli->desthost and srv->cli->share instead of
724 * server and share below to connect to the actual share,
725 * i.e., a normal share or a referred share from
726 * 'msdfs proxy' share.
728 srv = SMBC_server(ctx, context, true, server, share,
729 pp_workgroup, pp_username, pp_password);
733 server = srv->cli->desthost;
734 share = srv->cli->share;
737 * See if we've already created this special connection. Reference
738 * our "special" share name '*IPC$', which is an impossible real share
739 * name due to the leading asterisk.
741 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
742 pp_workgroup, pp_username, pp_password);
745 /* We didn't find a cached connection. Get the password */
746 if (!*pp_password || (*pp_password)[0] == '\0') {
747 /* ... then retrieve it now. */
748 SMBC_call_auth_fn(ctx, context, server, share,
752 if (!*pp_workgroup || !*pp_username || !*pp_password) {
759 if (smbc_getOptionUseKerberos(context)) {
760 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
764 nt_status = cli_full_connection(&ipc_cli,
765 global_myname(), server,
766 &ss, 0, "IPC$", "?????",
772 if (! NT_STATUS_IS_OK(nt_status)) {
773 DEBUG(1,("cli_full_connection failed! (%s)\n",
774 nt_errstr(nt_status)));
779 if (context->internal->smb_encryption_level) {
780 /* Attempt UNIX smb encryption. */
781 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
787 * context->smb_encryption_level ==
788 * 1 means don't fail if encryption can't be
789 * negotiated, == 2 means fail if encryption
790 * can't be negotiated.
793 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
795 if (context->internal->smb_encryption_level == 2) {
796 cli_shutdown(ipc_cli);
801 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
804 ipc_srv = SMB_MALLOC_P(SMBCSRV);
807 cli_shutdown(ipc_cli);
811 ZERO_STRUCTP(ipc_srv);
812 ipc_srv->cli = ipc_cli;
814 nt_status = cli_rpc_pipe_open_noauth(
815 ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
816 if (!NT_STATUS_IS_OK(nt_status)) {
817 DEBUG(1, ("cli_nt_session_open fail!\n"));
819 cli_shutdown(ipc_srv->cli);
825 * Some systems don't support
826 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
827 * so we might as well do it too.
830 nt_status = rpccli_lsa_open_policy(
834 GENERIC_EXECUTE_ACCESS,
837 if (!NT_STATUS_IS_OK(nt_status)) {
838 errno = SMBC_errno(context, ipc_srv->cli);
839 cli_shutdown(ipc_srv->cli);
843 /* now add it to the cache (internal or external) */
845 errno = 0; /* let cache function set errno if it likes */
846 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
851 DEBUG(3, (" Failed to add server to cache\n"));
855 cli_shutdown(ipc_srv->cli);
860 DLIST_ADD(context->internal->servers, ipc_srv);