Add dpm auth policy
[platform/core/security/dpm-auth.git] / api / password.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 <cstring>
18 #include <cassert>
19 #include <vector>
20 #include <utility>
21
22 #include <tizen.h>
23 #include <tizen_type.h>
24
25 #include <dpm/pil/policy-client.h>
26
27 #include "auth.h"
28
29 template <typename T>
30 class Array final {
31 public:
32         Array() = delete;
33         Array(std::vector<T> &&list) : list(std::move(list)), it(this->list.begin())
34         {
35         }
36
37         Array(const std::vector<T> &list) : list(list), it(this->list.begin())
38         {
39         }
40
41         T *next() {
42                 if (it != list.end()) {
43                         return &(*it++);
44                 }
45                 return NULL;
46         }
47
48         bool isEnd() {
49                 return it == list.end();
50         }
51
52 private:
53         std::vector<T> list;
54         typename std::vector<T>::iterator it;
55 };
56
57 EXPORT_API int dpm_password_set_quality(void* handle, int quality)
58 {
59         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
60         RET_ON_FAILURE(quality >= 0, DPM_ERROR_INVALID_PARAMETER);
61
62         DevicePolicyClient &client = GetDevicePolicyClient(handle);
63
64         try {
65                 Status<int> ret { -1 };
66                 ret = client.methodCall<int>("Password::setQuality", quality);
67                 return ret.get();
68         } catch (...) {
69                 return -1;
70         }
71 }
72
73 EXPORT_API int dpm_password_get_quality(void* handle, int *quality)
74 {
75         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
76         RET_ON_FAILURE(quality, DPM_ERROR_INVALID_PARAMETER);
77
78         DevicePolicyClient &client = GetDevicePolicyClient(handle);
79
80         try {
81                 Status<int> ret { 0 };
82                 ret = client.methodCall<int>("Password::getQuality");
83                 if (ret.get() < 0) {
84                         return -1;
85                 }
86
87                 *quality = ret.get();
88         } catch (...) {
89                 return -1;
90         }
91
92         return DPM_ERROR_NONE;
93 }
94
95 EXPORT_API int dpm_password_set_minimum_length(void* handle, int value)
96 {
97         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
98         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
99
100         DevicePolicyClient &client = GetDevicePolicyClient(handle);
101
102         try {
103                 Status<int> ret { 0 };
104                 ret = client.methodCall<int>("Password::setMinimumLength", value);
105                 return ret.get();
106         } catch (...) {
107                 return -1;
108         }
109 }
110
111 EXPORT_API int dpm_password_get_minimum_length(void* handle, int *value)
112 {
113         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
114         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
115
116         DevicePolicyClient &client = GetDevicePolicyClient(handle);
117
118         try {
119                 Status<int> ret { 0 };
120                 ret = client.methodCall<int>("Password::getMinimumLength");
121
122                 if (ret.get() < 0) {
123                         return -1;
124                 }
125
126                 *value = ret.get();
127         } catch (...) {
128                 return -1;
129         }
130
131         return DPM_ERROR_NONE;
132 }
133
134 EXPORT_API int dpm_password_set_min_complex_chars(void* handle, int value)
135 {
136         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
137         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
138
139         DevicePolicyClient &client = GetDevicePolicyClient(handle);
140
141         try {
142                 Status<int> ret { -1 };
143                 ret = client.methodCall<int>("Password::setMinComplexChars", value);
144                 return ret.get();
145         } catch (...) {
146                 return -1;
147         }
148 }
149
150 EXPORT_API int dpm_password_get_min_complex_chars(void* handle, int *value)
151 {
152         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
153         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
154
155         DevicePolicyClient &client = GetDevicePolicyClient(handle);
156
157         try {
158                 Status<int> ret { 0 };
159                 ret = client.methodCall<int>("Password::getMinComplexChars");
160                 if (ret.get() < 0) {
161                         return -1;
162                 }
163
164                 *value = ret.get();
165         } catch (...) {
166                 return -1;
167         }
168
169         return DPM_ERROR_NONE;
170 }
171
172 EXPORT_API int dpm_password_set_maximum_failed_attempts_for_wipe(void* handle, int value)
173 {
174         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
175         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
176
177         DevicePolicyClient &client = GetDevicePolicyClient(handle);
178
179         try {
180                 Status<int> ret { -1 };
181                 ret = client.methodCall<int>("Password::setMaximumFailedForWipe", value);
182                 return ret.get();
183         } catch (...) {
184                 return -1;
185         }
186 }
187
188 EXPORT_API int dpm_password_get_maximum_failed_attempts_for_wipe(void* handle, int *value)
189 {
190         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
191         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
192
193         DevicePolicyClient &client = GetDevicePolicyClient(handle);
194
195         try {
196                 Status<int> ret { 0 };
197                 ret = client.methodCall<int>("Password::getMaximumFailedForWipe");
198                 if (ret.get() < 0) {
199                         return -1;
200                 }
201
202                 *value = ret.get();
203         } catch (...) {
204                 return -1;
205         }
206
207         return DPM_ERROR_NONE;
208 }
209
210 EXPORT_API int dpm_password_set_expires(void* handle, int value)
211 {
212         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
213         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
214
215         DevicePolicyClient &client = GetDevicePolicyClient(handle);
216
217         try {
218                 Status<int> ret { -1 };
219                 ret = client.methodCall<int>("Password::setExpires", value);
220                 return ret.get();
221         } catch (...) {
222                 return -1;
223         }
224 }
225
226 EXPORT_API int dpm_password_get_expires(void* handle, int *value)
227 {
228         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
229         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
230
231         DevicePolicyClient &client = GetDevicePolicyClient(handle);
232
233         try {
234                 Status<int> ret { 0 };
235                 ret = client.methodCall<int>("Password::getExpires");
236                 if (ret.get() < 0) {
237                         return -1;
238                 }
239
240                 *value = ret.get();
241         } catch (...) {
242                 return -1;
243         }
244
245         return DPM_ERROR_NONE;
246 }
247
248 EXPORT_API int dpm_password_set_history(void* handle, int value)
249 {
250         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
251         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
252
253         DevicePolicyClient &client = GetDevicePolicyClient(handle);
254
255         try {
256                 Status<int> ret { -1 };
257                 ret = client.methodCall<int>("Password::setHistory", value);
258                 return ret.get();
259         } catch (...) {
260                 return -1;
261         }
262 }
263
264 EXPORT_API int dpm_password_get_history(void* handle, int *value)
265 {
266         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
267         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
268
269         DevicePolicyClient &client = GetDevicePolicyClient(handle);
270
271         try {
272                 Status<int> ret { 0 };
273                 ret = client.methodCall<int>("Password::getHistory");
274                 if (ret.get() < 0) {
275                         return -1;
276                 }
277
278                 *value = ret.get();
279         } catch (...) {
280                 return -1;
281         }
282
283         return DPM_ERROR_NONE;
284 }
285
286 EXPORT_API int dpm_password_set_pattern(void* handle, const char *pattern)
287 {
288         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
289         RET_ON_FAILURE(pattern, DPM_ERROR_INVALID_PARAMETER);
290
291         DevicePolicyClient &client = GetDevicePolicyClient(handle);
292
293         try {
294                 Status<int> ret { -1 };
295                 ret = client.methodCall<int>("Password::setPattern", pattern);
296                 return ret.get();
297         } catch (...) {
298                 return -1;
299         }
300 }
301
302 EXPORT_API int dpm_password_reset(void* handle, const char *passwd)
303 {
304         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
305         RET_ON_FAILURE(passwd, DPM_ERROR_INVALID_PARAMETER);
306
307         DevicePolicyClient &client = GetDevicePolicyClient(handle);
308
309         try {
310                 Status<int> ret { -1 };
311                 ret = client.methodCall<int>("Password::reset", passwd);
312                 return ret.get();
313         } catch (...) {
314                 return -1;
315         }
316 }
317
318 EXPORT_API int dpm_password_enforce_change(void* handle)
319 {
320         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
321
322         DevicePolicyClient &client = GetDevicePolicyClient(handle);
323
324         try {
325                 Status<int> ret { -1 };
326                 ret = client.methodCall<int>("Password::enforceChange");
327                 return ret.get();
328         } catch (...) {
329                 return -1;
330         }
331 }
332
333 EXPORT_API int dpm_password_set_max_inactivity_time_device_lock(void* handle, int value)
334 {
335         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
336         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
337
338         DevicePolicyClient &client = GetDevicePolicyClient(handle);
339
340         try {
341                 Status<int> ret { -1 };
342                 ret = client.methodCall<int>("Password::setMaxInactivityTimeDeviceLock", value);
343                 return ret.get();
344         } catch (...) {
345                 return -1;
346         }
347 }
348
349 EXPORT_API int dpm_password_get_max_inactivity_time_device_lock(void* handle, int *value)
350 {
351         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
352         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
353
354         DevicePolicyClient &client = GetDevicePolicyClient(handle);
355
356         try {
357                 Status<int> ret { 0 };
358                 ret = client.methodCall<int>("Password::getMaxInactivityTimeDeviceLock");;
359                 if (ret.get() < 0) {
360                         return -1;
361                 }
362
363                 *value = ret.get();
364         } catch (...) {
365                 return -1;
366         }
367
368         return DPM_ERROR_NONE;
369 }
370
371 EXPORT_API int dpm_password_set_status(void* handle, dpm_password_status_e status)
372 {
373         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
374         RET_ON_FAILURE(status >= DPM_PASSWORD_STATUS_NORMAL &&
375                                    status <= DPM_PASSWORD_STATUS_PATTERN_CHANGED,
376                                    DPM_ERROR_INVALID_PARAMETER);
377
378         DevicePolicyClient &client = GetDevicePolicyClient(handle);
379
380         try {
381                 Status<int> ret { -1 };
382                 ret = client.methodCall<int>("Password::setStatus", (int)status);
383                 return ret.get();
384         } catch (...) {
385                 return -1;
386         }
387 }
388
389 EXPORT_API int dpm_password_get_ret(void* handle, dpm_password_status_e *status)
390 {
391         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
392         RET_ON_FAILURE(status, DPM_ERROR_INVALID_PARAMETER);
393
394         DevicePolicyClient &client = GetDevicePolicyClient(handle);
395
396         try {
397                 Status<int> ret { 0 };
398                 ret = client.methodCall<int>("Password::getStatus");
399                 if (ret.get() < 0) {
400                         return -1;
401                 }
402
403                 *status = (dpm_password_status_e)ret.get();
404         } catch (...) {
405                 return -1;
406         }
407
408         return DPM_ERROR_NONE;
409 }
410
411 EXPORT_API int dpm_password_delete_pattern(void* handle)
412 {
413         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
414
415         DevicePolicyClient &client = GetDevicePolicyClient(handle);
416
417         try {
418                 Status<int> ret { -1 };
419                 ret = client.methodCall<int>("Password::deletePattern");
420                 return ret.get();
421         } catch (...) {
422                 return -1;
423         }
424 }
425
426 EXPORT_API int dpm_password_get_pattern(void* handle, char **pattern)
427 {
428         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
429         RET_ON_FAILURE(pattern, DPM_ERROR_INVALID_PARAMETER);
430
431         DevicePolicyClient &client = GetDevicePolicyClient(handle);
432
433         try {
434                 Status<std::string> ret { std::string() };
435                 ret = client.methodCall<std::string>("Password::getPattern");
436                 *pattern = ::strdup(ret.get().c_str());
437         } catch (...) {
438                 return -1;
439         }
440
441         return DPM_ERROR_NONE;
442 }
443
444 EXPORT_API int dpm_password_set_maximum_character_occurrences(void* handle, int value)
445 {
446         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
447         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
448
449         DevicePolicyClient &client = GetDevicePolicyClient(handle);
450
451         try {
452                 Status<int> ret { -1 };
453                 ret = client.methodCall<int>("Password::setMaximumCharacterOccurrences", value);
454                 return ret.get();
455         } catch (...) {
456                 return -1;
457         }
458 }
459
460 EXPORT_API int dpm_password_get_maximum_character_occurrences(void* handle, int *value)
461 {
462         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
463         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
464
465         DevicePolicyClient &client = GetDevicePolicyClient(handle);
466
467         try {
468                 Status<int> ret { 0 };
469                 ret = client.methodCall<int>("Password::getMaximumCharacterOccurrences");
470                 if (ret.get() < 0) {
471                         return -1;
472                 }
473
474                 *value = ret.get();
475         } catch (...) {
476                 return -1;
477         }
478
479         return DPM_ERROR_NONE;
480 }
481
482 EXPORT_API int dpm_password_set_maximum_numeric_sequence_length(void* handle, int value)
483 {
484         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
485         RET_ON_FAILURE(value >= 0, DPM_ERROR_INVALID_PARAMETER);
486
487         DevicePolicyClient &client = GetDevicePolicyClient(handle);
488
489         try {
490                 Status<int> ret { -1 };
491                 ret = client.methodCall<int>("Password::setMaximumNumericSequenceLength", value);
492                 return ret.get();
493         } catch (...) {
494                 return -1;
495         }
496 }
497
498 EXPORT_API int dpm_password_get_maximum_numeric_sequence_length(void* handle, int *value)
499 {
500         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
501         RET_ON_FAILURE(value, DPM_ERROR_INVALID_PARAMETER);
502
503         DevicePolicyClient &client = GetDevicePolicyClient(handle);
504
505         try {
506                 Status<int> ret { 0 };
507                 ret = client.methodCall<int>("Password::getMaximumNumericSequenceLength");
508                 if (ret.get() < 0) {
509                         return -1;
510                 }
511
512                 *value = ret.get();
513         } catch (...) {
514                 return -1;
515         }
516
517         return DPM_ERROR_NONE;
518 }
519
520 typedef Array<std::string> dpm_password_iterator;
521
522 EXPORT_API dpm_password_iterator_h dpm_password_create_iterator(void* handle)
523 {
524         RET_ON_FAILURE(handle, NULL);
525
526         DevicePolicyClient &client = GetDevicePolicyClient(handle);
527
528         Status<std::vector<std::string>> status { std::vector<std::string>() };
529         status = client.methodCall<std::vector<std::string>>("Password::getForbiddenStrings");
530
531         dpm_password_iterator *iter = new dpm_password_iterator(status.get());
532         return reinterpret_cast<dpm_password_iterator_h>(iter);
533 }
534
535 EXPORT_API int dpm_password_iterator_next(dpm_password_iterator_h iter, const char **result)
536 {
537         RET_ON_FAILURE(iter, DPM_ERROR_INVALID_PARAMETER);
538         RET_ON_FAILURE(result, DPM_ERROR_INVALID_PARAMETER);
539
540         dpm_password_iterator *it = reinterpret_cast<dpm_password_iterator *>(iter);
541
542         if (it->isEnd())
543                 *result = NULL;
544         else
545                 *result = it->next()->c_str();
546
547         return DPM_ERROR_NONE;
548 }
549
550 EXPORT_API int dpm_password_destroy_iterator(dpm_password_iterator_h iter)
551 {
552         RET_ON_FAILURE(iter, DPM_ERROR_INVALID_PARAMETER);
553
554         delete reinterpret_cast<dpm_password_iterator *>(iter);
555
556         return DPM_ERROR_NONE;
557 }
558
559 EXPORT_API int dpm_password_set_forbidden_strings(void* handle, const char *strings[], int length)
560 {
561         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
562
563         DevicePolicyClient &client = GetDevicePolicyClient(handle);
564         std::vector<std::string> forbiddenStrings;
565
566         for (int i = 0; i < length; i++)
567                 forbiddenStrings.push_back(strings[i]);
568
569         try {
570                 Status<int> ret { -1 };
571                 ret = client.methodCall<int>("Password::setForbiddenStrings", forbiddenStrings);
572                 return ret.get();
573         } catch(...) {
574                 return -1;
575         }
576 }
577
578 EXPORT_API int dpm_password_set_recovery(void* handle, int enable)
579 {
580         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
581
582         DevicePolicyClient &client = GetDevicePolicyClient(handle);
583
584         try {
585                 Status<int> ret { -1 };
586                 ret = client.methodCall<int>("Password::setRecovery", enable);
587                 return ret.get();
588         } catch (...) {
589                 return -1;
590         }
591 }
592
593 EXPORT_API int dpm_password_get_recovery(void* handle, int *enable)
594 {
595         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
596         RET_ON_FAILURE(enable, DPM_ERROR_INVALID_PARAMETER);
597
598         DevicePolicyClient &client = GetDevicePolicyClient(handle);
599
600         try {
601                 Status<int> ret { false };
602                 ret = client.methodCall<int>("Password::getRecovery");
603                 if (ret.get() < 0) {
604                         return -1;
605                 }
606
607                 *enable = ret.get();
608         } catch (...) {
609                 return -1;
610         }
611
612         return DPM_ERROR_NONE;
613 }
614