175 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
108 lines
7.2 KiB
C++
108 lines
7.2 KiB
C++
/* Source-only pre-credential DNS on the existing account worker.
|
|
* This code requires an explicitly qualified original login caller. */
|
|
extern int gw_private_worker_resolve(unsigned int profile,char out[16]);
|
|
LONG WINAPI gw_wire_account_active(void);
|
|
static int account_worker_busy_locked(void);
|
|
static SRWLOCK gw_preflight_lock=SRWLOCK_INIT;
|
|
static LONG gw_preflight_state;
|
|
static volatile LONG gw_preflight_launch_ready;
|
|
static HMODULE gw_preflight_module;
|
|
static unsigned int gw_preflight_profile,gw_preflight_generation,gw_preflight_last_generation;
|
|
static ULONGLONG gw_preflight_started,gw_preflight_completed;
|
|
static char gw_preflight_address[16];
|
|
static DWORD WINAPI gw_preflight_worker(void *unused){
|
|
char address[16]={0};unsigned int profile;int resolved,mailbox_ok;unsigned int i;HMODULE module;
|
|
(void)unused;
|
|
/* Do not retire until the owner has recorded the worker handle. */
|
|
while(!InterlockedCompareExchange(&gw_preflight_launch_ready,0,0))Sleep(0);
|
|
module=gw_preflight_module;
|
|
AcquireSRWLockShared(&gw_preflight_lock);profile=gw_preflight_profile;ReleaseSRWLockShared(&gw_preflight_lock);
|
|
mailbox_ok=gw_wire_auth_credentials_data.username_bytes==0 && gw_wire_auth_credentials_data.password_bytes==0 &&
|
|
!gw_wire_auth_data.username[0] && !gw_wire_auth_data.password_path[0] &&
|
|
InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,-2,-2)==-2;
|
|
resolved=mailbox_ok && gw_private_worker_resolve(profile,address)==0;
|
|
mailbox_ok=gw_wire_auth_credentials_data.username_bytes==0 && gw_wire_auth_credentials_data.password_bytes==0 &&
|
|
!gw_wire_auth_data.username[0] && !gw_wire_auth_data.password_path[0] &&
|
|
InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,-2,-2)==-2;
|
|
/* If a producer broke the reservation, do not erase its data or publish. */
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);
|
|
if(gw_preflight_state==1 && GetTickCount64()-gw_preflight_started>=3000)gw_preflight_state=-2;
|
|
if(gw_preflight_state==1 && resolved && mailbox_ok){
|
|
for(i=0;i<16;i++)gw_preflight_address[i]=address[i];
|
|
gw_preflight_completed=GetTickCount64();gw_preflight_state=2;
|
|
}else{
|
|
if(gw_preflight_state==1)gw_preflight_state=-1;
|
|
InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,0,-2);
|
|
}
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);
|
|
SecureZeroMemory(address,sizeof(address));InterlockedExchange(&active,0);
|
|
FreeLibraryAndExitThread(module,0);return 0;
|
|
}
|
|
/* Caller must complete this before enabling credential entry/capture. */
|
|
LONG WINAPI gw_private_preflight_start(unsigned int profile,unsigned int generation){
|
|
HANDLE worker;HMODULE module;struct gw_wire_auth_credentials *credentials=&gw_wire_auth_credentials_data;
|
|
if((profile!=2 && profile!=3 && profile!=4 && profile!=5)||!generation||gw_wire_account_active()||InterlockedCompareExchange(&active,1,0))return -1;
|
|
AcquireSRWLockExclusive(&worker_lock);
|
|
if(account_worker_busy_locked()){ReleaseSRWLockExclusive(&worker_lock);InterlockedExchange(&active,0);return -1;}
|
|
if(gw_wire_auth_data.state || gw_wire_auth_data.username[0] || gw_wire_auth_data.password_path[0] ||
|
|
credentials->abi!=1 || credentials->size!=sizeof(*credentials) || credentials->username_bytes || credentials->password_bytes ||
|
|
InterlockedCompareExchange((LONG*)&credentials->state,-2,0)!=0){InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -2;}
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);
|
|
if(generation<=gw_preflight_last_generation){
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);InterlockedCompareExchange((LONG*)&credentials->state,0,-2);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -3;
|
|
}
|
|
gw_preflight_last_generation=gw_preflight_generation=generation;gw_preflight_profile=profile;
|
|
gw_preflight_started=GetTickCount64();gw_preflight_completed=0;SecureZeroMemory(gw_preflight_address,sizeof(gw_preflight_address));gw_preflight_state=1;
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);
|
|
if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,(LPCWSTR)(const void*)&gw_preflight_worker,&module)){
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);gw_preflight_state=-1;ReleaseSRWLockExclusive(&gw_preflight_lock);
|
|
InterlockedCompareExchange((LONG*)&credentials->state,0,-2);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -4;
|
|
}
|
|
gw_preflight_module=module;InterlockedExchange(&gw_preflight_launch_ready,0);
|
|
worker=CreateThread(0,0,gw_preflight_worker,0,0,0);
|
|
if(!worker){
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);gw_preflight_state=-1;ReleaseSRWLockExclusive(&gw_preflight_lock);
|
|
InterlockedCompareExchange((LONG*)&credentials->state,0,-2);FreeLibrary(module);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -4;
|
|
}
|
|
worker_handle=worker;InterlockedExchange(&gw_preflight_launch_ready,1);ReleaseSRWLockExclusive(&worker_lock);return 0;
|
|
}
|
|
|
|
/* This read never waits for the provider: a timed-out worker stays pinned. */
|
|
LONG WINAPI gw_private_preflight_status(unsigned int profile,unsigned int generation){
|
|
LONG result=-3;AcquireSRWLockExclusive(&gw_preflight_lock);
|
|
if(profile==gw_preflight_profile && generation==gw_preflight_generation){
|
|
if(gw_preflight_state==1 && GetTickCount64()-gw_preflight_started>=3000)gw_preflight_state=-2;
|
|
result=gw_preflight_state;
|
|
}
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);return result;
|
|
}
|
|
|
|
/* Exactly one matching result, valid for 60 seconds, no partial output. */
|
|
LONG WINAPI gw_private_preflight_take(unsigned int profile,unsigned int generation,char out[16],ULONGLONG *resolved_at){
|
|
LONG result=-1;unsigned int i;
|
|
if(!out||gw_wire_account_active())return -1;
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);
|
|
if(gw_preflight_state==2 && profile==gw_preflight_profile && generation==gw_preflight_generation){
|
|
if(GetTickCount64()-gw_preflight_completed<60000 && !gw_wire_auth_credentials_data.username_bytes &&
|
|
!gw_wire_auth_credentials_data.password_bytes && !gw_wire_auth_data.username[0] && !gw_wire_auth_data.password_path[0] &&
|
|
InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,0,-2)==-2){
|
|
for(i=0;i<16;i++)out[i]=gw_preflight_address[i];
|
|
if(resolved_at)*resolved_at=gw_preflight_completed;
|
|
result=0;
|
|
}else InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,0,-2);
|
|
SecureZeroMemory(gw_preflight_address,sizeof(gw_preflight_address));gw_preflight_state=3;
|
|
}
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);return result;
|
|
}
|
|
|
|
/* Explicitly abandon a ready result and release its mailbox reservation. */
|
|
LONG WINAPI gw_private_preflight_discard(unsigned int profile,unsigned int generation){
|
|
LONG result=-1;if(gw_wire_account_active())return -1;
|
|
AcquireSRWLockExclusive(&gw_preflight_lock);
|
|
if(gw_preflight_state==2 && profile==gw_preflight_profile && generation==gw_preflight_generation){
|
|
InterlockedCompareExchange((LONG*)&gw_wire_auth_credentials_data.state,0,-2);
|
|
SecureZeroMemory(gw_preflight_address,sizeof(gw_preflight_address));gw_preflight_state=3;result=0;
|
|
}
|
|
ReleaseSRWLockExclusive(&gw_preflight_lock);return result;
|
|
}
|