s3:libsmb: get rid of cli_state_remote_name
[mat/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 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             const char *share,
243             char **pp_workgroup,
244             char **pp_username,
245             char **pp_password,
246             bool *in_cache)
247 {
248         SMBCSRV *srv=NULL;
249         char *workgroup = NULL;
250         struct cli_state *c = NULL;
251         const char *server_n = server;
252         int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
253         uint32 fs_attrs = 0;
254         const char *username_used;
255         NTSTATUS status;
256         char *newserver, *newshare;
257         int flags = 0;
258
259         ZERO_STRUCT(c);
260         *in_cache = false;
261
262         if (server[0] == 0) {
263                 errno = EPERM;
264                 return NULL;
265         }
266
267         /* Look for a cached connection */
268         srv = SMBC_find_server(ctx, context, server, share,
269                                pp_workgroup, pp_username, pp_password);
270
271         /*
272          * If we found a connection and we're only allowed one share per
273          * server...
274          */
275         if (srv &&
276             *share != '\0' &&
277             smbc_getOptionOneSharePerServer(context)) {
278
279                 /*
280                  * ... then if there's no current connection to the share,
281                  * connect to it.  SMBC_find_server(), or rather the function
282                  * pointed to by context->get_cached_srv_fn which
283                  * was called by SMBC_find_server(), will have issued a tree
284                  * disconnect if the requested share is not the same as the
285                  * one that was already connected.
286                  */
287
288                 /*
289                  * Use srv->cli->desthost and srv->cli->share instead of
290                  * server and share below to connect to the actual share,
291                  * i.e., a normal share or a referred share from
292                  * 'msdfs proxy' share.
293                  */
294                 if (!cli_state_has_tcon(srv->cli)) {
295                         /* Ensure we have accurate auth info */
296                         SMBC_call_auth_fn(ctx, context,
297                                           smbXcli_conn_remote_name(srv->cli->conn),
298                                           srv->cli->share,
299                                           pp_workgroup,
300                                           pp_username,
301                                           pp_password);
302
303                         if (!*pp_workgroup || !*pp_username || !*pp_password) {
304                                 errno = ENOMEM;
305                                 cli_shutdown(srv->cli);
306                                 srv->cli = NULL;
307                                 smbc_getFunctionRemoveCachedServer(context)(context,
308                                                                             srv);
309                                 return NULL;
310                         }
311
312                         /*
313                          * We don't need to renegotiate encryption
314                          * here as the encryption context is not per
315                          * tid.
316                          */
317
318                         status = cli_tree_connect(srv->cli,
319                                                   srv->cli->share,
320                                                   "?????",
321                                                   *pp_password,
322                                                   strlen(*pp_password)+1);
323                         if (!NT_STATUS_IS_OK(status)) {
324                                 errno = map_errno_from_nt_status(status);
325                                 cli_shutdown(srv->cli);
326                                 srv->cli = NULL;
327                                 smbc_getFunctionRemoveCachedServer(context)(context,
328                                                                             srv);
329                                 srv = NULL;
330                         }
331
332                         /* Determine if this share supports case sensitivity */
333                         if (is_ipc) {
334                                 DEBUG(4,
335                                       ("IPC$ so ignore case sensitivity\n"));
336                                 status = NT_STATUS_OK;
337                         } else {
338                                 status = cli_get_fs_attr_info(c, &fs_attrs);
339                         }
340
341                         if (!NT_STATUS_IS_OK(status)) {
342                                 DEBUG(4, ("Could not retrieve "
343                                           "case sensitivity flag: %s.\n",
344                                           nt_errstr(status)));
345
346                                 /*
347                                  * We can't determine the case sensitivity of
348                                  * the share. We have no choice but to use the
349                                  * user-specified case sensitivity setting.
350                                  */
351                                 if (smbc_getOptionCaseSensitive(context)) {
352                                         cli_set_case_sensitive(c, True);
353                                 } else {
354                                         cli_set_case_sensitive(c, False);
355                                 }
356                         } else if (!is_ipc) {
357                                 DEBUG(4,
358                                       ("Case sensitive: %s\n",
359                                        (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
360                                         ? "True"
361                                         : "False")));
362                                 cli_set_case_sensitive(
363                                         c,
364                                         (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
365                                          ? True
366                                          : False));
367                         }
368
369                         /*
370                          * Regenerate the dev value since it's based on both
371                          * server and share
372                          */
373                         if (srv) {
374                                 const char *remote_name =
375                                         smbXcli_conn_remote_name(srv->cli->conn);
376
377                                 srv->dev = (dev_t)(str_checksum(remote_name) ^
378                                                    str_checksum(srv->cli->share));
379                         }
380                 }
381         }
382
383         /* If we have a connection... */
384         if (srv) {
385
386                 /* ... then we're done here.  Give 'em what they came for. */
387                 *in_cache = true;
388                 goto done;
389         }
390
391         /* If we're not asked to connect when a connection doesn't exist... */
392         if (! connect_if_not_found) {
393                 /* ... then we're done here. */
394                 return NULL;
395         }
396
397         if (!*pp_workgroup || !*pp_username || !*pp_password) {
398                 errno = ENOMEM;
399                 return NULL;
400         }
401
402         DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
403
404         DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
405
406         status = NT_STATUS_UNSUCCESSFUL;
407
408         if (smbc_getOptionUseKerberos(context)) {
409                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
410         }
411
412         if (smbc_getOptionFallbackAfterKerberos(context)) {
413                 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
414         }
415
416         if (smbc_getOptionUseCCache(context)) {
417                 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
418         }
419
420         if (share == NULL || *share == '\0' || is_ipc) {
421                 /*
422                  * Try 139 first for IPC$
423                  */
424                 status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
425                                         smbc_getNetbiosName(context),
426                                         SMB_SIGNING_DEFAULT, flags, &c);
427         }
428
429         if (!NT_STATUS_IS_OK(status)) {
430                 /*
431                  * No IPC$ or 139 did not work
432                  */
433                 status = cli_connect_nb(server_n, NULL, 0, 0x20,
434                                         smbc_getNetbiosName(context),
435                                         SMB_SIGNING_DEFAULT, flags, &c);
436         }
437
438         if (!NT_STATUS_IS_OK(status)) {
439                 errno = map_errno_from_nt_status(status);
440                 return NULL;
441         }
442
443         cli_set_timeout(c, smbc_getTimeout(context));
444
445         status = cli_negprot(c, PROTOCOL_NT1);
446
447         if (!NT_STATUS_IS_OK(status)) {
448                 cli_shutdown(c);
449                 errno = ETIMEDOUT;
450                 return NULL;
451         }
452
453         username_used = *pp_username;
454
455         if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
456                                                *pp_password,
457                                                strlen(*pp_password),
458                                                *pp_password,
459                                                strlen(*pp_password),
460                                                *pp_workgroup))) {
461
462                 /* Failed.  Try an anonymous login, if allowed by flags. */
463                 username_used = "";
464
465                 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
466                     !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
467                                                        *pp_password, 1,
468                                                        *pp_password, 0,
469                                                        *pp_workgroup))) {
470
471                         cli_shutdown(c);
472                         errno = EPERM;
473                         return NULL;
474                 }
475         }
476
477         status = cli_init_creds(c, username_used,
478                                 *pp_workgroup, *pp_password);
479         if (!NT_STATUS_IS_OK(status)) {
480                 errno = map_errno_from_nt_status(status);
481                 cli_shutdown(c);
482                 return NULL;
483         }
484
485         DEBUG(4,(" session setup ok\n"));
486
487         /* here's the fun part....to support 'msdfs proxy' shares
488            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
489            here before trying to connect to the original share.
490            cli_check_msdfs_proxy() will fail if it is a normal share. */
491
492         if ((cli_state_capabilities(c) & CAP_DFS) &&
493                         cli_check_msdfs_proxy(ctx, c, share,
494                                 &newserver, &newshare,
495                                 /* FIXME: cli_check_msdfs_proxy() does
496                                    not support smbc_smb_encrypt_level type */
497                                 context->internal->smb_encryption_level ?
498                                         true : false,
499                                 *pp_username,
500                                 *pp_password,
501                                 *pp_workgroup)) {
502                 cli_shutdown(c);
503                 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
504                                 newserver, newshare, pp_workgroup,
505                                 pp_username, pp_password, in_cache);
506                 TALLOC_FREE(newserver);
507                 TALLOC_FREE(newshare);
508                 return srv;
509         }
510
511         /* must be a normal share */
512
513         status = cli_tree_connect(c, share, "?????", *pp_password,
514                                   strlen(*pp_password)+1);
515         if (!NT_STATUS_IS_OK(status)) {
516                 errno = map_errno_from_nt_status(status);
517                 cli_shutdown(c);
518                 return NULL;
519         }
520
521         DEBUG(4,(" tconx ok\n"));
522
523         /* Determine if this share supports case sensitivity */
524         if (is_ipc) {
525                 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
526                 status = NT_STATUS_OK;
527         } else {
528                 status = cli_get_fs_attr_info(c, &fs_attrs);
529         }
530
531         if (!NT_STATUS_IS_OK(status)) {
532                 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
533                           nt_errstr(status)));
534
535                 /*
536                  * We can't determine the case sensitivity of the share. We
537                  * have no choice but to use the user-specified case
538                  * sensitivity setting.
539                  */
540                 if (smbc_getOptionCaseSensitive(context)) {
541                         cli_set_case_sensitive(c, True);
542                 } else {
543                         cli_set_case_sensitive(c, False);
544                 }
545         } else if (!is_ipc) {
546                 DEBUG(4, ("Case sensitive: %s\n",
547                           (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
548                            ? "True"
549                            : "False")));
550                 cli_set_case_sensitive(c,
551                                        (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
552                                         ? True
553                                         : False));
554         }
555
556         if (context->internal->smb_encryption_level) {
557                 /* Attempt UNIX smb encryption. */
558                 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
559                                                           username_used,
560                                                           *pp_password,
561                                                           *pp_workgroup))) {
562
563                         /*
564                          * context->smb_encryption_level == 1
565                          * means don't fail if encryption can't be negotiated,
566                          * == 2 means fail if encryption can't be negotiated.
567                          */
568
569                         DEBUG(4,(" SMB encrypt failed\n"));
570
571                         if (context->internal->smb_encryption_level == 2) {
572                                 cli_shutdown(c);
573                                 errno = EPERM;
574                                 return NULL;
575                         }
576                 }
577                 DEBUG(4,(" SMB encrypt ok\n"));
578         }
579
580         /*
581          * Ok, we have got a nice connection
582          * Let's allocate a server structure.
583          */
584
585         srv = SMB_MALLOC_P(SMBCSRV);
586         if (!srv) {
587                 cli_shutdown(c);
588                 errno = ENOMEM;
589                 return NULL;
590         }
591
592         ZERO_STRUCTP(srv);
593         srv->cli = c;
594         srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
595         srv->no_pathinfo = False;
596         srv->no_pathinfo2 = False;
597         srv->no_nt_session = False;
598
599 done:
600         if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
601                 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
602         } else {
603                 workgroup = *pp_workgroup;
604         }
605         if(!workgroup) {
606                 if (c != NULL) {
607                         cli_shutdown(c);
608                 }
609                 SAFE_FREE(srv);
610                 return NULL;
611         }
612
613         /* set the credentials to make DFS work */
614         smbc_set_credentials_with_fallback(context,
615                                            workgroup,
616                                            *pp_username,
617                                            *pp_password);
618
619         return srv;
620 }
621
622 SMBCSRV *
623 SMBC_server(TALLOC_CTX *ctx,
624                 SMBCCTX *context,
625                 bool connect_if_not_found,
626                 const char *server,
627                 const char *share,
628                 char **pp_workgroup,
629                 char **pp_username,
630                 char **pp_password)
631 {
632         SMBCSRV *srv=NULL;
633         bool in_cache = false;
634
635         srv = SMBC_server_internal(ctx, context, connect_if_not_found,
636                         server, share, pp_workgroup,
637                         pp_username, pp_password, &in_cache);
638
639         if (!srv) {
640                 return NULL;
641         }
642         if (in_cache) {
643                 return srv;
644         }
645
646         /* Now add it to the cache (internal or external)  */
647         /* Let the cache function set errno if it wants to */
648         errno = 0;
649         if (smbc_getFunctionAddCachedServer(context)(context, srv,
650                                                 server, share,
651                                                 *pp_workgroup,
652                                                 *pp_username)) {
653                 int saved_errno = errno;
654                 DEBUG(3, (" Failed to add server to cache\n"));
655                 errno = saved_errno;
656                 if (errno == 0) {
657                         errno = ENOMEM;
658                 }
659                 SAFE_FREE(srv);
660                 return NULL;
661         }
662
663         DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
664                 server, share, srv));
665
666         DLIST_ADD(context->internal->servers, srv);
667         return srv;
668 }
669
670 /*
671  * Connect to a server for getting/setting attributes, possibly on an existing
672  * connection.  This works similarly to SMBC_server().
673  */
674 SMBCSRV *
675 SMBC_attr_server(TALLOC_CTX *ctx,
676                  SMBCCTX *context,
677                  const char *server,
678                  const char *share,
679                  char **pp_workgroup,
680                  char **pp_username,
681                  char **pp_password)
682 {
683         int flags;
684         struct cli_state *ipc_cli = NULL;
685         struct rpc_pipe_client *pipe_hnd = NULL;
686         NTSTATUS nt_status;
687         SMBCSRV *srv=NULL;
688         SMBCSRV *ipc_srv=NULL;
689
690         /*
691          * Use srv->cli->desthost and srv->cli->share instead of
692          * server and share below to connect to the actual share,
693          * i.e., a normal share or a referred share from
694          * 'msdfs proxy' share.
695          */
696         srv = SMBC_server(ctx, context, true, server, share,
697                         pp_workgroup, pp_username, pp_password);
698         if (!srv) {
699                 return NULL;
700         }
701         server = smbXcli_conn_remote_name(srv->cli->conn);
702         share = srv->cli->share;
703
704         /*
705          * See if we've already created this special connection.  Reference
706          * our "special" share name '*IPC$', which is an impossible real share
707          * name due to the leading asterisk.
708          */
709         ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
710                                    pp_workgroup, pp_username, pp_password);
711         if (!ipc_srv) {
712
713                 /* We didn't find a cached connection.  Get the password */
714                 if (!*pp_password || (*pp_password)[0] == '\0') {
715                         /* ... then retrieve it now. */
716                         SMBC_call_auth_fn(ctx, context, server, share,
717                                           pp_workgroup,
718                                           pp_username,
719                                           pp_password);
720                         if (!*pp_workgroup || !*pp_username || !*pp_password) {
721                                 errno = ENOMEM;
722                                 return NULL;
723                         }
724                 }
725
726                 flags = 0;
727                 if (smbc_getOptionUseKerberos(context)) {
728                         flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
729                 }
730                 if (smbc_getOptionUseCCache(context)) {
731                         flags |= CLI_FULL_CONNECTION_USE_CCACHE;
732                 }
733
734                 nt_status = cli_full_connection(&ipc_cli,
735                                                 lp_netbios_name(), server,
736                                                 NULL, 0, "IPC$", "?????",
737                                                 *pp_username,
738                                                 *pp_workgroup,
739                                                 *pp_password,
740                                                 flags,
741                                                 SMB_SIGNING_DEFAULT);
742                 if (! NT_STATUS_IS_OK(nt_status)) {
743                         DEBUG(1,("cli_full_connection failed! (%s)\n",
744                                  nt_errstr(nt_status)));
745                         errno = ENOTSUP;
746                         return NULL;
747                 }
748
749                 if (context->internal->smb_encryption_level) {
750                         /* Attempt UNIX smb encryption. */
751                         if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
752                                                                   *pp_username,
753                                                                   *pp_password,
754                                                                   *pp_workgroup))) {
755
756                                 /*
757                                  * context->smb_encryption_level ==
758                                  * 1 means don't fail if encryption can't be
759                                  * negotiated, == 2 means fail if encryption
760                                  * can't be negotiated.
761                                  */
762
763                                 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
764
765                                 if (context->internal->smb_encryption_level == 2) {
766                                         cli_shutdown(ipc_cli);
767                                         errno = EPERM;
768                                         return NULL;
769                                 }
770                         }
771                         DEBUG(4,(" SMB encrypt ok on IPC$\n"));
772                 }
773
774                 ipc_srv = SMB_MALLOC_P(SMBCSRV);
775                 if (!ipc_srv) {
776                         errno = ENOMEM;
777                         cli_shutdown(ipc_cli);
778                         return NULL;
779                 }
780
781                 ZERO_STRUCTP(ipc_srv);
782                 ipc_srv->cli = ipc_cli;
783
784                 nt_status = cli_rpc_pipe_open_noauth(
785                         ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
786                 if (!NT_STATUS_IS_OK(nt_status)) {
787                         DEBUG(1, ("cli_nt_session_open fail!\n"));
788                         errno = ENOTSUP;
789                         cli_shutdown(ipc_srv->cli);
790                         free(ipc_srv);
791                         return NULL;
792                 }
793
794                 /*
795                  * Some systems don't support
796                  * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
797                  * so we might as well do it too.
798                  */
799
800                 nt_status = rpccli_lsa_open_policy(
801                         pipe_hnd,
802                         talloc_tos(),
803                         True,
804                         GENERIC_EXECUTE_ACCESS,
805                         &ipc_srv->pol);
806
807                 if (!NT_STATUS_IS_OK(nt_status)) {
808                         errno = SMBC_errno(context, ipc_srv->cli);
809                         cli_shutdown(ipc_srv->cli);
810                         return NULL;
811                 }
812
813                 /* now add it to the cache (internal or external) */
814
815                 errno = 0;      /* let cache function set errno if it likes */
816                 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
817                                                              server,
818                                                              "*IPC$",
819                                                              *pp_workgroup,
820                                                              *pp_username)) {
821                         DEBUG(3, (" Failed to add server to cache\n"));
822                         if (errno == 0) {
823                                 errno = ENOMEM;
824                         }
825                         cli_shutdown(ipc_srv->cli);
826                         free(ipc_srv);
827                         return NULL;
828                 }
829
830                 DLIST_ADD(context->internal->servers, ipc_srv);
831         }
832
833         return ipc_srv;
834 }