groupinfo.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_strings.h"
  17. #include "fspr_portable.h"
  18. #include "fspr_user.h"
  19. #include "fspr_private.h"
  20. #if APR_HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. APR_DECLARE(fspr_status_t) fspr_gid_get(fspr_gid_t *gid,
  24. const char *groupname, fspr_pool_t *p)
  25. {
  26. #ifdef _WIN32_WCE
  27. return APR_ENOTIMPL;
  28. #else
  29. SID_NAME_USE sidtype;
  30. char anydomain[256];
  31. char *domain;
  32. DWORD sidlen = 0;
  33. DWORD domlen = sizeof(anydomain);
  34. DWORD rv;
  35. char *pos;
  36. if (pos = strchr(groupname, '/')) {
  37. domain = fspr_pstrndup(p, groupname, pos - groupname);
  38. groupname = pos + 1;
  39. }
  40. else if (pos = strchr(groupname, '\\')) {
  41. domain = fspr_pstrndup(p, groupname, pos - groupname);
  42. groupname = pos + 1;
  43. }
  44. else {
  45. domain = NULL;
  46. }
  47. /* Get nothing on the first pass ... need to size the sid buffer
  48. */
  49. rv = LookupAccountName(domain, groupname, domain, &sidlen,
  50. anydomain, &domlen, &sidtype);
  51. if (sidlen) {
  52. /* Give it back on the second pass
  53. */
  54. *gid = fspr_palloc(p, sidlen);
  55. domlen = sizeof(anydomain);
  56. rv = LookupAccountName(domain, groupname, *gid, &sidlen,
  57. anydomain, &domlen, &sidtype);
  58. }
  59. if (!sidlen || !rv) {
  60. return fspr_get_os_error();
  61. }
  62. return APR_SUCCESS;
  63. #endif
  64. }
  65. APR_DECLARE(fspr_status_t) fspr_gid_name_get(char **groupname, fspr_gid_t groupid, fspr_pool_t *p)
  66. {
  67. #ifdef _WIN32_WCE
  68. *groupname = fspr_pstrdup(p, "Administrators");
  69. #else
  70. SID_NAME_USE type;
  71. char name[MAX_PATH], domain[MAX_PATH];
  72. DWORD cbname = sizeof(name), cbdomain = sizeof(domain);
  73. if (!groupid)
  74. return APR_EINVAL;
  75. if (!LookupAccountSid(NULL, groupid, name, &cbname, domain, &cbdomain, &type))
  76. return fspr_get_os_error();
  77. if (type != SidTypeGroup && type != SidTypeWellKnownGroup
  78. && type != SidTypeAlias)
  79. return APR_EINVAL;
  80. *groupname = fspr_pstrdup(p, name);
  81. #endif
  82. return APR_SUCCESS;
  83. }
  84. APR_DECLARE(fspr_status_t) fspr_gid_compare(fspr_gid_t left, fspr_gid_t right)
  85. {
  86. if (!left || !right)
  87. return APR_EINVAL;
  88. #ifndef _WIN32_WCE
  89. if (!IsValidSid(left) || !IsValidSid(right))
  90. return APR_EINVAL;
  91. if (!EqualSid(left, right))
  92. return APR_EMISMATCH;
  93. #endif
  94. return APR_SUCCESS;
  95. }