proxyauth.c 7.1 KB

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