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