Source code of Windows XP (NT5)
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.

462 lines
10 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 root path of a DS.
  124. HRESULT GetLDAPRootPath
  125. (
  126. IN PLDAP pldap,
  127. OUT LPTSTR* ppszRootPath
  128. );
  129. // Gets the DNs of all children of a DS object.
  130. HRESULT GetChildrenDN
  131. (
  132. IN PLDAP i_pldap,
  133. IN LPCTSTR i_lpszBase,
  134. IN ULONG i_ulScope,
  135. IN LPTSTR i_lpszChildObjectClass,
  136. OUT PLDAPNAME* o_ppDistNames
  137. );
  138. // Internal function to prepare LDAPMod
  139. HRESULT PrepareLDAPMods
  140. (
  141. IN LDAP_ATTR_VALUE i_pAttrValue[],
  142. IN LDAP_ENTRY_ACTION i_AddModDel,
  143. IN ULONG i_ulCountOfVals,
  144. OUT LDAPMod* o_ppModVals[]
  145. );
  146. // Adds a new record or values.
  147. HRESULT AddValues
  148. (
  149. IN PLDAP i_pldap,
  150. IN LPCTSTR i_DN,
  151. IN ULONG i_ulCountOfVals,
  152. OUT LDAP_ATTR_VALUE i_pAttrValue[],
  153. IN BSTR i_bstrDC = NULL
  154. );
  155. // Modifies an existing record or values.
  156. HRESULT ModifyValues
  157. (
  158. IN PLDAP i_pldap,
  159. IN LPCTSTR i_DN,
  160. IN ULONG i_ulCountOfVals,
  161. OUT LDAP_ATTR_VALUE i_pAttrValue[]
  162. );
  163. // Deletes values from an existing record or values.
  164. HRESULT DeleteValues
  165. (
  166. IN PLDAP i_pldap,
  167. IN LPCTSTR i_DN,
  168. IN ULONG i_ulCountOfVals,
  169. IN LDAP_ATTR_VALUE i_pAttrValue[]
  170. );
  171. // Deletes an object from DS.
  172. HRESULT DeleteDSObject
  173. (
  174. IN PLDAP i_pldap,
  175. IN LPCTSTR i_DN,
  176. IN bool i_bDeleteRecursively = true
  177. );
  178. // Free ModVals.
  179. HRESULT FreeModVals
  180. (
  181. IN OUT LDAPMod ***io_pppMod
  182. );
  183. // Gets a string corresponding to the ldap error code.
  184. LPTSTR ErrorString
  185. (
  186. DWORD i_ldapErrCode
  187. );
  188. // Checks if an object with given DN exists.
  189. HRESULT IsValidObject
  190. (
  191. IN PLDAP i_pldap,
  192. IN BSTR i_bstrObjectDN
  193. );
  194. // Gets the DN of an object given old style name.
  195. HRESULT CrackName(
  196. IN HANDLE i_hDS,
  197. IN LPTSTR i_lpszOldTypeName,
  198. IN DS_NAME_FORMAT i_formatIn,
  199. IN DS_NAME_FORMAT i_formatdesired,
  200. OUT BSTR* o_pbstrResult
  201. );
  202. // return S_FALSE if it's not NT5 domain
  203. HRESULT GetDomainInfo(
  204. IN LPCTSTR i_bstrDomain,
  205. OUT BSTR* o_pbstrDC = NULL, // return DC's Dns name
  206. OUT BSTR* o_pbstrDomainDnsName = NULL, // return Domain's Dns name
  207. OUT BSTR* o_pbstrDomainDN = NULL, // return DC=nttest,DC=microsoft,DC=com
  208. OUT BSTR* o_pbstrLDAPDomainPath = NULL,// return LDAP://<DC>/<DomainDN>
  209. OUT BSTR* o_pbstrDomainGuid = NULL // return Domain's guid
  210. );
  211. HRESULT GetRootDomainName(
  212. IN LPCTSTR i_bstrDomainName,
  213. OUT BSTR* o_pbstrRootDomainName
  214. );
  215. void
  216. DebugOutLDAPError(
  217. IN PLDAP i_pldap,
  218. IN ULONG i_ulError,
  219. IN PCTSTR i_pszLDAPFunctionName
  220. );
  221. HRESULT ExtendDN
  222. (
  223. IN LPTSTR i_lpszCN,
  224. IN LPTSTR i_lpszDN,
  225. OUT BSTR *o_pbstrNewDN
  226. );
  227. HRESULT ExtendDNIfLongJunctionName
  228. (
  229. IN LPTSTR i_lpszJunctionName,
  230. IN LPCTSTR i_lpszBaseDN,
  231. OUT BSTR *o_pbstrNewDN
  232. );
  233. HRESULT GetJunctionPathPartitions
  234. (
  235. OUT PVOID *o_ppBuffer,
  236. OUT DWORD *o_pdwEntries,
  237. IN LPCTSTR i_pszJunctionPath
  238. );
  239. HRESULT CreateExtraNodesIfLongJunctionName
  240. (
  241. IN PLDAP i_pldap,
  242. IN LPCTSTR i_lpszJunctionName,
  243. IN LPCTSTR i_lpszBaseDN,
  244. IN LPCTSTR i_lpszObjClass
  245. );
  246. HRESULT DeleteExtraNodesIfLongJunctionName
  247. (
  248. IN PLDAP i_pldap,
  249. IN LPCTSTR i_lpszJunctionName,
  250. IN LPCTSTR i_lpszDN
  251. );
  252. HRESULT CreateObjectSimple
  253. (
  254. IN PLDAP i_pldap,
  255. IN LPCTSTR i_lpszDN,
  256. IN LPCTSTR i_lpszObjClass
  257. );
  258. HRESULT CreateObjectsRecursively
  259. (
  260. IN PLDAP i_pldap,
  261. IN BSTR i_bstrDN,
  262. IN UINT i_nLenPrefix,
  263. IN LPCTSTR i_lpszObjClass
  264. );
  265. HRESULT DeleteAncestorNodesIfEmpty
  266. (
  267. IN PLDAP i_pldap,
  268. IN LPCTSTR i_lpszDN,
  269. IN DWORD i_dwCount
  270. );
  271. // Replace all occurences of '\' with '|' in the given string.
  272. HRESULT ReplaceChar
  273. (
  274. IN OUT BSTR io_bstrString,
  275. IN TCHAR i_cOldChar,
  276. IN TCHAR i_cNewChar
  277. );
  278. HRESULT GetDfsLinkNameFromDN(
  279. IN BSTR i_bstrReplicaSetDN,
  280. OUT BSTR* o_pbstrDfsLinkName
  281. );
  282. HRESULT GetReplicaSetContainer(
  283. PLDAP i_pldap,
  284. BSTR i_bstrDfsName,
  285. BSTR* o_pbstrContainerDN
  286. );
  287. HRESULT GetSubscriberDN(
  288. IN BSTR i_bstrReplicaSetDN,
  289. IN BSTR i_bstrDomainGuid,
  290. IN BSTR i_bstrComputerDN,
  291. OUT BSTR* o_pbstrSubscriberDN
  292. );
  293. HRESULT CreateNtfrsMemberObject(
  294. IN PLDAP i_pldap,
  295. IN BSTR i_bstrMemberDN,
  296. IN BSTR i_bstrComputerDN,
  297. IN BSTR i_bstrDCofComputerObj
  298. );
  299. HRESULT CreateNtfrsSubscriberObject(
  300. IN PLDAP i_pldap,
  301. IN BSTR i_bstrSubscriberDN,
  302. IN BSTR i_bstrMemberDN,
  303. IN BSTR i_bstrRootPath,
  304. IN BSTR i_bstrStagingPath,
  305. IN BSTR i_bstrDC
  306. );
  307. HRESULT CreateNtdsConnectionObject(
  308. IN PLDAP i_pldap,
  309. IN BSTR i_bstrConnectionDN,
  310. IN BSTR i_bstrFromMemberDN,
  311. IN BOOL i_bEnable
  312. );
  313. HRESULT CreateNtfrsSettingsObjects(
  314. IN PLDAP i_pldap,
  315. IN BSTR i_bstrReplicaSetDN
  316. );
  317. HRESULT DeleteNtfrsReplicaSetObjectAndContainers(
  318. IN PLDAP i_pldap,
  319. IN BSTR i_bstrReplicaSetDN
  320. );
  321. HRESULT CreateNtfrsSubscriptionsObjects(
  322. IN PLDAP i_pldap,
  323. IN BSTR i_bstrSubscriberDN,
  324. IN BSTR i_bstrComputerDN
  325. );
  326. HRESULT DeleteNtfrsSubscriberObjectAndContainers(
  327. IN PLDAP i_pldap,
  328. IN BSTR i_bstrSubscriberDN,
  329. IN BSTR i_bstrComputerDN
  330. );
  331. HRESULT DeleteDSObjectsIfEmpty(
  332. IN PLDAP i_pldap,
  333. IN LPCTSTR i_lpszDN,
  334. IN int i_nPrefixLength
  335. );
  336. HRESULT SetConnectionSchedule(
  337. IN PLDAP i_pldap,
  338. IN BSTR i_bstrConnectionDN,
  339. IN SCHEDULE* i_pSchedule);
  340. HRESULT UuidToStructuredString(
  341. UUID* i_pUuid,
  342. BSTR* o_pbstr
  343. );
  344. HRESULT ScheduleToVariant(
  345. IN SCHEDULE* i_pSchedule,
  346. OUT VARIANT* o_pVar
  347. );
  348. HRESULT VariantToSchedule(
  349. IN VARIANT* i_pVar,
  350. OUT PSCHEDULE* o_ppSchedule
  351. );
  352. HRESULT CompareSchedules(
  353. IN SCHEDULE* i_pSchedule1,
  354. IN SCHEDULE* i_pSchedule2
  355. );
  356. HRESULT CopySchedule(
  357. IN SCHEDULE* i_pSrcSchedule,
  358. OUT PSCHEDULE* o_ppDstSchedule
  359. );
  360. HRESULT GetDefaultSchedule(
  361. OUT PSCHEDULE* o_ppSchedule
  362. );
  363. HRESULT GetSchemaVersion(IN PLDAP i_pldap);
  364. HRESULT GetSchemaVersionEx(
  365. IN BSTR i_bstrName,
  366. IN BOOL i_bServer = TRUE // TRUE if i_bstrName is a server, FALSE if i_bstrName is a domain
  367. );
  368. HRESULT LdapConnectToDC(IN LPCTSTR i_pszDC, OUT PLDAP* o_ppldap);
  369. HRESULT
  370. GetErrorMessage(
  371. IN DWORD i_dwError,
  372. OUT BSTR* o_pbstrErrorMsg
  373. );
  374. HRESULT
  375. FormatMessageString(
  376. OUT BSTR *o_pbstrMsg,
  377. IN DWORD dwErr,
  378. IN UINT iStringId,
  379. ...);
  380. HRESULT DsBindToDS(BSTR i_bstrDomain, BSTR *o_pbstrDC, HANDLE *o_phDS);
  381. #ifdef DEBUG
  382. void PrintTimeDelta(LPCTSTR pszMsg, SYSTEMTIME* pt0, SYSTEMTIME* pt1);
  383. #endif // DEBUG
  384. #endif //#ifndef _LDAPUTILS_H