Samr.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. {
  79. this.SystemName = systemName;
  80. this.AccessMask = accessMask;
  81. this.Handle = handle;
  82. }
  83. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  84. public override void Encode_in(NdrBuffer dst)
  85. {
  86. dst.Enc_ndr_referent(SystemName, 1);
  87. if (SystemName != null)
  88. {
  89. dst.Enc_ndr_string(SystemName);
  90. }
  91. dst.Enc_ndr_long(AccessMask);
  92. }
  93. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  94. public override void Decode_out(NdrBuffer src)
  95. {
  96. Handle.Decode(src);
  97. Retval = src.Dec_ndr_long();
  98. }
  99. }
  100. public class SamrConnect4 : DcerpcMessage
  101. {
  102. public override int GetOpnum()
  103. {
  104. return unchecked(0x3e);
  105. }
  106. public int Retval;
  107. public string SystemName;
  108. public int Unknown;
  109. public int AccessMask;
  110. public Rpc.PolicyHandle Handle;
  111. public SamrConnect4(string systemName, int unknown, int accessMask, Rpc.PolicyHandle
  112. handle)
  113. {
  114. this.SystemName = systemName;
  115. this.Unknown = unknown;
  116. this.AccessMask = accessMask;
  117. this.Handle = handle;
  118. }
  119. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  120. public override void Encode_in(NdrBuffer dst)
  121. {
  122. dst.Enc_ndr_referent(SystemName, 1);
  123. if (SystemName != null)
  124. {
  125. dst.Enc_ndr_string(SystemName);
  126. }
  127. dst.Enc_ndr_long(Unknown);
  128. dst.Enc_ndr_long(AccessMask);
  129. }
  130. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  131. public override void Decode_out(NdrBuffer src)
  132. {
  133. Handle.Decode(src);
  134. Retval = src.Dec_ndr_long();
  135. }
  136. }
  137. public class SamrOpenDomain : DcerpcMessage
  138. {
  139. public override int GetOpnum()
  140. {
  141. return unchecked(0x07);
  142. }
  143. public int Retval;
  144. public Rpc.PolicyHandle Handle;
  145. public int AccessMask;
  146. public Rpc.SidT Sid;
  147. public Rpc.PolicyHandle DomainHandle;
  148. public SamrOpenDomain(Rpc.PolicyHandle handle, int accessMask, Rpc.SidT sid, Rpc.PolicyHandle
  149. 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, int resumeHandle
  305. , int acctFlags, SamrSamArray sam, int numEntries)
  306. {
  307. this.DomainHandle = domainHandle;
  308. this.ResumeHandle = resumeHandle;
  309. this.AcctFlags = acctFlags;
  310. this.Sam = sam;
  311. this.NumEntries = numEntries;
  312. }
  313. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  314. public override void Encode_in(NdrBuffer dst)
  315. {
  316. DomainHandle.Encode(dst);
  317. dst.Enc_ndr_long(ResumeHandle);
  318. dst.Enc_ndr_long(AcctFlags);
  319. }
  320. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  321. public override void Decode_out(NdrBuffer src)
  322. {
  323. ResumeHandle = src.Dec_ndr_long();
  324. int samp = src.Dec_ndr_long();
  325. if (samp != 0)
  326. {
  327. if (Sam == null)
  328. {
  329. Sam = new SamrSamArray();
  330. }
  331. Sam.Decode(src);
  332. }
  333. NumEntries = src.Dec_ndr_long();
  334. Retval = src.Dec_ndr_long();
  335. }
  336. }
  337. public class SamrOpenAlias : DcerpcMessage
  338. {
  339. public override int GetOpnum()
  340. {
  341. return unchecked(0x1b);
  342. }
  343. public int Retval;
  344. public Rpc.PolicyHandle DomainHandle;
  345. public int AccessMask;
  346. public int Rid;
  347. public Rpc.PolicyHandle AliasHandle;
  348. public SamrOpenAlias(Rpc.PolicyHandle domainHandle, int accessMask, int rid, Rpc.PolicyHandle
  349. aliasHandle)
  350. {
  351. this.DomainHandle = domainHandle;
  352. this.AccessMask = accessMask;
  353. this.Rid = rid;
  354. this.AliasHandle = aliasHandle;
  355. }
  356. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  357. public override void Encode_in(NdrBuffer dst)
  358. {
  359. DomainHandle.Encode(dst);
  360. dst.Enc_ndr_long(AccessMask);
  361. dst.Enc_ndr_long(Rid);
  362. }
  363. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  364. public override void Decode_out(NdrBuffer src)
  365. {
  366. AliasHandle.Decode(src);
  367. Retval = src.Dec_ndr_long();
  368. }
  369. }
  370. public class SamrGetMembersInAlias : DcerpcMessage
  371. {
  372. public override int GetOpnum()
  373. {
  374. return unchecked(0x21);
  375. }
  376. public int Retval;
  377. public Rpc.PolicyHandle AliasHandle;
  378. public Lsarpc.LsarSidArray Sids;
  379. public SamrGetMembersInAlias(Rpc.PolicyHandle aliasHandle, Lsarpc.LsarSidArray
  380. sids)
  381. {
  382. this.AliasHandle = aliasHandle;
  383. this.Sids = sids;
  384. }
  385. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  386. public override void Encode_in(NdrBuffer dst)
  387. {
  388. AliasHandle.Encode(dst);
  389. }
  390. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  391. public override void Decode_out(NdrBuffer src)
  392. {
  393. Sids.Decode(src);
  394. Retval = src.Dec_ndr_long();
  395. }
  396. }
  397. public const int SeGroupMandatory = 1;
  398. public const int SeGroupEnabledByDefault = 2;
  399. public const int SeGroupEnabled = 4;
  400. public const int SeGroupOwner = 8;
  401. public const int SeGroupUseForDenyOnly = 16;
  402. public const int SeGroupResource = 536870912;
  403. public const int SeGroupLogonId = -1073741824;
  404. public class SamrRidWithAttribute : NdrObject
  405. {
  406. public int Rid;
  407. public int Attributes;
  408. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  409. public override void Encode(NdrBuffer dst)
  410. {
  411. dst.Align(4);
  412. dst.Enc_ndr_long(Rid);
  413. dst.Enc_ndr_long(Attributes);
  414. }
  415. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  416. public override void Decode(NdrBuffer src)
  417. {
  418. src.Align(4);
  419. Rid = src.Dec_ndr_long();
  420. Attributes = src.Dec_ndr_long();
  421. }
  422. }
  423. public class SamrRidWithAttributeArray : NdrObject
  424. {
  425. public int Count;
  426. public SamrRidWithAttribute[] Rids;
  427. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  428. public override void Encode(NdrBuffer dst)
  429. {
  430. dst.Align(4);
  431. dst.Enc_ndr_long(Count);
  432. dst.Enc_ndr_referent(Rids, 1);
  433. if (Rids != null)
  434. {
  435. dst = dst.Deferred;
  436. int ridss = Count;
  437. dst.Enc_ndr_long(ridss);
  438. int ridsi = dst.Index;
  439. dst.Advance(8 * ridss);
  440. dst = dst.Derive(ridsi);
  441. for (int i = 0; i < ridss; i++)
  442. {
  443. Rids[i].Encode(dst);
  444. }
  445. }
  446. }
  447. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  448. public override void Decode(NdrBuffer src)
  449. {
  450. src.Align(4);
  451. Count = src.Dec_ndr_long();
  452. int ridsp = src.Dec_ndr_long();
  453. if (ridsp != 0)
  454. {
  455. src = src.Deferred;
  456. int ridss = src.Dec_ndr_long();
  457. int ridsi = src.Index;
  458. src.Advance(8 * ridss);
  459. if (Rids == null)
  460. {
  461. if (ridss < 0 || ridss > unchecked(0xFFFF))
  462. {
  463. throw new NdrException(NdrException.InvalidConformance);
  464. }
  465. Rids = new SamrRidWithAttribute[ridss];
  466. }
  467. src = src.Derive(ridsi);
  468. for (int i = 0; i < ridss; i++)
  469. {
  470. if (Rids[i] == null)
  471. {
  472. Rids[i] = new SamrRidWithAttribute();
  473. }
  474. Rids[i].Decode(src);
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }