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.

182 lines
3.2 KiB

  1. /*--------------------------------------------------------------------------
  2. lt18.cpp
  3. ldap rfc1823 test
  4. Copyright (C) 1996 Microsoft Corporation
  5. All rights reserved.
  6. Authors:
  7. davidsan Dave Sanderman
  8. History:
  9. 06/17/96 davidsan Created.
  10. --------------------------------------------------------------------------*/
  11. #include "lt18.h"
  12. LDAP *g_pldap = NULL;
  13. BOOL
  14. FInit()
  15. {
  16. return TRUE;
  17. }
  18. BOOL
  19. FConnect(char *szServer)
  20. {
  21. int iRet;
  22. g_pldap = ldap_open(szServer, IPPORT_LDAP);
  23. if (!g_pldap)
  24. {
  25. printf("Couldn't ldap_open\n");
  26. return FALSE;
  27. }
  28. iRet = ldap_bind_s(g_pldap, "[email protected]", "test", LDAP_AUTH_SIMPLE);
  29. if (iRet != LDAP_SUCCESS)
  30. {
  31. printf("Couldn't ldap_bind: %d\n", iRet);
  32. return FALSE;
  33. }
  34. return TRUE;
  35. }
  36. char *g_rgszAttrib[] = {"title", "sn", "objectClass", "krbName", NULL};
  37. BOOL
  38. FSearch(char *szSearch)
  39. {
  40. int iRet;
  41. struct timeval timeout;
  42. LDAPMessage *res;
  43. LDAPMessage *entry;
  44. void *pv;
  45. char **rgsz;
  46. char **psz;
  47. BERVAL **rgpberval;
  48. BERVAL **ppberval;
  49. g_pldap->ld_deref = LDAP_DEREF_ALWAYS;
  50. g_pldap->ld_sizelimit = 100;
  51. g_pldap->ld_timelimit = 0;
  52. timeout.tv_usec = 0;
  53. timeout.tv_sec = 60 * 60;
  54. iRet = ldap_search_st(g_pldap,
  55. "c=us",
  56. LDAP_SCOPE_SUBTREE,
  57. szSearch,
  58. g_rgszAttrib,
  59. FALSE,
  60. &timeout,
  61. &res);
  62. if (iRet != LDAP_SUCCESS)
  63. {
  64. printf("Couldn't ldap_search_st: %d\n", iRet);
  65. ldap_msgfree(res);
  66. return FALSE;
  67. }
  68. entry = ldap_first_entry(g_pldap, res);
  69. if (!entry)
  70. {
  71. printf("No first entry.\n");
  72. ldap_msgfree(res);
  73. return FALSE;
  74. }
  75. while (entry)
  76. {
  77. char *szDN = ldap_get_dn(g_pldap, entry);
  78. printf("DN: %s\n", szDN);
  79. ldap_free_dn(szDN);
  80. char *szAttr = ldap_first_attribute(g_pldap, entry, &pv);
  81. while (szAttr)
  82. {
  83. printf("attr: %s\n", szAttr);
  84. rgsz = ldap_get_values(g_pldap, entry, szAttr);
  85. if (!rgsz)
  86. {
  87. printf(" no values!\n");
  88. }
  89. else
  90. {
  91. printf(" %d values:\n", ldap_count_values(rgsz));
  92. psz = rgsz;
  93. while (*psz)
  94. {
  95. printf(" val: %s\n", *psz);
  96. psz++;
  97. }
  98. ldap_value_free(rgsz);
  99. }
  100. rgpberval = ldap_get_values_len(g_pldap, entry, szAttr);
  101. if (!rgpberval)
  102. {
  103. printf(" no values_len!\n");
  104. }
  105. else
  106. {
  107. printf(" %d values_len:\n", ldap_count_values_len(rgpberval));
  108. ppberval = rgpberval;
  109. while (*ppberval)
  110. {
  111. printf(" val: %s (len %d)\n", (*ppberval)->bv_val, (*ppberval)->bv_len);
  112. ppberval++;
  113. }
  114. ldap_value_free_len(rgpberval);
  115. }
  116. szAttr = ldap_next_attribute(g_pldap, entry, &pv);
  117. }
  118. entry = ldap_next_entry(g_pldap, entry);
  119. }
  120. ldap_msgfree(res);
  121. return TRUE;
  122. }
  123. BOOL
  124. FTerm()
  125. {
  126. if (g_pldap)
  127. ldap_unbind(g_pldap);
  128. g_pldap = NULL;
  129. return TRUE;
  130. }
  131. void
  132. usage()
  133. {
  134. printf("usage: lt18 <server> <search string>\n");
  135. exit(1);
  136. }
  137. void __cdecl
  138. main(int argc, char **argv)
  139. {
  140. if (argc < 3)
  141. usage();
  142. if (!FInit())
  143. exit(1);
  144. if (!FConnect(argv[1]))
  145. exit(1);
  146. if (!FSearch(argv[2]))
  147. exit(1);
  148. FTerm();
  149. exit(0);
  150. }