xref: /reactos/dll/win32/advapi32/sec/lsa.c (revision 527f2f90)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS system libraries
4  * FILE:            dll/win32/advapi32/sec/lsa.c
5  * PURPOSE:         Local security authority functions
6  * PROGRAMMER:      Emanuele Aliberti
7  * UPDATE HISTORY:
8  *      19990322 EA created
9  *      19990515 EA stubs
10  *      20030202 KJK compressed stubs
11  *
12  */
13 
14 #include <advapi32.h>
15 
16 #include <lsa_c.h>
17 
18 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
19 
20 static
21 BOOL
22 LsapIsLocalComputer(PLSA_UNICODE_STRING ServerName)
23 {
24     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
25     BOOL Result;
26     LPWSTR buf;
27 
28     if (ServerName == NULL || ServerName->Length == 0 || ServerName->Buffer == NULL)
29         return TRUE;
30 
31     buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
32     Result = GetComputerNameW(buf, &dwSize);
33     if (Result && (ServerName->Buffer[0] == '\\') && (ServerName->Buffer[1] == '\\'))
34         ServerName += 2;
35     Result = Result && !lstrcmpW(ServerName->Buffer, buf);
36     HeapFree(GetProcessHeap(), 0, buf);
37 
38     return Result;
39 }
40 
41 
42 handle_t
43 __RPC_USER
44 PLSAPR_SERVER_NAME_bind(PLSAPR_SERVER_NAME pszSystemName)
45 {
46     handle_t hBinding = NULL;
47     LPWSTR pszStringBinding;
48     RPC_STATUS status;
49 
50     TRACE("PLSAPR_SERVER_NAME_bind() called\n");
51 
52     status = RpcStringBindingComposeW(NULL,
53                                       L"ncacn_np",
54                                       pszSystemName,
55                                       L"\\pipe\\lsarpc",
56                                       NULL,
57                                       &pszStringBinding);
58     if (status)
59     {
60         TRACE("RpcStringBindingCompose returned 0x%x\n", status);
61         return NULL;
62     }
63 
64     /* Set the binding handle that will be used to bind to the server. */
65     status = RpcBindingFromStringBindingW(pszStringBinding,
66                                           &hBinding);
67     if (status)
68     {
69         TRACE("RpcBindingFromStringBinding returned 0x%x\n", status);
70     }
71 
72     status = RpcStringFreeW(&pszStringBinding);
73     if (status)
74     {
75         TRACE("RpcStringFree returned 0x%x\n", status);
76     }
77 
78     return hBinding;
79 }
80 
81 
82 void
83 __RPC_USER
84 PLSAPR_SERVER_NAME_unbind(PLSAPR_SERVER_NAME pszSystemName,
85                           handle_t hBinding)
86 {
87     RPC_STATUS status;
88 
89     TRACE("PLSAPR_SERVER_NAME_unbind() called\n");
90 
91     status = RpcBindingFree(&hBinding);
92     if (status)
93     {
94         TRACE("RpcBindingFree returned 0x%x\n", status);
95     }
96 }
97 
98 
99 /*
100  * @implemented
101  */
102 NTSTATUS
103 WINAPI
104 LsaAddAccountRights(IN LSA_HANDLE PolicyHandle,
105                     IN PSID AccountSid,
106                     IN PLSA_UNICODE_STRING UserRights,
107                     IN ULONG CountOfRights)
108 {
109     LSAPR_USER_RIGHT_SET UserRightSet;
110     NTSTATUS Status;
111 
112     TRACE("LsaAddAccountRights(%p %p %p 0x%08x)\n",
113           PolicyHandle, AccountSid, UserRights, CountOfRights);
114 
115     UserRightSet.Entries = CountOfRights;
116     UserRightSet.UserRights = (PRPC_UNICODE_STRING)UserRights;
117 
118     RpcTryExcept
119     {
120         Status = LsarAddAccountRights((LSAPR_HANDLE)PolicyHandle,
121                                       (PRPC_SID)AccountSid,
122                                       &UserRightSet);
123 
124     }
125     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
126     {
127         Status = I_RpcMapWin32Status(RpcExceptionCode());
128     }
129     RpcEndExcept;
130 
131     return Status;
132 }
133 
134 
135 /*
136  * @implemented
137  */
138 NTSTATUS
139 WINAPI
140 LsaAddPrivilegesToAccount(IN LSA_HANDLE AccountHandle,
141                           IN PPRIVILEGE_SET PrivilegeSet)
142 {
143     NTSTATUS Status;
144 
145     TRACE("LsaAddPrivilegesToAccount(%p %p)\n",
146           AccountHandle, PrivilegeSet);
147 
148     RpcTryExcept
149     {
150         Status = LsarAddPrivilegesToAccount((LSAPR_HANDLE)AccountHandle,
151                                             (PLSAPR_PRIVILEGE_SET)PrivilegeSet);
152     }
153     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
154     {
155         Status = I_RpcMapWin32Status(RpcExceptionCode());
156     }
157     RpcEndExcept;
158 
159     return Status;
160 }
161 
162 
163 /*
164  * @implemented
165  */
166 NTSTATUS
167 WINAPI
168 LsaClearAuditLog(IN LSA_HANDLE PolicyHandle)
169 {
170     NTSTATUS Status;
171 
172     TRACE("LsaClearAuditLog(%p)\n", PolicyHandle);
173 
174     RpcTryExcept
175     {
176         Status = LsarClearAuditLog((LSAPR_HANDLE)PolicyHandle);
177     }
178     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
179     {
180         Status = I_RpcMapWin32Status(RpcExceptionCode());
181     }
182     RpcEndExcept;
183 
184     return Status;
185 }
186 
187 
188 /*
189  * @implemented
190  */
191 NTSTATUS
192 WINAPI
193 LsaClose(IN LSA_HANDLE ObjectHandle)
194 {
195     NTSTATUS Status;
196 
197     TRACE("LsaClose(%p) called\n", ObjectHandle);
198 
199     RpcTryExcept
200     {
201         Status = LsarClose((PLSAPR_HANDLE)&ObjectHandle);
202     }
203     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
204     {
205         Status = I_RpcMapWin32Status(RpcExceptionCode());
206     }
207     RpcEndExcept;
208 
209     return Status;
210 }
211 
212 
213 /*
214  * @implemented
215  */
216 NTSTATUS
217 WINAPI
218 LsaCreateAccount(IN LSA_HANDLE PolicyHandle,
219                  IN PSID AccountSid,
220                  IN ACCESS_MASK DesiredAccess,
221                  OUT PLSA_HANDLE AccountHandle)
222 {
223     NTSTATUS Status;
224 
225     TRACE("LsaCreateAccount(%p %p 0x%08lx %p)\n",
226           PolicyHandle, AccountSid, DesiredAccess, AccountHandle);
227 
228     RpcTryExcept
229     {
230         Status = LsarCreateAccount((LSAPR_HANDLE)PolicyHandle,
231                                    AccountSid,
232                                    DesiredAccess,
233                                    AccountHandle);
234     }
235     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
236     {
237         Status = I_RpcMapWin32Status(RpcExceptionCode());
238     }
239     RpcEndExcept;
240 
241     return Status;
242 }
243 
244 
245 /*
246  * @implemented
247  */
248 NTSTATUS
249 WINAPI
250 LsaCreateSecret(IN LSA_HANDLE PolicyHandle,
251                 IN PLSA_UNICODE_STRING SecretName,
252                 IN ACCESS_MASK DesiredAccess,
253                 OUT PLSA_HANDLE SecretHandle)
254 {
255     NTSTATUS Status;
256 
257     TRACE("LsaCreateSecret(%p %p 0x%08lx %p)\n",
258           PolicyHandle, SecretName, DesiredAccess, SecretHandle);
259 
260     RpcTryExcept
261     {
262         Status = LsarCreateSecret((LSAPR_HANDLE)PolicyHandle,
263                                   (PRPC_UNICODE_STRING)SecretName,
264                                   DesiredAccess,
265                                   SecretHandle);
266     }
267     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
268     {
269         Status = I_RpcMapWin32Status(RpcExceptionCode());
270     }
271     RpcEndExcept;
272 
273     return Status;
274 }
275 
276 
277 /*
278  * @implemented
279  */
280 NTSTATUS
281 WINAPI
282 LsaCreateTrustedDomain(IN LSA_HANDLE PolicyHandle,
283                        IN PLSA_TRUST_INFORMATION TrustedDomainInformation,
284                        IN ACCESS_MASK DesiredAccess,
285                        OUT PLSA_HANDLE TrustedDomainHandle)
286 {
287     NTSTATUS Status;
288 
289     TRACE("LsaCreateTrustedDomain(%p %p 0x%08lx %p)\n",
290           PolicyHandle, TrustedDomainInformation, DesiredAccess, TrustedDomainHandle);
291 
292     RpcTryExcept
293     {
294         Status = LsarCreateTrustedDomain((LSAPR_HANDLE)PolicyHandle,
295                                          (PLSAPR_TRUST_INFORMATION)TrustedDomainInformation,
296                                          DesiredAccess,
297                                          (PLSAPR_HANDLE)TrustedDomainHandle);
298     }
299     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
300     {
301         Status = I_RpcMapWin32Status(RpcExceptionCode());
302     }
303     RpcEndExcept;
304 
305     return Status;
306 }
307 
308 
309 /*
310  * @unimplemented
311  */
312 NTSTATUS
313 WINAPI
314 LsaCreateTrustedDomainEx(IN LSA_HANDLE PolicyHandle,
315                          IN PTRUSTED_DOMAIN_INFORMATION_EX TrustedDomainInformation,
316                          IN PTRUSTED_DOMAIN_AUTH_INFORMATION AuthenticationInformation,
317                          IN ACCESS_MASK DesiredAccess,
318                          OUT PLSA_HANDLE TrustedDomainHandle)
319 {
320     PLSAPR_TRUSTED_DOMAIN_AUTH_INFORMATION_INTERNAL EncryptedAuthInfo = NULL;
321     NTSTATUS Status;
322 
323     TRACE("LsaCreateTrustedDomainEx(%p %p %p 0x%08lx %p) stub\n",
324           PolicyHandle, TrustedDomainInformation, AuthenticationInformation,
325           DesiredAccess, TrustedDomainHandle);
326 
327     RpcTryExcept
328     {
329         /* FIXME: Encrypt AuthenticationInformation */
330 
331         Status = LsarCreateTrustedDomainEx2((LSAPR_HANDLE)PolicyHandle,
332                                             (PLSAPR_TRUSTED_DOMAIN_INFORMATION_EX)TrustedDomainInformation,
333                                             EncryptedAuthInfo,
334                                             DesiredAccess,
335                                             (PLSAPR_HANDLE)TrustedDomainHandle);
336     }
337     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
338     {
339         Status = I_RpcMapWin32Status(RpcExceptionCode());
340     }
341     RpcEndExcept;
342 
343     return Status;
344 }
345 
346 
347 /*
348  * @implemented
349  */
350 NTSTATUS
351 WINAPI
352 LsaDelete(IN LSA_HANDLE ObjectHandle)
353 {
354     NTSTATUS Status;
355 
356     TRACE("LsaDelete(%p)\n", ObjectHandle);
357 
358     RpcTryExcept
359     {
360         Status = LsarDelete((LSAPR_HANDLE)ObjectHandle);
361     }
362     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
363     {
364         Status = I_RpcMapWin32Status(RpcExceptionCode());
365     }
366     RpcEndExcept;
367 
368     return Status;
369 }
370 
371 
372 /*
373  * @implemented
374  */
375 NTSTATUS
376 WINAPI
377 LsaDeleteTrustedDomain(IN LSA_HANDLE PolicyHandle,
378                        IN PSID TrustedDomainSid)
379 {
380     NTSTATUS Status;
381 
382     TRACE("LsaDeleteTrustedDomain(%p %p)\n",
383           PolicyHandle, TrustedDomainSid);
384 
385     RpcTryExcept
386     {
387         Status = LsarDeleteTrustedDomain((LSAPR_HANDLE)PolicyHandle,
388                                          TrustedDomainSid);
389     }
390     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
391     {
392         Status = I_RpcMapWin32Status(RpcExceptionCode());
393     }
394     RpcEndExcept;
395 
396     return Status;
397 }
398 
399 
400 /*
401  * @implemented
402  */
403 NTSTATUS
404 WINAPI
405 LsaEnumerateAccountRights(IN LSA_HANDLE PolicyHandle,
406                           IN PSID AccountSid,
407                           OUT PLSA_UNICODE_STRING *UserRights,
408                           OUT PULONG CountOfRights)
409 {
410     LSAPR_USER_RIGHT_SET UserRightsSet;
411     NTSTATUS Status;
412 
413     TRACE("LsaEnumerateAccountRights(%p %p %p %p)\n",
414           PolicyHandle, AccountSid, UserRights, CountOfRights);
415 
416     UserRightsSet.Entries = 0;
417     UserRightsSet.UserRights = NULL;
418 
419     RpcTryExcept
420     {
421         Status = LsarEnumerateAccountRights((LSAPR_HANDLE)PolicyHandle,
422                                             AccountSid,
423                                             &UserRightsSet);
424 
425         *UserRights = (PUNICODE_STRING)UserRightsSet.UserRights;
426         *CountOfRights = UserRightsSet.Entries;
427     }
428     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
429     {
430         Status = I_RpcMapWin32Status(RpcExceptionCode());
431 
432         if (UserRightsSet.UserRights != NULL)
433             MIDL_user_free(UserRightsSet.UserRights);
434     }
435     RpcEndExcept;
436 
437     return Status;
438 }
439 
440 
441 /*
442  * @implemented
443  */
444 NTSTATUS
445 WINAPI
446 LsaEnumerateAccounts(IN LSA_HANDLE PolicyHandle,
447                      IN OUT PLSA_ENUMERATION_HANDLE EnumerationContext,
448                      OUT PVOID *Buffer,
449                      IN ULONG PreferedMaximumLength,
450                      OUT PULONG CountReturned)
451 {
452     LSAPR_ACCOUNT_ENUM_BUFFER AccountEnumBuffer;
453     NTSTATUS Status;
454 
455     TRACE("LsaEnumerateAccounts(%p %p %p %lu %p)\n",
456           PolicyHandle, EnumerationContext, Buffer,
457           PreferedMaximumLength, CountReturned);
458 
459     AccountEnumBuffer.EntriesRead = 0;
460     AccountEnumBuffer.Information = NULL;
461 
462     RpcTryExcept
463     {
464         Status = LsarEnumerateAccounts((LSAPR_HANDLE)PolicyHandle,
465                                        EnumerationContext,
466                                        &AccountEnumBuffer,
467                                        PreferedMaximumLength);
468 
469         *Buffer = AccountEnumBuffer.Information;
470         *CountReturned = AccountEnumBuffer.EntriesRead;
471     }
472     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
473     {
474         if (AccountEnumBuffer.Information != NULL)
475             MIDL_user_free(AccountEnumBuffer.Information);
476 
477         Status = I_RpcMapWin32Status(RpcExceptionCode());
478     }
479     RpcEndExcept;
480 
481     return Status;
482 }
483 
484 
485 /*
486  * @implemented
487  */
488 NTSTATUS
489 WINAPI
490 LsaEnumerateAccountsWithUserRight(IN LSA_HANDLE PolicyHandle,
491                                   IN PLSA_UNICODE_STRING UserRight OPTIONAL,
492                                   OUT PVOID *Buffer,
493                                   OUT PULONG CountReturned)
494 {
495     LSAPR_ACCOUNT_ENUM_BUFFER AccountEnumBuffer;
496     NTSTATUS Status;
497 
498     TRACE("LsaEnumerateAccountsWithUserRight(%p %p %p %p) stub\n",
499           PolicyHandle, UserRight, Buffer, CountReturned);
500 
501     AccountEnumBuffer.EntriesRead = 0;
502     AccountEnumBuffer.Information = NULL;
503 
504     RpcTryExcept
505     {
506         Status = LsarEnumerateAccountsWithUserRight((LSAPR_HANDLE)PolicyHandle,
507                                                     (PRPC_UNICODE_STRING)UserRight,
508                                                     &AccountEnumBuffer);
509 
510         *Buffer = AccountEnumBuffer.Information;
511         *CountReturned = AccountEnumBuffer.EntriesRead;
512     }
513     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
514     {
515         if (AccountEnumBuffer.Information != NULL)
516             MIDL_user_free(AccountEnumBuffer.Information);
517 
518         Status = I_RpcMapWin32Status(RpcExceptionCode());
519     }
520     RpcEndExcept;
521 
522     return Status;
523 }
524 
525 
526 /*
527  * @implemented
528  */
529 NTSTATUS
530 WINAPI
531 LsaEnumeratePrivileges(IN LSA_HANDLE PolicyHandle,
532                        IN OUT PLSA_ENUMERATION_HANDLE EnumerationContext,
533                        OUT PVOID *Buffer,
534                        IN ULONG PreferedMaximumLength,
535                        OUT PULONG CountReturned)
536 {
537     LSAPR_PRIVILEGE_ENUM_BUFFER PrivilegeEnumBuffer;
538     NTSTATUS Status;
539 
540     TRACE("LsaEnumeratePrivileges(%p %p %p %lu %p)\n",
541           PolicyHandle, EnumerationContext, Buffer,
542           PreferedMaximumLength, CountReturned);
543 
544     PrivilegeEnumBuffer.Entries = 0;
545     PrivilegeEnumBuffer.Privileges = NULL;
546 
547     RpcTryExcept
548     {
549         Status = LsarEnumeratePrivileges((LSAPR_HANDLE)PolicyHandle,
550                                          EnumerationContext,
551                                          &PrivilegeEnumBuffer,
552                                          PreferedMaximumLength);
553 
554         *Buffer = PrivilegeEnumBuffer.Privileges;
555         *CountReturned = PrivilegeEnumBuffer.Entries;
556 
557     }
558     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
559     {
560         if (PrivilegeEnumBuffer.Privileges != NULL)
561             MIDL_user_free(PrivilegeEnumBuffer.Privileges);
562 
563         Status = I_RpcMapWin32Status(RpcExceptionCode());
564     }
565     RpcEndExcept;
566 
567     return Status;
568 }
569 
570 
571 /*
572  * @implemented
573  */
574 NTSTATUS
575 WINAPI
576 LsaEnumeratePrivilegesOfAccount(IN LSA_HANDLE AccountHandle,
577                                 OUT PPRIVILEGE_SET *Privileges)
578 {
579     NTSTATUS Status;
580 
581     TRACE("LsaEnumeratePrivilegesOfAccount(%p %p)\n",
582           AccountHandle, Privileges);
583 
584     RpcTryExcept
585     {
586         Status = LsarEnumeratePrivilegesAccount((LSAPR_HANDLE)AccountHandle,
587                                                 (LSAPR_PRIVILEGE_SET **)Privileges);
588     }
589     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
590     {
591         Status = I_RpcMapWin32Status(RpcExceptionCode());
592     }
593     RpcEndExcept;
594 
595     return Status;
596 }
597 
598 
599 /*
600  * @unimplemented
601  */
602 NTSTATUS
603 WINAPI
604 LsaEnumerateTrustedDomains(IN LSA_HANDLE PolicyHandle,
605                            IN OUT PLSA_ENUMERATION_HANDLE EnumerationContext,
606                            OUT PVOID *Buffer,
607                            IN ULONG PreferedMaximumLength,
608                            OUT PULONG CountReturned)
609 {
610     FIXME("LsaEnumerateTrustedDomains(%p %p %p %lu %p) stub\n",
611           PolicyHandle, EnumerationContext, Buffer,
612           PreferedMaximumLength, CountReturned);
613 
614     if (CountReturned)
615         *CountReturned = 0;
616 
617     return STATUS_SUCCESS;
618 }
619 
620 
621 /*
622  * @unimplemented
623  */
624 NTSTATUS
625 WINAPI
626 LsaEnumerateTrustedDomainsEx(IN LSA_HANDLE PolicyHandle,
627                              IN OUT PLSA_ENUMERATION_HANDLE EnumerationContext,
628                              OUT PVOID *Buffer,
629                              IN ULONG PreferedMaximumLength,
630                              OUT PULONG CountReturned)
631 {
632     FIXME("LsaEnumerateTrustedDomainsEx(%p %p %p %lu %p) stub\n",
633           PolicyHandle, EnumerationContext, Buffer,
634           PreferedMaximumLength, CountReturned);
635 
636     if (CountReturned)
637         *CountReturned = 0;
638 
639     return STATUS_SUCCESS;
640 }
641 
642 
643 /*
644  * @implemented
645  */
646 NTSTATUS
647 WINAPI
648 LsaFreeMemory(IN PVOID Buffer)
649 {
650     TRACE("LsaFreeMemory(%p)\n", Buffer);
651     return RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
652 }
653 
654 
655 /*
656  * @implemented
657  */
658 NTSTATUS
659 WINAPI
660 LsaGetQuotasForAccount(IN LSA_HANDLE AccountHandle,
661                        OUT PQUOTA_LIMITS QuotaLimits)
662 {
663     NTSTATUS Status;
664 
665     TRACE("LsaGetQuotasForAccount(%p %p)\n",
666           AccountHandle, QuotaLimits);
667 
668     RpcTryExcept
669     {
670         Status = LsarGetQuotasForAccount((LSAPR_HANDLE)AccountHandle,
671                                          QuotaLimits);
672     }
673     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
674     {
675         Status = I_RpcMapWin32Status(RpcExceptionCode());
676     }
677     RpcEndExcept;
678 
679     return Status;
680 }
681 
682 
683 /*
684  * @unimplemented
685  */
686 NTSTATUS
687 WINAPI
688 LsaGetRemoteUserName(IN PLSA_UNICODE_STRING SystemName OPTIONAL,
689                      OUT PLSA_UNICODE_STRING *UserName,
690                      OUT PLSA_UNICODE_STRING *DomainName OPTIONAL)
691 {
692     PRPC_UNICODE_STRING UserNameString = NULL;
693     PRPC_UNICODE_STRING DomainNameString = NULL;
694     NTSTATUS Status;
695 
696     TRACE("LsaGetRemoteUserName(%s %p %p)\n",
697           SystemName ? debugstr_w(SystemName->Buffer) : "(null)",
698           UserName, DomainName);
699 
700     RpcTryExcept
701     {
702         Status = LsarGetUserName((PLSAPR_SERVER_NAME)SystemName,
703                                  &UserNameString,
704                                  (DomainName != NULL) ? &DomainNameString : NULL);
705 
706         *UserName = (PLSA_UNICODE_STRING)UserNameString;
707 
708         if (DomainName != NULL)
709             *DomainName = (PLSA_UNICODE_STRING)DomainNameString;
710     }
711     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
712     {
713         if (UserNameString != NULL)
714             MIDL_user_free(UserNameString);
715 
716         if (DomainNameString != NULL)
717             MIDL_user_free(DomainNameString);
718 
719         Status = I_RpcMapWin32Status(RpcExceptionCode());
720     }
721     RpcEndExcept;
722 
723     return Status;
724 }
725 
726 
727 /*
728  * @implemented
729  */
730 NTSTATUS
731 WINAPI
732 LsaGetSystemAccessAccount(IN LSA_HANDLE AccountHandle,
733                           OUT PULONG SystemAccess)
734 {
735     NTSTATUS Status;
736 
737     TRACE("LsaGetSystemAccessAccount(%p %p)\n",
738           AccountHandle, SystemAccess);
739 
740     RpcTryExcept
741     {
742         Status = LsarGetSystemAccessAccount((LSAPR_HANDLE)AccountHandle,
743                                             (ACCESS_MASK *)SystemAccess);
744     }
745     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
746     {
747         Status = I_RpcMapWin32Status(RpcExceptionCode());
748     }
749     RpcEndExcept;
750 
751     return Status;
752 }
753 
754 
755 /*
756  * @implemented
757  */
758 NTSTATUS
759 WINAPI
760 LsaGetUserName(OUT PUNICODE_STRING *UserName,
761                OUT PUNICODE_STRING *DomainName OPTIONAL)
762 {
763     PRPC_UNICODE_STRING UserNameString = NULL;
764     PRPC_UNICODE_STRING DomainNameString = NULL;
765     NTSTATUS Status;
766 
767     TRACE("LsaGetUserName(%p %p)\n",
768           UserName, DomainName);
769 
770     RpcTryExcept
771     {
772         Status = LsarGetUserName(NULL,
773                                  &UserNameString,
774                                  (DomainName != NULL) ? &DomainNameString : NULL);
775 
776         *UserName = (PUNICODE_STRING)UserNameString;
777 
778         if (DomainName != NULL)
779             *DomainName = (PUNICODE_STRING)DomainNameString;
780     }
781     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
782     {
783         if (UserNameString != NULL)
784             MIDL_user_free(UserNameString);
785 
786         if (DomainNameString != NULL)
787             MIDL_user_free(DomainNameString);
788 
789         Status = I_RpcMapWin32Status(RpcExceptionCode());
790     }
791     RpcEndExcept;
792 
793     return Status;
794 }
795 
796 
797 /*
798  * @implemented
799  */
800 NTSTATUS
801 WINAPI
802 LsaLookupNames(IN LSA_HANDLE PolicyHandle,
803                IN ULONG Count,
804                IN PLSA_UNICODE_STRING Names,
805                OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
806                OUT PLSA_TRANSLATED_SID *Sids)
807 {
808     LSAPR_TRANSLATED_SIDS TranslatedSids = {0, NULL};
809     ULONG MappedCount = 0;
810     NTSTATUS Status;
811 
812     TRACE("LsaLookupNames(%p %lu %p %p %p)\n",
813           PolicyHandle, Count, Names, ReferencedDomains, Sids);
814 
815     if (ReferencedDomains == NULL || Sids == NULL)
816         return STATUS_INVALID_PARAMETER;
817 
818     RpcTryExcept
819     {
820         *ReferencedDomains = NULL;
821         *Sids = NULL;
822 
823         TranslatedSids.Entries = Count;
824 
825         Status = LsarLookupNames((LSAPR_HANDLE)PolicyHandle,
826                                  Count,
827                                  (PRPC_UNICODE_STRING)Names,
828                                  (PLSAPR_REFERENCED_DOMAIN_LIST *)ReferencedDomains,
829                                  &TranslatedSids,
830                                  LsapLookupWksta,
831                                  &MappedCount);
832 
833         *Sids = (PLSA_TRANSLATED_SID)TranslatedSids.Sids;
834     }
835     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
836     {
837         if (TranslatedSids.Sids != NULL)
838             MIDL_user_free(TranslatedSids.Sids);
839 
840         Status = I_RpcMapWin32Status(RpcExceptionCode());
841     }
842     RpcEndExcept;
843 
844     return Status;
845 }
846 
847 
848 /*
849  * @implemented
850  */
851 NTSTATUS
852 WINAPI
853 LsaLookupNames2(IN LSA_HANDLE PolicyHandle,
854                 IN ULONG Flags,
855                 IN ULONG Count,
856                 IN PLSA_UNICODE_STRING Names,
857                 OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
858                 OUT PLSA_TRANSLATED_SID2 *Sids)
859 {
860     LSAPR_TRANSLATED_SIDS_EX2 TranslatedSids = {0, NULL};
861     ULONG MappedCount = 0;
862     NTSTATUS Status;
863 
864     TRACE("LsaLookupNames2(%p 0x%08x %lu %p %p %p)\n",
865           PolicyHandle, Flags, Count, Names, ReferencedDomains, Sids);
866 
867     if (ReferencedDomains == NULL || Sids == NULL)
868         return STATUS_INVALID_PARAMETER;
869 
870     RpcTryExcept
871     {
872         *ReferencedDomains = NULL;
873         *Sids = NULL;
874 
875         TranslatedSids.Entries = Count;
876 
877         Status = LsarLookupNames3((LSAPR_HANDLE)PolicyHandle,
878                                   Count,
879                                   (PRPC_UNICODE_STRING)Names,
880                                   (PLSAPR_REFERENCED_DOMAIN_LIST *)ReferencedDomains,
881                                   &TranslatedSids,
882                                   LsapLookupWksta,
883                                   &MappedCount,
884                                   Flags,
885                                   2);
886 
887         *Sids = (PLSA_TRANSLATED_SID2)TranslatedSids.Sids;
888     }
889     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
890     {
891         if (TranslatedSids.Sids != NULL)
892             MIDL_user_free(TranslatedSids.Sids);
893 
894         Status = I_RpcMapWin32Status(RpcExceptionCode());
895     }
896     RpcEndExcept;
897 
898     return Status;
899 }
900 
901 
902 /*
903  * @unimplemented
904  */
905 NTSTATUS
906 WINAPI
907 LsaLookupPrivilegeDisplayName(IN LSA_HANDLE PolicyHandle,
908                               IN PLSA_UNICODE_STRING Name,
909                               OUT PLSA_UNICODE_STRING *DisplayName,
910                               OUT PSHORT LanguageReturned)
911 {
912     FIXME("LsaLookupPrivilegeDisplayName(%p %p %p %p)\n",
913           PolicyHandle, Name, DisplayName, LanguageReturned);
914     return STATUS_NOT_IMPLEMENTED;
915 }
916 
917 
918 /*
919  * @implemented
920  */
921 NTSTATUS
922 WINAPI
923 LsaLookupPrivilegeName(IN LSA_HANDLE PolicyHandle,
924                        IN PLUID Value,
925                        OUT PUNICODE_STRING *Name)
926 {
927     PRPC_UNICODE_STRING NameBuffer = NULL;
928     NTSTATUS Status;
929 
930     TRACE("LsaLookupPrivilegeName(%p %p %p)\n",
931           PolicyHandle, Value, Name);
932 
933     RpcTryExcept
934     {
935         Status = LsarLookupPrivilegeName(PolicyHandle,
936                                          Value,
937                                          &NameBuffer);
938 
939         *Name = (PUNICODE_STRING)NameBuffer;
940     }
941     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
942     {
943         if (NameBuffer != NULL)
944             MIDL_user_free(NameBuffer);
945 
946         Status = I_RpcMapWin32Status(RpcExceptionCode());
947     }
948     RpcEndExcept;
949 
950     return Status;
951 }
952 
953 
954 /*
955  * @implemented
956  */
957 NTSTATUS
958 WINAPI
959 LsaLookupPrivilegeValue(IN LSA_HANDLE PolicyHandle,
960                         IN PLSA_UNICODE_STRING Name,
961                         OUT PLUID Value)
962 {
963     LUID Luid;
964     NTSTATUS Status;
965 
966     TRACE("LsaLookupPrivilegeValue(%p %p %p)\n",
967           PolicyHandle, Name, Value);
968 
969     RpcTryExcept
970     {
971         Status = LsarLookupPrivilegeValue(PolicyHandle,
972                                           (PRPC_UNICODE_STRING)Name,
973                                           &Luid);
974         if (Status == STATUS_SUCCESS)
975             *Value = Luid;
976     }
977     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
978     {
979         Status = I_RpcMapWin32Status(RpcExceptionCode());
980     }
981     RpcEndExcept;
982 
983     return Status;
984 }
985 
986 
987 /*
988  * @implemented
989  */
990 NTSTATUS
991 WINAPI
992 LsaLookupSids(IN LSA_HANDLE PolicyHandle,
993               IN ULONG Count,
994               IN PSID *Sids,
995               OUT PLSA_REFERENCED_DOMAIN_LIST *ReferencedDomains,
996               OUT PLSA_TRANSLATED_NAME *Names)
997 {
998     LSAPR_SID_ENUM_BUFFER SidEnumBuffer;
999     LSAPR_TRANSLATED_NAMES TranslatedNames;
1000     ULONG MappedCount = 0;
1001     NTSTATUS  Status;
1002 
1003     TRACE("LsaLookupSids(%p %lu %p %p %p)\n",
1004           PolicyHandle, Count, Sids, ReferencedDomains, Names);
1005 
1006     if (Count == 0)
1007         return STATUS_INVALID_PARAMETER;
1008 
1009     SidEnumBuffer.Entries = Count;
1010     SidEnumBuffer.SidInfo = (PLSAPR_SID_INFORMATION)Sids;
1011 
1012     RpcTryExcept
1013     {
1014         *ReferencedDomains = NULL;
1015         *Names = NULL;
1016 
1017         TranslatedNames.Entries = 0;
1018         TranslatedNames.Names = NULL;
1019 
1020         Status = LsarLookupSids((LSAPR_HANDLE)PolicyHandle,
1021                                 &SidEnumBuffer,
1022                                 (PLSAPR_REFERENCED_DOMAIN_LIST *)ReferencedDomains,
1023                                 &TranslatedNames,
1024                                 LsapLookupWksta,
1025                                 &MappedCount);
1026 
1027         *Names = (PLSA_TRANSLATED_NAME)TranslatedNames.Names;
1028     }
1029     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1030     {
1031         if (TranslatedNames.Names != NULL)
1032         {
1033             MIDL_user_free(TranslatedNames.Names);
1034         }
1035 
1036         Status = I_RpcMapWin32Status(RpcExceptionCode());
1037     }
1038     RpcEndExcept;
1039 
1040     return Status;
1041 }
1042 
1043 
1044 /******************************************************************************
1045  * LsaNtStatusToWinError
1046  *
1047  * PARAMS
1048  *   Status [I]
1049  *
1050  * @implemented
1051  */
1052 ULONG
1053 WINAPI
1054 LsaNtStatusToWinError(IN NTSTATUS Status)
1055 {
1056     TRACE("LsaNtStatusToWinError(0x%lx)\n", Status);
1057     return RtlNtStatusToDosError(Status);
1058 }
1059 
1060 
1061 /*
1062  * @implemented
1063  */
1064 NTSTATUS
1065 WINAPI
1066 LsaOpenAccount(IN LSA_HANDLE PolicyHandle,
1067                IN PSID AccountSid,
1068                IN ACCESS_MASK DesiredAccess,
1069                OUT PLSA_HANDLE AccountHandle)
1070 {
1071     NTSTATUS Status;
1072 
1073     TRACE("LsaOpenAccount(%p %p 0x%08lx %p)\n",
1074           PolicyHandle, AccountSid, DesiredAccess, AccountHandle);
1075 
1076     RpcTryExcept
1077     {
1078         Status = LsarOpenAccount((LSAPR_HANDLE)PolicyHandle,
1079                                  AccountSid,
1080                                  DesiredAccess,
1081                                  AccountHandle);
1082     }
1083     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1084     {
1085         Status = I_RpcMapWin32Status(RpcExceptionCode());
1086     }
1087     RpcEndExcept;
1088 
1089     return Status;
1090 }
1091 
1092 
1093 /******************************************************************************
1094  * LsaOpenPolicy
1095  *
1096  * PARAMS
1097  *   x1 []
1098  *   x2 []
1099  *   x3 []
1100  *   x4 []
1101  *
1102  * @implemented
1103  */
1104 NTSTATUS
1105 WINAPI
1106 LsaOpenPolicy(IN PLSA_UNICODE_STRING SystemName OPTIONAL,
1107               IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
1108               IN ACCESS_MASK DesiredAccess,
1109               OUT PLSA_HANDLE PolicyHandle)
1110 {
1111     NTSTATUS Status;
1112 
1113     TRACE("LsaOpenPolicy(%s %p 0x%08lx %p)\n",
1114           SystemName ? debugstr_w(SystemName->Buffer) : "(null)",
1115           ObjectAttributes, DesiredAccess, PolicyHandle);
1116 
1117     /* FIXME: RPC should take care of this */
1118     if (!LsapIsLocalComputer(SystemName))
1119         return RPC_NT_SERVER_UNAVAILABLE;
1120 
1121     RpcTryExcept
1122     {
1123         *PolicyHandle = NULL;
1124 
1125         Status = LsarOpenPolicy(SystemName ? SystemName->Buffer : NULL,
1126                                 (PLSAPR_OBJECT_ATTRIBUTES)ObjectAttributes,
1127                                 DesiredAccess,
1128                                 PolicyHandle);
1129     }
1130     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1131     {
1132         Status = I_RpcMapWin32Status(RpcExceptionCode());
1133     }
1134     RpcEndExcept;
1135 
1136     TRACE("LsaOpenPolicy() done (Status: 0x%08lx)\n", Status);
1137 
1138     return Status;
1139 }
1140 
1141 
1142 /*
1143  * @unimplemented
1144  */
1145 NTSTATUS
1146 WINAPI
1147 LsaOpenPolicySce(IN PLSA_UNICODE_STRING SystemName OPTIONAL,
1148                  IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
1149                  IN ACCESS_MASK DesiredAccess,
1150                  OUT PLSA_HANDLE PolicyHandle)
1151 {
1152     FIXME("LsaOpenPolicySce(%s %p 0x%08lx %p)\n",
1153           SystemName ? debugstr_w(SystemName->Buffer) : "(null)",
1154           ObjectAttributes, DesiredAccess, PolicyHandle);
1155     return STATUS_NOT_IMPLEMENTED;
1156 }
1157 
1158 
1159 /*
1160  * @implemented
1161  */
1162 NTSTATUS
1163 WINAPI
1164 LsaOpenSecret(IN LSA_HANDLE PolicyHandle,
1165               IN PLSA_UNICODE_STRING SecretName,
1166               IN ACCESS_MASK DesiredAccess,
1167               OUT PLSA_HANDLE SecretHandle)
1168 {
1169     NTSTATUS Status;
1170 
1171     TRACE("LsaOpenSecret(%p %p 0x%08lx %p)\n",
1172           PolicyHandle, SecretName, DesiredAccess, SecretHandle);
1173 
1174     RpcTryExcept
1175     {
1176         *SecretHandle = NULL;
1177 
1178         Status = LsarOpenSecret((LSAPR_HANDLE)PolicyHandle,
1179                                 (PRPC_UNICODE_STRING)SecretName,
1180                                 DesiredAccess,
1181                                 SecretHandle);
1182     }
1183     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1184     {
1185         Status = I_RpcMapWin32Status(RpcExceptionCode());
1186     }
1187     RpcEndExcept;
1188 
1189     TRACE("LsaOpenSecret() done (Status: 0x%08lx)\n", Status);
1190 
1191     return Status;
1192 }
1193 
1194 
1195 /*
1196  * @implemented
1197  */
1198 NTSTATUS
1199 WINAPI
1200 LsaOpenTrustedDomain(IN LSA_HANDLE PolicyHandle,
1201                      IN PSID TrustedDomainSid,
1202                      IN ACCESS_MASK DesiredAccess,
1203                      OUT PLSA_HANDLE TrustedDomainHandle)
1204 {
1205     NTSTATUS Status;
1206 
1207     TRACE("LsaOpenTrustedDomain(%p %p 0x%08lx %p)\n",
1208           PolicyHandle, TrustedDomainSid, DesiredAccess, TrustedDomainHandle);
1209 
1210     RpcTryExcept
1211     {
1212         Status = LsarOpenTrustedDomain((LSAPR_HANDLE)PolicyHandle,
1213                                        (PRPC_SID)TrustedDomainSid,
1214                                        DesiredAccess,
1215                                        (PLSAPR_HANDLE)TrustedDomainHandle);
1216     }
1217     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1218     {
1219         Status = I_RpcMapWin32Status(RpcExceptionCode());
1220     }
1221     RpcEndExcept;
1222 
1223     return Status;
1224 }
1225 
1226 
1227 /*
1228  * @implemented
1229  */
1230 NTSTATUS
1231 WINAPI
1232 LsaOpenTrustedDomainByName(IN LSA_HANDLE PolicyHandle,
1233                            IN PLSA_UNICODE_STRING TrustedDomainName,
1234                            IN ACCESS_MASK DesiredAccess,
1235                            OUT PLSA_HANDLE TrustedDomainHandle)
1236 {
1237     NTSTATUS Status;
1238 
1239     TRACE("LsaOpenTrustedDomainByName(%p %p 0x%08lx %p)\n",
1240           PolicyHandle, TrustedDomainName, DesiredAccess, TrustedDomainHandle);
1241 
1242     RpcTryExcept
1243     {
1244         Status = LsarOpenTrustedDomainByName((LSAPR_HANDLE)PolicyHandle,
1245                                              (PRPC_UNICODE_STRING)TrustedDomainName,
1246                                              DesiredAccess,
1247                                              TrustedDomainHandle);
1248     }
1249     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1250     {
1251         Status = I_RpcMapWin32Status(RpcExceptionCode());
1252     }
1253     RpcEndExcept;
1254 
1255     return Status;
1256 }
1257 
1258 
1259 /*
1260  * @implemented
1261  */
1262 NTSTATUS
1263 WINAPI
1264 LsaQueryDomainInformationPolicy(IN LSA_HANDLE PolicyHandle,
1265                                 IN POLICY_DOMAIN_INFORMATION_CLASS InformationClass,
1266                                 OUT PVOID *Buffer)
1267 {
1268     PLSAPR_POLICY_DOMAIN_INFORMATION PolicyInformation = NULL;
1269     NTSTATUS Status;
1270 
1271     TRACE("LsaQueryDomainInformationPolicy(%p %lu %p)\n",
1272           PolicyHandle, InformationClass, Buffer);
1273 
1274     RpcTryExcept
1275     {
1276         Status = LsarQueryDomainInformationPolicy((LSAPR_HANDLE)PolicyHandle,
1277                                                   InformationClass,
1278                                                   &PolicyInformation);
1279 
1280         *Buffer = PolicyInformation;
1281     }
1282     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1283     {
1284         if (PolicyInformation != NULL)
1285             MIDL_user_free(PolicyInformation);
1286 
1287         Status = I_RpcMapWin32Status(RpcExceptionCode());
1288     }
1289     RpcEndExcept;
1290 
1291     return Status;
1292 }
1293 
1294 
1295 /*
1296  * @implemented
1297  */
1298 NTSTATUS
1299 WINAPI
1300 LsaQueryForestTrustInformation(IN LSA_HANDLE PolicyHandle,
1301                                IN PLSA_UNICODE_STRING TrustedDomainName,
1302                                OUT PLSA_FOREST_TRUST_INFORMATION *ForestTrustInfo)
1303 {
1304     NTSTATUS Status;
1305 
1306     TRACE("LsaQueryForestTrustInformation(%p %p %p)\n",
1307           PolicyHandle, TrustedDomainName, ForestTrustInfo);
1308 
1309     RpcTryExcept
1310     {
1311         Status = LsarQueryForestTrustInformation((LSAPR_HANDLE)PolicyHandle,
1312                                                  TrustedDomainName,
1313                                                  ForestTrustDomainInfo,
1314                                                  ForestTrustInfo);
1315     }
1316     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1317     {
1318         Status = I_RpcMapWin32Status(RpcExceptionCode());
1319     }
1320     RpcEndExcept;
1321 
1322     return Status;
1323 }
1324 
1325 
1326 /*
1327  * @unimplemented
1328  */
1329 NTSTATUS
1330 WINAPI
1331 LsaQueryInfoTrustedDomain(IN LSA_HANDLE TrustedDomainHandle,
1332                           IN TRUSTED_INFORMATION_CLASS InformationClass,
1333                           OUT PVOID *Buffer)
1334 {
1335     FIXME("LsaQueryInfoTrustedDomain(%p %d %p) stub\n",
1336           TrustedDomainHandle, InformationClass, Buffer);
1337     return STATUS_NOT_IMPLEMENTED;
1338 }
1339 
1340 
1341 /*
1342  * @implemented
1343  */
1344 NTSTATUS
1345 WINAPI
1346 LsaQueryInformationPolicy(IN LSA_HANDLE PolicyHandle,
1347                           IN POLICY_INFORMATION_CLASS InformationClass,
1348                           OUT PVOID *Buffer)
1349 {
1350     PLSAPR_POLICY_INFORMATION PolicyInformation = NULL;
1351     NTSTATUS Status;
1352 
1353     TRACE("LsaQueryInformationPolicy(%p %d %p)\n",
1354           PolicyHandle, InformationClass, Buffer);
1355 
1356     RpcTryExcept
1357     {
1358         Status = LsarQueryInformationPolicy((LSAPR_HANDLE)PolicyHandle,
1359                                             InformationClass,
1360                                             &PolicyInformation);
1361         *Buffer = PolicyInformation;
1362     }
1363     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1364     {
1365         if (PolicyInformation != NULL)
1366             MIDL_user_free(PolicyInformation);
1367 
1368         Status = I_RpcMapWin32Status(RpcExceptionCode());
1369     }
1370     RpcEndExcept;
1371 
1372     TRACE("Done (Status: 0x%08x)\n", Status);
1373 
1374     return Status;
1375 }
1376 
1377 
1378 /*
1379  * @unimplemented
1380  */
1381 NTSTATUS
1382 WINAPI
1383 LsaQuerySecret(IN LSA_HANDLE SecretHandle,
1384                OUT PLSA_UNICODE_STRING *CurrentValue OPTIONAL,
1385                OUT PLARGE_INTEGER CurrentValueSetTime OPTIONAL,
1386                OUT PLSA_UNICODE_STRING *OldValue OPTIONAL,
1387                OUT PLARGE_INTEGER OldValueSetTime OPTIONAL)
1388 {
1389     PLSAPR_CR_CIPHER_VALUE EncryptedCurrentValue = NULL;
1390     PLSAPR_CR_CIPHER_VALUE EncryptedOldValue = NULL;
1391     PLSA_UNICODE_STRING DecryptedCurrentValue = NULL;
1392     PLSA_UNICODE_STRING DecryptedOldValue = NULL;
1393     SIZE_T BufferSize;
1394     NTSTATUS Status;
1395 
1396     TRACE("LsaQuerySecret(%p %p %p %p %p)\n",
1397           SecretHandle, CurrentValue, CurrentValueSetTime,
1398           OldValue, OldValueSetTime);
1399 
1400     RpcTryExcept
1401     {
1402         Status = LsarQuerySecret((PLSAPR_HANDLE)SecretHandle,
1403                                  &EncryptedCurrentValue,
1404                                  CurrentValueSetTime,
1405                                  &EncryptedOldValue,
1406                                  OldValueSetTime);
1407     }
1408     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1409     {
1410         Status = I_RpcMapWin32Status(RpcExceptionCode());
1411     }
1412     RpcEndExcept;
1413 
1414     if (!NT_SUCCESS(Status))
1415         goto done;
1416 
1417     /* Decrypt the current value */
1418     if (CurrentValue != NULL)
1419     {
1420          if (EncryptedCurrentValue == NULL)
1421          {
1422              *CurrentValue = NULL;
1423          }
1424          else
1425          {
1426              /* FIXME: Decrypt the current value */
1427              BufferSize = sizeof(LSA_UNICODE_STRING) + EncryptedCurrentValue->MaximumLength;
1428              DecryptedCurrentValue = midl_user_allocate(BufferSize);
1429              if (DecryptedCurrentValue == NULL)
1430              {
1431                  Status = STATUS_INSUFFICIENT_RESOURCES;
1432                  goto done;
1433              }
1434 
1435              DecryptedCurrentValue->Length = (USHORT)EncryptedCurrentValue->Length;
1436              DecryptedCurrentValue->MaximumLength = (USHORT)EncryptedCurrentValue->MaximumLength;
1437              DecryptedCurrentValue->Buffer = (PWSTR)(DecryptedCurrentValue + 1);
1438              RtlCopyMemory(DecryptedCurrentValue->Buffer,
1439                            EncryptedCurrentValue->Buffer,
1440                            EncryptedCurrentValue->Length);
1441 
1442              *CurrentValue = DecryptedCurrentValue;
1443          }
1444     }
1445 
1446     /* Decrypt the old value */
1447     if (OldValue != NULL)
1448     {
1449          if (EncryptedOldValue == NULL)
1450          {
1451              *OldValue = NULL;
1452          }
1453          else
1454          {
1455              /* FIXME: Decrypt the old value */
1456              BufferSize = sizeof(LSA_UNICODE_STRING) + EncryptedOldValue->MaximumLength;
1457              DecryptedOldValue = midl_user_allocate(BufferSize);
1458              if (DecryptedOldValue == NULL)
1459              {
1460                  Status = STATUS_INSUFFICIENT_RESOURCES;
1461                  goto done;
1462              }
1463 
1464              DecryptedOldValue->Length = (USHORT)EncryptedOldValue->Length;
1465              DecryptedOldValue->MaximumLength = (USHORT)EncryptedOldValue->MaximumLength;
1466              DecryptedOldValue->Buffer = (PWSTR)(DecryptedOldValue + 1);
1467              RtlCopyMemory(DecryptedOldValue->Buffer,
1468                            EncryptedOldValue->Buffer,
1469                            EncryptedOldValue->Length);
1470 
1471              *OldValue = DecryptedOldValue;
1472          }
1473     }
1474 
1475 done:
1476     if (!NT_SUCCESS(Status))
1477     {
1478         if (DecryptedCurrentValue != NULL)
1479             midl_user_free(DecryptedCurrentValue);
1480 
1481         if (DecryptedOldValue != NULL)
1482             midl_user_free(DecryptedOldValue);
1483 
1484         if (CurrentValue != NULL)
1485             *CurrentValue = NULL;
1486 
1487         if (OldValue != NULL)
1488             *OldValue = NULL;
1489     }
1490 
1491     if (EncryptedCurrentValue != NULL)
1492         midl_user_free(EncryptedCurrentValue);
1493 
1494     if (EncryptedOldValue != NULL)
1495         midl_user_free(EncryptedOldValue);
1496 
1497     return Status;
1498 }
1499 
1500 
1501 /*
1502  * @implemented
1503  */
1504 NTSTATUS
1505 WINAPI
1506 LsaQuerySecurityObject(IN LSA_HANDLE ObjectHandle,
1507                        IN SECURITY_INFORMATION SecurityInformation,
1508                        OUT PSECURITY_DESCRIPTOR *SecurityDescriptor)
1509 {
1510     LSAPR_SR_SECURITY_DESCRIPTOR SdBuffer;
1511     PLSAPR_SR_SECURITY_DESCRIPTOR SdPointer;
1512     NTSTATUS Status;
1513 
1514     TRACE("LsaQuerySecurityObject(%p %lx %p)\n",
1515           ObjectHandle, SecurityInformation, SecurityDescriptor);
1516 
1517     SdBuffer.Length = 0;
1518     SdBuffer.SecurityDescriptor = NULL;
1519 
1520     SdPointer = &SdBuffer;
1521 
1522     RpcTryExcept
1523     {
1524         Status = LsarQuerySecurityObject((LSAPR_HANDLE)ObjectHandle,
1525                                          SecurityInformation,
1526                                          &SdPointer);
1527         if (NT_SUCCESS(Status))
1528         {
1529             *SecurityDescriptor = SdBuffer.SecurityDescriptor;
1530         }
1531         else
1532         {
1533             *SecurityDescriptor = NULL;
1534         }
1535     }
1536     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1537     {
1538         Status = I_RpcMapWin32Status(RpcExceptionCode());
1539     }
1540     RpcEndExcept;
1541 
1542     return Status;
1543 }
1544 
1545 
1546 /*
1547  * @unimplemented
1548  */
1549 NTSTATUS
1550 WINAPI
1551 LsaQueryTrustedDomainInfo(IN LSA_HANDLE PolicyHandle,
1552                           IN PSID TrustedDomainSid,
1553                           IN TRUSTED_INFORMATION_CLASS InformationClass,
1554                           OUT PVOID *Buffer)
1555 {
1556     FIXME("LsaQueryTrustedDomainInfo(%p %p %d %p) stub\n",
1557           PolicyHandle, TrustedDomainSid, InformationClass, Buffer);
1558     return STATUS_OBJECT_NAME_NOT_FOUND;
1559 }
1560 
1561 
1562 /*
1563  * @implemented
1564  */
1565 NTSTATUS
1566 WINAPI
1567 LsaQueryTrustedDomainInfoByName(IN LSA_HANDLE PolicyHandle,
1568                                 IN PLSA_UNICODE_STRING TrustedDomainName,
1569                                 IN TRUSTED_INFORMATION_CLASS InformationClass,
1570                                 OUT PVOID *Buffer)
1571 {
1572     NTSTATUS Status;
1573 
1574     TRACE("LsaQueryTrustedDomainInfoByName(%p %p %d %p)\n",
1575           PolicyHandle, TrustedDomainName, InformationClass, Buffer);
1576 
1577     if (InformationClass == TrustedDomainAuthInformationInternal ||
1578         InformationClass == TrustedDomainFullInformationInternal)
1579         return STATUS_INVALID_INFO_CLASS;
1580 
1581     RpcTryExcept
1582     {
1583         Status = LsarQueryTrustedDomainInfoByName((LSAPR_HANDLE)PolicyHandle,
1584                                                   (PRPC_UNICODE_STRING)TrustedDomainName,
1585                                                   InformationClass,
1586                                                   (PLSAPR_TRUSTED_DOMAIN_INFO *)Buffer);
1587     }
1588     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1589     {
1590         Status = I_RpcMapWin32Status(RpcExceptionCode());
1591     }
1592     RpcEndExcept;
1593 
1594     return Status;
1595 }
1596 
1597 
1598 /*
1599  * @implemented
1600  */
1601 NTSTATUS
1602 WINAPI
1603 LsaRemoveAccountRights(IN LSA_HANDLE PolicyHandle,
1604                        IN PSID AccountSid,
1605                        IN BOOLEAN AllRights,
1606                        IN PLSA_UNICODE_STRING UserRights,
1607                        IN ULONG CountOfRights)
1608 {
1609     LSAPR_USER_RIGHT_SET UserRightSet;
1610 
1611     TRACE("LsaRemoveAccountRights(%p %p %d %p %lu)\n",
1612           PolicyHandle, AccountSid, AllRights, UserRights, CountOfRights);
1613 
1614     UserRightSet.Entries = CountOfRights;
1615     UserRightSet.UserRights = (PRPC_UNICODE_STRING)UserRights;
1616 
1617     RpcTryExcept
1618     {
1619         LsarRemoveAccountRights((LSAPR_HANDLE)PolicyHandle,
1620                                 (PRPC_SID)AccountSid,
1621                                 AllRights,
1622                                 &UserRightSet);
1623     }
1624     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1625     {
1626         I_RpcMapWin32Status(RpcExceptionCode());
1627     }
1628     RpcEndExcept;
1629 
1630     return STATUS_SUCCESS;
1631 }
1632 
1633 
1634 /*
1635  * @implemented
1636  */
1637 NTSTATUS
1638 WINAPI
1639 LsaRemovePrivilegesFromAccount(IN LSA_HANDLE AccountHandle,
1640                                IN BOOLEAN AllPrivileges,
1641                                IN PPRIVILEGE_SET Privileges OPTIONAL)
1642 {
1643     NTSTATUS Status;
1644 
1645     RpcTryExcept
1646     {
1647         Status = LsarRemovePrivilegesFromAccount((LSAPR_HANDLE)AccountHandle,
1648                                                  AllPrivileges,
1649                                                  (PLSAPR_PRIVILEGE_SET)Privileges);
1650     }
1651     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1652     {
1653         Status = I_RpcMapWin32Status(RpcExceptionCode());
1654     }
1655     RpcEndExcept;
1656 
1657     return Status;
1658 }
1659 
1660 
1661 /*
1662  * @unimplemented
1663  */
1664 NTSTATUS
1665 WINAPI
1666 LsaRetrievePrivateData(IN LSA_HANDLE PolicyHandle,
1667                        IN PLSA_UNICODE_STRING KeyName,
1668                        OUT PLSA_UNICODE_STRING *PrivateData)
1669 {
1670     FIXME("LsaRetrievePrivateData(%p %p %p) stub\n",
1671           PolicyHandle, KeyName, PrivateData);
1672     return STATUS_OBJECT_NAME_NOT_FOUND;
1673 }
1674 
1675 
1676 /*
1677  * @implemented
1678  */
1679 NTSTATUS
1680 WINAPI
1681 LsaSetDomainInformationPolicy(IN LSA_HANDLE PolicyHandle,
1682                               IN POLICY_DOMAIN_INFORMATION_CLASS InformationClass,
1683                               IN PVOID Buffer OPTIONAL)
1684 {
1685     NTSTATUS Status;
1686 
1687     TRACE("LsaSetDomainInformationPolicy(%p %d %p)\n",
1688           PolicyHandle, InformationClass, Buffer);
1689 
1690     RpcTryExcept
1691     {
1692         Status = LsarSetDomainInformationPolicy((LSAPR_HANDLE)PolicyHandle,
1693                                                 InformationClass,
1694                                                 (PLSAPR_POLICY_DOMAIN_INFORMATION)Buffer);
1695     }
1696     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1697     {
1698         Status = I_RpcMapWin32Status(RpcExceptionCode());
1699     }
1700     RpcEndExcept;
1701 
1702     return Status;
1703 }
1704 
1705 
1706 /*
1707  * @implemented
1708  */
1709 NTSTATUS
1710 WINAPI
1711 LsaSetForestTrustInformation(IN LSA_HANDLE PolicyHandle,
1712                              IN PLSA_UNICODE_STRING TrustedDomainName,
1713                              IN PLSA_FOREST_TRUST_INFORMATION ForestTrustInfo,
1714                              IN BOOL CheckOnly,
1715                              OUT PLSA_FOREST_TRUST_COLLISION_INFORMATION *CollisionInfo)
1716 {
1717     NTSTATUS Status;
1718 
1719     TRACE("LsaSetForestTrustInformation(%p %p %p %d %p)\n",
1720           PolicyHandle, TrustedDomainName, ForestTrustInfo, CheckOnly, CollisionInfo);
1721 
1722     RpcTryExcept
1723     {
1724         Status = LsarSetForestTrustInformation((LSAPR_HANDLE)PolicyHandle,
1725                                                TrustedDomainName,
1726                                                ForestTrustDomainInfo,
1727                                                ForestTrustInfo,
1728                                                CheckOnly,
1729                                                CollisionInfo);
1730     }
1731     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1732     {
1733         Status = I_RpcMapWin32Status(RpcExceptionCode());
1734     }
1735     RpcEndExcept;
1736 
1737     return Status;
1738 }
1739 
1740 
1741 /*
1742  * @implemented
1743  */
1744 NTSTATUS
1745 WINAPI
1746 LsaSetInformationPolicy(IN LSA_HANDLE PolicyHandle,
1747                         IN POLICY_INFORMATION_CLASS InformationClass,
1748                         IN PVOID Buffer)
1749 {
1750     NTSTATUS Status;
1751 
1752     TRACE("LsaSetInformationPolicy(%p %d %p)\n",
1753           PolicyHandle, InformationClass, Buffer);
1754 
1755     RpcTryExcept
1756     {
1757         Status = LsarSetInformationPolicy((LSAPR_HANDLE)PolicyHandle,
1758                                           InformationClass,
1759                                           (PLSAPR_POLICY_INFORMATION)Buffer);
1760     }
1761     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1762     {
1763         Status = I_RpcMapWin32Status(RpcExceptionCode());
1764     }
1765     RpcEndExcept;
1766 
1767     return Status;
1768 }
1769 
1770 
1771 /*
1772  * @unimplemented
1773  */
1774 NTSTATUS
1775 WINAPI
1776 LsaSetInformationTrustedDomain(IN LSA_HANDLE TrustedDomainHandle,
1777                                IN TRUSTED_INFORMATION_CLASS InformationClass,
1778                                IN PVOID Buffer)
1779 {
1780     FIXME("LsaSetInformationTrustedDomain(%p %d %p)\n",
1781           TrustedDomainHandle, InformationClass, Buffer);
1782     return STATUS_NOT_IMPLEMENTED;
1783 }
1784 
1785 
1786 /*
1787  * @implemented
1788  */
1789 NTSTATUS
1790 WINAPI
1791 LsaSetQuotasForAccount(IN LSA_HANDLE AccountHandle,
1792                        IN PQUOTA_LIMITS QuotaLimits)
1793 {
1794     NTSTATUS Status;
1795 
1796     TRACE("LsaSetQuotasForAccount(%p %p)\n",
1797           AccountHandle, QuotaLimits);
1798 
1799     RpcTryExcept
1800     {
1801         Status = LsarSetQuotasForAccount((LSAPR_HANDLE)AccountHandle,
1802                                          QuotaLimits);
1803     }
1804     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1805     {
1806         Status = I_RpcMapWin32Status(RpcExceptionCode());
1807     }
1808     RpcEndExcept;
1809 
1810     return Status;
1811 }
1812 
1813 
1814 /*
1815  * @implemented
1816  */
1817 NTSTATUS
1818 WINAPI
1819 LsaSetSecret(IN LSA_HANDLE SecretHandle,
1820              IN PLSA_UNICODE_STRING CurrentValue OPTIONAL,
1821              IN PLSA_UNICODE_STRING OldValue OPTIONAL)
1822 {
1823     PLSAPR_CR_CIPHER_VALUE EncryptedCurrentValue = NULL;
1824     PLSAPR_CR_CIPHER_VALUE EncryptedOldValue = NULL;
1825     SIZE_T BufferSize;
1826     NTSTATUS Status;
1827 
1828     TRACE("LsaSetSecret(%p %p %p)\n",
1829           SecretHandle, EncryptedCurrentValue, EncryptedOldValue);
1830 
1831     if (CurrentValue != NULL)
1832     {
1833         BufferSize = sizeof(LSAPR_CR_CIPHER_VALUE) + CurrentValue->MaximumLength;
1834         EncryptedCurrentValue = midl_user_allocate(BufferSize);
1835         if (EncryptedCurrentValue == NULL)
1836         {
1837             Status = STATUS_INSUFFICIENT_RESOURCES;
1838             goto done;
1839         }
1840 
1841         EncryptedCurrentValue->Length = CurrentValue->Length;
1842         EncryptedCurrentValue->MaximumLength = CurrentValue->MaximumLength;
1843         EncryptedCurrentValue->Buffer = (BYTE *)(EncryptedCurrentValue + 1);
1844         if (EncryptedCurrentValue->Buffer != NULL)
1845             memcpy(EncryptedCurrentValue->Buffer, CurrentValue->Buffer, CurrentValue->Length);
1846     }
1847 
1848     if (OldValue != NULL)
1849     {
1850         BufferSize = sizeof(LSAPR_CR_CIPHER_VALUE) + OldValue->MaximumLength;
1851         EncryptedOldValue = midl_user_allocate(BufferSize);
1852         if (EncryptedOldValue == NULL)
1853         {
1854             Status = STATUS_INSUFFICIENT_RESOURCES;
1855             goto done;
1856         }
1857 
1858         EncryptedOldValue->Length = OldValue->Length;
1859         EncryptedOldValue->MaximumLength = OldValue->MaximumLength;
1860         EncryptedOldValue->Buffer = (BYTE*)(EncryptedOldValue + 1);
1861         if (EncryptedOldValue->Buffer != NULL)
1862             memcpy(EncryptedOldValue->Buffer, OldValue->Buffer, OldValue->Length);
1863     }
1864 
1865     RpcTryExcept
1866     {
1867         Status = LsarSetSecret((LSAPR_HANDLE)SecretHandle,
1868                                EncryptedCurrentValue,
1869                                EncryptedOldValue);
1870     }
1871     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1872     {
1873         Status = I_RpcMapWin32Status(RpcExceptionCode());
1874     }
1875     RpcEndExcept;
1876 
1877 done:
1878     if (EncryptedCurrentValue != NULL)
1879         midl_user_free(EncryptedCurrentValue);
1880 
1881     if (EncryptedOldValue != NULL)
1882         midl_user_free(EncryptedOldValue);
1883 
1884     return Status;
1885 }
1886 
1887 
1888 /*
1889  * @implemented
1890  */
1891 NTSTATUS
1892 WINAPI
1893 LsaSetSecurityObject(IN LSA_HANDLE ObjectHandle,
1894                      IN SECURITY_INFORMATION SecurityInformation,
1895                      IN PSECURITY_DESCRIPTOR SecurityDescriptor)
1896 {
1897     LSAPR_SR_SECURITY_DESCRIPTOR SdBuffer = {0, NULL};
1898     ULONG SdLength = 0;
1899     NTSTATUS Status;
1900 
1901     TRACE("LsaSetSecurityObject(%p %lx %p)\n",
1902           ObjectHandle, SecurityInformation, SecurityDescriptor);
1903 
1904     Status = RtlMakeSelfRelativeSD(SecurityDescriptor,
1905                                    NULL,
1906                                    &SdLength);
1907     if (Status != STATUS_BUFFER_TOO_SMALL)
1908         return STATUS_INVALID_PARAMETER;
1909 
1910     SdBuffer.SecurityDescriptor = MIDL_user_allocate(SdLength);
1911     if (SdBuffer.SecurityDescriptor == NULL)
1912         return STATUS_INSUFFICIENT_RESOURCES;
1913 
1914     Status = RtlMakeSelfRelativeSD(SecurityDescriptor,
1915                                    (PSECURITY_DESCRIPTOR)SdBuffer.SecurityDescriptor,
1916                                    &SdLength);
1917     if (!NT_SUCCESS(Status))
1918         goto done;
1919 
1920     SdBuffer.Length = SdLength;
1921 
1922     RpcTryExcept
1923     {
1924         Status = LsarSetSecurityObject((LSAPR_HANDLE)ObjectHandle,
1925                                        SecurityInformation,
1926                                        &SdBuffer);
1927     }
1928     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1929     {
1930         Status = I_RpcMapWin32Status(RpcExceptionCode());
1931     }
1932     RpcEndExcept;
1933 
1934 done:
1935     if (SdBuffer.SecurityDescriptor != NULL)
1936         MIDL_user_free(SdBuffer.SecurityDescriptor);
1937 
1938     return Status;
1939 }
1940 
1941 
1942 /*
1943  * @implemented
1944  */
1945 NTSTATUS
1946 WINAPI
1947 LsaSetSystemAccessAccount(IN LSA_HANDLE AccountHandle,
1948                           IN ULONG SystemAccess)
1949 {
1950     NTSTATUS Status;
1951 
1952     TRACE("LsaSetSystemAccessAccount(%p 0x%lx)\n",
1953           AccountHandle, SystemAccess);
1954 
1955     RpcTryExcept
1956     {
1957         Status = LsarSetSystemAccessAccount((LSAPR_HANDLE)AccountHandle,
1958                                             SystemAccess);
1959     }
1960     RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1961     {
1962         Status = I_RpcMapWin32Status(RpcExceptionCode());
1963     }
1964     RpcEndExcept;
1965 
1966     return Status;
1967 }
1968 
1969 
1970 /*
1971  * @unimplemented
1972  */
1973 NTSTATUS
1974 WINAPI
1975 LsaSetTrustedDomainInfoByName(IN LSA_HANDLE PolicyHandle,
1976                               IN PLSA_UNICODE_STRING TrustedDomainName,
1977                               IN TRUSTED_INFORMATION_CLASS InformationClass,
1978                               IN PVOID Buffer)
1979 {
1980     FIXME("LsaSetTrustedDomainInfoByName(%p %p %d %p) stub\n",
1981           PolicyHandle, TrustedDomainName, InformationClass, Buffer);
1982     return STATUS_SUCCESS;
1983 }
1984 
1985 
1986 /*
1987  * @unimplemented
1988  */
1989 NTSTATUS
1990 WINAPI
1991 LsaSetTrustedDomainInformation(IN LSA_HANDLE PolicyHandle,
1992                                IN PSID TrustedDomainSid,
1993                                IN TRUSTED_INFORMATION_CLASS InformationClass,
1994                                IN PVOID Buffer)
1995 {
1996     FIXME("LsaSetTrustedDomainInformation(%p %p %d %p) stub\n",
1997           PolicyHandle, TrustedDomainSid, InformationClass, Buffer);
1998     return STATUS_SUCCESS;
1999 }
2000 
2001 
2002 /*
2003  * @unimplemented
2004  */
2005 NTSTATUS
2006 WINAPI
2007 LsaStorePrivateData(IN LSA_HANDLE PolicyHandle,
2008                     IN PLSA_UNICODE_STRING KeyName,
2009                     IN PLSA_UNICODE_STRING PrivateData OPTIONAL)
2010 {
2011     FIXME("LsaStorePrivateData(%p %p %p) stub\n",
2012           PolicyHandle, KeyName, PrivateData);
2013     return STATUS_OBJECT_NAME_NOT_FOUND;
2014 }
2015 
2016 /* EOF */
2017