1 /* cyglsa.c: LSA authentication module for Cygwin
3 Copyright 2006, 2008 Red Hat, Inc.
5 Written by Corinna Vinschen <corinna@vinschen.de>
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */
13 #define _CRT_SECURE_NO_DEPRECATE
15 #define WIN32_NO_STATUS
22 #include "../cygwin/cyglsa.h"
23 #include "../cygwin/include/cygwin/version.h"
25 static PLSA_SECPKG_FUNCS funcs;
26 static BOOL must_create_logon_sid;
29 DllMain (HINSTANCE inst, DWORD reason, LPVOID res)
33 case DLL_PROCESS_ATTACH:
34 case DLL_THREAD_ATTACH:
35 case DLL_THREAD_DETACH:
36 case DLL_PROCESS_DETACH:
42 #ifndef RtlInitEmptyUnicodeString
44 RtlInitEmptyUnicodeString(PUNICODE_STRING dest, PCWSTR buf, USHORT len)
47 dest->MaximumLength = len;
48 dest->Buffer = (PWSTR) buf;
52 static PUNICODE_STRING
53 uni_alloc (PWCHAR src, USHORT len)
57 if (!(tgt = funcs->AllocateLsaHeap (sizeof (UNICODE_STRING))))
59 tgt->Length = len * sizeof (WCHAR);
60 tgt->MaximumLength = tgt->Length + sizeof (WCHAR);
61 if (!(tgt->Buffer = funcs->AllocateLsaHeap (tgt->MaximumLength)))
63 funcs->FreeLsaHeap (tgt);
66 wcscpy (tgt->Buffer, src);
70 /* No, I don't want to include stdio.h so I take what ntdll offers. */
71 extern int _vsnprintf (char *, size_t, const char *, va_list);
73 static HANDLE fh = INVALID_HANDLE_VALUE;
76 printf (const char *format, ...)
83 if (fh == INVALID_HANDLE_VALUE)
86 va_start (ap, format);
87 ret = _vsnprintf (buf, 256, format, ap);
94 WriteFile (fh, buf, ret, &wr, NULL);
99 print_sid (const char *prefix, int idx, PISID sid)
103 printf ("%s", prefix);
105 printf ("[%d] ", idx);
106 printf ("(0x%08x) ", (INT_PTR) sid);
109 else if (IsBadReadPtr (sid, 8))
110 printf ("INVALID POINTER\n");
111 else if (!IsValidSid ((PSID) sid))
112 printf ("INVALID SID\n");
113 else if (IsBadReadPtr (sid, 8 + sizeof (DWORD) * sid->SubAuthorityCount))
114 printf ("INVALID POINTER SPACE\n");
117 printf ("S-%d-%d", sid->Revision, sid->IdentifierAuthority.Value[5]);
118 for (i = 0; i < sid->SubAuthorityCount; ++i)
119 printf ("-%lu", sid->SubAuthority[i]);
125 print_groups (PTOKEN_GROUPS grps)
129 printf ("Groups: (0x%08x) ", (INT_PTR) grps);
132 else if (IsBadReadPtr (grps, sizeof (DWORD)))
133 printf ("INVALID POINTER\n");
134 else if (IsBadReadPtr (grps, sizeof (DWORD) + sizeof (SID_AND_ATTRIBUTES)
136 printf ("INVALID POINTER SPACE\n");
139 printf ("Count: %lu\n", grps->GroupCount);
140 for (i = 0; i < grps->GroupCount; ++i)
142 printf ("(attr: 0x%lx)", grps->Groups[i].Attributes);
143 print_sid (" ", i, (PISID) grps->Groups[i].Sid);
149 print_privs (PTOKEN_PRIVILEGES privs)
153 printf ("Privileges: (0x%08x) ", (INT_PTR) privs);
156 else if (IsBadReadPtr (privs, sizeof (DWORD)))
157 printf ("INVALID POINTER\n");
158 else if (IsBadReadPtr (privs, sizeof (DWORD) + sizeof (LUID_AND_ATTRIBUTES)
159 * privs->PrivilegeCount))
160 printf ("INVALID POINTER SPACE\n");
163 printf ("Count: %lu\n", privs->PrivilegeCount);
164 for (i = 0; i < privs->PrivilegeCount; ++i)
165 printf ("Luid: {%ld, %lu} Attributes: 0x%lx\n",
166 privs->Privileges[i].Luid.HighPart,
167 privs->Privileges[i].Luid.LowPart,
168 privs->Privileges[i].Attributes);
173 print_dacl (PACL dacl)
177 printf ("DefaultDacl: (0x%08x) ", (INT_PTR) dacl);
180 else if (IsBadReadPtr (dacl, sizeof (ACL)))
181 printf ("INVALID POINTER\n");
182 else if (IsBadReadPtr (dacl, dacl->AclSize))
183 printf ("INVALID POINTER SPACE\n");
186 printf ("Rev: %d, Count: %d\n", dacl->AclRevision, dacl->AceCount);
187 for (i = 0; i < dacl->AceCount; ++i)
190 PACCESS_ALLOWED_ACE ace;
192 if (!GetAce (dacl, i, &vace))
193 printf ("[%lu] GetAce error %lu\n", i, GetLastError ());
196 ace = (PACCESS_ALLOWED_ACE) vace;
197 printf ("Type: %x, Flags: %x, Access: %lx,",
198 ace->Header.AceType, ace->Header.AceFlags, (DWORD) ace->Mask);
199 print_sid (" ", i, (PISID) &ace->SidStart);
206 print_tokinf (PLSA_TOKEN_INFORMATION_V2 ptok, size_t size,
207 PVOID got_start, PVOID gotinf_start, PVOID gotinf_end)
209 if (fh == INVALID_HANDLE_VALUE)
212 printf ("INCOMING: start: 0x%08x infstart: 0x%08x infend: 0x%08x\n",
213 (INT_PTR) got_start, (INT_PTR) gotinf_start,
214 (INT_PTR) gotinf_end);
216 printf ("LSA_TOKEN_INFORMATION_V2: 0x%08x - 0x%08x\n",
217 (INT_PTR) ptok, (INT_PTR) ptok + size);
220 printf ("User: (attr: 0x%lx)", ptok->User.User.Attributes);
221 print_sid (" ", -1, (PISID) ptok->User.User.Sid);
224 print_groups (ptok->Groups);
226 /* Primary Group SID */
227 print_sid ("Primary Group: ", -1, (PISID)ptok->PrimaryGroup.PrimaryGroup);
230 print_privs (ptok->Privileges);
233 print_sid ("Owner: ", -1, (PISID) ptok->Owner.Owner);
236 print_dacl (ptok->DefaultDacl.DefaultDacl);
242 LsaApInitializePackage (ULONG authp_id, PLSA_SECPKG_FUNCS dpt,
243 PLSA_STRING dummy1, PLSA_STRING dummy2,
244 PLSA_STRING *authp_name)
246 PLSA_STRING name = NULL;
247 DWORD vers, major, minor;
249 /* Set global pointer to lsa helper function table. */
252 /* Allocate and set the name of the authentication package. This is the
253 name which has to be used in LsaLookupAuthenticationPackage. */
254 if (!(name = funcs->AllocateLsaHeap (sizeof *name)))
255 return STATUS_NO_MEMORY;
256 if (!(name->Buffer = funcs->AllocateLsaHeap (sizeof (CYG_LSA_PKGNAME))))
258 funcs->FreeLsaHeap (name);
259 return STATUS_NO_MEMORY;
261 name->Length = sizeof (CYG_LSA_PKGNAME) - 1;
262 name->MaximumLength = sizeof (CYG_LSA_PKGNAME);
263 strcpy (name->Buffer, CYG_LSA_PKGNAME);
264 (*authp_name) = name;
266 vers = GetVersion ();
267 major = LOBYTE (LOWORD (vers));
268 minor = HIBYTE (LOWORD (vers));
269 /* Check if we're running on Windows 2000 or lower. If so, we must create
270 the logon sid in the group list by ourselves. */
271 if (major < 5 || (major == 5 && minor == 0))
272 must_create_logon_sid = TRUE;
275 fh = CreateFile ("C:\\cyglsa.dbgout", GENERIC_WRITE,
276 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
277 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
278 printf ("Initialized\n");
279 #endif /* DEBUGGING */
281 return STATUS_SUCCESS;
285 LsaApLogonUserEx (PLSA_CLIENT_REQUEST request, SECURITY_LOGON_TYPE logon_type,
286 PVOID auth, PVOID client_auth_base, ULONG auth_len,
287 PVOID *pbuf, PULONG pbuf_len, PLUID logon_id,
288 PNTSTATUS sub_stat, PLSA_TOKEN_INFORMATION_TYPE tok_type,
289 PVOID *tok, PUNICODE_STRING *account,
290 PUNICODE_STRING *authority, PUNICODE_STRING *machine)
295 SECPKG_CLIENT_INFO clinf;
296 PLSA_TOKEN_INFORMATION_V2 tokinf;
298 cyglsa_t *authinf = (cyglsa_t *) auth;
300 /* Check if the caller has the SeTcbPrivilege, otherwise refuse service. */
301 stat = funcs->GetClientInfo (&clinf);
302 if (stat != STATUS_SUCCESS)
304 printf ("GetClientInfo failed: 0x%08lx\n", stat);
307 if (!clinf.HasTcbPrivilege)
309 printf ("Client has no TCB privilege. Access denied.\n");
310 return STATUS_ACCESS_DENIED;
313 /* Make a couple of validity checks. */
314 if (auth_len < sizeof *authinf
315 || authinf->magic != CYG_LSA_MAGIC
316 || !authinf->username[0]
317 || !authinf->domain[0])
319 printf ("Invalid authentication parameter.\n");
320 return STATUS_INVALID_PARAMETER;
322 checksum = CYGWIN_VERSION_MAGIC (CYGWIN_VERSION_DLL_MAJOR,
323 CYGWIN_VERSION_DLL_MINOR);
324 csp = (PDWORD) &authinf->username;
325 csp_end = (PDWORD) ((PBYTE) authinf + auth_len);
326 while (csp < csp_end)
328 if (authinf->checksum != checksum)
330 printf ("Invalid checksum.\n");
331 return STATUS_INVALID_PARAMETER_3;
334 /* Set account to username and authority to domain resp. machine name.
335 The name of the logon account name as returned by LookupAccountSid
336 is created from here as "authority\account". */
337 authinf->username[UNLEN] = '\0';
338 authinf->domain[MAX_DOMAIN_NAME_LEN] = '\0';
339 if (account && !(*account = uni_alloc (authinf->username,
340 wcslen (authinf->username))))
342 printf ("No memory trying to create account.\n");
343 return STATUS_NO_MEMORY;
345 if (authority && !(*authority = uni_alloc (authinf->domain,
346 wcslen (authinf->domain))))
348 printf ("No memory trying to create authority.\n");
349 return STATUS_NO_MEMORY;
353 WCHAR mach[MAX_COMPUTERNAME_LENGTH + 1];
354 DWORD msize = MAX_COMPUTERNAME_LENGTH + 1;
355 if (!GetComputerNameW (mach, &msize))
356 wcscpy (mach, L"UNKNOWN");
357 if (!(*machine = uni_alloc (mach, wcslen (mach))))
359 printf ("No memory trying to create machine.\n");
360 return STATUS_NO_MEMORY;
363 /* Create a fake buffer in pbuf which is free'd again in the client.
364 Windows 2000 tends to crash when setting this pointer to NULL. */
367 #ifdef JUST_ANOTHER_NONWORKING_SOLUTION
369 WCHAR sam_username[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
370 SECURITY_STRING sam_user, prefix;
372 ULONG user_auth_size;
373 WCHAR flatname[UNLEN + 1];
374 UNICODE_STRING flatnm;
377 #endif /* JUST_ANOTHER_NONWORKING_SOLUTION */
379 stat = funcs->AllocateClientBuffer (request, 64UL, pbuf);
380 if (!LSA_SUCCESS (stat))
382 printf ("AllocateClientBuffer failed: 0x%08lx\n", stat);
385 #ifdef JUST_ANOTHER_NONWORKING_SOLUTION
386 prf.magic_pre = MAGIC_PRE;
388 prf.magic_post = MAGIC_POST;
391 /* That's how it was supposed to work according to MSDN... */
392 wcscpy (sam_username, authinf->domain);
393 wcscat (sam_username, L"\\");
394 wcscat (sam_username, authinf->username);
396 /* That's the only solution which worked, and then it only worked
397 for machine local accounts. No domain authentication possible.
398 STATUS_NO_SUCH_USER galore! */
399 wcscpy (sam_username, authinf->username);
401 RtlInitUnicodeString (&sam_user, sam_username);
402 RtlInitUnicodeString (&prefix, L"");
403 RtlInitEmptyUnicodeString (&flatnm, flatname,
404 (UNLEN + 1) * sizeof (WCHAR));
406 stat = funcs->GetAuthDataForUser (&sam_user, SecNameSamCompatible,
408 &user_auth_size, &flatnm);
409 if (!NT_SUCCESS (stat))
411 char sam_u[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
412 wcstombs (sam_u, sam_user.Buffer, sizeof (sam_u));
413 printf ("GetAuthDataForUser (%u,%u,%s) failed: 0x%08lx\n",
414 sam_user.Length, sam_user.MaximumLength, sam_u, stat);
418 memcpy (ts.SourceName, "Cygwin.1", 8);
419 ts.SourceIdentifier.HighPart = 0;
420 ts.SourceIdentifier.LowPart = 0x0104;
421 RtlInitEmptyUnicodeString (&flatnm, flatname,
422 (UNLEN + 1) * sizeof (WCHAR));
423 stat = funcs->ConvertAuthDataToToken (user_auth, user_auth_size,
424 SecurityDelegation, &ts,
425 Interactive, *authority,
426 &token, logon_id, &flatnm,
428 if (!NT_SUCCESS (stat))
430 printf ("ConvertAuthDataToToken failed: 0x%08lx\n", stat);
434 stat = funcs->DuplicateHandle (token, &prf.token);
435 if (!NT_SUCCESS (stat))
437 printf ("DuplicateHandle failed: 0x%08lx\n", stat);
441 stat = funcs->CopyToClientBuffer (request, sizeof prf, *pbuf, &prf);
442 if (!NT_SUCCESS (stat))
444 printf ("CopyToClientBuffer failed: 0x%08lx\n", stat);
447 funcs->FreeLsaHeap (user_auth);
448 #endif /* JUST_ANOTHER_NONWORKING_SOLUTION */
453 /* A PLSA_TOKEN_INFORMATION_V2 is allocated in one piece, so... */
454 #if defined (__x86_64__) || defined (_M_AMD64)
456 /* ...on 64 bit systems we have to convert the incoming 32 bit offsets
457 into 64 bit pointers. That requires to re-evaluate the size of the
458 outgoing tokinf structure and a somewhat awkward procedure to copy
459 the information over. */
464 PCYG_TOKEN_GROUPS src_grps;
466 PTOKEN_PRIVILEGES src_privs;
469 base = (LONG_PTR) &authinf->inf;
471 newsize = authinf->inf_size;
472 newsize += sizeof (TOKEN_USER) - sizeof (CYG_TOKEN_USER); /* User SID */
473 newsize += sizeof (PTOKEN_GROUPS) - sizeof (OFFSET); /* Groups */
474 src_grps = (PCYG_TOKEN_GROUPS) (base + authinf->inf.Groups);
475 newsize += src_grps->GroupCount /* Group SIDs */
476 * (sizeof (SID_AND_ATTRIBUTES)
477 - sizeof (CYG_SID_AND_ATTRIBUTES));
478 newsize += sizeof (PSID) - sizeof (OFFSET); /* Primary Group SID */
479 newsize += sizeof (PTOKEN_PRIVILEGES) - sizeof (OFFSET); /* Privileges */
480 newsize += 0; /* Owner SID */
481 newsize += sizeof (PACL) - sizeof (OFFSET); /* Default DACL */
483 if (!(tokinf = funcs->AllocateLsaHeap (newsize)))
484 return STATUS_NO_MEMORY;
485 tptr = (PBYTE)(tokinf + 1);
487 tokinf->ExpirationTime = authinf->inf.ExpirationTime;
489 src_sid = (PSID) (base + authinf->inf.User.User.Sid);
490 size = GetLengthSid (src_sid);
491 CopySid (size, (PSID) tptr, src_sid);
492 tokinf->User.User.Sid = (PSID) tptr;
494 tokinf->User.User.Attributes = authinf->inf.User.User.Attributes;
496 grps = (PTOKEN_GROUPS) tptr;
497 tokinf->Groups = grps;
498 grps->GroupCount = src_grps->GroupCount;
499 tptr += sizeof grps->GroupCount
500 + grps->GroupCount * sizeof (SID_AND_ATTRIBUTES);
502 for (i = 0; i < src_grps->GroupCount; ++i)
504 src_sid = (PSID) (base + src_grps->Groups[i].Sid);
505 size = GetLengthSid (src_sid);
506 CopySid (size, (PSID) tptr, src_sid);
507 tokinf->Groups->Groups[i].Sid = (PSID) tptr;
509 tokinf->Groups->Groups[i].Attributes = src_grps->Groups[i].Attributes;
511 /* Primary Group SID */
512 src_sid = (PSID) (base + authinf->inf.PrimaryGroup.PrimaryGroup);
513 size = GetLengthSid (src_sid);
514 CopySid (size, (PSID) tptr, src_sid);
515 tokinf->PrimaryGroup.PrimaryGroup = (PSID) tptr;
518 src_privs = (PTOKEN_PRIVILEGES) (base + authinf->inf.Privileges);
519 size = sizeof src_privs->PrivilegeCount
520 + src_privs->PrivilegeCount * sizeof (LUID_AND_ATTRIBUTES);
521 memcpy (tptr, src_privs, size);
522 tokinf->Privileges = (PTOKEN_PRIVILEGES) tptr;
525 tokinf->Owner.Owner = NULL;
527 src_acl = (PACL) (base + authinf->inf.DefaultDacl.DefaultDacl);
528 size = src_acl->AclSize;
529 memcpy (tptr, src_acl, size);
530 tokinf->DefaultDacl.DefaultDacl = (PACL) tptr;
534 /* ...on 32 bit systems we just allocate tokinf with the same size as
535 we get, copy the whole structure and convert offsets into pointers. */
537 /* Allocate LUID for usage in the logon SID on Windows 2000. This is
538 not done in the 64 bit code above for hopefully obvious reasons... */
541 if (must_create_logon_sid && !AllocateLocallyUniqueId (&logon_sid_id))
542 return STATUS_INSUFFICIENT_RESOURCES;
544 if (!(tokinf = funcs->AllocateLsaHeap (authinf->inf_size)))
545 return STATUS_NO_MEMORY;
546 memcpy (tokinf, &authinf->inf, authinf->inf_size);
549 tokinf->User.User.Sid = (PSID)
550 ((PBYTE) tokinf + (LONG_PTR) tokinf->User.User.Sid);
552 tokinf->Groups = (PTOKEN_GROUPS)
553 ((PBYTE) tokinf + (LONG_PTR) tokinf->Groups);
555 for (i = 0; i < tokinf->Groups->GroupCount; ++i)
557 tokinf->Groups->Groups[i].Sid = (PSID)
558 ((PBYTE) tokinf + (LONG_PTR) tokinf->Groups->Groups[i].Sid);
559 if (must_create_logon_sid
560 && tokinf->Groups->Groups[i].Attributes & SE_GROUP_LOGON_ID
561 && *GetSidSubAuthorityCount (tokinf->Groups->Groups[i].Sid) == 3
562 && *GetSidSubAuthority (tokinf->Groups->Groups[i].Sid, 0)
563 == SECURITY_LOGON_IDS_RID)
565 *GetSidSubAuthority (tokinf->Groups->Groups[i].Sid, 1)
566 = logon_sid_id.HighPart;
567 *GetSidSubAuthority (tokinf->Groups->Groups[i].Sid, 2)
568 = logon_sid_id.LowPart;
572 /* Primary Group SID */
573 tokinf->PrimaryGroup.PrimaryGroup = (PSID)
574 ((PBYTE) tokinf + (LONG_PTR) tokinf->PrimaryGroup.PrimaryGroup);
576 tokinf->Privileges = (PTOKEN_PRIVILEGES)
577 ((PBYTE) tokinf + (LONG_PTR) tokinf->Privileges);
579 tokinf->Owner.Owner = NULL;
581 tokinf->DefaultDacl.DefaultDacl = (PACL)
582 ((PBYTE) tokinf + (LONG_PTR) tokinf->DefaultDacl.DefaultDacl);
587 *tok = (PVOID) tokinf;
588 *tok_type = LsaTokenInformationV2;
590 print_tokinf (tokinf, authinf->inf_size, authinf, &authinf->inf,
591 (PVOID)((LONG_PTR) &authinf->inf + authinf->inf_size));
593 /* Create logon session. */
594 if (!AllocateLocallyUniqueId (logon_id))
596 funcs->FreeLsaHeap (*tok);
598 printf ("AllocateLocallyUniqueId failed: Win32 error %lu\n",
600 return STATUS_INSUFFICIENT_RESOURCES;
602 stat = funcs->CreateLogonSession (logon_id);
603 if (stat != STATUS_SUCCESS)
605 funcs->FreeLsaHeap (*tok);
607 printf ("CreateLogonSession failed: 0x%08lx\n", stat);
611 printf ("BINGO!!!\n", stat);
612 return STATUS_SUCCESS;
616 LsaApLogonTerminated(PLUID LogonId)
621 LsaApCallPackage (PLSA_CLIENT_REQUEST request, PVOID authinf,
622 PVOID client_auth_base, ULONG auth_len, PVOID *ret_buf,
623 PULONG ret_buf_len, PNTSTATUS ret_stat)
625 return STATUS_NOT_IMPLEMENTED;