ldapauth.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /*
  13. * ldapauth.cpp: Implements LDAP integration in the web server.
  14. *
  15. * Nitin More, John Kristian
  16. */
  17. /* #define DBG_PRINT */
  18. #include <stdio.h> /* for BUFSIZ */
  19. #include <string.h> /* for strncpy, strcat */
  20. #include <ldap.h>
  21. #include <prprf.h>
  22. #define DEFINE_LDAPU_STRINGS 1
  23. #include <ldaputil/certmap.h>
  24. #include <ldaputil/errors.h>
  25. #include <ldaputil/ldapauth.h>
  26. #include <ldaputili.h>
  27. #include "slapi-plugin.h"
  28. /*
  29. * ldapu_find
  30. * Description:
  31. * Caller should free res if it is not NULL.
  32. * Arguments:
  33. * ld Pointer to LDAP (assumes connection has been
  34. * established and the client has called the
  35. * appropriate bind routine)
  36. * base basedn (where to start the search)
  37. * scope scope for the search. One of
  38. * LDAP_SCOPE_SUBTREE, LDAP_SCOPE_ONELEVEL, and
  39. * LDAP_SCOPE_BASE
  40. * filter LDAP filter
  41. * attrs A NULL-terminated array of strings indicating which
  42. * attributes to return for each matching entry. Passing
  43. * NULL for this parameter causes all available
  44. * attributes to be retrieved.
  45. * attrsonly A boolean value that should be zero if both attribute
  46. * types and values are to be returned, non-zero if only
  47. * types are wanted.
  48. * res A result parameter which will contain the results of
  49. * the search upon completion of the call.
  50. * Return Values:
  51. * LDAPU_SUCCESS if entry is found
  52. * LDAPU_FAILED if entry is not found
  53. * <rv> if error, where <rv> can be passed to
  54. * ldap_err2string to get an error string.
  55. */
  56. int
  57. ldapu_find(LDAP *ld, const char *base, int scope, const char *filter, const char **attrs, int attrsonly, LDAPMessage **res)
  58. {
  59. int retval;
  60. #ifdef USE_THIS_CODE /* ASYNCHRONOUS */
  61. int msgid;
  62. #endif
  63. int numEntries;
  64. *res = 0;
  65. /* If base is NULL set it to null string */
  66. if (!base) {
  67. DBG_PRINT1("ldapu_find: basedn is missing -- assuming null string\n");
  68. base = "";
  69. }
  70. if (!filter || !*filter) {
  71. DBG_PRINT1("ldapu_find: filter is missing -- assuming objectclass=*\n");
  72. filter = ldapu_strings[LDAPU_STR_FILTER_DEFAULT];
  73. }
  74. DBG_PRINT2("\tbase:\t\"%s\"\n", base);
  75. DBG_PRINT2("\tfilter:\t\"%s\"\n", filter ? filter : "<NULL>");
  76. DBG_PRINT2("\tscope:\t\"%s\"\n",
  77. (scope == LDAP_SCOPE_SUBTREE ? "LDAP_SCOPE_SUBTREE"
  78. : (scope == LDAP_SCOPE_ONELEVEL ? "LDAP_SCOPE_ONELEVEL"
  79. : "LDAP_SCOPE_BASE")));
  80. retval = ldapu_search_s(ld, base, scope, filter, (char **)attrs,
  81. attrsonly, res);
  82. if (retval != LDAP_SUCCESS) {
  83. /* retval = ldap_result2error(ld, *res, 0); */
  84. DBG_PRINT2("ldapu_search_s: %s\n", ldapu_err2string(retval));
  85. return (retval);
  86. }
  87. numEntries = ldapu_count_entries(ld, *res);
  88. if (numEntries == 1) {
  89. /* success */
  90. return LDAPU_SUCCESS;
  91. } else if (numEntries == 0) {
  92. /* not found -- but not an error */
  93. DBG_PRINT1("ldapu_search_s: Entry not found\n");
  94. return LDAPU_FAILED;
  95. } else if (numEntries > 0) {
  96. /* Found more than one entry! */
  97. DBG_PRINT1("ldapu_search_s: Found more than one entry\n");
  98. return LDAPU_ERR_MULTIPLE_MATCHES;
  99. } else {
  100. /* should never get here */
  101. DBG_PRINT1("ldapu_search_s: should never reach here\n");
  102. ldapu_msgfree(ld, *res);
  103. return LDAP_OPERATIONS_ERROR;
  104. }
  105. }
  106. /* Search function for the cases where base = "" = NULL suffix, that is, search to
  107. * be performed on the entire DIT tree.
  108. * We actually do various searches taking a naming context at a time as the base for
  109. * the search. */
  110. int
  111. ldapu_find_entire_tree(LDAP *ld, int scope, const char *filter, const char **attrs, int attrsonly, LDAPMessage ***res)
  112. {
  113. int retval = LDAPU_FAILED;
  114. int rv, i, num_namingcontexts;
  115. LDAPMessage *result_entry, *result = NULL;
  116. const char *suffix_attr[2] = {"namingcontexts", NULL};
  117. /* these are private suffixes that may contain pseudo users
  118. e.g. replication manager that may have certs */
  119. int num_private_suffix = 1;
  120. const char *private_suffix_list[2] = {"cn=config", NULL};
  121. char **suffix_list, **suffix = NULL;
  122. rv = ldapu_find(ld, "", LDAP_SCOPE_BASE, "objectclass=*", suffix_attr, 0, &result);
  123. if (rv != LDAP_SUCCESS) {
  124. if (result)
  125. ldapu_msgfree(ld, result);
  126. return rv;
  127. }
  128. result_entry = ldapu_first_entry(ld, result);
  129. suffix = ldapu_get_values(ld, result_entry, suffix_attr[0]);
  130. suffix_list = suffix;
  131. num_namingcontexts = slapi_ldap_count_values(suffix);
  132. /* add private suffixes to our list of suffixes to search */
  133. if (num_private_suffix) {
  134. suffix_list = ldapu_realloc(suffix_list,
  135. sizeof(char *) * (num_namingcontexts + num_private_suffix + 1));
  136. if (!suffix_list) {
  137. if (result) {
  138. ldapu_msgfree(ld, result);
  139. }
  140. retval = LDAPU_FAILED;
  141. return retval;
  142. }
  143. for (i = num_namingcontexts; i < (num_namingcontexts + num_private_suffix); ++i) {
  144. suffix_list[i] = strdup(private_suffix_list[i - num_namingcontexts]);
  145. }
  146. suffix_list[i] = NULL;
  147. num_namingcontexts += num_private_suffix;
  148. suffix = suffix_list;
  149. }
  150. if (result)
  151. ldapu_msgfree(ld, result);
  152. result = 0;
  153. i = 0;
  154. /* ugaston - the caller function must remember to free the memory allocated here */
  155. *res = (LDAPMessage **)ldapu_malloc((num_namingcontexts + 1) * sizeof(LDAPMessage *));
  156. while (suffix && *suffix) {
  157. rv = ldapu_find(ld, *suffix, scope, filter, attrs, attrsonly, &result);
  158. if (scope == LDAP_SCOPE_BASE && rv == LDAP_SUCCESS) {
  159. retval = rv;
  160. (*res)[i++] = result;
  161. break;
  162. }
  163. switch (rv) {
  164. case LDAP_SUCCESS:
  165. if (retval == LDAP_SUCCESS) {
  166. retval = LDAPU_ERR_MULTIPLE_MATCHES;
  167. (*res)[i++] = result;
  168. break;
  169. }
  170. /* FALLTHROUGH */
  171. case LDAPU_ERR_MULTIPLE_MATCHES:
  172. retval = rv;
  173. (*res)[i++] = result;
  174. break;
  175. default:
  176. if (retval != LDAP_SUCCESS && retval != LDAPU_ERR_MULTIPLE_MATCHES) {
  177. retval = rv;
  178. }
  179. if (result)
  180. ldapu_msgfree(ld, result);
  181. result = 0;
  182. break;
  183. }
  184. suffix++;
  185. }
  186. (*res)[i] = NULL;
  187. ldapu_value_free(ld, suffix_list);
  188. return retval;
  189. }