LDAP.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ldap.h>
  5. #define LDAP_URI "ldap://ldap.technikum-wien.at:389"
  6. #define SEARCHBASE "dc=technikum-wien,dc=at"
  7. #define SCOPE LDAP_SCOPE_SUBTREE
  8. #define FILTER "(uid=if17b*)"
  9. static int ldapTryLoginIntern(char* user, char* password)
  10. {
  11. LDAP *ld; // LDAP resource handle
  12. BerValue *servercredp;
  13. BerValue cred;
  14. cred.bv_val = password;
  15. cred.bv_len = strlen(password);
  16. int rc = 0;
  17. int ldapversion = LDAP_VERSION3;
  18. /* setup LDAP connection */
  19. if(ldap_initialize(&ld, LDAP_URI) != LDAP_SUCCESS)
  20. {
  21. fprintf(stderr, "ldap_init failed");
  22. return 0;
  23. }
  24. if((rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldapversion)) != LDAP_SUCCESS)
  25. {
  26. fprintf(stderr, "ldap_set_option(PROTOCOL_VERSION): %s\n", ldap_err2string(rc));
  27. ldap_unbind_ext_s(ld, NULL, NULL);
  28. return 0;
  29. }
  30. if((rc = ldap_start_tls_s(ld, NULL, NULL)) != LDAP_SUCCESS)
  31. {
  32. fprintf(stderr, "ldap_start_tls_s(): %s\n", ldap_err2string(rc));
  33. ldap_unbind_ext_s(ld, NULL, NULL);
  34. return 0;
  35. }
  36. rc = ldap_sasl_bind_s(ld, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, &servercredp);
  37. if(rc != LDAP_SUCCESS)
  38. {
  39. return 0;
  40. }
  41. ldap_unbind_ext_s(ld, NULL, NULL);
  42. return 1;
  43. }
  44. int ldapTryLogin(char* user, char* password)
  45. {
  46. LDAP *ld; // LDAP resource handle
  47. LDAPMessage *result, *e; // LDAP result handle
  48. BerElement *ber; // array of attributes
  49. char *attribute;
  50. BerValue **vals;
  51. BerValue *servercredp;
  52. BerValue cred;
  53. cred.bv_val = "";
  54. cred.bv_len = 0;
  55. int i, rc = 0;
  56. const char *attribs[] = {"uid", "cn", NULL}; /* attribute array for search */
  57. int ldapversion = LDAP_VERSION3;
  58. /* setup LDAP connection */
  59. if(ldap_initialize(&ld, LDAP_URI) != LDAP_SUCCESS)
  60. {
  61. fprintf(stderr, "ldap_init failed");
  62. return 0;
  63. }
  64. if((rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldapversion)) != LDAP_SUCCESS)
  65. {
  66. fprintf(stderr, "ldap_set_option(PROTOCOL_VERSION): %s\n", ldap_err2string(rc));
  67. ldap_unbind_ext_s(ld, NULL, NULL);
  68. return 0;
  69. }
  70. if((rc = ldap_start_tls_s(ld, NULL, NULL)) != LDAP_SUCCESS)
  71. {
  72. fprintf(stderr, "ldap_start_tls_s(): %s\n", ldap_err2string(rc));
  73. ldap_unbind_ext_s(ld, NULL, NULL);
  74. return 0;
  75. }
  76. // anonymous bind
  77. rc = ldap_sasl_bind_s(ld, "", LDAP_SASL_SIMPLE, &cred, NULL, NULL, &servercredp);
  78. if(rc != LDAP_SUCCESS)
  79. {
  80. fprintf(stderr, "LDAP bind error: %s\n", ldap_err2string(rc));
  81. ldap_unbind_ext_s(ld, NULL, NULL);
  82. return 0;
  83. }
  84. /* perform ldap search */
  85. rc = ldap_search_ext_s(ld, SEARCHBASE, SCOPE, FILTER, (char **) attribs, 0, NULL, NULL, NULL, 500, &result);
  86. if(rc != LDAP_SUCCESS)
  87. {
  88. fprintf(stderr, "LDAP search error: %s\n", ldap_err2string(rc));
  89. ldap_unbind_ext_s(ld, NULL, NULL);
  90. return 0;
  91. }
  92. char buffer[1024];
  93. buffer[0] = '\0';
  94. for(e = ldap_first_entry(ld, result); e != NULL; e = ldap_next_entry(ld, e))
  95. {
  96. for(attribute = ldap_first_attribute(ld, e, &ber); attribute != NULL; attribute = ldap_next_attribute(ld, e, ber))
  97. {
  98. if((vals = ldap_get_values_len(ld, e, attribute)) != NULL)
  99. {
  100. for(i = 0; i < ldap_count_values_len(vals); i++)
  101. {
  102. if(strcmp(attribute, "uid") == 0 && strcmp(vals[i]->bv_val, user) == 0)
  103. {
  104. strncpy(buffer, ldap_get_dn(ld, e), 1023);
  105. }
  106. }
  107. ldap_value_free_len(vals);
  108. }
  109. // free memory used to store the attribute
  110. ldap_memfree(attribute);
  111. }
  112. // free memory used to store the value structure
  113. if(ber != NULL)
  114. {
  115. ber_free(ber, 0);
  116. }
  117. }
  118. /* free memory used for result */
  119. ldap_msgfree(result);
  120. printf("LDAP search suceeded\n");
  121. ldap_unbind_ext_s(ld, NULL, NULL);
  122. if(strlen(buffer) == 0)
  123. {
  124. return 0;
  125. }
  126. return ldapTryLoginIntern(buffer, password);
  127. }