pastebin - collaborative debugging tool
rovema.kpaste.net RSS


sidunixuser1.c
Posted by Anonymous on Thu 5th Oct 2023 09:10
raw | new post
modification of post by Anonymous (view diff)

  1. /*
  2.  * sidunixuser1.c - test for Unix_User+$UID SIDs
  3.  *
  4.  *
  5.  * Test with:
  6.  * $  gcc -g -Wall sidunixuser1.c -o sidunixuser1 && gdb -ex=r --args ./sidunixuser1
  7.  *
  8.  *
  9.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  10.  */
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <sddl.h>
  16.  
  17. /*
  18.  * Allocate a SID from SECURITY_SAMBA_UNIX_AUTHORITY, which encodes an
  19.  * UNIX/POSIX uid directly into a SID.
  20.  *
  21.  * Example:
  22.  * UID 1616 gets mapped to "Unix_User+1616", encoding the UID into the
  23.  * SID as "S-1-22-1-1616"
  24.  * $ getent passwd Unix_User+1616
  25.  * Unix_User+1616:*:4278191696:4278191696:U-Unix_User\1616,S-1-22-1-1616:/:/sbin/nologin
  26.  *
  27.  */
  28.  
  29. /* S-1-22-1-0 for uid 0 */
  30. #define SECURITY_SAMBA_UNIX_AUTHORITY { { 0,0,0,0,0,22 } }
  31. SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
  32.  
  33. BOOL AllocateUnixUserSID(unsigned long uid, PSID *pSid)
  34. {
  35.     PSID sid = NULL;
  36.     PSID malloced_sid = NULL;
  37.     DWORD sid_len;
  38.  
  39.     if (AllocateAndInitializeSid(&sid_id_auth, 2, 1, (DWORD)uid, 0, 0, 0, 0, 0, 0, &sid)) {
  40.         sid_len = GetLengthSid(sid);
  41.         malloced_sid = malloc(sid_len);
  42.  
  43.         if (malloced_sid) {
  44.             /*
  45.              * |AllocateAndInitializeSid()| has an own memory
  46.              * allocator, but we need the sid in memory from
  47.              * |malloc()|
  48.              */
  49.             if (CopySid(sid_len, malloced_sid, sid)) {
  50.                 FreeSid(sid);
  51.                 *pSid = malloced_sid;
  52.                 (void)printf("AllocateUnixUserSID(): Allocated Unix_User+%lu: success, len=%ld\n",
  53.                     uid, (long)sid_len);
  54.                 return TRUE;
  55.             }
  56.         }
  57.     }
  58.    
  59.     FreeSid(sid);
  60.     free(malloced_sid);
  61.     (void)printf("AllocateUnixUserSID(): Failed to allocate Unix_User+%lu: error code %d\n",
  62.         uid, GetLastError());
  63.     return FALSE;
  64. }
  65.  
  66. int fput_sidstr(FILE *f, PSID *sid)
  67. {
  68.     LPSTR string_sid = NULL;
  69.     int res;
  70.     if (!ConvertSidToStringSidA(*sid, &string_sid)) {
  71.         (void)printf("fput_sidstr: Failed to convert to string: error code %d\n", GetLastError());
  72.         return -1;
  73.     }
  74.     res = fputs(string_sid, f);
  75.     LocalFree(string_sid);
  76.     return res;
  77. }
  78.  
  79. int main(int ac, char *av[])
  80. {
  81.     // S-1-22-1-1
  82.     PSID sid = NULL;
  83.     PSID new_sid = NULL;
  84.     LPSTR string_sid = NULL;
  85.  
  86.     (void)puts("#start.");
  87.  
  88. #if 1
  89.     // Create a new SID with the given ID authority and no sub-authorities
  90.     if (!AllocateUnixUserSID(1616, &sid)) {
  91.         (void)printf("Failed to allocate SID: error code %d\n", GetLastError());
  92.         return 1;
  93.     }
  94.    
  95.     (void)printf("sid='");
  96.     (void)fput_sidstr(stdout, &sid);
  97.     (void)printf("'\n");
  98.     free(sid);
  99.    
  100.     sid = NULL;
  101.     // Create a new SID with the given ID authority and no sub-authorities
  102.     if (!AllocateUnixUserSID(1818, &sid)) {
  103.         (void)printf("Failed to allocate SID: error code %d\n", GetLastError());
  104.         return 1;
  105.     }
  106.    
  107.     (void)printf("sid='");
  108.     (void)fput_sidstr(stdout, &sid);
  109.     (void)printf("'\n");
  110.  
  111.     // Stringify and print
  112.     if (!ConvertSidToStringSidA(sid, &string_sid)) {
  113.         printf("Failed to convert to string: error code %d\n", GetLastError());
  114.         free(sid);
  115.         return 2;
  116.     }
  117.     (void)printf("string_sid='%s'\n", string_sid);
  118.  
  119.     // Destringify and print
  120.     if (ConvertStringSidToSidA(string_sid, &new_sid)) {
  121.         (void)printf("Success\n");
  122.     }
  123.     else {
  124.         (void)printf("Failed: error code %d\n", GetLastError());
  125.     }
  126. #endif
  127.     /* Part 2 */
  128.     (void)printf("# Part 2, custom ID\n");
  129.  
  130.     LocalFree(string_sid);
  131.     LocalFree(new_sid);
  132.     free(sid);
  133.     sid = NULL;
  134.     new_sid = NULL;    
  135.     string_sid = NULL;
  136.    
  137.     char sid_unix_user_buf[64];
  138.     snprintf(sid_unix_user_buf, sizeof(sid_unix_user_buf), "S-1-22-1-%lu", 1616UL);
  139.  
  140.     if (ConvertStringSidToSidA(sid_unix_user_buf, &sid)) {
  141.         (void)printf("Success\n");
  142.     }
  143.     else {
  144.         (void)printf("ConvertStringSidToSidA() Failed error code %d\n", GetLastError());
  145.     }
  146.     if (!ConvertSidToStringSidA(sid, &string_sid)) {
  147.         (void)printf("ConvertSidToStringSidA() failed error code %d\n", GetLastError());
  148.         FreeSid(sid);
  149.         return 2;
  150.     }
  151.     (void)printf("string_sid='%s'\n", string_sid);
  152.  
  153.     // Destringify and print
  154.     if (ConvertStringSidToSidA(string_sid, &new_sid)) {
  155.         (void)printf("Success\n");
  156.     }
  157.     else {
  158.         (void)printf("Failed: error code %d\n", GetLastError());
  159.     }
  160.  
  161.     if (!ConvertSidToStringSidA(new_sid, &string_sid)) {
  162.         (void)printf("ConvertSidToStringSidA() failed error code %d\n", GetLastError());
  163.         FreeSid(sid);
  164.         return 2;
  165.     }
  166.     (void)printf("string_sid='%s'\n", string_sid);
  167.  
  168.     // Clean up
  169.     LocalFree(string_sid);
  170.     LocalFree(new_sid);
  171.     FreeSid(sid);
  172.    
  173.     (void)puts("#done.");
  174.     return EXIT_SUCCESS;
  175. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at