- /*
- * sidunixuser1.c - test for Unix_User+$UID SIDs
- *
- *
- * Test with:
- * $ gcc -g -Wall sidunixuser1.c -o sidunixuser1 && gdb -ex=r --args ./sidunixuser1
- *
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sddl.h>
- /*
- * Allocate a SID from SECURITY_SAMBA_UNIX_AUTHORITY, which encodes an
- * UNIX/POSIX uid directly into a SID.
- *
- * Example:
- * UID 1616 gets mapped to "Unix_User+1616", encoding the UID into the
- * SID as "S-1-22-1-1616"
- * $ getent passwd Unix_User+1616
- * Unix_User+1616:*:4278191696:4278191696:U-Unix_User\1616,S-1-22-1-1616:/:/sbin/nologin
- *
- */
- /* S-1-22-1-0 for uid 0 */
- #define SECURITY_SAMBA_UNIX_AUTHORITY { { 0,0,0,0,0,22 } }
- SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
- BOOL AllocateUnixUserSID(unsigned long uid, PSID *pSid)
- {
- PSID sid = NULL;
- PSID malloced_sid = NULL;
- DWORD sid_len;
- if (AllocateAndInitializeSid(&sid_id_auth, 2, 1, (DWORD)uid, 0, 0, 0, 0, 0, 0, &sid)) {
- sid_len = GetLengthSid(sid);
- if (malloced_sid) {
- /*
- * |AllocateAndInitializeSid()| has an own memory
- * allocator, but we need the sid in memory from
- * |malloc()|
- */
- if (CopySid(sid_len, malloced_sid, sid)) {
- FreeSid(sid);
- *pSid = malloced_sid;
- uid, (long)sid_len);
- return TRUE;
- }
- }
- }
- FreeSid(sid);
- uid, GetLastError());
- return FALSE;
- }
- int fput_sidstr(FILE *f, PSID *sid)
- {
- LPSTR string_sid = NULL;
- int res;
- if (!ConvertSidToStringSidA(*sid, &string_sid)) {
- return -1;
- }
- LocalFree(string_sid);
- return res;
- }
- int main(int ac, char *av[])
- {
- // S-1-22-1-1
- PSID sid = NULL;
- PSID new_sid = NULL;
- LPSTR string_sid = NULL;
- #if 1
- // Create a new SID with the given ID authority and no sub-authorities
- if (!AllocateUnixUserSID(1616, &sid)) {
- return 1;
- }
- (void)fput_sidstr(stdout, &sid);
- sid = NULL;
- // Create a new SID with the given ID authority and no sub-authorities
- if (!AllocateUnixUserSID(1818, &sid)) {
- return 1;
- }
- (void)fput_sidstr(stdout, &sid);
- // Stringify and print
- if (!ConvertSidToStringSidA(sid, &string_sid)) {
- return 2;
- }
- // Destringify and print
- if (ConvertStringSidToSidA(string_sid, &new_sid)) {
- }
- else {
- }
- #endif
- /* Part 2 */
- LocalFree(string_sid);
- LocalFree(new_sid);
- sid = NULL;
- new_sid = NULL;
- string_sid = NULL;
- char sid_unix_user_buf[64];
- if (ConvertStringSidToSidA(sid_unix_user_buf, &sid)) {
- }
- else {
- }
- if (!ConvertSidToStringSidA(sid, &string_sid)) {
- FreeSid(sid);
- return 2;
- }
- // Destringify and print
- if (ConvertStringSidToSidA(string_sid, &new_sid)) {
- }
- else {
- }
- if (!ConvertSidToStringSidA(new_sid, &string_sid)) {
- FreeSid(sid);
- return 2;
- }
- // Clean up
- LocalFree(string_sid);
- LocalFree(new_sid);
- FreeSid(sid);
- return EXIT_SUCCESS;
- }
sidunixuser1.c
Posted by Anonymous on Thu 5th Oct 2023 09:10
raw | new post
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.