1 /**
2  * @file mega/user.h
3  * @brief Class for manipulating user / contact data
4  *
5  * (c) 2013-2014 by Mega Limited, Auckland, New Zealand
6  *
7  * This file is part of the MEGA SDK - Client Access Engine.
8  *
9  * Applications using the MEGA API must present a valid application key
10  * and comply with the the rules set forth in the Terms of Service.
11  *
12  * The MEGA SDK is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * @copyright Simplified (2-clause) BSD License.
17  *
18  * You should have received a copy of the license along with this
19  * program.
20  */
21 
22 #ifndef MEGA_USER_H
23 #define MEGA_USER_H 1
24 
25 #include "attrmap.h"
26 
27 namespace mega {
28 // user/contact
29 struct MEGA_API User : public Cacheable
30 {
31     // user handle
32     handle userhandle;
33 
34     // string identifier for API requests (either e-mail address or ASCII user
35     // handle)
36     string uid;
37 
38     // e-mail address
39     string email;
40 
41     // visibility status
42     visibility_t show;
43 
44     // shares by this user
45     handle_set sharing;
46 
47     // contact establishment timestamp
48     m_time_t ctime;
49 
50     BizMode mBizMode = BIZ_MODE_UNKNOWN;
51 
52     struct
53     {
54         bool keyring : 1;   // private keys
55         bool authring : 1;  // authentication information of the contact (signing key)
56         bool authrsa : 1;   // authentication information of the contact (RSA key)
57         bool authcu255 : 1; // authentication information of the contact (Cu25519 key)
58         bool lstint : 1;    // last interaction with the contact
59         bool puEd255 : 1;   // public key for Ed25519
60         bool puCu255 : 1;   // public key for Cu25519
61         bool sigPubk : 1;   // signature for RSA public key
62         bool sigCu255 : 1;  // signature for Cu255199 public key
63         bool avatar : 1;    // avatar image
64         bool firstname : 1;
65         bool lastname : 1;
66         bool country : 1;
67         bool birthday : 1;      // wraps status of birthday, birthmonth, birthyear
68         bool email : 1;
69         bool language : 1;      // preferred language code
70         bool pwdReminder : 1;   // password-reminder-dialog information
71         bool disableVersions : 1;   // disable fileversioning
72         bool contactLinkVerification : 1; // Verify contact requests with contact links
73         bool richPreviews : 1;  // enable messages with rich previews
74         bool lastPsa : 1;
75         bool rubbishTime : 1;   // days to keep nodes in rubbish bin before auto clean
76         bool storageState : 1;  // state of the storage (0 = green, 1 = orange, 2 = red)
77         bool geolocation : 1;   // enable send geolocations
78         bool cameraUploadsFolder : 1;   // target folder for Camera Uploads
79         bool myChatFilesFolder : 1;   // target folder for my chat files
80         bool pushSettings : 1;  // push notification settings
81         bool alias : 1; // user's aliases
82         bool unshareablekey : 1;    // key to encrypt unshareable node attributes
83         bool devicenames : 1; // device names
84     } changed;
85 
86     // user's public key
87     AsymmCipher pubk;
88     struct
89     {
90         bool pubkrequested : 1;
91         bool isTemporary : 1;
92     };
93 
94     // actions to take after arrival of the public key
95     deque<std::unique_ptr<PubKeyAction>> pkrs;
96 
97 private:
98     // persistent attributes (keyring, firstname...)
99     userattr_map attrs;
100 
101     // version of each attribute
102     userattr_map attrsv;
103 
104     // source tag
105     int tag;
106 
107 public:
108     void set(visibility_t, m_time_t);
109 
110     bool serialize(string*) override;
111     static User* unserialize(class MegaClient *, string*);
112 
113     // attribute methods: set/get/invalidate...
114     void setattr(attr_t at, string *av, string *v);
115     const string *getattr(attr_t at);
116     const string *getattrversion(attr_t at);
117     void invalidateattr(attr_t at);
118     bool isattrvalid(attr_t at);
119     void removeattr(attr_t at, const string *version = nullptr);
120     int updateattr(attr_t at, string *av, string *v);
121 
122     static string attr2string(attr_t at);
123     static string attr2longname(attr_t at);
124     static attr_t string2attr(const char *name);
125     static int needversioning(attr_t at);
126     static char scope(attr_t at);
127     static bool isAuthring(attr_t at);
128 
129     enum {
130         PWD_LAST_SUCCESS = 0x01,
131         PWD_LAST_SKIPPED = 0x02,
132         PWD_MK_EXPORTED = 0x04,
133         PWD_DONT_SHOW = 0x08,
134         PWD_LAST_LOGIN = 0x10
135     };
136 
137     static const int PWD_SHOW_AFTER_ACCOUNT_AGE = 7 * 24 * 60 * 60;
138     static const int PWD_SHOW_AFTER_LASTSUCCESS = 3 * 30 * 24 * 60 * 60;
139     static const int PWD_SHOW_AFTER_LASTLOGIN = 14 * 24 * 60 * 60;
140     static const int PWD_SHOW_AFTER_LASTSKIP = 3 * 30 * 24 * 60 * 60;
141     static const int PWD_SHOW_AFTER_LASTSKIP_LOGOUT = 1 * 30 * 24 * 60 * 60;
142 
143     static bool mergePwdReminderData(int numDetails, const char *data, unsigned int size, string *newValue);
144     static m_time_t getPwdReminderData(int numDetail, const char *data, unsigned int size);
145 
146     bool setChanged(attr_t at);
147 
148     void setTag(int tag);
149     int getTag();
150     void resetTag();
151 
152     User(const char* = NULL);
153 
154     // merges the new values in the given TLV. Returns true if TLV is changed.
155     static bool mergeUserAttribute(attr_t type, const string_map &newValuesMap, TLVstore &tlv);
156 };
157 
158 class AuthRing
159 {
160 public:
161     AuthRing(attr_t type, const TLVstore &authring);
162 
163     // return true if authring has changed (data can be pubKey or keySignature depending on authMethod)
164     void add(handle uh, const std::string &fingerprint, AuthMethod authMethod);
165 
166     // assumes the key is already tracked for uh (otherwise, it will throw)
167     void update(handle uh, AuthMethod authMethod);
168 
169     // return false if uh was not tracked
170     bool remove(handle uh);
171 
172     // return the authring as tlv container, ready to set as user's attribute
173     std::string *serialize(PrnGen &rng, SymmCipher &key) const;
174 
175     // false if uh is not tracked in the authring
176     bool isTracked(handle uh) const;
177 
178     // true for Cu25519 and RSA, false for Ed25519
179     bool isSignedKey() const;
180 
181     // true if key is tracked and authentication method is fingerprint/signature-verified
182     bool areCredentialsVerified(handle uh) const;
183 
184     // returns AUTH_METHOD_UNKNOWN if no authentication is found for the given user
185     AuthMethod getAuthMethod(handle uh) const;
186 
187     // returns the fingerprint of the public key for a given user, or empty string if user is not found
188     string getFingerprint(handle uh) const;
189 
190     // returns the list of tracked users
191     vector<handle> getTrackedUsers() const;
192 
193     // returns most significant 160 bits from SHA256, whether in binary or hexadecimal
194     static string fingerprint(const string &pubKey, bool hexadecimal = false);
195 
196     // returns the authring type for a given attribute type associated to a public key
197     static attr_t keyTypeToAuthringType(attr_t at);
198 
199     // returns the authring type for a given attribute type associated to a signature
200     static attr_t signatureTypeToAuthringType(attr_t at);
201 
202     // returns the attribute type associated to the corresponding signature for a given authring type
203     static attr_t authringTypeToSignatureType(attr_t at);
204 
205     // returns a human-friendly string for a given authentication method
206     static string authMethodToStr(AuthMethod authMethod);
207 
208 private:
209     attr_t mType;
210     map<handle, string> mFingerprint;
211     map<handle, AuthMethod> mAuthMethod;
212 };
213 
214 } // namespace
215 
216 #endif
217