s3: Fix Coverity ID 2325, RESOURCE_LEAK
[Samba.git] / source3 / libsmb / libsmb_server.c
blob961c9cde83e06ad70de026aac767f158f4cde59c
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 "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "../librpc/gen_ndr/ndr_lsa.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "libcli/security/security.h"
33 #include "libsmb/nmblib.h"
35 /*
36 * Check a server for being alive and well.
37 * returns 0 if the server is in shape. Returns 1 on error
39 * Also useable outside libsmbclient to enable external cache
40 * to do some checks too.
42 int
43 SMBC_check_server(SMBCCTX * context,
44 SMBCSRV * server)
46 socklen_t size;
47 struct sockaddr addr;
49 size = sizeof(addr);
50 return (getpeername(server->cli->fd, &addr, &size) == -1);
53 /*
54 * Remove a server from the cached server list it's unused.
55 * On success, 0 is returned. 1 is returned if the server could not be removed.
57 * Also useable outside libsmbclient
59 int
60 SMBC_remove_unused_server(SMBCCTX * context,
61 SMBCSRV * srv)
63 SMBCFILE * file;
65 /* are we being fooled ? */
66 if (!context || !context->internal->initialized || !srv) {
67 return 1;
70 /* Check all open files/directories for a relation with this server */
71 for (file = context->internal->files; file; file = file->next) {
72 if (file->srv == srv) {
73 /* Still used */
74 DEBUG(3, ("smbc_remove_usused_server: "
75 "%p still used by %p.\n",
76 srv, file));
77 return 1;
81 DLIST_REMOVE(context->internal->servers, srv);
83 cli_shutdown(srv->cli);
84 srv->cli = NULL;
86 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
88 smbc_getFunctionRemoveCachedServer(context)(context, srv);
90 SAFE_FREE(srv);
91 return 0;
94 /****************************************************************
95 * Call the auth_fn with fixed size (fstring) buffers.
96 ***************************************************************/
97 void
98 SMBC_call_auth_fn(TALLOC_CTX *ctx,
99 SMBCCTX *context,
100 const char *server,
101 const char *share,
102 char **pp_workgroup,
103 char **pp_username,
104 char **pp_password)
106 fstring workgroup;
107 fstring username;
108 fstring password;
109 smbc_get_auth_data_with_context_fn auth_with_context_fn;
111 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
112 strlcpy(username, *pp_username, sizeof(username));
113 strlcpy(password, *pp_password, sizeof(password));
115 /* See if there's an authentication with context function provided */
116 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
117 if (auth_with_context_fn)
119 (* auth_with_context_fn)(context,
120 server, share,
121 workgroup, sizeof(workgroup),
122 username, sizeof(username),
123 password, sizeof(password));
125 else
127 smbc_getFunctionAuthData(context)(server, share,
128 workgroup, sizeof(workgroup),
129 username, sizeof(username),
130 password, sizeof(password));
133 TALLOC_FREE(*pp_workgroup);
134 TALLOC_FREE(*pp_username);
135 TALLOC_FREE(*pp_password);
137 *pp_workgroup = talloc_strdup(ctx, workgroup);
138 *pp_username = talloc_strdup(ctx, username);
139 *pp_password = talloc_strdup(ctx, password);
143 void
144 SMBC_get_auth_data(const char *server, const char *share,
145 char *workgroup_buf, int workgroup_buf_len,
146 char *username_buf, int username_buf_len,
147 char *password_buf, int password_buf_len)
149 /* Default function just uses provided data. Nothing to do. */
154 SMBCSRV *
155 SMBC_find_server(TALLOC_CTX *ctx,
156 SMBCCTX *context,
157 const char *server,
158 const char *share,
159 char **pp_workgroup,
160 char **pp_username,
161 char **pp_password)
163 SMBCSRV *srv;
164 int auth_called = 0;
166 if (!pp_workgroup || !pp_username || !pp_password) {
167 return NULL;
170 check_server_cache:
172 srv = smbc_getFunctionGetCachedServer(context)(context,
173 server, share,
174 *pp_workgroup,
175 *pp_username);
177 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
178 !*pp_password || !(*pp_password)[0])) {
179 SMBC_call_auth_fn(ctx, context, server, share,
180 pp_workgroup, pp_username, pp_password);
183 * However, smbc_auth_fn may have picked up info relating to
184 * an existing connection, so try for an existing connection
185 * again ...
187 auth_called = 1;
188 goto check_server_cache;
192 if (srv) {
193 if (smbc_getFunctionCheckServer(context)(context, srv)) {
195 * This server is no good anymore
196 * Try to remove it and check for more possible
197 * servers in the cache
199 if (smbc_getFunctionRemoveUnusedServer(context)(context,
200 srv)) {
202 * We could not remove the server completely,
203 * remove it from the cache so we will not get
204 * it again. It will be removed when the last
205 * file/dir is closed.
207 smbc_getFunctionRemoveCachedServer(context)(context,
208 srv);
212 * Maybe there are more cached connections to this
213 * server
215 goto check_server_cache;
218 return srv;
221 return NULL;
225 * Connect to a server, possibly on an existing connection
227 * Here, what we want to do is: If the server and username
228 * match an existing connection, reuse that, otherwise, establish a
229 * new connection.
231 * If we have to create a new connection, call the auth_fn to get the
232 * info we need, unless the username and password were passed in.
235 static SMBCSRV *
236 SMBC_server_internal(TALLOC_CTX *ctx,
237 SMBCCTX *context,
238 bool connect_if_not_found,
239 const char *server,
240 const char *share,
241 char **pp_workgroup,
242 char **pp_username,
243 char **pp_password,
244 bool *in_cache)
246 SMBCSRV *srv=NULL;
247 char *workgroup = NULL;
248 struct cli_state *c;
249 struct nmb_name called, calling;
250 const char *server_n = server;
251 struct sockaddr_storage ss;
252 int tried_reverse = 0;
253 int port_try_first;
254 int port_try_next;
255 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
256 uint32 fs_attrs = 0;
257 const char *username_used;
258 NTSTATUS status;
259 char *newserver, *newshare;
261 zero_sockaddr(&ss);
262 ZERO_STRUCT(c);
263 *in_cache = false;
265 if (server[0] == 0) {
266 errno = EPERM;
267 return NULL;
270 /* Look for a cached connection */
271 srv = SMBC_find_server(ctx, context, server, share,
272 pp_workgroup, pp_username, pp_password);
275 * If we found a connection and we're only allowed one share per
276 * server...
278 if (srv &&
279 *share != '\0' &&
280 smbc_getOptionOneSharePerServer(context)) {
283 * ... then if there's no current connection to the share,
284 * connect to it. SMBC_find_server(), or rather the function
285 * pointed to by context->get_cached_srv_fn which
286 * was called by SMBC_find_server(), will have issued a tree
287 * disconnect if the requested share is not the same as the
288 * one that was already connected.
292 * Use srv->cli->desthost and srv->cli->share instead of
293 * server and share below to connect to the actual share,
294 * i.e., a normal share or a referred share from
295 * 'msdfs proxy' share.
297 if (srv->cli->cnum == (uint16) -1) {
298 /* Ensure we have accurate auth info */
299 SMBC_call_auth_fn(ctx, context,
300 srv->cli->desthost,
301 srv->cli->share,
302 pp_workgroup,
303 pp_username,
304 pp_password);
306 if (!*pp_workgroup || !*pp_username || !*pp_password) {
307 errno = ENOMEM;
308 cli_shutdown(srv->cli);
309 srv->cli = NULL;
310 smbc_getFunctionRemoveCachedServer(context)(context,
311 srv);
312 return NULL;
316 * We don't need to renegotiate encryption
317 * here as the encryption context is not per
318 * tid.
321 status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
322 *pp_password,
323 strlen(*pp_password)+1);
324 if (!NT_STATUS_IS_OK(status)) {
325 errno = map_errno_from_nt_status(status);
326 cli_shutdown(srv->cli);
327 srv->cli = NULL;
328 smbc_getFunctionRemoveCachedServer(context)(context,
329 srv);
330 srv = NULL;
333 /* Determine if this share supports case sensitivity */
334 if (is_ipc) {
335 DEBUG(4,
336 ("IPC$ so ignore case sensitivity\n"));
337 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
338 DEBUG(4, ("Could not retrieve "
339 "case sensitivity flag: %s.\n",
340 cli_errstr(c)));
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 {
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 make_nmb_name(&calling, smbc_getNetbiosName(context), 0x0);
396 make_nmb_name(&called , server, 0x20);
398 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
400 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
402 again:
404 zero_sockaddr(&ss);
406 /* have to open a new connection */
407 if ((c = cli_initialise()) == NULL) {
408 errno = ENOMEM;
409 return NULL;
412 if (smbc_getOptionUseKerberos(context)) {
413 c->use_kerberos = True;
416 if (smbc_getOptionFallbackAfterKerberos(context)) {
417 c->fallback_after_kerberos = True;
420 if (smbc_getOptionUseCCache(context)) {
421 c->use_ccache = True;
424 c->timeout = smbc_getTimeout(context);
427 * Force use of port 139 for first try if share is $IPC, empty, or
428 * null, so browse lists can work
430 if (share == NULL || *share == '\0' || is_ipc) {
431 port_try_first = 139;
432 port_try_next = 445;
433 } else {
434 port_try_first = 445;
435 port_try_next = 139;
438 c->port = port_try_first;
440 status = cli_connect(c, server_n, &ss);
441 if (!NT_STATUS_IS_OK(status)) {
443 /* First connection attempt failed. Try alternate port. */
444 c->port = port_try_next;
446 status = cli_connect(c, server_n, &ss);
447 if (!NT_STATUS_IS_OK(status)) {
448 cli_shutdown(c);
449 errno = ETIMEDOUT;
450 return NULL;
454 if (!cli_session_request(c, &calling, &called)) {
455 cli_shutdown(c);
456 if (strcmp(called.name, "*SMBSERVER")) {
457 make_nmb_name(&called , "*SMBSERVER", 0x20);
458 goto again;
459 } else { /* Try one more time, but ensure we don't loop */
461 /* Only try this if server is an IP address ... */
463 if (is_ipaddress(server) && !tried_reverse) {
464 fstring remote_name;
465 struct sockaddr_storage rem_ss;
467 if (!interpret_string_addr(&rem_ss, server,
468 NI_NUMERICHOST)) {
469 DEBUG(4, ("Could not convert IP address "
470 "%s to struct sockaddr_storage\n",
471 server));
472 errno = ETIMEDOUT;
473 return NULL;
476 tried_reverse++; /* Yuck */
478 if (name_status_find("*", 0, 0,
479 &rem_ss, remote_name)) {
480 make_nmb_name(&called,
481 remote_name,
482 0x20);
483 goto again;
487 errno = ETIMEDOUT;
488 return NULL;
491 DEBUG(4,(" session request ok\n"));
493 status = cli_negprot(c);
495 if (!NT_STATUS_IS_OK(status)) {
496 cli_shutdown(c);
497 errno = ETIMEDOUT;
498 return NULL;
501 username_used = *pp_username;
503 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
504 *pp_password,
505 strlen(*pp_password),
506 *pp_password,
507 strlen(*pp_password),
508 *pp_workgroup))) {
510 /* Failed. Try an anonymous login, if allowed by flags. */
511 username_used = "";
513 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
514 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
515 *pp_password, 1,
516 *pp_password, 0,
517 *pp_workgroup))) {
519 cli_shutdown(c);
520 errno = EPERM;
521 return NULL;
525 status = cli_init_creds(c, username_used,
526 *pp_workgroup, *pp_password);
527 if (!NT_STATUS_IS_OK(status)) {
528 errno = map_errno_from_nt_status(status);
529 cli_shutdown(c);
530 return NULL;
533 DEBUG(4,(" session setup ok\n"));
535 /* here's the fun part....to support 'msdfs proxy' shares
536 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
537 here before trying to connect to the original share.
538 cli_check_msdfs_proxy() will fail if it is a normal share. */
540 if ((c->capabilities & CAP_DFS) &&
541 cli_check_msdfs_proxy(ctx, c, share,
542 &newserver, &newshare,
543 /* FIXME: cli_check_msdfs_proxy() does
544 not support smbc_smb_encrypt_level type */
545 context->internal->smb_encryption_level ?
546 true : false,
547 *pp_username,
548 *pp_password,
549 *pp_workgroup)) {
550 cli_shutdown(c);
551 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
552 newserver, newshare, pp_workgroup,
553 pp_username, pp_password, in_cache);
554 TALLOC_FREE(newserver);
555 TALLOC_FREE(newshare);
556 return srv;
559 /* must be a normal share */
561 status = cli_tcon_andx(c, share, "?????", *pp_password,
562 strlen(*pp_password)+1);
563 if (!NT_STATUS_IS_OK(status)) {
564 errno = map_errno_from_nt_status(status);
565 cli_shutdown(c);
566 return NULL;
569 DEBUG(4,(" tconx ok\n"));
571 /* Determine if this share supports case sensitivity */
572 if (is_ipc) {
573 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
574 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
575 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
576 cli_errstr(c)));
579 * We can't determine the case sensitivity of the share. We
580 * have no choice but to use the user-specified case
581 * sensitivity setting.
583 if (smbc_getOptionCaseSensitive(context)) {
584 cli_set_case_sensitive(c, True);
585 } else {
586 cli_set_case_sensitive(c, False);
588 } else {
589 DEBUG(4, ("Case sensitive: %s\n",
590 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
591 ? "True"
592 : "False")));
593 cli_set_case_sensitive(c,
594 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
595 ? True
596 : False));
599 if (context->internal->smb_encryption_level) {
600 /* Attempt UNIX smb encryption. */
601 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
602 username_used,
603 *pp_password,
604 *pp_workgroup))) {
607 * context->smb_encryption_level == 1
608 * means don't fail if encryption can't be negotiated,
609 * == 2 means fail if encryption can't be negotiated.
612 DEBUG(4,(" SMB encrypt failed\n"));
614 if (context->internal->smb_encryption_level == 2) {
615 cli_shutdown(c);
616 errno = EPERM;
617 return NULL;
620 DEBUG(4,(" SMB encrypt ok\n"));
624 * Ok, we have got a nice connection
625 * Let's allocate a server structure.
628 srv = SMB_MALLOC_P(SMBCSRV);
629 if (!srv) {
630 cli_shutdown(c);
631 errno = ENOMEM;
632 return NULL;
635 ZERO_STRUCTP(srv);
636 srv->cli = c;
637 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
638 srv->no_pathinfo = False;
639 srv->no_pathinfo2 = False;
640 srv->no_nt_session = False;
642 done:
643 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
644 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
645 } else {
646 workgroup = *pp_workgroup;
648 if(!workgroup) {
649 return NULL;
652 /* set the credentials to make DFS work */
653 smbc_set_credentials_with_fallback(context,
654 workgroup,
655 *pp_username,
656 *pp_password);
658 return srv;
661 SMBCSRV *
662 SMBC_server(TALLOC_CTX *ctx,
663 SMBCCTX *context,
664 bool connect_if_not_found,
665 const char *server,
666 const char *share,
667 char **pp_workgroup,
668 char **pp_username,
669 char **pp_password)
671 SMBCSRV *srv=NULL;
672 bool in_cache = false;
674 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
675 server, share, pp_workgroup,
676 pp_username, pp_password, &in_cache);
678 if (!srv) {
679 return NULL;
681 if (in_cache) {
682 return srv;
685 /* Now add it to the cache (internal or external) */
686 /* Let the cache function set errno if it wants to */
687 errno = 0;
688 if (smbc_getFunctionAddCachedServer(context)(context, srv,
689 server, share,
690 *pp_workgroup,
691 *pp_username)) {
692 int saved_errno = errno;
693 DEBUG(3, (" Failed to add server to cache\n"));
694 errno = saved_errno;
695 if (errno == 0) {
696 errno = ENOMEM;
698 SAFE_FREE(srv);
699 return NULL;
702 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
703 server, share, srv));
705 DLIST_ADD(context->internal->servers, srv);
706 return srv;
710 * Connect to a server for getting/setting attributes, possibly on an existing
711 * connection. This works similarly to SMBC_server().
713 SMBCSRV *
714 SMBC_attr_server(TALLOC_CTX *ctx,
715 SMBCCTX *context,
716 const char *server,
717 const char *share,
718 char **pp_workgroup,
719 char **pp_username,
720 char **pp_password)
722 int flags;
723 struct sockaddr_storage ss;
724 struct cli_state *ipc_cli = NULL;
725 struct rpc_pipe_client *pipe_hnd = NULL;
726 NTSTATUS nt_status;
727 SMBCSRV *srv=NULL;
728 SMBCSRV *ipc_srv=NULL;
731 * Use srv->cli->desthost and srv->cli->share instead of
732 * server and share below to connect to the actual share,
733 * i.e., a normal share or a referred share from
734 * 'msdfs proxy' share.
736 srv = SMBC_server(ctx, context, true, server, share,
737 pp_workgroup, pp_username, pp_password);
738 if (!srv) {
739 return NULL;
741 server = srv->cli->desthost;
742 share = srv->cli->share;
745 * See if we've already created this special connection. Reference
746 * our "special" share name '*IPC$', which is an impossible real share
747 * name due to the leading asterisk.
749 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
750 pp_workgroup, pp_username, pp_password);
751 if (!ipc_srv) {
753 /* We didn't find a cached connection. Get the password */
754 if (!*pp_password || (*pp_password)[0] == '\0') {
755 /* ... then retrieve it now. */
756 SMBC_call_auth_fn(ctx, context, server, share,
757 pp_workgroup,
758 pp_username,
759 pp_password);
760 if (!*pp_workgroup || !*pp_username || !*pp_password) {
761 errno = ENOMEM;
762 return NULL;
766 flags = 0;
767 if (smbc_getOptionUseKerberos(context)) {
768 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
770 if (smbc_getOptionUseCCache(context)) {
771 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
774 zero_sockaddr(&ss);
775 nt_status = cli_full_connection(&ipc_cli,
776 global_myname(), server,
777 &ss, 0, "IPC$", "?????",
778 *pp_username,
779 *pp_workgroup,
780 *pp_password,
781 flags,
782 Undefined);
783 if (! NT_STATUS_IS_OK(nt_status)) {
784 DEBUG(1,("cli_full_connection failed! (%s)\n",
785 nt_errstr(nt_status)));
786 errno = ENOTSUP;
787 return NULL;
790 if (context->internal->smb_encryption_level) {
791 /* Attempt UNIX smb encryption. */
792 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
793 *pp_username,
794 *pp_password,
795 *pp_workgroup))) {
798 * context->smb_encryption_level ==
799 * 1 means don't fail if encryption can't be
800 * negotiated, == 2 means fail if encryption
801 * can't be negotiated.
804 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
806 if (context->internal->smb_encryption_level == 2) {
807 cli_shutdown(ipc_cli);
808 errno = EPERM;
809 return NULL;
812 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
815 ipc_srv = SMB_MALLOC_P(SMBCSRV);
816 if (!ipc_srv) {
817 errno = ENOMEM;
818 cli_shutdown(ipc_cli);
819 return NULL;
822 ZERO_STRUCTP(ipc_srv);
823 ipc_srv->cli = ipc_cli;
825 nt_status = cli_rpc_pipe_open_noauth(
826 ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
827 if (!NT_STATUS_IS_OK(nt_status)) {
828 DEBUG(1, ("cli_nt_session_open fail!\n"));
829 errno = ENOTSUP;
830 cli_shutdown(ipc_srv->cli);
831 free(ipc_srv);
832 return NULL;
836 * Some systems don't support
837 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
838 * so we might as well do it too.
841 nt_status = rpccli_lsa_open_policy(
842 pipe_hnd,
843 talloc_tos(),
844 True,
845 GENERIC_EXECUTE_ACCESS,
846 &ipc_srv->pol);
848 if (!NT_STATUS_IS_OK(nt_status)) {
849 errno = SMBC_errno(context, ipc_srv->cli);
850 cli_shutdown(ipc_srv->cli);
851 return NULL;
854 /* now add it to the cache (internal or external) */
856 errno = 0; /* let cache function set errno if it likes */
857 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
858 server,
859 "*IPC$",
860 *pp_workgroup,
861 *pp_username)) {
862 DEBUG(3, (" Failed to add server to cache\n"));
863 if (errno == 0) {
864 errno = ENOMEM;
866 cli_shutdown(ipc_srv->cli);
867 free(ipc_srv);
868 return NULL;
871 DLIST_ADD(context->internal->servers, ipc_srv);
874 return ipc_srv;