pw_verify.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2016 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. /*
  9. * pw_verify.c
  10. *
  11. * This contains helpers that take a DN and a password credential from a simple
  12. * bind or SASL PLAIN/LOGIN. It steps through the raw credential and returns
  13. *
  14. * SLAPI_BIND_SUCCESS : The credentials are correct for the DN.
  15. * SLAPI_BIND_ANONYMOUS : The credentials are anonymous.
  16. * SLAPI_BIND_REFERRAL : The DN provided is going to be a referal, go away!
  17. * LDAP_INVALID_CREDENTIALS : The credentials are incorrect for this DN, or not
  18. * enough material was provided.
  19. * LDAP_OPERATIONS_ERROR : Something went wrong during verification.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include "slap.h"
  25. #include "fe.h"
  26. int
  27. pw_verify_root_dn(const char *dn, const Slapi_Value *cred)
  28. {
  29. int result = LDAP_OPERATIONS_ERROR;
  30. char *root_pw = config_get_rootpw();
  31. if (root_pw != NULL && slapi_dn_isroot(dn)) {
  32. /* Now build a slapi value to give to slapi_pw_find_sv */
  33. Slapi_Value root_dn_pw_bval;
  34. slapi_value_init_string(&root_dn_pw_bval, root_pw);
  35. Slapi_Value *root_dn_pw_vals[] = {&root_dn_pw_bval, NULL};
  36. result = slapi_pw_find_sv(root_dn_pw_vals, cred);
  37. value_done(&root_dn_pw_bval);
  38. }
  39. slapi_ch_free_string(&root_pw);
  40. return result;
  41. }
  42. /*
  43. * This will work out which backend is needed, and then work from there.
  44. * You must set the SLAPI_BIND_TARGET_SDN, and SLAPI_BIND_CREDENTIALS to
  45. * the pblock for this to operate correctly.
  46. *
  47. * In the future, this will use the credentials and do mfa.
  48. *
  49. * All other results, it's already released.
  50. */
  51. int
  52. pw_verify_be_dn(Slapi_PBlock *pb, Slapi_Entry **referral)
  53. {
  54. int rc = 0;
  55. Slapi_Backend *be = NULL;
  56. if (slapi_mapping_tree_select(pb, &be, referral, NULL, 0) != LDAP_SUCCESS) {
  57. return SLAPI_BIND_NO_BACKEND;
  58. }
  59. if (*referral) {
  60. slapi_be_Unlock(be);
  61. return SLAPI_BIND_REFERRAL;
  62. }
  63. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  64. /* Put the credentials into the pb */
  65. if (be->be_bind == NULL) {
  66. /* Selected backend doesn't support binds! */
  67. slapi_be_Unlock(be);
  68. return LDAP_OPERATIONS_ERROR;
  69. }
  70. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  71. /* Make sure the result handlers are setup */
  72. set_db_default_result_handlers(pb);
  73. /* now take the dn, and check it */
  74. rc = (*be->be_bind)(pb);
  75. slapi_be_Unlock(be);
  76. return rc;
  77. }
  78. /*
  79. * Resolve the dn we have been requested to bind with and verify it's
  80. * valid, and has a backend.
  81. *
  82. * We are checking:
  83. * * is this anonymous?
  84. * * is this the rootdn?
  85. * * is this a real dn, which associates to a real backend.
  86. *
  87. * This is used in SASL autobinds, so we need to handle this validation.
  88. */
  89. int
  90. pw_validate_be_dn(Slapi_PBlock *pb, Slapi_Entry **referral)
  91. {
  92. Slapi_Backend *be = NULL;
  93. Slapi_DN *pb_sdn;
  94. struct berval *cred;
  95. ber_tag_t method;
  96. slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &pb_sdn);
  97. slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &cred);
  98. slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method);
  99. if (pb_sdn != NULL || cred != NULL) {
  100. return LDAP_OPERATIONS_ERROR;
  101. }
  102. if (*referral) {
  103. return SLAPI_BIND_REFERRAL;
  104. }
  105. /* We need a slapi_sdn_isanon? */
  106. if (method == LDAP_AUTH_SIMPLE && (cred == NULL || cred->bv_len == 0)) {
  107. return SLAPI_BIND_ANONYMOUS;
  108. }
  109. if (slapi_sdn_isroot(pb_sdn)) {
  110. /* This is a real identity */
  111. return SLAPI_BIND_SUCCESS;
  112. }
  113. if (slapi_mapping_tree_select(pb, &be, referral, NULL, 0) != LDAP_SUCCESS) {
  114. return SLAPI_BIND_NO_BACKEND;
  115. }
  116. slapi_be_Unlock(be);
  117. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  118. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  119. /* Make sure the result handlers are setup */
  120. set_db_default_result_handlers(pb);
  121. /* The backend associated with this identity is real. */
  122. return SLAPI_BIND_SUCCESS;
  123. }