source: trunk/server/source3/libsmb/libsmb_server.c

Last change on this file was 920, checked in by Silvan Scherrer, 9 years ago

Samba Server: apply latest security patches to trunk

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