Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pure Windows suda

8 views
Skip to first unread message

brianks®

unread,
Feb 8, 2024, 5:09:27 AMFeb 8
to
è stato scoperto il comando Sudo su Windows.

https://files.catbox.moe/yx78dq.png

Microsoft ha pubblicato un po' prematuramente l'articolo su Sudo su
Windows, ma poi lo ha immediatamente ritirato.
È stato annunciato per il prossimo Canary 26052.
La cosa interessante è che Sudo apparirà anche come open source su GitHub.

Guglielmo

unread,
Feb 8, 2024, 8:59:03 AMFeb 8
to
Il 08/02/2024, brianks® ha detto :
Arrivano buoni ultimi dato che sono decenni che c'è chi ha portato un
qualche clone di sudo anche su Windows.

Esempio:
https://github.com/gerardog/gsudo?tab=readme-ov-file

ObiWan

unread,
Feb 8, 2024, 9:20:47 AMFeb 8
to
:: 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);
}




ObiWan

unread,
Feb 8, 2024, 10:50:04 AMFeb 8
to
:: On Thu, 8 Feb 2024 15:20:45 +0100
:: (it.comp.os.win.windows10)
:: <20240208152...@mvps.org>
:: ObiWan <obi...@mvps.org> wrote:

> io me ne sono scritto uno, estremamente semplice in "C", eccolo

Poi ok, quello mi serviva per fare alcune cose, ma è lontano da essere
paragonabile al "sudo" di Linux, del resto la shell di windows è molto
diversa, per fare un esempio, se in Linux si lancia un comando tipo

cat file01|grep -i '0x'|more

i comandi sono eseguiti "in parallelo", ossia man mano che "cat" invia
output, questo viene elaborato da "grep" e passato a "more", la cosa è
diversa in windows, se lanciamo un comando equivalente, ossia

type file01|findstr /I "0x"|more

la type viene elaborata e SOLO alla fine l'output viene inviato a
"findstr" e, di nuovo, solo alla fine di questo il risultato viene
inviato a "more", in pratica l'elaborazione è sequenziale e non
parallela come avviene in Linux/Unix




brianks®

unread,
Feb 9, 2024, 4:00:01 AMFeb 9
to
0 new messages