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, OpenProcessToken) &&
31 GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
32 GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
33 GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
34 GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
39 PSID get_user_sid(void)
41 HANDLE proc = NULL, tok = NULL;
42 TOKEN_USER *user = NULL;
44 PSID sid = NULL, ret = NULL;
49 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
50 GetCurrentProcessId())) == NULL)
53 if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
56 if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
57 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
60 if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
63 if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
66 sidlen = GetLengthSid(user->User.Sid);
68 sid = (PSID)smalloc(sidlen);
70 if (!CopySid(sidlen, sid, user->User.Sid))
73 /* Success. Move sid into the return value slot, and null it out
74 * to stop the cleanup code freeing it. */
91 int getsids(char *error)
93 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
94 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
100 if ((usersid = get_user_sid()) == NULL) {
101 error = dupprintf("unable to construct SID for current user: %s",
102 win_strerror(GetLastError()));
108 if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
109 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
110 error = dupprintf("unable to construct SID for world: %s",
111 win_strerror(GetLastError()));
117 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
118 0, 0, 0, 0, 0, 0, 0, &networksid)) {
119 error = dupprintf("unable to construct SID for "
120 "local same-user access only: %s",
121 win_strerror(GetLastError()));
137 int make_private_security_descriptor(DWORD permissions,
138 PSECURITY_DESCRIPTOR *psd,
142 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
143 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
144 EXPLICIT_ACCESS ea[3];
153 if (!getsids(*error))
156 memset(ea, 0, sizeof(ea));
157 ea[0].grfAccessPermissions = permissions;
158 ea[0].grfAccessMode = REVOKE_ACCESS;
159 ea[0].grfInheritance = NO_INHERITANCE;
160 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
161 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
162 ea[1].grfAccessPermissions = permissions;
163 ea[1].grfAccessMode = GRANT_ACCESS;
164 ea[1].grfInheritance = NO_INHERITANCE;
165 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
166 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
167 ea[2].grfAccessPermissions = permissions;
168 ea[2].grfAccessMode = REVOKE_ACCESS;
169 ea[2].grfInheritance = NO_INHERITANCE;
170 ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
171 ea[2].Trustee.ptstrName = (LPTSTR)networksid;
173 acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
174 if (acl_err != ERROR_SUCCESS || *acl == NULL) {
175 *error = dupprintf("unable to construct ACL: %s",
176 win_strerror(acl_err));
180 *psd = (PSECURITY_DESCRIPTOR)
181 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
183 *error = dupprintf("unable to allocate security descriptor: %s",
184 win_strerror(GetLastError()));
188 if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
189 *error = dupprintf("unable to initialise security descriptor: %s",
190 win_strerror(GetLastError()));
194 if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
195 *error = dupprintf("unable to set owner in security descriptor: %s",
196 win_strerror(GetLastError()));
200 if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
201 *error = dupprintf("unable to set DACL in security descriptor: %s",
202 win_strerror(GetLastError()));
225 int setprocessacl(char *error)
227 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
228 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
229 EXPLICIT_ACCESS ea[2];
234 static const nastyace=WRITE_DAC | WRITE_OWNER |
235 PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
236 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION |
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));
272 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
278 error=dupprintf("Unable to set process ACL: %s",
279 win_strerror(GetLastError()));
295 #endif /* !defined NO_SECURITY */