|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * security.c |
| 4 | + * Microsoft Windows Win32 Security Support Functions |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * $PostgreSQL: pgsql/src/backend/port/win32/security.c,v 1.1 2004/06/24 21:02:42 tgl Exp $ |
| 10 | + * |
| 11 | + *------------------------------------------------------------------------- |
| 12 | + */ |
| 13 | + |
| 14 | +#include "postgres.h" |
| 15 | + |
| 16 | + |
| 17 | +/* |
| 18 | + * Returns nonzero if the current user has administrative privileges, |
| 19 | + * or zero if not. |
| 20 | + * |
| 21 | + * Note: this cannot use ereport() because it's called too early during |
| 22 | + * startup. |
| 23 | + */ |
| 24 | +int |
| 25 | +pgwin32_is_admin(void) |
| 26 | +{ |
| 27 | + HANDLE AccessToken; |
| 28 | + UCHAR InfoBuffer[1024]; |
| 29 | + PTOKEN_GROUPS Groups = (PTOKEN_GROUPS)InfoBuffer; |
| 30 | + DWORD InfoBufferSize; |
| 31 | + PSID AdministratorsSid; |
| 32 | + PSID PowerUsersSid; |
| 33 | + SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY }; |
| 34 | + UINT x; |
| 35 | + BOOL success; |
| 36 | + |
| 37 | + if(!OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&AccessToken)) |
| 38 | + { |
| 39 | + write_stderr("failed to open process token: %d\n", |
| 40 | + (int)GetLastError()); |
| 41 | + exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + if (!GetTokenInformation(AccessToken,TokenGroups,InfoBuffer, |
| 45 | + 1024, &InfoBufferSize)) |
| 46 | + { |
| 47 | + write_stderr("failed to get token information: %d\n", |
| 48 | + (int)GetLastError()); |
| 49 | + exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + CloseHandle(AccessToken); |
| 53 | + |
| 54 | + if(!AllocateAndInitializeSid(&NtAuthority, 2, |
| 55 | + SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, |
| 56 | + 0,&AdministratorsSid)) |
| 57 | + { |
| 58 | + write_stderr("failed to get SID for Administrators group: %d\n", |
| 59 | + (int)GetLastError()); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + if (!AllocateAndInitializeSid(&NtAuthority, 2, |
| 64 | + SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0, |
| 65 | + 0, &PowerUsersSid)) |
| 66 | + { |
| 67 | + write_stderr("failed to get SID for PowerUsers group: %d\n", |
| 68 | + (int)GetLastError()); |
| 69 | + exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + success = FALSE; |
| 73 | + |
| 74 | + for (x=0; x<Groups->GroupCount; x++) |
| 75 | + { |
| 76 | + if (EqualSid(AdministratorsSid, Groups->Groups[x].Sid) || |
| 77 | + EqualSid(PowerUsersSid, Groups->Groups[x].Sid)) |
| 78 | + { |
| 79 | + success = TRUE; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + FreeSid(AdministratorsSid); |
| 85 | + FreeSid(PowerUsersSid); |
| 86 | + return success; |
| 87 | +} |
| 88 | + |
| 89 | +/* |
| 90 | + * We consider ourselves running as a service if one of the following is |
| 91 | + * true: |
| 92 | + * |
| 93 | + * 1) We are running as Local System (only used by services) |
| 94 | + * 2) Our token contains SECURITY_SERVICE_RID (automatically added to the |
| 95 | + * process token by the SCM when starting a service) |
| 96 | + * |
| 97 | + * Return values: |
| 98 | + * 0 = Not service |
| 99 | + * 1 = Service |
| 100 | + * -1 = Error |
| 101 | + * |
| 102 | + * Note: we can't report errors via either ereport (we're called too early) |
| 103 | + * or write_stderr (because that calls this). We are therefore reduced to |
| 104 | + * writing directly on stderr, which sucks, but we have few alternatives. |
| 105 | + */ |
| 106 | +int |
| 107 | +pgwin32_is_service(void) |
| 108 | +{ |
| 109 | + static int _is_service = -1; |
| 110 | + HANDLE AccessToken; |
| 111 | + UCHAR InfoBuffer[1024]; |
| 112 | + PTOKEN_GROUPS Groups = (PTOKEN_GROUPS)InfoBuffer; |
| 113 | + PTOKEN_USER User = (PTOKEN_USER)InfoBuffer; |
| 114 | + DWORD InfoBufferSize; |
| 115 | + PSID ServiceSid; |
| 116 | + PSID LocalSystemSid; |
| 117 | + SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY }; |
| 118 | + UINT x; |
| 119 | + |
| 120 | + /* Only check the first time */ |
| 121 | + if (_is_service != -1) |
| 122 | + return _is_service; |
| 123 | + |
| 124 | + if (!OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&AccessToken)) { |
| 125 | + fprintf(stderr,"failed to open process token: %d\n", |
| 126 | + (int)GetLastError()); |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + /* First check for local system */ |
| 131 | + if (!GetTokenInformation(AccessToken,TokenUser,InfoBuffer,1024,&InfoBufferSize)) { |
| 132 | + fprintf(stderr,"failed to get token information: %d\n", |
| 133 | + (int)GetLastError()); |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + if (!AllocateAndInitializeSid(&NtAuthority,1, |
| 138 | + SECURITY_LOCAL_SYSTEM_RID,0,0,0,0,0,0,0, |
| 139 | + &LocalSystemSid)) { |
| 140 | + fprintf(stderr,"failed to get SID for local system account\n"); |
| 141 | + CloseHandle(AccessToken); |
| 142 | + return -1; |
| 143 | + } |
| 144 | + |
| 145 | + if (EqualSid(LocalSystemSid, User->User.Sid)) { |
| 146 | + FreeSid(LocalSystemSid); |
| 147 | + CloseHandle(AccessToken); |
| 148 | + _is_service = 1; |
| 149 | + return _is_service; |
| 150 | + } |
| 151 | + |
| 152 | + FreeSid(LocalSystemSid); |
| 153 | + |
| 154 | + /* Now check for group SID */ |
| 155 | + if (!GetTokenInformation(AccessToken,TokenGroups,InfoBuffer,1024,&InfoBufferSize)) { |
| 156 | + fprintf(stderr,"failed to get token information: %d\n", |
| 157 | + (int)GetLastError()); |
| 158 | + return -1; |
| 159 | + } |
| 160 | + |
| 161 | + if (!AllocateAndInitializeSid(&NtAuthority,1, |
| 162 | + SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, |
| 163 | + &ServiceSid)) { |
| 164 | + fprintf(stderr,"failed to get SID for service group\n"); |
| 165 | + CloseHandle(AccessToken); |
| 166 | + return -1; |
| 167 | + } |
| 168 | + |
| 169 | + _is_service = 0; |
| 170 | + for (x = 0; x < Groups->GroupCount; x++) |
| 171 | + { |
| 172 | + if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) |
| 173 | + { |
| 174 | + _is_service = 1; |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + FreeSid(ServiceSid); |
| 180 | + |
| 181 | + CloseHandle(AccessToken); |
| 182 | + |
| 183 | + return _is_service; |
| 184 | +} |
0 commit comments