re-organize security
[apps/core/preloaded/settings.git] / setting-security / src / setting-security.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /**
23  *@defgroup setting-security
24  *UG creation code for setting-security
25  */
26
27 #include <setting-security.h>
28 #define Max_Passwd_View_Type_Len        64
29 #ifndef UG_MODULE_API
30 #define UG_MODULE_API __attribute__ ((visibility("default")))
31 #endif
32
33
34 int pwd_handler_do_nothing(SettingSecurityUG *data, void* arg)
35 {
36         SETTING_TRACE_BEGIN;
37         //SettingSecurityUG *ad = (SettingSecurityUG *)data;
38         // DO NOTHING
39         return 0;
40 }
41
42 int pwd_handler_sec_pw_sim_lock_on(SettingSecurityUG *data, void* arg)
43 {
44         SETTING_TRACE_BEGIN;
45         SettingSecurityUG *ad = (SettingSecurityUG *)data;
46         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_PS, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
47         {
48                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
49         }
50         return 0;
51 }
52
53
54 int pwd_handler_sec_pw_sim_lock_off(SettingSecurityUG *data, void* arg)
55 {
56         SETTING_TRACE_BEGIN;
57         SettingSecurityUG *ad = (SettingSecurityUG *)data;
58         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_PS, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
59         {
60                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
61         }
62         return 0;
63 }
64
65
66 int pwd_handler_sec_pw_pin_lock_on(SettingSecurityUG *data, void* arg)
67 {
68         SETTING_TRACE_BEGIN;
69         SettingSecurityUG *ad = (SettingSecurityUG *)data;
70         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_SC, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
71         {
72                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
73         }
74         return 0;
75 }
76
77
78 int pwd_handler_sec_pw_pin_lock_off(SettingSecurityUG *data, void* arg)
79 {
80         SETTING_TRACE_BEGIN;
81         SettingSecurityUG *ad = (SettingSecurityUG *)data;
82         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_SC, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
83         {
84                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
85         }
86         return 0;
87 }
88
89
90 int pwd_handler_sec_pw_sim_lock_disabled(SettingSecurityUG *data, void* arg)
91 {
92         SETTING_TRACE_BEGIN;
93         SettingSecurityUG *ad = (SettingSecurityUG *)data;
94         // [in] Evas_Object *checkbtn
95         if (ad->data_sim_lk)
96                 setting_disable_genlist_item(ad->data_sim_lk->item);
97
98         return 0;
99 }
100
101
102 int pwd_handler_sec_pw_change_pin1(SettingSecurityUG *data, void* arg)
103 {
104         SETTING_TRACE_BEGIN;
105         SettingSecurityUG *ad = (SettingSecurityUG *)data;
106         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_SC, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
107         {
108                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
109         }
110         return 0;
111 }
112
113 int pwd_handler_sec_pw_change_pin2(SettingSecurityUG *data, void* arg)
114 {
115         SETTING_TRACE_BEGIN;
116         SettingSecurityUG *ad = (SettingSecurityUG *)data;
117
118         if (tel_get_sim_facility(ad->handle, TAPI_SIM_LOCK_FD, setting_security_sim_get_facility_cb, ad) != TAPI_API_SUCCESS)
119         {
120                 SETTING_TRACE_ERROR("tel_get_sim_facility(TAPI_SIM_LOCK_PS) failed");
121         }
122         return 0;
123 }
124
125
126 int pwd_handler_sec_pw_pin1_blocked(SettingSecurityUG *data, void* arg)
127 {
128         SETTING_TRACE_BEGIN;
129         SettingSecurityUG *ad = (SettingSecurityUG *)data;
130         return 0;
131 }
132
133
134 int pwd_handler_sec_pw_pin2_blocked(SettingSecurityUG *data, void* arg)
135 {
136         SETTING_TRACE_BEGIN;
137         SettingSecurityUG *ad = (SettingSecurityUG *)data;
138         return 0;
139 }
140
141 static struct _security_item security_table[] = {
142         { SETTING_SEC_PW_SIM_LOCK_ON,           "SETTING_PW_TYPE_SIM_LOCK_ON",                  pwd_handler_sec_pw_sim_lock_on },
143         { SETTING_SEC_PW_SIM_LOCK_OFF,          "SETTING_PW_TYPE_SIM_LOCK_OFF",                 pwd_handler_sec_pw_sim_lock_off },
144         { SETTING_SEC_PW_SIM_LOCK_DISABLED, "SETTING_PW_TYPE_SIM_LOCK_DISABLE",         pwd_handler_sec_pw_sim_lock_disabled},
145         { SETTING_SEC_PW_PIN_LOCK_ON,           "SETTING_PW_TYPE_PIN_LOCK_ON",                  pwd_handler_sec_pw_pin_lock_on },
146         { SETTING_SEC_PW_PIN_LOCK_OFF,          "SETTING_PW_TYPE_PIN_LOCK_OFF",                 pwd_handler_sec_pw_pin_lock_off },
147         { SETTING_SEC_PW_CHANGE_PIN1,           "SETTING_PW_TYPE_CHANGE_PIN1",                  pwd_handler_sec_pw_change_pin1},
148         { SETTING_SEC_PW_CHANGE_PIN2,           "SETTING_PW_TYPE_CHANGE_PIN2",                  pwd_handler_sec_pw_change_pin2},
149         { SETTING_SEC_PW_PIN1_BLOCKED,          "SETTING_PW_TYPE_PIN1_BLOCKED",                 pwd_handler_sec_pw_pin1_blocked},
150         { SETTING_SEC_PW_PIN2_BLOCKED,          "SETTING_PW_TYPE_PIN2_BLOCKED",                 pwd_handler_sec_pw_pin2_blocked},
151         { SETTING_SEC_PW_PIN1_BLOCKED,          "SETTING_PW_TYPE_PIN1_UNBLOCKED",               pwd_handler_sec_pw_pin_lock_on},
152         { SETTING_SEC_PW_PIN2_BLOCKED,          "SETTING_PW_TYPE_PIN2_UNBLOCKED",               pwd_handler_do_nothing},
153         /*------------------------------------------------------------------------------------------------------------*/
154         { SETTING_SEC_PW_MAX,                           NULL,                                                                   pwd_handler_do_nothing}
155 };
156
157 static void setting_security_ug_cb_resize(void *data, Evas *e,
158                                           Evas_Object *obj, void *event_info)
159 {
160         ret_if(data == NULL);
161
162         SettingSecurityUG *ad = (SettingSecurityUG *) data;     /* ad is point to data */
163         setting_view_update(&setting_view_security_main, ad);
164 }
165
166 static void *setting_security_ug_on_create(ui_gadget_h ug,
167                                            enum ug_mode mode, service_h service,
168                                            void *priv)
169 {
170         setting_retvm_if((!priv), NULL, "!priv");
171
172         SettingSecurityUG *securityUG = priv;
173         securityUG->ug = ug;
174
175         securityUG->win_main_layout = (Evas_Object *) ug_get_parent_layout(ug);
176         securityUG->win_get = (Evas_Object *) ug_get_window();
177         evas_object_show(securityUG->win_main_layout);
178         securityUG->evas = evas_object_evas_get(securityUG->win_main_layout);
179
180         setting_retvm_if(securityUG->win_main_layout == NULL, NULL,
181                          "[Setting >> About] cannot get main window ");
182
183         /* register view node table */
184         setting_view_node_table_intialize();
185         setting_view_node_table_register(&setting_view_security_main, NULL);
186         setting_view_node_table_register(&setting_view_security_sim_settings, &setting_view_security_main);
187
188         setting_create_Gendial_itc("dialogue/1text", &(securityUG->itc_1text));
189         setting_create_Gendial_itc("dialogue/2text.3", &(securityUG->itc_2text_2));
190         setting_create_Gendial_itc("dialogue/1text.1icon", &(securityUG->itc_1text_1icon));
191
192         securityUG->itc_seperator.item_style = "dialogue/separator/21/with_line";
193         securityUG->itc_seperator.func.text_get = NULL;
194         securityUG->itc_seperator.func.content_get = NULL;
195         securityUG->itc_seperator.func.state_get = NULL;
196         securityUG->itc_seperator.func.del = NULL;
197
198         securityUG->update_view_timer = NULL;
199         /*  creating a view. */
200         securityUG->handle =  tel_init(NULL);
201         if (!securityUG->handle) {
202                 SETTING_TRACE_ERROR("*** [ERR] tel_init. ***");
203                 //return NULL;
204         }
205         else {
206                 if (TAPI_API_SUCCESS != tel_get_sim_facility(securityUG->handle, TAPI_SIM_LOCK_PS, setting_security_sim_get_facility_cb, securityUG)) {
207                         SETTING_TRACE_ERROR("*** [ERR] tel_get_sim_facility(sim_status). ***");
208                 }
209
210                 if (TAPI_API_SUCCESS != tel_get_sim_facility(securityUG->handle, TAPI_SIM_LOCK_SC, setting_security_sim_get_facility_cb, securityUG)) {
211                         SETTING_TRACE_ERROR("*** [ERR] tel_get_sim_facility(pin1). ***");
212                 }
213         }
214
215         setting_view_node_set_cur_view(&setting_view_security_main);
216         setting_view_create(&setting_view_security_main, (void *)securityUG);
217         evas_object_event_callback_add(securityUG->win_main_layout,
218                                        EVAS_CALLBACK_RESIZE,
219                                        setting_security_ug_cb_resize,
220                                        securityUG);
221
222         return securityUG->ly_main;
223 }
224
225 static void setting_security_ug_on_start(ui_gadget_h ug, service_h service,
226                                          void *priv)
227 {
228 }
229
230 static void setting_security_ug_on_pause(ui_gadget_h ug, service_h service,
231                                          void *priv)
232 {
233 }
234
235 static void setting_security_ug_on_resume(ui_gadget_h ug, service_h service,
236                                           void *priv)
237 {
238 }
239
240 static void setting_security_ug_on_destroy(ui_gadget_h ug, service_h service,
241                                            void *priv)
242 {
243         setting_retm_if((!priv), "!priv");
244         SettingSecurityUG *securityUG = priv;
245
246         evas_object_event_callback_del(securityUG->win_main_layout, EVAS_CALLBACK_RESIZE, setting_security_ug_cb_resize);       /* fix flash issue for gallery */
247         securityUG->ug = ug;
248         if (securityUG->handle && tel_deinit(securityUG->handle) != TAPI_API_SUCCESS) {
249                 SETTING_TRACE_DEBUG
250                     ("%s*** [ERR] setting_network_unsubscribe_tapi_events. ***%s",
251                      SETTING_FONT_RED, SETTING_FONT_BLACK);
252         }
253
254         /*  delete the allocated objects. */
255         if (TRUE == setting_view_security_sim_settings.is_create) {
256                 setting_view_destroy(&setting_view_security_sim_settings, securityUG);
257         }
258         if (TRUE == setting_view_security_main.is_create) {
259                 setting_view_destroy(&setting_view_security_main, securityUG);
260         }
261
262         if (NULL != ug_get_layout(securityUG->ug)) {
263                 evas_object_hide((Evas_Object *) ug_get_layout(securityUG->ug));
264                 evas_object_del((Evas_Object *) ug_get_layout(securityUG->ug));
265         }
266 }
267
268 static void setting_security_ug_on_message(ui_gadget_h ug, service_h msg,
269                                            service_h service, void *priv)
270 {
271
272 }
273
274 static void setting_security_ug_on_event(ui_gadget_h ug,
275                                          enum ug_event event, service_h service,
276                                          void *priv)
277 {
278         switch (event) {
279         case UG_EVENT_LOW_MEMORY:
280                 break;
281         case UG_EVENT_LOW_BATTERY:
282                 break;
283         case UG_EVENT_LANG_CHANGE:
284                 break;
285         case UG_EVENT_ROTATE_PORTRAIT:
286                 break;
287         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
288                 break;
289         case UG_EVENT_ROTATE_LANDSCAPE:
290                 break;
291         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
292                 break;
293         case UG_EVENT_REGION_CHANGE:
294                 break;
295         default:
296                 break;
297         }
298 }
299
300 static void setting_security_ug_on_key_event(ui_gadget_h ug,
301                                              enum ug_key_event event,
302                                              service_h service, void *priv)
303 {
304         if (!priv)
305                 return;
306         SettingSecurityUG *ad = (SettingSecurityUG *) priv;     /* ad is point to priv */
307         switch (event) {
308         case UG_KEY_EVENT_END:
309                 {
310                         if (elm_naviframe_top_item_get(ad->navi_bar) ==
311                            elm_naviframe_bottom_item_get(ad->navi_bar)) {
312                                 ug_destroy_me(ug);
313                         } else {
314                                 /* elm_naviframe_item_pop(ad->navi_bar); */
315                                 setting_view_cb_at_endKey(ad);
316                         }
317                 }
318                 break;
319
320         default:
321                 break;
322         }
323 }
324
325 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
326 {
327         SettingSecurityUG *securityUG = calloc(1, sizeof(SettingSecurityUG));
328         setting_retvm_if(!securityUG, -1,
329                          "Create SettingSecurityUG obj failed");
330
331         ops->create = setting_security_ug_on_create;
332         ops->start = setting_security_ug_on_start;
333         ops->pause = setting_security_ug_on_pause;
334         ops->resume = setting_security_ug_on_resume;
335         ops->destroy = setting_security_ug_on_destroy;
336         ops->message = setting_security_ug_on_message;
337         ops->event = setting_security_ug_on_event;
338         ops->key_event = setting_security_ug_on_key_event;
339         ops->priv = securityUG;
340         ops->opt = UG_OPT_INDICATOR_ENABLE;
341
342         return 0;
343 }
344
345 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
346 {
347         struct SettingSecurityUG *securityUG;
348         setting_retm_if(!ops, "ops == NULL");
349
350         securityUG = ops->priv;
351         if (securityUG)
352                 FREE(securityUG);
353 }
354
355 /* ***************************************************
356  *
357  *general func
358  *
359  ***************************************************/
360 void setting_security_sim_get_facility_cb(TapiHandle *handle, int result, void *data, void *user_data)
361 {
362         SETTING_TRACE_BEGIN;
363         ret_if(!data || !user_data);
364         TelSimPinOperationResult_t sec_rt = result;
365         TelSimFacilityInfo_t *fi = data;
366         SettingSecurityUG *ad = user_data;
367
368         SETTING_TRACE("sec_rt[%d]", sec_rt);
369         SETTING_TRACE("fi->type[%d]", fi->type);
370         SETTING_TRACE("fi->f_status[%d]", fi->f_status);
371         if (sec_rt == TAPI_SIM_PIN_OPERATION_SUCCESS)
372         {
373                 SETTING_TRACE_DEBUG("PIN_OPERATION_SUCCESS");
374                 switch (fi->type)
375                 {
376                     case TAPI_SIM_LOCK_PS://sim status
377                         ad->sim_status = fi->f_status;
378                         break;
379                     case TAPI_SIM_LOCK_SC://pin1 status
380                         ad->pin1_status = fi->f_status;
381                         break;
382                     case TAPI_SIM_LOCK_FD://pin2/fdn status
383                         ad->pin2_status = fi->f_status;
384                         break;
385                     default:
386                         break;
387                 }
388         }
389         else
390         {
391                 //if failed, do nothing..
392         }
393
394         /* Update SIM Settings view if created. */
395         if(setting_view_security_sim_settings.is_create == 1)
396         {
397                 SETTING_TRACE_DEBUG("SIM settings view is already created. [%d]", setting_view_security_sim_settings.is_create);
398                 setting_view_update(&setting_view_security_sim_settings, ad);
399         }
400         SETTING_TRACE_END;
401 }
402
403 void setting_security_layout_passwd_ug_cb(ui_gadget_h ug,
404                                                  enum ug_mode mode, void *priv)
405 {
406         if (!priv)
407                 return;
408
409         SettingSecurityUG *ad = (SettingSecurityUG *) priv;
410         Evas_Object *base = ug_get_layout(ug);
411         if (!base)
412                 return;
413
414         switch (mode) {
415         case UG_MODE_FULLVIEW:
416                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
417                                                  EVAS_HINT_EXPAND);
418                 elm_win_resize_object_add(ad->win_get, base);
419                 evas_object_show(base);
420                 break;
421         default:
422                 break;
423         }
424
425         return;
426 }
427
428 void setting_security_destroy_password_ug_cb(ui_gadget_h ug,
429                                                     void *priv)
430 {
431         SETTING_TRACE_BEGIN;
432         ret_if(priv == NULL);
433         SettingSecurityUG *ad = (SettingSecurityUG *) priv;
434         if (ug) {
435                 ug_destroy(ug);
436                 ad->ug_passwd = NULL;
437         }
438 }
439
440 gboolean setting_security_create_password_sg(void *data)
441 {
442         SETTING_TRACE_BEGIN;
443         /* error check */
444         retv_if(data == NULL, FALSE);
445
446         SettingSecurityUG *ad = (SettingSecurityUG *) data;     /* ad is point to data */
447
448         /* prevent the ug from being loaded again due to window event queuing */
449         /* added by JTS: CQ H0100135346 */
450         if (ad->ug_passwd) {
451                 SETTING_TRACE("Password UG is already loaded.");
452                 return FALSE;
453         }
454
455         if((ad->pw_type < 0) || (ad->pw_type > SETTING_SEC_PW_MAX))
456                 return FALSE;
457
458         char str[Max_Passwd_View_Type_Len + 1] = { 0, };
459         safeCopyStr(str, security_table[ad->pw_type].pw_type_string, Max_Passwd_View_Type_Len);
460
461         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
462
463         if (!cbs)
464                 return FALSE;
465         cbs->layout_cb = setting_security_layout_passwd_ug_cb;
466         cbs->result_cb = setting_security_result_password_ug_cb;
467         cbs->destroy_cb = setting_security_destroy_password_ug_cb;
468         cbs->priv = (void *)ad;
469
470         service_h svc;
471         if (service_create(&svc)) {
472                 FREE(cbs);
473                 return FALSE;
474         }
475
476         service_add_extra_data(svc, "viewtype", str);
477         if(ad->input_pwd)
478         {
479                 SETTING_TRACE_DEBUG("Send current : %s", ad->input_pwd);
480                 service_add_extra_data(svc, "current", ad->input_pwd);
481         }
482         ad->ug_passwd = ug_create(ad->ug, "setting-password-efl", UG_MODE_FULLVIEW, svc, cbs);
483         if (NULL == ad->ug_passwd) {    /* error handling */
484                 evas_object_show(ad->ly_main);
485         }
486
487         service_destroy(svc);
488         FREE(cbs);
489
490         return TRUE;
491 }
492
493 /* ***************************************************
494  *
495  *call back func
496  *
497  ***************************************************/
498 // -1   on fail
499 // 0~n  on success
500 int _get_security_table_index(char* name)
501 {
502         int i=0;
503
504         for(i=0;i<SETTING_SEC_PW_MAX;i++)
505         {
506                 if (0 == safeStrCmp(name, security_table[i].pw_type_string))
507                         return i;
508         }
509         return -1;
510 }
511
512 void
513 setting_security_result_password_ug_cb(ui_gadget_h ug, service_h service,
514                                        void *priv)
515 {
516         SETTING_TRACE_BEGIN;
517         /* error check */
518         retm_if(priv == NULL, "Data paremeter is NULL");
519
520         SettingSecurityUG *ad = (SettingSecurityUG *) priv;     /* ad is point to priv */
521
522         char *result = NULL;
523         char *current = NULL;
524
525         service_get_extra_data(service, "result", &result);
526         service_get_extra_data(service, "current", &current);
527
528         /* divided pw view type */
529         SETTING_TRACE("ad->pw_type: %d result :%s", ad->pw_type, result);
530         ret_if(NULL == result);
531
532         if(safeStrCmp(result, "Cancel") == 0)
533         {
534                 /* To do : rollback previouse status. */
535         }
536         else
537         {
538                 int index =  _get_security_table_index(result);
539                 int ret;
540                 if (index != -1)
541                 {
542                         ret = security_table[index].passwd_handler(ad, NULL);
543                 } else {
544                         SETTING_TRACE_ERROR("no table entry");
545                 }
546                 if(ad->input_pwd)
547                         FREE(ad->input_pwd);
548         }
549
550         evas_object_show(ad->ly_main);
551         SETTING_TRACE_END;
552 }
553
554 #ifdef Status
555 #undef Status
556 #endif
557
558
559 /*****/
560 UG_MODULE_API int setting_plugin_reset(service_h service, void *priv)
561 {
562         SETTING_TRACE_BEGIN;
563         int ret = 0;
564 #if SUPPORT_RUN_SYSTEM_COMMAND
565         ret += excuteCmd(SETTING_POSTINST_FILE, 1, "security");
566 #else
567
568         /* it may write some stragne thing into /opt/data/setting/set_info */
569         /* setting_store_init_password(""); */
570         struct stat st;
571         if (stat(PWD_FILE, &st) == 0)//file exist
572         {
573                 if (remove(PWD_FILE) != 0) //delete the file
574                 {
575                         perror("remove");
576                         SETTING_TRACE("Failed to remove %s", PWD_FILE);
577                         ret += -1;
578                 }
579         }
580
581         #if SUPPORT_RCS_FOR_DEMO
582         ret += vconf_set_bool("db/setting/rcs", FALSE);
583         #endif
584 #endif
585         return ret;
586 }
587