lasgroup.cpp 6.6 KB

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