lasgroup.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* #define DBG_PRINT */
  13. /* lasgroup.c
  14. * This file contains the Group LAS code.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <netsite.h>
  19. #include "aclpriv.h"
  20. #include <libaccess/usrcache.h>
  21. #include <libaccess/las.h>
  22. #include <libaccess/dbtlibaccess.h>
  23. #include <libaccess/aclerror.h>
  24. #include <ldaputil/errors.h> /* for DBG_PRINT */
  25. #include "aclutil.h"
  26. #ifdef UTEST
  27. extern char *LASGroupGetUser();
  28. #endif /* UTEST */
  29. /*
  30. * LASGroupEval
  31. * INPUT
  32. * attr_name The string "group" - in lower case.
  33. * comparator CMP_OP_EQ or CMP_OP_NE only
  34. * attr_pattern A comma-separated list of groups
  35. * *cachable Always set to ACL_NOT_CACHABLE
  36. * subject Subjust property list
  37. * resource Resource property list
  38. * auth_info Authentication info, if any
  39. * RETURNS
  40. * retcode The usual LAS return codes.
  41. */
  42. int LASGroupEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  43. char *attr_pattern, ACLCachable_t *cachable,
  44. void **LAS_cookie, PList_t subject, PList_t resource,
  45. PList_t auth_info, PList_t global_auth)
  46. {
  47. char *groups = attr_pattern;
  48. int retcode;
  49. char *member_of;
  50. char *user;
  51. char *dbname;
  52. time_t *req_time = 0;
  53. const char *group;
  54. char delim;
  55. int len;
  56. int rv;
  57. *cachable = ACL_NOT_CACHABLE;
  58. *LAS_cookie = (void *)0;
  59. if (strcmp(attr_name, ACL_ATTR_GROUP) != 0) {
  60. nserrGenerate(errp, ACLERRINVAL, ACLERR4900, ACL_Program, 2, XP_GetAdminStr(DBT_lasGroupEvalReceivedRequestForAt_), attr_name);
  61. return LAS_EVAL_INVALID;
  62. }
  63. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  64. nserrGenerate(errp, ACLERRINVAL, ACLERR4910, ACL_Program, 2, XP_GetAdminStr(DBT_lasgroupevalIllegalComparatorDN_), comparator_string(comparator));
  65. return LAS_EVAL_INVALID;
  66. }
  67. if (!strcmp(attr_pattern, "anyone")) {
  68. *cachable = ACL_INDEF_CACHABLE;
  69. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  70. }
  71. /* Get the authenticated user */
  72. rv = ACL_GetAttribute(errp, ACL_ATTR_USER, (void **)&user,
  73. subject, resource, auth_info, global_auth);
  74. if (rv != LAS_EVAL_TRUE) {
  75. return rv;
  76. }
  77. rv = ACL_AuthInfoGetDbname(auth_info, &dbname);
  78. if (rv < 0) {
  79. char rv_str[16];
  80. sprintf(rv_str, "%d", rv);
  81. nserrGenerate(errp, ACLERRFAIL, ACLERR4920, ACL_Program, 2, XP_GetAdminStr(DBT_lasGroupEvalUnableToGetDatabaseName), rv_str);
  82. return LAS_EVAL_FAIL;
  83. }
  84. /* Regardless of cache, req_time needs to be filled. */
  85. req_time = acl_get_req_time(resource);
  86. if (NULL == req_time) {
  87. return LAS_EVAL_FAIL;
  88. }
  89. rv = LAS_EVAL_FALSE;
  90. if (acl_usr_cache_enabled()) {
  91. /* Loop through all the groups and check if any is in the cache */
  92. group = groups;
  93. delim = ',';
  94. while((group = acl_next_token_len(group, delim, &len)) != NULL) {
  95. rv = acl_usr_cache_group_len_check(user, dbname, group, len, *req_time);
  96. if (rv == LAS_EVAL_TRUE) {
  97. /* cached group exists */
  98. break;
  99. }
  100. if (0 != (group = strchr(group+len, delim)))
  101. group++;
  102. else
  103. break;
  104. }
  105. /* group need not be NULL-terminated */
  106. /* If you need to use it, copy it properly */
  107. group = 0;
  108. }
  109. if (rv != LAS_EVAL_TRUE) {
  110. /* not found in the cache or not one of the groups we want */
  111. PListDeleteProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS);
  112. PListInitProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS, groups, 0);
  113. PListDeleteProp(subject, ACL_ATTR_USER_ISMEMBER_INDEX, ACL_ATTR_USER_ISMEMBER);
  114. rv = ACL_GetAttribute(errp, ACL_ATTR_USER_ISMEMBER, (void **)&member_of,
  115. subject, resource, auth_info, global_auth);
  116. PListDeleteProp(subject, ACL_ATTR_GROUPS_INDEX, ACL_ATTR_GROUPS);
  117. if (rv != LAS_EVAL_TRUE && rv != LAS_EVAL_FALSE) {
  118. return rv;
  119. }
  120. if (rv == LAS_EVAL_TRUE) {
  121. /* User is a member of one of the groups */
  122. /* update the user's cache */
  123. acl_usr_cache_set_group(user, dbname, member_of, *req_time);
  124. }
  125. }
  126. if (rv == LAS_EVAL_TRUE) {
  127. retcode = (comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  128. }
  129. else {
  130. /* User is not a member of any of the groups */
  131. retcode = (comparator == CMP_OP_EQ ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  132. }
  133. DBG_PRINT4("%s LASGroupEval: uid = \"%s\" groups = \"%s\"\n",
  134. (retcode == LAS_EVAL_FALSE) ? "LAS_EVAL_FALSE"
  135. : (retcode == LAS_EVAL_TRUE) ? "LAS_EVAL_TRUE"
  136. : "Error",
  137. user, attr_pattern);
  138. return retcode;
  139. }
  140. /* LASGroupFlush
  141. * Deallocates any memory previously allocated by the LAS
  142. */
  143. void
  144. LASGroupFlush(void **las_cookie)
  145. {
  146. /* do nothing */
  147. return;
  148. }