sbind.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1993 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. /*
  19. * BindRequest ::= SEQUENCE {
  20. * version INTEGER,
  21. * name DistinguishedName, -- who
  22. * authentication CHOICE {
  23. * simple [0] OCTET STRING -- passwd
  24. * krbv42ldap [1] OCTET STRING -- OBSOLETE
  25. * krbv42dsa [2] OCTET STRING -- OBSOLETE
  26. * sasl [3] SaslCredentials -- LDAPv3
  27. * }
  28. * }
  29. *
  30. * BindResponse ::= SEQUENCE {
  31. * COMPONENTS OF LDAPResult,
  32. * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
  33. * }
  34. *
  35. */
  36. #include "portable.h"
  37. #include <stdio.h>
  38. #include <ac/socket.h>
  39. #include <ac/string.h>
  40. #include <ac/time.h>
  41. #include "ldap-int.h"
  42. /*
  43. * ldap_simple_bind - bind to the ldap server (and X.500). The dn and
  44. * password of the entry to which to bind are supplied. The message id
  45. * of the request initiated is returned.
  46. *
  47. * Example:
  48. * ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
  49. * "secret" )
  50. */
  51. int
  52. ldap_simple_bind(
  53. LDAP *ld,
  54. LDAP_CONST char *dn,
  55. LDAP_CONST char *passwd )
  56. {
  57. int rc;
  58. int msgid;
  59. struct berval cred;
  60. Debug0( LDAP_DEBUG_TRACE, "ldap_simple_bind\n" );
  61. assert( ld != NULL );
  62. assert( LDAP_VALID( ld ) );
  63. if ( passwd != NULL ) {
  64. cred.bv_val = (char *) passwd;
  65. cred.bv_len = strlen( passwd );
  66. } else {
  67. cred.bv_val = "";
  68. cred.bv_len = 0;
  69. }
  70. rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
  71. NULL, NULL, &msgid );
  72. return rc == LDAP_SUCCESS ? msgid : -1;
  73. }
  74. /*
  75. * ldap_simple_bind - bind to the ldap server (and X.500) using simple
  76. * authentication. The dn and password of the entry to which to bind are
  77. * supplied. LDAP_SUCCESS is returned upon success, the ldap error code
  78. * otherwise.
  79. *
  80. * Example:
  81. * ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
  82. * "secret" )
  83. */
  84. int
  85. ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
  86. {
  87. struct berval cred;
  88. Debug0( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n" );
  89. if ( passwd != NULL ) {
  90. cred.bv_val = (char *) passwd;
  91. cred.bv_len = strlen( passwd );
  92. } else {
  93. cred.bv_val = "";
  94. cred.bv_len = 0;
  95. }
  96. return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
  97. NULL, NULL, NULL );
  98. }