aclproxy.c 6.9 KB

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