postgresql/src/port/win32security.c
Tom Lane eacbe94ab1 Clean up minor inconsistencies in pg_attribute_printf() usage.
For some reason we'd never decorated pg_v*printf() with
pg_attribute_printf() annotations.  There is a convention for
how to label va_list-using printf functions (write zero for the
second argument), and we use that liberally elsewhere in the
code, but these core functions lacked it.  It's not clear how
much useful checking the compiler can do for calls of these,
but we might as well add the annotations.

Also, sync win32security.c's log_error() with our normal convention
that pg_attribute_printf must be attached to a function's declaration
not definition.  Apparently this file is only compiled with compilers
that aren't picky about that, but still it'd be better to be
consistent.

No back-patch since there's little reason to think we would catch
anything.

Discussion: https://postgr.es/m/3492412.1663283395@sss.pgh.pa.us
2022-09-16 11:10:48 -04:00

191 lines
4.6 KiB
C

/*-------------------------------------------------------------------------
*
* win32security.c
* Microsoft Windows Win32 Security Support Functions
*
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/port/win32security.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
static void log_error(const char *fmt,...) pg_attribute_printf(1, 2);
/*
* Utility wrapper for frontend and backend when reporting an error
* message.
*/
static void
log_error(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
#ifndef FRONTEND
write_stderr(fmt, ap);
#else
fprintf(stderr, fmt, ap);
#endif
va_end(ap);
}
/*
* Returns nonzero if the current user has administrative privileges,
* or zero if not.
*
* Note: this cannot use ereport() because it's called too early during
* startup.
*/
int
pgwin32_is_admin(void)
{
PSID AdministratorsSid;
PSID PowerUsersSid;
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
BOOL IsAdministrators;
BOOL IsPowerUsers;
if (!AllocateAndInitializeSid(&NtAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
0, &AdministratorsSid))
{
log_error(_("could not get SID for Administrators group: error code %lu\n"),
GetLastError());
exit(1);
}
if (!AllocateAndInitializeSid(&NtAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
0, &PowerUsersSid))
{
log_error(_("could not get SID for PowerUsers group: error code %lu\n"),
GetLastError());
exit(1);
}
if (!CheckTokenMembership(NULL, AdministratorsSid, &IsAdministrators) ||
!CheckTokenMembership(NULL, PowerUsersSid, &IsPowerUsers))
{
log_error(_("could not check access token membership: error code %lu\n"),
GetLastError());
exit(1);
}
FreeSid(AdministratorsSid);
FreeSid(PowerUsersSid);
if (IsAdministrators || IsPowerUsers)
return 1;
else
return 0;
}
/*
* We consider ourselves running as a service if one of the following is
* true:
*
* 1) Standard error is not valid (always the case for services, and pg_ctl
* running as a service "passes" that down to postgres,
* c.f. CreateRestrictedProcess())
* 2) We are running as LocalSystem (only used by services)
* 3) Our token contains SECURITY_SERVICE_RID (automatically added to the
* process token by the SCM when starting a service)
*
* The check for LocalSystem is needed, because surprisingly, if a service
* is running as LocalSystem, it does not have SECURITY_SERVICE_RID in its
* process token.
*
* Return values:
* 0 = Not service
* 1 = Service
* -1 = Error
*
* Note: we can't report errors via either ereport (we're called too early
* in the backend) or write_stderr (because that calls this). We are
* therefore reduced to writing directly on stderr, which sucks, but we
* have few alternatives.
*/
int
pgwin32_is_service(void)
{
static int _is_service = -1;
BOOL IsMember;
PSID ServiceSid;
PSID LocalSystemSid;
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
HANDLE stderr_handle;
/* Only check the first time */
if (_is_service != -1)
return _is_service;
/* Check if standard error is not valid */
stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
if (stderr_handle != INVALID_HANDLE_VALUE && stderr_handle != NULL)
{
_is_service = 0;
return _is_service;
}
/* Check if running as LocalSystem */
if (!AllocateAndInitializeSid(&NtAuthority, 1,
SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0,
&LocalSystemSid))
{
fprintf(stderr, "could not get SID for local system account\n");
return -1;
}
if (!CheckTokenMembership(NULL, LocalSystemSid, &IsMember))
{
fprintf(stderr, "could not check access token membership: error code %lu\n",
GetLastError());
FreeSid(LocalSystemSid);
return -1;
}
FreeSid(LocalSystemSid);
if (IsMember)
{
_is_service = 1;
return _is_service;
}
/* Check for service group membership */
if (!AllocateAndInitializeSid(&NtAuthority, 1,
SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0,
&ServiceSid))
{
fprintf(stderr, "could not get SID for service group: error code %lu\n",
GetLastError());
return -1;
}
if (!CheckTokenMembership(NULL, ServiceSid, &IsMember))
{
fprintf(stderr, "could not check access token membership: error code %lu\n",
GetLastError());
FreeSid(ServiceSid);
return -1;
}
FreeSid(ServiceSid);
if (IsMember)
_is_service = 1;
else
_is_service = 0;
return _is_service;
}