6161#endif
6262
6363#include "catalog/catalog.h"
64+ #include "common/restricted_token.h"
6465#include "common/username.h"
6566#include "mb/pg_wchar.h"
6667#include "getaddrinfo.h"
@@ -178,9 +179,6 @@ static char *authwarning = NULL;
178179static const char * boot_options = "-F" ;
179180static const char * backend_options = "--single -F -O -c search_path=pg_catalog -c exit_on_error=true" ;
180181
181- #ifdef WIN32
182- char * restrict_env ;
183- #endif
184182static const char * subdirs [] = {
185183 "global" ,
186184 "pg_xlog" ,
@@ -260,7 +258,6 @@ static void check_locale_name(int category, const char *locale,
260258static bool check_locale_encoding (const char * locale , int encoding );
261259static void setlocales (void );
262260static void usage (const char * progname );
263- void get_restricted_token (void );
264261void setup_pgdata (void );
265262void setup_bin_paths (const char * argv0 );
266263void setup_data_file_paths (void );
@@ -272,12 +269,6 @@ void create_xlog_symlink(void);
272269void warn_on_mount_point (int error );
273270void initialize_data_directory (void );
274271
275-
276- #ifdef WIN32
277- static int CreateRestrictedProcess (char * cmd , PROCESS_INFORMATION * processInfo );
278- #endif
279-
280-
281272/*
282273 * macros for running pipes to postgres
283274 */
@@ -2754,116 +2745,6 @@ setlocales(void)
27542745#endif
27552746}
27562747
2757- #ifdef WIN32
2758- typedef BOOL (WINAPI * __CreateRestrictedToken ) (HANDLE , DWORD , DWORD , PSID_AND_ATTRIBUTES , DWORD , PLUID_AND_ATTRIBUTES , DWORD , PSID_AND_ATTRIBUTES , PHANDLE );
2759-
2760- /* Windows API define missing from some versions of MingW headers */
2761- #ifndef DISABLE_MAX_PRIVILEGE
2762- #define DISABLE_MAX_PRIVILEGE 0x1
2763- #endif
2764-
2765- /*
2766- * Create a restricted token and execute the specified process with it.
2767- *
2768- * Returns 0 on failure, non-zero on success, same as CreateProcess().
2769- *
2770- * On NT4, or any other system not containing the required functions, will
2771- * NOT execute anything.
2772- */
2773- static int
2774- CreateRestrictedProcess (char * cmd , PROCESS_INFORMATION * processInfo )
2775- {
2776- BOOL b ;
2777- STARTUPINFO si ;
2778- HANDLE origToken ;
2779- HANDLE restrictedToken ;
2780- SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY };
2781- SID_AND_ATTRIBUTES dropSids [2 ];
2782- __CreateRestrictedToken _CreateRestrictedToken = NULL ;
2783- HANDLE Advapi32Handle ;
2784-
2785- ZeroMemory (& si , sizeof (si ));
2786- si .cb = sizeof (si );
2787-
2788- Advapi32Handle = LoadLibrary ("ADVAPI32.DLL" );
2789- if (Advapi32Handle != NULL )
2790- {
2791- _CreateRestrictedToken = (__CreateRestrictedToken ) GetProcAddress (Advapi32Handle , "CreateRestrictedToken" );
2792- }
2793-
2794- if (_CreateRestrictedToken == NULL )
2795- {
2796- fprintf (stderr , _ ("%s: WARNING: cannot create restricted tokens on this platform\n" ), progname );
2797- if (Advapi32Handle != NULL )
2798- FreeLibrary (Advapi32Handle );
2799- return 0 ;
2800- }
2801-
2802- /* Open the current token to use as a base for the restricted one */
2803- if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ALL_ACCESS , & origToken ))
2804- {
2805- fprintf (stderr , _ ("%s: could not open process token: error code %lu\n" ), progname , GetLastError ());
2806- return 0 ;
2807- }
2808-
2809- /* Allocate list of SIDs to remove */
2810- ZeroMemory (& dropSids , sizeof (dropSids ));
2811- if (!AllocateAndInitializeSid (& NtAuthority , 2 ,
2812- SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_ADMINS , 0 , 0 , 0 , 0 , 0 ,
2813- 0 , & dropSids [0 ].Sid ) ||
2814- !AllocateAndInitializeSid (& NtAuthority , 2 ,
2815- SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_POWER_USERS , 0 , 0 , 0 , 0 , 0 ,
2816- 0 , & dropSids [1 ].Sid ))
2817- {
2818- fprintf (stderr , _ ("%s: could not allocate SIDs: error code %lu\n" ),
2819- progname , GetLastError ());
2820- return 0 ;
2821- }
2822-
2823- b = _CreateRestrictedToken (origToken ,
2824- DISABLE_MAX_PRIVILEGE ,
2825- sizeof (dropSids ) / sizeof (dropSids [0 ]),
2826- dropSids ,
2827- 0 , NULL ,
2828- 0 , NULL ,
2829- & restrictedToken );
2830-
2831- FreeSid (dropSids [1 ].Sid );
2832- FreeSid (dropSids [0 ].Sid );
2833- CloseHandle (origToken );
2834- FreeLibrary (Advapi32Handle );
2835-
2836- if (!b )
2837- {
2838- fprintf (stderr , _ ("%s: could not create restricted token: error code %lu\n" ), progname , GetLastError ());
2839- return 0 ;
2840- }
2841-
2842- #ifndef __CYGWIN__
2843- AddUserToTokenDacl (restrictedToken );
2844- #endif
2845-
2846- if (!CreateProcessAsUser (restrictedToken ,
2847- NULL ,
2848- cmd ,
2849- NULL ,
2850- NULL ,
2851- TRUE,
2852- CREATE_SUSPENDED ,
2853- NULL ,
2854- NULL ,
2855- & si ,
2856- processInfo ))
2857-
2858- {
2859- fprintf (stderr , _ ("%s: could not start process for command \"%s\": error code %lu\n" ), progname , cmd , GetLastError ());
2860- return 0 ;
2861- }
2862-
2863- return ResumeThread (processInfo -> hThread );
2864- }
2865- #endif
2866-
28672748/*
28682749 * print help text
28692750 */
@@ -2957,53 +2838,6 @@ check_need_password(const char *authmethodlocal, const char *authmethodhost)
29572838 }
29582839}
29592840
2960- void
2961- get_restricted_token (void )
2962- {
2963- #ifdef WIN32
2964-
2965- /*
2966- * Before we execute another program, make sure that we are running with a
2967- * restricted token. If not, re-execute ourselves with one.
2968- */
2969-
2970- if ((restrict_env = getenv ("PG_RESTRICT_EXEC" )) == NULL
2971- || strcmp (restrict_env , "1" ) != 0 )
2972- {
2973- PROCESS_INFORMATION pi ;
2974- char * cmdline ;
2975-
2976- ZeroMemory (& pi , sizeof (pi ));
2977-
2978- cmdline = pg_strdup (GetCommandLine ());
2979-
2980- putenv ("PG_RESTRICT_EXEC=1" );
2981-
2982- if (!CreateRestrictedProcess (cmdline , & pi ))
2983- {
2984- fprintf (stderr , _ ("%s: could not re-execute with restricted token: error code %lu\n" ), progname , GetLastError ());
2985- }
2986- else
2987- {
2988- /*
2989- * Successfully re-execed. Now wait for child process to capture
2990- * exitcode.
2991- */
2992- DWORD x ;
2993-
2994- CloseHandle (pi .hThread );
2995- WaitForSingleObject (pi .hProcess , INFINITE );
2996-
2997- if (!GetExitCodeProcess (pi .hProcess , & x ))
2998- {
2999- fprintf (stderr , _ ("%s: could not get exit code from subprocess: error code %lu\n" ), progname , GetLastError ());
3000- exit (1 );
3001- }
3002- exit (x );
3003- }
3004- }
3005- #endif
3006- }
30072841
30082842void
30092843setup_pgdata (void )
@@ -3759,7 +3593,7 @@ main(int argc, char *argv[])
37593593
37603594 check_need_password (authmethodlocal , authmethodhost );
37613595
3762- get_restricted_token ();
3596+ get_restricted_token (progname );
37633597
37643598 setup_pgdata ();
37653599
0 commit comments