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.

322 lines
6.3 KiB

  1. /*--------------------------------------------------------------------------
  2. lt.cpp
  3. ldap test
  4. Copyright (C) 1996 Microsoft Corporation
  5. All rights reserved.
  6. Authors:
  7. davidsan Dave Sanderman
  8. History:
  9. 04/25/96 davidsan Created.
  10. --------------------------------------------------------------------------*/
  11. #include "lt.h"
  12. PLCLI g_plcli = NULL;
  13. BOOL
  14. FInit()
  15. {
  16. return TRUE;
  17. }
  18. BOOL
  19. FConnect(char *szServer)
  20. {
  21. HRESULT hr;
  22. XID xid;
  23. hr = HrCreateLdapClient(LDAP_VER_CURRENT, INTERFACE_VER_CURRENT, &g_plcli);
  24. if (FAILED(hr))
  25. {
  26. printf("HrCreateLdapClient returned %08X\n", hr);
  27. return FALSE;
  28. }
  29. hr = g_plcli->HrConnect(szServer, IPPORT_LDAP);
  30. if (FAILED(hr))
  31. {
  32. printf("HrConnect returned %08X\n", hr);
  33. return FALSE;
  34. }
  35. hr = g_plcli->HrBindSimple("cn=alex weinart, c=us", NULL, &xid);
  36. if (FAILED(hr))
  37. {
  38. printf("HrBindSimple returned %08X\n", hr);
  39. return FALSE;
  40. }
  41. hr = g_plcli->HrGetBindResponse(xid, INFINITE);
  42. if (FAILED(hr))
  43. {
  44. printf("HrBindSimple returned %08X\n", hr);
  45. return FALSE;
  46. }
  47. return TRUE;
  48. }
  49. char *g_rgszAttrib[] = {"title", "sn", "objectClass", "krbName"};
  50. BOOL
  51. FSearch()
  52. {
  53. HRESULT hr;
  54. XID xid;
  55. POBJ pobj;
  56. POBJ pobjT;
  57. PATTR pattr;
  58. PVAL pval;
  59. SP sp;
  60. FILTER filter;
  61. FILTER filterS1;
  62. FILTER filterS2;
  63. FILTER filterSS1;
  64. FILTER filterSS2;
  65. FILTER filterSS3;
  66. #ifdef OLDSEARCH
  67. filter.type = LDAP_FILTER_AND;
  68. filter.pfilterSub = &filterS1;
  69. filterS1.type = LDAP_FILTER_OR;
  70. filterS1.pfilterSub = &filterSS1;
  71. filterS1.pfilterNext = &filterS2;
  72. filterSS1.type = LDAP_FILTER_EQUALITY;
  73. filterSS1.ava.szAttrib = "cn";
  74. filterSS1.ava.szValue = "sander";
  75. filterSS1.pfilterNext = &filterSS2;
  76. filterSS2.type = LDAP_FILTER_EQUALITY;
  77. filterSS2.ava.szAttrib = "sn";
  78. filterSS2.ava.szValue = "sander";
  79. filterSS2.pfilterNext = &filterSS3;
  80. filterSS3.type = LDAP_FILTER_EQUALITY;
  81. filterSS3.ava.szAttrib = "uid";
  82. filterSS3.ava.szValue = "sander";
  83. filterSS3.pfilterNext = NULL;
  84. filterS2.type = LDAP_FILTER_EQUALITY;
  85. filterS2.ava.szAttrib = "objectClass";
  86. filterS2.ava.szValue = "Person";
  87. filterS2.pfilterNext = NULL;
  88. #else
  89. filter.type = LDAP_FILTER_SUBSTRINGS;
  90. filter.sub.szAttrib = "cn";
  91. filter.sub.szInitial = "davidsan";
  92. filter.sub.szAny = NULL;
  93. filter.sub.szFinal = NULL;
  94. // filter.type = LDAP_FILTER_EQUALITY;
  95. // filter.ava.szAttrib = "cn";
  96. // filter.ava.szValue = "davidsan4";
  97. #endif
  98. sp.szDNBase = "c=US";
  99. sp.scope = LDAP_SCOPE_SUBTREE;
  100. sp.deref = LDAP_DEREF_ALWAYS;
  101. sp.cRecordsMax = 0;
  102. sp.cSecondsMax = 0;
  103. sp.fAttrsOnly = FALSE;
  104. sp.pfilter = &filter;
  105. sp.cAttrib = 4;
  106. sp.rgszAttrib = g_rgszAttrib;
  107. hr = g_plcli->HrSearch(&sp, &xid);
  108. if (FAILED(hr))
  109. {
  110. printf("HrSearch returned %08X\n", hr);
  111. return FALSE;
  112. }
  113. hr = g_plcli->HrGetSearchResponse(xid, INFINITE, &pobj);
  114. if (FAILED(hr))
  115. {
  116. printf("HrGetSearchResponse returned %08X\n", hr);
  117. return FALSE;
  118. }
  119. pobjT = pobj;
  120. while (pobjT)
  121. {
  122. printf("OBJ: %s\n", pobjT->szDN);
  123. pattr = pobjT->pattrFirst;
  124. while (pattr)
  125. {
  126. printf(" ATTR: %s\n", pattr->szAttrib);
  127. pval = pattr->pvalFirst;
  128. while (pval)
  129. {
  130. printf(" VAL: %s\n", pval->szVal);
  131. pval = pval->pvalNext;
  132. }
  133. pattr = pattr->pattrNext;
  134. }
  135. pobjT = pobjT->pobjNext;
  136. }
  137. HrFreePobjList(pobj);
  138. return TRUE;
  139. }
  140. void
  141. SetPattr(PATTR pattr, PATTR pattrNext, char *szAttrib, PVAL pval)
  142. {
  143. pattr->pattrNext = pattrNext;
  144. pattr->szAttrib = szAttrib;
  145. pattr->pvalFirst = pval;
  146. }
  147. void
  148. SetPval(PVAL pval, PVAL pvalNext, char *szVal)
  149. {
  150. pval->pvalNext = pvalNext;
  151. pval->szVal = szVal;
  152. }
  153. BOOL
  154. FTestOtherStuff()
  155. {
  156. HRESULT hr;
  157. XID xid;
  158. hr = g_plcli->HrDelete("c=us, o=my pants, cn=davidsan", &xid);
  159. if (FAILED(hr))
  160. {
  161. printf("HrDelete returned %08X\n", hr);
  162. return FALSE;
  163. }
  164. hr = g_plcli->HrGetDeleteResponse(xid, INFINITE);
  165. if (FAILED(hr))
  166. {
  167. printf("HrGetDeleteResponse returned %08X\n", hr);
  168. }
  169. hr = g_plcli->HrDelete("alex", &xid);
  170. if (FAILED(hr))
  171. {
  172. printf("HrDelete returned %08X\n", hr);
  173. return FALSE;
  174. }
  175. hr = g_plcli->HrGetDeleteResponse(xid, INFINITE);
  176. if (FAILED(hr))
  177. {
  178. printf("HrGetDeleteResponse returned %08X\n", hr);
  179. }
  180. hr = g_plcli->HrCompare("c=us, [email protected]", "URL", "ftp://ftp.netcom.com/pub/sa/sandmann", &xid);
  181. if (FAILED(hr))
  182. {
  183. printf("HrCompare returned %08X\n", hr);
  184. return FALSE;
  185. }
  186. hr = g_plcli->HrGetCompareResponse(xid, INFINITE);
  187. if (FAILED(hr))
  188. {
  189. printf("HrGetCompareResponse returned %08X\n", hr);
  190. }
  191. hr = g_plcli->HrModifyRDN("foo", "bar", TRUE, &xid);
  192. if (FAILED(hr))
  193. {
  194. printf("HrModifyRDN returned %08X\n", hr);
  195. return FALSE;
  196. }
  197. hr = g_plcli->HrGetModifyRDNResponse(xid, INFINITE);
  198. if (FAILED(hr))
  199. {
  200. printf("HrGetModifyRDNResponse returned %08X\n", hr);
  201. }
  202. hr = g_plcli->HrModifyRDN("c=us, cn=alex weinart", "bar", TRUE, &xid);
  203. if (FAILED(hr))
  204. {
  205. printf("HrModifyRDN returned %08X\n", hr);
  206. return FALSE;
  207. }
  208. hr = g_plcli->HrGetModifyRDNResponse(xid, INFINITE);
  209. if (FAILED(hr))
  210. {
  211. printf("HrGetModifyRDNResponse returned %08X\n", hr);
  212. }
  213. // test add: add an obj with dn "c=us, cn=davidsan" and some other props
  214. ATTR attr1;
  215. VAL val1;
  216. ATTR attr2;
  217. VAL val2;
  218. ATTR attr3;
  219. VAL val3;
  220. SetPattr(&attr1, &attr2, "uid", &val1);
  221. SetPval(&val1, NULL, "davidsan");
  222. SetPattr(&attr2, &attr3, "sn", &val2);
  223. SetPval(&val2, NULL, "sanderman");
  224. SetPattr(&attr3, NULL, "st", &val3);
  225. SetPval(&val3, NULL, "wa");
  226. hr = g_plcli->HrAdd("c=us, cn=davidsan", &attr1, &xid);
  227. if (FAILED(hr))
  228. {
  229. printf("HrAdd returned %08X\n", hr);
  230. return FALSE;
  231. }
  232. hr = g_plcli->HrGetAddResponse(xid, INFINITE);
  233. if (FAILED(hr))
  234. {
  235. printf("HrGetAddResponse returned %08X\n", hr);
  236. }
  237. return TRUE;
  238. }
  239. BOOL
  240. FTerm()
  241. {
  242. if (g_plcli)
  243. {
  244. g_plcli->HrUnbind();
  245. if (g_plcli->HrIsConnected() == NOERROR)
  246. g_plcli->HrDisconnect();
  247. g_plcli->Release();
  248. }
  249. return TRUE;
  250. }
  251. void
  252. usage()
  253. {
  254. printf("usage: lt <server>\n");
  255. exit(1);
  256. }
  257. void __cdecl
  258. main(int argc, char **argv)
  259. {
  260. if (argc < 2)
  261. usage();
  262. if (!FInit())
  263. exit(1);
  264. if (!FConnect(argv[1]))
  265. exit(1);
  266. if (!FSearch())
  267. exit(1);
  268. // FTestOtherStuff();
  269. FTerm();
  270. exit(0);
  271. }