pastebin - collaborative debugging tool
rovema.kpaste.net RSS


get_file_sid1.c
Posted by Anonymous on Fri 6th Oct 2023 11:21
raw | new post

  1.  
  2. /*
  3.  * get_file_sid1.c - print Win32 owner/group SIDs for given files
  4.  * in ksh93 compould variable format (print -C/read -C)
  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 <sddl.h>
  13. #include <aclapi.h>
  14.  
  15. static
  16. int fput_sidstr(FILE *f, PSID *sid)
  17. {
  18.     LPSTR string_sid = NULL;
  19.     int res;
  20.     if (!ConvertSidToStringSidA(*sid, &string_sid)) {
  21.         (void)printf("fput_sidstr: Failed to convert to string: error code %d\n", GetLastError());
  22.         return -1;
  23.     }
  24.     res = fputs(string_sid, f);
  25.     LocalFree(string_sid);
  26.     return res;
  27. }
  28.  
  29. static
  30. int print_file_sids(char *filename)
  31. {
  32.     HANDLE hFile;
  33.     DWORD dwRtnCode = 0;
  34.     PSID pSidOwner = NULL;
  35.     PSID pSidGroup = NULL;
  36.     PSECURITY_DESCRIPTOR pSD = NULL;
  37.     DWORD dwErrorCode;
  38.    
  39.     hFile = CreateFileA(
  40.         filename,
  41.         GENERIC_READ,
  42.         FILE_SHARE_READ,
  43.         NULL,
  44.         OPEN_EXISTING,
  45.         FILE_ATTRIBUTE_NORMAL,
  46.         NULL);
  47.    
  48.     if (hFile == INVALID_HANDLE_VALUE) {
  49.         dwErrorCode = GetLastError();
  50.         (void)fprintf(stderr, "CreateFile error = %d for filename='%s'\n",
  51.             dwErrorCode, filename);
  52.         return EXIT_FAILURE;
  53.     }
  54.    
  55.     dwRtnCode = GetSecurityInfo(
  56.         hFile,
  57.         SE_FILE_OBJECT,
  58.         OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION,
  59.         &pSidOwner,
  60.         &pSidGroup,
  61.         NULL,
  62.         NULL,
  63.         &pSD);
  64.  
  65.     if (dwRtnCode != ERROR_SUCCESS) {
  66.         dwErrorCode = GetLastError();
  67.         (void)fprintf(stderr, "GetSecurityInfo error = %d for filename '%s'\n",
  68.             dwErrorCode, filename);
  69.         return EXIT_FAILURE;
  70.     }
  71.  
  72.     (void)printf("( filename='%s' ", filename);
  73.     (void)printf("sid_owner='");
  74.     (void)fput_sidstr(stdout, &pSidOwner);
  75.     (void)printf("' ");
  76.  
  77.     (void)printf("sid_group='");
  78.     (void)fput_sidstr(stdout, &pSidGroup);
  79.     (void)printf("' ");
  80.  
  81.     (void)printf(")\n");
  82.    
  83.     return EXIT_SUCCESS;
  84. }
  85.  
  86. int main(int ac, char *av[])
  87. {
  88.     int i;
  89.     int res = EXIT_SUCCESS;
  90.  
  91.     (void)fprintf(stderr, "#start.\n");
  92.  
  93.     for (i=1 ; i < ac ; i++) {
  94.         if (print_file_sids(av[i]) != EXIT_SUCCESS)
  95.             res = EXIT_FAILURE;
  96.     }
  97.  
  98.     (void)fprintf(stderr, "#done.\n");
  99.    
  100.     return res;
  101. }

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