Files
WoW-Hybrid-gateway/native/wine_source_realm_consumer.inc

59 lines
3.9 KiB
C++

/* Source-only in-process admission of a genuine UI realm proposal.
* The authenticated account worker owns this call; no synthetic realm/result. */
extern LONG WINAPI gw_source_profile_kind(void);
extern int gw_private_source_realm_endpoint_eligible(unsigned int,const unsigned char address[4],uint16_t);
static int gw_realm_name_valid(const char *s,unsigned int bound){
unsigned int i=0,n=0;
while(i<bound && s[i]){
unsigned int c=(unsigned char)s[i++],need=0,k,cp;
if(c<0x20 || c==0x7f)return 0;
if(c<0x80){n++;continue;}
if(c>=0xc2 && c<=0xdf){need=1;cp=c&0x1f;}
else if(c>=0xe0 && c<=0xef){need=2;cp=c&0x0f;}
else if(c>=0xf0 && c<=0xf4){need=3;cp=c&7;}
else return 0;
for(k=0;k<need;k++){unsigned int next;if(i>=bound||!s[i])return 0;next=(unsigned char)s[i++];if((next&0xc0)!=0x80)return 0;cp=(cp<<6)|(next&0x3f);}
if((need==2 && cp<0x800)||(need==3 && cp<0x10000)||(cp>=0xd800 && cp<=0xdfff)||cp>0x10ffff)return 0;
n+=need+1;
}
return i<bound && n>0 && n<=128;
}
static int gw_realm_same_text(const char *a,const char *b,unsigned int bound){
unsigned int i;for(i=0;i<bound;i++){if(a[i]!=b[i])return 0;if(!a[i])return i>0;}return 0;
}
static int gw_private_consume_source_realm(struct gw_wire_call *auth,ULONGLONG start){
LONG64 request;uint32_t generation,index;struct gw_wire_account_ui_state view;struct gw_native_realm visible;
struct gw_wire_call check={0};unsigned char ip[4];uint16_t port;LONG result=-1;LONG kind=gw_source_profile_kind();
if(kind!=2 && kind!=3 && kind!=4 && kind!=5)return 0;
request=InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,0,0);
if(!request || InterlockedCompareExchange((LONG*)&gw_wire_account_ui_data.state,1,1)!=1)return 0;
generation=(uint32_t)((uint64_t)request>>32);index=(uint32_t)request;
if(!generation||!index||--index>=64)return 0;
AcquireSRWLockExclusive(&channel_lock);
account_ui_header(&view);
if(InterlockedCompareExchange64((LONG64*)&gw_wire_account_ui_data.request,0,0)!=request ||
InterlockedCompareExchange((LONG*)&gw_wire_account_ui_data.state,1,1)!=1){result=0;goto finish;}
if(owned_endpoint.kind!=1 || view.generation!=generation || credentials_generation!=generation ||
!view.genuine_discovery || view.state!=5 || view.selected_index!=-1 || view.transport_prepared ||
InterlockedCompareExchange((LONG*)&gw_wire_account_ui_data.enabled,1,1)!=1 ||
InterlockedCompareExchange(&active,0,0)!=1 || view.realm_count<1 || view.realm_count>64 ||
index>=(uint32_t)view.realm_count || expired_locked(start))goto reject;
visible=gw_wire_auth_realms[index];
check.abi=GW_WINE_WIRE_ABI;check.size=sizeof(check);check.login=auth->login;check.index=index;
if(!invoke(&check,GW_WIRE_REALM_AT) || !gw_realm_name_valid(visible.name,sizeof(visible.name)) ||
!gw_realm_name_valid(check.realm.name,sizeof(check.realm.name)) || visible.wire_id>255 ||
visible.wire_id!=check.realm.wire_id || visible.type!=check.realm.type || visible.flags!=check.realm.flags ||
!gw_realm_same_text(visible.name,check.realm.name,sizeof(visible.name)) ||
!gw_realm_same_text(visible.address,check.realm.address,sizeof(visible.address)) ||
gw_engine_realm_endpoint_ipv4(&check.realm,ip,&port) || !gw_private_source_realm_endpoint_eligible((unsigned int)kind,ip,port) ||
expired_locked(start) || InterlockedCompareExchange((LONG*)&gw_wire_auth_data.selected_index,(LONG)index,-1)!=-1)goto reject;
InterlockedExchange((LONG*)&gw_wire_account_ui_data.error,0);
InterlockedExchange((LONG*)&gw_wire_account_ui_data.state,2);result=1;goto finish;
reject:
/* Proposal rejected; never claim a Source connection or fabricate an error. */
InterlockedExchange((LONG*)&gw_wire_account_ui_data.error,-1);
InterlockedExchange((LONG*)&gw_wire_account_ui_data.state,3);
finish:
ReleaseSRWLockExclusive(&channel_lock);return result;
}