2826
}
2827
}
2828
2829
-static PSID get_current_user_sid(void)
2829
+static void get_current_user_sid(PSID *sid, HANDLE *linked_token)
2830
{
2831
HANDLE token;
2832
DWORD len = 0;
2833
- PSID result = NULL;
2833
+ TOKEN_ELEVATION_TYPE elevationType;
2834
+ DWORD size;
2835
+
2836
+ *sid = NULL;
2837
+ *linked_token = NULL;
2838
2839
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
2836
- return NULL;
2840
+ return;
2841
2842
if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
2843
TOKEN_USER *info = xmalloc((size_t)len);
2844
if (GetTokenInformation(token, TokenUser, info, len, &len)) {
2845
len = GetLengthSid(info->User.Sid);
2842
- result = xmalloc(len);
2843
- if (!CopySid(len, result, info->User.Sid)) {
2846
+ *sid = xmalloc(len);
2847
+ if (!CopySid(len, *sid, info->User.Sid)) {
2848
error(_("failed to copy SID (%ld)"),
2849
GetLastError());
2846
- FREE_AND_NULL(result);
2850
+ FREE_AND_NULL(*sid);
2851
}
2852
}
2853
FREE_AND_NULL(info);
2854
}
2851
- CloseHandle(token);
2855
2853
- return result;
2856
+ if (GetTokenInformation(token, TokenElevationType, &elevationType, sizeof(elevationType), &size) &&
2857
+ elevationType == TokenElevationTypeLimited) {
2858
+ /*
2859
+ * The current process is run by a member of the Administrators
2860
+ * group, but is not running elevated.
2861
+ */
2862
+ if (!GetTokenInformation(token, TokenLinkedToken, linked_token, sizeof(*linked_token), &size))
2863
+ linked_token = NULL; /* there is no linked token */
2864
+ }
2865
+
2866
+ CloseHandle(token);
2867
}
2868
2869
static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
2944
else if (sid && IsValidSid(sid)) {
2945
/* Now, verify that the SID matches the current user's */
2946
static PSID current_user_sid;
2947
+ static HANDLE linked_token;
2948
BOOL is_member;
2949
2950
if (!current_user_sid)
2937
- current_user_sid = get_current_user_sid();
2951
+ get_current_user_sid(¤t_user_sid, &linked_token);
2952
2953
if (current_user_sid &&
2954
IsValidSid(current_user_sid) &&
2955
EqualSid(sid, current_user_sid))
2956
result = 1;
2957
else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) &&
2944
- CheckTokenMembership(NULL, sid, &is_member) &&
2945
- is_member)
2958
+ ((CheckTokenMembership(NULL, sid, &is_member) &&
2959
+ is_member) ||
2960
+ (linked_token &&
2961
+ CheckTokenMembership(linked_token, sid, &is_member) &&
2962
+ is_member)))
2963
/*
2964
* If owned by the Administrators group, and the
2965
* current user is an administrator, we consider that