s3:libsmb: avoid cli_set_username() in SMBC_server_internal()
[samba.git] / source3 / libsmb / libsmb_server.c
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.
11
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.
16
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.
21
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/>.
24 */
25
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"
36
37 /*
38  * Check a server for being alive and well.
39  * returns 0 if the server is in shape. Returns 1 on error
40  *
41  * Also useable outside libsmbclient to enable external cache
42  * to do some checks too.
43  */
44 int
45 SMBC_check_server(SMBCCTX * context,
46                   SMBCSRV * server)
47 {
48         if (!cli_state_is_connected(server->cli)) {
49                 return 1;
50         }
51
52         return 0;
53 }
54
55 /*
56  * Remove a server from the cached server list it's unused.
57  * On success, 0 is returned. 1 is returned if the server could not be removed.
58  *
59  * Also useable outside libsmbclient
60  */
61 int
62 SMBC_remove_unused_server(SMBCCTX * context,
63                           SMBCSRV * srv)
64 {
65         SMBCFILE * file;
66
67         /* are we being fooled ? */
68         if (!context || !context->internal->initialized || !srv) {
69                 return 1;
70         }
71
72         /* Check all open files/directories for a relation with this server */
73         for (file = context->internal->files; file; file = file->next) {
74                 if (file->srv == srv) {
75                         /* Still used */
76                         DEBUG(3, ("smbc_remove_usused_server: "
77                                   "%p still used by %p.\n",
78                                   srv, file));
79                         return 1;
80                 }
81         }
82
83         DLIST_REMOVE(context->internal->servers, srv);
84
85         cli_shutdown(srv->cli);
86         srv->cli = NULL;
87
88         DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
89
90         smbc_getFunctionRemoveCachedServer(context)(context, srv);
91
92         SAFE_FREE(srv);
93         return 0;
94 }
95
96 /****************************************************************
97  * Call the auth_fn with fixed size (fstring) buffers.
98  ***************************************************************/
99 static void
100 SMBC_call_auth_fn(TALLOC_CTX *ctx,
101                   SMBCCTX *context,
102                   const char *server,
103                   const char *share,
104                   char **pp_workgroup,
105                   char **pp_username,
106                   char **pp_password)
107 {
108         fstring workgroup;
109         fstring username;
110         fstring password;
111         smbc_get_auth_data_with_context_fn auth_with_context_fn;
112
113         strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
114         strlcpy(username, *pp_username, sizeof(username));
115         strlcpy(password, *pp_password, sizeof(password));
116
117         /* See if there's an authentication with context function provided */
118         auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
119         if (auth_with_context_fn)
120         {
121             (* auth_with_context_fn)(context,
122                                      server, share,
123                                      workgroup, sizeof(workgroup),
124                                      username, sizeof(username),
125                                      password, sizeof(password));
126         }
127         else
128         {
129             smbc_getFunctionAuthData(context)(server, share,
130                                               workgroup, sizeof(workgroup),
131                                               username, sizeof(username),
132                                               password, sizeof(password));
133         }
134
135         TALLOC_FREE(*pp_workgroup);
136         TALLOC_FREE(*pp_username);
137         TALLOC_FREE(*pp_password);
138
139         *pp_workgroup = talloc_strdup(ctx, workgroup);
140         *pp_username = talloc_strdup(ctx, username);
141         *pp_password = talloc_strdup(ctx, password);
142 }
143
144
145 void
146 SMBC_get_auth_data(const char *server, const char *share,
147                    char *workgroup_buf, int workgroup_buf_len,
148                    char *username_buf, int username_buf_len,
149                    char *password_buf, int password_buf_len)
150 {
151         /* Default function just uses provided data.  Nothing to do. */
152 }
153
154
155
156 SMBCSRV *
157 SMBC_find_server(TALLOC_CTX *ctx,
158                  SMBCCTX *context,
159                  const char *server,
160                  const char *share,
161                  char **pp_workgroup,
162                  char **pp_username,
163                  char **pp_password)
164 {
165         SMBCSRV *srv;
166         int auth_called = 0;
167
168         if (!pp_workgroup || !pp_username || !pp_password) {
169                 return NULL;
170         }
171
172 check_server_cache:
173
174         srv = smbc_getFunctionGetCachedServer(context)(context,
175                                                        server, share,
176                                                        *pp_workgroup,
177                                                        *pp_username);
178
179         if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
180                                      !*pp_password || !(*pp_password)[0])) {
181                 SMBC_call_auth_fn(ctx, context, server, share,
182                                   pp_workgroup, pp_username, pp_password);
183
184                 /*
185                  * However, smbc_auth_fn may have picked up info relating to
186                  * an existing connection, so try for an existing connection
187                  * again ...
188                  */
189                 auth_called = 1;
190                 goto check_server_cache;
191
192         }
193
194         if (srv) {
195                 if (smbc_getFunctionCheckServer(context)(context, srv)) {
196                         /*
197                          * This server is no good anymore
198                          * Try to remove it and check for more possible
199                          * servers in the cache
200                          */
201                         if (smbc_getFunctionRemoveUnusedServer(context)(context,
202                                                                         srv)) {
203                                 /*
204                                  * We could not remove the server completely,
205                                  * remove it from the cache so we will not get
206                                  * it again. It will be removed when the last
207                                  * file/dir is closed.
208                                  */
209                                 smbc_getFunctionRemoveCachedServer(context)(context,
210                                                                             srv);
211                         }
212
213                         /*
214                          * Maybe there are more cached connections to this
215                          * server
216                          */
217                         goto check_server_cache;
218                 }
219
220                 return srv;
221         }
222
223         return NULL;
224 }
225
226 /*
227  * Connect to a server, possibly on an existing connection
228  *
229  * Here, what we want to do is: If the server and username
230  * match an existing connection, reuse that, otherwise, establish a
231  * new connection.
232  *
233  * If we have to create a new connection, call the auth_fn to get the
234  * info we need, unless the username and password were passed in.
235  */
236
237 static SMBCSRV *
238 SMBC_server_internal(TALLOC_CTX *ctx,
239             SMBCCTX *context,
240             bool connect_if_not_found,
241             const char *server,
242             uint16_t port,
243             const char *share,
244             char **pp_workgroup,
245             char **pp_username,
246             char **pp_password,
247             bool *in_cache)
248 {
249         SMBCSRV *srv=NULL;
250         char *workgroup = NULL;
251         struct cli_state *c = NULL;
252         const char *server_n = server;
253         int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
254         uint32 fs_attrs = 0;
255         const char *username_used;
256         NTSTATUS status;
257         char *newserver, *newshare;
258         int flags = 0;
259         struct smbXcli_tcon *tcon = NULL;
260
261         ZERO_STRUCT(c);
262         *in_cache = false;
263
264         if (server[0] == 0) {
265                 errno = EPERM;
266                 return NULL;
267         }
268
269         /* Look for a cached connection */
270         srv = SMBC_find_server(ctx, context, server, share,
271                                pp_workgroup, pp_username, pp_password);
272
273         /*
274          * If we found a connection and we're only allowed one share per
275          * server...
276          */
277         if (srv &&
278             share != NULL && *share != '\0' &&
279             smbc_getOptionOneSharePerServer(context)) {
280
281                 /*
282                  * ... then if there's no current connection to the share,
283                  * connect to it.  SMBC_find_server(), or rather the function
284                  * pointed to by context->get_cached_srv_fn which
285                  * was called by SMBC_find_server(), will have issued a tree
286                  * disconnect if the requested share is not the same as the
287                  * one that was already connected.
288                  */
289
290                 /*
291                  * Use srv->cli->desthost and srv->cli->share instead of
292                  * server and share below to connect to the actual share,
293                  * i.e., a normal share or a referred share from
294                  * 'msdfs proxy' share.
295                  */
296                 if (!cli_state_has_tcon(srv->cli)) {
297                         /* Ensure we have accurate auth info */
298                         SMBC_call_auth_fn(ctx, context,
299                                           smbXcli_conn_remote_name(srv->cli->conn),
300                                           srv->cli->share,
301                                           pp_workgroup,
302                                           pp_username,
303                                           pp_password);
304
305                         if (!*pp_workgroup || !*pp_username || !*pp_password) {
306                                 errno = ENOMEM;
307                                 cli_shutdown(srv->cli);
308                                 srv->cli = NULL;
309                                 smbc_getFunctionRemoveCachedServer(context)(context,
310                                                                             srv);
311                                 return NULL;
312                         }
313
314                         /*
315                          * We don't need to renegotiate encryption
316                          * here as the encryption context is not per
317                          * tid.
318                          */
319
320                         status = cli_tree_connect(srv->cli,
321                                                   srv->cli->share,
322                                                   "?????",
323                                                   *pp_password,
324                                                   strlen(*pp_password)+1);
325                         if (!NT_STATUS_IS_OK(status)) {
326                                 errno = map_errno_from_nt_status(status);
327                                 cli_shutdown(srv->cli);
328                                 srv->cli = NULL;
329                                 smbc_getFunctionRemoveCachedServer(context)(context,
330                                                                             srv);
331                                 srv = NULL;
332                         }
333
334                         /* Determine if this share supports case sensitivity */
335                         if (is_ipc) {
336                                 DEBUG(4,
337                                       ("IPC$ so ignore case sensitivity\n"));
338                                 status = NT_STATUS_OK;
339                         } else {
340                                 status = cli_get_fs_attr_info(c, &fs_attrs);
341                         }
342
343                         if (!NT_STATUS_IS_OK(status)) {
344                                 DEBUG(4, ("Could not retrieve "
345                                           "case sensitivity flag: %s.\n",
346                                           nt_errstr(status)));
347
348                                 /*
349                                  * We can't determine the case sensitivity of
350                                  * the share. We have no choice but to use the
351                                  * user-specified case sensitivity setting.
352                                  */
353                                 if (smbc_getOptionCaseSensitive(context)) {
354                                         cli_set_case_sensitive(c, True);
355                                 } else {
356                                         cli_set_case_sensitive(c, False);
357                                 }
358                         } else if (!is_ipc) {
359                                 DEBUG(4,
360                                       ("Case sensitive: %s\n",
361                                        (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
362                                         ? "True"
363                                         : "False")));
364                                 cli_set_case_sensitive(
365                                         c,
366                                         (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
367                                          ? True
368                                          : False));
369                         }
370
371                         /*
372                          * Regenerate the dev value since it's based on both
373                          * server and share
374                          */
375                         if (srv) {
376                                 const char *remote_name =
377                                         smbXcli_conn_remote_name(srv->cli->conn);
378
379                                 srv->dev = (dev_t)(str_checksum(remote_name) ^
380                                                    str_checksum(srv->cli->share));
381                         }
382                 }
383         }
384
385         /* If we have a connection... */
386         if (srv) {
387
388                 /* ... then we're done here.  Give 'em what they came for. */
389                 *in_cache = true;
390                 goto done;
391         }
392
393         /* If we're not asked to connect when a connection doesn't exist... */
394         if (! connect_if_not_found) {
395                 /* ... then we're done here. */
396                 return NULL;
397         }
398
399         if (!*pp_workgroup || !*pp_username || !*pp_password) {
400                 errno = ENOMEM;
401                 return NULL;
402         }
403
404         DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
405
406         DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
407
408         status = NT_STATUS_UNSUCCESSFUL;
409
410         if (smbc_getOptionUseKerberos(context)) {
411                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
412         }
413
414         if (smbc_getOptionFallbackAfterKerberos(context)) {
415                 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
416         }
417
418         if (smbc_getOptionUseCCache(context)) {
419                 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
420         }
421
422         if (smbc_getOptionUseNTHash(context)) {
423                 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
424         }
425
426         if (port == 0) {
427                 if (share == NULL || *share == '\0' || is_ipc) {
428                         /*
429                          * Try 139 first for IPC$
430                          */
431                         status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
432                                         smbc_getNetbiosName(context),
433                                         SMB_SIGNING_DEFAULT, flags, &c);
434                 }
435         }
436
437         if (!NT_STATUS_IS_OK(status)) {
438                 /*
439                  * No IPC$ or 139 did not work
440                  */
441                 status = cli_connect_nb(server_n, NULL, port, 0x20,
442                                         smbc_getNetbiosName(context),
443                                         SMB_SIGNING_DEFAULT, flags, &c);
444         }
445
446         if (!NT_STATUS_IS_OK(status)) {
447                 errno = map_errno_from_nt_status(status);
448                 return NULL;
449         }
450
451         cli_set_timeout(c, smbc_getTimeout(context));
452
453         status = smbXcli_negprot(c->conn, c->timeout,
454                                  lp_client_min_protocol(),
455                                  lp_client_max_protocol());
456         if (!NT_STATUS_IS_OK(status)) {
457                 cli_shutdown(c);
458                 errno = ETIMEDOUT;
459                 return NULL;
460         }
461
462         if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
463                 /* Ensure we ask for some initial credits. */
464                 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
465         }
466
467         username_used = *pp_username;
468
469         if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
470                                                *pp_password,
471                                                strlen(*pp_password),
472                                                *pp_password,
473                                                strlen(*pp_password),
474                                                *pp_workgroup))) {
475
476                 /* Failed.  Try an anonymous login, if allowed by flags. */
477                 username_used = "";
478
479                 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
480                     !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
481                                                        *pp_password, 1,
482                                                        *pp_password, 0,
483                                                        *pp_workgroup))) {
484
485                         cli_shutdown(c);
486                         errno = EPERM;
487                         return NULL;
488                 }
489         }
490
491         DEBUG(4,(" session setup ok\n"));
492
493         /* here's the fun part....to support 'msdfs proxy' shares
494            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
495            here before trying to connect to the original share.
496            cli_check_msdfs_proxy() will fail if it is a normal share. */
497
498         if (smbXcli_conn_dfs_supported(c->conn) &&
499                         cli_check_msdfs_proxy(ctx, c, share,
500                                 &newserver, &newshare,
501                                 /* FIXME: cli_check_msdfs_proxy() does
502                                    not support smbc_smb_encrypt_level type */
503                                 context->internal->smb_encryption_level ?
504                                         true : false,
505                                 *pp_username,
506                                 *pp_password,
507                                 *pp_workgroup)) {
508                 cli_shutdown(c);
509                 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
510                                 newserver, port, newshare, pp_workgroup,
511                                 pp_username, pp_password, in_cache);
512                 TALLOC_FREE(newserver);
513                 TALLOC_FREE(newshare);
514                 return srv;
515         }
516
517         /* must be a normal share */
518
519         status = cli_tree_connect(c, share, "?????", *pp_password,
520                                   strlen(*pp_password)+1);
521         if (!NT_STATUS_IS_OK(status)) {
522                 errno = map_errno_from_nt_status(status);
523                 cli_shutdown(c);
524                 return NULL;
525         }
526
527         DEBUG(4,(" tconx ok\n"));
528
529         if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
530                 tcon = c->smb2.tcon;
531         } else {
532                 tcon = c->smb1.tcon;
533         }
534
535         /* Determine if this share supports case sensitivity */
536         if (is_ipc) {
537                 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
538                 status = NT_STATUS_OK;
539         } else {
540                 status = cli_get_fs_attr_info(c, &fs_attrs);
541         }
542
543         if (!NT_STATUS_IS_OK(status)) {
544                 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
545                           nt_errstr(status)));
546
547                 /*
548                  * We can't determine the case sensitivity of the share. We
549                  * have no choice but to use the user-specified case
550                  * sensitivity setting.
551                  */
552                 if (smbc_getOptionCaseSensitive(context)) {
553                         cli_set_case_sensitive(c, True);
554                 } else {
555                         cli_set_case_sensitive(c, False);
556                 }
557         } else if (!is_ipc) {
558                 DEBUG(4, ("Case sensitive: %s\n",
559                           (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
560                            ? "True"
561                            : "False")));
562                 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
563         }
564
565         if (context->internal->smb_encryption_level) {
566                 /* Attempt UNIX smb encryption. */
567                 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
568                                                           username_used,
569                                                           *pp_password,
570                                                           *pp_workgroup))) {
571
572                         /*
573                          * context->smb_encryption_level == 1
574                          * means don't fail if encryption can't be negotiated,
575                          * == 2 means fail if encryption can't be negotiated.
576                          */
577
578                         DEBUG(4,(" SMB encrypt failed\n"));
579
580                         if (context->internal->smb_encryption_level == 2) {
581                                 cli_shutdown(c);
582                                 errno = EPERM;
583                                 return NULL;
584                         }
585                 }
586                 DEBUG(4,(" SMB encrypt ok\n"));
587         }
588
589         /*
590          * Ok, we have got a nice connection
591          * Let's allocate a server structure.
592          */
593
594         srv = SMB_MALLOC_P(SMBCSRV);
595         if (!srv) {
596                 cli_shutdown(c);
597                 errno = ENOMEM;
598                 return NULL;
599         }
600
601         ZERO_STRUCTP(srv);
602         srv->cli = c;
603         srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
604         srv->no_pathinfo = False;
605         srv->no_pathinfo2 = False;
606         srv->no_pathinfo3 = False;
607         srv->no_nt_session = False;
608
609 done:
610         if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
611                 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
612         } else {
613                 workgroup = *pp_workgroup;
614         }
615         if(!workgroup) {
616                 if (c != NULL) {
617                         cli_shutdown(c);
618                 }
619                 SAFE_FREE(srv);
620                 return NULL;
621         }
622
623         /* set the credentials to make DFS work */
624         smbc_set_credentials_with_fallback(context,
625                                            workgroup,
626                                            *pp_username,
627                                            *pp_password);
628
629         return srv;
630 }
631
632 SMBCSRV *
633 SMBC_server(TALLOC_CTX *ctx,
634                 SMBCCTX *context,
635                 bool connect_if_not_found,
636                 const char *server,
637                 uint16_t port,
638                 const char *share,
639                 char **pp_workgroup,
640                 char **pp_username,
641                 char **pp_password)
642 {
643         SMBCSRV *srv=NULL;
644         bool in_cache = false;
645
646         srv = SMBC_server_internal(ctx, context, connect_if_not_found,
647                         server, port, share, pp_workgroup,
648                         pp_username, pp_password, &in_cache);
649
650         if (!srv) {
651                 return NULL;
652         }
653         if (in_cache) {
654                 return srv;
655         }
656
657         /* Now add it to the cache (internal or external)  */
658         /* Let the cache function set errno if it wants to */
659         errno = 0;
660         if (smbc_getFunctionAddCachedServer(context)(context, srv,
661                                                 server, share,
662                                                 *pp_workgroup,
663                                                 *pp_username)) {
664                 int saved_errno = errno;
665                 DEBUG(3, (" Failed to add server to cache\n"));
666                 errno = saved_errno;
667                 if (errno == 0) {
668                         errno = ENOMEM;
669                 }
670                 SAFE_FREE(srv);
671                 return NULL;
672         }
673
674         DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
675                 server, share, srv));
676
677         DLIST_ADD(context->internal->servers, srv);
678         return srv;
679 }
680
681 /*
682  * Connect to a server for getting/setting attributes, possibly on an existing
683  * connection.  This works similarly to SMBC_server().
684  */
685 SMBCSRV *
686 SMBC_attr_server(TALLOC_CTX *ctx,
687                  SMBCCTX *context,
688                  const char *server,
689                  uint16_t port,
690                  const char *share,
691                  char **pp_workgroup,
692                  char **pp_username,
693                  char **pp_password)
694 {
695         int flags;
696         struct cli_state *ipc_cli = NULL;
697         struct rpc_pipe_client *pipe_hnd = NULL;
698         NTSTATUS nt_status;
699         SMBCSRV *srv=NULL;
700         SMBCSRV *ipc_srv=NULL;
701
702         /*
703          * Use srv->cli->desthost and srv->cli->share instead of
704          * server and share below to connect to the actual share,
705          * i.e., a normal share or a referred share from
706          * 'msdfs proxy' share.
707          */
708         srv = SMBC_server(ctx, context, true, server, port, share,
709                         pp_workgroup, pp_username, pp_password);
710         if (!srv) {
711                 return NULL;
712         }
713         server = smbXcli_conn_remote_name(srv->cli->conn);
714         share = srv->cli->share;
715
716         /*
717          * See if we've already created this special connection.  Reference
718          * our "special" share name '*IPC$', which is an impossible real share
719          * name due to the leading asterisk.
720          */
721         ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
722                                    pp_workgroup, pp_username, pp_password);
723         if (!ipc_srv) {
724
725                 /* We didn't find a cached connection.  Get the password */
726                 if (!*pp_password || (*pp_password)[0] == '\0') {
727                         /* ... then retrieve it now. */
728                         SMBC_call_auth_fn(ctx, context, server, share,
729                                           pp_workgroup,
730                                           pp_username,
731                                           pp_password);
732                         if (!*pp_workgroup || !*pp_username || !*pp_password) {
733                                 errno = ENOMEM;
734                                 return NULL;
735                         }
736                 }
737
738                 flags = 0;
739                 if (smbc_getOptionUseKerberos(context)) {
740                         flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
741                 }
742                 if (smbc_getOptionUseCCache(context)) {
743                         flags |= CLI_FULL_CONNECTION_USE_CCACHE;
744                 }
745
746                 nt_status = cli_full_connection(&ipc_cli,
747                                                 lp_netbios_name(), server,
748                                                 NULL, 0, "IPC$", "?????",
749                                                 *pp_username,
750                                                 *pp_workgroup,
751                                                 *pp_password,
752                                                 flags,
753                                                 SMB_SIGNING_DEFAULT);
754                 if (! NT_STATUS_IS_OK(nt_status)) {
755                         DEBUG(1,("cli_full_connection failed! (%s)\n",
756                                  nt_errstr(nt_status)));
757                         errno = ENOTSUP;
758                         return NULL;
759                 }
760
761                 if (context->internal->smb_encryption_level) {
762                         /* Attempt UNIX smb encryption. */
763                         if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
764                                                                   *pp_username,
765                                                                   *pp_password,
766                                                                   *pp_workgroup))) {
767
768                                 /*
769                                  * context->smb_encryption_level ==
770                                  * 1 means don't fail if encryption can't be
771                                  * negotiated, == 2 means fail if encryption
772                                  * can't be negotiated.
773                                  */
774
775                                 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
776
777                                 if (context->internal->smb_encryption_level == 2) {
778                                         cli_shutdown(ipc_cli);
779                                         errno = EPERM;
780                                         return NULL;
781                                 }
782                         }
783                         DEBUG(4,(" SMB encrypt ok on IPC$\n"));
784                 }
785
786                 ipc_srv = SMB_MALLOC_P(SMBCSRV);
787                 if (!ipc_srv) {
788                         errno = ENOMEM;
789                         cli_shutdown(ipc_cli);
790                         return NULL;
791                 }
792
793                 ZERO_STRUCTP(ipc_srv);
794                 ipc_srv->cli = ipc_cli;
795
796                 nt_status = cli_rpc_pipe_open_noauth(
797                         ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
798                 if (!NT_STATUS_IS_OK(nt_status)) {
799                         DEBUG(1, ("cli_nt_session_open fail!\n"));
800                         errno = ENOTSUP;
801                         cli_shutdown(ipc_srv->cli);
802                         free(ipc_srv);
803                         return NULL;
804                 }
805
806                 /*
807                  * Some systems don't support
808                  * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
809                  * so we might as well do it too.
810                  */
811
812                 nt_status = rpccli_lsa_open_policy(
813                         pipe_hnd,
814                         talloc_tos(),
815                         True,
816                         GENERIC_EXECUTE_ACCESS,
817                         &ipc_srv->pol);
818
819                 if (!NT_STATUS_IS_OK(nt_status)) {
820                         errno = SMBC_errno(context, ipc_srv->cli);
821                         cli_shutdown(ipc_srv->cli);
822                         free(ipc_srv);
823                         return NULL;
824                 }
825
826                 /* now add it to the cache (internal or external) */
827
828                 errno = 0;      /* let cache function set errno if it likes */
829                 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
830                                                              server,
831                                                              "*IPC$",
832                                                              *pp_workgroup,
833                                                              *pp_username)) {
834                         DEBUG(3, (" Failed to add server to cache\n"));
835                         if (errno == 0) {
836                                 errno = ENOMEM;
837                         }
838                         cli_shutdown(ipc_srv->cli);
839                         free(ipc_srv);
840                         return NULL;
841                 }
842
843                 DLIST_ADD(context->internal->servers, ipc_srv);
844         }
845
846         return ipc_srv;
847 }