Add strict-oidentd mode
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef STORAGE_H
22 #define STORAGE_H
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "coreidentity.h"
28 #include "message.h"
29 #include "network.h"
30
31 class Storage : public QObject
32 {
33     Q_OBJECT
34
35 public:
36     Storage(QObject *parent = 0);
37     virtual ~Storage() {};
38
39     enum State {
40         IsReady,    // ready to go
41         NeedsSetup, // need basic setup (ask the user for input)
42         NotAvailable // remove the storage backend from the list of avaliable backends
43     };
44
45     enum HashVersion {
46         Sha1,
47         Sha2_512,
48         Latest=Sha2_512
49
50     };
51
52 public slots:
53     /* General */
54
55     //! Check if the storage type is available.
56     /** A storage subclass should return true if it can be successfully used, i.e. if all
57      *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
58      * \return True if and only if the storage class can be successfully used.
59      */
60     virtual bool isAvailable() const = 0;
61
62     //! Returns the identifier of the authenticator backend
63     /** \return A string that can be used by the client to identify the authenticator backend */
64     virtual QString backendId() const = 0;
65
66     //! Returns the display name of the storage backend
67     /** \return A string that can be used by the client to name the storage backend */
68     virtual QString displayName() const = 0;
69
70     //! Returns a description of this storage backend
71     /** \return A string that can be displayed by the client to describe the storage backend */
72     virtual QString description() const = 0;
73
74     //! Returns data required to configure the authenticator backend
75     /**
76      * A list of flattened triples for each field: {key, translated field name, default value}
77      * The default value's type determines the kind of input widget to be shown
78      * (int -> QSpinBox; QString -> QLineEdit)
79      * \return A list of triples defining the data to be shown in the configuration dialog
80      */
81     virtual QVariantList setupData() const = 0;
82
83     //! Setup the storage provider.
84     /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
85      *  \param settings   Hostname, port, username, password, ...
86      *  \return True if and only if the storage provider was initialized successfully.
87      */
88     virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
89
90     //! Initialize the storage provider
91     /** \param settings   Hostname, port, username, password, ...
92      *  \return the State the storage backend is now in (see Storage::State)
93      */
94     virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
95
96     //! Makes temp data persistent
97     /** This Method is periodically called by the Quassel Core to make temporary
98      *  data persistant. This reduces the data loss drastically in the
99      *  unlikely case of a Core crash.
100      */
101     virtual void sync() = 0;
102
103     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
104
105     /* User handling */
106
107     //! Add a new core user to the storage.
108     /** \param user     The username of the new user
109      *  \param password The cleartext password for the new user
110      *  \return The new user's UserId
111      */
112     virtual UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") = 0;
113
114     //! Update a core user's password.
115     /** \param user     The user's id
116      *  \param password The user's new password
117      *  \return true on success.
118      */
119     virtual bool updateUser(UserId user, const QString &password) = 0;
120
121     //! Rename a user
122     /** \param user     The user's id
123      *  \param newName  The user's new name
124      */
125     virtual void renameUser(UserId user, const QString &newName) = 0;
126
127     //! Validate a username with a given password.
128     /** \param user     The username to validate
129      *  \param password The user's alleged password
130      *  \return A valid UserId if the password matches the username; 0 else
131      */
132     virtual UserId validateUser(const QString &user, const QString &password) = 0;
133
134     //! Check if a user with given username exists. Do not use for login purposes!
135     /** \param username  The username to validate
136      *  \return A valid UserId if the user exists; 0 else
137      */
138     virtual UserId getUserId(const QString &username) = 0;
139
140     //! Get the authentication provider for a given user.
141     /** \param username  The username to validate
142      *  \return The name of the auth provider if the UserId exists, "" otherwise.
143      */
144     virtual QString getUserAuthenticator(const UserId userid) = 0;
145
146
147     //! Determine the UserId of the internal user
148     /** \return A valid UserId if the password matches the username; 0 else
149      */
150     virtual UserId internalUser() = 0;
151
152     //! Remove a core user from storage.
153     /** \param user     The userid to delete
154      */
155     virtual void delUser(UserId user) = 0;
156
157     //! Store a user setting persistently
158     /**
159      * \param userId       The users Id
160      * \param settingName  The Name of the Setting
161      * \param data         The Value
162      */
163     virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
164
165     //! Retrieve a persistent user setting
166     /**
167      * \param userId       The users Id
168      * \param settingName  The Name of the Setting
169      * \param default      Value to return in case it's unset.
170      * \return the Value of the Setting or the default value if it is unset.
171      */
172     virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
173
174     /* Identity handling */
175     virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
176     virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
177     virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
178     virtual QList<CoreIdentity> identities(UserId user) = 0;
179
180     /* Network handling */
181
182     //! Create a new Network in the storage backend and return it unique Id
183     /** \param user        The core user who owns this network
184      *  \param networkInfo The networkInfo holding the network definition
185      *  \return the NetworkId of the newly created Network. Possibly invalid.
186      */
187     virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
188
189     //! Apply the changes to NetworkInfo info to the storage engine
190     /**
191      *  \param user        The core user
192      *  \param networkInfo The Updated NetworkInfo
193      *  \return true if successfull.
194      */
195     virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
196
197     //! Permanently remove a Network and all the data associated with it.
198     /** \note This method is thredsafe.
199      *
200      *  \param user        The core user
201      *  \param networkId   The network to delete
202      *  \return true if successfull.
203      */
204     virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
205
206     //! Returns a list of all NetworkInfos for the given UserId user
207     /** \note This method is thredsafe.
208      *
209      *  \param user        The core user
210      *  \return QList<NetworkInfo>.
211      */
212     virtual QList<NetworkInfo> networks(UserId user) = 0;
213
214     //! Get a list of Networks to restore
215     /** Return a list of networks the user was connected at the time of core shutdown
216      *  \note This method is threadsafe.
217      *
218      *  \param user  The User Id in question
219      */
220     virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
221
222     //! Update the connected state of a network
223     /** \note This method is threadsafe
224      *
225      *  \param user        The Id of the networks owner
226      *  \param networkId   The Id of the network
227      *  \param isConnected whether the network is connected or not
228      */
229     virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
230
231     //! Get a hash of channels with their channel keys for a given network
232     /** The keys are channel names and values are passwords (possibly empty)
233      *  \note This method is threadsafe
234      *
235      *  \param user       The id of the networks owner
236      *  \param networkId  The Id of the network
237      */
238     virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
239
240     //! Update the connected state of a channel
241     /** \note This method is threadsafe
242      *
243      *  \param user       The Id of the networks owner
244      *  \param networkId  The Id of the network
245      *  \param channel    The name of the channel
246      *  \param isJoined   whether the channel is connected or not
247      */
248     virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
249
250     //! Update the key of a channel
251     /** \note This method is threadsafe
252      *
253      *  \param user       The Id of the networks owner
254      *  \param networkId  The Id of the network
255      *  \param channel    The name of the channel
256      *  \param key        The key of the channel (possibly empty)
257      */
258     virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
259
260     //! retrieve last known away message for session restore
261     /** \note This method is threadsafe
262      *
263      *  \param user       The Id of the networks owner
264      *  \param networkId  The Id of the network
265      */
266     virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
267
268     //! Make away message persistent for session restore
269     /** \note This method is threadsafe
270      *
271      *  \param user       The Id of the networks owner
272      *  \param networkId  The Id of the network
273      *  \param awayMsg    The current away message of own user
274      */
275     virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
276
277     //! retrieve last known user mode for session restore
278     /** \note This method is threadsafe
279      *
280      *  \param user       The Id of the networks owner
281      *  \param networkId  The Id of the network
282      */
283     virtual QString userModes(UserId user, NetworkId networkId) = 0;
284
285     //! Make our user modes persistent for session restore
286     /** \note This method is threadsafe
287      *
288      *  \param user       The Id of the networks owner
289      *  \param networkId  The Id of the network
290      *  \param userModes  The current user modes of own user
291      */
292     virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
293
294     /* Buffer handling */
295
296     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
297     /** \param user      The core user who owns this buffername
298      *  \param networkId The network id
299      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
300      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
301      *  \param create    Whether or not the buffer should be created if it doesnt exist
302      *  \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
303      */
304     virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
305
306     //! Get the unique BufferInfo for a bufferId
307     /** \param user      The core user who owns this buffername
308      *  \param bufferId  The id of the buffer
309      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
310      */
311     virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
312
313     //! Request a list of all buffers known to a user.
314     /** This method is used to get a list of all buffers we have stored a backlog from.
315      *  \param user  The user whose buffers we request
316      *  \return A list of the BufferInfos for all buffers as requested
317      */
318     virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
319
320     //! Request a list of BufferIds for a given NetworkId
321     /** \note This method is threadsafe.
322      *
323      *  \param user  The user whose buffers we request
324      *  \param networkId  The NetworkId of the network in question
325      *  \return List of BufferIds belonging to the Network
326      */
327     virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
328
329     //! Remove permanently a buffer and it's content from the storage backend
330     /** This call cannot be reverted!
331      *  \param user      The user who is the owner of the buffer
332      *  \param bufferId  The bufferId
333      *  \return true if successfull
334      */
335     virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
336
337     //! Rename a Buffer
338     /** \note This method is threadsafe.
339      *  \param user      The id of the buffer owner
340      *  \param bufferId  The bufferId
341      *  \param newName   The new name of the buffer
342      *  \return true if successfull
343      */
344     virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
345
346     //! Merge the content of two Buffers permanently. This cannot be reversed!
347     /** \note This method is threadsafe.
348      *  \param user      The id of the buffer owner
349      *  \param bufferId1 The bufferId of the remaining buffer
350      *  \param bufferId2 The buffer that is about to be removed
351      *  \return true if successfull
352      */
353     virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
354
355     //! Update the LastSeenDate for a Buffer
356     /** This Method is used to make the LastSeenDate of a Buffer persistent
357      * \param user      The Owner of that Buffer
358      * \param bufferId  The buffer id
359      * \param MsgId     The Message id of the message that has been just seen
360      */
361     virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
362
363     //! Get a Hash of all last seen message ids
364     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
365      * \param user      The Owner of the buffers
366      */
367     virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
368
369     //! Update the MarkerLineMsgId for a Buffer
370     /** This Method is used to make the marker line position of a Buffer persistent
371      *  \note This method is threadsafe.
372      *
373      * \param user      The Owner of that Buffer
374      * \param bufferId  The buffer id
375      * \param MsgId     The Message id where the marker line should be placed
376      */
377     virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
378
379     //! Get a Hash of all marker line message ids
380     /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
381      *  \note This method is threadsafe.
382      *
383      * \param user      The Owner of the buffers
384      */
385     virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
386
387     //! Update the BufferActivity for a Buffer
388     /** This Method is used to make the activity state of a Buffer persistent
389      *  \note This method is threadsafe.
390      *
391      * \param user      The Owner of that Buffer
392      * \param bufferId  The buffer id
393      * \param MsgId     The Message id where the marker line should be placed
394      */
395     virtual void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) = 0;
396
397     //! Get a Hash of all buffer activity states
398     /** This Method is called when the Quassel Core is started to restore the BufferActivities
399      *  \note This method is threadsafe.
400      *
401      * \param user      The Owner of the buffers
402      */
403     virtual QHash<BufferId, Message::Types> bufferActivities(UserId id) = 0;
404
405     //! Get the bitset of buffer activity states for a buffer
406     /** This method is used to load the activity state of a buffer when its last seen message changes.
407      *  \note This method is threadsafe.
408      *
409      * \param bufferId The buffer
410      * \param lastSeenMsgId     The last seen message
411      */
412     virtual Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) = 0;
413
414     /* Message handling */
415
416     //! Store a Message in the storage backend and set its unique Id.
417     /** \param msg  The message object to be stored
418      *  \return true on success
419      */
420     virtual bool logMessage(Message &msg) = 0;
421
422     //! Store a list of Messages in the storage backend and set their unique Id.
423     /** \param msgs The list message objects to be stored
424      *  \return true on success
425      */
426     virtual bool logMessages(MessageList &msgs) = 0;
427
428     //! Request a certain number messages stored in a given buffer.
429     /** \param buffer   The buffer we request messages from
430      *  \param first    if != -1 return only messages with a MsgId >= first
431      *  \param last     if != -1 return only messages with a MsgId < last
432      *  \param limit    if != -1 limit the returned list to a max of \limit entries
433      *  \return The requested list of messages
434      */
435     virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
436
437     //! Request a certain number of messages across all buffers
438     /** \param first    if != -1 return only messages with a MsgId >= first
439      *  \param last     if != -1 return only messages with a MsgId < last
440      *  \param limit    Max amount of messages
441      *  \return The requested list of messages
442      */
443     virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
444
445     //! Get the auth username associated with a userId
446     /** \param user  The user to retrieve the username for
447      *  \return      The username for the user
448      */
449     virtual const QString getAuthusername(UserId user) = 0;
450
451 signals:
452     //! Sent when a new BufferInfo is created, or an existing one changed somehow.
453     void bufferInfoUpdated(UserId user, const BufferInfo &);
454     //! Sent when a Buffer was renamed
455     void bufferRenamed(const QString &newName, const QString &oldName);
456     //! Sent when a new user has been added
457     void userAdded(UserId, const QString &username);
458     //! Sent when a user has been renamed
459     void userRenamed(UserId, const QString &newname);
460     //! Sent when a user has been removed
461     void userRemoved(UserId);
462
463 protected:
464     QString hashPassword(const QString &password);
465     bool checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version);
466
467 private:
468     QString hashPasswordSha1(const QString &password);
469     bool checkHashedPasswordSha1(const QString &password, const QString &hashedPassword);
470
471     QString hashPasswordSha2_512(const QString &password);
472     bool checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword);
473     QString sha2_512(const QString &input);
474 };
475
476
477 #endif