Getting Windows "MACHINE SID" without fork() & exec()?
Greywolf
greywolf@starwolf.com
Tue Apr 28 18:15:25 GMT 2026
On 2026-04-24 05:11, Corinna Vinschen via Cygwin wrote:
> On Apr 23 19:07, Takeshi Nishimura via Cygwin wrote:
>> Does Cygwin have a secret shell variable or /proc file which contains
>> the current machine's MACHINE SID, without having to resort to calling
>> an external program (no fork(), no exec(), please)?
>
> You can use your own simple tool in plain C:
>
> $ cat > adom.c <<EOF
> #include <stdio.h>
> #include <windows.h>
> #include <ntsecapi.h>
> #include <ntstatus.h>
> #include <sddl.h>
>
> int
> main ()
> {
> LSA_OBJECT_ATTRIBUTES oa = { 0, 0, 0, 0, 0, 0 };
> PPOLICY_ACCOUNT_DOMAIN_INFO adom;
> NTSTATUS status;
> LPWSTR sidstr;
> HANDLE lsa;
>
> status = LsaOpenPolicy(NULL, &oa, POLICY_VIEW_LOCAL_INFORMATION, &lsa);
> if (status == STATUS_SUCCESS)
> {
> status = LsaQueryInformationPolicy (lsa, PolicyAccountDomainInformation,
> (PVOID *) &adom);
> if (status == STATUS_SUCCESS)
> {
> ConvertSidToStringSidW (adom->DomainSid, &sidstr);
> printf ("%ls\n", sidstr);
> LocalFree (sidstr);
> }
> LsaFreeMemory (adom);
> LsaClose (lsa);
> }
> return 0;
> }
> EOF
> $ gcc -o adom adom.c
> $ ./adom
> S-1-5-21-2941109648-2854651930-1480659642
>
>
> Corinna
>
...conversely, what's wrong with getting your machine's SID and stashing it
in, say, /etc/machine-sid or /etc/sid or as a variable somewhere in
/etc/profile.d/machine-sid.sh, i.e.
MACHINE_SID="thisisth-esid-fory-ouro-wncomputer00"
If you have a slow-ish dynamic method, cron it to run once a day or something,
but, honestly: How often does your machine's SID change that you need to
fetch it as a dynamic object on a regular basis?
--
--*greywolf;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0x2C80F168914E2DF6.asc
Type: application/pgp-keys
Size: 3090 bytes
Desc: OpenPGP public key
URL: <https://cygwin.com/pipermail/cygwin/attachments/20260428/abfa6046/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://cygwin.com/pipermail/cygwin/attachments/20260428/abfa6046/attachment-0001.sig>
More information about the Cygwin
mailing list