lasgroup.cpp 4.6 KB

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