Files

937 lines
56 KiB
C

/* A real Windows worker owns the genuine SRP account and prepared protocol
* channel. The private guild context is Source-name-derived only after proof;
* no callbacks, sockets, login outcome, persona or BattleTag is faked.
*/
#include <windows.h>
void gw_wire_ui_chosen_reset(void);
#include <intrin.h>
#include "wine_wire_abi.h"
#include "wine_wire_host.h"
#include "wire_public_hash_v1.private.h"
#include "wire_public_addon_v1.private.h"
#include "account_identity_compare.private.h"
#include "wine_session_retirement.h"
#include "wine_engine_realm.h"
#include "ui/source_account_cancel.h"
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
#include "priority_route_slot.h"
extern int gw_private_public_ipv4_eligible(const unsigned char[4]);
#endif
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
#include "wine_source_profile.h"
#endif
extern NTSTATUS WINAPI gw_wire_dispatch(struct gw_wire_call*);
extern void gw_wire_account_identity_note(uint32_t,uint32_t,uint32_t,uint32_t);
__declspec(dllexport) struct gw_wire_auth_control gw_wire_auth_data={.abi=1,.size=sizeof(struct gw_wire_auth_control)};
__declspec(dllexport) struct gw_wire_account_ui_control gw_wire_account_ui_data={.abi=1,.size=sizeof(struct gw_wire_account_ui_control)};
__declspec(dllexport) struct gw_native_realm gw_wire_auth_realms[64];
__declspec(dllexport) struct gw_wire_auth_credentials gw_wire_auth_credentials_data={.abi=1,.size=sizeof(struct gw_wire_auth_credentials)};
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
__declspec(dllexport) struct GwWirePublicHashV1 gw_wire_public_hash_data={.abi=1,.size=sizeof(struct GwWirePublicHashV1)};
__declspec(dllexport) struct GwWirePublicAddonV1 gw_wire_public_addon_data={.abi=1,.size=sizeof(struct GwWirePublicAddonV1)};
#endif
__declspec(dllexport) struct gw_wire_endpoint_profile gw_wire_endpoint_profile_data={.abi=1,.size=sizeof(struct gw_wire_endpoint_profile)};
static struct gw_wire_endpoint_profile owned_endpoint;
static volatile LONG active;
static uint32_t credentials_generation;
static volatile LONG identity_note_generation;
static uint32_t ui_auth_generation;
static uint32_t preference_generation;
static unsigned char preference_digest[32];
#if defined(GW_NATIVE_LINKED_BACKEND) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
#include "realm_preference_key.h"
#if GW_SOURCE_ONLY_PROFILE == 5
#define GW_SCOPE_TEXT_(x) #x
#define GW_SCOPE_TEXT(x) GW_SCOPE_TEXT_(x)
static const char preference_scope[] = "local/" GW_SOURCE_LOCAL_AUTH_IPV4 ":" GW_SCOPE_TEXT(GW_SOURCE_LOCAL_AUTH_PORT);
#else
static const char preference_scope[] = "octowow";
#endif
#endif
/* Genuine terminal outcomes remain visible after Source-owned retirement.
* Header only: no realm rows, account input or native login state is retained. */
static struct gw_wire_account_ui_state ui_terminal_failure;
static struct gw_wire_account_ui_state ui_terminal_closed;
static void account_ui_publish_terminal_failure(void);
static void account_ui_publish_terminal_closed(void);
static LONG ui_bridge_ready;
static HANDLE worker_handle;
static SRWLOCK worker_lock=SRWLOCK_INIT;
static volatile LONG retirement_complete;
static SRWLOCK channel_lock=SRWLOCK_INIT;
static uint64_t prepared_channel;
static struct gw_native_realm prepared_realm;
static uint32_t prepared_realm_index,prepared_profile_kind;
static uint16_t prepared_port;
__declspec(dllexport) struct gw_wire_session_lease gw_wire_session_lease_data={.abi=1,.size=sizeof(struct gw_wire_session_lease)};
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 3 || GW_SOURCE_ONLY_PROFILE == 4 || GW_SOURCE_ONLY_PROFILE == 5)
#define GW_SOURCE_ONLY_ACTIVE 1
#include "wine_source_preflight.inc"
#elif defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE != 0
#error Unsupported Source-only build profile
#else
#define GW_SOURCE_ONLY_ACTIVE 0
#endif
#include "wine_club_source_context.inc"
static LONG club_source_status;
LONG WINAPI gw_wire_club_source_context_state(void){return club_source_status;}
/* All lease inspection is borrowed under channel_lock. A retired/cancelled
* worker must return before touching lease fields or invoking its clock. */
static int engine_lease_attached_locked(void){
if(InterlockedCompareExchange(&active,0,0)!=1 ||
InterlockedCompareExchange((LONG*)&gw_wire_auth_data.state,0,0)!=2 ||
InterlockedCompareExchange((LONG*)&gw_wire_auth_data.cancel,0,0) ||
InterlockedCompareExchange(&retirement_complete,0,0))return 0;
return !InterlockedCompareExchange((LONG*)&gw_wire_session_lease_data.released,0,0) &&
InterlockedCompareExchange((LONG*)&gw_wire_session_lease_data.attached,0,0)==1;
}
static int engine_lease_current_locked(void){
ULONGLONG last,now;
if(!engine_lease_attached_locked())return 0;
last=(ULONGLONG)InterlockedCompareExchange64((LONG64*)&gw_wire_session_lease_data.last_heartbeat_ms,0,0);
now=GetTickCount64();return now>=last && now-last<30000;
}
/* Uses the authenticated registry handle only while borrowed under its lock.
* Neither caller-provided auth flags nor a guessed engine session can attach. */
__declspec(dllexport) LONG WINAPI gw_wire_account_attach_engine_session(void){
struct gw_wire_call check={0};LONG result=-1;
AcquireSRWLockShared(&channel_lock);
if(prepared_channel && gw_wire_auth_data.state==2 && !gw_wire_auth_data.cancel && gw_wire_auth_data.server_proof_verified==1){
check.abi=GW_WINE_WIRE_ABI;check.size=sizeof(check);check.operation=GW_WIRE_AUTHENTICATED;check.transport=prepared_channel;
if(!gw_wire_dispatch(&check) && !check.result && check.value==1){
club_source_status=(LONG)club_source_publish();
if(!club_source_status)club_source_status=1;
InterlockedExchange64((LONG64*)&gw_wire_session_lease_data.last_heartbeat_ms,(LONG64)GetTickCount64());
InterlockedExchange((LONG*)&gw_wire_session_lease_data.released,0);
InterlockedExchange((LONG*)&gw_wire_session_lease_data.attached,1);result=0;
}
}
ReleaseSRWLockShared(&channel_lock);return result;
}
__declspec(dllexport) LONG WINAPI gw_wire_account_engine_heartbeat(void){
LONG result=-1;AcquireSRWLockShared(&channel_lock);
if(engine_lease_attached_locked()){
InterlockedExchange64((LONG64*)&gw_wire_session_lease_data.last_heartbeat_ms,(LONG64)GetTickCount64());
InterlockedIncrement((LONG*)&gw_wire_session_lease_data.heartbeats);result=0;
}
ReleaseSRWLockShared(&channel_lock);return result;
}
__declspec(dllexport) void WINAPI gw_wire_account_detach_engine_session(void){
AcquireSRWLockExclusive(&channel_lock);
if(InterlockedExchange((LONG*)&gw_wire_session_lease_data.attached,0)==1){
InterlockedExchange((LONG*)&gw_wire_session_lease_data.released,1);
InterlockedExchange((LONG*)&gw_wire_auth_data.cancel,1);
}
ReleaseSRWLockExclusive(&channel_lock);
}
static int terminated(const char* value,unsigned int bound){unsigned int i;for(i=0;i<bound;++i)if(!value[i])return i>0;return 0;}
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 5
/* The frame callback already validates its engine owner and thread. For an
* explicit pinned local profile, the numeric address is the endpoint: no DNS
* token is needed. This path still uses the real account worker and SRP. */
#if !defined(GW_SOURCE_LOCAL_AUTH_IPV4) || !defined(GW_SOURCE_LOCAL_AUTH_PORT)
#error Profile5 account worker requires a pinned Source authentication endpoint
#endif
static int source_owned_pinned(void* owner,uint32_t generation,const unsigned char actual[4],unsigned int port){
struct gw_source_profile configured={.abi=1,.size=sizeof(struct gw_source_profile)};
const struct gw_session_retirement_control* retirement=&gw_wire_session_retirement_data;
static const char pinned_text[32]=GW_SOURCE_LOCAL_AUTH_IPV4;
unsigned char pinned[4];
if(!owner || !generation || generation>GW_SESSION_EPOCH_LIMIT ||
retirement->abi!=1 || retirement->size!=sizeof(*retirement) ||
retirement->next_credentials_generation!=generation ||
InterlockedCompareExchange((LONG*)&gw_wire_auth_data.cancel,0,0)!=0)return 0;
if(generation==1){
if(retirement->state!=0 || retirement->expected_credentials_generation || retirement->completed_credentials_generation)return 0;
}else if(retirement->state!=2 || retirement->owner!=(uint64_t)(uintptr_t)owner ||
retirement->expected_credentials_generation!=generation-1 || retirement->completed_credentials_generation!=generation-1)return 0;
return gw_source_profile_read(&configured)==5 && configured.kind==5 &&
configured.auth_port==GW_SOURCE_LOCAL_AUTH_PORT &&
configured.auth_port==port &&
!__builtin_memcmp(configured.hostname,pinned_text,sizeof(pinned_text)) &&
!gw_engine_numeric_ipv4_parse(configured.hostname,sizeof(configured.hostname),pinned) &&
!__builtin_memcmp(actual,pinned,sizeof(pinned));
}
#endif
static void credentials_wipe(LONG state){
struct gw_wire_auth_credentials* data=&gw_wire_auth_credentials_data;
SecureZeroMemory(data->username,sizeof(data->username));
SecureZeroMemory(data->password,sizeof(data->password));
SecureZeroMemory(data->padding,sizeof(data->padding));
data->username_bytes=data->password_bytes=0;
InterlockedExchange((LONG*)&data->state,state);
}
static int credentials_valid(int fresh){
const struct gw_wire_auth_credentials* data=&gw_wire_auth_credentials_data;unsigned int i;
if(data->abi!=1 || data->size!=sizeof(*data) || !data->generation ||
(fresh ? data->generation<=credentials_generation : data->generation!=credentials_generation) || data->reserved ||
!data->username_bytes || data->username_bytes>32 || !data->password_bytes || data->password_bytes>64 || data->username[data->username_bytes])return 0;
for(i=0;i<data->username_bytes;++i)if((unsigned char)data->username[i]<32 || (unsigned char)data->username[i]>126)return 0;
for(i=0;i<data->password_bytes;++i)if(data->password[i]<32 || data->password[i]>126)return 0;
return 1;
}
#if GW_SOURCE_ONLY_ACTIVE
extern LONG WINAPI gw_source_profile_kind(void);
extern int gw_private_source_realm_endpoint_eligible(unsigned int,const unsigned char[4],uint16_t);
static int gw_realm_name_valid(const char*,unsigned int);
static int source_realm_permitted(const struct gw_native_realm* realm){unsigned char address[4];uint16_t port;return !(realm->flags&2)&&gw_realm_name_valid(realm->name,sizeof(realm->name))&&!gw_engine_realm_endpoint_ipv4(realm,address,&port)&&gw_private_source_realm_endpoint_eligible((unsigned int)gw_source_profile_kind(),address,port);}
#endif
static int selectable(const struct gw_native_realm* realm){
const char* prefix="127.0.0.1:";unsigned int i=0;
#if GW_SOURCE_ONLY_ACTIVE
if((realm->flags&2)||!gw_realm_name_valid(realm->name,sizeof(realm->name)))return 0;
if(owned_endpoint.kind==1){unsigned char address[4];uint16_t port;LONG kind=gw_source_profile_kind();
if(gw_engine_realm_endpoint_ipv4(realm,address,&port))return 0;
return kind==2||kind==3||kind==4||kind==5?gw_private_source_realm_endpoint_eligible((unsigned int)kind,address,port):1;}
#else
if(owned_endpoint.kind==1){unsigned char address[4];uint16_t port;return !gw_engine_realm_endpoint_ipv4(realm,address,&port);}
#endif
if(realm->flags&2)return 0;
while(prefix[i]){if(prefix[i]!=realm->address[i])return 0;++i;}return 1;
}
#if GW_SOURCE_ONLY_ACTIVE
static int invoke(struct gw_wire_call* call,unsigned int operation){NTSTATUS status;call->operation=operation;status=gw_wire_dispatch(call);if(status){call->result=(int32_t)status;return 0;}return !call->result;}
#else
static int invoke(struct gw_wire_call* call,unsigned int operation){call->operation=operation;return !gw_wire_dispatch(call)&&!call->result;}
#endif
/* Live worker states: 1 authenticating, 5 waiting for the realm choice, 2 transport ready.
* State 5 must count as live or the realm wait ends at once (error 7 on Octo). */
/* Caller holds channel_lock (shared or exclusive). SRW locks are not
* recursive: code already inside channel_lock must use this, never expired(). */
static int expired_locked(ULONGLONG start){
LONG state=InterlockedCompareExchange((LONG*)&gw_wire_auth_data.state,0,0);
if(InterlockedCompareExchange(&active,0,0)!=1 ||
InterlockedCompareExchange((LONG*)&gw_wire_auth_data.cancel,0,0) ||
InterlockedCompareExchange(&retirement_complete,0,0) || (state!=1 && state!=2 && state!=5))return 1;
return !engine_lease_current_locked() && GetTickCount64()-start>=gw_wire_auth_data.retention_ms;
}
static int expired(ULONGLONG start){
int done;AcquireSRWLockShared(&channel_lock);done=expired_locked(start);
ReleaseSRWLockShared(&channel_lock);return done;
}
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
/* Local retention expiry before transport is distinct from user cancellation. */
static int source_pretransport_expired(ULONGLONG start,int *failure){
if(!expired(start))return 0;
if(!InterlockedCompareExchange((LONG*)&gw_wire_auth_data.cancel,0,0))*failure=7;
return 1;
}
#endif
#if GW_SOURCE_ONLY_ACTIVE
#include "source_auth_detail.inc"
extern int gw_source_preflight_consume_session(uint32_t,const char[16],uint32_t);
static int gw_private_consume_source_realm(struct gw_wire_call*,ULONGLONG);
/* The graphical account page owns preference lookup and manual choice.
* Keep the single-realm shortcut for a LOCAL caller without a picker. */
static void select_source_singleton(unsigned int count,unsigned int available,unsigned int chosen){
LONG kind=gw_source_profile_kind();
if(kind==5){
if(InterlockedCompareExchange(&ui_bridge_ready,0,0) || owned_endpoint.kind!=1 || count!=1)return;
}else if(kind==4)return;
if(available==1)InterlockedCompareExchange((LONG*)&gw_wire_auth_data.selected_index,(LONG)chosen,-1);
}
/* The original displayed indexes stay valid only while the server continues
* to confirm the same realms. Cancellation and local expiry win during I/O. */
static int wait_for_source_realm_choice(struct gw_wire_call* call,unsigned int realm_count,ULONGLONG* start){
ULONGLONG last_reply=GetTickCount64();
while(gw_wire_auth_data.selected_index==-1 && !expired(*start)){
if(realm_count && GetTickCount64()-last_reply>=30000){
if(!invoke(call,GW_WIRE_CONFIRM_REALM_LIST)){
gw_wire_auth_data.discovery_result=call->result;
return 0;
}
if(expired(*start))break;
last_reply=GetTickCount64();
*start=last_reply;
}
gw_private_consume_source_realm(call,*start);
if(gw_wire_auth_data.selected_index==-1)Sleep(25);
}
return 1;
}
#endif
#include "wine_login_result_journal.inc"
static DWORD WINAPI authenticate(void* memory_input){
struct gw_wire_call call={0};const ULONGLONG began=GetTickCount64();ULONGLONG start=began;unsigned int i,available=0,chosen=0,realm_count=0;
struct gw_native_realm selected_realm={0};
char username[33]={0};uint8_t password[64]={0};uint32_t username_bytes=0,password_bytes=0;int failed=0,authenticated;
struct gw_native_auth_memory_ipv4_config explicit_auth={0};
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
struct gw_native_auth_memory_ipv4_hash_config explicit_hash={0};
uint8_t version_hash[20]={0};
uint8_t addon_bytes[10000]={0};uint32_t addon_count=0;
#endif
call.abi=GW_WINE_WIRE_ABI;call.size=sizeof(call);
gw_wire_auth_data.thread=(LONG)GetCurrentThreadId();
call.auth.abi=GW_NATIVE_ABI;call.auth.auth_port=(uint16_t)gw_wire_auth_data.auth_port;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
/* Use the same immutable profile port consumed by preflight, not mutable UI data. */
{struct gw_source_profile profile={1,sizeof(profile),0,0,{0}};
if(gw_source_profile_read(&profile)!=GW_SOURCE_ONLY_PROFILE || !profile.auth_port || profile.auth_port>65535){if(memory_input)credentials_wipe(-1);failed=1;goto close;}
call.auth.auth_port=(uint16_t)profile.auth_port;}
#endif
call.auth.username=gw_wire_auth_data.username;call.auth.password_path=gw_wire_auth_data.password_path;
call.auth.version_hash_path=gw_wire_auth_data.version_hash_path;
if(memory_input){
struct gw_wire_auth_credentials* data=&gw_wire_auth_credentials_data;
password_bytes=data->password_bytes;
if(data->state!=2 || !credentials_valid(0) || !password_bytes || password_bytes>sizeof(password)){
credentials_wipe(-1);failed=1;goto close;
}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(!gw_wire_public_hash_take(&gw_wire_public_hash_data,data->generation,version_hash)){
credentials_wipe(-1);failed=1;goto close;
}
#endif
// Fixed-size copies remain bounded even if a producer violates the
// immutable-publication contract between scheduling and worker entry.
__builtin_memcpy(username,data->username,sizeof(username));
__builtin_memcpy(password,data->password,sizeof(password));
username_bytes=data->username_bytes;
data->consumed_generation=data->generation;credentials_wipe(3);
call.auth.username=username;call.auth.password_path=0;call.input=password;call.input_bytes=password_bytes;
}
if(gw_wire_auth_data.cancel)goto close;
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
if(owned_endpoint.kind==1 && !owned_endpoint.auth_ipv4[0]){
char resolved[16]={0};unsigned char check[4];
int choice=gw_priority_slot_take(credentials_generation,resolved);
/* A still-pending browser flow must not silently send this attempt
* over the public route. Glue returns before C_Login.Login in that
* state; this catches any other native caller. */
if(choice<0 || (choice==0 && gw_private_worker_resolve(2,resolved)) ||
!resolved[0] || resolved[15] ||
gw_engine_numeric_ipv4_parse(resolved,sizeof(resolved),check) ||
!gw_private_public_ipv4_eligible(check)){
gw_wire_auth_data.auth_result=-9;failed=1;goto close;
}
__builtin_memcpy(owned_endpoint.auth_ipv4,resolved,sizeof(resolved));
}
#endif
if(owned_endpoint.kind==1){
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(memory_input){
explicit_hash.abi=GW_NATIVE_IPV4_HASH_ABI;explicit_hash.size=sizeof(explicit_hash);
explicit_hash.auth_port=call.auth.auth_port;explicit_hash.auth_ipv4=owned_endpoint.auth_ipv4;
explicit_hash.username=username;explicit_hash.password=password;
explicit_hash.password_bytes=password_bytes;
explicit_hash.version_hash=version_hash;explicit_hash.version_hash_bytes=sizeof(version_hash);
call.auth=(struct gw_native_auth_config){0};
call.input=(const uint8_t*)&explicit_hash;call.input_bytes=sizeof(explicit_hash);
}else
#endif
{
explicit_auth.abi=GW_NATIVE_IPV4_ABI;explicit_auth.size=sizeof(explicit_auth);explicit_auth.auth_port=call.auth.auth_port;
explicit_auth.auth_ipv4=owned_endpoint.auth_ipv4;explicit_auth.username=username;explicit_auth.password=password;
explicit_auth.password_bytes=password_bytes;explicit_auth.version_hash_path=call.auth.version_hash_path;
call.auth=(struct gw_native_auth_config){0};call.input=(const uint8_t*)&explicit_auth;call.input_bytes=sizeof(explicit_auth);
}
#if GW_SOURCE_ONLY_ACTIVE
if(memory_input && (gw_source_profile_kind()==2 || gw_source_profile_kind()==3 || gw_source_profile_kind()==4 || gw_source_profile_kind()==5)){
NTSTATUS status;uint32_t generation=credentials_generation;
call.reserved=GW_AUTH_DETAIL_REQUEST_V1;
call.operation=(gw_source_profile_kind()==5||gw_source_profile_kind()==2)?GW_WIRE_AUTHENTICATE_MEMORY_IPV4_HASH:GW_WIRE_AUTHENTICATE_MEMORY_IPV4;
if(call.operation==GW_WIRE_AUTHENTICATE_MEMORY_IPV4_HASH){
if(InterlockedCompareExchange(&identity_note_generation,(LONG)generation,0)==0){
struct GwAccountIdentityComparison identity={0,3};
if(generation &&
gw_wire_auth_credentials_data.consumed_generation==generation)
identity=gw_identity_compare_expected((const unsigned char*)username,username_bytes);
gw_wire_account_identity_note(generation,username_bytes,
identity.expected_bytes,identity.code);
}
/* The runner's private comparator must not outlive dispatch. */
SetEnvironmentVariableA(GW_ACCOUNT_IDENTITY_EXPECTED_ENV,NULL);
}
status=gw_wire_dispatch(&call);
if(status)call.result=(int32_t)status;
authenticated=!status && !call.result;
if(!status && call.result==-2 && call.reserved==GW_AUTH_DETAIL_REQUEST_V1 && !gw_wire_auth_data.cancel)
gw_auth_detail_publish(generation,call.value);
call.reserved=0;
}else
#endif
authenticated=invoke(&call,GW_WIRE_AUTHENTICATE_MEMORY_IPV4);
}else authenticated=invoke(&call,memory_input?GW_WIRE_AUTHENTICATE_MEMORY:GW_WIRE_AUTHENTICATE);
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(authenticated && memory_input && !gw_wire_auth_data.cancel)
{
club_source_prepare(username,username_bytes,credentials_generation);
#if defined(GW_NATIVE_LINKED_BACKEND)
unsigned char digest[32] = {0};
if (gw_realm_preference_digest(preference_scope, sizeof(preference_scope) - 1,
username, username_bytes, digest)) {
AcquireSRWLockExclusive(&channel_lock);
if (ui_auth_generation && ui_auth_generation == credentials_generation && !gw_wire_auth_data.cancel) {
memcpy(preference_digest, digest, sizeof(digest));
preference_generation = ui_auth_generation;
}
ReleaseSRWLockExclusive(&channel_lock);
}
SecureZeroMemory(digest, sizeof(digest));
#endif
}
#endif
SecureZeroMemory(password,sizeof(password));SecureZeroMemory(username,sizeof(username));
SecureZeroMemory(&explicit_auth,sizeof(explicit_auth));
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
SecureZeroMemory(&explicit_hash,sizeof(explicit_hash));
SecureZeroMemory(version_hash,sizeof(version_hash));
#endif
call.input=0;call.input_bytes=0;call.auth.username=call.auth.password_path=0;
if(!authenticated){gw_wire_auth_data.auth_result=call.result;failed=1;goto close;}
gw_wire_auth_data.auth_result=0;gw_wire_auth_data.server_proof_verified=1;
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
if(source_pretransport_expired(start,&failed))goto close;
#else
if(expired(start))goto close;
#endif
if(!invoke(&call,GW_WIRE_DISCOVER)){gw_wire_auth_data.discovery_result=call.result;failed=2;goto close;}
gw_wire_auth_data.discovery_result=0;
if(!invoke(&call,GW_WIRE_REALM_COUNT)||call.value>64){failed=3;goto close;}
realm_count=call.value;gw_wire_auth_data.realm_count=(LONG)realm_count;
for(i=0;i<realm_count;++i){
call.index=i;if(!invoke(&call,GW_WIRE_REALM_AT)){failed=3;goto close;}
gw_wire_auth_realms[i]=call.realm;if(selectable(&call.realm)){++available;chosen=i;}
}
#if GW_SOURCE_ONLY_ACTIVE
select_source_singleton(realm_count,available,chosen);
#else
if(gw_wire_auth_data.selected_index==-1 && available==1)gw_wire_auth_data.selected_index=(LONG)chosen;
#endif
if(gw_wire_auth_data.selected_index==-1){
InterlockedExchange((LONG*)&gw_wire_auth_data.state,5);
#if GW_SOURCE_ONLY_ACTIVE
if(!wait_for_source_realm_choice(&call,realm_count,&start)){failed=2;goto close;}
#else
while(gw_wire_auth_data.selected_index==-1&&!expired(start))Sleep(25);
#endif
}
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
if(source_pretransport_expired(start,&failed))goto close;
#else
if(expired(start))goto close;
#endif
chosen=(uint32_t)gw_wire_auth_data.selected_index;
if(chosen>=realm_count){failed=4;goto close;}
// The exported list/index are UI inputs. Re-fetch the actual chosen row
// from the authenticated core owner and bind it to the same transport.
call.index=chosen;if(!invoke(&call,GW_WIRE_REALM_AT)){failed=4;goto close;}selected_realm=call.realm;
#if GW_SOURCE_ONLY_ACTIVE
if((gw_source_profile_kind()==2||gw_source_profile_kind()==3||gw_source_profile_kind()==4||gw_source_profile_kind()==5) &&
(owned_endpoint.kind!=1 || !source_realm_permitted(&selected_realm))){failed=4;goto close;}
#endif
call.index=chosen;call.addon_path=gw_wire_auth_data.addon_path;call.build=5875;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(memory_input){
if(gw_wire_auth_credentials_data.state!=3 ||
gw_wire_auth_credentials_data.consumed_generation!=credentials_generation ||
!gw_wire_public_addon_take(&gw_wire_public_addon_data,credentials_generation,
addon_bytes,&addon_count)){
failed=4;goto close;
}
call.addon_path=0;call.input=addon_count?addon_bytes:0;call.input_bytes=addon_count;
if(!invoke(&call,GW_WIRE_TRANSPORT_OPEN_IPV4_BYTES)){
gw_wire_auth_data.transport_result=call.result;failed=4;goto close;
}
call.input=0;call.input_bytes=0;
}else
#endif
if(!invoke(&call,owned_endpoint.kind==1?GW_WIRE_TRANSPORT_OPEN_IPV4:GW_WIRE_TRANSPORT_OPEN)){
gw_wire_auth_data.transport_result=call.result;failed=4;goto close;
}
if(!invoke(&call,GW_WIRE_PORT)||!call.value||call.value>65535){failed=4;goto close;}
gw_wire_auth_data.transport_result=0;gw_wire_auth_data.transport_prepared=1;
AcquireSRWLockExclusive(&channel_lock);
prepared_realm=selected_realm;prepared_realm_index=chosen;prepared_profile_kind=owned_endpoint.kind;
prepared_port=(uint16_t)call.value;prepared_channel=call.transport;
ReleaseSRWLockExclusive(&channel_lock);
gw_wire_ui_chosen_reset();
InterlockedExchange((LONG*)&gw_wire_auth_data.state,2);
retain:
while(!expired(start))Sleep(25);
close:
SecureZeroMemory(password,sizeof(password));SecureZeroMemory(username,sizeof(username));
SecureZeroMemory(&explicit_auth,sizeof(explicit_auth));
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
SecureZeroMemory(&explicit_hash,sizeof(explicit_hash));
SecureZeroMemory(version_hash,sizeof(version_hash));
SecureZeroMemory(addon_bytes,sizeof(addon_bytes));
#endif
call.input=0;call.input_bytes=0;call.auth.username=call.auth.password_path=0;
/* A future engine pipe borrows the channel synchronously. Cancellation
* closes it only after every such call has returned; no stack-owned token
* or native C++ pointer is lent to an asynchronous engine callback. */
AcquireSRWLockExclusive(&channel_lock);
/* Recheck under the ownership lock: a last-moment genuine world-auth
* attach must not race diagnostic expiry into closing its live channel. */
if(!failed && prepared_channel && gw_wire_auth_data.state==2 && !gw_wire_auth_data.cancel && engine_lease_current_locked()){
ReleaseSRWLockExclusive(&channel_lock);goto retain;
}
InterlockedExchange((LONG*)&gw_wire_session_lease_data.attached,0);
InterlockedExchange((LONG*)&gw_wire_session_lease_data.released,1);prepared_channel=0;
club_source_clear();
prepared_realm=(struct gw_native_realm){0};prepared_realm_index=prepared_profile_kind=0;prepared_port=0;
if(call.transport && !invoke(&call,GW_WIRE_TRANSPORT_CLOSE))failed=5;
if(call.login && !invoke(&call,GW_WIRE_LOGIN_CLOSE))failed=5;
InterlockedExchange(&retirement_complete,(!call.transport && !call.login)?1:0);
ReleaseSRWLockExclusive(&channel_lock);
SecureZeroMemory(gw_wire_auth_data.username,sizeof(gw_wire_auth_data.username));
SecureZeroMemory(gw_wire_auth_data.password_path,sizeof(gw_wire_auth_data.password_path));
SecureZeroMemory(gw_wire_auth_data.version_hash_path,sizeof(gw_wire_auth_data.version_hash_path));
SecureZeroMemory(gw_wire_auth_data.addon_path,sizeof(gw_wire_auth_data.addon_path));
gw_wire_auth_data.error=failed;gw_wire_auth_data.elapsed_ms=(LONG)(GetTickCount64()-began);
{
uint32_t detail=0;
#if GW_SOURCE_ONLY_ACTIVE
detail=gw_source_auth_detail_v1(credentials_generation);
#endif
gw_wire_login_result_journal(credentials_generation,gw_wire_auth_data.error,
gw_wire_auth_data.auth_result,gw_wire_auth_data.discovery_result,
gw_wire_auth_data.transport_result,gw_wire_auth_data.cancel,
gw_wire_auth_data.elapsed_ms,detail);
}
AcquireSRWLockExclusive(&channel_lock);
if(failed)account_ui_publish_terminal_failure();
else account_ui_publish_terminal_closed();
ReleaseSRWLockExclusive(&channel_lock);
InterlockedExchange(&active,0);
InterlockedExchange((LONG*)&gw_wire_auth_data.state,failed?3:4);return 0;
}
/* Call with worker_lock held. A handle is retired once, after its thread
* actually exits; a failed wait or close leaves ownership intact. */
static int account_worker_busy_locked(void){
if(worker_handle){
if(WaitForSingleObject(worker_handle,0)!=WAIT_OBJECT_0)return 1;
if(!CloseHandle(worker_handle))return 1;
worker_handle=0;
}
return 0;
}
LONG WINAPI gw_wire_account_active(void){
LONG busy;AcquireSRWLockExclusive(&worker_lock);
busy=InterlockedCompareExchange(&active,0,0)!=0 || account_worker_busy_locked();
ReleaseSRWLockExclusive(&worker_lock);return busy;
}
/* Own-module consumers use this frozen choice, never the mutable input POD. */
LONG WINAPI gw_wire_account_endpoint_kind(void){return (LONG)owned_endpoint.kind;}
/* Source-backed UI boundary: explicit public fields only, no Lua/model writes. */
static void account_ui_bind_generation(int memory_input){
/* Called only after start's existing real-input/profile guards pass,
* before the new worker can run. Retirement must not clear this latch. */
if(memory_input){
ui_terminal_failure=(struct gw_wire_account_ui_state){0};
ui_terminal_closed=(struct gw_wire_account_ui_state){0};
}
preference_generation = 0;
SecureZeroMemory(preference_digest, sizeof(preference_digest));
ui_auth_generation=memory_input?credentials_generation:0;
InterlockedExchange((LONG*)&gw_wire_account_ui_data.enabled,
ui_auth_generation && InterlockedCompareExchange(&ui_bridge_ready,1,1)==1);
}
#include "wine_account_ui_header.inc"
/* Caller holds channel_lock. The worker's final outcome is authoritative;
* only its presentation header is made terminal before active/state publish. */
static void account_ui_publish_terminal_failure(void){
#if GW_SOURCE_ONLY_ACTIVE
if(!ui_auth_generation || ui_auth_generation!=credentials_generation ||
ui_auth_generation>GW_SESSION_EPOCH_LIMIT || !gw_wire_auth_data.error)return;
account_ui_header(&ui_terminal_failure);
ui_terminal_failure.state=3;
ui_terminal_failure.genuine_discovery=0;
ui_terminal_failure.copied_realms=0;
#endif
}
/* Publish only after the Source worker has closed both owning handles. Like
* failure, closed must survive automatic retirement long enough for a polling
* project UI to acknowledge this exact generation. It carries no realm rows. */
static void account_ui_publish_terminal_closed(void){
#if GW_SOURCE_ONLY_ACTIVE
if(!ui_auth_generation || ui_auth_generation!=credentials_generation ||
ui_auth_generation>GW_SESSION_EPOCH_LIMIT || gw_wire_auth_data.error ||
!InterlockedCompareExchange(&retirement_complete,0,0) || prepared_channel ||
!InterlockedCompareExchange((LONG*)&gw_wire_session_lease_data.released,0,0))return;
account_ui_header(&ui_terminal_closed);
ui_terminal_closed.state=4;
ui_terminal_closed.genuine_discovery=0;
ui_terminal_closed.copied_realms=0;
#endif
}
static void account_ui_visible_header(struct gw_wire_account_ui_state* view){
if(ui_terminal_failure.generation &&
ui_terminal_failure.generation==credentials_generation &&
(!ui_auth_generation || ui_auth_generation==ui_terminal_failure.generation)){
*view=ui_terminal_failure;
return;
}
if(ui_terminal_closed.generation &&
ui_terminal_closed.generation==credentials_generation &&
(!ui_auth_generation || ui_auth_generation==ui_terminal_closed.generation)){
*view=ui_terminal_closed;
return;
}
account_ui_header(view);
}
#if GW_SOURCE_ONLY_ACTIVE
#include "wine_source_realm_consumer.inc"
#endif
#if defined(GW_PRIVATE_TEST_COLD_START_TRACE)
#include <stdio.h>
/* Local test builds only: log each change of the header the login screen polls
* (state, cancel, error, transport, generation; no realm rows or credentials). */
static void account_ui_trace_note(LONG result,const struct gw_wire_account_ui_state* v){
static LONG last=-1;char path[1024],line[200];DWORD length,written=0;HANDLE file;int n;LONG key;
key=result<0?(LONG)(0x40000000|(-result)):(LONG)((v->state&15)|((v->cancel&1)<<4)|((v->error?1:0)<<5)|((v->transport_prepared&1)<<6)|((LONG)(v->generation&0xffff)<<8));
if(InterlockedExchange(&last,key)==key)return;
length=GetEnvironmentVariableA("GW_NATIVE_STARTUP_LOG",path,sizeof(path));
if(!length||length>=sizeof(path))return;
n=snprintf(line,sizeof(line),"GW_UI_SNAPSHOT result=%ld state=%ld cancel=%ld error=%ld transport=%ld gen=%lu sel=%ld ms=%lu\n",
(long)result,(long)v->state,(long)v->cancel,(long)v->error,(long)v->transport_prepared,(unsigned long)v->generation,(long)v->selected_index,(unsigned long)GetTickCount());
file=CreateFileA(path,FILE_APPEND_DATA,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(file==INVALID_HANDLE_VALUE)return;
WriteFile(file,line,(DWORD)n,&written,0);CloseHandle(file);
}
#endif
/* Login-screen ordering gate (reason-14 race, 2026-11-24). The glue Lua must
* observe the chosen-realm header (state 2) before the native realm connection
* opens; when its first state-2 read lands mid world-auth the glue disconnects
* (LOG_DISCONNECT 14, 3 ms after AuthSession). The frame thread therefore holds
* the realm open until a snapshot has returned state 2, or 2 s have passed
* (fail-open: the previous unconditional behaviour). */
static volatile LONG ui_chosen_seen=1;
static volatile LONG64 ui_chosen_since;
void gw_wire_ui_chosen_reset(void){
InterlockedExchange64(&ui_chosen_since,(LONG64)GetTickCount64());
InterlockedExchange(&ui_chosen_seen,0);
}
int gw_wire_ui_chosen_ready(void){
if(InterlockedCompareExchange(&ui_chosen_seen,0,0))return 1;
return GetTickCount64()-(ULONGLONG)InterlockedCompareExchange64(&ui_chosen_since,0,0)>=2000;
}
static LONG account_ui_snapshot(struct gw_wire_account_ui_state* view,
struct gw_native_realm* realms,uint32_t capacity,unsigned char *scope){
struct gw_wire_account_ui_state before,after;struct gw_native_realm rows[64];uint32_t i,count=0;LONG result=0;
if(!view || capacity>64 || (capacity && !realms))return -1;
memset(view,0,sizeof(*view));
AcquireSRWLockShared(&channel_lock);
if(gw_wire_auth_data.abi!=1 || gw_wire_auth_data.size!=sizeof(gw_wire_auth_data) || owned_endpoint.kind>1){result=-2;goto finish;}
account_ui_visible_header(&before);
if(before.realm_count<0 || before.realm_count>64){result=-3;goto finish;}
if(before.genuine_discovery){
if((before.state==2 && (!prepared_channel || !prepared_port || !before.transport_prepared)) ||
(before.state==5 && (before.transport_prepared || InterlockedCompareExchange(&active,0,0)!=1))){result=-4;goto finish;}
count=(uint32_t)before.realm_count;
if(count>capacity || (count && !realms)){result=-5;goto finish;}
for(i=0;i<count;++i){
rows[i]=gw_wire_auth_realms[i];
if(!terminated(rows[i].name,sizeof(rows[i].name)) || !terminated(rows[i].address,sizeof(rows[i].address))){result=-6;goto finish;}
}
}
MemoryBarrier();account_ui_visible_header(&after);
if(memcmp(&before,&after,sizeof(before))){result=-7;goto finish;}
if(count)memcpy(realms,rows,count*sizeof(*realms));
if (scope && before.genuine_discovery && before.server_proof_verified == 1 &&
before.generation && before.generation == preference_generation && before.generation == ui_auth_generation)
memcpy(scope, preference_digest, sizeof(preference_digest));
before.copied_realms=count;*view=before;
if(before.state==2 && before.transport_prepared==1)InterlockedExchange(&ui_chosen_seen,1);
finish:
ReleaseSRWLockShared(&channel_lock);
#if defined(GW_PRIVATE_TEST_COLD_START_TRACE)
account_ui_trace_note(result,view);
#endif
return result;
}
__declspec(dllexport) LONG WINAPI gw_wire_account_ui_snapshot(struct gw_wire_account_ui_state* view,
struct gw_native_realm* realms,uint32_t capacity){
return account_ui_snapshot(view, realms, capacity, NULL);
}
/* Optional v2 entry: the digest and rows are copied under the same lock. */
__declspec(dllexport) LONG WINAPI gw_wire_account_ui_snapshot_scoped(struct gw_wire_account_ui_state* view,
struct gw_native_realm* realms, uint32_t capacity, unsigned char scope[32]){
if (!scope) return -1;
SecureZeroMemory(scope, 32);
return account_ui_snapshot(view, realms, capacity, scope);
}
__declspec(dllexport) LONG WINAPI gw_wire_account_ui_select(uint32_t index,uint32_t generation){
struct gw_wire_account_ui_state view;struct gw_native_realm row;LONG result=-1;LONG64 previous,request;
if(!generation || index>=64)return -1;
AcquireSRWLockShared(&channel_lock);account_ui_header(&view);
if(generation!=view.generation || view.cancel || !view.genuine_discovery || view.state!=5 ||
view.transport_prepared || InterlockedCompareExchange(&active,0,0)!=1 || view.selected_index!=-1 ||
InterlockedCompareExchange((LONG*)&gw_wire_account_ui_data.enabled,1,1)!=1){result=-2;goto finish;}
if(view.realm_count<0 || view.realm_count>64 || index>=(uint32_t)view.realm_count){result=-3;goto finish;}
row=gw_wire_auth_realms[index];
if(!terminated(row.name,sizeof(row.name)) || !terminated(row.address,sizeof(row.address)) || (row.flags&2)){result=-4;goto finish;}
if(owned_endpoint.kind>1){result=-5;goto finish;}
/* This is a user proposal, not transport admission. Reuse the supervisor's
* require_genuine/require_realm checks rather than duplicating its public
* IPv4/profile policy here. The worker's selected_index is untouched. */
request=(LONG64)(((uint64_t)generation<<32)|((uint64_t)index+1));
previous=InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,0,0);
if(previous && ((uint32_t)((uint64_t)previous>>32)<generation ||
InterlockedCompareExchange((LONG*)&gw_wire_account_ui_data.state,3,3)==3)){
InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,0,previous);previous=0;
}
if(previous || InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,request,0)){result=-6;goto finish;}
InterlockedExchange((LONG*)&gw_wire_account_ui_data.error,0);
InterlockedExchange((LONG*)&gw_wire_account_ui_data.state,1);result=0;
finish:
#if defined(GW_PRIVATE_TEST_COLD_START_TRACE)
{char path[1024],line[200];DWORD length=GetEnvironmentVariableA("GW_NATIVE_STARTUP_LOG",path,sizeof(path)),written=0;
if(length&&length<sizeof(path)){HANDLE file=CreateFileA(path,FILE_APPEND_DATA,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
int n=snprintf(line,sizeof(line),"GW_UI_SELECT result=%ld index=%lu gen=%lu/%lu genuine=%ld state=%ld cancel=%ld sel=%ld transport=%ld enabled=%ld endpoint=%ld count=%ld\n",
(long)result,(unsigned long)index,(unsigned long)generation,(unsigned long)view.generation,(long)view.genuine_discovery,(long)view.state,(long)view.cancel,
(long)view.selected_index,(long)view.transport_prepared,(long)gw_wire_account_ui_data.enabled,(long)owned_endpoint.kind,(long)view.realm_count);
if(file!=INVALID_HANDLE_VALUE){WriteFile(file,line,(DWORD)n,&written,0);CloseHandle(file);}}}
#endif
ReleaseSRWLockShared(&channel_lock);return result;
}
/* Caller holds channel_lock exclusively. Generation publication/retirement
* use this same lock: a delayed cancel cannot cross into the next attempt.
* Only publish intent here; the existing worker closes its own handles and
* then reports a genuine terminal state. Never claim that request == close. */
static LONG account_cancel_request_locked(uint32_t expected_generation, int legacy_file){
struct gw_wire_account_ui_state view;
if((!expected_generation && !legacy_file) || expected_generation>GW_SESSION_EPOCH_LIMIT)
return GW_SOURCE_CANCEL_INVALID_GENERATION;
account_ui_header(&view);
if(expected_generation!=view.generation ||
((!legacy_file || expected_generation) && expected_generation!=credentials_generation) ||
InterlockedCompareExchange(&active,0,0)!=1)
return GW_SOURCE_CANCEL_NOT_CURRENT;
if((view.state!=5 && view.state!=1) || view.selected_index!=-1 ||
view.transport_prepared || prepared_channel ||
InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,0,0) ||
InterlockedCompareExchange((LONG*)&gw_wire_session_lease_data.attached,0,0))
return GW_SOURCE_CANCEL_PAST_SELECTION;
InterlockedExchange((LONG*)&gw_wire_auth_data.cancel,1);
return GW_SOURCE_CANCEL_ACCEPTED;
}
static int32_t source_account_cancel_generation(uint32_t expected_generation){
LONG result;
AcquireSRWLockExclusive(&channel_lock);
result=account_cancel_request_locked(expected_generation,0);
ReleaseSRWLockExclusive(&channel_lock);
return result;
}
__declspec(dllexport) int32_t WINAPI gw_wire_account_ui_cancel_generation(uint32_t expected_generation){
return source_account_cancel_generation(expected_generation);
}
/* Compatibility entry retains its realm-choice-only scope. New ordinary
* project hosts must retain/pass the attempt ID to the typed callback. */
__declspec(dllexport) LONG WINAPI gw_wire_account_ui_cancel(void){
struct gw_wire_account_ui_state view;
gw_source_account_cancel_fn cancel=source_account_cancel_generation;
AcquireSRWLockShared(&channel_lock);account_ui_header(&view);
ReleaseSRWLockShared(&channel_lock);
if(view.state!=5)return GW_SOURCE_CANCEL_NOT_CURRENT;
if(!view.generation){
LONG result;
/* Legacy file-input owners have no UI generation. Keep only their
* existing realm-choice scope; never admit generation zero through
* the typed callback used by the owned account page. */
AcquireSRWLockExclusive(&channel_lock);account_ui_header(&view);
result=view.state==5 ? account_cancel_request_locked(0,1) : GW_SOURCE_CANCEL_NOT_CURRENT;
ReleaseSRWLockExclusive(&channel_lock);return result;
}
return cancel(view.generation);
}
/* Explicit owned frame command after facade and published UI readiness.
* Merely inspecting Source public state must never activate native UI mode. */
__declspec(dllexport) void WINAPI gw_wire_account_ui_activate(void){
InterlockedExchange(&ui_bridge_ready,1);
InterlockedExchange((LONG*)&gw_wire_account_ui_data.enabled,ui_auth_generation!=0);
}
/* Copy the immutable selected destination while borrowing its live channel.
* Exported UI rows/index cannot redirect an already prepared transport. */
LONG WINAPI gw_wire_account_selected_realm(struct gw_native_realm* realm,uint32_t* index,uint32_t* kind,uint16_t* port){
LONG result=-1;
if(!realm||!index||!kind||!port)return -1;
*realm=(struct gw_native_realm){0};*index=*kind=0;*port=0;
AcquireSRWLockShared(&channel_lock);
if(prepared_channel && prepared_port && gw_wire_auth_data.state==2 && !gw_wire_auth_data.cancel &&
gw_wire_auth_data.server_proof_verified==1 && gw_wire_auth_data.transport_prepared==1){
*realm=prepared_realm;*index=prepared_realm_index;*kind=prepared_profile_kind;*port=prepared_port;result=0;
}
ReleaseSRWLockShared(&channel_lock);return result;
}
static LONG gw_wire_account_start_internal(void* owner){
HANDLE worker;struct gw_wire_auth_control* data=&gw_wire_auth_data;struct gw_wire_auth_credentials* credentials=&gw_wire_auth_credentials_data;
int memory_input;struct gw_wire_endpoint_profile profile;unsigned int i;unsigned char parsed[4];
(void)owner;
if(gw_wire_account_active() || InterlockedCompareExchange(&active,1,0))return -1;
/* Keep the claim and handle publication indivisible to status/shutdown
* callers, including when the new worker fails before CreateThread returns. */
AcquireSRWLockExclusive(&worker_lock);
if(account_worker_busy_locked()){ReleaseSRWLockExclusive(&worker_lock);InterlockedExchange(&active,0);return -1;}
#if GW_SOURCE_ONLY_ACTIVE
LONG credential_state=InterlockedCompareExchange((LONG*)&credentials->state,2,1);
/* A completed preflight can still own the mailbox after its worker exits.
* Only its matching take/discard may release that reservation. */
if(credential_state==-2){InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -1;}
memory_input=credential_state==1;
#else
memory_input=InterlockedCompareExchange((LONG*)&credentials->state,2,1)==1;
#endif
profile=gw_wire_endpoint_profile_data;
if(data->abi!=1 || data->size!=sizeof(*data) || data->state ||
!data->auth_port || data->auth_port>65535 || data->retention_ms<100 || data->retention_ms>120000 ||
!terminated(data->version_hash_path,sizeof(data->version_hash_path)) || !terminated(data->addon_path,sizeof(data->addon_path)) ||
profile.abi!=1 || profile.size!=sizeof(profile) || profile.kind>1 || profile.reserved ||
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
/* Public Source: an empty kind-1 address asks this worker to resolve
* the immutable profile host at submission (no expiring DNS token). */
(profile.kind==1 && (!memory_input || profile.auth_ipv4[0] || (parsed[0]=0))) ||
#else
(profile.kind==1 && (!memory_input || gw_engine_numeric_ipv4_parse(profile.auth_ipv4,sizeof(profile.auth_ipv4),parsed))) ||
#endif
(memory_input ? (data->password_path[0] || !credentials_valid(1)) :
((credentials->state!=0 && credentials->state!=3 && credentials->state!=-1) || credentials->username_bytes || credentials->password_bytes ||
!terminated(data->username,sizeof(data->username)) || !terminated(data->password_path,sizeof(data->password_path))))){
credentials_wipe(-1);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -1;
}
if(!profile.kind)for(i=0;i<sizeof(profile.auth_ipv4);++i)if(profile.auth_ipv4[i]){credentials_wipe(-1);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -1;}
#if GW_SOURCE_ONLY_ACTIVE
if(gw_source_profile_kind()!=0 && (!memory_input || profile.kind!=1 ||
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
gw_source_profile_kind()!=2 || !data->auth_port || data->auth_port!=3724)){
#elif defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 5
(!source_owned_pinned(owner,credentials->generation,parsed,data->auth_port) &&
gw_source_preflight_consume_session(credentials->generation,profile.auth_ipv4,data->auth_port)))){
#else
gw_source_preflight_consume_session(credentials->generation,profile.auth_ipv4,data->auth_port))){
#endif
credentials_wipe(-1);InterlockedExchange(&active,0);ReleaseSRWLockExclusive(&worker_lock);return -1;
}
#endif
AcquireSRWLockExclusive(&channel_lock);
owned_endpoint=profile;
if(memory_input)credentials_generation=credentials->generation;
#if GW_SOURCE_ONLY_ACTIVE
if(memory_input)gw_auth_detail_begin(credentials_generation);else gw_auth_detail_clear();
#endif
/* Only a real memory-input submission supplies the UI's authentication
* generation. Legacy file-input tests retain their desktop selection path. */
account_ui_bind_generation(memory_input);
data->server_proof_verified=data->transport_prepared=0;
data->auth_result=data->discovery_result=data->transport_result=-1;
InterlockedExchange((LONG*)&data->state,1);
ReleaseSRWLockExclusive(&channel_lock);
InterlockedExchange(&retirement_complete,0);
worker=CreateThread(0,0,authenticate,memory_input?(void*)1:0,0,0);
if(!worker){
credentials_wipe(-1);data->error=6;data->elapsed_ms=0;
AcquireSRWLockExclusive(&channel_lock);
/* No worker exists to run close: release its empty lease before the
* terminal state makes this generation eligible for retirement. */
InterlockedExchange((LONG*)&gw_wire_session_lease_data.attached,0);
InterlockedExchange((LONG*)&gw_wire_session_lease_data.released,1);
InterlockedExchange(&retirement_complete,1);
account_ui_publish_terminal_failure();ReleaseSRWLockExclusive(&channel_lock);
gw_wire_login_result_journal(credentials_generation,data->error,data->auth_result,
data->discovery_result,data->transport_result,data->cancel,data->elapsed_ms,0);
InterlockedExchange(&active,0);InterlockedExchange((LONG*)&data->state,3);ReleaseSRWLockExclusive(&worker_lock);return -2;
}
worker_handle=worker;ReleaseSRWLockExclusive(&worker_lock);return 0;
}
/* Standalone callers retain the existing preflight contract. The engine
* frame supplies the owner for the separately scoped local profile. */
__declspec(dllexport) LONG WINAPI gw_wire_account_start(void){return gw_wire_account_start_internal(0);}
LONG WINAPI gw_wire_account_start_owned(void* owner){return gw_wire_account_start_internal(owner);}
/* Adapter boundary for a Windows-owned packet pipe. It opens no socket, never
* lends keys/tokens, and cannot authenticate, close or replace another session.
* The caller owns its input/output buffers until this synchronous call returns.
*/
__declspec(dllexport) NTSTATUS WINAPI gw_wire_account_transport(struct gw_wire_call* call){
NTSTATUS status;
if(!call || call->abi!=GW_WINE_WIRE_ABI || call->size!=sizeof(*call))return (NTSTATUS)0xc000000d;
call->login=call->transport=0;
call->result=-1;call->required=call->consumed=call->value=0;call->packet=(struct gw_native_packet){0};
switch(call->operation){
case GW_WIRE_PORT:case GW_WIRE_CHALLENGE:case GW_WIRE_AUTH_REQUEST:case GW_WIRE_FEED:
case GW_WIRE_PACKET:case GW_WIRE_SEND:case GW_WIRE_AUTHENTICATED:case GW_WIRE_CHARACTER_COUNT:
case GW_WIRE_ACCOUNT_NAME:case GW_WIRE_CHARACTER_AT:case GW_WIRE_ROSTER_DIAGNOSTICS:case GW_WIRE_ENCODE_ROSTER:case GW_WIRE_AUTH_INITIALIZATION:
case GW_WIRE_CREATE_CHARACTER_CHALLENGES:case GW_WIRE_CREATE_CHARACTER:case GW_WIRE_CREATE_RESULT:case GW_WIRE_PRIVATE_GAMEPLAY_CAPTURE:
case GW_WIRE_GLUE_FEATURES:case GW_WIRE_RESTRICTIONS:case GW_WIRE_CLIENT_INITIALIZATION:case GW_WIRE_DB_REPLY_MISSING:case GW_WIRE_HOTFIX_REPLY:
case GW_WIRE_DIAGNOSTIC_CLIENT_DROP:case GW_WIRE_BEGIN_WORLD:case GW_WIRE_WORLD_PACKET:case GW_WIRE_INITIAL_SPELL_IDS:case GW_WIRE_ACTIVE_MOVER:case GW_WIRE_MOVEMENT:case GW_WIRE_SAY:case GW_WIRE_WORLD_REQUEST:case GW_WIRE_ENTITY_STATE:case GW_WIRE_MOTION_STATE:case GW_WIRE_GAMEPLAY:case GW_WIRE_INVENTORY_STATE:case GW_WIRE_AURA_STATE:break;
default:return (NTSTATUS)0xc000000d;
}
AcquireSRWLockShared(&channel_lock);
if(!prepared_channel || gw_wire_auth_data.state!=2 || gw_wire_auth_data.cancel){ReleaseSRWLockShared(&channel_lock);return (NTSTATUS)0xc0000184;}
call->login=0;call->transport=prepared_channel;
status=gw_wire_dispatch(call);call->transport=0;
ReleaseSRWLockShared(&channel_lock);return status;
}
/* Source-owned retirement: a host request never writes account/proof outcomes. */
uint32_t WINAPI gw_wire_account_generation(void){return credentials_generation;}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
static int public_rows_retirement_ready(uint32_t expected){
const struct GwWirePublicHashV1* hash=&gw_wire_public_hash_data;
const struct GwWirePublicAddonV1* addon=&gw_wire_public_addon_data;
LONG hash_state=InterlockedCompareExchange((LONG*)&hash->state,0,0);
LONG addon_state=InterlockedCompareExchange((LONG*)&addon->state,0,0);
return hash->abi==1 && hash->size==sizeof(*hash) && !hash->reserved &&
addon->abi==1 && addon->size==sizeof(*addon) && addon->byte_count<10000 &&
((hash_state==0 && !hash->generation) ||
((hash_state==1 || hash_state==2) && hash->generation==expected)) &&
((addon_state==0 && !addon->generation && !addon->byte_count) ||
((addon_state==1 || addon_state==2) && addon->generation==expected));
}
static void public_rows_retire_generation(void){
SecureZeroMemory(gw_wire_public_hash_data.bytes,sizeof(gw_wire_public_hash_data.bytes));
gw_wire_public_hash_data.generation=0;gw_wire_public_hash_data.reserved=0;
InterlockedExchange(&gw_wire_public_hash_data.state,0);
SecureZeroMemory(gw_wire_public_addon_data.bytes,sizeof(gw_wire_public_addon_data.bytes));
gw_wire_public_addon_data.generation=0;gw_wire_public_addon_data.byte_count=0;
InterlockedExchange(&gw_wire_public_addon_data.state,0);
}
#endif
int WINAPI gw_wire_account_retirement_ready(uint32_t expected){
if(!expected || expected>=GW_SESSION_EPOCH_LIMIT || expected!=credentials_generation)return -1;
if(gw_wire_account_active())return -2;
if(gw_wire_auth_data.state!=3 && gw_wire_auth_data.state!=4)return -3;
if(!InterlockedCompareExchange(&retirement_complete,0,0))return -7;
if(prepared_channel || prepared_port || prepared_realm_index || prepared_profile_kind)return -4;
if(!gw_wire_session_lease_data.released || gw_wire_auth_credentials_data.state==1 || gw_wire_auth_credentials_data.state==2)return -5;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(!public_rows_retirement_ready(expected))return -8;
#endif
return 0;
}
int WINAPI gw_wire_account_retire_generation(uint32_t expected){
int result=gw_wire_account_retirement_ready(expected);
if(result)return result;
if(InterlockedCompareExchange(&active,-1,0))return -2;
AcquireSRWLockExclusive(&channel_lock);
if(prepared_channel || worker_handle || expected!=credentials_generation){ReleaseSRWLockExclusive(&channel_lock);InterlockedExchange(&active,0);return -6;}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(!public_rows_retirement_ready(expected)){ReleaseSRWLockExclusive(&channel_lock);InterlockedExchange(&active,0);return -8;}
public_rows_retire_generation();
#endif
#if GW_SOURCE_ONLY_ACTIVE
gw_auth_detail_clear();
#endif
club_source_clear();
credentials_wipe(0);
gw_wire_auth_credentials_data=(struct gw_wire_auth_credentials){.abi=1,.size=sizeof(gw_wire_auth_credentials_data)};
gw_wire_auth_data=(struct gw_wire_auth_control){.abi=1,.size=sizeof(gw_wire_auth_data)};
gw_wire_account_ui_data=(struct gw_wire_account_ui_control){.abi=1,.size=sizeof(gw_wire_account_ui_data)};
preference_generation = 0;
SecureZeroMemory(preference_digest, sizeof(preference_digest));
ui_auth_generation=0; /* Preserve the already activated UI bridge. */
SecureZeroMemory(gw_wire_auth_realms,sizeof(gw_wire_auth_realms));
owned_endpoint=(struct gw_wire_endpoint_profile){0};
gw_wire_endpoint_profile_data=(struct gw_wire_endpoint_profile){.abi=1,.size=sizeof(gw_wire_endpoint_profile_data)};
prepared_realm=(struct gw_native_realm){0};prepared_realm_index=prepared_profile_kind=0;prepared_port=0;
gw_wire_session_lease_data=(struct gw_wire_session_lease){.abi=1,.size=sizeof(gw_wire_session_lease_data)};
/* credentials_generation is intentionally monotonic, never reset. */
InterlockedExchange(&identity_note_generation,0);
ReleaseSRWLockExclusive(&channel_lock);InterlockedExchange(&active,0);return 0;
}