licentivo-sdk 0.11.3 → 0.11.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/licentivo.rb +1 -1
- data/native/device_logout.h +5 -3
- data/native/feature_tasks.h +45 -3
- data/native/licentivo.c +57 -8
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bb2068e66ec94daf6230590f0e85bf78dea783f3c52a49cf3c926dc45962c557
|
|
4
|
+
data.tar.gz: 41c2941a07a16324c88cc6eb1b199e35833eca2bd58ef67f131405df800c8110
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c40659cedf3386d9bb5eecd59eff96bff974d9e342c33744a8b073ff50679eeec857fc2a45fc84ca1d7998af752c42f076f7dbde119e295b4e43038d0b918d78
|
|
7
|
+
data.tar.gz: f3b194d1c08018b6532a86a032bf4202b97cd68d41654f31d4025512feef2715a1832a3e48f7be9057783d0a44e88878ad1ed207abf0be3ab205f5acaa8ccbdb
|
data/README.md
CHANGED
|
@@ -13,10 +13,10 @@ Requirements: Ruby 3.1+, Fiddle, C11 compiler and libcurl development files. Sou
|
|
|
13
13
|
Install the public SDK package in your existing application project:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
gem install licentivo-sdk --version 0.11.
|
|
16
|
+
gem install licentivo-sdk --version 0.11.5
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
For Bundler, use `gem "licentivo-sdk", "= 0.11.
|
|
19
|
+
For Bundler, use `gem "licentivo-sdk", "= 0.11.5"` in your Gemfile.
|
|
20
20
|
|
|
21
21
|
No SDK source download, extraction or local path dependency is required.
|
|
22
22
|
|
data/lib/licentivo.rb
CHANGED
data/native/device_logout.h
CHANGED
|
@@ -45,7 +45,7 @@ static void load_logout_state(ln_client*c){
|
|
|
45
45
|
for(cJSON*s=records->child;s;s=s->next){const char*b=logout_text(s,"binding_key");if(b[0]&&cJSON_IsTrue(get(s,"signed_out"))&&!get(c->logout_bindings,b))cJSON_AddBoolToObject(c->logout_bindings,b,1);}
|
|
46
46
|
for(cJSON*s=records->child;s;s=s->next){const char*b=logout_text(s,"binding_key");if((!strcmp(h,logout_text(s,"source_hash"))||(logout_text(c->cfg,"launch_connection_id")[0]&&binding[0]&&!strcmp(b,binding))||(!c->auth&&!source_input(c)[0]))&&(!b[0]||!binding[0]||!strcmp(b,binding))){cJSON_Delete(c->logout_state);c->logout_state=cJSON_Duplicate(s,1);if(cJSON_IsTrue(get(s,"signed_out"))&&!text(c->cfg,"license_key")[0])set_denied(c);}for(const cJSON*t=get(s,"pending")->child;t;t=t->next){int found=0;for(cJSON*x=tickets->child;x;x=x->next)if(!strcmp(logout_text(t,"operation_id"),logout_text(x,"operation_id")))found=1;if(!found)cJSON_AddItemToArray(tickets,cJSON_Duplicate(t,1));}}
|
|
47
47
|
cJSON_ReplaceItemInObjectCaseSensitive(c->logout_state,"pending",tickets);cJSON_Delete(records);
|
|
48
|
-
if(is_denied(c)){cJSON_Delete(c->auth);c->auth=NULL;}
|
|
48
|
+
if(is_denied(c)||cJSON_IsTrue(get(c->logout_state,"signed_out"))){cJSON_Delete(c->auth);c->auth=NULL;}
|
|
49
49
|
}
|
|
50
50
|
static int acknowledge_logout(ln_client*c,const char*operation){
|
|
51
51
|
cJSON*records=logout_records(c);char*dir=logout_directory(c);int ok=dir!=NULL;
|
|
@@ -55,17 +55,19 @@ static int acknowledge_logout(ln_client*c,const char*operation){
|
|
|
55
55
|
free(dir);cJSON_Delete(records);return ok;
|
|
56
56
|
}
|
|
57
57
|
static int revocation_error(const char*code){const char*codes[]={"LICENSE_REVOKED","LICENSE_SUSPENDED","LICENSE_EXPIRED","DEVICE_RELEASED","DEVICE_NOT_ACTIVE","DEVICE_CREDENTIAL_REVOKED","DEVICE_CREDENTIAL_INVALID","AUTHORIZATION_SESSION_REVOKED","AUTHORIZATION_SESSION_EXPIRED","LAUNCH_LOGIN_REQUIRED","LAUNCH_ENTITLEMENT_REVOKED","LAUNCH_ENTITLEMENT_EXPIRED"};for(size_t i=0;i<sizeof(codes)/sizeof(codes[0]);i++)if(!strcmp(code,codes[i]))return 1;return 0;}
|
|
58
|
-
static int
|
|
58
|
+
static int deny_runtime_mode(ln_client*c,int queue_release){
|
|
59
59
|
set_denied(c);if(!c->logout_state)new_logout_state(c);char h[65];
|
|
60
60
|
if((source_input(c)[0]||c->auth)&&source_hash(c,h))cJSON_ReplaceItemInObjectCaseSensitive(c->logout_state,"source_hash",cJSON_CreateString(h));
|
|
61
61
|
cJSON_ReplaceItemInObjectCaseSensitive(c->logout_state,"signed_out",cJSON_CreateBool(1));
|
|
62
|
+
cJSON_DeleteItemFromObjectCaseSensitive(c->logout_state,"offline_request_id");cJSON_AddStringToObject(c->logout_state,"offline_request_id","");
|
|
62
63
|
const cJSON*ticket=get(c->auth,"logout_ticket");const char*operation=text(ticket,"operation_id");int found=0;
|
|
63
64
|
char binding[80];logout_binding(ticket,binding);if(binding[0]){cJSON_DeleteItemFromObjectCaseSensitive(c->logout_state,"binding_key");cJSON_AddStringToObject(c->logout_state,"binding_key",binding);if(c->logout_bindings&&!get(c->logout_bindings,binding))cJSON_AddBoolToObject(c->logout_bindings,binding,1);}
|
|
64
|
-
if(operation){for(cJSON*p=pending_logouts(c)->child;p;p=p->next)if(text(p,"operation_id")&&!strcmp(operation,text(p,"operation_id")))found=1;if(!found)cJSON_AddItemToArray(pending_logouts(c),cJSON_Duplicate(ticket,1));}
|
|
65
|
+
if(queue_release&&operation){for(cJSON*p=pending_logouts(c)->child;p;p=p->next)if(text(p,"operation_id")&&!strcmp(operation,text(p,"operation_id")))found=1;if(!found)cJSON_AddItemToArray(pending_logouts(c),cJSON_Duplicate(ticket,1));}
|
|
65
66
|
int ok=save_logout_state(c);invalidate(c);cJSON_Delete(c->auth);c->auth=NULL;c->auth_dirty=0;
|
|
66
67
|
/* Keep the immutable configuration graph stable for concurrent local checks. */
|
|
67
68
|
const char*private_names[]={"license_key","identity_token"};for(int i=0;i<2;i++){char*secret=(char*)text(c->cfg,private_names[i]);if(secret){size_t count=strlen(secret);volatile unsigned char*p=(volatile unsigned char*)secret;while(count--)*p++=0;}}return ok;
|
|
68
69
|
}
|
|
70
|
+
static int deny_runtime(ln_client*c){return deny_runtime_mode(c,1);}
|
|
69
71
|
static int logout_binding_blocked(ln_client*c,const cJSON*result){
|
|
70
72
|
const char*connection=text(c->cfg,"launch_connection_id");char binding[80];const cJSON*ticket=get(result,"logout_ticket");logout_binding(ticket,binding);
|
|
71
73
|
if(!connection||!connection[0]||!cJSON_IsTrue(get(c->logout_bindings,binding)))return 0;
|
data/native/feature_tasks.h
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
1
|
/* Included by licentivo.c only. Private task IDs/outcome, never business data. */
|
|
2
|
+
static int task_time_digits(const char*value,int offset,int length,int*out){
|
|
3
|
+
int result=0;for(int i=0;i<length;i++){unsigned char ch=(unsigned char)value[offset+i];if(ch<'0'||ch>'9')return 0;result=result*10+ch-'0';}*out=result;return 1;
|
|
4
|
+
}
|
|
5
|
+
/* Reservation receipts use RFC 3339, including fractional seconds and offsets.
|
|
6
|
+
* Convert the civil date directly so parsing is independent of the host timezone. */
|
|
7
|
+
static int task_deadline(const char*value,double*out){
|
|
8
|
+
if(!value)return 0;size_t length=strlen(value);if(length<20||length>35)return 0;
|
|
9
|
+
if(value[4]!='-'||value[7]!='-'||value[10]!='T'||value[13]!=':'||value[16]!=':')return 0;
|
|
10
|
+
int year,month,day,hour,minute,second;
|
|
11
|
+
if(!task_time_digits(value,0,4,&year)||!task_time_digits(value,5,2,&month)||!task_time_digits(value,8,2,&day)||!task_time_digits(value,11,2,&hour)||!task_time_digits(value,14,2,&minute)||!task_time_digits(value,17,2,&second))return 0;
|
|
12
|
+
if(year<1||month<1||month>12||hour>23||minute>59||second>59)return 0;
|
|
13
|
+
const int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};int leap=year%4==0&&(year%100!=0||year%400==0);
|
|
14
|
+
if(day<1||day>month_days[month-1]+(month==2&&leap))return 0;
|
|
15
|
+
size_t pos=19;double fraction=0;
|
|
16
|
+
if(value[pos]=='.'){
|
|
17
|
+
pos++;int digits=0;double place=.1;
|
|
18
|
+
while(pos<length&&value[pos]>='0'&&value[pos]<='9'){if(++digits>9)return 0;fraction+=(value[pos++]-'0')*place;place*=.1;}
|
|
19
|
+
if(!digits)return 0;
|
|
20
|
+
}
|
|
21
|
+
int offset=0;
|
|
22
|
+
if(pos+1==length&&value[pos]=='Z')pos++;
|
|
23
|
+
else if(pos+6==length&&(value[pos]=='+'||value[pos]=='-')&&value[pos+3]==':'){
|
|
24
|
+
int hours,minutes;if(!task_time_digits(value,(int)pos+1,2,&hours)||!task_time_digits(value,(int)pos+4,2,&minutes)||hours>23||minutes>59)return 0;
|
|
25
|
+
offset=(hours*60+minutes)*60*(value[pos]=='+'?1:-1);pos+=6;
|
|
26
|
+
}else return 0;
|
|
27
|
+
if(pos!=length)return 0;
|
|
28
|
+
int64_t previous=year-1,days=365*previous+previous/4-previous/100+previous/400-719162;
|
|
29
|
+
for(int m=1;m<month;m++)days+=month_days[m-1]+(m==2&&leap);
|
|
30
|
+
*out=(double)((days+day-1)*86400+hour*3600+minute*60+second-offset)+fraction;return 1;
|
|
31
|
+
}
|
|
32
|
+
static double task_wall_time(void){
|
|
33
|
+
#ifdef _WIN32
|
|
34
|
+
FILETIME raw;ULARGE_INTEGER ticks;GetSystemTimeAsFileTime(&raw);ticks.LowPart=raw.dwLowDateTime;ticks.HighPart=raw.dwHighDateTime;
|
|
35
|
+
return (double)(ticks.QuadPart-116444736000000000ULL)/10000000.0;
|
|
36
|
+
#else
|
|
37
|
+
struct timespec now;if(clock_gettime(CLOCK_REALTIME,&now)==0)return (double)now.tv_sec+now.tv_nsec/1e9;
|
|
38
|
+
return (double)time(NULL);
|
|
39
|
+
#endif
|
|
40
|
+
}
|
|
2
41
|
static int task_error(ln_client*c,const char*code){lock(c);snprintf(c->error,sizeof(c->error),"%s",code);c->http_status=0;c->retryable=0;c->request_id[0]=0;view_lock(c);int online=c->view_online;view_unlock(c);publish(c,online);unlock(c);return 0;}
|
|
3
42
|
static int task_enter(ln_client*c){view_lock(c);int busy=c->task_busy;if(!busy)c->task_busy=1;view_unlock(c);return !busy;}
|
|
4
43
|
static void task_leave(ln_client*c){view_lock(c);c->task_busy=0;view_unlock(c);}
|
|
@@ -25,7 +64,7 @@ static int task_finish(ln_client*c,const char*path,cJSON*task,int succeeded){
|
|
|
25
64
|
char*raw=succeeded?ln_commit(c,text(task,"reservation_id"),text(task,"operation_id")):ln_cancel(c,text(task,"reservation_id"),text(task,"operation_id"));
|
|
26
65
|
cJSON*receipt=raw?ln_json(raw):NULL;ln_free_string(raw);if(!receipt)return 0;
|
|
27
66
|
const char*state=text(receipt,"status"),*expected=succeeded?"committed":"canceled";
|
|
28
|
-
int ok=state
|
|
67
|
+
int ok=state&&(!strcmp(state,expected)||(!succeeded&&!strcmp(state,"expired")));cJSON_Delete(receipt);
|
|
29
68
|
if(!ok)return task_error(c,"RESERVATION_CLOSED");task_state(task,expected);
|
|
30
69
|
return write_state(path,task)?1:task_error(c,"TASK_JOURNAL_ERROR");
|
|
31
70
|
}
|
|
@@ -49,10 +88,13 @@ int ln_run_metered_feature(ln_client*c,const char*feature,int64_t quantity,const
|
|
|
49
88
|
const char*held=text(hold,"status");
|
|
50
89
|
if(held&&!strcmp(held,"committed")){cJSON_ReplaceItemInObjectCaseSensitive(task,"reservation_id",cJSON_CreateString(text(hold,"reservation_id")));task_state(task,"committed");cJSON_Delete(hold);ok=write_state(path,task)?1:task_error(c,"TASK_JOURNAL_ERROR");goto done;}
|
|
51
90
|
if(!held||strcmp(held,"pending")||!text(hold,"reservation_id")){cJSON_Delete(hold);task_error(c,"RESERVATION_CLOSED");goto done;}
|
|
91
|
+
double deadline=0;int valid_deadline=task_deadline(text(hold,"expires_at"),&deadline);
|
|
52
92
|
cJSON_ReplaceItemInObjectCaseSensitive(task,"reservation_id",cJSON_CreateString(text(hold,"reservation_id")));cJSON_Delete(hold);task_state(task,"business_running");
|
|
53
93
|
if(!write_state(path,task)){raw=ln_cancel(c,text(task,"reservation_id"),id);ln_free_string(raw);task_error(c,"TASK_JOURNAL_ERROR");goto done;}
|
|
54
|
-
|
|
55
|
-
|
|
94
|
+
int allowed=ln_check_feature(c,feature),unexpired=valid_deadline&&deadline>task_wall_time();
|
|
95
|
+
if(!allowed||!unexpired||!action(context)){
|
|
96
|
+
const char*failure=!allowed?"FEATURE_NOT_ENTITLED":!unexpired?"RESERVATION_EXPIRED":"BUSINESS_ACTION_FAILED";
|
|
97
|
+
task_state(task,"cancel_pending");if(write_state(path,task))task_finish(c,path,task,0);task_error(c,failure);goto done;
|
|
56
98
|
}
|
|
57
99
|
task_state(task,"commit_pending");if(!write_state(path,task)){task_error(c,"BUSINESS_COMPLETED_JOURNAL_ERROR");goto done;}
|
|
58
100
|
ok=task_finish(c,path,task,1);
|
data/native/licentivo.c
CHANGED
|
@@ -48,7 +48,17 @@ static int state_windows_stat(const char*path,struct stat*info){
|
|
|
48
48
|
if(!ok){errno=error==ERROR_FILE_NOT_FOUND||error==ERROR_PATH_NOT_FOUND?ENOENT:EACCES;return -1;}
|
|
49
49
|
memset(info,0,sizeof(*info));info->st_size=data.nFileSizeHigh||data.nFileSizeLow>2147483647?2147483647:(long)data.nFileSizeLow;return 0;
|
|
50
50
|
}
|
|
51
|
-
static int state_windows_move(const char*from,const char*to){
|
|
51
|
+
static int state_windows_move(const char*from,const char*to){
|
|
52
|
+
wchar_t*a=state_windows_path(from),*b=state_windows_path(to);int ok=0;
|
|
53
|
+
if(a&&b)for(unsigned attempt=0;attempt<8;attempt++){
|
|
54
|
+
if(MoveFileExW(a,b,MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH)){ok=1;break;}
|
|
55
|
+
DWORD error=GetLastError();
|
|
56
|
+
/* Readers and file scanners can briefly hold either journal file open. */
|
|
57
|
+
if((error!=ERROR_SHARING_VIOLATION&&error!=ERROR_LOCK_VIOLATION&&error!=ERROR_ACCESS_DENIED)||attempt==7)break;
|
|
58
|
+
Sleep(10*(attempt+1));
|
|
59
|
+
}
|
|
60
|
+
free(a);free(b);return ok;
|
|
61
|
+
}
|
|
52
62
|
#define fopen(path,mode) state_windows_open(path,mode)
|
|
53
63
|
#define remove(path) state_windows_remove(path)
|
|
54
64
|
#define stat(path,info) state_windows_stat(path,info)
|
|
@@ -299,7 +309,7 @@ ln_client*ln_init(const char*config){if(!ensure_curl())return NULL;cJSON*cfg=ln_
|
|
|
299
309
|
#else
|
|
300
310
|
pthread_mutex_init(&c->mutex,NULL);pthread_mutex_init(&c->view_mutex,NULL);atomic_init(&c->stop,0);atomic_init(&c->denied,0);
|
|
301
311
|
#endif
|
|
302
|
-
load_credential(c);load_logout_state(c);if(!source_input(c)[0]&&!c->auth&&!is_denied(c)){ln_destroy(c);return NULL;}const char*path=text(cfg,"cache_path");char*b=!is_denied(c)&&path&&path[0]?readfile(path,LIMIT):NULL;if(b){cJSON*s=ln_json(b);int64_t version,observed;char h[65];int hash_ok=source_hash(c,h);if(hash_ok&&s&&num(s,"version",&version)&&version==1&&text(s,"license_hash")&&!strcmp(text(s,"license_hash"),h)&&num(s,"observed_at",&observed)&&time(NULL)+300>=observed&&ln_verify(get(s,"lease"),keys,text(cfg,"product_id"),text(cfg,"device_id"),time(NULL))){int interval=3600,mode=envelope_policy(get(s,"lease"),&interval);if(mode!=1&&client_context(c,get(s,"lease"))){c->cached=cJSON_Duplicate(get(s,"lease"),1);c->wall=observed;c->interval=interval;c->offline=mode;}}cJSON_Delete(s);free(b);}publish(c,0);if(has_pending_logouts(c))ln_start_heartbeat(c);return c;}
|
|
312
|
+
load_credential(c);load_logout_state(c);if(!source_input(c)[0]&&!c->auth&&!is_denied(c)){ln_destroy(c);return NULL;}const char*path=text(cfg,"cache_path");char*b=!is_denied(c)&&!cJSON_IsTrue(get(c->logout_state,"signed_out"))&&path&&path[0]?readfile(path,LIMIT):NULL;if(b){cJSON*s=ln_json(b);int64_t version,observed;char h[65];int hash_ok=source_hash(c,h);if(hash_ok&&s&&num(s,"version",&version)&&version==1&&text(s,"license_hash")&&!strcmp(text(s,"license_hash"),h)&&num(s,"observed_at",&observed)&&time(NULL)+300>=observed&&ln_verify(get(s,"lease"),keys,text(cfg,"product_id"),text(cfg,"device_id"),time(NULL))){int interval=3600,mode=envelope_policy(get(s,"lease"),&interval);if(mode!=1&&client_context(c,get(s,"lease"))){c->cached=cJSON_Duplicate(get(s,"lease"),1);c->wall=observed;c->interval=interval;c->offline=mode;}}cJSON_Delete(s);free(b);}publish(c,0);if(has_pending_logouts(c))ln_start_heartbeat(c);return c;}
|
|
303
313
|
ln_client*ln_init_file(const char*p){char*b=readfile(p,LIMIT);if(!b)return NULL;ln_client*c=ln_init(b);free(b);return c;}
|
|
304
314
|
ln_client*ln_init_product(const char*product_id,const char*trusted_keys_json,const char*license_key){
|
|
305
315
|
if(!product_id||!trusted_keys_json||!license_key)return NULL;
|
|
@@ -380,9 +390,10 @@ static void sleepms(int ms){
|
|
|
380
390
|
#endif
|
|
381
391
|
}
|
|
382
392
|
static size_t receive_header(char*data,size_t size,size_t n,void*arg){size_t length=size*n;ln_client*c=arg;if(length>12&&!strncmp(data,"Retry-After:",12)){char value[32];size_t count=length-12;if(count>31)count=31;memcpy(value,data+12,count);value[count]=0;long seconds=strtol(value,NULL,10);if(seconds>0)c->retry_after=seconds>86400?86400:(int)seconds;}return length;}
|
|
393
|
+
static int authorization_rejected(const char*code){return revocation_error(code)||!strcmp(code,"PRODUCT_DISABLED")||!strcmp(code,"TENANT_SUSPENDED")||!strcmp(code,"LICENSE_INVALID")||!strcmp(code,"DEVICE_CREDENTIAL_MISMATCH")||!strcmp(code,"LAUNCH_ENVIRONMENT_MISMATCH")||!strcmp(code,"SESSION_EXPIRED")||!strcmp(code,"VERSION_NOT_ENTITLED")||!strcmp(code,"VERSION_NOT_PUBLISHED")||!strcmp(code,"MAINTENANCE_EXPIRED")||!strcmp(code,"MAINTENANCE_STATE_INVALID")||!strcmp(code,"FEATURE_NOT_ENTITLED")||!strcmp(code,"HARDWARE_ID_REQUIRED")||!strcmp(code,"VIRTUAL_MACHINE_NOT_ALLOWED")||!strcmp(code,"SDK_UPGRADE_REQUIRED")||!strcmp(code,"LAUNCH_POLICY_UNSUPPORTED")||!strcmp(code,"RUNTIME_WINDOW_INVALID");}
|
|
383
394
|
static cJSON*request_extra(ln_client*c,const char*op,const char*activation,int*transient,const char*operation_id,const cJSON*extra){*transient=0;c->error[0]=0;c->request_id[0]=0;c->retryable=0;c->http_status=0;c->retry_after=0;if(!prepare_credential(c))return NULL;if(!strcmp(op,"activate")&&temporary(c)&&!text(c->auth,"device_key")[0]&&!c->cached)operation_id=text(c->auth,"request_id");unsigned char random[16];if(!random_bytes(random,16)){strcpy(c->error,"RANDOM_SOURCE_FAILED");return NULL;}char idem[120]="Idempotency-Key: ";size_t used=strlen(idem);for(int i=0;i<16;i++)sprintf(idem+used+i*2,"%02x",random[i]);if(operation_id){if(strlen(operation_id)>80||strpbrk(operation_id," \t\r\n")){strcpy(c->error,"INVALID_OPERATION_ID");return NULL;}snprintf(idem,sizeof(idem),"Idempotency-Key: %s",operation_id);}cJSON*body=cJSON_CreateObject();const char*names[]={"product_id","license_key","device_id","device_name","app_version"};for(int i=0;i<5;i++){const char*s=text(c->cfg,names[i]);cJSON_AddStringToObject(body,names[i],s?s:"");}cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation);cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));if(extra)for(const cJSON*x=extra->child;x;x=x->next)cJSON_AddItemToObject(body,x->string,cJSON_Duplicate(x,1));if(!get(c->auth,"logout_ticket"))cJSON_AddBoolToObject(body,"request_logout_ticket",1);if(text(c->cfg,"launch_connection_id")){cJSON_AddStringToObject(body,"launch_connection_id",text(c->cfg,"launch_connection_id"));cJSON_AddStringToObject(body,"environment",text(c->cfg,"environment")?text(c->cfg,"environment"):"");}if(!runtime_key(c)[0]&&text(c->cfg,"identity_token")){const char*identity_names[]={"identity_token","launch_connection_id","environment"};for(int i=0;i<3;i++){const char*v=text(c->cfg,identity_names[i]);cJSON_DeleteItemFromObjectCaseSensitive(body,identity_names[i]);cJSON_AddStringToObject(body,identity_names[i],v?v:"");}op="launch/exchange";}char*json=cJSON_PrintUnformatted(body);cJSON_Delete(body);const char*base=text(c->cfg,"base_url");char*url=malloc(strlen(base)+strlen(op)+20);if(!url||!json){free(url);free(json);return NULL;}strcpy(url,base);size_t len=strlen(url);if(len&&url[len-1]=='/')url[len-1]=0;strcat(url,"/api/v1/");strcat(url,op);struct curl_slist*headers=NULL;headers=curl_slist_append(headers,"Content-Type: application/json");headers=curl_slist_append(headers,idem);cJSON*result=NULL;
|
|
384
395
|
for(int attempt=0;attempt<2;attempt++){if(attempt)sleepms(200);struct buffer b={{0},0,0};CURL*curl=curl_easy_init();if(!curl)break;curl_easy_setopt(curl,CURLOPT_URL,url);curl_easy_setopt(curl,CURLOPT_POSTFIELDS,json);curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,receive);curl_easy_setopt(curl,CURLOPT_WRITEDATA,&b);int64_t timeout=10;num(c->cfg,"timeout_seconds",&timeout);curl_easy_setopt(curl,CURLOPT_TIMEOUT,(long)timeout);const char*proxy=text(c->cfg,"proxy_url");if(proxy&&proxy[0])curl_easy_setopt(curl,CURLOPT_PROXY,proxy);curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,receive_header);curl_easy_setopt(curl,CURLOPT_HEADERDATA,c);curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5L);curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1L);curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,0L);curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L);curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,2L);const char*ca=getenv("LICENTIVO_CA_BUNDLE");if(ca&&ca[0])curl_easy_setopt(curl,CURLOPT_CAINFO,ca);CURLcode err=curl_easy_perform(curl);long status=0;curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&status);curl_easy_cleanup(curl);if(b.oversized){strcpy(c->error,"RESPONSE_TOO_LARGE");*transient=0;break;}c->http_status=status;if(err!=CURLE_OK){*transient=1;c->retryable=1;strcpy(c->error,"SERVER_UNAVAILABLE");continue;}*transient=0;if(memchr(b.data,0,b.size)){strcpy(c->error,"INVALID_JSON");break;}cJSON*env=ln_json(b.data);if(!env){strcpy(c->error,"UNEXPECTED_RESPONSE");c->retryable=status==429||status>=500;*transient=c->retryable;if(c->retry_after>0)c->retry_at=(int64_t)time(NULL)+c->retry_after;if(status>=500)continue;break;}const char*trace=text(env,"request_id");snprintf(c->request_id,sizeof(c->request_id),"%s",trace?trace:"");if(status!=200||get(env,"error")){const char*code=text(get(env,"error"),"code");snprintf(c->error,sizeof(c->error),"%s",code?code:"UNEXPECTED_RESPONSE");c->retryable=status==429||status>=500;*transient=c->retryable;if(c->retry_after>0)c->retry_at=(int64_t)time(NULL)+c->retry_after;cJSON_Delete(env);if(status>=500)continue;break;}result=cJSON_Duplicate(get(env,"data"),1);cJSON_Delete(env);break;}
|
|
385
|
-
curl_slist_free_all(headers);free(url);free(json);if(!result
|
|
396
|
+
curl_slist_free_all(headers);free(url);free(json);if(!result){if(revocation_error(c->error))deny_runtime(c);else if(authorization_rejected(c->error))invalidate(c);}return result;
|
|
386
397
|
}
|
|
387
398
|
static cJSON*request(ln_client*c,const char*op,const char*activation,int*transient){return request_extra(c,op,activation,transient,NULL,NULL);}
|
|
388
399
|
#include "device_logout.h"
|
|
@@ -406,7 +417,7 @@ static DWORD WINAPI heartbeat_run(LPVOID arg){
|
|
|
406
417
|
#else
|
|
407
418
|
static void*heartbeat_run(void*arg){
|
|
408
419
|
#endif
|
|
409
|
-
ln_client*c=arg;lock(c);int refresh=c->refresh_pending;c->refresh_pending=0;unlock(c);if(refresh&&!stopped(c))ln_refresh_online(c);while(!stopped(c)){unsigned char r=0;random_bytes(&r,1);lock(c);double wait=has_pending_logouts(c)?2:(c->interval<600?600:c->interval)*(1+r/255.0*.1);double retry=(double)c->retry_at-time(NULL);if(retry>wait)wait=retry;int disabled=(c->interval==0||is_denied(c))&&!has_pending_logouts(c);unlock(c);if(disabled)break;double end=monotonic()+wait;while(!stopped(c)&&monotonic()<end)sleepms(100);if(!stopped(c)){if(is_denied(c)||ln_logout_pending(c))ln_retry_pending_logouts(c);if(!is_denied(c))ln_heartbeat(c);}}lock(c);c->finished=1;unlock(c);return 0;}
|
|
420
|
+
ln_client*c=arg;lock(c);int refresh=c->refresh_pending;c->refresh_pending=0;unlock(c);if(refresh&&!stopped(c))ln_refresh_online(c);while(!stopped(c)){unsigned char r=0;random_bytes(&r,1);lock(c);double wait=has_pending_logouts(c)?2:(c->interval<600?600:c->interval)*(1+r/255.0*.1);if(!has_pending_logouts(c)&&c->cached){cJSON*claims=claims_from(c->cached);int64_t expires=0;if(num(claims,"expires_at",&expires)){double remaining=(double)expires-time(NULL);if(remaining>0){double early=remaining*.8;if(early<1)early=1;if(early<wait)wait=early;}}cJSON_Delete(claims);}double retry=(double)c->retry_at-time(NULL);if(retry>wait)wait=retry;int disabled=(c->interval==0||is_denied(c))&&!has_pending_logouts(c);unlock(c);if(disabled)break;double end=monotonic()+wait;while(!stopped(c)&&monotonic()<end)sleepms(100);if(!stopped(c)){if(is_denied(c)||ln_logout_pending(c))ln_retry_pending_logouts(c);if(!is_denied(c))ln_heartbeat(c);}}lock(c);c->finished=1;unlock(c);return 0;}
|
|
410
421
|
int ln_start_heartbeat(ln_client*c){if(!c)return 0;if(c->started){lock(c);int finished=c->finished;unlock(c);if(!finished)return 1;ln_stop_heartbeat(c);}lock(c);int disabled=c->interval==0;c->finished=0;unlock(c);if(disabled&&!c->refresh_pending)return 1;
|
|
411
422
|
#ifdef _WIN32
|
|
412
423
|
InterlockedExchange(&c->stop,0);c->thread=CreateThread(NULL,0,heartbeat_run,c,0,NULL);if(!c->thread)return 0;
|
|
@@ -434,10 +445,48 @@ ln_client*ln_open(const char*path){ln_client*c=ln_init_file(path);if(c&&!ln_star
|
|
|
434
445
|
ln_client*ln_open_with_license(const char*path,const char*license_key){ln_client*c=ln_init_file_with_license(path,license_key);if(c&&!ln_start(c)){ln_destroy(c);return NULL;}return c;}
|
|
435
446
|
void ln_free_string(char*s){free(s);}
|
|
436
447
|
char*ln_consume(ln_client*c,const char*feature,int64_t quantity,const char*operation_id){if(!c||!feature||!operation_id||!operation_id[0])return NULL;lock(c);if(!cache_valid(c)){strcpy(c->error,"LICENSE_CHECK_REQUIRED");unlock(c);return NULL;}char*activation=activation_id(c);cJSON*extra=cJSON_CreateObject();cJSON_AddStringToObject(extra,"feature",feature);cJSON_AddNumberToObject(extra,"quantity",(double)quantity);int transient=0;cJSON*result=request_extra(c,"consume",activation,&transient,operation_id,extra);char*out=result?cJSON_PrintUnformatted(get(result,"consumption")):NULL;cJSON_Delete(extra);free(activation);if(!result&&(!strcmp(c->error,"LICENSE_REVOKED")||!strcmp(c->error,"LICENSE_SUSPENDED")||!strcmp(c->error,"DEVICE_RELEASED")||!strcmp(c->error,"DEVICE_CREDENTIAL_REVOKED")||!strcmp(c->error,"LICENSE_EXPIRED")))invalidate(c);int success=result!=NULL;cJSON_Delete(result);publish(c,success);unlock(c);notify(c);return out;}
|
|
437
|
-
char*ln_offline_request(ln_client*c,const char*operation){
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
448
|
+
char*ln_offline_request(ln_client*c,const char*operation){
|
|
449
|
+
if(!c||is_denied(c)||!operation||(strcmp(operation,"activate")&&strcmp(operation,"deactivate")))return NULL;
|
|
450
|
+
lock(c);if(!prepare_credential(c)){unlock(c);return NULL;}
|
|
451
|
+
int deactivate=!strcmp(operation,"deactivate");char*activation=NULL;
|
|
452
|
+
if(deactivate){if(!cache_valid(c)){unlock(c);return NULL;}activation=activation_id(c);}
|
|
453
|
+
char id[37];if(!request_uuid(id)){free(activation);unlock(c);return NULL;}
|
|
454
|
+
cJSON*body=cJSON_CreateObject();const char*names[]={"product_id","license_key","device_id","device_name","app_version"};
|
|
455
|
+
for(int i=0;i<5;i++){const char*v=text(c->cfg,names[i]);cJSON_AddStringToObject(body,names[i],v?v:"");}
|
|
456
|
+
cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation?activation:"");cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));cJSON_AddBoolToObject(body,"request_logout_ticket",1);cJSON_AddNumberToObject(body,"version",1);cJSON_AddStringToObject(body,"request_id",id);cJSON_AddNumberToObject(body,"created_at",(double)time(NULL));cJSON_AddStringToObject(body,"operation",operation);
|
|
457
|
+
const char*connection=text(c->cfg,"launch_connection_id"),*identity=text(c->cfg,"identity_token"),*environment=text(c->cfg,"environment"),*device_key=text(c->auth,"device_key");
|
|
458
|
+
if(connection&&connection[0]){cJSON_AddStringToObject(body,"launch_connection_id",connection);cJSON_AddStringToObject(body,"environment",environment?environment:"");}
|
|
459
|
+
if((!device_key||!device_key[0])&&identity&&identity[0])cJSON_AddStringToObject(body,"identity_token",identity);
|
|
460
|
+
int ok=1;
|
|
461
|
+
if(!deactivate){cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"offline_request_id");cJSON_AddStringToObject(c->auth,"offline_request_id",id);c->auth_dirty=1;ok=save_credential(c);
|
|
462
|
+
if(ok&&cJSON_IsTrue(get(c->logout_state,"signed_out"))){cJSON_DeleteItemFromObjectCaseSensitive(c->logout_state,"offline_request_id");cJSON_AddStringToObject(c->logout_state,"offline_request_id",id);ok=save_logout_state(c);}
|
|
463
|
+
}
|
|
464
|
+
char*out=ok?cJSON_PrintUnformatted(body):NULL;cJSON_Delete(body);free(activation);
|
|
465
|
+
if(out&&deactivate&&!deny_runtime_mode(c,0)){free(out);out=NULL;strcpy(c->error,"LOCAL_CREDENTIAL_CLEANUP_FAILED");}
|
|
466
|
+
unlock(c);notify(c);return out;
|
|
467
|
+
}
|
|
468
|
+
int ln_import_offline(ln_client*c,const char*request_json,const char*response_json){
|
|
469
|
+
if(!c||is_denied(c)||!request_json||!response_json||strlen(request_json)>LIMIT||strlen(response_json)>LIMIT)return 0;
|
|
470
|
+
lock(c);cJSON*req=ln_json(request_json),*res=ln_json(response_json);int64_t version=0;int ok=req&&res&&num(res,"version",&version)&&version==1;
|
|
471
|
+
const char*fields[]={"request_id","operation"};for(int i=0;i<2;i++){const char*a=text(req,fields[i]),*b=text(res,fields[i]);if(!a||!b||strcmp(a,b))ok=0;}
|
|
472
|
+
if(!text(req,"operation")||strcmp(text(req,"operation"),"activate"))ok=0;
|
|
473
|
+
const char*context[]={"product_id","device_id"};for(int i=0;i<2;i++){const char*a=text(req,context[i]),*b=text(c->cfg,context[i]);if(!a||!b||strcmp(a,b))ok=0;}
|
|
474
|
+
const char*input=text(req,"license_key"),*runtime=runtime_key(c),*source=text(c->auth,"source_hash"),*device_key=text(c->auth,"device_key"),*token=text(req,"identity_token"),*configured=text(c->cfg,"identity_token");char digest[65];
|
|
475
|
+
const char*request_source=token&&token[0]?token:input;
|
|
476
|
+
int same_source=request_source&&request_source[0]&&input&&runtime&&!strcmp(input,runtime)&&(!token||!token[0]||(configured&&!strcmp(token,configured)));
|
|
477
|
+
if(!same_source&&request_source&&request_source[0]&&device_key&&device_key[0]&&source&&hash(request_source,digest)&&!strcmp(digest,source))same_source=1;
|
|
478
|
+
if(!same_source)ok=0;
|
|
479
|
+
const char*current=text(c->auth,"offline_request_id"),*expected=text(req,"request_id");
|
|
480
|
+
if(!current||!current[0]||cJSON_IsTrue(get(c->logout_state,"signed_out")))current=text(c->logout_state,"offline_request_id");
|
|
481
|
+
if((current&¤t[0]&&(!expected||strcmp(current,expected)))||(cJSON_IsTrue(get(c->logout_state,"signed_out"))&&(!current||!current[0])))ok=0;
|
|
482
|
+
const cJSON*result=get(res,"result"),*envelope=get(result,"lease");if(!client_context(c,envelope)||!ln_verify(envelope,get(c->cfg,"trusted_keys"),text(c->cfg,"product_id"),text(c->cfg,"device_id"),time(NULL)))ok=0;
|
|
483
|
+
cJSON*claims=claims_from(envelope);const char*id=text(claims,"offline_request_id"),*activation=text(claims,"activation_id"),*response_id=text(result,"activation_id");if(!id||!expected||strcmp(id,expected)||!activation||!response_id||strcmp(activation,response_id)||envelope_policy(envelope,NULL)==1)ok=0;
|
|
484
|
+
if(ok){
|
|
485
|
+
ok=accept_credential(c,result)&&prepare_credential(c);
|
|
486
|
+
if(ok){cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"offline_request_id");cJSON_AddStringToObject(c->auth,"offline_request_id",expected);c->auth_dirty=1;ok=save_credential(c);}
|
|
487
|
+
if(ok){cJSON_Delete(c->cached);c->cached=cJSON_Duplicate(envelope,1);c->wall=time(NULL);c->tick=monotonic();c->offline=envelope_policy(c->cached,&c->interval);ok=save(c);if(!ok)invalidate(c);}
|
|
488
|
+
}
|
|
489
|
+
if(ok)c->error[0]=0;else strcpy(c->error,"INVALID_OFFLINE_RESPONSE");cJSON_Delete(claims);cJSON_Delete(req);cJSON_Delete(res);publish(c,0);unlock(c);notify(c);return ok;
|
|
441
490
|
}
|
|
442
491
|
|
|
443
492
|
static char*reservation_call(ln_client*c,const char*op,const char*operation_id,const cJSON*extra){if(!c||!operation_id||!operation_id[0])return NULL;lock(c);if(!cache_valid(c)){strcpy(c->error,"LICENSE_CHECK_REQUIRED");publish(c,0);unlock(c);return NULL;}char*activation=activation_id(c);int transient=0;cJSON*result=request_extra(c,op,activation,&transient,operation_id,extra);char*out=result?cJSON_PrintUnformatted(get(result,"reservation")):NULL;if(!result&&(!strcmp(c->error,"LICENSE_REVOKED")||!strcmp(c->error,"LICENSE_SUSPENDED")||!strcmp(c->error,"DEVICE_RELEASED")||!strcmp(c->error,"DEVICE_CREDENTIAL_REVOKED")||!strcmp(c->error,"LICENSE_EXPIRED")))invalidate(c);publish(c,result!=NULL);free(activation);cJSON_Delete(result);unlock(c);notify(c);return out;}
|