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