- /*
- * get_file_sid1.c - print Win32 owner/group SIDs for given files
- * in ksh93 compould variable format (print -C/read -C)
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sddl.h>
- #include <aclapi.h>
- static
- int fput_sidstr(FILE *f, PSID *sid)
- {
- LPSTR string_sid = NULL;
- int res;
- if (!ConvertSidToStringSidA(*sid, &string_sid)) {
- (void)printf("fput_sidstr: Failed to convert to string: error code %d\n", GetLastError());
- return -1;
- }
- res = fputs(string_sid, f);
- LocalFree(string_sid);
- return res;
- }
- static
- int print_file_sids(char *filename)
- {
- HANDLE hFile;
- DWORD dwRtnCode = 0;
- PSID pSidOwner = NULL;
- PSID pSidGroup = NULL;
- PSECURITY_DESCRIPTOR pSD = NULL;
- DWORD dwErrorCode;
- hFile = CreateFileA(
- filename,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- dwErrorCode = GetLastError();
- (void)fprintf(stderr, "CreateFile error = %d for filename='%s'\n",
- dwErrorCode, filename);
- return EXIT_FAILURE;
- }
- dwRtnCode = GetSecurityInfo(
- hFile,
- SE_FILE_OBJECT,
- OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION,
- &pSidOwner,
- &pSidGroup,
- NULL,
- NULL,
- &pSD);
- if (dwRtnCode != ERROR_SUCCESS) {
- dwErrorCode = GetLastError();
- (void)fprintf(stderr, "GetSecurityInfo error = %d for filename '%s'\n",
- dwErrorCode, filename);
- return EXIT_FAILURE;
- }
- (void)printf("( filename='%s' ", filename);
- (void)printf("sid_owner='");
- (void)fput_sidstr(stdout, &pSidOwner);
- (void)printf("' ");
- (void)printf("sid_group='");
- (void)fput_sidstr(stdout, &pSidGroup);
- (void)printf("' ");
- (void)printf(")\n");
- return EXIT_SUCCESS;
- }
- int main(int ac, char *av[])
- {
- int i;
- int res = EXIT_SUCCESS;
- (void)fprintf(stderr, "#start.\n");
- for (i=1 ; i < ac ; i++) {
- if (print_file_sids(av[i]) != EXIT_SUCCESS)
- res = EXIT_FAILURE;
- }
- (void)fprintf(stderr, "#done.\n");
- return res;
- }
get_file_sid1.c
Posted by Anonymous on Fri 6th Oct 2023 11:21
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.