Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active July 29, 2021 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou1okada/76b6ec985464dc509edf75d19594e251 to your computer and use it in GitHub Desktop.
Save kou1okada/76b6ec985464dc509edf75d19594e251 to your computer and use it in GitHub Desktop.
wautils: Windows Account Utilities
/**
* LookupAccountName
* Copyright (c) 2018 Koichi OKADA. All rights reserved.
* This source code is distributed under the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <sddl.h>
int main(int argc, char *argv[])
{
int exitcode = EXIT_FAILURE;
BOOL result;
PSID Sid = NULL;
DWORD cbSid;
DWORD cchReferencedDomainName;
LPSTR ReferencedDomainName = NULL;
SID_NAME_USE eUse;
LPSTR StringSid = NULL;
do {
if (argc < 2) {
fprintf(stderr, "Usage: LookupAccountName SystemName AccountName\n\n");
break;
}
result = LookupAccountName(
argv[1], // LPCSTR lpSystemName,
argv[2], // LPCSTR lpAccountName,
NULL, // PSID Sid,
&cbSid, // LPDWORD cbSid,
NULL, // LPSTR ReferencedDomainName,
&cchReferencedDomainName, // LPDWORD cchReferencedDomainName,
&eUse // PSID_NAME_USE peUse
);
Sid = malloc(cbSid);
ReferencedDomainName = malloc(cchReferencedDomainName);
result = LookupAccountName(
argv[1], // LPCSTR lpSystemName,
argv[2], // LPCSTR lpAccountName,
Sid, // PSID Sid,
&cbSid, // LPDWORD cbSid,
ReferencedDomainName, // LPSTR ReferencedDomainName,
&cchReferencedDomainName, // LPDWORD cchReferencedDomainName,
&eUse // PSID_NAME_USE peUse
);
if (result == 0) {
fprintf(stderr, "Error: LookupAccountName failed.\n");
break;
}
result = ConvertSidToStringSid(
Sid, // PSID Sid,
&StringSid // LPSTR *StringSid
);
if (result == 0) {
fprintf(stderr, "Error: ConvertSidToStringSid failed.\n");
break;
}
printf("ReferencedDomainName : %s\n", ReferencedDomainName);
printf("SystemName : %s\n", argv[1]);
printf("AccountName : %s\n", argv[2]);
printf("SID : %s\n", StringSid);
exitcode = EXIT_SUCCESS;
} while(0);
if (Sid) free(Sid);
if (ReferencedDomainName) free(ReferencedDomainName);
if (StringSid) LocalFree(StringSid);
return exitcode;
}
/**
* LookupAccountSid
* Copyright (c) 2018 Koichi OKADA. All rights reserved.
* This source code is distributed under the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <sddl.h>
int main(int argc, char *argv[])
{
int exitcode = EXIT_FAILURE;
BOOL result;
PSID Sid = NULL;
LPSTR Name = NULL;
DWORD cchName;
DWORD cchReferencedDomainName;
LPSTR ReferencedDomainName = NULL;
SID_NAME_USE eUse;
do {
if (argc < 2) {
fprintf(stderr, "Usage: LookupAccountSid SystemName Sid\n\n");
break;
}
result = ConvertStringSidToSid(
argv[2], // LPCSTR StringSid,
&Sid // PSID *Sid
);
if (result == 0) {
fprintf(stderr, "Error: ConvertStringSidToSid failed.\n");
break;
}
result = LookupAccountSid(
argv[1], // LPCSTR lpSystemName,
Sid, // PSID Sid,
Name, // LPSTR Name,
&cchName, // LPDWORD cchName,
ReferencedDomainName, // LPSTR ReferencedDomainName,
&cchReferencedDomainName, // LPDWORD cchReferencedDomainName,
&eUse // PSID_NAME_USE peUse
);
Name = malloc(cchName);
ReferencedDomainName = malloc(cchReferencedDomainName);
result = LookupAccountSid(
argv[1], // LPCSTR lpSystemName,
Sid, // PSID Sid,
Name, // LPSTR Name,
&cchName, // LPDWORD cchName,
ReferencedDomainName, // LPSTR ReferencedDomainName,
&cchReferencedDomainName, // LPDWORD cchReferencedDomainName,
&eUse // PSID_NAME_USE peUse
);
if (result == 0) {
fprintf(stderr, "Error: LookupAccountSid failed.\n");
break;
}
printf("SystemName : %s\n", argv[1]);
printf("SID : %s\n", argv[2]);
printf("ReferencedDomainName : %s\n", ReferencedDomainName);
printf("AccountName : %s\n", Name);
exitcode = EXIT_SUCCESS;
} while(0);
if (Sid) LocalFree(Sid);
if (Name) free(Name);
if (ReferencedDomainName) free(ReferencedDomainName);
return exitcode;
}
TARGETS = LookupAccountName LookupAccountSid
all: $(TARGETS)
LookupAccountName: LookupAccountName.c
$(CC) -o $@ $<
LookupAccountSid: LookupAccountSid.c
$(CC) -o $@ $<
clean:
$(RM) $(TARGETS)
.PHONY: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment