s3:libsmb: Use cli_cm_force_encryption() instead of cli_force_encryption()
[samba.git] / source3 / libsmb / libsmb_server.c
1 /*
2    Unix SMB/Netbios implementation.
3    SMB client library implementation
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Richard Sharpe 2000, 2002
6    Copyright (C) John Terpstra 2000
7    Copyright (C) Tom Jansen (Ninja ISD) 2002
8    Copyright (C) Derrell Lipman 2003-2008
9    Copyright (C) Jeremy Allison 2007, 2008
10    Copyright (C) SATOH Fumiyasu <fumiyas@osstech.co.jp> 2009.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "libsmb/libsmb.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "../librpc/gen_ndr/ndr_lsa.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "rpc_client/cli_lsarpc.h"
33 #include "libcli/security/security.h"
34 #include "libsmb/nmblib.h"
35 #include "../libcli/smb/smbXcli_base.h"
36
37 /*
38  * Check a server for being alive and well.
39  * returns 0 if the server is in shape. Returns 1 on error
40  *
41  * Also useable outside libsmbclient to enable external cache
42  * to do some checks too.
43  */
44 int
45 SMBC_check_server(SMBCCTX * context,
46                   SMBCSRV * server)
47 {
48         time_t now;
49
50         if (!cli_state_is_connected(server->cli)) {
51                 return 1;
52         }
53
54         now = time_mono(NULL);
55
56         if (server->last_echo_time == (time_t)0 ||
57                         now > server->last_echo_time +
58                                 (server->cli->timeout/1000)) {
59                 unsigned char data[16] = {0};
60                 NTSTATUS status = cli_echo(server->cli,
61                                         1,
62                                         data_blob_const(data, sizeof(data)));
63                 if (!NT_STATUS_IS_OK(status)) {
64                         return 1;
65                 }
66                 server->last_echo_time = now;
67         }
68         return 0;
69 }
70
71 /*
72  * Remove a server from the cached server list it's unused.
73  * On success, 0 is returned. 1 is returned if the server could not be removed.
74  *
75  * Also useable outside libsmbclient
76  */
77 int
78 SMBC_remove_unused_server(SMBCCTX * context,
79                           SMBCSRV * srv)
80 {
81         SMBCFILE * file;
82
83         /* are we being fooled ? */
84         if (!context || !context->internal->initialized || !srv) {
85                 return 1;
86         }
87
88         /* Check all open files/directories for a relation with this server */
89         for (file = context->internal->files; file; file = file->next) {
90                 if (file->srv == srv) {
91                         /* Still used */
92                         DEBUG(3, ("smbc_remove_usused_server: "
93                                   "%p still used by %p.\n",
94                                   srv, file));
95                         return 1;
96                 }
97         }
98
99         DLIST_REMOVE(context->internal->servers, srv);
100
101         cli_shutdown(srv->cli);
102         srv->cli = NULL;
103
104         DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
105
106         smbc_getFunctionRemoveCachedServer(context)(context, srv);
107
108         SAFE_FREE(srv);
109         return 0;
110 }
111
112 /****************************************************************
113  * Call the auth_fn with fixed size (fstring) buffers.
114  ***************************************************************/
115 static void
116 SMBC_call_auth_fn(TALLOC_CTX *ctx,
117                   SMBCCTX *context,
118                   const char *server,
119                   const char *share,
120                   char **pp_workgroup,
121                   char **pp_username,
122                   char **pp_password)
123 {
124         fstring workgroup = { 0 };
125         fstring username = { 0 };
126         fstring password = { 0 };
127         smbc_get_auth_data_with_context_fn auth_with_context_fn;
128
129         if (*pp_workgroup != NULL) {
130                 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
131         }
132         if (*pp_username != NULL) {
133                 strlcpy(username, *pp_username, sizeof(username));
134         }
135         if (*pp_password != NULL) {
136                 strlcpy(password, *pp_password, sizeof(password));
137         }
138
139         /* See if there's an authentication with context function provided */
140         auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
141         if (auth_with_context_fn)
142         {
143             (* auth_with_context_fn)(context,
144                                      server, share,
145                                      workgroup, sizeof(workgroup),
146                                      username, sizeof(username),
147                                      password, sizeof(password));
148         }
149         else
150         {
151             smbc_getFunctionAuthData(context)(server, share,
152                                               workgroup, sizeof(workgroup),
153                                               username, sizeof(username),
154                                               password, sizeof(password));
155         }
156
157         TALLOC_FREE(*pp_workgroup);
158         TALLOC_FREE(*pp_username);
159         TALLOC_FREE(*pp_password);
160
161         *pp_workgroup = talloc_strdup(ctx, workgroup);
162         *pp_username = talloc_strdup(ctx, username);
163         *pp_password = talloc_strdup(ctx, password);
164 }
165
166
167 void
168 SMBC_get_auth_data(const char *server, const char *share,
169                    char *workgroup_buf, int workgroup_buf_len,
170                    char *username_buf, int username_buf_len,
171                    char *password_buf, int password_buf_len)
172 {
173         /* Default function just uses provided data.  Nothing to do. */
174 }
175
176
177
178 SMBCSRV *
179 SMBC_find_server(TALLOC_CTX *ctx,
180                  SMBCCTX *context,
181                  const char *server,
182                  const char *share,
183                  char **pp_workgroup,
184                  char **pp_username,
185                  char **pp_password)
186 {
187         SMBCSRV *srv;
188         int auth_called = 0;
189
190         if (!pp_workgroup || !pp_username || !pp_password) {
191                 return NULL;
192         }
193
194 check_server_cache:
195
196         srv = smbc_getFunctionGetCachedServer(context)(context,
197                                                        server, share,
198                                                        *pp_workgroup,
199                                                        *pp_username);
200
201         if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
202                                      !*pp_password || !(*pp_password)[0])) {
203                 SMBC_call_auth_fn(ctx, context, server, share,
204                                   pp_workgroup, pp_username, pp_password);
205
206                 /*
207                  * However, smbc_auth_fn may have picked up info relating to
208                  * an existing connection, so try for an existing connection
209                  * again ...
210                  */
211                 auth_called = 1;
212                 goto check_server_cache;
213
214         }
215
216         if (srv) {
217                 if (smbc_getFunctionCheckServer(context)(context, srv)) {
218                         /*
219                          * This server is no good anymore
220                          * Try to remove it and check for more possible
221                          * servers in the cache
222                          */
223                         if (smbc_getFunctionRemoveUnusedServer(context)(context,
224                                                                         srv)) {
225                                 /*
226                                  * We could not remove the server completely,
227                                  * remove it from the cache so we will not get
228                                  * it again. It will be removed when the last
229                                  * file/dir is closed.
230                                  */
231                                 smbc_getFunctionRemoveCachedServer(context)(context,
232                                                                             srv);
233                         }
234
235                         /*
236                          * Maybe there are more cached connections to this
237                          * server
238                          */
239                         goto check_server_cache;
240                 }
241
242                 return srv;
243         }
244
245         return NULL;
246 }
247
248 /*
249  * Connect to a server, possibly on an existing connection
250  *
251  * Here, what we want to do is: If the server and username
252  * match an existing connection, reuse that, otherwise, establish a
253  * new connection.
254  *
255  * If we have to create a new connection, call the auth_fn to get the
256  * info we need, unless the username and password were passed in.
257  */
258
259 static SMBCSRV *
260 SMBC_server_internal(TALLOC_CTX *ctx,
261             SMBCCTX *context,
262             bool connect_if_not_found,
263             const char *server,
264             uint16_t port,
265             const char *share,
266             char **pp_workgroup,
267             char **pp_username,
268             char **pp_password,
269             bool *in_cache)
270 {
271         SMBCSRV *srv=NULL;
272         char *workgroup = NULL;
273         struct cli_state *c = NULL;
274         const char *server_n = server;
275         int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
276         uint32_t fs_attrs = 0;
277         const char *username_used = NULL;
278         const char *password_used = NULL;
279         NTSTATUS status;
280         char *newserver, *newshare;
281         int flags = 0;
282         struct smbXcli_tcon *tcon = NULL;
283         int signing_state = SMB_SIGNING_DEFAULT;
284
285         ZERO_STRUCT(c);
286         *in_cache = false;
287
288         if (server[0] == 0) {
289                 errno = EPERM;
290                 return NULL;
291         }
292
293         /* Look for a cached connection */
294         srv = SMBC_find_server(ctx, context, server, share,
295                                pp_workgroup, pp_username, pp_password);
296
297         /*
298          * If we found a connection and we're only allowed one share per
299          * server...
300          */
301         if (srv &&
302             share != NULL && *share != '\0' &&
303             smbc_getOptionOneSharePerServer(context)) {
304
305                 /*
306                  * ... then if there's no current connection to the share,
307                  * connect to it.  SMBC_find_server(), or rather the function
308                  * pointed to by context->get_cached_srv_fn which
309                  * was called by SMBC_find_server(), will have issued a tree
310                  * disconnect if the requested share is not the same as the
311                  * one that was already connected.
312                  */
313
314                 /*
315                  * Use srv->cli->desthost and srv->cli->share instead of
316                  * server and share below to connect to the actual share,
317                  * i.e., a normal share or a referred share from
318                  * 'msdfs proxy' share.
319                  */
320                 if (!cli_state_has_tcon(srv->cli)) {
321                         /* Ensure we have accurate auth info */
322                         SMBC_call_auth_fn(ctx, context,
323                                           smbXcli_conn_remote_name(srv->cli->conn),
324                                           srv->cli->share,
325                                           pp_workgroup,
326                                           pp_username,
327                                           pp_password);
328
329                         if (!*pp_workgroup || !*pp_username || !*pp_password) {
330                                 errno = ENOMEM;
331                                 cli_shutdown(srv->cli);
332                                 srv->cli = NULL;
333                                 smbc_getFunctionRemoveCachedServer(context)(context,
334                                                                             srv);
335                                 return NULL;
336                         }
337
338                         /*
339                          * We don't need to renegotiate encryption
340                          * here as the encryption context is not per
341                          * tid.
342                          */
343
344                         status = cli_tree_connect(srv->cli,
345                                                   srv->cli->share,
346                                                   "?????",
347                                                   *pp_password);
348                         if (!NT_STATUS_IS_OK(status)) {
349                                 errno = map_errno_from_nt_status(status);
350                                 cli_shutdown(srv->cli);
351                                 srv->cli = NULL;
352                                 smbc_getFunctionRemoveCachedServer(context)(context,
353                                                                             srv);
354                                 srv = NULL;
355                         }
356
357                         /* Determine if this share supports case sensitivity */
358                         if (is_ipc) {
359                                 DEBUG(4,
360                                       ("IPC$ so ignore case sensitivity\n"));
361                                 status = NT_STATUS_OK;
362                         } else {
363                                 status = cli_get_fs_attr_info(c, &fs_attrs);
364                         }
365
366                         if (!NT_STATUS_IS_OK(status)) {
367                                 DEBUG(4, ("Could not retrieve "
368                                           "case sensitivity flag: %s.\n",
369                                           nt_errstr(status)));
370
371                                 /*
372                                  * We can't determine the case sensitivity of
373                                  * the share. We have no choice but to use the
374                                  * user-specified case sensitivity setting.
375                                  */
376                                 if (smbc_getOptionCaseSensitive(context)) {
377                                         cli_set_case_sensitive(c, True);
378                                 } else {
379                                         cli_set_case_sensitive(c, False);
380                                 }
381                         } else if (!is_ipc) {
382                                 DEBUG(4,
383                                       ("Case sensitive: %s\n",
384                                        (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
385                                         ? "True"
386                                         : "False")));
387                                 cli_set_case_sensitive(
388                                         c,
389                                         (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
390                                          ? True
391                                          : False));
392                         }
393
394                         /*
395                          * Regenerate the dev value since it's based on both
396                          * server and share
397                          */
398                         if (srv) {
399                                 const char *remote_name =
400                                         smbXcli_conn_remote_name(srv->cli->conn);
401
402                                 srv->dev = (dev_t)(str_checksum(remote_name) ^
403                                                    str_checksum(srv->cli->share));
404                         }
405                 }
406         }
407
408         /* If we have a connection... */
409         if (srv) {
410
411                 /* ... then we're done here.  Give 'em what they came for. */
412                 *in_cache = true;
413                 goto done;
414         }
415
416         /* If we're not asked to connect when a connection doesn't exist... */
417         if (! connect_if_not_found) {
418                 /* ... then we're done here. */
419                 return NULL;
420         }
421
422         if (!*pp_workgroup || !*pp_username || !*pp_password) {
423                 errno = ENOMEM;
424                 return NULL;
425         }
426
427         DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
428
429         DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
430
431         status = NT_STATUS_UNSUCCESSFUL;
432
433         if (smbc_getOptionUseKerberos(context)) {
434                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
435         }
436
437         if (smbc_getOptionFallbackAfterKerberos(context)) {
438                 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
439         }
440
441         if (smbc_getOptionUseCCache(context)) {
442                 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
443         }
444
445         if (smbc_getOptionUseNTHash(context)) {
446                 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
447         }
448
449         if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
450                 signing_state = SMB_SIGNING_REQUIRED;
451         }
452
453         if (port == 0) {
454                 if (share == NULL || *share == '\0' || is_ipc) {
455                         /*
456                          * Try 139 first for IPC$
457                          */
458                         status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
459                                         smbc_getNetbiosName(context),
460                                         signing_state, flags, &c);
461                 }
462         }
463
464         if (!NT_STATUS_IS_OK(status)) {
465                 /*
466                  * No IPC$ or 139 did not work
467                  */
468                 status = cli_connect_nb(server_n, NULL, port, 0x20,
469                                         smbc_getNetbiosName(context),
470                                         signing_state, flags, &c);
471         }
472
473         if (!NT_STATUS_IS_OK(status)) {
474                 errno = map_errno_from_nt_status(status);
475                 return NULL;
476         }
477
478         cli_set_timeout(c, smbc_getTimeout(context));
479
480         status = smbXcli_negprot(c->conn, c->timeout,
481                                  lp_client_min_protocol(),
482                                  lp_client_max_protocol());
483         if (!NT_STATUS_IS_OK(status)) {
484                 cli_shutdown(c);
485                 errno = ETIMEDOUT;
486                 return NULL;
487         }
488
489         if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
490                 /* Ensure we ask for some initial credits. */
491                 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
492         }
493
494         username_used = *pp_username;
495         password_used = *pp_password;
496
497         if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
498                                                password_used,
499                                                *pp_workgroup))) {
500
501                 /* Failed.  Try an anonymous login, if allowed by flags. */
502                 username_used = "";
503                 password_used = "";
504
505                 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
506                     !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
507                                                        password_used,
508                                                        *pp_workgroup))) {
509
510                         cli_shutdown(c);
511                         errno = EPERM;
512                         return NULL;
513                 }
514         }
515
516         DEBUG(4,(" session setup ok\n"));
517
518         /* here's the fun part....to support 'msdfs proxy' shares
519            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
520            here before trying to connect to the original share.
521            cli_check_msdfs_proxy() will fail if it is a normal share. */
522
523         if (smbXcli_conn_dfs_supported(c->conn) &&
524                         cli_check_msdfs_proxy(ctx, c, share,
525                                 &newserver, &newshare,
526                                 /* FIXME: cli_check_msdfs_proxy() does
527                                    not support smbc_smb_encrypt_level type */
528                                 context->internal->smb_encryption_level ?
529                                         true : false,
530                                 *pp_username,
531                                 *pp_password,
532                                 *pp_workgroup)) {
533                 cli_shutdown(c);
534                 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
535                                 newserver, port, newshare, pp_workgroup,
536                                 pp_username, pp_password, in_cache);
537                 TALLOC_FREE(newserver);
538                 TALLOC_FREE(newshare);
539                 return srv;
540         }
541
542         /* must be a normal share */
543
544         status = cli_tree_connect(c, share, "?????", *pp_password);
545         if (!NT_STATUS_IS_OK(status)) {
546                 errno = map_errno_from_nt_status(status);
547                 cli_shutdown(c);
548                 return NULL;
549         }
550
551         DEBUG(4,(" tconx ok\n"));
552
553         if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
554                 tcon = c->smb2.tcon;
555         } else {
556                 tcon = c->smb1.tcon;
557         }
558
559         /* Determine if this share supports case sensitivity */
560         if (is_ipc) {
561                 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
562                 status = NT_STATUS_OK;
563         } else {
564                 status = cli_get_fs_attr_info(c, &fs_attrs);
565         }
566
567         if (!NT_STATUS_IS_OK(status)) {
568                 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
569                           nt_errstr(status)));
570
571                 /*
572                  * We can't determine the case sensitivity of the share. We
573                  * have no choice but to use the user-specified case
574                  * sensitivity setting.
575                  */
576                 if (smbc_getOptionCaseSensitive(context)) {
577                         cli_set_case_sensitive(c, True);
578                 } else {
579                         cli_set_case_sensitive(c, False);
580                 }
581         } else if (!is_ipc) {
582                 DEBUG(4, ("Case sensitive: %s\n",
583                           (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
584                            ? "True"
585                            : "False")));
586                 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
587         }
588
589         if (context->internal->smb_encryption_level) {
590                 /* Attempt encryption. */
591                 status = cli_cm_force_encryption(c,
592                                                  username_used,
593                                                  password_used,
594                                                  *pp_workgroup,
595                                                  share);
596                 if (!NT_STATUS_IS_OK(status)) {
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         DLIST_ADD(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_pathinfo3 = False;
633         srv->no_nt_session = False;
634
635 done:
636         if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
637                 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
638         } else {
639                 workgroup = *pp_workgroup;
640         }
641         if(!workgroup) {
642                 if (c != NULL) {
643                         cli_shutdown(c);
644                 }
645                 SAFE_FREE(srv);
646                 return NULL;
647         }
648
649         /* set the credentials to make DFS work */
650         smbc_set_credentials_with_fallback(context,
651                                            workgroup,
652                                            *pp_username,
653                                            *pp_password);
654
655         return srv;
656 }
657
658 SMBCSRV *
659 SMBC_server(TALLOC_CTX *ctx,
660                 SMBCCTX *context,
661                 bool connect_if_not_found,
662                 const char *server,
663                 uint16_t port,
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, port, 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                  uint16_t port,
716                  const char *share,
717                  char **pp_workgroup,
718                  char **pp_username,
719                  char **pp_password)
720 {
721         int flags;
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, port, share,
735                         pp_workgroup, pp_username, pp_password);
736         if (!srv) {
737                 return NULL;
738         }
739         server = smbXcli_conn_remote_name(srv->cli->conn);
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                 int signing_state = SMB_SIGNING_DEFAULT;
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                 if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
773                         signing_state = SMB_SIGNING_REQUIRED;
774                 }
775
776                 nt_status = cli_full_connection(&ipc_cli,
777                                                 lp_netbios_name(), server,
778                                                 NULL, 0, "IPC$", "?????",
779                                                 *pp_username,
780                                                 *pp_workgroup,
781                                                 *pp_password,
782                                                 flags,
783                                                 signing_state);
784                 if (! NT_STATUS_IS_OK(nt_status)) {
785                         DEBUG(1,("cli_full_connection failed! (%s)\n",
786                                  nt_errstr(nt_status)));
787                         errno = ENOTSUP;
788                         return NULL;
789                 }
790
791                 if (context->internal->smb_encryption_level) {
792                         /* Attempt encryption. */
793                         nt_status = cli_cm_force_encryption(ipc_cli,
794                                                             *pp_username,
795                                                             *pp_password,
796                                                             *pp_workgroup,
797                                                             "IPC$");
798                         if (!NT_STATUS_IS_OK(nt_status)) {
799
800                                 /*
801                                  * context->smb_encryption_level ==
802                                  * 1 means don't fail if encryption can't be
803                                  * negotiated, == 2 means fail if encryption
804                                  * can't be negotiated.
805                                  */
806
807                                 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
808
809                                 if (context->internal->smb_encryption_level == 2) {
810                                         cli_shutdown(ipc_cli);
811                                         errno = EPERM;
812                                         return NULL;
813                                 }
814                         }
815                         DEBUG(4,(" SMB encrypt ok on IPC$\n"));
816                 }
817
818                 ipc_srv = SMB_MALLOC_P(SMBCSRV);
819                 if (!ipc_srv) {
820                         errno = ENOMEM;
821                         cli_shutdown(ipc_cli);
822                         return NULL;
823                 }
824
825                 ZERO_STRUCTP(ipc_srv);
826                 DLIST_ADD(ipc_srv->cli, ipc_cli);
827
828                 nt_status = cli_rpc_pipe_open_noauth(
829                         ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
830                 if (!NT_STATUS_IS_OK(nt_status)) {
831                         DEBUG(1, ("cli_nt_session_open fail!\n"));
832                         errno = ENOTSUP;
833                         cli_shutdown(ipc_srv->cli);
834                         free(ipc_srv);
835                         return NULL;
836                 }
837
838                 /*
839                  * Some systems don't support
840                  * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
841                  * so we might as well do it too.
842                  */
843
844                 nt_status = rpccli_lsa_open_policy(
845                         pipe_hnd,
846                         talloc_tos(),
847                         True,
848                         GENERIC_EXECUTE_ACCESS,
849                         &ipc_srv->pol);
850
851                 if (!NT_STATUS_IS_OK(nt_status)) {
852                         errno = SMBC_errno(context, ipc_srv->cli);
853                         cli_shutdown(ipc_srv->cli);
854                         free(ipc_srv);
855                         return NULL;
856                 }
857
858                 /* now add it to the cache (internal or external) */
859
860                 errno = 0;      /* let cache function set errno if it likes */
861                 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
862                                                              server,
863                                                              "*IPC$",
864                                                              *pp_workgroup,
865                                                              *pp_username)) {
866                         DEBUG(3, (" Failed to add server to cache\n"));
867                         if (errno == 0) {
868                                 errno = ENOMEM;
869                         }
870                         cli_shutdown(ipc_srv->cli);
871                         free(ipc_srv);
872                         return NULL;
873                 }
874
875                 DLIST_ADD(context->internal->servers, ipc_srv);
876         }
877
878         return ipc_srv;
879 }