aclproxy.c 6.4 KB

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