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