vc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /* ACKNOWLEDGEMENTS:
  16. * This program was originally developed by Kurt D. Zeilenga for inclusion in
  17. * OpenLDAP Software.
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. /*
  26. * LDAP Verify Credentials operation
  27. *
  28. * The request is an extended request with OID 1.3.6.1.4.1.4203.666.6.5 with value of
  29. * the BER encoding of:
  30. *
  31. * VCRequest ::= SEQUENCE {
  32. * cookie [0] OCTET STRING OPTIONAL,
  33. * name LDAPDN,
  34. * authentication AuthenticationChoice,
  35. * controls [2] Controls OPTIONAL
  36. * }
  37. *
  38. * where LDAPDN, AuthenticationChoice, and Controls are as defined in RFC 4511.
  39. *
  40. * The response is an extended response with no OID and a value of the BER encoding of
  41. *
  42. * VCResponse ::= SEQUENCE {
  43. * resultCode ResultCode,
  44. * diagnosticMessage LDAPString,
  45. * cookie [0] OCTET STRING OPTIONAL,
  46. * serverSaslCreds [1] OCTET STRING OPTIONAL,
  47. * controls [2] Controls OPTIONAL
  48. * }
  49. *
  50. * where ResultCode is the result code enumeration from RFC 4511, and LDAPString and Controls are as
  51. * defined in RFC 4511.
  52. */
  53. int ldap_parse_verify_credentials(
  54. LDAP *ld,
  55. LDAPMessage *res,
  56. int * code,
  57. char ** diagmsg,
  58. struct berval **cookie,
  59. struct berval **screds,
  60. LDAPControl ***ctrls)
  61. {
  62. int rc;
  63. char *retoid = NULL;
  64. struct berval *retdata = NULL;
  65. assert(ld != NULL);
  66. assert(LDAP_VALID(ld));
  67. assert(res != NULL);
  68. assert(code != NULL);
  69. assert(diagmsg != NULL);
  70. rc = ldap_parse_extended_result(ld, res, &retoid, &retdata, 0);
  71. if( rc != LDAP_SUCCESS ) {
  72. ldap_perror(ld, "ldap_parse_verify_credentials");
  73. return rc;
  74. }
  75. if (retdata) {
  76. ber_tag_t tag;
  77. ber_len_t len;
  78. ber_int_t i;
  79. BerElement * ber = ber_init(retdata);
  80. struct berval diagmsg_bv = BER_BVNULL;
  81. if (!ber) {
  82. rc = ld->ld_errno = LDAP_NO_MEMORY;
  83. goto done;
  84. }
  85. rc = LDAP_DECODING_ERROR;
  86. if (ber_scanf(ber, "{im" /*"}"*/, &i, &diagmsg_bv) == LBER_ERROR) {
  87. goto ber_done;
  88. }
  89. if ( diagmsg != NULL ) {
  90. *diagmsg = LDAP_MALLOC( diagmsg_bv.bv_len + 1 );
  91. AC_MEMCPY( *diagmsg, diagmsg_bv.bv_val, diagmsg_bv.bv_len );
  92. (*diagmsg)[diagmsg_bv.bv_len] = '\0';
  93. }
  94. *code = i;
  95. tag = ber_peek_tag(ber, &len);
  96. if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE) {
  97. if (ber_scanf(ber, "O", cookie) == LBER_ERROR)
  98. goto ber_done;
  99. tag = ber_peek_tag(ber, &len);
  100. }
  101. if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_SCREDS) {
  102. if (ber_scanf(ber, "O", screds) == LBER_ERROR)
  103. goto ber_done;
  104. tag = ber_peek_tag(ber, &len);
  105. }
  106. if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS) {
  107. int nctrls = 0;
  108. char * opaque;
  109. *ctrls = LDAP_MALLOC(1 * sizeof(LDAPControl *));
  110. if (!*ctrls) {
  111. rc = LDAP_NO_MEMORY;
  112. goto ber_done;
  113. }
  114. *ctrls[nctrls] = NULL;
  115. for(tag = ber_first_element(ber, &len, &opaque);
  116. tag != LBER_ERROR;
  117. tag = ber_next_element(ber, &len, opaque))
  118. {
  119. LDAPControl *tctrl;
  120. LDAPControl **tctrls;
  121. tctrl = LDAP_CALLOC(1, sizeof(LDAPControl));
  122. /* allocate pointer space for current controls (nctrls)
  123. * + this control + extra NULL
  124. */
  125. tctrls = !tctrl ? NULL : LDAP_REALLOC(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
  126. if (!tctrls) {
  127. /* allocation failure */
  128. if (tctrl) LDAP_FREE(tctrl);
  129. ldap_controls_free(*ctrls);
  130. *ctrls = NULL;
  131. rc = LDAP_NO_MEMORY;
  132. goto ber_done;
  133. }
  134. tctrls[nctrls++] = tctrl;
  135. tctrls[nctrls] = NULL;
  136. tag = ber_scanf(ber, "{a" /*"}"*/, &tctrl->ldctl_oid);
  137. if (tag == LBER_ERROR) {
  138. *ctrls = NULL;
  139. ldap_controls_free(tctrls);
  140. goto ber_done;
  141. }
  142. tag = ber_peek_tag(ber, &len);
  143. if (tag == LBER_BOOLEAN) {
  144. ber_int_t crit;
  145. tag = ber_scanf(ber, "b", &crit);
  146. tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
  147. tag = ber_peek_tag(ber, &len);
  148. }
  149. if (tag == LBER_OCTETSTRING) {
  150. tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
  151. } else {
  152. BER_BVZERO( &tctrl->ldctl_value );
  153. }
  154. *ctrls = tctrls;
  155. }
  156. }
  157. rc = LDAP_SUCCESS;
  158. ber_done:
  159. ber_free(ber, 1);
  160. }
  161. done:
  162. ber_bvfree(retdata);
  163. ber_memfree(retoid);
  164. return rc;
  165. }
  166. int
  167. ldap_verify_credentials(LDAP *ld,
  168. struct berval *cookie,
  169. LDAP_CONST char *dn,
  170. LDAP_CONST char *mechanism,
  171. struct berval *cred,
  172. LDAPControl **vcctrls,
  173. LDAPControl **sctrls,
  174. LDAPControl **cctrls,
  175. int *msgidp)
  176. {
  177. int rc;
  178. BerElement *ber;
  179. struct berval reqdata;
  180. assert(ld != NULL);
  181. assert(LDAP_VALID(ld));
  182. assert(msgidp != NULL);
  183. ber = ber_alloc_t(LBER_USE_DER);
  184. if (dn == NULL) dn = "";
  185. if (mechanism == LDAP_SASL_SIMPLE) {
  186. assert(!cookie);
  187. rc = ber_printf(ber, "{stO" /*"}"*/,
  188. dn, LDAP_AUTH_SIMPLE, cred);
  189. } else {
  190. if (!cred || BER_BVISNULL(cred)) {
  191. if (cookie) {
  192. rc = ber_printf(ber, "{tOst{sN}" /*"}"*/,
  193. LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, cookie,
  194. dn, LDAP_AUTH_SASL, mechanism);
  195. } else {
  196. rc = ber_printf(ber, "{st{sN}N" /*"}"*/,
  197. dn, LDAP_AUTH_SASL, mechanism);
  198. }
  199. } else {
  200. if (cookie) {
  201. rc = ber_printf(ber, "{tOst{sON}" /*"}"*/,
  202. LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, cookie,
  203. dn, LDAP_AUTH_SASL, mechanism, cred);
  204. } else {
  205. rc = ber_printf(ber, "{st{sON}" /*"}"*/,
  206. dn, LDAP_AUTH_SASL, mechanism, cred);
  207. }
  208. }
  209. }
  210. if (rc < 0) {
  211. rc = ld->ld_errno = LDAP_ENCODING_ERROR;
  212. goto done;
  213. }
  214. if (vcctrls && *vcctrls) {
  215. LDAPControl *const *c;
  216. rc = ber_printf(ber, "t{" /*"}"*/, LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS);
  217. for (c=vcctrls; *c; c++) {
  218. rc = ldap_pvt_put_control(*c, ber);
  219. if (rc != LDAP_SUCCESS) {
  220. rc = ld->ld_errno = LDAP_ENCODING_ERROR;
  221. goto done;
  222. }
  223. }
  224. rc = ber_printf(ber, /*"{{"*/ "}N}");
  225. } else {
  226. rc = ber_printf(ber, /*"{"*/ "N}");
  227. }
  228. if (rc < 0) {
  229. rc = ld->ld_errno = LDAP_ENCODING_ERROR;
  230. goto done;
  231. }
  232. rc = ber_flatten2(ber, &reqdata, 0);
  233. if (rc < 0) {
  234. rc = ld->ld_errno = LDAP_ENCODING_ERROR;
  235. goto done;
  236. }
  237. rc = ldap_extended_operation(ld, LDAP_EXOP_VERIFY_CREDENTIALS,
  238. &reqdata, sctrls, cctrls, msgidp);
  239. done:
  240. ber_free(ber, 1);
  241. return rc;
  242. }
  243. int
  244. ldap_verify_credentials_s(
  245. LDAP *ld,
  246. struct berval *cookie,
  247. LDAP_CONST char *dn,
  248. LDAP_CONST char *mechanism,
  249. struct berval *cred,
  250. LDAPControl **vcictrls,
  251. LDAPControl **sctrls,
  252. LDAPControl **cctrls,
  253. int *rcode,
  254. char **diagmsg,
  255. struct berval **scookie,
  256. struct berval **scred,
  257. LDAPControl ***vcoctrls)
  258. {
  259. int rc;
  260. int msgid;
  261. LDAPMessage *res;
  262. rc = ldap_verify_credentials(ld, cookie, dn, mechanism, cred, vcictrls, sctrls, cctrls, &msgid);
  263. if (rc != LDAP_SUCCESS) return rc;
  264. if (ldap_result(ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res) == -1 || !res) {
  265. return ld->ld_errno;
  266. }
  267. rc = ldap_parse_verify_credentials(ld, res, rcode, diagmsg, scookie, scred, vcoctrls);
  268. if (rc != LDAP_SUCCESS) {
  269. ldap_msgfree(res);
  270. return rc;
  271. }
  272. return( ldap_result2error(ld, res, 1));
  273. }
  274. #ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS_INTERACTIVE
  275. int
  276. ldap_verify_credentials_interactive (
  277. LDAP *ld,
  278. LDAP_CONST char *dn, /* usually NULL */
  279. LDAP_CONST char *mech,
  280. LDAPControl **vcControls,
  281. LDAPControl **serverControls,
  282. LDAPControl **clientControls,
  283. /* should be client controls */
  284. unsigned flags,
  285. LDAP_SASL_INTERACT_PROC *proc,
  286. void *defaults,
  287. void *context;
  288. /* as obtained from ldap_result() */
  289. LDAPMessage *result,
  290. /* returned during bind processing */
  291. const char **rmech,
  292. int *msgid )
  293. {
  294. if (!ld && context) {
  295. assert(!dn);
  296. assert(!mech);
  297. assert(!vcControls);
  298. assert(!serverControls);
  299. assert(!defaults);
  300. assert(!result);
  301. assert(!rmech);
  302. assert(!msgid);
  303. /* special case to avoid having to expose a separate dispose context API */
  304. sasl_dispose((sasl_conn_t)context);
  305. return LDAP_SUCCESS;
  306. }
  307. ld->ld_errno = LDAP_NOT_SUPPORTED;
  308. return ld->ld_errno;
  309. }
  310. #endif