Remove security-server dependency
[apps/native/starter.git] / src / mobile / lock_pwd_verification.c
1 /*
2  * Copyright (c) 2009-2014 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 <Elementary.h>
18 #include <security-manager.h>
19 #include <vconf.h>
20 #include <vconf-keys.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23
24 #include "util.h"
25 #include "status.h"
26 #include "lock_mgr.h"
27 #include "lock_pwd_util.h"
28 #include "lock_pwd_verification.h"
29 #include "lock_pwd_simple.h"
30
31 #define PASSWORD_LENGTH_MIN 4
32 #define PASSWORD_LENGTH_MAX 16
33
34 typedef enum {
35         NORMAL_PWD = 0,
36         EMPTY_PWD = 1,
37         OVERLENGTH_PWD = 2,
38 } lock_pwd_type;
39
40 static struct _s_lock_pwd_verification {
41         unsigned int current_attempt;
42         unsigned int remain_attempt;
43         unsigned int max_attempt;
44         unsigned int expire_sec;
45         unsigned int incorrect_count;
46 } s_lock_pwd_verification = {
47         .current_attempt = 0,
48         .remain_attempt = 0,
49         .max_attempt = 0,
50         .expire_sec = 0,
51         .incorrect_count = 0,
52 };
53
54
55
56 int lock_pwd_verification_current_attempt_get(void)
57 {
58         return s_lock_pwd_verification.current_attempt;
59 }
60
61
62 int lock_pwd_verification_remain_attempt_get(void)
63 {
64         return s_lock_pwd_verification.remain_attempt;
65 }
66
67
68 static Eina_Bool _pwd_verification_check_pwd(const char *str)
69 {
70 #if 0
71         int ret = SECURITY_SERVER_API_ERROR_PASSWORD_MISMATCH;
72         unsigned int current_attempt = 0;
73         unsigned int max_attempt = 0;
74         unsigned int expire_sec = 0;
75
76         ret = security_server_chk_pwd(str, &current_attempt, &max_attempt, &expire_sec);
77         _D("ret(%d), current_attempt(%d), max_attempt(%d), valid_sec(%d)", ret, current_attempt, max_attempt, expire_sec);
78
79         s_lock_pwd_verification.current_attempt = current_attempt;
80         s_lock_pwd_verification.max_attempt = max_attempt;
81         s_lock_pwd_verification.expire_sec = expire_sec;
82
83         switch(ret) {
84         case SECURITY_SERVER_API_SUCCESS:
85         case SECURITY_SERVER_API_ERROR_PASSWORD_EXPIRED:
86                 _E("Correct password");
87                 return EINA_TRUE;
88         case SECURITY_SERVER_API_ERROR_PASSWORD_RETRY_TIMER:
89                 _E("Timer set! : not saved");
90                 break;
91         default:
92                 _E("Incorrect password");
93                 break;
94         }
95 #endif
96
97         return EINA_FALSE;
98 }
99
100
101 static lock_pwd_type _pwd_verification_check_length(const char *str, int min, int max)
102 {
103         int len = 0;
104
105         retv_if(!str, EMPTY_PWD);
106
107         len = strlen(str);
108         retv_if(len == 0, EMPTY_PWD);
109
110         retv_if(len < min || len > max, OVERLENGTH_PWD);
111
112         return NORMAL_PWD;
113 }
114
115 static void _pwd_values_init(void)
116 {
117         s_lock_pwd_verification.current_attempt = 0;
118         s_lock_pwd_verification.remain_attempt = PASSWORD_ATTEMPTS_MAX_NUM;
119         s_lock_pwd_verification.max_attempt = PASSWORD_ATTEMPTS_MAX_NUM;
120         s_lock_pwd_verification.incorrect_count = 0;
121 }
122
123 lock_pwd_event_e lock_pwd_verification_verify(const char *password)
124 {
125         lock_pwd_type pwd_type = NORMAL_PWD;
126
127         retv_if(!password, PWD_EVENT_EMPTY);
128
129         pwd_type = _pwd_verification_check_length(password, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX);
130         switch(pwd_type) {
131         case NORMAL_PWD:
132                 if (_pwd_verification_check_pwd(password)) {
133                         _D("Correct Password");
134                         _pwd_values_init();
135                         return PWD_EVENT_CORRECT;
136                 } else {
137                         s_lock_pwd_verification.incorrect_count++;
138                         s_lock_pwd_verification.remain_attempt--;
139                         _D("incorrect_count(%d), remain_attempt(%d)", s_lock_pwd_verification.incorrect_count, s_lock_pwd_verification.remain_attempt);
140
141                         if (s_lock_pwd_verification.remain_attempt == 0) {
142                                 _pwd_values_init();
143                                 return PWD_EVENT_INPUT_BLOCK;
144                         } else {
145                                 return PWD_EVENT_INCORRECT;
146                         }
147                 }
148                 break;
149         case EMPTY_PWD:
150                 return PWD_EVENT_EMPTY;
151                 break;
152         case OVERLENGTH_PWD:
153                 return PWD_EVENT_OVER;
154                 break;
155         }
156
157         return PWD_EVENT_INCORRECT;
158 }
159
160 void lock_pwd_verification_policy_create(void)
161 {
162 #if 0
163         int ret = 0;
164         unsigned int current_attempt = 0;
165         unsigned int max_attempt = 0;
166         unsigned int expire_sec = 0;
167
168         ret = security_server_is_pwd_valid(&current_attempt, &max_attempt, &expire_sec);
169         _D("policy status(%d), current_attempt(%d), max_attempt(%d)", ret, current_attempt, max_attempt);
170
171         if (ret == SECURITY_SERVER_API_ERROR_NO_PASSWORD ||
172                         ret == SECURITY_SERVER_API_ERROR_PASSWORD_EXIST) {
173                 s_lock_pwd_verification.current_attempt = current_attempt;
174                 s_lock_pwd_verification.max_attempt = max_attempt;
175                 s_lock_pwd_verification.expire_sec = expire_sec;
176         }
177
178         s_lock_pwd_verification.remain_attempt = PASSWORD_ATTEMPTS_MAX_NUM;
179         s_lock_pwd_verification.incorrect_count = 0;
180 #endif
181
182         return;
183 }
184
185 void lock_pwd_verification_popup_create(lock_pwd_event_e event)
186 {
187         char popup_text[BUF_SIZE_512] = { 0, };
188         int remain_attempt = 0;
189         int current_attempt = 0;
190
191         current_attempt = lock_pwd_verification_current_attempt_get();
192         remain_attempt = lock_pwd_verification_remain_attempt_get();
193         _D("current_attemp(%d), remain_attempt(%d)", current_attempt, remain_attempt);
194
195         switch(event) {
196         case PWD_EVENT_INCORRECT_WARNING:
197                 snprintf(popup_text, sizeof(popup_text), _("IDS_LCKSCN_POP_YOU_HAVE_ATTEMPTED_TO_UNLOCK_THE_DEVICE_INCORRECTLY_P1SD_P1SD_TIMES_YOU_HAVE_P2SD_ATTEMPTS_LEFT_BEFORE_THE_DEVICE_IS_RESET_THE_DEVICE_IS_RESET_TO_FACTORY_MSG"), current_attempt, remain_attempt);
198                 lock_pwd_util_popup_create(NULL, popup_text, NULL, 0.0);
199                 break;
200         case PWD_EVENT_INPUT_BLOCK:
201                 snprintf(popup_text, sizeof(popup_text), _("IDS_LCKSCN_POP_YOU_HAVE_MADE_P1SD_UNSUCCESSFUL_ATTEMPTS_TO_UNLOCK_YOUR_DEVICE_TRY_AGAIN_IN_P2SD_SECONDS"), PASSWORD_ATTEMPTS_MAX_NUM, PASSWORD_BLOCK_SECONDS);
202                 lock_pwd_util_popup_create(_("IDS_LCKSCN_HEADER_UNABLE_TO_UNLOCK_SCREEN_ABB"), popup_text, NULL, 15.0);
203                 break;
204         case PWD_EVENT_INPUT_BLOCK_WARNING:
205                 snprintf(popup_text, sizeof(popup_text), _("IDS_LCKSCN_POP_YOU_HAVE_ATTEMPTED_TO_UNLOCK_THE_DEVICE_INCORRECTLY_P1SD_TIMES_YOU_HAVE_P2SD_ATTEMPTS_LEFT_BEFORE_THE_DEVICE_IS_RESET_TO_FACTORY_MSG"), current_attempt, remain_attempt);
206                 lock_pwd_util_popup_create(NULL, popup_text, NULL, 0.0);
207                 break;
208         default:
209                 break;
210         }
211 }