lasgroup.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* #define DBG_PRINT */
  42. /* lasgroup.c
  43. * This file contains the Group LAS code.
  44. */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <netsite.h>
  48. #include "aclpriv.h"
  49. #include <libaccess/usrcache.h>
  50. #include <libaccess/las.h>
  51. #include <libaccess/dbtlibaccess.h>
  52. #include <libaccess/aclerror.h>
  53. #include <ldaputil/errors.h> /* for DBG_PRINT */
  54. #include "aclutil.h"
  55. #ifdef UTEST
  56. extern char *LASGroupGetUser();
  57. #endif /* UTEST */
  58. /*
  59. * LASGroupEval
  60. * INPUT
  61. * attr_name The string "group" - in lower case.
  62. * comparator CMP_OP_EQ or CMP_OP_NE only
  63. * attr_pattern A comma-separated list of groups
  64. * *cachable Always set to ACL_NOT_CACHABLE
  65. * subject Subjust property list
  66. * resource Resource property list
  67. * auth_info Authentication info, if any
  68. * RETURNS
  69. * retcode The usual LAS return codes.
  70. */
  71. int LASGroupEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  72. char *attr_pattern, ACLCachable_t *cachable,
  73. void **LAS_cookie, PList_t subject, PList_t resource,
  74. PList_t auth_info, PList_t global_auth)
  75. {
  76. char *groups = attr_pattern;
  77. int retcode;
  78. char *member_of;
  79. char *user;
  80. char *dbname;
  81. time_t *req_time = 0;
  82. const char *group;
  83. char delim;
  84. int len;
  85. int rv;
  86. *cachable = ACL_NOT_CACHABLE;
  87. *LAS_cookie = (void *)0;
  88. if (strcmp(attr_name, ACL_ATTR_GROUP) != 0) {
  89. nserrGenerate(errp, ACLERRINVAL, ACLERR4900, ACL_Program, 2, XP_GetAdminStr(DBT_lasGroupEvalReceivedRequestForAt_), attr_name);
  90. return LAS_EVAL_INVALID;
  91. }
  92. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  93. nserrGenerate(errp, ACLERRINVAL, ACLERR4910, ACL_Program, 2, XP_GetAdminStr(DBT_lasgroupevalIllegalComparatorDN_), comparator_string(comparator));
  94. return LAS_EVAL_INVALID;
  95. }
  96. if (!strcmp(attr_pattern, "anyone")) {
  97. *cachable = ACL_INDEF_CACHABLE;
  98. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  99. }
  100. /* Get the authenticated user */
  101. rv = ACL_GetAttribute(errp, ACL_ATTR_USER, (void **)&user,
  102. subject, resource, auth_info, global_auth);
  103. if (rv != LAS_EVAL_TRUE) {
  104. return rv;
  105. }
  106. rv = ACL_AuthInfoGetDbname(auth_info, &dbname);
  107. if (rv < 0) {
  108. char rv_str[16];
  109. sprintf(rv_str, "%d", rv);
  110. nserrGenerate(errp, ACLERRFAIL, ACLERR4920, ACL_Program, 2, XP_GetAdminStr(DBT_lasGroupEvalUnableToGetDatabaseName), rv_str);
  111. return LAS_EVAL_FAIL;
  112. }
  113. /* Regardless of cache, req_time needs to be filled. */
  114. req_time = acl_get_req_time(resource);
  115. if (NULL == req_time) {
  116. return LAS_EVAL_FAIL;
  117. }
  118. rv = LAS_EVAL_FALSE;
  119. if (acl_usr_cache_enabled()) {
  120. /* Loop through all the groups and check if any is in the cache */
  121. group = groups;
  122. delim = ',';
  123. while((group = acl_next_token_len(group, delim, &len)) != NULL) {
  124. rv = acl_usr_cache_group_len_check(user, dbname, group, len, *req_time);
  125. if (rv == LAS_EVAL_TRUE) {
  126. /* cached group exists */
  127. break;
  128. }
  129. if (0 != (group = strchr(group+len, delim)))
  130. group++;
  131. else
  132. break;
  133. }
  134. /* group need not be NULL-terminated */
  135. /* If you need to use it, copy it properly */
  136. group = 0;
  137. }
  138. if (rv != LAS_EVAL_TRUE) {
  139. /* not found in the cache or not one of the groups we want */
  140. PListDeleteProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS);
  141. PListInitProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS, groups, 0);
  142. PListDeleteProp(subject, ACL_ATTR_USER_ISMEMBER_INDEX, ACL_ATTR_USER_ISMEMBER);
  143. rv = ACL_GetAttribute(errp, ACL_ATTR_USER_ISMEMBER, (void **)&member_of,
  144. subject, resource, auth_info, global_auth);
  145. PListDeleteProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS);
  146. if (rv != LAS_EVAL_TRUE && rv != LAS_EVAL_FALSE) {
  147. return rv;
  148. }
  149. if (rv == LAS_EVAL_TRUE) {
  150. /* User is a member of one of the groups */
  151. /* update the user's cache */
  152. acl_usr_cache_set_group(user, dbname, member_of, *req_time);
  153. }
  154. }
  155. if (rv == LAS_EVAL_TRUE) {
  156. retcode = (comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  157. }
  158. else {
  159. /* User is not a member of any of the groups */
  160. retcode = (comparator == CMP_OP_EQ ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  161. }
  162. DBG_PRINT4("%s LASGroupEval: uid = \"%s\" groups = \"%s\"\n",
  163. (retcode == LAS_EVAL_FALSE) ? "LAS_EVAL_FALSE"
  164. : (retcode == LAS_EVAL_TRUE) ? "LAS_EVAL_TRUE"
  165. : "Error",
  166. user, attr_pattern);
  167. return retcode;
  168. }
  169. /* LASGroupFlush
  170. * Deallocates any memory previously allocated by the LAS
  171. */
  172. void
  173. LASGroupFlush(void **las_cookie)
  174. {
  175. /* do nothing */
  176. return;
  177. }