:: On Thu, 08 Feb 2024 14:59:04 +0100
:: (it.comp.os.win.windows10)
:: <uq2mn5$20682$
1...@dont-email.me>
:: Guglielmo <mmemmoTO...@tiscali.it.invalid> wrote:
> Arrivano buoni ultimi dato che sono decenni che c'è chi ha portato un
> qualche clone di sudo anche su Windows.
io me ne sono scritto uno, estremamente semplice in "C", eccolo
/*
cl -Os sudo.c
del sudo.obj
exit
***********************
to compile:
type sudo.c|cmd.exe
***********************
*/
#define VC_EXTRALEAN
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <windows.h>
#pragma comment(lib,"shell32")
#pragma comment(lib,"advapi32")
#define PATHSIZ (MAX_PATH*2)
#define RUN_ADM "runas"
//#define DBG_FLAG
/*
** retrieve current directory
*/
LPSTR getCurrDir()
{
LPSTR lpCurrDir = NULL;
DWORD dwSize = 0;
// retrieve the current directory full path
dwSize = GetCurrentDirectory(0, lpCurrDir);
if (0 == dwSize) return(NULL);
dwSize+=5;
lpCurrDir = (LPSTR)calloc(sizeof(UCHAR), dwSize);
if (NULL == lpCurrDir) return(NULL);
if (0 == GetCurrentDirectory(dwSize, lpCurrDir))
{
free(lpCurrDir);
return(NULL);
}
strcat(lpCurrDir, "\\");
return(lpCurrDir);
}
/*
** check if our process is elevated
*/
BOOL IsProcessElevated()
{
BOOL fIsElevated = FALSE;
HANDLE hToken = NULL;
TOKEN_ELEVATION elevation;
DWORD dwSize;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
if (GetTokenInformation(hToken, TokenElevation, &elevation,
sizeof(elevation), &dwSize))
{
fIsElevated = elevation.TokenIsElevated;
}
}
if (hToken)
{
CloseHandle(hToken);
hToken = NULL;
}
return(fIsElevated);
}
/*
** execute command with elevated privileges
*/
BOOL shExec(LPSTR lpProg, LPSTR lpArgs, LPSTR lpCurrDir)
{
SHELLEXECUTEINFO ShExecInfo = {0};
BOOL bRet, bElevated;
bElevated = IsProcessElevated();
#ifdef DBG_FLAG
fprintf(stderr, "lpProg = %s\n", lpProg);
fprintf(stderr, "lpArgs = %s\n", lpArgs);
fprintf(stderr, "lpCurrDir = %s\n", lpCurrDir);
fprintf(stderr, "bElevated = %d\n", bElevated);
#endif
// setup the shellexecute infos
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_DEFAULT;
ShExecInfo.hwnd = NULL;
if (bElevated)
{
ShExecInfo.lpVerb = NULL;
} else {
ShExecInfo.lpVerb = RUN_ADM;
}
ShExecInfo.lpFile = lpProg;
ShExecInfo.lpParameters = lpArgs;
ShExecInfo.lpDirectory = lpCurrDir;
ShExecInfo.nShow = SW_SHOWNORMAL;
ShExecInfo.hInstApp = NULL;
// starts the program requesting privileges elevation
bRet = ShellExecuteEx(&ShExecInfo);
return(bRet);
}
/*
** main entry point
*/
DWORD main(int argc, char *argv[])
{
LPSTR cmdLine = NULL;
LPSTR cmdProg = NULL;
LPSTR cmdArgs = NULL;
LPSTR lpCurrDir = NULL;
LPSTR pPos = NULL;
DWORD dwErr = 0;
// check if args ok
if (argc==1)
{
fprintf(stderr, "usage: %s program [arg1 ... argN]\n", argv[0]);
fprintf(stderr, "e.g. : %s notepad c:\\foo\\bar.txt\n\n", argv[0]);
return(1);
}
// fetch the full command line and extract program and arguments
cmdLine = GetCommandLine();
if (NULL != (pPos = strchr(cmdLine, ' ')))
{
cmdProg = 2+pPos;
if (NULL != (pPos = strchr(cmdProg, ' ')))
{
*pPos = '\0';
cmdArgs = ++pPos;
}
} else {
fprintf(stderr, "%s: invalid arguments\n", argv[0]);
return(2);
}
// retrieve the current directory
if (NULL == (lpCurrDir = getCurrDir()))
{
dwErr = GetLastError();
fprintf(stderr, "%s: GetCurrentDirectory() error 0x%04X\n",
argv[0], dwErr);
return(dwErr);
}
// starts the program requesting privileges elevation
if (FALSE == shExec(cmdProg, cmdArgs, lpCurrDir))
{
dwErr = GetLastError();
fprintf(stderr, "%s: ShellExecuteEx error 0x%04X\n", argv[0],
dwErr);
}
// all done
free(cmdLine);
free(lpCurrDir);
// zero if all ok
return(dwErr);
}