/* * Retrieve the SID of the current user. The returned PSID must be freed by the caller using LocalFree() */ static PSID GetSid(void) { TOKEN_USER* tu = NULL; DWORD len; HANDLE token; PSID ret = NULL; char* psid_string = NULL; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) { dprintf("OpenProcessToken failed: %s", WindowsErrorString()); return NULL; } if (!GetTokenInformation(token, TokenUser, tu, 0, &len)) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { dprintf("GetTokenInformation (pre) failed: %s", WindowsErrorString()); return NULL; } tu = (TOKEN_USER*)calloc(1, len); } if (tu == NULL) { return NULL; } if (GetTokenInformation(token, TokenUser, tu, len, &len)) { /* * now of course, the interesting thing is that if you return tu->User.Sid * but free tu, the PSID pointer becomes invalid after a while. * The workaround? Convert to string then back to PSID */ if (!ConvertSidToStringSidA(tu->User.Sid, &psid_string)) { dprintf("unable to convert SID to string: %s", WindowsErrorString()); ret = NULL; } else { if (!ConvertStringSidToSidA(psid_string, &ret)) { dprintf("unable to convert string back to SID: %s", WindowsErrorString()); ret = NULL; } // MUST use LocalFree() LocalFree(psid_string); } } else { ret = NULL; dprintf("GetTokenInformation (real) failed: %s", WindowsErrorString()); } free(tu); return ret; }
// TODO: allow commandline options (v2) // TODO: remove existing infs for similar devices (v2) int __cdecl main(int argc_ansi, char** argv_ansi) { DWORD r; BOOL b; int i, ret, argc = argc_ansi, si=0; char** argv = argv_ansi; wchar_t **wenv, **wargv; char* hardware_id = NULL; char* device_id = NULL; char* user_sid = NULL; char* inf_name = NULL; char path[MAX_PATH_LENGTH]; char destname[MAX_PATH_LENGTH]; uintptr_t syslog_reader_thid = -1L; // Connect to the messaging pipe pipe_handle = CreateFileA(INSTALLER_PIPE_NAME, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL); if (pipe_handle == INVALID_HANDLE_VALUE) { // If we can't connect to the pipe, someone is probably trying to run us standalone printf("This application can not be run from the command line.\n"); printf("Please use your initial installer application if you want to install the driver.\n"); return WDI_ERROR_NOT_SUPPORTED; } if (init_dlls()) { plog("could not init DLLs"); ret = WDI_ERROR_RESOURCE; goto out; } // Initialize COM for Restore Point disabling CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // libwdi provides the arguments as UTF-16 => read them and convert to UTF-8 if (__wgetmainargs != NULL) { __wgetmainargs(&argc, &wargv, &wenv, 1, &si); argv = calloc(argc, sizeof(char*)); for (i=0; i<argc; i++) { argv[i] = wchar_to_utf8(wargv[i]); } } else { plog("unable to access UTF-16 args - trying ANSI"); } if (argc < 2) { printf("usage: %s <inf_name>\n", argv[0]); plog("missing inf_name parameter"); } inf_name = argv[1]; plog("got parameter %s", argv[1]); r = GetFullPathNameU(".", MAX_PATH_LENGTH, path, NULL); if ((r == 0) || (r > MAX_PATH_LENGTH)) { plog("could not retrieve absolute path of working directory"); ret = WDI_ERROR_ACCESS; goto out; } safe_strcat(path, MAX_PATH_LENGTH, "\\"); safe_strcat(path, MAX_PATH_LENGTH, inf_name); device_id = req_id(IC_GET_DEVICE_ID); hardware_id = req_id(IC_GET_HARDWARE_ID); // Will be used if we ever need to create a file, as the original user, from this app user_sid = req_id(IC_GET_USER_SID); ConvertStringSidToSidA(user_sid, &user_psid); // Setup the syslog reader thread syslog_ready_event = CreateEvent(NULL, TRUE, FALSE, NULL); syslog_terminate_event = CreateEvent(NULL, TRUE, FALSE, NULL); syslog_reader_thid = _beginthread(syslog_reader_thread, 0, 0); if ( (syslog_reader_thid == -1L) || (WaitForSingleObject(syslog_ready_event, 2000) != WAIT_OBJECT_0) ) { plog("Unable to create syslog reader thread"); SetEvent(syslog_terminate_event); // NB: if you try to close the syslog reader thread handle, you get a // "more recent driver was found" error from UpdateForPnP. Weird... } // Disable the creation of a restore point disable_system_restore(true); // Find if the device is plugged in send_status(IC_SET_TIMEOUT_INFINITE); if (hardware_id != NULL) { plog("Installing driver for %s - please wait...", hardware_id); b = UpdateDriverForPlugAndPlayDevicesU(NULL, hardware_id, path, INSTALLFLAG_FORCE, NULL); send_status(IC_SET_TIMEOUT_DEFAULT); if (b == true) { // Success plog("driver update completed"); enumerate_device(device_id); ret = WDI_SUCCESS; goto out; } ret = process_error(GetLastError(), path); if (ret != WDI_SUCCESS) { goto out; } } // TODO: try URL for OEMSourceMediaLocation (v2) plog("Copying inf file (for the next time device is plugged) - please wait..."); send_status(IC_SET_TIMEOUT_INFINITE); b = SetupCopyOEMInfU(path, NULL, SPOST_PATH, 0, destname, MAX_PATH_LENGTH, NULL, NULL); send_status(IC_SET_TIMEOUT_DEFAULT); if (b) { plog("copied inf to %s", destname); ret = WDI_SUCCESS; enumerate_device(device_id); goto out; } ret = process_error(GetLastError(), path); if (ret != WDI_SUCCESS) { goto out; } // If needed, flag removed devices for reinstallation. see: // http://msdn.microsoft.com/en-us/library/aa906206.aspx check_removed(hardware_id); out: // Report any error status code and wait for target app to read it send_status(IC_INSTALLER_COMPLETED); pstat(ret); // Restore the system restore point creation original settings disable_system_restore(false); // TODO: have libwi send an ACK? Sleep(1000); SetEvent(syslog_terminate_event); if (argv != argv_ansi) { for (i=0; i<argc; i++) { safe_free(argv[i]); } safe_free(argv); } CloseHandle(syslog_ready_event); CloseHandle(syslog_terminate_event); CloseHandle((HANDLE)syslog_reader_thid); CloseHandle(pipe_handle); return ret; }
static void test_LsaLookupSids(void) { LSA_REFERENCED_DOMAIN_LIST *list; LSA_OBJECT_ATTRIBUTES attrs; LSA_TRANSLATED_NAME *names; LSA_HANDLE policy; TOKEN_USER *user; NTSTATUS status; HANDLE token; DWORD size; BOOL ret; PSID sid; memset(&attrs, 0, sizeof(attrs)); attrs.Length = sizeof(attrs); status = LsaOpenPolicy(NULL, &attrs, POLICY_LOOKUP_NAMES, &policy); ok(status == STATUS_SUCCESS, "got 0x%08x\n", status); ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &token); ok(ret, "got %d\n", ret); ret = GetTokenInformation(token, TokenUser, NULL, 0, &size); ok(!ret, "got %d\n", ret); user = HeapAlloc(GetProcessHeap(), 0, size); ret = GetTokenInformation(token, TokenUser, user, size, &size); ok(ret, "got %d\n", ret); status = LsaLookupSids(policy, 1, &user->User.Sid, &list, &names); ok(status == STATUS_SUCCESS, "got 0x%08x\n", status); ok(list->Entries > 0, "got %d\n", list->Entries); if (list->Entries) { ok((char*)list->Domains - (char*)list > 0, "%p, %p\n", list, list->Domains); ok((char*)list->Domains[0].Sid - (char*)list->Domains > 0, "%p, %p\n", list->Domains, list->Domains[0].Sid); ok(list->Domains[0].Name.MaximumLength > list->Domains[0].Name.Length, "got %d, %d\n", list->Domains[0].Name.MaximumLength, list->Domains[0].Name.Length); } LsaFreeMemory(names); LsaFreeMemory(list); HeapFree(GetProcessHeap(), 0, user); CloseHandle(token); ret = ConvertStringSidToSidA("S-1-1-0", &sid); ok(ret == TRUE, "ConvertStringSidToSidA returned false\n"); status = LsaLookupSids(policy, 1, &sid, &list, &names); ok(status == STATUS_SUCCESS, "got 0x%08x\n", status); ok(list->Entries > 0, "got %d\n", list->Entries); if (list->Entries) { ok((char*)list->Domains - (char*)list > 0, "%p, %p\n", list, list->Domains); ok((char*)list->Domains[0].Sid - (char*)list->Domains > 0, "%p, %p\n", list->Domains, list->Domains[0].Sid); ok(list->Domains[0].Name.MaximumLength > list->Domains[0].Name.Length, "got %d, %d\n", list->Domains[0].Name.MaximumLength, list->Domains[0].Name.Length); ok(list->Domains[0].Name.Buffer != NULL, "domain[0] name buffer is null\n"); } LsaFreeMemory(names); LsaFreeMemory(list); FreeSid(sid); status = LsaClose(policy); ok(status == STATUS_SUCCESS, "got 0x%08x\n", status); }