bind.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* bind.c */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  17. * All rights reserved.
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/socket.h>
  23. #include <ac/string.h>
  24. #include <ac/time.h>
  25. #include "ldap-int.h"
  26. #include "ldap_log.h"
  27. /*
  28. * BindRequest ::= SEQUENCE {
  29. * version INTEGER,
  30. * name DistinguishedName, -- who
  31. * authentication CHOICE {
  32. * simple [0] OCTET STRING -- passwd
  33. * krbv42ldap [1] OCTET STRING -- OBSOLETE
  34. * krbv42dsa [2] OCTET STRING -- OBSOLETE
  35. * sasl [3] SaslCredentials -- LDAPv3
  36. * }
  37. * }
  38. *
  39. * BindResponse ::= SEQUENCE {
  40. * COMPONENTS OF LDAPResult,
  41. * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
  42. * }
  43. *
  44. * (Source: RFC 2251)
  45. */
  46. /*
  47. * ldap_bind - bind to the ldap server (and X.500). The dn and password
  48. * of the entry to which to bind are supplied, along with the authentication
  49. * method to use. The msgid of the bind request is returned on success,
  50. * -1 if there's trouble. ldap_result() should be called to find out the
  51. * outcome of the bind request.
  52. *
  53. * Example:
  54. * ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
  55. * LDAP_AUTH_SIMPLE )
  56. */
  57. int
  58. ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
  59. {
  60. Debug0( LDAP_DEBUG_TRACE, "ldap_bind\n" );
  61. switch ( authmethod ) {
  62. case LDAP_AUTH_SIMPLE:
  63. return( ldap_simple_bind( ld, dn, passwd ) );
  64. case LDAP_AUTH_SASL:
  65. /* user must use ldap_sasl_bind */
  66. /* FALL-THRU */
  67. default:
  68. ld->ld_errno = LDAP_AUTH_UNKNOWN;
  69. return( -1 );
  70. }
  71. }
  72. /*
  73. * ldap_bind_s - bind to the ldap server (and X.500). The dn and password
  74. * of the entry to which to bind are supplied, along with the authentication
  75. * method to use. This routine just calls whichever bind routine is
  76. * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
  77. * some other error indication).
  78. *
  79. * Examples:
  80. * ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
  81. * "secret", LDAP_AUTH_SIMPLE )
  82. * ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
  83. * NULL, LDAP_AUTH_KRBV4 )
  84. */
  85. int
  86. ldap_bind_s(
  87. LDAP *ld,
  88. LDAP_CONST char *dn,
  89. LDAP_CONST char *passwd,
  90. int authmethod )
  91. {
  92. Debug0( LDAP_DEBUG_TRACE, "ldap_bind_s\n" );
  93. switch ( authmethod ) {
  94. case LDAP_AUTH_SIMPLE:
  95. return( ldap_simple_bind_s( ld, dn, passwd ) );
  96. case LDAP_AUTH_SASL:
  97. /* user must use ldap_sasl_bind */
  98. /* FALL-THRU */
  99. default:
  100. return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
  101. }
  102. }