proxyauth.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <ldap.h>
  13. #include "slap.h"
  14. #define BEGIN do {
  15. #define END } while(0);
  16. /* ------------------------------------------------------------
  17. * LDAPProxyAuth
  18. *
  19. * ProxyAuthControl ::= SEQUENCE {
  20. * authorizationDN LDAPDN
  21. * }
  22. */
  23. struct LDAPProxyAuth
  24. {
  25. char *auth_dn;
  26. };
  27. typedef struct LDAPProxyAuth LDAPProxyAuth;
  28. /*
  29. * delete_LDAPProxyAuth
  30. */
  31. static void
  32. delete_LDAPProxyAuth(LDAPProxyAuth *spec)
  33. {
  34. if (!spec) return;
  35. slapi_ch_free((void**)&spec->auth_dn);
  36. slapi_ch_free((void**)&spec);
  37. }
  38. /*
  39. * parse_LDAPProxyAuth
  40. *
  41. * Parse a BER encoded value into the compoents of the LDAP ProxyAuth control.
  42. * The 'version' parameter should be 1 or 2.
  43. *
  44. * Returns an LDAP error code (LDAP_SUCCESS if all goes well) and sets
  45. * *errtextp if appropriate.
  46. */
  47. static int
  48. parse_LDAPProxyAuth(struct berval *spec_ber, int version, char **errtextp,
  49. LDAPProxyAuth **out)
  50. {
  51. int lderr = LDAP_OPERATIONS_ERROR; /* pessimistic */
  52. LDAPProxyAuth *spec = NULL;
  53. BerElement *ber = NULL;
  54. char *errstring = "unable to parse proxied authorization control";
  55. Slapi_DN *sdn = NULL;
  56. BEGIN
  57. ber_tag_t tag;
  58. if ( version != 1 && version != 2 ) {
  59. break;
  60. }
  61. if (!BV_HAS_DATA(spec_ber)) {
  62. break;
  63. }
  64. /* create_LDAPProxyAuth */
  65. spec = (LDAPProxyAuth*)slapi_ch_calloc(1,sizeof (LDAPProxyAuth));
  66. if (!spec) {
  67. break;
  68. }
  69. if (version == 2 && (spec_ber->bv_val[0] != CHAR_OCTETSTRING)) {
  70. /* This doesn't start with an octet string, so just use the actual value */
  71. spec->auth_dn = slapi_ch_strdup(spec_ber->bv_val);
  72. } else {
  73. ber = ber_init(spec_ber);
  74. if (!ber) {
  75. break;
  76. }
  77. if ( version == 1 ) {
  78. tag = ber_scanf(ber, "{a}", &spec->auth_dn);
  79. } else {
  80. tag = ber_scanf(ber, "a", &spec->auth_dn);
  81. }
  82. if (tag == LBER_ERROR) {
  83. lderr = LDAP_PROTOCOL_ERROR;
  84. break;
  85. }
  86. }
  87. /*
  88. * In version 2 of the control, the control value is actually an
  89. * authorization ID (see section 9 of RFC 2829). We only support
  90. * the "dnAuthzId" flavor, which looks like "dn:<DN>" where <DN> is
  91. * an actual DN, e.g., "dn:uid=bjensen,dc=example,dc=com". So we
  92. * need to strip off the dn: if present and reject the operation if
  93. * not.
  94. */
  95. if (2 == version) {
  96. if ( NULL == spec->auth_dn || strlen( spec->auth_dn ) < 3 ||
  97. strncmp( "dn:", spec->auth_dn, 3 ) != 0 ) {
  98. lderr = LDAP_INSUFFICIENT_ACCESS; /* per Proxied Auth. I-D */
  99. errstring = "proxied authorization id must be a DN (dn:...)";
  100. break;
  101. }
  102. /* memmove is safe for overlapping copy */
  103. memmove ( spec->auth_dn, spec->auth_dn + 3, strlen(spec->auth_dn) - 2);/* 1 for '\0' */
  104. }
  105. lderr = LDAP_SUCCESS; /* got it! */
  106. sdn = slapi_sdn_new_dn_passin(spec->auth_dn);
  107. spec->auth_dn = slapi_ch_strdup(slapi_sdn_get_ndn(sdn));
  108. slapi_sdn_free(&sdn);
  109. if (NULL == spec->auth_dn) {
  110. lderr = LDAP_INVALID_SYNTAX;
  111. }
  112. END
  113. /* Cleanup */
  114. if (ber) ber_free(ber, 1);
  115. if ( LDAP_SUCCESS != lderr)
  116. {
  117. if (spec) delete_LDAPProxyAuth(spec);
  118. spec = 0;
  119. if ( NULL != errtextp ) {
  120. *errtextp = errstring;
  121. }
  122. }
  123. *out = spec;
  124. return lderr;
  125. }
  126. /*
  127. * proxyauth_dn - find the users DN in the proxyauth control if it is
  128. * present. The return value has been malloced for you.
  129. *
  130. * Returns an LDAP error code. If anything than LDAP_SUCCESS is returned,
  131. * the error should be returned to the client. LDAP_SUCCESS is always
  132. * returned if the proxy auth control is not present or not critical.
  133. */
  134. int
  135. proxyauth_get_dn( Slapi_PBlock *pb, char **proxydnp, char **errtextp )
  136. {
  137. char *dn = 0;
  138. LDAPProxyAuth *spec = 0;
  139. int rv, lderr = LDAP_SUCCESS; /* optimistic */
  140. BEGIN
  141. struct berval *spec_ber;
  142. LDAPControl **controls;
  143. int present;
  144. int critical;
  145. int version = 1;
  146. rv = slapi_pblock_get( pb, SLAPI_REQCONTROLS, &controls );
  147. if (rv) break;
  148. present = slapi_control_present( controls, LDAP_CONTROL_PROXYAUTH,
  149. &spec_ber, &critical );
  150. if (!present) {
  151. present = slapi_control_present( controls, LDAP_CONTROL_PROXIEDAUTH,
  152. &spec_ber, &critical );
  153. if (!present) break;
  154. version = 2;
  155. /*
  156. * Note the according to the Proxied Authorization I-D, the
  157. * control is always supposed to be marked critical by the
  158. * client. If it is not, we return a protocolError.
  159. */
  160. if ( !critical ) {
  161. lderr = LDAP_PROTOCOL_ERROR;
  162. if ( NULL != errtextp ) {
  163. *errtextp = "proxy control must be marked critical";
  164. }
  165. break;
  166. }
  167. }
  168. rv = parse_LDAPProxyAuth(spec_ber, version, errtextp, &spec);
  169. if (LDAP_SUCCESS != rv) {
  170. if ( critical ) {
  171. lderr = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  172. } else {
  173. lderr = rv;
  174. }
  175. break;
  176. }
  177. dn = slapi_ch_strdup(spec->auth_dn);
  178. if (slapi_dn_isroot(dn) ) {
  179. if (critical)
  180. lderr = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  181. else
  182. lderr = LDAP_UNWILLING_TO_PERFORM;
  183. *errtextp = "Proxy dn should not be rootdn";
  184. break;
  185. }
  186. END
  187. if (spec) delete_LDAPProxyAuth(spec);
  188. if ( NULL != proxydnp ) {
  189. *proxydnp = dn;
  190. } else {
  191. slapi_ch_free_string(&dn);
  192. }
  193. return lderr;
  194. }