2 * winsecur.c: implementation of winsecur.h.
10 #if !defined NO_SECURITY
12 #define WINSECUR_GLOBAL
15 /* Initialised once, then kept around to reuse forever */
16 static PSID worldsid, networksid, usersid;
21 static int attempted = FALSE;
22 static int successful;
23 static HMODULE advapi;
27 advapi = load_system32_dll("advapi32.dll");
28 successful = advapi &&
29 GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
30 GET_WINDOWS_FUNCTION(advapi, SetSecurityInfo) &&
31 GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
32 GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
33 GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
34 GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
35 GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
40 PSID get_user_sid(void)
42 HANDLE proc = NULL, tok = NULL;
43 TOKEN_USER *user = NULL;
45 PSID sid = NULL, ret = NULL;
50 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
51 GetCurrentProcessId())) == NULL)
54 if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
57 if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
58 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
61 if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
64 if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
67 sidlen = GetLengthSid(user->User.Sid);
69 sid = (PSID)smalloc(sidlen);
71 if (!CopySid(sidlen, sid, user->User.Sid))
74 /* Success. Move sid into the return value slot, and null it out
75 * to stop the cleanup code freeing it. */
92 int getsids(char *error)
94 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
95 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
101 if ((usersid = get_user_sid()) == NULL) {
102 error = dupprintf("unable to construct SID for current user: %s",
103 win_strerror(GetLastError()));
109 if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
110 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
111 error = dupprintf("unable to construct SID for world: %s",
112 win_strerror(GetLastError()));
118 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
119 0, 0, 0, 0, 0, 0, 0, &networksid)) {
120 error = dupprintf("unable to construct SID for "
121 "local same-user access only: %s",
122 win_strerror(GetLastError()));
138 int make_private_security_descriptor(DWORD permissions,
139 PSECURITY_DESCRIPTOR *psd,
143 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
144 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
145 EXPLICIT_ACCESS ea[3];
154 if (!getsids(*error))
157 memset(ea, 0, sizeof(ea));
158 ea[0].grfAccessPermissions = permissions;
159 ea[0].grfAccessMode = REVOKE_ACCESS;
160 ea[0].grfInheritance = NO_INHERITANCE;
161 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
162 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
163 ea[1].grfAccessPermissions = permissions;
164 ea[1].grfAccessMode = GRANT_ACCESS;
165 ea[1].grfInheritance = NO_INHERITANCE;
166 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
167 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
168 ea[2].grfAccessPermissions = permissions;
169 ea[2].grfAccessMode = REVOKE_ACCESS;
170 ea[2].grfInheritance = NO_INHERITANCE;
171 ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
172 ea[2].Trustee.ptstrName = (LPTSTR)networksid;
174 acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
175 if (acl_err != ERROR_SUCCESS || *acl == NULL) {
176 *error = dupprintf("unable to construct ACL: %s",
177 win_strerror(acl_err));
181 *psd = (PSECURITY_DESCRIPTOR)
182 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
184 *error = dupprintf("unable to allocate security descriptor: %s",
185 win_strerror(GetLastError()));
189 if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
190 *error = dupprintf("unable to initialise security descriptor: %s",
191 win_strerror(GetLastError()));
195 if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
196 *error = dupprintf("unable to set owner in security descriptor: %s",
197 win_strerror(GetLastError()));
201 if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
202 *error = dupprintf("unable to set DACL in security descriptor: %s",
203 win_strerror(GetLastError()));
226 int setprocessacl(char *error)
228 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
229 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
230 EXPLICIT_ACCESS ea[2];
235 static const nastyace=WRITE_DAC | WRITE_OWNER |
236 PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
237 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION |
238 PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
239 PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
240 PROCESS_SUSPEND_RESUME;
245 memset(ea, 0, sizeof(ea));
248 ea[0].grfAccessPermissions = nastyace;
249 ea[0].grfAccessMode = DENY_ACCESS;
250 ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
251 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
252 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
255 ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
256 ea[1].grfAccessMode = GRANT_ACCESS;
257 ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
258 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
259 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
261 acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
263 if (acl_err != ERROR_SUCCESS || acl == NULL) {
264 error = dupprintf("unable to construct ACL: %s",
265 win_strerror(acl_err));
269 if (ERROR_SUCCESS != p_SetSecurityInfo
270 (GetCurrentProcess(), SE_KERNEL_OBJECT,
271 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
272 usersid, NULL, acl, NULL)) {
273 error=dupprintf("Unable to set process ACL: %s",
274 win_strerror(GetLastError()));
290 #endif /* !defined NO_SECURITY */