s3-libsmb: Remove use of cli_errstr()
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_server.c
blobd83d8ff3474e7cc770f20577f1c72a2b2a08c74f
1 /*
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/>.
26 #include "includes.h"
27 #include "libsmb/libsmb.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "../librpc/gen_ndr/ndr_lsa.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "rpc_client/cli_lsarpc.h"
33 #include "libcli/security/security.h"
34 #include "libsmb/nmblib.h"
36 /*
37 * Check a server for being alive and well.
38 * returns 0 if the server is in shape. Returns 1 on error
40 * Also useable outside libsmbclient to enable external cache
41 * to do some checks too.
43 int
44 SMBC_check_server(SMBCCTX * context,
45 SMBCSRV * server)
47 socklen_t size;
48 struct sockaddr addr;
50 size = sizeof(addr);
51 return (getpeername(server->cli->fd, &addr, &size) == -1);
54 /*
55 * Remove a server from the cached server list it's unused.
56 * On success, 0 is returned. 1 is returned if the server could not be removed.
58 * Also useable outside libsmbclient
60 int
61 SMBC_remove_unused_server(SMBCCTX * context,
62 SMBCSRV * srv)
64 SMBCFILE * file;
66 /* are we being fooled ? */
67 if (!context || !context->internal->initialized || !srv) {
68 return 1;
71 /* Check all open files/directories for a relation with this server */
72 for (file = context->internal->files; file; file = file->next) {
73 if (file->srv == srv) {
74 /* Still used */
75 DEBUG(3, ("smbc_remove_usused_server: "
76 "%p still used by %p.\n",
77 srv, file));
78 return 1;
82 DLIST_REMOVE(context->internal->servers, srv);
84 cli_shutdown(srv->cli);
85 srv->cli = NULL;
87 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
89 smbc_getFunctionRemoveCachedServer(context)(context, srv);
91 SAFE_FREE(srv);
92 return 0;
95 /****************************************************************
96 * Call the auth_fn with fixed size (fstring) buffers.
97 ***************************************************************/
98 void
99 SMBC_call_auth_fn(TALLOC_CTX *ctx,
100 SMBCCTX *context,
101 const char *server,
102 const char *share,
103 char **pp_workgroup,
104 char **pp_username,
105 char **pp_password)
107 fstring workgroup;
108 fstring username;
109 fstring password;
110 smbc_get_auth_data_with_context_fn auth_with_context_fn;
112 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
113 strlcpy(username, *pp_username, sizeof(username));
114 strlcpy(password, *pp_password, sizeof(password));
116 /* See if there's an authentication with context function provided */
117 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
118 if (auth_with_context_fn)
120 (* auth_with_context_fn)(context,
121 server, share,
122 workgroup, sizeof(workgroup),
123 username, sizeof(username),
124 password, sizeof(password));
126 else
128 smbc_getFunctionAuthData(context)(server, share,
129 workgroup, sizeof(workgroup),
130 username, sizeof(username),
131 password, sizeof(password));
134 TALLOC_FREE(*pp_workgroup);
135 TALLOC_FREE(*pp_username);
136 TALLOC_FREE(*pp_password);
138 *pp_workgroup = talloc_strdup(ctx, workgroup);
139 *pp_username = talloc_strdup(ctx, username);
140 *pp_password = talloc_strdup(ctx, password);
144 void
145 SMBC_get_auth_data(const char *server, const char *share,
146 char *workgroup_buf, int workgroup_buf_len,
147 char *username_buf, int username_buf_len,
148 char *password_buf, int password_buf_len)
150 /* Default function just uses provided data. Nothing to do. */
155 SMBCSRV *
156 SMBC_find_server(TALLOC_CTX *ctx,
157 SMBCCTX *context,
158 const char *server,
159 const char *share,
160 char **pp_workgroup,
161 char **pp_username,
162 char **pp_password)
164 SMBCSRV *srv;
165 int auth_called = 0;
167 if (!pp_workgroup || !pp_username || !pp_password) {
168 return NULL;
171 check_server_cache:
173 srv = smbc_getFunctionGetCachedServer(context)(context,
174 server, share,
175 *pp_workgroup,
176 *pp_username);
178 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
179 !*pp_password || !(*pp_password)[0])) {
180 SMBC_call_auth_fn(ctx, context, server, share,
181 pp_workgroup, pp_username, pp_password);
184 * However, smbc_auth_fn may have picked up info relating to
185 * an existing connection, so try for an existing connection
186 * again ...
188 auth_called = 1;
189 goto check_server_cache;
193 if (srv) {
194 if (smbc_getFunctionCheckServer(context)(context, srv)) {
196 * This server is no good anymore
197 * Try to remove it and check for more possible
198 * servers in the cache
200 if (smbc_getFunctionRemoveUnusedServer(context)(context,
201 srv)) {
203 * We could not remove the server completely,
204 * remove it from the cache so we will not get
205 * it again. It will be removed when the last
206 * file/dir is closed.
208 smbc_getFunctionRemoveCachedServer(context)(context,
209 srv);
213 * Maybe there are more cached connections to this
214 * server
216 goto check_server_cache;
219 return srv;
222 return NULL;
226 * Connect to a server, possibly on an existing connection
228 * Here, what we want to do is: If the server and username
229 * match an existing connection, reuse that, otherwise, establish a
230 * new connection.
232 * If we have to create a new connection, call the auth_fn to get the
233 * info we need, unless the username and password were passed in.
236 static SMBCSRV *
237 SMBC_server_internal(TALLOC_CTX *ctx,
238 SMBCCTX *context,
239 bool connect_if_not_found,
240 const char *server,
241 const char *share,
242 char **pp_workgroup,
243 char **pp_username,
244 char **pp_password,
245 bool *in_cache)
247 SMBCSRV *srv=NULL;
248 char *workgroup = NULL;
249 struct cli_state *c = NULL;
250 const char *server_n = server;
251 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
252 uint32 fs_attrs = 0;
253 const char *username_used;
254 NTSTATUS status;
255 char *newserver, *newshare;
257 ZERO_STRUCT(c);
258 *in_cache = false;
260 if (server[0] == 0) {
261 errno = EPERM;
262 return NULL;
265 /* Look for a cached connection */
266 srv = SMBC_find_server(ctx, context, server, share,
267 pp_workgroup, pp_username, pp_password);
270 * If we found a connection and we're only allowed one share per
271 * server...
273 if (srv &&
274 *share != '\0' &&
275 smbc_getOptionOneSharePerServer(context)) {
278 * ... then if there's no current connection to the share,
279 * connect to it. SMBC_find_server(), or rather the function
280 * pointed to by context->get_cached_srv_fn which
281 * was called by SMBC_find_server(), will have issued a tree
282 * disconnect if the requested share is not the same as the
283 * one that was already connected.
287 * Use srv->cli->desthost and srv->cli->share instead of
288 * server and share below to connect to the actual share,
289 * i.e., a normal share or a referred share from
290 * 'msdfs proxy' share.
292 if (srv->cli->cnum == (uint16) -1) {
293 /* Ensure we have accurate auth info */
294 SMBC_call_auth_fn(ctx, context,
295 srv->cli->desthost,
296 srv->cli->share,
297 pp_workgroup,
298 pp_username,
299 pp_password);
301 if (!*pp_workgroup || !*pp_username || !*pp_password) {
302 errno = ENOMEM;
303 cli_shutdown(srv->cli);
304 srv->cli = NULL;
305 smbc_getFunctionRemoveCachedServer(context)(context,
306 srv);
307 return NULL;
311 * We don't need to renegotiate encryption
312 * here as the encryption context is not per
313 * tid.
316 status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
317 *pp_password,
318 strlen(*pp_password)+1);
319 if (!NT_STATUS_IS_OK(status)) {
320 errno = map_errno_from_nt_status(status);
321 cli_shutdown(srv->cli);
322 srv->cli = NULL;
323 smbc_getFunctionRemoveCachedServer(context)(context,
324 srv);
325 srv = NULL;
328 /* Determine if this share supports case sensitivity */
329 if (is_ipc) {
330 DEBUG(4,
331 ("IPC$ so ignore case sensitivity\n"));
332 status = NT_STATUS_OK;
333 } else {
334 status = cli_get_fs_attr_info(c, &fs_attrs);
337 if (!NT_STATUS_IS_OK(status)) {
338 DEBUG(4, ("Could not retrieve "
339 "case sensitivity flag: %s.\n",
340 nt_errstr(status)));
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);
349 } else {
350 cli_set_case_sensitive(c, False);
352 } else if (!is_ipc) {
353 DEBUG(4,
354 ("Case sensitive: %s\n",
355 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
356 ? "True"
357 : "False")));
358 cli_set_case_sensitive(
360 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
361 ? True
362 : False));
366 * Regenerate the dev value since it's based on both
367 * server and share
369 if (srv) {
370 srv->dev = (dev_t)(str_checksum(srv->cli->desthost) ^
371 str_checksum(srv->cli->share));
376 /* If we have a connection... */
377 if (srv) {
379 /* ... then we're done here. Give 'em what they came for. */
380 *in_cache = true;
381 goto done;
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. */
387 return NULL;
390 if (!*pp_workgroup || !*pp_username || !*pp_password) {
391 errno = ENOMEM;
392 return NULL;
395 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
397 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
399 status = NT_STATUS_UNSUCCESSFUL;
401 if (share == NULL || *share == '\0' || is_ipc) {
403 * Try 139 first for IPC$
405 status = cli_connect_nb(server_n, NULL, 139, 0x20,
406 smbc_getNetbiosName(context),
407 Undefined, &c);
410 if (!NT_STATUS_IS_OK(status)) {
412 * No IPC$ or 139 did not work
414 status = cli_connect_nb(server_n, NULL, 0, 0x20,
415 smbc_getNetbiosName(context),
416 Undefined, &c);
419 if (!NT_STATUS_IS_OK(status)) {
420 errno = map_errno_from_nt_status(status);
421 return NULL;
424 if (smbc_getOptionUseKerberos(context)) {
425 c->use_kerberos = True;
428 if (smbc_getOptionFallbackAfterKerberos(context)) {
429 c->fallback_after_kerberos = True;
432 if (smbc_getOptionUseCCache(context)) {
433 c->use_ccache = True;
436 c->timeout = smbc_getTimeout(context);
438 status = cli_negprot(c);
440 if (!NT_STATUS_IS_OK(status)) {
441 cli_shutdown(c);
442 errno = ETIMEDOUT;
443 return NULL;
446 username_used = *pp_username;
448 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
449 *pp_password,
450 strlen(*pp_password),
451 *pp_password,
452 strlen(*pp_password),
453 *pp_workgroup))) {
455 /* Failed. Try an anonymous login, if allowed by flags. */
456 username_used = "";
458 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
459 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
460 *pp_password, 1,
461 *pp_password, 0,
462 *pp_workgroup))) {
464 cli_shutdown(c);
465 errno = EPERM;
466 return NULL;
470 status = cli_init_creds(c, username_used,
471 *pp_workgroup, *pp_password);
472 if (!NT_STATUS_IS_OK(status)) {
473 errno = map_errno_from_nt_status(status);
474 cli_shutdown(c);
475 return NULL;
478 DEBUG(4,(" session setup ok\n"));
480 /* here's the fun part....to support 'msdfs proxy' shares
481 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
482 here before trying to connect to the original share.
483 cli_check_msdfs_proxy() will fail if it is a normal share. */
485 if ((c->capabilities & CAP_DFS) &&
486 cli_check_msdfs_proxy(ctx, c, share,
487 &newserver, &newshare,
488 /* FIXME: cli_check_msdfs_proxy() does
489 not support smbc_smb_encrypt_level type */
490 context->internal->smb_encryption_level ?
491 true : false,
492 *pp_username,
493 *pp_password,
494 *pp_workgroup)) {
495 cli_shutdown(c);
496 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
497 newserver, newshare, pp_workgroup,
498 pp_username, pp_password, in_cache);
499 TALLOC_FREE(newserver);
500 TALLOC_FREE(newshare);
501 return srv;
504 /* must be a normal share */
506 status = cli_tcon_andx(c, share, "?????", *pp_password,
507 strlen(*pp_password)+1);
508 if (!NT_STATUS_IS_OK(status)) {
509 errno = map_errno_from_nt_status(status);
510 cli_shutdown(c);
511 return NULL;
514 DEBUG(4,(" tconx ok\n"));
516 /* Determine if this share supports case sensitivity */
517 if (is_ipc) {
518 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
519 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
520 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
521 cli_errstr(c)));
524 * We can't determine the case sensitivity of the share. We
525 * have no choice but to use the user-specified case
526 * sensitivity setting.
528 if (smbc_getOptionCaseSensitive(context)) {
529 cli_set_case_sensitive(c, True);
530 } else {
531 cli_set_case_sensitive(c, False);
533 } else {
534 DEBUG(4, ("Case sensitive: %s\n",
535 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
536 ? "True"
537 : "False")));
538 cli_set_case_sensitive(c,
539 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
540 ? True
541 : False));
544 if (context->internal->smb_encryption_level) {
545 /* Attempt UNIX smb encryption. */
546 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
547 username_used,
548 *pp_password,
549 *pp_workgroup))) {
552 * context->smb_encryption_level == 1
553 * means don't fail if encryption can't be negotiated,
554 * == 2 means fail if encryption can't be negotiated.
557 DEBUG(4,(" SMB encrypt failed\n"));
559 if (context->internal->smb_encryption_level == 2) {
560 cli_shutdown(c);
561 errno = EPERM;
562 return NULL;
565 DEBUG(4,(" SMB encrypt ok\n"));
569 * Ok, we have got a nice connection
570 * Let's allocate a server structure.
573 srv = SMB_MALLOC_P(SMBCSRV);
574 if (!srv) {
575 cli_shutdown(c);
576 errno = ENOMEM;
577 return NULL;
580 ZERO_STRUCTP(srv);
581 srv->cli = c;
582 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
583 srv->no_pathinfo = False;
584 srv->no_pathinfo2 = False;
585 srv->no_nt_session = False;
587 done:
588 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
589 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
590 } else {
591 workgroup = *pp_workgroup;
593 if(!workgroup) {
594 if (c != NULL) {
595 cli_shutdown(c);
597 SAFE_FREE(srv);
598 return NULL;
601 /* set the credentials to make DFS work */
602 smbc_set_credentials_with_fallback(context,
603 workgroup,
604 *pp_username,
605 *pp_password);
607 return srv;
610 SMBCSRV *
611 SMBC_server(TALLOC_CTX *ctx,
612 SMBCCTX *context,
613 bool connect_if_not_found,
614 const char *server,
615 const char *share,
616 char **pp_workgroup,
617 char **pp_username,
618 char **pp_password)
620 SMBCSRV *srv=NULL;
621 bool in_cache = false;
623 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
624 server, share, pp_workgroup,
625 pp_username, pp_password, &in_cache);
627 if (!srv) {
628 return NULL;
630 if (in_cache) {
631 return srv;
634 /* Now add it to the cache (internal or external) */
635 /* Let the cache function set errno if it wants to */
636 errno = 0;
637 if (smbc_getFunctionAddCachedServer(context)(context, srv,
638 server, share,
639 *pp_workgroup,
640 *pp_username)) {
641 int saved_errno = errno;
642 DEBUG(3, (" Failed to add server to cache\n"));
643 errno = saved_errno;
644 if (errno == 0) {
645 errno = ENOMEM;
647 SAFE_FREE(srv);
648 return NULL;
651 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
652 server, share, srv));
654 DLIST_ADD(context->internal->servers, srv);
655 return srv;
659 * Connect to a server for getting/setting attributes, possibly on an existing
660 * connection. This works similarly to SMBC_server().
662 SMBCSRV *
663 SMBC_attr_server(TALLOC_CTX *ctx,
664 SMBCCTX *context,
665 const char *server,
666 const char *share,
667 char **pp_workgroup,
668 char **pp_username,
669 char **pp_password)
671 int flags;
672 struct sockaddr_storage ss;
673 struct cli_state *ipc_cli = NULL;
674 struct rpc_pipe_client *pipe_hnd = NULL;
675 NTSTATUS nt_status;
676 SMBCSRV *srv=NULL;
677 SMBCSRV *ipc_srv=NULL;
680 * Use srv->cli->desthost and srv->cli->share instead of
681 * server and share below to connect to the actual share,
682 * i.e., a normal share or a referred share from
683 * 'msdfs proxy' share.
685 srv = SMBC_server(ctx, context, true, server, share,
686 pp_workgroup, pp_username, pp_password);
687 if (!srv) {
688 return NULL;
690 server = srv->cli->desthost;
691 share = srv->cli->share;
694 * See if we've already created this special connection. Reference
695 * our "special" share name '*IPC$', which is an impossible real share
696 * name due to the leading asterisk.
698 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
699 pp_workgroup, pp_username, pp_password);
700 if (!ipc_srv) {
702 /* We didn't find a cached connection. Get the password */
703 if (!*pp_password || (*pp_password)[0] == '\0') {
704 /* ... then retrieve it now. */
705 SMBC_call_auth_fn(ctx, context, server, share,
706 pp_workgroup,
707 pp_username,
708 pp_password);
709 if (!*pp_workgroup || !*pp_username || !*pp_password) {
710 errno = ENOMEM;
711 return NULL;
715 flags = 0;
716 if (smbc_getOptionUseKerberos(context)) {
717 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
719 if (smbc_getOptionUseCCache(context)) {
720 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
723 zero_sockaddr(&ss);
724 nt_status = cli_full_connection(&ipc_cli,
725 lp_netbios_name(), server,
726 &ss, 0, "IPC$", "?????",
727 *pp_username,
728 *pp_workgroup,
729 *pp_password,
730 flags,
731 Undefined);
732 if (! NT_STATUS_IS_OK(nt_status)) {
733 DEBUG(1,("cli_full_connection failed! (%s)\n",
734 nt_errstr(nt_status)));
735 errno = ENOTSUP;
736 return NULL;
739 if (context->internal->smb_encryption_level) {
740 /* Attempt UNIX smb encryption. */
741 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
742 *pp_username,
743 *pp_password,
744 *pp_workgroup))) {
747 * context->smb_encryption_level ==
748 * 1 means don't fail if encryption can't be
749 * negotiated, == 2 means fail if encryption
750 * can't be negotiated.
753 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
755 if (context->internal->smb_encryption_level == 2) {
756 cli_shutdown(ipc_cli);
757 errno = EPERM;
758 return NULL;
761 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
764 ipc_srv = SMB_MALLOC_P(SMBCSRV);
765 if (!ipc_srv) {
766 errno = ENOMEM;
767 cli_shutdown(ipc_cli);
768 return NULL;
771 ZERO_STRUCTP(ipc_srv);
772 ipc_srv->cli = ipc_cli;
774 nt_status = cli_rpc_pipe_open_noauth(
775 ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
776 if (!NT_STATUS_IS_OK(nt_status)) {
777 DEBUG(1, ("cli_nt_session_open fail!\n"));
778 errno = ENOTSUP;
779 cli_shutdown(ipc_srv->cli);
780 free(ipc_srv);
781 return NULL;
785 * Some systems don't support
786 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
787 * so we might as well do it too.
790 nt_status = rpccli_lsa_open_policy(
791 pipe_hnd,
792 talloc_tos(),
793 True,
794 GENERIC_EXECUTE_ACCESS,
795 &ipc_srv->pol);
797 if (!NT_STATUS_IS_OK(nt_status)) {
798 errno = SMBC_errno(context, ipc_srv->cli);
799 cli_shutdown(ipc_srv->cli);
800 return NULL;
803 /* now add it to the cache (internal or external) */
805 errno = 0; /* let cache function set errno if it likes */
806 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
807 server,
808 "*IPC$",
809 *pp_workgroup,
810 *pp_username)) {
811 DEBUG(3, (" Failed to add server to cache\n"));
812 if (errno == 0) {
813 errno = ENOMEM;
815 cli_shutdown(ipc_srv->cli);
816 free(ipc_srv);
817 return NULL;
820 DLIST_ADD(context->internal->servers, ipc_srv);
823 return ipc_srv;