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;
53 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
54 GetCurrentProcessId())) == NULL)
57 if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
60 if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
61 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
64 if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
67 if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
70 sidlen = GetLengthSid(user->User.Sid);
72 sid = (PSID)smalloc(sidlen);
74 if (!CopySid(sidlen, sid, user->User.Sid))
77 /* Success. Move sid into the return value slot, and null it out
78 * to stop the cleanup code freeing it. */
95 int getsids(char *error)
97 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
98 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
104 if ((usersid = get_user_sid()) == NULL) {
105 error = dupprintf("unable to construct SID for current user: %s",
106 win_strerror(GetLastError()));
112 if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
113 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
114 error = dupprintf("unable to construct SID for world: %s",
115 win_strerror(GetLastError()));
121 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
122 0, 0, 0, 0, 0, 0, 0, &networksid)) {
123 error = dupprintf("unable to construct SID for "
124 "local same-user access only: %s",
125 win_strerror(GetLastError()));
141 int make_private_security_descriptor(DWORD permissions,
142 PSECURITY_DESCRIPTOR *psd,
146 EXPLICIT_ACCESS ea[3];
155 if (!getsids(*error))
158 memset(ea, 0, sizeof(ea));
159 ea[0].grfAccessPermissions = permissions;
160 ea[0].grfAccessMode = REVOKE_ACCESS;
161 ea[0].grfInheritance = NO_INHERITANCE;
162 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
163 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
164 ea[1].grfAccessPermissions = permissions;
165 ea[1].grfAccessMode = GRANT_ACCESS;
166 ea[1].grfInheritance = NO_INHERITANCE;
167 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
168 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
169 ea[2].grfAccessPermissions = permissions;
170 ea[2].grfAccessMode = REVOKE_ACCESS;
171 ea[2].grfInheritance = NO_INHERITANCE;
172 ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
173 ea[2].Trustee.ptstrName = (LPTSTR)networksid;
175 acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
176 if (acl_err != ERROR_SUCCESS || *acl == NULL) {
177 *error = dupprintf("unable to construct ACL: %s",
178 win_strerror(acl_err));
182 *psd = (PSECURITY_DESCRIPTOR)
183 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
185 *error = dupprintf("unable to allocate security descriptor: %s",
186 win_strerror(GetLastError()));
190 if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
191 *error = dupprintf("unable to initialise security descriptor: %s",
192 win_strerror(GetLastError()));
196 if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
197 *error = dupprintf("unable to set owner in security descriptor: %s",
198 win_strerror(GetLastError()));
202 if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
203 *error = dupprintf("unable to set DACL in security descriptor: %s",
204 win_strerror(GetLastError()));
227 int setprocessacl(char *error)
229 EXPLICIT_ACCESS ea[2];
234 static const DWORD nastyace=WRITE_DAC | WRITE_OWNER |
235 PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
237 PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
238 PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
239 PROCESS_SUSPEND_RESUME;
244 memset(ea, 0, sizeof(ea));
247 ea[0].grfAccessPermissions = nastyace;
248 ea[0].grfAccessMode = DENY_ACCESS;
249 ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
250 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
251 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
254 ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
255 ea[1].grfAccessMode = GRANT_ACCESS;
256 ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
257 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
258 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
260 acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
262 if (acl_err != ERROR_SUCCESS || acl == NULL) {
263 error = dupprintf("unable to construct ACL: %s",
264 win_strerror(acl_err));
268 if (ERROR_SUCCESS != p_SetSecurityInfo
269 (GetCurrentProcess(), SE_KERNEL_OBJECT,
270 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
271 usersid, NULL, acl, NULL)) {
272 error=dupprintf("Unable to set process ACL: %s",
273 win_strerror(GetLastError()));
289 #endif /* !defined NO_SECURITY */