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