forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
1559 lines (1325 loc) · 35.5 KB
/
user.cpp
File metadata and controls
1559 lines (1325 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file user.cpp
* @brief Class for manipulating user / contact data
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/user.h"
#include "mega/megaclient.h"
#include "mega/logging.h"
#include "mega/base64.h"
namespace mega {
User::User(const char* cemail)
{
userhandle = UNDEF;
show = VISIBILITY_UNKNOWN;
ctime = 0;
pubkrequested = false;
isTemporary = false;
resetTag();
if (cemail)
{
email = cemail;
}
memset(&changed, 0, sizeof(changed));
}
bool User::mergeUserAttribute(attr_t type, const string_map &newValuesMap, TLVstore &tlv)
{
bool modified = false;
for (const auto &it : newValuesMap)
{
const char *key = it.first.c_str();
string newValue = it.second;
string currentValue;
if (tlv.find(key)) // the key may not exist in the current user attribute
{
Base64::btoa(tlv.get(key), currentValue);
}
if (newValue != currentValue)
{
if (type == ATTR_ALIAS && newValue[0] == '\0')
{
// alias being removed
tlv.reset(key);
}
else
{
tlv.set(key, Base64::atob(newValue));
}
modified = true;
}
}
return modified;
}
bool User::serialize(string* d)
{
unsigned char l;
unsigned short ll;
time_t ts;
AttrMap attrmap;
char attrVersion = '1';
d->reserve(d->size() + 100 + attrmap.storagesize(10));
d->append((char*)&userhandle, sizeof userhandle);
// FIXME: use m_time_t & Serialize64 instead
ts = ctime;
d->append((char*)&ts, sizeof ts);
d->append((char*)&show, sizeof show);
l = (unsigned char)email.size();
d->append((char*)&l, sizeof l);
d->append(email.c_str(), l);
d->append((char*)&attrVersion, 1);
char bizMode = 0;
if (mBizMode != BIZ_MODE_UNKNOWN) // convert number to ascii
{
bizMode = static_cast<char>('0' + mBizMode);
}
d->append((char*)&bizMode, 1);
d->append("\0\0\0\0\0", 6);
// serialization of attributes
l = (unsigned char)attrs.size();
d->append((char*)&l, sizeof l);
for (userattr_map::iterator it = attrs.begin(); it != attrs.end(); it++)
{
d->append((char*)&it->first, sizeof it->first);
ll = (unsigned short)it->second.size();
d->append((char*)&ll, sizeof ll);
d->append(it->second.data(), ll);
if (attrsv.find(it->first) != attrsv.end())
{
ll = (unsigned short)attrsv[it->first].size();
d->append((char*)&ll, sizeof ll);
d->append(attrsv[it->first].data(), ll);
}
else
{
ll = 0;
d->append((char*)&ll, sizeof ll);
}
}
if (pubk.isvalid())
{
pubk.serializekey(d, AsymmCipher::PUBKEY);
}
return true;
}
User* User::unserialize(MegaClient* client, string* d)
{
handle uh;
time_t ts;
visibility_t v;
unsigned char l;
unsigned short ll;
string m;
User* u;
const char* ptr = d->data();
const char* end = ptr + d->size();
int i;
char attrVersion;
if (ptr + sizeof(handle) + sizeof(time_t) + sizeof(visibility_t) + 2 > end)
{
return NULL;
}
uh = MemAccess::get<handle>(ptr);
ptr += sizeof uh;
// FIXME: use m_time_t & Serialize64
ts = MemAccess::get<time_t>(ptr);
ptr += sizeof ts;
v = MemAccess::get<visibility_t>(ptr);
ptr += sizeof v;
l = *ptr++;
if (l)
{
if (ptr + l > end)
{
return NULL;
}
m.assign(ptr, l);
}
ptr += l;
if (ptr + sizeof(char) + sizeof(char) > end)
{
return NULL;
}
attrVersion = MemAccess::get<char>(ptr);
ptr += sizeof(attrVersion);
char bizModeValue = MemAccess::get<char>(ptr);
ptr += sizeof(bizModeValue);
BizMode bizMode;
switch (bizModeValue)
{
case '0':
bizMode = BIZ_MODE_SUBUSER;
break;
case '1':
bizMode = BIZ_MODE_MASTER;
break;
default:
bizMode = BIZ_MODE_UNKNOWN;
break;
}
for (i = 6; i--;)
{
if (ptr + MemAccess::get<unsigned char>(ptr) < end)
{
ptr += MemAccess::get<unsigned char>(ptr) + 1;
}
}
if ((i >= 0) || !(u = client->finduser(uh, 1)))
{
return NULL;
}
client->mapuser(uh, m.c_str());
u->set(v, ts);
u->resetTag();
u->mBizMode = bizMode;
if (attrVersion == '\0')
{
AttrMap attrmap;
if ((ptr < end) && !(ptr = attrmap.unserialize(ptr, end)))
{
client->discarduser(uh);
return NULL;
}
}
else if (attrVersion == '1')
{
attr_t key;
if (ptr + sizeof(char) > end)
{
client->discarduser(uh);
return NULL;
}
l = *ptr++;
for (int i = 0; i < l; i++)
{
if (ptr + sizeof key + sizeof(ll) > end)
{
client->discarduser(uh);
return NULL;
}
key = MemAccess::get<attr_t>(ptr);
ptr += sizeof key;
ll = MemAccess::get<short>(ptr);
ptr += sizeof ll;
if (ptr + ll + sizeof(ll) > end)
{
client->discarduser(uh);
return NULL;
}
if (!u->isattrvalid(key))
{
u->attrs[key].assign(ptr, ll);
}
ptr += ll;
ll = MemAccess::get<short>(ptr);
ptr += sizeof ll;
if (ll)
{
if (ptr + ll > end)
{
client->discarduser(uh);
return NULL;
}
if (!u->isattrvalid(key))
{
u->attrsv[key].assign(ptr,ll);
}
ptr += ll;
}
}
}
const string *av = (u->isattrvalid(ATTR_KEYRING)) ? u->getattr(ATTR_KEYRING) : NULL;
if (av)
{
TLVstore *tlvRecords = TLVstore::containerToTLVrecords(av, &client->key);
if (tlvRecords)
{
if (tlvRecords->find(EdDSA::TLV_KEY))
{
client->signkey = new EdDSA(client->rng, (unsigned char *) tlvRecords->get(EdDSA::TLV_KEY).data());
if (!client->signkey->initializationOK)
{
delete client->signkey;
client->signkey = NULL;
LOG_warn << "Failed to load chat key from local cache.";
}
else
{
LOG_info << "Signing key loaded from local cache.";
}
}
if (tlvRecords->find(ECDH::TLV_KEY))
{
client->chatkey = new ECDH((unsigned char *) tlvRecords->get(ECDH::TLV_KEY).data());
if (!client->chatkey->initializationOK)
{
delete client->chatkey;
client->chatkey = NULL;
LOG_warn << "Failed to load chat key from local cache.";
}
else
{
LOG_info << "Chat key successfully loaded from local cache.";
}
}
delete tlvRecords;
}
else
{
LOG_warn << "Failed to decrypt keyring from cache";
}
}
if ((ptr < end) && !u->pubk.setkey(AsymmCipher::PUBKEY, (byte*)ptr, int(end - ptr)))
{
client->discarduser(uh);
return NULL;
}
return u;
}
void User::setattr(attr_t at, string *av, string *v)
{
setChanged(at);
if (at != ATTR_AVATAR) // avatar is saved to disc
{
attrs[at] = *av;
}
attrsv[at] = v ? *v : "N";
}
void User::invalidateattr(attr_t at)
{
setChanged(at);
attrsv.erase(at);
}
void User::removeattr(attr_t at, const string *version)
{
if (isattrvalid(at))
{
setChanged(at);
}
attrs.erase(at);
if (version)
{
attrsv[at] = *version;
}
else
{
attrsv.erase(at);
}
}
// updates the user attribute value+version only if different
int User::updateattr(attr_t at, std::string *av, std::string *v)
{
if (attrsv[at] == *v)
{
return 0;
}
setattr(at, av, v);
return 1;
}
// returns the value if there is value (even if it's invalid by now)
const string * User::getattr(attr_t at)
{
userattr_map::const_iterator it = attrs.find(at);
if (it != attrs.end())
{
return &(it->second);
}
return NULL;
}
bool User::isattrvalid(attr_t at)
{
return attrs.count(at) && attrsv.count(at);
}
string User::attr2string(attr_t type)
{
string attrname;
// Special first character (required, except for the oldest attributes):
// `+` is public and unencrypted
// `#` is 'protected' and unencrypted, the API will allow contacts to fetch it but not give it out to non-contacts
// `^` is private but unencrypted, i.e.the API won't give it out to anybody except you, but the API can read the value as well
// `*` is private and encrypted, API only gives it to you and the API doesn't have a way to know the true value
// `%` business usage
// Special second character (optional)
// ! only store a single copy and do not keep a history of changes
// ~ only store one time (ignore subsequent updates, and no history of course)
switch(type)
{
case ATTR_AVATAR:
attrname = "+a";
break;
case ATTR_FIRSTNAME:
attrname = "firstname";
break;
case ATTR_LASTNAME:
attrname = "lastname";
break;
case ATTR_AUTHRING:
attrname = "*!authring";
break;
case ATTR_AUTHRSA:
attrname = "*!authRSA";
break;
case ATTR_AUTHCU255:
attrname = "*!authCu255";
break;
case ATTR_LAST_INT:
attrname = "*!lstint";
break;
case ATTR_ED25519_PUBK:
attrname = "+puEd255";
break;
case ATTR_CU25519_PUBK:
attrname = "+puCu255";
break;
case ATTR_SIG_RSA_PUBK:
attrname = "+sigPubk";
break;
case ATTR_SIG_CU255_PUBK:
attrname = "+sigCu255";
break;
case ATTR_KEYRING:
attrname = "*keyring";
break;
case ATTR_COUNTRY:
attrname = "country";
break;
case ATTR_BIRTHDAY:
attrname = "birthday";
break;
case ATTR_BIRTHMONTH:
attrname = "birthmonth";
break;
case ATTR_BIRTHYEAR:
attrname = "birthyear";
break;
case ATTR_LANGUAGE:
attrname = "^!lang";
break;
case ATTR_PWD_REMINDER:
attrname = "^!prd";
break;
case ATTR_DISABLE_VERSIONS:
attrname = "^!dv";
break;
case ATTR_CONTACT_LINK_VERIFICATION:
attrname = "^clv";
break;
case ATTR_RICH_PREVIEWS:
attrname = "*!rp";
break;
case ATTR_LAST_PSA:
attrname = "^!lastPsa";
break;
case ATTR_RUBBISH_TIME:
attrname = "^!rubbishtime";
break;
case ATTR_STORAGE_STATE:
attrname = "^!usl";
break;
case ATTR_GEOLOCATION:
attrname = "*!geo";
break;
case ATTR_CAMERA_UPLOADS_FOLDER:
attrname = "*!cam";
break;
case ATTR_MY_CHAT_FILES_FOLDER:
attrname = "*!cf";
break;
case ATTR_PUSH_SETTINGS:
attrname = "^!ps";
break;
case ATTR_UNSHAREABLE_KEY:
attrname = "*~usk"; // unshareable key (for encrypting attributes that should not be shared)
break;
case ATTR_ALIAS:
attrname = "*!>alias";
break;
case ATTR_DEVICE_NAMES:
attrname = "*!dn";
break;
case ATTR_UNKNOWN: // empty string
break;
}
return attrname;
}
string User::attr2longname(attr_t type)
{
string longname;
switch(type)
{
case ATTR_AVATAR:
longname = "AVATAR";
break;
case ATTR_FIRSTNAME:
longname = "FIRSTNAME";
break;
case ATTR_LASTNAME:
longname = "LASTNAME";
break;
case ATTR_AUTHRING:
longname = "AUTHRING";
break;
case ATTR_AUTHRSA:
longname = "AUTHRSA";
break;
case ATTR_AUTHCU255:
longname = "AUTHCU255";
break;
case ATTR_LAST_INT:
longname = "LAST_INT";
break;
case ATTR_ED25519_PUBK:
longname = "ED25519_PUBK";
break;
case ATTR_CU25519_PUBK:
longname = "CU25519_PUBK";
break;
case ATTR_SIG_RSA_PUBK:
longname = "SIG_RSA_PUBK";
break;
case ATTR_SIG_CU255_PUBK:
longname = "SIG_CU255_PUBK";
break;
case ATTR_KEYRING:
longname = "KEYRING";
break;
case ATTR_COUNTRY:
longname = "COUNTRY";
break;
case ATTR_BIRTHDAY:
longname = "BIRTHDAY";
break;
case ATTR_BIRTHMONTH:
longname = "BIRTHMONTH";
break;
case ATTR_BIRTHYEAR:
longname = "BIRTHYEAR";
break;
case ATTR_LANGUAGE:
longname = "LANGUAGE";
break;
case ATTR_PWD_REMINDER:
longname = "PWD_REMINDER";
break;
case ATTR_DISABLE_VERSIONS:
longname = "DISABLE_VERSIONS";
break;
case ATTR_CONTACT_LINK_VERIFICATION:
longname = "CONTACT_LINK_VERIFICATION";
break;
case ATTR_RICH_PREVIEWS:
longname = "RICH_PREVIEWS";
break;
case ATTR_LAST_PSA:
longname = "LAST_PSA";
break;
case ATTR_RUBBISH_TIME:
longname = "RUBBISH_TIME";
break;
case ATTR_STORAGE_STATE:
longname = "STORAGE_STATE";
break;
case ATTR_GEOLOCATION:
longname = "GEOLOCATION";
break;
case ATTR_UNSHAREABLE_KEY:
longname = "UNSHAREABLE_KEY";
break;
case ATTR_CAMERA_UPLOADS_FOLDER:
longname = "CAMERA_UPLOADS_FOLDER";
break;
case ATTR_MY_CHAT_FILES_FOLDER:
longname = "MY_CHAT_FILES_FOLDER";
break;
case ATTR_UNKNOWN:
longname = ""; // empty string
break;
case ATTR_PUSH_SETTINGS:
longname = "PUSH_SETTINGS";
break;
case ATTR_ALIAS:
longname = "ALIAS";
break;
case ATTR_DEVICE_NAMES:
longname = "DEVICE_NAMES";
break;
}
return longname;
}
attr_t User::string2attr(const char* name)
{
if (!strcmp(name, "*keyring"))
{
return ATTR_KEYRING;
}
else if (!strcmp(name, "*!authring"))
{
return ATTR_AUTHRING;
}
else if (!strcmp(name, "*!authRSA"))
{
return ATTR_AUTHRSA;
}
else if (!strcmp(name, "*!authCu255"))
{
return ATTR_AUTHCU255;
}
else if (!strcmp(name, "*!lstint"))
{
return ATTR_LAST_INT;
}
else if (!strcmp(name, "+puCu255"))
{
return ATTR_CU25519_PUBK;
}
else if (!strcmp(name, "+puEd255"))
{
return ATTR_ED25519_PUBK;
}
else if (!strcmp(name, "+sigPubk"))
{
return ATTR_SIG_RSA_PUBK;
}
else if (!strcmp(name, "+sigCu255"))
{
return ATTR_SIG_CU255_PUBK;
}
else if (!strcmp(name, "+a"))
{
return ATTR_AVATAR;
}
else if (!strcmp(name, "firstname"))
{
return ATTR_FIRSTNAME;
}
else if (!strcmp(name, "lastname"))
{
return ATTR_LASTNAME;
}
else if (!strcmp(name, "country"))
{
return ATTR_COUNTRY;
}
else if (!strcmp(name, "birthday"))
{
return ATTR_BIRTHDAY;
}
else if(!strcmp(name, "birthmonth"))
{
return ATTR_BIRTHMONTH;
}
else if(!strcmp(name, "birthyear"))
{
return ATTR_BIRTHYEAR;
}
else if(!strcmp(name, "^!lang"))
{
return ATTR_LANGUAGE;
}
else if(!strcmp(name, "^!prd"))
{
return ATTR_PWD_REMINDER;
}
else if(!strcmp(name, "^!dv"))
{
return ATTR_DISABLE_VERSIONS;
}
else if(!strcmp(name, "^clv"))
{
return ATTR_CONTACT_LINK_VERIFICATION;
}
else if(!strcmp(name, "*!rp"))
{
return ATTR_RICH_PREVIEWS;
}
else if(!strcmp(name, "^!lastPsa"))
{
return ATTR_LAST_PSA;
}
else if(!strcmp(name, "^!rubbishtime"))
{
return ATTR_RUBBISH_TIME;
}
else if(!strcmp(name, "^!usl"))
{
return ATTR_STORAGE_STATE;
}
else if(!strcmp(name, "*!geo"))
{
return ATTR_GEOLOCATION;
}
else if (!strcmp(name, "*!cam"))
{
return ATTR_CAMERA_UPLOADS_FOLDER;
}
else if(!strcmp(name, "*!cf"))
{
return ATTR_MY_CHAT_FILES_FOLDER;
}
else if(!strcmp(name, "^!ps"))
{
return ATTR_PUSH_SETTINGS;
}
else if (!strcmp(name, "*~usk"))
{
return ATTR_UNSHAREABLE_KEY;
}
else if (!strcmp(name, "*!>alias"))
{
return ATTR_ALIAS;
}
else if (!strcmp(name, "*!dn"))
{
return ATTR_DEVICE_NAMES;
}
else
{
return ATTR_UNKNOWN; // attribute not recognized
}
}
int User::needversioning(attr_t at)
{
switch(at)
{
case ATTR_AVATAR:
case ATTR_FIRSTNAME:
case ATTR_LASTNAME:
case ATTR_COUNTRY:
case ATTR_BIRTHDAY:
case ATTR_BIRTHMONTH:
case ATTR_BIRTHYEAR:
case ATTR_LANGUAGE:
case ATTR_PWD_REMINDER:
case ATTR_DISABLE_VERSIONS:
case ATTR_RICH_PREVIEWS:
case ATTR_LAST_PSA:
case ATTR_RUBBISH_TIME:
case ATTR_GEOLOCATION:
case ATTR_MY_CHAT_FILES_FOLDER:
case ATTR_PUSH_SETTINGS:
return 0;
case ATTR_LAST_INT:
case ATTR_ED25519_PUBK:
case ATTR_CU25519_PUBK:
case ATTR_SIG_RSA_PUBK:
case ATTR_SIG_CU255_PUBK:
case ATTR_KEYRING:
case ATTR_AUTHRING:
case ATTR_AUTHRSA:
case ATTR_AUTHCU255:
case ATTR_CONTACT_LINK_VERIFICATION:
case ATTR_ALIAS:
case ATTR_CAMERA_UPLOADS_FOLDER:
case ATTR_UNSHAREABLE_KEY:
case ATTR_DEVICE_NAMES:
return 1;
case ATTR_STORAGE_STATE: //putua is forbidden for this attribute
default:
return -1;
}
}
char User::scope(attr_t at)
{
switch(at)
{
case ATTR_KEYRING:
case ATTR_AUTHRING:
case ATTR_AUTHRSA:
case ATTR_AUTHCU255:
case ATTR_LAST_INT:
case ATTR_RICH_PREVIEWS:
case ATTR_GEOLOCATION:
case ATTR_CAMERA_UPLOADS_FOLDER:
case ATTR_MY_CHAT_FILES_FOLDER:
case ATTR_UNSHAREABLE_KEY:
case ATTR_ALIAS:
case ATTR_DEVICE_NAMES:
return '*';
case ATTR_AVATAR:
case ATTR_ED25519_PUBK:
case ATTR_CU25519_PUBK:
case ATTR_SIG_RSA_PUBK:
case ATTR_SIG_CU255_PUBK:
return '+';
case ATTR_LANGUAGE:
case ATTR_PWD_REMINDER:
case ATTR_DISABLE_VERSIONS:
case ATTR_CONTACT_LINK_VERIFICATION:
case ATTR_LAST_PSA:
case ATTR_RUBBISH_TIME:
case ATTR_STORAGE_STATE:
case ATTR_PUSH_SETTINGS:
return '^';
default:
return '0';
}
}
bool User::isAuthring(attr_t at)
{
return (at == ATTR_AUTHRING || at == ATTR_AUTHCU255 || at == ATTR_AUTHRSA);
}
bool User::mergePwdReminderData(int numDetails, const char *data, unsigned int size, string *newValue)
{
if (numDetails == 0)
{
return false;
}
// format: <lastSuccess>:<lastSkipped>:<mkExported>:<dontShowAgain>:<lastLogin>
string oldValue;
if (data && size)
{
oldValue.assign(data, size);
// ensure the old value has a valid format
if (std::count(oldValue.begin(), oldValue.end(), ':') != 4
|| oldValue.length() < 9)
{
oldValue = "0:0:0:0:0";
}
}
else // no existing value, set with default values and update it consequently
{
oldValue = "0:0:0:0:0";
}
bool lastSuccess = (numDetails & PWD_LAST_SUCCESS) != 0;
bool lastSkipped = (numDetails & PWD_LAST_SKIPPED) != 0;
bool mkExported = (numDetails & PWD_MK_EXPORTED) != 0;
bool dontShowAgain = (numDetails & PWD_DONT_SHOW) != 0;
bool lastLogin = (numDetails & PWD_LAST_LOGIN) != 0;
bool changed = false;
// Timestamp for last successful validation of password in PRD
m_time_t tsLastSuccess;
size_t len = oldValue.find(":");
string buf = oldValue.substr(0, len) + "#"; // add character control '#' for conversion
oldValue = oldValue.substr(len + 1); // skip ':'
if (lastSuccess)
{
changed = true;
tsLastSuccess = m_time();
}
else
{
char *pEnd = NULL;
tsLastSuccess = strtoll(buf.data(), &pEnd, 10);
if (*pEnd != '#' || tsLastSuccess == LLONG_MAX || tsLastSuccess == LLONG_MIN)
{
tsLastSuccess = 0;
changed = true;
}
}
// Timestamp for last time the PRD was skipped
m_time_t tsLastSkipped;
len = oldValue.find(":");
buf = oldValue.substr(0, len) + "#";
oldValue = oldValue.substr(len + 1);
if (lastSkipped)
{
tsLastSkipped = m_time();
changed = true;
}
else
{
char *pEnd = NULL;
tsLastSkipped = strtoll(buf.data(), &pEnd, 10);
if (*pEnd != '#' || tsLastSkipped == LLONG_MAX || tsLastSkipped == LLONG_MIN)
{
tsLastSkipped = 0;
changed = true;
}
}
// Flag for Recovery Key exported
bool flagMkExported;
len = oldValue.find(":");
if (len != 1)
{
return false;
}
buf = oldValue.substr(0, len) + "#";
oldValue = oldValue.substr(len + 1);
if (mkExported && !(buf.at(0) == '1'))