Bug 15680 - Trust domains are not created
Summary: Trust domains are not created
Status: RESOLVED FIXED
Alias: None
Product: Samba 4.1 and newer
Classification: Unclassified
Component: DCE-RPCs and pipes (show other bugs)
Version: 4.20.2
Hardware: All All
: P5 major (vote)
Target Milestone: ---
Assignee: Jule Anger
QA Contact: Samba QA Contact
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-07-09 14:22 UTC by Dinar Yulmukhametov
Modified: 2025-06-12 12:46 UTC (History)
5 users (show)

See Also:


Attachments
trust create log with -d 10 and commented try-except (14.03 KB, text/plain)
2024-07-12 07:59 UTC, Dinar Yulmukhametov
no flags Details
Network trace (rpcclient lsaquery against Windows Server 2012) (6.57 KB, application/octet-stream)
2024-07-17 12:15 UTC, Andreas Schneider
no flags Details
Draft patches for a fix... (15.96 KB, patch)
2024-07-18 16:18 UTC, Stefan Metzmacher
no flags Details
Log with patch from Stefan Metzmacher (14.85 KB, text/x-log)
2024-07-25 06:18 UTC, Dinar Yulmukhametov
no flags Details
patch for 4.22 (47.91 KB, patch)
2025-02-18 06:52 UTC, Andreas Schneider
metze: review+
Details
patch for 4.21 (44.59 KB, patch)
2025-02-18 06:56 UTC, Andreas Schneider
metze: review+
Details
Patch for 4.21 backported from master/4.22 (330.21 KB, patch)
2025-06-06 10:43 UTC, Ralph Böhme
metze: review+
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dinar Yulmukhametov 2024-07-09 14:22:48 UTC
When creating trusted domains with Windows Server 2016 and older, Samba
receives an ACCESS_DENIED response. After that, Windows disconnects
the connection.

Steps to reproduce:

$ samba-tool domain trust create win.dom.net --direction="both" --type="external" --quarantined="no" --create-location="both" --skip-validation -Uadministrator@win.dom.net
...
ERROR: REMOTE_DC[DC1.win.dom.net]: failed to query LSA_POLICY_INFO_DNS - ERROR(0xC0000022) - {Access Denied} A process has requested access to an object but has not been granted those access rights.

Actual result:
"Access denied" error

Expected Results:
Successful create of a trust domain
Comment 1 Andreas Schneider 2024-07-12 06:58:12 UTC
Can you please provide a the full output of samba-tool with log level 10 and a corresponding network trace?
Comment 2 Andreas Schneider 2024-07-12 07:01:35 UTC
You can also remove the try: line and the except: block in OpenPolicyFallback and you will get the NTError we probably miss to handle.
Comment 3 Dinar Yulmukhametov 2024-07-12 07:59:41 UTC
Created attachment 18375 [details]
trust create log with -d 10 and commented try-except

This is output for command:

samba-tool domain trust create winlocal.net --direction="both" --type="external" --quarantined="no" --create-location="both" --skip-validation -UAdministrator@winlocal.net -d 10

and OpenPolicyFallback function with commented try-except:

code:

def OpenPolicyFallback(
    conn: lsa.lsarpc,
    system_name: str,
    in_version: int,
    in_revision_info: lsa.revision_info1,
    sec_qos: bool = False,
    access_mask: int = 0,
):
    attr = lsa.ObjectAttribute()
    if sec_qos:
        qos = lsa.QosInfo()
        qos.len = 0xc
        qos.impersonation_level = 2
        qos.context_mode = 1
        qos.effective_only = 0

        attr.sec_qos = qos

    # try:
    out_version, out_rev_info, policy = conn.OpenPolicy3(
        system_name,
        attr,
        access_mask,
        in_version,
        in_revision_info
    )
    # except NTSTATUSError as e:
    #     if e.args[0] == NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE:
    #         out_version = 1
    #         out_rev_info = lsa.revision_info1()
    #         out_rev_info.revision = 1
    #         out_rev_info.supported_features = 0

    #         policy = conn.OpenPolicy2(system_name, attr, access_mask)
    #     else:
    #         raise

    return out_version, out_rev_info, policy
Comment 4 Andreas Schneider 2024-07-12 08:57:58 UTC
It looks like that the OpenPolicy3() succeeds, but then QueryInfoPolicy2() fails with ACCESS_DENIED using the policy handle returned by OpenPolicy3. A network trace would show more details.

If that's the case, then it it sounds like a Windows bug. MS-LSAD states:

> Note: LsarOpenPolicy3 supersedes LsarOpenPolicy2 and MUST be used whenever
> possible.

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lsad/9f88a19f-3bb0-474c-8308-a9f661e6c760
Comment 5 Andreas Schneider 2024-07-12 09:30:26 UTC
On the other hand, 3.1.4.4.3 LsarQueryInformationPolicy2 (Opnum 46) has:

> PolicyHandle: An RPC context handle obtained from either LsarOpenPolicy or 
> LsarOpenPolicy2.

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lsad/516f503c-0230-489d-b012-e650b46b66a2

I will contact Micosoft for clarification.
Comment 6 Andreas Schneider 2024-07-17 12:15:20 UTC
Created attachment 18378 [details]
Network trace (rpcclient lsaquery against Windows Server 2012)
Comment 7 Andreas Schneider 2024-07-17 13:03:28 UTC
If I do a LsarOpenPolicy3 call against Windows Server 2016, I get 
DCERPC_FAULT_ACCESS_DENIED and the dcerpc connection is closed by the server.

The dcerpc fault is unexpected and the disconnect is unfortunate.
Normally you get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE and then be able to call 
LsarOpenPolicy2 :-(
Comment 8 Stefan Metzmacher 2024-07-18 16:18:50 UTC
Created attachment 18379 [details]
Draft patches for a fix...

These need someone to cleanup, but the basics should work...
Comment 9 Dinar Yulmukhametov 2024-07-25 06:17:12 UTC
> Draft patches for a fix...

I checked the patch on the latest version of Samba. Unfortunately, it doesn't work. I am attaching a log with debugging of level 10.
Comment 10 Dinar Yulmukhametov 2024-07-25 06:18:38 UTC
Created attachment 18384 [details]
Log with patch from Stefan Metzmacher
Comment 11 Andreas Schneider 2024-07-25 07:19:17 UTC
The patch metze posted is far from complete.

If you call OpenPolicy3 against a Windows Server which doesn't support it, it will close the pipe. Metze implemented this behavior so Samba does the same. He also added a bunch of hacks so that you can test it in selftest (make testenv).

The next step is to implement the fallbacks correctly. If OpenPolicy3 fails and we have a disconnected pipe, reconnect and try OpenPolciy2. This is exactly what Windows does and we need to to the same. All the client code (trusts.py, rpcclient, winbind) needs to be able to deal with this.

A good start is probably getting the lsa_utils test to work:

make test TESTS=samba.tests.dcerpc.lsa_utils


I wont be able to work on this the next weeks.
Comment 12 Stefan Metzmacher 2024-07-25 07:29:05 UTC
(In reply to Dinar Yulmukhametov from comment #10)

Please also upload a network capture belonging to the logs.
Comment 13 Stefan Metzmacher 2025-02-11 17:11:34 UTC
I noticed that there's a bug in get_lsa_info()
it doesn't pass policy_access correctly to
OpenPolicyFallback, it passes it as sec_qos, so access_mask is always 0.
Comment 14 Samba QA Contact 2025-02-17 18:34:13 UTC
This bug was referenced in samba master:

d2ac6221db48b93581d7ce48d31f8851c88b77bc
0c68d9bc0cd5873f7b59be0fe93d64d6d47b5a57
3bbe35d42c4d4a0ce663580dfb035b6beb329ebb
a094a29e426cc79e23bb4d866334d7735159fb41
2c171fb1b8c88034a98c3aaf052e99ba5dbbafd9
73ce15e7d5b7ea867849f1aa4fa5390830660f11
f9a3fc19f1e212c54351c3f94978e66fceeb8835
a814f5d90a3fb85a94c9516dba224037e8fd76f1
Comment 15 Andreas Schneider 2025-02-18 06:52:00 UTC
Created attachment 18571 [details]
patch for 4.22
Comment 16 Andreas Schneider 2025-02-18 06:56:08 UTC
Created attachment 18572 [details]
patch for 4.21
Comment 17 Jule Anger 2025-02-20 10:15:54 UTC
Pushed to autobuild-v4-{22,21}-test.
Comment 18 Samba QA Contact 2025-02-20 11:23:13 UTC
This bug was referenced in samba v4-22-test:

60dc107d2a6154bf3254218e10b91c2a91ee88d9
d0420684649383ffbd309d23c69a0bf8a051fc06
1a3be37e0eb564604b20c5d2ab1842661d466433
310b5c9dcecfba72b3b02632fca08a68c042d2c1
82aa83142598f99d662fb9f16aa20c5e2f5fafa5
1f84f56c6df0813e488701704daf2174bd0f7eb3
8a7346f6c03dd4f5e8394997e1d118d33c950c35
29bd6fe9cbe538b267bf0ed66823cfe8599afb3d
Comment 19 Samba QA Contact 2025-02-20 13:02:45 UTC
This bug was referenced in samba v4-22-stable (Release samba-4.22.0rc3):

60dc107d2a6154bf3254218e10b91c2a91ee88d9
d0420684649383ffbd309d23c69a0bf8a051fc06
1a3be37e0eb564604b20c5d2ab1842661d466433
310b5c9dcecfba72b3b02632fca08a68c042d2c1
82aa83142598f99d662fb9f16aa20c5e2f5fafa5
1f84f56c6df0813e488701704daf2174bd0f7eb3
8a7346f6c03dd4f5e8394997e1d118d33c950c35
29bd6fe9cbe538b267bf0ed66823cfe8599afb3d
Comment 20 Jule Anger 2025-02-26 13:10:33 UTC
Patch for 4.21 failed during make:

../../source3/rpc_client/cli_pipe.c: In function ‘cli_rpc_pipe_reopen_np_noauth’:
../../source3/rpc_client/cli_pipe.c:3475:19: error: ‘struct rpc_pipe_client’ has no member named ‘assoc’
 3475 |         if (rpccli->assoc == NULL) {
      |                   ^~

Reassigning to Andreas.
Comment 21 Oliver Friedrich 2025-05-27 13:10:43 UTC
I can confirm the issue and I also run in the same compilation problems when I try to apply the backport for 4.21

Are there any updates?

FYR: https://forge.univention.org/bugzilla/show_bug.cgi?id=58299
Comment 22 Ralph Böhme 2025-06-06 10:43:42 UTC
Created attachment 18650 [details]
Patch for 4.21 backported from master/4.22
Comment 23 Oliver Friedrich 2025-06-06 14:34:40 UTC
I tried out the new patch and was able to re-compile the debian package.

Steps (on debian 12 / UCS5.2):
* apt-get build-dep samba
* apt-get source samba
* cd samba-4.21.1/
* patch -p1 < ../samba-bug-15680-v4-21-fix-trust-win2026.patch 
* dpkg-buildpackage -b


Will check the trust creation with M$ AD next.
Comment 24 Oliver Friedrich 2025-06-11 13:50:48 UTC
I can confirm that this works with the patched samba 4.21 as expected:

samba-tool domain trust create win-2k22.intranet --direction="both" --type="external" --quarantined="no" --create-location="both" --skip-validation -UAdministrator@win-2k22.intranet -d 10
...
no errors

The trust relationship is also valid on the Windows AD side.
Tested with Windows Server 2022, functional Level 2016.
Comment 25 Jule Anger 2025-06-12 09:30:21 UTC
Pushed to autobuild-v4-21-test.
Comment 26 Samba QA Contact 2025-06-12 12:41:05 UTC
This bug was referenced in samba v4-21-test:

fe5b8928443d3e9aac04b6748ccb9973b883f250
4ae32d6faa0e9e075cdeceb2d57c98f372b00448
1cbbe56266b81ffeedb57f2a3283b274e33981c7
30b077b67810adcd320facf0d41c4a0836fa0abe
0a0d87f4093c88e6ba43951f7182d8bcac49fbc2
e8e6b68539bebf93ad6ec8e86386705cb8f4cc15
b106c7d77aec6c2c82937fb24ef3ee6a16087cb6
ccb5e9694e30028c8bf849fdab1f06ecca861d26
Comment 27 Jule Anger 2025-06-12 12:46:54 UTC
Closing out bug report.

Thanks!