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"
29 #include "../librpc/gen_ndr/ndr_lsa.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "libcli/security/security.h"
33 #include "libsmb/nmblib.h"
36 * Check a server for being alive and well.
37 * returns 0 if the server is in shape. Returns 1 on error
39 * Also useable outside libsmbclient to enable external cache
40 * to do some checks too.
43 SMBC_check_server(SMBCCTX * context,
50 return (getpeername(server->cli->fd, &addr, &size) == -1);
54 * Remove a server from the cached server list it's unused.
55 * On success, 0 is returned. 1 is returned if the server could not be removed.
57 * Also useable outside libsmbclient
60 SMBC_remove_unused_server(SMBCCTX * context,
65 /* are we being fooled ? */
66 if (!context || !context->internal->initialized || !srv) {
70 /* Check all open files/directories for a relation with this server */
71 for (file = context->internal->files; file; file = file->next) {
72 if (file->srv == srv) {
74 DEBUG(3, ("smbc_remove_usused_server: "
75 "%p still used by %p.\n",
81 DLIST_REMOVE(context->internal->servers, srv);
83 cli_shutdown(srv->cli);
86 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
88 smbc_getFunctionRemoveCachedServer(context)(context, srv);
94 /****************************************************************
95 * Call the auth_fn with fixed size (fstring) buffers.
96 ***************************************************************/
98 SMBC_call_auth_fn(TALLOC_CTX *ctx,
109 smbc_get_auth_data_with_context_fn auth_with_context_fn;
111 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
112 strlcpy(username, *pp_username, sizeof(username));
113 strlcpy(password, *pp_password, sizeof(password));
115 /* See if there's an authentication with context function provided */
116 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
117 if (auth_with_context_fn)
119 (* auth_with_context_fn)(context,
121 workgroup, sizeof(workgroup),
122 username, sizeof(username),
123 password, sizeof(password));
127 smbc_getFunctionAuthData(context)(server, share,
128 workgroup, sizeof(workgroup),
129 username, sizeof(username),
130 password, sizeof(password));
133 TALLOC_FREE(*pp_workgroup);
134 TALLOC_FREE(*pp_username);
135 TALLOC_FREE(*pp_password);
137 *pp_workgroup = talloc_strdup(ctx, workgroup);
138 *pp_username = talloc_strdup(ctx, username);
139 *pp_password = talloc_strdup(ctx, password);
144 SMBC_get_auth_data(const char *server, const char *share,
145 char *workgroup_buf, int workgroup_buf_len,
146 char *username_buf, int username_buf_len,
147 char *password_buf, int password_buf_len)
149 /* Default function just uses provided data. Nothing to do. */
155 SMBC_find_server(TALLOC_CTX *ctx,
166 if (!pp_workgroup || !pp_username || !pp_password) {
172 srv = smbc_getFunctionGetCachedServer(context)(context,
177 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
178 !*pp_password || !(*pp_password)[0])) {
179 SMBC_call_auth_fn(ctx, context, server, share,
180 pp_workgroup, pp_username, pp_password);
183 * However, smbc_auth_fn may have picked up info relating to
184 * an existing connection, so try for an existing connection
188 goto check_server_cache;
193 if (smbc_getFunctionCheckServer(context)(context, srv)) {
195 * This server is no good anymore
196 * Try to remove it and check for more possible
197 * servers in the cache
199 if (smbc_getFunctionRemoveUnusedServer(context)(context,
202 * We could not remove the server completely,
203 * remove it from the cache so we will not get
204 * it again. It will be removed when the last
205 * file/dir is closed.
207 smbc_getFunctionRemoveCachedServer(context)(context,
212 * Maybe there are more cached connections to this
215 goto check_server_cache;
225 * Connect to a server, possibly on an existing connection
227 * Here, what we want to do is: If the server and username
228 * match an existing connection, reuse that, otherwise, establish a
231 * If we have to create a new connection, call the auth_fn to get the
232 * info we need, unless the username and password were passed in.
236 SMBC_server_internal(TALLOC_CTX *ctx,
238 bool connect_if_not_found,
247 char *workgroup = NULL;
249 struct nmb_name called, calling;
250 const char *server_n = server;
251 struct sockaddr_storage ss;
252 int tried_reverse = 0;
255 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
257 const char *username_used;
259 char *newserver, *newshare;
265 if (server[0] == 0) {
270 /* Look for a cached connection */
271 srv = SMBC_find_server(ctx, context, server, share,
272 pp_workgroup, pp_username, pp_password);
275 * If we found a connection and we're only allowed one share per
280 smbc_getOptionOneSharePerServer(context)) {
283 * ... then if there's no current connection to the share,
284 * connect to it. SMBC_find_server(), or rather the function
285 * pointed to by context->get_cached_srv_fn which
286 * was called by SMBC_find_server(), will have issued a tree
287 * disconnect if the requested share is not the same as the
288 * one that was already connected.
292 * Use srv->cli->desthost and srv->cli->share instead of
293 * server and share below to connect to the actual share,
294 * i.e., a normal share or a referred share from
295 * 'msdfs proxy' share.
297 if (srv->cli->cnum == (uint16) -1) {
298 /* Ensure we have accurate auth info */
299 SMBC_call_auth_fn(ctx, context,
306 if (!*pp_workgroup || !*pp_username || !*pp_password) {
308 cli_shutdown(srv->cli);
310 smbc_getFunctionRemoveCachedServer(context)(context,
316 * We don't need to renegotiate encryption
317 * here as the encryption context is not per
321 status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
323 strlen(*pp_password)+1);
324 if (!NT_STATUS_IS_OK(status)) {
325 errno = map_errno_from_nt_status(status);
326 cli_shutdown(srv->cli);
328 smbc_getFunctionRemoveCachedServer(context)(context,
333 /* Determine if this share supports case sensitivity */
336 ("IPC$ so ignore case sensitivity\n"));
337 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
338 DEBUG(4, ("Could not retrieve "
339 "case sensitivity flag: %s.\n",
343 * We can't determine the case sensitivity of
344 * the share. We have no choice but to use the
345 * user-specified case sensitivity setting.
347 if (smbc_getOptionCaseSensitive(context)) {
348 cli_set_case_sensitive(c, True);
350 cli_set_case_sensitive(c, False);
354 ("Case sensitive: %s\n",
355 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
358 cli_set_case_sensitive(
360 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
366 * Regenerate the dev value since it's based on both
370 srv->dev = (dev_t)(str_checksum(srv->cli->desthost) ^
371 str_checksum(srv->cli->share));
376 /* If we have a connection... */
379 /* ... then we're done here. Give 'em what they came for. */
384 /* If we're not asked to connect when a connection doesn't exist... */
385 if (! connect_if_not_found) {
386 /* ... then we're done here. */
390 if (!*pp_workgroup || !*pp_username || !*pp_password) {
395 make_nmb_name(&calling, smbc_getNetbiosName(context), 0x0);
396 make_nmb_name(&called , server, 0x20);
398 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
400 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
406 /* have to open a new connection */
407 if ((c = cli_initialise()) == NULL) {
412 if (smbc_getOptionUseKerberos(context)) {
413 c->use_kerberos = True;
416 if (smbc_getOptionFallbackAfterKerberos(context)) {
417 c->fallback_after_kerberos = True;
420 if (smbc_getOptionUseCCache(context)) {
421 c->use_ccache = True;
424 c->timeout = smbc_getTimeout(context);
427 * Force use of port 139 for first try if share is $IPC, empty, or
428 * null, so browse lists can work
430 if (share == NULL || *share == '\0' || is_ipc) {
431 port_try_first = 139;
434 port_try_first = 445;
438 c->port = port_try_first;
440 status = cli_connect(c, server_n, &ss);
441 if (!NT_STATUS_IS_OK(status)) {
443 /* First connection attempt failed. Try alternate port. */
444 c->port = port_try_next;
446 status = cli_connect(c, server_n, &ss);
447 if (!NT_STATUS_IS_OK(status)) {
454 if (!cli_session_request(c, &calling, &called)) {
456 if (strcmp(called.name, "*SMBSERVER")) {
457 make_nmb_name(&called , "*SMBSERVER", 0x20);
459 } else { /* Try one more time, but ensure we don't loop */
461 /* Only try this if server is an IP address ... */
463 if (is_ipaddress(server) && !tried_reverse) {
465 struct sockaddr_storage rem_ss;
467 if (!interpret_string_addr(&rem_ss, server,
469 DEBUG(4, ("Could not convert IP address "
470 "%s to struct sockaddr_storage\n",
476 tried_reverse++; /* Yuck */
478 if (name_status_find("*", 0, 0,
479 &rem_ss, remote_name)) {
480 make_nmb_name(&called,
491 DEBUG(4,(" session request ok\n"));
493 status = cli_negprot(c);
495 if (!NT_STATUS_IS_OK(status)) {
501 username_used = *pp_username;
503 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
505 strlen(*pp_password),
507 strlen(*pp_password),
510 /* Failed. Try an anonymous login, if allowed by flags. */
513 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
514 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
525 status = cli_init_creds(c, username_used,
526 *pp_workgroup, *pp_password);
527 if (!NT_STATUS_IS_OK(status)) {
528 errno = map_errno_from_nt_status(status);
533 DEBUG(4,(" session setup ok\n"));
535 /* here's the fun part....to support 'msdfs proxy' shares
536 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
537 here before trying to connect to the original share.
538 cli_check_msdfs_proxy() will fail if it is a normal share. */
540 if ((c->capabilities & CAP_DFS) &&
541 cli_check_msdfs_proxy(ctx, c, share,
542 &newserver, &newshare,
543 /* FIXME: cli_check_msdfs_proxy() does
544 not support smbc_smb_encrypt_level type */
545 context->internal->smb_encryption_level ?
551 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
552 newserver, newshare, pp_workgroup,
553 pp_username, pp_password, in_cache);
554 TALLOC_FREE(newserver);
555 TALLOC_FREE(newshare);
559 /* must be a normal share */
561 status = cli_tcon_andx(c, share, "?????", *pp_password,
562 strlen(*pp_password)+1);
563 if (!NT_STATUS_IS_OK(status)) {
564 errno = map_errno_from_nt_status(status);
569 DEBUG(4,(" tconx ok\n"));
571 /* Determine if this share supports case sensitivity */
573 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
574 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
575 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
579 * We can't determine the case sensitivity of the share. We
580 * have no choice but to use the user-specified case
581 * sensitivity setting.
583 if (smbc_getOptionCaseSensitive(context)) {
584 cli_set_case_sensitive(c, True);
586 cli_set_case_sensitive(c, False);
589 DEBUG(4, ("Case sensitive: %s\n",
590 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
593 cli_set_case_sensitive(c,
594 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
599 if (context->internal->smb_encryption_level) {
600 /* Attempt UNIX smb encryption. */
601 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
607 * context->smb_encryption_level == 1
608 * means don't fail if encryption can't be negotiated,
609 * == 2 means fail if encryption can't be negotiated.
612 DEBUG(4,(" SMB encrypt failed\n"));
614 if (context->internal->smb_encryption_level == 2) {
620 DEBUG(4,(" SMB encrypt ok\n"));
624 * Ok, we have got a nice connection
625 * Let's allocate a server structure.
628 srv = SMB_MALLOC_P(SMBCSRV);
637 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
638 srv->no_pathinfo = False;
639 srv->no_pathinfo2 = False;
640 srv->no_nt_session = False;
643 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
644 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
646 workgroup = *pp_workgroup;
652 /* set the credentials to make DFS work */
653 smbc_set_credentials_with_fallback(context,
662 SMBC_server(TALLOC_CTX *ctx,
664 bool connect_if_not_found,
672 bool in_cache = false;
674 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
675 server, share, pp_workgroup,
676 pp_username, pp_password, &in_cache);
685 /* Now add it to the cache (internal or external) */
686 /* Let the cache function set errno if it wants to */
688 if (smbc_getFunctionAddCachedServer(context)(context, srv,
692 int saved_errno = errno;
693 DEBUG(3, (" Failed to add server to cache\n"));
702 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
703 server, share, srv));
705 DLIST_ADD(context->internal->servers, srv);
710 * Connect to a server for getting/setting attributes, possibly on an existing
711 * connection. This works similarly to SMBC_server().
714 SMBC_attr_server(TALLOC_CTX *ctx,
723 struct sockaddr_storage ss;
724 struct cli_state *ipc_cli = NULL;
725 struct rpc_pipe_client *pipe_hnd = NULL;
728 SMBCSRV *ipc_srv=NULL;
731 * Use srv->cli->desthost and srv->cli->share instead of
732 * server and share below to connect to the actual share,
733 * i.e., a normal share or a referred share from
734 * 'msdfs proxy' share.
736 srv = SMBC_server(ctx, context, true, server, share,
737 pp_workgroup, pp_username, pp_password);
741 server = srv->cli->desthost;
742 share = srv->cli->share;
745 * See if we've already created this special connection. Reference
746 * our "special" share name '*IPC$', which is an impossible real share
747 * name due to the leading asterisk.
749 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
750 pp_workgroup, pp_username, pp_password);
753 /* We didn't find a cached connection. Get the password */
754 if (!*pp_password || (*pp_password)[0] == '\0') {
755 /* ... then retrieve it now. */
756 SMBC_call_auth_fn(ctx, context, server, share,
760 if (!*pp_workgroup || !*pp_username || !*pp_password) {
767 if (smbc_getOptionUseKerberos(context)) {
768 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
770 if (smbc_getOptionUseCCache(context)) {
771 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
775 nt_status = cli_full_connection(&ipc_cli,
776 global_myname(), server,
777 &ss, 0, "IPC$", "?????",
783 if (! NT_STATUS_IS_OK(nt_status)) {
784 DEBUG(1,("cli_full_connection failed! (%s)\n",
785 nt_errstr(nt_status)));
790 if (context->internal->smb_encryption_level) {
791 /* Attempt UNIX smb encryption. */
792 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
798 * context->smb_encryption_level ==
799 * 1 means don't fail if encryption can't be
800 * negotiated, == 2 means fail if encryption
801 * can't be negotiated.
804 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
806 if (context->internal->smb_encryption_level == 2) {
807 cli_shutdown(ipc_cli);
812 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
815 ipc_srv = SMB_MALLOC_P(SMBCSRV);
818 cli_shutdown(ipc_cli);
822 ZERO_STRUCTP(ipc_srv);
823 ipc_srv->cli = ipc_cli;
825 nt_status = cli_rpc_pipe_open_noauth(
826 ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
827 if (!NT_STATUS_IS_OK(nt_status)) {
828 DEBUG(1, ("cli_nt_session_open fail!\n"));
830 cli_shutdown(ipc_srv->cli);
836 * Some systems don't support
837 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
838 * so we might as well do it too.
841 nt_status = rpccli_lsa_open_policy(
845 GENERIC_EXECUTE_ACCESS,
848 if (!NT_STATUS_IS_OK(nt_status)) {
849 errno = SMBC_errno(context, ipc_srv->cli);
850 cli_shutdown(ipc_srv->cli);
854 /* now add it to the cache (internal or external) */
856 errno = 0; /* let cache function set errno if it likes */
857 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
862 DEBUG(3, (" Failed to add server to cache\n"));
866 cli_shutdown(ipc_srv->cli);
871 DLIST_ADD(context->internal->servers, ipc_srv);