Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
11 KiB

  1. /*++
  2. Module Name:
  3. LDAPUtils.h
  4. Abstract:
  5. This is the header file for the LDAP utility functions.
  6. */
  7. #ifndef _LDAPUTILS_H
  8. #define _LDAPUTILS_H
  9. #include <stdafx.h>
  10. #include <winldap.h> // For LDAP APIs.
  11. #include <ntdsapi.h>
  12. #include <schedule.h>
  13. // Defines Values;
  14. #define MAX_RDN_KEY_SIZE 64 // ds\src\inc\ntdsa.h
  15. #define CN_SYSTEM _T("System")
  16. #define CN_FRS _T("File Replication Service")
  17. #define CN_DFSVOLUMES _T("DFS Volumes")
  18. #define CN_NTFRSSUBSCRIPTIONS _T("NTFRS Subscriptions")
  19. #define CN_DFSCONFIGURATION _T("Dfs-Configuration")
  20. #define CN_COMPUTERS _T("Computers")
  21. #define CN_DFSVOLUMES_PREFIX _T(",CN=DFS Volumes,CN=File Replication Service,CN=System")
  22. #define CN_DFSVOLUMES_PREFIX_COMMA _T(",CN=DFS Volumes,CN=File Replication Service,CN=System,")
  23. #define OBJCLASS_ATTRIBUTENAME _T("objectClass")
  24. typedef enum LDAP_ENTRY_ACTION
  25. {
  26. ADD_VALUE = 0,
  27. MODIFY_VALUE,
  28. DELETE_VALUE
  29. };
  30. typedef struct _LDAPNAME
  31. {
  32. CComBSTR bstrLDAPName;
  33. _LDAPNAME *Next;
  34. _LDAPNAME():Next(NULL)
  35. {
  36. }
  37. } LDAPNAME, *PLDAPNAME;
  38. // This holds a linked list of LDAP attributes and value.
  39. // Used in ldap_add, ldap_modify etc.
  40. typedef struct _LDAP_ATTR_VALUE
  41. {
  42. CComBSTR bstrAttribute; // Attribute name.
  43. void* vpValue; // Pointer to value buffer, void pointer to handle char as
  44. // well as binary values.
  45. BOOLEAN bBerValue; // Is this a BerValue?
  46. ULONG ulLength; // Size of a BerValue;
  47. _LDAP_ATTR_VALUE* Next; // The bBerValue fields of the structures other than
  48. // the head of the list are ignored.
  49. _LDAP_ATTR_VALUE():
  50. vpValue(NULL),
  51. bBerValue(false),
  52. ulLength(0),
  53. Next(NULL)
  54. {
  55. }
  56. } LDAP_ATTR_VALUE, *PLDAP_ATTR_VALUE;
  57. typedef struct _LDAPLLIST
  58. {
  59. PLDAP_ATTR_VALUE pAttrValues;
  60. _LDAPLLIST *Next;
  61. _LDAPLLIST():Next(NULL)
  62. {
  63. }
  64. } LDAPLLIST, *PLDAPLLIST;
  65. typedef struct _LLISTELEM
  66. {
  67. PTSTR** pppszAttrValues;
  68. _LLISTELEM *Next;
  69. _LLISTELEM(PTSTR** pppszValues):
  70. pppszAttrValues(pppszValues),
  71. Next(NULL)
  72. {
  73. }
  74. ~_LLISTELEM()
  75. {
  76. PTSTR** pppszValues = pppszAttrValues;
  77. while (*pppszValues)
  78. ldap_value_free(*pppszValues++);
  79. free(pppszAttrValues);
  80. }
  81. } LListElem;
  82. HRESULT FreeLDAPNamesList
  83. (
  84. IN PLDAPNAME i_pLDAPNames // pointer to list to be freed.
  85. );
  86. HRESULT FreeAttrValList
  87. (
  88. IN PLDAP_ATTR_VALUE i_pAttrVals // pointer to list to be freed.
  89. );
  90. // Connect To DS (LDAP)
  91. HRESULT ConnectToDS
  92. (
  93. IN PCTSTR i_lpszDomainName, // DNS or non DNS format.
  94. OUT PLDAP *o_ppldap,
  95. OUT BSTR* o_pbstrDC = NULL
  96. );
  97. // Close connection to DS
  98. HRESULT CloseConnectionToDS
  99. (
  100. IN PLDAP i_pldap
  101. );
  102. // Gets Values for an attribute from an LDAP Object.
  103. HRESULT GetValues
  104. (
  105. IN PLDAP i_pldap,
  106. IN PCTSTR i_lpszBase,
  107. IN PCTSTR i_lpszSearchFilter,
  108. IN ULONG i_ulScope,
  109. IN ULONG i_ulAttrCount,
  110. IN LDAP_ATTR_VALUE i_pAttributes[],
  111. OUT PLDAP_ATTR_VALUE o_ppValues[]
  112. );
  113. void FreeLListElem(LListElem* pElem);
  114. HRESULT GetValuesEx
  115. (
  116. IN PLDAP i_pldap,
  117. IN PCTSTR i_pszBase,
  118. IN ULONG i_ulScope,
  119. IN PCTSTR i_pszSearchFilter,
  120. IN PCTSTR i_pszAttributes[],
  121. OUT LListElem** o_ppElem
  122. );
  123. // Gets the DNs of all children of a DS object.
  124. HRESULT GetChildrenDN
  125. (
  126. IN PLDAP i_pldap,
  127. IN LPCTSTR i_lpszBase,
  128. IN ULONG i_ulScope,
  129. IN LPTSTR i_lpszChildObjectClass,
  130. OUT PLDAPNAME* o_ppDistNames
  131. );
  132. // Internal function to prepare LDAPMod
  133. HRESULT PrepareLDAPMods
  134. (
  135. IN LDAP_ATTR_VALUE i_pAttrValue[],
  136. IN LDAP_ENTRY_ACTION i_AddModDel,
  137. IN ULONG i_ulCountOfVals,
  138. OUT LDAPMod* o_ppModVals[]
  139. );
  140. // Adds a new record or values.
  141. HRESULT AddValues
  142. (
  143. IN PLDAP i_pldap,
  144. IN LPCTSTR i_DN,
  145. IN ULONG i_ulCountOfVals,
  146. OUT LDAP_ATTR_VALUE i_pAttrValue[],
  147. IN BSTR i_bstrDC = NULL
  148. );
  149. // Modifies an existing record or values.
  150. HRESULT ModifyValues
  151. (
  152. IN PLDAP i_pldap,
  153. IN LPCTSTR i_DN,
  154. IN ULONG i_ulCountOfVals,
  155. OUT LDAP_ATTR_VALUE i_pAttrValue[]
  156. );
  157. // Deletes values from an existing record or values.
  158. HRESULT DeleteValues
  159. (
  160. IN PLDAP i_pldap,
  161. IN LPCTSTR i_DN,
  162. IN ULONG i_ulCountOfVals,
  163. IN LDAP_ATTR_VALUE i_pAttrValue[]
  164. );
  165. // Deletes an object from DS.
  166. HRESULT DeleteDSObject
  167. (
  168. IN PLDAP i_pldap,
  169. IN LPCTSTR i_DN,
  170. IN bool i_bDeleteRecursively = true
  171. );
  172. // Free ModVals.
  173. HRESULT FreeModVals
  174. (
  175. IN OUT LDAPMod ***io_pppMod
  176. );
  177. // Gets a string corresponding to the ldap error code.
  178. LPTSTR ErrorString
  179. (
  180. DWORD i_ldapErrCode
  181. );
  182. // Checks if an object with given DN exists.
  183. HRESULT IsValidObject
  184. (
  185. IN PLDAP i_pldap,
  186. IN BSTR i_bstrObjectDN
  187. );
  188. // Gets the DN of an object given old style name.
  189. HRESULT CrackName(
  190. IN HANDLE i_hDS,
  191. IN LPTSTR i_lpszOldTypeName,
  192. IN DS_NAME_FORMAT i_formatIn,
  193. IN DS_NAME_FORMAT i_formatdesired,
  194. OUT BSTR* o_pbstrResult
  195. );
  196. // return S_FALSE if it's not NT5 domain
  197. HRESULT GetDomainInfo(
  198. IN LPCTSTR i_bstrDomain,
  199. OUT BSTR* o_pbstrDC = NULL, // return DC's Dns name
  200. OUT BSTR* o_pbstrDomainDnsName = NULL, // return Domain's Dns name
  201. OUT BSTR* o_pbstrDomainDN = NULL, // return DC=nttest,DC=microsoft,DC=com
  202. OUT BSTR* o_pbstrLDAPDomainPath = NULL,// return LDAP://<DC>/<DomainDN>
  203. OUT BSTR* o_pbstrDomainGuid = NULL // return Domain's guid
  204. );
  205. void
  206. DebugOutLDAPError(
  207. IN PLDAP i_pldap,
  208. IN ULONG i_ulError,
  209. IN PCTSTR i_pszLDAPFunctionName
  210. );
  211. HRESULT ExtendDN
  212. (
  213. IN LPTSTR i_lpszCN,
  214. IN LPTSTR i_lpszDN,
  215. OUT BSTR *o_pbstrNewDN
  216. );
  217. HRESULT ExtendDNIfLongJunctionName
  218. (
  219. IN LPTSTR i_lpszJunctionName,
  220. IN LPCTSTR i_lpszBaseDN,
  221. OUT BSTR *o_pbstrNewDN
  222. );
  223. HRESULT GetJunctionPathPartitions
  224. (
  225. OUT PVOID *o_ppBuffer,
  226. OUT DWORD *o_pdwEntries,
  227. IN LPCTSTR i_pszJunctionPath
  228. );
  229. HRESULT CreateExtraNodesIfLongJunctionName
  230. (
  231. IN PLDAP i_pldap,
  232. IN LPCTSTR i_lpszJunctionName,
  233. IN LPCTSTR i_lpszBaseDN,
  234. IN LPCTSTR i_lpszObjClass
  235. );
  236. HRESULT DeleteExtraNodesIfLongJunctionName
  237. (
  238. IN PLDAP i_pldap,
  239. IN LPCTSTR i_lpszJunctionName,
  240. IN LPCTSTR i_lpszDN
  241. );
  242. HRESULT CreateObjectSimple
  243. (
  244. IN PLDAP i_pldap,
  245. IN LPCTSTR i_lpszDN,
  246. IN LPCTSTR i_lpszObjClass
  247. );
  248. HRESULT CreateObjectsRecursively
  249. (
  250. IN PLDAP i_pldap,
  251. IN BSTR i_bstrDN,
  252. IN UINT i_nLenPrefix,
  253. IN LPCTSTR i_lpszObjClass
  254. );
  255. HRESULT DeleteAncestorNodesIfEmpty
  256. (
  257. IN PLDAP i_pldap,
  258. IN LPCTSTR i_lpszDN,
  259. IN DWORD i_dwCount
  260. );
  261. // Replace all occurences of '\' with '|' in the given string.
  262. HRESULT ReplaceChar
  263. (
  264. IN OUT BSTR io_bstrString,
  265. IN TCHAR i_cOldChar,
  266. IN TCHAR i_cNewChar
  267. );
  268. HRESULT GetDfsLinkNameFromDN(
  269. IN BSTR i_bstrReplicaSetDN,
  270. OUT BSTR* o_pbstrDfsLinkName
  271. );
  272. HRESULT GetSubscriberDN(
  273. IN BSTR i_bstrReplicaSetDN,
  274. IN BSTR i_bstrDomainGuid,
  275. IN BSTR i_bstrComputerDN,
  276. OUT BSTR* o_pbstrSubscriberDN
  277. );
  278. HRESULT CreateNtfrsMemberObject(
  279. IN PLDAP i_pldap,
  280. IN BSTR i_bstrMemberDN,
  281. IN BSTR i_bstrComputerDN,
  282. IN BSTR i_bstrDCofComputerObj
  283. );
  284. HRESULT CreateNtfrsSubscriberObject(
  285. IN PLDAP i_pldap,
  286. IN BSTR i_bstrSubscriberDN,
  287. IN BSTR i_bstrMemberDN,
  288. IN BSTR i_bstrRootPath,
  289. IN BSTR i_bstrStagingPath,
  290. IN BSTR i_bstrDC
  291. );
  292. HRESULT CreateNtdsConnectionObject(
  293. IN PLDAP i_pldap,
  294. IN BSTR i_bstrConnectionDN,
  295. IN BSTR i_bstrFromMemberDN,
  296. IN BOOL i_bEnable,
  297. IN DWORD i_dwOptions
  298. );
  299. HRESULT CreateNtfrsSettingsObjects(
  300. IN PLDAP i_pldap,
  301. IN BSTR i_bstrReplicaSetDN
  302. );
  303. HRESULT DeleteNtfrsReplicaSetObjectAndContainers(
  304. IN PLDAP i_pldap,
  305. IN BSTR i_bstrReplicaSetDN
  306. );
  307. HRESULT CreateNtfrsSubscriptionsObjects(
  308. IN PLDAP i_pldap,
  309. IN BSTR i_bstrSubscriberDN,
  310. IN BSTR i_bstrComputerDN
  311. );
  312. HRESULT DeleteNtfrsSubscriberObjectAndContainers(
  313. IN PLDAP i_pldap,
  314. IN BSTR i_bstrSubscriberDN,
  315. IN BSTR i_bstrComputerDN
  316. );
  317. HRESULT DeleteDSObjectsIfEmpty(
  318. IN PLDAP i_pldap,
  319. IN LPCTSTR i_lpszDN,
  320. IN int i_nPrefixLength
  321. );
  322. HRESULT SetConnectionSchedule(
  323. IN PLDAP i_pldap,
  324. IN BSTR i_bstrConnectionDN,
  325. IN SCHEDULE* i_pSchedule);
  326. HRESULT SetConnectionOptions(
  327. IN PLDAP i_pldap,
  328. IN BSTR i_bstrConnectionDN,
  329. IN DWORD i_dwOptions);
  330. HRESULT UuidToStructuredString(
  331. UUID* i_pUuid,
  332. BSTR* o_pbstr
  333. );
  334. HRESULT ScheduleToVariant(
  335. IN SCHEDULE* i_pSchedule,
  336. OUT VARIANT* o_pVar
  337. );
  338. HRESULT VariantToSchedule(
  339. IN VARIANT* i_pVar,
  340. OUT PSCHEDULE* o_ppSchedule
  341. );
  342. HRESULT CompareSchedules(
  343. IN SCHEDULE* i_pSchedule1,
  344. IN SCHEDULE* i_pSchedule2
  345. );
  346. HRESULT CopySchedule(
  347. IN SCHEDULE* i_pSrcSchedule,
  348. OUT PSCHEDULE* o_ppDstSchedule
  349. );
  350. HRESULT GetDefaultSchedule(
  351. OUT PSCHEDULE* o_ppSchedule
  352. );
  353. HRESULT GetSchemaVersion(IN PLDAP i_pldap);
  354. HRESULT GetSchemaVersionEx(
  355. IN BSTR i_bstrName,
  356. IN BOOL i_bServer = TRUE // TRUE if i_bstrName is a server, FALSE if i_bstrName is a domain
  357. );
  358. HRESULT LdapConnectToDC(IN LPCTSTR i_pszDC, OUT PLDAP* o_ppldap);
  359. HRESULT
  360. GetErrorMessage(
  361. IN DWORD i_dwError,
  362. OUT BSTR* o_pbstrErrorMsg
  363. );
  364. HRESULT
  365. FormatMessageString(
  366. OUT BSTR *o_pbstrMsg,
  367. IN DWORD dwErr,
  368. IN UINT iStringId,
  369. ...);
  370. HRESULT DsBindToDS(BSTR i_bstrDomain, BSTR *o_pbstrDC, HANDLE *o_phDS);
  371. #ifdef DEBUG
  372. void PrintTimeDelta(LPCTSTR pszMsg, SYSTEMTIME* pt0, SYSTEMTIME* pt1);
  373. #endif // DEBUG
  374. #endif //#ifndef _LDAPUTILS_H