pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


isunixusersid.c - test whether a SID is a Unix-User or Unix-Group SID
Posted by Anonymous on Tue 14th May 2024 14:41
raw | new post

  1.  
  2. /*
  3.  * isunixusersid.c - test whether a SID is a Unix-User or
  4.  * Unix-Group SID
  5.  *
  6.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  7.  */
  8.  
  9. #include <windows.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <stdbool.h>
  13.  
  14. typedef DWORD uid_t;
  15. typedef DWORD gid_t;
  16.  
  17. #define SECURITY_SAMBA_UNIX_AUTHORITY { { 0,0,0,0,0,22 } }
  18. SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
  19.  
  20. static
  21. BOOL allocate_unixuser_sid(unsigned long uid, PSID *pSid)
  22. {
  23.     PSID sid = NULL;
  24.     PSID malloced_sid = NULL;
  25.     DWORD sid_len;
  26.  
  27.     if (AllocateAndInitializeSid(&sid_id_auth, 2, 1, (DWORD)uid,
  28.         0, 0, 0, 0, 0, 0, &sid)) {
  29.         sid_len = GetLengthSid(sid);
  30.  
  31.         malloced_sid = malloc(sid_len);
  32.  
  33.         if (malloced_sid) {
  34.             /*
  35.              * |AllocateAndInitializeSid()| has an own memory
  36.              * allocator, but we need the sid in memory from
  37.              * |malloc()|
  38.              */
  39.             if (CopySid(sid_len, malloced_sid, sid)) {
  40.                 FreeSid(sid);
  41.                 *pSid = malloced_sid;
  42.                 return TRUE;
  43.             }
  44.         }
  45.     }
  46.  
  47.     FreeSid(sid);
  48.     free(malloced_sid);
  49.     return FALSE;
  50. }
  51.  
  52. static
  53. BOOL allocate_unixgroup_sid(unsigned long gid, PSID *pSid)
  54. {
  55.     PSID sid = NULL;
  56.     PSID malloced_sid = NULL;
  57.     DWORD sid_len;
  58.  
  59.     if (AllocateAndInitializeSid(&sid_id_auth, 2, 2, (DWORD)gid,
  60.         0, 0, 0, 0, 0, 0, &sid)) {
  61.         sid_len = GetLengthSid(sid);
  62.  
  63.         malloced_sid = malloc(sid_len);
  64.  
  65.         if (malloced_sid) {
  66.             /*
  67.              * |AllocateAndInitializeSid()| has an own memory
  68.              * allocator, but we need the sid in memory from
  69.              * |malloc()|
  70.              */
  71.             if (CopySid(sid_len, malloced_sid, sid)) {
  72.                 FreeSid(sid);
  73.                 *pSid = malloced_sid;
  74.                 return TRUE;
  75.             }
  76.         }
  77.     }
  78.  
  79.     FreeSid(sid);
  80.     free(malloced_sid);
  81.     return FALSE;
  82. }
  83.  
  84. bool unixusersid2uid(PSID psid, uid_t *puid)
  85. {
  86.     if (!psid)
  87.         return false;
  88.        
  89.     PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(psid);
  90.     if ((*GetSidSubAuthorityCount(psid) == 2) &&
  91.         (psia->Value[0] == 0) &&
  92.         (psia->Value[1] == 0) &&
  93.         (psia->Value[2] == 0) &&
  94.         (psia->Value[3] == 0) &&
  95.         (psia->Value[4] == 0) &&
  96.         (psia->Value[5] == 22) &&
  97.         (*GetSidSubAuthority(psid, 0) == 1)) {
  98.         *puid = *GetSidSubAuthority(psid, 1);
  99.         return true;
  100.     }
  101.    
  102.     return false;
  103. }
  104.  
  105. bool unixgroupsid2gid(PSID psid, gid_t *pgid)
  106. {
  107.     if (!psid)
  108.         return false;
  109.        
  110.     PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(psid);
  111.     if ((*GetSidSubAuthorityCount(psid) == 2) &&
  112.         (psia->Value[0] == 0) &&
  113.         (psia->Value[1] == 0) &&
  114.         (psia->Value[2] == 0) &&
  115.         (psia->Value[3] == 0) &&
  116.         (psia->Value[4] == 0) &&
  117.         (psia->Value[5] == 22) &&
  118.         (*GetSidSubAuthority(psid, 0) == 2)) {
  119.         *pgid = *GetSidSubAuthority(psid, 1);
  120.         return true;
  121.     }
  122.    
  123.     return false;
  124. }
  125.  
  126. static
  127. void print_sid_data(PSID usid)
  128. {
  129.     (void)printf("GetSidSubAuthorityCount=%d\n", (int)*GetSidSubAuthorityCount(usid));
  130.  
  131.     PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(usid);
  132.     for (int i = 0 ; i < 6 ; i++) {
  133.         (void)printf("sia[%d]=%d\n", i, (int)psia->Value[i]);
  134.     }
  135.     (void)printf("sidsubauth1=%d\n", (int)*GetSidSubAuthority(usid, 0));
  136.     (void)printf("sidsubauth1=%d\n", (int)*GetSidSubAuthority(usid, 1));
  137.     (void)printf("sidsubauth2=%d\n", (int)*GetSidSubAuthority(usid, 2));
  138.     (void)printf("sidsubauth3=%d\n", (int)*GetSidSubAuthority(usid, 3));
  139.     (void)printf("sidsubauth4=%d\n", (int)*GetSidSubAuthority(usid, 4));
  140.     (void)printf("sidsubauth5=%d\n", (int)*GetSidSubAuthority(usid, 5));
  141.     (void)printf("sidsubauth6=%d\n", (int)*GetSidSubAuthority(usid, 6));
  142.     (void)printf("sidsubauth7=%d\n", (int)*GetSidSubAuthority(usid, 7));
  143.     (void)printf("sidsubauth8=%d\n", (int)*GetSidSubAuthority(usid, 8));
  144.     (void)printf("sidsubauth9=%d\n", (int)*GetSidSubAuthority(usid, 9));
  145.    
  146.     uid_t uid = 0;
  147.     if (unixusersid2uid(usid, &uid)) {
  148.         (void)printf("unixusersid2uid() uid=%d\n", uid);
  149.     }
  150.     else {
  151.         (void)printf("unixusersid2uid() failed.\n");
  152.     }
  153.  
  154.     gid_t gid = 0;
  155.     if (unixgroupsid2gid(usid, &gid)) {
  156.         (void)printf("unixgroupsid2gid() gid=%d\n", gid);
  157.     }
  158.     else {
  159.         (void)printf("unixgroupsid2gid() failed.\n");
  160.     }
  161. }
  162.  
  163. int main(int ac, char *av[])
  164. {
  165.     PSID usid;
  166.     PSID gsid;
  167.  
  168.     (void)puts("#start.");
  169.    
  170.     (void)printf("# Testing UnixUser+1616:\n");
  171.     (void)allocate_unixuser_sid(1616, &usid);
  172.     print_sid_data(usid);
  173.  
  174.     (void)printf("# Testing UnixGroup+1846:\n");
  175.     (void)allocate_unixgroup_sid(1846, &gsid);
  176.     print_sid_data(gsid);
  177.    
  178.     (void)puts("#done.");
  179.     return 0;
  180. }

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