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