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