2 * winsecur.c: implementation of winsecur.h.
10 #if !defined NO_SECURITY
12 #define WINSECUR_GLOBAL
17 static int attempted = FALSE;
18 static int successful;
19 static HMODULE advapi;
23 advapi = load_system32_dll("advapi32.dll");
24 successful = advapi &&
25 GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
26 GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
27 GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
28 GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
29 GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
30 GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
37 static int attempted = FALSE;
38 static int successful;
43 crypt = load_system32_dll("crypt32.dll");
45 GET_WINDOWS_FUNCTION(crypt, CryptProtectMemory);
50 PSID get_user_sid(void)
52 HANDLE proc = NULL, tok = NULL;
53 TOKEN_USER *user = NULL;
55 PSID sid = NULL, ret = NULL;
60 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
61 GetCurrentProcessId())) == NULL)
64 if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
67 if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
68 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
71 if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
74 if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
77 sidlen = GetLengthSid(user->User.Sid);
79 sid = (PSID)smalloc(sidlen);
81 if (!CopySid(sidlen, sid, user->User.Sid))
84 /* Success. Move sid into the return value slot, and null it out
85 * to stop the cleanup code freeing it. */
102 int make_private_security_descriptor(DWORD permissions,
103 PSECURITY_DESCRIPTOR *psd,
108 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
109 EXPLICIT_ACCESS ea[3];
118 *error = dupprintf("unable to load advapi32.dll");
122 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
123 0, 0, 0, 0, 0, 0, 0, networksid)) {
124 *error = dupprintf("unable to construct SID for "
125 "local same-user access only: %s",
126 win_strerror(GetLastError()));
130 memset(ea, 0, sizeof(ea));
131 ea[0].grfAccessPermissions = permissions;
132 ea[0].grfAccessMode = REVOKE_ACCESS;
133 ea[0].grfInheritance = NO_INHERITANCE;
134 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
135 ea[0].Trustee.ptstrName = "EVERYONE";
136 ea[1].grfAccessPermissions = permissions;
137 ea[1].grfAccessMode = GRANT_ACCESS;
138 ea[1].grfInheritance = NO_INHERITANCE;
139 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
140 ea[1].Trustee.ptstrName = "CURRENT_USER";
141 ea[2].grfAccessPermissions = permissions;
142 ea[2].grfAccessMode = REVOKE_ACCESS;
143 ea[2].grfInheritance = NO_INHERITANCE;
144 ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
145 ea[2].Trustee.ptstrName = (LPTSTR)*networksid;
147 if (p_SetEntriesInAclA(3, ea, NULL, acl) != ERROR_SUCCESS || *acl == NULL) {
148 *error = dupprintf("unable to construct ACL: %s",
149 win_strerror(GetLastError()));
153 *psd = (PSECURITY_DESCRIPTOR)
154 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
156 *error = dupprintf("unable to allocate security descriptor: %s",
157 win_strerror(GetLastError()));
161 if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
162 *error = dupprintf("unable to initialise security descriptor: %s",
163 win_strerror(GetLastError()));
167 if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
168 *error = dupprintf("unable to set DACL in security descriptor: %s",
169 win_strerror(GetLastError()));
182 LocalFree(*networksid);
196 #endif /* !defined NO_SECURITY */