bsdtar_cygwin.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*-
  2. * Copyright (c) 2009 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD$
  26. */
  27. #if defined(__CYGWIN__)
  28. #define _WIN32_WINNT 0x0500
  29. #define WINVER 0x0500
  30. #include "bsdtar_platform.h"
  31. #include <errno.h>
  32. #include <stddef.h>
  33. #include <sys/utime.h>
  34. #include <sys/stat.h>
  35. #include <process.h>
  36. #include <stdlib.h>
  37. #include <wchar.h>
  38. #include <windows.h>
  39. #include <sddl.h>
  40. #include "bsdtar.h"
  41. #ifndef LIST_H
  42. static int
  43. _is_privileged(HANDLE thandle, const char *sidlist[])
  44. {
  45. TOKEN_USER *tuser;
  46. TOKEN_GROUPS *tgrp;
  47. DWORD bytes;
  48. PSID psid;
  49. DWORD i, g;
  50. int member;
  51. psid = NULL;
  52. tuser = NULL;
  53. tgrp = NULL;
  54. member = 0;
  55. for (i = 0; sidlist[i] != NULL && member == 0; i++) {
  56. if (psid != NULL)
  57. LocalFree(psid);
  58. /* mingw/cygwin: incorrectly prototypes arg 1 as LPSTR
  59. * instead of LPCSTR. Work around it here
  60. */
  61. if (ConvertStringSidToSidA((char *)sidlist[i], &psid) == 0) {
  62. errno = EPERM;
  63. return (-1);
  64. }
  65. if (tuser == NULL) {
  66. GetTokenInformation(thandle, TokenUser, NULL, 0, &bytes);
  67. tuser = malloc(bytes);
  68. if (tuser == NULL) {
  69. errno = ENOMEM;
  70. member = -1;
  71. break;
  72. }
  73. if (GetTokenInformation(thandle, TokenUser, tuser, bytes, &bytes) == 0) {
  74. errno = EPERM;
  75. member = -1;
  76. break;
  77. }
  78. }
  79. member = EqualSid(tuser->User.Sid, psid);
  80. if (member)
  81. break;
  82. if (tgrp == NULL) {
  83. GetTokenInformation(thandle, TokenGroups, NULL, 0, &bytes);
  84. tgrp = malloc(bytes);
  85. if (tgrp == NULL) {
  86. errno = ENOMEM;
  87. member = -1;
  88. break;
  89. }
  90. if (GetTokenInformation(thandle, TokenGroups, tgrp, bytes, &bytes) == 0) {
  91. errno = EPERM;
  92. member = -1;
  93. break;
  94. }
  95. }
  96. for (g = 0; g < tgrp->GroupCount; g++) {
  97. member = EqualSid(tgrp->Groups[g].Sid, psid);
  98. if (member)
  99. break;
  100. }
  101. }
  102. LocalFree(psid);
  103. free(tuser);
  104. free(tgrp);
  105. return (member);
  106. }
  107. int
  108. bsdtar_is_privileged(struct bsdtar *bsdtar)
  109. {
  110. HANDLE thandle;
  111. int ret;
  112. const char *sidlist[] = {
  113. "S-1-5-32-544", /* Administrators */
  114. "S-1-5-32-551", /* Backup Operators */
  115. NULL
  116. };
  117. (void)bsdtar;/* UNUSED */
  118. if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &thandle) == 0) {
  119. bsdtar_warnc(bsdtar, EPERM, "Failed to check privilege");
  120. return (0);
  121. }
  122. ret = _is_privileged(thandle, sidlist);
  123. if (ret < 0) {
  124. bsdtar_warnc(bsdtar, errno, "Failed to check privilege");
  125. return (0);
  126. }
  127. return (ret);
  128. }
  129. #endif /* LIST_H */
  130. #endif