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