ptbind.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * ptbind.c - LDAP bind-related code for Pass Through Authentication
  43. *
  44. */
  45. #include "passthru.h"
  46. static int
  47. passthru_simple_bind_once_s( PassThruServer *srvr, char *dn,
  48. struct berval *creds, LDAPControl **reqctrls, int *lderrnop,
  49. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  50. LDAPControl ***resctrlsp );
  51. /*
  52. * Attempt to chain a bind request off to "srvr." We return an LDAP error
  53. * code that indicates whether we successfully got a response from the
  54. * other server or not. If we succeed, we return LDAP_SUCCESS and *lderrnop
  55. * is set to the result code from the remote server.
  56. *
  57. * Note that in the face of "ldap server down" or "ldap connect failed" errors
  58. * we make up to "tries" attempts to bind to the remote server. Since we
  59. * are only interested in recovering silently when the remote server is up
  60. * but decided to close our connection, we retry without pausing between
  61. * attempts.
  62. */
  63. int
  64. passthru_simple_bind_s( Slapi_PBlock *pb, PassThruServer *srvr, int tries,
  65. char *dn, struct berval *creds, LDAPControl **reqctrls, int *lderrnop,
  66. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  67. LDAPControl ***resctrlsp )
  68. {
  69. int rc;
  70. PASSTHRU_ASSERT( srvr != NULL );
  71. PASSTHRU_ASSERT( tries > 0 );
  72. PASSTHRU_ASSERT( creds != NULL );
  73. PASSTHRU_ASSERT( lderrnop != NULL );
  74. PASSTHRU_ASSERT( refurlsp != NULL );
  75. do {
  76. /*
  77. * check to see if operation has been abandoned...
  78. */
  79. if ( slapi_op_abandoned( pb )) {
  80. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  81. "operation abandoned\n" );
  82. rc = LDAP_USER_CANCELLED;
  83. } else {
  84. rc = passthru_simple_bind_once_s( srvr, dn, creds, reqctrls,
  85. lderrnop, matcheddnp, errmsgp, refurlsp, resctrlsp );
  86. }
  87. } while ( PASSTHRU_LDAP_CONN_ERROR( rc ) && --tries > 0 );
  88. return( rc );
  89. }
  90. /*
  91. * like passthru_simple_bind_s() but only makes one attempt.
  92. */
  93. static int
  94. passthru_simple_bind_once_s( PassThruServer *srvr, char *dn,
  95. struct berval *creds, LDAPControl **reqctrls, int *lderrnop,
  96. char **matcheddnp, char **errmsgp, struct berval ***refurlsp,
  97. LDAPControl ***resctrlsp )
  98. {
  99. int rc, msgid;
  100. char **referrals;
  101. struct timeval tv, *timeout;
  102. LDAPMessage *result;
  103. LDAP *ld;
  104. /*
  105. * Grab an LDAP connection to use for this bind.
  106. */
  107. ld = NULL;
  108. if (( rc = passthru_get_connection( srvr, &ld )) != LDAP_SUCCESS ) {
  109. goto release_and_return;
  110. }
  111. /*
  112. * Send the bind operation (need to retry on LDAP_SERVER_DOWN)
  113. */
  114. if (( rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, creds, reqctrls,
  115. NULL, &msgid )) != LDAP_SUCCESS ) {
  116. goto release_and_return;
  117. }
  118. /*
  119. * determine timeout value (how long we will wait for a response)
  120. * if timeout is NULL or zero'd, we wait indefinitely.
  121. */
  122. if ( srvr->ptsrvr_timeout == NULL || ( srvr->ptsrvr_timeout->tv_sec == 0
  123. && srvr->ptsrvr_timeout->tv_usec == 0 )) {
  124. timeout = NULL;
  125. } else {
  126. tv = *srvr->ptsrvr_timeout; /* struct copy */
  127. timeout = &tv;
  128. }
  129. /*
  130. * Wait for a result.
  131. */
  132. rc = ldap_result( ld, msgid, 1, timeout, &result );
  133. /*
  134. * Interpret the result.
  135. */
  136. if ( rc == 0 ) { /* timeout */
  137. /*
  138. * Timed out waiting for a reply from the server.
  139. */
  140. rc = LDAP_TIMEOUT;
  141. } else if ( rc < 0 ) {
  142. /*
  143. * Some other error occurred (no result received).
  144. */
  145. rc = slapi_ldap_get_lderrno( ld, matcheddnp, errmsgp );
  146. } else {
  147. /*
  148. * Got a result from remote server -- parse it.
  149. */
  150. rc = ldap_parse_result( ld, result, lderrnop, matcheddnp, errmsgp,
  151. &referrals, resctrlsp, 1 );
  152. if ( referrals != NULL ) {
  153. *refurlsp = passthru_strs2bervals( referrals );
  154. slapi_ldap_value_free( referrals );
  155. }
  156. }
  157. release_and_return:
  158. if ( ld != NULL ) {
  159. passthru_release_connection( srvr, ld, PASSTHRU_LDAP_CONN_ERROR( rc ));
  160. }
  161. return( rc );
  162. }