- #include <iostream>
- #include <windows.h>
- #include <sddl.h>
- int main()
- {
- // S-1-22-1-1
- #define SECURITY_SAMBA_UNIX_AUTHORITY {0,0,0,0,0,22}
- SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
- PSID sid = 0;
- PSID new_sid = 0;
- LPWSTR string_sid = NULL;
- /*
- *
- * $ getent passwd Unix_User+0
- * Unix_User+0:*:4278190080:4278190080:U-Unix_User\0,S-1-22-1-0:/:/sbin/nologin
- *
- */
- #if 0
- // Create a new SID with the given ID authority and no sub-authorities
- if (!AllocateAndInitializeSid(&sid_id_auth, 2, 1, 0, 0, 0, 0, 0, 0, 0, &sid)) {
- std::cout << "Failed to allocate SID: error code " << GetLastError() << std::endl;
- return 1;
- }
- // Stringify and print
- if (!ConvertSidToStringSidW(sid, &string_sid)) {
- std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
- FreeSid(sid);
- return 2;
- }
- std::wcout << string_sid << std::endl;
- // Destringify and print
- if (ConvertStringSidToSidW(string_sid, &new_sid)) {
- std::cout << "Success" << std::endl;
- }
- else {
- std::cout << "Failed: error code " << GetLastError() << std::endl;
- }
- #endif
- /* Part 2 */
- std::wcout << "# Part 2, custom ID" << std::endl;
- LocalFree(string_sid);
- LocalFree(new_sid);
- FreeSid(sid);
- sid = NULL;
- new_sid = NULL;
- string_sid = NULL;
- char sid_unix_user_buf[256];
- snprintf(sid_unix_user_buf, sizeof(sid_unix_user_buf), "S-1-22-1-%lu", 1616UL);
- if (ConvertStringSidToSidA(sid_unix_user_buf, &sid)) {
- std::cout << "Success" << std::endl;
- }
- else {
- std::cout << "Failed: error code " << GetLastError() << std::endl;
- }
- if (!ConvertSidToStringSidW(sid, &string_sid)) {
- std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
- FreeSid(sid);
- return 2;
- }
- std::wcout << string_sid << std::endl;
- // Destringify and print
- if (ConvertStringSidToSidW(string_sid, &new_sid)) {
- std::cout << "Success" << std::endl;
- }
- else {
- std::cout << "Failed: error code " << GetLastError() << std::endl;
- }
- if (!ConvertSidToStringSidW(new_sid, &string_sid)) {
- std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
- FreeSid(sid);
- return 2;
- }
- std::wcout << string_sid << std::endl;
- // Clean up
- LocalFree(string_sid);
- LocalFree(new_sid);
- FreeSid(sid);
- }
sidunixuser1.cc
Posted by Anonymous on Wed 4th Oct 2023 09:17
raw | new post
view followups (newest first): sidunixuser1.c by Anonymous
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.