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