lasuser.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /* lasuser.c
  39. * This file contains the User LAS code.
  40. */
  41. #include <netsite.h>
  42. #include <base/shexp.h>
  43. #include <base/util.h>
  44. #include <libaccess/las.h>
  45. #include <libaccess/dbtlibaccess.h>
  46. #include <libaccess/aclerror.h>
  47. #include "aclutil.h"
  48. #ifdef UTEST
  49. extern char * LASUserGetUser();
  50. #endif
  51. /*
  52. * LASUserEval
  53. * INPUT
  54. * attr_name The string "user" - in lower case.
  55. * comparator CMP_OP_EQ or CMP_OP_NE only
  56. * attr_pattern A comma-separated list of users
  57. * *cachable Always set to ACL_NOT_CACHABLE.
  58. * subject Subject property list
  59. * resource Resource property list
  60. * auth_info Authentication info, if any
  61. * RETURNS
  62. * retcode The usual LAS return codes.
  63. */
  64. int LASUserEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  65. char *attr_pattern, ACLCachable_t *cachable,
  66. void **LAS_cookie, PList_t subject, PList_t resource,
  67. PList_t auth_info, PList_t global_auth)
  68. {
  69. char *uid;
  70. char *users;
  71. char *user;
  72. char *comma;
  73. int retcode;
  74. int matched;
  75. int is_owner;
  76. int rv;
  77. *cachable = ACL_NOT_CACHABLE;
  78. *LAS_cookie = (void *)0;
  79. if (strcmp(attr_name, ACL_ATTR_USER) != 0) {
  80. nserrGenerate(errp, ACLERRINVAL, ACLERR5700, ACL_Program, 2, XP_GetAdminStr(DBT_lasUserEvalReceivedRequestForAtt_), attr_name);
  81. return LAS_EVAL_INVALID;
  82. }
  83. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  84. nserrGenerate(errp, ACLERRINVAL, ACLERR5710, ACL_Program, 2, XP_GetAdminStr(DBT_lasuserevalIllegalComparatorDN_), comparator_string(comparator));
  85. return LAS_EVAL_INVALID;
  86. }
  87. if (!strcmp(attr_pattern, "anyone")) {
  88. *cachable = ACL_INDEF_CACHABLE;
  89. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  90. }
  91. /* get the authenticated user name */
  92. #ifndef UTEST
  93. rv = ACL_GetAttribute(errp, ACL_ATTR_USER, (void **)&uid,
  94. subject, resource, auth_info, global_auth);
  95. if (rv != LAS_EVAL_TRUE) {
  96. return rv;
  97. }
  98. #else
  99. uid = (char *)LASUserGetUser();
  100. #endif
  101. /* We have an authenticated user */
  102. if (!strcmp(attr_pattern, "all")) {
  103. return comparator == CMP_OP_EQ ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  104. }
  105. users = STRDUP(attr_pattern);
  106. if (!users) {
  107. nserrGenerate(errp, ACLERRNOMEM, ACLERR5720, ACL_Program, 1,
  108. XP_GetAdminStr(DBT_lasuserevalRanOutOfMemoryN_));
  109. return LAS_EVAL_FAIL;
  110. }
  111. user = users;
  112. matched = 0;
  113. /* check if the uid is one of the users */
  114. while(user != 0 && *user != 0 && !matched) {
  115. if ((comma = strchr(user, ',')) != NULL) {
  116. *comma++ = 0;
  117. }
  118. /* ignore leading whitespace */
  119. while(*user == ' ' || *user == '\t') user++;
  120. if (*user) {
  121. /* ignore trailing whitespace */
  122. int len = strlen(user);
  123. char *ptr = user+len-1;
  124. while(*ptr == ' ' || *ptr == '\t') *ptr-- = 0;
  125. }
  126. if (!strcasecmp(user, ACL_ATTR_OWNER)) {
  127. rv = ACL_GetAttribute(errp, ACL_ATTR_IS_OWNER, (void **)&is_owner,
  128. subject, resource, auth_info, global_auth);
  129. if (rv == LAS_EVAL_TRUE)
  130. matched = 1;
  131. else
  132. /* continue checking for next user */
  133. user = comma;
  134. }
  135. else if (!WILDPAT_CASECMP(uid, user)) {
  136. /* uid is one of the users */
  137. matched = 1;
  138. }
  139. else {
  140. /* continue checking for next user */
  141. user = comma;
  142. }
  143. }
  144. if (comparator == CMP_OP_EQ) {
  145. retcode = (matched ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
  146. }
  147. else {
  148. retcode = (matched ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  149. }
  150. FREE(users);
  151. return retcode;
  152. }
  153. /* LASUserFlush
  154. * Deallocates any memory previously allocated by the LAS
  155. */
  156. void
  157. LASUserFlush(void **las_cookie)
  158. {
  159. /* do nothing */
  160. return;
  161. }