s3-utils: net - Fix one error/usage message
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_server.c
blob0af9798d2be6a793a27cbf4642f9b52349c438ce
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 if (!cli_state_is_connected(server->cli)) {
48 return 1;
51 return 0;
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;
256 int flags = 0;
258 ZERO_STRUCT(c);
259 *in_cache = false;
261 if (server[0] == 0) {
262 errno = EPERM;
263 return NULL;
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
272 * server...
274 if (srv &&
275 *share != '\0' &&
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 (!cli_state_has_tcon(srv->cli)) {
294 /* Ensure we have accurate auth info */
295 SMBC_call_auth_fn(ctx, context,
296 cli_state_remote_name(srv->cli),
297 srv->cli->share,
298 pp_workgroup,
299 pp_username,
300 pp_password);
302 if (!*pp_workgroup || !*pp_username || !*pp_password) {
303 errno = ENOMEM;
304 cli_shutdown(srv->cli);
305 srv->cli = NULL;
306 smbc_getFunctionRemoveCachedServer(context)(context,
307 srv);
308 return NULL;
312 * We don't need to renegotiate encryption
313 * here as the encryption context is not per
314 * tid.
317 status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
318 *pp_password,
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);
323 srv->cli = NULL;
324 smbc_getFunctionRemoveCachedServer(context)(context,
325 srv);
326 srv = NULL;
329 /* Determine if this share supports case sensitivity */
330 if (is_ipc) {
331 DEBUG(4,
332 ("IPC$ so ignore case sensitivity\n"));
333 status = NT_STATUS_OK;
334 } else {
335 status = cli_get_fs_attr_info(c, &fs_attrs);
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(4, ("Could not retrieve "
340 "case sensitivity flag: %s.\n",
341 nt_errstr(status)));
344 * We can't determine the case sensitivity of
345 * the share. We have no choice but to use the
346 * user-specified case sensitivity setting.
348 if (smbc_getOptionCaseSensitive(context)) {
349 cli_set_case_sensitive(c, True);
350 } else {
351 cli_set_case_sensitive(c, False);
353 } else if (!is_ipc) {
354 DEBUG(4,
355 ("Case sensitive: %s\n",
356 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
357 ? "True"
358 : "False")));
359 cli_set_case_sensitive(
361 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
362 ? True
363 : False));
367 * Regenerate the dev value since it's based on both
368 * server and share
370 if (srv) {
371 const char *remote_name =
372 cli_state_remote_name(srv->cli);
374 srv->dev = (dev_t)(str_checksum(remote_name) ^
375 str_checksum(srv->cli->share));
380 /* If we have a connection... */
381 if (srv) {
383 /* ... then we're done here. Give 'em what they came for. */
384 *in_cache = true;
385 goto done;
388 /* If we're not asked to connect when a connection doesn't exist... */
389 if (! connect_if_not_found) {
390 /* ... then we're done here. */
391 return NULL;
394 if (!*pp_workgroup || !*pp_username || !*pp_password) {
395 errno = ENOMEM;
396 return NULL;
399 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
401 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
403 status = NT_STATUS_UNSUCCESSFUL;
405 if (smbc_getOptionUseKerberos(context)) {
406 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
409 if (smbc_getOptionFallbackAfterKerberos(context)) {
410 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
413 if (smbc_getOptionUseCCache(context)) {
414 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
417 if (share == NULL || *share == '\0' || is_ipc) {
419 * Try 139 first for IPC$
421 status = cli_connect_nb(server_n, NULL, 139, 0x20,
422 smbc_getNetbiosName(context),
423 Undefined, flags, &c);
426 if (!NT_STATUS_IS_OK(status)) {
428 * No IPC$ or 139 did not work
430 status = cli_connect_nb(server_n, NULL, 0, 0x20,
431 smbc_getNetbiosName(context),
432 Undefined, flags, &c);
435 if (!NT_STATUS_IS_OK(status)) {
436 errno = map_errno_from_nt_status(status);
437 return NULL;
440 cli_set_timeout(c, smbc_getTimeout(context));
442 status = cli_negprot(c, PROTOCOL_NT1);
444 if (!NT_STATUS_IS_OK(status)) {
445 cli_shutdown(c);
446 errno = ETIMEDOUT;
447 return NULL;
450 username_used = *pp_username;
452 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
453 *pp_password,
454 strlen(*pp_password),
455 *pp_password,
456 strlen(*pp_password),
457 *pp_workgroup))) {
459 /* Failed. Try an anonymous login, if allowed by flags. */
460 username_used = "";
462 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
463 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
464 *pp_password, 1,
465 *pp_password, 0,
466 *pp_workgroup))) {
468 cli_shutdown(c);
469 errno = EPERM;
470 return NULL;
474 status = cli_init_creds(c, username_used,
475 *pp_workgroup, *pp_password);
476 if (!NT_STATUS_IS_OK(status)) {
477 errno = map_errno_from_nt_status(status);
478 cli_shutdown(c);
479 return NULL;
482 DEBUG(4,(" session setup ok\n"));
484 /* here's the fun part....to support 'msdfs proxy' shares
485 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
486 here before trying to connect to the original share.
487 cli_check_msdfs_proxy() will fail if it is a normal share. */
489 if ((cli_state_capabilities(c) & CAP_DFS) &&
490 cli_check_msdfs_proxy(ctx, c, share,
491 &newserver, &newshare,
492 /* FIXME: cli_check_msdfs_proxy() does
493 not support smbc_smb_encrypt_level type */
494 context->internal->smb_encryption_level ?
495 true : false,
496 *pp_username,
497 *pp_password,
498 *pp_workgroup)) {
499 cli_shutdown(c);
500 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
501 newserver, newshare, pp_workgroup,
502 pp_username, pp_password, in_cache);
503 TALLOC_FREE(newserver);
504 TALLOC_FREE(newshare);
505 return srv;
508 /* must be a normal share */
510 status = cli_tcon_andx(c, share, "?????", *pp_password,
511 strlen(*pp_password)+1);
512 if (!NT_STATUS_IS_OK(status)) {
513 errno = map_errno_from_nt_status(status);
514 cli_shutdown(c);
515 return NULL;
518 DEBUG(4,(" tconx ok\n"));
520 /* Determine if this share supports case sensitivity */
521 if (is_ipc) {
522 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
523 status = NT_STATUS_OK;
524 } else {
525 status = cli_get_fs_attr_info(c, &fs_attrs);
528 if (!NT_STATUS_IS_OK(status)) {
529 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
530 nt_errstr(status)));
533 * We can't determine the case sensitivity of the share. We
534 * have no choice but to use the user-specified case
535 * sensitivity setting.
537 if (smbc_getOptionCaseSensitive(context)) {
538 cli_set_case_sensitive(c, True);
539 } else {
540 cli_set_case_sensitive(c, False);
542 } else if (!is_ipc) {
543 DEBUG(4, ("Case sensitive: %s\n",
544 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
545 ? "True"
546 : "False")));
547 cli_set_case_sensitive(c,
548 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
549 ? True
550 : False));
553 if (context->internal->smb_encryption_level) {
554 /* Attempt UNIX smb encryption. */
555 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
556 username_used,
557 *pp_password,
558 *pp_workgroup))) {
561 * context->smb_encryption_level == 1
562 * means don't fail if encryption can't be negotiated,
563 * == 2 means fail if encryption can't be negotiated.
566 DEBUG(4,(" SMB encrypt failed\n"));
568 if (context->internal->smb_encryption_level == 2) {
569 cli_shutdown(c);
570 errno = EPERM;
571 return NULL;
574 DEBUG(4,(" SMB encrypt ok\n"));
578 * Ok, we have got a nice connection
579 * Let's allocate a server structure.
582 srv = SMB_MALLOC_P(SMBCSRV);
583 if (!srv) {
584 cli_shutdown(c);
585 errno = ENOMEM;
586 return NULL;
589 ZERO_STRUCTP(srv);
590 srv->cli = c;
591 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
592 srv->no_pathinfo = False;
593 srv->no_pathinfo2 = False;
594 srv->no_nt_session = False;
596 done:
597 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
598 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
599 } else {
600 workgroup = *pp_workgroup;
602 if(!workgroup) {
603 if (c != NULL) {
604 cli_shutdown(c);
606 SAFE_FREE(srv);
607 return NULL;
610 /* set the credentials to make DFS work */
611 smbc_set_credentials_with_fallback(context,
612 workgroup,
613 *pp_username,
614 *pp_password);
616 return srv;
619 SMBCSRV *
620 SMBC_server(TALLOC_CTX *ctx,
621 SMBCCTX *context,
622 bool connect_if_not_found,
623 const char *server,
624 const char *share,
625 char **pp_workgroup,
626 char **pp_username,
627 char **pp_password)
629 SMBCSRV *srv=NULL;
630 bool in_cache = false;
632 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
633 server, share, pp_workgroup,
634 pp_username, pp_password, &in_cache);
636 if (!srv) {
637 return NULL;
639 if (in_cache) {
640 return srv;
643 /* Now add it to the cache (internal or external) */
644 /* Let the cache function set errno if it wants to */
645 errno = 0;
646 if (smbc_getFunctionAddCachedServer(context)(context, srv,
647 server, share,
648 *pp_workgroup,
649 *pp_username)) {
650 int saved_errno = errno;
651 DEBUG(3, (" Failed to add server to cache\n"));
652 errno = saved_errno;
653 if (errno == 0) {
654 errno = ENOMEM;
656 SAFE_FREE(srv);
657 return NULL;
660 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
661 server, share, srv));
663 DLIST_ADD(context->internal->servers, srv);
664 return srv;
668 * Connect to a server for getting/setting attributes, possibly on an existing
669 * connection. This works similarly to SMBC_server().
671 SMBCSRV *
672 SMBC_attr_server(TALLOC_CTX *ctx,
673 SMBCCTX *context,
674 const char *server,
675 const char *share,
676 char **pp_workgroup,
677 char **pp_username,
678 char **pp_password)
680 int flags;
681 struct cli_state *ipc_cli = NULL;
682 struct rpc_pipe_client *pipe_hnd = NULL;
683 NTSTATUS nt_status;
684 SMBCSRV *srv=NULL;
685 SMBCSRV *ipc_srv=NULL;
688 * Use srv->cli->desthost and srv->cli->share instead of
689 * server and share below to connect to the actual share,
690 * i.e., a normal share or a referred share from
691 * 'msdfs proxy' share.
693 srv = SMBC_server(ctx, context, true, server, share,
694 pp_workgroup, pp_username, pp_password);
695 if (!srv) {
696 return NULL;
698 server = cli_state_remote_name(srv->cli);
699 share = srv->cli->share;
702 * See if we've already created this special connection. Reference
703 * our "special" share name '*IPC$', which is an impossible real share
704 * name due to the leading asterisk.
706 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
707 pp_workgroup, pp_username, pp_password);
708 if (!ipc_srv) {
710 /* We didn't find a cached connection. Get the password */
711 if (!*pp_password || (*pp_password)[0] == '\0') {
712 /* ... then retrieve it now. */
713 SMBC_call_auth_fn(ctx, context, server, share,
714 pp_workgroup,
715 pp_username,
716 pp_password);
717 if (!*pp_workgroup || !*pp_username || !*pp_password) {
718 errno = ENOMEM;
719 return NULL;
723 flags = 0;
724 if (smbc_getOptionUseKerberos(context)) {
725 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
727 if (smbc_getOptionUseCCache(context)) {
728 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
731 nt_status = cli_full_connection(&ipc_cli,
732 lp_netbios_name(), server,
733 NULL, 0, "IPC$", "?????",
734 *pp_username,
735 *pp_workgroup,
736 *pp_password,
737 flags,
738 Undefined);
739 if (! NT_STATUS_IS_OK(nt_status)) {
740 DEBUG(1,("cli_full_connection failed! (%s)\n",
741 nt_errstr(nt_status)));
742 errno = ENOTSUP;
743 return NULL;
746 if (context->internal->smb_encryption_level) {
747 /* Attempt UNIX smb encryption. */
748 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
749 *pp_username,
750 *pp_password,
751 *pp_workgroup))) {
754 * context->smb_encryption_level ==
755 * 1 means don't fail if encryption can't be
756 * negotiated, == 2 means fail if encryption
757 * can't be negotiated.
760 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
762 if (context->internal->smb_encryption_level == 2) {
763 cli_shutdown(ipc_cli);
764 errno = EPERM;
765 return NULL;
768 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
771 ipc_srv = SMB_MALLOC_P(SMBCSRV);
772 if (!ipc_srv) {
773 errno = ENOMEM;
774 cli_shutdown(ipc_cli);
775 return NULL;
778 ZERO_STRUCTP(ipc_srv);
779 ipc_srv->cli = ipc_cli;
781 nt_status = cli_rpc_pipe_open_noauth(
782 ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
783 if (!NT_STATUS_IS_OK(nt_status)) {
784 DEBUG(1, ("cli_nt_session_open fail!\n"));
785 errno = ENOTSUP;
786 cli_shutdown(ipc_srv->cli);
787 free(ipc_srv);
788 return NULL;
792 * Some systems don't support
793 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
794 * so we might as well do it too.
797 nt_status = rpccli_lsa_open_policy(
798 pipe_hnd,
799 talloc_tos(),
800 True,
801 GENERIC_EXECUTE_ACCESS,
802 &ipc_srv->pol);
804 if (!NT_STATUS_IS_OK(nt_status)) {
805 errno = SMBC_errno(context, ipc_srv->cli);
806 cli_shutdown(ipc_srv->cli);
807 return NULL;
810 /* now add it to the cache (internal or external) */
812 errno = 0; /* let cache function set errno if it likes */
813 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
814 server,
815 "*IPC$",
816 *pp_workgroup,
817 *pp_username)) {
818 DEBUG(3, (" Failed to add server to cache\n"));
819 if (errno == 0) {
820 errno = ENOMEM;
822 cli_shutdown(ipc_srv->cli);
823 free(ipc_srv);
824 return NULL;
827 DLIST_ADD(context->internal->servers, ipc_srv);
830 return ipc_srv;