- /*
- * isunixusersid.c - test whether a SID is a Unix-User or
- * Unix-Group SID
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- typedef DWORD uid_t;
- typedef DWORD gid_t;
- #define SECURITY_SAMBA_UNIX_AUTHORITY { { 0,0,0,0,0,22 } }
- SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
- static
- BOOL allocate_unixuser_sid(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;
- return TRUE;
- }
- }
- }
- FreeSid(sid);
- return FALSE;
- }
- static
- BOOL allocate_unixgroup_sid(unsigned long gid, PSID *pSid)
- {
- PSID sid = NULL;
- PSID malloced_sid = NULL;
- DWORD sid_len;
- if (AllocateAndInitializeSid(&sid_id_auth, 2, 2, (DWORD)gid,
- 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;
- return TRUE;
- }
- }
- }
- FreeSid(sid);
- return FALSE;
- }
- bool unixusersid2uid(PSID psid, uid_t *puid)
- {
- if (!psid)
- return false;
- PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(psid);
- if ((*GetSidSubAuthorityCount(psid) == 2) &&
- (psia->Value[0] == 0) &&
- (psia->Value[1] == 0) &&
- (psia->Value[2] == 0) &&
- (psia->Value[3] == 0) &&
- (psia->Value[4] == 0) &&
- (psia->Value[5] == 22) &&
- (*GetSidSubAuthority(psid, 0) == 1)) {
- *puid = *GetSidSubAuthority(psid, 1);
- return true;
- }
- return false;
- }
- bool unixgroupsid2gid(PSID psid, gid_t *pgid)
- {
- if (!psid)
- return false;
- PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(psid);
- if ((*GetSidSubAuthorityCount(psid) == 2) &&
- (psia->Value[0] == 0) &&
- (psia->Value[1] == 0) &&
- (psia->Value[2] == 0) &&
- (psia->Value[3] == 0) &&
- (psia->Value[4] == 0) &&
- (psia->Value[5] == 22) &&
- (*GetSidSubAuthority(psid, 0) == 2)) {
- *pgid = *GetSidSubAuthority(psid, 1);
- return true;
- }
- return false;
- }
- static
- void print_sid_data(PSID usid)
- {
- PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(usid);
- for (int i = 0 ; i < 6 ; i++) {
- }
- uid_t uid = 0;
- if (unixusersid2uid(usid, &uid)) {
- }
- else {
- }
- gid_t gid = 0;
- if (unixgroupsid2gid(usid, &gid)) {
- }
- else {
- }
- }
- int main(int ac, char *av[])
- {
- PSID usid;
- PSID gsid;
- (void)allocate_unixuser_sid(1616, &usid);
- print_sid_data(usid);
- (void)allocate_unixgroup_sid(1846, &gsid);
- print_sid_data(gsid);
- return 0;
- }
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
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.