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 * Check a server for being alive and well.
32 * returns 0 if the server is in shape. Returns 1 on error
34 * Also useable outside libsmbclient to enable external cache
35 * to do some checks too.
38 SMBC_check_server(SMBCCTX
* context
,
45 return (getpeername(server
->cli
->fd
, &addr
, &size
) == -1);
49 * Remove a server from the cached server list it's unused.
50 * On success, 0 is returned. 1 is returned if the server could not be removed.
52 * Also useable outside libsmbclient
55 SMBC_remove_unused_server(SMBCCTX
* context
,
60 /* are we being fooled ? */
61 if (!context
|| !context
->internal
->initialized
|| !srv
) {
65 /* Check all open files/directories for a relation with this server */
66 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
67 if (file
->srv
== srv
) {
69 DEBUG(3, ("smbc_remove_usused_server: "
70 "%p still used by %p.\n",
76 DLIST_REMOVE(context
->internal
->servers
, srv
);
78 cli_shutdown(srv
->cli
);
81 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
83 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
89 /****************************************************************
90 * Call the auth_fn with fixed size (fstring) buffers.
91 ***************************************************************/
93 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
104 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
106 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
107 strlcpy(username
, *pp_username
, sizeof(username
));
108 strlcpy(password
, *pp_password
, sizeof(password
));
110 /* See if there's an authentication with context function provided */
111 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
112 if (auth_with_context_fn
)
114 (* auth_with_context_fn
)(context
,
116 workgroup
, sizeof(workgroup
),
117 username
, sizeof(username
),
118 password
, sizeof(password
));
122 smbc_getFunctionAuthData(context
)(server
, share
,
123 workgroup
, sizeof(workgroup
),
124 username
, sizeof(username
),
125 password
, sizeof(password
));
128 TALLOC_FREE(*pp_workgroup
);
129 TALLOC_FREE(*pp_username
);
130 TALLOC_FREE(*pp_password
);
132 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
133 *pp_username
= talloc_strdup(ctx
, username
);
134 *pp_password
= talloc_strdup(ctx
, password
);
139 SMBC_get_auth_data(const char *server
, const char *share
,
140 char *workgroup_buf
, int workgroup_buf_len
,
141 char *username_buf
, int username_buf_len
,
142 char *password_buf
, int password_buf_len
)
144 /* Default function just uses provided data. Nothing to do. */
150 SMBC_find_server(TALLOC_CTX
*ctx
,
161 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
167 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
172 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
173 !*pp_password
|| !(*pp_password
)[0])) {
174 SMBC_call_auth_fn(ctx
, context
, server
, share
,
175 pp_workgroup
, pp_username
, pp_password
);
178 * However, smbc_auth_fn may have picked up info relating to
179 * an existing connection, so try for an existing connection
183 goto check_server_cache
;
188 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
190 * This server is no good anymore
191 * Try to remove it and check for more possible
192 * servers in the cache
194 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
197 * We could not remove the server completely,
198 * remove it from the cache so we will not get
199 * it again. It will be removed when the last
200 * file/dir is closed.
202 smbc_getFunctionRemoveCachedServer(context
)(context
,
207 * Maybe there are more cached connections to this
210 goto check_server_cache
;
220 * Connect to a server, possibly on an existing connection
222 * Here, what we want to do is: If the server and username
223 * match an existing connection, reuse that, otherwise, establish a
226 * If we have to create a new connection, call the auth_fn to get the
227 * info we need, unless the username and password were passed in.
231 SMBC_server(TALLOC_CTX
*ctx
,
233 bool connect_if_not_found
,
242 struct nmb_name called
, calling
;
243 const char *server_n
= server
;
244 struct sockaddr_storage ss
;
245 int tried_reverse
= 0;
248 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
250 const char *username_used
;
256 if (server
[0] == 0) {
261 /* Look for a cached connection */
262 srv
= SMBC_find_server(ctx
, context
, server
, share
,
263 pp_workgroup
, pp_username
, pp_password
);
266 * If we found a connection and we're only allowed one share per
271 smbc_getOptionOneSharePerServer(context
)) {
274 * ... then if there's no current connection to the share,
275 * connect to it. SMBC_find_server(), or rather the function
276 * pointed to by context->get_cached_srv_fn which
277 * was called by SMBC_find_server(), will have issued a tree
278 * disconnect if the requested share is not the same as the
279 * one that was already connected.
281 if (srv
->cli
->cnum
== (uint16
) -1) {
282 /* Ensure we have accurate auth info */
283 SMBC_call_auth_fn(ctx
, context
, server
, share
,
288 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
290 cli_shutdown(srv
->cli
);
292 smbc_getFunctionRemoveCachedServer(context
)(context
,
298 * We don't need to renegotiate encryption
299 * here as the encryption context is not per
303 if (!cli_send_tconX(srv
->cli
, share
, "?????",
305 strlen(*pp_password
)+1)) {
307 errno
= SMBC_errno(context
, srv
->cli
);
308 cli_shutdown(srv
->cli
);
310 smbc_getFunctionRemoveCachedServer(context
)(context
,
315 /* Determine if this share supports case sensitivity */
318 ("IPC$ so ignore case sensitivity\n"));
319 } else if (!cli_get_fs_attr_info(c
, &fs_attrs
)) {
320 DEBUG(4, ("Could not retrieve "
321 "case sensitivity flag: %s.\n",
325 * We can't determine the case sensitivity of
326 * the share. We have no choice but to use the
327 * user-specified case sensitivity setting.
329 if (smbc_getOptionCaseSensitive(context
)) {
330 cli_set_case_sensitive(c
, True
);
332 cli_set_case_sensitive(c
, False
);
336 ("Case sensitive: %s\n",
337 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
340 cli_set_case_sensitive(
342 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
348 * Regenerate the dev value since it's based on both
352 srv
->dev
= (dev_t
)(str_checksum(server
) ^
353 str_checksum(share
));
358 /* If we have a connection... */
361 /* ... then we're done here. Give 'em what they came for. */
365 /* If we're not asked to connect when a connection doesn't exist... */
366 if (! connect_if_not_found
) {
367 /* ... then we're done here. */
371 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
376 make_nmb_name(&calling
, smbc_getNetbiosName(context
), 0x0);
377 make_nmb_name(&called
, server
, 0x20);
379 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
381 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
387 /* have to open a new connection */
388 if ((c
= cli_initialise()) == NULL
) {
393 if (smbc_getOptionUseKerberos(context
)) {
394 c
->use_kerberos
= True
;
397 if (smbc_getOptionFallbackAfterKerberos(context
)) {
398 c
->fallback_after_kerberos
= True
;
401 c
->timeout
= smbc_getTimeout(context
);
404 * Force use of port 139 for first try if share is $IPC, empty, or
405 * null, so browse lists can work
407 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
408 port_try_first
= 139;
411 port_try_first
= 445;
415 c
->port
= port_try_first
;
417 status
= cli_connect(c
, server_n
, &ss
);
418 if (!NT_STATUS_IS_OK(status
)) {
420 /* First connection attempt failed. Try alternate port. */
421 c
->port
= port_try_next
;
423 status
= cli_connect(c
, server_n
, &ss
);
424 if (!NT_STATUS_IS_OK(status
)) {
431 if (!cli_session_request(c
, &calling
, &called
)) {
433 if (strcmp(called
.name
, "*SMBSERVER")) {
434 make_nmb_name(&called
, "*SMBSERVER", 0x20);
436 } else { /* Try one more time, but ensure we don't loop */
438 /* Only try this if server is an IP address ... */
440 if (is_ipaddress(server
) && !tried_reverse
) {
442 struct sockaddr_storage rem_ss
;
444 if (!interpret_string_addr(&rem_ss
, server
,
446 DEBUG(4, ("Could not convert IP address "
447 "%s to struct sockaddr_storage\n",
453 tried_reverse
++; /* Yuck */
455 if (name_status_find("*", 0, 0,
456 &rem_ss
, remote_name
)) {
457 make_nmb_name(&called
,
468 DEBUG(4,(" session request ok\n"));
470 if (!cli_negprot(c
)) {
476 username_used
= *pp_username
;
478 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
480 strlen(*pp_password
),
482 strlen(*pp_password
),
485 /* Failed. Try an anonymous login, if allowed by flags. */
488 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
489 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
500 DEBUG(4,(" session setup ok\n"));
502 if (!cli_send_tconX(c
, share
, "?????",
503 *pp_password
, strlen(*pp_password
)+1)) {
504 errno
= SMBC_errno(context
, c
);
509 DEBUG(4,(" tconx ok\n"));
511 /* Determine if this share supports case sensitivity */
513 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
514 } else if (!cli_get_fs_attr_info(c
, &fs_attrs
)) {
515 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
519 * We can't determine the case sensitivity of the share. We
520 * have no choice but to use the user-specified case
521 * sensitivity setting.
523 if (smbc_getOptionCaseSensitive(context
)) {
524 cli_set_case_sensitive(c
, True
);
526 cli_set_case_sensitive(c
, False
);
529 DEBUG(4, ("Case sensitive: %s\n",
530 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
533 cli_set_case_sensitive(c
,
534 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
539 if (context
->internal
->smb_encryption_level
) {
540 /* Attempt UNIX smb encryption. */
541 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
547 * context->smb_encryption_level == 1
548 * means don't fail if encryption can't be negotiated,
549 * == 2 means fail if encryption can't be negotiated.
552 DEBUG(4,(" SMB encrypt failed\n"));
554 if (context
->internal
->smb_encryption_level
== 2) {
560 DEBUG(4,(" SMB encrypt ok\n"));
564 * Ok, we have got a nice connection
565 * Let's allocate a server structure.
568 srv
= SMB_MALLOC_P(SMBCSRV
);
576 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
577 srv
->no_pathinfo
= False
;
578 srv
->no_pathinfo2
= False
;
579 srv
->no_nt_session
= False
;
581 /* now add it to the cache (internal or external) */
582 /* Let the cache function set errno if it wants to */
584 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
588 int saved_errno
= errno
;
589 DEBUG(3, (" Failed to add server to cache\n"));
597 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
598 server
, share
, srv
));
600 DLIST_ADD(context
->internal
->servers
, srv
);
614 * Connect to a server for getting/setting attributes, possibly on an existing
615 * connection. This works similarly to SMBC_server().
618 SMBC_attr_server(TALLOC_CTX
*ctx
,
627 struct sockaddr_storage ss
;
628 struct cli_state
*ipc_cli
;
629 struct rpc_pipe_client
*pipe_hnd
;
631 SMBCSRV
*ipc_srv
=NULL
;
634 * See if we've already created this special connection. Reference
635 * our "special" share name '*IPC$', which is an impossible real share
636 * name due to the leading asterisk.
638 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
639 pp_workgroup
, pp_username
, pp_password
);
642 /* We didn't find a cached connection. Get the password */
643 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
644 /* ... then retrieve it now. */
645 SMBC_call_auth_fn(ctx
, context
, server
, share
,
649 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
656 if (smbc_getOptionUseKerberos(context
)) {
657 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
661 nt_status
= cli_full_connection(&ipc_cli
,
662 global_myname(), server
,
663 &ss
, 0, "IPC$", "?????",
669 if (! NT_STATUS_IS_OK(nt_status
)) {
670 DEBUG(1,("cli_full_connection failed! (%s)\n",
671 nt_errstr(nt_status
)));
676 if (context
->internal
->smb_encryption_level
) {
677 /* Attempt UNIX smb encryption. */
678 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
684 * context->smb_encryption_level ==
685 * 1 means don't fail if encryption can't be
686 * negotiated, == 2 means fail if encryption
687 * can't be negotiated.
690 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
692 if (context
->internal
->smb_encryption_level
== 2) {
693 cli_shutdown(ipc_cli
);
698 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
701 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
704 cli_shutdown(ipc_cli
);
708 ZERO_STRUCTP(ipc_srv
);
709 ipc_srv
->cli
= ipc_cli
;
711 pipe_hnd
= cli_rpc_pipe_open_noauth(ipc_srv
->cli
,
715 DEBUG(1, ("cli_nt_session_open fail!\n"));
717 cli_shutdown(ipc_srv
->cli
);
723 * Some systems don't support
724 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
725 * so we might as well do it too.
728 nt_status
= rpccli_lsa_open_policy(
732 GENERIC_EXECUTE_ACCESS
,
735 if (!NT_STATUS_IS_OK(nt_status
)) {
736 errno
= SMBC_errno(context
, ipc_srv
->cli
);
737 cli_shutdown(ipc_srv
->cli
);
741 /* now add it to the cache (internal or external) */
743 errno
= 0; /* let cache function set errno if it likes */
744 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
749 DEBUG(3, (" Failed to add server to cache\n"));
753 cli_shutdown(ipc_srv
->cli
);
758 DLIST_ADD(context
->internal
->servers
, ipc_srv
);