16 #include "mysys_priv.h"
30 static my_bool is_nt()
32 return GetVersion() < 0x80000000;
40 typedef struct st_my_security_attr
74 int my_security_attr_create(SECURITY_ATTRIBUTES **psa,
const char **perror,
75 DWORD owner_rights, DWORD everyone_rights)
78 SID_IDENTIFIER_AUTHORITY world_auth= SECURITY_WORLD_SID_AUTHORITY;
81 SECURITY_ATTRIBUTES *sa= 0;
83 DWORD owner_token_length, dacl_length;
84 SECURITY_DESCRIPTOR *sd;
85 PTOKEN_USER owner_token;
87 My_security_attr *attr;
99 if (! AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
100 0, 0, 0, 0, 0, 0, 0, &everyone_sid))
102 *perror=
"Failed to retrieve the SID of Everyone group";
113 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &htoken))
115 *perror=
"Failed to retrieve thread access token";
118 GetTokenInformation(htoken, TokenUser, 0, 0, &owner_token_length);
120 if (! my_multi_malloc(MYF(MY_WME),
121 &sa, ALIGN_SIZE(
sizeof(SECURITY_ATTRIBUTES)) +
122 sizeof(My_security_attr),
123 &sd,
sizeof(SECURITY_DESCRIPTOR),
124 &owner_token, owner_token_length,
127 *perror=
"Failed to allocate memory for SECURITY_ATTRIBUTES";
130 memset(owner_token, 0, owner_token_length);
131 if (! GetTokenInformation(htoken, TokenUser, owner_token,
132 owner_token_length, &owner_token_length))
134 *perror=
"GetTokenInformation failed";
137 owner_sid= owner_token->User.Sid;
139 if (! IsValidSid(owner_sid))
141 *perror=
"IsValidSid failed";
146 dacl_length=
sizeof(ACL) + (
sizeof(ACCESS_ALLOWED_ACE)-
sizeof(DWORD)) * 2 +
147 GetLengthSid(everyone_sid) + GetLengthSid(owner_sid);
150 if (! (dacl= (PACL) my_malloc(dacl_length, MYF(MY_ZEROFILL|MY_WME))))
152 *perror=
"Failed to allocate memory for DACL";
155 if (! InitializeAcl(dacl, dacl_length, ACL_REVISION))
157 *perror=
"Failed to initialize DACL";
160 if (! AddAccessAllowedAce(dacl, ACL_REVISION, everyone_rights, everyone_sid))
162 *perror=
"Failed to set up DACL";
165 if (! AddAccessAllowedAce(dacl, ACL_REVISION, owner_rights, owner_sid))
167 *perror=
"Failed to set up DACL";
170 if (! InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION))
172 *perror=
"Could not initialize security descriptor";
175 if (! SetSecurityDescriptorDacl(sd, TRUE, dacl, FALSE))
177 *perror=
"Failed to install DACL";
181 sa->nLength=
sizeof(*sa);
182 sa->bInheritHandle= TRUE;
183 sa->lpSecurityDescriptor= sd;
185 attr= (My_security_attr*) (((
char*) sa) + ALIGN_SIZE(
sizeof(*sa)));
186 attr->everyone_sid= everyone_sid;
194 FreeSid(everyone_sid);
211 void my_security_attr_free(SECURITY_ATTRIBUTES *sa)
215 My_security_attr *attr= (My_security_attr*)
216 (((
char*)sa) + ALIGN_SIZE(
sizeof(*sa)));
217 FreeSid(attr->everyone_sid);