0154161b1748e4940b3ce11168b91e77fef1152b
[reactos.git] / reactos / dll / win32 / lsasrv / privileges.c
1 #define WIN32_NO_STATUS
2 #include <windows.h>
3 #include <ntsecapi.h>
4 #define NTOS_MODE_USER
5 #include <ndk/ntndk.h>
6
7 #include <wchar.h>
8 #include <string.h>
9
10
11 #include <wine/debug.h>
12
13
14 static const WCHAR SE_CREATE_TOKEN_NAME_W[] =
15 { 'S','e','C','r','e','a','t','e','T','o','k','e','n','P','r','i','v','i','l','e','g','e',0 };
16 static const WCHAR SE_ASSIGNPRIMARYTOKEN_NAME_W[] =
17 { 'S','e','A','s','s','i','g','n','P','r','i','m','a','r','y','T','o','k','e','n','P','r','i','v','i','l','e','g','e',0 };
18 static const WCHAR SE_LOCK_MEMORY_NAME_W[] =
19 { 'S','e','L','o','c','k','M','e','m','o','r','y','P','r','i','v','i','l','e','g','e',0 };
20 static const WCHAR SE_INCREASE_QUOTA_NAME_W[] =
21 { 'S','e','I','n','c','r','e','a','s','e','Q','u','o','t','a','P','r','i','v','i','l','e','g','e',0 };
22 static const WCHAR SE_MACHINE_ACCOUNT_NAME_W[] =
23 { 'S','e','M','a','c','h','i','n','e','A','c','c','o','u','n','t','P','r','i','v','i','l','e','g','e',0 };
24 static const WCHAR SE_TCB_NAME_W[] =
25 { 'S','e','T','c','b','P','r','i','v','i','l','e','g','e',0 };
26 static const WCHAR SE_SECURITY_NAME_W[] =
27 { 'S','e','S','e','c','u','r','i','t','y','P','r','i','v','i','l','e','g','e',0 };
28 static const WCHAR SE_TAKE_OWNERSHIP_NAME_W[] =
29 { 'S','e','T','a','k','e','O','w','n','e','r','s','h','i','p','P','r','i','v','i','l','e','g','e',0 };
30 static const WCHAR SE_LOAD_DRIVER_NAME_W[] =
31 { 'S','e','L','o','a','d','D','r','i','v','e','r','P','r','i','v','i','l','e','g','e',0 };
32 static const WCHAR SE_SYSTEM_PROFILE_NAME_W[] =
33 { 'S','e','S','y','s','t','e','m','P','r','o','f','i','l','e','P','r','i','v','i','l','e','g','e',0 };
34 static const WCHAR SE_SYSTEMTIME_NAME_W[] =
35 { 'S','e','S','y','s','t','e','m','t','i','m','e','P','r','i','v','i','l','e','g','e',0 };
36 static const WCHAR SE_PROF_SINGLE_PROCESS_NAME_W[] =
37 { 'S','e','P','r','o','f','i','l','e','S','i','n','g','l','e','P','r','o','c','e','s','s','P','r','i','v','i','l','e','g','e',0 };
38 static const WCHAR SE_INC_BASE_PRIORITY_NAME_W[] =
39 { 'S','e','I','n','c','r','e','a','s','e','B','a','s','e','P','r','i','o','r','i','t','y','P','r','i','v','i','l','e','g','e',0 };
40 static const WCHAR SE_CREATE_PAGEFILE_NAME_W[] =
41 { 'S','e','C','r','e','a','t','e','P','a','g','e','f','i','l','e','P','r','i','v','i','l','e','g','e',0 };
42 static const WCHAR SE_CREATE_PERMANENT_NAME_W[] =
43 { 'S','e','C','r','e','a','t','e','P','e','r','m','a','n','e','n','t','P','r','i','v','i','l','e','g','e',0 };
44 static const WCHAR SE_BACKUP_NAME_W[] =
45 { 'S','e','B','a','c','k','u','p','P','r','i','v','i','l','e','g','e',0 };
46 static const WCHAR SE_RESTORE_NAME_W[] =
47 { 'S','e','R','e','s','t','o','r','e','P','r','i','v','i','l','e','g','e',0 };
48 static const WCHAR SE_SHUTDOWN_NAME_W[] =
49 { 'S','e','S','h','u','t','d','o','w','n','P','r','i','v','i','l','e','g','e',0 };
50 static const WCHAR SE_DEBUG_NAME_W[] =
51 { 'S','e','D','e','b','u','g','P','r','i','v','i','l','e','g','e',0 };
52 static const WCHAR SE_AUDIT_NAME_W[] =
53 { 'S','e','A','u','d','i','t','P','r','i','v','i','l','e','g','e',0 };
54 static const WCHAR SE_SYSTEM_ENVIRONMENT_NAME_W[] =
55 { 'S','e','S','y','s','t','e','m','E','n','v','i','r','o','n','m','e','n','t','P','r','i','v','i','l','e','g','e',0 };
56 static const WCHAR SE_CHANGE_NOTIFY_NAME_W[] =
57 { 'S','e','C','h','a','n','g','e','N','o','t','i','f','y','P','r','i','v','i','l','e','g','e',0 };
58 static const WCHAR SE_REMOTE_SHUTDOWN_NAME_W[] =
59 { 'S','e','R','e','m','o','t','e','S','h','u','t','d','o','w','n','P','r','i','v','i','l','e','g','e',0 };
60 static const WCHAR SE_UNDOCK_NAME_W[] =
61 { 'S','e','U','n','d','o','c','k','P','r','i','v','i','l','e','g','e',0 };
62 static const WCHAR SE_SYNC_AGENT_NAME_W[] =
63 { 'S','e','S','y','n','c','A','g','e','n','t','P','r','i','v','i','l','e','g','e',0 };
64 static const WCHAR SE_ENABLE_DELEGATION_NAME_W[] =
65 { 'S','e','E','n','a','b','l','e','D','e','l','e','g','a','t','i','o','n','P','r','i','v','i','l','e','g','e',0 };
66 static const WCHAR SE_MANAGE_VOLUME_NAME_W[] =
67 { 'S','e','M','a','n','a','g','e','V','o','l','u','m','e','P','r','i','v','i','l','e','g','e',0 };
68 static const WCHAR SE_IMPERSONATE_NAME_W[] =
69 { 'S','e','I','m','p','e','r','s','o','n','a','t','e','P','r','i','v','i','l','e','g','e',0 };
70 static const WCHAR SE_CREATE_GLOBAL_NAME_W[] =
71 { 'S','e','C','r','e','a','t','e','G','l','o','b','a','l','P','r','i','v','i','l','e','g','e',0 };
72
73 static const WCHAR * const WellKnownPrivNames[SE_MAX_WELL_KNOWN_PRIVILEGE + 1] =
74 {
75 NULL,
76 NULL,
77 SE_CREATE_TOKEN_NAME_W,
78 SE_ASSIGNPRIMARYTOKEN_NAME_W,
79 SE_LOCK_MEMORY_NAME_W,
80 SE_INCREASE_QUOTA_NAME_W,
81 SE_MACHINE_ACCOUNT_NAME_W,
82 SE_TCB_NAME_W,
83 SE_SECURITY_NAME_W,
84 SE_TAKE_OWNERSHIP_NAME_W,
85 SE_LOAD_DRIVER_NAME_W,
86 SE_SYSTEM_PROFILE_NAME_W,
87 SE_SYSTEMTIME_NAME_W,
88 SE_PROF_SINGLE_PROCESS_NAME_W,
89 SE_INC_BASE_PRIORITY_NAME_W,
90 SE_CREATE_PAGEFILE_NAME_W,
91 SE_CREATE_PERMANENT_NAME_W,
92 SE_BACKUP_NAME_W,
93 SE_RESTORE_NAME_W,
94 SE_SHUTDOWN_NAME_W,
95 SE_DEBUG_NAME_W,
96 SE_AUDIT_NAME_W,
97 SE_SYSTEM_ENVIRONMENT_NAME_W,
98 SE_CHANGE_NOTIFY_NAME_W,
99 SE_REMOTE_SHUTDOWN_NAME_W,
100 SE_UNDOCK_NAME_W,
101 SE_SYNC_AGENT_NAME_W,
102 SE_ENABLE_DELEGATION_NAME_W,
103 SE_MANAGE_VOLUME_NAME_W,
104 SE_IMPERSONATE_NAME_W,
105 SE_CREATE_GLOBAL_NAME_W,
106 };
107
108 static const WCHAR * const DefaultPrivNames[] =
109 {
110 L"SeCreateTokenPrivilege",
111 L"SeAssignPrimaryTokenPrivilege",
112 L"SeLockMemoryPrivilege",
113 L"SeIncreaseQuotaPrivilege",
114 L"SeMachineAccountPrivilege",
115 L"SeTcbPrivilege",
116 L"SeSecurityPrivilege",
117 L"SeTakeOwnershipPrivilege",
118 L"SeLoadDriverPrivilege",
119 L"SeSystemProfilePrivilege",
120 L"SeSystemtimePrivilege",
121 L"SeProfileSingleProcessPrivilege",
122 L"SeIncreaseBasePriorityPrivilege",
123 L"SeCreatePagefilePrivilege",
124 L"SeCreatePermanentPrivilege",
125 L"SeBackupPrivilege",
126 L"SeRestorePrivilege",
127 L"SeShutdownPrivilege",
128 L"SeDebugPrivilege",
129 L"SeAuditPrivilege",
130 L"SeSystemEnvironmentPrivilege",
131 L"SeChangeNotifyPrivilege",
132 L"SeRemoteShutdownPrivilege",
133 L"SeUndockPrivilege",
134 L"SeSyncAgentPrivilege",
135 L"SeEnableDelegationPrivilege",
136 L"SeManageVolumePrivilege",
137 L"SeImpersonatePrivilege",
138 L"SeCreateGlobalPrivilege"
139 };
140
141
142 NTSTATUS
143 LsarpLookupPrivilegeName(PLUID Value,
144 PUNICODE_STRING *Name)
145 {
146 PUNICODE_STRING NameBuffer;
147
148 if (Value->HighPart ||
149 (Value->LowPart < SE_MIN_WELL_KNOWN_PRIVILEGE ||
150 Value->LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE))
151 {
152 return STATUS_NO_SUCH_PRIVILEGE;
153 }
154
155 NameBuffer = MIDL_user_allocate(sizeof(UNICODE_STRING));
156 if (NameBuffer == NULL)
157 return STATUS_NO_MEMORY;
158
159 NameBuffer->Length = wcslen(WellKnownPrivNames[Value->LowPart]) * sizeof(WCHAR);
160 NameBuffer->MaximumLength = NameBuffer->Length + sizeof(WCHAR);
161
162 NameBuffer->Buffer = MIDL_user_allocate(NameBuffer->MaximumLength);
163 if (NameBuffer == NULL)
164 {
165 MIDL_user_free(NameBuffer);
166 return STATUS_NO_MEMORY;
167 }
168
169 wcscpy(NameBuffer->Buffer, WellKnownPrivNames[Value->LowPart]);
170
171 *Name = NameBuffer;
172
173 return STATUS_SUCCESS;
174 }
175
176
177 NTSTATUS
178 LsarpLookupPrivilegeValue(PUNICODE_STRING Name,
179 PLUID Value)
180 {
181 ULONG Priv;
182
183 if (Name->Length == 0 || Name->Buffer == NULL)
184 return STATUS_NO_SUCH_PRIVILEGE;
185
186 for (Priv = 0; Priv < sizeof(DefaultPrivNames) / sizeof(DefaultPrivNames[0]); Priv++)
187 {
188 if (0 == _wcsicmp(Name->Buffer, DefaultPrivNames[Priv]))
189 {
190 Value->LowPart = Priv + SE_MIN_WELL_KNOWN_PRIVILEGE;
191 Value->HighPart = 0;
192 return STATUS_SUCCESS;
193 }
194 }
195
196 return STATUS_NO_SUCH_PRIVILEGE;
197 }