Add dpm auth policy
[platform/core/security/dpm-auth.git] / plugin / password-manager.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <klay/exception.h>
18
19 #include "password-manager.h"
20
21 PasswordManager::PasswordManager(uid_t uid) :
22         user(uid)
23 {
24         if (auth_passwd_new_policy(&p_policy) != AUTH_PASSWD_API_SUCCESS) {
25                 throw runtime::Exception("Failed to get auth instance");
26         }
27
28         auth_passwd_set_user(p_policy, user);
29 }
30
31 PasswordManager::~PasswordManager()
32 {
33         auth_passwd_free_policy(p_policy);
34 }
35
36 void PasswordManager::setQuality(PasswordManager::QualityType quality)
37 {
38         if (auth_passwd_set_quality(p_policy, quality) != AUTH_PASSWD_API_SUCCESS) {
39                 throw runtime::Exception("Failed to enforce password quality");
40         }
41 }
42
43 void PasswordManager::setMinimumLength(int value)
44 {
45         if (auth_passwd_set_min_length(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
46                 throw runtime::Exception("Failed to set minimum length");
47         }
48 }
49
50 void PasswordManager::setMinimumComplexCharacters(int value)
51 {
52         if (auth_passwd_set_min_complex_char_num(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
53                 throw runtime::Exception("Failed to set minimum complex characters");
54         }
55 }
56
57 void PasswordManager::setMaximumFailedForWipe(int value)
58 {
59         if (auth_passwd_set_max_attempts(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
60                 throw runtime::Exception("Failed to set maximum failed count for wipe");
61         }
62 }
63
64 void PasswordManager::setExpires(int value)
65 {
66         if (auth_passwd_set_validity(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
67                 throw runtime::Exception("Failed to set expire");
68         }
69 }
70
71 void PasswordManager::setHistory(int value)
72 {
73         if (auth_passwd_set_history_size(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
74                 throw runtime::Exception("Failed to set history size");
75         }
76 }
77
78 void PasswordManager::setPattern(const char* pattern)
79 {
80         if (auth_passwd_set_pattern(p_policy, pattern) != AUTH_PASSWD_API_SUCCESS) {
81                 throw runtime::Exception("Failed to set pattern");
82         }
83 }
84
85 void PasswordManager::deletePatern()
86 {
87         if (auth_passwd_set_pattern(p_policy, NULL) != AUTH_PASSWD_API_SUCCESS) {
88                 throw runtime::Exception("Failed to delete pattern");
89         }
90 }
91
92 void PasswordManager::setMaximumCharacterOccurrences(int value)
93 {
94         if (auth_passwd_set_max_char_occurrences(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
95                 throw runtime::Exception("Failed to set maximum character occurrences");
96         }
97 }
98
99 void PasswordManager::setMaximumNumericSequenceLength(int value)
100 {
101         if (auth_passwd_set_max_num_seq_len(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
102                 throw runtime::Exception("Failed to set maximum numeric sequence length");
103         }
104 }
105
106 void PasswordManager::setForbiddenStrings(const std::vector<std::string> &forbiddenStrings)
107 {
108         for (const std::string& str : forbiddenStrings) {
109                 if (auth_passwd_set_forbidden_passwd(p_policy, str.c_str()) != AUTH_PASSWD_API_SUCCESS) {
110                         throw runtime::Exception("Failed to set forbidden strings");
111                 }
112         }
113 }
114
115 void PasswordManager::enforce()
116 {
117         if (auth_passwd_set_policy(p_policy) != AUTH_PASSWD_API_SUCCESS) {
118                 throw runtime::Exception("Failed to enforce policy");
119         }
120 }
121
122 void PasswordManager::resetPassword(const std::string& password)
123 {
124         if (auth_passwd_reset_passwd(AUTH_PWD_NORMAL, user, password.c_str()) != AUTH_PASSWD_API_SUCCESS) {
125                 throw runtime::Exception("Failed to set reset password");
126         }
127 }