librpc: use DATA_BLOB in CFDATA structure
[Samba.git] / source3 / libsmb / libsmb_server.c
blobb0e5926fa65613c7fc461be0967d82f42d4e371b
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 useable 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 return 1;
66 server->last_echo_time = now;
68 return 0;
72 * Remove a server from the cached server list it's unused.
73 * On success, 0 is returned. 1 is returned if the server could not be removed.
75 * Also useable outside libsmbclient
77 int
78 SMBC_remove_unused_server(SMBCCTX * context,
79 SMBCSRV * srv)
81 SMBCFILE * file;
83 /* are we being fooled ? */
84 if (!context || !context->internal->initialized || !srv) {
85 return 1;
88 /* Check all open files/directories for a relation with this server */
89 for (file = context->internal->files; file; file = file->next) {
90 if (file->srv == srv) {
91 /* Still used */
92 DEBUG(3, ("smbc_remove_usused_server: "
93 "%p still used by %p.\n",
94 srv, file));
95 return 1;
99 DLIST_REMOVE(context->internal->servers, srv);
101 cli_shutdown(srv->cli);
102 srv->cli = NULL;
104 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
106 smbc_getFunctionRemoveCachedServer(context)(context, srv);
108 SAFE_FREE(srv);
109 return 0;
112 /****************************************************************
113 * Call the auth_fn with fixed size (fstring) buffers.
114 ***************************************************************/
115 static void
116 SMBC_call_auth_fn(TALLOC_CTX *ctx,
117 SMBCCTX *context,
118 const char *server,
119 const char *share,
120 char **pp_workgroup,
121 char **pp_username,
122 char **pp_password)
124 fstring workgroup = { 0 };
125 fstring username = { 0 };
126 fstring password = { 0 };
127 smbc_get_auth_data_with_context_fn auth_with_context_fn;
129 if (*pp_workgroup != NULL) {
130 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
132 if (*pp_username != NULL) {
133 strlcpy(username, *pp_username, sizeof(username));
135 if (*pp_password != NULL) {
136 strlcpy(password, *pp_password, sizeof(password));
139 /* See if there's an authentication with context function provided */
140 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
141 if (auth_with_context_fn)
143 (* auth_with_context_fn)(context,
144 server, share,
145 workgroup, sizeof(workgroup),
146 username, sizeof(username),
147 password, sizeof(password));
149 else
151 smbc_getFunctionAuthData(context)(server, share,
152 workgroup, sizeof(workgroup),
153 username, sizeof(username),
154 password, sizeof(password));
157 TALLOC_FREE(*pp_workgroup);
158 TALLOC_FREE(*pp_username);
159 TALLOC_FREE(*pp_password);
161 *pp_workgroup = talloc_strdup(ctx, workgroup);
162 *pp_username = talloc_strdup(ctx, username);
163 *pp_password = talloc_strdup(ctx, password);
167 void
168 SMBC_get_auth_data(const char *server, const char *share,
169 char *workgroup_buf, int workgroup_buf_len,
170 char *username_buf, int username_buf_len,
171 char *password_buf, int password_buf_len)
173 /* Default function just uses provided data. Nothing to do. */
178 SMBCSRV *
179 SMBC_find_server(TALLOC_CTX *ctx,
180 SMBCCTX *context,
181 const char *server,
182 const char *share,
183 char **pp_workgroup,
184 char **pp_username,
185 char **pp_password)
187 SMBCSRV *srv;
188 int auth_called = 0;
190 if (!pp_workgroup || !pp_username || !pp_password) {
191 return NULL;
194 check_server_cache:
196 srv = smbc_getFunctionGetCachedServer(context)(context,
197 server, share,
198 *pp_workgroup,
199 *pp_username);
201 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
202 !*pp_password || !(*pp_password)[0])) {
203 SMBC_call_auth_fn(ctx, context, server, share,
204 pp_workgroup, pp_username, pp_password);
207 * However, smbc_auth_fn may have picked up info relating to
208 * an existing connection, so try for an existing connection
209 * again ...
211 auth_called = 1;
212 goto check_server_cache;
216 if (srv) {
217 if (smbc_getFunctionCheckServer(context)(context, srv)) {
219 * This server is no good anymore
220 * Try to remove it and check for more possible
221 * servers in the cache
223 if (smbc_getFunctionRemoveUnusedServer(context)(context,
224 srv)) {
226 * We could not remove the server completely,
227 * remove it from the cache so we will not get
228 * it again. It will be removed when the last
229 * file/dir is closed.
231 smbc_getFunctionRemoveCachedServer(context)(context,
232 srv);
236 * Maybe there are more cached connections to this
237 * server
239 goto check_server_cache;
242 return srv;
245 return NULL;
249 * Connect to a server, possibly on an existing connection
251 * Here, what we want to do is: If the server and username
252 * match an existing connection, reuse that, otherwise, establish a
253 * new connection.
255 * If we have to create a new connection, call the auth_fn to get the
256 * info we need, unless the username and password were passed in.
259 static SMBCSRV *
260 SMBC_server_internal(TALLOC_CTX *ctx,
261 SMBCCTX *context,
262 bool connect_if_not_found,
263 const char *server,
264 uint16_t port,
265 const char *share,
266 char **pp_workgroup,
267 char **pp_username,
268 char **pp_password,
269 bool *in_cache)
271 SMBCSRV *srv=NULL;
272 char *workgroup = NULL;
273 struct cli_state *c = NULL;
274 const char *server_n = server;
275 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
276 uint32_t fs_attrs = 0;
277 const char *username_used = NULL;
278 const char *password_used = NULL;
279 NTSTATUS status;
280 char *newserver, *newshare;
281 int flags = 0;
282 struct smbXcli_tcon *tcon = NULL;
283 int signing_state = SMB_SIGNING_DEFAULT;
284 struct cli_credentials *creds = NULL;
285 bool use_kerberos = false;
286 bool fallback_after_kerberos = false;
287 bool use_ccache = false;
288 bool pw_nt_hash = false;
290 ZERO_STRUCT(c);
291 *in_cache = false;
293 if (server[0] == 0) {
294 errno = EPERM;
295 return NULL;
298 /* Look for a cached connection */
299 srv = SMBC_find_server(ctx, context, server, share,
300 pp_workgroup, pp_username, pp_password);
303 * If we found a connection and we're only allowed one share per
304 * server...
306 if (srv &&
307 share != NULL && *share != '\0' &&
308 smbc_getOptionOneSharePerServer(context)) {
311 * ... then if there's no current connection to the share,
312 * connect to it. SMBC_find_server(), or rather the function
313 * pointed to by context->get_cached_srv_fn which
314 * was called by SMBC_find_server(), will have issued a tree
315 * disconnect if the requested share is not the same as the
316 * one that was already connected.
320 * Use srv->cli->desthost and srv->cli->share instead of
321 * server and share below to connect to the actual share,
322 * i.e., a normal share or a referred share from
323 * 'msdfs proxy' share.
325 if (!cli_state_has_tcon(srv->cli)) {
326 /* Ensure we have accurate auth info */
327 SMBC_call_auth_fn(ctx, context,
328 smbXcli_conn_remote_name(srv->cli->conn),
329 srv->cli->share,
330 pp_workgroup,
331 pp_username,
332 pp_password);
334 if (!*pp_workgroup || !*pp_username || !*pp_password) {
335 errno = ENOMEM;
336 cli_shutdown(srv->cli);
337 srv->cli = NULL;
338 smbc_getFunctionRemoveCachedServer(context)(context,
339 srv);
340 return NULL;
344 * We don't need to renegotiate encryption
345 * here as the encryption context is not per
346 * tid.
349 status = cli_tree_connect(srv->cli,
350 srv->cli->share,
351 "?????",
352 *pp_password);
353 if (!NT_STATUS_IS_OK(status)) {
354 errno = map_errno_from_nt_status(status);
355 cli_shutdown(srv->cli);
356 srv->cli = NULL;
357 smbc_getFunctionRemoveCachedServer(context)(context,
358 srv);
359 srv = NULL;
362 /* Determine if this share supports case sensitivity */
363 if (is_ipc) {
364 DEBUG(4,
365 ("IPC$ so ignore case sensitivity\n"));
366 status = NT_STATUS_OK;
367 } else {
368 status = cli_get_fs_attr_info(c, &fs_attrs);
371 if (!NT_STATUS_IS_OK(status)) {
372 DEBUG(4, ("Could not retrieve "
373 "case sensitivity flag: %s.\n",
374 nt_errstr(status)));
377 * We can't determine the case sensitivity of
378 * the share. We have no choice but to use the
379 * user-specified case sensitivity setting.
381 if (smbc_getOptionCaseSensitive(context)) {
382 cli_set_case_sensitive(c, True);
383 } else {
384 cli_set_case_sensitive(c, False);
386 } else if (!is_ipc) {
387 DEBUG(4,
388 ("Case sensitive: %s\n",
389 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
390 ? "True"
391 : "False")));
392 cli_set_case_sensitive(
394 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
395 ? True
396 : False));
400 * Regenerate the dev value since it's based on both
401 * server and share
403 if (srv) {
404 const char *remote_name =
405 smbXcli_conn_remote_name(srv->cli->conn);
407 srv->dev = (dev_t)(str_checksum(remote_name) ^
408 str_checksum(srv->cli->share));
413 /* If we have a connection... */
414 if (srv) {
416 /* ... then we're done here. Give 'em what they came for. */
417 *in_cache = true;
418 goto done;
421 /* If we're not asked to connect when a connection doesn't exist... */
422 if (! connect_if_not_found) {
423 /* ... then we're done here. */
424 return NULL;
427 if (!*pp_workgroup || !*pp_username || !*pp_password) {
428 errno = ENOMEM;
429 return NULL;
432 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
434 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
436 status = NT_STATUS_UNSUCCESSFUL;
438 if (smbc_getOptionUseKerberos(context)) {
439 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
440 use_kerberos = true;
443 if (smbc_getOptionFallbackAfterKerberos(context)) {
444 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
445 fallback_after_kerberos = true;
448 if (smbc_getOptionUseCCache(context)) {
449 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
450 use_ccache = true;
453 if (smbc_getOptionUseNTHash(context)) {
454 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
455 pw_nt_hash = true;
458 if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
459 signing_state = SMB_SIGNING_REQUIRED;
462 if (port == 0) {
463 if (share == NULL || *share == '\0' || is_ipc) {
465 * Try 139 first for IPC$
467 status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
468 smbc_getNetbiosName(context),
469 signing_state, flags, &c);
473 if (!NT_STATUS_IS_OK(status)) {
475 * No IPC$ or 139 did not work
477 status = cli_connect_nb(server_n, NULL, port, 0x20,
478 smbc_getNetbiosName(context),
479 signing_state, flags, &c);
482 if (!NT_STATUS_IS_OK(status)) {
483 errno = map_errno_from_nt_status(status);
484 return NULL;
487 cli_set_timeout(c, smbc_getTimeout(context));
489 status = smbXcli_negprot(c->conn, c->timeout,
490 lp_client_min_protocol(),
491 lp_client_max_protocol());
492 if (!NT_STATUS_IS_OK(status)) {
493 cli_shutdown(c);
494 errno = ETIMEDOUT;
495 return NULL;
498 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
499 /* Ensure we ask for some initial credits. */
500 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
503 username_used = *pp_username;
504 password_used = *pp_password;
506 creds = cli_session_creds_init(c,
507 username_used,
508 *pp_workgroup,
509 NULL, /* realm */
510 password_used,
511 use_kerberos,
512 fallback_after_kerberos,
513 use_ccache,
514 pw_nt_hash);
515 if (creds == NULL) {
516 cli_shutdown(c);
517 errno = ENOMEM;
518 return NULL;
521 status = cli_session_setup_creds(c, creds);
522 if (!NT_STATUS_IS_OK(status)) {
524 /* Failed. Try an anonymous login, if allowed by flags. */
525 username_used = "";
526 password_used = "";
528 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
529 !NT_STATUS_IS_OK(cli_session_setup_anon(c))) {
531 cli_shutdown(c);
532 errno = EPERM;
533 return NULL;
537 DEBUG(4,(" session setup ok\n"));
539 /* here's the fun part....to support 'msdfs proxy' shares
540 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
541 here before trying to connect to the original share.
542 cli_check_msdfs_proxy() will fail if it is a normal share. */
544 if (smbXcli_conn_dfs_supported(c->conn) &&
545 cli_check_msdfs_proxy(ctx, c, share,
546 &newserver, &newshare,
547 /* FIXME: cli_check_msdfs_proxy() does
548 not support smbc_smb_encrypt_level type */
549 context->internal->smb_encryption_level ?
550 true : false,
551 creds)) {
552 cli_shutdown(c);
553 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
554 newserver, port, newshare, pp_workgroup,
555 pp_username, pp_password, in_cache);
556 TALLOC_FREE(newserver);
557 TALLOC_FREE(newshare);
558 return srv;
561 /* must be a normal share */
563 status = cli_tree_connect_creds(c, share, "?????", creds);
564 if (!NT_STATUS_IS_OK(status)) {
565 errno = map_errno_from_nt_status(status);
566 cli_shutdown(c);
567 return NULL;
570 DEBUG(4,(" tconx ok\n"));
572 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
573 tcon = c->smb2.tcon;
574 } else {
575 tcon = c->smb1.tcon;
578 /* Determine if this share supports case sensitivity */
579 if (is_ipc) {
580 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
581 status = NT_STATUS_OK;
582 } else {
583 status = cli_get_fs_attr_info(c, &fs_attrs);
586 if (!NT_STATUS_IS_OK(status)) {
587 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
588 nt_errstr(status)));
591 * We can't determine the case sensitivity of the share. We
592 * have no choice but to use the user-specified case
593 * sensitivity setting.
595 if (smbc_getOptionCaseSensitive(context)) {
596 cli_set_case_sensitive(c, True);
597 } else {
598 cli_set_case_sensitive(c, False);
600 } else if (!is_ipc) {
601 DEBUG(4, ("Case sensitive: %s\n",
602 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
603 ? "True"
604 : "False")));
605 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
608 if (context->internal->smb_encryption_level) {
609 /* Attempt encryption. */
610 status = cli_cm_force_encryption_creds(c,
611 creds,
612 share);
613 if (!NT_STATUS_IS_OK(status)) {
616 * context->smb_encryption_level == 1
617 * means don't fail if encryption can't be negotiated,
618 * == 2 means fail if encryption can't be negotiated.
621 DEBUG(4,(" SMB encrypt failed\n"));
623 if (context->internal->smb_encryption_level == 2) {
624 cli_shutdown(c);
625 errno = EPERM;
626 return NULL;
629 DEBUG(4,(" SMB encrypt ok\n"));
633 * Ok, we have got a nice connection
634 * Let's allocate a server structure.
637 srv = SMB_MALLOC_P(SMBCSRV);
638 if (!srv) {
639 cli_shutdown(c);
640 errno = ENOMEM;
641 return NULL;
644 ZERO_STRUCTP(srv);
645 DLIST_ADD(srv->cli, c);
646 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
647 srv->no_pathinfo = False;
648 srv->no_pathinfo2 = False;
649 srv->no_pathinfo3 = False;
650 srv->no_nt_session = False;
652 done:
653 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
654 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
655 } else {
656 workgroup = *pp_workgroup;
658 if(!workgroup) {
659 if (c != NULL) {
660 cli_shutdown(c);
662 SAFE_FREE(srv);
663 return NULL;
666 /* set the credentials to make DFS work */
667 smbc_set_credentials_with_fallback(context,
668 workgroup,
669 *pp_username,
670 *pp_password);
672 return srv;
675 SMBCSRV *
676 SMBC_server(TALLOC_CTX *ctx,
677 SMBCCTX *context,
678 bool connect_if_not_found,
679 const char *server,
680 uint16_t port,
681 const char *share,
682 char **pp_workgroup,
683 char **pp_username,
684 char **pp_password)
686 SMBCSRV *srv=NULL;
687 bool in_cache = false;
689 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
690 server, port, share, pp_workgroup,
691 pp_username, pp_password, &in_cache);
693 if (!srv) {
694 return NULL;
696 if (in_cache) {
697 return srv;
700 /* Now add it to the cache (internal or external) */
701 /* Let the cache function set errno if it wants to */
702 errno = 0;
703 if (smbc_getFunctionAddCachedServer(context)(context, srv,
704 server, share,
705 *pp_workgroup,
706 *pp_username)) {
707 int saved_errno = errno;
708 DEBUG(3, (" Failed to add server to cache\n"));
709 errno = saved_errno;
710 if (errno == 0) {
711 errno = ENOMEM;
713 SAFE_FREE(srv);
714 return NULL;
717 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
718 server, share, srv));
720 DLIST_ADD(context->internal->servers, srv);
721 return srv;
725 * Connect to a server for getting/setting attributes, possibly on an existing
726 * connection. This works similarly to SMBC_server().
728 SMBCSRV *
729 SMBC_attr_server(TALLOC_CTX *ctx,
730 SMBCCTX *context,
731 const char *server,
732 uint16_t port,
733 const char *share,
734 char **pp_workgroup,
735 char **pp_username,
736 char **pp_password)
738 int flags;
739 struct cli_state *ipc_cli = NULL;
740 struct rpc_pipe_client *pipe_hnd = NULL;
741 NTSTATUS nt_status;
742 SMBCSRV *srv=NULL;
743 SMBCSRV *ipc_srv=NULL;
746 * Use srv->cli->desthost and srv->cli->share instead of
747 * server and share below to connect to the actual share,
748 * i.e., a normal share or a referred share from
749 * 'msdfs proxy' share.
751 srv = SMBC_server(ctx, context, true, server, port, share,
752 pp_workgroup, pp_username, pp_password);
753 if (!srv) {
754 return NULL;
756 server = smbXcli_conn_remote_name(srv->cli->conn);
757 share = srv->cli->share;
760 * See if we've already created this special connection. Reference
761 * our "special" share name '*IPC$', which is an impossible real share
762 * name due to the leading asterisk.
764 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
765 pp_workgroup, pp_username, pp_password);
766 if (!ipc_srv) {
767 int signing_state = SMB_SIGNING_DEFAULT;
769 /* We didn't find a cached connection. Get the password */
770 if (!*pp_password || (*pp_password)[0] == '\0') {
771 /* ... then retrieve it now. */
772 SMBC_call_auth_fn(ctx, context, server, share,
773 pp_workgroup,
774 pp_username,
775 pp_password);
776 if (!*pp_workgroup || !*pp_username || !*pp_password) {
777 errno = ENOMEM;
778 return NULL;
782 flags = 0;
783 if (smbc_getOptionUseKerberos(context)) {
784 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
786 if (smbc_getOptionUseCCache(context)) {
787 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
789 if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
790 signing_state = SMB_SIGNING_REQUIRED;
793 nt_status = cli_full_connection(&ipc_cli,
794 lp_netbios_name(), server,
795 NULL, 0, "IPC$", "?????",
796 *pp_username,
797 *pp_workgroup,
798 *pp_password,
799 flags,
800 signing_state);
801 if (! NT_STATUS_IS_OK(nt_status)) {
802 DEBUG(1,("cli_full_connection failed! (%s)\n",
803 nt_errstr(nt_status)));
804 errno = ENOTSUP;
805 return NULL;
808 if (context->internal->smb_encryption_level) {
809 /* Attempt encryption. */
810 nt_status = cli_cm_force_encryption(ipc_cli,
811 *pp_username,
812 *pp_password,
813 *pp_workgroup,
814 "IPC$");
815 if (!NT_STATUS_IS_OK(nt_status)) {
818 * context->smb_encryption_level ==
819 * 1 means don't fail if encryption can't be
820 * negotiated, == 2 means fail if encryption
821 * can't be negotiated.
824 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
826 if (context->internal->smb_encryption_level == 2) {
827 cli_shutdown(ipc_cli);
828 errno = EPERM;
829 return NULL;
832 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
835 ipc_srv = SMB_MALLOC_P(SMBCSRV);
836 if (!ipc_srv) {
837 errno = ENOMEM;
838 cli_shutdown(ipc_cli);
839 return NULL;
842 ZERO_STRUCTP(ipc_srv);
843 DLIST_ADD(ipc_srv->cli, ipc_cli);
845 nt_status = cli_rpc_pipe_open_noauth(
846 ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
847 if (!NT_STATUS_IS_OK(nt_status)) {
848 DEBUG(1, ("cli_nt_session_open fail!\n"));
849 errno = ENOTSUP;
850 cli_shutdown(ipc_srv->cli);
851 free(ipc_srv);
852 return NULL;
856 * Some systems don't support
857 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
858 * so we might as well do it too.
861 nt_status = rpccli_lsa_open_policy(
862 pipe_hnd,
863 talloc_tos(),
864 True,
865 GENERIC_EXECUTE_ACCESS,
866 &ipc_srv->pol);
868 if (!NT_STATUS_IS_OK(nt_status)) {
869 errno = SMBC_errno(context, ipc_srv->cli);
870 cli_shutdown(ipc_srv->cli);
871 free(ipc_srv);
872 return NULL;
875 /* now add it to the cache (internal or external) */
877 errno = 0; /* let cache function set errno if it likes */
878 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
879 server,
880 "*IPC$",
881 *pp_workgroup,
882 *pp_username)) {
883 DEBUG(3, (" Failed to add server to cache\n"));
884 if (errno == 0) {
885 errno = ENOMEM;
887 cli_shutdown(ipc_srv->cli);
888 free(ipc_srv);
889 return NULL;
892 DLIST_ADD(context->internal->servers, ipc_srv);
895 return ipc_srv;