s4:lib/socket: simplify iface_list_wildcard() and its callers
[Samba.git] / source4 / scripting / devel / createtrust
blob7f1ba33b85ce2c78b08c2230d20e2316c3c963c8
1 #!/usr/bin/env python
3 # create a domain trust
5 import sys
6 from optparse import OptionParser
8 sys.path.insert(0, "bin/python")
10 import samba
11 import samba.getopt as options
12 from samba.dcerpc import lsa, security, drsblobs
13 from samba.ndr import ndr_pack
14 import random
16 def arcfour_encrypt(key, data):
17 from Crypto.Cipher import ARC4
18 c = ARC4.new(key)
19 return c.encrypt(data)
21 def string_to_array(string):
22 blob = [0] * len(string)
24 for i in range(len(string)):
25 blob[i] = ord(string[i])
27 return blob
29 ########### main code ###########
30 if __name__ == "__main__":
31 parser = OptionParser("createtrust [options] server")
32 sambaopts = options.SambaOptions(parser)
33 credopts = options.CredentialsOptionsDouble(parser)
34 parser.add_option_group(credopts)
36 (opts, args) = parser.parse_args()
38 lp = sambaopts.get_loadparm()
39 creds = credopts.get_credentials(lp)
41 if len(args) != 1:
42 parser.error("You must supply a server")
44 if not creds.authentication_requested():
45 parser.error("You must supply credentials")
47 server = args[0]
49 binding_str = "ncacn_np:%s[print]" % server
51 lsaconn = lsa.lsarpc(binding_str, lp, creds)
53 objectAttr = lsa.ObjectAttribute()
54 objectAttr.sec_qos = lsa.QosInfo()
56 pol_handle = lsaconn.OpenPolicy2(''.decode('utf-8'),
57 objectAttr, security.SEC_FLAG_MAXIMUM_ALLOWED)
59 name = lsa.String()
60 name.string = "sub2.win2k3.obed.home.abartlet.net"
61 try:
62 info = lsaconn.QueryTrustedDomainInfoByName(pol_handle, name, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
64 lsaconn.DeleteTrustedDomain(pol_handle, info.info_ex.sid)
65 except RuntimeError:
66 pass
68 info = lsa.TrustDomainInfoInfoEx()
69 info.domain_name.string = "sub2.win2k3.obed.home.abartlet.net"
70 info.netbios_name.string = "sub2"
71 info.sid = security.dom_sid("S-1-5-21-538090388-3760119675-95745416")
72 info.trust_direction = lsa.LSA_TRUST_DIRECTION_INBOUND | lsa.LSA_TRUST_DIRECTION_OUTBOUND
73 info.trust_type = lsa.LSA_TRUST_TYPE_UPLEVEL
74 info.trust_attributes = lsa.LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
76 password_blob = string_to_array("password".encode('utf-16-le'))
78 clear_value = drsblobs.AuthInfoClear()
79 clear_value.size = len(password_blob)
80 clear_value.password = password_blob
82 clear_authentication_information = drsblobs.AuthenticationInformation()
83 clear_authentication_information.LastUpdateTime = 0
84 clear_authentication_information.AuthType = lsa.TRUST_AUTH_TYPE_CLEAR
85 clear_authentication_information.AuthInfo = clear_value
87 version_value = drsblobs.AuthInfoVersion()
88 version_value.version = 1
90 version = drsblobs.AuthenticationInformation()
91 version.LastUpdateTime = 0
92 version.AuthType = lsa.TRUST_AUTH_TYPE_VERSION
93 version.AuthInfo = version_value
95 authentication_information_array = drsblobs.AuthenticationInformationArray()
96 authentication_information_array.count = 2
97 authentication_information_array.array = [clear_authentication_information, version]
99 outgoing = drsblobs.trustAuthInOutBlob()
100 outgoing.count = 1
101 outgoing.current = authentication_information_array
103 trustpass = drsblobs.trustDomainPasswords()
104 confounder = [3] * 512
106 for i in range(512):
107 confounder[i] = random.randint(0, 255)
109 trustpass.confounder = confounder
111 # print "confounder: ", trustpass.confounder
113 trustpass.outgoing = outgoing
114 trustpass.incoming = outgoing
116 trustpass_blob = ndr_pack(trustpass)
118 # print "trustpass_blob: ", list(trustpass_blob)
120 encrypted_trustpass = arcfour_encrypt(lsaconn.session_key, trustpass_blob)
122 # print "encrypted_trustpass: ", list(encrypted_trustpass)
124 auth_blob = lsa.DATA_BUF2()
125 auth_blob.size = len(encrypted_trustpass)
126 auth_blob.data = string_to_array(encrypted_trustpass)
128 auth_info = lsa.TrustDomainInfoAuthInfoInternal()
129 auth_info.auth_blob = auth_blob
132 # print "auth_info.auth_blob.data: ", auth_info.auth_blob.data
134 trustdom_handle = lsaconn.CreateTrustedDomainEx2(pol_handle,
135 info,
136 auth_info,
137 security.SEC_STD_DELETE)