Samr.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using SharpCifs.Dcerpc.Ndr;
  18. namespace SharpCifs.Dcerpc.Msrpc
  19. {
  20. public class Samr
  21. {
  22. public static string GetSyntax()
  23. {
  24. return "12345778-1234-abcd-ef00-0123456789ac:1.0";
  25. }
  26. public const int AcbDisabled = 1;
  27. public const int AcbHomdirreq = 2;
  28. public const int AcbPwnotreq = 4;
  29. public const int AcbTempdup = 8;
  30. public const int AcbNormal = 16;
  31. public const int AcbMns = 32;
  32. public const int AcbDomtrust = 64;
  33. public const int AcbWstrust = 128;
  34. public const int AcbSvrtrust = 256;
  35. public const int AcbPwnoexp = 512;
  36. public const int AcbAutolock = 1024;
  37. public const int AcbEncTxtPwdAllowed = 2048;
  38. public const int AcbSmartcardRequired = 4096;
  39. public const int AcbTrustedForDelegation = 8192;
  40. public const int AcbNotDelegated = 16384;
  41. public const int AcbUseDesKeyOnly = 32768;
  42. public const int AcbDontRequirePreauth = 65536;
  43. public class SamrCloseHandle : DcerpcMessage
  44. {
  45. public override int GetOpnum()
  46. {
  47. return unchecked(0x01);
  48. }
  49. public int Retval;
  50. public Rpc.PolicyHandle Handle;
  51. public SamrCloseHandle(Rpc.PolicyHandle handle)
  52. {
  53. this.Handle = handle;
  54. }
  55. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  56. public override void Encode_in(NdrBuffer dst)
  57. {
  58. Handle.Encode(dst);
  59. }
  60. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  61. public override void Decode_out(NdrBuffer src)
  62. {
  63. Retval = src.Dec_ndr_long();
  64. }
  65. }
  66. public class SamrConnect2 : DcerpcMessage
  67. {
  68. public override int GetOpnum()
  69. {
  70. return unchecked(0x39);
  71. }
  72. public int Retval;
  73. public string SystemName;
  74. public int AccessMask;
  75. public Rpc.PolicyHandle Handle;
  76. public SamrConnect2(string systemName, int accessMask, Rpc.PolicyHandle handle)
  77. {
  78. this.SystemName = systemName;
  79. this.AccessMask = accessMask;
  80. this.Handle = handle;
  81. }
  82. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  83. public override void Encode_in(NdrBuffer dst)
  84. {
  85. dst.Enc_ndr_referent(SystemName, 1);
  86. if (SystemName != null)
  87. {
  88. dst.Enc_ndr_string(SystemName);
  89. }
  90. dst.Enc_ndr_long(AccessMask);
  91. }
  92. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  93. public override void Decode_out(NdrBuffer src)
  94. {
  95. Handle.Decode(src);
  96. Retval = src.Dec_ndr_long();
  97. }
  98. }
  99. public class SamrConnect4 : DcerpcMessage
  100. {
  101. public override int GetOpnum()
  102. {
  103. return unchecked(0x3e);
  104. }
  105. public int Retval;
  106. public string SystemName;
  107. public int Unknown;
  108. public int AccessMask;
  109. public Rpc.PolicyHandle Handle;
  110. public SamrConnect4(string systemName, int unknown, int accessMask, Rpc.PolicyHandle handle)
  111. {
  112. this.SystemName = systemName;
  113. this.Unknown = unknown;
  114. this.AccessMask = accessMask;
  115. this.Handle = handle;
  116. }
  117. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  118. public override void Encode_in(NdrBuffer dst)
  119. {
  120. dst.Enc_ndr_referent(SystemName, 1);
  121. if (SystemName != null)
  122. {
  123. dst.Enc_ndr_string(SystemName);
  124. }
  125. dst.Enc_ndr_long(Unknown);
  126. dst.Enc_ndr_long(AccessMask);
  127. }
  128. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  129. public override void Decode_out(NdrBuffer src)
  130. {
  131. Handle.Decode(src);
  132. Retval = src.Dec_ndr_long();
  133. }
  134. }
  135. public class SamrOpenDomain : DcerpcMessage
  136. {
  137. public override int GetOpnum()
  138. {
  139. return unchecked(0x07);
  140. }
  141. public int Retval;
  142. public Rpc.PolicyHandle Handle;
  143. public int AccessMask;
  144. public Rpc.SidT Sid;
  145. public Rpc.PolicyHandle DomainHandle;
  146. public SamrOpenDomain(Rpc.PolicyHandle handle,
  147. int accessMask,
  148. Rpc.SidT sid,
  149. Rpc.PolicyHandle domainHandle)
  150. {
  151. this.Handle = handle;
  152. this.AccessMask = accessMask;
  153. this.Sid = sid;
  154. this.DomainHandle = domainHandle;
  155. }
  156. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  157. public override void Encode_in(NdrBuffer dst)
  158. {
  159. Handle.Encode(dst);
  160. dst.Enc_ndr_long(AccessMask);
  161. Sid.Encode(dst);
  162. }
  163. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  164. public override void Decode_out(NdrBuffer src)
  165. {
  166. DomainHandle.Decode(src);
  167. Retval = src.Dec_ndr_long();
  168. }
  169. }
  170. public class SamrSamEntry : NdrObject
  171. {
  172. public int Idx;
  173. public Rpc.Unicode_string Name;
  174. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  175. public override void Encode(NdrBuffer dst)
  176. {
  177. dst.Align(4);
  178. dst.Enc_ndr_long(Idx);
  179. dst.Enc_ndr_short(Name.Length);
  180. dst.Enc_ndr_short(Name.MaximumLength);
  181. dst.Enc_ndr_referent(Name.Buffer, 1);
  182. if (Name.Buffer != null)
  183. {
  184. dst = dst.Deferred;
  185. int nameBufferl = Name.Length / 2;
  186. int nameBuffers = Name.MaximumLength / 2;
  187. dst.Enc_ndr_long(nameBuffers);
  188. dst.Enc_ndr_long(0);
  189. dst.Enc_ndr_long(nameBufferl);
  190. int nameBufferi = dst.Index;
  191. dst.Advance(2 * nameBufferl);
  192. dst = dst.Derive(nameBufferi);
  193. for (int i = 0; i < nameBufferl; i++)
  194. {
  195. dst.Enc_ndr_short(Name.Buffer[i]);
  196. }
  197. }
  198. }
  199. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  200. public override void Decode(NdrBuffer src)
  201. {
  202. src.Align(4);
  203. Idx = src.Dec_ndr_long();
  204. src.Align(4);
  205. if (Name == null)
  206. {
  207. Name = new Rpc.Unicode_string();
  208. }
  209. Name.Length = (short)src.Dec_ndr_short();
  210. Name.MaximumLength = (short)src.Dec_ndr_short();
  211. int nameBufferp = src.Dec_ndr_long();
  212. if (nameBufferp != 0)
  213. {
  214. src = src.Deferred;
  215. int nameBuffers = src.Dec_ndr_long();
  216. src.Dec_ndr_long();
  217. int nameBufferl = src.Dec_ndr_long();
  218. int nameBufferi = src.Index;
  219. src.Advance(2 * nameBufferl);
  220. if (Name.Buffer == null)
  221. {
  222. if (nameBuffers < 0 || nameBuffers > unchecked(0xFFFF))
  223. {
  224. throw new NdrException(NdrException.InvalidConformance);
  225. }
  226. Name.Buffer = new short[nameBuffers];
  227. }
  228. src = src.Derive(nameBufferi);
  229. for (int i = 0; i < nameBufferl; i++)
  230. {
  231. Name.Buffer[i] = (short)src.Dec_ndr_short();
  232. }
  233. }
  234. }
  235. }
  236. public class SamrSamArray : NdrObject
  237. {
  238. public int Count;
  239. public SamrSamEntry[] Entries;
  240. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  241. public override void Encode(NdrBuffer dst)
  242. {
  243. dst.Align(4);
  244. dst.Enc_ndr_long(Count);
  245. dst.Enc_ndr_referent(Entries, 1);
  246. if (Entries != null)
  247. {
  248. dst = dst.Deferred;
  249. int entriess = Count;
  250. dst.Enc_ndr_long(entriess);
  251. int entriesi = dst.Index;
  252. dst.Advance(12 * entriess);
  253. dst = dst.Derive(entriesi);
  254. for (int i = 0; i < entriess; i++)
  255. {
  256. Entries[i].Encode(dst);
  257. }
  258. }
  259. }
  260. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  261. public override void Decode(NdrBuffer src)
  262. {
  263. src.Align(4);
  264. Count = src.Dec_ndr_long();
  265. int entriesp = src.Dec_ndr_long();
  266. if (entriesp != 0)
  267. {
  268. src = src.Deferred;
  269. int entriess = src.Dec_ndr_long();
  270. int entriesi = src.Index;
  271. src.Advance(12 * entriess);
  272. if (Entries == null)
  273. {
  274. if (entriess < 0 || entriess > unchecked(0xFFFF))
  275. {
  276. throw new NdrException(NdrException.InvalidConformance);
  277. }
  278. Entries = new SamrSamEntry[entriess];
  279. }
  280. src = src.Derive(entriesi);
  281. for (int i = 0; i < entriess; i++)
  282. {
  283. if (Entries[i] == null)
  284. {
  285. Entries[i] = new SamrSamEntry();
  286. }
  287. Entries[i].Decode(src);
  288. }
  289. }
  290. }
  291. }
  292. public class SamrEnumerateAliasesInDomain : DcerpcMessage
  293. {
  294. public override int GetOpnum()
  295. {
  296. return unchecked(0x0f);
  297. }
  298. public int Retval;
  299. public Rpc.PolicyHandle DomainHandle;
  300. public int ResumeHandle;
  301. public int AcctFlags;
  302. public SamrSamArray Sam;
  303. public int NumEntries;
  304. public SamrEnumerateAliasesInDomain(Rpc.PolicyHandle domainHandle,
  305. int resumeHandle,
  306. int acctFlags,
  307. SamrSamArray sam,
  308. int numEntries)
  309. {
  310. this.DomainHandle = domainHandle;
  311. this.ResumeHandle = resumeHandle;
  312. this.AcctFlags = acctFlags;
  313. this.Sam = sam;
  314. this.NumEntries = numEntries;
  315. }
  316. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  317. public override void Encode_in(NdrBuffer dst)
  318. {
  319. DomainHandle.Encode(dst);
  320. dst.Enc_ndr_long(ResumeHandle);
  321. dst.Enc_ndr_long(AcctFlags);
  322. }
  323. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  324. public override void Decode_out(NdrBuffer src)
  325. {
  326. ResumeHandle = src.Dec_ndr_long();
  327. int samp = src.Dec_ndr_long();
  328. if (samp != 0)
  329. {
  330. if (Sam == null)
  331. {
  332. Sam = new SamrSamArray();
  333. }
  334. Sam.Decode(src);
  335. }
  336. NumEntries = src.Dec_ndr_long();
  337. Retval = src.Dec_ndr_long();
  338. }
  339. }
  340. public class SamrOpenAlias : DcerpcMessage
  341. {
  342. public override int GetOpnum()
  343. {
  344. return unchecked(0x1b);
  345. }
  346. public int Retval;
  347. public Rpc.PolicyHandle DomainHandle;
  348. public int AccessMask;
  349. public int Rid;
  350. public Rpc.PolicyHandle AliasHandle;
  351. public SamrOpenAlias(Rpc.PolicyHandle domainHandle,
  352. int accessMask,
  353. int rid,
  354. Rpc.PolicyHandle aliasHandle)
  355. {
  356. this.DomainHandle = domainHandle;
  357. this.AccessMask = accessMask;
  358. this.Rid = rid;
  359. this.AliasHandle = aliasHandle;
  360. }
  361. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  362. public override void Encode_in(NdrBuffer dst)
  363. {
  364. DomainHandle.Encode(dst);
  365. dst.Enc_ndr_long(AccessMask);
  366. dst.Enc_ndr_long(Rid);
  367. }
  368. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  369. public override void Decode_out(NdrBuffer src)
  370. {
  371. AliasHandle.Decode(src);
  372. Retval = src.Dec_ndr_long();
  373. }
  374. }
  375. public class SamrGetMembersInAlias : DcerpcMessage
  376. {
  377. public override int GetOpnum()
  378. {
  379. return unchecked(0x21);
  380. }
  381. public int Retval;
  382. public Rpc.PolicyHandle AliasHandle;
  383. public Lsarpc.LsarSidArray Sids;
  384. public SamrGetMembersInAlias(Rpc.PolicyHandle aliasHandle, Lsarpc.LsarSidArray sids)
  385. {
  386. this.AliasHandle = aliasHandle;
  387. this.Sids = sids;
  388. }
  389. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  390. public override void Encode_in(NdrBuffer dst)
  391. {
  392. AliasHandle.Encode(dst);
  393. }
  394. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  395. public override void Decode_out(NdrBuffer src)
  396. {
  397. Sids.Decode(src);
  398. Retval = src.Dec_ndr_long();
  399. }
  400. }
  401. public const int SeGroupMandatory = 1;
  402. public const int SeGroupEnabledByDefault = 2;
  403. public const int SeGroupEnabled = 4;
  404. public const int SeGroupOwner = 8;
  405. public const int SeGroupUseForDenyOnly = 16;
  406. public const int SeGroupResource = 536870912;
  407. public const int SeGroupLogonId = -1073741824;
  408. public class SamrRidWithAttribute : NdrObject
  409. {
  410. public int Rid;
  411. public int Attributes;
  412. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  413. public override void Encode(NdrBuffer dst)
  414. {
  415. dst.Align(4);
  416. dst.Enc_ndr_long(Rid);
  417. dst.Enc_ndr_long(Attributes);
  418. }
  419. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  420. public override void Decode(NdrBuffer src)
  421. {
  422. src.Align(4);
  423. Rid = src.Dec_ndr_long();
  424. Attributes = src.Dec_ndr_long();
  425. }
  426. }
  427. public class SamrRidWithAttributeArray : NdrObject
  428. {
  429. public int Count;
  430. public SamrRidWithAttribute[] Rids;
  431. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  432. public override void Encode(NdrBuffer dst)
  433. {
  434. dst.Align(4);
  435. dst.Enc_ndr_long(Count);
  436. dst.Enc_ndr_referent(Rids, 1);
  437. if (Rids != null)
  438. {
  439. dst = dst.Deferred;
  440. int ridss = Count;
  441. dst.Enc_ndr_long(ridss);
  442. int ridsi = dst.Index;
  443. dst.Advance(8 * ridss);
  444. dst = dst.Derive(ridsi);
  445. for (int i = 0; i < ridss; i++)
  446. {
  447. Rids[i].Encode(dst);
  448. }
  449. }
  450. }
  451. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  452. public override void Decode(NdrBuffer src)
  453. {
  454. src.Align(4);
  455. Count = src.Dec_ndr_long();
  456. int ridsp = src.Dec_ndr_long();
  457. if (ridsp != 0)
  458. {
  459. src = src.Deferred;
  460. int ridss = src.Dec_ndr_long();
  461. int ridsi = src.Index;
  462. src.Advance(8 * ridss);
  463. if (Rids == null)
  464. {
  465. if (ridss < 0 || ridss > unchecked(0xFFFF))
  466. {
  467. throw new NdrException(NdrException.InvalidConformance);
  468. }
  469. Rids = new SamrRidWithAttribute[ridss];
  470. }
  471. src = src.Derive(ridsi);
  472. for (int i = 0; i < ridss; i++)
  473. {
  474. if (Rids[i] == null)
  475. {
  476. Rids[i] = new SamrRidWithAttribute();
  477. }
  478. Rids[i].Decode(src);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }