ldapauth.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * ldapauth.cpp: Implements LDAP integration in the web server.
  43. *
  44. * Nitin More, John Kristian
  45. */
  46. /* #define DBG_PRINT */
  47. #include <stdio.h> /* for BUFSIZ */
  48. #include <string.h> /* for strncpy, strcat */
  49. #include <ldap.h>
  50. #include <prprf.h>
  51. #define DEFINE_LDAPU_STRINGS 1
  52. #include <ldaputil/certmap.h>
  53. #include <ldaputil/errors.h>
  54. #include <ldaputil/ldapauth.h>
  55. #include <ldaputili.h>
  56. #include "slapi-plugin.h"
  57. /*
  58. * ldapu_find
  59. * Description:
  60. * Caller should free res if it is not NULL.
  61. * Arguments:
  62. * ld Pointer to LDAP (assumes connection has been
  63. * established and the client has called the
  64. * appropriate bind routine)
  65. * base basedn (where to start the search)
  66. * scope scope for the search. One of
  67. * LDAP_SCOPE_SUBTREE, LDAP_SCOPE_ONELEVEL, and
  68. * LDAP_SCOPE_BASE
  69. * filter LDAP filter
  70. * attrs A NULL-terminated array of strings indicating which
  71. * attributes to return for each matching entry. Passing
  72. * NULL for this parameter causes all available
  73. * attributes to be retrieved.
  74. * attrsonly A boolean value that should be zero if both attribute
  75. * types and values are to be returned, non-zero if only
  76. * types are wanted.
  77. * res A result parameter which will contain the results of
  78. * the search upon completion of the call.
  79. * Return Values:
  80. * LDAPU_SUCCESS if entry is found
  81. * LDAPU_FAILED if entry is not found
  82. * <rv> if error, where <rv> can be passed to
  83. * ldap_err2string to get an error string.
  84. */
  85. int ldapu_find (LDAP *ld, const char *base, int scope,
  86. const char *filter, const char **attrs,
  87. int attrsonly, LDAPMessage **res)
  88. {
  89. int retval;
  90. #ifdef USE_THIS_CODE /* ASYNCHRONOUS */
  91. int msgid;
  92. #endif
  93. int numEntries;
  94. *res = 0;
  95. /* If base is NULL set it to null string */
  96. if (!base) {
  97. DBG_PRINT1("ldapu_find: basedn is missing -- assuming null string\n");
  98. base = "";
  99. }
  100. if (!filter || !*filter) {
  101. DBG_PRINT1("ldapu_find: filter is missing -- assuming objectclass=*\n");
  102. filter = ldapu_strings[LDAPU_STR_FILTER_DEFAULT];
  103. }
  104. DBG_PRINT2("\tbase:\t\"%s\"\n", base);
  105. DBG_PRINT2("\tfilter:\t\"%s\"\n", filter ? filter : "<NULL>");
  106. DBG_PRINT2("\tscope:\t\"%s\"\n",
  107. (scope == LDAP_SCOPE_SUBTREE ? "LDAP_SCOPE_SUBTREE"
  108. : (scope == LDAP_SCOPE_ONELEVEL ? "LDAP_SCOPE_ONELEVEL"
  109. : "LDAP_SCOPE_BASE")));
  110. retval = ldapu_search_s(ld, base, scope, filter, (char **)attrs,
  111. attrsonly, res);
  112. if (retval != LDAP_SUCCESS)
  113. {
  114. /* retval = ldap_result2error(ld, *res, 0); */
  115. DBG_PRINT2("ldapu_search_s: %s\n", ldapu_err2string(retval));
  116. return(retval);
  117. }
  118. numEntries = ldapu_count_entries(ld, *res);
  119. if (numEntries == 1) {
  120. /* success */
  121. return LDAPU_SUCCESS;
  122. }
  123. else if (numEntries == 0) {
  124. /* not found -- but not an error */
  125. DBG_PRINT1("ldapu_search_s: Entry not found\n");
  126. return LDAPU_FAILED;
  127. }
  128. else if (numEntries > 0) {
  129. /* Found more than one entry! */
  130. DBG_PRINT1("ldapu_search_s: Found more than one entry\n");
  131. return LDAPU_ERR_MULTIPLE_MATCHES;
  132. }
  133. else {
  134. /* should never get here */
  135. DBG_PRINT1("ldapu_search_s: should never reach here\n");
  136. ldapu_msgfree(ld, *res);
  137. return LDAP_OPERATIONS_ERROR;
  138. }
  139. }
  140. /* Search function for the cases where base = "" = NULL suffix, that is, search to
  141. * be performed on the entire DIT tree.
  142. * We actually do various searches taking a naming context at a time as the base for
  143. * the search. */
  144. int ldapu_find_entire_tree (LDAP *ld, int scope,
  145. const char *filter, const char **attrs,
  146. int attrsonly, LDAPMessage ***res)
  147. {
  148. int retval = LDAPU_FAILED;
  149. int rv,i, num_namingcontexts;
  150. LDAPMessage *result_entry, *result = NULL;
  151. const char *suffix_attr[2] = {"namingcontexts", NULL};
  152. /* these are private suffixes that may contain pseudo users
  153. e.g. replication manager that may have certs */
  154. int num_private_suffix = 1;
  155. const char *private_suffix_list[2] = {"cn=config", NULL};
  156. char **suffix_list, **suffix = NULL;
  157. rv = ldapu_find(ld, "",LDAP_SCOPE_BASE, "objectclass=*", suffix_attr, 0, &result);
  158. if (rv != LDAP_SUCCESS) {
  159. if (result) ldapu_msgfree(ld, result);
  160. return rv;
  161. }
  162. result_entry = ldapu_first_entry(ld, result);
  163. suffix = ldapu_get_values(ld, result_entry, suffix_attr[0]);
  164. suffix_list = suffix;
  165. num_namingcontexts = slapi_ldap_count_values(suffix);
  166. /* add private suffixes to our list of suffixes to search */
  167. if (num_private_suffix) {
  168. suffix_list = ldapu_realloc(suffix_list,
  169. sizeof(char *)*(num_namingcontexts+num_private_suffix+1));
  170. if (!suffix_list) {
  171. if (result) {
  172. ldapu_msgfree(ld, result);
  173. }
  174. retval = LDAPU_FAILED;
  175. return retval;
  176. }
  177. for (i = num_namingcontexts; i < (num_namingcontexts+num_private_suffix); ++i) {
  178. suffix_list[i] = strdup(private_suffix_list[i-num_namingcontexts]);
  179. }
  180. suffix_list[i] = NULL;
  181. num_namingcontexts += num_private_suffix;
  182. suffix = suffix_list;
  183. }
  184. if (result) ldapu_msgfree(ld, result);
  185. result = 0;
  186. i = 0;
  187. /* ugaston - the caller function must remember to free the memory allocated here */
  188. *res = (LDAPMessage **) ldapu_malloc((num_namingcontexts + 1) * sizeof(LDAPMessage *));
  189. while (suffix && *suffix) {
  190. rv = ldapu_find(ld, *suffix, scope, filter, attrs, attrsonly, &result);
  191. if (scope == LDAP_SCOPE_BASE && rv == LDAP_SUCCESS) {
  192. retval = rv;
  193. (*res)[i++] = result;
  194. break;
  195. }
  196. switch (rv) {
  197. case LDAP_SUCCESS:
  198. if (retval == LDAP_SUCCESS) {
  199. retval = LDAPU_ERR_MULTIPLE_MATCHES;
  200. (*res)[i++] = result;
  201. break;
  202. }
  203. case LDAPU_ERR_MULTIPLE_MATCHES:
  204. retval = rv;
  205. (*res)[i++] = result;
  206. break;
  207. default:
  208. if (retval != LDAP_SUCCESS && retval != LDAPU_ERR_MULTIPLE_MATCHES) {
  209. retval = rv;
  210. }
  211. if (result) ldapu_msgfree(ld, result);
  212. result = 0;
  213. break;
  214. }
  215. suffix++;
  216. }
  217. (*res)[i] = NULL;
  218. ldapu_value_free(ld, suffix_list);
  219. return retval;
  220. }