lasemail.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* lasemail.cpp
  42. * This file contains the Email LAS code.
  43. */
  44. #include <ldap.h>
  45. #include <nsacl/aclapi.h>
  46. #define ACL_ATTR_EMAIL "email"
  47. extern "C" {
  48. extern int LASEmailEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, char *attr_pattern, ACLCachable_t *cachable, void **LAS_cookie, PList_t subject, PList_t resource, PList_t auth_info, PList_t global_auth);
  49. extern void LASEmailFlush(void **las_cookie);
  50. extern int LASEmailModuleInit();
  51. }
  52. /*
  53. * LASEmailEval
  54. * INPUT
  55. * attr_name The string "email" - in lower case.
  56. * comparator CMP_OP_EQ or CMP_OP_NE only
  57. * attr_pattern A comma-separated list of emails
  58. * (we currently support only one e-mail addr)
  59. * *cachable Always set to ACL_NOT_CACHABLE.
  60. * subject Subject property list
  61. * resource Resource property list
  62. * auth_info Authentication info, if any
  63. * RETURNS
  64. * retcode The usual LAS return codes.
  65. */
  66. int LASEmailEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  67. char *attr_pattern, ACLCachable_t *cachable,
  68. void **LAS_cookie, PList_t subject, PList_t resource,
  69. PList_t auth_info, PList_t global_auth)
  70. {
  71. char *uid;
  72. char *email;
  73. int rv;
  74. LDAP *ld;
  75. char *basedn;
  76. LDAPMessage *res;
  77. int numEntries;
  78. char filter[1024];
  79. int matched;
  80. *cachable = ACL_NOT_CACHABLE;
  81. *LAS_cookie = (void *)0;
  82. if (strcmp(attr_name, ACL_ATTR_EMAIL) != 0) {
  83. fprintf(stderr, "LASEmailEval called for incorrect attr \"%s\"\n",
  84. attr_name);
  85. return LAS_EVAL_INVALID;
  86. }
  87. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  88. fprintf(stderr, "LASEmailEval called with incorrect comparator %d\n",
  89. comparator);
  90. return LAS_EVAL_INVALID;
  91. }
  92. if (!strcmp(attr_pattern, "anyone")) {
  93. *cachable = ACL_INDEF_CACHABLE;
  94. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  95. }
  96. /* get the authenticated user name */
  97. rv = ACL_GetAttribute(errp, ACL_ATTR_USER, (void **)&uid,
  98. subject, resource, auth_info, global_auth);
  99. if (rv != LAS_EVAL_TRUE) {
  100. return rv;
  101. }
  102. /* We have an authenticated user */
  103. if (!strcmp(attr_pattern, "all")) {
  104. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  105. }
  106. /* do an ldap lookup for: (& (uid=<user>) (mail=<email>)) */
  107. rv = ACL_LDAPDatabaseHandle(errp, NULL, &ld, &basedn);
  108. if (rv != LAS_EVAL_TRUE) {
  109. fprintf(stderr, "unable to get LDAP handle\n");
  110. return rv;
  111. }
  112. /* Formulate the filter -- assume single e-mail in attr_pattern */
  113. /* If we support multiple comma separated e-mail addresses in the
  114. * attr_pattern then the filter will look like:
  115. * (& (uid=<user>) (| (mail=<email1>) (mail=<email2>)))
  116. */
  117. sprintf(filter, "(& (uid=%s) (mail=%s))", uid, attr_pattern);
  118. rv = ldap_search_s(ld, basedn, LDAP_SCOPE_SUBTREE, filter,
  119. 0, 0, &res);
  120. if (rv != LDAP_SUCCESS)
  121. {
  122. fprintf(stderr, "ldap_search_s: %s\n", ldap_err2string(rv));
  123. return LAS_EVAL_FAIL;
  124. }
  125. numEntries = ldap_count_entries(ld, res);
  126. if (numEntries == 1) {
  127. /* success */
  128. LDAPMessage *entry = ldap_first_entry(ld, res);
  129. char *dn = ldap_get_dn(ld, entry);
  130. fprintf(stderr, "ldap_search_s: Entry found. DN: \"%s\"\n", dn);
  131. ldap_memfree(dn);
  132. matched = 1;
  133. }
  134. else if (numEntries == 0) {
  135. /* not found -- but not an error */
  136. fprintf(stderr, "ldap_search_s: Entry not found. Filter: \"%s\"\n",
  137. filter);
  138. matched = 0;
  139. }
  140. else if (numEntries > 0) {
  141. /* Found more than one entry! */
  142. fprintf(stderr, "ldap_search_s: Found more than one entry. Filter: \"%s\"\n",
  143. filter);
  144. return LAS_EVAL_FAIL;
  145. }
  146. if (comparator == CMP_OP_EQ) {
  147. rv = (matched ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  148. }
  149. else {
  150. rv = (matched ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  151. }
  152. return rv;
  153. }
  154. /* LASEmailFlush
  155. * Deallocates any memory previously allocated by the LAS
  156. */
  157. void
  158. LASEmailFlush(void **las_cookie)
  159. {
  160. /* do nothing */
  161. return;
  162. }
  163. /* LASEmailModuleInit --
  164. * Register the e-mail LAS.
  165. *
  166. * To load this functions in the web server, compile the file in
  167. * "lasemail.so" and add the following lines to the
  168. * <ServerRoot>/https-<name>/config/obj.conf file. Be sure to change the
  169. * "lasemail.so" portion to the full pathname. E.g. /nshome/lib/lasemail.so.
  170. *
  171. * Init fn="load-modules" funcs="LASEmailModuleInit" shlib="lasemail.so"
  172. * Init fn="acl-register-module" module="lasemail" func="LASEmailModuleInit"
  173. */
  174. int LASEmailModuleInit ()
  175. {
  176. NSErr_t err = NSERRINIT;
  177. NSErr_t *errp = &err;
  178. int rv;
  179. rv = ACL_LasRegister(errp, ACL_ATTR_EMAIL, LASEmailEval, LASEmailFlush);
  180. if (rv < 0) {
  181. fprintf(stderr, "ACL_LasRegister failed. Error: %d\n", rv);
  182. return rv;
  183. }
  184. return rv;
  185. }