lasemail.cpp 6.8 KB

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