Add new constants to password service. Change infinite expiration time.
[platform/core/security/security-server.git] / src / server / service / password-file.h
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        password-file.h
20  * @author      Zbigniew Jasinski (z.jasinski@samsung.com)
21  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
22  * @version     1.0
23  * @brief       Implementation of PasswordFile, used to manage password files.
24  */
25 #ifndef _PASSWORD_FILE_H_
26 #define _PASSWORD_FILE_H_
27
28 #include <string>
29 #include <vector>
30 #include <list>
31 #include <chrono>
32
33 #include <time.h>
34
35 #include <dpl/serialization.h>
36
37 namespace SecurityServer
38 {
39     class PasswordFile
40     {
41     public:
42         PasswordFile();
43
44         void writeMemoryToFile() const;
45         void writeAttemptToFile() const;
46
47         void setPassword(const std::string &password);
48         bool checkPassword(const std::string &password) const;
49         bool isPasswordActive() const;
50
51         void setHistory(unsigned int history);
52         unsigned int getHistorySize() const;
53
54         time_t getExpireTime() const;
55         time_t getExpireTimeLeft() const;
56         void setExpireTime(int expireTime);
57
58         //attempt manipulating functions
59         unsigned int getAttempt() const;
60         void resetAttempt();
61         void incrementAttempt();
62         int getMaxAttempt() const;
63         void setMaxAttempt(unsigned int maxAttempt);
64
65         bool isPasswordReused(const std::string &password) const;
66
67         bool checkExpiration() const;
68         bool checkIfAttemptsExceeded() const;
69         bool isIgnorePeriod() const;
70
71     private:
72         typedef std::vector<unsigned char> RawHash;
73         typedef std::chrono::duration<double> TimeDiff;
74         typedef std::chrono::time_point<std::chrono::monotonic_clock, TimeDiff> TimePoint;
75
76         struct Password: public ISerializable
77         {
78             Password();
79             Password(const RawHash& password);
80             Password(IStream& stream);
81
82             virtual void Serialize(IStream &stream) const;
83
84             RawHash m_password;
85         };
86
87         typedef std::list<Password> PasswordList;
88
89         void loadMemoryFromFile();
90
91         void resetTimer();
92         void preparePwdFile();
93         void prepareAttemptFile();
94         bool fileExists(const std::string &filename) const;
95         bool dirExists(const std::string &dirpath) const;
96         static RawHash hashPassword(const std::string &password);
97
98         mutable TimePoint m_retryTimerStart;
99
100         //password file data
101         PasswordList m_passwords;
102         unsigned int m_maxAttempt;
103         unsigned int m_historySize;
104         time_t m_expireTime;
105
106         //attempt file data
107         unsigned int m_attempt;
108     };
109 }    //namespace SecurityServer
110
111 #endif