首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C++存储NTFS安全描述符

用C++存储NTFS安全描述符
EN

Stack Overflow用户
提问于 2010-05-11 15:21:37
回答 1查看 313关注 0票数 1

我的目标是以其相同的本机状态存储NTFS安全描述符。其目的是按需恢复它。

我设法为此目的编写了代码,我想知道是否有人介意验证它的样本?( for循环表示我存储本机描述符的方式)

此示例仅包含"OWNER“的标志,但我的目的是对所有安全描述符标志应用相同的方法。

我只是个初学者,如果你能提醒我会很感激。谢谢,Doori Bar

代码语言:javascript
复制
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501

#include <stdio.h>
#include <windows.h>
#include "accctrl.h"
#include "aclapi.h"
#include "sddl.h"

int main (void)
{
  DWORD lasterror;
  PSECURITY_DESCRIPTOR PSecurityD1, PSecurityD2;
  HANDLE hFile;
  PSID owner;
  LPTSTR ownerstr;
  BOOL ownerdefault;

  int ret = 0;
  unsigned int i;

  hFile = CreateFile("c:\\boot.ini", GENERIC_READ | ACCESS_SYSTEM_SECURITY, 
                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 
                     FILE_FLAG_BACKUP_SEMANTICS, NULL);

  if (hFile == INVALID_HANDLE_VALUE) {
    fprintf(stderr,"CreateFile() failed. Error: INVALID_HANDLE_VALUE\n");
    return 1;
  }

  lasterror = GetSecurityInfo(hFile, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION
                              , &owner, NULL, NULL, NULL, &PSecurityD1);

  if (lasterror != ERROR_SUCCESS) {
    fprintf(stderr,"GetSecurityInfo() failed. Error: %lu;\n", lasterror);
    ret = 1;
    goto ret1;
  }

  ConvertSidToStringSid(owner,&ownerstr);
  printf("ownerstr of PSecurityD1: %s\n", ownerstr);

  /* The for loop represents the way I store the native descriptor */
  PSecurityD2 = malloc( GetSecurityDescriptorLength(PSecurityD1) * 
                        sizeof(unsigned char) );

  for (i=0; i < GetSecurityDescriptorLength(PSecurityD1); i++)
    ((unsigned char *) PSecurityD2)[i] = ((unsigned char *) PSecurityD1)[i];

  if (IsValidSecurityDescriptor(PSecurityD2) == 0) {
    fprintf(stderr,"IsValidSecurityDescriptor(PSecurityD2) failed.\n");
    ret = 2;
    goto ret2;
  }

  if (GetSecurityDescriptorOwner(PSecurityD2,&owner,&ownerdefault) == 0) {
    fprintf(stderr,"GetSecurityDescriptorOwner() failed.");
    ret = 2;
    goto ret2;
  }
  ConvertSidToStringSid(owner,&ownerstr);
  printf("ownerstr of PSecurityD2: %s\n", ownerstr);

  ret2:
  free(owner);
  free(ownerstr);
  free(PSecurityD1);
  free(PSecurityD2);
  ret1:
  CloseHandle(hFile);
  return ret;
}
EN

回答 1

Stack Overflow用户

发布于 2010-08-24 03:54:08

基本上没问题--声明的时候我会把owner,ownerstr,PSecurityD1和PSecurityD2去掉。否则(如果失败,您可能会释放未分配的内存)

您还可以使用复制循环的memcpy instread

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2808754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档