s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / source3 / libsmb / libsmb_server.c
blob69b51b829f6c1367ddd346d703d7fdbe3a5df6a9
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"
35 #include "../libcli/smb/smbXcli_base.h"
38 * Check a server for being alive and well.
39 * returns 0 if the server is in shape. Returns 1 on error
41 * Also usable outside libsmbclient to enable external cache
42 * to do some checks too.
44 int
45 SMBC_check_server(SMBCCTX * context,
46 SMBCSRV * server)
48 time_t now;
50 if (!cli_state_is_connected(server->cli)) {
51 return 1;
54 now = time_mono(NULL);
56 if (server->last_echo_time == (time_t)0 ||
57 now > server->last_echo_time +
58 (server->cli->timeout/1000)) {
59 unsigned char data[16] = {0};
60 NTSTATUS status = cli_echo(server->cli,
62 data_blob_const(data, sizeof(data)));
63 if (!NT_STATUS_IS_OK(status)) {
64 bool ok = false;
66 * Some SMB2 servers (not Samba or Windows)
67 * check the session status on SMB2_ECHO and return
68 * NT_STATUS_USER_SESSION_DELETED
69 * if the session was not set. That's OK, they still
70 * replied.
71 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13218
73 if (smbXcli_conn_protocol(server->cli->conn) >=
74 PROTOCOL_SMB2_02) {
75 if (NT_STATUS_EQUAL(status,
76 NT_STATUS_USER_SESSION_DELETED)) {
77 ok = true;
81 * Some NetApp servers return
82 * NT_STATUS_INVALID_PARAMETER.That's OK, they still
83 * replied.
84 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
86 if (NT_STATUS_EQUAL(status,
87 NT_STATUS_INVALID_PARAMETER)) {
88 ok = true;
90 if (!ok) {
91 return 1;
94 server->last_echo_time = now;
96 return 0;
100 * Remove a server from the cached server list it's unused.
101 * On success, 0 is returned. 1 is returned if the server could not be removed.
103 * Also usable outside libsmbclient
106 SMBC_remove_unused_server(SMBCCTX * context,
107 SMBCSRV * srv)
109 SMBCFILE * file;
111 /* are we being fooled ? */
112 if (!context || !context->internal->initialized || !srv) {
113 return 1;
116 /* Check all open files/directories for a relation with this server */
117 for (file = context->internal->files; file; file = file->next) {
118 if (file->srv == srv) {
119 /* Still used */
120 DEBUG(3, ("smbc_remove_usused_server: "
121 "%p still used by %p.\n",
122 srv, file));
123 return 1;
127 DLIST_REMOVE(context->internal->servers, srv);
129 cli_shutdown(srv->cli);
130 srv->cli = NULL;
132 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
134 smbc_getFunctionRemoveCachedServer(context)(context, srv);
136 SAFE_FREE(srv);
137 return 0;
140 /****************************************************************
141 * Call the auth_fn with fixed size (fstring) buffers.
142 ***************************************************************/
143 static void
144 SMBC_call_auth_fn(TALLOC_CTX *ctx,
145 SMBCCTX *context,
146 const char *server,
147 const char *share,
148 char **pp_workgroup,
149 char **pp_username,
150 char **pp_password)
152 fstring workgroup = { 0 };
153 fstring username = { 0 };
154 fstring password = { 0 };
155 smbc_get_auth_data_with_context_fn auth_with_context_fn;
157 if (*pp_workgroup != NULL) {
158 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
160 if (*pp_username != NULL) {
161 strlcpy(username, *pp_username, sizeof(username));
163 if (*pp_password != NULL) {
164 strlcpy(password, *pp_password, sizeof(password));
167 /* See if there's an authentication with context function provided */
168 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
169 if (auth_with_context_fn)
171 (* auth_with_context_fn)(context,
172 server, share,
173 workgroup, sizeof(workgroup),
174 username, sizeof(username),
175 password, sizeof(password));
177 else
179 smbc_getFunctionAuthData(context)(server, share,
180 workgroup, sizeof(workgroup),
181 username, sizeof(username),
182 password, sizeof(password));
185 TALLOC_FREE(*pp_workgroup);
186 TALLOC_FREE(*pp_username);
187 TALLOC_FREE(*pp_password);
189 *pp_workgroup = talloc_strdup(ctx, workgroup);
190 *pp_username = talloc_strdup(ctx, username);
191 *pp_password = talloc_strdup(ctx, password);
195 void
196 SMBC_get_auth_data(const char *server, const char *share,
197 char *workgroup_buf, int workgroup_buf_len,
198 char *username_buf, int username_buf_len,
199 char *password_buf, int password_buf_len)
201 /* Default function just uses provided data. Nothing to do. */
206 SMBCSRV *
207 SMBC_find_server(TALLOC_CTX *ctx,
208 SMBCCTX *context,
209 const char *server,
210 const char *share,
211 char **pp_workgroup,
212 char **pp_username,
213 char **pp_password)
215 SMBCSRV *srv;
216 int auth_called = 0;
218 if (!pp_workgroup || !pp_username || !pp_password) {
219 return NULL;
222 check_server_cache:
224 srv = smbc_getFunctionGetCachedServer(context)(context,
225 server, share,
226 *pp_workgroup,
227 *pp_username);
229 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
230 !*pp_password || !(*pp_password)[0])) {
231 SMBC_call_auth_fn(ctx, context, server, share,
232 pp_workgroup, pp_username, pp_password);
235 * However, smbc_auth_fn may have picked up info relating to
236 * an existing connection, so try for an existing connection
237 * again ...
239 auth_called = 1;
240 goto check_server_cache;
244 if (srv) {
245 if (smbc_getFunctionCheckServer(context)(context, srv)) {
247 * This server is no good anymore
248 * Try to remove it and check for more possible
249 * servers in the cache
251 if (smbc_getFunctionRemoveUnusedServer(context)(context,
252 srv)) {
254 * We could not remove the server completely,
255 * remove it from the cache so we will not get
256 * it again. It will be removed when the last
257 * file/dir is closed.
259 smbc_getFunctionRemoveCachedServer(context)(context,
260 srv);
264 * Maybe there are more cached connections to this
265 * server
267 goto check_server_cache;
270 return srv;
273 return NULL;
276 static struct cli_credentials *SMBC_auth_credentials(TALLOC_CTX *mem_ctx,
277 SMBCCTX *context,
278 const char *domain,
279 const char *username,
280 const char *password)
282 struct cli_credentials *creds = NULL;
283 bool use_kerberos = false;
284 bool fallback_after_kerberos = false;
285 bool use_ccache = false;
286 bool pw_nt_hash = false;
288 use_kerberos = smbc_getOptionUseKerberos(context);
289 fallback_after_kerberos = smbc_getOptionFallbackAfterKerberos(context);
290 use_ccache = smbc_getOptionUseCCache(context);
291 pw_nt_hash = smbc_getOptionUseNTHash(context);
293 creds = cli_session_creds_init(mem_ctx,
294 username,
295 domain,
296 NULL, /* realm */
297 password,
298 use_kerberos,
299 fallback_after_kerberos,
300 use_ccache,
301 pw_nt_hash);
302 if (creds == NULL) {
303 return NULL;
306 switch (context->internal->smb_encryption_level) {
307 case SMBC_ENCRYPTLEVEL_DEFAULT:
308 /* Use the config option */
309 break;
310 case SMBC_ENCRYPTLEVEL_NONE:
311 (void)cli_credentials_set_smb_encryption(
312 creds,
313 SMB_ENCRYPTION_OFF,
314 CRED_SPECIFIED);
315 break;
316 case SMBC_ENCRYPTLEVEL_REQUEST:
317 (void)cli_credentials_set_smb_encryption(
318 creds,
319 SMB_ENCRYPTION_DESIRED,
320 CRED_SPECIFIED);
321 break;
322 case SMBC_ENCRYPTLEVEL_REQUIRE:
323 default:
324 (void)cli_credentials_set_smb_encryption(
325 creds,
326 SMB_ENCRYPTION_REQUIRED,
327 CRED_SPECIFIED);
328 break;
332 return creds;
336 * Connect to a server, possibly on an existing connection
338 * Here, what we want to do is: If the server and username
339 * match an existing connection, reuse that, otherwise, establish a
340 * new connection.
342 * If we have to create a new connection, call the auth_fn to get the
343 * info we need, unless the username and password were passed in.
346 static SMBCSRV *
347 SMBC_server_internal(TALLOC_CTX *ctx,
348 SMBCCTX *context,
349 bool connect_if_not_found,
350 const char *server,
351 uint16_t port,
352 const char *share,
353 char **pp_workgroup,
354 char **pp_username,
355 char **pp_password,
356 bool *in_cache)
358 SMBCSRV *srv=NULL;
359 char *workgroup = NULL;
360 struct cli_state *c = NULL;
361 const char *server_n = server;
362 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
363 uint32_t fs_attrs = 0;
364 const char *username_used = NULL;
365 const char *password_used = NULL;
366 NTSTATUS status;
367 char *newserver, *newshare;
368 int flags = 0;
369 struct smbXcli_tcon *tcon = NULL;
370 int signing_state = SMB_SIGNING_DEFAULT;
371 struct cli_credentials *creds = NULL;
373 *in_cache = false;
375 if (server[0] == 0) {
376 errno = EPERM;
377 return NULL;
380 /* Look for a cached connection */
381 srv = SMBC_find_server(ctx, context, server, share,
382 pp_workgroup, pp_username, pp_password);
385 * If we found a connection and we're only allowed one share per
386 * server...
388 if (srv &&
389 share != NULL && *share != '\0' &&
390 smbc_getOptionOneSharePerServer(context)) {
393 * ... then if there's no current connection to the share,
394 * connect to it. SMBC_find_server(), or rather the function
395 * pointed to by context->get_cached_srv_fn which
396 * was called by SMBC_find_server(), will have issued a tree
397 * disconnect if the requested share is not the same as the
398 * one that was already connected.
402 * Use srv->cli->desthost and srv->cli->share instead of
403 * server and share below to connect to the actual share,
404 * i.e., a normal share or a referred share from
405 * 'msdfs proxy' share.
407 if (!cli_state_has_tcon(srv->cli)) {
408 /* Ensure we have accurate auth info */
409 SMBC_call_auth_fn(ctx, context,
410 smbXcli_conn_remote_name(srv->cli->conn),
411 srv->cli->share,
412 pp_workgroup,
413 pp_username,
414 pp_password);
416 if (!*pp_workgroup || !*pp_username || !*pp_password) {
417 errno = ENOMEM;
418 cli_shutdown(srv->cli);
419 srv->cli = NULL;
420 smbc_getFunctionRemoveCachedServer(context)(context,
421 srv);
422 return NULL;
426 * We don't need to renegotiate encryption
427 * here as the encryption context is not per
428 * tid.
431 status = cli_tree_connect(srv->cli,
432 srv->cli->share,
433 "?????",
434 *pp_password);
435 if (!NT_STATUS_IS_OK(status)) {
436 cli_shutdown(srv->cli);
437 errno = map_errno_from_nt_status(status);
438 srv->cli = NULL;
439 smbc_getFunctionRemoveCachedServer(context)(context,
440 srv);
441 srv = NULL;
442 goto not_found;
445 /* Determine if this share supports case sensitivity */
446 if (is_ipc) {
447 DEBUG(4,
448 ("IPC$ so ignore case sensitivity\n"));
449 status = NT_STATUS_OK;
450 } else {
451 status = cli_get_fs_attr_info(srv->cli, &fs_attrs);
454 if (!NT_STATUS_IS_OK(status)) {
455 DEBUG(4, ("Could not retrieve "
456 "case sensitivity flag: %s.\n",
457 nt_errstr(status)));
460 * We can't determine the case sensitivity of
461 * the share. We have no choice but to use the
462 * user-specified case sensitivity setting.
464 if (smbc_getOptionCaseSensitive(context)) {
465 cli_set_case_sensitive(srv->cli, true);
466 } else {
467 cli_set_case_sensitive(srv->cli, false);
469 } else if (!is_ipc) {
470 DEBUG(4,
471 ("Case sensitive: %s\n",
472 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
473 ? "True"
474 : "False")));
475 cli_set_case_sensitive(
476 srv->cli,
477 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
478 ? True
479 : False));
483 * Regenerate the dev value since it's based on both
484 * server and share
486 if (srv) {
487 const char *remote_name =
488 smbXcli_conn_remote_name(srv->cli->conn);
490 srv->dev = (dev_t)(str_checksum(remote_name) ^
491 str_checksum(srv->cli->share));
496 not_found:
498 /* If we have a connection... */
499 if (srv) {
501 /* ... then we're done here. Give 'em what they came for. */
502 *in_cache = true;
503 goto done;
506 /* If we're not asked to connect when a connection doesn't exist... */
507 if (! connect_if_not_found) {
508 /* ... then we're done here. */
509 return NULL;
512 if (!*pp_workgroup || !*pp_username || !*pp_password) {
513 errno = ENOMEM;
514 return NULL;
517 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
519 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
521 status = NT_STATUS_UNSUCCESSFUL;
523 if (context->internal->smb_encryption_level > SMBC_ENCRYPTLEVEL_NONE) {
524 signing_state = SMB_SIGNING_REQUIRED;
527 if (port == 0) {
528 if (share == NULL || *share == '\0' || is_ipc) {
530 * Try 139 first for IPC$
532 status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
533 smbc_getNetbiosName(context),
534 signing_state, flags, &c);
538 if (!NT_STATUS_IS_OK(status)) {
540 * No IPC$ or 139 did not work
542 status = cli_connect_nb(server_n, NULL, port, 0x20,
543 smbc_getNetbiosName(context),
544 signing_state, flags, &c);
547 if (!NT_STATUS_IS_OK(status)) {
548 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
549 DBG_ERR("NetBIOS support disabled, unable to connect\n");
552 errno = map_errno_from_nt_status(status);
553 return NULL;
556 cli_set_timeout(c, smbc_getTimeout(context));
558 status = smbXcli_negprot(c->conn,
559 c->timeout,
560 lp_client_min_protocol(),
561 lp_client_max_protocol(),
562 NULL,
563 NULL,
564 NULL);
565 if (!NT_STATUS_IS_OK(status)) {
566 cli_shutdown(c);
567 errno = map_errno_from_nt_status(status);
568 return NULL;
571 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
572 /* Ensure we ask for some initial credits. */
573 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
576 username_used = *pp_username;
577 password_used = *pp_password;
579 creds = SMBC_auth_credentials(c,
580 context,
581 *pp_workgroup,
582 username_used,
583 password_used);
584 if (creds == NULL) {
585 cli_shutdown(c);
586 errno = ENOMEM;
587 return NULL;
590 status = cli_session_setup_creds(c, creds);
591 if (!NT_STATUS_IS_OK(status)) {
593 /* Failed. Try an anonymous login, if allowed by flags. */
594 username_used = "";
595 password_used = "";
597 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
598 !NT_STATUS_IS_OK(cli_session_setup_anon(c))) {
600 cli_shutdown(c);
601 errno = map_errno_from_nt_status(status);
602 return NULL;
606 DEBUG(4,(" session setup ok\n"));
608 /* here's the fun part....to support 'msdfs proxy' shares
609 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
610 here before trying to connect to the original share.
611 cli_check_msdfs_proxy() will fail if it is a normal share. */
613 if (smbXcli_conn_dfs_supported(c->conn) &&
614 cli_check_msdfs_proxy(ctx, c, share,
615 &newserver, &newshare,
616 creds)) {
617 cli_shutdown(c);
618 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
619 newserver, port, newshare, pp_workgroup,
620 pp_username, pp_password, in_cache);
621 TALLOC_FREE(newserver);
622 TALLOC_FREE(newshare);
623 return srv;
626 /* must be a normal share */
628 status = cli_tree_connect_creds(c, share, "?????", creds);
629 if (!NT_STATUS_IS_OK(status)) {
630 cli_shutdown(c);
631 errno = map_errno_from_nt_status(status);
632 return NULL;
635 DEBUG(4,(" tconx ok\n"));
637 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
638 tcon = c->smb2.tcon;
639 } else {
640 tcon = c->smb1.tcon;
643 /* Determine if this share supports case sensitivity */
644 if (is_ipc) {
645 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
646 status = NT_STATUS_OK;
647 } else {
648 status = cli_get_fs_attr_info(c, &fs_attrs);
651 if (!NT_STATUS_IS_OK(status)) {
652 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
653 nt_errstr(status)));
656 * We can't determine the case sensitivity of the share. We
657 * have no choice but to use the user-specified case
658 * sensitivity setting.
660 if (smbc_getOptionCaseSensitive(context)) {
661 cli_set_case_sensitive(c, True);
662 } else {
663 cli_set_case_sensitive(c, False);
665 } else if (!is_ipc) {
666 DEBUG(4, ("Case sensitive: %s\n",
667 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
668 ? "True"
669 : "False")));
670 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
674 * Ok, we have got a nice connection
675 * Let's allocate a server structure.
678 srv = SMB_MALLOC_P(SMBCSRV);
679 if (!srv) {
680 cli_shutdown(c);
681 errno = ENOMEM;
682 return NULL;
685 ZERO_STRUCTP(srv);
686 DLIST_ADD(srv->cli, c);
687 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
688 srv->no_pathinfo = False;
689 srv->no_pathinfo2 = False;
690 srv->no_pathinfo3 = False;
691 srv->no_nt_session = False;
693 done:
694 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
695 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
696 } else {
697 workgroup = *pp_workgroup;
699 if(!workgroup) {
700 if (c != NULL) {
701 cli_shutdown(c);
703 SAFE_FREE(srv);
704 return NULL;
707 /* set the credentials to make DFS work */
708 smbc_set_credentials_with_fallback(context,
709 workgroup,
710 *pp_username,
711 *pp_password);
713 return srv;
716 SMBCSRV *
717 SMBC_server(TALLOC_CTX *ctx,
718 SMBCCTX *context,
719 bool connect_if_not_found,
720 const char *server,
721 uint16_t port,
722 const char *share,
723 char **pp_workgroup,
724 char **pp_username,
725 char **pp_password)
727 SMBCSRV *srv=NULL;
728 bool in_cache = false;
730 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
731 server, port, share, pp_workgroup,
732 pp_username, pp_password, &in_cache);
734 if (!srv) {
735 return NULL;
737 if (in_cache) {
738 return srv;
741 /* Now add it to the cache (internal or external) */
742 /* Let the cache function set errno if it wants to */
743 errno = 0;
744 if (smbc_getFunctionAddCachedServer(context)(context, srv,
745 server, share,
746 *pp_workgroup,
747 *pp_username)) {
748 int saved_errno = errno;
749 DEBUG(3, (" Failed to add server to cache\n"));
750 errno = saved_errno;
751 if (errno == 0) {
752 errno = ENOMEM;
754 SAFE_FREE(srv);
755 return NULL;
758 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
759 server, share, srv));
761 DLIST_ADD(context->internal->servers, srv);
762 return srv;
766 * Connect to a server for getting/setting attributes, possibly on an existing
767 * connection. This works similarly to SMBC_server().
769 SMBCSRV *
770 SMBC_attr_server(TALLOC_CTX *ctx,
771 SMBCCTX *context,
772 const char *server,
773 uint16_t port,
774 const char *share,
775 char **pp_workgroup,
776 char **pp_username,
777 char **pp_password)
779 int flags;
780 struct cli_state *ipc_cli = NULL;
781 struct rpc_pipe_client *pipe_hnd = NULL;
782 NTSTATUS nt_status;
783 SMBCSRV *srv=NULL;
784 SMBCSRV *ipc_srv=NULL;
787 * Use srv->cli->desthost and srv->cli->share instead of
788 * server and share below to connect to the actual share,
789 * i.e., a normal share or a referred share from
790 * 'msdfs proxy' share.
792 srv = SMBC_server(ctx, context, true, server, port, share,
793 pp_workgroup, pp_username, pp_password);
794 if (!srv) {
795 return NULL;
797 server = smbXcli_conn_remote_name(srv->cli->conn);
798 share = srv->cli->share;
801 * See if we've already created this special connection. Reference
802 * our "special" share name '*IPC$', which is an impossible real share
803 * name due to the leading asterisk.
805 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
806 pp_workgroup, pp_username, pp_password);
807 if (!ipc_srv) {
808 struct cli_credentials *creds = NULL;
810 /* We didn't find a cached connection. Get the password */
811 if (!*pp_password || (*pp_password)[0] == '\0') {
812 /* ... then retrieve it now. */
813 SMBC_call_auth_fn(ctx, context, server, share,
814 pp_workgroup,
815 pp_username,
816 pp_password);
817 if (!*pp_workgroup || !*pp_username || !*pp_password) {
818 errno = ENOMEM;
819 return NULL;
823 flags = 0;
825 creds = SMBC_auth_credentials(NULL,
826 context,
827 *pp_workgroup,
828 *pp_username,
829 *pp_password);
830 if (creds == NULL) {
831 errno = ENOMEM;
832 return NULL;
835 nt_status = cli_full_connection_creds(&ipc_cli,
836 lp_netbios_name(), server,
837 NULL, 0, "IPC$", "?????",
838 creds,
839 flags);
840 if (! NT_STATUS_IS_OK(nt_status)) {
841 TALLOC_FREE(creds);
842 DEBUG(1,("cli_full_connection failed! (%s)\n",
843 nt_errstr(nt_status)));
844 errno = ENOTSUP;
845 return NULL;
847 talloc_steal(ipc_cli, creds);
849 ipc_srv = SMB_MALLOC_P(SMBCSRV);
850 if (!ipc_srv) {
851 errno = ENOMEM;
852 cli_shutdown(ipc_cli);
853 return NULL;
856 ZERO_STRUCTP(ipc_srv);
857 DLIST_ADD(ipc_srv->cli, ipc_cli);
859 nt_status = cli_rpc_pipe_open_noauth(
860 ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
861 if (!NT_STATUS_IS_OK(nt_status)) {
862 DEBUG(1, ("cli_nt_session_open fail!\n"));
863 errno = ENOTSUP;
864 cli_shutdown(ipc_srv->cli);
865 free(ipc_srv);
866 return NULL;
870 * Some systems don't support
871 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
872 * so we might as well do it too.
875 nt_status = rpccli_lsa_open_policy(
876 pipe_hnd,
877 talloc_tos(),
878 True,
879 GENERIC_EXECUTE_ACCESS,
880 &ipc_srv->pol);
882 if (!NT_STATUS_IS_OK(nt_status)) {
883 cli_shutdown(ipc_srv->cli);
884 free(ipc_srv);
885 errno = cli_status_to_errno(nt_status);
886 return NULL;
889 /* now add it to the cache (internal or external) */
891 errno = 0; /* let cache function set errno if it likes */
892 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
893 server,
894 "*IPC$",
895 *pp_workgroup,
896 *pp_username)) {
897 DEBUG(3, (" Failed to add server to cache\n"));
898 if (errno == 0) {
899 errno = ENOMEM;
901 cli_shutdown(ipc_srv->cli);
902 free(ipc_srv);
903 return NULL;
906 DLIST_ADD(context->internal->servers, ipc_srv);
909 return ipc_srv;